Documentation

Examples.Language.OptionalBuffer

Imported structured results with an optional borrowed buffer #

A pure metadata helper classifies a length. An independently declared library uses that actual result to return either no buffer or a full borrowed slice. The client matches the imported optional handle, reads and increments its first cell only when present, and returns the structured result. Products and options here are source values carried by actual calls, not a host-side substitute for the source implementations.

Correctness is stated with ordinary arrays and preservation of outside views. OptionalBufferCompiled connects these same implementations to RAM execution.

The finite table of actual named source bodies.

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
      theorem Complexity.Language.Examples.OptionalBuffer.Metadata.classifyLength_contract_iff_triple (pre✝ : ℕ → Heap → Prop) (post✝ : ℕ → Heap → Option (ℕ × ℕ) → Heap → Prop) :
      classifyLength_contract pre✝ post✝ ↔ ∀ (n : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ n initialHeap }⦄ classifyLength_action n ⦃(fun (value : Option (ℕ × ℕ)) (finalHeap : Heap) => { down := post✝ n 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.OptionalBuffer.Metadata.classifyLength_action_eq (n : ℕ) :
      classifyLength_action n = have condition := decide (n = 0); if condition = true then pure none else have operand := (n, 0); pure (some operand)

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

      theorem Complexity.Language.Examples.OptionalBuffer.Metadata.classifyLength_total_iff (pre✝ : ℕ → Heap → Prop) (post✝ : ℕ → Heap → Option (ℕ × ℕ) → Heap → Prop) :
      classifyLength_contract pre✝ post✝ ↔ ∀ (n : ℕ) (initialHeap : Heap), pre✝ n initialHeap → ∃ (value : Option (ℕ × ℕ)) (finalHeap : Heap), classifyLength_action n initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ n initialHeap value finalHeap

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

      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

        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
          @[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 actual source action terminates with the generated native result and preserves every initial heap. Lean's native recursion principle proves this once.

            Exact total source contract inherited from the generated native function.

            Executable total value function generated from the same buffer-free source block.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              theorem Complexity.Language.Examples.OptionalBuffer.Metadata.classifyLength_spec {pre✝ : Env [Ty.nat] → Heap → Prop} {post✝ : Env [Ty.nat] → Heap → Option (ℕ × ℕ) → Heap → Prop} :
              FunctionTotal program classifyLengthId pre✝ post✝ → ∀ (n : ℕ) (continuation : Std.Do.PostCond (Option (ℕ × ℕ)) (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons n Env.empty) initialHeap ∧ ∀ (value : Option (ℕ × ℕ)) (finalHeap : Heap), post✝ (Env.cons n Env.empty) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ classifyLength_action n ⦃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 independently interpreted source body.

              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
                  @[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
                    theorem Complexity.Language.Examples.OptionalBuffer.Library.Source.inspect_total_iff (pre✝ : Buffer CellTy.nat → Heap → Prop) (post✝ : Buffer CellTy.nat → Heap → ℕ × Option (Buffer CellTy.nat) → Heap → Prop) :
                    inspect_contract pre✝ post✝ ↔ ∀ (xs : Buffer CellTy.nat) (initialHeap : Heap), pre✝ xs initialHeap → ∃ (value : ℕ × Option (Buffer CellTy.nat)) (finalHeap : Heap), inspect xs initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ xs initialHeap value finalHeap

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

                    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.OptionalBuffer.Library.Source.inspect_eq (xs : Buffer CellTy.nat) :
                      inspect xs = have operand := xs.length; do let summary ← Metadata.classifyLength_action operand match summary with | none => have operand := none; pure (0, operand) | some payload => have length := payload.1; have offset := payload.2; do let view ← xs.sliceM offset length have operand : Option (Buffer CellTy.nat) := some view pure (length, operand)

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

                      Added function bodies already typed against the complete linked table.

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

                        Signature-preserving positions of this imported source program.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          theorem Complexity.Language.Examples.OptionalBuffer.Library.Source.inspect_contract_iff_triple (pre✝ : Buffer CellTy.nat → Heap → Prop) (post✝ : Buffer CellTy.nat → Heap → ℕ × Option (Buffer CellTy.nat) → Heap → Prop) :
                          inspect_contract pre✝ post✝ ↔ ∀ (xs : Buffer CellTy.nat) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ xs initialHeap }⦄ inspect xs ⦃(fun (value : ℕ × Option (Buffer CellTy.nat)) (finalHeap : Heap) => { down := post✝ xs 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.

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

                            The finite table of actual named source bodies.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              theorem Complexity.Language.Examples.OptionalBuffer.Library.Source.inspect_spec {pre✝ : Env [Ty.buffer CellTy.nat] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.nat] → Heap → ℕ × Option (Buffer CellTy.nat) → Heap → Prop} :
                              FunctionTotal program inspectId pre✝ post✝ → ∀ (xs : Buffer CellTy.nat) (continuation : Std.Do.PostCond (ℕ × Option (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.empty) initialHeap ∧ ∀ (value : ℕ × Option (Buffer CellTy.nat)) (finalHeap : Heap), post✝ (Env.cons xs Env.empty) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ inspect xs ⦃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

                                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]

                                  Signatures of the functions added by this source declaration.

                                  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.

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

                                      Added function bodies already typed against the complete linked table.

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        @[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.

                                          @[reducible, inline]

                                          Signature-preserving positions of this imported source program.

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

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

                                                  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.OptionalBuffer.Implementation.Source.bump_contract_iff_triple (pre✝ : Buffer CellTy.nat → Heap → Prop) (post✝ : Buffer CellTy.nat → Heap → ℕ × Option (Buffer CellTy.nat) → Heap → Prop) :
                                                    bump_contract pre✝ post✝ ↔ ∀ (xs : Buffer CellTy.nat) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ xs initialHeap }⦄ bump xs ⦃(fun (value : ℕ × Option (Buffer CellTy.nat)) (finalHeap : Heap) => { down := post✝ xs 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.

                                                    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.OptionalBuffer.Implementation.Source.bump_total_iff (pre✝ : Buffer CellTy.nat → Heap → Prop) (post✝ : Buffer CellTy.nat → Heap → ℕ × Option (Buffer CellTy.nat) → Heap → Prop) :
                                                      bump_contract pre✝ post✝ ↔ ∀ (xs : Buffer CellTy.nat) (initialHeap : Heap), pre✝ xs initialHeap → ∃ (value : ℕ × Option (Buffer CellTy.nat)) (finalHeap : Heap), bump xs initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ xs initialHeap value finalHeap

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

                                                      theorem Complexity.Language.Examples.OptionalBuffer.Implementation.Source.bump_spec {pre✝ : Env [Ty.buffer CellTy.nat] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.nat] → Heap → ℕ × Option (Buffer CellTy.nat) → Heap → Prop} :
                                                      FunctionTotal program bumpId pre✝ post✝ → ∀ (xs : Buffer CellTy.nat) (continuation : Std.Do.PostCond (ℕ × Option (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.empty) initialHeap ∧ ∀ (value : ℕ × Option (Buffer CellTy.nat)) (finalHeap : Heap), post✝ (Env.cons xs Env.empty) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ bump xs ⦃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]

                                                      Signatures of the functions added by this source declaration.

                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For
                                                        theorem Complexity.Language.Examples.OptionalBuffer.Implementation.Source.bump_eq (xs : Buffer CellTy.nat) :
                                                        bump xs = do let pattern ← Library.Source.inspect xs have length : ℕ := pattern.1 have present : Option (Buffer CellTy.nat) := pattern.2 match present with | none => have operand := none; pure (length, operand) | some payload => do let head ← payload.readM 0 have operand : ℕ := head.add 1 payload.writeM 0 operand have operand : Option (Buffer CellTy.nat) := some payload pure (length, operand)

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

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

                                                            The library returns the original borrowed handle exactly when nonempty.

                                                            Equations
                                                            Instances For

                                                              The ordinary mathematical update of the first element, if any.

                                                              Equations
                                                              Instances For

                                                                The returned handle and all array/frame properties refer to the same final heap.

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

                                                                  The pure source helper has an ordinary optional-pair result.

                                                                  theorem Complexity.Language.Examples.OptionalBuffer.classifyLength_total :
                                                                  Metadata.classifyLength_contract (fun (x : ℕ) (x_1 : Heap) => True) fun (n : ℕ) (heap : Heap) (value : Option (ℕ × ℕ)) (finish : Heap) => (value = if n = 0 then none else some (n, 0)) ∧ finish = heap

                                                                  Generated correspondence transfers the same pure result to source execution.

                                                                  The imported classifier selects the actual full slice without changing the heap.

                                                                  Inspecting metadata terminates even for an empty view, without reading its cells.

                                                                  theorem Complexity.Language.Examples.OptionalBuffer.bump_spec (xs : Buffer CellTy.nat) (contents : Array ℕ) (heap : Heap) (observed : xs.Contents heap contents) :
                                                                  ⦃fun (entry : Heap) => ⌜entry = heap⌝⦄ Implementation.Source.bump xs ⦃Std.Do.PostCond.noThrow fun (result : ℕ × Option (Buffer CellTy.nat)) (finish : Heap) => ⌜bumpPost xs contents heap result finish⌝⦄

                                                                  The client's native proof uses the imported result and the ordinary buffer read/write contracts; the resulting frame concerns the actual write heap.

                                                                  theorem Complexity.Language.Examples.OptionalBuffer.bump_total (contents : Array ℕ) :
                                                                  Implementation.Source.bump_contract (fun (xs : Buffer CellTy.nat) (heap : Heap) => xs.Contents heap contents) fun (xs : Buffer CellTy.nat) (heap : Heap) (result : ℕ × Option (Buffer CellTy.nat)) (finish : Heap) => bumpPost xs contents heap result finish

                                                                  Source correctness in ordinary parameters: the returned optional handle, updated native array and outside-view frame share one real final heap.