What is a monad?
Asked Answered
U

48

1689

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.

Unlimber answered 4/9, 2008 at 23:26 Comment(16)
Eric Lippert wrote an answer to this questions (#2705152), which is due to some issues lives in a separate page.Singleness
Here's a new introduction using javascript - I found it very readable.Moldavia
See also Different ways to see a monad.Capricecapricious
See also Monads in picturesGoraud
A monad is an array of functions with helper operations. See this answerGoraud
Another quick article that resembles the sigfpe answer: github.com/quchen/articles/blob/master/…Mcclean
The best explanation I've heard so far actually comes from wikipedia; "Monads are programmable semicolons."Reverberator
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.Garold
An extremely easy-to-follow explanation by Douglas Crockford: youtube.com/watch?v=dkZFtimgAcMFirewarden
Monad is not a Haskell-specific concept, @HelderPereira. Your retag seems wrong to me.Ginoginsberg
@Ginoginsberg 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.Keitel
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).Choli
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."Nanette
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.Timeconsuming
Unfortunately, a monad is a thing that can only be explained by people that don't know how to explain stuff.Fine
A monad is a stylised form of continuation-passing. Now all you need is a tutorial about continuations...Detached
P
1300

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.
Peekaboo answered 4/9, 2008 at 23:26 Comment(48)
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...Inutility
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.Inutility
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.Inutility
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"...Inutility
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...Inutility
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.Bilingual
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.Juryman
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"...Vindication
(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...Vindication
... 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...Vindication
...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, ...Vindication
... "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 ...Vindication
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?Vindication
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.Brader
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.Brader
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.Operant
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.Operant
Monads come from category theory, which mathemeticians fondly refer to as "general abstract nonsense." they mean it as a compliment.Vindication
@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.Vindication
@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.Brader
@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.Vindication
@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?Brader
@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).Vindication
@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 >>=.Attrahent
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.Clio
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?Perlaperle
@Perlaperle If you were translating a Haskell IO function directly into C++: the >>= operator would translate into ";", not ">>" or "<<".Pagandom
@JeremyList, the C/C++ ; operator would translate, approximately, to the Haskell >> operator. I don't think C or C++ have anything much like >>=.Caseation
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>.Disulfiram
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.Disulfiram
@Disulfiram 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.Pagandom
@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.Disulfiram
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?Uzziel
Thanks a lot for this. This is a good explanation and the final puzzle piece which made me see the whole picture. \o/Lindon
@Uzziel 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.Nabataean
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.Disulfiram
@Disulfiram 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.Mcclary
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.Mcclary
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. @Mcclary the concept of monads is a concept in functional programming irrespective of its place or origin in mathematics.Disulfiram
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.Mcclary
This is a fantastic explanation. Thank you so much for your efforts. :)Henchman
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 ?Nert
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.Appomattox
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.Appomattox
"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?Hokum
@Keavon: You are correct. OCaml is "Some", Haskell is "Just"Peekaboo
There is a contextual call or apply operator called map which is written <$> and things that define it are called Applicatives or Functors, they take a function and lift it up into a datatype doing some contextual operation. There is are destructive operations called Monoids that have an operation called a catamorphism like a autofold that flattens out things. Anything you can call with cata . map is called a Monad. Since cata . map results in a pipeline there are lots of things that can be done with them. A Monad is anything that does the cata.map / =《《 which is a version of ($) 。Timeconsuming
I think the problem with Monads is that Haskell typeclasses are on what you can do, not hierarchical "what are they". Nearly all Haskell datatypes can implement a contextual call operator and flat operation, and it turns out when you do that to the normal datatypes you get a kind of embedded language or chain computation pipeline that can represent semantics of procedural statements like Maybe implements break, State which is combination of Writer & Reader implements Assignment, List flatMap implements multiple return like a cartesian join in sql, Tree, Tuple, Function, Sum, Product, etc...Timeconsuming
T
804

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.

Tipsy answered 4/9, 2008 at 23:26 Comment(6)
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.Androsphinx
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?)Tipsy
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.Pagandom
My above comment said ap: I meant liftMPagandom
@JeremyList liftM is the same as fmap. To define a monad, return/pure is required, in addition to >>= or join.Chatham
@Tipsy 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?Cutoff
D
216

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.

Dactylogram answered 4/9, 2008 at 23:26 Comment(11)
-> is right-associative, mirroring function application, which is left-associative, so leaving the parentheses out doesn't make a difference here.Plenary
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 wrappingsUnfrequented
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?Inutility
A longer explanation of this idea: blog.sigfpe.com/2007/04/trivial-monad.htmlLeprous
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?Perigee
Shouldn't it be: fmap :: (a -> b) -> Wrapped a -> Wrapped b ? haskell.org/haskellwiki/Monads_as_containersRemainderman
@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.Bilbao
@NikolajK right, they're the same. I'm just more familiar of thinking about it in terms of map for list.Remainderman
This answer is misleading. Some monads do not have a "wrapper" at all, such a functions from a fixed value.Earache
"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.Smaragdine
@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.Presbyterial
C
180

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.

Cowbind answered 4/9, 2008 at 23:26 Comment(4)
…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.Anemone
This JavaScript translation of Sigfpe's post is the new best way to learn monads, for people who don't already grok advanced Haskell!Weatherbound
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.Unsuitable
However, a function accepting the screen object as argument and returning its copy with text modified would be pure.Chunchung
L
90

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.

Lauree answered 4/9, 2008 at 23:26 Comment(5)
What exactly do you mean by "maps a function over it"?Bartels
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.Lauree
Monad is not a datatype. It is a rule of composing functions: https://mcmap.net/q/45980/-monad-in-plain-english-for-the-oop-programmer-with-no-fp-backgroundChunchung
@DmitriZaitsev is right, Monads actually provide its own data data type, Monads arent data typesPresbyterial
Beautiful answer.Wrapped
P
85

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

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

A functor is 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):

map id = id
map (p . q) = map p . map q

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 monad is essentially just a functor T with two extra methods, join, of type T (T a) -> T a, and unit (sometimes called return, fork, or pure) of type a -> T a. For lists in Haskell:

join :: [[a]] -> [a]
pure :: a -> [a]

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.

Papaw answered 4/9, 2008 at 23:26 Comment(4)
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.Ordure
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.Papaw
The video 'Brian Beckman: Don't fear the Monad' follows this same line of logic.Gynecology
For 'A functor is any type construction T' did you mean 'constructor' here rather than 'construction'?Wrapped
B
54

[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:

Monads are simply a way to wrapping things and provide methods to do operations on the wrapped stuff without unwrapping it.

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

  1. 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.

  2. 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 Ints inside, and if so, will unwrap the Ints, 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 Ints 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 everywhere.

But the example is just what happens for Maybe. If the extra information was an IO, then that special operator defined for IOs would be called instead, and it could do something totally different before performing the addition. (OK, adding two IO Ints 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 have a language construct – syntax and all – that allows you to declare 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 Four book and memorise the construction of a particular pattern, you just write code that implements this pattern in an agnostic, generic way once 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 understand monads find them so useful: 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.

Briquette answered 4/9, 2008 at 23:26 Comment(3)
Sometimes an explanation from a "learner" (like you) is more relevant to another learner than an explanation coming from an expert. Learners think alike :)Vannavannatta
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.Pagandom
"monad" roughly means "pattern" ... no.Cudweed
I
49

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:

  1. Why do you need a monad?
  2. What is a monad?
  3. How is a monad implemented?

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.

consolestate MyConsole = new consolestate;

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

consolestate FinalConsole = print(input(print(myconsole, "Hello, what's your name?")),"hello, %inputbuffer%!");

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:

consolestate FinalConsole = myconsole:
                            print("Hello, what's your name?"):
                            input():
                            print("hello, %inputbuffer%!");

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.

Inutility answered 4/9, 2008 at 23:26 Comment(2)
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!Curve
Here is an implementation: github.com/brianspinos777/Programming_cheat_sheets/blob/master/…Assess
D
45

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 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:

  1. an M shell for holding monad relevant information,
  2. a 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
  3. composable functions of the form, 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, monads 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:

let return a = [a]
let lift f a = return (f a)

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...

M_ 
return = (a -> Ma)
f = (a -> Mb)
g = (b -> Mc)

Then...

Left Identity  : (return a) >>= f === f a
Right Identity : Ma >>= return    === Ma
Associative    : Ma >>= (f >>= g) === Ma >>= ((fun x -> f x) >>= g)

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.

Disulfiram answered 4/9, 2008 at 23:26 Comment(6)
"...but I hope others find it useful" it was indeed useful for me, despite of all the emphasized sentences :DHannelorehanner
This is the most concise and clear explanation of monads I've ever read/watched/heard. Thank you!Stevenstevena
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: #2705152Chunchung
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: https://mcmap.net/q/45981/-a-monad-is-just-a-monoid-in-the-category-of-endofunctors-what-39-s-the-problemDisulfiram
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.Chunchung
@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 nowDisulfiram
O
37

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:

  1. Monads have restrictions on what they can do (see the definiton for details).
  2. Those restrictions, along with the fact that there are three operations involved, conform to the structure of something called a monad in Category Theory, which is an obscure branch of mathematics.
  3. They were designed by proponents of "pure" functional languages
  4. Proponents of pure functional languages like obscure branches of mathematics
  5. Because the math is obscure, and monads are associated with particular styles of programming, people tend to use the word monad as a sort of secret handshake. Because of this no one has bothered to invest in a better name.
Outsail answered 4/9, 2008 at 23:26 Comment(21)
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?Rectal
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.Rectal
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#.Rectal
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.Rectal
"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.Rectal
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.Outsail
@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.Rectal
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.Stylish
@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?Anemone
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.Outsail
@Scott: The standard name of "Abelian group" is also unapproachable to most people. I don't see a difference at all.Anemone
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.Outsail
@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. :-)Anemone
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.Outsail
@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.)Anemone
@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.Outsail
@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.)Anemone
@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.Outsail
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. #14090683 Seems like if Haskell doesn't get Abelian groups, then "distended wrinkle pods" will still be available as an improved name for monads :-).Nievesniflheim
"...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"Bilbao
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.Undersexed
A
35

(See also the answers at What 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:

So, things I hate about Haskell:

Let’s start with the obvious. Monad tutorials. No, not monads. Specifically the tutorials. They’re endless, overblown and dear god are they tedious. Further, I’ve never seen any convincing evidence that they actually help. Read the class definition, write some code, get over the scary name.

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-trivial motivating examples, unlike many of the artificial tutorials out there.

Anemone answered 4/9, 2008 at 23:26 Comment(5)
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.Rectal
+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.Ventral
@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.Anemone
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.Stjohn
Mentioning only type constructor is incomplete: https://mcmap.net/q/45980/-monad-in-plain-english-for-the-oop-programmer-with-no-fp-backgroundChunchung
E
25

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:

if( clause ) then block

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.

Eames answered 4/9, 2008 at 23:26 Comment(0)
R
15

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.

Rectal answered 4/9, 2008 at 23:26 Comment(0)
D
10

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.

Disconnection answered 4/9, 2008 at 23:26 Comment(1)
The python examples made it easy to comprehend! Thanks for sharing.Haarlem
H
9

tl;dr

{-# LANGUAGE InstanceSigs #-}

newtype Id t = Id t

instance Monad Id where
   return :: t -> Id t
   return = Id

   (=<<) :: (a -> Id b) -> Id a -> Id b
   f =<< (Id x) = f x

Prologue

The application operator $ of functions

forall a b. a -> b

is canonically defined

($) :: (a -> b) -> a -> b
f $ x = f x

infixr 0 $

in terms of Haskell-primitive function application f x (infixl 10).

Composition . is defined in terms of $ as

(.) :: (b -> c) -> (a -> b) -> (a -> c)
f . g = \ x -> f $ g x

infixr 9 .

and satisfies the equivalences forall f g h.

     f . id  =  f            :: c -> d   Right identity
     id . g  =  g            :: b -> c   Left identity
(f . g) . h  =  f . (g . h)  :: a -> d   Associativity

. 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.

{-# LANGUAGE KindSignatures #-}

class Functor (f :: * -> *) where
   map :: (a -> b) -> (f a -> f b)

In addition to following statically enforced type protocol, instances of the functor type class must obey the algebraic functor laws forall f g.

       map id  =  id           :: f t -> f t   Identity
map f . map g  =  map (f . g)  :: f a -> f c   Composition / short cut fusion

Functor computations have the type

forall f t. Functor f => f t

A computation c r consists in results r within context c.

Unary monadic functions or Kleisli arrows have the type

forall m a b. Functor m => a -> m b

Kleisi 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 =>

(m, return, (=<<))

implemented as the type class

class Functor m => Monad m where
   return :: t -> m t
   (=<<)  :: (a -> m b) -> m a -> m b

infixr 1 =<<

The Kleisli identity return is a Kleisli arrow that promotes a value t into monadic context m. Extension or Kleisli application =<< applies a Kleisli arrow a -> m b to results of a computation m a.

Kleisli composition <=< is defined in terms of extension as

(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c)
f <=< g = \ x -> f =<< g x

infixr 1 <=<

<=< 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.

   f <=< return  =  f                :: c -> m d   Right identity
   return <=< g  =  g                :: b -> m c   Left identity
(f <=< g) <=< h  =  f <=< (g <=< h)  :: a -> m d   Associativity

<=< is associative, and return is its right and left identity.

Identity

The identity type

type Id t = t

is the identity function on types

Id :: * -> *

Interpreted as a functor,

   return :: t -> Id t
=      id :: t ->    t

    (=<<) :: (a -> Id b) -> Id a -> Id b
=     ($) :: (a ->    b) ->    a ->    b

    (<=<) :: (b -> Id c) -> (a -> Id b) -> (a -> Id c)
=     (.) :: (b ->    c) -> (a ->    b) -> (a ->    c)

In canonical Haskell, the identity monad is defined

newtype Id t = Id t

instance Functor Id where
   map :: (a -> b) -> Id a -> Id b
   map f (Id x) = Id (f x)

instance Monad Id where
   return :: t -> Id t
   return = Id

   (=<<) :: (a -> Id b) -> Id a -> Id b
   f =<< (Id x) = f x

Option

An option type

data Maybe t = Nothing | Just t

encodes computation Maybe t that not necessarily yields a result t, computation that may “fail”. The option monad is defined

instance Functor Maybe where
   map :: (a -> b) -> (Maybe a -> Maybe b)
   map f (Just x) = Just (f x)
   map _ Nothing  = Nothing

instance Monad Maybe where
   return :: t -> Maybe t
   return = Just

   (=<<) :: (a -> Maybe b) -> Maybe a -> Maybe b
   f =<< (Just x) = f x
   _ =<< Nothing  = Nothing

a -> Maybe b is applied to a result only if Maybe a yields a result.

newtype Nat = Nat Int

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

toNat :: Int -> Maybe Nat
toNat i | i >= 0    = Just (Nat i)
        | otherwise = Nothing

The natural numbers are not closed under subtraction.

(-?) :: Nat -> Nat -> Maybe Nat
(Nat n) -? (Nat m) = toNat (n - m)

infixl 6 -?

The option monad covers a basic form of exception handling.

(-? 20) <=< toNat :: Int -> Maybe Nat

List

The list monad, over the list type

data [] t = [] | t : [t]

infixr 5 :

and its additive monoid operation “append”

(++) :: [t] -> [t] -> [t]
(x : xs) ++ ys = x : xs ++ ys
[]       ++ ys = ys

infixr 5 ++

encodes nonlinear computation [t] yielding a natural amount 0, 1, ... of results t.

instance Functor [] where
   map :: (a -> b) -> ([a] -> [b])
   map f (x : xs) = f x : map f xs
   map _ []       = []

instance Monad [] where
   return :: t -> [t]
   return = (: [])

   (=<<) :: (a -> [b]) -> [a] -> [b]
   f =<< (x : xs) = f x ++ (f =<< xs)
   _ =<< []       = []

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 be

divisors :: Integral t => t -> [t]
divisors n = filter (`divides` n) [2 .. n - 1]

divides :: Integral t => t -> t -> Bool
(`divides` n) = (== 0) . (n `rem`)

then

forall n.  let { f = f <=< divisors } in f n   =   []

In defining the monad type class, instead of extension =<<, the Haskell standard uses its flip, the bind operator >>=.

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

   (>>) :: forall a b. m a -> m b -> m b
   m >> k = m >>= \ _ -> k
   {-# INLINE (>>) #-}

   return :: a -> m a
   return = pure

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

class              Functor f
class Functor m => Monad m

In Haskell, the current standard hierarchy is

class                  Functor f
class Functor p     => Applicative p
class Applicative m => Monad m

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

for a in (1, ..., 10)
   for b in (1, ..., 10)
      p <- a * b
      if even(p)
         yield p

roughly translates to the do block,

do a <- [1 .. 10]
   b <- [1 .. 10]
   let p = a * b
   guard (even p)
   return p

the equivalent monad comprehension,

[ p | a <- [1 .. 10], b <- [1 .. 10], let p = a * b, even p ]

and the expression

[1 .. 10] >>= (\ a ->
   [1 .. 10] >>= (\ b ->
      let p = a * b in
         guard (even p) >>       -- [ () | even p ] >>
            return p
      )
   )

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

let x = v in e    =   (\ x -> e)  $  v   =   v  &  (\ x -> e)
do { r <- m; c }  =   (\ r -> c) =<< m   =   m >>= (\ r -> c)

where

(&) :: a -> (a -> b) -> b
(&) = flip ($)

infixl 0 &

The guard function is defined

guard :: Additive m => Bool -> m ()
guard True  = return ()
guard False = fail

where the unit type or “empty tuple”

data () = ()

Additive monads that support choice and failure can be abstracted over using a type class

class Monad m => Additive m where
   fail  :: m t
   (<|>) :: m t -> m t -> m t

infixl 3 <|>

instance Additive Maybe where
   fail = Nothing

   Nothing <|> m = m
   m       <|> _ = m

instance Additive [] where
   fail = []
   (<|>) = (++)

where fail and <|> form a monoid forall k l m.

     k <|> fail  =  k
     fail <|> l  =  l
(k <|> l) <|> m  =  k <|> (l <|> m)

and fail is the absorbing/annihilating zero element of additive monads

_ =<< fail  =  fail

If in

guard (even p) >> return p

even p is true, then the guard produces [()], and, by the definition of >>, the local constant function

\ _ -> return p

is 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 processor is a function

forall st t. st -> (t, st)

that transitions a state st and yields a result t. The state st can be anything. Nothing, flag, count, array, handle, machine, world.

The type of state processors is usually called

type State st t = st -> (t, st)

The state processor monad is the kinded * -> * functor State st. Kleisli arrows of the state processor monad are functions

forall st a b. a -> (State st) b

In canonical Haskell, the lazy version of the state processor monad is defined

newtype State st t = State { stateProc :: st -> (t, st) }

instance Functor (State st) where
   map :: (a -> b) -> ((State st) a -> (State st) b)
   map f (State p) = State $ \ s0 -> let (x, s1) = p s0
                                     in  (f x, s1)

instance Monad (State st) where
   return :: t -> (State st) t
   return x = State $ \ s -> (x, s)

   (=<<) :: (a -> (State st) b) -> (State st) a -> (State st) b
   f =<< (State p) = State $ \ s0 -> let (x, s1) = p s0
                                     in  stateProc (f x) s1

A state processor is run by supplying an initial state:

run :: State st t -> st -> (t, st)
run = stateProc

eval :: State st t -> st -> t
eval = fst . run

exec :: State st t -> st -> st
exec = snd . run

State access is provided by primitives get and put, methods of abstraction over stateful monads:

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}

class Monad m => Stateful m st | m -> st where
   get :: m st
   put :: st -> m ()

m -> st declares a functional dependency of the state type st on the monad m; that a State t, for example, will determine the state type to be t uniquely.

instance Stateful (State st) st where
   get :: State st st
   get = State $ \ s -> (s, s)

   put :: st -> State st ()
   put s = State $ \ _ -> ((), s)

with the unit type used analogously to void in C.

modify :: Stateful m st => (st -> st) -> m ()
modify f = do
   s <- get
   put (f s)

gets :: Stateful m st => (st -> t) -> m t
gets f = do
   s <- get
   return (f s)

gets is often used with record field accessors.

The state monad equivalent of the variable threading

let s0 = 34
    s1 = (+ 1) s0
    n = (* 12) s1
    s2 = (+ 7) s1
in  (show n, s2)

where s0 :: Int, is the equally referentially transparent, but infinitely more elegant and practical

(flip run) 34
   (do
      modify (+ 1)
      n <- gets (* 12)
      modify (+ 7)
      return (show n)
   )

modify (+ 1) is a computation of type State Int (), except for its effect equivalent to return ().

(flip run) 34
   (modify (+ 1) >>
      gets (* 12) >>= (\ n ->
         modify (+ 7) >>
            return (show n)
      )
   )

The monad law of associativity can be written in terms of >>= forall m f g.

(m >>= f) >>= g  =  m >>= (\ x -> f x >>= g)

or

do {                 do {                   do {
   r1 <- do {           x <- m;                r0 <- m;
      r0 <- m;   =      do {            =      r1 <- f r0;
      f r0                 r1 <- f x;          g r1
   };                      g r1             }
   g r1                 }
}                    }

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

for :: Monad m => (a -> m b) -> [a] -> m ()
for f = foldr ((>>) . f) (return ())

while :: Monad m => m Bool -> m t -> m ()
while c m = do
   b <- c
   if b then m >> while c m
        else return ()

forever :: Monad m => m t
forever m = m >> forever m

Input/Output

data World

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:

type IO t = World -> (t, World)

Interaction is facilitated by impure primitives

getChar         :: IO Char
putChar         :: Char -> IO ()
readFile        :: FilePath -> IO String
writeFile       :: FilePath -> String -> IO ()
hSetBuffering   :: Handle -> BufferMode -> IO ()
hTell           :: Handle -> IO Integer
. . .              . . .

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.

unsafePerformIO :: IO t -> t

Or, at least, should.

The type signature of a Haskell program

main :: IO ()
main = putStrLn "Hello, World!"

expands to

World -> ((), World)

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

Tobj :  Obj(C) -> Obj(D)
   f :: *      -> *

and for each morphism in C a morphism in D

Tmor :  HomC(X, Y) -> HomD(Tobj(X), Tobj(Y))
 map :: (a -> b)   -> (f a -> f b)

where X, Y are objects in C. HomC(X, Y) is the homomorphism class of all morphisms X -> Y in C. The functor must preserve morphism identity and composition, the “structure” of C, in D.

                    Tmor    Tobj

      T(id)  =  id        : T(X) -> T(X)   Identity
T(f) . T(g)  =  T(f . g)  : T(X) -> T(Z)   Composition

The Kleisli category of a category C is given by a Kleisli triple

<T, eta, _*>

of an endofunctor

T : C -> C

(f), an identity morphism eta (return), and an extension operator * (=<<).

Each Kleisli morphism in Hask

      f :  X -> T(Y)
      f :: a -> m b

by the extension operator

   (_)* :  Hom(X, T(Y)) -> Hom(T(X), T(Y))
  (=<<) :: (a -> m b)   -> (m a -> m b)

is given a morphism in Hask’s Kleisli category

     f* :  T(X) -> T(Y)
(f =<<) :: m a  -> m b

Composition in the Kleisli category .T is given in terms of extension

 f .T g  =  f* . g       :  X -> T(Z)
f <=< g  =  (f =<<) . g  :: a -> m c

and satisfies the category axioms

       eta .T g  =  g                :  Y -> T(Z)   Left identity
   return <=< g  =  g                :: b -> m c

       f .T eta  =  f                :  Z -> T(U)   Right identity
   f <=< return  =  f                :: c -> m d

  (f .T g) .T h  =  f .T (g .T h)    :  X -> T(U)   Associativity
(f <=< g) <=< h  =  f <=< (g <=< h)  :: a -> m d

which, applying the equivalence transformations

     eta .T g  =  g
     eta* . g  =  g               By definition of .T
     eta* . g  =  id . g          forall f.  id . f  =  f
         eta*  =  id              forall f g h.  f . h  =  g . h  ==>  f  =  g

(f .T g) .T h  =  f .T (g .T h)
(f* . g)* . h  =  f* . (g* . h)   By definition of .T
(f* . g)* . h  =  f* . g* . h     . is associative
    (f* . g)*  =  f* . g*         forall f g h.  f . h  =  g . h  ==>  f  =  g

in terms of extension are canonically given

               eta*  =  id                 :  T(X) -> T(X)   Left identity
       (return =<<)  =  id                 :: m t -> m t

           f* . eta  =  f                  :  Z -> T(U)      Right identity
   (f =<<) . return  =  f                  :: c -> m d

          (f* . g)*  =  f* . g*            :  T(X) -> T(Z)   Associativity
(((f =<<) . g) =<<)  =  (f =<<) . (g =<<)  :: m a -> m c

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 endofunctor

     T :  C -> C
     f :: * -> *

and two natural tranformations

   eta :  Id -> T
return :: t  -> f t

    mu :  T . T   -> T
  join :: f (f t) -> f t

satisfying the equivalences

       mu . T(mu)  =  mu . mu               :  T . T . T -> T . T   Associativity
  join . map join  =  join . join           :: f (f (f t)) -> f t

      mu . T(eta)  =  mu . eta       =  id  :  T -> T               Identity
join . map return  =  join . return  =  id  :: f t -> f t

The monad type class is then defined

class Functor m => Monad m where
   return :: t -> m t
   join   :: m (m t) -> m t

The canonical mu implementation of the option monad:

instance Monad Maybe where
   return = Just

   join (Just m) = m
   join Nothing  = Nothing

The concat function

concat :: [[a]] -> [a]
concat (x : xs) = x ++ concat xs
concat []       = []

is the join of the list monad.

instance Monad [] where
   return :: t -> [t]
   return = (: [])

   (=<<) :: (a -> [b]) -> ([a] -> [b])
   (f =<<) = concat . map f

Implementations of join can be translated from extension form using the equivalence

     mu  =  id*           :  T . T -> T
   join  =  (id =<<)      :: m (m t) -> m t

The reverse translation from mu to extension form is given by

     f*  =  mu . T(f)     :  T(X) -> T(Y)
(f =<<)  =  join . map f  :: m a -> m b

But why should a theory so abstract be of any use for programming?

The answer is simple: as computer scientists, we value abstraction! When we design the interface to a software component, we want it to reveal as little as possible about the implementation. We want to be able to replace the implementation with many alternatives, many other ‘instances’ of the same ‘concept’. When we design a generic interface to many program libraries, it is even more important that the interface we choose have a variety of implementations. It is the generality of the monad concept which we value so highly, it is because category theory is so abstract that its concepts are so useful for programming.

It is hardly suprising, then, that the generalisation of monads that we present below also has a close connection to category theory. But we stress that our purpose is very practical: it is not to ‘implement category theory’, it is to find a more general way to structure combinator libraries. It is simply our good fortune that mathematicians have already done much of the work for us!

from Generalising Monads to Arrows by John Hughes

Helpful answered 4/9, 2008 at 23:26 Comment(1)
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.Histamine
P
9

I've been thinking of Monads in a different way, lately. I've been thinking of them as abstracting out execution order in 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.

Potentilla answered 4/9, 2008 at 23:26 Comment(0)
M
8

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

Menorca answered 4/9, 2008 at 23:26 Comment(0)
C
7

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).

Christmas answered 4/9, 2008 at 23:26 Comment(0)
I
7

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.

Isham answered 4/9, 2008 at 23:26 Comment(2)
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.Particularity
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.Nanette
L
7

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 Monad

The 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.

Lifeordeath answered 4/9, 2008 at 23:26 Comment(4)
If you use jQuery, this explanation can be very helpful, especially if your Haskell isn't strongFirstnighter
JQuery is emphatically not a monad. The linked article is wrong.Zambia
Being "emphatic" isn't very convincing. For some useful discussion on the topic, see Is jQuery a monad - Stack OverflowCady
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 · GitHubCady
H
5

A monad is a container, but for data. A special container.

All containers can have openings and handles and spouts, but these containers are all guaranteed to have certain openings and handles and spouts.

Why? Because these guaranteed openings and handles and spouts are useful for picking up and linking together the containers in specific, common ways.

This allows you to pick up different containers without having to know much about them. It also allows different kinds of containers to link together easily.

Helbona answered 4/9, 2008 at 23:26 Comment(0)
N
5

A Monad is an Applicative (i.e. something that you can lift binary -- hence, "n-ary" -- functions to,(1) and inject pure values into(2)) Functor (i.e. something that you can map over,(3) i.e. lift unary functions to(3)) with the added ability to flatten the nested datatype (with each of the three notions following its corresponding set of laws). In Haskell, this flattening operation is called join.

The general (generic, parametric) type of this "join" operation is:

join  ::  Monad m  =>  m (m a)  ->  m a

for any monad m (NB all ms in the type are the same!).

A specific m monad defines its specific version of join working for any value type a "carried" by the monadic values of type m a. Some specific types are:

join  ::  [[a]]           -> [a]         -- for lists, or nondeterministic values
join  ::  Maybe (Maybe a) -> Maybe a     -- for Maybe, or optional values
join  ::  IO    (IO    a) -> IO    a     -- for I/O-produced values

The join operation converts an m-computation producing an m-computation of a-type values into one combined m-computation of a-type values. This allows for combination of computation steps into one larger computation.

This computation steps-combining "bind" (>>=) operator simply uses fmap and join together, i.e.

(ma >>= k)  ==  join (fmap k ma)
{-
  ma        :: m a            -- `m`-computation which produces `a`-type values
  k         ::   a -> m b     --  create new `m`-computation from an `a`-type value
  fmap k ma :: m    ( m b )   -- `m`-computation of `m`-computation of `b`-type values
  (m >>= k) :: m        b     -- `m`-computation which produces `b`-type values
-}

Conversely, join can be defined via bind, join mma == join (fmap id mma) == mma >>= id where id ma = ma -- whichever is more convenient for a given type m.

For monads, both the do-notation and its equivalent bind-using code,

do { x <- mx ; y <- my ; return (f x y) }        --   x :: a   ,   mx :: m a
                                                 --   y :: b   ,   my :: m b
mx >>= (\x ->                                    -- nested
            my >>= (\y ->                        --  lambda
                         return (f x y) ))       --   functions

can be read as

first "do" mx, and when it's done, get its "result" as x and let me use it to "do" something else.

In a given do block, each of the values to the right of the binding arrow <- is of type m a for some type a and the same monad m throughout the do block.

return x is a neutral m-computation which just produces the pure value x it is given, such that binding any m-computation with return does not change that computation at all.


(1) with liftA2 :: Applicative m => (a -> b -> c) -> m a -> m b -> m c

(2) with pure :: Applicative m => a -> m a

(3) with fmap :: Functor m => (a -> b) -> m a -> m b

There's also the equivalent Monad methods,

liftM2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
return :: Monad m =>  a            -> m a
liftM  :: Monad m => (a -> b)      -> m a -> m b

Given a monad, the other definitions could be made as

pure   a       = return a
fmap   f ma    = do { a <- ma ;            return (f a)   }
liftA2 f ma mb = do { a <- ma ; b <- mb  ; return (f a b) }
(ma >>= k)     = do { a <- ma ; b <- k a ; return  b      }
Nanette answered 4/9, 2008 at 23:26 Comment(0)
Y
5

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 Monads allow function composition that involves values in contexts.

Yulandayule answered 4/9, 2008 at 23:26 Comment(5)
here is an implementation: github.com/brianspinos777/Programming_cheat_sheets/blob/master/…Assess
IOW, Monad is generalized function call protocol.Nanette
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)Stevenstevena
@Stevenstevena I think that should be the emphasis for functors?Yulandayule
@Yulandayule 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.Stevenstevena
U
5

This 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(<x, messages>) := <x, messages "called f. ">
g(<x, messages>) := <x, messages "called g. ">
wrap(x)          := <x, "">

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:

  f(g(wrap(x)))
= f(g(<x, "">))
= f(<x, "called g. ">)
= <x, "called g. called f. ">

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:

f(x)    := <x, "called f. ">
g(x)    := <x, "called g. ">
wrap(x) := <x, "">

But look at what happens when you compose them:

  f(g(wrap(x)))
= f(g(<x, "">))
= f(<<x, "">, "called g. ">)
= <<<x, "">, "called g. ">, "called f. ">

The problem is that passing a pair into a function does not give you what you want. But what if you could feed a pair into a function:

  feed(f, feed(g, wrap(x)))
= feed(f, feed(g, <x, "">))
= feed(f, <x, "called g. ">)
= <x, "called g. called f. ">

Read feed(f, m) as "feed m into f". To feed a pair <x, messages> into a function f is to pass x into f, get <y, message> out of f, and return <y, messages message>.

feed(f, <x, messages>) := let <y, message> = f(x)
                          in  <y, messages message>

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

First: if you wrap a value and then feed the resulting pair into a function:

  feed(f, wrap(x))
= feed(f, <x, "">)
= let <y, message> = f(x)
  in  <y, "" message>
= let <y, message> = <x, "called f. ">
  in  <y, "" message>
= <x, "" "called f. ">
= <x, "called f. ">
= f(x)

That is the same as passing the value into the function.

Second: if you feed a pair into wrap:

  feed(wrap, <x, messages>)
= let <y, message> = wrap(x)
  in  <y, messages message>
= let <y, message> = <x, "">
  in  <y, messages message>
= <x, messages "">
= <x, messages>

That does not change the pair.

Third: if you define a function that takes x and feeds g(x) into f:

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

and feed a pair into it:

  feed(h, <x, messages>)
= let <y, message> = h(x)
  in  <y, messages message>
= let <y, message> = feed(f, g(x))
  in  <y, messages message>
= let <y, message> = feed(f, <x, "called g. ">)
  in  <y, messages message>
= let <y, message> = let <z, msg> = f(x)
                     in  <z, "called g. " msg>
  in <y, messages message>
= let <y, message> = let <z, msg> = <x, "called f. ">
                     in  <z, "called g. " msg>
  in <y, messages message>
= let <y, message> = <x, "called g. " "called f. ">
  in <y, messages message>
= <x, messages "called g. " "called f. ">
= feed(f, <x, messages "called g. ">)
= feed(f, feed(g, <x, messages>))

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:

  • Feeding a wrapped t into a function is the same as passing the 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

    • passes the t into g
    • gets an M u (call it n) from g
    • feeds n into f

    is the same as

    • feeding m into g
    • getting n from g
    • feeding 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.

Unsuitable answered 4/9, 2008 at 23:26 Comment(0)
S
5

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.

trait M[+A] {
  def flatMap[B](f: A => M[B]): M[B] // AKA bind

  // Pseudo Meta Code
  def isValidMonad: Boolean = {
    // for every parameter the following holds
    def isAssociativeOn[X, Y, Z](x: M[X], f: X => M[Y], g: Y => M[Z]): Boolean =
      x.flatMap(f).flatMap(g) == x.flatMap(f(_).flatMap(g))

    // for every parameter X and x, there exists an id
    // such that the following holds
    def isAnIdentity[X](x: M[X], id: X => M[X]): Boolean =
      x.flatMap(id) == x
  }
}

E.g.

// These could be any functions
val f: Int => Option[String] = number => if (number == 7) Some("hello") else None
val g: String => Option[Double] = string => Some(3.14)

// Observe these are identical. Since Option is a Monad 
// they will always be identical no matter what the functions are
scala> Some(7).flatMap(f).flatMap(g)
res211: Option[Double] = Some(3.14)

scala> Some(7).flatMap(f(_).flatMap(g))
res212: Option[Double] = Some(3.14)


// As Option is a Monad, there exists an identity:
val id: Int => Option[Int] = x => Some(x)

// Observe these are identical
scala> Some(7).flatMap(id)
res213: Option[Int] = Some(7)

scala> Some(7)
res214: Some[Int] = Some(7)

NOTE Strictly 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-category

Smidgen answered 4/9, 2008 at 23:26 Comment(0)
P
5

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.

Provided answered 4/9, 2008 at 23:26 Comment(0)
V
5

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.

Vellavelleity answered 4/9, 2008 at 23:26 Comment(0)
C
4

Let the below "{| a |m}" represent some piece of monadic data. A data type which advertises an a:

        (I got an a!)
          /        
    {| a |m}

Function, f, knows how to create a monad, if only it had an a:

       (Hi f! What should I be?)
                      /
(You?. Oh, you'll be /
 that data there.)  /
 /                 /  (I got a b.)
|    --------------      |
|  /                     |
f a                      |
  |--later->       {| b |m}

Here we see function, f, tries to evaluate a monad but gets rebuked.

(Hmm, how do I get that a?)
 o       (Get lost buddy.
o         Wrong type.)
o       /
f {| a |m}

Funtion, f, finds a way to extract the a by using >>=.

        (Muaahaha. How you 
         like me now!?)       
    (Better.)      \
        |     (Give me that a.)
(Fine, well ok.)    |
         \          |
   {| a |m}   >>=   f

Little does f know, the monad and >>= are in collusion.

            (Yah got an a for me?)       
(Yeah, but hey    | 
 listen. I got    |
 something to     |
 tell you first   |
 ...)   \        /
         |      /
   {| a |m}   >>=   f

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

 data Maybe a = Nothing | Just a

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

Wherein, if the case is Just a

            (Yah what is it?)       
(... hm? Oh,      |
forget about it.  |
Hey a, yr up.)    | 
            \     |
(Evaluation  \    |
time already? \   |
Hows my hair?) |  |
      |       /   |
      |  (It's    |
      |  fine.)  /
      |   /     /    
   {| a |m}   >>=   f

But for the case of Nothing

        (Yah what is it?)       
(... There      |
is no a. )      |
  |        (No a?)
(No a.)         |
  |        (Ok, I'll deal
  |         with this.)
   \            |
    \      (Hey f, get lost.) 
     \          |   ( Where's my a? 
      \         |     I evaluate a)
       \    (Not any more  |
        \    you don't.    |
         |   We're returning
         |   Nothing.)   /
         |      |       /
         |      |      /
         |      |     /
   {| a |m}   >>=   f      (I got a b.)
                    |  (This is   \
                    |   such a     \
                    |   sham.) o o  \
                    |               o|
                    |--later-> {| b |m}

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:

(Ok, here's your a. Well, its
 a bunch of them, actually.)
  |
  |    (Thanks, no problem. Ok
  |     f, here you go, an a.)
  |       |
  |       |        (Thank's. See
  |       |         you later.)
  |  (Whoa. Hold up f,      |
  |   I got another         |
  |   a for you.)           |
  |       |      (What? No, sorry.
  |       |       Can't do it. I 
  |       |       have my hands full
  |       |       with all these "b" 
  |       |       I just made.) 
  |  (I'll hold those,      |
  |   you take this, and   /
  |   come back for more  /
  |   when you're done   / 
  |   and we'll do it   / 
  |   again.)          /
   \      |  ( Uhhh. All right.)
    \     |       /    
     \    \      /
{| a |m}   >>=  f  

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.

Courtneycourtrai answered 4/9, 2008 at 23:26 Comment(0)
L
4

A very simple answer is:

Monads are an abstraction that provide an interface for encapsulating values, for computing new encapsulated values, and for unwrapping the encapsulated value.

What's convenient about them in practice is that they provide a uniform interface for creating data types that model state while not being stateful.

It's important to understand that a Monad is an abstraction, that is, an abstract interface for dealing with a certain kind of data structure. That interface is then used to build data types that have monadic behavior.

You can find a very good and practical introduction in Monads in Ruby, Part 1: Introduction.

Laidlaw answered 4/9, 2008 at 23:26 Comment(0)
W
4

What the world needs is another monad blog post, but I think this is useful in identifying existing monads in the wild.

Sierpinski triangle

The above is a fractal called Sierpinski triangle, the only fractal I can remember to draw. Fractals are self-similar structure like the above triangle, in which the parts are similar to the whole (in this case exactly half the scale as parent triangle).

Monads are fractals. Given a monadic data structure, its values can be composed to form another value of the data structure. This is why it's useful to programming, and this is why it occurrs in many situations.

Wilder answered 4/9, 2008 at 23:26 Comment(3)
Do you mean "what the world doesn't need ..."? Nice analogy though!Dipper
@Gynecology you're right - the meaning is clear enough. Sarcasm unintended, apologies to the author.Dipper
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.Wilder
Z
4

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

Zambia answered 4/9, 2008 at 23:26 Comment(2)
See if this helps projects.tmorris.net/public/what-does-monad-mean/artifacts/1.1/…Zambia
Google Code is going to be closed down on 2016-01-15. Most projects are now read-only, as of 2015-08-24.Unlimber
M
4

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).

Moldavia answered 4/9, 2008 at 23:26 Comment(0)
M
3

Essentially, and Practically, monads allow callback nesting
(with a mutually-recursively-threaded state (pardon the hyphens))
(in a composable (or decomposable) fashion)
(with type safety (sometimes (depending on the language)))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

E.G. this is NOT a monad:

//JavaScript is 'Practical'
var getAllThree = 
         bind(getFirst, function(first){  
  return bind(getSecond,function(second){  
  return bind(getThird, function(third){  
    var fancyResult = // And now make do fancy 
                      // with first, second,
                      // and third 
    return RETURN(fancyResult);
  });});});  

But monads enable such code.
The monad is actually the set of types for:
{bind,RETURN,maybe others I don't know...}.
Which is essentially inessential, and practically impractical.

So now I can use it:

var fancyResultReferenceOutsideOfMonad =  
  getAllThree(someKindOfInputAcceptableToOurGetFunctionsButProbablyAString);  

//Ignore this please, throwing away types, yay JavaScript:
//  RETURN = K
//  bind = \getterFn,cb -> 
//    \in -> let(result,newState) = getterFn(in) in cb(result)(newState)

Or Break it up:

var getFirstTwo = 
           bind(getFirst, function(first){  
    return bind(getSecond,function(second){  
      var fancyResult2 = // And now make do fancy 
                         // with first and second
      return RETURN(fancyResult2);
    });})
  , getAllThree = 
           bind(getFirstTwo, function(fancyResult2){  
    return bind(getThird,    function(third){  
      var fancyResult3 = // And now make do fancy 
                         // with fancyResult2,
                         // and third 
      return RETURN(fancyResult3);
    });});

Or ignore certain results:

var getFirstTwo = 
           bind(getFirst, function(first){  
    return bind(getSecond,function(second){  
      var fancyResult2 = // And now make do fancy 
                         // with first and second
      return RETURN(fancyResult2);
    });})
  , getAllThree = 
           bind(getFirstTwo, function(____dontCare____NotGonnaUse____){  
    return bind(getThird,    function(three){  
      var fancyResult3 = // And now make do fancy 
                         // with `three` only!
      return RETURN(fancyResult3);
    });});

Or simplify a trivial case from:

var getFirstTwo = 
           bind(getFirst, function(first){  
    return bind(getSecond,function(second){  
      var fancyResult2 = // And now make do fancy 
                         // with first and second
      return RETURN(fancyResult2);
    });})
  , getAllThree = 
           bind(getFirstTwo, function(_){  
    return bind(getThird,    function(three){  
      return RETURN(three);
    });});

To (using "Right Identity"):

var getFirstTwo = 
           bind(getFirst, function(first){  
    return bind(getSecond,function(second){  
      var fancyResult2 = // And now make do fancy 
                         // with first and second
      return RETURN(fancyResult2);
    });})
  , getAllThree = 
           bind(getFirstTwo, function(_){  
    return getThird;
    });

Or jam them back together:

var getAllThree = 
           bind(getFirst, function(first_dontCareNow){  
    return bind(getSecond,function(second_dontCareNow){  
    return getThird;
    });});

The practicality of these abilities doesn't really emerge,
or become clear until you try to solve really messy problems
like parsing, or module/ajax/resource loading.

Can you imagine thousands of lines of indexOf/subString logic?
What if frequent parsing steps were contained in little functions?
Functions like chars, spaces,upperChars, or digits?
And what if those functions gave you the result in a callback,
without having to mess with Regex groups, and arguments.slice?
And what if their composition/decomposition was well understood?
Such that you could build big parsers from the bottom up?

So the ability to manage nested callback scopes is incredibly practical,
especially when working with monadic parser combinator libraries.
(that is to say, in my experience)

DON'T GET HUNG UP ON:
- CATEGORY-THEORY
- MAYBE-MONADS
- MONAD LAWS
- HASKELL
- !!!!

Myalgia answered 4/9, 2008 at 23:26 Comment(0)
M
3

Princess's explanation of F# Computation Expressions helped me, though I still can't say I've really understood.

EDIT: this series - explaining monads with javascript - is the one that 'tipped the balance' for me.

I think that understanding monads is something that creeps up on you. In that sense, reading as many 'tutorials' as you can is a good idea, but often strange stuff (unfamiliar language or syntax) prevents your brain from concentrating on the essential.

Some things that I had difficulty understanding:

  • Rules-based explanations never worked for me, because most practical examples actually require more than just return/bind.
  • Also, calling them rules didn't help. It is more a case of "there are these things that have something in common, let's call the things 'monads', and the bits in common 'rules'".
  • Return (a -> M<a>) and Bind (M<a> -> (a -> M<b>) -> M<b>) are great, but what I could never understand is HOW Bind could extract the a from M<a> in order to pass it into a -> M<b>. I don't think I've ever read anywhere (maybe it's obvious to everyone else), that the reverse of Return (M<a> -> a) has to exist inside the monad, it just doesn't need to be exposed.
Moldavia answered 4/9, 2008 at 23:26 Comment(5)
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?Whelan
@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.Moldavia
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 checkDisulfiram
@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.Hypopituitarism
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.Nanette
A
2

Once you understand a use case for monads, you will understand monads. Here is an example "use case": Validation - an incomming payload to an API endpoint needs to be validated.

  1. i write a few low level validation functions to validate different parts of the payload
  2. i need to write some code to call each of the validation functions and return with either:
    • some validated, possibly transformed payload, or
    • a validation error

Without Monads:

DateTime ValidateDateString(string dateTimeString);
int ValidateCustomerID (int); // returns the validated customer id
SomethingWow ValidateSomeCustomerRelatedDate(int customerID, DateTime someDate);

Call each of the validation functions in sequence, fail with an error if one fails:

  • Maybe each function raises an exception, wrap the whole thing in an exception handler
  • Maybe we change the above signatures to also return a boolean and an error message ... so we can check each return value and only proceed if it was true or we return with the error message
  • But we cant compose them, generically, into 1 function because they all return different types ... int, DateTime and SomethingWow

With the Monad Bind operation

Create a return type Result<T>

public class Result<T>
{
    public static Result<T> Success(T v)
    {
        return new Result<T>
        {
            Value = v, IsValid = true, Error = "",
        };
    }

    public static Result<T> Failure(string error)
    {
        return new Result<T>
        {
            Value = default, IsValid = false, Error = error,
        };
    }

    public Result<Tb> Then<Tb>(Func<T, Result<Tb>> f)
    {
        return IsValid ? f(Value) : CastAs<Tb>();
    }

    public Result<Tb> CastAs<Tb>()
    {
        return Result<Tb>.Failure(Error);
    }

    public T Value { get; set; }
    public bool IsValid { get; set; }
    public string Error { get; set; }
}

now wrap the validation functions to return via this type (if they failed they return using the Result Failure operation, supplying the validation message):

Result<DateTime> ValidateDateString(string dateTimeString);
Result<int> ValidateCustomerID (int); // returns the validated customer id
Result<SomethingWow> ValidateSomeCustomerRelatedDate(int customerID, DateTime someDate);

Use Result<T> bind operation (like the monad Bind, in principle .. i called it Then):

public Result<CustomerCommand> Validate(Request request)
{
    Result<CustomerCommand> r = ValidateDateString(request.SomeDateString)
        .Then(someValidDate => ValidateCustomerID(request.CustomerID)
            .Then(validCustomerID => ValidateSomeCustomerRelatedDate(validCustomerID , someValidDate)
                    .Then(wow =>
                    {
                        var m = new CustomerCommand()
                        {
                            CustomerID = validCustomerID,
                            SomeDate = someValidDate,
                            WowFactor = wow
                        };
                        return Result<CustomerCommand>.Success(m);
                    })
                )));
    return r;
}

So the Monad serves the purpose of letting us compose the low level validation functions into 1 big validation function, taking out the need keep writing the if the last bit returns ok and dropping out as soon as there is a failure. The curious signature of bind gives our program access to intermediary low level outputs allowing us to build the final output out of these validated simple low level outputs

Ashe answered 4/9, 2008 at 23:26 Comment(0)
G
2

Another attempt at explaining monads, using just Python lists and the map function. I fully accept this isn't a full explanation, but I hope it gets at the core concepts.

I got the basis of this from the funfunfunction video on Monads and the Learn You A Haskell chapter 'For a Few Monads More'. I highly recommend watching the funfunfunction video.

At it's very simplest, Monads are objects that have a map and flatMap functions (bind in Haskell). There are some extra required properties, but these are the core ones.

flatMap 'flattens' the output of map, for lists this just concatenates the values of the list e.g.

concat([[1], [4], [9]]) = [1, 4, 9]

So in Python we can very basically implement a Monad with just these two functions:

def flatMap(func, lst):
    return concat(map(func, lst))

def concat(lst):
    return sum(lst, [])

func is any function that takes a value and returns a list e.g.

lambda x: [x*x]

Explanation

For clarity I created the concat function in Python via a simple function, which sums the lists i.e. [] + [1] + [4] + [9] = [1, 4, 9] (Haskell has a native concat method).

I'm assuming you know what the map function is e.g.:

>>> list(map(lambda x: [x*x], [1,2,3]))
[[1], [4], [9]]

Flattening is the key concept of Monads and for each object which is a Monad this flattening allows you to get at the value that is wrapped in the Monad.

Now we can call:

>>> flatMap(lambda x: [x*x], [1,2,3])
[1, 4, 9]

This lambda is taking a value x and putting it into a list. A monad works with any function that goes from a value to a type of the monad, so a list in this case.

That's your monad defined.

I think the question of why they're useful has been answered in other questions.

More explanation

Other examples that aren't lists are JavaScript Promises, which have the then method and JavaScript Streams which have a flatMap method.

So Promises and Streams use a slightly different function which flattens out a Stream or a Promise and returns the value from within.

The Haskell list monad has the following definition:

instance Monad [] where  
    return x = [x]  
    xs >>= f = concat (map f xs)  
    fail _ = [] 

i.e. there are three functions return (not to be confused with return in most other languages), >>= (the flatMap) and fail.

Hopefully you can see the similarity between:

xs >>= f = concat (map f xs)

and:

def flatMap(f, xs):
    return concat(map(f, xs))
Gynecology answered 4/9, 2008 at 23:26 Comment(0)
H
2

I'm trying to understand monads as well. It's my version:

Monads are about making abstractions about repetitive things. Firstly, monad itself is a typed interface (like an abstract generic class), that has two functions: bind and return that have defined signatures. And then, we can create concrete monads based on that abstract monad, of course with specific implementations of bind and return. Additionally, bind and return must fulfill a few invariants in order to make it possible to compose/chain concrete monads.

Why create the monad concept while we have interfaces, types, classes and other tools to create abstractions? Because monads give more: they enforce rethinking problems in a way that enables to compose data without any boilerplate.

Halvaard answered 4/9, 2008 at 23:26 Comment(0)
H
2

Explaining monads seems to be like explaining control-flow statements. Imagine that a non-programmer asks you to explain them?

You can give them an explanation involving the theory - Boolean Logic, register values, pointers, stacks, and frames. But that would be crazy.

You could explain them in terms of the syntax. Basically all control-flow statements in C have curly brackets, and you can distinguish the condition and the conditional code by where they are relative to the brackets. That may be even crazier.

Or you could also explain loops, if statements, routines, subroutines, and possibly co-routines.

Monads can replace a fairly large number of programming techniques. There's a specific syntax in languages that support them, and some theories about them.

They are also a way for functional programmers to use imperative code without actually admitting it, but that's not their only use.

Hesychast answered 4/9, 2008 at 23:26 Comment(1)
Lenses, not Monads, are a way for functional programmers to use imperative code without actually admitting it.Spirograph
G
1

According to What we talk about when we talk about monads the question "What is a monad" is wrong:

The short answer to the question "What is a monad?" is that it is a monoid in the category of endofunctors or that it is a generic data type equipped with two operations that satisfy certain laws. This is correct, but it does not reveal an important bigger picture. This is because the question is wrong. In this paper, we aim to answer the right question, which is "What do authors really say when they talk about monads?"

While that paper does not directly answer what a monad is it helps understanding what people with different backgrounds mean when they talk about monads and why.

Galitea answered 4/9, 2008 at 23:26 Comment(1)
+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.Tunnage
W
1

In the Coursera "Principles of Reactive Programming" training - Erik Meier describes them as:

"Monads are return types that guide you through the happy path." -Erik Meijer
Wulf answered 4/9, 2008 at 23:26 Comment(1)
That's a surprising - and v poor - reflection on Coursera.Goofy
H
1

Mathematial thinking

For short: An Algebraic Structure for Combining Computations.

return data: create a computation who just simply generate a data in monad world.

(return data) >>= (return func): The second parameter accept first parameter as a data generator and create a new computations which concatenate them.

You can think that (>>=) and return won't do any computation itself. They just simply combine and create computations.

Any monad computation will be compute if and only if main trigs it.

Hyperkinesia answered 4/9, 2008 at 23:26 Comment(1)
Big Mistake : monad computation can be triggerred wo. main.Pterodactyl
C
1

http://mikehadlow.blogspot.com/2011/02/monads-in-c-8-video-of-my-ddd9-monad.html

This is the video you are looking for.

Demonstrating in C# what the problem is with composition and aligning the types, and then implementing them properly in C#. Towards the end he displays how the same C# code looks in F# and finally in Haskell.

Cooker answered 4/9, 2008 at 23:26 Comment(0)
G
1

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.

Girandole answered 4/9, 2008 at 23:26 Comment(2)
As I understand, monads are more than that. Encapsulating mutable state in a "pure" functional languages is only one application of monads.Menorca
This is very limited and therefore improper way of thinking about monadsPoundal
U
0

For people coming from the imperative background (c# specifically),

consider the following code

bool ReturnTrueorFalse(SomeObject input)
{
    if(input.Property1 is invalid)
    {
        return false;
    }

    if(input.Property2 is invalid)
    {
        return false;
    }

    DoSomething();
    return true;
}

You would have seen lots of code like this, you would have even seen no early returns but all the checks are done nested. Now, Monad is a pattern where this can be flattened like below

Monad<bool> ReturnTrueorFalse(SomeObject input) =>
    from isProperty1Valid in input.Property1
    from isProperty2Valid in input.Property2
    select Monad.Create(isProperty1Valid && isProperty2Valid);

There are a few things to note here. First, the function's return value is changed. Second, both the properties of input have to be Monad. Next, Monad should implement SelectMany(LINQ's flattening operator). Since SelectMany is implemented for that type, the statements can be written using the query syntax

So, what is a Monad? It is a structure that flattens expressions that return the same type in a composable way. This is particularly useful in functional programming because most functional applications tend to keep the state and IO at the edge layer of the application (eg: Controllers) and return Monad based return values throughout the call stack until the value is required to be unwrapped. The biggest plus for me when I first saw this was it was so easy on the eyes as it was so declarative.

The best example of a Monad that every c# (these days almost everyone) developer can immediately recognize is async/await. Before.Net4.5 we had to write Task-based statements using ContinueWith for handling callbacks, after async/await we started using synchronous syntax for asynchronous syntax. This is possible because Task is a "monad".

Refer to this for a detailed explanation, this for simple implementation and language-ext for lots of awesome Monads and tons of information about functional programming in general for an OOP developer

Unpleasant answered 4/9, 2008 at 23:26 Comment(0)
C
0

A Monad is a box with a special machine attached that allows you to make one normal box out of two nested boxes - but still retaining some of the shape of both boxes.

Concretely, it allows you to perform join, of type Monad m => m (m a) -> m a.

It also needs a return action, which just wraps a value. return :: Monad m => a -> m a
You could also say join unboxes and return wraps - but join is not of type Monad m => m a -> a (It doesn't unwrap all Monads, it unwraps Monads with Monads inside of them.)

So it takes a Monad box (Monad m =>, m) with a box inside it ((m a)) and makes a normal box (m a).

However, usually a Monad is used in terms of the (>>=) (spoken "bind") operator, which is essentially just fmap and join after each other. Concretely,

x >>= f = join (fmap f x)
(>>=) :: Monad m => (a -> m b) -> m a -> m b

Note here that the function comes in the second argument, as opposed to fmap.

Also, join = (>>= id).

Now why is this useful? Essentially, it allows you to make programs that string together actions, while working in some sort of framework (the Monad).

The most prominent use of Monads in Haskell is the IO Monad.
Now, IO is the type that classifies an Action in Haskell. Here, the Monad system was the only way of preserving (big fancy words):

  • Referential Transparency
  • Lazyness
  • Purity

In essence, an IO action such as getLine :: IO String can't be replaced by a String, as it always has a different type. Think of IO as a sort of magical box that teleports the stuff to you.
However, still just saying that getLine :: IO String and all functions accept IO a causes mayhem as maybe the functions won't be needed. What would const "üp§" getLine do? (const discards the second argument. const a b = a.) The getLine doesn't need to be evaluated, but it's supposed to do IO! This makes the behaviour rather unpredictable - and also makes the type system less "pure", as all functions would take a and IO a values.

Enter the IO Monad.

To string to actions together, you just flatten the nested actions.
And to apply a function to the output of the IO action, the a in the IO a type, you just use (>>=).

As an example, to output an entered line (to output a line is a function which produces an IO action, matching the right argument of >>=):

getLine >>= putStrLn :: IO ()
-- putStrLn :: String -> IO ()

This can be written more intuitively with the do environment:

do line <- getLine
   putStrLn line

In essence, a do block like this:

do x <- a
   y <- b
   z <- f x y
   w <- g z
   h x
   k <- h z
   l k w

... gets transformed into this:

a     >>= \x ->
b     >>= \y ->
f x y >>= \z ->
g z   >>= \w ->
h x   >>= \_ ->
h z   >>= \k ->
l k w

There's also the >> operator for m >>= \_ -> f (when the value in the box isn't needed to make the new box in the box) It can also be written a >> b = a >>= const b (const a b = a)

Also, the return operator is modeled after the IO-intuituion - it returns a value with minimal context, in this case no IO. Since the a in IO a represents the returned type, this is similar to something like return(a) in imperative programming languages - but it does not stop the chain of actions! f >>= return >>= g is the same as f >>= g. It's only useful when the term you return has been created earlier in the chain - see above.

Of course, there are other Monads, otherwise it wouldn't be called Monad, it'd be called somthing like "IO Control".

For example, the List Monad (Monad []) flattens by concatenating - making the (>>=) operator perform a function on all elements of a list. This can be seen as "indeterminism", where the List is the many possible values and the Monad Framework is making all the possible combinations.

For example (in GHCi):

Prelude> [1, 2, 3] >>= replicate 3  -- Simple binding
[1, 1, 1, 2, 2, 2, 3, 3, 3]
Prelude> concat (map (replicate 3) [1, 2, 3])  -- Same operation, more explicit
[1, 1, 1, 2, 2, 2, 3, 3, 3]
Prelude> [1, 2, 3] >> "uq"
"uququq"
Prelude> return 2 :: [Int]
[2]
Prelude> join [[1, 2], [3, 4]]
[1, 2, 3, 4]

because:

join a = concat a
a >>= f = join (fmap f a)
return a = [a]  -- or "= (:[])"

The Maybe Monad just nullifies all results to Nothing if that ever occurs. That is, binding auto-checks if the function (a >>= f) returns or the value (a >>= f) is Nothing - and then returns Nothing as well.

join       Nothing  = Nothing
join (Just Nothing) = Nothing
join (Just x)       = x
a >>= f             = join (fmap f a)

or, more explicitly:

Nothing  >>= _      = Nothing
(Just x) >>= f      = f x

The State Monad is for functions that also modify some shared state - s -> (a, s), so the argument of >>= is :: a -> s -> (a, s).
The name is a sort of misnomer, since State really is for state-modifying functions, not for the state - the state itself really has no interesting properties, it just gets changed.

For example:

pop ::       [a] -> (a , [a])
pop (h:t) = (h, t)
sPop = state pop   -- The module for State exports no State constructor,
                   -- only a state function

push :: a -> [a] -> ((), [a])
push x l  = ((), x : l)
sPush = state push

swap = do a <- sPop
          b <- sPop
          sPush a
          sPush b

get2 = do a <- sPop
          b <- sPop
          return (a, b)

getswapped = do swap
                get2

then:

Main*> runState swap [1, 2, 3]
((), [2, 1, 3])
Main*> runState get2 [1, 2, 3]
((1, 2), [1, 2, 3]
Main*> runState (swap >> get2) [1, 2, 3]
((2, 1), [2, 1, 3])
Main*> runState getswapped [1, 2, 3]
((2, 1), [2, 1, 3])

also:

Prelude> runState (return 0) 1
(0, 1)
Cheryl answered 4/9, 2008 at 23:26 Comment(0)
D
0

If you are asking for a succinct, practical explanation for something so abstract, then you can only hope for an abstract answer:

a -> b

is one way of representing a computation from as to bs. You can chain computations, aka compose them together:

(b -> c) -> (a -> b) -> (a -> c)

More complex computations demand more complex types, e.g.:

a -> f b

is the type of computations from as to bs that are into fs. You can also compose them:

(b -> f c) -> (a -> f b) -> (a -> f c)

It turns out this pattern appears literally everywhere and has the same properties as the first composition above (associativity, right- and left-identity).

One had to give this pattern a name, but then would it help to know that the first composition is formally characterised as a Semigroupoid?

"Monads are just as interesting and important as parentheses" (Oleg Kiselyov)

Dressing answered 4/9, 2008 at 23:26 Comment(0)
P
-1

following your brief, succinct, practical indications:

The easiest way to understand a monad is as a way to apply/compose functions within a context. Let's say you have two computations which both can be seen as two mathematical functions f and g.

  • f takes a String and produces another String (take the first two letters)
  • g takes a String and produces another String (upper case transformation)

So in any language the transformation "take the first two letter and convert them to upper case" would be written g(f("some string")). So, in the world of pure perfect functions, composition is just: do one thing and then do the other.

But let's say we live in the world of functions which can fail. For example: the input string might be one char long so f would fail. So in this case

  • f takes a String and produces a String or Nothing.
  • g produces a String only if f hasn't failed. Otherwise, produces Nothing

So now, g(f("some string")) needs some extra checking: "Compute f, if it fails then g should return Nothing, else compute g"

This idea can be applied to any parametrized type as follows:

Let Context[Sometype] be a computation of Sometype within a Context. Considering functions

  • f:: AnyType -> Context[Sometype]
  • g:: Sometype -> Context[AnyOtherType]

the composition g(f()) should be readed as "compute f. Within this context do some extra computations and then compute g if it has sense within the context"

Prisca answered 4/9, 2008 at 23:26 Comment(0)
D
-1

Explanation

It's quite simple, when explained in C#/Java terms:

  1. A monad is a function that takes arguments and returns a special type.

  2. The special type that this monad returns is also called monad. (A monad is a combination of #1 and #2)

  3. There's some syntactic sugar to make calling this function and conversion of types easier.

Example

A monad is useful to make the life of the functional programmer easier. The typical example: The Maybe monad takes two parameters, a value and a function. It returns null if the passed value is null. Otherwise it evaluates the function. If we needed a special return type, we would call this return type Maybe as well. A very crude implementation would look like this:

object Maybe(object value, Func<object,object> function)
{
    if(value==null)
        return null;

    return function(value);
}

This is spectacularly useless in C# because this language lacks the required syntactic sugar to make monads useful. But monads allow you to write more concise code in functional programming languages.

Oftentimes programmers call monads in chains, like so:

var x = Maybe(x, x2 => Maybe(y, y2 => Add(x2, y2)));

In this example the Add method would only be called if x and y are both non-null, otherwise null will be returned.

Answer

To answer the original question: A monad is a function AND a type. Like an implementation of a special interface.

Dmitri answered 4/9, 2008 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.