Documentation

Complexity.Language.Eval.Locals.LocalReturn.While

While contracts with local completion #

A continuing iteration supplies a related mathematical state and well-founded progress. An iteration which stores a local result instead proves its exit postcondition: the actual next guard returns false, without requiring an invariant or a decrease for that completed state.

The rules retain the original guard, body, complete locals, heaps and normal source control. A supplied execution of the stopped guard must certify the real false-guard step; no new phase type, evaluator or loop is introduced. LocalReturn.guard_stopped supplies this fact for the existing guarded source fragment. Source faults, guard fallthrough and enclosing function returns are not successful local completion.

theorem Complexity.Language.Stmt.LocalReturn.guard_stopped {signatures : List Signature} {Γ : List Ty} {τ : Ty} {program : Program signatures} (pending : Atom Γ τ.option) (test : Stmt signatures Γ Ty.bool) (state : State Γ) (value : Value τ) (stopped : pending.eval state.locals = some value) :
Exec program (guard pending test) state state (Control.returned false)

A stored local result makes the actual guarded fragment return false, without evaluating the original test or changing the state.

theorem Complexity.Language.TotalWP.while_completion {Model : Type u} {signatures : List Signature} {Γ : List Ty} {τ result : Ty} {program : Program signatures} {guard : Stmt signatures Γ Ty.bool} {body : Stmt signatures Γ result} (pending : Atom Γ τ.option) (stateRel : Model → State Γ → Prop) (invariant : Model → Prop) {relation : Model → Model → Prop} (wellFounded : WellFounded relation) (post : Option (Value τ) → State Γ → Prop) (stoppedGuard : ∀ (state : State Γ) (value : Value τ), pending.eval state.locals = some value → Exec program guard state state (Control.returned false)) (step : ∀ (model : Model) (current : State Γ), invariant model → stateRel model current → TotalWP program guard (fun (x : State Γ) => False) (fun (again : Value Ty.bool) (afterGuard : State Γ) => if again = true then TotalWP program body (fun (afterBody : State Γ) => match pending.eval afterBody.locals with | none => ∃ (next : Model), invariant next ∧ stateRel next afterBody ∧ relation next model | some value => post (some value) afterBody) (fun (x : Value result) (x_1 : State Γ) => False) afterGuard else post (pending.eval afterGuard.locals) afterGuard) current) {model : Model} {entry : State Γ} (initial : invariant model) (represented : stateRel model entry) :
TotalWP program (guard.while body) (fun (finish : State Γ) => post (pending.eval finish.locals) finish) (fun (x : Value result) (x_1 : State Γ) => False) entry

Prove the actual loop using well-founded progress only for continuing iterations. A stored result closes the loop through its real false guard, retaining normal source control and the completed state's actual heap.

theorem Complexity.Language.Stmt.observe_while_completion_contract {Model : Type u} {signatures : List Signature} {Γ : List Ty} {τ result : Ty} {Input Locals : Type} (view : Env Γ ≃ Locals) (program : Program signatures) (guard : Stmt signatures Γ Ty.bool) (body : Stmt signatures Γ result) (pending : Atom Γ τ.option) (stoppedGuard : ∀ (state : State Γ) (value : Value τ), pending.eval state.locals = some value → Exec program guard state state (Control.returned false)) (stateRel : Model → Locals → Heap → Prop) (invariant : Model → Prop) {relation : Model → Model → Prop} (wellFounded : WellFounded relation) (ready : Model → Locals → Heap → Locals → Heap → Prop) (post : Option (Value τ) → Locals → Heap → Prop) (guardSpec : ∀ (model : Model), invariant model → BlockSpec (fun (locals : Locals) => observe view guard program locals) (stateRel model) (fun (x : Locals) (x_1 : Heap) (x_2 : Locals) (x_3 : Heap) => False) fun (start : Locals) (heap : Heap) (again : Value Ty.bool) (afterGuard : Locals) (finish : Heap) => if again = true then ready model start heap afterGuard finish else post (pending.eval (view.symm afterGuard)) afterGuard finish) (bodySpec : ∀ (model : Model) (start : Locals) (heap : Heap), invariant model → stateRel model start heap → BlockSpec (fun (locals : Locals) => observe view body program locals) (ready model start heap) (fun (x : Locals) (x_1 : Heap) (afterBody : Locals) (finish : Heap) => match pending.eval (view.symm afterBody) with | none => ∃ (next : Model), invariant next ∧ stateRel next afterBody finish ∧ relation next model | some value => post (some value) afterBody finish) fun (x : Locals) (x_1 : Heap) (x_2 : Value result) (x_3 : Locals) (x_4 : Heap) => False) (inputLocals : Input → Locals) (model : Model) (initial : invariant model) :
BlockSpec (fun (input : Input) => observe view (guard.while body) program (inputLocals input)) (fun (input : Input) (heap : Heap) => stateRel model (inputLocals input) heap) (fun (x : Input) (x_1 : Heap) (output : Locals) (finish : Heap) => post (pending.eval (view.symm output)) output finish) fun (x : Input) (x_1 : Heap) (x_2 : Value result) (x_3 : Locals) (x_4 : Heap) => False

Compose ordinary-local guard and body contracts for the same loop. The mathematical relation may describe heap-backed values and need not encode the source environment. Only a body with no stored local result must supply a smaller model; a stored result supplies the exit postcondition directly. inputLocals merely selects complete entry coordinates for the existing action.

theorem Complexity.Language.Stmt.observe_while_visible_completion_contract {Model : Type u} {signatures : List Signature} {Γ : List Ty} {τ result : Ty} {Locals Visible : Type} (view : Env Γ ≃ Locals) (program : Program signatures) (guard : Stmt signatures Γ Ty.bool) (body : Stmt signatures Γ result) (pending : Atom Γ τ.option) (stoppedGuard : ∀ (state : State Γ) (value : Value τ), pending.eval state.locals = some value → Exec program guard state state (Control.returned false)) (visible : Locals → Visible) (entry : Visible → Locals) (reconstruct : Locals → Option (Value τ) → Visible → Locals) (visibleEntry : ∀ (start : Visible), visible (entry start) = start) (pendingEntry : ∀ (start : Visible), pending.eval (view.symm (entry start)) = none) (reconstructNone : ∀ (start output : Visible), reconstruct (entry start) none output = entry output) (guardFrame : ∀ (start : Visible) (heap : Heap) (output : Locals) (finish : Heap) (again : Value Ty.bool), observe view guard program (entry start) heap = Part.some ((Control.returned again, output), finish) → entry (visible output) = output) (bodyFrame : ∀ (start : Visible) (heap : Heap) (output : Locals) (finish : Heap), observe view body program (entry start) heap = Part.some ((Control.normal, output), finish) → reconstruct (entry start) (pending.eval (view.symm output)) (visible output) = output) (stateRel : Model → Visible → Heap → Prop) (invariant : Model → Prop) {relation : Model → Model → Prop} (wellFounded : WellFounded relation) (ready : Model → Visible → Heap → Visible → Heap → Prop) (normal : Visible → Heap → Prop) (returned : Value τ → Visible → Heap → Prop) (guardSpec : ∀ (model : Model), invariant model → BlockSpec (fun (start : Visible) => observe view guard program (entry start)) (stateRel model) (fun (x : Visible) (x_1 : Heap) (x_2 : Locals) (x_3 : Heap) => False) fun (start : Visible) (heap : Heap) (again : Value Ty.bool) (output : Locals) (finish : Heap) => if again = true then ready model start heap (visible output) finish else normal (visible output) finish) (bodySpec : ∀ (model : Model) (start : Visible) (heap : Heap), invariant model → stateRel model start heap → BlockSpec (fun (input : Visible) => observe view body program (entry input)) (ready model start heap) (fun (x : Visible) (x_1 : Heap) (output : Locals) (finish : Heap) => match pending.eval (view.symm output) with | none => ∃ (next : Model), invariant next ∧ stateRel next (visible output) finish ∧ relation next model | some value => returned value (visible output) finish) fun (x : Visible) (x_1 : Heap) (x_2 : Value result) (x_3 : Locals) (x_4 : Heap) => False) (model : Model) (initial : invariant model) :
BlockSpec (fun (start : Visible) => observe view (guard.while body) program (entry start)) (stateRel model) (fun (x : Visible) (x_1 : Heap) (output : Locals) (finish : Heap) => match pending.eval (view.symm output) with | none => normal (visible output) finish | some value => returned value (visible output) finish) fun (x : Visible) (x_1 : Heap) (x_2 : Value result) (x_3 : Locals) (x_4 : Heap) => False

Use visible guard and body contracts without exposing compiler completion slots. The supplied frames concern the same actual observations: a guard restores a continuing entry, and a body with no pending result restores the next entry. Completed bodies need neither a next invariant nor a decrease. The original full locals, actual normal control and final heap are retained.