Exercise 11 Name
____________________ Points __/33
Document last modified:
datatype AST =
Const of int |
Plus of AST * AST |
Times of AST * AST;
- fun val1 (Const(n)) = Const(n)
- | val1 (Plus(Const(v1),Const(v2))) = Const(v1+v2)
- | val1 (Plus(E1,E2))=val1(Plus(val1(E1),val1(E2)))
- | val1 (Times(Const(v1),Const(v2))) = Const(v1*v2)
- | val1 (Times(E1,E2))=val1(Times(val1(E1),val1(E2)));
Language 1
- (3) Give the following in Language 1:
- 6 + 5
- 6 + 5 * 3
- (4) Trace the following executions:
- val1(Const 3);
- val1(Times(Const 3,Const 4);
- val1(Times(Const 2,Plus(Const 3,Const 4));
datatype AST =
Const of int |
Var of string |
Plus of AST * AST |
Times of AST * AST |
Let of string * AST * AST;
- fun lookup (V,(var,value)::T) = if (V=var) then value else lookup(V,T);
- fun val2 (Const(X), _) = Const(X)
- | val2 (Var(V), Context) = lookup(V,Context)
- | val2 (Plus(Const(X),Const(Y)),_) = Const(X+Y)
- | val2 (Plus(X, Y), Context) =
val2(Plus(val2(X,Context),val2(Y,Context)), Context)
- | val2 (Times(Const(X),Const(Y)),_) = Const(X*Y)
- | val2 (Times(X, Y), Context) =
val2(Times(val2(X,Context),val2(Y,Context)), Context)
- | val2 (Let(V,Exp1,Exp2), Context) = val2(Exp2, (V,
val2(Exp1, Context))::Context);
Language 2
- (1) Give the ML syntax for:
val2(Let("X", Const 2, Times(Var "X“,Const
3)), []);
- (1) Give the Language Two syntax for:
let val X = 3 in X*5 end
- (1) Give the Language Two syntax for:
let val X = 3 in X*5+X end
- (1) Which line(s) reduces variables?
- (1) Which line(s) reduces Plus?
- (1) Which line(s) reduces Let?
- (1) Where exactly is a variable name bound to a value?
- (2) Trace lines executed by:
val2(Var “Y", [(“Y",Const 6)]);
- (4) What is V, Exp1, Exp2, Context for:
val2(Let(“Y",Const 3,Var “Y"),[(“Y",Const
6)]);
- (2) What is the environment during execution of line 8 for:
val2(Let(“Y",Const 3,Var “Y"),[(“Y",Const
6)]);
- (2) Trace lines executed by:
val2(Let(“Y",Const 3,Var “Y"),[(“Y",Const
6)]);
datatype AST =
Const of int |
Var of string |
Plus of AST * AST |
Times of AST * AST |
Let of string * AST * AST |
Fn of string * AST |
Apply of AST * AST;
- fun lookup (V,(var,value)::T) = if (V=var) then value else lookup(V,T);
- fun val3 (Const X, _) = Const(X)
- | val3 (Var V, Context) = lookup(V, Context)
- | val3 (Plus(Const X,Const Y),_) = Const(X+Y)
- | val3 (Plus(X, Y), Context) =
val3(Plus(val3(X,Context),val3(Y,Context)), Context)
- | val3 (Times(Const X,Const Y),_) = Const(X*Y)
- | val3 (Times(X, Y), Context) =
val3(Times(val3(X,Context),val3(Y,Context)), Context)
- | val3 (Let(V,Exp1,Exp2), Context) = val3(Exp2, (V,
val3(Exp1, Context))::Context)
- | val3 (Fn(Formal, Body), _) = Fn(Formal, Body)
- | val3 (Apply( Fn(Formal, Body), Actual), Context) =
val3( Body,(Formal,val3(Actual, Context))::Context)
- | val3 (Apply( Var(V), Actual), Context) = val3( Apply(
val3(Var(V), Context), Actual), Context);
Language 3
- (2) What is the result of:
val3(Fn(“z”, Plus(Var “z”, Const 2)), []);
- (4) What are the values of Formal, Body, Actual, Context?
val3( Apply( Fn(“x”, Plus(Var “x”, Const 4)), Const 2), []);
- (3) What are the first 3 line(s) in the reduction of:
val3( Apply( Fn(“x”,
Plus(Var “x”, Const 4)), Const 2), []);