Monads explained (sort of) to a C# developer
A monad is a fancy word for a generic type of the form
MyMonad<T> (a generic type of arity 1).
A monad is special because it adds ‘special powers’ to the
T that it wraps.
These ‘special powers’ won’t sound very special to an imperative programmer, so you have to squint to see them but bear with me.
IEnumerable<T>is a monad that gives values of type
Tthe special power of nondeterminism, or the ability to ‘be’ multiple values at once.
Nullable<T>is a monad that gives values of type
Tthe special power of nullability, or the ability to be absent.
Task<T>is a monad that gives values of type
Tthe special power of asynchronicity, or the ability to be used before they are computed.
The trick with monads comes when you want to play with the
T values, because they are inside another type. C# introduced language changes to make dealing with values inside these monads easier:
IEnumerable<T>, we use LINQ to work with the
Nullable<T>, we don’t really have good language support–you have to use
.GetValue*to ‘escape’ from the
Task<T>, we have
awaitto let us play with the
In languages like Haskell or F#, you have a general purpose way to define your own monads and define how to play with the values inside them. This is accomplished by defining two methods, usually called
// Return lets you put a T inside the monad: public static MyMonad<T> Return<T>(T value); // Bind lets you get a take a MyMonad<T>, get at the T inside it, // and turn it into a MyMonad<U>: public static MyMonad<U> Bind<T, U>(MyMonad<T> value, Func<T, MyMonad<U>> operation);
In Haskell, these signatures look more or less like:
class Monad m where return :: a -> m a bind :: m a -> (a -> m b) -> m b
Return lets you make a
Task<string> from a
Bind lets you take the
string out of
Task<string>, and turn it into a
Monads are used extensively in functional programming to add ‘special powers’ like exceptions, async, state, logging, etc. to the exact regions of your code where you need them. Haskell, for example, is a much smaller language than C# because it doesn’t allow values to be null, it doesn’t allow mutation, it doesn’t have exceptions, and it doesn’t have LINQ; however, you can define these features (and more–e.g. massively parallel cloud computing) with monads and use them precisely.