




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A part of the cs 421 spring 2006 course notes. It discusses continuation-passing interpreters and how to represent and manipulate control contexts using exceptions, first-class continuations, and threads. Evaluating simple expressions, recursive definitions, conditional expressions, primitive applications, procedure applications, let expressions, and assignments.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Gul Agha CS 421 Spring 2006
4/19/2006 CS 421 Spring 2006 2
4/19/2006 CS 421 Spring 2006 3
(define eval-expression (lambda (exp env) (cases expression exp (lit-exp (datum) datum ) (var-exp (id) ..) (proc-exp (id body) … ) (letrec-exp (proc-names idss bodies letrec-body) ..) (if-exp (test-exp true-exp false-exp) … ) (primapp-exp (prim rands) … ) (app-exp (rator rands) …) (let-exp (ids rands body) …) (varassign-exp (id rhs-exp) …))))
4/19/2006 CS 421 Spring 2006 5
(define eval-expression (lambda (exp env) (cases expression exp (lit-exp (datum) datum ) (var-exp (id) (apply-env env id)) (proc-exp (ids body) (closure ids body env)))))
4/19/2006 CS 421 Spring 2006 6
(define eval-expression (lambda (exp env) (cases expression exp (letrec-exp (proc-names idss bodies letrec-body) (eval-expression letrec-body (extend-env-recursively proc-names idss bodies env)))
4/19/2006 CS 421 Spring 2006 7
(if-exp (test-exp true-exp false-exp) (if (true-value? (eval-expression test-exp env)) (eval-expression true-exp env) (eval-expression false-exp env)))
4/19/2006 CS 421 Spring 2006 8
(define eval-expression (lambda (exp env) (cases expression exp (primapp-exp (prim rands) (let ((args (eval-rands rands env))) (apply-primitive prim args)) ))))
4/19/2006 CS 421 Spring 2006 9
(app-exp (rator rands) (let ((proc (eval-expression rator env)) (args (eval-rands rands env))) (if (procval? proc) (apply-procval proc args) (eopl:error 'eval-expression "Attempt to apply non-procedure ~s" proc)))) 4/19/2006 CS 421 Spring 2006 10
4/19/2006 CS 421 Spring 2006 11
(varassign-exp (id rhs-exp) (begin (setref! (apply-env-ref env id) (eval-expression rhs-exp env)) 1))
4/19/2006 CS 421 Spring 2006 12
4/19/2006 CS 421 Spring 2006 19
4/19/2006 CS 421 Spring 2006 20
(letrec-exp (proc-names idss bodies letrec-body) (eval-expression letrec-body (extend-env-recursively proc-names idss bodies env) cont)) ;not halt continuation
4/19/2006 CS 421 Spring 2006 21
(if-exp (test-exp true-exp false-exp) (eval-expression test-exp env (test-cont true-exp false-exp env cont)))
4/19/2006 CS 421 Spring 2006 22
(define test-cont (lambda (true-exp false-exp env cont) (lambda (val) (if (true-value? val) (eval-expression true-exp env cont) (eval-expression false-exp env cont)
4/19/2006 CS 421 Spring 2006 23
4/19/2006 CS 421 Spring 2006 24
(define-datatype continuation continuation? (halt-cont) (test-cont (true-exp expression?) (false-exp expression?) (env environment?) (cont continuation?)))
4/19/2006 CS 421 Spring 2006 25
(define apply-cont (lambda (cont val) (cases continuation cont (halt-cont ( ) (begin (write val) (newline)) … )
4/19/2006 CS 421 Spring 2006 26
(test-cont (true-exp false-exp env cont) (if (true-value? val) (eval-expression true-exp env cont) (eval-expression false-exp env cont)))
4/19/2006 CS 421 Spring 2006 27
(let-exp (ids rands body) (eval-rands rands env (let-exp-cont ids env body cont)))
4/19/2006 CS 421 Spring 2006 28
(define-datatype continuation continuation? …… (let-exp-cont (ids (list-of symbol?)) (env environment?) (body expression?) (cont continuation?)) … )
4/19/2006 CS 421 Spring 2006 29
(define apply-cont (lambda (cont val) (cases continuation cont … (let-exp-cont (ids env body cont) (let ((new-env (extend-env ids val env))) (eval-expression body new-env cont))) … )))
4/19/2006 CS 421 Spring 2006 30
(primapp-exp (prim rands) (eval-rands rands env (prim-args-cont prim cont)))
4/19/2006 CS 421 Spring 2006 37
(define-datatype procval procval? (closure (ids (list-of symbol?)) (body expression?) (env environment?)))
4/19/2006 CS 421 Spring 2006 38
(define apply-procval (lambda (proc args cont) (cases procval proc (closure (ids body env) (eval-expression body (extend-env ids args env) cont)))))
4/19/2006 CS 421 Spring 2006 39
(define-datatype continuation continuation? … (prim-args-cont (prim primitive?) (cont continuation?)) … ) (define apply-cont (lambda (cont val) … (prim-args-cont (prim cont) (let ((args val)) (apply-cont cont (apply-primitive prim args))))..) 4/19/2006 CS 421 Spring 2006 40
4/19/2006 CS 421 Spring 2006 41
try-exp (body-exp handler-exp) raise-exp (exp)
4/19/2006 CS 421 Spring 2006 43
let index = proc (n, 1) letrec loop ( l ) = if null? (1) then raise sub1 (0) else if equal? (n, car(l)) then 0 else let add1 ((loop cdr(l))) in try (loop l) handle proc (x) x in …
4/19/2006 CS 421 Spring 2006 45
(try-exp (body-exp handler-exp) (eval-expression handler-exp env (handler-cont body-exp env cont))) (apply-cont (handler-cont body-exp env cont) handler-val) = (if (procval? handler-val) (eval-expression body-exp env (try-cont handler-val cont)) (eopl:error ‘eval-exp “Error handler not procedure …))
4/19/2006 CS 421 Spring 2006 47
4/19/2006 CS 421 Spring 2006 48