FIXED: CLP(FD): Correctly handle variable aliasing in                 global_cardinality/2. Salvador Fandino.
FIXED: CLP(FD): Correctly handle variable aliasing in
                global_cardinality/2. Salvador Fandino.

Aliasing tested with:

   :- use_module(library(clpfd)).

   neqs([], _)     --> [].
   neqs([V|Vs], X) --> ( [X #\= V] ; [] ), neqs(Vs, X).

   all_constraints([], _)      --> [].
   all_constraints([V|Vs], Ns) -->
           neqs(Ns, V),
           aliases(Vs, V),
           all_constraints(Vs, Ns).

   aliases([], _) --> [].
   aliases([V|Vs], V0) --> ( [V0 = V] ; [] ), aliases(Vs, V0).

   run :-
           length(Vs, N),
           MaxLCs #= N^2,
           between(1, MaxLCs, LCs),
           length(Cs, LCs),
           numlist(1, N, Ns),
           portray_clause(N-LCs),
           phrase(all_constraints(Vs,Ns), Cs),
           Vs ins 1..N,
           maplist(call, Cs),
           length(Values, N),
           length(Xs, N),
           Xs ins 0..N,
           label(Xs),
           pairs_keys_values(Pairs, Ns, Values),
           findall(Vs-Pairs, (Xs=Values,
                                 label(Vs),
                                 global_cardinality(Vs, Pairs)), Sol1),
           findall(Vs-Pairs, (global_cardinality(Vs, Pairs),
                                 Xs=Values,
                                 label(Vs)), Sol2),
           (   Sol1 == Sol2 -> true
           ;   Values = Xs,
               portray_clause(maplist(call, [X=Vs,global_cardinality(X,Pairs)|Cs])-Sol1-Sol2)
           ),
           false.

Previously:

   %?- run.
   %@ 1-1.
   %@ 2-1.
   %@ 2-2.
   %@ 2-3.
   %@ 2-4.
   %@ 3-1.
   %@ maplist(call, [B=[A, A, _], global_cardinality(B, [1-0, 2-1, 3-2]), A=A])-[[3, 3, 2]-[1-0, 2-1, 3-2]]-[].

Now:

   %?- run.
   %@ 1-1.
   %@ 2-1.
   %@ 2-2.
   %@ 2-3.
   %@ 2-4.
   %@ 3-1.
   %@ 3-2.

mndrix/swipl-devel,edechter/swipl-devel,koryonik/swipl-devel,koryonik/swipl-devel,edechter/swipl-devel,mndrix/swipl-devel,jn7163/swipl-devel,edechter/swipl-devel,edechter/swipl-devel,koryonik/swipl-devel,mndrix/swipl-devel,mndrix/swipl-devel,mndrix/swipl-devel,jn7163/swipl-devel,jn7163/swipl-devel,jn7163/swipl-devel,koryonik/swipl-devel,koryonik/swipl-devel,edechter/swipl-devel,jn7163/swipl-devel