Reputation:
I don't know how to write a Prolog program for the following scenario.
1. If any two person having same hobby then they like each other.
2. Every gardener likes the Sun.
I did this but I don't know whether it is correct or not.
like(gardener,sun).
Please help me to solve it.
Upvotes: 0
Views: 157
Reputation: 71119
Prolog rules follow the "reversed-IF" template:
Head :- Goal1, ..., GoalN.
means (roughly), "Head
holds if Goal1
, ..., GoalN
all hold".
Put the other way around it means, "if Goal1
, ..., GoalN
all hold, then Head
also holds".
This fits exactly your first sentence, thus it can be encoded as a rule:
likes(A, B) :- % Head :-
hobby( A, HobbyA), % Goal1,
hobby( B, HobbyB), % Goal2,
same( HobbyA, HobbyB), % Goal3,
dif( A, B). % Goal4.
% different persons, not the same one
The second sentence too fits the same template:
likes(A, sun) :-
isA(A, gardner).
With the most natural encoding of isA( X, Y)
as simply a unification X = Y
, this becomes equivalent to the fact that you wrote. Facts are rules with no body.
Upvotes: 3