How to compare/3 without surprises on non-ground terms?

Hard to say. It could be an issue with the current implementation as well as just that the fundamental problem shows up at a different place. The first is not unlikely though.

The cyclic comparison uses the same trick as for cyclic unification I learned from Bart Demoen and that is described in pl-prims.c in a comment starting at line 71. The idea is that while going through compounds you create a reference link from the compound at one side to the other one. Now, for unification and ==/2 this works fine as the argument order does not matter. For comparison, the argument order does matter.

Now, is there a fundamentally better (not so easy to define what is better) algorithm? Is it possible to improve on the current algorithm, possibly by keeping track of the “side” we are?

Anyone wishing to sort it out, see pl-prims.c, do_compare() :slight_smile: It surely is an interesting finding. Is there a pointer to the original discussion?