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

14-2-final

The document is a final exam for CS 320, Fall 2014, consisting of various programming and theoretical questions related to computer science concepts such as evaluation results of code snippets, garbage collection algorithms, tail call optimization, compilation, type checking, and type derivation. Students are instructed to write their answers in the provided space, with specific instructions for Korean students to respond in Korean. The exam includes both practical coding questions and theoretical discussions on programming languages and type systems.

Uploaded by

vedapac571
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)
2 views

14-2-final

The document is a final exam for CS 320, Fall 2014, consisting of various programming and theoretical questions related to computer science concepts such as evaluation results of code snippets, garbage collection algorithms, tail call optimization, compilation, type checking, and type derivation. Students are instructed to write their answers in the provided space, with specific instructions for Korean students to respond in Korean. The exam includes both practical coding questions and theoretical discussions on programming languages and type systems.

Uploaded by

vedapac571
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/ 13

Final Exam

CS 320, Fall 2014


Student id: Name:

Instructions: You have 150 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) What is the evaluation result of the following code?

(define table empty)

(define (remember v)
(local [(define n (length table))]
(begin (set! table (append table (list v)))
n)))

(define (lookup key) (list-ref table key))

(define (web-read/k prompt cont)


(local [(define key (remember cont))]
‘(,prompt "To continue, call resume/k with " ,key " and value")))

(define (resume/k key val)


(local [(define cont (lookup key))]
(cont val)))

(define (a)
(web-read/k "First number"
(lambda (v1)
(web-read/k "Second number"
(lambda (v2) (+ v1 v2))))))
(define (b) (begin (a) (a)))
(b)

1
2) (5pts) What is the evaluation result of the following code?

(define table empty)

(define (remember v)
(local [(define n (length table))]
(begin (set! table (append table (list v)))
n)))

(define (lookup key) (list-ref table key))

(define (web-read prompt)


(let/cc cont
(local [(define key (remember cont))]
(error ’web-read "~a; to continue, call resume with ~a and value" prompt key))))

(define (web-read-each prompts)


(map web-read prompts))

(define (m)
(apply format "my ~a saw a ~a rock" (web-read-each ‘("noun" "adjective"))))

(m)

3) (5pts) In the KCFAE language, what is the value of the following expression:

{withcc result
{{withcc escape
{result {+ 2 {withcc k
{escape k}}}}}
8}}

2
4) (10pts) The Swift programming language uses the reference counting garbage collection algorithm.

a) What is the correct order?


(ㄱ) When a count is decremented to 0, decrement counts for other records referenced by the
record, then free it
(ㄴ) When replacing a pointer to a record, decrement its count
(ㄷ) Attatch a count to every record, starting at 0
(ㄹ) When installing a pointer to a record, increment its count

b) Provide an example that shows why the order matters.

c) Describe good and bad things about the reference counting algorithm.

3
5) (5pts) Suppose a garbage-collected interepreter uses the following five kinds of records:

– Tag 1: a record containing two pointers


– Tag 2: a record containing one pointer and one integer
– Tag 3: a record containing one integer
– Tag 4: a record containing one integer and one pointer
– Tag 99: forwarding pointer (to to-space)

The interpreter has one register, which always contains a pointer, and a memory pool of size 26. The
allocator/collector is a two-space copying collector, so each space is of size 13. 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: 8
– From space: 1 3 8 3 0 4 7 3 2 0 8 3 42

What are the values in the register, the from-space, and the to-space after collection? Assume that
unallocated memory in to-space contains 0.

– Register:

– From space:

– To space:

6) (5pts) Tail call optimization


a) What is a tail call?

b) What is the tail call optimization?

c) What is a relation between the tail call optimization and continuation passing style?

4
7) (5pts) Compilation
a) Compare interpreters and compilers.

b) Compare the typecheck and interp functions.

8) (5pts) Compare the following types:


a) (τ1 τ2 · · · τn → τ0 ) and (τ1 → (τ2 → · · · (τn → τ0 ) · · · ))

b) (∀α (α → α)) and (∀α (∀β (α → β)))

5
9) (5pts) Describe the following:
a) Type inference

b) Polymorphic types

c) Type soundness

10) (5pts) What are the evaluation results of the following? If it is an error, describe where the error
occurs:
a) {withtype {fruit {apple bool}
{banana num}}
{{withtype {animal {apple (num -> num)}
{banana bool}}
{fun {x: fruit}
{cases fruit x
{apple {b} b}
{banana {n} {- 8 n}}}}}
{banana true}}}

b) {withtype {fruit {apple bool}


{banana num}}
{{withtype {fruit {apple (num -> num)}
{banana bool}}
{fun {x: fruit}
{cases fruit x
{apple {f} {f 97}}
{banana {b} {not b}}}}}
{banana true}}}

6
11) (5pts) Compare the following two versions of type checking function applications:
a) (define typecheck : (EXP TypeEnv -> Type)
(lambda (exp env)
(type-case EXP exp
...
[app (fn arg)
(local [(define result-type (varT (box (none))))]
(begin
(unify! (arrowT (typecheck arg env) result-type)
(typecheck fn env)
fn)
result-type))]
...)))
b) (define typecheck : (EXP TypeEnv -> Type)
(lambda (exp env)
(type-case EXP exp
...
[app (fn arg)
(type-case Type (typecheck fn env)
[arrowT (param-type result-type)
(if (equal? param-type (typecheck arg env))
result-type
(type-error arg (to-string param-type)))]
[else (type-error fn "function")])]
...)))

12) (10pts) Type derivation


a) Write the typing rule for the following expression:
{with {x τ e1 } e2 }

7
b) Given the partial typing rules:

Γ[α] ` e : τ
Γ ` [tyfun [α] e] : (∀α τ )

Γ ` τ0 Γ ` e : (∀α τ1 )
Γ ` [@ e τ0 ] : τ1 [α ← τ0 ]

Γ[α] ` τ
Γ ` (∀α τ )

[. . . α . . .] ` α

Draw the type derivation of the following expression:

{with {apply (∀α (∀β ((α → β) α → β)))


[tyfun [α] [tyfun [β] {fun {f : (α → β) x : α} {f x}}]]}
{[@ [@ apply (num -> num)] num] {fun {n : num} {- 17 n}} 9}}

8
13) (10pts) Given the following grammar and the partial typing rules:

e ::= n x:σ∈Γ στ


| {+ e e} Γ`x:τ
| x
| {fun {x} e} Γ[x : τ ] ` e : τ 0
| {e e} Γ ` {fun {x} e} : (τ → τ 0 )
| {with {x e} e}
τ ::= num Γ ` e1 : (τ → τ 0 ) Γ ` e2 : τ
| (τ -> τ ) Γ ` {e1 e2 } : τ 0
| α
σ ::= τ Γ ` e1 : τ Γ[x : Gen Γ (τ )] ` e2 : τ 0
| (∀α σ) Γ ` {with {x e1 } e2 } : τ 0

στ iff σ = (∀α1 (· · · (∀αn τ0 )) · · · ) ∧ τ = τ0 [αi ← τi ] where 1 ≤ i ≤ n

Gen Γ (τ ) = (∀α1 (· · · (∀αn τ )) · · · ) where {α1 , · · · , αn } = FTV (τ ) \ FTV (Γ)

FTV (num) = ∅
FTV ((τ1 → τ2 )) = FTV (τ1 ) ∪ FTV (τ2 )
FTV (α) = {α}
S
FTV (Γ) = x:σ∈Γ FTV (σ)
FTV ((∀α σ)) = FTV (σ) \ {α}

a) When we generalize a type τ to (∀α1 (· · · (∀αn τ )) · · · ) by Gen Γ (τ ), we do not generalize type


variables in Γ as {α1 , · · · , αn } = FTV (τ ) \ FTV (Γ) denotes. Why is it so?

b) Draw the type derivation of the following expression:


{{fun {x} {with {y x} {y 53}}} {fun {z} {+ z 1}}}

9
14) (20pts) We’d like to allow the following expression:

{withtype {{alpha list} {empty num}


{cons (alpha * {alpha list})}}
{rec {len : (forall alpha ({alpha list} -> num))
[tyfun [alpha]
{fun {l : {alpha list}}
{cases {alpha list} l
{empty {n} 0}
{cons {fxr}
{+ 1 {len {snd fxr}}}}}}]}
{+ {[@ len num] {[@ cons num] {pair 1 {[@ empty num] 0}}}}
{[@ len (num -> num)] {[@ empty (num -> num)] 0}}}}}

and of course the following as well:

{withtype {fruit {banana num} {apple bool}}


{{fun {x : fruit} {cases fruit x {banana {n} {+ n 9}} {apple {b} 76}}}
{banana 2}}}

a) Define the language syntax.

10
b) Write the operational semantics of the form σ ` e ⇒ v for the expressions withtype and cases.

c) Write the typing rules of the form Γ ` e : τ for the expressions withtype and cases and the
well-formedness of the added types.

11
d) Draw the type derivation of the following expression:
{withtype {{alpha list} {empty num}
{cons (alpha * {alpha list})}}
{[@ [tyfun [alpha]
{fun {l : {alpha list}}
{cases {alpha list} l {empty {n} n} {cons {p} 1}}}] num]
{[@ empty num] 0}}}

12
13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy