CSC302 2007S, Class 18: Continuations and Web Servers Admin: * B/c of weather Sam will not penalize late people today * Due on Monday: HW5. * Because I will be distributing the exam on Monday, there is no homework until after break. * Because Grinnell was closed, you have until 5 p.m. today to submit your comments on today's readings. * Instead of doing a reading for Monday, answer the following: * What is polymorphism? * What is overloading? * How do the two relate? * EC: Tonight at 7:30, Bucksbaum Rotunda, the legendary G-Tones Overview: * Continuations and the Web. * Sam's sample code. * Applications of continuations, revisited. * Continuation-Passing Style. /Review/ * Basic continuation concepts: * Encapsulate computation (stack plus ...) * Exist as a first-class value (can be returned or passed or generated) * Useful to choose alternate exit routes from a nested proc. * Useful for exceptions * Althought they are conceptually simple * Figuring out the details seems complex /Use of Continuations/ * Rarely used outside of Scheme/Lisp/Smalltalk * Most programmers find rare reasons to use them /Continuations and the Web/ * Basic problem of writing Web services: * HTTP is stateless * Programmers must implement state themselves * Typical solutions involve a single value to keep track of each conversation * Related problems * User may want to have multiple windows * User may want to redo requests * Getting state into Web services places an unnecessary burden on the programmer. * If we can associate a continuation with each page in a "conversation", we solve the state problem. * So, how do we implement this idea? /The Rebelsky Example/ * Model an "interactive" program Computer prompts User responds to prompt Computer prompts User responds to prompt ... Computer prints result * Step one: Provide infrastructue for prompting and getting responses. * Client perspective: * (prompt STRING) prints a prompt and returns a response * (result STRING) prints the final result * A sample client: Checking passwords (define double-super-top-secret-password "PASSWORD") (define pw double-super-top-secret-password) (define login (lambda () (if (eq? (prompt "Please enter THE password: ") pw) (result "Congratulations, you passed the test.") (result "Swords drop from the ceiling and fail you.")))) (define mp (lambda () (let* ((name (prompt "What is your name?")) (color (prompt "What is your favorite color?")) (swallow (prompt "What is the flying speed of a swallow?"))) ...))) * Three ways to write prompt and result * For a "local" interactive application (one done in DrScheme or the terminal window) * As part of a Web server * Simulating what happens in the Web server * The local version (define prompt (lambda (str) (display str) (read))) (define result (lambda (str) (display str) (newline))) * What happens on the Web (real or simulated?) * The caller of prompt expects to do something with the result (the rest of the dialog) * We'll make a continuation for that "rest of the dialog" call-with-current-continuation, call/cc, let/cc (define prompt (lambda (str) (let/cc cont ...))) * In a real Web page, the links will say "Use this continuation" * In our simulated one, we'll just print out the continuation * But ... you can't package a continuation into human readable form. * So ... * Generate a unique id for each continuation * Our server maintains a map from "continuation id" to continuation * Assume (genid) * Assume (store! cid cont) * Assume (get cid) * When the next Web page loads, we look up the continuation and call it with the value the user entered. * For the simulation, we need a (resume cid value) * It's easy (more or less) * To write the support procedures (prompt, etc.) * To write Web services that use them /Continuations, Concluded/ * Escape from code * Escape from code for exceptions * Web services (resumable interactions) * "Any interesting control can be implement with continuations" * Cooperative Multithreading * And others