136 <strong>Scheme</strong> <strong>and</strong> <strong>Functional</strong> <strong>Programming</strong>, <strong>2006</strong>
Interaction-Safe State for the Web Jay McCarthy Shriram Krishnamurthi Brown <strong>University</strong> jay@cs.brown.edu sk@cs.brown.edu Abstract Recent research has demonstrated that continuations provide a clean basis to describe interactive Web programs. This account, however, provides only a limited description <strong>of</strong> state, which is essential to Web applications. This state is affected by the numerous control operators (known as navigation buttons) in Web browsers, which make Web applications behave in unexpected <strong>and</strong> even erroneous ways. We describe these subtleties as discovered in the context <strong>of</strong> working Web applications. Based on this analysis we present linguistic extensions that accurately capture state in the context <strong>of</strong> the Web, presenting a novel form <strong>of</strong> dynamic scope. We support this investigation with a formal semantics <strong>and</strong> a discussion <strong>of</strong> applications. The results <strong>of</strong> this paper have already been successfully applied to working applications. 1. Introduction The Web has become one <strong>of</strong> the most effective media for s<strong>of</strong>tware deployment. Users no longer need to download large run-time systems, <strong>and</strong> developers are free to use their choice <strong>of</strong> programming language(s). Web browsers have grown in sophistication, enabling the construction <strong>of</strong> interfaces that increasingly rival desktop applications. The ability to centralize data improves access <strong>and</strong> reliability. Finally, individual users no longer need to install or upgrade s<strong>of</strong>tware, since this can be done centrally <strong>and</strong> seamlessly on the server, realizing a vision <strong>of</strong> always up-to-date s<strong>of</strong>tware. Set against these benefits, Web application developers must confront several problems. One <strong>of</strong> the most bothersome is the impact <strong>of</strong> the stateless Web protocol on the structure <strong>of</strong> the source program. The protocol forces developers to employ a form <strong>of</strong> continuationpassing style, where the continuation represents the computation that would otherwise be lost when the server terminates servlet execution at each interaction point. Recent research demonstrates that using continuations in the source reinstates the structure <strong>of</strong> the program [13, 14, 16, 22]. Another source <strong>of</strong> difficulty is the Web browser itself. Browsers permit users to perform actions such as cloning windows or clicking the Back button. These are effectively (extra-linguistic) control operators, because they have an effect on the program’s control flow. The interaction between these <strong>and</strong> state in the program Proceedings <strong>of</strong> the <strong>2006</strong> <strong>Scheme</strong> <strong>and</strong> <strong>Functional</strong> <strong>Programming</strong> Workshop <strong>University</strong> <strong>of</strong> Chicago Technical Report TR-<strong>2006</strong>-06 can have sufficiently unexpected consequences that it induces errors even in major commercial Web sites [18]. The use <strong>of</strong> continuations does not eliminate these problems because continuations do not close over the values <strong>of</strong> mutable state. One solution to this latter problem would be to disallow mutation entirely. Web applications do, however, contain stateful elements—e.g., the content <strong>of</strong> a shopping cart—that must persist over the course <strong>of</strong> a browsing session. To describe these succinctly <strong>and</strong> modularly (i.e., without transforming the entire program into a particular idiomatic style), it is natural to use mutation. It is therefore essential to have mutable server-side state that accounts for user interactions. In this paper, we present a notion <strong>of</strong> state that is appropriate for interactive Web applications. The described cells are mutable, <strong>and</strong> follow a peculiar scoping property: rather than being scoped over the syntactic tree <strong>of</strong> expressions, they are scoped over a dynamic tree <strong>of</strong> Web interactions. To motivate the need for this notion <strong>of</strong> state, we first illustrate the kinds <strong>of</strong> interactions that stateful code must support (Sec. 2). We then informally explain why traditional state mechanisms (that are, faultily, used in some existing Web applications) fail to demonstrate these requirements (Sec. 3) <strong>and</strong> then introduce our notion <strong>of</strong> state (Sec. 4) with a semantics (Sec. 5). We also briefly discuss applications (Sec. 6) <strong>and</strong> performance (Sec. 7) from deployed applications. 2. Motivation The PLT <strong>Scheme</strong> Web server [14], a modern Web server implemented entirely in <strong>Scheme</strong>, is a test-bed for experimenting with continuation-based Web programming. The server runs numerous working Web applications. One <strong>of</strong> the most prominent is CON- TINUE [15, 17], which manages the paper submission <strong>and</strong> review phases <strong>of</strong> academic conferences. CONTINUE has been used by several conferences including Compiler Construction, the Computer Security Foundations Workshop, the ACM Symposium on Principles <strong>of</strong> <strong>Programming</strong> Languages, the International Symposium on S<strong>of</strong>tware Testing <strong>and</strong> Analysis, the ACM Symposium on S<strong>of</strong>tware Visualization, <strong>and</strong> others. CONTINUE employs a sortable list display component. This component is used in multiple places, such as the display <strong>of</strong> the list <strong>of</strong> the papers submitted to the conference. The component has the following behaviors: the sort strategy may be reversed (i.e., it may be shown in descending or ascending order); when the user presses the Back button after changing the sort, the user sees the list sorted as it was prior to the change; the user may clone the window <strong>and</strong> explore the list with different sorts in different browser windows, without the sort used in one window interfering with the sort used in another; <strong>and</strong>, when the user returns to the list after a detour into some other part <strong>of</strong> the application, the list remains sorted in the same way as it was prior to the detour, while its content reflects changes to the state (e.g., it includes newly-submitted papers). 137
- Page 1:
Scheme and Functional Programming 2
- Page 4 and 5:
4 Scheme and Functional Programming
- Page 6 and 7:
6 Scheme and Functional Programming
- Page 8 and 9:
• A web browser that plays the ro
- Page 10 and 11:
and requests. When a client request
- Page 12 and 13:
above prevents pages from these dom
- Page 14 and 15:
14 Scheme and Functional Programmin
- Page 16 and 17:
2. Explaining Macros Macro expansio
- Page 18 and 19:
For completeness, here is the macro
- Page 20 and 21:
onment is extended in the original
- Page 22 and 23:
expand-term(term, env, phase) = emi
- Page 24 and 25:
Derivation ::= (make-mrule Syntax S
- Page 26 and 27:
True derivation (before macro hidin
- Page 28 and 29:
We assume that the reader has basic
- Page 30 and 31:
the value of the %eax register by 4
- Page 32 and 33:
Code generation for the new forms i
- Page 34 and 35:
we introduced in 3.11. The only dif
- Page 36 and 37:
the user code from interfering with
- Page 38 and 39:
38 Scheme and Functional Programmin
- Page 40 and 41:
mization and on creating efficient
- Page 42 and 43:
(a) stage (b) fifo (c) split (d) me
- Page 44 and 45:
This tagging is used later in the c
- Page 46 and 47:
(let ((clo_25 (%closure (lambda (y)
- Page 48 and 49:
nb. cycles per element 1400 1200 10
- Page 50 and 51:
50 Scheme and Functional Programmin
- Page 52 and 53:
a ∗ ❅ left right · b ❅ a S
- Page 54 and 55:
T ([spec], w) = { {w}, if w ∈ L([
- Page 56 and 57:
A([spec]): ✓✏ (where L([spec])
- Page 58 and 59:
construction commands. It is possib
- Page 60 and 61:
; Regular expression for Scheme num
- Page 62 and 63:
References [1] A. V. Aho, R. Sethi,
- Page 64 and 65:
(let ((n (cond ((char? var0) ) ((sy
- Page 66 and 67:
3. Survey An incomplete survey of a
- Page 68 and 69:
case monster 10 literals 100 litera
- Page 70 and 71:
70 Scheme and Functional Programmin
- Page 72 and 73:
modest programming requirements, an
- Page 74 and 75:
development of incomplete subsystem
- Page 76 and 77:
the previous request. This method a
- Page 78 and 79:
could be run indefinitely. This fun
- Page 80 and 81:
programmers reject Scheme without r
- Page 82 and 83:
(define interp (λ (env e) (case e
- Page 84 and 85:
Before describing the run-time sema
- Page 86 and 87: Figure 5. Cast Insertion Figure 6.
- Page 88 and 89: Figure 7. Evaluation Figure 8. Eval
- Page 90 and 91: catching type errors, as we do here
- Page 92 and 93: [34] J. C. Reynolds. Types, abstrac
- Page 94 and 95: let id (T:*) (x:T) : T = x; The tra
- Page 96 and 97: Figure 3: Regular Expressions and N
- Page 98 and 99: Figure 5: Evaluation Rules Evaluati
- Page 100 and 101: 5. Exact Substitution: E, (x = v :
- Page 102 and 103: Figure 9: Subtyping Algorithm Algor
- Page 104 and 105: for most type variables, and that m
- Page 106 and 107: 2. miniKanren Overview This section
- Page 108 and 109: 3. Pseudo-Variadic Relations Just a
- Page 110 and 111: Replacing run 10 with run ∗ cause
- Page 112 and 113: As might be expected, we could use
- Page 114 and 115: Of course there are still infinitel
- Page 116 and 117: To ensure that streams produced by
- Page 118 and 119: 118 Scheme and Functional Programmi
- Page 120 and 121: On the other hand, we can use a hig
- Page 122 and 123: (ev* (Q (lambda (x) (+ x 1)))) # >
- Page 124 and 125: (term ’lam (lambda (x) (if (equal
- Page 126 and 127: a message: there is no guarantee th
- Page 128 and 129: (! (self) 3) (?) =⇒ 1 (?? odd?) =
- Page 130 and 131: (define new-server (spawn (lambda (
- Page 132 and 133: The abstraction shown in this secti
- Page 134 and 135: Erlang Termite List length (µs) (
- Page 138 and 139: page of j contains the URL of the p
- Page 140 and 141: 4. Solution The failed attempts abo
- Page 142 and 143: · ; · ; · :: Store × Frame Stac
- Page 144 and 145: logically creates a sub-session of
- Page 146 and 147: on this work, including Ryan Culpep
- Page 148 and 149: ing application operation. 1 As a r
- Page 150 and 151: such as image glyphs corresponding
- Page 152 and 153: Phone For clarity, this code pr
- Page 154 and 155: A. Porting TinyScheme to Qualcomm B
- Page 156 and 157: 156 Scheme and Functional Programmi
- Page 158 and 159: ware installers have to install any
- Page 160 and 161: defines a module named circle-lib i
- Page 162 and 163: Figure 2. Sometimes special cases a
- Page 164 and 165: Considering all of these issues tog