UpValues
Details and Options
- UpValues[f] also gives rules corresponding to g[…,f,…].
- When evaluating an expression, matching upvalues are applied before matching downvalues. »
- If a function has attribute HoldAllComplete, no upvalues are applied prior to entering the body of the function. »
- UpValues are typically defined using UpSet, UpSetDelayed, TagSet or TagSetDelayed.
- You can specify the upvalues for f by making an assignment of the form UpValues[f]=list.
- The list returned by UpValues has elements of the form HoldPattern[lhs]:>rhs.
Examples
open all close allBasic Examples (1)
Define an upvalue for a symbol g using TagSetDelayed:
g/:f[g[x_]] := h[x]Define an upvalue using UpSetDelayed:
f[g] ^:= h[1]These are the upvalues associated with g:
UpValues[g]Scope (3)
UpValues returns rules corresponding to upvalues defined for a symbol:
g/:g[x_] + g[y_] := gplus[x, y]UpValues[g]f[x1] ^= 1;f[x2] ^= 3;f[y] ^= 5;Obtain the upvalues of symbols whose names start with x:
UpValues /@ Names["x*"]UpValues can be used to set the values directly:
UpValues[h] = {h[x_] * h[y_] :> htimes[x, y]};Definition[h]h[a] h[b]Applications (2)
The resulting rules are in the order given:
x/:x + y_ /; y > -2 := f[y]
x/:x + y_ /; y < 2 := g[y]UpValues[x]x + 1UpValues[x] = Reverse[UpValues[x]]x + 1Copy a symbol's definitions to another symbol:
g/:g[x_] + g[y_] := plus[x, y]UpValues[h] = UpValues[g] /. g -> hh[a] + h[b]% == g[a] + g[b]Properties & Relations (7)
Values can be defined by immediate or delayed assignments:
g/:f[g[_]] = 17;
g/:h[g[x_]] := h[x];UpValues[g]HoldPattern is used to protect the rules from their own definitions:
g/:f[g[x_]] := h[x]UpValues[g]f[g[x_]] :> h[x]UpValues["sym"] will issue a message if the specified symbol does not exist:
UpValues["x"]If the symbol exists but has no definitions, an empty list is returned:
x;
UpValues["x"]Definition and Information display upvalues but do not return them as values:
g/:f[g[x_]] := h[x]Definition[g]%//FullFormUpValues returns a value that can be used in a program:
UpValues[g]Evaluation of an expression involves applying matching upvalues:
g/:f[g[x_]] := h[x]Hold[f[g[a]]] /. UpValues[g]Matching upvalues are used before matching downvalues in evaluation:
f[g[x_]] ^:= h[x]f[x_] := 1{UpValues[g], DownValues[f]}The upvalue for g is used prior to the downvalue for f:
f[g[2]]Compare with a manual application of the upvalue and downvalue:
HoldComplete[f[g[2]]] /. UpValues[g]HoldComplete[f[g[2]]] /. DownValues[f]Define an upvalue for g that applies for all heads:
_[g[x_]] ^= 1UpValues[g]Even if a function has attribute HoldAll, the upvalue will be applied during evaluation:
Hold[g[x]]If a function has attribute HoldAllComplete, the upvalue will not be applied:
HoldComplete[g[x]]See Also
UpSet UpSetDelayed TagSetDelayed TagSet HoldAllComplete DownValues SubValues OwnValues Information ValueQ Clear ClearAll Save
Function Repository: DefinedSymbols
Tech Notes
Related Guides
Related Workflows
- Find All Defined Functions ▪
- Clear Definitions for Symbols and Functions
History
Introduced in 1991 (2.0) | Updated in 1996 (3.0)
Text
Wolfram Research (1991), UpValues, Wolfram Language function, https://reference.wolfram.com/language/ref/UpValues.html (updated 1996).
CMS
Wolfram Language. 1991. "UpValues." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 1996. https://reference.wolfram.com/language/ref/UpValues.html.
APA
Wolfram Language. (1991). UpValues. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/UpValues.html
BibTeX
@misc{reference.wolfram_2026_upvalues, author="Wolfram Research", title="{UpValues}", year="1996", howpublished="\url{https://reference.wolfram.com/language/ref/UpValues.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_upvalues, organization={Wolfram Research}, title={UpValues}, year={1996}, url={https://reference.wolfram.com/language/ref/UpValues.html}, note=[Accessed: 13-June-2026]}