Compile-time calculation – rosetta code 7 cases movie

####

An interesting property of Ada is that such calculations at compile time are performed with mathematical (i.e., unbounded) integers for intermediate results. On a compiler with 32-bit integers (gcc), the following code prints the value of ’20 choose 10′ = 184756:

In both cases, the identifier factorial10 is given the value 3628800 without any runtime calculations, although in many (or perhaps most) BASICs the first one is handled similarly to C’s #define: if it isn’t used elsewhere in the code, it doesn’t appear at all in the final executable.

C includes a macro processor that runs at compile-time. With the use of suitably imaginative macro library includes, it can be used in a similar way to C++ template metaprogramming or Lisp macros. Like C++, and unlike Lisp, the language used is usually very different from the C language used to write runtime code.

In this example, the 8fac function computes the factorial by folding 8times (the binary multiplication primitive) over a numeric range created by 8seq_iota (which requires 8X to be incremented, as it creates lists from L to R-1), a familiar way of computing this in functional languages. The result of the factorial calculation is an internal, "native" number which need to be converted to decimal by the 8to_lit function.

Assuming a definition from Factorial function#Common Lisp, we first have to make a small adjustment so that the function is available at compile time. Common Lisp does not have a single image building and deployment model. For instance, Common Lisp implementations can support a "C like" model whereby a compiler is invoked as a separate process to handle individual files, which are then loaded to form an image (analogous to linking). A Lisp compiler will not make available to itself the functions in a source file which it happens to be compiling, unless told to do so:

It is also possible to have a value computed at load time, when the code is loaded into the process, rather than at compile time; this is useful if the value to be computed contains objects that do not yet exist at compile time, or the value might vary due to properties which might be different while yet using the same compiled program (e.g. pathnames), but it is still constant for one execution of the program:

Lastly, Common Lisp has "compiler macros" which are user-defined handlers for function call optimization. A compiler macro is defined which has the same name as some user-defined function. When calls to that function are being compiled, they pass through the macro. The macro must analyze the arguments and rewrite the function call into something else, or return the original form.

Test with CLISP (taking advantage of its ! function) showing how a factorial call with a constant argument of 10 ends up compiled to the constant 3268800, but a factorial call with the argument a is compiled to a variable access and function call:

This is a placeholder since to do something more complex than text substitution macros Erlang offers parse transformations. This is a quote from their documentation: "Programmers are strongly advised not to engage in parse transformations". Somebody can do this task, but not I.

During a word definition, you can drop out of the compilation state with [ and go back in with ]. (This is where the naming conventions for [CHAR] and [‘] come from.) There are several flavors of LITERAL for compiling the result into the word.

Outside of a word definition, it’s fuzzy. If the following code is itself followed by a test and output, and is run in a script, then the construction of the bignum array (and the perhaps native-code compilation of more) happens at runtime. If the following code is followed by a command that creates an executable, the array will not be rebuilt on each run.

Constant expressions are evaluated at compile time. A constant expression though, is pretty simple and can’t have much more than literals, operators, and a special thing called iota. There is no way to loop in a constant expression and so the expanded expression below is about the simplest way of completing this task.

Note: Doing $([|fact 10|]) is the same than doing fact 10. [|something|] returns the abstract syntax tree of something. Thus [|fact 10|] returns the AST of the call to the fact function with 10 as argument. $(something) waits for an AST from a call to something.

Note: Currently, the mediawiki implementation is corrupting the above display due to a cascading sequence of bad design decisions and mis-interpreted specifications on the part of someone "contributing" to that implementation. To work around this issue, and see the original display, you can currently use either the "Edit" or "View Source" option, depending on whether you are logged in to rosettacode with an account that has edit rights here. (Please don’t actually save changes though.) If you are using View Source, you might want to do that in a new tab (so you also stay here with this view) and use your browser’s search capability to quickly scroll to this location in the source view.

Julia includes a powerful macro feature that can perform arbitrary code transformations at compile-time (or technically at parse-time), and can also execute arbitrary Julia code. For example, the following macro computes the factorial of n (a literal constant) and returns the value (e.g. to inline it in the resulting source code)