Adventures in Three Monads
January 1, 2010I’ve been busy at work over this winter break working on an article for The Monad Reader, entitled “Adventures in Three Monads.” The material will overlap the second part of my IAP talk Haskell Typeclasses that I will be delivering under the auspices of SIPB IAP.
The article itself is a literate Haskell file, and contains sample code I’ve cribbed from the various Haskell applications I’ve written over my year long flirtations with the language: included is code and explanation for the probabilistic Turing machine I built in order to brute-force a 6.004 assignment. (To the course staff: the code is incomplete enough that it’s not equivalent to publishing all of the solutions; intrepid readers will still have to write a search function themselves.)
I’ll be keeping a pre-print of the article available in my public directory. Questions, comments and suggestions would be greatly appreciated!
In the first example perhaps define:
fromList = (msum . map return)to make Listing 1 more readable
Hi, just to let you know your evensList example will actually return odds ;-)
I have just skimmed it, but will read it as soon as I can. The logict paper is very good but maybe it scares some people away (as there’s only 1 package in hackage that depends on it (http://bifunctor.homelinux.net/~roel/cgi-bin/hackage-scripts/revdeps/logict-0.3#direct)), and maybe now more programmers will start using it after reading your article.
About a third of the way down on page 2: “If the type signature is specied to be [a], the original list operations fall out again.” Could you reword this? What type signature? What does “fall out again” mean?
Bottom of page 10: “Note that they still are data constructors, so we can still use pattern matching, we can’t return any old value (it has to be of type GADTExample a), …”, but about a third of the way down on page 11: “As mentioned before, the return type need not be GADTExample a.” Looks like a contradiction.
Listing 14: Where does “prompt” come from?
Thanks for all of your comments! I’ve updated the article accordingly. Ricardo, very good catch. :-)
Re Anonymous “If the type signature is specied to be [a], the original list operations fall out again.” The list monad is an instance of MonadPlus. So any operator on MonadPlus, say mplus or mzero, has a generic type, but if you (by inference or explicitly) narrow the type to the list, you’ll find that it is equivalent to whatever type-specific function it corresponds to. I’ve tried to make this a little clearer in the article.
The state of the nondeterministic Turing Machine is defined as
data State = StateA | StateB
I was like, huh?
Hey Roy,
Thanks for the great comments! You should redownload the PDF, because I’ve made some updates (for example, I removed “faux monad” because both you and Brent Yorgey mentioned that it was confusing.)
Could you talk more about the Turing Machine’s state? I didn’t see any reference to the “StateA | StateB” thing.
Also, I’m interested in “the ordering being sensitive to monad transformations”, although you deleted it. Could you talk more about it on your blog?