0% found this document useful (0 votes)
0 views

17 1 Midterm

The document is a midterm exam for CS 320, Spring 2017, consisting of various programming and theoretical questions related to programming languages and their semantics. It includes tasks such as comparing programming languages, explaining first-class functions, evaluating expressions, and discussing the implementation of interpreters. The exam is closed-book and has a time limit of 180 minutes.

Uploaded by

전성진
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)
0 views

17 1 Midterm

The document is a midterm exam for CS 320, Spring 2017, consisting of various programming and theoretical questions related to programming languages and their semantics. It includes tasks such as comparing programming languages, explaining first-class functions, evaluating expressions, and discussing the implementation of interpreters. The exam is closed-book and has a time limit of 180 minutes.

Uploaded by

전성진
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/ 14

Midterm Exam

CS 320, Spring 2017


Student id: Name:

Instructions: You have 180 minutes to complete this closed-book, closed-note, closed-computer exam. Please
write all answers in the provided space. Korean students should write your answers in Korean.

1) (5pts) Different programming languages have different purposes and target application domains, which
lead them to make different design choices. For 2 programming languages of your choice not from this
course like FAE and BMFAE but from real world like C, Racket, Kotlin, Python, OCaml, Haskell, and
Scala, compare their pros and cons clearly.

2) (5pts) Explain what first-class functions are and write a code example that uses a first-class function
in your favorite language (Rust, JavaScript, RCFAE, Java, C++, · · · ).

1
3) (5pts) With the following list of function definitions in F1WAE:

{deffun {twice x} {+ x x}}


{deffun {x y} y}
{deffun {f x} {+ x 1}}
{deffun {g g} g}

Show the results of evaluating the following expressions. When it is an error, describe which error it is.

a) {twice twice}
b) {with {x 5} {x x}}
c) {g 3}
d) {g f}
e) {g g}

4) (5pts) Consider the following expression:

(let ([fac (lambda (m)


(let ([facX (lambda (facX)
(lambda (m)
(if (zero? m)
1
(* n (facX facX (- x 1))))))])
(facX facX m)))])
(fac m))

a) Draw arrows on the above expression from each bound variable to its binding occurrence.
b) Draw dotted arrows on the above expression from each shadowing variable to its shadowed vari-
able.

2
5) (5pts) Let’s call the following code A:

(let ([fac (let ([facX


(lambda (facX)
(lambda (n)
(let ([fac (facX facX)])
(if (zero? n) 1 (* n (fac (- n 1)))))))])
(facX facX))])
(fac 5))

and the following code B:

(let ([fac (let ([facX


(lambda (facX)
(let ([fac (facX facX)])
(lambda (n)
(if (zero? n) 1 (* n (fac (- n 1)))))))])
(facX facX))])
(fac 5))

a) What is the result of A?

b) What is the result of B?

6) (5pts) Consider the following definition of mk-rec and its use to define the recursive function fib:

(define (mk-rec body-proc)


(let ([fX (lambda (fX)
(let ([f (lambda (x) ((fX fX) x))])
(body-proc f)))])
(fX fX)))
(let ([fib (mk-rec
(lambda (fib)
; Usual fib
(lambda (n)
(if (or (= n 0) (= n 1)) 1 (+ (fib (- n 1)) (fib (- n 2)))))))])
(fib 10))

Describe what conditions body-proc should satisfy so that mk-rec can make its recursive version.

3
7) (5pts) The following code is an excerpt from the implementation of the interpreter for BFAE:

(define (store-lookup addr st)


(type-case Store st
[mtSto () (error ’store-lookup "unallocated")]
[aSto (sto-addr val rest-st)
(if (= addr sto-addr)
val
(store-lookup addr rest-st))]))

(define (interp expr ds st)


...
[setbox (bx-expr val-expr)
(interp-two bx-expr val-expr ds st
(lambda (bx-val val st1)
(v*s val
(aSto (boxV-address bx-val) val st1))))]
[openbox (bx-expr)
(type-case Value*Store (interp bx-expr ds st)
[v*s (bx-val st1)
(v*s (store-lookup (boxV-address bx-val) st1)
st1)])]
...)

Change the implementation of interp for the setbox case so that the old value of the box is dropped
(i.e., replaced with the new value) instead of merely hidden by the outside-in search order of store-lookup.
Define your helper function with its purpose and contract.

4
8) (5pts) What are the results of the following expression:

{rec {count {fun {n} {if0 n n {+ 1 {count {- n 1}}}}}}


{with {count {fun {x} {+ 42 {count x}}}}
{count 7}}}

in different scoping semantics when we evaluate it under the following environment?

(aRecSb ’count (box (closureV ’y (add (num 13) (id ’y)) (mtSub))) (mtSub))
a) Dynamic scope

b) Static scope

5
9) (10pts) For each of the following LFAE expressions, show how it is evaluated and its result.

a) {{fun {x} {x 8}} {+ 42 {fun {y} {- y x}}}}

b) {{fun {y} {{fun {x} {+ 3 x}} y}} 5}

6
10) (10pts) The following code is an excerpt from the implementation of the interpreter for BMFAE:

(define (interp expr ds st)


...
[app (f a)
(if (id? a)
(type-case Value*Store (interp f ds st)
[v*s (fv st1)
(local [(define b (lookup (id-name a) ds))]
(interp (closureV-body fv)
(aSub (closureV-param fv) b (closureV-ds fv))
st1))])
(interp-two f a ds st
(lambda (fv av st1)
(local [(define a (malloc st1))]
(interp (closureV-body fv)
(aSub (closureV-param fv) a (closureV-ds fv))
(aSto a av st1))))))]
...)

a) What is the calling convention of this semantics?

Consider the following expression in BMFAE:

{with {n 42}
{with {f {fun {g} {g n}}}
{f {fun {x} {+ x 8}}}}}
b) Show the environment and store just before evaluating addition in the call-by-reference semantics.

c) Show the environment and store just before evaluating addition in the call-by-value semantics.

7
11) (10pts) Consider the following language e:

e ::= a atomic expression


| ea function application
| fn m function expression
a ::= n number
| x identifier
m ::= p e pattern matching
| p e|m pattern matching sequence
p ::= _ wildcard pattern
| n number pattern
| x identifier pattern

where a value of the language v is either a number n or a closure hm, σi, a result of evaluation r is
either a value v or a failure in pattern matching ↑, which is different from run-time errors, and an
environment σ maps identifiers to their values.
The operational semantics rules for expressions and atomic expressions are as follows:
σ`e⇒r

σ`a⇒v σ ` e ⇒↑ σ ` e ⇒ hm, σ 0 i σ ` a ⇒ v (σ 0 , v) ` m ⇒ v 0
σ`a⇒v σ ` e a ⇒↑ σ ` e a ⇒ v0
σ ` e ⇒ hm, σ 0 i σ ` a ⇒ v (σ 0 , v) ` m ⇒↑
σ ` fn m ⇒ hm, σi
σ ` e a ⇒↑

σ`a⇒v
x ∈ Domain(σ)
σ`n⇒n
σ ` x ⇒ σ(x)

The semantics of pattern matching m and pattern p are as follows:


– Evaluation of p e under (σ, v) has two possibilities. First, when evaluation of p results in
a new environment σ 0 , the result of this pattern matching is the result of evaluation of e under
σ + σ 0 , where σ + σ 0 is a disjoint union of σ and σ 0 . Second, when evaluation of p produces ↑, the
evaluation of this pattern matching produces ↑ as well.
– Evaluation of “p e | m” under (σ, v) also has two possibilities. First, when evaluation of
p e succeeds with a value v 0 , the value of this pattern matching sequence is v 0 . Second, when
evaluation of p e fails, the result of evaluation of this pattern matching sequence is the result
of evaluation of m.
– Evaluation of the wildcard pattern _ under (σ, v) produces the empty environment.
– Evaluation of the number pattern n under (σ, v) has two possibilities. If n = v, it produces the
empty environment. Otherwise, it produces ↑.
– Evaluation of the identifier pattern x under (σ, v) produces a singleton environment {x 7→ v} if x
is not in the domain of σ.

Write the operational semantics for m and p of the forms (σ, v) ` m ⇒ r and (σ, v) ` p ⇒ σ/ ↑ ,
respectively, where (σ, v) ` p ⇒ σ/ ↑ denotes (σ, v) ` p ⇒ σ or (σ, v) ` p ⇒↑ . Remember that the
operational semantics do not specify run-time errors.

8
9
12) (10pts) Consider the following language e:

e ::= x
| λx.e
| ee
| {f e, · · · , f e}
| e.f
| e.f = e
| e; e
| (e)

where a value of the language is either an address a or a closure hλx.e, σi, an environment maps names
to values, and a store maps pairs of addresses and field names to values:

x ∈ Var
f ∈ Field
a ∈ Addr
fin
σ ∈ Env = Var → Value
hλx.e, σi ∈ Closure = Expr × Env
v ∈ Value = Closure + Addr
fin
M ∈ Store = (Addr × Field ) → Value

The semantics of some constructs are as follows:

– The value of a record {f1 e1 , · · · , fk ek } is an address a, and (a, fi ) maps the value vi evaluated
from the expression ei for each 1 ≤ i ≤ k in the store.
– The value of e.f is the value of the field f in the record e.
– The evaluation of e1 .f = e2 evaluates e1 first, and then e2 whose value is the value of the whole
expression. It maps the pair of the address a denoted by the value of e1 and the field name f to
the value of e2 , if (a, f ) exists in the store.

a) Write the operational semantics of the form σ, M ` e ⇒ v, M

10
b) Can we describe the semantics only with environments without stores? If so, describe how. If
not, describe why.

11
13) (20pts) Consider the following language e:

e ::= n
| {- e e}
| b
| {and e e}
| {not e}
| {if e e e}
| x
| {fun {x · · · x} e}
| {e · · · e}
| {rec {x e} e}

where n denotes a number, b denotes a boolean, x denotes an identifier, and a value of the language is
one of a number, a boolean, or a closure hλx1 · · · xn . e, σi. It does not support the short-circuiting
semantics.
a) Write the operational semantics of the form σ ` e ⇒ v for the expressions.

12
13
b) Write the evaluation derivation of the following expressions:

∅ ` {{fun {f m} {f m}} {fun {x} x} 8} ⇒

[zero 7→ hλx1 x2 x3 . {if x1 x2 x3 }, ∅i] ` {rec {visit {fun {b n} {if b n {visit {not b} {zero n}}}}}
{rec {zero {fun {x} {- 42 x}}}
{visit false 7}}} ⇒

14

You might also like