Here is an example of a Prolog program that defines some facts and rules related to family relationships:
% facts
father(john, peter).
father(john, mary).
mother(jane, peter).
mother(jane, mary).
married(john, jane).
% rules
parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X = Y.
spouse(X, Y) :- married(X, Y).
spouse(X, Y) :- married(Y, X).
This program defines the following relationships:
father(john, peter)
means that John is Peter's father.father(john, mary)
means that John is Mary's father.mother(jane, peter)
means that Jane is Peter's mother.mother(jane, mary)
means that Jane is Mary's mother.married(john, jane)
means that John is married to Jane.
The program also defines some rules:
parent(X, Y)
means that X is the parent of Y if X is Y's father or X is Y's mother.sibling(X, Y)
means that X and Y are siblings if they have the same parent.spouse(X, Y)
means that X and Y are spouses if X is married to Y or Y is married to X.
With this program, we can ask Prolog questions like:
- Who are Peter's parents?
parent(X, peter)
would returnX = john
andX = jane
. - Who are Mary's siblings?
sibling(X, mary)
would returnX = peter
andX = mary
(since Mary is her own sibling). - Who is John's spouse?
spouse(john, X)
would returnX = jane
.