Polish notation

From HandWiki
Short description: Mathematics notation with operators preceding operands

Polish notation (PN), also known as normal Polish notation (NPN),[1] Łukasiewicz notation, Warsaw notation, Polish prefix notation or simply prefix notation, is a mathematical notation in which operators precede their operands, in contrast to the more common infix notation, in which operators are placed between operands, as well as reverse Polish notation (RPN), in which operators follow 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,[2](p24)[3](p78)[4] who invented Polish notation in 1924.[5](p367, Footnote 3)[6](p180, Footnote 3)

The term Polish notation is sometimes taken (as the opposite of infix notation) to also include reverse Polish notation.[7]

When Polish notation is used as a syntax for mathematical expressions by programming language interpreters, it is readily parsed into abstract syntax trees and can, in fact, define a one-to-one representation for the same. Because of this, Lisp (see below) and related programming languages define their entire syntax in prefix notation (and others use postfix notation).

History

A quotation from a paper by Jan Łukasiewicz in 1931[5](p367, Footnote 3)[6](p180, Footnote 3) states how the notation was invented:

I came upon the idea of a parenthesis-free notation in 1924. I used that notation for the first time in my article Łukasiewicz (1), p. 610, footnote.

The reference cited by Łukasiewicz, i.e., Łukasiewicz (1),[8] is apparently a lithographed report in Polish. The referring paper[5] by Łukasiewicz was reviewed by Henry A. Pogorzelski in the Journal of Symbolic Logic in 1965.[9] Heinrich Behmann, editor in 1924 of the article of Moses Schönfinkel,[10] already had the idea of eliminating parentheses in logic formulas. In one of his papers Łukasiewicz stated that his notation is the most compact and the first linearly written parentheses-free notation, but not the first one as Gottlob Frege proposed his parentheses-free Begriffsschrift notation in 1879 already.[11]

Alonzo Church mentions this notation in his classic book on mathematical logic as worthy of remark in notational systems even contrasted to Alfred Whitehead and Bertrand Russell's logical notational exposition and work in Principia Mathematica.[12]

In Łukasiewicz's 1951 book, Aristotle's Syllogistic from the Standpoint of Modern Formal Logic, he mentions that the principle of his notation was to write the functors before the arguments to avoid brackets and that he had employed his notation in his logical papers since 1929.[3](p78) He then goes on to cite, as an example, a 1930 paper he wrote with Alfred Tarski on the sentential calculus.[13]

While no longer used much in logic,[14] Polish notation has since found a place in computer science.

Explanation

The expression for adding the numbers 1 and 2 is written in Polish notation as + 1 2 (prefix), rather than as 1 + 2 (infix). In more complex expressions, the operators still precede their operands, but the operands may themselves be expressions including again operators and their operands. For instance, the expression that would be written in conventional infix notation as

(5 − 6) × 7

can be written in Polish notation as

× (− 5 6) 7

Assuming a given arity of all involved operators (here the "−" denotes the binary operation of subtraction, not the unary function of sign-change), any well-formed prefix representation is unambiguous, and brackets within the prefix expression are unnecessary. As such, the above expression can be further simplified to

× − 5 6 7

The processing of the product is deferred until its two operands are available (i.e., 5 minus 6, and 7). As with any notation, the innermost expressions are evaluated first, but in Polish notation this "innermost-ness" can be conveyed by the sequence of operators and operands rather than by bracketing.

In the conventional infix notation, parentheses are required to override the standard precedence rules, since, referring to the above example, moving them

5 − (6 × 7)

or removing them

5 − 6 × 7

changes the meaning and the result of the expression. This version is written in Polish notation as

− 5 × 6 7.

When dealing with non-commutative operations, like division or subtraction, it is necessary to coordinate the sequential arrangement of the operands with the definition of how the operator takes its arguments, i.e., from left to right. For example, ÷ 10 5, with 10 to the left of 5, has the meaning of 10 ÷ 5 (read as "divide 10 by 5"), or − 7 6, with 7 left to 6, has the meaning of 7 − 6 (read as "subtract from 7 the operand 6").

Evaluation algorithm

Prefix/postfix notation is especially popular for its innate ability to express the intended order of operations without the need for parentheses and other precedence rules, as are usually employed with infix notation. Instead, the notation uniquely indicates which operator to evaluate first. The operators are assumed to have a fixed arity each, and all necessary operands are assumed to be explicitly given. A valid prefix expression always starts with an operator and ends with an operand. Evaluation can either proceed from left to right, or in the opposite direction. Starting at the left, the input string, consisting of tokens denoting operators or operands, is pushed token for token on a stack, until the top entries of the stack contain the number of operands that fits to the top most operator (immediately beneath). This group of tokens at the stacktop (the last stacked operator and the according number of operands) is replaced by the result of executing the operator on these/this operand(s). Then the processing of the input continues in this manner. The rightmost operand in a valid prefix expression thus empties the stack, except for the result of evaluating the whole expression. When starting at the right, the pushing of tokens is performed similarly, just the evaluation is triggered by an operator, finding the appropriate number of operands that fits its arity already at the stacktop. Now the leftmost token of a valid prefix expression must be an operator, fitting to the number of operands in the stack, which again yields the result. As can be seen from the description, a push-down store with no capability of arbitrary stack inspection suffices to implement this parsing.

The above sketched stack manipulation works—with mirrored input—also for expressions in reverse Polish notation.

Polish notation for logic

The table below shows the core of Jan Łukasiewicz's notation in modern logic. Some letters in the Polish notation table stand for particular words in Polish, as shown:

Concept Conventional
notation
Polish
notation
Polish
term
Negation [math]\displaystyle{ \neg\phi }[/math] [math]\displaystyle{ N\phi }[/math][2](pp27–28) negacja
Material conditional [math]\displaystyle{ \phi\to\psi }[/math] [math]\displaystyle{ C\phi\psi }[/math][2](pp28–31) implikacja
Disjunction [math]\displaystyle{ \phi\lor\psi }[/math] [math]\displaystyle{ A\phi\psi }[/math][2](pp34–35) alternatywa
Conjunction [math]\displaystyle{ \phi\land\psi }[/math] [math]\displaystyle{ K\phi\psi }[/math][2](pp35–36) koniunkcja
Non-conjunction [math]\displaystyle{ \phi\mid\psi }[/math] [math]\displaystyle{ D\phi\psi }[/math][2](p36) dysjunkcja
Biconditional [math]\displaystyle{ \phi\leftrightarrow\psi }[/math] [math]\displaystyle{ E\phi\psi }[/math][2](p37) or [math]\displaystyle{ Q\phi\psi }[/math][3](p108) ekwiwalencja
Universal quantifier [math]\displaystyle{ \forall p\,\phi }[/math] [math]\displaystyle{ \varPi p\,\phi }[/math][2](pp154–156) kwantyfikator ogólny
Existential quantifier [math]\displaystyle{ \exists p\,\phi }[/math] [math]\displaystyle{ \varSigma p\,\phi }[/math][2](p157) kwantyfikator szczegółowy
Verum [math]\displaystyle{ \top }[/math] [math]\displaystyle{ V }[/math][15](p275) prawdziwy
Possibility [math]\displaystyle{ \Diamond\phi }[/math] [math]\displaystyle{ M\phi }[/math][16](p52)[3](p134) or [math]\displaystyle{ \varDelta\phi }[/math][17](p111) możliwość
Necessity [math]\displaystyle{ \Box\phi }[/math] [math]\displaystyle{ L\phi }[/math][3](p134) or [math]\displaystyle{ \varGamma\phi }[/math][17](p111) konieczność

Note that the quantifiers ranged over propositional values in Łukasiewicz's work on many-valued logics.

Bocheński introduced a system of Polish notation that names all 16 binary connectives of classical propositional logic.[18](p16) For classical propositional logic, it is a compatible extension of the notation of Łukasiewicz. But the notations are incompatible in the sense that Bocheński uses [math]\displaystyle{ L }[/math] and [math]\displaystyle{ M }[/math] (for nonimplication and converse nonimplication) in propositional logic and Łukasiewicz uses [math]\displaystyle{ L }[/math] and [math]\displaystyle{ M }[/math] in modal logic.

Implementations

Prefix notation has seen wide application in Lisp S-expressions, where the brackets are required since the operators in the language are themselves data (first-class functions). Lisp functions may also be variadic. The Tcl programming language, much like Lisp also uses Polish notation through the mathop library. The Ambi[19] programming language uses Polish notation for arithmetic operations and program construction. LDAP filter syntax uses Polish prefix notation.[20]

Postfix notation is used in many stack-oriented programming languages like PostScript and Forth. CoffeeScript syntax also allows functions to be called using prefix notation, while still supporting the unary postfix syntax common in other languages.

The number of return values of an expression equals the difference between the number of operands in an expression and the total arity of the operators minus the total number of return values of the operators.

Polish notation, usually in postfix form, is the chosen notation of certain calculators, notably from Hewlett-Packard.[21] At a lower level, postfix operators are used by some stack machines such as the Burroughs large systems.

See also


References

  1. (in de) Arithmetische Algorithmen der Mikrorechentechnik (1 ed.). Berlin, Germany: VEB Verlag Technik. 1989. EAN 978-3-34100515-6. MPN 5539165. License 201.370/4/89. ISBN 3-34100515-3. https://books.google.com/books?id=DqYWAQAAMAAJ. Retrieved 2015-12-01. 
  2. 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 (in pl) Elementy logiki matematycznej (1 ed.). Warsaw, Poland: Państwowe Wydawnictwo Naukowe. 1929none ; Elements of Mathematical Logic. New York, USA: The MacMillan Company. 1963. 
  3. 3.0 3.1 3.2 3.3 3.4 Aristotle's Syllogistic from the Standpoint of Modern Formal Logic (2 ed.). Oxford University Press. 1957.  (Reprinted by Garland Publishing in 1987, ISBN 0-8240-6924-2.)
  4. "RPN Perspective". PPC Calculator Journal (Mathematics Department, Santa Monica College, Santa Monica, California, USA) 9 (5): 26–29. August 1982. http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.90.6448&rep=rep1&type=pdf. Retrieved 2022-07-02.  (12 pages)
  5. 5.0 5.1 5.2 "Uwagi o aksjomacie Nicoda i 'dedukcji uogólniającej'" (in pl). Księga pamiątkowa Polskiego Towarzystwa Filozoficznego We Lwowie, 12. II. 1904–1912. II. 1929. Lwów: Wydawnictwo Polskie Towarzystwo Filozoficzne. 1931. pp. 366–383. https://sbc.org.pl/dlibra/docmetadata?showContent=true&id=18864. 
  6. 6.0 6.1 Borkowski, L., ed (1970). "Comments on Nicod's Axiom and on 'Generalizing Deduction'". Selected Works. Amsterdam and London/Warszawa: North-Holland Publishing Company/Polish Scientific Publishers. pp. 179–196. 
  7. Data structures and other objects using Java (3 ed.). Pearson PLC Addison-Wesley. 2006. p. 334. ISBN 978-0-321-37525-4. https://books.google.com/books?id=Tok_AQAAIAAJ. 
  8. "O znaczeniu i potrzebach logiki matematycznej" (in pl). Nauka Polska 10: 604–620. 1929. https://www.sbc.org.pl/dlibra/publication/22955/edition/20205/content?format_id=2. 
  9. "Reviewed work(s): Remarks on Nicod's Axiom and on "Generalizing Deduction" by Jan Łukasiewicz, Jerzy Słupecki, Państwowe Wydawnictwo Naukowe". The Journal of Symbolic Logic (Association for Symbolic Logic) 30 (3): 376–377. September 1965. https://www.jstor.org/stable/2269644.  (NB. The original 1931 paper "Uwagi o aksjomacie Nicoda i 'dedukcji uogólniającej" by Jan Łukasiewicz was re-published at Państwowe Wydawnictwo Naukowe (National Scientific Publishers), Warsaw, Poland in 1961 in a volume edited by Jerzy Słupecki.)
  10. "Über die Bausteine der mathematischen Logik" (in de). Mathematische Annalen 92 (3–4): 305–316. 1924. doi:10.1007/BF01448013none ; van Heijenoort, Jean, ed (1967). "On the building blocks of mathematical logic". A Source Book in Mathematical Logic, 1879–1931. Harvard University Press. pp. 355–366. 
  11. (in de) Logische Notationen und deren Verarbeitung auf elektronischen Rechenanlagen aus theoretischer, praktischer und historischer Sicht. (Diploma thesis). Vienna, Austria. 2005. "Die ältesten Texte in den 'Selected Works', in denen Łukasiewicz polnische Notation verwendet, datieren relativ spät, sind aber Präsentationen vorangehender Arbeiten, die 'in the course of the years 1920–1930' (S. 131) stattgefunden haben, also auch keine genauere Zeitangabe geben." 
  12. Introduction to Mathematical Logic. Princeton, New Jersey, USA: Princeton University Press. 1944. p. 38. "[…] Worthy of remark is the parenthesis-free notation of Jan Łukasiewicz. In this the letters N, A, C, E, K are used in the roles of negation, disjunction, implication, equivalence, conjunction respectively. […]" 
  13. "Untersuchungen über den Aussagenkalküls" (in de). Comptes Rendus des Séances de la Société des Sciences et des Lettres de Varsovie 23 (Cl. III): 30–50. 1930. https://rcin.org.pl/impan/dlibra/publication/4839/edition/50601/content. 
  14. "Mhy bib I fail logic? Dyslexia in the teaching of logic", Tools for Teaching Logic: Third International Congress, TICTTL 2011, Salamanca, Spain, 1–4 June 2011, Proceedings, Lecture Notes in Artificial Intelligence, 6680, Springer Nature, 2011-06-01, pp. 162–169, doi:10.1007/978-3-642-21350-2_19, ISBN 978-3-64221349-6, https://books.google.com/books?id=be-pTR5TmZIC&pg=PA166, "[…] Polish or prefix notation has come to disuse given the difficulty that using it implies. […]" 
  15. "Der Äquivalenzenkalkül" (in de). Collectanea Logica 1: 145–169. 1939. 
  16. "Untersuchungen über den Aussagenkalküls" (in de). Comptes Rendus des Séances de la Société des Sciences et des Lettres de Varsovie 23 (Cl. III): 51–77. 1930. https://rcin.org.pl/impan/dlibra/publication/4839/edition/50601/content. 
  17. 17.0 17.1 "A System of Modal Logic". The Journal of Computing Systems 3 (1): 111–149. 1953. 
  18. (in fr) Précis de logique mathématique. Collection Synthese. 2. Bussum, Pays-Bas, Netherlands: F. G. Kroonder. 1949. https://burjcdigital.urjc.es/bitstream/handle/10115/1425/PRECIS_DE_LOGIQUE_MATHEMATIQUE.pdf?sequence=1&isAllowed=y. Retrieved 2023-11-12.  Translated as A Precis of Mathematical Logic. Dordrecht, Netherlands: D. Reidel Publishing Company. 1959. 
  19. "Google Code Archive - Long-term storage for Google Code Project Hosting". https://code.google.com/p/ambi/. 
  20. "LDAP Filter Syntax". http://www.ldapexplorer.com/en/manual/109010000-ldap-filter-syntax.htm. 
  21. "HP calculators - HP 35s RPN Mode". Hewlett-Packard. http://h20331.www2.hp.com/Hpsub/downloads/35_02_RPN_Mode.pdf. 

Further reading

External links