Lambda the Ultimate

inactiveTopic Events and Continuations
started 12/1/2003; 6:48:19 AM - last post 12/10/2003; 10:42:20 AM
Andris Birkmanis - Events and Continuations  blueArrow
12/1/2003; 6:48:19 AM (reads: 814, responses: 28)
This weekend I was trying to wrap my mind around continuation-based web servers (yet another time).

The thing that bothers me is how to model multiple forms/events. One can mimic HTML and treat submit buttons just as input fields, so they just get passed as values to a continuation.

Sometimes, however, I see it more natural to say we have not one continuation waiting for a form to be submitted, but as many continuations as submit buttons (nothing, of course, precludes their closures from overlapping, so there is no overhead or lack of expressability).

This approach is not limited to Web, of course. I think it may be used for any events-based application.

Are there any servers/frameworks that go this way? I was not able to conjure a good enough Google request :-)

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/1/2003; 7:19:18 AM (reads: 804, responses: 0)
I developed a nasty habit of replying to myself :-)

Continuations and threads: Expressing machine concurrency directly in advanced languages is a very interesting paper dealing with continuations and events on a very low level.

Hmm, I may write a compiler one day, if I continue reading low-level stuff :-)

On the serious side, this paper describes nicely the difference between a continuation and a thread (it's obvious, but an eye-opener for me).

Ethan Aubin - Re: Events and Continuations  blueArrow
12/1/2003; 9:08:05 AM (reads: 795, responses: 0)
Peter Hopkins presented a paper on embedding multiple continuations in HTML at Scheme2003. Its linked off his blog

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/1/2003; 10:58:35 AM (reads: 788, responses: 0)
The link from the blog does not work... But Google thinks readers of that paper might be also interested in this.

Pete Hopkins - Re: Events and Continuations  blueArrow
12/1/2003; 4:54:50 PM (reads: 759, responses: 1)
What is the problem you're getting from the blog link? Is the file not found or is the file corrupted? I just tried it and it seemed to work, but maybe .Mac is playing tricks on me.

If you're interested, I can e-mail you the paper.

My paper may not be quite what you're looking for in your original post. With the PLT webserver continuations are done at URL granularity, so multiple submit buttons on the same form will lead to the same continuation. The dispatch to multiple continuations is mechanical, though, and not too much of a problem to do manually.

I totally agree with you conceptually, though. Each submit button should have its own continuation.

The leakage stuff (that Cite Seer reference) is something I discovered after I wrote the paper. It's a more general look at what I address and I think very worth reading and studying further. Basically, when a user clicks a link or presses a button, but your code has to dig through the results and figure out what he did, and then do a dispatch, that's leakage. Going directly from URL to continuation is leakage-free.

Patrick Logan - Re: Events and Continuations  blueArrow
12/1/2003; 7:39:22 PM (reads: 755, responses: 0)
I was able to access it.

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/1/2003; 11:47:12 PM (reads: 753, responses: 0)
If you're interested, I can e-mail you the paper.

Thanks, I finally got the paper. Probably some ISP glitch, or LtU generated too much traffic ;-)

With the PLT webserver continuations are done at URL granularity, so multiple submit buttons on the same form will lead to the same continuation.

In our project we use hidden forms as a transport instead of submitting directly, so each button may submit to a different URL. It works for a long time, the only problem is, of course, lack of browser history, and therefore, of a back button. That hurts usability enormously. I just tried to analize the situation formally, in an effort to find the best of two worlds. From a technical point of view, we can attach some additional client-side scripting to buttons, so they generate different URLs even for normal HTML forms. In this case, though, we will have to forfeit traffic savings we have now - in many applications each client-server roundtrip redraws just a portion of the screen.

Looks almost as if we need to hide browser's native navbar, and introduce our own for manipulating our internal history :-)

And yes, I wholeheartedly recommend the leakage paper to everybody, it's a must to read. It just gives a name to a concept everybody feels is there (ok, at least most of my collegues feel :-) ).

andrew cooke - Re: Events and Continuations  blueArrow
12/2/2003; 6:31:41 AM (reads: 735, responses: 4)
What about the browser "back" button? This seems to tie in more with your multiple closures idea, because if a user uses the back button they expect (in my experience) to step backwards through the transaction state).

Using an "old" continuation should delete any continuations later in the tree.

I'm thinking of something vaguely similar for a data processing language/system. One processes data using a sequence of functions, forming a "line" of mutated data. The user can then step back up the line and change a parameter, either creating a "fork" with new data, or triggering a re-calculation of later data (or both :o). These trees/sequences can then be exported to other uses, applied to new data, etc.

(In practice, not all data would be stored, and much might be deleted for space economy, since it can be recalculated on demand (data that has expensive processing costs is less liekly to be deleted). This seems somewhat like weak references in a strictly functional language (which someone pointed me at a while ago, but which I haven't yet followed up)).

Off topic: wasn't there a post of Ehud's a while back saying something vaguely to the effect that Monads were not a good solution for IO? I can't find it, and have a comment that might (depending on teh details of what he did actually say) be relevant...

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/2/2003; 7:15:15 AM (reads: 728, responses: 3)
What about the browser "back" button?

I meant that if we use custom transport (not just submitting HTML from), then a browser does not update its history, and there is just no state to return to.

Otherwise, back button should be ok for a continuation-per-button server.

Just minor cosmetic problem: personally I would be more pleased to see disabled buttons/links instead of getting reply that this continuation was GCed (discontinued?). This, of course, is betting for even more tight integration with client side, and probably should be left for non-HTML clients.

andrew cooke - Re: Events and Continuations  blueArrow
12/2/2003; 7:54:45 AM (reads: 736, responses: 2)
OK, I thought you were choosing between a single continuation or many, rather than how you divide up many continuations (if you have many continuations per session anyway then what is the advantage of restricting to one per page?). Maybe I still don't understand, because I'm not sure what you're doing (I re-read the original post and it talks about "web servers", which I thought meant HTTP - what's "custom transport"?).

On the other hand, you talk about forms and buttons. If you're using HTML then you can't select continuations by URL *and* have one continuation per button (since a form can contain more than one submit button) unless you dyanmically hack the form URL with Javascript tied to a button action.

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/2/2003; 8:14:14 AM (reads: 744, responses: 1)
unless you dyanmically hack the form URL with Javascript tied to a button action.

I do :-)

The whole framework is trying to provide a rich client on top of a standard browser. It's using JS heavily, hacks DOM heavily, so it has it's dark side.

By custom transport I mean our buttons are not just submit-type-inputs. They don't even call submit() of the form. We encode the URL manually (using app independent JS), and submit it through invisible frame. We actually use HTTP, but push it down the stack, effectively implementing our own protocol on top of it. E.g., our JS is responsible for interpreting the response from the server. Kind of applet or ORB, but implemented in terms of HTTP, HTML, JS and servlets. And you bet we know official MSIE bugs by heart :-)

Oh, I got offtopic :-)

I thought you were choosing between a single continuation or many

No, now I already bought multiple continuations, the papers in the thread being the last evidence I am on the right track. Well, not THE right one, but I like it :-) May end up using Scheme both client and server side (gradually becoming fed up with Java). Or is it possible in English to be fed up by a drink? :-)

Chris Double - Re: Events and Continuations  blueArrow
12/2/2003; 1:58:28 PM (reads: 694, responses: 0)
The continuation based system I'm using allows multiple continuations on a page. Instead of using form buttons I use links (that look like fancy buttons via CSS and images). Pressing these calls a javascript function that changes the action of the form to the correct continuation url for that button:

document.forms.main.action=action document.forms.main.submit()

It associates closures to URL's in a manner similar to Peter's paper. It also has 'wizard' style page flows built in giving back, cancel, next and finish buttons 'out of the box' to save writing the closure code for that each time.

I use Sisc Scheme for the web server and all the continuations are serialized to storage allowing the server to be shut down and restarted in the middle of transactions and have them continue without interruption. The downside of this approach is the storage for the continuations. Currently I'm looking at 20-30k on average for a 5-6 page flow. These continuations hold all state btw. The main reson for the large size is that the current system has drop down combo boxes with data pulled from the database and this takes room in the continuation. I need to factor this out in a later revision.

I've taken the approach of 'expiring' the continuations after a period of time, and any request for that expired continuation going back to the main system screen after an informational message. Ideally I'd like to keep them around for a long time but disk storage for a busy system is an issue.

Ehud Lamm - Re: Events and Continuations  blueArrow
12/2/2003; 2:11:38 PM (reads: 720, responses: 0)
Andris: Oh, I got offtopic

A bit. But it all sounds pretty interesting, so keep us informed...

Pete Hopkins - Re: Events and Continuations  blueArrow
12/2/2003; 7:31:37 PM (reads: 672, responses: 2)
Chris --

Wow... that seems like a lot of data. Can you push some of it off to the client? One cool insight that has been shown to me is that the environment on the server side is semantically identical to form fields on the client side. Likewise, the store on the server is the same as cookies on the browser.

Though, as you say some of it is from the database, so a refactoring could fix that.

Paul Graunke, et al. have a paper Automatically Restructuring Programs For The Web that avoids storing continuations by pushing stuff onto the client, encrypted so that the it can't be tampered with. I don't know how ultimately practical their solution is but it's an interesting consideration.

_pEt3

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/3/2003; 12:21:04 AM (reads: 661, responses: 4)
Andris: Oh, I got offtopic Ehud: A bit. But it all sounds pretty interesting, so keep us informed...

<offtopic level="dirty"> The interesting part of our job is, clients themseves design GUI, and just want us to implement it in Web app. This means we do not have a say in GUI design at all. Therefore, screens with hundred input items are not uncommon, tabs nest to 4 or 5 levels, there are always-on-top modeless dialogs and other monstrosities. This is why we implemented rich client infrastructure.

OTOH, there was a strict prohibition of Java or ActiveX, so we did it in JS.

A test for your framework: main window (with tabs) has multiple lists of the same object type (say, products). On the first click on an 'Add' button of any product a new always-on-top modeless dialog must be open with a selected product being the only in the dialog's list. On subsequent clicks on 'Add' in the main window corresponding products must be appended to the list. Now the tricky part: the list in the dialog has editable fields, and upon adding a product to the list, the changes to previously added products must not disappear.

Sounds scary? It is scary. </offtopic>

Ehud Lamm - Re: Events and Continuations  blueArrow
12/3/2003; 3:42:25 AM (reads: 658, responses: 3)
The scary part is that the software world didn't manage to create a platform making this sort of application building more straigtforward.

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/3/2003; 4:47:44 AM (reads: 656, responses: 1)
Yes, the usual excuse being that the users do not need such complicated UI in the first place. When I am wearing my developer's hat, I tend to agree with this.

Manuel Simoni - Re: Events and Continuations  blueArrow
12/3/2003; 5:00:49 AM (reads: 653, responses: 0)
Yeah, the web is really scary.

Ehud Lamm - Re: Events and Continuations  blueArrow
12/3/2003; 5:11:20 AM (reads: 659, responses: 0)
I am talking about the platform. Not everything should be exposed to users all the time. But the platform should support this level of interaction, and provide application developers with high-level constructs for designing their applications, with the features they think their users need (or want).

Manuel Simoni - Re: Events and Continuations  blueArrow
12/3/2003; 5:40:12 AM (reads: 635, responses: 1)
Which platform?

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/3/2003; 5:43:18 AM (reads: 642, responses: 0)
Paul Graunke, et al. have a paper Automatically Restructuring Programs For The Web that avoids storing continuations by pushing stuff onto the client, encrypted so that the it can't be tampered with.

This seems ok if there is no side-effects in the application, which for me means it's not an application, but just a content-serving system.

Otherwise you either cheat your user, not saving everything in the continuation, or end up saving versions of the whole server-world on the client (think DB transactions with sufficient isolation level).

I do recognize that saving everything is not only very coslty, but in many cases counter-intuitive for the user. Classical example - shopping cart. The problem is, no framework can differentiate automatically, what is to be saved as a part of continuztion, and what not. Speaking in hardware terms, what lives in the registers, and what on the heap. I believe this distinction to be very application-specific - decided by app developer (or even user-specific - decided by user's preferences).

This statement is not to diminish the value of the paper you mentioned, just to open a new discussion point (continuation?).

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/3/2003; 5:57:47 AM (reads: 639, responses: 0)
This paper by Paul Graunke, et al. also mentions that some kind of discrimination between data relevant to a state of the application and irrelevant one is up to the application developer. They use this info for somewhat different purpose, though (I believe for validating that continuation required by a request is consistent with the current world).

Ehud Lamm - Re: Events and Continuations  blueArrow
12/3/2003; 6:02:19 AM (reads: 639, responses: 0)
Which platform?

The one that (unfortunately) doesn't exist...

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/4/2003; 7:57:07 AM (reads: 581, responses: 0)
A note to myself: express current implementation in terms of this, improve the result, and transform it back to Java+JS (or Scheme+Scheme).

Avi Bryant - Re: Events and Continuations  blueArrow
12/7/2003; 1:26:58 AM (reads: 534, responses: 0)
I'm sorry I didn't notice this thread earlier. Seaside supports multiple continuations per page, in a way that seems similar on a superficial reading to Peter Hopkins' send/suspend/dispatch - in both, each link and submit button on the page has its own associated closure. The way Seaside then layers call/cc on top of this allows multiple separate "threads" on the same page - you can, for example, have two instances of the same application embedded into a web page side by side with completely independent flow. I've found this to be essential for building UIs of any complexity.

Avi Bryant - Re: Events and Continuations  blueArrow
12/7/2003; 1:36:07 AM (reads: 542, responses: 0)
Andris - I blogged about saving state in continuations here. The short answer is that I agree with you - what state should be saved has to be explicitly determined by the application developer. I've tried the other options and they don't work.

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/10/2003; 10:37:08 AM (reads: 493, responses: 1)
Just for completenes, I have to mention the Cocoon project. There were some discussions about whether benefits of continuation-enabled web programming outweight the drawbacks. Not sure I understood the final decision.

There was (is?) a Scheme-based "flow-management" (codenamed Schecoon), as well as Rhino enhanced with continuations (proving the saying that JS is just alternative syntax for Scheme ;-) ).

I tried to use Cocoon in practice too soon (three years ago.. maybe four?..), which was a mistake. That unsuccessful attempt killed my interest to the project that seems to be still alive and kicking...

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/10/2003; 10:42:20 AM (reads: 489, responses: 0)
I blogged about saving state in continuations here.

Avi, thanks for the pointer. I am quite busy preparing for a vacation, so just noticed your post.

...

Aha, seems vaguely familiar: The most common kind of object to get registered in Seaside are what in Smalltalk are called ValueHolders, also sometimes called "boxes" - objects that consist solely of an instance variable and a getter/setter for it. One of the "right" things we did in our project (I think) was introduction of boxes. Absence of developer-visible references in Java is one of little things that make the language barely usable for many purposes...

Andris Birkmanis - Re: Events and Continuations  blueArrow
12/11/2003; 1:35:51 AM (reads: 476, responses: 0)
Some backtracking-oriented citations from Cocoon-dev:

I suggested that components being heavyweight resource, allowing them to cross continuation boundaries should be prohibited. Automatic release doesn't seem a good solution to me, as it would mean that script variables would hold released components, thus leading to unpredictable behaviour (think about stateful pooled components). So my opinion is to raise an error if there are some unreleased components when a continuation is created. This will allow users to quickly learn the safe practices related to component management in flow scripts.

The question then becomes: does anyone envision real-world scenarios in which stateful components *are* needed across continuation boundaries? Or can we *always* formulate our flow so that we don't need to keep component state across continuations? (for example, database connections can be acquired/released as needed precisely because they're pooled). On a separate thread, if *all* acquired components *must* be released prior to creating a continuation... wouldn't it make sense for the FOM implementation to automagically release them??

How about defining a FlowSafe interface (contains no state and can be released/looked up transparently), and maybe a FlowSerializable interface (has a way that the state can be stored into the continuation and then restored, all transparently? So you would have to consciously code your components to use either of these interfaces, otherwise you'll have to manually release them before creating a continuation.