Thursday, July 14, 2011

What is the USP of the monad?

I am preparing an introductory article on monads for a Dutch software magazine. It is intended for .NET developers. I have to restrict myself so I am looking for the Unique Selling Point (USP) of the monad.

This is the list of selling points I could find. I left out selling points of specific monads and used the formulation of Wikipedia:

  1. Monads are a kind of abstract data type constructor that encapsulate program logic instead of data in the domain model.
  2. Control structure/inspection.
  3. Hiding complexity with syntactic sugar.
  4. Composition : monads chain actions together to build a pipeline.
  5. To express input/output (I/O) operations and changes in state without using language features that introduce side effects.

I put them in the order of most favorite to least favorite.

My remarks:

1. Monads are a kind of abstract data type constructor that encapsulate program logic instead of data in the domain model.

This point can be split into two aspects of monads:

  1. The monadic type is a wrapper of a data type (If M is the name of the monad and t is a data type, then "M t" is the corresponding type in the monad).
  2. The functions: read (unit) and bind.

I think that the ability to have both access to the functionality of the wrapper and the original data type and a way to transform solution between the two is the most valuable selling point.

2. Control structure/inspection.

The monad is an excellent way to encapsulate inspection logic. Most monad tutorials start with the maybe monad because it is a simple example. It also shows the value of the monadic approach by hiding inspection plumbing.

3. Hiding complexity with syntactic sugar.

I think that the popularity of LINQ in C# and VB.NET is a proof that hiding complexity with syntactic sugar has its value.

4. Composition: monads chain actions together to build a pipeline.

In F# computation expressions or monads are also described as workflows. So this could be the USP. Sequence expression are a proof of the value of composition.

5. To express input/output (I/O) operations and changes in state without using language features that introduce side effects.

This is the least relevant point for a .NET developer. I do understand that this the most relevant one for a developer in a pure language or one that want to reduce the number of side effects.

Please feel free to add a command in case I missed a selling point or you have a better way to order the them.

Total Pageviews