Toy Prolog Examples
This page contains several example programs:
childOf(jay,john,jennie).
childOf(ron,john,jennie).
childOf(russ,john,jennie).
childOf(roger,john,jennie).
childOf(randy,jay,bev).
childOf(julie,jay,bev).
childOf(brad,jay,bev).
childOf(mark,jay,bev).
childOf(jen,jay,bev).
childOf(kevin,ron,peg).
childOf(jenifer,ron,peg).
childOf(michelle,russ,kathy).
childOf(brian,russ,kathy).
childOf(justin,roger,laura).
childOf(brent,roger,laura).
childOf(chelsea,roger,laura).
childOf(andrew,randy,penny).
childOf(anika,randy,penny).
childOf(jason,randy,penny).
childOf(leah,mike,julie).
childOf(braden,brad,laura2).
married(mark,chriss).
married(ron,dianna).
equal(X,X).
married(A,B) <= childOf(Child,A,B).
married(A,B) <= childOf(Child,B,A).
siblings(X,Y) <= childOf(X,Par1,Par2) and childOf(Y,Par1,Par2) and not equal(X,Y).
childOf(C,P) <= childOf(C,P,X).
childOf(C,P) <= childOf(C,X,P).
uncOf(U,N) <= childOf(N,Parent) and siblings(U,Parent).
? uncOf(X,randy).
Notice that this examples fails to get uncles and aunts by marriage.
We'll add them in in the next example.
childOf(jay,john,jennie).
childOf(ron,john,jennie).
childOf(russ,john,jennie).
childOf(roger,john,jennie).
childOf(randy,jay,bev).
childOf(julie,jay,bev).
childOf(brad,jay,bev).
childOf(mark,jay,bev).
childOf(jen,jay,bev).
childOf(kevin,ron,peg).
childOf(jenifer,ron,peg).
childOf(michelle,russ,kathy).
childOf(brian,russ,kathy).
childOf(justin,roger,laura).
childOf(brent,roger,laura).
childOf(chelsea,roger,laura).
childOf(andrew,randy,penny).
childOf(anika,randy,penny).
childOf(jason,randy,penny).
childOf(leah,mike,julie).
childOf(braden,brad,laura2).
married(mark,chriss).
married(ron,dianna).
equal(X,X).
married(A,B) <= childOf(Child,A,B).
married(A,B) <= childOf(Child,B,A).
siblings(X,Y) <= childOf(X,Par1,Par2) and childOf(Y,Par1,Par2) and not equal(X,Y).
childOf(C,P) <= childOf(C,P,X).
childOf(C,P) <= childOf(C,X,P).
dUncOf(U,N) <= childOf(N,Parent) and siblings(U,Parent).
mUncOf(U,N) <= dUncOf(X,N) and married(U,X).
uncOf(U,N) <= dUncOf(U,N).
uncOf(U,N) <= mUncOf(U,N).
? uncOf(X,andrew).
Oops. This still isn't correct. chriss should be
an aunt of andrew since she is married to
mark. The problem is that we have not made
married symetric.
childOf(jay,john,jennie).
childOf(ron,john,jennie).
childOf(russ,john,jennie).
childOf(roger,john,jennie).
childOf(randy,jay,bev).
childOf(julie,jay,bev).
childOf(brad,jay,bev).
childOf(mark,jay,bev).
childOf(jen,jay,bev).
childOf(kevin,ron,peg).
childOf(jenifer,ron,peg).
childOf(michelle,russ,kathy).
childOf(brian,russ,kathy).
childOf(justin,roger,laura).
childOf(brent,roger,laura).
childOf(chelsea,roger,laura).
childOf(andrew,randy,penny).
childOf(anika,randy,penny).
childOf(jason,randy,penny).
childOf(leah,mike,julie).
childOf(braden,brad,laura2).
married(mark,chriss).
married(ron,dianna).
equal(X,X).
married(A,B) <= childOf(Child,A,B).
{married(A,B) <= childOf(Child,B,A).} {don't need this now.}
married(A,B) <= married(B,A).
siblings(X,Y) <= childOf(X,Par1,Par2) and childOf(Y,Par1,Par2) and not equal(X,Y).
childOf(C,P) <= childOf(C,P,X).
childOf(C,P) <= childOf(C,X,P).
dUncOf(U,N) <= childOf(N,Parent) and siblings(U,Parent).
mUncOf(U,N) <= dUncOf(X,N) and married(X,U).
uncOf(U,N) <= dUncOf(U,N).
uncOf(U,N) <= mUncOf(U,N).
? uncOf(X,andrew).
Now what?!? We have an infinite loop! Why? The program
goes back and forth figuring out that, for example, mark and
chriss are married over and over.
(
married(mirk,chriss) => married(chriss,mark) => married(mark, chriss)
, etc.) Actually, it gets stuck even sooner when it is
figuring out about mike and julie.
One fix is given in the next attempt.
childOf(jay,john,jennie).
childOf(ron,john,jennie).
childOf(russ,john,jennie).
childOf(roger,john,jennie).
childOf(randy,jay,bev).
childOf(julie,jay,bev).
childOf(brad,jay,bev).
childOf(mark,jay,bev).
childOf(jen,jay,bev).
childOf(kevin,ron,peg).
childOf(jenifer,ron,peg).
childOf(michelle,russ,kathy).
childOf(brian,russ,kathy).
childOf(justin,roger,laura).
childOf(brent,roger,laura).
childOf(chelsea,roger,laura).
childOf(andrew,randy,penny).
childOf(anika,randy,penny).
childOf(jason,randy,penny).
childOf(leah,mike,julie).
childOf(braden,brad,laura2).
husbandwife(mark,chriss).
husbandwife(ron,dianna).
equal(X,X).
husbandwife(A,B) <= childOf(Child,A,B).
wifehusband(A,B) <= childOf(Child,B,A).
married(A,B) <= husbandwife(A,B).
married(A,B) <= wifehusband(A,B).
siblings(X,Y) <= childOf(X,Par1,Par2) and childOf(Y,Par1,Par2) and not equal(X,Y).
childOf(C,P) <= childOf(C,P,X).
childOf(C,P) <= childOf(C,X,P).
dUncOf(U,N) <= childOf(N,Parent) and siblings(U,Parent).
mUncOf(U,N) <= dUncOf(X,N) and married(X,U).
uncOf(U,N) <= dUncOf(U,N).
uncOf(U,N) <= mUncOf(U,N).
? uncOf(X,andrew).
witch(X) <= burns(X) and female(X).
burns(X) <= wooden(X).
wooden(X) <= floats(X).
floats(X) <= sameweight(duck, X).
female(girl).
sameweight(duck,girl). {by experiment}
? witch(girl).
{\fB After Monty Python (Sir Bedevere). \fP}
witch(X) <= burns(X) and female(X).
burns(X) <= wooden(X).
wooden(X) <= floats(X).
floats(X) <= sameweight(duck, X).
female(girl).
sameweight(duck,girl). {by experiment}
? witch(girl).
{\fB After Monty Python (Sir Bedevere). \fP}
The example above is based on Scene 5 of
Monty Python's Search for the Holy Grail .
If you are interested in the Monty Python sketch, here are links
to the text and
audio
Last modified:
Monday, 30-Apr-2001 03:11:49 EDT