I believe we can explain this behavior by referencing:
Unevaluated must be wrapper before argument evaluation, not after, else it isn't stripped.
Recall our discussion of the over-arching evaluator, and the fact that
your inputs and commands have two stages: their original form, and
the reduced form with arguments all evaluated.
Unevaluated is not meant to be a function or stable data type. It is
to be used as a wrapper on an argument in stage 1, before argument
evaluation. It is a signal to the evaluator to suppress the usual
evaluation of that argument.
...
Those of you who have experimented with Unevaluated have found that in some situations it doesn't vanish. This makes it seem confusing and inconsistent, like Sequence.
...
The subtle and confusing situation where Unevaluated persists is when an argument did not originally have a head of Unevaluated, but became Unevaluated[whatever] after argument evaluation finished.
Unevaluated
does not appear explicitly as the head of one of the arguments in plus[1., a]
, therefore Unevaluated[RandomReal[]]
is inserted into Plus
verbatim to become 1. + Unevaluated[RandomReal[]]
, which evaluates for the reason you described yourself in a comment:
There is no rule for Plus[1, Unevaluated[RandomReal[]]]
(i.e. for Plus[1, RandomReal[]]
with RandomReal[]
not evaluated to a number).
Aside 1
Revision
As xzczd noted in a comment Unevaluated
is stripped from the right-hand-side of RuleDelayed
when it (the rule expression) is evaluated. (Reference)
It appears in Definition
:
Definition[a]
a = Unevaluated[RandomReal[]]
Using my step
evaluation function with OwnValues
works too:
OwnValues[a] // step
{HoldPattern[a] :> Unevaluated[RandomReal[]]} (* HoldForm *)
The undocumented Language`ExtendedFullDefinition
returns the rules in a Language`DefinitionList
container which has HoldAll
:
Language`ExtendedFullDefinition[a]
Language`DefinitionList[HoldForm[a] ->
{OwnValues -> HoldPattern[a] :>
Unevaluated[RandomReal[]], SubValues -> {},
UpValues -> {}, DownValues -> {}, NValues -> {},
FormatValues -> {}, DefaultValues -> {},
Messages -> {}, Attributes -> {}}]
plus = Plus
, thenplus[1., Unevaluated[RandomReal[]]]
evaluates to1. + Unevaluated[RandomReal[]]
, so it has something to do with findingDownValues
ofplus
(in the sense that it is using a rule to replaceplus[ stuff]
withsomethingelse[ stuff ]
during whichstuff
gets evaluated perhaps, because it has to matchstuff
to the pattern rather than just the headplus
. $\endgroup$plus = Plus
, thenplus[1., Unevaluated[RandomReal[]]]
evaluates to1. + Unevaluated[RandomReal[]]
" is consistent with my understanding of evaluation: heads are evaluated first, so my "Aside 2" applies here. $\endgroup$Unevaluated
acts as stated only when it appears explicitly as the argument. You can't set an expression wrappedUnevaluated
to a variable and make it act the same way. $\endgroup$OwnValues
in Aside looks the same in both cases, buta
doesn't behave the same; definitiona := RandomReal[]
is equivalent to the first. $\endgroup$a // OwnValues // Trace
. Sadly no one has answered that question so currently this can't be marked as a duplicate. $\endgroup$