Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type ide = string;;
- type exp =
- EInt of int |
- EBool of bool |
- EString of string |
- Den of ide |
- Addizione of exp * exp |
- Sottrazione of exp * exp |
- Moltiplicazione of exp * exp |
- Divisione of exp * exp |
- Quadrato of exp |
- Cubo of exp |
- Esp of exp * exp |
- Zero of exp |
- Equivalente of exp * exp |
- Maggiore of exp * exp |
- Minore of exp * exp |
- MaggioreUg of exp * exp |
- MinoreUg of exp * exp |
- Meno of exp |
- Not of exp |
- And of exp * exp |
- Or of exp * exp|
- Impl of exp * exp|
- Xor of exp * exp |
- Nand of exp * exp |
- IfThenElse of exp * exp * exp |
- Let of ide * exp * exp |
- Fun of ide * exp |
- Apply of exp * exp |
- LetRec of ide * ide * exp * exp;;
- type 't env = ide -> 't;;
- 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
- and evFun = ide * exp * evT env;;
- let emptyenv = fun x -> Unbound;;
- let applyenv (r : 't env) (i : ide) = r i;;
- let bind (r : 't env) (i : ide) (v : 't) = fun x -> if (x = i) then v else applyenv r i;;
- let typecheck (s : string) (e : evT) : bool =
- match s with
- "int" -> (match e with
- Int(_) -> true |
- (_) -> false) |
- "string" -> (match e with
- String(_) -> true |
- (_) -> false) |
- "bool" -> (match e with
- Bool(_) -> true |
- (_) -> false) |
- _ -> failwith("Errore, tipo non valido");;
- (*let stringa x = match x with
- Int a -> "Intero"
- | String a -> "Stringa"
- | Bool a -> "Booleano"
- | Unbound -> "Unbound ci scommetto 10 euro";;*)
- let add x y = if (typecheck "int" x && typecheck "int" y) then
- (match (x, y) with
- (Int(a),Int(b)) -> Int(a+b) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let sott x y = if (typecheck "int" x && typecheck "int" y) then
- (match (x, y) with
- (Int(a),Int(b)) -> Int(a-b) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let molt x y = if (typecheck "int" x && typecheck "int" y) then
- (match (x, y) with
- (Int(a),Int(b)) -> Int(a*b) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let div x y = if (typecheck "int" x && typecheck "int" y) then
- (match (x, y) with
- (Int(a),Int(b)) -> if (b != 0) then Int(a+b) else failwith("Errore divisione per zero") |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let quad x = if (typecheck "int" x) then
- (match x with
- Int(a) -> Int(a*a) |
- (_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let cub x = if (typecheck "int" x) then
- (match x with
- (Int(a)) -> Int(a*a*a) |
- (_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let rec pow x y = match (x,y) with
- (0,0) -> failwith("Che stracazzo volevi fare") |
- (0,_) -> 0 |
- (1,_) -> 1 |
- (_,0) -> 1 |
- (x,1) -> x |
- (x,y) -> x * pow x (y-1);;
- let espon x y = if (typecheck "int" x && typecheck "int" y) then
- (match (x, y) with
- (Int(a),Int(b)) -> Int(pow a b) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let iszero x = if (typecheck "int" x) then
- (match x with
- (Int(a)) -> if x = Int(0) then Bool(true) else Bool(false) |
- (_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let meno x = if (typecheck "int" x) then
- (match x with
- (Int(a)) -> Int(-a) |
- (_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let equiv x y = if (typecheck "int" x && typecheck "int" y) then
- (match (x, y) with
- (Int(a),Int(b)) -> Bool(a = b) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let magg x y = if (typecheck "int" x && typecheck "int" y) then
- (match (x, y) with
- (Int(a),Int(b)) -> Bool(a > b) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let minn x y = if (typecheck "int" x && typecheck "int" y) then
- (match (x, y) with
- (Int(a),Int(b)) -> Bool(a < b)|
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let maggu x y = if (typecheck "int" x && typecheck "int" y) then
- (match (x, y) with
- (Int(a),Int(b)) -> Bool(a >= b) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let minu x y = if (typecheck "int" x && typecheck "int" y) then
- (match (x, y) with
- (Int(a),Int(b)) -> Bool(a <= b) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let et x y = if (typecheck "bool" x && typecheck "bool" y) then
- (match (x, y) with
- (Bool(a),Bool(b)) -> Bool(a && b) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let oooh x y = if (typecheck "bool" x && typecheck "bool" y) then
- (match (x, y) with
- (Bool(a),Bool(b)) -> (Bool(a || b)) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let impl x y = if (typecheck "bool" x && typecheck "bool" y) then
- (match (x, y) with
- (Bool(a),Bool(b)) -> if (a && not(b)) then Bool(false) else Bool(true) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let nooh x = if (typecheck "bool" x) then
- (match x with
- Bool(a) -> Bool(not(a))
- | (_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let nand x y = if (typecheck "bool" x && typecheck "bool" y) then
- (match (x, y) with
- (Bool(a),Bool(b)) -> Bool((not(a)&¬(b)) || (not(a)&&b) || (a&¬(b))) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let escorr x y = if (typecheck "bool" x && typecheck "bool" y) then
- (match (x, y) with
- (Bool(a),Bool(b)) -> Bool(not((a&&b) || (not(a)&¬(b)))) |
- (_,_) -> failwith("Match sbagliato"))
- else failwith("Errore di tipo");;
- let rec eval (x:exp) (r: 't env) = match x with
- EInt n -> Int n |
- EBool n -> Bool n |
- EString n -> String n |
- Den n -> applyenv r n |
- Addizione (n,m) -> add(eval n r)(eval m r) |
- Sottrazione (n,m) -> sott(eval n r)(eval m r)|
- Moltiplicazione (n,m) -> molt(eval n r)(eval m r)|
- Divisione (n,m) -> div(eval n r)(eval m r)|
- Maggiore (n,m) -> magg(eval n r)(eval m r)|
- Minore (n,m) -> minn(eval n r)(eval m r)|
- MinoreUg (n,m) -> minu(eval n r)(eval m r)|
- MaggioreUg (n,m) -> maggu(eval n r)(eval m r)|
- Equivalente (n,m) -> equiv(eval n r)(eval m r)|
- Cubo n -> cub (eval n r)|
- Quadrato n -> quad (eval n r)|
- Esp (n,m) -> espon(eval n r)(eval m r)|
- Zero n -> meno (eval n r)|
- Meno n -> meno (eval n r)|
- Not n -> nooh (eval n r)|
- And (n,m) -> et(eval n r)(eval m r)|
- Or (n,m) -> oooh(eval n r)(eval m r)|
- Xor (n,m) -> escorr(eval n r)(eval m r)|
- Nand (n,m) -> nand(eval n r)(eval m r)|
- Impl (n,m) -> impl(eval n r)(eval m r)|
- IfThenElse (cond,thn,els) -> let c = eval cond r in
- if c=Bool(true) then eval thn r else eval els r |
- Let (ide,e1,e2) -> eval e2 (bind r ide (eval e1 r)) |
- Fun(i,a) -> Funval (i,a,r) |
- Letrec(f, i,fBody,letBody) -> let benv = bind(r, f, (Recfunval(f, i, fBody, r)))in eval(letBody, benv)
- Apply (e1,e2) -> (*Devo dichiarare la chiusura della funzione*)
- let chiusura = (eval e1 r) in
- (match chiusura with
- Funval (id,corpo,r1) ->
- eval corpo (bind r1 id (eval e2 r)) |
- _ -> failwith("Errore, non รจ una funzione")) |
- (*TEST CASES*)
- let ambiente = emptyenv;;
- (*If then else, let, add, cubo, quadrato WORKS FINE*)
- let espr = IfThenElse(MaggioreUg(EInt 5, EInt 6),Let("x",Addizione(EInt 0, EInt 3),Cubo(Den "x")),Quadrato(Den "x"));;
- eval espr ambiente;;
- (*Tutte le operazioni booleane YEY*)
- let testBool1 = Impl(EBool true, EBool true);;
- eval testBool1 ambiente;;
- let funzTest = Fun("y",IfThenElse(MaggioreUg(EInt 5, EInt 6),Let("x",Addizione(EInt 0, EInt 3),Cubo(Den "x")),Quadrato(EInt 5)));;
- eval (Apply(funzTest, EInt 7)) ambiente;;
- let gabri = Fun("x", Addizione(Den "x", EInt 1));;
- let chiamata = Apply(gabri,(Moltiplicazione(EInt 7, EInt 12)));;
- eval chiamata ambiente;;
- let trasformaInAssoluto = Fun("x",IfThenElse(MaggioreUg(Den "x", EInt 0),Den "x", Meno(Den "x")));;
- let chiamata = Apply(trasformaInAssoluto, EInt (-3));;
- eval chiamata ambiente;;
- let test = Let("f",
- Fun("x",
- Let("g",
- Fun("y",
- Addizione(
- Den "y", EInt 2
- )
- )
- ,Den "g"
- )
- )
- ,Apply(Apply(Den "f", EInt 2),EInt 4));;
- let ricorsione = Apply(LetRec("fact",
- "x",
- IfThenElse(Equivalente(Den "x",
- EInt 0),
- EInt 1,
- Moltiplicazione(Den "x",
- Apply(Den "fact",
- Sottrazione(Den "x",
- EInt 1)))),
- Apply(Den "fact", EInt 5)),EInt 6);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement