FunctionDeclaration[name,typedfun]
コンパイラ環境での使用に適した型付き関数となる name を宣言する.
FunctionDeclaration
FunctionDeclaration[name,typedfun]
コンパイラ環境での使用に適した型付き関数となる name を宣言する.
詳細
- FunctionDeclarationは宣言の記号表現で,そのままでは評価されない.
- FunctionDeclarationの型付き関数はTyped[{t1,t2,…}tout]@Function[{x1,x2,…},body]の形でよい.
- FunctionDeclaration内のTyped[{t1,t2,…}tout]@DownValuesFunction[sym]の形式の型付き関数は,sym に添付された関数宣言を使う.
- Typed[{t1,t2,…}tout]@DownValuesFunction[sym]の形の型付き関数FunctionDeclarationは,計算にWolfram Engineを呼び出す.
- FunctionDeclarationは,CompilerEnvironmentAppendToの中およびFunctionCompileのような関数の第1引数で使うことができる.
- FunctionDeclarationで使われる名前は,型が異なる限り複数の宣言で使うことができる.
- FunctionDeclarationの中で宣言された関数は,他の宣言で使うことができる.
- FunctionDeclarationの中で宣言された関数は,ForAllTypeを使って複数の型について定義することができる.
- FunctionDeclarationの中で使われる型はTypeDeclarationが宣言する新たな型から構築できる.
- 外部ライブラリからエキスポートされた関数はLibraryFunctionDeclarationによって宣言することができる.
- 次は,サポートされるオプションである.
-
"Inline" Automatic この関数への呼出しがインラインになるべきか,関数呼出しのままになるべきか - 次は,"Inline"の可能な設定である.
-
Automatic この関数が非常に小さい場合にのみインラインにする "Hint" この関数が非常に大きくてもインラインにしようとする "Never" この関数はインラインにしない "Always" この関数は常にインラインにする
例題
すべて開く すべて閉じる例 (8)
FunctionDeclarationはFunctionCompileで使うことができる:
decl = FunctionDeclaration[AddTwo, Typed[{"Integer64"} -> "Integer64"]@Function[arg, 2 + arg]];cf = FunctionCompile[decl, Function[Typed[arg, "Integer64"], AddTwo[arg]]]cf[200]FunctionDeclarationを使ってデフォルトのコンパイラ環境に定義を加えることができる:
CompilerEnvironmentAppendTo[FunctionDeclaration[AddTwo, Typed[{"Integer64"} -> "Integer64"]@Function[arg, 2 + arg]]];cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, AddTwo[arg]]]cf[10]$CompilerEnvironment = CreateCompilerEnvironment[];FunctionDeclarationを使って新たなコンパイラ環境に宣言を加えることができる:
env = CreateCompilerEnvironment[];
CompilerEnvironmentAppendTo[env, {
FunctionDeclaration[AddTwo, Typed[{"Integer64"} -> "Integer64"]@Function[arg, 2 + arg]]}];CompilerEnvironmentオプションを使って環境を指定する:
cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, AddTwo[arg]], CompilerEnvironment -> env];cf[1]decl1 = FunctionDeclaration[CheckNumber, Typed[{"Integer64"} -> "Integer64"]@Function[arg, If[arg > 2, arg, 2]]];
decl2 = FunctionDeclaration[CheckNumber, Typed[{"Integer64", "Integer64"} -> "Integer64"]@Function[{arg1, arg2}, If[arg1 > arg2, arg1, arg2]]];
CompilerEnvironmentAppendTo[{decl1, decl2}];cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, CheckNumber[arg]]];cf[1]cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, CheckNumber[arg, 4]]];cf[3]$CompilerEnvironment = CreateCompilerEnvironment[];decl1 = FunctionDeclaration[CheckNumber1, Typed[{"Integer64"} -> "Integer64"]@Function[arg, CheckNumber2[arg, 2]]];
decl2 = FunctionDeclaration[CheckNumber2, Typed[{"Integer64", "Integer64"} -> "Integer64"]@Function[{arg1, arg2}, If[arg1 > arg2, arg1, arg2]]];cf = FunctionCompile[{decl1, decl2}, Function[{Typed[arg, "Integer64"]}, CheckNumber1[arg]]];cf[1]decl = FunctionDeclaration[printReturn, Typed[ForAllType[v, {v} -> v]]@Function[arg, Print[arg];arg]];cf = FunctionCompile[decl, Function[{Typed[arg, "Integer64"]}, printReturn[arg]]];cf[1]cf = FunctionCompile[decl, Function[{Typed[arg, "String"]}, printReturn[arg]]];cf["hello"]関数宣言には,:=で作性された定義を使ってDownValuesが設定できる:
allGreater[list_, val_] :=
AllTrue[list, # > val&]関数に名前と型を与え,実装がDownValuesから来ることを宣言する:
decl = FunctionDeclaration[allGreater, Typed[{"PackedArray"::["MachineInteger", 1], "MachineInteger"} -> "Boolean"]@DownValuesFunction[allGreater]];これらの定義は効率的なコンパイル済みコードに変換される点に注意のこと:
func = Function[{Typed[pa, "PackedArray"::["MachineInteger", 1]], Typed[val, "MachineInteger"]}, allGreater[pa, val]];
cf = FunctionCompile[decl, func];cf[{5, 6, 7, 9}, 2]優れているのは,このワークフローがコンパイルされていないバージョンと互換性がある点である:
func[{5, 6, 7, 9}, 2]data = Range[ 1, 10 ^ 4];
cf[data, 0]//RepeatedTimingfunc[data, 0]//RepeatedTiming関数宣言は,計算のためにWolfram Engineを呼び出すようにすることができる.
FactorIntegerを呼び出すが,コンパイル済みコードで関数に別の名前を与える:
decl = FunctionDeclaration[myFactorInteger, Typed[{"MachineInteger"} -> "PackedArray"::["MachineInteger", 2]]@KernelFunction[FactorInteger]];func = Function[{Typed[num, "Integer64"]}, myFactorInteger[num]];
cf = FunctionCompile[decl, func];計算はWolfram Engineで行われるので,コンパイル済みコードに利点がない可能性がある:
cf[5]スコープ (1)
TypeOfとTypeEvaluateを使って宣言内で型が設定できる:
type = ForAllType[{elem},
Element[elem, "Numbers"],
{elem} -> TypeEvaluate[TypeOf[Re[elem]]]];cf = FunctionCompile[FunctionDeclaration[MyFun1, Typed[type]@Function[x, Re[x]]], Function[Typed[arg, "ComplexReal64"], MyFun1[arg]]]cf[4. + 5. I]オプション (1)
"Inline" (1)
decl = FunctionDeclaration[TestFunc, Typed[{"Real64"} -> "Real64"]@Function[arg, Cos[Sin[Cos[arg]]]]];FunctionCompileExportString[decl, Function[Typed[arg, "Real64"], TestFunc[arg]], "WolframIR"]"Hint"の設定は,関数がそのコーラーにマージされる確率が高いことを意味する:
decl = FunctionDeclaration[TestFunc, Typed[{"Real64"} -> "Real64"]@Function[arg, Cos[Sin[Cos[arg]]]], "Inline" -> "Hint"];FunctionCompileExportString[decl, Function[Typed[arg, "Real64"], TestFunc[arg]], "WolframIR"]decl = FunctionDeclaration[TestFunc, Typed[{"Real64"} -> "Real64"]@Function[arg, arg]];FunctionCompileExportString[decl, Function[Typed[arg, "Real64"], TestFunc[arg]], "WolframIR"]"Never"の設定は,関数がそのコーラーにマージされないことを意味する:
decl = FunctionDeclaration[TestFunc, Typed[{"Real64"} -> "Real64"]@Function[arg, arg], "Inline" -> "Never"];FunctionCompileExportString[decl, Function[Typed[arg, "Real64"], TestFunc[arg]], "WolframIR"]"Always"の設定は,関数が常にコーラーにマージされることを意味する:
decl = FunctionDeclaration[MakePtr, Typed[{} -> "RawPointer"::["Integer64"]]@Function[{}, ToRawPointer[10]], "Inline" -> "Always"];これはスタック割当てを戻すことができることを意味するので便利である:
FunctionCompileExportString[decl, Function[{}, FromRawPointer[MakePtr[]]], "WolframIR"]テクニカルノート
関連するガイド
テキスト
Wolfram Research (2021), FunctionDeclaration, Wolfram言語関数, https://reference.wolfram.com/language/ref/FunctionDeclaration.html.
CMS
Wolfram Language. 2021. "FunctionDeclaration." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/FunctionDeclaration.html.
APA
Wolfram Language. (2021). FunctionDeclaration. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FunctionDeclaration.html
BibTeX
@misc{reference.wolfram_2026_functiondeclaration, author="Wolfram Research", title="{FunctionDeclaration}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/FunctionDeclaration.html}", note=[Accessed: 06-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_functiondeclaration, organization={Wolfram Research}, title={FunctionDeclaration}, year={2021}, url={https://reference.wolfram.com/language/ref/FunctionDeclaration.html}, note=[Accessed: 06-November-2026]}