Reading the comments in this answer has motivated me to request a full solution to part of this problem.
What I'd like is an efficient solution that returns True if there exist an OwnValue and False in all other cases. This narrow behavior mirrors what folks coming from other programming languages expect of asking if something has a value or not.
A solution needs to be able to handle symbols with any number of OwnValue, UpValues, DownValues, SubValues, NValues, and FormatValues. The only evalution that should occur is that required to get the value of the symbol's OwnValue.
Motivation
My motivation for this request is twofold.
- There have been many queries on this topic, but none that appear to be specific enough to narrow down to one correct interpretation. I hope this question solves that.
- The behavior requested mirrors the behavior of most programming languages that folks are familiar with. That is, when we think of a symbol/"variable" having a value, we really mean to ask if the symbol has some
OwnValuesor not. Yes there are other interpretations of what it means to have aValueQthat does not do extra evaluations, but this question does not cover them.
ValueQconsistent with Mathematica's evaluation semantics - but it turn out to not be very useful in practice, since it triggers even trivial evaluations. – Leonid Shifrin Mar 23 '12 at 21:08OwnValue. This would be equivalent (somewhat) to commonlisp symbol manipulation. In common lisp symbols have multiple values associated with them, a symbol-value, function-value, macro-value, documentation-value, attribute-list-value (also known as a plist), and so on. If I missed a full solution meeting those constraints then I feel that this question has no place here and ought to be closed. – nixeagle Mar 23 '12 at 21:28f[x]) will evaluate to something else (call it a value) without actually performing a full evaluation. So, the only possibility is to clone all symbols in the dependency tree of a given symbol, and evaluate the expression in a separate dynamic environment with the clones (so that any changes in clones won't affect the rest of the system). This will, however, be prohibitively expensive. – Leonid Shifrin Mar 23 '12 at 21:33f[x]the symbol can have a value that is not the result of evaluating the associated function? And that we are really unable to tell if the symbol is a function or not without actually asking MMA to evaluate it? I am aware that MMA is essentially a glorified pattern matcher and that fact may play a role into this problem. – nixeagle Mar 23 '12 at 21:37ClearAll[g];g[x_]:=(flag=True);f[x_]/;g[x]:=x^2. The point is,f[x]will evaluate tox^2, but it is not clear to me how you can establish this fact without leaking evaluation, because, if you don't allow any evaluation, you won't know whether the condition isTrueorFalse. You can find a way for this particular example, but imagine more complicated definitions and tests. – Leonid Shifrin Mar 23 '12 at 21:41OwnValue. I really think I'm massively confused now. I understand that you guys have a ton more experience than I, but yet I'm struggling to see why I can't just dismiss f[x] as having noOwnValueand reacting accordingly. MMA.SE has been suggesting I move this to chat for the last 2 or 3 replies... should we do so? – nixeagle Mar 23 '12 at 21:46ValueQshould "mean". Hopefully this question and answer(s) will prove worthwhile to keep as it is narrowly focused to one specific interpretation of theValueQquestions. – nixeagle Mar 23 '12 at 23:27ValueQdoes not leak evaluation. – Szabolcs Mar 24 '12 at 16:02ValueQdoes. This lays it out in black and white what the meaning of it is and how to get an alternate (sometimes expected) meaning. – nixeagle Mar 24 '12 at 16:45ValueQforOwnValue, so why not explicitly restrict it to that? – Szabolcs Mar 24 '12 at 17:17ownValueQto meanValueQby the second definition given. Mathematica has its reasons for definingValueQas it does, but this is not always what users expect or want. I've tried to make this distinction clear here with this question and answer. Hopefully it is clear enough to help other users who run into this and wonder. A question about how to getValueQto be "safe" using Mathematica's meaning is fodder for another question. – nixeagle Mar 24 '12 at 17:27