Documentation

Examples.Language.LinkedListFoldAllocation

Allocating native list folds on RAM #

The actual native reverseAppend calls the shared fold with the actual native ListReducer.push callback. Each callback calls the shared list constructor and retains three arena words. The existing source refinement supplies ordinary values.reverse ++ tail; the existing fold resource theorem supplies traversal, callback and allocation bounds for that same source implementation.

The complete invocation retains its real final memory, placement and cursor. Original list roots remain observable, including shared tails. The reservation bounds retained arena growth; it is not a claim about exact peak live space.

Infer the actual callback's constructor call and return costs from its body, using the constructor's independent cost certificate.

Equations
  • One or more equations did not get rendered due to their size.
Instances For

    The callback's inferred body count, before its two initialization instructions. Its exact equality is established by the measured execution below.

    Equations
    Instances For
      theorem Complexity.Language.Examples.LinkedList.push_ready_cost {w heapLimit cursor : ℕ} (accumulator : Option (NodeRef CellTy.nat)) (head : ℕ) (heap : Heap) (positive : 0 < w) (headFits : head < 2 ^ w) (space : cursor + 3 ≤ heapLimit) :
      ∃ (execution : Exec ListReducer.Source.program (ListReducer.Source.program.body ListReducer.Source.pushId) { locals := ListReducer.Source.push_args accumulator head, heap := heap } { locals := ListReducer.Source.push_args accumulator head, heap := (heap.cons head accumulator).2 } (Control.returned (some (heap.cons head accumulator).1))) (ready : Ram.LanguageCompiler.ArenaReady execution w heapLimit 1 cursor (cursor + 3)), Ram.LanguageCompiler.ArenaExecutionCost ready pushBodySteps

      The native callback reuses the constructor-call rule with its two arguments in callback order. Its result shares the original tail in the actual new heap.

      Each actual push reserves three words, independently of the length or sharing of the represented accumulator. Its mathematical domain imposes no heap limit.

      The callback's bound observes its existing call, allocation, copy and return rules. It includes initialization once and needs no comparison heap or capacity assumption beyond the readiness already supplied for the observed execution.

      The fold's complete callable-body bound is affine in the traversed list length. Its coefficient uses the actual relocated callback and compiler costs.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For

        Constant callback costs specialize the shared fold bound without proving another traversal or pricing a native List.foldl operation.

        The fold's existing affine bound at a fixed mathematical input length. Only the represented input and callback domain occur in its precondition; readiness and spare capacity are not needed to bound an already given execution.

        def Complexity.Language.Examples.LinkedList.reverseAppendCost (length : ℕ) :
        { bound : ℕ // ∀ (w heapLimit : ℕ) (values tailValues : List ℕ) (root tail : Option (NodeRef CellTy.nat)) (heap : Heap), values.length = length → (Representation.list CellTy.nat).Rel values root heap → (Representation.list CellTy.nat).Rel tailValues tail heap → Ram.LanguageCompiler.StmtArenaCostBound NativeLists.Source.program w heapLimit 3 (NativeLists.Source.program.body NativeLists.Source.reverseAppendId) { locals := NativeLists.Source.reverseAppend_args root tail, heap := heap } bound }

        Infer the native wrapper's bound from a length-indexed fold certificate. The mathematical inputs select that certificate; only the two roots are passed to the real source function. No wrapper call-table or field-count formula is supplied.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For

          The complete native invocation adds its own initialization, outer call and halt exactly once. The fold and callback bounds already include their own calls.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            theorem Complexity.Language.Examples.LinkedList.reverseAppend_measured {w heapLimit cursor : ℕ} (values tailValues : List ℕ) (root tail : Option (NodeRef CellTy.nat)) (heap : Heap) (positive : 0 < w) (observed : (Representation.list CellTy.nat).Rel values root heap) (tailObserved : (Representation.list CellTy.nat).Rel tailValues tail heap) (headFits : ∀ head ∈ values, head < 2 ^ w) (space : cursor + 3 * values.length ≤ heapLimit) :

            Compose the existing fold certificate directly with the actual wrapper's return. The retained observation concerns its real final cursor; the independent structural cost certificate can be applied without unpacking the execution.

            theorem Complexity.Language.Examples.LinkedList.reverseAppend_ready_cost {w heapLimit cursor : ℕ} (values tailValues : List ℕ) (root tail : Option (NodeRef CellTy.nat)) (heap : Heap) (positive : 0 < w) (observed : (Representation.list CellTy.nat).Rel values root heap) (tailObserved : (Representation.list CellTy.nat).Rel tailValues tail heap) (headFits : ∀ head ∈ values, head < 2 ^ w) (space : cursor + 3 * values.length ≤ heapLimit) :

            The actual generated caller composes the independently specified allocating fold with its return. Readiness begins at the real current cursor, and the cost and cursor bounds retain the same source execution throughout.

            theorem Complexity.Language.Examples.LinkedList.reverseAppend_execute {w heapLimit cursor : ℕ} {placement : ℕ → Ram.Word w} (values tailValues : List ℕ) (root tail : Option (NodeRef CellTy.nat)) {heap : Heap} (observed : (Representation.list CellTy.nat).Rel values root heap) (tailObserved : (Representation.list CellTy.nat).Rel tailValues tail heap) {entry : Ram.Source.State w} (launch : Ram.LanguageCompiler.FunctionArenaLaunch NativeLists.Source.program NativeLists.Source.reverseAppendId 3 heapLimit placement (NativeLists.Source.reverseAppend_args root tail) heap cursor entry) (space : cursor + 3 * values.length ≤ heapLimit) :

            The actual native reverseAppend halts with its ordinary mathematical result. The original lists remain observable in the same extended heap, and the returned execution carries the complete final RAM memory for subsequent calls. No copying of the original tail or disjointness of the two input roots is needed.

            Finite-word ranges of traversed heads come from the actual launch heap. The only additional arena allowance is three words per traversed element; the full time bound includes all three internal call levels and the final outer halt.

            Reuse the actual reverse/append wrapper's inferred bound as a callable certificate. Only its mathematical input length and current list observations select the certificate; the arguments remain the two actual roots.

            Infer the actual reverse wrapper's body bound from its call to reverseAppend. Constructing its empty tail, returning the result and the existing call-frame work are all supplied by structural compiler rules.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For

              The existing native reverse is available to later source calls through its inferred length-dependent bound, including initialization exactly once.

              theorem Complexity.Language.Examples.LinkedList.reverse_measured {w heapLimit cursor : ℕ} (values : List ℕ) (root : Option (NodeRef CellTy.nat)) (heap : Heap) (positive : 0 < w) (observed : (Representation.list CellTy.nat).Rel values root heap) (headFits : ∀ head ∈ values, head < 2 ^ w) (space : cursor + 3 * values.length ≤ heapLimit) :

              The native reverse wrapper composes its existing measured callee directly. Its returned root and real intermediate heap remain available for a later fold, without transporting operational witnesses through the caller.

              theorem Complexity.Language.Examples.LinkedList.reverse_ready_cost {w heapLimit cursor : ℕ} (values : List ℕ) (root : Option (NodeRef CellTy.nat)) (heap : Heap) (positive : 0 < w) (observed : (Representation.list CellTy.nat).Rel values root heap) (headFits : ∀ head ∈ values, head < 2 ^ w) (space : cursor + 3 * values.length ≤ heapLimit) :

              Execute the actual native reverse around the already measured reverse/append call. The real result, final heap and cursor are available to a subsequent caller; no list traversal or source-correctness proof is repeated.