## Introduction to Prolog Unification

Prolog unification has one or more variables given values to make two call terms as identical, and this process is called binding variables to values. In prolog unification, prolog agrees that the two terms to unify, i.e., variables unify with anything, and hence they will unify with each other. The process in which Prolog matched two terms is known as Prolog unification. The concept is similar to unification logic. This matching process is done from left to right, and it fails if there is no match found.

### Syntax of Prolog Unification

It is not the syntax of Prolog unification but the examples of Unification.

`?- X = X.`

`?- bob = X.`

`?- bob = robert.`

Prolog’s best feature is one of its built-in pattern matching algorithms, i.e., unification.

We have three types of terms involved with unification:

**Constants:**Like numbers (34) and franklin (lower case letters, also known as atoms).**Variables:**Like X and Y (words starting with upper case).**Complex Terms:**These terms have form.

Two terms are said to be unified if the two can be matched, and they are said to match only if:

- They are of the same term.

Or

- They contain variables that can unify so that two terms without the variables are the same.

For example, let us consider terms as ‘rex’ and ‘rex’; these unify as they are the same atom.

Similarly, terms 34 and 34 unify, as they are the same number.

Terms Y and Y unify, as they are of the same variable.

Terms men(bob) and men(robert) are also unified, as they are made equal by instantiating bob to Robert.

Terms men(bob, X) and men(X, robert) do not unify. Here, it is not possible to find instantiation of X, which makes both terms equal.

Instantiating X to bob would give terms men(bob, bob) and men(bob, robert), which shows these are unequal. However, instantiating X to robert would give men(bob, robert) and men(robert,robert), which are also not equal.

This leads us to one of the conclusions, such that users are not only interested in the fact that two terms unify, but users also want to know how variables are to be instantiated to be equal. Hence prolog performs all the necessary instantiation such that the terms are equal going forward. This particular functionality in Prolog allows to build of complex terms and makes unification one of the powerful programming mechanisms.

### Logic Behind Prolog Unification

Unification not only tells which terms Prolog will unify but also show what prolog will do to the variables to achieve unification. If value1 and value 2 are constants, then value1 and value2 unify if and only if they are of the same atom or same number. If value1 is a variable and value2 is any type of value, then value1 and value2 will unify, and value1 is instantiated to value2. Likewise, if value2 is variable and value1 is any type of value, then value1 and value2 will unify, and value2 is instantiated with to value1. If both value1 and value2 are variables, both are instantiated with each other and share values.

If value1 and value2 are complex terms, they both values unify if and only if.

- They have the same factor and arity.
- And all their corresponding arguments are unified.
- And variable instantiations are compatible with each other.

Two terms are unified if and only if they follow the previous three clauses that are to unify.

### Examples of Prolog Unification

Different examples are mentioned below:

#### Example #1

Rule number 1

**Code:**

`?- X = X.`

**Output:**

This shows Boolean value true according to rule no.1 mentioned above.

#### Example #2

Rule Number 4

**Code:**

`?- val(val1,val2(val3,val4(val5,val6)))=val(val1,val2(val3,val4(val5,val6))).`

**Output:**

#### Example #3

Prolog Unification

**Code:**

`?- men(bob,robert)= men(bob,robert).`

**Output:**

#### Example #4

Instantiation

**Code:**

`?- subject(ravi,Maths)=subject(Priya,science).`

**Output:**

This shows that values are instantiated.

#### Example #5

No Unification

**Code:**

`?- subject(ravi,priya)=subject(Maths,Maths).`

**Output:**

Here, there is no unification because subjects (Maths, Maths) must have the same 1st and 2nd arguments.

#### Example #6

Lists

**Code:**

`?-[val1,val2,val3]=[Val1,Val2|Val3].`

**Output:**

Unification on the list does not have to be restricted in finding the first head element. Here we can see 1st and 2nd elements (Val1 and Val2) and then tail (Val3).

#### Example #7

Notational Variants

**Code:**

`?- [ravi,priya,shweta] = [ravi|[priya|[shweta|[]]]].`

**Output:**

### Conclusion

We have seen what Prolog unification means and what is the actual logic behind unification. We have also listed out most of the useful examples to improve knowledge of the concept involved. This style of Prolog programming is particularly used in applications where important concepts have a natural hierarchical structure.

### Recommended Articles

This is a guide to Prolog Unification. Here we discuss the introduction, logic behind prolog unification, and examples, respectively. You may also have a look at the following articles to learn more –

41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses