Documentation

Examples.Language.Scope

Reusing nested temporary storage across source calls #

Implementation.make allocates one result cell outside the temporary scopes. Each call to Implementation.work allocates two nested temporary arrays, reads the outer temporary array after the inner scope has released its storage, and writes the result cell. A return from inside the outer scope also releases its temporary storage. The returned result is longer-lived than either scratch array; no copied host implementation or algorithm-specific lowering is used.

The mathematical result depends on whether an iteration and a nonempty read occur, while the required temporary capacity is independent of the iteration count. Source correctness and termination do not require a RAM capacity or instruction budget.

theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.body_spec (out✝ : Buffer CellTy.nat) (count n value : ℕ) {pre✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop} {normal✝ : ℕ → Option (Buffer CellTy.nat) → Heap → ℕ → Option (Buffer CellTy.nat) → Heap → Prop} {returned✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Buffer CellTy.nat → ℕ → Option (Buffer CellTy.nat) → Heap → Prop} :
body_contract out✝ count n value pre✝ normal✝ returned✝ → ∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (post : Std.Do.PostCond (Control (Ty.buffer CellTy.nat) × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ remaining result_join heap ∧ (∀ (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap), normal✝ remaining result_join heap after_remaining after_result_join finish → (post.1 (Control.normal, after_remaining, out✝, after_result_join, count, n, value, ()) finish).down) ∧ ∀ (value_1 : Buffer CellTy.nat) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap), returned✝ remaining result_join heap value_1 after_remaining after_result_join finish → (post.1 (Control.returned value_1, after_remaining, out✝, after_result_join, count, n, value, ()) finish).down }⦄ body remaining out✝ result_join count n value ⦃post⦄

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

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

theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.body_observe (locals✝ : Locals) :
Stmt.observe View Body program locals✝ = body locals✝.1 locals✝.2.1 locals✝.2.2.1 locals✝.2.2.2.1 locals✝.2.2.2.2.1

Coordinate conversion for composing this named observation.

theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.view_symm_apply (values✝ : Locals) :
View.symm values✝ = Env.cons values✝.1 (Env.cons values✝.2.1 (Env.cons values✝.2.2.1 (Env.cons values✝.2.2.2.1 (Env.cons values✝.2.2.2.2.1 Env.empty))))

Restore complete lexical coordinates without unfolding proof fields.

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.Scope.Implementation.Source.work_scope1.visible_reconstruct (initial✝ : Locals) (pending✝ : Option Unit) (visible✝ : Visible) :
    visible (reconstruct initial✝ pending✝ visible✝) = visible✝
    @[reducible, inline]

    Author relations use visible locals and actual endpoint heaps. The existing action still returns its full locals and actual source control.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope2.spec (scratchPending✝ : Option Unit) (temp✝ : Buffer CellTy.nat) (scratchPending✝¹ result_join✝ : Option Unit) (out : Buffer CellTy.nat) (n value : ℕ) (post✝ : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)) :
      ⦃fun (heap : Heap) => (Std.Do.wp (body scratchPending✝ temp✝ scratchPending✝¹ result_join✝ out n value)).apply (fun (outcome : Control Ty.unit × Locals) (finish : Heap) => { down := ScopeSafe heap { locals := View.symm outcome.2, heap := finish } outcome.1 ∧ (post✝.1 outcome (finish.take heap.objects.size)).down }, PUnit.unit) heap⦄ work_scope2 scratchPending✝ temp✝ scratchPending✝¹ result_join✝ out n value ⦃post✝⦄

      Compose the actual body specification with safe scratch release. Surviving locals and return values stay rooted; current retained contents are not rolled back.

      @[reducible, inline]

      Enter this source boundary with empty private completion slots.

      Equations
      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

          Local completion does not produce an enclosing source return.

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

            Observe the same body and its actual local completion slot. Mapping the result neither changes source control nor performs heap cleanup.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              theorem Complexity.Language.Examples.Scope.Implementation.Source.make_spec {pre✝ : Env [Ty.nat, Ty.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.nat, Ty.nat, Ty.nat] → Heap → Buffer CellTy.nat → Heap → Prop} :
              FunctionTotal program makeId pre✝ post✝ → ∀ (count n value : ℕ) (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 count (Env.cons n (Env.cons value Env.empty))) initialHeap ∧ ∀ (value_1 : Buffer CellTy.nat) (finalHeap : Heap), post✝ (Env.cons count (Env.cons n (Env.cons value Env.empty))) initialHeap value_1 finalHeap → (continuation.1 value_1 finalHeap).down }⦄ make count n value ⦃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 completion observation is the actual source slot read.

              @[reducible, inline]

              Complete lexical coordinates for this source block.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                @[reducible, inline]
                abbrev Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.body_model_contract {Index✝ : Type} :
                (Index✝ → Model) → Index✝ → (Heap → Prop) → (Heap → Index✝ → Heap → Prop) → (Heap → Buffer CellTy.nat → Index✝ → Heap → Prop) → Prop

                A supplied body contract may change the heap or complete the local block. Continuing and completed results have separate mathematical postconditions.

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

                  Coordinate conversion for composing this named observation.

                  A lossless coordinate permutation; no source operation is executed.

                  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
                      theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.contract_iff (out✝ : Buffer CellTy.nat) (count n value : ℕ) (pre✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (normal✝ : ℕ → Option (Buffer CellTy.nat) → Heap → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (returned✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Buffer CellTy.nat → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) :
                      contract out✝ count n value pre✝ normal✝ returned✝ ↔ ∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap), pre✝ remaining result_join heap → ⦃fun (current : Heap) => { down := current = heap }⦄ make_loop1 remaining out✝ result_join count n value ⦃(fun (outcome : Control (Ty.buffer CellTy.nat) × Locals) (finish : Heap) => { down := match outcome.1 with | Control.normal => have after := outcome.2; normal✝ remaining result_join heap after.1 after.2.2.1 finish | Control.returned value => have after := outcome.2; returned✝ remaining result_join heap value after.1 after.2.2.1 finish | Control.fault a => False }, PUnit.unit)⦄

                      Introduce named mutable variables directly, without unpacking internal coordinates.

                      @[reducible, inline]

                      Source-visible locals, without compiler completion slots.

                      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

                          The source-named mathematical value of this local.

                          Equations
                          Instances For
                            @[reducible, inline]

                            Author relations use visible locals and actual endpoint heaps. The existing action still returns its full locals and actual source control.

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

                              Observe lexical coordinates without unfolding the equivalence structure.

                              theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope2.body_completion_spec {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                              body_completion_contract pre✝ normal✝ returned✝ → ∀ (temp out : Buffer CellTy.nat) (n value : ℕ) (post : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ (temp, out, n, value, ()) heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ (temp, out, n, value, ()) heap output finish → (post.1 (Control.normal, reconstruct (entry (temp, out, n, value, ())) none output) finish).down) ∧ ∀ (value_1 : Unit) (output : Visible) (finish : Heap), returned✝ (temp, out, n, value, ()) heap value_1 output finish → (post.1 (Control.normal, reconstruct (entry (temp, out, n, value, ())) (some value_1) output) finish).down }⦄ body none temp none none out n value ⦃post⦄

                              Apply the same completion contract at independently named source inputs. The actual action has canonical arguments, so native specification lookup does not need to infer a tuple from its projections.

                              @[simp]
                              theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.modelEquiv_symm_tuple (remaining✝ : ℕ) (out✝ : Value (Ty.buffer CellTy.nat)) (value✝ n✝ count✝ : ℕ) :
                              modelEquiv.symm (remaining✝, out✝, count✝, n✝, value✝, ()) = mkModel remaining✝ out✝ value✝ n✝ count✝

                              Actual visible coordinates reconstruct the named mathematical locals.

                              The source-named mathematical value of this local.

                              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.Scope.Implementation.Source.work_scope2.continue_eq (entry✝ : Env [Ty.unit.option, Ty.buffer CellTy.nat, Ty.unit.option, Ty.unit.option, Ty.buffer CellTy.nat, Ty.nat, Ty.nat]) (next✝ : Env [Ty.unit.option, Ty.buffer CellTy.nat, Ty.unit.option, Ty.unit.option, Ty.buffer CellTy.nat, Ty.nat, Ty.nat] → ExceptT Fault (StateT Heap Part) Unit) :
                                  Code.evalWith program entry✝ next✝ = do let __discr ← MonadLift.monadLift (work_scope2 (View entry✝).1 (View entry✝).2.1 (View entry✝).2.2.1 (View entry✝).2.2.2.1 (View entry✝).2.2.2.2.1 (View entry✝).2.2.2.2.2.1 (View entry✝).2.2.2.2.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 locals.2.2.1 (Env.cons locals.2.2.2.1 (Env.cons (entry✝.get Var.here.there.there.there.there) (Env.cons (entry✝.get Var.here.there.there.there.there.there) (Env.cons (entry✝.get Var.here.there.there.there.there.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.

                                  theorem Complexity.Language.Examples.Scope.Implementation.Source.make_total_iff (pre✝ : ℕ → ℕ → ℕ → Heap → Prop) (post✝ : ℕ → ℕ → ℕ → Heap → Buffer CellTy.nat → Heap → Prop) :
                                  make_contract pre✝ post✝ ↔ ∀ (count n value : ℕ) (initialHeap : Heap), pre✝ count n value initialHeap → ∃ (value_1 : Buffer CellTy.nat) (finalHeap : Heap), make count n value initialHeap = Part.some (Except.ok value_1, finalHeap) ∧ post✝ count n value initialHeap value_1 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.Scope.Implementation.Source.make_loop1.body_completion_spec {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Buffer CellTy.nat → Visible → Heap → Prop} :
                                  body_completion_contract pre✝ normal✝ returned✝ → ∀ (remaining : ℕ) (out : Buffer CellTy.nat) (count n value : ℕ) (post : Std.Do.PostCond (Control (Ty.buffer CellTy.nat) × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ (remaining, out, count, n, value, ()) heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ (remaining, out, count, n, value, ()) heap output finish → (post.1 (Control.normal, reconstruct (entry (remaining, out, count, n, value, ())) none output) finish).down) ∧ ∀ (value_1 : Buffer CellTy.nat) (output : Visible) (finish : Heap), returned✝ (remaining, out, count, n, value, ()) heap value_1 output finish → (post.1 (Control.normal, reconstruct (entry (remaining, out, count, n, value, ())) (some value_1) output) finish).down }⦄ body remaining out none count n value ⦃post⦄

                                  Apply the same completion contract at independently named source inputs. The actual action has canonical arguments, so native specification lookup does not need to infer a tuple from its projections.

                                  theorem Complexity.Language.Examples.Scope.Implementation.Source.work_spec {pre✝ : Env [Ty.buffer CellTy.nat, Ty.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.nat, Ty.nat, Ty.nat] → Heap → Unit → Heap → Prop} :
                                  FunctionTotal program workId pre✝ post✝ → ∀ (out : Buffer CellTy.nat) (n value : ℕ) (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 out (Env.cons n (Env.cons value Env.empty))) initialHeap ∧ ∀ (value_1 : Unit) (finalHeap : Heap), post✝ (Env.cons out (Env.cons n (Env.cons value Env.empty))) initialHeap value_1 finalHeap → (continuation.1 value_1 finalHeap).down }⦄ work out n value ⦃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.

                                  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.Scope.Implementation.Source.make_loop1.scopeSafe_reconstruct (rootHeap✝ finalHeap✝ : Heap) (start✝ visible✝ : Visible) (pending✝ : Option (Buffer CellTy.nat)) :
                                    ScopeSafe rootHeap✝ { locals := View.symm (reconstruct (entry start✝) pending✝ visible✝), heap := finalHeap✝ } Control.normal ↔ VisibleRooted rootHeap✝ visible✝ ∧ ValueRooted rootHeap✝ pending✝

                                    The original full scratch safety condition after actual frame restoration. Ancestor slots are empty at entry; the current completion remains a checked root.

                                    Actual finite execution fixes every ancestor completion slot. This restores full locals; it does not discard roots or assume successful cleanup.

                                    theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.body_completion_spec_at {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                    body_completion_contract pre✝ normal✝ returned✝ → ∀ (start : Visible) (post : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ start heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ start heap output finish → (post.1 (Control.normal, reconstruct (entry start) none output) finish).down) ∧ ∀ (value : Unit) (output : Visible) (finish : Heap), returned✝ start heap value output finish → (post.1 (Control.normal, reconstruct (entry start) (some value) output) finish).down }⦄ body (entry start).1 (entry start).2.1 (entry start).2.2.1 (entry start).2.2.2.1 (entry start).2.2.2.2.1 ⦃post⦄

                                    Apply the visible completion contract, reconstructing the full output from this same actual execution rather than guessing hidden coordinates.

                                    The completion observation is the actual source slot read.

                                    theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope2.view_symm_apply (values✝ : Locals) :
                                    View.symm values✝ = Env.cons values✝.1 (Env.cons values✝.2.1 (Env.cons values✝.2.2.1 (Env.cons values✝.2.2.2.1 (Env.cons values✝.2.2.2.2.1 (Env.cons values✝.2.2.2.2.2.1 (Env.cons values✝.2.2.2.2.2.2.1 Env.empty))))))

                                    Restore complete lexical coordinates without unfolding proof fields.

                                    theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.visibleModelRel_iff (model✝ : Model) (locals✝ : Visible) (heap✝ : Heap) :
                                    visibleModelRel model✝ locals✝ heap✝ ↔ locals✝ = modelEquiv model✝

                                    Complete identity observations coincide with the proved coordinate equivalence.

                                    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
                                      @[simp]
                                      theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.modelEquiv_mkModel (remaining✝ : ℕ) (out✝ : Value (Ty.buffer CellTy.nat)) (value✝ n✝ count✝ : ℕ) :
                                      modelEquiv (mkModel remaining✝ out✝ value✝ n✝ count✝) = (remaining✝, out✝, count✝, n✝, value✝, ())

                                      A named mathematical constructor has the corresponding visible coordinates.

                                      Actual finite execution fixes every ancestor completion slot. This restores full locals; it does not discard roots or assume successful cleanup.

                                      theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.body_model_contract_iff {Index✝ : Type} (encode✝ : Index✝ → Model) (index✝ : Index✝) (pre✝ : Heap → Prop) (normal✝ : Heap → Index✝ → Heap → Prop) (completed✝ : Heap → Buffer CellTy.nat → Index✝ → Heap → Prop) :
                                      body_model_contract encode✝ index✝ pre✝ normal✝ completed✝ ↔ ∀ (heap : Heap), pre✝ heap → ⦃fun (current : Heap) => { down := current = heap }⦄ body_model_action (encode✝ index✝) ⦃(fun (outcome : Control (Ty.buffer CellTy.nat) × Option (Buffer CellTy.nat) × Model) (finish : Heap) => { down := match outcome.1 with | Control.normal => match outcome.2.1 with | none => ∃ (next : Index✝), outcome.2.2 = encode✝ next ∧ normal✝ heap next finish | some value => ∃ (next : Index✝), outcome.2.2 = encode✝ next ∧ completed✝ heap value next finish | Control.returned a => False | Control.fault a => False }, PUnit.unit)⦄

                                      Prove the actual body contract using mathematical locals and the real completion value. Continuing and completed outcomes retain the actual heap.

                                      @[reducible, inline]

                                      Restore the same full locals, retaining the ancestor slots from entry.

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope2.completion_contract_of_body {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                        (body_completion_contract pre✝ (fun (start : Visible) (heap : Heap) (output : Visible) (finish : Heap) => VisibleRooted heap output ∧ normal✝ start heap output (finish.take heap.objects.size)) fun (start : Visible) (heap : Heap) (value : Unit) (output : Visible) (finish : Heap) => VisibleRooted heap output ∧ ValueRooted heap value ∧ returned✝ start heap value output (finish.take heap.objects.size)) → completion_contract pre✝ normal✝ returned✝

                                        Close the actual scratch boundary from visible body relations. Roots are checked before reclamation; the author's postcondition uses the heap after that same reclamation. Private slots are restored by the body frame.

                                        The source-visible values retain only roots from this heap.

                                        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
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.guard_spec (out✝ : Buffer CellTy.nat) (count n value : ℕ) {pre✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop} {normal✝ : ℕ → Option (Buffer CellTy.nat) → Heap → ℕ → Option (Buffer CellTy.nat) → Heap → Prop} {returned✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Bool → ℕ → Option (Buffer CellTy.nat) → Heap → Prop} :
                                            guard_contract out✝ count n value pre✝ normal✝ returned✝ → ∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (post : Std.Do.PostCond (Control Ty.bool × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ remaining result_join heap ∧ (∀ (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap), normal✝ remaining result_join heap after_remaining after_result_join finish → (post.1 (Control.normal, after_remaining, out✝, after_result_join, count, n, value, ()) finish).down) ∧ ∀ (value_1 : Bool) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap), returned✝ remaining result_join heap value_1 after_remaining after_result_join finish → (post.1 (Control.returned value_1, after_remaining, out✝, after_result_join, count, n, value, ()) finish).down }⦄ guard remaining out✝ result_join count n value ⦃post⦄

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

                                            theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.body_completion_spec_at {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Buffer CellTy.nat → Visible → Heap → Prop} :
                                            body_completion_contract pre✝ normal✝ returned✝ → ∀ (start : Visible) (post : Std.Do.PostCond (Control (Ty.buffer CellTy.nat) × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ start heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ start heap output finish → (post.1 (Control.normal, reconstruct (entry start) none output) finish).down) ∧ ∀ (value : Buffer CellTy.nat) (output : Visible) (finish : Heap), returned✝ start heap value output finish → (post.1 (Control.normal, reconstruct (entry start) (some value) output) finish).down }⦄ body (entry start).1 (entry start).2.1 (entry start).2.2.1 (entry start).2.2.2.1 (entry start).2.2.2.2.1 (entry start).2.2.2.2.2.1 ⦃post⦄

                                            Apply the visible completion contract, reconstructing the full output from this same actual execution rather than guessing hidden coordinates.

                                            theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope2.pending_reconstruct (initial✝ : Locals) (pending✝ : Option Unit) (visible✝ : Visible) :
                                            pending (reconstruct initial✝ pending✝ visible✝) = pending✝

                                            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.Scope.Implementation.Source.make_loop1.guard_model_contract_iff {Index✝ : Type} (encode✝ : Index✝ → Model) (index✝ : Index✝) (pre✝ : Heap → Prop) (post✝ : Heap → Bool → Index✝ → Heap → Prop) :
                                              guard_model_contract encode✝ index✝ pre✝ post✝ ↔ ∀ (heap : Heap), pre✝ heap → ⦃fun (current : Heap) => { down := current = heap }⦄ guard_model_action (encode✝ index✝) ⦃(fun (outcome : Control Ty.bool × Model) (finish : Heap) => { down := match outcome.1 with | Control.normal => False | Control.returned again => ∃ (next : Index✝), outcome.2 = encode✝ next ∧ post✝ heap again next finish | Control.fault a => False }, PUnit.unit)⦄

                                              Prove the actual guard contract directly from ordinary mathematical locals. The input-coordinate and output-representation transport is shared.

                                              theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.variant_contract (out✝ : Buffer CellTy.nat) (count n value : ℕ) (invariant✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (variant✝ : ℕ → Option (Buffer CellTy.nat) → Heap → ℕ) (ready✝ : ℕ → Option (Buffer CellTy.nat) → Heap → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (normal✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (returned✝ : Buffer CellTy.nat → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) :
                                              (guard_contract out✝ count n value invariant✝ (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => False) fun (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap) (again : Bool) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => if again = true then ready✝ remaining result_join heap after_remaining after_result_join finish else normal✝ after_remaining after_result_join finish) → (∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap), invariant✝ remaining result_join heap → body_contract out✝ count n value (ready✝ remaining result_join heap) (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => invariant✝ after_remaining after_result_join finish ∧ variant✝ after_remaining after_result_join finish < variant✝ remaining result_join heap) fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (value : Buffer CellTy.nat) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => returned✝ value after_remaining after_result_join finish) → contract out✝ count n value invariant✝ (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => normal✝ after_remaining after_result_join finish) fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (value : Buffer CellTy.nat) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => returned✝ value after_remaining after_result_join 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.

                                              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.Scope.Implementation.Source.make_loop1.model_completion_effects_contract {Index✝ : Type} (encode✝ : Index✝ → Model) (invariant✝ : Index✝ → Heap → Prop) (prepared✝ : Index✝ → Heap → Index✝ → Heap → Prop) (step✝ : Index✝ → Index✝ → Prop) {relation✝ : Index✝ → Index✝ → Prop} :
                                                WellFounded relation✝ → ∀ (normal : Index✝ → Heap → Prop) (completed : Buffer CellTy.nat → Index✝ → Heap → Prop), (∀ (index : Index✝) (heap : Heap), invariant✝ index heap → guard_model_contract encode✝ index (fun (current : Heap) => current = heap) fun (initial : Heap) (again : Bool) (next : Index✝) (finish : Heap) => if again = true then prepared✝ index initial next finish else normal next finish) → (∀ (index : Index✝) (heap : Heap), invariant✝ index heap → ∀ (tested : Index✝) (after : Heap), prepared✝ index heap tested after → body_model_contract encode✝ tested (fun (current : Heap) => current = after) (fun (x : Heap) (next : Index✝) (finish : Heap) => step✝ index next ∧ invariant✝ next finish) fun (x : Heap) => completed) → (∀ (index : Index✝) (heap : Heap), invariant✝ index heap → ∀ (tested : Index✝) (after : Heap), prepared✝ index heap tested after → ∀ (next : Index✝), step✝ index next → relation✝ next index) → ∀ (index : Index✝), completion_contract (fun (locals : Visible) (heap : Heap) => visibleModelRel (encode✝ index) locals heap ∧ invariant✝ index heap) (fun (x : Visible) (x_1 : Heap) (output : Visible) (finish : Heap) => ∃ (next : Index✝), visibleModelRel (encode✝ next) output finish ∧ normal next finish) fun (x : Visible) (x_1 : Heap) (result : Buffer CellTy.nat) (output : Visible) (finish : Heap) => ∃ (next : Index✝), visibleModelRel (encode✝ next) output finish ∧ completed result next finish

                                                Compose mathematical contracts at the actual heaps before and after an effectful guard. Continuing rounds decrease from the state before the guard; a false guard or a completed body establishes the final condition.

                                                @[simp]
                                                theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.visibleModelRel_mkModel_iff (remaining✝ : ℕ) (out✝ : Value (Ty.buffer CellTy.nat)) (value✝ n✝ count✝ : ℕ) (locals✝ : Visible) (heap✝ : Heap) :
                                                visibleModelRel (mkModel remaining✝ out✝ value✝ n✝ count✝) locals✝ heap✝ ↔ locals✝ = (remaining✝, out✝, count✝, n✝, value✝, ())

                                                A complete identity local view is exactly the original visible coordinates. Raw handles contribute equality only; contents remain heap-dependent contracts.

                                                Source lexical immutability preserves these captures on every actual exit.

                                                theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.wellFounded_contract (out✝ : Buffer CellTy.nat) (count n value : ℕ) (invariant✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (relation✝ : Mutable × Heap → Mutable × Heap → Prop) :
                                                WellFounded relation✝ → ∀ (ready : ℕ → Option (Buffer CellTy.nat) → Heap → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (normal : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (returned : Buffer CellTy.nat → ℕ → Option (Buffer CellTy.nat) → Heap → Prop), (guard_contract out✝ count n value invariant✝ (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => False) fun (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap) (again : Bool) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => if again = true then ready remaining result_join heap after_remaining after_result_join finish else normal after_remaining after_result_join finish) → (∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap), invariant✝ remaining result_join heap → body_contract out✝ count n value (ready remaining result_join heap) (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => invariant✝ after_remaining after_result_join finish ∧ relation✝ ((after_remaining, after_result_join, ()), finish) ((remaining, result_join, ()), heap)) fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (value : Buffer CellTy.nat) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => returned value after_remaining after_result_join finish) → contract out✝ count n value invariant✝ (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => normal after_remaining after_result_join finish) fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (value : Buffer CellTy.nat) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => returned value after_remaining after_result_join 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.

                                                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]

                                                  Project source-visible coordinates from the unchanged full locals.

                                                  Equations
                                                  Instances For
                                                    theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.count_frame_contract (out✝ : Buffer CellTy.nat) (count n value : ℕ) (count✝ : ℕ → Option (Buffer CellTy.nat) → ℕ) (limit✝ : ℕ) (invariant✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (frame✝ : Heap → Heap → Prop) :
                                                    (∀ {a b c : Heap}, frame✝ a b → frame✝ b c → frame✝ a c) → ∀ (normal : Heap → Prop), (guard_contract out✝ count n value invariant✝ (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (x_3 : Heap) => False) fun (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap) (again : Bool) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => count✝ after_remaining after_result_join = count✝ remaining result_join ∧ finish = heap ∧ invariant✝ after_remaining after_result_join finish ∧ (again = true ↔ count✝ after_remaining after_result_join < limit✝)) → (body_contract out✝ count n value (fun (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap) => invariant✝ remaining result_join heap ∧ count✝ remaining result_join < limit✝) (fun (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => count✝ after_remaining after_result_join = count✝ remaining result_join + 1 ∧ invariant✝ after_remaining after_result_join finish ∧ frame✝ heap finish) fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (x_3 : Buffer CellTy.nat) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (x_4 : Heap) => False) → (∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap), invariant✝ remaining result_join heap → limit✝ ≤ count✝ remaining result_join → normal heap) → ∀ (initial : Heap), contract out✝ count n value (fun (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap) => invariant✝ remaining result_join heap ∧ frame✝ initial heap) (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => normal finish ∧ frame✝ initial finish) fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (x_3 : Buffer CellTy.nat) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (x_4 : 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.

                                                    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

                                                        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.Scope.Implementation.Source.work_scope1.continue_eq (entry✝ : Env [Ty.unit.option, Ty.unit.option, Ty.buffer CellTy.nat, Ty.nat, Ty.nat]) (next✝ : Env [Ty.unit.option, Ty.unit.option, Ty.buffer CellTy.nat, Ty.nat, Ty.nat] → ExceptT Fault (StateT Heap Part) Unit) :
                                                          Code.evalWith program entry✝ next✝ = do let __discr ← MonadLift.monadLift (work_scope1 (View entry✝).1 (View entry✝).2.1 (View entry✝).2.2.1 (View entry✝).2.2.2.1 (View entry✝).2.2.2.2.1) match __discr with | (control, locals) => match control with | Control.normal => next✝ (Env.cons locals.1 (Env.cons locals.2.1 (Env.cons (entry✝.get Var.here.there.there) (Env.cons (entry✝.get Var.here.there.there.there) (Env.cons (entry✝.get Var.here.there.there.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.

                                                          theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.completion_contract_of_body_spec {bodyPre✝ : Visible → Heap → Prop} {bodyNormal✝ : Visible → Heap → Visible → Heap → Prop} {bodyReturned✝ : Visible → Heap → Unit → Visible → Heap → Prop} {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                                          body_completion_contract bodyPre✝ bodyNormal✝ bodyReturned✝ → (∀ (start : Visible) (heap : Heap), pre✝ start heap → bodyPre✝ start heap) → (∀ (start : Visible) (heap : Heap) (output : Visible) (finish : Heap), pre✝ start heap → bodyNormal✝ start heap output finish → VisibleRooted heap output ∧ normal✝ start heap output (finish.take heap.objects.size)) → (∀ (start : Visible) (heap : Heap) (value : Unit) (output : Visible) (finish : Heap), pre✝ start heap → bodyReturned✝ start heap value output finish → VisibleRooted heap output ∧ ValueRooted heap value ∧ returned✝ start heap value output (finish.take heap.objects.size)) → completion_contract pre✝ normal✝ returned✝

                                                          Reuse an existing visible body contract across this actual scratch boundary. The consequences prove non-escape and the desired observations after trimming the current final heap, without rebuilding the body proof.

                                                          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.Scope.Implementation.Source.make_contract_iff_triple (pre✝ : ℕ → ℕ → ℕ → Heap → Prop) (post✝ : ℕ → ℕ → ℕ → Heap → Buffer CellTy.nat → Heap → Prop) :
                                                            make_contract pre✝ post✝ ↔ ∀ (count n value : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ count n value initialHeap }⦄ make count n value ⦃(fun (value_1 : Buffer CellTy.nat) (finalHeap : Heap) => { down := post✝ count n value initialHeap value_1 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.Scope.Implementation.Source.work_scope1.pending_reconstruct (initial✝ : Locals) (pending✝ : Option Unit) (visible✝ : Visible) :
                                                            pending (reconstruct initial✝ pending✝ visible✝) = pending✝
                                                            theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.observe (locals✝ : Locals) :
                                                            Stmt.observe View Code program locals✝ = work_scope1 locals✝.1 locals✝.2.1 locals✝.2.2.1 locals✝.2.2.2.1 locals✝.2.2.2.2.1

                                                            Coordinate conversion for composing this named observation.

                                                            theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.spec (out✝ : Buffer CellTy.nat) (count n value : ℕ) {pre✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop} {normal✝ : ℕ → Option (Buffer CellTy.nat) → Heap → ℕ → Option (Buffer CellTy.nat) → Heap → Prop} {returned✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Buffer CellTy.nat → ℕ → Option (Buffer CellTy.nat) → Heap → Prop} :
                                                            contract out✝ count n value pre✝ normal✝ returned✝ → ∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (post : Std.Do.PostCond (Control (Ty.buffer CellTy.nat) × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ remaining result_join heap ∧ (∀ (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap), normal✝ remaining result_join heap after_remaining after_result_join finish → (post.1 (Control.normal, after_remaining, out✝, after_result_join, count, n, value, ()) finish).down) ∧ ∀ (value_1 : Buffer CellTy.nat) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap), returned✝ remaining result_join heap value_1 after_remaining after_result_join finish → (post.1 (Control.returned value_1, after_remaining, out✝, after_result_join, count, n, value, ()) finish).down }⦄ make_loop1 remaining out✝ result_join count n value ⦃post⦄

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

                                                            @[reducible, inline]

                                                            Author relations use visible locals and actual endpoint heaps. The existing action still returns its full locals and actual source control.

                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For
                                                              theorem Complexity.Language.Examples.Scope.Implementation.Source.make_eq (count n value : ℕ) :
                                                              make count n value = have result_join := none; do let out ← Buffer.allocM 1 0 have remaining : ℕ := count let __discr ← MonadLift.monadLift (make_loop1 remaining out result_join count n value) match __discr with | (loopControl, loopLocals) => have remaining := loopLocals.1; have result_join := loopLocals.2.2.1; have __do_jp := fun (result_join : Option (Buffer CellTy.nat)) (y : Unit) => have __do_jp := fun (result_join : Option (Buffer CellTy.nat)) (y : Unit) => have __do_jp := fun (y : Unit) => throw Fault.missingReturn; match result_join with | none => do let y ← pure () __do_jp y | some payload => pure payload; match result_join with | none => have result_join := some out; do let y ← pure () __do_jp result_join y | some val => do let y ← pure () __do_jp result_join y; match loopControl with | Control.normal => do let y ← pure () __do_jp result_join y | Control.returned returned => pure returned | Control.fault error => do let y ← throw error __do_jp result_join y

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

                                                              Continuing completion coordinates recover the same full entry.

                                                              theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.guard_observe (locals✝ : Locals) :
                                                              Stmt.observe View Guard program locals✝ = guard locals✝.1 locals✝.2.1 locals✝.2.2.1 locals✝.2.2.2.1 locals✝.2.2.2.2.1 locals✝.2.2.2.2.2.1

                                                              Coordinate conversion for composing this named observation.

                                                              @[reducible, inline]

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

                                                              Equations
                                                              Instances For
                                                                @[reducible, inline]

                                                                Author relations use visible locals and actual endpoint heaps. The existing action still returns its full locals and actual source control.

                                                                Equations
                                                                • One or more equations did not get rendered due to their size.
                                                                Instances For
                                                                  theorem Complexity.Language.Examples.Scope.Implementation.Source.work_eq (out : Buffer CellTy.nat) (n value : ℕ) :
                                                                  work out n value = have result_join := none; have scratchPending := none; do let __discr ← MonadLift.monadLift (work_scope1 scratchPending result_join out n value) match __discr with | (loopControl, loopLocals) => have scratchPending := loopLocals.1; have result_join := loopLocals.2.1; have __do_jp := fun (result_join : Option Unit) (y : Unit) => have __do_jp := fun (result_join : Option Unit) (y : Unit) => have __do_jp := fun (y : Unit) => throw Fault.missingReturn; match result_join with | none => do let y ← pure () __do_jp y | some payload => pure payload; match scratchPending with | none => do let y ← pure () __do_jp result_join y | some localResult => have result_join := some localResult; do let y ← pure PUnit.unit __do_jp result_join y; match loopControl with | Control.normal => do let y ← pure () __do_jp result_join y | Control.returned returned => pure returned | Control.fault error => do let y ← throw error __do_jp result_join y

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

                                                                  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.Scope.Implementation.Source.make_loop1.variant_spec (out✝ : Buffer CellTy.nat) (count n value : ℕ) (invariant✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (variant✝ : ℕ → Option (Buffer CellTy.nat) → Heap → ℕ) (normal✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (returned✝ : Buffer CellTy.nat → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) :
                                                                    (∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (startHeap : Heap), invariant✝ remaining result_join startHeap → ⦃fun (currentHeap : Heap) => { down := currentHeap = startHeap }⦄ guard remaining out✝ result_join count n value ⦃(fun (guardOutcome : Control Ty.bool × Locals) (afterGuardHeap : Heap) => { down := have afterGuard_remaining := guardOutcome.2.1; have afterGuard_result_join := guardOutcome.2.2.2.1; match guardOutcome.1 with | Control.returned again => if again = true then ⦃fun (currentHeap : Heap) => { down := currentHeap = afterGuardHeap }⦄ body afterGuard_remaining out✝ afterGuard_result_join count n value ⦃(fun (bodyOutcome : Control (Ty.buffer CellTy.nat) × Locals) (afterBodyHeap : Heap) => { down := have afterBody_remaining := bodyOutcome.2.1; have afterBody_result_join := bodyOutcome.2.2.2.1; match bodyOutcome.1 with | Control.normal => invariant✝ afterBody_remaining afterBody_result_join afterBodyHeap ∧ variant✝ afterBody_remaining afterBody_result_join afterBodyHeap < variant✝ remaining result_join startHeap | Control.returned value => returned✝ value afterBody_remaining afterBody_result_join afterBodyHeap | Control.fault a => False }, PUnit.unit)⦄ else normal✝ afterGuard_remaining afterGuard_result_join afterGuardHeap | Control.normal => False | Control.fault a => False }, PUnit.unit)⦄) → ∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)), ⦃fun (heap : Heap) => { down := invariant✝ remaining result_join heap }⦄ make_loop1 remaining out✝ result_join count n value ⦃(fun (outcome : Control (Ty.buffer CellTy.nat) × Locals) (heap : Heap) => { down := have final_remaining := outcome.2.1; have final_result_join := outcome.2.2.2.1; match outcome.1 with | Control.normal => normal✝ final_remaining final_result_join heap | Control.returned value => returned✝ value final_remaining final_result_join 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.

                                                                    Actual finite execution fixes every ancestor completion slot. This restores full locals; it does not discard roots or assume successful cleanup.

                                                                    theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.completion_spec {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                                                    completion_contract pre✝ normal✝ returned✝ → ∀ (out : Buffer CellTy.nat) (n value : ℕ) (post : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ (out, n, value, ()) heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ (out, n, value, ()) heap output finish → (post.1 (Control.normal, reconstruct (entry (out, n, value, ())) none output) finish).down) ∧ ∀ (value_1 : Unit) (output : Visible) (finish : Heap), returned✝ (out, n, value, ()) heap value_1 output finish → (post.1 (Control.normal, reconstruct (entry (out, n, value, ())) (some value_1) output) finish).down }⦄ work_scope1 none none out n value ⦃post⦄

                                                                    Apply the same completion contract at independently named source inputs. The actual action has canonical arguments, so native specification lookup does not need to infer a tuple from its projections.

                                                                    theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.scopeSafe_reconstruct (rootHeap✝ finalHeap✝ : Heap) (start✝ visible✝ : Visible) (pending✝ : Option Unit) :
                                                                    ScopeSafe rootHeap✝ { locals := View.symm (reconstruct (entry start✝) pending✝ visible✝), heap := finalHeap✝ } Control.normal ↔ VisibleRooted rootHeap✝ visible✝ ∧ ValueRooted rootHeap✝ pending✝

                                                                    The original full scratch safety condition after actual frame restoration. Ancestor slots are empty at entry; the current completion remains a checked root.

                                                                    Observe mathematical locals at a visible completion boundary's actual heap.

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

                                                                      Enter this source boundary with empty private completion slots.

                                                                      Equations
                                                                      Instances For

                                                                        The completion observation is the actual source slot read.

                                                                        @[reducible, inline]

                                                                        The current local result; actual source control is unchanged.

                                                                        Equations
                                                                        Instances For
                                                                          theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.body_contract_iff (out✝ : Buffer CellTy.nat) (count n value : ℕ) (pre✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (normal✝ : ℕ → Option (Buffer CellTy.nat) → Heap → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (returned✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Buffer CellTy.nat → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) :
                                                                          body_contract out✝ count n value pre✝ normal✝ returned✝ ↔ ∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap), pre✝ remaining result_join heap → ⦃fun (current : Heap) => { down := current = heap }⦄ body remaining out✝ result_join count n value ⦃(fun (outcome : Control (Ty.buffer CellTy.nat) × Locals) (finish : Heap) => { down := match outcome.1 with | Control.normal => have after := outcome.2; normal✝ remaining result_join heap after.1 after.2.2.1 finish | Control.returned value => have after := outcome.2; returned✝ remaining result_join heap value after.1 after.2.2.1 finish | Control.fault a => False }, PUnit.unit)⦄

                                                                          Introduce named mutable variables directly, without unpacking internal coordinates.

                                                                          theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.model_rel_count {whileState✝ : Model} {locals✝ : Locals} {heap✝ : Heap} :
                                                                          modelRel whileState✝ locals✝ heap✝ → (Representation.ofEmbedding (Function.Embedding.refl ℕ)).Rel whileState✝.2.2.2.2 locals✝.2.2.2.1 heap✝

                                                                          Extract a field's observation without source-coordinate transport.

                                                                          Actual finite execution fixes every ancestor completion slot. This restores full locals; it does not discard roots or assume successful cleanup.

                                                                          Local completion does not produce an enclosing source return.

                                                                          theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.completion_spec_at {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                                                          completion_contract pre✝ normal✝ returned✝ → ∀ (start : Visible) (post : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ start heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ start heap output finish → (post.1 (Control.normal, reconstruct (entry start) none output) finish).down) ∧ ∀ (value : Unit) (output : Visible) (finish : Heap), returned✝ start heap value output finish → (post.1 (Control.normal, reconstruct (entry start) (some value) output) finish).down }⦄ work_scope1 (entry start).1 (entry start).2.1 (entry start).2.2.1 (entry start).2.2.2.1 (entry start).2.2.2.2.1 ⦃post⦄

                                                                          Apply the visible completion contract, reconstructing the full output from this same actual execution rather than guessing hidden coordinates.

                                                                          theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.body_completion_spec {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                                                          body_completion_contract pre✝ normal✝ returned✝ → ∀ (out : Buffer CellTy.nat) (n value : ℕ) (post : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ (out, n, value, ()) heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ (out, n, value, ()) heap output finish → (post.1 (Control.normal, reconstruct (entry (out, n, value, ())) none output) finish).down) ∧ ∀ (value_1 : Unit) (output : Visible) (finish : Heap), returned✝ (out, n, value, ()) heap value_1 output finish → (post.1 (Control.normal, reconstruct (entry (out, n, value, ())) (some value_1) output) finish).down }⦄ body none none out n value ⦃post⦄

                                                                          Apply the same completion contract at independently named source inputs. The actual action has canonical arguments, so native specification lookup does not need to infer a tuple from its projections.

                                                                          theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope2.completion_contract_of_body_spec {bodyPre✝ : Visible → Heap → Prop} {bodyNormal✝ : Visible → Heap → Visible → Heap → Prop} {bodyReturned✝ : Visible → Heap → Unit → Visible → Heap → Prop} {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                                                          body_completion_contract bodyPre✝ bodyNormal✝ bodyReturned✝ → (∀ (start : Visible) (heap : Heap), pre✝ start heap → bodyPre✝ start heap) → (∀ (start : Visible) (heap : Heap) (output : Visible) (finish : Heap), pre✝ start heap → bodyNormal✝ start heap output finish → VisibleRooted heap output ∧ normal✝ start heap output (finish.take heap.objects.size)) → (∀ (start : Visible) (heap : Heap) (value : Unit) (output : Visible) (finish : Heap), pre✝ start heap → bodyReturned✝ start heap value output finish → VisibleRooted heap output ∧ ValueRooted heap value ∧ returned✝ start heap value output (finish.take heap.objects.size)) → completion_contract pre✝ normal✝ returned✝

                                                                          Reuse an existing visible body contract across this actual scratch boundary. The consequences prove non-escape and the desired observations after trimming the current final heap, without rebuilding the body proof.

                                                                          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.Scope.Implementation.Source.make_loop1.wellFounded_spec (out✝ : Buffer CellTy.nat) (count n value : ℕ) (invariant✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (relation✝ : Mutable × Heap → Mutable × Heap → Prop) :
                                                                            WellFounded relation✝ → ∀ (normal : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (returned : Buffer CellTy.nat → ℕ → Option (Buffer CellTy.nat) → Heap → Prop), (∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (startHeap : Heap), invariant✝ remaining result_join startHeap → ⦃fun (currentHeap : Heap) => { down := currentHeap = startHeap }⦄ guard remaining out✝ result_join count n value ⦃(fun (guardOutcome : Control Ty.bool × Locals) (afterGuardHeap : Heap) => { down := have afterGuard_remaining := guardOutcome.2.1; have afterGuard_result_join := guardOutcome.2.2.2.1; match guardOutcome.1 with | Control.returned again => if again = true then ⦃fun (currentHeap : Heap) => { down := currentHeap = afterGuardHeap }⦄ body afterGuard_remaining out✝ afterGuard_result_join count n value ⦃(fun (bodyOutcome : Control (Ty.buffer CellTy.nat) × Locals) (afterBodyHeap : Heap) => { down := have afterBody_remaining := bodyOutcome.2.1; have afterBody_result_join := bodyOutcome.2.2.2.1; match bodyOutcome.1 with | Control.normal => invariant✝ afterBody_remaining afterBody_result_join afterBodyHeap ∧ relation✝ ((afterBody_remaining, afterBody_result_join, ()), afterBodyHeap) ((remaining, result_join, ()), startHeap) | Control.returned value => returned value afterBody_remaining afterBody_result_join afterBodyHeap | Control.fault a => False }, PUnit.unit)⦄ else normal afterGuard_remaining afterGuard_result_join afterGuardHeap | Control.normal => False | Control.fault a => False }, PUnit.unit)⦄) → ∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)), ⦃fun (heap : Heap) => { down := invariant✝ remaining result_join heap }⦄ make_loop1 remaining out✝ result_join count n value ⦃(fun (outcome : Control (Ty.buffer CellTy.nat) × Locals) (heap : Heap) => { down := have final_remaining := outcome.2.1; have final_result_join := outcome.2.2.2.1; match outcome.1 with | Control.normal => normal final_remaining final_result_join heap | Control.returned value => returned value final_remaining final_result_join 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.Scope.Implementation.Source.make_loop1.model_rel_out {whileState✝ : Model} {locals✝ : Locals} {heap✝ : Heap} :
                                                                            modelRel whileState✝ locals✝ heap✝ → (Representation.ofEmbedding (Function.Embedding.refl (Value (Ty.buffer CellTy.nat)))).Rel whileState✝.2.1 locals✝.2.1 heap✝

                                                                            Extract a field's observation without source-coordinate transport.

                                                                            theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope2.observe (locals✝ : Locals) :
                                                                            Stmt.observe View Code program locals✝ = work_scope2 locals✝.1 locals✝.2.1 locals✝.2.2.1 locals✝.2.2.2.1 locals✝.2.2.2.2.1 locals✝.2.2.2.2.2.1 locals✝.2.2.2.2.2.2.1

                                                                            Coordinate conversion for composing this named observation.

                                                                            @[reducible, inline]

                                                                            Author relations use visible locals and actual endpoint heaps. The existing action still returns its full locals and actual source control.

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

                                                                              Actual finite execution fixes every ancestor completion slot. This restores full locals; it does not discard roots or assume successful cleanup.

                                                                              Actual finite execution fixes every ancestor completion slot. This restores full locals; it does not discard roots or assume successful cleanup.

                                                                              theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.completion_spec {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Buffer CellTy.nat → Visible → Heap → Prop} :
                                                                              completion_contract pre✝ normal✝ returned✝ → ∀ (remaining : ℕ) (out : Buffer CellTy.nat) (count n value : ℕ) (post : Std.Do.PostCond (Control (Ty.buffer CellTy.nat) × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ (remaining, out, count, n, value, ()) heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ (remaining, out, count, n, value, ()) heap output finish → (post.1 (Control.normal, reconstruct (entry (remaining, out, count, n, value, ())) none output) finish).down) ∧ ∀ (value_1 : Buffer CellTy.nat) (output : Visible) (finish : Heap), returned✝ (remaining, out, count, n, value, ()) heap value_1 output finish → (post.1 (Control.normal, reconstruct (entry (remaining, out, count, n, value, ())) (some value_1) output) finish).down }⦄ make_loop1 remaining out none count n value ⦃post⦄

                                                                              Apply the same completion contract at independently named source inputs. The actual action has canonical arguments, so native specification lookup does not need to infer a tuple from its projections.

                                                                              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

                                                                                Local completion does not produce an enclosing source return.

                                                                                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.Scope.Implementation.Source.make_loop1.completion_spec_at {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Buffer CellTy.nat → Visible → Heap → Prop} :
                                                                                  completion_contract pre✝ normal✝ returned✝ → ∀ (start : Visible) (post : Std.Do.PostCond (Control (Ty.buffer CellTy.nat) × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ start heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ start heap output finish → (post.1 (Control.normal, reconstruct (entry start) none output) finish).down) ∧ ∀ (value : Buffer CellTy.nat) (output : Visible) (finish : Heap), returned✝ start heap value output finish → (post.1 (Control.normal, reconstruct (entry start) (some value) output) finish).down }⦄ make_loop1 (entry start).1 (entry start).2.1 (entry start).2.2.1 (entry start).2.2.2.1 (entry start).2.2.2.2.1 (entry start).2.2.2.2.2.1 ⦃post⦄

                                                                                  Apply the visible completion contract, reconstructing the full output from this same actual execution rather than guessing hidden coordinates.

                                                                                  theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.body_observe (locals✝ : Locals) :
                                                                                  Stmt.observe View Body program locals✝ = body locals✝.1 locals✝.2.1 locals✝.2.2.1 locals✝.2.2.2.1 locals✝.2.2.2.2.1 locals✝.2.2.2.2.2.1

                                                                                  Coordinate conversion for composing this named observation.

                                                                                  Continuing completion coordinates recover the same full entry.

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

                                                                                  theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope2.scopeSafe_reconstruct (rootHeap✝ finalHeap✝ : Heap) (start✝ visible✝ : Visible) (pending✝ : Option Unit) :
                                                                                  ScopeSafe rootHeap✝ { locals := View.symm (reconstruct (entry start✝) pending✝ visible✝), heap := finalHeap✝ } Control.normal ↔ VisibleRooted rootHeap✝ visible✝ ∧ ValueRooted rootHeap✝ pending✝

                                                                                  The original full scratch safety condition after actual frame restoration. Ancestor slots are empty at entry; the current completion remains a checked root.

                                                                                  @[reducible, inline]

                                                                                  Author relations use visible locals and actual endpoint heaps. The existing action still returns its full locals and actual source control.

                                                                                  Equations
                                                                                  • One or more equations did not get rendered due to their size.
                                                                                  Instances For
                                                                                    @[reducible, inline]
                                                                                    abbrev Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.guard_model_contract {Index✝ : Type} :
                                                                                    (Index✝ → Model) → Index✝ → (Heap → Prop) → (Heap → Bool → Index✝ → Heap → Prop) → Prop

                                                                                    A supplied actual guard contract, expressed through mathematical indices. Each output observation is made at the real final heap.

                                                                                    Equations
                                                                                    • One or more equations did not get rendered due to their size.
                                                                                    Instances For
                                                                                      theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.completion_contract_of_body {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                                                                      (body_completion_contract pre✝ (fun (start : Visible) (heap : Heap) (output : Visible) (finish : Heap) => VisibleRooted heap output ∧ normal✝ start heap output (finish.take heap.objects.size)) fun (start : Visible) (heap : Heap) (value : Unit) (output : Visible) (finish : Heap) => VisibleRooted heap output ∧ ValueRooted heap value ∧ returned✝ start heap value output (finish.take heap.objects.size)) → completion_contract pre✝ normal✝ returned✝

                                                                                      Close the actual scratch boundary from visible body relations. Roots are checked before reclamation; the author's postcondition uses the heap after that same reclamation. Private slots are restored by the body frame.

                                                                                      @[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.Examples.Scope.Implementation.Source.make_loop1.rel_contract (out✝ : Buffer CellTy.nat) (count n value : ℕ) {Model✝ : Type} (stateRel✝ : Model✝ → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (invariant✝ : Model✝ → Prop) {relation✝ : Model✝ → Model✝ → Prop} :
                                                                                        WellFounded relation✝ → ∀ (ready : Model✝ → ℕ → Option (Buffer CellTy.nat) → Heap → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (normal : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (returned : Buffer CellTy.nat → ℕ → Option (Buffer CellTy.nat) → Heap → Prop), (∀ (model : Model✝), invariant✝ model → guard_contract out✝ count n value (stateRel✝ model) (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => False) fun (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap) (again : Bool) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => if again = true then ready model remaining result_join heap after_remaining after_result_join finish else normal after_remaining after_result_join finish) → (∀ (model : Model✝) (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap), invariant✝ model → stateRel✝ model remaining result_join heap → body_contract out✝ count n value (ready model remaining result_join heap) (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => ∃ (next : Model✝), invariant✝ next ∧ stateRel✝ next after_remaining after_result_join finish ∧ relation✝ next model) fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (value : Buffer CellTy.nat) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => returned value after_remaining after_result_join finish) → ∀ (model : Model✝), invariant✝ model → contract out✝ count n value (stateRel✝ model) (fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => normal after_remaining after_result_join finish) fun (x : ℕ) (x_1 : Option (Buffer CellTy.nat)) (x_2 : Heap) (value : Buffer CellTy.nat) (after_remaining : ℕ) (after_result_join : Option (Buffer CellTy.nat)) (finish : Heap) => returned value after_remaining after_result_join finish

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

                                                                                        Observe the same guard in mathematical coordinates. Control and the actual final heap are untouched; this is not another source implementation.

                                                                                        Equations
                                                                                        • One or more equations did not get rendered due to their size.
                                                                                        Instances For
                                                                                          theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.regroup_apply (values✝ : Locals) :
                                                                                          Regroup values✝ = ((values✝.1, values✝.2.2.1, ()), values✝.2.1, values✝.2.2.2.1, values✝.2.2.2.2.1, values✝.2.2.2.2.2.1, ())
                                                                                          theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.model_completion_contract {Index✝ : Type} (encode✝ : Index✝ → Model) (test✝ : Index✝ → Bool) (invariant✝ : Index✝ → Heap → Prop) (step✝ : Index✝ → Index✝ → Prop) {relation✝ : Index✝ → Index✝ → Prop} :
                                                                                          WellFounded relation✝ → ∀ (normal : Index✝ → Heap → Prop) (completed : Buffer CellTy.nat → Index✝ → Heap → Prop), (∀ (index : Index✝) (heap : Heap), invariant✝ index heap → guard_model_contract encode✝ index (fun (current : Heap) => current = heap) fun (heap : Heap) (again : Bool) (next : Index✝) (finish : Heap) => again = test✝ index ∧ next = index ∧ finish = heap) → (∀ (index : Index✝) (heap : Heap), invariant✝ index heap → test✝ index = true → body_model_contract encode✝ index (fun (current : Heap) => current = heap) (fun (x : Heap) (next : Index✝) (finish : Heap) => step✝ index next ∧ invariant✝ next finish) fun (x : Heap) => completed) → (∀ (index next : Index✝) (heap : Heap), invariant✝ index heap → test✝ index = true → step✝ index next → relation✝ next index) → (∀ (index : Index✝) (heap : Heap), invariant✝ index heap → test✝ index = false → normal index heap) → ∀ (index : Index✝), completion_contract (fun (locals : Visible) (heap : Heap) => visibleModelRel (encode✝ index) locals heap ∧ invariant✝ index heap) (fun (x : Visible) (x_1 : Heap) (output : Visible) (finish : Heap) => ∃ (next : Index✝), visibleModelRel (encode✝ next) output finish ∧ normal next finish) fun (x : Visible) (x_1 : Heap) (result : Buffer CellTy.nat) (output : Visible) (finish : Heap) => ∃ (next : Index✝), visibleModelRel (encode✝ next) output finish ∧ completed result next finish

                                                                                          Compose the actual loop from mathematical local contracts. The supplied guard preserves the index and heap; only a continuing body needs descent. The step relation describes mathematical progress, not a pure implementation.

                                                                                          theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.model_rel_remaining {whileState✝ : Model} {locals✝ : Locals} {heap✝ : Heap} :
                                                                                          modelRel whileState✝ locals✝ heap✝ → (Representation.ofEmbedding (Function.Embedding.refl ℕ)).Rel whileState✝.1 locals✝.1 heap✝

                                                                                          Extract a field's observation without source-coordinate transport.

                                                                                          theorem Complexity.Language.Examples.Scope.Implementation.Source.work_contract_iff_triple (pre✝ : Buffer CellTy.nat → ℕ → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → ℕ → ℕ → Heap → Unit → Heap → Prop) :
                                                                                          work_contract pre✝ post✝ ↔ ∀ (out : Buffer CellTy.nat) (n value : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ out n value initialHeap }⦄ work out n value ⦃(fun (value_1 : Unit) (finalHeap : Heap) => { down := post✝ out n value initialHeap value_1 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.Scope.Implementation.Source.work_scope2.completion_spec {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                                                                          completion_contract pre✝ normal✝ returned✝ → ∀ (temp out : Buffer CellTy.nat) (n value : ℕ) (post : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ (temp, out, n, value, ()) heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ (temp, out, n, value, ()) heap output finish → (post.1 (Control.normal, reconstruct (entry (temp, out, n, value, ())) none output) finish).down) ∧ ∀ (value_1 : Unit) (output : Visible) (finish : Heap), returned✝ (temp, out, n, value, ()) heap value_1 output finish → (post.1 (Control.normal, reconstruct (entry (temp, out, n, value, ())) (some value_1) output) finish).down }⦄ work_scope2 none temp none none out n value ⦃post⦄

                                                                                          Apply the same completion contract at independently named source inputs. The actual action has canonical arguments, so native specification lookup does not need to infer a tuple from its projections.

                                                                                          The source-named mathematical value of this local.

                                                                                          Equations
                                                                                          Instances For
                                                                                            theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.completion_rel_contract {Model✝ : Type} (stateRel✝ : Model✝ → Visible → Heap → Prop) (invariant✝ : Model✝ → Prop) {relation✝ : Model✝ → Model✝ → Prop} :
                                                                                            WellFounded relation✝ → ∀ (ready : Model✝ → Visible → Heap → Visible → Heap → Prop) (normal : Visible → Heap → Prop) (returned : Buffer CellTy.nat → Visible → Heap → Prop), (∀ (model : Model✝), invariant✝ model → guard_completion_contract (stateRel✝ model) fun (start : Visible) (heap : Heap) (again : Bool) (output : Visible) (finish : Heap) => if again = true then ready model start heap output finish else normal output finish) → (∀ (model : Model✝) (start : Visible) (heap : Heap), invariant✝ model → stateRel✝ model start heap → body_completion_contract (ready model start heap) (fun (x : Visible) (x_1 : Heap) (output : Visible) (finish : Heap) => ∃ (next : Model✝), invariant✝ next ∧ stateRel✝ next output finish ∧ relation✝ next model) fun (x : Visible) (x_1 : Heap) => returned) → ∀ (model : Model✝), invariant✝ model → completion_contract (stateRel✝ model) (fun (x : Visible) (x_1 : Heap) => normal) fun (x : Visible) (x_1 : Heap) => returned

                                                                                            Relate this actual loop to a mathematical state without private completion slots. Only a continuing body supplies another invariant and a decrease; a local result exits through the real next guard.

                                                                                            @[reducible, inline]

                                                                                            The current local result; actual source control is unchanged.

                                                                                            Equations
                                                                                            Instances For

                                                                                              Source lexical immutability preserves these captures on every actual exit.

                                                                                              theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.visible_reconstruct (initial✝ : Locals) (pending✝ : Option (Buffer CellTy.nat)) (visible✝ : Visible) :
                                                                                              visible (reconstruct initial✝ pending✝ visible✝) = visible✝
                                                                                              @[reducible, inline]

                                                                                              The current local result; actual source control is unchanged.

                                                                                              Equations
                                                                                              Instances For

                                                                                                Assemble mathematical locals using source-variable parameter names.

                                                                                                Equations
                                                                                                Instances For

                                                                                                  Lossless proof coordinates, including fixed and shadowed captures.

                                                                                                  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.Scope.Implementation.Source.make_loop1.regroup_symm_apply (grouped✝ : Mutable × Captured) :
                                                                                                      Regroup.symm grouped✝ = (grouped✝.1.1, grouped✝.2.1, grouped✝.1.2.1, grouped✝.2.2.1, grouped✝.2.2.2.1, grouped✝.2.2.2.2.1, ())

                                                                                                      The source-visible values retain only roots from this heap.

                                                                                                      Equations
                                                                                                      • One or more equations did not get rendered due to their size.
                                                                                                      Instances For
                                                                                                        theorem Complexity.Language.Examples.Scope.Implementation.Source.work_total_iff (pre✝ : Buffer CellTy.nat → ℕ → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → ℕ → ℕ → Heap → Unit → Heap → Prop) :
                                                                                                        work_contract pre✝ post✝ ↔ ∀ (out : Buffer CellTy.nat) (n value : ℕ) (initialHeap : Heap), pre✝ out n value initialHeap → ∃ (value_1 : Unit) (finalHeap : Heap), work out n value initialHeap = Part.some (Except.ok value_1, finalHeap) ∧ post✝ out n value initialHeap value_1 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.Scope.Implementation.Source.make_loop1.guard_contract_iff (out✝ : Buffer CellTy.nat) (count n value : ℕ) (pre✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (normal✝ : ℕ → Option (Buffer CellTy.nat) → Heap → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) (returned✝ : ℕ → Option (Buffer CellTy.nat) → Heap → Bool → ℕ → Option (Buffer CellTy.nat) → Heap → Prop) :
                                                                                                        guard_contract out✝ count n value pre✝ normal✝ returned✝ ↔ ∀ (remaining : ℕ) (result_join : Option (Buffer CellTy.nat)) (heap : Heap), pre✝ remaining result_join heap → ⦃fun (current : Heap) => { down := current = heap }⦄ guard remaining out✝ result_join count n value ⦃(fun (outcome : Control Ty.bool × Locals) (finish : Heap) => { down := match outcome.1 with | Control.normal => have after := outcome.2; normal✝ remaining result_join heap after.1 after.2.2.1 finish | Control.returned value => have after := outcome.2; returned✝ remaining result_join heap value after.1 after.2.2.1 finish | Control.fault a => False }, PUnit.unit)⦄

                                                                                                        Introduce named mutable variables directly, without unpacking internal coordinates.

                                                                                                        theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.continue_eq (entry✝ : Env [Ty.nat, Ty.buffer CellTy.nat, (Ty.buffer CellTy.nat).option, Ty.nat, Ty.nat, Ty.nat]) (next✝ : Env [Ty.nat, Ty.buffer CellTy.nat, (Ty.buffer CellTy.nat).option, Ty.nat, Ty.nat, Ty.nat] → ExceptT Fault (StateT Heap Part) (Buffer CellTy.nat)) :
                                                                                                        Code.evalWith program entry✝ next✝ = do let __discr ← MonadLift.monadLift (make_loop1 (View entry✝).1 (View entry✝).2.1 (View entry✝).2.2.1 (View entry✝).2.2.2.1 (View entry✝).2.2.2.2.1 (View entry✝).2.2.2.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 locals.2.2.1 (Env.cons (entry✝.get Var.here.there.there.there) (Env.cons (entry✝.get Var.here.there.there.there.there) (Env.cons (entry✝.get Var.here.there.there.there.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 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.Scope.Implementation.Source.make_loop1.guard_completion_frame (start✝ : Visible) {heap✝ : Heap} {finish✝ : State [Ty.nat, Ty.buffer CellTy.nat, (Ty.buffer CellTy.nat).option, Ty.nat, Ty.nat, Ty.nat]} {control✝ : Control Ty.bool} :
                                                                                                          Exec program Guard { locals := View.symm (entry start✝), heap := heap✝ } finish✝ control✝ → entry (visible (View finish✝.locals)) = View finish✝.locals

                                                                                                          The actual guard leaves the private completion slots empty. Only the source-visible locals and heap may change before the loop body.

                                                                                                          Continuing completion coordinates recover the same full entry.

                                                                                                          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.Scope.Implementation.Source.work_scope2.visible_reconstruct (initial✝ : Locals) (pending✝ : Option Unit) (visible✝ : Visible) :
                                                                                                            visible (reconstruct initial✝ pending✝ visible✝) = visible✝
                                                                                                            @[reducible, inline]

                                                                                                            Restore the same full locals, retaining the ancestor slots from entry.

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

                                                                                                              Enter this source boundary with empty private completion slots.

                                                                                                              Equations
                                                                                                              Instances For
                                                                                                                @[reducible, inline]

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

                                                                                                                Equations
                                                                                                                Instances For
                                                                                                                  @[reducible, inline]

                                                                                                                  The real guard's contract mentions only source-visible inputs and outputs. Its compiler-slot frame is supplied by the generated bridge.

                                                                                                                  Equations
                                                                                                                  • One or more equations did not get rendered due to their size.
                                                                                                                  Instances For
                                                                                                                    theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope2.completion_spec_at {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                                                                                                    completion_contract pre✝ normal✝ returned✝ → ∀ (start : Visible) (post : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ start heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ start heap output finish → (post.1 (Control.normal, reconstruct (entry start) none output) finish).down) ∧ ∀ (value : Unit) (output : Visible) (finish : Heap), returned✝ start heap value output finish → (post.1 (Control.normal, reconstruct (entry start) (some value) output) finish).down }⦄ work_scope2 (entry start).1 (entry start).2.1 (entry start).2.2.1 (entry start).2.2.2.1 (entry start).2.2.2.2.1 (entry start).2.2.2.2.2.1 (entry start).2.2.2.2.2.2.1 ⦃post⦄

                                                                                                                    Apply the visible completion contract, reconstructing the full output from this same actual execution rather than guessing hidden coordinates.

                                                                                                                    @[reducible, inline]

                                                                                                                    Complete lexical coordinates for this source block.

                                                                                                                    Equations
                                                                                                                    • One or more equations did not get rendered due to their size.
                                                                                                                    Instances For
                                                                                                                      theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.pending_reconstruct (initial✝ : Locals) (pending✝ : Option (Buffer CellTy.nat)) (visible✝ : Visible) :
                                                                                                                      pending (reconstruct initial✝ pending✝ visible✝) = pending✝
                                                                                                                      @[reducible, inline]

                                                                                                                      The checked representation of this field at its current heap.

                                                                                                                      Equations
                                                                                                                      • One or more equations did not get rendered due to their size.
                                                                                                                      Instances For
                                                                                                                        theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.model_rel_value {whileState✝ : Model} {locals✝ : Locals} {heap✝ : Heap} :
                                                                                                                        modelRel whileState✝ locals✝ heap✝ → (Representation.ofEmbedding (Function.Embedding.refl ℕ)).Rel whileState✝.2.2.1 locals✝.2.2.2.2.2.1 heap✝

                                                                                                                        Extract a field's observation without source-coordinate transport.

                                                                                                                        @[reducible, inline]

                                                                                                                        Project source-visible coordinates from the unchanged full locals.

                                                                                                                        Equations
                                                                                                                        Instances For

                                                                                                                          Rearrange complete identity locals into their actual visible source order.

                                                                                                                          Equations
                                                                                                                          • One or more equations did not get rendered due to their size.
                                                                                                                          Instances For
                                                                                                                            theorem Complexity.Language.Examples.Scope.Implementation.Source.work_scope1.spec (scratchPending✝ result_join✝ : Option Unit) (out : Buffer CellTy.nat) (n value : ℕ) (post✝ : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)) :
                                                                                                                            ⦃fun (heap : Heap) => (Std.Do.wp (body scratchPending✝ result_join✝ out n value)).apply (fun (outcome : Control Ty.unit × Locals) (finish : Heap) => { down := ScopeSafe heap { locals := View.symm outcome.2, heap := finish } outcome.1 ∧ (post✝.1 outcome (finish.take heap.objects.size)).down }, PUnit.unit) heap⦄ work_scope1 scratchPending✝ result_join✝ out n value ⦃post✝⦄

                                                                                                                            Compose the actual body specification with safe scratch release. Surviving locals and return values stay rooted; current retained contents are not rolled back.

                                                                                                                            theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.observe (locals✝ : Locals) :
                                                                                                                            Stmt.observe View Code program locals✝ = make_loop1 locals✝.1 locals✝.2.1 locals✝.2.2.1 locals✝.2.2.2.1 locals✝.2.2.2.2.1 locals✝.2.2.2.2.2.1

                                                                                                                            Coordinate conversion for composing this named 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
                                                                                                                              @[reducible, inline]

                                                                                                                              Project source-visible coordinates from the unchanged full locals.

                                                                                                                              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

                                                                                                                                  Local completion does not produce an enclosing source return.

                                                                                                                                  The source-visible values retain only roots from this heap.

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

                                                                                                                                    Local completion does not produce an enclosing source return.

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

                                                                                                                                      Restore the same full locals, retaining the ancestor slots from entry.

                                                                                                                                      Equations
                                                                                                                                      Instances For

                                                                                                                                        Local completion does not produce an enclosing source return.

                                                                                                                                        The finite table of actual named source bodies.

                                                                                                                                        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

                                                                                                                                            Observe actual locals at the current heap, retaining their real aliases.

                                                                                                                                            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.

                                                                                                                                              @[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 same complete environment, grouped by source mutability.

                                                                                                                                                Equations
                                                                                                                                                • One or more equations did not get rendered due to their size.
                                                                                                                                                Instances For
                                                                                                                                                  theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.view_symm_apply (values✝ : Locals) :
                                                                                                                                                  View.symm values✝ = Env.cons values✝.1 (Env.cons values✝.2.1 (Env.cons values✝.2.2.1 (Env.cons values✝.2.2.2.1 (Env.cons values✝.2.2.2.2.1 (Env.cons values✝.2.2.2.2.2.1 Env.empty)))))

                                                                                                                                                  Restore complete lexical coordinates without unfolding proof fields.

                                                                                                                                                  theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.model_rel_n {whileState✝ : Model} {locals✝ : Locals} {heap✝ : Heap} :
                                                                                                                                                  modelRel whileState✝ locals✝ heap✝ → (Representation.ofEmbedding (Function.Embedding.refl ℕ)).Rel whileState✝.2.2.2.1 locals✝.2.2.2.2.1 heap✝

                                                                                                                                                  Extract a field's observation without source-coordinate transport.

                                                                                                                                                  @[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.Scope.Implementation.Source.work_scope2.body_completion_spec_at {pre✝ : Visible → Heap → Prop} {normal✝ : Visible → Heap → Visible → Heap → Prop} {returned✝ : Visible → Heap → Unit → Visible → Heap → Prop} :
                                                                                                                                                    body_completion_contract pre✝ normal✝ returned✝ → ∀ (start : Visible) (post : Std.Do.PostCond (Control Ty.unit × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ start heap ∧ (∀ (output : Visible) (finish : Heap), normal✝ start heap output finish → (post.1 (Control.normal, reconstruct (entry start) none output) finish).down) ∧ ∀ (value : Unit) (output : Visible) (finish : Heap), returned✝ start heap value output finish → (post.1 (Control.normal, reconstruct (entry start) (some value) output) finish).down }⦄ body (entry start).1 (entry start).2.1 (entry start).2.2.1 (entry start).2.2.2.1 (entry start).2.2.2.2.1 (entry start).2.2.2.2.2.1 (entry start).2.2.2.2.2.2.1 ⦃post⦄

                                                                                                                                                    Apply the visible completion contract, reconstructing the full output from this same actual execution rather than guessing hidden coordinates.

                                                                                                                                                    theorem Complexity.Language.Examples.Scope.Implementation.Source.make_loop1.captureView_symm_apply (grouped✝ : Mutable × Captured) :
                                                                                                                                                    CaptureView.symm grouped✝ = View.symm (grouped✝.1.1, grouped✝.2.1, grouped✝.1.2.1, grouped✝.2.2.1, grouped✝.2.2.2.1, grouped✝.2.2.2.2.1, ())

                                                                                                                                                    Restore grouped locals using the checked coordinate projection laws.

                                                                                                                                                    The ordinary mathematical contents expected after all calls.

                                                                                                                                                    Equations
                                                                                                                                                    Instances For

                                                                                                                                                      A single call either writes the value read from its initialized scratch array or leaves the result cell unchanged when that array is empty.

                                                                                                                                                      Equations
                                                                                                                                                      Instances For

                                                                                                                                                        One permanent result cell and two simultaneously allocated scratch arrays suffice independently of how many calls the loop performs.

                                                                                                                                                        Equations
                                                                                                                                                        Instances For

                                                                                                                                                          The inner body allocates its actual scratch array and then falls through. Its contract mentions only the original source variables and actual heap.

                                                                                                                                                          A nested allocation-only scope releases its entire fresh suffix. Both borrowed handles were live on entry, so neither is an escaping temporary.

                                                                                                                                                          The actual outer body reads its still-live temporary after inner cleanup. The ordinary contents contract describes the write to the surviving output.

                                                                                                                                                          Returning from the outer scope preserves the output's updated contents while releasing the temporary object from the actual final heap.

                                                                                                                                                          theorem Complexity.Language.Examples.Scope.work_eval (out : Buffer CellTy.nat) (n value previous : ℕ) (heap : Heap) (observed : out.Contents heap #[previous]) :
                                                                                                                                                          ∃ (finish : Heap), Implementation.Source.work out n value heap = Part.some (Except.ok (), finish) ∧ out.Contents finish (workContents n value previous)

                                                                                                                                                          The declared worker terminates and updates the ordinary contents of its borrowed output. No word width, memory capacity or time budget is assumed.

                                                                                                                                                          A source call reuses the worker's mathematical contents contract at its actual entry and final heaps, through the generated ordinary-argument bridge.

                                                                                                                                                          def Complexity.Language.Examples.Scope.invariant (out : Buffer CellTy.nat) (count n value remaining : ℕ) (heap : Heap) :

                                                                                                                                                          Only the remaining call count changes in the loop. The first completed nonempty call sets the result; all later calls preserve that same value.

                                                                                                                                                          Equations
                                                                                                                                                          Instances For
                                                                                                                                                            theorem Complexity.Language.Examples.Scope.invariant_initial (out : Buffer CellTy.nat) (count n value : ℕ) {heap : Heap} (observed : out.Contents heap #[0]) :
                                                                                                                                                            invariant out count n value count heap

                                                                                                                                                            The newly initialized output satisfies the loop invariant before any call.

                                                                                                                                                            theorem Complexity.Language.Examples.Scope.invariant_step (out : Buffer CellTy.nat) (count n value : ℕ) {remaining : ℕ} {heap finish : Heap} (current : invariant out count n value remaining heap) (active : 0 < remaining) (updated : out.Contents finish (workContents n value (if remaining < count ∧ 0 < n then value else 0))) :
                                                                                                                                                            invariant out count n value (remaining - 1) finish

                                                                                                                                                            A real call's contents contract advances the loop invariant. Termination uses the ordinary decrease of remaining, separately from machine cost.

                                                                                                                                                            theorem Complexity.Language.Examples.Scope.invariant_done (out : Buffer CellTy.nat) (count n value : ℕ) {remaining : ℕ} {heap : Heap} (current : invariant out count n value remaining heap) (finished : ¬0 < remaining) :
                                                                                                                                                            out.Contents heap (resultContents count n value)

                                                                                                                                                            At loop exit the result has the ordinary mathematical array contents.

                                                                                                                                                            The loop's source variables, selected by name. The buffer field records its handle; the invariant separately describes its contents at the current heap.

                                                                                                                                                            Equations
                                                                                                                                                            Instances For
                                                                                                                                                              theorem Complexity.Language.Examples.Scope.guard_model_spec (out : Buffer CellTy.nat) (count n value remaining : ℕ) (pre : Heap → Prop) :
                                                                                                                                                              Implementation.Source.make_loop1.guard_model_contract (loopModel out count n value) remaining pre fun (heap : Heap) (again : Bool) (next : ℕ) (finish : Heap) => again = decide (0 < remaining) ∧ next = remaining ∧ finish = heap

                                                                                                                                                              The actual guard decides positivity without changing the mathematical locals or heap. Its contract accepts any additional entry-heap condition.

                                                                                                                                                              theorem Complexity.Language.Examples.Scope.body_model_spec (out : Buffer CellTy.nat) (count n value : ℕ) {remaining : ℕ} {heap : Heap} (current : invariant out count n value remaining heap) (active : 0 < remaining) :
                                                                                                                                                              Implementation.Source.make_loop1.body_model_contract (loopModel out count n value) remaining (fun (entry : Heap) => entry = heap) (fun (x : Heap) (next : ℕ) (finish : Heap) => next = remaining - 1 ∧ invariant out count n value next finish) fun (x : Heap) (returned : Buffer CellTy.nat) (next : ℕ) (finish : Heap) => returned = out ∧ next = 0 ∧ out.Contents finish (resultContents count n value)

                                                                                                                                                              The worker's actual contents contract proves one mathematical loop round. Continuing rounds update the invariant; a local return supplies the final result.

                                                                                                                                                              theorem Complexity.Language.Examples.Scope.guard_eval (remaining : ℕ) (out : Buffer CellTy.nat) (count n value : ℕ) :
                                                                                                                                                              Implementation.Source.make_loop1.guard_completion_contract (fun (locals : Implementation.Source.make_loop1.Visible) (x : Heap) => locals = (remaining, out, count, n, value, ())) fun (x : Implementation.Source.make_loop1.Visible) (heap : Heap) (again : Bool) (locals : Implementation.Source.make_loop1.Visible) (finish : Heap) => again = decide (0 < remaining) ∧ locals = (remaining, out, count, n, value, ()) ∧ finish = heap

                                                                                                                                                              The loop's actual guard preserves its full lexical state and decides the ordinary positivity test used by the termination argument.

                                                                                                                                                              theorem Complexity.Language.Examples.Scope.body_spec (out : Buffer CellTy.nat) (count n value : ℕ) {remaining : ℕ} {heap : Heap} (current : invariant out count n value remaining heap) (active : 0 < remaining) :
                                                                                                                                                              Implementation.Source.make_loop1.body_completion_contract (fun (locals : Implementation.Source.make_loop1.Visible) (entry : Heap) => locals = (remaining, out, count, n, value, ()) ∧ entry = heap) (fun (x : Implementation.Source.make_loop1.Visible) (x_1 : Heap) (locals : Implementation.Source.make_loop1.Visible) (finish : Heap) => locals = (remaining - 1, out, count, n, value, ()) ∧ invariant out count n value (remaining - 1) finish) fun (x : Implementation.Source.make_loop1.Visible) (x_1 : Heap) (returned : Buffer CellTy.nat) (locals : Implementation.Source.make_loop1.Visible) (finish : Heap) => returned = out ∧ locals = (0, out, count, n, value, ()) ∧ out.Contents finish (resultContents count n value)

                                                                                                                                                              A real loop body calls the scoped worker and decreases only the remaining count. Its contents contract and complete local update also serve the separate resource proof; neither needs to re-prove the worker's behavior.

                                                                                                                                                              theorem Complexity.Language.Examples.Scope.loop_spec (out : Buffer CellTy.nat) (count n value remaining : ℕ) :
                                                                                                                                                              Implementation.Source.make_loop1.completion_contract (fun (locals : Implementation.Source.make_loop1.Visible) (heap : Heap) => locals = (remaining, out, count, n, value, ()) ∧ invariant out count n value remaining heap) (fun (x : Implementation.Source.make_loop1.Visible) (x_1 : Heap) (locals : Implementation.Source.make_loop1.Visible) (finish : Heap) => ∃ (left : ℕ), locals = (left, out, count, n, value, ()) ∧ out.Contents finish (resultContents count n value)) fun (x : Implementation.Source.make_loop1.Visible) (x_1 : Heap) (returned : Buffer CellTy.nat) (locals : Implementation.Source.make_loop1.Visible) (finish : Heap) => returned = out ∧ locals = (0, out, count, n, value, ()) ∧ out.Contents finish (resultContents count n value)

                                                                                                                                                              Lean's existing well-founded measure proves termination of the actual source loop. The relation concerns mathematical progress, not a machine budget; its body contract carries the current heap through every real call.

                                                                                                                                                              theorem Complexity.Language.Examples.Scope.make_spec (count n value : ℕ) :
                                                                                                                                                              ⦃fun (x : Heap) => { down := True }⦄ Implementation.Source.make count n value ⦃(fun (out : Buffer CellTy.nat) (finish : Heap) => { down := out.Contents finish (resultContents count n value) }, fun (x : Fault) (x_1 : Heap) => { down := False }, PUnit.unit)⦄

                                                                                                                                                              One source declaration allocates the surviving result and repeatedly uses the same nested scratch scopes. Its successful result has the ordinary array contents, for every count and every initial heap, including both empty cases.

                                                                                                                                                              theorem Complexity.Language.Examples.Scope.make_eval (count n value : ℕ) (heap : Heap) :
                                                                                                                                                              ∃ (out : Buffer CellTy.nat) (finish : Heap), Implementation.Source.make count n value heap = Part.some (Except.ok out, finish) ∧ out.Contents finish (resultContents count n value)

                                                                                                                                                              The mathematical contract supplies actual finite source evaluation and the retained output contents without a separate termination or budget proof.