Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Take for example

head[b_[PatternSequence[_, _]]] := 9

I know this example serves no purpose as is. It also happens in useful examples such as

head[b_[_, arg : PatternSequence[_, _]]] := {arg, b}

The definition is made propely, but it issues the message

Pattern::patvar: First element in pattern Pattern[1,_] is not a valid pattern name. >>

A simpler example of the problem, suggested by @celtschk in comments

b_[PatternSequence[_, _]] :> 0

and

b_[PatternSequence[]] :> 0

Particularly interesting is an example @OleksandrR found, in which the mysterious numbering of patterns becomes more evident

(p : {a_, b_, c_})[PatternSequence[]] -> Null

Pattern::patvar: "First element in pattern Pattern[4,{Pattern[1,_],Pattern[2,_],Pattern[3,_]}] is not a valid pattern name."

share|improve this question
    
Really strange! I'm looking forward to the answer to this one if it's not simply a bug. –  Mr.Wizard May 21 '12 at 22:37
1  
It is also issued for x /. b_[PatternSequence[_, _]] :> 0 — note the absence of head. It happens neither for b[PatternSequence[_,_]] nor for b_[_,_], so obviously it is triggered when a pattern as head is combined with a PatternSequence in the corresponding argument list. –  celtschk May 21 '12 at 22:48
1  
@celtschk, interesting. I'll add to that that it is triggered when a NAMED pattern as a head. If you remove the b it works fine –  Rojo May 21 '12 at 22:51
1  
Also it's essential that there's either more or less than one argument in the PatternSequence. b_[PatternSequence[_]] doesn't trigger the message, nor does b_[PatternSequence[__]], but both b_[PatternSequence[_, _, _]] and b_[PatternSequence[]] do. Also, applying the replacement rule isn't necessary; already using the pattern in b_[PatternSequence[]] :> 0 triggers the mesage (however just writing the pattern without the :> 0 doesn't). –  celtschk May 21 '12 at 22:57
2  
More than a workaround, I'd like to know WHY this message is issued. –  becko May 30 '12 at 1:53

2 Answers 2

This is a reported bug in the pattern matcher.

share|improve this answer
3  
Did you report this yourself? Or, do you have other confirmation of this? –  rcollyer May 31 '12 at 16:42
1  
Yes I reported it –  M.R. May 31 '12 at 17:37
    
Alright, I had reported it today too. +1 –  Rojo May 31 '12 at 18:05
    
+1 3 hours left for the bounty to expire. I guess I'll award it here. @Rojo I suppose this question should be tagged a bug then, shouldn't it? –  becko Jun 1 '12 at 1:37

Repeated (..) command may help to solve some of the problems. I think the problem is about repeated patterns. All below examples works with no warning

head[b_[PatternSequence[_, _]]] := 9 (*err*)
head[b_[PatternSequence[_ ..]]] := 9 (*works*)

b_[PatternSequence[(_)..]]:>0

b_[PatternSequence[__ ..]] :> 0 

b_[PatternSequence[(__)..]]:>0

head[b_[_, arg : PatternSequence[_, _]]] := {arg, b} (*err*)
head[b_[_, arg : PatternSequence[_ ..]]] := {arg, b} (*works*)

EDIT 1

head[___, b_[__, arg : PatternSequence[__ ..]]] := {b, {arg}}
head[f[1, 2, 3, 4]]

outputs with no warning but trims 1 element opps!

{f, {2, 3, 4}}

EDIT 2

head9[___, b_[___, arg : PatternSequence[__]]] := {b, {arg}}
head9[f[1, 2, 3, 4]]

outputs

 {f, {1, 2, 3, 4}}
share|improve this answer
1  
However both patterns are not equivalent. The second one matches head[f[1,2,3]] which the original one doesn't. Similar for the others. –  celtschk May 28 '12 at 21:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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