Documentation

Complexity.Language.Buffer.GetD

Array lookup with a default #

The source functions check the actual buffer length before reading a cell. Their mathematical observation is Lean's Array.getD, including the out-of-bounds case. A valid contents observation suffices; the caller supplies no index bound. Both branches preserve the entire heap and all previously observed arrays.

theorem Complexity.Language.Buffer.GetD.getNat_observe (arguments✝ : Env [Ty.buffer CellTy.nat, Ty.nat, Ty.nat]) :
program.eval getNatId arguments✝ = getNat arguments✝.head arguments✝.tail.head arguments✝.tail.tail.head

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

@[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
        theorem Complexity.Language.Buffer.GetD.getBool_spec {pre✝ : Env [Ty.buffer CellTy.bool, Ty.nat, Ty.bool] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.bool, Ty.nat, Ty.bool] → Heap → Bool → Heap → Prop} :
        FunctionTotal program getBoolId pre✝ post✝ → ∀ (source : Buffer CellTy.bool) (index : ℕ) (fallback : Bool) (continuation : Std.Do.PostCond Bool (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons source (Env.cons index (Env.cons fallback Env.empty))) initialHeap ∧ ∀ (value : Bool) (finalHeap : Heap), post✝ (Env.cons source (Env.cons index (Env.cons fallback Env.empty))) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ getBool source index fallback ⦃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]

        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
            noncomputable def Complexity.Language.Buffer.GetD.getBool (source : Buffer CellTy.bool) (index : ℕ) (fallback : Bool) :

            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]

              This named source function's index in its declared signature table.

              Equations
              Instances For
                theorem Complexity.Language.Buffer.GetD.getBool_eq (source : Buffer CellTy.bool) (index : ℕ) (fallback : Bool) :
                getBool source index fallback = have operand := source.length; have condition := decide (index < operand); if condition = true then do let result ← source.readM index pure result else pure fallback

                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
                  @[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.Buffer.GetD.getNat_eq (source : Buffer CellTy.nat) (index fallback : ℕ) :
                    getNat source index fallback = have operand := source.length; have condition := decide (index < operand); if condition = true then do let result ← source.readM index pure result else pure fallback

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

                    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.Buffer.GetD.getBool_total_iff (pre✝ : Buffer CellTy.bool → ℕ → Bool → Heap → Prop) (post✝ : Buffer CellTy.bool → ℕ → Bool → Heap → Bool → Heap → Prop) :
                      getBool_contract pre✝ post✝ ↔ ∀ (source : Buffer CellTy.bool) (index : ℕ) (fallback : Bool) (initialHeap : Heap), pre✝ source index fallback initialHeap → ∃ (value : Bool) (finalHeap : Heap), getBool source index fallback initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ source index fallback 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
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        theorem Complexity.Language.Buffer.GetD.getBool_observe (arguments✝ : Env [Ty.buffer CellTy.bool, Ty.nat, Ty.bool]) :
                        program.eval getBoolId arguments✝ = getBool arguments✝.head arguments✝.tail.head arguments✝.tail.tail.head

                        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
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          theorem Complexity.Language.Buffer.GetD.getNat_contract_iff_triple (pre✝ : Buffer CellTy.nat → ℕ → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → ℕ → ℕ → Heap → ℕ → Heap → Prop) :
                          getNat_contract pre✝ post✝ ↔ ∀ (source : Buffer CellTy.nat) (index fallback : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ source index fallback initialHeap }⦄ getNat source index fallback ⦃(fun (value : ℕ) (finalHeap : Heap) => { down := post✝ source index fallback 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]

                          This named source function's index in its declared signature table.

                          Equations
                          Instances For
                            theorem Complexity.Language.Buffer.GetD.getBool_contract_iff_triple (pre✝ : Buffer CellTy.bool → ℕ → Bool → Heap → Prop) (post✝ : Buffer CellTy.bool → ℕ → Bool → Heap → Bool → Heap → Prop) :
                            getBool_contract pre✝ post✝ ↔ ∀ (source : Buffer CellTy.bool) (index : ℕ) (fallback : Bool) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ source index fallback initialHeap }⦄ getBool source index fallback ⦃(fun (value : Bool) (finalHeap : Heap) => { down := post✝ source index fallback 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.Buffer.GetD.getNat_spec {pre✝ : Env [Ty.buffer CellTy.nat, Ty.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.nat, Ty.nat, Ty.nat] → Heap → ℕ → Heap → Prop} :
                            FunctionTotal program getNatId pre✝ post✝ → ∀ (source : Buffer CellTy.nat) (index fallback : ℕ) (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 source (Env.cons index (Env.cons fallback Env.empty))) initialHeap ∧ ∀ (value : ℕ) (finalHeap : Heap), post✝ (Env.cons source (Env.cons index (Env.cons fallback Env.empty))) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ getNat source index fallback ⦃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.Buffer.GetD.getNat_total_iff (pre✝ : Buffer CellTy.nat → ℕ → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → ℕ → ℕ → Heap → ℕ → Heap → Prop) :
                            getNat_contract pre✝ post✝ ↔ ∀ (source : Buffer CellTy.nat) (index fallback : ℕ) (initialHeap : Heap), pre✝ source index fallback initialHeap → ∃ (value : ℕ) (finalHeap : Heap), getNat source index fallback initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ source index fallback initialHeap value finalHeap

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

                            noncomputable def Complexity.Language.Buffer.GetD.getNat (source : Buffer CellTy.nat) (index fallback : ℕ) :

                            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.Buffer.GetD.getNat_eval (values : Array ℕ) (index fallback : ℕ) (source : Buffer CellTy.nat) (heap : Heap) (observed : (Representation.array CellTy.nat).Rel values source heap) :
                              getNat source index fallback heap = Part.some (Except.ok (values.getD index fallback), heap)

                              Natural lookup returns the ordinary array element or the supplied default, without allocation or modification of any observed storage.

                              theorem Complexity.Language.Buffer.GetD.getBool_eval (values : Array Bool) (index : ℕ) (fallback : Bool) (source : Buffer CellTy.bool) (heap : Heap) (observed : (Representation.array CellTy.bool).Rel values source heap) :
                              getBool source index fallback heap = Part.some (Except.ok (values.getD index fallback), heap)

                              Boolean lookup uses the same bounds check and actual read as natural lookup.

                              Natural array, index and default observed at the same invocation heap.

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

                                Boolean array, index and default observed at the same invocation heap.

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

                                  Natural lookup refines Lean's total defaulted array access.

                                  Boolean lookup refines Lean's total defaulted array access.

                                  theorem Complexity.Language.Buffer.GetD.getNat_eval_exists_preserving (values : Array ℕ) (index fallback : ℕ) (source : Buffer CellTy.nat) (heap : Heap) (observed : (Representation.array CellTy.nat).Rel values source heap) :
                                  ∃ (returned : ℕ) (finish : Heap), getNat source index fallback heap = Part.some (Except.ok returned, finish) ∧ Representation.nat.Rel (values.getD index fallback) returned finish ∧ heap.ShapeExtends finish ∧ PreservesContents heap finish

                                  The natural read preserves all old array observations, including aliases.

                                  theorem Complexity.Language.Buffer.GetD.getBool_eval_exists_preserving (values : Array Bool) (index : ℕ) (fallback : Bool) (source : Buffer CellTy.bool) (heap : Heap) (observed : (Representation.array CellTy.bool).Rel values source heap) :
                                  ∃ (returned : Bool) (finish : Heap), getBool source index fallback heap = Part.some (Except.ok returned, finish) ∧ Representation.bool.Rel (values.getD index fallback) returned finish ∧ heap.ShapeExtends finish ∧ PreservesContents heap finish

                                  The Boolean read preserves all old array observations, including aliases.

                                  theorem Complexity.Language.Buffer.GetD.getNat_eval_exists (values : Array ℕ) (index fallback : ℕ) (source : Buffer CellTy.nat) (heap : Heap) (observed : (Representation.array CellTy.nat).Rel values source heap) :
                                  ∃ (returned : ℕ) (finish : Heap), getNat source index fallback heap = Part.some (Except.ok returned, finish) ∧ Representation.nat.Rel (values.getD index fallback) returned finish ∧ heap.ShapeExtends finish

                                  Natural lookup supplies the standard represented-call shape frame.

                                  theorem Complexity.Language.Buffer.GetD.getBool_eval_exists (values : Array Bool) (index : ℕ) (fallback : Bool) (source : Buffer CellTy.bool) (heap : Heap) (observed : (Representation.array CellTy.bool).Rel values source heap) :
                                  ∃ (returned : Bool) (finish : Heap), getBool source index fallback heap = Part.some (Except.ok returned, finish) ∧ Representation.bool.Rel (values.getD index fallback) returned finish ∧ heap.ShapeExtends finish

                                  Boolean lookup supplies the standard represented-call shape frame.