Module 2
Querying structured objects within structured objects

 

 

We have already seen how to form queries for structured objects without nested structured objects within them. Querying structured objects within structured objects is only a small step further on.

To recap briefly: we saw with the simple examples (such as mammal(gorilla, jambo).) that we may use either atomic terms or variables to stand for each argument. With structured objects within structured objects, we have to ensure that each item in the nested structured object is covered by either a term or a variable. To illustrate in more detail, let's look again at an example we diagrammed above:  
 

     parents(mother('n''pongo'), father(jambo)).
There are a range of possible queries:  
 
     | ?- parents(mother(Mum), father(Dad)).                   1
          Mum=n'pongo
          Dad=jambo
     | ?- parents(Mum, Dad).                                   2
          Mum=mother(n'pongo)
          Dad=father(jambo)
     | ?- parents(mother('n''pongo'), father(Dad)).            3
          Dad=jambo
and you could probably devise a few more.  
 

We'll look again at the diagrams and we should be able to see how the three queries match the structured object.


Query 1

We will show the query in outline italic text connected by dashed lines.
 

 

The important thing is that each node in the original tree has a corresponding node in the tree that diagrams the query. The leaf nodes are variables in this example.    
 

 
Query 2

In this example, variables are used to stand for non-terminal nodes. In Prolog's response to the query, this was indicated by a reply showing that a variable had been unified with a structure, eg: Mum=mother('n''pongo')


 
 

I have used the empty triangle to indicate that the variable will stand for the node and everything growing from it.  
 

 

Query 3

This example has all nodes specified, as with the first query, but differs in that one of the terminal nodes is represented by an atom ("'n''pongo'").


   

Take time to work through the Self-Test 3