2 amended mistake
source | link

The problem is not that Times is a built-in symbol. It is a consequence ofAs explained in another answer it stems from the Flat attributeevaluation of the Times. In this particular example, for in the purposeLHS of pattern matching Times[a, b] is considered the same as Times[Times[a,b]], hence the rule replaces x with Times[a,b] rather than. My previous explanation Sequence[a,b](see edit) was wrong and misleading.

Having said that, one way to make it work is to simply replace the Head

In[1]:= a b /. Times -> (g[##, 0] &)

Out[1]:= g[a, b, 0]

If you have more complicated replacement rules you can always replace Times with a new symbol e.g. newTimes, make it Orderless if needed and then apply the rules to newTimes.

The problem is not that Times is a built-in symbol. It is a consequence of the Flat attribute of Times. In this particular example, for the purpose of pattern matching Times[a, b] is considered the same as Times[Times[a,b]], hence the rule replaces x with Times[a,b] rather than Sequence[a,b].

Having said that, one way to make it work is to simply replace the Head

In[1]:= a b /. Times -> (g[##, 0] &)

Out[1]:= g[a, b, 0]

If you have more complicated replacement rules you can always replace Times with a new symbol e.g. newTimes, make it Orderless if needed and then apply the rules to newTimes.

The problem is not that Times is a built-in symbol. As explained in another answer it stems from the evaluation of the Times in the LHS of the rule. My previous explanation (see edit) was wrong and misleading.

Having said that, one way to make it work is to simply replace the Head

In[1]:= a b /. Times -> (g[##, 0] &)

Out[1]:= g[a, b, 0]

If you have more complicated replacement rules you can always replace Times with a new symbol e.g. newTimes, make it Orderless if needed and then apply the rules to newTimes.

1
source | link

The problem is not that Times is a built-in symbol. It is a consequence of the Flat attribute of Times. In this particular example, for the purpose of pattern matching Times[a, b] is considered the same as Times[Times[a,b]], hence the rule replaces x with Times[a,b] rather than Sequence[a,b].

Having said that, one way to make it work is to simply replace the Head

In[1]:= a b /. Times -> (g[##, 0] &)

Out[1]:= g[a, b, 0]

If you have more complicated replacement rules you can always replace Times with a new symbol e.g. newTimes, make it Orderless if needed and then apply the rules to newTimes.