Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Unification Operators



In prolog, = operator is a unification operator. It is used to make two items identifical by instantiating the variable as necessary. It acts as a pattern matching and assignment operator as well.

Usage

Example - Instantiating X and Y to 9.

| ?- X = 9.

X = 9

yes
| ?- 9 = Y.

Y = 9

yes
| ?-

Example - Comparing Values and Variables.

| ?- 9 = 9.

yes
| ?- 9 = 7.

no
| ?- X = Y.

Y = X

yes
| ?-

Example - Unifying Structure.

| ?- [Head | Tail] = [1, 2, 3].

Head = 1
Tail = [2,3]

yes
| ?-

Non-Unifiable Operator

\= is a non-unifiable operator and returns success when the arguments cannot be unified.

Example - Non-Unifiable Operator Usage

| ?- 9 \= 7.

yes
| ?- 9 \= 9.

no
| ?- X \= 9.

no
| ?- 

As X can be unified with a value, last expression fails.

Advertisements