DownValuesFunction[sym]
represents a function that uses definitions attached to sym when compiling.
DownValuesFunction
DownValuesFunction[sym]
represents a function that uses definitions attached to sym when compiling.
Details
- DownValuesFunction can be used directly in compiled code.
- DownValuesFunction can be used in compiled declarations specified by FunctionDeclaration.
- DownValuesFunction specifications typically use Typed to give type annotations.
- When DownValuesFunction is compiled, it creates a function from symbol declarations and compiles it.
- Declarations that use DownValuesFunction can use polymorphic types.
- The following pattern constructs are supported:
-
_ match an argument x_ match an argument assigning to the name x _h match if the head is h x_?test match if test[x] evaluates to True p/;cond match if cond evaluates to True Except[p] match if p does not match p1|p2 match if one of pi matches f[g[p]] match a compound expression (for inert expressions and packed arrays) __,___ match a sequence (for inert expressions)
Examples
open all close allBasic Examples (2)
Define a squaring function in the Wolfram Engine:
square[x_] := x ^ 2Use its downvalues to create a compiled function for the integer-typed case:
funcComp = FunctionCompile[
Function[Typed[arg, "MachineInteger"],
Typed[DownValuesFunction[square], {"MachineInteger"} -> "MachineInteger"][arg]
]
]Apply the compiled function to an integer argument:
funcComp[100]Define the factorial function using recursive downvalues:
factorial[0] = factorial[1] = 1;
factorial[n_] := n * factorial[n - 1]Use FunctionDeclaration to associate the factorial with an integer-type signature and its downvalue implementation:
factorialC = FunctionCompile[
FunctionDeclaration[factorial, Typed[{"MachineInteger"} -> "MachineInteger"]@DownValuesFunction[factorial]
],
Function[Typed[arg, "MachineInteger"], factorial[arg]]
]Call the compiled function on an integer input:
factorialC[10]Scope (13)
Multiple Definitions (1)
Several definitions attached to the same symbol can be used:
f2[x_, y_] /; x > y := y
f2[x_, 2] := x
f2[x_, y_] := x + yCompile a function that uses the definitions:
cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"], Typed[a2, "MachineInteger"]}, Typed[DownValuesFunction[f2], {"MachineInteger", "MachineInteger"} -> "MachineInteger"][a1, a2]]]This calls the definition that uses a condition:
cf[55, 22]This calls the definition that uses a literal 2:
cf[1, 2]This calls the last catchall definition:
cf[55, 200]Multiple Declarations (1)
Definitions attached to a symbol can be used in more than one declaration with different types:
funA[x_, y_] := x + yMake two declarations with different types and compile functions that use each:
decls = {
FunctionDeclaration[funA, Typed[{"MachineInteger", "MachineInteger"} -> "MachineInteger"]@
DownValuesFunction[funA]
],
FunctionDeclaration[funA,
Typed[{"Real64", "Real64"} -> "Real64"]@
DownValuesFunction[funA]
]
};
cfs = FunctionCompile[decls, <|
"int" -> Function[{Typed[a1, "MachineInteger"], Typed[a2, "MachineInteger"]},
funA[a1, a2]
],
"real" -> Function[{Typed[a1, "Real64"], Typed[a2, "Real64"]}, funA[a1, a2]
]
|>]cfs["int"][2, 3]cfs["real"][2.5, 3.25]Polymorphic Declarations (1)
Definitions attached to a symbol can be declared with a polymorphic type:
funB[x_, y_] := x + yMake a polymorphic declaration and compile functions that use it for different types:
decl = FunctionDeclaration[ funB, Typed[ForAllType[t, {t, t} -> t]]@DownValuesFunction[funB]];
cfs = FunctionCompile[ {decl}, <|"int" -> Function[{Typed[a1, "MachineInteger"], Typed[a2, "MachineInteger"]}, funB[a1, a2]], "real" -> Function[{Typed[a1, "Real64"], Typed[a2, "Real64"]}, funB[a1, a2]]|>]cfs["int"][2, 3]cfs["real"][2.5, 3.25]Recursive Calls (1)
Function declarations that use a symbol definition can call themselves:
fact[0] := 1
fact[n_] := n * fact[n - 1]Make a declaration and compile a function that uses it:
decl = FunctionDeclaration[fact, Typed[{"MachineInteger"} -> "MachineInteger"]@DownValuesFunction[fact]];
cf = FunctionCompile[{decl}, Function[{Typed[a1, "MachineInteger"]}, fact[a1]]]cf[10]Unnamed Patterns (1)
A pattern argument does not need a name:
unnamedPattern[_] := "noName"Compile a function that uses the definition:
cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"]}, Typed[ DownValuesFunction[unnamedPattern], {"MachineInteger"} -> "String"][a1]]]Call the function with different values:
cf[1]Repeated Patterns (1)
If the same name appears more than once in a definition, when called, the value must be the same:
repeatedPattern[x_, x_] := {x}
repeatedPattern[x_, y_] := {x, y}Compile a function that uses the definition:
cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"], Typed[a2, "MachineInteger"]}, Typed[DownValuesFunction[repeatedPattern], {"MachineInteger", "MachineInteger"} -> "PackedArray"::["MachineInteger", 1]][a1, a2]]]Call the function with different values:
cf[1, 2]Call the function with the same value and it uses the definition for the same value:
cf[3, 3]Matching the Head (1)
A pattern can be written to match the head:
headMatch[x_Real] := "Real"
headMatch[x_] := "NotReal"Make a polymorphic declaration that uses the definition:
decl = FunctionDeclaration[headMatch, Typed[ForAllType["e", {"e"} -> "String"]]@DownValuesFunction[headMatch]];Compile a function that uses the declaration with an argument that does not match the head:
cf = FunctionCompile[{decl}, Function[{Typed[a1, "MachineInteger"]}, headMatch[a1]]];Call the function with an integer and that uses the definition that does not match the head:
cf[1]Compile a function that uses the declaration with an argument that does match the head:
cf = FunctionCompile[{decl}, Function[{Typed[a1, "Real64"]}, headMatch[a1]]];Call the function with a real and that uses the definition that matches the head:
cf[1.5]Pattern Test (1)
Compiled patterns can use PatternTest:
patternTest[x_ ? EvenQ] := {0, x}
patternTest[x_] := {1, x}Compile a function that uses the definition:
cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"]}, Typed[DownValuesFunction[patternTest], {"MachineInteger"} -> "PackedArray"::["MachineInteger", 1]][a1]]];cf[2]cf[21]Condition (1)
Compiled patterns can use Condition:
conditionPattern[x_, y_] /; x > y := {y, x}
conditionPattern[x_, y_] := {x, y}Compile a function that uses the definition:
cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"], Typed[a2, "MachineInteger"]}, Typed[DownValuesFunction[conditionPattern], {"MachineInteger", "MachineInteger"} -> "PackedArray"::["MachineInteger", 1]][a1, a2]]];The input has arguments in order:
cf[1, 2]The input has arguments not in order, so in the output, they are reversed:
cf[7, 3]Except (1)
Compiled patterns can use Except:
exceptPattern[Except[y_ ? EvenQ]] := {y, 0}
exceptPattern[y_] := {y}Make a declaration and compile a function that uses it:
cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"]}, Typed[DownValuesFunction[exceptPattern], {"MachineInteger"} -> "PackedArray"::["MachineInteger", 1]][a1]]];The argument is even, so the first definition is not used:
cf[20]The argument is odd, so the first definition is used:
cf[3]exceptPattern2[y : Except[_ ? EvenQ, y_ /; y > 20]] := {y, 0}
exceptPattern2[y_] := {y}Compile a function that uses the definition:
cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"]}, Typed[DownValuesFunction[exceptPattern2], {"MachineInteger"} -> "PackedArray"::["MachineInteger", 1]][a1]]];The argument is even and greater than 20, so the first definition is not used:
cf[24]The argument is odd and greater than 20, so the first definition is used:
cf[25]The argument is odd but less than 20, so the first definition is not used:
cf[3]Alternatives (1)
Compiled patterns can use Alternatives:
alternativesPattern[x : (_Integer | _Real)] := xdecl = FunctionDeclaration[alternativesPattern, Typed[ForAllType["e", {"e"} -> "e"]]@DownValuesFunction[alternativesPattern]];Compile a function to use the declaration:
cf = FunctionCompile[{decl}, Function[{Typed[a1, "MachineInteger"]}, alternativesPattern[a1]]];This function matches the input, so a result is generated:
cf[1]Compile a function with a different type to use the declaration:
cf = FunctionCompile[{decl}, Function[{Typed[a1, "Real64"]}, alternativesPattern[a1]]];This function also matches the input, so a result is generated:
cf[1.5]Compound Patterns (1)
Compilation supports compounds patterns for certain types such as "InertExpression" and "PackedArray":
compoundPattern[{x_, y_}] := x[y]
compoundPattern[x_] := xCompile a function that uses the definition:
cf = FunctionCompile[Function[{Typed[a1, "InertExpression"]}, Typed[DownValuesFunction[compoundPattern], {"InertExpression"} -> "InertExpression"][a1]]];This function matches the input so a result is generated:
cf[{f, x}]Compile a function with a different type to use the declaration:
cf[{a, b, c}]Sequences (1)
Compilation supports certain sequence patterns for "InertExpression":
sequencePattern[{x_, y__}] := x[y]
sequencePattern[_] := InertExpression[$Failed]Compile a function that uses the definition:
cf = FunctionCompile[Function[{Typed[a1, "InertExpression"]}, Typed[DownValuesFunction[sequencePattern], {"InertExpression"} -> "InertExpression"][a1]]];cf[{f, x, y, z}]The pattern with a sequence does not match, so this returns a failure:
cf[{f}]The pattern can have a compound head of any expression type:
sequencePattern2[f[x_, {__, x_}]] := x
sequencePattern2[_] := InertExpression[$Failed]cf = FunctionCompile[Function[{Typed[a1, "InertExpression"]}, Typed[DownValuesFunction[sequencePattern2], {"InertExpression"} -> "InertExpression"][a1]]];cf[f[g, {1, 2, 3, 4, g}]]cf[f[g, {1, 2, 3, 4, h}]]Applications (1)
Symbol Definitions (1)
Declarations that use symbol definitions are useful if the declaration is used more than once:
restrictFlow[ x_, y_] :=
Module[{z},
z = 1 - 1 / (x + I y) ^ 2;
{{x, y}, {Re[z], -Im[z]}}
]A FunctionDeclaration that refers to the symbol:
decl = FunctionDeclaration[restrictFlow, Typed[{"Real64", "Real64"} -> "PackedArray"::["Real64", 2]]@DownValuesFunction[restrictFlow]];A function that uses the symbol:
func = Function[{Typed[x0, "Real64"], Typed[x1, "Real64"], Typed[y0, "Real64"], Typed[y1, "Real64"]}, Table[Flatten[ Table[{restrictFlow[x + d / 4, y], restrictFlow[x + d / 4, -y]}, {x, x0, x1}, {y, y0, y1}], 2] , {d, 0, 3, 0.3}]];Compiled code for the function:
cf = FunctionCompile[decl, func]data = cf[-7, 5, 0.5, 3];A vector plot based on the data that was created:
ListVectorPlot[First[data]]The compiled version is much faster:
func[-7, 5, 0.5, 3];//RepeatedTimingcf[-7, 5, 0.5, 3];//RepeatedTimingAnimate[ListVectorPlot[Part[data, i]], {i, 1, Length[data], 1}, SaveDefinitions -> True, AnimationRunning -> False]Possible Issues (5)
Updating (1)
DownValuesFunction uses the definitions used when the compilation is carried out:
testFun[x_] := x ^ 2func = Function[Typed[arg, "MachineInteger"], Typed[DownValuesFunction[testFun], {"MachineInteger"} -> "MachineInteger"][arg]];
funcComp = FunctionCompile[func];funcComp[10]testFun[x_] := x ^ 3The result uses the definitions in place when the compilation is done:
funcComp[10]Definition Restrictions (1)
If there is no definition that applies to particular arguments, a runtime error case will be added:
fun1[x_ ? Positive] :=
xdecl = FunctionDeclaration[fun1, Typed[{"Integer64"} -> "Integer64"]@DownValuesFunction[fun1]];
funcComp = FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun1[arg]]]funcComp[10]There is no definition that works for this input and hence a runtime error results:
funcComp[-10]Type Consistency (1)
All the branches of pattern definitions must return the same type:
fun10[x_ /; x > 10] :=
True
fun10[x_] :=
10decl = FunctionDeclaration[fun10, Typed[{"Integer64"} -> "Integer64"]@DownValuesFunction[fun10]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun10[arg]]]All the branches of pattern definitions must compile for the arguments:
fun11[x_ /; x > 1] :=
Sin[x]
fun11[x_] :=
xdecl = FunctionDeclaration[fun11, Typed[{"Real64"} -> "Real64"]@DownValuesFunction[fun11]];
FunctionCompile[decl, Function[Typed[arg, "Real64"], fun11[arg]]]decl = FunctionDeclaration[fun11, Typed[{"Integer64"} -> "Integer64"]@DownValuesFunction[fun11]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun11[arg]]]Unsupported Patterns (1)
Optional patterns are not supported:
fun5[x_, y_ : 10] :=
xdecl = FunctionDeclaration[fun5, Typed[{"Integer64"} -> "Integer64"]@DownValueFunction[fun5]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun5[arg]]]Held patterns are not supported:
fun8[HoldPattern[y_]] :=
xdecl = FunctionDeclaration[fun8, Typed[{"Integer64"} -> "Integer64"]@DownValuesFunction[fun8]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun8[arg]]]Verbatim patterns are not supported:
fun9[Verbatim[y_]] :=
xdecl = FunctionDeclaration[fun9, Typed[{"Integer64"} -> "Integer64"]@DownValuesFunction[fun9]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun9[arg]]]Sequence Patterns (1)
Sequence patterns do not support polymorphic declarations:
fun10[{x_, y__}] := x[y]Make a polymorphic declaration, but the compilation cannot use it:
decl = FunctionDeclaration[fun10, Typed[ ForAllType["e", {"e"} -> "e"]]@DownValuesFunction[fun10]];
FunctionCompile[{decl}, Function[{Typed[a1, "InertExpression"]}, fun10[a1]]];Tech Notes
Related Guides
Text
Wolfram Research (2022), DownValuesFunction, Wolfram Language function, https://reference.wolfram.com/language/ref/DownValuesFunction.html (updated 2025).
CMS
Wolfram Language. 2022. "DownValuesFunction." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/DownValuesFunction.html.
APA
Wolfram Language. (2022). DownValuesFunction. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DownValuesFunction.html
BibTeX
@misc{reference.wolfram_2026_downvaluesfunction, author="Wolfram Research", title="{DownValuesFunction}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/DownValuesFunction.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_downvaluesfunction, organization={Wolfram Research}, title={DownValuesFunction}, year={2025}, url={https://reference.wolfram.com/language/ref/DownValuesFunction.html}, note=[Accessed: 13-June-2026]}