Download presentation
Presentation is loading. Please wait.
Published byQuinten Banker Modified over 9 years ago
1
How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science
2
What does this mean? 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 …” It’s really all about the Web. Academia and the Real World
3
What does this mean? Functions are values like all other values. Continuations are fine values, too. Tail calls are jumps. Macros don’t surprise you. Functions live on a different planet. What’s a continuation? We have do. Who needs protection? It’s really about the Web. Common Lisp and PLT Scheme
4
What does this mean? The “Orbitz” Problem Programming the Interactive Web Continuations, closures and the Web Academia and the Real World, Again
5
The Interactive Web and the “Orbitz Problem”
15
The “Orbitz Problem” The same problem found on Web sites of Microsoft / Apple / … Continental Airlines / Orbitz / Hertz / … the National Science Foundation … … and many other companies
16
Programming the Interactive Web
17
The Interactive Web Numerous Web APIs: The Common Gateway Interface (CGI) Java Servlets Active Server Pages, Java Server Pages Scripting languages (Perl, PHP, etc) Microsoft’s Web Services
18
Printing a Message (Console) (print “Hello, World\n”) (exit)
19
Printing a Message (Web) (print Test Hello, World! ) (exit)
20
Do it!.. In DrScheme
21
Printing Uptime (Console) (print “Uptime: %s\n” (system “uptime”)) (exit)
22
Printing Uptime (Web) (print Uptime (system “uptime”) ) (exit)
23
Do it! … In DrScheme …
24
Area of Circle (Console) (define r (read “Enter radius:” ) (print “area is %d\n” (* 3.14 r r)) (exit)
25
Area of Circle (Web) Enter radius: (define r (get_binding “radius” bindings)) (print area is (3.14*r*r)
26
Adding Two Numbers (Console) (define n1 (prompt-read “Enter first:”)) (define n2 (prompt-read “Enter second:”)) (print “sum: %d\n”(+ n1 n2)) (exit)
27
Adding Two Numbers: User Interfaces Enter first: Enter second: Enter first: Enter second:
28
Adding Two Numbers: Web Interactions
34
Adding Two Numbers (Web) Enter first: (define n1 (get “n1 bindings)) …
35
Adding Two Numbers: The Problem Web scripts write a page, then terminate When the user replies, another script reads the form’s bindings and performs the next step
36
Adding Two Numbers (Web) Enter first: (define n1 (get_binding “n1” bindings)) … Enter second: (define n2 (get_binding “n2” bindings)) sum: (+ n1 n2)
37
In Practice System halts cleanly with an error –The user doesn’t 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”
38
Enter second: : n1: Adding Two Numbers (Web) Enter first: (define n1 (get_binding “n1” bindings)) … Enter second: (define n1 (get_binding “n1” bindings)) (define n2 (get_binding “n2” bindings)) sum: (+ n1 n2)
39
The Actual Form The Addition Page Enter the second number: <form method="get" action="http://www..../cgi-second.ss">
40
General Web Programming Problems “Invert” the program Manually tracks hidden fields It’s difficult and mistakes have painful consequences.
41
Bad News That’s the easy part!
42
The Real Picture The script and the user are scriptuser Event lines coroutines!
43
Control Flow: Back Button scriptuserscriptuser A silent action!
44
Control Flow: Cloning scriptuser scriptuser
45
Control Flow: Bookmarks scriptuser scriptuser
46
What Programmers Need “Multiply-resumable and restartable coroutines” No language has exactly this – the new control operator for the Web How do we implement it?
47
How to Reengineer Programs for the Web Automated Software Engineering 2002 J. Automated Software Engineering 2003
48
The Legacy Code (define n1 (read “Enter first: ”)) (define n2 (read “Enter second: ”)) (print “sum: %d\n” (+ n1 n2)) (exit)
49
How 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 Enter first: )) (define n2 (read/web Enter second: )) (print sum: (+ n1 n2) ) (exit)
50
But: the program structure is destroyed (define n1 (read/web Enter first: )) (define n2 (read/web Enter second: )) (print sum: (+ n1 n2) )) (exit) (define (main) (print Enter first: )) (define (f1 form) (print <input hidden name=“n1” value= (form-n1 form)> Enter second: )) (define (f2 form) (print The sum is (+ (form-n1 form) (form-n2 form))
51
The 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
52
The Key Insight The manual conversion simply implements the continuation-passing style transformation!
53
Step 1: Create Function for the “Rest of the Computation” (define n1 (read/web Enter first: )) … n2 … (read/web/k Enter first: (lambda(n1) … n2 …
54
The Result (read/web/k Enter first: (lambda (n1) (read/web/k Enter second: (lambda(n2) (print sum: (+ n1 n2 ) )
55
Lift Functions (define (main) (read/web/k Enter first: f1)) (define (f1 n1) (read/web/k Enter second: f2)) (define (f2 n2) (print sum: (+ n1 n2)
56
Step 2: Propagate Free Variables (define (main) (read/web/k Enter first: f1)) (define (f1 n1) (read/web/k/args n1 Enter second: f2)) (define (f2 n1 n2) = (print sum: (+ n1 n2) )
57
Convert to Web API (define (f1 n1) (read/web/k/args n1 Enter second: f2)) (define (f1 form) (print <input hidden name=“n1” value=(form-n1 form)> Enter second: ))
58
Resulting Web Application (define (main) (print Enter first: )) (define (f1 form) (print <input hidden name=“n1” value=(form-n1 form)> Enter second: )) (define (f2 form) (print sum: (+ (form-n1 form) (form-n2 form)) ))
59
Summary 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
60
A Remarkable Coincidence These are known as: Continuation-passing style transformation Lambda lifting Closure conversion (to first-order values) You’ve studied them in academic compilers & languages courses, especially when functions are ordinary values.
61
Can’t Languages do Better? European Symposium on Programming 2001 J. Higher-Order Symbolic Logic 2004 see also: Hughes (1999), Queinnec (ICFP 2000), Thielman (ESOP 2002)
62
Program Structure Reinstatement (define n1 (read “Enter first: ”)) (define n2 (read “Enter second: ”)) (print “sum: %d\n” (+ n1 n2)) (exit) (define n1 (read/web Enter first: )) (define n2 (read/web Enter second: )) (print sum: (+ n1 n2) (exit) What we have:What we want:
63
Now What? APIs offer form, cookie, &c primitives Why not an API with read/web ?
64
Now what? Programmers: Stand up for your rights – make server implementers work harder!
65
The Real Primitive read/web is a small lie: (define n1 (read/web Enter first: )) We provide send/suspend: (define n1 (send/suspend (lambda (k) Enter first: )) send/suspend generates the URL that resumes computation
66
Generated URLs send/suspend generates a URL: http://host/servlets/add.ss;id281*k2-95799725 When a consumer uses this URL, the Web server delivers the filled out form as the result of the call to send/suspend.
67
And send/suspend is what … … a plain old call-with-current-contiuation.
68
The PLT Web Server send/suspend associates url’s with continuation objects reading and writing becomes a coroutine action the rest is (interesting) engineering
69
The Moral of the Story
70
How Many Continuations can Dance on the Head of a Pin 933262154439441526816992388 562667004907159682643816214 685929638952175999932299156 089414639761565182862536979 208272237582511852109168640 00000000000000000000000
71
So 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.
72
The End Shriram Krisnamurthi (Brown) Robert Bruce Findler (Chicago) Matthew Flatt (Utah) Paul T. Graunke (Northeastern) Credits … http://www.plt-scheme.org/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.