Monday, 4 February 2019

Prolog: Unification


Unification is a process of mapping items with variables.

For example, below facts file contain eats relation, it specifies who eats what.

facts.pl
eats(krishna, mangoes).
eats(krishna, ghee).

eats(rama, grapes).
eats(rama, rice).
eats(rama, pineapple).

How can we find ‘what rama eats’?
You can query using unification.
eats(rama, X).


If you query Prolog using above statement, Prolog, tries to map X with possible answers in the database. Prolog will give you the first matching answer, if you are satisfied with the answer, then press ., else press ;, then Prolog will give you the next possible answer if exists.

1 ?- consult(facts).
true.

2 ?- eats(rama, X).
X = grapes ;
X = rice .

3 ?- eats(krishna, X).
X = mangoes .

4 ?- eats(X, mangoes).
X = krishna.


As you see the above queries, prolog prints both the variable name and matched value.


Previous                                                 Next                                                 Home

No comments:

Post a Comment