#
# chains(P,X) produces a lexordered list of chains in a poset P with
#  vertex set X.
# chains(P,n) does the same, using X={1,...,n}.
# chains(P) does the same, assuming that P has no isolated vertices.
# In the lexorder, chain A precedes chain 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* chains
#  -any other name, such as q, the result is the generating series for
#   chain sizes (not lengths) (i.e., the sum of q^nops(C) for all chains C)
#  -a range, such as 2..7, the result is a lexordered list of chains in P
#   with sizes in this range.
#
`posets/chains`:=proc(P) local Q,X,Y,n,lb,ub,down,i,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 q='max' then
    n:=nops(X); Y:={$1..n};
    Q:=subs({seq(X[i]=i,i=1..n)},P);
    down:=`posets/coversets`(Q,Y);
    down[n+1]:=Y minus map(e->e[1],Q);
    for i to n+1 do down[i]:=sort([op(down[i])]) od;
    Q:=`posets/chains/max`(n+1,down);
    subs({seq(i=X[i],i=1..n),n+1=NULL},Q)
  elif q<>NULL then
    `posets/chains/series`(P,X,q)
  else
    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);
    `posets/chains/list`(Q,X,lb,ub)
  fi
end:
#
# Either a chain omits the last member of X, or it does not...
# The input is always kept sane:  nops(X) >= lb <= ub >= 0.
#
`posets/chains/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 n<=ub and 2*nops(Q)=n*(n-1) then RETURN([X]) else RETURN([]) fi
    elif ub<1 then
      if lb<=0 then RETURN([[]]) else RETURN([]) fi
    elif n=1 then RETURN([[],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 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[2],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 the series, there is an easy one-pass approach.
# A seq() would be faster, but less space-efficient than g:=g+f[y].
#
`posets/chains/series`:=proc(P,X,q) local f,g,f0,x,y;
  f:=`posets/downsets`(P,X); f0:=1;
  for x in X do
    g:=1; for y in f[x] do g:=g+f[y] od;
    f[x]:=expand(q*g); f0:=f0+f[x]
  od; f0
end:
#
`posets/chains/max`:=proc(x,down) local new;
  if nops(down[x])=0 then [[x]] else
    new:=map(op,map(procname,down[x],down));
    map((c,y)->[op(c),y],new,x)
  fi
end:
#
save `posets/chains`, `posets/chains/list`, `posets/chains/series`,
  `posets/chains/max`, cat(HomeLib,`/posets/chains.m`);
read cat(HomeLib,`/help/posets/chains`);
quit;

