I'm trying to solve the first puzzle in Smullyan's To Mock a Mockingbird using clojure.core.logic, not because it is particularly hard, but rather as an exercise. The puzzle states that there's a garden with three colors of flowers: red, yellow, and blue. Each color is present at least once and no matter what three flowers you pick there will be a red and a yellow one among them. Question: is the third necessarily blue?
The basic skeleton of the logic code is quite straightforward:
(run 5 [flowers]
(counto flowers 3)
(containso flowers [:red :yellow])
(fresh [garden]
(containso garden [:red :yellow :blue])
(containso garden flowers)
(forall [flower-selection] (...))))
counto
and containso
are manually implemented and do the obvious thing. I'm new to this so there might be existing library support that I'm missing. The important line is the one beginning with forall
. forall
is basically what I wish would exist, but can't seem to find. The only construct I'm aware of that could go in this place is fresh
. But fresh
essentially performs existential quantification ∃. What I want here is universal quantification ∀.
I'm not interested in a garden for which it is possible to select three flowers which contain a red and a yellow one. I'm interested in a garden which necessarily leads to such a choice.