Javatpoint Logo
Javatpoint Logo

Arithmetic in Prolog

In the previous sections, the examples are non-numerical. In this section, we will use is/2 built-in predicate. This predicate is predefined as an infix operator. The is/2 predicate is placed between the two arguments.

If the first argument is an unbound variable, the predicate is/2 are mostly used. The goal A is -4.2 which shows that A is bound to number -4.2, and the goal succeed.

Arithmetic expression or number can be expressed by the second argument.

For example

In arithmetic expression, any variables must already be bound. The value of these variables must be numerical. The value of arithmetic expression bounds the variable of first argument. If it is not, an error message will be generated as result.

In arithmetic expression, + - * / symbols are special type of infix operator, and these operators are also known as arithmetic operators. In Prolog, operators are used as predicates but here operators are functions and these operators return a numerical value.

Arithmetic expressions can include variables, numbers, operators, and arithmetic functions. These will be written in parentheses with their arguments. These will return numerical values just like the arithmetic operators.

For example: Square bracket of 25.

The minus(-) arithmetic operator is used as a binary infix operator, which is used to describe the difference of two numerical values like A - 2. It is also used as a unary prefix operator, which is used to describe the negative of a numerical value like

Arithmetic functions and Arithmetic operators available in Prolog are shown in the following table:


The is predicate is used in the normal way. The first argument can be a bound variable or a number with numerical value. In two arguments, the numerical values are calculated. If these values are equal, the goal succeeds. It fails if these values are not equal.

The goal A is A + 1 will always fail, whether or not A is bound.

A different approach is used to increase a value by one.

?- increase(4).


?- increase(4, A).

A = 5

Relational Operators

The relational operators are =:=, >, <, >=, =/=, =<. The relational operators compare the values of two arguments. If the first argument's value is equal to, greater than, less than, greater than or equal to, not equal to, less than or equal to the value of second argument, the goal succeeds. Both arguments can be arithmetic expression, bound variable or numbers.

Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA