19

Mathematica Version 11.2 desktop / Windows 10 Pro 64-bit

I am trying to understand if a symbol can have both OwnValues and DownValues.

Is this acceptable or not acceptable?

As an experiment, I noticed that when one assigns the OwnValues such as "y = a" compared to the DownValues makes a difference which I cannot explain. The following is an example.

y[] = b; y[x] = 1; y = a;
??y

Global`y
$\qquad$y = a $\quad$ y[ ] = b $\quad$ y[x] = 1

{OwnValues[y], DownValues[y]}
(* {{HoldPattern[y]:>a},{HoldPattern[y[]]:>b,HoldPattern[y[x]]:>1}} *)

Remove[y]

y=a; y[]=b; y[x]=1;
??y

Global`y
$\qquad$ y = a

{OwnValues[y], DownValues[y]}
(* {{HoldPattern[y]:>a}, {}} *)

The help says Set ( = ) has attribute HoldFirst. Therefore, the lhs should not be evaluated. So it should not matter if you have y, y[], or y[x] because they are not evaluated. However, for y = a; y[] = b; y[x] = 1;, it does seem to matter since only y = a is defined.

Please clarify.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
John
  • 455
  • 2
  • 6
  • 3
    Regarding HoldFirst, I immediately thought of Leonid's 2015 comment: "[Having a hold attribute] simply means that arguments are passed to the function in unevaluated form, but does not restrict what functions decide to do with them." – jjc385 Nov 26 '17 at 23:37
  • 1
    See the second bullet point in the first chapter in tutorial/Evaluation to learn why this will be a problem. – Kuba Nov 27 '17 at 06:23

1 Answers1

16

Yes, a symbol can have both own-values and down-values, but it is usually bad practice to give a symbol both. Further, the order in which they are created matters, but there are problems both ways.

OwnValue first

y = 42;
OwnValues @ y

{HoldPattern[y] :> 42}

But now you can't set a own-value, because

y[42] = 0

Set::write: Tag Integer in 42[42] is Protected.
0

DownValues @ y

{}

DowValue first

x[42] = 0;
DownValues @ x

{HoldPattern[x[42]] :> 0}

x = 42;
OwnValues @ x

{HoldPattern[x] :> 42}

The symbol x has both an own-value and a down-value, but look what happens when x[42] is evaluated.

x[42]

42[42]

This happens because under normal evaluation rules, Mathematica evaluates the head x of x[42] before anything else. When evaluating a symbol such as x, it looks at own-vales first and finds one. It uses that own-value to replace x with 42. It next evaluates the parts (arguments) of expression and of course gets 42, and so x[42] evaluates to 42[42]. Mathematica doesn't look for a down-value of x because it doesn't need to.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257