-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresume.tex
352 lines (307 loc) · 8.29 KB
/
resume.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
\input{preamble.tex}
\usepackage[cache=false]{minted}
\title[]{Structuring Effectful Computations}
\subtitle{}
% \author[Георгий Лукьянов]{%
% Георгий Лукьянов\texorpdfstring{\\}{ }
% \author[]{%
% Георгий Лукьянов\texorpdfstring{\\}{ }
% \textit{[email protected]}\texorpdfstring{\\}{ }
% Артём Пеленицын \texorpdfstring{\\}{ }
% \textit{[email protected]}
% }
\author[Georgy\,Lukyanov]
{%
\texorpdfstring{
% \centering
Georgy Lukyanov\\ \scriptsize{\textit{[email protected]}}
}
{Georgy Lukyanov}
}
\date{1 November 2017}%
\institute[]{%
Newcastle University \texorpdfstring{\\}{ }
School of Engineering\texorpdfstring{\\}{ }
Tuura (https://github.com/tuura)\texorpdfstring{\\}{ }
}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%% Intro %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Why control side effects?}
\begin{frame}[fragile]{What is a side effect}
\begin{block}{Effectful function in an impure language}
\begin{minted}{csharp}
int plus(int x, int y) {
printf("Unstructured side effect");
return x + y;
}
\end{minted}
\end{block}
\begin{block}{Pure Haskell function}
\begin{minted}{haskell}
plus :: Int -> Int -> Int
plut x y = x + y
\end{minted}
\end{block}
\begin{block}{Effectful Haskell function}
\begin{minted}{haskell}
plusIO :: Int -> Int -> IO Int
plutIO x y = do
print "Structured side effect."
return (x + y)
\end{minted}
\end{block}
\end{frame}
\section{Haskell and effects}
\begin{frame}[fragile]{Functor}
\begin{block}{Functor type class}
\begin{minted}{csharp}
class Functor f where
fmap :: (a -> b) -> f a -> f b
\end{minted}
\end{block}
\pause
\begin{block}{Input a number}
\begin{minted}{haskell}
getInt :: IO Int
getInt = fmap read getLine
\end{minted}
\end{block}
\pause
\begin{block}{}
\begin{minted}{haskell}
ghci> 42
42
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{Monad}
\begin{block}{Monad type class}
\begin{minted}{haskell}
class Functor m => Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
\end{minted}
\end{block}
\pause
\begin{block}{Input two numbers and form a pair}
\begin{minted}{haskell}
getTwoInts :: IO (Int, Int)
getTwoInts = do x <- getInt
y <- getInt
return (x, y)
\end{minted}
\end{block}
\pause
\begin{block}{}
\begin{minted}{haskell}
ghci> 1
ghci> 2
(1,2)
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{do-notation}
\begin{block}{}
\begin{minted}{haskell}
main :: IO ()
main = do
putStrLn "What is your name?"
putStrLn "|> "
name <- getLine
putStrLn $ "Hello, " ++ name ++ "!"
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{MonadReader: static environment}
\begin{block}{MonadReader type class}
\begin{minted}{haskell}
class Monad m => MonadReader r m | m -> r where
-- | Retrieves the monad environment.
ask :: m r
-- | Executes a computation in a modified environment.
local :: (r -> r) -> m a -> m a
-- | Retrieves a function of the current environment.
reader :: (r -> a) -> m a
\end{minted}
\end{block}
\pause
\begin{block}{Read the environment and return it}
\begin{minted}{haskell}
readerExample :: (MonadReader Int m) => m Int
readerExample = do x <- ask
return x
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{MonadRandom: random values}
\begin{block}{MonadRandom type class}
\begin{minted}{haskell}
class (Monad m) => MonadRandom m where
getRandomR :: (Random a) => (a, a) -> m a
getRandom :: (Random a) => m a
getRandomRs :: (Random a) => (a, a) -> m [a]
getRandoms :: (Random a) => m [a]
\end{minted}
\end{block}
\pause
\begin{block}{Get two random numbers}
\begin{minted}{haskell}
randomPair :: (MonadRandom m) => m (Int, Int)
randomPair = do x <- getRandom
y <- getRandom
return (x, y)
\end{minted}
\end{block}
\end{frame}
\section{Programming with controlled effects}
\begin{frame}[fragile]{Zombie apocalypse}
\begin{figure}
\centering
\def\svgwidth{\columnwidth}
\input{people_and_zombey.pdf_tex}
\end{figure}
\end{frame}
\begin{frame}[fragile]{Problem: randomly split the group in pairs}
\begin{figure}
\centering
\def\svgwidth{\columnwidth}
\input{people.pdf_tex}
\end{figure}
\end{frame}
\begin{frame}[fragile]{Domain types}
\begin{block}{}
\begin{minted}{haskell}
type Survivor = Int
type Group = [Survivor]
-- | Survivors keep watch in pairs
type Shift = (Survivor, Survivor)
-- | One night's schedule has three shifts
type Schedule = (Shift, Shift, Shift)
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{Possible shifts for the Group (no randomness)}
\begin{block}{}
\begin{minted}{haskell}
possibleShifts :: (MonadReader Group m) => m [Shift]
possibleShifts = do
people <- ask -- people :: Group
return (pairs people)
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{Possible randomised shifts for the Group}
\begin{block}{}
\begin{minted}{haskell}
randomisedShifts :: ( MonadReader Group m
, MonadRandom m) => m [Shift]
randomisedShifts = do
shifts <- possibleShifts
randomised <- shuffleM shifts
return randomised
\end{minted}
\end{block}
\pause
\begin{block}{Shuffling function: the source of randomness}
\begin{minted}{haskell}
shuffleM :: MonadRandom m => [a] -> m [a]
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{Infinite stream of randomised shifts}
\begin{block}{}
\begin{minted}{haskell}
shifts :: ( MonadReader Group m
, MonadRandom m) => m [Shift]
shifts = fmap (concat . repeat) randomisedShifts
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{Infinite stream of randomised shifts (cryptic)}
\begin{block}{}
\begin{minted}{haskell}
shifts :: ( MonadReader Group m
, MonadRandom m) => m [Shift]
shifts = fmap (concat . repeat) step
where step = fmap pairs ask >>= shuffleM
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{Building the schedule}
\begin{block}{}
\begin{minted}{haskell}
-- | Construct a randomised schedule
buildSchedule :: ( MonadReader Group m
, MonadRandom m) => m Schedule
buildSchedule = do
[x, y, z] <- fmap (take 3) shifts
return (x, y, z)
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{Handling effects}
\begin{block}{}
\begin{minted}{haskell}
main :: IO ()
main = do
let people = [1..5]
schedule <- evalRandTIO $
runReaderT buildSchedule people
putStrLn $ "Watch schedule for this night: " ++
show schedule
\end{minted}
\end{block}
\end{frame}
\section{Programming WITHOUT controlled effects}
\begin{frame}[fragile]{Domain types}
\begin{block}{}
\begin{minted}{cpp}
using Survivor = int;
using Group = vector<Survivor>;
using Shift = tuple<int, int>;
using Schedule = tuple<Shift, Shift, Shift>;
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{Silently shuffling shifts}
\begin{block}{}
\begin{minted}{cpp}
vector<Shift> shifts (const Group & people) {
auto ps = pairs(people);
random_shuffle(ps.begin(), ps.end(), randomGen);
return ps;
}
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{Explicatively treading the parameter}
\begin{block}{}
\begin{minted}{cpp}
Schedule buildSchedule (const Group & people) {
auto shs = shifts(people);
return make_tuple(shs[0], shs[1], shs[2]);
}
\end{minted}
\end{block}
\end{frame}
\begin{frame}[fragile]{Summary}
\begin{itemize}
\item Track effects in types.
\item Execute pure functions in an effectful context.
\item Use \mintinline{haskell}{do}-notation to build ``imperative'' DSLs.
\end{itemize}
\end{frame}
\begin{frame}{References}
\begin{itemize}
\item Monadic Parser Combinators // \textit{Graham Hutton}, \textit{Erik Meijer} – Department of Computer Science, University of Nottingham, 1996
\item These slides and code \url{https://github.com/tuura/striot-monads-seminar}
\end{itemize}
\end{frame}
\end{document}