Documentation

Examples.Language.BufferCompiled

Compiling the borrowed-buffer consumer #

The existing clipHead source program reads, calls its branching helper, writes, slices and returns a real borrowed view. Its source correctness proof is reused unchanged. Realization adds the actual scalar and descriptor ranges; the backend uses the shared heap representation, including overlapping aliases.

Independent cost composition counts the same operations and actual helper call. The final theorem retains the changed source heap, its represented target state, the two returned descriptor fields and the halted invocation's measured count. Neither a register proof nor an instruction-price annotation is supplied here.

The helper needs representable inputs and one Boolean guard, but no nested call.

Only the read cell, limit and actual view length need source range facts. The current contents justify access; aliases are not required to be disjoint.

Descriptor binding, read, helper call, write, length, slice, reassignment and return are charged by the shared lowering rules. This conditional cost proof does not repeat correctness.

theorem Complexity.Language.Examples.BorrowedBuffer.clipHead_runUntil_le {w heapLimit : ℕ} (hw : 0 < w) (placement : ℕ → Ram.Word w) (xs : Buffer CellTy.nat) (limit : ℕ) (sourceHeap : Heap) (contents : Array ℕ) (nonempty : 0 < contents.size) (observed : xs.Contents sourceHeap contents) (lengthFits : xs.length < 2 ^ w) (limitFits : limit < 2 ^ w) (entry : Ram.Source.State w) (represented : Ram.LanguageCompiler.HeapRep placement heapLimit sourceHeap entry) (codeCapacity : List.length (Ram.LanguageCompiler.lowerCode Implementation.program Implementation.clipHeadId) < 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.clipHead xs limit sourceHeap = Part.some (Except.ok xs, finalHeap) ∧ xs.Contents finalHeap (contents.set 0 (min contents[0] limit) nonempty) ∧ Ram.Source.FunctionExec (Ram.LanguageCompiler.lowerProgram Implementation.program) heapLimit 1 (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.clipHeadId) (Ram.LanguageCompiler.envWords placement (Env.cons xs (Env.cons limit Env.empty))) entry (Ram.LanguageCompiler.valueWords placement xs) targetFinish ∧ Ram.LanguageCompiler.HeapRep placement heapLimit finalHeap targetFinish ∧ Ram.LocalCompiler.Function.runUntil (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerProgram Implementation.program) (↑Implementation.clipHeadId) 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.clipHeadId) bodySteps + 1, reason := Ram.StopReason.halted } ∧ Ram.LocalCompiler.Function.returnedValues 2 target = Ram.LanguageCompiler.valueWords placement xs ∧ Ram.Source.State.Observes heapLimit 0 targetFinish target ∧ (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.clipHeadId).bodyTime (Ram.LanguageCompiler.lowerProgram Implementation.program) heapLimit (Ram.LanguageCompiler.envWords placement (Env.cons xs (Env.cons limit Env.empty))) entry = Part.some bodySteps ∧ bodySteps ≤ Ram.LanguageCompiler.callCost Implementation.program Implementation.clampId 13 + 38 ∧ Ram.LocalCompiler.Function.callSteps (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.clipHeadId) bodySteps + 1 ≤ Ram.LocalCompiler.Function.callSteps (Ram.LanguageCompiler.programControl Implementation.program) (Ram.LanguageCompiler.lowerFunc Implementation.program Implementation.clipHeadId) (Ram.LanguageCompiler.callCost Implementation.program Implementation.clampId 13 + 38) + 1

The same compiled invocation returns the actual descriptor and represents the source program's updated contents. The read value's range follows from the input heap representation; no additional cell-range or no-alias premise is imposed. The independent body bound includes internal calls, and the complete bound adds the outer call and halt once.