## Prolog Programming Techniques.

Family relationships can be used to illustrate Prolog's ability to define basic relations such as parenthood and derived relations such as grand-parent-hood. A child has two parents - a mother and a father. A parent is either a mother or a father. A grand-parent of a child is a parent of a parent of the child. These rules can easily be expressed in Prolog.

```parents(william, diana,     charles).
parents(henry,   diana,     charles).
parents(charles, elizabeth, philip).
parents(diana,   frances,   edward).

parent(C,M) <= parents(C,M,D).
parent(C,D) <= parents(C,M,D).

grandparent(C,GP) <= parent(C,P) and parent(P,GP).

?grandparent(william, Who).

{\fB Grand-Parent Relation. \fP}
```

Note that a parent is either a mother or a father. This is expressed by two rules in Prolog. A mother is a parent and it is also true that a father is a parent.

The query `? grandparent(william, Who)' asks if william has a grand-parent and if so who? William has four grand-parents and the simple Prolog interpreter described later finds them all:

```
grandparen(william, frances) yes
grandparen(william, edward) yes
grandparen(william, elizabeth) yes
grandparen(william, philip) yes

--- Solutions to ? grandparent(william, Who). ---

```

[Previous Page] [Next Page] [Index] © L. Allison, Dept. Computer Science, Monash University