Wagner's book says that, in the Standard Evaluation Procedure,
- 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.
Answer
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. forPlus[1, RandomReal[]]withRandomReal[]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 -> {}}]
Comments
Post a Comment