https://wiki.haskell.org/api.php?action=feedcontributions&user=Tkemps&feedformat=atomHaskellWiki - User contributions [en]2021-08-03T02:14:25ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=Typeclassopedia&diff=62381Typeclassopedia2018-03-28T16:53:26Z<p>Tkemps: Update for a dead link</p>
<hr />
<div>''By [[User:Byorgey|Brent Yorgey]], byorgey@gmail.com''<br />
<br />
''Originally published 12 March 2009 in [http://www.haskell.org/wikiupload/8/85/TMR-Issue13.pdf issue 13] of [http://themonadreader.wordpress.com/ the Monad.Reader]. Ported to the Haskell wiki in November 2011 by [[User:Geheimdienst|Geheimdienst]].''<br />
<br />
''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:Typeclassopedia|talk page]].''<br />
<br />
=Abstract=<br />
<br />
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.<br />
<br />
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.<br />
<br />
=Introduction=<br />
<br />
Have you ever had any of the following thoughts?<br />
* What the heck is a monoid, and how is it different from a mon<u>a</u>d?<br />
<br />
* 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?<br />
<br />
* 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.<br />
<br />
* 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.<br />
<br />
If you have, look no further! You, too, can write and understand concise, elegant, idiomatic Haskell code with the best of them.<br />
<br />
There are two keys to an expert Haskell hacker’s wisdom:<br />
# Understand the types.<br />
# Gain a deep intuition for each type class and its relationship to other type classes, backed up by familiarity with many examples.<br />
<br />
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<br />
<code>fmap</code> here ... nope, let’s see ... maybe I need another <code>(.)</code> somewhere? ... um ...”<br />
<br />
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—<br />
<br />
:''There is no royal road to Haskell. {{h:title|Well, he probably would have said it if he knew Haskell.|—Euclid}}''<br />
<br />
This document can only be a starting point, since good intuition comes from hard work, [http://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/ 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.<br />
<br />
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 [{{HackageDocs|base|Prelude}} <code>Prelude</code>], the type system, data types, and type classes.<br />
<br />
The type classes we will be discussing and their interrelationships ([[:File:Dependencies.txt|source code for this graph can be found here]]):<br />
<br />
[[Image:Typeclassopedia-diagram.png]]<br />
<br />
{{note|<code>Apply</code> can be found in the [http://hackage.haskell.org/package/semigroupoids <code>semigroupoids</code> package], and <code>Comonad</code> in the [http://hackage.haskell.org/package/comonad <code>comonad</code> package].}}<br />
<br />
* <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>.<br />
* <span style="border-bottom: 2px dotted black">Dotted lines</span> indicate some other sort of relationship.<br />
* <code>Monad</code> and <code>ArrowApply</code> are equivalent.<br />
* <code>Apply</code> and <code>Comonad</code> are greyed out since they are not actually (yet?) in the standard Haskell libraries {{noteref}}.<br />
<br />
One more note before we begin. The original spelling of “type class” is with two words, as evidenced by, for example, the [http://www.haskell.org/onlinereport/haskell2010/ Haskell 2010 Language 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.<br />
<br />
We now begin with the simplest type class of all: <code>Functor</code>.<br />
<br />
=Functor=<br />
<br />
The <code>Functor</code> class ([{{HackageDocs|base|Prelude}}#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.<br />
<br />
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.<br />
<br />
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.<br />
<br />
==Definition==<br />
<br />
Here is the type class declaration for <code>Functor</code>:<br />
<br />
<haskell><br />
class Functor f where<br />
fmap :: (a -> b) -> f a -> f b<br />
<br />
(<$) :: a -> f b -> f a<br />
(<$) = fmap . const<br />
</haskell><br />
<br />
<code>Functor</code> is exported by the <code>Prelude</code>, so no special imports are needed to use it. Note that the <code>(<$)</code> operator is provided for convenience, with a default implementation in terms of <code>fmap</code>; it is included in the class just to give <code>Functor</code> instances the opportunity to provide a more efficient implementation than the default. To understand <code>Functor</code>, then, we really need to understand <code>fmap</code>.<br />
<br />
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 a concrete type like <code>Int</code>; it is a sort of ''type function'' which takes another type as a parameter. More precisely, the ''kind'' of <code>f</code> must be <code>* -> *</code>. For example, <code>Maybe</code> is such a type with kind <code>* -> *</code>: <code>Maybe</code> is not a concrete type by itself (that is, there are no values of type <code>Maybe</code>), 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>.<br />
<br />
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.<br />
<br />
Finally, we can understand <code>(<$)</code>: instead of applying a function to the values a container/context, it simply replaces them with a given value. This is the same as applying a constant function, so <code>(<$)</code> can be implemented in terms of <code>fmap</code>.<br />
<br />
==Instances==<br />
<br />
{{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>.}}<br />
<br />
{{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.}}<br />
<br />
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:<br />
<br />
<haskell><br />
instance Functor [] where<br />
fmap :: (a -> b) -> [a] -> [b]<br />
fmap _ [] = []<br />
fmap g (x:xs) = g x : fmap g xs<br />
-- or we could just say fmap = map<br />
<br />
instance Functor Maybe where<br />
fmap :: (a -> b) -> Maybe a -> Maybe b<br />
fmap _ Nothing = Nothing<br />
fmap g (Just a) = Just (g a)<br />
</haskell><br />
<br />
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.<br />
<br />
There are other <code>Functor</code> instances in the standard library as well:<br />
<br />
* <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.<br />
<br />
* <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>.<br />
<br />
* <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.<br />
<br />
* <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.<br />
<br />
* 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].<br />
<br />
{{Exercises|<br />
<ol><br />
<li>Implement <code>Functor</code> instances for <code>Either e</code> and <code>((->) e)</code>.</li><br />
<li>Implement <code>Functor</code> instances for <code>((,) e)</code> and for <code>Pair</code>, defined as <br />
<br />
<haskell>data Pair a = Pair a a</haskell><br />
<br />
Explain their similarities and differences.<br />
</li><br />
<li>Implement a <code>Functor</code> instance for the type <code>ITree</code>, defined as<br />
<br />
<haskell><br />
data ITree a = Leaf (Int -> a) <br />
| Node [ITree a]<br />
</haskell><br />
</li><br />
<li>Give an example of a type of kind <code>* -> *</code> which cannot be made an instance of <code>Functor</code> (without using <code>undefined</code>).<br />
</li><br />
<li>Is this statement true or false? <br />
<br />
:''The composition of two <code>Functor</code>s is also a <code>Functor</code>.''<br />
<br />
If false, give a counterexample; if true, prove it by exhibiting some appropriate Haskell code.<br />
</li><br />
</ol><br />
}}<br />
<br />
==Laws==<br />
<br />
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:<br />
<br />
<haskell><br />
fmap id = id<br />
fmap (g . h) = (fmap g) . (fmap h)<br />
</haskell><br />
<br />
{{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].}}<br />
<br />
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}}.<br />
<br />
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.<br />
<br />
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?<br />
<br />
<haskell><br />
-- Evil Functor instance<br />
instance Functor [] where<br />
fmap :: (a -> b) -> [a] -> [b]<br />
fmap _ [] = []<br />
fmap g (x:xs) = g x : g x : fmap g xs<br />
</haskell><br />
<br />
Any Haskeller worth their salt would reject this code as a gruesome abomination.<br />
<br />
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, [http://byorgey.wordpress.com/2010/03/03/deriving-pleasure-from-ghc-6-12-1/ GHC can automatically derive] <code>Functor</code> instances for many data types.<br />
<br />
{{note|Actually, if <code>seq</code>/<code>undefined</code> are considered, it [http://stackoverflow.com/a/8323243/305559 is possible] to have an implementation which satisfies the first law but not the second. The rest of the comments in this section should be considered in a context where <code>seq</code> and <code>undefined</code> are excluded.}}<br />
<br />
A [https://github.com/quchen/articles/blob/master/second_functor_law.md similar argument also 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.{{noteref}}<br />
<br />
{{Exercises|<br />
# Although it is not possible for a <code>Functor</code> instance to satisfy the first <code>Functor</code> law but not the second (excluding <code>undefined</code>), the reverse is possible. Give an example of a (bogus) <code>Functor</code> instance which satisfies the second law but not the first.<br />
# Which laws are violated by the evil <code>Functor</code> instance for list shown above: both laws, or the first law alone? Give specific counterexamples.<br />
}}<br />
<br />
==Intuition==<br />
<br />
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).<br />
<br />
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”.<br />
<br />
==Utility functions==<br />
<br />
There are a few more <code>Functor</code>-related functions which can be imported from the <code>Data.Functor</code> module.<br />
<br />
* <code>(<$>)</code> is defined as a synonym for <code>fmap</code>. This enables a nice infix style that mirrors the <code>($)</code> operator for function application. For example, <code>f $ 3</code> applies the function <code>f</code> to 3, whereas <code>f <$> [1,2,3]</code> applies <code>f</code> to each member of the list.<br />
* <code>($>) :: Functor f => f a -> b -> f b</code> is just <code>flip (<$)</code>, and can occasionally be useful. To keep them straight, you can remember that <code>(<$)</code> and <code>($>)</code> point towards the value that will be kept.<br />
* <code>void :: Functor f => f a -> f ()</code> is a specialization of <code>(<$)</code>, that is, <code>void x = () <$ x</code>. This can be used in cases where a computation computes some value but the value should be ignored.<br />
<br />
==Further reading==<br />
<br />
A good starting point for reading about the category theory behind the concept of a functor is the excellent [http://en.wikibooks.org/wiki/Haskell/Category_theory Haskell wikibook page on category theory].<br />
<br />
=Applicative=<br />
<br />
A somewhat newer addition to the pantheon of standard Haskell type classes, ''applicative functors'' represent an abstraction lying in between <code>Functor</code> and <code>Monad</code> in expressivity, 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 [{{HackageDocs|base|Control-Applicative}} <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.<br />
<br />
==Definition==<br />
<br />
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 a context. <code>Applicative</code> gives us just such a tool, <code>(<*>)</code> (variously pronounced as "apply", "app", or "splat"). It also provides a method, <code>pure</code>, for embedding values in a default, “effect free” context. Here is the type class declaration for <code>Applicative</code>, as defined in <code>Control.Applicative</code>:<br />
<br />
<haskell><br />
class Functor f => Applicative f where<br />
pure :: a -> f a<br />
infixl 4 <*>, *>, <*<br />
(<*>) :: f (a -> b) -> f a -> f b<br />
<br />
(*>) :: f a -> f b -> f b<br />
a1 *> a2 = (id <$ a1) <*> a2<br />
<br />
(<*) :: f a -> f b -> f a<br />
(<*) = liftA2 const<br />
</haskell><br />
<br />
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.<br />
<br />
<code>(*>)</code> and <code>(<*)</code> are provided for convenience, in case a particular instance of <code>Applicative</code> can provide more efficient implementations, but they are provided with default implementations. For more on these operators, see the section on [[#Utility functions|Utility functions]] below.<br />
<br />
{{note|Recall that <code>($)</code> is just function application: <code>f $ x {{=}} f x</code>.}}<br />
<br />
As always, it’s crucial to understand the type signatures. First, consider <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>.<br />
<br />
<code>pure</code> takes a value of any type <code>a</code>, and returns a context/container of type <code>f a</code>. The intention is that <code>pure</code> creates some sort of “default” container or “effect free” context. In fact, the behavior of <code>pure</code> is quite constrained by the laws it should satisfy in conjunction with <code>(<*>)</code>. Usually, for a given implementation of <code>(<*>)</code> there is only one possible implementation of <code>pure</code>.<br />
<br />
(Note that previous versions of the Typeclassopedia explained <code>pure</code> in terms of a type class <code>Pointed</code>, which can still be found in the [http://hackage.haskell.org/package/pointed <code>pointed</code> package]. However, the current consensus is that <code>Pointed</code> is not very useful after all. For a more detailed explanation, see [[Why not Pointed?]])<br />
<br />
==Laws==<br />
<br />
{{note|See<br />
[{{HackageDocs|base|Control-Applicative}} haddock for Applicative] and [http://www.soi.city.ac.uk/~ross/papers/Applicative.html Applicative programming with effects]}}<br />
<br />
Traditionally, there are four laws that <code>Applicative</code> instances should satisfy {{noteref}}. In some sense, they are all concerned with making sure that <code>pure</code> deserves its name:<br />
<br />
* The identity law:<br /><haskell>pure id <*> v = v</haskell><br />
* Homomorphism:<br /><haskell>pure f <*> pure x = pure (f x)</haskell>Intuitively, applying a non-effectful function to a non-effectful argument in an effectful context is the same as just applying the function to the argument and then injecting the result into the context with <code>pure</code>.<br />
* Interchange:<br /><haskell>u <*> pure y = pure ($ y) <*> u</haskell>Intuitively, this says that when evaluating the application of an effectful function to a pure argument, the order in which we evaluate the function and its argument doesn't matter.<br />
* Composition:<br /><haskell>u <*> (v <*> w) = pure (.) <*> u <*> v <*> w </haskell>This one is the trickiest law to gain intuition for. In some sense it is expressing a sort of associativity property of <code>(<*>)</code>. The reader may wish to simply convince themselves that this law is type-correct.<br />
<br />
Considered as left-to-right rewrite rules, the homomorphism, interchange, and composition laws actually constitute an algorithm for transforming any expression using <code>pure</code> and <code>(<*>)</code> into a canonical form with only a single use of <code>pure</code> at the very beginning and only left-nested occurrences of <code>(<*>)</code>. Composition allows reassociating <code>(<*>)</code>; interchange allows moving occurrences of <code>pure</code> leftwards; and homomorphism allows collapsing multiple adjacent occurrences of <code>pure</code> into one.<br />
<br />
There is also a law specifying how <code>Applicative</code> should relate to <code>Functor</code>:<br />
<br />
<haskell><br />
fmap g x = pure g <*> x<br />
</haskell><br />
<br />
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. Since <code>(<$>)</code> is a synonym for <code>fmap</code>, the above law can also be expressed as:<br />
<br />
<code>g <$> x = pure g <*> x</code>.<br />
<br />
{{Exercises|<br />
# (Tricky) One might imagine a variant of the interchange law that says something about applying a pure function to an effectful argument. Using the above laws, prove that<haskell>pure f <*> x = pure (flip ($)) <*> x <*> pure f</haskell><br />
}}<br />
<br />
==Instances==<br />
<br />
Most of the standard types which are instances of <code>Functor</code> are also instances of <code>Applicative</code>.<br />
<br />
<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.<br />
<br />
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]).<br />
<br />
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:<br />
<br />
<haskell><br />
newtype ZipList a = ZipList { getZipList :: [a] }<br />
<br />
instance Applicative ZipList where<br />
pure :: a -> ZipList a<br />
pure = undefined -- exercise<br />
<br />
(<*>) :: ZipList (a -> b) -> ZipList a -> ZipList b<br />
(ZipList gs) <*> (ZipList xs) = ZipList (zipWith ($) gs xs)<br />
</haskell><br />
<br />
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>. <br />
<br />
The other <code>Applicative</code> instance for lists, based on the nondeterministic computation point of view, is:<br />
<br />
<haskell><br />
instance Applicative [] where<br />
pure :: a -> [a]<br />
pure x = [x]<br />
<br />
(<*>) :: [a -> b] -> [a] -> [b]<br />
gs <*> xs = [ g x | g <- gs, x <- xs ]<br />
</haskell><br />
<br />
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.<br />
<br />
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<br />
<br />
<haskell><br />
pure (+) <*> [2,3,4] <*> pure 4<br />
</haskell><br />
<br />
or, more idiomatically,<br />
<br />
<haskell><br />
(+) <$> [2,3,4] <*> pure 4.<br />
</haskell><br />
<br />
There are several other <code>Applicative</code> instances as well:<br />
<br />
* <code>IO</code> is an instance of <code>Applicative</code>, and behaves exactly as you would think: to execute <code>m1 <*> m2</code>, first <code>m1</code> is executed, resulting in a function <code>f</code>, then <code>m2</code> is executed, resulting in a value <code>x</code>, and finally the value <code>f x</code> is returned as the result of executing <code>m1 <*> m2</code>.<br />
<br />
* <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.<br />
<br />
* 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]]).<br />
<br />
* 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.<br />
<br />
{{Exercises|<br />
# Implement an instance of <code>Applicative</code> for <code>Maybe</code>.<br />
# Determine the correct definition of <code>pure</code> for the <code>ZipList</code> instance of <code>Applicative</code>—there is only one implementation that satisfies the law relating <code>pure</code> and <code>(<*>)</code>.<br />
}}<br />
<br />
==Intuition==<br />
<br />
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.<br />
<br />
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.<br />
<br />
This suggests the proper translation of the idealized notation <math>[[g \; x_1 \; x_2 \; \cdots \; x_n]]\ </math> into Haskell, namely<br />
<haskell><br />
g <$> x1 <*> x2 <*> ... <*> xn,<br />
</haskell><br />
<br />
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.<br />
<br />
Note that <code>pure</code> allows embedding “non-effectful” arguments in the middle of an idiomatic application, like<br />
<haskell><br />
g <$> x1 <*> pure x2 <*> x3<br />
</haskell><br />
which has type <code>f d</code>, given<br />
<haskell><br />
g :: a -> b -> c -> d<br />
x1 :: f a<br />
x2 :: b<br />
x3 :: f c<br />
</haskell><br />
<br />
The double brackets are commonly known as “idiom brackets”, because they allow writing “idiomatic” function application, that is, function application that looks normal but has some special, non-standard meaning (determined by the particular instance of <code>Applicative</code> being used). Idiom brackets are not supported by GHC, but they are supported by the [http://personal.cis.strath.ac.uk/~conor/pub/she/ Strathclyde Haskell Enhancement], a preprocessor which (among many other things) translates idiom brackets into standard uses of <code>(<$>)</code> and <code>(<*>)</code>. This can result in much more readable code when making heavy use of <code>Applicative</code>.<br />
<br />
In addition, as of GHC 8, the <code>ApplicativeDo</code> extension enables <code>g <$> x1 <*> x2 <*> ... <*> xn</code> to be written in a different style:<br />
<haskell><br />
do v1 <- x1<br />
v2 <- x2<br />
...<br />
vn <- xn<br />
pure (g v1 v2 ... vn)<br />
</haskell><br />
See the Further Reading section below as well as the discussion of do-notation in the Monad section for more information.<br />
<br />
==Utility functions==<br />
<br />
<code>Control.Applicative</code> provides several utility functions that work generically with any <code>Applicative</code> instance.<br />
<br />
* <code>liftA :: Applicative f => (a -> b) -> f a -> f b</code>. This should be familiar; of course, it is the same as <code>fmap</code> (and hence also the same as <code>(<$>)</code>), but with a more restrictive type. This probably exists to provide a parallel to <code>liftA2</code> and <code>liftA3</code>, but there is no reason you should ever need to use it.<br />
<br />
* <code>liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c</code> lifts a 2-argument function to operate in the context of some <code>Applicative</code>. When <code>liftA2</code> is fully applied, as in <code>liftA2 f arg1 arg2</code>,it is typically better style to instead use <code>f <$> arg1 <*> arg2</code>. However, <code>liftA2</code> can be useful in situations where it is partially applied. For example, one could define a <code>Num</code> instance for <code>Maybe Integer</code> by defining <code>(+) = liftA2 (+)</code> and so on.<br />
<br />
* There is a <code>liftA3</code> but no <code>liftAn</code> for larger <code>n</code>.<br />
<br />
* <code>(*>) :: Applicative f => f a -> f b -> f b</code> sequences the effects of two <code>Applicative</code> computations, but discards the result of the first. For example, if <code>m1, m2 :: Maybe Int</code>, then <code>m1 *> m2</code> is <code>Nothing</code> whenever either <code>m1</code> or <code>m2</code> is <code>Nothing</code>; but if not, it will have the same value as <code>m2</code>.<br />
<br />
* Likewise, <code>(<*) :: Applicative f => f a -> f b -> f a</code> sequences the effects of two computations, but keeps only the result of the first, discarding the result of the second. Just as with <code>(<$)</code> and <code>($>)</code>, to keep <code>(<*)</code> and <code>(*>)</code> straight, remember that they point towards the values that will be kept.<br />
<br />
* <code>(<**>) :: Applicative f => f a -> f (a -> b) -> f b</code> is similar to <code>(<*>)</code>, but where the first computation produces value(s) which are provided as input to the function(s) produced by the second computation. Note this is not the same as <code>flip (<*>)</code>, because the effects are performed in the opposite order. This is possible to observe with any <code>Applicative</code> instance with non-commutative effects, such as the instance for lists: <code>(<**>) [1,2] [(+5),(*10)]</code> produces a different result than <code>(flip (<*>))</code> on the same arguments. <br />
<br />
* <code>when :: Applicative f => Bool -> f () -> f ()</code> conditionally executes a computation, evaluating to its second argument if the test is <code>True</code>, and to <code>pure ()</code> if the test is <code>False</code>.<br />
<br />
* <code>unless :: Applicative f => Bool -> f () -> f ()</code> is like <code>when</code>, but with the test negated.<br />
<br />
* The <code>guard</code> function is for use with instances of <code>Alternative</code> (an extension of <code>Applicative</code> to incorporate the ideas of failure and choice), which is discussed in the [[#Failure_and_choice:_Alternative.2C_MonadPlus.2C_ArrowPlus|section on <code>Alternative</code> and friends]].<br />
<br />
{{Exercises|<br />
# Implement a function <haskell>sequenceAL :: Applicative f => [f a] -> f [a]</haskell>. There is a generalized version of this, <code>sequenceA</code>, which works for any <code>Traversable</code> (see the later section on Traversable), but implementing this version specialized to lists is a good exercise.<br />
}}<br />
<br />
==Alternative formulation==<br />
<br />
An alternative, equivalent formulation of <code>Applicative</code> is given by<br />
<br />
<haskell><br />
class Functor f => Monoidal f where<br />
unit :: f ()<br />
(**) :: f a -> f b -> f (a,b)<br />
</haskell><br />
<br />
{{note|In category-theory speak, we say <code>f</code> is a ''lax'' monoidal functor because there aren't necessarily functions in the other direction, like <code>f (a, b) -> (f a, f b)</code>.}}<br />
Intuitively, this states that a <i>monoidal</i> functor{{noteref}} is one which has some sort of "default shape" and which supports some sort of "combining" operation. <code>pure</code> and <code>(<*>)</code> are equivalent in power to <code>unit</code> and <code>(**)</code> (see the Exercises below). More technically, the idea is that <code>f</code> preserves the "monoidal structure" given by the pairing constructor <code>(,)</code> and unit type <code>()</code>. This can be seen even more clearly if we rewrite the types of <code>unit</code> and <code>(**)</code> as<br />
<haskell><br />
unit' :: () -> f ()<br />
(**') :: (f a, f b) -> f (a, b)<br />
</haskell><br />
<br />
Furthermore, to deserve the name "monoidal" (see the [[#Monoid|section on Monoids]]), instances of <code>Monoidal</code> ought to satisfy the following laws, which seem much more straightforward than the traditional <code>Applicative</code> laws:<br />
<br />
{{note|In this and the following laws, <code>≅</code> refers to <i>isomorphism</i> rather than equality. In particular we consider <code>(x,()) ≅ x ≅ ((),x)</code> and <code>((x,y),z) ≅ (x,(y,z))</code>.}}<br />
* Left identity{{noteref}}: <haskell>unit ** v ≅ v</haskell><br />
* Right identity: <haskell>u ** unit ≅ u</haskell><br />
* Associativity: <haskell>u ** (v ** w) ≅ (u ** v) ** w</haskell><br />
<br />
These turn out to be equivalent to the usual <code>Applicative</code> laws. In a category theory setting, one would also require a naturality law:<br />
<br />
{{note|Here <code>g *** h {{=}} \(x,y) -> (g x, h y)</code>. See [[#Arrow|Arrows]].}}<br />
* Naturality: <haskell>fmap (g *** h) (u ** v) = fmap g u ** fmap h v</haskell><br />
<br />
but in the context of Haskell, this is a free theorem.<br />
<br />
Much of this section was taken from [http://blog.ezyang.com/2012/08/applicative-functors/ a blog post by Edward Z. Yang]; see his actual post for a bit more information.<br />
<br />
{{Exercises|<br />
# Implement <code>pure</code> and <code>(<*>)</code> in terms of <code>unit</code> and <code>(**)</code>, and vice versa.<br />
# Are there any <code>Applicative</code> instances for which there are also functions <code>f () -> ()</code> and <code>f (a,b) -> (f a, f b)</code>, satisfying some "reasonable" laws?<br />
# (Tricky) Prove that given your implementations from the first exercise, the usual <code>Applicative</code> laws and the <code>Monoidal</code> laws stated above are equivalent.<br />
}}<br />
<br />
==Further reading==<br />
<br />
[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.<br />
<br />
{{note|Introduced by [http://conal.net/papers/simply-reactive/ an earlier paper] that was since superseded by [http://conal.net/papers/push-pull-frp/ Push-pull functional reactive programming].}}<br />
<br />
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.<br />
<br />
Although the [http://hackage.haskell.org/package/parsec 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.<br />
<br />
A couple other nice examples of <code>Applicative</code> in action include the [http://web.archive.org/web/20090416111947/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].<br />
<br />
Gershom Bazerman's [http://comonad.com/reader/2012/abstracting-with-applicatives/ post] contains many insights into applicatives.<br />
<br />
The <code>ApplicativeDo</code> extension is described in [https://ghc.haskell.org/trac/ghc/wiki/ApplicativeDo this wiki page], and in more detail in [http://doi.org/10.1145/2976002.2976007 this Haskell Symposium paper].<br />
<br />
=Monad=<br />
<br />
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.<br />
<br />
* Haskell does, in fact, single out monads for special attention by making them the framework in which to construct I/O operations.<br />
* Haskell also singles out monads for special attention by providing a special syntactic sugar for monadic expressions: the <code>do</code>-notation. (As of GHC 8, <code>do</code>-notation can be used with <code>Applicative</code> as well, but the notation is still fundamentally related to monads.)<br />
* <code>Monad</code> has been around longer than other abstract models of computation such as <code>Applicative</code> or <code>Arrow</code>.<br />
* 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]).<br />
<br />
I will let you judge for yourself whether these are good reasons.<br />
<br />
In the end, despite all the hoopla, <code>Monad</code> is just another type class. Let’s take a look at its definition.<br />
<br />
==Definition==<br />
As of GHC 7.10, [{{HackageDocs|base|Prelude}}#t:Monad <code>Monad</code>] is defined as:<br />
<br />
<haskell><br />
class Applicative m => Monad m where<br />
return :: a -> m a<br />
(>>=) :: m a -> (a -> m b) -> m b<br />
(>>) :: m a -> m b -> m b<br />
m >> n = m >>= \_ -> n<br />
<br />
fail :: String -> m a<br />
</haskell><br />
<br />
(Prior to GHC 7.10, <code>Applicative</code> was not a superclass of <code>Monad</code>, for historical reasons.)<br />
<br />
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 [{{HackageDocs|base|Control-Monad}} <code>Control.Monad</code>].<br />
<br />
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.) For historical reasons, we still have both names, but they should always denote the same value (although this cannot be enforced). Likewise, <code>(>>)</code> should be the same as <code>(*>)</code> from <code>Applicative</code>. It is possible that <code>return</code> and <code>(>>)</code> may eventually be removed from the <code>Monad</code> class: see the [https://ghc.haskell.org/trac/ghc/wiki/Proposal/MonadOfNoReturn Monad of No Return proposal].<br />
<br />
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''.<br />
<br />
The <code>fail</code> function is an awful hack that has no place in the <code>Monad</code> class; more on this later.<br />
<br />
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''.<br />
<br />
We could spend a while talking about the intuition behind <code>(>>=)</code>—and we will. But first, let’s look at some examples.<br />
<br />
==Instances==<br />
<br />
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 [http://hackage.haskell.org/package/transformers <code>transformers</code> package].<br />
<br />
<ul><br />
<li>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.<br />
</li><br />
<li>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<br />
<br />
<haskell><br />
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b.<br />
</haskell><br />
<br />
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:<br />
<br />
<haskell><br />
instance Monad Maybe where<br />
return :: a -> Maybe a<br />
return = Just<br />
<br />
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b<br />
(Just x) >>= g = g x<br />
Nothing >>= _ = Nothing<br />
</haskell><br />
<br />
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.<br />
</li><br />
<br />
<li>The <code>Monad</code> instance for the list constructor <code>[]</code> is similar to its <code>Applicative</code> instance; see the exercise below.<br />
</li><br />
<br />
<li>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.<br />
</li><br />
<br />
<li>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.<br />
<br />
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).<br />
</li><br />
<br />
<li>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.<br />
</li><br />
<br />
<li>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).<br />
</li><br />
<br />
<li>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.<br />
</li><br />
</ul><br />
<br />
{{Exercises|<br />
<ol><br />
<li>Implement a <code>Monad</code> instance for the list constructor, <code>[]</code>. Follow the types!</li><br />
<li>Implement a <code>Monad</code> instance for <code>((->) e)</code>.</li><br />
<li>Implement <code>Functor</code> and <code>Monad</code> instances for <code>Free f</code>, defined as<br />
<haskell><br />
data Free f a = Var a<br />
| Node (f (Free f a))<br />
</haskell><br />
You may assume that <code>f</code> has a <code>Functor</code> instance. This is known as the ''free monad'' built from the functor <code>f</code>.<br />
</li><br />
</ol><br />
}}<br />
<br />
==Intuition==<br />
<br />
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 (actually, this is exactly the situation with <code>Applicative</code>). 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.<br />
<br />
{{note|Actually, because Haskell allows general recursion, one can recursively construct ''infinite'' grammars, and hence <code>Applicative</code> (together with <code>Alternative</code>) is enough to parse any context-sensitive language with a finite alphabet. See [http://byorgey.wordpress.com/2012/01/05/parsing-context-sensitive-languages-with-applicative/ Parsing context-sensitive languages with Applicative].}}<br />
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. This also means that parsers built using an <code>Applicative</code> interface can only parse context-free languages; in order to parse context-sensitive languages a <code>Monad</code> interface is needed.{{noteref}}<br />
<br />
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.<br />
<br />
{{note|1=You might hear some people claim that the definition in terms of <code>return</code>, <code>fmap</code>, and <code>join</code> is the “math definition” and the definition in terms of <code>return</code> and <code>(>>=)</code> is something specific to Haskell. In fact, both definitions were known in the mathematics community long before Haskell picked up monads.}}<br />
<br />
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>:<br />
<br />
<haskell><br />
class Applicative m => Monad'' m where<br />
join :: m (m a) -> m a<br />
</haskell><br />
<br />
In fact, the canonical definition of monads in category theory is 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 an alternative formulation with <code>(>>=)</code> instead of <code>join</code> since it is more convenient to use {{noteref}}. 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>.)<br />
<br />
{{Exercises|<br />
# Implement <code>(>>{{=}})</code> in terms of <code>fmap</code> (or <code>liftM</code>) and <code>join</code>.<br />
# Now implement <code>join</code> and <code>fmap</code> (<code>liftM</code>) in terms of <code>(>>{{=}})</code> and <code>return</code>.<br />
}}<br />
<br />
==Utility functions==<br />
<br />
The [{{HackageDocs|base|Control-Monad}} <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].<br />
<br />
* <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 a consequence of the fact that the <code>Monad</code> type class did not require a <code>Functor</code> instance until recently, even though mathematically speaking, every monad is a functor. If you are using GHC 7.10 or newer, you should avoid using <code>liftM</code> and just use <code>fmap</code> instead.<br />
<br />
* <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>.<br />
<br />
* <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> (see the exercise at the end of the Utility Functions section for Applicative). Note that the actual type of <code>sequence</code> is more general, and works over any <code>Traversable</code> rather than just lists; see the [[#Traversable|section on <code>Traversable</code>]].<br />
<br />
* <code>replicateM :: Monad m => Int -> m a -> m [a]</code> is simply a combination of [{{HackageDocs|base|Prelude}}#v:replicate <code>replicate</code>] and <code>sequence</code>.<br />
<br />
* <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. Again, these functions actually work over any <code>Traversable</code>, not just lists, and they can also be defined in terms of <code>Applicative</code>, not <code>Monad</code>: the analogue of <code>mapM</code> for <code>Applicative</code> is called <code>traverse</code>.<br />
<br />
* <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.<br />
<br />
* <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>.<br />
<br />
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.<br />
<br />
Other monadic functions which are occasionally useful include <code>filterM</code>, <code>zipWithM</code>, <code>foldM</code>, and <code>forever</code>.<br />
<br />
==Laws==<br />
<br />
There are several laws that instances of <code>Monad</code> should satisfy (see also the [[Monad laws]] wiki page). The standard presentation is:<br />
<br />
<haskell><br />
return a >>= k = k a<br />
m >>= return = m<br />
m >>= (\x -> k x >>= h) = (m >>= k) >>= h<br />
</haskell><br />
<br />
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.<br />
<br />
{{note|I like to pronounce this operator “fish”.}}<br />
<br />
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>. <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:<br />
<br />
<haskell><br />
return >=> g = g<br />
g >=> return = g<br />
(g >=> h) >=> k = g >=> (h >=> k)<br />
</haskell><br />
<br />
{{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.}}<br />
<br />
Ah, much better! The laws simply state that <code>return</code> is the identity of <code>(>=>)</code>, and that <code>(>=>)</code> is associative {{noteref}}.<br />
<br />
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].<br />
<br />
{{Exercises|<br />
# Given the definition <code>g >{{=}}> h {{=}} \x -> g x >>{{=}} h</code>, prove the equivalence of the above laws and the usual monad laws.<br />
}}<br />
<br />
==<code>do</code> notation==<br />
<br />
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:<br />
<br />
<haskell><br />
a >>= \x -><br />
b >><br />
c >>= \y -><br />
d<br />
</haskell><br />
<br />
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:<br />
<br />
<haskell><br />
do { x <- a<br />
; b<br />
; y <- c<br />
; d<br />
}<br />
</haskell><br />
<br />
(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:<br />
<br />
<pre><br />
do e → e<br />
do { e; stmts } → e >> do { stmts }<br />
do { v <- e; stmts } → e >>= \v -> do { stmts }<br />
do { let decls; stmts} → let decls in do { stmts }<br />
</pre><br />
<br />
This is not quite the whole story, since <code>v</code> might be a pattern instead of a variable. For example, one can write<br />
<br />
<haskell><br />
do (x:xs) <- foo<br />
bar x<br />
</haskell><br />
<br />
but what happens if <code>foo</code> is 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://www.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]].<br />
<br />
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.<br />
<br />
Sometimes, the full power of <code>Monad</code> is not needed to desugar <code>do</code>-notation. For example,<br />
<br />
<haskell><br />
do x <- foo1<br />
y <- foo2<br />
z <- foo3<br />
return (g x y z)<br />
</haskell><br />
<br />
would normally be desugared to <code>foo1 >>= \x -> foo2 >>= \y -> foo3 >>= \z -> return (g x y z)</code>, but this is equivalent to <code>g <$> foo1 <*> foo2 <*> foo3</code>. With the <code>ApplicativeDo</code> extension enabled (as of GHC 8.0), GHC tries hard to desugar <code>do</code>-blocks using <code>Applicative</code> operations wherever possible. This can sometimes lead to efficiency gains, even for types which also have <code>Monad</code> instances, since in general <code>Applicative</code> computations may be run in parallel, whereas monadic ones may not. For example, consider<br />
<br />
<haskell><br />
g :: Int -> Int -> M Int<br />
<br />
-- These could be expensive<br />
bar, baz :: M Int<br />
<br />
foo :: M Int<br />
foo = do<br />
x <- bar<br />
y <- baz<br />
g x y<br />
</haskell><br />
<br />
<code>foo</code> definitely depends on the <code>Monad</code> instance of <code>M</code>, since the effects generated by the whole computation may depend (via <code>g</code>) on the <code>Int</code> outputs of <code>bar</code> and <code>baz</code>. Nonetheless, with <code>ApplicativeDo</code> enabled, <code>foo</code> can be desugared as<br />
<haskell><br />
join (g <$> bar <*> baz)<br />
</haskell><br />
which may allow <code>bar</code> and <code>baz</code> to be computed in parallel, since they at least do not depend on each other.<br />
<br />
The <code>ApplicativeDo</code> extension is described in [https://ghc.haskell.org/trac/ghc/wiki/ApplicativeDo this wiki page], and in more detail in [http://doi.org/10.1145/2976002.2976007 this Haskell Symposium paper].<br />
<br />
==Further reading==<br />
<br />
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.<br />
<br />
{{note|1=<br />
[[All About Monads]],<br />
[http://www.haskell.org/haskellwiki/Monads_as_Containers Monads as containers],<br />
[http://en.wikibooks.org/w/index.php?title=Haskell/Understanding_monads Understanding monads],<br />
[[The Monadic Way]],<br />
[http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html You Could Have Invented Monads! (And Maybe You Already Have.)],<br />
[http://www.haskell.org/pipermail/haskell-cafe/2006-November/019190.html there’s a monster in my Haskell!],<br />
[http://kawagner.blogspot.com/2007/02/understanding-monads-for-real.html Understanding Monads. For real.],<br />
[http://www.randomhacks.net/articles/2007/03/12/monads-in-15-minutes Monads in 15 minutes: Backtracking and Maybe],<br />
[http://www.haskell.org/haskellwiki/Monads_as_computation Monads as computation],<br />
[http://metafoo.co.uk/practical-monads.txt Practical Monads]}}<br />
<br />
There are, of course, numerous monad tutorials of varying quality {{noteref}}.<br />
<br />
A few of the best include Cale Gibbard’s [http://www.haskell.org/haskellwiki/Monads_as_Containers Monads as containers] and [http://www.haskell.org/haskellwiki/Monads_as_computation Monads as computation]; Jeff Newbern’s [[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”].)<br />
<br />
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], Tim Newsham’s [http://www.thenewsh.com/~newsham/haskell/monad.html What’s a Monad?], and Chris Smith's excellent article [http://cdsmith.wordpress.com/2012/04/18/why-do-monads-matter/ Why Do Monads Matter?]. 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]].<br />
<br />
For help constructing monads from scratch, and for obtaining a "deep embedding" of monad operations suitable for use in, say, compiling a domain-specific language, see [http://projects.haskell.org/operational Apfelmus's operational package].<br />
<br />
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.<br />
<br />
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]].<br />
<br />
Monads can be generalized in various ways; for an exposition of one possibility, see Robert Atkey’s paper on [https://bentnib.org/paramnotions-jfp.pdf parameterized monads], or Dan Piponi’s [http://blog.sigfpe.com/2009/02/beyond-monads.html Beyond Monads].<br />
<br />
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’ 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>. Jonathan Hill and Keith Clarke have [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.53.6497 an early paper explaining the connection between monads as they arise in category theory and as used in functional programming]. There is also a [http://okmij.org/ftp/Computation/IO-monad-history.html web page by Oleg Kiselyov] explaining the history of the IO monad.<br />
<br />
Links to many more research papers related to monads can be found under [[Research papers/Monads and arrows]].<br />
<br />
=MonadFail=<br />
<br />
Some monads support a notion of ''failure'', without necessarily supporting the notion of ''recovery'' suggested by <code>MonadPlus</code>, and possibly including a primitive error reporting mechanism. This notion is expressed by the relatively unprincipled <code>MonadFail</code>. When the <code>MonadFailDesugaring</code> language extension is enabled, the <code>fail</code> method from <code>MonadFail</code> is used for pattern match failure in <code>do</code> bindings rather than the traditional <code>fail</code> method of the <code>Monad</code> class. This language change is being implemented because there are many monads, such as <code>Reader</code>, <code>State</code>, <code>Writer</code>, <code>RWST</code>, and <code>Cont</code> that simply do not support a legitimate <code>fail</code> method.<br />
<br />
See the [https://prime.haskell.org/wiki/Libraries/Proposals/MonadFail MonadFail proposal] for more information.<br />
<br />
==Definition==<br />
<br />
<haskell><br />
class Monad m => MonadFail m where<br />
fail :: String -> m a<br />
</haskell><br />
<br />
==Law==<br />
<br />
<haskell><br />
fail s >>= m = fail s<br />
</haskell><br />
<br />
=Monad transformers=<br />
<br />
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.<br />
<br />
==Standard monad transformers==<br />
<br />
The [http://hackage.haskell.org/package/transformers transformers] library provides a number of standard ''monad transformers''. Each monad transformer adds a particular capability/feature/effect to any existing monad.<br />
<br />
* [http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-Identity.html <code>IdentityT</code>] is the identity transformer, which maps a monad to (something isomorphic to) itself. This may seem useless at first glance, but it is useful for the same reason that the <code>id</code> function is useful -- it can be passed as an argument to things which are parameterized over an arbitrary monad transformer, when you do not actually want any extra capabilities.<br />
* [http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-State.html <code>StateT</code>] adds a read-write state.<br />
* [http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-Reader.html <code>ReaderT</code>] adds a read-only environment.<br />
* [http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-Writer.html <code>WriterT</code>] adds a write-only log.<br />
* [http://hackage.haskell.org/packages/archive/transformers/0.2.2.0/doc/html/Control-Monad-Trans-RWS.html <code>RWST</code>] conveniently combines <code>ReaderT</code>, <code>WriterT</code>, and <code>StateT</code> into one.<br />
* [http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-Maybe.html <code>MaybeT</code>] adds the possibility of failure.<br />
* [http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-Error.html <code>ErrorT</code>] adds the possibility of failure with an arbitrary type to represent errors.<br />
* [http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-List.html <code>ListT</code>] adds non-determinism (however, see the discussion of <code>ListT</code> below).<br />
* [http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-Cont.html <code>ContT</code>] adds continuation handling.<br />
<br />
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>. Monad 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 (indeed, it simply disappears); 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”; <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.)<br />
Intuitively, the monads become "more fundamental" the further inside the stack you get, and the effects of inner monads "have precedence" over the effects of outer ones. Of course, this is just handwaving, and if you are unsure of the proper order for some monads you wish to combine, there is no substitute for using <code>@unmtl</code> or simply trying out the various options.<br />
<br />
==Definition and laws==<br />
<br />
All monad transformers should implement the <code>MonadTrans</code> type class, defined in <code>Control.Monad.Trans.Class</code>:<br />
<br />
<haskell><br />
class MonadTrans t where<br />
lift :: Monad m => m a -> t m a<br />
</haskell><br />
<br />
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>.)<br />
<br />
<code>lift</code> must satisfy the laws<br />
<haskell><br />
lift . return = return<br />
lift (m >>= f) = lift m >>= (lift . f)<br />
</haskell><br />
which intuitively state that <code>lift</code> transforms <code>m a</code> computations into <code>t m a</code> computations in a "sensible" way, which sends the <code>return</code> and <code>(>>=)</code> of <code>m</code> to the <code>return</code> and <code>(>>=)</code> of <code>t m</code>.<br />
<br />
{{Exercises|<br />
# What is the kind of <code>t</code> in the declaration of <code>MonadTrans</code>?<br />
}}<br />
<br />
==Transformer type classes and "capability" style==<br />
<br />
{{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.}}<br />
<br />
There are also type classes (provided by the [http://hackage.haskell.org/package/mtl <code>mtl</code> package]) for the operations of each transformer. For example, the <code>MonadState</code> type class provides the state-specific methods <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}}.<br />
<br />
These type classes serve two purposes. First, they get rid of (most of) the need for explicitly using <code>lift</code>, giving a type-directed way to automatically determine the right number of calls to <code>lift</code>. Simply writing <code>put</code> will be automatically translated into <code>lift . put</code>, <code>lift . lift . put</code>, or something similar depending on what concrete monad stack you are using.<br />
<br />
Second, they give you more flexibility to switch between different concrete monad stacks. For example, if you are writing a state-based algorithm, don't write<br />
<haskell><br />
foo :: State Int Char<br />
foo = modify (*2) >> return 'x'<br />
</haskell><br />
but rather<br />
<haskell><br />
foo :: MonadState Int m => m Char<br />
foo = modify (*2) >> return 'x'<br />
</haskell><br />
Now, if somewhere down the line you realize you need to introduce the possibility of failure, you might switch from <code>State Int</code> to <code>MaybeT (State Int)</code>. The type of the first version of <code>foo</code> would need to be modified to reflect this change, but the second version of <code>foo</code> can still be used as-is.<br />
<br />
However, this sort of "capability-based" style (<i>e.g.</i> specifying that <code>foo</code> works for any monad with the "state capability") quickly runs into problems when you try to naively scale it up: for example, what if you need to maintain two independent states? A framework for solving this and related problems is described by Schrijvers and Olivera ([http://users.ugent.be/~tschrijv/Research/papers/icfp2011.pdf Monads, zippers and views: virtualizing the monad stack, ICFP 2011]) and is implemented in the [http://hackage.haskell.org/package/Monatron <code>Monatron</code> package].<br />
<br />
==Composing monads==<br />
<br />
Is the composition of two monads always a monad? As hinted previously, the answer is no.<br />
<br />
Since <code>Applicative</code> functors are closed under composition, the problem must lie with <code>join</code>. Indeed, suppose <code>m</code> and <code>n</code> are arbitrary monads; to make a monad out of their composition we would need to be able to implement<br />
<haskell><br />
join :: m (n (m (n a))) -> m (n a)<br />
</haskell><br />
but it is not clear how this could be done in general. The <code>join</code> method for <code>m</code> is no help, because the two occurrences of <code>m</code> are not next to each other (and likewise for <code>n</code>).<br />
<br />
However, one situation in which it can be done is if <code>n</code> ''distributes'' over <code>m</code>, that is, if there is a function<br />
<haskell><br />
distrib :: n (m a) -> m (n a)<br />
</haskell><br />
satisfying certain laws. See Jones and Duponcheel ([http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.42.2605 Composing Monads]); see also the [[#Traversable|section on Traversable]].<br />
<br />
For a much more in-depth discussion and analysis of the failure of monads to be closed under composition, see [http://stackoverflow.com/questions/13034229/concrete-example-showing-that-monads-are-not-closed-under-composition-with-proo?lq=1 this question on StackOverflow].<br />
<br />
{{Exercises|<br />
* Implement <code>join :: M (N (M (N a))) -> M (N a)</code>, given <code>distrib :: N (M a) -> M (N a)</code> and assuming <code>M</code> and <code>N</code> are instances of <code>Monad</code>.<br />
}}<br />
<br />
==Further reading==<br />
<br />
Much of the monad transformer library (originally [http://hackage.haskell.org/package/mtl <code>mtl</code>], now split between <code>mtl</code> and [http://hackage.haskell.org/package/transformers <code>transformers</code>]), 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’ 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.<br />
<br />
See [http://article.gmane.org/gmane.comp.lang.haskell.libraries/17139 Edward Kmett's mailing list message] for a description of the history and relationships among monad transformer packages (<code>mtl</code>, <code>transformers</code>, <code>monads-fd</code>, <code>monads-tf</code>).<br />
<br />
There are two excellent references on monad transformers. Martin Grabmüller’s [http://catamorph.de/publications/2004-10-01-monad-transformers.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].<br />
<br />
The <code>ListT</code> transformer from the <code>transformers</code> package comes with the caveat that <code>ListT m</code> is only a monad when <code>m</code> is ''commutative'', that is, when <code>ma >>= \a -> mb >>= \b -> foo</code> is equivalent to <code>mb >>= \b -> ma >>= \a -> foo</code> (i.e. the order of <code>m</code>'s effects does not matter). For one explanation why, see Dan Piponi's blog post [http://blog.sigfpe.com/2006/11/why-isnt-listt-monad.html "Why isn't <code><nowiki>ListT []</nowiki></code> a monad"]. For more examples, as well as a design for a version of <code>ListT</code> which does not have this problem, see [http://www.haskell.org/haskellwiki/ListT_done_right <code>ListT</code> done right].<br />
<br />
There is an alternative way to compose monads, using coproducts, as described by [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.8.3581 Lüth and Ghani]. This method is interesting but has not (yet?) seen widespread use. For a more recent alternative, see Kiselyov et al's [http://okmij.org/ftp/Haskell/extensible/exteff.pdf Extensible Effects: An Alternative to Monad Transformers].<br />
<br />
=MonadFix=<br />
<br />
''Note: <code>MonadFix</code> is included here for completeness (and because it is interesting) but seems not to be used much. Skipping this section on a first read-through is perfectly OK (and perhaps even recommended).''<br />
<br />
==<code>do rec</code> notation==<br />
<br />
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 (effectful) recursion. This is [http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#recursive-do-notation supported in GHC] by a special “recursive do” notation, enabled by the <code>-XRecursiveDo</code> flag. Within a <code>do</code> block, one may have a nested <code>rec</code> block, like so:<br />
<haskell><br />
do { x <- foo<br />
; rec { y <- baz<br />
; z <- bar<br />
; bob<br />
}<br />
; w <- frob<br />
}<br />
</haskell><br />
Normally (if we had <code>do</code> in place of <code>rec</code> in the above example), <code>y</code> would be in scope in <code>bar</code> and <code>bob</code> but not in <code>baz</code>, and <code>z</code> would be in scope only in <code>bob</code>. With the <code>rec</code>, however, <code>y</code> and <code>z</code> are both in scope in all three of <code>baz</code>, <code>bar</code>, and <code>bob</code>. A <code>rec</code> block is analogous to a <code>let</code> block such as<br />
<haskell><br />
let { y = baz<br />
; z = bar<br />
}<br />
in bob<br />
</haskell><br />
because, in Haskell, every variable bound in a <code>let</code>-block is in scope throughout the entire block. (From this point of view, Haskell's normal <code>do</code> blocks are analogous to Scheme's <code>let*</code> construct.)<br />
<br />
What could such a feature be used for? One of the motivating examples given in the original paper describing <code>MonadFix</code> (see below) is encoding circuit descriptions. A line in a <code>do</code>-block such as <br />
<haskell><br />
x <- gate y z<br />
</haskell><br />
describes a gate whose input wires are labeled <code>y</code> and <code>z</code> and whose output wire is labeled <code>x</code>. Many (most?) useful circuits, however, involve some sort of feedback loop, making them impossible to write in a normal <code>do</code>-block (since some wire would have to be mentioned as an input ''before'' being listed as an output). Using a <code>rec</code> block solves this problem.<br />
<br />
==Examples and intuition==<br />
<br />
Of course, not every monad supports such recursive binding. However, as mentioned above, it suffices to have an implementation of <code>mfix :: (a -> m a) -> m a</code>, satisfying a few laws. Let's try implementing <code>mfix</code> for the <code>Maybe</code> monad. That is, we want to implement a function<br />
<haskell><br />
maybeFix :: (a -> Maybe a) -> Maybe a<br />
</haskell><br />
{{note|Actually, <code>fix</code> is implemented slightly differently for efficiency reasons; but the given definition is equivalent and simpler for the present purpose.}}<br />
Let's think for a moment about the implementation {{noteref}} of the non-monadic <code>fix :: (a -> a) -> a</code>:<br />
<haskell><br />
fix f = f (fix f)<br />
</haskell><br />
Inspired by <code>fix</code>, our first attempt at implementing <code>maybeFix</code> might be something like<br />
<haskell><br />
maybeFix :: (a -> Maybe a) -> Maybe a<br />
maybeFix f = maybeFix f >>= f<br />
</haskell><br />
This has the right type. However, something seems wrong: there is nothing in particular here about <code>Maybe</code>; <code>maybeFix</code> actually has the more general type <code>Monad m => (a -> m a) -> m a</code>. But didn't we just say that not all monads support <code>mfix</code>?<br />
<br />
The answer is that although this implementation of <code>maybeFix</code> has the right type, it does ''not'' have the intended semantics. If we think about how <code>(>>=)</code> works for the <code>Maybe</code> monad (by pattern-matching on its first argument to see whether it is <code>Nothing</code> or <code>Just</code>) we can see that this definition of <code>maybeFix</code> is completely useless: it will just recurse infinitely, trying to decide whether it is going to return <code>Nothing</code> or <code>Just</code>, without ever even so much as a glance in the direction of <code>f</code>.<br />
<br />
The trick is to simply ''assume'' that <code>maybeFix</code> will return <code>Just</code>, and get on with life!<br />
<haskell><br />
maybeFix :: (a -> Maybe a) -> Maybe a<br />
maybeFix f = ma<br />
where ma = f (fromJust ma)<br />
</haskell><br />
This says that the result of <code>maybeFix</code> is <code>ma</code>, and assuming that <code>ma = Just x</code>, it is defined (recursively) to be equal to <code>f x</code>.<br />
<br />
Why is this OK? Isn't <code>fromJust</code> almost as bad as <code>unsafePerformIO</code>? Well, usually, yes. This is just about the only situation in which it is justified! The interesting thing to note is that <code>maybeFix</code> ''will never crash'' -- although it may, of course, fail to terminate. The only way we could get a crash is if we try to evaluate <code>fromJust ma</code> when we know that <code>ma = Nothing</code>. But how could we know <code>ma = Nothing</code>? Since <code>ma</code> is defined as <code>f (fromJust ma)</code>, it must be that this expression has already been evaluated to <code>Nothing</code> -- in which case there is no reason for us to be evaluating <code>fromJust ma</code> in the first place! <br />
<br />
To see this from another point of view, we can consider three possibilities. First, if <code>f</code> outputs <code>Nothing</code> without looking at its argument, then <code>maybeFix f</code> clearly returns <code>Nothing</code>. Second, if <code>f</code> always outputs <code>Just x</code>, where <code>x</code> depends on its argument, then the recursion can proceed usefully: <code>fromJust ma</code> will be able to evaluate to <code>x</code>, thus feeding <code>f</code>'s output back to it as input. Third, if <code>f</code> tries to use its argument to decide whether to output <code>Just</code> or <code>Nothing</code>, then <code>maybeFix f</code> will not terminate: evaluating <code>f</code>'s argument requires evaluating <code>ma</code> to see whether it is <code>Just</code>, which requires evaluating <code>f (fromJust ma)</code>, which requires evaluating <code>ma</code>, ... and so on.<br />
<br />
There are also instances of <code>MonadFix</code> for lists (which works analogously to the instance for <code>Maybe</code>), for <code>ST</code>, and for <code>IO</code>. The [http://hackage.haskell.org/packages/archive/base/latest/doc/html/System-IO.html#fixIO instance for <code>IO</code>] is particularly amusing: it creates a new (empty) <code>MVar</code>, immediately reads its contents using <code>unsafeInterleaveIO</code> (which delays the actual reading lazily until the value is needed), uses the contents of the <code>MVar</code> to compute a new value, which it then writes back into the <code>MVar</code>. It almost seems, spookily, that <code>mfix</code> is sending a value back in time to itself through the <code>MVar</code> -- though of course what is really going on is that the reading is delayed just long enough (via <code>unsafeInterleaveIO</code>) to get the process bootstrapped.<br />
<br />
{{Exercises|<br />
* Implement a <code>MonadFix</code> instance for <code>[]</code>.<br />
}}<br />
<br />
==<code>mdo</code> syntax==<br />
<br />
The example at the start of this section can also be written<br />
<br />
<haskell><br />
mdo { x <- foo<br />
; y <- baz<br />
; z <- bar<br />
; bob<br />
; w <- frob<br />
}<br />
</haskell><br />
<br />
which will be translated into the original example (assuming that, say, <code>bar</code> and <code>bob</code> refer to <code>y</code>. The difference is that <code>mdo</code> will analyze the code in order to find minimal recursive blocks, which will be placed in <code>rec</code> blocks, whereas <code>rec</code> blocks desugar directly into calls to <code>mfix</code> without any further analysis.<br />
<br />
==Further reading==<br />
<br />
For more information (such as the precise desugaring rules for <code>rec</code> blocks), see Levent Erkök and John Launchbury's 2002 Haskell workshop paper, [http://sites.google.com/site/leventerkok/recdo.pdf?attredirects=0 A Recursive do for Haskell], or for full details, 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]. (Note, while reading, that <code>MonadFix</code> used to be called <code>MonadRec</code>.) You can also read the [http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#recursive-do-notation GHC user manual section on recursive do-notation].<br />
<br />
=Semigroup=<br />
<br />
A semigroup is a set <math>S\ </math> together with a binary operation <math>\oplus\ </math> which<br />
combines elements from <math>S\ </math>. The <math>\oplus\ </math> operator is required to be associative<br />
(that is, <math>(a \oplus b) \oplus c = a \oplus (b \oplus c)\ </math>, for any<br />
<math>a,b,c\ </math> which are elements of <math>S\ </math>).<br />
<br />
For example, the natural numbers under addition form a semigroup: the sum of any two natural numbers is a natural number, and <math>(a+b)+c = a+(b+c)\ </math> for any natural numbers <math>a\ </math>, <math>b\ </math>, and <math>c\,\ </math>. The integers under multiplication also form a semigroup, as do the integers (or rationals, or reals) under <math>\max\ </math> or <math>\min\ </math>, Boolean values under conjunction and disjunction, lists under concatenation, functions from a set to itself under composition ... Semigroups show up all over the place, once you know to look for them.<br />
<br />
==Definition==<br />
<br />
As of version 4.9 of the <code>base</code> package (which comes with GHC 8.0), semigroups are defined in the <code>Data.Semigroup</code> module. (If you are working with a previous version of base, or want to write a library that supports previous versions of base, you can use the <code>semigroups</code> package.)<br />
<br />
The definition of the <code>Semigroup</code> type class ([https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Semigroup.html haddock]) is as follows:<br />
<br />
<haskell><br />
class Semigroup a where<br />
(<>) :: a -> a -> a<br />
<br />
sconcat :: NonEmpty a -> a<br />
sconcat = sconcat (a :| as) = go a as where<br />
go b (c:cs) = b <> go c cs<br />
go b [] = b<br />
<br />
stimes :: Integral b => b -> a -> a<br />
stimes = ...<br />
</haskell><br />
<br />
The really important method is <code>(<>)</code>, representing the associative binary operation. The other two methods have default implementations in terms of <code>(<>)</code>, and are included in the type class in case some instances can give more efficient implementations than the default.<br />
<br />
<code>sconcat</code> reduces a nonempty list using <code>(<>)</code>. For most instances, this is the same as <code>foldr1 (<>)</code>, but it can be constant-time for idempotent semigroups.<br />
<br />
<code>stimes n</code> is equivalent to (but sometimes considerably more efficient than) <code>sconcat . replicate n</code>. Its default definition uses multiplication by doubling (also known as exponentiation by squaring). For many semigroups, this is an important optimization; for some, such as lists, it is terrible and must be overridden.<br />
<br />
See the [https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Semigroup.html haddock documentation] for more information on <code>sconcat</code> and <code>stimes</code>.<br />
<br />
==Laws==<br />
<br />
The only law is that <code>(<>)</code> must be associative:<br />
<br />
<haskell><br />
(x <> y) <> z = x <> (y <> z)<br />
</haskell><br />
<br />
=Monoid=<br />
<br />
Many semigroups have a special element <math>e</math> for which the binary operation <math>\oplus</math> is the identity, that is, <math>e \oplus x = x \oplus e = x</math> for every element <math>x</math>. Such a semigroup-with-identity-element is called a ''monoid''.<br />
<br />
==Definition==<br />
<br />
The definition of the <code>Monoid</code> type class (defined in<br />
<code>Data.Monoid</code>; [{{HackageDocs|base|Data-Monoid}} haddock]) is:<br />
<br />
<haskell><br />
class Monoid a where<br />
mempty :: a<br />
mappend :: a -> a -> a<br />
<br />
mconcat :: [a] -> a<br />
mconcat = foldr mappend mempty<br />
</haskell><br />
<br />
The <code>mempty</code> value specifies the identity element of the monoid, and <code>mappend</code><br />
is the binary operation. The default definition for <code>mconcat</code><br />
“reduces” a list of elements by combining them all with <code>mappend</code>,<br />
using a right fold. It is only in the <code>Monoid</code> class so that specific<br />
instances have the option of providing an alternative, more efficient<br />
implementation; usually, you can safely ignore <code>mconcat</code> when creating<br />
a <code>Monoid</code> instance, since its default definition will work just fine.<br />
<br />
The <code>Monoid</code> methods are rather unfortunately named; they are inspired<br />
by the list instance of <code>Monoid</code>, where indeed <code>mempty = []</code> and <code>mappend = (++)</code>, but this is misleading since many<br />
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). The situation is made somewhat better by <code>(<>)</code>, which is provided as an alias for <code>mappend</code>.<br />
<br />
Note that the <code>(<>)</code> alias for <code>mappend</code> conflicts with the <code>Semigroup</code> method of the same name. For this reason, <code>Data.Semigroup</code> re-exports much of <code>Data.Monoid</code>; to use semigroups and monoids together, just import <code>Data.Semigroup</code>, and make sure all your types have both <code>Semigroup</code> and <code>Monoid</code> instances (and that <code>(<>) = mappend</code>).<br />
<br />
==Laws==<br />
<br />
Of course, every <code>Monoid</code> instance should actually be a monoid in the<br />
mathematical sense, which implies these laws:<br />
<br />
<haskell><br />
mempty `mappend` x = x<br />
x `mappend` mempty = x<br />
(x `mappend` y) `mappend` z = x `mappend` (y `mappend` z)<br />
</haskell><br />
<br />
==Instances==<br />
<br />
There are quite a few interesting <code>Monoid</code> instances defined in <code>Data.Monoid</code>.<br />
<br />
<ul><br />
<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><br />
<br />
<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.<br />
<br />
<haskell><br />
> getSum (mconcat . map Sum $ [1..5])<br />
15<br />
> getProduct (mconcat . map Product $ [1..5])<br />
120<br />
</haskell><br />
<br />
This example code is silly, of course; we could just write<br />
<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 on <code>Foldable</code>]].</li><br />
<br />
<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><br />
<br />
<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><br />
<br />
<li><code>Endo a</code> is a newtype wrapper for functions <code>a -> a</code>, which form a monoid under composition.</li><br />
<br />
<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><br />
<br />
<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><br />
<br />
<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><br />
</ul><br />
<br />
<code>Monoid</code> is also used to enable several other type class instances.<br />
As noted previously, we can use <code>Monoid</code> to make <code>((,) e)</code> an instance of <code>Applicative</code>:<br />
<br />
<haskell><br />
instance Monoid e => Applicative ((,) e) where<br />
pure :: Monoid e => a -> (e,a)<br />
pure x = (mempty, x)<br />
<br />
(<*>) :: Monoid e => (e,a -> b) -> (e,a) -> (e,b)<br />
(u, f) <*> (v, x) = (u `mappend` v, f x)<br />
</haskell><br />
<br />
<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.<br />
<br />
<code>Monoid</code> also plays a key role in the <code>Foldable</code> type class (see section [[#Foldable|Foldable]]).<br />
<br />
==Further reading==<br />
<br />
Monoids got a fair bit of attention in 2009, when<br />
[http://blog.enfranchisedmind.com/2009/01/random-thoughts-on-haskell/ a blog post by Brian Hurt]<br />
complained about the fact that the names of many Haskell type classes<br />
(<code>Monoid</code> in particular) are taken from abstract mathematics. This<br />
resulted in [http://thread.gmane.org/gmane.comp.lang.haskell.cafe/50590 a long Haskell-cafe thread]<br />
arguing the point and discussing monoids in general.<br />
<br />
{{note|May its name live forever.}}<br />
<br />
However, this was quickly followed by several blog posts about<br />
<code>Monoid</code> {{noteref}}. First, Dan Piponi<br />
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<br />
Heinrich Apfelmus’ [http://apfelmus.nfshost.com/monoid-fingertree.html Monoids and Finger Trees], an accessible exposition of<br />
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<br />
use of <code>Monoid</code> to implement an elegant and generic data structure.<br />
Dan Piponi then wrote two fascinating articles about using <code>Monoids</code><br />
(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]<br />
<br />
In a similar vein, David Place’s article on improving <code>Data.Map</code> in<br />
order to compute incremental folds (see [http://www.haskell.org/wikiupload/6/6a/TMR-Issue11.pdf the Monad Reader issue 11])<br />
is also a<br />
good example of using <code>Monoid</code> to generalize a data structure.<br />
<br />
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://izbicki.me/blog/gausian-distributions-are-monoids combining probability distributions], and a brilliant series of posts by Chung-Chieh Shan and Dylan Thurston 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/WordNumbers3/ part 3], [http://conway.rutgers.edu/~ccshan/wiki/blog/posts/WordNumbers4/ part 4]).<br />
<br />
As unlikely as it sounds, monads can actually be viewed as a sort of<br />
monoid, with <code>join</code> playing the role of the binary operation and<br />
<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].<br />
<br />
=Failure and choice: Alternative, MonadPlus, ArrowPlus=<br />
<br />
Several classes (<code>Applicative</code>, <code>Monad</code>, <code>Arrow</code>) have "monoidal" subclasses, intended to model computations that support "failure" and "choice" (in some appropriate sense).<br />
<br />
==Definition==<br />
<br />
The <code>Alternative</code> type class ([{{HackageDocs|base|Control-Applicative}}#g:2 haddock])<br />
is for <code>Applicative</code> functors which also have<br />
a monoid structure:<br />
<br />
<haskell><br />
class Applicative f => Alternative f where<br />
empty :: f a<br />
(<|>) :: f a -> f a -> f a<br />
<br />
some :: f a -> f [a]<br />
many :: f a -> f [a]<br />
</haskell><br />
<br />
The basic intuition is that <code>empty</code> represents some sort of "failure", and <code>(<|>)</code> represents a choice between alternatives. (However, this intuition does not fully capture the nuance possible; see the section on Laws below.) Of course, <code>(<|>)</code> should be associative and <code>empty</code> should be the identity element for it.<br />
Instances of <code>Alternative</code> must implement <code>empty</code> and <code>(&lt;|&gt;)</code>; <code>some</code> and <code>many</code> have default implementations but are included in the class since specialized implementations may be more efficient than the default.<br />
<br />
The default definitions of <code>some</code> and <code>many</code> are essentially given by<br />
<br />
<haskell><br />
some v = (:) <$> v <*> many v<br />
many v = some v <|> pure []<br />
</haskell><br />
<br />
(though for some reason, in actual fact they are not defined via mutual recursion). The intuition is that both keep running <code>v</code>, collecting its results into a list, until it fails; <code>some v</code> requires <code>v</code> to succeed at least once, whereas <code>many v</code> does not require it to succeed at all. That is, <code>many</code> represents 0 or more repetitions of <code>v</code>, whereas <code>some</code> represents 1 or more repetitions. Note that <code>some</code> and <code>many</code> do not make sense for all instances of <code>Alternative</code>; they are discussed further below.<br />
<br />
Likewise, <code>MonadPlus</code> ([{{HackageDocs|base|Control-Monad}}#t:MonadPlus haddock])<br />
is for <code>Monad</code>s with a monoid structure:<br />
<br />
<haskell><br />
class Monad m => MonadPlus m where<br />
mzero :: m a<br />
mplus :: m a -> m a -> m a<br />
</haskell><br />
<br />
Finally, <code>ArrowZero</code> and <code>ArrowPlus</code> ([{{HackageDocs|base|Control-Arrow}}#t:ArrowZero haddock])<br />
represent <code>Arrow</code>s ([[#Arrow|see below]]) with a<br />
monoid structure:<br />
<br />
<haskell><br />
class Arrow arr => ArrowZero arr where<br />
zeroArrow :: b `arr` c<br />
<br />
class ArrowZero arr => ArrowPlus arr where<br />
(<+>) :: (b `arr` c) -> (b `arr` c) -> (b `arr` c)<br />
</haskell><br />
<br />
==Instances==<br />
<br />
Although this document typically discusses laws before presenting example instances, for <code>Alternative</code> and friends it is worth doing things the other way around, because there is some controversy over the laws and it helps to have some concrete examples in mind when discussing them. We mostly focus on <code>Alternative</code> in this section and the next; now that <code>Applicative</code> is a superclass of <code>Monad</code>, there is little reason to use <code>MonadPlus</code> any longer, and <code>ArrowPlus</code> is rather obscure.<br />
<br />
* <code>Maybe</code> is an instance of <code>Alternative</code>, where <code>empty</code> is <code>Nothing</code> and the choice operator <code>(<|>)</code> results in its first argument when it is <code>Just</code>, and otherwise results in its second argument. Hence folding over a list of <code>Maybe</code> with <code>(<|>)</code> (which can be done with <code>asum</code> from <code>Data.Foldable</code>) results in the first non-<code>Nothing</code> value in the list (or <code>Nothing</code> if there are none).<br />
<br />
* <code>[]</code> is an instance, with <code>empty</code> given by the empty list, and <code>(<|>)</code> equal to <code>(++)</code>. It is worth pointing out that this is identical to the <code>Monoid</code> instance for <code>[a]</code>, whereas the <code>Alternative</code> and <code>Monoid</code> instances for <code>Maybe</code> are different: the <code>Monoid</code> instance for <code>Maybe a</code> requires a <code>Monoid</code> instance for <code>a</code>, and monoidally combines the contained values when presented with two <code>Just</code>s.<br />
<br />
Let's think about the behavior of <code>some</code> and <code>many</code> for <code>Maybe</code> and <code>[]</code>. For <code>Maybe</code>, we have <code>some Nothing = (:) <$> Nothing <*> many Nothing = Nothing <*> many Nothing = Nothing</code>. Hence we also have <code>many Nothing = some Nothing <|> pure [] = Nothing <|> pure [] = pure [] = Just []</code>. Boring. But what about applying <code>some</code> and <code>many</code> to <code>Just</code>? In fact, <code>some (Just a)</code> and <code>many (Just a)</code> are both bottom! The problem is that since <code>Just a</code> is always "successful", the recursion will never terminate. In theory the result "should be" the infinite list <code>[a,a,a,...]</code> but it cannot even start producing any elements of this list, because there is no way for the <code>(<*>)</code> operator to yield any output until it knows that the result of the call to <code>many</code> will be <code>Just</code>.<br />
<br />
You can work out the behavior for <code>[]</code> yourself, but it ends up being quite similar: <code>some</code> and <code>many</code> yield boring results when applied to the empty list, and yield bottom when applied to any non-empty list.<br />
<br />
In the end, <code>some</code> and <code>many</code> really only make sense when used with some sort of "stateful" <code>Applicative</code> instance, for which an action <code>v</code>, when run multiple times, can succeed some finite number of times and then fail. For example, parsers have this behavior, and indeed, parsers were the original motivating example for the <code>some</code> and <code>many</code> methods; more on this below.<br />
<br />
* Since GHC 8.0 (that is, <code>base-4.9</code>), there is an instance of <code>Alternative</code> for <code>IO</code>. <code>empty</code> throws an I/O exception, and <code>(<|>)</code> works by first running its left-hand argument; if the left-hand argument throws an I/O exception, <code>(<|>)</code> catches the exception and then calls its second argument. (Note that other types of exceptions are not caught.) There are other, much better ways to handle I/O errors, but this is a quick and dirty way that may work for simple, one-off programs, such as expressions typed at the GHCi prompt. For example, if you want to read the contents of a file but use some default contents in case the file does not exist, you can just write <code>readFile "somefile.txt" <|> return "default file contents"</code>.<br />
<br />
* <code>Concurrently</code> from the <code>async</code> package has an <code>Alternative</code> instance, for which <code>c1 <|> c2</code> races <code>c1</code> and <code>c2</code> in parallel, and returns the result of whichever finishes first. <code>empty</code> corresponds to the action that runs forever without returning a value.<br />
<br />
* Practically any parser type (e.g. from <code>parsec</code>, <code>megaparsec</code>, <code>trifecta</code>, ...) has an <code>Alternative</code> instance, where <code>empty</code> is an unconditional parse failure, and <code>(<|>)</code> is left-biased choice. That is, <code>p1 <|> p2</code> first tries parsing with <code>p1</code>, and if <code>p1</code> fails then it tries <code>p2</code> instead.<br />
<br />
<code>some</code> and <code>many</code> work particularly well with parser types having an <code>Applicative</code> instance: if <code>p</code> is a parser, then <code>some p</code> parses one or more consecutive occurrences of <code>p</code> (i.e. it will parse as many occurrences of <code>p</code> as possible and then stop), and <code>many p</code> parses zero or more occurrences.<br />
<br />
==Laws==<br />
<br />
Of course, instances of <code>Alternative</code> should satisfy the monoid laws<br />
<br />
<haskell><br />
empty <|> x = x<br />
x <|> empty = x<br />
(x <|> y) <|> z = x <|> (y <|> z)<br />
</haskell><br />
<br />
The documentation for <code>some</code> and <code>many</code> states that they should be the "least solution" (i.e. least in the definedness partial order) to their characterizing, mutually recursive default definitions. However, [https://www.reddit.com/r/haskell/comments/2j8bvl/laws_of_some_and_many/ this is controversial], and probably wasn't really thought out very carefully.<br />
<br />
Since <code>Alternative</code> is a subclass of <code>Applicative</code>, a natural question is, "how should <code>empty</code> and <code>(<|>)</code> interact with <code>(<*>)</code> and <code>pure</code>?"<br />
<br />
First of all, everyone agrees on the ''left zero'' law:<br />
<br />
<haskell><br />
empty <*> f = empty<br />
</haskell><br />
<br />
After this is where it starts to get a bit hairy though. It turns out there are several other laws one might imagine adding, and different instances satisfy different laws.<br />
<br />
* ''Right Zero'':<p>Another obvious law would be <haskell>f <*> empty = empty</haskell></p><p>This law is satisfied by most instances; however, it is not satisfied by <code>IO</code>. Once the effects in <code>f</code> have been executed, there is no way to roll them back if we later encounter an exception.</p><br />
<br />
* ''Left Distribution'':<p><haskell>(a <|> b) <*> c = (a <*> c) <|> (b <*> c)</haskell></p><p>This distributivity law is satisfied by <code>[]</code> and <code>Maybe</code>, as you may verify. However, it is ''not'' satisfied by <code>IO</code> or most parsers. The reason is that <code>a</code> and <code>b</code> can have effects which influence execution of <code>c</code>, and the left-hand side may end up failing where the right-hand side succeeds.</p><p>For example, consider <code>IO</code>, and suppose that <code>a</code> always executes successfully, but <code>c</code> throws an I/O exception after <code>a</code> has run. Concretely, say, <code>a</code> might ensure that a certain file does not exist (deleting it if it does exist or doing nothing if it does not), and then <code>c</code> tries to read that file. In that case <code>(a <|> b) <*> c</code> will first delete the file, ignoring <code>b</code> since <code>a</code> is successful, and then throw an exception when <code>c</code> tries to read the file. On the other hand, <code>b</code> might ensure that the same file in question ''does'' exist. In that case <code>(a <*> c) <|> (b <*> c)</code> would succeed: after <code>(a <*> c)</code> throws an exception, it would be caught by <code>(<|>)</code>, and then <code>(b <*> c)</code> would be tried.</p><p>This law does not hold for parsers for a similar reason: <code>(a <|> b) <*> c</code> has to "commit" to parsing with <code>a</code> or <code>b</code> before running <code>c</code>, whereas <code>(a <*> c) <|> (b <*> c)</code> allows backtracking if <code>a <*> c</code> fails. In the particular case that <code>a</code> succeeds but <code>c</code> fails after <code>a</code> but not after <code>b</code>, these may give different results. For example, suppose <code>a</code> and <code>c</code> both expect to see two asterisks, but <code>b</code> expects to see only one. If there are only three asterisks in the input, <code>b <*> c</code> will be successful whereas <code>a <*> c</code> will not.</p><br />
<br />
* ''Right Distribution'':<p><haskell>a <*> (b <|> c) = (a <*> b) <|> (a <*> c)</haskell></p><p>This law is not satisfied by very many instances, but it's still worth discussing. In particular the law is still satisfied by <code>Maybe</code>. However, it is ''not'' satisfied by, for example, lists. The problem is that the results come out in a different order. For example, suppose <code>a = [(+1), (*10)]</code>, <code>b = [2]</code>, and <code>c = [3]</code>. Then the left-hand side yields <code>[3,4,20,30]</code>, whereas the right-hand side is <code>[3,20,4,30]</code>.</p><p><code>IO</code> does not satisfy it either, since, for example, <code>a</code> may succeed only the ''second'' time it is executed. Parsers, on the other hand, may or may not satisfy this law, depending on how they handle backtracking. Parsers for which <code>(<|>)</code> by itself does full backtracking will satisfy the law; but for many parser combinator libraries this is not the case, for efficiency reasons. For example, parsec fails this law: if <code>a</code> succeeds while consuming some input, and afterwards <code>b</code> fails without consuming any input, then the left-hand side may succeed while the right-hand side fails: after <code>(a <*> b)</code> fails, the right-hand side tries to re-run <code>a</code> without backtracking over the input the original <code>a</code> consumed.</p><br />
<br />
* ''Left Catch'':<p><haskell>(pure a) <|> x = pure a</haskell></p><p>Intuitively, this law states that <code>pure</code> should always represent a "successful" computation. It is satisfied by <code>Maybe</code>, <code>IO</code>, and parsers. However, it is not satisfied by lists, since lists collect ''all'' possible results: it corresponds to <code>[a] ++ x == [a]</code> which is obviously false.</p><br />
<br />
This, then, is the situation: we have a lot of instances of <code>Alternative</code> (and <code>MonadPlus</code>), with each instance satisfying some ''subset'' of these laws. Moreover, it's not always the ''same'' subset, so there is no obvious "default" set of laws to choose. For now at least, we just have to live with the situation. When using a particular instance of <code>Alternative</code> or <code>MonadPlus</code>, it's worth thinking carefully about which laws it satisfies.<br />
<br />
==Utility functions==<br />
<br />
There are a few <code>Alternative</code>-specific utility functions worth mentioning:<br />
<br />
* <haskell>guard :: Alternative f => Bool -> f ()</haskell> checks the given condition, and evaluates to <code>pure ()</code> if the condition holds, and <code>empty</code> if not. This can be used to create a conditional failure point in the middle of a computation, where the computation only proceeds if a certain condition holds.<br />
<br />
* <haskell>optional :: Alternative f => f a -> f (Maybe a)</haskell> reifies potential failure into the <code>Maybe</code> type: that is, <code>optional x</code> is a computation which always succeeds, returning <code>Nothing</code> if <code>x</code> fails and <code>Just a</code> if <code>x</code> successfully results in <code>a</code>. It is useful, for example, in the context of parsers, where it corresponds to a production which can occur zero or one times.<br />
<br />
==Further reading==<br />
<br />
There used to be a type class called <code>MonadZero</code> containing only<br />
<code>mzero</code>, representing monads with failure. The <code>do</code>-notation requires<br />
some notion of failure to deal with failing pattern matches.<br />
Unfortunately, <code>MonadZero</code> was scrapped in favor of adding the <code>fail</code><br />
method to the <code>Monad</code> class. If we are lucky, someday <code>MonadZero</code> will<br />
be restored, and <code>fail</code> will be banished to the bit bucket where it<br />
belongs (see [[MonadPlus reform proposal]]). The idea is that any<br />
<code>do</code>-block which uses pattern matching (and hence may fail) would require<br />
a <code>MonadZero</code> constraint; otherwise, only a <code>Monad</code> constraint would be<br />
required.<br />
<br />
A great 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].<br />
<br />
Another interesting use of <code>MonadPlus</code> can be found in Christiansen et al, [http://www-ps.informatik.uni-kiel.de/~sad/icfp2016-preprint.pdf All Sorts of Permutations], from ICFP 2016.<br />
<br />
The [https://hackage.haskell.org/package/logict logict package] defines a type with prominent <code>Alternative</code> and <code>MonadPlus</code> instances that can be used to efficiently enumerate possibilities subject to constraints, ''i.e.'' logic programming; it's like the list monad on steroids.<br />
<br />
=Foldable=<br />
<br />
The <code>Foldable</code> class, defined in the <code>Data.Foldable</code><br />
module ([{{HackageDocs|base|Data-Foldable}} haddock]), abstracts over containers which can be<br />
“folded” into a summary value. This allows such folding operations<br />
to be written in a container-agnostic way.<br />
<br />
==Definition==<br />
<br />
The definition of the <code>Foldable</code> type class is:<br />
<br />
<haskell><br />
class Foldable t where<br />
fold :: Monoid m => t m -> m<br />
foldMap :: Monoid m => (a -> m) -> t a -> m<br />
foldr :: (a -> b -> b) -> b -> t a -> b<br />
foldr' :: (a -> b -> b) -> b -> t a -> b<br />
foldl :: (b -> a -> b) -> b -> t a -> b<br />
foldl' :: (b -> a -> b) -> b -> t a -> b<br />
foldr1 :: (a -> a -> a) -> t a -> a<br />
foldl1 :: (a -> a -> a) -> t a -> a<br />
toList :: t a -> [a]<br />
null :: t a -> Bool<br />
length :: t a -> Int<br />
elem :: Eq a => a -> t a -> Bool<br />
maximum :: Ord a => t a -> a<br />
minimum :: Ord a => t a -> a<br />
sum :: Num a => t a -> a<br />
product :: Num a => t a -> a<br />
</haskell><br />
<br />
This may look complicated, but in fact, to make a <code>Foldable</code> instance<br />
you only need to implement one method: your choice of <code>foldMap</code> or<br />
<code>foldr</code>. All the other methods have default implementations in terms<br />
of these, and are included in the class in case more<br />
efficient implementations can be provided.<br />
<br />
==Instances and examples==<br />
<br />
The type of <code>foldMap</code> should make it clear what it is supposed to do:<br />
given a way to convert the data in a container into a <code>Monoid</code> (a<br />
function <code>a -> m</code>) and a container of <code>a</code>’s (<code>t a</code>), <code>foldMap</code><br />
provides a way to iterate over the entire contents of the container,<br />
converting all the <code>a</code>’s to <code>m</code>’s and combining all the <code>m</code>’s with<br />
<code>mappend</code>. The following code shows two examples: a simple<br />
implementation of <code>foldMap</code> for lists, and a binary tree example<br />
provided by the <code>Foldable</code> documentation.<br />
<br />
<haskell><br />
instance Foldable [] where<br />
foldMap :: Monoid m => (a -> m) -> [a] -> m<br />
foldMap g = mconcat . map g<br />
<br />
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)<br />
<br />
instance Foldable Tree where<br />
foldMap :: Monoid m => (a -> m) -> Tree a -> m<br />
foldMap f Empty = mempty<br />
foldMap f (Leaf x) = f x<br />
foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f r<br />
</haskell><br />
<br />
The <code>foldr</code> function has a type similar to the <code>foldr</code> found in the <code>Prelude</code>, but<br />
more general, since the <code>foldr</code> in the <code>Prelude</code> works only on lists.<br />
<br />
The <code>Foldable</code> module also provides instances for <code>Maybe</code> and <code>Array</code>;<br />
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>,<br />
and <code>Sequence</code>) provide their own <code>Foldable</code> instances.<br />
<br />
{{Exercises|<br />
# Implement <code>fold</code> in terms of <code>foldMap</code>.<br />
# What would you need in order to implement <code>foldMap</code> in terms of <code>fold</code>?<br />
# Implement <code>foldMap</code> in terms of <code>foldr</code>.<br />
# Implement <code>foldr</code> in terms of <code>foldMap</code> (hint: use the <code>Endo</code> monoid).<br />
# What is the type of <code>foldMap . foldMap</code>? Or <code>foldMap . foldMap . foldMap</code>, etc.? What do they do?<br />
}}<br />
<br />
==Derived folds==<br />
<br />
Given an instance of <code>Foldable</code>, we can write generic,<br />
container-agnostic functions such as:<br />
<br />
<haskell><br />
-- Compute the size of any container.<br />
containerSize :: Foldable f => f a -> Int<br />
containerSize = getSum . foldMap (const (Sum 1))<br />
<br />
-- Compute a list of elements of a container satisfying a predicate.<br />
filterF :: Foldable f => (a -> Bool) -> f a -> [a]<br />
filterF p = foldMap (\a -> if p a then [a] else [])<br />
<br />
-- Get a list of all the Strings in a container which include the<br />
-- letter a.<br />
aStrings :: Foldable f => f String -> [String]<br />
aStrings = filterF (elem 'a')<br />
</haskell><br />
<br />
The <code>Foldable</code> module also provides a large number of predefined<br />
folds. These used to be generalized versions of <code>Prelude</code> functions of the<br />
same name that only worked on lists; but [https://wiki.haskell.org/Foldable_Traversable_In_Prelude as of GHC 7.10], the generalized versions themselves are now exported from the Prelude: for example, <code>concat</code>, <code>concatMap</code>, <code>and</code>,<br />
<code>or</code>, <code>any</code>, <code>all</code>, <code>sum</code>, <code>product</code>, <code>maximum</code>(<code>By</code>),<br />
<code>minimum</code>(<code>By</code>), <code>elem</code>, <code>notElem</code>, and <code>find</code>. For example, before GHC 7.10, <code>length</code> used to have type <code>length :: [a] -> Int</code>; now it has type <code>Foldable t => t a -> Int</code> (and is in fact the same as the <code>containerSize</code> function shown above).<br />
<br />
The important function <code>toList</code> is also provided, which turns any <code>Foldable</code> structure into a list of its elements in left-right order; it works by folding with the list monoid.<br />
<br />
There are also generic functions that work with <code>Applicative</code> or<br />
<code>Monad</code> instances to generate some sort of computation from each<br />
element in a container, and then perform all the side effects from<br />
those computations, discarding the results: <code>traverse_</code>, <code>sequenceA_</code>,<br />
and others. The results must be discarded because the <code>Foldable</code><br />
class is too weak to specify what to do with them: we cannot, in<br />
general, make an arbitrary <code>Applicative</code> or <code>Monad</code> instance into a <code>Monoid</code>, but we can make <code>m ()</code> into a <code>Monoid</code> for any such <code>m</code>. If we do have an <code>Applicative</code> or <code>Monad</code> with a monoid<br />
structure—that is, an <code>Alternative</code> or a <code>MonadPlus</code>—then we can<br />
use the <code>asum</code> or <code>msum</code> functions, which can combine the results as<br />
well. Consult the [{{HackageDocs|base|Data-Foldable}} <code>Foldable</code> documentation] for<br />
more details on any of these functions.<br />
<br />
Note that the <code>Foldable</code> operations always forget the structure of<br />
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<br />
type of any operations defined in the <code>Foldable</code> module. Many times<br />
this is exactly what we want, but sometimes we would like to be able<br />
to generically traverse a container while preserving its<br />
structure—and this is exactly what the <code>Traversable</code> class provides,<br />
which will be discussed in the next section.<br />
<br />
{{Exercises|<br />
# Implement <code>toList :: Foldable f {{=}}> f a -> [a]</code> in terms of either <code>foldr</code> or <code>foldMap</code>.<br />
# Show how one could implement the generic version of <code>foldr</code> in terms of <code>toList</code>, assuming we had only the list-specific <code>foldr :: (a -> b -> b) -> b -> [a] -> b</code>.<br />
# Pick some of the following functions to implement: <code>concat</code>, <code>concatMap</code>, <code>and</code>, <code>or</code>, <code>any</code>, <code>all</code>, <code>sum</code>, <code>product</code>, <code>maximum</code>(<code>By</code>), <code>minimum</code>(<code>By</code>), <code>elem</code>, <code>notElem</code>, and <code>find</code>. Figure out how they generalize to <code>Foldable</code> and come up with elegant implementations using <code>fold</code> or <code>foldMap</code> along with appropriate <code>Monoid</code> instances.<br />
}}<br />
<br />
==Utility functions==<br />
<br />
* <code>asum :: (Alternative f, Foldable t) => t (f a) -> f a</code> takes a container full of computations and combines them using <code>(<|>)</code>.<br />
<br />
* <code>sequenceA_ :: (Applicative f, Foldable t) => t (f a) -> f ()</code> takes a container full of computations and runs them in sequence, discarding the results (that is, they are used only for their effects). Since the results are discarded, the container only needs to be <code>Foldable</code>. (Compare with <code>sequenceA :: (Applicative f, Traversable t) => t (f a) -> f (t a)</code>, which requires a stronger <code>Traversable</code> constraint in order to be able to reconstruct a container of results having the same shape as the original container.)<br />
<br />
* <code>traverse_ :: (Applicative f, Foldable t) => (a -> f b) -> t a -> f ()</code> applies the given function to each element in a foldable container and sequences the effects (but discards the results).<br />
<br />
* <code>for_</code> is the same as <code>traverse_</code> but with its arguments flipped. This is the moral equivalent of a "foreach" loop in an imperative language.<br />
<br />
* For historical reasons, there are also variants of all the above with overly-restrictive <code>Monad</code>(-like) constraints: <code>msum</code> is the same as <code>asum</code> specialized to <code>MonadPlus</code>, and <code>sequence_</code>, <code>mapM_</code>, and <code>forM_</code> respectively are <code>Monad</code> specializations of <code>sequenceA_</code>, <code>traverse_</code>, and <code>for_</code>.<br />
<br />
{{Exercises|<br />
# Implement <code>traverse_</code> in terms of <code>sequenceA_</code> and vice versa. One of these will need an extra constraint. What is it?<br />
}}<br />
<br />
==Foldable actually isn't==<br />
<br />
The generic term "fold" is often used to refer to the more technical concept of [[Catamorphisms|catamorphism]]. Intuitively, given a way to summarize "one level of structure" (where recursive subterms have already been replaced with their summaries), a catamorphism can summarize an entire recursive structure. It is important to realize that <code>Foldable</code> does <i>not</i> correspond to catamorphisms, but to something weaker. In particular, <code>Foldable</code> allows observing only the left-right traversal order of elements within a structure, not the actual structure itself. Put another way, every use of <code>Foldable</code> can be expressed in terms of <code>toList</code>. For example, <code>fold</code> itself is equivalent to <code>mconcat . toList</code>.<br />
<br />
This is sufficient for many tasks, but not all. For example, consider trying to compute the depth of a <code>Tree</code>: try as we might, there is no way to implement it using <code>Foldable</code>. However, it <i>can</i> be implemented as a catamorphism.<br />
<br />
==Further reading==<br />
<br />
The <code>Foldable</code> class had its genesis in [http://www.soi.city.ac.uk/~ross/papers/Applicative.html McBride and Paterson’s paper]<br />
introducing <code>Applicative</code>, although it has<br />
been fleshed out quite a bit from the form in the paper.<br />
<br />
An interesting use of <code>Foldable</code> (as well as <code>Traversable</code>) can be<br />
found in Janis Voigtländer’s paper [http://doi.acm.org/10.1145/1480881.1480904 Bidirectionalization for free!].<br />
<br />
For more on the relationship between <code>fold</code>, <code>foldMap</code>, and <code>foldr</code>, see [https://byorgey.wordpress.com/2012/11/05/foldr-is-made-of-monoids/ foldr is made of monoids].<br />
<br />
There was [http://tojans.me/blog/2015/10/13/foldable-for-non-haskellers-haskells-controversial-ftp-proposal/ quite a bit of controversy] in the Haskell community about a [https://wiki.haskell.org/Foldable_Traversable_In_Prelude proposal to integrate <code>Foldable</code> (and <code>Traversable</code>) more tightly into the Prelude], known as the FTP. Some of the controversy centered around <code>Foldable</code> instances such as the one for <code>((,) a)</code>, which, together with generalized types for functions such as <code>length :: Foldable t => t a -> Int</code>, allow one to derive seemingly nonsensical results such as <code>length (2,3) = 1</code>. Here is a [https://www.youtube.com/watch?v=87re_yIQMDw humorous talk] poking fun at the situation.<br />
<br />
=Traversable=<br />
<br />
==Definition==<br />
<br />
The <code>Traversable</code> type class, defined in the <code>Data.Traversable</code><br />
module ([{{HackageDocs|base|Data-Traversable}} haddock]), is:<br />
<br />
<haskell><br />
class (Functor t, Foldable t) => Traversable t where<br />
traverse :: Applicative f => (a -> f b) -> t a -> f (t b)<br />
sequenceA :: Applicative f => t (f a) -> f (t a)<br />
mapM :: Monad m => (a -> m b) -> t a -> m (t b)<br />
sequence :: Monad m => t (m a) -> m (t a)<br />
</haskell><br />
<br />
As you can see, every <code>Traversable</code> is also a <code>Foldable</code> <code>Functor</code>. To make a <code>Traversable</code> instance, it suffices to implement either <code>traverse</code> or<br />
<code>sequenceA</code>; the other methods all have default implementations in<br />
terms of these. Note that <code>mapM</code> and <code>sequence</code> only exist for historical reasons; especially now that <code>Applicative</code> is a superclass of <code>Monad</code>, they are nothing more than copies of <code>traverse</code> and <code>sequenceA</code>, respectively, but with more restrictive types.<br />
<br />
==Intuition==<br />
<br />
<br />
The key method of the <code>Traversable</code> class is <code>traverse</code>, which has the following type:<br />
<haskell><br />
traverse :: Applicative f => (a -> f b) -> t a -> f (t b)<br />
</haskell><br />
This leads us to view <code>Traversable</code> as a generalization of <code>Functor</code>. <code>traverse</code> is an "effectful <code>fmap</code>": it allows us to map over a structure of type <code>t a</code>, applying a function to every element of type <code>a</code> in order to produce a new structure of type <code>t b</code>; but along the way the function may have some effects (captured by the applicative functor <code>f</code>).<br />
<br />
Alternatively, we may consider the <code>sequenceA</code> function. Consider its type:<br />
<haskell><br />
sequenceA :: Applicative f => t (f a) -> f (t a)<br />
</haskell><br />
This answers the fundamental question: when can we commute two<br />
functors? For example, can we turn a tree of lists into a list of<br />
trees?<br />
<br />
The ability to compose two monads depends crucially on this ability to<br />
commute functors. Intuitively, if we want to build a composed monad<br />
<code>M a = m (n a)</code> out of monads <code>m</code> and <code>n</code>, then to be able to<br />
implement <code>join :: M (M a) -> M a</code>, that is,<br />
<code>join :: m (n (m (n a))) -> m (n a)</code>, we have to be able to commute<br />
the <code>n</code> past the <code>m</code> to get <code>m (m (n (n a)))</code>, and then we can use the<br />
<code>join</code>s for <code>m</code> and <code>n</code> to produce something of type <code>m (n a)</code>. See<br />
[http://web.cecs.pdx.edu/~mpj/pubs/springschool.html Mark Jones’ paper] for more details.<br />
<br />
It turns out that given a <code>Functor</code> constraint on the type <code>t</code>, <code>traverse</code> and <code>sequenceA</code> are equivalent in power: either can be implemented in terms of the other.<br />
<br />
{{Exercises|<br />
# There are at least two natural ways to turn a tree of lists into a list of trees. What are they, and why?<br />
# Give a natural way to turn a list of trees into a tree of lists.<br />
# What is the type of <code>traverse . traverse</code>? What does it do?<br />
# Implement <code>traverse</code> in terms of <code>sequenceA</code>, and vice versa.<br />
}}<br />
<br />
==Instances and examples==<br />
<br />
What’s an example of a <code>Traversable</code> instance?<br />
The following code shows an example instance for the same<br />
<code>Tree</code> type used as an example in the previous <code>Foldable</code> section. It<br />
is instructive to compare this instance with a <code>Functor</code> instance for<br />
<code>Tree</code>, which is also shown.<br />
<br />
<haskell><br />
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)<br />
<br />
instance Traversable Tree where<br />
traverse :: Applicative f => (a -> f b) -> Tree a -> f (Tree b) <br />
traverse g Empty = pure Empty<br />
traverse g (Leaf x) = Leaf <$> g x<br />
traverse g (Node l x r) = Node <$> traverse g l<br />
<*> g x<br />
<*> traverse g r<br />
<br />
instance Functor Tree where<br />
fmap :: (a -> b) -> Tree a -> Tree b<br />
fmap g Empty = Empty<br />
fmap g (Leaf x) = Leaf $ g x<br />
fmap g (Node l x r) = Node (fmap g l)<br />
(g x)<br />
(fmap g r)<br />
</haskell><br />
<br />
It should be clear that the <code>Traversable</code> and <code>Functor</code> instances for<br />
<code>Tree</code> are structurally identical; the only difference is that the <code>Functor</code><br />
instance involves normal function application, whereas the<br />
applications in the <code>Traversable</code> instance take place within an<br />
<code>Applicative</code> context, using <code>(<$>)</code> and <code>(<*>)</code>. This same pattern will hold for any type.<br />
<br />
Any <code>Traversable</code> functor is also <code>Foldable</code>, and a <code>Functor</code>. We can see<br />
this not only from the class declaration, but by the fact that we can<br />
implement the methods of both classes given only the <code>Traversable</code><br />
methods.<br />
<br />
The standard libraries provide a number of <code>Traversable</code> instances,<br />
including instances for <code>[]</code>, <code>ZipList</code>, <code>Maybe</code>, <code>((,) e)</code>, <code>Sum</code>, <code>Product</code>, <code>Either e</code>, <code>Map</code>, <code>Tree</code>, and <code>Sequence</code>.<br />
Notably, <code>Set</code> is not <code>Traversable</code>, although it is <code>Foldable</code>.<br />
<br />
{{Exercises|<br />
# Implement <code>fmap</code> and <code>foldMap</code> using only the <code>Traversable</code> methods. (Note that the <code>Traversable</code> module provides these implementations as <code>fmapDefault</code> and <code>foldMapDefault</code>.)<br />
# Implement <code>Traversable</code> instances for <code>[]</code>, <code>Maybe</code>, <code>((,) e)</code>, and <code>Either e</code>.<br />
# Explain why <code>Set</code> is <code>Foldable</code> but not <code>Traversable</code>.<br />
# Show that <code>Traversable</code> functors compose: that is, implement an instance for <code>Traversable (Compose f g)</code> given <code>Traversable</code> instances for <code>f</code> and <code>g</code>.<br />
}}<br />
<br />
==Laws==<br />
<br />
Any instance of <code>Traversable</code> must satisfy the following two laws, where <code>Identity</code> is the identity functor (as defined in the [http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Data-Functor-Identity.html <code>Data.Functor.Identity</code> module] from the <code>transformers</code> package), and <code>Compose</code> wraps the composition of two functors (as defined in [http://hackage.haskell.org/packages/archive/transformers/0.3.0.0/doc/html/Data-Functor-Compose.html <code>Data.Functor.Compose</code>]):<br />
<br />
# <code>traverse Identity = Identity</code><br />
# <code>traverse (Compose . fmap g . f) = Compose . fmap (traverse g) . traverse f</code><br />
<br />
The first law essentially says that traversals cannot make up arbitrary effects. The second law explains how doing two traversals in sequence can be collapsed to a single traversal.<br />
<br />
Additionally, suppose <code>eta</code> is an "<code>Applicative</code> morphism", that is,<br />
<haskell><br />
eta :: forall a f g. (Applicative f, Applicative g) => f a -> g a<br />
</haskell><br />
and <code>eta</code> preserves the <code>Applicative</code> operations: <code>eta (pure x) = pure x</code> and <code>eta (x <*> y) = eta x <*> eta y</code>. Then, by parametricity, any instance of <code>Traversable</code> satisfying the above two laws will also satisfy <code>eta . traverse f = traverse (eta . f)</code>.<br />
<br />
==Further reading==<br />
<br />
The <code>Traversable</code> class also had its genesis in [http://www.soi.city.ac.uk/~ross/papers/Applicative.html McBride and Paterson’s <code>Applicative</code> paper],<br />
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],<br />
which also contains a wealth of references to related work.<br />
<br />
<code>Traversable</code> forms a core component of Edward Kmett's [http://hackage.haskell.org/package/lens lens library]. Watching [https://vimeo.com/56063074 Edward's talk on the subject] is a highly recommended way to gain better insight into <code>Traversable</code>, <code>Foldable</code>, <code>Applicative</code>, and many other things besides.<br />
<br />
For references on the <code>Traversable</code> laws, see Russell O'Connor's [http://article.gmane.org/gmane.comp.lang.haskell.libraries/17778 mailing list post] (and subsequent thread), and [https://arxiv.org/abs/1202.2919 this paper by Jaskelioff and Rypacek] for a more in-depth discussion. Daniel Mlot also has [http://duplode.github.io/posts/traversable-a-remix.html this very nice blog post] explaining how <code>Traversable</code> arises by considering a variant on the usual Kleisli category of a monad, which also sheds light on where the <code>Traversable</code> laws come from.<br />
<br />
[http://elvishjerricco.github.io/2017/03/23/applicative-sorting.html This blog post by Will Fancher] shows how to use <code>Traversable</code> along with a clever choice of <code>Applicative</code> to efficiently sort any <code>Traversable</code> container.<br />
<br />
=Category=<br />
<br />
<code>Category</code> is a relatively recent addition to the Haskell standard libraries. It generalizes the notion of function composition to general “morphisms”.<br />
<br />
{{note|GHC 7.6.1 changed its rules regarding types and type variables. Now, any operator at the type level is treated as a type ''constructor'' rather than a type ''variable''; prior to GHC 7.6.1 it was possible to use <code>(~&gt;)</code> instead of <code>`arr`</code>. For more information, see [http://thread.gmane.org/gmane.comp.lang.haskell.glasgow.user/21350 the discussion on the GHC-users mailing list]. For a new approach to nice arrow notation that works with GHC 7.6.1, see [http://article.gmane.org/gmane.comp.lang.haskell.glasgow.user/22615 this message] and also [http://article.gmane.org/gmane.comp.lang.haskell.glasgow.user/22616 this message] from Edward Kmett, though for simplicity I haven't adopted it here.}}<br />
The definition of the <code>Category</code> type class (from<br />
<code>Control.Category</code>; [{{HackageDocs|base|Control-Category}} haddock]) is shown below. For ease of reading, note that I have used an infix type variable <code>`arr`</code>, in parallel with the infix function type constructor <code>(->)</code>. {{noteref}} This syntax is not part of Haskell 2010. 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 <code>`arr`</code> for <code>Category</code> as well as <code>Arrow</code>.<br />
<br />
<haskell><br />
class Category arr where<br />
id :: a `arr` a<br />
(.) :: (b `arr` c) -> (a `arr` b) -> (a `arr` c)<br />
<br />
-- The same thing, with a normal (prefix) type constructor<br />
class Category cat where<br />
id :: cat a a<br />
(.) :: cat b c -> cat a b -> cat a c<br />
</haskell><br />
<br />
Note that an instance of <code>Category</code> should be a type which takes two type arguments, that is, something of kind <code>* -> * -> *</code>. It is instructive to imagine the type variable <code>cat</code> replaced by the function constructor <code>(->)</code>: indeed, in this case we recover precisely the familiar identity function <code>id</code> and function composition operator <code>(.)</code> defined in the standard <code>Prelude</code>.<br />
<br />
Of course, the <code>Category</code> module provides exactly such an instance of<br />
<code>Category</code> for <code>(->)</code>. But it also provides one other instance, shown below, which should be familiar from the previous discussion of the <code>Monad</code> laws. <code>Kleisli m a b</code>, as defined in the <code>Control.Arrow</code> module, is just a <code>newtype</code> wrapper around <code>a -> m b</code>.<br />
<br />
<haskell><br />
newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }<br />
<br />
instance Monad m => Category (Kleisli m) where<br />
id :: Kleisli m a a<br />
id = Kleisli return<br />
<br />
(.) :: Kleisli m b c -> Kleisli m a b -> Kleisli m a c<br />
Kleisli g . Kleisli h = Kleisli (h >=> g)<br />
</haskell><br />
<br />
The only laws that <code>Category</code> instances should satisfy are that <code>id</code> should be the identity of <code>(.)</code>, and <code>(.)</code> should be associative. This is kind of like being a monoid, except that, unlike with monoids, not any two values can be composed with <code>(.)</code>---their types have to match up.<br />
<br />
Finally, the <code>Category</code> module exports two additional operators:<br />
<code>(<<<)</code>, which is just a synonym for <code>(.)</code>, and <code>(>>>)</code>, which is <code>(.)</code> with its arguments reversed. (In previous versions of the libraries, these operators were defined as part of the <code>Arrow</code> class.)<br />
<br />
==Further reading==<br />
<br />
The name <code>Category</code> is a bit misleading, since the <code>Category</code> class cannot represent arbitrary categories, but only categories whose objects are objects of <code>Hask</code>, the category of Haskell types. For a more general treatment of categories within Haskell, see the [http://hackage.haskell.org/package/category-extras category-extras package]. For more about category theory in general, see the excellent [http://en.wikibooks.org/wiki/Haskell/Category_theory Haskell wikibook page],<br />
[http://books.google.com/books/about/Category_theory.html?id=-MCJ6x2lC7oC Steve Awodey’s new book], Benjamin Pierce’s [http://books.google.com/books/about/Basic_category_theory_for_computer_scien.html?id=ezdeaHfpYPwC Basic category theory for computer scientists], or [http://folli.loria.fr/cds/1999/esslli99/courses/barr-wells.html Barr and Wells category theory lecture notes]. [http://dekudekuplex.wordpress.com/2009/01/19/motivating-learning-category-theory-for-non-mathematicians/ Benjamin Russell’s blog post]<br />
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.<br />
<br />
=Arrow=<br />
<br />
The <code>Arrow</code> class represents another abstraction of computation, in a<br />
similar vein to <code>Monad</code> and <code>Applicative</code>. However, unlike <code>Monad</code><br />
and <code>Applicative</code>, whose types only reflect their output, the type of<br />
an <code>Arrow</code> computation reflects both its input and output. Arrows<br />
generalize functions: if <code>arr</code> is an instance of <code>Arrow</code>, a value of<br />
type <code>b `arr` c</code> can be thought of as a computation which takes values of<br />
type <code>b</code> as input, and produces values of type <code>c</code> as output. In the<br />
<code>(->)</code> instance of <code>Arrow</code> this is just a pure function; in general, however,<br />
an arrow may represent some sort of “effectful” computation.<br />
<br />
==Definition==<br />
<br />
The definition of the <code>Arrow</code> type class, from<br />
<code>Control.Arrow</code> ([{{HackageDocs|base|Control-Arrow}} haddock]), is:<br />
<br />
<haskell><br />
class Category arr => Arrow arr where<br />
arr :: (b -> c) -> (b `arr` c)<br />
first :: (b `arr` c) -> ((b, d) `arr` (c, d))<br />
second :: (b `arr` c) -> ((d, b) `arr` (d, c))<br />
(***) :: (b `arr` c) -> (b' `arr` c') -> ((b, b') `arr` (c, c'))<br />
(&&&) :: (b `arr` c) -> (b `arr` c') -> (b `arr` (c, c'))<br />
</haskell><br />
<br />
{{note|In versions of the <code>base</code><br />
package prior to version 4, there is no <code>Category</code> class, and the<br />
<code>Arrow</code> class includes the arrow composition operator <code>(>>>)</code>. It<br />
also includes <code>pure</code> as a synonym for <code>arr</code>, but this was removed<br />
since it conflicts with the <code>pure</code> from <code>Applicative</code>.}}<br />
<br />
The first thing to note is the <code>Category</code> class constraint, which<br />
means that we get identity arrows and arrow composition for free:<br />
given two arrows <code>g :: b `arr` c</code> and <code>h :: c `arr` d</code>, we can form their<br />
composition <code>g >>> h :: b `arr` d</code> {{noteref}}.<br />
<br />
As should be a familiar pattern by now, the only methods which must be<br />
defined when writing a new instance of <code>Arrow</code> are <code>arr</code> and <code>first</code>;<br />
the other methods have default definitions in terms of these, but are<br />
included in the <code>Arrow</code> class so that they can be overridden with more<br />
efficient implementations if desired.<br />
<br />
==Intuition==<br />
<br />
Let’s look at each of the arrow methods in turn. [http://www.haskell.org/arrows/ Ross Paterson’s web page on arrows] has nice diagrams which can help<br />
build intuition.<br />
<br />
* The <code>arr</code> function takes any function <code>b -> c</code> and turns it into a generalized arrow <code>b `arr` 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).<br />
<br />
* 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>.<br />
<br />
* 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>.<br />
<br />
* 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>.<br />
<br />
* 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>.<br />
<br />
==Instances==<br />
<br />
The <code>Arrow</code> library itself only provides two <code>Arrow</code> instances, both<br />
of which we have already seen: <code>(->)</code>, the normal function<br />
constructor, and <code>Kleisli m</code>, which makes functions of<br />
type <code>a -> m b</code> into <code>Arrow</code>s for any <code>Monad m</code>. These instances are:<br />
<br />
<haskell><br />
instance Arrow (->) where<br />
arr :: (b -> c) -> (b -> c)<br />
arr g = g<br />
<br />
first :: (b -> c) -> ((b,d) -> (c,d))<br />
first g (x,y) = (g x, y)<br />
<br />
newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }<br />
<br />
instance Monad m => Arrow (Kleisli m) where<br />
arr :: (b -> c) -> Kleisli m b c<br />
arr f = Kleisli (return . f)<br />
<br />
first :: Kleisli m b c -> Kleisli m (b,d) (c,d)<br />
first (Kleisli f) = Kleisli (\ ~(b,d) -> do c <- f b<br />
return (c,d) )<br />
</haskell><br />
<br />
==Laws==<br />
<br />
{{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].}}<br />
<br />
There are quite a few laws that instances of <code>Arrow</code> should<br />
satisfy {{noteref}}:<br />
<br />
<haskell><br />
arr id = id<br />
arr (h . g) = arr g >>> arr h<br />
first (arr g) = arr (g *** id)<br />
first (g >>> h) = first g >>> first h<br />
first g >>> arr (id *** h) = arr (id *** h) >>> first g<br />
first g >>> arr fst = arr fst >>> g<br />
first (first g) >>> arr assoc = arr assoc >>> first g<br />
<br />
assoc ((x,y),z) = (x,(y,z))<br />
</haskell><br />
<br />
Note that this version of the laws is slightly different than the laws given in the<br />
first two above references, since several of the laws have now been<br />
subsumed by the <code>Category</code> laws (in particular, the requirements that<br />
<code>id</code> is the identity arrow and that <code>(>>>)</code> is associative). The laws<br />
shown here follow those in Paterson’s Programming with Arrows, which uses the<br />
<code>Category</code> class.<br />
<br />
{{note|Unless category-theory-induced insomnolence is your cup of tea.}}<br />
<br />
The reader is advised not to lose too much sleep over the <code>Arrow</code><br />
laws {{noteref}}, since it is not essential to understand them in order to<br />
program with arrows. There are also laws that <code>ArrowChoice</code>,<br />
<code>ArrowApply</code>, and <code>ArrowLoop</code> instances should satisfy; the interested<br />
reader should consult [http://www.soi.city.ac.uk/~ross/papers/fop.html Paterson: Programming with Arrows].<br />
<br />
==ArrowChoice==<br />
<br />
Computations built using the <code>Arrow</code> class, like those built using<br />
the <code>Applicative</code> class, are rather inflexible: the structure of the computation<br />
is fixed at the outset, and there is no ability to choose between<br />
alternate execution paths based on intermediate results.<br />
The <code>ArrowChoice</code> class provides exactly such an ability:<br />
<br />
<haskell><br />
class Arrow arr => ArrowChoice arr where<br />
left :: (b `arr` c) -> (Either b d `arr` Either c d)<br />
right :: (b `arr` c) -> (Either d b `arr` Either d c)<br />
(+++) :: (b `arr` c) -> (b' `arr` c') -> (Either b b' `arr` Either c c')<br />
(|||) :: (b `arr` d) -> (c `arr` d) -> (Either b c `arr` d)<br />
</haskell><br />
<br />
A comparison of <code>ArrowChoice</code> to <code>Arrow</code> will reveal a striking<br />
parallel between <code>left</code>, <code>right</code>, <code>(+++)</code>, <code>(|||)</code> and <code>first</code>,<br />
<code>second</code>, <code>(***)</code>, <code>(&&&)</code>, respectively. Indeed, they are dual:<br />
<code>first</code>, <code>second</code>, <code>(***)</code>, and <code>(&&&)</code> all operate on product types<br />
(tuples), and <code>left</code>, <code>right</code>, <code>(+++)</code>, and <code>(|||)</code> are the<br />
corresponding operations on sum types. In general, these operations<br />
create arrows whose inputs are tagged with <code>Left</code> or <code>Right</code>, and can<br />
choose how to act based on these tags.<br />
<br />
* 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.<br />
<br />
* 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>.<br />
<br />
* 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.<br />
<br />
* 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.<br />
<br />
The <code>ArrowChoice</code> class allows computations to choose among a finite number of execution paths, based on intermediate results. The possible<br />
execution paths must be known in advance, and explicitly assembled with <code>(+++)</code> or <code>(|||)</code>. However, sometimes more flexibility is<br />
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>.<br />
<br />
==ArrowApply==<br />
<br />
The <code>ArrowApply</code> type class is:<br />
<br />
<haskell><br />
class Arrow arr => ArrowApply arr where<br />
app :: (b `arr` c, b) `arr` c<br />
</haskell><br />
<br />
If we have computed an arrow as the output of some previous<br />
computation, then <code>app</code> allows us to apply that arrow to an input,<br />
producing its output as the output of <code>app</code>. As an exercise, the<br />
reader may wish to use <code>app</code> to implement an alternative “curried”<br />
version, <code>app2 :: b `arr` ((b `arr` c) `arr` c)</code>.<br />
<br />
This notion of being able to ''compute'' a new computation<br />
may sound familiar:<br />
this is exactly what the monadic bind operator <code>(>>=)</code> does. It<br />
should not particularly come as a surprise that <code>ArrowApply</code> and<br />
<code>Monad</code> are exactly equivalent in expressive power. In particular,<br />
<code>Kleisli m</code> can be made an instance of <code>ArrowApply</code>, and any instance<br />
of <code>ArrowApply</code> can be made a <code>Monad</code> (via the <code>newtype</code> wrapper<br />
<code>ArrowMonad</code>). As an exercise, the reader may wish to try<br />
implementing these instances:<br />
<br />
<haskell><br />
class Arrow arr => ArrowApply arr where<br />
app :: (b `arr` c, b) `arr` c<br />
<br />
instance Monad m => ArrowApply (Kleisli m) where<br />
app :: Kleisli m (Kleisli m b c, b) c<br />
app = -- exercise<br />
<br />
newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)<br />
<br />
instance ArrowApply a => Monad (ArrowMonad a) where<br />
return :: b -> ArrowMonad a b<br />
return = -- exercise<br />
<br />
(>>=) :: ArrowMonad a a -> (a -> ArrowMonad a b) -> ArrowMonad a b<br />
(ArrowMonad a) >>= k = -- exercise<br />
</haskell><br />
<br />
==ArrowLoop==<br />
<br />
The <code>ArrowLoop</code> type class is:<br />
<br />
<haskell><br />
class Arrow a => ArrowLoop a where<br />
loop :: a (b, d) (c, d) -> a b c<br />
<br />
trace :: ((b,d) -> (c,d)) -> b -> c<br />
trace f b = let (c,d) = f (b,d) in c<br />
</haskell><br />
<br />
It describes arrows that can use recursion to compute results, and is<br />
used to desugar the <code>rec</code> construct in arrow notation (described<br />
below).<br />
<br />
Taken by itself, the type of the <code>loop</code> method does not seem to tell<br />
us much. Its intention, however, is a generalization of the <code>trace</code><br />
function which is also shown. The <code>d</code> component of the first arrow’s<br />
output is fed back in as its own input. In other words, the arrow<br />
<code>loop g</code> is obtained by recursively “fixing” the second component of<br />
the input to <code>g</code>.<br />
<br />
It can be a bit difficult to grok what the <code>trace</code> function is doing.<br />
How can <code>d</code> appear on the left and right sides of the <code>let</code>? Well,<br />
this is Haskell’s laziness at work. There is not space here for a<br />
full explanation; the interested reader is encouraged to study the<br />
standard <code>fix</code> function, and to read [http://www.soi.city.ac.uk/~ross/papers/fop.html Paterson’s arrow tutorial].<br />
<br />
==Arrow notation==<br />
<br />
Programming directly with the arrow combinators can be painful,<br />
especially when writing complex computations which need to retain<br />
simultaneous reference to a number of intermediate results. With<br />
nothing but the arrow combinators, such intermediate results must be<br />
kept in nested tuples, and it is up to the programmer to remember<br />
which intermediate results are in which components, and to swap,<br />
reassociate, and generally mangle tuples as necessary. This problem<br />
is solved by the special arrow notation supported by GHC, similar to<br />
<code>do</code> notation for monads, that allows names to be assigned to<br />
intermediate results while building up arrow computations. An example<br />
arrow implemented using arrow notation, taken from<br />
Paterson, is:<br />
<br />
<haskell><br />
class ArrowLoop arr => ArrowCircuit arr where<br />
delay :: b -> (b `arr` b)<br />
<br />
counter :: ArrowCircuit arr => Bool `arr` Int<br />
counter = proc reset -> do<br />
rec output <- idA -< if reset then 0 else next<br />
next <- delay 0 -< output + 1<br />
idA -< output<br />
</haskell><br />
<br />
This arrow is intended to<br />
represent a recursively defined counter circuit with a reset line.<br />
<br />
There is not space here for a full explanation of arrow notation; the<br />
interested reader should consult<br />
[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].<br />
<br />
==Further reading==<br />
<br />
An excellent starting place for the student of arrows is the [http://www.haskell.org/arrows/ arrows web page], which contains an<br />
introduction and many references. Some key papers on arrows include<br />
Hughes’ original paper introducing arrows, [http://dx.doi.org/10.1016/S0167-6423(99)00023-4 Generalising monads to arrows], and [http://www.soi.city.ac.uk/~ross/papers/notation.html Paterson’s paper on arrow notation].<br />
<br />
Both Hughes and Paterson later wrote accessible tutorials intended for a broader<br />
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].<br />
<br />
Although Hughes’ goal in defining the <code>Arrow</code> class was to<br />
generalize <code>Monad</code>s, and it has been said that <code>Arrow</code> lies “between<br />
<code>Applicative</code> and <code>Monad</code>” in power, they are not directly<br />
comparable. The precise relationship remained in some confusion until<br />
[http://homepages.inf.ed.ac.uk/wadler/papers/arrows-and-idioms/arrows-and-idioms.pdf analyzed by Lindley, Wadler, and Yallop], who<br />
also invented a new calculus of arrows, based on the lambda calculus,<br />
which considerably simplifies the presentation of the arrow laws<br />
(see [http://homepages.inf.ed.ac.uk/wadler/papers/arrows/arrows.pdf The arrow calculus]). There is also a precise technical sense in which [http://just-bottom.blogspot.de/2010/04/programming-with-effects-story-so-far.html <code>Arrow</code> can be seen as the intersection of <code>Applicative</code> and <code>Category</code>].<br />
<br />
Some examples of <code>Arrow</code>s include [https://wiki.haskell.org/Yampa Yampa], the<br />
[http://www.fh-wedel.de/~si/HXmlToolbox/ Haskell XML Toolkit], and the functional GUI library [[Grapefruit]].<br />
<br />
Some extensions to arrows have been explored; for example, the<br />
<code>BiArrow</code>s of Alimarine et al. ([http://wiki.clean.cs.ru.nl/download/papers/2005/alia2005-biarrowsHaskellWorkshop.pdf "There and Back Again: Arrows for Invertible Programming"]), for two-way instead of one-way<br />
computation.<br />
<br />
The Haskell wiki has [[Research papers/Monads and Arrows|links to many additional research papers relating to <code>Arrow</code>s]].<br />
<br />
=Comonad=<br />
<br />
The final type class we will examine is <code>Comonad</code>. The <code>Comonad</code> class<br />
is the categorical dual of <code>Monad</code>; that is, <code>Comonad</code> is like <code>Monad</code><br />
but with all the function arrows flipped. It is not actually in the<br />
standard Haskell libraries, but it has seen some interesting uses<br />
recently, so we include it here for completeness.<br />
<br />
==Definition==<br />
<br />
The <code>Comonad</code> type class, defined in the <code>Control.Comonad</code> module of<br />
the [http://hackage.haskell.org/package/comonad comonad library], is:<br />
<br />
<haskell><br />
class Functor w => Comonad w where<br />
extract :: w a -> a<br />
<br />
duplicate :: w a -> w (w a)<br />
duplicate = extend id<br />
<br />
extend :: (w a -> b) -> w a -> w b<br />
extend f = fmap f . duplicate<br />
</haskell><br />
<br />
As you can see, <code>extract</code> is the dual of <code>return</code>, <code>duplicate</code> is the dual of <code>join</code>, and <code>extend</code> is the dual of <code>(=<<)</code>. The definition of <code>Comonad</code> is a bit redundant, giving the programmer the choice on whether extend or duplicate are implemented; the other operation then has a default implementation.<br />
<br />
A prototypical example of a <code>Comonad</code> instance is:<br />
<br />
<haskell><br />
-- Infinite lazy streams<br />
data Stream a = Cons a (Stream a)<br />
<br />
-- 'duplicate' is like the list function 'tails'<br />
-- 'extend' computes a new Stream from an old, where the element<br />
-- at position n is computed as a function of everything from<br />
-- position n onwards in the old Stream<br />
instance Comonad Stream where<br />
extract :: Stream a -> a<br />
extract (Cons x _) = x<br />
<br />
duplicate :: Stream a -> Stream (Stream a)<br />
duplicate s@(Cons x xs) = Cons s (duplicate xs)<br />
<br />
extend :: (Stream a -> b) -> Stream a -> Stream b<br />
extend g s@(Cons x xs) = Cons (g s) (extend g xs)<br />
-- = fmap g (duplicate s)<br />
</haskell><br />
<br />
==Further reading==<br />
<br />
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 [http://fmapfixreturn.wordpress.com/2008/07/09/comonads-in-everyday-life/ 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.<br />
<br />
Uustalu and Vene have a number of papers exploring ideas related to comonads and functional programming:<br />
* [http://dx.doi.org/10.1016/j.entcs.2008.05.029 Comonadic Notions of Computation]<br />
* [http://www.ioc.ee/~tarmo/papers/sfp01-book.pdf The dual of substitution is redecoration] (Also available as [http://www.cs.ut.ee/~varmo/papers/sfp01-book.ps.gz ps.gz].)<br />
* [http://dx.doi.org/10.1016/j.ic.2005.08.005 Recursive coalgebras from comonads]<br />
* [http://www.fing.edu.uy/~pardo/papers/njc01.ps.gz Recursion schemes from comonads]<br />
* [http://cs.ioc.ee/~tarmo/papers/essence.pdf The Essence of Dataflow Programming].<br />
<br />
Gabriel Gonzalez's [http://www.haskellforall.com/2013/02/you-could-have-invented-comonads.html Comonads are objects] points out similarities between comonads and object-oriented programming.<br />
<br />
The [http://hackage.haskell.org/package/comonad-transformers comonad-transformers] package contains comonad transformers.<br />
<br />
=Acknowledgements=<br />
<br />
A special thanks to all of those who taught me about standard Haskell<br />
type classes and helped me develop good intuition for them,<br />
particularly Jules Bean (quicksilver), Derek Elkins (ddarius), Conal<br />
Elliott (conal), Cale Gibbard (Cale), David House, Dan Piponi<br />
(sigfpe), and Kevin Reid (kpreid).<br />
<br />
I also thank the many people who provided a mountain of helpful<br />
feedback and suggestions on a first draft of the Typeclassopedia: David Amos,<br />
Kevin Ballard, Reid Barton, Doug Beardsley, Joachim Breitner, Andrew<br />
Cave, David Christiansen, Gregory Collins, Mark Jason Dominus, Conal<br />
Elliott, Yitz Gale, George Giorgidze, Steven Grady, Travis Hartwell,<br />
Steve Hicks, Philip Hölzenspies, Edward Kmett, Eric Kow, Serge Le<br />
Huitouze, Felipe Lessa, Stefan Ljungstrand, Eric Macaulay, Rob MacAulay, Simon Meier,<br />
Eric Mertens, Tim Newsham, Russell O’Connor, Conrad Parker, Walt<br />
Rorie-Baety, Colin Ross, Tom Schrijvers, Aditya Siram, C. Smith,<br />
Martijn van Steenbergen, Joe Thornber, Jared Updike, Rob Vollmert,<br />
Andrew Wagner, Louis Wasserman, and Ashley Yakeley, as well as a few<br />
only known to me by their IRC nicks: b_jonas, maltem, tehgeekmeister,<br />
and ziman. I have undoubtedly omitted a few inadvertently, which in<br />
no way diminishes my gratitude.<br />
<br />
Finally, I would like to thank Wouter Swierstra for his fantastic work<br />
editing the Monad.Reader, and my wife Joyia for her patience during<br />
the process of writing the Typeclassopedia.<br />
<br />
=About the author=<br />
<br />
Brent Yorgey ([http://byorgey.wordpress.com/ blog], [http://www.cis.upenn.edu/~byorgey/ homepage]) is (as of November 2011) a fourth-year Ph.D. student in the [http://www.cis.upenn.edu/~plclub/ programming languages group] at the [http://www.upenn.edu 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.<br />
<br />
=Colophon=<br />
<br />
The Typeclassopedia was written by Brent Yorgey and initially published in March 2009. Painstakingly converted to wiki syntax by [[User:Geheimdienst]] in November 2011, after asking Brent’s permission.<br />
<br />
If something like this TeX to wiki syntax conversion ever needs to be done again, here are some vim commands that helped:<br />
<br />
* <nowiki>%s/\\section{\([^}]*\)}/=\1=/gc</nowiki><br />
* <nowiki>%s/\\subsection{\([^}]*\)}/==\1==/gc</nowiki><br />
* <nowiki>%s/^ *\\item /\r* /gc</nowiki><br />
* <nowiki>%s/---/—/gc</nowiki><br />
* <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)""<br />
* <nowiki>%s/|\([^|]*\)|/<code>\1<\/code>/gc</nowiki><br />
* <nowiki>%s/\\dots/.../gc</nowiki><br />
* <nowiki>%s/^\\label{.*$//gc</nowiki><br />
* <nowiki>%s/\\emph{\([^}]*\)}/''\1''/gc</nowiki><br />
* <nowiki>%s/\\term{\([^}]*\)}/''\1''/gc</nowiki><br />
<br />
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<br />
[[Media:Typeclassopedia.pdf|original Typeclassopedia PDF]]<br />
with the<br />
[http://code.haskell.org/~byorgey/TMR/Issue13/typeclassopedia.bib original bibliography file].<br />
<br />
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.<br />
<br />
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:<br />
<br />
* <nowiki>egrep -o "\cite\{[^\}]*\}" ~/typeclassopedia.lhs | cut -c 6- | tr "," "\n" | tr -d "}" > /tmp/citations</nowiki><br />
* <nowiki>for i in $(cat /tmp/citations); do grep -A99 "$i" ~/typeclassopedia.bib|egrep -B99 '^\}$' -m1 ; done > ~/typeclasso-refs-sorted</nowiki><br />
<br />
[[Category:Applicative Functor]]<br />
[[Category:Arrow]]<br />
[[Category:Functor]]<br />
[[Category:Monad]]<br />
[[Category:Standard classes]]<br />
[[Category:Standard libraries]]<br />
[[Category:Standard packages]]<br />
[[Category:Standard types]]</div>Tkempshttps://wiki.haskell.org/index.php?title=Attribute_grammar&diff=62300Attribute grammar2018-01-26T07:06:45Z<p>Tkemps: /* Portals or other rich resources */</p>
<hr />
<div>__TOC__<br />
<br />
== Introduction ==<br />
<br />
Especially for functional programmers (with Haskell examples) -- see<br />
Wouter Swierstra: [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4/Why_Attribute_Grammars_Matter Why Attribute Grammars Matter] (published in [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4 The Monad.Reader, Issue Four]).<br />
And also very deep connections with<br />
* catamorphisms, category theory<br />
* attribute grammars<br />
are described there.<br />
And there are links from that article to other materials providing deep insights in these powerful theories: attribute grammar tools and articles of the Essential Haskell Compiler project.<br />
<br />
It is no wonder that it is exactly a compiler project homepage that provides good circular programming and attribute grammar tutorials<br />
* both on its [http://www.cs.uu.nl/wiki/Ehc/WebHome project page]<br />
* and on a separate [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem attribute grammar] page:<br />
the reason may be that a compiler project is complex enough to require good [[separation of concerns]], modularity, reuse (goals of Aspect Oriented Programming, too), and attribute grammars are powerful tools to achieve these goals.<br />
<br />
* [http://en.wikipedia.org/wiki/Attribute_grammar Wikipedia article]<br />
* An Attribute Grammar tutorial by Swiestra [http://foswiki.cs.uu.nl/foswiki/pub/Swierstra/ResearchTalks/AgTutorial-org.pdf Attribute grammars – A short tutorial]<br />
* Also [http://www.haskell.org/happy/ Happy] contains AG-related capabilities, see [http://www.haskell.org/happy/doc/html/sec-AttributeGrammar.html Chapter 4. Attribute Grammars]<br />
* Attribute Grammars were originally proposed be Donald E. Knuth in [http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.455.1434&rep=rep1&type=pdf Semantics of Context-Free Languages].<br />
* Knuth describes the origin of the ideas behind Attribute Grammars in [http://www.dcs.warwick.ac.uk/~sk/cs325/gag.pdf The Genesis of Attribute Grammars]. Furthermore he gives a rich bibliography and some error corrections for some of his papers there.<br />
<br />
== Separation of concerns ==<br />
<br />
Both Swiestra's article (mentioned above) and the Essential Haskell Compiler Project tool (mentioned below) remarks the connection of attribute grammars to the topic of [[separation of concerns]], a goal of aspect orinted programming. This goal can be achieved by multiple ways in functional programming, see the concepts [[monad]] and [[arrow]]). Swiestra'a article mentions analogies between<br />
* attribute grammars,<br />
* (Reader, Writer, State) [[monad]]s,<br />
* (fan-out operation of) [[arrow]]s.<br />
<br />
== Portals or other rich resources ==<br />
<br />
* [http://foswiki.cs.uu.nl/foswiki/HUT/AttributeGrammarSystem Attribute Grammar page of Utrecht Univesity]<br />
* [https://www.haskell.org/happy/doc/html/sec-AtrributeGrammarsInHappy.html Description of Attibute Grammar aspects in the Happy parser generator system for Haskell]<br />
* [http://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/index.html The FNC-2 Attribute Grammar System]<br />
* [http://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/AGtexte.html Resources for Attribute Grammar research by Didier Parigot (INRIA)]<br />
* Team OSCAR at INRIA is stopped since 1999 but the following page contains some older publications: [https://www.inria.fr/en/teams/oscar Former Team OSCAR Home Page]<br />
<br />
== Tools ==<br />
<br />
Utrecht University's [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem Attribute Grammar System] tools include also an attribute grammar compiler, UUAGC. The concept of attribute grammar was used in their [http://www.cs.uu.nl/wiki/Ehc/WebHome Essential Haskell Compiler] project, which gives us not only a working programming language, but also a good didactical material about using attribute grammars, e.g. in writing compilers.<br />
<br />
Albeits these materials are self-contained, they reveal that the theory of attribute grammars is related to other concepts<br />
([[circular programming]], catamorphism).<br />
<br />
Robert Dockins has reported [http://haskell.org/communities/06-2006/html/report.html#happy-ag Attribute Grammar Support for Happy] in the Haskell Communities and Activities Report (10th edititon, June 2006)<br />
<br />
== Related concepts ==<br />
<br />
* [[Circular programming]]<br />
* Catamorphism, see [[Category theory]]<br />
<br />
[[Category:Theoretical foundations]]<br />
[[Category:Compiler tools]]</div>Tkempshttps://wiki.haskell.org/index.php?title=Attribute_grammar&diff=62299Attribute grammar2018-01-26T07:01:25Z<p>Tkemps: /* Introduction */</p>
<hr />
<div>__TOC__<br />
<br />
== Introduction ==<br />
<br />
Especially for functional programmers (with Haskell examples) -- see<br />
Wouter Swierstra: [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4/Why_Attribute_Grammars_Matter Why Attribute Grammars Matter] (published in [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4 The Monad.Reader, Issue Four]).<br />
And also very deep connections with<br />
* catamorphisms, category theory<br />
* attribute grammars<br />
are described there.<br />
And there are links from that article to other materials providing deep insights in these powerful theories: attribute grammar tools and articles of the Essential Haskell Compiler project.<br />
<br />
It is no wonder that it is exactly a compiler project homepage that provides good circular programming and attribute grammar tutorials<br />
* both on its [http://www.cs.uu.nl/wiki/Ehc/WebHome project page]<br />
* and on a separate [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem attribute grammar] page:<br />
the reason may be that a compiler project is complex enough to require good [[separation of concerns]], modularity, reuse (goals of Aspect Oriented Programming, too), and attribute grammars are powerful tools to achieve these goals.<br />
<br />
* [http://en.wikipedia.org/wiki/Attribute_grammar Wikipedia article]<br />
* An Attribute Grammar tutorial by Swiestra [http://foswiki.cs.uu.nl/foswiki/pub/Swierstra/ResearchTalks/AgTutorial-org.pdf Attribute grammars – A short tutorial]<br />
* Also [http://www.haskell.org/happy/ Happy] contains AG-related capabilities, see [http://www.haskell.org/happy/doc/html/sec-AttributeGrammar.html Chapter 4. Attribute Grammars]<br />
* Attribute Grammars were originally proposed be Donald E. Knuth in [http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.455.1434&rep=rep1&type=pdf Semantics of Context-Free Languages].<br />
* Knuth describes the origin of the ideas behind Attribute Grammars in [http://www.dcs.warwick.ac.uk/~sk/cs325/gag.pdf The Genesis of Attribute Grammars]. Furthermore he gives a rich bibliography and some error corrections for some of his papers there.<br />
<br />
== Separation of concerns ==<br />
<br />
Both Swiestra's article (mentioned above) and the Essential Haskell Compiler Project tool (mentioned below) remarks the connection of attribute grammars to the topic of [[separation of concerns]], a goal of aspect orinted programming. This goal can be achieved by multiple ways in functional programming, see the concepts [[monad]] and [[arrow]]). Swiestra'a article mentions analogies between<br />
* attribute grammars,<br />
* (Reader, Writer, State) [[monad]]s,<br />
* (fan-out operation of) [[arrow]]s.<br />
<br />
== Portals or other rich resources ==<br />
<br />
[http://foswiki.cs.uu.nl/foswiki/HUT/AttributeGrammarSystem Attribute Grammar page of Utrecht Univesity]<br />
<br />
[https://www.haskell.org/happy/doc/html/sec-AtrributeGrammarsInHappy.html Description of Attibute Grammar aspects in the Happy parser generator system for Haskell]<br />
<br />
[http://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/index.html The FNC-2 Attribute Grammar System]<br />
<br />
[http://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/AGtexte.html Resources for Attribute Grammar research by Didier Parigot (INRIA)]<br />
<br />
Team OSCAR at INRIA is stopped since 1999 but the following page contains some older publications:<br />
<br />
[https://www.inria.fr/en/teams/oscar Former Team OSCAR Home Page]<br />
<br />
== Tools ==<br />
<br />
Utrecht University's [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem Attribute Grammar System] tools include also an attribute grammar compiler, UUAGC. The concept of attribute grammar was used in their [http://www.cs.uu.nl/wiki/Ehc/WebHome Essential Haskell Compiler] project, which gives us not only a working programming language, but also a good didactical material about using attribute grammars, e.g. in writing compilers.<br />
<br />
Albeits these materials are self-contained, they reveal that the theory of attribute grammars is related to other concepts<br />
([[circular programming]], catamorphism).<br />
<br />
Robert Dockins has reported [http://haskell.org/communities/06-2006/html/report.html#happy-ag Attribute Grammar Support for Happy] in the Haskell Communities and Activities Report (10th edititon, June 2006)<br />
<br />
== Related concepts ==<br />
<br />
* [[Circular programming]]<br />
* Catamorphism, see [[Category theory]]<br />
<br />
[[Category:Theoretical foundations]]<br />
[[Category:Compiler tools]]</div>Tkempshttps://wiki.haskell.org/index.php?title=Attribute_grammar&diff=62298Attribute grammar2018-01-26T06:55:54Z<p>Tkemps: /* Introduction */</p>
<hr />
<div>__TOC__<br />
<br />
== Introduction ==<br />
<br />
Especially for functional programmers (with Haskell examples) -- see<br />
Wouter Swierstra: [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4/Why_Attribute_Grammars_Matter Why Attribute Grammars Matter] (published in [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4 The Monad.Reader, Issue Four]).<br />
And also very deep connections with<br />
* catamorphisms, category theory<br />
* attribute grammars<br />
are described there.<br />
And there are links from that article to other materials providing deep insights in these powerful theories: attribute grammar tools and articles of the Essential Haskell Compiler project.<br />
<br />
It is no wonder that it is exactly a compiler project homepage that provides good circular programming and attribute grammar tutorials<br />
* both on its [http://www.cs.uu.nl/wiki/Ehc/WebHome project page]<br />
* and on a separate [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem attribute grammar] page:<br />
the reason may be that a compiler project is complex enough to require good [[separation of concerns]], modularity, reuse (goals of Aspect Oriented Programming, too), and attribute grammars are powerful tools to achieve these goals.<br />
<br />
* [http://en.wikipedia.org/wiki/Attribute_grammar Wikipedia article]<br />
* An Attribute Grammar tutorial by Swiestra [http://foswiki.cs.uu.nl/foswiki/pub/Swierstra/ResearchTalks/AgTutorial-org.pdf Attribute grammars – A short tutorial]<br />
* Also [http://www.haskell.org/happy/ Happy] contains AG-related capabilities, see [http://www.haskell.org/happy/doc/html/sec-AttributeGrammar.html Chapter 4. Attribute Grammars]<br />
* Attribute Grammars were originally proposed be Donald E. Knuth in [http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.455.1434&rep=rep1&type=pdf Semantics of Context-Free Languages].<br />
<br />
== Separation of concerns ==<br />
<br />
Both Swiestra's article (mentioned above) and the Essential Haskell Compiler Project tool (mentioned below) remarks the connection of attribute grammars to the topic of [[separation of concerns]], a goal of aspect orinted programming. This goal can be achieved by multiple ways in functional programming, see the concepts [[monad]] and [[arrow]]). Swiestra'a article mentions analogies between<br />
* attribute grammars,<br />
* (Reader, Writer, State) [[monad]]s,<br />
* (fan-out operation of) [[arrow]]s.<br />
<br />
== Portals or other rich resources ==<br />
<br />
[http://foswiki.cs.uu.nl/foswiki/HUT/AttributeGrammarSystem Attribute Grammar page of Utrecht Univesity]<br />
<br />
[https://www.haskell.org/happy/doc/html/sec-AtrributeGrammarsInHappy.html Description of Attibute Grammar aspects in the Happy parser generator system for Haskell]<br />
<br />
[http://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/index.html The FNC-2 Attribute Grammar System]<br />
<br />
[http://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/AGtexte.html Resources for Attribute Grammar research by Didier Parigot (INRIA)]<br />
<br />
Team OSCAR at INRIA is stopped since 1999 but the following page contains some older publications:<br />
<br />
[https://www.inria.fr/en/teams/oscar Former Team OSCAR Home Page]<br />
<br />
== Tools ==<br />
<br />
Utrecht University's [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem Attribute Grammar System] tools include also an attribute grammar compiler, UUAGC. The concept of attribute grammar was used in their [http://www.cs.uu.nl/wiki/Ehc/WebHome Essential Haskell Compiler] project, which gives us not only a working programming language, but also a good didactical material about using attribute grammars, e.g. in writing compilers.<br />
<br />
Albeits these materials are self-contained, they reveal that the theory of attribute grammars is related to other concepts<br />
([[circular programming]], catamorphism).<br />
<br />
Robert Dockins has reported [http://haskell.org/communities/06-2006/html/report.html#happy-ag Attribute Grammar Support for Happy] in the Haskell Communities and Activities Report (10th edititon, June 2006)<br />
<br />
== Related concepts ==<br />
<br />
* [[Circular programming]]<br />
* Catamorphism, see [[Category theory]]<br />
<br />
[[Category:Theoretical foundations]]<br />
[[Category:Compiler tools]]</div>Tkempshttps://wiki.haskell.org/index.php?title=Attribute_grammar&diff=62297Attribute grammar2018-01-26T06:50:53Z<p>Tkemps: /* Introduction */</p>
<hr />
<div>__TOC__<br />
<br />
== Introduction ==<br />
<br />
Especially for functional programmers (with Haskell examples) -- see<br />
Wouter Swierstra: [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4/Why_Attribute_Grammars_Matter Why Attribute Grammars Matter] (published in [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4 The Monad.Reader, Issue Four]).<br />
And also very deep connections with<br />
* catamorphisms, category theory<br />
* attribute grammars<br />
are described there.<br />
And there are links from that article to other materials providing deep insights in these powerful theories: attribute grammar tools and articles of the Essential Haskell Compiler project.<br />
<br />
It is no wonder that it is exactly a compiler project homepage that provides good circular programming and attribute grammar tutorials<br />
* both on its [http://www.cs.uu.nl/wiki/Ehc/WebHome project page]<br />
* and on a separate [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem attribute grammar] page:<br />
the reason may be that a compiler project is complex enough to require good [[separation of concerns]], modularity, reuse (goals of Aspect Oriented Programming, too), and attribute grammars are powerful tools to achieve these goals.<br />
<br />
* [http://en.wikipedia.org/wiki/Attribute_grammar Wikipedia article]<br />
* [http://www-rocq.inria.fr/oscar/www/fnc2/manual/node32.html A Tutorial Introduction to Attribute Grammars], contained in [http://www-rocq.inria.fr/oscar/www/fnc2/manual/manual.html The FNC-2 System User's Guide and Reference Manual], written by Martin JOURDAN and Didier PARIGOT<br />
* Also [http://www.haskell.org/happy/ Happy] contains AG-related capabilities, see [http://www.haskell.org/happy/doc/html/sec-AttributeGrammar.html Chapter 4. Attribute Grammars]<br />
* Attribute Grammars were originally proposed be Donald E. Knuth in [http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.455.1434&rep=rep1&type=pdf Semantics of Context-Free Languages].<br />
<br />
== Separation of concerns ==<br />
<br />
Both Swiestra's article (mentioned above) and the Essential Haskell Compiler Project tool (mentioned below) remarks the connection of attribute grammars to the topic of [[separation of concerns]], a goal of aspect orinted programming. This goal can be achieved by multiple ways in functional programming, see the concepts [[monad]] and [[arrow]]). Swiestra'a article mentions analogies between<br />
* attribute grammars,<br />
* (Reader, Writer, State) [[monad]]s,<br />
* (fan-out operation of) [[arrow]]s.<br />
<br />
== Portals or other rich resources ==<br />
<br />
[http://foswiki.cs.uu.nl/foswiki/HUT/AttributeGrammarSystem Attribute Grammar page of Utrecht Univesity]<br />
<br />
[https://www.haskell.org/happy/doc/html/sec-AtrributeGrammarsInHappy.html Description of Attibute Grammar aspects in the Happy parser generator system for Haskell]<br />
<br />
[http://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/index.html The FNC-2 Attribute Grammar System]<br />
<br />
[http://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/AGtexte.html Resources for Attribute Grammar research by Didier Parigot (INRIA)]<br />
<br />
Team OSCAR at INRIA is stopped since 1999 but the following page contains some older publications:<br />
<br />
[https://www.inria.fr/en/teams/oscar Former Team OSCAR Home Page]<br />
<br />
== Tools ==<br />
<br />
Utrecht University's [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem Attribute Grammar System] tools include also an attribute grammar compiler, UUAGC. The concept of attribute grammar was used in their [http://www.cs.uu.nl/wiki/Ehc/WebHome Essential Haskell Compiler] project, which gives us not only a working programming language, but also a good didactical material about using attribute grammars, e.g. in writing compilers.<br />
<br />
Albeits these materials are self-contained, they reveal that the theory of attribute grammars is related to other concepts<br />
([[circular programming]], catamorphism).<br />
<br />
Robert Dockins has reported [http://haskell.org/communities/06-2006/html/report.html#happy-ag Attribute Grammar Support for Happy] in the Haskell Communities and Activities Report (10th edititon, June 2006)<br />
<br />
== Related concepts ==<br />
<br />
* [[Circular programming]]<br />
* Catamorphism, see [[Category theory]]<br />
<br />
[[Category:Theoretical foundations]]<br />
[[Category:Compiler tools]]</div>Tkempshttps://wiki.haskell.org/index.php?title=The_Monad.Reader/Issue4/Why_Attribute_Grammars_Matter&diff=62296The Monad.Reader/Issue4/Why Attribute Grammars Matter2018-01-26T06:42:42Z<p>Tkemps: /* The attribute grammar solution */</p>
<hr />
<div>=Why Attribute Grammars Matter=<br />
:''by Wouter Swierstra for The Monad.Reader Issue Four''; 01-07-05<br />
<br />
==Introduction==<br />
Almost twenty years have passed since John Hughes influential paper [http://www.math.chalmers.se/~rjmh/Papers/whyfp.html Why Functional Programming Matters]. At the same time the first work on<br />
attribute grammars and their relation to functional programming<br />
appeared. Despite the growing popularity of functional programming,<br />
attribute grammars remain remarkably less renown.<br />
<br />
The purpose of this article is twofold. On the one hand it illustrates how<br />
functional programming sometimes scales poorly and how<br />
attribute grammars can remedy these problems. On the other hand it aims to<br />
provide a gentle introduction to attribute grammars for seasoned functional<br />
programmers.<br />
<br />
==The problem==<br />
John Hughes argues that with the increasing complexity of modern<br />
software systems, modularity has become of paramount importance to software<br />
development. Functional languages provide new kinds of ''glue'' that create<br />
new opportunities for more modular code. In particular, Hughes stresses<br />
the importance of higher-order functions and lazy evaluation. There are<br />
plenty of examples where this works nicely - yet situations arise where<br />
the glue that functional programming provides somehow isn't quite enough.<br />
<br />
Perhaps a small example is in order. Suppose we want to write a function<br />
<tt>diff :: [Float] -> [Float]</tt> that given a list <tt>xs</tt>, calculates a new list where every element <tt>x</tt> is replaced with the difference between <tt>x</tt> and the<br />
average of <tt>xs</tt>. Similar problems pop up in any library for performing<br />
statistical calculations.<br />
<br />
===Higher-order functions===<br />
Let's tackle the problem with some of Haskell's most powerful glue - higher-order functions. Any beginning Haskell programmer should be able to concoct the solution presented in Listing One. The average is computed using functions from the Prelude. The obvious function using this average is then mapped over the original list. So far, so good.<br />
<br />
<haskell><br />
--- Listing One ---<br />
<br />
diff :: [Float] -> [Float]<br />
diff xs = map (\x -> x - (avg xs)) xs<br />
<br />
avg :: [Float] -> Float<br />
avg xs = sum xs / genericLength xs<br />
</haskell><br />
<br />
There are, however, a few things swept under the rug in this example. First<br />
of all, this simple problem requires three traversals of the original<br />
list. Computing additional values from the original list will require even<br />
more traversals.<br />
<br />
Secondly, the solution is so concise because it depends on Prelude<br />
functions. If the values were stored in a slightly different data structure,<br />
the solution would require a lot of tedious work. We could, of course,<br />
define our own higher-order functions, such as <tt>map</tt> and <tt>fold</tt>, or even<br />
resort to generic programming. There are, however,<br />
more ways to skin this particular cat.<br />
<br />
This problem illustrates the sheer elegance of functional programming. We<br />
do pay a price for the succinctness of the solution. Multiple traversals<br />
and boilerplate code can both be quite a head-ache. If we want to perform<br />
complex computations over custom data structures, we may want to consider an<br />
alternative approach.<br />
<br />
Fortunately, as experienced functional programmers, we have another card up<br />
our sleeve.<br />
<br />
===Lazy evaluation===<br />
The second kind of glue that functional programming provides is lazy<br />
evaluation. In essence, lazy evaluation only evaluates expressions when<br />
they become absolutely necessary.<br />
<br />
In particular, lazy evaluation enables the definition of ''circular programs'' that bear a dangerous resemblance to undefined values. Circular<br />
programs tuple separate computations, relying on lazy evaluation to feed<br />
the results of one computation to another.<br />
<br />
In our example, we could simply compute the length and sum of the list at<br />
the same time:<br />
<br />
<haskell><br />
average :: [Float] -> Float<br />
average xs = let<br />
nil = (0.0, 0.0)<br />
cons x (s,l) = (x + s, 1.0 + l)<br />
(sum,length) = foldr cons nil xs<br />
in sum / length<br />
</haskell><br />
<br />
We can eliminate traversals by tupling computations! Can we compute the<br />
resulting list at the same time as computing the sum and length? Let's try:<br />
<br />
<haskell><br />
diff :: [Float] -> [Float]<br />
diff xs = let<br />
nil = (0.0, 0.0, [])<br />
cons x (s,l,rs) = (x+s, 1.0+l, (x - ....) : rs)<br />
(sum,length,res) = foldr cons nil xs<br />
in res<br />
</haskell><br />
<br />
We run into trouble when we try to use the average to construct the<br />
resulting list. The problem is, that we haven't computed the average, but<br />
somehow want to use it during the traversal. To solve this, we don't actually<br />
compute the resulting list, but rather compute a function taking the<br />
average to the resulting list:<br />
<br />
<haskell><br />
diff :: [Float] -> [Float]<br />
diff xs =<br />
let<br />
nil = (0.0, 0.0, \avg -> [])<br />
cons x (s,l,rs) = (x+s, 1.0+l, \avg -> (x - avg) : rs avg)<br />
(sum,length,res) = foldr cons nil xs<br />
in<br />
res (sum / length)<br />
</haskell><br />
<br />
We can generalize this idea a bit further. Suppose that we want to compute<br />
other values that use the average. We could just add an <tt>avg</tt> argument to<br />
every element of the tuple that needs the average. It is a bit nicer,<br />
however, to lift the <tt>avg</tt> argument outside the tuple. Our final listing<br />
now becomes:<br />
<br />
<haskell><br />
--- Listing Two ---<br />
<br />
diff :: [Float] -> [Float]<br />
diff xs =<br />
let<br />
nil avg = (0.0, 0.0, [])<br />
cons x fs avg =<br />
let<br />
(s,l,ds) = fs avg<br />
in<br />
(s+x,l+1.0,x-avg : ds)<br />
(sum,length,ds) = foldr cons nil xs (sum / length)<br />
in<br />
ds<br />
</haskell><br />
<br />
Now every element of the tuple can refer to the average, rather than just<br />
the final list.<br />
<br />
This ''credit card transformation'' eliminates multiple traversals by<br />
tupling computations. We use the average without worrying if we have<br />
actually managed to compute it. When we actually write the fold, however,<br />
we have to put our average where our mouth is. Fortunately, the <tt>sum</tt> and<br />
<tt>length</tt> don't depend on the average, so we are free to use these values to<br />
tie the recursive knot.<br />
<br />
The code in Listing Two only needs a single traversal and one<br />
higher-order function. It apparently solves the problems with the code in<br />
Listing One.<br />
<br />
Hold on a minute. What ever happened to the elegance of our previous<br />
solution? Our second solution appears to have sacrificed clarity for the<br />
sake of efficiency. Who in their right minds would want to write the code<br />
in Listing Two? I wouldn't. Maybe, just maybe, we can do a bit better.<br />
<br />
==Attribute Grammars==<br />
Before even explaining what an attribute grammar is, think back to when you<br />
first learned about ''folds''. Initially, a fold seems like a silly<br />
abstraction. Why should I bother writing simple functions as folds? After<br />
all, I already know how to write the straightforward solution. It's only<br />
after a great deal of experience with functional programming that you learn<br />
to recognize folds as actually being a worthwhile abstraction. Learning<br />
about attribute grammars is similar in more ways than one.<br />
<br />
So what are attribute grammars? I'll have a bit more to say about that<br />
later. For now, let's see what the attribute grammar solution to our<br />
running example looks like.<br />
<br />
===The attribute grammar solution===<br />
I'll introduce attribute grammars using the syntax of the [http://www.cs.uu.nl/wiki/bin/view/HUT/AttributeGrammarSystem Utrecht University Attribute Grammar] system or UUAG for short. The UUAG system takes a file<br />
containing an attribute grammar definition and generates a Haskell module<br />
containing ''semantic functions'', determined by the attribute grammar. The<br />
attribute grammar determines a computation over some data structure; the<br />
semantic functions correspond to the actual Haskell functions that perform<br />
the computation.<br />
<br />
Although the UUAG system's syntax closely resembles Haskell, it is<br />
important to realize that the UUAG system is a Haskell pre-processor and<br />
not a complete Haskell compiler.<br />
<br />
So what does an attribute grammar file look like? Well, first of all we<br />
have to declare the data structure we're working with. In our example, we<br />
simply have a list of Floats.<br />
<br />
<haskell><br />
--- Listing Three ---<br />
<br />
DATA Root<br />
| Root list : List<br />
DATA List<br />
| Nil<br />
| Cons head : Float tail : List<br />
</haskell><br />
<br />
Datatypes are declared with the keyword <tt>DATA</tt>, followed by a list of<br />
constructors. Every node explicitly gives the name and type of all its<br />
children. In our example we have an empty list <tt>Nil</tt> and a list constructor<br />
<tt>Cons</tt> with two children, <tt>head</tt> and <tt>tail</tt>. For reasons that will become<br />
apparent later on, we add an additional datatype corresponding to the root<br />
of our list.<br />
<br />
So now that we've declared our datatype, let's add some ''attributes''. If we<br />
want to compute the average element, we'll need the length of the<br />
list. Listing Four introduces our first attribute corresponding to<br />
a list's length.<br />
<br />
<haskell><br />
--- Listing Four ---<br />
<br />
ATTR List [|| length : Float]<br />
SEM List<br />
| Nil lhs.length = 0.0<br />
| Cons lhs.length = 1.0 + @tail.length<br />
</haskell><br />
<br />
Let's go over the code line by line.<br />
<br />
An attribute has to be declared before it can actually be defined. An<br />
attribute is declared using the <tt>ATTR</tt> statement. This example declares a<br />
single ''synthesized'' attribute called <tt>length</tt> of type <tt>Float</tt>. A<br />
synthesized attribute is typically a value you are trying to compute bottom<br />
up. Synthesized attributes are declared to the right of the second<br />
vertical bar. We'll see other kinds of attributes shortly.<br />
<br />
Now that we've declared our first attribute, we can actually define it. A<br />
<tt>SEM</tt> statement begins by declaring for which data type attributes are<br />
being defined. In our example we want to define an attribute on a <tt>List</tt>,<br />
hence we write <tt>SEM List</tt>. We can subsequently give attribute definitions<br />
for the constructors of our <tt>List</tt> data type.<br />
<br />
Every attribute definition consists of several parts. We begin by<br />
mentioning the constructor for which we define an attribute. In our example<br />
we give two definitions, one for <tt>Nil</tt> and one for <tt>Cons</tt>.<br />
<br />
The second part of the attribute definition describes which attribute is<br />
being defined. In our example we define the attribute <tt>length</tt> for the<br />
''left-hand side'', or <tt>lhs</tt>. A lot of the terminology associated with<br />
attribute grammars comes from the world of context-free grammars. As this<br />
tutorial focuses on functional programmers, rather than formal language<br />
gurus, feel free to read <tt>lhs</tt> as "parent node". It seems a bit odd to<br />
write <tt>lhs.length</tt> explicitly, but we'll see later on why merely writing<br />
<tt>length</tt> doesn't suffice.<br />
<br />
Basically, all we've only said that the two definitions define the <tt>length</tt><br />
of <tt>Nil</tt> and <tt>Cons</tt>. We still have to fill in the necessary definition. The<br />
actual definition of the attributes takes place to the right of the equals<br />
sign. Programmers are free to write any valid Haskell expression. In fact,<br />
the UUAG system does not analyse the attribute definitions at all, but merely<br />
copies them straight into the resulting Haskell module. In our example, we<br />
want the length of the empty list to be <tt>0.0</tt>. The case for <tt>Cons</tt> is a bit<br />
trickier.<br />
<br />
In the <tt>Cons</tt> case we want to increment the length computed so far. To<br />
do so we need to be able to refer to other attributes. In particular we<br />
want to refer to the <tt>length</tt> attribute of the tail. The expression<br />
<tt>@tail.length</tt> does just that. In general, you're free to refer to any<br />
synthesized attribute ''attr'' of a child node ''c'' by writing <tt>@c.attr</tt>.<br />
<br />
The <tt>length</tt> attribute can be depicted pictorally as follows:<br />
<br />
[[Image:WAGM-Avg.png|The length attribute]]<br />
<br />
'''Exercise:''' Declare and define a synthesized attribute <tt>sum</tt> that<br />
computes the sum of a <tt>List</tt>. You can refer to a value <tt>val</tt> stored at a node as <tt>@val</tt>. For instance, write <tt>@head</tt> to refer to the float stored in a <tt>Cons</tt> node. Draw the corresponding picture if you're stuck.<br />
<br />
Now we've defined <tt>length</tt> and <tt>sum</tt>, let's compute the average. We'll know the sum and the length of the entire list at the Root node. Using those attributes we can compute the average and ''broadcast'' the average through the rest of the list. Let's start with the picture this time:<br />
<br />
[[Image:WAGM-Length.png|The average attribute]]<br />
<br />
The previous synthesized attributes, <tt>length</tt> and <tt>sum</tt>, defined bottom-up<br />
computations. We're now in the situation, however, where we want to pass<br />
information through the tree from a parent node to its child nodes using an<br />
''inherited'' attribute. Listing Five defines an inherited attribute <tt>avg</tt><br />
that corresponds to the picture we just drew.<br />
<br />
<haskell><br />
--- Listing Five ---<br />
ATTR List [ avg : Float|| ]<br />
SEM Root<br />
| Root list.avg = @list.sum / @list.length<br />
<br />
SEM List<br />
| Cons tail.avg = @lhs.avg<br />
<br />
</haskell><br />
<br />
Inherited attributes are declared to the left of the two vertical<br />
bars. Once we've declared an inherited attribute <tt>avg</tt> on lists, we're<br />
obliged to define how every constructor passes an <tt>avg</tt> to its children of<br />
type <tt>List</tt>.<br />
<br />
In our example, there are only two constructors with children<br />
of type <tt>List</tt>, namely <tt>Root</tt> and <tt>Cons</tt>. At the <tt>Root</tt> we compute the<br />
average, using the synthesized attributes <tt>sum</tt> and <tt>length</tt>, and pass the<br />
result to the <tt>list</tt> child. At the <tt>Cons</tt> node, we merely copy down the<br />
<tt>avg</tt> we received from our parent. Analogous to synthesized attributes, we<br />
can refer to an inherited attribute <tt>attr</tt> by writing <tt>@lhs.attr</tt>.<br />
<br />
Admittedly, this inherited attribute is not terribly interesting. There are<br />
plenty of other examples, however, where an inherited attribute represents<br />
important contextual information. Think of passing around the set of<br />
assumptions when writing a type checker, for instance.<br />
<br />
'''Exercise:''' To complete the attribute grammar, define an attribute<br />
<tt>res</tt> that computes the resulting list. Should it be inherited or<br />
synthesized? You may want to draw a picture.<br />
<br />
===Running the UUAG===<br />
Now suppose you've completed the exercises and copied the examples in a<br />
single file called <tt>Diff.ag</tt>. How do we actually use the attribute grammar?<br />
This is were the UUAG compiler steps in. Running the UUAG compiler on the<br />
source attribute grammar file generates a new <tt>Diff.hs</tt> file, which we can<br />
then compile like any other Haskell file.<br />
<br />
<haskell><br />
> uuagc -a Diff.ag<br />
> ghci Diff.hs<br />
</haskell><br />
<br />
The <tt>Diff.hs</tt> file contains several ingredients.<br />
<br />
Firstly, new Haskell datatypes are generated corresponding to <tt>DATA</tt><br />
declarations in the attribute grammar. For every generated datatype a<br />
corresponding <tt>fold</tt> is generated. The attribute definitions determine the<br />
arguments passed to the folds. Browsing through the generated code can<br />
actually be quite instructive.<br />
<br />
Inherited attributes are passed to recursive calls of the fold. Synthesized<br />
attributes are tupled and returned as the result of the computation. In<br />
essence, we've reproduced our original solution in Listing Two - but now<br />
without the hassle associated with spelling out [[catamorphism]]s with a higher<br />
order domain and a compound codomain.<br />
<br />
The attribute grammar solution is just as efficient as our earlier solution<br />
relying on lazy evaluation, yet the code is hardly different from what we<br />
would write in a straightforward Haskell solution. It really is the best of<br />
both worlds. The two types of glue that John Hughes pinpoints in his<br />
original article just aren't enough. I would like to think that sometimes<br />
attribute grammars are sometimes capable of providing just the right<br />
missing bit of glue.<br />
<br />
===What are attribute grammars?===<br />
So just what are attribute grammars? Well, that depends on who you ask,<br />
really. I've tried to sum up some different views below.<br />
<br />
Attribute grammars add semantics to a context free grammar. Although it is<br />
easy enough to describe a language's syntax using a context free grammar,<br />
accurately describing a language's semantics is notoriously<br />
difficult. Attribute grammars specify a language's semantics by<br />
'decorating' a context free grammar with those attributes you are interested<br />
in.<br />
<br />
Attribute grammars describe tree traversals. All imperative implementations<br />
of attribute grammar systems perform tree traversals to compute some<br />
value. Basically an attribute grammar declares ''which'' values to compute<br />
and an attribute grammar system executes these computations. Once you've<br />
made this observation, the close relation to functional programming should<br />
not come as a surprise.<br />
<br />
Attribute grammars are a formalism for writing catamorphisms in a<br />
compositional fashion. Basically, the only thing the UUAG compiler does is<br />
generate large folds that I couldn't be bothered writing myself. It takes<br />
away all the elbow grease involved with maintaining and extending such<br />
code. In a sense the compiler does absolutely nothing new; it just makes<br />
life a lot easier.<br />
<br />
Attribute grammars provide framework for aspect oriented programming in<br />
functional languages. Lately there has been a lot of buzz about the<br />
importance of ''aspects'' and ''aspect oriented programming''. Attribute<br />
grammars provide a clear and well-established framework for splitting code<br />
into separate aspects. By spreading attribute definitions over several<br />
different files and grouping them according to aspect, attribute grammars<br />
provide a natural setting for aspect oriented programming.<br />
<br />
How do attribute grammars relate to other Haskell abstractions? I'll try to<br />
put my finger on some of the more obvious connections, but I'm pretty sure<br />
there's a great deal more that I don't cover here.<br />
<br />
==What else is out there?==<br />
Everyone loves monads. They're what makes IO possible in Haskell. There are<br />
extensive standard libraries and syntactic sugar specifically designed to<br />
make life with monads easier. There are an enormous number of Haskell<br />
libraries based on the monadic interface. They represent one of the most<br />
substantial developments of functional programming in the last decade.<br />
<br />
Yet somehow, the single most common question asked by fledgling Haskell<br />
programmers is probably ''What are monads?''. Beginners have a hard time<br />
grasping the concept of monads and yet connoisseurs recognize a monad in<br />
just about every code snippet. I think the more important question is:<br />
''What are monads good for?''<br />
<br />
Monads provide a simple yet powerful abstract notion of computation. In<br />
essence, a monad describes how sequence computations. This is crucial in<br />
order to perform IO in a functional language; by constraining all IO<br />
actions to a single interface of sequenced computations, the programmer is<br />
prevented from creating utter chaos. The real power of monads is in the<br />
interface they provide.<br />
<br />
John Hughes identified modularity as the single biggest blessing of<br />
functional programming. The obvious question is: how modular is the monadic<br />
interface? This really depends on you're definition of modularity. Let me<br />
be more specific. How can you combine two arbitrary monads? You can't. This<br />
is my greatest concern with monads. Once you choose your specific notion of<br />
computation, you have to stick to it through thick and thin.<br />
<br />
What about monad transformers? Monad transformers allow you to add a<br />
specific monad's functionality on top of any existing monad. What seems<br />
like a solution, more often than not, turns out to introduce more problems<br />
than you bargained for. Adding new functionality to a monad involves<br />
lifting all the computations from the previous monad to the new<br />
one. Although I could learn to live with this, it gets even worse. As every<br />
monad transformer really changes the underlying monad. The order in<br />
which monad transformers are applied really makes a difference. If I want<br />
to add error reporting and state to some existing monad, should I be forced<br />
to consider the order in which I add them?<br />
<br />
Monads are extremely worthwhile for the interface they provide. Monadic<br />
libraries are great, but changing and extending monadic code can be a<br />
pain. Can we do better? Well I probably wouldn't have started this monadic<br />
intermezzo if I didn't have some sort of answer.<br />
<br />
Let's start off with <tt>Reader</tt> monads, for instance. Essentially, <tt>Reader</tt><br />
monads adds an argument to some computation. Wait a minute, this reminds me<br />
of inherited attributes. What about <tt>Writer</tt> monads? They correspond to<br />
synthesized attributes of course. Finally, <tt>State</tt> monads correspond to<br />
''chained'' attributes, or attributes that are both synthesized and<br />
inherited. The real edge attribute grammars hold over monad transformers is that you<br />
can define new attributes ''without'' worrying about the order in which you<br />
define them or adapting existing code.<br />
<br />
Do other abstractions capture other notions related to attribute grammars?<br />
Of course they do! Just look at the function space arrows instance. The notion of<br />
combining two distinct computations using the <tt>(&&&)</tt> operator relates to<br />
the concept of ''joining'' two attribute grammars by collecting their<br />
attribute definitions. When you look at the <tt>loop</tt> combinator, I can<br />
only be grateful that an attribute grammar system deals with attribute<br />
dependencies automatically.<br />
<br />
There really is a lot of related work. Implicit parameters? Inherited attributes! Linear implicit parameters? Chained attributes! Concepts that are so natural in the setting of attribute grammars, yet seem contrived when added to Haskell. This strengthens my belief that functional programmers can really benefit from even the most fleeting experience with attribute grammars; although I'd like to think that if you've read this far, you're hungry for more.<br />
<br />
==Further reading==<br />
This more or less covers the tutorial section of this article. The best way<br />
to learn more about attribute grammars is by actually using them. To conclude the tutorial, I've<br />
included a small example for you to play with. I've written a parser for a very<br />
simple wiki formatting language not entirely unlike the one used to produce this<br />
document. So far the HTML generated after parsing a document is fairly poor. It's up to<br />
you to improve it!<br />
<br />
You can download the initial version here. Don't forget to install the [http://www.cs.uu.nl/wiki/bin/view/HUT/Download]. It might<br />
be worthwhile to have a look at the [http://www.cs.uu.nl/wiki/bin/view/HUT/AttributeGrammarManual UUAG manual] as there's a lot of technical detail that I haven't mentioned.<br />
<br />
If you're particularly daring, you may want to take a look at the [http://www.cs.uu.nl/wiki/Ehc/WebHome Essential Haskell Compiler] being developed at Utrecht. It's almost completely written using the UUAG and is designed to be suitable for education and experimentation. The compiler was presented at the Summer School for Advanced Functional Programming in Tartu, Estonia last summer. As a result, there's a lot written about it already.<br />
<br />
Dive on in!<br />
<br />
[[Category:Article]]</div>Tkempshttps://wiki.haskell.org/index.php?title=The_Monad.Reader/Issue4/Why_Attribute_Grammars_Matter&diff=62295The Monad.Reader/Issue4/Why Attribute Grammars Matter2018-01-26T06:40:20Z<p>Tkemps: /* The attribute grammar solution */</p>
<hr />
<div>=Why Attribute Grammars Matter=<br />
:''by Wouter Swierstra for The Monad.Reader Issue Four''; 01-07-05<br />
<br />
==Introduction==<br />
Almost twenty years have passed since John Hughes influential paper [http://www.math.chalmers.se/~rjmh/Papers/whyfp.html Why Functional Programming Matters]. At the same time the first work on<br />
attribute grammars and their relation to functional programming<br />
appeared. Despite the growing popularity of functional programming,<br />
attribute grammars remain remarkably less renown.<br />
<br />
The purpose of this article is twofold. On the one hand it illustrates how<br />
functional programming sometimes scales poorly and how<br />
attribute grammars can remedy these problems. On the other hand it aims to<br />
provide a gentle introduction to attribute grammars for seasoned functional<br />
programmers.<br />
<br />
==The problem==<br />
John Hughes argues that with the increasing complexity of modern<br />
software systems, modularity has become of paramount importance to software<br />
development. Functional languages provide new kinds of ''glue'' that create<br />
new opportunities for more modular code. In particular, Hughes stresses<br />
the importance of higher-order functions and lazy evaluation. There are<br />
plenty of examples where this works nicely - yet situations arise where<br />
the glue that functional programming provides somehow isn't quite enough.<br />
<br />
Perhaps a small example is in order. Suppose we want to write a function<br />
<tt>diff :: [Float] -> [Float]</tt> that given a list <tt>xs</tt>, calculates a new list where every element <tt>x</tt> is replaced with the difference between <tt>x</tt> and the<br />
average of <tt>xs</tt>. Similar problems pop up in any library for performing<br />
statistical calculations.<br />
<br />
===Higher-order functions===<br />
Let's tackle the problem with some of Haskell's most powerful glue - higher-order functions. Any beginning Haskell programmer should be able to concoct the solution presented in Listing One. The average is computed using functions from the Prelude. The obvious function using this average is then mapped over the original list. So far, so good.<br />
<br />
<haskell><br />
--- Listing One ---<br />
<br />
diff :: [Float] -> [Float]<br />
diff xs = map (\x -> x - (avg xs)) xs<br />
<br />
avg :: [Float] -> Float<br />
avg xs = sum xs / genericLength xs<br />
</haskell><br />
<br />
There are, however, a few things swept under the rug in this example. First<br />
of all, this simple problem requires three traversals of the original<br />
list. Computing additional values from the original list will require even<br />
more traversals.<br />
<br />
Secondly, the solution is so concise because it depends on Prelude<br />
functions. If the values were stored in a slightly different data structure,<br />
the solution would require a lot of tedious work. We could, of course,<br />
define our own higher-order functions, such as <tt>map</tt> and <tt>fold</tt>, or even<br />
resort to generic programming. There are, however,<br />
more ways to skin this particular cat.<br />
<br />
This problem illustrates the sheer elegance of functional programming. We<br />
do pay a price for the succinctness of the solution. Multiple traversals<br />
and boilerplate code can both be quite a head-ache. If we want to perform<br />
complex computations over custom data structures, we may want to consider an<br />
alternative approach.<br />
<br />
Fortunately, as experienced functional programmers, we have another card up<br />
our sleeve.<br />
<br />
===Lazy evaluation===<br />
The second kind of glue that functional programming provides is lazy<br />
evaluation. In essence, lazy evaluation only evaluates expressions when<br />
they become absolutely necessary.<br />
<br />
In particular, lazy evaluation enables the definition of ''circular programs'' that bear a dangerous resemblance to undefined values. Circular<br />
programs tuple separate computations, relying on lazy evaluation to feed<br />
the results of one computation to another.<br />
<br />
In our example, we could simply compute the length and sum of the list at<br />
the same time:<br />
<br />
<haskell><br />
average :: [Float] -> Float<br />
average xs = let<br />
nil = (0.0, 0.0)<br />
cons x (s,l) = (x + s, 1.0 + l)<br />
(sum,length) = foldr cons nil xs<br />
in sum / length<br />
</haskell><br />
<br />
We can eliminate traversals by tupling computations! Can we compute the<br />
resulting list at the same time as computing the sum and length? Let's try:<br />
<br />
<haskell><br />
diff :: [Float] -> [Float]<br />
diff xs = let<br />
nil = (0.0, 0.0, [])<br />
cons x (s,l,rs) = (x+s, 1.0+l, (x - ....) : rs)<br />
(sum,length,res) = foldr cons nil xs<br />
in res<br />
</haskell><br />
<br />
We run into trouble when we try to use the average to construct the<br />
resulting list. The problem is, that we haven't computed the average, but<br />
somehow want to use it during the traversal. To solve this, we don't actually<br />
compute the resulting list, but rather compute a function taking the<br />
average to the resulting list:<br />
<br />
<haskell><br />
diff :: [Float] -> [Float]<br />
diff xs =<br />
let<br />
nil = (0.0, 0.0, \avg -> [])<br />
cons x (s,l,rs) = (x+s, 1.0+l, \avg -> (x - avg) : rs avg)<br />
(sum,length,res) = foldr cons nil xs<br />
in<br />
res (sum / length)<br />
</haskell><br />
<br />
We can generalize this idea a bit further. Suppose that we want to compute<br />
other values that use the average. We could just add an <tt>avg</tt> argument to<br />
every element of the tuple that needs the average. It is a bit nicer,<br />
however, to lift the <tt>avg</tt> argument outside the tuple. Our final listing<br />
now becomes:<br />
<br />
<haskell><br />
--- Listing Two ---<br />
<br />
diff :: [Float] -> [Float]<br />
diff xs =<br />
let<br />
nil avg = (0.0, 0.0, [])<br />
cons x fs avg =<br />
let<br />
(s,l,ds) = fs avg<br />
in<br />
(s+x,l+1.0,x-avg : ds)<br />
(sum,length,ds) = foldr cons nil xs (sum / length)<br />
in<br />
ds<br />
</haskell><br />
<br />
Now every element of the tuple can refer to the average, rather than just<br />
the final list.<br />
<br />
This ''credit card transformation'' eliminates multiple traversals by<br />
tupling computations. We use the average without worrying if we have<br />
actually managed to compute it. When we actually write the fold, however,<br />
we have to put our average where our mouth is. Fortunately, the <tt>sum</tt> and<br />
<tt>length</tt> don't depend on the average, so we are free to use these values to<br />
tie the recursive knot.<br />
<br />
The code in Listing Two only needs a single traversal and one<br />
higher-order function. It apparently solves the problems with the code in<br />
Listing One.<br />
<br />
Hold on a minute. What ever happened to the elegance of our previous<br />
solution? Our second solution appears to have sacrificed clarity for the<br />
sake of efficiency. Who in their right minds would want to write the code<br />
in Listing Two? I wouldn't. Maybe, just maybe, we can do a bit better.<br />
<br />
==Attribute Grammars==<br />
Before even explaining what an attribute grammar is, think back to when you<br />
first learned about ''folds''. Initially, a fold seems like a silly<br />
abstraction. Why should I bother writing simple functions as folds? After<br />
all, I already know how to write the straightforward solution. It's only<br />
after a great deal of experience with functional programming that you learn<br />
to recognize folds as actually being a worthwhile abstraction. Learning<br />
about attribute grammars is similar in more ways than one.<br />
<br />
So what are attribute grammars? I'll have a bit more to say about that<br />
later. For now, let's see what the attribute grammar solution to our<br />
running example looks like.<br />
<br />
===The attribute grammar solution===<br />
I'll introduce attribute grammars using the syntax of the [http://www.cs.uu.nl/wiki/bin/view/HUT/AttributeGrammarSystem Utrecht University Attribute Grammar] system or UUAG for short. The UUAG system takes a file<br />
containing an attribute grammar definition and generates a Haskell module<br />
containing ''semantic functions'', determined by the attribute grammar. The<br />
attribute grammar determines a computation over some data structure; the<br />
semantic functions correspond to the actual Haskell functions that perform<br />
the computation.<br />
<br />
Although the UUAG system's syntax closely resembles Haskell, it is<br />
important to realize that the UUAG system is a Haskell pre-processor and<br />
not a complete Haskell compiler.<br />
<br />
So what does an attribute grammar file look like? Well, first of all we<br />
have to declare the data structure we're working with. In our example, we<br />
simply have a list of Floats.<br />
<br />
<haskell><br />
--- Listing Three ---<br />
<br />
DATA Root<br />
| Root list : List<br />
DATA List<br />
| Nil<br />
| Cons head : Float tail : List<br />
</haskell><br />
<br />
Datatypes are declared with the keyword <tt>DATA</tt>, followed by a list of<br />
constructors. Every node explicitly gives the name and type of all its<br />
children. In our example we have an empty list <tt>Nil</tt> and a list constructor<br />
<tt>Cons</tt> with two children, <tt>head</tt> and <tt>tail</tt>. For reasons that will become<br />
apparent later on, we add an additional datatype corresponding to the root<br />
of our list.<br />
<br />
So now that we've declared our datatype, let's add some ''attributes''. If we<br />
want to compute the average element, we'll need the length of the<br />
list. Listing Four introduces our first attribute corresponding to<br />
a list's length.<br />
<br />
<haskell><br />
--- Listing Four ---<br />
<br />
ATTR List [|| length : Float]<br />
SEM List<br />
| Nil lhs.length = 0.0<br />
| Cons lhs.length = 1.0 + @tail.length<br />
</haskell><br />
<br />
Let's go over the code line by line.<br />
<br />
An attribute has to be declared before it can actually be defined. An<br />
attribute is declared using the <tt>ATTR</tt> statement. This example declares a<br />
single ''synthesized'' attribute called <tt>length</tt> of type <tt>Float</tt>. A<br />
synthesized attribute is typically a value you are trying to compute bottom<br />
up. Synthesized attributes are declared to the right of the second<br />
vertical bar. We'll see other kinds of attributes shortly.<br />
<br />
Now that we've declared our first attribute, we can actually define it. A<br />
<tt>SEM</tt> statement begins by declaring for which data type attributes are<br />
being defined. In our example we want to define an attribute on a <tt>List</tt>,<br />
hence we write <tt>SEM List</tt>. We can subsequently give attribute definitions<br />
for the constructors of our <tt>List</tt> data type.<br />
<br />
Every attribute definition consists of several parts. We begin by<br />
mentioning the constructor for which we define an attribute. In our example<br />
we give two definitions, one for <tt>Nil</tt> and one for <tt>Cons</tt>.<br />
<br />
The second part of the attribute definition describes which attribute is<br />
being defined. In our example we define the attribute <tt>length</tt> for the<br />
''left-hand side'', or <tt>lhs</tt>. A lot of the terminology associated with<br />
attribute grammars comes from the world of context-free grammars. As this<br />
tutorial focuses on functional programmers, rather than formal language<br />
gurus, feel free to read <tt>lhs</tt> as "parent node". It seems a bit odd to<br />
write <tt>lhs.length</tt> explicitly, but we'll see later on why merely writing<br />
<tt>length</tt> doesn't suffice.<br />
<br />
Basically, all we've only said that the two definitions define the <tt>length</tt><br />
of <tt>Nil</tt> and <tt>Cons</tt>. We still have to fill in the necessary definition. The<br />
actual definition of the attributes takes place to the right of the equals<br />
sign. Programmers are free to write any valid Haskell expression. In fact,<br />
the UUAG system does not analyse the attribute definitions at all, but merely<br />
copies them straight into the resulting Haskell module. In our example, we<br />
want the length of the empty list to be <tt>0.0</tt>. The case for <tt>Cons</tt> is a bit<br />
trickier.<br />
<br />
In the <tt>Cons</tt> case we want to be increment the length computed so far. To<br />
do so we need to be able to refer to other attributes. In particular we<br />
want to refer to the <tt>length</tt> attribute of the tail. The expression<br />
<tt>@tail.length</tt> does just that. In general, you're free to refer to any<br />
synthesized attribute ''attr'' of a child node ''c'' by writing <tt>@c.attr</tt>.<br />
<br />
The <tt>length</tt> attribute can be depicted pictorally as follows:<br />
<br />
[[Image:WAGM-Avg.png|The length attribute]]<br />
<br />
'''Exercise:''' Declare and define a synthesized attribute <tt>sum</tt> that<br />
computes the sum of a <tt>List</tt>. You can refer to a value <tt>val</tt> stored at a<br />
node as <tt>@val</tt>. For instance, write <tt>@head</tt> to refer to the float stored in<br />
at a <tt>Cons</tt> node. Draw the corresponding picture if you're stuck.<br />
<br />
Now we've defined <tt>length</tt> and <tt>sum</tt>, let's compute the average. We'll know<br />
the sum and the length of the entire list at the Root node. Using those<br />
attributes we can compute the average and ''broadcast'' the average through<br />
the rest of the list. Let's start with the picture this time:<br />
<br />
[[Image:WAGM-Length.png|The average attribute]]<br />
<br />
The previous synthesized attributes, <tt>length</tt> and <tt>sum</tt>, defined bottom-up<br />
computations. We're now in the situation, however, where we want to pass<br />
information through the tree from a parent node to its child nodes using an<br />
''inherited'' attribute. Listing Five defines an inherited attribute <tt>avg</tt><br />
that corresponds to the picture we just drew.<br />
<br />
<haskell><br />
--- Listing Five ---<br />
ATTR List [ avg : Float|| ]<br />
SEM Root<br />
| Root list.avg = @list.sum / @list.length<br />
<br />
SEM List<br />
| Cons tail.avg = @lhs.avg<br />
<br />
</haskell><br />
<br />
Inherited attributes are declared to the left of the two vertical<br />
bars. Once we've declared an inherited attribute <tt>avg</tt> on lists, we're<br />
obliged to define how every constructor passes an <tt>avg</tt> to its children of<br />
type <tt>List</tt>.<br />
<br />
In our example, there are only two constructors with children<br />
of type <tt>List</tt>, namely <tt>Root</tt> and <tt>Cons</tt>. At the <tt>Root</tt> we compute the<br />
average, using the synthesized attributes <tt>sum</tt> and <tt>length</tt>, and pass the<br />
result to the <tt>list</tt> child. At the <tt>Cons</tt> node, we merely copy down the<br />
<tt>avg</tt> we received from our parent. Analogous to synthesized attributes, we<br />
can refer to an inherited attribute <tt>attr</tt> by writing <tt>@lhs.attr</tt>.<br />
<br />
Admittedly, this inherited attribute is not terribly interesting. There are<br />
plenty of other examples, however, where an inherited attribute represents<br />
important contextual information. Think of passing around the set of<br />
assumptions when writing a type checker, for instance.<br />
<br />
'''Exercise:''' To complete the attribute grammar, define an attribute<br />
<tt>res</tt> that computes the resulting list. Should it be inherited or<br />
synthesized? You may want to draw a picture.<br />
<br />
===Running the UUAG===<br />
Now suppose you've completed the exercises and copied the examples in a<br />
single file called <tt>Diff.ag</tt>. How do we actually use the attribute grammar?<br />
This is were the UUAG compiler steps in. Running the UUAG compiler on the<br />
source attribute grammar file generates a new <tt>Diff.hs</tt> file, which we can<br />
then compile like any other Haskell file.<br />
<br />
<haskell><br />
> uuagc -a Diff.ag<br />
> ghci Diff.hs<br />
</haskell><br />
<br />
The <tt>Diff.hs</tt> file contains several ingredients.<br />
<br />
Firstly, new Haskell datatypes are generated corresponding to <tt>DATA</tt><br />
declarations in the attribute grammar. For every generated datatype a<br />
corresponding <tt>fold</tt> is generated. The attribute definitions determine the<br />
arguments passed to the folds. Browsing through the generated code can<br />
actually be quite instructive.<br />
<br />
Inherited attributes are passed to recursive calls of the fold. Synthesized<br />
attributes are tupled and returned as the result of the computation. In<br />
essence, we've reproduced our original solution in Listing Two - but now<br />
without the hassle associated with spelling out [[catamorphism]]s with a higher<br />
order domain and a compound codomain.<br />
<br />
The attribute grammar solution is just as efficient as our earlier solution<br />
relying on lazy evaluation, yet the code is hardly different from what we<br />
would write in a straightforward Haskell solution. It really is the best of<br />
both worlds. The two types of glue that John Hughes pinpoints in his<br />
original article just aren't enough. I would like to think that sometimes<br />
attribute grammars are sometimes capable of providing just the right<br />
missing bit of glue.<br />
<br />
===What are attribute grammars?===<br />
So just what are attribute grammars? Well, that depends on who you ask,<br />
really. I've tried to sum up some different views below.<br />
<br />
Attribute grammars add semantics to a context free grammar. Although it is<br />
easy enough to describe a language's syntax using a context free grammar,<br />
accurately describing a language's semantics is notoriously<br />
difficult. Attribute grammars specify a language's semantics by<br />
'decorating' a context free grammar with those attributes you are interested<br />
in.<br />
<br />
Attribute grammars describe tree traversals. All imperative implementations<br />
of attribute grammar systems perform tree traversals to compute some<br />
value. Basically an attribute grammar declares ''which'' values to compute<br />
and an attribute grammar system executes these computations. Once you've<br />
made this observation, the close relation to functional programming should<br />
not come as a surprise.<br />
<br />
Attribute grammars are a formalism for writing catamorphisms in a<br />
compositional fashion. Basically, the only thing the UUAG compiler does is<br />
generate large folds that I couldn't be bothered writing myself. It takes<br />
away all the elbow grease involved with maintaining and extending such<br />
code. In a sense the compiler does absolutely nothing new; it just makes<br />
life a lot easier.<br />
<br />
Attribute grammars provide framework for aspect oriented programming in<br />
functional languages. Lately there has been a lot of buzz about the<br />
importance of ''aspects'' and ''aspect oriented programming''. Attribute<br />
grammars provide a clear and well-established framework for splitting code<br />
into separate aspects. By spreading attribute definitions over several<br />
different files and grouping them according to aspect, attribute grammars<br />
provide a natural setting for aspect oriented programming.<br />
<br />
How do attribute grammars relate to other Haskell abstractions? I'll try to<br />
put my finger on some of the more obvious connections, but I'm pretty sure<br />
there's a great deal more that I don't cover here.<br />
<br />
==What else is out there?==<br />
Everyone loves monads. They're what makes IO possible in Haskell. There are<br />
extensive standard libraries and syntactic sugar specifically designed to<br />
make life with monads easier. There are an enormous number of Haskell<br />
libraries based on the monadic interface. They represent one of the most<br />
substantial developments of functional programming in the last decade.<br />
<br />
Yet somehow, the single most common question asked by fledgling Haskell<br />
programmers is probably ''What are monads?''. Beginners have a hard time<br />
grasping the concept of monads and yet connoisseurs recognize a monad in<br />
just about every code snippet. I think the more important question is:<br />
''What are monads good for?''<br />
<br />
Monads provide a simple yet powerful abstract notion of computation. In<br />
essence, a monad describes how sequence computations. This is crucial in<br />
order to perform IO in a functional language; by constraining all IO<br />
actions to a single interface of sequenced computations, the programmer is<br />
prevented from creating utter chaos. The real power of monads is in the<br />
interface they provide.<br />
<br />
John Hughes identified modularity as the single biggest blessing of<br />
functional programming. The obvious question is: how modular is the monadic<br />
interface? This really depends on you're definition of modularity. Let me<br />
be more specific. How can you combine two arbitrary monads? You can't. This<br />
is my greatest concern with monads. Once you choose your specific notion of<br />
computation, you have to stick to it through thick and thin.<br />
<br />
What about monad transformers? Monad transformers allow you to add a<br />
specific monad's functionality on top of any existing monad. What seems<br />
like a solution, more often than not, turns out to introduce more problems<br />
than you bargained for. Adding new functionality to a monad involves<br />
lifting all the computations from the previous monad to the new<br />
one. Although I could learn to live with this, it gets even worse. As every<br />
monad transformer really changes the underlying monad. The order in<br />
which monad transformers are applied really makes a difference. If I want<br />
to add error reporting and state to some existing monad, should I be forced<br />
to consider the order in which I add them?<br />
<br />
Monads are extremely worthwhile for the interface they provide. Monadic<br />
libraries are great, but changing and extending monadic code can be a<br />
pain. Can we do better? Well I probably wouldn't have started this monadic<br />
intermezzo if I didn't have some sort of answer.<br />
<br />
Let's start off with <tt>Reader</tt> monads, for instance. Essentially, <tt>Reader</tt><br />
monads adds an argument to some computation. Wait a minute, this reminds me<br />
of inherited attributes. What about <tt>Writer</tt> monads? They correspond to<br />
synthesized attributes of course. Finally, <tt>State</tt> monads correspond to<br />
''chained'' attributes, or attributes that are both synthesized and<br />
inherited. The real edge attribute grammars hold over monad transformers is that you<br />
can define new attributes ''without'' worrying about the order in which you<br />
define them or adapting existing code.<br />
<br />
Do other abstractions capture other notions related to attribute grammars?<br />
Of course they do! Just look at the function space arrows instance. The notion of<br />
combining two distinct computations using the <tt>(&&&)</tt> operator relates to<br />
the concept of ''joining'' two attribute grammars by collecting their<br />
attribute definitions. When you look at the <tt>loop</tt> combinator, I can<br />
only be grateful that an attribute grammar system deals with attribute<br />
dependencies automatically.<br />
<br />
There really is a lot of related work. Implicit parameters? Inherited attributes! Linear implicit parameters? Chained attributes! Concepts that are so natural in the setting of attribute grammars, yet seem contrived when added to Haskell. This strengthens my belief that functional programmers can really benefit from even the most fleeting experience with attribute grammars; although I'd like to think that if you've read this far, you're hungry for more.<br />
<br />
==Further reading==<br />
This more or less covers the tutorial section of this article. The best way<br />
to learn more about attribute grammars is by actually using them. To conclude the tutorial, I've<br />
included a small example for you to play with. I've written a parser for a very<br />
simple wiki formatting language not entirely unlike the one used to produce this<br />
document. So far the HTML generated after parsing a document is fairly poor. It's up to<br />
you to improve it!<br />
<br />
You can download the initial version here. Don't forget to install the [http://www.cs.uu.nl/wiki/bin/view/HUT/Download]. It might<br />
be worthwhile to have a look at the [http://www.cs.uu.nl/wiki/bin/view/HUT/AttributeGrammarManual UUAG manual] as there's a lot of technical detail that I haven't mentioned.<br />
<br />
If you're particularly daring, you may want to take a look at the [http://www.cs.uu.nl/wiki/Ehc/WebHome Essential Haskell Compiler] being developed at Utrecht. It's almost completely written using the UUAG and is designed to be suitable for education and experimentation. The compiler was presented at the Summer School for Advanced Functional Programming in Tartu, Estonia last summer. As a result, there's a lot written about it already.<br />
<br />
Dive on in!<br />
<br />
[[Category:Article]]</div>Tkempshttps://wiki.haskell.org/index.php?title=The_Monad.Reader/Issue4/Why_Attribute_Grammars_Matter&diff=62294The Monad.Reader/Issue4/Why Attribute Grammars Matter2018-01-26T06:38:53Z<p>Tkemps: /* The attribute grammar solution */</p>
<hr />
<div>=Why Attribute Grammars Matter=<br />
:''by Wouter Swierstra for The Monad.Reader Issue Four''; 01-07-05<br />
<br />
==Introduction==<br />
Almost twenty years have passed since John Hughes influential paper [http://www.math.chalmers.se/~rjmh/Papers/whyfp.html Why Functional Programming Matters]. At the same time the first work on<br />
attribute grammars and their relation to functional programming<br />
appeared. Despite the growing popularity of functional programming,<br />
attribute grammars remain remarkably less renown.<br />
<br />
The purpose of this article is twofold. On the one hand it illustrates how<br />
functional programming sometimes scales poorly and how<br />
attribute grammars can remedy these problems. On the other hand it aims to<br />
provide a gentle introduction to attribute grammars for seasoned functional<br />
programmers.<br />
<br />
==The problem==<br />
John Hughes argues that with the increasing complexity of modern<br />
software systems, modularity has become of paramount importance to software<br />
development. Functional languages provide new kinds of ''glue'' that create<br />
new opportunities for more modular code. In particular, Hughes stresses<br />
the importance of higher-order functions and lazy evaluation. There are<br />
plenty of examples where this works nicely - yet situations arise where<br />
the glue that functional programming provides somehow isn't quite enough.<br />
<br />
Perhaps a small example is in order. Suppose we want to write a function<br />
<tt>diff :: [Float] -> [Float]</tt> that given a list <tt>xs</tt>, calculates a new list where every element <tt>x</tt> is replaced with the difference between <tt>x</tt> and the<br />
average of <tt>xs</tt>. Similar problems pop up in any library for performing<br />
statistical calculations.<br />
<br />
===Higher-order functions===<br />
Let's tackle the problem with some of Haskell's most powerful glue - higher-order functions. Any beginning Haskell programmer should be able to concoct the solution presented in Listing One. The average is computed using functions from the Prelude. The obvious function using this average is then mapped over the original list. So far, so good.<br />
<br />
<haskell><br />
--- Listing One ---<br />
<br />
diff :: [Float] -> [Float]<br />
diff xs = map (\x -> x - (avg xs)) xs<br />
<br />
avg :: [Float] -> Float<br />
avg xs = sum xs / genericLength xs<br />
</haskell><br />
<br />
There are, however, a few things swept under the rug in this example. First<br />
of all, this simple problem requires three traversals of the original<br />
list. Computing additional values from the original list will require even<br />
more traversals.<br />
<br />
Secondly, the solution is so concise because it depends on Prelude<br />
functions. If the values were stored in a slightly different data structure,<br />
the solution would require a lot of tedious work. We could, of course,<br />
define our own higher-order functions, such as <tt>map</tt> and <tt>fold</tt>, or even<br />
resort to generic programming. There are, however,<br />
more ways to skin this particular cat.<br />
<br />
This problem illustrates the sheer elegance of functional programming. We<br />
do pay a price for the succinctness of the solution. Multiple traversals<br />
and boilerplate code can both be quite a head-ache. If we want to perform<br />
complex computations over custom data structures, we may want to consider an<br />
alternative approach.<br />
<br />
Fortunately, as experienced functional programmers, we have another card up<br />
our sleeve.<br />
<br />
===Lazy evaluation===<br />
The second kind of glue that functional programming provides is lazy<br />
evaluation. In essence, lazy evaluation only evaluates expressions when<br />
they become absolutely necessary.<br />
<br />
In particular, lazy evaluation enables the definition of ''circular programs'' that bear a dangerous resemblance to undefined values. Circular<br />
programs tuple separate computations, relying on lazy evaluation to feed<br />
the results of one computation to another.<br />
<br />
In our example, we could simply compute the length and sum of the list at<br />
the same time:<br />
<br />
<haskell><br />
average :: [Float] -> Float<br />
average xs = let<br />
nil = (0.0, 0.0)<br />
cons x (s,l) = (x + s, 1.0 + l)<br />
(sum,length) = foldr cons nil xs<br />
in sum / length<br />
</haskell><br />
<br />
We can eliminate traversals by tupling computations! Can we compute the<br />
resulting list at the same time as computing the sum and length? Let's try:<br />
<br />
<haskell><br />
diff :: [Float] -> [Float]<br />
diff xs = let<br />
nil = (0.0, 0.0, [])<br />
cons x (s,l,rs) = (x+s, 1.0+l, (x - ....) : rs)<br />
(sum,length,res) = foldr cons nil xs<br />
in res<br />
</haskell><br />
<br />
We run into trouble when we try to use the average to construct the<br />
resulting list. The problem is, that we haven't computed the average, but<br />
somehow want to use it during the traversal. To solve this, we don't actually<br />
compute the resulting list, but rather compute a function taking the<br />
average to the resulting list:<br />
<br />
<haskell><br />
diff :: [Float] -> [Float]<br />
diff xs =<br />
let<br />
nil = (0.0, 0.0, \avg -> [])<br />
cons x (s,l,rs) = (x+s, 1.0+l, \avg -> (x - avg) : rs avg)<br />
(sum,length,res) = foldr cons nil xs<br />
in<br />
res (sum / length)<br />
</haskell><br />
<br />
We can generalize this idea a bit further. Suppose that we want to compute<br />
other values that use the average. We could just add an <tt>avg</tt> argument to<br />
every element of the tuple that needs the average. It is a bit nicer,<br />
however, to lift the <tt>avg</tt> argument outside the tuple. Our final listing<br />
now becomes:<br />
<br />
<haskell><br />
--- Listing Two ---<br />
<br />
diff :: [Float] -> [Float]<br />
diff xs =<br />
let<br />
nil avg = (0.0, 0.0, [])<br />
cons x fs avg =<br />
let<br />
(s,l,ds) = fs avg<br />
in<br />
(s+x,l+1.0,x-avg : ds)<br />
(sum,length,ds) = foldr cons nil xs (sum / length)<br />
in<br />
ds<br />
</haskell><br />
<br />
Now every element of the tuple can refer to the average, rather than just<br />
the final list.<br />
<br />
This ''credit card transformation'' eliminates multiple traversals by<br />
tupling computations. We use the average without worrying if we have<br />
actually managed to compute it. When we actually write the fold, however,<br />
we have to put our average where our mouth is. Fortunately, the <tt>sum</tt> and<br />
<tt>length</tt> don't depend on the average, so we are free to use these values to<br />
tie the recursive knot.<br />
<br />
The code in Listing Two only needs a single traversal and one<br />
higher-order function. It apparently solves the problems with the code in<br />
Listing One.<br />
<br />
Hold on a minute. What ever happened to the elegance of our previous<br />
solution? Our second solution appears to have sacrificed clarity for the<br />
sake of efficiency. Who in their right minds would want to write the code<br />
in Listing Two? I wouldn't. Maybe, just maybe, we can do a bit better.<br />
<br />
==Attribute Grammars==<br />
Before even explaining what an attribute grammar is, think back to when you<br />
first learned about ''folds''. Initially, a fold seems like a silly<br />
abstraction. Why should I bother writing simple functions as folds? After<br />
all, I already know how to write the straightforward solution. It's only<br />
after a great deal of experience with functional programming that you learn<br />
to recognize folds as actually being a worthwhile abstraction. Learning<br />
about attribute grammars is similar in more ways than one.<br />
<br />
So what are attribute grammars? I'll have a bit more to say about that<br />
later. For now, let's see what the attribute grammar solution to our<br />
running example looks like.<br />
<br />
===The attribute grammar solution===<br />
I'll introduce attribute grammars using the syntax of the [http://www.cs.uu.nl/wiki/bin/view/HUT/AttributeGrammarSystem Utrecht University Attribute Grammar] system or UUAG for short. The UUAG system takes a file<br />
containing an attribute grammar definition and generates a Haskell module<br />
containing ''semantic functions'', determined by the attribute grammar. The<br />
attribute grammar determines a computation over some data structure; the<br />
semantic functions correspond to the actual Haskell functions that perform<br />
the computation.<br />
<br />
Although the UUAG system's syntax closely resembles Haskell, it is<br />
important to realize that the UUAG system is a Haskell pre-processor and<br />
not a complete Haskell compiler.<br />
<br />
So what does an attribute grammar file look like? Well, first of all we<br />
have to declare the data structure we're working with. In our example, we<br />
simply have a list of Floats.<br />
<br />
<haskell><br />
--- Listing Three ---<br />
<br />
DATA Root<br />
| Root list : List<br />
DATA List<br />
| Nil<br />
| Cons head : Float tail : List<br />
</haskell><br />
<br />
Datatypes are declared with the keyword <tt>DATA</tt>, followed by a list of<br />
constructors. Every node explicitly gives the name and type of all its<br />
children. In our example we have an empty list <tt>Nil</tt> and a list constructor<br />
<tt>Cons</tt> with two children, <tt>head</tt> and <tt>tail</tt>. For reasons that will become<br />
apparent later on, we add an additional datatype corresponding to the root<br />
of our list.<br />
<br />
So now that we've declared our datatype, let's add some ''attributes''. If we<br />
want to compute the average element, we'll need the length of the<br />
list. Listing Four introduces our first attribute corresponding to<br />
a list's length.<br />
<br />
<haskell><br />
--- Listing Four ---<br />
<br />
ATTR List [|| length : Float]<br />
SEM List<br />
| Nil lhs.length = 0.0<br />
| Cons lhs.length = 1.0 + @tail.length<br />
</haskell><br />
<br />
Let's go over the code line by line.<br />
<br />
An attribute has to be declared before it can actually be defined. An<br />
attribute is declared using the <tt>ATTR</tt> statement. This example declares a<br />
single ''synthesized'' attribute called <tt>length</tt> of type <tt>Float</tt>. A<br />
synthesized attribute is typically a value you are trying to compute bottom<br />
up. Synthesized attributes are declared to the right of the second<br />
vertical bar. We'll see other kinds attributes shortly.<br />
<br />
Now that we've declared our first attribute, we can actually define it. A<br />
<tt>SEM</tt> statement begins by declaring for which data type attributes are<br />
being defined. In our example we want to define an attribute on a <tt>List</tt>,<br />
hence we write <tt>SEM List</tt>. We can subsequently give attribute definitions<br />
for the constructors of our <tt>List</tt> data type.<br />
<br />
Every attribute definition consists of several parts. We begin by<br />
mentioning the constructor for which we define an attribute. In our example<br />
we give two definitions, one for <tt>Nil</tt> and one for <tt>Cons</tt>.<br />
<br />
The second part of the attribute definition describes which attribute is<br />
being defined. In our example we define the attribute <tt>length</tt> for the<br />
''left-hand side'', or <tt>lhs</tt>. A lot of the terminology associated with<br />
attribute grammars comes from the world of context-free grammars. As this<br />
tutorial focuses on functional programmers, rather than formal language<br />
gurus, feel free to read <tt>lhs</tt> as "parent node". It seems a bit odd to<br />
write <tt>lhs.length</tt> explicitly, but we'll see later on why merely writing<br />
<tt>length</tt> doesn't suffice.<br />
<br />
Basically, all we've only said that the two definitions define the <tt>length</tt><br />
of <tt>Nil</tt> and <tt>Cons</tt>. We still have to fill in the necessary definition. The<br />
actual definition of the attributes takes place to the right of the equals<br />
sign. Programmers are free to write any valid Haskell expression. In fact,<br />
the UUAG system does not analyse the attribute definitions at all, but merely<br />
copies them straight into the resulting Haskell module. In our example, we<br />
want the length of the empty list to be <tt>0.0</tt>. The case for <tt>Cons</tt> is a bit<br />
trickier.<br />
<br />
In the <tt>Cons</tt> case we want to be increment the length computed so far. To<br />
do so we need to be able to refer to other attributes. In particular we<br />
want to refer to the <tt>length</tt> attribute of the tail. The expression<br />
<tt>@tail.length</tt> does just that. In general, you're free to refer to any<br />
synthesized attribute ''attr'' of a child node ''c'' by writing <tt>@c.attr</tt>.<br />
<br />
The <tt>length</tt> attribute can be depicted pictorally as follows:<br />
<br />
[[Image:WAGM-Avg.png|The length attribute]]<br />
<br />
'''Exercise:''' Declare and define a synthesized attribute <tt>sum</tt> that<br />
computes the sum of a <tt>List</tt>. You can refer to a value <tt>val</tt> stored at a<br />
node as <tt>@val</tt>. For instance, write <tt>@head</tt> to refer to the float stored in<br />
at a <tt>Cons</tt> node. Draw the corresponding picture if you're stuck.<br />
<br />
Now we've defined <tt>length</tt> and <tt>sum</tt>, let's compute the average. We'll know<br />
the sum and the length of the entire list at the Root node. Using those<br />
attributes we can compute the average and ''broadcast'' the average through<br />
the rest of the list. Let's start with the picture this time:<br />
<br />
[[Image:WAGM-Length.png|The average attribute]]<br />
<br />
The previous synthesized attributes, <tt>length</tt> and <tt>sum</tt>, defined bottom-up<br />
computations. We're now in the situation, however, where we want to pass<br />
information through the tree from a parent node to its child nodes using an<br />
''inherited'' attribute. Listing Five defines an inherited attribute <tt>avg</tt><br />
that corresponds to the picture we just drew.<br />
<br />
<haskell><br />
--- Listing Five ---<br />
ATTR List [ avg : Float|| ]<br />
SEM Root<br />
| Root list.avg = @list.sum / @list.length<br />
<br />
SEM List<br />
| Cons tail.avg = @lhs.avg<br />
<br />
</haskell><br />
<br />
Inherited attributes are declared to the left of the two vertical<br />
bars. Once we've declared an inherited attribute <tt>avg</tt> on lists, we're<br />
obliged to define how every constructor passes an <tt>avg</tt> to its children of<br />
type <tt>List</tt>.<br />
<br />
In our example, there are only two constructors with children<br />
of type <tt>List</tt>, namely <tt>Root</tt> and <tt>Cons</tt>. At the <tt>Root</tt> we compute the<br />
average, using the synthesized attributes <tt>sum</tt> and <tt>length</tt>, and pass the<br />
result to the <tt>list</tt> child. At the <tt>Cons</tt> node, we merely copy down the<br />
<tt>avg</tt> we received from our parent. Analogous to synthesized attributes, we<br />
can refer to an inherited attribute <tt>attr</tt> by writing <tt>@lhs.attr</tt>.<br />
<br />
Admittedly, this inherited attribute is not terribly interesting. There are<br />
plenty of other examples, however, where an inherited attribute represents<br />
important contextual information. Think of passing around the set of<br />
assumptions when writing a type checker, for instance.<br />
<br />
'''Exercise:''' To complete the attribute grammar, define an attribute<br />
<tt>res</tt> that computes the resulting list. Should it be inherited or<br />
synthesized? You may want to draw a picture.<br />
<br />
===Running the UUAG===<br />
Now suppose you've completed the exercises and copied the examples in a<br />
single file called <tt>Diff.ag</tt>. How do we actually use the attribute grammar?<br />
This is were the UUAG compiler steps in. Running the UUAG compiler on the<br />
source attribute grammar file generates a new <tt>Diff.hs</tt> file, which we can<br />
then compile like any other Haskell file.<br />
<br />
<haskell><br />
> uuagc -a Diff.ag<br />
> ghci Diff.hs<br />
</haskell><br />
<br />
The <tt>Diff.hs</tt> file contains several ingredients.<br />
<br />
Firstly, new Haskell datatypes are generated corresponding to <tt>DATA</tt><br />
declarations in the attribute grammar. For every generated datatype a<br />
corresponding <tt>fold</tt> is generated. The attribute definitions determine the<br />
arguments passed to the folds. Browsing through the generated code can<br />
actually be quite instructive.<br />
<br />
Inherited attributes are passed to recursive calls of the fold. Synthesized<br />
attributes are tupled and returned as the result of the computation. In<br />
essence, we've reproduced our original solution in Listing Two - but now<br />
without the hassle associated with spelling out [[catamorphism]]s with a higher<br />
order domain and a compound codomain.<br />
<br />
The attribute grammar solution is just as efficient as our earlier solution<br />
relying on lazy evaluation, yet the code is hardly different from what we<br />
would write in a straightforward Haskell solution. It really is the best of<br />
both worlds. The two types of glue that John Hughes pinpoints in his<br />
original article just aren't enough. I would like to think that sometimes<br />
attribute grammars are sometimes capable of providing just the right<br />
missing bit of glue.<br />
<br />
===What are attribute grammars?===<br />
So just what are attribute grammars? Well, that depends on who you ask,<br />
really. I've tried to sum up some different views below.<br />
<br />
Attribute grammars add semantics to a context free grammar. Although it is<br />
easy enough to describe a language's syntax using a context free grammar,<br />
accurately describing a language's semantics is notoriously<br />
difficult. Attribute grammars specify a language's semantics by<br />
'decorating' a context free grammar with those attributes you are interested<br />
in.<br />
<br />
Attribute grammars describe tree traversals. All imperative implementations<br />
of attribute grammar systems perform tree traversals to compute some<br />
value. Basically an attribute grammar declares ''which'' values to compute<br />
and an attribute grammar system executes these computations. Once you've<br />
made this observation, the close relation to functional programming should<br />
not come as a surprise.<br />
<br />
Attribute grammars are a formalism for writing catamorphisms in a<br />
compositional fashion. Basically, the only thing the UUAG compiler does is<br />
generate large folds that I couldn't be bothered writing myself. It takes<br />
away all the elbow grease involved with maintaining and extending such<br />
code. In a sense the compiler does absolutely nothing new; it just makes<br />
life a lot easier.<br />
<br />
Attribute grammars provide framework for aspect oriented programming in<br />
functional languages. Lately there has been a lot of buzz about the<br />
importance of ''aspects'' and ''aspect oriented programming''. Attribute<br />
grammars provide a clear and well-established framework for splitting code<br />
into separate aspects. By spreading attribute definitions over several<br />
different files and grouping them according to aspect, attribute grammars<br />
provide a natural setting for aspect oriented programming.<br />
<br />
How do attribute grammars relate to other Haskell abstractions? I'll try to<br />
put my finger on some of the more obvious connections, but I'm pretty sure<br />
there's a great deal more that I don't cover here.<br />
<br />
==What else is out there?==<br />
Everyone loves monads. They're what makes IO possible in Haskell. There are<br />
extensive standard libraries and syntactic sugar specifically designed to<br />
make life with monads easier. There are an enormous number of Haskell<br />
libraries based on the monadic interface. They represent one of the most<br />
substantial developments of functional programming in the last decade.<br />
<br />
Yet somehow, the single most common question asked by fledgling Haskell<br />
programmers is probably ''What are monads?''. Beginners have a hard time<br />
grasping the concept of monads and yet connoisseurs recognize a monad in<br />
just about every code snippet. I think the more important question is:<br />
''What are monads good for?''<br />
<br />
Monads provide a simple yet powerful abstract notion of computation. In<br />
essence, a monad describes how sequence computations. This is crucial in<br />
order to perform IO in a functional language; by constraining all IO<br />
actions to a single interface of sequenced computations, the programmer is<br />
prevented from creating utter chaos. The real power of monads is in the<br />
interface they provide.<br />
<br />
John Hughes identified modularity as the single biggest blessing of<br />
functional programming. The obvious question is: how modular is the monadic<br />
interface? This really depends on you're definition of modularity. Let me<br />
be more specific. How can you combine two arbitrary monads? You can't. This<br />
is my greatest concern with monads. Once you choose your specific notion of<br />
computation, you have to stick to it through thick and thin.<br />
<br />
What about monad transformers? Monad transformers allow you to add a<br />
specific monad's functionality on top of any existing monad. What seems<br />
like a solution, more often than not, turns out to introduce more problems<br />
than you bargained for. Adding new functionality to a monad involves<br />
lifting all the computations from the previous monad to the new<br />
one. Although I could learn to live with this, it gets even worse. As every<br />
monad transformer really changes the underlying monad. The order in<br />
which monad transformers are applied really makes a difference. If I want<br />
to add error reporting and state to some existing monad, should I be forced<br />
to consider the order in which I add them?<br />
<br />
Monads are extremely worthwhile for the interface they provide. Monadic<br />
libraries are great, but changing and extending monadic code can be a<br />
pain. Can we do better? Well I probably wouldn't have started this monadic<br />
intermezzo if I didn't have some sort of answer.<br />
<br />
Let's start off with <tt>Reader</tt> monads, for instance. Essentially, <tt>Reader</tt><br />
monads adds an argument to some computation. Wait a minute, this reminds me<br />
of inherited attributes. What about <tt>Writer</tt> monads? They correspond to<br />
synthesized attributes of course. Finally, <tt>State</tt> monads correspond to<br />
''chained'' attributes, or attributes that are both synthesized and<br />
inherited. The real edge attribute grammars hold over monad transformers is that you<br />
can define new attributes ''without'' worrying about the order in which you<br />
define them or adapting existing code.<br />
<br />
Do other abstractions capture other notions related to attribute grammars?<br />
Of course they do! Just look at the function space arrows instance. The notion of<br />
combining two distinct computations using the <tt>(&&&)</tt> operator relates to<br />
the concept of ''joining'' two attribute grammars by collecting their<br />
attribute definitions. When you look at the <tt>loop</tt> combinator, I can<br />
only be grateful that an attribute grammar system deals with attribute<br />
dependencies automatically.<br />
<br />
There really is a lot of related work. Implicit parameters? Inherited attributes! Linear implicit parameters? Chained attributes! Concepts that are so natural in the setting of attribute grammars, yet seem contrived when added to Haskell. This strengthens my belief that functional programmers can really benefit from even the most fleeting experience with attribute grammars; although I'd like to think that if you've read this far, you're hungry for more.<br />
<br />
==Further reading==<br />
This more or less covers the tutorial section of this article. The best way<br />
to learn more about attribute grammars is by actually using them. To conclude the tutorial, I've<br />
included a small example for you to play with. I've written a parser for a very<br />
simple wiki formatting language not entirely unlike the one used to produce this<br />
document. So far the HTML generated after parsing a document is fairly poor. It's up to<br />
you to improve it!<br />
<br />
You can download the initial version here. Don't forget to install the [http://www.cs.uu.nl/wiki/bin/view/HUT/Download]. It might<br />
be worthwhile to have a look at the [http://www.cs.uu.nl/wiki/bin/view/HUT/AttributeGrammarManual UUAG manual] as there's a lot of technical detail that I haven't mentioned.<br />
<br />
If you're particularly daring, you may want to take a look at the [http://www.cs.uu.nl/wiki/Ehc/WebHome Essential Haskell Compiler] being developed at Utrecht. It's almost completely written using the UUAG and is designed to be suitable for education and experimentation. The compiler was presented at the Summer School for Advanced Functional Programming in Tartu, Estonia last summer. As a result, there's a lot written about it already.<br />
<br />
Dive on in!<br />
<br />
[[Category:Article]]</div>Tkempshttps://wiki.haskell.org/index.php?title=Attribute_grammar&diff=62293Attribute grammar2018-01-26T06:35:07Z<p>Tkemps: /* Portals or other rich resources */</p>
<hr />
<div>__TOC__<br />
<br />
== Introduction ==<br />
<br />
Especially for functional programmers (with Haskell examples) -- see<br />
Wouter Swierstra: [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4/Why_Attribute_Grammars_Matter Why Attribute Grammars Matter] (published in [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4 The Monad.Reader, Issue Four]).<br />
And also very deep connections with<br />
* catamorphisms, category theory<br />
* attribute grammars<br />
are described there.<br />
And there are links from that article to other materials providing deep insights in these powerful theories: attribute grammar tools and articles of the Essential Haskell Compiler project.<br />
<br />
It is no wonder that it is exactly a compiler project homepage that provides good circular programming and attribute grammar tutorials<br />
* both on its [http://www.cs.uu.nl/wiki/Ehc/WebHome project page]<br />
* and on a separate [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem attribute grammar] page:<br />
the reason may be that a compiler project is complex enough to require good [[separation of concerns]], modularity, reuse (goals of Aspect Oriented Programming, too), and attribute grammars are powerful tools to achieve these goals.<br />
<br />
* [http://en.wikipedia.org/wiki/Attribute_grammar Wikipedia article]<br />
* [http://www-rocq.inria.fr/oscar/www/fnc2/manual/node32.html A Tutorial Introduction to Attribute Grammars], contained in [http://www-rocq.inria.fr/oscar/www/fnc2/manual/manual.html The FNC-2 System User's Guide and Reference Manual], written by Martin JOURDAN and Didier PARIGOT<br />
* Also [http://www.haskell.org/happy/ Happy] contains AG-related capabilities, see [http://www.haskell.org/happy/doc/html/sec-AttributeGrammar.html Chapter 4. Attribute Grammars]<br />
<br />
== Separation of concerns ==<br />
<br />
Both Swiestra's article (mentioned above) and the Essential Haskell Compiler Project tool (mentioned below) remarks the connection of attribute grammars to the topic of [[separation of concerns]], a goal of aspect orinted programming. This goal can be achieved by multiple ways in functional programming, see the concepts [[monad]] and [[arrow]]). Swiestra'a article mentions analogies between<br />
* attribute grammars,<br />
* (Reader, Writer, State) [[monad]]s,<br />
* (fan-out operation of) [[arrow]]s.<br />
<br />
== Portals or other rich resources ==<br />
<br />
[http://foswiki.cs.uu.nl/foswiki/HUT/AttributeGrammarSystem Attribute Grammar page of Utrecht Univesity]<br />
<br />
[https://www.haskell.org/happy/doc/html/sec-AtrributeGrammarsInHappy.html Description of Attibute Grammar aspects in the Happy parser generator system for Haskell]<br />
<br />
[http://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/index.html The FNC-2 Attribute Grammar System]<br />
<br />
[http://www-sop.inria.fr/members/Didier.Parigot/www/fnc2/AGtexte.html Resources for Attribute Grammar research by Didier Parigot (INRIA)]<br />
<br />
Team OSCAR at INRIA is stopped since 1999 but the following page contains some older publications:<br />
<br />
[https://www.inria.fr/en/teams/oscar Former Team OSCAR Home Page]<br />
<br />
== Tools ==<br />
<br />
Utrecht University's [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem Attribute Grammar System] tools include also an attribute grammar compiler, UUAGC. The concept of attribute grammar was used in their [http://www.cs.uu.nl/wiki/Ehc/WebHome Essential Haskell Compiler] project, which gives us not only a working programming language, but also a good didactical material about using attribute grammars, e.g. in writing compilers.<br />
<br />
Albeits these materials are self-contained, they reveal that the theory of attribute grammars is related to other concepts<br />
([[circular programming]], catamorphism).<br />
<br />
Robert Dockins has reported [http://haskell.org/communities/06-2006/html/report.html#happy-ag Attribute Grammar Support for Happy] in the Haskell Communities and Activities Report (10th edititon, June 2006)<br />
<br />
== Related concepts ==<br />
<br />
* [[Circular programming]]<br />
* Catamorphism, see [[Category theory]]<br />
<br />
[[Category:Theoretical foundations]]<br />
[[Category:Compiler tools]]</div>Tkempshttps://wiki.haskell.org/index.php?title=Attribute_grammar&diff=62292Attribute grammar2018-01-26T06:26:37Z<p>Tkemps: /* Portals or other rich resources */</p>
<hr />
<div>__TOC__<br />
<br />
== Introduction ==<br />
<br />
Especially for functional programmers (with Haskell examples) -- see<br />
Wouter Swierstra: [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4/Why_Attribute_Grammars_Matter Why Attribute Grammars Matter] (published in [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4 The Monad.Reader, Issue Four]).<br />
And also very deep connections with<br />
* catamorphisms, category theory<br />
* attribute grammars<br />
are described there.<br />
And there are links from that article to other materials providing deep insights in these powerful theories: attribute grammar tools and articles of the Essential Haskell Compiler project.<br />
<br />
It is no wonder that it is exactly a compiler project homepage that provides good circular programming and attribute grammar tutorials<br />
* both on its [http://www.cs.uu.nl/wiki/Ehc/WebHome project page]<br />
* and on a separate [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem attribute grammar] page:<br />
the reason may be that a compiler project is complex enough to require good [[separation of concerns]], modularity, reuse (goals of Aspect Oriented Programming, too), and attribute grammars are powerful tools to achieve these goals.<br />
<br />
* [http://en.wikipedia.org/wiki/Attribute_grammar Wikipedia article]<br />
* [http://www-rocq.inria.fr/oscar/www/fnc2/manual/node32.html A Tutorial Introduction to Attribute Grammars], contained in [http://www-rocq.inria.fr/oscar/www/fnc2/manual/manual.html The FNC-2 System User's Guide and Reference Manual], written by Martin JOURDAN and Didier PARIGOT<br />
* Also [http://www.haskell.org/happy/ Happy] contains AG-related capabilities, see [http://www.haskell.org/happy/doc/html/sec-AttributeGrammar.html Chapter 4. Attribute Grammars]<br />
<br />
== Separation of concerns ==<br />
<br />
Both Swiestra's article (mentioned above) and the Essential Haskell Compiler Project tool (mentioned below) remarks the connection of attribute grammars to the topic of [[separation of concerns]], a goal of aspect orinted programming. This goal can be achieved by multiple ways in functional programming, see the concepts [[monad]] and [[arrow]]). Swiestra'a article mentions analogies between<br />
* attribute grammars,<br />
* (Reader, Writer, State) [[monad]]s,<br />
* (fan-out operation of) [[arrow]]s.<br />
<br />
== Portals or other rich resources ==<br />
<br />
Team OSCAR at INRIA is stopped since 1999 but the following page contains some older publications:<br />
<br />
[https://www.inria.fr/en/teams/oscar Former Team OSCAR Home Page]<br />
<br />
== Tools ==<br />
<br />
Utrecht University's [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem Attribute Grammar System] tools include also an attribute grammar compiler, UUAGC. The concept of attribute grammar was used in their [http://www.cs.uu.nl/wiki/Ehc/WebHome Essential Haskell Compiler] project, which gives us not only a working programming language, but also a good didactical material about using attribute grammars, e.g. in writing compilers.<br />
<br />
Albeits these materials are self-contained, they reveal that the theory of attribute grammars is related to other concepts<br />
([[circular programming]], catamorphism).<br />
<br />
Robert Dockins has reported [http://haskell.org/communities/06-2006/html/report.html#happy-ag Attribute Grammar Support for Happy] in the Haskell Communities and Activities Report (10th edititon, June 2006)<br />
<br />
== Related concepts ==<br />
<br />
* [[Circular programming]]<br />
* Catamorphism, see [[Category theory]]<br />
<br />
[[Category:Theoretical foundations]]<br />
[[Category:Compiler tools]]</div>Tkempshttps://wiki.haskell.org/index.php?title=Attribute_grammar&diff=62291Attribute grammar2018-01-26T06:26:19Z<p>Tkemps: /* Portals or other rich resources */</p>
<hr />
<div>__TOC__<br />
<br />
== Introduction ==<br />
<br />
Especially for functional programmers (with Haskell examples) -- see<br />
Wouter Swierstra: [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4/Why_Attribute_Grammars_Matter Why Attribute Grammars Matter] (published in [http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue4 The Monad.Reader, Issue Four]).<br />
And also very deep connections with<br />
* catamorphisms, category theory<br />
* attribute grammars<br />
are described there.<br />
And there are links from that article to other materials providing deep insights in these powerful theories: attribute grammar tools and articles of the Essential Haskell Compiler project.<br />
<br />
It is no wonder that it is exactly a compiler project homepage that provides good circular programming and attribute grammar tutorials<br />
* both on its [http://www.cs.uu.nl/wiki/Ehc/WebHome project page]<br />
* and on a separate [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem attribute grammar] page:<br />
the reason may be that a compiler project is complex enough to require good [[separation of concerns]], modularity, reuse (goals of Aspect Oriented Programming, too), and attribute grammars are powerful tools to achieve these goals.<br />
<br />
* [http://en.wikipedia.org/wiki/Attribute_grammar Wikipedia article]<br />
* [http://www-rocq.inria.fr/oscar/www/fnc2/manual/node32.html A Tutorial Introduction to Attribute Grammars], contained in [http://www-rocq.inria.fr/oscar/www/fnc2/manual/manual.html The FNC-2 System User's Guide and Reference Manual], written by Martin JOURDAN and Didier PARIGOT<br />
* Also [http://www.haskell.org/happy/ Happy] contains AG-related capabilities, see [http://www.haskell.org/happy/doc/html/sec-AttributeGrammar.html Chapter 4. Attribute Grammars]<br />
<br />
== Separation of concerns ==<br />
<br />
Both Swiestra's article (mentioned above) and the Essential Haskell Compiler Project tool (mentioned below) remarks the connection of attribute grammars to the topic of [[separation of concerns]], a goal of aspect orinted programming. This goal can be achieved by multiple ways in functional programming, see the concepts [[monad]] and [[arrow]]). Swiestra'a article mentions analogies between<br />
* attribute grammars,<br />
* (Reader, Writer, State) [[monad]]s,<br />
* (fan-out operation of) [[arrow]]s.<br />
<br />
== Portals or other rich resources ==<br />
<br />
Team OSCAR at INRIA is stopped since 1999 but contains some older publications:<br />
<br />
[https://www.inria.fr/en/teams/oscar Former Team OSCAR Home Page]<br />
<br />
== Tools ==<br />
<br />
Utrecht University's [http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem Attribute Grammar System] tools include also an attribute grammar compiler, UUAGC. The concept of attribute grammar was used in their [http://www.cs.uu.nl/wiki/Ehc/WebHome Essential Haskell Compiler] project, which gives us not only a working programming language, but also a good didactical material about using attribute grammars, e.g. in writing compilers.<br />
<br />
Albeits these materials are self-contained, they reveal that the theory of attribute grammars is related to other concepts<br />
([[circular programming]], catamorphism).<br />
<br />
Robert Dockins has reported [http://haskell.org/communities/06-2006/html/report.html#happy-ag Attribute Grammar Support for Happy] in the Haskell Communities and Activities Report (10th edititon, June 2006)<br />
<br />
== Related concepts ==<br />
<br />
* [[Circular programming]]<br />
* Catamorphism, see [[Category theory]]<br />
<br />
[[Category:Theoretical foundations]]<br />
[[Category:Compiler tools]]</div>Tkempshttps://wiki.haskell.org/index.php?title=Learning_Haskell&diff=40209Learning Haskell2011-05-30T19:21:32Z<p>Tkemps: </p>
<hr />
<div>__NOTOC__<br />
[[Category:Tutorials]]<br />
<br />
This portal points to places where you can go if you want to learn Haskell. <br />
<br />
The [[Introduction|Introduction to Haskell]] on the Haskell website tells you what Haskell gives you: substantially increased programmer productivity, shorter, clearer, and more maintainable code, fewer errors, higher reliability, a smaller semantic gap between the programmer and the language, shorter lead times. There is an old but still relevant paper about [http://weblog.raganwald.com/2007/03/why-why-functional-programming-matters.html Why Functional Programming Matters] (PDF) by John Hughes. More recently, Sebastian Sylvan wrote an article about [[Why Haskell Matters]].<br />
<br />
There is also a [http://www.haskell.org/haskellwiki/Comparison table comparing Haskell to other functional languages]. Many questions about functional programming are answered by the [http://www.cs.nott.ac.uk/~gmh//faq.html comp.lang.functional FAQ].<br />
<br />
==Implementations==<br />
<br />
Here is an overview about Haskell implementations (for new, or non-expert users, we recommend starting with the [http://haskell.org/platform Haskell Platform]).<br />
<br />
{| border=1 cellspacing=0 cellpadding=5 bgcolor=#FFFFFF width=100%|<br />
|-<br />
| <br />
! Messages<br />
! Size<br />
! Tools<br />
! Remarks <br />
|-<br />
| [http://www.haskell.org/ghc/ GHC]<br />
| +<br />
| -<br />
| ++<br />
| Many language extensions; generated code is very fast. ''The most popular implementation.''<br />
|-<br />
| [http://www.haskell.org/hugs/ Hugs]<br />
| +/-<br />
| ++<br />
| -<br />
| Fast compilation; used a lot for learning Haskell and rapid code development. See also [[WinHugs]].<br />
|-<br />
| [http://www.cs.york.ac.uk/fp/nhc98/ nhc98]<br />
| +<br />
| +<br />
| ++<br />
| Profiling, debugging, tracing. ''Not actively developed.''<br />
|-<br />
| [[Yhc]]<br />
| +<br />
| +<br />
| ?<br />
| Compiles to bytecodes. Runtime easily portable. ''Not actively developed.''<br />
|-<br />
| [http://www.cs.uu.nl/helium/ Helium]<br />
| ++<br />
| ++<br />
| -<br />
| No type classes (yet!) and thus incompatible with most material on this site. Made for teaching/learning. Excellent error messages.<br />
|-<br />
| [http://www.cs.uu.nl/wiki/UHC UHC]<br />
| +/-<br />
| -<br />
| +/-<br />
| Developed for experimentation with language features. As a Haskell compiler still under development.<br />
|}<br />
<br />
Detailed information on the implementations can be found in a [[Implementations|separate article]].<br />
<br />
== Material ==<br />
<br />
Below there are links to certain introductory material. If you want to dig deeper, see [[Books and tutorials]].<br />
<br />
=== Textbooks ===<br />
<br />
* [http://www.haskell.org/soe The Haskell School of Expression]<br />
* [http://www.cs.ukc.ac.uk/people/staff/sjt/craft2e/ Haskell: the Craft of Functional Programming]<br />
* [http://www.prenhall.com/allbooks/ptr_0134843460.html Introduction to Functional Programming using Haskell]<br />
* [http://books.cambridge.org/0521277248.htm An Introduction to Functional Programming Systems Using Haskell]<br />
* [http://www.iro.umontreal.ca/~lapalme/Algorithms-functional.html Algorithms: A functional programming approach]<br />
* [http://homepages.cwi.nl/~jve/HR/ The Haskell Road to Logic, Maths, and Programming]<br />
* [http://www.cs.nott.ac.uk/~gmh/book.html Programming in Haskell]<br />
* [http://book.realworldhaskell.org/] Real World Haskell<br />
<br />
=== Online tutorials ===<br />
<br />
* [[Meta-tutorial]]<br />
* [http://en.wikibooks.org/wiki/Haskell Haskell Wikibook] A long tutorial on Haskell that includes "Yet Another Haskell Tutorial", "Write Yourself a Scheme in 48 Hours" and "All about monads".<br />
* [http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf YAHT - Yet Another Haskell Tutorial] (good tutorial available online, also [http://pub.hal3.name/daume02yaht.pdf here])<br />
* [http://www.cs.ou.edu/~rlpage/fpclassCurrent/textbook/haskell.shtml Two dozen short lessons]<br />
* [http://www.haskell.org/tutorial/ A Gentle Introduction to Haskell] - classic text, but not so gentle really :D<br />
* [ftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf Haskell-Tutorial]<br />
* [http://www.informatik.uni-bonn.de/~ralf/teaching/Hskurs_toc.html Online Haskell Course] (German)<br />
* [http://www.haskell.org/~pairwise/intro/intro.html Haskell for C Programmers]<br />
* [http://learnyouahaskell.com/ Learn You a Haskell for Great Good!] Beautiful, illustrated Haskell tutorial for programmers with less of a functional programming background.<br />
<br />
=== Advanced tutorials ===<br />
<br />
* [[Hitchhikers guide to Haskell]]<br />
* [http://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours Write Yourself a Scheme in 48 Hours]<br />
* [http://research.microsoft.com/Users/simonpj/papers/marktoberdorf/ Tackling the Awkward Squad] (on I/O, interfacing to C, concurrency and exceptions)<br />
<br />
=== Debugging/profiling/optimization ===<br />
<br />
=== Monads ===<br />
<br />
* [http://sigfpe.blogspot.com/2006/08/you-could-have-invented-monads-and.html You Could Have Invented Monads! (And Maybe You Already Have.)]<br />
* [http://citeseer.ist.psu.edu/wadler95monads.html Monads for Functional Programming]<br />
* [http://www.haskell.org/all_about_monads/html/ All about monads]<br />
* [[IO inside|IO inside: down the Rabbit Hole]]<br />
* [http://spbhug.folding-maps.org/wiki/MonadsEn Monads]<br />
<br />
=== Type classes ===<br />
<br />
* [http://homepages.inf.ed.ac.uk/wadler/papers/class/class.ps.gz The paper that at first time introduced type classes and their implementation using dictionaries]<br />
* [[Research papers/Type systems#Type classes|More papers on the type classes]]<br />
<br />
=== Generic programming ===<br />
<br />
* [Dead link; [http://replay.web.archive.org/20090207090211/http://www.cs.vu.nl/boilerplate/ IA backup]] [http://www.cs.vu.nl/boilerplate/#papers Scrap your boilerplate]<br />
<br />
=== Popular libraries ===<br />
<br />
* ByteStrings?<br />
* [http://www.cs.uu.nl/people/daan/download/parsec/parsec.html Parsec, a fast combinator parser]<br />
* [[Modern array libraries]]<br />
* [http://www.haskell.org/haskellwiki/Gtk2Hs/Tutorials Gtk2Hs, the GUI library]<br />
<br />
=== Reference ===<br />
<br />
* The official language definition: [[Language and library specification]]<br />
* [[HaskellNewbie|Haskell Newbie]]<br />
* [http://cs.anu.edu.au/student/comp1100/haskell/tourofprelude.html Tour of the Haskell Prelude]<br />
* [http://zvon.org/other/haskell/Outputglobal/index.html Haskell Reference]<br />
* Haskell [[Reference card]]<br />
* [http://members.chello.nl/hjgtuyl/tourdemonad.html A tour of the Haskell Monad functions]<br />
* [http://www.cs.uu.nl/helium/docs/TourOfPrelude.html Tour of the Helium Prelude]<br />
* [http://www.cs.ukc.ac.uk/people/staff/sjt/craft2e/errors/allErrors.html Some common Hugs error messages]<br />
* [http://blog.codeslower.com/2008/10/The-Haskell-Cheatsheet The Haskell Cheatsheet] - A reference card and mini-tutorial in one.<br />
<br />
=== Course material ===<br />
* [http://www.cse.chalmers.se/edu/course/TDA555/ Introduction to Functional Programming, Chalmers]<br />
* [http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/external.html Programming in Haskell, Chalmers]<br />
* [http://www.cse.chalmers.se/edu/course/afp/ Advanced Functional Programming, Chalmers]<br />
* [Dead link] [http://www.cs.chalmers.se/~koen/Aafp/ Advanced Advanced Functional Programming, Chalmers]<br />
* [http://www.cs.caltech.edu/courses/cs11/material/haskell/index.html CS 11 Caltech]<br />
* [http://www.cs.uu.nl/docs/vakken/lfp/ Functional programming]: course notes ([http://www.cs.uu.nl/~jeroen/courses/fp-eng.pdf English], [http://www.cs.uu.nl/~jeroen/courses/fp-nl.pdf Dutch], [http://www.cs.uu.nl/~jeroen/courses/fp-sp.pdf Spanish]), slides in Dutch<br />
* [http://www.cse.unsw.edu.au/~cs1011/05s2/ CS1011]: Tutorials, lab exercises and solutions<br />
* [Hungarian] [http://pnyf.inf.elte.hu/fp/Index.xml Functional Programming]: online Haskell exercises (maybe Explorer can't show xml content)</div>Tkemps