What is a monad?

295,047

Solution 1

First: The term monad is a bit vacuous if you are not a mathematician. An alternative term is computation builder which is a bit more descriptive of what they are actually useful for.

They are a pattern for chaining operations. It looks a bit like method chaining in object-oriented languages, but the mechanism is slightly different.

The pattern is mostly used in functional languages (especially Haskell which uses monads pervasively) but can be used in any language which support higher-order functions (that is, functions which can take other functions as arguments).

Arrays in JavaScript support the pattern, so let’s use that as the first example.

The gist of the pattern is we have a type (Array in this case) which has a method which takes a function as argument. The operation supplied must return an instance of the same type (i.e. return an Array).

First an example of method chaining which does not use the monad pattern:

[1,2,3].map(x => x + 1)

The result is [2,3,4]. The code does not conform to the monad pattern, since the function we are supplying as an argument returns a number, not an Array. The same logic in monad form would be:

[1,2,3].flatMap(x => [x + 1])

Here we supply an operation which returns an Array, so now it conforms to the pattern. The flatMap method executes the provided function for every element in the array. It expects an array as result for each invocation (rather than single values), but merges the resulting set of arrays into a single array. So the end result is the same, the array [2,3,4].

(The function argument provided to a method like map or flatMap is often called a "callback" in JavaScript. I will call it the "operation" since it is more general.)

If we chain multiple operations (in the traditional way):

[1,2,3].map(a => a + 1).filter(b => b != 3)

Results in the array [2,4]

The same chaining in monad form:

[1,2,3].flatMap(a => [a + 1]).flatMap(b => b != 3 ? [b] : [])

Yields the same result, the array [2,4].

You will immediately notice that the monad form is quite a bit uglier than the non-monad! This just goes to show that monads are not necessarily “good”. They are a pattern which is sometimes beneficial and sometimes not.

Do note that the monad pattern can be combined in a different way:

[1,2,3].flatMap(a => [a + 1].flatMap(b => b != 3 ? [b] : []))

Here the binding is nested rather than chained, but the result is the same. This is an important property of monads as we will see later. It means two operations combined can be treated the same as a single operation.

The operation is allowed to return an array with different element types, for example transforming an array of numbers into an array of strings or something else; as long as it still an Array.

This can be described a bit more formally using Typescript notation. An array has the type Array<T>, where T is the type of the elements in the array. The method flatMap() takes a function argument of the type T => Array<U> and returns an Array<U>.

Generalized, a monad is any type Foo<Bar> which has a "bind" method which takes a function argument of type Bar => Foo<Baz> and returns a Foo<Baz>.

This answers what monads are. The rest of this answer will try to explain through examples why monads can be a useful pattern in a language like Haskell which has good support for them.

Haskell and Do-notation

To translate the map/filter example directly to Haskell, we replace flatMap with the >>= operator:

[1,2,3] >>= \a -> [a+1] >>= \b -> if b == 3 then [] else [b] 

The >>= operator is the bind function in Haskell. It does the same as flatMap in JavaScript when the operand is a list, but it is overloaded with different meaning for other types.

But Haskell also has a dedicated syntax for monad expressions, the do-block, which hides the bind operator altogether:

 do a <- [1,2,3] 
    b <- [a+1] 
    if b == 3 then [] else [b] 

This hides the "plumbing" and lets you focus on the actual operations applied at each step.

In a do-block, each line is an operation. The constraint still holds that all operations in the block must return the same type. Since the first expression is a list, the other operations must also return a list.

The back-arrow <- looks deceptively like an assignment, but note that this is the parameter passed in the bind. So, when the expression on the right side is a List of Integers, the variable on the left side will be a single Integer – but will be executed for each integer in the list.

Example: Safe navigation (the Maybe type)

Enough about lists, lets see how the monad pattern can be useful for other types.

Some functions may not always return a valid value. In Haskell this is represented by the Maybe-type, which is an option that is either Just value or Nothing.

Chaining operations which always return a valid value is of course straightforward:

streetName = getStreetName (getAddress (getUser 17)) 

But what if any of the functions could return Nothing? We need to check each result individually and only pass the value to the next function if it is not Nothing:

case getUser 17 of
      Nothing -> Nothing 
      Just user ->
         case getAddress user of
            Nothing -> Nothing 
            Just address ->
              getStreetName address

Quite a lot of repetitive checks! Imagine if the chain was longer. Haskell solves this with the monad pattern for Maybe:

do
  user <- getUser 17
  addr <- getAddress user
  getStreetName addr

This do-block invokes the bind-function for the Maybe type (since the result of the first expression is a Maybe). The bind-function only executes the following operation if the value is Just value, otherwise it just passes the Nothing along.

Here the monad-pattern is used to avoid repetitive code. This is similar to how some other languages use macros to simplify syntax, although macros achieve the same goal in a very different way.

Note that it is the combination of the monad pattern and the monad-friendly syntax in Haskell which result in the cleaner code. In a language like JavaScript without any special syntax support for monads, I doubt the monad pattern would be able to simplify the code in this case.

Mutable state

Haskell does not support mutable state. All variables are constants and all values immutable. But the State type can be used to emulate programming with mutable state:

add2 :: State Integer Integer
add2 = do
        -- add 1 to state
         x <- get
         put (x + 1)
         -- increment in another way
         modify (+1)
         -- return state
         get


evalState add2 7
=> 9

The add2 function builds a monad chain which is then evaluated with 7 as the initial state.

Obviously this is something which only makes sense in Haskell. Other languages support mutable state out of the box. Haskell is generally "opt-in" on language features - you enable mutable state when you need it, and the type system ensures the effect is explicit. IO is another example of this.

IO

The IO type is used for chaining and executing “impure” functions.

Like any other practical language, Haskell has a bunch of built-in functions which interface with the outside world: putStrLine, readLine and so on. These functions are called “impure” because they either cause side effects or have non-deterministic results. Even something simple like getting the time is considered impure because the result is non-deterministic – calling it twice with the same arguments may return different values.

A pure function is deterministic – its result depends purely on the arguments passed and it has no side effects on the environment beside returning a value.

Haskell heavily encourages the use of pure functions – this is a major selling point of the language. Unfortunately for purists, you need some impure functions to do anything useful. The Haskell compromise is to cleanly separate pure and impure, and guarantee that there is no way that pure functions can execute impure functions, directly or indirect.

This is guaranteed by giving all impure functions the IO type. The entry point in Haskell program is the main function which have the IO type, so we can execute impure functions at the top level.

But how does the language prevent pure functions from executing impure functions? This is due to the lazy nature of Haskell. A function is only executed if its output is consumed by some other function. But there is no way to consume an IO value except to assign it to main. So if a function wants to execute an impure function, it has to be connected to main and have the IO type.

Using monad chaining for IO operations also ensures that they are executed in a linear and predictable order, just like statements in an imperative language.

This brings us to the first program most people will write in Haskell:

main :: IO ()
main = do 
        putStrLn ”Hello World”

The do keyword is superfluous when there is only a single operation and therefore nothing to bind, but I keep it anyway for consistency.

The () type means “void”. This special return type is only useful for IO functions called for their side effect.

A longer example:

main = do
    putStrLn "What is your name?"
    name <- getLine
    putStrLn "hello" ++ name

This builds a chain of IO operations, and since they are assigned to the main function, they get executed.

Comparing IO with Maybe shows the versatility of the monad pattern. For Maybe, the pattern is used to avoid repetitive code by moving conditional logic to the binding function. For IO, the pattern is used to ensure that all operations of the IO type are sequenced and that IO operations cannot "leak" to pure functions.

Summing up

In my subjective opinion, the monad pattern is only really worthwhile in a language which has some built-in support for the pattern. Otherwise it just leads to overly convoluted code. But Haskell (and some other languages) have some built-in support which hides the tedious parts, and then the pattern can be used for a variety of useful things. Like:

  • Avoiding repetitive code (Maybe)
  • Adding language features like mutable state or exceptions for delimited areas of the program.
  • Isolating icky stuff from nice stuff (IO)
  • Embedded domain-specific languages (Parser)
  • Adding GOTO to the language.

Solution 2

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 heck would 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 one kind of monad. Why? How?

Well, the bind function can just 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: Every useful monad does something else in addition to just being a monad. Every useful monad has a "special power", which makes it unique.

(A monad that does nothing special 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 Monad or Exception 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 a third exception 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. (The List Monad.)

  • As well as passing a "result" from one step to another, you can have the bind function pass extra data around 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. (The Reader 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. (The State Monad and its cousin the Writer Monad.)

  • Because you're only simulating destructive updates, you can trivially do things that would be impossible with real destructive updates. For example, you can undo the last update, or revert 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 without monads too. It's just drastically easier using monads.

Solution 3

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:

data Wrapped a = Wrap a

To wrap stuff we define

return :: a -> Wrapped a
return x = Wrap x

To perform operations without unwrapping, say you have a function f :: a -> b, then you can do this to lift that function to act on wrapped values:

fmap :: (a -> b) -> (Wrapped a -> Wrapped b)
fmap f (Wrap x) = Wrap (f x)

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 :: (a -> Wrapped b) -> (Wrapped a -> Wrapped b)
bind f (Wrap x) = f x

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.

Solution 4

But, You could have invented Monads!

sigfpe says:

But all of these introduce monads as something esoteric in need of explanation. But what I want to argue is that they aren't esoteric at all. In fact, faced with various problems in functional programming you would have been led, inexorably, to certain solutions, all of which are examples of monads. In fact, I hope to get you to invent them now if you haven't already. It's then a small step to notice that all of these solutions are in fact the same solution in disguise. And after reading this, you might be in a better position to understand other documents on monads because you'll recognise everything you see as something you've already invented.

Many of the problems that monads try to solve are related to the issue of side effects. So we'll start with them. (Note that monads let you do more than handle side-effects, in particular many types of container object can be viewed as monads. Some of the introductions to monads find it hard to reconcile these two different uses of monads and concentrate on just one or the other.)

In an imperative programming language such as C++, functions behave nothing like the functions of mathematics. For example, suppose we have a C++ function that takes a single floating point argument and returns a floating point result. Superficially it might seem a little like a mathematical function mapping reals to reals, but a C++ function can do more than just return a number that depends on its arguments. It can read and write the values of global variables as well as writing output to the screen and receiving input from the user. In a pure functional language, however, a function can only read what is supplied to it in its arguments and the only way it can have an effect on the world is through the values it returns.

Solution 5

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 of parametric polymorphism.)

In Haskell notation, the monad interface is written

class Monad m where
  return :: a -> m a
  (>>=) :: forall a b . m a -> (a -> m b) -> m b

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:

instance Monad [ ] where
    []     >>= k = []
    (x:xs) >>= k = k x ++ (xs >>= k)
    return x     = [x]

instance Monad Maybe where
    Just x  >>= k = k x
    Nothing >>= k = Nothing
    return x      = Just x

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.

Share:
295,047
ljs
Author by

ljs

I'm a software developer living in Guildford trying hard to suck less every year. I've worked with coffeescript, javascript, HTML, C#, F#, SQL, Go, C, and C++. The Q&amp;A's on this site are quite grossly out of date so I hereby disclaim that my views now may not reflect those expressed by this account, etc. etc. :-P

Updated on November 18, 2021

Comments

  • ljs
    ljs over 2 years

    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.

    • P Shved
      P Shved about 14 years
      Eric Lippert wrote an answer to this questions (stackoverflow.com/questions/2704652/…), which is due to some issues lives in a separate page.
    • tina Miller
      tina Miller about 13 years
      Here's a new introduction using javascript - I found it very readable.
    • Petr
      Petr over 11 years
    • cibercitizen1
      cibercitizen1 over 10 years
    • cibercitizen1
      cibercitizen1 about 10 years
      A monad is an array of functions with helper operations. See this answer
    • Calpau
      Calpau about 10 years
      Another quick article that resembles the sigfpe answer: github.com/quchen/articles/blob/master/…
    • haroldcarr
      haroldcarr almost 10 years
      See my FPComplete article on the WHY you would want to use monads (not how they work) : bit.ly/MH9zRD
    • ApproachingDarknessFish
      ApproachingDarknessFish almost 10 years
      The best explanation I've heard so far actually comes from wikipedia; "Monads are programmable semicolons."
    • LiweiZ
      LiweiZ over 9 years
      I came across this article: stephanboyer.com/post/9/monads-part-1-a-design-pattern. I found it is the best and most efficient concept delivery to layman like me so far. The author actually has other articles for monad as well.
    • Nick Budden
      Nick Budden almost 9 years
      An extremely easy-to-follow explanation by Douglas Crockford: youtube.com/watch?v=dkZFtimgAcM
    • Palec
      Palec over 8 years
      Monad is not a Haskell-specific concept, @HelderPereira. Your retag seems wrong to me.
    • Helder Pereira
      Helder Pereira over 8 years
      @Palec I know, but the question mentions Haskell and the top-voted answer uses Haskell to explain it. I just thought it would be useful to make it easier for people who are learning Haskell to find it, as this is a very important concept of the language. You are free though to remove it, if you think it doesn't make sense.
    • epicdig07
      epicdig07 about 7 years
      I like to say that monad is a construction which lets you put all your crap in (doing actual job, aka side effects), and present you a fancy box to keep your code functionnal (read side effect free).
    • Will Ness
      Will Ness over 5 years
      Monad is EDSL. See this: "Someone at some point noticed, "oh, in order to get impure effects from pure code I need to do metaprogramming, which means one of my types needs to be 'programs which compute an X'. I want to take a 'program that computes an X' and a function which takes an X and produces the next program, a 'program that computes a Y', and somehow glue them together into a 'program which computes a Y' " (which is the bind operation). The IO monad was born."
    • dbaltor
      dbaltor over 5 years
      This Tomasz Nurkiewicz's article is the best explanation aiming at Java developers I've found.
    • Will Ness
      Will Ness over 3 years
      shorter: Monad is (Embedded) Interpreter Pattern (where statements of the language being interpreted are first-class values of our programming language itself).
    • aoeu256
      aoeu256 about 3 years
      Monads are embedded domain specific languages that have statements (semicolons). The semicolons could represent statements in traditional procedural languages, joins in SQL, or whatever you want. Since you can merge monads to make languages with richer semantics, or split monads, you can also think of monads as being the SEMANTICS of the languages themselves.
  • Matthias Benkard
    Matthias Benkard over 15 years
    -> is right-associative, mirroring function application, which is left-associative, so leaving the parentheses out doesn't make a difference here.
  • Rabarberski
    Rabarberski about 15 years
    Your explanation did the trick for me. I would have added though a limited summing of some standard monads (reader, state, maybe, ...) to illustrate some practical uses and wrappings
  • Jared Updike
    Jared Updike almost 15 years
    The only problem with Wadler's paper is the notation is different but I agree that the paper is pretty compelling and a clear concise motivation for applying monads.
  • Jared Updike
    Jared Updike almost 15 years
    Monads weren't 'designed', they were applied from one domain (category theory) to another (I/O in purely functional programming languages). Did Newton 'design' the calculus?
  • Jared Updike
    Jared Updike almost 15 years
    Point 1 and 2 above are correct and useful. Points 4 and 5 are sort of ad hominem, even if more or less true. They don't really help explain monads.
  • Jared Updike
    Jared Updike almost 15 years
    The Maybe monad is not really equivalent to Nullable<T>. More accurately I would say the type Maybe T is equivalent to Nullable<T> more or less (except all types in Haskell are non-nullable by default (useful! e.g. all strings are always non-null); in C# Nullable<string> for example doesn't compile since it is always nullable whether you like it or not) but the use of Maybe as a Monad is more general than what you might do in C# with Nullable<T>; any code using monads can use the Maybe monad but no equivalent abstraction is exists and is idiomatic in C#.
  • Jared Updike
    Jared Updike almost 15 years
    Re: 4, 5: The "Secret handshake" thing is a red herring. Programming is full of jargon. Haskell just happens to call stuff what it is without pretending to rediscover something. If it exists in mathematics already, why make up a new name for it? The name is really not the reason people don't get monads; they are a subtle concept. The average person probably understands addition and multiplication, why don't they get the concept of an Abelian Group? Because it is more abstract and general and that person hasn't done the work to wrap their head around the concept. A name change wouldn't help.
  • Jared Updike
    Jared Updike almost 15 years
    "Finally it will provide support for combining itself with another monad to produce a composite monad." Are you talking about >>= or monad transformers? Are you saying "a monad provides a way for combining two monadic actions of the same type (bind or >==)"? or "a monad usually provides a way of layering itself with other monads as in monad transformers"? Your language here is sloppy and confusing, possibly misleading.
  • Scott Wisniewski
    Scott Wisniewski almost 15 years
    Sigh... I'm not making an attack on Haskell ... I was making a joke. So, I don't really get the bit about being "ad hominem". Yes, the calculus was "designed". That's why, for example, calculus students are taught the Leibniz notation, rather than the icky stuff Netwton used. Better design. Good names help understanding a lot. If I called Abelian Groups "distended wrinkle pods", you may have trouble understanding me. You might be saying "but that name is nonsense", no one would ever call them that. To people who have never heard of category theory "monad" sounds like nonsense.
  • Jared Updike
    Jared Updike over 14 years
    @Scott: sorry if my extensive comments made it seem I was getting defensive about Haskell. I enjoy your humor about the secret handshake and you will note I said it is more or less true. :-) If you called Abelian Groups "distended wrinkle pods" you would be making the same mistake of trying to give monads a "better name" (cf. F# "computation expressions"): the term exists and people who care know what monads are, but not what "warm fuzzy things" are (or "computation expressions"). If I understand your use of the term "type operator" correctly there are lots of other type operators than monads.
  • Breton
    Breton over 14 years
    As someone who has had a great deal of problems understanding monads, I can say that this answer helped.. a little. However, there's still some things that I don't understand. In what way is the list comprehension a monad? Is there an expanded form of that example? Another thing that really bothers me about most monad explanations, including this one- Is that they keep mixing up "what is a monad?" with "what is a monad good for?" and "How is a monad implemented?". you jumped that shark when you wrote "A monad is basically just a type that supports the >>= operator." Which just had me...
  • Breton
    Breton over 14 years
    Scratching my head. That seems like an implementation detail, and it doesn't really help me answer the question "Why should I use a monad". It may be true, but the explanation up until that point didn't prepare me for it. I wasn't thinking " Why that's a tricky problem indeed, why, what I would need for that is some kind of type which supports the >>= operator. Oh hey, turns out that's what a monad is!" nope, that wasn't running through my head, because I didn't know what a >>= operator was, or what THAT's good for, nor was I presented with a problem that it solves.
  • Breton
    Breton over 14 years
    You made up for it later of course, a little bit, but I'm afraid I still don't know the answer to the question "What is a monad", let alone a simple succinct explanation.
  • Breton
    Breton over 14 years
    Also I disagree with your conclusion about why monads are hard. If monads themselves aren't complex, then you should be able to explain what they are without a bunch of baggage. I don't want to know about the implementation when I ask the question "What is a monad", I want to know what itch it's meant to be scratching. So far it seems like the answer is "Because the authors of haskell are sadomasochists and decided that you should do something stupidly complex to accomplish simple things, so you HAVE to learn monads to use haskell, not because they're in any way useful in themselves"...
  • Breton
    Breton over 14 years
    But.. that can't be right, can it? I think monads are hard because nobody can seem to figure out how to explain them without getting caught up in confusing implementation details. I mean.. what is a school bus? It's a metal platform with a device in the front which consumes a refined petroleum product to drive in a cycle some metallic pistons, which in turn rotate a crank shaft attached to some gears which drive some wheels. The wheels have inflated rubber bags around them which interface with an ashphalt surface to cause a collection of seats to move forward. The seats move forward because...
  • Martin Jonáš
    Martin Jonáš over 14 years
    Excellent answer, you got my vote. But one small detail - to be monad, type has to have defined not just bind operator (>>=) but return function too.
  • Thomas Eding
    Thomas Eding over 14 years
    5: A name is but a name :D But to be honest, I don't see how one could come up with a 'better' name that somehow holds meaning without eschewing some concept of what monads are all about.
  • Kevin Won
    Kevin Won over 14 years
    slight addition to def of 'higher order function': they can take OR RETURN functions. That's why they are 'higher' 'cos they do things with themselves.
  • Apocalisp
    Apocalisp over 14 years
    By that definition, addition is a higher-order function. It takes a number and returns a function that adds that number to another. So no, higher order functions are strictly functions whose domain consists of functions.
  • Casebash
    Casebash about 14 years
    What exactly do you mean by "maps a function over it"?
  • Chris Conway
    Chris Conway about 14 years
    Casebash, I'm being deliberately informal in the introduction. See the examples near the end to get a sense of what "mapping a function" entails.
  • Peaker
    Peaker almost 14 years
    That is more like Applicative than Monad. With Monads, you have to get data from the pipes before you can choose the next pipe to connect.
  • Tony Morris
    Tony Morris over 13 years
  • Sasha Chedygov
    Sasha Chedygov over 13 years
    I completely agree with @Breton. You gave good examples of what kinds of code uses monads but you failed, IMO, to explain what a monad is and how it works, which is what's important.
  • Breton
    Breton over 13 years
    I don't think this is a very good explanation at all. Monads are simply A way? okay, which way? Why wouldn't I encapsulate using a class instead of a monad?
  • Adrian
    Adrian over 13 years
    Sometimes an explanation from a "learner" (like you) is more relevant to another learner than an explanation coming from an expert. Learners think alike :)
  • byteclub
    byteclub over 13 years
    If you use jQuery, this explanation can be very helpful, especially if your Haskell isn't strong
  • ChenZhou
    ChenZhou over 13 years
    As I understand, monads are more than that. Encapsulating mutable state in a "pure" functional languages is only one application of monads.
  • stakx - no longer contributing
    stakx - no longer contributing over 13 years
    +1 for the "monad tutorial fallacy". Tutorials on monads are akin to having several tutorials trying to explain the concept of integer numbers. One tutorial would say, "1 is similar to an apple"; another tutorial says, "2 is like a pear"; a third one says, "3 is basically an orange". But you never get the whole picture from any single tutorial. What I've taken from that is that monads are an abstract concept which can be used for many quite different purposes.
  • sidgeon smythe
    sidgeon smythe over 13 years
    @Scott: You seem to be saying that "distended wrinkle pods" is a worse name than "Abelian group", the standard name. And yet you also say that a made-up name may be beter than "monad", the standard name?
  • sidgeon smythe
    sidgeon smythe over 13 years
    @stakx: Yes, true. But I didn't mean that monads are an abstraction that you cannot learn or shouldn't learn; only that it's best to learn it after you've seen enough concrete examples to perceive a single underlying abstraction. See my other answer here.
  • Scott Wisniewski
    Scott Wisniewski over 13 years
    I'm saying the standard name of "monad" is unapproachable to most people. It was chosen by type theorists familiar with Category Theory. What makes sense to them, and what normal people are comfortable with, are very different.
  • sidgeon smythe
    sidgeon smythe over 13 years
    @Scott: The standard name of "Abelian group" is also unapproachable to most people. I don't see a difference at all.
  • Scott Wisniewski
    Scott Wisniewski over 13 years
    I didn't say "abelean group" was an approachable name. I used "distended wrinkle pods" in place of "abelian groups" to point out how foreign a name like "monad" is to a normal person.
  • sidgeon smythe
    sidgeon smythe over 13 years
    @Scott: So you're effectively proposing renaming "abelian group" as well — effectively saying that for every abstract concept that people haven't yet learnt, we must invent new names, and this is supposed to help matters. :-)
  • Scott Wisniewski
    Scott Wisniewski over 13 years
    No. I'm saying that a programmer shouldn't have to understand category theory, that monads are perfectly understood programing concepts without category theory, and that wrapping them with category theory only serves to obfuscate them. I'm not advocating anything with regards to names used in abstract algebra.
  • sidgeon smythe
    sidgeon smythe over 13 years
    @Scott: We're moving in circles. There's an exact parallel here between (monad, category theory) and (Abelian group, algebra). Do you agree? (Assuming you do…) In either case, the relevant concepts—the first elements of the pairs—can be understood without learning the entire theory (e.g. anyone can understand what an Abelian group is without knowing the whole of group theory), but this is not a reason for renaming the terms. (Or is it, according to you?) (BTW, please prefix your messages with "@shr…" or I won't be notified.)
  • Scott Wisniewski
    Scott Wisniewski over 13 years
    @shreevatsar No, they are not the same. An Abelian Group is not a design pattern. It's a mathematical construct. Monads, in category theory, are also mathematical constructs. My point is not that there are problems with the names of mathematical constructs. My point is that it's bad to name the design pattern after the mathematical construct.
  • sidgeon smythe
    sidgeon smythe over 13 years
    @Scott: I still don't quite see the difference…. But perhaps better to get to the crux of the matter: can you suggest a better name than "monad"? ("Type operator" is a poor choice, for reasons already mentioned by Jared.)
  • Scott Wisniewski
    Scott Wisniewski over 13 years
    @ShreevastaR Sure. "Expression Decorator" is the first thing that comes to mind. It conveys some meaning about what a Monad does, and how it works. It may not convey as much precision as "monad" does, but I think that's a good thing.
  • Rafael S. Calsaverini
    Rafael S. Calsaverini over 13 years
    Sometimes I feel that there are so many tutorials that try to convince the reader that monads are useful by using code that do complicated or useful stuff. That hindered my understanding for months. I don't learn that way. I prefer to see extremely simple code, doing something stupid that I can mentally go through and I couldn't find this kind of example. I can't learn if the first example is a monad to parse a complicate grammar. I can learn if it's a monad to sum integers.
  • sidgeon smythe
    sidgeon smythe about 13 years
    …best way not only on the internet, but anywhere. (Wadler's original paper Monads for functional programming that I mentioned in my answer below is also good.) None of the zillions of tutorials-by-analogy come close.
  • Charlie Flowers
    Charlie Flowers over 12 years
    Breton, and @musicfreak, he did explain what a monad is and how it works ... but for some reason you weren't able to follow it. I want to help, but I already know too much because I'm a little (just a little) ahead of you on the learning curve. If we can work together to find the gap, I would be glad to help close it. If you've already learned the answers, of course, then please let me know. Let me answer one question which may be part of the gap -- "Why do we need monads in addition to the rest of Haskell?" We need monads because we want to be able to write a series of "things"...
  • Charlie Flowers
    Charlie Flowers over 12 years
    (and things is just "snippets of code"), and express that those things are "joined together", without having to repeat everywhere what "joined together" means in the specific case. For example, say you're processing XML, and you need to get to the third Transaction element, to its fourth LineItem element, and read its Quantity attribute. Simple enough. You could even write an XPATH to get right to it. But it is complicated by the aggravating fact that each of these items might be missing entirely. So, is there even a 3rd Transaction element? What if the doc has only 2 Transaction elements...
  • Charlie Flowers
    Charlie Flowers over 12 years
    ... or none at all? What if it DOES have 3 Transaction elements, but the 3rd Transaction element does NOT have a 4th LineItem element? And finally, what if there is a 4th LineItem element, but that element does not have a Quantity attribute? This becomes a pain in the ass because your code to walk the path to the Quantity attribute you care about is "littered" with a whole bunch of error checking code to deal with the cases where the items you need are missing from the document. Haskell wants to be a powerful programming language that solves these thorny expressiveness problems...
  • Charlie Flowers
    Charlie Flowers over 12 years
    ...for you. In this case, Haskell can solve the problem. Why? Because Haskell is very good at dealing with anonymous functions. If you give Haskell a list of anonymous functions, it can carry that list around and execute those functions later, and with different parameters, etc. Therefore, Haskell can allow you to write your "path walking" code to get to the Quantity attribute without littering it with error checking code. You would write your path walking code as a list of anonymous functions, and you will use a syntax that says, ...
  • Charlie Flowers
    Charlie Flowers over 12 years
    ... "this anonymous function is joined to that one, which is joined to this other one". And separately, in an entirely different place in your code, you define what "joined to" means. In the example of walking XML to find the Quantity attribute, "joined to" will be defined as "proceed to the next anonymous function IF the previous anonymous function found the XML node it expected to find. Otherwise, stop evaluating all the anonymous functions and report an error." So ... I think this explains ...
  • Charlie Flowers
    Charlie Flowers over 12 years
    why monads exist in Haskell, why you'd want to use one, and part (the most important part) of how they work. If you couple this with JacquesB's excellent answer, are we getting somewhere? What is your next burning question?
  • Laurence Gonsalves
    Laurence Gonsalves over 12 years
    I read all of this and still don't know what a monad is, aside from the fact that it's something Haskell programmers don't understand well enough to explain. The examples don't help much, given that these are all things one can do without monads, and this answer doesn't make it clear how monads make them any easier, only more confusing. The one part of this answer that came close to being useful was where the syntactic sugar of example #2 was removed. I say came close because, aside from the first line, the expansion doesn't bear any real resemblance to the original.
  • Laurence Gonsalves
    Laurence Gonsalves over 12 years
    Another problem that seems to be endemic to explanations of monads is that it's written in Haskell. I'm not saying Haskell is a bad language -- I'm saying it's a bad language for explaining monads. If I knew Haskell I'd already understand monads, so if you want to explain monads, start by using a language that people who don't know monads are more likely to understand. If you must use Haskell, don't use the syntactic sugar at all -- use the smallest, simplest subset of the language you can, and don't assume an understanding of Haskell IO.
  • Ben
    Ben over 12 years
    Having done an honours-level university subject entirely dedicated to functional programming and been pronounced an "advanced Haskell programmer" by my lecturer... I still feel like I only vaguely know what monads are. I think the trouble is that monads are very very abstract concepts (which is what makes them so useful). I understand plenty of individual monads very well (list, IO, Maybe, Cont, ST, etc); what is elusive is what this thing called monad is that somehow unifies all those concepts.
  • Ben
    Ben over 12 years
    And the trouble is, you can't give a nice clear concrete example of that. Because if you try... it's just List, or Maybe, or IO. Which are easy to grasp, but don't necessarily help the unenlightened see the whole of monads. I think this is why they're hard; they are indeed simple enough (the number of things you have to memorise to correctly implement and use monads is far smaller than what you need to know to use classes in Java, for example). But the unifying concept of monad is just very very abstract.
  • sdcvvc
    sdcvvc over 12 years
    A longer explanation of this idea: blog.sigfpe.com/2007/04/trivial-monad.html
  • ninjagecko
    ninjagecko about 12 years
    One can substitute in f=Sqrt for the fmap example to better understand. To borrow notation from other languages, the fmap example boils down to fmap(f)([x]) means the same as [f(x)] (fmap(f) is the "lifted" version of f), and the bind example boils down to bind(fThenWrap)([x]) means to the same as fThenWrap(x). I'm curious why we'd have functions of the form a -> Wrapped b lying around though?
  • Charlie Flowers
    Charlie Flowers about 12 years
    Monads come from category theory, which mathemeticians fondly refer to as "general abstract nonsense." they mean it as a compliment.
  • Charlie Flowers
    Charlie Flowers about 12 years
    @LaurenceGonsalves i only have time right now to say one thing, but i hope it will help. You said you could do all this without a monad. And yes, you can. A monad is similiar to a dsl, in that you can do without dsl's. But they are nice because they make it easier to express and maintain what you're trying to express.
  • Laurence Gonsalves
    Laurence Gonsalves about 12 years
    @CharlieFlowers Sorry, no, that doesn't help. You say monads make things easier to "express and maintain" without any evidence to back that up. On the contrary, I see lots of people that don't understand them, and piles of unhelpful tutorials which suggest that even the supposedly "enlightened" don't understand monads well enough to explain them. Your comparison to DSLs also makes me wonder if you're talking about do-notation rather than monads. I'd like to understand actual monads before learning about the sugar.
  • Charlie Flowers
    Charlie Flowers about 12 years
    @LaurenceGonsalves i think you are conflating two separate things: first, the question, "can we justify the existence of monads?"; and second, "please help me understand what a monad is." All of my comments in this answer are meant to help (if only in a small way) towards question # 2. I'm not really much interested in discussing question # 1 (Perfectly valid question, just not something i am all that interested in). So my comment about dsl's is not an argument in favor of monads, and therefore presenting evidence would be out of place.
  • Laurence Gonsalves
    Laurence Gonsalves about 12 years
    @CharlieFlowers What was your comment, "A monad is similiar to a dsl, in that you can do without dsl's. But they are nice because they make it easier to express and maintain what you're trying to express." intended to be if not an argument in favor of monads?
  • Charlie Flowers
    Charlie Flowers about 12 years
    @LaurenceGonsalves Like I said, it was an attempt to answer the question, "please help me understand what a monad is." You made the point that a monad is not necessary for solving the example problems people listed, and I responded, saying, "That's right, they are not necessary. They are meant to be a nice 'bonus' to help improve your expressiveness." This helps explain the monad's place in the grand scheme of things. They're not necessary for Turing-completeness, just as DSL's aren't. They're meant to help you improve your expressiveness (just as DSL's are).
  • subrat71
    subrat71 about 12 years
    I appreciate your answer—especially the final concession that all of this is of course possible too without monads. One point to be made is that it's mostly easier with monads, but it's often not as efficient as doing it without them. Once you need to involve transformers, the extra layering of function calls (and function objects created) has a cost that's hard to see and control, rendered invisible by clever syntax.
  • MathematicalOrchid
    MathematicalOrchid about 12 years
    In Haskell at least, most of the overhead of monads gets stripped away by the optimiser. So the only real "cost" is in brain power required. (This is not insignificant if "maintainability" is something you care about.) But usually, monads make things easier, not harder. (Otherwise, why would you bother?)
  • Tony Morris
    Tony Morris almost 12 years
    JQuery is emphatically not a monad. The linked article is wrong.
  • Vinicius Seufitele
    Vinicius Seufitele over 11 years
    You seem to be the only one who finally addressed my main problem with understanding Monads. Nobody ever talks about HOW can the value be extracted. Is it implementation dependant?
  • tina Miller
    tina Miller over 11 years
    @ViniciusSeufitele, thanks for your comment. I'm afraid that my understanding hasn't advanced a great deal since I wrote this answer, so I can't really add much. The value extraction logically has to exist, so maybe that's why nobody bothers to mention it.
  • Travis Stevens
    Travis Stevens over 11 years
    Lenses, not Monads, are a way for functional programmers to use imperative code without actually admitting it.
  • Zach Conn
    Zach Conn over 11 years
    @LaurenceGonsalves I agree with Charlie here. "Monad" mostly refers to an interface pattern useful for constructing complex behaviors in a purely functional way. Monads exist more for convenience rather than necessity. The Maybe monad is a great example of this. It's not necessary. You could just chain together long sequences of if-then-else statements in your code, but who wants to do that? So instead of using the ordinary function composition operator ., you use an 'overloaded' function composition operator which inserts the if-then-else stuff automatically. This operator is >>=.
  • gwideman
    gwideman over 11 years
    I feel Scott is very much on point. The term "monoid" was really bugging me, so I posted a question about its origin, and got a great answer. stackoverflow.com/questions/14090183/… Seems like if Haskell doesn't get Abelian groups, then "distended wrinkle pods" will still be available as an improved name for monads :-).
  • nealmcb
    nealmcb about 11 years
    Being "emphatic" isn't very convincing. For some useful discussion on the topic, see Is jQuery a monad - Stack Overflow
  • nealmcb
    nealmcb about 11 years
    See also Douglas Crackford's Google Talk Monads and Gonads and his Javascript code for doing modads, expanding on the similar behavior of AJAX libraries and Promises: douglascrockford/monad · GitHub
  • Sam Watkins
    Sam Watkins over 10 years
    This JavaScript translation of Sigfpe's post is the new best way to learn monads, for people who don't already grok advanced Haskell!
  • Aditya M P
    Aditya M P about 10 years
    Thanks, @CharlieFlowers and OP. I think I have an understanding now of the monad, though it is quite a tenuous grasp. I suppose better understanding will come only when I actually start using these concepts.
  • gman
    gman about 10 years
    How is this example of >>= any different than C++ io >> and << operators? Is there a distinction that would help explain why one is a monad and the others not monads?
  • Jeremy List
    Jeremy List about 10 years
    @gman If you were translating a Haskell IO function directly into C++: the >>= operator would translate into ";", not ">>" or "<<".
  • Jeremy List
    Jeremy List about 10 years
    I'm not sure whether or not Haskell supports this but mathematically you can define a monad either in terms of >>= and return or join and ap. >>= and return are what make monads practically useful but join and ap give a more intuitive understanding of what a monad is.
  • Jeremy List
    Jeremy List about 10 years
    My above comment said ap: I meant liftM
  • Nikolaj-K
    Nikolaj-K about 10 years
    "...conform to the structure of something called a monad in Category Theory, which is an obscure branch of mathematics." Why exactly is it "obscure"? Because the concepts are only 70 years old and never made it into school curricula so that people generally know about it? Once you invest a few days in learning about natural transformations - two parts (return and join) of the triple which make up a monad - you see that they are indeed the nice and natural ones, amongst all functions. Meanwhile, the mp3 format, an USB stick, or the term "foobar" are pretty ad hoc and not natural. "Man made"
  • dfeuer
    dfeuer almost 10 years
    @JeremyList, the C/C++ ; operator would translate, approximately, to the Haskell >> operator. I don't think C or C++ have anything much like >>=.
  • nomen
    nomen almost 10 years
    Sequencing isn't the only reason to define a monad. A monad is just any functor which has bind and return. Bind and return give you sequencing. But they give other things as well. Also, note that your favorite imperative language is effectively a fancy IO monad with OO classes. Making it easy to define monads means it's easy to use the interpreter pattern -- define a dsl as a monad and interpret it!
  • mb21
    mb21 almost 10 years
    Shouldn't it be: fmap :: (a -> b) -> Wrapped a -> Wrapped b ? haskell.org/haskellwiki/Monads_as_containers
  • Nikolaj-K
    Nikolaj-K almost 10 years
    @mb21: In case you're just pointing out that there are too many brackets, note that a->b->c is actually only short for a->(b->c). Writing this particular example as (a -> b) -> (Ta -> Tb) is strictly speaking just adding unncessary characters, but it's morally "the right thing to do" as it emphasises that fmap maps a function of type a -> b to a function of type Ta -> Tb. And originally, that's what functors do in category theory and that's where monads come from.
  • mb21
    mb21 almost 10 years
    @NikolajK right, they're the same. I'm just more familiar of thinking about it in terms of map for list.
  • George
    George over 9 years
    Please correct me if I am wrong: Monads address a problem which also shows up in arithmetic as DivByZero error. In particular, a sequence of interdependent calculations must allow for the presence of a DivByZero exception. The Monadic solution is to embrace the Nullable<Number> and provide a function to "lift" (wrap) existing operations so as to accept Nullable<Number> and another to convert a Number to Nullable<Number>.
  • George
    George over 9 years
    Please correct me if I am wrong: Monads address a problem which also shows up in arithmetic as DivByZero. In particular, a sequence of interdependent calculations must allow for the presence of a DivByZero exception. The Monadic solution is to embrace the Nullable<Number> and provide a function to wrap the existing operators so as to allow them to accept Nullable<Number> (BIND) and another to life Numbers to Nullable<Number> (RETURN). So, a Monad is a container or expanded type with functions that lift the target type to be expanded and a wrapper for it's operators to accept them.
  • Jeremy List
    Jeremy List over 9 years
    @George You just described the Maybe monad. The other ones do different things. The problem with explaining monads is that each one on its own is pretty obvious but the fact that they have a common structure can be counter-intuitive at first.
  • Jeremy List
    Jeremy List over 9 years
    What makes something a monad is the existence of a function with type M (M a) -> M a. The fact that you can turn that into one of type M a -> (a -> M b) -> M b is what makes them useful.
  • George
    George over 9 years
    @Jeremy I was trying to provide a type centric treatment. For example, that a monadic number may be defined to include a DivByZero as a value. Or, similarly, the state monad be considered an expansion of the operator to include a state or context parameter. And so on. Then the BIND and RETURN functions become monadic operators for wrapping or adapting existing values operations to the monadic version of their type. However, perhaps a type treatment is not sufficiently general.
  • George
    George over 9 years
    I have a discussion that treats the monad as a type expansion where the original type, b, is converted to an expanded type M<b> and the associated operators are wrapped to now service M<b>. These wrappers are what will handle the peculiarities of the monad. In particular, extracting the original type from the expanded type and passing it to it's wrapped operator and subsequently promoting the result. The benefit of the monad is that you retain simple declarative expressions. In my treatment I discussed expanding the numeric types system to include a DivByZero value to obviate the need to check
  • jpmc26
    jpmc26 over 9 years
    Did I understand this right if I walk away thinking that a monad is basically just a rigorous way of defining and discussing an algorithm, i.e. a set of steps to arrive at some result? I'm sure there's a lot of intricacies that statement ignores, but is that the gist of it?
  • groverboy
    groverboy over 9 years
    Do you mean "what the world doesn't need ..."? Nice analogy though!
  • Michael Welch
    Michael Welch about 9 years
    @ViniciusSeufitele , yes it is implementation dependent. The person writing the function >>= has access to the internals of the monad. For example see Maybe Monad and look for instance Monad Maybe. You'll see that when the left hand side is Just x then we return k x. The pattern matching does the unwrap for you. Something analogous happens in every monad implementation.
  • Jordan
    Jordan about 9 years
    This is how I learned what a monad is. Walking the reader through the process of inventing a concept is often the best way to teach the concept.
  • Titou
    Titou almost 9 years
    Big Mistake : monad computation can be triggerred wo. main.
  • Syssiphus
    Syssiphus over 8 years
    Thanks a lot for this. This is a good explanation and the final puzzle piece which made me see the whole picture. \o/
  • Peter Mortensen
    Peter Mortensen over 8 years
    Google Code is going to be closed down on 2016-01-15. Most projects are now read-only, as of 2015-08-24.
  • Daenyth
    Daenyth over 8 years
    @jpmc26 I'd agree with that. The way that I like to express monads is that they provide a way to abstract away control flow patterns when you have operations that depend on the results of previous operations.
  • Mulan
    Mulan almost 8 years
    "monad" roughly means "pattern" ... no.
  • Dmitri Zaitsev
    Dmitri Zaitsev almost 8 years
    However, a function accepting the screen object as argument and returning its copy with text modified would be pure.
  • Dmitri Zaitsev
    Dmitri Zaitsev almost 8 years
    Monad is not a datatype. It is a rule of composing functions: stackoverflow.com/a/37345315/1614973
  • Dmitri Zaitsev
    Dmitri Zaitsev almost 8 years
    Mentioning only type constructor is incomplete: stackoverflow.com/a/37345315/1614973
  • icc97
    icc97 over 7 years
    The video 'Brian Beckman: Don't fear the Monad' follows this same line of logic.
  • Admin
    Admin over 7 years
    "...but I hope others find it useful" it was indeed useful for me, despite of all the emphasized sentences :D
  • James
    James over 7 years
    This is the most concise and clear explanation of monads I've ever read/watched/heard. Thank you!
  • Dmitri Zaitsev
    Dmitri Zaitsev over 7 years
    There is important difference between Monad and Monoid. Monad is a rule to "compose" functions between different types, so they do not form a binary operation as required for Monoids, see here for more details: stackoverflow.com/questions/2704652/…
  • George
    George over 7 years
    Yes. You are correct. Your article was over my head :). However, I found this treatment very helpful (and added it to mine as a direction to others). Thanks your the heads up: stackoverflow.com/a/7829607/1612190
  • Dmitri Zaitsev
    Dmitri Zaitsev about 7 years
    You might have confused Algebraic group theory with Category theory where Monad is coming from. The former is the theory of algebraic groups, which is unrelated.
  • George
    George about 7 years
    @Dmitri Zitsev I have recently become better acquainted with monads and considering revising my treatment. Your comment will like spur me on. I think I have the correct treatment now
  • groverboy
    groverboy about 7 years
    @icc97 you're right - the meaning is clear enough. Sarcasm unintended, apologies to the author.
  • Eugene Yokota
    Eugene Yokota about 7 years
    What the world needs is another comment thread confirming a sarcasm, but if read carefully I've written but so that should make it clear.
  • George
    George almost 7 years
    One of the problems with Haskell is its use of arbitrarily defined symbols. As obvious as a symbol may appear to be, like the symbol for bind itself, >>=, quite of a lot of arbitrary stuff may be going on. I speak here concerning the use of, <|>, in the parse example. A definition is needed.
  • 4castle
    4castle almost 7 years
    @JeremyList liftM is the same as fmap. To define a monad, return/pure is required, in addition to >>= or join.
  • jmrah
    jmrah almost 7 years
    Coming from a non-math, non-functional programming background, this answer made the most sense to me.
  • robotmay
    robotmay over 6 years
    This is the first answer that actually gave me some idea of what the hell a monad is. Thank you for finding a way to explain it!
  • Gabriel
    Gabriel over 6 years
    @George The <|> combinator is not from the language itself, it's defined in a library called Parsec which is used to build parsers (and it heavily relies in monadic abstractions). I don't know why OP didn't explain that in his answer.
  • Gabriel
    Gabriel over 6 years
    Guys, monads are from the Math world. If you want to know what a monad is, you have to talk to a mathematician. Just because it is in some programming languages it doesn't mean programmers are suppose to explain what it is. For example, programming languages can sum numbers, but if you want to have an explanation about what summing is you have to talk to a mathematician. I hope it is clear now. My fellow programmers, stop trying to explain monads.
  • WestCoastProjects
    WestCoastProjects over 6 years
    That's a surprising - and v poor - reflection on Coursera.
  • George
    George over 6 years
    My understanding has grown and I've been contemplating a rewrite of the above. Due to this current activity I think I may do so soon. @Gabriel the concept of monads is a concept in functional programming irrespective of its place or origin in mathematics.
  • Gabriel
    Gabriel over 6 years
    I intentionally went a bit too far in my words to make my point clear. Of course some programmers are able to explain and understand what a monad is --and I am not one of them, by the way.
  • Brian Joseph Spinos
    Brian Joseph Spinos over 6 years
  • Brian Joseph Spinos
    Brian Joseph Spinos over 6 years
  • Will Ness
    Will Ness over 6 years
    IOW, Monad is generalized function call protocol.
  • Ryan Efendy
    Ryan Efendy about 6 years
    The python examples made it easy to comprehend! Thanks for sharing.
  • James
    James about 6 years
    You answer is the most helpful in my opinion. Although I have to say that I think the emphasis needs to be on the fact that the functions you're refering to don't just involve values in contexts, they actively put values in contexts. So for example, a function, f :: m a -> m b would very easily compose with another function, g :: m b -> m c. But monads (bind specifically) allows us to perpetually compose functions which put their input in the same context, without us needing to take the value out of that context first (which would effectively remove information from the value)
  • Jonas
    Jonas about 6 years
    @James I think that should be the emphasis for functors?
  • James
    James about 6 years
    @Jonas I guess I didn't explain propperly. When I say that the functions put values in contexts, I mean that they have type (a -> m b). These are very useful since putting a value into a context adds new information to it but it would usually be difficult to chain a (a -> m b) and a (b -> m c) together since we can't just take the value out of the context. So we would have to use some convoluted process to chain these functions together in a sensible way depending on the specific context and monads just allow us to do this in a consistent way, regardless of the context.
  • Admin
    Admin about 6 years
    This answer is misleading. Some monads do not have a "wrapper" at all, such a functions from a fixed value.
  • Swapnil B.
    Swapnil B. about 6 years
    This is a fantastic explanation. Thank you so much for your efforts. :)
  • Stav Alfi
    Stav Alfi almost 6 years
    This should be the accepted answer with most votes. Great explanation!
  • Wancieho
    Wancieho almost 6 years
    @MathematicalOrchid you mention "chaining operations together" and that gets me thinking of jQuery's chaining? Or is jQuery's implementation completely different? Is this also related to HoCs?
  • Will Ness
    Will Ness over 5 years
    yes, you describe the Applicative, not the Monad. Monad is, building the next pipe segment on the spot, depending on the data that reached that point, inside the pipe.
  • Bercovici Adrian
    Bercovici Adrian over 5 years
    Can someone explain me why does the if odd x works in the first example since x is a list. If i try : f=odd [1,2,3] .Why does it not break when inside the IO Monad ?
  • Dan Mandel
    Dan Mandel almost 5 years
    "Monads are simply a way to wrapping things and provide methods to do operations on the wrapped stuff without unwrapping it." Classes can be used for the exact same thing, can't they? It's difficult for me to see the difference from that statement alone.
  • KolA
    KolA over 4 years
    +1. No kidding, I really think that "monad is monoid in the category of endofunctors" is really the best explanation available. It is precise and explains monad in three simpler terms (monoid, endofunctor, category) which are far more accessible when studied individually. The reason why every monad tutorial fails is that it tries to jump over 10 steps of a ledder rather than walk them one by one.
  • sksallaj
    sksallaj over 4 years
    @DmitriZaitsev is right, Monads actually provide its own data data type, Monads arent data types
  • sksallaj
    sksallaj over 4 years
    @DanMandel Monads are design patterns that supply its own datatype wrapper. Monads are designed in a way to abstract boilerplate code. So as you call a Monad in your code, it does behind the scenes stuff that you dont want to worry about. Think about Nullable<T> or IEnumerable<T>, what do they do behind the scenes? Thats Monad.
  • radrow
    radrow over 3 years
    This is very limited and therefore improper way of thinking about monads
  • Bondolin
    Bondolin about 3 years
    Thank you for the excellent write-up. I've been cranking around Mark Seemann's blog (e.g. blog.ploeh.dk/2018/06/04/church-encoded-maybe) and so this kind of already makes some sense, but this is such an abstract concept that I am continually trying to get a better handle on it.
  • Bondolin
    Bondolin about 3 years
    So @LaurenceGonsalves FWIW, here's the first do example rewritten in C#, using instead the analogous from...select query syntax: _ = from a in new List<int> { 1, 2, 3 } from b in new List<int> { a + 1 } from c in b == 3 ? new List<int>() : new List<int> { b } select c; Reads a whole lot better across multiple lines, but SO comments :P Also note that just as Haskell's do covers up >>=, C#'s query syntax uses SelectMany under the hood.
  • wide_eyed_pupil
    wide_eyed_pupil almost 3 years
    I didn't understand most of this as I'm new to Haskell, but bookmarked for it's thoroughness. Thx for going to the effort. I think I'm going to keep coming back to the question what is a monad for a long time, each time hopefully a bit more base to work with.
  • cheznead
    cheznead almost 3 years
    Beautiful answer.
  • cheznead
    cheznead almost 3 years
    For 'A functor is any type construction T' did you mean 'constructor' here rather than 'construction'?
  • Will Ness
    Will Ness almost 3 years
    saying that Bind has type m a -> (a -> m b) -> m b is a mathematician's way to say that a particular m's implementation of Bind is responsible for / encapsulates / hides / already has / the ability to extract that a for itself. the implementer of Bind for a particular m uses the particulars of that type to get to the a "inside" m a. so we the common user of monadic interface don't have to bother ourselves with the particulars of that extraction. it's done for us as part of how that m implements "monadic" interface, how it "is" a "monad". it gives us Bind, that's how.
  • Keavon
    Keavon over 2 years
    "Some functions may not always return a valid value. In Haskell this is represented by the Maybe-type, which is an option that is either Some value or Nothing." Is this an error, shouldn't it say Just value rather than Some value?
  • JacquesB
    JacquesB over 2 years
    @Keavon: You are correct. OCaml is "Some", Haskell is "Just"
  • AndrewC
    AndrewC about 2 years
    I have a maths degree and even some algebraists think category theory is a bit obscure and general! The name monad is particularly unhelpful because it slaps people in the face with the fact that they don't understand it AT ALL and gives them no conceptual way in. If it were called Wiring, people would still be discussing Wiring a lot and writing tutorials about how to do Env Wiring (reader), Logging Wiring (Writer), State Wiring, List Wiring, Maybe Wiring, Exception Wiring, etc etc. Wiring Transformers would still be complicated. People would be talking about adding Wiring to other languages.