From C++ to HERCs Prolog: Assignment statement

In this example we will re-define the eq instruction, which is part of the implementation.

Let's try to implement an assignment function in C++ type of language. We will pass a reference to a variable and the value. Our function will assign the value to the referenced variable. Below is the typical code:

void assign (int & a, int b)
{
    a = b;
}

This code is not completely universal. It can only deal with integer variables and values. This is not important at the moment. What is important is the fact that in Prolog we don't specify any data types. We will now re-write this definition without data types.

assign (a, b)
{
    a = b;
}

This code will not compile. Please, don't worry too much. It is a kind of a pseudo-code anyway.
Please, make an observation. The body of this function contains just a single instruction. It assigns the value to the variable. In Prolog you can actually remove this assignment instruction. THIS IS IMPORTANT !!! so I will write it again. In Prolog you can actually remove this assignment instruction.
how?
.... by changing the names of the parameters.

assign (a, a)
{
//    a = a;
}

... or simply....
assign (a, a) {}

And this is all you have to write in Prolog! Empty body! Just the parameter names are identical.

How does it work?

Quite simply in fact. In situation like this, Prolog expects you to pass two identical values. If they are not identical (i.e. one of the is a variable), then Prolog will try to unify them. Therefore, if one of the parameters is a variable, then this variable will become the same thing as the other parameter. In any case, after this instruction, those two "things" will be identical. And if they can not be unified? What then? Well, it will fail. Prolog will backtrack.

Before we see some examples, we will translate the abovementioned definition into the proper syntax. Of course, we will use S-Expressions again.

From: assign (a, a) {} through {(assign a a)} to [[assign *a *a]]

Examples now:
[res [show [*x *y]] [assign *x 12] [show [*x *y]]] will show [*0 *1] and [12 *0].
[res [show [*x *y]] [assign 12 *y] [show [*x *y]]] will show [*0 *1] and [*0 12].
[res [show [*x *y]] [assign *x *y] [show [*x *y]]] will show [*0 *1] and [*0 *0].
... and this particularly interesting one!
[res [show [*x *y]] [assign *x *y] [show [*x *y]] [assign 12 *y] [show [*x *y]]] will show [*0 *1], then [*0 *0] and [12 12].
That is right! The first assignment here binds the two variables together. Therefore, if one of them gets the value assigned, the other one gets it assigne as well.

What if assign can not unify parameters? Then it will backtrack.
[res [assign 12 14]] will show Doesn't work.
It is a very interesting situation indeed. As you can see, it is possible to use the assign instruction to compare the two parameters.

Other computer languages usually have several different methods of comparison. In Prolog, you use assignment instruction to compare things. Our assign instructin is in fact a pre-defined Prolog eq instruction.