Prolog Tutorial 4: Structures

James Power, 1997.


Much of the information that we want to represent in a program is compound, that is, it consists of entities which have a number of different attributes.

For example, the person entity might have a number of attributes such as age, height, weight, and so on.

In languages like C we represent this information using structs; in an OO language we'd probably use a class. In Prolog we use structures.

In general, a structure can appear in a clause anywhere a variable or constant would appear: it is another form of term. Thus, using a structure in Prolog corresponds to an instance of a class in an OO language.

As with all other terms we have used in Prolog, structures do not need to be declared; we can simply use them wherever we want.

The General Form of a Structure

A structure has the form:
  structure-name ( attribute, ..., attribute )

Note

Note that structures look like predicates, but they work differently. Remember: predicates represent relationships; structures (and other terms) represent objects.

Prolog tells the difference between predicates and structures only by seeing where they appear in a clause. Structures (just like any other terms) never appear on their own: they must always appear as the argument to some predicate.

Arithmetic "Functions" and Structures

You might have noticed that Prolog does not treat structures any differently during unification from the arithmetic functions (like log or cos) that we met in the last tutorial. This is due to the declarative nature of Prolog: log(10) represents an object, not a computation, and thus can be treated like any other object.

This represents an important difference from imperative languages: in Prolog it is important to think of terms like log(10) as structures rather than function-calls when it comes to unification.


A simple example of using structures

Suppose we want to represent cars with attributes make, age, price.

We might use a three-place structure called car; e.g. car(ford, 3, 5000) might represent a 3-year-old Ford selling for $5,000.

Structures of this type could be used in clauses such as:

And we can pose queries like: "What kind of Ford does Mick have?"

Query: has(mick, car(ford, Age, Price))
Answer: Age=2, Price=2000

If we only want to get information about some fields we can use Prolog's "don't care" marker - the underscore character - to indicate this.

The underscore "_" has indicated to Prolog that we aren't fussy about what matches these fields (and that we don't want to know what does).

If we wanted to know what make of car sold for under 5000, we might ask:


Exercises

  1. Type the "car" example above into a Prolog program (called car.pl; try some queries to make sure you understand what is happening. Also, try adding a "colour" field to the structure.

  2. Data on each employee of a company consists of the following: employee's name, department in which s/he works, her/his position in the department (secretary, head, accountant etc.), number of years of service, basic salary, and the name of their immediate boss. The company director is his/her own boss!

    Write a Prolog database containing the employees' information (make up 5 or 6 entries) - this should be a list of facts containing "employee-details" structures. Now, based on this, make up some rules to answer the following: (the name of the rule, along with its arity is given in each case)


Written by James Power
Released under the GNU Free Documentation License
Last revised: 22 Oct 2006