4.11.7 The Source Module

For any given procedure call, or goal, the source module is the module in which the corresponding predicate must be visible. That is, unless the predicate is built-in, it must be defined in, or imported into, the source module.

For goals typed at the top-level, the source module is the type-in module, which is user by default—see ref-mod-tyi. For goals appearing in a file (either as goal clauses or as normal clauses), the source module is the one into which that file has been loaded.

There are a number of built-in predicates that take predicate specifications, clauses, or goals as arguments. Each of these types of argument must be understood with reference to some module. For example, assert/1 takes a clause as its argument, and it must decide into which module that clause should be asserted. The default assumption is that it asserts the clause into the source module. Another example is call/1. The goal (A) calls the predicate foo/1 in the source module; this ensures that in the compound goal (B) both occurrences of foo/1 refer to the same predicate.

     call(foo(X)) (A)
     
     call(foo(X)), foo(Y)  (B)

All predicates that refer to the source module allow you to override it by explicitly naming some other module to be used instead. This is done by prefixing the relevant argument of the predicate with the module to be used followed by a ‘:’ operator. For example (C), asserts f(x) in module m.

     | ?- assert(m:f(x)). (C)

Note that if you call a goal in a specified module, overriding the normal visibility rules (see ref-mod-vis), the source module for that goal is the one you specify, not the module in which this call occurs. For example (D), has exactly the same effect as (C)—f(x) is asserted in module m. In other words, prefixing a goal with a module duplicates the effect of calling that goal from that module.

     | ?- m:assert(f(x)). (D)

Another built-in predicate that refers to the source module is compile/1. In this case, the argument is a file, or list of files, rather than a predicate specification, clause, or goal. However, in the case where a file is not a module-file, compile/1 must decide into which module to compile its clauses, and it chooses the source module by default. This means that you can compile a file File into a specific module M using

     | ?- compile(M:File).

Thus if File is a module-file, this command would cause its public predicates to be imported into module M. If File is a non-module-file, it is loaded into module M.

For a list of the built-in predicates that depend on the source module, see ref-mod-mne. In some cases, user-defined predicates may also require the concept of a source module. This is discussed in ref-mod-met.


Send feedback on this subject.