Polish notation
Polish notation, also known as prefix notation or Łukasiewicz notation, is a form of mathematical and logical expression where operators precede their operands, enabling unambiguous parsing without the need for parentheses or operator precedence rules.[1][2] It was invented in the 1920s by the Polish logician and mathematician Jan Łukasiewicz to facilitate the analysis of propositional logic by eliminating syntactic ambiguities inherent in infix notation.[3][2]
Łukasiewicz, a pioneer in mathematical logic and the founder of the Warsaw School of Logic, developed this notation during his efforts to formalize Aristotelian syllogisms and multi-valued logics.[3] The system was first systematically presented in his 1929 book Elementy logiki matematycznej (translated as Elements of Mathematical Logic in 1963), where it served as a tool for representing logical connectives and truth functions without brackets.[3][2] Inspired by the function-argument structure in mathematics, the notation uses uppercase letters for operators (e.g., C for implication, N for negation, K for conjunction, A for disjunction) and lowercase letters for atomic propositions or variables.[3]
A key advantage of Polish notation is its fixed prefix structure, which allows expressions to be validated through a simple counting procedure: begin with a count of 1 for the main operator, increment by 1 for each additional argument of multi-place operators, and decrement by 1 for each operand, ensuring the count reaches zero at the end for well-formed formulas.[2] For instance, the infix expression p → (q ∧ r) translates to C p K q r in Polish notation, reducing the symbol count and removing all parentheses.[3]
Infix: p → (q ∧ r)
Polish: C p K q r
Infix: p → (q ∧ r)
Polish: C p K q r
This compactness proved especially useful in early logical mechanization and influenced the creation of reverse Polish notation (postfix), which powers stack-based computation in programming languages, Forth, and many calculators.[4][3] While primarily applied in logic and theoretical computer science, Polish notation's parenthesis-free design continues to aid in algorithm design, expression evaluation, and automated theorem proving.[2]
Historical Development
Origins in Logic
Polish notation, also known as prefix notation, originated in the early 20th-century work of Polish logician Jan Łukasiewicz as a means to express logical propositions unambiguously without parentheses. Developed during the 1920s at the Lwów–Warsaw School of Logic—a prominent center for mathematical logic in Poland influenced by Kazimierz Twardowski—Łukasiewicz's innovation addressed the ambiguities inherent in traditional infix notations for logical connectives. This school emphasized rigorous formalization of logic, drawing inspiration from Aristotle's syllogistic reasoning and the need to systematize deductive processes in propositional logic.[5][6]
Łukasiewicz first introduced the notation in his 1924 paper on the concept of implication, where he placed operators before their arguments to eliminate precedence issues and structural ambiguity. For instance, in infix form, expressions like negation (~P), conjunction (· P Q), or disjunction (v P Q) require parentheses for complex combinations to avoid misinterpretation, such as in (~P · Q) versus ~(P · Q). In Polish notation, these become prefix forms like Np for negation of P, K P Q for conjunction of P and Q, and A P Q for disjunction, allowing straightforward reading from left to right without additional symbols. This prefix placement ensured that every operator's scope was immediately clear, facilitating the formalization of deduction in a compact manner.[7][8]
In the 1930s, Łukasiewicz extended the notation to handle multi-argument functions, further refining its applicability in propositional calculus and many-valued logics. These developments, building on his earlier work, supported the Lwów–Warsaw School's broader efforts to axiomatize logical systems and explore non-classical logics, such as three-valued logic for future contingents. The notation's design not only streamlined manual logical analysis but also laid foundational principles for unambiguous expression in formal reasoning.[5]
Adoption in Computing
In the mid-20th century, Polish notation transitioned from its origins in mathematical logic to practical applications in computer science, particularly as a means to simplify expression parsing and evaluation in resource-constrained early computers. Researchers recognized that the prefix structure eliminated the need for parentheses and operator precedence rules, enabling unambiguous, linear processing of symbolic expressions without complex decoding logic. This made it appealing for both software interpreters and potential hardware designs during the 1940s and 1950s, when computing resources were limited and efficient parsing was critical for performance.[9]
A seminal contribution came from Arthur W. Burks, Don W. Warren, and Jesse B. Wright in 1954, who analyzed a logical machine design incorporating parenthesis-free notation—equivalent to Polish prefix notation—to facilitate direct evaluation of logical formulas. Their work demonstrated how prefix notation allowed the machine to process expressions sequentially without recursive descent or auxiliary storage for grouping, reducing the complexity of the control unit and enabling stack-free evaluation through a simple scanning mechanism that applied operators to preceding operands. This exploration highlighted prefix notation's potential for hardware implementations, influencing discussions on instruction formats that minimized decoding overhead in prototype computers of the era.[10]
The notation's influence extended to artificial intelligence and symbolic computation in the 1950s and 1960s, where it supported list-processing languages for manipulating complex expressions. John McCarthy, in developing Lisp starting in 1958, adopted a parenthesized variant of prefix notation—termed "Cambridge Polish"—to represent both programs and data uniformly as symbolic lists. This choice facilitated recursive evaluation and subexpression analysis, essential for AI tasks like theorem proving, by allowing the interpreter to immediately identify the primary operator and recurse on operands. Lisp's 1960 implementation as a prefix-based system marked a key milestone, embedding Polish notation in one of the earliest high-level languages for symbolic computation and inspiring subsequent AI research.[11][9]
Fundamentals
Definition and Principles
Polish notation, also known as prefix notation or Łukasiewicz notation, is a system for expressing mathematical and logical expressions in which operators precede their operands, thereby removing the necessity for parentheses and explicit operator precedence rules. This notation ensures that every expression is unambiguous and can be parsed linearly from left to right, with the structure directly mirroring the hierarchical organization of operations.[7]
The syntax of Polish notation defines expressions recursively: an atomic expression consists of a single variable or constant, while a compound expression begins with an operator symbol followed by a number of subexpressions equal to the operator's arity. For instance, a unary operator requires one subexpression, a binary operator requires two, and an n-ary operator requires n subexpressions, each of which must itself be a complete expression. This arity specification—unary for operations like negation, binary for addition or conjunction, and potentially higher for functions like summation—guarantees unique decomposition without ambiguity.[7]
Semantically, Polish notation represents expressions as full binary trees (or more generally, ordered trees), where the root of each subtree is the operator and its children are the operands, providing a clear, parenthesization-free equivalent to infix notations. For example, the arithmetic expression (x + y) \times z, which equals x \times z + y \times z under standard precedence, is written as \times + x y z in Polish notation. Similarly, in logic, the conjunction of propositions p and the negation of q is denoted K p N q, where K is the binary conjunction operator and N is the unary negation. These properties make Polish notation particularly suited for formal systems requiring precise, machine-readable representations.[7]
Comparison to Other Notations
Polish notation, also known as prefix notation, places the operator before its operands, contrasting with infix notation where the operator is positioned between the operands, as in the expression 3 + 4 * 5, which requires parentheses and operator precedence rules to resolve ambiguity.[12] In Polish notation, the equivalent expression is written as + 3 * 4 5, eliminating the need for such rules and parentheses.[13]
Postfix notation, or reverse Polish notation (RPN), positions the operator after its operands, as in 3 4 5 * +, and was proposed in 1954 by Arthur Burks, Don Warren, and Jesse Wright for use in early computer designs.[14] RPN facilitates straightforward stack-based evaluation by scanning from left to right but requires right-to-left reading for intuitive comprehension of expression hierarchy.[12]
Key differences among these notations lie in their traversal order and parsing suitability: Polish notation supports left-to-right prefix evaluation, making it amenable to recursive descent parsing in top-down language implementations; infix notation prioritizes human readability despite its inherent ambiguity without precedence conventions; and postfix notation excels in stack-oriented processing but lacks hierarchical readability when scanned sequentially.[15][16]
Conversions between these notations can be achieved through tree traversal methods on the expression's abstract syntax tree: preorder traversal yields Polish notation, inorder traversal produces infix (with adjustments for parentheses), and postorder traversal generates postfix.[16]
The trade-offs of Polish notation include reduced parsing complexity in recursive programming languages due to its unambiguous, parenthesis-free structure, though it is less intuitive for human users compared to the familiar infix form.[7][12]
Evaluation Methods
Core Algorithm
The core algorithm for evaluating Polish notation expressions employs a recursive procedure that processes the expression from left to right, exploiting its unambiguous prefix structure to avoid issues with operator precedence or parentheses. Upon encountering an atom (such as a numeric constant or variable), the algorithm simply returns its value. When an operator is encountered as the first element of a subexpression, the algorithm determines the operator's arity (number of required operands), recursively evaluates the immediately following subexpressions to obtain those operands, and then applies the operator to the results, yielding the subexpression's value. This method aligns with the evaluation semantics introduced in early computing systems for prefix forms, where expressions are treated as nested function applications.[17]
The following pseudocode illustrates the standard recursive evaluation function, assuming the expression is represented as a list or array and that operator arities are predefined:
[function](/page/Function) evaluate([expr](/page/Expr), pos):
[token](/page/Token) = [expr](/page/Expr)[pos]
if is_atom([token](/page/Token)):
[return](/page/Return) get_value([token](/page/Token)), pos + 1
else:
[operator](/page/Operator) = [token](/page/Token)
[arity](/page/Arity) = get_arity([operator](/page/Operator))
operands = []
current_pos = pos + 1
for i from 1 to [arity](/page/Arity):
operand_value, current_pos = evaluate([expr](/page/Expr), current_pos)
operands.[append](/page/Append)(operand_value)
result = [apply](/page/Apply)([operator](/page/Operator), operands)
[return](/page/Return) result, current_pos
[function](/page/Function) evaluate([expr](/page/Expr), pos):
[token](/page/Token) = [expr](/page/Expr)[pos]
if is_atom([token](/page/Token)):
[return](/page/Return) get_value([token](/page/Token)), pos + 1
else:
[operator](/page/Operator) = [token](/page/Token)
[arity](/page/Arity) = get_arity([operator](/page/Operator))
operands = []
current_pos = pos + 1
for i from 1 to [arity](/page/Arity):
operand_value, current_pos = evaluate([expr](/page/Expr), current_pos)
operands.[append](/page/Append)(operand_value)
result = [apply](/page/Apply)([operator](/page/Operator), operands)
[return](/page/Return) result, current_pos
To evaluate a complete expression expr, invoke evaluate(expr, 0) and verify that the returned position equals the expression length; the first return value is the overall result. This formulation mirrors the recursive eval function in symbolic expression processing systems, where special handling for atoms and recursive calls on argument lists ensures correct computation.[17]
The time complexity of this algorithm is O(n), where n is the number of tokens in the expression, as the recursion traverses each token exactly once in a single pass, with constant-time operations per token assuming fixed-arity operators.[18] Error handling is inherent in the recursion: arity mismatches are detected by insufficient subexpressions (failing to gather the required operands), while incomplete expressions are flagged if the final position does not reach the end of the input; such checks can raise exceptions or return error indicators during the recursive calls.[17]
This pure recursive approach assumes prior knowledge of the prefix structure, where operators precede all operands without ambiguity, and requires no explicit stack for evaluation—though an iterative stack-based variant exists for tail-recursion optimization or memory efficiency in deep expressions.[17]
Worked Examples
To illustrate the evaluation of Polish notation, consider the arithmetic expression + \, 3 \, * \, 4 \, 5, which corresponds to the infix form $3 + (4 \times 5). The evaluation proceeds recursively from left to right, scanning the input sequence. The leading operator + indicates a binary operation requiring two operands. The first operand is the atom 3. The second operand is the subexpression * \, 4 \, 5, which itself begins with the binary operator *; its operands are the atoms 4 and 5, yielding $4 \times 5 = 20. Applying the outer operator gives $3 + 20 = 23. This recursive process can be visualized as a tree: the root node is + with left child 3 and right child a subtree rooted at * with children 4 and 5; evaluation traverses the tree depth-first, computing leaves first and propagating results upward.
A more complex arithmetic example is / \, + \, * \, 2 \, 3 \, 4 \, 5, equivalent to infix ( (2 \times 3) + 4 ) / 5. Scanning left to right, the leading / requires two operands. The first is the subexpression + \, * \, 2 \, 3 \, 4, starting with binary +; its first operand is * \, 2 \, 3 (binary * with atoms 2 and 3, yielding 6), and its second is atom 4, so $6 + 4 = 10. The second operand for / is atom 5. Thus, $10 / 5 = 2. The recursion depth here reaches three levels: innermost *, then +, then /; at each level, arity is resolved by consuming exactly two subexpressions or atoms per binary operator, assembling the result only after full operand evaluation.
For a simple logical example, evaluate v \, P \, \cdot \, Q \, ( ~ R ), where v denotes disjunction (\lor), \cdot conjunction (\land), and ~ negation (\lnot); assume propositional atoms P, Q, R with truth values (true, true, false) for concreteness. The leading binary v requires two operands. The first is atom P (true). The second is \cdot \, Q \, ( ~ R ), a binary \cdot with first operand Q (true) and second ~ R (unary ~ on R, yielding false, so \lnot false = true); thus, true \land true = true. Applying v gives true \lor true = true. The trace scans the input sequentially, recursing into subexpressions like ~ R (depth 1 under \cdot, depth 2 under v); results are assembled post-recursion, ensuring arity (unary for ~, binary for others) prevents premature application.[19]
A common pitfall in evaluation is applying an operator before fully resolving its operands, such as mistaking * \, 4 \, 5 in the first example as immediate without recursion, leading to incorrect partial results like treating 4 and 5 separately from the outer +. Proper input scanning from left and strict recursion depth management avoids this by confirming complete subexpression consumption before returning.
Applications in Logic
In Polish notation, propositional logic formulas are structured by treating logical connectives as prefix operators that precede their operands, eliminating the need for parentheses through fixed arity conventions. Unary connectives such as negation are represented simply as the operator followed by the proposition, for example, N P for ¬P, where N denotes negation and P is a propositional variable.[3] Binary connectives like implication follow similarly, written as C P Q for P → Q, with C standing for implication and the operands P and Q placed after without additional grouping symbols.[3] For generality, connectives can extend to n-ary forms, allowing multi-place operators to apply to multiple arguments in sequence, as formalized in systems where the operator's arity determines the number of subformulas it governs.[20]
This prefix structure extends naturally to predicate logic, where quantifiers function as unary prefix operators binding variables to subsequent formulas, such as A x P(x) for ∀x P(x), with A representing the universal quantifier and P(x) the predicate applied to variable x.[21] Function symbols in terms are likewise prefixed to their arguments, for instance, f x y for f(x, y), enabling nested compositions like g f x h y z for g(f(x), h(y, z)) without ambiguity.[21] Multi-place predicates integrate seamlessly, with the predicate symbol preceding its term arguments, maintaining the strict prefix order for all operators including relations like R x y for R(x, y).[20]
The representation aligns with formula trees, where Polish notation corresponds to a preorder traversal of the syntax tree: the root node (main operator or connective) is written first, followed recursively by the traversals of its subtrees in left-to-right order.[22] This traversal method inherently avoids ambiguity in nested scopes, as the fixed arity of each operator dictates the partitioning of the sequence into subformulas, ensuring unique parsing without precedence rules or delimiters.[20]
Jan Łukasiewicz's original system enforced a strict prefix format for all operators, including multi-place predicates and connectives, as introduced in his work on three-valued logic to facilitate compact, linear expression of complex formulas.[3] For example, the principle of non-contradiction ¬(P ∧ ¬P) appears as N K P N P, with K for conjunction.[3]
Conversion from infix notation to Polish notation involves systematic prefixing while preserving the formula's truth-conditional structure: recursively convert subformulas, then place the main operator before the sequence of converted operands, adjusting for operator arity to maintain equivalence.[20] For instance, the infix (P → (Q ∧ R)) becomes C P K Q R, ensuring the implication's scope over the conjunction is retained through prefix ordering.[22]
Benefits for Reasoning
Polish notation's prefix structure ensures complete unambiguity in formula representation, eliminating parsing disputes that arise from operator precedence and parentheses in infix notations, which is particularly beneficial for complex logical proofs. This clarity facilitates automated theorem proving by allowing proofs to be modeled as unambiguous tree structures, such as D-terms using detachment operations, enabling efficient mechanized search and compression in systems like Prover9.[7][23]
The notation's design supports ease of mechanization through its inherent recursive nature, where the leading symbol identifies the primary connective, and subformulas are processed recursively, lending itself to formal definitions of logical consequence and validity without additional disambiguation rules.[7] This recursive parsing also enables simple algorithmic checks for well-formedness, such as a counting procedure that verifies formula balance by tracking connective and variable occurrences.[7]
Pedagogically, Polish notation reveals the hierarchical structure of logical arguments by explicitly prefixing operators, making the main connective and nested dependencies immediately apparent, which aids in teaching deductive processes and formula analysis once the initial learning curve is overcome.[7]
Historically, the notation enabled Jan Łukasiewicz's axiomatizations of propositional logic and the development of three-valued logics without errors from precedence conventions, as seen in his 1924 and subsequent works where prefix forms streamlined the representation of multi-valued implications.[7][5]
Despite these advantages, Polish notation is less intuitive for arguments resembling natural language, where infix forms align more closely with everyday expression, contributing to its limited adoption in broader logical discourse.[7]
Practical Implementations
In Programming Languages
Polish notation, also known as prefix notation, forms the foundational syntax of the Lisp family of programming languages, introduced by John McCarthy in 1958 as a means to represent both code and data uniformly through s-expressions.[11] In Lisp, expressions such as arithmetic operations are written with the operator preceding its operands, enclosed in parentheses, for example, (+ 1 (* 2 3)), which evaluates to 7.[24] This prefix structure enables homoiconicity, where code is treated as data, facilitating powerful macro systems that allow programmatic manipulation of source code during compilation or evaluation.[11]
Dialects of Lisp, such as Scheme and Clojure, have retained this prefix notation as their core syntactic form, preserving the uniformity that supports metaprogramming and extensibility.[25] Scheme, developed in the 1970s, uses fully parenthesized prefix expressions to define procedures and handle higher-order functions seamlessly.[25] Similarly, Clojure, a modern Lisp dialect running on the Java Virtual Machine since 2007, employs prefix notation for all function applications, enhancing interoperability with host platforms while maintaining Lisp's expressive power.[26]
Historically, Polish notation has appeared in variants of ALGOL, particularly as an intermediate representation in compilers for ALGOL 60, where it simplified expression parsing and evaluation.[27] Prolog, a logic programming language from the 1970s, incorporates prefix operators for unary predicates and arithmetic, such as negation as \+ Goal, aligning with its term-based syntax for rule definitions.[28]
In functional programming paradigms, prefix notation synergizes with concepts like currying and higher-order functions by treating all operators uniformly as function calls, allowing partial application and composition without special precedence rules.[29] This uniformity simplifies the implementation of abstractions, as functions can be passed and returned like any data, promoting concise expressions for complex computations.
Modern domain-specific languages, such as Wolfram Mathematica introduced in 1988, utilize prefix forms for mathematical expressions, exemplified by Plus[1, Times[2, 3]], which supports both symbolic computation and numerical evaluation in a consistent manner.
Despite these advantages, the prefix notation in Lisp variants has faced challenges regarding human readability, primarily due to dense parenthesization and the need to scan left-to-right for operator placement, prompting extensions like infix operator definitions in Common Lisp via reader macros or libraries such as Readable.[30]
Stack-Based Processing
Polish notation, also known as prefix notation, can be evaluated using an iterative stack-based algorithm that processes the expression without recursion. The method scans the expression from right to left, pushing operand atoms onto a stack and, upon encountering an operator, popping the required number of operands (typically two for binary operators), applying the operation, and pushing the result back onto the stack.[13] This approach ensures unambiguous evaluation by handling operator precedence inherently through the notation's structure.[31]
The following pseudocode illustrates the core iterative algorithm for a binary operator expression in Polish notation:
initialize empty [stack](/page/Stack) S
scan [tokens](/page/Token) from right to left in the expression
for each [token](/page/Token):
if [token](/page/Token) is [operand](/page/Operand):
[push](/page/Push) [token](/page/Token) to S
else if [token](/page/Token) is binary [operator](/page/Operator):
pop left_operand from S
pop right_operand from S
result = apply [operator](/page/Operator) to left_operand and right_operand
[push](/page/Push) result to S
return top of S (the final result)
initialize empty [stack](/page/Stack) S
scan [tokens](/page/Token) from right to left in the expression
for each [token](/page/Token):
if [token](/page/Token) is [operand](/page/Operand):
[push](/page/Push) [token](/page/Token) to S
else if [token](/page/Token) is binary [operator](/page/Operator):
pop left_operand from S
pop right_operand from S
result = apply [operator](/page/Operator) to left_operand and right_operand
[push](/page/Push) result to S
return top of S (the final result)
In this process, the first popped operand serves as the left operand and the second as the right, due to the last-in-first-out nature of the stack and the right-to-left scan.[13] For example, evaluating the expression + 2 3 (equivalent to 2 + 3) involves pushing 3, then 2, popping 2 (left) and 3 (right), computing 5, and pushing it as the result.[32]
This iterative method offers advantages over recursive evaluation, particularly for deeply nested expressions, as it avoids potential recursion depth limits in programming environments while maintaining linear time complexity O(n) where n is the expression length.[33] Space usage is O(n) in the worst case for the stack to hold intermediate results, but requires only constant additional space beyond the stack itself.[13]
A common variant adapts the algorithm for reverse Polish notation (postfix), which scans left to right instead: operands are pushed, and operators pop the right operand first followed by the left, reflecting the notation's operand-operator order.[13] The key difference for prefix scanning lies in the reversed traversal direction, which aligns operator discovery after its operands have been buffered on the stack.
Stack-based processing of Polish notation finds practical use in compilers for parsing mathematical expressions during code generation and in interpreters for efficient runtime evaluation of formulas, where the stack manages intermediate computations seamlessly.[33]