monad laws
All monads should follow (and in practice, all built-ins do), with respect to the above three operations,
- left identity
return a >>= k = k a
k
will be a function that accepsa
and givesm a
, so instead of wrappinga
to bind it to k, we should be able to apply.
- right identity
m >>= return = m
- returning an "empty" monad should just give the monad instance itself
- associativity
(m a >>= f) >>= g = m a >>= (\x -> f x >>= g)
- On the LHS;
f
is of typea -> m b
, so the first term ism b
g
is of typeb -> m c
, result ism c
- On the RHS:
\x -> f x
immediately takes typea
and givesm b
- bound to
g
to givem c
- in this sense, the bracket part is a lambda wrapper on simply
f >>= g
that allows it to be a function that fits thebind
typesig.
- On the LHS;