Documentation

Examples.Language.Traversal

A borrowed-buffer traversal with a helper call and branch #

Each iteration reads the current cell, calls the source increment function, selects the bounded value and writes it to the same shared buffer. The source uses an ordinary range loop with an immutable iteration index. The complete source proof shows termination and the native array map result by fun x => min (x + 1) limit; no host array operation executes in place of the declared traversal. Examples.Language.TraversalCompiled connects this same source proof to its compiled invocation and an independent linear execution-cost bound.

A lossless coordinate permutation; no source operation is executed.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.guard_spec (xs : Buffer CellTy.nat) (limit : ℕ) {pre✝ : ℕ → Heap → Prop} {normal✝ : ℕ → Heap → ℕ → Heap → Prop} {returned✝ : ℕ → Heap → Bool → ℕ → Heap → Prop} :
    guard_contract xs limit pre✝ normal✝ returned✝ → ∀ (i : ℕ) (post : Std.Do.PostCond (Control Ty.bool × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ i heap ∧ (∀ (after_i : ℕ) (finish : Heap), normal✝ i heap after_i finish → (post.1 (Control.normal, after_i, xs, limit, ()) finish).down) ∧ ∀ (value : Bool) (after_i : ℕ) (finish : Heap), returned✝ i heap value after_i finish → (post.1 (Control.returned value, after_i, xs, limit, ()) finish).down }⦄ guard i xs limit ⦃post⦄

    Apply a chosen mathematical contract to the native block action. Continuations see ordinary mutable outputs; proved frames restore the fixed captures.

    Coordinate conversion for composing this named observation.

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

    @[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.Traversal.Implementation.boundedMap_loop1.count_frame_contract (xs : Buffer CellTy.nat) (limit : ℕ) (count✝ : ℕ → ℕ) (limit✝ : ℕ) (invariant✝ : ℕ → Heap → Prop) (frame✝ : Heap → Heap → Prop) :
      (∀ {a b c : Heap}, frame✝ a b → frame✝ b c → frame✝ a c) → ∀ (normal : Heap → Prop), (guard_contract xs limit invariant✝ (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (x_2 : Heap) => False) fun (i : ℕ) (heap : Heap) (again : Bool) (after_i : ℕ) (finish : Heap) => count✝ after_i = count✝ i ∧ finish = heap ∧ invariant✝ after_i finish ∧ (again = true ↔ count✝ after_i < limit✝)) → (body_contract xs limit (fun (i : ℕ) (heap : Heap) => invariant✝ i heap ∧ count✝ i < limit✝) (fun (i : ℕ) (heap : Heap) (after_i : ℕ) (finish : Heap) => count✝ after_i = count✝ i + 1 ∧ invariant✝ after_i finish ∧ frame✝ heap finish) fun (x : ℕ) (x_1 : Heap) (x_2 : Unit) (after_i : ℕ) (x_3 : Heap) => False) → (∀ (i : ℕ) (heap : Heap), invariant✝ i heap → limit✝ ≤ count✝ i → normal heap) → ∀ (initial : Heap), contract xs limit (fun (i : ℕ) (heap : Heap) => invariant✝ i heap ∧ frame✝ initial heap) (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (finish : Heap) => normal finish ∧ frame✝ initial finish) fun (x : ℕ) (x_1 : Heap) (x_2 : Unit) (after_i : ℕ) (x_3 : Heap) => False

      Compose an increasing count and a transitive heap frame from the actual guard and body contracts. The guard preserves the count and heap; normal body steps increase the count by one. Array effects and the exit consequence remain supplied mathematical facts, without a runtime budget.

      theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.spec (xs : Buffer CellTy.nat) (limit : ℕ) {pre✝ : ℕ → Heap → Prop} {normal✝ : ℕ → Heap → ℕ → Heap → Prop} {returned✝ : ℕ → Heap → Unit → ℕ → Heap → Prop} :
      contract xs limit pre✝ normal✝ returned✝ → ∀ (i : ℕ) (post : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ i heap ∧ (∀ (after_i : ℕ) (finish : Heap), normal✝ i heap after_i finish → (post.1 (Control.normal, after_i, xs, limit, ()) finish).down) ∧ ∀ (value : Unit) (after_i : ℕ) (finish : Heap), returned✝ i heap value after_i finish → (post.1 (Control.returned value, after_i, xs, limit, ()) finish).down }⦄ boundedMap_loop1 i xs limit ⦃post⦄

      Apply a chosen mathematical contract to the native block action. Continuations see ordinary mutable outputs; proved frames restore the fixed captures.

      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 actual source block observed with ordinary named local inputs.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.continue_eq (entry✝ : Env [Ty.nat, Ty.buffer CellTy.nat, Ty.nat]) (next✝ : Env [Ty.nat, Ty.buffer CellTy.nat, Ty.nat] → ExceptT Fault (StateT Heap Part) Unit) :
          Code.evalWith program entry✝ next✝ = do let __discr ← MonadLift.monadLift (boundedMap_loop1 (View entry✝).1 (View entry✝).2.1 (View entry✝).2.2.1) match __discr with | (control, locals) => match control with | Control.normal => next✝ (Env.cons locals.1 (Env.cons (entry✝.get Var.here.there) (Env.cons (entry✝.get Var.here.there.there) Env.empty))) | Control.returned value => pure value | Control.fault error => throw error

          Resume from actual mutable locals; immutable captures are preserved by the source frame.

          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]
            abbrev Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.contract (xs : Buffer CellTy.nat) (limit : ℕ) :
            (ℕ → Heap → Prop) → (ℕ → Heap → ℕ → Heap → Prop) → (ℕ → Heap → Unit → ℕ → Heap → Prop) → Prop

            Relational total correctness with ordinary mutable parameters and fixed captures. The existing block contract retains both actual heaps and excludes faults.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_spec {pre✝ : Env [Ty.buffer CellTy.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.nat, Ty.nat] → Heap → Unit → Heap → Prop} :
              FunctionTotal program boundedMapId pre✝ post✝ → ∀ (xs : Buffer CellTy.nat) (limit : ℕ) (continuation : Std.Do.PostCond Unit (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 : Unit) (finalHeap : Heap), post✝ (Env.cons xs (Env.cons limit Env.empty)) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ boundedMap 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.Traversal.Implementation.boundedMap_loop1.regroup_apply (values✝ : Locals) :
              Regroup values✝ = ((values✝.1, ()), values✝.2.1, values✝.2.2.1, ())

              Restore complete lexical coordinates without unfolding proof fields.

              theorem Complexity.Language.Examples.Traversal.Implementation.boundedMapPair_spec {pre✝ : Env [Ty.buffer CellTy.nat, Ty.buffer CellTy.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.nat, Ty.buffer CellTy.nat, Ty.nat] → Heap → Unit → Heap → Prop} :
              FunctionTotal program boundedMapPairId pre✝ post✝ → ∀ (xs ys : Buffer CellTy.nat) (limit : ℕ) (continuation : Std.Do.PostCond Unit (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons xs (Env.cons ys (Env.cons limit Env.empty))) initialHeap ∧ ∀ (value : Unit) (finalHeap : Heap), post✝ (Env.cons xs (Env.cons ys (Env.cons limit Env.empty))) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ boundedMapPair xs ys 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.

              @[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.Traversal.Implementation.increment_spec {pre✝ : Env [Ty.nat] → Heap → Prop} {post✝ : Env [Ty.nat] → Heap → ℕ → Heap → Prop} :
                FunctionTotal program incrementId pre✝ post✝ → ∀ (x : ℕ) (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.empty) initialHeap ∧ ∀ (value : ℕ) (finalHeap : Heap), post✝ (Env.cons x Env.empty) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ increment x ⦃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.

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

                @[reducible, inline]

                Relational total correctness with ordinary mutable parameters and fixed captures. The existing block contract retains both actual heaps and excludes faults.

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

                  The actual value-producing guard, reevaluated on every iteration.

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

                    Relational total correctness with ordinary mutable parameters and fixed captures. The existing block contract retains both actual heaps and excludes faults.

                    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.

                      theorem Complexity.Language.Examples.Traversal.Implementation.increment_total_iff (pre✝ : ℕ → Heap → Prop) (post✝ : ℕ → Heap → ℕ → Heap → Prop) :
                      increment_contract pre✝ post✝ ↔ ∀ (x : ℕ) (initialHeap : Heap), pre✝ x initialHeap → ∃ (value : ℕ) (finalHeap : Heap), increment x initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ x initialHeap value finalHeap

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

                      Coordinate conversion for composing this named observation.

                      theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.wellFounded_spec (xs : Buffer CellTy.nat) (limit : ℕ) (invariant✝ : ℕ → Heap → Prop) (relation✝ : Mutable × Heap → Mutable × Heap → Prop) :
                      WellFounded relation✝ → ∀ (normal : ℕ → Heap → Prop) (returned : Unit → ℕ → Heap → Prop), (∀ (i : ℕ) (startHeap : Heap), invariant✝ i startHeap → ⦃fun (currentHeap : Heap) => { down := currentHeap = startHeap }⦄ guard i xs limit ⦃(fun (guardOutcome : Control Ty.bool × Locals) (afterGuardHeap : Heap) => { down := have afterGuard_i := guardOutcome.2.1; match guardOutcome.1 with | Control.returned again => if again = true then ⦃fun (currentHeap : Heap) => { down := currentHeap = afterGuardHeap }⦄ body afterGuard_i xs limit ⦃(fun (bodyOutcome : Control Ty.unit × Locals) (afterBodyHeap : Heap) => { down := have afterBody_i := bodyOutcome.2.1; match bodyOutcome.1 with | Control.normal => invariant✝ afterBody_i afterBodyHeap ∧ relation✝ ((afterBody_i, ()), afterBodyHeap) ((i, ()), startHeap) | Control.returned value => returned value afterBody_i afterBodyHeap | Control.fault a => False }, PUnit.unit)⦄ else normal afterGuard_i afterGuardHeap | Control.normal => False | Control.fault a => False }, PUnit.unit)⦄) → ∀ (i : ℕ), ⦃fun (heap : Heap) => { down := invariant✝ i heap }⦄ boundedMap_loop1 i xs limit ⦃(fun (outcome : Control Ty.unit × Locals) (heap : Heap) => { down := have final_i := outcome.2.1; match outcome.1 with | Control.normal => normal final_i heap | Control.returned value => returned value final_i heap | Control.fault a => False }, PUnit.unit)⦄

                      Prove this actual loop with fixed captures and well-founded progress on its mutable locals. Only normal body exits must decrease; guard effects, returns and faults retain their real heap.

                      theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.contract_iff (xs : Buffer CellTy.nat) (limit : ℕ) (pre✝ : ℕ → Heap → Prop) (normal✝ : ℕ → Heap → ℕ → Heap → Prop) (returned✝ : ℕ → Heap → Unit → ℕ → Heap → Prop) :
                      contract xs limit pre✝ normal✝ returned✝ ↔ ∀ (i : ℕ) (heap : Heap), pre✝ i heap → ⦃fun (current : Heap) => { down := current = heap }⦄ boundedMap_loop1 i xs limit ⦃(fun (outcome : Control Ty.unit × Locals) (finish : Heap) => { down := match outcome.1 with | Control.normal => have after := outcome.2; normal✝ i heap after.1 finish | Control.returned value => have after := outcome.2; returned✝ i heap value after.1 finish | Control.fault a => False }, PUnit.unit)⦄

                      Introduce named mutable variables directly, without unpacking internal coordinates.

                      The actual parsed lexical body, with its enclosing return type.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        theorem Complexity.Language.Examples.Traversal.Implementation.boundedMapPair_total_iff (pre✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → Unit → Heap → Prop) :
                        boundedMapPair_contract pre✝ post✝ ↔ ∀ (xs ys : Buffer CellTy.nat) (limit : ℕ) (initialHeap : Heap), pre✝ xs ys limit initialHeap → ∃ (value : Unit) (finalHeap : Heap), boundedMapPair xs ys limit initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ xs ys limit initialHeap value finalHeap

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

                        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.Traversal.Implementation.increment_total :
                          increment_contract (fun (x_source : Value Ty.nat) (initialHeap : Heap) => True) fun (x_source : Value Ty.nat) (initialHeap : Heap) (returned : Value Ty.nat) (finalHeap : Heap) => returned = increment_model ((fun (value : ℕ) => value) x_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.

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

                          Source lexical immutability preserves these captures on every actual exit.

                          theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.rel_contract (xs : Buffer CellTy.nat) (limit : ℕ) {Model✝ : Type} (stateRel✝ : Model✝ → ℕ → Heap → Prop) (invariant✝ : Model✝ → Prop) {relation✝ : Model✝ → Model✝ → Prop} :
                          WellFounded relation✝ → ∀ (ready : Model✝ → ℕ → Heap → ℕ → Heap → Prop) (normal : ℕ → Heap → Prop) (returned : Unit → ℕ → Heap → Prop), (∀ (model : Model✝), invariant✝ model → guard_contract xs limit (stateRel✝ model) (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (finish : Heap) => False) fun (i : ℕ) (heap : Heap) (again : Bool) (after_i : ℕ) (finish : Heap) => if again = true then ready model i heap after_i finish else normal after_i finish) → (∀ (model : Model✝) (i : ℕ) (heap : Heap), invariant✝ model → stateRel✝ model i heap → body_contract xs limit (ready model i heap) (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (finish : Heap) => ∃ (next : Model✝), invariant✝ next ∧ stateRel✝ next after_i finish ∧ relation✝ next model) fun (x : ℕ) (x_1 : Heap) (value : Unit) (after_i : ℕ) (finish : Heap) => returned value after_i finish) → ∀ (model : Model✝), invariant✝ model → contract xs limit (stateRel✝ model) (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (finish : Heap) => normal after_i finish) fun (x : ℕ) (x_1 : Heap) (value : Unit) (after_i : ℕ) (finish : Heap) => returned value after_i finish

                          Prove this actual loop using a related mathematical state and well-founded progress. Fixed captures are retained by the generated execution frames.

                          The actual source block observed with ordinary named local inputs.

                          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.Traversal.Implementation.boundedMapPair_contract_iff_triple (pre✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → Unit → Heap → Prop) :
                              boundedMapPair_contract pre✝ post✝ ↔ ∀ (xs ys : Buffer CellTy.nat) (limit : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ xs ys limit initialHeap }⦄ boundedMapPair xs ys limit ⦃(fun (value : Unit) (finalHeap : Heap) => { down := post✝ xs ys 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.

                              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.Traversal.Implementation.increment_contract_iff_triple (pre✝ : ℕ → Heap → Prop) (post✝ : ℕ → Heap → ℕ → Heap → Prop) :
                                increment_contract pre✝ post✝ ↔ ∀ (x : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ x initialHeap }⦄ increment x ⦃(fun (value : ℕ) (finalHeap : Heap) => { down := post✝ x 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.

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

                                theorem Complexity.Language.Examples.Traversal.Implementation.increment_action_rel_native (x : ℕ) (initialHeap✝ : Heap) :
                                ∃ (returned : Value Ty.nat) (finish : Heap), increment x initialHeap✝ = Part.some (Except.ok returned, finish) ∧ (Representation.ofEmbedding (Function.Embedding.refl ℕ)).Rel (increment_model x) 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.

                                The same complete environment, grouped by source mutability.

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

                                  Source lexical immutability preserves these captures on every actual exit.

                                  theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.wellFounded_contract (xs : Buffer CellTy.nat) (limit : ℕ) (invariant✝ : ℕ → Heap → Prop) (relation✝ : Mutable × Heap → Mutable × Heap → Prop) :
                                  WellFounded relation✝ → ∀ (ready : ℕ → Heap → ℕ → Heap → Prop) (normal : ℕ → Heap → Prop) (returned : Unit → ℕ → Heap → Prop), (guard_contract xs limit invariant✝ (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (finish : Heap) => False) fun (i : ℕ) (heap : Heap) (again : Bool) (after_i : ℕ) (finish : Heap) => if again = true then ready i heap after_i finish else normal after_i finish) → (∀ (i : ℕ) (heap : Heap), invariant✝ i heap → body_contract xs limit (ready i heap) (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (finish : Heap) => invariant✝ after_i finish ∧ relation✝ ((after_i, ()), finish) ((i, ()), heap)) fun (x : ℕ) (x_1 : Heap) (value : Unit) (after_i : ℕ) (finish : Heap) => returned value after_i finish) → contract xs limit invariant✝ (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (finish : Heap) => normal after_i finish) fun (x : ℕ) (x_1 : Heap) (value : Unit) (after_i : ℕ) (finish : Heap) => returned value after_i finish

                                  Prove a named loop from independent mathematical guard and body contracts. Actual guard effects and early body returns are retained; only normal iterations decrease.

                                  Observe lexical coordinates without unfolding the equivalence structure.

                                  The actual source block observed with ordinary named local inputs.

                                  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
                                      theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.variant_contract (xs : Buffer CellTy.nat) (limit : ℕ) (invariant✝ : ℕ → Heap → Prop) (variant✝ : ℕ → Heap → ℕ) (ready✝ : ℕ → Heap → ℕ → Heap → Prop) (normal✝ : ℕ → Heap → Prop) (returned✝ : Unit → ℕ → Heap → Prop) :
                                      (guard_contract xs limit invariant✝ (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (finish : Heap) => False) fun (i : ℕ) (heap : Heap) (again : Bool) (after_i : ℕ) (finish : Heap) => if again = true then ready✝ i heap after_i finish else normal✝ after_i finish) → (∀ (i : ℕ) (heap : Heap), invariant✝ i heap → body_contract xs limit (ready✝ i heap) (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (finish : Heap) => invariant✝ after_i finish ∧ variant✝ after_i finish < variant✝ i heap) fun (x : ℕ) (x_1 : Heap) (value : Unit) (after_i : ℕ) (finish : Heap) => returned✝ value after_i finish) → contract xs limit invariant✝ (fun (x : ℕ) (x_1 : Heap) (after_i : ℕ) (finish : Heap) => normal✝ after_i finish) fun (x : ℕ) (x_1 : Heap) (value : Unit) (after_i : ℕ) (finish : Heap) => returned✝ value after_i finish

                                      Prove a named loop from independent mathematical guard and body contracts. Actual guard effects and early body returns are retained; only normal iterations decrease.

                                      theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.body_spec (xs : Buffer CellTy.nat) (limit : ℕ) {pre✝ : ℕ → Heap → Prop} {normal✝ : ℕ → Heap → ℕ → Heap → Prop} {returned✝ : ℕ → Heap → Unit → ℕ → Heap → Prop} :
                                      body_contract xs limit pre✝ normal✝ returned✝ → ∀ (i : ℕ) (post : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ i heap ∧ (∀ (after_i : ℕ) (finish : Heap), normal✝ i heap after_i finish → (post.1 (Control.normal, after_i, xs, limit, ()) finish).down) ∧ ∀ (value : Unit) (after_i : ℕ) (finish : Heap), returned✝ i heap value after_i finish → (post.1 (Control.returned value, after_i, xs, limit, ()) finish).down }⦄ body i xs limit ⦃post⦄

                                      Apply a chosen mathematical contract to the native block action. Continuations see ordinary mutable outputs; proved frames restore the fixed captures.

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

                                      theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.body_contract_iff (xs : Buffer CellTy.nat) (limit : ℕ) (pre✝ : ℕ → Heap → Prop) (normal✝ : ℕ → Heap → ℕ → Heap → Prop) (returned✝ : ℕ → Heap → Unit → ℕ → Heap → Prop) :
                                      body_contract xs limit pre✝ normal✝ returned✝ ↔ ∀ (i : ℕ) (heap : Heap), pre✝ i heap → ⦃fun (current : Heap) => { down := current = heap }⦄ body i xs limit ⦃(fun (outcome : Control Ty.unit × Locals) (finish : Heap) => { down := match outcome.1 with | Control.normal => have after := outcome.2; normal✝ i heap after.1 finish | Control.returned value => have after := outcome.2; returned✝ i heap value after.1 finish | Control.fault a => False }, PUnit.unit)⦄

                                      Introduce named mutable variables directly, without unpacking internal coordinates.

                                      theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_eq (xs : Buffer CellTy.nat) (limit : ℕ) :
                                      boundedMap xs limit = have i := 0; have positiveStep := ⋯; do let __discr ← MonadLift.monadLift (boundedMap_loop1 i xs limit) match __discr with | (loopControl, loopLocals) => have i := loopLocals.1; have __do_jp := fun (y : Unit) => pure (); match loopControl with | Control.normal => do let y ← pure () __do_jp y | Control.returned returned => pure returned | Control.fault error => do let y ← throw error __do_jp y

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

                                      theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.guard_contract_iff (xs : Buffer CellTy.nat) (limit : ℕ) (pre✝ : ℕ → Heap → Prop) (normal✝ : ℕ → Heap → ℕ → Heap → Prop) (returned✝ : ℕ → Heap → Bool → ℕ → Heap → Prop) :
                                      guard_contract xs limit pre✝ normal✝ returned✝ ↔ ∀ (i : ℕ) (heap : Heap), pre✝ i heap → ⦃fun (current : Heap) => { down := current = heap }⦄ guard i xs limit ⦃(fun (outcome : Control Ty.bool × Locals) (finish : Heap) => { down := match outcome.1 with | Control.normal => have after := outcome.2; normal✝ i heap after.1 finish | Control.returned value => have after := outcome.2; returned✝ i heap value after.1 finish | Control.fault a => False }, PUnit.unit)⦄

                                      Introduce named mutable variables directly, without unpacking internal coordinates.

                                      Restore grouped locals using the checked coordinate projection laws.

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

                                        Source lexical immutability preserves these captures on every actual exit.

                                        theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_total_iff (pre✝ : Buffer CellTy.nat → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → ℕ → Heap → Unit → Heap → Prop) :
                                        boundedMap_contract pre✝ post✝ ↔ ∀ (xs : Buffer CellTy.nat) (limit : ℕ) (initialHeap : Heap), pre✝ xs limit initialHeap → ∃ (value : Unit) (finalHeap : Heap), boundedMap 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 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 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
                                              theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_contract_iff_triple (pre✝ : Buffer CellTy.nat → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → ℕ → Heap → Unit → Heap → Prop) :
                                              boundedMap_contract pre✝ post✝ ↔ ∀ (xs : Buffer CellTy.nat) (limit : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ xs limit initialHeap }⦄ boundedMap xs limit ⦃(fun (value : Unit) (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.

                                              This actual source block, retaining its loop or allocation-scope semantics.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.regroup_symm_apply (grouped✝ : Mutable × Captured) :
                                                Regroup.symm grouped✝ = (grouped✝.1.1, grouped✝.2.1, grouped✝.2.2.1, ())

                                                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]

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

                                                          Ordinary mathematical function generated from the same represented source block.

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

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

                                                              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.Traversal.Implementation.boundedMap_loop1.variant_spec (xs : Buffer CellTy.nat) (limit : ℕ) (invariant✝ : ℕ → Heap → Prop) (variant✝ : ℕ → Heap → ℕ) (normal✝ : ℕ → Heap → Prop) (returned✝ : Unit → ℕ → Heap → Prop) :
                                                                (∀ (i : ℕ) (startHeap : Heap), invariant✝ i startHeap → ⦃fun (currentHeap : Heap) => { down := currentHeap = startHeap }⦄ guard i xs limit ⦃(fun (guardOutcome : Control Ty.bool × Locals) (afterGuardHeap : Heap) => { down := have afterGuard_i := guardOutcome.2.1; match guardOutcome.1 with | Control.returned again => if again = true then ⦃fun (currentHeap : Heap) => { down := currentHeap = afterGuardHeap }⦄ body afterGuard_i xs limit ⦃(fun (bodyOutcome : Control Ty.unit × Locals) (afterBodyHeap : Heap) => { down := have afterBody_i := bodyOutcome.2.1; match bodyOutcome.1 with | Control.normal => invariant✝ afterBody_i afterBodyHeap ∧ variant✝ afterBody_i afterBodyHeap < variant✝ i startHeap | Control.returned value => returned✝ value afterBody_i afterBodyHeap | Control.fault a => False }, PUnit.unit)⦄ else normal✝ afterGuard_i afterGuardHeap | Control.normal => False | Control.fault a => False }, PUnit.unit)⦄) → ∀ (i : ℕ), ⦃fun (heap : Heap) => { down := invariant✝ i heap }⦄ boundedMap_loop1 i xs limit ⦃(fun (outcome : Control Ty.unit × Locals) (heap : Heap) => { down := have final_i := outcome.2.1; match outcome.1 with | Control.normal => normal✝ final_i heap | Control.returned value => returned✝ value final_i heap | Control.fault a => False }, PUnit.unit)⦄

                                                                Prove this actual loop with fixed captures and well-founded progress on its mutable locals. Only normal body exits must decrease; guard effects, returns and faults retain their real heap.

                                                                @[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.Traversal.Implementation.increment_action_rel_native_preserving (x : ℕ) (initialHeap✝ : Heap) :
                                                                  ∃ (returned : Value Ty.nat) (finish : Heap), increment x initialHeap✝ = Part.some (Except.ok returned, finish) ∧ (Representation.ofEmbedding (Function.Embedding.refl ℕ)).Rel (increment_model x) 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.

                                                                  Lossless proof coordinates, including fixed and shadowed captures.

                                                                  Equations
                                                                  • One or more equations did not get rendered due to their size.
                                                                  Instances For
                                                                    theorem Complexity.Language.Examples.Traversal.Implementation.boundedMap_loop1.body_observe (locals✝ : Locals) :
                                                                    Stmt.observe View Body program locals✝ = body locals✝.1 locals✝.2.1 locals✝.2.2.1

                                                                    Coordinate conversion for composing this named observation.

                                                                    The actual helper returns the mathematical increment and preserves every initial heap. This statement does not impose a machine word width or budget.

                                                                    def Complexity.Language.Examples.Traversal.invariant (xs : Buffer CellTy.nat) (limit : ℕ) (contents : Array ℕ) (i : ℕ) (heap : Heap) :

                                                                    At the traversal boundary, the processed prefix uses the mathematical update while the unread suffix still has its original values.

                                                                    Equations
                                                                    • One or more equations did not get rendered due to their size.
                                                                    Instances For
                                                                      theorem Complexity.Language.Examples.Traversal.invariant_zero (xs : Buffer CellTy.nat) (limit : ℕ) (contents : Array ℕ) {heap : Heap} (observed : xs.Contents heap contents) :
                                                                      invariant xs limit contents 0 heap

                                                                      Before the first iteration, the mapped prefix is empty.

                                                                      theorem Complexity.Language.Examples.Traversal.invariant_read (xs : Buffer CellTy.nat) (limit : ℕ) (contents : Array ℕ) {i : ℕ} {heap : Heap} (current : invariant xs limit contents i heap) (bound : i < contents.size) :
                                                                      heap.read xs i = Except.ok contents[i]

                                                                      The next cell still contains its original mathematical value.

                                                                      theorem Complexity.Language.Examples.Traversal.guard_contract (xs : Buffer CellTy.nat) (limit : ℕ) (contents : Array ℕ) :
                                                                      Implementation.boundedMap_loop1.guard_contract xs limit (invariant xs limit contents) (fun (x : ℕ) (x_1 : Heap) (x_2 : ℕ) (x_3 : Heap) => False) fun (i : ℕ) (heap : Heap) (again : Bool) (j : ℕ) (finish : Heap) => j = i ∧ finish = heap ∧ invariant xs limit contents j finish ∧ (again = true ↔ j < contents.size)

                                                                      The guard returns its ordinary mathematical decision and preserves the index and heap. Fixed captures stay inside the generated block interface.

                                                                      theorem Complexity.Language.Examples.Traversal.body_contract (xs : Buffer CellTy.nat) (limit : ℕ) (contents : Array ℕ) :
                                                                      Implementation.boundedMap_loop1.body_contract xs limit (fun (i : ℕ) (heap : Heap) => invariant xs limit contents i heap ∧ i < contents.size) (fun (i : ℕ) (heap : Heap) (j : ℕ) (finish : Heap) => j = i + 1 ∧ invariant xs limit contents j finish ∧ xs.PreservesOutside heap finish) fun (x : ℕ) (x_1 : Heap) (x_2 : Unit) (x_3 : ℕ) (x_4 : Heap) => False

                                                                      One native body proof advances the mathematical prefix and retains every outside-buffer observation at the same actual final heap.

                                                                      theorem Complexity.Language.Examples.Traversal.loop_contract (xs : Buffer CellTy.nat) (limit : ℕ) (contents : Array ℕ) (initial : Heap) :
                                                                      Implementation.boundedMap_loop1.contract xs limit (fun (i : ℕ) (heap : Heap) => invariant xs limit contents i heap ∧ xs.PreservesOutside initial heap) (fun (x : ℕ) (x_1 : Heap) (x_2 : ℕ) (heap : Heap) => xs.Contents heap (Array.map (fun (x : CellValue CellTy.nat) => min (x + 1) limit) contents) ∧ xs.PreservesOutside initial heap) fun (x : ℕ) (x_1 : Heap) (x_2 : Unit) (x_3 : ℕ) (x_4 : Heap) => False

                                                                      The prefix invariant and outside-buffer frame compose across the actual iterations. Descent is ordinary subtraction on the number of unread cells; the guard and body are specified independently, with their real endpoint heaps.

                                                                      theorem Complexity.Language.Examples.Traversal.boundedMap_eval_frame (xs : Buffer CellTy.nat) (limit : ℕ) {contents : Array ℕ} {heap : Heap} (observed : xs.Contents heap contents) :
                                                                      ∃ (finish : Heap), Implementation.boundedMap xs limit heap = Part.some (Except.ok (), finish) ∧ xs.Contents finish (Array.map (fun (x : CellValue CellTy.nat) => min (x + 1) limit) contents) ∧ xs.PreservesOutside heap finish

                                                                      The traversal's ordinary array result and its outside-buffer frame hold at the same actual final heap. This includes disjoint slices of the same object.

                                                                      theorem Complexity.Language.Examples.Traversal.boundedMap_eval (xs : Buffer CellTy.nat) (limit : ℕ) {contents : Array ℕ} {heap : Heap} (observed : xs.Contents heap contents) :
                                                                      ∃ (finish : Heap), Implementation.boundedMap xs limit heap = Part.some (Except.ok (), finish) ∧ xs.Contents finish (Array.map (fun (x : CellValue CellTy.nat) => min (x + 1) limit) contents)

                                                                      The declared traversal terminates and updates the actual buffer to the ordinary native array map. This is the same source program, not a host map used as its implementation, and correctness imposes no proposed runtime budget.

                                                                      theorem Complexity.Language.Examples.Traversal.boundedMap_total (contents : Array ℕ) :
                                                                      Implementation.boundedMap_contract (fun (xs : Buffer CellTy.nat) (x : ℕ) (heap : Heap) => xs.Contents heap contents) fun (xs : Buffer CellTy.nat) (limit : ℕ) (x : Heap) (x_1 : Unit) (finish : Heap) => xs.Contents finish (Array.map (fun (x : CellValue CellTy.nat) => min (x + 1) limit) contents)

                                                                      The ordinary array specification transfers to the source function contract through its generated argument interface, with no register or environment proof.

                                                                      theorem Complexity.Language.Examples.Traversal.boundedMap_total_frame (contents : Array ℕ) :
                                                                      Implementation.boundedMap_contract (fun (xs : Buffer CellTy.nat) (x : ℕ) (heap : Heap) => xs.Contents heap contents) fun (xs : Buffer CellTy.nat) (limit : ℕ) (heap : Heap) (x : Unit) (finish : Heap) => xs.Contents finish (Array.map (fun (x : CellValue CellTy.nat) => min (x + 1) limit) contents) ∧ xs.PreservesOutside heap finish

                                                                      Callers receive the same array-map specification together with preservation of every disjoint borrowed view, relative to their actual calling heap.

                                                                      Observe ordinary array input and output without changing the in-place implementation or its Unit return. The output is read at the final heap.

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

                                                                        The existing implementation proof supplies the shared mathematical correspondence; it is not another implementation or loop induction.

                                                                        An ordinary array theorem composes with implementation correspondence. This proves a property of the same source execution without reopening its loop.