Reverse Polish notation
Reverse Polish notation (RPN) is a mathematical notation in which every operator follows all of its operands, in contrast to Polish notation (PN), which puts the operator in the prefix position. It is also known as postfix notation and is parenthesis-free as long as operator arities are fixed. The description "Polish" refers to the nationality of logician Jan Łukasiewicz, who invented (prefix) Polish notation in the 1920s.
The reverse Polish scheme was proposed in 1954 by Burks, Warren, and Wright and was independently reinvented by F. L. Bauer and E. W. Dijkstra in the early 1960s to reduce computer memory access and utilize the stack to evaluate expressions. The algorithms and notation for this scheme were extended by Australian philosopher and computer scientist Charles Hamblin in the mid-1950s.
During the 1970s and 1980s, RPN was well-known to many calculator users, as Hewlett-Packard used it in their pioneering 9100A and HP-35 scientific calculators, the succeeding Voyager series - and also the "cult" HP-12C financial calculator.
- 1 Explanation
- 2 Practical implications
- 3 Postfix algorithm
- 4 Converting from infix notation
- 5 Implementations
- 6 See also
- 7 References
- 8 External links
In reverse Polish notation the operators follow their operands; for instance, to add 3 and 4, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand; so the expression written "3 − 4 + 5" in conventional notation would be written "3 4 − 5 +" in RPN: 4 is first subtracted from 3, then 5 added to it. An advantage of RPN is that it removes the need for parentheses that are required by infix. While "3 − 4 × 5" can also be written "3 − (4 × 5)", that means something quite different from "(3 − 4) × 5". In postfix, the former could be written "3 4 5 × −", which unambiguously means "3 (4 5 ×) −" which reduces to "3 20 −"; the latter could be written "3 4 − 5 ×" (or 5 3 4 − ×, if keeping similar formatting), which unambiguously means "(3 4 −) 5 ×".
Despite the name, reverse Polish notation is not exactly the reverse of Polish notation, for the operands of non-commutative operations are still written in the conventional order (e.g. "÷ 6 3" in Polish notation and "6 3 ÷" in reverse Polish both evaluate to 2, whereas "3 6 ÷" in reverse Polish notation would evaluate to ½).
In comparison testing of reverse Polish notation with algebraic notation, reverse Polish has been found to lead to faster calculations, for two reasons. Because reverse Polish calculators do not need expressions to be parenthesized, fewer operations need to be entered to perform typical calculations. Additionally, users of reverse Polish calculators made fewer mistakes than for other types of calculator. 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. However, anecdotal evidence suggests that reverse Polish notation is more difficult for users to learn than algebraic notation.
The algorithm for evaluating any postfix expression is fairly straightforward:
- While there are input tokens left
- Read the next token from input.
- If the token is a value
- Push it onto the stack.
- Otherwise, the token is an operator (operator here includes both operators and functions).
- It is known a priori that the operator takes n arguments.
- If there are fewer than n values on the stack
- (Error) The user has not input sufficient values in the expression.
- Else, Pop the top n values from the stack.
- Evaluate the operator, with the values as arguments.
- Push the returned results, if any, back onto the stack.
- If there is only one value in the stack
- That value is the result of the calculation.
- Otherwise, there are more values in the stack
- (Error) The user input has too many values.
The infix expression "5 + ((1 + 2) × 4) − 3" can be written down like this in RPN:
- 5 1 2 + 4 × + 3 −
The expression is evaluated left-to-right, with the inputs interpreted as shown in the following table (the Stack is the list of values the algorithm is "keeping track of" after the Operation given in the middle column has taken place):
|Pop two values (1, 2) and push result (3)|
|Pop two values (3, 4) and push result (12)|
|+||Add||17||Pop two values (5, 12) and push result (17)|
|−||Subtract||14||Pop two values (17, 3) and push result (14)|
When a computation is finished, its result remains as the top (and only) value in the stack; in this case, 14.
As was demonstrated in the Algebraic mode, it is usually easier (fewer keystrokes) in working a problem like this to begin with the arithmetic operations inside the parentheses first.
- 1 2 + 4 × 5 + 3 −
Converting from infix notation
There are other ways of producing postfix expressions from infix notation. 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.
History of implementations
The first computers to implement architectures enabling RPN were the English Electric Company's KDF9 machine, which was announced in 1960 and delivered (i.e. made available commercially) in 1963, and the American Burroughs B5000, announced in 1961 and also delivered in 1963. One of the designers of the B5000, Robert S. Barton, later wrote that he developed RPN independently of Hamblin sometime in 1958 after reading a 1954 textbook on symbolic logic by Irving Copi, where he found a reference to Polish notation, which made him read the works of Jan Łukasiewicz as well, and before he was aware of Hamblin's work. Designed by Robert "Bob" Appleby Ragen, Friden introduced RPN to the desktop calculator market with the EC-130 supporting a four-level stack in June 1963. The successor EC-132 added a square root function in April 1965. Around 1966, the Monroe Epic calculator supported an unnamed input scheme resembling RPN as well.
Hewlett-Packard engineers designed the 9100A Desktop Calculator in 1968 with RPN with only three stack levels, a RPN variant later referred to as three-level RPN. This calculator popularized RPN among the scientific and engineering communities. The HP-35, the world's first handheld scientific calculator, introduced the classical four-level RPN in 1972. HP used RPN 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 LCD-based calculators in the early 1980s such as the HP-10C, HP-11C, HP-15C, HP-16C, and the financial calculator, the HP-12C also used RPN. In 1988 Hewlett-Packard introduced a business calculator, the HP-19B, without RPN, but its 1990 successor, the HP-19BII, gave users the option of using algebraic notation or RPN.
Around 1987, HP introduced RPL, an object-oriented successor to RPN. It deviates from classical RPN by utilizing a stack only limited by the amount of available memory (instead of three or four fixed levels) and which can 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 duplicates values into Y under certain conditions, both part of the specific ruleset of the automatic operational stack in classical RPN in order to ease some calculations and to save keystrokes, but which had shown to also sometimes cause confusion among users not familiar with these properties. 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 RPN. While calculators emulating classical models continue to support classical RPN, new RPN models feature a variant of RPN, where the Enter key behaves as in RPL. This latter variant is sometimes known as entry RPN. In 2013, the HP Prime introduced a 128-level form of entry RPN called Advanced RPN. By early 2016, only the 12C, 12C Platinum, 35s and Prime remain active HP models supporting RPN.
WP 31S and WP 34S
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 RPN with either a four- or an eight-level stack. An eight-level stack was already suggested by John A. Ball in 1978.
Prinz and Prinztronic were own-brand trade names of the British Dixons photographic and electronic goods stores retail chain, which was 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 Programmable Scientific Calculator which featured RPN.
Soviet programmable calculators (MK-52, MK-61, B3-34 and earlier B3-21 models) used RPN for both automatic mode and programming. Modern Russian calculators MK-161 and MK-152, designed and manufactured in Novosibirsk since 2007 and offered by Semico, are backward compatible with them. Their extended architecture is also based on reverse Polish notation.
Existing implementations using reverse Polish notation include:
- Any stack-oriented programming language, such as:
- Hardware calculators:
- Software calculators:
- Mac OS X Calculator
- Several Apple iPhone applications e.g. "reverse polish notation calculator"
- Several Android applications e.g. "RealCalc"
- Unix system calculator program dc
- Emacs lisp library package calc
- Xorg calculator (xcalc)
- grpn scientific/engineering calculator using the GIMP Toolkit (GTK)
- F-Correlatives in MultiValue dictionary items
- RRDTool, a widely used tabulating and graphing software
- grdmath, a program for algebraic operations on NetCDF grids, part of Generic Mapping Tools (GMT) suite
- galculator, a GTK desktop calculator
- Winking Parser (used in Winkin' and Print&Share).
- Łukasiewicz, Jan (1957). Aristotle's Syllogistic from the Standpoint of Modern Formal Logic. Oxford University Press. (Reprinted by Garland Publishing in 1987. ISBN 0-8240-6924-2)
- C. L. Hamblin : Translation to and from Polish notation. Computer Journal, 5: 210-213. 
- Ball, John A. (1978). Algorithms for RPN calculators (1 ed.). Cambridge, Massachusetts, USA: Wiley-Interscience, John Wiley & Sons, Inc. ISBN 0-471-03070-8.
- Burks, A. W.; Warren, D. W.; Wright, J. B. (1954). "An Analysis of a Logical Machine Using Parenthesis-Free Notation". Mathematical Tables and Other Aids to Computation 8 (46): 53. doi:10.2307/2001990. JSTOR 2001990.
- "Charles L. Hamblin and his work" by Peter McBurney
- "Charles L. Hamblin: Computer Pioneer" by Peter McBurney, July 27, 2008. "Hamblin soon became aware of the problems of (a) computing mathematical formulae containing brackets, and (b) the memory overhead in having dealing with memory stores each of which had its own name. One solution to the first problem was Jan Lukasiewicz's Polish notation, which enables a writer of mathematical notation to instruct a reader the order in which to execute the operations (e.g. addition, multiplication, etc) without using brackets. Polish notation achieves this by having an operator (+, ×, etc) precede the operands to which it applies, e.g., +ab, instead of the usual, a+b. Hamblin, with his training in formal logic, knew of Lukasiewicz's work."
- Osborne, Thomas E. (2010) . "Tom Osborne's Story in His Own Words". Steve Leibson. Retrieved 2016-01-01.
I changed the architecture to use RPN (Reverse Polish Notation), which is the ideal notation for programming environment in which coding efficiency is critical. In the beginning, that change was not well received...
- Peterson, Kristina (2011-05-04). "Wall Street's Cult Calculator Turns 30". Wall Street Journal.
- Kasprzyk, D. M.; Drury, C. G.; Bialas, W. F. (1979), "Human behaviour and performance in calculator use with Algebraic and Reverse Polish Notation", Ergonomics 22 (9): 1011, doi:10.1080/00140137908924675.
- Agate, S. J.; Drury, C. G. (1980), "Electronic calculators: which notation is the better?", Applied Ergonomics 11 (1): 2–6, doi:10.1016/0003-6870(80)90114-3, PMID 15676368.
- Hoffman, Errol; Ma, Patrick; See, Jason; Yong, Chee Kee; Brand, Jason; Poulton, Matthew (1994), "Calculator logic: when and why is RPN superior to algebraic?", Applied Ergonomics 25 (5): 327–333, doi:10.1016/0003-6870(94)90048-5.
-  A New Approach to the Design of a Digital Computer (1961)
-  The Burroughs B5000 Conference (1985) p. 49
- "Oral History: Burroughs B5000 Conference", OH 98. Oral history on 6 September 1985, conducted by Bernard A. Galler and Robert F. Rosin, sponsored by AFIPS and Burroughs Corporation, at Marina del Rey, California, archived by the Charles Babbage Institute, University of Minnesota, Minneapolis.
- "1928-2012 Obituary Condolences Robert (Bob) Ragen". Retrieved 2016-01-01.
Bob holds over 80 patents awarded during his work as Director of RD for Friden, and Singer and as Senior Project Engineer at Xerox. He retired from Xerox RD in 1990. He is responsible for the development of the first commercial electronic calculator, the Friden 130, which has been displayed at the Smithsonian.
- Monnier, Richard E. (September 1968). "A New Electronic Calculator with Computerlike Capabilities" (PDF). Hewlett-Packard Journal (Palo Alto, California, USA: Hewlett-Packard) 20 (1): 3–9. Retrieved 2016-01-03.
- Laporte, Jacques (2014-05-22). "The slide rule killer: a milestone in computer history". Archived from the original on 2015-02-20. Retrieved 2016-01-01.
- HP Calculators
- Shirriff, Ken. "Reversing Sinclair's amazing 1974 calculator hack - half the ROM of the HP-35". Retrieved 2013-12-09.
- Sharwood, Simon (2013-09-02). "Google chap reverse engineers Sinclair Scientific Calculator". The Register. Retrieved 2013-12-09.
- Elektronika B3-21 page on RSkey.org
- Elektronika MK-161 page on RSkey.org
- MK-152: Old Russian Motive in a New Space Age.
- Redin, James (2005-02-12) . "RPN or DAL? A brief analysis of Reverse Polish Notation against Direct Algebraic Logic". Retrieved 2015-09-12.
- Hicks, David G. (2013) . "What is RPN?". The Museum of HP Calculators (MoHPC). Retrieved 2015-09-12.
- Klaver, Hans (2014). "RPN Tutorial, incl. some things HP did not tell". Retrieved 2015-09-12.
- Brown, Bob (2015-06-05) . "Postfix Notation Mini-Lecture". Information Technology Department, College of Computing and Software Engineering, Kennesaw State University. Retrieved 2015-09-12.
- Wirth, Niklaus (2005-06-05). "Good Ideas, Through the Looking Glass" (PDF). Zürich. Retrieved 2015-09-12.
- Rosettacode.org providing many implementations in several programming languages.