Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.02 KB | None | 0 0
  1. type ide = string
  2. type exp =
  3. | Eint of int
  4. | Ebool of bool
  5. | Estring of string
  6. | Den of ide
  7. | Prod of exp * exp
  8. | Sum of exp * exp
  9. | Diff of exp * exp
  10. | Eq of exp * exp
  11. | Minus of exp
  12. | Strlen of exp
  13. | Strsub of exp * exp * exp
  14. | Strcat of exp * exp
  15. | Strget of exp * exp
  16. | Reflect of exp
  17. | Iszero of exp
  18. | Or of exp * exp
  19. | And of exp * exp
  20. | Not of exp
  21. | Ifthenelse of exp * exp * exp
  22. | Val of exp
  23. | Let of ide * exp * exp
  24. | Newloc of exp
  25. | Fun of ide list * exp
  26. | Appl of exp * exp list
  27. | Rec of ide * exp
  28. | Proc of ide list * decl * com list
  29. and decl = (ide * exp) list * (ide * exp) list
  30. and com =
  31. | Assign of exp * exp
  32. | Cifthenelse of exp * com list * com list
  33. | While of exp * com list
  34. | Block of decl * com list
  35. | Call of exp * exp list
  36.  
  37. (* implementazione funzionale dell'ambiente *)
  38. (*
  39. module Funenv:ENV =
  40. struct *)
  41. exception WrongBindlist
  42. type 't env = string -> 't
  43. let emptyenv(x) = function y -> x
  44. let applyenv(x,y) = x y
  45. let bind((r: 'a env) , (l:string), (e:'a)) =
  46. function lu -> if lu = l then e else applyenv(r,lu)
  47. let rec bindlist(r, il, el) = match (il,el) with
  48. | ([],[]) -> r
  49. | i::il1, e::el1 -> bindlist (bind(r, i, e), il1, el1)
  50. | _ -> raise WrongBindlist
  51. (*)
  52. end
  53. *)
  54.  
  55. (*
  56. module type STORE =
  57. sig
  58. type 't store
  59. type loc
  60. val emptystore : 't -> 't store
  61. val allocate : 't store * 't -> loc * 't store
  62. val update : 't store * loc * 't -> 't store
  63. val applystore : 't store * loc -> 't
  64. end
  65. module Funstore:STORE =
  66. struct
  67. *)
  68. type loc = int
  69. type 't store = loc -> 't
  70. let (newloc,initloc) = let count = ref(-1) in
  71. (fun () -> count := !count +1; !count),
  72. (fun () -> count := -1)
  73. let emptystore(x) = initloc(); function y -> x
  74. let applystore(x,y) = x y
  75. let allocate((r: 'a store) , (e:'a)) = let l = newloc() in
  76. (l, function lu -> if lu = l then e else applystore(r,lu))
  77. let update((r: 'a store) , (l:loc), (e:'a)) =
  78. function lu -> if lu = l then e else applystore(r,lu)
  79. (*
  80. end
  81. *)
  82.  
  83. (* domini semantici *)
  84. type eval =
  85. | Int of int
  86. | Bool of bool
  87. | String of string
  88. | Novalue
  89. | Funval of efun
  90. and dval =
  91. | Dint of int
  92. | Dbool of bool
  93. | Dstring of string
  94. | Unbound
  95. | Dloc of loc
  96. | Dfunval of efun
  97. | Dprocval of proc
  98. and mval =
  99. | Mint of int
  100. | Mbool of bool
  101. | Mstring of string
  102. | Undefined
  103. and efun = (dval list) * (mval store) -> eval (*scoping statico*)
  104. and proc = (dval list) * (mval store) -> mval store
  105.  
  106. exception Nonstorable
  107. exception Nonexpressible
  108. let evaltomval e = match e with
  109. | Int n -> Mint n
  110. | Bool n -> Mbool n
  111. | String n ->Mstring n
  112. | _ -> raise Nonstorable
  113. let mvaltoeval m = match m with
  114. | Mint n -> Int n
  115. | Mbool n -> Bool n
  116. | Mstring n -> String n
  117. | _ -> Novalue
  118. let evaltodval e = match e with
  119. | Int n -> Dint n
  120. | Bool n -> Dbool n
  121. | String n -> Dstring n
  122. | Novalue -> Unbound
  123. | Funval n -> Dfunval n
  124. let dvaltoeval e = match e with
  125. | Dint n -> Int n
  126. | Dbool n -> Bool n
  127. | Dstring n -> String n
  128. | Dloc n -> raise Nonexpressible
  129. | Dfunval n -> Funval n
  130. | Dprocval n -> raise Nonexpressible
  131. | Unbound -> Novalue
  132.  
  133. (* operazioni primitive *)
  134. let typecheck (x, y) = match x with
  135. | "int" -> (match y with
  136. | Int(u) -> true
  137. | _ -> false)
  138. | "bool" -> (match y with
  139. | Bool(u) -> true
  140. | _ -> false)
  141. | "string"-> (match y with
  142. | String(u)-> true
  143. | _ ->false)
  144. | _ -> failwith ("not a valid type")
  145. let minus x = if typecheck("int",x) then (match x with Int(y) -> Int(-y) )
  146. else failwith ("type error")
  147. let iszero x = if typecheck("int",x) then (match x with Int(y) -> Bool(y=0) )
  148. else failwith ("type error")
  149. let equ (x,y) = if typecheck("int",x) & typecheck("int",y)
  150. then (match (x,y) with (Int(u), Int(w)) -> Bool(u = w))
  151. else failwith ("type error")
  152. let plus (x,y) = if typecheck("int",x) & typecheck("int",y)
  153. then (match (x,y) with (Int(u), Int(w)) -> Int(u+w))
  154. else failwith ("type error")
  155. let diff (x,y) = if typecheck("int",x) & typecheck("int",y)
  156. then (match (x,y) with (Int(u), Int(w)) -> Int(u-w))
  157. else failwith ("type error")
  158. let mult (x,y) = if typecheck("int",x) & typecheck("int",y)
  159. then (match (x,y) with (Int(u), Int(w)) -> Int(u*w))
  160. else failwith ("type error")
  161. let strlen (x) =if typecheck("string", x)
  162. then (match (x) with (String(u))-> Int(String.length u))
  163. else failwith("Type error")
  164. let strcat (x,y) =if typecheck("string", x) & typecheck("string",y)
  165. then (match (x,y) with (String(u), String(w)) -> String(String.concat "" [u;w]))
  166. else failwith("Type error")
  167. let strsub (x,y,z) =if typecheck("string", x) & typecheck("int",y) & typecheck("int",z)
  168. then (match (x,y,z) with (String(u), Int(w),Int(v)) -> String(String.sub u w (v-w)))
  169. else failwith("Type error")
  170. let strget (x,y) =if typecheck("string", x) & typecheck("int",y)
  171. then (match (x,y) with (String(u), Int(w)) -> String(String.make 1 (String.get u w)))
  172. else failwith("Type error")
  173. let et (x,y) = if typecheck("bool",x) & typecheck("bool",y)
  174. then (match (x,y) with (Bool(u), Bool(w)) -> Bool(u & w))
  175. else failwith ("type error")
  176. let vel (x,y) = if typecheck("bool",x) & typecheck("bool",y)
  177. then (match (x,y) with (Bool(u), Bool(w)) -> Bool(u or w))
  178. else failwith ("type error")
  179. let non x = if typecheck("bool",x)
  180. then (match x with Bool(y) -> Bool(not y) )
  181. else failwith ("type error")
  182. (*let rec elemEval (e:string) =
  183. let funComp=String.split_on_char '(' e in
  184. let funName=funComp.hd in
  185. let funParams=String.split_on_char ',' (funComp.nth 1) in
  186.  
  187.  
  188. match funName with
  189. | "Assign" -> sem Assign((funParams.nth 0),(funParams.nth 0))
  190. |
  191. (*let regExp= Str.regExp "(Assign)";;
  192. if Str.string_match regExp e 0
  193. then Assign()*)
  194. let reflect (x, r, s) =if typecheck("string",x)
  195. then (match(x)with (String(u))-> let strList=String.split_on_char ';' u in
  196. for s in strList:
  197. elemEval s r s
  198. )
  199. else failwith("Type error");*)
  200.  
  201. (*
  202. Togliere quadre
  203.  
  204. prova 1:
  205. aggiungere reflect in sem con r & s
  206.  
  207. *)
  208. (* fino a qui giusto *)
  209.  
  210.  
  211. (* funzioni di valutazione semantica (denotazionale) *)
  212. let rec makefun ((a:exp),(x:dval env)) = match a with
  213. | Fun(ii,aa) -> Dfunval(function (d, s) -> sem aa (bindlist (x, ii, d)) s)
  214. | _ -> failwith ("Non-functional object")
  215. and makefunrec (i, Fun(ii, aa), r) =
  216. let functional ff (d, s1) =
  217. let r1 = bind(bindlist(r, ii, d), i, Dfunval(ff)) in sem aa r1 s1 in
  218. let rec fix = function x -> functional fix x in Funval(fix)
  219. and makeproc((a:exp),(x:dval env)) = match a with
  220. | Proc(ii,dl,cl) -> Dprocval(function (d, s) -> semb (dl,cl) (bindlist (x, ii, d)) s)
  221. | _ -> failwith ("Non-functional object")
  222. and applyfun ((ev1:dval),(ev2:dval list), s) = match ev1 with
  223. | Dfunval(x) -> x (ev2, s)
  224. | _ -> failwith ("attempt to apply a non-functional object")
  225. and applyproc ((ev1:dval),(ev2:dval list), s) = match ev1 with
  226. | Dprocval(x) -> x (ev2, s)
  227. | _ -> failwith ("attempt to apply a non-functional object")
  228. and sem (e:exp) (r:dval env) (s: mval store) =
  229. match e with
  230. | Eint(n) -> Int(n)
  231. | Ebool(b) -> Bool(b)
  232. | Estring(s)-> String(s)
  233. | Den(i) -> dvaltoeval(applyenv(r,i))
  234. | Iszero(a) -> iszero((sem a r s) )
  235. | Eq(a,b) -> equ((sem a r s) ,(sem b r s) )
  236. | Prod(a,b) -> mult ( (sem a r s), (sem b r s))
  237. | Sum(a,b) -> plus ( (sem a r s), (sem b r s))
  238. | Diff(a,b) -> diff ( (sem a r s), (sem b r s))
  239. | Minus(a) -> minus( (sem a r s))
  240. | And(a,b) -> et ( (sem a r s), (sem b r s))
  241. | Or(a,b) -> vel ( (sem a r s), (sem b r s))
  242. | Not(a) -> non( (sem a r s))
  243. | Strlen(a) -> strlen( (sem a r s))
  244.  
  245. (* | Reflect(a) -> reflect( (sem a r s) r s)*)
  246.  
  247. | Strcat(a,b) -> strcat( (sem a r s),(sem b r s))
  248. | Strsub(a,b,c) -> strsub((sem a r s),(sem b r s),(sem c r s))
  249. | Strget (a,b) -> strget((sem a r s),(sem b r s))
  250. | Ifthenelse(a,b,c) -> let g = sem a r s in
  251. if typecheck("bool",g) then (if g = Bool(true) then sem b r s else sem c r s)
  252. else failwith ("nonboolean guard")
  253. | Val(e) -> let (v, s1) = semden e r s in (match v with
  254. | Dloc n -> mvaltoeval(applystore(s1, n))
  255. | _ -> failwith("not a variable"))
  256. | Let(i,e1,e2) -> let (v, s1) = semden e1 r s in sem e2 (bind (r ,i, v)) s1
  257. | Fun(i,e1) -> dvaltoeval(makefun(e,r))
  258. | Rec(i,e1) -> makefunrec(i, e1, r)
  259. | Appl(a,b) -> let (v1, s1) = semlist b r s in applyfun(evaltodval(sem a r s), v1, s1)
  260. | _ -> failwith ("nonlegal expression for sem")
  261. and semden (e:exp) (r:dval env) (s: mval store) = match e with
  262. | Den(i) -> (applyenv(r,i), s)
  263. | Fun(i, e1) -> (makefun(e, r), s)
  264. | Proc(i, dl, cl) -> (makeproc(e, r), s)
  265. | Newloc(e) -> let m = evaltomval(sem e r s) in let (l, s1) = allocate(s, m) in (Dloc l, s1)
  266. | _ -> (evaltodval(sem e r s), s)
  267. and semlist el r s = match el with
  268. | [] -> ([], s)
  269. | e::el1 -> let (v1, s1) = semden e r s in let (v2, s2) = semlist el1 r s1 in (v1 :: v2, s2)
  270. and semc (c: com) (r:dval env) (s: mval store) = match c with
  271. | Assign(e1, e2) -> let (v1, s1) = semden e1 r s in (match v1 with
  272. | Dloc(n) -> update(s1, n, evaltomval(sem e2 r s))
  273. | _ -> failwith ("wrong location in assignment"))
  274. | Cifthenelse(e, cl1, cl2) -> let g = sem e r s in
  275. if typecheck("bool",g) then
  276. (if g = Bool(true) then semcl cl1 r s else semcl cl2 r s)
  277. else failwith ("nonboolean guard")
  278. | While(e, cl) ->
  279. let functional ((fi: mval store -> mval store)) =
  280. function sigma ->
  281. let g = sem e r sigma in
  282. if typecheck("bool",g) then
  283. (if g = Bool(true) then fi(semcl cl r sigma) else sigma)
  284. else failwith ("nonboolean guard")
  285. in
  286. let rec ssfix = function x -> functional ssfix x in ssfix(s)
  287. | Call(e1, e2) -> let (p, s1) = semden e1 r s in let (v, s2) = semlist e2 r s1 in
  288. applyproc(p, v, s2)
  289. | Block(dl,cl) -> semb (dl, cl) r s
  290. and semcl cl r s = match cl with
  291. | [] -> s
  292. | c::cl1 -> semcl cl1 r (semc c r s)
  293. | _ -> failwith ("wrong semcl")
  294.  
  295. (* semantica di blocchi e dichiarazioni and semb ((dl, rdl), cl) r s =*)
  296. and semb ((dl, rdl), cl) r s =
  297. let (r1, s1) = semdl (dl, rdl) r s in semcl cl r1 s1
  298. and semdl (dl, rl) r s = let (r1, s1) = semdv dl r s in
  299. semdr rl r1 s1
  300. and semdv dl r s = match dl with
  301. | [] -> (r,s)
  302. | (i,e)::dl1 -> let (v, s1) = semden e r s in
  303. semdv dl1 (bind(r, i, v)) s1
  304. and semdr rl r s =
  305. let functional ((r1: dval env)) = (match rl with
  306. | [] -> r
  307. | (i,e) :: rl1 -> let (v, s2) = semden e r1 s in
  308. let (r2, s3) = semdr rl1 (bind(r, i, v)) s in r2) in
  309. let rec rfix = function x -> functional rfix x in (rfix, s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement