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:


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?


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?

  • 1
    $\begingroup$ As to Internal`ComparePatterns there's How to generally match, unify and merge patterns?. $\endgroup$
    – jkuczm
    Aug 2, 2017 at 13:45
  • 1
    $\begingroup$ You can play with Internal`CompareToPAT options to see different results than {"Structural difference", None, ...} $\endgroup$
    – jkuczm
    Aug 2, 2017 at 13:51
  • $\begingroup$ @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. $\endgroup$
    – b3m2a1
    Aug 2, 2017 at 21:06
  • $\begingroup$ Since it's undocumented I also don't know details, but it seems Internal`CompareToPAT is 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}, or Internal`CompareToPAT[{1., 1.}, {2., 3.}, PrecisionGoal -> #] & /@ {0, None, 1} $\endgroup$
    – jkuczm
    Aug 3, 2017 at 11:53
  • $\begingroup$ @jkuczm good example there. I've been working in a sort-of similar way and still am very confused. $\endgroup$
    – b3m2a1
    Aug 3, 2017 at 14:54


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.