
# If down is the closure table of a poset P with vertex set X,
# generate the list of order ideals of P, sorted by increasing size.

ideals:=proc(down,X) local Q,i,j,A,anti;
  Q:={seq(seq([i,j],i=down[j]),j=X)};
  Q:=map(proc(x) if x[1]<>x[2] then x fi end,Q);
  anti:=`ideals/antichains`(Q,X);
  anti:=[seq({seq(op(down[i]),i=A)},A=anti)];
  sort(anti,(x,y)->evalb(nops(x)<nops(y)))
end;

# If P is transitively closed with vertex set X,
# return the set of all antichains of P.

`ideals/antichains`:=proc(P,X) local P1,P2,X1;
  if nops(X)<2 then RETURN({{},X}) fi;
  P2:=map(proc(x,y) if x[1]<>y and x[2]<>y then x fi end,P,X[1]);
  X1:=X minus {X[1],op(map(op,P minus P2))};
  P1:=map(proc(e,Y) if member(e[1],Y) and member(e[2],Y) then e fi end,P,X1);
  {op(map((x,y)->{op(x),y},procname(P1,X1),X[1])),
    op(procname(P2,subsop(1=NULL,X)))};
end:

