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

Re: good pictures vs. good stories (was: Re: CPS in Parrot)



Bruce Lewis wrote:
 > 7. Application: ([3] [3]) makes [3] be returned at place [3], making
 > [3] be the returned at place [6].  This makes an infinite loop at
 > this step.

This seems to be in agreement with what the left-to-right evaluating
scheme implementations actually display, except for one minor detail.
The above explanation fails to highlight the fact that while one of
the two intertwined continuations [3] and [6] stays the same
throughout the execution of the program, the other one is constantly
recaptured.

Specifically, suppose you define

(define c1
  (lambda (x)
    (display 'c1)
    (newline)
    (display x)
    (newline)
    (call-with-current-continuation x)))

with c2, c3, and c4 defined in a similar manner.

Guile says,

    guile> ((c1 c2) (c3 c4))
    c1
    #<procedure c2 (x)>
    c2
    #<continuation 1194 @ 806b8a8>
    c3
    #<procedure c4 (x)>
    c4
    #<continuation 1194 @ 806df68>
    c3
    #<procedure c4 (x)>
    c4
    #<continuation 1194 @ 8070628>
    c3
    #<procedure c4 (x)>
    c4
    #<continuation 1194 @ 8072ce8>
    c3
    #<procedure c4 (x)>
    c4
    #<continuation 1194 @ 80753a8>
    c3
    #<procedure c4 (x)>
    c4
    #<continuation 1194 @ 8077a68>
    c3
    #<procedure c4 (x)>
    c4
    #<continuation 1194 @ 807a128>
    ...

Note that while the continuation, whose printed representation is
#<procedure c4 (x)>, stays the same, the other one constantly changes.

The MIT/GNU Scheme gives you similar results:

    1 ]=> ((c1 c2) (c3 c4))
    c3
    #[compound-procedure 1 c4]
    c4
    #[continuation 2]
    c1
    #[compound-procedure 3 c2]
    c2
    #[continuation 4]
    c1
    #[compound-procedure 3 c2]
    c2
    #[continuation 5]
    c1
    #[compound-procedure 3 c2]
    c2
    #[continuation 6]
    c1
    #[compound-procedure 3 c2]
    c2
    #[continuation 7]
    c1
    #[compound-procedure 3 c2]
    c2
    #[continuation 8]
    c1
    #[compound-procedure 3 c2]

The #[compound-procedure 3 c2] continuation remains in use throughout
the execution of the program, while the other one is constantly
recaptured.

Awright, I promise to read The Seasoned Schemer as soon as I am done
with "Effective Java", "Concurrency: State Models & Java Programs",
and "The Little Lisper".  No more stupid questions till after I've
read the book.

Anyhow, the consensus seems to be that Graham is right and trying to
visualize the invocation sequence is an exercise in frustration.  Is
that the takeaway from this discussion?


Vadim









P.S. Speaking parenthetically, MzScheme is less than useful in this
particular exercise, insofar as its printed representation of reified
continuations fails to differentiate between different continuation
objects.  It should probably adopt either the Guile, or the MIT/GNU
Scheme approach.  Here's what it currently does:

    > ((c1 c2) (c3 c4))
    c1
    #<procedure:c2>
    c2
    #<continuation>
    c3
    #<procedure:c4>
    c4
    #<continuation>
    c3
    #<procedure:c4>
    c4
    #<continuation>
    c3
    #<procedure:c4>
    c4
    #<continuation>
    c3
    #<procedure:c4>
    c4
    #<continuation>
    c3
    #<procedure:c4>
    c4
    #<continuation>
    c3