Recipes, not cooking
Since lesson 1 you have been writing main :: IO () and using do without asking how a pure language can print anything at all. Here is the answer, and it is less mysterious than its reputation.
A value of type IO a is an action: a description of some input and output that, when performed, produces a value of type a. Think of a recipe. Writing a recipe down does not cook anything; you can copy it, put it in a list, pass it to a friend, or combine two recipes into a menu. Only when someone follows it does the kitchen fill with smoke. In Haskell, the runtime system follows exactly one recipe: main. Everything else is about building that recipe out of smaller ones, and building is pure.
That design gives you something unusual. Because effects live in the type, a glance at a signature tells you whether a function can touch the outside world. parseAge :: String -> Either String Int cannot read files or send emails; loadConfig :: FilePath -> IO Config can. Most of a well-written Haskell program is pure, with a thin layer of IO at the edges.
You have also seen, in lesson 5, a problem that looks unrelated: a chain of lookups that may each fail turns into a staircase of nested case expressions. It turns out that "do this, then do that with the result" is the same pattern for IO actions, for computations that may fail, and for several other things. That shared pattern is what Haskell calls a monad, and do notation works for all of them.
The pieces
Definition— IO action
A value of type IO a describes an interaction with the outside world that yields an a. putStrLn "hi" :: IO (), getLine :: IO String, readFile "notes.txt" :: IO String. Building such a value performs nothing; the runtime performs main, and with it every action main is built from.
Definition— do-notation
Inside do, each line is an action of the same monad. x <- action performs action and names its result x for the following lines. A line without <- performs an action and ignores its result. let y = expr names a pure value. The value of the whole block is the value of its last line.
Definition— pure (or return)
pure x is the action that does nothing and yields x. The older name return means the same in a monad, and has nothing to do with leaving a function: it does not stop the do block.
Definition— Functor, Applicative, Monad
Three classes, each adding one power to the one before:
fmap :: Functor f => (a -> b) -> f a -> f b -- also written <$>
pure :: Applicative f => a -> f a
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
(>>=) :: Monad m => m a -> (a -> m b) -> m b -- pronounced "bind"fmap changes the result inside a context. <*> combines several independent computations in a context. >>= runs a computation and feeds its result to a function that chooses the next computation: that dependency is what only a monad can express.
Three monads at work
IO: actions are values
import Control.Monad (forM_, when)
import Data.IORef (modifyIORef', newIORef, readIORef)
-- A function that returns an action. Calling it prints nothing by itself.
greet :: String -> IO ()
greet name = putStrLn ("Hello, " ++ name ++ "!")
-- Actions are values, so we can pass them to functions.
twice :: IO () -> IO ()
twice action = do
action
action
main :: IO ()
main = do
let actions = [greet "Tomás", greet "Aroha", putStrLn "(end of list)"]
twice (greet "Wanjiru")
sequence_ (reverse actions)
forM_ [1 .. 5 :: Int] $ \i ->
when (even i) (putStrLn ("even: " ++ show i))
total <- newIORef (0 :: Int) -- a mutable cell, usable only inside IO
forM_ [5, 10, 20] $ \x -> modifyIORef' total (+ x)
t <- readIORef total
print t
answer <- pure 41 -- pure wraps a value in an action; <- takes it out again
print (answer + 1)Hello, Wanjiru!
Hello, Wanjiru!
(end of list)
Hello, Aroha!
Hello, Tomás!
even: 2
even: 4
35
42Notice what didn't happen. The let actions = [...] line built a list of three actions and printed nothing. twice received an action and performed it two times. sequence_ performed a list of actions in order, here reversed. These are ordinary functions on ordinary values; there is nothing special about IO except that the runtime performs main.
forM_ xs f is mapM_ with its arguments swapped, which reads like a for loop when the body is a lambda. when cond action performs the action only if the condition holds. An IORef is a mutable variable, and its operations are IO actions, so mutation can only happen in IO code, where the type announces it. Use it sparingly: in Haskell, state usually travels as function arguments and results.
Reading input works the same way: line <- getLine performs the action of reading one line and names the resulting String. (The programs in this course are run without any input, so they don't read any.)
Maybe and Either: stop at the first failure
import Control.Monad (when)
import Text.Read (readMaybe)
data Person = Person {name :: String, age :: Int} deriving (Show)
-- Maybe: each step may fail, and the first failure stops the chain.
safeDiv :: Int -> Int -> Maybe Int
safeDiv _ 0 = Nothing
safeDiv a b = Just (a `div` b)
share :: Int -> Int -> Int -> Maybe Int
share total people tables = do
perPerson <- safeDiv total people
perTable <- safeDiv total tables
pure (perPerson + perTable)
-- Either: the same idea, but a failure carries a message.
parseAge :: String -> Either String Int
parseAge s = case readMaybe s of
Nothing -> Left ("not a number: " ++ s)
Just n
| n < 0 -> Left ("negative age: " ++ show n)
| otherwise -> Right n
parsePerson :: String -> Either String Person
parsePerson line = do
(n, rest) <- case break (== ',') line of
(n, ',' : rest) -> Right (n, rest)
_ -> Left ("no comma in " ++ show line)
when (null n) (Left "empty name")
a <- parseAge rest
pure (Person n a)
main :: IO ()
main = do
print (share 120 4 3, share 120 0 3)
mapM_ (print . parsePerson) ["Zainab,34", "Lars,-2", "Mia,abc", ",20", "nofields"]
print (traverse parsePerson ["Zainab,34", "Hiro,51"])
print (traverse parsePerson ["Zainab,34", "Lars,-2", "Mia,abc"])(Just 70,Nothing)
Right (Person {name = "Zainab", age = 34})
Left "negative age: -2"
Left "not a number: abc"
Left "empty name"
Left "no comma in \"nofields\""
Right [Person {name = "Zainab", age = 34},Person {name = "Hiro", age = 51}]
Left "negative age: -2"Here do is not about IO at all. In the Maybe monad, x <- m means "if m is Just x, carry on with x; if it is Nothing, the whole block is Nothing". In the Either String monad it means the same, except that the failure carries a message, and the first Left is the one you get. Compare parsePerson with the nested case staircase of lesson 5: every step can fail, yet the code reads as a straight line, and no failure can be silently ignored.
readMaybe, from Text.Read, is the safe cousin of read: it returns Nothing instead of crashing on bad input. And when (null n) (Left "empty name") works in Either just as it works in IO, because when is defined for every monad.
traverse deserves a place in your vocabulary. traverse parsePerson applies a function that may fail to every element of a list and returns either all the results or the first error. It is the honest way to validate a batch of input: you never end up with a half-checked list.
Functor, Applicative and the list monad
import Control.Monad (guard)
main :: IO ()
main = do
-- Functor: change the value inside the context.
print (fmap (* 2) (Just 21), (* 2) <$> [1, 2, 3])
-- Applicative: combine several independent computations.
print ((+) <$> Just 3 <*> Just 4, (+) <$> Just 3 <*> Nothing)
print (max <$> [1, 5] <*> [3, 4])
print (traverse (\x -> if x > 0 then Just x else Nothing) [3, 1, 2])
print (sequence [Just 1, Nothing, Just 3])
-- Monad: the next step depends on the previous result.
-- In the list monad, "<-" means "try every possibility".
let sevens = do
a <- [1 .. 6]
b <- [1 .. 6]
guard (a + b == 7)
pure (a, b)
print sevens
print [(a, b) | a <- [1 .. 6], b <- [1 .. 6], a + b == 7] -- the same thing
print ([1, 2, 3] >>= \x -> [x, x * 10])
print (Just 5 >>= \x -> if x > 3 then Just (x * 2) else Nothing)(Just 42,[2,4,6])
(Just 7,Nothing)
[3,4,5,5]
Just [3,1,2]
Nothing
[(1,6),(2,5),(3,4),(4,3),(5,2),(6,1)]
[(1,6),(2,5),(3,4),(4,3),(5,2),(6,1)]
[1,10,2,20,3,30]
Just 10<$> is fmap written as an operator. The pattern f <$> a <*> b applies a two-argument function to two values in a context: two Maybes (any Nothing makes the result Nothing), or two lists (every combination). sequence turns a list of Maybes into a Maybe of a list, failing if any element fails.
The list monad treats a list as "several possible results". a <- [1 .. 6] means "for each possible a", guard discards the branches where a condition fails, and the result collects every surviving outcome: the six ways two dice can show a total of seven. If that looks like a list comprehension, it is no coincidence; comprehensions are this monad with nicer syntax.
The last two lines use >>= directly, without do. On lists, xs >>= f applies f to each element and concatenates the resulting lists. On Maybe, it feeds the value inside a Just to f, or stops at Nothing.
What a monad is, concretely
The type of >>= says it all: m a -> (a -> m b) -> m b. Given a computation that will produce an a, and a function that, given that a, chooses the next computation, produce the combined computation. Every monad answers one question: what does "and then" mean here?
| Monad | m >>= f means | Typical use |
|---|---|---|
Maybe | if m produced a value, pass it to f; otherwise stop with Nothing | lookups and parsing that may fail |
Either e | the same, but stop with the error Left e | validation with error messages |
[] | pass each result to f and concatenate all the outcomes | search, combinations |
IO | perform m, then perform the action f chooses from its result | input and output |
A do block is shorthand for a chain of >>= and lambdas. This block
do
x <- safeDiv 100 a
y <- safeDiv x b
pure (x + y)means exactly
safeDiv 100 a >>= \x ->
safeDiv x b >>= \y ->
pure (x + y)Each <- becomes a >>= followed by a lambda that names the result. A line without <- uses >>, which is >>= with a function that ignores its argument. There is no magic: you could write every Haskell program without do, and early Haskell programmers sometimes did.
Why three classes rather than one? Because each extra power costs something. Every monad is an applicative and every applicative is a functor, but not the other way round, and the weaker interfaces let libraries do more: an applicative computation's structure is known in advance (no step depends on an earlier result), so a parser library can analyse it, and a library such as Haxl, which Meta built for its spam-fighting rules, can batch and parallelise independent data fetches automatically. The rule of thumb professionals follow is to use the weakest interface that does the job: fmap if you only change a result, <*> if the steps are independent, >>= only when the next step depends on the previous result.
The monad laws say that pure really does nothing (pure x >>= f is f x, and m >>= pure is m) and that chaining is associative, so you can split a long do block into smaller functions without changing its meaning. Every standard instance obeys them.
IO in real programs
Professional Haskell code usually has a shape sometimes called functional core, imperative shell: parsing, validation and business logic are pure functions returning Maybe, Either or plain values, easy to test; a thin layer in IO reads input, calls the core, and writes output.
The everyday IO vocabulary, all in the standard library:
import System.Environment (getArgs)
import System.IO (hFlush, stdout)
main :: IO ()
main = do
args <- getArgs -- command-line arguments, a [String]
contents <- readFile "input.txt" -- the whole file as a String
putStr "Your name? " >> hFlush stdout
name <- getLine -- one line from standard input
writeFile "out.txt" (name ++ "\n" ++ show (length (lines contents)))For large applications, teams add structure on top of IO. A common pattern, often called ReaderT over IO, passes an environment (configuration, database handles, a logger) to every function implicitly, using the mtl and transformers libraries, which ship with GHC. Other teams use effect libraries such as effectful, which let a type say which effects a function may use, not just "any IO". You will meet these in real codebases; the foundation is always the >>= of this lesson.
Common misconceptions
Misconception
"return leaves the function, as in C or Python." return x (or pure x) is just an action that yields x; the lines after it in a do block still run. do { return 1; putStrLn "still here" } prints the message.
Misconception
"Monads are about side effects." Maybe, Either and lists are monads with no effects at all; the abstraction is about sequencing, and IO is one instance among many.
Misconception
"I need to get the value out of the IO." Inside a do block, x <- action gives you the value to use in the following lines; that is all you ever need. Rather than getting values out of IO, you bring pure functions in: fmap parse (readFile path), or contents <- readFile path followed by pure code. (A function named unsafePerformIO does exist for library authors with very specific needs; ordinary code never uses it.)
Misconception
"let name = getLine reads a line." let only gives a name to a value, and getLine is an action, so name is the action itself, of type IO String. Nothing is read, and using name as a string is a type error. name <- getLine is what performs the action and names its result. The rule: <- for running an action and taking its result, let for naming a pure value.
Misconception
"<- works anywhere, like assignment." It only appears inside do (and comprehensions), and the thing on its right must be a computation in the same monad as the whole block. You cannot use <- on a Maybe inside an IO block; match on it with case instead, or use maybe, fromMaybe or traverse.
Exercises
1. IO, do-notation and monads, concretely
Warm-upComputationalWhat does this program print? Write the lines separated by single spaces.
main :: IO ()
main = do
putStrLn "a"
return ()
putStrLn "b"
n <- return 7
print (n * 6)
2. IO, do-notation and monads, concretely
CoreComputationalWhat does this program print?
safeDiv :: Int -> Int -> Maybe Int
safeDiv _ 0 = Nothing
safeDiv a b = Just (a `div` b)
main :: IO ()
main = print (traverse (safeDiv 12) [1, 2, 3], traverse (safeDiv 12) [4, 0, 6])
3. IO, do-notation and monads, concretely
CoreComputationalWhat does this program print?
main :: IO ()
main = print ((,) <$> [1, 2] <*> "ab")
4. IO, do-notation and monads, concretely
CoreComputationalWhat does this program print?
check :: Int -> Either String Int
check n = if n > 100 then Left ("too big: " ++ show n) else Right n
main :: IO ()
main = print $ do
a <- check 40
b <- check (a * 3)
c <- check (b - 50)
pure (a + c)
5. IO, do-notation and monads, concretely
CoreHumanWhich expressions mean the same as do { line <- getLine; putStrLn (reverse line) }? Choose all that apply.
6. IO, do-notation and monads, concretely
CoreComputationalWhat does this program print?
main :: IO ()
main = print (length pairs)
where
pairs = do
x <- [1 .. 4]
y <- [x .. 4]
pure (x, y)
7. IO, do-notation and monads, concretely
StretchHumanYou need to load a user's profile and, separately, the current exchange rates, then combine them into a page. Neither load depends on the other's result. Which is the weakest interface that expresses this, letting a library run both loads at once?
Further reading and resources
- Control.Monad documentation (base)forM_, when, unless, guard, replicateM, foldM and the other monadic helpers.
- Haskell 2010 Language Report, section 3.14: Do ExpressionsThe exact translation of do-notation into >>=.
- Monads for functional programming · Philip Wadler · 1995Lecture notes that introduced monads to many programmers through small interpreters, from the Advanced Functional Programming summer school.
- TypeclassopediaFunctor, Applicative and Monad with their laws, and how they relate.