# Non-Standard Evaluation

While most built-in *Mathematica* functions follow the standard evaluation procedure, some important ones do not. For example, most of the *Mathematica* functions associated with the construction and execution of programs use non-standard evaluation procedures. In typical cases, the functions either never evaluate some of their arguments, or do so in a special way under their own control.

x=y | do not evaluate the left-hand side |

If[p,a,b] | evaluate a if p is True, and b if it is False |

Do[expr,{n}] | evaluate expr n times |

Plot[f,{x,...}] | evaluate f with a sequence of numerical values for x |

Function[{x},body] | do not evaluate until the function is applied |

Some functions that use non-standard evaluation procedures.

When you give a definition such as , *Mathematica* does not evaluate the that appears on the left-hand side. You can see that there would be trouble if the was evaluated. The reason is that if you had previously set , then evaluating in the definition would put the definition into the nonsensical form .

In the standard evaluation procedure, each argument of a function is evaluated in turn. This is prevented by setting the attributes HoldFirst, HoldRest and HoldAll. These attributes make *Mathematica* "hold" particular arguments in an unevaluated form.

HoldFirst | do not evaluate the first argument |

HoldRest | evaluate only the first argument |

HoldAll | evaluate none of the arguments |

Attributes for holding function arguments in unevaluated form.

In[1]:= |

Out[1]= |

In[2]:= |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

Even though a function may have attributes which specify that it should hold certain arguments unevaluated, you can always explicitly tell *Mathematica* to evaluate those arguments by giving the arguments in the form Evaluate[arg].

In[6]:= |

Out[6]= |

f[Evaluate[arg]] | evaluate arg immediately, even though attributes of f may specify that it should be held |

Forcing the evaluation of function arguments.

By holding its arguments, a function can control when those arguments are evaluated. By using Evaluate, you can force the arguments to be evaluated immediately, rather than being evaluated under the control of the function. This capability is useful in a number of circumstances.

In[7]:= |

Out[7]= |

*value*of , namely is set to .

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

In most cases, you want all expressions you give to *Mathematica* to be evaluated. Sometimes, however, you may want to prevent the evaluation of certain expressions. For example, if you want to manipulate pieces of a *Mathematica* program symbolically, then you must prevent those pieces from being evaluated while you are manipulating them.

You can use the functions Hold and HoldForm to keep expressions unevaluated. These functions work simply by carrying the attribute HoldAll, which prevents their arguments from being evaluated. The functions provide "wrappers" inside which expressions remain unevaluated.

The difference between Hold[expr] and HoldForm[expr] is that in standard *Mathematica* output format, Hold is printed explicitly, while HoldForm is not. If you look at the full internal *Mathematica* form, you can however see both functions.

In[10]:= |

Out[10]= |

*Mathematica*output format.

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]//FullForm= | |

In[13]:= |

Out[13]= |

Hold[expr] | keep expr unevaluated |

HoldComplete[expr] | keep expr unevaluated and prevent upvalues associated with expr from being used |

HoldForm[expr] | keep expr unevaluated, and print without HoldForm |

ReleaseHold[expr] | remove Hold and HoldForm in expr |

Extract[expr,index,Hold] | get a part of expr, wrapping it with Hold to prevent evaluation |

Functions for handling unevaluated expressions.

In[14]:= |

Out[14]= |

In[15]:= |

Out[15]= |

f[...,Unevaluated[expr],...] | give expr unevaluated as an argument to f |

Temporary prevention of argument evaluation.

In[16]:= |

Out[16]= |

In[17]:= |

Out[17]= |

Unevaluated[expr] effectively works by temporarily giving a function an attribute like HoldFirst, and then supplying expr as an argument to the function.

SequenceHold | do not flatten out Sequence objects that appear as arguments |

HoldAllComplete | treat all arguments as completely inert |

Attributes for preventing other aspects of evaluation.

By setting the attribute HoldAll, you can prevent *Mathematica* from evaluating the arguments of a function. But even with this attribute set, *Mathematica* will still do some transformations on the arguments. By setting SequenceHold you can prevent it from flattening out Sequence objects that appear in the arguments. And by setting HoldAllComplete you can also inhibit the stripping of Unevaluated, and prevent *Mathematica* from using any upvalues it finds associated with the arguments.