Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. (*
  2.  
  3. Trabalho Final 2016/2
  4.  
  5. Grupo:
  6.  
  7. Leonardo Silva Rosa 242292
  8. Marcelo Haider 220505
  9. Gabriel Martins 228416
  10.  
  11. Avaliador : Big-Step
  12.  
  13. *)
  14.  
  15. type Variavel = string
  16.  
  17. type Operador =
  18. | Soma
  19. | Subtracao
  20. | Mult
  21. | Div
  22. | Menor
  23. | MenorIgual
  24. | Igual
  25. | Diferente
  26. | MaiorIgual
  27. | Maior
  28.  
  29. type Expressao =
  30. | Num of int
  31. | Bool of bool
  32. | Op of Expressao * Operador * Expressao
  33. | If of Expressao * Expressao * Expressao
  34. | Var of Variavel
  35. | App of Expressao * Expressao
  36. | Fn of Variavel * Expressao
  37. | Let of Variavel * Expressao * Expressao
  38. | LetRec of Variavel * Variavel * Expressao * Expressao
  39. | Nil
  40. | Cons of Expressao * Expressao
  41. | IsEmpty of Expressao
  42. | Hd of Expressao
  43. | Tl of Expressao
  44. | Raise
  45. | TryWith of Expressao * Expressao
  46.  
  47. type Valor =
  48. | VNumerico of int
  49. | VBooleano of bool
  50. | VClosure of Variavel * Expressao * Ambiente
  51. | VClosureRec of Variavel * Variavel * Expressao * Ambiente
  52. | VList of Valor * Valor
  53. | VNil
  54. | VRaise
  55. and
  56. Ambiente = (Variavel * Valor) list
  57.  
  58. (*-----------------------------------------------------------------------*)
  59.  
  60. (*--- Excecoes ---*)
  61.  
  62. exception CantGetValueException
  63. exception CantEvaluateException
  64. exception VariableNotDefinedException
  65.  
  66. (*-----------------------------------------------------------------------*)
  67.  
  68. (*--- Operadores ---*)
  69.  
  70. let soma (a: Valor)(b: Valor) : Valor =
  71. match (a,b) with
  72. | (VNumerico(x), VNumerico(y)) -> VNumerico(x+y)
  73. | _ -> raise CantGetValueException
  74.  
  75. let subt (a: Valor)(b: Valor) : Valor =
  76. match (a,b) with
  77. | (VNumerico(x), VNumerico(y)) -> VNumerico(x-y)
  78. | _ -> raise CantGetValueException
  79.  
  80. let mult (a: Valor)(b: Valor) : Valor =
  81. match (a,b) with
  82. | (VNumerico(x), VNumerico(y)) -> VNumerico(x*y)
  83. | _ -> raise CantGetValueException
  84.  
  85. let div (a: Valor)(b: Valor) : Valor =
  86. match (a,b) with
  87. | (VNumerico(x), VNumerico(y)) -> VNumerico(x/y)
  88. | _ -> raise CantGetValueException
  89.  
  90. let menor (a: Valor)(b: Valor) : Valor =
  91. match (a,b) with
  92. | (VNumerico(x), VNumerico(y)) -> VBooleano(x<y)
  93. | _ -> raise CantGetValueException
  94.  
  95. let menorIgual (a: Valor)(b: Valor) : Valor =
  96. match (a,b) with
  97. | (VNumerico(x), VNumerico(y)) -> VBooleano(x<=y)
  98. | _ -> raise CantGetValueException
  99.  
  100. let maior (a: Valor)(b: Valor) : Valor =
  101. match (a,b) with
  102. | (VNumerico(x), VNumerico(y)) -> VBooleano(x>y)
  103. | _ -> raise CantGetValueException
  104.  
  105. let maiorIgual (a: Valor)(b: Valor) : Valor =
  106. match (a,b) with
  107. | (VNumerico(x), VNumerico(y)) -> VBooleano(x>=y)
  108. | _ -> raise CantGetValueException
  109.  
  110. let igual (a: Valor)(b: Valor) : Valor =
  111. match (a,b) with
  112. | (VNumerico(x), VNumerico(y)) -> VBooleano(x=y)
  113. | _ -> raise CantGetValueException
  114.  
  115. let diferente (a: Valor)(b: Valor) : Valor =
  116. match (a,b) with
  117. | (VNumerico(x), VNumerico(y)) -> VBooleano(x<>y)
  118. | _ -> raise CantGetValueException
  119.  
  120. (*-----------------------------------------------------------------------*)
  121.  
  122. (*--- AMBIENTE ---*)
  123.  
  124. let rec lookupAmbiente (ambiente: Ambiente) (x: Variavel) : Valor =
  125. match ambiente with
  126. | [] -> raise VariableNotDefinedException
  127. | (variavel, valor) :: tl ->
  128. if variavel = x then
  129. valor
  130. else
  131. lookupAmbiente tl x;;
  132.  
  133. let adicionaAmbiente (ambiente: Ambiente) (x: Variavel) (v: Valor) : Ambiente =
  134. (x, v) :: ambiente
  135.  
  136. (*-----------------------------------------------------------------------*)
  137.  
  138. (*--- Semântica Big-Step ---*)
  139.  
  140. let rec avalia (ambiente: Ambiente) (e: Expressao) : Valor =
  141.  
  142. match e with
  143.  
  144. | Raise -> VRaise
  145.  
  146. | Num e -> VNumerico e
  147.  
  148. | Bool e -> VBooleano e
  149.  
  150. | Var e -> lookupAmbiente ambiente e
  151.  
  152. | If (e1, e2, e3) ->
  153. (match avalia ambiente e1 with
  154. | VRaise -> VRaise
  155. | VBooleano true -> avalia ambiente e2
  156. | VBooleano false -> avalia ambiente e3
  157. | _ -> raise CantEvaluateException
  158. )
  159.  
  160. | Op (e1, op, e2) ->
  161. let v1 = avalia ambiente e1 in
  162. let v2 = avalia ambiente e2 in
  163. (match (v1, v2) with
  164. | VRaise, x -> VRaise
  165. | x, VRaise -> VRaise
  166. | _ -> (match op with
  167. | Soma -> soma v1 v2
  168. | Subtracao -> subt v1 v2
  169. | Mult -> mult v1 v2
  170. | Div -> div v1 v2
  171. | Menor -> menor v1 v2
  172. | MenorIgual -> menorIgual v1 v2
  173. | Igual -> igual v1 v2
  174. | Diferente -> diferente v1 v2
  175. | MaiorIgual -> maiorIgual v1 v2
  176. | Maior -> maior v1 v2))
  177.  
  178. | Fn (x, e1) ->
  179. VClosure(x, e1, ambiente)
  180.  
  181. | App (e1, e2) ->
  182. let e1' = avalia ambiente e1 in
  183. let e2' = avalia ambiente e2 in
  184. match (e1', e2') with
  185. //Bs-App
  186. | (VRaise, x) -> VRaise
  187. | (x, VRaise) -> VRaise
  188. | (VClosure(x, e, env), v) ->
  189. let env' : Ambiente = (adicionaAmbiente env x v) in
  190. avalia env' e
  191. //Bs-AppRec
  192. | (VClosureRec(f, x, e, env), v) ->
  193. let env' : Ambiente = (adicionaAmbiente (adicionaAmbiente env x v) f (VClosureRec(f, x, e, env))) in
  194. avalia env' e
  195. | _ -> raise CantEvaluateException
  196.  
  197. | Let (x, e1, e2) ->
  198. let env' : Ambiente = (adicionaAmbiente ambiente x (avalia ambiente e1))
  199. in avalia env' e2
  200.  
  201. | LetRec (f, x, e1, e2) ->
  202. let closerec = VClosureRec(f, x, e1, ambiente) in
  203. avalia (adicionaAmbiente ambiente f closerec) e2
  204.  
  205. | Nil -> VNil
  206.  
  207. | Cons (e1, e2) ->
  208. let e1' = (avalia ambiente e1) in
  209. match e1' with
  210. | VRaise -> VRaise
  211. | _ ->
  212. let e2' = (avalia ambiente e2) in
  213. match e2' with
  214. | VRaise -> VRaise
  215. | _ -> VList (e1', e2')
  216.  
  217. | IsEmpty e ->
  218. let e' = (avalia ambiente e) in
  219. match (e') with
  220. | VRaise -> VRaise
  221. | VNil -> VBooleano (true)
  222. | VList (x, y) -> VBooleano (false)
  223. | _ -> raise CantEvaluateException
  224.  
  225. | Hd e ->
  226. let e' = (avalia ambiente e) in
  227. match (e') with
  228. | VRaise -> VRaise
  229. | VList (x, y) -> x
  230. | _ -> raise CantEvaluateException
  231.  
  232. | Tl e ->
  233. let e' = (avalia ambiente e) in
  234. match (e') with
  235. | VRaise -> VRaise
  236. | VList (x, y) -> y
  237. | _ -> raise CantEvaluateException
  238.  
  239. | TryWith (e1, e2) ->
  240. let e1' = (avalia ambiente e1) in
  241. match (e1') with
  242. | VRaise -> avalia ambiente e2
  243. | _ -> e1'
  244.  
  245.  
  246. (*-----------------------------------------------------------------------*)
  247.  
  248. (*--- Testes ---*)
  249.  
  250. let ambiente : Ambiente = [];;
  251.  
  252. // Teste de fatorial
  253.  
  254. let numero = 6;;
  255. let igualA1 = Op(Var "x", Igual, Num 1);;
  256. let menos1 = Op(Var "x", Subtracao, Num 1);;
  257. let funcao = If(igualA1, Num 1, Op(Var "x", Mult, App(Var "fat", menos1)));;
  258. let fatorial = LetRec("fat", "x", funcao, App(Var "fat", Num numero));;
  259.  
  260. avalia ambiente fatorial;;
  261.  
  262. // Testes listas
  263.  
  264. let listexp = Cons(Num 1, Cons(Num 2, Cons(Num 3, Nil)));;
  265. let listexp2 = Cons(Num 1, Nil);;
  266. let listexp3 = Cons(Num 0, listexp);;
  267.  
  268. avalia ambiente listexp;;
  269. avalia ambiente listexp2;;
  270. avalia ambiente listexp3;;
  271.  
  272. let isempty1 = IsEmpty(listexp);;
  273. let isempty2 = IsEmpty(Nil);;
  274.  
  275. avalia ambiente isempty1;;
  276. avalia ambiente isempty2;;
  277.  
  278. let hd1 = Hd(listexp);;
  279. let tl1 = Tl(listexp);;
  280. let hd2 = Hd(listexp2);;
  281. let tl2 = Tl(listexp2);;
  282.  
  283. avalia ambiente hd1;;
  284. avalia ambiente tl1;;
  285. avalia ambiente hd2;;
  286. avalia ambiente tl2;;
  287.  
  288. // Testes Try-with
  289.  
  290. let tw = TryWith(Raise, Num 1);;
  291. let tw2 = TryWith(Num 2, Raise);;
  292. let tw3 = TryWith(Raise, Raise);;
  293.  
  294. avalia ambiente tw;;
  295. avalia ambiente tw2;;
  296. avalia ambiente tw3;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement