Chapter 9

Modules

On this page

A module is a bundle of predicate and function definitions that are stored in one file. A module forms a namespace. Two definitions can have the same name if they reside in different modules. Because modules avoid name clashes, they are very useful for managing source files of large programs.

9.1 Module and Import Declarations

In Picat, source files must have the extension name ".pi". A module is a source file that begins with a module name declaration in the form:

  module Name.

where Name must be the same as the main file name. A file that does not begin with a module declaration is assumed to belong to the default global module. The following names are reserved for system modules and should not be used to name user modules: basic, bp, cp, datetime, glb, io, math, mip, nn, ordset, os, planner, sat, smt, sys, and util.

In order to use symbols that are defined in another module, users must explicitly import them with an import declaration in the form:

  import Name1, …, Namen .

where each imported Namei is a module name. For each imported module, the compiler searches for it first in the importing source file’s directory and then in the search path that is specified by the environment variable PICATPATH. If no module is found, the compiler gives an error message. Several modules are imported by default, including basic, math, io, and sys.

The import relation is not transitive. Suppose that there are three modules: A, B, and C. If A imports B and B imports C, then A still needs to import C in order to reference C’s symbols.

The built-in command cl("xxx") compiles the file xxx.pi and loads the generated code into the interpreter. The built-in command load("xxx") loads the bytecode file xxx.qi. It compiles the source file xxx.pi only when necessary. The load command also imports the public symbols defined in the module to the interpreter. This allows users to use these symbols on the command line without explicitly importing the symbols. If the file xxx.pi imports modules, those module files will be compiled and loaded when necessary.

A program file can contain include directives of the form:

  include Name.

where Name is a string indicating a file name relative to the program file. The include directive causes the content of the file named Name to be copied verbatim to where the directive occurs. The include directive allows users to split a large program into several source files. Note that the included source files cannot contain module or import declarations, and no definitions can span multiple files.

9.2 Binding Calls to Definitions

The Picat system has a global symbol table for atoms, a global symbol table for structure names, and a global symbol table for modules. For each module, Picat maintains a symbol table for the public predicate and function symbols defined in the module. Private symbols that are defined in a module are compiled away, and are never stored in the symbol table. While predicate and function symbols can be local to a module, atoms and structures are always global.

The Picat module system is static, meaning that the binding of normal (or non-higher-order) calls to their definitions takes place at compile time. For each call, the compiler first searches the default modules for a definition that has the same name as the call. If no definition is found, then the compiler searches for a definition in the enclosing module. If no definition is found, the compiler searches the imported modules in the order that they were imported. If no definition is found in any of these modules, then the compiler will issue a warning1 , assuming the symbol is defined in the global module.

It is possible for two imported modules to contain different definitions that have the same name. When multiple names match a call, the order of the imported items determines which definition is used. Picat allows users to use qualified names to explicitly select a definition. A module-qualified call is a call preceded by a module name and ’.’ without intervening whitespace.

Example

% qsort.pi
module qsort.

sort([]) = [].
sort([H|T]) = 
  sort([E : E in T, E =< H]) ++ [H] ++ sort([E : E in T, E > H]).

% isort.pi
module isort.

sort([]) = [].
sort([H|T]) = insert(H, sort(T)).

private
insert(X, []) = [X].
insert(X, Ys@[Y|_]) = Zs, X =< Y => Zs = [X|Ys].
insert(X, [Y|Ys]) = [Y|insert(X,Ys)].

The module qsort defines a function named sort using quick sort, and the module isort defines a function of the same name using insertion sort. In the following session, both modules are used.

Picat> load("qsort")
Picat> load("isort")
Picat> L = sort([2,1,3])
L = [1,2,3]
Picat> L = qsort.sort([2,1,3])
L = [1,2,3]
Picat> L = isort.sort([2,1,3])
L = [1,2,3]

As sort is also defined in the basic module, which is preloaded, that function is used for the command L = sort([2,1,3]).

Module names are just atoms. Consequently, it is possible to bind a variable to a module name. Nevertheless, in a module-qualified call M.C, the module name can never be a variable. Recall that the dot notation is also used to access attributes and to call predicates and functions. The notation M.C is treated as a call or an attribute if M is not an atom.

Suppose that users want to define a function named generic_sort(M, L) that sorts list L using the sort function defined in module M. Users cannot just call M.sort(L), since M is a variable. Users can, however, select a function based on the value held in M by using function facts as follows:

generic_sort(qsort, L) = qsort.sort(L).
generic_sort(isort, L) = isort.sort(L).

9.3 Binding Higher-order Calls

Because Picat forbids variable module qualifiers and terms in dot notations, it is impossible to create module-qualified higher-order terms. For a higher-order call, if the compiler knows the name of the higher-order term, as in findall(X, member(X,L)), then it searches for a definition for the name, just like it does for a normal call. However, if the name is unknown, as in F(X, Y), then the compiler generates code to search for a definition. For a higher-order call, the runtime system searches modules in the following order:

  1. The implicitly imported built-in modules basic, math, io, and sys.
  2. The current list of loaded modules that is returned by the built-in loaded_modules(), excluding the pre-imported built-in modules.
  3. The global module.

For a module-qualified higher-order call such as m.F(X, Y), the runtime system first searches module m, followed by the implicitly imported built-in modules and the global module.

As private symbols are compiled away at compile time, higher-order terms can never reference private symbols. Due to the overhead of runtime search, the use of higher-order calls is discouraged.

9.4 Library Modules

Picat comes with a library of standard modules, described in separate chapters. The function sys.loaded_modules() returns a list of modules that are currently in the system.

1A warning is issued instead of an error. This allows users to test incomplete programs with missing definitions.