Continuation-Passing Interpreters: Representing and Manipulating Control Contexts, Study notes of Computer Science

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

Pre 2010

Uploaded on 03/16/2009

koofers-user-umf-1
koofers-user-umf-1 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Continuation-Passing Interpreters
Gul Agha
CS 421 Spring 2006
4/19/2006 CS 421 Spring 2006 2
Representing and Manipulating
Control Contexts
Let interpreter explicitly pass continuations
Add language facilities to manipulate
continuations:
Exceptions
First-class continuations
Threads
Add a new parameter to eval-expression
expression, environment, continuation
4/19/2006 CS 421 Spring 2006 3
Review
Environment passing interpreters
Simple expressions
Recursive definitions
Conditional expressions
Primitive applications
Procedure applications
Let expressions
Assignments
4/19/2006 CS 421 Spring 2006 4
Environment Passing Interpreter
(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
Evaluating simple expressions
(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
Evaluating Recursive Definitions
(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)))
pf3
pf4
pf5
pf8

Partial preview of the text

Download Continuation-Passing Interpreters: Representing and Manipulating Control Contexts and more Study notes Computer Science in PDF only on Docsity!

Continuation-Passing Interpreters

Gul Agha CS 421 Spring 2006

4/19/2006 CS 421 Spring 2006 2

Representing and Manipulating

Control Contexts

  • Let interpreter explicitly pass continuations
  • Add language facilities to manipulate continuations: - Exceptions - First-class continuations - Threads
  • Add a new parameter to eval-expression
    • expression, environment, continuation

4/19/2006 CS 421 Spring 2006 3

Review

  • Environment passing interpreters
    • Simple expressions
    • Recursive definitions
    • Conditional expressions
    • Primitive applications
    • Procedure applications
    • Let expressions
    • Assignments 4/19/2006 CS 421 Spring 2006 4

Environment Passing Interpreter

(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

Evaluating simple expressions

(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

Evaluating Recursive Definitions

(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

Evaluating Conditional Expressions

(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

Evaluating Primitive Applications

(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

Evaluating Procedure

Applications

(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

Evaluating Let Expressions

  • Retain call-by-value behavior for let expressions (let-exp (ids rands body) (let ((args (eval-let-exp-rands rands env))) (eval-expression body (extend-env ids args env))))

4/19/2006 CS 421 Spring 2006 11

Evaluating Assignments

(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

Continuation Passing Interpreter

  • Add continuations
  • Pass continuation to each expression
  • What about top level continuation?
    • Read-eval-print loop
    • Introduce a continuation to which the result of evaluating the initial (main body) expression goes
    • This is the “final” continuation…
    • What should this continuation do?

4/19/2006 CS 421 Spring 2006 19

Recursive Procedure Definitions

  • Creates a new environment without calling eval-expression
  • Evaluates the body in the new environment
  • Returns the value of the body
    • Control context of the body is the same is that of the entire expression

4/19/2006 CS 421 Spring 2006 20

Recursive procedures

(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

Evaluation of Conditionals

(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

Continuation for Tests

(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

Applying Continuations

(define apply-cont

(lambda (cont v)

(cont v)))

4/19/2006 CS 421 Spring 2006 24

Data Structure Representation of

Continuations

(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

Applying Continuations in

Records

(define apply-cont (lambda (cont val) (cases continuation cont (halt-cont ( ) (begin (write val) (newline)) )

4/19/2006 CS 421 Spring 2006 26

Applying Continuation (contd.)

(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

Evaluating Local Bindings

(let-exp (ids rands body) (eval-rands rands env (let-exp-cont ids env body cont)))

4/19/2006 CS 421 Spring 2006 28

Continuations for Let

Expressions

(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

Applying Let Continuations

(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

Primitive Operations

(primapp-exp (prim rands) (eval-rands rands env (prim-args-cont prim cont)))

4/19/2006 CS 421 Spring 2006 37

Procedures

(define-datatype procval procval? (closure (ids (list-of symbol?)) (body expression?) (env environment?)))

4/19/2006 CS 421 Spring 2006 38

Applying Procedures

(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

Primitive Arguments

Continuation

(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

An Imperative Interpreter

  • Shared variables in place of bindings (eval-expression exp env cont) (eval-rands rands env cont) (apply-procval proc args cont) (apply-cont cont val)
  • Add seven global registers
  • Use assignments to the register

4/19/2006 CS 421 Spring 2006 41

Exception Handling

Æ handle Æ raise

  • Evaluate the second expression and install it as an exception handler_._
  • If an exception is raised invoke the most recent exception handler. 4/19/2006 CS 421 Spring 2006 42

Record Representation for

Exceptions AST

try-exp (body-exp handler-exp) raise-exp (exp)

4/19/2006 CS 421 Spring 2006 43

Example

  • list-index numbers returns the first occurrence of a given number in a list of letrec index (n,1) = if null?(1) then sub1 (0) else if equal? (n, car(l)) then 0 else let p = (index n cdr(l)) in if equal?(p, sub1(0)) then sub1(0) else add1(p) in … 4/19/2006 CS 421 Spring 2006 44

List Index using Exception Handling

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

Implementing Exception Handling

  • Add two new continuation handlers (handler-cont (body expression?) (env environment?) (cont continuation?)) (try-cont (handler expval?) (cont continuation?)) 4/19/2006 CS 421 Spring 2006 46

Applying Continuations

(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

Normal Execution

  • If the body returns normally: (apply-cont (try-cont handler cont) val) = (apply-cont cont val)

4/19/2006 CS 421 Spring 2006 48

Exception Raised

  • If an exception is raised, search through the continuation for the nearest handler. In eval-expression: (raise-exp (exp) (eval-expression exp env (raise-cont cont))) In continuations: (apply-cont (raise-cont cont) val) = (find-handler val cont)