Sunday, 18 January 2009

Unification

How do we ask: ''what does Fred eat ?'‘
eats(fred,oranges).
How do we ask: “what Fred eats” ?
?- eats(fred,what).
But Prolog will say “no” to this question. The reason is that Prolog can't find the relation “eats(fred,what)” in its database.
In this case we have to use a variable which will be unified to match a relation given in the program. This process is known as unification.


Now that we know how to use a variables, we can ask the same question as before using the variable What instead of an atom.
?- eats(fred,What).
What=orange
Yes
In this case Prolog try to unify the variable with an atom. ''What=oranges'' means that the query is successfull when “what” is unified with oranges.


With the same program if we ask :
?- eats(Who,oranges).
Which means who eats oranges. To this query Prolog should answer :
?- eats(Who,oranges).
Who=fred
yes

No comments:

Post a Comment