Fortran I
Take a look
Fortran I, which is an ancient programming language (at least for me), had an exotic algorithm to decide which operator will be calculated first. First of all, let's define somethings we need to proceed.
- Let be a finite set of binary operators which we should consider.
- Let be a function which indicates the priority of each operator in .
- If an operator has a higher priority than , it must hold that .
What I want to prove is the following algorithm transforms an math expression
- to an expression that can be evaluated without the priority information
- wihtout changing the evaluation result.
Let's go ahead to the actual algorithm.
Input: S, f, and an math expression E
Output: the transformed expression E'
M := max(f(S))
E' := <M+1 opening parantheses>
For each c in S:
If c is opening paranthesis:
E' := <M+1 opening paranthesis>
Else If c is closing paranthesis:
E' := <M+1 closing paranthesis>
Else If c is an operator in S:
E' := E'||<f(c) closing parantheses>||c||<f(c) opening parantheses>
Else:
E' := E'||c
End If
End
E' := E'||<M+1 closing parantheses>
Return E'
The following is an illustration of each step in a transformation of expression .
⇒ E' = (((
7 ⇒ E' = (((7
× ⇒ E' = (((7)×(
( ⇒ E' = (((7)×((((
2 ⇒ E' = (((7)×((((2
+ ⇒ E' = (((7)×((((2))+((
3 ⇒ E' = (((7)×((((2))+((3
) ⇒ E' = (((7)×((((2))+((3)))
⇒ E' = (((7)×((((2))+((3))))))
The number of preceeding unclosed open parantheses in the transformed expression takes a crucial role on analyzing what's going on. Let me call it potential on the latter part of this post. The number over each symbol represents the potential of it.
An actual proof
There are two things to be proved:
- The evaluation result doesn't change.
- It can be evaluated without knowing the operation priority .
Result consistency I & Evaluability without f
First things first, let's go ahead on the first one. We're going to prove that result consistency holds when doesn't include any parantheses.
Notice that there is no potential difference between right before and right after of each binary operator.
This allows me to say that a potential of each constant number in is exactly for an integer .
Also, a potential of each binary operator is smaller (thus at most ) than the number right before it.
So, all symbols (includuing numbers and binary operators) have non-negative potentials, which is exactly for each binary operator . This ensures the paranthesis validness of .
Therefore, the symbol with the highest priority (i.e. the minimum value of ) has the highest potential. This leads for it to be evaluated first.
At the same time, it means that the second condition, which is the evaluability of without , does hold.
Result consistency II
This part extends the result of the previous part to an expression which includes parantheses.
It can be done by simple logics:
- An opening paranthesis increases potential by .
- Every valid expression without parantheses cannot decrease the potential larger than .
- So, an expression which satisfies the followings must evaluate first.
- includes a paranthesized expression
- and doesn't include any other parantheses
- It can be proved that must be correctly transformed by the given algorithm by an induction.
- So any expression can be transformed to which satisfies result consistency condition.
Summary
So, the given algorithm correctly transform any valid expression to , which satisfies two conditions described before.
includes too many parantheses to ensure the result consistency, however one can calculate a potential of each symbol rather than putting the actual parantheses.
If one has calculated a potential of each symbol, it is easy to calculate the evaluation result with an well-known algorithm named topological sort.
This transformation can be interpreted as an embedding process of the operation priority to the expression itself.