Documentation

Examples.Language.ScopeCompiled

Compiling repeated calls with reusable temporary storage #

The source declaration and its mathematical correctness proof remain unchanged. Its real worker allocates nested scratch arrays, returns through scope cleanup, and updates a result allocated outside those scopes. Resource readiness adds only finite-word ranges, actual call nesting and simultaneous scratch capacity.

The loop reuses the same arena boundary after every call. Its source proof supplies termination; the compiled memory guarantee concerns every real access of the complete call-and-halt invocation, including transient accesses and the call stack, rather than only the final allocator cursor. The seeded physical footprint also retains the original arena prefix; repeated scratch and stack addresses are counted once, independently of the call count.

The existing scalar realization rules handle the actual generated guard. Only the remaining call count is read; heap contents do not affect it.

theorem Complexity.Language.Examples.Scope.guard_ready {w limit depth cursor : ℕ} (hw : 0 < w) (remaining : ℕ) (out : Buffer CellTy.nat) (count n value : ℕ) (heap : Heap) {finish : State [Ty.nat, Ty.buffer CellTy.nat, (Ty.buffer CellTy.nat).option, Ty.nat, Ty.nat, Ty.nat]} {decision : Bool} (remainingFits : remaining < 2 ^ w) (execution : Exec Implementation.Source.program Implementation.Source.make_loop1.Guard { locals := Implementation.Source.make_loop1.View.symm (Implementation.Source.make_loop1.entry (remaining, out, count, n, value, ())), heap := heap } finish (Control.returned decision)) :
Ram.LanguageCompiler.ArenaReady execution w limit depth cursor cursor

Realize the exact successful guard execution already used by source correctness, with no allocation and no change to the arena cursor.

theorem Complexity.Language.Examples.Scope.body_ready {w limit cursor : ℕ} (hw : 0 < w) (remaining : ℕ) (out : Buffer CellTy.nat) (count n value : ℕ) (heap : Heap) {finish : State [Ty.nat, Ty.buffer CellTy.nat, (Ty.buffer CellTy.nat).option, Ty.nat, Ty.nat, Ty.nat]} {control : Control (Ty.buffer CellTy.nat)} (outRooted : out.Rooted heap) (outFits : out.length < 2 ^ w) (remainingFits : remaining < 2 ^ w) (nFits : n < 2 ^ w) (valueFits : value < 2 ^ w) (capacity : cursor + 2 * n ≤ limit) (execution : Exec Implementation.Source.program Implementation.Source.make_loop1.Body { locals := Implementation.Source.make_loop1.View.symm (Implementation.Source.make_loop1.entry (remaining, out, count, n, value, ())), heap := heap } finish control) (successful : Ram.LanguageCompiler.ControlFits w control) :
Ram.LanguageCompiler.ArenaReady execution w limit 1 cursor cursor

The body calls the actual reclaiming worker and decrements the saved count. The caller and callee share one cursor; returning from the worker restores its scratch extent before the caller's normal continuation.

theorem Complexity.Language.Examples.Scope.loop_ready {w limit cursor : ℕ} (hw : 0 < w) (remaining : ℕ) (out : Buffer CellTy.nat) (count n value : ℕ) (heap : Heap) {finish : State [Ty.nat, Ty.buffer CellTy.nat, (Ty.buffer CellTy.nat).option, Ty.nat, Ty.nat, Ty.nat]} {control : Control (Ty.buffer CellTy.nat)} (current : invariant out count n value remaining heap) (countFits : count < 2 ^ w) (nFits : n < 2 ^ w) (valueFits : value < 2 ^ w) (capacity : cursor + 2 * n ≤ limit) (execution : Exec Implementation.Source.program Implementation.Source.make_loop1.Code { locals := Implementation.Source.make_loop1.View.symm (Implementation.Source.make_loop1.entry (remaining, out, count, n, value, ())), heap := heap } finish control) (successful : Ram.LanguageCompiler.ControlFits w control) :
Ram.LanguageCompiler.ArenaReady execution w limit 1 cursor cursor

Every finite source loop reuses the same scratch capacity. The original mathematical invariant and body contract supply preservation; the additional proof checks only ranges and one level of actual callee nesting.

The generated maker's ordinary arguments, in the source function table's parameter order. This is an environment encoding, not another implementation.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    theorem Complexity.Language.Examples.Scope.make_ready {w limit cursor : ℕ} (hw : 0 < w) (count n value : ℕ) (heap : Heap) {finish : State [Ty.nat, Ty.nat, Ty.nat]} {out : Buffer CellTy.nat} (countFits : count < 2 ^ w) (nFits : n < 2 ^ w) (valueFits : value < 2 ^ w) (capacity : cursor + 1 + 2 * n ≤ limit) (execution : Exec Implementation.Source.program Implementation.Source.makeBody { locals := makeArgs count n value, heap := heap } finish (Control.returned out)) :
    Ram.LanguageCompiler.ArenaReady execution w limit 1 cursor (cursor + 1)

    The complete maker retains only its one result cell. Its loop contract tracks both ordinary completion and the actual last-round local return; temporary capacity is independent of count.

    A sufficient physical workspace envelope: the retained entry arena, one result word, two simultaneously live scratch arrays, and two actual call frames. The frame size is derived from the fixed generated program, not from count.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      theorem Complexity.Language.Examples.Scope.make_runUntil {w cursor : ℕ} (count n value : ℕ) {heap : Heap} {entry : Ram.Source.State w} {placement : ℕ → Ram.Word w} (launch : Ram.LanguageCompiler.FunctionArenaLaunch Implementation.Source.program Implementation.Source.makeId 1 (heapLimit cursor n) placement (makeArgs count n value) heap cursor entry) :
      ∃ (outcome : Ram.LanguageCompiler.FunctionArenaExecution Implementation.Source.program Implementation.Source.makeId 1 (heapLimit cursor n) placement (makeArgs count n value) heap entry), Buffer.Contents outcome.value outcome.heap (resultContents count n value) ∧ outcome.cursor = cursor + 1 ∧ (∀ address ∈ outcome.heapAccesses, BitVec.toNat address < workspaceWords cursor n) ∧ outcome.heapAccesses.card ≤ workspaceWords cursor n

      The same high-level declaration returns its mathematical array in the real halted RAM invocation. Every actual memory access, and therefore every distinct word touched, stays within a workspace bound independent of the iteration count. The shared launch carries the same preloaded representation, scalar ranges and word/code capacity. The result retains actual RAM memory, returned fields and counted execution; input loading and output conversion remain outside it.

      Repeated scratch calls have a count-independent physical footprint in their actual halted invocation. The seed includes the complete preloaded arena prefix, and the access set includes transient scratch and call-frame words. Reclamation does not erase accesses, but reusing their addresses does not charge them again. This retains the original correctness and returned cursor; it is not a claim of exact peak-live storage or of free input loading.