Reputation: 6434
I've set out to solve a logical problem found in a kid game just to prove myself that some problems has more than one solution.
The game is as follow, you have multiple kind of candy, and with some partial instruction you need to find the correct set of candy.
Candies are defined by kind and color.
Insctuction are on the following form
Here are my facts
bear(bear_yellow).
bear(bear_green).
bear(bear_orange).
bear(bear_red).
licorice(licorice_red).
cake(cake_blue).
cake(cake_red).
cake(cake_green).
cake(cake_yellow).
fish(fish_yellow).
fish(fish_orange).
fish(fish_red).
fish(fish_green).
jelly_bean(jelly_bean_red).
jelly_bean(jelly_bean_green).
jelly_bean(jelly_bean_pink).
jelly_bean(jelly_bean_purple).
jelly_bean(jelly_bean_blue).
blue(cake_blue).
blue(jelly_bean_fish).
yellow(bear_yellow).
yellow(cake_yellow).
yellow(fish_yellow).
red(bear_red).
red(licorice_red).
red(cake_red).
red(fish_red).
red(jelly_bean_red).
green(bear_green).
green(fish_green).
green(jelly_bean_green).
orange(bear_orange).
orange(fish_orange).
pink(jelly_bean_pink).
purple(jelly_bean_purple).
is_positive(NUMBER) :- NUMBER > 0.
contains_at_least_one(CANDYS, TEST) :-
count(CANDYS, TEST, NB), is_positive(NB).
count([], _, 0).
count([X|TAIL], F, RES) :- call(F,X) -> count(TAIL, F, ACC), RES is ACC+1; count(TAIL, F, RES).
contains_at_least_one_list(_, []) :- true.
contains_at_least_one_list(CANDYS, [TEST|TEST_TAIL]) :-
contains_at_least_one(CANDYS, TEST),
contains_at_least_one_list(CANDYS, TEST_TAIL).
I think there is something fishy with the predicate contains_at_least_one_list
but i can't found what's the problem.
With the previous definitions here is a question that is not correct
contains_at_least_one_list([bear_red, fish_orange], [red, orange])
%which return true and false
Here is a print screen from the trace
I'didn't practice prolog for 10 years, and it's obvious to me that that the question is very vague, if you can come up with a better question or even a link to a better stated question feel free
Upvotes: 0
Views: 618
Reputation: 9378
Many Prolog predicates will "return true and false". For example:
?- member(a, [a, b, c]).
true ;
false.
The false
doesn't mean that a
is not a member of the list [a, b, c]
. It means that after Prolog derived once that a
is a member of [a, b, c]
(that's the true
answer), you asked it if there is another way of deriving the fact that a
is a member of [a, b, c]
. There isn't, so the second answer is false
. But this second answer is independent of the first one. It does not override or invalidate it. a
is a member of [a, b, c]
.
Some queries can succeed several times:
?- member(a, [a, b, a, c]).
true ;
true ;
false.
Here there are two occurrences of a
in the list, so if you ask for an alternative after the first answer, Prolog will be able to find a second one. But not a third one, so the third answer is false
. Again, this is nothing to worry about. A final false
after a success doesn't mean "there is no successful answer". It just means "there is no other successful answer".
Upvotes: 1