Advertisement
Luiggi_98

camel2

Dec 12th, 2018
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 11.00 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. Addizione of exp * exp |
  8. Sottrazione of exp * exp |
  9. Moltiplicazione of exp * exp |
  10. Divisione of exp * exp |
  11. Quadrato of exp |
  12. Cubo of exp |
  13. Esp of exp * exp |
  14. Zero of exp |
  15. Equivalente of exp * exp |
  16. Maggiore of exp * exp |
  17. Minore of exp * exp |
  18. MaggioreUg of exp * exp |
  19. MinoreUg of exp * exp |
  20. Meno of exp |
  21. Not of exp |
  22. And of exp * exp |
  23. Or of exp * exp|
  24. Impl of exp * exp|
  25. Xor of exp * exp |
  26. Nand of exp * exp |
  27. IfThenElse of exp * exp * exp |
  28. Let of ide * exp * exp |
  29. Fun of ide * exp |
  30. Apply of exp * exp |
  31. LetRec of ide * ide * exp * exp;;
  32.  
  33. type 't env = ide -> 't;;
  34.  
  35. type evT = Bool of bool | Int of int | Den of string | String of string | Funval of evFun | RecFunval of ide * ide * exp * evT env | Unbound
  36. and evFun = ide * exp * evT env;;
  37.  
  38.  
  39. let emptyenv = fun x -> Unbound;;
  40.  
  41. let applyenv (r : 't env) (i : ide) = r i;;
  42.  
  43. let bind (r : 't env) (i : ide) (v : 't) = fun x -> if (x = i) then v else applyenv r i;;
  44.  
  45. let typecheck (s : string) (e : evT) : bool =
  46.     match s with
  47.         "int" -> (match e with  
  48.             Int(_) -> true |
  49.             (_) -> false) |
  50.         "string" -> (match e with
  51.             String(_) -> true |
  52.             (_) -> false) |
  53.         "bool" -> (match e with
  54.             Bool(_) -> true |
  55.             (_) -> false) |
  56.         _ -> failwith("Errore, tipo non valido");;
  57.  
  58. (*let stringa x = match x with
  59.     Int a -> "Intero"
  60.     | String a -> "Stringa"
  61.     | Bool a -> "Booleano"
  62.     | Unbound -> "Unbound ci scommetto 10 euro";;*)
  63.  
  64. let add x y = if (typecheck "int" x && typecheck "int" y) then
  65.                     (match (x, y) with
  66.                         (Int(a),Int(b)) -> Int(a+b) |
  67.                         (_,_) -> failwith("Match sbagliato"))
  68.               else failwith("Errore di tipo");;
  69. let sott x y = if (typecheck "int" x && typecheck "int" y) then
  70.                     (match (x, y) with
  71.                         (Int(a),Int(b)) -> Int(a-b) |
  72.                         (_,_) -> failwith("Match sbagliato"))
  73.               else failwith("Errore di tipo");;
  74. let molt x y = if (typecheck "int" x && typecheck "int" y) then
  75.                     (match (x, y) with
  76.                         (Int(a),Int(b)) -> Int(a*b) |
  77.                         (_,_) -> failwith("Match sbagliato"))
  78.               else failwith("Errore di tipo");;
  79. let div x y = if (typecheck "int" x && typecheck "int" y) then
  80.                     (match (x, y) with
  81.                         (Int(a),Int(b)) -> if (b != 0) then Int(a+b) else failwith("Errore divisione per zero") |
  82.                         (_,_) -> failwith("Match sbagliato"))
  83.               else failwith("Errore di tipo");;
  84. let quad x = if (typecheck "int" x) then
  85.                     (match x with
  86.                         Int(a) -> Int(a*a) |
  87.                         (_) -> failwith("Match sbagliato"))
  88.               else failwith("Errore di tipo");;
  89. let cub x = if (typecheck "int" x) then
  90.                     (match x with
  91.                         (Int(a)) -> Int(a*a*a) |
  92.                         (_) -> failwith("Match sbagliato"))
  93.               else failwith("Errore di tipo");;
  94.  
  95. let rec pow x y = match (x,y) with
  96.                     (0,0) -> failwith("Che stracazzo volevi fare") |
  97.                     (0,_) -> 0 |
  98.                     (1,_) -> 1 |
  99.                     (_,0) -> 1 |
  100.                     (x,1) -> x |
  101.                     (x,y) -> x * pow x (y-1);;
  102.  
  103. let espon x y = if (typecheck "int" x && typecheck "int" y) then
  104.                     (match (x, y) with
  105.                         (Int(a),Int(b)) -> Int(pow a b) |
  106.                         (_,_) -> failwith("Match sbagliato"))
  107.               else failwith("Errore di tipo");;
  108.  
  109. let iszero x = if (typecheck "int" x) then
  110.                     (match x with
  111.                         (Int(a)) -> if x = Int(0) then Bool(true) else Bool(false) |
  112.                         (_) -> failwith("Match sbagliato"))
  113.               else failwith("Errore di tipo");;
  114. let meno x = if (typecheck "int" x) then
  115.                     (match x with
  116.                         (Int(a)) -> Int(-a) |
  117.                         (_) -> failwith("Match sbagliato"))
  118.               else failwith("Errore di tipo");;
  119.  
  120. let equiv x y = if (typecheck "int" x && typecheck "int" y) then
  121.                     (match (x, y) with
  122.                         (Int(a),Int(b)) -> Bool(a = b) |
  123.                         (_,_) -> failwith("Match sbagliato"))
  124.               else failwith("Errore di tipo");;
  125.  
  126. let magg x y = if (typecheck "int" x && typecheck "int" y) then
  127.                     (match (x, y) with
  128.                         (Int(a),Int(b)) -> Bool(a > b) |
  129.                         (_,_) -> failwith("Match sbagliato"))
  130.               else failwith("Errore di tipo");;
  131.  
  132. let minn x y = if (typecheck "int" x && typecheck "int" y) then
  133.                     (match (x, y) with
  134.                         (Int(a),Int(b)) -> Bool(a < b)|
  135.                         (_,_) -> failwith("Match sbagliato"))
  136.               else failwith("Errore di tipo");;
  137. let maggu x y = if (typecheck "int" x && typecheck "int" y) then
  138.                     (match (x, y) with
  139.                         (Int(a),Int(b)) -> Bool(a >= b) |
  140.                         (_,_) -> failwith("Match sbagliato"))
  141.               else failwith("Errore di tipo");;
  142. let minu x y = if (typecheck "int" x && typecheck "int" y) then
  143.                     (match (x, y) with
  144.                         (Int(a),Int(b)) -> Bool(a <= b) |
  145.                         (_,_) -> failwith("Match sbagliato"))
  146.               else failwith("Errore di tipo");;
  147. let et x y = if (typecheck "bool" x && typecheck "bool" y) then
  148.                     (match (x, y) with
  149.                         (Bool(a),Bool(b)) -> Bool(a && b) |
  150.                         (_,_) -> failwith("Match sbagliato"))
  151.               else failwith("Errore di tipo");;
  152. let oooh x y = if (typecheck "bool" x && typecheck "bool" y) then
  153.                     (match (x, y) with
  154.                         (Bool(a),Bool(b)) -> (Bool(a || b)) |
  155.                         (_,_) -> failwith("Match sbagliato"))
  156.               else failwith("Errore di tipo");;
  157. let impl x y = if (typecheck "bool" x && typecheck "bool" y) then
  158.                     (match (x, y) with
  159.                         (Bool(a),Bool(b)) -> if (a && not(b)) then Bool(false) else Bool(true) |
  160.                         (_,_) -> failwith("Match sbagliato"))
  161.               else failwith("Errore di tipo");;
  162. let nooh x = if (typecheck "bool" x) then
  163.                 (match x with
  164.                     Bool(a) -> Bool(not(a))
  165.                 | (_) -> failwith("Match sbagliato"))
  166.              else failwith("Errore di tipo");;
  167. let nand x y = if (typecheck "bool" x && typecheck "bool" y) then
  168.                     (match (x, y) with
  169.                         (Bool(a),Bool(b)) -> Bool((not(a)&&not(b)) || (not(a)&&b) || (a&&not(b))) |
  170.                         (_,_) -> failwith("Match sbagliato"))
  171.               else failwith("Errore di tipo");;
  172. let escorr x y = if (typecheck "bool" x && typecheck "bool" y) then
  173.                     (match (x, y) with
  174.                         (Bool(a),Bool(b)) -> Bool(not((a&&b) || (not(a)&&not(b)))) |
  175.                         (_,_) -> failwith("Match sbagliato"))
  176.               else failwith("Errore di tipo");;
  177.  
  178. let rec eval (x:exp) (r: 't env) = match x with
  179.     EInt n -> Int n |
  180.     EBool n -> Bool n |
  181.     EString n -> String n |
  182.     Den n -> applyenv r n |
  183.     Addizione (n,m) -> add(eval n r)(eval m r) |
  184.     Sottrazione (n,m) -> sott(eval n r)(eval m r)|
  185.     Moltiplicazione (n,m) -> molt(eval n r)(eval m r)|
  186.     Divisione (n,m) -> div(eval n r)(eval m r)|
  187.     Maggiore (n,m) -> magg(eval n r)(eval m r)|
  188.     Minore (n,m) -> minn(eval n r)(eval m r)|
  189.     MinoreUg (n,m) -> minu(eval n r)(eval m r)|
  190.     MaggioreUg (n,m) -> maggu(eval n r)(eval m r)|
  191.     Equivalente (n,m) -> equiv(eval n r)(eval m r)|
  192.     Cubo n -> cub (eval n r)|
  193.     Quadrato n -> quad (eval n r)|
  194.     Esp (n,m) -> espon(eval n r)(eval m r)|
  195.     Zero n -> meno (eval n r)|
  196.     Meno n -> meno (eval n r)|
  197.     Not n -> nooh (eval n r)|
  198.     And (n,m) -> et(eval n r)(eval m r)|
  199.     Or (n,m) -> oooh(eval n r)(eval m r)|
  200.     Xor (n,m) -> escorr(eval n r)(eval m r)|
  201.     Nand (n,m) -> nand(eval n r)(eval m r)|
  202.     Impl (n,m) -> impl(eval n r)(eval m r)|
  203.     IfThenElse (cond,thn,els) -> let c = eval cond r in
  204.                                     if c=Bool(true) then eval thn r else eval els r |
  205.     Let (ide,e1,e2) -> eval e2 (bind r ide (eval e1 r)) |
  206.     Fun(i,a) -> Funval (i,a,r) |
  207.     Letrec(f, i,fBody,letBody) -> let benv = bind(r, f, (Recfunval(f, i, fBody, r)))in eval(letBody, benv)
  208.     Apply (e1,e2) -> (*Devo dichiarare la chiusura della funzione*)
  209.             let chiusura = (eval e1 r) in
  210.                 (match chiusura with
  211.                 Funval (id,corpo,r1) ->
  212.                     eval corpo (bind r1 id (eval e2 r)) |
  213.                
  214.                 _ -> failwith("Errore, non รจ una funzione")) |
  215.    
  216.  
  217.  
  218.     (*TEST CASES*)
  219.     let ambiente = emptyenv;;
  220.     (*If then else, let, add, cubo, quadrato WORKS FINE*)
  221.     let espr = IfThenElse(MaggioreUg(EInt 5, EInt 6),Let("x",Addizione(EInt 0, EInt 3),Cubo(Den "x")),Quadrato(Den "x"));;
  222.     eval espr ambiente;;
  223.     (*Tutte le operazioni booleane YEY*)
  224.     let testBool1 = Impl(EBool true, EBool true);;
  225.     eval testBool1 ambiente;;
  226.  
  227.     let funzTest = Fun("y",IfThenElse(MaggioreUg(EInt 5, EInt 6),Let("x",Addizione(EInt 0, EInt 3),Cubo(Den "x")),Quadrato(EInt 5)));;
  228.     eval (Apply(funzTest, EInt 7)) ambiente;;
  229.     let gabri = Fun("x", Addizione(Den "x", EInt 1));;
  230.     let chiamata = Apply(gabri,(Moltiplicazione(EInt 7, EInt 12)));;
  231.     eval chiamata ambiente;;
  232.  
  233.     let trasformaInAssoluto = Fun("x",IfThenElse(MaggioreUg(Den "x", EInt 0),Den "x", Meno(Den "x")));;
  234.     let chiamata = Apply(trasformaInAssoluto, EInt (-3));;
  235.     eval chiamata ambiente;;
  236.     let test = Let("f",
  237.                     Fun("x",
  238.                         Let("g",
  239.                             Fun("y",
  240.                                 Addizione(
  241.                                     Den "y", EInt 2
  242.                                     )
  243.                             )
  244.                             ,Den "g"
  245.                         )
  246.                     )
  247.                     ,Apply(Apply(Den "f", EInt 2),EInt 4));;
  248.    
  249.     let ricorsione = Apply(LetRec("fact",
  250.                                     "x",
  251.                                         IfThenElse(Equivalente(Den "x",
  252.                                                                 EInt 0),
  253.                                         EInt 1,
  254.                                         Moltiplicazione(Den "x",
  255.                                                             Apply(Den "fact",
  256.                                                                             Sottrazione(Den "x",
  257.                                                                                             EInt 1)))),
  258.                                                                                                 Apply(Den "fact", EInt 5)),EInt 6);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement