How to convert logic rules to Prolog notation?
This is my assignment:
My attempt was:
a) If Fred is a father of Mike, then Fred is an ancestor of Mike.father( X, Y ). /* X is father of Y */
ancestor( fred, mike ) :- father( fred, mike ).
b) An animal is a mammal if it is a human or its parents were mammals.
parent( X, Y ). /* X is parent of Y */
human( X ). /* X is human */
mammal( X ) :- human( X ).
mammal( X ) :- parent( P, X ), mammal( P ).
c) You have attained the ultimate state if you are happy, healthy, and wise.
happy( X ). /* X is happy */
healthy( X ). /* X is healthy */
wise( X ). /* X is wise */
attain_ultimate_state( X ) :- happy( X ), healthy( X ), wise( X ).
d) Every dog likes all people.
dog( X ). /* X is a dog */
people( Y ). /* Y is human */
like( X, Y ) :- dog( X ), people( Y ).
e) The Lakers will win games 2, 3, 5, and 7, but lose the other 3 games in the series with New Orleans.
game( one ).
game( two ).
game( three ).
game( four ).
game( five ).
game( six ).
game( seven ).
win( laker, new_orleans, game( two ) ).
win( laker, new_orleans, game( three ) ).
win( laker, new_orleans, game( five ) ).
win( laker, new_orleans, game( seven ) ).
lose( laker, new_orleans, game( one ) ).
lose( laker, new_orleans, game( four ) ).
lose( laker, new_orleans, game( six ) ).
f) If P and Q, then R or S
and( X, Y ). /* X and Y */
or( X, Y ). /* X or Y */
imply( X, Y ). /* X imply Y */
or( r, s ) :- and( p, q ).
g) P implies Q is equivalent to the disjunction of not P with Q.
and( X, Y ). /* X and Y */
or( X, Y ). /* X or Y */
imply( X, Y ). /* X imply Y */
imply( p, q ) == or( not( p ), q ).
h) P exclusive_or Q is when P inclusive_or Q, but not (P and Q).
and( X, Y ). /* X and Y */
or( X, Y ). /* X or Y */
imply( X, Y ). /* X imply Y */
imply( p, q ) == or( not( p ), q ).
exclusive_or( X, Y ). /* X exclusive or Y */
inclusive_or( X, Y ). /* X inclusive or Y */
exclusive_or( p, q ) :- inclusive_or( p, q ), not( and( p, q ) ).
i) Jack is disappointed when it rains and any student misses class.
disappointed( X ). /* X is disappointed */
missed_class( X ). /* X missed class */
rain. /* it rains */
disappointed( jack ) :- rain, missed_class( _ ).
j) To be or not to be, that is the question.
to_be( X ).
question( X ) :- to_be( X ).
question( X ) :- not( to_be( X ) ).
We're using Concepts of Programming Languages by Robert W. Sebesta
as our textbook for this course. Unfortunately, there are very few examples about how to convert from logic rules to Prolog nota开发者_如何学Gotion in the book. Although I finished them all, most of my answer was guessing. So I wonder if someone could give me a hint, or suggestion on my work above. Any idea or feedback are welcome.
Thank you,
I can only imagine i am in your class as we have the same questions and book ;) Only posted 1 hour ago and already it shows up on google, lol
a) Same.
b) Currently this would be true as long as there is one parent that is a mammal, but because the problem says 'parents' and i 2nd guess everything the professor asks us, i specifically checked that there is a mother(M,X), and a father(F,X) and that M and F are mammals.
c) Same
d) more or less same: likes(dog,X):-person(X).
e) sighs This is what i showed to the professor and he didn't seem happy. He said i made it more complicated then it needed to be... but he unhelpful on what that meant.
game(1,newOrleans).
game(2,city1).
game(3,city2).
game(4,newOrleans).
game(5,cit2).
game(6,newOrleans).
game(7,city1).
win(X):-game(X,Y), not(city(Y)).
city(newOrleans).
f) You don't need imply there as :- stands for imply. I'm still working on f-h. I asked the professor help on this one and all i could get out of him is i didn't need p(x) and can just use p and q.
j) Same, but i have them on one line with ';' to do an or
question( X ) :- to_be(X);not(to_be(X)).
Which i'm not 100% is right. He might be the question needs to be 'to be' or 'not to be' question(tobe);question(nottobe).
quote(tobe).
quote(nottobe).
question(X):-quote(X).
UPDATE
Forgot 5i),this is what i have.
rain(tue).
skipped_class(tue,frank).
disappointed(jack,Day):-rain(Day), skipped_class(Day,Student).
jack will be disappointed on tue as it rain and frank skipped. Not sure if i've done this right but i'm going to stick with this.
UPDATE 2
Uh i just realized i can use true,false as values and not p. and q.
and(P,Q):-P,Q.
inclusive_or(P,Q):-P;Q.
exclusive_or(P,Q):-inclusive_or(P,Q),not(and(P,Q)).
Exclusive_or of P and Q will be true if its inclusive or (either P or Q must be true), and not both p and Q are true.
?-exclusive_or(true,true). false.
精彩评论