Having briefly looked at Haskell recently, what would be a *brief, succinct, practical* explanation as to what a monad essentially is?

I have found most explanations I've come across to be fairly inaccessible and lacking in practical detail.

Translate

Having briefly looked at Haskell recently, what would be a *brief, succinct, practical* explanation as to what a monad essentially is?

I have found most explanations I've come across to be fairly inaccessible and lacking in practical detail.

所有的回答

First: The term

monadis a bit vacuous if you are not a mathematician. An alternative term iscomputation builderwhich is a bit more descriptive of what they are actually useful for.You ask for practical examples:

Example 1: List comprehension:This expression returns the doubles of all odd numbers in the range from 1 to 10. Very useful!

It turns out this is really just syntactic sugar for some operations within the List monad. The same list comprehension can be written as:

Or even:

Example 2: Input/Output:Both examples use monads, AKA computation builders. The common theme is that the monad

chains operationsin some specific, useful way. In the list comprehension, the operations are chained such that if an operation returns a list, then the following operations are performed onevery itemin the list. The IO monad on the other hand performs the operations sequentially, but passes a "hidden variable" along, which represents "the state of the world", which allows us to write I/O code in a pure functional manner.It turns out the pattern of

chaining operationsis quite useful and is used for lots of different things in Haskell.Another example is exceptions: Using the

`Error`

monad, operations are chained such that they are performed sequentially, except if an error is thrown, in which case the rest of the chain is abandoned.Both the list-comprehension syntax and the do-notation are syntactic sugar for chaining operations using the

`>>=`

operator. A monad is basically just a type that supports the`>>=`

operator.Example 3: A parserThis is a very simple parser which parses either a quoted string or a number:

The operations

`char`

,`digit`

, etc. are pretty simple. They either match or don't match. The magic is the monad which manages the control flow: The operations are performed sequentially until a match fails, in which case the monad backtracks to the latest`<|>`

and tries the next option. Again, a way of chaining operations with some additional, useful semantics.Example 4: Asynchronous programmingThe above examples are in Haskell, but it turns out F# also supports monads. This example is stolen from Don Syme:

This method fetches a web page. The punch line is the use of

`GetResponseAsync`

- it actually waits for the response on a separate thread, while the main thread returns from the function. The last three lines are executed on the spawned thread when the response have been received.In most other languages you would have to explicitly create a separate function for the lines that handle the response. The

`async`

monad is able to "split" the block on its own and postpone the execution of the latter half. (The`async {}`

syntax indicates that the control flow in the block is defined by the`async`

monad.)How they workSo how can a monad do all these fancy control-flow thing? What actually happens in a do-block (or a

computation expressionas they are called in F#), is that every operation (basically every line) is wrapped in a separate anonymous function. These functions are then combined using the`bind`

operator (spelled`>>=`

in Haskell). Since the`bind`

operation combines functions, it can execute them as it sees fit: sequentially, multiple times, in reverse, discard some, execute some on a separate thread when it feels like it and so on.As an example, this is the expanded version of the IO-code from example 2:

This is uglier, but it's also more obvious what is actually going on. The

`>>=`

operator is the magic ingredient: It takes a value (on the left side) and combines it with a function (on the right side), to produce a new value. This new value is then taken by the next`>>=`

operator and again combined with a function to produce a new value.`>>=`

can be viewed as a mini-evaluator.Note that

`>>=`

is overloaded for different types, so every monad has its own implementation of`>>=`

. (All the operations in the chain have to be of the type of the same monad though, otherwise the`>>=`

operator won't work.)The simplest possible implementation of

`>>=`

just takes the value on the left and applies it to the function on the right and returns the result, but as said before, what makes the whole pattern useful is when there is something extra going on in the monad's implementation of`>>=`

.There is some additional cleverness in how the values are passed from one operation to the next, but this requires a deeper explanation of the Haskell type system.

Summing upIn Haskell-terms a monad is a parameterized type which is an instance of the Monad type class, which defines

`>>=`

along with a few other operators. In layman's terms, a monad is just a type for which the`>>=`

operation is defined.In itself

`>>=`

is just a cumbersome way of chaining functions, but with the presence of the do-notation which hides the "plumbing", the monadic operations turns out to be a very nice and useful abstraction, useful many places in the language, and useful for creating your own mini-languages in the language.Why are monads hard?For many Haskell-learners, monads are an obstacle they hit like a brick wall. It's not that monads themselves are complex, but that the implementation relies on many other advanced Haskell features like parameterized types, type classes, and so on. The problem is that Haskell I/O is based on monads, and I/O is probably one of the first things you want to understand when learning a new language - after all, it's not much fun to create programs which don't produce any output. I have no immediate solution for this chicken-and-egg problem, except treating I/O like "magic happens here" until you have enough experience with other parts of language. Sorry.

Excellent blog on monads: http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Explaining "what is a monad" is a bit like saying "what is a number?" We use numbers all the time. But imagine you met someone who didn't know anything about numbers. How the

heckwould you explain what numbers are? And how would you even begin to describe why that might be useful?What is a monad? The short answer: It's a specific way of chaining operations together.

In essence, you're writing execution steps and linking them together with the "bind function". (In Haskell, it's named

`>>=`

.) You can write the calls to the bind operator yourself, or you can use syntax sugar which makes the compiler insert those function calls for you. But either way, each step is separated by a call to this bind function.So the bind function is like a semicolon; it separates the steps in a process. The bind function's job is to take the output from the previous step, and feed it into the next step.

That doesn't sound too hard, right? But there is

more than onekind of monad. Why? How?Well, the bind function

canjust take the result from one step, and feed it to the next step. But if that's "all" the monad does... that actually isn't very useful. And that's important to understand: Everyusefulmonad does something elsein additionto just being a monad. Everyusefulmonad has a "special power", which makes it unique.(A monad that does

nothingspecial is called the "identity monad". Rather like the identity function, this sounds like an utterly pointless thing, yet turns out not to be... But that's another story™.)Basically, each monad has its own implementation of the bind function. And you can write a bind function such that it does hoopy things between execution steps. For example:

If each step returns a success/failure indicator, you can have bind execute the next step only if the previous one succeeded. In this way, a failing step aborts the whole sequence "automatically", without any conditional testing from you. (The

Failure Monad.)Extending this idea, you can implement "exceptions". (The

Error MonadorException Monad.) Because you're defining them yourself rather than it being a language feature, you can define how they work. (E.g., maybe you want to ignore the first two exceptions and only abort when athirdexception is thrown.)You can make each step return

multiple results, and have the bind function loop over them, feeding each one into the next step for you. In this way, you don't have to keep writing loops all over the place when dealing with multiple results. The bind function "automatically" does all that for you. (TheList Monad.)As well as passing a "result" from one step to another, you can have the bind function

pass extra dataaround as well. This data now doesn't show up in your source code, but you can still access it from anywhere, without having to manually pass it to every function. (TheReader Monad.)You can make it so that the "extra data" can be replaced. This allows you to

simulate destructive updates, without actually doing destructive updates. (TheState Monadand its cousin theWriter Monad.)Because you're only

simulatingdestructive updates, you can trivially do things that would be impossible withrealdestructive updates. For example, you canundo the last update, orrevert to an older version.You can make a monad where calculations can be

paused, so you can pause your program, go in and tinker with internal state data, and then resume it.You can implement "continuations" as a monad. This allows you to

break people's minds!All of this and more is possible with monads. Of course, all of this is also perfectly possible

withoutmonads too. It's just drasticallyeasierusing monads.Actually, contrary to common understanding of Monads, they have nothing to do with state. Monads are simply a way to wrapping things and provide methods to do operations on the wrapped stuff without unwrapping it.

For example, you can create a type to wrap another one, in Haskell:

To wrap stuff we define

To perform operations without unwrapping, say you have a function

`f :: a -> b`

, then you can do this toliftthat function to act on wrapped values:That's about all there is to understand. However, it turns out that there is a more general function to do this

lifting, which is`bind`

:`bind`

can do a bit more than`fmap`

, but not vice versa. Actually,`fmap`

can be defined only in terms of`bind`

and`return`

. So, when defining a monad.. you give its type (here it was`Wrapped a`

) and then say how its`return`

and`bind`

operations work.The cool thing is that this turns out to be such a general pattern that it pops up all over the place, encapsulating state in a pure way is only one of them.

For a good article on how monads can be used to introduce functional dependencies and thus control order of evaluation, like it is used in Haskell's IO monad, check out IO Inside.

As for understanding monads, don't worry too much about it. Read about them what you find interesting and don't worry if you don't understand right away. Then just diving in a language like Haskell is the way to go. Monads are one of these things where understanding trickles into your brain by practice, one day you just suddenly realize you understand them.

But, You could have invented Monads!

A monad is a datatype that has two operations:

`>>=`

(aka`bind`

) and`return`

(aka`unit`

).`return`

takes an arbitrary value and creates an instance of the monad with it.`>>=`

takes an instance of the monad and maps a function over it. (You can see already that a monad is a strange kind of datatype, since in most programming languages you couldn't write a function that takes an arbitrary value and creates a type from it. Monads use a kind ofparametric polymorphism.)In Haskell notation, the monad interface is written

These operations are supposed to obey certain "laws", but that's not terrifically important: the "laws" just codify the way sensible implementations of the operations ought to behave (basically, that

`>>=`

and`return`

ought to agree about how values get transformed into monad instances and that`>>=`

is associative).Monads are not just about state and I/O: they abstract a common pattern of computation that includes working with state, I/O, exceptions, and non-determinism. Probably the simplest monads to understand are lists and option types:

where

`[]`

and`:`

are the list constructors,`++`

is the concatenation operator, and`Just`

and`Nothing`

are the`Maybe`

constructors. Both of these monads encapsulate common and useful patterns of computation on their respective data types (note that neither has anything to do with side effects or I/O).You really have to play around writing some non-trivial Haskell code to appreciate what monads are about and why they are useful.

You should first understand what a functor is. Before that, understand higher-order functions.

A

higher-order functionis simply a function that takes a function as an argument.A

functoris any type construction`T`

for which there exists a higher-order function, call it`map`

, that transforms a function of type`a -> b`

(given any two types`a`

and`b`

) into a function`T a -> T b`

. This`map`

function must also obey the laws of identity and composition such that the following expressions return true for all`p`

and`q`

(Haskell notation):For example, a type constructor called

`List`

is a functor if it comes equipped with a function of type`(a -> b) -> List a -> List b`

which obeys the laws above. The only practical implementation is obvious. The resulting`List a -> List b`

function iterates over the given list, calling the`(a -> b)`

function for each element, and returns the list of the results.A

monadis essentially just a functor`T`

with two extra methods,, of type`join`

`T (T a) -> T a`

, and`unit`

(sometimes called`return`

,`fork`

, or`pure`

) of type`a -> T a`

. For lists in Haskell:Why is that useful? Because you could, for example,

`map`

over a list with a function that returns a list.`Join`

takes the resulting list of lists and concatenates them.`List`

is a monad because this is possible.You can write a function that does

`map`

, then`join`

. This function is called`bind`

, or`flatMap`

, or`(>>=)`

, or`(=<<)`

. This is normally how a monad instance is given in Haskell.A monad has to satisfy certain laws, namely that

`join`

must be associative. This means that if you have a value`x`

of type`[[[a]]]`

then`join (join x)`

should equal`join (map join x)`

. And`pure`

must be an identity for`join`

such that`join (pure x) == x`

.[Disclaimer: I am still trying to fully grok monads. The following is just what I have understood so far. If it’s wrong, hopefully someone knowledgeable will call me on the carpet.]

Arnar wrote:

That’s precisely it. The idea goes like this:

You take some kind of value and wrap it with some additional information. Just like the value is of a certain kind (eg. an integer or a string), so the additional information is of a certain kind.

E.g., that extra information might be a

`Maybe`

or an`IO`

.Then you have some operators that allow you to operate on the wrapped data while carrying along that additional information. These operators use the additional information to decide how to change the behaviour of the operation on the wrapped value.

E.g., a

`Maybe Int`

can be a`Just Int`

or`Nothing`

. Now, if you add a`Maybe Int`

to a`Maybe Int`

, the operator will check to see if they are both`Just Int`

s inside, and if so, will unwrap the`Int`

s, pass them the addition operator, re-wrap the resulting`Int`

into a new`Just Int`

(which is a valid`Maybe Int`

), and thus return a`Maybe Int`

. But if one of them was a`Nothing`

inside, this operator will just immediately return`Nothing`

, which again is a valid`Maybe Int`

. That way, you can pretend that your`Maybe Int`

s are just normal numbers and perform regular math on them. If you were to get a`Nothing`

, your equations will still produce the right result –without you having to litter checks for.`Nothing`

everywhereBut the example is just what happens for

`Maybe`

. If the extra information was an`IO`

, then that special operator defined for`IO`

s would be called instead, and it could do something totally different before performing the addition. (OK, adding two`IO Int`

s together is probably nonsensical – I’m not sure yet.) (Also, if you paid attention to the`Maybe`

example, you have noticed that “wrapping a value with extra stuff” is not always correct. But it’s hard to be exact, correct and precise without being inscrutable.)Basically,

“monad” roughly means “pattern”. But instead of a book full of informally explained and specifically named Patterns, you now havea language construct– syntax and all – that allows you todeclare new patterns as things in your program. (The imprecision here is all the patterns have to follow a particular form, so a monad is not quite as generic as a pattern. But I think that’s the closest term that most people know and understand.)And that is why people find monads so confusing: because they are such a generic concept. To ask what makes something a monad is similarly vague as to ask what makes something a pattern.

But think of the implications of having syntactic support in the language for the idea of a pattern: instead of having to read the

Gang of Fourbook and memorise the construction of a particular pattern, you justwrite code that implements this pattern in an agnostic, generic wayonce and then you are done! You can then reuse this pattern, like Visitor or Strategy or Façade or whatever, just by decorating the operations in your code with it, without having to re-implement it over and over!So that is why people who

understandmonads find them souseful: it’s not some ivory tower concept that intellectual snobs pride themselves on understanding (OK, that too of course, teehee), but actually makes code simpler.After much striving, I think I finally understand the monad. After rereading my own lengthy critique of the overwhelmingly top voted answer, I will offer this explanation.

There are three questions that need to be answered to understand monads:

As I noted in my original comments, too many monad explanations get caught up in question number 3, without, and before really adequately covering question 2, or question 1.

Why do you need a monad?Pure functional languages like Haskell are different from imperative languages like C, or Java in that, a pure functional program is not necessarily executed in a specific order, one step at a time. A Haskell program is more akin to a mathematical function, in which you may solve the "equation" in any number of potential orders. This confers a number of benefits, among which is that it eliminates the possibility of certain kinds of bugs, particularly those relating to things like "state".

However, there are certain problems that are not so straightforward to solve with this style of programming. Some things, like console programming, and file i/o, need things to happen in a particular order, or need to maintain state. One way to deal with this problem is to create a kind of object that represents the state of a computation, and a series of functions that take a state object as input, and return a new modified state object.

So let's create a hypothetical "state" value, that represents the state of a console screen. exactly how this value is constructed is not important, but let's say it's an array of byte length ascii characters that represents what is currently visible on the screen, and an array that represents the last line of input entered by the user, in pseudocode. We've defined some functions that take console state, modify it, and return a new console state.

So to do console programming, but in a pure functional manner, you would need to nest a lot of function calls inside eachother.

Programming in this way keeps the "pure" functional style, while forcing changes to the console to happen in a particular order. But, we'll probably want to do more than just a few operations at a time like in the above example. Nesting functions in that way will start to become ungainly. What we want, is code that does essentially the same thing as above, but is written a bit more like this:

This would indeed be a more convenient way to write it. How do we do that though?

What is a monad?Once you have a type (such as

`consolestate`

) that you define along with a bunch of functions designed specifically to operate on that type, you can turn the whole package of these things into a "monad" by defining an operator like`:`

(bind) that automatically feeds return values on its left, into function parameters on its right, and a`lift`

operator that turns normal functions, into functions that work with that specific kind of bind operator.How is a monad implemented?See other answers, that seem quite free to jump into the details of that.

(See also the answers atWhat is a monad?)A good motivation to Monads is sigfpe (Dan Piponi)'s You Could Have Invented Monads! (And Maybe You Already Have). There are a LOT of other monad tutorials, many of which misguidedly try to explain monads in "simple terms" using various analogies: this is the monad tutorial fallacy; avoid them.

As DR MacIver says in

Tell us why your language sucks:You say you understand the Maybe monad? Good, you're on your way. Just start using other monads and sooner or later you'll understand what monads are in general.

[If you are mathematically oriented, you might want to ignore the dozens of tutorials and learn the definition, or follow lectures in category theory :) The main part of the definition is that a Monad M involves a "type constructor" that defines for each existing type "T" a new type "M T", and some ways for going back and forth between "regular" types and "M" types.]

Also, surprisingly enough, one of the best introductions to monads is actually one of the early academic papers introducing monads, Philip Wadler's Monads for functional programming. It actually has practical,

non-trivialmotivating examples, unlike many of the artificial tutorials out there.A monad is, effectively, a form of "type operator". It will do three things. First it will "wrap" (or otherwise convert) a value of one type into another type (typically called a "monadic type"). Secondly it will make all the operations (or functions) available on the underlying type available on the monadic type. Finally it will provide support for combining its self with another monad to produce a composite monad.

The "maybe monad" is essentially the equivalent of "nullable types" in Visual Basic / C#. It takes a non nullable type "T" and converts it into a "Nullable<T>", and then defines what all the binary operators mean on a Nullable<T>.

Side effects are represented simillarly. A structure is created that holds descriptions of side effects alongside a function's return value. The "lifted" operations then copy around side effects as values are passed between functions.

They are called "monads" rather than the easier-to-grasp name of "type operators" for several reasons:

After giving an answer to this question a few years ago, I believe I can improve and simplify that response with...

A monad is a function composition technique that externalizes treatment for some input scenarios using a composing function,

`bind`

, to pre-process input during composition.In normal composition, the function,

`compose (>>)`

, is use to apply the composed function to the result of its predecessor in sequence. Importantly, the function being composed is required to handle all scenarios of its input.`(x -> y) >> (y -> z)`

This design can be improved by restructuring the input so that relevant states are more easily interrogated. So, instead of simply

`y`

the value can become`Mb`

such as, for instance,`(is_OK, b)`

if`y`

included a notion of validity.For example, when the input is only possibly a number, instead of returning a string which can contain dutifully contain a number or not, you could restructure the type into a

`bool`

indicating the presence of a valid number and a number in tuple such as,`bool * float`

. The composed functions would now no longer need to parse an input string to determine whether a number exists but could merely inspect the`bool`

portion of a tuple.`(Ma -> Mb) >> (Mb -> Mc)`

Here, again, composition occurs naturally with

`compose`

and so each function must handle all scenarios of its input individually, though doing so is now much easier.However, what if we could externalize the effort of interrogation for those times where handling a scenario is routine. For example, what if our program does nothing when the input is not OK as in when

`is_OK`

is`false`

. If that were done then composed functions would not need to handle that scenario themselves, dramatically simplifying their code and effecting another level of reuse.To achieve this externalization we could use a function,

`bind (>>=)`

, to perform the`composition`

instead of`compose`

. As such, instead of simply transferring values from the output of one function to the input of another`Bind`

would inspect the`M`

portion of`Ma`

and decide whether and how to apply the composed function to the`a`

. Of course, the function`bind`

would be defined specifically for our particular`M`

so as to be able to inspect its structure and perform whatever type of application we want. Nonetheless, the`a`

can be anything since`bind`

merely passes the`a`

uninspected to the the composed function when it determines application necessary. Additionally, the composed functions themselves no longer need to deal with the`M`

portion of the input structure either, simplifying them. Hence...`(a -> Mb) >>= (b -> Mc)`

or more succinctly`Mb >>= (b -> Mc)`

In short, a monad externalizes and thereby provides standard behaviour around the treatment of certain input scenarios once the input becomes designed to sufficiently expose them. This design is a

`shell and content`

model where the shell contains data relevant to the application of the composed function and is interrogated by and remains only available to the`bind`

function.Therefore, a monad is three things:

`M`

shell for holding monad relevant information,`bind`

function implemented to make use of this shell information in its application of the composed functions to the content value(s) it finds within the shell, and`a -> Mb`

, producing results that include monadic management data.Generally speaking, the input to a function is far more restrictive than its output which may include such things as error conditions; hence, the

`Mb`

result structure is generally very useful. For instance, the division operator does not return a number when the divisor is`0`

.Additionally,

`monad`

s may include wrap functions that wrap values,`a`

, into the monadic type,`Ma`

, and general functions,`a -> b`

, into monadic functions,`a -> Mb`

, by wrapping their results after application. Of course, like`bind`

, such wrap functions are specific to`M`

. An example:The design of the

`bind`

function presumes immutable data structures and pure functions others things get complex and guarantees cannot be made. As such, there are monadic laws:Given...

Then...

`Associativity`

means that`bind`

preserves the order of evaluation regardless of when`bind`

is applied. That is, in the definition of`Associativity`

above, the force early evaluation of the parenthesized`binding`

of`f`

and`g`

will only result in a function that expects`Ma`

in order to complete the`bind`

. Hence the evaluation of`Ma`

must be determined before its value can become applied to`f`

and that result in turn applied to`g`

.Monads are to control flow what abstract data types are to data.

In other words, many developers are comfortable with the idea of Sets, Lists, Dictionaries (or Hashes, or Maps), and Trees. Within those data types there are many special cases (for instance InsertionOrderPreservingIdentityHashMap).

However, when confronted with program "flow" many developers haven't been exposed to many more constructs than if, switch/case, do, while, goto (grr), and (maybe) closures.

So, a monad is simply a control flow construct. A better phrase to replace monad would be 'control type'.

As such, a monad has slots for control logic, or statements, or functions - the equivalent in data structures would be to say that some data structures allow you to add data, and remove it.

For example, the "if" monad:

at its simplest has two slots - a clause, and a block. The

`if`

monad is usually built to evaluate the result of the clause, and if not false, evaluate the block. Many developers are not introduced to monads when they learn 'if', and it just isn't necessary to understand monads to write effective logic.Monads can become more complicated, in the same way that data structures can become more complicated, but there are many broad categories of monad that may have similar semantics, but differing implementations and syntax.

Of course, in the same way that data structures may be iterated over, or traversed, monads may be evaluated.

Compilers may or may not have support for user-defined monads. Haskell certainly does. Ioke has some similar capabilities, although the term monad is not used in the language.

My favorite Monad tutorial:

http://www.haskell.org/haskellwiki/All_About_Monads

(out of 170,000 hits on a Google search for "monad tutorial"!)

@Stu: The point of monads is to allow you to add (usually) sequential semantics to otherwise pure code; you can even compose monads (using Monad Transformers) and get more interesting and complicated combined semantics, like parsing with error handling, shared state, and logging, for example. All of this is possible in pure code, monads just allow you to abstract it away and reuse it in modular libraries (always good in programming), as well as providing convenient syntax to make it look imperative.

Haskell already has operator overloading[1]: it uses type classes much the way one might use interfaces in Java or C# but Haskell just happens to also allow non-alphanumeric tokens like + && and > as infix identifiers. It's only operator overloading in your way of looking at it if you mean "overloading the semicolon" [2]. It sounds like black magic and asking for trouble to "overload the semicolon" (picture enterprising Perl hackers getting wind of this idea) but the point is that without monads

there is no semicolon, since purely functional code does not require or allow explicit sequencing.This all sounds much more complicated than it needs to. sigfpe's article is pretty cool but uses Haskell to explain it, which sort of fails to break the chicken and egg problem of understanding Haskell to grok Monads and understanding Monads to grok Haskell.

[1] This is a separate issue from monads but monads use Haskell's operator overloading feature.

[2] This is also an oversimplification since the operator for chaining monadic actions is >>= (pronounced "bind") but there is syntactic sugar ("do") that lets you use braces and semicolons and/or indentation and newlines.

I've been thinking of Monads in a different way, lately. I've been thinking of them as abstracting out

execution orderin a mathematical way, which makes new kinds of polymorphism possible.If you're using an imperative language, and you write some expressions in order, the code ALWAYS runs exactly in that order.

And in the simple case, when you use a monad, it feels the same -- you define a list of expressions that happen in order. Except that, depending on which monad you use, your code might run in order (like in IO monad), in parallel over several items at once (like in the List monad), it might halt partway through (like in the Maybe monad), it might pause partway through to be resumed later (like in a Resumption monad), it might rewind and start from the beginning (like in a Transaction monad), or it might rewind partway to try other options (like in a Logic monad).

And because monads are polymorphic, it's possible to run the same code in different monads, depending on your needs.

Plus, in some cases, it's possible to combine monads together (with monad transformers) to get multiple features at the same time.

I am still new to monads, but I thought I would share a link I found that felt really good to read (WITH PICTURES!!): http://www.matusiak.eu/numerodix/blog/2012/3/11/monads-for-the-layman/ (no affiliation)

Basically, the warm and fuzzy concept that I got from the article was the concept that monads are basically adapters that allow disparate functions to work in a composable fashion, i.e. be able to string up multiple functions and mix and match them without worrying about inconsistent return types and such. So the BIND function is in charge of keeping apples with apples and oranges with oranges when we're trying to make these adapters. And the LIFT function is in charge of taking "lower level" functions and "upgrading" them to work with BIND functions and be composable as well.

I hope I got it right, and more importantly, hope that the article has a valid view on monads. If nothing else, this article helped whet my appetite for learning more about monads.

In addition to the excellent answers above, let me offer you a link to the following article (by Patrick Thomson) which explains monads by relating the concept to the JavaScript library

jQuery(and its way of using "method chaining" to manipulate the DOM): jQuery is a MonadThe jQuery documentation itself doesn't refer to the term "monad" but talks about the "builder pattern" which is probably more familiar. This doesn't change the fact that you have a proper monad there maybe without even realizing it.

Monads Are Not Metaphors, but a practically useful abstraction emerging from a common pattern, as Daniel Spiewak explains.

A monad is a way of combining computations together that share a common context. It is like building a network of pipes. When constructing the network, there is no data flowing through it. But when I have finished piecing all the bits together with 'bind' and 'return' then I invoke something like

`runMyMonad monad data`

and the data flows through the pipes.In practice, monad is a custom implementation of function composition operator that takes care of side effects and incompatible input and return values (for chaining).

If I've understood correctly, IEnumerable is derived from monads. I wonder if that might be an interesting angle of approach for those of us from the C# world?

For what it's worth, here are some links to tutorials that helped me (and no, I still haven't understood what monads are).

The two things that helped me best when learning about there were:

Chapter 8, "Functional Parsers," from Graham Hutton's book Programming in Haskell. This doesn't mention monads at all, actually, but if you can work through chapter and really understand everything in it, particularly how a sequence of bind operations is evaluated, you'll understand the internals of monads. Expect this to take several tries.

The tutorial All About Monads. This gives several good examples of their use, and I have to say that the analogy in Appendex I worked for me.

Monoid appears to be something that ensures that all operations defined on a Monoid and a supported type will always return a supported type inside the Monoid. Eg, Any number + Any number = A number, no errors.

Whereas division accepts two fractionals, and returns a fractional, which defined division by zero as Infinity in haskell somewhy(which happens to be a fractional somewhy)...

In any case, it appears Monads are just a way to ensure that your chain of operations behaves in a predictable way, and a function that claims to be Num -> Num, composed with another function of Num->Num called with x does not say, fire the missiles.

On the other hand, if we have a function which does fire the missiles, we can compose it with other functions which also fire the missiles, because our intent is clear -- we want to fire the missiles -- but it won't try printing "Hello World" for some odd reason.

In Haskell, main is of type IO (), or IO [()], the distiction is strange and I will not discuss it but here's what I think happens:

If I have main, I want it to do a chain of actions, the reason I run the program is to produce an effect -- usually though IO. Thus I can chain IO operations together in main in order to -- do IO, nothing else.

If I try to do something which does not "return IO", the program will complain that the chain does not flow, or basically "How does this relate to what we are trying to do -- an IO action", it appears to force the programmer to keep their train of thought, without straying off and thinking about firing the missiles, while creating algorithms for sorting -- which does not flow.

Basically, Monads appear to be a tip to the compiler that "hey, you know this function that returns a number here, it doesn't actually always work, it can sometimes produce a Number, and sometimes Nothing at all, just keep this in mind". Knowing this, if you try to assert a monadic action, the monadic action may act as a compile time exception saying "hey, this isn't actually a number, this CAN be a number, but you can't assume this, do something to ensure that the flow is acceptable." which prevents unpredictable program behavior -- to a fair extent.

It appears monads are not about purity, nor control, but about maintaining an identity of a category on which all behavior is predictable and defined, or does not compile. You cannot do nothing when you are expected to do something, and you cannot do something if you are expected to do nothing (visible).

The biggest reason I could think of for Monads is -- go look at Procedural/OOP code, and you will notice that you do not know where the program starts, nor ends, all you see is a lot of jumping and a lot of math,magic,and missiles. You will not be able to maintain it, and if you can, you will spend quite a lot of time wrapping your mind around the whole program before you can understand any part of it, because modularity in this context is based on interdependant "sections" of code, where code is optimized to be as related as possible for promise of efficiency/inter-relation. Monads are very concrete, and well defined by definition, and ensure that the flow of program is possible to analyze, and isolate parts which are hard to analyze -- as they themselves are monads. A monad appears to be a "comprehensible unit which is predictable upon its full understanding" -- If you understand "Maybe" monad, there's no possible way it will do anything except be "Maybe", which appears trivial, but in most non monadic code, a simple function "helloworld" can fire the missiles, do nothing, or destroy the universe or even distort time -- we have no idea nor have any guarantees that IT IS WHAT IT IS. A monad GUARANTEES that IT IS WHAT IT IS. which is very powerful.

All things in "real world" appear to be monads, in the sense that it is bound by definite observable laws preventing confusion. This does not mean we have to mimic all the operations of this object to create classes, instead we can simply say "a square is a square", nothing but a square, not even a rectangle nor a circle, and "a square has area of the length of one of it's existing dimensions multiplied by itself. No matter what square you have, if it's a square in 2D space, it's area absolutely cannot be anything but its length squared, it's almost trivial to prove. This is very powerful because we do not need to make assertions to make sure that our world is the way it is, we just use implications of reality to prevent our programs from falling off track.

Im pretty much guaranteed to be wrong but I think this could help somebody out there, so hopefully it helps somebody.

In the context of Scala you will find the following to be the simplest definition. Basically flatMap (or bind) is 'associative' and there exists an identity.

E.g.

NOTEStrictly speaking the definition of a Monad in functional programming is not the same as the definition of a Monad in Category Theory, which is defined in turns of`map`

and`flatten`

. Though they are kind of equivalent under certain mappings. This presentations is very good: http://www.slideshare.net/samthemonad/monad-presentation-scala-as-a-categoryThis answer begins with a motivating example, works through the example, derives an example of a monad, and formally defines "monad".

Consider these three functions in pseudocode:

`f`

takes an ordered pair of the form`<x, messages>`

and returns an ordered pair. It leaves the first item untouched and appends`"called f. "`

to the second item. Same with`g`

.You can compose these functions and get your original value, along with a string that shows which order the functions were called in:

You dislike the fact that

`f`

and`g`

are responsible for appending their own log messages to the previous logging information. (Just imagine for the sake of argument that instead of appending strings,`f`

and`g`

must perform complicated logic on the second item of the pair. It would be a pain to repeat that complicated logic in two -- or more -- different functions.)You prefer to write simpler functions:

But look at what happens when you compose them:

The problem is that

passinga pair into a function does not give you what you want. But what if you couldfeeda pair into a function:Read

`feed(f, m)`

as "feed`m`

into`f`

". Tofeeda pair`<x, messages>`

into a function`f`

is topass`x`

into`f`

, get`<y, message>`

out of`f`

, and return`<y, messages message>`

.Notice what happens when you do three things with your functions:

First: if you wrap a value and then

feedthe resulting pair into a function:That is the same as

passingthe value into the function.Second: if you feed a pair into

`wrap`

:That does not change the pair.

Third: if you define a function that takes

`x`

and feeds`g(x)`

into`f`

:and feed a pair into it:

That is the same as feeding the pair into

`g`

and feeding the resulting pair into`f`

.You have most of a monad. Now you just need to know about the data types in your program.

What type of value is

`<x, "called f. ">`

? Well, that depends on what type of value`x`

is. If`x`

is of type`t`

, then your pair is a value of type "pair of`t`

and string". Call that type`M t`

.`M`

is a type constructor:`M`

alone does not refer to a type, but`M _`

refers to a type once you fill in the blank with a type. An`M int`

is a pair of an int and a string. An`M string`

is a pair of a string and a string. Etc.Congratulations, you have created a monad!Formally, your monad is the tuple

`<M, feed, wrap>`

.A monad is a tuple

`<M, feed, wrap>`

where:`M`

is a type constructor.`feed`

takes a (function that takes a`t`

and returns an`M u`

) and an`M t`

and returns an`M u`

.`wrap`

takes a`v`

and returns an`M v`

.`t`

,`u`

, and`v`

are any three types that may or may not be the same. A monad satisfies the three properties you proved for your specific monad:Feedinga wrapped`t`

into a function is the same aspassingthe unwrapped`t`

into the function.Formally:

`feed(f, wrap(x)) = f(x)`

Feeding an

`M t`

into`wrap`

does nothing to the`M t`

.Formally:

`feed(wrap, m) = m`

Feeding an

`M t`

(call it`m`

) into a function that`t`

into`g`

`M u`

(call it`n`

) from`g`

`n`

into`f`

is the same as

`m`

into`g`

`n`

from`g`

`n`

into`f`

Formally:

`feed(h, m) = feed(f, feed(g, m))`

where`h(x) := feed(f, g(x))`

Typically,

`feed`

is called`bind`

(AKA`>>=`

in Haskell) and`wrap`

is called`return`

.I will try to explain

`Monad`

in the context of Haskell.In functional programming, function composition is important. It allows our program to consist of small, easy-to-read functions.

Let's say we have two functions:

`g :: Int -> String`

and`f :: String -> Bool`

.We can do

`(f . g) x`

, which is just the same as`f (g x)`

, where`x`

is an`Int`

value.When doing composition/applying the result of one function to another, having the types match up is important. In the above case, the type of the result returned by

`g`

must be the same as the type accepted by`f`

.But sometimes values are in contexts, and this makes it a bit less easy to line up types. (Having values in contexts is very useful. For example, the

`Maybe Int`

type represents an`Int`

value that may not be there, the`IO String`

type represents a`String`

value that is there as a result of performing some side effects.)Let's say we now have

`g1 :: Int -> Maybe String`

and`f1 :: String -> Maybe Bool`

.`g1`

and`f1`

are very similar to`g`

and`f`

respectively.We can't do

`(f1 . g1) x`

or`f1 (g1 x)`

, where`x`

is an`Int`

value. The type of the result returned by`g1`

is not what`f1`

expects.We could compose

`f`

and`g`

with the`.`

operator, but now we can't compose`f1`

and`g1`

with`.`

. The problem is that we can't straightforwardly pass a value in a context to a function that expects a value that is not in a context.Wouldn't it be nice if we introduce an operator to compose

`g1`

and`f1`

, such that we can write`(f1 OPERATOR g1) x`

?`g1`

returns a value in a context. The value will be taken out of context and applied to`f1`

. And yes, we have such an operator. It's`<=<`

.We also have the

`>>=`

operator that does for us the exact same thing, though in a slightly different syntax.We write:

`g1 x >>= f1`

.`g1 x`

is a`Maybe Int`

value. The`>>=`

operator helps take that`Int`

value out of the "perhaps-not-there" context, and apply it to`f1`

. The result of`f1`

, which is a`Maybe Bool`

, will be the result of the entire`>>=`

operation.And finally, why is

`Monad`

useful? Because`Monad`

is the type class that defines the`>>=`

operator, very much the same as the`Eq`

type class that defines the`==`

and`/=`

operators.To conclude, the

`Monad`

type class defines the`>>=`

operator that allows us to pass values in a context (we call these monadic values) to functions that don't expect values in a context. The context will be taken care of.If there is one thing to remember here, it is that

.`Monad`

s allow function composition that involves values in contexts## tl;dr

## Prologue

The application operator

`$`

of functionsis canonically defined

in terms of Haskell-primitive function application

`f x`

(`infixl 10`

).Composition

`.`

is defined in terms of`$`

asand satisfies the equivalences

`forall f g h.`

`.`

is associative, and`id`

is its right and left identity.## The Kleisli triple

In programming, a monad is a functor type constructor with an instance of the monad type class. There are several equivalent variants of definition and implementation, each carrying slightly different intuitions about the monad abstraction.

A functor is a type constructor

`f`

of kind`* -> *`

with an instance of the functor type class.In addition to following statically enforced type protocol, instances of the functor type class must obey the algebraic

functor laws`forall f g.`

Functor

computationshave the typeA computation

`c r`

consists inresults`r`

withincontext`c`

.Unary monadic functions or

Kleisli arrowshave the typeKleisi arrows are functions that take one argument

`a`

and return a monadic computation`m b`

.Monads are canonically defined in terms of the

Kleisli triple`forall m. Functor m =>`

implemented as the type class

The

Kleisli identity`return`

is a Kleisli arrow that promotes a value`t`

into monadic context`m`

.ExtensionorKleisli application`=<<`

applies a Kleisli arrow`a -> m b`

to results of a computation`m a`

.Kleisli composition`<=<`

is defined in terms of extension as`<=<`

composes two Kleisli arrows, applying the left arrow to results of the right arrow’s application.Instances of the monad type class must obey the

monad laws, most elegantly stated in terms of Kleisli composition:`forall f g h.`

`<=<`

is associative, and`return`

is its right and left identity.## Identity

The identity type

is the identity function on types

Interpreted as a functor,

In canonical Haskell, the identity monad is defined

## Option

An option type

encodes computation

`Maybe t`

that not necessarily yields a result`t`

, computation that may “fail”. The option monad is defined`a -> Maybe b`

is applied to a result only if`Maybe a`

yields a result.The natural numbers can be encoded as those integers greater than or equal to zero.

The natural numbers are not closed under subtraction.

The option monad covers a basic form of exception handling.

## List

The list monad, over the list type

and its additive monoid operation “append”

encodes

nonlinearcomputation`[t]`

yielding a natural amount`0, 1, ...`

of results`t`

.Extension

`=<<`

concatenates`++`

all lists`[b]`

resulting from applications`f x`

of a Kleisli arrow`a -> [b]`

to elements of`[a]`

into a single result list`[b]`

.Let the proper divisors of a positive integer

`n`

bethen

In defining the monad type class, instead of extension

`=<<`

, the Haskell standard uses its flip, thebindoperator`>>=`

.For simplicity's sake, this explanation uses the type class hierarchy

In Haskell, the current standard hierarchy is

because not only is every monad a functor, but every applicative is a functor and every monad is an applicative, too.

Using the list monad, the imperative pseudocode

roughly translates to the

do block,the equivalent

monad comprehension,and the expression

Do notation and monad comprehensions are syntactic sugar for nested bind expressions. The bind operator is used for local name binding of monadic results.

where

The guard function is defined

where the

unit typeor “empty tuple”Additive monadsthat supportchoiceandfailurecan be abstracted over using a type classwhere

`fail`

and`<|>`

form a monoid`forall k l m.`

and

`fail`

is the absorbing/annihilating zero element of additive monadsIf in

`even p`

is true, then the guard produces`[()]`

, and, by the definition of`>>`

, the local constant functionis applied to the result

`()`

. If false, then the guard produces the list monad’s`fail`

(`[]`

), which yields no result for a Kleisli arrow to be applied`>>`

to, so this`p`

is skipped over.## State

Infamously, monads are used to encode stateful computation.

A

state processoris a functionthat transitions a state

`st`

and yields a result`t`

. Thestate`st`

can be anything. Nothing, flag, count, array, handle, machine, world.The type of state processors is usually called

The state processor monad is the kinded

`* -> *`

functor`State st`

. Kleisli arrows of the state processor monad are functionsIn canonical Haskell, the lazy version of the state processor monad is defined

A state processor is run by supplying an initial state:

State access is provided by primitives

`get`

and`put`

, methods of abstraction overstatefulmonads:`m -> st`

declares afunctional dependencyof the state type`st`

on the monad`m`

; that a`State t`

, for example, will determine the state type to be`t`

uniquely.with the unit type used analogously to

`void`

in C.`gets`

is often used with record field accessors.The state monad equivalent of the variable threading

where

`s0 :: Int`

, is the equally referentially transparent, but infinitely more elegant and practical`modify (+ 1)`

is a computation of type`State Int ()`

, except for itseffectequivalent to`return ()`

.The monad law of associativity can be written in terms of

`>>=`

`forall m f g.`

or

Like in expression-oriented programming (e.g. Rust), the last statement of a block represents its yield. The bind operator is sometimes called a “programmable semicolon”.

Iteration control structure primitives from structured imperative programming are emulated monadically

## Input/Output

The I/O world state processor monad is a reconciliation of pure Haskell and the real world, of functional denotative and imperative operational semantics. A close analogue of the actual strict implementation:

Interaction is facilitated by impure primitives

The impurity of code that uses

`IO`

primitives is permanently protocolized by the type system. Because purity is awesome, what happens in`IO`

, stays in`IO`

.Or, at least, should.

The type signature of a Haskell program

expands to

A function that transforms a world.

## Epilogue

The category whiches objects are Haskell types and whiches morphisms are functions between Haskell types is, “fast and loose”, the category

`Hask`

.A functor

`T`

is a mapping from a category`C`

to a category`D`

; for each object in`C`

an object in`D`

and for each morphism in

`C`

a morphism in`D`

where

`X`

,`Y`

are objects in`C`

.`HomC(X, Y)`

is thehomomorphism classof all morphisms`X -> Y`

in`C`

. The functor must preserve morphism identity and composition, the “structure” of`C`

, in`D`

.The

Kleisli categoryof a category`C`

is given by a Kleisli tripleof an endofunctor

(

`f`

), an identity morphism`eta`

(`return`

), and an extension operator`*`

(`=<<`

).Each Kleisli morphism in

`Hask`

by the extension operator

is given a morphism in

`Hask`

’s Kleisli categoryComposition in the Kleisli category

`.T`

is given in terms of extensionand satisfies the

category axiomswhich, applying the equivalence transformations

in terms of extension are canonically given

Monads can also be defined in terms not of Kleislian extension, but a natural transformation

`mu`

, in programming called`join`

. A monad is defined in terms of`mu`

as a triple over a category`C`

, of an endofunctorand two natural tranformations

satisfying the equivalences

The monad type class is then defined

The canonical

`mu`

implementation of the option monad:The

`concat`

functionis the

`join`

of the list monad.Implementations of

`join`

can be translated from extension form using the equivalenceThe reverse translation from

`mu`

to extension form is given byPhilip Wadler:

Monads for functional programmingSimon L Peyton Jones, Philip Wadler:

Imperative functional programmingJonathan M. D. Hill, Keith Clarke:

An introduction to category theory, category theory monads, and their relationship to functional programming´Kleisli category

Eugenio Moggi:

Notions of computation and monadsWhat a monad is not

from

Generalising Monads to Arrowsby John HughesWhat the world needs is another monad blog post, but I think this is useful in identifying existing monads in the wild.

http://code.google.com/p/monad-tutorial/ is a work in progress to address exactly this question.

A monad is a thing used to encapsulate objects that have changing state. It is most often encountered in languages that otherwise do not allow you to have modifiable state (e.g., Haskell).

An example would be for file I/O.

You would be able to use a monad for file I/O to isolate the changing state nature to just the code that used the Monad. The code inside the Monad can effectively ignore the changing state of the world outside the Monad - this makes it a lot easier to reason about the overall effect of your program.

Let the below "

`{| a |m}`

" represent some piece of monadic data. A data type which advertises an`a`

:Function,

`f`

, knows how to create a monad, if only it had an`a`

:Here we see function,

`f`

, tries to evaluate a monad but gets rebuked.Funtion,

`f`

, finds a way to extract the`a`

by using`>>=`

.Little does

`f`

know, the monad and`>>=`

are in collusion.But what do they actually talk about? Well, that depends on the monad. Talking solely in the abstract has limited use; you have to have some experience with particular monads to flesh out the understanding.

For instance, the data type Maybe

has a monad instance which will acts like the following...

Wherein, if the case is

`Just a`

But for the case of

`Nothing`

So the Maybe monad lets a computation continue if it actually contains the

`a`

it advertises, but aborts the computation if it doesn't. The result, however is still a piece of monadic data, though not the output of`f`

. For this reason, the Maybe monad is used to represent the context of failure.Different monads behave differently. Lists are other types of data with monadic instances. They behave like the following:

In this case, the function knew how to make a list from it's input, but didn't know what to do with extra input and extra lists. The bind

`>>=`

, helped`f`

out by combining the multiple outputs. I include this example to show that while`>>=`

is responsible for extracting`a`

, it also has access to the eventual bound output of`f`

. Indeed, it will never extract any`a`

unless it knows the eventual output has the same type of context.There are other monads which are used to represent different contexts. Here's some characterizations of a few more. The

`IO`

monad doesn't actually have an`a`

, but it knows a guy and will get that`a`

for you. The`State st`

monad has a secret stash of`st`

that it will pass to`f`

under the table, even though`f`

just came asking for an`a`

. The`Reader r`

monad is similar to`State st`

, although it only lets`f`

look at`r`

.The point in all this is that any type of data which is declared itself to be a Monad is declaring some sort of context around extracting a value from the monad. The big gain from all this? Well, its easy enough to couch a calculation with some sort of context. It can get messy, however, when stringing together multiple context laden calculations. The monad operations take care of resolving the interactions of context so that the programmer doesn't have to.

Note, that use of the

`>>=`

eases a mess by by taking some of the autonomy away from`f`

. That is, in the above case of`Nothing`

for instance,`f`

no longer gets to decide what to do in the case of`Nothing`

; it's encoded in`>>=`

. This is the trade off. If it was necessary for`f`

to decide what to do in the case of`Nothing`

, then`f`

should have been a function from`Maybe a`

to`Maybe b`

. In this case,`Maybe`

being a monad is irrelevant.Note, however, that sometimes a data type does not export it's constructors (looking at you IO), and if we want to work with the advertised value we have little choice but to work with it's monadic interface.