
# Return true if the canonical form of dual(P) is strictly cheaper than
# the canonical form of P. (So we can avoid testing both P and its dual).
#
# Also, assign generators of the automorphism group of P to args[3].
#
# We exploit the fact that P, as produced by 'traverse', is
# already in a canonical form that has maximal elements that cover
# a minimum number of elements listed first. So we need to do the
# same for the dual before we compare.
#
# Note: this code is broken if P is empty. But we intend to precede
# this with a test for connectivity, so this issue should not occur.

dualsquash:=proc(P,n) local Y,ct,i,d,pi,ag,f,dP;
  Y:=map(op,P) minus map(x->x[2],P);
  ct:=table([seq(i=0,i=Y)]);
  for i in P do if member(i[1],Y) then ct[i[1]]:=ct[i[1]]+1 fi od;
  d:=min(seq(ct[i],i=Y));
  Y:=map(proc(x,y,z) if y[x]=z then x fi end,Y,ct,d);
  dP:=posets['dual'](P);
  pi:=`posets/canorder`(dP,[Y,{$1..n} minus Y],'ag');
  assign(args[3],ag);
  dP:=subs({seq(pi[i]=i,i=1..n)},dP);
  f:=`orbitsquash/first`(dP minus P);
  Y:=P minus dP;
  for i in Y do
    if i[1]<f[1] or (i[1]=f[1] and i[2]<f[2]) then RETURN(true) fi
  od; false
end;

