AI Glossary / Graphs and symbolic AI / Advanced
Prefix
Definition
Prefix notation places an operator before its arguments. For example, +(4, 7) expresses the same addition as the infix expression 4 + 7. In symbolic AI, operator-first notation can represent nested terms, function applications and logical expressions. This entry concerns expression notation, rather than the different meaning of 'prefix' as the beginning of a word or string.
What it means
The position of an operator is a writing convention. Addition can be written between its operands, before them or after them without changing the intended arithmetic. What matters is preserving the expression's structure and the order of arguments. Prefix notation makes an expression's outer operation appear first, followed by the expressions on which it operates. Each argument can itself be a nested expression, allowing a larger calculation or symbolic term to be described recursively.
How it works
Start with an operator and provide the arguments required by its grammar. In a function-style form, minus(12, 5) has the operator minus and two arguments. In a parenthesized Lisp-style form, the corresponding structure can be written (- 12 5). These have different surface punctuation but both put the operator first.
A parser identifies the operator and recursively reads its arguments to construct an expression tree. Fully specified grouping or known operator arities makes that structure recoverable. Parenthesis-free Polish notation can be unambiguous when arities and token boundaries are fixed. Prefix position alone does not supply missing information when operators accept varying numbers of arguments. It also does not, by itself, determine a programming language's evaluation strategy or the meaning of its operators.
Three practical examples
- A symbolic reasoning system stores a term such as add(quantity, reserve) with its function symbol first. It can manipulate the term's structure without confusing the function with an argument.
- A language interpreter reads parenthesized calls with the function expression before its arguments. Racket, for example, documents this form and checks whether the resulting function accepts the supplied arguments.
- A formula-conversion tool translates an infix arithmetic expression into prefix form before constructing a tree. Testing nested subtraction helps ensure that the converter preserves argument order instead of merely rearranging visible symbols.
Think of it this way
Prefix notation resembles an instruction that begins with the action: 'subtract the second amount from the first amount.' The operation comes first, followed by the information it needs. If one amount must be calculated first, its own instruction fits inside the larger one. The nesting tells you which result belongs to which operation.
Worked example
Consider the infix expression (18 - 6) / (2 + 1). Its outermost operation is division. The first argument is the subtraction expression 18 - 6; the second is the addition expression 2 + 1.
In function-style prefix notation, write divide(subtract(18, 6), add(2, 1)). In a parenthesized operator-first form, write (/ (- 18 6) (+ 2 1)). If all three operators are binary and tokens are separated, the parenthesis-free prefix form is / - 18 6 + 2 1.
Read the final form recursively. The initial slash requires two complete arguments. The next token, minus, begins the first argument and consumes 18 and 6. Plus begins the second argument and consumes 2 and 1. The resulting tree has three operator nodes and four numeric leaves, making seven nodes altogether.
Evaluate the subtraction to get 12 and the addition to get 3. Divide 12 by 3 to obtain 4. Writing the same expression in postfix notation would put each operator after its arguments: 18 6 - 2 1 + /. The three notations describe the same tree under the stated arithmetic conventions.
Now compare subtract(18, subtract(6, 2)) with subtract(subtract(18, 6), 2). The first is 18 - (6 - 2), which equals 14. The second is (18 - 6) - 2, which equals 10. Moving parentheses or changing the tree changes the result even though both expressions contain the same numbers and two subtraction operators. Prefix notation preserves meaning only when its grouping and argument order are preserved.
Illustrative case study
In a fictional formula editor, the team converts user-entered infix expressions into a prefix representation for storage. Its first demonstration uses only addition and multiplication with convenient inputs, so an argument-order bug goes unnoticed.
The team adds the two nested subtraction examples above and discovers that both were being stored as the same tree. It corrects the conversion and checks that converting back to fully parenthesized infix preserves the original grouping. It also tests missing arguments and extra tokens. The exercise verifies structural fidelity instead of relying only on a few arithmetic outputs that could agree by coincidence.
Common misconceptions and mistakes
Prefix notation does not mean evaluating the leftmost operator immediately before obtaining its argument values. Operator placement and evaluation rules are separate matters. A symbolic system may manipulate a term without numerically evaluating it at all.
Another mistake is assuming that prefix expressions never need delimiters. Known fixed arities can make a token stream self-structuring, but variable-arity calls require enough syntax to identify their boundaries. Finally, preserving the collection of tokens is insufficient: subtraction and division demonstrate why argument order and nesting matter. The arithmetic meaning of a slash also depends on the language or model, including its treatment of integer operands.
Business use
Prefix notation is useful background for teams working with rule engines, symbolic formulas, programming languages or structured representations generated by AI. The operational concern is whether expressions are parsed, stored and translated without changing their meaning. Define the grammar, operator arities and numeric conventions explicitly. Validate expressions before using them in calculations, and test noncommutative operators and nested structures. A user-facing editor can still display familiar notation while its internal representation preserves the underlying expression tree.
People also ask
Is ordinary function-call notation a form of prefix notation?
Yes, when the function or operator precedes its arguments.
Does prefix notation automatically make a calculation faster?
No. Performance depends on parsing, representation and execution, not just where the operator is written.
Are all parenthesized operator-first forms ordinary function calls?
No. Languages can also define special syntactic forms with their own rules.
Check your understanding
What is the outer operator in the worked expression?
Answer: Division.
How many leaves are in its expression tree?
Answer: Four numeric leaves.
Why do the two nested subtraction examples differ?
Answer: Their grouping assigns different expressions to the subtraction operators' arguments.
Related terms
- Arity
- Parsing
- Infix notation
- postfix notation
- Polish notation
- operator
- operand
- expression tree
- function application
- symbolic term
Learn next
Translate (20 / 5) - (9 - 7) into both prefix forms and postfix notation. Draw its expression tree, evaluate it and check that a round-trip conversion preserves the grouping.
More in Graphs and symbolic AI
- Preference constraints - Preference constraints express which assignments are more desirable in a constraint problem, rather than simply declaring which assignments are permitted.
- Preference elicitation - Preference elicitation is the process of obtaining information about what a person or group prefers so a system can support decisions on their behalf.
- Preference independence - Preference independence, also called preferential independence, means that the ranking of alternatives on a specified set of attributes does not change when...
- Preflow-Push - Preflow-push, also called push-relabel, is an algorithmic approach for finding a maximum flow from a source to a sink in a capacitated network.
- Premise - A premise is a statement used as a starting point for reasoning.
- Principle of indifference - The principle of indifference assigns equal probabilities to alternatives when the available information provides no reason to favor one over another.
Sources
AIMA glossary, prefix: https://github.com/aimacode/aima-glossary/blob/master/glossary.md Racket Guide, Function Calls: https://docs.racket-lang.org/guide/application.html
Previous: Prefilter (anti-aliasing) Next: Prefix code