About POSTPONE semantics in edge cases #103
Replies: 2 comments 2 replies
-
I think there is a false premise in one of your first sentences, when you say:
But, this is incorrect: If no effect is detected, it does not imply that the behavior is "to do nothing". The opposite implication is obviously true, though. We all agree that the word Making In the example
the word In your example, giving at prompt
when Regards. |
Beta Was this translation helpful? Give feedback.
-
The reason for the different understanding of how
The reader could think that this definition for the term The term
I have prepared a proposal to make the definition for the term immediate word more clear. |
Beta Was this translation helpful? Give feedback.
-
Preceding part: How
postpone
should work.Introduction
In the section "2.1 Definitions of terms" of the standard (both Forth-94 and Forth-2012 versions) we can find the following definition for the compilation semantics term:
An obvious premise is that any behavior can be observed by some detectable side effects (including changes in the stacks). If, in some conditions, no effects are detected, then the behavior under those conditions is to do nothing (take no actions).
Since a definition of a term implies equivalence of the left and right parts (the definiendum and the definiens), we can state:
Exploration #
Now we can explore one interesting thing.
Let's consider a word
x]
that is defined as follows:The word "
]
" (right-bracket) is an ordinary word that enters compilation state.So what are the compilation semantics for
x]
?This is probably quite unexpected, but no side effect can be detected by a standard program when
x]
is encountered (and processed) by the Forth text interpreter in compilation state. Hence, the compilation semantics forx]
are to do nothing!Now let's consider the ordinary word
y]
, which is defined as follows:The compilation semantics of
postpone
are to parse a lexeme (that is a word name), find the corresponding word, and append the compilation semantics of the found word to the current definition.Hence, the phrase
postpone x]
above during compilation appends the compilation semantics ofx]
to the execution semantics of the wordy]
. Since the compilation semantics forx]
are to do nothing, semantically nothing is appended toy]
.So, what are the execution semantics for
y]
? They are to do nothing! And hence, the interpretation semantics fory]
are to do nothing too.Excusing #
Now let's test[1] your Forth system:
The last line should print
123
. But on most systems it prints nothing or throws an exception. Do they have an excuse for that? The only possible excuse is the official TC answer to the RFI Q99-027 (raw copy). This answer means that a standard program is not allowed to perform compilation semantics in interpretation state.So, according to A99-027, an ambiguous condition exists when
y]
is performed in interpretation state. And then, a Forth system is allowed to do anything in this case.But if your standard Forth system doesn't rely on A99-027, then this test should print
123
according to the normative text of the standard.[1] This test (in a slightly different form) was first published in comp.lang.forth by Anton Ertl. He and me both supposed that
y]
should enter compilation state. But later I realized that this was incorrect.Solution #
A standard compliant polyfill for
postpone
can be defined viafind-name
andname>compile
, or via a full-fledgedfind
as follows:This implementation takes into account that the behavior of the phrase
name>compile ( x xt-compiler ) execute
may be STATE-dependent (since it can be STATE-dependent for some user-defined words), thereforeexecute-compiling
is used in this case too.This implementation also defines the interpretation semantics for
postpone
, that are to just perform the compilation semantics of the parsed argument.See also an implementation for
postpone
that doesn't rely on the previouspostpone
in my gist.NB: such implementation for
postpone
will correctly work for any immediate state-dependent word, including definitions (or redefinitions) for standard dual-semantics words. But some non standard programs can work incorrectly, if they rely on A99-027 and on some particular behavior in the case of a corresponding ambiguous condition.Questions or objections? :)
Beta Was this translation helpful? Give feedback.
All reactions