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

Continuations



Unfortunately, I think that Common Lisp style multiple-values provide
something which cannot be conveniently obtained with explicit
continuation passing.

Consider the problem of writting a mapping procedure, MVMAP, like MAP
(previously MAPCAR) with the constraint that (MVMAP f . lists) return
as many lists of values as f returns values (assuming that f returns
the same number of values on every invocation).  Then (MAP f . lists)
is equivalent to (MVMAP f . lists) if f returns a single value.

The problem writting it in continuation passing style is that f must
expect a continuation given to it by MVMAP, but then +, for example,
cannot be used since it does not expect an explicit continuation.
I agree that this can be "patched" by doing something like

(mvmap explicit->implicit (implicit->explicit +) . lists)

where EXPLICIT->IMPLICIT is the explicit continuation to MVMAP, used
to pass on the "return" value to the implicit continuation, and is defined by

(define (explicit->implicit val) val)	; Gives error if more than one value

and IMPLICIT->EXPLICIT makes a procedure, which only expects an
implicit continuation, accept an explicit continuation:

(define (implicit->explicit f)
  (lambda (cont . args)
    (cont (apply f args))))

but this is somewhat clumsy, especially if MVMAP were to replace MAP,
as it probably should if it were implemented.

The problem arises because in Common Lisp returning one value is not
much different from returning several, since almost all of the burden
is put on the "receiving" end (and, of course, in VALUES).  By
implementing multiple values in terms of explicit continuation
passing, we make procedures which potentially "return" multiple values
(even if they happen to always "return" one) quite different from
"normal" procedures since these do not expect an explicit
continuation.