Guest User

Untitled

a guest
Jan 23rd, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 5.69 KB | None | 0 0
  1. (* Gestione Ambiente*)
  2. type 't env = (string * 't) list
  3. exception WrongBindlist
  4. let emptyenv(x) = [("", x)]
  5. let rec applyenv(x, y) = match x with
  6.   | [(_, e)] -> e
  7.   | (i1, e1) :: x1 -> if y = i1 then e1
  8.       else applyenv(x1, y)
  9.   | [] -> failwith("wrong env")  
  10. let bind(r, l, e) = (l, e) :: r
  11. let rec bindlist(r, il, el) = match (il, el) with
  12.   | ([], []) -> r
  13.   | (i::il1, e::el1) -> bindlist(bind(r, i, e), il1, el1)
  14.   | _ -> raise WrongBindlist
  15. ;;
  16.  
  17.  
  18. (* Linguaggio Funzionale Didattico*)
  19. type ide = string
  20. type exp =
  21.     | Eint of int
  22.     | Ebool of bool
  23.     | Den of ide
  24.     | Sum of exp * exp
  25.     | Diff of exp * exp
  26.     | Prod of exp * exp
  27.     | Eq of exp * exp
  28.     | Minus of exp
  29.     | Iszero of exp
  30.     | Or of exp * exp
  31.     | And of exp * exp
  32.     | Not of exp
  33.     | Ifthenelse of exp * exp * exp
  34.     | Let of ide * exp * exp(* Dichiarazione di ide: modifica ambiente*)
  35.     | Fun of ide * exp(* Astrazione di funzione*)
  36.     | Apply of exp * exp (* Applicazione di funzione*)
  37.     | Letrec of ide(*nome fun*)*ide(*par.formale*)*exp(*body fun*)*exp(*body let*)
  38.     | Etup of tuple (*Tupla come espressione*)
  39.     | Pipe of tuple (*Concatenazione di funzioni*)
  40.     | ManyTimes of int * exp (*Esecuzione iterata di una funzione*)
  41. and tuple =
  42.     | Nil (*Tupla vuota*)
  43.     | Seq of exp * tuple (*Tupla di espressioni*)
  44. ;;
  45.  
  46.  
  47. (*Eval*)
  48. type eval=
  49.     | Int of int
  50.     | Bool of bool
  51.     | Unbound
  52.     | RecFunVal of ide * ide * exp * eval env
  53.     | Funval of efun
  54.     | ValTup of evtuple
  55. and efun = ide* exp * eval env
  56. and evtuple =
  57.     | Nil
  58.     | Seq of eval * evtuple
  59. ;;
  60.  
  61.  
  62.  
  63. (*Type Checker Dinamico*)
  64. let typecheck(x, y) = match x with
  65.   | "int" ->  
  66.       (match y with
  67.         | Int(u) -> true
  68.         | _ -> false)
  69.   | "bool" ->
  70.       (match y with
  71.         |Bool(b) -> true
  72.         |_->false)
  73.   |_->failwith("error");;
  74.  
  75.  
  76. let plus(x, y) = if typecheck("int", x) && typecheck("int", y) then  
  77.     (match (x, y) with
  78.       |(Int(u), Int(w)) -> Int(u + w)
  79.       |_->failwith("error"))
  80.   else failwith ("error");;
  81.  
  82.  
  83. let diff(x,y)=if typecheck("int",x) && typecheck("int", y) then
  84.     (match (x, y) with
  85.       |(Int(u), Int(w)) -> Int(u - w)
  86.       |_->failwith("error"))
  87.   else failwith ("type error");;
  88.  
  89.  
  90. let prod(x,y)=if typecheck("int",x) && typecheck("int", y) then
  91.     (match (x, y) with
  92.       |(Int(u), Int(w)) -> Int(u * w)
  93.       |_->failwith("error"))
  94.   else failwith ("type error");;
  95.  
  96.  
  97. let iszero(x)=if typecheck("int",x) then
  98.     (match x with
  99.       |Int(u)->if u=0 then Bool(true) else Bool(false)
  100.       |_->failwith("error"))
  101.   else failwith("type error");;
  102.  
  103.  
  104. let equ(x,y)=if typecheck("int",x) && typecheck("int", y) then
  105.     (match (x, y) with
  106.       |(Int(u), Int(w)) -> if u=w then Bool(true) else Bool(false)
  107.       |_->failwith("error"))
  108.   else failwith ("type error");;
  109.  
  110.  
  111. let minus(x)=if typecheck("int",x) then
  112.     (match x with
  113.       |Int(u)->Int(-u)
  114.       |_->failwith("error"))
  115.   else failwith("type error");;
  116.  
  117.  
  118. let et(x,y)=if typecheck("bool",x) && typecheck("bool", y) then
  119.     (match (x, y) with
  120.       |(Bool(u), Bool(w)) -> Bool(u && w)
  121.       |_->failwith("error"))
  122.   else failwith ("type error");;
  123.  
  124.  
  125. let vel(x,y)=if typecheck("bool",x) && typecheck("bool", y) then
  126.     (match (x, y) with
  127.       |(Bool(u), Bool(w)) -> Bool(u || w)
  128.       |_->failwith("error"))
  129.   else failwith ("type error");;
  130.  
  131.  
  132. let non(x)=if typecheck("bool",x) then
  133.     (match x with
  134.       |Bool(u) -> Bool(not(u))
  135.       |_->failwith("error"))
  136.   else failwith ("type error");;
  137.    
  138. (*Interprete a scoping statico*)
  139. let rec sem ((e: exp), (r: eval env)) =
  140.   (match e with
  141.     | Eint(n) -> Int(n)
  142.     | Ebool(b) -> Bool(b)
  143.     | Den(i) -> applyenv(r, i)
  144.     | Sum(a, b) ->  plus(sem(a, r), sem(b, r))
  145.     | Diff(a, b)  ->  diff(sem(a, r), sem(b, r))
  146.     | Prod(a,b)->prod(sem(a,r), sem(b,r))
  147.     | Iszero(a) -> iszero(sem(a, r))
  148.     | Eq(a, b) -> equ(sem(a, r),sem(b, r))
  149.     | Minus(a) ->  minus(sem(a, r))
  150.     | And(a, b) ->  et(sem(a, r), sem(b, r))
  151.     | Or(a, b) ->  vel(sem(a, r), sem(b, r))
  152.     | Not(a) -> non(sem(a, r))
  153.     | Ifthenelse(a, b, c) -> let g = sem(a, r) in
  154.           if typecheck("bool", g) then
  155.             (if g = Bool(true) then sem(b, r) else sem(c, r))
  156.           else failwith ("nonboolean guard")
  157.     | Let(i, e1, e2) -> sem(e2, bind (r, i, sem(e1, r)))
  158.     | Fun(i,a) -> Funval(i,a,r)
  159.     | Letrec(f, i, fBody,letBody) ->
  160.         let benv =
  161.           bind(r, f, (RecFunVal(f, i, fBody, r)))
  162.         in sem(letBody, benv)
  163.     | Etup(tup) -> (match tup with
  164.         | Seq(elem, ttup) ->
  165.             let evelem = sem(elem, r) in
  166.             let ValTup(evttup) = sem(Etup(ttup), r) in
  167.                 ValTup(Seq(evelem, evttup))
  168.         | Nil -> ValTup(Nil))
  169.     | Apply(Den f, eArg) ->
  170.         (let fclosure= sem(Den f, r) in
  171.            match fclosure with
  172.              | Funval(arg, fbody, fDecEnv) ->
  173.                  sem(fbody, bind(fDecEnv, arg, sem(eArg, r)))
  174.              | RecFunVal(f, arg, fbody, fDecEnv) ->
  175.                  let aVal= sem(eArg, r) in
  176.                  let rEnv= bind(fDecEnv, f, fclosure) in
  177.                  let aEnv= bind(rEnv, arg, aVal) in
  178.                    sem(fbody, aEnv)
  179.              | _ -> failwith("non functional value"))
  180.     | Apply(Pipe tup, eArg) -> unrollPipe tup sem(eArg, r) r
  181.     | Apply(_,_) -> failwith("not function"))
  182.    
  183. and rec unrollPipe tup evarg r = match tup with
  184.         | Seq(Den f, ttup) ->
  185.             let fclosure= sem(Den f, r) in
  186.             match fclosure with
  187.                 | Funval(arg, fbody, fDecEnv) ->
  188.                     let fres = sem(fbody, bind(fDecEnv, arg, evarg)) in
  189.                         unrollPipe Pipe(ttup) fres r
  190.                 | RecFunVal(f, arg, fbody, fDecEnv) ->
  191.                     let rEnv= bind(fDecEnv, f, fclosure) in
  192.                     let aEnv= bind(rEnv, arg, evarg) in
  193.                     let fres = sem(fbody, aEnv) in
  194.                         unrollPipe Pipe(ttup) fres r
  195.         | Seq(Pipe(ftup), ttup) ->
  196.             let fres = unrollPipe Pipe(ftup) evarg r in
  197.                 unrollPipe Pipe(ttup) fres r
  198.         | Nil -> evarg
  199.         | _ -> failwith("bad Pipe")
  200. ;;
  201.  
  202.  
  203. (*Prova valutazione tupla*)
  204. sem(
  205.     Etup(
  206.         Seq( Sum(Eint(10), Eint(2)),
  207.         Seq( Eint(29),
  208.             Nil))),
  209.     (emptyenv Unbound));;
Add Comment
Please, Sign In to add comment