### 9.4.2 Defining operators

In addition to providing a user friendly operator notation for certain functors, Prolog also let's you define your own operators. So you could for example define a postfix operator `is_dead` and then Prolog would allow you to write `zed is_dead` as a fact in your database instead of `is_dead(zed)`.

Operator definitions in Prolog look like this:

`:- op(`Precedence`, `Type`, `Name`).`

Precedence is a number between 0 and 1200. The precedence of `=`, for instance, is 700, the precedence of `+` is 500, and the precedence of `*` 400. Type is an atom specifying the type and associativity of the operator. In the case of `+` this atom is `yfx`, which says that `+` is an infix operator `f` represents the operator and `x` and `y` the arguments. Furthermore, `x` stands for an argument which has a precedence which is lower than the precedence of `+` and `y` stands for an argument which has a precedence which lower or equal to the precedence of `+`. There are the following possibilities for what Type may look like:

 infix `xfx`, `xfy`, `yfx` prefix `fx`, `fy` suffix `xf`, `yf`

So, your operator definition for `is_dead` could look as follows:

`:- op(500, xf, is_dead).`

Here are the definitions for some of the built-in operators. You can see that operators with the same properties can be specified in one statement by giving a list of their names instead of a single name as third argument of `op`.

 `:- op( 1200, xfx, [ :-, --> ]).` `:- op( 1200,  fx, [ :-, ?- ]).` `:- op( 1100, xfy, [ ; ]).` `:- op( 1000, xfy, [ ',' ]).` `:- op(  700, xfx, [ =, is, =.., ==, \==, ` `                    =:=, =\=, <, >, =<, >= ]).` `:- op(  500, yfx, [ +, -]).` `:- op(  500,  fx, [ +, - ]).` `:- op(  300, xfx, [ mod ]).` `:- op(  200, xfy, [ ^ ]).`

One final thing to note is, that operator definitions don't specify the meaning of an operator, but only describe how it can be used syntactically. An operator definition doesn't say anything about when a query involving this operator will evaluate to true. It is only a definition extending the syntax of Prolog. So, if the operator `is_dead` is defined as above and you ask the query `zed is_dead`, Prolog won't complain about illegal syntax (as it would without this definition), but it will try to prove the goal `is_dead(zed)`, which is Prolog's internal representation of `zed is_dead`. And this is what operator definitions do. They just tell Prolog how to translate a user friendly notation into real Prolog notation. So, what would be Prolog's answer to the query `zed is_dead`? It would be `no`, because Prolog would try to prove `is_dead(zed)`, but not find any matching clause in the database. Unless, of course, your database would look like this, for instance:

`:- op(500, xf, is_dead). kill(marsellus,zed).is_dead(X) :- kill(_,X).`

In this case, Prolog would answer `yes` to the query `zed is_dead`.

Patrick Blackburn, Johan Bos and Kristina Striegnitz
Version 1.2.5 (20030212)