[REBOL] Re: About CONTINUATIONS
From: chaz:innocent at: 25-Feb-2002 0:24
Holger's email has much to ponder. His article (hope he submits it to
REBOLforces!) helps explain why RT chose to base the REBOL interpreter on
Stack Machine (SM) technology, rather than going with Finite State Machine
(FSM). Because they went that way, REBOL developers gain
1) REBOL's legendary compatibility with major operating systems, unlike
2) more efficient use of memory, than with FSM
3) error handling of diverse REBOL features like /Command libraries and
/View events, apparently not possible with FSM
4) better support from RT because the REBOL interpreter is easier for RT
But of course "For every door that opens, a door closes." Because RT chose
SM instead of FSM, we get
1) greater incompatibility with PalmOS
2) no continuations
3) a much lower probability that RT will implement tail recursive
optimization, a feature which doesn't apparently make sense to implement on
an interpreter like REBOL, anyway.
We also get a nice slam on Java, as an extra bonus!
Now if only RT folks would show up on this list more often. Articles like
this go a long way towards really deepening developer understanding.
----- Original Message -----
From: "Holger Kruse" <[holger--rebol--net]>
Sent: Sunday, February 24, 2002 5:34 PM
Subject: [REBOL] Re: About CONTINUATIONS
On Sun, Feb 24, 2002 at 03:27:39PM -0500, [Robbo1Mark--aol--com] wrote:
> Whilst continuations are a tricky concept to come to terms with initially
and joe average user won't bother to learn about them it doesn't mean that
they are not a desirable language feature. In my opinion they ARE definitely
on a list of desirable language features.
> Do I wish they were still in REBOL? probably YES.
Ok, since you are not going to stop complaining until someone from RT makes
statement, I hereby do so :-).
Continuations were not "removed" by us. They became a victim of a complete
reimplementation of REBOL, by necessity. There was never an option of
continuations and still advance REBOL in the way it has happened, and there
still no such option.
In order to understand how continuations fit into a language interpreter we
first need to examine the different ways an interpreter can be constructed.
There are three fundamental ways of doing this:
1) Using a Stack Machine (SM). This is what REBOL 2.x does. In a stack
machine-based interpreter the calling path through the interpreted program
somewhat corresponds to the calling path within the interpreter, i.e.
a function is called in the program, the interpreter also calls a function
within the interpreter, to evaluate the function. Similarly for expressions
etc. Recursive interpretation is handled by recursively calling interpreter
functions, i.e. such functions have to be reentrant.
This means that in an SM-based interpreter a significant portion of the
of the interpreter is kept implicitly, on the stack or in local variables,
registers etc., i.e. it is not available at the language level. Because of
continuations (which require all state information to be explicit, to allow
to be manipulated at the language level) cannot be implemented in a purely
2) Using a Finite State Machine (FSM). This is what REBOL 1.x did. In an
FSM-based interpreter all interpreter state is stored explicitly. The CPU
is hardly used at all, and the interpreter does not recurse on itself.
recursive evaluation is performed by extending the size of the state
This can allow the interpreter stack to become a first-class datatype at the
3) A combination. See below.
The distinction between FSM-based and SM-based systems is not unique to
interpreters. You will find a similar distinction in other places. For
in an application (top-down or event-driven, i.e. bottom-up), and in
pieces of operating systems (filesystem, network stack, GUI engine etc.), so
anyone who has some non-trivial experience in software development should be
familiar with the significance of this (and the problems that occur when
models "clash" in some way).
So what are the advantages and disadvantages of both methods, when applied
FSM-based interpreters tend to be more flexible. Some features, such as
continuations, are only available in FSM-based interpreters, because they
require explicit access to the interpreter state. Other features, such as
threading and coroutines are much easier to implement in FSM-based
FSM-based interpreters integrate better with environments which are
as well. For instance if the OS is FSM-based, such as PalmOS, then having an
FSM-based interpreter tends to allow better integration, smoother user
On the other hand SM-based interpreters integrate better with operating
that are SM-based as well, which pretty much covers all modern operating
systems, with the exception of parts of the GUI engine. In particular
systems which rely heavily on a "top down with callbacks" model, like
integrate better with SM-based interpreters, if those callbacks are supposed
be exposed at the language level in some way.
FSM-based interpreters usually cannot adequately expose OS features that are
SM-based at the language level. For instance, REBOL exposes OS library calls
/Pro and /Command, with callbacks in the soon-to-be-released new View/Pro
Command versions. This would be pretty much impossible to do with an
environment. To understand why, just imagine that within the callback a new
continuation chain branches off, the main branch returns, through the OS
function, and then the program decides to continue within the continuation,
finally return through the OS function again. Obviously that is impossible,
because the stack frame of the OS function no longer exists. A large number
features of contemporary operating systems rely on a stack frame, in one way
another, i.e. exposing those features at the language level is only possible
interpreters which correlate stack frame to interpreter state. An FSM-based
interpreter does not do that, so it cannot expose those features.
FSM-based interpreters are inherently slower and usually require more
compilers, CPUs etc. have been designed with SMs in mind and highly optimize
SM-related operations, which are for the most part standardized for each
platform these days (stack frames, calling conventions etc.). On the other
FSM designs always require explicit FSM code, more elaborate memory
etc., and cannot make use of the existing resources as well. For instance
an SM-based interpreter can implicitly store the result of a condition in a
flag and act on it immediately (conditional jump), an FSM-based interpreter
needs to allocate a data structure for it and store the result as a flag,
loop back in the interpreter, examine the state, and finally examine the
remove it from the state, and act on it. That is an order of magniture more
FSM-based interpreters are usually much more difficult to write and
just like event-driven programs tend to be more difficult to work with than
procedural programs (with few exceptions, like class-based GUI engines).
is probably what Joe Marshall's comments regarding qualifications and
requirements for developers comes from. The side effect of this is that
FSM-based interpreters tend to be less agile: development and testing take
planning and effort. Changes happen more slowly.
3) Finally, here is that third method I mentioned above: It is possible for
interpreter to use a "hybrid" method of FSM and SM, by using a stack machine
most of the time, and capturing pending information within an on-the-fly
continuation structure. The main advantage of this method is that most of
time, performance is about as good as you can expect from an SM-based
interpreter. The problem is that this is usually not practical, at least
in non-trivial interpreters. That's because there are only two ways of doing
it: explicitly, by unrolling and recreating function calls, or by "hacking
in", using assembly language stubs that analyze and synthesize stack frames.
Even though some languages have successfully used both methods in some
scope and on selected platforms, they are not suitable for REBOL. Explicit
unrolling is infeasible because it would have to be supported in too many
places. REBOL/Core has around 100 natives. Add to that actions and ops, and
becomes several hundred. Adding code to serialize the state to all of those
functions would take months to implement and probably years to test. The
solution is easier to do on some platforms, but very platform-specific, i.e.
not suitable for REBOL. Besides, even though continuation could be supported
either way, this would still not solve the problem that continuations are
fundamentally incompatible with stack-based OS features, such as library
> Continuations make loads of intersting and advanced things possible
without detracting anything away from the user / programmer who doesn't use
or understand them.
Yes, and they also make loads of interesting and advanced things IMpossible.
In an error!-based error handling scheme such as REBOL it is possible to
handle error conditions even with libraries and callbacks, and with
event-driven programs. With continuations this would be impossible. You
end up with an environment which offers multiple features, but without the
ability to use them in combination. From a modularity point of view that is
much worse than offering a smaller set of features with full orthogonality.
> Scheme and Common Lisp both have them and so does Stackless Python and ML.
There is loads of interesting Comp Sci literature on continuations and they
are a well understood and used feature for advanced programming in the above
languages, especially in the field of modelling concurrency and parallel
Yes, that's the point, really. Continuations are a somewhat theoretical
concept. They severely clash with how operating systems and runtime systems
are designed. This is why they are only implemented in languages which are
academic in nature. When a language matures to the point that it becomes
interesting beyond the academic realm, other considerations, such as
performance, integration with existing environments etc. become more
This kind of maturing is exactly what happened in the transition from REBOL
> From what I can see they didn't fully explore the continuation model to
it's full extent, Joe Marshall states as such. Iam sure there is a LOT they
could have learned from advanced Scheme & ML implementations which STILL
have these features. These languages are not SO fundamentally different from
REBOL which is at it's core a prefix functional language.
That's completely besides the point. Continuations have nothing to do with
language model, only with the interpreter model.
> This is all theoretical but raises some interesting issues regarding
language design and implementation.
Yes, exactly. Implementation more than design. The only aspect interesting
design is whether continuations should be first class.
> I was a bit disturbed that Joe Marshall stated that with his
implementation "nobody else in the company could extend the interpreter.."
SURELY NOT? What about Carl Sassenrath?
> Also "You need a degree in Comp. Sci from a few select places to really
understand all this.." IS THAT A PROBLEM? Is there anything wrong with that?
Don't take these comments too literally :-). Joe was just trying to make a
point. The current interpreter is an order of magnitude easier to maintain,
extend and test.
> The solutions *ARE* there if you take the time to learn and understand
Sort of. Solutions are not universal. They only apply to certain domains.
instance if you sacrifice OS interaction, or graphics, or networking or
other things, then this may allow you to make certain implementation changes
that allow the implementation of other features. Java demonstrates this
it has one interesting and important feature: operational safety and
of the virtual machine from its environment. It pays a high price though: no
pointers, no unions, no arrays of structures, very limited casting,
high numbers of run-time tests, and thus overall bad performance. Some say,
retrospect, that this was too high a price.
What this illustrates is that just because a solution is theoretically
and even has been shown to work well in one domain does not mean that is is
even applicable in another one, and some choices are mutually exclusive.
why there IS more than one language you can choose from. Keeping
in REBOL would have prevented REBOL from entering certain domains. For
/View would have been impossible, for performance reasons. There is the
library/callback issue. There is agility, integration with existing software
packages and libraries. There are other issues...
And to answer the inevitable second question, regarding tail recursion
optimization (or more generally, tail call optimization):
First of all, we need to distinguish between the interpreter and compiler
because the way they deal with tail call optimization is quite different:
In a compiler the optimization is not performed at run time, but at compile
time, and it involves unrolling the stack frame before making the final
Sometimes that is as easy as replacing "jsr...; rts" with "jmp", but
make it more tricky. In any case, there is generally NO reason NOT to do it.
The only price you pay is, perhaps, slightly longer compilation time. The
resulting code is usually shorter, quicker and needs less memory, i.e. it is
In an interpreter the situation is different. The interpreter needs to
determine at run time whether a call is a "tail call" and allows
Because of that there is a trade-off: the benefit of successful tail call
optimization has to be weighed against the additional effort to determine,
dynamically, whether such optimization is possible. If that determination on
the average takes up more CPU time or memory than the actual optimization
saves, then such an optimization should not be implemented. Compilers
do not have that problem.
I do not have any hard data regarding that particular trade-off, but my
impression is that in "normal" programs (i.e. programs that were not written
particularly to prove a point, demonstrate a concept etc., in the academic
domain), it is very rare to have tail calls that benefit from optimization,
so I tend to suspect that adding tail call optimization may, in most cases,
have a detrimental effect.
It would certainly be possible to add tail call optimization back in,
it is somewhat more difficult to do in an SM-based interpreter than an
FSM-based interpreter (because it is not enough to just prune the state
structure -- the stack has to be unrolled, too). However I could think of
probably dozens of feature requests for REBOL that I would consider more
important, benefitting more users, and resulting in more significant
improvements, so don't count on tail call optimization to be very high on
priority list :-).