Documentation

Complexity.Language.Eval.Locals.Verification

Loop correctness with ordinary local values #

The invariant and decrease relation below concern ordinary local coordinates and the actual shared heap. They are transported through the lossless view to the existing source well-founded loop rule. No loop interpreter, source product type, machine representation or proposed instruction budget is introduced.

The guard specification starts before the guard and retains its actual locals and heap. A false guard establishes the final postcondition there. A true guard runs the body from that state; only normal completion must restore the invariant and decrease relative to the state before the guard. Early body returns instead establish their result postcondition, while faults and missing guard returns cannot prove these strict specifications.

observe_while_contract separates the guard and body into ordinary relational BlockSpec contracts. Its ready predicate retains the complete iteration's starting state, so the body can establish well-founded progress without a nested native triple or manual control-outcome transport in the author's proof.

theorem Complexity.Language.Stmt.observe_while_spec {signatures : List Signature} {Γ : List Ty} {result : Ty} {Locals : Type} (view : Env Γ ≃ Locals) (program : Program signatures) (guard : Stmt signatures Γ Ty.bool) (body : Stmt signatures Γ result) (invariant : Locals → Heap → Prop) (relation : Locals × Heap → Locals × Heap → Prop) (wellFounded : WellFounded relation) (post : Std.Do.PostCond (Control result × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)) (step : ∀ (startLocals : Locals) (startHeap : Heap), invariant startLocals startHeap → ⦃fun (current : Heap) => { down := current = startHeap }⦄ observe view guard program startLocals ⦃(fun (guardOutcome : Control Ty.bool × Locals) (afterGuardHeap : Heap) => { down := match guardOutcome.1 with | Control.returned again => if again = true then ⦃fun (current : Heap) => { down := current = afterGuardHeap }⦄ observe view body program guardOutcome.2 ⦃(fun (bodyOutcome : Control result × Locals) (afterBodyHeap : Heap) => { down := match bodyOutcome.1 with | Control.normal => invariant bodyOutcome.2 afterBodyHeap ∧ relation (bodyOutcome.2, afterBodyHeap) (startLocals, startHeap) | Control.returned value => (post.1 (Control.returned value, bodyOutcome.2) afterBodyHeap).down | Control.fault a => False }, PUnit.unit)⦄ else (post.1 (Control.normal, guardOutcome.2) afterGuardHeap).down | Control.normal => False | Control.fault a => False }, PUnit.unit)⦄) (locals : Locals) :
⦃fun (heap : Heap) => { down := invariant locals heap }⦄ observe view (guard.while body) program locals ⦃post⦄

Prove the actual source loop using an invariant and well-founded decrease over ordinary locals and shared heaps. The guard and body are the same block observations used in the loop equation, not supplied executable callbacks.

theorem Complexity.Language.Stmt.observe_while_variant_spec {signatures : List Signature} {Γ : List Ty} {result : Ty} {Locals : Type} (view : Env Γ ≃ Locals) (program : Program signatures) (guard : Stmt signatures Γ Ty.bool) (body : Stmt signatures Γ result) (invariant : Locals → Heap → Prop) (variant : Locals → Heap → ℕ) (post : Std.Do.PostCond (Control result × Locals) (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure)) (step : ∀ (startLocals : Locals) (startHeap : Heap), invariant startLocals startHeap → ⦃fun (current : Heap) => { down := current = startHeap }⦄ observe view guard program startLocals ⦃(fun (guardOutcome : Control Ty.bool × Locals) (afterGuardHeap : Heap) => { down := match guardOutcome.1 with | Control.returned again => if again = true then ⦃fun (current : Heap) => { down := current = afterGuardHeap }⦄ observe view body program guardOutcome.2 ⦃(fun (bodyOutcome : Control result × Locals) (afterBodyHeap : Heap) => { down := match bodyOutcome.1 with | Control.normal => invariant bodyOutcome.2 afterBodyHeap ∧ variant bodyOutcome.2 afterBodyHeap < variant startLocals startHeap | Control.returned value => (post.1 (Control.returned value, bodyOutcome.2) afterBodyHeap).down | Control.fault a => False }, PUnit.unit)⦄ else (post.1 (Control.normal, guardOutcome.2) afterGuardHeap).down | Control.normal => False | Control.fault a => False }, PUnit.unit)⦄) (locals : Locals) :
⦃fun (heap : Heap) => { down := invariant locals heap }⦄ observe view (guard.while body) program locals ⦃post⦄

A natural-valued mathematical variant is a specialization of the same well-founded rule. It measures a complete guard/body iteration, not runtime instructions, fuel or a budget required for functional correctness.

theorem Complexity.Language.Stmt.observe_while_contract {signatures : List Signature} {Γ : List Ty} {result : Ty} {Locals : Type} (view : Env Γ ≃ Locals) (program : Program signatures) (guard : Stmt signatures Γ Ty.bool) (body : Stmt signatures Γ result) (invariant : Locals → Heap → Prop) (relation : Locals × Heap → Locals × Heap → Prop) (wellFounded : WellFounded relation) (ready : Locals → Heap → Locals → Heap → Prop) (normal : Locals → Heap → Prop) (returned : Value result → Locals → Heap → Prop) (guardSpec : BlockSpec (fun (locals : Locals) => observe view guard program locals) invariant (fun (x : Locals) (x_1 : Heap) (x_2 : Locals) (x_3 : Heap) => False) fun (start : Locals) (startHeap : Heap) (again : Value Ty.bool) (afterGuard : Locals) (afterGuardHeap : Heap) => if again = true then ready start startHeap afterGuard afterGuardHeap else normal afterGuard afterGuardHeap) (bodySpec : ∀ (start : Locals) (startHeap : Heap), invariant start startHeap → BlockSpec (fun (locals : Locals) => observe view body program locals) (ready start startHeap) (fun (x : Locals) (x_1 : Heap) (finish : Locals) (heap : Heap) => invariant finish heap ∧ relation (finish, heap) (start, startHeap)) fun (x : Locals) (x_1 : Heap) => returned) :
BlockSpec (fun (locals : Locals) => observe view (guard.while body) program locals) invariant (fun (x : Locals) (x_1 : Heap) => normal) fun (x : Locals) (x_1 : Heap) => returned

Compose independent mathematical contracts for a loop guard and body. The guard exposes its actual result and final state. A true result establishes ready, which relates that state to the complete iteration's starting state; a false result establishes the normal postcondition at the guard's actual exit.

The body contract starts at this real intermediate state. Only normal body completion restores the invariant and decreases the well-founded relation; early returns establish their own postcondition. The contracts and conclusion refer to the existing observations, without outcome-tuple or nested-WP work in the mathematical premises.

theorem Complexity.Language.Stmt.observe_while_variant_contract {signatures : List Signature} {Γ : List Ty} {result : Ty} {Locals : Type} (view : Env Γ ≃ Locals) (program : Program signatures) (guard : Stmt signatures Γ Ty.bool) (body : Stmt signatures Γ result) (invariant : Locals → Heap → Prop) (variant : Locals → Heap → ℕ) (ready : Locals → Heap → Locals → Heap → Prop) (normal : Locals → Heap → Prop) (returned : Value result → Locals → Heap → Prop) (guardSpec : BlockSpec (fun (locals : Locals) => observe view guard program locals) invariant (fun (x : Locals) (x_1 : Heap) (x_2 : Locals) (x_3 : Heap) => False) fun (start : Locals) (startHeap : Heap) (again : Value Ty.bool) (afterGuard : Locals) (afterGuardHeap : Heap) => if again = true then ready start startHeap afterGuard afterGuardHeap else normal afterGuard afterGuardHeap) (bodySpec : ∀ (start : Locals) (startHeap : Heap), invariant start startHeap → BlockSpec (fun (locals : Locals) => observe view body program locals) (ready start startHeap) (fun (x : Locals) (x_1 : Heap) (finish : Locals) (heap : Heap) => invariant finish heap ∧ variant finish heap < variant start startHeap) fun (x : Locals) (x_1 : Heap) => returned) :
BlockSpec (fun (locals : Locals) => observe view (guard.while body) program locals) invariant (fun (x : Locals) (x_1 : Heap) => normal) fun (x : Locals) (x_1 : Heap) => returned

Use a natural mathematical variant with the same separate guard and body contracts. Its strict decrease is measured across a complete guard/body round, so an effectful guard cannot silently change the comparison's starting state.