0% found this document useful (0 votes)
9 views5 pages

MT 2009 Key

Uploaded by

elvanbuseanli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

MT 2009 Key

Uploaded by

elvanbuseanli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

METU, Department of Computer Engineering

CENG 242 - PROGRAMMING LANGUAGES CONCEPTS


MID TERM EXAM (Spring 2009)
CLOSED NOTES AND BOOKS, 100 pts, DURATION: 120 mins, 5 questions, 5 pages)

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?

X α , Y α , Z α , W α , P α , Q α , R α , S α , T α , U α , V α , X Int , M Int , X (Int,Int) , P Int ,


P (Int,Int) , Q Int , R Int

Note: Write as equivalence classes like:


A≡B≡C
D
E≡F ≡G≡H







Sα≡Tα≡Uα≡Vα
X Int
M Int
X (Int,Int)
P Int
P (Int,Int)
Q Int
R Int

(P ≡ Q and P Int ≡ Q Int, if cartesian is assumed to be distributive over disjoint union)

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.

8: local pointer assigned to global pointer

17: local variable address assigned to global pointer

19: local pointer assigned to global pointer

b)Assume you want to add garbage collecting feature to C. Answer the following (in a couple of short
sentences each):

i. Which extra information you need to keep for each variable?


(Number of references/pointers to the variable or
The list of references/pointers to the variable)
ii. What basic operations does garbage collector do in an assignment like:
px=&a;
First (decrement reference count / delete from list) of previous dereference of px, deallocate (if 0 /
if empty). Then (increment reference count of a/add px to list of references to a)
iii. Does garbage collector need to do extra things for composite values (structs)? If yes what?
If structures has reference/pointer members their references need to be handled. Structure members
should be recursed

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)

a) λx. if (x + (λz.z ∗ z 2) == x2 ) then x2 + x else 2/x endif (λp.p − 4 4) 7→

if ( (λp.p − 4 4) + (λz.z ∗ z 2) == (λp.p − 4 4)2 )

then (λp.p − 4 4)2 + (λp.p − 4 4) else 2/ (λp.p − 4 4) endif 7→

if ( (4 − 4) + (λz.z ∗ z 2) == (λp.p − 4 4)2 ) then ... else ... endif 7→

if ( (4 − 4) + 2 ∗ 2) == (λp.p − 4 4)2 ) then ... else ... endif 7→

if ( (4 − 4) + 2 ∗ 2) == (4 − 4)2 ) then ... else ... endif 7→

if f alse then ... else ... endif 7→ 2/(λp.p − 4 4) 7→

2/(4 − 4) 7→ 2/0 7→ error!

b) λx. if (x + y:(λz.z ∗ z 2) == x2 ) then x2 + x else 2/x endif (λp.p − 4 4) 7→

if ( x:(λp.p − 4 4) + y:(λz.z ∗ z 2) == x:(λp.p − 4 4)2 )


then x:(λp.p − 4 4)2 + x:(λp.p − 4 4) else 2/x:(λp.p − 4 4) endif 7→

if ( x:(4 − 4) + y:(λz.z ∗ z 2) == x:(λp.p − 4 4)2 )


then x:(λp.p − 4 4)2 + x:(λp.p − 4 4) else 2/x:(λp.p − 4 4) endif 7→

if ( x:0 + y:(λz.z ∗ z 2) == x:02 ) then x:02 + x:0 else 2/x:0 endif 7→

if ( x:0 + y:(2 ∗ 2) == x:02 ) then x:02 + x:0 else 2/x:0 endif 7→

if f alse then x:02 + x:0 else 2/x:0 endif 7→

2/x:0 7→ error!

c) λy.λx. if (x + y == x2 ) then x2 + x else 2/x endif (2 ∗ 2) (λp.p − 4 4) 7→

λx. if (x + 4 == x2 ) then x2 + x else 2/x endif (λp.p − 4 4) 7→

λx. if (x + 4 == x2 ) then x2 + x else 2/x endif (4 − 4) 7→

if (0 + 4 == 02 ) then 02 + 0 else 2/0 endif 7→

if f alse then 02 + 0 else 2/0 endif 7→

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.

You might also like