How do we say something like “What does Fred eat?”
Suppose we had the following fact in our database:
eats( fred, mangoes ).
How do we ask what fred
eats?
We could type in something like
?- eats( fred, what ).
However Prolog will say no
.
The reason for this is that what
does not match with mangoes
.
In order to match arguments in this way we must use a variable.
The process of matching items with variables is known as unification.
Variables are starting with a capital letter, for example,
X /* a capital letter */
VaRiAbLe /* a word——either case of letters */
My_name /* linking words together via '_' */
Thus returning to our first question we can find out what fred
eats by typing
?- eats( fred, What ).
What = mangoes
yes
As a result of this query, the variable What
has matched (or unified) with mangoes
.
We say that the variable What
now has the binding mangoes
.
When we pose a query, if the query is successful, Prolog prints both the variable and the variable name, as we see above.
Variable Example I
Let's consider some examples using facts.
First consider the following database.
loves( john, mary ).
loves( fred, hobbies ).