I have found a predicate named seq_compare
defined below is drastically effective for a problem, which I didn’t expected so much befere.
This is a sample of the ordering.
a==b < a->b < xor(a, b) < a*b < a+b < a < -a
However the current definition looks not smart. Maybe I should write term_expansion/2
which expands a list
[==/2, ->/2, xor/2, */2, +/2, -/1]
to the target definition.
Is there smarter way than term_expansion
? Use of the compare predicate is so busy in my application, comparative time efficiency is required. Thanks for suggestions in the future.
seq_compare(=, X, X):-!.
seq_compare(C, ==(X, Y), ==(U, V)):-!, seq_compare(C, X, Y, U, V).
seq_compare(C, ->(X, Y), ->(U, V)):-!, seq_compare(C, X, Y, U, V).
seq_compare(C, +(X, Y), +(U, V)):-!, seq_compare(C, X, Y, U, V).
seq_compare(C, *(X, Y), *(U, V)):-!, seq_compare(C, X, Y, U, V).
seq_compare(C, xor(X, Y), xor(U, V)):-!, seq_compare(C, X, Y, U, V).
seq_compare(<, ==(_, _), _):-!.
seq_compare(>, _, ==(_, _)):-!.
seq_compare(<, ->(_, _), _):-!.
seq_compare(>, _, ->(_, _)):-!.
seq_compare(<, xor(_, _), _):-!.
seq_compare(>, _, xor(_, _)):-!.
seq_compare(<, *(_, _), _) :-!.
seq_compare(>, _, *(_, _)) :-!.
seq_compare(<, +(_, _), _) :-!.
seq_compare(>, _, +(_, _)) :-!.
seq_compare(C, -(X), -(Y)) :-!, seq_compare(C, X, Y).
seq_compare(C, X, -(Y)) :-!,
( X = Y -> C = (<)
; seq_compare(C, X, Y)
).
seq_compare(C, -(X), Y):-!,
( X = Y -> C = (>)
; seq_compare(C, X, Y)
).
seq_compare(C, X, Y):- compare(C, X, Y).
% seq_compare/5
seq_compare(C, X, Y, U, V):-
seq_compare(C0, X, U),
( C0 = (=) -> seq_compare(C, Y, V)
; C = C0
).