Skip to main content

programming - Unexpected behavior of Unevaluated



This question of mine got a bit ruined by the fact that I got a bit confused. I hope this separate question will take away this confusion. My confusion was that I found it strange that we have, for


x = 3;
g[_Symbol] := "yay"

that as expected


g[Unevaluated[x]]


"yay"


but


g[_Symbol] := "yay"
g[Sequence[Unevaluated[x]]]


g[Unevaluated[x]]

and even


With[
{yyyy = g[Sequence[Unevaluated[x]]]}

, Identity[yyyy]
]


g[Unevaluated[x]]

The strange thing here is that even though g[Unevaluated[x]] is not in its "final form" (at a fixed point), in the sense that a rule can be applied to this as we can see above, Mathematica stops evaluating. I show the third example in which With occurs, because one might have thought that the behavior occurs because Mathematica assumes that rules for g have already been applied or something. But even when we use With (or in fact, Identity, With is really not necessary) to start a "clean evaluation", Mathematica refuses to do the last step.


For a little while, I had the following question/hypothesis about this: "Does Mathematica remember if an expression has been fully evaluated?". Using that I found another similar example. We have


Clear[h, somethingElse, something]
h[something, something = somethingElse]



h[something, somethingElse]

even though, if we evaluate the resulting expression again, we have


h[something, somethingElse]


h[somethingElse,somethingElse]


But this time, we have


Clear[h, somethingElse, something]
Identity[h[something, something = somethingElse]]


h[somethingElse, somethingElse]

So that Mathematica does continue evaluation in this case. The same happens for a user-defined Identity. Note that the following does not result in a "fixed point" (expression that is left unchanged by the rules).


Clear[h, somethingElse, something]
List[h[something, something = somethingElse]]



{h[something, somethingElse]}

Which probably makes sense. I guess Identity is defined in terms of a rule, and after a rule we have to evaluate again. However, if we put something in a list, we can assume that the thing inside the list was evaluated correctly so we don't have to evaluate again.


Little tentative conclusion: It is not true that Mathematica repeatedly evaluates an expression until it does not change anymore. It seems to be a little more subtle than that.


The question is: Is it a bug that


g[_Symbol] := "yay"
Identity[g[Sequence[Unevaluated[x]]]]


Evaluates to



 g[Unevaluated[x]]

?




Comments