IO and purity
IO is a type
A function that prints has a different type from a function that computes: putStrLn :: String -> IO (). The IO is part of the type, so a value of type IO () is a description of an action, and the action happens only when the runtime executes it. That is the whole trick of purity: the computation is still a value, and the effect is in the type.
putStrLn :: String -> IO ()
print :: Show a => a -> IO ()
getLine :: IO String
pure :: a -> IO a
pure 3 :: IO Int is an action that produces 3 and does nothing else. A function in IO can call a pure function freely, but a pure function cannot call an IO one: there is no way to write it that type checks, which is the next but one section.
main is an IO value
The entry point is a value of type IO (), and the runtime executes it once. A do block of actions is the usual shape, and it is the same notation as the last lesson, now over the IO monad:
module Main (main) where
import Data.Char (toUpper)
main :: IO ()
main = do
putStr "one "
putStrLn "two"
result <- fmap (* 2) (pure (21 :: Int))
print result
let line = "text"
putStrLn (map toUpper line)
ghc -Wall -O1 -o io io.hs
./io
one two
42
TEXT
putStr writes without a newline, so the first two lines end up on one output line. result <- runs an action and binds the value it produced; the action here is pure 21 mapped with (* 2), which is how a pure function is applied to the result of an action without leaving IO.
fmap over IO
IO is a functor, an applicative and a monad, so the tools of the last three lessons work on it. fmap f applies a pure function to the value an action produces, and >>= runs one action and feeds its result to a function that returns the next action.
fmap (* 2) (pure 21) -- an IO Int that produces 42
pure 21 >>= \x -> pure (x * 2) -- the same, with bind
mapM_ print [1, 2, 3] -- three actions, each printing one number
replicateM_ 3 (putStrLn "x") -- three copies of one action
mapM_ is the loop of the IO world: it runs an action for every element and discards the results. The underscore version returns IO (); mapM collects the results in a list instead. Both are in the Prelude.
A pure function cannot print
The type checker enforces the separation, and the error is worth seeing once. A function declared with a pure type that tries to print does not compile:
module Main (main) where
addOne :: Int -> Int
addOne x = print x
main :: IO ()
main = print (addOne 1)
ghc -Wall -O1 -o pureerror pureerror.hs
pureerror.hs:4:12: error: [GHC-83865]
• Couldn't match expected type ‘Int’ with actual type ‘IO ()’
• In the expression: print x
In an equation for ‘addOne’: addOne x = print x
|
4 | addOne x = print x
| ^^^^^^^
The message says it exactly: print x has type IO () and the signature promises Int. The fix is to move the printing to the caller, in main, and keep addOne pure: main = print (addOne 1), which is what the program already does below the error. This is the compiler enforcing a design rule, not a missing feature.
Exercises
- Print three lines with mapM_ putStrLn and a list of strings.
- Use fmap over pure 5 to double a number and print the result.
- Explain the type of putStrLn . show and why it is useful.
- Write readNumber :: IO Int that returns pure 42, then print it with >>=.
- Change the signature of addOne to Int -> IO Int and make the program compile with the print inside.
- Explain why a function of type Int -> Int can be called a million times with the same argument and always answer the same.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the type
IO ()mean?Answer
An action that may do effects and produces no useful value
IO wraps an action; the () says it produces nothing that the program uses.
-
What is the type of
putStrLn?Answer
String -> IO ()It takes a string, performs the effect and returns the unit value in IO.
-
What does
putStr "a"do differently fromputStrLn "a"?Answer
putStr does not add a newline
The two differ only in the trailing newline, which is why the first output line was one two.
-
Why can a function of type
Int -> Intnot print?Answer
print has type IO (), so the types do not match
The effect is in the IO type, and a pure signature has no IO in it.
-
What does this program print?
module Main (main) where main :: IO () main = do x <- fmap (* 2) (pure (21 :: Int)) print xAnswer
42
The action produces 21, fmap doubles it and print shows 42.
-
What does
mapM_ print [1, 2, 3]do?Answer
Runs print for each element and discards the results
mapM_ is the looping action; the underscore says the results are discarded.
-
What is the error
print xinsideaddOne :: Int -> Int?Answer
GHC-83865: could not match Int with IO ()
The expression has type IO () and the signature promises Int, which is the classic type mismatch.
-
What does
fmap (* 2) (pure 21) :: IO Intrepresent?Answer
An action that produces 42
The action produces a value and fmap transforms it before the runtime uses it.
-
Which functions run an action and let you use its result?
Answer
>>=, and<-in a do blockBind and the arrow pass the produced value to the next step.
-
What does
putStrLn (map toUpper "text")print?Answer
TEXT
The pure function uppercases the string and putStrLn prints the result.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev