14
$\begingroup$

I thought there was no difference between f[a_] = 2 + a; and f[a_] := 2 + a; because the resulting DownValues[f] are identical.

However, the Information[f] are different:

Global`f

f[a_] = 2 + a

and

Global`f

f[a_] := 2 + a

Is that difference entirely due to some flag telling how the DownValues arose, or do non-historical differences (like the mechanism upon calling) exist too?

$\endgroup$
  • 1
    $\begingroup$ try ClearAll[f1, f2, a];a = 5; f1[a_] = 2 + a; f2[a_] := 2 + a;DownValues /@ {f1, f2}? $\endgroup$ – kglr Sep 10 '18 at 7:09
  • $\begingroup$ I just answered something very similar here. $\endgroup$ – halirutan Sep 10 '18 at 8:44
  • 1
    $\begingroup$ @kglr I'm afraid this isn't what OP concerns, the RHS changes after evaluating in this case, which is against OP's title. $\endgroup$ – xzczd Sep 15 '18 at 6:23
  • $\begingroup$ Thanks @xzczd; good point. Should have read the title first:) $\endgroup$ – kglr Sep 15 '18 at 6:35
  • $\begingroup$ Related: mathematica.stackexchange.com/questions/18393/… $\endgroup$ – rm -rf Sep 15 '18 at 17:05
15
+50
$\begingroup$

This is just my guess about what is going on. First, there is really an internal difference between the two approaches, as is shown in the OP. Another place where this difference manifests itself is with the function Language`ExtendedDefinition:

Clear[f];
f[a_] = 2 + a;

Language`ExtendedDefinition[f]

Language`DefinitionList[f->{OwnValues->{},SubValues->{},UpValues->{},DownValues->{HoldPattern[f[a_]]->2+a},NValues->{},FormatValues->{},DefaultValues->{},Messages->{},Attributes->{}}]

Notice the Rule instead of the RuleDelayed in the DownValues rule. For the SetDelayed version:

Clear[g]
g[a_] := 2 + a

Language`ExtendedDefinition[g]

Language`DefinitionList[g->{OwnValues->{},SubValues->{},UpValues->{},DownValues->{HoldPattern[g[a_]]:>2+a},NValues->{},FormatValues->{},DefaultValues->{},Messages->{},Attributes->{}}]

This time the DownValues rule uses RuleDelayed.

So, I think the real question is why the output of DownValues doesn't use Rule:

Clear[f]
f[a_] = 2 + a;

DownValues[f]

{HoldPattern[f[a_]] :> 2 + a}

I think the answer is that if DownValues did use Rule, then the output would be confusing if a had acquired a value after f was defined. Suppose the output of DownValues was:

dv = {HoldPattern[f[a_]] -> 2 + a}

{HoldPattern[f[a_]] -> 2 + a}

At some point, a is given a value:

a = 3;

And then, later, the user wants to know the DownValues of f:

dv

{HoldPattern[f[a_]] -> 5}

The user would be confused into thinking that the RHS of the rule was 5, when it is really 2+a. So, the function DownValues purposefully replaces Rule with RuleDelayed to avoid this confusion. Note that an alternative was to introduce another hold-type wrapper around the output of DownValues, and this is actually what Language`ExtendedDefinition does.

$\endgroup$
  • $\begingroup$ Interesting. Then, does this internal difference have any deeper influence? I guess the answer is no, but being just a normal user, I dare not affirm. $\endgroup$ – xzczd Sep 17 '18 at 7:58
  • $\begingroup$ Is there a detectable difference between the two kinds of definitions though? I mean a difference in behaviour, ignoring directly checking Definitions or ExtendedDefinition. $\endgroup$ – Szabolcs Sep 20 '18 at 8:29
  • 1
    $\begingroup$ @Szabolcs My guess is that there is no example where this difference in definitions would exhibit a difference in evaluation behavior. However, I wouldn't be surprised if somebody could come up with one. $\endgroup$ – Carl Woll Sep 22 '18 at 21:45
2
$\begingroup$

Also, it should be pointed out, that DownValues are the same, but Definition is still different. I guess this might be the "flag" op asked about.

Information displays Definition by calling it. While one might think Information only "illustrates" whatever is kept internally, then probably Definition really is what the kernel knows. Same output, but maybe a semantic difference one could say.

In[32]:= ClearAll[f, a]
f[a_] = a + 2
Definition[f]
f[a_] := a + 2
Definition[f]

Out[33]= 2 + a

Out[34]= Definition[f]

Out[36]= Definition[f]
$\endgroup$
  • $\begingroup$ Actually, Information internally calls Definition: Clear[f, x]; f[x_] = x; Trace[Information[f], Definition[_]] // Flatten // FullForm. $\endgroup$ – xzczd Sep 16 '18 at 13:18
  • $\begingroup$ Thanks, I knew that, but improved my answer to emphesize my message. Do you agree with what I now state? $\endgroup$ – Johu Sep 16 '18 at 13:40
  • $\begingroup$ Hmm… I think "what the kernel knows" is a somewhat vague description, and I tend to think Definition is also kind of illustration. Try the following example: Clear[f, g, x]; f[x_] = x; DownValues[g] = DownValues[f]; {Definition@g, UpValues@g, DownValues@g} // InputForm $\endgroup$ – xzczd Sep 16 '18 at 14:13
0
$\begingroup$

Reading the title of the question, I can't help but notice the part about what gets evaluated and when:

"What is the difference between Set and SetDelayed when evaluating the RHS leaves it unchanged?"

where I emphasize the later part.

I think there is a slight misunderstanding about what is the difference between Set and SetDelayed with regard to the time of evaluation of the RHS.

In the example provided, both definitions seem to be equivalent, especially after looking at the relevant DownValues-which appear identical- because care is not taken to account for the OwnValues of the symbol used in the definition of f, here a. Thus, it appears that the two definitions are equivalent, when in actual fact they are not.

If prior to the evaluation of the definition of f using Set, the symbol a had obtained a value eg by evaluating a=-2 at a prior point, the two definitions would have had different Information and different DownValues.

For the sake of comparison, juxtapose the following definitions to the ones in the Q:

a = -2;
f[a_] = 2 + a;
Information[f]
DownValues[f]

Global`f

f[a_] = 0

{ HoldPattern[f[a_]] :> 0 }

Clear[a, f] (* it's not necessary to clear a but we do it anyway. *)

f[a_] := 2 + a;
Information[f]
DownValues[f]

Global`f

f[a_] := 2 + a

{ HoldPattern[f[a_]] :> 2 + a }

After evaluating the lines above, I hope it becomes clear that the two definitions are not equivalent.

In the case of Set, the RHS appears to be left unevaluated, when there are no OwnValues for a, but that is not the case!. By construction, Set always evaluates its RHS.

$\endgroup$
  • 4
    $\begingroup$ I'm sorry, but as pointed out in previous comment, OP only concerns about cases that RHS is unchanged after evaluating, so example like a = -2; f[a_] = 2 + a is out of scope of this question. $\endgroup$ – xzczd Sep 16 '18 at 8:45
  • 1
    $\begingroup$ I think there is also a copypaste mistake in the first example... SetDelayed acts as Set $\endgroup$ – Johu Sep 16 '18 at 9:32
  • $\begingroup$ @Johu indeed, thanks for pointing it out! $\endgroup$ – yosimitsu kodanuri Sep 16 '18 at 10:31
  • 1
    $\begingroup$ Well, it seems that you've misunderstood OP's question in some way. "I don't understand how the RHS of Set could remain unchanged in the context of the Q. " Simplest case: Clear[f, x]; f[x_] = x;. $\endgroup$ – xzczd Sep 16 '18 at 13:14
  • 3
    $\begingroup$ If you give it a thought, the Q is actually quite interesting. Take a close look at Carls answer. It is not about interchangeable use, but it is about hidden information, which makes the two different even if DownValues are the same! $\endgroup$ – Johu Sep 17 '18 at 21:53

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.