How to define (and name) the corresponding safe term comparison predicates in ISO Prolog?

iso_dif/2 is much simpler to implement than a comparison: The built-in \= operator is available You now exactly what arguments to provide to\= Definition Based on your comments, the safe comparison means that the order won’t change if variables in both subterms are instanciated. If we name the comparison lt, we have for example: lt(a(X), … Read more

Prolog: First duplicate value

Here is a pure version using dif/2 which implements sound inequality. dif/2 is offered by B-Prolog, YAP-Prolog, SICStus-Prolog and SWI-Prolog. firstdup(E, [E|L]) :- member(E, L). firstdup(E, [N|L]) :- non_member(N, L), firstdup(E, L). member(E, [E|_L]). member(E, [_X|L]) :- member(E, L). non_member(_E, []). non_member(E, [F|Fs]) :- dif(E, F), non_member(E, Fs). The advantages are that it can also … Read more

Using \==/2 or dif/2

For elegance and didactic reasons alone, dif/2 is clearly preferable here and also in the vast majority of other cases, since as you already note “a lot of unnecessary unifications might take place” otherwise, and also because dif/2 is a pure and nicely declarative predicate that can be used in all directions and at any … Read more