Gabrielle,

Thanks for your descriptions!  They've stimulated some additional
questions, given below.  Might I trouble you for some more ideas?

[EMAIL PROTECTED] wrote:
> 
> My model for binding/contexts is the following:
> 
>     {{context! * *}}
>      ^         | `---------> <<... 1 ...>> ; values (simplified notation)
>       \        |
>        \       `-----------> <<... a ...>> ; words (simplified notation)
>         \
>          \
> {{word! * *}}
>         |
>         V
>         << #"a" >>
> 
  ...
> 
> >> obj: make object! [
> [    a: 1
> [    b: 2
> [    ]
> >> first obj
> == [self a b]
> >> second obj
> == [
>     make object! [
>         a: 1
>         b: 2
>     ] 1 2]
>

>From other languages, I had a concept of "environment" -- basically
a dictionary ("a-list" in LISP, "hash" in Perl, etc.) which supported
looking up a variable/word/token and obtaining the corresponding
value.  That much seems compatible with the model you provided.

You've persuaded me that one part of that concept does NOT apply to
REBOL -- that of searching a chain of environments, from most-local
to most-global, to find the appropriate binding for a word.  While
that notion works well for pure lexical scoping and pure dynamic
scoping, I don't see how it effectively supports REBOL's
word-at-a-time capability accessed via 'bind.

It seems clear that context-juggling is implicitly performed via
entry and exit to the bodies of functions and 'use, as in:

    >> a: "global a"
    == "global a"
    >> f: func [][print a]
    >> g: func [/local a][a: "g's local a"  print a  f  print a]
    >> h: func [][use [a][a: "h's local a" print a g print a]]
    >> h
    h's local a
    g's local a
    global a
    g's local a
    h's local a
    >>

I'm not clear on whether it's better to think of the above as

1)  a single word 'a whose context is being magically managed by
    the interpreter, or

2)  three words, whose names happen to share the same spelling.

To display my ignorance (and confusion) on this point, consider this
console transcript:

    >> a: 1
    == 1
    >> b: 2
    == 2
    >> e: [a b c]
    == [a b c]
    >> reduce e
    ** Script Error: c has no value.
    ** Where: c

That was expected, as I had not defined 'c previously.

    >> f: func [n /local c] [c: n  bind e 'c  print e]
    >> f 12
    1 2 12

So far, still expected, as 'f supplies a context for 'c to be
evaluated in.

    >> e
    == [a b c]
    >> print e
    1 2 12
    >> c
    ** Script Error: c has no value.
    ** Where: c
    >> same? 'c third e
    == false

This WASN'T what I expected.  It appears that the third element of
'e is a different 'c from the one whose name I can type into
the console.

    >> g: func [m /local a b] [bind e 'a  a: m  b: m + 1  print e]
    >> g 20
    20 21 12
    >> e
    == [a b c]
    >> print e
    20 21 12

No surprise here.

    >> h: func [][bind e third e  print e]
    >> h
    20 21 12

Hmmmm.  Within 'f (where we've bound 'c) the words 'a and 'b would
have evaluated globally.  However, attempting to bind 'e back to
that context doesn't restore 'a and 'b (in e!) to refer to the
global 'a and 'b.

    >> bind e 'f
    == [a b c]
    >> print e
    ** Script Error: c has no value.
    ** Where: c
    >> a
    == 1
    >> print first e
    a
    >> print get first e
    1
    >>

But 'f is defined in the global context for which 'a -> 1, 'b -> 2,
and 'c is undefined.  So, although it seems clear why this last use
of 'bind restored 'a and 'b (in 'e!) to their global binding, it's
not immediately obvious to me why 'c (in 'e) got clobbered (when
the 'bind in 'g didn't affect it).

Any suggestions?

> 
> So, every word simply mantains a reference to a context (and
> probably, for efficiency, directly to its value in that context
> --- this could be proven in the previous version of REBOL),

Fascinating!  Could you provide a code speciment that demonstrates?

> and
> all the functions that modify the binding of a word simply change
> that reference.
> 
> This has allowed me to explain almost every case I encountered in
> REBOL so far; I hope it can be useful. I don't know how much
> accurate it is.
> 

I'm still puzzled about what appears to be evidence of TWO words
named 'c above.  I'd be very interested in your thoughts on that.

-jn-

Reply via email to