Docsity

Practice Midterm Examination - Compiling and Programming Systems | CS 50200, Exams of Computer Science

Partial preview of the text

Download Practice Midterm Examination - Compiling and Programming Systems | CS 50200 and more Exams Computer Science in PDF only on Docsity!

CS 502. Compilers

Midterm Examination

October 14, 2008

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.

  1. (10 points) Consider a pattern-matching strategy that tests x first, then y, then z. Show the smallest program that enforces this order using conditionals instead of case.

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

  1. (10 points) Suppose you employed a strategy that tested y first, instead of x. Show the smallest program that enforces this order using conditionals instead of case. fun f(x,y,z) = if y then if x then if z then 4 else 3 else 2 else if z then 1 else 3
  2. (15 points) Precisely describe a heuristic that would lead you to choose one strategy over the other. A heuristic that considers the necessity of a term would work well: a subterm is needed when it is examined by all possible decision trees. In this example, x need not be examined if y is false. Similarly, z need not be examined if y is true. However, to eliminate the first two rows of the pattern, y’s value is required. Since patterns are examined top-down, we can argue that y is more needed than either x or z.

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.

  1. (10 points) Consider the CPS’ed program (where k and k’ represent continuations, and the notation “let cont k (x) =... ” denotes a local continuation, and gN onT ail denotes a non-tail call to function g).

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

  1. (10 points) Observe that g’s continuation is always k’. Using this observation, write the CPS’ed program that transforms g into a continuation within f.

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)

  1. (20 points) Consider the following program that sums the elements of a vector v:

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.

  1. (10 points) Should this transformation be performed on CPS’ed programs before or after closure conversion? Make sure to explain your answer. Since the transformation moves code (it takes a procedure and moves it into another procedure as a local continuation), free variable capture must be avoided. If the transformation is performed on source code, tracking where higher-order procedures are called is substantially more complicated. Expressing the transformation as a source-level rewrite further complicated by the difficulty in dis- tinguishing continuations from procedures. Thus, it is probably best to perform the transformation on closure-converted (first-order) programs.

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

  1. (15 points) What is the result of applying useless variable elimination to the following ML program:

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

  1. (25 points) Describe how to detect useless variables using polymorphic type inference. Hint: we can separate the optimization into two parts: in the first, we replace all sub-expressions that are useless with a special value (say ”empty”); in the second part, the program is rewritten to eliminate such values. Use type-inference to identify those variables that could be replaced with empty. You will need to exploit polymorphism. Perform type inference in a top-down manner. Consider the following expression:

((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.