oprator "or" in prolog

145 views
Skip to first unread message

fahime....@gmail.com

unread,
Nov 25, 2016, 8:30:47 AM11/25/16
to swi-p...@googlegroups.com

     Jack owns a dog.

      Every dog owner is an animal lover.

      No animal lover kills an animal.

       Either Jack or Curiosity killed the cat, who is named Tuna.

      Did Curiosity kill the cat?

      Dog is an animal.

      Cat is an animal.

      ____________________________________

        In prolog?

       own(jack,dog).

       animallover(X):-own(X,dog).

       not(kill(X,Y)):-animallover(X),animal(Y).

       kill(curi,tuna);kill(jack,tuna).

       animal(dog).

       animal(cat).

       cat(tuna).

        ___________

         Find my mistakes,plz

         Thanks.




 

Martin Sondergaard

unread,
Nov 25, 2016, 12:21:08 PM11/25/16
to swi-p...@googlegroups.com

> Either Jack or Curiosity killed Tuna the cat.


You could represent that with these two rules :

kill(jack, tuna) :- not kill(curi, tuna).
kill(curi, tuna) :- not kill(jack, tuna).

This would cause an infinite loop though, this needs fixing.
Maybe like this:

kill(jack, tuna) :- not kill(curi, tuna).
kill(curi, tuna) :- not kill_fact(curi, tuna).

kill_fact(cat, mouse).
kill_fact(dog, rabbit).
kill_fact(tuna, rat).

Would that be a good solution?
Can anyone suggest a better way to represent "or"?

--
Martin Sondergaard.


kint...@gmail.com

unread,
Nov 25, 2016, 7:43:45 PM11/25/16
to SWI-Prolog
~~~
#!/usr/bin/swipl -l

/*

Every dog owner is an animal lover.
No animal lover kills an animal.
Either Jack or Curiosity killed Tuna the cat.

Did Curiosity kill the cat?
*/


%// Jack owns a dog. //%

definite_owner_of_dogs('jack') .

%// Every dog owner is an animal lover. //%

definite_lover_of_animals(_who_) :- definite_owner_of_dogs(_who_) .

%// No animal lover kills an animal. //%

possible_killer_of_animals(_who_) :- \+ definite_lover_of_animals(_who_) .

%// Either Jack or Curiosity killed Tuna the cat. //%

possible_killer_of_tuna('jack') .

possible_killer_of_tuna('curiosity') .

%// Did Curiosity kill the cat? //%

end_of_file .

?-
possible_killer_of_tuna(_who_)
, possible_killer_of_animals(_who_)
, _killer_of_tuna_ = _who_
.

/*
_who_ = _killer_of_tuna_, _killer_of_tuna_ = curiosity.
*/

~~~
kintalken
~~~
Message has been deleted

fahime....@gmail.com

unread,
Nov 26, 2016, 8:40:52 PM11/26/16
to swi-p...@googlegroups.com
Thank you very much, Mr. Martin Sondergaard and Mr kintalken
As you can see, the line 3, after the displacement, Prolog is false to me.
It seems that, should the application be written in a way that 'tuna' a cat and the cat is an animal.
If it is possible you explain me more. Thank

definite_owner_of_dogs('jack') .
definite_lover_of_animals(_who_) :- definite_owner_of_dogs(_who_) .
possible_killer_of_animals(_who_) :- not(definite_lover_of_animals(_who_)) .
possible_killer_of_tuna('jack') .
possible_killer_of_tuna('curiosity') .


1 ?- possible_killer_of_tuna(_who_)
, possible_killer_of_animals(_who_)
|    , _killer_of_tuna_ = _who_.
_who_ = _killer_of_tuna_, _killer_of_tuna_ = curiosity.

2 ?- possible_killer_of_tuna(_who_),possible_killer_of_animals(_who_).
_who_ = curiosity.

3 ?- possible_killer_of_animals(_who_),possible_killer_of_tuna(_who_).
false.

4 ?- 





On Friday, November 25, 2016 at 5:00:47 PM UTC+3:30, fahime....@gmail.com wrote:

---- Jack owns a dog.

•     Every dog owner is an animal lover.

      No animal lover kills an animal.

       Either Jack or Curiosity killed Tuna the cat.

      Did Curiosity kill the cat?

      ____________________________________

        In prolog?

       own(jack,dog).

       animallover(X):-own(X,dog).

       not(kill(X,Y)):-animallover(X),animal(Y).

       kill(curi,tuna);kill(jack,tuna).

       animal(dog).

       animal(cat).

       cat(tuna).

        ___________

         Find my mistakes,plz

t         Thanks.




 

kint...@gmail.com

unread,
Nov 28, 2016, 1:31:34 AM11/28/16
to SWI-Prolog
~~~
<blockquote>

As you can see, the line 3, after the displacement, Prolog is false to me.
2 ?- possible_killer_of_tuna(_who_),possible_killer_of_animals(_who_).
_who_ = curiosity.
3 ?- possible_killer_of_animals(_who_),possible_killer_of_tuna(_who_).
false.
</blockquote>

good question !
You want the solution to be <i> steadfast </i> .

<script>
#!/usr/bin/swipl -l

person(person('jack')) .
person(person('curiosity')) .
definite_owner_of_dogs(person('jack')) .
definite_lover_of_animals(_who_) :- definite_owner_of_dogs(_who_) .
%old%possible_killer_of_animals(_who_) :- not(definite_lover_of_animals(_who_)) .
possible_killer_of_animals(_who_) :- person(_who_), not(definite_lover_of_animals(_who_)) .
possible_killer_of_tuna(person('jack')) .
possible_killer_of_tuna(person('curiosity')) .

end_of_file .

?- possible_killer_of_tuna(_who_) , possible_killer_of_animals(_who_) .
_who_ = person(curiosity).

?- possible_killer_of_animals(_who_) , possible_killer_of_tuna(_who_) .
_who_ = person(curiosity).
</script>
~~~
kintalken
~~~

kint...@gmail.com

unread,
Nov 28, 2016, 2:01:00 AM11/28/16
to SWI-Prolog
<blockquote>

It seems that, should the application be written in a way that 'tuna' a cat and the cat is an animal.
</blockquote>

<script>
/*
- there is a dog that is an animal named 'dolphin' .
- there is a cat that is an animal named 'tuna' .
Jack owns the dog.

Every dog owner is an animal lover.
No animal lover kills an animal.
Either Jack or Curiosity killed Tuna the cat.
Did Curiosity kill the cat?
*/

#!/usr/bin/swipl -l

animal(animal(dog,'dolphin')) .
animal(animal(cat,'tuna')) .


person(person('jack')) .
person(person('curiosity')) .

owned_owner(animal(dog,'dolphin'),person('jack')) .

lover_of_animals(_person_) :- owned_owner(animal(dog,_),_person_) .

killer_of_animals(_person_) :- person(_person_), not(lover_of_animals(_person_)) .

possible_killer(person('jack'),animal(cat,'tuna')) .
possible_killer(person('curiosity'),animal(cat,'tuna')) .

end_of_file .

?- possible_killer(_person_,animal(cat,'tuna')) , killer_of_animals(_person_) .
_person_ = person(curiosity).

?- killer_of_animals(_person_) , possible_killer(_person_,animal(cat,'tuna')) .
_person_ = person(curiosity).

</script>


Reply all
Reply to author
Forward
0 new messages