# Untitled

a guest
Dec 13th, 2018
131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. type exp = CstInt of int
2. | CstTrue
3. | CstFalse
4. | Times of exp * exp
5. | Sum of exp * exp
6. | Sub of exp * exp
7. | Eq of exp * exp
8. | Iszero of exp
9. | Or of exp * exp
10. | And of exp * exp
11. | Not of exp
12. | Ifthenelse of exp * exp * exp
13.
14. type evT = Int of int | Bool of bool
15.
16. let typecheck (x, y) = match x with
17. | "int" ->
18. (match y with
19. | Int(u) -> true
20. | _ -> false)
21. | "bool" ->
22. (match y with
23. | Bool(u) -> true
24. | _ -> false)
25. | _ -> failwith ("not a valid type");;
26.
27. let is_zero x = match (typecheck("int",x), x) with
28. | (true, Int(y)) -> Bool(y=0)
29. | (_, _) -> failwith("run-time error");;
30. let int_eq(x,y) =
31. match (typecheck("int",x), typecheck("int",y), x, y) with
32. | (true, true, Int(v), Int(w)) -> Bool(v = w)
33. | (_,_,_,_) -> failwith("run-time error ");;
34. let int_plus(x, y) =
35. match(typecheck("int",x), typecheck("int",y), x, y) with
36. | (true, true, Int(v), Int(w)) -> Int(v + w)
37. | (_,_,_,_) -> failwith("run-time error ");;
38. let int_times(x, y) =
39. match(typecheck("int",x), typecheck("int",y), x, y) with
40. | (true, true, Int(v), Int(w)) -> Int(v * w)
41. | (_,_,_,_) -> failwith("run-time error ");;
42. let int_sub(x, y) =
43. match(typecheck("int",x), typecheck("int",y), x, y) with
44. | (true, true, Int(v), Int(w)) -> Int(v - w)
45. | (_,_,_,_) -> failwith("run-time error ");;
46. let bool_and(x, y) =
47. match(typecheck("bool",x),typecheck("bool",y), x, y) with
48. | (true, true, Bool(v), Bool(w)) -> Bool(v && w)
49. | (_,_,_,_) -> failwith("run-time error ");;
50. let bool_or(x, y) =
51. match(typecheck("bool",x),typecheck("bool",y), x, y) with
52. | (true, true, Bool(v), Bool(w)) -> Bool(v || w)
53. | (_,_,_,_) -> failwith("run-time error ");;
54. let bool_not(x) =
55. match(typecheck("bool",x), x) with
56. | (true, Bool(v)) -> Bool(not(v))
57. | (_,_) -> failwith("run-time error ");;
58.
59. let rec eval e =
60. match e with
61. | CstInt(n) -> Int(n)
62. | CstTrue -> Bool(true)
63. | CstFalse -> Bool(false)
64. | Iszero(e1) -> is_zero(eval(e1))
65. | Eq(e1, e2) -> int_eq(eval(e1), eval(e2))
66. | Times(e1,e2) -> int_times(eval(e1), eval(e2))
67. | Sum(e1, e2) -> int_plus(eval(e1), eval(e2))
68. | Sub(e1, e2) -> int_sub(eval(e1), eval(e2))
69. | And(e1, e2) -> bool_and(eval(e1), eval(e2))
70. | Or(e1, e2) -> bool_or(eval(e1), eval(e2))
71. | Not(e1) -> bool_not(eval(e1))
72. | Ifthenelse(e1,e2,e3) ->
73. let g = eval(e1) in
74. (match (typecheck("bool", g), g) with
75. |(true, Bool(true)) -> eval(e2)
76. |(true, Bool(false)) -> eval(e3)
77. |(_, _) -> failwith ("nonboolean guard"))
78. | _ -> failwith ("run-time error");;
79.
80.
81. let e1= Eq(CstInt(5),CstInt(5));;
82. let e2 = Sum(CstInt(5),CstInt(1));;
83. let e3 = CstInt(2);;
84. let e = Ifthenelse(e1,e2,e3);;
85. eval e;;