[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
call/cc and proper tail recursion
CALL-WITH-CURRENT-CONTINUATION creates a continuation which can
occcupy storage...
Not so. CALL-WITH-CURRENT-CONTINUATION creates a procedure, but it
has no business creating a new continuation. If you're confused
about this, you might refer to its formal semantics on page 36 of
the R4RS.
...As paragraph 4 in Section 1.1 points out,
Implementations are not required reclaim unused storage (only allowed).
On the other hand, we judge implementations on how well they reclaim
unused storage and take a dim view of implementations that do it
poorly.
While we are on this topic, there are 2 places where the requirement
for tail-recursiveness becomes unworkable. DYNAMIC-WIND is one:
The very semantics of dynamic-wind say that it calls its second
argument with a continuation other than the continuation passed
to dynamic wind. No one who understands its semantics could
possibly think that
(define (foo)
(DYNAMIC-WIND BEFORE foo AFTER))
is a tail-recursive loop for general BEFORE and AFTER, and no one
with realistic expectations for optimizing compilers will expect
the loop to become tail-recursive for any particular BEFORE or AFTER.
And the other is some form of EVAL (which will be part of R5RS).
(define (foo) (EVAL '(foo)))
This ought to be a tail-recursive infinite loop. I can understand
that some implementations might have trouble making it properly
tail-recursive, but it ought to be.
There are only 3 other essential procedures for which
tail-recursiveness can be a problem, CALL-WITH-CURRENT-CONTINUATION,
APPLY, and FOR-EACH.
As Rozas pointed out, FOR-EACH is obviously not required to be properly
tail-recursive. The other two have to be tail-recursive if Scheme is
to be a good meta-language for writing interpreters. If Feeley has
found that implementors don't understand this, then I favor adding an
explicit requirement of tail-recursiveness for these procedures (EVAL
APPLY, CALL-WITH-CURRENT-CONTINUATION) to the next report.
Sanity check: All three are properly tail-recursive in MacScheme and
in Chez Scheme. FOR-EACH is not, since it always returns #f (MacScheme)
or a non-printing object (Chez Scheme).
Will