% some of the prolog things % version 1 of tom and jerry world isa(tom, cat). isa(jerry, mouse). isa(jerrys-nephew, mouse). isa(spike, dog). would-eat(X, Y) :- isa(X, cat), isa(Y, mouse). would-chase(X, Y) :- would-eat(X, Y). would-chase(X, Y) :- dog(X), cat(Y). hates(X, Y) :- mould-eat(Y, X). likes(X, Y) :- hates(X, Z), would-chase(Z, Y). % version 2 of tom and jerry world isa(tom, cat). isa(jerry, mouse). isa(jerrys-nephew, mouse). isa(spike, dog). isa(timmy, tiger). would-eat(X, Y) :- isa(X, cat), isa(Y, mouse). would-eat(X, Y) :- isa(X, tiger). would-chase(X, Y) :- would-eat(X, Y). would-chase(X, Y) :- isa(X, dog), isa(Y, cat). hates(X, Y) :- would-eat(Y, X). likes(X, Y) :- hates(X, Z), would-chase(Y, Z). % version 3 of tom and jerry world fact(tom, isa, cat). fact(jerry, isa, mouse). fact(jerrys-nephew, isa, mouse). fact(spike, isa, dog). fact(timmy, isa, tiger). fact(X, eats, Y) :- fact(X, isa, cat), fact(Y, isa, mouse). fact(X, eats, Y) :- fact(X, isa, tiger). fact(X, chases, Y) :- fact(X, eats, Y). fact(X, chases, Y) :- fact(X, isa, dog), fact(Y, isa, cat). fact(X, hates, Y) :- fact(Y, eats, X). fact(X, likes, Y) :- fact(X, hates, Z), fact(Y, chases, Z). % working with lists (len 1 is just setting the scenes) length, append, reverse len1([], 0). len1([A], 1). len1([A, B], 2). len1([A, B, C], 3). len([], 0). len([H | T], N) :- len(T, X), N is X + 1. app([], A, A). app([H | T], A, [H | B]) :- app(T, A, B). rev([], A, A). rev([H | T], A, B) :- rev(T, [H | A], B). rev(A, B) :- rev(A, [], B). % the simpsons father(homer, bart). father(homer, lisa). father(abe, homer). father(abe, herb). mother(mona, homer). mother(marge, bart). mother(marge, lisa). married(homer, marge). married(mona, abe). married(A, B) :- married(B, A). sibling(A, B) :- father(C, A), father(C, B). sibling(A, B) :- mother(C, A), mother(C, B). parent(A, B) :- father(A, B). parent(A, B) :- mother(A, B). child(A, B) :- parent(B, A). ancestor(A, B) :- parent(A, B). ancestor(A, B) :- parent(A, C), ancestor(C, B). uncle(A, B) :- sibling(A, C), parent(C, B).