REBOL path operator vs division ambiguity

209 Views Asked by At

I've started looking into REBOL, just for fun, and as a fan of programming languages, I really like seeing new ideas and even just alternative syntaxes. REBOL is definitely full of these. One thing I noticed is the use of '/' as the path operator which can be used similarly to the '.' operator in most object-oriented programming languages. I have not programmed in REBOL extensively, just looked at some examples and read some documentation, but it isn't clear to me why there's no ambiguity with the '/' operator.

x: 4
y: 2
result: x/y

In my example, this should be division, but it seems like it could just as easily be the path operator if x were an object or function refinement. How does REBOL handle the ambiguity? Is it just a matter of an overloaded operator and the type system so it doesn't know until runtime? Or is it something I'm missing in the grammar and there really is a difference?

UPDATE Found a good piece of example code:

sp: to-integer (100 * 2 * length? buf) / d/3 / 1024 / 1024

It appears that arithmetic division requires whitespace, while the path operator requires no whitespace. Is that it?

4

There are 4 best solutions below

3
On BEST ANSWER

The code guide says:

White-space is used in general for delimiting (for separating symbols).

This is especially important because words may contain characters such as + and -.

http://www.rebol.com/r3/docs/guide/code-syntax.html

One acquired skill of being a REBOler is to get the hang of inserting whitespace in expressions where other languages usually do not require it :)

0
On

This question deserves an answer from the syntactic point of view. In Rebol, there is no "path operator", in fact. The x/y is a syntactic element called path. As opposed to that the standalone / (delimited by spaces) is not a path, it is a word (which is usually interpreted as the division operator). In Rebol you can examine syntactic elements like this:

length? code: [x/y x / y] ; == 4
type? first code ; == path!
type? second code

, etc.

0
On

Spaces are generally needed in Rebol, but there are exceptions here and there for "special" characters, such as those delimiting series. For instance:

  • [a b c] is the same as [ a b c ]
  • (a b c) is the same as ( a b c )
  • [a b c]def is the same as [a b c] def

Some fairly powerful tools for doing introspection of syntactic elements are type?, quote, and probe. The quote operator prevents the interpreter from giving behavior to things. So if you tried something like:

>> data: [x [y 10]]
>> type? data/x/y
>> probe data/x/y

The "live" nature of the code would dig through the path and give you an integer! of value 10. But if you use quote:

>> data: [x [y 10]]
>> type? quote data/x/y
>> probe quote data/x/y

Then you wind up with a path! whose value is simply data/x/y, it never gets evaluated.

In the internal representation, a PATH! is quite similar to a BLOCK! or a PAREN!. It just has this special distinctive lexical type, which allows it to be treated differently. Although you've noticed that it can behave like a "dot" by picking members out of an object or series, that is only how it is used by the DO dialect. You could invent your own ideas, let's say you make the "russell" command:

russell [
    x: 10
    y: 20
    z: 30
    x/y/z
    (
        print x
        print y
        print z
    )
]

Imagine that in my fanciful example, this outputs 30, 10, 20...because what the russell function does is evaluate its block in such a way that a path is treated as an instruction to shift values. So x/y/z means x=>y, y=>z, and z=>x. Then any code in parentheses is run in the DO dialect. Assignments are treated normally.

When you want to make up a fun new riff on how to express yourself, Rebol takes care of a lot of the grunt work. So for example the parentheses are guaranteed to have matched up to get a paren!. You don't have to go looking for all that yourself, you just build your dialect up from the building blocks of all those different types...and hook into existing behaviors (such as the DO dialect for basics like math and general computation, and the mind-bending PARSE dialect for some rather amazing pattern matching muscle).

But speaking of "all those different types", there's yet another weirdo situation for slash that can create another type:

>> type? quote /foo

This is called a refinement!, and happens when you start a lexical element with a slash. You'll see it used in the DO dialect to call out optional parameter sets to a function. But once again, it's just another symbolic LEGO in the parts box. You can ascribe meaning to it in your own dialects that is completely different...

0
On

While I didn't find any written definitive clarification, I did also find that +,-,* and others are valid characters in a word, so clearly it requires a space.

x*y

Is a valid identifier

x * y

Performs multiplication. It looks like the path operator is just another case of this.