# Help me understand this behaviour of the standard evaluation procedure with Unevaluated

Wagner's book says that, in the Standard Evaluation Procedure,

1. If no applicable rules where found and any of the part_i has the head Unevaluated, restore that head.

This explains why h[Unevaluated[RandomReal[]]] or plain Unevaluated[RandomReal[]] are inert.

However consider this:

plus[x_, y_] := x + y


Then

(A) plus[1., Unevaluated[RandomReal[]]] gives 1.81318

but

a = Unevaluated@Unevaluated[RandomReal[]]; plus[1., a] (one level of Unevaluated is stripped by Set, I think)

gives 1. + Unevaluated[RandomReal[]]. Shouldn't this behave exactly like (A)?

Aside

How come that after both a = Unevaluated[RandomReal[]] and a = Unevaluated@Unevaluated[RandomReal[]], OwnValues@a gives

{HoldPattern[a] :> RandomReal[]}

when clearly the second assignment behaves differently, e.g. in the example given herein?

Aside 2

If I use Plus, Plus[1., Unevaluated[RandomReal[]]] and a = Unevaluated@Unevaluated[RandomReal[]]; Plus[1., a] both give 1. + Unevaluated[RandomReal[]] as expected.

• And if instead you define plus = Plus, then plus[1., Unevaluated[RandomReal[]]] evaluates to 1. + Unevaluated[RandomReal[]], so it has something to do with finding DownValues of plus (in the sense that it is using a rule to replace plus[ stuff] with somethingelse[ stuff ] during which stuff gets evaluated perhaps, because it has to match stuff to the pattern rather than just the head plus. – march Aug 18 '16 at 17:48
• @march That "plus = Plus, then plus[1., Unevaluated[RandomReal[]]] evaluates to 1. + Unevaluated[RandomReal[]]" is consistent with my understanding of evaluation: heads are evaluated first, so my "Aside 2" applies here. – masterxilo Aug 18 '16 at 18:03
• I think Unevaluated acts as stated only when it appears explicitly as the argument. You can't set an expression wrapped Unevaluated to a variable and make it act the same way. – BoLe Aug 18 '16 at 19:16
• OwnValues in Aside looks the same in both cases, but a doesn't behave the same; definition a := RandomReal[] is equivalent to the first. – BoLe Aug 18 '16 at 19:22
• Essentially the same question as this one: mathematica.stackexchange.com/q/110499/1871, just try a // OwnValues // Trace. Sadly no one has answered that question so currently this can't be marked as a duplicate. – xzczd Sep 18 '16 at 6:33

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 LanguageExtendedFullDefinition returns the rules in a LanguageDefinitionList container which has HoldAll:

LanguageExtendedFullDefinition[a]

LanguageDefinitionList[HoldForm[a] ->
{OwnValues -> HoldPattern[a] :>
Unevaluated[RandomReal[]], SubValues -> {},
UpValues -> {}, DownValues -> {}, NValues -> {},
FormatValues -> {}, DefaultValues -> {},
Messages -> {}, Attributes -> {}}]

• Well, as to the aside 1, as mentioned in the comment above, I think the issue here is just "Rule or RuleDelayed will strip all Unevaluated when evaluated" i.e. those Unevaluateds are actually stored in OwnValues but are simply "killed" when OwnValues@a is executed. Just try a // OwnValues // Trace. – xzczd Oct 18 '16 at 1:31
• @xzczd Good catch! I guess that's why LanguageDefinitionList has HoldAll. – Mr.Wizard Oct 18 '16 at 8:17
• I have read this theory more than 20+ times,but I cannot understand your must be wrapper before argument evaluation.So this theory can explain why Plus[2, Unevaluated[3]] can get result,but Plus[2, Unevaluated[RandomReal[]]]` not? – yode Mar 5 '17 at 10:30
• @yode I am falling asleep. I will have to answer tomorrow. Please remind me as I will probably forget. – Mr.Wizard Mar 5 '17 at 10:31
• Sorry for disturbing..Good night. :) – yode Mar 5 '17 at 10:32