Optional pattern behaves differently in version 8, 9, and 10+
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
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 integern
is optional anInfinity
per defaultn
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.
pattern-matching argument-patterns compatibility
 |Â
show 2 more comments
up vote
8
down vote
favorite
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 integern
is optional anInfinity
per defaultn
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.
pattern-matching argument-patterns compatibility
1
In M9 and earlier, the issue is thatInfinity
does not have the headInteger
. 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 theInteger
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
 |Â
show 2 more comments
up vote
8
down vote
favorite
up vote
8
down vote
favorite
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 integern
is optional anInfinity
per defaultn
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.
pattern-matching argument-patterns compatibility
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 integern
is optional anInfinity
per defaultn
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.
pattern-matching argument-patterns compatibility
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 thatInfinity
does not have the headInteger
. 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 theInteger
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
 |Â
show 2 more comments
1
In M9 and earlier, the issue is thatInfinity
does not have the headInteger
. 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 theInteger
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
 |Â
show 2 more comments
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
In M9 and earlier, the issue is that
Infinity
does not have the headInteger
. 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 theInteger
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