Unevaluated, Plus and Times (was Re: Re: Re: Re: Mathematica language issues)
- To: mathgroup at smc.vnet.net
- Subject: [mg53173] Unevaluated, Plus and Times (was Re: [mg53112] Re: [mg53050] Re: Re: Mathematica language issues)
- From: "Fred Simons" <f.h.simons at tue.nl>
- Date: Mon, 27 Dec 2004 06:41:53 -0500 (EST)
- References: <200412241058.FAA05777@smc.vnet.net> <opsjit5yewiz9bcq@monster.ma.dl.cox.net>
- Sender: owner-wri-mathgroup at wolfram.com
The discussion on the behaviour of Unevaluated in cases where it should not be used lasts already for a long time. Though irrelevant there are some interesting aspects in it. The general behaviour of Unevaluated has been explained by Andrzej Kozlowsky. Let me reformulate it very short and therefore inaccurate: when during the evaluation process the expression changes, the arguments that originally were wrapped in Unevaluated are not rewrapped. It is very important to note that changes only due to attributes do not count as an change. Examples have been given to demonstrate that Times and Plus behave differently. For example the expression Unevaluated[z]+2. Mathematica looks at z+2. The terms are reordered due to the attribute Orderless so Mathematica arrives at 2+z. That is the end of the evaluation. The change is only due to the attributes so we expect rewrapping of Unevaluated, that is the result 2+Unevaluated[z]. But the outcome is 2+z. In my previous mail I tried to explain this behaviour from the fact that the evaluation of expressions with head Plus or Times is slightly different. According to Bobby, that explanation was not very clear, so in this mail I try to do it better. There is evidence that as soon as the head Times or Plus of an expression is recognized, Mathematica does some sort of pre-evaluation of the arguments. All numerical arguments are multplied or added and the result is placed befor the other arguments. Only after this pre-evaluation the attributes of Times and Plus are applied and the evaluation continues. I will only consider the function Plus. The pre-evaluation of the arguments can be seen from the following function preplus, in which the rules used by Mathematica are 'caught': Attributes[preplus]={HoldAllComplete}; preplus[x___] := Block[{Plus}, plus@@Plus[x]] Now we can imitate the evaluation of an expression with head Plus by first doing the pre-evaluation with the function preplus and then apply Plus to the result. Here are some examples. Evaluation of the expression 2+Unevaluated[z] is done in the following way: In[12]:= preplus[2,Unevaluated[z]] Plus @@ % Out[12]= plus[2,Unevaluated[z]] Out[13]= 2+Unevaluated[z] The pre-evaluation does not change the arguments, so in the input of Plus the argument z is still wrapped in Unevaluated. Plus do not change the expression so at the end the argument z is wrapped in Unevaluated. The expression 1+1+Unevaluated[z] gives a different result: In[14]:= preplus[1,1,Unevaluated[z]] Plus @@ % Out[14]= plus[2,z] Out[15]= 2+z The pre-evaluation changed the arguments so Unevaluated has completely disappeared in the input of Plus. The expression 2+Unevaluated[z+2]: In[16]:= preplus[2, Unevaluated[z+2]] Plus @@ % Out[16]= plus[2,Unevaluated[z+2]] Out[17]= 4+z Here the pre-evaluation has no effect, so after applying the attributes Flat and Orderless, Plus is called with arguments 2, 2 and z. Plus has no rules for these combination of argument, so it has to return Plus[2,2, Unevaluated[z]]. But then the evaluation of course continues: In[18]:= preplus[2, 2, Unevaluated[z]] Plus @@ % Out[18]= plus[4,z] Out[19]= 4+z Pre-evaluation of the arguments gives a new set of arguments, so z is no longer wrapped in Unevaluated, and the outcome is 4+z. My conclusion is that the result of all curious examples with head Plus or Times where some of the arguments supplied with Unevaluated can be predicted in the way Andrzej has described when one takes into account the pre-evaluation, as I did in the above examples. Fred Simons Eindhoven Ubiversity of Technology
- References:
- Re: Re: Re: Mathematica language issues
- From: "Fred Simons" <f.h.simons@tue.nl>
- Re: Re: Re: Mathematica language issues