Reputation: 41
I am very new to PROLOG so it might be a very trivial question, but I absolutely have no idea how to solve it. There are 4 sentences I need to formulate into PROLOG code:
All hounds howl at night.
Anyone who has any cats will not have any mice.
Light sleepers do not have anything which howls at night.
John has either a cat or a hound.
I convert sentences to well-formed formula in first-order predicate calculus like that
- ∀x (HOUND(x) → HOWL(x))
- ∀x ∀y (HAVE(x,y) ∧ CAT (y) → ¬∃z (HAVE(x,z) ∧ MOUSE (z)))
- ∀x (Light_Sleeper(x) → ¬∃y (HAVE (x,y) ∧ HOWL(y)))
- ∃x (HAVE (John,x) ∧ (CAT(x) ∨ HOUND(x)))
Now I don't know how to write them in Prolog.
Also, how can I make query on them?
Upvotes: 4
Views: 2369
Reputation: 441
- All hounds howl at night.
hound(marshall).
hound(rubble).
howls_at_night(X) :-
hound(X).
%% ?- howls_at_night(everest).
%% false.
%% ?- howls_at_night(rubble).
%% true.
%% ?- howls_at_night(Name).
%% Name = marshall
%% Name = rubble.
- Anyone who has any cats will not have any mice.
cat(tom).
mice(jerry).
has(mammy, tom).
wont_have_mice(X) :- has(X, Y), cat(Y).
may_have_mice(X) :- has(X, Y) -> \+ cat(Y) ; true.
%% ?- may_have_mice(john).
%% true.
%% ?- wont_have_mice(john).
%% false.
%% ?- wont_have_mice(mammy).
%% true.
%% ?- may_have_mice(mammy).
%% false.
- Light sleepers do not have anything which howls at night.
has(ryder, marshall).
has(ryder, rubble).
could_be_a_lightsleeper(X) :- has(X, Y) -> \+ howls_at_night(Y) ; true.
is_not_a_lightsleeper(X) :- has(X, Y), howls_at_night(Y).
%% ?- could_be_a_lightsleeper(max).
%% true.
%% ?- could_be_a_lightsleeper(ryder).
%% false.
%% ?- could_be_a_lightsleeper(Name).
%% Name = mammy.
%% ?- is_not_a_lightsleeper(max).
%% false.
%% ?- is_not_a_lightsleeper(mammy).
%% false.
%% ?- is_not_a_lightsleeper(max).
%% false.
%% ?- is_not_a_lightsleeper(Name).
%% Name = ryder.
- John has either a cat or a hound.
has_cat_or_hound(X, Y) :- has(X, Y), (cat(Y) ; hound(Y)).
john_has(Y) :- cat(Y) ; hound(Y).
Upvotes: 1