0
votes

I have to write the statement apple implies red or green in my prolog program. Here is a sample of the code I implemented:

:-dynamic(red/1).

:-dynamic(green/1).

 apple(a).

 apple(b).

 apple(A):-red(A);green(A).

My problem is that ?- red(a) and ?-green(a) should return false because this is not a given fact I place them as dynamic and now ?-red(a) and ?-green(a) do return false. However red(A);green(A). should return true because an apple is either red OR green

Thank for your responses: however me whole problem is that ?-red(a) and green(a) are both surpose to return false when ran separately. while ?-red(a);green(a) should return true. This is where I'm lost. If you have any suggestion on how to get there result the will be greatly welcomed.

1
Thank guys for you responses:Rico Man
@ChrisMartin: Your edit is not much of an improvement...false

1 Answers

0
votes

If you consider :- to mean "is implied by" or "is true if", then you can see that you have your final rule backwards (i.e. if something is red or green, then it is an apple). Further, Prolog does not let you draw an inference that includes an OR. Thus, the closest you can come is the following:

red(X) :- apple(X).
green(X) :- apple(X).

That is, something is red if it is an apple, and something is green if it is an apple. Yes, this does mean that you can prove that something is both red and green if it is an apple, but you don't HAVE too, and it will satisfy your requirement that red(A); green(A) succeeds (with A being something that is an apple). If you require that red(A) and green(A) can't both be true at the same time, that wil require a bit more work.