A type T
is a subtype of type M[X]
iff:
- There exists a function
unit
of typeX → M[X]
- There exists a function
bind
of type(M[X], X → R) → R
- For all
x:X
andf:X→R
:bind(unit(x),f) ≡ f(x)
The natural inclination is to ask, "What are the semantics here?" but it turns out that the semantics depend wholly on the type. The more monadic types we define, the clearer it will become how the above constraints are useful.
We can show that any type T
has a trivially monadic structure (or: ∀T:T⊆M[T]
):
let unit(t:T) ≡ t
let bind(t:T,f:T → R) ≡ f(t)
- Proof: Let type
R
,a:T
andf:T → R
. Then,bind(unit(a),f) ≡ bind(a,f) ≡ f(a)
.
Therefore, ∀T:T⊆M[T]
. QED.
So everything is a monad? What the hell? There is so much nuance to monads that it is hard to intuit them from a rigorous definition.
Definition. The Unit
type is a type containing one member, denoted (), or the dimensionless tuple. Using the definition
above, we show that () has type M[()]:
let unit(u:Unit) ≡ unit(()) ≡ unit() ≡ ()
let bind(u:Unit, f:Unit → R) ≡ bind((), f:() → R) ≡ bind(f:() → R) ≡ f()
- Proof: Let type
R
andf:() → R
. Then,bind(unit(()),f) ≡ bind((),f) ≡ bind(f) ≡ f()
.
Therefore, ()⊆M[()].
Definition. Given type T[X]
and x:X
, then T(x)
is a value of type T[X]
.
Definition. Define a type Box[X], such that:
box(x:X):Box[X] ≡ Box(x)
.unbox(Box(x), f:Box[X] → R) ≡ f(x)
.- Proof: Let type
R
,a:T
andf:T → R
. Then,unbox(box(a),f) ≡ unbox(Box(a),f) ≡ f(a)
.