Javatpoint Logo
Javatpoint Logo

Equality Operators in Prolog

To test the equality and inequality, Prolog has three types of relational operators. The value of arithmetic expression can be compared by the first type of relational operator. The terms can be compared by other two types of relational operator.

Equality Operator (=:=)

Given Arithmetic expression

If E1 and E2 evaluate to the same value, the above E1 =:= E2 succeed.

For example:

Prolog uses checkeven/1 predicate to identify whether an integer is odd or even.


The // is the division operator. It divides the first argument to the second argument and the result of this division truncates to the nearest integer between it and zero. So 7//2 is 3, 18//2 is 9, -27//2 is -13, -10//2 is -5. Divide the integer by 2 and multiplies it by 2 will give the original integer if it is even, otherwise it is not.

Inequality Operator (=\=)

Given arithmetic expression

If E1 and E2 do not evaluate to the same value, arithmetic expression E1 =\= E2 succeeds.

For example:

Terms Identical

Given Goal

In the infix operator ==, both arguments must be terms. If Term1 and Term2 are identical, the above goal succeeds.

For example:

When the arithmetic expression value is used with is/2, the arithmetic expression value can only evaluate. Here the term is defined by 5+4 with arguments 5 and 4 and functor +. This term is totally different from 2+7 term.

Term Not Identical (\==)

Given goal

The above goal is used to test whether Term1 and Term2 are not identical. If Term1 == Term2 fails, the Term1 \== Term2 succeeds. Otherwise the goal fails.

For example:

The above output shows that variables A and B are different variables and both are unbound.

Term Identical With Unification (=)

The == operator is similar to equality operator = but with vital difference. If Term1 and Term2 unify, the Term1==Term2 succeeds.

For example:

Non-Unification (\=)

Given goal

If Term1 = Term2 fails, the above goal succeeds. That means two terms cannot be unified. Otherwise the above goal fails.

For example:







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA