-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_without_breaking_a_sweat.tex
357 lines (316 loc) · 8.64 KB
/
io_without_breaking_a_sweat.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
%!TEX program = xelatex
\documentclass{beamer}
\usepackage{listings}
\usepackage{color}
\usepackage{hyperref}
\usetheme{Execushares}
\title{IO Without Breaking a Sweat}
\subtitle{Explaining Haskell's IO without Monads}
\author{Chris Wilson}
\date{March 3, 2014}
\setcounter{showSlideNumbers}{1}
% code listings (other settings:
% language, firstline, lastline)
\lstloadlanguages{Haskell}
\lstset{
basicstyle=\ttfamily,
language=Haskell,
tabsize=4,
upquote=true,
xleftmargin=\parindent
}
\begin{document}
\setcounter{showProgressBar}{0}
\setcounter{showSlideNumbers}{0}
\frame{\titlepage}
\begin{frame}{Contents}
\begin{enumerate}
\item IO as a Value \\ \textcolor{ExecusharesGrey}{\footnotesize\hspace{1em} See how IO is just another value like anything else}
\item Working with IO \\ \textcolor{ExecusharesGrey}{\footnotesize\hspace{1em} Use the fact that \emph{IO is a value} to get stuff done}
\item Next Steps \\ \textcolor{ExecusharesGrey}{\footnotesize\hspace{1em} Some thoughts and links}
\end{enumerate}
\end{frame}
\setcounter{framenumber}{0}
\setcounter{showProgressBar}{1}
\setcounter{showSlideNumbers}{1}
\section{Introduction}
\begin{frame}{Before I Start}
This presentation is based on Neil Mitchell's excellent blog post,
\emph{Haskell IO Without Monads}: \\
\small{\url{http://neilmitchell.blogspot.co.uk/2010/01/haskell-io-without-monads.html}}
\end{frame}
\section{IO as a Value}
\begin{frame}{Four IO Functions}
\begin{itemize}
\item \lstinline{readFile :: FilePath -> IO String} \\ read in a file
\item \lstinline{writeFile :: FilePath -> String -> IO ()} \\ write out a file
\item \lstinline{getArgs :: IO [String]} \\ get command line arguments (as a list of strings)
\item \lstinline{putStrLn :: String -> IO ()} \\ write a string to the
console followed by a newline
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]{Simple IO Pattern}
\begin{lstlisting}
main :: IO ()
main = do
src <- readFile "file.in"
writeFile "file.out" (operate src)
operate :: String -> String
operate = -- your code here
\end{lstlisting}
\begin{itemize}
\item The ``processor'' function, \lstinline{operate}, is just a normal function.
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Action List Pattern}
\begin{lstlisting}
main :: IO ()
main = do
x1 <- expr1
x2 <- expr2
-- ...
xN <- exprN
return ()
\end{lstlisting}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Action List Pattern}
\begin{lstlisting}
main :: IO ()
main = do
[arg1,arg2] <- getArgs
src <- readFile arg1
res <- return (operate src)
_ <- writeFile arg2 res
return ()
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Simplifying IO}
You can simplify IO according to three rules:
\begin{enumerate}
\item \lstinline{_ <- x} can be written as \lstinline{x}.
\item If the second-to-last thing in a do block has no binding arrow (\lstinline{<-}) and is of type \lstinline{IO ()}, then you can leave off the \lstinline{return ()}.
\item \lstinline{x <- return y} can be re-written as
\lstinline{let x = y}
\end{enumerate}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Simplifying IO}
\begin{lstlisting}
main :: IO ()
main = do
[arg1,arg2] <- getArgs
src <- readFile arg1
res <- return (operate src)
_ <- writeFile arg2 res
return ()
\end{lstlisting}
\begin{itemize}
\item We can re-factor our code using these rules!
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Simplifying IO}
\begin{lstlisting}
main :: IO ()
main = do
[arg1,arg2] <- getArgs
src <- readFile arg1
res <- return (operate src)
writeFile arg2 res
return ()
\end{lstlisting}
\begin{itemize}
\item Rule 1
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]{Simplifying IO}
\begin{lstlisting}
main :: IO ()
main = do
[arg1,arg2] <- getArgs
src <- readFile arg1
res <- return (operate src)
writeFile arg2 res
\end{lstlisting}
\begin{itemize}
\item Rule 2
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]{Simplifying IO}
\begin{lstlisting}
main :: IO ()
main = do
[arg1,arg2] <- getArgs
src <- readFile arg1
let res = operate src
writeFile arg2 res
\end{lstlisting}
\begin{itemize}
\item Rule 3
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]{Nested IO}
\begin{itemize}
\item We can also \emph{nest} IO actions...
\end{itemize}
\begin{lstlisting}
title :: String -> IO ()
title str = do
putStrLn str
putStrLn (replicate (length str) '-')
putStrLn ""
\end{lstlisting}
\begin{itemize}
\item ...and then use it in \lstinline{main}.
\end{itemize}
\begin{lstlisting}
main :: IO ()
main = do
title "Hello"
title "Goodbye"
\end{lstlisting}
\end{frame}
\begin{frame}[containsverbatim]{Returning IO Values}
\begin{itemize}
\item We're not just limited to the \lstinline{IO ()} type, we can return values from IO
\item This function returns the first two command line args as a tuple:
\end{itemize}
\begin{lstlisting}
readArgs :: IO (String,String)
readArgs = do
xs <- getArgs
let x1 = if length xs > 0
then xs !! 0 else "file.in"
let x2 = if length xs > 1
then xs !! 1 else "file.out"
return (x1,x2)
\end{lstlisting}
\end{frame}
\begin{frame}[containsverbatim]{Returning IO Values}
\begin{itemize}
\item Now we can use \lstinline{readArgs} in \lstinline{main}:
\end{itemize}
\begin{lstlisting}
main :: IO ()
main = do
(arg1,arg2) <- readArgs
src <- readFile arg1
let res = operate src
writeFile arg2 res
\end{lstlisting}
\end{frame}
\begin{frame}[containsverbatim]{Optional IO}
\begin{itemize}
\item In any \emph{real} program, we need to \emph{optionally} run code in response to input:
\end{itemize}
\begin{lstlisting}
main :: IO ()
main = do
xs <- getArgs
if null xs then do
putStrLn "You entered no arguments"
else do
putStrLn ("You entered " ++ show xs)
\end{lstlisting}
\end{frame}
\section{Working with IO}
\begin{frame}[containsverbatim]{Remember, IO is a Value}
\begin{itemize}
\item Recall that the \lstinline{title} function had type \lstinline{IO ()}
\item Which means it can be used as-is in a do block to run the action three times
\item That is, we \emph{don't} have to immediately execute
\lstinline{IO} actions
\end{itemize}
\begin{lstlisting}
main :: IO ()
main = do
let x = title "Welcome"
x
x
x
\end{lstlisting}
\end{frame}
\begin{frame}[containsverbatim]{We Can Pass Arguments to IO}
\begin{lstlisting}
replicateM_ :: Int -> IO () -> IO ()
replicateM_ n act = do
if n == 0 then do
return ()
else do
act
replicateM_ (n-1) act
\end{lstlisting}
\begin{itemize}
\item We recursively run the \lstinline{IO ()} as many times as we need,
\item so, rewriting our last example:
\end{itemize}
\begin{lstlisting}
main :: IO ()
main = do
let x = title "Welcome"
replicateM_ 3 x
\end{lstlisting}
\end{frame}
\begin{frame}[containsverbatim]{Store IO in Structures}
\begin{itemize}
\item \lstinline{sequence_} runs a list of actions in turn:
\end{itemize}
\begin{lstlisting}
sequence_ :: [IO ()] -> IO ()
sequence_ xs = do
if null xs then do
return ()
else do
head xs
sequence_ (tail xs)
\end{lstlisting}
\begin{itemize}
\item We can refactor \lstinline{replicateM_} using \lstinline{sequence_}:
\end{itemize}
\begin{lstlisting}
replicateM_ :: Int -> IO () -> IO ()
replicateM_ n act = sequence_ (replicate n act)
\end{lstlisting}
\end{frame}
\begin{frame}[containsverbatim]{Pattern Match}
\begin{itemize}
\item Keeping in mind that \lstinline{IO} is just a value, we can pattern match on it
\item let's refactor that definition of \lstinline{sequence_}:
\end{itemize}
\begin{lstlisting}
sequence_ :: [IO ()] -> IO ()
sequence_ [] = return ()
sequence_ (x:xs) = do
x
sequence_ xs
\end{lstlisting}
\end{frame}
\begin{frame}[containsverbatim]{A Short Example}
\begin{lstlisting}
main :: IO ()
main = do
xs <- getArgs
sequence_ (map operateFile xs)
operateFile :: FilePath -> IO ()
operateFile x = do
src <- readFile x
writeFile (x ++ ".out") (operate src)
operate :: String -> String
operate = -- ...your code here
\end{lstlisting}
\begin{itemize}
\item This performs \lstinline{operate} on each file given on the command line.
\end{itemize}
\end{frame}
\section{Next Steps}
\begin{frame}{Other Things to Check Out}
\begin{itemize}
\item \href{http://www.cs.nott.ac.uk/~gmh/book.html}{\emph{Programming in Haskell}} by Graham Hutton (chapters 8 \& 9)
\item \href{https://wiki.haskell.org/Monads_as_Containers}{Monads as Containers}
\item Many more useful functions can be found in the \href{http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html}{Control.Monad} package
\end{itemize}
\end{frame}
\end{document}