-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmonads.html
More file actions
736 lines (581 loc) · 22.5 KB
/
monads.html
File metadata and controls
736 lines (581 loc) · 22.5 KB
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
<HTML>
<HEAD>
<TITLE>A Monad Tutorial</TITLE>
</HEAD>
<BODY>
<P><DIV ALIGN="CENTER"><H1>Brian's Monad Tutorial</H1></DIV></P>
<P><DIV ALIGN="CENTER"><H2>The Problems With Monad Tutorials</H2></DIV></P>
<P><OL><LI>Bad metaphors- "A monad is like a burrito wrapped in a space
suit- it protects you from the toxic waste, and you can just reach in
and grab all the apples you want." Solution: no metaphors, just
code.</LI>
<LI>Excess of category theory- "A monad is just a monoid in the category
of endofunctors, what's the problem?" Solution: <B>NO CATEGORY
THEORY!</B></LI>
<LI>A need for "hands on" experience- see <A HREF="https://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/">Abstract Intuition and the Monad Tutoral Fallacy</A>.
Solution: stay concrete, lots of code, homework exercises. For our
concrete examples, I borrow from the blog post
<A HREF="http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html">You Could Have Invented Monads! (And Maybe You Already Have.)</A>.
</LI>
<LI>The "four faces" of monads: <OL><LI>Where they come from (category
theory)</LI><LI>How they are implemented</LI><LI>How they are
used</LI><LI>How they are thought about or designed with.</LI></OL>
Solution: address each face separately, except the category theory which
we don't need to address.</LI>
<LI>Unfamiliarity with the concept of functions as data- many Monads are
based around treating functions as data, and this is rare in other
languages, and thus unfamiliar to most programmers.
</OL></P>
<P><DIV ALIGN="CENTER"><H2>Motivating Example #1</H2></DIV></P>
<P>We have three functions, f, g, and h, so f calls g, and g calls h:</P>
<P><CODE><PRE>
f :: ... -> something
f ... =
...
let r = g ... in
...
something
g :: ... -> somethingElse
g ... =
...
let r = h ... in
...
somethingElse
h :: ... -> whatever
h ... =
...
</PRE></CODE></P>
<P>Now h needs some information (of type blah) from where f is called. So
now we need to thread that information through the whole calling stack:</P>
<P><CODE><PRE>
f :: ... -> blah -> something
f ... blah =
...
let r = g ... blah in
...
something
g :: ... -> blah -> somethingElse
g ... blah =
...
let r = h ... blah in
...
somethingElse
h :: ... -> blah -> whatever
h ... blah =
...
</PRE></CODE></P>
<P><DIV ALIGN="CENTER"><H2>The Reader Type</H2></DIV></P>
<P>I start by introducing a type:</P>
<P><CODE><PRE>
type Reader s a = s -> a
</PRE></CODE></P>
<P>Note that the <CODE>type</CODE> declaration is Haskell is just like
the <CODE>typedef</CODE> declaration in C/C++. All it does is introduce
a type macro- I can always replace the type on the left with the type on
the right. Indeed, I often will, to show what's going on under the hood
(so to speak). So now we can clean up our types a little:</P>
<P><CODE><PRE>
f :: ... -> Reader blah something
...
g :: ... -> Reader blah somethingElse
...
h :: ... -> Reader blah whatever
...
</PRE></CODE></P>
<P>Note: in Haskell, functions are first class, which means it makes
perfect sense to return a function as a value. Also, Haskell has
partial function application- which means there is no difference between
a function returning a function, and having the function just take that
many more arguments. In other words:</P>
<P><CODE><PRE>
-- A function that takes two arguments and returns some value
f foo bar = ...
</PRE></CODE></P>
<P>and:</P>
<P><CODE><PRE>
-- A function that takes one argument and returns a function
-- that takes one argument and returns some value
f foo = (\bar -> ...)
</PRE></CODE></P>
<P>are treate the same. It's even OK to do:</P>
<P><CODE><PRE>
-- A function that takes one argument, does some stuff, and then
-- returns a function that takes one argument and returns some value
f foo =
...
(\bar -> ...)
</PRE></CODE></P>
<P>These three different bits of code are all effectively
equivalent.</P>
<P>We've improved the situation, but not much. What we need is a
special "reader let", which automatically appends the blah value. This
is hard, as it requires help from the compiler. We'll get back to that.
However, there is something we can do now. A little know stunt in
functional programming is that if we have:</P>
<P><CODE><PRE>
-- A let expression.
let x = foo in bar
</PRE></CODE></P>
<P>we can replace it with:</P>
<P><CODE><PRE>
-- a function expression applied to an argument.
(\x -> bar) foo
</PRE></CODE></P>
<P>And the code behaves the same. In fact, this is how let is defined
in theory. Apply is much easier than let to replace in code. So,
instead of a special let, let's do a special apply. Flip the order of
the args, so we can do:</P>
<P><CODE><PRE>
apply foo (\x -> bar)
</PRE></CODE></P>
<P>We want foo to have the type <CODE>Reader s a</CODE>, and we want the
whole thing to have a return type of <CODE>Reader s b</CODE>, so we can
go:</P>
<P><CODE><PRE>
f :: ... -> Reader blah something
f ... =
...
apply (g ...)
(\r ->
...
something)
</PRE></CODE></P>
<P>and have apply do the right thing. And we can make an initial
cut at the code:</P>
<P><CODE><PRE>
apply :: (Reader r a) -> (a -> b) -> Reader r b
-- apply :: (r -> a) -> (a -> b) -> r -> b
apply f g s = g (f s)
</PRE></CODE></P>
<P>This type signature looks familiar. Where have I seen it before?</P>
<P><CODE><PRE>
fmap :: Functor f => (a -> b) -> f a -> f b
</PRE></CODE></P>
<P>But there is a problem: what happens if we want to call two functions
and thread the variable into both?</P>
<P><CODE><PRE>
f :: ... -> Reader blah something
f ... =
...
apply (g ...)
(\r ->
...
apply (g2 ...)
(\r2 ->
...
something))
</PRE></CODE></P>
<P>I can use fmap for the original <CODE>apply</CODE>, I want
<CODE>apply</CODE> to have the type: <CODE>(Reader r a) -> (a ->
Reader r b) -> Reader r b</CODE>. The <CODE>apply</CODE> function
then becomes:</P>
<P><CODE><PRE>
apply :: Reader r a -> (a -> Reader r b) -> Reader r b
-- apply :: (r -> a) -> (a -> r > b) -> r -> b
apply f g s = g (f s) s
</PRE></CODE></P>
<P>But this causes another problem:</P>
<P><CODE><PRE>
f :: ... -> Reader blah something
f ... =
...
apply (g ...)
(\r ->
...
apply (g2 ...)
(\r2 ->
...
something)) -- Type error right here!
</PRE></CODE></P>
<P>The <CODE>something</CODE> expression has type
<CODE>something</CODE>, but we want it to have type <CODE>Reader blah
something</CODE>. We could user <CODE>fmap</CODE>, or we could just
provide a fixup function:</P>
<P><CODE><PRE>
fixup :: a -> Reader r a
-- fixup :: a -> r -> a
fixup x _ = x
</PRE></CODE></P>
<P>Now we can go:</P>
<P><CODE><PRE>
f :: ... -> Reader blah something
f ... =
...
apply (g ...)
(\r ->
...
apply (g2 ...)
(\r2 ->
...
fixup something))
</PRE></CODE></P>
<P>I will, at this point, just rename <CODE>apply</CODE> to be the operator
<CODE>>>=</CODE>, and the <CODE>fixup</CODE> function to
<CODE>return</CODE>. This should start looking familiar:</P>
<P><CODE><PRE>
f :: ... -> Reader blah something
f ... =
...
(g ...) >>=
(\r ->
...
(g2 ...) >>=
(\r2 ->
...
return something))
</PRE></CODE></P>
<P>Two last things we need, if we want to have Reader be an abstract
data type (i.e., we don't let people see it's just a function under the
hood). We need some way to call f with our initial blah. We need some
function of type:</P>
<P><CODE><PRE>
runReader :: Reader r a -> r -> a
</PRE></CODE></P>
<P>And, we need some way to access the read variable down where we need
it (down in <CODE>h</CODE>). If we had a function:</P>
<P><CODE><PRE>
ask :: Reader r r
</PRE></CODE></P>
<P>We could write <CODE>h</CODE> like:</P>
<P><CODE><PRE>
h :: ... -> Reader blah whatever
h ... = ask (\blah -> ...)
</PRE></CODE></P>
<P><B>HOMEWORK:</B> Implement the <CODE>runReader</CODE> and
<CODE>ask</CODE> functions.</P>
<P><DIV ALIGN="CENTER"><B>Performance</B></DIV></P>
<P>One of the concerns people have with writing this abstract code is
the loss of performance- we're introducing a lot of unnecessary closures
and lambda expressions. Rest assured, the ghc compiler optimizes all of
this, and recovers code equalivalent to our original hand-threaded
code. The ghc compiler knows about the let/apply equivalence (so it's
not so unknown as I lead you to believe), and will happily convert back
from our apply the (more efficient) let representation. Also, it knows
about returning functions vr.s just taking more arguments, and will
happily swap that back. We can write our nice, generic code, and
Haskell will apply the obvious transformations to turn it into efficient
code.</P>
<P><DIV ALIGN="CENTER"><H2>Motivating Example #2</H2></DIV></P>
<P>Same as above, except now h doesn't just take a blah, it returns an
updated blah (common with immutable data):
<P><CODE><PRE>
f :: ... -> blah -> (something, blah)
f ... blah =
...
let (r, blah') = g ... blah in
...
(something, blah')
g :: ... -> blah -> (somethingElse, blah)
g ... blah =
...
let (r, blah') = h ... blah in
...
(somethingElse, blah')
h :: ... -> blah -> (whatever, blah)
h ... blah =
...
</PRE></CODE></P>
<P><DIV ALIGN="CENTER"><H2>The State Type</H2></DIV></P>
<P>We pull the same trick, just a little more complicated. We
define (spelling out the types):</P>
<P><CODE><PRE>
type State s a = s -> (a, s)
(>>=) :: State s a -> (a -> State s b) -> State s b
-- (>>=) :: (s -> (a, s)) -> (a -> s -> (b, s)) -> s -> (b, s)
f >>= g =
-- f :: State s a == s -> (a, s)
-- g :: a -> State s b == a -> s -> (b, s)
-- we want to produce a State s b == s -> (b, s)
\s ->
let (x, s') = f s
g x s'
return :: a -> State s a
-- return :: a -> s -> (a, s)
return x s = (x, s)
</PRE></CODE></P>
<P>So now I can do:</P>
<P><CODE><PRE>
f :: ... -> State blah something
f ... =
...
(g ...) >>=
(\r ->
...
return something)
g :: ... -> State blah somethingElse
g ... blah =
...
(h ...) >>=
(\r ->
...
somethingElse)
</PRE></CODE></P>
<P><B>HOMEWORK:</B> To make <CODE>State</CODE> an abstract type (like we
made <CODE>Reader</CODE>), we need three new functions:
<P><CODE><PRE>
runState :: State s a -> s -> (a, s)
get :: State s s
put :: s -> State s ()
</PRE></CODE></P>
<P>Write these functions. With these funtions, how would you write
<CODE>h</CODE> (approximately)?</P>
<P><DIV ALIGN="CENTER"><H2>The Monad Typeclass</H2></DIV></P>
<P>Now we have a problem. We have two definitions (with very similar
types) of the <CODE>>>=</CODE> operator:</P>
<P><CODE><PRE>
(>>=) :: Reader r a -> (a -> Reader r b) -> Reader r b
(>>=) :: State s a -> (a -> State s b) -> State s b
</PRE></CODE></P>
<P>And two copies of the <CODE>return</CODE> function:</P>
<P><CODE><PRE>
return :: a -> Reader r a
return :: a -> State s a
</PRE></CODE></P>
<P>This causes Haskell to be unhappy. The solution is to introduce a
type class:</P>
<P><CODE><PRE>
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
</PRE></CODE></P>
<P>Our implementation of <CODE>Reader</CODE> would then be:</P>
<P><CODE><PRE>
type Reader r a = r -> a
instance Monad (Reader r) where
-- We replace the m with (Reader r), so
-- m a -> (a -> m b) -> m b
-- (Reader r) a -> (a -> (Reader r) b) -> (Reader r) b
-- Reader r a -> (a -> Reader r b) -> Reader r b
f >>= g = \r -> g (f r) r
-- a -> m a
-- a -> (Reader r) a
-- a -> Reader r a
return x = \_ -> x
</PRE></CODE></P>
<P>And <CODE>State</CODE> would be:</P>
<P><CODE><PRE>
type State s a = s -> (a, s)
instance Monad (State s) where
f >> g = \s -> let (x, s') = f s in g x s'
return x = \s -> (x, s)
</PRE></CODE></P>
<P><B>HOMEWORK:</B> Is it possible to write the <CODE>fmap</CODE>
function using only <CODE>>>=</CODE> and <CODE>return</CODE>?
What does this mean about the relationship between <CODE>Functor</CODE>
and <CODE>Monad</CODE>?</P>
<P><B>HOMEWORK:</B> Read up on the <CODE>Applicative</CODE> type class-
can it be implemented using only <CODE>>>=</CODE> and
<CODE>return</CODE>? What does that say about the relationship between
<CODE>Applicative</CODE> and <CODE>Monad</CODE>?</P>
<P><B>HOMEWORK:</B> The <CODE>Reader</CODE> type is "half of" the
<CODE>State</CODE> type. Can you implement the "other half"- the
<CODE>Writer</CODE> type? How would you deal with the state not being
written, or being written multiple times?</P>
<P><DIV ALIGN="CENTER"><H2>A Digression</H2></DIV></P>
<P>Consider the following code for a moment:</P>
<P><CODE><PRE>
instance Monad [] where
[] >>= _ = []
(x : xs) >>= f = (f x) ++ (xs >>= f)
return x = [ x ]
</PRE></CODE></P>
<P>So lists are monads.</P>
<P><B>HOMEWORK:</B> Can you write similar definitions for
<CODE>Maybe</CODE>? How about <CODE>Either String</CODE>?</P>
<P><DIV ALIGN="CENTER"><H2>Side effects and the IO Type</H2></DIV></P>
<P>Problem: the only way to guarantee that expression A is evaluated
before expression B is to have expression B depend upon the value
produced by A. But side-effecting computations have ordering dependency
instead of data dependency- we want to print the <CODE>"password:
"</CODE> and <EM>then</EM> read the input line. Many side-effecting
expressions do not even have a sensible data output- OK,
<CODE>getLine</CODE> returns a <CODE>String</CODE>, but what does
<CODE>putStr</CODE> return?</P>
<P>Solution: we model the ordering dependency as a "fake" data
dependency. We introduce a special value, of type <CODE>World#</CODE>
which represents "the state of the outside world". So now:</P>
<P><CODE><PRE>
putStr :: String -> World# -> ((), World#)
getLine :: World# -> (String, World#)
</PRE></CODE></P>
<P>All side-effecting computations now return at least one peice of
"meaningful" data- the updated state of the world.</P>
<P>Problem: Now we need to thread the <CODE>World#</CODE> value through
your code. If only there was a way to do that...</P>
<P><CODE><PRE>
type IO a = State World# a
putStr :: String -> IO ()
getLine :: IO String
</PRE></CODE></P>
<P><B>HOMEWORK:</B> Consider the following Haskell program:</P>
<P><CODE><PRE>
main = getLine >>=
(\line1 ->
getLine >>=
(\line2 ->
putStrLn line1))
</PRE></CODE></P>
<P>If I run it, and give it the input:<PRE>
hello
world
</PRE> (note, as two separate lines), what does the program output, and
why? What does a value of the type <CODE>IO String</CODE> represent?</P>
<P><DIV ALIGN="CENTER"><H2>The Do Notation</H2></DIV></P>
<P>Problem: continually have to introduce new lambda expressions to bind
the results of monadic computations is a pain. It sure would be nice to
have a "monadic let". And Haskell gives us that with the Do
notation:</P>
<P><CODE><PRE>
main = do
line1 <- getLine
line2 <- getLine
putStrLn line1
</PRE></CODE></P>
<P>The do notation "desugars" to a sequence of >>= calls and
lambda expressions. So the code:</P>
<P><CODE><PRE>
do
var <- expr
...
</PRE></CODE></P>
<P>becomes:</P>
<P><CODE><PRE>
expr >>=
(\var ->
do
...)
</PRE></CODE></P>
<P>Likewise,</P>
<P><CODE><PRE>
do
expr
...
</PRE></CODE></P>
<P>becomes:</P>
<P><CODE><PRE>
expr >>=
(\ _ ->
do
...)
</PRE></CODE></P>
<P>And:</P>
<P><CODE><PRE>
do
let var = expr
...
</PRE></CODE></P>
<P>(Note the lack of an "in" there!) becomes: </P>
<P><CODE><PRE>
let var = expr in
do
...
</PRE></CODE></P>
<P>The last statement in a do block is a pure expression which is the
final result. This is normally a <CODE>return</CODE> call, but it
can be a control flow expression that itself contains do blocks:</P>
<P><CODE><PRE>
main = do
putStrLn "What is your guess?"
guess <- getLine
-- Now, this is the last statement in this do block
if ((read guess) == 3::Int) then
-- Which contains two do blocks inside
do
putStrLn "Correct!"
return ()
else
do
putStrLn "Wrong!"
main
</PRE></CODE></P>
<P><B>HOMEWORK:</B> Rewrite the guessing game example above to
explicitly thread the <CODE>World#</CODE> value around (i.e. desugar the
do blocks, inline the definition of <CODE>>>=</CODE>). How does
the last call to main work?</P>
<P><DIV ALIGN="CENTER"><H2>Monad Transformers</H2></DIV></P>
<P>So, <CODE>IO</CODE> is "impure"- but a lot of the monads we've seen
are pure- <CODE>State</CODE>, <CODE>Reader</CODE>, <CODE>Writer</CODE>,
<CODE>Maybe</CODE>, <CODE>Either</CODE>, <CODE>List</CODE>, and so on.
They do mix fairly promiscuously, and cleanly, with other monads. Can
we express that?</P>
<P><CODE><PRE>
type StateT s m a = s -> m (a, s)
instance Monad m => Monad StateT s m where
f >>= g = \s -> do
(x, s') <- f s
g x s'
return x s = return (x, s)
</PRE></CODE></P>
<P>This is the "State Transformer"- it stacks on top of another monad
and threads a state variable through.</P>
<P><B>HOMEWORK:</B> Write the transformer versions of ReaderT, WriterT,
and MaybeT.</P>
<P><B>HOMEWORK:</B> Look up the source code to the standard StateT,
ReaderT, WriterT, and MaybeT implementations on hackage. How does your
code differ from theres? Why did they design their code the way they
did?</P>
<P><DIV ALIGN="CENTER"><H2>Monad Laws</H2></DIV></P>
<P>The three monad laws:</P>
<P><TABLE CELLPADDING=10><TR><TD>1</TD><TD>Left Identity:</TD><TD><CODE>(return x) >>=
f</CODE></TD><TD><=></TD><TD><CODE>f x</CODE></TD></TR>
<TR><TD>2</TD><TD>Right Identity:</TD><TD><CODE>m >>=
return</CODE></TD><TD><=></TD><TD><CODE>m</CODE></TD></TR>
<TR><TD>3</TD><TD>Associativity:</TD><TD><CODE>(m >>= f) >>=
g</CODE></TD><TD><=></TD><TD><CODE>m>>= (\x -> f x
>>= g)</CODE></TD></TR></TABLE></P>
<P>The english translation:</P>
<P>Rules 1 and 2 mean that the only thing return can do is inject a
value into the monad, it can not perform any other action. Rule 3 says
that the only important thing is the ordering of the bindings. One can
always pull subsequences out into their own values or functions.</P>
<P><EM>In other words, the monad laws just mean that the monad
implementation has to behave sensibly.</EM></P>
<P>The monad laws also only hold in the lack of bottom (expressions that
throw exceptions, go into infinite loops, exit the program, etc.).
Every so often a debate emerges on the haskell mailing lists over wether
the monad laws are actually followed or not. Rather than participating
in such debates, I recommend reading <A
HREF="http://www.cs.ox.ac.uk/jeremy.gibbons/publications/fast+loose.pdf">Fast
and Loose Reasoning is Morally Correct</A>, by Danielsson, Hughes,
Jansso, and Gibbons. Or, better yet, going outside to get some fresh
air and sunshine.</P>
<P><B>HOMEWORK:</B> If you really insist, prove that this implementation
of <CODE>return</CODE> for the List monad is incorrect:</P>
<P><CODE><PRE>
return x = [ x, x ]
</PRE></CODE></P>
<P><B>HOMEWORK:</B> If you really enjoy this sort of thing, go back and
prove all of our implementations up until this point do, in fact, follow
the monad laws.</P>
<P><DIV ALIGN="CENTER"><H2>Designing with Monads</H2></DIV></P>
<P>In terms of design, a monad is:
<UL><LI>An action</LI>
<LI>Returning a value</LI>
<LI>In a domain that has special abilities</LI></UL></P>
<P>There are the operations all monads support (<CODE>>>=</CODE>,
<CODE>return</CODE>), and operations only supported by specific monads
(<CODE>get</CODE>, <CODE>put</CODE>, <CODE>putStrLn</CODE>, etc.). The
"special abilities" of the monad are in these specific operations.</P>
<P><TABLE BORDER=1 CELLPADDING=5>
<TR><TD>Monad</TD><TD>Special abilities</TD></TR>
<TR><TD>IO</TD><TD>Can do I/O and/or side-effects</TD></TR>
<TR><TD>State</TD><TD>Has read/write access to a "global variable"</TD></TR>
<TR><TD>Reader</TD><TD>Has read-only access to a "global variable"</TD></TR>
<TR><TD>Writer</TD><TD>Has write-only access to a "global variable"</TD></TR>
<TR><TD>Maybe</TD><TD>May not produce an output</TD></TR>
<TR><TD>List</TD><TD>May produce multiple outputs</TD></TR>
</TABLE></P>
<P>In addition, you can define your own monads, either directly, or
using one of the standard monads. For example, you might do:</P>
<P><CODE><PRE> type WithDBConn a = Reader DB.Conn a</PRE></CODE></P>
<P>To define a monad whose domain is "has a connection to the database".
This is one of the main ways to structure a Haskell program on the large
scale- using monads to define the major "domains" of a program (code
that needs a connection to the database, etc.).</P>
<P><B>HOMEWORK:</B> Think of a program you might want to write. What
are the major domains that program will want? How would you implement
them?</P>
<P><DIV ALIGN="CENTER"><H2>The last bit of homework</H2></DIV></P>
<P><B>HOMEWORK:</B> Go write Haskell code using monads!</P>
<P><DIV ALIGN="CENTER"><H1>FINI</H1></DIV></P>
</BODY>