1 Functors and Free Monads
An instance of F should contain a function called
_map constrained by whichever base type is being functor-ized.
Mu is the free monad. I like calling it
Mu for two reasons: because calling it “free” is confusing and not informative; and because
Mu behaves like the type-level analog to the Y-combinator, called Mu.
These two are different names for the same thing, mostly for aesthetic reasons. You’ll see.
This is the cool part.
bind is defined for any type which is wrapped by
Mu, and for a type to be wrapped by
Mu it must instantiate our
F type. Thus we can ask for an “instance” argument, called
2 An example: re-creating the
Think of how much time we have collectively lost to massive failure due to null pointer errors. Now behold how simple optional types are to implement, and weep:
Now for the fun part. I will create an
F instance for
Optional along with some convenience functions to use in monadic computations. While I don’t do it here, this could be derived automatically.
In ~12 lines of real code, I have created a
Maybe clone and proven it is a functor. As a result all the remaining code necessary to compose a monad has been derived automatically.
Since this was a free monad, the only remaining code is that to “run” the monadic computation built up using
Without further ado, here is some example code written in our
Try it out for yourself to see the results.