Difference between revisions of "Typeclassopedia"

From HaskellWiki
Jump to: navigation, search
m (Definition: no hask tags in links)
(Revert to plain code tags for inline examples)
Line 16: Line 16:
 
* What the heck is a monoid, and how is it different from a mon<u>a</u>d?
 
* What the heck is a monoid, and how is it different from a mon<u>a</u>d?
   
* I finally figured out how to use [[Parsec]] with do-notation, and someone told me I should use something called <hask>Applicative</hask> instead. Um, what?
+
* I finally figured out how to use [[Parsec]] with do-notation, and someone told me I should use something called <code>Applicative</code> instead. Um, what?
   
* Someone in the [[IRC channel|#haskell]] IRC channel used <hask>(***)</hask>, and when I asked lambdabot to tell me its type, it printed out scary gobbledygook that didn’t even fit on one line! Then someone used <hask>fmap fmap fmap</hask> and my brain exploded.
+
* Someone in the [[IRC channel|#haskell]] IRC channel used <code>(***)</code>, and when I asked lambdabot to tell me its type, it printed out scary gobbledygook that didn’t even fit on one line! Then someone used <code>fmap fmap fmap</code> and my brain exploded.
   
* When I asked how to do something I thought was really complicated, people started typing things like <hask>zip.ap fmap.(id &&& wtf)</hask> and the scary thing is that they worked! Anyway, I think those people must actually be robots because there’s no way anyone could come up with that in two seconds off the top of their head.
+
* When I asked how to do something I thought was really complicated, people started typing things like <code>zip.ap fmap.(id &&& wtf)</code> and the scary thing is that they worked! Anyway, I think those people must actually be robots because there’s no way anyone could come up with that in two seconds off the top of their head.
   
 
If you have, look no further! You, too, can write and understand concise, elegant, idiomatic Haskell code with the best of them.
 
If you have, look no further! You, too, can write and understand concise, elegant, idiomatic Haskell code with the best of them.
Line 29: Line 29:
   
 
It’s impossible to overstate the importance of the first; the patient student of type signatures will uncover many profound secrets. Conversely, anyone ignorant of the types in their code is doomed to eternal uncertainty. “Hmm, it doesn’t compile ... maybe I’ll stick in an
 
It’s impossible to overstate the importance of the first; the patient student of type signatures will uncover many profound secrets. Conversely, anyone ignorant of the types in their code is doomed to eternal uncertainty. “Hmm, it doesn’t compile ... maybe I’ll stick in an
<hask>fmap</hask> here ... nope, let’s see ... maybe I need another <hask>(.)</hask> somewhere? ... um ...”
+
<code>fmap</code> here ... nope, let’s see ... maybe I need another <code>(.)</code> somewhere? ... um ...”
   
 
The second key—gaining deep intuition, backed by examples—is also important, but much more difficult to attain. A primary goal of this document is to set you on the road to gaining such intuition. However—
 
The second key—gaining deep intuition, backed by examples—is also important, but much more difficult to attain. A primary goal of this document is to set you on the road to gaining such intuition. However—
Line 43: Line 43:
 
[[Image:Typeclassopedia-diagram.png]]
 
[[Image:Typeclassopedia-diagram.png]]
   
{{note|When Typeclassopedia was originally written, <hask>Pointed</hask> and <hask>Comonad</hask> were in the [http://hackage.haskell.org/package/category-extras category-extras] library. It has since been deprecated and they have moved to the [http://hackage.haskell.org/package/pointed pointed package] and the [http://hackage.haskell.org/package/comonad comonad package]. —Geheimdienst, Nov 2011}}
+
{{note|When Typeclassopedia was originally written, <code>Pointed</code> and <code>Comonad</code> were in the [http://hackage.haskell.org/package/category-extras category-extras] library. It has since been deprecated and they have moved to the [http://hackage.haskell.org/package/pointed pointed package] and the [http://hackage.haskell.org/package/comonad comonad package]. —Geheimdienst, Nov 2011}}
   
* <span style="border-bottom: 2px solid black">Solid arrows</span> point from the general to the specific; that is, if there is an arrow from <hask>Foo</hask> to <hask>Bar</hask> it means that every <hask>Bar</hask> is (or should be, or can be made into) a <hask>Foo</hask>.
+
* <span style="border-bottom: 2px solid black">Solid arrows</span> point from the general to the specific; that is, if there is an arrow from <code>Foo</code> to <code>Bar</code> it means that every <code>Bar</code> is (or should be, or can be made into) a <code>Foo</code>.
 
* <span style="border-bottom: 2px dotted black">Dotted arrows</span> indicate some other sort of relationship.
 
* <span style="border-bottom: 2px dotted black">Dotted arrows</span> indicate some other sort of relationship.
* <hask>Monad</hask> and <hask>ArrowApply</hask> are equivalent.
+
* <code>Monad</code> and <code>ArrowApply</code> are equivalent.
* <hask>Pointed</hask> and <hask>Comonad</hask> are greyed out since they are not actually (yet) in the standard Haskell libraries {{noteref}}.
+
* <code>Pointed</code> and <code>Comonad</code> are greyed out since they are not actually (yet) in the standard Haskell libraries {{noteref}}.
   
 
One more note before we begin. The original spelling of “type class” is with two words, as evidenced by, for example, the [http://haskell.org/onlinereport/ Haskell 98 Revised Report], early papers on type classes like [http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.5639 Type classes in Haskell] and [http://research.microsoft.com/en-us/um/people/simonpj/papers/type-class-design-space/ Type classes: exploring the design space], and [http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.168.4008 Hudak et al.’s history of Haskell]. However, as often happens with two-word phrases that see a lot of use, it has started to show up as one word (“typeclass”) or, rarely, hyphenated (“type-class”). When wearing my prescriptivist hat, I prefer “type class”, but realize (after changing into my descriptivist hat) that there's probably not much I can do about it.
 
One more note before we begin. The original spelling of “type class” is with two words, as evidenced by, for example, the [http://haskell.org/onlinereport/ Haskell 98 Revised Report], early papers on type classes like [http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.5639 Type classes in Haskell] and [http://research.microsoft.com/en-us/um/people/simonpj/papers/type-class-design-space/ Type classes: exploring the design space], and [http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.168.4008 Hudak et al.’s history of Haskell]. However, as often happens with two-word phrases that see a lot of use, it has started to show up as one word (“typeclass”) or, rarely, hyphenated (“type-class”). When wearing my prescriptivist hat, I prefer “type class”, but realize (after changing into my descriptivist hat) that there's probably not much I can do about it.
   
We now begin with the simplest type class of all: <hask>Functor</hask>.
+
We now begin with the simplest type class of all: <code>Functor</code>.
   
 
=Functor=
 
=Functor=
   
The <hask>Functor</hask> class ([http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor haddock]) is the most basic and ubiquitous type class in the Haskell libraries. A simple intuition is that a <hask>Functor</hask> represents a “container” of some sort, along with the ability to apply a function uniformly to every element in the container. For example, a list is a container of elements, and we can apply a function to every element of a list, using <hask>map</hask>. As another example, a binary tree is also a container of elements, and it’s not hard to come up with a way to recursively apply a function to every element in a tree.
+
The <code>Functor</code> class ([http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor haddock]) is the most basic and ubiquitous type class in the Haskell libraries. A simple intuition is that a <code>Functor</code> represents a “container” of some sort, along with the ability to apply a function uniformly to every element in the container. For example, a list is a container of elements, and we can apply a function to every element of a list, using <code>map</code>. As another example, a binary tree is also a container of elements, and it’s not hard to come up with a way to recursively apply a function to every element in a tree.
   
Another intuition is that a <hask>Functor</hask> represents some sort of “computational context”. This intuition is generally more useful, but is more difficult to explain, precisely because it is so general. Some examples later should help to clarify the <hask>Functor</hask>-as-context point of view.
+
Another intuition is that a <code>Functor</code> represents some sort of “computational context”. This intuition is generally more useful, but is more difficult to explain, precisely because it is so general. Some examples later should help to clarify the <code>Functor</code>-as-context point of view.
   
In the end, however, a <hask>Functor</hask> is simply what it is defined to be; doubtless there are many examples of <hask>Functor</hask> instances that don’t exactly fit either of the above intuitions. The wise student will focus their attention on definitions and examples, without leaning too heavily on any particular metaphor. Intuition will come, in time, on its own.
+
In the end, however, a <code>Functor</code> is simply what it is defined to be; doubtless there are many examples of <code>Functor</code> instances that don’t exactly fit either of the above intuitions. The wise student will focus their attention on definitions and examples, without leaning too heavily on any particular metaphor. Intuition will come, in time, on its own.
   
 
==Definition==
 
==Definition==
   
Here is the type class declaration for <hask>Functor</hask>:
+
Here is the type class declaration for <code>Functor</code>:
   
 
<haskell>
 
<haskell>
Line 71: Line 71:
 
</haskell>
 
</haskell>
   
<hask>Functor</hask> is exported by the <hask>Prelude</hask>, so no special imports are needed to use it.
+
<code>Functor</code> is exported by the <code>Prelude</code>, so no special imports are needed to use it.
   
First, the <hask>f a</hask> and <hask>f b</hask> in the type signature for <hask>fmap</hask> tell us that <hask>f</hask> isn’t just a type; it is a ''type constructor'' which takes another type as a parameter. (A more precise way to say this is that the ''kind'' of <hask>f</hask> must be <hask>* -> *</hask>.) For example, <hask>Maybe</hask> is such a type constructor: <hask>Maybe</hask> is not a type in and of itself, but requires another type as a parameter, like <hask>Maybe Integer</hask>. So it would not make sense to say <hask>instance Functor Integer</hask>, but it could make sense to say <hask>instance Functor Maybe</hask>.
+
First, the <code>f a</code> and <code>f b</code> in the type signature for <code>fmap</code> tell us that <code>f</code> isn’t just a type; it is a ''type constructor'' which takes another type as a parameter. (A more precise way to say this is that the ''kind'' of <code>f</code> must be <code>* -> *</code>.) For example, <code>Maybe</code> is such a type constructor: <code>Maybe</code> is not a type in and of itself, but requires another type as a parameter, like <code>Maybe Integer</code>. So it would not make sense to say <code>instance Functor Integer</code>, but it could make sense to say <code>instance Functor Maybe</code>.
   
Now look at the type of <hask>fmap</hask>: it takes any function from <hask>a</hask> to <hask>b</hask>, and a value of type <hask>f a</hask>, and outputs a value of type <hask>f b</hask>. From the container point of view, the intention is that <hask>fmap</hask> applies a function to each element of a container, without altering the structure of the container. From the context point of view, the intention is that <hask>fmap</hask> applies a function to a value without altering its context. Let’s look at a few specific examples.
+
Now look at the type of <code>fmap</code>: it takes any function from <code>a</code> to <code>b</code>, and a value of type <code>f a</code>, and outputs a value of type <code>f b</code>. From the container point of view, the intention is that <code>fmap</code> applies a function to each element of a container, without altering the structure of the container. From the context point of view, the intention is that <code>fmap</code> applies a function to a value without altering its context. Let’s look at a few specific examples.
   
 
==Instances==
 
==Instances==
   
{{note|Recall that <hask>[]</hask> has two meanings in Haskell: it can either stand for the empty list, or, as here, it can represent the list type constructor (pronounced “list-of”). In other words, the type <hask>[a]</hask> (list-of-<hask>a</hask>) can also be written <hask>[] a</hask>.}}
+
{{note|Recall that <code>[]</code> has two meanings in Haskell: it can either stand for the empty list, or, as here, it can represent the list type constructor (pronounced “list-of”). In other words, the type <code>[a]</code> (list-of-<code>a</code>) can also be written <code>[] a</code>.}}
   
{{note|You might ask why we need a separate <hask>map</hask> function. Why not just do away with the current list-only <hask>map</hask> function, and rename <hask>fmap</hask> to <hask>map</hask> instead? Well, that’s a good question. The usual argument is that someone just learning Haskell, when using <hask>map</hask> incorrectly, would much rather see an error about lists than about <hask>Functor</hask>s.}}
+
{{note|You might ask why we need a separate <code>map</code> function. Why not just do away with the current list-only <code>map</code> function, and rename <code>fmap</code> to <code>map</code> instead? Well, that’s a good question. The usual argument is that someone just learning Haskell, when using <code>map</code> incorrectly, would much rather see an error about lists than about <code>Functor</code>s.}}
   
As noted before, the list constructor <hask>[]</hask> is a functor {{noteref}}; we can use the standard list function <hask>map</hask> to apply a function to each element of a list {{noteref}}. The <hask>Maybe</hask> type constructor is also a functor, representing a container which might hold a single element. The function <hask>fmap g</hask> has no effect on <hask>Nothing</hask> (there are no elements to which <hask>g</hask> can be applied), and simply applies <hask>g</hask> to the single element inside a <hask>Just</hask>. Alternatively, under the context interpretation, the list functor represents a context of nondeterministic choice; that is, a list can be thought of as representing a single value which is nondeterministically chosen from among several possibilities (the elements of the list). Likewise, the <hask>Maybe</hask> functor represents a context with possible failure. These instances are:
+
As noted before, the list constructor <code>[]</code> is a functor {{noteref}}; we can use the standard list function <code>map</code> to apply a function to each element of a list {{noteref}}. The <code>Maybe</code> type constructor is also a functor, representing a container which might hold a single element. The function <code>fmap g</code> has no effect on <code>Nothing</code> (there are no elements to which <code>g</code> can be applied), and simply applies <code>g</code> to the single element inside a <code>Just</code>. Alternatively, under the context interpretation, the list functor represents a context of nondeterministic choice; that is, a list can be thought of as representing a single value which is nondeterministically chosen from among several possibilities (the elements of the list). Likewise, the <code>Maybe</code> functor represents a context with possible failure. These instances are:
   
 
<haskell>
 
<haskell>
Line 96: Line 96:
 
</haskell>
 
</haskell>
   
As an aside, in idiomatic Haskell code you will often see the letter <hask>f</hask> used to stand for both an arbitrary <hask>Functor</hask> and an arbitrary function. In this document, <hask>f</hask> represents only <hask>Functor</hask>s, and <hask>g</hask> or <hask>h</hask> always represent functions, but you should be aware of the potential confusion. In practice, what <hask>f</hask> stands for should always be clear from the context, by noting whether it is part of a type or part of the code.
+
As an aside, in idiomatic Haskell code you will often see the letter <code>f</code> used to stand for both an arbitrary <code>Functor</code> and an arbitrary function. In this document, <code>f</code> represents only <code>Functor</code>s, and <code>g</code> or <code>h</code> always represent functions, but you should be aware of the potential confusion. In practice, what <code>f</code> stands for should always be clear from the context, by noting whether it is part of a type or part of the code.
   
There are other <hask>Functor</hask> instances in the standard libraries; below are a few. Note that some of these instances are not exported by the <hask>Prelude</hask>; to access them, you can import <hask>Control.Monad.Instances</hask>.
+
There are other <code>Functor</code> instances in the standard libraries; below are a few. Note that some of these instances are not exported by the <code>Prelude</code>; to access them, you can import <code>Control.Monad.Instances</code>.
   
* <hask>Either e</hask> is an instance of <hask>Functor</hask>; <hask>Either e a</hask> represents a container which can contain either a value of type <hask>a</hask>, or a value of type <hask>e</hask> (often representing some sort of error condition). It is similar to <hask>Maybe</hask> in that it represents possible failure, but it can carry some extra information about the failure as well.
+
* <code>Either e</code> is an instance of <code>Functor</code>; <code>Either e a</code> represents a container which can contain either a value of type <code>a</code>, or a value of type <code>e</code> (often representing some sort of error condition). It is similar to <code>Maybe</code> in that it represents possible failure, but it can carry some extra information about the failure as well.
   
* <hask>((,) e)</hask> represents a container which holds an “annotation” of type <hask>e</hask> along with the actual value it holds. It might be clearer to write it as <hask>(e,)</hask>, by analogy with an operator section like <hask>(1+)</hask>, but that syntax is not allowed in types (although it is allowed in expressions with the <hask>TupleSections</hask> extension enabled). However, you can certainly ''think'' of it as <hask>(e,)</hask>.
+
* <code>((,) e)</code> represents a container which holds an “annotation” of type <code>e</code> along with the actual value it holds. It might be clearer to write it as <code>(e,)</code>, by analogy with an operator section like <code>(1+)</code>, but that syntax is not allowed in types (although it is allowed in expressions with the <code>TupleSections</code> extension enabled). However, you can certainly ''think'' of it as <code>(e,)</code>.
   
* <hask>((->) e)</hask> (which can be thought of as <hask>(e ->)</hask>; see above), the type of functions which take a value of type <hask>e</hask> as a parameter, is a <hask>Functor</hask>. As a container, <hask>(e -> a)</hask> represents a (possibly infinite) set of values of <hask>a</hask>, indexed by values of <hask>e</hask>. Alternatively, and more usefully, <hask>((->) e)</hask> can be thought of as a context in which a value of type <hask>e</hask> is available to be consulted in a read-only fashion. This is also why <hask>((->) e)</hask> is sometimes referred to as the ''reader monad''; more on this later.
+
* <code>((->) e)</code> (which can be thought of as <code>(e ->)</code>; see above), the type of functions which take a value of type <code>e</code> as a parameter, is a <code>Functor</code>. As a container, <code>(e -> a)</code> represents a (possibly infinite) set of values of <code>a</code>, indexed by values of <code>e</code>. Alternatively, and more usefully, <code>((->) e)</code> can be thought of as a context in which a value of type <code>e</code> is available to be consulted in a read-only fashion. This is also why <code>((->) e)</code> is sometimes referred to as the ''reader monad''; more on this later.
   
* <hask>IO</hask> is a <hask>Functor</hask>; a value of type <hask>IO a</hask> represents a computation producing a value of type <hask>a</hask> which may have I/O effects. If <hask>m</hask> computes the value <hask>x</hask> while producing some I/O effects, then <hask>fmap g m</hask> will compute the value <hask>g x</hask> while producing the same I/O effects.
+
* <code>IO</code> is a <code>Functor</code>; a value of type <code>IO a</code> represents a computation producing a value of type <code>a</code> which may have I/O effects. If <code>m</code> computes the value <code>x</code> while producing some I/O effects, then <code>fmap g m</code> will compute the value <code>g x</code> while producing the same I/O effects.
   
* Many standard types from the [http://hackage.haskell.org/package/containers/ containers library] (such as <hask>Tree</hask>, <hask>Map</hask>, and <hask>Sequence</hask>) are instances of <hask>Functor</hask>. A notable exception is <hask>Set</hask>, which cannot be made a <hask>Functor</hask> in Haskell (although it is certainly a mathematical functor) since it requires an <hask>Ord</hask> constraint on its elements; <hask>fmap</hask> must be applicable to ''any'' types <hask>a</hask> and <hask>b</hask>. However, <hask>Set</hask> (and other similarly restricted data types) can be made an instance of a suitable generalization of <hask>Functor</hask>, either by [http://article.gmane.org/gmane.comp.lang.haskell.cafe/78052/ making <code>a</code> and <code>b</code> arguments to the <code>Functor</code> type class themselves], or by adding an [http://blog.omega-prime.co.uk/?p=127 associated constraint].
+
* Many standard types from the [http://hackage.haskell.org/package/containers/ containers library] (such as <code>Tree</code>, <code>Map</code>, and <code>Sequence</code>) are instances of <code>Functor</code>. A notable exception is <code>Set</code>, which cannot be made a <code>Functor</code> in Haskell (although it is certainly a mathematical functor) since it requires an <code>Ord</code> constraint on its elements; <code>fmap</code> must be applicable to ''any'' types <code>a</code> and <code>b</code>. However, <code>Set</code> (and other similarly restricted data types) can be made an instance of a suitable generalization of <code>Functor</code>, either by [http://article.gmane.org/gmane.comp.lang.haskell.cafe/78052/ making <code>a</code> and <code>b</code> arguments to the <code>Functor</code> type class themselves], or by adding an [http://blog.omega-prime.co.uk/?p=127 associated constraint].
   
 
{{Exercises|
 
{{Exercises|
 
<ol>
 
<ol>
<li>Implement <hask>Functor</hask> instances for <hask>Either e</hask> and <hask>((->) e)</hask>.</li>
+
<li>Implement <code>Functor</code> instances for <code>Either e</code> and <code>((->) e)</code>.</li>
<li>Implement <hask>Functor</hask> instances for <hask>((,) e)</hask> and for <hask>Pair</hask>, defined as
+
<li>Implement <code>Functor</code> instances for <code>((,) e)</code> and for <code>Pair</code>, defined as
   
 
<haskell>data Pair a = Pair a a</haskell>
 
<haskell>data Pair a = Pair a a</haskell>
Line 119: Line 119:
 
Explain their similarities and differences.
 
Explain their similarities and differences.
 
</li>
 
</li>
<li>Give an example of a type which cannot be made an instance of <hask>Functor</hask> (without using <hask>undefined</hask>).
+
<li>Give an example of a type which cannot be made an instance of <code>Functor</code> (without using <code>undefined</code>).
 
</li>
 
</li>
 
</ol>
 
</ol>
Line 126: Line 126:
 
==Laws==
 
==Laws==
   
As far as the Haskell language itself is concerned, the only requirement to be a <hask>Functor</hask> is an implementation of <hask>fmap</hask> with the proper type. Any sensible <hask>Functor</hask> instance, however, will also satisfy the ''functor laws'', which are part of the definition of a mathematical functor. There are two:
+
As far as the Haskell language itself is concerned, the only requirement to be a <code>Functor</code> is an implementation of <code>fmap</code> with the proper type. Any sensible <code>Functor</code> instance, however, will also satisfy the ''functor laws'', which are part of the definition of a mathematical functor. There are two:
   
 
<haskell>
 
<haskell>
Line 133: Line 133:
 
</haskell>
 
</haskell>
   
{{note|Technically, these laws make <hask>f</hask> and <hask>fmap</hask> together an endofunctor on ''Hask'', the category of Haskell types (ignoring [[Bottom|&perp;]], which is a party pooper). See [http://en.wikibooks.org/wiki/Haskell/Category_theory Wikibook: Category theory].}}
+
{{note|Technically, these laws make <code>f</code> and <code>fmap</code> together an endofunctor on ''Hask'', the category of Haskell types (ignoring [[Bottom|&perp;]], which is a party pooper). See [http://en.wikibooks.org/wiki/Haskell/Category_theory Wikibook: Category theory].}}
   
Together, these laws ensure that <hask>fmap g</hask> does not change the ''structure'' of a container, only the elements. Equivalently, and more simply, they ensure that <hask>fmap g</hask> changes a value without altering its context {{noteref}}.
+
Together, these laws ensure that <code>fmap g</code> does not change the ''structure'' of a container, only the elements. Equivalently, and more simply, they ensure that <code>fmap g</code> changes a value without altering its context {{noteref}}.
   
 
The first law says that mapping the identity function over every item in a container has no effect. The second says that mapping a composition of two functions over every item in a container is the same as first mapping one function, and then mapping the other.
 
The first law says that mapping the identity function over every item in a container has no effect. The second says that mapping a composition of two functions over every item in a container is the same as first mapping one function, and then mapping the other.
   
As an example, the following code is a “valid” instance of <hask>Functor</hask> (it typechecks), but it violates the functor laws. Do you see why?
+
As an example, the following code is a “valid” instance of <code>Functor</code> (it typechecks), but it violates the functor laws. Do you see why?
   
 
<haskell>
 
<haskell>
Line 150: Line 150:
 
Any Haskeller worth their salt would reject this code as a gruesome abomination.
 
Any Haskeller worth their salt would reject this code as a gruesome abomination.
   
Unlike some other type classes we will encounter, a given type has at most one valid instance of <hask>Functor</hask>. This [http://article.gmane.org/gmane.comp.lang.haskell.libraries/15384 can be proven] via the [http://homepages.inf.ed.ac.uk/wadler/topics/parametricity.html#free ''free theorem''] for the type of <hask>fmap</hask>. In fact, the same argument shows that any <hask>Functor</hask> instance satisfying the first law (<hask>fmap id = id</hask>) will automatically satisfy the second law as well. Practically, this means that only the first law needs to be checked (usually by a very straightforward induction) to ensure that a <hask>Functor</hask> instance is valid.
+
Unlike some other type classes we will encounter, a given type has at most one valid instance of <code>Functor</code>. This [http://article.gmane.org/gmane.comp.lang.haskell.libraries/15384 can be proven] via the [http://homepages.inf.ed.ac.uk/wadler/topics/parametricity.html#free ''free theorem''] for the type of <code>fmap</code>. In fact, the same argument shows that any <code>Functor</code> instance satisfying the first law (<code>fmap id = id</code>) will automatically satisfy the second law as well. Practically, this means that only the first law needs to be checked (usually by a very straightforward induction) to ensure that a <code>Functor</code> instance is valid.
   
 
{{Exercises|
 
{{Exercises|
# Although it is not possible for a <hask>Functor</hask> instance to satisfy the first <hask>Functor</hask> law but not the second, the reverse is possible. Give an example of a (bogus) <hask>Functor</hask> instance which satisfies the second law but not the first.
+
# Although it is not possible for a <code>Functor</code> instance to satisfy the first <code>Functor</code> law but not the second, the reverse is possible. Give an example of a (bogus) <code>Functor</code> instance which satisfies the second law but not the first.
 
}}
 
}}
   
 
==Intuition==
 
==Intuition==
   
There are two fundamental ways to think about <hask>fmap</hask>. The first has already been mentioned: it takes two parameters, a function and a container, and applies the function “inside” the container, producing a new container. Alternately, we can think of <hask>fmap</hask> as applying a function to a value in a context (without altering the context).
+
There are two fundamental ways to think about <code>fmap</code>. The first has already been mentioned: it takes two parameters, a function and a container, and applies the function “inside” the container, producing a new container. Alternately, we can think of <code>fmap</code> as applying a function to a value in a context (without altering the context).
   
Just like all other Haskell functions of “more than one parameter”, however, <hask>fmap</hask> is actually ''curried'': it does not really take two parameters, but takes a single parameter and returns a function. For emphasis, we can write <hask>fmap</hask>’s type with extra parentheses: <hask>fmap :: (a -> b) -> (f a -> f b)</hask>. Written in this form, it is apparent that <hask>fmap</hask> transforms a “normal” function (<hask>g :: a -> b</hask>) into one which operates over containers/contexts (<hask>fmap g :: f a -> f b</hask>). This transformation is often referred to as a ''lift''; <hask>fmap</hask> “lifts” a function from the “normal world” into the “<hask>f</hask> world”.
+
Just like all other Haskell functions of “more than one parameter”, however, <code>fmap</code> is actually ''curried'': it does not really take two parameters, but takes a single parameter and returns a function. For emphasis, we can write <code>fmap</code>’s type with extra parentheses: <code>fmap :: (a -> b) -> (f a -> f b)</code>. Written in this form, it is apparent that <code>fmap</code> transforms a “normal” function (<code>g :: a -> b</code>) into one which operates over containers/contexts (<code>fmap g :: f a -> f b</code>). This transformation is often referred to as a ''lift''; <code>fmap</code> “lifts” a function from the “normal world” into the “<code>f</code> world”.
   
 
==Further reading==
 
==Further reading==
Line 168: Line 168:
 
=Pointed=
 
=Pointed=
   
{{note|The <hask>Pointed</hask> type class lives in the [http://hackage.haskell.org/package/pointed pointed library], moved from the [http://hackage.haskell.org/package/category-extras category-extras library]. The <hask>point</hask> function was originally named <hask>pure</hask>.<br><br>Edward Kmett, the author of category-extras, pointed, and many related packages, has since moved his focus to [http://hackage.haskell.org/package/semigroupoids semigroupoids] and [http://hackage.haskell.org/package/semigroups semigroups]. He finds them more interesting and useful, and considers <hask>Pointed</hask> to be historical now (he still provides the pointed package only because “people were whinging”). Nevertheless, <hask>Pointed</hask> has kept its value for explaining, and its place in Typeclassopedia. —Geheimdienst, Nov 2011}}
+
{{note|The <code>Pointed</code> type class lives in the [http://hackage.haskell.org/package/pointed pointed library], moved from the [http://hackage.haskell.org/package/category-extras category-extras library]. The <code>point</code> function was originally named <code>pure</code>.<br><br>Edward Kmett, the author of category-extras, pointed, and many related packages, has since moved his focus to [http://hackage.haskell.org/package/semigroupoids semigroupoids] and [http://hackage.haskell.org/package/semigroups semigroups]. He finds them more interesting and useful, and considers <code>Pointed</code> to be historical now (he still provides the pointed package only because “people were whinging”). Nevertheless, <code>Pointed</code> has kept its value for explaining, and its place in Typeclassopedia. —Geheimdienst, Nov 2011}}
   
The <hask>Pointed</hask> type class represents ''pointed functors''. It is not actually a type class in the standard libraries {{noteref}}. But it ''could'' be, and it’s useful in understanding a few other type classes, notably <hask>Applicative</hask> and <hask>Monad</hask>, so let’s pretend for a minute.
+
The <code>Pointed</code> type class represents ''pointed functors''. It is not actually a type class in the standard libraries {{noteref}}. But it ''could'' be, and it’s useful in understanding a few other type classes, notably <code>Applicative</code> and <code>Monad</code>, so let’s pretend for a minute.
   
Given a <hask>Functor</hask>, the <hask>Pointed</hask> class represents the additional ability to put a value into a “default context”. Often, this corresponds to creating a container with exactly one element, but it is more general than that. The type class declaration for <hask>Pointed</hask> is:
+
Given a <code>Functor</code>, the <code>Pointed</code> class represents the additional ability to put a value into a “default context”. Often, this corresponds to creating a container with exactly one element, but it is more general than that. The type class declaration for <code>Pointed</code> is:
   
 
<haskell>
 
<haskell>
Line 179: Line 179:
 
</haskell>
 
</haskell>
   
Most of the standard <hask>Functor</hask> instances could also be instances of <hask>Pointed</hask>—for example, the <hask>Maybe</hask> instance of <hask>Pointed</hask> is <hask>point = Just</hask>; there are many possible implementations for lists, the most natural of which is <hask>point x = [x]</hask>; for <hask>((->) e)</hask> it is ... well, I’ll let you work it out. (Just follow the types!)
+
Most of the standard <code>Functor</code> instances could also be instances of <code>Pointed</code>—for example, the <code>Maybe</code> instance of <code>Pointed</code> is <code>point = Just</code>; there are many possible implementations for lists, the most natural of which is <code>point x = [x]</code>; for <code>((->) e)</code> it is ... well, I’ll let you work it out. (Just follow the types!)
   
One example of a <hask>Functor</hask> which is not <hask>Pointed</hask> is <hask>((,) e)</hask>. If you try implementing <hask>point :: a -> (e,a)</hask> you will quickly see why: since the type <hask>e</hask> is completely arbitrary, there is no way to generate a value of type <hask>e</hask> out of thin air! However, as we will see, <hask>((,) e)</hask> can be made <hask>Pointed</hask> if we place an additional restriction on <hask>e</hask> which allows us to generate a default value of type <hask>e</hask> (the most common solution is to make <hask>e</hask> an instance of <hask>Monoid</hask>).
+
One example of a <code>Functor</code> which is not <code>Pointed</code> is <code>((,) e)</code>. If you try implementing <code>point :: a -> (e,a)</code> you will quickly see why: since the type <code>e</code> is completely arbitrary, there is no way to generate a value of type <code>e</code> out of thin air! However, as we will see, <code>((,) e)</code> can be made <code>Pointed</code> if we place an additional restriction on <code>e</code> which allows us to generate a default value of type <code>e</code> (the most common solution is to make <code>e</code> an instance of <code>Monoid</code>).
   
{{note|For those interested in category theory, this law states precisely that <hask>point</hask> is a natural transformation from the identity functor to <hask>f</hask>.}} The <hask>Pointed</hask> class has only one law {{noteref}}:
+
{{note|For those interested in category theory, this law states precisely that <code>point</code> is a natural transformation from the identity functor to <code>f</code>.}} The <code>Pointed</code> class has only one law {{noteref}}:
   
 
<haskell>
 
<haskell>
Line 189: Line 189:
 
</haskell>
 
</haskell>
   
{{note|... modulo &perp;, <hask>seq</hask>, and assuming a lawful <hask>Functor</hask> instance.}}
+
{{note|... modulo &perp;, <code>seq</code>, and assuming a lawful <code>Functor</code> instance.}}
   
However, you need not worry about it: this law is actually a so-called “free theorem” guaranteed by parametricity (see Wadler’s [http://homepages.inf.ed.ac.uk/wadler/topics/parametricity.html#free Theorems for free!]); it’s impossible to write an instance of <hask>Pointed</hask> which does not satisfy it {{noteref}}.
+
However, you need not worry about it: this law is actually a so-called “free theorem” guaranteed by parametricity (see Wadler’s [http://homepages.inf.ed.ac.uk/wadler/topics/parametricity.html#free Theorems for free!]); it’s impossible to write an instance of <code>Pointed</code> which does not satisfy it {{noteref}}.
   
 
=Applicative=
 
=Applicative=
   
A somewhat newer addition to the pantheon of standard Haskell type classes, ''applicative functors'' represent an abstraction lying exactly in between <hask>Functor</hask> and <hask>Monad</hask>, first described by McBride and Paterson. The title of their classic paper, [http://www.soi.city.ac.uk/~ross/papers/Applicative.html Applicative Programming with Effects], gives a hint at the intended intuition behind the [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html <code>Applicative</code>] type class. It encapsulates certain sorts of “effectful” computations in a functionally pure way, and encourages an “applicative” programming style. Exactly what these things mean will be seen later.
+
A somewhat newer addition to the pantheon of standard Haskell type classes, ''applicative functors'' represent an abstraction lying exactly in between <code>Functor</code> and <code>Monad</code>, first described by McBride and Paterson. The title of their classic paper, [http://www.soi.city.ac.uk/~ross/papers/Applicative.html Applicative Programming with Effects], gives a hint at the intended intuition behind the [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html <code>Applicative</code>] type class. It encapsulates certain sorts of “effectful” computations in a functionally pure way, and encourages an “applicative” programming style. Exactly what these things mean will be seen later.
   
 
==Definition==
 
==Definition==
   
The <hask>Applicative</hask> class adds a single capability to <hask>Pointed</hask> functors. Recall that <hask>Functor</hask> allows us to lift a “normal” function to a function on computational contexts. But <hask>fmap</hask> doesn’t allow us to apply a function which is itself in a context to a value in another context. <hask>Applicative</hask> gives us just such a tool. Here is the type class declaration for <hask>Applicative</hask>, as defined in <hask>Control.Applicative</hask>:
+
The <code>Applicative</code> class adds a single capability to <code>Pointed</code> functors. Recall that <code>Functor</code> allows us to lift a “normal” function to a function on computational contexts. But <code>fmap</code> doesn’t allow us to apply a function which is itself in a context to a value in another context. <code>Applicative</code> gives us just such a tool. Here is the type class declaration for <code>Applicative</code>, as defined in <code>Control.Applicative</code>:
   
 
<haskell>
 
<haskell>
Line 207: Line 207:
 
</haskell>
 
</haskell>
   
Note that every <hask>Applicative</hask> must also be a <hask>Functor</hask>. In fact, as we will see, <hask>fmap</hask> can be implemented using the <hask>Applicative</hask> methods, so every <hask>Applicative</hask> is a functor whether we like it or not; the <hask>Functor</hask> constraint forces us to be honest.
+
Note that every <code>Applicative</code> must also be a <code>Functor</code>. In fact, as we will see, <code>fmap</code> can be implemented using the <code>Applicative</code> methods, so every <code>Applicative</code> is a functor whether we like it or not; the <code>Functor</code> constraint forces us to be honest.
   
{{note|Recall that <hask>($)</hask> is just function application: <hask>f $ x {{=}} f x</hask>.}}
+
{{note|Recall that <code>($)</code> is just function application: <code>f $ x {{=}} f x</code>.}}
   
As always, it’s crucial to understand the type signature of <hask>(<*>)</hask>. The best way of thinking about it comes from noting that the type of <hask>(<*>)</hask> is similar to the type of <hask>($)</hask> {{noteref}}, but with everything enclosed in an <hask>f</hask>. In other words, <hask>(<*>)</hask> is just function application within a computational context. The type of <hask>(<*>)</hask> is also very similar to the type of <hask>fmap</hask>; the only difference is that the first parameter is <hask>f (a -> b)</hask>, a function in a context, instead of a “normal” function <hask>(a -> b)</hask>.
+
As always, it’s crucial to understand the type signature of <code>(<*>)</code>. The best way of thinking about it comes from noting that the type of <code>(<*>)</code> is similar to the type of <code>($)</code> {{noteref}}, but with everything enclosed in an <code>f</code>. In other words, <code>(<*>)</code> is just function application within a computational context. The type of <code>(<*>)</code> is also very similar to the type of <code>fmap</code>; the only difference is that the first parameter is <code>f (a -> b)</code>, a function in a context, instead of a “normal” function <code>(a -> b)</code>.
   
Of course, <hask>pure</hask> looks rather familiar. It is the <hask>point</hask> function from the <hask>Pointed</hask> type class. If we actually had it in the standard library, and <hask>pure</hask> appearing under the other name didn’t bother you, then <hask>Applicative</hask> could instead be defined as:
+
Of course, <code>pure</code> looks rather familiar. It is the <code>point</code> function from the <code>Pointed</code> type class. If we actually had it in the standard library, and <code>pure</code> appearing under the other name didn’t bother you, then <code>Applicative</code> could instead be defined as:
   
 
<haskell>
 
<haskell>
Line 225: Line 225:
 
[http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html haddock for Applicative] and [http://www.soi.city.ac.uk/~ross/papers/Applicative.html Applicative programming with effects]}}
 
[http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html haddock for Applicative] and [http://www.soi.city.ac.uk/~ross/papers/Applicative.html Applicative programming with effects]}}
   
There are several laws that <hask>Applicative</hask> instances should satisfy {{noteref}}, but only one is crucial to developing intuition, because it specifies how <hask>Applicative</hask> should relate to <hask>Functor</hask> (the other four mostly specify the exact sense in which <hask>pure</hask> deserves its name). This law is:
+
There are several laws that <code>Applicative</code> instances should satisfy {{noteref}}, but only one is crucial to developing intuition, because it specifies how <code>Applicative</code> should relate to <code>Functor</code> (the other four mostly specify the exact sense in which <code>pure</code> deserves its name). This law is:
   
 
<haskell>
 
<haskell>
Line 231: Line 231:
 
</haskell>
 
</haskell>
   
It says that mapping a pure function <hask>g</hask> over a context <hask>x</hask> is the same as first injecting <hask>g</hask> into a context with <hask>pure</hask>, and then applying it to <hask>x</hask> with <hask>(<*>)</hask>. In other words, we can decompose <hask>fmap</hask> into two more atomic operations: injection into a context, and application within a context. The <hask>Control.Applicative</hask> module also defines <hask>(<$>)</hask> as a synonym for <hask>fmap</hask>, so the above law can also be expressed as:
+
It says that mapping a pure function <code>g</code> over a context <code>x</code> is the same as first injecting <code>g</code> into a context with <code>pure</code>, and then applying it to <code>x</code> with <code>(<*>)</code>. In other words, we can decompose <code>fmap</code> into two more atomic operations: injection into a context, and application within a context. The <code>Control.Applicative</code> module also defines <code>(<$>)</code> as a synonym for <code>fmap</code>, so the above law can also be expressed as:
   
<hask>g <$> x = pure g <*> x</hask>.
+
<code>g <$> x = pure g <*> x</code>.
   
 
==Instances==
 
==Instances==
   
Most of the standard types which are instances of <hask>Functor</hask> are also instances of <hask>Applicative</hask>.
+
Most of the standard types which are instances of <code>Functor</code> are also instances of <code>Applicative</code>.
   
<hask>Maybe</hask> can easily be made an instance of <hask>Applicative</hask>; writing such an instance is left as an exercise for the reader.
+
<code>Maybe</code> can easily be made an instance of <code>Applicative</code>; writing such an instance is left as an exercise for the reader.
   
The list type constructor <hask>[]</hask> can actually be made an instance of <hask>Applicative</hask> in two ways; essentially, it comes down to whether we want to think of lists as ordered collections of elements, or as contexts representing multiple results of a nondeterministic computation (see Wadler’s [http://www.springerlink.com/content/y7450255v2670167/ How to replace failure by a list of successes]).
+
The list type constructor <code>[]</code> can actually be made an instance of <code>Applicative</code> in two ways; essentially, it comes down to whether we want to think of lists as ordered collections of elements, or as contexts representing multiple results of a nondeterministic computation (see Wadler’s [http://www.springerlink.com/content/y7450255v2670167/ How to replace failure by a list of successes]).
   
Let’s first consider the collection point of view. Since there can only be one instance of a given type class for any particular type, one or both of the list instances of <hask>Applicative</hask> need to be defined for a <hask>newtype</hask> wrapper; as it happens, the nondeterministic computation instance is the default, and the collection instance is defined in terms of a <hask>newtype</hask> called <hask>ZipList</hask>. This instance is:
+
Let’s first consider the collection point of view. Since there can only be one instance of a given type class for any particular type, one or both of the list instances of <code>Applicative</code> need to be defined for a <code>newtype</code> wrapper; as it happens, the nondeterministic computation instance is the default, and the collection instance is defined in terms of a <code>newtype</code> called <code>ZipList</code>. This instance is:
   
 
<haskell>
 
<haskell>
Line 253: Line 253:
 
</haskell>
 
</haskell>
   
To apply a list of functions to a list of inputs with <hask>(<*>)</hask>, we just match up the functions and inputs elementwise, and produce a list of the resulting outputs. In other words, we “zip” the lists together with function application, <hask>($)</hask>; hence the name <hask>ZipList</hask>. As an exercise, determine the correct definition of <hask>pure</hask>—there is only one implementation that satisfies the law (see section “Laws”).
+
To apply a list of functions to a list of inputs with <code>(<*>)</code>, we just match up the functions and inputs elementwise, and produce a list of the resulting outputs. In other words, we “zip” the lists together with function application, <code>($)</code>; hence the name <code>ZipList</code>. As an exercise, determine the correct definition of <code>pure</code>—there is only one implementation that satisfies the law (see section “Laws”).
   
The other <hask>Applicative</hask> instance for lists, based on the nondeterministic computation point of view, is:
+
The other <code>Applicative</code> instance for lists, based on the nondeterministic computation point of view, is:
   
 
<haskell>
 
<haskell>
Line 265: Line 265:
 
Instead of applying functions to inputs pairwise, we apply each function to all the inputs in turn, and collect all the results in a list.
 
Instead of applying functions to inputs pairwise, we apply each function to all the inputs in turn, and collect all the results in a list.
   
Now we can write nondeterministic computations in a natural style. To add the numbers <hask>3</hask> and <hask>4</hask> deterministically, we can of course write <hask>(+) 3 4</hask>. But suppose instead of <hask>3</hask> we have a nondeterministic computation that might result in <hask>2</hask>, <hask>3</hask>, or <hask>4</hask>; then we can write
+
Now we can write nondeterministic computations in a natural style. To add the numbers <code>3</code> and <code>4</code> deterministically, we can of course write <code>(+) 3 4</code>. But suppose instead of <code>3</code> we have a nondeterministic computation that might result in <code>2</code>, <code>3</code>, or <code>4</code>; then we can write
   
 
<haskell>
 
<haskell>
Line 277: Line 277:
 
</haskell>
 
</haskell>
   
There are several other <hask>Applicative</hask> instances as well:
+
There are several other <code>Applicative</code> instances as well:
   
* <hask>IO</hask> is an instance of <hask>Applicative</hask>, and behaves exactly as you would think: when <hask>g <$> m1 <*> m2 <*> m3</hask> is executed, the effects from the <hask>mi</hask>’s happen in order from left to right.
+
* <code>IO</code> is an instance of <code>Applicative</code>, and behaves exactly as you would think: when <code>g <$> m1 <*> m2 <*> m3</code> is executed, the effects from the <code>mi</code>’s happen in order from left to right.
   
* <hask>((,) a)</hask> is an <hask>Applicative</hask>, as long as <hask>a</hask> is an instance of <hask>Monoid</hask> ([[#Monoid|section Monoid]]). The <hask>a</hask> values are accumulated in parallel with the computation.
+
* <code>((,) a)</code> is an <code>Applicative</code>, as long as <code>a</code> is an instance of <code>Monoid</code> ([[#Monoid|section Monoid]]). The <code>a</code> values are accumulated in parallel with the computation.
   
* The <hask>Applicative</hask> module defines the <hask>Const</hask> type constructor; a value of type <hask>Const a b</hask> simply contains an <hask>a</hask>. This is an instance of <hask>Applicative</hask> for any <hask>Monoid a</hask>; this instance becomes especially useful in conjunction with things like <hask>Foldable</hask> ([[#Foldable|section Foldable]]).
+
* The <code>Applicative</code> module defines the <code>Const</code> type constructor; a value of type <code>Const a b</code> simply contains an <code>a</code>. This is an instance of <code>Applicative</code> for any <code>Monoid a</code>; this instance becomes especially useful in conjunction with things like <code>Foldable</code> ([[#Foldable|section Foldable]]).
   
* The <hask>WrappedMonad</hask> and <hask>WrappedArrow</hask> newtypes make any instances of <hask>Monad</hask> ([[#Monad|section Monad]]) or <hask>Arrow</hask> ([[#Arrow|section Arrow]]) respectively into instances of <hask>Applicative</hask>; as we will see when we study those type classes, both are strictly more expressive than <hask>Applicative</hask>, in the sense that the <hask>Applicative</hask> methods can be implemented in terms of their methods.
+
* The <code>WrappedMonad</code> and <code>WrappedArrow</code> newtypes make any instances of <code>Monad</code> ([[#Monad|section Monad]]) or <code>Arrow</code> ([[#Arrow|section Arrow]]) respectively into instances of <code>Applicative</code>; as we will see when we study those type classes, both are strictly more expressive than <code>Applicative</code>, in the sense that the <code>Applicative</code> methods can be implemented in terms of their methods.
   
 
==Intuition==
 
==Intuition==
   
McBride and Paterson’s paper introduces the notation <math>[[g \; x_1 \; x_2 \; \cdots \; x_n]]\ </math> to denote function application in a computational context. If each <math>x_i\ </math> has type <math>f \; t_i\ </math> for some applicative functor <math>f\ </math>, and <math>g\ </math> has type <math>t_1 \to t_2 \to \dots \to t_n \to t\ </math>, then the entire expression <math>[[g \; x_1 \; \cdots \; x_n]]\ </math> has type <math>f \; t\ </math>. You can think of this as applying a function to multiple “effectful” arguments. In this sense, the double bracket notation is a generalization of <hask>fmap</hask>, which allows us to apply a function to a single argument in a context.
+
McBride and Paterson’s paper introduces the notation <math>[[g \; x_1 \; x_2 \; \cdots \; x_n]]\ </math> to denote function application in a computational context. If each <math>x_i\ </math> has type <math>f \; t_i\ </math> for some applicative functor <math>f\ </math>, and <math>g\ </math> has type <math>t_1 \to t_2 \to \dots \to t_n \to t\ </math>, then the entire expression <math>[[g \; x_1 \; \cdots \; x_n]]\ </math> has type <math>f \; t\ </math>. You can think of this as applying a function to multiple “effectful” arguments. In this sense, the double bracket notation is a generalization of <code>fmap</code>, which allows us to apply a function to a single argument in a context.
   
Why do we need <hask>Applicative</hask> to implement this generalization of <hask>fmap</hask>? Suppose we use <hask>fmap</hask> to apply <hask>g</hask> to the first parameter <hask>x1</hask>. Then we get something of type <hask>f (t2 -> ... t)</hask>, but now we are stuck: we can’t apply this function-in-a-context to the next argument with <hask>fmap</hask>. However, this is precisely what <hask>(<*>)</hask> allows us to do.
+
Why do we need <code>Applicative</code> to implement this generalization of <code>fmap</code>? Suppose we use <code>fmap</code> to apply <code>g</code> to the first parameter <code>x1</code>. Then we get something of type <code>f (t2 -> ... t)</code>, but now we are stuck: we can’t apply this function-in-a-context to the next argument with <code>fmap</code>. However, this is precisely what <code>(<*>)</code> allows us to do.
   
 
This suggests the proper translation of the idealized notation <math>[[g \; x_1 \; x_2 \; \cdots \; x_n]]\ </math> into Haskell, namely
 
This suggests the proper translation of the idealized notation <math>[[g \; x_1 \; x_2 \; \cdots \; x_n]]\ </math> into Haskell, namely
Line 298: Line 298:
 
</haskell>
 
</haskell>
   
recalling that <hask>Control.Applicative</hask> defines <hask>(<$>)</hask> as convenient infix shorthand for <hask>fmap</hask>. This is what is meant by an “applicative style”—effectful computations can still be described in terms of function application; the only difference is that we have to use the special operator <hask>(<*>)</hask> for application instead of simple juxtaposition.
+
recalling that <code>Control.Applicative</code> defines <code>(<$>)</code> as convenient infix shorthand for <code>fmap</code>. This is what is meant by an “applicative style”—effectful computations can still be described in terms of function application; the only difference is that we have to use the special operator <code>(<*>)</code> for application instead of simple juxtaposition.
   
 
==Further reading==
 
==Further reading==
   
There are many other useful combinators in the standard libraries implemented in terms of <hask>pure</hask> and <hask>(<*>)</hask>: for example, <hask>(*>)</hask>, <hask>(<*)</hask>, <hask>(<**>)</hask>, <hask>(<$)</hask>, and so on (see [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html haddock for Applicative]). Judicious use of such secondary combinators can often make code using <hask>Applicative</hask>s much easier to read.
+
There are many other useful combinators in the standard libraries implemented in terms of <code>pure</code> and <code>(<*>)</code>: for example, <code>(*>)</code>, <code>(<*)</code>, <code>(<**>)</code>, <code>(<$)</code>, and so on (see [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html haddock for Applicative]). Judicious use of such secondary combinators can often make code using <code>Applicative</code>s much easier to read.
   
[http://www.soi.city.ac.uk/~ross/papers/Applicative.html McBride and Paterson’s original paper] is a treasure-trove of information and examples, as well as some perspectives on the connection between <hask>Applicative</hask> and category theory. Beginners will find it difficult to make it through the entire paper, but it is extremely well-motivated—even beginners will be able to glean something from reading as far as they are able.
+
[http://www.soi.city.ac.uk/~ross/papers/Applicative.html McBride and Paterson’s original paper] is a treasure-trove of information and examples, as well as some perspectives on the connection between <code>Applicative</code> and category theory. Beginners will find it difficult to make it through the entire paper, but it is extremely well-motivated—even beginners will be able to glean something from reading as far as they are able.
   
 
{{note|Introduced by [http://conal.net/papers/simply-reactive/ an earlier paper] that was since superceded by [http://conal.net/papers/push-pull-frp/ Push-pull functional reactive programming]. —Geheimdienst, Nov 2011}}
 
{{note|Introduced by [http://conal.net/papers/simply-reactive/ an earlier paper] that was since superceded by [http://conal.net/papers/push-pull-frp/ Push-pull functional reactive programming]. —Geheimdienst, Nov 2011}}
   
Conal Elliott has been one of the biggest proponents of <hask>Applicative</hask>. For example, the [http://conal.net/papers/functional-images/ Pan library for functional images] and the reactive library for functional reactive programming (FRP) {{noteref}} make key use of it; his blog also contains [http://conal.net/blog/tag/applicative-functor many examples of <hask>Applicative</hask> in action]. Building on the work of McBride and Paterson, Elliott also built the [[TypeCompose]] library, which embodies the observation (among others) that <hask>Applicative</hask> types are closed under composition; therefore, <hask>Applicative</hask> instances can often be automatically derived for complex types built out of simpler ones.
+
Conal Elliott has been one of the biggest proponents of <code>Applicative</code>. For example, the [http://conal.net/papers/functional-images/ Pan library for functional images] and the reactive library for functional reactive programming (FRP) {{noteref}} make key use of it; his blog also contains [http://conal.net/blog/tag/applicative-functor many examples of <code>Applicative</code> in action]. Building on the work of McBride and Paterson, Elliott also built the [[TypeCompose]] library, which embodies the observation (among others) that <code>Applicative</code> types are closed under composition; therefore, <code>Applicative</code> instances can often be automatically derived for complex types built out of simpler ones.
   
Although the [http://legacy.cs.uu.nl/daan/parsec.html Parsec parsing library] ([http://legacy.cs.uu.nl/daan/download/papers/parsec-paper.pdf paper]) was originally designed for use as a monad, in its most common use cases an <hask>Applicative</hask> instance can be used to great effect; [http://www.serpentine.com/blog/2008/02/06/the-basics-of-applicative-functors-put-to-practical-work/ Bryan O’Sullivan’s blog post] is a good starting point. If the extra power provided by <hask>Monad</hask> isn’t needed, it’s usually a good idea to use <hask>Applicative</hask> instead.
+
Although the [http://legacy.cs.uu.nl/daan/parsec.html Parsec parsing library] ([http://legacy.cs.uu.nl/daan/download/papers/parsec-paper.pdf paper]) was originally designed for use as a monad, in its most common use cases an <code>Applicative</code> instance can be used to great effect; [http://www.serpentine.com/blog/2008/02/06/the-basics-of-applicative-functors-put-to-practical-work/ Bryan O’Sullivan’s blog post] is a good starting point. If the extra power provided by <code>Monad</code> isn’t needed, it’s usually a good idea to use <code>Applicative</code> instead.
   
A couple other nice examples of <hask>Applicative</hask> in action include the [http://chrisdone.com/blog/html/2009-02-10-applicative-configfile-hsql.html ConfigFile and HSQL libraries] and the [http://groups.inf.ed.ac.uk/links/formlets/ formlets library].
+
A couple other nice examples of <code>Applicative</code> in action include the [http://chrisdone.com/blog/html/2009-02-10-applicative-configfile-hsql.html ConfigFile and HSQL libraries] and the [http://groups.inf.ed.ac.uk/links/formlets/ formlets library].
   
 
=Monad=
 
=Monad=
   
It’s a safe bet that if you’re reading this, you’ve heard of monads—although it’s quite possible you’ve never heard of <hask>Applicative</hask> before, or <hask>Arrow</hask>, or even <hask>Monoid</hask>. Why are monads such a big deal in Haskell? There are several reasons.
+
It’s a safe bet that if you’re reading this, you’ve heard of monads—although it’s quite possible you’ve never heard of <code>Applicative</code> before, or <code>Arrow</code>, or even <code>Monoid</code>. Why are monads such a big deal in Haskell? There are several reasons.
   
 
* Haskell does, in fact, single out monads for special attention by making them the framework in which to construct I/O operations.
 
* Haskell does, in fact, single out monads for special attention by making them the framework in which to construct I/O operations.
* Haskell also singles out monads for special attention by providing a special syntactic sugar for monadic expressions: the <hask>do</hask>-notation.
+
* Haskell also singles out monads for special attention by providing a special syntactic sugar for monadic expressions: the <code>do</code>-notation.
* <hask>Monad</hask> has been around longer than other abstract models of computation such as <hask>Applicative</hask> or <hask>Arrow</hask>.
+
* <code>Monad</code> has been around longer than other abstract models of computation such as <code>Applicative</code> or <code>Arrow</code>.
 
* The more monad tutorials there are, the harder people think monads must be, and the more new monad tutorials are written by people who think they finally “get” monads (the [http://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/ monad tutorial fallacy]).
 
* The more monad tutorials there are, the harder people think monads must be, and the more new monad tutorials are written by people who think they finally “get” monads (the [http://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/ monad tutorial fallacy]).
   
 
I will let you judge for yourself whether these are good reasons.
 
I will let you judge for yourself whether these are good reasons.
   
In the end, despite all the hoopla, <hask>Monad</hask> is just another type class. Let’s take a look at its definition.
+
In the end, despite all the hoopla, <code>Monad</code> is just another type class. Let’s take a look at its definition.
   
 
==Definition==
 
==Definition==
Line 341: Line 341:
 
</haskell>
 
</haskell>
   
The <hask>Monad</hask> type class is exported by the <hask>Prelude</hask>, along with a few standard instances. However, many utility functions are found in [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html <code>Control.Monad</code>], and there are also several instances (such as <hask>((->) e)</hask>) defined in [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad-Instances.html <code>Control.Monad.Instances</code>].
+
The <code>Monad</code> type class is exported by the <code>Prelude</code>, along with a few standard instances. However, many utility functions are found in [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html <code>Control.Monad</code>], and there are also several instances (such as <code>((->) e)</code>) defined in [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad-Instances.html <code>Control.Monad.Instances</code>].
   
Let’s examine the methods in the <hask>Monad</hask> class one by one. The type of <hask>return</hask> should look familiar; it’s the same as <hask>pure</hask>. Indeed, <hask>return</hask> ''is'' <hask>pure</hask>, but with an unfortunate name. (Unfortunate, since someone coming from an imperative programming background might think that <hask>return</hask> is like the C or Java keyword of the same name, when in fact the similarities are minimal.) From a mathematical point of view, every monad is an applicative functor, but for historical reasons, the <hask>Monad</hask> type class declaration unfortunately does not require this.
+
Let’s examine the methods in the <code>Monad</code> class one by one. The type of <code>return</code> should look familiar; it’s the same as <code>pure</code>. Indeed, <code>return</code> ''is'' <code>pure</code>, but with an unfortunate name. (Unfortunate, since someone coming from an imperative programming background might think that <code>return</code> is like the C or Java keyword of the same name, when in fact the similarities are minimal.) From a mathematical point of view, every monad is an applicative functor, but for historical reasons, the <code>Monad</code> type class declaration unfortunately does not require this.
   
We can see that <hask>(>>)</hask> is a specialized version of <hask>(>>=)</hask>, with a default implementation given. It is only included in the type class declaration so that specific instances of <hask>Monad</hask> can override the default implementation of <hask>(>>)</hask> with a more efficient one, if desired. Also, note that although <hask>_ >> n = n</hask> would be a type-correct implementation of <hask>(>>)</hask>, it would not correspond to the intended semantics: the intention is that <hask>m >> n</hask> ignores the ''result'' of <hask>m</hask>, but not its ''effects''.
+
We can see that <code>(>>)</code> is a specialized version of <code>(>>=)</code>, with a default implementation given. It is only included in the type class declaration so that specific instances of <code>Monad</code> can override the default implementation of <code>(>>)</code> with a more efficient one, if desired. Also, note that although <code>_ >> n = n</code> would be a type-correct implementation of <code>(>>)</code>, it would not correspond to the intended semantics: the intention is that <code>m >> n</code> ignores the ''result'' of <code>m</code>, but not its ''effects''.
   
The <hask>fail</hask> function is an awful hack that has no place in the <hask>Monad</hask> class; more on this later.
+
The <code>fail</code> function is an awful hack that has no place in the <code>Monad</code> class; more on this later.
   
The only really interesting thing to look at—and what makes <hask>Monad</hask> strictly more powerful than <hask>Applicative</hask>—is <hask>(>>=)</hask>, which is often called ''bind''. An alternative definition of <hask>Monad</hask> could look like:
+
The only really interesting thing to look at—and what makes <code>Monad</code> strictly more powerful than <code>Applicative</code>—is <code>(>>=)</code>, which is often called ''bind''. An alternative definition of <code>Monad</code> could look like:
   
 
<haskell>
 
<haskell>
Line 356: Line 356:
 
</haskell>
 
</haskell>
   
We could spend a while talking about the intuition behind <hask>(>>=)</hask>—and we will. But first, let’s look at some examples.
+
We could spend a while talking about the intuition behind <code>(>>=)</code>—and we will. But first, let’s look at some examples.
   
 
==Instances==
 
==Instances==
   
Even if you don’t understand the intuition behind the <hask>Monad</hask> class, you can still create instances of it by just seeing where the types lead you. You may be surprised to find that this actually gets you a long way towards understanding the intuition; at the very least, it will give you some concrete examples to play with as you read more about the <hask>Monad</hask> class in general. The first few examples are from the standard <hask>Prelude</hask>; the remaining examples are from the monad transformer library ([http://hackage.haskell.org/package/mtl mtl]).
+
Even if you don’t understand the intuition behind the <code>Monad</code> class, you can still create instances of it by just seeing where the types lead you. You may be surprised to find that this actually gets you a long way towards understanding the intuition; at the very least, it will give you some concrete examples to play with as you read more about the <code>Monad</code> class in general. The first few examples are from the standard <code>Prelude</code>; the remaining examples are from the monad transformer library ([http://hackage.haskell.org/package/mtl mtl]).
   
* The simplest possible instance of <hask>Monad</hask> is [http://hackage.haskell.org/packages/archive/mtl/1.1.0.2/doc/html/Control-Monad-Identity.html <hask>Identity</hask>], which is described in Dan Piponi’s highly recommended blog post on [http://blog.sigfpe.com/2007/04/trivial-monad.html The Trivial Monad]. Despite being “trivial”, it is a great introduction to the <hask>Monad</hask> type class, and contains some good exercises to get your brain working.
+
* The simplest possible instance of <code>Monad</code> is [http://hackage.haskell.org/packages/archive/mtl/1.1.0.2/doc/html/Control-Monad-Identity.html <code>Identity</code>], which is described in Dan Piponi’s highly recommended blog post on [http://blog.sigfpe.com/2007/04/trivial-monad.html The Trivial Monad]. Despite being “trivial”, it is a great introduction to the <code>Monad</code> type class, and contains some good exercises to get your brain working.
* The next simplest instance of <hask>Monad</hask> is <hask>Maybe</hask>. We already know how to write <hask>return</hask>/<hask>pure</hask> for <hask>Maybe</hask>. So how do we write <hask>(>>=)</hask>? Well, let’s think about its type. Specializing for <hask>Maybe</hask>, we have
+
* The next simplest instance of <code>Monad</code> is <code>Maybe</code>. We already know how to write <code>return</code>/<code>pure</code> for <code>Maybe</code>. So how do we write <code>(>>=)</code>? Well, let’s think about its type. Specializing for <code>Maybe</code>, we have
 
<haskell>
 
<haskell>
 
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b.
 
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b.
 
</haskell>
 
</haskell>
   
:If the first argument to <hask>(>>=)</hask> is <hask>Just x</hask>, then we have something of type <hask>a</hask> (namely, <hask>x</hask>), to which we can apply the second argument—resulting in a <hask>Maybe b</hask>, which is exactly what we wanted. What if the first argument to <hask>(>>=)</hask> is <hask>Nothing</hask>? In that case, we don’t have anything to which we can apply the <hask>a -> Maybe b</hask> function, so there’s only one thing we can do: yield <hask>Nothing</hask>. This instance is:
+
:If the first argument to <code>(>>=)</code> is <code>Just x</code>, then we have something of type <code>a</code> (namely, <code>x</code>), to which we can apply the second argument—resulting in a <code>Maybe b</code>, which is exactly what we wanted. What if the first argument to <code>(>>=)</code> is <code>Nothing</code>? In that case, we don’t have anything to which we can apply the <code>a -> Maybe b</code> function, so there’s only one thing we can do: yield <code>Nothing</code>. This instance is:
   
 
<haskell>
 
<haskell>
Line 377: Line 377:
 
</haskell>
 
</haskell>
   
:We can already get a bit of intuition as to what is going on here: if we build up a computation by chaining together a bunch of functions with <hask>(>>=)</hask>, as soon as any one of them fails, the entire computation will fail (because <hask>Nothing >>= f</hask> is <hask>Nothing</hask>, no matter what <hask>f</hask> is). The entire computation succeeds only if all the constituent functions individually succeed. So the <hask>Maybe</hask> monad models computations which may fail.
+
:We can already get a bit of intuition as to what is going on here: if we build up a computation by chaining together a bunch of functions with <code>(>>=)</code>, as soon as any one of them fails, the entire computation will fail (because <code>Nothing >>= f</code> is <code>Nothing</code>, no matter what <code>f</code> is). The entire computation succeeds only if all the constituent functions individually succeed. So the <code>Maybe</code> monad models computations which may fail.
   
* The <hask>Monad</hask> instance for the list constructor <hask>[]</hask> is similar to its <hask>Applicative</hask> instance; see the exercise below.
+
* The <code>Monad</code> instance for the list constructor <code>[]</code> is similar to its <code>Applicative</code> instance; see the exercise below.
   
* Of course, the <hask>IO</hask> constructor is famously a <hask>Monad</hask>, but its implementation is somewhat magical, and may in fact differ from compiler to compiler. It is worth emphasizing that the <hask>IO</hask> monad is the ''only'' monad which is magical. It allows us to build up, in an entirely pure way, values representing possibly effectful computations. The special value <hask>main</hask>, of type <hask>IO ()</hask>, is taken by the runtime and actually executed, producing actual effects. Every other monad is functionally pure, and requires no special compiler support. We often speak of monadic values as “effectful computations”, but this is because some monads allow us to write code ''as if'' it has side effects, when in fact the monad is hiding the plumbing which allows these apparent side effects to be implemented in a functionally pure way.
+
* Of course, the <code>IO</code> constructor is famously a <code>Monad</code>, but its implementation is somewhat magical, and may in fact differ from compiler to compiler. It is worth emphasizing that the <code>IO</code> monad is the ''only'' monad which is magical. It allows us to build up, in an entirely pure way, values representing possibly effectful computations. The special value <code>main</code>, of type <code>IO ()</code>, is taken by the runtime and actually executed, producing actual effects. Every other monad is functionally pure, and requires no special compiler support. We often speak of monadic values as “effectful computations”, but this is because some monads allow us to write code ''as if'' it has side effects, when in fact the monad is hiding the plumbing which allows these apparent side effects to be implemented in a functionally pure way.
   
* As mentioned earlier, <hask>((->) e)</hask> is known as the ''reader monad'', since it describes computations in which a value of type <hask>e</hask> is available as a read-only environment.
+
* As mentioned earlier, <code>((->) e)</code> is known as the ''reader monad'', since it describes computations in which a value of type <code>e</code> is available as a read-only environment.
   
:The [http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Reader.html <hask>Control.Monad.Reader</hask>] module provides the <hask>Reader e a</hask> type, which is just a convenient <hask>newtype</hask> wrapper around <hask>(e -> a)</hask>, along with an appropriate <hask>Monad</hask> instance and some <hask>Reader</hask>-specific utility functions such as <hask>ask</hask> (retrieve the environment), <hask>asks</hask> (retrieve a function of the environment), and <hask>local</hask> (run a subcomputation under a different environment).
+
:The [http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Reader.html <code>Control.Monad.Reader</code>] module provides the <code>Reader e a</code> type, which is just a convenient <code>newtype</code> wrapper around <code>(e -> a)</code>, along with an appropriate <code>Monad</code> instance and some <code>Reader</code>-specific utility functions such as <code>ask</code> (retrieve the environment), <code>asks</code> (retrieve a function of the environment), and <code>local</code> (run a subcomputation under a different environment).
   
* The [http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Writer-Lazy.html <hask>Control.Monad.Writer</hask>] module provides the <hask>Writer</hask> monad, which allows information to be collected as a computation progresses. <hask>Writer w a</hask> is isomorphic to <hask>(a,w)</hask>, where the output value <hask>a</hask> is carried along with an annotation or “log” of type <hask>w</hask>, which must be an instance of <hask>Monoid</hask> (see [[#Monoid|section Monoid]]); the special function <hask>tell</hask> performs logging.
+
* The [http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Writer-Lazy.html <code>Control.Monad.Writer</code>] module provides the <code>Writer</code> monad, which allows information to be collected as a computation progresses. <code>Writer w a</code> is isomorphic to <code>(a,w)</code>, where the output value <code>a</code> is carried along with an annotation or “log” of type <code>w</code>, which must be an instance of <code>Monoid</code> (see [[#Monoid|section Monoid]]); the special function <code>tell</code> performs logging.
   
* The [http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State-Lazy.html <hask>Control.Monad.State</hask>] module provides the <hask>State s a</hask> type, a <hask>newtype</hask> wrapper around <hask>s -> (a,s)</hask>. Something of type <hask>State s a</hask> represents a stateful computation which produces an <hask>a</hask> but can access and modify the state of type <hask>s</hask> along the way. The module also provides <hask>State</hask>-specific utility functions such as <hask>get</hask> (read the current state), <hask>gets</hask> (read a function of the current state), <hask>put</hask> (overwrite the state), and <hask>modify</hask> (apply a function to the state).
+
* The [http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State-Lazy.html <code>Control.Monad.State</code>] module provides the <code>State s a</code> type, a <code>newtype</code> wrapper around <code>s -> (a,s)</code>. Something of type <code>State s a</code> represents a stateful computation which produces an <code>a</code> but can access and modify the state of type <code>s</code> along the way. The module also provides <code>State</code>-specific utility functions such as <code>get</code> (read the current state), <code>gets</code> (read a function of the current state), <code>put</code> (overwrite the state), and <code>modify</code> (apply a function to the state).
   
* The [http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Cont.html <hask>Control.Monad.Cont</hask>] module provides the <hask>Cont</hask> monad, which represents computations in continuation-passing style. It can be used to suspend and resume computations, and to implement non-local transfers of control, co-routines, other complex control structures—all in a functionally pure way. <hask>Cont</hask> has been called the [http://blog.sigfpe.com/2008/12/mother-of-all-monads.html “mother of all monads”] because of its universal properties.
+
* The [http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Cont.html <code>Control.Monad.Cont</code>] module provides the <code>Cont</code> monad, which represents computations in continuation-passing style. It can be used to suspend and resume computations, and to implement non-local transfers of control, co-routines, other complex control structures—all in a functionally pure way. <code>Cont</code> has been called the [http://blog.sigfpe.com/2008/12/mother-of-all-monads.html “mother of all monads”] because of its universal properties.
   
 
{{Exercises|
 
{{Exercises|
# Implement a <hask>Monad</hask> instance for the list constructor, <hask>[]</hask>. Follow the types!
+
# Implement a <code>Monad</code> instance for the list constructor, <code>[]</code>. Follow the types!
# Implement a <hask>Monad</hask> instance for <hask>((->) e)</hask>.
+
# Implement a <code>Monad</code> instance for <code>((->) e)</code>.
 
}}
 
}}
   
 
==Intuition==
 
==Intuition==
   
Let’s look more closely at the type of <hask>(>>=)</hask>. The basic intuition is that it combines two computations into one larger computation. The first argument, <hask>m a</hask>, is the first computation. However, it would be boring if the second argument were just an <hask>m b</hask>; then there would be no way for the computations to interact with one another. So, the second argument to <hask>(>>=)</hask> has type <hask>a -> m b</hask>: a function of this type, given a ''result'' of the first computation, can produce a second computation to be run. In other words, <hask>x >>= k</hask> is a computation which runs <hask>x</hask>, and then uses the result(s) of <hask>x</hask> to ''decide'' what computation to run second, using the output of the second computation as the result of the entire computation.
+
Let’s look more closely at the type of <code>(>>=)</code>. The basic intuition is that it combines two computations into one larger computation. The first argument, <code>m a</code>, is the first computation. However, it would be boring if the second argument were just an <code>m b</code>; then there would be no way for the computations to interact with one another. So, the second argument to <code>(>>=)</code> has type <code>a -> m b</code>: a function of this type, given a ''result'' of the first computation, can produce a second computation to be run. In other words, <code>x >>= k</code> is a computation which runs <code>x</code>, and then uses the result(s) of <code>x</code> to ''decide'' what computation to run second, using the output of the second computation as the result of the entire computation.
   
Intuitively, it is this ability to use the output from previous computations to decide what computations to run next that makes <hask>Monad</hask> more powerful than <hask>Applicative</hask>. The structure of an <hask>Applicative</hask> computation is fixed, whereas the structure of a <hask>Monad</hask> computation can change based on intermediate results.
+
Intuitively, it is this ability to use the output from previous computations to decide what computations to run next that makes <code>Monad</code> more powerful than <code>Applicative</code>. The structure of an <code>Applicative</code> computation is fixed, whereas the structure of a <code>Monad</code> computation can change based on intermediate results.
   
To see the increased power of <hask>Monad</hask> from a different point of view, let’s see what happens if we try to implement <hask>(>>=)</hask> in terms of <hask>fmap</hask>, <hask>pure</hask>, and <hask>(<*>)</hask>. We are given a value <hask>x</hask> of type <hask>m a</hask>, and a function <hask>k</hask> of type <hask>a -> m b</hask>, so the only thing we can do is apply <hask>k</hask> to <hask>x</hask>. We can’t apply it directly, of course; we have to use <hask>fmap</hask> to lift it over the <hask>m</hask>. But what is the type of <hask>fmap k</hask>? Well, it’s <hask>m a -> m (m b)</hask>. So after we apply it to <hask>x</hask>, we are left with something of type <hask>m (m b)</hask>—but now we are stuck; what we really want is an <hask>m b</hask>, but there’s no way to get there from here. We can ''add'' <hask>m</hask>’s using <hask>pure</hask>, but we have no way to ''collapse'' multiple <hask>m</hask>’s into one.
+
To see the increased power of <code>Monad</code> from a different point of view, let’s see what happens if we try to implement <code>(>>=)</code> in terms of <code>fmap</code>, <code>pure</code>, and <code>(<*>)</code>. We are given a value <code>x</code> of type <code>m a</code>, and a function <code>k</code> of type <code>a -> m b</code>, so the only thing we can do is apply <code>k</code> to <code>x</code>. We can’t apply it directly, of course; we have to use <code>fmap</code> to lift it over the <code>m</code>. But what is the type of <code>fmap k</code>? Well, it’s <code>m a -> m (m b)</code>. So after we apply it to <code>x</code>, we are left with something of type <code>m (m b)</code>—but now we are stuck; what we really want is an <code>m b</code>, but there’s no way to get there from here. We can ''add'' <code>m</code>’s using <code>pure</code>, but we have no way to ''collapse'' multiple <code>m</code>’s into one.
   
This ability to collapse multiple <hask>m</hask>’s is exactly the ability provided by the function <hask>join :: m (m a) -> m a</hask>, and it should come as no surprise that an alternative definition of <hask>Monad</hask> can be given in terms of <hask>join</hask>:
+
This ability to collapse multiple <code>m</code>’s is exactly the ability provided by the function <code>join :: m (m a) -> m a</code>, and it should come as no surprise that an alternative definition of <code>Monad</code> can be given in terms of <code>join</code>:
   
 
<haskell>
 
<haskell>
Line 413: Line 413:
 
</haskell>
 
</haskell>
   
In fact, monads in category theory are defined in terms of <hask>return</hask>, <hask>fmap</hask>, and <hask>join</hask> (often called <math>\eta</math>, <math>T</math>, and <math>\mu</math> in the mathematical literature). Haskell uses the equivalent formulation in terms of <hask>(>>=)</hask> instead of <hask>join</hask> since it is more convenient to use; however, sometimes it can be easier to think about <hask>Monad</hask> instances in terms of <hask>join</hask>, since it is a more “atomic” operation. (For example, <hask>join</hask> for the list monad is just <hask>concat</hask>.)
+
In fact, monads in category theory are defined in terms of <code>return</code>, <code>fmap</code>, and <code>join</code> (often called <math>\eta</math>, <math>T</math>, and <math>\mu</math> in the mathematical literature). Haskell uses the equivalent formulation in terms of <code>(>>=)</code> instead of <code>join</code> since it is more convenient to use; however, sometimes it can be easier to think about <code>Monad</code> instances in terms of <code>join</code>, since it is a more “atomic” operation. (For example, <code>join</code> for the list monad is just <code>concat</code>.)
   
 
{{Exercises|
 
{{Exercises|
# Implement <hask>(>>{{=}})</hask> in terms of <hask>fmap</hask> (or <hask>liftM</hask>) and <hask>join</hask>.
+
# Implement <code>(>>{{=}})</code> in terms of <code>fmap</code> (or <code>liftM</code>) and <code>join</code>.
# Now implement <hask>join</hask> and <hask>fmap</hask> (<hask>liftM</hask>) in terms of <hask>(>>{{=}})</hask> and <hask>return</hask>.
+
# Now implement <code>join</code> and <code>fmap</code> (<code>liftM</code>) in terms of <code>(>>{{=}})</code> and <code>return</code>.
 
}}
 
}}
   
 
==Utility functions==
 
==Utility functions==
   
The [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html <hask>Control.Monad</hask>] module provides a large number of convenient utility functions, all of which can be implemented in terms of the basic <hask>Monad</hask> operations (<hask>return</hask> and <hask>(>>=)</hask> in particular). We have already seen one of them, namely, <hask>join</hask>. We also mention some other noteworthy ones here; implementing these utility functions oneself is a good exercise. For a more detailed guide to these functions, with commentary and example code, see Henk-Jan van Tuyl’s [http://members.chello.nl/hjgtuyl/tourdemonad.html tour].
+
The [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html <code>Control.Monad</code>] module provides a large number of convenient utility functions, all of which can be implemented in terms of the basic <code>Monad</code> operations (<code>return</code> and <code>(>>=)</code> in particular). We have already seen one of them, namely, <code>join</code>. We also mention some other noteworthy ones here; implementing these utility functions oneself is a good exercise. For a more detailed guide to these functions, with commentary and example code, see Henk-Jan van Tuyl’s [http://members.chello.nl/hjgtuyl/tourdemonad.html tour].
   
{{note|Still, it is unclear how this "bug" should be fixed. Making <hask>Monad</hask> require a <hask>Functor</hask> instance has some drawbacks, as mentioned in this [http://www.haskell.org/pipermail/haskell-prime/2011-January/003312.html 2011 mailing-list discussion]. —Geheimdienst}}
+
{{note|Still, it is unclear how this "bug" should be fixed. Making <code>Monad</code> require a <code>Functor</code> instance has some drawbacks, as mentioned in this [http://www.haskell.org/pipermail/haskell-prime/2011-January/003312.html 2011 mailing-list discussion]. —Geheimdienst}}
   
* <hask>liftM :: Monad m => (a -> b) -> m a -> m b</hask>. This should be familiar; of course, it is just <hask>fmap</hask>. The fact that we have both <hask>fmap</hask> and <hask>liftM</hask> is an unfortunate consequence of the fact that the <hask>Monad</hask> type class does not require a <hask>Functor</hask> instance, even though mathematically speaking, every monad is a functor. However, <hask>fmap</hask> and <hask>liftM</hask> are essentially interchangeable, since it is a bug (in a social rather than technical sense) for any type to be an instance of <hask>Monad</hask> without also being an instance of <hask>Functor</hask> {{noteref}}.
+
* <code>liftM :: Monad m => (a -> b) -> m a -> m b</code>. This should be familiar; of course, it is just <code>fmap</code>. The fact that we have both <code>fmap</code> and <code>liftM</code> is an unfortunate consequence of the fact that the <code>Monad</code> type class does not require a <code>Functor</code> instance, even though mathematically speaking, every monad is a functor. However, <code>fmap</code> and <code>liftM</code> are essentially interchangeable, since it is a bug (in a social rather than technical sense) for any type to be an instance of <code>Monad</code> without also being an instance of <code>Functor</code> {{noteref}}.
   
* <hask>ap :: Monad m => m (a -> b) -> m a -> m b</hask> should also be familiar: it is equivalent to <hask>(<*>)</hask>, justifying the claim that the <hask>Monad</hask> interface is strictly more powerful than <hask>Applicative</hask>. We can make any <hask>Monad</hask> into an instance of <hask>Applicative</hask> by setting <hask>pure = return</hask> and <hask>(<*>) = ap</hask>.
+
* <code>ap :: Monad m => m (a -> b) -> m a -> m b</code> should also be familiar: it is equivalent to <code>(<*>)</code>, justifying the claim that the <code>Monad</code> interface is strictly more powerful than <code>Applicative</code>. We can make any <code>Monad</code> into an instance of <code>Applicative</code> by setting <code>pure = return</code> and <code>(<*>) = ap</code>.
   
* <hask>sequence :: Monad m => [m a] -> m [a]</hask> takes a list of computations and combines them into one computation which collects a list of their results. It is again something of a historical accident that <hask>sequence</hask> has a <hask>Monad</hask> constraint, since it can actually be implemented only in terms of <hask>Applicative</hask>. There is an additional generalization of <hask>sequence</hask> to structures other than lists, which will be discussed in the [[#Traversable|section on <hask>Traversable</hask>]].
+
* <code>sequence :: Monad m => [m a] -> m [a]</code> takes a list of computations and combines them into one computation which collects a list of their results. It is again something of a historical accident that <code>sequence</code> has a <code>Monad</code> constraint, since it can actually be implemented only in terms of <code>Applicative</code>. There is an additional generalization of <code>sequence</code> to structures other than lists, which will be discussed in the [[#Traversable|section on <code>Traversable</code>]].
   
* <hask>replicateM :: Monad m => Int -> m a -> m [a]</hask> is simply a combination of [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:replicate <hask>replicate</hask>] and <hask>sequence</hask>.
+
* <code>replicateM :: Monad m => Int -> m a -> m [a]</code> is simply a combination of [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:replicate <code>replicate</code>] and <code>sequence</code>.
   
* <hask>when :: Monad m => Bool -> m () -> m ()</hask> conditionally executes a computation, evaluating to its second argument if the test is <hask>True</hask>, and to <hask>return ()</hask> if the test is <hask>False</hask>. A collection of other sorts of monadic conditionals can be found in the [http://hackage.haskell.org/package/IfElse <hask>IfElse</hask> package].
+
* <code>when :: Monad m => Bool -> m () -> m ()</code> conditionally executes a computation, evaluating to its second argument if the test is <code>True</code>, and to <code>return ()</code> if the test is <code>False</code>. A collection of other sorts of monadic conditionals can be found in the [http://hackage.haskell.org/package/IfElse <code>IfElse</code> package].
   
* <hask>mapM :: Monad m => (a -> m b) -> [a] -> m [b]</hask> maps its first argument over the second, and <hask>sequence</hask>s the results. The <hask>forM</hask> function is just <hask>mapM</hask> with its arguments reversed; it is called <hask>forM</hask> since it models generalized <hask>for</hask> loops: the list <hask>[a]</hask> provides the loop indices, and the function <hask>a -> m b</hask> specifies the “body” of the loop for each index.
+
* <code>mapM :: Monad m => (a -> m b) -> [a] -> m [b]</code> maps its first argument over the second, and <code>sequence</code>s the results. The <code>forM</code> function is just <code>mapM</code> with its arguments reversed; it is called <code>forM</code> since it models generalized <code>for</code> loops: the list <code>[a]</code> provides the loop indices, and the function <code>a -> m b</code> specifies the “body” of the loop for each index.
   
* <hask>(=<<) :: Monad m => (a -> m b) -> m a -> m b</hask> is just <hask>(>>=)</hask> with its arguments reversed; sometimes this direction is more convenient since it corresponds more closely to function application.
+
* <code>(=<<) :: Monad m => (a -> m b) -> m a -> m b</code> is just <code>(>>=)</code> with its arguments reversed; sometimes this direction is more convenient since it corresponds more closely to function application.
   
* <hask>(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c</hask> is sort of like function composition, but with an extra <hask>m</hask> on the result type of each function, and the arguments swapped. We’ll have more to say about this operation later. There is also a flipped variant, <hask>(<=<)</hask>.
+
* <code>(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c</code> is sort of like function composition, but with an extra <code>m</code> on the result type of each function, and the arguments swapped. We’ll have more to say about this operation later. There is also a flipped variant, <code>(<=<)</code>.
   
* The <hask>guard</hask> function is for use with instances of <hask>MonadPlus</hask>, which is discussed at the end of the [[#Monoid|<hask>Monoid</hask> section]].
+
* The <code>guard</code> function is for use with instances of <code>MonadPlus</code>, which is discussed at the end of the [[#Monoid|<code>Monoid</code> section]].
   
Many of these functions also have “underscored” variants, such as <hask>sequence_</hask> and <hask>mapM_</hask>; these variants throw away the results of the computations passed to them as arguments, using them only for their side effects.
+
Many of these functions also have “underscored” variants, such as <code>sequence_</code> and <code>mapM_</code>; these variants throw away the results of the computations passed to them as arguments, using them only for their side effects.
   
Other monadic functions which are occasionally useful include <hask>filterM</hask>, <hask>zipWithM</hask>, <hask>foldM</hask>, and <hask>forever</hask>.
+
Other monadic functions which are occasionally useful include <code>filterM</code>, <code>zipWithM</code>, <code>foldM</code>, and <code>forever</code>.
   
 
==Laws==
 
==Laws==
   
There are several laws that instances of <hask>Monad</hask> should satisfy (see also the [[Monad laws]] wiki page). The standard presentation is:
+
There are several laws that instances of <code>Monad</code> should satisfy (see also the [[Monad laws]] wiki page). The standard presentation is:
   
 
<haskell>
 
<haskell>
Line 460: Line 460:
 
</haskell>
 
</haskell>
   
The first and second laws express the fact that <hask>return</hask> behaves nicely: if we inject a value <hask>a</hask> into a monadic context with <hask>return</hask>, and then bind to <hask>k</hask>, it is the same as just applying <hask>k</hask> to <hask>a</hask> in the first place; if we bind a computation <hask>m</hask> to <hask>return</hask>, nothing changes. The third law essentially says that <hask>(>>=)</hask> is associative, sort of. The last law ensures that <hask>fmap</hask> and <hask>liftM</hask> are the same for types which are instances of both <hask>Functor</hask> and <hask>Monad</hask>—which, as already noted, should be every instance of <hask>Monad</hask>.
+
The first and second laws express the fact that <code>return</code> behaves nicely: if we inject a value <code>a</code> into a monadic context with <code>return</code>, and then bind to <code>k</code>, it is the same as just applying <code>k</code> to <code>a</code> in the first place; if we bind a computation <code>m</code> to <code>return</code>, nothing changes. The third law essentially says that <code>(>>=)</code> is associative, sort of. The last law ensures that <code>fmap</code> and <code>liftM</code> are the same for types which are instances of both <code>Functor</code> and <code>Monad</code>—which, as already noted, should be every instance of <code>Monad</code>.
   
 
{{note|I like to pronounce this operator “fish”, but that’s probably not the canonical pronunciation ...}}
 
{{note|I like to pronounce this operator “fish”, but that’s probably not the canonical pronunciation ...}}
   
However, the presentation of the above laws, especially the third, is marred by the asymmetry of <hask>(>>=)</hask>. It’s hard to look at the laws and see what they’re really saying. I prefer a much more elegant version of the laws, which is formulated in terms of <hask>(>=>)</hask> {{noteref}}. Recall that <hask>(>=>)</hask> “composes” two functions of type <hask>a -> m b</hask> and <hask>b -> m c</hask>. You can think of something of type <hask>a -> m b</hask> (roughly) as a function from <hask>a</hask> to <hask>b</hask> which may also have some sort of effect in the context corresponding to <hask>m</hask>. (Note that <hask>return</hask> is such a function.) <hask>(>=>)</hask> lets us compose these “effectful functions”, and we would like to know what properties <hask>(>=>)</hask> has. The monad laws reformulated in terms of <hask>(>=>)</hask> are:
+
However, the presentation of the above laws, especially the third, is marred by the asymmetry of <code>(>>=)</code>. It’s hard to look at the laws and see what they’re really saying. I prefer a much more elegant version of the laws, which is formulated in terms of <code>(>=>)</code> {{noteref}}. Recall that <code>(>=>)</code> “composes” two functions of type <code>a -> m b</code> and <code>b -> m c</code>. You can think of something of type <code>a -> m b</code> (roughly) as a function from <code>a</code> to <code>b</code> which may also have some sort of effect in the context corresponding to <code>m</code>. (Note that <code>return</code> is such a function.) <code>(>=>)</code> lets us compose these “effectful functions”, and we would like to know what properties <code>(>=>)</code> has. The monad laws reformulated in terms of <code>(>=>)</code> are:
   
 
<haskell>
 
<haskell>
Line 472: Line 472:
 
</haskell>
 
</haskell>
   
{{note|As fans of category theory will note, these laws say precisely that functions of type <hask>a -> m b</hask> are the arrows of a category with <hask>(>{{=}}>)</hask> as composition! Indeed, this is known as the ''Kleisli category'' of the monad <hask>m</hask>. It will come up again when we discuss <hask>Arrow</hask>s.}}
+
{{note|As fans of category theory will note, these laws say precisely that functions of type <code>a -> m b</code> are the arrows of a category with <code>(>{{=}}>)</code> as composition! Indeed, this is known as the ''Kleisli category'' of the monad <code>m</code>. It will come up again when we discuss <code>Arrow</code>s.}}
   
Ah, much better! The laws simply state that <hask>return</hask> is the identity of <hask>(>=>)</hask>, and that <hask>(>=>)</hask> is associative {{noteref}}. Working out the equivalence between these two formulations, given the definition <hask>g >=> h = \x -> g x >>= h</hask>, is left as an exercise.
+
Ah, much better! The laws simply state that <code>return</code> is the identity of <code>(>=>)</code>, and that <code>(>=>)</code> is associative {{noteref}}. Working out the equivalence between these two formulations, given the definition <code>g >=> h = \x -> g x >>= h</code>, is left as an exercise.
   
There is also a formulation of the monad laws in terms of <hask>fmap</hask>, <hask>return</hask>, and <hask>join</hask>; for a discussion of this formulation, see the Haskell [http://en.wikibooks.org/wiki/Haskell/Category_theory wikibook page on category theory].
+
There is also a formulation of the monad laws in terms of <code>fmap</code>, <code>return</code>, and <code>join</code>; for a discussion of this formulation, see the Haskell [http://en.wikibooks.org/wiki/Haskell/Category_theory wikibook page on category theory].
   
==<hask>do</hask> notation==
+
==<code>do</code> notation==
   
Haskell’s special <hask>do</hask> notation supports an “imperative style” of programming by providing syntactic sugar for chains of monadic expressions. The genesis of the notation lies in realizing that something like <hask>a >>= \x -> b >> c >>= \y -> d </hask> can be more readably written by putting successive computations on separate lines:
+
Haskell’s special <code>do</code> notation supports an “imperative style” of programming by providing syntactic sugar for chains of monadic expressions. The genesis of the notation lies in realizing that something like <code>a >>= \x -> b >> c >>= \y -> d </code> can be more readably written by putting successive computations on separate lines:
   
 
<haskell>
 
<haskell>
Line 489: Line 489:
 
</haskell>
 
</haskell>
   
This emphasizes that the overall computation consists of four computations <hask>a</hask>, <hask>b</hask>, <hask>c</hask>, and <hask>d</hask>, and that <hask>x</hask> is bound to the result of <hask>a</hask>, and <hask>y</hask> is bound to the result of <hask>c</hask> (<hask>b</hask>, <hask>c</hask>, and <hask>d</hask> are allowed to refer to <hask>x</hask>, and <hask>d</hask> is allowed to refer to <hask>y</hask> as well). From here it is not hard to imagine a nicer notation:
+
This emphasizes that the overall computation consists of four computations <code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code>, and that <code>x</code> is bound to the result of <code>a</code>, and <code>y</code> is bound to the result of <code>c</code> (<code>b</code>, <code>c</code>, and <code>d</code> are allowed to refer to <code>x</code>, and <code>d</code> is allowed to refer to <code>y</code> as well). From here it is not hard to imagine a nicer notation:
   
 
<haskell>
 
<haskell>
Line 499: Line 499:
 
</haskell>
 
</haskell>
   
(The curly braces and semicolons may optionally be omitted; the Haskell parser uses layout to determine where they should be inserted.) This discussion should make clear that <hask>do</hask> notation is just syntactic sugar. In fact, <hask>do</hask> blocks are recursively translated into monad operations (almost) like this:
+
(The curly braces and semicolons may optionally be omitted; the Haskell parser uses layout to determine where they should be inserted.) This discussion should make clear that <code>do</code> notation is just syntactic sugar. In fact, <code>do</code> blocks are recursively translated into monad operations (almost) like this:
   
 
<pre>
 
<pre>
Line 508: Line 508:
 
</pre>
 
</pre>
   
This is not quite the whole story, since <hask>v</hask> might be a pattern instead of a variable. For example, one can write
+
This is not quite the whole story, since <code>v</code> might be a pattern instead of a variable. For example, one can write
   
 
<haskell>
 
<haskell>
Line 515: Line 515:
 
</haskell>
 
</haskell>
   
but what happens if <hask>foo</hask> produces an empty list? Well, remember that ugly <hask>fail</hask> function in the <hask>Monad</hask> type class declaration? That’s what happens. See [http://haskell.org/onlinereport/exps.html#sect3.14 section 3.14 of the Haskell Report] for the full details. See also the discussion of <hask>MonadPlus</hask> and <hask>MonadZero</hask> in the [[#Other monoidal classes: Alternative, MonadPlus, ArrowPlus|section on other monoidal classes]].
+
but what happens if <code>foo</code> produces an empty list? Well, remember that ugly <code>fail</code> function in the <code>Monad</code> type class declaration? That’s what happens. See [http://haskell.org/onlinereport/exps.html#sect3.14 section 3.14 of the Haskell Report] for the full details. See also the discussion of <code>MonadPlus</code> and <code>MonadZero</code> in the [[#Other monoidal classes: Alternative, MonadPlus, ArrowPlus|section on other monoidal classes]].
   
A final note on intuition: <hask>do</hask> notation plays very strongly to the “computational context” point of view rather than the “container” point of view, since the binding notation <hask>x <- m</hask> is suggestive of “extracting” a single <hask>x</hask> from <hask>m</hask> and doing something with it. But <hask>m</hask> may represent some sort of a container, such as a list or a tree; the meaning of <hask>x <- m</hask> is entirely dependent on the implementation of <hask>(>>=)</hask>. For example, if <hask>m</hask> is a list, <hask>x <- m</hask> actually means that <hask>x</hask> will take on each value from the list in turn.
+
A final note on intuition: <code>do</code> notation plays very strongly to the “computational context” point of view rather than the “container” point of view, since the binding notation <code>x <- m</code> is suggestive of “extracting” a single <code>x</code> from <code>m</code> and doing something with it. But <code>m</code> may represent some sort of a container, such as a list or a tree; the meaning of <code>x <- m</code> is entirely dependent on the implementation of <code>(>>=)</code>. For example, if <code>m</code> is a list, <code>x <- m</code> actually means that <code>x</code> will take on each value from the list in turn.
   
 
==Monad transformers==
 
==Monad transformers==
   
One would often like to be able to combine two monads into one: for example, to have stateful, nondeterministic computations (<hask>State</hask> + <hask>[]</hask>), or computations which may fail and can consult a read-only environment (<hask>Maybe</hask> + <hask>Reader</hask>), and so on. Unfortunately, monads do not compose as nicely as applicative functors (yet another reason to use <hask>Applicative</hask> if you don’t need the full power that <hask>Monad</hask> provides), but some monads can be combined in certain ways.
+
One would often like to be able to combine two monads into one: for example, to have stateful, nondeterministic computations (<code>State</code> + <code>[]</code>), or computations which may fail and can consult a read-only environment (<code>Maybe</code> + <code>Reader</code>), and so on. Unfortunately, monads do not compose as nicely as applicative functors (yet another reason to use <code>Applicative</code> if you don’t need the full power that <code>Monad</code> provides), but some monads can be combined in certain ways.
   
The monad transformer library [http://hackage.haskell.org/package/mtl mtl] provides a number of ''monad transformers'', such as <hask>StateT</hask>, <hask>ReaderT</hask>, <hask>ErrorT</hask> ([http://hackage.haskell.org/packages/archive/mtl/1.1.0.2/doc/html/Control-Monad-Error.html haddock]), and (soon) <hask>MaybeT</hask>, which can be applied to other monads to produce a new monad with the effects of both. For example, <hask>StateT s Maybe</hask> is an instance of <hask>Monad</hask>; computations of type <hask>StateT s Maybe a</hask> may fail, and have access to a mutable state of type <hask>s</hask>. These transformers can be multiply stacked. One thing to keep in mind while using monad transformers is that the order of composition matters. For example, when a <hask>StateT s Maybe a</hask> computation fails, the state ceases being updated; on the other hand, the state of a <hask>MaybeT (State s) a</hask> computation may continue to be modified even after the computation has failed. (This may seem backwards, but it is correct. Monad transformers build composite monads “inside out”; for example, <hask>MaybeT (State s) a</hask> is isomorphic to <hask>s -> (Maybe a, s)</hask>. Lambdabot has an indispensable <hask>@unmtl</hask> command which you can use to “unpack” a monad transformer stack in this way.)
+
The monad transformer library [http://hackage.haskell.org/package/mtl mtl] provides a number of ''monad transformers'', such as <code>StateT</code>, <code>ReaderT</code>, <code>ErrorT</code> ([http://hackage.haskell.org/packages/archive/mtl/1.1.0.2/doc/html/Control-Monad-Error.html haddock]), and (soon) <code>MaybeT</code>, which can be applied to other monads to produce a new monad with the effects of both. For example, <code>StateT s Maybe</code> is an instance of <code>Monad</code>; computations of type <code>StateT s Maybe a</code> may fail, and have access to a mutable state of type <code>s</code>. These transformers can be multiply stacked. One thing to keep in mind while using monad transformers is that the order of composition matters. For example, when a <code>StateT s Maybe a</code> computation fails, the state ceases being updated; on the other hand, the state of a <code>MaybeT (State s) a</code> computation may continue to be modified even after the computation has failed. (This may seem backwards, but it is correct. Monad transformers build composite monads “inside out”; for example, <code>MaybeT (State s) a</code> is isomorphic to <code>s -> (Maybe a, s)</code>. Lambdabot has an indispensable <code>@unmtl</code> command which you can use to “unpack” a monad transformer stack in this way.)
   
All monad transformers should implement the <hask>MonadTrans</hask> type class, defined in <hask>Control.Monad.Trans</hask>:
+
All monad transformers should implement the <code>MonadTrans</code> type class, defined in <code>Control.Monad.Trans</code>:
   
 
<haskell>
 
<haskell>
Line 532: Line 532:
 
</haskell>
 
</haskell>
   
It allows arbitrary computations in the base monad <hask>m</hask> to be “lifted” into computations in the transformed monad <hask>t m</hask>. (Note that type application associates to the left, just like function application, so <hask>t m a = (t m) a</hask>. As an exercise, you may wish to work out <hask>t</hask>’s kind, which is rather more interesting than most of the kinds we’ve seen up to this point.) However, you should only have to think about <hask>MonadTrans</hask> when defining your own monad transformers, not when using predefined ones.
+
It allows arbitrary computations in the base monad <code>m</code> to be “lifted” into computations in the transformed monad <code>t m</code>. (Note that type application associates to the left, just like function application, so <code>t m a = (t m) a</code>. As an exercise, you may wish to work out <code>t</code>’s kind, which is rather more interesting than most of the kinds we’ve seen up to this point.) However, you should only have to think about <code>MonadTrans</code> when defining your own monad transformers, not when using predefined ones.
   
 
{{note|The only problem with this scheme is the quadratic number of instances required as the number of standard monad transformers grows—but as the current set of standard monad transformers seems adequate for most common use cases, this may not be that big of a deal.}}
 
{{note|The only problem with this scheme is the quadratic number of instances required as the number of standard monad transformers grows—but as the current set of standard monad transformers seems adequate for most common use cases, this may not be that big of a deal.}}
   
There are also type classes such as <hask>MonadState</hask>, which provides state-specific methods like <hask>get</hask> and <hask>put</hask>, allowing you to conveniently use these methods not only with <hask>State</hask>, but with any monad which is an instance of <hask>MonadState</hask>—including <hask>MaybeT (State s)</hask>, <hask>StateT s (ReaderT r IO)</hask>, and so on. Similar type classes exist for <hask>Reader</hask>, <hask>Writer</hask>, <hask>Cont</hask>, <hask>IO</hask>, and others {{noteref}}.
+
There are also type classes such as <code>MonadState</code>, which provides state-specific methods like <code>get</code> and <code>put</code>, allowing you to conveniently use these methods not only with <code>State</code>, but with any monad which is an instance of <code>MonadState</code>—including <code>MaybeT (State s)</code>, <code>StateT s (ReaderT r IO)</code>, and so on. Similar type classes exist for <code>Reader</code>, <code>Writer</code>, <code>Cont</code>, <code>IO</code>, and others {{noteref}}.
   
 
There are two excellent references on monad transformers. Martin Grabmüller’s [http://user.cs.tu-berlin.de/~magr/pub/Transformers.en.html Monad Transformers Step by Step] is a thorough description, with running examples, of how to use monad transformers to elegantly build up computations with various effects. [http://cale.yi.org/index.php/How_To_Use_Monad_Transformers Cale Gibbard’s article] on how to use monad transformers is more practical, describing how to structure code using monad transformers to make writing it as painless as possible. Another good starting place for learning about monad transformers is a [http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html blog post by Dan Piponi].
 
There are two excellent references on monad transformers. Martin Grabmüller’s [http://user.cs.tu-berlin.de/~magr/pub/Transformers.en.html Monad Transformers Step by Step] is a thorough description, with running examples, of how to use monad transformers to elegantly build up computations with various effects. [http://cale.yi.org/index.php/How_To_Use_Monad_Transformers Cale Gibbard’s article] on how to use monad transformers is more practical, describing how to structure code using monad transformers to make writing it as painless as possible. Another good starting place for learning about monad transformers is a [http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html blog post by Dan Piponi].
Line 542: Line 542:
 
==MonadFix==
 
==MonadFix==
   
The <hask>MonadFix</hask> class describes monads which support the special fixpoint operation <hask>mfix :: (a -> m a) -> m a</hask>, which allows the output of monadic computations to be defined via recursion. This is supported in GHC and Hugs by a special “recursive do” notation, <hask>mdo</hask>. For more information, see Levent Erkök’s thesis, [http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.15.1543&rep=rep1&type=pdf Value Recursion in Monadic Computations].
+
The <code>MonadFix</code> class describes monads which support the special fixpoint operation <code>mfix :: (a -> m a) -> m a</code>, which allows the output of monadic computations to be defined via recursion. This is supported in GHC and Hugs by a special “recursive do” notation, <code>mdo</code>. For more information, see Levent Erkök’s thesis, [http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.15.1543&rep=rep1&type=pdf Value Recursion in Monadic Computations].
   
 
==Further reading==
 
==Further reading==
Line 548: Line 548:
 
Philip Wadler was the first to propose using monads to structure functional programs. [http://homepages.inf.ed.ac.uk/wadler/topics/monads.html His paper] is still a readable introduction to the subject.
 
Philip Wadler was the first to propose using monads to structure functional programs. [http://homepages.inf.ed.ac.uk/wadler/topics/monads.html His paper] is still a readable introduction to the subject.
   
Much of the monad transformer library [http://hackage.haskell.org/package/mtl mtl], including the <hask>Reader</hask>, <hask>Writer</hask>, <hask>State</hask>, and other monads, as well as the monad transformer framework itself, was inspired by Mark Jones’s classic paper [http://web.cecs.pdx.edu/~mpj/pubs/springschool.html Functional Programming with Overloading and Higher-Order Polymorphism]. It’s still very much worth a read—and highly readable—after almost fifteen years.
+
Much of the monad transformer library [http://hackage.haskell.org/package/mtl mtl], including the <code>Reader</code>, <code>Writer</code>, <code>State</code>, and other monads, as well as the monad transformer framework itself, was inspired by Mark Jones’s classic paper [http://web.cecs.pdx.edu/~mpj/pubs/springschool.html Functional Programming with Overloading and Higher-Order Polymorphism]. It’s still very much worth a read—and highly readable—after almost fifteen years.
   
 
{{note|
 
{{note|
Line 564: Line 564:
 
There are, of course, numerous monad tutorials of varying quality {{noteref}}.
 
There are, of course, numerous monad tutorials of varying quality {{noteref}}.
   
A few of the best include Cale Gibbard’s [http://haskell.org/haskellwiki/Monads_as_Containers Monads as containers] and [http://haskell.org/haskellwiki/Monads_as_computation Monads as computation]; Jeff Newbern’s [http://www.haskell.org/all_about_monads/html/ All About Monads], a comprehensive guide with lots of examples; and Dan Piponi’s [http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html You Could Have Invented Monads!], which features great exercises. If you just want to know how to use <hask>IO</hask>, you could consult the [[Introduction to IO]]. Even this is just a sampling; the [[monad tutorials timeline]] is a more complete list. (All these monad tutorials have prompted parodies like [http://koweycode.blogspot.com/2007/01/think-of-monad.html think of a monad ...] as well as other kinds of backlash like [http://ahamsandwich.wordpress.com/2007/07/26/monads-and-why-monad-tutorials-are-all-awful/ Monads! (and Why Monad Tutorials Are All Awful)] or [http://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/ Abstraction, intuition, and the “monad tutorial fallacy”].)
+
A few of the best include Cale Gibbard’s [http://haskell.org/haskellwiki/Monads_as_Containers Monads as containers] and [http://haskell.org/haskellwiki/Monads_as_computation Monads as computation]; Jeff Newbern’s [http://www.haskell.org/all_about_monads/html/ All About Monads], a comprehensive guide with lots of examples; and Dan Piponi’s [http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html You Could Have Invented Monads!], which features great exercises. If you just want to know how to use <code>IO</code>, you could consult the [[Introduction to IO]]. Even this is just a sampling; the [[monad tutorials timeline]] is a more complete list. (All these monad tutorials have prompted parodies like [http://koweycode.blogspot.com/2007/01/think-of-monad.html think of a monad ...] as well as other kinds of backlash like [http://ahamsandwich.wordpress.com/2007/07/26/monads-and-why-monad-tutorials-are-all-awful/ Monads! (and Why Monad Tutorials Are All Awful)] or [http://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/ Abstraction, intuition, and the “monad tutorial fallacy”].)
   
Other good monad references which are not necessarily tutorials include [http://members.chello.nl/hjgtuyl/tourdemonad.html Henk-Jan van Tuyl’s tour] of the functions in <hask>Control.Monad</hask>, Dan Piponi’s [http://blog.sigfpe.com/2006/10/monads-field-guide.html field guide], and Tim Newsham’s [http://www.thenewsh.com/~newsham/haskell/monad.html What’s a Monad?]. There are also many blog posts which have been written on various aspects of monads; a collection of links can be found under [[Blog articles/Monads]].
+
Other good monad references which are not necessarily tutorials include [http://members.chello.nl/hjgtuyl/tourdemonad.html Henk-Jan van Tuyl’s tour] of the functions in <code>Control.Monad</code>, Dan Piponi’s [http://blog.sigfpe.com/2006/10/monads-field-guide.html field guide], and Tim Newsham’s [http://www.thenewsh.com/~newsham/haskell/monad.html What’s a Monad?]. There are also many blog posts which have been written on various aspects of monads; a collection of links can be found under [[Blog articles/Monads]].
   
One of the quirks of the <hask>Monad</hask> class and the Haskell type system is that it is not possible to straightforwardly declare <hask>Monad</hask> instances for types which require a class constraint on their data, even if they are monads from a mathematical point of view. For example, <hask>Data.Set</hask> requires an <hask>Ord</hask> constraint on its data, so it cannot be easily made an instance of <hask>Monad</hask>. A solution to this problem was [http://www.randomhacks.net/articles/2007/03/15/data-set-monad-haskell-macros first described by Eric Kidd], and later made into a [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/rmonad library named rmonad] by Ganesh Sittampalam and Peter Gavin.
+
One of the quirks of the <code>Monad</code> class and the Haskell type system is that it is not possible to straightforwardly declare <code>Monad</code> instances for types which require a class constraint on their data, even if they are monads from a mathematical point of view. For example, <code>Data.Set</code> requires an <code>Ord</code> constraint on its data, so it cannot be easily made an instance of <code>Monad</code>. A solution to this problem was [http://www.randomhacks.net/articles/2007/03/15/data-set-monad-haskell-macros first described by Eric Kidd], and later made into a [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/rmonad library named rmonad] by Ganesh Sittampalam and Peter Gavin.
   
There are many good reasons for eschewing <hask>do</hask> notation; some have gone so far as to [[Do_notation_considered_harmful|consider it harmful]].
+
There are many good reasons for eschewing <code>do</code> notation; some have gone so far as to [[Do_notation_considered_harmful|consider it harmful]].
   
 
Monads can be generalized in various ways; for an exposition of one possibility, see Robert Atkey’s paper on [http://homepages.inf.ed.ac.uk/ratkey/paramnotions-jfp.pdf parameterized monads], or Dan Piponi’s [http://blog.sigfpe.com/2009/02/beyond-monads.html Beyond Monads].
 
Monads can be generalized in various ways; for an exposition of one possibility, see Robert Atkey’s paper on [http://homepages.inf.ed.ac.uk/ratkey/paramnotions-jfp.pdf parameterized monads], or Dan Piponi’s [http://blog.sigfpe.com/2009/02/beyond-monads.html Beyond Monads].
   
For the categorically inclined, monads can be viewed as monoids ([http://blog.sigfpe.com/2008/11/from-monoids-to-monads.html From Monoids to Monads]) and also as closure operators [http://blog.plover.com/math/monad-closure.html Triples and Closure]. Derek Elkins’s article in [http://www.haskell.org/wikiupload/8/85/TMR-Issue13.pdf issue 13 of the Monad.Reader] contains an exposition of the category-theoretic underpinnings of some of the standard <hask>Monad</hask> instances, such as <hask>State</hask> and <hask>Cont</hask>. There is also an alternative way to compose monads, using coproducts, as described by [http://doi.acm.org/10.1145/583852.581492 Lüth and Ghani], although this method has not (yet?) seen widespread use.
+
For the categorically inclined, monads can be viewed as monoids ([http://blog.sigfpe.com/2008/11/from-monoids-to-monads.html From Monoids to Monads]) and also as closure operators [http://blog.plover.com/math/monad-closure.html Triples and Closure]. Derek Elkins’s article in [http://www.haskell.org/wikiupload/8/85/TMR-Issue13.pdf issue 13 of the Monad.Reader] contains an exposition of the category-theoretic underpinnings of some of the standard <code>Monad</code> instances, such as <code>State</code> and <code>Cont</code>. There is also an alternative way to compose monads, using coproducts, as described by [http://doi.acm.org/10.1145/583852.581492 Lüth and Ghani], although this method has not (yet?) seen widespread use.
   
 
Links to many more research papers related to monads can be found under [[Research papers/Monads and arrows]].
 
Links to many more research papers related to monads can be found under [[Research papers/Monads and arrows]].
Line 598: Line 598:
 
==Definition==
 
==Definition==
   
The definition of the <hask>Monoid</hask> type class (defined in
+
The definition of the <code>Monoid</code> type class (defined in
<hask>Data.Monoid</hask>; [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html haddock]) is:
+
<code>Data.Monoid</code>; [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html haddock]) is:
   
 
<haskell>
 
<haskell>
Line 610: Line 610:
 
</haskell>
 
</haskell>
   
The <hask>mempty</hask> value specifies the identity element of the monoid, and <hask>mappend</hask>
+
The <code>mempty</code> value specifies the identity element of the monoid, and <code>mappend</code>
is the binary operation. The default definition for <hask>mconcat</hask>
+
is the binary operation. The default definition for <code>mconcat</code>
“reduces” a list of elements by combining them all with <hask>mappend</hask>,
+
“reduces” a list of elements by combining them all with <code>mappend</code>,
using a right fold. It is only in the <hask>Monoid</hask> class so that specific
+
using a right fold. It is only in the <code>Monoid</code> class so that specific
 
instances have the option of providing an alternative, more efficient
 
instances have the option of providing an alternative, more efficient
implementation; usually, you can safely ignore <hask>mconcat</hask> when creating
+
implementation; usually, you can safely ignore <code>mconcat</code> when creating
a <hask>Monoid</hask> instance, since its default definition will work just fine.
+
a <code>Monoid</code> instance, since its default definition will work just fine.
   
The <hask>Monoid</hask> methods are rather unfortunately named; they are inspired
+
The <code>Monoid</code> methods are rather unfortunately named; they are inspired
by the list instance of <hask>Monoid</hask>, where indeed <hask>mempty = []</hask> and <hask>mappend = (++)</hask>, but this is misleading since many
+
by the list instance of <code>Monoid</code>, where indeed <code>mempty = []</code> and <code>mappend = (++)</code>, but this is misleading since many
 
monoids have little to do with appending (see these [http://thread.gmane.org/gmane.comp.lang.haskell.cafe/50590 Comments from OCaml Hacker Brian Hurt] on the haskell-cafe mailing list).
 
monoids have little to do with appending (see these [http://thread.gmane.org/gmane.comp.lang.haskell.cafe/50590 Comments from OCaml Hacker Brian Hurt] on the haskell-cafe mailing list).
   
 
==Laws==
 
==Laws==
   
Of course, every <hask>Monoid</hask> instance should actually be a monoid in the
+
Of course, every <code>Monoid</code> instance should actually be a monoid in the
 
mathematical sense, which implies these laws:
 
mathematical sense, which implies these laws:
   
Line 635: Line 635:
 
==Instances==
 
==Instances==
   
There are quite a few interesting <hask>Monoid</hask> instances defined in <hask>Data.Monoid</hask>.
+
There are quite a few interesting <code>Monoid</code> instances defined in <code>Data.Monoid</code>.
   
 
<ul>
 
<ul>
<li><hask>[a]</hask> is a <hask>Monoid</hask>, with <hask>mempty = []</hask> and <hask>mappend = (++)</hask>. It is not hard to check that <hask>(x ++ y) ++ z = x ++ (y ++ z)</hask> for any lists <hask>x</hask>, <hask>y</hask>, and <hask>z</hask>, and that the empty list is the identity: <hask>[] ++ x = x ++ [] = x</hask>.</li>
+
<li><code>[a]</code> is a <code>Monoid</code>, with <code>mempty = []</code> and <code>mappend = (++)</code>. It is not hard to check that <code>(x ++ y) ++ z = x ++ (y ++ z)</code> for any lists <code>x</code>, <code>y</code>, and <code>z</code>, and that the empty list is the identity: <code>[] ++ x = x ++ [] = x</code>.</li>
   
<li>As noted previously, we can make a monoid out of any numeric type under either addition or multiplication. However, since we can’t have two instances for the same type, <hask>Data.Monoid</hask> provides two <hask>newtype</hask> wrappers, <hask>Sum</hask> and <hask>Product</hask>, with appropriate <hask>Monoid</hask> instances.
+
<li>As noted previously, we can make a monoid out of any numeric type under either addition or multiplication. However, since we can’t have two instances for the same type, <code>Data.Monoid</code> provides two <code>newtype</code> wrappers, <code>Sum</code> and <code>Product</code>, with appropriate <code>Monoid</code> instances.
   
 
<haskell>
 
<haskell>
Line 650: Line 650:
   
 
This example code is silly, of course; we could just write
 
This example code is silly, of course; we could just write
<hask>sum [1..5]</hask> and <hask>product [1..5]</hask>. Nevertheless, these instances are useful in more generalized settings, as we will see in the [[Foldable|section <hask>Foldable</hask>]].</li>
+
<code>sum [1..5]</code> and <code>product [1..5]</code>. Nevertheless, these instances are useful in more generalized settings, as we will see in the [[Foldable|section <code>Foldable</code>]].</li>
   
<li><hask>Any</hask> and <hask>All</hask> are <hask>newtype</hask> wrappers providing <hask>Monoid</hask> instances for <hask>Bool</hask> (under disjunction and conjunction, respectively).</li>
+
<li><code>Any</code> and <code>All</code> are <code>newtype</code> wrappers providing <code>Monoid</code> instances for <code>Bool</code> (under disjunction and conjunction, respectively).</li>
   
<li> There are three instances for <hask>Maybe</hask>: a basic instance which lifts a <hask>Monoid</hask> instance for <hask>a</hask> to an instance for <hask>Maybe a</hask>, and two <hask>newtype</hask> wrappers <hask>First</hask> and <hask>Last</hask> for which <hask>mappend</hask> selects the first (respectively last) non-<hask>Nothing</hask> item.</li>
+
<li> There are three instances for <code>Maybe</code>: a basic instance which lifts a <code>Monoid</code> instance for <code>a</code> to an instance for <code>Maybe a</code>, and two <code>newtype</code> wrappers <code>First</code> and <code>Last</code> for which <code>mappend</code> selects the first (respectively last) non-<code>Nothing</code> item.</li>
   
<li><hask>Endo a</hask> is a newtype wrapper for functions <hask>a -> a</hask>, which form a monoid under composition.</li>
+
<li><code>Endo a</code> is a newtype wrapper for functions <code>a -> a</code>, which form a monoid under composition.</li>
   
<li>There are several ways to “lift” <hask>Monoid</hask> instances to instances with additional structure. We have already seen that an instance for <hask>a</hask> can be lifted to an instance for <hask>Maybe a</hask>. There are also tuple instances: if <hask>a</hask> and <hask>b</hask> are instances of code>Monoid</hask>, then so is <hask>(a,b)</hask>, using the monoid operations for <hask>a</hask> and <hask>b</hask> in the obvious pairwise manner. Finally, if <hask>a</hask> is a <hask>Monoid</hask>, then so is the function type <hask>e -> a</hask> for any <hask>e</hask>; in particular, <hask>g `mappend` h</hask> is the function which applies both <hask>g</hask> and <hask>h</hask> to its argument and then combines the results using the underlying <hask>Monoid</hask> instance for <hask>a</hask>. This can be quite useful and elegant (see [http://thread.gmane.org/gmane.comp.lang.haskell.cafe/52416 example]).</li>
+
<li>There are several ways to “lift” <code>Monoid</code> instances to instances with additional structure. We have already seen that an instance for <code>a</code> can be lifted to an instance for <code>Maybe a</code>. There are also tuple instances: if <code>a</code> and <code>b</code> are instances of code>Monoid</code>, then so is <code>(a,b)</code>, using the monoid operations for <code>a</code> and <code>b</code> in the obvious pairwise manner. Finally, if <code>a</code> is a <code>Monoid</code>, then so is the function type <code>e -> a</code> for any <code>e</code>; in particular, <code>g `mappend` h</code> is the function which applies both <code>g</code> and <code>h</code> to its argument and then combines the results using the underlying <code>Monoid</code> instance for <code>a</code>. This can be quite useful and elegant (see [http://thread.gmane.org/gmane.comp.lang.haskell.cafe/52416 example]).</li>
   
<li>The type <hask>Ordering = LT || EQ || GT</hask> is a <hask>Monoid</hask>, defined in such a way that <hask>mconcat (zipWith compare xs ys)</hask> computes the lexicographic ordering of <hask>xs</hask> and <hask>ys</hask> (if <hask>xs</hask> and <hask>ys</hask> have the same length). In particular, <hask>mempty = EQ</hask>, and <hask>mappend</hask> evaluates to its leftmost non-<hask>EQ</hask> argument (or <hask>EQ</hask> if both arguments are <hask>EQ</hask>). This can be used together with the function instance of <hask>Monoid</hask> to do some clever things ([http://www.reddit.com/r/programming/comments/7cf4r/monoids_in_my_programming_language/c06adnx example]).</li>
+
<li>The type <code>Ordering = LT || EQ || GT</code> is a <code>Monoid</code>, defined in such a way that <code>mconcat (zipWith compare xs ys)</code> computes the lexicographic ordering of <code>xs</code> and <code>ys</code> (if <code>xs</code> and <code>ys</code> have the same length). In particular, <code>mempty = EQ</code>, and <code>mappend</code> evaluates to its leftmost non-<code>EQ</code> argument (or <code>EQ</code> if both arguments are <code>EQ</code>). This can be used together with the function instance of <code>Monoid</code> to do some clever things ([http://www.reddit.com/r/programming/comments/7cf4r/monoids_in_my_programming_language/c06adnx example]).</li>
   
<li>There are also <hask>Monoid</hask> instances for several standard data structures in the containers library ([http://hackage.haskell.org/packages/archive/containers/0.2.0.0/doc/html/index.html haddock]), including <hask>Map</hask>, <hask>Set</hask>, and <hask>Sequence</hask>.</li>
+
<li>There are also <code>Monoid</code> instances for several standard data structures in the containers library ([http://hackage.haskell.org/packages/archive/containers/0.2.0.0/doc/html/index.html haddock]), including <code>Map</code>, <code>Set</code>, and <code>Sequence</code>.</li>
 
</ul>
 
</ul>
   
<hask>Monoid</hask> is also used to enable several other type class instances.
+
<code>Monoid</code> is also used to enable several other type class instances.
As noted previously, we can use <hask>Monoid</hask> to make <hask>((,) e)</hask> an instance of <hask>Applicative</hask>:
+
As noted previously, we can use <code>Monoid</code> to make <code>((,) e)</code> an instance of <code>Applicative</code>:
   
 
<haskell>
 
<haskell>
Line 674: Line 674:
 
</haskell>
 
</haskell>
   
<hask>Monoid</hask> can be similarly used to make <hask>((,) e)</hask> an instance of <hask>Monad</hask> as well; this is known as the ''writer monad''. As we’ve already seen, <hask>Writer</hask> and <hask>WriterT</hask> are a newtype wrapper and transformer for this monad, respectively.
+
<code>Monoid</code> can be similarly used to make <code>((,) e)</code> an instance of <code>Monad</code> as well; this is known as the ''writer monad''. As we’ve already seen, <code>Writer</code> and <code>WriterT</code> are a newtype wrapper and transformer for this monad, respectively.
   
<hask>Monoid</hask> also plays a key role in the <hask>Foldable</hask> type class (see section [[#Foldable|Foldable]]).
+
<code>Monoid</code> also plays a key role in the <code>Foldable</code> type class (see section [[#Foldable|Foldable]]).
   
 
==Other monoidal classes: Alternative, MonadPlus, ArrowPlus==
 
==Other monoidal classes: Alternative, MonadPlus, ArrowPlus==
   
The <hask>Alternative</hask> type class ([http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#g:2 haddock])
+
The <code>Alternative</code> type class ([http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#g:2 haddock])
is for <hask>Applicative</hask> functors which also have
+
is for <code>Applicative</code> functors which also have
 
a monoid structure:
 
a monoid structure:
   
Line 690: Line 690:
 
</haskell>
 
</haskell>
   
Of course, instances of <hask>Alternative</hask> should satisfy the monoid laws.
+
Of course, instances of <code>Alternative</code> should satisfy the monoid laws.
   
Likewise, <hask>MonadPlus</hask> ([http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html#t:MonadPlus haddock])
+
Likewise, <code>MonadPlus</code> ([http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html#t:MonadPlus haddock])
is for <hask>Monad</hask>s with a monoid structure:
+
is for <code>Monad</code>s with a monoid structure:
   
 
<haskell>
 
<haskell>
Line 701: Line 701:
 
</haskell>
 
</haskell>
   
The <hask>MonadPlus</hask> documentation states that it is intended to model
+
The <code>MonadPlus</code> documentation states that it is intended to model
 
monads which also support “choice and failure”; in addition to the
 
monads which also support “choice and failure”; in addition to the
monoid laws, instances of <hask>MonadPlus</hask> are expected to satisfy
+
monoid laws, instances of <code>MonadPlus</code> are expected to satisfy
   
 
<haskell>
 
<haskell>
Line 710: Line 710:
 
</haskell>
 
</haskell>
   
which explains the sense in which <hask>mzero</hask> denotes failure. Since
+
which explains the sense in which <code>mzero</code> denotes failure. Since
<hask>mzero</hask> should be the identity for <hask>mplus</hask>, the computation <hask>m1 `mplus` m2</hask> succeeds (evaluates to something other than <hask>mzero</hask>) if
+
<code>mzero</code> should be the identity for <code>mplus</code>, the computation <code>m1 `mplus` m2</code> succeeds (evaluates to something other than <code>mzero</code>) if
either <hask>m1</hask> or <hask>m2</hask> does; so <hask>mplus</hask> represents choice. The <hask>guard</hask>
+
either <code>m1</code> or <code>m2</code> does; so <code>mplus</code> represents choice. The <code>guard</code>
function can also be used with instances of <hask>MonadPlus</hask>; it requires a
+
function can also be used with instances of <code>MonadPlus</code>; it requires a
condition to be satisfied and fails (using <hask>mzero</hask>) if it is not. A
+
condition to be satisfied and fails (using <code>mzero</code>) if it is not. A
simple example of a <hask>MonadPlus</hask> instance is <hask>[]</hask>, which is exactly the
+
simple example of a <code>MonadPlus</code> instance is <code>[]</code>, which is exactly the
same as the <hask>Monoid</hask> instance for <hask>[]</hask>: the empty list represents
+
same as the <code>Monoid</code> instance for <code>[]</code>: the empty list represents
 
failure, and list concatenation represents choice. In general,
 
failure, and list concatenation represents choice. In general,
however, a <hask>MonadPlus</hask> instance for a type need not be the same as its
+
however, a <code>MonadPlus</code> instance for a type need not be the same as its
<hask>Monoid</hask> instance; <hask>Maybe</hask> is an example of such a type. A great
+
<code>Monoid</code> instance; <code>Maybe</code> is an example of such a type. A great
introduction to the <hask>MonadPlus</hask> type class, with interesting examples
+
introduction to the <code>MonadPlus</code> type class, with interesting examples
 
of its use, is Doug Auclair’s ''MonadPlus: What a Super Monad!'' in [http://www.haskell.org/wikiupload/6/6a/TMR-Issue11.pdf the Monad.Reader issue 11].
 
of its use, is Doug Auclair’s ''MonadPlus: What a Super Monad!'' in [http://www.haskell.org/wikiupload/6/6a/TMR-Issue11.pdf the Monad.Reader issue 11].
   
There used to be a type class called <hask>MonadZero</hask> containing only
+
There used to be a type class called <code>MonadZero</code> containing only
<hask>mzero</hask>, representing monads with failure. The <hask>do</hask>-notation requires
+
<code>mzero</code>, representing monads with failure. The <code>do</code>-notation requires
 
some notion of failure to deal with failing pattern matches.
 
some notion of failure to deal with failing pattern matches.
Unfortunately, <hask>MonadZero</hask> was scrapped in favor of adding the <hask>fail</hask>
+
Unfortunately, <code>MonadZero</code> was scrapped in favor of adding the <code>fail</code>
method to the <hask>Monad</hask> class. If we are lucky, someday <hask>MonadZero</hask> will
+
method to the <code>Monad</code> class. If we are lucky, someday <code>MonadZero</code> will
be restored, and <hask>fail</hask> will be banished to the bit bucket where it
+
be restored, and <code>fail</code> will be banished to the bit bucket where it
 
belongs (see [[MonadPlus reform proposal]]). The idea is that any
 
belongs (see [[MonadPlus reform proposal]]). The idea is that any
<hask>do</hask>-block which uses pattern matching (and hence may fail) would require
+
<code>do</code>-block which uses pattern matching (and hence may fail) would require
a <hask>MonadZero</hask> constraint; otherwise, only a <hask>Monad</hask> constraint would be
+
a <code>MonadZero</code> constraint; otherwise, only a <code>Monad</code> constraint would be
 
required.
 
required.
   
Finally, <hask>ArrowZero</hask> and <hask>ArrowPlus</hask> ([http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#t:ArrowZero haddock])
+
Finally, <code>ArrowZero</code> and <code>ArrowPlus</code> ([http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#t:ArrowZero haddock])
represent <hask>Arrow</hask>s ([[#Arrow|see below]]) with a
+
represent <code>Arrow</code>s ([[#Arrow|see below]]) with a
 
monoid structure:
 
monoid structure:
   
Line 752: Line 752:
 
[http://enfranchisedmind.com/blog/posts/random-thoughts-on-haskell/ a blog post by Brian Hurt], in which he
 
[http://enfranchisedmind.com/blog/posts/random-thoughts-on-haskell/ a blog post by Brian Hurt], in which he
 
complained about the fact that the names of many Haskell type classes
 
complained about the fact that the names of many Haskell type classes
(<hask>Monoid</hask> in particular) are taken from abstract mathematics. This
+
(<code>Monoid</code> in particular) are taken from abstract mathematics. This
 
resulted in [http://thread.gmane.org/gmane.comp.lang.haskell.cafe/50590 a long haskell-cafe thread]
 
resulted in [http://thread.gmane.org/gmane.comp.lang.haskell.cafe/50590 a long haskell-cafe thread]
 
arguing the point and discussing monoids in general.
 
arguing the point and discussing monoids in general.
Line 759: Line 759:
   
 
However, this was quickly followed by several blog posts about
 
However, this was quickly followed by several blog posts about
<hask>Monoid</hask> {{noteref}}. First, Dan Piponi
+
<code>Monoid</code> {{noteref}}. First, Dan Piponi
 
wrote a great introductory post, [http://blog.sigfpe.com/2009/01/haskell-monoids-and-their-uses.html Haskell Monoids and their Uses]. This was quickly followed by
 
wrote a great introductory post, [http://blog.sigfpe.com/2009/01/haskell-monoids-and-their-uses.html Haskell Monoids and their Uses]. This was quickly followed by
 
Heinrich Apfelmus’s [http://apfelmus.nfshost.com/monoid-fingertree.html Monoids and Finger Trees], an accessible exposition of
 
Heinrich Apfelmus’s [http://apfelmus.nfshost.com/monoid-fingertree.html Monoids and Finger Trees], an accessible exposition of
 
Hinze and Paterson’s [http://www.soi.city.ac.uk/%7Eross/papers/FingerTree.html classic paper on 2-3 finger trees], which makes very clever
 
Hinze and Paterson’s [http://www.soi.city.ac.uk/%7Eross/papers/FingerTree.html classic paper on 2-3 finger trees], which makes very clever
use of <hask>Monoid</hask> to implement an elegant and generic data structure.
+
use of <code>Monoid</code> to implement an elegant and generic data structure.
Dan Piponi then wrote two fascinating articles about using <hask>Monoids</hask>
+
Dan Piponi then wrote two fascinating articles about using <code>Monoids</code>
 
(and finger trees): [http://blog.sigfpe.com/2009/01/fast-incremental-regular-expression.html Fast Incremental Regular Expressions] and [http://blog.sigfpe.com/2009/01/beyond-regular-expressions-more.html Beyond Regular Expressions]
 
(and finger trees): [http://blog.sigfpe.com/2009/01/fast-incremental-regular-expression.html Fast Incremental Regular Expressions] and [http://blog.sigfpe.com/2009/01/beyond-regular-expressions-more.html Beyond Regular Expressions]
   
In a similar vein, David Place’s article on improving <hask>Data.Map</hask> in
+
In a similar vein, David Place’s article on improving <code>Data.Map</code> in
 
order to compute incremental folds (see [http://www.haskell.org/sitewiki/images/6/6a/TMR-Issue11.pdf the Monad Reader issue 11])
 
order to compute incremental folds (see [http://www.haskell.org/sitewiki/images/6/6a/TMR-Issue11.pdf the Monad Reader issue 11])
 
is also a
 
is also a
good example of using <hask>Monoid</hask> to generalize a data structure.
+
good example of using <code>Monoid</code> to generalize a data structure.
   
Some other interesting examples of <hask>Monoid</hask> use include [http://www.reddit.com/r/programming/comments/7cf4r/monoids_in_my_programming_language/c06adnx building elegant list sorting combinators],
+
Some other interesting examples of <code>Monoid</code> use include [http://www.reddit.com/r/programming/comments/7cf4r/monoids_in_my_programming_language/c06adnx building elegant list sorting combinators],
 
[http://byorgey.wordpress.com/2008/04/17/collecting-unstructured-information-with-the-monoid-of-partial-knowledge/ collecting unstructured information],
 
[http://byorgey.wordpress.com/2008/04/17/collecting-unstructured-information-with-the-monoid-of-partial-knowledge/ collecting unstructured information],
 
and a brilliant series of posts by Chung-Chieh Shan and Dylan Thurston
 
and a brilliant series of posts by Chung-Chieh Shan and Dylan Thurston
using <hask>Monoid</hask>s to [http://conway.rutgers.edu/~ccshan/wiki/blog/posts/WordNumbers1/ elegantly solve a difficult combinatorial puzzle] (followed by
+
using <code>Monoid</code>s to [http://conway.rutgers.edu/~ccshan/wiki/blog/posts/WordNumbers1/ elegantly solve a difficult combinatorial puzzle] (followed by
 
[http://conway.rutgers.edu/~ccshan/wiki/blog/posts/WordNumbers2/ part 2],
 
[http://conway.rutgers.edu/~ccshan/wiki/blog/posts/WordNumbers2/ part 2],
 
[http://conway.rutgers.edu/~ccshan/wiki/blog/posts/WordNumbers3/ part 3],
 
[http://conway.rutgers.edu/~ccshan/wiki/blog/posts/WordNumbers3/ part 3],
Line 781: Line 781:
   
 
As unlikely as it sounds, monads can actually be viewed as a sort of
 
As unlikely as it sounds, monads can actually be viewed as a sort of
monoid, with <hask>join</hask> playing the role of the binary operation and
+
monoid, with <code>join</code> playing the role of the binary operation and
<hask>return</hask> the role of the identity; see [http://blog.sigfpe.com/2008/11/from-monoids-to-monads.html Dan Piponi’s blog post].
+
<code>return</code> the role of the identity; see [http://blog.sigfpe.com/2008/11/from-monoids-to-monads.html Dan Piponi’s blog post].
   
 
=Foldable=
 
=Foldable=
   
The <hask>Foldable</hask> class, defined in the <hask>Data.Foldable</hask>
+
The <code>Foldable</code> class, defined in the <code>Data.Foldable</code>
 
module ([http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Foldable.html haddock]), abstracts over containers which can be
 
module ([http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Foldable.html haddock]), abstracts over containers which can be
 
“folded” into a summary value. This allows such folding operations
 
“folded” into a summary value. This allows such folding operations
Line 793: Line 793:
 
==Definition==
 
==Definition==
   
The definition of the <hask>Foldable</hask> type class is:
+
The definition of the <code>Foldable</code> type class is:
   
 
<haskell>
 
<haskell>
Line 806: Line 806:
 
</haskell>
 
</haskell>
   
This may look complicated, but in fact, to make a <hask>Foldable</hask> instance
+
This may look complicated, but in fact, to make a <code>Foldable</code> instance
you only need to implement one method: your choice of <hask>foldMap</hask> or
+
you only need to implement one method: your choice of <code>foldMap</code> or
<hask>foldr</hask>. All the other methods have default implementations in terms
+
<code>foldr</code>. All the other methods have default implementations in terms
 
of these, and are presumably included in the class in case more
 
of these, and are presumably included in the class in case more
 
efficient implementations can be provided.
 
efficient implementations can be provided.
Line 814: Line 814:
 
==Instances and examples==
 
==Instances and examples==
   
The type of <hask>foldMap</hask> should make it clear what it is supposed to do:
+
The type of <code>foldMap</code> should make it clear what it is supposed to do:
given a way to convert the data in a container into a <hask>Monoid</hask> (a
+
given a way to convert the data in a container into a <code>Monoid</code> (a
function <hask>a -> m</hask>) and a container of <hask>a</hask>’s (<hask>t a</hask>), <hask>foldMap</hask>
+
function <code>a -> m</code>) and a container of <code>a</code>’s (<code>t a</code>), <code>foldMap</code>
 
provides a way to iterate over the entire contents of the container,
 
provides a way to iterate over the entire contents of the container,
converting all the <hask>a</hask>’s to <hask>m</hask>’s and combining all the <hask>m</hask>’s with
+
converting all the <code>a</code>’s to <code>m</code>’s and combining all the <code>m</code>’s with
<hask>mappend</hask>. The following code shows two examples: a simple
+
<code>mappend</code>. The following code shows two examples: a simple
implementation of <hask>foldMap</hask> for lists, and a binary tree example
+
implementation of <code>foldMap</code> for lists, and a binary tree example
provided by the <hask>Foldable</hask> documentation.
+
provided by the <code>Foldable</code> documentation.
   
 
<haskell>
 
<haskell>
Line 836: Line 836:
 
</haskell>
 
</haskell>
   
The <hask>foldr</hask> function has a type similar to the <hask>foldr</hask> found in the <hask>Prelude</hask>, but
+
The <code>foldr</code> function has a type similar to the <code>foldr</code> found in the <code>Prelude</code>, but
more general, since the <hask>foldr</hask> in the <hask>Prelude</hask> works only on lists.
+
more general, since the <code>foldr</code> in the <code>Prelude</code> works only on lists.
   
The <hask>Foldable</hask> module also provides instances for <hask>Maybe</hask> and <hask>Array</hask>;
+
The <code>Foldable</code> module also provides instances for <code>Maybe</code> and <code>Array</code>;
additionally, many of the data structures found in the standard [http://hackage.haskell.org/package/containers containers library] (for example, <hask>Map</hask>, <hask>Set</hask>, <hask>Tree</hask>,
+
additionally, many of the data structures found in the standard [http://hackage.haskell.org/package/containers containers library] (for example, <code>Map</code>, <code>Set</code>, <code>Tree</code>,
and <hask>Sequence</hask>) provide their own <hask>Foldable</hask> instances.
+
and <code>Sequence</code>) provide their own <code>Foldable</code> instances.
   
 
==Derived folds==
 
==Derived folds==
   
Given an instance of <hask>Foldable</hask>, we can write generic,
+
Given an instance of <code>Foldable</code>, we can write generic,
 
container-agnostic functions such as:
 
container-agnostic functions such as:
   
Line 863: Line 863:
 
</haskell>
 
</haskell>
   
The <hask>Foldable</hask> module also provides a large number of predefined
+
The <code>Foldable</code> module also provides a large number of predefined
folds, many of which are generalized versions of <hask>Prelude</hask> functions of the
+
folds, many of which are generalized versions of <code>Prelude</code> functions of the
same name that only work on lists: <hask>concat</hask>, <hask>concatMap</hask>, <hask>and</hask>,
+
same name that only work on lists: <code>concat</code>, <code>concatMap</code>, <code>and</code>,
<hask>or</hask>, <hask>any</hask>, <hask>all</hask>, <hask>sum</hask>, <hask>product</hask>, <hask>maximum</hask>(<hask>By</hask>),
+
<code>or</code>, <code>any</code>, <code>all</code>, <code>sum</code>, <code>product</code>, <code>maximum</code>(<code>By</code>),
<hask>minimum</hask>(<hask>By</hask>), <hask>elem</hask>, <hask>notElem</hask>, and <hask>find</hask>. The reader may enjoy
+
<code>minimum</code>(<code>By</code>), <code>elem</code>, <code>notElem</code>, and <code>find</code>. The reader may enjoy
coming up with elegant implementations of these functions using <hask>fold</hask>
+
coming up with elegant implementations of these functions using <code>fold</code>
or <hask>foldMap</hask> and appropriate <hask>Monoid</hask> instances.
+
or <code>foldMap</code> and appropriate <code>Monoid</code> instances.
   
There are also generic functions that work with <hask>Applicative</hask> or
+
There are also generic functions that work with <code>Applicative</code> or
<hask>Monad</hask> instances to generate some sort of computation from each
+
<code>Monad</code> instances to generate some sort of computation from each
 
element in a container, and then perform all the side effects from
 
element in a container, and then perform all the side effects from
those computations, discarding the results: <hask>traverse_</hask>, <hask>sequenceA_</hask>,
+
those computations, discarding the results: <code>traverse_</code>, <code>sequenceA_</code>,
and others. The results must be discarded because the <hask>Foldable</hask>
+
and others. The results must be discarded because the <code>Foldable</code>
 
class is too weak to specify what to do with them: we cannot, in
 
class is too weak to specify what to do with them: we cannot, in
general, make an arbitrary <hask>Applicative</hask> or <hask>Monad</hask> instance into a
+
general, make an arbitrary <code>Applicative</code> or <code>Monad</code> instance into a
<hask>Monoid</hask>. If we do have an <hask>Applicative</hask> or <hask>Monad</hask> with a monoid
+
<code>Monoid</code>. If we do have an <code>Applicative</code> or <code>Monad</code> with a monoid
structure—that is, an <hask>Alternative</hask> or a <hask>MonadPlus</hask>—then we can
+
structure—that is, an <code>Alternative</code> or a <code>MonadPlus</code>—then we can
use the <hask>asum</hask> or <hask>msum</hask> functions, which can combine the results as
+
use the <code>asum</code> or <code>msum</code> functions, which can combine the results as
well. Consult the [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Foldable.html <hask>Foldable</hask> documentation] for
+
well. Consult the [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Foldable.html <code>Foldable</code> documentation] for
 
more details on any of these functions.
 
more details on any of these functions.
   
Note that the <hask>Foldable</hask> operations always forget the structure of
+
Note that the <code>Foldable</code> operations always forget the structure of
the container being folded. If we start with a container of type <hask>t a</hask> for some <hask>Foldable t</hask>, then <hask>t</hask> will never appear in the output
+
the container being folded. If we start with a container of type <code>t a</code> for some <code>Foldable t</code>, then <code>t</code> will never appear in the output
type of any operations defined in the <hask>Foldable</hask> module. Many times
+
type of any operations defined in the <code>Foldable</code> module. Many times
 
this is exactly what we want, but sometimes we would like to be able
 
this is exactly what we want, but sometimes we would like to be able
 
to generically traverse a container while preserving its
 
to generically traverse a container while preserving its
structure—and this is exactly what the <hask>Traversable</hask> class provides,
+
structure—and this is exactly what the <code>Traversable</code> class provides,
 
which will be discussed in the next section.
 
which will be discussed in the next section.
   
 
==Further reading==
 
==Further reading==
   
The <hask>Foldable</hask> class had its genesis in [http://www.soi.city.ac.uk/~ross/papers/Applicative.html McBride and Paterson’s paper]
+
The <code>Foldable</code> class had its genesis in [http://www.soi.city.ac.uk/~ross/papers/Applicative.html McBride and Paterson’s paper]
introducing <hask>Applicative</hask>, although it has
+
introducing <code>Applicative</code>, although it has
 
been fleshed out quite a bit from the form in the paper.
 
been fleshed out quite a bit from the form in the paper.
   
An interesting use of <hask>Foldable</hask> (as well as <hask>Traversable</hask>) can be
+
An interesting use of <code>Foldable</code> (as well as <code>Traversable</code>) can be
 
found in Janis Voigtländer’s paper [http://doi.acm.org/10.1145/1480881.1480904 Bidirectionalization for free!].
 
found in Janis Voigtländer’s paper [http://doi.acm.org/10.1145/1480881.1480904 Bidirectionalization for free!].
   
Line 905: Line 905:
 
==Definition==
 
==Definition==
   
The <hask>Traversable</hask> type class, defined in the <hask>Data.Traversable</hask>
+
The <code>Traversable</code> type class, defined in the <code>Data.Traversable</code>
 
module ([http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Traversable.html haddock]), is:
 
module ([http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Traversable.html haddock]), is:
   
Line 916: Line 916:
 
</haskell>
 
</haskell>
   
As you can see, every <hask>Traversable</hask> is also a foldable functor. Like
+
As you can see, every <code>Traversable</code> is also a foldable functor. Like
<hask>Foldable</hask>, there is a lot in this type class, but making instances is
+
<code>Foldable</code>, there is a lot in this type class, but making instances is
actually rather easy: one need only implement <hask>traverse</hask> or
+
actually rather easy: one need only implement <code>traverse</code> or
<hask>sequenceA</hask>; the other methods all have default implementations in
+
<code>sequenceA</code>; the other methods all have default implementations in
 
terms of these functions. A good exercise is to figure out what the default
 
terms of these functions. A good exercise is to figure out what the default
implementations should be: given either <hask>traverse</hask> or <hask>sequenceA</hask>, how
+
implementations should be: given either <code>traverse</code> or <code>sequenceA</code>, how
would you define the other three methods? (Hint for <hask>mapM</hask>:
+
would you define the other three methods? (Hint for <code>mapM</code>:
<hask>Control.Applicative</hask> exports the <hask>WrapMonad</hask> newtype, which makes any
+
<code>Control.Applicative</code> exports the <code>WrapMonad</code> newtype, which makes any
<hask>Monad</hask> into an <hask>Applicative</hask>. The <hask>sequence</hask> function can be implemented in terms
+
<code>Monad</code> into an <code>Applicative</code>. The <code>sequence</code> function can be implemented in terms
of <hask>mapM</hask>.)
+
of <code>mapM</code>.)
   
 
==Intuition==
 
==Intuition==
   
The key method of the <hask>Traversable</hask> class, and the source of its
+
The key method of the <code>Traversable</code> class, and the source of its
unique power, is <hask>sequenceA</hask>. Consider its type:
+
unique power, is <code>sequenceA</code>. Consider its type:
 
<haskell>
 
<haskell>
 
sequenceA :: Applicative f => t (f a) -> f (t a)
 
sequenceA :: Applicative f => t (f a) -> f (t a)
Line 942: Line 942:
 
The ability to compose two monads depends crucially on this ability to
 
The ability to compose two monads depends crucially on this ability to
 
commute functors. Intuitively, if we want to build a composed monad
 
commute functors. Intuitively, if we want to build a composed monad
<hask>M a = m (n a)</hask> out of monads <hask>m</hask> and <hask>n</hask>, then to be able to
+
<code>M a = m (n a)</code> out of monads <code>m</code> and <code>n</code>, then to be able to
implement <hask>join :: M (M a) -> M a</hask>, that is,
+
implement <code>join :: M (M a) -> M a</code>, that is,
<hask>join :: m (n (m (n a))) -> m (n a)</hask>, we have to be able to commute
+
<code>join :: m (n (m (n a))) -> m (n a)</code>, we have to be able to commute
the <hask>n</hask> past the <hask>m</hask> to get <hask>m (m (n (n a)))</hask>, and then we can use the
+
the <code>n</code> past the <code>m</code> to get <code>m (m (n (n a)))</code>, and then we can use the
<hask>join</hask>s for <hask>m</hask> and <hask>n</hask> to produce something of type <hask>m (n a)</hask>. See
+
<code>join</code>s for <code>m</code> and <code>n</code> to produce something of type <code>m (n a)</code>. See
 
[http://web.cecs.pdx.edu/~mpj/pubs/springschool.html Mark Jones’s paper] for more details.
 
[http://web.cecs.pdx.edu/~mpj/pubs/springschool.html Mark Jones’s paper] for more details.
   
 
==Instances and examples==
 
==Instances and examples==
   
What’s an example of a <hask>Traversable</hask> instance?
+
What’s an example of a <code>Traversable</code> instance?
 
The following code shows an example instance for the same
 
The following code shows an example instance for the same
<hask>Tree</hask> type used as an example in the previous <hask>Foldable</hask> section. It
+
<code>Tree</code> type used as an example in the previous <code>Foldable</code> section. It
is instructive to compare this instance with a <hask>Functor</hask> instance for
+
is instructive to compare this instance with a <code>Functor</code> instance for
<hask>Tree</hask>, which is also shown.
+
<code>Tree</code>, which is also shown.
   
 
<haskell>
 
<haskell>
Line 975: Line 975:
 
</haskell>
 
</haskell>
   
It should be clear that the <hask>Traversable</hask> and <hask>Functor</hask> instances for
+
It should be clear that the <code>Traversable</code> and <code>Functor</code> instances for
<hask>Tree</hask> are almost identical; the only difference is that the <hask>Functor</hask>
+
<code>Tree</code> are almost identical; the only difference is that the <code>Functor</code>
 
instance involves normal function application, whereas the
 
instance involves normal function application, whereas the
applications in the <hask>Traversable</hask> instance take place within an
+
applications in the <code>Traversable</code> instance take place within an
<hask>Applicative</hask> context, using <hask>(<$>)</hask> and <hask>(<*>)</hask>. In fact, this will
+
<code>Applicative</code> context, using <code>(<$>)</code> and <code>(<*>)</code>. In fact, this will
 
be
 
be
 
true for any type.
 
true for any type.
   
Any <hask>Traversable</hask> functor is also <hask>Foldable</hask>, and a <hask>Functor</hask>. We can see
+
Any <code>Traversable</code> functor is also <code>Foldable</code>, and a <code>Functor</code>. We can see
 
this not only from the class declaration, but by the fact that we can
 
this not only from the class declaration, but by the fact that we can
implement the methods of both classes given only the <hask>Traversable</hask>
+
implement the methods of both classes given only the <code>Traversable</code>
methods. A good exercise is to implement <hask>fmap</hask> and <hask>foldMap</hask> using
+
methods. A good exercise is to implement <code>fmap</code> and <code>foldMap</code> using
only the <hask>Traversable</hask> methods; the implementations are surprisingly
+
only the <code>Traversable</code> methods; the implementations are surprisingly
elegant. The <hask>Traversable</hask> module provides these
+
elegant. The <code>Traversable</code> module provides these
implementations as <hask>fmapDefault</hask> and <hask>foldMapDefault</hask>.
+
implementations as <code>fmapDefault</code> and <code>foldMapDefault</code>.
   
The standard libraries provide a number of <hask>Traversable</hask> instances,
+
The standard libraries provide a number of <code>Traversable</code> instances,
including instances for <hask>[]</hask>, <hask>Maybe</hask>, <hask>Map</hask>, <hask>Tree</hask>, and <hask>Sequence</hask>.
+
including instances for <code>[]</code>, <code>Maybe</code>, <code>Map</code>, <code>Tree</code>, and <code>Sequence</code>.
Notably, <hask>Set</hask> is not <hask>Traversable</hask>, although it is <hask>Foldable</hask>.
+
Notably, <code>Set</code> is not <code>Traversable</code>, although it is <code>Foldable</code>.
   
 
==Further reading==
 
==Further reading==
   
The <hask>Traversable</hask> class also had its genesis in [http://www.soi.city.ac.uk/~ross/papers/Applicative.html McBride and Paterson’s
+
The <code>Traversable</code> class also had its genesis in [http://www.soi.city.ac.uk/~ross/papers/Applicative.html McBride and Paterson’s
<hask>Applicative</hask> paper], and is described in
+
<code>Applicative</code> paper], and is described in
 
more detail in Gibbons and Oliveira, [http://www.comlab.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf The Essence of the Iterator Pattern], which also contains a wealth of
 
more detail in Gibbons and Oliveira, [http://www.comlab.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf The Essence of the Iterator Pattern], which also contains a wealth of
 
references to related work.
 
references to related work.
Line 1,004: Line 1,004:
 
=Category=
 
=Category=
   
<hask>Category</hask> is another fairly new addition to the Haskell standard
+
<code>Category</code> is another fairly new addition to the Haskell standard
 
libraries; you may or may not have it installed depending on the
 
libraries; you may or may not have it installed depending on the
version of your <hask>base</hask> package. It generalizes the notion of
+
version of your <code>base</code> package. It generalizes the notion of
 
function composition to general “morphisms”.
 
function composition to general “morphisms”.
   
The definition of the <hask>Category</hask> type class (from
+
The definition of the <code>Category</code> type class (from
<hask>Control.Category</hask>—[http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Category.html haddock]) is shown below. For ease of reading, note that I have used an
+
<code>Control.Category</code>—[http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Category.html haddock]) is shown below. For ease of reading, note that I have used an
infix type constructor <hask>(~>)</hask>, much like the infix function type
+
infix type constructor <code>(~>)</code>, much like the infix function type
constructor <hask>(->)</hask>. This syntax is not part of Haskell 98.
+
constructor <code>(->)</code>. This syntax is not part of Haskell 98.
 
The second definition shown is the one used in the standard libraries.
 
The second definition shown is the one used in the standard libraries.
 
For the remainder of this document, I will use the infix type
 
For the remainder of this document, I will use the infix type
constructor <hask>(~>)</hask> for <hask>Category</hask> as well as <hask>Arrow</hask>.
+
constructor <code>(~>)</code> for <code>Category</code> as well as <code>Arrow</code>.
   
 
<haskell>
 
<haskell>
Line 1,028: Line 1,028:
 
</haskell>
 
</haskell>
   
Note that an instance of <hask>Category</hask> should be a type constructor which
+
Note that an instance of <code>Category</code> should be a type constructor which
takes two type arguments, that is, something of kind <hask>* -> * -> *</hask>. It
+
takes two type arguments, that is, something of kind <code>* -> * -> *</code>. It
is instructive to imagine the type constructor variable <hask>cat</hask> replaced
+
is instructive to imagine the type constructor variable <code>cat</code> replaced
by the function constructor <hask>(->)</hask>: indeed, in this case we recover
+
by the function constructor <code>(->)</code>: indeed, in this case we recover
precisely the familiar identity function <hask>id</hask> and function composition
+
precisely the familiar identity function <code>id</code> and function composition
operator <hask>(.)</hask> defined in the standard <hask>Prelude</hask>.
+
operator <code>(.)</code> defined in the standard <code>Prelude</code>.
   
Of course, the <hask>Category</hask> module provides exactly such an instance of
+
Of course, the <code>Category</code> module provides exactly such an instance of
<hask>Category</hask> for <hask>(->)</hask>. But it also provides one other instance, shown
+
<code>Category</code> for <code>(->)</code>. But it also provides one other instance, shown
 
below, which should be familiar from the
 
below, which should be familiar from the
previous discussion of the <hask>Monad</hask> laws. <hask>Kleisli m a b</hask>, as defined
+
previous discussion of the <code>Monad</code> laws. <code>Kleisli m a b</code>, as defined
in the <hask>Control.Arrow</hask> module, is just a <hask>newtype</hask> wrapper around <hask>a -> m b</hask>.
+
in the <code>Control.Arrow</code> module, is just a <code>newtype</code> wrapper around <code>a -> m b</code>.
   
 
<haskell>
 
<haskell>
Line 1,049: Line 1,049:
 
</haskell>
 
</haskell>
   
The only law that <hask>Category</hask> instances should satisfy is that <hask>id</hask> and
+
The only law that <code>Category</code> instances should satisfy is that <code>id</code> and
<hask>(.)</hask> should form a monoid—that is, <hask>id</hask> should be the identity of
+
<code>(.)</code> should form a monoid—that is, <code>id</code> should be the identity of
<hask>(.)</hask>, and <hask>(.)</hask> should be associative.
+
<code>(.)</code>, and <code>(.)</code> should be associative.
   
Finally, the <hask>Category</hask> module exports two additional operators:
+
Finally, the <code>Category</code> module exports two additional operators:
<hask>(<<<)</hask>, which is just a synonym for <hask>(.)</hask>, and <hask>(>>>)</hask>, which is
+
<code>(<<<)</code>, which is just a synonym for <code>(.)</code>, and <code>(>>>)</code>, which is
<hask>(.)</hask> with its arguments reversed. (In previous versions of the
+
<code>(.)</code> with its arguments reversed. (In previous versions of the
libraries, these operators were defined as part of the <hask>Arrow</hask> class.)
+
libraries, these operators were defined as part of the <code>Arrow</code> class.)
   
 
==Further reading==
 
==Further reading==
   
The name <hask>Category</hask> is a bit misleading, since the <hask>Category</hask> class
+
The name <code>Category</code> is a bit misleading, since the <code>Category</code> class
 
cannot represent arbitrary categories, but only categories whose
 
cannot represent arbitrary categories, but only categories whose
objects are objects of <hask>Hask</hask>, the category of Haskell types. For a
+
objects are objects of <code>Hask</code>, the category of Haskell types. For a
 
more general treatment of categories within Haskell, see the
 
more general treatment of categories within Haskell, see the
 
[http://hackage.haskell.org/package/category-extras category-extras package]. For more about
 
[http://hackage.haskell.org/package/category-extras category-extras package]. For more about
Line 1,078: Line 1,078:
 
=Arrow=
 
=Arrow=
   
The <hask>Arrow</hask> class represents another abstraction of computation, in a
+
The <code>Arrow</code> class represents another abstraction of computation, in a
similar vein to <hask>Monad</hask> and <hask>Applicative</hask>. However, unlike <hask>Monad</hask>
+
similar vein to <code>Monad</code> and <code>Applicative</code>. However, unlike <code>Monad</code>
and <hask>Applicative</hask>, whose types only reflect their output, the type of
+
and <code>Applicative</code>, whose types only reflect their output, the type of
an <hask>Arrow</hask> computation reflects both its input and output. Arrows
+
an <code>Arrow</code> computation reflects both its input and output. Arrows
generalize functions: if <hask>(~>)</hask> is an instance of <hask>Arrow</hask>, a value of
+
generalize functions: if <code>(~>)</code> is an instance of <code>Arrow</code>, a value of
type <hask>b ~> c</hask> can be thought of as a computation which takes values of
+
type <code>b ~> c</code> can be thought of as a computation which takes values of
type <hask>b</hask> as input, and produces values of type <hask>c</hask> as output. In the
+
type <code>b</code> as input, and produces values of type <code>c</code> as output. In the
<hask>(->)</hask> instance of <hask>Arrow</hask> this is just a pure function; in general, however,
+
<code>(->)</code> instance of <code>Arrow</code> this is just a pure function; in general, however,
 
an arrow may represent some sort of “effectful” computation.
 
an arrow may represent some sort of “effectful” computation.
   
 
==Definition==
 
==Definition==
   
The definition of the <hask>Arrow</hask> type class, from
+
The definition of the <code>Arrow</code> type class, from
<hask>Control.Arrow</hask> ([http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html haddock]), is:
+
<code>Control.Arrow</code> ([http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html haddock]), is:
   
 
<haskell>
 
<haskell>
Line 1,102: Line 1,102:
 
</haskell>
 
</haskell>
   
{{note|In versions of the <hask>base</hask>
+
{{note|In versions of the <code>base</code>
package prior to version 4, there is no <hask>Category</hask> class, and the
+
package prior to version 4, there is no <code>Category</code> class, and the
<hask>Arrow</hask> class includes the arrow composition operator <hask>(>>>)</hask>. It
+
<code>Arrow</code> class includes the arrow composition operator <code>(>>>)</code>. It
also includes <hask>pure</hask> as a synonym for <hask>arr</hask>, but this was removed
+
also includes <code>pure</code> as a synonym for <code>arr</code>, but this was removed
since it conflicts with the <hask>pure</hask> from <hask>Applicative</hask>.}}
+
since it conflicts with the <code>pure</code> from <code>Applicative</code>.}}
   
The first thing to note is the <hask>Category</hask> class constraint, which
+
The first thing to note is the <code>Category</code> class constraint, which
 
means that we get identity arrows and arrow composition for free:
 
means that we get identity arrows and arrow composition for free:
given two arrows <hask>g :: b ~> c</hask> and <hask>h :: c ~> d</hask>, we can form their
+
given two arrows <code>g :: b ~> c</code> and <code>h :: c ~> d</code>, we can form their
composition <hask>g >>> h :: b ~> d</hask> {{noteref}}.
+
composition <code>g >>> h :: b ~> d</code> {{noteref}}.
   
 
As should be a familiar pattern by now, the only methods which must be
 
As should be a familiar pattern by now, the only methods which must be
defined when writing a new instance of <hask>Arrow</hask> are <hask>arr</hask> and <hask>first</hask>;
+
defined when writing a new instance of <code>Arrow</code> are <code>arr</code> and <code>first</code>;
 
the other methods have default definitions in terms of these, but are
 
the other methods have default definitions in terms of these, but are
included in the <hask>Arrow</hask> class so that they can be overridden with more
+
included in the <code>Arrow</code> class so that they can be overridden with more
 
efficient implementations if desired.
 
efficient implementations if desired.
   
Line 1,124: Line 1,124:
 
build intuition.
 
build intuition.
   
* The <hask>arr</hask> function takes any function <hask>b -> c</hask> and turns it into a generalized arrow <hask>b ~> c</hask>. The <hask>arr</hask> method justifies the claim that arrows generalize functions, since it says that we can treat any function as an arrow. It is intended that the arrow <hask>arr g</hask> is “pure” in the sense that it only computes <hask>g</hask> and has no “effects” (whatever that might mean for any particular arrow type).
+
* The <code>arr</code> function takes any function <code>b -> c</code> and turns it into a generalized arrow <code>b ~> c</code>. The <code>arr</code> method justifies the claim that arrows generalize functions, since it says that we can treat any function as an arrow. It is intended that the arrow <code>arr g</code> is “pure” in the sense that it only computes <code>g</code> and has no “effects” (whatever that might mean for any particular arrow type).
   
* The <hask>first</hask> method turns any arrow from <hask>b</hask> to <hask>c</hask> into an arrow from <hask>(b,d)</hask> to <hask>(c,d)</hask>. The idea is that <hask>first g</hask> uses <hask>g</hask> to process the first element of a tuple, and lets the second element pass through unchanged. For the function instance of <hask>Arrow</hask>, of course, <hask>first g (x,y) = (g x, y)</hask>.
+
* The <code>first</code> method turns any arrow from <code>b</code> to <code>c</code> into an arrow from <code>(b,d)</code> to <code>(c,d)</code>. The idea is that <code>first g</code> uses <code>g</code> to process the first element of a tuple, and lets the second element pass through unchanged. For the function instance of <code>Arrow</code>, of course, <code>first g (x,y) = (g x, y)</code>.
   
* The <hask>second</hask> function is similar to <hask>first</hask>, but with the elements of the tuples swapped. Indeed, it can be defined in terms of <hask>first</hask> using an auxiliary function <hask>swap</hask>, defined by <hask>swap (x,y) = (y,x)</hask>.
+
* The <code>second</code> function is similar to <code>first</code>, but with the elements of the tuples swapped. Indeed, it can be defined in terms of <code>first</code> using an auxiliary function <code>swap</code>, defined by <code>swap (x,y) = (y,x)</code>.
   
* The <hask>(***)</hask> operator is “parallel composition” of arrows: it takes two arrows and makes them into one arrow on tuples, which has the behavior of the first arrow on the first element of a tuple, and the behavior of the second arrow on the second element. The mnemonic is that <hask>g *** h</hask> is the ''product'' (hence <hask>*</hask>) of <hask>g</hask> and <hask>h</hask>. For the function instance of <hask>Arrow</hask>, we define <hask>(g *** h) (x,y) = (g x, h y)</hask>. The default implementation of <hask>(***)</hask> is in terms of <hask>first</hask>, <hask>second</hask>, and sequential arrow composition <hask>(>>>)</hask>. The reader may also wish to think about how to implement <hask>first</hask> and <hask>second</hask> in terms of <hask>(***)</hask>.
+
* The <code>(***)</code> operator is “parallel composition” of arrows: it takes two arrows and makes them into one arrow on tuples, which has the behavior of the first arrow on the first element of a tuple, and the behavior of the second arrow on the second element. The mnemonic is that <code>g *** h</code> is the ''product'' (hence <code>*</code>) of <code>g</code> and <code>h</code>. For the function instance of <code>Arrow</code>, we define <code>(g *** h) (x,y) = (g x, h y)</code>. The default implementation of <code>(***)</code> is in terms of <code>first</code>, <code>second</code>, and sequential arrow composition <code>(>>>)</code>. The reader may also wish to think about how to implement <code>first</code> and <code>second</code> in terms of <code>(***)</code>.
   
* The <hask>(&&&)</hask> operator is “fanout composition” of arrows: it takes two arrows <hask>g</hask> and <hask>h</hask> and makes them into a new arrow <hask>g &&& h</hask> which supplies its input as the input to both <hask>g</hask> and <hask>h</hask>, returning their results as a tuple. The mnemonic is that <hask>g &&& h</hask> performs both <hask>g</hask> ''and'' <hask>h</hask> (hence <hask>&</hask>) on its input. For functions, we define <hask>(g &&& h) x = (g x, h x)</hask>.
+
* The <code>(&&&)</code> operator is “fanout composition” of arrows: it takes two arrows <code>g</code> and <code>h</code> and makes them into a new arrow <code>g &&& h</code> which supplies its input as the input to both <code>g</code> and <code>h</code>, returning their results as a tuple. The mnemonic is that <code>g &&& h</code> performs both <code>g</code> ''and'' <code>h</code> (hence <code>&</code>) on its input. For functions, we define <code>(g &&& h) x = (g x, h x)</code>.
   
 
==Instances==
 
==Instances==
   
The <hask>Arrow</hask> library itself only provides two <hask>Arrow</hask> instances, both
+
The <code>Arrow</code> library itself only provides two <code>Arrow</code> instances, both
of which we have already seen: <hask>(->)</hask>, the normal function
+
of which we have already seen: <code>(->)</code>, the normal function
constructor, and <hask>Kleisli m</hask>, which makes functions of
+
constructor, and <code>Kleisli m</code>, which makes functions of
type <hask>a -> m b</hask> into <hask>Arrow</hask>s for any <hask>Monad m</hask>. These instances are:
+
type <code>a -> m b</code> into <code>Arrow</code>s for any <code>Monad m</code>. These instances are:
   
 
<haskell>
 
<haskell>
Line 1,158: Line 1,158:
 
{{note|See [http://dx.doi.org/10.1016/S0167-6423(99)00023-4 John Hughes: Generalising monads to arrows]; [http://homepages.inf.ed.ac.uk/wadler/papers/arrows/arrows.pdf Sam Lindley, Philip Wadler, Jeremy Yallop: The arrow calculus]; [http://www.soi.city.ac.uk/~ross/papers/fop.html Ross Paterson: Programming with Arrows].}}
 
{{note|See [http://dx.doi.org/10.1016/S0167-6423(99)00023-4 John Hughes: Generalising monads to arrows]; [http://homepages.inf.ed.ac.uk/wadler/papers/arrows/arrows.pdf Sam Lindley, Philip Wadler, Jeremy Yallop: The arrow calculus]; [http://www.soi.city.ac.uk/~ross/papers/fop.html Ross Paterson: Programming with Arrows].}}
   
There are quite a few laws that instances of <hask>Arrow</hask> should
+
There are quite a few laws that instances of <code>Arrow</code> should
 
satisfy {{noteref}}:
 
satisfy {{noteref}}:
   
Line 1,175: Line 1,175:
 
Note that this version of the laws is slightly different than the laws given in the
 
Note that this version of the laws is slightly different than the laws given in the
 
first two above references, since several of the laws have now been
 
first two above references, since several of the laws have now been
subsumed by the <hask>Category</hask> laws (in particular, the requirements that
+
subsumed by the <code>Category</code> laws (in particular, the requirements that
<hask>id</hask> is the identity arrow and that <hask>(>>>)</hask> is associative). The laws
+
<code>id</code> is the identity arrow and that <code>(>>>)</code> is associative). The laws
 
shown here follow those in Paterson’s Programming with Arrows, which uses the
 
shown here follow those in Paterson’s Programming with Arrows, which uses the
<hask>Category</hask> class.
+
<code>Category</code> class.
   
 
{{note|Unless category-theory-induced insomnolence is your cup of tea.}}
 
{{note|Unless category-theory-induced insomnolence is your cup of tea.}}
   
The reader is advised not to lose too much sleep over the <hask>Arrow</hask>
+
The reader is advised not to lose too much sleep over the <code>Arrow</code>
 
laws {{noteref}}, since it is not essential to understand them in order to
 
laws {{noteref}}, since it is not essential to understand them in order to
program with arrows. There are also laws that <hask>ArrowChoice</hask>,
+
program with arrows. There are also laws that <code>ArrowChoice</code>,
<hask>ArrowApply</hask>, and <hask>ArrowLoop</hask> instances should satisfy; the interested
+
<code>ArrowApply</code>, and <code>ArrowLoop</code> instances should satisfy; the interested
 
reader should consult [http://www.soi.city.ac.uk/~ross/papers/fop.html Paterson: Programming with Arrows].
 
reader should consult [http://www.soi.city.ac.uk/~ross/papers/fop.html Paterson: Programming with Arrows].
   
 
==ArrowChoice==
 
==ArrowChoice==
   
Computations built using the <hask>Arrow</hask> class, like those built using
+
Computations built using the <code>Arrow</code> class, like those built using
the <hask>Applicative</hask> class, are rather inflexible: the structure of the computation
+
the <code>Applicative</code> class, are rather inflexible: the structure of the computation
 
is fixed at the outset, and there is no ability to choose between
 
is fixed at the outset, and there is no ability to choose between
 
alternate execution paths based on intermediate results.
 
alternate execution paths based on intermediate results.
The <hask>ArrowChoice</hask> class provides exactly such an ability:
+
The <code>ArrowChoice</code> class provides exactly such an ability:
   
 
<haskell>
 
<haskell>
Line 1,204: Line 1,204:
 
</haskell>
 
</haskell>
   
A comparison of <hask>ArrowChoice</hask> to <hask>Arrow</hask> will reveal a striking
+
A comparison of <code>ArrowChoice</code> to <code>Arrow</code> will reveal a striking
parallel between <hask>left</hask>, <hask>right</hask>, <hask>(+++)</hask>, <hask>(|||)</hask> and <hask>first</hask>,
+
parallel between <code>left</code>, <code>right</code>, <code>(+++)</code>, <code>(|||)</code> and <code>first</code>,
<hask>second</hask>, <hask>(***)</hask>, <hask>(&&&)</hask>, respectively. Indeed, they are dual:
+
<code>second</code>, <code>(***)</code>, <code>(&&&)</code>, respectively. Indeed, they are dual:
<hask>first</hask>, <hask>second</hask>, <hask>(***)</hask>, and <hask>(&&&)</hask> all operate on product types
+
<code>first</code>, <code>second</code>, <code>(***)</code>, and <code>(&&&)</code> all operate on product types
(tuples), and <hask>left</hask>, <hask>right</hask>, <hask>(+++)</hask>, and <hask>(|||)</hask> are the
+
(tuples), and <code>left</code>, <code>right</code>, <code>(+++)</code>, and <code>(|||)</code> are the
 
corresponding operations on sum types. In general, these operations
 
corresponding operations on sum types. In general, these operations
create arrows whose inputs are tagged with <hask>Left</hask> or <hask>Right</hask>, and can
+
create arrows whose inputs are tagged with <code>Left</code> or <code>Right</code>, and can
 
choose how to act based on these tags.
 
choose how to act based on these tags.
   
* If <hask>g</hask> is an arrow from <hask>b</hask> to <hask>c</hask>, then <hask>left g</hask> is an arrow from <hask>Either b d</hask> to <hask>Either c d</hask>. On inputs tagged with <hask>Left</hask>, the <hask>left g</hask> arrow has the behavior of <hask>g</hask>; on inputs tagged with <hask>Right</hask>, it behaves as the identity.
+
* If <code>g</code> is an arrow from <code>b</code> to <code>c</code>, then <code>left g</code> is an arrow from <code>Either b d</code> to <code>Either c d</code>. On inputs tagged with <code>Left</code>, the <code>left g</code> arrow has the behavior of <code>g</code>; on inputs tagged with <code>Right</code>, it behaves as the identity.
   
* The <hask>right</hask> function, of course, is the mirror image of <hask>left</hask>. The arrow <hask>right g</hask> has the behavior of <hask>g</hask> on inputs tagged with <hask>Right</hask>.
+
* The <code>right</code> function, of course, is the mirror image of <code>left</code>. The arrow <code>right g</code> has the behavior of <code>g</code> on inputs tagged with <code>Right</code>.
   
* The <hask>(+++)</hask> operator performs “multiplexing”: <hask>g +++ h</hask> behaves as <hask>g</hask> on inputs tagged with <hask>Left</hask>, and as <hask>h</hask> on inputs tagged with <hask>Right</hask>. The tags are preserved. The <hask>(+++)</hask> operator is the ''sum'' (hence <hask>+</hask>) of two arrows, just as <hask>(***)</hask> is the product.
+
* The <code>(+++)</code> operator performs “multiplexing”: <code>g +++ h</code> behaves as <code>g</code> on inputs tagged with <code>Left</code>, and as <code>h</code> on inputs tagged with <code>Right</code>. The tags are preserved. The <code>(+++)</code> operator is the ''sum'' (hence <code>+</code>) of two arrows, just as <code>(***)</code> is the product.
   
* The <hask>(|||)</hask> operator is “merge” or “fanin”: the arrow <hask>g ||| h</hask> behaves as <hask>g</hask> on inputs tagged with <hask>Left</hask>, and <hask>h</hask> on inputs tagged with <hask>Right</hask>, but the tags are discarded (hence, <hask>g</hask> and <hask>h</hask> must have the same output type). The mnemonic is that <hask>g ||| h</hask> performs either <hask>g</hask> ''or'' <hask>h</hask> on its input.
+
* The <code>(|||)</code> operator is “merge” or “fanin”: the arrow <code>g ||| h</code> behaves as <code>g</code> on inputs tagged with <code>Left</code>, and <code>h</code> on inputs tagged with <code>Right</code>, but the tags are discarded (hence, <code>g</code> and <code>h</code> must have the same output type). The mnemonic is that <code>g ||| h</code> performs either <code>g</code> ''or'' <code>h</code> on its input.
   
The <hask>ArrowChoice</hask> class allows computations to choose among a finite number of execution paths, based on intermediate results. The possible
+
The <code>ArrowChoice</code> class allows computations to choose among a finite number of execution paths, based on intermediate results. The possible
execution paths must be known in advance, and explicitly assembled with <hask>(+++)</hask> or <hask>(|||)</hask>. However, sometimes more flexibility is
+
execution paths must be known in advance, and explicitly assembled with <code>(+++)</code> or <code>(|||)</code>. However, sometimes more flexibility is
needed: we would like to be able to ''compute'' an arrow from intermediate results, and use this computed arrow to continue the computation. This is the power given to us by <hask>ArrowApply</hask>.
+
needed: we would like to be able to ''compute'' an arrow from intermediate results, and use this computed arrow to continue the computation. This is the power given to us by <code>ArrowApply</code>.
   
 
==ArrowApply==
 
==ArrowApply==
   
The <hask>ArrowApply</hask> type class is:
+
The <code>ArrowApply</code> type class is:
   
 
<haskell>
 
<haskell>
Line 1,235: Line 1,235:
   
 
If we have computed an arrow as the output of some previous
 
If we have computed an arrow as the output of some previous
computation, then <hask>app</hask> allows us to apply that arrow to an input,
+
computation, then <code>app</code> allows us to apply that arrow to an input,
producing its output as the output of <hask>app</hask>. As an exercise, the
+
producing its output as the output of <code>app</code>. As an exercise, the
reader may wish to use <hask>app</hask> to implement an alternative “curried”
+
reader may wish to use <code>app</code> to implement an alternative “curried”
version, <hask>app2 :: b ~> ((b ~> c) ~> c)</hask>.
+
version, <code>app2 :: b ~> ((b ~> c) ~> c)</code>.
   
 
This notion of being able to ''compute'' a new computation
 
This notion of being able to ''compute'' a new computation
 
may sound familiar:
 
may sound familiar:
this is exactly what the monadic bind operator <hask>(>>=)</hask> does. It
+
this is exactly what the monadic bind operator <code>(>>=)</code> does. It
should not particularly come as a surprise that <hask>ArrowApply</hask> and
+
should not particularly come as a surprise that <code>ArrowApply</code> and
<hask>Monad</hask> are exactly equivalent in expressive power. In particular,
+
<code>Monad</code> are exactly equivalent in expressive power. In particular,
<hask>Kleisli m</hask> can be made an instance of <hask>ArrowApply</hask>, and any instance
+
<code>Kleisli m</code> can be made an instance of <code>ArrowApply</code>, and any instance
of <hask>ArrowApply</hask> can be made a <hask>Monad</hask> (via the <hask>newtype</hask> wrapper
+
of <code>ArrowApply</code> can be made a <code>Monad</code> (via the <code>newtype</code> wrapper
<hask>ArrowMonad</hask>). As an exercise, the reader may wish to try
+
<code>ArrowMonad</code>). As an exercise, the reader may wish to try
 
implementing these instances:
 
implementing these instances:
   
Line 1,263: Line 1,263:
 
==ArrowLoop==
 
==ArrowLoop==
   
The <hask>ArrowLoop</hask> type class is:
+
The <code>ArrowLoop</code> type class is:
   
 
<haskell>
 
<haskell>
Line 1,274: Line 1,274:
   
 
It describes arrows that can use recursion to compute results, and is
 
It describes arrows that can use recursion to compute results, and is
used to desugar the <hask>rec</hask> construct in arrow notation (described
+
used to desugar the <code>rec</code> construct in arrow notation (described
 
below).
 
below).
   
Taken by itself, the type of the <hask>loop</hask> method does not seem to tell
+
Taken by itself, the type of the <code>loop</code> method does not seem to tell
us much. Its intention, however, is a generalization of the <hask>trace</hask>
+
us much. Its intention, however, is a generalization of the <code>trace</code>
function which is also shown. The <hask>d</hask> component of the first arrow’s
+
function which is also shown. The <code>d</code> component of the first arrow’s
 
output is fed back in as its own input. In other words, the arrow
 
output is fed back in as its own input. In other words, the arrow
<hask>loop g</hask> is obtained by recursively “fixing” the second component of
+
<code>loop g</code> is obtained by recursively “fixing” the second component of
the input to <hask>g</hask>.
+
the input to <code>g</code>.
   
It can be a bit difficult to grok what the <hask>trace</hask> function is doing.
+
It can be a bit difficult to grok what the <code>trace</code> function is doing.
How can <hask>d</hask> appear on the left and right sides of the <hask>let</hask>? Well,
+
How can <code>d</code> appear on the left and right sides of the <code>let</code>? Well,
 
this is Haskell’s laziness at work. There is not space here for a
 
this is Haskell’s laziness at work. There is not space here for a
 
full explanation; the interested reader is encouraged to study the
 
full explanation; the interested reader is encouraged to study the
standard <hask>fix</hask> function, and to read [http://www.soi.city.ac.uk/~ross/papers/fop.html Paterson’s arrow tutorial].
+
standard <code>fix</code> function, and to read [http://www.soi.city.ac.uk/~ross/papers/fop.html Paterson’s arrow tutorial].
   
 
==Arrow notation==
 
==Arrow notation==
Line 1,300: Line 1,300:
 
reassociate, and generally mangle tuples as necessary. This problem
 
reassociate, and generally mangle tuples as necessary. This problem
 
is solved by the special arrow notation supported by GHC, similar to
 
is solved by the special arrow notation supported by GHC, similar to
<hask>do</hask> notation for monads, that allows names to be assigned to
+
<code>do</code> notation for monads, that allows names to be assigned to
 
intermediate results while building up arrow computations. An example
 
intermediate results while building up arrow computations. An example
 
arrow implemented using arrow notation, taken from
 
arrow implemented using arrow notation, taken from
Line 1,333: Line 1,333:
 
audience: [http://www.soi.city.ac.uk/~ross/papers/fop.html Paterson: Programming with Arrows] and [http://www.cse.chalmers.se/~rjmh/afp-arrows.pdf Hughes: Programming with Arrows].
 
audience: [http://www.soi.city.ac.uk/~ross/papers/fop.html Paterson: Programming with Arrows] and [http://www.cse.chalmers.se/~rjmh/afp-arrows.pdf Hughes: Programming with Arrows].
   
Although Hughes’s goal in defining the <hask>Arrow</hask> class was to
+
Although Hughes’s goal in defining the <code>Arrow</code> class was to
generalize <hask>Monad</hask>s, and it has been said that <hask>Arrow</hask> lies “between
+
generalize <code>Monad</code>s, and it has been said that <code>Arrow</code> lies “between
<hask>Applicative</hask> and <hask>Monad</hask>” in power, they are not directly
+
<code>Applicative</code> and <code>Monad</code>” in power, they are not directly
 
comparable. The precise relationship remained in some confusion until
 
comparable. The precise relationship remained in some confusion until
 
[http://homepages.inf.ed.ac.uk/wadler/papers/arrows-and-idioms/arrows-and-idioms.pdf analyzed by Lindley, Wadler, and Yallop], who
 
[http://homepages.inf.ed.ac.uk/wadler/papers/arrows-and-idioms/arrows-and-idioms.pdf analyzed by Lindley, Wadler, and Yallop], who
Line 1,342: Line 1,342:
 
(see [http://homepages.inf.ed.ac.uk/wadler/papers/arrows/arrows.pdf The arrow calculus]).
 
(see [http://homepages.inf.ed.ac.uk/wadler/papers/arrows/arrows.pdf The arrow calculus]).
   
Some examples of <hask>Arrow</hask>s include [http://www.haskell.org/yampa/ Yampa], the
+
Some examples of <code>Arrow</code>s include [http://www.haskell.org/yampa/ Yampa], the
 
[http://www.fh-wedel.de/~si/HXmlToolbox/ Haskell XML Toolkit], and the functional GUI library [[Grapefruit]].
 
[http://www.fh-wedel.de/~si/HXmlToolbox/ Haskell XML Toolkit], and the functional GUI library [[Grapefruit]].
   
 
Some extensions to arrows have been explored; for example, the
 
Some extensions to arrows have been explored; for example, the
[http://www.cs.ru.nl/A.vanWeelden/bi-arrows/ <hask>BiArrow</hask>s of Alimarine et al.], for two-way instead of one-way
+
[http://www.cs.ru.nl/A.vanWeelden/bi-arrows/ <code>BiArrow</code>s of Alimarine et al.], for two-way instead of one-way
 
computation.
 
computation.
   
The Haskell wiki has [[Research papers/Monads and Arrows|links to many additional research papers relating to <hask>Arrow</hask>s]].
+
The Haskell wiki has [[Research papers/Monads and Arrows|links to many additional research papers relating to <code>Arrow</code>s]].
   
 
=Comonad=
 
=Comonad=
   
The final type class we will examine is <hask>Comonad</hask>. The <hask>Comonad</hask> class
+
The final type class we will examine is <code>Comonad</code>. The <code>Comonad</code> class
is the categorical dual of <hask>Monad</hask>; that is, <hask>Comonad</hask> is like <hask>Monad</hask>
+
is the categorical dual of <code>Monad</code>; that is, <code>Comonad</code> is like <code>Monad</code>
 
but with all the function arrows flipped. It is not actually in the
 
but with all the function arrows flipped. It is not actually in the
 
standard Haskell libraries, but it has seen some interesting uses
 
standard Haskell libraries, but it has seen some interesting uses
Line 1,361: Line 1,361:
 
==Definition==
 
==Definition==
   
The <hask>Comonad</hask> type class, defined in the <hask>Control.Comonad</hask> module of
+
The <code>Comonad</code> type class, defined in the <code>Control.Comonad</code> module of
 
the [http://hackage.haskell.org/package/category-extras category-extras library], is:
 
the [http://hackage.haskell.org/package/category-extras category-extras library], is:
   
Line 1,373: Line 1,373:
 
</haskell>
 
</haskell>
   
As you can see, <hask>extract</hask> is the dual of <hask>return</hask>, <hask>duplicate</hask> is the
+
As you can see, <code>extract</code> is the dual of <code>return</code>, <code>duplicate</code> is the
dual of <hask>join</hask>, and <hask>extend</hask> is the dual of <hask>(>>=)</hask> (although its
+
dual of <code>join</code>, and <code>extend</code> is the dual of <code>(>>=)</code> (although its
 
arguments are in a different order). The definition
 
arguments are in a different order). The definition
of <hask>Comonad</hask> is a bit redundant (after all, the <hask>Monad</hask> class does not
+
of <code>Comonad</code> is a bit redundant (after all, the <code>Monad</code> class does not
need <hask>join</hask>), but this is so that a <hask>Comonad</hask> can be defined by <hask>fmap</hask>,
+
need <code>join</code>), but this is so that a <code>Comonad</code> can be defined by <code>fmap</code>,
<hask>extract</hask>, and ''either'' <hask>duplicate</hask> or <hask>extend</hask>. Each has a
+
<code>extract</code>, and ''either'' <code>duplicate</code> or <code>extend</code>. Each has a
 
default implementation in terms of the other.
 
default implementation in terms of the other.
   
A prototypical example of a <hask>Comonad</hask> instance is:
+
A prototypical example of a <code>Comonad</code> instance is:
   
 
<haskell>
 
<haskell>
Line 1,463: Line 1,463:
 
* <nowiki>%s/---/—/gc</nowiki>
 
* <nowiki>%s/---/—/gc</nowiki>
 
* <nowiki>%s/\$\([^$]*\)\$/<math>\1\\ <\/math>/gc</nowiki> ''Appending “\ ” forces images to be rendered. Otherwise, Mediawiki would go back and forth between one font for short <nowiki><math></nowiki> tags, and another more Tex-like font for longer tags (containing more than a few characters)""
 
* <nowiki>%s/\$\([^$]*\)\$/<math>\1\\ <\/math>/gc</nowiki> ''Appending “\ ” forces images to be rendered. Otherwise, Mediawiki would go back and forth between one font for short <nowiki><math></nowiki> tags, and another more Tex-like font for longer tags (containing more than a few characters)""
* <nowiki>%s/|\([^|]*\)|/<hask>\1<\/hask>/gc</nowiki>
+
* <nowiki>%s/|\([^|]*\)|/<code>\1<\/code>/gc</nowiki>
 
* <nowiki>%s/\\dots/.../gc</nowiki>
 
* <nowiki>%s/\\dots/.../gc</nowiki>
 
* <nowiki>%s/^\\label{.*$//gc</nowiki>
 
* <nowiki>%s/^\\label{.*$//gc</nowiki>

Revision as of 01:49, 27 November 2011

By Brent Yorgey, byorgey@cis.upenn.edu

Originally published 12 March 2009 in issue 13 of the Monad.Reader. Ported to the Haskell wiki in November 2011 by Geheimdienst.

This is now the official version of the Typeclassopedia and supersedes the version published in the Monad.Reader. Please help update and extend it by editing it yourself or by leaving comments, suggestions, and questions on the talk page.

Abstract

The standard Haskell libraries feature a number of type classes with algebraic or category-theoretic underpinnings. Becoming a fluent Haskell hacker requires intimate familiarity with them all, yet acquiring this familiarity often involves combing through a mountain of tutorials, blog posts, mailing list archives, and IRC logs.

The goal of this document is to serve as a starting point for the student of Haskell wishing to gain a firm grasp of its standard type classes. The essentials of each type class are introduced, with examples, commentary, and extensive references for further reading.

Introduction

Have you ever had any of the following thoughts?

  • What the heck is a monoid, and how is it different from a monad?
  • I finally figured out how to use Parsec with do-notation, and someone told me I should use something called Applicative instead. Um, what?
  • Someone in the #haskell IRC channel used (***), and when I asked lambdabot to tell me its type, it printed out scary gobbledygook that didn’t even fit on one line! Then someone used fmap fmap fmap and my brain exploded.
  • When I asked how to do something I thought was really complicated, people started typing things like zip.ap fmap.(id &&& wtf) and the scary thing is that they worked! Anyway, I think those people must actually be robots because there’s no way anyone could come up with that in two seconds off the top of their head.

If you have, look no further! You, too, can write and understand concise, elegant, idiomatic Haskell code with the best of them.

There are two keys to an expert Haskell hacker’s wisdom:

  1. Understand the types.
  2. Gain a deep intuition for each type class and its relationship to other type classes, backed up by familiarity with many examples.

It’s impossible to overstate the importance of the first; the patient student of type signatures will uncover many profound secrets. Conversely, anyone ignorant of the types in their code is doomed to eternal uncertainty. “Hmm, it doesn’t compile ... maybe I’ll stick in an fmap here ... nope, let’s see ... maybe I need another (.) somewhere? ... um ...”

The second key—gaining deep intuition, backed by examples—is also important, but much more difficult to attain. A primary goal of this document is to set you on the road to gaining such intuition. However—

There is no royal road to Haskell. —Euclid

This document can only be a starting point, since good intuition comes from hard work, not from learning the right metaphor. Anyone who reads and understands all of it will still have an arduous journey ahead—but sometimes a good starting point makes a big difference.

It should be noted that this is not a Haskell tutorial; it is assumed that the reader is already familiar with the basics of Haskell, including the standard Prelude, the type system, data types, and type classes.

The type classes we will be discussing and their interrelationships:

Typeclassopedia-diagram.png

∗ When Typeclassopedia was originally written, Pointed and Comonad were in the category-extras library. It has since been deprecated and they have moved to the pointed package and the comonad package. —Geheimdienst, Nov 2011

  • Solid arrows point from the general to the specific; that is, if there is an arrow from Foo to Bar it means that every Bar is (or should be, or can be made into) a Foo.
  • Dotted arrows indicate some other sort of relationship.
  • Monad and ArrowApply are equivalent.
  • Pointed and Comonad are greyed out since they are not actually (yet) in the standard Haskell libraries .

One more note before we begin. The original spelling of “type class” is with two words, as evidenced by, for example, the Haskell 98 Revised Report, early papers on type classes like Type classes in Haskell and Type classes: exploring the design space, and Hudak et al.’s history of Haskell. However, as often happens with two-word phrases that see a lot of use, it has started to show up as one word (“typeclass”) or, rarely, hyphenated (“type-class”). When wearing my prescriptivist hat, I prefer “type class”, but realize (after changing into my descriptivist hat) that there's probably not much I can do about it.

We now begin with the simplest type class of all: Functor.

Functor

The Functor class (haddock) is the most basic and ubiquitous type class in the Haskell libraries. A simple intuition is that a Functor represents a “container” of some sort, along with the ability to apply a function uniformly to every element in the container. For example, a list is a container of elements, and we can apply a function to every element of a list, using map. As another example, a binary tree is also a container of elements, and it’s not hard to come up with a way to recursively apply a function to every element in a tree.

Another intuition is that a Functor represents some sort of “computational context”. This intuition is generally more useful, but is more difficult to explain, precisely because it is so general. Some examples later should help to clarify the Functor-as-context point of view.

In the end, however, a Functor is simply what it is defined to be; doubtless there are many examples of Functor instances that don’t exactly fit either of the above intuitions. The wise student will focus their attention on definitions and examples, without leaning too heavily on any particular metaphor. Intuition will come, in time, on its own.

Definition

Here is the type class declaration for Functor:

class Functor f where
  fmap :: (a -> b) -> f a -> f b

Functor is exported by the Prelude, so no special imports are needed to use it.

First, the f a and f b in the type signature for fmap tell us that f isn’t just a type; it is a type constructor which takes another type as a parameter. (A more precise way to say this is that the kind of f must be * -> *.) For example, Maybe is such a type constructor: Maybe is not a type in and of itself, but requires another type as a parameter, like Maybe Integer. So it would not make sense to say instance Functor Integer, but it could make sense to say instance Functor Maybe.

Now look at the type of fmap: it takes any function from a to b, and a value of type f a, and outputs a value of type f b. From the container point of view, the intention is that fmap applies a function to each element of a container, without altering the structure of the container. From the context point of view, the intention is that fmap applies a function to a value without altering its context. Let’s look at a few specific examples.

Instances

∗ Recall that [] has two meanings in Haskell: it can either stand for the empty list, or, as here, it can represent the list type constructor (pronounced “list-of”). In other words, the type [a] (list-of-a) can also be written [] a.

∗ You might ask why we need a separate map function. Why not just do away with the current list-only map function, and rename fmap to map instead? Well, that’s a good question. The usual argument is that someone just learning Haskell, when using map incorrectly, would much rather see an error about lists than about Functors.

As noted before, the list constructor [] is a functor ; we can use the standard list function map to apply a function to each element of a list . The Maybe type constructor is also a functor, representing a container which might hold a single element. The function fmap g has no effect on Nothing (there are no elements to which g can be applied), and simply applies g to the single element inside a Just. Alternatively, under the context interpretation, the list functor represents a context of nondeterministic choice; that is, a list can be thought of as representing a single value which is nondeterministically chosen from among several possibilities (the elements of the list). Likewise, the Maybe functor represents a context with possible failure. These instances are:

instance Functor [] where
  fmap _ []     = []
  fmap g (x:xs) = g x : fmap g xs
  -- or we could just say fmap = map

instance Functor Maybe where
  fmap _ Nothing  = Nothing
  fmap g (Just a) = Just (g a)

As an aside, in idiomatic Haskell code you will often see the letter f used to stand for both an arbitrary Functor and an arbitrary function. In this document, f represents only Functors, and g or h always represent functions, but you should be aware of the potential confusion. In practice, what f stands for should always be clear from the context, by noting whether it is part of a type or part of the code.

There are other Functor instances in the standard libraries; below are a few. Note that some of these instances are not exported by the Prelude; to access them, you can import Control.Monad.Instances.

  • Either e is an instance of Functor; Either e a represents a container which can contain either a value of type a, or a value of type e (often representing some sort of error condition). It is similar to Maybe in that it represents possible failure, but it can carry some extra information about the failure as well.
  • ((,) e) represents a container which holds an “annotation” of type e along with the actual value it holds. It might be clearer to write it as (e,), by analogy with an operator section like (1+), but that syntax is not allowed in types (although it is allowed in expressions with the TupleSections extension enabled). However, you can certainly think of it as (e,).
  • ((->) e) (which can be thought of as (e ->); see above), the type of functions which take a value of type e as a parameter, is a Functor. As a container, (e -> a) represents a (possibly infinite) set of values of a, indexed by values of e. Alternatively, and more usefully, ((->) e) can be thought of as a context in which a value of type e is available to be consulted in a read-only fashion. This is also why ((->) e) is sometimes referred to as the reader monad; more on this later.
  • IO is a Functor; a value of type IO a represents a computation producing a value of type a which may have I/O effects. If m computes the value x while producing some I/O effects, then fmap g m will compute the value g x while producing the same I/O effects.
  • Many standard types from the containers library (such as Tree, Map, and Sequence) are instances of Functor. A notable exception is Set, which cannot be made a Functor in Haskell (although it is certainly a mathematical functor) since it requires an Ord constraint on its elements; fmap must be applicable to any types a and b. However, Set (and other similarly restricted data types) can be made an instance of a suitable generalization of Functor, either by making a and b arguments to the Functor type class themselves, or by adding an associated constraint.
Exercises
  1. Implement Functor instances for Either e and ((->) e).
  2. Implement Functor instances for ((,) e) and for Pair, defined as
    data Pair a = Pair a a
    

    Explain their similarities and differences.

  3. Give an example of a type which cannot be made an instance of Functor (without using undefined).

Laws

As far as the Haskell language itself is concerned, the only requirement to be a Functor is an implementation of fmap with the proper type. Any sensible Functor instance, however, will also satisfy the functor laws, which are part of the definition of a mathematical functor. There are two:

fmap id = id
fmap (g . h) = (fmap g) . (fmap h)

∗ Technically, these laws make f and fmap together an endofunctor on Hask, the category of Haskell types (ignoring , which is a party pooper). See Wikibook: Category theory.

Together, these laws ensure that fmap g does not change the structure of a container, only the elements. Equivalently, and more simply, they ensure that fmap g changes a value without altering its context .

The first law says that mapping the identity function over every item in a container has no effect. The second says that mapping a composition of two functions over every item in a container is the same as first mapping one function, and then mapping the other.

As an example, the following code is a “valid” instance of Functor (it typechecks), but it violates the functor laws. Do you see why?

-- Evil Functor instance
instance Functor [] where
  fmap _ [] = []
  fmap g (x:xs) = g x : g x : fmap g xs

Any Haskeller worth their salt would reject this code as a gruesome abomination.

Unlike some other type classes we will encounter, a given type has at most one valid instance of Functor. This can be proven via the free theorem for the type of fmap. In fact, the same argument shows that any Functor instance satisfying the first law (fmap id = id) will automatically satisfy the second law as well. Practically, this means that only the first law needs to be checked (usually by a very straightforward induction) to ensure that a Functor instance is valid.

Exercises
  1. Although it is not possible for a Functor instance to satisfy the first Functor law but not the second, the reverse is possible. Give an example of a (bogus) Functor instance which satisfies the second law but not the first.

Intuition

There are two fundamental ways to think about fmap. The first has already been mentioned: it takes two parameters, a function and a container, and applies the function “inside” the container, producing a new container. Alternately, we can think of fmap as applying a function to a value in a context (without altering the context).

Just like all other Haskell functions of “more than one parameter”, however, fmap is actually curried: it does not really take two parameters, but takes a single parameter and returns a function. For emphasis, we can write fmap’s type with extra parentheses: fmap :: (a -> b) -> (f a -> f b). Written in this form, it is apparent that fmap transforms a “normal” function (g :: a -> b) into one which operates over containers/contexts (fmap g :: f a -> f b). This transformation is often referred to as a lift; fmap “lifts” a function from the “normal world” into the “f world”.

Further reading

A good starting point for reading about the category theory behind the concept of a functor is the excellent Haskell wikibook page on category theory.

Pointed

∗ The Pointed type class lives in the pointed library, moved from the category-extras library. The point function was originally named pure.

Edward Kmett, the author of category-extras, pointed, and many related packages, has since moved his focus to semigroupoids and semigroups. He finds them more interesting and useful, and considers Pointed to be historical now (he still provides the pointed package only because “people were whinging”). Nevertheless, Pointed has kept its value for explaining, and its place in Typeclassopedia. —Geheimdienst, Nov 2011

The Pointed type class represents pointed functors. It is not actually a type class in the standard libraries . But it could be, and it’s useful in understanding a few other type classes, notably Applicative and Monad, so let’s pretend for a minute.

Given a Functor, the Pointed class represents the additional ability to put a value into a “default context”. Often, this corresponds to creating a container with exactly one element, but it is more general than that. The type class declaration for Pointed is:

class Functor f => Pointed f where
  point :: a -> f a     -- aka pure, singleton, return, unit

Most of the standard Functor instances could also be instances of Pointed—for example, the Maybe instance of Pointed is point = Just; there are many possible implementations for lists, the most natural of which is point x = [x]; for ((->) e) it is ... well, I’ll let you work it out. (Just follow the types!)

One example of a Functor which is not Pointed is ((,) e). If you try implementing point :: a -> (e,a) you will quickly see why: since the type e is completely arbitrary, there is no way to generate a value of type e out of thin air! However, as we will see, ((,) e) can be made Pointed if we place an additional restriction on e which allows us to generate a default value of type e (the most common solution is to make e an instance of Monoid).

∗ For those interested in category theory, this law states precisely that point is a natural transformation from the identity functor to f. The Pointed class has only one law :

fmap g . point  =  point . g

∗ ... modulo ⊥, seq, and assuming a lawful Functor instance.

However, you need not worry about it: this law is actually a so-called “free theorem” guaranteed by parametricity (see Wadler’s Theorems for free!); it’s impossible to write an instance of Pointed which does not satisfy it .

Applicative

A somewhat newer addition to the pantheon of standard Haskell type classes, applicative functors represent an abstraction lying exactly in between Functor and Monad, first described by McBride and Paterson. The title of their classic paper, Applicative Programming with Effects, gives a hint at the intended intuition behind the Applicative type class. It encapsulates certain sorts of “effectful” computations in a functionally pure way, and encourages an “applicative” programming style. Exactly what these things mean will be seen later.

Definition

The Applicative class adds a single capability to Pointed functors. Recall that Functor allows us to lift a “normal” function to a function on computational contexts. But fmap doesn’t allow us to apply a function which is itself in a context to a value in another context. Applicative gives us just such a tool. Here is the type class declaration for Applicative, as defined in Control.Applicative:

class Functor f => Applicative f where
  pure  :: a -> f a     -- aka point
  (<*>) :: f (a -> b) -> f a -> f b

Note that every Applicative must also be a Functor. In fact, as we will see, fmap can be implemented using the Applicative methods, so every Applicative is a functor whether we like it or not; the Functor constraint forces us to be honest.

∗ Recall that ($) is just function application: f $ x = f x.

As always, it’s crucial to understand the type signature of (<*>). The best way of thinking about it comes from noting that the type of (<*>) is similar to the type of ($) , but with everything enclosed in an f. In other words, (<*>) is just function application within a computational context. The type of (<*>) is also very similar to the type of fmap; the only difference is that the first parameter is f (a -> b), a function in a context, instead of a “normal” function (a -> b).

Of course, pure looks rather familiar. It is the point function from the Pointed type class. If we actually had it in the standard library, and pure appearing under the other name didn’t bother you, then Applicative could instead be defined as:

class Pointed f => Applicative' f where
  (<*>) :: f (a -> b) -> f a -> f b

Laws

∗ See haddock for Applicative and Applicative programming with effects

There are several laws that Applicative instances should satisfy , but only one is crucial to developing intuition, because it specifies how Applicative should relate to Functor (the other four mostly specify the exact sense in which pure deserves its name). This law is:

fmap g x = pure g <*> x

It says that mapping a pure function g over a context x is the same as first injecting g into a context with pure, and then applying it to x with (<*>). In other words, we can decompose fmap into two more atomic operations: injection into a context, and application within a context. The Control.Applicative module also defines (<$>) as a synonym for fmap, so the above law can also be expressed as:

g <$> x = pure g <*> x.

Instances

Most of the standard types which are instances of Functor are also instances of Applicative.

Maybe can easily be made an instance of Applicative; writing such an instance is left as an exercise for the reader.

The list type constructor [] can actually be made an instance of Applicative in two ways; essentially, it comes down to whether we want to think of lists as ordered collections of elements, or as contexts representing multiple results of a nondeterministic computation (see Wadler’s How to replace failure by a list of successes).

Let’s first consider the collection point of view. Since there can only be one instance of a given type class for any particular type, one or both of the list instances of Applicative need to be defined for a newtype wrapper; as it happens, the nondeterministic computation instance is the default, and the collection instance is defined in terms of a newtype called ZipList. This instance is:

newtype ZipList a = ZipList { getZipList :: [a] }

instance Applicative ZipList where
  pure = undefined   -- exercise
  (ZipList gs) <*> (ZipList xs) = ZipList (zipWith ($) gs xs)

To apply a list of functions to a list of inputs with (<*>), we just match up the functions and inputs elementwise, and produce a list of the resulting outputs. In other words, we “zip” the lists together with function application, ($); hence the name ZipList. As an exercise, determine the correct definition of pure—there is only one implementation that satisfies the law (see section “Laws”).

The other Applicative instance for lists, based on the nondeterministic computation point of view, is:

instance Applicative [] where
  pure x = [x]
  gs <*> xs = [ g x | g <- gs, x <- xs ]

Instead of applying functions to inputs pairwise, we apply each function to all the inputs in turn, and collect all the results in a list.

Now we can write nondeterministic computations in a natural style. To add the numbers 3 and 4 deterministically, we can of course write (+) 3 4. But suppose instead of 3 we have a nondeterministic computation that might result in 2, 3, or 4; then we can write

  pure (+) <*> [2,3,4] <*> pure 4

or, more idiomatically,

  (+) <$> [2,3,4] <*> pure 4.

There are several other Applicative instances as well:

  • IO is an instance of Applicative, and behaves exactly as you would think: when g <$> m1 <*> m2 <*> m3 is executed, the effects from the mi’s happen in order from left to right.
  • ((,) a) is an Applicative, as long as a is an instance of Monoid (section Monoid). The a values are accumulated in parallel with the computation.
  • The Applicative module defines the Const type constructor; a value of type Const a b simply contains an a. This is an instance of Applicative for any Monoid a; this instance becomes especially useful in conjunction with things like Foldable (section Foldable).
  • The WrappedMonad and WrappedArrow newtypes make any instances of Monad (section Monad) or Arrow (section Arrow) respectively into instances of Applicative; as we will see when we study those type classes, both are strictly more expressive than Applicative, in the sense that the Applicative methods can be implemented in terms of their methods.

Intuition

McBride and Paterson’s paper introduces the notation [[g \; x_1 \; x_2 \; \cdots \; x_n]]\ to denote function application in a computational context. If each x_i\ has type f \; t_i\ for some applicative functor f\ , and g\ has type t_1 \to t_2 \to \dots \to t_n \to t\ , then the entire expression [[g \; x_1 \; \cdots \; x_n]]\ has type f \; t\ . You can think of this as applying a function to multiple “effectful” arguments. In this sense, the double bracket notation is a generalization of fmap, which allows us to apply a function to a single argument in a context.

Why do we need Applicative to implement this generalization of fmap? Suppose we use fmap to apply g to the first parameter x1. Then we get something of type f (t2 -> ... t), but now we are stuck: we can’t apply this function-in-a-context to the next argument with fmap. However, this is precisely what (<*>) allows us to do.

This suggests the proper translation of the idealized notation [[g \; x_1 \; x_2 \; \cdots \; x_n]]\ into Haskell, namely

  g <$> x1 <*> x2 <*> ... <*> xn,

recalling that Control.Applicative defines (<$>) as convenient infix shorthand for fmap. This is what is meant by an “applicative style”—effectful computations can still be described in terms of function application; the only difference is that we have to use the special operator (<*>) for application instead of simple juxtaposition.

Further reading

There are many other useful combinators in the standard libraries implemented in terms of pure and (<*>): for example, (*>), (<*), (<**>), (<$), and so on (see haddock for Applicative). Judicious use of such secondary combinators can often make code using Applicatives much easier to read.

McBride and Paterson’s original paper is a treasure-trove of information and examples, as well as some perspectives on the connection between Applicative and category theory. Beginners will find it difficult to make it through the entire paper, but it is extremely well-motivated—even beginners will be able to glean something from reading as far as they are able.

∗ Introduced by an earlier paper that was since superceded by Push-pull functional reactive programming. —Geheimdienst, Nov 2011

Conal Elliott has been one of the biggest proponents of Applicative. For example, the Pan library for functional images and the reactive library for functional reactive programming (FRP) make key use of it; his blog also contains many examples of Applicative in action. Building on the work of McBride and Paterson, Elliott also built the TypeCompose library, which embodies the observation (among others) that Applicative types are closed under composition; therefore, Applicative instances can often be automatically derived for complex types built out of simpler ones.

Although the Parsec parsing library (paper) was originally designed for use as a monad, in its most common use cases an Applicative instance can be used to great effect; Bryan O’Sullivan’s blog post is a good starting point. If the extra power provided by Monad isn’t needed, it’s usually a good idea to use Applicative instead.

A couple other nice examples of Applicative in action include the ConfigFile and HSQL libraries and the formlets library.

Monad

It’s a safe bet that if you’re reading this, you’ve heard of monads—although it’s quite possible you’ve never heard of Applicative before, or Arrow, or even Monoid. Why are monads such a big deal in Haskell? There are several reasons.

  • Haskell does, in fact, single out monads for special attention by making them the framework in which to construct I/O operations.
  • Haskell also singles out monads for special attention by providing a special syntactic sugar for monadic expressions: the do-notation.
  • Monad has been around longer than other abstract models of computation such as Applicative or Arrow.
  • The more monad tutorials there are, the harder people think monads must be, and the more new monad tutorials are written by people who think they finally “get” monads (the monad tutorial fallacy).

I will let you judge for yourself whether these are good reasons.

In the end, despite all the hoopla, Monad is just another type class. Let’s take a look at its definition.

Definition

The type class declaration for Monad is:

class Monad m where
  return :: a -> m a
  (>>=)  :: m a -> (a -> m b) -> m b
  (>>)   :: m a -> m b -> m b
  m >> n = m >>= \_ -> n

  fail   :: String -> m a

The Monad type class is exported by the Prelude, along with a few standard instances. However, many utility functions are found in Control.Monad, and there are also several instances (such as ((->) e)) defined in Control.Monad.Instances.

Let’s examine the methods in the Monad class one by one. The type of return should look familiar; it’s the same as pure. Indeed, return is pure, but with an unfortunate name. (Unfortunate, since someone coming from an imperative programming background might think that return is like the C or Java keyword of the same name, when in fact the similarities are minimal.) From a mathematical point of view, every monad is an applicative functor, but for historical reasons, the Monad type class declaration unfortunately does not require this.

We can see that (>>) is a specialized version of (>>=), with a default implementation given. It is only included in the type class declaration so that specific instances of Monad can override the default implementation of (>>) with a more efficient one, if desired. Also, note that although _ >> n = n would be a type-correct implementation of (>>), it would not correspond to the intended semantics: the intention is that m >> n ignores the result of m, but not its effects.

The fail function is an awful hack that has no place in the Monad class; more on this later.

The only really interesting thing to look at—and what makes Monad strictly more powerful than Applicative—is (>>=), which is often called bind. An alternative definition of Monad could look like:

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

We could spend a while talking about the intuition behind (>>=)—and we will. But first, let’s look at some examples.

Instances

Even if you don’t understand the intuition behind the Monad class, you can still create instances of it by just seeing where the types lead you. You may be surprised to find that this actually gets you a long way towards understanding the intuition; at the very least, it will give you some concrete examples to play with as you read more about the Monad class in general. The first few examples are from the standard Prelude; the remaining examples are from the monad transformer library (mtl).

  • The simplest possible instance of Monad is Identity, which is described in Dan Piponi’s highly recommended blog post on The Trivial Monad. Despite being “trivial”, it is a great introduction to the Monad type class, and contains some good exercises to get your brain working.
  • The next simplest instance of Monad is Maybe. We already know how to write return/pure for Maybe. So how do we write (>>=)? Well, let’s think about its type. Specializing for Maybe, we have
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b.
If the first argument to (>>=) is Just x, then we have something of type a (namely, x), to which we can apply the second argument—resulting in a Maybe b, which is exactly what we wanted. What if the first argument to (>>=) is Nothing? In that case, we don’t have anything to which we can apply the a -> Maybe b function, so there’s only one thing we can do: yield Nothing. This instance is:
instance Monad Maybe where
  return = Just
  (Just x) >>= g = g x
  Nothing  >>= _ = Nothing
We can already get a bit of intuition as to what is going on here: if we build up a computation by chaining together a bunch of functions with (>>=), as soon as any one of them fails, the entire computation will fail (because Nothing >>= f is Nothing, no matter what f is). The entire computation succeeds only if all the constituent functions individually succeed. So the Maybe monad models computations which may fail.
  • The Monad instance for the list constructor [] is similar to its Applicative instance; see the exercise below.
  • Of course, the IO constructor is famously a Monad, but its implementation is somewhat magical, and may in fact differ from compiler to compiler. It is worth emphasizing that the IO monad is the only monad which is magical. It allows us to build up, in an entirely pure way, values representing possibly effectful computations. The special value main, of type IO (), is taken by the runtime and actually executed, producing actual effects. Every other monad is functionally pure, and requires no special compiler support. We often speak of monadic values as “effectful computations”, but this is because some monads allow us to write code as if it has side effects, when in fact the monad is hiding the plumbing which allows these apparent side effects to be implemented in a functionally pure way.
  • As mentioned earlier, ((->) e) is known as the reader monad, since it describes computations in which a value of type e is available as a read-only environment.
The Control.Monad.Reader module provides the Reader e a type, which is just a convenient newtype wrapper around (e -> a), along with an appropriate Monad instance and some Reader-specific utility functions such as ask (retrieve the environment), asks (retrieve a function of the environment), and local (run a subcomputation under a different environment).
  • The Control.Monad.Writer module provides the Writer monad, which allows information to be collected as a computation progresses. Writer w a is isomorphic to (a,w), where the output value a is carried along with an annotation or “log” of type w, which must be an instance of Monoid (see section Monoid); the special function tell performs logging.
  • The Control.Monad.State module provides the State s a type, a newtype wrapper around s -> (a,s). Something of type State s a represents a stateful computation which produces an a but can access and modify the state of type s along the way. The module also provides State-specific utility functions such as get (read the current state), gets (read a function of the current state), put (overwrite the state), and modify (apply a function to the state).
  • The Control.Monad.Cont module provides the Cont monad, which represents computations in continuation-passing style. It can be used to suspend and resume computations, and to implement non-local transfers of control, co-routines, other complex control structures—all in a functionally pure way. Cont has been called the “mother of all monads” because of its universal properties.
Exercises
  1. Implement a Monad instance for the list constructor, []. Follow the types!
  2. Implement a Monad instance for ((->) e).

Intuition

Let’s look more closely at the type of (>>=). The basic intuition is that it combines two computations into one larger computation. The first argument, m a, is the first computation. However, it would be boring if the second argument were just an m b; then there would be no way for the computations to interact with one another. So, the second argument to (>>=) has type a -> m b: a function of this type, given a result of the first computation, can produce a second computation to be run. In other words, x >>= k is a computation which runs x, and then uses the result(s) of x to decide what computation to run second, using the output of the second computation as the result of the entire computation.

Intuitively, it is this ability to use the output from previous computations to decide what computations to run next that makes Monad more powerful than Applicative. The structure of an Applicative computation is fixed, whereas the structure of a Monad computation can change based on intermediate results.

To see the increased power of Monad from a different point of view, let’s see what happens if we try to implement (>>=) in terms of fmap, pure, and (<*>). We are given a value x of type m a, and a function k of type a -> m b, so the only thing we can do is apply k to x. We can’t apply it directly, of course; we have to use fmap to lift it over the m. But what is the type of fmap k? Well, it’s m a -> m (m b). So after we apply it to x, we are left with something of type m (m b)—but now we are stuck; what we really want is an m b, but there’s no way to get there from here. We can add m’s using pure, but we have no way to collapse multiple m’s into one.

This ability to collapse multiple m’s is exactly the ability provided by the function join :: m (m a) -> m a, and it should come as no surprise that an alternative definition of Monad can be given in terms of join:

class Applicative m => Monad'' m where
  join :: m (m a) -> m a

In fact, monads in category theory are defined in terms of return, fmap, and join (often called \eta, T, and \mu in the mathematical literature). Haskell uses the equivalent formulation in terms of (>>=) instead of join since it is more convenient to use; however, sometimes it can be easier to think about Monad instances in terms of join, since it is a more “atomic” operation. (For example, join for the list monad is just concat.)

Exercises
  1. Implement (>>=) in terms of fmap (or liftM) and join.
  2. Now implement join and fmap (liftM) in terms of (>>=) and return.

Utility functions

The Control.Monad module provides a large number of convenient utility functions, all of which can be implemented in terms of the basic Monad operations (return and (>>=) in particular). We have already seen one of them, namely, join. We also mention some other noteworthy ones here; implementing these utility functions oneself is a good exercise. For a more detailed guide to these functions, with commentary and example code, see Henk-Jan van Tuyl’s tour.

∗ Still, it is unclear how this "bug" should be fixed. Making Monad require a Functor instance has some drawbacks, as mentioned in this 2011 mailing-list discussion. —Geheimdienst

  • liftM :: Monad m => (a -> b) -> m a -> m b. This should be familiar; of course, it is just fmap. The fact that we have both fmap and liftM is an unfortunate consequence of the fact that the Monad type class does not require a Functor instance, even though mathematically speaking, every monad is a functor. However, fmap and liftM are essentially interchangeable, since it is a bug (in a social rather than technical sense) for any type to be an instance of Monad without also being an instance of Functor .
  • ap :: Monad m => m (a -> b) -> m a -> m b should also be familiar: it is equivalent to (<*>), justifying the claim that the Monad interface is strictly more powerful than Applicative. We can make any Monad into an instance of Applicative by setting pure = return and (<*>) = ap.
  • sequence :: Monad m => [m a] -> m [a] takes a list of computations and combines them into one computation which collects a list of their results. It is again something of a historical accident that sequence has a Monad constraint, since it can actually be implemented only in terms of Applicative. There is an additional generalization of sequence to structures other than lists, which will be discussed in the section on Traversable.
  • replicateM :: Monad m => Int -> m a -> m [a] is simply a combination of replicate and sequence.
  • when :: Monad m => Bool -> m () -> m () conditionally executes a computation, evaluating to its second argument if the test is True, and to return () if the test is False. A collection of other sorts of monadic conditionals can be found in the IfElse package.
  • mapM :: Monad m => (a -> m b) -> [a] -> m [b] maps its first argument over the second, and sequences the results. The forM function is just mapM with its arguments reversed; it is called forM since it models generalized for loops: the list [a] provides the loop indices, and the function a -> m b specifies the “body” of the loop for each index.
  • (=<<) :: Monad m => (a -> m b) -> m a -> m b is just (>>=) with its arguments reversed; sometimes this direction is more convenient since it corresponds more closely to function application.
  • (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c is sort of like function composition, but with an extra m on the result type of each function, and the arguments swapped. We’ll have more to say about this operation later. There is also a flipped variant, (<=<).
  • The guard function is for use with instances of MonadPlus, which is discussed at the end of the Monoid section.

Many of these functions also have “underscored” variants, such as sequence_ and mapM_; these variants throw away the results of the computations passed to them as arguments, using them only for their side effects.

Other monadic functions which are occasionally useful include filterM, zipWithM, foldM, and forever.

Laws

There are several laws that instances of Monad should satisfy (see also the Monad laws wiki page). The standard presentation is:

return a >>= k  =  k a
m >>= return    =  m
m >>= (\x -> k x >>= h)  =  (m >>= k) >>= h

fmap f xs  =  xs >>= return . f  =  liftM f xs

The first and second laws express the fact that return behaves nicely: if we inject a value a into a monadic context with return, and then bind to k, it is the same as just applying k to a in the first place; if we bind a computation m to return, nothing changes. The third law essentially says that (>>=) is associative, sort of. The last law ensures that fmap and liftM are the same for types which are instances of both Functor and Monad—which, as already noted, should be every instance of Monad.

∗ I like to pronounce this operator “fish”, but that’s probably not the canonical pronunciation ...

However, the presentation of the above laws, especially the third, is marred by the asymmetry of (>>=). It’s hard to look at the laws and see what they’re really saying. I prefer a much more elegant version of the laws, which is formulated in terms of (>=>) . Recall that (>=>) “composes” two functions of type a -> m b and b -> m c. You can think of something of type a -> m b (roughly) as a function from a to b which may also have some sort of effect in the context corresponding to m. (Note that return is such a function.) (>=>) lets us compose these “effectful functions”, and we would like to know what properties (>=>) has. The monad laws reformulated in terms of (>=>) are:

return >=> g  =  g
g >=> return  =  g
(g >=> h) >=> k  =  g >=> (h >=> k)

∗ As fans of category theory will note, these laws say precisely that functions of type a -> m b are the arrows of a category with (>=>) as composition! Indeed, this is known as the Kleisli category of the monad m. It will come up again when we discuss Arrows.

Ah, much better! The laws simply state that return is the identity of (>=>), and that (>=>) is associative . Working out the equivalence between these two formulations, given the definition g >=> h = \x -> g x >>= h, is left as an exercise.

There is also a formulation of the monad laws in terms of fmap, return, and join; for a discussion of this formulation, see the Haskell wikibook page on category theory.

do notation

Haskell’s special do notation supports an “imperative style” of programming by providing syntactic sugar for chains of monadic expressions. The genesis of the notation lies in realizing that something like a >>= \x -> b >> c >>= \y -> d can be more readably written by putting successive computations on separate lines:

a >>= \x ->
b >>
c >>= \y ->
d

This emphasizes that the overall computation consists of four computations a, b, c, and d, and that x is bound to the result of a, and y is bound to the result of c (b, c, and d are allowed to refer to x, and d is allowed to refer to y as well). From here it is not hard to imagine a nicer notation:

do { x <- a ;
     b      ;
     y <- c ;
     d
   }

(The curly braces and semicolons may optionally be omitted; the Haskell parser uses layout to determine where they should be inserted.) This discussion should make clear that do notation is just syntactic sugar. In fact, do blocks are recursively translated into monad operations (almost) like this:

                  do e → e
       do { e; stmts } → e >> do { stmts }
  do { v <- e; stmts } → e >>= \v -> do { stmts }
do { let decls; stmts} → let decls in do { stmts }

This is not quite the whole story, since v might be a pattern instead of a variable. For example, one can write

do (x:xs) <- foo
   bar x

but what happens if foo produces an empty list? Well, remember that ugly fail function in the Monad type class declaration? That’s what happens. See section 3.14 of the Haskell Report for the full details. See also the discussion of MonadPlus and MonadZero in the section on other monoidal classes.

A final note on intuition: do notation plays very strongly to the “computational context” point of view rather than the “container” point of view, since the binding notation x <- m is suggestive of “extracting” a single x from m and doing something with it. But m may represent some sort of a container, such as a list or a tree; the meaning of x <- m is entirely dependent on the implementation of (>>=). For example, if m is a list, x <- m actually means that x will take on each value from the list in turn.

Monad transformers

One would often like to be able to combine two monads into one: for example, to have stateful, nondeterministic computations (State + []), or computations which may fail and can consult a read-only environment (Maybe + Reader), and so on. Unfortunately, monads do not compose as nicely as applicative functors (yet another reason to use Applicative if you don’t need the full power that Monad provides), but some monads can be combined in certain ways.

The monad transformer library mtl provides a number of monad transformers, such as StateT, ReaderT, ErrorT (haddock), and (soon) MaybeT, which can be applied to other monads to produce a new monad with the effects of both. For example, StateT s Maybe is an instance of Monad; computations of type StateT s Maybe a may fail, and have access to a mutable state of type s. These transformers can be multiply stacked. One thing to keep in mind while using monad transformers is that the order of composition matters. For example, when a StateT s Maybe a computation fails, the state ceases being updated; on the other hand, the state of a MaybeT (State s) a computation may continue to be modified even after the computation has failed. (This may seem backwards, but it is correct. Monad transformers build composite monads “inside out”; for example, MaybeT (State s) a is isomorphic to s -> (Maybe a, s). Lambdabot has an indispensable @unmtl command which you can use to “unpack” a monad transformer stack in this way.)

All monad transformers should implement the MonadTrans type class, defined in Control.Monad.Trans:

class MonadTrans t where
  lift :: Monad m => m a -> t m a

It allows arbitrary computations in the base monad m to be “lifted” into computations in the transformed monad t m. (Note that type application associates to the left, just like function application, so t m a = (t m) a. As an exercise, you may wish to work out t’s kind, which is rather more interesting than most of the kinds we’ve seen up to this point.) However, you should only have to think about MonadTrans when defining your own monad transformers, not when using predefined ones.

∗ The only problem with this scheme is the quadratic number of instances required as the number of standard monad transformers grows—but as the current set of standard monad transformers seems adequate for most common use cases, this may not be that big of a deal.

There are also type classes such as MonadState, which provides state-specific methods like get and put, allowing you to conveniently use these methods not only with State, but with any monad which is an instance of MonadState—including MaybeT (State s), StateT s (ReaderT r IO), and so on. Similar type classes exist for Reader, Writer, Cont, IO, and others .

There are two excellent references on monad transformers. Martin Grabmüller’s Monad Transformers Step by Step is a thorough description, with running examples, of how to use monad transformers to elegantly build up computations with various effects. Cale Gibbard’s article on how to use monad transformers is more practical, describing how to structure code using monad transformers to make writing it as painless as possible. Another good starting place for learning about monad transformers is a blog post by Dan Piponi.

MonadFix

The MonadFix class describes monads which support the special fixpoint operation mfix :: (a -> m a) -> m a, which allows the output of monadic computations to be defined via recursion. This is supported in GHC and Hugs by a special “recursive do” notation, mdo. For more information, see Levent Erkök’s thesis, Value Recursion in Monadic Computations.

Further reading

Philip Wadler was the first to propose using monads to structure functional programs. His paper is still a readable introduction to the subject.

Much of the monad transformer library mtl, including the Reader, Writer, State, and other monads, as well as the monad transformer framework itself, was inspired by Mark Jones’s classic paper Functional Programming with Overloading and Higher-Order Polymorphism. It’s still very much worth a read—and highly readable—after almost fifteen years.

∗ {{{1}}}

There are, of course, numerous monad tutorials of varying quality .

A few of the best include Cale Gibbard’s Monads as containers and Monads as computation; Jeff Newbern’s All About Monads, a comprehensive guide with lots of examples; and Dan Piponi’s You Could Have Invented Monads!, which features great exercises. If you just want to know how to use IO, you could consult the Introduction to IO. Even this is just a sampling; the monad tutorials timeline is a more complete list. (All these monad tutorials have prompted parodies like think of a monad ... as well as other kinds of backlash like Monads! (and Why Monad Tutorials Are All Awful) or Abstraction, intuition, and the “monad tutorial fallacy”.)

Other good monad references which are not necessarily tutorials include Henk-Jan van Tuyl’s tour of the functions in Control.Monad, Dan Piponi’s field guide, and Tim Newsham’s What’s a Monad?. There are also many blog posts which have been written on various aspects of monads; a collection of links can be found under Blog articles/Monads.

One of the quirks of the Monad class and the Haskell type system is that it is not possible to straightforwardly declare Monad instances for types which require a class constraint on their data, even if they are monads from a mathematical point of view. For example, Data.Set requires an Ord constraint on its data, so it cannot be easily made an instance of Monad. A solution to this problem was first described by Eric Kidd, and later made into a library named rmonad by Ganesh Sittampalam and Peter Gavin.

There are many good reasons for eschewing do notation; some have gone so far as to consider it harmful.

Monads can be generalized in various ways; for an exposition of one possibility, see Robert Atkey’s paper on parameterized monads, or Dan Piponi’s Beyond Monads.

For the categorically inclined, monads can be viewed as monoids (From Monoids to Monads) and also as closure operators Triples and Closure. Derek Elkins’s article in issue 13 of the Monad.Reader contains an exposition of the category-theoretic underpinnings of some of the standard Monad instances, such as State and Cont. There is also an alternative way to compose monads, using coproducts, as described by Lüth and Ghani, although this method has not (yet?) seen widespread use.

Links to many more research papers related to monads can be found under Research papers/Monads and arrows.

Monoid

A monoid is a set S\ together with a binary operation \oplus\ which combines elements from S\ . The \oplus\ operator is required to be associative (that is, (a \oplus b) \oplus c = a \oplus (b \oplus c)\ , for any a,b,c\ which are elements of S\ ), and there must be some element of S\ which is the identity with respect to \oplus\ . (If you are familiar with group theory, a monoid is like a group without the requirement that inverses exist.) For example, the natural numbers under addition form a monoid: the sum of any two natural numbers is a natural number; (a+b)+c = a+(b+c)\ for any natural numbers a\ , b\ , and c\,\ ; and zero is the additive identity. The integers under multiplication also form a monoid, as do natural numbers under \max\ , Boolean values under conjunction and disjunction, lists under concatenation, functions from a set to itself under composition ... Monoids show up all over the place, once you know to look for them.

Definition

The definition of the Monoid type class (defined in Data.Monoid; haddock) is:

class Monoid a where
  mempty  :: a
  mappend :: a -> a -> a

  mconcat :: [a] -> a
  mconcat = foldr mappend mempty

The mempty value specifies the identity element of the monoid, and mappend is the binary operation. The default definition for mconcat “reduces” a list of elements by combining them all with mappend, using a right fold. It is only in the Monoid class so that specific instances have the option of providing an alternative, more efficient implementation; usually, you can safely ignore mconcat when creating a Monoid instance, since its default definition will work just fine.

The Monoid methods are rather unfortunately named; they are inspired by the list instance of Monoid, where indeed mempty = [] and mappend = (++), but this is misleading since many monoids have little to do with appending (see these Comments from OCaml Hacker Brian Hurt on the haskell-cafe mailing list).

Laws

Of course, every Monoid instance should actually be a monoid in the mathematical sense, which implies these laws:

mempty `mappend` x = x
x `mappend` mempty = x
(x `mappend` y) `mappend` z = x `mappend` (y `mappend` z)

Instances

There are quite a few interesting Monoid instances defined in Data.Monoid.

  • [a] is a Monoid, with mempty = [] and mappend = (++). It is not hard to check that (x ++ y) ++ z = x ++ (y ++ z) for any lists x, y, and z, and that the empty list is the identity: [] ++ x = x ++ [] = x.
  • As noted previously, we can make a monoid out of any numeric type under either addition or multiplication. However, since we can’t have two instances for the same type, Data.Monoid provides two newtype wrappers, Sum and Product, with appropriate Monoid instances.
    > getSum (mconcat . map Sum $ [1..5])
    15
    > getProduct (mconcat . map Product $ [1..5])
    120
    

    This example code is silly, of course; we could just write

    sum [1..5] and product [1..5]. Nevertheless, these instances are useful in more generalized settings, as we will see in the section Foldable.
  • Any and All are newtype wrappers providing Monoid instances for Bool (under disjunction and conjunction, respectively).
  • There are three instances for Maybe: a basic instance which lifts a Monoid instance for a to an instance for Maybe a, and two newtype wrappers First and Last for which mappend selects the first (respectively last) non-Nothing item.
  • Endo a is a newtype wrapper for functions a -> a, which form a monoid under composition.
  • There are several ways to “lift” Monoid instances to instances with additional structure. We have already seen that an instance for a can be lifted to an instance for Maybe a. There are also tuple instances: if a and b are instances of code>Monoid</code>, then so is (a,b), using the monoid operations for a and b in the obvious pairwise manner. Finally, if a is a Monoid, then so is the function type e -> a for any e; in particular, g `mappend` h is the function which applies both g and h to its argument and then combines the results using the underlying Monoid instance for a. This can be quite useful and elegant (see example).
  • The type Ordering = LT || EQ || GT is a Monoid, defined in such a way that mconcat (zipWith compare xs ys) computes the lexicographic ordering of xs and ys (if xs and ys have the same length). In particular, mempty = EQ, and mappend evaluates to its leftmost non-EQ argument (or EQ if both arguments are EQ). This can be used together with the function instance of Monoid to do some clever things (example).
  • There are also Monoid instances for several standard data structures in the containers library (haddock), including Map, Set, and Sequence.
  • </ul>

    Monoid is also used to enable several other type class instances. As noted previously, we can use Monoid to make ((,) e) an instance of Applicative:

    instance Monoid e => Applicative ((,) e) where
      pure x = (mempty, x)
      (u, f) <*> (v, x) = (u `mappend` v, f x)
    

    Monoid can be similarly used to make ((,) e) an instance of Monad as well; this is known as the writer monad. As we’ve already seen, Writer and WriterT are a newtype wrapper and transformer for this monad, respectively.

    Monoid also plays a key role in the Foldable type class (see section Foldable).

    Other monoidal classes: Alternative, MonadPlus, ArrowPlus

    The Alternative type class (haddock) is for Applicative functors which also have a monoid structure:

    class Applicative f => Alternative f where
      empty :: f a
      (<|>) :: f a -> f a -> f a
    

    Of course, instances of Alternative should satisfy the monoid laws.

    Likewise, MonadPlus (haddock) is for Monads with a monoid structure:

    class Monad m => MonadPlus m where
      mzero :: m a
      mplus :: m a -> m a -> m a
    

    The MonadPlus documentation states that it is intended to model monads which also support “choice and failure”; in addition to the monoid laws, instances of MonadPlus are expected to satisfy

    mzero >>= f  =  mzero
    v >> mzero   =  mzero
    

    which explains the sense in which mzero denotes failure. Since mzero should be the identity for mplus, the computation m1 `mplus` m2 succeeds (evaluates to something other than mzero) if either m1 or m2 does; so mplus represents choice. The guard function can also be used with instances of MonadPlus; it requires a condition to be satisfied and fails (using mzero) if it is not. A simple example of a MonadPlus instance is [], which is exactly the same as the Monoid instance for []: the empty list represents failure, and list concatenation represents choice. In general, however, a MonadPlus instance for a type need not be the same as its Monoid instance; Maybe is an example of such a type. A great introduction to the MonadPlus type class, with interesting examples of its use, is Doug Auclair’s MonadPlus: What a Super Monad! in the Monad.Reader issue 11.

    There used to be a type class called MonadZero containing only mzero, representing monads with failure. The do-notation requires some notion of failure to deal with failing pattern matches. Unfortunately, MonadZero was scrapped in favor of adding the fail method to the Monad class. If we are lucky, someday MonadZero will be restored, and fail will be banished to the bit bucket where it belongs (see MonadPlus reform proposal). The idea is that any do-block which uses pattern matching (and hence may fail) would require a MonadZero constraint; otherwise, only a Monad constraint would be required.

    Finally, ArrowZero and ArrowPlus (haddock) represent Arrows (see below) with a monoid structure:

    class Arrow (~>) => ArrowZero (~>) where
      zeroArrow :: b ~> c
    
    class ArrowZero (~>) => ArrowPlus (~>) where
      (<+>) :: (b ~> c) -> (b ~> c) -> (b ~> c)
    

    Further reading

    Monoids have gotten a fair bit of attention recently, ultimately due to a blog post by Brian Hurt, in which he complained about the fact that the names of many Haskell type classes (Monoid in particular) are taken from abstract mathematics. This resulted in a long haskell-cafe thread arguing the point and discussing monoids in general.

    ∗ May its name live forever.

    However, this was quickly followed by several blog posts about Monoid . First, Dan Piponi wrote a great introductory post, Haskell Monoids and their Uses. This was quickly followed by Heinrich Apfelmus’s Monoids and Finger Trees, an accessible exposition of Hinze and Paterson’s classic paper on 2-3 finger trees, which makes very clever use of Monoid to implement an elegant and generic data structure. Dan Piponi then wrote two fascinating articles about using Monoids (and finger trees): Fast Incremental Regular Expressions and Beyond Regular Expressions

    In a similar vein, David Place’s article on improving Data.Map in order to compute incremental folds (see the Monad Reader issue 11) is also a good example of using Monoid to generalize a data structure.

    Some other interesting examples of Monoid use include building elegant list sorting combinators, collecting unstructured information, and a brilliant series of posts by Chung-Chieh Shan and Dylan Thurston using Monoids to elegantly solve a difficult combinatorial puzzle (followed by part 2, part 3, part 4).

    As unlikely as it sounds, monads can actually be viewed as a sort of monoid, with join playing the role of the binary operation and return the role of the identity; see Dan Piponi’s blog post.

    Foldable

    The Foldable class, defined in the Data.Foldable module (haddock), abstracts over containers which can be “folded” into a summary value. This allows such folding operations to be written in a container-agnostic way.

    Definition

    The definition of the Foldable type class is:

    class Foldable t where
      fold    :: Monoid m => t m -> m
      foldMap :: Monoid m => (a -> m) -> t a -> m
    
      foldr   :: (a -> b -> b) -> b -> t a -> b
      foldl   :: (a -> b -> a) -> a -> t b -> a
      foldr1  :: (a -> a -> a) -> t a -> a
      foldl1  :: (a -> a -> a) -> t a -> a
    

    This may look complicated, but in fact, to make a Foldable instance you only need to implement one method: your choice of foldMap or foldr. All the other methods have default implementations in terms of these, and are presumably included in the class in case more efficient implementations can be provided.

    Instances and examples

    The type of foldMap should make it clear what it is supposed to do: given a way to convert the data in a container into a Monoid (a function a -> m) and a container of a’s (t a), foldMap provides a way to iterate over the entire contents of the container, converting all the a’s to m’s and combining all the m’s with mappend. The following code shows two examples: a simple implementation of foldMap for lists, and a binary tree example provided by the Foldable documentation.

    instance Foldable [] where
      foldMap g = mconcat . map g
    
    data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
    
    instance Foldable Tree where
      foldMap f Empty = mempty
      foldMap f (Leaf x) = f x
      foldMap f (Node l k r) = foldMap f l ++ f k ++ foldMap f r
        where (++) = mappend
    

    The foldr function has a type similar to the foldr found in the Prelude, but more general, since the foldr in the Prelude works only on lists.

    The Foldable module also provides instances for Maybe and Array; additionally, many of the data structures found in the standard containers library (for example, Map, Set, Tree, and Sequence) provide their own Foldable instances.

    Derived folds

    Given an instance of Foldable, we can write generic, container-agnostic functions such as:

    -- Compute the size of any container.
    containerSize :: Foldable f => f a -> Int
    containerSize = getSum . foldMap (const (Sum 1))
    
    -- Compute a list of elements of a container satisfying a predicate.
    filterF :: Foldable f => (a -> Bool) -> f a -> [a]
    filterF p = foldMap (\a -> if p a then [a] else [])
    
    -- Get a list of all the Strings in a container which include the
    -- letter a.
    aStrings :: Foldable f => f String -> [String]
    aStrings = filterF (elem 'a')
    

    The Foldable module also provides a large number of predefined folds, many of which are generalized versions of Prelude functions of the same name that only work on lists: concat, concatMap, and, or, any, all, sum, product, maximum(By), minimum(By), elem, notElem, and find. The reader may enjoy coming up with elegant implementations of these functions using fold or foldMap and appropriate Monoid instances.

    There are also generic functions that work with Applicative or Monad instances to generate some sort of computation from each element in a container, and then perform all the side effects from those computations, discarding the results: traverse_, sequenceA_, and others. The results must be discarded because the Foldable class is too weak to specify what to do with them: we cannot, in general, make an arbitrary Applicative or Monad instance into a Monoid. If we do have an Applicative or Monad with a monoid structure—that is, an Alternative or a MonadPlus—then we can use the asum or msum functions, which can combine the results as well. Consult the Foldable documentation for more details on any of these functions.

    Note that the Foldable operations always forget the structure of the container being folded. If we start with a container of type t a for some Foldable t, then t will never appear in the output type of any operations defined in the Foldable module. Many times this is exactly what we want, but sometimes we would like to be able to generically traverse a container while preserving its structure—and this is exactly what the Traversable class provides, which will be discussed in the next section.

    Further reading

    The Foldable class had its genesis in McBride and Paterson’s paper introducing Applicative, although it has been fleshed out quite a bit from the form in the paper.

    An interesting use of Foldable (as well as Traversable) can be found in Janis Voigtländer’s paper Bidirectionalization for free!.

    Traversable

    Definition

    The Traversable type class, defined in the Data.Traversable module (haddock), is:

    class (Functor t, Foldable t) => Traversable t where
      traverse  :: Applicative f => (a -> f b) -> t a -> f (t b)
      sequenceA :: Applicative f => t (f a) -> f (t a)
      mapM      :: Monad m => (a -> m b) -> t a -> m (t b)
      sequence  :: Monad m => t (m a) -> m (t a)
    

    As you can see, every Traversable is also a foldable functor. Like Foldable, there is a lot in this type class, but making instances is actually rather easy: one need only implement traverse or sequenceA; the other methods all have default implementations in terms of these functions. A good exercise is to figure out what the default implementations should be: given either traverse or sequenceA, how would you define the other three methods? (Hint for mapM: Control.Applicative exports the WrapMonad newtype, which makes any Monad into an Applicative. The sequence function can be implemented in terms of mapM.)

    Intuition

    The key method of the Traversable class, and the source of its unique power, is sequenceA. Consider its type:

      sequenceA :: Applicative f => t (f a) -> f (t a)
    

    This answers the fundamental question: when can we commute two functors? For example, can we turn a tree of lists into a list of trees? (Answer: yes, in two ways. Figuring out what they are, and why, is left as an exercise. A much more challenging question is whether a list of trees can be turned into a tree of lists.)

    The ability to compose two monads depends crucially on this ability to commute functors. Intuitively, if we want to build a composed monad M a = m (n a) out of monads m and n, then to be able to implement join :: M (M a) -> M a, that is, join :: m (n (m (n a))) -> m (n a), we have to be able to commute the n past the m to get m (m (n (n a))), and then we can use the joins for m and n to produce something of type m (n a). See Mark Jones’s paper for more details.

    Instances and examples

    What’s an example of a Traversable instance? The following code shows an example instance for the same Tree type used as an example in the previous Foldable section. It is instructive to compare this instance with a Functor instance for Tree, which is also shown.

    data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
    
    instance Traversable Tree where
      traverse g Empty        = pure Empty
      traverse g (Leaf x)     = Leaf <$> g x
      traverse g (Node l x r) = Node <$> traverse g l
                                     <*> g x
                                     <*> traverse g r
    
    instance Functor Tree where
      fmap     g Empty        = Empty
      fmap     g (Leaf x)     = Leaf $ g x
      fmap     g (Node l x r) = Node (fmap g l)
                                     (g x)
                                     (fmap g r)
    

    It should be clear that the Traversable and Functor instances for Tree are almost identical; the only difference is that the Functor instance involves normal function application, whereas the applications in the Traversable instance take place within an Applicative context, using (<$>) and (<*>). In fact, this will be true for any type.

    Any Traversable functor is also Foldable, and a Functor. We can see this not only from the class declaration, but by the fact that we can implement the methods of both classes given only the Traversable methods. A good exercise is to implement fmap and foldMap using only the Traversable methods; the implementations are surprisingly elegant. The Traversable module provides these implementations as fmapDefault and foldMapDefault.

    The standard libraries provide a number of Traversable instances, including instances for [], Maybe, Map, Tree, and Sequence. Notably, Set is not Traversable, although it is Foldable.

    Further reading

    The Traversable class also had its genesis in [http://www.soi.city.ac.uk/~ross/papers/Applicative.html McBride and Paterson’s Applicative paper], and is described in more detail in Gibbons and Oliveira, The Essence of the Iterator Pattern, which also contains a wealth of references to related work.

    Category

    Category is another fairly new addition to the Haskell standard libraries; you may or may not have it installed depending on the version of your base package. It generalizes the notion of function composition to general “morphisms”.

    The definition of the Category type class (from Control.Categoryhaddock) is shown below. For ease of reading, note that I have used an infix type constructor (~>), much like the infix function type constructor (->). This syntax is not part of Haskell 98. The second definition shown is the one used in the standard libraries. For the remainder of this document, I will use the infix type constructor (~>) for Category as well as Arrow.

    class Category (~>) where
      id  :: a ~> a
      (.) :: (b ~> c) -> (a ~> b) -> (a ~> c)
    
    -- The same thing, with a normal (prefix) type constructor
    class Category cat where
      id  :: cat a a
      (.) :: cat b c -> cat a b -> cat a c
    

    Note that an instance of Category should be a type constructor which takes two type arguments, that is, something of kind * -> * -> *. It is instructive to imagine the type constructor variable cat replaced by the function constructor (->): indeed, in this case we recover precisely the familiar identity function id and function composition operator (.) defined in the standard Prelude.

    Of course, the Category module provides exactly such an instance of Category for (->). But it also provides one other instance, shown below, which should be familiar from the previous discussion of the Monad laws. Kleisli m a b, as defined in the Control.Arrow module, is just a newtype wrapper around a -> m b.

    newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
    
    instance Monad m => Category (Kleisli m) where
      id = Kleisli return
      Kleisli g . Kleisli h = Kleisli (h >=> g)
    

    The only law that Category instances should satisfy is that id and (.) should form a monoid—that is, id should be the identity of (.), and (.) should be associative.

    Finally, the Category module exports two additional operators: (<<<), which is just a synonym for (.), and (>>>), which is (.) with its arguments reversed. (In previous versions of the libraries, these operators were defined as part of the Arrow class.)

    Further reading

    The name Category is a bit misleading, since the Category class cannot represent arbitrary categories, but only categories whose objects are objects of Hask, the category of Haskell types. For a more general treatment of categories within Haskell, see the category-extras package. For more about category theory in general, see the excellent Haskell wikibook page, Steve Awodey’s new book, Benjamin Pierce’s Basic category theory for computer scientists, or Barr and Wells’s category theory lecture notes. Benjamin Russell’s blog post is another good source of motivation and category theory links. You certainly don’t need to know any category theory to be a successful and productive Haskell programmer, but it does lend itself to much deeper appreciation of Haskell’s underlying theory.

    Arrow

    The Arrow class represents another abstraction of computation, in a similar vein to Monad and Applicative. However, unlike Monad and Applicative, whose types only reflect their output, the type of an Arrow computation reflects both its input and output. Arrows generalize functions: if (~>) is an instance of Arrow, a value of type b ~> c can be thought of as a computation which takes values of type b as input, and produces values of type c as output. In the (->) instance of Arrow this is just a pure function; in general, however, an arrow may represent some sort of “effectful” computation.

    Definition

    The definition of the Arrow type class, from Control.Arrow (haddock), is:

    class Category (~>) => Arrow (~>) where
      arr :: (b -> c) -> (b ~> c)
      first :: (b ~> c) -> ((b, d) ~> (c, d))
      second :: (b ~> c) -> ((d, b) ~> (d, c))
      (***) :: (b ~> c) -> (b' ~> c') -> ((b, b') ~> (c, c'))
      (&&&) :: (b ~> c) -> (b ~> c') -> (b ~> (c, c'))
    

    ∗ In versions of the base package prior to version 4, there is no Category class, and the Arrow class includes the arrow composition operator (>>>). It also includes pure as a synonym for arr, but this was removed since it conflicts with the pure from Applicative.

    The first thing to note is the Category class constraint, which means that we get identity arrows and arrow composition for free: given two arrows g :: b ~> c and h :: c ~> d, we can form their composition g >>> h :: b ~> d .

    As should be a familiar pattern by now, the only methods which must be defined when writing a new instance of Arrow are arr and first; the other methods have default definitions in terms of these, but are included in the Arrow class so that they can be overridden with more efficient implementations if desired.

    Intuition

    Let’s look at each of the arrow methods in turn. Ross Paterson’s web page on arrows has nice diagrams which can help build intuition.

    • The arr function takes any function b -> c and turns it into a generalized arrow b ~> c. The arr method justifies the claim that arrows generalize functions, since it says that we can treat any function as an arrow. It is intended that the arrow arr g is “pure” in the sense that it only computes g and has no “effects” (whatever that might mean for any particular arrow type).
    • The first method turns any arrow from b to c into an arrow from (b,d) to (c,d). The idea is that first g uses g to process the first element of a tuple, and lets the second element pass through unchanged. For the function instance of Arrow, of course, first g (x,y) = (g x, y).
    • The second function is similar to first, but with the elements of the tuples swapped. Indeed, it can be defined in terms of first using an auxiliary function swap, defined by swap (x,y) = (y,x).
    • The (***) operator is “parallel composition” of arrows: it takes two arrows and makes them into one arrow on tuples, which has the behavior of the first arrow on the first element of a tuple, and the behavior of the second arrow on the second element. The mnemonic is that g *** h is the product (hence *) of g and h. For the function instance of Arrow, we define (g *** h) (x,y) = (g x, h y). The default implementation of (***) is in terms of first, second, and sequential arrow composition (>>>). The reader may also wish to think about how to implement first and second in terms of (***).
    • The (&&&) operator is “fanout composition” of arrows: it takes two arrows g and h and makes them into a new arrow g &&& h which supplies its input as the input to both g and h, returning their results as a tuple. The mnemonic is that g &&& h performs both g and h (hence &) on its input. For functions, we define (g &&& h) x = (g x, h x).

    Instances

    The Arrow library itself only provides two Arrow instances, both of which we have already seen: (->), the normal function constructor, and Kleisli m, which makes functions of type a -> m b into Arrows for any Monad m. These instances are:

    instance Arrow (->) where
      arr g = g
      first g (x,y) = (g x, y)
    
    newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
    
    instance Monad m => Arrow (Kleisli m) where
      arr f = Kleisli (return . f)
      first (Kleisli f) = Kleisli (\ ~(b,d) -> do c <- f b
                                                  return (c,d) )
    

    Laws

    ∗ See John Hughes: Generalising monads to arrows; Sam Lindley, Philip Wadler, Jeremy Yallop: The arrow calculus; Ross Paterson: Programming with Arrows.

    There are quite a few laws that instances of Arrow should satisfy :

                           arr id  =  id
                      arr (h . g)  =  arr g >>> arr h
                    first (arr g)  =  arr (g *** id)
                  first (g >>> h)  =  first g >>> first h
       first g >>> arr (id *** h)  =  arr (id *** h) >>> first g
              first g >>> arr fst  =  arr fst >>> g
    first (first g) >>> arr assoc  =  arr assoc >>> first g
    
    assoc ((x,y),z) = (x,(y,z))
    

    Note that this version of the laws is slightly different than the laws given in the first two above references, since several of the laws have now been subsumed by the Category laws (in particular, the requirements that id is the identity arrow and that (>>>) is associative). The laws shown here follow those in Paterson’s Programming with Arrows, which uses the Category class.

    ∗ Unless category-theory-induced insomnolence is your cup of tea.

    The reader is advised not to lose too much sleep over the Arrow laws , since it is not essential to understand them in order to program with arrows. There are also laws that ArrowChoice, ArrowApply, and ArrowLoop instances should satisfy; the interested reader should consult Paterson: Programming with Arrows.

    ArrowChoice

    Computations built using the Arrow class, like those built using the Applicative class, are rather inflexible: the structure of the computation is fixed at the outset, and there is no ability to choose between alternate execution paths based on intermediate results. The ArrowChoice class provides exactly such an ability:

    class Arrow (~>) => ArrowChoice (~>) where
      left  :: (b ~> c) -> (Either b d ~> Either c d)
      right :: (b ~> c) -> (Either d b ~> Either d c)
      (+++) :: (b ~> c) -> (b' ~> c') -> (Either b b' ~> Either c c')
      (|||) :: (b ~> d) -> (c ~> d) -> (Either b c ~> d)
    

    A comparison of ArrowChoice to Arrow will reveal a striking parallel between left, right, (+++), (|||) and first, second, (***), (&&&), respectively. Indeed, they are dual: first, second, (***), and (&&&) all operate on product types (tuples), and left, right, (+++), and (|||) are the corresponding operations on sum types. In general, these operations create arrows whose inputs are tagged with Left or Right, and can choose how to act based on these tags.

    • If g is an arrow from b to c, then left g is an arrow from Either b d to Either c d. On inputs tagged with Left, the left g arrow has the behavior of g; on inputs tagged with Right, it behaves as the identity.
    • The right function, of course, is the mirror image of left. The arrow right g has the behavior of g on inputs tagged with Right.
    • The (+++) operator performs “multiplexing”: g +++ h behaves as g on inputs tagged with Left, and as h on inputs tagged with Right. The tags are preserved. The (+++) operator is the sum (hence +) of two arrows, just as (***) is the product.
    • The (|||) operator is “merge” or “fanin”: the arrow g ||| h behaves as g on inputs tagged with Left, and h on inputs tagged with Right, but the tags are discarded (hence, g and h must have the same output type). The mnemonic is that g ||| h performs either g or h on its input.

    The ArrowChoice class allows computations to choose among a finite number of execution paths, based on intermediate results. The possible execution paths must be known in advance, and explicitly assembled with (+++) or (|||). However, sometimes more flexibility is needed: we would like to be able to compute an arrow from intermediate results, and use this computed arrow to continue the computation. This is the power given to us by ArrowApply.

    ArrowApply

    The ArrowApply type class is:

    class Arrow (~>) => ArrowApply (~>) where
      app :: (b ~> c, b) ~> c
    

    If we have computed an arrow as the output of some previous computation, then app allows us to apply that arrow to an input, producing its output as the output of app. As an exercise, the reader may wish to use app to implement an alternative “curried” version, app2 :: b ~> ((b ~> c) ~> c).

    This notion of being able to compute a new computation may sound familiar: this is exactly what the monadic bind operator (>>=) does. It should not particularly come as a surprise that ArrowApply and Monad are exactly equivalent in expressive power. In particular, Kleisli m can be made an instance of ArrowApply, and any instance of ArrowApply can be made a Monad (via the newtype wrapper ArrowMonad). As an exercise, the reader may wish to try implementing these instances:

    instance Monad m => ArrowApply (Kleisli m) where
      app =    -- exercise
    
    newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
    
    instance ArrowApply a => Monad (ArrowMonad a) where
      return               =    -- exercise
      (ArrowMonad a) >>= k =    -- exercise
    

    ArrowLoop

    The ArrowLoop type class is:

    class Arrow a => ArrowLoop a where
      loop :: a (b, d) (c, d) -> a b c
    
    trace :: ((b,d) -> (c,d)) -> b -> c
    trace f b = let (c,d) = f (b,d) in c
    

    It describes arrows that can use recursion to compute results, and is used to desugar the rec construct in arrow notation (described below).

    Taken by itself, the type of the loop method does not seem to tell us much. Its intention, however, is a generalization of the trace function which is also shown. The d component of the first arrow’s output is fed back in as its own input. In other words, the arrow loop g is obtained by recursively “fixing” the second component of the input to g.

    It can be a bit difficult to grok what the trace function is doing. How can d appear on the left and right sides of the let? Well, this is Haskell’s laziness at work. There is not space here for a full explanation; the interested reader is encouraged to study the standard fix function, and to read Paterson’s arrow tutorial.

    Arrow notation

    Programming directly with the arrow combinators can be painful, especially when writing complex computations which need to retain simultaneous reference to a number of intermediate results. With nothing but the arrow combinators, such intermediate results must be kept in nested tuples, and it is up to the programmer to remember which intermediate results are in which components, and to swap, reassociate, and generally mangle tuples as necessary. This problem is solved by the special arrow notation supported by GHC, similar to do notation for monads, that allows names to be assigned to intermediate results while building up arrow computations. An example arrow implemented using arrow notation, taken from Paterson, is:

    class ArrowLoop (~>) => ArrowCircuit (~>) where
      delay :: b -> (b ~> b)
    
    counter :: ArrowCircuit (~>) => Bool ~> Int
    counter = proc reset -> do
                rec output <- idA     -< if reset then 0 else next
                    next   <- delay 0 -< output + 1
                idA -< output
    

    This arrow is intended to represent a recursively defined counter circuit with a reset line.

    There is not space here for a full explanation of arrow notation; the interested reader should consult [http://www.soi.city.ac.uk/~ross/papers/notation.html Paterson’s paper introducing the notation], or his later [http://www.soi.city.ac.uk/~ross/papers/fop.html tutorial which presents a simplified version].

    Further reading

    An excellent starting place for the student of arrows is the arrows web page, which contains an introduction and many references. Some key papers on arrows include Hughes’s original paper introducing arrows, Generalising monads to arrows, and Paterson’s paper on arrow notation.

    Both Hughes and Paterson later wrote accessible tutorials intended for a broader audience: Paterson: Programming with Arrows and Hughes: Programming with Arrows.

    Although Hughes’s goal in defining the Arrow class was to generalize Monads, and it has been said that Arrow lies “between Applicative and Monad” in power, they are not directly comparable. The precise relationship remained in some confusion until analyzed by Lindley, Wadler, and Yallop, who also invented a new calculus of arrows, based on the lambda calculus, which considerably simplifies the presentation of the arrow laws (see The arrow calculus).

    Some examples of Arrows include Yampa, the Haskell XML Toolkit, and the functional GUI library Grapefruit.

    Some extensions to arrows have been explored; for example, the BiArrows of Alimarine et al., for two-way instead of one-way computation.

    The Haskell wiki has links to many additional research papers relating to Arrows.

    Comonad

    The final type class we will examine is Comonad. The Comonad class is the categorical dual of Monad; that is, Comonad is like Monad but with all the function arrows flipped. It is not actually in the standard Haskell libraries, but it has seen some interesting uses recently, so we include it here for completeness.

    Definition

    The Comonad type class, defined in the Control.Comonad module of the category-extras library, is:

    class Functor f => Copointed f where
      extract :: f a -> a
    
    class Copointed w => Comonad w where
      duplicate :: w a -> w (w a)
      extend :: (w a -> b) -> w a -> w b
    

    As you can see, extract is the dual of return, duplicate is the dual of join, and extend is the dual of (>>=) (although its arguments are in a different order). The definition of Comonad is a bit redundant (after all, the Monad class does not need join), but this is so that a Comonad can be defined by fmap, extract, and either duplicate or extend. Each has a default implementation in terms of the other.

    A prototypical example of a Comonad instance is:

    -- Infinite lazy streams
    data Stream a = Cons a (Stream a)
    
    instance Functor Stream where
      fmap g (Cons x xs) = Cons (g x) (fmap g xs)
    
    instance Copointed Stream where
      extract (Cons x _) = x
    
    -- 'duplicate' is like the list function 'tails'
    -- 'extend' computes a new Stream from an old, where the element
    --   at position n is computed as a function of everything from
    --   position n onwards in the old Stream
    instance Comonad Stream where
      duplicate s@(Cons x xs) = Cons s (duplicate xs)
      extend g s@(Cons x xs)  = Cons (g s) (extend g xs)
                           -- = fmap g (duplicate s)
    

    Further reading

    Dan Piponi explains in a blog post what [http://blog.sigfpe.com/2006/12/evaluating-cellular-automata-is.html cellular automata have to do with comonads]. In another blog post, Conal Elliott has examined [http://conal.net/blog/posts/functional-interactive-behavior/ a comonadic formulation of functional reactive programming]. Sterling Clover’s blog post Comonads in everyday life explains the relationship between comonads and zippers, and how comonads can be used to design a menu system for a web site.

    Uustalu and Vene have a number of papers exploring ideas related to comonads and functional programming:

    Acknowledgements

    A special thanks to all of those who taught me about standard Haskell type classes and helped me develop good intuition for them, particularly Jules Bean (quicksilver), Derek Elkins (ddarius), Conal Elliott (conal), Cale Gibbard (Cale), David House, Dan Piponi (sigfpe), and Kevin Reid (kpreid).

    I also thank the many people who provided a mountain of helpful feedback and suggestions on a first draft of the Typeclassopedia: David Amos, Kevin Ballard, Reid Barton, Doug Beardsley, Joachim Breitner, Andrew Cave, David Christiansen, Gregory Collins, Mark Jason Dominus, Conal Elliott, Yitz Gale, George Giorgidze, Steven Grady, Travis Hartwell, Steve Hicks, Philip Hölzenspies, Edward Kmett, Eric Kow, Serge Le Huitouze, Felipe Lessa, Stefan Ljungstrand, Eric Macaulay, Rob MacAulay, Simon Meier, Eric Mertens, Tim Newsham, Russell O’Connor, Conrad Parker, Walt Rorie-Baety, Colin Ross, Tom Schrijvers, Aditya Siram, C. Smith, Martijn van Steenbergen, Joe Thornber, Jared Updike, Rob Vollmert, Andrew Wagner, Louis Wasserman, and Ashley Yakeley, as well as a few only known to me by their IRC nicks: b_jonas, maltem, tehgeekmeister, and ziman. I have undoubtedly omitted a few inadvertently, which in no way diminishes my gratitude.

    Finally, I would like to thank Wouter Swierstra for his fantastic work editing the Monad.Reader, and my wife Joyia for her patience during the process of writing the Typeclassopedia.

    About the author

    Brent Yorgey (blog, homepage) is (as of November 2011) a fourth-year Ph.D. student in the programming languages group at the University of Pennsylvania. He enjoys teaching, creating EDSLs, playing Bach fugues, musing upon category theory, and cooking tasty lambda-treats for the denizens of #haskell.

    Colophon

    The Typeclassopedia was written by Brent Yorgey and initally published in March 2009. Painstakingly converted to wiki syntax by User:Geheimdienst in November 2011, after asking Brent’s permission.

    If something like this tex to wiki syntax conversion ever needs to be done again, here are some vim commands that helped:

    • %s/\\section{\([^}]*\)}/=\1=/gc
    • %s/\\subsection{\([^}]*\)}/==\1==/gc
    • %s/^ *\\item /\r* /gc
    • %s/---/—/gc
    • %s/\$\([^$]*\)\$/<math>\1\\ <\/math>/gc Appending “\ ” forces images to be rendered. Otherwise, Mediawiki would go back and forth between one font for short <math> tags, and another more Tex-like font for longer tags (containing more than a few characters)""
    • %s/|\([^|]*\)|/<code>\1<\/code>/gc
    • %s/\\dots/.../gc
    • %s/^\\label{.*$//gc
    • %s/\\emph{\([^}]*\)}/''\1''/gc
    • %s/\\term{\([^}]*\)}/''\1''/gc

    The biggest issue was taking the academic-paper-style citations and turning them into hyperlinks with an appropriate title and an appropriate target. In most cases there was an obvious thing to do (e.g. online PDFs of the cited papers or Citeseer entries). Sometimes, however, it’s less clear and you might want to check the original Typeclassopedia PDF with the original bibliography file.

    To get all the citations into the main text, I first tried processing the source with Tex or Lyx. This didn’t work due to missing unfindable packages, syntax errors, and my general ineptitude with Tex.

    I then went for the next best solution, which seemed to be extracting all instances of “\cite{something}” from the source and in that order pulling the referenced entries from the .bib file. This way you can go through the source file and sorted-references file in parallel, copying over what you need, without searching back and forth in the .bib file. I used:

    • egrep -o "\cite\{[^\}]*\}" ~/typeclassopedia.lhs | cut -c 6- | tr "," "\n" | tr -d "}" > /tmp/citations
    • for i in $(cat /tmp/citations); do grep -A99 "$i" ~/typeclassopedia.bib|egrep -B99 '^\}$' -m1 ; done > ~/typeclasso-refs-sorted