I want to return a Compile
d function built from a string that uses values of local variables:
f[x_] := Module[{code},
code = "Compile[{},x,CompilationTarget->\"C\"]";
ToExpression[code]
];
While f[1]
is a compiled function, f[1][]
yields an error due to x
.
Plain use of Compile
works:
g[x_] := Module[{},
Compile[{}, x, CompilationTarget -> "C"]
];
with g[1][]
returning 1
.
Why does the compilation from string not recognize the local variable?
PS: I can work around this by inserting ToString[x,InputForm]
into the string, but would like to understand better what happens.
PPS: In the original code, both parameters and code string are more involved.
Answer
I think that J.M.s advice to avoid using strings for that kind of meta-programming is the best answer you can get for that question. Looking up the meta-programming question to gather ideas how you can better tackle your specific problem is certainly worth the effort, for completeness here is his suggestion for your simplified example once more:
f[x_] := Module[{code},
code = Hold[Compile[{}, x, CompilationTarget -> "C"]];
ReleaseHold[code]
];
If you are new to Mathematica or have done similar things in other languages, you might think it is easier to manipulate strings but in Mathematica (and other homoiconic languages) it is in fact easier and much safer to manipulate held expressions. Due to its powerful pattern matcher Mathematica makes many of these tasks quite easy, although it sometimes is a bit tricky to handle evaluation order correctly.
You have also asked why your string approach doesn't work. To understand that you have to understand that at a deeper level a function definition in Mathematica is nothing but a definition of a global replacement rule. So evaluating a function will essentially insert the arguments literally into the RHS of the function definition and then evaluate the result. So you can think of function evaluation of some function definition like:
f[x_]:=Sin[Pi*x]
that f[5]
will actually do something like:
ReleaseHold[With[{x=5},Hold[Sin[Pi*x]]]]
or
ReleaseHold[Hold[f[5]] /. f[x_] :> Sin[Pi*x]]
This will even work for any symbol with Hold
attributes, as this example illustrates:
f[x_]:=Hold[x]
f[1]
With this picture in mind, it does probably not come as a surprise that inserting into strings does not work. This is one of the many reasons why doing meta-programming with strings is usually not the best solution.
If you really decide to go with strings (which I wouldn't suggest) you should realize that what you actually want to do here is to insert a constant into the code to compile -- when working with strings that is more like filling a string template than actually using a variable. Here is what I would do in that case:
f[x_] := Module[{code},
code = "Compile[{},`x`,CompilationTarget->\"C\"]";
ToExpression[TemplateApply[code, <|"x" -> x|>]]
];
Comments
Post a Comment