From d46c0c7ed6dfc0faa7b5fa6515f7e640f2a2ec76 Mon Sep 17 00:00:00 2001 From: David Ledvinka Date: Fri, 20 Nov 2026 22:52:03 +0000 Subject: [PATCH 1/4] Add weakest preconditions for measurable space monads --- RandomDo.lean | 1 + RandomDo/WP/MeasurableSpaceMonad.lean | 153 ++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 RandomDo/WP/MeasurableSpaceMonad.lean diff --git a/RandomDo.lean b/RandomDo.lean index 44782b1..7adf62f 100644 --- a/RandomDo.lean +++ b/RandomDo.lean @@ -26,3 +26,4 @@ public import RandomDo.Tactic.IsMarkov.Deriving public import RandomDo.Tactic.IsMarkov.Elab public import RandomDo.Tactic.IsMarkov.ForInStep public import RandomDo.Tactic.IsMarkov.Lemmas +public import RandomDo.WP.MeasurableSpaceMonad diff --git a/RandomDo/WP/MeasurableSpaceMonad.lean b/RandomDo/WP/MeasurableSpaceMonad.lean new file mode 100644 index 0000000..8e26ab0 --- /dev/null +++ b/RandomDo/WP/MeasurableSpaceMonad.lean @@ -0,0 +1,153 @@ +/- +Copyright (c) 2026 David Ledvinka. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: David Ledvinka, Gaëtan Serré +-/ +module + +public import RandomDo.MeasurableSpaceMonad.Defs +public import Std.WP.Triple.SpecLemmas +public import Std.WP.Gadget.Assert + +/-! +# Weakest-precondition interpretations of measurable space monads + +This file defines the `WPMeasurableSpaceMonad` class which provides an API for defining and +using (monadic) weakest preconditions for measurable space monads. It is the analogue +of core Lean's `WPMonad`. +-/ + +@[expose] public section + +open Lean.Order MeasureTheory Std.WP + +universe u v w w' + +namespace Std.WP + +/-- Weakest precondition measurable space monad: A measurable space monad `m` whose weakest +precondition interpretation is sound for `mPure` and `mBind`. +Analogue of core Lean's `Std.WP.WPMonad`. -/ +class WPMeasurableSpaceMonad (m : (α : Type u) → [MeasurableSpace α] → Type v) + (Pred : outParam (Type w)) (EPred : outParam (Type w')) + [MeasurableSpaceMonad m] [∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] + [Assertion Pred] [MeasurableSpace Pred] [Assertion EPred] + extends LawfulMeasurableSpaceMonad m where + /-- The weakest precondition interpretation of `m` at every measurable result type. -/ + toWP : ∀ α, [MeasurableSpace α] → WP (m α) α Pred EPred + /-- A measurable postcondition gives a measurable weakest precondition. -/ + measurable_wp {α : Type u} [MeasurableSpace α] {post : α → Pred} + (hpost : Measurable post) (epost : EPred) : + Measurable (fun x : m α ↦ (toWP α).wp x post epost) + /-- Soundness of `mPure`: the postcondition applied to `x` implies the weakest + precondition of `mPure x`. -/ + mPure_le_wp_mPure {α : Type u} [MeasurableSpace α] + (x : α) (post : α → Pred) (epost : EPred) (hpost : Measurable post) : + post x ⊑ (toWP α).wp (mPure (f := m) x) post epost + /-- Soundness of `mBind`: composing weakest preconditions is at least as strong as the weakest + precondition of `>>=ₘ`. -/ + mBind_le_wp_mBind {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] + (x : m α) (f : α → m β) (post : β → Pred) (epost : EPred) + (hf : Measurable f) (hpost : Measurable post) : + (toWP α).wp x (fun a ↦ (toWP β).wp (f a) post epost) epost ⊑ + (toWP β).wp (x >>=ₘ f) post epost + +attribute [fun_prop] WPMeasurableSpaceMonad.measurable_wp + +end Std.WP + +variable {m : (α : Type u) → [MeasurableSpace α] → Type v} + [MeasurableSpaceMonad m] [∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] + {Pred : Type w} {EPred : Type w'} [Assertion Pred] [MeasurableSpace Pred] [Assertion EPred] + {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] + {pre : Pred} {epost : EPred} + +namespace Std.WP + +/-- A monadic `WP` interpretation is sourced from the monad's `WPMeasurableSpaceMonad` instance. +Low priority so a program type with a bespoke `WP` (e.g. a non-monadic one) is preferred. -/ +instance (priority := low) WPMeasurableSpaceMonad.instWP + [inst : WPMeasurableSpaceMonad m Pred EPred] : WP (m α) α Pred EPred := + inst.toWP α + +variable [WPMeasurableSpaceMonad m Pred EPred] + +theorem WPMeasurableSpaceMonad.mMap_le_wp_mMap (f : α → β) (x : m α) (hf : Measurable f) : + ∀ post epost, Measurable post → + wp x (fun a => post (f a)) epost ⊑ wp (f <$>ₘ x) post epost := by + intro post epost hpost + rw [← mBind_mPure_comp hf] + apply PartialOrder.rel_trans; rotate_left + · exact mBind_le_wp_mBind x (mPure <| f ·) post epost (by fun_prop) hpost + apply WP.wp_consequence + intro a; exact mPure_le_wp_mPure (f a) post epost hpost + +namespace Triple + +theorem mPure {post : α → Pred} (a : α) (h : pre ⊑ post a) (hpost : Measurable post) : + Triple (MeasureTheory.mPure (f := m) a) pre post epost := + ⟨PartialOrder.rel_trans h (WPMeasurableSpaceMonad.mPure_le_wp_mPure a post epost hpost)⟩ + +theorem mBind {post : β → Pred} (x : m α) (f : α → m β) (mid : α → Pred) + (hx : Triple x pre mid epost) (hf : ∀ a, Triple (f a) (mid a) post epost) + (hf_meas : Measurable f) (hpost : Measurable post) : + Triple (x >>=ₘ f) pre post epost := + ⟨PartialOrder.rel_trans hx.le_wp + (PartialOrder.rel_trans + (WP.wp_consequence x mid (fun a => wp (f a) post epost) epost (fun a => (hf a).le_wp)) + (WPMeasurableSpaceMonad.mBind_le_wp_mBind x f post epost hf_meas hpost))⟩ + +theorem mMap {post : β → Pred} (f : α → β) (x : m α) + (h : Triple x pre (fun a => post (f a)) epost) + (hf : Measurable f) (hpost : Measurable post) : + Triple (f <$>ₘ x) pre post epost := + ⟨PartialOrder.rel_trans h.le_wp + (WPMeasurableSpaceMonad.mMap_le_wp_mMap f x hf post epost hpost)⟩ + +end Triple + +@[spec] theorem Spec.mPure (a : α) {post : α → Pred} (hpost : Measurable post) : + Triple (MeasureTheory.mPure (f := m) a) (post a) post epost := + Triple.mPure a PartialOrder.rel_refl hpost + +@[spec] theorem Spec.mBind (x : m α) (f : α → m β) {post : β → Pred} + (hf : Measurable f) (hpost : Measurable post) : + Triple (x >>=ₘ f) (wp x (fun a => binderNameHint a f (wp (f a) post epost)) epost) + post epost := + Triple.mBind x f (fun a => wp (f a) post epost) + (Triple.intro PartialOrder.rel_refl) (fun _ => Triple.intro PartialOrder.rel_refl) hf hpost + +@[spec] theorem Spec.mMap (f : α → β) (x : m α) {post : β → Pred} + (hf : Measurable f) (hpost : Measurable post) : + Triple (f <$>ₘ x) (wp x (fun a => post (f a)) epost) post epost := + Triple.mMap f x (Triple.intro PartialOrder.rel_refl) hf hpost + +end Std.WP + +section Assert + +namespace MeasureTheory.WP + +set_option linter.unusedVariables false in +/-- A no-op computation used as a verification gadget to inject assertions into the program. + +The `as` parameter is the assertion to be checked. At runtime, `assertGadget` is simply +`mPure ⟨⟩`. Analogue of core's `Std.WP.Gadget.assertGadget`. -/ +@[nolint unusedArguments] +def assertGadget [WPMeasurableSpaceMonad m Pred EPred] (as : Pred) : m PUnit := mPure ⟨⟩ + +variable [WPMeasurableSpaceMonad m Pred EPred] + +@[fun_prop] theorem measurable_assertGadget {α : Type*} [MeasurableSpace α] (as : α → Pred) : + Measurable (fun a => assertGadget (m := m) (as a)) := measurable_const + +@[spec] theorem Spec.assertGadget (as : Pred) [Heyting Pred] + {post : PUnit → Pred} : + Triple (WP.assertGadget (m := m) as) (as ⊓ (as ⇨ post ⟨⟩)) post epost := by + simpa [WP.assertGadget] using + (Triple.mPure (m := m) (pre := as ⊓ (as ⇨ post ⟨⟩)) (post := post) (epost := epost) + ⟨⟩ meet_himp_le (by fun_prop)) + +end MeasureTheory.WP + +end Assert From de256efd6b8bbb18eb27e5c58ef9677bd611327f Mon Sep 17 00:00:00 2001 From: David Ledvinka Date: Sat, 21 Nov 2026 14:24:57 +0000 Subject: [PATCH 2/4] Include the MId weakest-precondition instance with the monad class --- RandomDo/WP/MeasurableSpaceMonad.lean | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/RandomDo/WP/MeasurableSpaceMonad.lean b/RandomDo/WP/MeasurableSpaceMonad.lean index 8e26ab0..6012a69 100644 --- a/RandomDo/WP/MeasurableSpaceMonad.lean +++ b/RandomDo/WP/MeasurableSpaceMonad.lean @@ -7,6 +7,7 @@ module public import RandomDo.MeasurableSpaceMonad.Defs public import Std.WP.Triple.SpecLemmas +public import Std.WP.Monad.Instances public import Std.WP.Gadget.Assert /-! @@ -54,6 +55,15 @@ class WPMeasurableSpaceMonad (m : (α : Type u) → [MeasurableSpace α] → Typ attribute [fun_prop] WPMeasurableSpaceMonad.measurable_wp +-- Reuse core's `Id` weakest-precondition interpretation for `MId`. +#check Std.WP.Id.instWPMonad + +instance : WPMeasurableSpaceMonad MId Prop EStack⟨⟩ where + toWP _ := inferInstanceAs (WP (Id _) _ Prop EStack⟨⟩) + measurable_wp hpost _ := hpost + mPure_le_wp_mPure _ _ _ _ := PartialOrder.rel_refl + mBind_le_wp_mBind _ _ _ _ _ _ := PartialOrder.rel_refl + end Std.WP variable {m : (α : Type u) → [MeasurableSpace α] → Type v} From d89e7d5f4549e60b544da5d77b2601860bb74158 Mon Sep 17 00:00:00 2001 From: David Ledvinka Date: Sat, 21 Nov 2026 14:32:39 +0000 Subject: [PATCH 3/4] Clean up WP namespace scope and remove the review check --- RandomDo/WP/MeasurableSpaceMonad.lean | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/RandomDo/WP/MeasurableSpaceMonad.lean b/RandomDo/WP/MeasurableSpaceMonad.lean index 6012a69..3ba7bb4 100644 --- a/RandomDo/WP/MeasurableSpaceMonad.lean +++ b/RandomDo/WP/MeasurableSpaceMonad.lean @@ -24,6 +24,12 @@ open Lean.Order MeasureTheory Std.WP universe u v w w' +variable {m : (α : Type u) → [MeasurableSpace α] → Type v} + [MeasurableSpaceMonad m] [∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] + {Pred : Type w} {EPred : Type w'} [Assertion Pred] [MeasurableSpace Pred] [Assertion EPred] + {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] + {pre : Pred} {epost : EPred} + namespace Std.WP /-- Weakest precondition measurable space monad: A measurable space monad `m` whose weakest @@ -55,25 +61,12 @@ class WPMeasurableSpaceMonad (m : (α : Type u) → [MeasurableSpace α] → Typ attribute [fun_prop] WPMeasurableSpaceMonad.measurable_wp --- Reuse core's `Id` weakest-precondition interpretation for `MId`. -#check Std.WP.Id.instWPMonad - instance : WPMeasurableSpaceMonad MId Prop EStack⟨⟩ where toWP _ := inferInstanceAs (WP (Id _) _ Prop EStack⟨⟩) measurable_wp hpost _ := hpost mPure_le_wp_mPure _ _ _ _ := PartialOrder.rel_refl mBind_le_wp_mBind _ _ _ _ _ _ := PartialOrder.rel_refl -end Std.WP - -variable {m : (α : Type u) → [MeasurableSpace α] → Type v} - [MeasurableSpaceMonad m] [∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] - {Pred : Type w} {EPred : Type w'} [Assertion Pred] [MeasurableSpace Pred] [Assertion EPred] - {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] - {pre : Pred} {epost : EPred} - -namespace Std.WP - /-- A monadic `WP` interpretation is sourced from the monad's `WPMeasurableSpaceMonad` instance. Low priority so a program type with a bespoke `WP` (e.g. a non-monadic one) is preferred. -/ instance (priority := low) WPMeasurableSpaceMonad.instWP From 0de20da8ba5d0345fc0869ae9f364eb44d195838 Mon Sep 17 00:00:00 2001 From: David Ledvinka Date: Sat, 21 Nov 2026 14:46:47 +0000 Subject: [PATCH 4/4] Place measurable WP declarations in their owning namespaces --- RandomDo/WP/MeasurableSpaceMonad.lean | 29 +++++++++++++++++---------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/RandomDo/WP/MeasurableSpaceMonad.lean b/RandomDo/WP/MeasurableSpaceMonad.lean index 3ba7bb4..ceaa837 100644 --- a/RandomDo/WP/MeasurableSpaceMonad.lean +++ b/RandomDo/WP/MeasurableSpaceMonad.lean @@ -30,7 +30,7 @@ variable {m : (α : Type u) → [MeasurableSpace α] → Type v} {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] {pre : Pred} {epost : EPred} -namespace Std.WP +namespace MeasureTheory /-- Weakest precondition measurable space monad: A measurable space monad `m` whose weakest precondition interpretation is sound for `mPure` and `mBind`. @@ -73,9 +73,8 @@ instance (priority := low) WPMeasurableSpaceMonad.instWP [inst : WPMeasurableSpaceMonad m Pred EPred] : WP (m α) α Pred EPred := inst.toWP α -variable [WPMeasurableSpaceMonad m Pred EPred] - -theorem WPMeasurableSpaceMonad.mMap_le_wp_mMap (f : α → β) (x : m α) (hf : Measurable f) : +theorem WPMeasurableSpaceMonad.mMap_le_wp_mMap [WPMeasurableSpaceMonad m Pred EPred] + (f : α → β) (x : m α) (hf : Measurable f) : ∀ post epost, Measurable post → wp x (fun a => post (f a)) epost ⊑ wp (f <$>ₘ x) post epost := by intro post epost hpost @@ -85,7 +84,11 @@ theorem WPMeasurableSpaceMonad.mMap_le_wp_mMap (f : α → β) (x : m α) (hf : apply WP.wp_consequence intro a; exact mPure_le_wp_mPure (f a) post epost hpost -namespace Triple +end MeasureTheory + +namespace Std.WP.Triple + +variable [WPMeasurableSpaceMonad m Pred EPred] theorem mPure {post : α → Pred} (a : α) (h : pre ⊑ post a) (hpost : Measurable post) : Triple (MeasureTheory.mPure (f := m) a) pre post epost := @@ -107,7 +110,13 @@ theorem mMap {post : β → Pred} (f : α → β) (x : m α) ⟨PartialOrder.rel_trans h.le_wp (WPMeasurableSpaceMonad.mMap_le_wp_mMap f x hf post epost hpost)⟩ -end Triple +end Std.WP.Triple + +namespace MeasureTheory.WP + +section MonadSpec + +variable [WPMeasurableSpaceMonad m Pred EPred] @[spec] theorem Spec.mPure (a : α) {post : α → Pred} (hpost : Measurable post) : Triple (MeasureTheory.mPure (f := m) a) (post a) post epost := @@ -125,12 +134,10 @@ end Triple Triple (f <$>ₘ x) (wp x (fun a => post (f a)) epost) post epost := Triple.mMap f x (Triple.intro PartialOrder.rel_refl) hf hpost -end Std.WP +end MonadSpec section Assert -namespace MeasureTheory.WP - set_option linter.unusedVariables false in /-- A no-op computation used as a verification gadget to inject assertions into the program. @@ -151,6 +158,6 @@ variable [WPMeasurableSpaceMonad m Pred EPred] (Triple.mPure (m := m) (pre := as ⊓ (as ⇨ post ⟨⟩)) (post := post) (epost := epost) ⟨⟩ meet_himp_le (by fun_prop)) -end MeasureTheory.WP - end Assert + +end MeasureTheory.WP