10-final
10-final
Instructions: You have 120 minutes to complete this closed-book, closed-note, closed-computer exam. Please
write all answers in the provided space, plus the back of the exam if necessary.
1) (5 points) Which homework assignment did you enjoy the most? (Indicating you enjoyed none of them
is a fine answer, but do not pick more than one.)
2) (5 points) Among the programming languages that you’ve ever heard of, which language is your favorite
and why?
1
3) (20 points) The following web servlet implementation (main handler plus helper function) uses web-read,
which takes only a prompt and uses let/cc internally to obtain a continuation. Convert the servlet
and some functions to instead use web-read/k, which takes a prompt and an explicit continuation
procedure (and does not use let/cc internally). Don’t convert a function if it doesn’t need to change.
Note that family-handler gets the family tree information from the user starting from the user to
the user’s ancestors until unknown ancestors, and it shows the family tree via the show-tree function.
You should assume that the list->html function requires no interaction with the user.
(define (family-handler)
(show-tree (get-family "You")))
(define (show-tree t)
(if (string? t)
t
(list->html t)))
2
4) (20 points) Suppose a garbage-collected interepreter uses the following two kinds of records:
The interpreter has one register, which always contains a pointer, and a memory pool of size 24. The
allocator/collector is a two-space copying collector, so each space is of size 12. Records are allocated
consecutively in to-space, starting from the first memory location, 0.
The following is a snapshot of memory just before a collection where all memory has been allocated:
– Register: 7
– To space: 2 5 7 1 0 1 8 2 0 7 1 1
What are the values in the register and the new to-space (which is also addressed starting from 0) after
collection? Assume that unallocated memory in to-space contains 0.
– Register:
– To space:
3
5) (20 points) Given the following typing rules:
Γ ` hnumi : num
Γ ` true : bool
Γ ` false : bool
Γ ` e1 : num Γ ` e2 : num
Γ ` {+ e1 e2 } : num
[. . . x : τ . . .] ` x : τ
Γ[x1 : τ1 . . . xn : τn ] ` e : τ0
Γ ` {fun {x1 : τ1 . . . xn : τn } e} : (τ1 . . . τn → τ0 )
Γ ` e0 : (τ1 . . . τn → τ0 ) Γ ` e1 : τ1 ... Γ ` en : τn
Γ ` {e0 e1 . . . en } : τ0
Γ ` e1 : bool Γ ` e2 : τ0 Γ ` e3 : τ0
Γ ` {if e1 e2 e3 } : τ0
4
5
6) (30 points) Given the following expression:
{{fun {b1}
{{fun {b2}
{seqn {setbox b1 8}
{openbox b2}}}
b1}}
{newbox 7}}
Describe a trace of the BCFAE evaluation in terms of arguments to interp for every call. (There will
be 13 calls.) Write them out in the order in which the calls to interp occur during evaluation. You
do not need to show the results returned by interp.
Write down the arguments using the {} notation, not using the more verbose data constructors. Also,
use an arrow notation for both the store and the environment.
An example, if the first call to interp were:
exp: {+ x y}
env: {x -> 3, y -> 4}
sto: {}
exp: x
env: {x -> 3, y -> 4}
sto: {}
exp: y
env: {x -> 3, y -> 4}
sto: {}
Note that the environment and store read in order from left to right.
6
7
#lang plai
(define-type BCFAE
[num (n number?)]
[add (lhs BCFAE?) (rhs BCFAE?)]
[sub (lhs BCFAE?) (rhs BCFAE?)]
[id (name symbol?)]
[fun (param symbol?) (body BCFAE?)]
[app (fun-expr BCFAE?) (arg-expr BCFAE?)]
[newbox (val-expr BCFAE?)]
[setbox (box-expr BCFAE?) (val-expr BCFAE?)]
[openbox (box-expr BCFAE?)]
[seqn (first BCFAE?) (second BCFAE?)])
(define-type BCFAE-Value
[numV (n number?)]
[closureV (param symbol?) (body BCFAE?) (ds DefrdSub?)]
[boxV (address integer?)])
(define-type DefrdSub
[mtSub]
[aSub (name symbol?) (value BCFAE-Value?) (rest DefrdSub?)])
(define-type Store
[mtSto]
[aSto (address integer?) (value BCFAE-Value?) (rest Store?)])
(define-type Value*Store
[v*s (value BCFAE-Value?) (store Store?)])
;; -------------------------------------------------------------
8
;; -------------------------------------------------------------
;; interp-two : BCFAE BCFAE DefrdSub Store (Value Value Store -> Value*Store
;; -> Value*Store
(define (interp-two expr1 expr2 ds st handle)
(type-case Value*Store (interp expr1 ds st)
[v*s (val1 st2)
(type-case Value*Store (interp expr2 ds st2)
[v*s (val2 st3)
(handle val1 val2 st3)])]))
9
;; num-op : (number number -> number) -> (BCFAE-Value BCFAE-Value -> BCFAE-Value)
(define (num-op op x y)
(numV (op (numV-n x) (numV-n y))))
10