Skip to content

Commit 9e370dd

Browse files
author
David Ledvinka
committed
Merge standalone measurable monad documentation
2 parents ad59547 + 0462759 commit 9e370dd

1 file changed

Lines changed: 70 additions & 18 deletions

File tree

‎RandomDo/Monad/MeasurableSpace.lean‎

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,48 @@ public import Mathlib.Control.Random
99
public import Mathlib.MeasureTheory.Measure.GiryMonad
1010

1111
/-!
12-
# Measurable Space Monad
12+
# Measurable space monads
1313
14-
**TODO**
14+
This file defines `MeasurableSpaceFunctor` and `MeasurableSpaceMonad` as classes on functions of
15+
the form `m : (α : Type u) → [MeasurableSpace α] → Type v`. They play the role of Lean core's
16+
`Functor` and `Monad` for types equipped with a `MeasurableSpace` instance. Assuming that instances
17+
are lawful, this corresponds to functors and monads on the category of measurable spaces.
1518
1619
## Main definitions
1720
18-
* `MeasurableSpaceFunctor f`: **TODO**
19-
* `MeasurableSpaceMonad m`: **TODO**
20-
* `LawfulMeasurableSpaceFunctor f`: **TODO**
21-
* `LawfulMeasurableSpaceMonad m`: **TODO**
21+
* `MeasurableSpaceFunctor`: A typeclass for functors on types equipped with a `MeasurableSpace`
22+
instance.
23+
* `MeasurableSpaceMonad`: A typeclass for monads on types equipped with a `MeasurableSpace`
24+
instance.
25+
* `LawfulMeasurableSpaceFunctor`: A `Prop`-valued typeclass which states that a given
26+
`MeasurableSpaceFunctor` satisfies the laws of a functor on measurable spaces.
27+
* `LawfulMeasurableSpaceMonad`: A `Prop`-valued typeclass which states that a given
28+
`MeasurableSpaceMonad` satisfies the laws of a monad on measurable spaces.
2229
* `MeasurableSpaceForIn`: **TODO**
2330
* `MeasurableSpaceForIn'`: **TODO**
2431
32+
## Notation
33+
34+
The following notation is available:
35+
36+
* `f <$>ₘ x`: `MeasurableSpaceFunctor.mMap f x`.
37+
* `x >>=ₘ f`: `MeasurableSpaceBind.mBind x f`.
38+
39+
## Implementation details
40+
41+
The development of `MeasurableSpaceMonad` mirrors the development of `Monad` in Lean core, including
42+
the definition of intermediate classes `MeasurableSpaceFunctor`, `MeasurableSpacePure`, and
43+
`MeasurableSpaceBind` except that there is no `MeasurableSpaceApplicative` and instead
44+
`MeasurableSpaceMonad` extends all three of the preceding classes directly. This is because
45+
defining a `MeasurableSpaceApplicative` would require defining an analogue of
46+
`Seq.seq : m (α → β) → (Unit → m α) → m β`, and we would like such an operation to be jointly
47+
measurable in `m (α → β)` and `Unit → m α`, but requiring this operation to be jointly measurable
48+
would already fail for the identity monad: there, `seq f (fun _ => x) = f x`, so `seq` reduces to
49+
function evaluation. No σ-algebra on the space of all measurable functions makes evaluation
50+
jointly measurable in general;
51+
see R. J. Aumann, [*Borel structures for function spaces*](https://doi.org/10.1215/ijm/1255631584),
52+
*Illinois Journal of Mathematics* **5** (1961), no. 4, 614–630, §3.
53+
2554
-/
2655

2756
@[expose] public section
@@ -34,25 +63,36 @@ universe u v
3463

3564
section MeasurableSpaceMonad
3665

37-
/-- A functor on types with a `MeasurableSpace` instance. The `mMap` operator `<$>ₘ` is overloaded
38-
via instances of this class. This class does not require proofs of the `MeasurableSpaceFunctor`
39-
axioms. Proofs may be provided or required via the `LawfulMeasurableSpaceFunctor` class. -/
66+
/--
67+
A functor on types with a `MeasurableSpace` instance. This means a function
68+
`f : (α : Type u) → [MeasurableSpace α] → Type v` equipped with an
69+
`mMap [MeasurableSpace α] [MeasurableSpace β] : (α → β) → f α → f β` operator,
70+
which can be written with the notation `<$>ₘ`.
71+
72+
A lawful instance of this class requires a `MeasurableSpace` instance on each output type `f α`,
73+
and that `mMap` maps measurable functions `α → β` to measurable functions `f α → f β`, maps
74+
identity functions to identity functions, and respects composition of measurable functions.
75+
This class does not require its instances to be lawful. Proofs of lawful instances can be
76+
provided via the `Prop`-valued class `LawfulMeasurableSpaceFunctor`.
77+
78+
Lawful instances of this class correspond to functors on the category of measurable spaces.
79+
-/
4080
class MeasurableSpaceFunctor (f : (α : Type u) → [MeasurableSpace α] → Type v) :
4181
Type (max (u+1) v) where
4282
/--
4383
Applies a function inside a functor on measurable spaces. This is used to overload the
4484
`<$>ₘ` operator.
4585
46-
When mapping a constant function (if one cares about executing the code), use
47-
`Functor.mMapConst` instead, because it may be more efficient.
86+
When mapping a constant function in executable code, use
87+
`MeasurableSpaceFunctor.mMapConst` instead, because it may be more efficient.
4888
-/
4989
mMap {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] : (α → β) → f α → f β
5090
/--
5191
Mapping a constant function.
5292
5393
Given `a : α` and `v : f β`, `mMapConst a v` is equivalent to `(fun _ => a) <$>ₘ v`. For some
54-
functors, this can be implemented more efficiently; for all other functors, the default
55-
implementation may be used.
94+
functors where executable code is relevant, this can be implemented more efficiently; for all
95+
other functors, the default implementation may be used.
5696
-/
5797
mMapConst {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] : α → f β → f α :=
5898
mMap ∘ (const _)
@@ -66,7 +106,7 @@ The `mPure` function is overloaded via `MeasurableSpacePure` instances.
66106
-/
67107
class MeasurableSpacePure (f : (α : Type u) → [MeasurableSpace α] → Type v) where
68108
/-- Given `a : α` where `α` has a `MeasurableSpace` instance, `mPure a : f α` represents an
69-
action that does nothing and returns a -/
109+
action that does nothing and returns `a`. -/
70110
mPure {α : Type u} [MeasurableSpace α] : α → f α
71111

72112
/--
@@ -85,8 +125,21 @@ class MeasurableSpaceBind (m : (α : Type u) → [MeasurableSpace α] → Type v
85125

86126
@[inherit_doc] infixl:55 " >>=ₘ " => MeasurableSpaceBind.mBind
87127

88-
/-- A monad on types with a `MeasurableSpace` instance. This abstraction allows the user to write
89-
code that depends on "random" side-effects -/
128+
/--
129+
A monad on types with a `MeasurableSpace` instance.
130+
131+
Monads are an abstraction of sequential control flow and side effects in functional programming,
132+
and `MeasurableSpaceMonad`s allow the use of this abstraction on programs taking values in
133+
`MeasurableSpace`s. The canonical example of such a monad is `Measure` where programs
134+
"written" in the `Measure` monad can represent programs with "random" outputs whose output
135+
distribution is given by the output measure. `MeasurableSpaceMonad`s support `rdo` notation,
136+
the measurable-space analogue of `do` notation, for specifying these programs.
137+
138+
A lawful instance of this class should satisfy various properties so that the instance corresponds
139+
to a monad on the category of measurable spaces. These are specified precisely in the `Prop`-valued
140+
class `LawfulMeasurableSpaceMonad` and proofs of lawfulness can be given by defining an instance
141+
of that class.
142+
-/
90143
class MeasurableSpaceMonad (m : (α : Type u) → [MeasurableSpace α] → Type v) :
91144
Type (max (u+1) v)
92145
extends MeasurableSpaceFunctor m, MeasurableSpacePure m, MeasurableSpaceBind m where
@@ -159,8 +212,7 @@ class LawfulMeasurableSpaceMonad
159212
mBind_mPure_comp {α β : Type u} [MeasurableSpace α] [MeasurableSpace β]
160213
{f : α → β} (hf : Measurable f) (x : m α) :
161214
x >>=ₘ (fun a => mPure (f a)) = f <$>ₘ x
162-
/-- `mPure` followed by `mBind` of a function application is equivalent to function
163-
application. -/
215+
/-- Binding `mPure x` to a measurable function `f` is equivalent to applying `f` to `x`. -/
164216
mPure_mBind {α β : Type u} [MeasurableSpace α] [MeasurableSpace β]
165217
(x : α) {f : α → m β} (hf : Measurable f) :
166218
mPure x >>=ₘ f = f x

0 commit comments

Comments
 (0)