While reading this answer I feel confused. If we do not care any specific detail of the original question, then ybeltukov's answer essentially makes use of the following fact
h_[a___, G[x_, ___, y_], b___] ^:= y[x] G[2, e]
This example shows that unlike
TagSetDelayed which imperatively require that the assignment is associated to some symbol called assignment tag,
UpSetDelayed can be used to make assignment associated to pattern object as well (like the
h_ in this example).
However, when executing
G[2, e] it seems that the evaluator treats the flat input
G[2, e] as
ghostHead_[G[2, e]] such that the user-defined
G can be applied.
I cannot understand this. I assume there is some very fundamental mechanism taking effect in this example. Could someone point out the missing knowledge?
I am also confused about the use of
Simplify in ybeltukov's answer. Why
Simplify can reduce
G[2, e] to
e? I have not found any documentation revealing this.
As Szabolcs points out in the comment, because the
StandardForm is by default used by the Front End for output, the plain input
G[2,e] is evaluated to
G[2,e] then wrapped as
which applies to the
UpValues defined by
h_[a___, G[x_, ___, y_], b___] ^:= y[x]
where, according to the standard Wolfram Language evaluation process, the
G is used prior to the
StandardFrom, so that the
StandardFrom is shadowed.
This is a side effect of the Front End wrapping
StandardForm to the output, but we may make use of it.
We can also understand ybeltukov's answer which is,
h_[a___, G[x_, ___, y_], b___] ^:= h[a, y[x], b] Simplify[G[2,e]]
Simplify also acts as a wrapper whose
DownValues is also shadowed, and according to the upvalue definition,
Simplify[G[2,e]] is evaluated as
Simplify[e] which further reduces to
e according to the definition of
With such understanding, we can replace the
this will return the same result but with better efficiency, because
StandardForm does not require searching for applicable transformation rules.