12
$\begingroup$

When trying to switch from version 10.3 to 11.3 I noticed that some of my code do not work properly. After some investigation I found that the problem is with pattern matcher. Here is the example:

toSort = Subsets[Range[13]][[2 ;;]];
gaIndexSort[ind : {{__Integer?Positive} ..}, 
  ord_String: "InvDeg[Lex]"] := "matched"



 gaIndexSort[toSort, "Lex"]
(* not evaluated *)

Evaluation in 10.3 and 11.0 works as expected, however in 11.3 message "General::maxrec: Recursion limit exceeded; positive match might be missed." is issued and the function is not evaluated. Of course, I can change the pattern to, for example,

gaIndexSort1[ind : {_List ..}, ord_String: "InvDeg[Lex]"] := 
  "matched1" /; 
   AllTrue[Union[Flatten[ind]], (Positive[#] && IntegerQ[#] &)];



gaIndexSort1[toSort, "Lex"]
(*"matched1"*)

Which works as expected. The questions, however, remains 1) is the change intentional and 2) how to effectively match large recursive pattern in most effective way.

$\endgroup$
5
  • $\begingroup$ What do you mean by "recursive" pattern? I don't see any recursion, or am I missing something? As for the issue itself: Seems buggy to me, as removing the default value from the second parameter fixes the issue $\endgroup$
    – Lukas Lang
    Commented Jul 22, 2019 at 11:27
  • $\begingroup$ @LukasLang I just name it after message name General::maxrec: which claims that Mma thinks it is recursion somewhere. At least this message is not issued for, say, toSort = Subsets[Range[10]][[2 ;;]]; or smaller lists. $\endgroup$
    – Acus
    Commented Jul 22, 2019 at 11:55
  • 3
    $\begingroup$ The change was intentional, to address crashes from deep recursion in pattern matching. As noted in post, one can address this using more specific patterns that give rise to less need for dynamic backtracking in the pattern matching code. $\endgroup$ Commented Jul 30, 2019 at 16:21
  • $\begingroup$ @DanielLichtblau Can your post this information as an answer? $\endgroup$
    – Acus
    Commented Jul 31, 2019 at 13:47
  • 4
    $\begingroup$ Side note: AllTrue[DeleteDuplicates[Flatten[toSort]], (Positive[#] && IntegerQ[#] &)] is faster than using Union. And if you need it sorted, Sort@DeleteDuplicates[Flatten[toSort]] is faster than Union, too, on the example toSort. $\endgroup$
    – Michael E2
    Commented Jul 31, 2019 at 16:13

1 Answer 1

12
$\begingroup$

The change was intentional, to address crashes from deep recursion in pattern matching.

As noted in post, one can address this using more specific patterns that give rise to less need for dynamic backtracking in the pattern matching code.

Anecdote: A corporate customer got bitten by it and complained (not terribly loudly, I'm happy to say). But... they were the ones who pushed hardest for the change to begin with.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.