Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

REBOL SCOPING / CONTEXT EXPLANATIONS Re:(4)

 [1/3] from: larry:ecotope at: 27-Jul-2000 12:26


Hi Brett Good job! Your "explanation" agrees well with the one I came up with. Just one small correction. You wrote:
> So that "get b" evalutes to b1. "get-it b1" calls the function in the
object
> and thus returns the value of the word "b" in the object that is
associated with "b1".
> This value (a string) is then appended to the value (a block) of the
global word "var".
> 3.3.3) The same goes for the other two iterations, except that for "b3" a > function of the nested object returns the value of "a" in that context.
Actually the value of the word 'b in the object referenced by 'b1 is the block [a], which is then appended to the global 'var block. The same for 'b2. For 'b3, get-it returns the function func [][a]. So that
>> print mold var
[a a func [][a]] The 3 instances of the word 'a are each bound to the separate contexts of 'b1, 'b2, and 'b3 respectively. The evaluation of the a's is done by the print command which reduces the block before forming it, i.e. 'print block prints the string that would be produce by 'reform block.
>> reduce var
== ["Time" "for you" "to leave!"]
>> reform var
== "Time for you to leave!" I have included below the script that I used to investigate certain key points in Carl's Conundrum. It also shows how one can access the same data in the anonymous object without using the encapsulated get-it func. Of course, Carl's example was very contrived -- how else could one short script have 15 uses (one hidden in 'b2) of the word 'b. :-) BTW The problem with recycle which I posted can be avoided by naming the anonymous object created by the function 'b. o: make object! [.... Fun stuff :-) -Larry ----- Original Message ----- From: <[bhandley--zip--com--au]> To: <[list--rebol--com]> Sent: Thursday, July 27, 2000 8:45 AM Subject: [REBOL] REBOL SCOPING / CONTEXT EXPLANATIONS Re:(3)
> Well this deserved a response, since it's has consumed so much of my time
:)
> I do not claim my response to be elegant, but I hope it is practically > accurate.
<<quoted lines omitted: 10>>
> b > ]
....
> var: []
....
> foreach b b: b bind [append var get-it get b] first b
---------------snip rest----------------------- --------------code----------------------- REBOL [ Title: "Carl's conundrum" File: %context.r Date: 20-Jul-2000 ] b: function [][b] [ print ["^/function entry: local b type is" type? :b] make object! [ b1: make object! [a: "Time" b: [a]] b2: make b1 [a: "for you"] b3: make b2 [a: "to leave!" b: does [a]] set 'b [b1 b2 b3] get-it: func [b] [get in b 'b] ] print ["^/after object: global b type is" type? get in system/words 'b ] print ["^/after object: local b type is" type? :b] b ] print ["^/after function def: global b type is" type? :b] var: [] foreach b b: b bind [ print ["^/in foreach: local b is" :b "with value" mold get b] append var get-it get b ] first b print ["^/value of global b is" mold :b] print ["^/value of global var is" mold var] print var print "^/Direct access (not using get-it) to data in object^/" var2: copy [] foreach x b [append var2 get in get :x 'b] print mold var2 print var2 ;end of script

 [2/3] from: bhandley:zip:au at: 28-Jul-2000 10:51


Hi Larry,
> one small correction.
Yep. Slipped on a banana as I was nearing the finish line. I knew I should have gone to bed - just had to get rid of the "monkey on my back" :)
> Of > course, Carl's example was very contrived -- how else could one short
script
> have 15 uses (one hidden in 'b2) of the word 'b. :-)
Indeed. Apart from 'use, hopefully Carl has run out of more uses! Brett.

 [3/3] from: bhandley::zip::com::au at: 28-Jul-2000 1:45


Well this deserved a response, since it's has consumed so much of my time :) I do not claim my response to be elegant, but I hope it is practically accurate. And I want to know if I'll be eligible for that master's degree! A description of Carl's Completely Confusing Complex Contexts. b: function [] [b] [ make object! [ b1: make object! [a: "Time" b: [a]] b2: make b1 [a: "for you"] b3: make b2 [a: "to leave!" b: does [a]] set 'b [b1 b2 b3] get-it: func [b] [get in b 'b] ] b ] 1) A set-word starts evaluation, this involves first evaluating it's parameters. 1.1) The parameter to the set-word is the "function" function - it begins evaluation. 1.1.1) A "b" word is found in the vars parameter to the "function" function and thus becomes a part of the context of this function. 1.1.2) The body block becomes the body of the function. 1.2) The function returns the specified function and this used as a value to define the global word 'b var: [] 2) Another set-word is encountered and process, it results in a block being created as a value which is used to define the global word "var". foreach b b: b bind [append var get-it get b] first b 3) foreach is encountered and a new temporary context is created, and it's first parameter - the word "b" becomes part of that context. 3.1) The second parameter to foreach is evaluated. It is a set-word. The set-word evaluates it's parameter which is the global word "b". This word has the value of the function created in 1). 3.1.1) The function is evaluated. 3.1.1.1) A make is encountered and evaluated. The type parameter to the make is object! so an object definition dialect is used to interpret the spec parameter of make. This dialect applies any set-words (but no "set" expressions) in the spec block to the context of the object being created. In this case there will be four such set-words. 3.1.1.2.1) During interpretation of the object spec block, a set-word is encountered and evaluated. The parameter to this set-word is evaluated and it a make which has parameters that are evaluated. The type is object!. This initiates interpretation of the spec block using an object definition dialect. An object context will apply to the spec parameter. The spec block is evaluated and any set-words in the block apply to context of the object being created. The second set-word in this spec block sets the word "b" to have a value of a block that contains a word "a". 3.1.1.2.2) The next set-word is evaluated in the same manner as the first. The type parameter to the make is evaluated and returns an object. The spec parameter to the make is evaluated and applied (in make+datatype fashion) to the creation of a new object. 3.1.1.2.3) The next set-word is evaluted in the same manner again. During this evaluation, the second set-word in the spec block evaluates the word does . The word "does" evaluates it's parameter and returns a function that will return the word "a" in the context of the object. 3.1.1.2.4) A set expression is encountered it's parameters are therefore evaluated and this results in the word "b" of the function context having a value of a block containing the three words "b1 b2 b3". Note that each of these three words are defined in the context of the object. 3.1.1.2.5) The last set-word of this object is encountered. It results in the word "get-it" defined as having the value of a function. This function takes a parameter, locally known as "b", and applys an "in" expression to it (so "b" better be an object). The in expression finds in the context of the object a word literally named "b". The result of this is passed to the "get" expression in order to look up a value of the returned word. This then will be the result of the function. 3.1.1.2.6) The make object! is concluded and an object is returned from the evalutation. 3.1.1.2) The word "b" is evaluated and returns the value set there in 3.1.1.2.4. This then becomes the result of the function. 3.1.2) The global word "b" is then set with the value returned from the function. Ie a block of three words "b1 b2 b3". This then is returned to become the data block of the foreach. 3.2) The body parameter of foreach is evaluated. 3.2.1) bind is encountered and it's parameters are evaluated. 3.2.1.1) bind evaluates known-word parameter - thus the expression "first b" is evaluated and so this results in returning a word whose definition is in the context of the object created by the function in 3.1.1.2.6. 3.2.1.2) bind binds each word of the data block that it can find in the context of the known-word to that same context. In this case "get-it" is bound to the object context. 3.2.2) bind returns the bound block. 3.3) foreach begins evaluating the body block (bound block) for each element of it's data block - setting the value of it's iterator word as it goes. 3.3.1) On the first iteration the local "b" word is set to the word "b1". 3.3.2) The body block is evaluated. Thus append's parameters are recursively evaluated. So that "get b" evalutes to b1. "get-it b1" calls the function in the object and thus returns the value of the word "b" in the object that is associated with "b1". This value (a string) is then appended to the value (a block) of the global word "var". 3.3.3) The same goes for the other two iterations, except that for "b3" a function of the nested object returns the value of "a" in that context. Fiiiinnnalllllyyyy... print var 4) You can guess. Brett Handley. ----- Original Message ----- From: <[carl--sassenrath--com]> To: <[list--rebol--com]> Sent: Friday, July 21, 2000 3:10 AM Subject: [REBOL] REBOL SCOPING / CONTEXT EXPLANATIONS Re:(2)
> Good explanation. Fun with contexts. I like the "do" not "try" zen yoda
thing. Nice touch. Here's the next one to zen. "Fetch these objects from my hand..."

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted