#
# P &+ Q returns the ordinal sum of the posets P and Q. Similarly, one
#  may use P &+ Q &+ R, etc. If one or more of the posets, say P, has
#  isolated vertices, then one must use (P,X) &+ Q or (P,n) &+ Q,
#  according to whether X or {1,...,n} is the vertex set of P.
# The output will be a poset on the vertex set {1,2,...,m} for some m.
#  Note that P &+ Q never has isolated vertices.
# N.B.: rather than trap errors, we must return bad input unevaluated
#  in order to avoid breaking certain Maple commands in V.3 or later.
# It would be safer to name this `&o` or `&s`....
#
`posets/&+`:=proc() local P,X,i,j,k,n,X0,X1;
  k:=0; n:=0; P:=table();
  for i to nargs do
    k:=k+1; P[k]:=args[i];
    if not type(P[k],'set'('list')) then RETURN('procname(args)')
      elif i=nargs or type(args[i+1],'set'('list')) then X:=map(op,P[k])
      elif type(args[i+1],'set') then i:=i+1; X:=args[i]
      elif type(args[i+1],'integer') then i:=i+1; X:={$1..args[i]}
      else RETURN('procname(args)') fi;
    if i=nargs then X1:={} else X1:=X minus map(e->e[1],P[k]) fi;
    if i=1 then X0:={} else X0:=X minus map(e->e[2],P[k]) fi;
    P[k]:=subs({seq(X[j]=n+j,j=1..nops(X))},[P[k],X0,X1]);    
    n:=n+nops(X)
  od;
  if k=1 then P[1][1],n else
    {seq(op(P[n][1]),n=1..k),
      seq(seq(seq([i,j],i=P[n][3]),j=P[n+1][2]),n=1..k-1)} fi;
end:
#
save `posets/&+`, cat(HomeLib,`/posets/ord_sum.m`);
read cat(HomeLib,`/help/posets/ord_sum`);
quit;

