5
$\begingroup$

I am unclear about the OwnValues for Set and SetDelayed.

Remove["Global`*"]
ySet = a; ySetDelayed := b;
{OwnValues[ySet], OwnValues[ySetDelayed]}

Out[10]= {{HoldPattern[ySet] :> a}, {HoldPattern[ySetDelayed] :> b}}

Let me clarify why using :> seems strange to me.

The Wofram documentation for "How to | Create and Use Rules" says

In[11]:= x -> 3
Out[11]= x -> 3

By looking at the output for x->3, you can see that this rule does not do anything: the output is simply the rule itself. This is because rules do not do anything when they are alone. You must use a rule with an expression for it to be of any use.

  • Since Rules like -> and :> are used with /., why do they appear in OwnValues since OwnValues has to do with definitions? There must be some implied assumption for using them.

  • Why does it store them as RuleDelayed (:>) for both Set and SetDelayed?

  • For ySet = a;, why is it {HoldPattern[ySet] :> a} rather than {HoldPattern[ySet] -> a}?

There is a discuss of this issue at "Question on Definition and values of symbols", but no one seems to know.

Why do I care? To properly understand and program Mathematica, one needs to understand the evaluation process. verbeia.com has a detailed granular outline of "The Evaluation Process" which is more detailed than the Wolfram documentation. Knowing how definitions are processed is important so knowing how Rule and RuleDelayed fit into the picture is necessary.

$\endgroup$
  • 1
    $\begingroup$ An informal and incomplete attempt to answer the first question: Exaggerating a bit, the Mathematica kernel can do basically one thing (and it can do that pretty well) and that is replacing expressions. The whole language (as far as I can tell) is based on the concept of replacements. Metaphorically, it is the kernel itself that calls Replace and ReplaceAll during evaluation. All the other functionality (like the concept of Function) is emulated from this basic principle. $\endgroup$ – Henrik Schumacher Jan 3 '18 at 23:05
3
$\begingroup$

In your example:

ySet = a;
OwnValues[ySet]

{HoldPattern[ySet] :> a}

If Rule were used instead of RuleDelayed, then the OwnValues of ySet would change whenever a changed. For example, if we had:

ownValues = {HoldPattern[ySet] -> a}
a=1;
ownValues

{HoldPattern[ySet] -> a}

{HoldPattern[ySet] -> 1}

OwnValues of ySet should only change Set or SetDelayed is used on ySet. A better example demonstrating the difference between Set and SetDelayed is:

ySet = 1+1;
ySetDelayed := 1+1;

OwnValues[ySet]
OwnValues[ySetDelayed]

{HoldPattern[ySet] :> 2}

{HoldPattern[ySetDelayed] :> 1 + 1}

$\endgroup$
0
$\begingroup$

This seems fine:

Clear[ySet, a]
{HoldPattern[ySet] :> a, HoldPattern[ySet] -> a}

But this suggest a problem if we were to use Rule:

Clear[ySet, a]
a = 1;
{HoldPattern[ySet] :> a, HoldPattern[ySet] -> a}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.