I'm trying to use Prolog to reason about constraints and then query the knowledgebase to pass these constraints on to a solver (can't use clpfd).
%a first example would be constrainsquare(Row,Col,Val)
constrainsquare(1,1,3).
constrainsquare(1,2,2).
and I can then query all constraints using something like bagof/3. This is not statisfactory because I also want to be able to write
constrainsquare(3,4,8):-constrainsquare(3,3,7).
The ability to say that if the solution has a 7 at position 3,3; it necessarily has an 8 at position 3,4.
Now you can no longer gather all constraints using something like bagof/3.
How would you do this ideomatically in prolog?
Note that I cannot simply do
constrainsquare(L) :-
member(cs(1,1,3),L),
member(cs(1,2,2),L),
member(cs(3,4,8),L),
member(cs(3,3,7),L).
because I'm receiving new facts about the solution periodically and cannot alter existing facts.
At the moment I'm thinking about using a list of constraints and doing something like
info(cell(1,1,3)).
info(cell(1,2,2)).
constrainsquare(I,[I]).
partialinfo(cell(3,4,8),cell(3,3,7)).
....
and then query it by running bagoff to obtain [cell(1,1,3)],[cell(1,2,2)]... and then folding/appending to [cell(1,1,3),cell(1,2,2)] but this feels a bit 'meh'. I want to know the "proper" way.
bagof/3and why it doesn't work? If I assert factsconstrainsquare(1,1,3).,constrainsquare(1,2,2).,constrainsquare(3,3,7).and the ruleconstrainsquare(3,4,8):-constrainsquare(3,3,7).and query,bagof([A,B,C], constrainsquare(A,B,C), L), I get all of the expected solutions listed inL. So I don't quite understand the problem. - lurkerbagof/3.bagof/3doesn't gather constraints, but rather it gathers known solutions assuming the currently asserted facts and rules. - lurker