#
# antichains(P,X) produces a lexordered list of antichains in a poset P
#  with vertex set X.
# antichains(P,n) does the same, using X={1,...,n}.
# antichains(P) does the same, assuming that P has no isolated vertices.
# In the lexorder, antichain A precedes antichain B if the "last" member
#  of the symmetric difference of A and B occurs in B. Here, "last" is
#  determined by some chosen linear extension of the vertex set of P.
# To force a particular choice of linear extension, provide X as a *list*
#  of vertices in the desired order.
# Optionally, if the final argument (the 2nd or 3rd) is:
#  -the name 'max', the result is a lexordered list of *maximal* antichains
#  -any other name, such as q, the result is the generating series for
#   antichains (i.e., the sum of q^nops(A) for all antichains A)
#  -a range, such as 2..7, the result is a lexordered list of antichains
#   in P with sizes in this range.
#
`posets/antichains`:=proc(P) local Q,X,n,lb,ub,q;
  lb:=NULL; q:=NULL; n:=nargs;
  if type(args[n],'range') then
    lb:=op(1,args[n]); ub:=op(2,args[n]); n:=n-1
  elif type(args[n],'name') then
    q:=args[n]; n:=n-1 fi;
  if n>2 then ERROR(`invalid arguments`)
    elif n=2 and type(args[2],'list') then X:=args[2]
    else X:=map(op,posets['filter'](args[1..n])) fi;
  if lb=NULL then lb:=0; ub:=nops(X)
    elif nops(X)<lb or ub<0 or ub<lb then RETURN([]) fi;
  Q:=posets['closure'](P,X);
  if q=NULL then `posets/antichains/list`(Q,X,lb,ub)
    elif q='max' then `posets/antichains/max`(Q,X)
    else `posets/antichains/series`(Q,X,q) fi
end:
#
# Either an antichain omits the last member of X, or it does not...
# The input is always kept sane:  nops(X) >= lb <= ub >= 0.
#
`posets/antichains/list`:=proc(Q,X,lb,ub) local n,mx,P,Y,L;
  n:=nops(X);
  if n=0 then RETURN([{}])
    elif n-1<lb then
      if nops(Q)=0 and n<=ub then RETURN([{op(X)}]) else RETURN([]) fi
    elif ub<1 then
      if lb<=0 then RETURN([{}]) else RETURN([]) fi
    elif n=1 then RETURN([{},{op(X)}]) fi;
  mx:=X[n]; Y:=subsop(n=NULL,X);
  P:=map(proc(e,y) if e[2]<>y then e fi end,Q,mx);
  L:=procname(P,Y,lb,ub);
  Y:=map(proc(x,y,Z) if not member([x,y],Z) then x fi end,Y,mx,Q);
  if nops(Y)<lb-1 then RETURN(L) elif nops(Y)<n-1 then
    P:=map(proc(e,Z) if member(e[1],Z) then e fi end,P,Y) fi;
  [op(L),op(map((x,y)->{op(x),y},procname(P,Y,lb-1,ub-1),mx))]
end:
#
# For a series, we can exit early when Q is empty.
#
`posets/antichains/series`:=proc(Q,X,q) local n,mx,P,Y,f;
  if nops(Q)=0 then RETURN(expand((1+q)^nops(X))) fi;
  n:=nops(X); mx:=X[n]; Y:=subsop(n=NULL,X);
  P:=map(proc(e,y) if e[2]<>y then e fi end,Q,mx);
  f:=procname(P,Y,q);
  Y:=map(proc(x,y,Z) if not member([x,y],Z) then x fi end,Y,mx,Q);
  if nops(Y)=n-1 then f+expand(q*f) else
    P:=map(proc(e,Z) if member(e[1],Z) then e fi end,P,Y);
    f+expand(q*procname(P,Y,q))
  fi
end:
#
`posets/antichains/max`:=proc(Q,X) local n,mx,P,L,Y,L2;
  if nops(Q)=0 then RETURN([{op(X)}]) fi;
  n:=nops(X); mx:=X[n]; Y:=subsop(n=NULL,X);
  P:=map(proc(e,y) if e[2]<>y then e fi end,Q,mx);
  L:=procname(P,Y);
  Y:=map(proc(x,y,Z) if not member([x,y],Z) then x fi end,Y,mx,Q);
  if nops(Y)=n-1 then
    map((x,y)->{op(x),y},L,mx)
  else
    P:=map(proc(e,Z) if member(e[1],Z) then e fi end,P,Y);
    L2:=procname(P,Y);
    L:=map(proc(x,Z) if not member(x,Z) then x fi end,L,L2);
    [op(L),op(map((x,y)->{op(x),y},L2,mx))]
  fi
end:
#
save `posets/antichains`, `posets/antichains/list`,
  `posets/antichains/series`, `posets/antichains/max`,
  cat(HomeLib,`/posets/antichains.m`);
read cat(HomeLib,`/help/posets/antichains`);
quit;

