Menu

Quotations

thebeez

In essence, quotations are not much more than an embedded :NONAME definition, e.g.

defer tellnumber

: say99 :noname 45 44 + . ; is tellnumber ;
: say11 :noname 5 6 + . ; is tellnumber ;

In short, the :NONAME definition leaves an execution token which is assigned immediately to TELLNUMBER. But.. (and here is the catch) Forth normally doesn't allow nested definitions. Just 4tH does..

What the current definition of the ANS-Forth [: ;] does is close to what 4tH does: it creates a jump over the words by AHEAD..THEN. AHEAD is just like IF, just the condition always renders false, so this is identical:

0 IF  ." Never executed" THEN
AHEAD ." Never executed" THEN

4tH doesn't have AHEAD, since there is little use for it. It would be trivial to add though. At the same time [: leaves the dictionary pointer HERE (it's a bit more complex than that, but for understanding the concept it's academical) which serves as an execution token.

So far the technical stuff, but what can you do with it? The most interesting thing would be looping, e.g.

: words 0 ?do dup >r execute r> loop ;
s" printed!" s" are" s" words" s" All" [: type space ;] 4 words

Or branching:

: larger? > if swap then execute drop ;

[: ." The first is larger" ;] [: ." The second is larger" ;] 4 5 larger? cr
[: ." The first is larger" ;] [: ." The second is larger" ;] 5 4 larger? cr

Some people (like the creator of Factor) see significant benefits from this approach. While I do see its uses (since I've already used it - duh) I'm not quite sure there's a general benefit. However, it's not a "dirty" technique, so I see no reason not to support it. That's all..

Since quotations are not bound to any data and not "save state" (that means they always restart from fresh when you invoke them) it's just a bunch of instructions bound to a single execution token. Not a closure or anything fancy.

You may choose to use them - or not. Your choice. The "overhead" is two table entries in the internal symboltable, since its execution semantics are completely identical to :NONAME and ;.