3
$\begingroup$

I want to return a Compiled 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.

$\endgroup$
8
  • 3
    $\begingroup$ Why must you use strings? f[x_] := Module[{code}, code = Hold[Compile[{}, x, CompilationTarget -> "C"]]; ReleaseHold[code]] $\endgroup$ Jan 6, 2017 at 18:01
  • $\begingroup$ @J.M. I do not have to use strings, but it was natural to use them: The actual code is generated programmatically; in particular, some of the code structure depends on parameters, while other parts are boilerplate code. Strings offered a simple way to avoid redundancy and assemble the code to be compiled. $\endgroup$
    – mrupp
    Jan 6, 2017 at 18:11
  • 1
    $\begingroup$ Judicious use of With[] for insertion, along with Hold[]/ReleaseHold[] seems to me a more flexible way to go about it. You might be interested in looking up past meta-programming questions. $\endgroup$ Jan 6, 2017 at 18:14
  • $\begingroup$ try Evaluate[x] in your string (sory untested) $\endgroup$
    – george2079
    Jan 6, 2017 at 18:25
  • 1
    $\begingroup$ I expect you realize but to be sure your compiled code is attempting to reference a global symbol x which does not exist. If you do x=3;f[1][] you get 3. (Not useful, but it should help see whats going on.). $\endgroup$
    – george2079
    Jan 6, 2017 at 20:38

1 Answer 1

7
$\begingroup$

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|>]]
];
$\endgroup$
5
  • $\begingroup$ The string solution is essentially what I do right now. The With solution is still giving me trouble. In the actual code, I need to insert blocks of code that refer to variables defined in a template code. Schematically, block = ...code using x and y...; template = ...code defining y; block; more code...; return compiled template. Could you provide an example for the With/Hold/ReleaseHold approach that does that? $\endgroup$
    – mrupp
    Jan 6, 2017 at 23:12
  • $\begingroup$ The string-based equivalent would be f[x_] := Module[{template, code, block}, block = StringReplace["z = y*#x#;", "#x#" -> ToString[x, InputForm]]; template = "Compile[{}, Module[{y,z}, y = 2; #block# z], CompilationTarget->\"C\"]"; code = StringReplace[template, "#block#" -> block]; ToExpression[code] ]; for which f[3][] yields 6. $\endgroup$
    – mrupp
    Jan 6, 2017 at 23:27
  • $\begingroup$ @mrupp, that should probably have been a separate question, but: f[x_] := Block[{template, block, y, z}, block = Hold[Unevaluated[z = y x]]; template = Hold[Compile[{}, Module[{y, z}, y = 2; #; z], CompilationTarget -> "C"]] &; template @@ block // ReleaseHold]. Note the use of Block[] (dynamic scoping). $\endgroup$ Jan 7, 2017 at 10:04
  • $\begingroup$ @mrupp: I think J.M. is right in suggesting to make this an own question which probably could refer to this one and should contain your string-based code so people have something to start with. I think that question could trigger some interesting answers... $\endgroup$ Jan 7, 2017 at 11:19
  • $\begingroup$ I created a separate question mathematica.stackexchange.com/questions/134911/… $\endgroup$
    – mrupp
    Jan 7, 2017 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.