Title: How Many Continuations can Dance on the Head of a Pin
1How Many Continuations can Dance on the Head of a
Pin
- Matthias Felleisen
- King of Continuations
- and
- Professor of Computer Science
2What does this mean?
Academia and the Real World
- The theory of continuations (85-95)
- Phil Here is the king of continuations.
- The practice of PLT Scheme at ICFP (95-
- Dick Gabriel Theoreticians just count how many
continuations - Its really all about the Web.
3What does this mean?
Common Lisp and PLT Scheme
- Functions are values like all other values.
- Continuations are fine values, too.
- Tail calls are jumps.
- Macros dont surprise you.
- Functions live on a different planet.
- Whats a continuation?
- We have do.
- Who needs protection?
Its really about the Web.
4What does this mean?
- The Orbitz Problem
- Programming the Interactive Web
- Continuations, closures and the Web
- Academia and the Real World, Again
5The Interactive Web and the Orbitz Problem
6(No Transcript)
7(No Transcript)
8(No Transcript)
9(No Transcript)
10(No Transcript)
11(No Transcript)
12(No Transcript)
13(No Transcript)
14(No Transcript)
15The Orbitz Problem
- The same problem found on Web sites of
- Microsoft / Apple /
- Continental Airlines / Orbitz / Hertz /
- the National Science Foundation
- and many other companies
16Programming the Interactive Web
17The Interactive Web
- Numerous Web APIs
- The Common Gateway Interface (CGI)
- Java Servlets
- Active Server Pages, Java Server Pages
- Scripting languages (Perl, PHP, etc)
- Microsofts Web Services
18Printing a Message (Console)
- (print Hello, World\n)
- (exit)
19Printing a Message (Web)
- (print
- lthtmlgt
- ltheadgtlttitlegtTestlt/titlegt
- lt/headgt
- ltbodygt
- ltpgtHello, World!lt/pgt
- lt/bodygt
- lt/htmlgt )
- (exit)
20Do it! .. In DrScheme
21Printing Uptime (Console)
- (print Uptime s\n
- (system uptime))
- (exit)
22Printing Uptime (Web)
- (print
- lthtmlgt
- ltheadgtlttitlegtUptimelt/titlegt
- lt/headgt
- ltbodygt
- ltpgt(system uptime)lt/pgt
- lt/bodygt
- lt/htmlgt)
- (exit)
23Do it! In DrScheme
24Area of Circle (Console)
- (define r (read Enter radius )
- (print area is d\n
- ( 3.14 r r))
- (exit)
25Area of Circle (Web)
(define r (get_binding radius
bindings)) (print ltpgtarea is (3.14rr)lt/pgt
26Adding Two Numbers (Console)
- (define n1
- (prompt-read Enter first))
- (define n2
- (prompt-read Enter second))
- (print sum d\n( n1 n2))
- (exit)
27Adding Two Numbers User Interfaces
Enter first
Enter second
28Adding Two Numbers Web Interactions
29Adding Two Numbers Web Interactions
30Adding Two Numbers Web Interactions
31Adding Two Numbers Web Interactions
32Adding Two Numbers Web Interactions
33Adding Two Numbers Web Interactions
34Adding Two Numbers (Web)
(define n1 (get n1 bindings)) ltformgtlt/formgt
35Adding Two Numbers The Problem
- Web scripts write a page, then terminate
- When the user replies, another script reads the
forms bindings and performs the next step
36Adding Two Numbers(Web)
(define n1 (get_binding n1
bindings)) ltformgtlt/formgt
37In Practice
- System halts cleanly with an error
- The user doesnt get a useful answer
- The user may not understand the error
- User expended a lot of effort and time
- Program captures variable by accident (i.e., it
implements dynamic scope!), or - internal server error
38Adding Two Numbers(Web)
(define n1 (get_binding n1
bindings)) ltformgtlt/formgt
(define n1 (get_binding n1
bindings)) (define n2 (get_binding n2
bindings)) ltpgtsum ( n1 n2)lt/pgt
39The Actual Form
- lthtmlgt
- ltheadgt
- lttitlegtThe Addition Pagelt/titlegt
- ltbodygt
- ltpgtEnter the second numberlt/pgt
- ltform method"get"
- action"http//www. .../cgi-second.ss"gt
- ltinput type"hidden" namen1" value1729"gt
- ltinput type"text" namen2" value"0"gt
- lt/formgt
- lt/htmlgt
40General Web Programming Problems
- Invert the program
- Manually tracks hidden fields
- Its difficult and mistakes have painful
consequences.
41Bad News
42The Real Picture
The script and the user are
Event lines
coroutines!
script
user
43Control Flow Back Button
A silent action!
44Control Flow Cloning
script
user
45Control Flow Bookmarks
script
user
46What Programmers Need
- Multiply-resumable and
- restartable coroutines
- No language has exactly this the new control
operator for the Web - How do we implement it?
47How to Reengineer Programs for the Web
- Automated Software Engineering 2002
- J. Automated Software Engineering 2003
48The Legacy Code
- (define n1
- (read Enter first ))
- (define n2
- (read Enter second ))
- (print sum d\n
- ( n1 n2))
- (exit)
49How we should take this code to the Web
- (define n1
- (read
- Enter first ))
- (define n2
- (read
- Enter second ))
- (print
- sum d\n
- ( n1 n2)
- (exit)
(define n1 (read/web ltformgtEnter first
lt/formgt)) (define n2 (read/web ltformgtEnter
second lt/formgt)) (print ltpgtsum ( n1
n2)lt/pgt) (exit)
50But the program structure is destroyed
- (define n1
- (read/web
- ltformgtEnter first lt/formgt))
- (define n2
- (read/web
- ltformgtEnter second lt/formgt))
- (print
- ltpgtsum ( n1 n2)lt/pgt ))
- (exit)
- (define (main)
- (print
- ltform actionf1gt
- Enter first
- ltinput namen1gt
- lt/formgt ))
- (define (f1 form)
- (print
- ltform actionf2gt
- ltinput hidden namen1
- value (form-n1 form)gt
- Enter second
- ltinput namen2gt
- lt/formgt))
- (define (f2 form)
- (print
- The sum is
51The Reengineering Challenge
- Web interfaces have grown up
- from scripts to programs
- (or services)
- Need debugging, maintenance, evolution,
- We would like a Web compiler that automatically
- splits programs into procedures by form
- propagates fields as needed
- preserves behavior yet accommodates bizarre
control
52The Key Insight
- The manual conversion
- simply implements the
- continuation-passing style
- transformation!
53Step 1 Create Function for the Rest of the
Computation
- (define n1
- (read/web ltformgtEnter first lt/formgt))
- n2
(read/web/k ltformgtEnter first lt/formgt
(lambda(n1) n2
54The Result
- (read/web/k
- ltformgtEnter first lt/formgt
- (lambda (n1)
- (read/web/k
- ltformgtEnter second lt/formgt
- (lambda(n2)
- (print ltpgtsum
- ( n1 n2)lt/pgt)
55Lift Functions
- (define (main)
- (read/web/k
- ltformgtEnter first lt/formgt f1))
- (define (f1 n1)
- (read/web/k
- ltformgtEnter second lt/formgt f2))
- (define (f2 n2)
- (print ltpgtsum
- ( n1 n2)lt/pgt
56Step 2 Propagate Free Variables
- (define (main)
- (read/web/k
- ltformgtEnter first lt/formgt f1))
- (define (f1 n1)
- (read/web/k/args n1
- ltformgtEnter second lt/formgt f2))
- (define (f2 n1 n2)
- (print ltpgtsum
- ( n1 n2)lt/pgt)
57Convert to Web API
- (define (f1 n1)
- (read/web/k/args n1
- ltformgtEnter second lt/formgt f2))
(define (f1 form) (print ltform actionf2gt
ltinput hidden namen1
value(form-n1 form)gt Enter second
ltinput namen2gt lt/formgt))
58Resulting Web Application
- (define (main)
- (print
- ltform actionf1gt
- Enter first
- ltinput namen1gt
- lt/formgt))
(define (f1 form) (print ltform actionf2gt
ltinput hidden namen1
value(form-n1 form)gt Enter second
ltinput namen2gt lt/formgt))
(define (f2 form) (print ltpgtsum ( (form-n1
form) (form-n2 form))lt/pgt))
59Summary
- Three transformations
- Make all value receivers explicit functions
- Make all functions top-level, replace free
variables with explicit parameters - Replace first-class functions with first-order
representations
60A Remarkable Coincidence
- These are known as
- Continuation-passing style transformation
- Lambda lifting
- Closure conversion (to first-order values)
- Youve studied them in academic compilers
- languages courses, especially when functions are
- ordinary values.
61Cant Languages do Better?
- European Symposium on Programming 2001
- J. Higher-Order Symbolic Logic 2004
- see also
- Hughes (1999), Queinnec (ICFP 2000), Thielman
(ESOP 2002)
62Program Structure Reinstatement
What we have
What we want
- (define n1
- (read
- Enter first ))
- (define n2
- (read
- Enter second ))
- (print
- sum d\n
- ( n1 n2))
- (exit)
- (define n1
- (read/web
- ltformgtEnter firstlt/formgt))
- (define n2
- (read/web
- ltformgtEnter secondlt/formgt))
- (print
- ltpgtsum
- ( n1 n2)lt/pgt
- (exit)
63Now What?
- APIs offer form, cookie, c primitives
- Why not an API with read/web ?
64Now what?
Programmers Stand up for your rights make
server implementers work harder!
65The Real Primitive
- read/web is a small lie
- (define n1
- (read/web
- ltform action???gtEnter firstlt/formgt))
- We provide send/suspend
- (define n1
- (send/suspend
- (lambda (k)
- ltform actionkgtEnter firstlt/formgt))
66Generated URLs
- send/suspend generates a URL
- http//host/servlets/add.ssid281k2-95799725
- When a consumer uses this URL, the Web
- server delivers the filled out form as the result
- of the call to send/suspend .
67And send/suspend is what
a plain old call-with-current-contiuation.
68The PLT Web Server
- send/suspend associates urls with continuation
objects - reading and writing becomes a coroutine action
- the rest is (interesting) engineering
69The Moral of the Story
70How Many Continuations can Dance on the Head of a
Pin
933262154439441526816992388 5626670049071596826438
16214 685929638952175999932299156 0894146397615651
82862536979 208272237582511852109168640 0000000000
0000000000000
71So what does this mean?
Because we have closures and continuations in
Scheme and because this motivates us to think
about CPS and friends, we can design, implement,
and teach robust interactive Web programs
easily.
Can Common LISPers do it? Paul Graham did it.
But with closures and hygienic macros, things
would have been so much easier and cleaner.
72The End
http//www.plt-scheme.org/
Credits
- Shriram Krisnamurthi (Brown)
- Robert Bruce Findler (Chicago)
- Matthew Flatt (Utah)
- Paul T. Graunke (Northeastern)