Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

F# language: 2 An expression language with variables In this problem we will con

ID: 3749910 • Letter: F

Question

F# language: 2 An expression language with variables In this problem we will consider a variation of the datatype expr where we represent operators with an enumeration type instead of the string type and we add support for variables, to represent arithmet ic expressions with variables. type oper Neg I Not type oper2 Add Mul Sub Less I Eq I And type aexpr I C of int I V of string l Op1 of operiaexpr I Op2 of oper2 xpaexpr With the two datatypes above, an expression like -(y +3), say, is represented as Op2 (Mul, Op1(Neg, Var "x"), Op2(Add, Var "y" C 3)) Similarly, an expression like-(int for programs written as aexpr expres- sions. Function aeval takes an environment e and an expression a as input and returns the value of a in that environment The function should evaluate variables in a as specified in e, failing with failwith if a contains a variable that is undefined in e. It should interpret Neg, Add, Mul, and Sub respectively as integer negation, addition, multiplication and subtraction. It should interpret the remain operators as the Boolean operators in C. Namely, Less (e,e evaluates to 1 if e evaluates to a number smaller than the number e2 evaluates to, and it evaluates to 0 otherwise e Not e evaluates to 1 if e evaluates to 0 and evaluates to 1 otherwise Eq(e, 2) evaluates to 1 if e and e2 evaluate to the same number, and evaluates to 0 otherwise; And (e, e2) evaluates to 0 if e1 or e2 evaluates to 0, and evaluates to 1 otherwise. You can use the provided function find to lookup the value of a variable in an environment

Explanation / Answer

As per given data we can assume three F# variables are e1,e2 and e3 of type aepr

The values for these three variables as follows

e1 = v-(w+z)

e2 = 2*(v-4*w-z)

e3 = x+y+z+v

according to usual procedure rules between operators and treating + and binary – as left-associative operators

expressions of required F# variables as follows

e1 = Op2(Sub,Var "v",Op2(Add,Var "w" ,Var "z"))

e2 = Op2(Mul,C 2,Op2(Mul,Op2(Sub,Var "v",C 4),Op2(Sub,Var "w",Var "z")

e3 = Op2(Add,Var "x",Op2(Add,Var "y",Op2(Add,Var "z",Var "v")))

Note: type aexpr=

| C of int

| V of string

| op1 of oper1*aexpr

| op2 of oper2*aexpr*aexpr