[REBOL] Re: Binding
From: robert:muench:robertmuench at: 20-Nov-2002 9:36
> -----Original Message-----
> From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]
> On Behalf Of Ladislav Mecir
> Sent: Wednesday, November 20, 2002 2:37 AM
> To: [rebol-list--rebol--com]
> Subject: [REBOL] Re: Binding
> > in Rebol a block is just a
> > container. The words inside the block carry the context around.
>
> Well said.
Hi, :-) fine! It seems that I slowly understand the internal structure
better and better.
> Of course, you can collect sample words of some contexts and
> check all words which context they are from.
Yes, but I need to know about the context ;-)
> The trouble is, that some context are "being created" during the
> evaluation of a rebol script,
That's what I expected now. This is a bit like the "anonymous unions".
And now I see why we have to specify know-words for BIND for example:
The context can't be referenced, only words from the context. We have to
keep track of the contexts in mind. What I realy would like is a
context-querying thing.
> which means, that your collection may never be complete.
>
> Example:
>
> use [x] ['x]
>
> Whenever the USE function is evaluated, it creates a new
> context, i.e. if you evaluate the above code line three
> times, you will create three new contexts.
Ok, and if you don't assing it to a word, your context isn't
referenceable. It will be used implicitly but can't be accessed
explicitly.
> Sample context-creating functions: USE, MAKE (OBJECT! FUNCTION!),
> REPEAT, FOREACH. Any function from the above list creates
> exactly one context whenever it is evaluated.
Is there a list of all functions that create contexts? IMO this would
help a lot to understand all this.
> Illustration:
>
> block: []
> loop 2 [append block use [x] ['x]] ; == [x x]
> set block/1 1
> set block/2 2
> reduce block ; == [1 2]
This code realy looks crazy to people not used to this. So it would be
possible to write a program only using the word X in different contexts.
I don't want to reverse engineer such a code ;-).
BTW: Do you think it would be possible to write a function that
minimizes the number of contexts for a rebol script? And that replaces
as much words with the same name as possible.
> > I read somewhere in your ...
> > articles that the context of a word can be changed. Is this right?
>
> 1) Yes. That is what e.g. BIND normally does for Rebol
> blocks. It replaces the words contained in the block by their
> counterparts bound to the specified context, if possible.
In this case I would say it a bit different: BIND uses the given word as
a human-readable-descriptor. The word has no meaning at all, it's just a
description for BIND that specifies what to search in the context where
the KNOWN-WORD is included and than replaces the descriptor with a word
that has the same descriptor (IIRC you call it the argument of a word)
as the specified one, the same context as KNOWN-WORD and things like the
value of the word etc. are than accessable.
> 2) No. You cannot change the context of a word without
> replacing the word. (You may ignore this, if it isn't
> understandable for you ATM)
I think I understand this now. The key-word here is REPLACE, the word
looks like the same but it's not. So can we say what is normaly known as
a variable and that is referenced by name in Rebol a variable has two
properties: A name and a context. Only this combination is a full
qualifier for a "variable".
> Only the context as whole can be freed.
Hmm... Isn't this now a bit different in semantics to what we said
before? We are talking about blocks as mixed-context-containers. Ok. We
have contexts as a collection ("block") of words that share the property
same-context but don't have any functional meaning (a context is just a
collection and not a sequence that can be evaluated). So blocks and
contexts are orthogonal.
So why can't a word be removed from a context? Imagine a context with
1000 words and only 1 is still in use. Than i have 999 words that are in
memory but not used. Does this explain the memory footprint of Rebol?
> If you have got at least one context word, you are (at least
> theoretically) able to get all words of the specified context using
> BIND.
But we can't query the context, right? I can't say something like:
foreach word get-context KNOWN-WORD [?? word]
> It might be a good start to colorize the code from the
> Computed Binding chapter of Contexts.html, what do you think?
Good idea! I think I'm going to write an other "words & context"
article: Words & Contexts for dummies or something like this. And using
colors to show the contexts is a very nice way :-)). Maybe you can alter
your stuff too.
> > I think I now know why: Because the words carry the context around,
> > ... you have a set of words with a lot of different contexts, which
> > are used in the actual block.
>
> That's it.
Well, fine. It takes some time but understanding all this is very
important to get the spirit of Rebol. I hope others learn as much as I
do. Robert