Cheap compare for cyclic terms [injective collation keys]

Representation based comparison does seem to work well on everything I’ve tested, e.g., “Monkey test” for transitivity and anti-symmetry with repeat counts of 10,000,000.

A non-mathematical perspective: Representation based comparison requires the definition of a mapping predicate from any term to an acyclic equivalent such that the total order of the mapped terms is the same as the order of the original terms. These mapped terms can then be compared using standard ordering, i.e., compare/3 to define the order of the original terms.

The success of this approach depends on the definition of the mapping function. It has to ensure that the standard order of acyclic terms is respected. It also has to address the issues of transitivity and anti-symmetry with cyclic terms that cause issues with the current compare/3. @j4n_bur53’s naish/2 seems to nicely meet both of these requirements but it is not the only one so, subject to some level of standardization, it’s an implementation dependent solution. But a pretty good one IMO.

Note that since lexical ordering of cyclic terms is undecidable, different mapping functions may order cyclic terms differently. But this shouldn’t cause a problem if the ordering is only locally significant, i.e., defined by the same mapping predicate.