Packages and language extension

Author(s): Daniel Cabeza, The Ciao Development Team.

This chapter documents package files and the main builtin directives that Ciao provides for implementing syntactic and semantic language extensions.

These directives allow the definition of new operators, new declarations, code translations, etc. Although these directives can be used directly in any source file, it is highly recommended to group them into packages, where each package contains a set of operators and transformations that together provide a particular syntactic and semantic extension to the modules that load it. Most Ciao extensions, such as functional syntax, constraint solving, or breadth-first search are implemented as packages. The structure and code of these Ciao library packages can serve as useful examples of how to build packages in general.

See also [CH00a] for a detailed description of the Ciao module system and, in particular, packages and the language extension primitives.


Usage and interface

  • Library usage:
    These directives are builtin in Ciao, so nothing special has to be done to use them.

Documentation on internals

DECLARATIONpackage/1

Usage::- package(Name).

Declares the current file to be a package of name Name. Like in modules, Name must match the name of the file where the package resides, without extension. This directive must appear first in the file.

  • The following properties should hold at call time:
    (modules:modulename/1)Name is a module name (an atom).

DECLARATIONop/3

Usage:ISO:- op(Priority,Op_spec,Operator).

Updates the operator table for reading the terms in the rest of the current text, in the same way as the builtin op/3 does. Its scope is local to the current text. Usually included in package files.

Usage::- new_declaration(Predicate).

Declares Predicate to be a valid declaration in the rest of the current text. Such declarations are simply ignored by the compiler or top level, but can be used by other code processing programs such as an automatic documentator. Also, they can easily translated into standard code (a set of facts and/or rules) by defining a suitable expansion (e.g., by add_sentence_trans/1, etc.). This is tipically done in package files.

Equivalent to new_declaration(Predicate, off).

Usage::- new_declaration(Predicate,In_Itf).

Declares Predicate to be a valid declaration in the rest of the current text. Such declarations will be included in the interface file for this file if In_Itf is 'on', not if it is 'off'. Including such declarations in interface files makes them visible while processing other modules which make use of this one.

Usage::- load_compilation_module(File).

Loads code defined in File into the compiler, usually including predicates which define translations of clauses, sentences, and terms, for use with the declarations add_sentence_trans/2 and similar ones. The application order of translations is determined by ascending priority numbers. Normally included in package files.

Usage::- add_sentence_trans(Predicate,Priority).

Starts a translation, defined by Predicate, of the terms read by the compiler in the rest of the current text. For each subsequent term read by the compiler, the translation predicate is called to obtain a new term which will be used by the compiler as if it where the term present in the file. If the call fails, the term is used as such. A list may be returned also, to translate a single term into several terms. Before calling the translation predicate with actual program terms, it is called with an input of 0 to give an opportunity of making initializations for the module, discarding the result (note that normally a 0 could not be there). Predicate must be exported by a module previously loaded with a load_compilation_module/1 declaration. Normally included in package files.

DECLARATIONadd_term_trans/2

Usage::- add_term_trans(P,Priority).

Starts a translation, defined by Predicate, of the terms and sub-terms read by the compiler in the rest of the current text. This translation is performed after all translations defined by add_sentence_trans/1 are done. For each subsequent term read by the compiler, and recursively any subterm included, the translation predicate is called to possibly obtain a new term to replace the old one. Care must be taken of not introducing an endless loop of translations. Predicate must be exported by a module previously loaded with a load_compilation_module/1 declaration. Normally included in package files.

DECLARATIONadd_goal_trans/2

Usage::- add_goal_trans(Predicate,Priority).

Declares a translation, defined by Predicate, of the goals present in the clauses of the current text. This translation is performed after all translations defined by add_sentence_trans/1 and add_term_trans/1 are done. For each clause read by the compiler, the translation predicate is called with each goal present in the clause to possibly obtain other goal to substitute the original one, and the translation is subsequently applied to the resulting goal. Care must be taken of not introducing an endless loop of translations. Predicate must be exported by a module previously loaded with a load_compilation_module/1 declaration. Bear in mind that this type of translation noticeably slows down compilation. Normally included in package files.

Usage::- add_clause_trans(Predicate,Priority).

Declares a translation, defined by Predicate, of the clauses of the current text. The translation is performed before add_goal_trans/1 translations but after add_sentence_trans/1 and add_term_trans/1 translations. The usefulness of this translation is that information of the interface of related modules is available when it is performed. For each clause read by the compiler, the translation predicate is called with the first argument instantiated to a structure clause(Head,Body), and the predicate must return in the second argument a similar structure, without changing the functor in Head (or fail, in which case the clause is used as is). Before executing the translation predicate with actual clauses it is called with an input of clause(0,0), discarding the result.

A translation predicate is a predicate of arity 2 or 3 used to make compile-time translations. The compiler invokes a translation predicate instantiating its first argument with the item to be translated, and if the predicate is of arity 3 its third argument with the name of the module where the translation is done. If the call is successful, the second argument is used as if that item were in the place of the original, else the original item is used.

Usage:translation_predname(P)

P is a translation predicate spec (has arity 2 or 3).

    Documentation on imports

    This module has the following direct dependencies: