21
$\begingroup$

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.

$\endgroup$
2
  • 3
    $\begingroup$ 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." $\endgroup$ Commented Nov 26, 2017 at 23:37
  • 1
    $\begingroup$ See the second bullet point in the first chapter in tutorial/Evaluation to learn why this will be a problem. $\endgroup$ Commented Nov 27, 2017 at 6:23

1 Answer 1

20
$\begingroup$

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 down-value, because

y[42] = 0 

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

DownValues @ y 

{}

DownValue 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.

$\endgroup$
2
  • 1
    $\begingroup$ Thank you for elaborating this. This essentially means to me that it is completely unpractical to assign OwnValues and DownValues to the same symbol, right? $\endgroup$ Commented Jun 23, 2018 at 11:59
  • 2
    $\begingroup$ @TheoTiger. Yes $\endgroup$ Commented Jun 23, 2018 at 17:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.