We recently ran into an issue when testing Rubi on older versions. It boils down to this simple example which I didn't dare to simplify further as I cannot test it in versions < 10

Test[XXX[expr_, x_], n_Integer : Infinity, OptionsPattern[]] := True /;
  Head[x] === Symbol && n > 0;
Test[___] := False;
XXX[blub, x] // Test

The important part is n in the pattern and it should say

  • n is an integer
  • n is optional an Infinity per default
  • n must be greater zero

Above code gives False in versions 8 and 9, but True in version 10 and 11. I'm not looking for a workaround, but I'd like to understand why it returns False in the older versions.

Maybe someone who still has access can give me some insight.

  • 1
    In M9 and earlier, the issue is that Infinity does not have the head Integer. I believe they relaxed the pattern matcher in M10 to allow such patterns. – Carl Woll Aug 18 at 8:20
  • I also had this idea, but I was under the impression that the default parameter does not have to have the head from the pattern. E.g. f[x_Integer: "Error"] := x; f[] returns "Error" although it also has not the Integer head. – halirutan Aug 18 at 8:22
  • @CarlWoll Ah, OK. Now I understand your comment. You mean in earlier versions the head of the default must match if there is a restriction on the pattern, right? – halirutan Aug 18 at 8:25
  • Yes, that's right. – Carl Woll Aug 18 at 8:25
  • @CarlWoll That is a completely reasonable explanation. If you post it as answer and someone with 8 or 9 can test it, I'm happy to accept it. – halirutan Aug 18 at 8:27

In M9 and earlier, the default value must match any pattern restrictions. In your example, the head of the default is not Integer, and so the default value could never be triggered. In either 10.2 or 10.3 the pattern matcher was relaxed so that the default value no longer needed to match pattern restrictions.

  • 2
    +1. The original behaviour and changes are discussed in (108636), (4937) and on SE in (6401255). – WReach Aug 18 at 12:29
  • @WReach I wonder how you found all these references. I certainly wouldn't. – Leonid Shifrin Aug 18 at 17:08
  • @Leonid I remembered that one of these questions existed because it had appeared around the time I had sent feedback to WRI about subtle interactions between facilities with implicit pattern expansion (Flat, OneIdentity, Optional, etc.). Once I found the first question, the others were linked. – WReach Aug 18 at 18:19
  • @WReach I see. That explains it. – Leonid Shifrin Aug 18 at 19:52

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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