Module 2
What is a structured object?

 

 

The data types we have seen so far have been either atomic or variables. We could include such things in our programs, but (as with procedural languages) these simple data objects are of limited use. We need a more complex and structured data type to indicate some relationships between simple data objects and thus to be able to express and manipulate complex concepts, allowing us to write worth-while programs.

We will illustrate this with an example using information about gorillas held in zoo collections. Unlike most other animals, (except perhaps chimps) gorillas tend to be given names. It is also important to know the parentage of each animal so as to avoid interbreeding at a later stage. (For some animals, such as the red panda, each one in captivity is registered in a stud-book held as a database on a computer.)

Let us start with a simple example. We'll divide the animals into groups such as mammals, reptiles, birds... For each individual, we want to know its species and its name.

     mammal(gorilla, jambo).
This is a structured object. It has three main attributes:
 
functor
the name of the structure. Here it is "mammal". The functor is always an atom.
argument(s)
this is the number of data objects in parentheses after the functor. In this example, there are two arguments: "gorilla" and "jambo". These are both atoms, but arguments need not be atoms, as we shall see below. Arguments are always separated by ",", which you can read as meaning "and".
arity
this denotes the number of arguments that a structure has. This structure has an arity of 2. Structures are commonly referred to by their functor and their arity, eg mammal/2.
This is the basis of your first Prolog program. Using the editor associated with your Prolog system, add the fact "mammal(gorilla, jambo)." to your Prolog database (not forgetting the full stop). You can now enter queries. For instance, you can enter the following queries one-by-one:
 
     | ?- mammal(gorilla, jambo).
     | ?- mammal(Species, Name).
     | ?- mammal(Species, jambo).
     | ?- mammal(_, Name).
I'll leave you to discover the solutions for yourself, but try to predict each answer before you enter each query.
 

Do you think it is worth trying the following queries?
 

     | ?- mammal(Name, Species).
     | ?- mammal(jambo, gorilla).
You will get a response to the first of these two queries that looks something like:
 
     Name=gorilla
     Species=jambo
The response to the second query is "no". When it matches arguments, Prolog matches them firstly on their position and then just as it matches atomic objects (as we saw in the previous Module).
 

One point worth noting is that the queries have just the same syntax as the structured objects.  

Take time to work through the Self-Test 1

Structured objects within structured objects
 

The small example of the gorillas above is very limited. We would most probably want to add which zoo the animal lives in now, sex and some information about parents and date of birth.

Let us extend the structure, concentrating first on parents. Gorillas have two parents, male and female. This may seem obvious and simplistic, but starts to uncover the problem of how we design our data structures. We will examine some of the alternatives.  
 

 
Using argument order

The query using mammal(jambo, gorilla). that didn't work showed that the order of the arguments is crucial. Thus we could simply decide that the fourth and fifth arguments would be the mother and father respectively, eg:  
 
     mammal(gorilla, 'n''gola', female, 'n''ponga', jambo, 1988).
This is satisfactory, providing we always remember which position each argument has, even after returning to the program after a gap of several months or years.
 

 

Using a structure - 1

If we want to help system builders and maintainers, we could add more information by holding parentage within a structured object. For instance:
     parents('n''pongo', jambo)
This would be inserted into the main structure as follows:  
 
     mammal(gorilla, 'n''gola', female, parents('n''pongo', jambo),
                      1988).
(What is the arity of this object?)                                  
 
 
Using a structure - 2

The previous example doesn't help people looking at the program to remember which is the mother and which is the father. It is important that the information is correct on this point: two dominant males placed in the same cage would probably fight. We could use structures as follows:  
 
     mother('n''pongo').
     father(jambo).
These could be inserted in one of two ways:  
 
     mammal(gorilla, 'n''gola', female, mother('n''pongo'), 
                     father(jambo), 1988).
or
     mammal(gorilla, 'n''gola', female, parents(mother('n''pongo'),
                     father(jambo)), 1988).
              
 
What is the correct way?

Unfortunately there is no rule for telling you which method to choose. You have to use your judgement and your experience. As general guidance, it is preferable to use structures within structures if it makes your program easier to use, and this is the subject of the next section.