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

10-final

The document is a final exam for CS 320, Fall 2010, consisting of various programming and theoretical questions related to functional programming and interpreters. It includes questions on homework preferences, favorite programming languages, web servlet implementation, garbage collection, typing rules, and evaluation tracing. The exam is designed to test students' understanding of concepts in programming languages and their practical application.

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)
11 views

10-final

The document is a final exam for CS 320, Fall 2010, consisting of various programming and theoretical questions related to functional programming and interpreters. It includes questions on homework preferences, favorite programming languages, web servlet implementation, garbage collection, typing rules, and evaluation tracing. The exam is designed to test students' understanding of concepts in programming languages and their practical application.

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/ 10

Final Exam

CS 320, Fall 2010


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, 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.)

– Homework #1: HtDP Exercises 3.3.5, 4.4.1, 4.4.2


– Homework #2: Function print to produce a string representation of a given λ expression
– Homework #3: Functions to collect free, binding, and bound identifiers
– Homework #4: Multiple arguments starting with F1WAE
– Homework #5: New setbox and multiple sequences
– Homework #6: More operators and multiple arguments with continuations
– Homework #7: Records and exceptions
– Homework #8: Booleans, pairs, and multiple arguments

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 (get-family who)


(local [(define person (web-read who))]
(if (equal? person "unknown")
"???"
(list person
(get-family (format "~a’s mother" person))
(get-family (format "~a’s father" person))))))

(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:

– Tag 1: a record containing one integer


– Tag 2: a record containing two pointers

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

Draw type derivations of the following expressions:

a) {{fun {x:num y:num} {+ x y}} 5}


b) {+ {{fun {x:num} {+ x 2}} 5} 8}

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:

(interp (parse ‘{+ x y})


(aSub ’x (numV 3) (aSub ’y (numV 4) (mtSub)))
(mtSto))

then a model solution would be:

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?)])

;; -------------------------------------------------------------

;; parse : S-expr -> BCFAE


(define (parse sexp)
(cond
[(number? sexp) (num sexp)]
[(symbol? sexp) (id sexp)]
[(pair? sexp)
(case (car sexp)
[(+) (add (parse (second sexp)) (parse (third sexp)))]
[(-) (sub (parse (second sexp)) (parse (third sexp)))]
[(fun) (fun (first (second sexp)) (parse (third sexp)))]
[(newbox) (newbox (parse (second sexp)))]
[(setbox) (setbox (parse (second sexp)) (parse (third sexp)))]
[(openbox) (openbox (parse (second sexp)))]
[(seqn) (seqn (parse (second sexp)) (parse (third sexp)))]
[else (app (parse (first sexp)) (parse (second sexp)))])]))

8
;; -------------------------------------------------------------

;; interp : BCFAE DefrdSub Store -> Value*Store


(define (interp bcfae ds st)
(type-case BCFAE bcfae
[num (n) (v*s (numV n) st)]
[add (l r) (interp-two l r ds st
(lambda (v1 v2 st1) (v*s (num+ v1 v2) st1)))]
[sub (l r) (interp-two l r ds st
(lambda (v1 v2 st1) (v*s (num- v1 v2) st1)))]
[id (name) (v*s (lookup name ds) st)]
[fun (param body-expr)
(v*s (closureV param body-expr ds) st)]
[app (fun-expr arg-expr)
(interp-two fun-expr arg-expr ds st
(lambda (fun-val arg-val st1)
(interp (closureV-body fun-val)
(aSub (closureV-param fun-val)
arg-val
(closureV-ds fun-val))
st1)))]
[newbox (expr)
(type-case Value*Store (interp expr ds st)
[v*s (val st1)
(local [(define a (malloc st1))]
(v*s (boxV a)
(aSto a val st1)))])]
[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)])]
[seqn (a b) (interp-two a b ds st
(lambda (v1 v2 st1) (v*s v2 st1)))]))

;; 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))))

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


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

;; malloc : Store -> integer


(define (malloc st)
(+ 1 (max-address st)))

;; max-address : Store -> integer


(define (max-address st)
(type-case Store st
[mtSto () 0]
[aSto (n v st1) (max n (max-address st1))]))

;; lookup : symbol DefrdSub -> BCFAE-Value


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

;; store-lookup : number Store -> BCFAE-Value


(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))]))

10

You might also like