Copyright 2000 by Neal R. Wagner.
You should refer to the writeup about education for several concepts important here. For this discussion, data (or ``raw'' data) is computer data without any concern for its meaning. Information is data with a meaning, data that means something. A data base is a collection of information that allows one to extract meaningful data in many different ways. A knowledge base, as represented by the prolog program below, has even more: in addition to the information (called facts in prolog), and the ability to extract information, there is also the ability to deduce new facts using prolog rules.
Here are some facts for a simple prolog knowledge base. It gives facts about a small family tree, with part of four generations. Here is a graphical representation of the tree. The two parents are above the children, and a line connects parents, while a circle and line descends to each child. Males are given in all capitol letters. (The graph is given to help orient you to the example, but it is not needed in the remainder, since all aspects of this diagram are implicit in the facts and rules given later.)
GEN 1: ALBERTUS lydia ALBERT may
| | | |
+--o------+ +----o---+
| |
GEN 2: RALPH alberta JOHN cleo
| | | |
+---o--------o-------o---+ +--o-------o--------o--+
| | | | | |
GEN 3: joan WAYNE martha NEAL debbie elizabeth SEAN kathy
| | | | | |
+-0---+ +-o----o-----o--+ +-o-----o-+
| | | | | |
GEN 4: amy NATE IAN bethany KYLE DANIEL
In the example below, a fact like "male(neal)."
means that some object "neal" has property "male". (In fact,
we mean that the person "neal" is a male individual.)
Similarly, "parent(neal, nate)." means that "neal"
is the parent of "nate". (Together, we know that these two
facts mean that "neal" is the father of "nate", but that will
come later.) There are also facts stating that persons
are female, and that some people are married to others.
Here is the full list of facts:
FACTS:
male(albertus). male(albert). male(ralph). male(neal). male(wayne). male(nate). male(ian). male(john). male(sean). male(kyle). male(daniel). male(shane). female(lydia). female(may). female(alberta). female(joan). female(amy). female(bethany). female(debbie). female(cleo). female(elizabeth). female(kathy). female(martha). parent(albertus, ralph). parent(albert, alberta). parent(ralph, neal). parent(ralph, wayne). parent(ralph, martha). parent(wayne, amy). parent(neal, nate). parent(neal, ian). parent(neal, bethany). parent(john, debbie). parent(john, sean). parent(john, elizabeth). parent(sean, kyle). parent(sean, daniel). parent(lydia, ralph). parent(may, alberta). parent(alberta, neal). parent(alberta, wayne). parent(alberta, martha). parent(debbie, nate). parent(debbie, ian). parent(debbie, bethany). parent(cleo, debbie). parent(cleo, sean). parent(cleo, elizabeth). parent(kathy, kyle). parent(kathy, daniel). parent(joan, amy). married(neal, debbie). married(ralph, alberta). married(albertus, lydia). married(albert, may). married(sean, kathy). married(john, cleo). married(debbie,neal). married(alberta, ralph). married(lydia, albertus). married(may, albert). married(kathy, sean). married(cleo, john).
For example, the rule father(X, Y) :- male(X), parent(X, Y). means that X is the father of Y in case X is male and X is the parent of Y.
Some rules are very simple, as child(Y, X) :- parent(X, Y). which says that Y is the child of X in case X is the parent of Y. Other rules get complicated, like the rule for sibling(X, Y), where X and Y are siblings if the have the same father Z and the same mother W, and are not the same person. (Without the last, a person would be a sibling of themselves.)
Another kind of complicated rule is the rule for ancestor(X,Y). Here there are two parts: X is the ancestor of Y in case X is the parent of Y, and in another way, X is the ancestor of Y in case X is the parent of someone someone Z, and Z is the ancestor of Y. (When there are two rules like this, you can connect then with "or".)
Finally the rule for auntoruncle(X, W) is complicated because an uncle (or an aunt) can be a blood relative, or can be an uncle or aunt through a marriage.
RULES:
spouse(X, Y) :- married(X, Y).
husband(X, Y) :- male(X), married(X, Y).
wife(X, Y) :- female(X), married(X, Y).
father(X, Y) :- male(X), parent(X, Y).
mother(X, Y) :- female(X), parent(X, Y).
sibling(X, Y) :- father(Z, X), father(Z, Y),
mother(W, X), mother(W, Y), not(X = Y).
brother(X, Y) :- male(X), sibling(X, Y).
sister(X, Y) :- female(X), sibling(X, Y).
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
grandfather(X, Z) :- male(X), grandparent(X, Z).
grandmother(X, Z) :- female(X), grandparent(X, Z).
grandchild(X, Z) :- grandparent(Z, X).
grandson(X, Z) :- male(X), grandchild(X, Z).
granddaughter(X, Z) :- female(X), grandchild(X, Z).
ancestor(X,Y) :- parent(X,Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
child(Y, X) :- parent(X, Y).
son(Y, X) :- male(Y), child(Y, X).
daughter(Y, X) :- female(Y), child(Y, X).
descendent(Y, X) :- ancestor(X, Y).
auntoruncle(X, W) :- sibling(X, Y), parent(Y, W).
auntoruncle(X, Z) :- married(X,Y), sibling(Y,W), parent(W,Z).
uncle(X, W) :- male(X), auntoruncle(X, W).
aunt(X, W) :- female(X), auntoruncle(X, W).
cousin(X, Y) :- parent(Z, X), auntoruncle(Z, Y).
nieceornephew(X, Y) :- parent(Z, X), sibling(Z, Y).
nephew(X, Y) :- male(X), nieceornephew(X, Y).
niece(X, Y) :- female(X), nieceornephew(X, Y).
greatgrandparent(X, Z) :- parent(X, Y), grandparent(Y, Z).
greatgrandfather(X, Z) :- male(X), greatgrandparent(X, Z).
greatgrandmother(X, Z) :- female(X), greatgrandparent(X, Z).
greatgrandchild(X, Z) :- child(X, Y), grandchild(Y, Z).
greatgrandson(X, Z) :- male(X), greatgrandchild(X, Z).
greatgranddaughter(X, Z) :- female(X), greatgrandchild(X, Z).
parentinlaw(X, Y) :- married(Y, Z), parent(X, Z).
fatherinlaw(X, Y) :- male(X), parentinlaw(X, Y).
motherinlaw(X, Y) :- female(X), parentinlaw(X, Y).
siblinginlaw(X, Y) :- married(Y, Z), sibling(X, Z).
brotherinlaw(X, Y) :- male(X), siblinginlaw(X, Y).
sisterinlaw(X, Y) :- female(X), siblinginlaw(X, Y).
childinlaw(X, Y) :- married(X, Z), child(Z, Y).
soninlaw(X, Y) :- male(X), childinlaw(X, Y).
daughterinlaw(X, Y) :- female(X), childinlaw(X, Y).
Finally, one can deduce new specific facts from the given collection
of facts and rules. For this reason the system is called a
knowledge base. After the prolog system "absorbs"
the given facts and rules, it presents the user with a
prompt: ?- . One can type in a query and see what
matches the system can achieve.
(Below, boldface shows what you type.) For example:
?- daughter(X,Y).
X = alberta
Y = albert ;
X = alberta
Y = may ;
X = amy
Y = wayne ;
X = amy
Y = joan
(plus many more daughters)
Similarly, albert has two greatgranddaughters, as is shown by
?- greatgranddaughter(X, albert).
X = amy ;
X = bethany ;
No
Here the "no" means that albert has no more greatgranddaughters
(in this restricted family tree). Thus we see that the following
facts are true:
greatgranddaughter(amy, albert).
greatgranddaughter(bethany, albert).
Here are two more queries and their results:
?- soninlaw(X,Y).
X = ralph
Y = albert ;
X = ralph
Y = may ;
X = neal
Y = john ;
X = neal
Y = cleo ;
No
?- aunt(debbie, X).
X = kyle ;
X = daniel ;
X = amy ;
No