-
Notifications
You must be signed in to change notification settings - Fork 5
/
lec09.html
690 lines (574 loc) · 21.4 KB
/
lec09.html
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to Haskell - Lecture 9</title>
<meta name="description" content="A course on the world's fastest growing functional programming language">
<meta name="author" content="Nishant Shukla">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/serif.css" id="theme">
<link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
<style>
body {
padding-top: 30px;
padding-bottom: 40px;
}
</style>
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/pojoaque.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
</head>
S*
<body>
<script src="nav.js"></script>
<script>
function toggleElements(one, two) {
document.getElementById(one).style.display = 'none';
document.getElementById(two).style.display = 'block';
}
</script>
<div class="reveal">
<!-- Slides begin here! -->
<div class="slides">
<section>
<section>
<h1>Introduction To Haskell</h1>
<p>Lecture 9</p>
<p>
<br>
</p>
<p>M.. M.. MONADS!</p>
</section>
<section>
<h3>Using These Slides</h3>
<h4>Every slide has a secret note.</h4>
<small>
<ul>
<li>On <b>Chrome</b>: press <code>F12</code>, then click <b>Console</b></li>
<li>On <b>IE</b>: press <code>F12</code>, then click <b>Console</b></li>
<li>On <b>Firefox</b>: <code>Ctrl+Shift+k</code></li>
</ul>
</small>
<br>
<br>
<p><h3>Shortcut Keys:</h3></p>
<center>
<table width="80%">
<tr>
<td><code>↓</code>, <code>PgDn</code>, <code>n</code>, <code>j</code></td>
<td>next slide</td>
</tr>
<tr>
<td><code>↑</code>, <code>PgUp</code>, <code>p</code>, <code>k</code></td>
<td>prev slide</td>
</tr>
<tr>
<td><code>Esc</code></td>
<td>enables <code>ctrl+f</code> globally</td>
</tr>
</table>
</center>
<aside class="notes">Hi there! This is a secret lecture note. Every slide has a little blurb of text like this!</aside>
</section>
<script>
function clearAll() {
document.getElementById('revA').style.display = 'none';
document.getElementById('revB').style.display = 'none';
document.getElementById('revC').style.display = 'none';
document.getElementById('revD').style.display = 'none';
}
function showOne(strong) {
clearAll();
document.getElementById(strong).style.display = 'block';
}
</script>
<section>
<h3>Review of Homework 8</h3>
<small><a href="lec08.html#/0/19" target="_blank">Write an IO program that reverses text</a></small>
<table style="width:100%">
<tr>
<td><center><a onclick="showOne('revA');">A</a></center></td>
<td><center><a onclick="showOne('revB');">B</a></center></td>
<td><center><a onclick="showOne('revC');">C</a></center></td>
<td><center><a onclick="showOne('revD');">D</a></center></td>
</tr>
</table>
<pre id='revA'><code class='haskell'>
-- Designed by Eric J. Bomgardner
module Main where
main = do
theInput <- readFile "input.txt"
writeFile "output.txt" (reverse theInput)
</code></pre>
<pre id='revB' style="display:none;"><code class='haskell'>
-- Designed by Saul Brodsky
module Main where
main = do
answer <- readFile "input.txt"
let rev = reverse answer in writeFile "output.txt" rev
</code></pre>
<pre id='revC' style="display:none;"><code class='haskell'>
-- Designed by Travis Dean
main = interact reverse
-- This must be run by directing input and output like so:
-- $ runhaskell reverseFile.hs < input.txt > output.txt
</code></pre>
<pre id='revD' style="display:none;"><code class='haskell'>
-- Designed by Anish S. Tondwalkar
main = readFile input >>= return . reverse >>= writeFile output
where input = "input"
output = "output"
</code></pre>
</section>
<section>
<h2>The <code>Maybe</code> data type</h2>
<pre><code class="haskell">
data Maybe a = Nothing | Just a
</code></pre>
<div class="fragment roll-in">
<p>Use <code>Maybe</code> when the output isn't guaranteed</p>
<pre class="fragment roll-in"><code class="haskell">
Prelude> import Data.List
Prelude> :t elemIndex
elemIndex :: Eq a => a -> [a] -> Maybe Int
</code></pre>
<pre class="fragment roll-in"><code class="haskell">
Prelude Data.List> elemIndex 'b' "abc"
Just 1
Prelude Data.List> elemIndex 'z' "abc"
Nothing
</code></pre>
</div>
<aside class="notes">Notice the different styles people use to arrive at the some solution.</aside>
</section>
<section>
<h3>Context</h3>
<p><code>Maybe</code> wraps things into a context.</p>
<br>
<ul>
<li><i><code>Just 1</code></i> vs <i><code>1</code></i></li>
<br>
<li><i><code>Just "hello"</code></i> vs <i><code>"hello"</code></i></li>
<br>
<li><i><code>Just 'a'</code></i> vs <i><code>'a'</code></i></li>
<br>
<li><i><code>Nothing</code></i> vs <i><code><b>ERROR!</b></code></i></li>
</ul>
<aside class="notes">A context gives some guarantee and safety to a value.</aside>
</section>
<section>
<h2>Functor</h2>
<p>A Functor generalizes the <code>map</code> function</p>
<p>It implements <code>fmap</code></p>
<pre><code class="haskell">
fmap :: (a -> b) -> f a -> f b
</code></pre>
<br>
<p>Naturally, a list derives the Functor Typeclass</p>
<pre><code class="haskell">
Prelude> fmap (+1) [1,2,3]
[2,3,4]
</code></pre>
<aside class="notes">Sometimes you want to map over a different data type, like a Graph or Tree.</aside>
</section>
<section>
<h3>What else is a Functor?</h3>
<div class="fragment roll-in">
<p><code>Maybe</code> is a Functor</p>
<pre><code class="haskell">
*Main> fmap (+1) (Just 1)
Just 2
</code></pre>
</div>
<br>
<div class="fragment roll-in">
<p><code>IO</code> is a Functor</p>
<pre><code class="haskell">
*Main> fmap (++"!") getLine
hi
"hi!"
</code></pre>
</div>
<aside class="notes">If it can be mapped, it's probably a Functor.</aside>
</section>
<section>
<h3>The Functor Laws</h3>
<small>Additionally, these rules are necessary to define a Functor.</small>
<p>First law:</p>
<pre><code class="haskell">
fmap id F = F
</code></pre>
<br>
<p>Second law:</p>
<pre><code class="haskell">
fmap (f . g) F = fmap f (fmap g F)
</code></pre>
<aside class="notes">A true Functor not only is of the Functor Typeclass, but also follows these two laws.</aside>
</section>
<section>
<h2>Applicative Typeclass</h2>
<pre><code>
Prelude> import Control.Applicative
Prelude> (*) <$> Just 2 <*> Just 8
Just 16
</code></pre>
<br>
<p><small>We won't go into much detail about the Applicative Typeclass</small></p>
<p><small><a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids" target="_blank">There is a great introduction here</a></small></p>
<aside class="notes">These allow powerful utilization of things in context.</aside>
</section>
<section>
<h2>Monoid</h2>
<p>A <b>Monoid</b> is a function (•) that satisfies the following:</p>
<ol>
<li class="fragment roll-in"><p>∀ a,b ∈ S: <b>a•b ∈ S</b></p>
<small>The Set (S) is <b>closed</b> under the binary function (•).</small></li>
<li class="fragment roll-in"><p>∀ a,b,c ∈ S: <b>(a•b)•c = a•(b•c)</b></p>
<small>The binary function is <b>associative</b></small>
</li>
<li class="fragment roll-in"><p>∃ e∈S: ∀ a∈S: <b>e•a = a•e = a</b></p>
<small><i>e</i> is the <b>identity</b> element</small></li>
</ol>
<br>
<br>
<small class="fragment roll-in">(A Monoid is a <a target="_blank" href="http://simple.wikipedia.org/wiki/Group_theory">Group</a> without the requirement of an inverse)</small>
<small class="fragment roll-in">(Or a <a target="_blank" href="http://en.wikipedia.org/wiki/Monoid#Relation_to_category_theory">Category</a> with a single object.)</small>
<aside class="notes">A Monoid and Monad share some similarities as you will see later.</aside>
</section>
<section>
<h3>Examples of Monoids</h3>
<p><code>+</code> , <code>0</code> is a Monoid.</p>
<br>
<p>Which of the following are Monoids?</p>
<ul>
<li><code>*</code> , <code>1</code> , <b>S={0,1,2,...}</b></li>
<li><code>||</code> , <code>False</code> , <b>S={True, False}</b></li>
<li><code>++</code> , <code>[]</code> , <b>S={"", "a", "ab",...}</b></li>
<li><code>∪</code> , <code>∅</code> , <b>S={A,B,...}</b></li>
<li><code>÷</code> , <code>1</code> , <b>S={1,2,...}</b></li>
</ul>
<aside class="notes">Monoids occur more likely than you think.</aside>
</section>
<section>
<h3>Try it out</h3>
<p>In Haskell, the <i>binary function</i> is called <code>mappend</code> and its <i>identity element</i> is <code>mempty</code></p>
<pre><code>
Prelude> import Data.Monoid
Prelude Data.Monoid> [1,2,3] `mappend` [4,5,6]
[1,2,3,4,5,6]
Prelude Data.Monoid> [1,2,3] `mappend` mempty
[1,2,3]
Prelude Data.Monoid> mempty `mappend` [1,2,3]
[1,2,3]
</code></pre>
<p>As you can see above, lists are Monoids.</p>
<aside class="notes">Haskell is heavily influenced by mathematics. Monoids, Functors, and Monads all have roots in Category Theory.</aside>
</section>
<section>
<h3>Quick Reminder</h3>
<p>If you have a type <code>a</code> and a function <code>a -> b</code></p>
<p>then you can get <code>b</code>.</p>
<pre><code class="haskell">
Prelude> odd 3
True
</code></pre>
<aside class="notes">Haskell's type system is simply about puzzle-fitting. If the types match, the function will evaluate.</aside>
</section>
<section>
<h3><code>Maybe</code> Continued</h3>
<p>If we instead have <code>m a</code> and a function <code>a -> m b</code></p>
<p>How would we get <code>m b</code>?</p>
<br>
<div class="fragment roll-in">
<p>For example, given <code>Maybe a</code> and <code>a -> Maybe b</code></p>
<p>How would we get <code>Maybe b</code>?</p>
</div>
<br>
<div class="fragment roll-in">
<p>The answer we desire looks like this</p>
<pre><code class="haskell">
(>>=) :: (Monad m) => m a -> (a -> m b) -> m b
</code></pre>
<p><code>>>=</code> is called a <i>bind</i></p>
</div>
<aside class="notes">Think of bind as removing a context from an input to pass it though a function.</aside>
</section>
<section>
<h3>Necessity of <i>bind</i></h3>
<p>Let's say we had the function <code>f</code> below</p>
<pre><code class="haskell">
f :: Int -> Maybe Int
f n = Just (n+1)
</code></pre>
<pre><code class="haskell">
Prelude> f 1
Just 2
</code></pre>
<br>
<div class="fragment roll-in">
<p>What if we wanted to pass in <code>Just 1</code>?</p>
<pre><code class="haskell">
Prelude> f (Just 1)
ERROR!
</code></pre>
</div>
<aside class="notes">We can use bind to temporarily remove contexts.</aside>
</section>
<section>
<h2><i>bind</i> <code>(>>=)</code></h2>
<pre><code class="haskell">
Prelude> :t (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b
</code></pre>
<pre><code class="haskell">
Prelude> Just 1 >>= f
Just 2
</code></pre>
<aside class="notes">If you squint, the bind function looks like we're stuffing a context into a function.</aside>
</section>
<section>
<h2>The <code>Monad</code> Typeclass</h2>
<p>A Monad is Typeclass with some extra rules.</p>
<br>
<p>The two important functions: <code>return</code> and <code>>>=</code></p>
<pre><code class="haskell">
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
</code></pre>
<aside class="notes">Bind is actually super important. It is one of the necessary functions for the Monad Typeclass.</aside>
</section>
<section>
<h3>The <code>>></code> function</h3>
<p>This function simply glues together Monads.</p>
<pre><code class="haskell">
Prelude> print "foo" >>= \_ -> print "bar"
"foo"
"bar"
</code></pre>
<pre><code class="haskell">
Prelude> print "foo" >> print "bar"
"foo"
"bar"
</code></pre>
<br>
<small>The <code>do</code> notation is basically an interweaving of <code>>></code></small>
<pre><code class="haskell">
main = do
print "foo"
print "bar"
</code></pre>
<aside class="notes">All haskell code can be written without the do-notation.</aside>
</section>
<section data-state="soothe">
<h3>Minimize <code>do</code> usage</h3>
<p>Write this piece of code without using <code>do</code></p>
<pre><code class="haskell">
main = do
putStrLn "Enter name:"
name <- getLine
putStrLn ("Hi " ++ name)
</code></pre>
<br>
<p class="fragment roll-in">Try it out by yourself</p>
<br>
<pre class="fragment roll-in"><code>
main = putStrLn "Enter name:" >> getLine >>= putStrLn.("Hi " ++)
</code></pre>
<aside class="notes">Isn't that just surprisingly elegant?</aside>
</section>
<section>
<h2>Monad Laws</h2>
<p>For something to truly be a Monad,</p>
<p>it must also obey the following laws: <a href="http://www.haskell.org/haskellwiki/Monad_Laws" target="_blank">*</a></p>
<br>
<ul>
<li>
<p>Left Identity</p>
<small><code>return a >>= f</code> ≡ <code>f a</code></small>
</li>
<li>
<p>Right Identity</p>
<small><code>m >>= return</code> ≡ <code>m</code></small>
</li>
<li>
<p>Associativity</p>
<small><code>(m >>= f) >>= g</code> ≡ <code>m >>= (\x -> f x >>= g)</code></small>
</li>
</ul>
<br>
<br>
<p class="fragment roll-in"><small>These laws are somewhat similar to those of Monoids.</small></p>
<aside class="notes">Just like for Functors, Monads have their own laws.</aside>
</section>
<section>
<h2>Monad Laws</h2>
<p>Here's another way to write the three laws</p>
<table style="width:100%">
<tr>
<td style="width:48%">
<pre><code class="haskell">
do
x' <- return x
f x'
</code></pre>
</td>
<td style="width:4%">≡</td>
<td style="width:48%">
<pre><code class="haskell">
do
f x
</code></pre>
</td>
</tr>
<tr>
<td style="width:48%">
<pre><code class="haskell">
do
x <- m
return x
</code></pre>
</td>
<td style="width:4%">≡</td>
<td style="width:48%">
<pre><code class="haskell">
do
m
</code></pre>
</td>
</tr>
<tr>
<td style="width:48%">
<pre><code class="haskell">
do
y <- do
x <- m
f x
g y
</code></pre>
</td>
<td style="width:4%">≡</td>
<td style="width:48%">
<pre><code class="haskell">
do
x <- m
y <- f x
g y
</code></pre>
</td>
</tr>
</table>
<aside class="notes">This is just a translation of the Monad laws.</aside>
</section>
<section>
<h3>It's based off the Kleisli composition operator <a href="http://www.haskell.org/haskellwiki/Monad_Laws" target="_blank">*</a></h3>
<small>(This is for your own curiosity.)</small>
<pre><code class="haskell">
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
</code></pre>
<br>
<ul>
<li><p>Left Identity</p>
<small><code>return >=> g</code> ≡ <code>g</code></small>
</li>
<li><p>Right Identity</p>
<small><code>f >=> return</code> ≡ <code>f</code></small></li>
<li><p>Associativity</p>
<small><code>(f >=> g) >=> h</code> ≡ <code>f >=> (g >=> h)</code></small></li>
</ul>
<aside class="notes">Now the laws truly response their names.</aside>
</section>
<section>
<h3>Why such specific rules?</h3>
<p>These Monad laws form a mathematical <a href="http://en.wikipedia.org/wiki/Category_theory" target="_blank">category</a>.</p>
<br>
<div class="fragment roll-in">
<q>A <b>monad</b> in X is just a <b>monoid</b> in the category of <b>endofunctors</b> of X, with product × replaced by <b>composition of endofunctors</b> and <b>unit</b> set by the <b>identity endofunctor</b>.</q>
<p><a href="http://books.google.com/books?id=MXboNPdTv7QC&pg=PA138&dq=%22a+monad+in+X+is+just+a%22+mac+lane&hl=en&sa=X&ei=f2c9UZeaFuam2gWnpoDIDw&ved=0CDAQ6AEwAA#v=onepage&q=%22a%20monad%20in%20X%20is%20just%20a%22%20mac%20lane&f=false" target="_blank">-- Mac Lane</a></p>
</div>
<br>
<div class="fragment roll-in">
<q><b>Monads</b> are like <b>burritos</b></q>
<p><a href="http://blog.plover.com/prog/burritos.html" target="_blank">-- Mark Dominus</a></p>
</div>
<aside class="notes">Add this tutorial to the collection of Monad tutorials.</aside>
</section>
<section>
<h1>Homework</h1>
<h2></h2>
<ol>
<li>Fill out this <a href="https://docs.google.com/forms/d/18jRKwcSqo1fQxt5rwojAYFxdXR5jTO2l3zGd33RvhoQ/viewform" target="_blank">week's form</a></li>
<li>Think about final project ideas.</li>
<small>
<ul>
<li>Building a SuDoKu solver</li>
<li>Building a personal finance software</li>
<li>Adding a library to Hackage</li>
<li>Using Haskell syntax as formal documentation</li>
<li>...</li>
<li>etc.</li>
</ul>
</small>
</ol>
</section>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
mouseWheel: true,
rollingLinks: false,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.8.3.min.js"><\/script>')</script>
<script src="js/vendor/bootstrap.min.js"></script>
<script src="js/main.js"></script>
<script>
Reveal.addEventListener( 'slidechanged', function( event ) {
// event.previousSlide, event.currentSlide, event.indexh, event.indexv
var notes = event.currentSlide.querySelector(".notes");
if(notes) {
console.info("---");
console.info(notes.innerHTML.replace(/\n\s+/g,'\n'));
}
} );
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-29747714-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>