Variables and Unification

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 distinguished by starting with a capital letter. Here are some examples:


X         /* a capital letter */

VaRiAbLe /* a word - it be made up or either case of letters */

My_name /* we can link words together via '_' (underscore) */

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 Examples 1

Let's consider some examples using facts. First consider the following database.


loves(john,mary).

loves(fred,hobbies).

Now let's look at some simple queries using variables


?- loves(john,Who).      /* Who does john love? */

Who=mary /* yes , Who gets bound to mary */

yes /* and the query succeeds*/

?- loves(arnold,Who) /* does arnold love anybody */

no /* no, arnold doesn't match john or fred */

?- loves(fred,Who). /* Who does fred love */

Who = hobbies /* Note the to Prolog Who is just the name of a variable, it */

yes /* semantic connotations are not picked up, hence Who unifies */

/* with hobbies */

Variable Examples 2

Here are some more difficult object/variable comparisons. Consider the following database showing a library of cassette tapes. Notice the final argument to tape, which is the name of a favourite song from the album.


tape(1,van_morrison,astral_weeks,madam_george).

tape(2,beatles,sgt_pepper,a_day_in_the_life).

tape(3,beatles,abbey_road,something).

tape(4,rolling_stones,sticky_fingers,brown_sugar).

tape(5,eagles,hotel_california,new_kid_in_town).

Let's now look at some queries.


?- tape(5,Artist,Album,Fave_Song).             /* what are the contents of tape 5 */

Artist=eagles

Album=hotel_california

Fave_Song=new_kid_in_town

yes

?- tape(4,rolling_stones,sticky_fingers,Song). /* find just song */

Song=brown_sugar /* which you like best from the album */

yes

Exercise 3 - Variable Exercise

Here are some problems for which unification sometimes succeeds and sometimes fails. The items to be compared are labelled with • signs. Indicate the outcome of the proposed match using the yes/no buttons. If you think the unification succeeds, write down any bindings made e.g if (say) X gets bound to foo, then write X=foo as the substitution.


This is the end of the variables and unification topic.


Next Topic

Return to Introduction Menu