ForAllType[x,type]
表示由 x 参数化的类型.
ForAllType[x,cond,type]
表示满足 cond 的类型.
ForAllType[{x1,x1,…},cond,type]
表示具有多个参数的类型.
ForAllType
ForAllType[x,type]
表示由 x 参数化的类型.
ForAllType[x,cond,type]
表示满足 cond 的类型.
ForAllType[{x1,x1,…},cond,type]
表示具有多个参数的类型.
更多信息
- ForAllType 不会自行运算,但可以在接受类型(包括 Typed、FunctionDeclaration 等)的函数内部使用.
- ForAllType 用于指定多态函数的类型.
- cond 的可能形式包括:
-
Element[x,"absType"] 断言 x 是抽象类型的成员 TypeEvaluate[…] 运行任意函数 Element[x,{ty1,ty2,…}] 断言 x 是给定类型之一 NotElement[x,{ty1,ty2,…}] 断言 x 不是给定的任何类型 - cond 可以采用 Element[xi,"absType"] 的形式,其中 "absType" 是一个抽象类型. »
- cond 可以是 Element[xi,{ty1,ty2,…}] 的形式,其中 tyi 是具体类型. »
- TypeEvaluate 可以在 cond 下使用. »
- cond 可以作为条件列表给出.
范例
打开所有单元 关闭所有单元基本范例 (1)
表示一个函数的类型,该函数接受类型为 t 的对象列表并返回一个类型为 t 的单个对象:
type = ForAllType[t, {"ListVector"::[t]} -> t];dec = FunctionDeclaration[polyFunc, Typed[type]@Function[list, First[list]]];FunctionCompile[dec, Function[Typed[list, "ListVector"::["MachineInteger"]], polyFunc[list]]][{7, 3, 6, 4}]FunctionCompile[dec, Function[Typed[list, "ListVector"::["String"]], polyFunc[list]]][{"Hello", "World", "Foo"}]范围 (8)
表示一个函数的类型,它接受任何压缩数组并返回一个秩数更高的数组:
type = ForAllType[{t, r}, {"PackedArray"[t, r]} -> "PackedArray"::[t, TypeEvaluate[r + 1]]];cf = FunctionCompile[{
FunctionDeclaration[addRank, Typed[type]@Function[array, {array}]]
}, Function[Typed[array, "PackedArray"::["Integer64", 2]], addRank[array]]]cf[{{1, 2, 3}, {4, 5, 6}}]type = ForAllType[t, t∈"Numbers", {} -> "RawPointer"::[t]];cf = FunctionCompile[{
FunctionDeclaration[polyOutput, Typed[type]@Function[{}, ToRawPointer[]]]
}, Function[{}, TypeOf@Typed[polyOutput[], "RawPointer"::["Integer64"]]]]cf[]type = ForAllType[t, t∈"Integers", {t} -> t];funcDec = FunctionDeclaration[addtwo, Typed[type]@Function[arg, arg + Typed[2, TypeOf[arg]]]];cf = FunctionCompile[funcDec, Function[Typed[arg, "Integer32"], addtwo[arg]]]cf[10]cf = FunctionCompile[funcDec, Function[Typed[arg, "Real64"], addtwo[arg]]]type = ForAllType[{t, r}, TypeEvaluate[r < 3], {"PackedArray"[t, r]} -> "PackedArray"::[t, r]];funcDec = FunctionDeclaration[arrayIdentity, Typed[type]@Function[arg, arg]];cf = FunctionCompile[funcDec, Function[Typed[arg, "PackedArray"::["MachineInteger", 2]], arrayIdentity[arg]]]cf[{{1, 2, 3}, {4, 5, 6}}]cf2 = FunctionCompile[funcDec, Function[Typed[arg, "PackedArray"::["MachineInteger", 3]], arrayIdentity[arg]]]下面是同一个函数的两个声明:一个用于 "Integers",一个用于 "UnsignedIntegers":
decl1 = FunctionDeclaration[funTest, Typed[ForAllType[t, t∈"Integers", {t} -> "MachineInteger"]]@Function[arg, 1]];
decl2 = FunctionDeclaration[funTest, Typed[ForAllType[t, t∈"UnsignedIntegers", {t} -> "MachineInteger"]]@Function[arg, 2]];FunctionCompile[{decl1, decl2}, Function[Typed[arg, "Integer64"], funTest[arg]]][0]在这里,两个限制条件都匹配,并且使用了最窄的声明("UnsignedMachineIntegers" 的声明):
FunctionCompile[{decl1, decl2}, Function[Typed[arg, "UnsignedInteger64"], funTest[arg]]][0]CompilerInformation 可以帮助了解 "Integer64" 等类型如何与 "Integers" 等抽象类型协同工作.
此声明同时适用于 "Integer64" 和 "Real64":
decl1 = FunctionDeclaration[funTest, Typed[ForAllType[t, t∈{"Integer64", "Real64"}, {t} -> t]]@Function[arg, arg]];FunctionCompile[{decl1}, Function[Typed[arg, "Integer64"], funTest[arg]]][42]FunctionCompile[{decl1}, Function[Typed[arg, "Real64"], funTest[arg]]][42.1]此声明不能应用于 "Integer64" 或 "Real64":
decl1 = FunctionDeclaration[funTest, Typed[ForAllType[t, t∉{"Integer64", "Real64"}, {t} -> t]]@Function[arg, arg]];FunctionCompile[{decl1}, Function[Typed[arg, "Boolean"], funTest[arg]]][True]FunctionCompile[{decl1}, Function[Typed[arg, "Integer64"], funTest[arg]]]decl1 = FunctionDeclaration[funTest, Typed[ForAllType[{t, e, r}, t∈{"Integer64", "PackedArray"::[e, r]}, {t} -> t]]@Function[arg, arg]];FunctionCompile[{decl1}, Function[Typed[arg, "Integer64"], funTest[arg]]][42]FunctionCompile[{decl1}, Function[Typed[arg, "PackedArray"::["Integer64", 1]], funTest[arg]]][{42, 2}]技术笔记
文本
Wolfram Research (2022),ForAllType,Wolfram 语言函数,https://reference.wolfram.com/language/ref/ForAllType.html (更新于 2026 年).
CMS
Wolfram 语言. 2022. "ForAllType." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2026. https://reference.wolfram.com/language/ref/ForAllType.html.
APA
Wolfram 语言. (2022). ForAllType. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/ForAllType.html 年
BibTeX
@misc{reference.wolfram_2026_foralltype, author="Wolfram Research", title="{ForAllType}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/ForAllType.html}", note=[Accessed: 16-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_foralltype, organization={Wolfram Research}, title={ForAllType}, year={2026}, url={https://reference.wolfram.com/language/ref/ForAllType.html}, note=[Accessed: 16-November-2026]}