import Base import ../tensor_indexed.bend as Indexed import ../traversal_array.bend as Traversal import ../traversal_loop.bend as Loop import ./storage_tree_model.bend as Tree import ./storage_certificate.bend as Certificate import ./storage_witness.bend as Witness import ./traversal_witness.bend as TraversalWitness import ./traversal_certificate.bend as TraversalCertificate def sample(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~operation: U32 -> Element -> Element, -input: Array,+context: Context,+position: U32,-result: Array & Element, evidence: TraversalWitness.Sample,Element,input,result>) -> TraversalWitness.Sample,Element,input,Indexed.value(~Element,~Context,~index,~operation,context,position,result)>: match evidence: case TraversalWitness.Sample{value,equation}: %Equal.sym(Array & Element,result,(input,value),equation) : TraversalWitness.Sample,Element,input,Indexed.value(~Element,~Context,~index,~operation,context,position,_)> TraversalWitness.Sample{operation(index(context,position),value),{==}} def read(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~operation: U32 -> Element -> Element, -input: Array,context: Context,position: U32,evidence: Witness.ArrayWitness) -> TraversalWitness.Sample,Element,input,Indexed.read(~Element,~Context,~index,~operation,input,context,position)>: +position = position sample(~Element,~Context,~index,~operation,input,context,position,Array.get(Element,input,position), TraversalWitness.array_sample(~Element,input,Array.get(Element,input,position),Witness.observed(Element,input,position,evidence))) def preserves(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~operation: U32 -> Element -> Element, +depth: Nat,call: Indexed.Call,-input: Array,-output: Array,input_certificate: Certificate.Certificate, equation: {output == Tree.pack(Element,Certificate.normalized(Element,depth,Tree.reflect(Element,output))) : Array}) -> TraversalWitness.ResultEvidence,Element,input,depth,Indexed.run(~Element,~Context,~index,~operation,call,input,output)>: Indexed.Call{count,context} = call TraversalCertificate.completed(~Array,~Element,~Context,~TraversalWitness.array_evidence(~Element), ~Traversal.run(~Array,~Element,~Context,~Indexed.read(~Element,~Context,~index,~operation),~Indexed.destination(~Context)), ~TraversalWitness.run(~Array,~Element,~Context,~TraversalWitness.array_evidence(~Element),~Indexed.read(~Element,~Context,~index,~operation), ~Indexed.destination(~Context),~read(~Element,~Context,~index,~operation)), count,0,context,input,output,Certificate.witness(Element,input,input_certificate),depth,equation) def certificates(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~operation: U32 -> Element -> Element, +call: Indexed.Call,-input: Array,-output: Array,input_certificate: Certificate.Certificate, output_certificate: Certificate.Certificate) -> TraversalCertificate.Certificates,Element,Certificate.family(~Element),Indexed.run(~Element,~Context,~index,~operation,call,input,output)>: Certificate.Certificate{+depth,+equation} = input_certificate TraversalCertificate.operation(~Array,~Element,~Indexed.Call,~Certificate.family(~Element), ~Indexed.run(~Element,~Context,~index,~operation),~preserves(~Element,~Context,~index,~operation), call,input,output,Certificate.Certificate{depth,equation},Certificate.Certificate{depth,equation},Certificate.Certificate{depth,equation},output_certificate) def next_block(~Element: Data,~Context: Data,+depth: Nat,block: U32,size: U32,context: Context,-input: Array,-state: Traversal.State<&1,&1,Array,Array,Indexed.Block>, evidence: TraversalWitness.StateEvidence,Element,Indexed.Block,input,depth,state>) -> TraversalWitness.StateEvidence,Element,Indexed.Blocks,input,depth,Indexed.next_block(~Element,~Context,block,size,context,state)>: match evidence: case TraversalWitness.StateEvidence{output,position,inner,equation,output_evidence}: %Equal.sym(Traversal.State<&1,&1,Array,Array,Indexed.Block>,state,Traversal.State{input,output,position,inner},equation) : TraversalWitness.StateEvidence,Element,Indexed.Blocks,input,depth,Indexed.next_block(~Element,~Context,block,size,context,_)> TraversalWitness.StateEvidence{output,position,Indexed.Blocks{(block + 1 : U32),size,context},{==},output_evidence} def block_step(~Element: Data,~Context: Data,~base: Context -> U32 -> U32,~operation: U32 -> Element -> Element,+depth: Nat,-input: Array,-state: Traversal.State<&1,&1,Array,Array,Indexed.Blocks>, +input_evidence: Witness.ArrayWitness,evidence: TraversalWitness.StateEvidence,Element,Indexed.Blocks,input,depth,state>) -> TraversalWitness.StateEvidence,Element,Indexed.Blocks,input,depth,Indexed.block_step(~Element,~Context,~base,~operation,state)>: match evidence: case TraversalWitness.StateEvidence{output,+position,Indexed.Blocks{+block,+size,+context},equation,output_evidence}: %Equal.sym(Traversal.State<&1,&1,Array,Array,Indexed.Blocks>,state,Traversal.State{input,output,position,Indexed.Blocks{block,size,context}},equation) : TraversalWitness.StateEvidence,Element,Indexed.Blocks,input,depth,Indexed.block_step(~Element,~Context,~base,~operation,_)> next_block(~Element,~Context,depth,block,size,context,input, Traversal.run(~Array,~Element,~Indexed.Block,~Indexed.read(~Element,~Indexed.Block,~Indexed.block_index,~operation),~Indexed.destination(~Indexed.Block),U32.to_nat(size), Traversal.State{input,output,position,Indexed.Block{base(context,block),position}}), TraversalWitness.run(~Array,~Element,~Indexed.Block,~TraversalWitness.array_evidence(~Element),~Indexed.read(~Element,~Indexed.Block,~Indexed.block_index,~operation), ~Indexed.destination(~Indexed.Block),~read(~Element,~Indexed.Block,~Indexed.block_index,~operation),depth,U32.to_nat(size),input, Traversal.State{input,output,position,Indexed.Block{base(context,block),position}},input_evidence, TraversalWitness.StateEvidence{output,position,Indexed.Block{base(context,block),position},{==},output_evidence})) def block_loop(~Element: Data,~Context: Data,~base: Context -> U32 -> U32,~operation: U32 -> Element -> Element,+depth: Nat,count: Nat,-input: Array,-state: Traversal.State<&1,&1,Array,Array,Indexed.Blocks>, +input_evidence: Witness.ArrayWitness,evidence: TraversalWitness.StateEvidence,Element,Indexed.Blocks,input,depth,state>) -> TraversalWitness.StateEvidence,Element,Indexed.Blocks,input,depth, Loop.run(~Traversal.State<&1,&1,Array,Array,Indexed.Blocks>,~Indexed.block_step(~Element,~Context,~base,~operation),count,state)>: match count: case 0n: evidence case 1n+rest: block_loop(~Element,~Context,~base,~operation,depth,rest,input,Indexed.block_step(~Element,~Context,~base,~operation,state),input_evidence, block_step(~Element,~Context,~base,~operation,depth,input,state,input_evidence,evidence)) type Tiling<-Context: Data> is Data: Tiling{total: U32,context: Context} def tiled_operation(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~base: Context -> U32 -> U32,~blocks: Context -> U32,~size: Context -> U32,~operation: U32 -> Element -> Element, tiling: Tiling,input: Array,output: Array) -> Array & Array: Tiling{total,context} = tiling Indexed.run_tiled(~Element,~Context,~index,~base,~blocks,~size,~operation,total,context,input,output) def whole_preserves(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~base: Context -> U32 -> U32,~operation: U32 -> Element -> Element, +depth: Nat,whole: Bool,blocks: U32,size: U32,total: U32,context: Context,-input: Array,-output: Array,input_certificate: Certificate.Certificate, equation: {output == Tree.pack(Element,Certificate.normalized(Element,depth,Tree.reflect(Element,output))) : Array}) -> TraversalWitness.ResultEvidence,Element,input,depth,Indexed.run_whole(~Element,~Context,~index,~base,~operation,whole,blocks,size,total,context,input,output)>: match whole: case True{}: TraversalCertificate.completed(~Array,~Element,~Indexed.Blocks,~TraversalWitness.array_evidence(~Element), ~Loop.run(~Traversal.State<&1,&1,Array,Array,Indexed.Blocks>,~Indexed.block_step(~Element,~Context,~base,~operation)),~block_loop(~Element,~Context,~base,~operation), U32.to_nat(blocks),0,Indexed.Blocks{0,size,context},input,output,Certificate.witness(Element,input,input_certificate),depth,equation) case False{}: preserves(~Element,~Context,~index,~operation,depth,Indexed.Call{U32.to_nat(total),context},input,output,input_certificate,equation) def tiled_preserves(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~base: Context -> U32 -> U32,~blocks: Context -> U32,~size: Context -> U32,~operation: U32 -> Element -> Element, +depth: Nat,tiling: Tiling,-input: Array,-output: Array,input_certificate: Certificate.Certificate, equation: {output == Tree.pack(Element,Certificate.normalized(Element,depth,Tree.reflect(Element,output))) : Array}) -> TraversalWitness.ResultEvidence,Element,input,depth,tiled_operation(~Element,~Context,~index,~base,~blocks,~size,~operation,tiling,input,output)>: Tiling{+total,+context} = tiling whole_preserves(~Element,~Context,~index,~base,~operation,depth,U32.is_le(total,Indexed.block_limit()) && Nat.is_eq(Nat.mul(U32.to_nat(blocks(context)),U32.to_nat(size(context))),U32.to_nat(total)), blocks(context),size(context),total,context,input,output,input_certificate,equation) def tiled_certificates(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~base: Context -> U32 -> U32,~blocks: Context -> U32,~size: Context -> U32,~operation: U32 -> Element -> Element, +total: U32,+context: Context,-input: Array,-output: Array,input_certificate: Certificate.Certificate, output_certificate: Certificate.Certificate) -> TraversalCertificate.Certificates,Element,Certificate.family(~Element),Indexed.run_tiled(~Element,~Context,~index,~base,~blocks,~size,~operation,total,context,input,output)>: Certificate.Certificate{+depth,+equation} = input_certificate TraversalCertificate.operation(~Array,~Element,~Tiling,~Certificate.family(~Element), ~tiled_operation(~Element,~Context,~index,~base,~blocks,~size,~operation),~tiled_preserves(~Element,~Context,~index,~base,~blocks,~size,~operation), Tiling{total,context},input,output,Certificate.Certificate{depth,equation},Certificate.Certificate{depth,equation},Certificate.Certificate{depth,equation},output_certificate)