Reputation: 371
I need your help for a simple PROLOG program. I am very new to PROLOG so it might be a very trivial question, but I absolutely have no idea how to solve it.
There are 5 sentences I need to formulate into PROLOG code:
-Bill owns a dog.
-Every dogowner likes animals.
-Every person who likes animals cannot hit an animal.
-Bill or Bull hit a cat whose name is Tom.
-Every cat is an animal.
I think I have the first 3 sentences:
dogowner(bill).
lovesanimal(X):- dogowner(X).
not(hitting(X,animal(Y))):-lovesanimal(X).
The last one also is not a problem. But how do I formulate the 4th?
Thank you for your help.
Upvotes: 0
Views: 124
Reputation: 26
"Every cat is an animal":
animal(X) :- cat(X).
"Every person who likes animals cannot hit an animal": I think the use of not(hitting(X,animal(Y))) may be confused ... I prefere use:
can(hitting(X, Y)) :- person(X), not(lovesanimal(X)), animal(Y).
In other hand, you must say the program several 'facts' (that aren't explicited in your premises)
Tom is a cat:
cat(tom).
Bill and Bull are persons:
person(Bill). person(Bull).
Finally, the predicate ';' use in pre-fixed notation:
or(hits(bill, tom), hits(bull, tom)).
However, such programe don't tell you who (Bill or Bull), actually hits Tom. You need a clause like this:
actually_hits(X, Y) :- can(hitting(X, Y)), (or(hits(X,Y),); or(, hits(X,Y)).
Finally, you can wish that the programe be more general; so, you can reemplace 'lovesanimal(X)' by 'likes(X, Y)':
likes(X, Y) :- dogowner(X), animal(Y).
And, of course, the rules must be reformulated as:
can(hitting(X, Y)) :- person(X), not(likes(X, Y)).
which say that "Every person who like something can not hit this thing''
Upvotes: 1