Documentation

Complexity.Language.List.Fold.Program

A callable list fold retaining its actual callback #

List.Fold.program adds the shared list traversal to an existing source program. The selected callback and every original function keep their implementations, including recursive calls, through the existing typed program embedding.

The correctness contract relates the result to native List.foldl. The callback may change the heap: the immutable list observation is preserved by source execution, not by an additional whole-heap-unchanged callback assumption.

def Complexity.Language.List.Fold.calleeMap (accTy : Ty) (kind : CellTy) (signatures : List Signature) :
SignatureMap signatures (foldSignature accTy kind :: signatures)

Original functions follow the single newly added fold entry.

Equations
Instances For
    def Complexity.Language.List.Fold.entry (accTy : Ty) (kind : CellTy) (signatures : List Signature) :
    Fin (foldSignature accTy kind :: signatures).length

    The new callable fold occupies the first function-table position.

    Equations
    Instances For
      def Complexity.Language.List.Fold.calleeEntry {signatures : List Signature} (accTy : Ty) (kind : CellTy) (fn : Fin signatures.length) :
      Fin (foldSignature accTy kind :: signatures).length

      The selected original callback in the extended function table.

      Equations
      Instances For
        @[simp]
        theorem Complexity.Language.List.Fold.entry_signature {signatures : List Signature} {accTy : Ty} {kind : CellTy} :
        (foldSignature accTy kind :: signatures)[entry accTy kind signatures] = foldSignature accTy kind
        @[simp]
        theorem Complexity.Language.List.Fold.calleeEntry_val {signatures : List Signature} {accTy : Ty} {kind : CellTy} (fn : Fin signatures.length) :
        ↑(calleeEntry accTy kind fn) = 1 + ↑fn
        theorem Complexity.Language.List.Fold.callee_signature {signatures : List Signature} {accTy : Ty} {kind : CellTy} {fn : Fin signatures.length} (same : signatures[fn] = stepSignature accTy kind) :
        (foldSignature accTy kind :: signatures)[calleeEntry accTy kind fn] = stepSignature accTy kind

        Relocation retains the selected callback's complete parameter/result type.

        def Complexity.Language.List.Fold.program {signatures : List Signature} {accTy : Ty} {kind : CellTy} (source : Program signatures) (fn : Fin signatures.length) (same : signatures[fn] = stepSignature accTy kind) :
        Program (foldSignature accTy kind :: signatures)

        Add one actual traversal, retaining every original implementation. The function index and its checked signature select the runtime callback.

        Equations
        Instances For
          theorem Complexity.Language.List.Fold.program_body {signatures : List Signature} {accTy : Ty} {kind : CellTy} (source : Program signatures) (fn : Fin signatures.length) (same : signatures[fn] = stepSignature accTy kind) :
          (program source fn same).body (entry accTy kind signatures) = body (calleeEntry accTy kind fn) ⋯

          The fold entry is exactly the shared source traversal with a real call to the relocated callback.

          theorem Complexity.Language.List.Fold.program_embeds {signatures : List Signature} {accTy : Ty} {kind : CellTy} (source : Program signatures) (fn : Fin signatures.length) (same : signatures[fn] = stepSignature accTy kind) :
          source.Embeds (calleeMap accTy kind signatures) (program source fn same)

          Every original function retains its body and actual renamed call graph.

          theorem Complexity.Language.List.Fold.calleeBody_renameCalls {accTy : Ty} {kind : CellTy} {source target : List Signature} {sourceProgram : Program source} {targetProgram : Program target} {map : SignatureMap source target} (embedded : sourceProgram.Embeds map targetProgram) {fn : Fin source.length} (same : source[fn] = stepSignature accTy kind) :
          calleeBody targetProgram (map.toFun fn) ⋯ = Stmt.renameCalls map (calleeBody sourceProgram fn same)

          The callback body observed at its declared step signature is the actual renamed original body. Resource contracts may therefore use the same embedding.

          theorem Complexity.Language.List.Fold.Contract.renameCalls {accTy : Ty} {kind : CellTy} {α : Type u} {R : Representation α accTy} {source target : List Signature} {sourceProgram : Program source} {targetProgram : Program target} {map : SignatureMap source target} {fn : Fin source.length} {same : source[fn] = stepSignature accTy kind} {step : α → CellValue kind → α} {domain : α → CellValue kind → Prop} (callee : Contract sourceProgram fn same R step domain) (embedded : sourceProgram.Embeds map targetProgram) :
          Contract targetProgram (map.toFun fn) ⋯ R step domain

          Reuse the complete represented callback contract through an actual source-table embedding, with the same mathematical domain and heap effects.

          theorem Complexity.Language.List.Fold.callee_contract {signatures : List Signature} {accTy : Ty} {kind : CellTy} {α : Type u} {R : Representation α accTy} (source : Program signatures) (fn : Fin signatures.length) (same : signatures[fn] = stepSignature accTy kind) {step : α → CellValue kind → α} {domain : α → CellValue kind → Prop} (callee : Contract source fn same R step domain) :
          Contract (program source fn same) (calleeEntry accTy kind fn) ⋯ R step domain

          The actual relocated callback reuses its original function contract.

          theorem Complexity.Language.List.Fold.program_total {signatures : List Signature} {accTy : Ty} {kind : CellTy} {α : Type u} {R : Representation α accTy} {source : Program signatures} {fn : Fin signatures.length} {same : signatures[fn] = stepSignature accTy kind} {step : α → CellValue kind → α} {domain : α → CellValue kind → Prop} (callee : Contract source fn same R step domain) (initial : α) (values : List (CellValue kind)) (allowed : Admissible step domain initial values) :
          FunctionTotal (program source fn same) (entry accTy kind signatures) (fun (args : Env (foldSignature accTy kind :: signatures)[entry accTy kind signatures].params) (heap : Heap) => R.Rel initial args.head heap ∧ (Representation.list kind).Rel values args.tail.head heap) fun (args : Env (foldSignature accTy kind :: signatures)[entry accTy kind signatures].params) (heap : Heap) (result : Value (foldSignature accTy kind :: signatures)[entry accTy kind signatures].result) (finish : Heap) => R.Rel (List.foldl step initial values) result finish ∧ (Representation.list kind).Rel values args.tail.head finish ∧ heap.ShapeExtends finish

          The callable entry returns native folded contents. The original list stays observable in the actual final heap even when callbacks allocate or mutate other source objects.

          def Complexity.Language.List.Fold.representation {accTy : Ty} {α : Type u} (R : Representation α accTy) (kind : CellTy) :
          FunctionRepresentation (α × List (CellValue kind)) (fun (x : α × List (CellValue kind)) => α) (foldSignature accTy kind)

          Mathematical accumulator/list arguments and an observed accumulator result.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            theorem Complexity.Language.List.Fold.program_refines {signatures : List Signature} {accTy : Ty} {kind : CellTy} {α : Type u} {R : Representation α accTy} {source : Program signatures} {fn : Fin signatures.length} {same : signatures[fn] = stepSignature accTy kind} {step : α → CellValue kind → α} {domain : α → CellValue kind → Prop} (callee : Contract source fn same R step domain) :
            RepresentedFunction.Refines (program source fn same) (entry accTy kind signatures) (representation R kind) (fun (input : α × List (CellValue kind)) => Admissible step domain input.1 input.2) fun (input : α × List (CellValue kind)) => List.foldl step input.1 input.2

            The same callable source program refines ordinary List.foldl. Only the callback's mathematical domain is required along the actual fold prefixes.