[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

call/cc and proper tail recursion



   A new "continuation invoking" mechanism would be needed to solve
   this problem.  In addition to call-with-current-continuation there
   would be a procedure (say resume-continuation) taking two arguments:
   a continuation and a thunk to compute the value to send to the
   continuation.  The second definition above would thus be:

   (define (foo) (call/cc (lambda (k) (resume-continuation k (lambda () (f x))))))

   The advantage of this is that now the implicit continuation passed to
   resume-continuation can be garbage collected as soon as
   resume-continuation is entered.  Resume-continuation simply restores k
   and jumps to the thunk.  Note that resume-continuation is not really
   needed (its functionality could be integrated into k).

   Comments?

What you call resume-continuation is called within-continuation in MIT
Scheme.  It is useful, among other things, for the debugger to
evaluate expressions in the dynamic state of a continuation, and to
emulate non-primitively-supported functionality.  For example, to
extend cwcc to handle multiple values:

(define (cwcc receiver)
  (primitive-cwcc
   (lambda (k)
     (let ((state (get-dynamic-state)))
       (receiver (lambda all
		   (within-continuation
		     k
		     (lambda ()
		       (set-dynamic-state! state)
		       (apply values all)))))))))

I've moved the call to SET-DYNAMIC-STATE! to "after the throw" because
we actually ran into a problem in MIT Scheme doing it on the other
side.

When the system detected that it was out of resources (stack/heap), it
tried to abort by invoking a continuation (of a repl).  However, the
process of changing the dynamic state before aborting could cons or
need more stack and would only make the problem worse.