[plt-dev] The Two State Solution: Native and Serializable Continuations in the PLT Web Server

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Mon May 18 16:47:15 EDT 2009

From: http://jay-mccarthy.blogspot.com/2009/05/two-state-solution-native-and.html

One of the annoyance of the stateless Web application language [1]
that comes with the PLT Web Server is that you can't call third-party
higher-order library procedures with arguments that try to capture
serializable continuations. (I know, you try to do that all the time.)
For example:

(build-list
 3
 (lambda (i)
   (call-with-serializable-current-continuation
    (lambda (k) (serialize k)))))

The problem is that the stateless language performs a transformation
on your program to extract the continuations into a serializable
representation. If you really need to do this, we've developed a
compromise called "The Two State Solution": one state on the client
and the other on the server. Only the third-party parts of the
continuation (in this case, the code inside build-list) are stored on
the server; everything else is shipped to the client. You just need to
annotate your code slightly to indicate where the transition is:

(serial->native
 (build-list
  3
  (lambda (i)
    (native->serial
     (call-with-serializable-current-continuation
      (lambda (k) (serialize k)))))))

serial->native signals the transition to the third-party and
native->serial signals the transition back.

It is still a little annoying to find when you've called these
third-party higher-order library procedures with arguments that try to
capture serializable continuations, so there's a simple macro that
provides a transitioning wrapper for you:

(define-native (build-list/native _ ho) build-list)

expands to:

(define (build-list/native fst snd)
  (serial->native
   (build-list
    fst
    (lambda args
      (native->serial
       (apply snd args))))))

This new feature is documented in the online manual [2], of course.

1. http://docs.plt-scheme.org/web-server/stateless.html
2. http://faculty.cs.byu.edu/~jay/plt-doc/web-server/stateless.html#(part._.Serializable_.Continuations)

-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://teammccarthy.org/jay

"The glory of God is Intelligence" - D&C 93


Posted on the dev mailing list.