Documentation

Examples.Language.Factorial

Ordinary proofs for recursive and iterative source factorials #

The source function makes a real self-call and multiplies its returned value. Its native Lean definition and ordinary natural-number induction prove the mathematical factorial result. Generated correspondence transfers that result to the same source program, retaining every initial shared heap.

The iterative declaration uses a finite source for and a mutable accumulator. Its native proof reduces the same iteration to an ordinary list fold, then reuses mathlib's factorial product formula. Generated correspondence supplies source correctness without an author-written loop invariant or heap adapter.

This is the high-level counterpart of the factorial use case in Examples.Ram.FactorialFunction. It proves independent source behavior, not word-RAM realizability for arbitrary natural inputs or an execution-time bound.

@[irreducible]

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

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    theorem Complexity.Language.Examples.Factorial.Implementation.factorial_action_eq (n : ℕ) :
    factorial_action n = have condition := decide (n = 0); if condition = true then pure 1 else have operand := n.sub 1; do let previous ← factorial_action operand pure (n.mul previous)

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

    The actual source action terminates with the generated native result and preserves every initial heap. Lean's native recursion principle proves this once.

    theorem Complexity.Language.Examples.Factorial.Implementation.factorial_spec {pre✝ : Env [Ty.nat] → Heap → Prop} {post✝ : Env [Ty.nat] → Heap → ℕ → Heap → Prop} :
    FunctionTotal program factorialId pre✝ post✝ → ∀ (n : ℕ) (continuation : Std.Do.PostCond ℕ (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons n Env.empty) initialHeap ∧ ∀ (value : ℕ) (finalHeap : Heap), post✝ (Env.cons n Env.empty) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ factorial_action n ⦃continuation⦄

    Apply a supplied source contract with ordinary named arguments and its actual returned value and final heap. No callee body or new contract is inferred.

    Exact total source contract inherited from the generated native function.

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

    theorem Complexity.Language.Examples.Factorial.Implementation.factorial_contract_iff_triple (pre✝ : ℕ → Heap → Prop) (post✝ : ℕ → Heap → ℕ → Heap → Prop) :
    factorial_contract pre✝ post✝ ↔ ∀ (n : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ n initialHeap }⦄ factorial_action n ⦃(fun (value : ℕ) (finalHeap : Heap) => { down := post✝ n initialHeap value finalHeap }, fun (x : Fault) (x_1 : Heap) => { down := False }, PUnit.unit)⦄

    Prove the ordinary-parameter source contract with a total-correctness triple for the same action. The ghost initial heap equals the actual entry heap; successful results use the actual final heap, and faults are excluded.

    @[reducible, inline]

    The actual declared argument environment, constructed from ordinary source parameters. This is proof-side parameter transport, not an additional runtime operation.

    Equations
    Instances For

      The finite table of actual named source bodies.

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

        Budget-free total correctness with ordinary source parameters and relational initial/final heap postconditions. This is the existing source function contract.

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

          The named function's actual 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.Factorial.Implementation.factorial_total_iff (pre✝ : ℕ → Heap → Prop) (post✝ : ℕ → Heap → ℕ → Heap → Prop) :
            factorial_contract pre✝ post✝ ↔ ∀ (n : ℕ) (initialHeap : Heap), pre✝ n initialHeap → ∃ (value : ℕ) (finalHeap : Heap), factorial_action n initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ n initialHeap value finalHeap

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

            @[reducible, inline]

            Apply a predicate, postcondition or bound with ordinary source parameters to the actual argument environment. No new contract or execution is introduced.

            Equations
            Instances For

              The named function's actual independently interpreted source body.

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

                Ordinary induction proves the implementation's mathematical result.

                theorem Complexity.Language.Examples.Factorial.factorial_total :
                Implementation.factorial_contract (fun (x : ℕ) (x_1 : Heap) => True) fun (n : ℕ) (heap : Heap) (value : ℕ) (finish : Heap) => value = n.factorial ∧ finish = heap

                Generated correspondence transfers the mathematical result to total source correctness without a second recursive proof or manual heap conversion.

                theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.captureView_symm_apply (grouped✝ : Mutable × Captured) :
                CaptureView.symm grouped✝ = View.symm (grouped✝.1.1, grouped✝.1.2.1, grouped✝.2.1, ())

                Restore grouped locals using the checked coordinate projection laws.

                theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.rel_contract (n : ℕ) {Model✝ : Type} (stateRel✝ : Model✝ → ℕ → ℕ → Heap → Prop) (invariant✝ : Model✝ → Prop) {relation✝ : Model✝ → Model✝ → Prop} :
                WellFounded relation✝ → ∀ (ready : Model✝ → ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop) (normal : ℕ → ℕ → Heap → Prop) (returned : ℕ → ℕ → ℕ → Heap → Prop), (∀ (model : Model✝), invariant✝ model → guard_contract n (stateRel✝ model) (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (finish : Heap) => False) fun (i acc : ℕ) (heap : Heap) (again : Bool) (after_i after_acc : ℕ) (finish : Heap) => if again = true then ready model i acc heap after_i after_acc finish else normal after_i after_acc finish) → (∀ (model : Model✝) (i acc : ℕ) (heap : Heap), invariant✝ model → stateRel✝ model i acc heap → body_contract n (ready model i acc heap) (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (finish : Heap) => ∃ (next : Model✝), invariant✝ next ∧ stateRel✝ next after_i after_acc finish ∧ relation✝ next model) fun (x x_1 : ℕ) (x_2 : Heap) (value after_i after_acc : ℕ) (finish : Heap) => returned value after_i after_acc finish) → ∀ (model : Model✝), invariant✝ model → contract n (stateRel✝ model) (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (finish : Heap) => normal after_i after_acc finish) fun (x x_1 : ℕ) (x_2 : Heap) (value after_i after_acc : ℕ) (finish : Heap) => returned value after_i after_acc finish

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

                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.Factorial.Iterative.factorial_loop1.guard_contract_iff (n : ℕ) (pre✝ : ℕ → ℕ → Heap → Prop) (normal✝ : ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop) (returned✝ : ℕ → ℕ → Heap → Bool → ℕ → ℕ → Heap → Prop) :
                  guard_contract n pre✝ normal✝ returned✝ ↔ ∀ (i acc : ℕ) (heap : Heap), pre✝ i acc heap → ⦃fun (current : Heap) => { down := current = heap }⦄ guard i acc n ⦃(fun (outcome : Control Ty.bool × Locals) (finish : Heap) => { down := match outcome.1 with | Control.normal => have after := outcome.2; normal✝ i acc heap after.1 after.2.1 finish | Control.returned value => have after := outcome.2; returned✝ i acc heap value after.1 after.2.1 finish | Control.fault a => False }, PUnit.unit)⦄

                  Introduce named mutable variables directly, without unpacking internal coordinates.

                  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.Factorial.Iterative.factorial_loop1.body_contract_iff (n : ℕ) (pre✝ : ℕ → ℕ → Heap → Prop) (normal✝ : ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop) (returned✝ : ℕ → ℕ → Heap → ℕ → ℕ → ℕ → Heap → Prop) :
                    body_contract n pre✝ normal✝ returned✝ ↔ ∀ (i acc : ℕ) (heap : Heap), pre✝ i acc heap → ⦃fun (current : Heap) => { down := current = heap }⦄ body i acc n ⦃(fun (outcome : Control Ty.nat × Locals) (finish : Heap) => { down := match outcome.1 with | Control.normal => have after := outcome.2; normal✝ i acc heap after.1 after.2.1 finish | Control.returned value => have after := outcome.2; returned✝ i acc heap value after.1 after.2.1 finish | Control.fault a => False }, PUnit.unit)⦄

                    Introduce named mutable variables directly, without unpacking internal coordinates.

                    theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.body_spec (n : ℕ) {pre✝ : ℕ → ℕ → Heap → Prop} {normal✝ : ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop} {returned✝ : ℕ → ℕ → Heap → ℕ → ℕ → ℕ → Heap → Prop} :
                    body_contract n pre✝ normal✝ returned✝ → ∀ (i acc : ℕ) (post : Std.Do.PostCond (Control Ty.nat × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ i acc heap ∧ (∀ (after_i after_acc : ℕ) (finish : Heap), normal✝ i acc heap after_i after_acc finish → (post.1 (Control.normal, after_i, after_acc, n, ()) finish).down) ∧ ∀ (value after_i after_acc : ℕ) (finish : Heap), returned✝ i acc heap value after_i after_acc finish → (post.1 (Control.returned value, after_i, after_acc, n, ()) finish).down }⦄ body i acc n ⦃post⦄

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

                    @[reducible, inline]
                    abbrev Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.guard_contract (n : ℕ) :
                    (ℕ → ℕ → Heap → Prop) → (ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop) → (ℕ → ℕ → Heap → Bool → ℕ → ℕ → Heap → Prop) → Prop

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

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.contract_iff (n : ℕ) (pre✝ : ℕ → ℕ → Heap → Prop) (normal✝ : ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop) (returned✝ : ℕ → ℕ → Heap → ℕ → ℕ → ℕ → Heap → Prop) :
                      contract n pre✝ normal✝ returned✝ ↔ ∀ (i acc : ℕ) (heap : Heap), pre✝ i acc heap → ⦃fun (current : Heap) => { down := current = heap }⦄ factorial_loop1 i acc n ⦃(fun (outcome : Control Ty.nat × Locals) (finish : Heap) => { down := match outcome.1 with | Control.normal => have after := outcome.2; normal✝ i acc heap after.1 after.2.1 finish | Control.returned value => have after := outcome.2; returned✝ i acc heap value after.1 after.2.1 finish | Control.fault a => False }, PUnit.unit)⦄

                      Introduce named mutable variables directly, without unpacking internal coordinates.

                      theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.guard_preservesCaptures {entry✝ finish✝ : State [Ty.nat, Ty.nat, Ty.nat]} {control✝ : Control Ty.bool} :
                      Exec program Guard entry✝ finish✝ control✝ → (CaptureView finish✝.locals).2 = (CaptureView entry✝.locals).2

                      Source lexical immutability preserves these captures on every actual exit.

                      theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.variant_contract (n : ℕ) (invariant✝ : ℕ → ℕ → Heap → Prop) (variant✝ : ℕ → ℕ → Heap → ℕ) (ready✝ : ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop) (normal✝ : ℕ → ℕ → Heap → Prop) (returned✝ : ℕ → ℕ → ℕ → Heap → Prop) :
                      (guard_contract n invariant✝ (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (finish : Heap) => False) fun (i acc : ℕ) (heap : Heap) (again : Bool) (after_i after_acc : ℕ) (finish : Heap) => if again = true then ready✝ i acc heap after_i after_acc finish else normal✝ after_i after_acc finish) → (∀ (i acc : ℕ) (heap : Heap), invariant✝ i acc heap → body_contract n (ready✝ i acc heap) (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (finish : Heap) => invariant✝ after_i after_acc finish ∧ variant✝ after_i after_acc finish < variant✝ i acc heap) fun (x x_1 : ℕ) (x_2 : Heap) (value after_i after_acc : ℕ) (finish : Heap) => returned✝ value after_i after_acc finish) → contract n invariant✝ (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (finish : Heap) => normal✝ after_i after_acc finish) fun (x x_1 : ℕ) (x_2 : Heap) (value after_i after_acc : ℕ) (finish : Heap) => returned✝ value after_i after_acc 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 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

                        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.Factorial.Iterative.factorial_spec {pre✝ : Env [Ty.nat] → Heap → Prop} {post✝ : Env [Ty.nat] → Heap → ℕ → Heap → Prop} :
                          FunctionTotal program factorialId pre✝ post✝ → ∀ (n : ℕ) (continuation : Std.Do.PostCond ℕ (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons n Env.empty) initialHeap ∧ ∀ (value : ℕ) (finalHeap : Heap), post✝ (Env.cons n Env.empty) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ factorial_action n ⦃continuation⦄

                          Apply a supplied source contract with ordinary named arguments and its actual returned value and final heap. No callee body or new contract is inferred.

                          theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.spec (n : ℕ) {pre✝ : ℕ → ℕ → Heap → Prop} {normal✝ : ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop} {returned✝ : ℕ → ℕ → Heap → ℕ → ℕ → ℕ → Heap → Prop} :
                          contract n pre✝ normal✝ returned✝ → ∀ (i acc : ℕ) (post : Std.Do.PostCond (Control Ty.nat × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ i acc heap ∧ (∀ (after_i after_acc : ℕ) (finish : Heap), normal✝ i acc heap after_i after_acc finish → (post.1 (Control.normal, after_i, after_acc, n, ()) finish).down) ∧ ∀ (value after_i after_acc : ℕ) (finish : Heap), returned✝ i acc heap value after_i after_acc finish → (post.1 (Control.returned value, after_i, after_acc, n, ()) finish).down }⦄ factorial_loop1 i acc n ⦃post⦄

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

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

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

                            A lossless coordinate permutation; no source operation is executed.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.guard_spec (n : ℕ) {pre✝ : ℕ → ℕ → Heap → Prop} {normal✝ : ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop} {returned✝ : ℕ → ℕ → Heap → Bool → ℕ → ℕ → Heap → Prop} :
                              guard_contract n pre✝ normal✝ returned✝ → ∀ (i acc : ℕ) (post : Std.Do.PostCond (Control Ty.bool × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)), ⦃fun (heap : Heap) => { down := pre✝ i acc heap ∧ (∀ (after_i after_acc : ℕ) (finish : Heap), normal✝ i acc heap after_i after_acc finish → (post.1 (Control.normal, after_i, after_acc, n, ()) finish).down) ∧ ∀ (value : Bool) (after_i after_acc : ℕ) (finish : Heap), returned✝ i acc heap value after_i after_acc finish → (post.1 (Control.returned value, after_i, after_acc, n, ()) finish).down }⦄ guard i acc n ⦃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.Factorial.Iterative.factorial_contract_iff_triple (pre✝ : ℕ → Heap → Prop) (post✝ : ℕ → Heap → ℕ → Heap → Prop) :
                              factorial_contract pre✝ post✝ ↔ ∀ (n : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ n initialHeap }⦄ factorial_action n ⦃(fun (value : ℕ) (finalHeap : Heap) => { down := post✝ n initialHeap value finalHeap }, fun (x : Fault) (x_1 : Heap) => { down := False }, PUnit.unit)⦄

                              Prove the ordinary-parameter source contract with a total-correctness triple for the same action. The ghost initial heap equals the actual entry heap; successful results use the actual final heap, and faults are excluded.

                              @[reducible, inline]
                              abbrev Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.contract (n : ℕ) :
                              (ℕ → ℕ → Heap → Prop) → (ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop) → (ℕ → ℕ → Heap → ℕ → ℕ → ℕ → Heap → Prop) → Prop

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

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For
                                theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.body_preservesCaptures {entry✝ finish✝ : State [Ty.nat, Ty.nat, Ty.nat]} {control✝ : Control Ty.nat} :
                                Exec program Body entry✝ finish✝ control✝ → (CaptureView finish✝.locals).2 = (CaptureView entry✝.locals).2

                                Source lexical immutability preserves these captures on every actual exit.

                                @[reducible, inline]

                                Fixed lexical captures, independent of the shared heap.

                                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
                                    theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.continue_eq (entry✝ : Env [Ty.nat, Ty.nat, Ty.nat]) (next✝ : Env [Ty.nat, Ty.nat, Ty.nat] → ExceptT Fault (StateT Heap Part) ℕ) :
                                    Code.evalWith program entry✝ next✝ = do let __discr ← MonadLift.monadLift (factorial_loop1 (View entry✝).1 (View entry✝).2.1 (View entry✝).2.2.1) match __discr with | (control, locals) => match control with | Control.normal => next✝ (Env.cons locals.1 (Env.cons locals.2.1 (Env.cons (entry✝.get Var.here.there.there) Env.empty))) | Control.returned value => pure value | Control.fault error => throw error

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

                                    Observe lexical coordinates without unfolding the equivalence structure.

                                    theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.wellFounded_contract (n : ℕ) (invariant✝ : ℕ → ℕ → Heap → Prop) (relation✝ : Mutable × Heap → Mutable × Heap → Prop) :
                                    WellFounded relation✝ → ∀ (ready : ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop) (normal : ℕ → ℕ → Heap → Prop) (returned : ℕ → ℕ → ℕ → Heap → Prop), (guard_contract n invariant✝ (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (finish : Heap) => False) fun (i acc : ℕ) (heap : Heap) (again : Bool) (after_i after_acc : ℕ) (finish : Heap) => if again = true then ready i acc heap after_i after_acc finish else normal after_i after_acc finish) → (∀ (i acc : ℕ) (heap : Heap), invariant✝ i acc heap → body_contract n (ready i acc heap) (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (finish : Heap) => invariant✝ after_i after_acc finish ∧ relation✝ ((after_i, after_acc, ()), finish) ((i, acc, ()), heap)) fun (x x_1 : ℕ) (x_2 : Heap) (value after_i after_acc : ℕ) (finish : Heap) => returned value after_i after_acc finish) → contract n invariant✝ (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (finish : Heap) => normal after_i after_acc finish) fun (x x_1 : ℕ) (x_2 : Heap) (value after_i after_acc : ℕ) (finish : Heap) => returned value after_i after_acc 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 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.Factorial.Iterative.factorial_total :
                                      FunctionTotal program factorialId (fun (x : Env signatures[factorialId].params) (x_1 : Heap) => True) fun (arguments : Env signatures[factorialId].params) (initialHeap : Heap) (value : Value signatures[factorialId].result) (finalHeap : Heap) => value = factorial arguments.head ∧ finalHeap = initialHeap

                                      Exact total source contract inherited from the generated native function.

                                      theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.guard_observe (locals✝ : Locals) :
                                      Stmt.observe View Guard program locals✝ = guard locals✝.1 locals✝.2.1 locals✝.2.2.1

                                      Coordinate conversion for composing this named observation.

                                      @[reducible, inline]

                                      The actual declared argument environment, constructed from ordinary source parameters. This is proof-side parameter transport, not an additional runtime operation.

                                      Equations
                                      Instances For
                                        theorem Complexity.Language.Examples.Factorial.Iterative.factorial_total_iff (pre✝ : ℕ → Heap → Prop) (post✝ : ℕ → Heap → ℕ → Heap → Prop) :
                                        factorial_contract pre✝ post✝ ↔ ∀ (n : ℕ) (initialHeap : Heap), pre✝ n initialHeap → ∃ (value : ℕ) (finalHeap : Heap), factorial_action n initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ n initialHeap value finalHeap

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

                                        theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.wellFounded_spec (n : ℕ) (invariant✝ : ℕ → ℕ → Heap → Prop) (relation✝ : Mutable × Heap → Mutable × Heap → Prop) :
                                        WellFounded relation✝ → ∀ (normal : ℕ → ℕ → Heap → Prop) (returned : ℕ → ℕ → ℕ → Heap → Prop), (∀ (i acc : ℕ) (startHeap : Heap), invariant✝ i acc startHeap → ⦃fun (currentHeap : Heap) => { down := currentHeap = startHeap }⦄ guard i acc n ⦃(fun (guardOutcome : Control Ty.bool × Locals) (afterGuardHeap : Heap) => { down := have afterGuard_i := guardOutcome.2.1; have afterGuard_acc := guardOutcome.2.2.1; match guardOutcome.1 with | Control.returned again => if again = true then ⦃fun (currentHeap : Heap) => { down := currentHeap = afterGuardHeap }⦄ body afterGuard_i afterGuard_acc n ⦃(fun (bodyOutcome : Control Ty.nat × Locals) (afterBodyHeap : Heap) => { down := have afterBody_i := bodyOutcome.2.1; have afterBody_acc := bodyOutcome.2.2.1; match bodyOutcome.1 with | Control.normal => invariant✝ afterBody_i afterBody_acc afterBodyHeap ∧ relation✝ ((afterBody_i, afterBody_acc, ()), afterBodyHeap) ((i, acc, ()), startHeap) | Control.returned value => returned value afterBody_i afterBody_acc afterBodyHeap | Control.fault a => False }, PUnit.unit)⦄ else normal afterGuard_i afterGuard_acc afterGuardHeap | Control.normal => False | Control.fault a => False }, PUnit.unit)⦄) → ∀ (i acc : ℕ), ⦃fun (heap : Heap) => { down := invariant✝ i acc heap }⦄ factorial_loop1 i acc n ⦃(fun (outcome : Control Ty.nat × Locals) (heap : Heap) => { down := have final_i := outcome.2.1; have final_acc := outcome.2.2.1; match outcome.1 with | Control.normal => normal final_i final_acc heap | Control.returned value => returned value final_i final_acc 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.Factorial.Iterative.factorial_loop1.eq_pure (i✝ acc✝ n : ℕ) (positiveStep✝ : 0 < 1) :
                                        (∀ (index : ℕ) (mutable : ℕ × Unit), index < n → body index mutable.1 n = have outcome := (fun (index : ℕ) (mutable : ℕ × Unit) => (have i := index; have acc := mutable.1; have pattern := i; have i_1 := pattern; have operand := i_1.add 1; have acc := acc.mul operand; have i := i.add 1; pure (none, acc, ())).run) index mutable; pure (match outcome.1 with | none => (Control.normal, index + 1, outcome.2.1, n, ()) | some value => (Control.returned value, index, outcome.2.1, n, ()))) → factorial_loop1 i✝ acc✝ n = have outcome := have outcome := (forIn [i✝:n] (none, i✝, acc✝, ()) fun (index : ℕ) (state : Option ℕ × ℕ × ℕ × Unit) => have outcome := (fun (index : ℕ) (mutable : ℕ × Unit) => (have i := index; have acc := mutable.1; have pattern := i; have i_1 := pattern; have operand := i_1.add 1; have acc := acc.mul operand; have i := i.add 1; pure (none, acc, ())).run) index state.2.2; outcome.1.elim (pure (ForInStep.yield (none, index + 1, outcome.2))) fun (value : ℕ) => pure (ForInStep.done (some value, index, outcome.2))).run; (outcome.1, outcome.2.1, outcome.2.2.1, n, ()); pure (outcome.1.elim Control.normal Control.returned, outcome.2)

                                        Finite iteration correspondence, with its one-body obligation left in the enclosing function's callee and recursive-hypothesis scope.

                                        theorem Complexity.Language.Examples.Factorial.Iterative.factorial_action_eq (n : ℕ) :
                                        factorial_action n = have acc := 1; have i := 0; have positiveStep := ⋯; do let __discr ← MonadLift.monadLift (factorial_loop1 i acc n) match __discr with | (loopControl, loopLocals) => have i := loopLocals.1; have acc := loopLocals.2.1; have __do_jp := fun (y : Unit) => pure acc; match loopControl with | Control.normal => do let y ← pure () __do_jp y | Control.returned returned => pure returned | Control.fault error => do let y ← throw error __do_jp y

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

                                        @[reducible, inline]
                                        abbrev Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.body_contract (n : ℕ) :
                                        (ℕ → ℕ → Heap → Prop) → (ℕ → ℕ → Heap → ℕ → ℕ → Heap → Prop) → (ℕ → ℕ → Heap → ℕ → ℕ → ℕ → Heap → Prop) → Prop

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

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For
                                          theorem Complexity.Language.Examples.Factorial.Iterative.factorial_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.empty))

                                          Restore complete lexical coordinates without unfolding proof fields.

                                          theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.count_frame_contract (n : ℕ) (count✝ : ℕ → ℕ → ℕ) (limit✝ : ℕ) (invariant✝ : ℕ → ℕ → Heap → Prop) (frame✝ : Heap → Heap → Prop) :
                                          (∀ {a b c : Heap}, frame✝ a b → frame✝ b c → frame✝ a c) → ∀ (normal : Heap → Prop), (guard_contract n invariant✝ (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (x_3 : Heap) => False) fun (i acc : ℕ) (heap : Heap) (again : Bool) (after_i after_acc : ℕ) (finish : Heap) => count✝ after_i after_acc = count✝ i acc ∧ finish = heap ∧ invariant✝ after_i after_acc finish ∧ (again = true ↔ count✝ after_i after_acc < limit✝)) → (body_contract n (fun (i acc : ℕ) (heap : Heap) => invariant✝ i acc heap ∧ count✝ i acc < limit✝) (fun (i acc : ℕ) (heap : Heap) (after_i after_acc : ℕ) (finish : Heap) => count✝ after_i after_acc = count✝ i acc + 1 ∧ invariant✝ after_i after_acc finish ∧ frame✝ heap finish) fun (x x_1 : ℕ) (x_2 : Heap) (x_3 after_i after_acc : ℕ) (x_4 : Heap) => False) → (∀ (i acc : ℕ) (heap : Heap), invariant✝ i acc heap → limit✝ ≤ count✝ i acc → normal heap) → ∀ (initial : Heap), contract n (fun (i acc : ℕ) (heap : Heap) => invariant✝ i acc heap ∧ frame✝ initial heap) (fun (x x_1 : ℕ) (x_2 : Heap) (after_i after_acc : ℕ) (finish : Heap) => normal finish ∧ frame✝ initial finish) fun (x x_1 : ℕ) (x_2 : Heap) (x_3 after_i after_acc : ℕ) (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.

                                          theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.regroup_symm_apply (grouped✝ : Mutable × Captured) :
                                          Regroup.symm grouped✝ = (grouped✝.1.1, grouped✝.1.2.1, grouped✝.2.1, ())
                                          theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.regroup_apply (values✝ : Locals) :
                                          Regroup values✝ = ((values✝.1, values✝.2.1, ()), values✝.2.2.1, ())

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

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.variant_spec (n : ℕ) (invariant✝ : ℕ → ℕ → Heap → Prop) (variant✝ : ℕ → ℕ → Heap → ℕ) (normal✝ : ℕ → ℕ → Heap → Prop) (returned✝ : ℕ → ℕ → ℕ → Heap → Prop) :
                                            (∀ (i acc : ℕ) (startHeap : Heap), invariant✝ i acc startHeap → ⦃fun (currentHeap : Heap) => { down := currentHeap = startHeap }⦄ guard i acc n ⦃(fun (guardOutcome : Control Ty.bool × Locals) (afterGuardHeap : Heap) => { down := have afterGuard_i := guardOutcome.2.1; have afterGuard_acc := guardOutcome.2.2.1; match guardOutcome.1 with | Control.returned again => if again = true then ⦃fun (currentHeap : Heap) => { down := currentHeap = afterGuardHeap }⦄ body afterGuard_i afterGuard_acc n ⦃(fun (bodyOutcome : Control Ty.nat × Locals) (afterBodyHeap : Heap) => { down := have afterBody_i := bodyOutcome.2.1; have afterBody_acc := bodyOutcome.2.2.1; match bodyOutcome.1 with | Control.normal => invariant✝ afterBody_i afterBody_acc afterBodyHeap ∧ variant✝ afterBody_i afterBody_acc afterBodyHeap < variant✝ i acc startHeap | Control.returned value => returned✝ value afterBody_i afterBody_acc afterBodyHeap | Control.fault a => False }, PUnit.unit)⦄ else normal✝ afterGuard_i afterGuard_acc afterGuardHeap | Control.normal => False | Control.fault a => False }, PUnit.unit)⦄) → ∀ (i acc : ℕ), ⦃fun (heap : Heap) => { down := invariant✝ i acc heap }⦄ factorial_loop1 i acc n ⦃(fun (outcome : Control Ty.nat × Locals) (heap : Heap) => { down := have final_i := outcome.2.1; have final_acc := outcome.2.2.1; match outcome.1 with | Control.normal => normal✝ final_i final_acc heap | Control.returned value => returned✝ value final_i final_acc heap | Control.fault a => False }, PUnit.unit)⦄

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

                                            @[reducible, inline]

                                            The source program's declared first-order signatures.

                                            Equations
                                            Instances For

                                              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

                                                The actual source action terminates with the generated native result and preserves every initial heap. Lean's native recursion principle proves this once.

                                                theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.observe (locals✝ : Locals) :
                                                Stmt.observe View Code program locals✝ = factorial_loop1 locals✝.1 locals✝.2.1 locals✝.2.2.1

                                                Coordinate conversion for composing this named observation.

                                                theorem Complexity.Language.Examples.Factorial.Iterative.factorial_loop1.preservesCaptures {entry✝ finish✝ : State [Ty.nat, Ty.nat, Ty.nat]} {control✝ : Control Ty.nat} :
                                                Exec program Code entry✝ finish✝ control✝ → (CaptureView finish✝.locals).2 = (CaptureView entry✝.locals).2

                                                Source lexical immutability preserves these captures on every actual exit.

                                                @[reducible, inline]

                                                Apply a predicate, postcondition or bound with ordinary source parameters to the actual argument environment. No new contract or execution is introduced.

                                                Equations
                                                Instances For

                                                  The named function's actual independently interpreted source body.

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

                                                    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

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

                                                      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.Factorial.Iterative.factorial_loop1.body_observe (locals✝ : Locals) :
                                                        Stmt.observe View Body program locals✝ = body locals✝.1 locals✝.2.1 locals✝.2.2.1

                                                        Coordinate conversion for composing this named observation.

                                                        The finite table of actual named source bodies.

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

                                                          The finite source iteration computes mathlib's factorial. Its proof uses ordinary fold and product identities, without source execution bookkeeping.

                                                          theorem Complexity.Language.Examples.Factorial.iterative_factorial_total :
                                                          Iterative.factorial_contract (fun (x : ℕ) (x_1 : Heap) => True) fun (n : ℕ) (heap : Heap) (value : ℕ) (finish : Heap) => value = n.factorial ∧ finish = heap

                                                          The generated pure correspondence transfers the iterative result to the same source body, with no second termination proof or assumed machine budget.