## 3   Variables and Unification

### 3.1   Simples unifications

How can we ask something like ''what does Fred eat ?'' If we have the following program :
```     eats(fred,oranges).
```
How do we ask what fred eats ? We could write something like this :
```     ?- eats(fred,what).
```
But Prolog will say no. The reason is that Prolog can't find the relation eats(fred,what) in hi database. In this case we have to use a variable wich will be unified to match a relation given in the program. This process is known as unification.
Variables are distinguished from atoms by starting with a capital letter. Here are some exemples of variables :variables
```   X            /* a single capital letter                    */

VaRiAbLe     /* a word beginning with an upper case letter */

Two_words    /* two words separated with an underscore     */
```
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=oranges

yes
```
In this case Prolog try to unifie 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).
```
In this example we ask who eats oranges. To this query Prolog should answer :
```     ?- eats(Who,oranges).

Who=fred

yes
```
```     ?- eats(Who,apple).

no
```
Prolog answer no because he can't find in his database any atom than can match with this relation.
Now if we only want to know if something is eated by anyone and we don't care about that person we can use the underscore. The '_' can be used like any variable.
For example if we ask eats(fred,_) the result will be :
```     ?- eats(fred,_).

yes
```
The result will be yes because Prolog can find a relation of eat between fred and something. But Prolog will not tell use the value of '_'.
Now if we have the following program :
```     eats(fred,apple).

eats(fred,oranges).
```
```     ?- eats(fred,What).
```
The first answer will be ''What=apple'' because that is the unification that match the first relation of eats with fred in the database. Then prolog will be waiting for you to press a key. If you press enter Prolog will be ready for a new query. In most implemetation if you press the key '';'' then Prolog will try to find if there is any other successful unification. Prolog will give the second result ''What=orange'' because this the second one in the program. If you press again the key '';'' then Prolog will try to find a third unification. The result will be ''no'' because he is not able to find any other successful unification.
```     ?- eats(fred,What).

What=apple ;

What=oranges ;

no
```

### 3.2   Variables unification example

Consider this exemple of program that could be used by a library :
```     book(1,title1,author1).

book(2,title2,author1).

book(3,title3,author2).

book(4,title4,author3).
```
Now if we want to know if we have a book from the author2 we can ask :
```     ?- book(_,_,author2).

yes
```
If we want to know which book from the author1 we have :
```     ?- book(_,X,author1).

X=title1 ;

X=title2 ;
```

 <-- The Facts - Rules -->