Operators in Prolog are simply a notational convenience. We could
represent the English statement ``2 plus 3'' in Prolog as
+(2, 3) but this
isn't the way we are accustomed to seeing the expression. A more
familiar notation involves making the + an INFIX operator so that we
can write 2 + 3. When an operator is defined it is also allocated two
descriptive attributes:
+ is 500 while the precedence
of * (the multiplication operator) is 400. The rule is that in a
nested term the operator with highest precedence value is the
principal functor. For instance, the term 2 + 3 * 5 has principal
functor +. If we were to write it in standard Prolog syntax this
term would look like this: +(2,*(3,5)). NOTE that terms representing
numerical expressions are NOT automatically evaluated, like in a
functional language. There is a special built--in predicate for
doing this, which we shall meet later on.
fx
or fy.
xf or yf.
xfx, xfy or yfx.
+ operator
is both prefix (e.g. + 2) and infix (e.g. 2 + 3).
You can check what the current operators in your Prolog system are by
using the current_op/3 built-in predicate. You can also define your
own operators using the built--in op/3 predicate but we won't worry
about this yet. To display a term containing operators in standard
Prolog syntax, use the built-in predicate display/1.
| ?- current_op(Precedence, Type, +).
Precedence=500
Type=fx ;
Precedence=500
Type=yfx
yes
| ?- current_op(Precedence, Type, *).
Precedence=400
Type=yfx
yes
| ?- display(2 + 3 + 4).
+(+(2,3),4)
yes