Unset¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Unset[lhs]
or lhs =. removes any rule whose left-hand side is lhs, up to renaming of pattern variables. A bare symbol clears its value; a function form clears the matching definition on the head symbol.
Notes
Unset has attribute HoldFirst; Protected symbols are not affected.Examples (12)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (3)¶
In[1]:= x = 5; x =.; x
Out[1]= x
In[2]:= f[x_] := x^2; f[x_] =.; f[3]
Out[2]= f[3]
In[3]:= fact[1] = 1; fact[n_] := n fact[n - 1]; fact[1] =.; DownValues[fact]
Out[3]= {HoldPattern[fact[n_]] :> n fact[n - 1]}
Applications (9)¶
The postfix form of Unset
Removes only the g[1] rule
Pattern names may differ: the rule is found up to renaming
The general rule survives
In[9]:= fact[1] = 1; fact[n_] := n fact[n - 1]; fact[1] =.; DownValues[fact]
Out[9]= {HoldPattern[fact[n_]] :> n fact[n - 1]}
A conditional rule
A Protected symbol: Unset::wrsym
No matching rule: still Null
Implementation notes¶
Algorithm. builtin_unset (src/core.c) implements lhs =., removing the
single definition whose left-hand side is lhs rather than every rule on a
symbol. It first works out which symbol owns the rule and whether it is an
OwnValue or a DownValue: a bare symbol is an OwnValue on itself; f[...] is a
DownValue keyed by the head f; and because f[x_] /; cond =. parses to
Unset[Condition[f[x_], cond]], a Condition wrapper is unwrapped to find the
inner head. A non-assignable left-hand side (e.g. Unset[5]) returns NULL.
A Protected owner is refused with Unset::wrsym (mirroring Set),
returning Null. Otherwise symtab_remove_matching_rule(name, lhs, own_value)
deletes exactly one rule, leaving the symbol's other definitions, attributes and
remaining rules intact. To find it, the function canonicalizes the query the way
add_rule canonicalized the stored patterns (pattern_canonicalize), then
alpha-normalizes both sides (pattern_alpha_normalize). So f[y_] =. removes
a rule defined as f[x_] := .... A removal bumps the evaluation clock so that
cached results which relied on the rule are dropped. The head returns Null
whether or not a rule was found.
Attributes & limits. Unset carries HoldFirst | Protected, so the target
is not evaluated to its current value before the rule is located. It takes one
argument.
=.is a low-precedence postfix operator (precedence 40, likeSet), so it captures the whole preceding expression:a b =.parses asUnset[a b]. The guard against a trailing digit keepsk =.5parsing asSet[k, 0.5].Unsethas attributes{HoldFirst, Protected}; it holdslhs, so the symbol (not its value) is operated on.Protectedsymbols are not affected.- Always returns
Null, whether or not a matching rule was found.
Attributes: HoldFirst, Protected.
References¶
See also: Set
- Source:
src/core.c - Specification:
docs/spec/builtins/assignment-and-rules.md - Tests:
tests/test_unset.c
Notes & additional examples¶
Notes¶
lhs =.(Unset) removes the one definition whose left-hand side islhs, rather than every rule on a symbol. A bare symbol loses its OwnValue, andf[args] =.loses exactly that DownValue.- Left-hand sides are compared after canonicalization and alpha-renaming of
pattern variables, so
f[y_] =.removesf[x_] := .... ACondition-wrapped left-hand side is matched on its inner head. HoldFirst | Protected: the target is not evaluated before the rule is located. The result isNullwhether or not a rule was found.- A
Protectedowner is refused withUnset::wrsym, routed through the message funnel soChecksees it. - See also:
Clear,ClearAll,Set,TagUnset,DownValues.