Top Banner
How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science
72

How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Dec 14, 2015

Download

Documents

Quinten Banker
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

How Many Continuations can Dance on the Head of a Pin

Matthias FelleisenKing of Continuations

and Professor of Computer Science

Page 2: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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

Page 3: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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

Page 4: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

What does this mean?

• The “Orbitz” Problem

• Programming the Interactive Web

• Continuations, closures and the Web

• Academia and the Real World, Again

Page 5: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

The Interactive Web and the “Orbitz Problem”

Page 6: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.
Page 7: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.
Page 8: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.
Page 9: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.
Page 10: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.
Page 11: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.
Page 12: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.
Page 13: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.
Page 14: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.
Page 15: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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

Page 16: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Programming the Interactive Web

Page 17: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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

Page 18: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Printing a Message (Console)

(print “Hello, World\n”)

(exit)

Page 19: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Printing a Message (Web)

(print <html> <head><title>Test</title> </head> <body> <p>Hello, World!</p> </body> </html> )(exit)

Page 20: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Do it! .. In DrScheme

Page 21: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Printing Uptime (Console)

(print “Uptime: %s\n”

(system “uptime”))

(exit)

Page 22: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Printing Uptime (Web)

(print <html> <head><title>Uptime</title> </head> <body> <p>(system “uptime”)</p> </body> </html>)(exit)

Page 23: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Do it! … In DrScheme …

Page 24: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Area of Circle (Console)

(define r (read “Enter radius:” )

(print “area is %d\n”

(* 3.14 r r))

(exit)

Page 25: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Area of Circle (Web)

Enter radius:

(define r (get_binding “radius” bindings))(print <p>area is (3.14*r*r)</p>

Page 26: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Adding Two Numbers (Console)

(define n1

(prompt-read “Enter first:”))

(define n2

(prompt-read “Enter second:”))

(print “sum: %d\n”(+ n1 n2))

(exit)

Page 27: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Adding Two Numbers: User Interfaces

Enter first:

Enter second:

Enter first:

Enter second:

Page 28: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Adding Two Numbers: Web Interactions

Page 29: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Adding Two Numbers: Web Interactions

Page 30: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Adding Two Numbers: Web Interactions

Page 31: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Adding Two Numbers: Web Interactions

Page 32: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Adding Two Numbers: Web Interactions

Page 33: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Adding Two Numbers: Web Interactions

Page 34: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Adding Two Numbers (Web)

Enter first: (define n1

(get “n1 bindings))

<form>…</form>

Page 35: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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

Page 36: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Adding Two Numbers(Web)

Enter first:

(define n1 (get_binding “n1” bindings))<form>…</form>

Enter second:

(define n2 (get_binding “n2” bindings))

<p>sum: (+ n1 n2)</p>

Page 37: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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”

Page 38: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Enter second:

n1::

Adding Two Numbers(Web)

Enter first:

(define n1 (get_binding “n1” bindings))<form>…</form>

Enter second:

(define n1 (get_binding “n1” bindings))(define n2 (get_binding “n2” bindings))<p>sum: (+ n1 n2)</p>

Page 39: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

The Actual Form

<html><head><title>The Addition Page</title><body><p>Enter the second number:</p><form method="get" action="http://www. .../cgi-second.ss"><input type="hidden" name=“n1" value=“1729"><input type="text" name=“n2" value="0"></form></html>

Page 40: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

General Web Programming Problems

• “Invert” the program

• Manually tracks hidden fields

• It’s difficult and mistakes have painful consequences.

Page 41: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Bad News

That’s the easy part!

Page 42: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

The Real Picture

The scriptand the user

are

script user

Event lines

coroutines!

Page 43: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Control Flow: Back Button

script user script user

A silent action!

Page 44: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Control Flow: Cloning

script user script user

Page 45: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Control Flow: Bookmarks

script user script user

Page 46: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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?

Page 47: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

How to Reengineer Programs for the Web

Automated Software Engineering 2002J. Automated Software Engineering

2003

Page 48: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

The Legacy Code

(define n1

(read “Enter first: ”))

(define n2

(read “Enter second: ”))

(print “sum: %d\n”

(+ n1 n2))

(exit)

Page 49: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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 <form>Enter first: </form>))(define n2 (read/web <form>Enter second: </form>))(print <p>sum: (+ n1 n2)</p>)(exit)

Page 50: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

But: the program structure is destroyed

(define n1

(read/web

<form>Enter first: </form>))

(define n2

(read/web

<form>Enter second: </form>))

(print

<p>sum: (+ n1 n2)</p> ))

(exit)

(define (main) (print <form action=“f1”> Enter first: <input name=“n1”> </form> ))

(define (f1 form) (print <form action=“f2”> <input hidden name=“n1” value= (form-n1 form)> Enter second: <input name=“n2”> </form>))

(define (f2 form) (print The sum is (+ (form-n1 form) (form-n2 form))

Page 51: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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

Page 52: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

The Key Insight

The manual conversionsimply implements the

continuation-passing style transformation!

Page 53: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Step 1: Create Function for the “Rest of the Computation”

(define n1

(read/web <form>Enter first: </form>))

… n2 …

(read/web/k

<form>Enter first: </form>

(lambda(n1)

… n2 …

Page 54: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

The Result

(read/web/k <form>Enter first: </form>

(lambda (n1)

(read/web/k <form>Enter second: </form>

(lambda(n2) (print <p>sum: (+ n1 n2)</p>)

Page 55: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Lift Functions

(define (main) (read/web/k <form>Enter first: </form> f1))(define (f1 n1) (read/web/k <form>Enter second: </form> f2))(define (f2 n2) (print <p>sum: (+ n1 n2)</p>

Page 56: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Step 2: Propagate Free Variables

(define (main) (read/web/k <form>Enter first: </form> f1))(define (f1 n1) (read/web/k/args n1 <form>Enter second: </form> f2))(define (f2 n1 n2) = (print <p>sum: (+ n1 n2)</p>)

Page 57: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Convert to Web API

(define (f1 n1)

(read/web/k/args n1

<form>Enter second: </form> f2))

(define (f1 form)

(print

<form action=“f2”>

<input hidden name=“n1”

value=(form-n1 form)>

Enter second:

<input name=“n2”>

</form>))

Page 58: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Resulting Web Application

(define (main) (print <form action=“f1”> Enter first: <input name=“n1”> </form>))

(define (f1 form)

(print

<form action=“f2”>

<input hidden name=“n1”

value=(form-n1 form)>

Enter second:

<input name=“n2”>

</form>))

(define (f2 form)

(print

<p>sum: (+ (form-n1 form) (form-n2 form))</p>))

Page 59: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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

Page 60: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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 areordinary values.

Page 61: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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)

Page 62: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Program Structure Reinstatement

(define n1

(read

“Enter first: ”))

(define n2

(read

“Enter second: ”))

(print

“sum: %d\n”

(+ n1 n2))

(exit)

(define n1

(read/web

<form>Enter first:</form>))

(define n2

(read/web

<form>Enter second:</form>))

(print

<p>sum:

(+ n1 n2)</p>

(exit)

What we have: What we want:

Page 63: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Now What?

APIs offer form, cookie, &c primitives

Why not an API with read/web ?

Page 64: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Now what?

Programmers: Stand up for your rights –

make server implementers work harder!

Page 65: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

The Real Primitive

read/web is a small lie:(define n1 (read/web <form action=???>Enter first:</form>))

We provide send/suspend:(define n1 (send/suspend (lambda (k) <form action=k>Enter first:</form>))

send/suspend generates theURL that resumes computation

Page 66: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

Generated URLs

send/suspend generates a URL:

http://host/servlets/add.ss;id281*k2-95799725

When a consumer uses this URL, the Webserver delivers the filled out form as the

resultof the call to send/suspend .

Page 67: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

And send/suspend is what …

… a plain old call-with-current-contiuation.

Page 68: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

The PLT Web Server

• send/suspend associates url’s with continuation objects

• reading and writing becomes a coroutine action

• the rest is (interesting) engineering

Page 69: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

The Moral of the Story

Page 70: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

How Many Continuations can Dance on the Head of a Pin

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Page 71: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

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, andteach robust interactive Webprograms 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.

Page 72: How Many Continuations can Dance on the Head of a Pin Matthias Felleisen King of Continuations and Professor of Computer Science.

The End

• Shriram Krisnamurthi (Brown)• Robert Bruce Findler (Chicago)• Matthew Flatt (Utah)• Paul T. Graunke (Northeastern)

Credits …http://www.plt-scheme.org/