Documentation

Examples.Language.Buffer

Reading, calling, writing and returning a borrowed buffer #

The source program reads the current first cell, calls a real branching helper, writes its returned value, takes a full slice and returns that actual view. Its specification uses ordinary native array contents and Array.set. A slice does not copy storage: the returned handle is the original view, now observing the shared write. All other cells of the view retain their original values.

The proof uses generated function equations and the shared heap contracts, without exposing object storage, typed environment construction or registers. This file establishes source behavior; machine realization and cost are separate.

The finite table of actual named source bodies.

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

    Ordinary mathematical function generated from the same represented source block.

    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
        theorem Complexity.Language.Examples.BorrowedBuffer.Implementation.clamp_action_rel_native (x limit : ℕ) (initialHeap✝ : Heap) :
        ∃ (returned : Value Ty.nat) (finish : Heap), clamp x limit initialHeap✝ = Part.some (Except.ok returned, finish) ∧ (Representation.ofEmbedding (Function.Embedding.refl ℕ)).Rel (clamp_model x limit) returned finish ∧ (fun (finish : Heap) => initialHeap✝.ShapeExtends finish) finish

        Successful execution observes the native result in its actual final heap and retains every previously represented immutable list.

        Automatic correspondence for the same generated source function, without a machine budget.

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

        @[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

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

          theorem Complexity.Language.Examples.BorrowedBuffer.Implementation.clamp_total_iff (pre✝ : ℕ → ℕ → Heap → Prop) (post✝ : ℕ → ℕ → Heap → ℕ → Heap → Prop) :
          clamp_contract pre✝ post✝ ↔ ∀ (x limit : ℕ) (initialHeap : Heap), pre✝ x limit initialHeap → ∃ (value : ℕ) (finalHeap : Heap), clamp x limit initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ x limit 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

            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.BorrowedBuffer.Implementation.clamp_action_eq_native (x limit : ℕ) (initialHeap✝ : Heap) :
              clamp x limit initialHeap✝ = Part.some (Except.ok (clamp_model x limit), initialHeap✝)

              The same source execution returns the encoded mathematical result without changing its supplied heap, under the actual input representations.

              @[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

                The named function's actual independently interpreted source body.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  theorem Complexity.Language.Examples.BorrowedBuffer.Implementation.clipHead_total_iff (pre✝ : Buffer CellTy.nat → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → ℕ → Heap → Buffer CellTy.nat → Heap → Prop) :
                  clipHead_contract pre✝ post✝ ↔ ∀ (xs : Buffer CellTy.nat) (limit : ℕ) (initialHeap : Heap), pre✝ xs limit initialHeap → ∃ (value : Buffer CellTy.nat) (finalHeap : Heap), clipHead xs limit initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ xs limit 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 source program's declared first-order signatures.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    theorem Complexity.Language.Examples.BorrowedBuffer.Implementation.clamp_spec {pre✝ : Env [Ty.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.nat, Ty.nat] → Heap → ℕ → Heap → Prop} :
                    FunctionTotal program clampId pre✝ post✝ → ∀ (x limit : ℕ) (continuation : Std.Do.PostCond ℕ (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons x (Env.cons limit Env.empty)) initialHeap ∧ ∀ (value : ℕ) (finalHeap : Heap), post✝ (Env.cons x (Env.cons limit Env.empty)) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ clamp x limit ⦃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.

                    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.BorrowedBuffer.Implementation.clipHead_contract_iff_triple (pre✝ : Buffer CellTy.nat → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → ℕ → Heap → Buffer CellTy.nat → Heap → Prop) :
                      clipHead_contract pre✝ post✝ ↔ ∀ (xs : Buffer CellTy.nat) (limit : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ xs limit initialHeap }⦄ clipHead xs limit ⦃(fun (value : Buffer CellTy.nat) (finalHeap : Heap) => { down := post✝ xs limit 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.BorrowedBuffer.Implementation.clamp_total :
                      clamp_contract (fun (x_source limit_source : Value Ty.nat) (initialHeap : Heap) => True) fun (x_source limit_source : Value Ty.nat) (initialHeap : Heap) (returned : Value Ty.nat) (finalHeap : Heap) => returned = clamp_model ((fun (value : ℕ) => value) x_source) ((fun (value : ℕ) => value) limit_source) ∧ finalHeap = initialHeap

                      Exact total source contract derived from the checked encoded-result equation and the existing raw-input reconstruction, with the same final heap.

                      theorem Complexity.Language.Examples.BorrowedBuffer.Implementation.clipHead_spec {pre✝ : Env [Ty.buffer CellTy.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.nat, Ty.nat] → Heap → Buffer CellTy.nat → Heap → Prop} :
                      FunctionTotal program clipHeadId pre✝ post✝ → ∀ (xs : Buffer CellTy.nat) (limit : ℕ) (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 xs (Env.cons limit Env.empty)) initialHeap ∧ ∀ (value : Buffer CellTy.nat) (finalHeap : Heap), post✝ (Env.cons xs (Env.cons limit Env.empty)) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ clipHead xs limit ⦃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.

                      theorem Complexity.Language.Examples.BorrowedBuffer.Implementation.clamp_contract_iff_triple (pre✝ : ℕ → ℕ → Heap → Prop) (post✝ : ℕ → ℕ → Heap → ℕ → Heap → Prop) :
                      clamp_contract pre✝ post✝ ↔ ∀ (x limit : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ x limit initialHeap }⦄ clamp x limit ⦃(fun (value : ℕ) (finalHeap : Heap) => { down := post✝ x limit 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.

                      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.BorrowedBuffer.Implementation.clipHead_eq (xs : Buffer CellTy.nat) (limit : ℕ) :
                        clipHead xs limit = have view := xs; do let head ← view.readM 0 let updated ← clamp head limit view.writeM 0 updated have operand : ℕ := view.length let r ← view.sliceM 0 operand have view : Buffer CellTy.nat := r pure view

                        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.BorrowedBuffer.Implementation.clamp_action_rel_native_preserving (x limit : ℕ) (initialHeap✝ : Heap) :
                          ∃ (returned : Value Ty.nat) (finish : Heap), clamp x limit initialHeap✝ = Part.some (Except.ok returned, finish) ∧ (Representation.ofEmbedding (Function.Embedding.refl ℕ)).Rel (clamp_model x limit) returned finish ∧ (fun (finish : Heap) => initialHeap✝.ShapeExtends finish ∧ Buffer.PreservesContents initialHeap✝ finish) finish

                          Successful execution observes the native result in its actual final heap and retains every previously represented immutable list.

                          @[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
                            @[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
                                theorem Complexity.Language.Examples.BorrowedBuffer.Implementation.clamp_eq (x limit : ℕ) :
                                clamp x limit = have condition := decide (x ≤ limit); if condition = true then pure x else pure limit

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

                                The branching helper computes the ordinary mathematical minimum and preserves any current heap, without assuming that heap is empty.

                                theorem Complexity.Language.Examples.BorrowedBuffer.clipHead_spec (xs : Buffer CellTy.nat) (limit : ℕ) (contents : Array ℕ) (nonempty : 0 < contents.size) :
                                ⦃fun (heap : Heap) => { down := xs.Contents heap contents }⦄ Implementation.clipHead xs limit ⦃(fun (view : Buffer CellTy.nat) (finish : Heap) => { down := view = xs ∧ view.Contents finish (contents.set 0 (min contents[0] limit) nonempty) }, fun (x : Fault) (x_1 : Heap) => { down := False }, PUnit.unit)⦄

                                Native verification composes the public read, write and slice specifications. The remaining conditions concern only ordinary array contents and the returned view.

                                theorem Complexity.Language.Examples.BorrowedBuffer.clipHead_eval (xs : Buffer CellTy.nat) (limit : ℕ) {heap : Heap} {contents : Array ℕ} (observed : xs.Contents heap contents) (nonempty : 0 < contents.size) :
                                ∃ (finish : Heap), Implementation.clipHead xs limit heap = Part.some (Except.ok xs, finish) ∧ xs.Contents finish (contents.set 0 (min contents[0] limit) nonempty)

                                The same native triple establishes actual finite source evaluation and its final-heap contents, rather than only a partial-correctness implication.

                                An ordinary array specification of the same declaration, obtained through its generated curried contract equivalence rather than source constructor proofs.