3f80 Monad - Telecomix Crypto Munitions Bureau

Monad

From Telecomix Crypto Munitions Bureau

Jump to: navigation, search

[edit] Monads

Monads could probably in some sense be considered as a design pattern.

The most well-known monad in Haskell is probably the I/O-monad, since you must use it to write a complete program, to be able to communicate with the Operating System, but it's probably not the best example of understanding what monads are about, since it's a bit of a sin-bin. (Many features that people haven't (yet) bothered to separate cleanly into monads on their own are included in it). If you only want to get started on doing I/O, try Introduction to IO or The IO Monad for People who Simply Don't Care.

Monads are not only used for Input & Output, but also for state (assignment to mutable variables and arrays, pointers references), error handling, nondeterminism (backtracking), parsing, continuations (goto/longjmp on steroids), logging, concurrency, serializing, randomness, simulation, dynamic scope extent, Software Transactional Memory, and more. (Each of the preceding items more or less is a separate monad.) The basic monadic API that all monads have in common is very small; to do any useful work in a particular monad you need to use extra operations specific for that monad.

Monads are probably learned best by first working through the most common of them, as opposed to attempting to tackle the general idea first. (This is because the monadic API is such a general API that says very little, so there's not much to "get a hold onto".)

Do note that there's a lot of monadic tutorials out there, many of them not very good, see Abstraction, intuition, and the “monad tutorial fallacy”. The following are ok (but preferences will probably differ, depending on the person) :

Monads have been described as "programmable semi-colon".

Finally, even though monads are required (specifically the I/O-monad is required) to do Input/Output in Haskell, while most other languages don't use monads much and certainly not for I/O (using side-effects instead), there could still be reasons for using monads in another language, e.g. to write parsers in a pleasant way or to implement light-weight cooperative threads. The Linc interface and syntax in C# is more or less the same as monads. (Note : If I/O wasn't done via a monad in Haskell, then it would be a PITA to try to figure out in which order side-effects happen, due to lazy evaluation.)

Personal tools
0