Is there a way to extract the types of the arguments and the return type from a CompiledFunction expression? I am writing a package for code generation (basically a wrapper around CCodeGenerate) where the user supplies CompiledFunction expressions, and it would be nice if I could extract argument and return types directly from these expressions. (Otherwise the user has to specify them correctly, and that's a source of errors.)

  • 1
    $\begingroup$ When cf = Compile[...]; you can get the types of the arguments with cf[[2]]. The index 2 can be determined by inspecting FullForm[cf] and might be version dependent, so be sure to check carefully. $\endgroup$ – DaveStrider Sep 18 '14 at 11:47

The argument pattern can be read directly from the CompiledFunction expression as DaveStrider commented:

cf = Compile[{{x, _Real}, {y, _Integer}}, Round[x/y]];

{_Real, _Integer}

The result information is printed by the CompiledFunctionTools package command CompilePrint:


      2 arguments
      2 Integer registers
      3 Real registers
      Underflow checking off
      Overflow checking off
      Integer overflow checking on
      RuntimeAttributes -> {}

      R0 = A1
      I0 = A2
      Result = I1

1 R1 = I0
2 R2 = Reciprocal[ R1]
3 R1 = R0 * R2
4 I1 = Round[ R1]
5 Return

Note that A1 and A2 are the arguments, assigned to Real and Integer registers respectively. The output is an Integer (I1). This information can be more directly read from ToCompiledProcedure:

CompiledResult[Register[Integer, 1]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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