| /- | |
| Copyright (c) 2026 Lean FRO LLC. All rights reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Sebastian Graf | |
| -/ | |
| module | |
| prelude | |
| public import Std.WP.Frame | |
| public import Std.WP.Monad.Basic | |
| universe u v w z t | |
| @[expose] public section | |
| set_option linter.missingDocs true | |
| /-! | |
| # Framing for a monadic `wp` | |
| `WPMonad.withFrameClosure` reinterprets a `WPMonad` through the | |
| `Lean.Order.PredTrans.frameClosure` of its base weakest precondition, so that every program frames | |
| every resource. | |
| -/ | |
| open Lean.Order Std.WP | |
| namespace Std.WP | |
| /-- Reinterpret a `WPMonad m` so its weakest precondition is the `frameClosure` of the | |
| base wp over a family of supremum-preserving resource operators `op r` that act by `comp` with | |
| unit `e`. | |
| Every program then frames every resource (`WP.frames_of_frameClosure`). | |
| A separation logic depends on this property: every frame `op r` passes through the `wp` of every | |
| program in every state. A caller of a spec picks a frame and applies the frame rule for that | |
| frame. -/ | |
| @[instance_reducible] noncomputable def WPMonad.withFrameClosure {m : Type v → Type w} [Monad m] | |
| {P : Type u} {E : Type z} [Assertion P] [Assertion E] | |
| {R : Type t} (op : R → P → P) {opE : R → E → E} [FrameOp op E opE] {comp : R → R → R} {e : R} | |
| (hact : ∀ r r' a, op (comp r r') a = op r (op r' a)) | |
| (hactE : ∀ r r' E', opE (comp r r') E' = opE r (opE r' E')) | |
| (hunit : ∀ a, op e a = a) (hunitE : ∀ E', opE e E' = E') | |
| (base : WPMonad m P E) : WPMonad m P E where | |
| toLawfulMonad := base.toLawfulMonad | |
| toWP α := WP.withFrameClosure op (base.toWP α) | |
| pure_le_wp_pure x post E' := by | |
| show post x ⊑ (((base.toWP _).trans (pure x)).frameClosure op).apply post E' | |
| refine (PredTrans.le_frameClosure_iff op _).mpr fun r => ?_ | |
| exact base.pure_le_wp_pure x (fun a => op r (post a)) (opE r E') | |
| bind_le_wp_bind x f post E' := by | |
| show (((base.toWP _).trans x).frameClosure op).apply | |
| (fun a => (((base.toWP _).trans (f a)).frameClosure op).apply post E') E' | |
| ⊑ (((base.toWP _).trans (x >>= f)).frameClosure op).apply post E' | |
| refine (PredTrans.le_frameClosure_iff op _).mpr fun r => ?_ | |
| refine PartialOrder.rel_trans (PredTrans.frameClosure_frames op comp hact hactE _ _ _ r) ?_ | |
| refine PartialOrder.rel_trans (PredTrans.frameClosure_le op e hunit hunitE _ _ _) ?_ | |
| refine PartialOrder.rel_trans ?_ | |
| (base.bind_le_wp_bind x f (fun a => op r (post a)) (opE r E')) | |
| refine WP.wp_monotone_post fun a => ?_ | |
| exact PartialOrder.rel_trans (PredTrans.frameClosure_frames op comp hact hactE _ _ _ r) | |
| (PredTrans.frameClosure_le op e hunit hunitE _ _ _) | |
| end Std.WP |