Skip to content

Commit d36ca45

Browse files
authored
Merge pull request #21 from LeanMachineLearning/codex/split-measurable-space-monad
Split MeasurableSpaceMonad into Defs and ForIn
2 parents 0fa0e95 + ff82ff5 commit d36ca45

8 files changed

Lines changed: 128 additions & 65 deletions

File tree

‎RandomDo.lean‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ module -- shake: keep-all --deprecated_module: ignore
22

33
public import RandomDo.ForMathlib.MeasureTheory.MeasurableSpace.Embedding
44
public import RandomDo.Measurable
5+
public import RandomDo.MeasurableSpaceMonad.Defs
6+
public import RandomDo.MeasurableSpaceMonad.ForIn
57
public import RandomDo.Monad.ForInInstances
68
public import RandomDo.Monad.Instances
7-
public import RandomDo.Monad.MeasurableSpace
89
public import RandomDo.Monad.Notation
910
public import RandomDo.NumLean.Binomial
1011
public import RandomDo.NumLean.Distributions
Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Authors: David Ledvinka
55
-/
66
module
77

8-
public import Mathlib.Control.Random
9-
public import Mathlib.MeasureTheory.Measure.GiryMonad
8+
public import Mathlib.MeasureTheory.MeasurableSpace.Constructions
9+
public import Mathlib.Tactic.FunProp
1010

1111
/-!
1212
# Measurable space monads
@@ -26,12 +26,10 @@ instance.
2626
`MeasurableSpaceFunctor` satisfies the laws of a functor on measurable spaces.
2727
* `LawfulMeasurableSpaceMonad`: A `Prop`-valued typeclass which states that a given
2828
`MeasurableSpaceMonad` satisfies the laws of a monad on measurable spaces.
29-
* `MeasurableSpaceForIn`: **TODO**
30-
* `MeasurableSpaceForIn'`: **TODO**
3129
3230
## Notation
3331
34-
The following notation is available:
32+
The following notation is available in the `MeasureTheory` scope:
3533
3634
* `f <$>ₘ x`: `MeasurableSpaceFunctor.mMap f x`.
3735
* `x >>=ₘ f`: `MeasurableSpaceBind.mBind x f`.
@@ -55,9 +53,9 @@ see R. J. Aumann, [*Borel structures for function spaces*](https://doi.org/10.12
5553

5654
@[expose] public section
5755

58-
open Function
56+
namespace MeasureTheory
5957

60-
section
58+
open Function
6159

6260
universe u v
6361

@@ -97,7 +95,7 @@ class MeasurableSpaceFunctor (f : (α : Type u) → [MeasurableSpace α] → Typ
9795
mMapConst {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] : α → f β → f α :=
9896
mMap ∘ (const _)
9997

100-
@[inherit_doc] infixr:100 " <$>ₘ " => MeasurableSpaceFunctor.mMap
98+
@[inherit_doc] scoped infixr:100 " <$>ₘ " => MeasurableSpaceFunctor.mMap
10199

102100
/--
103101
The `mPure` function is overloaded via `MeasurableSpacePure` instances.
@@ -123,7 +121,7 @@ class MeasurableSpaceBind (m : (α : Type u) → [MeasurableSpace α] → Type v
123121
-/
124122
mBind {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] : m α → (α → m β) → m β
125123

126-
@[inherit_doc] infixl:55 " >>=ₘ " => MeasurableSpaceBind.mBind
124+
@[inherit_doc] scoped infixl:55 " >>=ₘ " => MeasurableSpaceBind.mBind
127125

128126
/--
129127
A monad on types with a `MeasurableSpace` instance.
@@ -232,8 +230,8 @@ class LawfulMeasurableSpaceMonad
232230
open LawfulMeasurableSpaceMonad
233231

234232
attribute [fun_prop] measurable_mPure measurable_mBind
235-
attribute [simp] pure_bind bind_assoc bind_pure_comp
236-
attribute [grind <=] pure_bind
233+
attribute [simp] mPure_mBind mBind_assoc mBind_mPure_comp
234+
attribute [grind <=] mPure_mBind
237235

238236
variable {m : (α : Type u) → [MeasurableSpace α] → Type v} [MeasurableSpaceMonad m]
239237
[∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] [LawfulMeasurableSpaceMonad m]
@@ -263,50 +261,8 @@ theorem mBind_mPure_unit {x : m PUnit} : (x >>=ₘ fun _ => mPure ⟨⟩) = x :=
263261
{g : β → m γ} (hg : Measurable g) :
264262
((f <$>ₘ x) >>=ₘ fun b => g b) = (x >>=ₘ fun a => g (f a)) := by
265263
rw [← mBind_mPure_comp hf]
266-
simp (disch := fun_prop) [mBind_assoc, mPure_mBind]
264+
simp (disch := fun_prop) only [mBind_assoc, mPure_mBind]
267265

268266
end Lawful
269267

270-
end
271-
272-
section MeasurableSpaceFor
273-
274-
universe uρ uα u v
275-
276-
variable {α : Type u} [mα : MeasurableSpace α] (m : (α : Type u) → [MeasurableSpace α] → Type v)
277-
{m' : (α : Type u) → Type v}
278-
279-
instance instMeasurableSpace {β : Type u} [mβ : MeasurableSpace β] :
280-
MeasurableSpace (ForInStep β) := mβ.map ForInStep.yield ⊓ mβ.map ForInStep.done
281-
282-
/--
283-
Monadic iteration in `rdo`-blocks, using the `for x in xs` notation.
284-
-/
285-
class MeasurableSpaceForIn (ρ : Type uρ) (α : outParam (Type uα)) where
286-
/--
287-
Monadically iterates over the contents of a collection `xs`, with a local state `b` and the
288-
possibility of early termination.
289-
-/
290-
forIn {β : Type u} [MeasurableSpace β] (xs : ρ) (b : β)
291-
(f : α → β → m (ForInStep β)) : m β
292-
293-
/--
294-
Monadic iteration in `rdo`-blocks with a membership proof, using the `for h : x in xs` notation.
295-
-/
296-
class MeasurableSpaceForIn' (ρ : Type uρ) (α : outParam (Type uα))
297-
(d : outParam (Membership α ρ)) where
298-
/--
299-
Monadically iterates over the contents of a collection `xs`, with a local state `b` and the
300-
possibility of early termination. At each iteration, the body of the loop is provided with a proof
301-
that the current element is in the collection.
302-
-/
303-
forIn' {β : Type u} [MeasurableSpace β] (xs : ρ) (b : β)
304-
(f : (a : α) → a ∈ xs → β → m (ForInStep β)) : m β
305-
306-
@[simps]
307-
instance (priority := 500) instMeasurableSpaceForInOfForIn'
308-
{ρ : Type uρ} {α : Type uα} {d : Membership α ρ} [MeasurableSpaceForIn' m ρ α d] :
309-
MeasurableSpaceForIn m ρ α where
310-
forIn x b f := MeasurableSpaceForIn'.forIn' x b fun a _ s => f a s
311-
312-
end MeasurableSpaceFor
268+
end MeasureTheory
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/-
2+
Copyright (c) 2026 David Ledvinka. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: David Ledvinka
5+
-/
6+
module
7+
8+
public import RandomDo.MeasurableSpaceMonad.Defs
9+
10+
/-!
11+
# Iteration in measurable space monads
12+
13+
This file defines the typeclasses `MeasurableSpaceForIn` and `MeasurableSpaceForIn'`, the
14+
`MeasurableSpaceMonad` analogues of Lean core's `ForIn` and `ForIn'`, respectively.
15+
Instances of `MeasurableSpaceForIn` specify how to iterate over a collection of
16+
elements, running a monadic loop body at each step while carrying a local state and allowing
17+
early termination. These instances enable the use of `for x in xs rdo ...` in `rdo` notation.
18+
`MeasurableSpaceForIn'` provides the same operation, but also passes the loop body a proof that the
19+
current element belongs to the collection. It enables the use of `for h : x in xs rdo ...` in
20+
`rdo` notation (as well as `for x in xs rdo ...` since a `MeasurableSpaceForIn'` instance also
21+
provides a `MeasurableSpaceForIn` instance).
22+
23+
-/
24+
25+
@[expose] public section
26+
27+
namespace MeasureTheory
28+
29+
universe uρ uα u v
30+
31+
instance instMeasurableSpaceForInStep {β : Type u} [mβ : MeasurableSpace β] :
32+
MeasurableSpace (ForInStep β) :=
33+
mβ.map ForInStep.yield ⊓ mβ.map ForInStep.done
34+
35+
/--
36+
Monadic iteration in `rdo` blocks, using the `for x in xs rdo ...` notation.
37+
38+
The parameter `m` is the monad of the `rdo` block in which iteration is performed, `ρ` is the type
39+
of the collection being iterated over, and `α` is the type of elements.
40+
41+
This is the measurable space analogue of `ForIn`.
42+
-/
43+
class MeasurableSpaceForIn (m : (α : Type u) → [MeasurableSpace α] → Type v)
44+
(ρ : Type uρ) (α : outParam (Type uα)) where
45+
/--
46+
Monadically iterates over the contents of a collection `xs`, with a local state `b` and the
47+
possibility of early termination. This is the measurable space analogue of `ForIn.forIn`.
48+
-/
49+
forIn {β : Type u} [MeasurableSpace β] (xs : ρ) (b : β) (f : α → β → m (ForInStep β)) : m β
50+
51+
export MeasurableSpaceForIn (forIn)
52+
53+
/--
54+
Monadic iteration in `rdo` blocks with a membership proof, using the `for h : x in xs rdo ...`
55+
notation.
56+
57+
The parameter `m` is the monad of the `rdo` block in which iteration is performed, `ρ` is the type
58+
of the collection being iterated over, `α` is the type of elements, and `d` is the specific
59+
membership predicate to provide.
60+
61+
This is the measurable space analogue of `ForIn'`.
62+
-/
63+
class MeasurableSpaceForIn' (m : (α : Type u) → [MeasurableSpace α] → Type v)
64+
(ρ : Type uρ) (α : outParam (Type uα)) (d : outParam (Membership α ρ)) where
65+
/--
66+
Monadically iterates over the contents of a collection `xs`, with a local state `b` and the
67+
possibility of early termination. At each iteration, the body of the loop is provided with a
68+
proof that the current element is in the collection. This is the measurable space analogue of
69+
`ForIn'.forIn'`.
70+
-/
71+
forIn' {β : Type u} [MeasurableSpace β] (x : ρ) (b : β)
72+
(f : (a : α) → a ∈ x → β → m (ForInStep β)) : m β
73+
74+
export MeasurableSpaceForIn' (forIn')
75+
76+
variable {m : (α : Type u) → [MeasurableSpace α] → Type v} {ρ : Type uρ} {α : Type uα}
77+
78+
/--
79+
A `MeasurableSpaceForIn'` instance also provides a `MeasurableSpaceForIn` instance by ignoring the
80+
membership proof. We set the priority to 500 so it is below the default.
81+
-/
82+
@[default_instance]
83+
instance (priority := 500) instMeasurableSpaceForInOfForIn' {d : Membership α ρ}
84+
[MeasurableSpaceForIn' m ρ α d] : MeasurableSpaceForIn m ρ α where
85+
forIn x b f := forIn' x b fun a _ => f a
86+
87+
@[simp] theorem forIn'_eq_forIn [d : Membership α ρ] [MeasurableSpaceForIn' m ρ α d]
88+
{β : Type u} [MeasurableSpace β] (x : ρ) (b : β)
89+
(f : (a : α) → a ∈ x → β → m (ForInStep β)) (g : (a : α) → β → m (ForInStep β))
90+
(h : ∀ a h b, f a h b = g a b) :
91+
forIn' x b f = forIn x b g := by
92+
simp only [forIn]
93+
congr
94+
funext a ha b
95+
exact h a ha b
96+
97+
@[wf_preprocess] theorem forIn_eq_forIn' [d : Membership α ρ] [MeasurableSpaceForIn' m ρ α d]
98+
{β : Type u} [MeasurableSpace β] (x : ρ) (b : β) (f : α → β → m (ForInStep β)) :
99+
forIn x b f = forIn' x b (fun x h => binderNameHint x f <| binderNameHint h () <| f x) := by
100+
rfl
101+
102+
end MeasureTheory

‎RandomDo/Monad/ForInInstances.lean‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ end MeasurableSpace
4848

4949
universe u v
5050

51-
open MeasurableSpacePure
51+
open MeasureTheory MeasurableSpacePure
5252

5353
variable {α : Type u} [mα : MeasurableSpace α] {m : (α : Type u) → [MeasurableSpace α] → Type v}
5454

‎RandomDo/Monad/Instances.lean‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ Authors: David Ledvinka
55
-/
66
module
77

8-
public import RandomDo.Monad.MeasurableSpace
8+
public import RandomDo.MeasurableSpaceMonad.Defs
9+
public import Mathlib.Control.Random
10+
public import Mathlib.MeasureTheory.Measure.GiryMonad
911
public import Mathlib.Probability.ProductMeasure
1012

1113
/-!
@@ -17,6 +19,8 @@ public import Mathlib.Probability.ProductMeasure
1719

1820
@[expose] public section
1921

22+
open MeasureTheory
23+
2024
universe u v w
2125

2226
/-- A (core) monad automatically defines a (not necessarily lawful) measurable space monad by
@@ -32,8 +36,6 @@ instance {m : Type u → Type v} [Monad m] :
3236
/-- A measurable space monad for pseudo random number generation. -/
3337
abbrev PseudoRandomM := Monad.toMeasurableSpaceMonad Rand
3438

35-
open MeasureTheory
36-
3739
open MeasurableSpacePure MeasurableSpaceBind MeasurableSpaceFunctor MeasurableSpaceMonad
3840

3941
@[simps]

‎RandomDo/Monad/Notation.lean‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ Authors: David Ledvinka
55
-/
66
module
77

8-
public import RandomDo.Monad.MeasurableSpace
8+
public import RandomDo.MeasurableSpaceMonad.ForIn
9+
public import Mathlib.MeasureTheory.MeasurableSpace.Prod
10+
public meta import Lean.Elab.BuiltinDo
911
public meta import Lean.Meta.ProdN
1012

1113
/-!
@@ -17,7 +19,7 @@ public meta import Lean.Meta.ProdN
1719

1820
open Lean Lean.Parser Lean.Parser.Term Lean.Meta Lean.Elab Lean.Elab.Do Lean.Elab.Term Std Meta
1921

20-
open MeasureTheory Measure MeasurableSpacePure MeasurableSpaceBind
22+
open MeasureTheory MeasurableSpacePure MeasurableSpaceBind
2123

2224
public meta section
2325

‎RandomDo/Tactic/Computable/Deriving.lean‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Authors: Gaëtan Serré
66
module
77

88
public meta import RandomDo.Tactic.Computable.Counterparts
9-
public import RandomDo.Monad.MeasurableSpace
9+
public import RandomDo.MeasurableSpaceMonad.ForIn
1010
public meta import Lean.Elab.Tactic.Basic
1111

1212
/-!
@@ -44,7 +44,7 @@ module that writes it: the interpreter then asks for the code of what it calls,
4444

4545
public meta section
4646

47-
open Lean Meta NumLean
47+
open Lean Meta NumLean MeasureTheory
4848

4949
namespace RDo.Tactic
5050

‎RandomDo/Tactic/IsMarkov/ForInStep.lean‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Authors: Gaëtan Serré
66
module
77

88
public import RandomDo.Tactic.IsMarkov.Defs
9-
public import RandomDo.Monad.MeasurableSpace
9+
public import RandomDo.MeasurableSpaceMonad.ForIn
1010

1111
/-!
1212
# Measurability of `ForInStep`

0 commit comments

Comments
 (0)