Mid-Term Exam 2 --------------- The mid-term exam will be open-book and open-note, but closed-computer. Any the material covered in class through 10/31 may appear on the exam. Example questions and answers: 1. Lexical scope Given an expression let x = 3 y = w in let x = 7 y = x in letrec f = proc(z)(f +(z,x)) in (f y) * Draw arrows from bound variable to binding occurrences: v---------. let x = 3 \ y = w \ in let x = 7 <-/---------------. ,---> y = x -' v---------. \ / in letrec f = proc(z)(f +(z, x)) \ ,^-----------' `----------|-. in (f y) * List free vars: w * List bound vars: x y z f * Replace bound variables with lexical indicies, @(depth, offset): let x = 3 y = 5 in let x = 7 y = @(0,0) in letrec f = proc(z)(@(1,0) +(@(0,0), @(2,0))) in (@(0,0), @(1,1)) 2. Environments and closures Given the following expression for a call-by-value language (denoted values = expressed values): let y = 2 in let f = proc(x)+(x,y) in (f 3) * Describe the closure bound to "f" at the point where "(f 3)" is the current expression. Args: x Body: +(x,y) Environment: { y = 2 } * Describe the environment at the point during evaluation where "+(x,y)" is the current expression. Environment: { y = 2, x = 3} 3. Evaluation by Interpreter Given the following expression: let y = 2 in let f = proc(x)+(x,y) in (f 3) Describe a trace of the evalaution in terms of arguments to an "eval-expression" interpreter function for every call. For literal, variable, and proc expressions, show the result. Use the notation from the lecture notes. eval expr = let y = 2 in let f = proc(x)+(x,y) in (f 3) env = { } eval expr = 2 env = { } result = 2 eval expr = let f = proc(x)+(x,y) in (f 3) env = E1 = { y = 2 , {} } eval expr = proc(x)+(x,y) env = E1 result = eval expr = (f 3) env = E2 = { f = , E1 } eval expr = f env = E2 result = eval expr = 3 env = E2 result = 3 eval expr = +(x, y) env = { x = 3 , E1 } eval expr = x env = {x = 3 , E1} result = 3 eval expr = y env = {x = 3 , E1} result = 2 4. Assignment * Problems like HW7 5. Calling conventions * Problems like Exercise 8.1 6. Types * Given an expression, find and justify its type: (proc ((int -> int) x)(x 0) proc(int y)y) - - - ---------------------/--|- ----------/- ---------------------/---|------|-----/--- | / / \ | / | / (int -> int) int | int | | | | (int -> int) -> int int -> int \ int * Given the expression (proc (T1 x)(x false) proc(T2 y)y) what type must replace T1 and T2 so that the overall expression to has a type? T1: (bool -> bool) T2: bool