0% found this document useful (0 votes)
34 views9 pages

11 Midterm

This document is a midterm exam for CS 320, Spring 2011, consisting of various questions related to programming languages and their semantics. It includes grammar analysis, function definitions, shadowing examples, and evaluation traces in the context of FWAE and RCFAE languages. Students are instructed to complete the exam in 120 minutes, with the option to write answers in Korean.

Uploaded by

zasiman0
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)
34 views9 pages

11 Midterm

This document is a midterm exam for CS 320, Spring 2011, consisting of various questions related to programming languages and their semantics. It includes grammar analysis, function definitions, shadowing examples, and evaluation traces in the context of FWAE and RCFAE languages. Students are instructed to complete the exam in 120 minutes, with the option to write answers in Korean.

Uploaded by

zasiman0
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/ 9

Midterm Exam

CS 320, Spring 2011


Student id: Name:

Instructions: You have 120 minutes to complete this closed-book, closed-note, closed-computer exam. Please
write all answers in the provided space. You’re free to write your answers in Korean.

1) (5pts) Given the following grammar:

hcoffeei ::= espresso


| hmilki on hcoffeei
| hcoffeei on hmilki
| hflavori on hcoffeei
hmilki ::= milk-foam
| steamed-milk
hflavori ::= caramel
| cinnamon
| cocoa-powder
| chocolate-syrup

Which of the following are examples of hcoffeei?


a) caramel latte macchiato
b) espresso
c) steamed-milk on caramel on milk-foam on espresso
d) chocolate-syrup on cocoa-powder on cinnamon on milk-foam on steamed-milk on espresso
e) steamed-milk on espresso on chocolate-syrup

2) (5pts) Explain why the following is or is not hcoffeei:

cocoa-powder on milk-foam on steamed-milk on espresso

1
3) (10pts) Given the following expression in the FWAE language:

{with {h {fun {x} {f x}}}


{with {f {fun {x} {h x}}}
{with {y {with {f {fun {x} {f {+ x n}}}}
{f y}}}
{+ y m}}}}
a) Draw arrows on the above expression from each bound variable to its binding occurrence.
b) List the names of free variables:

and bound variables:

4) (10pts) The following function parse-fd takes an s-expression, which represents a function definition
in the F1WAE language, and returns a corresponding abstract syntax node:

; parse-fd: sexp -> FunDef


(define (parse-fd sexp)
(match sexp
[(list ’deffun (list f x) b) (fundef f x (parse b))]))

a) What is FunDef?

b) What is deffun?

c) What is fundef?
5) (10pts) Which of the following are examples of shadowing?
a) {with {’x {with {’x 3} {- 5 ’x}}}
{+ 1 ’x}}

b) {with {’x 3}
{with {’y 5} {+ 1 ’x}}}

c) {with {’x 3}
{with {’x 5} {+ 1 ’x}}}

6) (10pts) Rewrite the following FWAE code in FAE, that is, rewrite it without using with:

{with {y 42} {fun {x} {+ y x}}}

7) (10pts) The following code is a partial implementation of the interpreter for RCFAE:

; interp : RCFAE DefrdSub -> RCFAE-Value


(define (interp rcfae ds)
(type-case RCFAE rcfae
...
[rec (f fun-expr body)
(local [(define value-holder (box (numV 42)))
(define new-ds (aRecSub f value-holder ds))]
(begin
(set-box! value-holder (interp fun-expr new-ds))
(interp body new-ds)))]))
Explain how this code implements recursive functions. For example, how initializing f to 42 works.
8) (10pts) What are the results of the following expressions in lazy evaluation?
a) {{fun {x} 320} {+ 1 {fun {y} 2}}}

b) {{fun {x} {- x 8}} {42 {fun {y} 2}}}

9) (10pts) The following code is a partial implementation of the interpreter for FWAE:

; interp : FWAE DefrdSub -> FWAE-Value


(define (interp fwae ds)
(type-case FWAE fwae
...
[app (f a) (local [(define f-val (interp f ds))
(define a-val (interp a ds))]
(interp (closureV-body f-val)
(aSub (closureV-param f-val)
a-val
(closureV-ds f-val))))]))

Now, the following code is a corresponding implementation for lazy evaluation. Fill in the missing
expressions:

; interp : CFAL DefrdSub -> CFAL-Value


(define (interp cfal ds)
(type-case CFAL cfal
...
[app (f a) (local [(define f-val (_____________________))
(define a-val (_____________________))]
(interp (closureV-body f-val)
(aSub (closureV-param f-val)
a-val
(closureV-ds f-val))))]))
where CFAL-Value is defined as follows:

(define-type CFAL-Value
[numV (n number?)]
[closureV (param symbol?) (body CFAL?) (ds DefrdSub?)]
[exprV (expr CFAL?) (ds DefrdSub?)])
10) (20pts) Given the following FWAE expression:

{with {f {fun {m} {fun {n} {- n m}}}}


{with {g {f 8}}
{with {x 42}
{g x}}}}

Describe a trace of the evaluation in terms of arguments to an interp function for every call. (There
will be 15 calls.) The interp function takes two arguments — an expression and a deferred substitution
— so show both for each call. For number, variable, and fun expressions, show the result value, which
is immediate. Use the back of the exam for additional space, and use the following abbreviations and
possibly more abbreviations to save time:

E0 = the whole expression


E1 = {fun {m} {fun {n} {- n m}}}
E2 = {with {g {f 8}} {with {x 42} {g x}}}
E3 = {with {x 42} {g x}}
(define-type FWAE
[num (n number?)]
[add (lhs FWAE?) (rhs FWAE?)]
[sub (lhs FWAE?) (rhs FWAE?)]
[with (name symbol?) (init FWAE?) (body FWAE?)]
[id (name symbol?)]
[fun (param symbol?) (body FWAE?)]
[app (ftn FWAE?) (arg FWAE?)])

(define-type FWAE-Value
[numV (n number?)]
[closureV (param symbol?) (body FWAE?) (ds DefrdSub?)])

(define-type DefrdSub
[mtSub]
[aSub (name symbol?) (value FWAE-Value?) (ds DefrdSub?)])

; interp : FWAE DefrdSub -> FWAE-Value


(define (interp fwae ds)
(type-case FWAE fwae
[num (n) (numV n)]
[add (l r) (num+ (interp l ds) (interp r ds))]
[sub (l r) (num- (interp l ds) (interp r ds))]
[id (name) (lookup name ds)]
[with (x i b) (interp b (aSub x (interp i ds) ds))]
[fun (x b) (closureV x b ds)]
[app (f a) (local [(define f-val (interp f ds))
(define a-val (interp a ds))]
(interp (closureV-body f-val)
(aSub (closureV-param f-val)
a-val
(closureV-ds f-val))))]))

;; num-op : (number number -> number) -> (FWAE-Value FWAE-Value -> FWAE-Value)
(define (num-op op x y)
(numV (op (numV-n x) (numV-n y))))

(define (num+ x y) (num-op + x y))


(define (num- x y) (num-op - x y))

(define (lookup name ds)


(type-case DefrdSub ds
[mtSub () (error ’lookup "free variable")]
[aSub (sub-name num rest-ds)
(if (symbol=? sub-name name)
num
(lookup name rest-ds))]))

You might also like