3
$\begingroup$

Why is it that

fun[x, y] /. fun[z__] :> z

leads to

Sequence[x,y]

but

Dot[x, y] /. Dot[z__] :> z

gives

Dot[x,y]

I want it to be

Sequence[x,y]

Makes no sense. How do I make it work like I think it should?

$\endgroup$
3
  • $\begingroup$ I thought it was related to the attribute OneIdentity (or Flat) of Dot but I can't reproduce the behaviour using a custom head that has these attributes set. $\endgroup$
    – Szabolcs
    Commented Jan 24, 2014 at 19:04
  • $\begingroup$ Yeah, I thought it was that too. But it turned out not to be. $\endgroup$
    – QuantumDot
    Commented Jan 24, 2014 at 19:04
  • $\begingroup$ Related: Pattern matching on Orderless functions inside Hold $\endgroup$
    – Mr.Wizard
    Commented Jan 24, 2014 at 19:48

2 Answers 2

10
$\begingroup$

The problem is that Dot[z__] is evaluated to z__

The solution:

 Dot[x, y] /. HoldPattern[Dot[z__]] :> z

Sequence[x, y]

$\endgroup$
1
  • 1
    $\begingroup$ Alternatives: Dot[x, y] /. Verbatim[Dot][z__] :> z and Dot[x, y] /. (p : Dot)[z__] :> z $\endgroup$
    – Mr.Wizard
    Commented Jan 25, 2014 at 1:06
1
$\begingroup$

While this doesn't explain the behavior you notice (andre's answer does) and all the right methods have been covered here (See Mr.Wizard's alternatives), I found a nice work-around that will work with any Head (replace Dot with your Head of choice). Here it is:

Dot[x, y] /. z_Dot :> Sequence @@ z

(* Sequence[x, y] *)
$\endgroup$
3
  • $\begingroup$ This is a good alternative. Be aware that there is an evaluation difference that affects held expressions, e.g.: Hold[{Dot[x, y]}] /. z_Dot :> q[Sequence @@ z] (or more simply z_Dot :> q @@ z) versus Hold[{Dot[x, y]}] /. Verbatim[Dot][z__] :> q[z] $\endgroup$
    – Mr.Wizard
    Commented Jan 26, 2014 at 22:53
  • $\begingroup$ @Mr.Wizard. Thanks, that's an interesting example and I guess one can always use Trott-Strzebonski to save the day Hold[{Dot[x, y]}] /. z_Dot :> With[{s = q[Sequence @@ z]}, s /; True] $\endgroup$
    – RunnyKine
    Commented Jan 26, 2014 at 23:13
  • $\begingroup$ That is yet another evaluation sequence because you are inducing evaluation where there otherwise would be none. As a counterexample consider: Hold[{Dot[1, 2]}] /. Verbatim[Dot][z__] :> +z. I think you will find this hard to do in a single step using z_Dot :> . . .; in fact if you can I want to know about it because I can't think of a solution. $\endgroup$
    – Mr.Wizard
    Commented Jan 27, 2014 at 4:09

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.