Home » Operators in Prolog

Operators in Prolog

by Online Tutorials Library

Operators in Prolog

notation contains a number of arguments in parenthesis like likes(hary, jack).

Any user-defined predicate which has two arguments can be converted into an infix operator as an alternative. In this, we can write the functor between the two arguments, and they have no parenthesis like

Any user-defined predicate which has one argument can be converted into a prefix operator. In this, we can write the functor before the arguments, and they have no parenthesis like

Instead of

Alternatively, we can convert a unary operator into a postfix operator. In this, we can write the functor after the argument.

The rules can also use the operator notation to aid readability. Some users of Prolog may find a rule like

If the above rule is written as follow, it is easier to understand:

If preferred, the operator can use the standard notation ‘functor and argument’. ‘Mixed’ notation is also permitted if likes/2, is_female/1, owns/2, and isa_dog/1 are all operators and a valid form of the previous rule.

At the system prompt, if the user enters a goal using op predicate, any user-defined predicate which has one or two arguments can convert into an operator. The op predicate has three arguments.

For example

‘Operator precedence’ is shown by the first argument. It is an integer from 0 upwards. On the implementation, the range of numbers depends. If the number is lower, the precedence will be higher. When the operator is used more than once in a term, the values of operator precedence will be used to determine the order of operator. In most cases, we will use an arbitrary value like 145.

The second argument is represented by one of the three atoms:

xfy shows that it is a binary predicate, and it is to be converted into an infix operator.

fy shows that it is a unary predicate, and it is to be converted into a prefix operator.

xf shows that it is a unary operator, and it is to be converted into a postfix operator.

The name of the predicate is shown by the third argument. That predicate is to be converted into an operator.

We can also convert a predicate into an operator by placing a line like

In a Prolog program, consult or reconsult is used to load a file. Note that it must include the prompt(two character ?-). When a goal is used in this way, the whole line is called as a directive. In this case, before the first clause that uses the likes operator, the directive must be placed in a file.

Various built-in predicates have been pre-defined as an operator. To compare the numerical value, these include relational operator like < that denote ‘greater than’ and > that denotes ‘less than’.

In the body of the rule, the following terms may be included.

The built-in predicate is also used the bracket notation that is defined as an operator.

For example


You may also like