[smlnj] callcc and throw vs. capture and escape

26 views
Skip to first unread message

Ronald Garcia

unread,
Apr 10, 2008, 11:19:59 AM4/10/08
to comp-l...@moderators.isc.org
Hi,

I am using SMLNJ and trying to understand the difference between
callcc/throw and capture/escape. As far as I can tell, they differ
only in how they treat exception handlers; however the textual
descriptions of them that I have seen are not clear to me.

For instance:

capture f
Apply f to the "current continuation". If f invokes this continuation
with argument x, it is as if (capture f) had returned x as a result,
except that the exception-handler is not properly restored (it is
still that of the invoker).

This description makes it sound as though each continuation has only
one exception handler. However my intuition for exceptions suggests
that handlers should be sprinkled throughout the continuation (one for
every "handle" expression in the current dynamic extent). Is the
description I quote above written in terms of how exceptions are
implemented in SMLNJ (i.e. does a continuation have a single
"exception handler" that handles all exceptions), or does SML really
have a notion of a single exception handler?

Is there a formal specification of capture and escape somewhere?
Perhaps something similar to Felleisen and Wright's specification of
callcc in CoreML?

Thanks,
ron


Matthias Blume

unread,
Apr 26, 2008, 1:05:24 AM4/26/08
to comp-l...@moderators.isc.org
Ronald Garcia <ron.g...@gmail.com> writes:

> Hi,
>
> I am using SMLNJ and trying to understand the difference between
> callcc/throw and capture/escape. As far as I can tell, they differ
> only in how they treat exception handlers; however the textual
> descriptions of them that I have seen are not clear to me.
>
> For instance:
>
> capture f
> Apply f to the "current continuation". If f invokes this continuation
> with argument x, it is as if (capture f) had returned x as a result,
> except that the exception-handler is not properly restored (it is
> still that of the invoker).
>
> This description makes it sound as though each continuation has only
> one exception handler. However my intuition for exceptions suggests
> that handlers should be sprinkled throughout the continuation (one for
> every "handle" expression in the current dynamic extent). Is the
> description I quote above written in terms of how exceptions are
> implemented in SMLNJ (i.e. does a continuation have a single
> "exception handler" that handles all exceptions), or does SML really
> have a notion of a single exception handler?

Exception handlers are dynamically nested, so only the innermost one
is in effect at any given time. However, notice that the handler
installed by a "handle" expression will usually have an implicit
default "catch-all" case that chains back to the previous handler.
(This is, unless you write your own explicit catch-all case.)

As a result, at any given time, there is only one exception handler.
In SML/NJ, the handler is implemented as a continuation that is stored
in a global variable. An alternative (and most likely better)
approach is to pass the exception handler as an implicit argument to
every function call, but for historical reasons this is currently not
the case in SML/NJ.

> Is there a formal specification of capture and escape somewhere?

No, not that I know of. This is an SML/NJ-ism anyway, which was added
as a workaround for problems caused by the fact that the global
exception handler variable is manipulated by side-effecting
operations.

Matthias

Reply all
Reply to author
Forward
0 new messages