-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCAML-interpreter.ml
539 lines (483 loc) · 19.1 KB
/
OCAML-interpreter.ml
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
(* Author: Giuseppe Muschetta *)
(* Abstract syntax *)
type ide = string;;
type exp =
| CstInt of int
| CstTrue
| CstFalse
| CstStr of ide
| Den of ide
| Equals of exp * exp
| Minor of exp * exp
| Greater of exp * exp
| Minoreq of exp * exp
| Greatereq of exp * exp
| Sum of exp * exp
| Diff of exp * exp
| Times of exp * exp
| Div of exp * exp
| Mod of exp * exp
| And of exp * exp
| Or of exp * exp
| Not of exp
| Minus of exp
| Ifthenelse of exp * exp * exp
| Let of ide * exp * exp
| Letrec of ide * ide * exp * exp
| Fun of ide * exp
| Apply of exp * exp
| Dict of dictionary
| Get of exp * ide
| Add of exp * ide * exp
| Remove of exp * ide
| Clear of exp
| ApplyOver of exp * exp
and dictionary = Empty | Item of (ide * exp) * dictionary;; (* dictionary type *)
(* Environment *)
type 't env = (ide * 't) list;;
(* Expressible types *)
type evT =
| Unbound
| Int of int
| Bool of bool
| String of string
| Closure of ide * exp * evT env
| RecClosure of ide * ide * exp * evT env
| DynFun of ide * exp
| RecDynFun of ide * ide * exp
| Dictionary of (ide * evT) list;;
(* Keeping going on with the environment *)
let emptyEnv = [];;
let bind (r : 't env) (i : ide) (v : evT) = (i, v)::r;;
let rec lookup (r : 't env) (i : ide) =
match r with
| [] -> Unbound
| (nome, valore)::tail when nome = i -> valore
| _::tail -> lookup tail i;;
(* RunTime support *)
(* Type check *)
let typecheck (s : string) (v : evT) : bool =
match s with
| "int" -> (match v with
Int n -> true
| _ -> false )
| "bool" -> (match v with
Bool p -> true
| _ -> false )
| "dictionary" -> ( match v with (* controllo per il tipo dizionario *)
Dictionary d -> true
| _ -> false )
| _ -> failwith ("Type error");;
(* Exceptions *)
exception GenericError of string;;
exception KeyNotFound;;
exception ExistingKey of string;;
(* Aux functions for eval / rt_eval *)
let et x y =
if typecheck "bool" x && typecheck "bool" y
then (
match (x, y) with
| (Bool n, Bool u) -> Bool(n && u)
| (_,_) -> raise (GenericError "et function")
)
else failwith("Type error");;
let times x y =
if typecheck "int" x && typecheck "int" y
then (
match (x, y) with
| (Int n, Int u) -> Int(n * u)
| (_,_) -> raise (GenericError "prod function")
)
else failwith("Type error");;
let div x y =
if typecheck "int" x && typecheck "int" y
then (
match (x, y) with
| (Int n, Int u) -> if u = 0 then failwith ("Division by zero")
else Int(n / u)
| (_,_) -> raise (GenericError "div function")
)
else failwith("Type error");;
let modulo x y =
if typecheck "int" x && typecheck "int" y
then (
match (x, y) with
| (Int n, Int u) -> if u = 0 then failwith ("Division by zero")
else Int(n mod u)
| (_,_) -> raise (GenericError "div function")
)
else failwith("Type error");;
let sum x y =
if typecheck "int" x && typecheck "int" y
then (
match (x, y) with
| (Int n, Int u) -> Int(n + u)
| (_,_) -> raise (GenericError "sum function")
)
else failwith("Type error");;
let diff x y =
if typecheck "int" x && typecheck "int" y
then (
match (x, y) with
| (Int n, Int u) -> if n > u then Int(n - u)
else Int(u - n)
| (_,_) -> raise (GenericError "diff function")
)
else failwith("Type error");;
let equals x y =
if typecheck "int" x && typecheck "int" y
then (
match (x, y) with
| (Int n, Int u) -> Bool(n = u)
| (_,_) -> raise (GenericError "eq function")
)
else failwith("Type error");;
let minoreq x y =
if typecheck "int" x && typecheck "int" y
then (
match (x, y) with
| (Int n, Int u) -> Bool(n <= u)
| (_,_) -> raise (GenericError "minoreq function")
)
else failwith("Type error");;
let minor x y =
if typecheck "int" x && typecheck "int" y
then (
match (x, y) with
| (Int n, Int u) -> Bool(n < u)
| (_,_) -> raise (GenericError "minor function")
)
else failwith("Type error");;
let greatereq x y =
if typecheck "int" x && typecheck "int" y
then (
match (x, y) with
| (Int n, Int u) -> Bool(n >= u)
| (_,_) -> raise (GenericError "majoreq function")
)
else failwith("Type error");;
let greater x y =
if typecheck "int" x && typecheck "int" y
then (
match (x, y) with
| (Int n, Int u) -> Bool(n > u)
| (_,_) -> raise (GenericError "major function")
)
else failwith("Type error");;
let vel x y =
if typecheck "bool" x && typecheck "bool" y
then (
match (x, y) with
| (Bool n, Bool u) -> Bool(n || u)
| (_,_) -> raise (GenericError "vel function")
)
else failwith("Type error");;
let non x =
if typecheck "bool" x = true
then (
match x with
| Bool true -> Bool false
| Bool false -> Bool true
| _ -> raise (GenericError "non function")
)
else failwith ("Type error");;
let minus x =
if typecheck "int" x = true
then (
match x with
| Int n -> Int(-n)
| _ -> raise (GenericError "minus function")
)
else failwith ("Type error");;
(* It counts key occurrences *)
let rec contaOcc dict key =
match dict with
| Empty -> 0
| Item ((k, v), tail) -> if k = key
then 1 + contaOcc tail key
else contaOcc tail key;;
(* It returns the key value if exists, else it raises an exception *)
let rec get dict key =
match dict with
| [] -> raise KeyNotFound
| (k, v)::tail -> if k = key
then v
else get tail key;;
(* If the dictionary doesn't contain the key then this function adds the couple key-value,
else it raises an exception *)
let rec add dict key value =
match dict with
| [] -> (key, value)::[]
| (k, v)::tail -> if k = key
then raise (ExistingKey "Key already in the dictionary")
else (k, v)::(add tail key value);;
(* If the dictionary contains the key then this function removes the couple key-value,
else it raises an exception *)
let rec remove dict key =
match dict with
| [] -> raise KeyNotFound
| (k, v)::tail -> if k = key
then tail
else (k, v)::(remove tail key);;
(* Interpreter with static scope rule *)
let rec eval (e: exp) (r: 't env) : evT =
match e with
| CstInt n -> Int n
| CstTrue -> Bool true
| CstFalse -> Bool false
| CstStr s -> String s
| Den i -> lookup r i
| Minoreq (a, b) -> minoreq (eval a r) (eval b r)
| Minor (a, b) -> minor (eval a r) (eval b r)
| Greatereq (a, b) -> greatereq (eval a r) (eval b r)
| Greater (a, b) -> greater (eval a r) (eval b r)
| Times (a, b) -> times (eval a r) (eval b r)
| Div (a, b) -> div (eval a r) (eval b r)
| Mod (a, b) -> modulo (eval a r) (eval b r)
| Sum (a, b) -> sum (eval a r) (eval b r)
| Diff (a, b) -> diff (eval a r) (eval b r)
| Minus a -> minus (eval a r)
| And (a, b) -> et (eval a r) (eval b r)
| Or (a, b) -> vel (eval a r) (eval b r)
| Not a -> non (eval a r)
| Equals (a, b) -> equals (eval a r) (eval b r)
| Ifthenelse (guard, thEn, eLse) ->
let guardia = eval guard r in
if typecheck "bool" guardia = true
then
match guardia with
| Bool true -> eval thEn r
| Bool false -> eval eLse r
| _ -> raise (GenericError "Unexpected")
else failwith ("Non boolean guard")
| Let (i, e1, e2) -> eval (e2) (bind r i (eval e1 r))
| Letrec (f, param, fbody, lbody) ->
let bEnv = bind r f (RecClosure (f, param, fbody, r)) in
eval lbody bEnv
| Fun (param, fbody) -> Closure (param, fbody, r)
| Apply (eF, eArg) ->
let fclosure = eval eF r in (
match fclosure with
| Closure (arg, fbody, fDecEnv) ->
let aVal = eval eArg r in
let aEnv = bind fDecEnv arg aVal in
eval fbody aEnv
| RecClosure (f, arg, fbody, fDecEnv) ->
let aVal = eval eArg r in
let rEnv = bind fDecEnv f fclosure in
let aEnv = bind rEnv arg aVal in
eval fbody aEnv
| _ -> failwith ("Non functional value")
)
| Dict dict -> Dictionary (applyEval dict r)
| Get (dict, key) -> (
let eD = eval dict r in
if typecheck "dictionary" eD then
match eD with
| Dictionary d -> get d key
| _ -> raise (GenericError "eval function")
else failwith ("Type error")
)
| Add (dict, key, value) -> Dictionary (
let eD = eval dict r in
if typecheck "dictionary" eD then
match eD with
| Dictionary d -> add d key (eval value r)
| _ -> raise (GenericError "eval function")
else failwith ("Type error")
)
| Remove (dict, key) -> Dictionary (
let eD = eval dict r in
if typecheck "dictionary" eD then
match eD with
| Dictionary d -> remove d key
| _ -> raise (GenericError "eval function")
else failwith ("Type error")
)
| Clear dict -> (
let eD = eval dict r in
if typecheck "dictionary" eD then
match eD with
| Dictionary d -> Dictionary ([])
| _ -> raise (GenericError "eval function")
else failwith ("Type error")
)
| ApplyOver (f, dict) -> Dictionary (
let eD = eval dict r in
if typecheck "dictionary" eD then
match eD with
| Dictionary d -> (
match eval f r with
| Closure (arg, fbody, fDecEnv) -> applyOver f d r
| RecClosure (fn, arg, fbody, fDecEnv) -> applyOver f d r
| _ -> failwith ("Non functional value")
)
| _ -> raise (GenericError "eval function")
else failwith ("Type error")
)
and applyEval dict r =
match dict with
| Empty -> []
| Item((k, v), tail) -> if contaOcc dict k = 1
then (k, eval v r)::(applyEval tail r)
else failwith ("Il dizionario non può avere chiavi doppioni")
and applyOver f d r =
match d with
| [] -> []
| (k, v)::tail -> (k, applica f v r)::(applyOver f tail r)
and applica f v r =
match eval f r with
| Closure (arg, fbody, fDecEnv) -> (
let aEnv = bind fDecEnv arg v in
eval fbody aEnv
)
| RecClosure (fn, arg, fbody, fDecEnv) -> (
let rEnv = bind fDecEnv fn (eval f r) in
let aEnv = bind rEnv arg v in
eval fbody aEnv
)
| _ -> failwith ("Non functional value")
;;
(* Interpreter with dynamic scope rule *)
let rec rt_eval (e : exp) (r : 't env): evT =
match e with
| CstInt n -> Int n
| CstTrue -> Bool true
| CstFalse -> Bool false
| CstStr s -> String s
| Den i -> lookup r i
| Minoreq (a, b) -> minoreq (rt_eval a r) (rt_eval b r)
| Minor (a, b) -> minor (rt_eval a r) (rt_eval b r)
| Greatereq (a, b) -> greatereq (rt_eval a r) (rt_eval b r)
| Greater (a, b) -> greater (rt_eval a r) (rt_eval b r)
| Times (a, b) -> times (rt_eval a r) (rt_eval b r)
| Div (a, b) -> div (rt_eval a r) (rt_eval b r)
| Mod (a, b) -> modulo (rt_eval a r) (rt_eval b r)
| Sum (a, b) -> sum (rt_eval a r) (rt_eval b r)
| Diff (a, b) -> diff (rt_eval a r) (rt_eval b r)
| Minus a -> minus (rt_eval a r)
| And (a, b) -> et (rt_eval a r) (rt_eval b r)
| Or (a, b) -> vel (rt_eval a r) (rt_eval b r)
| Not a -> non (rt_eval a r)
| Equals (a, b) -> equals (rt_eval a r) (rt_eval b r)
| Ifthenelse (guard, thEn, eLse) ->
let guardia = rt_eval guard r in
if typecheck "bool" guardia = true
then
match guardia with
| Bool true -> rt_eval thEn r
| Bool false -> rt_eval eLse r
| _ -> raise (GenericError "Unexpected")
else failwith ("Non boolean guard")
| Let (i, e1, e2) -> rt_eval (e2) (bind r i (rt_eval e1 r))
| Letrec (f, param, fbody, lbody) ->
let bEnv = bind r f (RecDynFun (f,param,fbody)) in
rt_eval lbody bEnv
| Fun (param, fbody) -> DynFun (param, fbody)
| Apply (eF, eArg) ->
let fval = rt_eval eF r in (
match fval with
| DynFun (arg, fbody) ->
let aVal = rt_eval eArg r in
let aEnv = bind r arg aVal in
rt_eval fbody aEnv
| RecDynFun (f, arg, fbody) ->
let aVal = rt_eval eArg r in
let rEnv = bind r f fval in
let aEnv = bind rEnv arg aVal in
rt_eval fbody aEnv
| _ -> failwith ("Non functional value")
)
| Dict dict -> Dictionary (applyEval dict r)
| Get (dict, key) -> (
let eD = rt_eval dict r in
if typecheck "dictionary" eD then
match eD with
| Dictionary d -> get d key
| _ -> raise (GenericError "rt_eval function")
else failwith ("Type error")
)
| Add (dict, key, value) -> Dictionary (
let eD = rt_eval dict r in
if typecheck "dictionary" eD then
match eD with
| Dictionary d -> add d key (rt_eval value r)
| _ -> raise (GenericError "rt_eval function")
else failwith ("Type error")
)
| Remove (dict, key) -> Dictionary (
let eD = rt_eval dict r in
if typecheck "dictionary" eD then
match eD with
| Dictionary d -> remove d key
| _ -> raise (GenericError "rt_eval function")
else failwith ("Type error")
)
| Clear dict -> (
let eD = rt_eval dict r in
if typecheck "dictionary" eD then
match eD with
| Dictionary d -> Dictionary ([])
| _ -> raise (GenericError "rt_eval function")
else failwith ("Type error")
)
| ApplyOver (f, dict) -> Dictionary (
let eD = rt_eval dict r in
if typecheck "dictionary" eD then
match eD with
| Dictionary d -> (
match rt_eval f r with
| DynFun (arg, fbody) -> applyOver f d r
| RecDynFun (fn, arg, fbody) -> applyOver f d r
| _ -> failwith ("Non functional value")
)
| _ -> raise (GenericError "rt_eval function")
else failwith ("Type error")
)
and applyEval dict r =
match dict with
| Empty -> []
| Item((k, v), tail) -> if contaOcc dict k = 1
then (k, rt_eval v r)::(applyEval tail r)
else failwith ("Il dizionario non può avere chiavi doppioni")
and applyOver f d r =
match d with
| [] -> []
| (k, v)::tail -> (k, applica f v r)::(applyOver f tail r)
and applica f v r =
match rt_eval f r with
| DynFun (arg, fbody) -> (
let aEnv = bind r arg v in
rt_eval fbody aEnv
)
| RecDynFun (fn, arg, fbody) -> (
let rEnv = bind r fn (rt_eval f r) in
let aEnv = bind rEnv arg v in
rt_eval fbody aEnv
)
| _ -> failwith ("Non functional value")
;;
(* ==================================== TEST ==================================== *)
(* It creates a dictionary with duplicate keys, then it raises an exception *)
let e0 = Dict(Item(("one", CstInt 1), Item(("two", CstInt 2), Item(("three", CstInt 3), Item(("two", CstInt 4), Empty)))));;
(* It creates a dictionary *)
let e1 = Dict(Item(("one", CstInt 1), Item(("two", CstInt 2), Item(("three", CstInt 3), Item(("four", CstInt 4), Empty)))));;
(* It returns the value associated with the "three" key *)
let e2 = Get(e1, "three");;
(* It adds the couple ("five", 5) to the dictionary *)
let e3 = Add(e1, "five", CstInt 5);;
(* It adds the couple ("six", 6) to the dictionary. It's called on e3 *)
let e4 = Add(e3, "six", Sum(CstInt 2, CstInt 4));;
(* It removes the "three" key and the associated value. It's called on e4 *)
let e5 = Remove(e4, "three");;
(* It returns an empty dictionary *)
let e6 = Clear(e3);;
(* It applies the x -> x + 3 function to every element in the dictionary. It's called on e4 *)
let e7 = ApplyOver(Fun("x", Sum(Den "x", CstInt 3)), e4);;
(* Test for the eval function with dynamic scope rule *)
let e8 = Let("x", CstInt 1, Let("f", Fun("y", Den "x"), Let("x", CstInt 2, Apply (Den "f", CstInt 0))));;
(* let e8 =
let x = 1 in
let f = fun y -> x in
let x = 2 in f 0;;
with dynamic scope it should return 1, else (with static scope) it should return 2 *)