Trivial Monad
Contents
Dan Piponi in his post about Monads started by taking a design pattern of a wrapper.


In this design pattern there are only two functions, one
is to wrap anything with the wrapper. Let’s call it return'
(otherwise
it clashes with return
in Prelude
):


The other function is to take an unwrappingtransforming function, take a wrapped value and to convert it into another wrapped value by using the function.


So while we have the option of unwrapping the value using pattern matching:


We would not want to do that because in real world, this
wrapper is called Monad
and it quickly becomes unwieldy to pattern
match all of its subclasses. Any unwrapping of the values needs to be
done in the bind
function. Remember, we can access the unwrapped value
in the function passed to bind
as its first parameter.
This excellent explanation is followed by exercises.
Here are my attempts:
The first function takes an Integer and a Wrapped integer and creates
their sum. The unwrapping happens in bind
. This pattern is essentially
about doing something with wrapped and free value.


The second function takes two wrapped values and create a new one. Here the =bind=s are nested and this does not look like a very clean approach.


Using the above definition of h
, g
can be redefined as:


We also have the option of creating Functor, Applicative, and Monad instances for W:


So the code gets a little cleaner:


There’s also one method of unwrapping one layer of a doubly wrapped value:


This is a good example because the function which goes into bind has type of a > W a
, and id
has the type a > a
. I could think about the second
(commented) function only after coming up with the first one.
Therefore, >>=
function unwraps one Monad
value and transforms it to
another Monad
value.


Author Tushar Tyagi
LastMod May 25, 2016