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

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



Vadim Nasardinov <el-vadimo@comcast.net> writes:

> By the same token, (call/cc3 call/cc4) returns k3.  So, the entire
> original thingy evaluates to (k1 k3).  This, of course, returns k3.
> Easy as pie.  Now, what on earth is k3?  At this point, I have this
> little voice in my head that tells me I would be better off, if I
> tried to visualize the tree of activation frames instead of trying to
> think about this symbolically, as Paul Graham advises.

Hmmm...I don't think so.  I think you're on the right track, but you
need to dive into the actual source of call/cc1 etc.  Here, I'll show
you my implementation (not completely working yet), annotated with
numbers [n] to denote places that continuations might jump to.

; FIXME: implement (magic).

(define (call/cc1 f)
  (let ((return-here (magic)))
    [1](f return-here)))

(define (call/cc2 f)
  (let ((return-here (magic)))
    [2](f return-here)))

(define (call/cc3 f)
  (let ((return-here (magic)))
    [3](f return-here)))

(define (call/cc4 f)
  (let ((return-here (magic)))
    [4](f return-here)))

([5](call/cc1 call/cc2) [6](call/cc3 call/cc4))

; FIXME: Hastily-compiled explanation below should be double-checked.

1. (call/cc1 call/cc2) A continuation to [5] is created, and call/cc2
is called with it as an argument.

2. call/cc2 creates a continuation to [1], and invokes [5] with [1] as
an argument.  This returns [1] to the operator position in the main
expression.

3. (call/cc3 call/cc4) A continuation to [6] is created, and call/cc4
is called with it as an argument.

4. call/cc4 creates a continuation to [3], and invokes [6] with [3] as
an argument.  This returns [3] to the operand position in the main
expression.

5. Application: ([1] [3]) makes [3] be returned at place [1], making
[3] be the returned at place [5].

6. Now the program is in the state where it evaluates
(call/cc3 call/cc4) again, again returning [3].

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.

No continuations to places [2] or [4] are created, because call/cc2
and call/cc4 only get continuations as arguments; they never get
call/cc as an argument.

-- 
"Notwithstanding fervent argument that patent protection is essential
 for the growth of the software industry, commentators have noted
 that `this industry is growing by leaps and bounds without it.'"
        -- US Supreme Court Justice John Paul Stevens, March 3, 1981.