Thursday 14 February 2019

Prolog: Unifying operator (=)


When you try to compare two variables X and Y using = operator, Prolog tries to unify the variables.

If X and Y are two variables, then prolog follow below rules to unify them.

a. If X is uninstantiated variable and Y is some instantiated term, then X and Y are equal. X will be instantiated with the term pointed out by Y.

7 ?- capital(delhi, india) = X.
X = capital(delhi, india).

8 ?- write($X).
capital(delhi,india)
X = capital(delhi, india).

As you see above example, Since X is uninstantiated variable, = (unifying operator) initialize the structure capital(delhi, india) to X.


b. Numbers are always equal to themselves.
11 ?- 10 = 10.
true.

12 ?- 123.4 = 123.4.
true.

13 ?- 12e34 = 12e34.
true.


c. Atoms are always equal to themselves.
14 ?- krishna = krishna.
true.

15 ?- laptop = mobile.
false.

16 ?- pen = pen.
true.

d. Two structures are equal,
         1. If they have same function
         2. They have same number of components

         3. All the components are equal.

17 ?- employee(1, personalDetails(krishna, gurram), projectDetails(cpcs, 10)) = employee(1, personalDetails(krishna, gurram), projectDetails(cpcs, 10)).
true.


As I said, = operator tries to unify the variables. Below pattern maps Id, PersonalDetails and ProjectDetails.

18 ?- employee(1, personalDetails(krishna, gurram), projectDetails(cpcs, 10)) = employee(Id, PersonalDetails, ProjectDetails).
Id = 1,
PersonalDetails = personalDetails(krishna, gurram),
ProjectDetails = projectDetails(cpcs, 10).




Previous                                                 Next                                                 Home

No comments:

Post a Comment