Conditional compilation

Author(s): Jose F. Morales.

This package defines a series of directives for conditional compilation that allow the inclusion or exclusion of code blocks (which may contain nested conditional directives) based on the truth value at compile time of special goals called conditions. The syntax for conditional directives is:

:- if(Cond1).
  <<Block1>>
:- elif(Cond2).
  <<Block2>>
:- else.
  <<BlockN>>
:- endif.

where elif(_) can appear zero or more times and the else part is optional. The sentences in Block1 are included if the condition in Cond1 is satisfied, else Block2 is included if Cond2 is satisfied (and so on for each elif), and BlockN if no previous condition is satisfied.

Conditions

The valid conditions are restricted to a subset of goals that can be safely evaluated at compile time. At this moment, only the following ones are accepted:

  • Compile-time values of prolog flags (current_prolog_flag/2).
  • Conjunctions, disjunctions, or negations of conditions.
  • Calls to facts previously defined with :- compilation_fact(Fact). This is an experimental feature that may change in the future.
  • defined(F/N) (or defined(F), equivalent to defined(F/0)), which succeeds only if there is a definition for the compilation fact F/N.

Usage and interface

Known bugs and planned improvements

  • Syntax and semantics of conditions for conditional code may change in the future (:- compilation_fact(X)). Avoid using that in production code.
  • Errors in this package do not show the program line numbers.