CS 421 Spring 2005: Continuations and Control, Study notes of Computer Science

A part of the cs 421 spring 2005 course notes on continuations and control. It covers various topics such as imperative version of fact-iter, cps style code, environment, cps form of remove, first-class continuations, call/cc, and co-routines.

Typology: Study notes

Pre 2010

Uploaded on 03/11/2009

koofers-user-ajr
koofers-user-ajr 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Continuations and Control
Gul Agha
CS 421
CS 421 Spring 2005 2
Imperative Version of fact-iter
(define n-reg '*)
(define a-reg '*)
(define fact-iter
(lambda (n)
(set! n-reg n)
(set! a-reg 1)
(fact-iter-acc/reg)))
CS 421 Spring 2005 3
Imperative version (contd.)
(define fact-iter-acc/reg
(lambda ( )
(if (zero? n-reg)
a-reg
(begin (set! a-reg (* n-reg a-reg))
(set! n-reg (– n-reg 1))
(fact-iter-acc/reg)))))
CS 421 Spring 2005 4
Imperative Transform on
CPS style code
Additional complication:
What environment will the continuation be
evaluated in?
CS 421 Spring 2005 5
CPS form of Remove
(define remove
(lambda (s los k)
(if (null? los)
(k '( ))
(if (eq? s (car los))
(remove s (cdr los) k)
(remove s (cdr los)
(lambda (v)
(k (cons (car los) v))) )))))
CS 421 Spring 2005 6
Imperative Transform on
remove-cps
(define s-reg *)
(define los-reg *)
(define k-reg *)
(define remove
(lambda (s los k)
(set! s-reg s)
(set! los-reg los)
(set! k-reg k)
(remove/reg)))
pf3
pf4
pf5

Partial preview of the text

Download CS 421 Spring 2005: Continuations and Control and more Study notes Computer Science in PDF only on Docsity!

Continuations and Control

Gul Agha

CS 421

CS 421 Spring 2005 2

Imperative Version of fact-iter

(define n-reg '*)

(define a-reg '*)

(define fact-iter

(lambda (n)

(set! n-reg n)

(set! a-reg 1)

(fact-iter-acc/reg)))

CS 421 Spring 2005 3

Imperative version (contd.)

(define fact-iter-acc/reg

(lambda ( )

(if (zero? n-reg)

a-reg

(begin (set! a-reg (* n-reg a-reg))

(set! n-reg (– n-reg 1))

(fact-iter-acc/reg)))))

CS 421 Spring 2005 4

Imperative Transform on

CPS style code

  • Additional complication:

What environment will the continuation be

evaluated in?

CS 421 Spring 2005 5

CPS form of Remove

(define remove (lambda (s los k) (if (null? los) (k '( )) (if (eq? s (car los)) (remove s (cdr los) k) (remove s (cdr los) (lambda (v) (k (cons (car los) v))) )))))

CS 421 Spring 2005 6

Imperative Transform on

remove-cps

(define s-reg ’) (define los-reg ’) (define k-reg ’*) (define remove (lambda (s los k) (set! s-reg s) (set! los-reg los) (set! k-reg k) (remove/reg)))

CS 421 Spring 2005 7

(define remove/reg (lambda ( ) (if (null? los-reg) (k '( )) (if (eq? s-reg (car los-reg)) (begin (set! los-reg (cdr los-reg)) (remove/reg)) (begin (set! k (lambda (v) (k-reg (cons (car los-reg) v)))) (set! los-reg (cdr los-reg)) (remove/reg))))

Wrong!! Why?

CS 421 Spring 2005 8

Environment

  • Continuations must save the bindings in

(part of) the current environment that they

need.

  • Linked list of continuations
  • Interpreters take care of storing the current

environment.

fun env fun env fun env

CS 421 Spring 2005 9

What cps does not achieve?

  • Time complexity class of code is

unchanged.

  • Time or space requirement may change

only by a constant factor.

CS 421 Spring 2005 10

Fibonacci

(define fib (lambda (n) (if (or (= n 0) (= n 1)) n (+ (fib (– n 1)) (fib (– n 2))))) (define fib (lambda (n k) (if (or (= n 0) (= n 1)) (k n) (fib (– n 1) (lambda (v) (k (+ v (fib (– n 2)))) )) )))

CS 421 Spring 2005 11

CPS Fib

(define fib (lambda (n k) (if (or (= n 0) (= n 1)) (k n) (fib (– n 1) (lambda (v) (fib (– n 2) (lambda (w) k (+ v w)))) )))

  • Still exponential time!

CS 421 Spring 2005 12

Linear Recursive Fib

(define fib (lambda (n) (car fib*(n))))

(define fib* (lambda (n) (cond ((= n 0) (cons 0 0)) ((= n 1) (cons 0 1)) (else (let ((x (fib* (– n 1)))) (cons (+ (car x) (cdr x)) (car x)))))))

Exercise: convert to cps form!

CS 421 Spring 2005 19

Co-routines

  • Co-routines provide a symmetric control model.
    • Form of concurrency.
  • Used in simulations and games (Simula)
    • Co-routine for each player or object.
    • Master unit calls one co- routine.
    • Co-routines yield to each other.

CS 421 Spring 2005 20

Co-routines

(define (make-coroutine (lambda (co-body) (letrec ((state (lambda ( ) (co-body resume))) (resume (lambda (that) (call/cc (lambda (this) (set! state this) (that))) ))) (lambda ( ) (state)) )))

CS 421 Spring 2005 21

Example Co-routine

(define A (make-coroutine (lambda (resume) (….) (resume B) (….) (resume B) (….) ‘finito)))

(define B (make-coroutine (lambda (resume) (….) (resume A) (….) (resume A) (….) ‘finito)))

CS 421 Spring 2005 22

Binding current continuation

(let/cc k body)

Is a macro for:

(call/cc (lambda (k) body))

  • Can there be more than one continuation

stored?

  • What is the extent of a continuation?
  • What is (call/cc call/cc)?

CS 421 Spring 2005 23

What does this do?

(let ((cont #f)) (call/cc (lambda (k) (set! cont k))) (cont #f) )

  • Initial value of cont unimportant
  • Current continuation is bound to k
  • cont is bound to k
  • cont is invoked with an unimportant value
  • We have a time warp

CS 421 Spring 2005 24

Lexically Scoped Exceptions

  • Also called block/break exceptions.
  • Lexically surrounding block must catch

exception.

  • A structured form of labels and gotos
  • Examples:
    • return, break, continue in C have single point of return.
    • Labeled break in Java.

CS 421 Spring 2005 25

True Exceptions

Also called catch/throw exceptions (define find-symbol (lambda (id tree) (letrec ((find (lambda (tree) (if (pair? tree) (or (find (car tree)) (find (cdr tree))) (if (eq? tree id) (throw 'find t) f ))) (catch 'find (find tree)) ) CS 421 Spring 2005 26

Escape in Common Lisp

(defun fact (n)

(prog (r)

(setq r 1)

loop (cond ((= n 1) (return r)))

(setq r (* n r))

(setq n (- n 1))

(go loop) ) )

CS 421 Spring 2005 27

Escape from Nested Context

in Common Lisp

(defun fact2 (n)

(prog (r)

(setq r 1)

loop (setq r (* (cond ((= n 1) (return r))

('else n) )

r ))

(setq n (– n 1))

(go loop) ) )

CS 421 Spring 2005 28

Continuations with

Indefinite Extent

(define (fact n)

(let ((r 1) (k 'void))

(call/cc (lambda (c) (set! k c) 'void))

(set! r (* r n))

(set! n (– n 1))

(if (= n 1)

r

(k 'recurse)) ) )

CS 421 Spring 2005 29

Continuations with Indefinite Extent

(define (fact n)

(let ((r 1))

(let ((k (call/cc (lambda (c) c))))

(set! r (* r n))

(set! n (– n 1))

(if (= n 1)

r

(k k)) ) ) )