



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Name:
Answer each of the questions in the space provided. If you require additional space, use the blank pages attached at the end of the exam.
In the program fragments you write, you are free to use any syntactic abstractions you wish including let and letrec.
Score: Maximum Score: 175
Question 1 (35 points):
Consider the following expression:
fun f(p as (x,y,z)) = (case p of ( ,false,true) => 1 | (false,true, ) => 2 | ( , ,false) => 3 | ( , ,true) => 4)
(The notation ”p as (x,y,z)” declares argument p to be a tuple whose first component is named x, second y, and third z.)
We would like to transform this program into an equivalent one that only uses if expressions.
fun f(x,y,z) = if x then if y then if z then 4 else 3 else if z then 1 else 3 else if y then 2 else if z then 1 else 3
Suppose a procedure p is always called with the same continuation k within some function q. Then, we can transform p into a continuation within q and eliminate the continuation argument from calls to p.
fun g (y, k) = k (y - 1) fun f (b, k) = let cont k’ (x) = k (x + 1) in if b then gNonTail (13, k’) else gNonTail (15, k’) end
Write down the original (non CPS-ed) version of this program.
fun g y = y - 1 fun f b = (if b then g 13 else g 15) + 1
fun f (b,k) = let cont k’(x) = k (x + 1) fun g(y) = k’(y - 1) in if b then gTail (13) else gTail (15)
fun loop(v,i,s) = if i = length(v) then s else loop(v,i+1, s+ sub(v,i))
fun sum (v) = loop(v,0,0)
What is the effect of performing the transformation described above on this program? In particular, what properties of the program does this transformation reveal for subsequent optimizations? The transformation moves loop inside sum, and treats it as a local continuation. The tail call to loop by sum, and the inner call to loop can now be expressed as goto’s (jumps) within sum, avoiding the overhead of a procedure call.
A useless variable is one which contributes nothing to the final outcome of a computation. For example, in the following program, the second argument to loop does not contribute to the value of the computation, and can be eliminated.
let fun loop(a,bogus,j) = if (j > 100) then a * j else loop(f(a,j), bogus*2, j+1) in loop(a,3,1) end
One transformation to eliminate useless variables would turn the above program into:
let fun loop(a,j) = if (j > 100) then a * j else loop(f(a,j), j+1) in loop(a,1) end
let fun f1(x,y) = x fun f2(x,y) = x+x fun f3(x,y) = y val g = if p(a,b) then f1 else f val h = if q(a,b) then f1 else f in g(x,h) end
let fun f1(x,y) = x fun f2(x,y) = x + x val g = if p(a,b) then f1 else f in g(x) end
((fn (x,y) => x) (1,2)) + 1
Let M be the application, and let N be the function fn(x,y) => x. M has type int. Thus, N ’s type must be int × ’a → int, and the type of (1,2) is int × ’a. Ordinary type inference would unify ’a and int, but useless variable elimination can simply instantiate ’a to empty since ’a is not subsequently used (i.e., unified) in the outer context.