Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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
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
@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
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
More than a workaround, I'd like to know WHY this message is issued. – becko May 30 '12 at 1:53

This is a reported bug in the pattern matcher.

share|improve this answer
Did you report this yourself? Or, do you have other confirmation of this? – rcollyer May 31 '12 at 16:42
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 


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


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}}


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


 {f, {1, 2, 3, 4}}
share|improve this answer
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


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.