Saturday, December 01, 2012

Introduction to state monads

I went to my first Zurich FSharp meeting the other day and made a presentation on how to program with state monads in F#.

The presentation is designed to pull you off the ground, to a higher order programming style, using the state and maybe monad as stepping stones. It finishes with stateful lifting.

Here is a link to a slightly improved version: youtube .

Link to sources are below but I would recommend that if you really do not know this type of programming and want to learn, then you should type it back in and allow yourself some creative liberty. This is usually a much better way to learn, than to just copy code!

You can watch it here if you want:

ModularStateMonadsInFSharp.pdf
XState1.fs
XState2.fs
Script.fsx



5 comments:

Ville Nurmi said...

I want to say thanks for your presentation. I read the PDF, watched the video and worked on the code for several hours, typing it "in my own words" and trying out alternative ways. After all this I feel that I am finally comfortable enough with monads in order to use them in my own code.

I'm quite new to functional programming and I've been learning about monads for a while from various sources. What was really helpful in your presentation was how you built your way to the monad starting from simple imperative code. Also lifting the state monad to apply to a part of a complex structure was exactly what I wanted to know about practical use of the state monad.

So thanks once again, this has been really helpful!

James Litsios said...

Thank Ville,
Giving that presentation was a little bit like making an airplane take off, and just let it go higher and higher, and although I could see that I lost some of public, it was still a very exciting thing to do. Finishing with the lifting felt a little bit like overdoing it, but I have seen so many monadic state example never get past a "flatworld" view of state. I really wanted to to leave my audience with the path to go past that "limited" view.

Thanks again, comments like that are always a pleasure!

Anonymous said...

I have been struggling with Monads in both Scala and F# for the past year. This well thought out presentation turned the light on for me at last, so a very big thank you.

Regards

TK

Unknown said...

Hello,

I have just come across your talk on YouTube. It’s difficult to explain Monads/Computational Expressions but you have done the best job I have watched or read so far. Your approach of building up to Monads from standard code was a good approach. I am surprised with all the books coming out on F# there are none that just cover computational expressions.

Regards,

Mark.

James Litsios said...

Thank you Mark for your kind comment!
The thing is, "pure" functional programming is mathematics, and while mathematics can be built in an infinite many ways, it is natural to explain monads with types, because monads "are a type like thing". So most FP programmers see "monads as a type class (or something like that)". That is also the case for me "now", but when I wrote this presentation, in 2012, that was not the case. In 2012, I was still very much working with "monads as a rewrite invariances". FYI, rewrites tells you how you can replace some code by other code. (To make the story short, I think I took the rewrite route because I have written much "generative code", where code is "rewritten" a few times before being written out). Types are most often defined as rewrites, but then the type is often used as shorthand, without actually exposing rewrites too much. When you have a single notion of semantics, you can skip much about types, and just just work with rewrite rules, which is what this presentation is about. However, if have a complex interplay of semantics, that is, if your program is written "in multiple very different ways which co-interact", then types feel like the only sane way to separate the different meaning of your code, and I cannot see how a pure rewrite approach could do the job.
Regards, James