I would like to be able to define the values of a particular function for new inputs.
f[x1]=f1;
f[x2]=f2;
I would also like to be able redefine the values for most inputs.
f[x1]=f3;
f[x2]=f0;
But there are certain definitions I would like to protect.
f[x0]=f0;
Protect[f] protects all definitions and prevents me from defining the values for new inputs.
I could set/remove/set the Protected attribute for f but that isn't any fun because there are very few definitions I would like protected, and many that get defined and redefined.
My other thought would be to modify Set:
Unprotect[Set]
f::fixeddef="The definition of f for this value cannot be modified"
Set[f[x0],_]:=(Message[f::fixeddef]; $Failed)
SetAttributes[Set,Protected]
This doesn't completely work though:
f[x0]=f1 (* fails appropriately *)
{f[x0],f[x1]}={f3,f4} (* returns {f3,f4} instead of {f0,f4} *)
I assumed Set threads itself over nested lists, recursively calling Set until the lhs is a Symbol, but apprently I assumed wrong.
How does Set work and how can I protect the function definitions only for particular inputs?
fdo not show the value you want becausefitself was never evaluated, but that seems like quite an edge case. Otherwise I think this method is robust and practical. – Mr.Wizard Sep 10 '13 at 23:25fixis used, even if it's used for the same Set. I'd like to see that cleaned up. We should only ever need one definition forf. – Mr.Wizard Sep 10 '13 at 23:29guardis your trick. It would be nice if there was some way thatfwould only be re-evaluated when a change was made that affected the value off, but I don't know how to go about it. – Mr.Wizard Sep 11 '13 at 00:15ValueFunctionin theExperimentalcontext, which may show some promise. – Timothy Wofford Oct 20 '13 at 20:44ValueFunctionis forOwnValuesonly, but my memory may be wrong – Rojo Oct 20 '13 at 20:52