YAP 7.1.0
Module Support

More...

Detailed Description

Files

file  newmod.yap
 support for creating a new module
 

Class Documentation

◆ use_module/1

class use_module/1

use_module( +Files )

properties: directive

loads a module file

This predicate loads the file specified by Files, importing all their public predicates into the current type-in module It is implemented as if by:

load_files(F, [if(not_loaded),must_be_module(true)]).
use_module( +Files )
load_files(+_Files_, +_Options_)

Notice that Files may be a single file, or a list with a number files The Files are loaded in YAP only once, even if they have been updated meanwhile YAP should also verify whether the files actually define modules Please consult load_files/3 for other options when loading a file

Predicate name clashes between two different modules may arise, either when trying to import predicates that are also defined in the current type-in module, or by trying to import the same predicate from two different modules

In the first case, the local predicate is considered to have priority and use_module/1 simply gives a warning As an example, if the file a.pl contains:

:- a/1module( a, [] ).
:- use_module(b).
a(1).
a(X) :- b(X).
module(+M)

and the file b.pl contains:

:- a/1b/1module( b, [,] ).
a(2).
b(1).

YAP will execute as follows:

?- [a].
% consulting .../a.pl...
% consulting .../b.pl...
% consulted .../b.pl in module b, 0 msec 0 bytes
% consulted .../a.pl in module a, 1 msec 0 bytes
.
?- a(X).
X = 1 ? ;
X = 1.

The example shows that the query a(X)has a single answer, the one defined in a.pl Calls to a(X)succeed in the top-level, because the module a was loaded into user On the other hand, b(X)is not exported by a.pl, and is not available to calls, although it can be accessed as a predicate in the module 'a' by using the : operator

Next, consider the three files c.pl, d1.pl, and d2.pl:

% c.pl
:- a/1module( c, [] ).
:- use_module([d1, d2]).
a(X) :-
b(X).
a(X) :-
c(X).
a(X) :-
d(X).
% d1.pl
:- module( d1, [b/1,c/1] ).
b(2).
c(3).
% d2.pl
:- module( d2, [b/1,d/1] ).
b(1).
d(4).

The result is as follows:

dd -d d
YAP 6.3.4 (x86_64-darwin13.3.0): Tue Jul 15 10:42:11 CDT 2014
ERROR
3 /.,
PERMISSION ERROR- .: @ b/1
?- a(X).
X = 2 ? ;
ERRORaa
EXISTENCE ERROR- a @a c/1 is a, a a a a:$user_call/2
Goal a a:c(_131290)

The state of the module system after this error is undefined

BuiltIn predicates {#ModuleBuiltins)

\pred module(+ M:atom,+ L:list ) is directive the current file defines module M with exports L The list may include

  • predicate indicators
  • operator definitions that look like calls to op/3

The list L may include predicates imported from other modules If you want to fully reexport a module, or a sub-set, also consider reexport/1

Similar to module/2 , this directive defines the file where it appears in as a module file; it must be the first declaration in the file M must be an atom specifying the module name; L must be a list containing the module's public predicates specification, in the form [predicate_name/arity,...]

The last argument Options must be a list of options, which can be:

  • filename the filename for a module to import into the current module
  • library( +File ) a library file to import into the current module
  • hide( +Opt) if Opt is false, keep source code for current module, if true, disable
  • export(+PredicateIndicator ) Add predicates to the public list of the context module This implies the predicate will be imported into another module if this module is imported with use_module/1 and use_module/2
  • export_list(? Mod,? ListOfPredicateIndicator) The list ListOfPredicateIndicator contains all predicates exported by module Mod

Note that predicates are normally exported using the directive module/2 The export/1 argumwnt is meant to allow export from dynamically created modules The directive argument may also be a list of predicates

properties: directive

load a module file

This predicate loads the file specified by Files, importing all their public predicates into the current type-in module It is implemented as if by:

use_module(F) :-
load_files(F, [if(not_loaded),must_be_module(true)]).

Notice that Files may be a single file, or a list with a number files The Files are loaded in YAP only once, even if they have been updated meanwhile YAP should also verify whether the files actually define modules Please consult load_files/3 for other options when loading a file

Predicate name clashes between two different modules may arise, either when trying to import predicates that are also defined in the current type-in module, or by trying to import the same predicate from two different modules

In the first case, the local predicate is considered to have priority and use_module/1 simply gives a warning As an example, if the file a.pl contains:

:- module( a, [a/1] ).
:- use_module(b).
a(1).
a(X) :- b(X).

and the file b.pl contains:

:- module( b, [a/1,b/1] ).
a(2).
b(1).

YAP will execute as follows:

?- [a].
consulting .../a.pl...
consulting .../b.pl...
consulted .../b.pl in module b, 0 msec 0 bytes
consulted .../a.pl in module a, 1 msec 0 bytes
true.
?- a(X).
X = 1 ? ;
X = 1.

The example shows that the query a(X)has a single answer, the one defined in a.pl Calls to a(X)succeed in the top-level, because the module a was loaded into user On the other hand, b(X)is not exported by a.pl, and is not available to calls, although it can be accessed as a predicate in the module 'a' by using the : operator

Next, consider the three files c.pl, d1.pl, and d2.pl:

c.pl
:- module( c, [a/1] ).
:- use_module([d1, d2]).
a(X) :-
b(X).
a(X) :-
c(X).
a(X) :-
d(X).
d1.pl
:- module( d1, [b/1,c/1] ).
vvb(2).
c(3).
d2.pl
:- module( d2, [b/1,d/1] ).
b(1).
d(4).

The result is as follows:

./yap -l c
YAP 6.3.4 (x86_64-darwin13.3.0): Tue Jul 15 10:42:11 CDT 2014
ERROR!!
at line 3 in o/d2.pl,
PERMISSION ERROR- loading .../c.pl: modules d1 and d2 both define @ref b/1
?- a(X).
X = 2 ? ;
ERROR!!
EXISTENCE ERROR- procedure @ref c/1 is undefined, called from context prolog:$user_call/2
Goal was c:c(_131290)

The state of the module system after this error is undefined

◆ use_module/2

class use_module/2

use_module(+Files, +Imports)

loads a module file but only imports the named predicates

This predicate loads the file specified by Files, importing their public predicates specified by Imports into the current type-in module It is implemented as if by:

use_module(Files, Imports) :-
load_files(Files, [if(not_loaded),must_be_module(true),imports(Imports)]).
use_module(+Files, +Imports)

The Imports argument may be use to specify which predicates one wants to load It can also be used to give the predicates a different name As an example, the graphs library is implemented on top of the red-black trees library, and some predicates are just aliases:

:- rb_min/3rb_max/3use_module(library(rbtrees), [
@ref as min_assoc,
@ref as max_assoc,
...]).
rb_max( +T, -Key, -Value)
rb_min(+T, -Key, -Value)

Unfortunately it is still not possible to change argument order

loads a module file but only imports the named predicates

This predicate loads the file specified by Files, importing their public predicates specified by Imports into the current type-in module It is implemented as if by:

use_module(Files, Imports) :-
load_files(Files, [if(not_loaded),must_be_module(true),imports(Imports)]).

The Imports argument may be use to specify which predicates one wants to load It can also be used to give the predicates a different name As an example, the graphs library is implemented on top of the red-black trees library, and some predicates are just aliases:

:- use_module(library(rbtrees), [
@ref rb_min/3 as min_assoc,
@ref rb_max/3 as max_assoc,
...]).

Unfortunately it is still not possible to change argument order

◆ use_module/3

class use_module/3

use_module(? M,? F,+ L)

SICStus directive for loading a module, it can operate in two different ways:

  1. If the module M is given, import the procedures in L from M to the current source module
  2. Otherwise, operate as use_module/2 , and load the files specified by F, importing the predicates specified in the list L

◆ current_module/1

class current_module/1

current_module( ? Mod:atom)

properties: nondeterministic

Succeeds if M is a user-visible modules A module is defined as soon as some predicate defined in the module is loaded, as soon as a goal in the module is called, or as soon as it becomes the current type-in module

◆ current_module/2

class current_module/2

current_module( ? Mod:atom, ? F : file )

properties: nondeterministic

Succeeds if M is a module associated with the file F, that is, if File is the source for M If M is not declared in a file, F unifies with user

◆ abolish_module/1

class abolish_module/1

abolish_module( + Mod)

properties: deterministic

get rid of a module and of all predicates included in the module

◆ set_base_module/1

class set_base_module/1

set_base_module( +Expor tingModule )

properties: deterministic

All predicates exported from ExportingModule are automatically available to the other source modules

This built-in was introduced by SWI-Prolog In YAP, by default, modules only inherit from prolog This extension allows predicates in the current module (see module/2 and module/1 ) to inherit from user or other modules

Definition at line 2 of file modules.yap.

◆ import_module/2

class import_module/2

import_module( +ImportingModule, +ExportingModule )

properties: deterministic

All exported predicates from ExportingModule are automatically available to the source module ImportModule

This innovation was introduced by SWI-Prolog By default, modules only inherit from prolog and user This extension allows predicates in any module to inherit from user and other modules

◆ add_import_module/3

class add_import_module/3

add_import_module( + Module, + ImportModule , +_Pos_)

properties: deterministic

Add all exports in ImportModule as available to Module

All exported predicates from ExportModule are made available to the source module ImportModule If Position is bound to start the module ImportModule is tried first, if Position is bound to end, the module is consulted last

◆ delete_import_module/2

class delete_import_module/2

delete_import_module( + ExportModule, + ImportModule )

properties: deterministic

Exports in ImportModule are no longer available to Module

All exported predicates from ExportModule are discarded from the ones used vy the source module ImportModule

◆ module_property/2

class module_property/2

module_property( +Module, ? Property )

properties: nondeterministic

Enumerate non-deterministically the main properties of Module

Reports the following properties of Module:

  • class( ?_Class_ ): whether it is a system, library, or user module
  • line_count(?_Ls_): number of lines in source file (if there is one)
  • file(?_F_): source file for Module (if there is one)
  • exports(-Es): list of all predicate symbols and operator symbols exported or re-exported by this module