17 1 Midterm
17 1 Midterm
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:
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}
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:
6) (5pts) Consider the following definition of mk-rec and its use to define the recursive function fib:
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:
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:
(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.
6
10) (10pts) The following code is an excerpt from the implementation of the interpreter for 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:
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)
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 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.
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:
[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