I have a type
type expr =
Const of int
| Var of string
| Bin of expr * binop * expr
also
type binop =
Plus
| Minus
| Mul
| Div
and
type value =
Int of int
| Closure of env * string option * string * expr
Binop performs an operation on two exprs and I need to write an eval function that takes in a closure and expr and returns a value
ex.
let evn = [("z1",Int 0);("x",Int 1);("y",Int 2);("z",Int 3);("z1",Int 4)];;
val evn : (string * Nano.value) list =
[("z1", Int 0); ("x", Int 1); ("y", Int 2); ("z", Int 3); ("z1", Int 4)]
# let e1 = Bin(Bin(Var "x",Plus,Var "y"), Minus, Bin(Var "z",Plus,Var "z1"));;
val e1 : Nano.expr =
Bin (Bin (Var "x", Plus, Var "y"), Minus, Bin (Var "z", Plus, Var "z1"))
# eval (evn, e1);;
- : Nano.value = Int 0
The issue Im having is that the function needs to return INT int instead of an actual into value.
This is what I wrote so far
let rec eval (evn,e) = match e with
| Const a -> Int a
| Var x-> Int (lookup (x,evn) )
| Bin( Var x, Plus, Var y) -> Int ( eval(evn,Var x) + eval(evn,Var y) )
| Bin( Var x, Minus, Var y) -> Int ( eval(evn,Var x) + eval(evn,Var y) )
| Bin( Var x, Mul, Var y) -> Int ( eval(evn,Var x) + eval(evn,Var y) )
| Bin( Var x, Div, Var y) -> Int ( eval(evn,Var x) + eval(evn,Var y) )
;;
lookup looks up x in the closure evn and returns its value the issue Im having is I need to perform integer operations in the bin matches but since eval returns types of value I cant do that. How would I change the code in the last four matches to make it be able to do the arithmetic and return type Int of int after that's done?