Advertisement
Guest User

Untitled

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