# Informal Equivalents of Mathematica “Set” and “SetDelayed”

How would one distinguish between what is meant by Mathematica's "Set" and "SetDelayed" functions in informal mathematical notation? Is there a way to make this distinction any any reasonably standard formal logics? Also, how is informal mathematical notation and formal logic notation related to Mathematica's use of variables like 'x_'? Here is the relevant part of the Mathematica documentation:

http://reference.wolfram.com/mathematica/howto/CreateDefinitionsForVariablesAndFunctions.html

-

Since Mathematica is fundamentally a rewriting system, we can use the concept of a Normal Form to address your question.

We can formalize the behavior of Mathematica this way to get at your question. Consider two sets of rewrite rules, which will store all the rules you enter into Mathematica. In the first set, which we'll call Eager, all the right-hand sides of the rules are rewritten immediately to their respective normal forms. In the second set, however, which we'll call Lazy, no such rewriting takes place. The correspondence with Mathematica is that Eager is the default, including the Set command and the read-eval-print loop; SetDelayed corresponds with Lazy.

Following the example in the Mathematica documentation, entering y = 4 at the prompt puts the rewrite rule $y \rightarrow 4$ into the Eager set:

Eager = $\{y \rightarrow 4\}$, Lazy = $\{\}$

Entering z := y^2 at the prompt puts the rule $z \rightarrow y^2$ into the Lazy set:

Eager = $\{y \rightarrow 4\}$, Lazy = $\{z \rightarrow y^2\}$

Entering z at the prompt causes z to be rewritten to its normal form using the union of Eager and Lazy.

$z \Rightarrow 16$

Now if we enter y = 3 at the prompt, the rule that's already there is replaced:

Eager = $\{y \rightarrow 3\}$, Lazy = $\{z \rightarrow y^2\}$

So entering z again causes it to be rewritten to its normal form:

$z \Rightarrow 9$

The logic underlying this example is equational, that is, first-order logic with functions and equality. In this setting, rewrite rules are simply equations with a preferred direction.

As for variables like x_, I don't see anything corresponding in rewrite rules to this kind of notation. My guess is that this formalism is designed to inhibit the default Mathematica evaluator, since if we already have x = 3 and type the following into Mathematica....

f[x] := x^2

... we'd get

f[3] := x^2

...which is clearly not what we want. Rewrite systems avoid this problem by not usually making constant bindings like x = 4 into rewrite rules. This is necessary since it's important to be able to rewrite the left-hand sides of rewrite rules into their normal forms, not just the right-hand sides.

-

QUESTION 1: How would one distinguish between what is meant by Mathematica's "Set" and "SetDelayed" functions in informal mathematical notation?

Set vs. SetDelayed

f[x_] = Expand[x^2]

x^2

{f[3], f[3.5], f[x + 1]}

{9, 12.25, (x+1)^2}

f[x+1] is not expanded because that was evaluated when f was defined.

Also Set is evaluated once for certain definitions.

r1 = Random[]; {r1, r1, r1}

{0.937245,0.937245,0.937245}

SetDelayed is evaluated each time.

r2 := Random[]; {r2, r2, r2}

{0.687744,0.629629,0.732141}

Clear["`*"]

f[x_] := Expand[x^2]

Notice when you evaluate this expression, there is no output. That's because the rhs is held until the function f is evaluated.

{f[24],f[x + 2],f[(h - 4)^3]}

{576, x^2+4 x+4, h^6-24 h^5+240 h^4-1280 h^3+3840 h^2-6144 h+4096}

Now everything works because Expand is evaluated after f gets its input.

QUESTION 3: Also, how is informal mathematical notation and formal logic notation related to Mathematica's use of variables like 'x_'?

The underscore is what Mathematica uses to show input variables. It is used with Set and SetDelayed. Even f[x_] is evaluated without :=.

f[x_]

x_^2

The human mind knows that the x in f(x) is the input. Computers don't unless you tell them.

QUESTION 2: Is there a way to make this distinction any any reasonably standard formal logics?

I have never seen it. The human mind would always evaluate like a SetDelayed definition, wouldn't it?

Here's a link to the notebook. Hope this helps.

-
It seems to me that Quine42 already knows this behavior, and is asking about representation or notation of said behavior outside of Mathematica. –  Mr.Wizard May 20 '11 at 7:49
Please, do answer his question, since you seem to understand it better. –  mathheadinc Jun 2 '11 at 7:01
I am not a mathematician. –  Mr.Wizard Jun 2 '11 at 12:09