Documentation

LeanPool.OrderMaintenance.Machine.Logic

Program logic #

A weakest-precondition calculus for exec, with cost. Nothing here is trusted: every rule is a theorem about the interpreter.

wp c Q s says: from s, c terminates normally (no fail, no machine error), and Q holds of the final state and the number of steps taken. The rules below push wp through the syntax. For a concrete program, simp only [wp_simps] turns the goal into a pure verification condition; each loop needs an invariant (wp_loop).

o terminated normally and Q holds of its final state and cost.

Equations
Instances For
    def OrderMaintenance.Machine.wp (c : Stmt) (Q : State → Nat → Prop) (s : State) :

    Normal-termination weakest precondition for a state-and-cost postcondition.

    Equations
    Instances For
      def OrderMaintenance.Machine.Triple (P : State → Prop) (c : Stmt) (Q : State → Prop) (k : Nat) :

      Hoare triple with a cost bound: from every state satisfying P, c terminates normally in at most k steps, in a state satisfying Q.

      Equations
      Instances For

        Hoare triple with a cost bound: from every state satisfying P, c terminates normally in at most k steps, in a state satisfying Q.

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

          Outcomes #

          theorem OrderMaintenance.Machine.Out.Ok.mono {o : Out} {Q R : State → Nat → Prop} (h : ∀ (s : State) (n : Nat), Q s n → R s n) (ho : o.Ok Q) :
          o.Ok R
          @[simp]
          theorem OrderMaintenance.Machine.Out.ok_mk {s : State} {n : Nat} {st : Status} {Q : State → Nat → Prop} :
          { st := s, cost := n, status := st }.Ok Q ↔ st = Status.ok ∧ Q s n
          theorem OrderMaintenance.Machine.Out.ok_bind {o : Out} {g : State → Out} {Q : State → Nat → Prop} :
          (o.bind g).Ok Q ↔ o.Ok fun (s : State) (n : Nat) => (g s).Ok fun (s' : State) (m : Nat) => Q s' (n + m)

          One rule per construct #

          theorem OrderMaintenance.Machine.wp_mono {c : Stmt} {Q R : State → Nat → Prop} {s : State} (h : ∀ (s : State) (n : Nat), Q s n → R s n) (hc : wp c Q s) :
          wp c R s
          theorem OrderMaintenance.Machine.wp_skip {Q : State → Nat → Prop} {s : State} :
          wp Stmt.skip Q s ↔ Q s 1
          theorem OrderMaintenance.Machine.wp_assign {x : Reg} {e : Expr} {Q : State → Nat → Prop} {s : State} :
          wp (x ::= e) Q s ↔ ∃ (v : Nat), Expr.eval s e = some v ∧ Q ((s.bump (Expr.maxv s e)).setLoc x v) (1 + e.cost)
          theorem OrderMaintenance.Machine.wp_store {a : Arr} {i e : Expr} {Q : State → Nat → Prop} {s : State} :
          wp (Stmt.store a i e) Q s ↔ ∃ (j : Nat), ∃ (v : Nat), Expr.eval s i = some j ∧ Expr.eval s e = some v ∧ j < s.cap a ∧ Q (((s.bump (Expr.maxv s i)).bump (Expr.maxv s e)).write a j v) (1 + i.cost + e.cost)
          theorem OrderMaintenance.Machine.wp_alloc {a : Arr} {n : Expr} {Q : State → Nat → Prop} {s : State} :
          wp (Stmt.alloc a n) Q s ↔ ∃ (k : Nat), Expr.eval s n = some k ∧ Q ((s.bump (Expr.maxv s n)).alloc a k) (1 + n.cost)
          theorem OrderMaintenance.Machine.wp_seq {c d : Stmt} {Q : State → Nat → Prop} {s : State} :
          wp (c;; d) Q s ↔ wp c (fun (s' : State) (n : Nat) => wp d (fun (s'' : State) (m : Nat) => Q s'' (n + m)) s') s
          theorem OrderMaintenance.Machine.wp_ite {c : Expr} {t f : Stmt} {Q : State → Nat → Prop} {s : State} :
          wp (Stmt.ite c t f) Q s ↔ ∃ (v : Nat), Expr.eval s c = some v ∧ wp (if v ≠ 0 then t else f) (fun (s' : State) (m : Nat) => Q s' (1 + c.cost + m)) (s.bump (Expr.maxv s c))
          theorem OrderMaintenance.Machine.wp_call {nm : String} {ps rs ds : List Reg} {b : Stmt} {args : List Expr} {Q : State → Nat → Prop} {s : State} :
          wp (Stmt.call nm ps b rs args ds) Q s ↔ ∃ (vs : List Nat), evalList s args = some vs ∧ wp b (fun (s' : State) (m : Nat) => Q (s'.leave s.loc rs ds) (1 + costList args + ds.length + m)) ((s.bump (maxvList s args)).enter ps vs)
          theorem OrderMaintenance.Machine.wp_catch_ok {c h : Stmt} {Q : State → Nat → Prop} {s : State} (hc : wp c (fun (s' : State) (n : Nat) => Q s' (1 + n)) s) :
          wp (c.catch h) Q s

          A catch whose body terminates normally behaves like the body, plus one step.

          theorem OrderMaintenance.Machine.wp_catch_fail {c h : Stmt} {Q : State → Nat → Prop} {s : State} (hc : (exec c s).status = Status.fail) (hh : wp h (fun (s' : State) (m : Nat) => Q s' (1 + (exec c s).cost + m)) (exec c s).st) :
          wp (c.catch h) Q s

          A catch whose body fails runs the handler from the state the body reached.

          Loops #

          theorem OrderMaintenance.Machine.runFor_inv (f : State → Out) (x : Reg) (I : Nat → State → Prop) (kb N : Nat) (hb : ∀ (k : Nat), k < N → ∀ (s : State), I k s → (f (s.setLoc x k)).Ok fun (s' : State) (m : Nat) => I (k + 1) s' ∧ m ≤ kb) (n k : Nat) (s : State) :
          k + n = N → I k s → (runFor f x k n s).Ok fun (s' : State) (c : Nat) => I N s' ∧ c ≤ n * (1 + kb)
          theorem OrderMaintenance.Machine.wp_loop {x : Reg} {n : Expr} {b : Stmt} {Q : State → Nat → Prop} {s : State} (N : Nat) (I : Nat → State → Prop) (kb : Nat) (hn : Expr.eval s n = some N) (h0 : I 0 (s.bump (Expr.maxv s n))) (hb : ∀ (k : Nat), k < N → ∀ (s : State), I k s → wp b (fun (s' : State) (m : Nat) => I (k + 1) s' ∧ m ≤ kb) (s.setLoc x k)) (hQ : ∀ (s' : State) (c : Nat), I N s' → c ≤ 1 + n.cost + N * (1 + kb) → Q s' c) :
          wp (Stmt.loop x n b) Q s

          The loop rule. N is the value of the bound, I k the invariant before iteration k, and kb a bound on the cost of one execution of the body.

          Triples #

          theorem OrderMaintenance.Machine.Triple.conseq {P P' Q Q' : State → Prop} {c : Stmt} {k k' : Nat} (h : ⦃P⦄ c ⦃Q⦄≤ k) (hP : ∀ (s : State), P' s → P s) (hQ : ∀ (s : State), Q s → Q' s) (hk : k ≤ k') :
          ⦃P'⦄ c ⦃Q'⦄≤ k'
          theorem OrderMaintenance.Machine.Triple.seq {P Q R : State → Prop} {c d : Stmt} {k₁ k₂ : Nat} (h₁ : ⦃P⦄ c ⦃R⦄≤ k₁) (h₂ : ⦃R⦄ d ⦃Q⦄≤ k₂) :
          ⦃P⦄ c;; d ⦃Q⦄≤ k₁ + k₂
          theorem OrderMaintenance.Machine.Triple.wp {P Q : State → Prop} {c : Stmt} {k : Nat} {R : State → Nat → Prop} {s : State} (h : ⦃P⦄ c ⦃Q⦄≤ k) (hs : P s) (hR : ∀ (s' : State) (n : Nat), Q s' → n ≤ k → R s' n) :

          Use a triple inside a wp goal.

          Simp sets #