8
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$
    – march
    Commented Aug 18, 2016 at 17:48
  • $\begingroup$ @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. $\endgroup$
    – masterxilo
    Commented Aug 18, 2016 at 18:03
  • $\begingroup$ 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. $\endgroup$
    – BoLe
    Commented Aug 18, 2016 at 19:16
  • $\begingroup$ OwnValues in Aside looks the same in both cases, but a doesn't behave the same; definition a := RandomReal[] is equivalent to the first. $\endgroup$
    – BoLe
    Commented Aug 18, 2016 at 19:22
  • $\begingroup$ 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. $\endgroup$
    – xzczd
    Commented Sep 18, 2016 at 6:33

1 Answer 1

3
$\begingroup$

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 -> {}}]
$\endgroup$
6
  • 2
    $\begingroup$ 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. $\endgroup$
    – xzczd
    Commented Oct 18, 2016 at 1:31
  • 1
    $\begingroup$ @xzczd Good catch! I guess that's why Language`DefinitionList has HoldAll. $\endgroup$
    – Mr.Wizard
    Commented Oct 18, 2016 at 8:17
  • $\begingroup$ 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? $\endgroup$
    – yode
    Commented Mar 5, 2017 at 10:30
  • $\begingroup$ @yode I am falling asleep. I will have to answer tomorrow. Please remind me as I will probably forget. $\endgroup$
    – Mr.Wizard
    Commented Mar 5, 2017 at 10:31
  • $\begingroup$ Sorry for disturbing..Good night. :) $\endgroup$
    – yode
    Commented Mar 5, 2017 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.