HoldPattern
is a persistent head that prevents evaluation of its argument(s) yet is transparent to pattern matching. Unevaluated
is a temporary wrapper that prevents the evaluation of a single argument* by a single head at one point in the evaluation, and then is stripped. Once it is stripped it cannot have any further effect on the evaluation.
You can use Unevaluated
in your example pattern, but it must wrap the entire rule rather than only the left hand side:
{g, 3*(10 + g)} /. Unevaluated[Plus[t__] :> {t}] (* simplified RHS for clarity *)
{g, {30, 3 g}}
Unevaluated[Plus[t__]] :> {t}
would not work even if Unevaluated
acted normally(1) because it would only prevent RuleDelayed
from evaluating Plus[t__]
but the entire rule would be (re)evaluated by ReplaceAll
and Plus[t__]
reduced to t__
.
* Although undocumented and even marked in red as a syntax error Unevaluated
will accept multiple arguments, and when activated it effectively transforms into Sequence
while simultaneously holding. This transformation takes place after the Sequence would be flattened in the normal evaluation procedure, so it remains as a single expression even if the head (f
below) does not have SequenceHold
or HoldAllComplete
.
ClearAll[f]
f[x_] := HoldComplete[x]
f[Unevaluated[1 + 1, 2 + 2, 3 + 3]]
HoldComplete[Sequence[1 + 1, 2 + 2, 3 + 3]]
Observe that Sequence[1 + 1, 2 + 2, 3 + 3]
is bound to the single parameter pattern x_
, rather than becoming f[1 + 1, 2 + 2, 3 + 3]
which would not match the definition given. In other words the behavior is the same as:
f[Unevaluated @ Sequence[1 + 1, 2 + 2, 3 + 3]]
Hold
(or anything with aHold*
attribute) andUnevaluated
. Google for this and you'll find several discussions (many point to the Villegas tutorial).Unevaluated
gets stripped out in the first "evaluation step". Its argument will get evaluated during the second step. $\endgroup$Unevaluated[1 + 1]
,Unevaluated[1 + 1] + 1
andHold[1 + 1] + 1
andTracePrint
their evaluations. The first one is a special case because evaluation stops whenUnevaluated[...]
reach the top level. The second one evaluates to(1+1)+1
which then evaluates further immediately. $\endgroup$