guile-devel
[Top][All Lists]
Advanced

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

Re: Special variables to relax boxing


From: Noah Lavine
Subject: Re: Special variables to relax boxing
Date: Thu, 21 Mar 2013 17:11:21 -0400

Hi,

Stefan and Mark, I think you are talking past each other. Stefan is offering a very concrete definition of what he wants, and Mark is looking for a more abstract version. Here is what I think Stefan wants, in the language of R5RS' storage model:

A variable is simply a name for a particular location, and each variable refers to its own unique location. When a continuation is captured, that continuation gets its own set of "mirror" locations, one for each variable that is in the scope of the continuation. These "mirror" locations are initialized with the same values that were in the locations of the corresponding variables, but they are distinct from those locations and are immutable.

When a continuation is called, it is run in an environment in which each of the variables that was in scope when it was captured points to a new location, which is initialized to have the same value as the corresponding "mirror" location.

Note that each variable name gets at least three distinct locations in this description: the original location, when the program was being run, the "mirror" location, when the continuation was captured, and the new location, when the continuation was called. I believe this is sufficient (and necessary?) to give the semantics that Stefan wants.

Let me also give a more concrete example, which I believe captures *why* this is important. Let's say you're writing a loop with a counter that says when to terminate:

(let ((count 0))
  (let iter ()
    ... do-some-stuff ...
    (when (< count 1000000)
      (set! count (+ count 1))
      (iter))))

Now pretend that do-some-stuff captures its continuation when count is equal to 10. Stefan wants a situation where, no matter how many times that continuation is called, *each call* to the continuation will have (= count 10). I think this is very natural if you're using an imperative style, but I'm not sure what the best way is to achieve it.

Best,
Noah


On Thu, Mar 21, 2013 at 4:15 PM, Stefan Israelsson Tampe <address@hidden> wrote:
On Thursday, March 21, 2013 03:03:06 PM Mark H Weaver wrote:
> Stefan, you're still describing your proposal in terms of low-level
> implementation details such as stacks.  In the general case, we cannot
> store environment structures on the stack.  Furthermore, in the
> general case *all* variables in scheme are bound to locations, not
> values.  Only in special cases can we use stacks, and only in special
> cases can we avoid boxing variables.  These are only _optimizations_.
>
> If you're serious about this proposal, please read sections 3.1 and
> 3.4 of the R5RS carefully.  Explain your proposed _semantics_ (not
> the implementation details) in those terms, where *all* variables are
> bound to _locations_, and where there is no stack at all (everything
> is conceptually stored in a garbage-collected heap).
>
> We need to understand the *semantics* in the simplest possible terms
> before we even begin to think about how to implement it.
>
>     Thanks,
>       Mark

Ok, the sematics for the simple version is are,

Assume k, the continuation associated with with a dynamic wind or
unwind assume that there is a map from each continuation (k,id)
to a value and getting and setting of this value is done through
ref-get and ref-set, assume that the winder and the rewinder lambda
takes a first argument k beeing the continuation under action, finally
make-id will make a unique object. then the semantic would be:

(define-syntax-rule (with-special (a) code)
  (let ((id (make-id)))
    (dynamic-wind
       (lambda (k) (set! a (ref-get k id)))
       (lambda () code)
       (lambda (k)  (ref-set! k id a)))))

A possible refinment of this is
associate to k two predicates e.g.
 (do-wind? k kind) predicate and a (do-unwind? k kind) wich takes
a parameter kind, then use the semanics

(define-syntax-rule (with-special (a kind) code)
  (let ((id (make-id)))
    (dynamic-wind
       (lambda (k)
         (when (do-wind? k kind)
           (set! a (ref-get k id))))
       (lambda () code)
       (lambda (k)
         (when (do-unwind? k kind)
           (ref-set! k id a))))))

Hopes this helps!

/Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]