Hi!
I am trying to create pairs of variables from a list of literals that contain unbound variables (ClauseLiterals
below): I want to create pairs that combine all the variables in each literal with all the variables in the other literals. See the program at the bottom of this post.
Running test1
yields the behavior I was expecting:
?- test1.
[h(_15806,_15808),p(_15818,_15820),q(_15830,_15832,_15834)]
_15806,_15818
_15806,_15820
_15808,_15818
_15808,_15820
_15806,_15830
_15806,_15832
_15806,_15834
_15808,_15830
_15808,_15832
_15808,_15834
_15818,_15830
_15818,_15832
_15818,_15834
_15820,_15830
_15820,_15832
_15820,_15834
false.
But running test2
, which I want to use in my real program, yields this:
?- test2.
[h(_17660,_17662),p(_17672,_17674),q(_17684,_17686,_17688)]
_28,_32
_28,_34
_30,_32
_30,_34
_28,_36
_28,_38
_28,_40
_30,_36
_30,_38
_30,_40
_32,_36
_32,_38
_32,_40
_34,_36
_34,_38
_34,_40
true.
Why are new variables seemingly introduced? I.e. _34
, _40
, and so on?
Thanks in advance!
Cheers/JCR
test1:-
ClauseLiterals = [h(A, B), p(C, D), q(E, F, G)],
writeln(ClauseLiterals),
variablePairForClause(ClauseLiterals, Pair),
writeln(Pair),
fail.
test2:-
ClauseLiterals = [h(A, B), p(C, D), q(E, F, G)],
writeln(ClauseLiterals),
bagof(Pair, variablePairForClause(ClauseLiterals, Pair), Pairs),
maplist(writeln, Pairs).
variablePairForClause(ClauseLiterals, Pair):-
length(ClauseLiterals, NLiterals),
between(1, NLiterals, Index1),
StartIndex2 is Index1 + 1,
between(StartIndex2, NLiterals, Index2),
nth1(Index1, ClauseLiterals, Literal1),
nth1(Index2, ClauseLiterals, Literal2),
term_variables(Literal1, Literal1Vars),
term_variables(Literal2, Literal2Vars),
member(P1, Literal1Vars),
member(P2, Literal2Vars),
Pair = (P1, P2).