#
# meet(L) returns a symmetric table of values for the meet function of a 
#  lattice or meet-semilattice L. That is, for each pair of vertices [a,b]
#  of L, meet(L)[a,b] is the greatest lower bound for a and b.
# meet(L,[a,b,...]) will return the greatest lower bound for vertices a,b,...
# The procedure does *not* verify that L is a lattice or semilattice.
# In fact, L can be any poset, in which case, the answer returned is a
#  maximal lower bound, or NULL, if there is no such lower bound.
#
`posets/meet`:=proc(L) local X,n,down,i,j,res,elts;
  n:=nargs; if type(args[n],'list') then n:=n-1 fi;
  if n=1 then X:=map(op,L)
    elif type(args[2],'integer') then X:={$1..args[2]}
    else X:=args[2] fi;
  X:=map(op,posets['filter'](L,X)); n:=nops(X);
  down:=`posets/downsets`(subs({seq(X[i]=i,i=1..n)},L),[$1..n]);
  for i to n do down[i]:={i,op(down[i])} od;
  if type(args[nargs],'list') then res:={$1..n};
    elts:=subs({seq(X[i]=i,i=1..n)},args[nargs]);
    for i in elts do res:=res intersect down[i] od;
    if nops(res)>0 then X[max(op(res))] fi
  else
    res:=table('symmetric');
    for i to n do
      res[X[i],X[i]]:=X[i];
      for j from i+1 to n do
        elts:=down[i] intersect down[j];
        if nops(elts)>0 then res[X[i],X[j]]:=X[max(op(elts))] fi;
      od;
    od; op(res)
  fi
end:
#
save `posets/meet`, cat(HomeLib,`/posets/meet.m`);
read cat(HomeLib,`/help/posets/meet`);
quit;

