MT 2009 Key
MT 2009 Key
NAME: ID:
Question 1 : (20 pts) The following type definitions in Haskell are given:
data X a = L1 ( Int , a ) | L2 ( a , Int ) Int × α + α × Int
data Y a = L3 ( a , ( a , a )) α × hα × αi
data Z a = L4 (( a , a ) , a ) hα × αi × α
data W a = L5 ( a , a , a ) α×α×α
data M a = L6 (X a ) | L7 (Y a ) Int × α + α × Int + α × hα × αi
data P a = L8 a | L9 ( a , X a) α + hα × (Int × α + α × Int)i
data Q a = L10 ( a , ( Int , a )) | L11 ( a ,( a , Int )) | L12 a
α × hInt × αi + α × hα × Inti + α
data R a = L13 (P a , X a) P α × Xα
data S a = L14 a | L15 (S a) | L16 (S a ) α + Sα + Sα
data T a = L17 a | L18 (T a) | L19 (T a ) α + Tα + Tα
data U a = L20 a | L21 (V a) | L22 (U a ) α + V α + Uα
data V a = L23 a | L24 (U a) | L25 (V a ) α + Uα + V α
Assuming your Haskell version uses structural equivalence and ignores all constructor tags, which
of the following types are equivalent?
1
Question 2 : (20 pts) a)Given the following C program:
1 int * px ,* py ; 15 pg =& b ;
2 int x ; 16 ph = m a l l o c ( sizeof ( int ));
3 int f ( int * pa ) { 17 py =& b ;
4 int * p f ; 18 * px = a ;
5 int b ; 19 px = pg ;
6 p f =& b ; 20 return & x ;
7 * px = b ; 21 }
8 px = p f ; 22 int main () {
9 py = m a l l o c ( sizeof ( int )); 23 int *pm;
10 return * pa ; 24 pm= m a l l o c ( sizeof ( int ));
11 } 25 ...
12 int * g ( int a ) { 26 return 0;
13 int * pg ,* ph ; 27 }
14 int b ;
Assume your compiler wants to avoid dangling references and try to force a rule:
“Do not assign or send address of a variable to a longer lifetime pointer or variable”
at compile time. According to this rule, assume your compiler produces warnings. Give those warnings
as line numbers plus a very short explanation produced by your compiler for the program above.
b)Assume you want to add garbage collecting feature to C. Answer the following (in a couple of short
sentences each):
2
Question 3 : (20 pts)
1 x =3
2 y =2
3 z =1
4 f a = a +1
5 let x =2
6 z = f 1 x1+ y1
7 f a = if a <=2 then 3
8 else ( f 2 ( a -1))+ z 1
9 y = f 3 x 2 +1
10 in z 2 + f 4 3
a)Give the line number of the binding occurrences of the following symbols, assuming the definitions
between let and in are sequentially composed and functions are not recursive.
x1 : 5 x2 : 5 y1 : 2 z1 : 6 z2 : 6
f1 : 4 f2 : 4 f3 : 7 f4 : 7
b)Give the line number of the binding occurrences of the following symbols, assuming the definitions
between let and in are sequentially composed and functions are recursive.
x1 : 5 x2 : 5 y1 : 2 z1 : 6 z2 : 6
f1 : 4 f2 : 7 f3 : 7 f4 : 7
c)Give the line number of the binding occurrences of the following symbols, assuming the definitions
between let and in are collaterally composed and functions are recursive.
x1 : 1 x2 : 1 y1 : 2 z1 : 3 z2 : 6
f1 : 4 f2 :4 or 7 f3 : 4 f4 : 7
d)Give the line number of the binding occurrences of the following symbols, assuming the definitions
between let and in are collateral-recursively composed and functions are recursive (Haskell style).
x1 : 5 x2 : 5 y1 : 9 z1 : 6 z2 : 6
f1 : 7 f2 : 7 f3 : 7 f4 : 7
3
Question 4 : (20 pts) Consider the following lambda expression. Show the steps and results of its
evaluation using (a) normal-order evaluation, (b) lazy evaluation, and (c) eager evaluation. +, *, / and
- are the binary arithmetic operations, 2 is a unary operator for square, ‘==’ is the boolean operator
of equality check.
λy.λx. if (x + y == x2 ) then x2 + x else 2/x endif (λz.z ∗ z 2) (λp.p − 4 4)
2/x:0 7→ error!
2/0 7→ error!
4
Question 5 : (20 pts) Suppose that we want to add some parametric polymorphism to C (not to
C++). The goal of the designers is to have more abstraction in C programs, e.g. t binop (t x, t y){...}
for a function binop to do some binary operation for any type t. Similarly, something like t id (a
x, b y, c z, d q){...} may return one of the formal parameters, where t is supposed to range over
possible formal parameter types, which can be any polytype a,b,c,d.
a)What constructions (definitions, declarations etc.) would you add to C to incorporate this change?
You don’t need to be exhaustive or complete. Just give us an idea how you would proceed.
b)Can we do something similar without introducing parametric polymorphism to C, using the currently
available declarative and definitional properties of the language? Briefly comment to what extent this
can be done.