2
votes

I need to write in prolog these first order statements. How are they to be written? red(X) should return false, green(X) should return false, and green(X) or red(X) should return true

enter image description here

I have this code already:

% Assigning Facts

apple(a).
apple(b).
orange(c).
pear(d).
carrot(e).
onion(f).
pepper(g).
% Assigning Rules

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

fruit(X) :- apple(X).
fruit(X) :- orange(X).
fruit(X) :- pear(X).

vegetable(X) :- carrot(X).
vegetable(X) :- pepper(X).

tasty(X) :- fruit(X).
tasty(X) :- carrot(X).
tasty(X) :- not(onion(X)).

vegetable(X) :- not(tasty(X)).
1
tasty/1 and vegetable/1 will not work when the argument is unbound, therefor they can also not be used in the "exists" solution below. - Mostowski Collapse
Your first two lines say that every apple is both red and green. Probably not what you wanted. - firefrorefiddle

1 Answers

5
votes

What is the 'exists' method in Prolog that you already know of?

How about:

red(X).               % ∃x Red(x)
green(X).             % ∃x Green(x)
(red(X) ; green(X)).  % ∃x Red(x) v Green(x)

Prolog will enumerate bindings of X which satisfy each call, or fail trying. If you just wanted to test their existence and not collect bindings, you can ignore the variable and cut after the first binding, like so:

red(_), !.

You've also asked how to test if something is exclusively red or green, but not both. Try this:

((\+ red(X), green(X)) ; (red(X), \+ green(X)).  % ∃x Red(x) ⊻ Green(x)

Where means exclusive-or (XOR).