<div dir="ltr"><div>> The closure in g remembers that p was bound to 222, and the closure in</div><div>> h remembers that p was bound to 111.  So in the call (g 0) the result</div><div>> will be 222 (i.e. 0+222), and in the first call (h 0) the result will</div>
<div>> be 111 (i.e. 0+111).  What happens in the last expression?  The</div><div>> parameter p is dynamically bound to 333, and then h is called. Because</div><div>> h remembers that p was bound to 111, it reinstates this binding for</div>
<div>> the evaluation of its body, (+ y (p)), so the result is 111</div><div>> (i.e. 0+111).  So that's what I meant when I said "a calling function</div><div>> can't use parameter objects to pass implicit parameters to the called</div>
<div>> function".  The idiom (parameterize ((p ...)) (fn ...)) no longer</div><div>> works for passing the implicit parameter p to fn.</div><div>> </div><div>> Note that when parameter objects are automatically captured by</div>
<div>> closures, parameter objects behave like lexical variables!</div><div>> Consequently they would be completely redundant in the language.</div><div><br></div><div>I understand now. Thank you for the in-depth explanation. Okay, I</div>
<div>realize that I am asking for something slightly inconsistent. Do you</div><div>have any ideas then how I could design a library interface to</div><div>accomplish the following?</div><div><br></div><div>EXPLICIT PARAMETER PASSING:</div>
<div><br></div><div>Consider a library for managing stacks.</div><div>   (make-stack)      : Create a new stack</div><div>   (push stack item) : Pushes the item onto the stack.</div><div>   (peek stack)      : Retrieves the top of the stack.</div>
<div>   (pop stack)       : Pops off the top of the stack.</div><div>   (empty? stack)    : Checks if the stack is empty.</div><div><br></div><div>Usage of this library would be standard:</div><div>   (define (make-random-stack)</div>
<div>      (let [(mystack (make-stack))]</div><div>         (push mystack 0)</div><div>         (push mystack 1)</div><div>         (store-random-callback</div><div>            (lambda () (push mystack 0)))</div><div>         (helper mystack)</div>
<div>         mystack))</div><div><br></div><div>   (define (helper s)</div><div>      (pop s))</div><div><br></div><div>IMPLICIT PARAMETER PASSING:</div><div><br></div><div>It is annoying to have to thread the stack variable through the</div>
<div>argument lists of all my functions. So I would rather have it passed</div><div>implicitly. The (with-stack a-stack ...) command/macro tells the stack</div><div>library that I would like all following push/pop/peek commands to</div>
<div>operate on the given a-stack.</div><div><br></div><div>   (define (make-random-stack)</div><div>      (let [(mystack (make-stack))]</div><div>         (with-stack mystack</div><div>            (push 0)</div><div>            (push 1)</div>
<div>            (store-random-callback</div><div>               (lambda () (push 0)))</div><div>            (helper))</div><div>         mystack))</div><div><br></div><div>   (define (helper)</div><div>      (pop))</div>
<div><br></div><div><br></div><div>I feel that this is something reasonable to ask for, even though I now</div><div>know that there is some inherent ambiguity (related to when and when</div><div>not to capture) in the interface. </div>
<div><br></div><div>Thank you again for the in-depth explanations.</div><div>  -Patrick</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Nov 4, 2013 at 7:46 AM, Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca" target="_blank">feeley@iro.umontreal.ca</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
On Nov 3, 2013, at 2:18 PM, Patrick Li <<a href="mailto:patrickli.2001@gmail.com">patrickli.2001@gmail.com</a>> wrote:<br>
<br>
> Thank you very much for your reply Marc!<br>
><br>
> >This implementation technique has problems:<br>
> ><br>
> >- the cost of call/cc will be proportional to the number of parameters<br>
> >  (because at the time of creating a continuation you have to take a<br>
> >  snapshot of all those parameters, and when the continuation is<br>
> >  invoked you have to copy the snapshot back to the parameters)<br>
><br>
> The cost of call/cc would be proportional to the number of<br>
> parameterize (and hence dynamic-wind) calls you would be currently<br>
> nested in right? Other parts of the program, that are not as deeply<br>
> nested in parameterize, would not be affected. Is my understanding correct?<br>
<br>
</div>When a continuation is invoked, some dynamic bindings must first be undone (unwinding phase) and then some dynamic bindings myst be redone (winding phase).  So it is proportional to the sum of the nesting depths (or more precisely, the nesting depth from the closest common branches of the dynamic environment).<br>

<div class="im"><br>
><br>
> >- it doesn't handle mutation (you would have to introduce cells for<br>
> >  that, and dereference these cells when accessing the parameter)<br>
><br>
> I don't quite understand this aspect.<br>
><br>
> I expect:<br>
>    (parameterize [(a 3)]<br>
>       ... body ...)<br>
> to be roughly equivalent to:<br>
>    (let [(old-a a)]<br>
>       (dynamic-wind<br>
>          (lambda () (set! a 3))<br>
>          (lambda () ... body ...)<br>
>          (lambda () (set! a old-a))))<br>
> So I would expect to be able to use (set! a 10) from within the body<br>
> just like normal.<br>
<br>
</div>This would work in a single threaded Scheme implementation, but not in a multithreaded implementation.  For your information this is called the "shallow binding" implementation, and it was (is) used in some implementations of Lisp (google it).<br>

<div><div class="h5"><br>
><br>
> >- it doesn't work in a multithreaded environment (the content of the<br>
> >  parameters will be clobbered by the different threads operating on<br>
> >  those parameters)<br>
><br>
> Yes. Threading & Dynamic bindings together seem subtle.<br>
><br>
> >> 2) Is there a way to get a closure to close over the dynamic<br>
> >> environment? The use case is that I want to use dynamic variables<br>
> >> *purely* to avoid having to pass commonly used arguments<br>
> >> explicitly. Thus I am using the dynamic variable as an "implicitly"<br>
> >> passed argument. And in such a case, I would like closures to refer<br>
> >> to the value of the dynamic variable at the time of closure<br>
> >> creation.<br>
> ><br>
> >Closures capture the lexical variables.  Parameter objects can't be<br>
> >automatically captured by closures because that would mean that a<br>
> >calling function can't use parameter objects to pass implicit<br>
> >parameters to the called function.  So if you want to capture the<br>
> >value of some parameter objects you will have to do it manually,<br>
> >i.e. something like:<br>
> ><br>
> >(define p (make-parameter 42))<br>
> ><br>
> >(define (make-adder) ;; creates a function which adds the value of<br>
> >  parameter p (let ((captured-p (p))) (lambda (x) (parameterize ((p<br>
> >  captured-p)) (+ x (p))))))<br>
> ><br>
> >(define a (make-adder))<br>
> ><br>
> >(p 1000) ;; change value of parameter p<br>
> ><br>
> >(pp (a 10)) => 52<br>
> ><br>
> >(p) => 1000<br>
><br>
> I understand the example that you gave on how to manually capture the<br>
> parameter object. Could you clarify what you mean by "a<br>
> calling function can't use parameter objects to pass implicit<br>
> parameters to the called function"?<br>
<br>
</div></div>Lets assume that parameter objects are automatically captured by closures.  That means that when a closure is called, the dynamic bindings that were in effect at the moment that the closure was created will be reinstated at the beginning of the execution of the body of the closure.  Now examine the following code and ponder what is returned by the last 3 expressions:<br>

<br>
(define p (make-parameter 111))<br>
<br>
(define (make-adder x)<br>
  (parameterize ((p x))<br>
    (f)))<br>
<br>
(define (f)<br>
  (lambda (y) (+ y (p))))<br>
<br>
(define g (make-adder 222))<br>
(define h (f))<br>
<br>
(g 0) => ?<br>
(h 0) => ?<br>
(parameterize ((p 333)) (h 0)) => ?<br>
<br>
The closure in g remembers that p was bound to 222, and the closure in h remembers that p was bound to 111.  So in the call (g 0) the result will be 222 (i.e. 0+222), and in the first call (h 0) the result will be 111 (i.e. 0+111).  What happens in the last expression?  The parameter p is dynamically bound to 333, and then h is called. Because h remembers that p was bound to 111, it reinstates this binding for the evaluation of its body, (+ y (p)), so the result is 111 (i.e. 0+111).  So that's what I meant when I said "a calling function can't use parameter objects to pass implicit parameters to the called function".  The idiom (parameterize ((p ...)) (fn ...)) no longer works for passing the implicit parameter p to fn.<br>

<br>
Note that when parameter objects are automatically captured by closures, parameter objects behave like lexical variables!  Consequently they would be completely redundant in the language.<br>
<span class="HOEnZb"><font color="#888888"><br>
Marc<br>
<br>
</font></span></blockquote></div><br></div>