0
votes

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.

1
How you formulate things depends on the context. Furthermore Prolog only supports horn clauses. Although you can model everything in it, it sometimes requires some creativity. - Willem Van Onsem
Yeah I should maybe at that I want to answer the question Who hit Tom?. - Peter
This is not difficult i give you solutions for two and try to do your homework for the next three self: owns(bill,dog). likes(X,animals):- owns(X,dog). - Luai Ghunim
@LuaiGhunim thanks for your help. I think then the third one should look like this: not(hit(X)):-likes(X,animals). or hit(X):-not(likes(X,animals)). - Peter
hit(X) , what does X hit? cannothit(X,animal):- likes(X,animals). or hit(X,animal):- \+ likes(X,animal). . This is \+ used for inverting values. - Luai Ghunim

1 Answers

1
votes

"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''