-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWeek06Live.hs
222 lines (168 loc) · 5.19 KB
/
Week06Live.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
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
module Week06Live where
-- REMINDER: Class Test:
-- Wednesday 30th October 12:00 noon ---> Thursday 31st October 12:00 noon
-- Test will be via MyPlace
-- Test is worth 50% and marked out of 50
-- WEEK 06 : Simulating Side Effects
-- f :: Int -> Int
-- f 0 (today) == f 0 (tomorrow)
-- f :: Int -> Effect Int
-- int f(int i)
--
-- - read the clock in the computer
-- - ask the user for input
-- - post cat picture to your favourite social network (Myspace)
-- - Launch the nuclear weapons
-- Week 06 : Simulating Side Effects
-- Week 07 : Common interface
-- Week 08 : Real I/O and side effects with the common interface
-- Simulate exceptions
data Tree a
= Leaf a
| Node (Tree a) (Tree a)
deriving Show
find :: Eq a => a -> Tree (a, b) -> Maybe b
find k (Leaf (k', v))
| k == k' = Just v
| otherwise = Nothing
find k (Node l r) = case find k l of
Just v -> Just v
Nothing -> find k r
andThen :: Maybe a -> (a -> Maybe b) -> Maybe b
andThen Nothing k = Nothing
andThen (Just v) k = k v
failure :: Maybe a
failure = Nothing
find2 :: (Eq k1, Eq k2)
=> k1 -> k2
-> Tree (k1, Tree (k2, a)) -> Maybe a
find2 k1 k2 t =
find k1 t `andThen` \ t2 -> find k2 t2
-- Tree t1 = find(k1, t);
-- return find(k2, t1)
returnOk :: a -> Maybe a
returnOk x = Just x
findAll :: Eq k => Tree (k, v) -> [k] -> Maybe [v]
findAll dictionary [] = Just []
findAll dictionary (k:ks) =
find k dictionary `andThen` \ v ->
findAll dictionary ks `andThen` \ vs ->
returnOk (v : vs)
-- State
-- int i = 0;
--
-- i = i + 1;
type State s a = s -> (a, s)
andThenState :: State s a
-> (a -> State s b)
-> State s b
andThenState c k initial =
let (a, intermediate) = c initial in
k a intermediate
returnState :: a -> State s a
returnState v s = (v, s)
getState :: State s s
getState s = (s, s)
putState :: s -> State s ()
putState new old = ((), new)
numberTree :: Tree a -> State Int (Tree (a, Int))
numberTree (Leaf a) =
getState `andThenState` \ i ->
putState (i + 1) `andThenState` \ _ ->
returnState (Leaf (a, i))
numberTree (Node l r) =
numberTree l `andThenState` \ numbered_l ->
numberTree r `andThenState` \ numbered_r ->
returnState (Node numbered_l numbered_r)
-- let (numbered_l, i1) = numberTree l i0
-- (numbered_r, i2) = numberTree r i1
-- in (Node numbered_l numbered_r, i2)
example = Node (Node (Leaf 'a') (Leaf 'b')) (Leaf 'c')
-- andThen :: Maybe a -> (a -> Maybe b) -> Maybe b
-- andThenState :: State s a -> (a -> State s b) -> State s b
-- c `andThen` k ~=~ A x = c;
-- k[x]
--
-- Choice a -> Choice a -> Choice a
-- andThenChoice :: Choice a -> (a -> Choice b) -> Choice b
-- Printing
type Logging log a = (log, a)
andThenLogging
:: Semigroup log
=> Logging log a -> (a -> Logging log b) -> Logging log b
andThenLogging (output1, a) k =
let (output2, b) = k a in
(output1 <> output2, b)
returnLogging :: Monoid log => a -> Logging log a
returnLogging a = (mempty, a)
logging :: String -> Logging [String] ()
logging str = ([str], ())
printTree :: Show a => Tree a -> Logging [String] (Tree a)
printTree (Leaf a) =
logging ("Visiting " ++ show a) `andThenLogging` \ _ ->
returnLogging (Leaf a)
printTree (Node l r) =
printTree l `andThenLogging` \ l' ->
printTree r `andThenLogging` \ r' ->
returnLogging (Node l' r')
-- I/O Processes
data Process a
= End a
| Input (String -> Process a)
| Output String (Process a)
{-
Input
|
/----\----- .....
/ \
"Alice" "Bob"
| |
Output "Hello Alice" Output "Hello Bob"
| |
End () End ()
-}
greeter :: Process ()
greeter = Input (\name -> Output ("Hello " ++ name) (End ()))
{- name <- input;
print ("Hello " ++ name);
return ()
-}
andThenProcess :: Process a -> (a -> Process b) -> Process b
andThenProcess (End a) k = k a
andThenProcess (Input react) k
= Input (\ str -> react str `andThenProcess` k)
andThenProcess (Output msg p) k
= Output msg (p `andThenProcess` k)
runProcess :: [String] -> Process a -> Logging [String] a
runProcess inputs (End a) = returnLogging a
runProcess (i : inputs) (Input react)
= runProcess inputs (react i)
runProcess inputs (Output msg p)
= logging msg `andThenLogging` \ _ ->
runProcess inputs p
input :: Process String
input = Input (\ str -> End str)
output :: String -> Process ()
output msg = Output msg (End ())
returnProcess :: a -> Process a
returnProcess = End
greeter2 :: Process ()
greeter2 =
input `andThenProcess` \ name ->
output ("Hello " ++ name) `andThenProcess` \ _ ->
returnProcess ()
greeter3 :: Process ()
greeter3 =
input `andThenProcess` \name ->
if name == "Bob" then
(output "That is a silly name" `andThenProcess` \ _ ->
greeter3)
else
(output ("Hello " ++ name) `andThenProcess` \ _ ->
returnProcess ())
realRunProcess :: Process a -> IO a
realRunProcess (End a) = return a
realRunProcess (Input react) =
do input <- getLine; realRunProcess (react input)
realRunProcess (Output msg p) =
do putStrLn msg; realRunProcess p