Reverse Polish notation

Mathematical notation in which every operator follows all of its operands

Reverse Polish notation (RPN), also known as reverse Łukasiewicz notation, Polish postfix notation or simply postfix notation, is a mathematical notation in which operators follow their operands, in contrast to Polish notation (PN), in which operators precede their operands. It does not need any parentheses as long as each operator has a fixed number of operands. The description "Polish" refers to the nationality of logician Jan Łukasiewicz,[1] who invented Polish notation in 1924.[2][3]

The reverse Polish scheme was proposed in 1954 by Arthur Burks, Don Warren, and Jesse Wright[4] and was independently reinvented by Friedrich L. Bauer and Edsger W. Dijkstra in the early 1960s to reduce computer memory access and use the stack to evaluate expressions. The algorithms and notation for this scheme were extended by the Australian philosopher and computer scientist Charles L. Hamblin in the mid-1950s.[5][6][7][8][9][10]

During the 1970s and 1980s, Hewlett-Packard used RPN in all of their desktop and hand-held calculators, and has continued to use it in some models into the 2020s.[11][12] In computer science, reverse Polish notation is used in stack-oriented programming languages such as Forth, STOIC, PostScript, RPL and Joy.

In reverse Polish notation, the operators follow their operands; for instance, to add 3 and 4 together, one would write 3 4 + rather than 3 + 4. If there are multiple operations, operators are given immediately after their final operands (often an operator takes two operands, in which case the operator is written after the second operand); so the expression written 3 − 4 + 5 in conventional notation would be written 3 4 − 5 + in reverse Polish notation: 4 is first subtracted from 3, then 5 is added to it. An advantage of reverse Polish notation is that it removes the need for parentheses that are required by infix notation. While 3 − 4 × 5 can also be written 3 − (4 × 5), that means something quite different from (3 − 4) × 5. In reverse Polish notation, the former could be written 3 4 5 × −, which unambiguously means 3 (4 5 ×) − which reduces to 3 20 − (which can further be reduced to -17); the latter could be written 3 4 − 5 × (or 5 3 4 − ×, if keeping similar formatting), which unambiguously means (3 4 −) 5 ×.

In comparison, testing of reverse Polish notation with algebraic notation, reverse Polish has been found to lead to faster calculations, for two reasons. The first reason is that reverse Polish calculators do not need expressions to be parenthesized, so fewer operations need to be entered to perform typical calculations. Additionally, users of reverse Polish calculators made fewer mistakes than for other types of calculators.[13][14] Later research clarified that the increased speed from reverse Polish notation may be attributed to the smaller number of keystrokes needed to enter this notation, rather than to a smaller cognitive load on its users.[15] However, anecdotal evidence suggests that reverse Polish notation is more difficult for users to learn than algebraic notation.[14]

Edsger W. Dijkstra invented the shunting-yard algorithm to convert infix expressions to postfix expressions (reverse Polish notation), so named because its operation resembles that of a railroad shunting yard.

There are other ways of producing postfix expressions from infix expressions. Most operator-precedence parsers can be modified to produce postfix expressions; in particular, once an abstract syntax tree has been constructed, the corresponding postfix expression is given by a simple post-order traversal of that tree.

The first computers to implement architectures enabling reverse Polish notation were the English Electric Company's KDF9 machine, which was announced in 1960 and commercially available in 1963,[16] and the Burroughs B5000, announced in 1961 and also delivered in 1963:

Presumably, the KDF9 designers drew ideas from Hamblin's GEORGE (General Order Generator),[5][6][8] an autocode programming system written for a DEUCE computer installed at the University of Sydney, Australia, in 1957.[5][6][8][16]

One of the designers of the B5000, Robert S. Barton, later wrote that he developed reverse Polish notation independently of Hamblin sometime in 1958 after reading a 1954 textbook on symbolic logic by Irving Copi,[17][18][19] where he found a reference to Polish notation,[19] which made him read the works of Jan Łukasiewicz as well,[19] and before he was aware of Hamblin's work.

Friden introduced reverse Polish notation to the desktop calculator market with the EC-130, designed by Robert "Bob" Appleby Ragen,[20] supporting a four-level stack[3] in June 1963.[21] The successor EC-132 added a square root function in April 1965.[22] Around 1966, the Monroe Epic calculator supported an unnamed input scheme resembling RPN as well.[3]

A promotional Hewlett-Packard "No Equals" hat from the 1980s – both a boast and a reference to RPN

Hewlett-Packard engineers designed the 9100A Desktop Calculator in 1968 with reverse Polish notation[11] with only three stack levels with working registers X ("keyboard"), Y ("accumulate") and visible storage register Z ("temporary"),[23][24] a reverse Polish notation variant later referred to as three-level RPN. This calculator popularized reverse Polish notation among the scientific and engineering communities. The HP-35, the world's first handheld scientific calculator,[11] introduced the classical four-level RPN with its specific ruleset of the so-called operational (memory) stack[25] (later also called automatic memory stack[26][27]) in 1972.[28] In this scheme, the ↵ Enter key duplicates values into Y under certain conditions, and the top register gets duplicated on drops in order to ease some calculations and to save keystrokes.[27] HP used reverse Polish notation on every handheld calculator it sold, whether scientific, financial, or programmable, until it introduced the HP-10 adding machine calculator in 1977. By this time, HP was the leading manufacturer of calculators for professionals, including engineers and accountants.

Later calculators with LCD displays in the early 1980s, such as the HP-10C, HP-11C, HP-15C, HP-16C, and the financial HP-12C calculator also used reverse Polish notation. In 1988, Hewlett-Packard introduced a business calculator, the HP-19B, without reverse Polish notation, but its 1990 successor, the HP-19BII, gave users the option of using algebraic or reverse Polish notation again.

Around 1987, HP introduced RPL, an object-oriented successor to reverse Polish notation. It deviates from classical reverse Polish notation by using a stack only limited by the amount of available memory (instead of three or four fixed levels) and which could hold all kinds of data objects (including symbols, strings, lists, matrices, graphics, programs, etc.) instead of just numbers. It also changed the behaviour of the stack to no longer duplicate the top register on drops (since in an unlimited stack there is no longer a top register) and the behaviour of the ↵ Enter key so that it no longer duplicated values into Y, which had shown to sometimes cause confusion among users not familiar with the specific properties of the automatic memory stack. From 1990 to 2003, HP manufactured the HP-48 series of graphing RPL calculators, and in 2006 introduced the HP 50g.

As of 2011, Hewlett-Packard was offering the calculator models 12C, 12C Platinum, 17bII+, 20b, 30b, 33s, 35s, 48gII (RPL) and 50g (RPL) which support reverse Polish notation.[29] While calculators emulating classical models continue to support classical reverse Polish notation, new reverse Polish notation models feature a variant of reverse Polish notation, where the ↵ Enter key behaves as in RPL. This latter variant is sometimes known as entry RPN.[30] In 2013, the HP Prime introduced a 128-level form of entry RPN called advanced RPN. By late 2017, only the 12C, 12C Platinum, 17bii+, 35s and Prime remain active HP models supporting reverse Polish notation.

The community-developed calculators WP 31S and WP 34S, which are based on the HP 20b/HP 30b hardware platform, support Hewlett-Packard-style classical reverse Polish notation with either a four- or an eight-level stack. A seven-level stack had been implemented in the MITS 7400C scientific desktop calculator in 1972[31][32][33] and an eight-level stack was already suggested by John A. Ball in 1978.[3]

In Britain, Clive Sinclair's Sinclair Scientific and Scientific Programmable models used reverse Polish notation.[34][35]

In 1974 Commodore produced the Minuteman *6 (MM6) without ↵ Enter key and the Minuteman *6X (MM6X) with ↵ Enter key, both implementing a form of two-level RPN. The SR4921 RPN came with a variant of four-level RPN with stack levels named X, Y, Z, and W (rather than T). In contrast to Hewlett-Packard's reverse Polish notation implementation, W filled with 0 instead of its contents being duplicated on stack drops.[36]

Prinz and Prinztronic were own-brand trade names of the British Dixons photographic and electronic goods stores retail chain, later rebranded as Currys Digital stores, and became part of DSG International. A variety of calculator models was sold in the 1970s under the Prinztronic brand, all made for them by other companies.

Among these was the PROGRAM[37] Programmable Scientific Calculator which featured reverse Polish notation.

The Aircraft Navigation Computer Heathkit OC-1401/OCW-1401 used five-level RPN in 1978.

Soviet programmable calculators (MK-52, MK-61, B3-34 and earlier B3-21[38] models) used reverse Polish notation for both automatic mode and programming. Modern Russian calculators MK-161[39] and MK-152,[40] designed and manufactured in Novosibirsk since 2007 and offered by Semico,[41] are backwards compatible with them. Their extended architecture is also based on reverse Polish notation.