As part of looking at this question I dug into Names["Internal`*Compare*"] and ended up with:
{Internal`CompareToPAT, Internal`CompareNumeric, Internal`ComparePatterns}
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:
Internal`ComparePatterns
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:
Internal`ComparePatterns @@@ {
{_, _},
{_, __},
{_, ___},
{_, _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?
Internal`CompareToPAT
This seems to specify the position of first structural difference? Consider:
Internal`CompareToPAT @@@ {
{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.
Can anyone provide more info?
Internal`ComparePatternsthere's How to generally match, unify and merge patterns?. – jkuczm Aug 02 '17 at 13:45Internal`CompareToPAToptions to see different results than{"Structural difference", None, ...}– jkuczm Aug 02 '17 at 13:51Tolerance,AccuracyGoal, orPerformanceGoalreally makes sense here. – b3m2a1 Aug 02 '17 at 21:06Internal`CompareToPATis testing some form of equality of expressions with details controlled by those options. Check for example:Internal`CompareToPAT[{1., 1.}, {2., 3.}, Tolerance -> #] & /@ {None, 10, 100}, orInternal`CompareToPAT[{1., 1.}, {2., 3.}, PrecisionGoal -> #] & /@ {0, None, 1}– jkuczm Aug 03 '17 at 11:53