Weakest Precondition Interpretation #
This module defines the weakest precondition interpretation WP of monadic programs
in terms of predicate transformers PredTrans.
An instance WP m Pred EPred determines a function wpTrans : m α → PredTrans Pred EPred α that
interprets a program x : m α as a predicate transformer. The function wp is the
user-facing wrapper: wp x post epost computes the weakest precondition for x to
satisfy normal postcondition post and exception postcondition epost.
Assertion Language Classes #
Assertion and Assertion are both just alias type classes for CompleteLattice.
We introduce them for readability: Assertion Pred marks Pred as the assertion language for
normal postconditions, while Assertion EPred marks EPred as the type of exception
postconditions.
Pre-defined instances #
WP Id Prop EPost.nil— pure computations.WP (StateT σ m) (σ → Pred) EPred— stateful computations.WP (ExceptT ε m) Pred (EPost.cons (ε → Pred) EPred)— computations with exceptions.WP (ReaderT ρ m) (ρ → Pred) EPred— reader computations.WP (Except ε) Prop EPost⟨ε → Prop⟩— concrete exception type.WP (EStateM ε σ) (σ → Prop) (ε → σ → Prop)— concrete error-state monad.
An assertion type is equipped with a CompleteLattice structure,
used as the carrier for pre- and postconditions.
Instances
An assertion type is a chain-complete partial order.
Instances For
WP Typeclass #
The WP typeclass defines weakest precondition semantics for monads.
A WP m Pred EPred instance provides a monad morphism wpTrans : m α → PredTrans Pred EPred α
satisfying:
wp_trans_pure:pure xis at least as strong as its predicate transformer.wp_trans_bind: sequential composition is sound.wp_trans_monotone: the transformer is monotone in both postconditions.
Weakest precondition monad: a monad m with a sound interpretation as predicate
transformers over assertion language Pred with exception postconditions EPred.
- seq_assoc {α β γ : Type u} (x : m α) (g : m (α → β)) (h : m (β → γ)) : h <*> (g <*> x) = Function.comp <$> h <*> g <*> x
The weakest precondition transformer for a monadic program.
Soundness of
pure: the postcondition applied toximplies the WP ofpure x.- wp_trans_bind {α β : Type u} (x : m α) (f : α → m β) : Lean.Order.PartialOrder.rel (do let x ← wpTrans x wpTrans (f x)) (wpTrans (x >>= f))
Soundness of
bind: composing WPs is at least as strong as the WP of>>=. Monotonicity: weaker postconditions yield weaker preconditions.
Instances
Compute the weakest precondition of x for normal postcondition post and
exception postcondition epost.
Instances For
Derived WP Lemmas #
One-directional consequences of the WP axioms for pure, bind, monotonicity,
and weakening.
Derived Theorems for map and seq #
One direction of the derived theorems wp_map and wp_seq. The full bidirectional
equality from Std.Do cannot be proven with our current axioms since wp_bind only
gives one direction (⊑).
WP Instances #
ExceptT lifts a WP instance by adding an exception postcondition layer.
Type Alias Instances #
WP instances for concrete monads that are type aliases for transformer stacks.
Adequacy Lemmas #
These lemmas bridge wp reasoning to concrete program properties. Each one says:
if wp prog ... holds, then a property P holds of the program's result.