happy(X) :- rich(X), famous(X).
X is rich
X is famous. What about logical-or, ∨, also called
disjunction? If we want to say that
rich or if
famous, we have two possibilities:
;operator (pronounced "or").
Option 1:happy1(X) :- rich(X). happy1(X) :- famous(X).
Option 2:happy2(X) :- rich(X) ; famous(X).
Sometimes it is necessary, or at least advisable,
to wrap parentheses
( ) around a
disjunction in order to make clear what how the
conjunctions and disjunctions interact. If you
leave out the parentheses, Prolog has its own
rules about the precedence
of logical-and and logical-or, which might or might
not correspond to what you intend. If you use
parentheses, the things inside the parentheses are done
first, so everything is crystal clear.
Option 3:happy3(X) :- attractive(X), ( rich(X) ; famous(X) ).
Xis attractive, and
Xis either rich or famous:
happy3(X) ⇐ attractive(X) ∧ (rich(X) ∨ famous(X)).*
* The Prolog Dictionary wishes to acknowledge that a range of world religions and philosophies would not agree that the criteria specified for
happy3are either necessary nor sufficient for happiness. The definitions given in this article, are, therefore, purely intended as Prolog programming examples, and should not be taken as a practical guide to life. Thank you.