https://wiki.haskell.org/api.php?action=feedcontributions&user=Dfeuer&feedformat=atomHaskellWiki - User contributions [en]2024-03-28T07:16:25ZUser contributionsMediaWiki 1.35.5https://wiki.haskell.org/index.php?title=Taking_over_a_package&diff=65601Taking over a package2023-04-27T21:49:18Z<p>Dfeuer: Degender language</p>
<hr />
<div>There are situations where a certain package on Hackage is effectively unmaintained, or is simply in need of a minor update but the maintainer of the package is unavailable.<br />
<br />
= If you '''can''' contact the author/maintainer =<br />
<br />
The author/maintainer may simply be too busy to do the updates, but may be happy to delegate or to pass on ownership entirely.<br />
<br />
Explain what you want to do, for example if you want to take over entirely, share ongoing maintenance responsibility, or do a one-off update without any ongoing responsibility.<br />
<br />
Ask them to give you permission to upload new versions of the package. What needs to happen technically is for your hackage username to be added to the maintainer group for the package in question. They can do this in one of two ways:<br />
<br />
* The current maintainer can do it directly on the package maintainer group page on Hackage: <code>https://hackage.haskell.org/package/<package>/maintainers/</code><br/> You will need to tell them your hackage username. They simply follow the "edit" link and add your username to the group.<br />
<br />
''OR''<br />
<br />
* The current maintainer can email the hackage administrators ([mailto:hackage-admin@haskell.org hackage-admin@haskell.org]), saying s/he wants you to take over the package, or at least wants you to be allowed to upload new versions.<br/>This is not the the hackage administrators' preferred option for obvious reasons, but may be your best bet if the current maintainer is exceedingly busy.<br />
<br />
= If you '''cannot''' contact the author/maintainer =<br />
<br />
This situation is more tricky because it involves overriding the normal rights of authors/maintainers.<br />
<br />
The hackage administrators have a procedure for this situation which you can follow:<br />
<br />
# Try to contact the maintainer. Give them reasonable time to respond.<br />
# State your intention to take over the package in a public forum (we recommend the haskell-cafe and/or libraries list). CC the maintainer.<br />
# Wait a while.<br />
# Send an email to the hackage administrators ([mailto:hackage-admin@haskell.org hackage-admin@haskell.org]), with a link to the public email thread.<br />
# The admins will grant you maintenance rights or upload a patched version for you.<br />
<br />
== Timetable ==<br />
# From time of announcement of intention to takeover to actual takeover, there will be a 2-6 week period where the admins give the maintainer a chance to respond, with the exact time in any case up to the discretion of the admins.<br />
# If someone has their package taken over because they did not respond, then, for the following year, should they request it, the admins will unconditionally return it to them.<br />
<br />
[[Category:FAQ]]<br />
[[Category:Packages]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Editors&diff=62794Editors2019-02-27T00:36:17Z<p>Dfeuer: nedit.org seems dead; use SourceForge link instead</p>
<hr />
<div>[[Category:Tools]]<br />
<br />
''For detailed information on Haskell IDEs, see [[IDEs]].''<br />
<br />
This page lists Haskell-aware editors with support for Haskell syntax highlighting and formatting support in text editors, grouped by platform.<br />
<br />
Multi-platform<br />
* [https://atom.io/ Atom]<br />
* [https://www.gnu.org/software/emacs/ Emacs]<br />
* [https://www.geany.org/ Geany]<br />
** [https://haskell.github.io/haskell-mode/manual/latest/ Haskell Mode]<br />
** Integration with [[Hugs]]<br />
* [http://www.jedsoft.org/jed/ Jed]<br />
* [http://www.activestate.com/komodo-edit Komodo Edit]<br />
* [https://sourceforge.net/projects/nedit/ Nedit]<br />
* [http://www.sublimetext.com/ Sublime Text] (proprietary)<br />
* [https://www.vim.org/ Vim]<br />
** [https://www.vim.org/scripts/script.php?script_id=2356 Superior Haskell Interaction Mode]<br />
* [https://code.visualstudio.com/ Visual Studio Code] (with Haskell extension)<br />
** [https://www.reddit.com/r/haskell/comments/5yc7e0/have_you_tried_visual_studio_code_for_coding/ Have you tried Visual Studio Code for coding Haskell?] (Reddit)<br />
** [https://marketplace.visualstudio.com/search?term=Haskell&target=VSCode&category=Programming%20Languages&sortBy=Relevance Haskell extensions]<br />
** [https://medium.com/@dogwith1eye/setting-up-haskell-in-vs-code-on-macos-d2cc1ce9f60a Setting up Haskell in VS Code on macOS] (blog)<br />
* [[Yi]] (work in progress; no binaries yet)<br />
* [https://kate-editor.org/ Kate] (KDE-based, originally for *nix, preview versions for win & mac)<br />
Linux<br />
* [http://www.scintilla.org/SciTE.html SciTE] (Also see these [[Tips for using SciTE with Haskell|tips]])<br />
Windows<br />
* [http://www.textpad.com/ Textpad]<br />
* [http://notepad-plus.sourceforge.net Notepad++]<br />
: See also the [[Windows#Editors | Windows page]]<br />
MacOS X<br />
* [http://www.codingmonkeys.de/subethaedit/ SubEthaEdit]<br />
* [http://macromates.com/ TextMate] has a [http://macromates.com/textmate/manual/bundles#installing_a_bundle Haskell bundle], now kept on [https://github.com/textmate/haskell.tmbundle github].<br />
* [http://www.barebones.com/products/bbedit/ BBEdit] and [http://www.barebones.com/products/textwrangler/ TextWrangler] have a [http://code.google.com/p/bbedit-haskell/ codeless language module for Haskell]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/70&diff=6213899 questions/Solutions/702017-09-24T17:45:35Z<p>Dfeuer: </p>
<hr />
<div>(**) Tree construction from a node string.<br />
<br />
We suppose that the nodes of a multiway tree contain single characters. In the depth-first order sequence of its nodes, a special character ^ has been inserted whenever, during the tree traversal, the move is a backtrack to the previous level.<br />
<br />
By this rule, the tree below (<tt>tree5</tt>) is represented as: <tt>afg^^c^bd^e^^^</tt><br />
<br />
https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/p70.gif<br />
<br />
Define the syntax of the string and write a predicate tree(String,Tree) to construct the Tree when the String is given.<br />
Make your predicate work in both directions.<br />
<br />
We could write separate printing and parsing functions, but the problem statement asks for a bidirectional function.<br />
<br />
First we need a parser monad, with some primitives:<br />
<br />
<haskell><br />
newtype P a = P { runP :: String -> Maybe (a, String) }<br />
<br />
instance Monad P where<br />
return x = P $ \ s -> Just (x, s)<br />
P v >>= f = P $ \ s -> do<br />
(x, s') <- v s<br />
runP (f x) s'<br />
<br />
instance MonadPlus P where<br />
mzero = P $ \ _ -> Nothing<br />
P u `mplus` P v = P $ \ s -> u s `mplus` v s<br />
<br />
charP :: P Char<br />
charP = P view_list<br />
where view_list [] = Nothing<br />
view_list (c:cs) = Just (c, cs)<br />
<br />
literalP :: Char -> P ()<br />
literalP c = do { c' <- charP; guard (c == c') }<br />
<br />
spaceP :: P ()<br />
spaceP = P (\ s -> Just ((), dropWhile isSpace s))<br />
</haskell><br />
<br />
Next a <tt>Syntax</tt> type, combining printing and parsing functions:<br />
<br />
<haskell><br />
data Syntax a = Syntax {<br />
display :: a -> String,<br />
parse :: P a<br />
}<br />
</haskell><br />
<br />
(We don't use a class, because we want multiple syntaxes for a given type.)<br />
Some combinators for building syntaxes:<br />
<br />
<haskell><br />
-- concatenation<br />
(<*>) :: Syntax a -> Syntax b -> Syntax (a,b)<br />
a <*> b = Syntax {<br />
display = \ (va,vb) -> display a va ++ display b vb,<br />
parse = liftM2 (,) (parse a) (parse b)<br />
}<br />
<br />
-- alternatives<br />
(<|>) :: Syntax a -> Syntax b -> Syntax (Either a b)<br />
a <|> b = Syntax {<br />
display = either (display a) (display b),<br />
parse = liftM Left (parse a) `mplus` liftM Right (parse b)<br />
}<br />
<br />
char :: Syntax Char<br />
char = Syntax return charP<br />
<br />
literal :: Char -> Syntax ()<br />
literal c = Syntax (const [c]) (literalP c)<br />
<br />
space :: Syntax ()<br />
space = Syntax (const " ") spaceP<br />
<br />
iso :: (a -> b) -> (b -> a) -> Syntax a -> Syntax b<br />
iso a_to_b b_to_a a = Syntax {<br />
display = display a . b_to_a,<br />
parse = liftM a_to_b (parse a)<br />
}<br />
</haskell><br />
<br />
The last one maps a syntax using an isomorphism between types.<br />
Some uses of this function:<br />
<br />
<haskell><br />
-- concatenation, with no value in the first part<br />
(*>) :: Syntax () -> Syntax a -> Syntax a<br />
p *> q = iso snd ((,) ()) (p <*> q)<br />
<br />
-- list of a's, followed by finish<br />
list :: Syntax a -> Syntax () -> Syntax [a]<br />
list a finish = iso toList fromList (finish <|> (a <*> list a finish))<br />
where toList (Left _) = []<br />
toList (Right (x, xs)) = x:xs<br />
fromList [] = Left ()<br />
fromList (x:xs) = Right (x, xs)<br />
</haskell><br />
<br />
Now we can define the syntax of depth-first presentations:<br />
<br />
<haskell><br />
df :: Syntax (Tree Char)<br />
df = iso toTree fromTree (char <*> list df (literal '^'))<br />
where toTree (x, ts) = Node x ts<br />
fromTree (Node x ts) = (x, ts)<br />
</haskell><br />
<br />
We are using the isomorphism between <tt>Tree a</tt> and <tt>(a, [Tree a])</tt>.<br />
Some examples:<br />
<br />
<haskell><br />
Tree> display df tree5<br />
"afg^^c^bd^e^^^"<br />
Tree> runP (parse df) "afg^^c^bd^e^^^"<br />
Just (Node 'a' [Node 'f' [Node 'g' []],Node 'c' [],Node 'b' [Node 'd' [],Node 'e' []]],"")<br />
</haskell><br />
<br />
----<br />
<br />
A more naive solution, trying to split the string with stack<br />
<haskell><br />
stringToTree :: String -> Tree Char<br />
stringToTree (x:xs@(y:ys)) <br />
| y == '^' = Node x []<br />
| otherwise = Node x (map stringToTree subs)<br />
where subs = snd $ foldl parse ([],[]) $ init xs<br />
parse ([],[]) z = ([z], [[z]])<br />
parse (stack, acc) z = (stack', acc')<br />
where stack' <br />
| z == '^' = init stack<br />
| otherwise = stack ++ [z]<br />
acc' = if stack == []<br />
then acc ++ [[z]]<br />
else (init acc) ++ [(last acc) ++ [z]] <br />
</haskell><br />
<br />
----<br />
<br />
A simple solution that uses Standard Prelude functions:<br />
<haskell><br />
stringToTree :: String -> Tree Char<br />
stringToTree (x:'^':"") = Node x []<br />
stringToTree (x:xs) = Node x ys<br />
where <br />
z = map fst $ filter ((==) 0 . snd) $ zip [0..] $ <br />
scanl (+) 0 $ map (\x -> if x == '^' then -1 else 1) xs<br />
ys = map (stringToTree . uncurry (sub xs)) $ zip (init z) (tail z)<br />
sub s a b = take (b - a) $ drop a s<br />
</haskell><br />
<br />
----<br />
<br />
A solution similar to that of problem 69, special casing the root node:<br />
<haskell><br />
stringToTree :: String -> Tree Char<br />
stringToTree (x:xs) = Node x (fst (stringToTrees xs))<br />
where stringToTrees (x:xs)<br />
| x == '^' = ([], xs)<br />
| otherwise = ([Node x trees0] ++ trees1, rest1)<br />
where (trees0, rest0) = stringToTrees xs<br />
(trees1, rest1) = stringToTrees rest0<br />
</haskell><br />
<br />
----<br />
<br />
It's more direct to convert Tree back to string<br />
<haskell><br />
import Data.List<br />
<br />
treeToString :: Tree Char -> String<br />
treeToString (Node x ts) <br />
= [x] ++ (concat $ intersperse "^" (map treeToString ts)) ++ "^"<br />
</haskell><br />
<br />
----<br />
<br />
The exercise was really designed for Prolog, and calls for a bidirectional predicate. The closest thing to that, in Haskell, is a class! This is really a parsing/pretty-printing solution, with the names to match. Rather than trying to work with strings and a special "up" character (which won't really work at the type level), we use lists of explicit instructions. The requested predicate is BuildTree.<br />
<br />
<haskell><br />
{-# language MultiParamTypeClasses, FunctionalDependencies,<br />
FlexibleContexts, FlexibleInstances, UndecidableInstances,<br />
DataKinds, PolyKinds #-}<br />
<br />
import Data.Tree<br />
data Instr c = Up | Down c<br />
<br />
class ParseTree str t '[]<br />
=> BuildTree (str :: [Instr c]) (t :: Tree c) | str -> t, t -> str<br />
instance ParseTree str t '[]<br />
=> BuildTree str t<br />
</haskell><br />
<br />
We use two helpers to implement it:<br />
<br />
<haskell><br />
class ParseTree (str :: [Instr c]) (t :: Tree c) (rem :: [Instr c])<br />
| str -> t rem, t rem -> str<br />
class ParseForest (str :: [Instr c]) (ts :: [Tree c]) (rem :: [Instr c])<br />
| str -> ts rem, ts rem -> str<br />
<br />
instance ParseTree ('Down c ': 'Up ': r) ('Node c '[]) r<br />
instance ParseForest ('Down d ': is) (t ': ts) r<br />
=> ParseTree ('Down c ': 'Down d ': is) ('Node c (t ': ts)) r<br />
<br />
instance ParseForest ('Up ': is) '[] is<br />
instance ( ParseTree ('Down c ': is) t r<br />
, ParseForest r ts r')<br />
=> ParseForest ('Down c ': is) (t ': ts) r'<br />
</haskell><br />
<br />
[[Category:Programming exercise spoilers]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/70&diff=6213799 questions/Solutions/702017-09-24T17:42:51Z<p>Dfeuer: </p>
<hr />
<div>(**) Tree construction from a node string.<br />
<br />
We suppose that the nodes of a multiway tree contain single characters. In the depth-first order sequence of its nodes, a special character ^ has been inserted whenever, during the tree traversal, the move is a backtrack to the previous level.<br />
<br />
By this rule, the tree below (<tt>tree5</tt>) is represented as: <tt>afg^^c^bd^e^^^</tt><br />
<br />
https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/p70.gif<br />
<br />
Define the syntax of the string and write a predicate tree(String,Tree) to construct the Tree when the String is given.<br />
Make your predicate work in both directions.<br />
<br />
We could write separate printing and parsing functions, but the problem statement asks for a bidirectional function.<br />
<br />
First we need a parser monad, with some primitives:<br />
<br />
<haskell><br />
newtype P a = P { runP :: String -> Maybe (a, String) }<br />
<br />
instance Monad P where<br />
return x = P $ \ s -> Just (x, s)<br />
P v >>= f = P $ \ s -> do<br />
(x, s') <- v s<br />
runP (f x) s'<br />
<br />
instance MonadPlus P where<br />
mzero = P $ \ _ -> Nothing<br />
P u `mplus` P v = P $ \ s -> u s `mplus` v s<br />
<br />
charP :: P Char<br />
charP = P view_list<br />
where view_list [] = Nothing<br />
view_list (c:cs) = Just (c, cs)<br />
<br />
literalP :: Char -> P ()<br />
literalP c = do { c' <- charP; guard (c == c') }<br />
<br />
spaceP :: P ()<br />
spaceP = P (\ s -> Just ((), dropWhile isSpace s))<br />
</haskell><br />
<br />
Next a <tt>Syntax</tt> type, combining printing and parsing functions:<br />
<br />
<haskell><br />
data Syntax a = Syntax {<br />
display :: a -> String,<br />
parse :: P a<br />
}<br />
</haskell><br />
<br />
(We don't use a class, because we want multiple syntaxes for a given type.)<br />
Some combinators for building syntaxes:<br />
<br />
<haskell><br />
-- concatenation<br />
(<*>) :: Syntax a -> Syntax b -> Syntax (a,b)<br />
a <*> b = Syntax {<br />
display = \ (va,vb) -> display a va ++ display b vb,<br />
parse = liftM2 (,) (parse a) (parse b)<br />
}<br />
<br />
-- alternatives<br />
(<|>) :: Syntax a -> Syntax b -> Syntax (Either a b)<br />
a <|> b = Syntax {<br />
display = either (display a) (display b),<br />
parse = liftM Left (parse a) `mplus` liftM Right (parse b)<br />
}<br />
<br />
char :: Syntax Char<br />
char = Syntax return charP<br />
<br />
literal :: Char -> Syntax ()<br />
literal c = Syntax (const [c]) (literalP c)<br />
<br />
space :: Syntax ()<br />
space = Syntax (const " ") spaceP<br />
<br />
iso :: (a -> b) -> (b -> a) -> Syntax a -> Syntax b<br />
iso a_to_b b_to_a a = Syntax {<br />
display = display a . b_to_a,<br />
parse = liftM a_to_b (parse a)<br />
}<br />
</haskell><br />
<br />
The last one maps a syntax using an isomorphism between types.<br />
Some uses of this function:<br />
<br />
<haskell><br />
-- concatenation, with no value in the first part<br />
(*>) :: Syntax () -> Syntax a -> Syntax a<br />
p *> q = iso snd ((,) ()) (p <*> q)<br />
<br />
-- list of a's, followed by finish<br />
list :: Syntax a -> Syntax () -> Syntax [a]<br />
list a finish = iso toList fromList (finish <|> (a <*> list a finish))<br />
where toList (Left _) = []<br />
toList (Right (x, xs)) = x:xs<br />
fromList [] = Left ()<br />
fromList (x:xs) = Right (x, xs)<br />
</haskell><br />
<br />
Now we can define the syntax of depth-first presentations:<br />
<br />
<haskell><br />
df :: Syntax (Tree Char)<br />
df = iso toTree fromTree (char <*> list df (literal '^'))<br />
where toTree (x, ts) = Node x ts<br />
fromTree (Node x ts) = (x, ts)<br />
</haskell><br />
<br />
We are using the isomorphism between <tt>Tree a</tt> and <tt>(a, [Tree a])</tt>.<br />
Some examples:<br />
<br />
<haskell><br />
Tree> display df tree5<br />
"afg^^c^bd^e^^^"<br />
Tree> runP (parse df) "afg^^c^bd^e^^^"<br />
Just (Node 'a' [Node 'f' [Node 'g' []],Node 'c' [],Node 'b' [Node 'd' [],Node 'e' []]],"")<br />
</haskell><br />
<br />
----<br />
<br />
A more naive solution, trying to split the string with stack<br />
<haskell><br />
stringToTree :: String -> Tree Char<br />
stringToTree (x:xs@(y:ys)) <br />
| y == '^' = Node x []<br />
| otherwise = Node x (map stringToTree subs)<br />
where subs = snd $ foldl parse ([],[]) $ init xs<br />
parse ([],[]) z = ([z], [[z]])<br />
parse (stack, acc) z = (stack', acc')<br />
where stack' <br />
| z == '^' = init stack<br />
| otherwise = stack ++ [z]<br />
acc' = if stack == []<br />
then acc ++ [[z]]<br />
else (init acc) ++ [(last acc) ++ [z]] <br />
</haskell><br />
<br />
----<br />
<br />
A simple solution that uses Standard Prelude functions:<br />
<haskell><br />
stringToTree :: String -> Tree Char<br />
stringToTree (x:'^':"") = Node x []<br />
stringToTree (x:xs) = Node x ys<br />
where <br />
z = map fst $ filter ((==) 0 . snd) $ zip [0..] $ <br />
scanl (+) 0 $ map (\x -> if x == '^' then -1 else 1) xs<br />
ys = map (stringToTree . uncurry (sub xs)) $ zip (init z) (tail z)<br />
sub s a b = take (b - a) $ drop a s<br />
</haskell><br />
<br />
----<br />
<br />
A solution similar to that of problem 69, special casing the root node:<br />
<haskell><br />
stringToTree :: String -> Tree Char<br />
stringToTree (x:xs) = Node x (fst (stringToTrees xs))<br />
where stringToTrees (x:xs)<br />
| x == '^' = ([], xs)<br />
| otherwise = ([Node x trees0] ++ trees1, rest1)<br />
where (trees0, rest0) = stringToTrees xs<br />
(trees1, rest1) = stringToTrees rest0<br />
</haskell><br />
<br />
----<br />
<br />
It's more direct to convert Tree back to string<br />
<haskell><br />
import Data.List<br />
<br />
treeToString :: Tree Char -> String<br />
treeToString (Node x ts) <br />
= [x] ++ (concat $ intersperse "^" (map treeToString ts)) ++ "^"<br />
</haskell><br />
<br />
----<br />
<br />
The exercise was really designed for Prolog, and calls for a bidirectional predicate. The closest thing to that, in Haskell, is a class! This is really a parsing/pretty-printing solution, with the names to match. The requested predicate is BuildTree:<br />
<br />
<haskell><br />
class ParseTree str t '[]<br />
=> BuildTree (str :: [Instr c]) (t :: Tree c) | str -> t, t -> str<br />
instance ParseTree str t '[]<br />
=> BuildTree str t<br />
</haskell><br />
<br />
We use two helpers to implement it:<br />
<br />
<haskell><br />
class ParseTree (str :: [Instr c]) (t :: Tree c) (rem :: [Instr c])<br />
| str -> t rem, t rem -> str<br />
class ParseForest (str :: [Instr c]) (ts :: [Tree c]) (rem :: [Instr c])<br />
| str -> ts rem, ts rem -> str<br />
<br />
instance ParseTree ('Down c ': 'Up ': r) ('Node c '[]) r<br />
instance ParseForest ('Down d ': is) (t ': ts) r<br />
=> ParseTree ('Down c ': 'Down d ': is) ('Node c (t ': ts)) r<br />
<br />
instance ParseForest ('Up ': is) '[] is<br />
instance ( ParseTree ('Down c ': is) t r<br />
, ParseForest r ts r')<br />
=> ParseForest ('Down c ': is) (t ': ts) r'<br />
</haskell><br />
<br />
[[Category:Programming exercise spoilers]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/70&diff=6213699 questions/Solutions/702017-09-23T19:46:20Z<p>Dfeuer: </p>
<hr />
<div>(**) Tree construction from a node string.<br />
<br />
We suppose that the nodes of a multiway tree contain single characters. In the depth-first order sequence of its nodes, a special character ^ has been inserted whenever, during the tree traversal, the move is a backtrack to the previous level.<br />
<br />
By this rule, the tree below (<tt>tree5</tt>) is represented as: <tt>afg^^c^bd^e^^^</tt><br />
<br />
https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/p70.gif<br />
<br />
Define the syntax of the string and write a predicate tree(String,Tree) to construct the Tree when the String is given.<br />
Make your predicate work in both directions.<br />
<br />
We could write separate printing and parsing functions, but the problem statement asks for a bidirectional function.<br />
<br />
First we need a parser monad, with some primitives:<br />
<br />
<haskell><br />
newtype P a = P { runP :: String -> Maybe (a, String) }<br />
<br />
instance Monad P where<br />
return x = P $ \ s -> Just (x, s)<br />
P v >>= f = P $ \ s -> do<br />
(x, s') <- v s<br />
runP (f x) s'<br />
<br />
instance MonadPlus P where<br />
mzero = P $ \ _ -> Nothing<br />
P u `mplus` P v = P $ \ s -> u s `mplus` v s<br />
<br />
charP :: P Char<br />
charP = P view_list<br />
where view_list [] = Nothing<br />
view_list (c:cs) = Just (c, cs)<br />
<br />
literalP :: Char -> P ()<br />
literalP c = do { c' <- charP; guard (c == c') }<br />
<br />
spaceP :: P ()<br />
spaceP = P (\ s -> Just ((), dropWhile isSpace s))<br />
</haskell><br />
<br />
Next a <tt>Syntax</tt> type, combining printing and parsing functions:<br />
<br />
<haskell><br />
data Syntax a = Syntax {<br />
display :: a -> String,<br />
parse :: P a<br />
}<br />
</haskell><br />
<br />
(We don't use a class, because we want multiple syntaxes for a given type.)<br />
Some combinators for building syntaxes:<br />
<br />
<haskell><br />
-- concatenation<br />
(<*>) :: Syntax a -> Syntax b -> Syntax (a,b)<br />
a <*> b = Syntax {<br />
display = \ (va,vb) -> display a va ++ display b vb,<br />
parse = liftM2 (,) (parse a) (parse b)<br />
}<br />
<br />
-- alternatives<br />
(<|>) :: Syntax a -> Syntax b -> Syntax (Either a b)<br />
a <|> b = Syntax {<br />
display = either (display a) (display b),<br />
parse = liftM Left (parse a) `mplus` liftM Right (parse b)<br />
}<br />
<br />
char :: Syntax Char<br />
char = Syntax return charP<br />
<br />
literal :: Char -> Syntax ()<br />
literal c = Syntax (const [c]) (literalP c)<br />
<br />
space :: Syntax ()<br />
space = Syntax (const " ") spaceP<br />
<br />
iso :: (a -> b) -> (b -> a) -> Syntax a -> Syntax b<br />
iso a_to_b b_to_a a = Syntax {<br />
display = display a . b_to_a,<br />
parse = liftM a_to_b (parse a)<br />
}<br />
</haskell><br />
<br />
The last one maps a syntax using an isomorphism between types.<br />
Some uses of this function:<br />
<br />
<haskell><br />
-- concatenation, with no value in the first part<br />
(*>) :: Syntax () -> Syntax a -> Syntax a<br />
p *> q = iso snd ((,) ()) (p <*> q)<br />
<br />
-- list of a's, followed by finish<br />
list :: Syntax a -> Syntax () -> Syntax [a]<br />
list a finish = iso toList fromList (finish <|> (a <*> list a finish))<br />
where toList (Left _) = []<br />
toList (Right (x, xs)) = x:xs<br />
fromList [] = Left ()<br />
fromList (x:xs) = Right (x, xs)<br />
</haskell><br />
<br />
Now we can define the syntax of depth-first presentations:<br />
<br />
<haskell><br />
df :: Syntax (Tree Char)<br />
df = iso toTree fromTree (char <*> list df (literal '^'))<br />
where toTree (x, ts) = Node x ts<br />
fromTree (Node x ts) = (x, ts)<br />
</haskell><br />
<br />
We are using the isomorphism between <tt>Tree a</tt> and <tt>(a, [Tree a])</tt>.<br />
Some examples:<br />
<br />
<haskell><br />
Tree> display df tree5<br />
"afg^^c^bd^e^^^"<br />
Tree> runP (parse df) "afg^^c^bd^e^^^"<br />
Just (Node 'a' [Node 'f' [Node 'g' []],Node 'c' [],Node 'b' [Node 'd' [],Node 'e' []]],"")<br />
</haskell><br />
<br />
----<br />
<br />
A more naive solution, trying to split the string with stack<br />
<haskell><br />
stringToTree :: String -> Tree Char<br />
stringToTree (x:xs@(y:ys)) <br />
| y == '^' = Node x []<br />
| otherwise = Node x (map stringToTree subs)<br />
where subs = snd $ foldl parse ([],[]) $ init xs<br />
parse ([],[]) z = ([z], [[z]])<br />
parse (stack, acc) z = (stack', acc')<br />
where stack' <br />
| z == '^' = init stack<br />
| otherwise = stack ++ [z]<br />
acc' = if stack == []<br />
then acc ++ [[z]]<br />
else (init acc) ++ [(last acc) ++ [z]] <br />
</haskell><br />
<br />
----<br />
<br />
A simple solution that uses Standard Prelude functions:<br />
<haskell><br />
stringToTree :: String -> Tree Char<br />
stringToTree (x:'^':"") = Node x []<br />
stringToTree (x:xs) = Node x ys<br />
where <br />
z = map fst $ filter ((==) 0 . snd) $ zip [0..] $ <br />
scanl (+) 0 $ map (\x -> if x == '^' then -1 else 1) xs<br />
ys = map (stringToTree . uncurry (sub xs)) $ zip (init z) (tail z)<br />
sub s a b = take (b - a) $ drop a s<br />
</haskell><br />
<br />
----<br />
<br />
A solution similar to that of problem 69, special casing the root node:<br />
<haskell><br />
stringToTree :: String -> Tree Char<br />
stringToTree (x:xs) = Node x (fst (stringToTrees xs))<br />
where stringToTrees (x:xs)<br />
| x == '^' = ([], xs)<br />
| otherwise = ([Node x trees0] ++ trees1, rest1)<br />
where (trees0, rest0) = stringToTrees xs<br />
(trees1, rest1) = stringToTrees rest0<br />
</haskell><br />
<br />
----<br />
<br />
It's more direct to convert Tree back to string<br />
<haskell><br />
import Data.List<br />
<br />
treeToString :: Tree Char -> String<br />
treeToString (Node x ts) <br />
= [x] ++ (concat $ intersperse "^" (map treeToString ts)) ++ "^"<br />
</haskell><br />
<br />
----<br />
<br />
Since the exercise was really designed for Prolog, let's write an implementation at the class level. This is really a parsing/pretty-printing solution, with the names to match. The requested predicate is BuildTree:<br />
<br />
<haskell><br />
class ParseTree str t '[] => BuildTree (str :: [Instr c]) (t :: Tree c) | str -> t, t -> str<br />
instance ParseTree str t '[] => BuildTree str t<br />
</haskell><br />
<br />
We use two helpers to implement it:<br />
<br />
<haskell><br />
class ParseTree (str :: [Instr c]) (t :: Tree c) (rem :: [Instr c]) | str -> t rem, t rem -> str<br />
class ParseForest (str :: [Instr c]) (ts :: [Tree c]) (rem :: [Instr c]) | str -> ts rem, ts rem -> str<br />
<br />
instance ParseTree ('Down c ': 'Up ': r) ('Node c '[]) r<br />
instance ParseForest ('Down d ': is) (t ': ts) r => ParseTree ('Down c ': 'Down d ': is) ('Node c (t ': ts)) r<br />
instance ParseForest ('Up ': is) '[] is<br />
instance ( ParseTree ('Down c ': is) t r<br />
, ParseForest r ts r') => ParseForest ('Down c ': is) (t ': ts) r'<br />
</haskell><br />
<br />
[[Category:Programming exercise spoilers]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Typeclassopedia&diff=61970Typeclassopedia2017-07-06T00:34:06Z<p>Dfeuer: /* Utility functions */</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:<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). It is possible that <code>return</code> may eventually be removed from the <code>Monad</code> class.<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, this is a lie: using a Haskell parsing library 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 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 [http://homepages.inf.ed.ac.uk/ratkey/paramnotions-jfp.pdf parameterized monads], or Dan Piponi’s [http://blog.sigfpe.com/2009/02/beyond-monads.html Beyond Monads].<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</class>. 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 />
==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>Dfeuerhttps://wiki.haskell.org/index.php?title=Typeclassopedia&diff=61892Typeclassopedia2017-06-08T00:08:36Z<p>Dfeuer: /* Definition */</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:<br />
<br />
[[Image:Typeclassopedia-diagram.png]]<br />
<br />
{{note|<code>Semigroup</code> can be found in the [http://hackage.haskell.org/package/semigroups <code>semigroups</code> package], <code>Apply</code> 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 arrows</span> indicate some other sort of relationship.<br />
* <code>Monad</code> and <code>ArrowApply</code> are equivalent.<br />
* <code>Semigroup</code>, <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 _ [] = []<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 _ 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 _ [] = []<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 = undefined -- exercise<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 x = [x]<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). It is possible that <code>return</code> may eventually be removed from the <code>Monad</code> class.<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 = Just<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, this is a lie: using a Haskell parsing library 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 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 [http://homepages.inf.ed.ac.uk/ratkey/paramnotions-jfp.pdf parameterized monads], or Dan Piponi’s [http://blog.sigfpe.com/2009/02/beyond-monads.html Beyond Monads].<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</class>. 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 />
==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 x = (mempty, x)<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 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 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.<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 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 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 return<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 g = g<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 f = Kleisli (return . f)<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 />
instance Monad m => ArrowApply (Kleisli m) where<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 = -- exercise<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 (Cons x _) = x<br />
duplicate s@(Cons x xs) = Cons s (duplicate xs)<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>Dfeuerhttps://wiki.haskell.org/index.php?title=Typeclassopedia&diff=61891Typeclassopedia2017-06-07T23:58:58Z<p>Dfeuer: </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:<br />
<br />
[[Image:Typeclassopedia-diagram.png]]<br />
<br />
{{note|<code>Semigroup</code> can be found in the [http://hackage.haskell.org/package/semigroups <code>semigroups</code> package], <code>Apply</code> 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 arrows</span> indicate some other sort of relationship.<br />
* <code>Monad</code> and <code>ArrowApply</code> are equivalent.<br />
* <code>Semigroup</code>, <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 _ [] = []<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 _ 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 _ [] = []<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 = undefined -- exercise<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 x = [x]<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). It is possible that <code>return</code> may eventually be removed from the <code>Monad</code> class.<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 = Just<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, this is a lie: using a Haskell parsing library 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 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 [http://homepages.inf.ed.ac.uk/ratkey/paramnotions-jfp.pdf parameterized monads], or Dan Piponi’s [http://blog.sigfpe.com/2009/02/beyond-monads.html Beyond Monads].<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</class>. 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 />
==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. <code>sconcat</code> reduces a nonempty list using <code>(<>)</code>; <code>stimes n</code> is equivalent to (but sometimes considerably more efficient than) <code>sconcat . replicate n</code>. 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 x = (mempty, x)<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 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 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.<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 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 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 return<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 g = g<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 f = Kleisli (return . f)<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 />
instance Monad m => ArrowApply (Kleisli m) where<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 = -- exercise<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 (Cons x _) = x<br />
duplicate s@(Cons x xs) = Cons s (duplicate xs)<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>Dfeuerhttps://wiki.haskell.org/index.php?title=Typeclassopedia&diff=61890Typeclassopedia2017-06-07T23:56:06Z<p>Dfeuer: Add MonadFail</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:<br />
<br />
[[Image:Typeclassopedia-diagram.png]]<br />
<br />
{{note|<code>Semigroup</code> can be found in the [http://hackage.haskell.org/package/semigroups <code>semigroups</code> package], <code>Apply</code> 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 arrows</span> indicate some other sort of relationship.<br />
* <code>Monad</code> and <code>ArrowApply</code> are equivalent.<br />
* <code>Semigroup</code>, <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 _ [] = []<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 _ 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 _ [] = []<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 = undefined -- exercise<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 x = [x]<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). It is possible that <code>return</code> may eventually be removed from the <code>Monad</code> class.<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 = Just<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, this is a lie: using a Haskell parsing library 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 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 [http://homepages.inf.ed.ac.uk/ratkey/paramnotions-jfp.pdf parameterized monads], or Dan Piponi’s [http://blog.sigfpe.com/2009/02/beyond-monads.html Beyond Monads].<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</class>. 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 />
==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. <code>sconcat</code> reduces a nonempty list using <code>(<>)</code>; <code>stimes n</code> is equivalent to (but more efficient than) <code>sconcat . replicate n</code>. 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 x = (mempty, x)<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 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 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.<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 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 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 return<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 g = g<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 f = Kleisli (return . f)<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 />
instance Monad m => ArrowApply (Kleisli m) where<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 = -- exercise<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 (Cons x _) = x<br />
duplicate s@(Cons x xs) = Cons s (duplicate xs)<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>Dfeuerhttps://wiki.haskell.org/index.php?title=User:John_Peterson&diff=61609User:John Peterson2017-03-08T19:52:24Z<p>Dfeuer: /* John Peterson */</p>
<hr />
<div>== John Peterson ==<br />
<br />
John Peterson (died March 2017) was a computer science researcher, instructor, and professor. He co-authored the Haskell Report and was active in the Haskell community, including helping to maintaining haskell.org.<br />
<br />
Peterson was on the research faculty at Yale University from 1990 to 2004.<br />
<br />
From 2005 until his death, Peterson was a professor at Western State College (http://www.western.edu), high in the Colorado mountains.<br />
<br />
He had also taught at the University of Arizona and Central Connecticut State University.<br />
<br />
<br />
Home pages:<br />
*http://haskell.org/jcp<br />
*http://www.western.edu/computerscience</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Research_papers/Parallelism_and_concurrency&diff=61416Research papers/Parallelism and concurrency2017-01-10T05:15:01Z<p>Dfeuer: /* Lock free data structures and transactional memory */ Fix broken links</p>
<hr />
<div>__TOC__<br />
<br />
See also [[Libraries_and_tools/Concurrency_and_parallelism]]<br />
<br />
==General==<br />
<br />
* [http://www.haskell.org/~simonmar/bib/bib.html Simon Marlow's publications on parallelism and GHC]<br />
<br />
==Concurrent Haskell==<br />
<br />
;[http://research.microsoft.com/~simonpj/Papers/conc-ffi/index.htm Extending the Haskell Foreign Function Interface with Concurrency]<br />
:Simon Marlow, Simon Peyton Jones, and Wolfgang Thaller, Proceedings of the Haskell Workshop, Snowbird, Sept 2004.<br />
<br />
;[http://www.haskell.org/ghc/docs/papers/concurrent-haskell.ps.gz Concurrent Haskell]<br />
:SL Peyton Jones, A Gordon, S Finne, 23rd ACM Symposium on Principles of Programming Languages, St Petersburg Beach, Florida, Jan 1996, pp295-308.<br />
<br />
;[http://research.microsoft.com/~simonpj/Papers/marktoberdorf Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell]<br />
:In "Engineering theories of software construction", ed Tony Hoare, Manfred Broy, Ralf Steinbruggen, IOS Press, ISBN 1 58603 1724, 2001, pp47-96.<br />
<br />
;[http://www.haskell.org/~simonmar/papers/web-server-jfp.pdf Developing a high-performance web server in Concurrent Haskell]<br />
:Simon Marlow. Journal of Functional Programming, 12(4+5):359--374, July 2002<br />
<br />
;[http://www.cis.upenn.edu/~stevez/papers/LZ06b.pdf A language-based approach to unifying events and threads]<br />
:Peng Li and Steve Zdancewic, April 2006.<br />
<br />
;[http://citeseer.ist.psu.edu/184227.html Four Concurrency Primitives for Haskell]<br />
:Enno Scholz (1995)<br />
<br />
;[http://web.cecs.pdx.edu/~mpj/pubs/par.html Implicit and explicit parallel programming in Haskell]<br />
:Mark P. Jones and Paul Hudak, Research Report YALEU/DCS/RR-982, Yale University, New Haven, Connecticut, USA, August 1993.<br />
<br />
==Parallel Haskell==<br />
<br />
;[http://www.haskell.org/~simonmar/papers/multiproc.pdf Haskell on a Shared-Memory Multiprocessor]<br />
:Tim Harris, Simon Marlow, Simon Peyton Jones) Haskell '05: Proceedings of the 2005 ACM SIGPLAN workshop on Haskell, pages 49--61, Tallinn, Estonia, ACM Press, September 2005<br />
<br />
;[http://www.cse.unsw.edu.au/~chak/papers/CLPKM06.html Data Parallel Haskell: a status report]<br />
:Manuel M. T. Chakravarty, Roman Leshchinskiy, Simon Peyton Jones, Gabriele Keller, and Simon Marlow, 2006.<br />
<br />
;[http://www.scpe.org/vols/vol07/no3/SCPE_7_3_02.pdf Managing Heterogeneity in a Grid Parallel Haskell]<br />
:A. D. Al Zain, P. W. Trinder, G.J.Michaelson and H-W.Loid<br />
<br />
;[http://www.macs.hw.ac.uk/~trinder/papers/EdenGpH.ps Comparing Parallel Functional Languages: Programming and Performance]<br />
:Loidl H-W. Rubio F. Scaife N. Hammond K. Horiguchi S. Klusik U. Loogen R. Michaelson G.J. Pena R. Priebe S. Trinder P.W. Higher-order and Symbolic Computation 16(3) (2003).<br />
<br />
;[http://www.macs.hw.ac.uk/~trinder/papers/ParDistrHaskells.ps Parallel and Distributed Haskells]<br />
:Trinder P.W. Loidl H-W. Pointon R.F. Journal of Functional Programming 12(4&5) (2002), pp 469-510.<br />
<br />
;[http://www.macs.hw.ac.uk/~dsg/gph/papers/abstracts/europar00.html The Multi-Architecture Performance of the Parallel Functional Language GPH]<br />
:Trinder P.W. Loidl H-W. Barry E. Rebon Portillo A.J. Euro-Par 2000 - Parallel Processing, Munich, Germany, Bode A. Ludwig T. and Wismuller R. (Eds) Springer Verlag LNCS (September 2000), pp 739-743.<br />
<br />
;[http://www-fp.dcs.st-and.ac.uk/pfpbook/ Large-scale Functional Applications]<br />
:Trinder P.W. Loidl H-W. Hammond K. Chapter of Parallel Functional Programming, Hammond K. and Michaelson G. (Eds) Springer Verlag ISBN 1-85233-092-9 (2000).<br />
<br />
;[http://www.macs.hw.ac.uk/~dsg/gdh/papers/diyGdh_ifl00_draft.ps The Design and Implementation of Glasgow distributed Haskell]<br />
:Pointon R.F. Trinder P.W. Loidl H-W. IFL'00 - 12th International Workshop on the Implementation of Functional Languages, Aachen, Germany (September 2000) Springer Verlag LNCS 2011, pp 53-70..<br />
<br />
;[http://www.macs.hw.ac.uk/~dsg/gdh/papers/sprocl.ps Motivation for GdH, a Distributed Non-strict Functional Language]<br />
:Trinder P.W. International Workshop on Parallel and Distributed Computing for Symbolic, Irregular Applications (PDSIA'99), Sendai, Japan (July 1999), T. Ito, T Yuasa (Eds), World Scientific ISBN 981-02-4139-9 (2000), pp 72-81.<br />
<br />
;[http://www.macs.hw.ac.uk/~dsg/gph/papers/abstracts/cpe.html Engineering Parallel Symbolic Programs in GpH]<br />
:Loidl H-W. Trinder P.W. Hall C.V. Hammond K. Junaidu S.B. Morgan R.G. Peyton Jones S.L. Concurrency: Practice and Experience, 11(12) (December 1999), pp 701-752.<br />
<br />
;[http://www.macs.hw.ac.uk/~dsg/gph/papers/abstracts/strategies.html Algorithm + Strategy = Parallelism]<br />
:Trinder P.W. Hammond K. Loidl H-W. Peyton Jones S.L. Journal of Functional Programming, 8(1) (January 1998) pp~23-60.<br />
<br />
;[http://www.macs.hw.ac.uk/~gnik/apset/transitionsystem.ps.gz Towards an operational semantics for a parallel non-strict functional language]<br />
:Hall J.G. Baker-Finch C. Trinder P.W. King D.J. Proceedings of the 10th. Int. Workshop on Implementation of Functional Languages, University College London, England (September 1998), Springer Verlag LNCS 1595, pp 55-67 .<br />
<br />
;[http://www.macs.hw.ac.uk/~gnik/apset/strat-prof.ps.gz A Strategic Profiler for Glasgow Parallel Haskell (GpH)]<br />
:King D.J. Hall J.G. Trinder P.W. Proceedings of the 10th. Int. Workshop on Implementation of Functional Languages, University College London, England (September 1998), {\em Springer Verlag} LNCS 1595, pp 90-104. <br />
<br />
;[http://www.macs.hw.ac.uk/~dsg/gph/papers/abstracts/gum.html GUM: a Portable Parallel implementation of Haskell]<br />
:Trinder P. Hammond K. Mattson J. Partridge A. Peyton Jones S. Proceedings of Programming Languages Design and Implementation (PLDI), Philadelphia, USA, (May 1996) pp79-88.<br />
<br />
;[http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.53.8875 An Operational Semantics for Parallel Lazy Evaluation]<br />
:Clem Baker-Finch, University of Canberra, ICFP 2000.p162-173<br />
<br />
==Lock free data structures and transactional memory==<br />
<br />
;[https://www.microsoft.com/en-us/research/publication/composable-memory-transactions/ Composable Memory Transactions] <br />
:Tim Harris, Simon Marlow, Simon Peyton Jones, Maurice Herlihy. PPoPP'05: ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming, Chicago, Illinois, June 2005<br />
<br />
;[https://www.microsoft.com/en-us/research/publication/lock-free-data-structures-using-stms-in-haskell/ Lock Free Data Structures using STMs in Haskell]<br />
:Anthony Discolo, Tim Harris, Simon Marlow, Simon Peyton Jones, Satnam Singh) FLOPS 2006: Eighth International Symposium on Functional and Logic Programming, Fuji Susono, JAPAN, April 2006<br />
<br />
;[https://www.microsoft.com/en-us/research/publication/transactional-memory-data-invariants/ Transactional memory with data invariants]<br />
:Tim Harris, Simon Peyton-Jones. March 2006 TRANSACT '06<br />
<br />
==Parallel garbage collection==<br />
<br />
See also [[Research_papers/Runtime_systems#Garbage_collection|garbage collection]].<br />
<br />
;[http://research.microsoft.com/~simonpj/papers/parallel-gc/index.htm Parallel generational-copying garbage collection with a block-structured heap]<br />
:Simon Marlow, Tim Harris, Roshan P. James, Simon Peyton Jones, International Symposium on Memory Management 2008.<br />
<br />
;[http://portal.acm.org/citation.cfm?id=378823&dl=ACM&coll=GUIDE&CFID=11111111&CFTOKEN=2222222 A parallel, real-time garbage collector]<br />
:P Cheng, GE Blelloch 2001<br />
<br />
;[http://reports-archive.adm.cs.cmu.edu/anon/usr0/ftp/home/anon/usr/anon/2001/CMU-CS-01-174.pdf Scalable Real-time Parallel Garbage Collection for Symmetric Multiprocessors]<br />
:P Cheng, 2001<br />
<br />
;[http://www.macs.hw.ac.uk/~dsg/gph/papers/ps/graphing.ps.gz GRAPHing the Future]<br />
:K Hammond, HW Loidl, JS Mattson, AS Partridge, SL Peyton Jones<br />
<br />
;[http://portal.acm.org/citation.cfm%3Fid%3D1133255.1134022 Correctness-preserving derivation of concurrent garbage collection algorithms]<br />
:MT Vechev, E Yahav, DF Bacon, 2006.<br />
<br />
;[http://portal.acm.org/citation.cfm?id=301631.301648 On bounding time and space for multiprocessor garbage collection]<br />
:GE Blelloch, P Cheng, 1999<br />
<br />
;[http://www.ai.mit.edu/projects/aries/Documents/jhbrown_thesis_proposal.pdf Memory Management on a Massively Parallel Capability Architecture]<br />
:J Brown<br />
<br />
==Mobile Haskell==<br />
<br />
;[http://www.jucs.org/jucs_12_7/strong_mobility_in_mobile Strong Mobility in Mobile Haskell]<br />
:Rauber Du Bois A. Trinder P.W. Loidl H.W. Journal of Universal Computer Science, 12(7) (2006), pp 868-884.<br />
<br />
;[http://www.macs.hw.ac.uk/~trinder/papers/SBLP05.pdf mHaskell: Mobile Computation in a Purely Functional Language]<br />
:Rauber Du Bois A. Trinder P.W. Loidl H.W. Journal of Universal Computer Science, 11(7) (2005), pp 1234-1254.<br />
<br />
;[http://www.macs.hw.ac.uk/~trinder/papers/TFP03-mhaskell.pdf Implementing Mobile Haskell]<br />
:Rauber Du Bois A. Trinder P.W. Loidl H.W. Proc. TFP'03 --- Trends in Functional Programming Vol.4, Intellect (September 2003), pp 79-94.<br />
<br />
;[http://www.macs.hw.ac.uk/~trinder/papers/wflp03.ps Towards a Mobile Haskell]<br />
:Rauber Du Bois A. Trinder P.W. Loidl H.W. Proc. 12th Int. Workshop on Functional and (Constraint) Logic Programming (WFLP 2003), Valencia, Spain (June 2003), pp 113-116.<br />
<br />
==Parallel graph reduction==<br />
<br />
;[http://portal.acm.org/citation.cfm?id=36583.36590&coll=Portal&dl=GUIDE&CFID=11111111&CFTOKEN=2222222 GRIP A high-performance architecture for parallel graph reduction]<br />
:SLP Jones, C Clack, J Salkild, M Hardie, 1987.<br />
<br />
;[http://portal.acm.org/citation.cfm?id=99370.99386 Parallel graph reduction with the (v, G)-machine]<br />
:L Augustsson, T Johnsson, 1990<br />
<br />
See also [[Applications and libraries/Concurrency and parallelism]]<br />
<br />
[[Category:Research]]<br />
[[Category:Parallel]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Hac_%CF%86/Attendees&diff=61017Hac φ/Attendees2016-08-17T02:07:05Z<p>Dfeuer: /* Additional Comments */</p>
<hr />
<div>This is a partial list of attendees for [[Hac φ]]. Please refer to the [[Hac φ|main page]] for more information.<br />
<br />
= Attendees =<br />
<br />
Feel free to list yourself here -- though this is ''not'' registration. You can register [https://goo.gl/forms/510yxJ4KOHR8QKYg1 here].<br />
<br />
{| class="wikitable"<br />
! Nickname<br />
! Real Name<br />
! Mobile #<br />
! Accommodation<br />
|-<br />
|<br />
| Antal Spector-Zabusky<br />
| <br />
| local<br />
|-<br />
|<br />
| Kenny Foner<br />
|<br />
| local<br />
|-<br />
|<br />
| John Leo<br />
|<br />
| TBD<br />
|-<br />
| cdsmith<br />
| Chris Smith<br />
| 719-640-8663<br />
| Club Quarters Hotel<br />
|}<br />
<br />
= Additional Comments =<br />
<br />
Please use this section to leave comments for other attendees, e.g. for organizing accommodation.<br />
<br />
* David Feuer is looking for a place to stay.<br />
<br />
[[Category:Community]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=A_practical_Template_Haskell_Tutorial&diff=60776A practical Template Haskell Tutorial2016-05-14T22:05:53Z<p>Dfeuer: Use `for` instead of combining `sequence` with a list comprehension.</p>
<hr />
<div>This tutorial explores the Glasgow Haskell Compiler's compile-time meta programming in Template Haskell. It motivates use cases for meta programming and explains the different Template Haskell features on simple toy programs. The aim is to give an overview of Template Haskell's functionality in an example-driven manner.<br />
<br />
= Introduction =<br />
<br />
Template Haskell (TH) is the standard framework for doing type-safe, compile-time meta programming in the Glasgow Haskell Compiler (GHC). It allows writing Haskell meta programs, which are evaluated at compile-time, and which produce Haskell programs as the results of their execution.<br />
<br />
Template Haskell was conceived by Tim Sheard and Simon Peyton Jones<ref name="th1" /> by drawing on the ideas of Lisp macros, but in the typed setting of Haskell. Since then, the original implementation has evolved quite a bit<ref name="th2" /><ref name="th3" />. Most notably, in 2007 Geoffrey Mainland added support for quasi quoting<ref name="qq" />, which makes the embedding of domain specific languages into the Haskell host language much easier.<br />
<br />
As it exists today, Template Haskell has two main areas of application: Haskell code generation at compile-time and facilitating the embedding of domain specific languages.<br />
<br />
As a code generator, Template Haskell empowers a user to write many, syntactically different, programs all at once by means of a single meta program. All that is needed is a uniform, algorithmic description to create the different result programs. And the meta program then precisely implements the algorithm to compute all the different result programs as its output. This proves useful for example to avoid writing the same repetitive, boilerplate code over and over again. To this end, Template Haskell is used (among many others) in the <code>aeson</code> library to automatically derive a data type's <code>ToJSON</code> and <code>FromJSON</code> instances for JSON serialization; and in the <code>lens</code> library to mechanically create a data type's lenses.<br />
<br />
As a framework for creating domain specific languages (EDSLs), Template Haskell allows a user to embed programs written in another programming language inside of a Haskell program. This enables writing parts of the program in the concrete, domain specific syntax of a different programming language. It has the benefit to think about -- and to express -- domain specific problems in the language best suited for the task. In particular, it lets a user focus on the domain specific problem and removes all additional language burdens induced by inconvenient syntax, unsuited control constructs, etc. Programs from the embedded language are parsed and translated into corresponding (but syntactically heavier) Haskell code at compile-time by Template Haskell. In this sense, (e.g.,) the shakespearean template languages from the <code>shakespeare</code> library use Template Haskell at their core. They expose succinct domain specific languages to write HTML, CSS, and Javascript code inside of a Haskell based web application.<br />
<br />
= Template Haskell by Examples =<br />
<br />
In this section, we will review the Template Haskell features to write meta programs. The first set of examples show-cases Template Haskell's potential as a code generator; the second set of examples highlights its facilities to create embedded domain specific languages (EDSLs). All examples require GHC's language extension <code>TemplateHaskell</code> to be enabled.<br />
<br />
To avoid confusion in the sequel, we distinguish between meta programs and object programs. Meta programs are the Haskell programs that run at compile-time and which generate Template Haskell object programs as the results of their execution; they are the programs that devise or manipulate other programs by some algorithmic means. Object programs, on the other hand, are the Template Haskell programs manipulated and built by the Haskell meta programs at compile-time.<br />
<br />
== Template Haskell as a Code Generator ==<br />
<br />
As an introductory example, consider Haskell's <code>Prelude</code> function <hask>curry :: ((a,b) -> c) -> a -> b -> c</hask>, which converts a function taking a pair to its curried equivalent. Unfortunately, there are no <code>Prelude</code> functions that provide the same currying functionality for functions taking arbitrary <math>n</math>-tuples. Moreover, having to write more than a few of these functions manually is, while trivial, a very repetitive and cumbersome task. Instead we wish to generate needed <hask>curry3</hask>, <hask>curry5</hask>, or <hask>curry8</hask> functions through a single meta program on demand. Template Haskell let's us do just this. The idea is to write a meta function <hask>curryN :: Int -> Q Exp</hask> which, given a number <code>n >= 1</code>, ''constructs the source code'' for an <math>n</math>-ary <hask>curry</hask> function:<br />
<br />
<haskell><br />
<br />
{-# LANGUAGE TemplateHaskell #-}<br />
import Control.Monad<br />
import Language.Haskell.TH<br />
<br />
curryN :: Int -> Q Exp<br />
curryN n = do<br />
f <- newName "f"<br />
xs <- replicateM n (newName "x")<br />
let args = map VarP (f:xs)<br />
ntup = TupE (map VarE xs)<br />
return $ LamE args (AppE (VarE f) ntup)<br />
<br />
</haskell><br />
<br />
Meta function <hask>curryN</hask> builds a lambda abstraction <hask>LamE</hask> that pattern matches against a function <hask>f</hask> and <math>n</math> argument variables <hask>x1</hask>, <hask>x2</hask>, ..., <hask>xn</hask>; in its body, it then applies function <hask>f</hask> to the <math>n</math>-tuple <hask>(x1, x2, ..., xn)</hask> derived from the pattern matched variables. The names used to refer to the variables <hask>f</hask> and <hask>x1</hask> through <hask>xn</hask> are generated monadically by function <hask>newName :: String -> Q Name</hask> to always generate fresh names not used anywhere else. Hence, the value returned by <hask>curryN</hask> is a monadic computation of type <code>Q Exp</code>. When executed, this monadic computation yields an expression <code>Exp</code> representing the object program of an <math>n</math>-ary curry function. For example, <hask>(curryN 3)</hask> returns a monadic computation that yields an expression representing the object program of the <hask>curry3</hask> function of type <hask>((a, b, c) -> d) -> a -> b -> c -> d</hask> in abstract syntax.<br />
<br />
To run a meta program like <hask>curryN</hask> at compile-time, we enclose it with Template Haskell's ''splice'' operator <code>$</code> by writing (e.g.,) <hask>$(curryN 3)</hask>. This evaluates the meta program <code>curryN 3</code> and puts the resulting object program <hask>\f x1 x2 x3 -> f (x1, x2, x3)</hask> in place of the splice. In general, the splice operator <code>$</code> can be applied to any monadic <code>Q</code> computation, hereby performing this computation at compile-time and inserting the resulting object program as real Haskell code. To ensure type safety, meta programs to be run are type checked beforehand to indeed yield a valid Template Haskell object program. To this end only imported, fully-typechecked meta programs can be run via the splice operator <code>$</code>: in particular, we have to evaluate the meta program <code>$(curryN 3)</code> in a separate module to where <code>curryN</code> is defined.<br />
<br />
To generate function declarations for the first <math>n</math> curry functions, we can devise a further meta program on top of <hask>curryN</hask> as follows:<br />
<br />
<haskell><br />
genCurries :: Int -> Q [Dec]<br />
genCurries n = for [1..n] mkCurryDec<br />
where mkCurryDec ith = do<br />
cury <- curryN ith<br />
let name = mkName $ "curry" ++ show ith<br />
return $ FunD name [Clause [] (NormalB cury) []]<br />
</haskell><br />
<br />
Running <hask>$(genCurries 20)</hask> will then splice in the first 20 curry functions at compile-time, namely:<br />
<br />
<haskell><br />
curry1 = \ f x1 -> f (x1)<br />
curry2 = \ f x1 x2 -> f (x1, x2)<br />
curry3 = \ f x1 x2 x3 -> f (x1, x2, x3)<br />
curry4 = \ f x1 x2 x3 x4 -> f (x1, x2, x3, x4)<br />
...<br />
curry20 = \ f x1 x2 ... x20 -> f (x1, x2, ..., x20)<br />
</haskell><br />
<br />
Note that in this case, <hask>genCurries</hask> returns a list of top-level function declarations that bind the anonymous lambda abstractions built by <hask>curryN</hask>. Also, to name the function bindings, we use function <hask>mkName :: String -> Name</hask> instead of <hask>newName :: String -> Q Name</hask>. The reason is that here we want to generate functions <hask>curry1</hask> to <hask>curry20</hask> with exactly the prescribed names, so they can be captured and referred to from other parts of the program.<br />
<br />
Evaluating Haskell (meta) programs at compile-time and splicing in the generated object programs as regular Haskell code is the first central building block of Template Haskell. The two other core mechanisms are exhibited by the implementations of <hask>curryN</hask> and <hask>genCurries</hask>: algebraic data types and the quotation monad <code>Q</code>.<br />
<br />
First, object programs created by Template Haskell are represented as regular algebraic data types, describing a program in the form of an abstract syntax tree. The Template Haskell library provides algebraic data types <code>Exp</code>, <code>Pat</code>, <code>Dec</code>, and <code>Type</code> to represent Haskell's surface syntax of expressions, patterns, declarations, and types, respectively. Virtually every concrete Haskell syntactic construct has a corresponding abstract syntax constructor in one of the four ADTs. Furthermore, all Haskell identifiers are represented by the abstract <code>Name</code> data type. By representing object programs as regular algebraic data types (and thus as data), normal Haskell can be used as the meta programming language to build object programs.<br />
<br />
Second, TH object programs are built inside the quotation monad <code>Q</code>. This monad is performed by the splice operator "<code>$</code>" at compile-time as part of evaluating the meta program. In the examples so far, the <code>Q</code> monad was only needed to provide fresh identifiers with function <hask>newName :: String -> Q Name</hask> for the generated Haskell expressions. The other main feature that requires a monadic construction of object programs is ''reification'', which allows to query compile-time information during the object program's construction. We will explain reification in detail later.<br />
<br />
Thus, Template Haskell's core functionality constitutes evaluating object programs with "<code>$</code>" and building them from algebraic data types inside the quotation monad <code>Q</code>. However, constructing object programs in terms of their abstract syntax trees is quite verbose and leads to clumsy meta programs. Therefore the Template Haskell API also provides two further interfaces to build object programs more conveniently: ''syntax construction functions'' and ''quotation brackets''.<br />
<br />
Syntax construction functions directly relate to the syntax constructors from the algebraic data types <code>Exp</code>, <code>Pat</code>, <code>Dec</code>, and <code>Type</code> for representing Haskell code. However, they hide the monadic nature of building object programs. For example, recall our definition of the <hask>genCurries</hask> meta function from above:<br />
<br />
<haskell><br />
genCurries :: Int -> Q [Dec]<br />
genCurries n = for [1..n] mkCurryDec<br />
where mkCurryDec ith = do<br />
cury <- curryN ith<br />
let name = mkName $ "curry" ++ show ith<br />
return $ FunD name [Clause [] (NormalB cury) []]<br />
</haskell><br />
<br />
To use the object program generated by the sub call to <hask>curryN</hask> in the larger context of the returned function declaration, we have to first perform <hask>curryN</hask> and bind its result to <hask>cury</hask>. The reason is that we have to account for <hask>curryN</hask>'s generation of fresh names before we can continue. Using syntax construction functions instead of data constructors, however, abstracts from the monadic construction of <hask>genCurries</hask>, thus making its code a little shorter:<br />
<br />
<haskell><br />
genCurries :: Int -> Q [Dec]<br />
genCurries n = for [1..n] mkCurryDec<br />
where mkCurryDec ith = funD name [clause [] (normalB (curryN ith)) []]<br />
where name = mkName $ "curry" ++ show ith<br />
</haskell><br />
<br />
The new <code>funD</code>, <code>clause</code>, and <code>normalB</code> functions directly correspond to the formerly used <code>FunD</code>, <code>Clause</code>, and <code>NormalB</code> constructors. The only difference lies in their types:<br />
<br />
{|<br />
|-<br />
|<hask>FunD :: Name -> [Clause] -> Dec|</hask><br />
|<hask>funD :: Name -> [Q Clause] -> Q Dec</hask><br />
|-<br />
|<hask>Clause :: [Pat] -> Body -> Clause</hask><br />
|<hask>clause :: [Q Pat] -> Q Body -> Q Clause</hask><br />
|-<br />
|<hask>NormalB :: Exp -> Body</hask><br />
|<hask>normalB :: Q Exp -> Q Body</hask><br />
|}<br />
<br />
While the syntax constructors work with raw TH expressions, the syntax construction functions expect their monadic counterparts. They construct a TH object program directly in <code>Q</code>, thus freeing the API consumer from doing the monadic wrapping and unwrapping manually. For every syntax constructor, there is a corresponding monadic syntax construction function provided.<br />
<br />
On top of syntax construction functions, quotation brackets are a further shortcut for representing Haskell code. They allow to specify an object program using just regular Haskell syntax by enclosing it inside oxford brackets <code>[| .. |]</code>. That way, object programs can be specified yet much more succinctly. For example, a meta program building a Haskell expression for the identity function is still quite verbose, if expressed with either ADTs or syntax construction functions:<br />
<br />
<br />
<haskell><br />
genId :: Q Exp<br />
genId = do<br />
x <- newName "x"<br />
lamE [varP x] (varE x)<br />
</haskell><br />
<br />
<br />
Using quotation brackets, writing the same meta program can be abbreviated much further as:<br />
<br />
<haskell><br />
genId' :: Q Dec<br />
genId' = [| \x -> x |]<br />
</haskell><br />
<br />
Quotation brackets quote regular Haskell code as the corresponding object program fragments inside the <code>Q</code> monad. There are quotation brackets for quoting Haskell expressions (<code>[e| .. |]|</code>), patterns (<code>[p| .. |]</code>), declarations (<code>[d| .. |]</code>), and types (<code>[t| .. |]</code>). Writing <code>[| .. |]</code> is hereby just another way of saying <code>[e| .. |]</code>. Using quotation brackets in a sense ''lifts'' Haskell's concrete syntax into corresponding object program expressions inside the <code>Q</code> monad. By doing so, quotation brackets represent the dual of the already introduced splice operator <code>$</code>: Evaluating a meta program with "<code>$</code>" splices in the generated object program as real Haskell code; in contrast, quotation brackets <code>[| .. |]</code> turn real Haskell code into an object program. Consequently, quotation brackets and the splice operator cancel each other out. The equation <code>$([| e |]) = e</code> holds for all expressions <code>e</code> and similar equations hold for declarations, and types<ref name="th2" />.<br />
<br />
In addition, there is support for quoting Haskell (value and type) identifiers as corresponding <code>Name</code>s inside Template Haskell. This allows to refer to regular Haskell identifiers from within TH object programs. For example, writing <hask>'genId</hask> yields a TH <code>Name</code> referring to the <hask>genId</hask> identifier. Similarly, <hask>''Q</hask> gives a <code>Name</code> referring to the <code>Q</code> type identifier.<br />
<br />
=== Generic Maps ===<br />
<br />
As a second example that uses both syntax construction functions as well as quotation brackets, let's consider a meta program <hask>mapN :: Int -> Q Dec</hask> to build "generic" <hask>map</hask> functions at compile-time. Invoking <hask>$(mapN 1)</hask> should generate the well-known standard function <hask>map :: (a -> b) -> [a] -> [b]</hask>; evaluating <hask>$(mapN 2)</hask> should splice in a binary map function of type <hask>(a -> b -> c) -> [a] -> [b] -> [c]</hask>, and so on.<ref>Note that <math>n</math>-ary maps are better written using Applicative Functors and <code>ZipList</code>s. For understanding Template Haskell as a code generator, this example is still useful though.</ref><br />
<br />
<haskell><br />
mapN :: Int -> Q Dec<br />
mapN n<br />
| n >= 1 = funD name [cl1, cl2]<br />
| otherwise = fail "mapN: argument n may not be <= 0."<br />
where<br />
name = mkName $ "map" ++ show n<br />
cl1 = do f <- newName "f"<br />
xs <- replicateM n (newName "x")<br />
ys <- replicateM n (newName "ys")<br />
let argPatts = varP f : consPatts<br />
consPatts = [ [p| $(varP x) : $(varP ys) |]<br />
| (x,ys) <- xs `zip` ys ]<br />
apply = foldl (\ g x -> [| $g $(varE x) |])<br />
first = apply (varE f) xs<br />
rest = apply (varE name) (f:ys)<br />
clause argPatts (normalB [| $first : $rest |]) []<br />
cl2 = clause (replicate (n+1) wildP) (normalB (conE `[])) []<br />
</haskell><br />
<br />
The implementation of <hask>mapN</hask> is very much in the spirit of meta function <hask>curryN</hask> from the first example. For instance, evaluating splice <hask>$(mapN 3)</hask> splices in the following map function at compile-time:<br />
<br />
<haskell><br />
map3 f (x:xs) (y:ys) (z:zs) = f x y z : map3 f xs ys zs<br />
map3 _ _ _ _ = []<br />
</haskell><br />
<br />
Nonetheless, meta function <hask>mapN</hask> exhibits a couple of new Template Haskell features: First, quotation brackets and splices are used in several places to abbreviate the object program construction. For example, helper definition <hask>apply</hask> used to generate <hask>map3</hask>'s body <hask>f x y z : map3 f xs ys zs</hask> shows the use of quotation brackets; it also highlights how splicing (<hask>$</hask>) and quotes (<hask>[| .. |]</hask>) cancel each other out. Second, identifier quotes (namely, <hask>'[]</hask>) are used to create an object program <code>Name</code> that refers to Haskell's built-in list constructor <hask>[]</hask>. Third, the example advertises how all three APIs for building Template Haskell object programs can be interleaved. The lowermost verbose API of building a raw TH data value inside the quotation monad <code>Q</code> can be abbreviated, where possible, with syntax constructor functions and quotation brackets.<br />
<br />
Lastly, the <hask>mapN</hask> example exemplifies how Haskell's static scoping is extended to object programs. The scoping principle for object programs is just as in normal Haskell: Identifiers are bound to their lexically enclosing binders in scope at the point the object program is ''defined''. Quotation brackets and splices don't alter static scopes, even though splices may bring an object program into scope at a location, where a conflicting closure is present. For example, consider this snippet:<br />
<br />
<haskell><br />
x :: Int<br />
x = 42<br />
<br />
static :: Q Exp<br />
static = [| x |]<br />
<br />
plus42 :: Int -> Int<br />
plus42 x = $static + x<br />
</haskell><br />
<br />
Here the occurrence of <hask>x</hask> in <hask>static</hask> refers to the global identifier <hask>x</hask> that is lexically in scope during its definition. Splicing in <hask>static</hask> into a different scope later where a different local <hask>x</hask> is present (i.e., <hask>plus42</hask>'s local identifier <hask>x</hask>), doesn't alter the link between <hask>static</hask>'s <hask>x</hask> and the global identifier <hask>x</hask>.<br />
<br />
The only exception to static scoping in Template Haskell are the names generated by <hask>mkName :: String -> Name</hask>. These names implement dynamic scoping and \textit{can} be captured in spliced-in code. Changing the previous snippet to<br />
<br />
<haskell><br />
x :: Int<br />
x = 42<br />
<br />
dynamic :: Q Exp<br />
dynamic = VarE (mkName "x")<br />
<br />
times2 :: Int -> Int<br />
times2 x = $dynamic + x<br />
</haskell><br />
<br />
results in the identifier <hask>x</hask> spliced in by <hask>$dynamic</hask> to be bound to the closest <hask>x</hask> in scope. Hence, its binder is <hask>times2</hask>'s local identifier <hask>x</hask> and ''not'' the global <hask>x</hask>.<br />
<br />
=== Reification ===<br />
<br />
The final major Template Haskell feature not yet described is program ''reification''. Briefly, reification allows a meta program to query compile-time information about other program parts while constructing the object program. It allows the meta program to inspect other program pieces to answer questions such as: "what's this variable's type?", "what are the class instances of this type class?", or "which constructors does this data type have and and how do they look like?". The main use case is to generate boilerplate code which ''auto-completes'' manually written code. A prime example is to generically derive type class instances from bare data type definitions.<br />
<br />
Suppose we've defined the following polymorphic data types for representing potentially erroneous values, lists, and binary trees, respectively:<br />
<br />
<haskell><br />
data Result e a = Err e | Ok a<br />
data List a = Nil | Cons a (List a)<br />
data Tree a = Leaf a | Node (Tree a) a (Tree a)<br />
</haskell><br />
<br />
Moreover, suppose we want to derive <code>Functor</code> instances for all of these types. Deriving these instances manually is straightforward, but writing them all out by hand is quite cumbersome. Especially since writing a <code>Functor</code> instance follows the same pattern across all of the above types and in fact any type <code>T a</code>.<br />
<br />
To make a type constructor <code>T</code> an instance of <code>Functor</code>, one needs to implement method <hask>fmap :: (a -> b) -> T a -> T b</hask>. Its definition is hereby precisely determined by parametricity and the functor laws: By parametricity, all values of type <code>a</code> must be replaced according to the provided function with values of type <code>b</code>. Furthermore, by the functor laws, all other shapes of the input value of type <code>T a</code> must be preserved when transforming it to the output value of type <code>T b</code>.<br />
<br />
Meta function <hask>deriveFunctor :: Name -> Q [Dec]</hask> below implements the idea of this algorithm:<br />
<br />
<haskell><br />
data Deriving = Deriving { tyCon :: Name, tyVar :: Name }<br />
<br />
deriveFunctor :: Name -> Q [Dec]<br />
deriveFunctor ty<br />
= do (TyConI tyCon) <- reify ty<br />
(tyConName, tyVars, cs) <- case tyCon of<br />
DataD _ nm tyVars cs _ -> return (nm, tyVars, cs)<br />
NewtypeD _ nm tyVars c _ -> return (nm, tyVars, [c])<br />
_ -> fail "deriveFunctor: tyCon may not be a type synonym."<br />
<br />
let (KindedTV tyVar StarT) = last tyVars<br />
instanceType = conT ``Functor `appT`<br />
(foldl apply (conT tyConName) (init tyVars))<br />
<br />
putQ $ Deriving tyConName tyVar<br />
sequence [instanceD (return []) instanceType [genFmap cs]]<br />
where<br />
apply t (PlainTV name) = appT t (varT name)<br />
apply t (KindedTV name _) = appT t (varT name)<br />
</haskell><br />
<br />
Given the name of a type constructor (e.g. <code>Result</code>, <code>List</code>, etc.), <hask>deriveFunctor</hask> derives the code for this type constructor's <code>Functor</code> instance. For example, running the splice <hask>$(deriveFunctor ''Tree)</hask> generates the following code:<br />
<br />
<haskell><br />
instance Functor Tree where<br />
fmap f (Leaf x) = Leaf (f x)<br />
fmap f (Node l x r) = Node (fmap f l) (f x) (fmap f r)<br />
</haskell><br />
<br />
Most notably, meta function <hask>deriveFunctor</hask> shows reification in action. It calls function <hask>reify :: Name -> Q Info</hask> on the input type constructor's name to yield information about this data type's definition. Using <hask>reify</hask>, it thus learns whether the data type was defined using the <code>data</code> or <code>newtype</code> keyword, which constructors it defines and what their shapes are. Based on the learned structure, <hask>deriveFunctor</hask> is then able to generate a suitable definition of <hask>fmap</hask> and its different clauses via the auxiliaries <hask>genFmap</hask>, <hask>genFmapClause</hask>, and <hask>newField</hask>, defined below. These auxiliary definitions generate one <hask>fmap</hask> clause for each of the data type's constructors. And each clause then transforms its constructor by recursively modifying all of the constructor's fields of type <code>a</code> through <hask>fmap</hask>'s function <code>f</code>, while retaining all other shapes.<br />
<br />
<haskell><br />
genFmap :: [Con] -> Q Dec<br />
genFmap constructors<br />
= do funD `fmap (map genFmapClause constructors)<br />
<br />
genFmapClause :: Con -> Q Clause<br />
genFmapClause (NormalC name fieldTypes)<br />
= do f <- newName "f"<br />
fieldNames <- replicateM (length fieldTypes) (newName "x")<br />
<br />
let pats = varP f:[conP name (map varP fieldNames)]<br />
body = normalB $ appsE $<br />
conE name : map (newField f) (zip fieldNames fieldTypes)<br />
<br />
clause pats body []<br />
<br />
newField :: Name -> (Name, StrictType) -> Q Exp<br />
newField f (x, (_, fieldType))<br />
= do Just (Deriving typeCon typeVar) <- getQ<br />
case fieldType of<br />
VarT typeVar' | typeVar' == typeVar -><br />
[| $(varE f) $(varE x) |]<br />
ty `AppT` VarT typeVar' |<br />
leftmost ty == (ConT typeCon) && typeVar' == typeVar -><br />
[| fmap $(varE f) $(varE x) |]<br />
_ -> [| $(varE x) |]<br />
<br />
leftmost :: Type -> Type<br />
leftmost (AppT ty1 _) = leftmost ty1<br />
leftmost ty = ty<br />
</haskell><br />
<br />
In an analogous manner to <hask>deriveFunctor</hask>, a function <hask>deriveFoldable :: Name -> Q [Dec]</hask> can be devised to derive a data type's <code>Foldable</code> instance. All that is needed is to provide a definition for function <hask>foldMap :: Monoid m => (a -> m) -> T a -> m</hask>. Again, <hask>foldMap</hask>'s definition follows directly from a data type's bare definition, which can be observed by means of reification. This highlights particularly how the functionality offered by Template Haskell provides a low-level API into the GHC compiler to manipulate abstract syntax trees at compile-time. This mechanism is quite powerful and even allows to simulate some of GHC's offered language extensions, e.g., <code>-XDeriveFunctor</code> and <code>-XDeriveFoldable</code>, to be implemented as a library on top of Template Haskell.<br />
<br />
== Template Haskell for building Embedded Domain specific Languages (EDSLs) ==<br />
<br />
To see Template Haskell's potential for building an EDSL, consider the problem of pattern matching text with regular expressions. Suppose, as part of a Haskell program we need to devise many different regular expressions and use them to pattern match text fragments. Regular expressions are easily defined by an algebraic data type capturing their structure, as well as an evaluator checking whether a regular expression matches some input string. <ref>This example draws on Penn's CIS 552 ''Advanced Programming'' course, specifically Assignment 5: http://www.seas.upenn.edu/~cis552/current/hw/hw05/Main.html.</ref><br />
<br />
<haskell><br />
data RegExp<br />
= Char (Set Char) -- [a], [abc], [a-z]; matches a single character from the specified class<br />
| Alt RegExp RegExp -- r1 | r2 (alternation); matches either r1 or r2<br />
| Seq RegExp RegExp -- r1 r2 (concatenation); matches r1 followed by r2<br />
| Star RegExp -- r* (Kleene star); matches r zero or more times<br />
| Empty -- matches only the empty string<br />
| Void -- matches nothing (always fails)<br />
| Var String -- a variable holding another regexp (explained later)<br />
deriving Show<br />
<br />
match :: RegExp -> String -> Bool<br />
match r s = nullable (foldl deriv r s)<br />
</haskell><br />
<br />
The evaluator <hask>match</hask> is hereby based on the concept of derivatives<ref name="regexp-derivs" />: an initial regular expression <hask>r</hask> matches an input string <hask>s</hask>, if <hask>r</hask> matches the first character of <hask>s</hask> and its derivative regular expression <hask>(deriv r)</hask> matches the remainder of <hask>s</hask>:<br />
<br />
<haskell><br />
nullable :: RegExp -> Bool<br />
nullable (Char _) = False<br />
nullable (Alt r1 r2) = nullable r1 || nullable r2<br />
nullable (Seq r1 r2) = nullable r1 && nullable r2<br />
nullable (Star _) = True<br />
nullable Empty = True<br />
nullable Void = False<br />
nullable (Var _) = False<br />
<br />
deriv :: RegExp -> Char -> RegExp<br />
deriv (Char cs) c<br />
| c `Set.member` cs = Empty<br />
| otherwise = Void<br />
deriv (Alt r1 r2) c = Alt (deriv r1 c) (deriv r2 c)<br />
deriv (Seq r1 r2) c<br />
| nullable r1 = Alt (Seq (deriv r1 c) r2) (deriv r2 c)<br />
| otherwise = Seq (deriv r1 c) r2<br />
deriv (Star r) c = deriv (Alt Empty (Seq r (Star r))) c<br />
deriv Empty _ = Void<br />
deriv Void _ = Void<br />
deriv (Var _) _ = Void<br />
</haskell><br />
<br />
The <code>RegExp</code> data type and the <hask>match</hask> function solve the initially posed problem of providing regular expressions in Haskell. However, specifying regular expressions in abstract syntax is extremely tedious. For example, consider defining a regular expression for checking the wellformedness of email addresses ending with the top level domain '''.com'''. In its usual concrete syntax, such a regular expression is easily defined as <code>([a-z]|[0-9])*@@([a-z]|[0-9])*.com</code>, but writing it in terms of the <code>RegExp</code> dataype is verbose and unintuitive. Moreover, parsing functions like<br />
<br />
* <hask>compile :: String -> RegExp</hask>, or<br />
* <hask>compile' :: String -> Either CompileError RegExp</hask><br />
<br />
do not remedy the problem of working with regular expressions in concrete syntax. Due to "compiling" regular expressions at run time, they don't provide any compile-time type-safety guarantees that the input raw expression is wellformed; thus they lead to either run time exceptions for illformed regular expressions (e.g., <hask>compile</hask>) or induce a tedious handling for compiled regexes (e.g., <hask>compile'</hask>).<br />
<br />
To preserve type safety and yet to be able to use regular expressions conveniently, we want to embed the concrete regular expression syntax into the Haskell host language. This can be done via Template Haskell's quasi quotes and furthermore enabling the <code>QuasiQuotes</code> extension. This allows defining ''quasi quotes'' for regular expressions, denoted <code>|[regex| .. |]</code>, where anything inside the quasi quote is considered part of an embedded regular expression language. Using quasi quotes, we can then specify the regex for email addresses from above naturally as follows:<br />
<br />
<haskell><br />
validDotComMail :: RegExp<br />
validDotComMail = [regex|([a-z]|[0-9])*@([a-z]|[0-9])*.com|]<br />
</haskell><br />
<br />
We can even compose regular expressions easily from smaller building blocks:<br />
<br />
<haskell><br />
alphaNum, validDotComMail' :: RegExp<br />
alphaNum = [regex|[a-z]|[0-9]|]<br />
validDotComMail' = [regex|${alphaNum}*@${alphaNum}*.com|]<br />
</haskell><br />
<br />
Writing <hask>${alphaNum}</hask> interpolates the regex referred to by <hask>alphaNum</hask> into the larger regex <hask>validDotComMail'</hask>. In essence, this means that we can define our own notion of splicing values from the Haskell meta language into the embedded object language of regular expressions. We can go further and even allow to run Haskell code when interpolating with <code>${..}</code>. For example, refining our wellformedness check for '''.com''' mail addresses, we might want to ensure at least one character to occur on either side of the "@" symbol:<br />
<br />
<haskell><br />
chars, validDotComMail'' :: RegExp<br />
chars = [regex|[a-z]|[A-Z]|[0-9]|[-_.]|]<br />
validDotComMail'' = [regex|${plus chars}@${plus chars}.com|]<br />
<br />
plus :: RegExp -> RegExp<br />
plus r = Seq r (Star r)<br />
</haskell><br />
<br />
Here, <hask>plus</hask> corresponds to the usual regex combinator that requires a given regex to occur at least once. Note how <hask>plus</hask> is defined as a regular Haskell function and then used ''inside'' of the embedded regex language to build the regular expression for <hask>validDotComMail''</hask>.<br />
<br />
Intuitively, a quasi quote like <code>[regex| .. |]</code> converts an embedded language's concrete syntax to Haskell code at compile-time. It is defined by a ''quasi quoter'', which is a parser for the embedded language. Its task is to parse the embedded language's syntax into a corresponding Template Haskell expression and then to splice this expression as real Haskell code in place of the quasi quote. The conversion of embedded language code to corresponding Haskell code hereby happens before typechecking the Haskell module. Hence, trying to splice in malformed embedded language fragments will raise a Haskell type error at compile-time.<br />
<br />
The quasi quoter <code>regex</code> for our embedded language of regular expressions can be defined as follows:<br />
<br />
<haskell><br />
regex :: QuasiQuoter<br />
regex = QuasiQuoter {<br />
quoteExp = compile<br />
, quotePat = notHandled "patterns"<br />
, quoteType = notHandled "types"<br />
, quoteDec = notHandled "declarations"<br />
}<br />
where notHandled things = error $<br />
things ++ " are not handled by the regex quasiquoter."<br />
<br />
compile :: String -> Q Exp<br />
compile s =<br />
case P.parse regexParser "" s of<br />
Left err -> fail (show err)<br />
Right regexp -> [e| regexp |]<br />
</haskell><br />
<br />
That is, formally a <code>QuasiQuoter</code> consists of four parsers,<br />
<br />
<haskell><br />
quoteExp :: String -> Q Exp<br />
quotePat :: String -> Q Pat<br />
quoteType :: String -> Q Type<br />
quoteDec :: String -> Q Dec<br />
</haskell><br />
<br />
to parse raw strings of the embedded language into the different categories of Haskell syntax. In this example, however, we only want to splice embedded regular expressions into the context of Haskell expressions, so we only define the <code>quoteExp</code> parser in the <code>regex</code> quasi quoter. This parser compiles an embedded regular expression given as a string into a corresponding Template Haskell expression.<br />
<br />
Compilation by the <hask>compile</hask> function proceeds in two stages: First, we parse the input string regex into a corresponding <code>RegExp</code> value. Second, we encode this <code>RegExp</code> value as a Haskell expression in Template Haskell's <code>Q Exp</code> type. It is the second step that allows us to interpolate variables (or even code) from the Haskell host language into the EDSL for regular expressions.<br />
<br />
Parsing a raw regular expression into a corresponding <code>RegExp</code> value is a routine task using (e.g.) the ''parsec'' library:<br />
<br />
<haskell><br />
regexParser :: Parsec String () RegExp<br />
regexParser = alts <* eof where<br />
atom = try var <|> char<br />
var = Var <$> (string "${" *> many1 (noneOf "}") <* P.char '}')<br />
char = charclass <|> singlechar<br />
singlechar = (Char . Set.singleton) <$> noneOf specials<br />
charclass = fmap (Char . Set.fromList) $<br />
P.char '[' *> content <* P.char ']'<br />
content = try (concat <$> many1 range)<br />
<|> many1 (noneOf specials)<br />
range = enumFromTo<br />
<$> (noneOf specials <* P.char '-')<br />
<*> noneOf specials<br />
alts = try (Alt <$> seqs <*> (P.char '|' *> alts)) <|> seqs<br />
seqs = try (Seq <$> star <*> seqs) <|> star<br />
star = try (Star <$> (atom <* P.char '*'))<br />
<|> try (Star <$> (P.char '(' *> alts <* string ")*"))<br />
<|> atom<br />
specials = "[]()*|"<br />
</haskell><br />
<br />
To represent regular expressions of type <code>RegExp</code> as Template Haskell expressions of type <code>Q Exp</code>, Template Haskell's <code>Lift</code> typeclass is used. Its method <hask>lift :: Lift a => a -> Q Exp</hask> lifts values from the Haskell meta language (e.g., a <code>RegExp</code> value) into Template Haskell's expression language (i.e., a <code>Q Exp</code> value). The <hask>lift</hask> function is implicitly invoked by quote <hask>[e| regexp |]</hask> in function <hask>compile</hask>.<br />
<br />
Most of the lifting is a direct encoding of the syntactic structure of the <code>RegExp</code> value; the only interesting case is when lifting the regular expression variable <hask>Var vars</hask>. In this case, we treat the words in the string <hask>vars</hask> as referring to identifiers from the Haskell host language, which we apply in a left associative manner to each other. Doing this enables interpolation of Haskell identifiers or even simple forms of Haskell expressions into our EDSL of regular expressions as shown by the regexes <hask>validDotComMail'</hask>, and <hask>validDotComMail''</hask> above.<br />
<br />
<haskell><br />
instance Lift a => Lift (Set a) where<br />
lift set = appE (varE `Set.fromList) (lift (Set.toList set))<br />
<br />
instance Lift RegExp where<br />
-- lift :: RegExp -> Q Exp<br />
lift (Char cs) = apply `Char [lift cs]<br />
lift (Alt r1 r2) = apply `Alt (map lift [r1, r2])<br />
lift (Seq r1 r2) = apply `Seq (map lift [r1, r2])<br />
lift (Star r1) = apply `Star (map lift [r1])<br />
lift Empty = apply `Empty []<br />
lift Void = apply `Void []<br />
lift (Var vars) = foldl1 appE $ map (varE . mkName) (words vars)<br />
<br />
apply :: Name -> [Q Exp] -> Q Exp<br />
apply n = foldl appE (conE n)<br />
</haskell><br />
<br />
These two steps constitute the conversion of raw string regular expressions into Template Haskell expressions inside of the <hask>compile</hask> function and define the <code>regex</code> quasiquoter. Whenever we write a quasi quote like <hask>[regex| .. |]</hask> in a Haskell expression context, <hask>regex</hask>'s parser <hask>quoteExp</hask> converts the regex EDSL into a Template Haskell expression <code>Q Exp</code> and splices in the result as a wellformed <code>RegExp</code> value. This example shows how Template Haskell and quasi quotes can be used to define a type-safe, domain specific language for regular expressions.<br />
<br />
=== Shakespearean Templates ===<br />
<br />
In much the same manner as in the last example, Template Haskell and quasi quotes are used in Michael Snoyman's <code>shakespeare</code> library<ref name="shakespeare" /><ref name="shakespeare-lib" />. It defines embedded templating languages for working with the internet's web languages from within a Haskell web application. In particular, the <code>shakespeare</code> library provides the template languages ''Hamlet'', ''Cassius'', and ''Julius'' for writing embedded HTML, CSS, and Javascript code, respectively. All three templating languages internally work quite similarly to the previous example's EDSL for regular expressions: quasi quotes allow one to write HTML, CSS, or JavaScript code in concrete (though slightly modified) syntax inside of Haskell. Moreover, identifiers from the Haskell host language as well as code fragments can be interpolated into the template languages at compile-time. In the remainder we will briefly show-case the <code>shakespeare</code> library's templating language Hamlet for creating HTML documents; the other templating languages Cassius and Julius are similar.<br />
<br />
To create and output a simple web page from inside a Haskell application, the following is enough:<br />
<br />
<haskell><br />
import Data.Text<br />
import Text.Hamlet<br />
import Text.Blaze.Html.Renderer.String<br />
<br />
data Page = Home | About | Github<br />
<br />
mkUrls :: Page -> [(Text, Text)] -> Text<br />
mkUrls Home _ = "/home.html"<br />
mkUrls About _ = "/about.html"<br />
mkUrls Github _ = "https://www.github.com/bollmann"<br />
<br />
webPage :: Text -> Text -> HtmlUrl Page<br />
webPage title content = [hamlet|<br />
<html><br />
<head><br />
<title>#{Text.toUpper title}<br />
<body><br />
<h1>#{title}<br />
<div>Welcome to my Shakespearean Templates page!<br />
<hr><br />
<div>Links:<br />
<ul><br />
<a href=@{Home}>My Homepage<br />
<a href=@{About}>About me<br />
<a href=@{Github}>Check out my Github<br />
<hr><br />
<div>#{content}<br />
|]<br />
<br />
main = putStrLn $ renderHtml $<br />
webPage "Hello Shakespeare!" "Hello World!" mkUrls<br />
</haskell><br />
<br />
Running this Haskell program, outputs an HTML page as specified by the Hamlet templating language, embedded through quasi quote <hask>[hamlet| .. |]</hask> in function <hask>webPage</hask>. Hamlet closely resembles real HTML syntax, but is even more terse: instead of a closing HTML tag, Hamlet uses indentation to indicate the span of the tag. Furthermore, Hamlet allows to interpolate code or identifiers from the Haskell host language when creating an HTML template. Interpolation of Haskell code into Hamlet is done by writing <code>#{ .. }</code>. In the above example, the HTML page's title and content are interpolated from Haskell identifiers. Note particularly how in the webpage's title we uppercase the interpolated title using Haskell's <hask>Text.toUpper</hask> function ''inside'' of the Hamlet language.<br />
<br />
In addition to this standard interpolation, Hamlet can also interpolate links by writing <code>@{..}</code>. These links are specified as values of the <hask>Page</hask> data type inside the template and the <hask>mkUrls</hask> render function translates them to real URLs later. Hamlet's URL interpolation has commonly be phrased as creating "type-safe URLs". One reason is that, just like with normal variable interpolation, all interpolated links have to exist and be type correct at compile-time; in this case, links must be values of the <code>Page</code> data type. Hence, as soon as a link's constructor shape is changed, the compiler statically forces us to update all references to this link as well. Furthermore, there is only one distinct place in the code to maintain or update a link's raw URL, thus minimizing the risk of dead URLs.<br />
<br />
For example, suppose we want to add more external links to our web page. We could model this fact by changing the <hask>Page</hask> data type to<br />
<br />
<haskell><br />
data Page = Home | About | External ExternalPage<br />
data ExternalPage = Github | Haskell | Reddit<br />
</haskell><br />
<br />
and, moreover, changing the <hask>mkUrls</hask> renderer function to account for the new links:<br />
<br />
<haskell><br />
mkUrls :: Page -> [(Text, Text)] -> Text<br />
mkUrls Home _ = "/home.html"<br />
mkUrls About _ = "/about.html"<br />
mkUrls (External page) _ = mkExternalUrls page<br />
<br />
mkExternalUrls :: ExternalPage -> Text<br />
mkExternalUrls Github = "https://www.github.com"<br />
mkExternalUrls Haskell = "http://www.haskell.org"<br />
mkExternalUrls Reddit = "http://www.reddit.com/r/haskell"<br />
</haskell><br />
<br />
Doing just these changes, will then cause a compile-time error in our <hask>webPage</hask> template, since we haven't updated the <hask>Github</hask> reference to our newly adjusted link structure. Hence, the compiler reminds (and in fact forces) us to update all locations in the code that used the old <hask>Github</hask> link to now use the new <hask>External Github</hask> (as well as optionally the <hask>External Haskell</hask>, etc.) links.<br />
<br />
Finally, Hamlet allows to use some control constructs like if conditionals, for loops, and let bindings to embed basic business logic into a webpage's template. Michael Snoyman gives a gentle (and much more in-depth) introduction to shakespearean templates and Yesod<ref name="shakespeare" /><ref name="yesod" />.<br />
<br />
= References =<br />
<br />
<references><br />
<ref name="th1">Tim Sheard and Simon Peyton Jones. Template Meta-Programming for Haskell. SIGPLAN Not., 37(12):60-75, December 2002.</ref><br />
<ref name="th2">Tim Sheard and Simon Peyton Jones. Notes on Template Haskell, Version 2. URL: http://research.microsoft.com/simonpj/papers/meta-haskell/notes2.ps, 2003.</ref><br />
<ref name="th3">Simon Peyton Jones. Major Proposed Revision of Template Haskell. URL: https://ghc.haskell.org/trac/ghc/blog/Template%20Haskell%20Proposal, 2010</ref><br />
<ref name="qq">Geoffrey Mainland. Why it's nice to be quoted: Quasiquoting for Haskell. In ''Proceedings of the ACM SIGPLAN Workshop on Haskell'', Haskell '07, pages 73-82, New York, NY, USA, 2007. ACM</ref><br />
<ref name="regexp-derivs">Janusz A. Brzozowski. Derivatives of regular expressions. J. ACM, 11(4):481–494, October 1964.</ref><br />
<ref name="shakespeare">Michael Snoyman. Shakespearean Templates. URL: http://www.yesodweb.com/book/shakespearean-templates [Accessed: May 2016].</ref><br />
<ref name="shakespeare-lib">Michael Snoyman. The <code>shakespeare</code> Haskell library. URL: http://hackage.haskell.org/package/shakespeare [Accessed: May 2016].</ref><br />
<ref name="yesod">Michael Snoyman. Haskell and Yesod. URL: http://www.yesodweb.com/book-1.4 [Accessed: May 2016].</ref><br />
</references></div>Dfeuerhttps://wiki.haskell.org/index.php?title=A_practical_Template_Haskell_Tutorial&diff=60775A practical Template Haskell Tutorial2016-05-14T22:04:28Z<p>Dfeuer: Use `for` instead of combining `sequence` with a list comprehension.</p>
<hr />
<div>This tutorial explores the Glasgow Haskell Compiler's compile-time meta programming in Template Haskell. It motivates use cases for meta programming and explains the different Template Haskell features on simple toy programs. The aim is to give an overview of Template Haskell's functionality in an example-driven manner.<br />
<br />
= Introduction =<br />
<br />
Template Haskell (TH) is the standard framework for doing type-safe, compile-time meta programming in the Glasgow Haskell Compiler (GHC). It allows writing Haskell meta programs, which are evaluated at compile-time, and which produce Haskell programs as the results of their execution.<br />
<br />
Template Haskell was conceived by Tim Sheard and Simon Peyton Jones<ref name="th1" /> by drawing on the ideas of Lisp macros, but in the typed setting of Haskell. Since then, the original implementation has evolved quite a bit<ref name="th2" /><ref name="th3" />. Most notably, in 2007 Geoffrey Mainland added support for quasi quoting<ref name="qq" />, which makes the embedding of domain specific languages into the Haskell host language much easier.<br />
<br />
As it exists today, Template Haskell has two main areas of application: Haskell code generation at compile-time and facilitating the embedding of domain specific languages.<br />
<br />
As a code generator, Template Haskell empowers a user to write many, syntactically different, programs all at once by means of a single meta program. All that is needed is a uniform, algorithmic description to create the different result programs. And the meta program then precisely implements the algorithm to compute all the different result programs as its output. This proves useful for example to avoid writing the same repetitive, boilerplate code over and over again. To this end, Template Haskell is used (among many others) in the <code>aeson</code> library to automatically derive a data type's <code>ToJSON</code> and <code>FromJSON</code> instances for JSON serialization; and in the <code>lens</code> library to mechanically create a data type's lenses.<br />
<br />
As a framework for creating domain specific languages (EDSLs), Template Haskell allows a user to embed programs written in another programming language inside of a Haskell program. This enables writing parts of the program in the concrete, domain specific syntax of a different programming language. It has the benefit to think about -- and to express -- domain specific problems in the language best suited for the task. In particular, it lets a user focus on the domain specific problem and removes all additional language burdens induced by inconvenient syntax, unsuited control constructs, etc. Programs from the embedded language are parsed and translated into corresponding (but syntactically heavier) Haskell code at compile-time by Template Haskell. In this sense, (e.g.,) the shakespearean template languages from the <code>shakespeare</code> library use Template Haskell at their core. They expose succinct domain specific languages to write HTML, CSS, and Javascript code inside of a Haskell based web application.<br />
<br />
= Template Haskell by Examples =<br />
<br />
In this section, we will review the Template Haskell features to write meta programs. The first set of examples show-cases Template Haskell's potential as a code generator; the second set of examples highlights its facilities to create embedded domain specific languages (EDSLs). All examples require GHC's language extension <code>TemplateHaskell</code> to be enabled.<br />
<br />
To avoid confusion in the sequel, we distinguish between meta programs and object programs. Meta programs are the Haskell programs that run at compile-time and which generate Template Haskell object programs as the results of their execution; they are the programs that devise or manipulate other programs by some algorithmic means. Object programs, on the other hand, are the Template Haskell programs manipulated and built by the Haskell meta programs at compile-time.<br />
<br />
== Template Haskell as a Code Generator ==<br />
<br />
As an introductory example, consider Haskell's <code>Prelude</code> function <hask>curry :: ((a,b) -> c) -> a -> b -> c</hask>, which converts a function taking a pair to its curried equivalent. Unfortunately, there are no <code>Prelude</code> functions that provide the same currying functionality for functions taking arbitrary <math>n</math>-tuples. Moreover, having to write more than a few of these functions manually is, while trivial, a very repetitive and cumbersome task. Instead we wish to generate needed <hask>curry3</hask>, <hask>curry5</hask>, or <hask>curry8</hask> functions through a single meta program on demand. Template Haskell let's us do just this. The idea is to write a meta function <hask>curryN :: Int -> Q Exp</hask> which, given a number <code>n >= 1</code>, ''constructs the source code'' for an <math>n</math>-ary <hask>curry</hask> function:<br />
<br />
<haskell><br />
<br />
{-# LANGUAGE TemplateHaskell #-}<br />
import Control.Monad<br />
import Language.Haskell.TH<br />
<br />
curryN :: Int -> Q Exp<br />
curryN n = do<br />
f <- newName "f"<br />
xs <- replicateM n (newName "x")<br />
let args = map VarP (f:xs)<br />
ntup = TupE (map VarE xs)<br />
return $ LamE args (AppE (VarE f) ntup)<br />
<br />
</haskell><br />
<br />
Meta function <hask>curryN</hask> builds a lambda abstraction <hask>LamE</hask> that pattern matches against a function <hask>f</hask> and <math>n</math> argument variables <hask>x1</hask>, <hask>x2</hask>, ..., <hask>xn</hask>; in its body, it then applies function <hask>f</hask> to the <math>n</math>-tuple <hask>(x1, x2, ..., xn)</hask> derived from the pattern matched variables. The names used to refer to the variables <hask>f</hask> and <hask>x1</hask> through <hask>xn</hask> are generated monadically by function <hask>newName :: String -> Q Name</hask> to always generate fresh names not used anywhere else. Hence, the value returned by <hask>curryN</hask> is a monadic computation of type <code>Q Exp</code>. When executed, this monadic computation yields an expression <code>Exp</code> representing the object program of an <math>n</math>-ary curry function. For example, <hask>(curryN 3)</hask> returns a monadic computation that yields an expression representing the object program of the <hask>curry3</hask> function of type <hask>((a, b, c) -> d) -> a -> b -> c -> d</hask> in abstract syntax.<br />
<br />
To run a meta program like <hask>curryN</hask> at compile-time, we enclose it with Template Haskell's ''splice'' operator <code>$</code> by writing (e.g.,) <hask>$(curryN 3)</hask>. This evaluates the meta program <code>curryN 3</code> and puts the resulting object program <hask>\f x1 x2 x3 -> f (x1, x2, x3)</hask> in place of the splice. In general, the splice operator <code>$</code> can be applied to any monadic <code>Q</code> computation, hereby performing this computation at compile-time and inserting the resulting object program as real Haskell code. To ensure type safety, meta programs to be run are type checked beforehand to indeed yield a valid Template Haskell object program. To this end only imported, fully-typechecked meta programs can be run via the splice operator <code>$</code>: in particular, we have to evaluate the meta program <code>$(curryN 3)</code> in a separate module to where <code>curryN</code> is defined.<br />
<br />
To generate function declarations for the first <math>n</math> curry functions, we can devise a further meta program on top of <hask>curryN</hask> as follows:<br />
<br />
<haskell><br />
genCurries :: Int -> Q [Dec]<br />
genCurries n = sequence [ mkCurryDec i | i <- [1..n] ]<br />
where mkCurryDec ith = do<br />
cury <- curryN ith<br />
let name = mkName $ "curry" ++ show ith<br />
return $ FunD name [Clause [] (NormalB cury) []]<br />
</haskell><br />
<br />
Running <hask>$(genCurries 20)</hask> will then splice in the first 20 curry functions at compile-time, namely:<br />
<br />
<haskell><br />
curry1 = \ f x1 -> f (x1)<br />
curry2 = \ f x1 x2 -> f (x1, x2)<br />
curry3 = \ f x1 x2 x3 -> f (x1, x2, x3)<br />
curry4 = \ f x1 x2 x3 x4 -> f (x1, x2, x3, x4)<br />
...<br />
curry20 = \ f x1 x2 ... x20 -> f (x1, x2, ..., x20)<br />
</haskell><br />
<br />
Note that in this case, <hask>genCurries</hask> returns a list of top-level function declarations that bind the anonymous lambda abstractions built by <hask>curryN</hask>. Also, to name the function bindings, we use function <hask>mkName :: String -> Name</hask> instead of <hask>newName :: String -> Q Name</hask>. The reason is that here we want to generate functions <hask>curry1</hask> to <hask>curry20</hask> with exactly the prescribed names, so they can be captured and referred to from other parts of the program.<br />
<br />
Evaluating Haskell (meta) programs at compile-time and splicing in the generated object programs as regular Haskell code is the first central building block of Template Haskell. The two other core mechanisms are exhibited by the implementations of <hask>curryN</hask> and <hask>genCurries</hask>: algebraic data types and the quotation monad <code>Q</code>.<br />
<br />
First, object programs created by Template Haskell are represented as regular algebraic data types, describing a program in the form of an abstract syntax tree. The Template Haskell library provides algebraic data types <code>Exp</code>, <code>Pat</code>, <code>Dec</code>, and <code>Type</code> to represent Haskell's surface syntax of expressions, patterns, declarations, and types, respectively. Virtually every concrete Haskell syntactic construct has a corresponding abstract syntax constructor in one of the four ADTs. Furthermore, all Haskell identifiers are represented by the abstract <code>Name</code> data type. By representing object programs as regular algebraic data types (and thus as data), normal Haskell can be used as the meta programming language to build object programs.<br />
<br />
Second, TH object programs are built inside the quotation monad <code>Q</code>. This monad is performed by the splice operator "<code>$</code>" at compile-time as part of evaluating the meta program. In the examples so far, the <code>Q</code> monad was only needed to provide fresh identifiers with function <hask>newName :: String -> Q Name</hask> for the generated Haskell expressions. The other main feature that requires a monadic construction of object programs is ''reification'', which allows to query compile-time information during the object program's construction. We will explain reification in detail later.<br />
<br />
Thus, Template Haskell's core functionality constitutes evaluating object programs with "<code>$</code>" and building them from algebraic data types inside the quotation monad <code>Q</code>. However, constructing object programs in terms of their abstract syntax trees is quite verbose and leads to clumsy meta programs. Therefore the Template Haskell API also provides two further interfaces to build object programs more conveniently: ''syntax construction functions'' and ''quotation brackets''.<br />
<br />
Syntax construction functions directly relate to the syntax constructors from the algebraic data types <code>Exp</code>, <code>Pat</code>, <code>Dec</code>, and <code>Type</code> for representing Haskell code. However, they hide the monadic nature of building object programs. For example, recall our definition of the <hask>genCurries</hask> meta function from above:<br />
<br />
<haskell><br />
genCurries :: Int -> Q [Dec]<br />
genCurries n = for [1..n] mkCurryDec<br />
where mkCurryDec ith = do<br />
cury <- curryN ith<br />
let name = mkName $ "curry" ++ show ith<br />
return $ FunD name [Clause [] (NormalB cury) []]<br />
</haskell><br />
<br />
To use the object program generated by the sub call to <hask>curryN</hask> in the larger context of the returned function declaration, we have to first perform <hask>curryN</hask> and bind its result to <hask>cury</hask>. The reason is that we have to account for <hask>curryN</hask>'s generation of fresh names before we can continue. Using syntax construction functions instead of data constructors, however, abstracts from the monadic construction of <hask>genCurries</hask>, thus making its code a little shorter:<br />
<br />
<haskell><br />
genCurries :: Int -> Q [Dec]<br />
genCurries n = for [1..n] mkCurryDec<br />
where mkCurryDec ith = funD name [clause [] (normalB (curryN ith)) []]<br />
where name = mkName $ "curry" ++ show ith<br />
</haskell><br />
<br />
The new <code>funD</code>, <code>clause</code>, and <code>normalB</code> functions directly correspond to the formerly used <code>FunD</code>, <code>Clause</code>, and <code>NormalB</code> constructors. The only difference lies in their types:<br />
<br />
{|<br />
|-<br />
|<hask>FunD :: Name -> [Clause] -> Dec|</hask><br />
|<hask>funD :: Name -> [Q Clause] -> Q Dec</hask><br />
|-<br />
|<hask>Clause :: [Pat] -> Body -> Clause</hask><br />
|<hask>clause :: [Q Pat] -> Q Body -> Q Clause</hask><br />
|-<br />
|<hask>NormalB :: Exp -> Body</hask><br />
|<hask>normalB :: Q Exp -> Q Body</hask><br />
|}<br />
<br />
While the syntax constructors work with raw TH expressions, the syntax construction functions expect their monadic counterparts. They construct a TH object program directly in <code>Q</code>, thus freeing the API consumer from doing the monadic wrapping and unwrapping manually. For every syntax constructor, there is a corresponding monadic syntax construction function provided.<br />
<br />
On top of syntax construction functions, quotation brackets are a further shortcut for representing Haskell code. They allow to specify an object program using just regular Haskell syntax by enclosing it inside oxford brackets <code>[| .. |]</code>. That way, object programs can be specified yet much more succinctly. For example, a meta program building a Haskell expression for the identity function is still quite verbose, if expressed with either ADTs or syntax construction functions:<br />
<br />
<br />
<haskell><br />
genId :: Q Exp<br />
genId = do<br />
x <- newName "x"<br />
lamE [varP x] (varE x)<br />
</haskell><br />
<br />
<br />
Using quotation brackets, writing the same meta program can be abbreviated much further as:<br />
<br />
<haskell><br />
genId' :: Q Dec<br />
genId' = [| \x -> x |]<br />
</haskell><br />
<br />
Quotation brackets quote regular Haskell code as the corresponding object program fragments inside the <code>Q</code> monad. There are quotation brackets for quoting Haskell expressions (<code>[e| .. |]|</code>), patterns (<code>[p| .. |]</code>), declarations (<code>[d| .. |]</code>), and types (<code>[t| .. |]</code>). Writing <code>[| .. |]</code> is hereby just another way of saying <code>[e| .. |]</code>. Using quotation brackets in a sense ''lifts'' Haskell's concrete syntax into corresponding object program expressions inside the <code>Q</code> monad. By doing so, quotation brackets represent the dual of the already introduced splice operator <code>$</code>: Evaluating a meta program with "<code>$</code>" splices in the generated object program as real Haskell code; in contrast, quotation brackets <code>[| .. |]</code> turn real Haskell code into an object program. Consequently, quotation brackets and the splice operator cancel each other out. The equation <code>$([| e |]) = e</code> holds for all expressions <code>e</code> and similar equations hold for declarations, and types<ref name="th2" />.<br />
<br />
In addition, there is support for quoting Haskell (value and type) identifiers as corresponding <code>Name</code>s inside Template Haskell. This allows to refer to regular Haskell identifiers from within TH object programs. For example, writing <hask>'genId</hask> yields a TH <code>Name</code> referring to the <hask>genId</hask> identifier. Similarly, <hask>''Q</hask> gives a <code>Name</code> referring to the <code>Q</code> type identifier.<br />
<br />
=== Generic Maps ===<br />
<br />
As a second example that uses both syntax construction functions as well as quotation brackets, let's consider a meta program <hask>mapN :: Int -> Q Dec</hask> to build "generic" <hask>map</hask> functions at compile-time. Invoking <hask>$(mapN 1)</hask> should generate the well-known standard function <hask>map :: (a -> b) -> [a] -> [b]</hask>; evaluating <hask>$(mapN 2)</hask> should splice in a binary map function of type <hask>(a -> b -> c) -> [a] -> [b] -> [c]</hask>, and so on.<ref>Note that <math>n</math>-ary maps are better written using Applicative Functors and <code>ZipList</code>s. For understanding Template Haskell as a code generator, this example is still useful though.</ref><br />
<br />
<haskell><br />
mapN :: Int -> Q Dec<br />
mapN n<br />
| n >= 1 = funD name [cl1, cl2]<br />
| otherwise = fail "mapN: argument n may not be <= 0."<br />
where<br />
name = mkName $ "map" ++ show n<br />
cl1 = do f <- newName "f"<br />
xs <- replicateM n (newName "x")<br />
ys <- replicateM n (newName "ys")<br />
let argPatts = varP f : consPatts<br />
consPatts = [ [p| $(varP x) : $(varP ys) |]<br />
| (x,ys) <- xs `zip` ys ]<br />
apply = foldl (\ g x -> [| $g $(varE x) |])<br />
first = apply (varE f) xs<br />
rest = apply (varE name) (f:ys)<br />
clause argPatts (normalB [| $first : $rest |]) []<br />
cl2 = clause (replicate (n+1) wildP) (normalB (conE `[])) []<br />
</haskell><br />
<br />
The implementation of <hask>mapN</hask> is very much in the spirit of meta function <hask>curryN</hask> from the first example. For instance, evaluating splice <hask>$(mapN 3)</hask> splices in the following map function at compile-time:<br />
<br />
<haskell><br />
map3 f (x:xs) (y:ys) (z:zs) = f x y z : map3 f xs ys zs<br />
map3 _ _ _ _ = []<br />
</haskell><br />
<br />
Nonetheless, meta function <hask>mapN</hask> exhibits a couple of new Template Haskell features: First, quotation brackets and splices are used in several places to abbreviate the object program construction. For example, helper definition <hask>apply</hask> used to generate <hask>map3</hask>'s body <hask>f x y z : map3 f xs ys zs</hask> shows the use of quotation brackets; it also highlights how splicing (<hask>$</hask>) and quotes (<hask>[| .. |]</hask>) cancel each other out. Second, identifier quotes (namely, <hask>'[]</hask>) are used to create an object program <code>Name</code> that refers to Haskell's built-in list constructor <hask>[]</hask>. Third, the example advertises how all three APIs for building Template Haskell object programs can be interleaved. The lowermost verbose API of building a raw TH data value inside the quotation monad <code>Q</code> can be abbreviated, where possible, with syntax constructor functions and quotation brackets.<br />
<br />
Lastly, the <hask>mapN</hask> example exemplifies how Haskell's static scoping is extended to object programs. The scoping principle for object programs is just as in normal Haskell: Identifiers are bound to their lexically enclosing binders in scope at the point the object program is ''defined''. Quotation brackets and splices don't alter static scopes, even though splices may bring an object program into scope at a location, where a conflicting closure is present. For example, consider this snippet:<br />
<br />
<haskell><br />
x :: Int<br />
x = 42<br />
<br />
static :: Q Exp<br />
static = [| x |]<br />
<br />
plus42 :: Int -> Int<br />
plus42 x = $static + x<br />
</haskell><br />
<br />
Here the occurrence of <hask>x</hask> in <hask>static</hask> refers to the global identifier <hask>x</hask> that is lexically in scope during its definition. Splicing in <hask>static</hask> into a different scope later where a different local <hask>x</hask> is present (i.e., <hask>plus42</hask>'s local identifier <hask>x</hask>), doesn't alter the link between <hask>static</hask>'s <hask>x</hask> and the global identifier <hask>x</hask>.<br />
<br />
The only exception to static scoping in Template Haskell are the names generated by <hask>mkName :: String -> Name</hask>. These names implement dynamic scoping and \textit{can} be captured in spliced-in code. Changing the previous snippet to<br />
<br />
<haskell><br />
x :: Int<br />
x = 42<br />
<br />
dynamic :: Q Exp<br />
dynamic = VarE (mkName "x")<br />
<br />
times2 :: Int -> Int<br />
times2 x = $dynamic + x<br />
</haskell><br />
<br />
results in the identifier <hask>x</hask> spliced in by <hask>$dynamic</hask> to be bound to the closest <hask>x</hask> in scope. Hence, its binder is <hask>times2</hask>'s local identifier <hask>x</hask> and ''not'' the global <hask>x</hask>.<br />
<br />
=== Reification ===<br />
<br />
The final major Template Haskell feature not yet described is program ''reification''. Briefly, reification allows a meta program to query compile-time information about other program parts while constructing the object program. It allows the meta program to inspect other program pieces to answer questions such as: "what's this variable's type?", "what are the class instances of this type class?", or "which constructors does this data type have and and how do they look like?". The main use case is to generate boilerplate code which ''auto-completes'' manually written code. A prime example is to generically derive type class instances from bare data type definitions.<br />
<br />
Suppose we've defined the following polymorphic data types for representing potentially erroneous values, lists, and binary trees, respectively:<br />
<br />
<haskell><br />
data Result e a = Err e | Ok a<br />
data List a = Nil | Cons a (List a)<br />
data Tree a = Leaf a | Node (Tree a) a (Tree a)<br />
</haskell><br />
<br />
Moreover, suppose we want to derive <code>Functor</code> instances for all of these types. Deriving these instances manually is straightforward, but writing them all out by hand is quite cumbersome. Especially since writing a <code>Functor</code> instance follows the same pattern across all of the above types and in fact any type <code>T a</code>.<br />
<br />
To make a type constructor <code>T</code> an instance of <code>Functor</code>, one needs to implement method <hask>fmap :: (a -> b) -> T a -> T b</hask>. Its definition is hereby precisely determined by parametricity and the functor laws: By parametricity, all values of type <code>a</code> must be replaced according to the provided function with values of type <code>b</code>. Furthermore, by the functor laws, all other shapes of the input value of type <code>T a</code> must be preserved when transforming it to the output value of type <code>T b</code>.<br />
<br />
Meta function <hask>deriveFunctor :: Name -> Q [Dec]</hask> below implements the idea of this algorithm:<br />
<br />
<haskell><br />
data Deriving = Deriving { tyCon :: Name, tyVar :: Name }<br />
<br />
deriveFunctor :: Name -> Q [Dec]<br />
deriveFunctor ty<br />
= do (TyConI tyCon) <- reify ty<br />
(tyConName, tyVars, cs) <- case tyCon of<br />
DataD _ nm tyVars cs _ -> return (nm, tyVars, cs)<br />
NewtypeD _ nm tyVars c _ -> return (nm, tyVars, [c])<br />
_ -> fail "deriveFunctor: tyCon may not be a type synonym."<br />
<br />
let (KindedTV tyVar StarT) = last tyVars<br />
instanceType = conT ``Functor `appT`<br />
(foldl apply (conT tyConName) (init tyVars))<br />
<br />
putQ $ Deriving tyConName tyVar<br />
sequence [instanceD (return []) instanceType [genFmap cs]]<br />
where<br />
apply t (PlainTV name) = appT t (varT name)<br />
apply t (KindedTV name _) = appT t (varT name)<br />
</haskell><br />
<br />
Given the name of a type constructor (e.g. <code>Result</code>, <code>List</code>, etc.), <hask>deriveFunctor</hask> derives the code for this type constructor's <code>Functor</code> instance. For example, running the splice <hask>$(deriveFunctor ''Tree)</hask> generates the following code:<br />
<br />
<haskell><br />
instance Functor Tree where<br />
fmap f (Leaf x) = Leaf (f x)<br />
fmap f (Node l x r) = Node (fmap f l) (f x) (fmap f r)<br />
</haskell><br />
<br />
Most notably, meta function <hask>deriveFunctor</hask> shows reification in action. It calls function <hask>reify :: Name -> Q Info</hask> on the input type constructor's name to yield information about this data type's definition. Using <hask>reify</hask>, it thus learns whether the data type was defined using the <code>data</code> or <code>newtype</code> keyword, which constructors it defines and what their shapes are. Based on the learned structure, <hask>deriveFunctor</hask> is then able to generate a suitable definition of <hask>fmap</hask> and its different clauses via the auxiliaries <hask>genFmap</hask>, <hask>genFmapClause</hask>, and <hask>newField</hask>, defined below. These auxiliary definitions generate one <hask>fmap</hask> clause for each of the data type's constructors. And each clause then transforms its constructor by recursively modifying all of the constructor's fields of type <code>a</code> through <hask>fmap</hask>'s function <code>f</code>, while retaining all other shapes.<br />
<br />
<haskell><br />
genFmap :: [Con] -> Q Dec<br />
genFmap constructors<br />
= do funD `fmap (map genFmapClause constructors)<br />
<br />
genFmapClause :: Con -> Q Clause<br />
genFmapClause (NormalC name fieldTypes)<br />
= do f <- newName "f"<br />
fieldNames <- replicateM (length fieldTypes) (newName "x")<br />
<br />
let pats = varP f:[conP name (map varP fieldNames)]<br />
body = normalB $ appsE $<br />
conE name : map (newField f) (zip fieldNames fieldTypes)<br />
<br />
clause pats body []<br />
<br />
newField :: Name -> (Name, StrictType) -> Q Exp<br />
newField f (x, (_, fieldType))<br />
= do Just (Deriving typeCon typeVar) <- getQ<br />
case fieldType of<br />
VarT typeVar' | typeVar' == typeVar -><br />
[| $(varE f) $(varE x) |]<br />
ty `AppT` VarT typeVar' |<br />
leftmost ty == (ConT typeCon) && typeVar' == typeVar -><br />
[| fmap $(varE f) $(varE x) |]<br />
_ -> [| $(varE x) |]<br />
<br />
leftmost :: Type -> Type<br />
leftmost (AppT ty1 _) = leftmost ty1<br />
leftmost ty = ty<br />
</haskell><br />
<br />
In an analogous manner to <hask>deriveFunctor</hask>, a function <hask>deriveFoldable :: Name -> Q [Dec]</hask> can be devised to derive a data type's <code>Foldable</code> instance. All that is needed is to provide a definition for function <hask>foldMap :: Monoid m => (a -> m) -> T a -> m</hask>. Again, <hask>foldMap</hask>'s definition follows directly from a data type's bare definition, which can be observed by means of reification. This highlights particularly how the functionality offered by Template Haskell provides a low-level API into the GHC compiler to manipulate abstract syntax trees at compile-time. This mechanism is quite powerful and even allows to simulate some of GHC's offered language extensions, e.g., <code>-XDeriveFunctor</code> and <code>-XDeriveFoldable</code>, to be implemented as a library on top of Template Haskell.<br />
<br />
== Template Haskell for building Embedded Domain specific Languages (EDSLs) ==<br />
<br />
To see Template Haskell's potential for building an EDSL, consider the problem of pattern matching text with regular expressions. Suppose, as part of a Haskell program we need to devise many different regular expressions and use them to pattern match text fragments. Regular expressions are easily defined by an algebraic data type capturing their structure, as well as an evaluator checking whether a regular expression matches some input string. <ref>This example draws on Penn's CIS 552 ''Advanced Programming'' course, specifically Assignment 5: http://www.seas.upenn.edu/~cis552/current/hw/hw05/Main.html.</ref><br />
<br />
<haskell><br />
data RegExp<br />
= Char (Set Char) -- [a], [abc], [a-z]; matches a single character from the specified class<br />
| Alt RegExp RegExp -- r1 | r2 (alternation); matches either r1 or r2<br />
| Seq RegExp RegExp -- r1 r2 (concatenation); matches r1 followed by r2<br />
| Star RegExp -- r* (Kleene star); matches r zero or more times<br />
| Empty -- matches only the empty string<br />
| Void -- matches nothing (always fails)<br />
| Var String -- a variable holding another regexp (explained later)<br />
deriving Show<br />
<br />
match :: RegExp -> String -> Bool<br />
match r s = nullable (foldl deriv r s)<br />
</haskell><br />
<br />
The evaluator <hask>match</hask> is hereby based on the concept of derivatives<ref name="regexp-derivs" />: an initial regular expression <hask>r</hask> matches an input string <hask>s</hask>, if <hask>r</hask> matches the first character of <hask>s</hask> and its derivative regular expression <hask>(deriv r)</hask> matches the remainder of <hask>s</hask>:<br />
<br />
<haskell><br />
nullable :: RegExp -> Bool<br />
nullable (Char _) = False<br />
nullable (Alt r1 r2) = nullable r1 || nullable r2<br />
nullable (Seq r1 r2) = nullable r1 && nullable r2<br />
nullable (Star _) = True<br />
nullable Empty = True<br />
nullable Void = False<br />
nullable (Var _) = False<br />
<br />
deriv :: RegExp -> Char -> RegExp<br />
deriv (Char cs) c<br />
| c `Set.member` cs = Empty<br />
| otherwise = Void<br />
deriv (Alt r1 r2) c = Alt (deriv r1 c) (deriv r2 c)<br />
deriv (Seq r1 r2) c<br />
| nullable r1 = Alt (Seq (deriv r1 c) r2) (deriv r2 c)<br />
| otherwise = Seq (deriv r1 c) r2<br />
deriv (Star r) c = deriv (Alt Empty (Seq r (Star r))) c<br />
deriv Empty _ = Void<br />
deriv Void _ = Void<br />
deriv (Var _) _ = Void<br />
</haskell><br />
<br />
The <code>RegExp</code> data type and the <hask>match</hask> function solve the initially posed problem of providing regular expressions in Haskell. However, specifying regular expressions in abstract syntax is extremely tedious. For example, consider defining a regular expression for checking the wellformedness of email addresses ending with the top level domain '''.com'''. In its usual concrete syntax, such a regular expression is easily defined as <code>([a-z]|[0-9])*@@([a-z]|[0-9])*.com</code>, but writing it in terms of the <code>RegExp</code> dataype is verbose and unintuitive. Moreover, parsing functions like<br />
<br />
* <hask>compile :: String -> RegExp</hask>, or<br />
* <hask>compile' :: String -> Either CompileError RegExp</hask><br />
<br />
do not remedy the problem of working with regular expressions in concrete syntax. Due to "compiling" regular expressions at run time, they don't provide any compile-time type-safety guarantees that the input raw expression is wellformed; thus they lead to either run time exceptions for illformed regular expressions (e.g., <hask>compile</hask>) or induce a tedious handling for compiled regexes (e.g., <hask>compile'</hask>).<br />
<br />
To preserve type safety and yet to be able to use regular expressions conveniently, we want to embed the concrete regular expression syntax into the Haskell host language. This can be done via Template Haskell's quasi quotes and furthermore enabling the <code>QuasiQuotes</code> extension. This allows defining ''quasi quotes'' for regular expressions, denoted <code>|[regex| .. |]</code>, where anything inside the quasi quote is considered part of an embedded regular expression language. Using quasi quotes, we can then specify the regex for email addresses from above naturally as follows:<br />
<br />
<haskell><br />
validDotComMail :: RegExp<br />
validDotComMail = [regex|([a-z]|[0-9])*@([a-z]|[0-9])*.com|]<br />
</haskell><br />
<br />
We can even compose regular expressions easily from smaller building blocks:<br />
<br />
<haskell><br />
alphaNum, validDotComMail' :: RegExp<br />
alphaNum = [regex|[a-z]|[0-9]|]<br />
validDotComMail' = [regex|${alphaNum}*@${alphaNum}*.com|]<br />
</haskell><br />
<br />
Writing <hask>${alphaNum}</hask> interpolates the regex referred to by <hask>alphaNum</hask> into the larger regex <hask>validDotComMail'</hask>. In essence, this means that we can define our own notion of splicing values from the Haskell meta language into the embedded object language of regular expressions. We can go further and even allow to run Haskell code when interpolating with <code>${..}</code>. For example, refining our wellformedness check for '''.com''' mail addresses, we might want to ensure at least one character to occur on either side of the "@" symbol:<br />
<br />
<haskell><br />
chars, validDotComMail'' :: RegExp<br />
chars = [regex|[a-z]|[A-Z]|[0-9]|[-_.]|]<br />
validDotComMail'' = [regex|${plus chars}@${plus chars}.com|]<br />
<br />
plus :: RegExp -> RegExp<br />
plus r = Seq r (Star r)<br />
</haskell><br />
<br />
Here, <hask>plus</hask> corresponds to the usual regex combinator that requires a given regex to occur at least once. Note how <hask>plus</hask> is defined as a regular Haskell function and then used ''inside'' of the embedded regex language to build the regular expression for <hask>validDotComMail''</hask>.<br />
<br />
Intuitively, a quasi quote like <code>[regex| .. |]</code> converts an embedded language's concrete syntax to Haskell code at compile-time. It is defined by a ''quasi quoter'', which is a parser for the embedded language. Its task is to parse the embedded language's syntax into a corresponding Template Haskell expression and then to splice this expression as real Haskell code in place of the quasi quote. The conversion of embedded language code to corresponding Haskell code hereby happens before typechecking the Haskell module. Hence, trying to splice in malformed embedded language fragments will raise a Haskell type error at compile-time.<br />
<br />
The quasi quoter <code>regex</code> for our embedded language of regular expressions can be defined as follows:<br />
<br />
<haskell><br />
regex :: QuasiQuoter<br />
regex = QuasiQuoter {<br />
quoteExp = compile<br />
, quotePat = notHandled "patterns"<br />
, quoteType = notHandled "types"<br />
, quoteDec = notHandled "declarations"<br />
}<br />
where notHandled things = error $<br />
things ++ " are not handled by the regex quasiquoter."<br />
<br />
compile :: String -> Q Exp<br />
compile s =<br />
case P.parse regexParser "" s of<br />
Left err -> fail (show err)<br />
Right regexp -> [e| regexp |]<br />
</haskell><br />
<br />
That is, formally a <code>QuasiQuoter</code> consists of four parsers,<br />
<br />
<haskell><br />
quoteExp :: String -> Q Exp<br />
quotePat :: String -> Q Pat<br />
quoteType :: String -> Q Type<br />
quoteDec :: String -> Q Dec<br />
</haskell><br />
<br />
to parse raw strings of the embedded language into the different categories of Haskell syntax. In this example, however, we only want to splice embedded regular expressions into the context of Haskell expressions, so we only define the <code>quoteExp</code> parser in the <code>regex</code> quasi quoter. This parser compiles an embedded regular expression given as a string into a corresponding Template Haskell expression.<br />
<br />
Compilation by the <hask>compile</hask> function proceeds in two stages: First, we parse the input string regex into a corresponding <code>RegExp</code> value. Second, we encode this <code>RegExp</code> value as a Haskell expression in Template Haskell's <code>Q Exp</code> type. It is the second step that allows us to interpolate variables (or even code) from the Haskell host language into the EDSL for regular expressions.<br />
<br />
Parsing a raw regular expression into a corresponding <code>RegExp</code> value is a routine task using (e.g.) the ''parsec'' library:<br />
<br />
<haskell><br />
regexParser :: Parsec String () RegExp<br />
regexParser = alts <* eof where<br />
atom = try var <|> char<br />
var = Var <$> (string "${" *> many1 (noneOf "}") <* P.char '}')<br />
char = charclass <|> singlechar<br />
singlechar = (Char . Set.singleton) <$> noneOf specials<br />
charclass = fmap (Char . Set.fromList) $<br />
P.char '[' *> content <* P.char ']'<br />
content = try (concat <$> many1 range)<br />
<|> many1 (noneOf specials)<br />
range = enumFromTo<br />
<$> (noneOf specials <* P.char '-')<br />
<*> noneOf specials<br />
alts = try (Alt <$> seqs <*> (P.char '|' *> alts)) <|> seqs<br />
seqs = try (Seq <$> star <*> seqs) <|> star<br />
star = try (Star <$> (atom <* P.char '*'))<br />
<|> try (Star <$> (P.char '(' *> alts <* string ")*"))<br />
<|> atom<br />
specials = "[]()*|"<br />
</haskell><br />
<br />
To represent regular expressions of type <code>RegExp</code> as Template Haskell expressions of type <code>Q Exp</code>, Template Haskell's <code>Lift</code> typeclass is used. Its method <hask>lift :: Lift a => a -> Q Exp</hask> lifts values from the Haskell meta language (e.g., a <code>RegExp</code> value) into Template Haskell's expression language (i.e., a <code>Q Exp</code> value). The <hask>lift</hask> function is implicitly invoked by quote <hask>[e| regexp |]</hask> in function <hask>compile</hask>.<br />
<br />
Most of the lifting is a direct encoding of the syntactic structure of the <code>RegExp</code> value; the only interesting case is when lifting the regular expression variable <hask>Var vars</hask>. In this case, we treat the words in the string <hask>vars</hask> as referring to identifiers from the Haskell host language, which we apply in a left associative manner to each other. Doing this enables interpolation of Haskell identifiers or even simple forms of Haskell expressions into our EDSL of regular expressions as shown by the regexes <hask>validDotComMail'</hask>, and <hask>validDotComMail''</hask> above.<br />
<br />
<haskell><br />
instance Lift a => Lift (Set a) where<br />
lift set = appE (varE `Set.fromList) (lift (Set.toList set))<br />
<br />
instance Lift RegExp where<br />
-- lift :: RegExp -> Q Exp<br />
lift (Char cs) = apply `Char [lift cs]<br />
lift (Alt r1 r2) = apply `Alt (map lift [r1, r2])<br />
lift (Seq r1 r2) = apply `Seq (map lift [r1, r2])<br />
lift (Star r1) = apply `Star (map lift [r1])<br />
lift Empty = apply `Empty []<br />
lift Void = apply `Void []<br />
lift (Var vars) = foldl1 appE $ map (varE . mkName) (words vars)<br />
<br />
apply :: Name -> [Q Exp] -> Q Exp<br />
apply n = foldl appE (conE n)<br />
</haskell><br />
<br />
These two steps constitute the conversion of raw string regular expressions into Template Haskell expressions inside of the <hask>compile</hask> function and define the <code>regex</code> quasiquoter. Whenever we write a quasi quote like <hask>[regex| .. |]</hask> in a Haskell expression context, <hask>regex</hask>'s parser <hask>quoteExp</hask> converts the regex EDSL into a Template Haskell expression <code>Q Exp</code> and splices in the result as a wellformed <code>RegExp</code> value. This example shows how Template Haskell and quasi quotes can be used to define a type-safe, domain specific language for regular expressions.<br />
<br />
=== Shakespearean Templates ===<br />
<br />
In much the same manner as in the last example, Template Haskell and quasi quotes are used in Michael Snoyman's <code>shakespeare</code> library<ref name="shakespeare" /><ref name="shakespeare-lib" />. It defines embedded templating languages for working with the internet's web languages from within a Haskell web application. In particular, the <code>shakespeare</code> library provides the template languages ''Hamlet'', ''Cassius'', and ''Julius'' for writing embedded HTML, CSS, and Javascript code, respectively. All three templating languages internally work quite similarly to the previous example's EDSL for regular expressions: quasi quotes allow one to write HTML, CSS, or JavaScript code in concrete (though slightly modified) syntax inside of Haskell. Moreover, identifiers from the Haskell host language as well as code fragments can be interpolated into the template languages at compile-time. In the remainder we will briefly show-case the <code>shakespeare</code> library's templating language Hamlet for creating HTML documents; the other templating languages Cassius and Julius are similar.<br />
<br />
To create and output a simple web page from inside a Haskell application, the following is enough:<br />
<br />
<haskell><br />
import Data.Text<br />
import Text.Hamlet<br />
import Text.Blaze.Html.Renderer.String<br />
<br />
data Page = Home | About | Github<br />
<br />
mkUrls :: Page -> [(Text, Text)] -> Text<br />
mkUrls Home _ = "/home.html"<br />
mkUrls About _ = "/about.html"<br />
mkUrls Github _ = "https://www.github.com/bollmann"<br />
<br />
webPage :: Text -> Text -> HtmlUrl Page<br />
webPage title content = [hamlet|<br />
<html><br />
<head><br />
<title>#{Text.toUpper title}<br />
<body><br />
<h1>#{title}<br />
<div>Welcome to my Shakespearean Templates page!<br />
<hr><br />
<div>Links:<br />
<ul><br />
<a href=@{Home}>My Homepage<br />
<a href=@{About}>About me<br />
<a href=@{Github}>Check out my Github<br />
<hr><br />
<div>#{content}<br />
|]<br />
<br />
main = putStrLn $ renderHtml $<br />
webPage "Hello Shakespeare!" "Hello World!" mkUrls<br />
</haskell><br />
<br />
Running this Haskell program, outputs an HTML page as specified by the Hamlet templating language, embedded through quasi quote <hask>[hamlet| .. |]</hask> in function <hask>webPage</hask>. Hamlet closely resembles real HTML syntax, but is even more terse: instead of a closing HTML tag, Hamlet uses indentation to indicate the span of the tag. Furthermore, Hamlet allows to interpolate code or identifiers from the Haskell host language when creating an HTML template. Interpolation of Haskell code into Hamlet is done by writing <code>#{ .. }</code>. In the above example, the HTML page's title and content are interpolated from Haskell identifiers. Note particularly how in the webpage's title we uppercase the interpolated title using Haskell's <hask>Text.toUpper</hask> function ''inside'' of the Hamlet language.<br />
<br />
In addition to this standard interpolation, Hamlet can also interpolate links by writing <code>@{..}</code>. These links are specified as values of the <hask>Page</hask> data type inside the template and the <hask>mkUrls</hask> render function translates them to real URLs later. Hamlet's URL interpolation has commonly be phrased as creating "type-safe URLs". One reason is that, just like with normal variable interpolation, all interpolated links have to exist and be type correct at compile-time; in this case, links must be values of the <code>Page</code> data type. Hence, as soon as a link's constructor shape is changed, the compiler statically forces us to update all references to this link as well. Furthermore, there is only one distinct place in the code to maintain or update a link's raw URL, thus minimizing the risk of dead URLs.<br />
<br />
For example, suppose we want to add more external links to our web page. We could model this fact by changing the <hask>Page</hask> data type to<br />
<br />
<haskell><br />
data Page = Home | About | External ExternalPage<br />
data ExternalPage = Github | Haskell | Reddit<br />
</haskell><br />
<br />
and, moreover, changing the <hask>mkUrls</hask> renderer function to account for the new links:<br />
<br />
<haskell><br />
mkUrls :: Page -> [(Text, Text)] -> Text<br />
mkUrls Home _ = "/home.html"<br />
mkUrls About _ = "/about.html"<br />
mkUrls (External page) _ = mkExternalUrls page<br />
<br />
mkExternalUrls :: ExternalPage -> Text<br />
mkExternalUrls Github = "https://www.github.com"<br />
mkExternalUrls Haskell = "http://www.haskell.org"<br />
mkExternalUrls Reddit = "http://www.reddit.com/r/haskell"<br />
</haskell><br />
<br />
Doing just these changes, will then cause a compile-time error in our <hask>webPage</hask> template, since we haven't updated the <hask>Github</hask> reference to our newly adjusted link structure. Hence, the compiler reminds (and in fact forces) us to update all locations in the code that used the old <hask>Github</hask> link to now use the new <hask>External Github</hask> (as well as optionally the <hask>External Haskell</hask>, etc.) links.<br />
<br />
Finally, Hamlet allows to use some control constructs like if conditionals, for loops, and let bindings to embed basic business logic into a webpage's template. Michael Snoyman gives a gentle (and much more in-depth) introduction to shakespearean templates and Yesod<ref name="shakespeare" /><ref name="yesod" />.<br />
<br />
= References =<br />
<br />
<references><br />
<ref name="th1">Tim Sheard and Simon Peyton Jones. Template Meta-Programming for Haskell. SIGPLAN Not., 37(12):60-75, December 2002.</ref><br />
<ref name="th2">Tim Sheard and Simon Peyton Jones. Notes on Template Haskell, Version 2. URL: http://research.microsoft.com/simonpj/papers/meta-haskell/notes2.ps, 2003.</ref><br />
<ref name="th3">Simon Peyton Jones. Major Proposed Revision of Template Haskell. URL: https://ghc.haskell.org/trac/ghc/blog/Template%20Haskell%20Proposal, 2010</ref><br />
<ref name="qq">Geoffrey Mainland. Why it's nice to be quoted: Quasiquoting for Haskell. In ''Proceedings of the ACM SIGPLAN Workshop on Haskell'', Haskell '07, pages 73-82, New York, NY, USA, 2007. ACM</ref><br />
<ref name="regexp-derivs">Janusz A. Brzozowski. Derivatives of regular expressions. J. ACM, 11(4):481–494, October 1964.</ref><br />
<ref name="shakespeare">Michael Snoyman. Shakespearean Templates. URL: http://www.yesodweb.com/book/shakespearean-templates [Accessed: May 2016].</ref><br />
<ref name="shakespeare-lib">Michael Snoyman. The <code>shakespeare</code> Haskell library. URL: http://hackage.haskell.org/package/shakespeare [Accessed: May 2016].</ref><br />
<ref name="yesod">Michael Snoyman. Haskell and Yesod. URL: http://www.yesodweb.com/book-1.4 [Accessed: May 2016].</ref><br />
</references></div>Dfeuerhttps://wiki.haskell.org/index.php?title=GHC.Generics&diff=59233GHC.Generics2015-01-26T16:05:06Z<p>Dfeuer: /* Example: serialization */</p>
<hr />
<div>GHC 7.2 includes improved support for ''datatype-generic programming'' through two features, enabled with two flags: <tt>DeriveGeneric</tt> and <tt>DefaultSignatures</tt>. We show how this all works in this page, starting with a detailed example.<br />
<br />
== Example: serialization ==<br />
<br />
Suppose you are writing a class for serialization of data. You have a type <hask>Bit</hask> representing bits, and a class <hask>Serialize</hask>:<br />
<br />
<haskell><br />
data Bit = O | I<br />
<br />
class Serialize a where<br />
put :: a -> [Bit]<br />
</haskell><br />
<br />
You might have written some instances already:<br />
<br />
<haskell><br />
instance Serialize Int where<br />
put i = serializeInt i<br />
<br />
instance Serialize a => Serialize [a] where<br />
put [] = []<br />
put (h:t) = put h ++ put t<br />
</haskell><br />
<br />
A user of your library, however, will have his own datatypes, like:<br />
<br />
<haskell><br />
data UserTree a = Node a (UserTree a) (UserTree a) | Leaf<br />
</haskell><br />
<br />
He will have to specify an <hask>instance Serialize (UserTree a) where ...</hask> himself. This, however, is tedious, especially because most instances will probably be rather trivial, and should be derived automatically.<br />
<br />
It is here that generic programming can help you. If you are familiar with [http://www.cs.uu.nl/wiki/bin/view/GenericProgramming/SYB SYB] you could use it at this stage, but now we'll see how to do this with the GHC Generic features.<br />
<br />
=== Generic serialization ===<br />
<br />
First you have to tell the compiler how to serialize any datatype, in general. Since Haskell datatypes have a regular structure, this means you can just explain how to serialize a few basic datatypes.<br />
<br />
==== Representation types ====<br />
<br />
We can represent most Haskell datatypes using only the following primitive types:<br />
<br />
<haskell><br />
-- | Unit: used for constructors without arguments<br />
data U1 p = U1<br />
<br />
-- | Constants, additional parameters and recursion of kind *<br />
newtype K1 i c p = K1 { unK1 :: c }<br />
<br />
-- | Meta-information (constructor names, etc.)<br />
newtype M1 i c f p = M1 { unM1 :: f p }<br />
<br />
-- | Sums: encode choice between constructors<br />
infixr 5 :+:<br />
data (:+:) f g p = L1 (f p) | R1 (g p)<br />
<br />
-- | Products: encode multiple arguments to constructors<br />
infixr 6 :*:<br />
data (:*:) f g p = f p :*: g p<br />
</haskell><br />
<br />
For starters, try to ignore the <tt>p</tt> parameter in all types; it's there just for future compatibility. The easiest way to understand how you can use these types to represent others is to see an example. Let's represent the <hask>UserTree</hask> type shown before:<br />
<br />
<haskell><br />
type RepUserTree a =<br />
-- A UserTree is either a Leaf, which has no arguments<br />
U1<br />
-- ... or it is a Node, which has three arguments that we put in a product<br />
:+: a :*: UserTree a :*: UserTree a<br />
</haskell><br />
<br />
Simple, right? Different constructors become alternatives of a sum, and multiple arguments become products. In fact, we want to have some more information in the representation, like datatype and constructor names, and to know if a product argument is a parameter or a type. We use the other primitives for this, and the representation looks more like:<br />
<br />
<haskell><br />
type RealRepUserTree a =<br />
-- Information about the datatype<br />
M1 D Data_UserTree (<br />
-- Leaf, with information about the constructor<br />
M1 C Con_Leaf U1<br />
-- Node, with information about the constructor<br />
:+: M1 C Con_Node (<br />
-- Constructor argument, which could have information<br />
-- about a record selector label<br />
M1 S NoSelector (<br />
-- Argument, tagged with P because it is a parameter<br />
K1 P a)<br />
-- Another argument, tagged with R because it is <br />
-- a recursive occurrence of a type<br />
:*: M1 S NoSelector (K1 R (UserTree a))<br />
-- Idem<br />
:*: M1 S NoSelector (K1 R (UserTree a))<br />
))<br />
</haskell><br />
<br />
A bit more complicated, but essentially the same. Datatypes like <hask>Data_UserTree</hask> are empty datatypes used only for providing meta-information in the representation; you don't have to worry much about them for now. Also, GHC generates these representations for you automatically, so you should never have to define them yourself! All of this is explained in much more detail in Section 2.1. of [http://dreixel.net/research/pdf/gdmh.pdf the original paper describing the new generic deriving mechanism].<br />
<br />
==== A generic function ====<br />
<br />
Since GHC can represent user types using only those primitive types, all you have to do is to tell GHC how to serialize each of the individual primitive types. The best way to do that is to create a new type class:<br />
<br />
<haskell><br />
class GSerialize f where<br />
gput :: f a -> [Bit]<br />
</haskell><br />
<br />
This class looks very much like the original <hask>Serialize</hask> class, just that the type argument is of kind <hask>* -> *</hask>, since our generic representation types have this <tt>p</tt> parameter lying around. Now we need to give instances for each of the basic types. For units there's nothing to serialize:<br />
<br />
<haskell><br />
instance GSerialize U1 where<br />
gput U1 = []<br />
</haskell><br />
<br />
The serialization of multiple arguments is simply the concatenation of each of the individual serializations:<br />
<br />
<haskell><br />
instance (GSerialize a, GSerialize b) => GSerialize (a :*: b) where<br />
gput (a :*: b) = gput a ++ gput b<br />
</haskell><br />
<br />
The case for sums is the most interesting, as we have to record which alternative we are in. We will use a 0 for left injections and a 1 for right injections:<br />
<br />
<haskell><br />
instance (GSerialize a, GSerialize b) => GSerialize (a :+: b) where<br />
gput (L1 x) = O : gput x<br />
gput (R1 x) = I : gput x<br />
</haskell><br />
<br />
We don't need to encode the meta-information, so we just go over it recursively :<br />
<br />
<haskell><br />
instance (GSerialize a) => GSerialize (M1 i c a) where<br />
gput (M1 x) = gput x<br />
</haskell><br />
<br />
Finally, we're only left with the arguments. For these we will just use our first class, <hask>Serialize</hask>, again:<br />
<br />
<haskell><br />
instance (Serialize a) => GSerialize (K1 i a) where<br />
gput (K1 x) = put x<br />
</haskell><br />
<br />
So, if a user datatype has a parameter which is instantiated to <hask>Int</hask>, at this stage we will use the library instance for <hask>Serialize Int</hask>.<br />
<br />
==== Default implementations ====<br />
<br />
We've seen how to represent user types generically, and how to define functions on representation types. However, we still have to tie these two together, explaining how to convert user types to their representation and then applying the generic function.<br />
<br />
The representation <hask>RepUserTree</hask> we have seen earlier is only one component of the representation; we also need functions to convert to and from the user datatype into the representation. For that we use another type class:<br />
<br />
<haskell><br />
class Generic a where<br />
-- Encode the representation of a user datatype<br />
type Rep a :: * -> *<br />
-- Convert from the datatype to its representation<br />
from :: a -> (Rep a) x<br />
-- Convert from the representation to the datatype<br />
to :: (Rep a) x -> a<br />
</haskell><br />
<br />
So, for the <hask>UserTree</hask> datatype shown before, GHC generates the following instance:<br />
<br />
<haskell><br />
instance Generic (UserTree a) where<br />
type Rep (UserTree a) = RepUserTree a<br />
<br />
from Leaf = L1 U1<br />
from (Node a l r) = R1 (a :*: l :*: r)<br />
<br />
to (L1 U1) = Leaf<br />
to (R1 (a :*: l :*: r)) = Node a l r<br />
</haskell><br />
<br />
(Note that we are using the simpler representation <hask>RepUserTree</hask> instead of the real representation <hask>RealRepUserTree</hask>, just for simplicity.)<br />
<br />
Equipped with a <hask>Generic</hask> instance, we are ready to tell the compiler how it can serialize any representable type:<br />
<br />
<haskell><br />
putDefault :: (Generic a, GSerialize (Rep a)) => a -> [Bit]<br />
putDefault a = gput (from a)<br />
</haskell><br />
<br />
The type of <hask>putDefault</hask> says that we can serialize any <tt>a</tt> into a list of bits, as long as that <tt>a</tt> is <hask>Generic</hask>, and its representation <hask>Rep a</hask> has a <hask>GSerialize</hask> instance. The implementation is very simple: first convert the value to its representation using <hask>from</hask>, and then call <hask>gput</hask> on that representation.<br />
<br />
However, we still have to write a <hask>Serialize</hask> instance for the user dataype:<br />
<br />
<haskell><br />
instance (Serialize a) => Serialize (UserTree a) where<br />
put = putDefault<br />
</haskell><br />
<br />
=== Using GHC's new features ===<br />
<br />
What we have seen so far could all already be done, at the cost of writing a lot of boilerplate code yourself (or spending hours writing [[Template Haskell]] code to do it for you). Now we'll see how the new features of GHC can help you.<br />
<br />
==== Deriving representations ====<br />
<br />
The <hask>Generic</hask> class, and all the representation types, come with GHC in the <tt>GHC.Generics</tt> module. GHC can also derive <hask>Generic</hask> for user types, so all the user has to do is:<br />
<br />
<haskell><br />
{-# LANGUAGE DeriveGeneric #-}<br />
<br />
data UserTree a = Node a (UserTree a) (UserTree a) | Leaf<br />
deriving Generic<br />
</haskell><br />
<br />
(Standalone deriving also works fine, and you can use it for types you have not defined yourself, but are imported from somewhere else.) You will need the new <tt>DeriveGeneric</tt> language pragma.<br />
<br />
==== More general default methods ====<br />
<br />
We don't want the user to have to write the <hask>instance Serialize (UserTree a)</hask> himself, since most of the times it will just be <hask>putDefault</hask>. However, we cannot make <hask>putDefault</hask> the default implementation of the <hask>put</hask> method, because that would require adding the <hask>(Generic a, GSerialize (Rep a))</hask> constraint to the class head. This would restrict the ability to give ad-hoc instances for types that are not representable, for instance.<br />
<br />
We solved this by allowing the user to give a different signature for default methods:<br />
<br />
<haskell><br />
{-# LANGUAGE DefaultSignatures #-}<br />
<br />
class Serialize a where<br />
put :: a -> [Bit]<br />
<br />
default put :: (Generic a, GSerialize (Rep a)) => a -> [Bit]<br />
put a = gput (from a)<br />
</haskell><br />
<br />
With the new language pragma <tt>DefaultSignatures</tt>, GHC allows you to put the keyword <hask>default</hask> before a (new) type signature for a method inside a class declaration. If you give such a default type signature, then you have to provide a default method implementation, which will be type-checked using the default signature, and not the original one.<br />
<br />
Now the user can simply write:<br />
<br />
<haskell><br />
instance (Serialize a) => Serialize (UserTree a)<br />
</haskell><br />
<br />
GHC fills out the implementation for <hask>put</hask> using the default method. It will type-check correctly because we have a <hask>Generic</hask> instance for <hask>UserTree</hask>, and <hask>GSerialize</hask> instances for all the representation types.<br />
<br />
=== Complete working example ===<br />
<br />
<haskell><br />
{-# LANGUAGE DefaultSignatures, DeriveGeneric, TypeOperators, FlexibleContexts #-}<br />
<br />
import GHC.Generics<br />
import Data.Bits<br />
<br />
<br />
data Bit = O | I deriving Show<br />
<br />
class Serialize a where<br />
put :: a -> [Bit]<br />
<br />
default put :: (Generic a, GSerialize (Rep a)) => a -> [Bit]<br />
put a = gput (from a)<br />
<br />
get :: [Bit] -> (a, [Bit])<br />
<br />
default get :: (Generic a, GSerialize (Rep a)) => [Bit] -> (a, [Bit])<br />
get xs = (to x, xs')<br />
where (x, xs') = gget xs<br />
<br />
class GSerialize f where<br />
gput :: f a -> [Bit]<br />
gget :: [Bit] -> (f a, [Bit])<br />
<br />
-- | Unit: used for constructors without arguments<br />
instance GSerialize U1 where<br />
gput U1 = []<br />
gget xs = (U1, xs)<br />
<br />
-- | Constants, additional parameters and recursion of kind *<br />
instance (GSerialize a, GSerialize b) => GSerialize (a :*: b) where<br />
gput (a :*: b) = gput a ++ gput b<br />
gget xs = (a :*: b, xs'')<br />
where (a, xs') = gget xs<br />
(b, xs'') = gget xs'<br />
<br />
-- | Meta-information (constructor names, etc.)<br />
instance (GSerialize a, GSerialize b) => GSerialize (a :+: b) where<br />
gput (L1 x) = O : gput x<br />
gput (R1 x) = I : gput x<br />
gget (O:xs) = (L1 x, xs')<br />
where (x, xs') = gget xs<br />
gget (I:xs) = (R1 x, xs')<br />
where (x, xs') = gget xs<br />
<br />
-- | Sums: encode choice between constructors<br />
instance (GSerialize a) => GSerialize (M1 i c a) where<br />
gput (M1 x) = gput x<br />
gget xs = (M1 x, xs')<br />
where (x, xs') = gget xs<br />
<br />
-- | Products: encode multiple arguments to constructors<br />
instance (Serialize a) => GSerialize (K1 i a) where<br />
gput (K1 x) = put x<br />
gget xs = (K1 x, xs')<br />
where (x, xs') = get xs<br />
<br />
instance Serialize Bool where<br />
put True = [I]<br />
put False = [O]<br />
get (I:xs) = (True, xs)<br />
get (O:xs) = (False, xs)<br />
<br />
--<br />
-- Try it out. (Normally this would be in a separate module.)<br />
--<br />
<br />
data UserTree a = Node a (UserTree a) (UserTree a) | Leaf<br />
deriving (Generic, Show)<br />
<br />
instance (Serialize a) => Serialize (UserTree a)<br />
<br />
main = do<br />
let xs = put True<br />
print (fst . get $ xs :: Bool)<br />
let ys = put (Leaf :: UserTree Bool)<br />
print (fst . get $ ys :: UserTree Bool)<br />
let zs = put (Node False Leaf Leaf :: UserTree Bool)<br />
print (fst . get $ zs :: UserTree Bool)<br />
</haskell><br />
<br />
== Different perspectives ==<br />
<br />
We outline the changes introduced in 7.2 regarding support for generic programming from the perspective of three different types of users: the end-user, the generic programmer, and the GHC hacker.<br />
<br />
=== The end-user ===<br />
<br />
If you know nothing about generic programming and would like to keep it that way, then you will be pleased to know that using generics in GHC 7.2 is easier than ever. As soon as you encounter a class with a default signature (like <tt>Serialize</tt> above), you will be able to give empty instances for your datatypes, like this:<br />
<br />
<haskell><br />
instance (Serialize a) => Serialize (UserTree a)<br />
</haskell><br />
<br />
You will need to add a <hask>deriving Generic</hask> clause to each datatype that you want to have generic implementations for. You might have datatypes that use other datatypes, and you might need <tt>Generic</tt> instances for those too. In that case, you can import the module where the datatype is defined and give a standalone deriving <tt>Generic</tt> instance. In either case, you will need the <tt>-XDeriveGeneric</tt> flag.<br />
<br />
=== The generic programmer ===<br />
<br />
If you are a library author and are eager to make your classes easy to instantiate by your users, then you should invest some time in defining instances for each of the representation types of <tt>GHC.Generics</tt> and defining a generic default method. See the example for <tt>Serialize</tt> above, and the [http://dreixel.net/research/pdf/gdmh.pdf original paper] for many other examples (but make sure to check the [[#Changes from the paper|changes from the paper]]).<br />
<br />
=== The GHC hacker ===<br />
<br />
If you are working on the GHC source code, you might find it useful to know what kind of changes were made. There is a [http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/GenericDeriving Trac wiki page] with a lower-level overview of things and also keeping track of what still needs to be done.<br />
<br />
== Changes from the paper ==<br />
<br />
In the [http://dreixel.net/research/pdf/gdmh.pdf paper] we describe the implementation in [http://www.cs.uu.nl/wiki/UHC UHC]. The implementation in GHC is slightly different:<br />
<br />
* <tt>Representable0</tt> and <tt>Representable1</tt> have become <tt>Generic</tt> and <tt>Generic1</tt>, respectively. <tt>from0</tt>, <tt>to0</tt>, and <tt>Rep0</tt> also lost the <tt>0</tt> at the end of their names.<br />
<br />
* We are using type families, so the <tt>Generic</tt> and <tt>Generic1</tt> type classes have only one type argument. So, in GHC the classes look like what we describe in the "Avoiding extensions" part of Section 2.3 of the paper. This change affects only a generic function writer, and not a generic function user.<br />
<br />
* Default definitions (Section 3.3) work differently. In GHC we don't use a <tt>DERIVABLE</tt> pragma; instead, a type class can declare a ''generic default method'', which is akin to a standard default method, but includes a default type signature. This removes the need for a separate default definition and a pragma. For example, the <tt>Encode</tt> class of Section 3.1 is now:<br />
<haskell><br />
class Encode a where<br />
encode :: a -> [Bit]<br />
default encode :: (Generic a, Encode1 (Rep a)) => a -> [Bit]<br />
encode = encode1 . from<br />
</haskell><br />
<br />
* To derive generic functionality to a user type, the user no longer uses <hask>deriving instance</hask> (Section 4.6.1). Instead, the user gives an instance without defining the method; GHC then uses the generic default. For instance:<br />
<haskell><br />
instance Encode [a] -- works if there is an instance Generic [a]<br />
</haskell><br />
<br />
== Limitations ==<br />
<br />
We cannot derive <tt>Generic</tt> instances for:<br />
* Datatypes with a context;<br />
* Existentially-quantified datatypes;<br />
* GADTs.<br />
<br />
<br />
[[Category:GHC]]<br />
[[Category:Language extensions]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=GHC.Generics&diff=59232GHC.Generics2015-01-26T16:03:38Z<p>Dfeuer: Not so new.</p>
<hr />
<div>GHC 7.2 includes improved support for ''datatype-generic programming'' through two features, enabled with two flags: <tt>DeriveGeneric</tt> and <tt>DefaultSignatures</tt>. We show how this all works in this page, starting with a detailed example.<br />
<br />
== Example: serialization ==<br />
<br />
Suppose you are writing a class for serialization of data. You have a type <hask>Bit</hask> representing bits, and a class <hask>Serialize</hask>:<br />
<br />
<haskell><br />
data Bit = O | I<br />
<br />
class Serialize a where<br />
put :: a -> [Bit]<br />
</haskell><br />
<br />
You might have written some instances already:<br />
<br />
<haskell><br />
instance Serialize Int where<br />
put i = serializeInt i<br />
<br />
instance Serialize a => Serialize [a] where<br />
put [] = []<br />
put (h:t) = put h ++ put t<br />
</haskell><br />
<br />
A user of your library, however, will have his own datatypes, like:<br />
<br />
<haskell><br />
data UserTree a = Node a (UserTree a) (UserTree a) | Leaf<br />
</haskell><br />
<br />
He will have to specify an <hask>instance Serialize (UserTree a) where ...</hask> himself. This, however, is tedious, especially because most instances will probably be rather trivial, and should be derived automatically.<br />
<br />
It is here that generic programming can help you. If you are familiar with [http://www.cs.uu.nl/wiki/bin/view/GenericProgramming/SYB SYB] you could use it at this stage, but now we'll see how to do this with the new features of GHC 7.2.<br />
<br />
=== Generic serialization ===<br />
<br />
First you have to tell the compiler how to serialize any datatype, in general. Since Haskell datatypes have a regular structure, this means you can just explain how to serialize a few basic datatypes.<br />
<br />
==== Representation types ====<br />
<br />
We can represent most Haskell datatypes using only the following primitive types:<br />
<br />
<haskell><br />
-- | Unit: used for constructors without arguments<br />
data U1 p = U1<br />
<br />
-- | Constants, additional parameters and recursion of kind *<br />
newtype K1 i c p = K1 { unK1 :: c }<br />
<br />
-- | Meta-information (constructor names, etc.)<br />
newtype M1 i c f p = M1 { unM1 :: f p }<br />
<br />
-- | Sums: encode choice between constructors<br />
infixr 5 :+:<br />
data (:+:) f g p = L1 (f p) | R1 (g p)<br />
<br />
-- | Products: encode multiple arguments to constructors<br />
infixr 6 :*:<br />
data (:*:) f g p = f p :*: g p<br />
</haskell><br />
<br />
For starters, try to ignore the <tt>p</tt> parameter in all types; it's there just for future compatibility. The easiest way to understand how you can use these types to represent others is to see an example. Let's represent the <hask>UserTree</hask> type shown before:<br />
<br />
<haskell><br />
type RepUserTree a =<br />
-- A UserTree is either a Leaf, which has no arguments<br />
U1<br />
-- ... or it is a Node, which has three arguments that we put in a product<br />
:+: a :*: UserTree a :*: UserTree a<br />
</haskell><br />
<br />
Simple, right? Different constructors become alternatives of a sum, and multiple arguments become products. In fact, we want to have some more information in the representation, like datatype and constructor names, and to know if a product argument is a parameter or a type. We use the other primitives for this, and the representation looks more like:<br />
<br />
<haskell><br />
type RealRepUserTree a =<br />
-- Information about the datatype<br />
M1 D Data_UserTree (<br />
-- Leaf, with information about the constructor<br />
M1 C Con_Leaf U1<br />
-- Node, with information about the constructor<br />
:+: M1 C Con_Node (<br />
-- Constructor argument, which could have information<br />
-- about a record selector label<br />
M1 S NoSelector (<br />
-- Argument, tagged with P because it is a parameter<br />
K1 P a)<br />
-- Another argument, tagged with R because it is <br />
-- a recursive occurrence of a type<br />
:*: M1 S NoSelector (K1 R (UserTree a))<br />
-- Idem<br />
:*: M1 S NoSelector (K1 R (UserTree a))<br />
))<br />
</haskell><br />
<br />
A bit more complicated, but essentially the same. Datatypes like <hask>Data_UserTree</hask> are empty datatypes used only for providing meta-information in the representation; you don't have to worry much about them for now. Also, GHC generates these representations for you automatically, so you should never have to define them yourself! All of this is explained in much more detail in Section 2.1. of [http://dreixel.net/research/pdf/gdmh.pdf the original paper describing the new generic deriving mechanism].<br />
<br />
==== A generic function ====<br />
<br />
Since GHC can represent user types using only those primitive types, all you have to do is to tell GHC how to serialize each of the individual primitive types. The best way to do that is to create a new type class:<br />
<br />
<haskell><br />
class GSerialize f where<br />
gput :: f a -> [Bit]<br />
</haskell><br />
<br />
This class looks very much like the original <hask>Serialize</hask> class, just that the type argument is of kind <hask>* -> *</hask>, since our generic representation types have this <tt>p</tt> parameter lying around. Now we need to give instances for each of the basic types. For units there's nothing to serialize:<br />
<br />
<haskell><br />
instance GSerialize U1 where<br />
gput U1 = []<br />
</haskell><br />
<br />
The serialization of multiple arguments is simply the concatenation of each of the individual serializations:<br />
<br />
<haskell><br />
instance (GSerialize a, GSerialize b) => GSerialize (a :*: b) where<br />
gput (a :*: b) = gput a ++ gput b<br />
</haskell><br />
<br />
The case for sums is the most interesting, as we have to record which alternative we are in. We will use a 0 for left injections and a 1 for right injections:<br />
<br />
<haskell><br />
instance (GSerialize a, GSerialize b) => GSerialize (a :+: b) where<br />
gput (L1 x) = O : gput x<br />
gput (R1 x) = I : gput x<br />
</haskell><br />
<br />
We don't need to encode the meta-information, so we just go over it recursively :<br />
<br />
<haskell><br />
instance (GSerialize a) => GSerialize (M1 i c a) where<br />
gput (M1 x) = gput x<br />
</haskell><br />
<br />
Finally, we're only left with the arguments. For these we will just use our first class, <hask>Serialize</hask>, again:<br />
<br />
<haskell><br />
instance (Serialize a) => GSerialize (K1 i a) where<br />
gput (K1 x) = put x<br />
</haskell><br />
<br />
So, if a user datatype has a parameter which is instantiated to <hask>Int</hask>, at this stage we will use the library instance for <hask>Serialize Int</hask>.<br />
<br />
==== Default implementations ====<br />
<br />
We've seen how to represent user types generically, and how to define functions on representation types. However, we still have to tie these two together, explaining how to convert user types to their representation and then applying the generic function.<br />
<br />
The representation <hask>RepUserTree</hask> we have seen earlier is only one component of the representation; we also need functions to convert to and from the user datatype into the representation. For that we use another type class:<br />
<br />
<haskell><br />
class Generic a where<br />
-- Encode the representation of a user datatype<br />
type Rep a :: * -> *<br />
-- Convert from the datatype to its representation<br />
from :: a -> (Rep a) x<br />
-- Convert from the representation to the datatype<br />
to :: (Rep a) x -> a<br />
</haskell><br />
<br />
So, for the <hask>UserTree</hask> datatype shown before, GHC generates the following instance:<br />
<br />
<haskell><br />
instance Generic (UserTree a) where<br />
type Rep (UserTree a) = RepUserTree a<br />
<br />
from Leaf = L1 U1<br />
from (Node a l r) = R1 (a :*: l :*: r)<br />
<br />
to (L1 U1) = Leaf<br />
to (R1 (a :*: l :*: r)) = Node a l r<br />
</haskell><br />
<br />
(Note that we are using the simpler representation <hask>RepUserTree</hask> instead of the real representation <hask>RealRepUserTree</hask>, just for simplicity.)<br />
<br />
Equipped with a <hask>Generic</hask> instance, we are ready to tell the compiler how it can serialize any representable type:<br />
<br />
<haskell><br />
putDefault :: (Generic a, GSerialize (Rep a)) => a -> [Bit]<br />
putDefault a = gput (from a)<br />
</haskell><br />
<br />
The type of <hask>putDefault</hask> says that we can serialize any <tt>a</tt> into a list of bits, as long as that <tt>a</tt> is <hask>Generic</hask>, and its representation <hask>Rep a</hask> has a <hask>GSerialize</hask> instance. The implementation is very simple: first convert the value to its representation using <hask>from</hask>, and then call <hask>gput</hask> on that representation.<br />
<br />
However, we still have to write a <hask>Serialize</hask> instance for the user dataype:<br />
<br />
<haskell><br />
instance (Serialize a) => Serialize (UserTree a) where<br />
put = putDefault<br />
</haskell><br />
<br />
=== Using GHC's new features ===<br />
<br />
What we have seen so far could all already be done, at the cost of writing a lot of boilerplate code yourself (or spending hours writing [[Template Haskell]] code to do it for you). Now we'll see how the new features of GHC can help you.<br />
<br />
==== Deriving representations ====<br />
<br />
The <hask>Generic</hask> class, and all the representation types, come with GHC in the <tt>GHC.Generics</tt> module. GHC can also derive <hask>Generic</hask> for user types, so all the user has to do is:<br />
<br />
<haskell><br />
{-# LANGUAGE DeriveGeneric #-}<br />
<br />
data UserTree a = Node a (UserTree a) (UserTree a) | Leaf<br />
deriving Generic<br />
</haskell><br />
<br />
(Standalone deriving also works fine, and you can use it for types you have not defined yourself, but are imported from somewhere else.) You will need the new <tt>DeriveGeneric</tt> language pragma.<br />
<br />
==== More general default methods ====<br />
<br />
We don't want the user to have to write the <hask>instance Serialize (UserTree a)</hask> himself, since most of the times it will just be <hask>putDefault</hask>. However, we cannot make <hask>putDefault</hask> the default implementation of the <hask>put</hask> method, because that would require adding the <hask>(Generic a, GSerialize (Rep a))</hask> constraint to the class head. This would restrict the ability to give ad-hoc instances for types that are not representable, for instance.<br />
<br />
We solved this by allowing the user to give a different signature for default methods:<br />
<br />
<haskell><br />
{-# LANGUAGE DefaultSignatures #-}<br />
<br />
class Serialize a where<br />
put :: a -> [Bit]<br />
<br />
default put :: (Generic a, GSerialize (Rep a)) => a -> [Bit]<br />
put a = gput (from a)<br />
</haskell><br />
<br />
With the new language pragma <tt>DefaultSignatures</tt>, GHC allows you to put the keyword <hask>default</hask> before a (new) type signature for a method inside a class declaration. If you give such a default type signature, then you have to provide a default method implementation, which will be type-checked using the default signature, and not the original one.<br />
<br />
Now the user can simply write:<br />
<br />
<haskell><br />
instance (Serialize a) => Serialize (UserTree a)<br />
</haskell><br />
<br />
GHC fills out the implementation for <hask>put</hask> using the default method. It will type-check correctly because we have a <hask>Generic</hask> instance for <hask>UserTree</hask>, and <hask>GSerialize</hask> instances for all the representation types.<br />
<br />
=== Complete working example ===<br />
<br />
<haskell><br />
{-# LANGUAGE DefaultSignatures, DeriveGeneric, TypeOperators, FlexibleContexts #-}<br />
<br />
import GHC.Generics<br />
import Data.Bits<br />
<br />
<br />
data Bit = O | I deriving Show<br />
<br />
class Serialize a where<br />
put :: a -> [Bit]<br />
<br />
default put :: (Generic a, GSerialize (Rep a)) => a -> [Bit]<br />
put a = gput (from a)<br />
<br />
get :: [Bit] -> (a, [Bit])<br />
<br />
default get :: (Generic a, GSerialize (Rep a)) => [Bit] -> (a, [Bit])<br />
get xs = (to x, xs')<br />
where (x, xs') = gget xs<br />
<br />
class GSerialize f where<br />
gput :: f a -> [Bit]<br />
gget :: [Bit] -> (f a, [Bit])<br />
<br />
-- | Unit: used for constructors without arguments<br />
instance GSerialize U1 where<br />
gput U1 = []<br />
gget xs = (U1, xs)<br />
<br />
-- | Constants, additional parameters and recursion of kind *<br />
instance (GSerialize a, GSerialize b) => GSerialize (a :*: b) where<br />
gput (a :*: b) = gput a ++ gput b<br />
gget xs = (a :*: b, xs'')<br />
where (a, xs') = gget xs<br />
(b, xs'') = gget xs'<br />
<br />
-- | Meta-information (constructor names, etc.)<br />
instance (GSerialize a, GSerialize b) => GSerialize (a :+: b) where<br />
gput (L1 x) = O : gput x<br />
gput (R1 x) = I : gput x<br />
gget (O:xs) = (L1 x, xs')<br />
where (x, xs') = gget xs<br />
gget (I:xs) = (R1 x, xs')<br />
where (x, xs') = gget xs<br />
<br />
-- | Sums: encode choice between constructors<br />
instance (GSerialize a) => GSerialize (M1 i c a) where<br />
gput (M1 x) = gput x<br />
gget xs = (M1 x, xs')<br />
where (x, xs') = gget xs<br />
<br />
-- | Products: encode multiple arguments to constructors<br />
instance (Serialize a) => GSerialize (K1 i a) where<br />
gput (K1 x) = put x<br />
gget xs = (K1 x, xs')<br />
where (x, xs') = get xs<br />
<br />
instance Serialize Bool where<br />
put True = [I]<br />
put False = [O]<br />
get (I:xs) = (True, xs)<br />
get (O:xs) = (False, xs)<br />
<br />
--<br />
-- Try it out. (Normally this would be in a separate module.)<br />
--<br />
<br />
data UserTree a = Node a (UserTree a) (UserTree a) | Leaf<br />
deriving (Generic, Show)<br />
<br />
instance (Serialize a) => Serialize (UserTree a)<br />
<br />
main = do<br />
let xs = put True<br />
print (fst . get $ xs :: Bool)<br />
let ys = put (Leaf :: UserTree Bool)<br />
print (fst . get $ ys :: UserTree Bool)<br />
let zs = put (Node False Leaf Leaf :: UserTree Bool)<br />
print (fst . get $ zs :: UserTree Bool)<br />
</haskell><br />
<br />
== Different perspectives ==<br />
<br />
We outline the changes introduced in 7.2 regarding support for generic programming from the perspective of three different types of users: the end-user, the generic programmer, and the GHC hacker.<br />
<br />
=== The end-user ===<br />
<br />
If you know nothing about generic programming and would like to keep it that way, then you will be pleased to know that using generics in GHC 7.2 is easier than ever. As soon as you encounter a class with a default signature (like <tt>Serialize</tt> above), you will be able to give empty instances for your datatypes, like this:<br />
<br />
<haskell><br />
instance (Serialize a) => Serialize (UserTree a)<br />
</haskell><br />
<br />
You will need to add a <hask>deriving Generic</hask> clause to each datatype that you want to have generic implementations for. You might have datatypes that use other datatypes, and you might need <tt>Generic</tt> instances for those too. In that case, you can import the module where the datatype is defined and give a standalone deriving <tt>Generic</tt> instance. In either case, you will need the <tt>-XDeriveGeneric</tt> flag.<br />
<br />
=== The generic programmer ===<br />
<br />
If you are a library author and are eager to make your classes easy to instantiate by your users, then you should invest some time in defining instances for each of the representation types of <tt>GHC.Generics</tt> and defining a generic default method. See the example for <tt>Serialize</tt> above, and the [http://dreixel.net/research/pdf/gdmh.pdf original paper] for many other examples (but make sure to check the [[#Changes from the paper|changes from the paper]]).<br />
<br />
=== The GHC hacker ===<br />
<br />
If you are working on the GHC source code, you might find it useful to know what kind of changes were made. There is a [http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/GenericDeriving Trac wiki page] with a lower-level overview of things and also keeping track of what still needs to be done.<br />
<br />
== Changes from the paper ==<br />
<br />
In the [http://dreixel.net/research/pdf/gdmh.pdf paper] we describe the implementation in [http://www.cs.uu.nl/wiki/UHC UHC]. The implementation in GHC is slightly different:<br />
<br />
* <tt>Representable0</tt> and <tt>Representable1</tt> have become <tt>Generic</tt> and <tt>Generic1</tt>, respectively. <tt>from0</tt>, <tt>to0</tt>, and <tt>Rep0</tt> also lost the <tt>0</tt> at the end of their names.<br />
<br />
* We are using type families, so the <tt>Generic</tt> and <tt>Generic1</tt> type classes have only one type argument. So, in GHC the classes look like what we describe in the "Avoiding extensions" part of Section 2.3 of the paper. This change affects only a generic function writer, and not a generic function user.<br />
<br />
* Default definitions (Section 3.3) work differently. In GHC we don't use a <tt>DERIVABLE</tt> pragma; instead, a type class can declare a ''generic default method'', which is akin to a standard default method, but includes a default type signature. This removes the need for a separate default definition and a pragma. For example, the <tt>Encode</tt> class of Section 3.1 is now:<br />
<haskell><br />
class Encode a where<br />
encode :: a -> [Bit]<br />
default encode :: (Generic a, Encode1 (Rep a)) => a -> [Bit]<br />
encode = encode1 . from<br />
</haskell><br />
<br />
* To derive generic functionality to a user type, the user no longer uses <hask>deriving instance</hask> (Section 4.6.1). Instead, the user gives an instance without defining the method; GHC then uses the generic default. For instance:<br />
<haskell><br />
instance Encode [a] -- works if there is an instance Generic [a]<br />
</haskell><br />
<br />
== Limitations ==<br />
<br />
We cannot derive <tt>Generic</tt> instances for:<br />
* Datatypes with a context;<br />
* Existentially-quantified datatypes;<br />
* GADTs.<br />
<br />
<br />
[[Category:GHC]]<br />
[[Category:Language extensions]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/14&diff=5917899 questions/Solutions/142014-12-28T20:45:29Z<p>Dfeuer: </p>
<hr />
<div>(*) Duplicate the elements of a list.<br />
<br />
<haskell><br />
dupli [] = []<br />
dupli (x:xs) = x:x:dupli xs<br />
</haskell><br />
<br />
or, using list comprehension syntax:<br />
<br />
<haskell><br />
dupli list = concat [[x,x] | x <- list]<br />
</haskell><br />
<br />
or, using the list monad:<br />
<haskell><br />
dupli xs = xs >>= (\x -> [x,x])<br />
</haskell><br />
<br />
or, using the list instance of <hask>Applicative</hask>:<br />
<haskell><br />
dupli = (<**> [id,id])<br />
</haskell><br />
<br />
or, using <hask>concatMap</hask>:<br />
<haskell><br />
dupli = concatMap (\x -> [x,x])<br />
</haskell><br />
<br />
also using <hask>concatMap</hask>:<br />
<haskell><br />
dupli = concatMap (replicate 2)<br />
</haskell><br />
<br />
or, using <hask>foldl</hask>:<br />
<haskell><br />
dupli = foldl (\acc x -> acc ++ [x,x]) []<br />
</haskell><br />
<br />
or, using <hask>foldr</hask>:<br />
<haskell><br />
dupli = foldr (\ x xs -> x : x : xs) []<br />
</haskell><br />
<br />
or, using silliness:<br />
<haskell><br />
dupli = foldr (\x -> (x:) . (x:)) []<br />
</haskell><br />
<br />
or, even sillier:<br />
<haskell><br />
dupli = foldr ((.) <$> (:) <*> (:)) []<br />
</haskell><br />
<br />
and here is the proof that <hask>((.) <$> (:) <*> (:)) = (\y z -> y:y:z)</hask>:<br />
<br />
<haskell><br />
(.) <$> (:) <*> (:) =<br />
((.) <$> (:)) <*> (:) = -- (<$>) is infixl 4, (<*>) is infixl 4<br />
((.) . (:)) <*> (:) = -- (<$>) == (.) for functions<br />
(\x -> (.) (x:)) <*> (:) = -- definition of (.)<br />
\y -> (\x -> (.) (x:)) y (y:) = -- definition of (<*>) for functions<br />
\y -> ((.) (y:)) (y:) = -- beta reduction (applying y to (\x -> (.) (x:)))<br />
\y -> (y:) . (y:) = -- changing (.) to its prefix form<br />
\y -> (\z -> (y:) ((y:) z)) = -- definition of (.)<br />
\y z -> y:y:z -- making it look nicer<br />
</haskell><br />
<br />
<br />
[[Category:Programming exercise spoilers]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/14&diff=5917799 questions/Solutions/142014-12-28T20:42:47Z<p>Dfeuer: </p>
<hr />
<div>(*) Duplicate the elements of a list.<br />
<br />
<haskell><br />
dupli [] = []<br />
dupli (x:xs) = x:x:dupli xs<br />
</haskell><br />
<br />
or, using list comprehension syntax:<br />
<br />
<haskell><br />
dupli list = concat [[x,x] | x <- list]<br />
</haskell><br />
<br />
or, using the list monad:<br />
<haskell><br />
dupli xs = xs >>= (\x -> [x,x])<br />
</haskell><br />
<br />
or, using the list instance of <hask>Applicative</hask>:<br />
<haskell><br />
duplicate = (<**> [id,id])<br />
</haskell><br />
<br />
or, using <hask>concatMap</hask>:<br />
<haskell><br />
dupli = concatMap (\x -> [x,x])<br />
</haskell><br />
<br />
also using <hask>concatMap</hask>:<br />
<haskell><br />
dupli = concatMap (replicate 2)<br />
</haskell><br />
<br />
or, using <hask>foldl</hask>:<br />
<haskell><br />
dupli = foldl (\acc x -> acc ++ [x,x]) []<br />
</haskell><br />
<br />
or, using <hask>foldr</hask>:<br />
<haskell><br />
dupli = foldr (\ x xs -> x : x : xs) []<br />
</haskell><br />
<br />
or, using silliness:<br />
<haskell><br />
dupli = foldr (\x -> (x:) . (x:)) []<br />
</haskell><br />
<br />
or, even sillier:<br />
<haskell><br />
dupli = foldr ((.) <$> (:) <*> (:)) []<br />
</haskell><br />
<br />
and here is the proof that <hask>((.) <$> (:) <*> (:)) = (\y z -> y:y:z)</hask>:<br />
<br />
<haskell><br />
(.) <$> (:) <*> (:) =<br />
((.) <$> (:)) <*> (:) = -- (<$>) is infixl 4, (<*>) is infixl 4<br />
((.) . (:)) <*> (:) = -- (<$>) == (.) for functions<br />
(\x -> (.) (x:)) <*> (:) = -- definition of (.)<br />
\y -> (\x -> (.) (x:)) y (y:) = -- definition of (<*>) for functions<br />
\y -> ((.) (y:)) (y:) = -- beta reduction (applying y to (\x -> (.) (x:)))<br />
\y -> (y:) . (y:) = -- changing (.) to its prefix form<br />
\y -> (\z -> (y:) ((y:) z)) = -- definition of (.)<br />
\y z -> y:y:z -- making it look nicer<br />
</haskell><br />
<br />
<br />
[[Category:Programming exercise spoilers]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/12&diff=5917699 questions/Solutions/122014-12-28T20:29:34Z<p>Dfeuer: </p>
<hr />
<div>(**) Decode a run-length encoded list. <br />
<br />
Given a run-length code list generated as specified in problem 11. Construct its uncompressed version.<br />
<br />
<haskell><br />
decodeModified :: [ListItem a] -> [a]<br />
decodeModified = concatMap decodeHelper<br />
where<br />
decodeHelper (Single x) = [x]<br />
decodeHelper (Multiple n x) = replicate n x<br />
</haskell><br />
<br />
We only need to map single instances of an element to a list containing only one element and multiple ones to a list containing the specified number of elements and concatenate these lists.<br />
<br />
A solution for the simpler encoding from problem 10 can be given as:<br />
<br />
<haskell><br />
decode :: [(Int, a)] -> [a]<br />
decode = concatMap (uncurry replicate)<br />
</haskell><br />
<br />
This can be easily extended given a helper function:<br />
<br />
<haskell><br />
toTuple :: ListItem a -> (Int, a)<br />
toTuple (Single x) = (1, x)<br />
toTuple (Multiple n x) = (n, x)<br />
</haskell><br />
<br />
as:<br />
<br />
<haskell><br />
decodeModified :: [ListItem a] -> [a]<br />
decodeModified = concatMap (uncurry replicate . toTuple)<br />
</haskell><br />
<br />
a naïve solution with <hask>foldl</hask>:<br />
<haskell><br />
decodeModified :: [ListItem a]-> [a]<br />
decodeModified = foldl (\x y -> x ++ decodeHelper y) []<br />
where<br />
decodeHelper :: ListItem a -> [a]<br />
decodeHelper (Single x) = [x]<br />
decodeHelper (Multiple n x) = replicate n x<br />
</haskell><br />
<br />
<hask>foldl</hask> can also be used to solve this problem:<br />
<br />
<haskell><br />
decodeModified :: [ListItem a] -> [a]<br />
decodeModified = foldl (\acc e -> case e of Single x -> acc ++ [x]; Multiple n x -> acc ++ replicate n x) []<br />
</haskell><br />
<br />
<br />
Another way to decode the simplified encoding (which encoding, in the opinion of this editor, is a far more sensible one for Haskell):<br />
<br />
<haskell><br />
decode :: Eq a => [(Int,a)] -> [a]<br />
decode xs = foldr f [] xs<br />
where<br />
f (1, x) r = x : r<br />
f (k, x) r = x : f (k-1, x) r<br />
</haskell><br />
<br />
Or, to make it a good transformer for list fusion,<br />
<br />
<haskell><br />
{-# INLINE decode #-}<br />
decode :: Eq a => [(Int,a)] -> [a]<br />
decode xs = build (\c n -><br />
let<br />
f (1, x) r = x `c` r<br />
f (k, x) r = x `c` f (k-1, x) r<br />
in<br />
foldr f n xs)<br />
</haskell><br />
<br />
[[Category:Programming exercise spoilers]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/12&diff=5917599 questions/Solutions/122014-12-28T20:16:47Z<p>Dfeuer: </p>
<hr />
<div>(**) Decode a run-length encoded list. <br />
<br />
Given a run-length code list generated as specified in problem 11. Construct its uncompressed version.<br />
<br />
<haskell><br />
decodeModified :: [ListItem a] -> [a]<br />
decodeModified = concatMap decodeHelper<br />
where<br />
decodeHelper (Single x) = [x]<br />
decodeHelper (Multiple n x) = replicate n x<br />
</haskell><br />
<br />
We only need to map single instances of an element to a list containing only one element and multiple ones to a list containing the specified number of elements and concatenate these lists.<br />
<br />
A solution for the simpler encoding from problem 10 can be given as:<br />
<br />
<haskell><br />
decode :: [(Int, a)] -> [a]<br />
decode = concatMap (uncurry replicate)<br />
</haskell><br />
<br />
This can be easily extended given a helper function:<br />
<br />
<haskell><br />
toTuple :: ListItem a -> (Int, a)<br />
toTuple (Single x) = (1, x)<br />
toTuple (Multiple n x) = (n, x)<br />
</haskell><br />
<br />
as:<br />
<br />
<haskell><br />
decodeModified :: [ListItem a] -> [a]<br />
decodeModified = concatMap (uncurry replicate . toTuple)<br />
</haskell><br />
<br />
a naïve solution with <hask>foldl</hask>:<br />
<haskell><br />
decodeModified :: [ListItem a]-> [a]<br />
decodeModified = foldl (\x y -> x ++ decodeHelper y) []<br />
where<br />
decodeHelper :: ListItem a -> [a]<br />
decodeHelper (Single x) = [x]<br />
decodeHelper (Multiple n x) = replicate n x<br />
</haskell><br />
<br />
<hask>foldl</hask> can also be used to solve this problem:<br />
<br />
<haskell><br />
decodeModified :: [ListItem a] -> [a]<br />
decodeModified = foldl (\acc e -> case e of Single x -> acc ++ [x]; Multiple n x -> acc ++ replicate n x) []<br />
</haskell><br />
<br />
<br />
Another way to decode the simplified encoding (which in the opinion of this editor is a far more sensible one for Haskell):<br />
<br />
<haskell><br />
decode :: Eq a => [(Int,a)] -> [a]<br />
decode xs = foldr f [] xs<br />
where<br />
f (1, x) r = x : r<br />
f (k, x) r = x : f (k-1, x) r<br />
</haskell><br />
<br />
Or, to make it a good transformer for list fusion,<br />
<br />
<haskell><br />
{-# INLINE decode #-}<br />
decode :: Eq a => [(Int,a)] -> [a]<br />
decode xs = build (\c n -><br />
let<br />
f (1, x) r = x `c` r<br />
f (k, x) r = x `c` f (k-1, x) r<br />
in<br />
foldr f n xs)<br />
</haskell><br />
<br />
[[Category:Programming exercise spoilers]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/10&diff=5917499 questions/Solutions/102014-12-28T19:29:39Z<p>Dfeuer: </p>
<hr />
<div>(*) Run-length encoding of a list.<br />
<br />
Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as lists (N E) where N is the number of duplicates of the element E.<br />
<br />
<haskell><br />
encode xs = map (\x -> (length x,head x)) (group xs)<br />
</haskell><br />
<br />
which can also be expressed as a list comprehension:<br />
<br />
<haskell><br />
[(length x, head x) | x <- group xs]<br />
</haskell><br />
<br />
Or writing it [[Pointfree]] (Note that the type signature is essential here to avoid hitting the [[Monomorphism Restriction]]):<br />
<br />
<haskell><br />
encode :: Eq a => [a] -> [(Int, a)]<br />
encode = map (\x -> (length x, head x)) . group<br />
</haskell><br />
<br />
Or (ab)using the "&&&" arrow operator for tuples:<br />
<br />
<haskell><br />
encode :: Eq a => [a] -> [(Int, a)]<br />
encode xs = map (length &&& head) $ group xs<br />
</haskell><br />
<br />
Or using the slightly more verbose (w.r.t. <hask>(&&&)</hask>) Applicative combinators:<br />
<br />
<haskell><br />
encode :: Eq a => [a] -> [(Int, a)]<br />
encode = map ((,) <$> length <*> head) . pack<br />
</haskell><br />
<br />
Or with the help of foldr (''pack'' is the resulting function from P09):<br />
<br />
<haskell><br />
encode xs = (enc . pack) xs<br />
where enc = foldr (\x acc -> (length x, head x) : acc) []<br />
</haskell><br />
<br />
Or using takeWhile and dropWhile:<br />
<br />
<haskell><br />
encode [] = []<br />
encode (x:xs) = (length $ x : takeWhile (==x) xs, x)<br />
: encode (dropWhile (==x) xs)<br />
</haskell><br />
<br />
Or without higher order functions:<br />
<br />
<haskell><br />
encode [] = []<br />
encode (x:xs) = encode' 1 x xs where<br />
encode' n x [] = [(n, x)]<br />
encode' n x (y:ys)<br />
| x == y = encode' (n + 1) x ys<br />
| otherwise = (n, x) : encode' 1 y ys<br />
</haskell><br />
<br />
Or we can make use of zip and group:<br />
<br />
<haskell> <br />
import List<br />
encode :: Eq a => [a] -> [(Int, a)]<br />
encode xs=zip (map length l) h where <br />
l = (group xs)<br />
h = map head l<br />
</haskell><br />
<br />
Or if we ignore the rule that we should use the result of P09,<br />
<br />
<haskell><br />
encode :: Eq a => [a] -> [(Int,a)]<br />
encode xs = foldr f final xs Nothing<br />
where<br />
f x r (Just a@(i,q)) | x == q = r (Just (i+1,q))<br />
| otherwise = a : r (Just (1, x))<br />
f x r Nothing = r (Just (1, x))<br />
<br />
final (Just a@(i,q)) = [a]<br />
final Nothing = []<br />
</haskell><br />
<br />
which can become a good transformer for list fusion like so:<br />
<br />
<haskell><br />
{-# INLINE encode #-}<br />
encode :: Eq a => [a] -> [(Int,a)]<br />
encode xs = build (\c n -><br />
let<br />
f x r (Just a@(i,q)) | x == q = r (Just (i+1,q))<br />
| otherwise = a `c` r (Just (1, x))<br />
f x r Nothing = r (Just (1, x))<br />
<br />
final (Just a@(i,q)) = a `c` n<br />
final Nothing = n<br />
<br />
in<br />
foldr f final xs Nothing)<br />
</haskell><br />
<br />
<br />
[[Category:Programming exercise spoilers]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/10&diff=5917399 questions/Solutions/102014-12-28T19:23:38Z<p>Dfeuer: </p>
<hr />
<div>(*) Run-length encoding of a list.<br />
<br />
Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as lists (N E) where N is the number of duplicates of the element E.<br />
<br />
<haskell><br />
encode xs = map (\x -> (length x,head x)) (group xs)<br />
</haskell><br />
<br />
which can also be expressed as a list comprehension:<br />
<br />
<haskell><br />
[(length x, head x) | x <- group xs]<br />
</haskell><br />
<br />
Or writing it [[Pointfree]] (Note that the type signature is essential here to avoid hitting the [[Monomorphism Restriction]]):<br />
<br />
<haskell><br />
encode :: Eq a => [a] -> [(Int, a)]<br />
encode = map (\x -> (length x, head x)) . group<br />
</haskell><br />
<br />
Or (ab)using the "&&&" arrow operator for tuples:<br />
<br />
<haskell><br />
encode :: Eq a => [a] -> [(Int, a)]<br />
encode xs = map (length &&& head) $ group xs<br />
</haskell><br />
<br />
Or using the slightly more verbose (w.r.t. <hask>(&&&)</hask>) Applicative combinators:<br />
<br />
<haskell><br />
encode :: Eq a => [a] -> [(Int, a)]<br />
encode = map ((,) <$> length <*> head) . pack<br />
</haskell><br />
<br />
Or with the help of foldr (''pack'' is the resulting function from P09):<br />
<br />
<haskell><br />
encode xs = (enc . pack) xs<br />
where enc = foldr (\x acc -> (length x, head x) : acc) []<br />
</haskell><br />
<br />
Or using takeWhile and dropWhile:<br />
<br />
<haskell><br />
encode [] = []<br />
encode (x:xs) = (length $ x : takeWhile (==x) xs, x)<br />
: encode (dropWhile (==x) xs)<br />
</haskell><br />
<br />
Or without higher order functions:<br />
<br />
<haskell><br />
encode [] = []<br />
encode (x:xs) = encode' 1 x xs where<br />
encode' n x [] = [(n, x)]<br />
encode' n x (y:ys)<br />
| x == y = encode' (n + 1) x ys<br />
| otherwise = (n, x) : encode' 1 y ys<br />
</haskell><br />
<br />
Or we can make use of zip and group:<br />
<br />
<haskell> <br />
import List<br />
encode :: Eq a => [a] -> [(Int, a)]<br />
encode xs=zip (map length l) h where <br />
l = (group xs)<br />
h = map head l<br />
</haskell><br />
<br />
Or if we ignore the rule that we should use the result of P09,<br />
<br />
<haskell><br />
encode :: Eq a => [a] -> [(Int,a)]<br />
encode xs = foldr f final xs Nothing<br />
where<br />
f x r (Just a@(i,q)) | x == q = r (Just (i+1,q))<br />
| otherwise = a : r (Just (1, x))<br />
f x r Nothing = r (Just (1, x))<br />
<br />
final (Just a@(i,q)) = [a]<br />
final Nothing = []<br />
</haskell><br />
<br />
which can become a good transformer for list fusion like so:<br />
<br />
<haskell><br />
encode :: Eq a => [a] -> [(Int,a)]<br />
encode xs = build (\c n -><br />
let<br />
f x r (Just a@(i,q)) | x == q = r (Just (i+1,q))<br />
| otherwise = a `c` r (Just (1, x))<br />
f x r Nothing = r (Just (1, x))<br />
<br />
final (Just a@(i,q)) = a `c` n<br />
final Nothing = n<br />
<br />
in<br />
foldr f final xs Nothing)<br />
</haskell><br />
<br />
<br />
[[Category:Programming exercise spoilers]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/8&diff=5917299 questions/Solutions/82014-12-28T18:57:36Z<p>Dfeuer: </p>
<hr />
<div>(**) Eliminate consecutive duplicates of list elements.<br />
<br />
<haskell><br />
compress :: Eq a => [a] -> [a]<br />
compress = map head . group<br />
</haskell><br />
<br />
We simply group equal values together (using Data.List.group), then take the head of each. <br />
Note that (with GHC) we must give an explicit type to ''compress'' otherwise we get:<br />
<br />
<haskell><br />
Ambiguous type variable `a' in the constraint:<br />
`Eq a'<br />
arising from use of `group' <br />
Possible cause: the monomorphism restriction applied to the following:<br />
compress :: [a] -> [a]<br />
Probable fix: give these definition(s) an explicit type signature<br />
or use -fno-monomorphism-restriction<br />
</haskell><br />
<br />
We can circumvent the monomorphism restriction by writing ''compress'' this way (See: section 4.5.4 of [http://haskell.org/onlinereport the report]):<br />
<br />
<haskell>compress xs = map head $ group xs</haskell><br />
<br />
An alternative solution is<br />
<br />
<haskell><br />
compress (x:ys@(y:_))<br />
| x == y = compress ys<br />
| otherwise = x : compress ys<br />
compress ys = ys<br />
</haskell><br />
<br />
A variation of the above using <hask>foldr</hask> (note that GHC erases the <hask>Maybe</hask>s, producing efficient code):<br />
<haskell><br />
compress xs = foldr f (const []) xs Nothing<br />
where<br />
f x r a@(Just q) | x == q = r a<br />
f x r _ = x : r (Just x)<br />
</haskell><br />
<br />
Another possibility using foldr (this one is not so efficient, because it pushes the whole input onto the "stack" before doing anything else):<br />
<br />
<haskell><br />
compress :: (Eq a) => [a] -> [a]<br />
compress = foldr skipDups []<br />
where skipDups x [] = [x]<br />
skipDups x acc<br />
| x == head acc = acc<br />
| otherwise = x : acc<br />
</haskell><br />
<br />
A very simple approach:<br />
<br />
<haskell><br />
compress [] = []<br />
compress (x:xs) = x : (compress $ dropWhile (== x) xs)<br />
</haskell><br />
<br />
Another approach, using foldr<br />
<br />
<haskell><br />
compress :: Eq a => [a] -> [a]<br />
compress x = foldr (\a b -> if a == (head b) then b else a:b) [last x] x<br />
</haskell><br />
<br />
Wrong solution using foldr<br />
<haskell><br />
compress :: Eq a => [a] -> [a]<br />
compress xs = foldr (\x acc -> if x `elem` acc then acc else x:acc) [] xs<br />
-- Main> compress [1, 1, 1, 2, 2, 1, 1]<br />
-- [2,1] - must be [1,2,1]<br />
</haskell><br />
<br />
<br />
<br />
and using foldl<br />
<br />
<haskell><br />
compress :: (Eq a) => [a] -> [a]<br />
compress x = foldl (\a b -> if (last a) == b then a else a ++ [b]) [head x] x<br />
compress' x = reverse $ foldl (\a b -> if (head a) == b then a else b:a) [head x] x<br />
</haskell><br />
<br />
A crazy variation that acts as a good transformer for fold/build fusion<br />
<br />
<haskell><br />
{-# INLINE compress #-}<br />
compress :: Eq a => [a] -> [a]<br />
compress xs = build (\c n -><br />
let<br />
f x r a@(Just q) | x == q = r a<br />
f x r _ = x `c` r (Just x)<br />
in<br />
foldr f (const n) xs Nothing)<br />
</haskell></div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/8&diff=5917199 questions/Solutions/82014-12-28T18:18:37Z<p>Dfeuer: </p>
<hr />
<div>(**) Eliminate consecutive duplicates of list elements.<br />
<br />
<haskell><br />
compress :: Eq a => [a] -> [a]<br />
compress = map head . group<br />
</haskell><br />
<br />
We simply group equal values together (using Data.List.group), then take the head of each. <br />
Note that (with GHC) we must give an explicit type to ''compress'' otherwise we get:<br />
<br />
<haskell><br />
Ambiguous type variable `a' in the constraint:<br />
`Eq a'<br />
arising from use of `group' <br />
Possible cause: the monomorphism restriction applied to the following:<br />
compress :: [a] -> [a]<br />
Probable fix: give these definition(s) an explicit type signature<br />
or use -fno-monomorphism-restriction<br />
</haskell><br />
<br />
We can circumvent the monomorphism restriction by writing ''compress'' this way (See: section 4.5.4 of [http://haskell.org/onlinereport the report]):<br />
<br />
<haskell>compress xs = map head $ group xs</haskell><br />
<br />
An alternative solution is<br />
<br />
<haskell><br />
compress (x:ys@(y:_))<br />
| x == y = compress ys<br />
| otherwise = x : compress ys<br />
compress ys = ys<br />
</haskell><br />
<br />
A variation of the above using <hask>foldr</hask> (note that GHC erases the <hask>Maybe</hask>s, producing efficient code):<br />
<haskell><br />
compress xs = foldr f (const []) xs Nothing<br />
where<br />
f x r a@(Just q) | x == q = r a<br />
f x r _ = x : r (Just x)<br />
</haskell><br />
<br />
Another possibility using foldr (this one is not so efficient, because it pushes the whole input onto the "stack" before doing anything else):<br />
<br />
<haskell><br />
compress :: (Eq a) => [a] -> [a]<br />
compress = foldr skipDups []<br />
where skipDups x [] = [x]<br />
skipDups x acc<br />
| x == head acc = acc<br />
| otherwise = x : acc<br />
</haskell><br />
<br />
A very simple approach:<br />
<br />
<haskell><br />
compress [] = []<br />
compress (x:xs) = x : (compress $ dropWhile (== x) xs)<br />
</haskell><br />
<br />
Another approach, using foldr<br />
<br />
<haskell><br />
compress :: Eq a => [a] -> [a]<br />
compress x = foldr (\a b -> if a == (head b) then b else a:b) [last x] x<br />
</haskell><br />
<br />
Wrong solution using foldr<br />
<haskell><br />
compress :: Eq a => [a] -> [a]<br />
compress xs = foldr (\x acc -> if x `elem` acc then acc else x:acc) [] xs<br />
-- Main> compress [1, 1, 1, 2, 2, 1, 1]<br />
-- [2,1] - must be [1,2,1]<br />
</haskell><br />
<br />
<br />
<br />
and using foldl<br />
<br />
<haskell><br />
compress :: (Eq a) => [a] -> [a]<br />
compress x = foldl (\a b -> if (last a) == b then a else a ++ [b]) [head x] x<br />
compress' x = reverse $ foldl (\a b -> if (head a) == b then a else b:a) [head x] x<br />
</haskell></div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/7&diff=5916899 questions/Solutions/72014-12-28T17:54:12Z<p>Dfeuer: </p>
<hr />
<div>(**) Flatten a nested list structure.<br />
<br />
<haskell><br />
data NestedList a = Elem a | List [NestedList a]<br />
<br />
flatten :: NestedList a -> [a]<br />
flatten (Elem x) = [x]<br />
flatten (List x) = concatMap flatten x<br />
</haskell><br />
or without concatMap<br />
<haskell><br />
flatten :: NestedList a -> [a]<br />
flatten (Elem a ) = [a]<br />
flatten (List (x:xs)) = flatten x ++ flatten (List xs)<br />
flatten (List []) = []<br />
</haskell><br />
or using things that act just like <hask>concatMap</hask><br />
<haskell><br />
flatten (Elem x) = return x<br />
flatten (List x) = flatten =<< x<br />
<br />
flatten (Elem x) = [x]<br />
flatten (List x) = foldMap flatten x<br />
</haskell><br />
<haskell><br />
flatten2 :: NestedList a -> [a]<br />
flatten2 a = flt' a []<br />
where flt' (Elem x) xs = x:xs<br />
flt' (List (x:ls)) xs = flt' x (flt' (List ls) xs)<br />
flt' (List []) xs = xs<br />
</haskell><br />
or with foldr<br />
<haskell><br />
flatten3 :: NestedList a -> [a]<br />
flatten3 (Elem x ) = [x]<br />
flatten3 (List xs) = foldr (++) [] $ map flatten3 xs<br />
</haskell><br />
<br />
or with an accumulator function:<br />
<haskell><br />
flatten4 = reverse . rec []<br />
where<br />
rec acc (List []) = acc<br />
rec acc (Elem x) = x:acc<br />
rec acc (List (x:xs)) = rec (rec acc x) (List xs)<br />
</haskell><br />
<br />
We have to define a new data type, because lists in Haskell are homogeneous.<br />
[1, [2, [3, 4], 5]] is a type error. Therefore, we must have a way of<br />
representing a list that may (or may not) be nested.<br />
<br />
Our NestedList datatype is either a single element of some type (Elem a), or a<br />
list of NestedLists of the same type. (List [NestedList a]).</div>Dfeuerhttps://wiki.haskell.org/index.php?title=99_questions/Solutions/2&diff=5916799 questions/Solutions/22014-12-28T17:26:19Z<p>Dfeuer: </p>
<hr />
<div>(*) Find the last but one element of a list.<br />
<br />
<haskell><br />
myButLast :: [a] -> a<br />
myButLast = last . init<br />
<br />
myButLast' x = reverse x !! 1<br />
<br />
myButLast'' [x,_] = x<br />
myButLast'' (_:xs) = myButLast'' xs<br />
<br />
myButLast''' (x:(_:[])) = x<br />
myButLast''' (_:xs) = myButLast''' xs<br />
<br />
myButLast'''' = head . tail . reverse<br />
<br />
lastbut1 :: Foldable f => f a -> a<br />
lastbut1 = fst . foldl (\(a,b) x -> (b,x)) (err1,err2)<br />
where<br />
err1 = error "lastbut1: Empty list"<br />
err2 = error "lastbut1: Singleton"<br />
<br />
lastbut1safe :: Foldable f => f a -> Maybe a<br />
lastbut1safe = fst . foldl (\(a,b) x -> (b,Just x)) (Nothing,Nothing)<br />
</haskell><br />
<br />
<br />
[[Category:Programming exercise spoilers]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Hugs&diff=58985Hugs2014-10-10T07:53:36Z<p>Dfeuer: </p>
<hr />
<div>== Note ==<br />
<br />
The Hugs interpreter is not currently maintained, and has not been updated since late 2006. It is obsolete, and should no longer be used for either general or most educational purposes. The rest of this page should be seen as largely historical.<br />
<br />
<br />
<br />
* HuGs = Haskell User´s Gofer System, one of the [[:Category:Implementations |Haskell implementations]].<br />
* The Hugs home page is http://www.haskell.org/hugs<br />
* Hugs 98 is a functional programming system based on Haskell 98, the de facto standard for non-strict functional programming languages. Hugs 98 provides an almost complete implementation of Haskell 98.<br />
* Hugs provides most of the common extensions including scoped variables, multiparameter typeclasses, foreign function interface, etc.<br />
* Hugs is exceptionally portable. If Hugs can't be installed on your machine, report it as a bug!<br />
* Hugs comes with almost all the same libraries as GHC.<br />
* Hugs and GHC both work on portability between the two - most code that works on one compiler works on the other without change.<br />
<br />
Actually, on Ubuntu Linux, hugs is packed with more libraries:<br />
<br />
<haskell><br />
freeglut3 (2.6.0-0ubuntu2) ...<br />
Setting up libhugs-base-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haskell98-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up hugs (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-opengl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-openal-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-alut-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-cabal-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-mtl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-fgl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-glut-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haskell-src-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haxml-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-x11-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-hgl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-hunit-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-parsec-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-network-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-quickcheck-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-stm-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-time-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-unix-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-xhtml-bundled (98.200609.21-5ubuntu1) ...<br />
Processing triggers for libc-bin ...<br />
<br />
</haskell><br />
<br />
Hugs on Ubuntu Linux can be installed using:<br />
<br />
'''sudo apt-get install hugs'''<br />
<br />
[[Category:Implementations]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Category_theory/Natural_transformation&diff=58860Category theory/Natural transformation2014-09-23T03:17:50Z<p>Dfeuer: Make broken link point to Internet Archive. If the page actually has been migrated somewhere else here, that can be patched up.</p>
<hr />
<div><haskell><br />
map even $ maybeToList $ Just 5<br />
</haskell><br />
yields the same as<br />
<haskell><br />
maybeToList $ fmap even $ Just 5<br />
</haskell><br />
yields: both yield<br />
<haskell><br />
[False]<br />
</haskell><br />
<br />
In the followings, this example will be used to illustrate the notion of natural transformation. If the examples are exaggerated and/or the definitions are incomprehensible, try [[#External links]].<br />
<br />
== Definition ==<br />
<br />
* Let <math>\mathcal C</math>, <math>\mathcal D</math> denote categories.<br />
* Let <math>\Phi, \Psi : \mathcal C \to \mathcal D</math> be functors.<br />
* Let <math>X, Y \in \mathbf{Ob}(\mathcal C)</math>. Let <math>f \in \mathrm{Hom}_{\mathcal C}(X, Y)</math>.<br />
Let us define the <math>\eta : \Phi \to \Psi</math> natural transformation. It associates to each object of <math>\mathcal{C}</math> a morphism of <math>\mathcal{D}</math> in the following way (usually, not sets are discussed here, but proper classes, so I do not use term “function” for this <math>\mathbf{Ob}(\mathcal C) \to \mathbf{Mor}(\mathcal D)</math> mapping):<br />
* <math>\forall A \in \mathbf{Ob}(\mathcal C) \longmapsto \eta_A \in \mathrm{Hom}_{\mathcal D}(\Phi(A), \Psi(A))</math>. We call <math>\eta_A</math> the component of <math>\eta</math> at ''A''.<br />
* <math>\eta_Y \cdot \Phi(f) = \Psi(f) \cdot \eta_X</math><br />
Thus, the following diagram commutes (in <math>\mathcal D</math>):<br />
<br />
[[Image:natural_transformation.png|center]]<br />
<br />
== Example: <hask>maybeToList</hask> ==<br />
<br />
As already mentioned<br />
<haskell><br />
map even $ maybeToList $ Just 5<br />
</haskell><br />
yields the same as<br />
<haskell><br />
maybeToList $ fmap even $ Just 5<br />
</haskell><br />
yields: both yield<br />
<haskell><br />
[False]<br />
</haskell><br />
This example will be shown in the light of the above definition in the followings.<br />
<br />
=== Vertical arrows: sides of objects ===<br />
<br />
… showing how the natural transformation works.<br />
<br />
:<math>\eta : \Phi \to \Psi</math><br />
<haskell>maybeToList :: Maybe a -> [a]</haskell><br />
<br />
==== Left: side of ''X'' object ====<br />
<br />
{| Border=2 CellPadding=2 CellSpacing=2 | Dia<br />
|+ <math>\eta_X : \Phi(X) \to \Psi(X)</math><br />
|<br />
| <hask>maybeToList :: Maybe Int -> [Int]</hask><br />
|-<br />
| <hask>Nothing</hask><br />
| <hask>[]</hask><br />
|-<br />
| <hask>Just 0</hask><br />
| <hask>[0]</hask><br />
|-<br />
| <hask>Just 1</hask><br />
| <hask>[1]</hask><br />
|}<br />
<br />
==== Right: side of ''Y'' object ====<br />
<br />
{| Border=2 CellPadding=2 CellSpacing=2 | Dia<br />
|+ <math>\eta_Y : \Phi(Y) \to \Psi(Y)</math><br />
|<br />
| <hask>maybeToList :: Maybe Bool -> [Bool]</hask><br />
|-<br />
| <hask>Nothing</hask><br />
| <hask>[]</hask><br />
|-<br />
| <hask>Just True</hask><br />
| <hask>[True]</hask><br />
|-<br />
| <hask>Just False</hask><br />
| <hask>[False]</hask><br />
|}<br />
<br />
=== Horizontal arrows: sides of functors ===<br />
<br />
:<math>f : X \to Y</math><br />
<haskell><br />
even :: Int -> Bool<br />
</haskell><br />
<br />
==== Side of <math>\Phi</math> functor ====<br />
<br />
{| Border=2 CellPadding=2 CellSpacing=2<br />
|+ <math>\Phi(f) : \Phi(X) \to \Phi(Y)</math><br />
|<br />
| <hask>fmap even:: Maybe Int -> Maybe Bool</hask><br />
|-<br />
| <hask>Nothing</hask><br />
| <hask>Nothing</hask><br />
|-<br />
| <hask>Just 0</hask><br />
| <hask>Just True</hask><br />
|-<br />
| <hask>Just 1</hask><br />
| <hask>Just False</hask><br />
|}<br />
<br />
==== Side of <math>\Psi</math> functor ====<br />
<br />
{| Border=2 CellPadding=2 CellSpacing=2<br />
|+ <math>\Psi(f) : \Psi(X) \to \Psi(Y)</math><br />
|<br />
| <hask>map even:: [Int] -> [Bool]</hask><br />
|-<br />
| <hask>[]</hask><br />
| <hask>[]</hask><br />
|-<br />
| <hask>[0]</hask><br />
| <hask>[True]</hask><br />
|-<br />
| <hask>[1]</hask><br />
| <hask>[False]</hask><br />
|}<br />
<br />
=== Commutativity of the diagram ===<br />
<br />
:<math>\Psi(f) \cdot \eta_X = \eta_Y \cdot \Phi(f)</math><br />
both paths span between<br />
:<math>\Phi(X) \to \Psi(Y)</math><br />
<br />
{| Border=2 CellPadding=2 CellSpacing=2<br />
| RowSpan=2|<br />
| ColSpan=2|<hask>Maybe Int -> [Bool]</hask><br />
|-<br />
| <hask>map even . maybeToList</hask><br />
| <hask>maybeToList . fmap even</hask><br />
|-<br />
| <hask>Nothing</hask><br />
| <hask>[]</hask><br />
| <hask>[]</hask><br />
|-<br />
| <hask>Just 0</hask><br />
| <hask>[True]</hask><br />
| <hask>[True]</hask><br />
|-<br />
| <hask>Just 1</hask><br />
| <hask>[False]</hask><br />
| <hask>[False]</hask><br />
|}<br />
<br />
=== Remarks ===<br />
<br />
* <hask>even</hask> has a more general type (<hask>Integral a => a -> Bool</hask>) than described here<br />
* Words “side”, “horizontal”, “vertical”, “left”, “right” serve here only to point to the discussed parts of a diagram, thus, they are not part of the scientific terminology.<br />
* If You want to modify the [[#Definition|commutative diagram]], see its [[Media:Natural_transformation.tex|source code]] (in LaTeX using <code>amscd</code>).<br />
<br />
== Operations ==<br />
<br />
=== Mixed ===<br />
<br />
The “mixed” operations described below will be important also in understanding the definition of “monad” concept in category theory.<br />
<br />
==== Functor and natural transformation ====<br />
<br />
Let us imagine a parser library, which contains functions for parsing a form. There are two kinds of cells:<br />
* containing data which are optional (e.g. name of spouse)<br />
* containing data which consist of an enumeration of items (e.g. names of acquired languages)<br />
<br />
<haskell><br />
spouse :: Parser (Maybe String)<br />
languages :: Parser [String]<br />
</haskell><br />
<br />
Let us imagine we have any processing (storing, archiving etc.) function which processes lists (or any other reason which forces us to convert our results to list format and exclude any Maybe's). (Perhaps, all this example is impractical and exaggerated, because in real life we should solve the whole thing in other ways.)<br />
<br />
Thus, we want to build a parser combinator (we could notate it graphically with something like <math>\mathrm?\!\!\to\!\!\mathrm*</math>) which converts a “zero-ore-one-occurrence” like parser to a “zero-or-one-or-many-occurrences” like parser.<br />
<br />
We can convert <hask>Maybe</hask> to list with <hask>maybeToList</hask><br />
But if we want to do something similar with a ''parser'' on Maybe's to achieve a ''parser'' on list, then <hask>maybeToList</hask> is not enough alone, we must <hask>fmap</hask> it.<br />
E.g. if we want to convert a parser like <hask>spouse</hask> to be of the same type as <hask>languages</hask>:<br />
<haskell><br />
fmap maybeToList spouse<br />
</haskell><br />
Let us see the types:<br />
We start with<br />
<haskell><br />
spouse :: Parser (Maybe String)<br />
</haskell><br />
:<math>\Lambda(\Phi(X))</math><br />
or using notion of composing functors<br />
:<math>(\Lambda \Phi)(X)</math><br />
We want to achieve<br />
<haskell><br />
fmap maybeToList spouse :: Parser [String]<br />
</haskell><br />
:<math>\Lambda(\Psi(X))</math><br />
:<math>(\Lambda \Psi)(X)</math><br />
thus we can infer<br />
<haskell><br />
fmap maybeToList :: Parser (Maybe [String]) -> Parser [String]<br />
</haskell><br />
:<math>(\Lambda\eta)_X \in \mathrm{Hom}_{\mathcal D}((\Lambda\Phi)(X),\;(\Lambda\Psi)(X))</math><br />
In fact, we have a new “datatype converter”: converting not Maybe's to lists, but parser on Maybe to Parser on list. Let us notate the corresponding natural transformation with <math>\Lambda\eta</math>:<br />
:To each <math>X \in \mathbf{Ob}(\mathcal C)</math> we associate <math>(\Lambda\eta)_X \in \mathrm{Hom}_{\mathcal D}((\Lambda\Phi)(X),\;(\Lambda\Psi)(X))</math><br />
:<math>\Lambda\eta : \Lambda\Phi \to \Lambda\Psi</math><br />
:<math>(\Lambda\eta)_X = \Lambda(\eta_X)</math><br />
<br />
Summary:<br />
<br />
:Let <math>\mathcal C, \mathcal D, \mathcal E</math> be categories<br />
:<math>\Phi, \Psi : \mathcal C \to \mathcal D</math> functors<br />
:<math>\Lambda : \mathcal D \to \mathcal E</math> functor<br />
:<math>\eta : \Phi \to \Psi</math> natural transformation<br />
Then let us define a new natural transformation:<br />
:<math>\Lambda\eta : \Lambda\Phi \to \Lambda\Psi</math><br />
:<math>(\Lambda\eta)_X = \Lambda(\eta_X)</math><br />
<br />
==== Natural transformation and functor ====<br />
<br />
:Let <math>\mathcal C, \mathcal D, \mathcal E</math> be categories<br />
:<math>\Delta : \mathcal C \to \mathcal D</math> functor<br />
:<math>\Phi, \Psi : \mathcal D \to \mathcal E</math> functors<br />
:<math>\eta : \Phi \to \Psi</math> natural transformation<br />
Then let us define a new natural transformation:<br />
:<math>\eta\Delta : \Phi\Delta \to \Psi\Delta</math><br />
:<math>(\eta\Delta)_X = \eta_{\Delta(X)}</math><br />
<br />
It can be illustrated by Haskell examples, too. Understanding it is made harder (easier?) by the fact that Haskell's type inference “(dis)solves” the main point, thus there is no “materialized” manifestation of it.<br />
<br />
<haskell><br />
convert :: Maybe (Term a) -> [Term a]<br />
</haskell><br />
<br />
Unlike [[#Functor and natural transformation|seen at <math>\mathrm?\!\!\to\!\!\mathrm*</math>]], the definition of this converter will not show much novelty:<br />
<br />
<haskell><br />
convert = maybeToList<br />
</haskell><br />
<br />
the most interesting thing is done automatically by type inference.<br />
<br />
== External links ==<br />
* [https://web.archive.org/web/20060925043212/http://www.haskell.org/hawiki/CategoryTheory_2fNaturalTransformation The corresponding HaWiki article] is not migrated here yet, so You can see it for more information.<br />
* Wikipedia's [http://en.wikipedia.org/wiki/Natural_transformation Natural transformation] article<br />
* [http://www.case.edu/artsci/math/wells/pub/ttt.html Toposes, Triples and Theories] written by Michael Barr and Charles Wells.<br />
<br />
[[Category:Theoretical foundations]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Hugs&diff=58819Hugs2014-09-15T07:50:23Z<p>Dfeuer: /* Note */</p>
<hr />
<div>== Note ==<br />
<br />
The Hugs interpreter is not currently maintained, and has not been updated since late 2006. It is obsolete, and should no longer be used for either general or most educational purposes. The rest of this page should be seen as purely historical. The Hugs website no longer functions, but Hugs can still be downloaded from [https://web.archive.org/web/20140828005338/http://www.haskell.org/hugs/ The Internet Archive].<br />
<br />
<br />
<br />
* HuGs = Haskell User´s Gofer System, one of the [[:Category:Implementations |Haskell implementations]].<br />
* The Hugs home page is http://www.haskell.org/hugs<br />
* Hugs 98 is a functional programming system based on Haskell 98, the de facto standard for non-strict functional programming languages. Hugs 98 provides an almost complete implementation of Haskell 98.<br />
* Hugs provides most of the common extensions including scoped variables, multiparameter typeclasses, foreign function interface, etc.<br />
* Hugs is exceptionally portable. If Hugs can't be installed on your machine, report it as a bug!<br />
* Hugs comes with almost all the same libraries as GHC.<br />
* Hugs and GHC both work on portability between the two - most code that works on one compiler works on the other without change.<br />
<br />
Actually, on Ubuntu Linux, hugs is packed with more libraries:<br />
<br />
<haskell><br />
freeglut3 (2.6.0-0ubuntu2) ...<br />
Setting up libhugs-base-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haskell98-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up hugs (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-opengl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-openal-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-alut-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-cabal-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-mtl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-fgl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-glut-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haskell-src-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haxml-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-x11-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-hgl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-hunit-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-parsec-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-network-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-quickcheck-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-stm-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-time-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-unix-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-xhtml-bundled (98.200609.21-5ubuntu1) ...<br />
Processing triggers for libc-bin ...<br />
<br />
</haskell><br />
<br />
Hugs on Ubuntu Linux can be installed using:<br />
<br />
'''sudo apt-get install hugs'''<br />
<br />
[[Category:Implementations]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Hugs&diff=58818Hugs2014-09-15T07:47:17Z<p>Dfeuer: /* Note */</p>
<hr />
<div>== Note ==<br />
<br />
The Hugs interpreter is not currently maintained, and has not been updated since late 2006. It is obsolete, and should no longer be used for either general or most educational purposes. The rest of this page should be seen as purely historical.<br />
<br />
<br />
<br />
* HuGs = Haskell User´s Gofer System, one of the [[:Category:Implementations |Haskell implementations]].<br />
* The Hugs home page is http://www.haskell.org/hugs<br />
* Hugs 98 is a functional programming system based on Haskell 98, the de facto standard for non-strict functional programming languages. Hugs 98 provides an almost complete implementation of Haskell 98.<br />
* Hugs provides most of the common extensions including scoped variables, multiparameter typeclasses, foreign function interface, etc.<br />
* Hugs is exceptionally portable. If Hugs can't be installed on your machine, report it as a bug!<br />
* Hugs comes with almost all the same libraries as GHC.<br />
* Hugs and GHC both work on portability between the two - most code that works on one compiler works on the other without change.<br />
<br />
Actually, on Ubuntu Linux, hugs is packed with more libraries:<br />
<br />
<haskell><br />
freeglut3 (2.6.0-0ubuntu2) ...<br />
Setting up libhugs-base-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haskell98-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up hugs (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-opengl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-openal-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-alut-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-cabal-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-mtl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-fgl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-glut-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haskell-src-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haxml-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-x11-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-hgl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-hunit-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-parsec-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-network-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-quickcheck-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-stm-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-time-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-unix-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-xhtml-bundled (98.200609.21-5ubuntu1) ...<br />
Processing triggers for libc-bin ...<br />
<br />
</haskell><br />
<br />
Hugs on Ubuntu Linux can be installed using:<br />
<br />
'''sudo apt-get install hugs'''<br />
<br />
[[Category:Implementations]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Hugs&diff=58817Hugs2014-09-15T07:44:37Z<p>Dfeuer: </p>
<hr />
<div>== Note ==<br />
<br />
The Hugs interpreter is not currently maintained, and has not been updated in many years. It is obsolete, and should no longer be used for either general or most educational purposes. The rest of this page should be seen as purely historical.<br />
<br />
<br />
<br />
* HuGs = Haskell User´s Gofer System, one of the [[:Category:Implementations |Haskell implementations]].<br />
* The Hugs home page is http://www.haskell.org/hugs<br />
* Hugs 98 is a functional programming system based on Haskell 98, the de facto standard for non-strict functional programming languages. Hugs 98 provides an almost complete implementation of Haskell 98.<br />
* Hugs provides most of the common extensions including scoped variables, multiparameter typeclasses, foreign function interface, etc.<br />
* Hugs is exceptionally portable. If Hugs can't be installed on your machine, report it as a bug!<br />
* Hugs comes with almost all the same libraries as GHC.<br />
* Hugs and GHC both work on portability between the two - most code that works on one compiler works on the other without change.<br />
<br />
Actually, on Ubuntu Linux, hugs is packed with more libraries:<br />
<br />
<haskell><br />
freeglut3 (2.6.0-0ubuntu2) ...<br />
Setting up libhugs-base-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haskell98-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up hugs (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-opengl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-openal-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-alut-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-cabal-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-mtl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-fgl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-glut-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haskell-src-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-haxml-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-x11-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-hgl-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-hunit-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-parsec-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-network-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-quickcheck-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-stm-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-time-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-unix-bundled (98.200609.21-5ubuntu1) ...<br />
Setting up libhugs-xhtml-bundled (98.200609.21-5ubuntu1) ...<br />
Processing triggers for libc-bin ...<br />
<br />
</haskell><br />
<br />
Hugs on Ubuntu Linux can be installed using:<br />
<br />
'''sudo apt-get install hugs'''<br />
<br />
[[Category:Implementations]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Foldl_as_foldr_alternative&diff=58814Foldl as foldr alternative2014-09-15T00:39:17Z<p>Dfeuer: </p>
<hr />
<div>This page explains how <hask>foldl</hask> can be written using <hask>foldr</hask>. Yes, there is already [[Foldl as foldr|such a page]]! This one explains it differently.<br />
<br />
<br />
The usual definition of <hask>foldl</hask> looks like this:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a [] = a<br />
foldl f a (x : xs) = foldl f (f a x) xs<br />
</haskell><br />
<br />
<br />
Now the <hask>f</hask> never changes in the recursion. It turns out things will be simpler later if we pull it out:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a list = go a list<br />
where<br />
go acc [] = acc<br />
go acc (x : xs) = go (f acc x) xs<br />
</haskell><br />
<br />
<br />
-----<br />
<br />
<br />
For some reason (maybe we're crazy; maybe we want to do weird things with fusion; who knows?) we want to write this using <hask>foldr</hask>. Haskell programmers like curry, so it's natural to see <hask>go acc xs</hask> as <hask>(go acc) xs</hask>&mdash;that is, to see <hask>go a</hask> as a function that takes a list and returns the result of folding <hask>f</hask> into the list starting with an accumulator value of <hask>a</hask>. This perspective, however, is the ''wrong one'' for what we're trying to do here. So let's change the order of the arguments of the helper:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a list = go2 list a<br />
where<br />
go2 [] acc = acc<br />
go2 (x : xs) acc = go2 xs (f acc x)<br />
</haskell><br />
<br />
<br />
So now we see that <hask>go2 xs</hask> is a function that takes an accumulator and uses it as the initial value to fold <hask>f</hask> into <hask>xs</hask>. With this shift of perspective, we can rewrite <hask>go2</hask> just a little, shifting its second argument into an explicit lambda:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a list = go2 list a<br />
where<br />
go2 [] = \acc -> acc<br />
go2 (x : xs) = \acc -> go2 xs (f acc x)<br />
</haskell><br />
<br />
<br />
Believe it or not, we're almost done! How is that? Let's parenthesize a bit for emphasis:<br />
<br />
<br />
<haskell><br />
foldl f a list = go2 list a<br />
where<br />
go2 [] = (\acc -> acc)<br />
go2 (x : xs) = \acc -> (go2 xs) (f acc x)<br />
</haskell><br />
<br />
<br />
This isn't an academic paper, so we won't mention Graham Hutton's "Tuturial on the Universality and Expressiveness of Fold", but <hask>go2</hask> fits the <hask>foldr</hask> pattern:<br />
<br />
<br />
<haskell><br />
go2 ys = foldr whatsit (\acc -> acc) ys<br />
where<br />
whatsit x r = \acc -> r (f acc x)<br />
</haskell><br />
<br />
<br />
Substituting this in,<br />
<br />
<br />
<haskell><br />
foldl f a list = (foldr whatsit (\acc -> acc) list) a<br />
where<br />
whatsit x r = \acc -> r (f acc x)<br />
</haskell><br />
<br />
<br />
And that's all she wrote! One way to look at this final expression is that <hask>whatsit</hask> takes an element of the list, a function produced by folding over the rest of the list, and the value of an accumulator. It applies <hask>f</hask> to the accumulator it's given and the list element, and passes the result forward to the function it got.</div>Dfeuerhttps://wiki.haskell.org/index.php?title=HaskellWiki_talk:Copyrights&diff=58781HaskellWiki talk:Copyrights2014-09-08T07:17:41Z<p>Dfeuer: /* Simple Permissive License */</p>
<hr />
<div>= Page name =<br />
<br />
I think that the name ''Copyrights'' is a bit strange because the topic of this page are not several copyrights. The central topic is not even copyright but licensing. What about including the information from this page into [[HaskellWiki:Licensing]]?<br />
<br />
= The license discussion =<br />
== Which license? ==<br />
<br />
* [http://www.gnu.org/licenses/fdl.txt GFDL 1.2?]<br />
* [http://creativecommons.org/ Creative Commons?]<br />
* [http://www.gnu.org/licenses/gpl.txt GPL?]<br />
* [http://www.opencontent.org/openpub/ OPL?]<br />
* [http://www.gnu.org/licenses/info/BSD_2Clause.html BSD 2-clause]<br />
* [http://www.gnu.org/licenses/info/BSD_3Clause.html BSD 3-clause]<br />
* [http://www.gnu.org/licenses/info/SimplePermissive.html Simple Permissive]<br />
* Public Domain?<br />
<br />
See also [http://www.gnu.org/licenses/license-list.html this giant list].<br />
<br />
GFDL is more convenient if we wish to import material from Wikipedia. Most Haskell library code is available under BSD. Opinions? &mdash;[[User:Ashley Y|Ashley Y]] 01:14, 9 January 2006 (EST)<br />
<br />
I think the content has to be such that it is easy to add things to Haskell implementations and libraries. As fptools and GHC are under the BSD3 license having the wiki with a more restrictive license can create problems.<br />
--[[User:EinarKarttunen|EinarKarttunen]] 03:12, 9 January 2006 (EST)<br />
<br />
There's a quick discussion of the various licenses at [http://lwn.net/Articles/108250/ LWN]. <br />
<br />
I'll just note that if we start with a very liberal license, it can be tightened later. If we start with a restrictive license, there is no such option (short of contacting every author (or their heirs), and having them agree to it).<br />
<br />
I don't care much for the FDL, it is too complex, and it's hard to grok how to deal with invariant sections, authorship (you are required to list at least five for derived works) etc.<br />
<br />
I notice it is possible to interpret the wiki as a collection of independent works (see Udo Stenzel's recent post on the haskell list) - which wreaks havoc on any scheme requiring attribution. If so, I agree PD is probably less hassle. <br />
<br />
(Of course, this assumes we are in a jurisdiction where PD is a meaningful concept - this excludes at least some European countries. I can't imagine this being a problem in reality, but perhaps there should be a textual note with a bit more detail?)<br />
<br />
--[[User:Ketil]]<br />
<br />
I've never heard of OPL, but have heard of the others, so perhaps OPL is too obscure. I would go for either public domain or BSD. Perhaps multiple licenses are appropriate, public domain, BSD, GPL and GFDL - if everything is licensed under all of them then everyone will be happy.<br />
<br />
--[[User:NeilMitchell|NeilMitchell]] 07:06, 9 January 2006 (EST)<br />
<br />
People have [[Special:Recentchanges|started contributing huge amounts of text]]. It may be too late... &mdash;[[User:Ashley Y|Ashley Y]] 12:52, 9 January 2006 (EST)<br />
<br />
I vote for public domain. I'm no license expert but I basically think that code in public manuals and the like should be completely free of any type of restrictions. You want to include it in your open-source app? Fine! You want to include it in your comercial multi-million dollar application? Fine! It's not like a wiki page will include large quantities of truly valuable intellectual property anyways, so less probably is more here. Why make it more complicated than it needs to be when it's likely it will have little or no practical consequences?<br />
Like someone said, if something is more restrictive then it can be flagged as such, or linked from the wiki to some other location.<br />
--[[User:SebastianSylvan|SebastianSylvan]] 13:02, 9 January 2006 (EST)<br />
<br />
Why not dual license under GNU FDL and BSD? Wouldn't that take care of most code- and documentation-reuse issues?<br />
<br />
--[[User:TomMoertel|TomMoertel]]<br />
<br />
There is some relevant discussion [http://wiki.creativecommons.org/wiki/Legal_arrangements here]. I would vote for using public domain by default, but it should be very easy to specify a more restrictive license using the wiki markup.<br />
<br />
I've later changed my mind on this issue. I think that a single, mandatory, and exeedingly permissive license would be the best solution. As for worries about the incompatability of wiki-ism and moral rights in some countries, I believe that the nature of wiki edit history makes it irrelevant. It is impossible to deface the work done by any person because it is trivial to find out (a) who made the original work and (b) who modified the work and exactly what they changed.<br />
<br />
This concern is especially invalid in the case of code snippets published to the wiki because: "Moral rights do not cause difficulty with computer software licenses, because civil law countries have made special statutory exceptions where, absent agreement to the contrary, an author may not object to the modification of a software work by a grantee who has acquired the right to adapt the work. (Source: French Copyright Law revision of July 3, 1985, art. 46. See Ginsburg, Reforms and Innovations Regarding Authors' and Performers' Rights in France: Commentary on the Law of July 3, 1985, 10 Colum.-VLA J.L. & Arts 83, 90 (1985))" (cited from: "Public domain." Wikipedia, The Free Encyclopedia. 11 Jan 2006, 02:50 UTC. 11 Jan 2006, 13:27 <http://en.wikipedia.org/w/index.php?title=Public_domain&oldid=34701571>)<br />
<br />
--[[User:SamGoldman|Sam]]<br />
<br />
For BSD we have to be careful, as there are a couple different versions floating around, with various levels of utility. Public domain has the issue that it doesn't disclaim warranties, and that's a little bit extra peace of mind it would be nice to have. I'm not sure I see a need for reproduction of copyright notices, so perhaps a<br />
[http://www.gnu.org/licenses/info/SimplePermissive.html Simple Permissive] license is the way to go.<br />
<br />
--[[User:AaronDenney|Aaron Denney]]<br />
== Public Domain straw poll ==<br />
<br />
Should we switch the license to public domain?<br />
<br />
* '''Yes''', or BSD &mdash;[[User:Ashley Y|Ashley Y]] 15:17, 9 January 2006 (EST)<br />
* '''Yes''' --[[User:NeilMitchell|NeilMitchell]] 13:28, 9 January 2006 (EST)<br />
* Yes, or BSD/CC-AT &mdash;[[User:Twanvl|Twanvl]] 13:30, 9 January 2006 (EST)<br />
* Yes, or BSD &mdash;[[User:CaleGibbard|CaleGibbard]] 13:45, 9 January 2006 (EST)<br />
* Yes, or BSD --[[User:EinarKarttunen|EinarKarttunen]] 13:47, 9 January 2006 (EST)<br />
* Yes --[[User:SebastianSylvan|SebastianSylvan]] 15:57, 9 January 2006 (EST)<br />
* Yes, or anything GFDL-compatible -- [[User:EricKow|EricKow]] 16:49, 9 January 2006 (EST)<br />
* Yes, [[User:JohnMeacham|John Meacham]]<br />
* Yes, or [[http://www.opensource.org/licenses/bsd-license.php New BSD License]], to let people from e.g. Germany contribute [[User:ArthurVanLeeuwen|Arthur van Leeuwen]] Updated Wed Jan 11 10:06:19 CET 2006<br />
* '''No''' &mdash; BSD (pref. w/out attribution clause) [[User:jodonoghue|Jeremy O'Donoghue]] 14:30, 10 January 2006 (GMT)<br />
* Yes, or BSD --[[User:Oc|Oc]] 09:42, 10 January 2006 (EST)<br />
* No, two-clause BSD, or a simple permissive with a disclaimer of warranty. --[[User:AaronDenney|Aaron Denney]]<br />
* No, go for a simple permissive licence instead --[[User:malcolm|Malcolm Wallace]]<br />
* No, go for a simple (very) permissive license. -- [[User:Wolfgang Jeltsch|Wolfgang Jeltsch]] 15:31, 11 January 2006 (EST)<br />
<br />
Just to clarify, this would mean by contributing, you agree to release everything to public domain with no option to reserve any rights. &mdash;[[User:Ashley Y|Ashley Y]] 14:18, 9 January 2006 (EST)<br />
<br />
Wouldn't that forbid contributions by anyone who can't put things in<br />
the public domain? It's not clear that you can do so in the USA, for<br />
example. Further, it would mean we couldn't use code from more strictly<br />
licensed sources, such as the Haskell 98 Report or the GHC documentation.<br />
Neither could we post snippets of the common Haskell libraries as<br />
material for discussion.<br />
&mdash;[[User:Brian Sniffen|Brian Sniffen]]<br />
<br />
:What licenses do the Haskell 98 report and the GHC documentation use? &mdash;[[User:Ashley Y|Ashley Y]] 15:52, 9 January 2006 (EST)<br />
<br />
Couldn't the copyright notice say "All content is considered to be in the public domain unless stated otherwise" or something like that? So if someone posts something which is BSD, they just say "The following code is made available under the BSD license".<br />
--[[User:SebastianSylvan|SebastianSylvan]] 16:02, 9 January 2006 (EST)<br />
<br />
:And then you'll have little bits of the wiki available under different licenses. Someone might take my little snippet of code, modify it, turn it into GPL, and then I can't use my original snippet.--[[User:NeilMitchell|NeilMitchell]] 16:45, 9 January 2006 (EST) <br />
<br />
::This is just untrue. no one can take away your rights to use something you wrote. it is just not possible. just like you can't be forced to release something under the GPL because you incorperate GPL code, you will be forced to remove the GPL code, but your code is always yours no matter what. (short of a legal sale of the copyright) ''(says [[User:JohnMeacham]])''<br />
<br />
::I think that's a highly unlikely scenario. A large part of this discussion is pointless. Most of the content will be original, and it is unlikely that any authors will bother tagging their contribution under a more restrictive license. In the rare case that we want to use something which isn't in the public domain (and we can't get the author to release it as such) we'll tag it with its license. --[[User:SebastianSylvan|SebastianSylvan]] 17:29, 9 January 2006 (EST)<br />
<br />
== "Public domain by default"? ==<br />
<br />
OK, so the current consensus seems to be "public domain by default":<br />
<br />
* The copyright statement:<br />
<br />
:"Content is in the public domain, except where noted on the page"<br />
<br />
* The contribution warning (see bottom of any edit page):<br />
<br />
:"Please note that all contributions to HaskellWiki are considered to be in the public domain, unless you clearly mark otherwise on the page (see [[Project:Copyrights]] for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here. '''Material marked as copyrighted is particularly liable to deletion.'''<br />You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. '''DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!'''"<br />
<br />
Opinions? Tweaks? Alternate options? &mdash;[[User:Ashley Y|Ashley Y]] 13:22, 10 January 2006 (EST)<br />
<br />
Would mandatory public domain be preferable? &mdash;[[User:Ashley Y|Ashley Y]] 16:54, 10 January 2006 (EST)<br />
<br />
<br />
:Sounds good to me. Maybe "public domain" could be a link to a page where it says something more explicitly ("Do what you want, but don't bug me!", but more eloquent). Useful for countries where "public domain" is a tricky issue perhaps?--[[User:SebastianSylvan|SebastianSylvan]] 17:09, 10 January 2006 (EST)<br />
<br />
::OK, I'll set this up tomorrow unless there are objections. I'll mark existing pages as copyrighted (unknown copyright for those that have been copied, and GFDL for the new ones), and encourage the people who wrote them to release them to the public domain. &mdash;[[User:Ashley Y|Ashley Y]] 19:30, 10 January 2006 (EST)<br />
<br />
::If you <nowiki>[</nowiki>[[User:SebastianSylvan|SebastianSylvan]]<nowiki>]</nowiki> want to remove the problems for users in such countries (which is a good thing :-) ), I'd propose to not use the term „public domain“ at all. Just use a license which in effect says: „Do what you want but don't bug me.“ AFAIK, licensing a work under such a license is different from not licensing it at all (public domain). -- [[User:Wolfgang Jeltsch|Wolfgang Jeltsch]] 15:31, 11 January 2006 (EST)<br />
<br />
:::Not licensing it at all is the same as "all rights reserved", i.e. not giving anyone any license. What we want is a maximal license to the public, which is similar to the public domain. &mdash;[[User:Ashley Y|Ashley Y]] 18:10, 11 January 2006 (EST)<br />
<br />
Simon Peyton-Jones thinks it should be a mandatory license instead. Poll:<br />
<br />
== Mandatory/Optional license straw poll ==<br />
<br />
Should the wiki be '''mandatory''' public domain (i.e. all contributions must in the public domain) or '''optional''' public domain (i.e. contributors can explicitly mark pages as being under another license)? &mdash;[[User:Ashley Y|Ashley Y]] 01:13, 11 January 2006 (EST)<br />
<br />
* '''mandatory''' (weak preference) &mdash;[[User:Ashley Y|Ashley Y]] 01:13, 11 January 2006 (EST)<br />
* '''mandatory''' for whatever license is decided, other content can be linked to and hosted elsewhere. --[[User:AaronDenney|Aaron Denney]]<br />
* '''mandatory''' then my brain can loose "legal thoughts" as soon as I get to haskell.org (a very good thing) --[[User:NeilMitchell|NeilMitchell]] 03:29, 11 January 2006 (EST)<br />
* '''mandatory''' but with the condition that it only applies to text in the actual markup, so images/pdfs/etc can be attached to a wiki page (I'm thinking about the Haskell definition here) under the conditions of their license. {{Unsigned|Speck}}<br />
* public domain should '''not be mandatory''' but a permissive license maybe could {{Unsigned|Wolfgang Jeltsch}}<br />
<br />
== Simple Permissive License ==<br />
<br />
I did come across [http://www.opensource.org/licenses/mit-license.php the MIT license], which seems close to what people want. Here is my non-expert attempt to adapt it, removing the condition, and changing "Software" to "Work":<br />
<br />
:Permission is hereby granted, free of charge, to any person obtaining this work (the "Work"), to deal in the Work without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Work, and to permit persons to whom the Work is furnished to do so.<br />
<br />
:THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS IN THE WORK.<br />
<br />
Better? &mdash;[[User:Ashley Y|Ashley Y]] 17:30, 12 January 2006 (EST)<br />
<br />
Seems much better to me (than public domain).<br />
--[[User:Malcolm|Malcolm]] 05:18, 13 January 2006 (EST)<br />
<br />
Done! &mdash;[[User:Ashley Y|Ashley Y]] 05:30, 14 January 2006 (UTC)<br />
<br />
As far as I can tell, this is practically the same as placing it in the public domain, but with more legalese to read through. --[[User:Dfeuer|Dfeuer]] ([[User talk:Dfeuer|talk]]) 07:17, 8 September 2014 (UTC)</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Foldl_as_foldr&diff=58757Foldl as foldr2014-09-05T21:19:54Z<p>Dfeuer: </p>
<hr />
<div>Note: there is an [[Foldl as foldr alternative|alternative explanation]] of some of the basics from a more elementary perspective.<br />
<br />
<br />
When you wonder whether to choose [[Foldr_Foldl_Foldl'|foldl or foldr]] you may remember,<br />
that both <hask>foldl</hask> and <hask>foldl'</hask> can be expressed as <hask>foldr</hask>.<br />
(<hask>foldr</hask> may [http://www.willamette.edu/~fruehr/haskell/evolution.html lean so far right] it came back left again.)<br />
It holds<br />
<haskell><br />
foldl :: (a -> b -> a) -> a -> [b] -> a<br />
foldl f a bs =<br />
foldr (\b g x -> g (f x b)) id bs a<br />
</haskell><br />
<br />
<br />
(The converse is not true, since <hask>foldr</hask> may work on infinite lists,<br />
which <hask>foldl</hask> variants never can do. However, for ''finite'' lists, <hask>foldr</hask> ''can'' also be written in terms of <hask>foldl</hask> (although losing laziness in the process), in a similar way like this:<br />
<haskell><br />
foldr :: (b -> a -> a) -> a -> [b] -> a<br />
foldr f a bs =<br />
foldl (\g b x -> g (f b x)) id bs a<br />
</haskell><br />
)<br />
<br />
Now the question are:<br />
* How can someone find a convolved expression like this?<br />
* How can we benefit from this rewrite?<br />
<br />
<br />
== Folding by concatenating updates ==<br />
<br />
Instead of thinking in terms of <hask>foldr</hask> and a function <hask>g</hask> as argument to the accumulator function,<br />
I find it easier to imagine a fold as a sequence of updates.<br />
An update is a function mapping from an old value to an updated new value.<br />
<haskell><br />
newtype Update a = Update {evalUpdate :: a -> a}<br />
</haskell><br />
We need a way to assemble several updates.<br />
To this end we define a <hask>Monoid</hask> instance.<br />
<haskell><br />
instance Monoid (Update a) where<br />
mempty = Update id<br />
mappend (Update x) (Update y) = Update (y.x)<br />
</haskell><br />
Now left-folding is straight-forward.<br />
<haskell><br />
foldlMonoid :: (a -> b -> a) -> a -> [b] -> a<br />
foldlMonoid f a bs =<br />
flip evalUpdate a $<br />
mconcat $<br />
map (Update . flip f) bs<br />
</haskell><br />
Now, where is the <hask>foldr</hask>?<br />
It is hidden in <hask>mconcat</hask>.<br />
<haskell><br />
mconcat :: Monoid a => [a] -> a<br />
mconcat = foldr mappend mempty<br />
</haskell><br />
Since <hask>mappend</hask> must be associative<br />
(and is actually associative for our <hask>Update</hask> monoid),<br />
<hask>mconcat</hask> could also be written as <hask>foldl</hask>,<br />
but this is avoided, precisely <hask>foldl</hask> fails on infinite lists.<br />
<br />
By the way:<br />
<hask>Update a</hask> is just <hask>Dual (Endo a)</hask>.<br />
If you use a <hask>State</hask> monad instead of a monoid,<br />
you obtain an alternative implementation of <hask>mapAccumL</hask>.<br />
<br />
<br />
== foldl which may terminate early ==<br />
<br />
The answer to the second question is:<br />
Using the <hask>foldr</hask> expression we can write variants of <hask>foldl</hask><br />
that behave slightly different from the original one.<br />
E.g. we can write a <hask>foldl</hask> that can stop before reaching the end of the input list<br />
and thus may also terminate on infinite input.<br />
The function <hask>foldlMaybe</hask> terminates with <hask>Nothing</hask> as result<br />
when it encounters a <hask>Nothing</hask> as interim accumulator result.<br />
<haskell><br />
foldlMaybe :: (a -> b -> Maybe a) -> a -> [b] -> Maybe a<br />
foldlMaybe f a bs =<br />
foldr (\b g x -> f x b >>= g) Just bs a<br />
</haskell><br />
<br />
Maybe the monoidic version is easier to understand.<br />
The implementation of the fold is actually the same, we do only use a different monoid.<br />
<haskell><br />
import Control.Monad ((>=>), )<br />
<br />
newtype UpdateMaybe a = UpdateMaybe {evalUpdateMaybe :: a -> Maybe a}<br />
<br />
instance Monoid (UpdateMaybe a) where<br />
mempty = UpdateMaybe Just<br />
mappend (UpdateMaybe x) (UpdateMaybe y) = UpdateMaybe (x>=>y)<br />
<br />
foldlMaybeMonoid :: (a -> b -> Maybe a) -> a -> [b] -> Maybe a<br />
foldlMaybeMonoid f a bs =<br />
flip evalUpdateMaybe a $<br />
mconcat $<br />
map (UpdateMaybe . flip f) bs<br />
</haskell><br />
<br />
<br />
== Practical example: Parsing numbers using a bound ==<br />
<br />
As a practical example consider a function that converts an integer string to an integer,<br />
but that aborts when the number exceeds a given bound.<br />
With this bound it is possible to call <hask>readBounded 1234 $ repeat '1'</hask><br />
which will terminate with <hask>Nothing</hask>.<br />
<haskell><br />
readBounded :: Integer -> String -> Maybe Integer<br />
readBounded bound str =<br />
case str of<br />
"" -> Nothing<br />
"0" -> Just 0<br />
_ -> foldr<br />
(\digit addLeastSig mostSig -><br />
let n = mostSig*10 + toInteger (Char.digitToInt digit)<br />
in guard (Char.isDigit digit) >><br />
guard (not (mostSig==0 && digit=='0')) >><br />
guard (n <= bound) >><br />
addLeastSig n)<br />
Just str 0<br />
<br />
readBoundedMonoid :: Integer -> String -> Maybe Integer<br />
readBoundedMonoid bound str =<br />
case str of<br />
"" -> Nothing<br />
"0" -> Just 0<br />
_ -><br />
let m digit =<br />
UpdateMaybe $ \mostSig -><br />
let n = mostSig*10 + toInteger (Char.digitToInt digit)<br />
in guard (Char.isDigit digit) >><br />
guard (not (mostSig==0 && digit=='0')) >><br />
guard (n <= bound) >><br />
Just n<br />
in evalUpdateMaybe (mconcat $ map m str) 0<br />
</haskell><br />
<br />
== See also ==<br />
<br />
* Graham Hutton: [http://www.cs.nott.ac.uk/~gmh/fold.pdf A tutorial on the universality and expressiveness of fold]<br />
* [[Fold]]<br />
* [[Foldr Foldl Foldl']]<br />
<br />
[[Category:Idioms]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Foldl_as_foldr_alternative&diff=58742Foldl as foldr alternative2014-09-04T05:42:08Z<p>Dfeuer: </p>
<hr />
<div>This page explains how <hask>foldl</hask> can be written using <hask>foldr</hask>. Yes, there is already [[Foldl as foldr|such a page]]! This one explains it differently.<br />
<br />
<br />
The usual definition of <hask>foldl</hask> looks like this:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a [] = a<br />
foldl f a (x : xs) = foldl f (f a x) xs<br />
</haskell><br />
<br />
<br />
Now the <hask>f</hask> never changes in the recursion. It turns out things will be simpler later if we pull it out:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a list = go a list<br />
where<br />
go a [] = a<br />
go a (x : xs) = go (f a x) xs<br />
</haskell><br />
<br />
<br />
-----<br />
<br />
<br />
For some reason (maybe we're crazy; maybe we want to do weird things with fusion; who knows?) we want to write this using <hask>foldr</hask>. Haskell programmers like curry, so it's natural to see <hask>go a xs</hask> as <hask>(go a) xs</hask>&mdash;that is, to see <hask>go a</hask> as a function that takes a list and returns the result of folding <hask>f</hask> into the list starting with an accumulator value of <hask>a</hask>. This perspective, however, is the ''wrong one'' for what we're trying to do here. So let's change the order of the arguments of the helper:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a list = go2 list a<br />
where<br />
go2 [] a = a<br />
go2 (x : xs) a = go2 xs (f a x)<br />
</haskell><br />
<br />
<br />
So now we see that <hask>go2 xs</hask> is a function that takes an accumulator and uses it as the initial value to fold <hask>f</hask> into <hask>xs</hask>. With this shift of perspective, we can rewrite <hask>go2</hask> just a little:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a list = go2 list a<br />
where<br />
go2 [] = \a -> a<br />
go2 (x : xs) = \a -> go2 xs (f a x)<br />
</haskell><br />
<br />
<br />
Believe it or not, we're almost done! How is that? Let's parenthesize a bit for emphasis:<br />
<br />
<br />
<haskell><br />
foldl f a list = go2 list a<br />
where<br />
go2 [] = (\a -> a)<br />
go2 (x : xs) = \a -> (go2 xs) (f a x)<br />
</haskell><br />
<br />
<br />
This isn't an academic paper, so we won't mention Graham Hutton's "Tuturial on the Universality and Expressiveness of Fold", but <hask>go2</hask> fits the <hask>foldr</hask> pattern:<br />
<br />
<br />
<haskell><br />
go2 ys = foldr whatsit (\a -> a) ys<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
<br />
Substituting this in,<br />
<br />
<br />
<haskell><br />
foldl f a list = (foldr whatsit (\a -> a) list) a<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
<br />
And that's all she wrote! One way to look at this final expression is that <hask>whatsit</hask> takes an element of the list, a function produced by folding over the rest of the list, and the value of an accumulator. It applies <hask>f</hask> to the accumulator it's given and the list element, and passes the result forward to the function it got.</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Foldl_as_foldr_alternative&diff=58741Foldl as foldr alternative2014-09-04T05:14:28Z<p>Dfeuer: </p>
<hr />
<div>This page explains how <hask>foldl</hask> can be written using <hask>foldr</hask>. Yes, there is already [[Foldl as foldr|such a page]]! This one explains it differently.<br />
<br />
The usual definition of <hask>foldl</hask> looks like this:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a [] = a<br />
foldl f a (x : xs) = foldl f (f a x) xs<br />
</haskell><br />
<br />
<br />
Now the <hask>f</hask> never changes in the recursion, so we don't really have to worry too much about it. For simplicity, then, let's pick one in particular:<br />
<br />
<br />
<haskell><br />
f :: Ord x => Set x -> x -> Set x<br />
f a x = insert x a<br />
</haskell><br />
<br />
<br />
While we're at it, let's give a name to <hask>foldl f</hask>: <hask>stuff</hask>. So<br />
<br />
<br />
<haskell><br />
stuff :: Ord x => Set x -> [x] -> Set x<br />
stuff a [] = a<br />
stuff a (x:xs) = stuff (f a x) xs<br />
</haskell><br />
<br />
<br />
takes all the elements of the list it's given and stuffs them into the <hask>Set</hask> it's given.<br />
<br />
<br />
-----<br />
<br />
<br />
For some reason (maybe we're crazy; maybe we want to do weird things with fusion; who knows?) we want to write this using <hask>foldr</hask>. Haskell programmers like curry, so it's natural to see <hask>stuff a xs</hask> as <hask>(stuff a) xs</hask>&mdash;that is, to see <hask>stuff a</hask> as a function that takes a list and returns the result of folding <hask>f</hask> into the list starting with an accumulator value of <hask>a</hask>. This perspective, however, is the ''wrong one'' for what we're trying to do here. So let's change the order of the arguments of <hask>stuff</hask>.<br />
<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy [] a = a<br />
stuffy (x : xs) a = stuffy xs (f a x)<br />
</haskell><br />
<br />
<br />
So now we see that <hask>stuffy xs</hask> is a function that takes an accumulator and uses it as the initial value to fold <hask>f</hask> into <hask>xs</hask>. With this shift of perspective, we can rewrite <hask>stuffy</hask> just a little:<br />
<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy a [] = \a -> a<br />
stuffy (x : xs) = \a -> stuffy xs (f a x)<br />
</haskell><br />
<br />
<br />
Believe it or not, we're almost done! How is that? Let's parenthesize a bit for emphasis:<br />
<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy [] = (\a -> a)<br />
stuffy (x : xs) = \a -> (stuffy xs) (f a x)<br />
</haskell><br />
<br />
<br />
This isn't an academic paper, so we won't mention Graham Hutton's "Tuturial on the Universality and Expressiveness of Fold", but <hask>stuffy</hask> fits the <hask>foldr</hask> pattern:<br />
<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy ys = foldr whatsit (\a -> a) ys<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
<br />
Substituting this in,<br />
<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy list a = (foldr whatsit (\a -> a) list) a<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
<br />
And that's just about it! We wanted <hask>stuff</hask>, however, not <hask>stuffy</hask>, so let's swap the argument order again:<br />
<br />
<br />
<haskell><br />
stuff :: Ord x => Set a -> [x] -> Set x<br />
stuff a list = (foldr whatsit (\a -> a) list) a<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
<br />
Now since we do want to be able to use general <hask>foldl</hask> forms, we should generalize it again:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a xs = (foldr whosit (\a -> a) list) a<br />
where<br />
whosit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
<br />
The way to look at this final expression is that <hask>whosit</hask> takes an element of the list, a function produced by folding <hask>f</hask> into the rest of the list, and the initial value, <hask>a</hask> of an accumulator. It applies <hask>f</hask> to the accumulator it's given and the list element, and passes the result forward to the function it got.</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Foldl_as_foldr_alternative&diff=58740Foldl as foldr alternative2014-09-04T05:10:01Z<p>Dfeuer: </p>
<hr />
<div>This page explains how <hask>foldl</hask> can be written using <hask>foldr</hask>. Yes, there is already [[Foldl as foldr|such a page]]! This one explains it differently.<br />
<br />
The usual definition of <hask>foldl</hask> looks like this:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a [] = a<br />
foldl f a (x : xs) = foldl f (f a x) xs<br />
</haskell><br />
<br />
<br />
Now the <hask>f</hask> never changes in the recursion, so we don't really have to worry too much about it. For simplicity, then, let's pick one in particular:<br />
<br />
<br />
<haskell><br />
f :: Ord x => Set x -> x -> Set x<br />
f a x = insert x a<br />
</haskell><br />
<br />
<br />
While we're at it, let's give a name to <hask>foldl f</hask>: <hask>stuff</hask>. So<br />
<br />
<br />
<haskell><br />
stuff :: Ord x => Set x -> [x] -> Set x<br />
stuff a [] = a<br />
stuff a (x:xs) = stuff (f a x) xs<br />
</haskell><br />
<br />
<br />
takes all the elements of the list it's given and stuffs them into the <hask>Set</hask> it's given.<br />
<br />
<br />
-----<br />
<br />
<br />
For some reason (maybe we're crazy; maybe we want to do weird things with fusion; who knows?) we want to write this using <hask>foldr</hask>. Haskell programmers like curry, so it's natural to see <hask>stuff a xs</hask> as <hask>(stuff a) xs</hask>&mdash;that is, to see <hask>stuff a</hask> as a function that takes a list and returns the result of folding <hask>f</hask> into the list starting with an accumulator value of <hask>a</hask>. This perspective, however, is the ''wrong one'' for what we're trying to do here. So let's change the order of the arguments of <hask>stuff</hask>.<br />
<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy [] a = a<br />
stuffy (x : xs) a = stuffy xs (f a x)<br />
</haskell><br />
<br />
<br />
So now we see that <hask>stuffy xs</hask> is a function that takes an accumulator and uses it as the initial value to fold <hask>f</hask> into <hask>xs</hask>. With this shift of perspective, we can rewrite <hask>stuffy</hask> just a little:<br />
<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy a [] = \a -> a<br />
stuffy (x : xs) = \a -> stuffy xs (f a x)<br />
</haskell><br />
<br />
<br />
Believe it or not, we're almost done! How is that? Let's parenthesize a bit for emphasis:<br />
<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy [] = (\a -> a)<br />
stuffy (x : xs) = \a -> (stuffy xs) (f a x)<br />
</haskell><br />
<br />
<br />
This isn't an academic paper, so we won't mention Graham Hutton's "Tuturial on the Universality and Expressiveness of Fold", but <hask>stuffy</hask> fits the <hask>foldr</hask> pattern:<br />
<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy ys = foldr whatsit (\a -> a) ys<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
<br />
Substituting this in,<br />
<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy list a = (foldr whatsit (\a -> a) list) a<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
<br />
And that's just about it! We wanted <hask>stuff</hask>, however, not <hask>stuffy</hask>, so let's swap the argument order again:<br />
<br />
<br />
<haskell><br />
stuff :: Ord x => Set a -> [x] -> Set x<br />
stuff a list = (foldr whatsit (\a -> a) list) a<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
<br />
Now since we do want to be able to use general <hask>foldl</hask> forms, we should gneralize it again:<br />
<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a xs = (foldr whosit (\a -> a) list) a<br />
where<br />
whosit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
<br />
The way to look at this final expression is that <hask>whosit</hask> takes an element of the list, a function produced by folding <hask>f</hask> into the rest of the list, and the initial value, <hask>a</hask> of an accumulator. It applies <hask>f</hask> to the accumulator it's given and the list element, and passes the result forward to the function it got.</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Foldl_as_foldr_alternative&diff=58739Foldl as foldr alternative2014-09-04T05:02:14Z<p>Dfeuer: </p>
<hr />
<div>This page explains how <hask>foldl</hask> can be written using <hask>foldr</hask>. Yes, there is already [[Foldl as foldr|such a page]]! This one explains it differently.<br />
<br />
The usual definition of <hask>foldl</hask> looks like this:<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a [] = a<br />
foldl f a (x : xs) = foldl f (f a x) xs<br />
</haskell><br />
<br />
Now the <hask>f</hask> never changes in the recursion, so we don't really have to worry too much about it. For simplicity, then, let's pick one in particular:<br />
<br />
<haskell><br />
f :: Ord x => Set x -> x -> Set x<br />
f a x = insert x a<br />
</haskell><br />
<br />
While we're at it, let's give a name to <hask>foldl f</hask>: <hask>stuff</hask>. So<br />
<br />
<haskell><br />
stuff :: Ord x => Set x -> [x] -> Set x<br />
stuff a [] = a<br />
stuff a (x:xs) = stuff (f a x) xs<br />
</haskell><br />
<br />
takes all the elements of the list it's given and stuffs them into the <hask>Set</hask> it's given.<br />
<br />
For some reason (maybe we're crazy; maybe we want to do weird things with fusion; who knows?) we want to write this using <hask>foldr</hask>. Haskell programmers like curry, so it's natural to see <hask>stuff a xs</hask> as <hask>(stuff a) xs</hask>&mdash;that is, to see <hask>stuff a</hask> as a function that takes a list and returns the result of folding <hask>f</hask> into the list starting with an accumulator value of <hask>a</hask>. This perspective, however, is the ''wrong one'' for what we're trying to do here. So let's change the order of the arguments of <hask>stuff</hask>.<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy [] a = a<br />
stuffy (x : xs) a = stuffy xs (f a x)<br />
</haskell><br />
<br />
So now we see that <hask>stuffy xs</hask> is a function that takes an accumulator and uses it as the initial value to fold <hask>f</hask> into <hask>xs</hask>. With this shift of perspective, we can rewrite <hask>stuffy</hask> just a little:<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy a [] = \a -> a<br />
stuffy (x : xs) = \a -> stuffy xs (f a x)<br />
</haskell><br />
<br />
Believe it or not, we're almost done! How is that? Let's parenthesize a bit for emphasis:<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy [] = (\a -> a)<br />
stuffy (x : xs) = \a -> (stuffy xs) (f a x)<br />
</haskell><br />
<br />
This isn't an academic paper, so we won't mention Graham Hutton's "Tuturial on the Universality and Expressiveness of Fold", but <hask>stuffy</hask> fits the <hask>foldr</hask> pattern:<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy ys = foldr whatsit (\a -> a) ys<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
Substituting this in,<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy list a = (foldr whatsit (\a -> a) list) a<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
And that's just about it! We wanted <hask>stuff</hask>, however, not <hask>stuffy</hask>, so let's swap the argument order again:<br />
<br />
<haskell><br />
stuff :: Ord x => Set a -> [x] -> Set x<br />
stuff a list = (foldr whatsit (\a -> a) list) a<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
Now since we do want to be able to use general <hask>foldl</hask> forms, we should gneralize it again:<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a xs = (foldr whosit (\a -> a) list) a<br />
where<br />
whosit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
The way to look at this final expression is that <hask>whosit</hask> takes an element of the list, a function produced by folding <hask>f</hask> into the rest of the list, and the initial value, <hask>a</hask> of an accumulator. It applies <hask>f</hask> to the accumulator it's given and the list element, and passes the result forward to the function it got.</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Foldl_as_foldr_alternative&diff=58738Foldl as foldr alternative2014-09-04T05:00:46Z<p>Dfeuer: </p>
<hr />
<div>This page explains how <hask>foldl</hask> can be written using <hask>foldr</hask>. Yes, there is already [[Foldl as foldr|such a page]]! This one explains it differently.<br />
<br />
The usual definition of <hask>foldl</hask> looks like this:<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a [] = a<br />
foldl f a (x : xs) = foldl f (f a x) xs<br />
</haskell><br />
<br />
Now the <hask>f</hask> never changes in the recursion, so we don't really have to worry too much about it. For simplicity, then, let's pick one in particular:<br />
<br />
<haskell><br />
f :: Ord x => Set x -> x -> Set x<br />
f a x = insert x a<br />
</haskell><br />
<br />
While we're at it, let's give a name to <hask>foldl f</hask>: <hask>stuff</hask>. So<br />
<br />
<haskell><br />
stuff :: Ord x => Set x -> [x] -> Set x<br />
stuff a [] = a<br />
stuff a (x:xs) = stuff (f a x) xs<br />
</haskell><br />
<br />
takes all the elements of the list it's given and stuffs them into the <hask>Set</hask> it's given.<br />
<br />
For some reason (maybe we're crazy; maybe we want to do weird things with fusion; who knows?) we want to write this using <hask>foldr</hask>. Haskell programmers like curry, so it's natural to see <hask>stuff a xs</hask> as <hask>(stuff a) xs</hask>&mdash;that is, to see <hask>stuff a</hask> as a function that takes a list and returns the result of folding <hask>f</hask> into the list starting with an accumulator value of <hask>a</hask>. This perspective, however, is the ''wrong one'' for what we're trying to do here. So let's change the order of the arguments of <hask>stuff</hask>.<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy [] a = a<br />
stuffy (x : xs) a = stuffy xs (f a x)<br />
</haskell><br />
<br />
So now we see that <hask>stuffy xs</hask> is a function that takes an accumulator and uses it as the initial value to fold <hask>f</hask> into <hask>xs</hask>. With this shift of perspective, we can rewrite <hask>stuffy</hask> just a little:<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy a [] = \a -> a<br />
stuffy (x : xs) = \a -> stuffy xs (f a x)<br />
</haskell><br />
<br />
Believe it or not, we're almost done! How is that? Let's parenthesize a bit for emphasis:<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy [] = (\a -> a)<br />
stuffy (x : xs) = \a -> (stuffy xs) (f a x)<br />
</haskell><br />
<br />
This isn't an academic paper, so we won't mention Graham Hutton's "Tuturial on the Universality and Expressiveness of Fold", but <hask>stuffy</hask> fits the <hask>foldr</hask> pattern:<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy ys = foldr whatsit (\a -> a) ys<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
Substituting this in,<br />
<br />
<haskell><br />
stuffy :: Ord x => [x] -> Set x -> Set x<br />
stuffy list a = (foldr whatsit (\a -> a) list) a<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
And that's just about it! We wanted <hask>stuff</hask>, however, not <hask>stuffy</hask>, so let's swap the argument order again:<br />
<br />
<haskell><br />
stuff :: Ord x => Set a -> [x] -> Set x<br />
stuff a list = (foldr whatsit (\a -> a) list) a<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
Now since we do want to be able to use general <hask>foldl</hask> forms, we should gneralize it again:<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a xs = (foldr whosit (\a -> a) list) a<br />
where<br />
whosit x r = \a -> r (f a x)<br />
</haskell.<br />
<br />
The way to look at this final expression is that <hask>whosit</hask> takes an element of the list, a function produced by folding <hask>f</hask> into the rest of the list, and the initial value, <hask>a</hask> of an accumulator. It applies <hask>f</hask> to the accumulator it's given and the list element, and passes the result forward to the function it got.</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Foldl_as_foldr_alternative&diff=58737Foldl as foldr alternative2014-09-04T01:32:57Z<p>Dfeuer: Created page with "This page explains how <hask>foldl</hask> can be written using <hask>foldr</hask>. Yes, there is already such a page! This one explains it differently. The..."</p>
<hr />
<div>This page explains how <hask>foldl</hask> can be written using <hask>foldr</hask>. Yes, there is already [[Foldl as foldr|such a page]]! This one explains it differently.<br />
<br />
The usual definition of <hask>foldl</hask> looks like this:<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a [] = a<br />
foldl f a (x : xs) = foldl f (f a x) xs<br />
</haskell><br />
<br />
Now the <hask>f</hask> never changes in the recursion. It turns out things will be simpler later if we pull it out:<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a list = go a list<br />
where<br />
go a [] = a<br />
go a (x : xs) = go (f a x) xs<br />
</haskell><br />
<br />
For some reason (maybe we're crazy; maybe we want to do weird things with fusion; who knows?) we want to write this using <hask>foldr</hask>. Haskell programmers like curry, so it's natural to see <hask>go a xs</hask> as <hask>(go a) xs</hask>&mdash;that is, to see <hask>go a</hask> as a function that takes a list and returns the result of folding <hask>f</hask> into the list starting with an accumulator value of <hask>a</hask>. This perspective, however, is the ''wrong one'' for what we're trying to do here. So let's change the order of the arguments of the helper:<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a list = go2 list a<br />
where<br />
go2 [] a = a<br />
go2 (x : xs) a = go2 xs (f a x)<br />
</haskell><br />
<br />
So now we see that <hask>go2 xs</hask> is a function that takes an accumulator and uses it as the initial value to fold <hask>f</hask> into <hask>xs</hask>. With this shift of perspective, we can rewrite <hask>go2</hask> just a little:<br />
<br />
<haskell><br />
foldl :: (a -> x -> r) -> a -> [x] -> r<br />
foldl f a list = go2 list a<br />
where<br />
go2 [] = \a -> a<br />
go2 (x : xs) = \a -> go2 xs (f a x)<br />
</haskell><br />
<br />
Believe it or not, we're almost done! How is that? Let's parenthesize a bit for emphasis:<br />
<br />
<haskell><br />
foldl f a list = go2 list a<br />
where<br />
go2 [] = (\a -> a)<br />
go2 (x : xs) = \a -> (go2 xs) (f a x)<br />
</haskell><br />
<br />
This isn't an academic paper, so we won't mention Graham Hutton's "Tuturial on the Universality and Expressiveness of Fold", but <hask>go2</hask> fits the <hask>foldr</hask> pattern:<br />
<br />
<haskell><br />
go2 ys = foldr whatsit (\a -> a) ys<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
Substituting this in,<br />
<br />
<haskell><br />
foldl f a list = (foldr whatsit (\a -> a) list) a<br />
where<br />
whatsit x r = \a -> r (f a x)<br />
</haskell><br />
<br />
And that's all she wrote! One way to look at this final expression is that <hask>whatsit</hask> takes an element of the list, a function produced by folding over the rest of the list, and the value of an accumulator. It applies <hask>f</hask> to the accumulator it's given and the list element, and passes the result forward to the function it got.</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Correctness_of_short_cut_fusion&diff=58680Correctness of short cut fusion2014-08-20T01:09:36Z<p>Dfeuer: /* foldr/build */</p>
<hr />
<div>==Short cut fusion==<br />
<br />
[[Short cut fusion]] allows elimination of intermediate data structures using rewrite rules that can also be performed automatically during compilation.<br />
<br />
The two most popular instances are the <hask>foldr</hask>/<hask>build</hask>- and the <hask>destroy</hask>/<hask>unfoldr</hask>-rule for Haskell lists.<br />
<br />
===<hask>foldr</hask>/<hask>build</hask>===<br />
<br />
The <hask>foldr</hask>/<hask>build</hask>-rule eliminates intermediate lists produced by <hask>build</hask> and consumed by <hask>foldr</hask>, where these functions are defined as follows:<br />
<br />
<haskell><br />
foldr :: (a -> b -> b) -> b -> [a] -> b<br />
foldr c n [] = n<br />
foldr c n (x:xs) = c x (foldr c n xs)<br />
<br />
build :: (forall b. (a -> b -> b) -> b -> b) -> [a]<br />
build g = g (:) []<br />
</haskell><br />
<br />
Note the ''rank-2 polymorphic'' type of <hask>build</hask>.<br />
<br />
The <hask>foldr</hask>/<hask>build</hask>-rule now means the following replacement for appropriately typed <hask>g</hask>, <hask>c</hask>, and <hask>n</hask>:<br />
<br />
<haskell><br />
foldr c n (build g) → g c n<br />
</haskell><br />
<br />
===<hask>destroy</hask>/<hask>unfoldr</hask>===<br />
<br />
The <hask>destroy</hask>/<hask>unfoldr</hask>-rule eliminates intermediate lists produced by <hask>unfoldr</hask> and consumed by <hask>destroy</hask>, where these functions are defined as follows:<br />
<br />
<haskell><br />
destroy :: (forall b. (b -> Maybe (a,b)) -> b -> c) -> [a] -> c<br />
destroy g = g step<br />
<br />
step :: [a] -> Maybe (a,[a])<br />
step [] = Nothing<br />
step (x:xs) = Just (x,xs)<br />
<br />
unfoldr :: (b -> Maybe (a,b)) -> b -> [a]<br />
unfoldr p e = case p e of Nothing -> []<br />
Just (x,e') -> x:unfoldr p e'<br />
</haskell><br />
<br />
Note the ''rank-2 polymorphic'' type of <hask>destroy</hask>.<br />
<br />
The <hask>destroy</hask>/<hask>unfoldr</hask>-rule now means the following replacement for appropriately typed <hask>g</hask>, <hask>p</hask>, and <hask>e</hask>:<br />
<br />
<haskell><br />
destroy g (unfoldr p e) → g p e<br />
</haskell><br />
<br />
==Correctness==<br />
<br />
If the <hask>foldr</hask>/<hask>build</hask>- and the <hask>destroy</hask>/<hask>unfoldr</hask>-rule are to be automatically performed during compilation, as is possible using [[GHC]]'s ''RULES pragmas'', we clearly want them to be equivalences.<br />
That is, the left- and right-hand sides should be semantically the same for each instance of either rule.<br />
Unfortunately, this is not so in Haskell.<br />
<br />
We can distinguish two situations, depending on whether <hask>g</hask> is defined using <hask>seq</hask> or not.<br />
<br />
===In the absence of <hask>seq</hask>===<br />
<br />
====<hask>foldr</hask>/<hask>build</hask>====<br />
<br />
If <hask>g</hask> does not use <hask>seq</hask>, then the <hask>foldr</hask>/<hask>build</hask>-rule really is a semantic equivalence, that is, it holds that <br />
<br />
<haskell><br />
foldr c n (build g) = g c n<br />
</haskell><br />
<br />
The two sides are interchangeable in any program without affecting semantics.<br />
<br />
====<hask>destroy</hask>/<hask>unfoldr</hask>====<br />
<br />
The <hask>destroy</hask>/<hask>unfoldr</hask>-rule, however, is not a semantic equivalence.<br />
To see this, consider the following instance:<br />
<br />
<haskell><br />
g = \x y -> case x y of Just z -> 0<br />
p = \x -> if x==0 then Just undefined else Nothing<br />
e = 0<br />
</haskell><br />
<br />
These values have appropriate types for being used in the <hask>destroy</hask>/<hask>unfoldr</hask>-rule. But with them, that rule's left-hand side "evaluates" as follows:<br />
<br />
<haskell><br />
destroy g (unfoldr p e) = g step (unfoldr p e)<br />
= case step (unfoldr p e) of Just z -> 0<br />
= case step (case p e of Nothing -> []<br />
Just (x,e') -> x:unfoldr p e') of Just z -> 0<br />
= case step (case Just undefined of Nothing -> []<br />
Just (x,e') -> x:unfoldr p e') of Just z -> 0<br />
= undefined<br />
</haskell><br />
<br />
while its right-hand side "evaluates" as follows:<br />
<br />
<haskell><br />
g p e = case p e of Just z -> 0<br />
= case Just undefined of Just z -> 0<br />
= 0<br />
</haskell><br />
<br />
Thus, by applying the <hask>destroy</hask>/<hask>unfoldr</hask>-rule, a nonterminating (or otherwise failing) program can be transformed into a safely terminating one.<br />
The obvious questions now are:<br />
<br />
# Can the converse also happen, that is, can a safely terminating program be transformed into a failing one?<br />
# Can a safely terminating program be transformed into another safely terminating one that gives a different value as result?<br />
<br />
There is no formal proof yet, but strong evidence supporting the conjecture that the answer to both questions is "'''No!'''".<br />
<br />
The conjecture goes that if <hask>g</hask> does not use <hask>seq</hask>, then the <hask>destroy</hask>/<hask>unfoldr</hask>-rule is a semantic approximation from left to right, that is, it holds that<br />
<br />
<haskell><br />
destroy g (unfoldr p e) ⊑ g p e<br />
</haskell><br />
<br />
What ''is'' known is that semantic equivalence can be recovered here by putting moderate restrictions on p.<br />
More precisely, if <hask>g</hask> does not use <hask>seq</hask> and <hask>p</hask> is a strict function that never returns <hask>Just ⊥</hask> (where ⊥ denotes any kind of failure or nontermination), then indeed:<br />
<br />
<haskell><br />
destroy g (unfoldr p e) = g p e<br />
</haskell><br />
<br />
===In the presence of <hask>seq</hask>===<br />
<br />
This is the more interesting setting, given that in Haskell there is no way to restrict the use of <hask>seq</hask>, so in any given program we must be prepared for the possibility that the <hask>g</hask> appearing in the <hask>foldr</hask>/<hask>build</hask>- or the <hask>destroy</hask>/<hask>unfoldr</hask>-rule is defined using <hask>seq</hask>.<br />
Unsurprisingly, it is also the setting in which more can go wrong than above.<br />
<br />
====<hask>foldr</hask>/<hask>build</hask>====<br />
<br />
In the presence of <hask>seq</hask>, the <hask>foldr</hask>/<hask>build</hask>-rule is not necessarily a semantic equivalence.<br />
The instance<br />
<br />
<haskell><br />
g = seq<br />
c = undefined<br />
n = 0<br />
</haskell><br />
<br />
shows, via similar "evaluations" as above, that the right-hand side (<hask>g c n</hask>) can be strictly less defined than the left-hand side (<hask>foldr c n (build g)</hask>).<br />
<br />
The converse cannot happen, because the following always holds:<br />
<br />
<haskell><br />
foldr c n (build g) ⊒ g c n<br />
</haskell><br />
<br />
Moreover, semantic equivalence can again be recovered by putting restrictions on the involved functions.<br />
<br />
On the consumption side, if <hask>(c ⊥ ⊥) ≠ ⊥</hask> and <hask>n ≠ ⊥</hask>, then even in the presence of <hask>seq</hask>:<br />
<br />
<haskell><br />
foldr c n (build g) = g c n<br />
</haskell><br />
<br />
On the production side, <hask>seq</hask> can be used safely as long as it is never used to force anything whose type <hask>build</hask> expects to be polymorphic. In particular, the function passed to build must not force either of its arguments, and must not force anything constructed using them. For example, in<br />
<br />
<haskell><br />
f x = build (\c n -> x `seq` (x `c` n))<br />
</haskell><br />
<br />
The usual equivalence holds, regardless of <hask>c</hask> and <hask>n</hask>:<br />
<br />
<haskell><br />
fold c n (f x) = x `seq` (x `c` n)<br />
</haskell><br />
<br />
For a more interesting example, we can define<br />
<br />
<haskell><br />
hyloList f q c n =<br />
case f q of<br />
Nothing -> n<br />
Just (x,q') -> x `c` hyloList f q' c n<br />
<br />
unfoldr f q = build (hyloList f q)<br />
</haskell><br />
<br />
Note that if <hask>f</hask> or <hask>q</hask> uses <hask>seq</hask>, then that will appear in the argument to <hask>build</hask>, but that is still safe because <hask>f</hask> and </hask>q</hask> have no way to get their hands on <hask>c</hask>, <hask>n</hask>, or anything built from them.<br />
<br />
====<hask>destroy</hask>/<hask>unfoldr</hask>====<br />
<br />
Contrary to the situation without <hask>seq</hask>, now also the <hask>destroy</hask>/<hask>unfoldr</hask>-rule may decrease the definedness of a program.<br />
This is witnessed by the following instance:<br />
<br />
<haskell><br />
g = \x y -> seq x 0<br />
p = undefined<br />
e = 0<br />
</haskell><br />
<br />
Here the left-hand side of the rule (<hask>destroy g (unfoldr p e)</hask>) yields <hask>0</hask>, while the right-hand side (<hask>g p e</hask>) yields <hask>undefined</hask>.<br />
<br />
Conditions for semantic approximation in either direction can be given as follows.<br />
<br />
If <hask>p ≠ ⊥</hask> and <hask>(p ⊥)</hask> ∈ {<hask>⊥</hask>, <hask>Just ⊥</hask>}, then: <br />
<br />
<haskell><br />
destroy g (unfoldr p e) ⊑ g p e<br />
</haskell><br />
<br />
If <hask>p</hask> is strict and total and never returns <hask>Just ⊥</hask>, then:<br />
<br />
<haskell><br />
destroy g (unfoldr p e) ⊒ g p e<br />
</haskell><br />
<br />
Of course, conditions for semantic equivalence can be obtained by combining the two laws above.<br />
<br />
==Discussion==<br />
<br />
Correctness of short cut fusion is not just an academic issue.<br />
All recent versions of [[GHC]] (at least 6.0 - 6.6) automatically perform transformations like <hask>foldr</hask>/<hask>build</hask> during their optimization pass (also in the disguise of more specialized rules such as <hask>head</hask>/<hask>build</hask>). The rules are specified in the GHC.Base and GHC.List modules.<br />
There has been at least one occasion where, as a result, a safely terminating program was turned into a failing one "in the wild", with a less artificial example than the ones given above.<br />
<br />
===<hask>foldr</hask>/<hask>build</hask>===<br />
<br />
As pointed out above, everything is fine with <hask>foldr</hask>/<hask>build</hask> in the absence of <hask>seq</hask>.<br />
If the producer (<hask>build g</hask>) of the intermediate list may be defined using <hask>seq</hask>, then the conditions <hask>(c ⊥ ⊥) ≠ ⊥</hask> and <hask>n ≠ ⊥</hask> better be satisified, lest the compiler transform a perfectly fine program into a failing one.<br />
<br />
The mentioned conditions are equivalent to requiring that the consumer (<hask>foldr c n</hask>) is a total function, that is, maps non-⊥ lists to a non-⊥ value.<br />
It is thus relatively easy to identify whether a list consumer defined in terms of <hask>foldr</hask> is eligible for <hask>foldr</hask>/<hask>build</hask>-fusion in the presence of <hask>seq</hask> or not.<br />
For example, the Prelude functions <hask>head</hask> and <hask>sum</hask> are generally not, while <hask>map</hask> is.<br />
<br />
There is, however, currently no way to detect automatically, inside the compiler, whether a particular instance of <hask>foldr</hask>/<hask>build</hask>-fusion is safe, i.e., whether the producer avoids <hask>seq</hask> or the consumer is total.<br />
<br />
===<hask>destroy</hask>/<hask>unfoldr</hask>===<br />
<br />
As above, the compiler cannot figure out automatically whether (and how) a given instance of <hask>destroy</hask>/<hask>unfoldr</hask>-fusion will change the semantics of a program.<br />
<br />
An easy way to get rid of the condition regarding <hask>p</hask> never returning <hask>Just ⊥</hask> is to slightly change the definitions of the functions involved:<br />
<br />
<haskell><br />
data Step a b = Done | Yield a b<br />
<br />
destroy' :: (forall b. (b -> Step a b) -> b -> c) -> [a] -> c<br />
destroy' g = g step'<br />
<br />
step' :: [a] -> Step a [a]<br />
step' [] = Done <br />
step' (x:xs) = Yield x xs<br />
<br />
unfoldr' :: (b -> Step a b) -> b -> [a]<br />
unfoldr' p e = case p e of Done -> []<br />
Yield x e' -> x:unfoldr' p e'<br />
</haskell><br />
<br />
The new type <hask>Step a b</hask> is almost isomorphic to <hask>Maybe (a,b)</hask>, but avoids the "junk value" <hask>Just ⊥</hask>. This change does not affect the expressiveness of <hask>unfoldr</hask> or <hask>unfoldr'</hask> with respect to list producers.<br />
But it allows some of the laws above to be simplified a bit.<br />
<br />
We would still have that if <hask>g</hask> does not use <hask>seq</hask>, then:<br />
<br />
<haskell><br />
destroy g' (unfoldr' p e) ⊑ g p e<br />
</haskell><br />
<br />
Moreover, if <hask>g</hask> does not use <hask>seq</hask> and <hask>p</hask> is strict, then even:<br />
<br />
<haskell><br />
destroy' g (unfoldr' p e) = g p e<br />
</haskell><br />
<br />
In the potential presence of <hask>seq</hask>, if <hask>p ≠ ⊥</hask> and <hask>p</hask> is strict, then: <br />
<br />
<haskell><br />
destroy' g (unfoldr' p e) ⊑ g p e<br />
</haskell><br />
<br />
Also without restriction regarding <hask>seq</hask>, if <hask>p</hask> is strict and total, then:<br />
<br />
<haskell><br />
destroy' g (unfoldr' p e) ⊒ g p e<br />
</haskell><br />
<br />
The worst change in program behavior from a complier user's point of view is when, through application of "optimization" rules, a safely terminating program is transformed into a failing one or one delivering a different result.<br />
This can happen in the presence of <hask>seq</hask>, for example with a producer of the form<br />
<br />
<haskell><br />
repeat x = unfoldr (\y -> Just (x,y)) undefined<br />
</haskell><br />
<br />
or <br />
<br />
<haskell><br />
repeat x = unfoldr' (\y -> Yield x y) undefined<br />
</haskell><br />
<br />
Fortunately, it cannot happen for any producer of a nonempty, spine-total list (i.e., one that contains at least one element and ends with <hask>[]</hask>).<br />
The reason is that for any such producer expressed via <hask>unfoldr</hask> or <hask>unfoldr'</hask> the conditions imposed on <hask>p</hask> in the left-to-right approximation laws above are necessarily fulfilled.<br />
<br />
A left-to-right approximation as in <br />
<br />
<haskell><br />
destroy g (unfoldr p e) ⊑ g p e<br />
</haskell><br />
<br />
under suitable preconditions might be acceptable in practice.<br />
After all, it only means that the transformed program may be "more terminating" than the original one, but not less so.<br />
<br />
If one insists on semantic equivalence rather than approximation, then the conditions imposed on the producer of the intermediate list become quite severe, in particular in the potential presence of <hask>seq</hask>.<br />
For example, the following producer has to be outlawed then: <br />
<br />
<haskell><br />
enumFromTo n m = unfoldr (\i -> if i>m then Nothing else Just (i,i+1)) n<br />
</haskell><br />
<br />
==Literature==<br />
<br />
Various parts of the above story, and elaborations thereof, are also told in the following papers:<br />
<br />
* A. Gill, J. Launchbury, and S.L. Peyton Jones. [http://doi.acm.org/10.1145/165180.165214 A short cut to deforestation]. Functional Programming Languages and Computer Architecture, Proceedings, pages 223-232, ACM Press, 1993.<br />
* J. Svenningsson. [http://doi.acm.org/10.1145/581478.581491 Shortcut fusion for accumulating parameters & zip-like functions]. International Conference on Functional Programming, Proceedings, pages 124-132, ACM Press, 2002.<br />
* P. Johann. [http://dx.doi.org/10.1017/S0960129504004578 On proving the correctness of program transformations based on free theorems for higher-order polymorphic calculi]. Mathematical Structures in Computer Science, 15:201-229, 2005.<br />
* P. Johann and J. Voigtländer. [http://iospress.metapress.com/openurl.asp?genre=article&issn=0169-2968&volume=69&issue=1&spage=63 The impact of seq on free theorems-based program transformations]. Fundamenta Informaticae, 69:63-102, 2006.<br />
* J. Voigtländer and P. Johann. [http://dx.doi.org/10.1016/j.tcs.2007.09.014 Selective strictness and parametricity in structural operational semantics, inequationally]. Theoretical Computer Science, 388:290-318, 2007.<br />
* J. Voigtländer. [http://doi.acm.org/10.1145/1328408.1328412 Proving Correctness via Free Theorems: The Case of the destroy/build-Rule]. Partial Evaluation and Semantics-Based Program Manipulation, Proceedings, pages 13-20, ACM Press, 2008.<br />
* J. Voigtländer. [http://dx.doi.org/10.1007/978-3-540-78969-7_13 Semantics and Pragmatics of New Shortcut Fusion Rules]. Functional and Logic Programming, Proceedings, LNCS 4989:163-179, Springer-Verlag, 2008.<br />
* P. Johann and J. Voigtländer. [http://wwwtcs.inf.tu-dresden.de/~voigt/iandc.pdf A family of syntactic logical relations for the semantics of Haskell-like languages]. Information and Computation, 207:341-368, 2009.<br />
<br />
<br />
<br />
[[Category:Tutorials]]<br />
[[Category:Program transformation]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Talk:Correctness_of_short_cut_fusion&diff=58654Talk:Correctness of short cut fusion2014-08-15T03:22:47Z<p>Dfeuer: /* Details about what uses of seq are dangerous */ new section</p>
<hr />
<div>If unfoldr would use a lazy pattern match:<br />
<haskell><br />
unfoldr :: (b -> Maybe (a,b)) -> b -> [a]<br />
unfoldr p e = case p e of Nothing -> []<br />
Just ~(x,e') -> x:unfoldr p e'<br />
</haskell><br />
the left hand side of the example without seq will be the same as the right hand side:<br />
<haskell><br />
destroy g (unfoldr p e) = g step (unfoldr p e)<br />
= case step (unfoldr p e) of Just z -> 0<br />
= case step (case p e of Nothing -> []<br />
Just ~(x,e') -> x:unfoldr p e') of Just z -> 0<br />
= case step (case Just undefined of Nothing -> []<br />
Just ~(x,e') -> x:unfoldr p e') of Just z -> 0<br />
= case step (undefined:unfoldr p undefined) of Just z -> 0<br />
= case Just (undefined,unfoldr p undefined) of Just z -> 0<br />
= 0<br />
</haskell><br />
--[[User:Twanvl|Twanvl]] 12:18, 13 February 2007 (UTC)<br />
<br />
== Details about what uses of seq are dangerous ==<br />
<br />
If I understand things properly, the essential problem with <hask>seq</hask> in <hask>foldr/build</hask> is that it allows the builder function<br />
<haskell><br />
g :: forall b . (a -> b -> b) -> b -> b<br />
g cons nil = ...<br />
</haskell> <br />
to do something with values of type <hask>b</hask> other than pass them to <hask>cons</hask>, namely to <hask>seq</hask> against them. If <hask>g</hask> doesn't force anything whose type includes <hask>b</hask>, it should, I believe, be safe to fuse. For example, <hask>unfoldr</hask> written using <hask>build</hask> should, I believe, be safe to fuse because the function it is given isn't passed the polymorphic cons and nil arguments&mdash;the list generation is left up to the known-<hask>seq</hask>-free machinery of <hask>unfoldr</hask>. --[[User:Dfeuer|Dfeuer]] ([[User talk:Dfeuer|talk]]) 03:22, 15 August 2014 (UTC)</div>Dfeuerhttps://wiki.haskell.org/index.php?title=FAQ&diff=58637FAQ2014-08-07T07:33:53Z<p>Dfeuer: /* How can I find type errors? */</p>
<hr />
<div>This FAQ is based on actual frequently-asked questions from [[IRC channel|<tt>#haskell</tt> IRC]]. The goal is simply to collect and edit some common answers. Beginner questions are still welcome on IRC, as always.<br />
<br />
This is a wiki, so please edit the text with any improvements you have. And feel free to add new questions, if they are frequently asked.<br />
<br />
== See also ==<br />
<br />
* [[:Category:FAQ|Other FAQs]]<br />
* The [[Introduction|introduction to Haskell]], and the FAQ at the end of that document.<br />
<br />
__TOC__<br />
<br />
= The real world =<br />
<br />
== Should I learn Haskell? ==<br />
<br />
That depends on your goals. In general, Haskellers will tell you that you should learn Haskell. :)<br />
<br />
Learning Haskell is fun. It will expand your mind and make you a better programmer in other languages. These are the immediate benefits.<br />
<br />
Haskell is also a great tool for solving real-world problems, but it can take many months of study to get to that point.<br />
<br />
== Is Haskell hard to learn? ==<br />
<br />
Any competent programmer can learn Haskell, but it will take more time and motivation than you may expect.<br />
<br />
Haskell requires learning a new way to think, not just new syntax for old concepts. This can be incredibly frustrating, as simple tasks seem impossibly difficult.<br />
<br />
Those with no prior programming experience may actually have an easier time learning Haskell, because they have less to un-learn.<br />
<br />
== How can I get started with Haskell ''right now''? ==<br />
<br />
Check out [http://tryhaskell.org/ Try Haskell].<br />
<br />
== What should I read for learning Haskell? ==<br />
<br />
The most popular resources are [http://learnyouahaskell.com/ ''Learn You a Haskell''] and [http://book.realworldhaskell.org/ ''Real World Haskell'']. Each is available online for free, or can be purchased in hardcopy.<br />
<br />
Many other [[tutorials]], [[books]], and [[Learning Haskell|other resources]] are available.<br />
<br />
== How can I get help with learning Haskell? ==<br />
<br />
Your options include:<br />
<br />
* [[IRC channel|<tt>#haskell</tt> on Freenode IRC]]<br />
* [http://stackoverflow.com/questions/tagged/haskell Stack Overflow]<br />
* The [http://www.haskell.org/mailman/listinfo/haskell-cafe Haskell-Cafe] mailing list<br />
<br />
== Will Haskell get me a job? ==<br />
<br />
There are plenty of [[Haskell in industry|companies using Haskell]], but it's still a tiny number compared to the software industry as a whole.<br />
<br />
There are also many companies which do not use Haskell, but prefer to hire people who know Haskell. It indicates that you learned something hard and obscure just for fun, which employers take as a sign of intelligence.<br />
<br />
== Is Haskell similar to Language X? ==<br />
<br />
Probably not. It's best if you approach Haskell with a clean slate. Most analogies to another language will break down somewhere, often in a subtle and misleading way. If you first learn the Haskell concepts for what they are, you can then draw useful connections to other languages.<br />
<br />
== What's the relationship between Haskell and GHC? ==<br />
<br />
Haskell is not a piece of software; it is a specification for a standardized programming language. The [[Language and library specification|latest version of the spec]] is the Haskell 2010 Report.<br />
<br />
[http://www.haskell.org/ghc/ GHC] is the Glorious Glasgow Haskell Compiler. It is by far the most popular and &quot;production-ready&quot; implementation of the standard Haskell language. It also implements many [http://www.haskell.org/ghc/docs/latest/html/users_guide/ghc-language-features.html extension] features that go above and beyond standard Haskell. Many programs use these features and so aren't &quot;written in Haskell&quot; in the strictest sense.<br />
<br />
You can use the term &quot;Haskell&quot; to refer to the standard language, and &quot;GHC Haskell&quot; when including GHC extensions.<br />
<br />
Besides GHC, several other [[implementations]] of Haskell are available. Each one provides its own extensions, some of which don't exist in GHC.<br />
<br />
== What is the Haskell Platform? ==<br />
<br />
The [http://hackage.haskell.org/platform/ Haskell Platform] is a copy of GHC bundled with a &quot;blessed&quot; set of useful libraries. It is the easiest way to get started with Haskell. It's not essential to start with the Platform, because you can install the same libraries as needed.<br />
<br />
== What is Haskell Prime (Haskell')? ==<br />
<br />
[http://hackage.haskell.org/trac/haskell-prime/ Haskell Prime] is a process which produces new versions of the Haskell language spec. It does not refer to a particular present or future version of Haskell.<br />
<br />
== My textbook uses Haskell 98. Is it very different from Haskell 2010? ==<br />
<br />
No. Haskell 2010 is a [http://www.haskell.org/pipermail/haskell/2009-November/021750.html very conservative change] to Haskell 98. It fixes small syntactic flaws, and standardizes several well-behaved extensions which GHC has supported for years.<br />
<br />
The standardization process is very slow because standardizing a flawed language can be a costly mistake. Extensions are accepted only once they are considered mature and well-understood.<br />
<br />
== How do I get libraries for Haskell? ==<br />
<br />
You can find libraries on [http://hackage.haskell.org/packages/archive/pkg-list.html Hackage], and install them with [[Cabal-Install|<tt>cabal-install</tt>]].<br />
<br />
== Is Haskell compiled? ==<br />
<br />
Usually. GHC, the most popular Haskell implementation, has an optimizing ahead-of-time native-code compiler, as well as a bytecode compiler and interpreter for interactive use.<br />
<br />
Haskell itself is not a &quot;compiled language&quot; because nothing in the Haskell spec requires implementations to be compilers.<br />
<br />
== Does Haskell have an interpreter? ==<br />
<br />
Yes, but maybe you instead mean &quot;Is there a program where I can type Haskell code and see it run immediately?&quot;. GHCi provides such a &quot;read-evaluate-print loop&quot;.<br />
<br />
= Paradigms =<br />
<br />
== Is learning Haskell the best way to learn functional programming? ==<br />
<br />
Not necessarily! Haskell is not a ''typical'' functional language. It can be overwhelming to learn the basic concepts of functional programming alongside static types, algebraic data, laziness, type classes, first-class IO, etc. For an introduction to FP by itself you might want to learn Scheme, or play with the FP features in your current favorite language.<br />
<br />
That said, many people choose Haskell as an introduction to FP and have success with that approach. Haskell has an extremely active community of people teaching, doing research, writing libraries, etc. Haskell is where interesting things happen in the FP space, so it's an exciting place to jump in.<br />
<br />
== I heard Haskell is pure functional. Does that mean I can't do imperative / OOP / aspect-oriented / logic programming in Haskell? ==<br />
<br />
No, &quot;pure functional&quot; has a specific technical meaning. It doesn't mean that functional is the only supported paradigm.<br />
<br />
Paradigms describe the techniques used in a particular program. For example, the Linux kernel is written in C, with pervasive use of functional, object-oriented, and aspect-oriented programming. The most we can say about a ''language'' is that it encourages or discourages a particular paradigm. Haskell is very flexible and can comfortably accommodate most paradigms, even when there is no built-in support.<br />
<br />
== I heard Haskell is pure functional. Does that mean it can't do IO? ==<br />
<br />
No; [[Introduction to IO|IO in Haskell]] is straightforward.<br />
<br />
== I heard Haskell is pure functional. Does that mean it doesn't have mutable state? ==<br />
<br />
No; see [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-IORef.html <tt>IORef</tt>] for a simple example. A more sophisticated example is [http://book.realworldhaskell.org/read/software-transactional-memory.html software transactional memory], which provides concurrent state more sophisticated than you'll find in most other imperative languages.<br />
<br />
== Wait, is Haskell imperative or is it functional? ==<br />
<br />
Both. In Haskell, functions are first class, and so are imperative actions.<br />
<br />
There is no reason to consider &quot;imperative language&quot; and &quot;functional language&quot; as mutually exclusive. It's only a historical accident that a few of the most popular imperative languages are unusually bad at functional programming. Functional imperative programming is extremely powerful and is supported by many languages.<br />
<br />
= Math =<br />
<br />
== Was Haskell designed by mathematicians? ==<br />
<br />
Haskell was designed by people studying programming language design. Perhaps programmers would consider them to be mathematicians, while mathematicians would consider them to be programmers.<br />
<br />
Designing a programming language is a hard thing to do. There are many non-obvious tradeoffs, and many lessons to be learned from past failures and successes. Yet many of today's most popular languages were designed by people who hadn't done their homework.<br />
<br />
Haskell was designed by people who knew what they were doing. It's not perfect, but the contrast to an amateur's design is striking.<br />
<br />
== Do I need to know advanced math in order to use Haskell? ==<br />
<br />
No. Certain concepts in Haskell are named after concepts in advanced math. But other languages also appropriate math terminology: consider &quot;[http://en.wikipedia.org/wiki/Singleton_pattern singleton]&quot;, not to mention &quot;function&quot; and &quot;variable&quot;. The way these programming concepts relate to actual mathematics is not necessarily important or relevant.<br />
<br />
In addition, some people write articles about advanced math, using Haskell syntax as their notation. These articles are interesting, but the connection to everyday programming work is usually remote.<br />
<br />
Knowing advanced math will enrich your experience using Haskell, but is by no means a prerequisite.<br />
<br />
= Types =<br />
<br />
== Doesn't a static type system just make it harder to write programs? ==<br />
<br />
Yes. In particular, it makes it ''much'' harder to write ''incorrect'' programs.<br />
<br />
The tradeoff is that correct programs also become somewhat harder to write. In Haskell, features like type inference mitigate this burden to a large extent.<br />
<br />
== How do I make a list with elements of different types? ==<br />
<br />
Are you sure that's what you want? Consider instead creating a single data type to encompass the alternatives:<br />
<br />
<haskell><br />
data Identifier<br />
= ByNumber Int<br />
| ByName String<br />
<br />
doStuff :: [Identifier] -> Whatever<br />
</haskell><br />
<br />
In many dynamically-typed languages you aren't allowed to create &quot;variant types&quot; like this. The type system itself is used as a single ad-hoc global variant type. Keep this in mind if you're translating designs from a dynamically-typed language to Haskell.<br />
<br />
== No really, how do I make a list of elements of different types? ==<br />
<br />
Well, you can't avoid putting all your values into one type. But sometimes the &quot;variant type&quot; approach above is too restrictive. Maybe you need to let other people add to the set of allowed types, the way [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.html Control.Exception] allows users to define new exception types.<br />
<br />
You can use an [[existential type]], possibly with a type class. Or you can use [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Dynamic.html Data.Dynamic].<br />
<br />
== I'm making an RPG. Should I define a type for each kind of monster, and a type class for them? ==<br />
<br />
Probably not. Some languages require a new type for each new behavior. In Haskell, behaviors are functions or IO actions, which are first-class values. So you can store behaviors in an ordinary data type:<br />
<br />
<haskell><br />
data MonsterOps = MonsterOps<br />
{ new :: Monster<br />
, move :: Monster -> Monster<br />
, attack :: Monster -> Player -> Player }<br />
<br />
data Monster = Monster<br />
{ position :: (Int, Int)<br />
, hitpoints :: Double }<br />
<br />
beholder :: MonsterOps<br />
beholder = MonsterOps new move attack where<br />
new = Monster (0,0) 9000<br />
move self = ...<br />
attack self player = ...<br />
</haskell><br />
<br />
This approach is especially nice if you want to generate or transform behaviors on the fly. See the article [http://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/ &quot;Haskell Antipattern: Existential Typeclass&quot;] for a longer discussion.<br />
<br />
== What's the difference between <hask>Integer</hask> and <hask>Int</hask>? ==<br />
<br />
<p><hask>Integer</hask> can represent arbitrarily large integers, up to using all of the storage on your machine.</p><br />
<br />
<p><hask>Int</hask> can only represent integers in a finite range. The [http://www.haskell.org/onlinereport/haskell2010/haskellch6.html#dx13-135009 language standard] only guarantees a range of -2<sup>29</sup> to (2<sup>29</sup> - 1). Most implementations will provide a full machine-size signed integer, i.e. 32 or 64 bits.</p><br />
<br />
Operations on <hask>Int</hask> can be much faster than operations on <hask>Integer</hask>, but overflow and underflow can cause weird bugs. Using <hask>Int</hask> in an initial design could be considered premature optimization. Unfortunately, many standard library functions (e.g. <hask>length</hask>, <hask>take</hask>) use <hask>Int</hask>.<br />
<br />
== How do I convert type <tt>A</tt> to type <tt>B</tt>? ==<br />
<br />
This is just another way of asking for a function of type <hask>A -> B</hask>. For example, you can convert <hask>Double</hask> to <hask>Int</hask> with <hask>round</hask>, <hask>ceiling</hask>, or <hask>floor</hask>. Haskell does not privilege one of these as ''the'' conversion.<br />
<br />
== Does Haskell have type casts? ==<br />
<br />
The word &quot;cast&quot; can mean a lot of different things.<br />
<br />
* You want to convert a value from one type to another, preserving some idea of what it means. For example, you might convert an <hask>Int</hask> to a <hask>Double</hask> which represents the same integer. In this case you'd just use a function of type <hask>Int -> Double</hask>, such as <hask>fromIntegral</hask>. Haskell doesn't provide special rules or syntax for these functions. See also the previous question.<br /><br /><br />
* You want to pass a value of more specific type to a function expecting a less specific type. There's no syntax for this in Haskell; you just do it. For example you can pass <hask>x :: Int</hask> to <hask>show :: (Show a) => a -> String</hask>, which automatically specializes the type of <hask>show</hask> to <hask>Int -> String</hask>. Note that Haskell does not have subtyping, so this only happens in the context of instantiating type variables.<br /><br /><br />
* You want to use a value of less specific type under the assumption of a more specific type, with a checkable runtime error if they do not match. This is rarely the right way to do things in Haskell, and probably indicates a conceptual / design problem instead. If you really do need such a cast, you can use [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Typeable.html#v:cast <tt>cast</tt>] from [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Typeable.html Data.Typeable]. In this case the &quot;checkable runtime error&quot; is <hask>cast</hask> returning <hask>Nothing</hask>. Note that Haskell does not have subtyping, so this only happens in the context of instantiating type variables.<br /><br /><br />
* You want to use a value of less specific type under the assumption of a more specific type, and if the assumption is incorrect, the program is allowed to segfault / silently corrupt data / give the attacker a root shell / send illicit photos to your boss. Also known as a "reinterpret cast". GHC Haskell has a way to do this, but I dare not speak its name. It's so dangerous and so unlikely to be what you want that it has no place in a general FAQ. You can ask on IRC or read the docs if you have the right kind of morbid curiosity.<br />
<br />
== How do I convert from one numeric type to another? ==<br />
<br />
Probably using one of these:<br />
<br />
<haskell><br />
fromIntegral :: (Integral a, Num b ) => a -> b<br />
realToFrac :: (Real a, Fractional b) => a -> b<br />
</haskell><br />
<br />
<p><hask>fromIntegral</hask> converts to a wider range of types, but <hask>realToFrac</hask> converts from types which aren't integers.</p><br />
<br />
== How do I convert <hask>Maybe Int</hask> to <hask>Int</hask>? ==<br />
<br />
Use pattern-matching. If <hask>mx :: Maybe Int</hask>:<br />
<br />
<haskell><br />
case mx of<br />
Just x -> ...<br />
Nothing -> ...<br />
</haskell><br />
<br />
This forces you to consider the <hask>Nothing</hask> case, and is the main advantage of <hask>Maybe</hask>, compared to adding a <tt>null</tt> value to every type.<br />
<br />
See also the functions [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html#v:maybe <tt>maybe</tt>] and [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html#v:fromMaybe <tt>fromMaybe</tt>] in the module [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html Data.Maybe].<br />
<br />
''Do not'' use <hask>fromJust</hask>, because passing <hask>Nothing</hask> will crash your program with a supremely unhelpful error message. Even when you want to assume the value is not <hask>Nothing</hask>, you can provide a better error message:<br />
<br />
<haskell><br />
let x = fromMaybe (error "custom error message") mx in ...<br />
</haskell><br />
<br />
If you pattern-match without a <hask>Nothing</hask> case:<br />
<br />
<haskell><br />
let Just x = mx in ...<br />
</haskell><br />
<br />
you'll at least get a line number in the error message:<br />
<br />
<pre><br />
*** Exception: foo.hs:2:9-24: Irrefutable pattern failed for pattern Data.Maybe.Just x<br />
</pre><br />
<br />
== How do I convert <hask>IO Int</hask> to <hask>Int</hask>? ==<br />
<br />
You can't; they represent totally different things. An <hask>Int</hask> is an integer. An <hask>IO Int</hask> is a description of how some IO could be performed, in the future, to produce an integer. The IO hasn't been performed yet, and might never happen or might happen more than once.<br />
<br />
See the [[Introduction to IO]].<br />
<br />
== How do I convert between <hask>String</hask> (or <hask>Text</hask>) and <hask>ByteString</hask>? ==<br />
<br />
<p><hask>String</hask> represents a sequence of Unicode characters. <hask>ByteString</hask> represents a sequence of bytes. There are many different, incompatible ways to represent Unicode characters as bytes. See [http://www.joelonsoftware.com/articles/Unicode.html this article] if you're fuzzy on the character / byte distinction.</p><br />
<br />
The module [http://hackage.haskell.org/packages/archive/text/0.11.1.5/doc/html/Data-Text-Encoding.html Data.Text.Encoding] from the <tt>text</tt> package provides functions for common Unicode encodings. For more obscure / legacy encodings, see the [http://hackage.haskell.org/package/text-icu <tt>text-icu</tt>] package.<br />
<br />
== How do I catch the error thrown by <hask>read</hask> on a parse failure? ==<br />
<br />
Don't. Instead use<br />
<br />
<haskell><br />
reads :: (Read a) => String -> [(a, String)]<br />
</haskell><br />
<br />
which returns a list of parses, each with a value and a remaining string. An example:<br />
<br />
<haskell><br />
safeRead :: (Read a) => String -> Maybe a<br />
safeRead x = case reads x of<br />
[(v,"")] -> Just v<br />
_ -> Nothing<br />
</haskell><br />
<br />
== What's the difference between <hask>type</hask>, <hask>data</hask>, and <hask>newtype</hask>? ==<br />
<br />
<p><hask>type</hask> introduces a synonym, which is fully interchangeable with the original type:</p><br />
<br />
<haskell><br />
type Foo = Int<br />
<br />
main = print ((2 :: Int) + (3 :: Foo))<br />
</haskell><br />
<br />
So it provides convenience and documentation, but no additional type checking.<br />
<br />
<p><hask>data</hask> is used to define new data types, distinct from any existing type.</p><br />
<br />
<p><hask>newtype</hask> can mostly be understood as a restricted form of <hask>data</hask>. You can use <hask>newtype</hask> when you have exactly one constructor with exactly one field. In those cases, <hask>newtype</hask> can give better performance than <hask>data</hask>.</p><br />
<br />
There is, however, a [[Newtype|subtle difference]] between <hask>data</hask> and <hask>newtype</hask> semantics, which is why the <hask>newtype</hask> optimization is not applied automatically.<br />
<br />
= Making it work =<br />
<br />
== How can I find type errors? ==<br />
<br />
There's no silver bullet, but here are a few useful techniques:<br />
<br />
* Comment out type signatures and see what GHC infers, using <tt>:t</tt> in GHCi.<br />
* Add more type signatures, for example inside <hask>let</hask>. This makes your assumptions clearer, so GHC's error message may better explain how your assumptions are inconsistent.<br />
* Replace some subexpressions with <hask>undefined</hask>, which can assume any type. In more recent versions of GHC, you should insert a "type hole", written <hask>_</hask> (a single underscore) instead of <hask>undefined</hask> for this purpose. GHC's error message will then indicate the type it's expecting for the expression in the hole.<br />
<br />
== How can I find bugs that occur at runtime? ==<br />
<br />
With pure functions, correctness is a matter of getting the right output for a given input. If one function gives incorrect results, you test the functions it calls, and so on until the bad code is located. You can perform these tests directly in GHCi, or with the help of a tool like QuickCheck.<br />
<br />
You can trace evaluation using [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Debug-Trace.html Debug.Trace]. You'll get a printout when the expression is evaluated. Due to lazy evaluation, this might be at an unexpected time. But this property is useful when debugging problems related to excessive laziness.<br />
<br />
GHCi also implements a [http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-debugger.html &quot;simple imperative-style debugger&quot;].<br />
<br />
Haskell is a natural fit for novel [http://ww2.cs.mu.oz.au/~bjpop/buddha/ &quot;declarative debugging&quot; tools] but to our knowledge, no such tool is production-ready.<br />
<br />
== Why do I get an &quot;undefined symbol&quot; linker error when compiling? ==<br />
<br />
If you're using GHC 6, you should pass <tt>--make</tt> so that GHC will automatically link the appropriate Haskell libraries.<br />
<br />
== How can I get a stack backtrace when my program throws an exception? ==<br />
<br />
The standard stack in GHC Haskell doesn't represent nested function calls. The more informative stack is the profiling cost-center stack, which only exists if your code is built for profiling.<br />
<br />
With GHC 7 you can do something like this:<br />
<br />
<pre>$ ghc -fforce-recomp -prof -auto-all -rtsopts foo.hs</pre><br />
For GHC 6 you should leave off <tt>-rtsopts</tt>, and you'll probably want <tt>--make</tt>.<br />
<br />
You can then run your program with the [http://www.haskell.org/ghc/docs/latest/html/users_guide/runtime-control.html#rts-options-debugging <tt>-xc</tt> RTS option`]:<br />
<br />
<pre>$ ./foo +RTS -xc</pre><br />
== How can I do automated unit testing? ==<br />
<br />
See the [http://book.realworldhaskell.org/read/testing-and-quality-assurance.html testing chapter] in ''Real World Haskell''.<br />
<br />
== How can I find and fix performance problems? ==<br />
<br />
See the [http://book.realworldhaskell.org/read/profiling-and-optimization.html profiling and optimization chapter] in ''Real World Haskell''.<br />
<br />
= Modules =<br />
<br />
== How do I deal with name clashes between modules? ==<br />
<br />
You can disambiguate by prefixing a module name:<br />
<br />
<haskell><br />
import Data.List<br />
import Data.Map<br />
<br />
f = Data.List.lookup 7<br />
g = Data.Map.lookup 7<br />
</haskell><br />
<br />
The import syntax gives you additional control:<br />
<br />
* With <hask>import qualified Foo</hask> the names from <hask>Foo</hask> can ''only'' be used qualified, and won't clash with unqualified names.<br />
<br />
* With <hask>import Foo as M</hask> you'd write <hask>M.x</hask> instead of <hask>Foo.x</hask>.<br />
<br />
You can combine these two features. A more common way to write the above example is:<br />
<br />
<haskell><br />
import qualified Data.Map as M<br />
<br />
f = lookup 7 -- unqualified, from Prelude<br />
g = M.lookup 7<br />
</haskell><br />
<br />
In general, most combinations of <hask>import</hask> features are allowed. You<br />
can combine <hask>as</hask> and <hask>qualified</hask> with import and<br />
<hask>hiding</hask> lists. You can import two modules <hask>as</hask> the same<br />
name, or one module <hask>as</hask> two names, with different import and<br />
<hask>hiding</hask> lists, <hask>qualified</hask> or unqualified, etc.<br />
<br />
== How do I control the <hask>Prelude</hask> import? ==<br />
<br />
Haskell modules implicitly import <hask>Prelude</hask>, unless an explicit import is given. So you can write<br />
<br />
<haskell><br />
import Prelude as P hiding (length, head)<br />
</haskell><br />
<br />
== How do I qualify the name of an infix operator? ==<br />
<br />
You prefix the module name, as usual:<br />
<br />
<haskell><br />
x = 2 + 3<br />
y = 2 Prelude.+ 3<br />
<br />
f = (+) 7<br />
g = (Prelude.+) 7<br />
</haskell><br />
<br />
This looks weird but works fine. The syntax does clash a bit:<br />
<br />
<haskell><br />
xs = [False..True] -- wrong, parses as qualified name<br />
xs = [False .. True] -- ok<br />
</haskell><br />
<br />
== How do I mention an infix operator in an export / import / <hask>hiding</hask> list? ==<br />
<br />
The same way as elsewhere: enclose it in parentheses.<br />
<br />
<haskell><br />
import Prelude ( succ, (+), length, (*) )<br />
</haskell><br />
<br />
== I listed a data type in my import list but its data constructors aren't in scope. How do I fix it? ==<br />
<br />
You have to import data constructors explicitly:<br />
<br />
<haskell><br />
import Prelude ( Maybe ) -- the type only<br />
import Prelude ( Maybe(Nothing) ) -- type and specific constructor(s)<br />
import Prelude ( Maybe(..) ) -- type and all its constructors<br />
</haskell><br />
<br />
== How can I import and re-export a whole module? ==<br />
<br />
<haskell><br />
module Bar ( module Foo ) where<br />
import Foo<br />
</haskell><br />
<br />
== How can I export another module ''and'' everything defined in this module? ==<br />
<br />
<haskell><br />
module Bar ( module Bar, module Foo ) where<br />
import Foo<br />
</haskell><br />
<br />
= The M-word =<br />
<br />
See also &quot;[[What a Monad is not]]&quot;.<br />
<br />
== I heard Haskell is about monads. I heard that the core feature of Haskell is monads. Is that true? ==<br />
<br />
Absolutely not.<br />
<br />
== Do I need to understand monads in order to do IO? ==<br />
<br />
Not really. &quot;<tt>Monad</tt>&quot; is the name of a generic API that applies to many different types, including the <hask>IO</hask> type. If you're only thinking about IO, you don't need to worry about how this API generalizes.<br />
<br />
See the [[Introduction to IO]].<br />
<br />
== What should I know before trying to understand monads? ==<br />
<br />
Let's look at part of the definition of <hask>Monad</hask>:<br /><br /><br />
<br />
<haskell><br />
class Monad m where<br />
(>>=) :: m a -> (a -> m b) -> m b<br />
</haskell><br />
<br />
This uses a lot of features:<br />
<br />
* first-class functions: the second argument to <hask>(>>=)</hask> is a function<br />
<br />
* type constructors (here <hask>m</hask>)<br />
<br />
* type class polymorphism<br />
<br />
* type class polymorphism ''over'' type constructors (which few other languages have)<br />
<br />
* parametric (not type class) polymorphism, over <hask>a</hask> and <hask>b</hask><br />
<br />
Each of these features is more fundamental than the specific idea of monads. If you understand each feature, then <hask>Monad</hask> is a small example of how they fit together. <hask>Functor</hask> is a slightly simpler example, and you can start there instead (see [[Typeclassopedia]]).<br />
<br />
== Aren't monads just a hack for handling impure things in a pure language? ==<br />
<br />
No. This is wrong in several ways.<br />
<br />
First, '''<hask>Monad</hask> isn't special.''' <hask>Monad</hask> is an ordinary Haskell type class. You can define it yourself in a few lines of pure Haskell code, so it's definitely not adding magical new capabilities to the language. Besides that, <hask>Monad</hask> is an API, not an implementation.<br /><br /><br />
<br />
(The <hask>do</hask> syntax ''is'' special, but it's only [[syntactic sugar]]. There's a straightforward translation to ordinary function calls, lambdas, etc.)<br /><br /><br />
<br />
Second, '''most monads have nothing to do with effects or "impurity".''' The <hask>Monad</hask> API is implemented by many different type constructors, including <hask>Maybe</hask> and <hask>[]</hask>. Lists and <hask>Maybe</hask> values are straightforward, familiar data values. There's nothing "impure" about them.<br /><br /><br />
<br />
Third, '''<hask>IO</hask> is not an exception to purity.''' IO actions are pure, first-class values like any other. You can create, store, and evaluate them without causing side effects. IO actions are just ''descriptions'' of IO which ''could'' be performed.<br /><br />
<br />
In short, Haskell programs are pure-functional programs which compute imperative programs.<br />
<br />
It's true that <hask>IO</hask> implements the <hask>Monad</hask> API, but that's not fundamental. You could instead use non-overloaded functions like<br /><br /><br />
<br />
<haskell><br />
returnIO :: a -> IO a<br />
bindIO :: IO a -> (a -> IO b) -> IO b<br />
</haskell><br />
<br />
<br />to glue together IO actions, and it would all work out basically the same. We use the generic <hask>Monad</hask> API for <hask>IO</hask> not because we have to, but because it lets us reuse convenient syntax and [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html libraries]. Indeed, you can [[Introduction to IO|introduce <tt>IO</tt>]] without mentioning <hask>Monad</hask> at all.<br />
<br />
== I heard monads are like burritos or space suits full of nuclear waste. Is that true? ==<br />
<br />
These analogies are not helpful. See [http://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/ &quot;Abstraction, intuition, and the 'monad tutorial fallacy&quot;'].<br />
<br />
== I can use monads but I feel like I still don't &quot;get&quot; them. What am I missing? ==<br />
<br />
You're not necessarily missing anything. &quot;<tt>Monad</tt>&quot; is just the name of a generic API that applies to many different types. The types implementing the <tt>Monad</tt> API don't have a lot in common.<br />
<br />
You might want to read &quot;[[Typeclassopedia]]&quot; to see how <tt>Monad</tt> fits in with other similar APIs.<br />
<br />
== What's the difference between <hask>State s</hask> and <hask>ST s</hask> monads? ==<br />
<br />
<tt>State s a</tt> is just a wrapper for the function type <hask>s -> (a, s)</hask>: a function that takes an &quot;old state&quot; and returns a &quot;new state&quot; along with its result. You can implement <hask>State</hask> in a few lines of standard Haskell, without any special help from the compiler.<br />
<br />
<hask>ST</hask> gives you true mutable variables with in-place update. You can't implement it yourself in standard Haskell. In GHC, <hask>STRef</hask> and <hask>IORef</hask> will behave the same way at runtime. The difference is the extra compile-time safety checking associated with <hask>runST</hask>.<br />
<br />
= Concurrency and parallelism =<br />
<br />
See also notes on [[parallel]] and [[Concurrency|concurrent]] programming.<br />
<br />
== How can I do X without resorting to threads? ==<br />
<br />
That's usually the wrong attitude. Threads are useful for solving problems. The attitude comes from other languages where some combination of the following holds:<br />
<br />
* Per-thread overhead consumes a lot of memory or CPU<br />
<br />
* Thread APIs are cumbersome, sometimes due to lacking first-class functions or IO actions<br />
<br />
* The thread implementation is fundamentally broken, e.g. a global interpreter lock<br />
<br />
* Threads break easily because programs constantly mutate implicitly-shared state<br />
<br />
None of these is true in GHC Haskell. Threads have disadvantages and are not always the right tool. But avoiding them at all costs is counterproductive.<br />
<br />
== What's the difference between concurrency and parallelism? ==<br />
<br />
Briefly: concurrency describes semantics; parallelism describes an implementation property.<br />
<br />
Concurrent programs are written with explicit threads of control. Concurrent semantics fit naturally with certain real-world problems, like a network server talking to many simultaneous clients. This is still a nice model for writing a network server, even if you only intend to run it on one CPU core — concurrency without parallelism.<br />
<br />
Parallel programs are those which run on multiple CPU cores simultaneously, regardless of how they were implemented.<br />
<br />
Concurrency is a popular way to obtain parallel performance, but converting a pure computation to use concurrent semantics is difficult and error-prone. GHC Haskell provides "[http://hackage.haskell.org/packages/archive/parallel/latest/doc/html/Control-Parallel-Strategies.html semi-implicit parallelism]" as an alternative. Adding these "annotations" to a program cannot change its behavior.<br />
<br />
There's a longer discussion on the [http://ghcmutterings.wordpress.com/2009/10/06/parallelism-concurrency/ GHC blog].<br />
<br />
== How do <hask>par</hask>, <hask>pseq</hask>, and <hask>seq</hask> relate? ==<br />
<br />
The expression <hask>par x y</hask> is semantically equivalent to <hask>y</hask>, but suggests to the runtime system that evaluating <hask>x</hask> in parallel might be a good idea. Usually <hask>x</hask> would be a variable referring to a thunk (unevaluated expression) that will later be needed.<br /><br /><br />
<br />
Now consider <hask>par x (x+y)</hask>. Evaluating this expression suggests evaluating <hask>x</hask> in parallel. But before the runtime system can act on that suggestion, we evaluate <hask>(x+y)</hask>, which will evaluate both <hask>x</hask> and <hask>y</hask> in sequence. It would be better to work on <hask>y</hask> for a while, and only demand <hask>x</hask> later, after perhaps some parallel work has occurred. We can use <hask>pseq</hask> to force this evaluation order, as in <hask>par x (pseq y (x+y))</hask>.<br /><br /><br />
<br />
The [http://hackage.haskell.org/packages/archive/parallel/latest/doc/html/Control-Parallel-Strategies.html Strategies] module provides a nicer interface to these <hask>par</hask> / <hask>pseq</hask> tricks.<br />
<br />
<p><hask>seq</hask> is similar to <hask>pseq</hask> but provides a weaker guarantee. [http://www.mail-archive.com/glasgow-haskell-users@haskell.org/msg10973.html The details] are subtle; suffice to say that if you're controlling evaluation order, you want <hask>pseq</hask>.</p><br />
<br />
== How do I do event-based IO in GHC Haskell? Should I call <tt>select</tt>, <tt>epoll</tt>, etc? ==<br />
<br />
No; just do blocking IO from multiple threads, and GHC's runtime system will make these calls for you. GHC Haskell gives you the performance benefits of event-based IO without making you turn your code inside-out.<br />
<br />
Threads in GHC are lightweight — both in performance and in the mental effort of using them. You can handle [http://blog.johantibell.com/2010/09/final-version-of-our-ghc-io-manager.html ten thousand concurrent requests] at high throughput with a naive "one thread per client" model.<br />
<br />
== What's the difference between <hask>forkIO</hask> and <hask>forkOS</hask>? ==<br />
<br />
It only matters if you're calling into a C library that cares about thread-local state. In that case, <hask>forkOS</hask> guarantees that the C library will see the same OS thread each time. Any difference beyond that is an implementation detail and subject to change.<br />
<br />
== How can I wait for a thread to finish and produce a result? ==<br />
<br />
There's a few libraries for this on Hackage, like [http://hackage.haskell.org/package/async <tt>async</tt>], [http://hackage.haskell.org/package/spawn <tt>spawn</tt>], and [http://hackage.haskell.org/package/threads <tt>threads</tt>].<br />
<br />
It's not hard to implement this yourself using [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent-MVar.html <tt>MVar</tt>], though it's harder to get the exception handling right.<br />
<br />
= IRC =<br />
<br />
== What does lambdabot's "info" command do? ==<br />
<br />
At first, it seems like it just echos whatever you type:<br />
<br />
<user> ?info foo bar baz<br />
<lambdabot> foo bar baz<br />
<br />
What is actually happening is that it autocorrects to the "undo" command, which desugars do blocks according to the Report's rules. So, a more interesting example might look like this:<br />
<br />
<user> ?info do { foo; x <- bar; baz }<br />
<lambdabot> foo >> bar >>= \ x -> baz<br />
<br />
[[Category:FAQ]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=ListT_done_right&diff=58554ListT done right2014-07-09T20:05:17Z<p>Dfeuer: </p>
<hr />
<div>== Introduction ==<br />
<br />
The Haskell hierarchical libraries implement a ListT monad transformer. There are, however, some problems with that implementation.<br />
<br />
* <hask>ListT</hask> imposes unnecessary strictness.<br />
* <hask>ListT</hask> isn't really a monad transformer, ie. <hask>ListT m</hask> isn't always a monad for a monad <hask>m</hask>.<br />
<br />
See the [[#Examples]] below for demonstrations of these problems.<br />
<br />
== Implementation ==<br />
<br />
The following implementation tries to provide a replacement for the ListT transformer using the following technique. Instead of associating a monadic side effect with a list of values (<hask>m [a]</hask>), it lets each element of the list have its own side effects, which only get `excecuted' if this element of the list is really inspected.<br />
<br />
There is also a [[ListT done right alternative]], and the [[Pipes]] package provides its own version as well.<br />
<br />
<haskell><br />
import Data.Maybe<br />
import Control.Monad.State<br />
import Control.Monad.Reader<br />
import Control.Monad.Error<br />
import Control.Monad.Cont<br />
<br />
-- The monadic list type<br />
data MList' m a = MNil | a `MCons` MList m a<br />
type MList m a = m (MList' m a)<br />
<br />
-- This can be directly used as a monad transformer<br />
newtype ListT m a = ListT { runListT :: MList m a }<br />
<br />
-- A "lazy" run function, which only calculates the first solution.<br />
runListT' :: Functor m => ListT m a -> m (Maybe (a, ListT m a))<br />
runListT' (ListT m) = fmap g m where<br />
g MNil = Nothing<br />
g (x `MCons` xs) = Just (x, ListT xs)<br />
<br />
-- In ListT from Control.Monad this one is the data constructor ListT, so sadly, this code can't be a drop-in replacement.<br />
liftList :: Monad m => [a] -> ListT m a<br />
liftList [] = ListT $ return MNil<br />
liftList (x:xs) = ListT . return $ x `MCons` (runListT $ liftList xs)<br />
<br />
instance Functor m => Functor (ListT m) where<br />
fmap f (ListT m) = ListT $ fmap (fmap f) m<br />
<br />
instance Functor m => Functor (MList' m) where <br />
fmap _ MNil = MNil<br />
fmap f (x `MCons` xs) = f x `MCons` fmap (fmap f) xs<br />
<br />
-- Why on earth isn't Monad declared `class Functor m => Monad m'?<br />
-- I assume that a monad is always a functor, so the contexts <br />
-- get a little larger than actually necessary<br />
instance (Functor m, Monad m) => Monad (ListT m) where<br />
return x = ListT . return $ x `MCons` return MNil<br />
m >>= f = joinListT $ fmap f m<br />
<br />
instance MonadTrans ListT where<br />
lift = ListT . liftM (`MCons` return MNil)<br />
<br />
instance (Functor m, Monad m) => MonadPlus (ListT m) where<br />
mzero = liftList []<br />
(ListT xs) `mplus` (ListT ys) = ListT $ xs `mAppend` ys<br />
<br />
-- Implemenation of join<br />
joinListT :: (Functor m, Monad m) => ListT m (ListT m a) -> ListT m a<br />
joinListT (ListT xss) = ListT . joinMList $ fmap (fmap runListT) xss<br />
<br />
joinMList :: (Functor m, Monad m) => MList m (MList m a) -> MList m a<br />
joinMList = (=<<) joinMList'<br />
<br />
joinMList' :: (Functor m, Monad m) => MList' m (MList m a) -> MList m a<br />
joinMList' MNil = return MNil<br />
joinMList' (x `MCons` xs) = x `mAppend` joinMList xs<br />
<br />
mAppend :: (Functor m, Monad m) => MList m a -> MList m a -> MList m a<br />
mAppend xs ys = (`mAppend'` ys) =<< xs<br />
<br />
mAppend' :: (Functor m, Monad m) => MList' m a -> MList m a -> MList m a<br />
mAppend' MNil ys = ys<br />
mAppend' (x `MCons` xs) ys = return $ x `MCons` mAppend xs ys<br />
<br />
-- These things typecheck, but I haven't made sure what they do is sensible.<br />
-- (callCC almost certainly has to be changed in the same way as throwError)<br />
instance (MonadIO m, Functor m) => MonadIO (ListT m) where<br />
liftIO = lift . liftIO<br />
<br />
instance (MonadReader s m, Functor m) => MonadReader s (ListT m) where<br />
ask = lift ask<br />
local f = ListT . local f . runListT<br />
<br />
instance (MonadState s m, Functor m) => MonadState s (ListT m) where<br />
get = lift get<br />
put = lift . put<br />
<br />
instance (MonadCont m, Functor m) => MonadCont (ListT m) where<br />
callCC f = ListT $<br />
callCC $ \c -><br />
runListT . f $ \a -> <br />
ListT . c $ a `MCons` return MNil<br />
<br />
instance (MonadError e m, Functor m) => MonadError e (ListT m) where<br />
throwError = lift . throwError<br />
{- This (perhaps more straightforward) implementation has the disadvantage<br />
that it only catches errors that occur at the first position of the <br />
list.<br />
m `catchError` h = ListT $ runListT m `catchError` \e -> runListT (h e)<br />
-}<br />
-- This is better because errors are caught everywhere in the list.<br />
(m :: ListT m a) `catchError` h = ListT . deepCatch . runListT $ m <br />
where<br />
deepCatch :: MList m a -> MList m a<br />
deepCatch ml = fmap deepCatch' ml `catchError` \e -> runListT (h e)<br />
<br />
deepCatch' :: MList' m a -> MList' m a<br />
deepCatch' MNil = MNil <br />
deepCatch' (x `MCons` xs) = x `MCons` deepCatch xs<br />
</haskell><br />
<br />
== Examples ==<br />
<br />
Here are some examples that show why the old ListT is not right, and how to use the new ListT instead.<br />
<br />
=== Sum of squares ===<br />
<br />
Here's a silly example how to use ListT. It checks if an <hask>Int</hask> <hask>n</hask> is a sum of two squares. Each inspected possibility is printed, and if the number is indeed a sum of squares, another message is printed. Note that with our ListT, runMyTest only evaluates the side effects needed to find the first representation of <hask>n</hask> as a sum of squares, which would be impossible with the ListT implementation of <hask>Control.Monad.List.ListT</hask>.<br />
<br />
<haskell><br />
myTest :: Int -> ListT IO (Int, Int)<br />
myTest n = do<br />
let squares = liftList . takeWhile (<=n) $ map (^(2::Int)) [0..]<br />
x <- squares<br />
y <- squares<br />
lift $ print (x,y)<br />
guard $ x + y == n<br />
lift $ putStrLn "Sum of squares."<br />
return (x,y)<br />
<br />
runMyTest :: Int -> IO (Int, Int) <br />
runMyTest = fmap (fst . fromJust) . runListT' . myTest<br />
</haskell><br />
<br />
A little example session (<hask>runMyTest'</hask> is implemented in exactly the same way as <hask>runMyTest</hask>, but uses <hask>Control.Monad.List.ListT</hask>):<br />
<br />
*Main> runMyTest 5<br />
(0,0)<br />
(0,1)<br />
(0,4)<br />
(1,0)<br />
(1,1)<br />
(1,4)<br />
Sum of squares.<br />
*Main> runMyTest' 5<br />
(0,0)<br />
(0,1)<br />
(0,4)<br />
(1,0)<br />
(1,1)<br />
(1,4)<br />
Sum of squares.<br />
(4,0)<br />
(4,1)<br />
Sum of squares.<br />
(4,4)<br />
<br />
=== Grouping effects ===<br />
<br />
I didn't understand the statement "<hask>ListT m</hask> isn't always a monad", even after I understood why it is too strict. I found the answer in [http://web.cecs.pdx.edu/~mpj/pubs/composing.html Composing Monads]. It's in fact a direct consequence of the unnecessary strictness. <hask>ListT m</hask> is not associative (which is one of the monad laws), because grouping affects when side effects are run (which may in turn affect the answers). Consider<br />
<br />
<haskell><br />
import Control.Monad.List<br />
import Data.IORef<br />
<br />
test1 :: ListT IO Int<br />
test1 = do<br />
r <- liftIO (newIORef 0)<br />
(next r `mplus` next r >> next r `mplus` next r) >> next r `mplus` next r<br />
<br />
test2 :: ListT IO Int<br />
test2 = do<br />
r <- liftIO (newIORef 0)<br />
next r `mplus` next r >> (next r `mplus` next r >> next r `mplus` next r)<br />
<br />
next :: IORef Int -> ListT IO Int<br />
next r = liftIO $ do x <- readIORef r<br />
writeIORef r (x+1)<br />
return x<br />
</haskell><br />
<br />
Under Control.Monad.List.ListT, test1 returns the answers<br />
<hask>[6,7,8,9,10,11,12,13]</hask> while test2 returns the answers<br />
<hask>[4,5,6,7,10,11,12,13]</hask>. Under the above ListT (if all answers are forced), both return<br />
<hask>[2,3,5,6,9,10,12,13]</hask>.<br />
<br />
[[Andrew Pimlott]]<br />
<br />
=== Order of printing ===<br />
<br />
Here is another (simpler?) example showing why "<hask>ListT m</hask> isn't always a monad".<br />
<br />
<haskell><br />
a,b,c :: ListT IO ()<br />
[a,b,c] = map (liftIO . putChar) ['a','b','c']<br />
<br />
t1 :: ListT IO ()<br />
t1 = ((a `mplus` a) >> b) >> c<br />
<br />
t2 :: ListT IO ()<br />
t2 = (a `mplus` a) >> (b >> c)<br />
</haskell><br />
<br />
Under <hask>Control.Monad.List.ListT</hask>, running <hask>runListT t1</hask> prints "aabbcc", while <hask>runListT t2</hask> instead prints "aabcbc". Under the above ListT, they both print "abc" (if all answers were forced, they would print "abcabc").<br />
<br />
[[Roberto Zunino]]<br />
<br />
=== Order of <hask>ListT []</hask> ===<br />
<br />
This is a simple example that doesn't use <hask>IO</hask>, only pure <hask>ListT []</hask>.<br />
<haskell><br />
v :: Int -> ListT [] Int<br />
v 0 = ListT [[0, 1]]<br />
v 1 = ListT [[0], [1]]<br />
<br />
main = do<br />
print $ runListT $ ((v >=> v) >=> v) 0<br />
-- = [[0,1,0,0,1],[0,1,1,0,1],[0,1,0,0],[0,1,0,1],[0,1,1,0],[0,1,1,1]]<br />
print $ runListT $ (v >=> (v >=> v)) 0<br />
-- = [[0,1,0,0,1],[0,1,0,0],[0,1,0,1],[0,1,1,0,1],[0,1,1,0],[0,1,1,1]]<br />
</haskell><br />
<br />
Clearly, <hask>ListT []</hask> fails to preserve the associativity monad law.<br />
<br />
This example violates the requirement given in [http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-List.html the documentation] that the inner monad has to be commutative. However, all the preceding examples use <hask>IO</hask> which is neither commutative, so I suppose this example is valid at the end. Most likely, a proper implementation of <hask>ListT</hask> should not have such a requirement.<br />
<br />
--[[User:Petr Pudlak|PetrP]] 19:15, 27 September 2012 (UTC)<br />
<br />
== Relation to Nondet ==<br />
<br />
NonDeterminism describes another monad transformer that can also be used to model nondeterminism. In fact, <hask>ListT</hask> and <hask>NondetT</hask> are quite similar with the following two functions translating between them<br />
<br />
<haskell><br />
toListT :: (Monad m) => NondetT m a -> ListT m a<br />
toListT (NondetT fold) = ListT $ fold ((return.) . MCons) (return MNil)<br />
<br />
toNondetT :: (Monad m) => ListT m a -> NondetT m a<br />
toNondetT (ListT ml) = NondetT (\c n -> fold c n ml) where<br />
fold :: Monad m => (a -> m b -> m b) -> m b -> MList m a -> m b<br />
fold c n xs = fold' c n =<< xs<br />
<br />
fold' :: Monad m => (a -> m b -> m b) -> m b -> MList' m a -> m b<br />
fold' _ n MNil = n<br />
fold' c n (x `MCons` xs) = c x (fold c n xs)<br />
</haskell><br />
<br />
<hask>ListT</hask> is smaller than <hask>NondetT</hask> in the sense that <hask>toListT . toNondetT</hask> is the identity (is it ok to call <hask>ListT</hask> `retract'?). However, these functions don't define an isomorphism (check for example <hask>NondetT (\_ n -> liftM2 const n n)</hask>).<br />
<br />
[[Thomas Jaeger]]<br />
<br />
''I propose to replace every occurence of `fmap` in the above code with `liftM`, thereby moving `class Functor` and the complaint about it not being a superclass of `Monad` completely out of the picture. I'd simply do it, if there wasn't this feeling that I have overlooked something obvious. What is it? -- [[Udo Stenzel]]''<br />
<br />
There's no particular reason why I used fmap, except that the page has the (unfortunate!) title "ListT Done Right", and having Functor superclass of Monad certainly is the right thing. But I agree, that mistake has long been done and I feel my half-hearted cure is worse than the disease. You can find an alternative, more concise definition of a ListT transformer based on even-style lists here: [[ListT done right alternative]]<br />
<br />
[[amb]] has AmbT, which could be considered as 'ListT done right' (since Amb is identical to the list monad).<br />
<br />
[[Category:Monad]]<br />
[[Category:Proposals]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=GADTs_for_dummies&diff=58393GADTs for dummies2014-06-29T20:50:28Z<p>Dfeuer: /* Further reading */ Fixed broken link; make links links</p>
<hr />
<div>[[Category:Tutorials]]<br />
[[Category:Language extensions]]<br />
<br />
For a long time, I didn't understand what GADTs were or how they could be used. It was sort of a conspiracy of silence &mdash; people who understood GADTs thought<br />
that everything was obvious and didn't need further explanation, but I still<br />
couldn't understand them.<br />
<br />
Now that I have an idea of how it works, I think that it was really obvious. :) So, I want to share my understanding of GADTs. Maybe the way I realized how GADTs work could help<br />
someone else. See also [[Generalised algebraic datatype]]<br />
<br />
== Type functions ==<br />
<br />
A "data" declaration is a way to declare both type constructor and data<br />
constructors. For example,<br />
<br />
<haskell><br />
data Either a b = Left a | Right b<br />
</haskell><br />
<br />
declares type constructor "Either" and two data constructors "Left"<br />
and "Right". Ordinary Haskell functions works with data constructors:<br />
<br />
<haskell><br />
isLeft (Left a) = True<br />
isLeft (Right b) = False<br />
</haskell><br />
<br />
but there is also an analogous way to work with type constructors!<br />
<br />
<haskell><br />
type X a = Either a a<br />
</haskell><br />
<br />
declares a TYPE FUNCTION named "X". Its parameter "a" must be some type<br />
and it returns some type as its result. We can't use "X" on data values,<br />
but we can use it on type values. Type constructors declared with<br />
"data" statements and type functions declared with "type" statements<br />
can be used together to build arbitrarily complex types. In such<br />
"computations" type constructors serves as basic "values" and type<br />
functions as a way to process them.<br />
<br />
Indeed, type functions in Haskell are very limited compared to<br />
ordinary functions - they don't support pattern matching,<br />
nor multiple statements, nor recursion.<br />
<br />
== Hypothetical Haskell extension - Full-featured type functions ==<br />
<br />
Let's build a hypothetical Haskell extension that mimics, for type<br />
functions, the well-known ways to define ordinary functions, including<br />
pattern matching:<br />
<br />
<haskell><br />
type F [a] = Set a<br />
</haskell><br />
<br />
multiple statements (this is meaningful only in the presence of pattern matching):<br />
<br />
<haskell><br />
type F Bool = Char<br />
F String = Int<br />
</haskell><br />
<br />
and recursion (which again needs pattern matching and multiple statements):<br />
<br />
<haskell><br />
type F [a] = F a<br />
F (Map a b) = F b<br />
F (Set a) = F a<br />
F a = a<br />
</haskell><br />
<br />
As you may already have guessed, this last definition calculates a simple base type of arbitrarily-nested collections, e.g.: <br />
<br />
<haskell><br />
F [[[Set Int]]] = <br />
F [[Set Int]] =<br />
F [Set Int] = <br />
F (Set Int) =<br />
F Int =<br />
Int<br />
</haskell><br />
<br />
Let's not forget about statement guards:<br />
<br />
<haskell><br />
type F a | IsSimple a == TrueType = a<br />
</haskell><br />
<br />
Here we define type function F only for simple datatypes by using a<br />
guard type function "IsSimple":<br />
<br />
<haskell><br />
type IsSimple Bool = TrueType<br />
IsSimple Int = TrueType<br />
....<br />
IsSimple Double = TrueType<br />
IsSimple a = FalseType<br />
<br />
data TrueType = T<br />
data FalseType = F<br />
</haskell><br />
<br />
These definitions seem a bit odd, and while we are in imaginary land,<br />
let's consider a way to write this shorter:<br />
<br />
<haskell><br />
type F a | IsSimple a = a<br />
<br />
type IsSimple Bool<br />
IsSimple Int<br />
....<br />
IsSimple Double<br />
</haskell><br />
<br />
Here, we defined a list of simple types. The implied result of all<br />
written statements for "IsSimple" is True, and False for<br />
everything else. Essentially, "IsSimple" is a TYPE PREDICATE!<br />
<br />
I really love it! :) How about constructing a predicate that traverses a<br />
complex type trying to decide whether it contains "Int" anywhere?<br />
<br />
<haskell><br />
type HasInt Int<br />
HasInt [a] = HasInt a<br />
HasInt (Set a) = HasInt a<br />
HasInt (Map a b) | HasInt a<br />
HasInt (Map a b) | HasInt b<br />
</haskell><br />
<br />
or a type function that substitutes one type with another inside<br />
arbitrarily-deep types:<br />
<br />
<haskell><br />
type Replace t a b | t==a = b<br />
Replace [t] a b = [Replace t a b]<br />
Replace (Set t) a b = Set (Replace t a b)<br />
Replace (Map t1 t2) a b = Map (Replace t1 a b) (Replace t2 a b)<br />
Replace t a b = t<br />
</haskell><br />
<br />
== One more hypothetical extension - multi-value type functions ==<br />
<br />
Let's add more fun! We will introduce one more hypothetical Haskell<br />
extension - type functions that may have MULTIPLE VALUES. Say,<br />
<br />
<haskell><br />
type Collection a = [a]<br />
Collection a = Set a<br />
Collection a = Map b a<br />
</haskell><br />
<br />
So, "Collection Int" has "[Int]", "Set Int" and "Map String Int" as<br />
its values, i.e. different collection types with elements of type<br />
"Int".<br />
<br />
Pay attention to the last statement of the "Collection" definition, where<br />
we used the type variable "b" that was not mentioned on the left side,<br />
nor defined in any other way. Since it's perfectly possible for the<br />
"Collection" function to have multiple values, using some free variable on<br />
the right side that can be replaced with any type is not a problem<br />
at all. "Map Bool Int", "Map [Int] Int" and "Map Int Int" all are<br />
possible values of "Collection Int" along with "[Int]" and "Set Int".<br />
<br />
At first glance, it seems that multiple-value functions are meaningless - they<br />
can't be used to define datatypes, because we need concrete types here. But<br />
if we take another look, they can be useful to define type constraints and<br />
type families.<br />
<br />
We can also represent a multiple-value function as a predicate:<br />
<br />
<haskell><br />
type Collection a [a]<br />
Collection a (Set a)<br />
Collection a (Map b a)<br />
</haskell><br />
<br />
If you're familiar with Prolog, you should know that a predicate, in contrast to<br />
a function, is a multi-directional thing - it can be used to deduce any<br />
parameter from the other ones. For example, in this hypothetical definition:<br />
<br />
<haskell><br />
head | Collection Int a :: a -> Int<br />
</haskell><br />
<br />
we define a 'head' function for any Collection containing Ints.<br />
<br />
And in this, again, hypothetical definition:<br />
<br />
<haskell><br />
data Safe c | Collection c a = Safe c a<br />
</haskell><br />
<br />
we deduced element type 'a' from collection type 'c' passed as the<br />
parameter to the type constructor.<br />
<br />
<br />
<br />
== Back to real Haskell - type classes ==<br />
<br />
After reading about all of these glorious examples, you may be wondering<br />
"Why doesn't Haskell support full-featured type functions?" Hold your breath...<br />
Haskell already contains them, and GHC has implemented all of the<br />
capabilities mentioned above for more than 10 years! They were just named...<br />
TYPE CLASSES! Let's translate all of our examples to their language:<br />
<br />
<haskell><br />
class IsSimple a<br />
instance IsSimple Bool<br />
instance IsSimple Int<br />
....<br />
instance IsSimple Double<br />
</haskell><br />
<br />
The Haskell'98 standard supports type classes with only one parameter.<br />
That limits us to only defining type predicates like this one. But GHC and<br />
Hugs support multi-parameter type classes that allow us to define<br />
arbitrarily-complex type functions<br />
<br />
<haskell><br />
class Collection a c<br />
instance Collection a [a]<br />
instance Collection a (Set a)<br />
instance Collection a (Map b a)<br />
</haskell><br />
<br />
All of the "hypothetical" Haskell extensions we investigated earlier are<br />
actually implemented at the type class level!<br />
<br />
Pattern matching:<br />
<br />
<haskell><br />
instance Collection a [a]<br />
</haskell><br />
<br />
Multiple statements:<br />
<br />
<haskell><br />
instance Collection a [a]<br />
instance Collection a (Set a)<br />
</haskell><br />
<br />
Recursion:<br />
<br />
<haskell><br />
instance (Collection a c) => Collection a [c]<br />
</haskell><br />
<br />
Pattern guards:<br />
<br />
<haskell><br />
instance (IsSimple a) => Collection a (UArray a)<br />
</haskell><br />
<br />
<br />
<br />
Let's define a type class which contains any collection which uses Int in<br />
its elements or indexes:<br />
<br />
<haskell><br />
class HasInt a<br />
instance HasInt Int<br />
instance (HasInt a) => HasInt [a]<br />
instance (HasInt a) => HasInt (Map a b)<br />
instance (HasInt b) => HasInt (Map a b)<br />
</haskell><br />
<br />
<br />
Another example is a class that replaces all occurrences of 'a' with<br />
'b' in type 't' and return the result as 'res':<br />
<br />
<haskell><br />
class Replace t a b res<br />
instance Replace t a a t<br />
instance (Replace t a b res)<br />
=> Replace [t] a b [res]<br />
instance (Replace t a b res)<br />
=> Replace (Set t) a b (Set res)<br />
instance (Replace t1 a b res1, Replace t2 a b res2)<br />
=> Replace (Map t1 t2) a b (Map res1 res2)<br />
instance Replace t a b t<br />
</haskell><br />
<br />
You can compare it to the hypothetical definition we gave earlier.<br />
It's important to note that type class instances, as opposed to<br />
function statements, are not checked in order. Instead, the most<br />
_specific_ instance is automatically selected. So, in the Replace case, the<br />
last instance, which is the most general instance, will be selected only if all the others<br />
fail to match, which is what we want.<br />
<br />
In many other cases this automatic selection is not powerful enough<br />
and we are forced to use some artificial tricks or complain to the<br />
language developers. The two most well-known language extensions<br />
proposed to solve such problems are instance priorities, which allow<br />
us to explicitly specify instance selection order, and '/=' constraints,<br />
which can be used to explicitly prohibit unwanted matches:<br />
<br />
<haskell><br />
instance Replace t a a t<br />
instance (a/=b) => Replace [t] a b [Replace t a b]<br />
instance (a/=b, t/=[_]) => Replace t a b t<br />
</haskell><br />
<br />
You can check that these instances no longer overlap.<br />
<br />
<br />
In practice, type-level arithmetic by itself is not very useful. It becomes a<br />
fantastic tool when combined with another feature that type classes provide -<br />
member functions. For example:<br />
<br />
<haskell><br />
class Collection a c where<br />
foldr1 :: (a -> a -> a) -> c -> a<br />
<br />
class Num a where<br />
(+) :: a -> a -> a<br />
<br />
sum :: (Num a, Collection a c) => c -> a<br />
sum = foldr1 (+)<br />
</haskell><br />
<br />
<br />
I'll also be glad to see the possibility of using type classes in data<br />
declarations, like this:<br />
<br />
<haskell><br />
data Safe c = (Collection c a) => Safe c a<br />
</haskell><br />
<br />
but as far as I know, this is not yet implemented.<br />
<br />
<br />
UNIFICATION<br />
...<br />
<br />
== Back to GADTs ==<br />
<br />
If you are wondering how all of these interesting type manipulations relate to<br />
GADTs, here is the answer. As you know, Haskell contains highly<br />
developed ways to express data-to-data functions. We also know that<br />
Haskell contains rich facilities to write type-to-type functions in the form of<br />
"type" statements and type classes. But how do "data" statements fit into this<br />
infrastructure?<br />
<br />
My answer: they just define a type-to-data constructor translation. Moreover,<br />
this translation may give multiple results. Say, the following definition:<br />
<br />
<haskell><br />
data Maybe a = Just a | Nothing<br />
</haskell><br />
<br />
defines type-to-data constructors function "Maybe" that has a parameter<br />
"a" and for each "a" has two possible results - "Just a" and<br />
"Nothing". We can rewrite it in the same hypothetical syntax that was<br />
used above for multi-value type functions:<br />
<br />
<haskell><br />
data Maybe a = Just a<br />
Maybe a = Nothing<br />
</haskell><br />
<br />
Or how about this:<br />
<br />
<haskell><br />
data List a = Cons a (List a)<br />
List a = Nil<br />
</haskell><br />
<br />
and this:<br />
<br />
<haskell><br />
data Either a b = Left a<br />
Either a b = Right b<br />
</haskell><br />
<br />
But how flexible are "data" definitions? As you should remember, "type"<br />
definitions were very limited in their features, while type classes,<br />
on the other hand, were more developed than ordinary Haskell functions<br />
facilities. What about features of "data" definitions examined as sort of functions?<br />
<br />
On the one hand, they supports multiple statements and multiple results and<br />
can be recursive, like the "List" definition above. On the other, that's all -<br />
no pattern matching or even type constants on the left side and no guards.<br />
<br />
Lack of pattern matching means that the left side can contain only free type<br />
variables. That in turn means that the left sides of all "data" statements for a<br />
type will be essentially the same. Therefore, repeated left sides in<br />
multi-statement "data" definitions are omitted and instead of<br />
<br />
<haskell><br />
data Either a b = Left a<br />
Either a b = Right b<br />
</haskell><br />
<br />
we write just<br />
<br />
<haskell><br />
data Either a b = Left a<br />
| Right b<br />
</haskell><br />
<br />
<br />
And here we finally come to GADTs! It's just a way to define data types using<br />
pattern matching and constants on the left side of "data" statements!<br />
Let's say we want to do this:<br />
<br />
<haskell><br />
data T String = D1 Int<br />
T Bool = D2<br />
T [a] = D3 (a,a)<br />
</haskell><br />
<br />
<br />
We cannot do this using a standard data definition. So, now we must use a GADT definition:<br />
<br />
<haskell><br />
data T a where<br />
D1 :: Int -> T String<br />
D2 :: T Bool<br />
D3 :: (a,a) -> T [a]<br />
</haskell><br />
<br />
Amazed? After all, GADTs seem to be a really simple and obvious extension to<br />
data type definition facilities.<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
The idea here is to allow a data constructor's return type to be specified<br />
directly:<br />
<br />
<haskell><br />
data Term a where<br />
Lit :: Int -> Term Int<br />
Pair :: Term a -> Term b -> Term (a,b)<br />
...<br />
</haskell><br />
<br />
In a function that performs pattern matching on Term, the pattern match gives<br />
type as well as value information. For example, consider this function:<br />
<br />
<haskell><br />
eval :: Term a -> a<br />
eval (Lit i) = i<br />
eval (Pair a b) = (eval a, eval b)<br />
...<br />
</haskell><br />
<br />
If the argument matches Lit, it must have been built with a Lit constructor,<br />
so type 'a' must be Int, and hence we can return 'i' (an Int) in the right<br />
hand side. The same thing applies to the Pair constructor.<br />
<br />
<br />
<br />
<br />
<br />
<br />
== Further reading ==<br />
<br />
The best paper on type level arithmetic using type classes I've seen<br />
is "Faking it: simulating dependent types in Haskell"<br />
( http://www.cs.nott.ac.uk/~ctm/faking.ps.gz ). Most of<br />
this article comes from his work.<br />
<br />
A great demonstration of type-level arithmetic is in the TypeNats package,<br />
which "defines type-level natural numbers and arithmetic operations on<br />
them including addition, subtraction, multiplication, division and GCD"<br />
( darcs get --partial --tag '0.1' http://www.eecs.tufts.edu/~rdocki01/typenats/ )<br />
<br />
I should also mention here [http://okmij.org/ftp/Haskell/types.html Oleg Kiselyov's page on type-level programming in Haskell].<br />
<br />
<br />
There are plenty of GADT-related papers, but the best one for beginners<br />
is [http://www.cs.ox.ac.uk/ralf.hinze/publications/With.pdf "Fun with phantom types"].<br />
Phantom types is another name of GADT. You should also know that this<br />
paper uses old GADT syntax. This paper is a must-read because it<br />
contains numerous examples of practical GADT usage - a theme completely<br />
omitted from my article.<br />
<br />
<br />
Other GADT-related papers:<br />
<br />
* [http://www.cs.nott.ac.uk/~nhn/Publications/icfp2005.pdf "Dynamic Optimization for Functional Reactive Programming using Generalized Algebraic Data Types"]<br />
<br />
* [http://citeseer.ist.psu.edu/rd/0,606209,1,0.25,Download/http:qSqqSqwww.informatik.uni-bonn.deqSq~ralfqSqpublicationsqSqPhantom.ps.gz "Phantom Types"] (actually a more scientific version of "Fun with phantom types")<br />
<br />
* [http://arxiv.org/ps/cs.PL/0403034 "Phantom types and subtyping"]<br />
<br />
* "Existentially quantified type classes" by Stuckey, Sulzmann and Wazny (URL?)</div>Dfeuerhttps://wiki.haskell.org/index.php?title=GADTs_for_dummies&diff=58389GADTs for dummies2014-06-29T20:13:36Z<p>Dfeuer: /* Back to real Haskell - type classes */ Fixed second Replace instance to match the rest—in its original form it made no sense.</p>
<hr />
<div>[[Category:Tutorials]]<br />
[[Category:Language extensions]]<br />
<br />
For a long time, I didn't understand what GADTs were or how they could be used. It was sort of a conspiracy of silence &mdash; people who understood GADTs thought<br />
that everything was obvious and didn't need further explanation, but I still<br />
couldn't understand them.<br />
<br />
Now that I have an idea of how it works, I think that it was really obvious. :) So, I want to share my understanding of GADTs. Maybe the way I realized how GADTs work could help<br />
someone else. See also [[Generalised algebraic datatype]]<br />
<br />
== Type functions ==<br />
<br />
A "data" declaration is a way to declare both type constructor and data<br />
constructors. For example,<br />
<br />
<haskell><br />
data Either a b = Left a | Right b<br />
</haskell><br />
<br />
declares type constructor "Either" and two data constructors "Left"<br />
and "Right". Ordinary Haskell functions works with data constructors:<br />
<br />
<haskell><br />
isLeft (Left a) = True<br />
isLeft (Right b) = False<br />
</haskell><br />
<br />
but there is also an analogous way to work with type constructors!<br />
<br />
<haskell><br />
type X a = Either a a<br />
</haskell><br />
<br />
declares a TYPE FUNCTION named "X". Its parameter "a" must be some type<br />
and it returns some type as its result. We can't use "X" on data values,<br />
but we can use it on type values. Type constructors declared with<br />
"data" statements and type functions declared with "type" statements<br />
can be used together to build arbitrarily complex types. In such<br />
"computations" type constructors serves as basic "values" and type<br />
functions as a way to process them.<br />
<br />
Indeed, type functions in Haskell are very limited compared to<br />
ordinary functions - they don't support pattern matching,<br />
nor multiple statements, nor recursion.<br />
<br />
== Hypothetical Haskell extension - Full-featured type functions ==<br />
<br />
Let's build a hypothetical Haskell extension that mimics, for type<br />
functions, the well-known ways to define ordinary functions, including<br />
pattern matching:<br />
<br />
<haskell><br />
type F [a] = Set a<br />
</haskell><br />
<br />
multiple statements (this is meaningful only in the presence of pattern matching):<br />
<br />
<haskell><br />
type F Bool = Char<br />
F String = Int<br />
</haskell><br />
<br />
and recursion (which again needs pattern matching and multiple statements):<br />
<br />
<haskell><br />
type F [a] = F a<br />
F (Map a b) = F b<br />
F (Set a) = F a<br />
F a = a<br />
</haskell><br />
<br />
As you may already have guessed, this last definition calculates a simple base type of arbitrarily-nested collections, e.g.: <br />
<br />
<haskell><br />
F [[[Set Int]]] = <br />
F [[Set Int]] =<br />
F [Set Int] = <br />
F (Set Int) =<br />
F Int =<br />
Int<br />
</haskell><br />
<br />
Let's not forget about statement guards:<br />
<br />
<haskell><br />
type F a | IsSimple a == TrueType = a<br />
</haskell><br />
<br />
Here we define type function F only for simple datatypes by using a<br />
guard type function "IsSimple":<br />
<br />
<haskell><br />
type IsSimple Bool = TrueType<br />
IsSimple Int = TrueType<br />
....<br />
IsSimple Double = TrueType<br />
IsSimple a = FalseType<br />
<br />
data TrueType = T<br />
data FalseType = F<br />
</haskell><br />
<br />
These definitions seem a bit odd, and while we are in imaginary land,<br />
let's consider a way to write this shorter:<br />
<br />
<haskell><br />
type F a | IsSimple a = a<br />
<br />
type IsSimple Bool<br />
IsSimple Int<br />
....<br />
IsSimple Double<br />
</haskell><br />
<br />
Here, we defined a list of simple types. The implied result of all<br />
written statements for "IsSimple" is True, and False for<br />
everything else. Essentially, "IsSimple" is a TYPE PREDICATE!<br />
<br />
I really love it! :) How about constructing a predicate that traverses a<br />
complex type trying to decide whether it contains "Int" anywhere?<br />
<br />
<haskell><br />
type HasInt Int<br />
HasInt [a] = HasInt a<br />
HasInt (Set a) = HasInt a<br />
HasInt (Map a b) | HasInt a<br />
HasInt (Map a b) | HasInt b<br />
</haskell><br />
<br />
or a type function that substitutes one type with another inside<br />
arbitrarily-deep types:<br />
<br />
<haskell><br />
type Replace t a b | t==a = b<br />
Replace [t] a b = [Replace t a b]<br />
Replace (Set t) a b = Set (Replace t a b)<br />
Replace (Map t1 t2) a b = Map (Replace t1 a b) (Replace t2 a b)<br />
Replace t a b = t<br />
</haskell><br />
<br />
== One more hypothetical extension - multi-value type functions ==<br />
<br />
Let's add more fun! We will introduce one more hypothetical Haskell<br />
extension - type functions that may have MULTIPLE VALUES. Say,<br />
<br />
<haskell><br />
type Collection a = [a]<br />
Collection a = Set a<br />
Collection a = Map b a<br />
</haskell><br />
<br />
So, "Collection Int" has "[Int]", "Set Int" and "Map String Int" as<br />
its values, i.e. different collection types with elements of type<br />
"Int".<br />
<br />
Pay attention to the last statement of the "Collection" definition, where<br />
we used the type variable "b" that was not mentioned on the left side,<br />
nor defined in any other way. Since it's perfectly possible for the<br />
"Collection" function to have multiple values, using some free variable on<br />
the right side that can be replaced with any type is not a problem<br />
at all. "Map Bool Int", "Map [Int] Int" and "Map Int Int" all are<br />
possible values of "Collection Int" along with "[Int]" and "Set Int".<br />
<br />
At first glance, it seems that multiple-value functions are meaningless - they<br />
can't be used to define datatypes, because we need concrete types here. But<br />
if we take another look, they can be useful to define type constraints and<br />
type families.<br />
<br />
We can also represent a multiple-value function as a predicate:<br />
<br />
<haskell><br />
type Collection a [a]<br />
Collection a (Set a)<br />
Collection a (Map b a)<br />
</haskell><br />
<br />
If you're familiar with Prolog, you should know that a predicate, in contrast to<br />
a function, is a multi-directional thing - it can be used to deduce any<br />
parameter from the other ones. For example, in this hypothetical definition:<br />
<br />
<haskell><br />
head | Collection Int a :: a -> Int<br />
</haskell><br />
<br />
we define a 'head' function for any Collection containing Ints.<br />
<br />
And in this, again, hypothetical definition:<br />
<br />
<haskell><br />
data Safe c | Collection c a = Safe c a<br />
</haskell><br />
<br />
we deduced element type 'a' from collection type 'c' passed as the<br />
parameter to the type constructor.<br />
<br />
<br />
<br />
== Back to real Haskell - type classes ==<br />
<br />
After reading about all of these glorious examples, you may be wondering<br />
"Why doesn't Haskell support full-featured type functions?" Hold your breath...<br />
Haskell already contains them, and GHC has implemented all of the<br />
capabilities mentioned above for more than 10 years! They were just named...<br />
TYPE CLASSES! Let's translate all of our examples to their language:<br />
<br />
<haskell><br />
class IsSimple a<br />
instance IsSimple Bool<br />
instance IsSimple Int<br />
....<br />
instance IsSimple Double<br />
</haskell><br />
<br />
The Haskell'98 standard supports type classes with only one parameter.<br />
That limits us to only defining type predicates like this one. But GHC and<br />
Hugs support multi-parameter type classes that allow us to define<br />
arbitrarily-complex type functions<br />
<br />
<haskell><br />
class Collection a c<br />
instance Collection a [a]<br />
instance Collection a (Set a)<br />
instance Collection a (Map b a)<br />
</haskell><br />
<br />
All of the "hypothetical" Haskell extensions we investigated earlier are<br />
actually implemented at the type class level!<br />
<br />
Pattern matching:<br />
<br />
<haskell><br />
instance Collection a [a]<br />
</haskell><br />
<br />
Multiple statements:<br />
<br />
<haskell><br />
instance Collection a [a]<br />
instance Collection a (Set a)<br />
</haskell><br />
<br />
Recursion:<br />
<br />
<haskell><br />
instance (Collection a c) => Collection a [c]<br />
</haskell><br />
<br />
Pattern guards:<br />
<br />
<haskell><br />
instance (IsSimple a) => Collection a (UArray a)<br />
</haskell><br />
<br />
<br />
<br />
Let's define a type class which contains any collection which uses Int in<br />
its elements or indexes:<br />
<br />
<haskell><br />
class HasInt a<br />
instance HasInt Int<br />
instance (HasInt a) => HasInt [a]<br />
instance (HasInt a) => HasInt (Map a b)<br />
instance (HasInt b) => HasInt (Map a b)<br />
</haskell><br />
<br />
<br />
Another example is a class that replaces all occurrences of 'a' with<br />
'b' in type 't' and return the result as 'res':<br />
<br />
<haskell><br />
class Replace t a b res<br />
instance Replace t a a t<br />
instance (Replace t a b res)<br />
=> Replace [t] a b [res]<br />
instance (Replace t a b res)<br />
=> Replace (Set t) a b (Set res)<br />
instance (Replace t1 a b res1, Replace t2 a b res2)<br />
=> Replace (Map t1 t2) a b (Map res1 res2)<br />
instance Replace t a b t<br />
</haskell><br />
<br />
You can compare it to the hypothetical definition we gave earlier.<br />
It's important to note that type class instances, as opposed to<br />
function statements, are not checked in order. Instead, the most<br />
_specific_ instance is automatically selected. So, in the Replace case, the<br />
last instance, which is the most general instance, will be selected only if all the others<br />
fail to match, which is what we want.<br />
<br />
In many other cases this automatic selection is not powerful enough<br />
and we are forced to use some artificial tricks or complain to the<br />
language developers. The two most well-known language extensions<br />
proposed to solve such problems are instance priorities, which allow<br />
us to explicitly specify instance selection order, and '/=' constraints,<br />
which can be used to explicitly prohibit unwanted matches:<br />
<br />
<haskell><br />
instance Replace t a a t<br />
instance (a/=b) => Replace [t] a b [Replace t a b]<br />
instance (a/=b, t/=[_]) => Replace t a b t<br />
</haskell><br />
<br />
You can check that these instances no longer overlap.<br />
<br />
<br />
In practice, type-level arithmetic by itself is not very useful. It becomes a<br />
fantastic tool when combined with another feature that type classes provide -<br />
member functions. For example:<br />
<br />
<haskell><br />
class Collection a c where<br />
foldr1 :: (a -> a -> a) -> c -> a<br />
<br />
class Num a where<br />
(+) :: a -> a -> a<br />
<br />
sum :: (Num a, Collection a c) => c -> a<br />
sum = foldr1 (+)<br />
</haskell><br />
<br />
<br />
I'll also be glad to see the possibility of using type classes in data<br />
declarations, like this:<br />
<br />
<haskell><br />
data Safe c = (Collection c a) => Safe c a<br />
</haskell><br />
<br />
but as far as I know, this is not yet implemented.<br />
<br />
<br />
UNIFICATION<br />
...<br />
<br />
== Back to GADTs ==<br />
<br />
If you are wondering how all of these interesting type manipulations relate to<br />
GADTs, here is the answer. As you know, Haskell contains highly<br />
developed ways to express data-to-data functions. We also know that<br />
Haskell contains rich facilities to write type-to-type functions in the form of<br />
"type" statements and type classes. But how do "data" statements fit into this<br />
infrastructure?<br />
<br />
My answer: they just define a type-to-data constructor translation. Moreover,<br />
this translation may give multiple results. Say, the following definition:<br />
<br />
<haskell><br />
data Maybe a = Just a | Nothing<br />
</haskell><br />
<br />
defines type-to-data constructors function "Maybe" that has a parameter<br />
"a" and for each "a" has two possible results - "Just a" and<br />
"Nothing". We can rewrite it in the same hypothetical syntax that was<br />
used above for multi-value type functions:<br />
<br />
<haskell><br />
data Maybe a = Just a<br />
Maybe a = Nothing<br />
</haskell><br />
<br />
Or how about this:<br />
<br />
<haskell><br />
data List a = Cons a (List a)<br />
List a = Nil<br />
</haskell><br />
<br />
and this:<br />
<br />
<haskell><br />
data Either a b = Left a<br />
Either a b = Right b<br />
</haskell><br />
<br />
But how flexible are "data" definitions? As you should remember, "type"<br />
definitions were very limited in their features, while type classes,<br />
on the other hand, were more developed than ordinary Haskell functions<br />
facilities. What about features of "data" definitions examined as sort of functions?<br />
<br />
On the one hand, they supports multiple statements and multiple results and<br />
can be recursive, like the "List" definition above. On the other, that's all -<br />
no pattern matching or even type constants on the left side and no guards.<br />
<br />
Lack of pattern matching means that the left side can contain only free type<br />
variables. That in turn means that the left sides of all "data" statements for a<br />
type will be essentially the same. Therefore, repeated left sides in<br />
multi-statement "data" definitions are omitted and instead of<br />
<br />
<haskell><br />
data Either a b = Left a<br />
Either a b = Right b<br />
</haskell><br />
<br />
we write just<br />
<br />
<haskell><br />
data Either a b = Left a<br />
| Right b<br />
</haskell><br />
<br />
<br />
And here we finally come to GADTs! It's just a way to define data types using<br />
pattern matching and constants on the left side of "data" statements!<br />
Let's say we want to do this:<br />
<br />
<haskell><br />
data T String = D1 Int<br />
T Bool = D2<br />
T [a] = D3 (a,a)<br />
</haskell><br />
<br />
<br />
We cannot do this using a standard data definition. So, now we must use a GADT definition:<br />
<br />
<haskell><br />
data T a where<br />
D1 :: Int -> T String<br />
D2 :: T Bool<br />
D3 :: (a,a) -> T [a]<br />
</haskell><br />
<br />
Amazed? After all, GADTs seem to be a really simple and obvious extension to<br />
data type definition facilities.<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
The idea here is to allow a data constructor's return type to be specified<br />
directly:<br />
<br />
<haskell><br />
data Term a where<br />
Lit :: Int -> Term Int<br />
Pair :: Term a -> Term b -> Term (a,b)<br />
...<br />
</haskell><br />
<br />
In a function that performs pattern matching on Term, the pattern match gives<br />
type as well as value information. For example, consider this function:<br />
<br />
<haskell><br />
eval :: Term a -> a<br />
eval (Lit i) = i<br />
eval (Pair a b) = (eval a, eval b)<br />
...<br />
</haskell><br />
<br />
If the argument matches Lit, it must have been built with a Lit constructor,<br />
so type 'a' must be Int, and hence we can return 'i' (an Int) in the right<br />
hand side. The same thing applies to the Pair constructor.<br />
<br />
<br />
<br />
<br />
<br />
<br />
== Further reading ==<br />
<br />
The best paper on type level arithmetic using type classes I've seen<br />
is "Faking it: simulating dependent types in Haskell"<br />
( http://www.cs.nott.ac.uk/~ctm/faking.ps.gz ). Most of<br />
this article comes from his work.<br />
<br />
A great demonstration of type-level arithmetic is in the TypeNats package,<br />
which "defines type-level natural numbers and arithmetic operations on<br />
them including addition, subtraction, multiplication, division and GCD"<br />
( darcs get --partial --tag '0.1' http://www.eecs.tufts.edu/~rdocki01/typenats/ )<br />
<br />
I should also mention here Oleg Kiselyov's page on type-level<br />
programming in Haskell: http://okmij.org/ftp/Haskell/types.html<br />
<br />
<br />
There are plenty of GADT-related papers, but the best one for beginners<br />
is "Fun with phantom types"<br />
(http://www.informatik.uni-bonn.de/~ralf/publications/With.pdf).<br />
Phantom types is another name of GADT. You should also know that this<br />
paper uses old GADT syntax. This paper is a must-read because it<br />
contains numerous examples of practical GADT usage - a theme completely<br />
omitted from my article.<br />
<br />
<br />
Other GADT-related papers:<br />
<br />
"Dynamic Optimization for Functional Reactive Programming using<br />
Generalized Algebraic Data Types"<br />
http://www.cs.nott.ac.uk/~nhn/Publications/icfp2005.pdf<br />
<br />
"Phantom types" (actually more scientific version of "Fun with phantom types")<br />
http://citeseer.ist.psu.edu/rd/0,606209,1,0.25,Download/http:qSqqSqwww.informatik.uni-bonn.deqSq~ralfqSqpublicationsqSqPhantom.ps.gz<br />
<br />
"Phantom types and subtyping" http://arxiv.org/ps/cs.PL/0403034<br />
<br />
"Existentially quantified type classes" by Stuckey, Sulzmann and Wazny (URL?)</div>Dfeuerhttps://wiki.haskell.org/index.php?title=What_a_Monad_is_not&diff=58354What a Monad is not2014-06-22T21:26:25Z<p>Dfeuer: /* Monads are not about ordering/sequencing */ Remove utter nonsense claiming Maybe is commutative and handwaving about join.</p>
<hr />
<div>==Warning==<br />
Don't be surprised if you leave this page more confused than before. That just means that it has successfully destroyed your false assumptions, or that you've fallen for some horrible inside joke. Beware of [[Zygohistomorphic prepromorphisms]]. Go for [http://ertes.de/articles/monads.html warm and fuzzy], instead.<br />
<br />
==Monads are not a good choice as topic for your first Haskell blog entry==<br />
...just accept that they're [http://blog.plover.com/prog/burritos.html burritos], and wait until later.<br />
<br />
==Monads are not a language feature==<br />
Really. They are defined in terms of Haskell, not Haskell in terms of them. Conversely,<br />
<br />
==Haskell doesn't need Monads==<br />
...well, apart from the current Haskell standard defining the way IO is done in terms of Monads: [http://donsbot.wordpress.com/2009/01/31/reviving-the-gofer-standard-prelude-circa-1994/ It could be done differently and still work].<br />
<br />
==Monads are not impure==<br />
...In no way whatsoever. You don't even need flexible morals to claim it. To be more specific, it's IO that's impure. That makes the IO monad impure. But that's not a general property of monads - just IO. And even then, we can pretend that Haskell is a purely functional description language for imperative programs. But we didn't want to employ flexible morals, now did we?<br />
<br />
==Monads are not about state==<br />
While it is certainly possible to abstract away explicit state passing by using a Monad, that's not what a monad is. Some examples for monads that are not about state: Identity monad, Reader monad, List monad, Continuation monad, Exception monad.<br />
<br />
==Monads are not about strictness==<br />
Monad operations (bind and return) have to be [[Non-strict semantics|non-strict]] in fact, always! However<br />
other operations can be specific to each monad.<br />
For instance some are strict (like IO), and some are non-strict (like []). Then there are some that come in multiple flavours, like State.<br />
<br />
Try the following:<br />
<br />
<haskell><br />
runState (sequence . repeat $ state (\x -> (x,x+1))) 0<br />
</haskell><br />
<br />
Having a look at the implementation of fixIO might be helpful, too.<br />
<br />
==Monads are not values==<br />
This point might be driven home best by pointing out that instance Monad Foo where ... is not a data type, but a declaration of a typeclass instance. However, to elaborate:<br />
<br />
Monads are not values in the same sense that addition and multiplication are not numbers: They capture a -- very specific -- relationship between values of a specific domain into a common abstraction. We're going to call these values monads manage ''mobits'', somewhat like this:<br />
<br />
<haskell><br />
type Mobit m a = Monad m => m a<br />
</haskell><br />
<br />
The IO monad manages mobits representing side-effects ("IO actions").<br />
<br />
The List monad manages mobits representing multiple values ("[a]")<br />
<br />
The Reader monads manages mobits that are pure computations that use asks to propagate information instead of explicit arguments<br />
<br />
...and while addition and multiplication are both monoids over the positive natural numbers, a monad is a monoid object in a category of endofunctors: return is the unit, and join is the binary operation. It couldn't be more simple. If that confuses you, it might be helpful to see a Monad as a lax functor from a terminal bicategory.<br />
<br />
==Monads are not a replacement for applicative functors==<br />
Instead, every monad ''is'' an applicative functor (as well as a functor). It is considered good practice not to use >>= if all you need is <*>, or even fmap.<br />
<br />
Not confusing which features of monads are specific to monads only and which stem from applicative functors is vitally important for a deeper understanding of monads. As an example, the applicative functor interface of parser libraries can parse context-free languages (modulo hacks abusing open recursion), while the monadic interface can parse context-sensitive grammars: Monads allow you to influence further processing by inspecting the result of your parse. To understand why, have a look at the type of >>=. To understand why applicative functors by themselves are sufficient to track the current parsing position and express sequencing, have a look at the [http://www.cs.uu.nl/research/techreps/repo/CS-2008/2008-044.pdf uu-parsinglib tutorial (pdf)].<br />
<br />
The exact differences are elaborated in even greater detail in Brent Yorgey's excellent [[Typeclassopedia]].<br />
<br />
==Monads are not about ordering/sequencing==<br />
Monads are commonly used to order sequences of computations. But this is misleading. Just as you can use monads for state, or strictness, you can use them to order computations. But there are also commutative monads, like Reader, that don't order anything. So ordering is not in any way essential to what a monad is.<br />
<br />
Let's have a look at what's meant by ordering. Consider an expression like<br />
<br />
<haskell><br />
let x = a<br />
y = b<br />
in f x y<br />
</haskell><br />
<br />
That gives the same result as<br />
<br />
<haskell><br />
let y = b<br />
x = a<br />
in f x y<br />
</haskell><br />
<br />
It doesn't matter what order we write the two bindings. But for doing I/O we'd like ordering. Monads allow us to express<br />
<br />
<haskell><br />
do<br />
x <- getChar<br />
y <- getChar<br />
return (x,y)<br />
</haskell><br />
<br />
and have it be different from<br />
<br />
<haskell><br />
do<br />
y <- getChar<br />
x <- getChar<br />
return (x,y)<br />
</haskell><br />
<br />
Unlike the first, the second example returns a pair of characters in the opposite order to which they were entered.<br />
<br />
It might help to meditate about the difference between 'assignment' and 'binding', right now.<br />
<br />
However, just to spoil the enlightenment you just attained, there are monads for which swapping the order of lines like this makes no difference: For example, the Reader monad.<br />
<br />
So while it is correct to say that monads can be used to order operations, it would be wrong to say that monads are a mechanism for ordering operations.<br />
<br />
This notion of commutativity looks superficially very different from the familiar one in vanilla algebra where a+b=b+a. It doesn't mean that<br />
<haskell><br />
m >> n === n >> m<br />
</haskell><br />
which doesn't hold in general for ''any'' non-trivial monad, as for the most part<br />
<haskell><br />
return 1 >> return 2 === return 2 =/= return 1 === return 2 >> return 1<br />
</haskell><br />
<br />
This shouldn't be too surprising, though, as >> isn't the binary operation of a monoid. The [http://ncatlab.org/nlab/show/commutative+algebraic+theory category-theoretic definition] of a commutative monad is rather more abstract.<br />
<br />
==See also==<br />
<br />
* [[Do notation considered harmful]]<br />
<br />
[[Category:FAQ]]<br />
[[Category:Monad]]</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Talk:What_a_Monad_is_not&diff=58352Talk:What a Monad is not2014-06-19T17:23:56Z<p>Dfeuer: /* In what sense is Maybe commutative? */</p>
<hr />
<div>==Nicolas Pouillard Comments==<br />
<br />
I disagree with:<br />
"So ordering is not in any way essential to what a monad is."<br />
<br />
There is commutative monads, great! Other than that the order is important. Moreover there is nothing wrong to see >>= as a sequencing operator.<br />
<br />
I also disagree the "IO is impure" paragraph:<br />
Indeed only IO will trigger visible side effects, but this is only due to the common evaluation of IO primitives. Moreover I would say that only the runtime system is impure because it does reduce 'main :: IO ()', other than that we are<br />
just building a computation plan in a pure way.<br />
<br />
Although I agree that the common issue is too mix monad and impurity, and the impurity question is only releated to IO.<br />
<br />
== "Monads are not values" ==<br />
<br />
Yes they are! But they are not of the type of their arguments. For instance "return 3" is a value that has (or can have) type "IO Int". But it is not a value of type Int. &mdash;[[User:Ashley Y|Ashley Y]] 23:44, 23 November 2009 (UTC)<br />
<br />
[Nicolas Pouillard]: The point was that the IO monad is not a value, of course there is values of type "IO Int". However the record/dictionary is a value and one could say that it is the monad.<br />
<br />
== In what sense is Maybe commutative? ==<br />
<br />
It seems to me that <hask>Maybe</hask> is not really a commutative monad, as claimed here and at [[Monad]].<br />
<br />
<haskell><br />
do<br />
x <- Nothing<br />
y <- undefined<br />
return (x+y)<br />
</haskell><br />
produces <hask>Nothing</hask> whereas<br />
<haskell><br />
do<br />
y <- undefined<br />
x <- Nothing<br />
return (x+y)<br />
</haskell><br />
produces <hask>undefined</hask>.<br />
<br />
Note that the <hask>Reader</hask> monad (also mentioned in [[Monad]]) ''does'' appear to be commutative.<br />
--[[User:Dfeuer|Dfeuer]] ([[User talk:Dfeuer|talk]]) 17:15, 19 June 2014 (UTC)</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Talk:What_a_Monad_is_not&diff=58351Talk:What a Monad is not2014-06-19T17:15:14Z<p>Dfeuer: /* In what sense is Maybe commutative? */</p>
<hr />
<div>==Nicolas Pouillard Comments==<br />
<br />
I disagree with:<br />
"So ordering is not in any way essential to what a monad is."<br />
<br />
There is commutative monads, great! Other than that the order is important. Moreover there is nothing wrong to see >>= as a sequencing operator.<br />
<br />
I also disagree the "IO is impure" paragraph:<br />
Indeed only IO will trigger visible side effects, but this is only due to the common evaluation of IO primitives. Moreover I would say that only the runtime system is impure because it does reduce 'main :: IO ()', other than that we are<br />
just building a computation plan in a pure way.<br />
<br />
Although I agree that the common issue is too mix monad and impurity, and the impurity question is only releated to IO.<br />
<br />
== "Monads are not values" ==<br />
<br />
Yes they are! But they are not of the type of their arguments. For instance "return 3" is a value that has (or can have) type "IO Int". But it is not a value of type Int. &mdash;[[User:Ashley Y|Ashley Y]] 23:44, 23 November 2009 (UTC)<br />
<br />
[Nicolas Pouillard]: The point was that the IO monad is not a value, of course there is values of type "IO Int". However the record/dictionary is a value and one could say that it is the monad.<br />
<br />
== In what sense is Maybe commutative? ==<br />
<br />
It seems to me that <hask>Maybe</hask> is not really a commutative monad, as claimed here and at [[Monad]].<br />
<br />
<haskell><br />
do<br />
x <- Nothing<br />
y <- undefined<br />
return (x+y)<br />
</haskell><br />
produces <hask>Nothing</hask> whereas<br />
<haskell><br />
do<br />
y <- undefined<br />
x <- Nothing<br />
return (x+y)<br />
</haskell><br />
produces <hask>undefined</hask>.<br />
--[[User:Dfeuer|Dfeuer]] ([[User talk:Dfeuer|talk]]) 17:15, 19 June 2014 (UTC)</div>Dfeuerhttps://wiki.haskell.org/index.php?title=Talk:What_a_Monad_is_not&diff=58350Talk:What a Monad is not2014-06-19T17:14:36Z<p>Dfeuer: /* In what sense is Maybe commutative? */ new section</p>
<hr />
<div>==Nicolas Pouillard Comments==<br />
<br />
I disagree with:<br />
"So ordering is not in any way essential to what a monad is."<br />
<br />
There is commutative monads, great! Other than that the order is important. Moreover there is nothing wrong to see >>= as a sequencing operator.<br />
<br />
I also disagree the "IO is impure" paragraph:<br />
Indeed only IO will trigger visible side effects, but this is only due to the common evaluation of IO primitives. Moreover I would say that only the runtime system is impure because it does reduce 'main :: IO ()', other than that we are<br />
just building a computation plan in a pure way.<br />
<br />
Although I agree that the common issue is too mix monad and impurity, and the impurity question is only releated to IO.<br />
<br />
== "Monads are not values" ==<br />
<br />
Yes they are! But they are not of the type of their arguments. For instance "return 3" is a value that has (or can have) type "IO Int". But it is not a value of type Int. &mdash;[[User:Ashley Y|Ashley Y]] 23:44, 23 November 2009 (UTC)<br />
<br />
[Nicolas Pouillard]: The point was that the IO monad is not a value, of course there is values of type "IO Int". However the record/dictionary is a value and one could say that it is the monad.<br />
<br />
== In what sense is Maybe commutative? ==<br />
<br />
It seems to me that <hask>Maybe</hask> is not really a commutative monad, as claimed here and at [[Monad]].<br />
<br />
<br />
<haskell><br />
do<br />
x <- Nothing<br />
y <- undefined<br />
return (x+y)<br />
</haskell><br />
produces <hask>Nothing</hask> whereas<br />
<haskell><br />
do<br />
y <- undefined<br />
x <- Nothing<br />
return (x+y)<br />
</haskell><br />
produces <hask>undefined</hask>.</div>Dfeuer