9
$\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$
  • $\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 Jul 22 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$ – user18792 Jul 22 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$ – Daniel Lichtblau Jul 30 at 16:21
  • $\begingroup$ @DanielLichtblau Can your post this information as an answer? $\endgroup$ – user18792 Jul 31 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 Jul 31 at 16:13
9
$\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, privacy policy and cookie policy

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