The evaluation semantics
[1/18] from: behrangsa::gmail::com at: 30-Aug-2005 12:20
Hi
I'm kinda new to REBOL. Actually I was a member of this list about two
years ago and I was registered with my Yahoo! account. I felt like
that I'm really missing REBOL so I've joined the list once again :-)
In the online manual at
http://www.rebol.com/docs/core23/rebolcore-3.html it's written that:
The values and words of a block are always evaluated from first to last...
So I expected the following statement, without the parantheses of
course, return 6 but it is erroneous:
length? "boat" + 2
Can someone please explain the reason behind this behaviour?
Thanks in advance,
--
Behrang Saeedzadeh
http://www.jroller.com/page/behrangsa
[2/18] from: antonr:lexicon at: 30-Aug-2005 18:02
Aha, you stumbled upon the "weird" behaviour of
rebol's OP! datatypes.
>> type? :+
== op!
Yes, the infix operators (+ - * /) behave a bit differently
than other "normal" rebol functions and actions.
The explanation I remember is that an expression such as:
arg1 + arg2
becomes (internally)
+ arg1 arg2
so that it is then like any other rebol function.
In your example, the + would have jumped just in
front of the string, and then took two arguments
boat
and 2, and tried to add them together.
You can rewrite it like this:
+ length? "boat" 2
But what we usually end up writing is:
(length? "boat") + 2
or
2 + length? "boat"
being conscious that in tight loops parens slow things down a tiny bit.
Anton.
[3/18] from: rebolek::gmail::com at: 30-Aug-2005 11:47
On 8/30/05, Anton Rolls <[antonr--lexicon--net]> wrote:
> Aha, you stumbled upon the "weird" behaviour of
> rebol's OP! datatypes.
<<quoted lines omitted: 6>>
> becomes (internally)
> + arg1 arg2
Little bit off-topic:
I was wondering, what is faster?
arg1 + arg2
or
add arg1 arg2
?
May seem strange, because first option must be internally translated, but
it's faster then 'add.
>> x: now/time/precise loop 10000000 [1 + 1] probe now/time/precise - x
0:00:02.719
== 0:00:02.719
>> x: now/time/precise loop 10000000 [add 1 1] probe now/time/precise - x
0:00:03.86
== 0:00:03.86
Bye, Rebolek
[4/18] from: gabriele::colellachiara::com at: 30-Aug-2005 12:38
Hi Boleslav,
On Tuesday, August 30, 2005, 11:47:08 AM, you wrote:
BB> May seem strange, because first option must be internally translated, but
BB> it's faster then 'add.
>>> x: now/time/precise loop 10000000 [1 + 1] probe now/time/precise - x
BB> 0:00:02.719
BB> == 0:00:02.719
>>> x: now/time/precise loop 10000000 [add 1 1] probe now/time/precise - x
BB> 0:00:03.86
BB> == 0:00:03.86
The reason is that + is somewhat treated like a keyword, while ADD
is not.
Regards,
Gabriele.
--
Gabriele Santilli <[gabriele--rebol--com]> --- http://www.rebol.com/
Colella Chiara software division --- http://www.colellachiara.com/
[5/18] from: antonr:lexicon at: 31-Aug-2005 18:56
That's what I thought.
>> type? :+
== op!
>> type? :add
== action!
I think the op!s are more fundamental. They probably get
defined first or are earlier in the symbol lookup list.
I'm only speculating but it could be something like that.
Anton.
[6/18] from: gabriele::colellachiara::com at: 31-Aug-2005 12:33
Hi Anton,
On Wednesday, August 31, 2005, 10:56:43 AM, you wrote:
AR> I think the op!s are more fundamental. They probably get
AR> defined first or are earlier in the symbol lookup list.
AR> I'm only speculating but it could be something like that.
It's not just the datatype, it the word itself being treated
specially.
>> set '+ func [a b] [print [a b]]
>> 1 + 2
** Script Error: Invalid operator: +
** Near: 1 + 2
Regards,
Gabriele.
--
Gabriele Santilli <[gabriele--rebol--com]> --- http://www.rebol.com/
Colella Chiara software division --- http://www.colellachiara.com/
[7/18] from: antonr::lexicon::net at: 31-Aug-2005 20:50
Yes, you're right. There's something strange going on there.
Anton.
[8/18] from: henrik::webz::dk at: 3-Sep-2005 14:01
On Aug 30, 2005, at 9:50 AM, Behrang Saeedzadeh wrote:
> Hi
> I'm kinda new to REBOL. Actually I was a member of this list about two
<<quoted lines omitted: 9>>
> length? "boat" + 2
> Can someone please explain the reason behind this behaviour?
That's because length? "boat" returns it's value to the physical left
of itself, e.g.:
4 <- length? "boat"
and not
length? "boat" -> 4
+ requires a value on each side where the one on the left, returns a
value to its right, e.g.:
<value> -> + <- <value>
but your statement produces:
<- <value> + <- <value>
The (very) new Wikibook has a bit about this, actually:
http://en.wikibooks.org/wiki/
Programming:REBOL#Simple_Programming_in_the_Console
The part about parentheses is not entirely accurate yet, but it
should fit with what you need.
--
Regards,
Henrik Mikael Kristensen
[9/18] from: lmecir::mbox::vol::cz at: 3-Sep-2005 18:44
Henrik Mikael Kristensen napsal(a):
>On Aug 30, 2005, at 9:50 AM, Behrang Saeedzadeh wrote:
>>Hi
<<quoted lines omitted: 36>>
>Regards,
>Henrik Mikael Kristensen
Actually, the reasoning is different. The fact is, that the + operator
has precedence over the length? function. Therefore the first opearation
the interpreter tries to evaluate is:
"boat" + 4
, which results in an error.
-L
[10/18] from: lmecir:mbox:vol:cz at: 3-Sep-2005 20:07
I was curious what is a more expected precedence for unbiased people,
and it looks, that the expression:
abs -4 + -5
is rather expected to yield -1 than 9.
Moreover, the evaluation order in Rebol can be called exception-based,
because in case:
abs -4 + -5
the operator + takes precedence, while in case
-4 + abs -5
the function ABS is evaluated as first. These aren't all the evaluation
exceptions in Rebol, the quantity of evaluation exceptions is quite high.
-L
[11/18] from: volker::nitsch::gmail::com at: 3-Sep-2005 22:14
On 9/3/05, Ladislav Mecir <[lmecir--mbox--vol--cz]> wrote:
> I was curious what is a more expected precedence for unbiased people,
> and it looks, that the expression:
<<quoted lines omitted: 7>>
> the function ABS is evaluated as first. These aren't all the evaluation
> exceptions in Rebol, the quantity of evaluation exceptions is quite high.
To me the number of exceptions is quite low:
Rebol looks at the next two values at the same time.
If you have a value followed by an operator,
it is evaluated first. If it is followed by another value+operator,
these are evaluated left to right.
Else it uses the next value, as we are used too.
abs -4 + -5
rebol sees "abs -4". No op, so takes "abs".
Then "-4 +". Operator.
Now it introduces a paren (so to speak).
That gives "abs ( -4 +"
First thing is closing that paren with the -5:
"abs ( -4 + (-5) )"
and that gives 9.
-4 + abs -5
rebol sees "-4 +". introducing paren:
(-4 +
. The right side has no more operators, so rebol sees only "abs".
Normal evaluation. "Abs" needs one arg.
There is "-5 end-of-block". No operator.
So "abs -5". That is "joined":
( -4 + (abs - 5) )
As a result, an operator can "steal" an argument from the left
function. Thus with
abs -4 + -5
the "+" can steal the -4. with
-4 + abs -5
there is nothing which can be stolen from.
That is why rebol-conditions sometimes looks "reversed",
if "volker" = ask "your name?" [..]
instead of
if ask "your name?" = "volker" [..]
The "=" cant steal in the second case.
Thus we need to write
if (ask "your name?") = "volker" [..]
and since we are lazy we save that paren. :)
> -L
>
> --
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
>
--
-Volker
Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem.
David
Wheeler
[12/18] from: lmecir::mbox::vol::cz at: 3-Sep-2005 23:10
Volker Nitsch napsal(a):
>On 9/3/05, Ladislav Mecir <[lmecir--mbox--vol--cz]> wrote:
>>
<<quoted lines omitted: 68>>
>indirection. But that usually will create another problem." David
>Wheeler
As I said, there *are* exceptions:
abc: func [:x] [abs x]
abc -4 + -5 ; == -1
-L
[13/18] from: volker:nitsch:gma:il at: 4-Sep-2005 1:21
On 9/3/05, Ladislav Mecir <[lmecir--mbox--vol--cz]> wrote:
> Volker Nitsch napsal(a):
>
> >On 9/3/05, Ladislav Mecir <[lmecir--mbox--vol--cz]> wrote:
> >
> As I said, there *are* exceptions:
>
> abc: func [:x] [abs x]
> abc -4 + -5 ; == -1
>
Agreed, and
abc: func ['x] [abs x]
But these are intended.
Maybe we use different meanings for "exceptions"?
To me they are "watch out", hard to explain, surprising things.
While rebols follows IMHO some simple, but not that obvious rules.
Which i tried to explain.
'x and :x in argument-lists say "dont look further".
That operator-evaluation is not applied then seems reasonable to me.
But yes, these are exceptions.
(actually i never used :x, its like 'x with a little difference (hmm,
= exception ;) i guess?)
> -L
> --
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
>
--
-Volker
Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem.
David
Wheeler
[14/18] from: lmecir:mbox:vol:cz at: 4-Sep-2005 8:15
Volker Nitsch napsal(a):
>On 9/3/05, Ladislav Mecir <[lmecir--mbox--vol--cz]> wrote:
>>
<<quoted lines omitted: 17>>
>Maybe we use different meanings for "exceptions"?
>To me they are "watch out", hard to explain, surprising things.
....
yes, they are surprising, I could have used surprises instead of
exceptions, if you prefer. Another surprise is this:
probe + 1 2 ; == 3
(at least I think, that it looks like a surprise, or exception)
Anyway, the fact is, that such exceptions/surprises are common in
natural languages too, where you can hardly find a rule without exceptions.
-L
[15/18] from: lmecir::mbox::vol::cz at: 4-Sep-2005 9:36
Volker Nitsch napsal(a):
>To me the number of exceptions is quite low:
>Rebol looks at the next two values at the same time.
>If you have a value followed by an operator,
>it is evaluated first. If it is followed by another value+operator,
>these are evaluated left to right.
>Else it uses the next value, as we are used too.
>
The trouble is, that this description is unreadable for beginners and it
cannot be easily amended to reflect all exceptional cases.
[16/18] from: behrangsa:gmai:l at: 5-Sep-2005 10:49
Hi all,
Thanks for all the responses. The main reason I asked this question is
that in the REBOL document it's written that "In REBOL there are no
special operator precedence rules for evaluating blocks" and as said
earlier "The values and words of a block are always evaluated from
first to last..."
But it turns out that this is not true all the time (or am I still
missing something?)
So I thought it would be better to update these portions of the document.
- Behi
On 9/4/05, Ladislav Mecir <[lmecir--mbox--vol--cz]> wrote:
> Volker Nitsch napsal(a):
> >To me the number of exceptions is quite low:
<<quoted lines omitted: 10>>
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
--
Behrang Saeedzadeh
http://www.jroller.com/page/behrangsa
[17/18] from: compkarori::gmail::com at: 5-Sep-2005 20:07
On 9/5/05, Behrang Saeedzadeh <[behrangsa--gmail--com]> wrote:
> Hi all,
>
> Thanks for all the responses. The main reason I asked this question is
> that in the REBOL document it's written that "In REBOL there are no
> special operator precedence rules for evaluating blocks" and as said
> earlier "The values and words of a block are always evaluated from
> first to last..."
Do you have a link to this?
> But it turns out that this is not true all the time (or am I still
> missing something?)
>
Here's a more permanent explanation
http://en.wikibooks.org/wiki/REBOL_Programming/Programming_in_REBOL#Precedence
Well, permanent until someone else changes it :)
--
Graham Chiu
[18/18] from: behrangsa::gmail::com at: 7-Sep-2005 13:26
Hi,
Sorry for the delay.
> Do you have a link to this?
Go to http://www.rebol.com/docs/core23/rebolcore-3.html and search for
precedence
. This is what you'll find:
In REBOL there are no special operator precedence rules for
evaluating blocks. The values and words of a block are always
evaluated from first to last, as shown in the following example [...]
> Here's a more permanent explanation
Thanks for the WIKI link. I'll definitely have a look at it.
Cheers,
Behi
--
Behrang Saeedzadeh
http://www.jroller.com/page/behrangsa
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted