       Re: Mathematica Evaluation Process

• To: mathgroup at smc.vnet.net
• Subject: [mg39655] Re: [mg39597] Mathematica Evaluation Process
• From: Omega Consulting <info at omegaconsultinggroup.com>
• Date: Thu, 27 Feb 2003 00:33:42 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Boy, those are some tough questions. I doubt there are many people who
could answer all of these. But I'll try and add what I know.

At 01:57 AM 2/25/2003, Ersek, Ted R wrote:
>For a long time I have wanted to include on my webstite an explanation of
>all processing an expression goes through from input to output (ignoring the
>
>Omega Consulting just put out a partial explanation at
>http://omegaconsultinggroup.com/Services/ezv2i02.html
>
>Many years ago David Withoff gave the explanation posted at
>http://library.wolfram.com/database/Conferences/4683/
>
>As far as I am aware the most complete explanation was giiven by David
>Wagner
>in his book "Power Programming With Mathematica The Kernel".  Unfortunately
>this wonderful book is out of print!
>
>
>However, there are some things that have never been explained as far as I
>know.
>
>(1)   How does pattern matching and all the tricky things that go with it
>(HoldPattern, Verbatim, OneIdentity, DefaultValues, ...)  fit into the
>Mathematica evaluation process?

I don't think Verbatim, HoldPattern, and DefaultValues are additional steps
in the evaluation process. What they do is alter the pattern matching
during the processing of UpValues, DownValues, and SubValues.

As for OneIdentity, that must be processed before pattern matching occurs
(perhaps around the same time as Flat, see A.4.1 of The Mathematica Book).
That's just a guess. I don't know for sure.

In a related issue, when processing the patterns in the standard evaluation
scheme, the patterns in definitions are applied to see if the expression
matches the pattern. If the arguments have been kept unevaluated (via
Unevaluated or a holding attribute), this can have some unusual effects.

If you use a structural pattern (such as x_List), no evaluation occurs, the
unevaluated structure is simply compared against the pattern. But if the
pattern contains a test, then the test is evaluated, which can evaluate the
argument whether or not the test succeeds. If the test does succeed, the
pattern is matched against the unevaluated expression. So you can get
double evaluation.

In:=g[x_List] := x

In:=g[x_] := 2

In:=g[Unevaluated[Print["arg"];{1,2,3}]]

Out=2

In:=f[x_?ListQ] := x

In:=f[x_] := 2

In:=f[Unevaluated[Print["arg"];{1,2,3}]]
From In:=arg
From In:=arg
Out={1,2,3}

>(2)   How do definitions assigned to  \$PreRead, \$Pre, \$Post, \$PrePrint,
>MakeExpression, MakeBoxes, Format[symb]  fit into the evaluation process?  I
>tried to explain what these things do and the order in which they are used
>on my website.
>but I couldn't  determine with good confidence where they fit in the
>evaluation process.
>
>(3)   The definition   foo[a_][x_,y_]:={a,x^2-y^2}  is stored in
>SubValues[foo].  When in the evaluation process is such a definition used?

According to A.4.1 of The Mathematica Book, they're evaluated at the same
time as DownValues.

>(4)   A definitions for  N[foo[x_]]  is stored in  NValues[foo].  When in
>the evaluation process are the NValues used?

It was always my understanding that NValues worked like this.

N[foo[x]] first evaluates foo[x].
If foo[x] evaluates to something, it then does N[something].
If foo[x] remains unevaluated, then it uses the NValue.

In:=f:=0

In:=N[f[x_]]:= N[x]

In:=N[f]
Out=0.

In:=N[f]
Out=2.

So the NValue acts similar to an UpValue. (like f /: N[f[...]] := ...)

>(5)   When in the evaluation process does the kernel use or change
>assignments to \$IterationLimit, \$HistoryLength, \$MessageList, Out[n]?
>
>----------------------
>Regards,
>    Ted Ersek

--------------------------------------------------------------
Omega Consulting