Documentation

Examples.Language.TraversalCompiled

Compiling the named mutable traversal #

The source traversal and its ordinary array-map correctness theorem are reused unchanged. Realization checks the actual intermediate increments and call nesting; independent cost rules count the same helper, branches and stores. The compiler supplies register transport and the connection to actual execution.

The source helper's existing ordinary equation supplies its callable contract without a second proof of the addition.

The actual increment, not only its later clipped value, must fit the word backend. The helper has no nested calls.

The proved primitive, return and initialization charges bound this helper's actual generated body. This bound is independent of its termination proof.

A uniform guard budget inferred by the existing compiler-derived rules. The witness is chosen before the source locals and heap are introduced.

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

    Guard evaluation includes the current buffer length, comparison and its Boolean return, using the automatically inferred uniform budget.

    The actual body budget is inferred once from its bindings, read, supplied callee certificate, branches and store. No operation-price table is added.

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

      A body includes the real read, helper call, selected store and local index update. Its uniform bound needs no second contents or termination proof.

      The shared linear-loop budget uses the inferred guard and body costs; the compiler rule supplies normal-continuation and final-exit charges.

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

        The real loop has a linear potential: every round pays for its guard, body and loop control, and the remaining constant pays for the final false guard. The original source invariant supplies preservation, not new contents or termination reasoning in the cost proof.

        Guard realization only needs the compared natural values and its Boolean result to fit. No assumption on the array's contents is used by the guard.

        theorem Complexity.Language.Examples.Traversal.body_realizable {w : ℕ} (hw : 0 < w) (xs : Buffer CellTy.nat) (limit : ℕ) (contents : Array ℕ) (i : ℕ) (heap : Heap) (current : invariant xs limit contents i heap) (bound : i < contents.size) (lengthFits : xs.length < 2 ^ w) (limitFits : limit < 2 ^ w) (incrementFits : contents[i] + 1 < 2 ^ w) :

        Realize a single body using its current unread cell and the already proved helper contract. The ordinary source invariant supplies access validity; only actual intermediate ranges and one call level are added here.

        theorem Complexity.Language.Examples.Traversal.loop_realizable {w : ℕ} (hw : 0 < w) (xs : Buffer CellTy.nat) (limit : ℕ) (contents : Array ℕ) (i : ℕ) (heap : Heap) (current : invariant xs limit contents i heap) (lengthFits : xs.length < 2 ^ w) (limitFits : limit < 2 ^ w) (incrementsFit : ∀ (j : ℕ) (hj : j < contents.size), contents[j] + 1 < 2 ^ w) :

        Source totality already proves termination of this loop. The additional proof only checks guard/body realizability and reuses the source step's closed invariant; there is no second well-founded induction or time-budget premise.

        theorem Complexity.Language.Examples.Traversal.boundedMap_realizable {w : ℕ} (hw : 0 < w) (contents : Array ℕ) (incrementsFit : ∀ (j : ℕ) (hj : j < contents.size), contents[j] + 1 < 2 ^ w) :

        The traversal uses one call level for its helper. Its original values' increments, limit and view length are the genuine bounded-word obligations.

        The inferred complete traversal-body budget, shared by direct and imported callers. It follows the actual compiler without duplicating its structural constants.

        Equations
        Instances For

          The complete inferred body bound reuses the source contents solely to identify the traversal's already proved state transitions.

          The complete invocation budget includes the compiler's outer call and final halt exactly once. Its body budget is the inferred traversal certificate.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            theorem Complexity.Language.Examples.Traversal.boundedMap_execute {w heapLimit : ℕ} {placement : ℕ → Ram.Word w} (xs : Buffer CellTy.nat) (limit : ℕ) (contents : Array ℕ) {heap : Heap} (observed : xs.Contents heap contents) (incrementsFit : ∀ (j : ℕ) (hj : j < contents.size), contents[j] + 1 < 2 ^ w) {entry : Ram.Source.State w} (launch : Ram.LanguageCompiler.FunctionLaunch Implementation.program Implementation.boundedMapId 1 heapLimit placement (Implementation.boundedMap_args xs limit) heap entry) :
            ∃ (outcome : Ram.LanguageCompiler.FunctionExecution Implementation.program Implementation.boundedMapId heapLimit placement (Implementation.boundedMap_args xs limit) heap entry), xs.Contents outcome.heap (Array.map (fun (x : CellValue CellTy.nat) => min (x + 1) limit) contents) ∧ xs.PreservesOutside heap outcome.heap ∧ outcome.result.steps ≤ boundedMapInvocationBound contents.size

            The compiled traversal returns its ordinary array-map result and preserves every disjoint borrowed view in the same actual final heap. The shared typed outcome retains source evaluation, halted execution, physical memory and return facts; its independent instruction bound describes that very invocation. The launch keeps the original word ranges, represented input and two-frame capacity, while actual intermediate increments must separately fit.

            theorem Complexity.Language.Examples.Traversal.boundedMap_runUntil_le_frame {w heapLimit : ℕ} (hw : 0 < w) (placement : ℕ → Ram.Word w) (xs : Buffer CellTy.nat) (limit : ℕ) (sourceHeap : Heap) (contents : Array ℕ) (observed : xs.Contents sourceHeap contents) (lengthFits : xs.length < 2 ^ w) (limitFits : limit < 2 ^ w) (incrementsFit : ∀ (j : ℕ) (hj : j < contents.size), contents[j] + 1 < 2 ^ w) (entry : Ram.Source.State w) (represented : Ram.LanguageCompiler.HeapRep placement heapLimit sourceHeap entry) (codeCapacity : List.length (Ram.LanguageCompiler.lowerCode Implementation.program Implementation.boundedMapId) < 2 ^ w) (stackCapacity : heapLimit + 2 * Ram.ABI.frameSize (Ram.LanguageCompiler.programControl Implementation.program) < 2 ^ w) :
            ∃ (finalHeap : Heap) (targetFinish : Ram.Source.State w) (bodySteps : ℕ) (target : Ram.State w), Implementation.boundedMap xs limit sourceHeap = Part.some (Except.ok (), finalHeap) ∧ xs.Contents finalHeap (Array.map (fun (x : CellValue CellTy.nat) => min (x + 1) limit) contents) ∧ xs.PreservesOutside sourceHeap finalHeap ∧ Ram.Source.FunctionExec (Ram.LanguageCompiler.lowerProgram Implementation.program) heapLimit 1 (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.boundedMapId) (Ram.LanguageCompiler.envWords placement (Env.cons xs (Env.cons limit Env.empty))) entry [] targetFinish ∧ Ram.LanguageCompiler.HeapRep placement heapLimit finalHeap targetFinish ∧ Ram.LocalCompiler.Function.runUntil (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerProgram Implementation.program) (↑Implementation.boundedMapId) 3 heapLimit (Ram.LanguageCompiler.envWords placement (Env.cons xs (Env.cons limit Env.empty))) entry = some { state := target, steps := Ram.LocalCompiler.Function.callSteps (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.boundedMapId) bodySteps + 1, reason := Ram.StopReason.halted } ∧ Ram.LocalCompiler.Function.returnedValues 0 target = [] ∧ Ram.Source.State.Observes heapLimit 0 targetFinish target ∧ (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.boundedMapId).bodyTime (Ram.LanguageCompiler.lowerProgram Implementation.program) heapLimit (Ram.LanguageCompiler.envWords placement (Env.cons xs (Env.cons limit Env.empty))) entry = Part.some bodySteps ∧ bodySteps ≤ boundedMapBodyBound contents.size ∧ Ram.LocalCompiler.Function.callSteps (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.boundedMapId) bodySteps + 1 ≤ Ram.LocalCompiler.Function.callSteps (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.boundedMapId) (boundedMapBodyBound contents.size) + 1

            The compiled traversal halts with its ordinary array-map result and preserves every initially observed disjoint view in the same represented final heap. Its independent linear instruction bound describes that very invocation. Preloaded objects, intermediate increments, code and stack capacity are explicit; no operational time budget or algorithm-specific register proof is assumed.

            theorem Complexity.Language.Examples.Traversal.boundedMap_runUntil_le {w heapLimit : ℕ} (hw : 0 < w) (placement : ℕ → Ram.Word w) (xs : Buffer CellTy.nat) (limit : ℕ) (sourceHeap : Heap) (contents : Array ℕ) (observed : xs.Contents sourceHeap contents) (lengthFits : xs.length < 2 ^ w) (limitFits : limit < 2 ^ w) (incrementsFit : ∀ (j : ℕ) (hj : j < contents.size), contents[j] + 1 < 2 ^ w) (entry : Ram.Source.State w) (represented : Ram.LanguageCompiler.HeapRep placement heapLimit sourceHeap entry) (codeCapacity : List.length (Ram.LanguageCompiler.lowerCode Implementation.program Implementation.boundedMapId) < 2 ^ w) (stackCapacity : heapLimit + 2 * Ram.ABI.frameSize (Ram.LanguageCompiler.programControl Implementation.program) < 2 ^ w) :
            ∃ (finalHeap : Heap) (targetFinish : Ram.Source.State w) (bodySteps : ℕ) (target : Ram.State w), Implementation.boundedMap xs limit sourceHeap = Part.some (Except.ok (), finalHeap) ∧ xs.Contents finalHeap (Array.map (fun (x : CellValue CellTy.nat) => min (x + 1) limit) contents) ∧ Ram.Source.FunctionExec (Ram.LanguageCompiler.lowerProgram Implementation.program) heapLimit 1 (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.boundedMapId) (Ram.LanguageCompiler.envWords placement (Env.cons xs (Env.cons limit Env.empty))) entry [] targetFinish ∧ Ram.LanguageCompiler.HeapRep placement heapLimit finalHeap targetFinish ∧ Ram.LocalCompiler.Function.runUntil (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerProgram Implementation.program) (↑Implementation.boundedMapId) 3 heapLimit (Ram.LanguageCompiler.envWords placement (Env.cons xs (Env.cons limit Env.empty))) entry = some { state := target, steps := Ram.LocalCompiler.Function.callSteps (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.boundedMapId) bodySteps + 1, reason := Ram.StopReason.halted } ∧ Ram.LocalCompiler.Function.returnedValues 0 target = [] ∧ Ram.Source.State.Observes heapLimit 0 targetFinish target ∧ (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.boundedMapId).bodyTime (Ram.LanguageCompiler.lowerProgram Implementation.program) heapLimit (Ram.LanguageCompiler.envWords placement (Env.cons xs (Env.cons limit Env.empty))) entry = Part.some bodySteps ∧ bodySteps ≤ boundedMapBodyBound contents.size ∧ Ram.LocalCompiler.Function.callSteps (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.boundedMapId) bodySteps + 1 ≤ Ram.LocalCompiler.Function.callSteps (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.boundedMapId) (boundedMapBodyBound contents.size) + 1

            The same compiled traversal halts, realizes the ordinary array-map result in its actual final heap, and satisfies the independent linear body bound. Preloaded objects, intermediate increments, code and stack capacity are explicit; no operational time budget or algorithm-specific register proof is assumed.