17
$\begingroup$

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."

$\endgroup$
36
  • $\begingroup$ Really strange! I'm looking forward to the answer to this one if it's not simply a bug. $\endgroup$
    – Mr.Wizard
    Commented May 21, 2012 at 22:37
  • 1
    $\begingroup$ 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. $\endgroup$
    – celtschk
    Commented May 21, 2012 at 22:48
  • 1
    $\begingroup$ @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 $\endgroup$
    – Rojo
    Commented May 21, 2012 at 22:51
  • 1
    $\begingroup$ 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). $\endgroup$
    – celtschk
    Commented May 21, 2012 at 22:57
  • 2
    $\begingroup$ More than a workaround, I'd like to know WHY this message is issued. $\endgroup$
    – a06e
    Commented May 30, 2012 at 1:53

2 Answers 2

9
+50
$\begingroup$

This is a reported bug in the pattern matcher.

$\endgroup$
4
  • 3
    $\begingroup$ Did you report this yourself? Or, do you have other confirmation of this? $\endgroup$
    – rcollyer
    Commented May 31, 2012 at 16:42
  • 1
    $\begingroup$ Yes I reported it $\endgroup$
    – M.R.
    Commented May 31, 2012 at 17:37
  • $\begingroup$ Alright, I had reported it today too. +1 $\endgroup$
    – Rojo
    Commented May 31, 2012 at 18:05
  • $\begingroup$ +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? $\endgroup$
    – a06e
    Commented Jun 1, 2012 at 1:37
1
$\begingroup$

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}}
$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$
    – celtschk
    Commented May 28, 2012 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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