ValueQ¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
ValueQ[expr]
gives True if a value has been defined for expr, False otherwise.
Notes
HoldAll: inspects the symbol itself, not its evaluated value. A bare symbol tests OwnValues; f\[...\] tests whether head f has any DownValues.Examples (9)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (5)¶
Head has DownValues
Bare symbol, only DownValues
HoldAll preserved via Unevaluated
Applications (4)¶
Now x carries an OwnValue
True as soon as the head f has a DownValue
The bare symbol f has only DownValues, so no OwnValue
Implementation notes¶
Algorithm. builtin_valueq (src/core.c) inspects the symbol table rather
than evaluating its argument — ValueQ carries HoldAll, so the argument reaches
the builtin unevaluated. A bare EXPR_SYMBOL is valued iff its SymbolDef
carries a non-empty own_values chain (an immediate or delayed assignment). A
compound f[...] is valued iff the SymbolDef of its head f carries any
down_values — regardless of whether the particular arguments would match a rule.
Everything else (numbers, strings, undefined symbols) returns False.
Data structures. Two symtab_lookup probes at most, reading the own_values
/ down_values linked lists hanging off the SymbolDef; no rule matching and no
tree walk.
Complexity / limits. O(1) (a hash lookup plus a null test on a list head).
Because the test is existence-of-a-rule, not applicability, ValueQ[f[a, b]] is
True as soon as f has any DownValue. Wrong arity routes a ValueQ::argx
diagnostic through builtin_arg_error. Attributes HoldAll, Protected.
Attributes: HoldAll, Protected.
References¶
See also: HoldAll
- Source:
src/core.c - Specification:
docs/spec/builtins/expression-information.md - Tests:
tests/test_core.c - Tests:
tests/test_nminimize.c
Notes & additional examples¶
Notes¶
ValueQ[expr] reports whether a value has been defined for expr without
evaluating it — it is HoldAll, so it inspects the symbol itself rather than the
value the symbol would evaluate to. A bare symbol is valued iff it carries an
OwnValue (an immediate x = 5 or a delayed y := RandomReal[]); a compound
f[...] is valued iff its head f carries any DownValue, regardless of whether
the particular arguments match a rule — which is why ValueQ[f[2]] is True while
ValueQ[f] is False. Numbers, strings, and undefined symbols give False.