Documentation

Examples.Language.Allocation

Returning an allocated array across a call and a later allocation #

make allocates an initialized array and returns its descriptor. retain calls that actual body, allocates a second array, then reads the original array's first cell when it is nonempty and returns the original descriptor. The empty case returns directly. Both allocations remain in the final heap.

The mathematical result is ordinary Array.replicate contents, with every old view preserved. Source termination has no capacity or time premise. Readiness separately requires fitting values and cumulative reserved capacity; costs compose the shared allocation, call, branch, read and return rules. This typed source consumer adds no algorithm-specific lowering or registers.

theorem Complexity.Language.Examples.Allocation.Named.Source.make_spec {pre✝ : Env [Ty.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.nat, Ty.nat] → Heap → Buffer CellTy.nat → Heap → Prop} :
FunctionTotal program makeId pre✝ post✝ → ∀ (n initial : ℕ) (continuation : Std.Do.PostCond (Buffer CellTy.nat) (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons n (Env.cons initial Env.empty)) initialHeap ∧ ∀ (value : Buffer CellTy.nat) (finalHeap : Heap), post✝ (Env.cons n (Env.cons initial Env.empty)) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ make n initial ⦃continuation⦄

Apply a supplied source contract with ordinary named arguments and its actual returned value and final heap. No callee body or new contract is inferred.

@[reducible, inline]

The same source action, without an asserted total mathematical model.

Equations
Instances For
    theorem Complexity.Language.Examples.Allocation.Named.Source.make_contract_iff_triple (pre✝ : ℕ → ℕ → Heap → Prop) (post✝ : ℕ → ℕ → Heap → Buffer CellTy.nat → Heap → Prop) :
    make_contract pre✝ post✝ ↔ ∀ (n initial : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ n initial initialHeap }⦄ make n initial ⦃(fun (value : Buffer CellTy.nat) (finalHeap : Heap) => { down := post✝ n initial initialHeap value finalHeap }, fun (x : Fault) (x_1 : Heap) => { down := False }, PUnit.unit)⦄

    Prove the ordinary-parameter source contract with a total-correctness triple for the same action. The ghost initial heap equals the actual entry heap; successful results use the actual final heap, and faults are excluded.

    theorem Complexity.Language.Examples.Allocation.Named.Source.make_total_iff (pre✝ : ℕ → ℕ → Heap → Prop) (post✝ : ℕ → ℕ → Heap → Buffer CellTy.nat → Heap → Prop) :
    make_contract pre✝ post✝ ↔ ∀ (n initial : ℕ) (initialHeap : Heap), pre✝ n initial initialHeap → ∃ (value : Buffer CellTy.nat) (finalHeap : Heap), make n initial initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ n initial initialHeap value finalHeap

    The source contract is equivalent to ordinary curried preconditions and successful result/heap postconditions, including termination and absence of faults.

    @[reducible, inline]

    The actual declared argument environment, constructed from ordinary source parameters. This is proof-side parameter transport, not an additional runtime operation.

    Equations
    Instances For
      @[reducible, inline]

      Apply a predicate, postcondition or bound with ordinary source parameters to the actual argument environment. No new contract or execution is introduced.

      Equations
      Instances For

        The named function's actual independently interpreted source body.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          @[reducible, inline]

          The source program's declared first-order signatures.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            @[reducible, inline]

            Budget-free total correctness with ordinary source parameters and relational initial/final heap postconditions. This is the existing source function contract.

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

              Fold an actual source invocation back to its named ordinary-argument observation.

              The finite table of actual named source bodies.

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

                The named function's actual partial source action, with ordinary typed arguments and an explicitly supplied shared heap.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  theorem Complexity.Language.Examples.Allocation.Named.Source.make_eq (n initial : ℕ) :
                  make n initial = do let xs ← Buffer.allocM n initial pure xs

                  One source-body equation in ordinary monadic notation; named callees remain opaque.

                  Ordinary arguments and the actual result observed in their real source heaps. This interface does not assert the existence of a total mathematical model.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    theorem Complexity.Language.Examples.Allocation.named_make_spec (n initial : ℕ) :
                    ⦃fun (x : Heap) => { down := True }⦄ Named.make n initial ⦃(fun (buffer : Buffer CellTy.nat) (heap : Heap) => { down := buffer.Contents heap (Array.replicate n initial) }, fun (x : Fault) (x_1 : Heap) => { down := False }, PUnit.unit)⦄

                    Named allocation has an ordinary native Array specification. The shared allocation specification supplies the proof; no register or capacity appears.

                    @[reducible, inline]

                    The actual producer and caller signatures.

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

                      Allocate exactly the requested initialized native array and return its view.

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

                        The nonempty path performs an actual read before returning the original descriptor.

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

                          Inspect the saved buffer after the second allocation, without reading an empty array.

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

                            A second allocation uses the caller's requested extra extent.

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

                              This is a real call to make, followed by allocation and use of its returned buffer.

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

                                Both bodies occupy the source function table used by the compiler.

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    @[reducible, inline]

                                    The first source allocation's actual returned handle.

                                    Equations
                                    Instances For
                                      @[reducible, inline]

                                      The actual source heap after both allocations, without reclamation.

                                      Equations
                                      Instances For
                                        theorem Complexity.Language.Examples.Allocation.retained_contents (heap : Heap) (n initial extra : ℕ) :
                                        (savedBuffer heap n initial).Contents (retainedHeap heap n initial extra) (Array.replicate n initial)

                                        The second allocation does not change the first array's mathematical contents.

                                        theorem Complexity.Language.Examples.Allocation.retained_read (heap : Heap) (n initial extra : ℕ) (positive : 0 < n) :
                                        (retainedHeap heap n initial extra).read (savedBuffer heap n initial) 0 = Except.ok initial

                                        A nonempty returned array is really read from the heap after the later allocation.

                                        theorem Complexity.Language.Examples.Allocation.make_exec (heap : Heap) (n initial : ℕ) :
                                        Exec program (program.body makeId) { locals := makeArgs n initial, heap := heap } { locals := makeArgs n initial, heap := (heap.alloc n initial).2 } (Control.returned (savedBuffer heap n initial))

                                        Producer execution terminates for all natural inputs and every initial heap.

                                        theorem Complexity.Language.Examples.Allocation.retain_exec (heap : Heap) (n initial extra : ℕ) :
                                        Exec program (program.body retainId) { locals := retainArgs n initial extra, heap := heap } { locals := retainArgs n initial extra, heap := retainedHeap heap n initial extra } (Control.returned (savedBuffer heap n initial))

                                        The caller retains both allocations and returns the first descriptor. The same theorem includes n = 0; no fake read or default cell is needed.

                                        theorem Complexity.Language.Examples.Allocation.retained_frame (heap : Heap) (n initial extra : ℕ) {kind : CellTy} {view : Buffer kind} {contents : Array (CellValue kind)} (observed : view.Contents heap contents) :
                                        view.Contents (retainedHeap heap n initial extra) contents

                                        Every old view survives the complete caller execution by the shared frame rule, without replaying its individual allocations in this proof.

                                        theorem Complexity.Language.Examples.Allocation.retained_empty (heap : Heap) (initial extra : ℕ) :
                                        (savedBuffer heap 0 initial).Contents (retainedHeap heap 0 initial extra) #[] ∧ (savedBuffer heap 0 initial).object = heap.objects.size

                                        The returned empty object remains valid and has fresh identity after the later allocation.

                                        The producer pays the shared allocator's counted initialization and its return.

                                        Equations
                                        Instances For

                                          Only the nonempty branch executes the heap read; branch prices come from the corresponding arena cost constructors.

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

                                            The continuation's second allocation does not reuse the first allocation's capacity.

                                            Equations
                                            Instances For

                                              Call overhead uses the real generated callee frame and includes its body flag initialization.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                theorem Complexity.Language.Examples.Allocation.make_ready_cost (heap : Heap) (n initial : ℕ) {w heapLimit depth cursor : ℕ} (nFits : n < 2 ^ w) (initialFits : initial < 2 ^ w) (capacity : cursor + n ≤ heapLimit) :
                                                ∃ (ready : Ram.LanguageCompiler.ArenaReady ⋯ w heapLimit depth cursor (cursor + n)), Ram.LanguageCompiler.ArenaExecutionCost ready (makeSteps n)

                                                The exact producer count observes the source execution above, with backend range/capacity assumptions separated from its unconditional mathematical result.

                                                theorem Complexity.Language.Examples.Allocation.retain_ready_cost (heap : Heap) (n initial extra : ℕ) {w heapLimit cursor : ℕ} (nFits : n < 2 ^ w) (initialFits : initial < 2 ^ w) (capacity : cursor + n + extra ≤ heapLimit) :
                                                ∃ (ready : Ram.LanguageCompiler.ArenaReady ⋯ w heapLimit 1 cursor (cursor + n + extra)), Ram.LanguageCompiler.ArenaExecutionCost ready (retainSteps n extra)

                                                One true source call and both allocations share a monotonically growing cursor. The exact call count is derived from the actual compiled function, not a chosen ABI price.

                                                theorem Complexity.Language.Examples.Allocation.retain_runUntil (heap : Heap) (n initial extra : ℕ) {w heapLimit cursor : ℕ} (hw : 0 < w) (initialFits : initial < 2 ^ w) (entry : Ram.Source.State w) {placement : ℕ → Ram.Word w} (arena : Ram.LanguageCompiler.ArenaRep placement cursor heapLimit heap entry) (capacity : cursor + n + extra ≤ heapLimit) (codeCapacity : List.length (Ram.LanguageCompiler.lowerCode program retainId) < 2 ^ w) (stackCapacity : heapLimit + 2 * Ram.ABI.frameSize (Ram.LanguageCompiler.programControl program) < 2 ^ w) :
                                                ∃ (finalPlacement : ℕ → Ram.Word w) (finishTarget : Ram.Source.State w) (target : Ram.State w), Ram.Source.FunctionMeasuredExec (Ram.LanguageCompiler.programControl program) (Ram.LanguageCompiler.lowerProgram program) heapLimit 1 (Ram.LanguageCompiler.lowerFunc program retainId) (Ram.LanguageCompiler.envWords placement (retainArgs n initial extra)) (retainSteps n extra + 2) entry (Ram.LanguageCompiler.valueWords finalPlacement (savedBuffer heap n initial)) finishTarget ∧ Ram.LanguageCompiler.ArenaRep finalPlacement (cursor + n + extra) heapLimit (retainedHeap heap n initial extra) finishTarget ∧ Ram.LanguageCompiler.Placement.Agrees heap placement finalPlacement ∧ Ram.LocalCompiler.Function.runUntil (Ram.LanguageCompiler.programControl program) (Ram.LanguageCompiler.lowerProgram program) (↑retainId) (Ram.LanguageCompiler.contextSize signatures[retainId].params) heapLimit (Ram.LanguageCompiler.envWords placement (retainArgs n initial extra)) entry = some { state := target, steps := Ram.LocalCompiler.Function.callSteps (Ram.LanguageCompiler.programControl program) (Ram.LanguageCompiler.lowerFunc program retainId) (retainSteps n extra + 2) + 1, reason := Ram.StopReason.halted } ∧ Ram.LocalCompiler.Function.returnedValues (Ram.LanguageCompiler.fieldCount signatures[retainId].result) target = Ram.LanguageCompiler.valueWords finalPlacement (savedBuffer heap n initial) ∧ (Ram.LanguageCompiler.bufferRef finalPlacement (savedBuffer heap n initial)).Rep heapLimit (Ram.LanguageCompiler.objectWords w (Array.replicate n initial)).toList finishTarget ∧ Ram.Source.State.Observes heapLimit 0 finishTarget target

                                                The actual compiled caller halts with the first array still represented after both allocations. The same final target witnesses the exact runner count, returned descriptor, extended placement and cumulative arena extent. The two-frame stack bound covers this caller and its real producer call.