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

[subject: error and trial]

 [1/15] from: rebol665:ifrance at: 4-Mar-2002 22:54


Hi rebollers, I am coming again with a question posted on November the 23th 2001. At the time, I thought I had understood Ladislav's answer, but the truth is I am not so sure. All began while reading Ladislav's contexts.html... There are Words that do not have the ability to refer to Rebol values. We can call them undefined Words. [ ...] This function can be used to find out, if a Word is undefined: undefined?: func [ {determines, if a word is undefined} word [any-word!] ] [ error? try [error? get/any :word] ] My question was "why use error? two times?". In my eyes, a more simple version of the function could be : u-def?: func [ {determines, if a word is undefined} word [any-word!] ] [ error? try [get/any :word] ] We can test these two functions whith Ladislav's own example of an undefined word. a-word: first first rebol/words ;== end!
>> undefined? a-word
== true
>> u-def? a-word
== true
>> undefined? 'print
== false
>> u-def? 'print
== false I am not pretending I am right, because I must be wrong! but I deeply need to understand where and why ! Patrick PS: this message has been posted twice because I realized that I did not use the right "from" (the one who is listed) for the first one. You should receive this one first if I am not wrong (again).

 [2/15] from: ryanc:iesco-dms at: 4-Mar-2002 15:41


Your suspicions are right, it is completely unnecessary. pat665 wrote:

 [3/15] from: lmecir:mbox:vol:cz at: 5-Mar-2002 8:46


Hi Pat, your code is almost correct, it just omits one special case. See the following example:
>> type? a: make error! "my error"
== error!
>> u-def? 'a
== true
>> undefined? 'a
== false Cheers L ------------------ From: "pat665" Sent: Monday, March 04, 2002 10:54 PM Subject: [REBOL] [subject: error and trial] Hi rebollers, I am coming again with a question posted on November the 23th 2001. At the time, I thought I had understood Ladislav's answer, but the truth is I am not so sure. All began while reading Ladislav's contexts.html... There are Words that do not have the ability to refer to Rebol values. We can call them undefined Words. [ ...] This function can be used to find out, if a Word is undefined: undefined?: func [ {determines, if a word is undefined} word [any-word!] ] [ error? try [error? get/any :word] ] My question was "why use error? two times?". In my eyes, a more simple version of the function could be : u-def?: func [ {determines, if a word is undefined} word [any-word!] ] [ error? try [get/any :word] ] We can test these two functions whith Ladislav's own example of an undefined word. a-word: first first rebol/words ;== end!
>> undefined? a-word
== true
>> u-def? a-word
== true
>> undefined? 'print
== false
>> u-def? 'print
== false I am not pretending I am right, because I must be wrong! but I deeply need to understand where and why ! Patrick

 [4/15] from: rebol665:ifrance at: 5-Mar-2002 13:32


Hi Ladislav, I was sure I was missing something. Very clever indeed and elegant once one has got it ! May I respectfully suggest that a little note or comment in contexts.html would help the reader ? Patrick

 [5/15] from: reboler::programmer::net at: 5-Mar-2002 9:28


I, too am unclear on the uses of 'undefined? Is it any different from using "not value? 'word" ? Much easier to use an existing native value, right? ;)

 [6/15] from: lmecir:mbox:vol:cz at: 5-Mar-2002 17:34


Hi Alan, unfortunately, it is a different notion. See the following example: unset 'a value? 'a ; == false undefined? 'a ; == false value? first first rebol/words ; == false undefined? first first rebol/words ; == true set 'a 1 ; == 1 set first first rebol/words 1 ** Script Error: end! is not defined in this context ** Near: set first first rebol/words 1 The difference is, that a defined word can either have a value or not, while an undefined word cannot have a value. Cheers L ----- Original Message ----- From: "alan parman" Sent: Tuesday, March 05, 2002 3:28 PM Subject: [REBOL] Re: [subject: error and trial] I, too am unclear on the uses of 'undefined? Is it any different from using "not value? 'word" ? Much easier to use an existing native value, right? ;)

 [7/15] from: reboler:programmer at: 5-Mar-2002 12:41


How about a new value confusing? As in confusing? 'undefined? ==> true :) I think I'm starting to see the difference.
>> probe new-word
** Script Error: new-word has no value ** Where: halt-view ** Near: probe new-word
>> value? 'new-word
== false
>> undefined? 'new-word
== false Is this the answer you want for 'undefined? ? Seems that this should give 'true ? Or is this the usage, undefined? 'word ==>true means that it is defined but has no value ? (makes the word 'undefined? confusing!) While value? 'word ==> false means that it is not defined (and therefore cannot have a value) ? How does one define a new word but give it no value. I still can't wrap my mind around the following...
>> nn: none
== none
>> probe nn
none == none
>> value? 'nn
== true
>> undefined? 'nn
== false
>> unset 'nn >> value? 'nn
== false
>> undefined? 'nn
== false And how about the following. Doesn't your example just show a deficiency in using 'first?
>> set in rebol/words 'end! 1
== 1
>> probe end!
1 == 1
>> value? 'end!
== true
>> undefined? 'end!
== false
>> unset end! >> unset 'end! >> value? 'end
== false
>> undefined? 'end
== false Here is another example from a fresh console session, without using 'first.
>> undefined? (in rebol/words 'end!)
== false

 [8/15] from: ryanc:iesco-dms at: 5-Mar-2002 10:34


Previously I wrote that the second 'error? was completely unnecessary. I stand corrected! Coincidently I was just explain to a coworker, that while rebol can be "spoken" easily like natural languages, in certain instances it can difficult to determine the exact interpetation like natural languages too. --Ryan Ladislav Mecir wrote:

 [9/15] from: holger:rebol at: 5-Mar-2002 10:59


On Tue, Mar 05, 2002 at 12:41:50PM -0500, alan parman wrote:
> How about a new value confusing? > As in confusing? 'undefined? ==> true :)
A little background information: In REBOL everything is a value. That includes words. So internally, something like 'foo or first [foo] is a value just like any other value (integers, blocks etc.). Values have properties that distinguish them from other values of the same type. For instance the (only) property of an integer! value is the number itself. block! values have two properties: a reference to the block series (which can be shared with other block! values), and an index. With this point of view, values of type word! have properties, too, exactly two of them: one is the spelling of the word, the other one is the context the word is bound into. Contexts are name-value lookup tables which are used to hold "variables" for functions, objects and use []. A context consists of two tables: a table of words and a table of corresponding values. For object!s you can actually look at those two tables:
>> a: make object! [b: 1 c: 2] >> first a
== [self b c] ; The table of all words in the context
>> second a
== [ ; The table of all corresponding values in the context make object! [ ; the first one being the object itself (self) b: 1 c: 2 ] 1 2] There is also one "global context" in REBOL that is used for globally defined variables. Almost all words used in REBOL are automatically bound into the global context (usually during 'load). Some words are later rebound into a different context, but only after they were first bound into the global context. For instance the words that appear inside of the body of a function are first bound into the global context when your script is loaded. Later, when REBOL executes the function definition, the words in the body of the function are rebound into the context created for the function. As a result, all words in a normally loaded REBOL script are always bound into some context, either the global context or some other context. Binding a word into a context does not assign a value to it. It only associates a word with a context. The value of a word in a context is defined separately, e.g. using 'set or the set-word notation. It IS possible to create words which are not bound into any context at all, and that is what Ladislav's undefined? function detects. A better name would be unbound?. It is not possible to assign a value to unbound words, because there is no context to hold the value. Unbound words are relatively rare in normal use (which is why you rarely see undefined? return true), but they do occur, usually in one of three ways: - Some natives that return words as symbols return unbound words. An example is "type?". It returns a word describing the datatype of the argument. That word is an unbound word:
>> undefined? type? 1
== true - "first" applied to an object! returns a block of words (the "left" half of the name-value lookup table used by the context of the object!). The words in that table are unbound. This also includes first system/words (because system/words is an object representing the global context).
>> a: make object! [b: 1] >> undefined? second first a
== true - to-block applied to a string! is similar to "load", but, unlike "load", does not bind the resulting words into the global context. All words remain unbound.
>> a: to-block "b" >> undefined? first a
== true In almost every other situation undefined? returns false, because the word has been bound into a context. In contrast, value? only returns true if a word is bound into a context AND in that context the value of the word has been defined (i.e. is different from unset!). The effect of the unset native is only to set the value of a word in its context to unset! (so value? subsequently returns false). It does not "unbind" a word from a context, i.e. the value returned by undefined? is not affected by it. The part that, at first, may make these concepts a little confusing, is that many users intuitively associate a word only with its spelling, and ignore the context. For instance:
>> type? 1
== integer!
>> undefined? type? 1
== true
>> undefined? integer!
== false In this example two different values representing words are used, which are both spelled "integer!": the first is the one returned by "type? 1". It is not bound into a context, so undefined? returns true. The second is the one typed into the console. When REBOL interprets the sequence of characters you type, it first loads it, which binds all words into the global block, so for THAT instance of the word "integer!" undefined? returns false. -- Holger Kruse [kruse--nordicglobal--com]

 [10/15] from: reboler:programmer at: 5-Mar-2002 14:58


Thanks, Holger! Do I have the following essentially correct?
>> probe end!
** Script Error: end! has no value ** Where: halt-view ** Near: probe end! ;rebol has not "seen" this word in the global context
>> probe first first rebol/words
end! ;rebol has seen this word before, in 'rebol/words == end!
>> undefined? first first rebol/words
== true ;'end! is not in the global context
>> undefined? in rebol/words 'end!
== false ;'end! is in the context of 'rebol/words
>> value? first first rebol/words
== false ;'end! does not have a value in the global context
>> value? in rebol/words 'end!
== false ;'end! does not have a value in the context of 'rebol/words

 [11/15] from: larry:ecotope at: 5-Mar-2002 12:16


Hi Holger, Thanks for the excellent discussion of this often confusing topic. It would be of great benefit to all REBOL programmers, if this material were present in the REBOL documentation. It should include mention of the "properties" of each REBOL type accessible to the programmer. The REBOL learning curve would be greatly improved, as I think many of us proceed on the basis of a somewhat incorrect mental model of values, etc. and then find ourselves caught short when something unexpected happens. -Larry

 [12/15] from: holger:rebol at: 5-Mar-2002 12:31


On Tue, Mar 05, 2002 at 02:58:27PM -0500, alan parman wrote:
> Thanks, Holger! > > Do I have the following essentially correct? > > >> probe end! > ** Script Error: end! has no value > ** Where: halt-view > ** Near: probe end! ;rebol has not "seen" this word in the global context
No, REBOL has seen the word, right after you typed it in, REBOL loaded your "script" and bound it into the global block. That's why undefined? would return false. You get the error because the word does not have a value in the global context (which is where the word was bound into).
> >> probe first first rebol/words > end! ;rebol has seen this word before, in 'rebol/words > == end!
Yes.
> >> undefined? first first rebol/words > == true ;'end! is not in the global context
Actually 'end! does appear in the global context:
>> found? find first rebol/words 'end!
== true True is returned in your example because words which appear in the word table of a context are always unbound.
> >> undefined? in rebol/words 'end! > == false ;'end! is in the context of 'rebol/words
Yes.
> >> value? first first rebol/words > == false ;'end! does not have a value in the global context
No, that does not matter in this example. See
>> a: 1 >> value? first find first rebol/words 'a
== false The reason why you are getting a 'false response is because you are getting the word from the word table of a context. Those words are always unbound, and therefore do not have a value. Whether a word with the same spelling, bound into that context, would have a value or not does not matter in the example.
> >> value? in rebol/words 'end! > == false ;'end! does not have a value in the context of 'rebol/words
Yes. Note that the context of rebol/words IS the global context. There is a difference between using 1. first find first some-context 'some-word (or variations of it) and 2. in some-context 'some-word 1 returns a word directly taken from the context's word table. Those words are always unbound, regardless of whether that same word, if bound into the context, has a value or not. This means value? on such a word always returns false?, undefined? always returns true. 2 creates a word that has the same spelling as the word you supply, but bound into the context (or none if the word does not appear in the context). Assuming the word does appear, value? on such a word tells you whether the word has a value in that context, undefined? always returns false. -- Holger Kruse [kruse--nordicglobal--com]

 [13/15] from: lmecir:mbox:vol:cz at: 6-Mar-2002 8:35


Hi all, thanks to Holger for his explanation, <<Holger>> ... A better name would be unbound?. ... <</Holger>> I feel, that the word 'undefined? may be confusing too. If we can agree on a better notion, I will change my text. Any votes (or other candidates)? Cheers L

 [14/15] from: rebol665:ifrance at: 6-Mar-2002 10:04


Hi Ladislav, I vote for Unbound? Patrick

 [15/15] from: rotenca:telvia:it at: 6-Mar-2002 17:55


As you know, 'unbound was my preferred by me, but this is against your (old?) idea that the undefined/special words are bound to a undefined/special context. --- Ciao Romano