-
Notifications
You must be signed in to change notification settings - Fork 0
/
DanielDubiel.hs
384 lines (368 loc) · 16.1 KB
/
DanielDubiel.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
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
-- Wymagamy, by moduł zawierał tylko bezpieczne funkcje
{-# LANGUAGE Safe #-}
-- Definiujemy moduł zawierający rozwiązanie.
-- Należy zmienić nazwę modułu na {Imie}{Nazwisko} gdzie za {Imie}
-- i {Nazwisko} należy podstawić odpowiednio swoje imię i nazwisko
-- zaczynające się wielką literą oraz bez znaków diakrytycznych.
module DanielDubiel (typecheck, eval) where
-- Importujemy moduły z definicją języka oraz typami potrzebnymi w zadaniu
import AST
import DataTypes
import qualified Data.Map as M
import Data.Maybe
-- Funkcja sprawdzająca typy
-- Dla wywołania typecheck fs vars e zakładamy, że zmienne występujące
-- w vars są już zdefiniowane i mają typ int, i oczekujemy by wyrażenia e
-- miało typ int
-- UWAGA: to nie jest jeszcze rozwiązanie; należy zmienić jej definicję.
typecheck :: [FunctionDef p] -> [Var] -> Expr p -> TypeCheckResult p
typecheck fun s expr =
case funTypeCheck fun fun (M.union (eatInput s) (eatFunc fun)) expr of
Rerror pos str -> Error pos str
Rtype TInt -> Ok
_ ->
Error (getData expr) "typeError: Program must return type int"
data ReturnType p
= Rerror p String
| Rtype Type
deriving (Eq)
funTypeCheck :: [FunctionDef p] -> [FunctionDef p] ->
M.Map Var (ReturnType p) -> Expr p -> ReturnType p
funTypeCheck constFun [] s wyr = checkTypes constFun s wyr
funTypeCheck constFun (x:xs) s wyr =
case checkTypes constFun
(M.insert (funcArg x) (Rtype (funcArgType x)) (eatFunc constFun))
(funcBody x) of
Rerror pos str -> Rerror pos str
Rtype typ
| typ == funcResType x -> funTypeCheck constFun xs s wyr
Rtype typ -> Rerror (getData (funcBody x))
("typeError: Function \"" ++ funcName x ++ "\" returns type " ++ show typ
++ ", should return " ++ show (funcResType x))
checkTypes :: [FunctionDef p] -> M.Map Var (ReturnType p) ->
Expr p -> ReturnType p
checkTypes fun s wyr =
case wyr of
EVar p str ->
fromMaybe
(Rerror p ("typeError: Variable \""
++ str ++ "\" not in scope"))
(M.lookup str s)
-------------------------------------------------------------------------------
ENum _ _ -> Rtype TInt
-------------------------------------------------------------------------------
EBool _ _ -> Rtype TBool
-------------------------------------------------------------------------------
EUnary p op a1 ->
case checkTypes fun s a1 of
Rerror pos str -> Rerror pos str
Rtype typ1 ->
case op of
UNot ->
case typ1 of
TBool -> Rtype TBool
_ -> Rerror p ("typeError: Expected expression: "
++ show op ++ " <bool>, got "
++ show op ++ " <" ++ show typ1 ++ ">")
UNeg ->
case typ1 of
TInt -> Rtype TInt
_ -> Rerror p ("typeError: Expected expression: "
++ show op ++ " <int>, got "
++ show op ++ " <" ++ show typ1 ++ ">")
-------------------------------------------------------------------------------
EBinary p op a1 a2 ->
case (checkTypes fun s a1, checkTypes fun s a2) of
(Rerror pos1 str1, _) -> Rerror pos1 str1
(_, Rerror pos2 str2) -> Rerror pos2 str2
(Rtype typ1, Rtype typ2) ->
case op of
nOp | nOp `elem` [BAnd,BOr] ->
if typ1 == TBool && typ2 == TBool
then Rtype TBool
else Rerror p ("typeError: Expected expression: <bool> '"
++ show op ++"' <bool>, got <" ++ show typ1 ++ "> '"
++ show op ++ "' <"++ show typ2 ++ ">")
nOp | nOp `elem` [BEq , BNeq , BLt , BGt , BLe , BGe] ->
if typ1 == TInt && typ2 == TInt
then Rtype TBool
else Rerror p ("typeError: Expected expression: <int> '"
++ show op ++"' <int>, got <" ++ show typ1 ++ "> '"
++ show op ++ "' <" ++ show typ2 ++ ">")
_ ->
if typ1 == TInt && typ2 == TInt
then Rtype TInt
else Rerror p ("typeError: Expected expression: <int> '"
++ show op ++"' <int>, got <" ++ show typ1 ++ "> '"
++ show op ++ "' <" ++ show typ2 ++ ">")
-------------------------------------------------------------------------------
ELet _ var a1 a2 ->
case checkTypes fun s a1 of
Rerror pos1 str -> Rerror pos1 str
typ1 -> checkTypes fun (M.insert var typ1 s) a2
-------------------------------------------------------------------------------
EIf p a1 a2 a3 ->
case (checkTypes fun s a1, checkTypes fun s a2, checkTypes fun s a3) of
(Rerror pos1 str1, _, _) -> Rerror pos1 str1
(_, Rerror pos2 str2, _) -> Rerror pos2 str2
(_, _, Rerror pos3 str3) -> Rerror pos3 str3
(Rtype TBool, Rtype t1, Rtype t2)
| t1 == t2 -> Rtype t1
(Rtype e1, _, _) | e1 /= TBool -> Rerror p
("typeError: In expression \"if e1 then e2 else e3\""
++ " e1 must have type <bool>, currently has <" ++ show e1 ++ ">")
(_, Rtype e1, Rtype e2) -> Rerror p
("typeError: In expression \"if e1 then e2 else e3\""
++ " e2 and e3 must have that same type, currently e2 has <"
++ show e1 ++ ">, e3 has <" ++ show e2 ++ ">")
-------------------------------------------------------------------------------
EFn _ var typ1 e1 ->
case checkTypes fun (M.insert var (Rtype typ1) s) e1 of
Rerror pos1 str1 -> Rerror pos1 str1
Rtype typ2 -> Rtype (TArrow typ1 typ2)
-------------------------------------------------------------------------------
EApp p e1 e2 ->
case checkTypes fun s e1 of
Rerror pos1 str1 -> Rerror pos1 str1
Rtype (TArrow typ1 typ2) ->
case checkTypes fun s e2 of
Rerror pos1 str1 -> Rerror pos1 str1
Rtype typ3 | typ3 == typ1 -> Rtype typ2
Rtype typ3 -> Rerror p
("typeError: Funcion expects type <" ++ show typ1
++ ">,got <" ++ show typ3 ++ ">")
Rtype t -> Rerror p
("typeError: Expected funcion type, got <" ++ show t ++ ">")
-------------------------------------------------------------------------------
EUnit _ -> Rtype TUnit
-------------------------------------------------------------------------------
EPair _ a1 a2 ->
case (checkTypes fun s a1, checkTypes fun s a2) of
(Rerror pos1 str1,_) -> Rerror pos1 str1
(_, Rerror pos2 str2) -> Rerror pos2 str2
(Rtype t1, Rtype t2) -> Rtype (TPair t1 t2)
-------------------------------------------------------------------------------
EFst p a1 ->
case checkTypes fun s a1 of
Rtype (TPair e1 _) -> Rtype e1
Rerror pos str -> Rerror pos str
_ -> Rerror p "typeError: fst must be applied on pair"
-------------------------------------------------------------------------------
ESnd p a1 ->
case checkTypes fun s a1 of
Rtype (TPair _ e2) -> Rtype e2
Rerror pos str -> Rerror pos str
_ -> Rerror p "typeError: snd must be applied on pair"
-------------------------------------------------------------------------------
ENil p a1 ->
case a1 of
(TList _) -> Rtype a1
_ -> Rerror p "typeError: Even empty list must be typed"
-------------------------------------------------------------------------------
ECons p a1 a2 ->
case (checkTypes fun s a1, checkTypes fun s a2) of
(Rerror pos1 str1,_) -> Rerror pos1 str1
(_,Rerror pos2 str2) -> Rerror pos2 str2
(Rtype t1, Rtype (TList t2))
| t1 == t2 -> Rtype (TList t1)
(Rtype t1, Rtype (TList t2)) -> Rerror p
("typeError: List expects type: <"
++ show t2 ++ ">, got <" ++ show t1 ++ ">")
(_, Rtype t2) -> Rerror p
("typeError: List constructor expects type: <_> :: <_ list>,"
++ " got <_> :: <" ++ show t2 ++ ">")
-------------------------------------------------------------------------------
EMatchL p a1 a2 (v1, v2, a3) ->
case (checkTypes fun s a1, checkTypes fun s a2) of
(Rerror pos1 str1,_) -> Rerror pos1 str1
(_,Rerror pos2 str2) -> Rerror pos2 str2
(Rtype (TList t),Rtype typ2) ->
case checkTypes fun (M.insert v2 (Rtype (TList t))
(M.insert v1 (Rtype t) s)) a3 of
Rerror pos3 str3 -> Rerror pos3 str3
Rtype typ3
| typ2 == typ3 -> Rtype typ2
_ -> Rerror p
("typeError: In \"match e with [] -> e1 | x1 :: x2 -> e2\""
++ " e1 and e2 must have that same type")
(Rtype t1, _) -> Rerror p ("typeError: Match expects"
++ " list, got type <" ++ show t1 ++ ">")
-- Zaminia typ wejściowy na typ wykorzystywany w trakcie typowania
eatInput :: [Var] -> M.Map Var (ReturnType p)
eatInput [] = M.empty
eatInput (x:xs) = M.insert x (Rtype TInt) (eatInput xs)
eatFunc :: [FunctionDef p] -> M.Map Var (ReturnType p)
eatFunc [] = M.empty
eatFunc (x:xs) = M.insert (funcName x)
(Rtype (TArrow (funcArgType x) (funcResType x))) (eatFunc xs)
-- Funkcja obliczająca wyrażenia
-- Dla wywołania eval fs input e przyjmujemy, że dla każdej pary (x, v)
-- znajdującej się w input, wartość zmiennej x wynosi v.
-- Możemy założyć, że definicje funckcji fs oraz wyrażenie e są dobrze
-- typowane, tzn. typecheck fs (map fst input) e = Ok
-- UWAGA: to nie jest jeszcze rozwiązanie; należy zmienić jej definicję.
eval :: [FunctionDef p] -> [(Var,Integer)] -> Expr p -> EvalResult
eval fun s wyr =
case evaluate fun (M.union (fixInput s) (buildEnv fun)) wyr of
I val -> Value val
_ -> RuntimeError
data EvalVal p
= I Integer
| B Bool
| U
| P (EvalVal p) (EvalVal p)
-- L wartość ogon
| L (EvalVal p) (EvalVal p)
| Fn (M.Map Var (EvalVal p)) Var (Expr p)
| Fun Var (Expr p)
| Empty
| RunError
deriving (Eq)
-- Funkcja pomocnicza obliczająca wartość wyrażenia
evaluate :: [FunctionDef p] -> M.Map Var (EvalVal p) -> Expr p -> EvalVal p
evaluate fun s wyr =
case wyr of
EVar _ var ->
case M.lookup var s of
Just (Fun var1 expr) -> Fn (buildEnv fun) var1 expr
Just val -> val
-------------------------------------------------------------------------------
ENum _ val -> I val
-------------------------------------------------------------------------------
EBool _ val -> B val
-------------------------------------------------------------------------------
EUnary _ op val ->
case (evaluate fun s val,op) of
(B v, UNot) -> B (not v)
(I v, UNeg) -> I (-v)
_ -> RunError
-------------------------------------------------------------------------------
EBinary _ op val1 val2 ->
case evaluate fun s val1 of
RunError -> RunError
v1 ->
case evaluate fun s val2 of
RunError -> RunError
v2 ->
case (v1,op,v2) of
(B v1, BAnd, B v2) ->
if v1 && v2
then B True
else B False
(B v1, BOr, B v2) ->
if v1 || v2
then B True
else B False
(I v1, BEq, I v2) ->
if v1 == v2
then B True
else B False
(I v1, BNeq, I v2) ->
if v1 == v2
then B False
else B True
(I v1, BLt, I v2) ->
if v1 < v2
then B True
else B False
(I v1, BGt, I v2) ->
if v1 > v2
then B True
else B False
(I v1, BLe, I v2) ->
if v1 <= v2
then B True
else B False
(I v1, BGe, I v2) ->
if v1 >= v2
then B True
else B False
(v1, BAdd, v2) ->
bin v1 "+" v2
(v1, BSub, v2) ->
bin v1 "-" v2
(v1, BMul, v2) ->
bin v1 "*" v2
(v1, BDiv, I v2) ->
if v2 == 0
then RunError
else bin v1 "div" (I v2)
(v1, BMod, I v2) ->
if v2 == 0
then RunError
else bin v1 "mod" (I v2)
-------------------------------------------------------------------------------
ELet _ var a1 a2 ->
case evaluate fun s a1 of
RunError -> RunError
v1 -> evaluate fun (M.insert var v1 s) a2
-------------------------------------------------------------------------------
EIf _ e1 e2 e3 ->
case evaluate fun s e1 of
B True -> evaluate fun s e2
B False -> evaluate fun s e3
_ -> RunError
-------------------------------------------------------------------------------
EFn _ var _ e1 ->
Fn s var e1
-------------------------------------------------------------------------------
EApp _ e1 e2 ->
case evaluate fun s e2 of
RunError -> RunError
val1 ->
let (Fn env arg expr) = evaluate fun s e1 in
evaluate fun (M.insert arg val1 env) expr
EUnit _ -> U
EPair _ e1 e2 ->
case evaluate fun s e1 of
RunError -> RunError
typ1 ->
case evaluate fun s e2 of
RunError -> RunError
typ2 -> P typ1 typ2
-------------------------------------------------------------------------------
EFst _ e1 ->
case evaluate fun s e1 of
P v1 _ -> v1
_ -> RunError
-------------------------------------------------------------------------------
ESnd _ e1 ->
case evaluate fun s e1 of
P _ v2 -> v2
_ -> RunError
-------------------------------------------------------------------------------
ENil _ _ -> Empty
-------------------------------------------------------------------------------
ECons _ e1 e2 ->
case evaluate fun s e1 of
RunError -> RunError
typ1 ->
case evaluate fun s e2 of
RunError -> RunError
typ2 -> L typ1 typ2
-------------------------------------------------------------------------------
EMatchL _ e1 e2 (v1, v2, e3) ->
case evaluate fun s e1 of
Empty -> evaluate fun s e2
L val1 val2 -> evaluate fun
(M.insert v2 val2 (M.insert v1 val1 s)) e3
_ -> RunError
-- Oblicza wartość wyrażenia binarnego
bin :: EvalVal p -> String -> EvalVal p -> EvalVal p
bin a op b =
case op of
"+" -> I (v1 + v2)
"-" -> I (v1 - v2)
"*" -> I (v1 * v2)
"div" -> I (div v1 v2)
"mod" -> I (mod v1 v2)
where
I v1 = a
I v2 = b
fixInput :: [(Var,Integer)] -> M.Map Var (EvalVal p)
fixInput [] = M.empty
fixInput ((var,val):xs) = M.insert var (I val) (fixInput xs)
buildEnv :: [FunctionDef p] -> M.Map Var (EvalVal p)
buildEnv [] = M.empty
buildEnv (x:xs) =
M.insert (funcName x) (Fun (funcArg x) (funcBody x)) (buildEnv xs)