There is a lot of talk about monads these days. I have read a few articles / blog posts, but I can\'t go far enough with their examples to fully grasp the concept. The reaso
It has been a year since I posted this question. After posting it, I delved into Haskell for a couple of months. I enjoyed it tremendously, but I placed it aside just as I was ready to delve into Monads. I went back to work and focused on the technologies my project required.
And last night, I came and re-read these responses. Most importantly, I re-read the specific C# example in the text comments of the Brian Beckman video someone mentions above. It was so completely clear and illuminating that I’ve decided to post it directly here.
Because of this comment, not only do I feel like I understand exactly what Monads are … I realize I’ve actually written some things in C# that are Monads … or at least very close, and striving to solve the same problems.
So, here’s the comment – this is all a direct quote from the comment here by sylvan:
This is pretty cool. It's a bit abstract though. I can imagine people who don't know what monads are already get confused due to the lack of real examples.
So let me try to comply, and just to be really clear I'll do an example in C#, even though it will look ugly. I'll add the equivalent Haskell at the end and show you the cool Haskell syntactic sugar which is where, IMO, monads really start getting useful.
Okay, so one of the easiest Monads is called the "Maybe monad" in Haskell. In C# the Maybe type is called
Nullable. It's basically a tiny class that just encapsulates the concept of a value that is either valid and has a value, or is "null" and has no value.A useful thing to stick inside a monad for combining values of this type is the notion of failure. I.e. we want to be able to look at multiple nullable values and return
nullas soon as any one of them is null. This could be useful if you, for example, look up lots of keys in a dictionary or something, and at the end you want to process all of the results and combine them somehow, but if any of the keys are not in the dictionary, you want to returnnullfor the whole thing. It would be tedious to manually have to check each lookup fornulland return, so we can hide this checking inside the bind operator (which is sort of the point of monads, we hide book-keeping in the bind operator which makes the code easier to use since we can forget about the details).Here's the program that motivates the whole thing (I'll define the
Bindlater, this is just to show you why it's nice).class Program { static Nullablef(){ return 4; } static Nullable g(){ return 7; } static Nullable h(){ return 9; } static void Main(string[] args) { Nullable z = f().Bind( fval => g().Bind( gval => h().Bind( hval => new Nullable ( fval + gval + hval )))); Console.WriteLine( "z = {0}", z.HasValue ? z.Value.ToString() : "null" ); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } Now, ignore for a moment that there already is support for doing this for
Nullablein C# (you can add nullable ints together and you get null if either is null). Let's pretend that there is no such feature, and it's just a user-defined class with no special magic. The point is that we can use theBindfunction to bind a variable to the contents of ourNullablevalue and then pretend that there's nothing strange going on, and use them like normal ints and just add them together. We wrap the result in a nullable at the end, and that nullable will either be null (if any off,gorhreturns null) or it will be the result of summingf,g, andhtogether. (this is analogous of how we can bind a row in a database to a variable in LINQ, and do stuff with it, safe in the knowledge that theBindoperator will make sure that the variable will only ever be passed valid row values).You can play with this and change any of
f,g, andhto return null and you will see that the whole thing will return null.So clearly the bind operator has to do this checking for us, and bail out returning null if it encounters a null value, and otherwise pass along the value inside the
Nullablestructure into the lambda.Here's the
Bindoperator:public static Nullable Bind( this Nullable a, Func> f ) where B : struct where A : struct { return a.HasValue ? f(a.Value) : null; }The types here are just like in the video. It takes an
M a(Nullablein C# syntax for this case), and a function fromatoM b(Func>in C# syntax), and it returns anM b(Nullable).The code simply checks if the nullable contains a value and if so extracts it and passes it onto the function, else it just returns null. This means that the
Bindoperator will handle all the null-checking logic for us. If and only if the value that we callBindon is non-null then that value will be "passed along" to the lambda function, else we bail out early and the whole expression is null. This allows the code that we write using the monad to be entirely free of this null-checking behaviour, we just useBindand get a variable bound to the value inside the monadic value (fval,gvalandhvalin the example code) and we can use them safe in the knowledge thatBindwill take care of checking them for null before passing them along.There are other examples of things you can do with a monad. For example you can make the
Bindoperator take care of an input stream of characters, and use it to write parser combinators. Each parser combinator can then be completely oblivious to things like back-tracking, parser failures etc., and just combine smaller parsers together as if things would never go wrong, safe in the knowledge that a clever implementation ofBindsorts out all the logic behind the difficult bits. Then later on maybe someone adds logging to the monad, but the code using the monad doesn't change, because all the magic happens in the definition of theBindoperator, the rest of the code is unchanged.Finally, here's the implementation of the same code in Haskell (
--begins a comment line).-- Here's the data type, it's either nothing, or "Just" a value -- this is in the standard library data Maybe a = Nothing | Just a -- The bind operator for Nothing Nothing >>= f = Nothing -- The bind operator for Just x Just x >>= f = f x -- the "unit", called "return" return = Just -- The sample code using the lambda syntax -- that Brian showed z = f >>= ( \fval -> g >>= ( \gval -> h >>= ( \hval -> return (fval+gval+hval ) ) ) ) -- The following is exactly the same as the three lines above z2 = do fval <- f gval <- g hval <- h return (fval+gval+hval)As you can see the nice
donotation at the end makes it look like straight imperative code. And indeed this is by design. Monads can be used to encapsulate all the useful stuff in imperative programming (mutable state, IO etc.) and used using this nice imperative-like syntax, but behind the curtains, it's all just monads and a clever implementation of the bind operator! The cool thing is that you can implement your own monads by implementing>>=andreturn. And if you do so those monads will also be able to use thedonotation, which means you can basically write your own little languages by just defining two functions!