From: sm86+@andrew.cmu.edu (Stefan Monnier)
Newsgroups: comp.lang.scheme
Subject: Re: trampolines
Date: 5 Sep 92 08:32:52 GMT
Article-I.D.: andrew.oeeC4oG00awM0Eim9c
	<shrchin.715668205@reading>
Organization: Junior, Math/Computer Science, Carnegie Mellon, Pittsburgh, PA

Excerpts from netnews.comp.lang.scheme: 5-Sep-92 Re: trampolines
Jonathan H. N. Chin@csug (3077)
> Rats. Thought I had tested that code. Obviously not. Okay then. How about
> we add another level thus:

> (define make-trampoline[2]
>   (lambda (proc)
>     (letrec ((dummy (lambda()
>                       (let ((result (proc)))
>                         (begin
>                           (set! dummy (lambda()
>                                         result))
>                            result)))))
>       (lambda()
>         (dummy)))))

> I tried this code, and it worked (or at least it seemed to... 8^).

> However, I now have an extra layer of indirection -- the extra function call.
> So, have I just lost the advantage of removing the test in the first place?

> I tend to think in C terms. Would I be correct to think that in code such as:

> (define x 0)
> (define trampoline (make-trampoline (lambda()
>                                       (+ x 1)))) 

> that `trampoline' "points to" the closure created by the `letrec', rather
> than pointing to `the-promise' which points to the closure. And hence when
> `the-promise' changes, there is no affect on `trampoline'. Something like:

>                      trampoline

>                          |
>                          v

>    the-promise  -->  #<closure>

> instead of:

>    trampoline --> the-promise --> #<closure>

> The question I then have is why does changing `dummy' in `make-trampoline[2]'
> affect the closure "(lambda() (dummy))" although changes to `the-promise'
> in the definition of `trampoline' (substituting `make-trampoline' for
> `make-trampoline[2]') are *not* propagated?

Because (lambda () (dummy)) is an object which holds a 'pointer' to the
variable 'dummy' whereas 'the-promise' is an object which only holds
the value of the promise !
In the lambda, the dummy won't be substituted with its value, but
with a 'pointer' to its value ! this additional level of indirection permits
the 'propagation'

> From my previous post:
> 1) What prevents the R^4 `make-promise' from recursing indefinitely?

Termination of make-promise is not a problem: it returns a procedure.
When you try to get the value of the promise, termination is directly
dependant of termination of the evaluation of the promise.
That seems logical !
But the second if is necessary, because the promise may refer recursively
to its value (and terminate cause the recursive call is dependant of an
external variable, for example)

> and now
> 2) Does `make-trampoline[2]' save over `make-promise' ?
> 3) If (2), how do I extend `make-trampoline[2]' to allow promises that
>    refer to their values?

> Thanks again,
> Jonathan
> -- 
> Jonathan H N Chin (9 kyu) \ Dept. of Cybernetics, \ "Respondeo, etsi mutabor"
> shrchin@uk.ac.rdg.susssys1 \ University of Reading \
> bq305@cleveland.freenet.edu \ Box 225, Whiteknights \ < Rosenstock-Huessy >
> jockstrap,mandy@CyberSpaceII \ Reading, RG6 2AY, U K \


	Stefan Monnier


-----------------------------------------------------
-- On the average, people seem to be acting normal --
-----------------------------------------------------
Newsgroups: comp.lang.scheme