#
# subinterval(P,[a,b]) returns the subposet of P formed by the
#  vertices x such that a <= x <= b.
# subinterval(P,[a,infinity]) does the same for all vertices >= a.
# subinterval(P,[-infinity,b]) does the same for all vertices <= b.
#
`posets/subinterval`:=proc(P) local n,i,X,down,x,new,Q;
  n:=nargs; i:=args[n]; n:=n-1;
  if n>2 then ERROR(`invalid arguments`)
    elif n=1 then X:=map(op,P)
    elif type(args[2],'integer') then X:={$1..args[2]}
    else X:=args[2] fi;
  if i[1]<>-infinity then
    Q:=[procname(posets['dual'](P),X,[-infinity,i[1]])];
    if nops(Q)>0 then
      Q:=subsop(1=posets['dual'](Q[1]),Q);
      procname(op(Q),[-infinity,i[2]]) fi
  elif i[2]=infinity then args[1..n]
  elif member(i[2],X) then 
    down:=`posets/coversets`(P,X);
    X:={}; new:={i[2]};
    while nops(new)>0 do
      X:={op(X),op(new)};
      new:={seq(op(down[x]),x=new)}
    od;
    Q:=map(proc(e,Y) if member(e[2],Y) then e fi end,P,X);
    if nops(Q)>0 then Q else {},X fi
  fi
end:
#
save `posets/subinterval`, cat(HomeLib,`/posets/subinterval.m`);
read cat(HomeLib,`/help/posets/subinterval`);
quit;

