Benefits of continuation?

Quinn Dunkan quinn at regurgitate.ugcs.caltech.edu
Fri Nov 3 13:10:06 EST 2000


On Fri, 03 Nov 2000 10:35:36 GMT, rerwig at my-deja.com <rerwig at my-deja.com> wrote:
>In article <ljt40tg9v67rf4iksb9p2olibo2q36iaur at 4ax.com>,
>  Courageous <jkraska1 at san.rr.com> wrote:
>> On Fri, 03 Nov 2000 08:00:22 GMT, rerwig at my-deja.com wrote:
>>
>> >I think I begin to understand the basics of continuation.
>> >But what I don't understand is how it can help me to create better
>> >code. In other words, what are the benefits on using continuation?
>>
>[snip]
>>
>> The real question is do you have a situation in which you need to
>> keep "restore points" at various times during execution so that you
>> can later go to them? If you don't, you almost certainly do not have
>> any reason to use continuations directly.
>>
>I think this remark above is very true. Context switches was the very
>first brain wave I had. Makes me wonder whether continuation actual ever
>will be used directly. Indirect usage ("under-water", hidden, etc.)
>seems more likely.

A popular use for continuations is to implement "ambiguous" or "multiple
return" values via backtracking.  For instance, in sather, one has 'iterators'
which act like little continuations.  A python-esque translation:

class IterList:
    def __init__(self, data=None):
        if data is None:
            self.data = []
        else:
            self.data = data
    def getitem!(self):
        # of course, if we used iterators, 'for' would be an iterator too
        for i in range(len(self.data)):
            yield self.data[i]
        quit

a = IterList([1,2,3,4])
b = IterList(['a', 'b', 'c'])
loop:
    m = a.getitem!()
    n = b.getitem!()
    print m, n

# prints:
# 1 a
# 2 b
# 3 c
# 
# notice the first fail quits the loop... a more icon-ish way would have
# each b.getitem! fail backtracks to a.getitem! so you'd have:
# 1 a
# 1 b
# 1 c
# 2 a
# 2 b
# 2 c
# etc.

Among other things, this would finally put that whole
'while line = f.readline()' thread to a rest:

loop:
    line = fp.getline!()
    ...

Notice this sort of thing achieves basically the same sort of result as a lazy
list.  If it seems an awful lot to add to a language just to avoid typing
'if not line: break', there are other things you can do with them :)

Obviously, python isn't going to add a whole bunch of keywords to support this
(it'd have to go remove the old control structures to not accumulate cruft so
it'd be a different language at that point anyway), but something equivalent
could be faked up with continuation objects.  I haven't given it a lot of
thought yet, but I'd like to be able to say something like:

iter = a.iterator()
print iter.val
iter.loop() # bounces back up to 'iter = a.iterator()'

which is probably possible by wrapping continuation objects...


Also, I gather that compiler/interpreter writers are often interested in
passing continuations around "in the buff" to implement things like control
structures and functions in their languages.



More information about the Python-list mailing list