# Why does Mathematica appear to change the head of certain expressions?

I don't know if the question trivial or not (if so I will delete it), but here is what I have:

I know that

x x
(*  x^2  *)


Looking at the full form gives

FullForm[Times[x, x]]
(* Power[x, 2] *)


It is clear that the Times head has been changed to Power head. I just wonder how does this happen. Can I control this behavior to stop this conversion between heads?

-

We can use this function to see that the conversion is not made during parsing:

parseString[s_String, prep : (True | False) : True] :=
FrontEndExecute[FrontEndUndocumentedTestFEParserPacket[s, prep]]

parseString["x x"]

{BoxData[RowBox[{"x", "x"}]], StandardForm}


We can use this to see that the conversion does not take place while converting boxes to StandardForm:

ToExpression[RowBox[{"x", "x"}], StandardForm, HoldComplete] // FullForm

HoldComplete[Times[x,x]]


We can see that the rule is applied as part of the normal evaluation sequence:

Times[x, x] // TracePrint


x x

Times

x

x

(x^2)

Power

x

2

You cannot generally "stop this conversion" but you can temporarily disable the rules for Times using Block:

Block[{Times},
ToString[x x]
]

"Times[x, x]"


You can also Hold or HoldComplete expressions and apply your own rules.

Neither of these methods lets Times otherwise behave as desired. I have lamented this problem myself with regard to Subtract and Divide as the "equivalent" forms they are converted into are neither equivalent nor as fast.

-
Mr.Wizard, do you have new insight in the problem with the short forms you linked to? You said in one of the comments you might post a partial solution at some point. I'm sure many people would be interested (including me)! – sebhofer Aug 3 '14 at 9:42
@sebhofer I wish I did. I tried a few things and I didn't come up with anything I wanted to use. However it's been a long time and I didn't try too hard as I was hoping someone else had a better idea. – Mr.Wizard Aug 3 '14 at 14:44

Mathematica embraces the notion of canonical form. Internally it wants all expressions to be reduced to their canonical forms.

Times[x, x] is not a canonical form, so it gets reduced to Power[x, 2]`, which is.

I share Mr.Wizard's occasional frustration over the choices of canon made by Wolfram Research, but I think it is far too late to expect any change. So grin and bear it, as we say here in the US.

-
thank you for the explanation and for the advice I am smiling right now while bearing the fact :) – Algohi Aug 3 '14 at 17:26