5
Hold @ t[t[1,1],2]/.t-> Times
Hold @ t[t[1,2],2]/.t-> Times
Hold[(1 1) 2]
Hold[2 2]

Why doesn't the second one give Hold[(1 2) 2]? It seems to have to do with 1 appearing in there.

Hold @ t[t[2,2],2]/.t-> Times gives Hold[(2 2) 2] but

Hold @ t[t[2,2],1]/.t-> Times gives Hold[2 2]

The result is the same if I do the replacement with :> or //. or using With or a global replacement rule (function).

Maybe the default value of Times, 1, is being used in these replacements? Can anyone explain this?

Kuba
  • 136,707
  • 13
  • 279
  • 740
masterxilo
  • 5,739
  • 17
  • 39

2 Answers2

7

This is a specific case of a more general topic: Understanding evaluation and typesetting

It is not a bug, nothing is evaluated. Why then Times' "special" formatting is recognized for nested t[t[1,2],2] as it would have been evaluated?

It is because of special formatting rules defined for Times (any symbol in general). Also certain Attributes affect pattern-matching which is performed during formatting/typesetting (Format/MakeBoxes).

ClearAll[h];

SetAttributes[h, Orderless]
Format[h[1, x__]] := Row[{x}]

Hold @ t[t[2, 1], 2] /. t -> h

% // FullForm
Hold[h[2,2]]

Hold[h[h[2,1],2]]

Of course for Times more rules are defined, it is only a small example to show the point.


Flatis an attribute that can be assigned to a symbol f to indicate that all expressions involving nested functions f should be flattened out. This property is accounted for in pattern matching.

Orderless [...]. This property is accounted for in pattern matching.

Kuba
  • 136,707
  • 13
  • 279
  • 740
3

This is purely a formatting "issue": enter image description here

masterxilo
  • 5,739
  • 17
  • 39