How Many Continuations can Dance on the Head of a Pin PowerPoint PPT Presentation

presentation player overlay
1 / 72
About This Presentation
Transcript and Presenter's Notes

Title: How Many Continuations can Dance on the Head of a Pin


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?
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.

3
What 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.
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
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)
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)
  • Microsofts Web Services

18
Printing a Message (Console)
  • (print Hello, World\n)
  • (exit)

19
Printing a Message (Web)
  • (print
  • lthtmlgt
  • ltheadgtlttitlegtTestlt/titlegt
  • lt/headgt
  • ltbodygt
  • ltpgtHello, World!lt/pgt
  • lt/bodygt
  • lt/htmlgt )
  • (exit)

20
Do it! .. In DrScheme
21
Printing Uptime (Console)
  • (print Uptime s\n
  • (system uptime))
  • (exit)

22
Printing Uptime (Web)
  • (print
  • lthtmlgt
  • ltheadgtlttitlegtUptimelt/titlegt
  • lt/headgt
  • ltbodygt
  • ltpgt(system uptime)lt/pgt
  • lt/bodygt
  • lt/htmlgt)
  • (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)
(define r (get_binding radius
bindings)) (print ltpgtarea is (3.14rr)lt/pgt
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
28
Adding Two Numbers Web Interactions
29
Adding Two Numbers Web Interactions
30
Adding Two Numbers Web Interactions
31
Adding Two Numbers Web Interactions
32
Adding Two Numbers Web Interactions
33
Adding Two Numbers Web Interactions
34
Adding Two Numbers (Web)
(define n1 (get n1 bindings)) ltformgtlt/formgt
35
Adding 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

36
Adding Two Numbers(Web)
(define n1 (get_binding n1
bindings)) ltformgtlt/formgt
37
In 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

38
Adding 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
39
The 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

40
General Web Programming Problems
  • Invert the program
  • Manually tracks hidden fields
  • Its difficult and mistakes have painful
    consequences.

41
Bad News
  • Thats the easy part!

42
The Real Picture
The script and the user are
Event lines
coroutines!
script
user
43
Control Flow Back Button
A silent action!
44
Control Flow Cloning
script
user
45
Control Flow Bookmarks
script
user
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 ltformgtEnter first
lt/formgt)) (define n2 (read/web ltformgtEnter
second lt/formgt)) (print ltpgtsum ( n1
n2)lt/pgt) (exit)
50
But 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

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 ltformgtEnter first lt/formgt))
  • n2

(read/web/k ltformgtEnter first lt/formgt
(lambda(n1) n2
54
The 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)

55
Lift 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

56
Step 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)

57
Convert 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))
58
Resulting 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))
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)
  • Youve studied them in academic compilers
  • languages courses, especially when functions are
  • ordinary values.

61
Cant 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
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)

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
  • ltform action???gtEnter firstlt/formgt))
  • We provide send/suspend
  • (define n1
  • (send/suspend
  • (lambda (k)
  • ltform actionkgtEnter firstlt/formgt))

66
Generated 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 .

67
And send/suspend is what
a plain old call-with-current-contiuation.
68
The PLT Web Server
  • send/suspend associates urls 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 5626670049071596826438
16214 685929638952175999932299156 0894146397615651
82862536979 208272237582511852109168640 0000000000
0000000000000
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
http//www.plt-scheme.org/
Credits
  • Shriram Krisnamurthi (Brown)
  • Robert Bruce Findler (Chicago)
  • Matthew Flatt (Utah)
  • Paul T. Graunke (Northeastern)
Write a Comment
User Comments (0)
About PowerShow.com