• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
    • RDF quality heuristics
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

4.3.1 Conditional compilation and program transformation
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Loading Prolog source files
          • Conditional compilation and program transformation
            • term_expansion/2
            • expand_term/2
            • goal_expansion/2
            • expand_goal/2
            • compile_aux_clauses/1
            • dcg_translate_rule/2
            • var_property/2
            • Program transformation with source layout info
            • Conditional compilation
              • if/1
              • elif/1
              • else/0
              • endif/0
    • Packages

4.3.1.2 Conditional compilation

Conditional compilation builds on the same principle as term_expansion/2, goal_expansion/2 and the expansion of grammar rules to compile sections of the source code conditionally. One of the reasons for introducing conditional compilation is to simplify writing portable code. See section C for more information. Here is a simple example:

:- if(\+source_exports(library(lists), suffix/2)).

suffix(Suffix, List) :-
        append(_, Suffix, List).

:- endif.

Note that these directives can only appear as separate terms in the input. Typical usage scenarios include:

  • Load different libraries on different dialects.
  • Define a predicate if it is missing as a system predicate.
  • Realise totally different implementations for a particular part of the code due to different capabilities.
  • Realise different configuration options for your software.
:- if(:Goal)
Compile subsequent code only if Goal succeeds. For enhanced portability, Goal is processed by expand_goal/2 before execution. If an error occurs, the error is printed and processing proceeds as if Goal has failed.
:- elif(:Goal)
Equivalent to :- else. :-if(Goal). ... :- endif. In a sequence as below, the section below the first matching elif is processed. If no test succeeds, the else branch is processed.
:- if(test1).
section_1.
:- elif(test2).
section_2.
:- elif(test3).
section_3.
:- else.
section_else.
:- endif.
:- else
Start‘else' branch.
:- endif
End of conditional compilation.

ClioPatria (version V3.1.1-40-g9d9e003)