The start of dif/2 contains:
dif(X,Y) :-
?=(X,Y),
!,
X \== Y.
Couldn’t the combination of ?=/2 and \==/2 instead be \=/2, to compare once instead of twice? I.e.:
dif(X,Y) :-
X \= Y,
!.
Ah, might also need, after that:
dif(X,Y) :-
X == Y,
!,
fail.
I suppose my question is, isn’t it better to optimize with the expectation that dif/2 will succeed?
Examples of performance improvement:
?- X = Y, time((between(1, 50000000, N), ?=(X, Y), X \== Y)).
% 149,999,999 inferences, 3.734 CPU in 3.734 seconds (100% CPU, 40175184 Lips)
?- X = Y, time((between(1, 50000000, N), X \= Y)).
% 99,999,999 inferences, 2.170 CPU in 2.170 seconds (100% CPU, 46092564 Lips)
and
?- X = 1, Y = 2, time((between(1, 50000000, N), ?=(X, Y), X \== Y, fail)).
% 149,999,999 inferences, 3.702 CPU in 3.702 seconds (100% CPU, 40522201 Lips)
?- X = 1, Y = 2, time((between(1, 50000000, N), X \= Y, fail)).
% 99,999,999 inferences, 2.065 CPU in 2.065 seconds (100% CPU, 48417016 Lips)