Module 2
Queries with more than one goal and the Anonymous Variable

 

 

Suppose our database includes information not only about animals but also the location and opening times of zoos, for instance:
 

     zoo(jersey, address('Les Augres Manor, Trinity, Jersey'),
               open(10), close(dusk)).
We could envisage wanting to know which zoos had certain animals and also wanting to know the opening times of these zoos.
 

We'll assume that (for the time being) we have only the following animal recorded in the database:
 

     mammal(gorilla, 'n''gola', female, parents(mother('n''pongo'),
                    father(jambo)), 1988, jersey).
To link the two structured objects, we need to find an argument they have in common. The obvious one is the name of the zoo. Our query has to clearly show that the value of the first argument in zoo/4 is the same as that of the sixth argument in mammal/6. So far we have seen only one practical way of doing this and this is by using a single variable.
 
     | ?- mammal(gorilla, Name, Sex, Parents, DoB, Zoo),
          zoo(Zoo, Address, Open, Close).
     Name=n'gola
     Sex=female
     Parents=parents(mother(n'pongo), father(jambo))
     DoB=1988
     Zoo=jersey
     Address=address('Les Augres Manor, Trinity, Jersey')
     Open=open(10)
     Close=close(dusk)
Let's look at the hypothesis that this query is forwarding:
 
  • there is a structured object in the database called mammal
  • this structured object has six arguments
  • the first argument must unify with the atom "gorilla"
  • the remaining five arguments will unify with the remaining five variables "Name, Sex, Parents, DoB, Zoo"
  • there is a structured object in the database called zoo
  • this structured object has four arguments
  • the first argument must unify with whatever Zoo was unified with in the fourth part of the hypothesis
  • the remaining three arguments will unify with the remaining three variables "Address, Open, Close"
If all these conditions are true, then the hypothesis has been proved.

We can say that this query consists of two goals. The two goals are joined by the use of the comma, ",". The comma is an important part of Prolog, because it can always be read as meaning "and". The comma is often called the conjunction.  


Using the anonymous variable
 

In the gorilla in the zoo example, we were interested in knowing which zoo had a gorilla and and what its opening time was. The list of variable instantiations given by Prolog is clearly obscure. It would be best if we could produce a beautifully formatted output display something along the lines of:
 
     The following zoos have gorillas:
         Zoo         Opening time
       Jersey            10am
We haven't learnt enough of Prolog to achieve this yet, but we can stop Prolog giving us some of the surplus variable instantiations.

We saw in Module 1 that variables may begin with the underscore character ("_"), for instance "_Triumph_". We can use the underscore character on its own as the "anonymous variable". It is used wherever we aren't interested in the value that a variable is instantiated to. We could rewrite the query about gorillas and zoos to exclude redundant information as follows:
 

     | ?- mammal(gorilla, _, _, _, _, Zoo),
          zoo(Zoo, _, Open, _).
     Zoo=jersey
     Open=open(10)
There is one peculiarity in the use of the anonymous variable. We have seen that once a variable gets a value it cannot be altered, but apparently the anonymous variable can have several values at the same time. In this example it is used to stand for: 'n''gola', female, parents(mother('n''pongo'), father(jambo)), 1988, address('Les Augres Manor, Trinity, Jersey'), close(dusk). In effect, Prolog treats the anonymous variable as so very unimportant that it doesn't even bother to take a note of what the anonymous variable stands for, and thereby it is possible for this variable to stand for several things apparently at once.
 

Take time to work through the Self-Test 4