This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Lec12Live.hs
133 lines (104 loc) · 2.8 KB
/
Lec12Live.hs
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
module Lec12Live where
{- LECTURE 12 : MONADS -}
{- 1. Recap some abstractions -}
f :: Int -> IO Int
f x = do print "running f (FIXME: remove this code before submitting)"; return (x+1)
class MyFunctor f where
myfmap :: (a -> b) -> f a -> f b
class MyFunctor f => MyApplicative f where
mypure :: a -> f a
(<**>) :: f (a -> b) -> f a -> f b
data Printing a = MkP [String] a
deriving Show
instance Functor Printing where
fmap f (MkP outputs a) = MkP outputs (f a)
instance Applicative Printing where
-- pure :: a -> Printing a
pure x = MkP [] x
-- (<*>) :: Printing (a->b) -> Printing a -> Printing b
MkP outputs1 f <*> MkP outputs2 a =
MkP (outputs1 ++ outputs2) (f a)
pr :: String -> Printing ()
pr s = MkP [s] ()
computation :: Int -> Printing Int
computation x = pure (\() -> x+1) <*> pr "called 'computation'"
{-
pr "called 'computation'" :: Printing ()
(\() -> x+1) :: () -> Int
pure (\() -> x+1) :: Printing (() -> Int)
pure (\() -> x+1) <*> pr "called 'computation'" :: Printing Int
-}
-- sequ :: Process a -> (a -> Process b) -> Process b
-- sequ = undefined
{-
class Applicative f => Monad f where
return :: a -> f a
(>>=) :: f a -> (a -> f b) -> f b
-}
instance Monad Printing where
return = pure
-- (>>=) :: Printing a -> (a -> Printing b) -> Printing b
MkP outputs1 a >>= f = MkP (outputs1++outputs2) b
where
MkP outputs2 b = f a
computation' :: Int -> Printing Int
computation' x =
do pr "called 'computation'"
pr "printing again"
return (x+1)
-- do c1 ===> c1 >>= \() -> c2
-- c2
-- do x <- c1 ===> c1 >>= \x -> c2
-- c2
{- 2. The 'Maybe' monad -}
{- data Maybe a = Nothing
| Just a
-}
{-
instance Monad Maybe where
return x = Just x
-- (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
Nothing >>= f = Nothing
Just a >== f = f a
-}
abort :: Maybe a
abort = Nothing
computation3 :: Int -> Maybe Int
computation3 x =
if x > 5 then abort else return (x+1)
data Tree a = Leaf | Node (Tree a) a (Tree a) deriving Show
checkTree :: Tree Int -> Maybe ()
checkTree Leaf = return ()
checkTree (Node l a r) =
if a < 0 then abort
else do checkTree l
checkTree r
{- 3. The 'List' monad -}
{-
instance Monad [] where
return x = [x]
-- (>>=) :: [a] -> (a -> [b]) -> [b]
as >>= f = concat (map f as)
-}
failure' :: [a]
failure' = []
triples :: [(Int,Int,Int)]
triples = do
x <- [1..20]
y <- [1..20]
z <- [1..20]
if x*x + y*y == z*z then
return (x,y,z)
else
failure'
{- 5. The 'IO' monad -}
{-
class Applicative f => Monad f where
return :: a -> f a
(>>=) :: f a -> (a -> f b) -> f b
-}
computation4 :: IO ()
computation4 =
do putStrLn "Hello, what is your name?"
name <- getLine
putStrLn ("Hello " ++ name)