## Can a symbol have both OwnValues and DownValues?

18

5

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


Globaly
$\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


Globaly
$\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.

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 – 2017-11-26T23:37:24.507

1

See the second bullet point in the first chapter in tutorial/Evaluation to learn why this will be a problem.

– Kuba – 2017-11-27T06:23:29.913

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.

1Thank you for elaborating this. This essentially means to me that it is completely unpractical to assign OwnValues and DownValues to the same symbol, right? – Theo Tiger – 2018-06-23T11:59:08.943

2@TheoTiger. Yes – m_goldberg – 2018-06-23T17:57:20.413