For cases where one has already assigned DownValues associated with the name 'a', is there an accepted way to block the assignment of OwnValues to the same name? (I originally came across this issue while playing with someone's attempt at implementing a data dictionary.)
Here's what I mean to avoid:
Remove[a];
a[1] := somethingDelayed
a[2] = somethingImmediate;
DownValues[a]
a[1]
a[2]
Returns...
{HoldPattern[a[1]] :> somethingDelayed,
HoldPattern[a[2]] :> somethingImmediate}
somethingDelayed
somethingImmediate
And now if we were to evaluate:
a = somethingThatScrewsUpHeads;
(* OwnValues[a] above stored in OwnValues *)
a[1]
a[2]
We get...
somethingThatScrewsUpHeads[1]
somethingThatScrewsUpHeads[2]
Is there an easy/flexible way to prevent OwnValues for any Name in DownValues? (Lemme guess... it's possible, but there's going to be a performance hit?)
I am not aware of any way to directly "block"
OwnValues
, and since Mathematica's evaluator evaluates heads before anything else (parts, application ofDownValues
,UpValues
andSubValues
, etc), this does not bring us anywhere (I discussed this problem briefly in my book).The problem with a straightforward approach is that it will likely be based on adding
DownValues
toSet
andSetDelayed
, since it looks like they can not be overloaded via UpValues.EDIT
As pointed by @WReach in the comments, for the case at hand
UpValues
can be successfully used, since we are dealing withSymbol
s which must be literally present inSet
/SetDelayed
, and therefore the tag depth1
is sufficient. My comment is more relevant to redefiningSet
on some heads, and when expressions with those heads must be allowed to be stored in a variable (cases likePart
assignments or custom data types distinguished by heads)END EDIT
However, adding
DownValues
forSet
andSetDelayed
is a recipe for disaster in most cases ( this thread is very illustrative), and should be used very rarely (if at all) and with extreme care.From the less extreme approaches, perhaps the simplest and safest, but not automatic way is to
Protect
the symbols after you define them. This method has a problem that you won't be able to add new or modify existing definitions, withoutUnprotect
-ing the symbol.Alternatively, and to automate things, you can use a number of tricks. One is to define custom assignment operators, such as
and consistently wrap
SetDelayed
- andSet
-based assignments indef
(I chose this syntax fordef
- keptSet
/SetDelayed
insidedef
- to keep the syntax highlighting), and the same forSet
. Here is how your example would look like:You can then go further and write a code - processing macro, that will wrap
SetDelayed
- andSet
-based assignments indef
everywhere in your code:So, you can just wrap your piece of code in
useDef
, and then don't have to change that piece of code at all. For example:In the interactive session, you can go one step further still and set
$Pre = useDef
, then you won't forget to wrap your code inuseDef
.EDIT
It is trivial to add diagnostic capabilities to
def
, by using the pattern - matcher. Here is a version that will issue a warning message in case when an assignment to a symbol withDownValues
is attempted:Again, by using
useDef[]
(possibly with$Pre
), this can be an effective debugging tool, since no changes in the original code are at all needed.I don't know if this is an "accepted" way, but you could define a rule that prevents
Set
andSetDelayed
from acting upona
:With this rule in place, any attempt to assign an
OwnValue
toa
will fail:However, this rule will still allow new
DownValues
fora
:Performance
The rule does not seem to have an appreciable impact on the performance of
Set
andSetDelayed
, presumably since the rule is installed as an up-value ona
. I tried to verify this by executing...... both before and after the installation of the rule. There was no observable change in the timing. I then tried installing
Set
-related up-values on 10,000 generated symbols, thus:Again, the timing did not change even with so many up-value rules in place. These results suggest that this technique is acceptable from a performance standpoint, although I would strongly advise performing performance tests within the context of your specific application.