Yet Another Monad Tutorial (part 8: more on state monads)

Aug 27, 2010 20:01


In the previous article, we learned what state monads were and how they worked. In this article we'll work through a complete (though very simple) example and we'll constrast Haskell's state handling with the equivalent code written in C.

Read more )

Leave a comment

Comments 16

bartosz October 31 2010, 18:24:20 UTC
I'm glad you decided to post this YAHMT (yet another Haskell monad tutorial), because it's the best I've seen so far. Please come back soon and continue. Whatever you're doing for a living is less important than this ;-)

I wonder if you could digress, just a little bit, into category theory to give some insight into monads as functors and all this lifting business. I understand a functor that lifts a function on some type to lists of that type. I also understand the list monad. But how are those two related?

Reply

Thanks! mvanier November 1 2010, 02:52:50 UTC
Bartosz, thanks so much for your kind comments. I really appreciate hearing from people who like my tutorials. I really will get back to this, but right now my teaching schedule is occupying me full-time -- sorry! Writing monad tutorials is more fun than most of the teaching I do, so expect more in the future ( ... )

Reply

Re: Thanks! bartosz November 1 2010, 23:11:08 UTC
Mike, I posted a link to your tutorial in my wordpress blog, so you might see some hits coming from there.

I know the facts about functors and monads, but I'd like to develop better intuitive understanding. For instance, your explanation of operator >>= in terms of function composition was an eye-opener for me. Once I got that, the rest just followed.

BTW, what's a good way of thinking about side effects. Do they, conceptually, happen at the end of the do block? What if you have two different side-effects monads and execute two do blocks, one for each monad. Can their side effects interleave?

Reply

Re: Thanks! mvanier November 3 2010, 23:11:47 UTC
Bartosz,

Thanks for the promotion! ;-) I'm really glad you like my approach.

I want to defer a full explanation of your question for my next blog post (about the IO monad) as there are a bunch of subtleties. The critical thing is that all operations occur in the correct sequence. If you have two do blocks in the IO monad, then they are both are executing inside another do block (i.e. in the IO monad, whether or not there is an actual do block); this means that they happen in sequence (no interleaving). If you want to execute an IO action outside of the IO monad there is unsafeInterleaveIO which can indeed interleave IO actions (and bad things can happen if you don't know what you're doing). See, for reference:

http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/System-IO-Unsafe.html#v:unsafeInterleaveIO

Reply


Thank you + MOAR! (please) mhwombat December 18 2010, 22:00:28 UTC
I've read dozens (literally) of monad tutorials, but your series has given me some valuable new insights into the whole monad thing. I hope you write more when you have time. If you need inspiration, here are some things that I would like to know more about. But really, anything you write will probably be exactly what I need next.

- Monad transformers. I'm doing some stuff with RandT right now, and if it hadn't been for the folks on beginners-haskell, I never would have figured out how to use it. I've got it working, but I probably only understand 80% of what I'm doing.

- Working with multiple monads at once. For example, I experimented with having a record structure with components that are monads, and the record as a whole was a monad too. I ran into some difficulties with that approach, but I wasn't sure whether that was because of my lack of knowledge, or because what I was trying to do was A Bad Idea.

Reply

Re: Thank you + MOAR! (please) mvanier December 19 2010, 04:08:29 UTC
Thanks for your comments! I definitely plan to write about monad transformers at some point. They are a classic example of something that sounds much worse than they are. Mostly it just involves a lot of bookkeeping when creating the monad transformer to make sure everything works right. Using transformers is usually pretty straightforward (at least in the cases I've encountered). I'm not as clear about your second point, so if you want to add any details I might be able to come up with something eventually.

Reply

Re: Thank you + MOAR! (please) mhwombat December 21 2010, 02:55:37 UTC
The second point is a bit difficult to explain, but this question I posted to the beginners mailing list will give you an example of the type of thing that I mean:

http://article.gmane.org/gmane.comp.lang.haskell.beginners/6055

I got this excellent reply from Brent Yorgey, which solved that problem and showed me a better approach (using an adapter) to accomplish what I needed

http://article.gmane.org/gmane.comp.lang.haskell.beginners/6067

That particular problem is sorted now, but perhaps it gives you an idea of the type of thing that has given me problems. But as I said before, whatever you write next will probably be just what I need to read.

Reply


painter 11 anonymous January 17 2011, 10:46:36 UTC
hey there I just wanted to comment your blog and say that I really enjoyed reading your blog post here. It was very informative and I also digg the way you write! Keep it up and I’ll be back to read more soon mate

Reply

Re: painter 11 mvanier January 18 2011, 05:33:55 UTC
Thanks!

Reply


sms anonymous April 16 2011, 02:05:43 UTC
Excellent posting. Undoubtedly you are an expert of such writing topics. It is just the first time I went through your post and to tell the truth it succeeds in making me visit here now and then.And yes i have digg your site mvanier.livejournal.com .

Reply


Thank you ext_766625 August 29 2011, 13:22:50 UTC
I really enjoyed reading your article specially State monads. It would be great if you can post some articles about about Monad transformer .

Reply

Re: Thank you mvanier December 20 2011, 06:32:37 UTC
That is an excellent idea and it's likely that that will be the next article in the series.

Reply


Leave a comment

Up