fun firstNotEqualTerm f l = caseList.find notEqualTerm l of
SOME (x,y) => f x y
| NONE => raise Bug "firstNotEqualTerm";
(* ------------------------------------------------------------------------- *) (* The weight of all constants must be at least 1, and there must be at most *) (* one unary function with weight 0. *) (* ------------------------------------------------------------------------- *)
val arityPrecedence : Term.function * Term.function -> order =
fn ((f1,n1),(f2,n2)) => case Int.compare (n1,n2) of
LESS => LESS
| EQUAL => Name.compare (f1,f2)
| GREATER => GREATER;
(* The default order *)
val default = {weight = uniformWeight, precedence = arityPrecedence};
(* ------------------------------------------------------------------------- *) (* Term weight-1 represented as a linear function of the weight-1 of the *) (* variables in the term (plus a constant). *) (* *) (* Note that the conditions on weight functions ensure that all weights are *) (* at least 1, so all weight-1s are at least 0. *) (* ------------------------------------------------------------------------- *)
datatype weight = Weight of int NameMap.map * int;
val weightEmpty : int NameMap.map = NameMap.new ();
val weightZero = Weight (weightEmpty,0);
fun weightIsZero (Weight (m,c)) = c = 0 andalso NameMap.null m;
fun weightNeg (Weight (m,c)) = Weight (NameMap.transform ~ m, ~c);
local fun add ((_,n1),(_,n2)) = let val n = n1 + n2 in if n = 0then NONE else SOME n end; in fun weightAdd (Weight (m1,c1)) (Weight (m2,c2)) =
Weight (NameMap.union add m1 m2, c1 + c2); end;
fun weightSubtract w1 w2 = weightAdd w1 (weightNeg w2);
fun weightTerm weight = let fun wt m c [] = Weight (m,c)
| wt m c (Term.Var v :: tms) = let val n = Option.getOpt (NameMap.peek m v, 0) in
wt (NameMap.insert m (v, n + 1)) (c + 1) tms end
| wt m c (Term.Fn (f,a) :: tms) =
wt m (c + weight (f, length a)) (a @ tms) in
fn tm => wt weightEmpty ~1 [tm] end;
fun weightLowerBound (w as Weight (m,c)) = if NameMap.exists (fn (_,n) => n < 0) m then NONE else SOME c;
(*MetisDebug valppWeightList= let funppCoeffn= ifn<0thenPrint.sequence(Print.ppString"~")(ppCoeff(~n)) elseifn=1thenPrint.skip elsePrint.ppIntn
funpp_tm(NONE,n)=Print.ppIntn |pp_tm(SOMEv,n)=Print.sequence(ppCoeffn)(Name.ppv) in fn[]=>Print.ppInt0 |tms=>Print.ppOpList"+"pp_tmtms end;
funppWeight(Weight(m,c))= let vall=NameMap.toListm vall=List.map(fn(v,n)=>(SOMEv,n))l vall=ifc=0thenlelsel@[(NONE,c)] in ppWeightListl end;
valweightToString=Print.toStringppWeight;
*)
(* ------------------------------------------------------------------------- *) (* The Knuth-Bendix term order. *) (* ------------------------------------------------------------------------- *)
fun compare {weight,precedence} = let fun weightDifference tm1 tm2 = let val w1 = weightTerm weight tm1 and w2 = weightTerm weight tm2 in
weightSubtract w2 w1 end
fun weightLess tm1 tm2 = let val w = weightDifference tm1 tm2 in if weightIsZero w then precedenceLess tm1 tm2 else weightDiffLess w tm1 tm2 end
and weightDiffLess w tm1 tm2 = case weightLowerBound w of
NONE => false
| SOME 0 => precedenceLess tm1 tm2
| SOME n => n > 0
fun weightDiffGreater w tm1 tm2 = weightDiffLess (weightNeg w) tm2 tm1
fun weightCmp tm1 tm2 = let val w = weightDifference tm1 tm2 in if weightIsZero w then precedenceCmp tm1 tm2 elseif weightDiffLess w tm1 tm2 then SOME LESS elseif weightDiffGreater w tm1 tm2 then SOME GREATER else NONE end
and precedenceCmp (Term.Fn (f1,a1)) (Term.Fn (f2,a2)) =
(case precedence ((f1, length a1), (f2, length a2)) of
LESS => SOME LESS
| EQUAL => firstNotEqualTerm weightCmp (zip a1 a2)
| GREATER => SOME GREATER)
| precedenceCmp _ _ = raise Bug "kboOrder.precendenceCmp" in
fn (tm1,tm2) => if Term.equal tm1 tm2 then SOME EQUAL else weightCmp tm1 tm2 end;
(*MetisTrace7 valcompare=fnkbo=>fn(tm1,tm2)=> let val()=Print.traceTerm.pp"KnuthBendixOrder.compare:tm1"tm1 val()=Print.traceTerm.pp"KnuthBendixOrder.compare:tm2"tm2 valresult=comparekbo(tm1,tm2) val()= caseresultof NONE=>trace"KnuthBendixOrder.compare:result=Incomparable\n" |SOMEx=> Print.tracePrint.ppOrder"KnuthBendixOrder.compare:result"x in result end;
*)
end
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.