




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. Unless otherwise noted, the language under consideration is defined by the following grammar:
e ∈ Exp ::= c | x | C | (fn x=>e) | e(e) | C(e) | p(e...e) | if etest then etrue else ef alse) c ∈ Const ::= 0 | 1 |... C ∈ Constructor ::= Cons | true | false |... p ∈ P rimop ::= + | − | ∗ |...
In the program fragments you write, you are free to use any syntactic abstractions you wish including let and letrec.
Score: Maximum Score: 145
Question 1 (50 points):
λx k.f (x, (λcv 1. g(x, (λcv 2. v 1 (v 2 , (λcv 3. k v 3 ))))))
λ x.((f x) (g x))
handle e 1 e 2
evaluates e 1 in the context of an exception handler defined by e 2. If e 1 produces a value v, then the value of the entire expression is v. If e 1 raises an exception with value v, then the value of the expression is the result of applying e 2 to v. A subexpression of e 1 can raise an exception by evaluating:
raise e
The raise operator supplies the value of e to the closest dynamically-enclosing handler. One way to compile exceptions to a CPS intermediate representation is to have every CPS expres- sion take two continuations, one which represents the normal continuation, and the other which represents the exception continuation, the continuation to jump to if an exception is raised. Show the CPS translation for a core language consisting of variables, conditional expressions, (single-argument) functions, and (single-argument) function application augmented with handle and raise expressions, by defining a function Cexn that given an expression, a normal continuation, and an exception continuation returns a CPS’ed expression.
C[handle e 1 e 2 ]kn ke = C[e 1 ]knCPS [e 2 ]kn ke C[raise e]kn ke = C[e]ke ke
CPS [x ]kn ke = kn(x) CPS [λx .e]kn ke = kn(λx k′ n k′ e.CPS [e]k (^) n′ k (^) e′ ) CPS [e 1 (e2 )]kn ke = CPS [e 1 ](λc f .CPS [e 2 ](λc x .f (x , kn , ke )) ke ) ke CPS [if e 1 then e 2 elsee 3 ]kn ke = CPS [e 1 ](λc b.if b = 0 then CPS [e 2 ]kn ke else CPS [e 3 ]kn ke ) ke
Question 2. (30 points)
Consider a program P of the form begin D; S end where D consists of a collection of procedure declarations, and S is a sequence of statements drawn from the simple while language augmented with procedures and procedure calls. Procedures take a single call-by-value parameter. The label on is marks the program point upon entry to the procedure, and the label on end marks the program point on exit. The two labels associated with a call mark the program point immediately prior and immediately after the corresponding call.
e ∈ AExp ::= x | n | a 1 op a 2 b ∈ BExp ::= true | false | not b | b 1 op b 2 D ∈ Decl ::= proc p(val x) isn^ S endx^ | D D S ∈ Stmt ::= [x:=a]^ | [skip]^ | S 1 ; S 2 | call p(a){y^ ,z^ } | if [b]then S 1 else S 2 | while [b]^ do S
For each procedure of the form:
proc p(val x) isn^ S endx
the set IAV(p) contains the global variables that might be assigned directly or indirectly when p is called.
AV ([skip]^ = φ AV ([x := a]) = {x} AV (S 1 ; S 2 ) = AV (S 1 ) ∪ AV (S 2 ) AV (if[b]^ then S 1 else S 2 ) = AV (S 1 ) ∪ AV (S 2 ) AV (while [b]^ do S) = AV (S) AV ([call p(a)]{lc, lr }) = φ
CP ([skip]^ = φ CP ([x := a]) = φ CP (S 1 ; S 2 ) = CP (S 1 ) ∪ CP (S 2 ) CP (if[b]^ then S 1 else S 2 ) = CP (S 1 ) ∪ CP (S 2 ) CP (while [b]^ do S) = CP (S) CP ([call p(a)]{lc, lr }) = {p}
IAV (p) = (AV (S)/{x} ∪
{IAV (p′)|p′^ ∈ CP (S)}
where proc p(val x) isn^ S endx^ ∈ D
begin proc fib(val z) is if z<3 then call add(1) else (call fib(z-1); call fib(z-2)) end; proc add(val u) is (y := y + u; u:= 0) end; y := 0; call fib(x) end
IAV (fib) = (φ/{z}) ∪ IAV (fib) ∪ IAV (add) IAV (add) = {y, u}/{u}
The least solution is IAV (fib) = IAV (add) = {y}.
Question 4 (20 points)
Consider the inclusion of an inlining optimization into your mini-ML compiler. Inlining is an optimization that replaces a procedure definition at the call-site where it is invoked. (Assume you have some set of metrics that determine which procedures are good candidates for inlining at their call-sites.)
Question 5 (25 points)
Consider the following program fragment:
let val x = 1 val y = 2 val z = 3 val f = fn w => x = y + w in f 100 end
One possible closure-converted representation for this program fragment would be:
let val x = 1 val y = 2 val z = 3 val code = (fn env => (fn w => (#x env) + (#y env) + w)) val env = { x = x, y = y } val f = (code,env) in (#1 f) (#2 f) 100 end
(τe → τ 1 → τ 2 ) × τe
let val y = 1 in if true then fn x => x + y else fn z => z
Using a flat-closure representation we get:
let val y = 1 in if true then (fn env => (fn x => x + (#y env)), {y=y}) else (fn env => (fn z => z), { }) end
This program will not type-check. Note the problem remains even under a linked-closure strategy.