1

I am trying to learn continuations and use them to implement coroutines in Scheme.

I have two procedures (coroutines) a and b, and I switch between them in the following way:

;; c is a continuation.
(define (a c)
   ...
   ;; Switch to the other coroutine.
   (call/cc c)
   ...


(define (b c)
   ...
   ;; Switch to the other coroutine.
   (call/cc c)
   ...

I start the execution with

(a b)

I have tested a small example and, indeed, the execution switches back and forth between the two procedures until one of the two terminates without calling the continuation it was given as an argument.

I have two questions:

  1. Is my example an appropriate / idiomatic implementation of coroutines using continuations?
  2. Is tail-call optimization mandatory for implementing coroutines with continuations? In my solution, switching too often between the two coroutines might cause a stack overflow if Scheme did not provide TCO. Are there other solutions that do not require TCO?

EDIT

Basile's comment seems to be sufficient to answer my question. The background of the question is that I would like this approach to implement coroutines in Python, where I do not have TCO.

Giorgio
  • 19,486
  • 16
  • 84
  • 135
  • Your first question is answerable with "yes" or "no." Are you sure that's your question, or would you prefer a bit more explanation? – Robert Harvey Jul 06 '14 at 19:16
  • Your second question seems trivially answerable with "Not if the stack doesn't go too deep." – Robert Harvey Jul 06 '14 at 19:16
  • @RobertHarvey: I have tried my example and it seems to work: the execution switches back and forth between the two coroutines, but I am not sure whether this solution is idiomatic in Scheme. Maybe there is a different and more idiomatic implementation of coroutines using continuations. – Giorgio Jul 06 '14 at 19:18
  • Yes to both questions. – Basile Starynkevitch Jul 06 '14 at 19:19
  • Also, I am not sure if there does not exist an alternative implementation that does not require TCO: I think such an implementation does not exist, but I am not sure. I would like to use a similar solution in another language and I hoped there does exist a solution that works without TCO. – Giorgio Jul 06 '14 at 19:20
  • Why would you deliberately seek out a solution that's not tail-call optimized? – Robert Harvey Jul 06 '14 at 20:09
  • @Robert Harvey: Maybe I have formulated my question in the wrong way. What I mean is that I would like to find a solution that will work even without TCO. This would be useful e.g. if I wanted to use continuations to implement coroutines in Python. – Giorgio Jul 06 '14 at 20:14
  • @BasileStarynkevitch: My question has a much simpler answer than I initially thought. If you wrote your comment as an answer I would mark it as the accepted answer. – Giorgio Jul 12 '14 at 07:16

1 Answers1

1

As I commented:

  • yes, your example is an appropriate implementation of coroutines using continuations.

  • yes, tail-call optimization is mandatory for implementing coroutines with continuations.

Basile Starynkevitch
  • 32,434
  • 6
  • 84
  • 125