UNB/ CS/ David Bremner/ teaching/ cs4613/ tests/ CS4613 final sample questions

Introduction

Scope

Consider a primitive {call-with {id v1} fex v2} which is equivalent to

{{let1 id v1 fex} v2}

except that the call is dynamically scoped with respect to id (and only id). Complete the callWith case of interp (below), so that the following (parser-free) tests pass.

(module+ test
  (print-only-errors #t)
  (define (example body)
    (let1E 'x (numE 3)
           (let1E 'f (lamE 'y (plusE (varE 'x) (varE 'y)))
                  body)))


  (test (interp (example (appE (varE 'f) (numE 4))) mt-env)
        (numV 7))

  (test (interp
         (example (callWith 'x (numE 5) (varE 'f) (numE 4))) mt-env)
        (numV 9))
(define (interp expr env)
  (type-case Exp expr
    ⋮
    [(let1E bound-id named-expr bound-body)
     (interp bound-body (extend env bound-id (interp named-expr env)))]
    [(lamE bound-id bound-body) (lamV bound-id bound-body env)]
    [(callWith with-id with-expr fun-expr arg-expr)
       (type-case Value (interp fun-expr env)
         [(lamV fun-param body-exp def-env)
          ___________________________________________________________________
          ___________________________________________________________________
          ___________________________________________________________________
          ___________________________________________________________________
          ___________________________________________________________________
          ]
         [else (error 'interp "not a function")])]
    [(appE fun-expr arg-expr)
     (let ([fval (interp fun-expr env)])
       (type-case Value fval
         [(lamV bound-id bound-body f-env)
          (interp bound-body
                  (extend f-env bound-id (interp arg-expr env)))]
         [else (error 'interp
                      (string-append "`call' expects a function, got: "
                                     (to-string fval)))]))]

Types

Given the following evaluation code for a join construct (and assuming the test passes),

[(joinE fun-1 fun-2)
 (funV 'x
       (appE fun-2 
             (appE fun-1 (varE 'x))) nv)]
(test (interp (appE 
               (joinE (lamE 'x (numTE) (plusE (varE 'x) (varE 'x)))
                      (lamE 'y (numTE) (minusE (varE 'y) (numE 1))))
               (numE 3))
              mt-env) 
      (numV 5))
  1. Given an appropriate typing rule or give racket code for the type checker.
  2. Typecheck the expression in the test using your rule/code, and any others needed.

Racket Continuations

Use let/cc to complete function target so that the following continuation based loop works (the working version prints “109876543210”)

(define (undefined-label v) (error 'goto "undefined label"))
(define target-continuation  undefined-label)

(define (target)
  _______________________________________________
  _______________________________________________
  _______________________________________________
  _______________________________________________)


(define (jump)
  (target-continuation (void)))

(define n 10)
(begin
  (target)
  (display n)
  (when (> n 0)
    (begin
      (set! n (sub1 n))
      (jump))))

GC

Consider the following heap state for a two space collector similar to assignment 5

#(14 12
     forwarded 20 forwarded 22 forwarded 14 4 forwarded 24 forwarded 17 6
     cons 20 22 cons 24 14 flat 2 flat () flat 1)
  1. Give a diagram of the current heap showing all records
  2. Assuming this state is just after a garbage collection, give the heap state before collection. If the state is not unique, give one possible sate.