Proposal: Standardize the well-known -ROT
This page is dedicated to discussing this specific proposal
ContributeContributions
EricBlake
[416] Standardize the well-known -ROTProposal2025-10-10 14:18:47
Author:
Eric Blake
Change Log:
2025-10-10 Initial proposal
Problem:
When doing stack-heavy manipulation (without locals), it is not uncommon to want to grab a single cell located underneath a cell pair, modify it, and then pull the cell pair back to the top of the stack. Equivalently, this can be viewed as pushing the single cell back under the cell pair, or undoing the effects of rot
.
Several proposed example implementations have relied on use of the word -rot
, because it is well-known across a number of Forth implementations, only to need correction when it is pointed out that the standard does not yet provide a single-word solution for this desired stack effect.
Solution:
Standardizing -rot
will guarantee that library code can use this word if present for "undoing" a rot
without worrying about it having different semantics than what most users have come to expect.
A pure-Forth implementation using other standard words is fairly trivial; therefore the new word can be optional. Note, however, that a native implementation of rot
typically needs just 4 register moves (including a temporary); implementing -rot
as rot rot
would naively require 8 register moves, while a native implementation can easily achieve the same 4-move performance of rot
.
The suggested pronunciation "m-rote" matches the package name of the public domain implementation given in theforth.net, where "m" is short for "minus"; however, gforth suggests the pronunciation "not_rote". Keep in mind that the standard already pronounces <>
as "not-equals" and 0<>
as "zero-not-equals", so I'm reluctant to use the pronunciation "not-" on a word that is spelled with -
rather than <>
.
Typical use:
The standard itself has already suggested using -rot
in NR>
.
Other comments on the standard (not yet at the level of formal proposals) have mentioned -rot
:
Potential future addition of ?:
that can define -rot
when it is not present
Potential future addition of 2roll
that can implement -rot
, 2swap
, and 2rot
.
Proposal:
Add a new glossary entry:
6.1.____ -ROT "m-rote" CORE-EXT
( x1 x2 x3 -- x3 x1 x2 )
Perform a reverse rotation of the top three stack entries.
Rationale: This operation undoes the effects of `rot`.
Reference implementation:
: -rot ( x1 x2 x3 -- x3 x1 x2 ) rot rot ;
Testing:
t{ 1 2 3 -rot -> 3 1 2 }t
(Editorial note: Other than in the subject, this proposal uses lower-case naming of all words, on the grounds that other proposals in flight deal with either making future Forth case-insensitive on standard words, or with ensuring the standard words are converted to upper-case in renderings as needed.)