It was surprising for me when I leaned the meaning of forach
goal e.g. foreach(between(1, 3, X), dif(X, Y))
, which is expanded at runtime to the goal
`( dif(1, Y), dif(2, Y), dif(3, Y) ).`
However, after a while, I understood that this is natural from logics. Suppose D is a finite domain, e.g, D=\{1,2,3\}. Then quantifiers can be eliminated into propositional conjunction or disjunction by following rules.
- \forall x\, p(x) \equiv p(1) \land p(2) \land p(3)
- \exists x\, p(x) \equiv p(1) \lor p(2) \lor p(3)
That is, in a finite domain, a first-order formula is a disguised form of a proposition. With this observation in mind, the meaning foreach(p(x), q(x, y))
is clear if it is written
\exists y \forall x ( p(x)\to q(x,y) )
which reads there exists y such that q(x,y) holds for each x satisfying p(x). Also it is read: find y such that q(x,y) holds for all x satisying p(x). I am posting a trivial observation, and I am afraid I am missing such quantifier scope aware description of foreach
.
Similary \forall x \exists y ( p(x)\to q(x,y) ) is close to a logical reading for forall(p(x), q(x, y)).
BTW. as English phrases, it is not clear for me the difference between “for all” and “for each”.
EDIT 2022.2.13.
Based on the observation above, I wrote a model finder for FOL closed formulae with finite domain. Though this is a toy codes it might be useful for instructive purpose on quantifiers of logics. I used my ZDD library for simplicity and my familiarity. Sample
is taken: “Every one likes some one.”
% ?- nnf_model([a,b,c], some(y, all(x, like(x, y)))).
%@ [like(a,c),like(b,c),like(c,c)]
%@ [like(a,b),like(b,b),like(c,b)]
%@ [like(a,a),like(b,a),like(c,a)]
%@ true.
% ?- nnf_model([a,b,c], -some(y, all(x, like(x, y)))).
%@ [-like(c,a),-like(c,b),-like(c,c)]
%@ [-like(c,a),-like(c,b),-like(b,c)]
% ...
% ?- nnf_model([a,b,c], all(x, some(y, like(x, y)))).
%@ [like(a,c),like(b,c),like(c,c)]
%@ [like(a,c),like(b,c),like(c,b)]
% ...
....