Optional pattern behaves differently in version 8, 9, and 10+

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite
1












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.







share|improve this question


















  • 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














up vote
8
down vote

favorite
1












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.







share|improve this question


















  • 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












up vote
8
down vote

favorite
1









up vote
8
down vote

favorite
1






1





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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








edited Aug 18 at 8:39









xzczd

23.8k364224




23.8k364224










asked Aug 18 at 8:14









halirutan♦

92.4k5210403




92.4k5210403







  • 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












  • 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







1




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




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





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




@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




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




@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










1 Answer
1






active

oldest

votes

















up vote
9
down vote













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.






share|improve this answer
















  • 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




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180206%2foptional-pattern-behaves-differently-in-version-8-9-and-10%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
9
down vote













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.






share|improve this answer
















  • 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














up vote
9
down vote













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.






share|improve this answer
















  • 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












up vote
9
down vote










up vote
9
down vote









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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 18 at 8:38









Carl Woll

54.7k269143




54.7k269143







  • 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












  • 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







2




2




+1. The original behaviour and changes are discussed in (108636), (4937) and on SE in (6401255).
– WReach
Aug 18 at 12:29




+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




@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




@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




@WReach I see. That explains it.
– Leonid Shifrin
Aug 18 at 19:52












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180206%2foptional-pattern-behaves-differently-in-version-8-9-and-10%23new-answer', 'question_page');

);

Post as a guest













































































這個網誌中的熱門文章

How to combine Bézier curves to a surface?

Mutual Information Always Non-negative

Why am i infinitely getting the same tweet with the Twitter Search API?