# What do the InternalCompare* functions do?

As part of looking at this question I dug into Names["Internal*Compare*"] and ended up with:

{InternalCompareToPAT, InternalCompareNumeric, InternalComparePatterns}

I was guessing they served as the backbone of whatever the canonical ordering is, but on further exploration they're related, but perhaps entirely disjoint from it. Well, CompareNumeric does tell us about ordering. But the other two seem to tell us about difference, but not ordering.

CompareNumeric has been explored here but the others haven't been treated on the site, it seems.

Here's my brief understanding that hopefully others will be able to expand:

### InternalComparePatterns

As jkuczm points out, this actually has a good discussion here.

This seems to be part of the pattern matcher (or something). Consider these cases:

InternalComparePatterns @@@ {
{_, _},
{_, __},
{_, ___},
{_, _String},
{_, 1 ...},
{2 ..., 1 ...},
{Optional[_, 1], _},
{Optional[_, 1], Optional[_, 2]},
{a_, _}
}

{"Identical", "Specific", "Specific", "Incomparable", "Incomparable", \
"Disjoint", "Incomparable", "Equivalent", "Equivalent"}


I think I've covered the core pattern classes, but what do they mean? What is "Specific"? Why are _ and _String incomparable?

### InternalCompareToPAT

This seems to specify the position of first structural difference? Consider:

InternalCompareToPAT @@@ {
{a, a},
{1, 2},
{"1", 2},
{"A", "a"},
{g[1], g[2]},
{g[1, 1], g[1, 2]},
{\[Pi], N@\[Pi]},
{g["c", "b"], g["c", "d"]}
}

{True, {{"Structural difference",
None, {}}}, {{"Structural difference",
None, {}}}, {{"Structural difference",
None, {}}}, {{"Structural difference",
None, {1}}}, {{"Structural difference",
None, {2}}}, True, {{"Structural difference", None, {2}}}}


But is everything a "StructuralDifference"? Why do they come as a list-in-a-list? It doesn't let me pass more than 2 arguments to it. And what is that None?

I know this is open-ended, but I think understanding it would be useful at the very least for understanding internal implementation details, even if it won't help elucidate how Sort does its thing.

• As to InternalComparePatterns there's How to generally match, unify and merge patterns?. Aug 2, 2017 at 13:45
• You can play with InternalCompareToPAT options to see different results than {"Structural difference", None, ...} Aug 2, 2017 at 13:51
• @jkuczm do you have a suggestion of what sort of manipulations to start with? In particular I don't quite understand how a Tolerance, AccuracyGoal, or PerformanceGoal really makes sense here. Aug 2, 2017 at 21:06
• Since it's undocumented I also don't know details, but it seems InternalCompareToPAT is testing some form of equality of expressions with details controlled by those options. Check for example: InternalCompareToPAT[{1., 1.}, {2., 3.}, Tolerance -> #] & /@ {None, 10, 100}, or InternalCompareToPAT[{1., 1.}, {2., 3.}, PrecisionGoal -> #] & /@ {0, None, 1} Aug 3, 2017 at 11:53