SICStus Prolog User's Manual

SICStus Prolog User's Manual
SICStus Prolog User's Manual
by the Intelligent Systems Laboratory
Swedish Institute of Computer Science
PO Box 1263
SE-164 29 Kista, Sweden
Release 3.7
September 1998
Swedish Institute of Computer Science
[email protected]
http://www.sics.se/isl/sicstus.html
c 1995 SICS
Copyright Swedish Institute of Computer Science
PO Box 1263
SE-164 29 Kista, Sweden
Permission is granted to make and distribute verbatim copies of this manual provided the copyright
notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modied versions of this manual under the conditions
for verbatim copying, provided that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language,
under the above conditions for modied versions, except that this permission notice may be stated
in a translation approved by SICS.
Introduction
1
Introduction
Prolog is a simple but powerful programming language developed at the University of Marseilles
[Roussel 75], as a practical tool for programming in logic [Kowalski 74]. From a user's point of view
the major attraction of the language is ease of programming. Clear, readable, concise programs
can be written quickly with few errors.
For an introduction to programming in Prolog, readers are recommended to consult [Sterling &
Shapiro 86]. However, for the benet of those who do not have access to a copy of this book,
and for those who have some prior knowledge of logic programming, a summary of the language
is included. For a more general introduction to the eld of Logic Programming see [Kowalski 79].
See Chapter 2 [Prolog Intro], page 27.
This manual describes a Prolog system developed at the Swedish Institute of Computer Science.
Parts of the system were developed by the project \Industrialization of SICStus Prolog" in collaboration with Ericsson Telecom AB, NobelTech Systems AB, Infologics AB and Televerket. The
system consists of a WAM emulator written in C, a library and runtime system written in C and
Prolog and an interpreter and a compiler written in Prolog. The Prolog engine is a Warren Abstract Machine (WAM) emulator [Warren 83]. Two modes of compilation are available: in-core
i.e. incremental, and le-to-le. When compiled, a predicate will run about 8 times faster and use
memory more economically. Implementation details can be found in [Carlsson 90] and in several
technical reports available from SICS.
SICStus Prolog follows the mainstream Prolog tradition in terms of syntax and built-in predicates,
and is largely compatible with DECsystem-10 Prolog and Quintus Prolog.
2
SICStus Prolog
Acknowledgments
3
Acknowledgments
The following people have contributed to the development of SICStus Prolog:
Jonas Almgren, Johan Andersson, Stefan Andersson, Kent Boortz, Per
rand, Goran Bage, Mats Carlsson, Jesper Eskilson, Lena Flood,
Seif Haridi, Ralph Haygood, Christian Holzbaur, Key Hyckenberg, Hans
Nilsson, Mats Nylen, Greger Ottosson, Dan Sahlin, Thomas
Sjoland, Johan Widen, and Emil Astrom.
The OR-parallel execution model and parallelization of SICStus Prolog are due to
Khayri A.M. Ali and Roland Karlsson
The Industrialization of SICStus Prolog (1988-1991) was funded by
Ericsson Telecom AB, NobelTech Systems AB, Infologics AB and
Televerket under the National Swedish Information Technology
Program IT4.
The development of release 3 (1991-1995) was funded in part by
Ellemtel Utvecklings AB
This manual is based on DECsystem-10 Prolog User's Manual by
D.L. Bowen, L. Byrd, F.C.N. Pereira,
L.M. Pereira, D.H.D. Warren
The Visandor visualization tool was developed at the Technical University of Madrid, and its
inclusion into this distribution was kindly permitted by Manuel Hermenegildo.
See Chapter 29 [CLPQR], page 247, for acknowledgments relevant to the clp(Q,R) constraint solver.
See Chapter 30 [CLPFD], page 271, for acknowledgments relevant to the clp(FD) constraint solver.
Quintus and Quintus Prolog are trademarks of Quintus Computer Systems, Inc. UNIX is a trademark of Bell Laboratories. DEC is a trademark of Digital Equipment Corporation. Intel is a
trademark of Intel Corp. Microsoft and Windows are trademarks of Microsoft Corp. OS/2 is a
trademark of IBM Corp. Macintosh is a trademark of Apple Computer, Inc.
4
SICStus Prolog
Notational Conventions
5
Notational Conventions
Keyboard Characters
When referring to keyboard characters, printing characters are written thus: a, while control
characters are written like this: ^A. Thus ^C is the character you get by holding down the CTL key
while you type c. Finally, the special control characters carriage-return, line-feed and space are
often abbreviated to RET, LFD and SPC respectively.
Mode Spec
When introducing a built-in predicate, we shall present its usage with a mode spec which has the
form name(arg, ..., arg) where each arg denotes how that argument should be instantiated in goals,
and has one of the following forms:
:ArgName This argument should be instantiated to a term denoting a goal or a clause or a predicate
name, or which otherwise needs special handling of module prexes.
+ArgName
This argument should be instantiated to a non-variable term.
-ArgName This argument should be uninstantiated.
?ArgName
This argument may or may not be instantiated.
Mode specs are not only used in the manual, but are part of the syntax of the language as well.
When used in the source code, however, the ArgName part must be omitted. That is, arg must be
either :, +, -, or ?.
Predicate Spec
In the context of certain compiler declarations, we shall need the following notation: Predicates
in Prolog are distinguished by their name and their arity. The notation name /arity is therefore
used when it is necessary to refer to a predicate unambiguously; e.g. concatenate/3 species the
predicate which is named \concatenate" and which takes 3 arguments. In general, a predicate
spec takes the form name /arity. With the introduction of the module system, this spec is only
dened relative to the \current" module. An absolute predicate spec must include a module prex:
module :name /arity.
6
SICStus Prolog
Development and Runtime Systems
The full Prolog system with top-level, compiler, debugger etc. is known as the Development System.
On some platforms, both a sequential and an OR-parallel Development System can be built. The
OR-parallel Development System is also known as Muse.
OR-parallelism executes clauses and disjunctions of a predicate in parallel and is useful for search
and all-solutions style programming. No change or annotations of the programs are necessary but
sometimes more parallelism may be utilized if the program is modied. The main issue is to avoid
side eects that might serialize the search. Except for some few exceptions Muse preserves the full
functionality of SICStus Prolog. Muse is still an experimental system and is not formally supported.
It is possible to link user-written C code with a subset of SICStus Prolog to create stand-alone
applications, called Runtime Systems. Only sequential Runtime Systems are available. When
introducing a built-in predicate, any limitations on its use in Runtime Systems will be mentioned.
Function Prototypes
Whenever this manual documents a C function as part of SICStus Prolog's foreign language interface, the function prototype will be displayed in ANSI C syntax.
ISO Compliance
SICStus Prolog complies in spirit, if not in detail, with the Draft International Standard ISO/IEC
13211-1 (PROLOG: Part 1|General Core). In particular, SICStus Prolog does not oer a strictly
conforming mode which rejects uses of implementation specic features. Minor deviations also
concern e.g. the syntax, the arithmetic functions, and the error system.
To aid programmers who wish to write standard compliant programs, built-in predicates that
have a counterpart in the ISO Prolog Standard are annotated with [ISO] in this manual, where
appropriate with a comment clarifying the dierence between the SICStus and the prescribed ISO
Prolog versions.
Chapter 1: How to Run Prolog
7
1 How to Run Prolog
SICStus Prolog oers the user an interactive programming environment with tools for incrementally building programs, debugging programs by following their executions, and modifying parts of
programs without having to start again from scratch.
The text of a Prolog program is normally created in a le or a number of les using one of the
standard text editors. The Prolog interpreter can then be instructed to read in programs from these
les; this is called consulting the le. Alternatively, the Prolog compiler can be used for compiling
the le.
1.1 Getting Started
Under UNIX, SICStus Prolog is normally started from one of the shells. On other platforms, it
is normally started by clicking on an icon. However, it is often convenient to run SICStus Prolog
under GNU Emacs instead. A GNU Emacs interface for SICStus Prolog is described later (see
Section 1.11 [Emacs Interface], page 18). From a shell, SICStus Prolog is started by typing:
% sicstus [-f] [-i] [-m] [-B[abspath]] [-R[abspath]] [-p boot-path]
[-base executable ] [-r restorele ] [-l prologle ]
[-P[T] [num]] [-F num] [-a argument...]
where the ags have the following meaning:
Fast start. Don't read any initialization le (`~/.sicstusrc' or `~/.sicstus.ini') on
startup. If the ag is omitted, SICStus Prolog will consult this le on startup if it
exists.
-i
Forced interactive. Prompt for user input, even if the standard input does not appear
to be a terminal.
-m
Use malloc()/free() in the memory manager's bottom layer.
-l prolog-le
Ensure that the le prolog-le is loaded on startup.
-r saved-state
Restore the saved state saved-state on startup. This is done before any initialization
le or prolog-le is loaded.
-a argument...
where the arguments can be retrieved from Prolog by prolog_flag(argv, Args ),
which will unify Args with argument... represented as a list of atoms.
-p boot-path
Overrides the default path for the Runtime Library, from which the value of the
user:library_directory/1 fact is derived. This option is only relevant for statically
linked Development Systems (see Section 8.9 [Development Systems], page 156) as an
alternative to setting the SP_PATH variable (see Section 1.1.2 [Environment Variables],
page 10). The use of this ag is discouraged. Use the -base option instead.
-f
8
SICStus Prolog
executable
Unix platforms only. Override the binary used when executing SICStus from within
the start-script. Useful, for example, for statically linked Development Systems (see
Section 8.9 [Development Systems], page 156) to avoid having to set environment variables such as SP_PATH (see Section 1.1.2 [Environment Variables], page 10) which are
set by the start-script.
-P [num]
-PT [num]
The uninstrumented (-P) or trace (-PT) version of Muse is started. As an option, num
species the initial number of workers used (defaults to 1). See Section 3.3.1 [Muse
Exe Model], page 43. Only available if Muse has been installed.
-F num
Only useful for the Sequent Symmetry version of Muse. Its purpose is to preallocate
shared memory for num workers. If the `-F' argument is not used, num is assumed to
be equal to the default number of workers.
-B[abspath]
Creates a saved state for a development system. This option is not needed for normal
use. If abspath is given, it species the absolute pathname for the saved state. Note:
There must not be a space before the path, or it will be interpreted as a separate
option.
-R[abspath]
Equivalent to the -B option, except that it builds a saved state for a runtime system
instead.
-base
Under Unix, a saved state le can be executed directly by typing:
% le argument...
This is equivalent to:
% sicstus -r
le
[-a
argument...]
Note: Beginning with SICStus version 3.7, saved-states do not store the complete path of the
binary sp.exe. Instead, they call the start-script sicstus, which is assumed to be found in the
shell's path. If there are several versions of SICStus installed, it is up to the user to make sure that
the correct start-script is found.
Notice that the ags are not available when executing saved states|all the command line arguments
are treated as Prolog arguments.
The Development System responds with a message of identication and the prompt `|
as it is ready to accept input, thus:
SICStus 3.7: Mon Mar
| ?-
?-
' as soon
2 19:22:44 MET 1998
At this point the top-level is expecting input of a directive, i.e. a query or command. See Section 1.4
[Directives], page 12. You cannot type in clauses immediately (see Section 1.3 [Inserting Clauses],
Chapter 1: How to Run Prolog
9
page 12). While typing in a directive, the prompt (on following lines) becomes `
'. That is,
the `| ?- ' appears only for the rst line of the directive, and subsequent lines are indented.
1.1.1 Finding Libraries at Runtime
As of SICStus Prolog 3.7, development systems and runtime systems have been reorganized around
a common runtime kernel. This kernel is delivered as a shared object/DLL, which means that the
runtime linker must be able to locate it at runtime. Under Windows, the runtime linker usually
has no problem locating the correct DLLs. The rest of this section is therefore Unix-specic.
Most Unix systems rely partly on the so called runtime linker search path which tells the runtime
linker where it should look for shared objects in addition to the default paths (such as `/lib',
`/usr/lib', `/usr/local/lib', and so on).
SICStus' start-script xes this by setting the runtime linker search path variable together with SP_
PATH (See Section 1.1.2 [Environment Variables], page 10) to their correct values. There is also a
ag -base to the start-script which forces it to use a dierent executable. This is very useful when
bulding statically linked Development Systems (see Section 8.9 [Development Systems], page 156).
By using the -base ag, the start-script can be used also for these. For example, to execute a
statically linked Development System called myapp:
% sicstus -base myapp
However, this is only possible to do with Development Systems. Runtime Systems cannot be started
using the SICStus start-script and therefore may have to set the runtime linker search path variable
explicitly. The following is how a typical error looks like when the runtime linker cannot nd the
SICStus runtime kernel:
ld.so.1: /home/bob/mysicstus/lib/sicstus37beta/bin/sp.exe: fatal: \
libsprt37.so: can't open file: errno=2
Killed
The cure for this is to explicitly set the runtime linker search path variable to contain the path to
`libsprt37.so'. For sh, bash, etc. this is done by:
% export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/home/bob/mysicstus/lib
and for csh, tcsh, etc. by:
% setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/home/bob/mysicstus/lib
The runtime linker search path variable is not always called LD_LIBRARY_PATH; this depends on
which runtime linker the system uses and how it is congured. Other possible names are LD_RUN_
PATH, LIBRARY_PATH, and SHLIB_PATH. The latter is mostly used under IRIX. Check your system
documentation or ask your system administrator for details.
10
SICStus Prolog
1.1.2 Environment Variables
The following environment variables can be set before starting SICStus Prolog. Some of these
override the default sizes of certain areas. The sizes are given in bytes, but may be followed by K
or M meaning kilobytes or megabytes respectively.
SP_CTYPE
SP_PATH
TMPDIR
Selects the appropriate character set standard: The supported values are euc (for EUC)
and iso_8859_1 (for ISO 8859/1). The latter is the default.
This environment variable is used to determine the location of the Runtime Library. It
is normally not necessary to set this environment variable; under UNIX it is hard-coded
into the start-up script and under Windows it is stored in the repository.
However, under some circumstances it is necessary to set this variable explicitly, for
example when executing Runtime System which do not specify a path in the third
argument to SP_initialize().
If set, indicates the pathname where temporary les should be created. Defaults to
`/usr/tmp'.
GLOBALSTKSIZE
Governs the initial size of the global stack.
LOCALSTKSIZE
Governs the initial size of the local stack.
CHOICESTKSIZE
Governs the initial size of the choicepoint stack.
TRAILSTKSIZE
Governs the initial size of the trail stack.
PROLOGINITSIZE
Governs the size of Prolog's initial memory allocation.
PROLOGMAXSIZE
Denes a limit on the amount of data space which Prolog will use.
PROLOGINCSIZE
Governs the amount of space Prolog asks the operating system for in any given memory
expansion.
PROLOGKEEPSIZE
Governs the size of space Prolog retains after performing some computation. By default,
Prolog gets memory from the operating system as the user program executes and
returns all free memory back to the operating system when the user program does not
need any more. If the programmer knows that her program, once it has grown to a
certain size, is likely to need as much memory for future computations, then she can
advise Prolog not to return all the free memory back to the operating system by setting
this variable. Only memory that is allocated above and beyond PROLOGKEEPSIZE is
returned to the OS; the rest will be kept.
Chapter 1: How to Run Prolog
11
Send bug reports to <[email protected]>. Bugs tend actually to be xed if they can be
isolated, so it is in your interest to report them in such a way that they can be easily reproduced.
The mailing list <[email protected]> is a mailing list for communication among users and
implementors. To [un]subscribe, write to <[email protected]>.
1.2 Reading in Programs
A program is made up of a sequence of clauses, possibly interspersed with directives to the system.
The clauses of a predicate do not have to be immediately consecutive, but remember that their
relative order may be important (see Section 2.3 [Procedural], page 34).
To input a program from a le le, just type the lename inside list brackets (followed by . and
RET), thus:
| ?- [le ].
This instructs the interpreter to read in (consult) the program. Note that it may be necessary to
surround the whole le specication le with single quotes to make it a legal Prolog atom; e.g.
| ?- ['myfile.pl'].
| ?- ['/usr/prolog/somefile'].
The specied le is then read in. Clauses in the le are stored so that they can later be interpreted,
while any directives are obeyed as they are encountered. When the end of the le is found, the
interpreter displays on the standard error stream the time spent for read-in. This indicates the
completion of the directive.
Predicates that expect the name of a Prolog source le as an argument use
absolute_file_name/2 (see Section 7.1.5 [Stream Pred], page 89) to look up the le. This predicate
will rst search for a le with the sux `.pl' added to the name given as an argument. If this fails
it will look for a le with no extra sux added. There is also support for libraries.
In general, this directive can be any list of lenames, such as:
| ?- [myprog,extras,tests].
In this case all three les would be consulted.
The clauses for all the predicates in the consulted les will replace any existing clauses for those
predicates, i.e. any such previously existing clauses in the database will be deleted.
Note that consult/1 in SICStus Prolog behaves like reconsult/1 in DEC-10 Prolog.
12
SICStus Prolog
1.3 Inserting Clauses at the Terminal
Clauses may also be typed in directly at the terminal, although this is only recommended if the
clauses will not be needed permanently, and are few in number. To enter clauses at the terminal,
you must give the special directive:
| ?- [user].
|
and the new prompt `| ' shows that the system is now in a state where it expects input of clauses
or directives. To return to top level, type ^D. The system responds thus:
{user consulted, 20 msec 200 bytes}
1.4 Directives: Queries and Commands
Directives are either queries or commands. Both are ways of directing the system to execute some
goal or goals.
In the following, suppose that list membership has been dened by loading the following clauses
from a le:
member(X, [X|_]).
member(X, [_|L]) :- member(X, L).
(Notice the use of anonymous variables written `_'.)
1.4.1 Queries
The full syntax of a query is `?-' followed by a sequence of goals. The top level expects queries.
This is signaled by the initial prompt `| ?- '. Thus a query at top level looks like:
| ?- member(b, [a,b,c]).
Remember that Prolog terms must terminate with a full stop (., possibly followed by layout text),
and that therefore Prolog will not execute anything until you have typed the full stop (and then
RET) at the end of the query.
If the goal(s) specied in a query can be satised, and if there are no variables as in this example,
then the system answers
yes
and execution of the query terminates.
If variables are included in the query, then the nal value of each variable is displayed (except for
variables whose names begin with _). Thus the query
Chapter 1: How to Run Prolog
13
| ?- member(X, [a,b,c]).
would be answered by
X = a
At this point the system is waiting for input of either just a RET or else a ; followed by RET. Simply
typing RET terminates the query; the system responds with `yes'. However, typing ; causes the
system to backtrack (see Section 2.3 [Procedural], page 34) looking for alternative solutions. If no
further solutions can be found it outputs `no'.
The outcome of some queries is shown below, where a number preceded by _ is a system-generated
name for a variable.
| ?- member(X, [tom,dick,harry]).
X = tom ;
X = dick ;
X = harry ;
no
| ?- member(X, [a,b,f(Y,c)]), member(X, [f(b,Z),d]).
X = f(b,c),
Y = b,
Z = c
yes
| ?- member(X, [f(_),g]).
X = f(_A)
yes
| ?-
Commands are like queries except that
1. Variable bindings are not displayed if and when the command succeeds.
2. You are not given the chance to backtrack through other solutions.
1.4.2 Commands
Commands start with the symbol `:-'. Any required output must be programmed explicitly; e.g.
the command:
14
SICStus Prolog
:- member(3, [1,2,3]), write(ok).
directs the system to check whether 3 belongs to the list [1,2,3]. Execution of a command
terminates when all the goals in the command have been successfully executed. Other alternative
solutions are not sought. If no solution can be found, the system prints:
{Warning: Goal - goal failed}
as a warning.
The principal use for commands (as opposed to queries) is to allow les to contain directives which
call various predicates, but for which you do not want to have the answers printed out. In such
cases you only want to call the predicates for their eect, i.e. you don't want terminal interaction
in the middle of consulting the le. A useful example would be the use of a directive in a le which
consults a whole list of other les, e.g.
:- [ bits, bobs, main, tests, data, junk ].
If a command like this were contained in the le `myprog' then typing the following at top-level
would be a quick way of reading in your entire program:
| ?- [myprog].
When simply interacting with the top-level this distinction between queries and commands is not
normally very important. At top-level you should just type queries normally. In a le, queries are
in fact treated as commands, i.e. if you wish to execute some goals then the directive in the le
must be preceded by `:-' or `?-', otherwise it would be treated as a clause.
1.5 Syntax Errors
Syntax errors are detected during reading. Each clause, directive or in general any term read in
by the built-in predicate read/1 that fails to comply with syntax requirements is displayed on the
standard error stream as soon as it is read, along with its position in the input stream and a mark
indicating the point in the string of symbols where the parser has failed to continue analysis, e.g.:
| member(X, X$L).
{SYNTAX ERROR: in line 5 (within 5-6)}
** , or ) expected in arguments **
member ( X , X
** here **
$ L ) .
if $ has not been declared as an inx operator.
Note that any comments in the faulty line are not displayed with the error message. If you are in
doubt about which clause was wrong you can use the listing/1 predicate to list all the clauses
which were successfully read-in, e.g.
Chapter 1: How to Run Prolog
15
| ?- listing(member/2).
Note: The built in predicates read/(1-2) normaly raise an exception on syntax errors (see Section 7.5 [Exception], page 101). The behavior is controlled by the ag syntax_errors (see prolog_
flag/3).
1.6 Undened Predicates
There is a dierence between predicates that have no denition and predicates that have no clauses.
The latter case is meaningful e.g. for dynamic predicates (see Section 5.2 [Declarations], page 53)
that clauses are being added to or removed from. There are good reasons for treating calls to
undened predicates as errors, as such calls easily arise from typing errors.
The system can optionally catch calls to predicates that have no denition. First the user dened
predicate user:unknown_predicate_handler/3 (see Section 7.5 [Exception], page 101) is called.
If undened or if the call fails the action is governed by the state of the unknown/2 ag which can
be:
trace
error
fail
which causes calls to undened predicates to be reported and the debugger to be entered
at the earliest opportunity.
which causes calls to such predicates to raise an exception (the default state). See
Section 7.5 [Exception], page 101.
which causes calls to such predicates to fail.
Calls to predicates that have no clauses are not caught.
The built-in predicate unknown(?OldState, ?NewState ) unies OldState with the current state and
sets the state to NewState. The built-in predicate debugging/0 prints the value of this state along
with its other information. This state is also controlled by the ag unknown (see prolog_flag/3).
1.7 Program Execution And Interruption
Execution of a program is started by giving the system a directive which contains a call to one of
the program's predicates.
Only when execution of one directive is complete does the system become ready for another directive. However, one may interrupt the normal execution of a directive by typing ^C. This ^C
interruption has the eect of suspending the execution, and the following message is displayed:
Prolog interruption (h or ? for help) ?
At this point, the Development System accepts one-letter commands corresponding to certain
actions. To execute an action simply type the corresponding character (lower or upper case)
followed by RET. The available commands in both Development Systems are:
16
SICStus Prolog
e
aborts the current computation.
continues the execution.
exits from SICStus Prolog, closing all les.
h
?
lists available commands.
a
c
The following commands are also available in the sequential Development System only:
b
invokes a recursive top-level.
d
z
t
switch on the debugger. See Chapter 6 [Debug Intro], page 59.
If the standard input stream is not connected to the terminal, e.g. by redirecting standard input
to a le or a pipe, the above ^C interrupt options are not available. Instead, typing ^C causes
SICStus Prolog to exit, and no terminal prompts are printed.
1.8 Exiting From The Top-Level
To exit from the top-level and return to the shell, either type ^D at the top-level, or call the built-in
predicate halt/0, or use the e (exit) command following a ^C interruption.
1.9 Nested Executions|Break and Abort
The Prolog system provides a way to suspend the execution of your program and to enter a new
incarnation of the top level where you can issue directives to solve goals etc. This is achieved by
issuing the directive (see Section 1.7 [Execution], page 15):
| ?- break.
This invokes a recursive top-level, indicated by the message:
{ Break level 1 }
You can now type queries just as if you were at top-level.
If another call of break/0 is encountered, it moves up to level 2, and so on. To close the break and
resume the execution which was suspended, type ^D. The debugger state and current input and
output streams will be restored, and execution will be resumed at the predicate call where it had
been suspended after printing the message:
Chapter 1: How to Run Prolog
17
{ End break }
Alternatively, the suspended execution can be aborted by calling the built-in predicate abort/0.
A suspended execution can be aborted by issuing the directive:
| ?- abort.
within a break. In this case no ^D is needed to close the break; all break levels are discarded and
the system returns right back to top-level. I/O streams remain open, but the debugger is switched
o. abort/0 may also be called from within a program.
1.10 Saving and Restoring Program States
Once a program has been read, the system will have available all the information necessary for its
execution. This information is called a program state.
The state of a program may be saved on disk for future execution. The state consists of all predicates
and modules except built-in predicates and clauses of volatile predicates, the current operator
declarations, the values of all writable Prolog ags except debugging, source_info, and the user_
* stream aliases (see Section 7.6 [State Info], page 104), any blackboard data (see Section 7.11
[Blackboard Primitives], page 116), internal database data (see Section 7.10 [Database], page 115),
and proling data (see Section 7.15 [Proling], page 121), but no information for source-linked
debugging.
To save a program into a le File, type the following directive. On UNIX platforms, the le becomes
executable:
| ?- save_program(File ).
You can also specify a goal to be run when a saved program is restored. This is done by:
| ?- save_program(File, start).
where start/0 is the predicate to be called.
Once a program has been saved into a le File, the following directive will restore the system to
the saved state:
| ?- restore(File ).
If a saved state has been moved or copied to another machine, the path names of foreign resources
and other les needed upon restore are typically dierent at restore time from their save time
values. To solve this problem, certain atoms will be relocated during restore as follows:
Atoms that had `$SP_PATH/library' (the name of the directory containing the Prolog Library)
as prex at save time will have that prex replaced by the corresponding restore time value.
Atoms that had the name of the directory containing File as prex at save time will have that
prex replaced by the corresponding restore time value.
18
SICStus Prolog
The purpose of this procedure is to be able to build and deploy an application consisting of a saved
state and other les as a directory tree with the saved state at the root: as long as the other les
maintain their relative position in the deployed copy, they can still be found upon restore.
Note: Foreign resources, see Section 8.2 [Calling C], page 128, are unloaded by save_
program/(1,2). The names and paths of the resources, typically `$SP_PATH/library' relative,
are however included in the saved state. After the save, and after restoring a saved state, this
information is used to reload the foreign resources again. The state of the foreign resource in terms
of global C variables and allocated memory is thus not preserved. Foreign resources may dene init
and deinit functions to take special action upon loading and unloading, see Section 8.2.6 [Init and
Deinit Functions], page 134.
In the Muse Development System, the above predicates may only be called when the system has
been adjusted to one worker; see Section 3.3.1 [Muse Exe Model], page 43.
1.11 Emacs Interface
This section explains how to use the GNU Emacs interface for SICStus Prolog, and how to customize
your GNU Emacs environment for it.
The advantages of using SICStus in the Emacs environment are source-linked debugging, auto
indentation, syntax highlighting, help on predened predicates (requires the SICStus info les to
be installed), loading code from inside Emacs, auto-ll mode, and more.
The mode was developed for GNU Emacs 19.34 but it works well for versions 19.31 through 20.2.
Earlier versions may not be able to provide syntax highlighting. The mode has also been reported
to work with XEmacs (version 19 and 20) but it will probably not work with GNU Emacs 18 or
earlier.
The Emacs interface is not part of SICStus Prolog proper, but is included in the distribution for
convenience. It was written by Emil Astrom and Milan Zamazal, based on an earlier version of the
mode written by Masanobu Umeda. Contributions has also been made by Johan Andersson, Peter
Olin, Mats Carlsson, Johan Bevemyr, Stefan Andersson, and Per Danielsson (all at SICS), and
Henrik Bakman at Uppsala University, Sweden. Some ideas and also a few lines of code have been
borrowed (with permission) from Oz.el by Ralf Scheidhauer and Michael Mehl, the Emacs major
mode for the Oz programming language. More ideas and code have been taken from the SICStus
debugger mode by Per Mildner.
1.11.1 Customizing Emacs
Assuming the GNU Emacs interface for SICStus Prolog has been installed, inserting the following
lines in your `~/.emacs' (or `_emacs' in case of Windows/DOS) will make Emacs use this mode
automatically when editing les with a `.pl' extension:
Chapter 1: How to Run Prolog
19
(setq load-path (cons "/usr/local/lib/sicstus37" load-path))
(autoload 'run-prolog "prolog" "Start a Prolog sub-process." t)
(autoload 'prolog-mode "prolog" "Major mode for editing Prolog programs." t)
(setq prolog-use-sicstus-sd t)
(setq auto-mode-alist (cons '("\\.pl$" . prolog-mode) auto-mode-alist))
where the path in the rst line is the le system path to `prolog.el' (the Emacs interface) and
`pltrace.el' (the process output lter for source-linked debugging). For example, `~/site-lisp'
means that the le is in the user's home directory, under site-lisp. MSDOS paths can be written
like `C:/Program Files/emacs-19.34/site-lisp'.
The last line above makes sure that les ending with `.pl' are assumed to be Prolog les and not
Perl, which is the default Emacs setting. If this is undesirable, remove that line. It is then necessary
for the user to manually switch to prolog mode by typing M-x prolog-mode after opening a prolog
le.
If the shell command sicstus is not available in the default path, then it is necessary to set the
value of the environment variable EPROLOG to a shell command to invoke SICStus Prolog. This is
an example for C Shell:
setenv EPROLOG /usr/local/bin/sicstus
1.11.2 Basic Conguration
If the following lines are not present in `~/.emacs', we suggest they are added, so that the font-lock
mode (syntax coloring support) is enabled for all major modes in Emacs that support it.
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)
If one wants to add font-locking only to the prolog mode, the two lines above could be replaced by:
(add-hook 'prolog-mode-hook 'turn-on-font-lock)
If the background color of Emacs is dark and it is dicult to read the code because of the chosen
colors, then it might be useful to add
(setq font-lock-background-mode 'dark)
1.11.3 Usage
A prolog process can be started by choosing Run Prolog from the Prolog menu, by typing C-c
RET, or by typing M-x run-prolog. It is however not strictly necessary to start a prolog process
manually since it is automatically done when consulting or compiling, if needed. The process can
be restarted (i.e. the old one is killed and a new one is created) by typing C-u C-c RET.
Programs are run and debugged in the normal way, with terminal I/O via the *prolog* buer.
The most common debugging predicates are available from the menu or via key-bindings.
20
SICStus Prolog
A particularly useful feature under the Emacs interface is source-linked debugging. This is enabled
or disabled using the Prolog/Source level debugging menu entry. It can also be enabled by
setting the Emacs variable prolog-use-sicstus-sd to t in `~/.emacs'. Both these methods set
the Prolog ag source_info to on. The ag should be on while loading the code to be debugged
and while debugging. If so, the debugger will display the source code location of the current goal
when it prompts for a debugger command, by overlaying the beginning of the current line of code
with an arrow. If source_info was off when the code was loaded, or if it was asserted or loaded
from user, the current goal will still be shown but out of context.
Note that if the code has been modied since it was last loaded, Prolog's line number information
may be invalid. If this happens, just reload the relevant buer.
Consultation and compilation is either done via the menu or with the following key-bindings:
C-c C-f
C-c C-b
C-c C-r
C-c C-p
C-c C-c f
C-c C-c b
C-c C-c r
C-c C-c p
Consult le.
Consult buer.
Consult region.
Consult predicate.
Compile le.
Compile buer.
Compile region.
Compile predicate.
The boundaries used when consulting and compiling predicates are the rst and last clauses of the
predicate the cursor is currently in.
Other useful key-bindings are:
M-n
M-p
M-a
M-e
M-C-c
M-C-a
M-C-e
M-C-h
M-{
M-}
M-h
Go to the next clause.
Go to the previous clause.
Go to beginning of clause.
Go to end of clause.
Mark clause.
Go to beginning of predicate.
Go to end of predicate.
Mark predicate.
Go to the previous paragraph (i.e. empty line).
Go to the next paragraph (i.e. empty line).
Mark paragraph.
Chapter 1: How to Run Prolog
M-C-n
M-C-p
M-;
21
Go to matching right parenthesis.
Go to matching left parenthesis.
Creates a comment at comment-column. This comment will always stay at this position
when the line is indented, regardless of changes in the text earlier on the line, provided
that prolog-align-comments-flag is set to t.
C-c C-t, C-u C-c C-t
Enable and disable tracing, respectively.
C-c C-d, C-u C-c C-d
Enable and disable debugging, respectively.
C-c C-z, C-u C-c C-z
C-c C-s
C-c C-n
C-c C-v a
C-c ?
Enable and disable zipping, respectively.
Insert the PredSpec of the current predicate into the code.
Insert the name of the current predicate into the code. This can be useful when writing
recursive predicates or predicates with several clauses. See also the prolog-electricdot-flag variable below.
Convert all variables in a region to anonymous variables. This can also be done using the Prolog/Transform/All variables to '_' menu entry. See also the prologelectric-underscore-flag Emacs variable.
Help on predicate (requires the SICStus info les to be installed), and the Emacs
variable prolog-info-predicate-index to be set to the predicate index of the info
les (defaults to "(sicstus)Predicate Index").
1.11.4 Mode Line
If working with an application split into several modules, it is recommended to let the source les
begin with a \mode line":
%%% -*- Module: ModuleName; -*-
The Emacs interface will look for the mode line and notify the SICStus Prolog module system that
the predicates being incrementally reconsulted or recompiled belong to the module ModuleName.
If SICStus Prolog recognizes the le as one being loaded before, it will remember what module it
belongs to. If the mode line is missing, and the le has not been loaded before, the predicates will
go into the type-in module. Even if the le has been loaded earlier, its lename may have a slightly
dierent appearance to Prolog via the Emacs interface, so it is safest to always include the mode
line. A mode line can be inserted by choosing Insert/Module modeline in the Prolog menu.
22
SICStus Prolog
1.11.5 Conguration
The behavior of the Emacs interface can be controlled by a set of user-congurable settings. Some
of these can be changed on the y, while some require Emacs to be restarted. To set a variable on
the y, type M-x set-variable RET VariableName RET Value RET. Note that variable names can
be completed by typing a few characters and then pressing TAB.
To set a variable so that the setting is used every time Emacs is started, add lines of the following
format to `~/.emacs':
(setq VariableName Value)
The available settings are:
prolog-system
The Prolog system to use. Available settings are
Defaults to 'sicstus.
'mercury, 'sicstus,
and
'swi.
prolog-system-version
The version of SICStus that is used. Defaults to '((sicstus (3 . 7))). Note that the
spaces are signicant!
prolog-use-sicstus-sd
Set to t to enable
the source-linked debugging extensions by default. Defaults to nil.
The debugging can be enabled via the Prolog menu even if this variable is nil. Note
that the source-linked debugging only works if prolog-system-version is set correctly.
pltrace-port-arrow-assoc
Only relevant for source-linked debugging, this controls how the various ports of invocation boxes (see Section 6.1 [Procedure Box], page 59) map to arrows that point into
the current line of code in source code buers. Initialized as:
'(("call" . ">>>") ("exit" . "+++") ("ndexit" . "?++") ("redo" . "<<<")
("fail" . "---") ("exception" . "==>"))
where ndexit is the non-deterministic variant of the Exit port.
prolog-indent-width
How many positions to indent the body of a clause. Defaults to tab-width, normally
8.
prolog-paren-indent
The number of positions to indent code inside grouping parentheses. Defaults to 4,
which gives the following indentation.
p :(
;
).
q1
q2,
q3
Note that the spaces between the parentheses and the code are automatically inserted
when TAB is pressed at those positions.
Chapter 1: How to Run Prolog
23
prolog-align-comments-flag
Set to nil to prevent
single %-comments to be automatically aligned. Defaults to t.
Note that comments with one % are indented to comment-column, comments with two
% to the code level, and that comments with three % are never changed when indenting.
prolog-indent-mline-comments-flag
Set to nil to prevent indentation
of text inside /* ... */ comments. Defaults t.
prolog-object-end-to-0-flag
Set to nil to indent the closing
Defaults to t.
}
of an object denition to
prolog-indent-width.
prolog-keywords
This is a list with keywords that are highlighted in a special color when used as commands (i.e. as :- keyword). Defaults to
'((sicstus ("block" "dynamic" "mode" "module" "multifile"
"meta_predicate" "parallel" "public" "sequential" "volatile")))
prolog-electric-newline-flag
Set to nil to prevent Emacs
RET. Defaults to t.
from automatically indenting the next line when pressing
prolog-hungry-delete-key-flag
Set to t to enable deletion
of all white space before the cursor when pressing the delete
key (unless inside a comment, string, or quoted atom). Defaults to nil.
prolog-electric-dot-flag
Set to t to enable
the electric dot function. If enabled, pressing . at the end of a
non-empty line inserts a dot and a newline. When pressed at the beginning of a line, a
new head of the last predicate is inserted. When pressed at the end of a line with only
whitespace, a recursive call to the current predicate is inserted. The function respects
the arity of the predicate and inserts parentheses and the correct number of commas
for separation of the arguments. Defaults to nil.
prolog-electric-underscore-flag
Set to t to enable the electric underscore function. When enabled, pressing underscore
(_) when the cursor is on a variable, replaces the variable with the anynomous variable.
Defaults to nil.
prolog-old-sicstus-keys-flag
Set to t to enable the
key-bindings of the old Emacs interface. These bindings are not
used by default since they violate GNU Emacs recommendations. Defaults to nil.
prolog-use-prolog-tokenizer-flag
Set to nil to use built-in functions of Emacs for parsing the source code when indenting.
This is faster than the default but does not handle some of the syntax peculiarities of
Prolog. Defaults to t.
prolog-parse-mode
What position the parsing is done from when indenting code. Two possible settings:
'beg-of-line and 'beg-of-clause. The rst is faster but may result in erroneous
indentation in /* ... */ comments. The default is 'beg-of-line.
24
SICStus Prolog
prolog-imenu-flag
Set to t to
enable a new Predicate menu which contains all predicates of the current
le. Choosing an entry in the menu moves the cursor to the start of that predicate.
Defaults to nil.
prolog-info-predicate-index
The info node for the SICStus predicate index. This is important if the online help
function is to be used (by pressing C-c ?, or choosing the Prolog/Help on predicate
menu entry). The default setting is "(sicstus)Predicate Index".
prolog-underscore-wordchar-flag
Set to nil to not make underscore (_)
a word-constituent character. Defaults to t.
1.11.6 Tips
Some general tips and tricks for using the SICStus mode and Emacs in general are given here.
1.11.6.1 Font-locking
When editing large les, it might happen that font-locking is not done because the le is too large.
Typing M-x lazy-lock-mode results in only the visible parts of the buer being highlighted, which
is much faster.
If the font-locking seems to be incorrect then choose Fontify Buffer from the Prolog menu.
1.11.6.2 Auto-ll mode
Auto-ll mode is enabled by typing M-x auto-fill-mode. This enables automatic line breaking
with some features. For example, the following multiline comment was created by typing M-;
followed by the text. The second line was indented and a % was added automatically.
dynamics([]).
% A list of pit furnace
% dynamic instances
1.11.6.3 Speed
There are several things to do if the speed of the Emacs environment is a problem:
First of all, make sure that `prolog.el' and `pltrace.el' are compiled, i.e. that there is a
`prolog.elc' and a `pltrace.elc' le at the same location as the original les. To do the
compilation, start Emacs and type M-x byte-compile-file RET Path RET, where Path is the
path to the `*.el' le. Do not be alarmed if there are a few warning messages as this is
normal. If all went well, there should now be a compiled le which is used the next time
Emacs is started.
Chapter 1: How to Run Prolog
25
The next thing to try is changing the setting of prolog-use-prolog-tokenizer-flag to
nil. This means that Emacs uses built-in functions for some of the source code parsing, thus
speeding up indentation. The problem is that it does not handle all peculiarities of the Prolog
syntax, so this is a trade-o between correctness and speed.
The setting of the prolog-parse-mode variable also aects the speed, 'beg-of-line being
faster than 'beg-of-clause.
1.11.6.4 Changing Colors
The prolog mode uses the default Emacs colors for font-locking as far as possible. The only custom
settings are in the prolog process buer. The default settings of the colors may not agree with your
preferences, so here is how to change them.
First of all, list all available faces (a face is a combined setting of foreground and background colors,
font, boldness, etc.) by typing M-x list-faces-display.
There are several functions that change the appearance of a face, the ones you will most likely need
are:
set-face-foreground
set-face-background
set-face-underline-p
make-face-bold
make-face-bold-italic
make-face-italic
make-face-unbold
make-face-unitalic
These can be tested interactively by typing M-x function-name. You will then be asked for the name
of the face to change and a value. If the buers are not updated according to the new settings,
then refontify the buer using the Fontify Buffer menu entry in the Prolog menu.
Colors are specied by a name or by RGB values. Available color names can be listed with M-x
list-colors-display.
To store the settings of the faces, a few lines must be added to `~/.emacs'. For example:
;; Customize font-lock faces
(add-hook 'font-lock-mode-hook
'(lambda ()
(set-face-foreground font-lock-variable-name-face "#00a000")
(make-face-bold font-lock-keyword-face)
(set-face-foreground font-lock-reference-face "Blue")
))
26
SICStus Prolog
Chapter 2: The Prolog Language
27
2 The Prolog Language
This chapter provides a brief introduction to the syntax and semantics of a certain subset of logic
(denite clauses, also known as Horn clauses), and indicates how this subset forms the basis of
Prolog.
2.1 Syntax, Terminology and Informal Semantics
2.1.1 Terms
The data objects of the language are called terms. A term is either a constant, a variable or a
compound term.
2.1.1.1 Integers
The constants include integers such as
0
1
999
-512
Besides the usual decimal, or base 10, notation, integers may also be written in any base from 2
to 36, of which base 2 (binary), 8 (octal), and 16 (hex) are probably the most useful. Letters A
through ! Z (upper or lower case) are used for bases greater than 10. E.g.
15
2'1111
8'17
all represent the integer fteen.
There is also a special notation for character constants. E.g.
0'A
0'\x41
0'\101
are all equivalent to 65 (the character code for A). `0'' followed by any character except \ (backslash)
is thus read as an integer. If `0'' is followed by \, the \ denotes the start of an escape sequence
with special meaning (see [Escape Sequences], page 401).
2.1.1.2 Floats
Constants also include oats such as
1.0
-3.141
4.5E7
-0.12e+8
12.0e-9
Note that there must be a decimal point in oats written with an exponent, and that there must
be at least one digit before and after the decimal point.
28
SICStus Prolog
2.1.1.3 Atoms
Constants also include atoms such as
a
void
=
:=
'Algol-68'
[]
Constants are denite elementary objects, and correspond to proper nouns in natural language.
For reference purposes, here is a list of the possible forms which an atom may take:
1. Any sequence of alphanumeric characters (including _), starting with a lower case letter.
2. Any sequence from the following set of characters:
+-*/\^<>=`~:[email protected]#$&
This set can in fact be larger; see [Token String], page 398 for a precise denition.
3. Any sequence of characters delimited by single quotes. If the single quote character is included
in the sequence it must be written twice, e.g. 'can''t'. Backslashes in the sequence denote
escape sequences (see [Escape Sequences], page 401).
4. Any of: ! ; [] {}
Note that the bracket pairs are special: [] and {} are atoms but [, ], {, and } are not.
However, when they are used as functors (see below) the form {X } is allowed as an alternative
to {}(X ). The form [X ] is the normal notation for lists, as an alternative to .(X,[]).
2.1.1.4 Variables
Variables may be written as any sequence of alphanumeric characters (including _) starting with
either a capital letter or _; e.g.
X
Value
A
A1
_3
_RESULT
If a variable is only referred to once in a clause, it does not need to be named and may be written
as an anonymous variable, indicated by the underline character _. A clause may contain several
anonymous variables; they are all read and treated as distinct variables.
A variable should be thought of as standing for some denite but unidentied object. This is
analogous to the use of a pronoun in natural language. Note that a variable is not simply a
writable storage location as in most programming languages; rather it is a local name for some
data object, cf. the variable of pure LISP and identity declarations in Algol68.
2.1.1.5 Compound Terms
The structured data objects of the language are the compound terms. A compound term comprises
a functor (called the principal functor of the term) and a sequence of one or more terms called
arguments. A functor is characterized by its name, which is an atom, and its arity or number
of arguments. For example the compound term whose functor is named point of arity 3, with
arguments X, Y and Z, is written
Chapter 2: The Prolog Language
29
point(X, Y, Z)
Note that an atom is considered to be a functor of arity 0.
Functors are generally analogous to common nouns in natural language. One may think of a functor
as a record type and the arguments of a compound term as the elds of a record. Compound terms
are usefully pictured as trees. For example, the term
s(np(john),vp(v(likes),np(mary)))
would be pictured as the structure
np
|
john
/
s
\
vp
/ \
v
np
|
|
likes mary
Sometimes it is convenient to write certain functors as operators|2-ary functors may be declared
as inx operators and 1-ary functors as prex or postx operators. Thus it is possible to write, e.g.
X+Y
(P;Q)
X<Y
+X
P;
<(X,Y)
+(X)
;(P)
as optional alternatives to
+(X,Y)
;(P,Q)
The use of operators is described fully below (see Section 2.6 [Operators], page 37).
Lists form an important class of data structures in Prolog. They are essentially the same as the
lists of LISP: a list either is the atom [] representing the empty list, or is a compound term with
functor . and two arguments which are respectively the head and tail of the list. Thus a list of the
rst three natural numbers is the structure
.
/ \
1
.
/ \
2
.
/ \
3
[]
which could be written, using the standard syntax, as
.(1,.(2,.(3,[])))
but which is normally written, in a special list notation, as
30
SICStus Prolog
[1,2,3]
The special list notation in the case when the tail of a list is a variable is exemplied by
[X|L]
[a,b|L]
representing
X
.
/ \
L
a
.
/ \
b
.
/ \
L
respectively.
Note that this notation does not add any new power to the language; it simply makes it more
readable. e.g. the above examples could equally be written
.(X,L)
.(a,.(b,L))
For convenience, a further notational variant is allowed for lists of integers which correspond to
character codes. Lists written in this notation are called strings. E.g.
"SICStus"
which represents exactly the same list as
[83,73,67,83,116,117,115]
As for quoted atoms, if a double quote character is included in the sequence it must be written
twice, e.g. 'can''t'. Backslashes in the sequence denote escape sequences (see [Escape Sequences],
page 401).
2.1.2 Programs
A fundamental unit of a logic program is the goal or procedure call. e.g.
gives(tom, apple, teacher)
reverse([1,2,3], L)
X<Y
A goal is merely a special kind of term, distinguished only by the context in which it appears in the
program. The (principal) functor of a goal identies what predicate the goal is for. It corresponds
roughly to a verb in natural language, or to a procedure name in a conventional programming
language.
A logic program consists simply of a sequence of statements called sentences, which are analogous
to sentences of natural language. A sentence comprises a head and a body. The head either consists
of a single goal or is empty. The body consists of a sequence of zero or more goals (i.e. it too may
be empty). If the head is not empty, the sentence is called a clause.
Chapter 2: The Prolog Language
31
If the body of a clause is empty, the clause is called a unit clause, and is written in the form
P.
where P is the head goal. We interpret this declaratively as
Goals matching P are true.
and procedurally as
Goals matching P are satised.
If the body of a clause is non-empty, the clause is called a non-unit clause, and is written in the
form
P :- Q, R, S.
where P is the head goal and Q, R and S are the goals which make up the body. We can read such
a clause either declaratively as
P is true if Q and R and S are true.
or procedurally as
To satisfy goal P, satisfy goals Q, R and S.
A sentence with an empty head is called a directive (see Section 1.4 [Directives], page 12), of which
the most important kind is called a query and is written in the form
?- P, Q.
where P and Q are the goals of the body. Such a query is read declaratively as
Are P and Q true?
and procedurally as
Satisfy goals P and Q.
Sentences generally contain variables. Note that variables in dierent sentences are completely independent, even if they have the same name|i.e. the lexical scope of a variable is limited to a single
sentence. Each distinct variable in a sentence should be interpreted as standing for an arbitrary
entity, or value. To illustrate this, here are some examples of sentences containing variables, with
possible declarative and procedural readings:
1.
employed(X ) :- employs(Y,X ).
\Any X is employed if any Y employs X."
\To nd whether a person X is employed, nd whether any Y employs X."
32
2.
SICStus Prolog
derivative(X,X,1).
\For any X, the derivative of X with respect to X is 1."
\The goal of nding a derivative for the expression X with respect to X itself is satised by
the result 1."
3. ?- ungulate(X ), aquatic(X ).
\Is it true, for any X, that X is an ungulate and X is aquatic?"
\Find an X which is both an ungulate and aquatic."
In any program, the predicate for a particular (principal) functor is the sequence of clauses in the
program whose head goals have that principal functor. For example, the predicate for a 3-ary
functor concatenate/3 might well consist of the two clauses
concatenate([], L, L).
concatenate([X|L1], L2, [X|L3]) :- concatenate(L1, L2, L3).
where concatenate(L1,L2,L3 ) means \the list L1 concatenated with the list L2 is the list L3".
Note that for predicates with clauses corresponding to a base case and a recursive case, the preferred
style is to write the base case clause rst.
In Prolog, several predicates may have the same name but dierent arities. Therefore, when it is
important to specify a predicate unambiguously, the form name /arity is used; e.g. concatenate/3.
Certain predicates are predened by built-in predicates supplied by the Prolog system. Such predicates are called built-in predicates.
As we have seen, the goals in the body of a sentence are linked by the operator `,' which can
be interpreted as conjunction (\and"). It is sometimes convenient to use an additional operator
`;', standing for disjunction (\or"). (The precedence of `;' is such that it dominates `,' but is
dominated by `:-'.) An example is the clause
grandfather(X, Z) :(mother(X, Y); father(X, Y)),
father(Y, Z).
which can be read as
For any X, Y and Z, X has Z as a grandfather if either the mother of X is Y or the
father of X is Y, and the father of Y is Z.
Such uses of disjunction can always be eliminated by dening an extra predicate|for instance the
previous example is equivalent to
grandfather(X,Z) :- parent(X,Y), father(Y,Z).
parent(X,Y) :- mother(X,Y).
parent(X,Y) :- father(X,Y).
|and so disjunction will not be mentioned further in the following, more formal, description of the
semantics of clauses.
Chapter 2: The Prolog Language
33
The token `|', when used outside a list, is an alias for `;'. The aliasing is performed when terms
are read in, so that
a :- b | c.
is read as if it were
a :- b ; c.
Note the double use of the `.' character. On the one hand it is used as a sentence terminator, while
on the other it may be used in a string of symbols which make up an atom (e.g. the list functor
./2). The rule used to disambiguate terms is that a `.' followed by layout-text is regarded as a
sentence terminator (see [Token String], page 398).
2.2 Declarative Semantics
The semantics of denite clauses should be fairly clear from the informal interpretations already
given. However it is useful to have a precise denition. The declarative semantics of denite clauses
tells us which goals can be considered true according to a given program, and is dened recursively
as follows.
A goal is true if it is the head of some clause instance and each of the goals (if any)
in the body of that clause instance is true, where an instance of a clause (or term) is
obtained by substituting, for each of zero or more of its variables, a new term for all
occurrences of the variable.
For example, if a program contains the preceding procedure for concatenate/3, then the declarative
semantics tells us that
?- concatenate([a], [b], [a,b]).
is true, because this goal is the head of a certain instance of the rst clause for concatenate/3,
namely,
concatenate([a], [b], [a,b]) :- concatenate([], [b], [b]).
and we know that the only goal in the body of this clause instance is true, since it is an instance
of the unit clause which is the second clause for concatenate/3.
34
SICStus Prolog
2.3 Procedural Semantics
Note that the declarative semantics makes no reference to the sequencing of goals within the body
of a clause, nor to the sequencing of clauses within a program. This sequencing information is,
however, very relevant for the procedural semantics which Prolog gives to denite clauses. The
procedural semantics denes exactly how the Prolog system will execute a goal, and the sequencing
information is the means by which the Prolog programmer directs the system to execute the program
in a sensible way. The eect of executing a goal is to enumerate, one by one, its true instances.
Here then is an informal denition of the procedural semantics. We rst illustrate the semantics
by the simple query
?- concatenate(X, Y, [a,b]).
We nd that it matches the head of the rst clause for concatenate/3, with X instantiated to
[a|X1 ]. The new variable X1 is constrained by the new query produced, which contains a single
recursive procedure call:
?- concatenate(X1, Y, [b]).
Again this goal matches the rst clause, instantiating X1 to [b|X2 ], and yielding the new query:
?- concatenate(X2, Y, [])
Now the single goal will only match the second clause, instantiating both X2 and Y to []. Since
there are no further goals to be executed, we have a solution
X = [a,b]
Y = []
i.e. a true instance of the original goal is
concatenate([a,b], [], [a,b])
If this solution is rejected, backtracking will generate the further solutions
X = [a]
Y = [b]
X = []
Y = [a,b]
in that order, by re-matching, against the second clause for concatenate, goals already solved once
using the rst clause.
Thus, in the procedural semantics, the set of clauses
H :- B1, ..., Bm.
H' :- B1', ..., Bm'.
...
are regarded as a procedure denition for some predicate H, and in a query
Chapter 2: The Prolog Language
?-
G1,
...,
35
Gn.
each Gi is regarded as a procedure call. To execute a query, the system selects by its computation
rule a goal, Gj say, and searches by its search rule a clause whose head matches Gj. Matching
is done by the unication algorithm (see [Robinson 65] which computes the most general unier,
mgu, of Gj and H. The mgu is unique if it exists. If a match is found, the current query is reduced
to a new query
?- (G1, ..., Gj-1, B1, ..., Bm, Gj+1, ..., Gn)mgu.
and a new cycle is started. The execution terminates when the empty query has been produced.
If there is no matching head for a goal, the execution backtracks to the most recent successful
match in an attempt to nd an alternative match. If such a match is found, an alternative new
query is produced, and a new cycle is started.
In SICStus Prolog, as in other Prolog systems, the search rule is simple: \search forward from the
beginning of the program".
The computation rule in traditional Prolog systems is also simple: \pick the leftmost goal of the
current query". However, SICStus Prolog and other modern implementations have a somewhat
more complex computation rule \pick the leftmost unblocked goal of the current query".
A goal can be blocked on one ore more uninstantiated variables, and a variable may block several
goals. Thus binding a variable can cause blocked goals to become unblocked, and backtracking can
cause currently unblocked goals to become blocked again. Moreover, if the current query is
?- G1, ..., Gj-1, Gj, Gj+1, ..., Gn.
where Gj is the rst unblocked goal, and matching Gj against a clause head causes several blocked
goals in G1, ..., Gj-1 to become unblocked, then these goals may become reordered. The internal
order of any two goals that were blocked on the same variable is retained, however.
Another consequence is that a query may be derived consisting entirely of blocked goals. Such a
query is said to have oundered. The top-level checks for this condition. If detected, the outstanding
blocked subgoals are printed on the standard error stream along with the answer substitution, to
notify the user that the answer (s)he has got is really a speculative one, since it is only valid if the
blocked goals can be satised.
A goal is blocked if certain arguments are uninstantiated and its predicate denition is annotated
with a matching block declaration (see Section 5.2.4 [Block Declarations], page 55). Goals of certain
built-in may also be blocked if their arguments are not suciently instantiated.
When this mechanism is used, the control structure resembles that of coroutines, suspending and
resuming dierent threads of control. When a computation has left blocked goals behind, the situation is analogous to spawning a new suspended thread. When a blocked goal becomes unblocked,
the situation is analogous to temporarily suspending the current thread and resuming the thread
to which the blocked goal belongs.
36
SICStus Prolog
2.4 Occurs-Check
It is possible, and sometimes useful, to write programs which unify a variable to a term in which
that variable occurs, thus creating a cyclic term. The usual mathematical theory behind Logic
Programming forbids the creation of cyclic terms, dictating that an occurs-check should be done
each time a variable is unied with a term. Unfortunately, an occurs-check would be so expensive
as to render Prolog impractical as a programming language. Thus cyclic terms may be created and
may cause loops trying to print them.
SICStus Prolog mitigates the problem by its ability to unify, compare (see Section 7.3 [Term
Compare], page 97), assert, and copy cyclic terms without looping. The write_term/2 built-in
predicate can optionally handle cyclic terms; see Section 7.1.3 [Term I/O], page 80. Unication with
occurs-check and predicates testing (a)cyclicity are available in a library package; see Chapter 17
[Term Utilities], page 205. Other predicates usually do not handle cyclic terms well.
2.5 The Cut Symbol
Besides the sequencing of goals and clauses, Prolog provides one other very important facility for
specifying control information. This is the cut symbol, written !. It is inserted in the program just
like a goal, but is not to be regarded as part of the logic of the program and should be ignored as
far as the declarative semantics is concerned.
The eect of the cut symbol is as follows. When rst encountered as a goal, cut succeeds immediately. If backtracking should later return to the cut, the eect is to fail the parent goal, i.e. that goal
which matched the head of the clause containing the cut, and caused the clause to be activated. In
other words, the cut operation commits the system to all choices made since the parent goal was
invoked, and causes other alternatives to be discarded. The goals thus rendered determinate are
the parent goal itself, any goals occurring before the cut in the clause containing the cut, and any
subgoals which were executed during the execution of those preceding goals.
For example:
member(X, [X|_]).
member(X, [_|L]) :- member(X, L).
This predicate can be used to test whether a given term is in a list. E.g.
| ?- member(b, [a,b,c]).
returns the answer `yes'. The predicate can also be used to extract elements from a list, as in
| ?- member(X, [d,e,f]).
With backtracking this will successively return each element of the list. Now suppose that the rst
clause had been written instead:
Chapter 2: The Prolog Language
37
member(X, [X|_]) :- !.
In this case, the above call would extract only the rst element of the list (d). On backtracking,
the cut would immediately fail the whole predicate.
x :- p, !, q.
x :- r.
This is equivalent to
x := if p then q else r;
in an Algol-like language.
It should be noticed that a cut discards all the alternatives since the parent goal, even when the
cut appears within a disjunction. This means that the normal method for eliminating a disjunction
by dening an extra predicate cannot be applied to a disjunction containing a cut.
A proper use of the cut is usually a major diculty for new Prolog programmers. The usual
mistakes are to over-use cut, and to let cuts destroy the logic. A cut that doesn't destroy the logic
is called a green cut; a cut that does is called a red cut. We would like to advise all users to follow
these general rules. Also see Chapter 10 [Example Intro], page 177.
Write each clause as a self-contained logic rule which just denes the truth of goals which
match its head. Then add cuts to remove any fruitless alternative computation paths that
may tie up memory.
Cuts are usually placed right after the head, sometimes preceded by simple tests.
Cuts are hardly ever needed in the last clause of a predicate.
2.6 Operators
Operators in Prolog are simply a notational convenience. For example, the expression 2+1 could
also be written +(2,1). This expression represents the data structure
/
2
+
\
1
and not the number 3. The addition would only be performed if the structure were passed as an
argument to an appropriate predicate such as is/2 (see Section 7.2 [Arithmetic], page 94).
The Prolog syntax caters for operators of three main kinds|inx, prex and postx. An inx
operator appears between its two arguments, while a prex operator precedes its single argument
and a postx operator is written after its single argument.
Each operator has a precedence, which is a number from 1 to 1200. The precedence is used to
disambiguate expressions where the structure of the term denoted is not made explicit through the
38
SICStus Prolog
use of parentheses. The general rule is that it is the operator with the highest precedence that is
the principal functor. Thus if `+' has a higher precedence than `/', then
a+b/c
a+(b/c)
are equivalent and denote the term +(a,/(b,c)). Note that the inx form of the term /(+(a,b),c)
must be written with explicit parentheses, i.e.
(a+b)/c
If there are two operators in the subexpression having the same highest precedence, the ambiguity
must be resolved from the types of the operators. The possible types for an inx operator are
xfx
xfy
yfx
Operators of type xfx are not associative: it is a requirement that both of the two subexpressions
which are the arguments of the operator must be of lower precedence than the operator itself,
i.e. their principal functors must be of lower precedence, unless the subexpression is explicitly
parenthesized (which gives it zero precedence).
Operators of type xfy are right-associative: only the rst (left-hand) subexpression must be of lower
precedence; the right-hand subexpression can be of the same precedence as the main operator. Leftassociative operators (type yfx) are the other way around.
A functor named name is declared as an operator of type Type and precedence precedence by the
command
:- op(precedence, type, name).
The argument name can also be a list of names of operators of the same type and precedence.
It is possible to have more than one operator of the same name, so long as they are of dierent
kinds, i.e. inx, prex or postx. An operator of any kind may be redened by a new declaration
of the same kind. This applies equally to operators which are provided as standard. Declarations
of all the standard operators can be found elsewhere (see [Standard Operators], page 403).
For example, the standard operators + and - are declared by
:- op(500, yfx, [ +, - ]).
so that
a-b+c
is valid syntax, and means
(a-b)+c
i.e.
Chapter 2: The Prolog Language
39
+
/
\
/ \
a
b
c
The list functor . is not a standard operator, but if we declare it thus:
:- op(900, xfy, .).
then a.b.c would represent the structure
.
/ \
a
.
/ \
b
c
Contrasting this with the diagram above for a-b+c shows the dierence between yfx operators
where the tree grows to the left, and xfy operators where it grows to the right. The tree cannot
grow at all for xfx operators; it is simply illegal to combine xfx operators having equal precedences
in this way.
The possible types for a prex operator are
fx
fy
and for a postx operator they are
xf
yf
The meaning of the types should be clear by analogy with those for inx operators. As an example,
if not were declared as a prex operator of type fy, then
not not P
would be a permissible way to write not(not(P)). If the type were fx, the preceding expression
would not be legal, although
not P
would still be a permissible form for not(P).
If these precedence and associativity rules seem rather complex, remember that you can always use
parentheses when in any doubt.
Note that the arguments of a compound term written in standard syntax must be expressions of
precedence below 1000. Thus it is necessary to parenthesize the expression P :- Q in
| ?- assert((P :- Q)).
40
SICStus Prolog
2.7 Syntax Restrictions
Note carefully the following syntax restrictions, which serve to remove potential ambiguity associated with prex operators.
1. In a term written in standard syntax, the principal functor and its following ( must not be
separated by any intervening layout-text. Thus
point (X,Y,Z)
is invalid syntax.
2. If the argument of a prex operator starts with a (, this ( must be separated from the operator
by at least one layout-char. Thus
:-(p;q),r.
(where `:-' is the prex operator) is invalid syntax. The system would try to interpret it as
the structure:
,
/ \
:r
|
;
/ \
p
q
That is, it would take `:-' to be a functor of arity 1. However, since the arguments of a functor
are required to be expressions of precedence below 1000, this interpretation would fail as soon
as the `;' (precedence 1100) was encountered.
In contrast, the term:
:- (p;q),r.
is valid syntax and represents the following structure.
:|
,
/ \
;
r
/ \
p
q
2.8 Comments
Comments have no eect on the execution of a program, but they are very useful for making
programs more readily comprehensible. Two forms of comment are allowed in Prolog:
1. The character % followed by any sequence of characters up to end of line.
2. The symbol /* followed by any sequence of characters (including new lines) up to */.
Chapter 3: Running Prolog in Parallel
41
3 Running Prolog in Parallel
This chapter describes what parallel Prolog execution is about, and how Muse can be used as a
\prolog accelerator" to make programs run faster in parallel than they do sequentially.
3.1 Introduction
Logic programs oer many opportunities for the exploitation of parallelism, the main ones being
OR-parallelism and AND-parallelism [Conery 83]. Each kind has its own advantages and disadvantages, but we have chosen to focus on OR-parallelism as a rst step for a number of reasons. From
our perspective, the wide range of potential applications and the very slight execution overhead
have been the main reasons. See [Lusk et al. 90] for a fuller discussion about the advantages of
OR-parallelism in the context of the Aurora system.
The goal with Muse, as with Aurora, has been to allow multiple processors to exploit the implicit
OR-parallelism of Prolog programs with a minimum of overhead, preserving the full Prolog language
with its standard semantics.
3.2 Running a Parallel Example
In this section we illustrate the parallelism in the well-known \8-queens" problem. This code is
reproduced from [Sterling & Shapiro 86] with permission from the authors:
% file: queens.pl
queens(N, Qs) :range(1, N, Ns),
queens(Ns, [], Qs).
queens([], Qs, Qs).
queens(UnplacedQs, SafeQs, Qs) :select(UnplacedQs, UnplacedQs1, Q),
not_attack(SafeQs, Q),
queens(UnplacedQs1, [Q|SafeQs], Qs).
not_attack(Xs, X) :not_attack(Xs, X, 1).
not_attack([], _, _) :- !.
not_attack([Y|Ys], X, N) :X =\= Y+N, X =\= Y-N,
N1 is N+1,
not_attack(Ys, X, N1).
select([X|Xs], Xs, X).
42
SICStus Prolog
select([Y|Ys], [Y|Zs], X) :- select(Ys, Zs, X).
range(N, N, [N]) :- !.
range(M, N, [M|Ns]) :M < N,
M1 is M+1,
range(M1, N, Ns).
run :-
statistics(walltime, [Start,_]),
findall(X, queens(10, X), L),
statistics(walltime, [End,_]),
length(L, N),
Time is End - Start,
format('~d solutions in ~3d seconds.~n', [N,Time]).
Note the use of the walltime key (i.e. real elapsed time) in the call to statistics/2. The runtime
key measures CPU time on the worker that happens to be executing, and so normally is not useful.
Below we show the result of running the queens problem rst with 5 workers and then with a single
worker. In this case the speed-up is 3.8.
% sicstus -P
SICStus -P 3 #0: Mon Oct 3 12:44:31 MET 1994
| ?- compile(queens).
{compiling /src/sicstus/muse/V3/examples/queens.pl...}
yes
| ?- muse_flag(num_workers,_,5).
| ?- run.
724 solutions in 2.760 seconds.
yes
| ?- muse_flag(num_workers,_,1).
| ?- run.
724 solutions in 10.400 seconds.
yes
| ?- Speedup is 10400/2760.
Speedup = 3.7681159420289854 ?
yes
| ?- halt.
%
Chapter 3: Running Prolog in Parallel
43
3.3 Main Issues of Parallel Execution
3.3.1 The Muse Model
The Muse model assumes a multiprocessor system with a number of processors or processes, called
workers, with identical local address spaces, and some global address space shared by all workers.
We use the term worker to represent a process or processor. The Muse model implements each
worker as a sequential Prolog engine with its own WAM stacks in private memory. The Prolog
program is stored in shared memory.
During execution, the actual work is to explore a search tree implicitly dened by the Prolog
program. The available work is represented as choicepoints containing outstanding execution alternatives on one of the WAM stacks.
The work is performed concurrently by the workers. A distributed scheduler is responsible for
dynamic load balancing of available work among available workers. Workers try to maximize the
time they spend working and minimize the time they spend scheduling. When a worker is working,
it adopts a depth-rst left-to-right search strategy.
In the Muse model each worker keeps its own WAM stacks to avoid conicting variable bindings
in common parts of the search tree. Whenever a worker runs Prolog it therefore can use (more
or less) the same machinery as the sequential SICStus Prolog implementation. The exceptions
are (1) bookkeeping of local work each time a choicepoint is created, (2) testing for signals at
predicate calls, and (3) synchronizing with other workers when taking work from a shared (public)
choicepoint. The overhead for doing this is some few percent making Muse (nearly) as ecient as
sequential SICStus Prolog.
When a worker (Q) runs out of work it must interrupt some busy worker (P) to get work. All of P's
private choicepoints are then made public and the dierence between P's and Q's state is copied
from P to Q. Q can then simply simulate failure to backtrack for work.
3.3.2 Scheduling
The two main functions of the scheduler are to maintain the sequential semantics of Prolog and to
match idle workers with the available work with minimal overhead. The sources of overhead in the
Muse model include (1) copying a part of worker state, (2) making local choicepoints sharable, and
(3) grabbing a piece of work (a task) from a shared choicepoint. Other important scheduling issues
are to maximize the grain size of computations, and to prefer \promising" work over less promising
work (see Section 3.3.3 [Muse Exe Cut], page 44).
44
SICStus Prolog
3.3.3 Cut, Side Eects and Suspension
Cut has a semantics strictly compatible with sequential Prolog. Sometimes a cut may be (partly)
invalid due to the fact that the current branch may be pruned by another cut of smaller scope
[Hausman 90]. In this case the worker executes as much as possible of the cut operation, saves
information in the public search tree on how to later on continue executing the cut operation, and
then the worker continues with the current work.
Calls to predicates that produce side-eects are required to synchronize, i.e. to proceed only if they
are on the leftmost branch of the entire tree. If a side-eect predicate is reached on a non-leftmost
branch, that branch will have to suspend.
Suspending and resuming execution branches can be costly, as these actions involve copying state.
The overhead can even outweigh the speedup gained from running in parallel. Therefore, the
use of side-eects in parallel computations is discouraged. Also see Section 3.5 [Programming
Considerations], page 45.
The current work may be endangered (i.e. possibly pruned) by some other worker executing cut
or raise_exception/1. Such work is called speculative work. If there exists untaken work that is
less speculative, then it might be a good idea to suspend the current work and move to the less
speculative one [Ali & Karlsson 92]. This functionality (called voluntary suspension) is implemented
in Muse and it improves execution of \search for one solution" problems dramatically, possibly
from speed-up 1 to nearly linear speed-up. Unfortunately the moving of workers introduces some
overhead (typically below 10 percent). Therefore it is possible to turn voluntary suspension o for
programs searching for all solutions.
3.4 Visualization Tools
The distribution provides two tools that enable one to obtain insight into the parallel behavior of
his Prolog program. These tools can help in understanding \performance bugs", situations in which
a program is executing correctly but not as fast as anticipated, due to an unforeseen restriction of
the degree of parallelism in the program. Once such restrictions are understood they can often be
removed by changing the program, resulting in greatly improved performance.
Both of these tools use log les, that is, they do not display graphics while the program is running,
but rather display the contents of a le that is created during program execution. This method has
several advantages.
A signicant log le can be retained and studied for a long time.
The log le can be \played" far more slowly than the original program ran, allowing careful
and detailed study.
The graphics tracing program need not run on the same machine as Muse itself.
In this manual we will not discuss the various graphics tracers in detail, but rather we will leave
the details of each to its own manual. We will, however, describe how to collect the log les each
one needs.
Chapter 3: Running Prolog in Parallel
45
3.4.1 Must
The Must graphics tracing facility [Karlsson 92] shows the Prolog search tree as it is expanded and
contracted in parallel by Muse workers.
Having created a version of Muse with the Must tracing option, the call \muse_trace(Query)" is
used for running the query \Query" with tracing. After the query is run, there will be a le (with
extension `.mt') containing a trace of the last query execution. The distribution only contains the
Sun-4 executable. It can be used both under SunOS 4.x (Solaris 1.x) and SunOS 5.x (Solaris 2.x).
The program itself has a dialog box to request the lename of the trace le or you can just specify
the le on the command line.
3.4.2 Visandor
The Visandor graphics tracing program was originally developed for the &-Prolog system
[Hermenegildo and Greene 90]. It is supplied in binary executable form for Sun-4 computers. More information about that tool and related work is available via WWW from:
`http://www.clip.dia.fi.upm.es', or via email from <[email protected]>.
The mt2vt tool may be used for converting a Must le to a Visandor le (with extension `.vt').
3.4.3 Vimust
The Must and Visandor tools can be combined into the Vimust tool. If the Must le (with extension
`.mt') and its corresponding Visandor le (with extension `.vt') both exist, they can the be viewed
together with the vimust tool.
3.5 Programming Considerations
It is possible to control the parallelism of a program by means of parallel and sequential
declarations (see Section 5.2 [Declarations], page 53). Sequential declarations may be necessary to
prevent innite loops in certain programming constructs, such as failure driven loops.
Calls of interpreted predicates are synchronized and all interpreted predicates are sequential. The
debugger also sequentializes the execution.
All non-deterministic built-in predicates are sequential. However, if a parallel predicate is called
by call/1, its execution is still parallel.
We have taken pains to preserve the observable \sequential" semantics during parallel execution.
This is achieved through synchronizing all built-in predicates with side-eects. As the foreign
predicates may involve side-eects, in principle they should be synchronized, too. We decided not
to synchronize the foreign predicates as this often may not be necessary. (The user can call the
muse_sync/0 built-in predicate immediately before the foreign predicate to achieve synchronization;
see Section 7.16 [Muse Pred], page 123).
46
SICStus Prolog
There are application areas where the principle of preserving sequential semantics seriously limits
the available parallelism, since communication between parallel execution branches becomes virtually impossible. The principle may even slow down the execution, since suspending and resuming
execution branches are costly operations. One way to overcome these limitations is to use the
foreign language interface. As an experimental language extension, we have furthermore provided
a set of primitives which can be used for non-synchronized communication. These primitives maintain a blackboard, i.e. a mapping from keys to terms, that can be read or written simultaneously
by parallel execution branches. See Section 7.11 [Blackboard Primitives], page 116.
Warning. The blackboard predicates, as well as predicates loaded via the foreign language interface,
can be executed even if the current work is speculative. These predicates are cavalier, i.e. a call
might be executed in a parallel execution, even if that call would never be reached in a sequential
execution, if the code contains cuts (see Section 2.5 [Cut], page 36) or calls to raise_exception/1.
This problem can be partly cured by avoiding cuts while searching for solutions, or by inserting extra
validity tests in the program before calling a cavalier primitive, tests which would be redundant in a
sequential execution. Remember that the validity tests may themselves succeed out of order if they
contain parallelism, so they should also be written without cuts and exceptions, or be explicitly
sequentialized.
Ideally, the cavalier predicates should be asynchronous rather than cavalier. This means that they
should suspend until they are no longer endangered by cuts or calls to raise_exception/1, but
that functionality cannot be provided in the current release, at least.
Other limitations in Muse concern certain built-in predicates and the foreign language interface.
The limitations are mentioned when describing these functions. They can be summarized as follows.
During parallel execution, predicates must not be dened (e.g. by assert/1) or abolished (by
abolish/(1-2)) on the y.
Fastcode compilation is not yet available.
The built-ins stream_select/3, stream_interrupt/3, and break/0 are not available.
Execution proling (see Section 7.15 [Proling], page 121) is not available.
The built-ins save_program/(1,2), restore/1, load_foreign_resource/1, unload_
foreign_resource/1, and load_foreign_files/2 require that Muse be adjusted to one
worker.
The statistics/2 options runtime is not meaningful.
Prolog cannot be called from C.
The hook variable SP_read_hook is not available.
Muse is still an experimental system and is not formally supported.
Chapter 4: The Module System
47
4 The Module System
By making use of the module systems facilities, programs can be divided into dierent modules.
Each module has its own independent predicate name space. This is an important feature for the
development of larger programs. The module system of SICStus Prolog is procedure based. This
means that only the predicates are local to a module, whereas terms are global. The module system
is at, not hierarchical, so all modules are visible to one another. It is non-strict, i.e. the normal
visibility rules can be overridden by special syntax. No overhead is incurred on compiled calls to
predicates in other modules. It is modeled after and compatible with the Quintus Prolog module
system. Finally, using the module system is optional, and SICStus Prolog may be used without
the user being aware of the module system at all.
Modules in SICStus Prolog can also be used for object-oriented programming. See Chapter 32 [Obj
Intro], page 327, for details.
4.1 Basic Concepts
Each predicate in the Prolog system, whether built-in or user dened, belongs to a module. A
predicate is generally only visible in the module where it is dened. However a predicate may be
imported by another module. It is thereby made visible in that module too. Built-in predicates
are visible in every module. Predicates declared as public in a module declaration (see below) are
exported. Normally only public predicates may be imported by another module.
For any given goal, the source module is the module in which the corresponding predicate must be
visible. Similarly, for any given clause, the source module of its head is the module into which the
clause is loaded.
For goals occurring in a source le with a module declaration, the source module is the declared
module. For goals occurring in a source le without a module declaration, the source module is
the module that the le is being loaded into. For goals typed at the top level, the source module
is the type-in module. The type-in module is by default the user module but may be changed by
the built-in predicate module/1.
The other predened module is the prolog module where all the built-in predicates reside. The
exported built-in predicates are automatically imported into each new module as it is created.
48
SICStus Prolog
4.2 Module Prexing
Notwithstanding the visibility rules, any predicate can be called from any other module by prexing
the goal with the module name and the colon operator, thus overriding the source module of the
goal:
| ?- foo:bar(X).
This feature is intended mainly for debugging purposes, since it dees the purposes of the module
system. If the prexed goal is a meta-predicate, however, the prexed module name may aect the
module name expansion of the goal (see Section 4.5 [Meta Exp], page 49).
It is also possible to override the source module of clauses and directives by module prexing. For
example,
:- dynamic mod:p/1.
p(X) :- mod:(q(X), r(X)).
mod:(q(X) :- r(X)).
mod:s(X) :- t(X).
declares mod:p/1 as dynamic, whatever the source module is; denes p/1 in the source module as
calling mod:q/1 and mod:r/1; denes mod:q/1 as calling mod:r/1; and denes mod:s/1 as calling
t/1 in the source module. The latter technique is particularly useful when the prex is user and
the predicate is a \hook" predicate such as user:portray/1 which must be dened in the user
module, but the rest of the le consists of predicates belonging to some other module.
4.3 Dening Modules
A module is normally dened by putting a module declaration rst in a source le. A module
declaration has the form:
:- module(ModuleName, ExportList[, Options ]).
When the le is loaded, all predicates in the le go into ModuleName and the predicates of the
ExportList are exported. When a module declaration is processed, all existing predicates in the
module are erased before the new ones are loaded. A le which contains a module declaration is
henceforth called a module le.
Options is an optional argument, and should be a list. The only available option is hidden(Bool ),
where Bool is false (the default) or true. In the latter case, tracing of the predicates of the
modules is disabled (although spy-points can be set), and no source information is generated at
compile time.
A module can also be dened dynamically by asserting or loading predicates to it:
| ?- assert(m:p(x)).
creates the module m, if it does not already exists, and asserts p(x) to it.
Chapter 4: The Module System
49
| ?- compile(m:f).
creates the module m and loads f into m.
Dynamically created modules have no public predicates.
4.4 Importation
When a module le is loaded by load_files/(1,2) or one of its shorthands (see Section 7.1.1
[Read In], page 73), by default all the public predicates of the module le are imported by the
receiving module. An explicit list of predicates to import may also be specied.
Clashes with already existing predicates, local or imported from other modules, are handled in two
dierent ways: If the receiving module is the user module, the user is asked for redenition of the
predicate. For other receiving modules, a warning is issued and the importation is canceled. In
the rst case redenition silently takes place if the ag redefine_warnings has the value off (see
prolog_flag/3). The binding of an imported predicate remains, even if the origin is reloaded or
deleted. However, abolish/1 breaks up the importation binding. When a module le is reloaded,
a check is made that the predicates imported by other modules are still in the public list. If that
is not the case, a warning is issued. Note that an imported predicate may be re-exported.
4.5 Module Name Expansion
Some predicates take goals as arguments (i.e. meta-predicates). These arguments must include a
module specication stating which module the goal refers. Some other predicates also need module
information i.e. compile/1. The property of needing module information is declared with a metapredicate declaration (see Section 4.6 [Meta Decl], page 50). Goals for these predicates are module
name expanded to ensure the module information. Goals issued at top level and goals appearing
as directives are expanded prior to execution while goals in the bodies of clauses are expanded
at compile time. The expansion is made by preceding the relevant argument with `Module :'. If
the goal is prexed by `Module :', Module is used for the expansion, otherwise the source/type-in
module is used. An argument is not expanded if:
1. It already has a module prex.
2. It is a variable which appears in an expandable position in the head of the clause.
Some examples:
| ?- [user].
| :- meta_predicate p(:), q(:).
| r(X) :- p(X).
| q(X) :- p(X).
| ^D
{user consulted, 40 msec 1088 bytes}
yes
50
SICStus Prolog
| ?- listing.
r(A) :-
q(A) :-
p(user:A).
p(A).
yes
Here, p/1 and q/1 are declared as meta-predicates while r/1 is not. Thus the clause r(X) :- p(X)
will be transformed to r(X) :- p(M :X), by item 2 above, where M is the type-in module, whereas
q(X) :- p(X) will not.
| ?- m:assert(f(1)).
Here, assert/1 is called in the module m. However, this does not ensure that f(1) is asserted into
m. The fact that assert/1 is a meta-predicate makes the system module name expand the goal,
transforming it to m:assert(m:f(1)) before execution. This way, assert/1 is supplied the correct
module information.
4.6 Meta-Predicate Declarations
The fact that a predicate needs module name expansion is declared in a meta-predicate declaration:
:- meta_predicate MetaPredSpec,
, MetaPredSpec.
:::
where each MetaPredSpec is a mode spec. E.g.
:- meta_predicate p(:, +).
which means that the rst argument of p/2 shall be module name expanded. The arguments in
the mode spec are interpreted as:
:
An integer
This argument, in any call to the declared predicate, shall be expanded. (Integers are
allowed for compatibility reasons).
Anything else e.g. +, - or ?
This argument shall not be expanded
A number of built-in predicates have predened meta-predicate declarations, as indicated by the
mode specs in this manual, e.g. call(:Term).
Chapter 5: Loading Programs
51
5 Loading Programs
Programs can be loaded in three dierent ways: consulted or compiled from source le, or loaded
from object les. The latter is the fastest way of loading programs, but of course requires that
the programs have been compiled to object les rst. Object les may be handy when developing
large applications consisting of many source les, but are not strictly necessary since it is possible
to save and restore entire execution states (see Section 7.17 [Misc Pred], page 124).
Consulted, or interpreted, predicates are equivalent to, but slower than, compiled ones. Although
they use dierent representations, the two types of predicates can call each other freely.
The SICStus Prolog compiler produces compact and ecient code, running about 8 times faster
than consulted code, and requiring much less runtime storage. Compiled Prolog programs are
comparable in eciency with LISP programs for the same task. However, against this, compilation
itself takes about twice as long as consulting, and tracing of goals that compile in-line are not
available in compiled code.
The compiler operates in three dierent modes, controlled by the \Compilation mode" ag (see
prolog_flag/3). The possible states of the ag are:
compactcode
fastcode
Compilation produces byte-coded abstract instructions. This is the default unless SICStus Prolog has been installed with support for fastcode compilation.
Compilation produces native machine instructions. Currently only available for 68K,
Sparc and MIPS platforms, but not for Muse. Fastcode runs about 3 times faster than
compactcode. This is the default if SICStus Prolog has been installed with support for
fastcode compilation.
profiledcode
Compilation produces byte-coded abstract instructions instrumented to produce execution proling data. See Section 7.15 [Proling], page 121. Proling is not available
in Runtime Systems.
debugcode
Compilation produces interpreted code, i.e. compiling is replaced by consulting.
The compilation mode can be changed by issuing the directive:
| ?- prolog_flag(compiling, OldValue, NewValue ).
A Prolog program consists of a sequence of sentences (see [Sentence], page 396). Commands and
queries encountered among the sentences are executed immediately as they are encountered, unless
they can be interpreted as declarations (see Section 5.2 [Declarations], page 53), which aect the
treatment of forthcoming clauses. Clauses are loaded as they are encountered.
A Prolog program may also contain a list of sentences (including the empty list). This is treated as
equivalent to those sentences occurring in place of the list. This feature makes it possible to have
user:term_expansion/(2,4) (see Section 7.1.2 [Denite], page 76) \return" a list of sentences,
instead of a single sentence.
52
SICStus Prolog
5.1 Predicates which Load Code
To consult a program, issue the directive:
| ?- consult(Files).
where Files is either the name of a le (including the le `user') or a list of lenames instructs the
processor to read-in the program which is in the les. For example:
| ?- consult([dbase,'extras.pl',user]).
When a directive is read it is immediately executed. Any predicate dened in the les erases any
clauses for that predicate already present. If the old clauses were loaded from a dierent le than the
present one, the user will be queried rst whether (s)he really wants the new denition. However,
if a multifile declaration (see Section 5.2 [Declarations], page 53) is read and the corresponding
predicate exists and has previously been declared as multifile, new clauses will be added to the
predicate, rather than replacing the old clauses. If clauses for some predicate appear in more
than one le, the later set will eectively overwrite the earlier set. The division of the program into
separate les does not imply any module structure|any predicate can call any other (see Chapter 4
[Module Intro], page 47).
consult/1,
used in conjunction with save_program/(1,2) and restore/1, makes it possible to
amend a program without having to restart from scratch and consult all the les which make up
the program. The consulted le is normally a temporary \patch" le containing only the amended
predicate(s). Note that it is possible to call consult(user) and then enter a patch directly on the
terminal (ending with ^D). This is only recommended for small, tentative patches.
| ?- [File|Files].
This is a shorthand way of consulting a list of les. (The case where there is just one lename in
the list was described earlier (see Section 1.2 [Reading In], page 11).
To compile a program in-core, use the built-in predicate:
| ?- compile(Files).
where Files is specied just as for consult/1.
The eect of compile/1 is very much like that of consult/1, except all new procedures will be
stored in compiled rather than consulted form. However, predicates declared as dynamic (see
below) will be stored in consulted form, even though compile/1 is used.
To compile a program into an object le, use the built-in predicate:
| ?- fcompile(Files).
where Files is specied just as for consult/1. For each lename in the list, the compiler will
append the sux `.pl' to it and try to locate a source le with that name and compile it to an
object le. The object lename if formed by appending the sux `.ql' to the specied name.
Chapter 5: Loading Programs
53
The internal state of SICStus Prolog is not changed as result of the compilation. See Section 5.3
[Considerations], page 58.
To load a program from a set of object les, use the built-in predicate:
| ?- load(Files).
where Files is either a single object lename (specied with or without a trailing `.ql') or a list
of lenames. For each lename in the list, this predicate will search for a le, with the sux `.ql'
added if not specied. This directive has the same eect as if the source les had been compiled
using compile/1 directly (but see Section 5.3 [Considerations], page 58).
Finally, to ensure that some les have been compiled or loaded, use the built-in predicate:
| ?- ensure_loaded(Files).
where Files is either a single lename or a list of lenames, similar to the arguments accepted by
the above predicates. The predicate takes the following action for each File in the list of lenames:
1. If the File is user, compile(user) is performed;
2. If File cannot be found, not even with a `.pl' or `.ql' extension, an error is signaled;
3. If an object le is found which has not yet been loaded or which has a modication time more
recent than what was recorded for the le when it was previously loaded, the le is loaded;
4. If a source le is found which has not yet been loaded or which has a modication time more
recent than what was recorded for the le when it was previously loaded, the le is compiled;
5. If both a source le and an object le are found, item 3 or 4 applies depending on which le
was modied most recently; in case 4 ensure_loaded/1 does not cause object les to become
recompiled.
6. Otherwise, no action is taken.
5.2 Declarations
When a program is to be loaded, it is sometimes necessary to tell the system to treat some of the
predicates specially. This information is supplied by including declarations about such predicates
in the source le, preceding any clauses for the predicates which they concern. A declaration is
written just as a command, beginning with `:-'. A declaration is eective from its occurrence
through the end of le.
Although declarations that aect more than one predicate may be collapsed into a single declaration,
the recommended style is to write the declarations for a predicate immediately before its rst clause.
Operator declarations are not declarations proper, but rather commands that modify the global
table of syntax operators. Operator declarations are executed as they are encountered while loading
programs.
The rest of this section details the available forms of predicate declarations.
54
SICStus Prolog
5.2.1 Multile Declarations
A declaration
:- multifile
PredSpec,
:::
,
PredSpec.
where each PredSpec is a predicate spec, causes the specied predicates to become multile. This
means that if more clauses are subsequently loaded from other les for the same predicate, then
the new clauses will not replace the old ones, but will be added at the end instead. As of release 3,
multile declarations are required in all les from where clauses to a multile predicate are loaded.
An example when multile declarations are particularly useful is in dening hook predicates. A hook
predicate is a user-dened predicate that somehow alters or customizes the behavior of SICStus
Prolog. A number of such hook predicates are described in this manual. Often, an application needs
to combine the functionality of several software modules, some of which dene clauses for such hook
predicates. By simply declaring every hook predicates as multile, the functionality of the clauses
for the hook predicates is automatically combined. If this is not done, the last software module to
dene clauses for a particular hook predicate will eectively supersede any clauses dened for the
same hook predicate in a previous module.
If a le containing clauses for a multile predicate is reloaded, the old clauses from the same le
are removed. The new clauses are added at the end.
If a multile predicate is loaded from a le with no multile declaration for it, the predicate is
redened as if it were an ordinary predicate (i.e. the user is asked for conrmation).
Clauses of multile predicates are (currently) always loaded in interpreted form, even if they were
processed by the compiler. If performance is an issue, dene the multile predicates as unit clauses
or as clauses with a single goal that just calls an auxiliary compiled predicate to perform any
time-critical computation.
If a multile predicate is declared dynamic in one le, it must also be done so in the other les
from where it is loaded. Hook predicates should always be declared as multile and dynamic, as
this is the convention followed in the library modules.
Multile declarations must precede any other declarations for the same predicate(s)!
5.2.2 Dynamic Declarations
A declaration
PredSpec, , PredSpec.
where each PredSpec is a predicate spec, causes the specied predicates to become dynamic, which
means that other predicates may inspect and modify them, adding or deleting individual clauses.
Dynamic predicates are always stored in consulted form even if a compilation is in progress. This
declaration is meaningful even if the le contains no clauses for a specied predicate|the eect is
then to dene a dynamic predicate with no clauses.
:- dynamic
:::
Chapter 5: Loading Programs
55
5.2.3 Volatile Declarations
A declaration
:- volatile
PredSpec,
:::
,
PredSpec.
where each PredSpec is a predicate spec, causes the specied predicates to become volatile.
A predicate should be declared as volatile if it refers to data that cannot or should not be saved in
a saved state. In most cases a volatile predicate will be dynamic, and it will be used to keep facts
about streams or memory references. When a program state is saved at run-time, the clauses of
all volatile predicates will be left unsaved. The predicate denitions will be saved though, which
means that the predicates will keep all properties, that is volatile and maybe dynamic or multile,
when the saved state is restored.
5.2.4 Block Declarations
The declaration
:- block
BlockSpec,
:::
,
BlockSpec.
where each BlockSpec is a mode spec, species conditions for blocking goals of the predicate
referred to by the mode spec (f/3 say). When a goal for f/3 is to be executed, the mode specs are
interpreted as conditions for blocking the goal, and if at least one condition evaluates to true, the
goal is blocked.
A block condition evaluates to true i all arguments specied as `-' are uninstantiated, in which
case the goal is blocked until at least one of those variables is instantiated. If several conditions
evaluate to true, the implementation picks one of them and blocks the goal accordingly.
The recommended style is to write the block declarations in front of the source code of the predicate
they refer to. Indeed, they are part of the source code of the predicate, and must precede the rst
clause. For example, with the denition:
:- block merge(-,?,-), merge(?,-,-).
merge([], Y,
merge(X, [],
merge([H|X],
merge([H|X],
Y).
X).
[E|Y], [H|Z]) :- H @< E, merge(X, [E|Y], Z).
[E|Y], [E|Z]) :- H @>= E, merge([H|X], Y, Z).
calls to merge/3 having uninstantiated arguments in the rst and third position or in the second
and third position will suspend.
The behavior of blocking goals for a given predicate on uninstantiated arguments cannot be switched
o, except by abolishing or redening the predicate.
Block declarations generalize the "wait declarations" of earlier versions of SICStus Prolog. A
declaration `:- wait f/3' in the old syntax corresponds to `:- block f(-,?,?)' in the current
56
SICStus Prolog
syntax. See Section 10.5.6 [Use Of Term Exp], page 182, for a simple way to extend the system to
accept the old syntax.
5.2.5 Meta-Predicate Declarations
A declaration
:- meta_predicate
MetaPredSpec,
:::
,
MetaPredSpec.
where each MetaPredSpec is a mode spec, informs the compiler that certain arguments of the
declared predicates are used for passing goals. To ensure the correct semantics in the context of
multiple modules, clauses or directives containing goals for the declared predicates may need to
have those arguments module name expanded. See Section 4.5 [Meta Exp], page 49, for details.
5.2.6 Module Declarations
A declaration
:- module(ModuleName,
ExportList[, Options ]).
where ExportList is a list of predicate specs, declares that the forthcoming predicates should go into
the module named ModuleName and that the predicates listed should be exported. See Section 4.3
[Def Modules], page 48, for details.
5.2.7 Public Declarations
A declaration
:- public
PredSpec,
:::
,
PredSpec.
where each PredSpec is a predicate spec, has no eect whatsoever, but is accepted for compatibility
reasons. In some Prologs, this declaration is necessary for making compiled predicates visible. In
SICStus Prolog, predicate visibility is handled by the module system. See Chapter 4 [Module Intro],
page 47.
5.2.8 Mode Declarations
A declaration
:- mode
ModeSpec,
:::
,
ModeSpec.
where each ModeSpec is a mode spec, has no eect whatsoever, but is accepted for compatibility
reasons. In some Prologs, this declaration helps reduce the size of the compiled code for a predicate,
and may speed up its execution. Unfortunately, writing mode declarations can be error-prone, and
since errors in mode declaration do not show up while running the predicates interpretively, new
Chapter 5: Loading Programs
57
bugs may show up when predicates are compiled. However, mode declarations may be used as a
commenting device, as they express the programmer's intention of data ow in predicates.
5.2.9 Parallel and Sequential Declarations
The following declarations serve for the explicit control of parallelism in the Muse Development
System.
:- parallel PredSpec,
, PredSpec.
:::
enables the clauses of the specied predicates to be run in parallel (this is the default).
:- sequential PredSpec,
, PredSpec.
:::
prohibits the clauses of the specied predicates from running in parallel.
Universal versions of both declarations also exist:
:- sequential.
:- parallel.
This indicates to the compiler that all subsequent predicates in the given le become sequential or
parallel, unless specically declared otherwise. The default is to treat all predicates as parallel. Sequential declarations may be necessary to prevent innite loops in certain programming constructs,
such as failure driven loops. e.g. the loop
program :initialize,
generate_solution(X),
process_solution(X),
fail_if_not_last(X),
!,
shut_down.
will generate an unbounded number of suspended branches if generate_solution/1 is a parallel
predicate with an unbounded number of alternatives and process_solution/1 involves a synchronized operation.
In some rare cases it might also be useful to prevent \slow down" due to too ne granularity in
some predicate call. To nd such cases you may use the Must trace tool (see Section 3.4.1 [Must],
page 45).
58
SICStus Prolog
5.3 Considerations for File-To-File Compilation
When compiling a source le to an object le, remember that clauses are loaded and directives are
executed at run time, not at compile time. Only predicate declarations are processed at compile
time. For instance, it does not work to include operator declarations or clauses of user:term_
expansion/(2,4) or user:goal_expansion/3 or any auxiliary predicates that they might need,
and rely on the new transformations to be eective for subsequent clauses of the same le or
subsequent les of the same compilation.
Any directives or clauses that aect the compile-time environment must be loaded prior to compiling
source les to object les. This also holds for meta-predicates called by the source les but dened
elsewhere, for module name expansion to work correctly. If this separation into les is unnatural
or inconvenient, one can easily ensure that the compile-time environment is up to date by doing:
| ?- ensure_loaded(Files ), fcompile(Files ).
Since module name expansion takes place at compile time, the module into which the le is to be
loaded must be known when compiling to object les. This is no problem for module les because
the module name is picked from the module declaration. When non-module les are compiled, the
le name may be prexed with the module name that is to be used for expansion:
| ?- fcompile(Module:Files).
If an object le is loaded into a dierent module from which it was compiled for, a warning is
issued.
Chapter 6: Debugging
59
6 Debugging
This chapter describes the debugging facilities that are available in Development Systems. The
purpose of these facilities is to provide information concerning the control ow of your program.
In the Muse Development System, the debugger eectively sequentializes the execution. Other
tools (see Section 3.4 [Visualization Tools], page 44) must be used to debug the parallel aspects of
execution.
The main features of the debugging package are as follows:
The Procedure Box model of Prolog execution which provides a simple way of visualizing
control ow, especially during backtracking. Control ow is viewed at the predicate level,
rather than at the level of individual clauses.
The ability to exhaustively trace your program or to selectively set spy-points. Spy-points
allow you to nominate interesting predicates at which the program is to pause so that you can
interact.
The wide choice of control and information options available during debugging.
The Procedure Box model of execution is also called the Byrd Box model after its inventor, Lawrence
Byrd.
Much of the information in this chapter is also in Chapter eight of [Clocksin & Mellish 81] which
is recommended as an introduction.
Unless otherwise stated, the debugger print goals using write_term/3 with the value of the Prolog
ag debugger_print_options (see Section 7.6 [State Info], page 104).
The debugger is not available in Runtime Systems and the predicates dened in this chapter are
undened; see Section 8.8 [Runtime Systems], page 151.
6.1 The Procedure Box Control Flow Model
During debugging, the debugger prints out a sequence of goals in various states of instantiation in
order to show the state the program has reached in its execution. However, in order to understand
what is occurring it is necessary to understand when and why the debugger prints out goals. As in
other programming languages, key points of interest are predicate entry and return, but in Prolog
there is the additional complexity of backtracking. One of the major confusions that novice Prolog
programmers have to face is the question of what actually happens when a goal fails and the system
suddenly starts backtracking. The Procedure Box model of Prolog execution views program control
ow in terms of movement about the program text. This model provides a basis for the debugging
mechanism in Development Systems, and enables the user to view the behavior of the program in
a consistent way.
Let us look at an example Prolog predicate :
60
SICStus Prolog
*--------------------------------------*
Call
|
|
Exit
---------> + descendant(X,Y) :- offspring(X,Y). + --------->
|
|
| descendant(X,Z) :|
<--------- +
offspring(X,Y), descendant(Y,Z). + <--------Fail
|
|
Redo
*-------------------+------------------*
|
<------------------------------+
Exception
The rst clause states that Y is a descendant of X if Y is an ospring of X, and the second clause
states that Z is a descendant of X if Y is an ospring of X and if Z is a descendant of Y. In the
diagram a box has been drawn around the whole predicate and labeled arrows indicate the control
ow in and out of this box. There are ve such arrows which we shall look at in turn.
Call
This arrow represents initial invocation of the predicate. When a goal of the form
descendant(X,Y) is required to be satised, control passes through the Call port of the
descendant box with the intention of matching a component clause and then satisfying
any subgoals in the body of that clause. Note that this is independent of whether such
a match is possible; i.e. rst the box is called, and then the attempt to match takes
place. Textually we can imagine moving to the code for descendant when meeting a
call to descendant in some other part of the code.
Exit
This arrow represents a successful return from the predicate. This occurs when the
initial goal has been unied with one of the component clauses and any subgoals have
been satised. Control now passes out of the Exit port of the descendant box. Textually
we stop following the code for descendant and go back to the place we came from.
Redo
This arrow indicates that a subsequent goal has failed and that the system is backtracking in an attempt to nd alternatives to previous solutions. Control passes through the
Redo port of the descendant box. An attempt will now be made to resatisfy one of the
component subgoals in the body of the clause that last succeeded; or, if that fails, to
completely rematch the original goal with an alternative clause and then try to satisfy
any subgoals in the body of this new clause. Textually we follow the code backwards
up the way we came looking for new ways of succeeding, possibly dropping down on to
another clause and following that if necessary.
Fail
This arrow represents a failure of the initial goal, which might occur if no clause is
matched, or if subgoals are never satised, or if any solution produced is always rejected
by later processing. Control now passes out of the Fail port of the descendant box and
the system continues to backtrack. Textually we move back to the code which called
this predicate and keep moving backwards up the code looking for choice points.
Exception This arrow represents an exception which was raised in the initial goal, either by a
call to raise_exception/1 or by an error in a built-in predicate. See Section 7.5
[Exception], page 101. Control now passes out of the Exception port of the descendant
box and the systems continues to pass the exception to outer levels. Textually we move
Chapter 6: Debugging
61
back to the code which called this predicate and keep moving backwards up the code
looking for a call to on_exception/3.
In terms of this model, the information we get about the procedure box is only the control ow
through these ve ports. This means that at this level we are not concerned with which clause
matches, and how any subgoals are satised, but rather we only wish to know the initial goal and
the nal outcome. However, it can be seen that whenever we are trying to satisfy subgoals, what
we are actually doing is passing through the ports of their respective boxes. If we were to follow
this, then we would have complete information about the control ow inside the procedure box.
Note that the box we have drawn round the predicate should really be seen as an invocation box.
That is, there will be a dierent box for each dierent invocation of the predicate. Obviously, with
something like a recursive predicate, there will be many dierent Calls and Exits in the control
ow, but these will be for dierent invocations. Since this might get confusing each invocation box
is given a unique integer identier.
6.2 Basic Debugging Predicates
Development Systems provide a range of built-in predicates for control of the debugging facilities.
The most basic predicates are as follows:
debug
zip
trace
Switches the debugger on, and ensures that the next time control reaches a spy-point,
a message will be produced and you will be prompted for a command. In order for
the full range of control ow information to be available it is necessary to have this
on from the start. When it is o the system does not remember invocations that are
being executed. (This is because it is expensive and not required for normal running
of programs.) You can switch Debug Mode on in the middle of execution, either from
within your program or after a ^C (see trace/0 below), but information prior to this
will just be unavailable.
Same as debug/0, except no debugging information is being collected, and so is almost
as fast as running with the debugger switched o.
Switches the debugger on, and ensures that the next time control enters an invocation
box, a message will be produced and you will be prompted for a command. The eect
of trace can also be achieved by typing t after a ^C interruption of a program.
At this point you have a number of options. See Section 6.5 [Debug Options], page 65.
In particular, you can just type RET to creep (or single-step) into your program. If you
continue to creep through your program you will see every entry and exit to/from every
invocation box, including compiled code, except for code belonging to hidden modules
(see Section 4.3 [Def Modules], page 48). You will notice that the debugger stops at
all ports. However, if this is not what you want, the following built-in predicate gives
full control over the ports at which you are prompted:
62
SICStus Prolog
leash(+Mode )
Leashing Mode is set to Mode. Leashing Mode determines the ports of invocation
boxes at which you are to be prompted when you Creep through your program. At
unleashed ports a tracing message is still output, but program execution does not stop
to allow user interaction. Note that the ports of spy-points are always leashed (and
cannot be unleashed). Mode can be a subset of the following, specied as a list:
call
Prompt on Call.
exit
Prompt on Exit.
redo
Prompt on Redo.
fail
Prompt on Fail.
exception
Prompt on Exception.
The initial value of Leashing Mode is [call,exit,redo,fail,exception] (full leashing).
nodebug
notrace
nozip
debugging
Switches the debugger o. If there are any spy-points set then they will be kept but
disabled.
Prints information about the current debugging state. This will show:
1. Whether undened predicates are being trapped.
2. Whether the debugger is switched on.
3. What spy-points have been set (see below).
4. What mode of leashing is in force (see below).
6.3 Spy-points
For programs of any size, it is clearly impractical to creep through the entire program. Spy-points
make it possible to stop the program whenever it gets to a particular predicate which is of interest.
Once there, one can set further spy-points in order to catch the control ow a bit further on, or
one can start creeping.
Setting a spy-point on a predicate indicates that you wish to see all control ow through the
various ports of its invocation boxes, except during skips. When control passes through any port
of a invocation box with a spy-point set on it, a message is output and the user is asked to interact.
Note that the current mode of leashing does not aect spy-points: user interaction is requested on
every port.
Spy-points are set and removed by the following built-in predicates. The rst two are also standard
operators:
spy
:Spec Sets spy-points on all the predicates given by Spec, which is is of the form:
Chapter 6: Debugging
63
Name
all predicates called Name no matter what arity, where Name is an atom
for a specic name or a variable for all names, or
Name/Arity
the predicate of that name and arity, or
Name/Low-High
Name/(Low-High)
the predicates of that name with arity in the range Low-High, or
Module:Spec
specifying a particular module (see Section 4.2 [Module Spec], page 48)
instead of the default type-in module, where Module is an atom for a
specic module or a variable for all modules, or
[Spec,...,Spec]
the set of all predicates covered by the Specs.
You cannot place a spy-point on an undened predicate. If you set some spy-points
when the debugger is switched o then it will be automatically switched on. Examples:
| ?- spy [user:p, m:q/(2-3)].
| ?- spy m:[p/1, q/1].
nospy
:Spec
Similar to spy Spec except that all the predicates given by Spec will have previously
set spy-points removed from them.
nospyall Removes all the spy-points that have been set.
spypoint_condition(:Goal,?Port,+Test)
Sets a conditional spy-point on the predicate for Goal. When the debugger reaches the
spy-point, three conditions must be met for the spy-point to apply: The actual goal
must match Goal, the actual port must match Port, and Test (an arbitrary Prolog
goal) must succeed.
The options available when you arrive at a spy-point are described later. See Section 6.5 [Debug
Options], page 65.
6.4 Format of Debugging messages
We shall now look at the exact format of the message output by the system at a port. All trace
messages are output to the standard error stream. This allows you to trace programs while they
are performing le I/O. The basic format is as follows:
E N S
23
6 Call: T foo(hello,there,_123) ?
E is only used at Exception ports and displays a pending exception.
N is only used at Exit ports and indicates whether the invocation could backtrack and nd alternative solutions. Unintended non-determinism is a source of ineciency, and this annotation can
64
SICStus Prolog
help spot such eciency bugs. It is printed as `?', indicating that foo/3 could backtrack and nd
alternative solutions, or ` ' otherwise.
S is a spy-point indicator. It is printed as `+', indicating that there is a spy-point on foo/3, or ` ',
denoting no spy-point.
The rst number is the unique invocation identier. It is nondecreasing regardless of whether or not
you are actually seeing the invocations (provided that the debugger is switched on). This number
can be used to cross correlate the trace messages for the various ports, since it is unique for every
invocation. It will also give an indication of the number of procedure calls made since the start of
the execution. The invocation counter starts again for every fresh execution of a command, and it
is also reset when retries (see later) are performed.
The number following this is the current depth; i.e. the number of direct ancestors this goal has.
The next word species the particular port (Call, Exit, Redo, Fail, or Exception).
T is a subterm trace. This is used in conjunction with the `^' command (set subterm), described
below. If a subterm has been selected, T is printed as the sequence of commands used to select the
subterm. Normally, however, T is printed as ` ', indicating that no subterm has been selected.
The goal is then printed so that you can inspect its current instantiation state.
The nal `?' is the prompt indicating that you should type in one of the option codes allowed (see
Section 6.5 [Debug Options], page 65). If this particular port is unleashed then you will obviously
not get this prompt since you have specied that you do not wish to interact at this point.
Note that calls that are compiled in-line and built-in predicates that are called directly from the
top level are not traced.
There are two exceptions to the above debugger message format. A message
S - - Block: p(_133)
indicates that the debugger has encountered a blocked goal, i.e. one which is temporarily suspended
due to insuciently instantiated arguments (see Section 2.3 [Procedural], page 34). No interaction
takes place at this point, and the debugger simply proceeds to the next goal in the execution stream.
The suspended goal will be eligible for execution once the blocking condition ceases to exist, at
which time a message
S - - Unblock: p(_133)
is printed.
Chapter 6: Debugging
65
6.5 Options Available during Debugging
This section describes the particular options that are available when the system prompts you after
printing out a debugging message. All the options are one letter mnemonics, some of which can
be optionally followed by a decimal integer. They are read from the standard input stream with
any blanks being completely ignored up to the end of the line (RET). Some options only actually
require the terminator; e.g. the creep option, as we have already seen, only requires RET.
The only option which you really have to remember is `h' (followed by RET). This provides help in
the form of the following list of available options.
RET
l
s
o
q
r
f
d
p
g
t
&
n
+
a
@
e
<
^
?
c
RET
l
z
creep
leap
skip
out
q-skip
retry
fail
display
print
ancestors
backtrace
blocked goals
nodebug
spy this
nospy this
abort
command
raise exception
reset printdepth
reset subterm
help
c
z
s
o
q
r
f
w
p
g
t
&
=
+
.
b
u
<i>
<n>
<i>
<i>
<i>
<i>
<n>
<n>
<n>
<i>
< <n>
^ <n>
h
creep
zip
skip i
out n
q-skip i
retry i
fail i
write
print partial
ancestors n
backtrace n
nth blocked goal
debugging
spy conditionally
find this
break
unify
set printdepth
set subterm
help
creep causes the debugger to single-step to the very next port and print a message.
Then if the port is leashed (see Section 6.2 [Basic Debug], page 61), the user is prompted
for further interaction. Otherwise, it continues creeping. If leashing is o, creep is the
same as leap (see below) except that a complete trace is printed on the standard error
stream.
leap causes the debugger to resume running your program, only stopping when a spypoint is reached (or when the program terminates). Leaping can thus be used to follow
the execution at a higher level than exhaustive tracing. All you need to do is to set
spy-points on an evenly spread set of pertinent predicates, and then follow the control
ow through these by leaping from one to the other. Debugging information is collected
while leaping, so when a spy-points is reached, it is possible to inspect the ancestor
goals, or creep into them upon entry to Redo ports.
zip is like leap, except no debugging information is being collected while zipping, resulting in signicant savings in memory and execution time.
66
s
o
q
r
f
d
p
SICStus Prolog
skip is only valid for Call and Redo ports. It skips over the entire execution of the
predicate. That is, you will not see anything until control comes back to this predicate
(at either the Exit port or the Fail port). Skip is particularly useful while creeping
since it guarantees that control will be returned after the (possibly complex) execution
within the box. If you skip then no message at all will appear until control returns.
This includes calls to predicates with spy-points set; they will be masked out during
the skip. No debugging information is being collected while skipping.
If you supply an integer argument, then this should denote an invocation number of an
ancestral goal. The system tries to get you to the Exit or Fail port of the invocation
box you have specied.
out is a shorthand for skipping to the Exit or Fail port of the immediate ancestor goal.
If you supply an integer argument n, it denotes skipping to the Exit or Fail port of the
nth ancestor goal.
quasi-skip is like a combination of leap and skip: execution stops when either control
comes back to this predicate, or a spy-point is reached. No debugging information is
being collected while quasi-skipping.
An integer argument can be supplied as for skip.
retry can be used at any of the four ports (although at the Call port it has no eect).
It transfers control back to the Call port of the box. This allows you to restart an
invocation when, for example, you nd yourself leaving with some weird result. The
state of execution is exactly the same as when you originally called, (unless you use side
eects in your program; i.e. asserts etc. will not be undone). When a retry is performed
the invocation counter is reset so that counting will continue from the current invocation
number regardless of what happened before the retry. This is in accord with the fact
that you have, in executional terms, returned to the state before anything else was
called.
If you supply an integer argument, then this should denote an invocation number of an
ancestral goal. The system tries to get you to the Call port of the box you have specied.
It does this by continuously failing until it reaches the right place. Unfortunately this
process cannot be guaranteed: it may be the case that the invocation you are looking
for has been cut out of the search space by cuts (!) in your program. In this case the
system fails to the latest surviving Call port before the correct one.
fail can be used at any of the four ports (although at the Fail port it has no eect). It
transfers control to the Fail port of the box, forcing the invocation to fail prematurely.
If you supply an integer after the command, then this is taken as specifying an invocation number and the system tries to get you to the Fail port of the invocation box you
have specied. It does this by continuously failing until it reaches the right place. Unfortunately this process cannot be guaranteed: it may be the case that the invocation
you are looking for has been cut out of the search space by cuts (!) in your program.
In this case the system fails to the latest surviving Fail port before the correct one.
display goal displays the current goal using display/1. See Write (below).
print goal re-prints the current goal. An argument will override the default printdepth,
treating 0 as innity.
Chapter 6: Debugging
w
g
t
&
n
=
+
67
write goal writes the current goal using write/1.
print ancestor goals provides you with a list of ancestors to the current goal, i.e. all
goals that are hierarchically above the current goal in the calling sequence. You can
always be sure of jumping to any goal in the ancestor list (by using retry etc). If you
supply an integer n, then only that number of ancestors will be printed. That is to say,
the last n ancestors will be printed counting back from the current goal. Each entry is
displayed just as they would be in a trace message.
print backtrace is the same as the above, but also shows any goals that have exited
non-deterministically and their ancestors. This information shows where there are
outstanding choices that the program could backtrack to. If you supply an integer n,
then only that number of goals will be printed.
Ancestors to the current goal are annotated with the `Call:' port, as they have not
yet exited, whereas goals that have exited are annotated with the `Exit:' port. The
backtrace is a tree rather than a stack: to nd the parent of a given goal with depth
indicator d, look for the closest goal above it with depth indicator d-1.
print blocked goals prints a list of the goals which are currently blocked in the current
debugging session together with the variable that each such goal is blocked on (see
Section 2.3 [Procedural], page 34). The goals are enumerated from 1 and up. If you
supply an integer n, then only that goal will be printed. Each entry is preceded by the
goal number followed by the variable name.
nodebug switches the debugger o. Note that this is the correct way to switch debugging o at a trace point. You cannot use the @ or b options because they always return
to the debugger.
debugging outputs information concerning the status of the debugging package. See
Section 7.14 [Debug Pred], page 120, the built-in debugging/0.
spy this sets a spy-point on the current goal. With an argument, prompts for a condition
to be tested each time the debugger reaches the spy-point. Conditions consist of three
parts: a port, a goal template, and a test (arbitrary Prolog goal), usually sharing some
variables with the test part. For the spy-point to apply, the port and goal parts must
match, and the test must succeed, otherwise the spy-point is simply disabled. The
following example illustrates how to dene a spy-point to only apply at Call ports
when a test is satised:
4 2 Call: nr([0,1,2,3,4,5,6,7,8,9,...],_267) ? + 1
Goal, Port, Cond: nr(L,_), call, (length(L,N), N<3).
{Spy-point placed on user:nr/2}
+ 4 2 Call: nr([0,1,2,3,4,5,6,7,8,9,...],_267) ? l
+ 32 30 Call: nr([8,9],_2771) ? s
.
a
nospy this removes the spy-point from the current goal, if it exists, and any condition
for that spy-point.
nd this outputs information about where the predicate being called is dened.
abort causes an abort of the current execution. All the execution states built so far are
destroyed and you are put right back at the top-level. (This is the same as the built-in
predicate abort/0.)
68
b
@
u
e
<
^
?
h
SICStus Prolog
break calls the built-in predicate break/0, thus putting you at a recursive top-level
with the execution so far sitting underneath you. When you end the break (^D) you
will be reprompted at the port at which you broke. The new execution is completely
separate from the suspended one; the invocation numbers will start again from 1 during
the break. The debugger is temporarily switched o as you call the break and will be
re-switched on when you nish the break and go back to the old execution. However,
any changes to the leashing or to spy-points will remain in eect.
command gives you the ability to call arbitrary Prolog goals. It is eectively a one-o
break (see above). The initial message `| :- ' will be output on the standard error
stream, and a command is then read from the standard input stream and executed as
if you were at top level.
unify is available at the Call port and gives you the option of providing a solution
to the goal from the standard input stream rather than executing the goal. This is
convenient e.g. for providing a \stub" for a predicate that has not yet been written.
A prompt will be output on the standard error stream, and the solution is then read
from the standard input stream and unied with the goal.
raise exception is available at all ports. A prompt will be output on the standard error
stream, and an exception term is then read from the standard input stream and raised
in the program being debugged.
This command, without arguments, resets the printdepth to 10. With an argument of
n, the printdepth is set to n, treating 0 as innity.
While at a particular port, a current subterm of the current goal is maintained. It
is the current subterm which is displayed, printed, or written when prompting for a
debugger command. Used in combination with the printdepth, this provides a means
for navigating in the current goal for focusing on the part which is of interest. The
current subterm is set to the current goal when arriving at a new port. This command,
without arguments, resets the current subterm to the current goal. With an argument
of n (> 0), the current subterm is replaced by its n:th subterm. With an argument of
0, the current subterm is replaced by its parent term. With a list of arguments, the
arguments are applied from left to right.
help displays the table of options given above.
6.6 Consulting during Debugging
It is possible, and sometimes useful, to consult a le whilst in the middle of program execution.
Predicates, which have been successfully executed and are subsequently redened by a consult
and are later reactivated by backtracking, will not notice the change of their denitions. In other
words, it is as if every predicate, when called, creates a virtual copy of its denition for backtracking
purposes.
If SICStus Prolog is run via the Emacs interface, the commands for loading code (such as C-c
C-p, consulting the current predicate) are not directly available when the system prompts you after
Chapter 6: Debugging
69
printing out a debugging message. Press b followed by RET to get a recursive top-level, ready to
accept the Emacs commands. Type ^D to return to the debugging port.
6.7 Catching Exceptions
Usually, exceptions that occur during debugging sessions are displayed only in trace mode and
for invocation boxes for predicates with spy-points on them, and not during skips. However, it is
sometimes useful to make exceptions trap to the debugger at the earliest opportunity instead. The
following predicate provides such a possibility
error_exception(+Exception)
user:error_exception(+Exception)
A hook predicate. This predicate is called at all exception ports. If it succeeds, the
debugger enters trace mode and prints an exception port message. Otherwise, the
debugger mode is unchanged and a message is printed only in trace mode or if a spypoint is reached, and not during skips.
70
SICStus Prolog
Chapter 7: Built-In Predicates
71
7 Built-In Predicates
It is not possible to redene built-in predicates. An attempt to do so will give an error message.
See [Pred Summary], page 379.
SICStus Prolog provides a wide range of built-in predicates to perform the following tasks:
Input / Output
Reading-in Programs
Term and Goal Expansion
Input and Output of Terms
Character I/O
Stream I/O
Dec-10 Prolog File I/O
Arithmetic
Comparison of Terms
Control
Error and Exception Handling
Information about the State of the Program
Meta-Logic
Modication of Terms
Modication of the Program
Internal Database
Blackboard Primitives
All Solutions
Coroutining
Debugging
Execution Proling
Muse Support
Miscellaneous
The following descriptions of the built-in predicates are grouped according to the above categorization of their tasks.
7.1 Input / Output
There are two sets of le manipulation predicates in SICStus Prolog. One set is inherited from
DEC-10 Prolog. These predicates always refer to a le by name. The other set of predicates is
modeled after Quintus Prolog and refer to les as streams. Streams correspond to the le pointers
used at the operating system level.
A stream can be opened and connected to a lename or le descriptor for input or output by calling
the predicate open/3. open/3 will return a reference to a stream. The stream may then be passed
as an argument to various I/O predicates. The predicate close/1 is used for closing a stream. The
predicate current_stream/3 is used for retrieving information about a stream, and for nding the
currently existing streams.
72
SICStus Prolog
Prolog streams can be accessed from C functions as well. See Section 8.5 [SICStus Streams],
page 144, for details.
The possible formats of a stream are:
'$stream'(X )
A stream connected to some le. X is an integer.
user_input
An alias initially referring to the UNIX stdin stream. The alias can be changed with
prolog_flag/3 and accessed by the C variable SP_stdin.
user_output
An alias initially referring to the UNIX stdout stream. The alias can be changed with
prolog_flag/3 and accessed by the C variable SP_stdout.
user_error
An alias initially referring to the UNIX stderr stream. The alias can be changed with
prolog_flag/3 and accessed by the C variable SP_stderr.
This stream is used by the Prolog top level and debugger, and for progress and error
messages.
The DEC-10 Prolog I/O predicates manipulate streams implicitly, by maintaining the notion of a
current input stream and a current output stream. The current input and output streams are set
to the user_input and user_output initially and for every new break (see Section 1.9 [Nested],
page 16). The predicate see/1 (tell/1) can be used for setting the current input (output) stream to
newly opened streams for particular les. The predicate seen/0 (told/0) close the current input
(output) stream, and resets it to the standard input (output) stream. The predicate seeing/1
(telling/1) is used for retrieving the lename associated with the current input (output) streams.
The possible formats of a lename are:
user
File
This \lename" stands for the standard input or output stream, depending on context.
Terminal output is only guaranteed to be displayed if the output stream is explicitly
ushed.
where File is any atom other than user, denotes a le File (with an optional `.pl'
sux when consulting or compiling or an optional `.ql' sux in load/1) located using
services provided by the operating system. On UNIX systems, Filenames beginning
with `/' are absolute; other lenames are looked up in the current working directory.
Alias (File)
where File is an atom, denotes an alias which must be rewritten using the user dened
predicate user:file_search_path/2 until an atomic lename is obtained.
Filenames beginning with `~' or `$' are treated specially. For example,
`~/sample.pl'
is equivalent to `/home/sics/al/sample.pl', if `/home/sics/al' is the user's home
directory. (This is also equivalent to `$HOME/sample.pl' as explained below.)
Chapter 7: Built-In Predicates
73
`~clyde/sample.pl'
is equivalent to `/home/sics/clyde/sample.pl', if `/home/sics/clyde' is Clyde's
home directory.
`$UTIL/sample.pl'
is equivalent to `/usr/local/src/utilities/sample.pl', provided the value of the
environment variable UTIL is `/usr/local/src/utilities'.
Failure to open a le normally causes an exception to be raised. This behavior can be turned o
and on by of the built-in predicates nofileerrors/0 and fileerrors/0 described below.
7.1.1 Reading-in Programs
When the predicates discussed in this section are invoked, lenames are relative to the current
working directory. During the load, the current working directory is temporarily changed to the
directory containing the le being read in. This has the eect that if one of these predicates is
invoked recursively, the lename of the recursive load is relative to the directory of the enclosing
load. See Chapter 5 [Load Intro], page 51, for an introduction to these predicates.
Directives will be executed in order of occurrence. Be aware of the changed current working
directory as it could have an eect on the semantics of directives. Only the rst solution of directives
is produced, and variable bindings are not displayed. Directives that fail or raise exceptions cause
warnings, but do not terminate the load.
Most of the predicates listed below take an argument Files which is a single le name or a list of
le names. Source les usually end with an `.pl' sux and object le names with an `.ql' sux.
These suxes are optional. Each le name may optionally be prexed by a module name. The
module name species where to import the exported predicates of a module le, or where to store
the predicates of a non-module le. The module is created if it doesn't exist already.
absolute_file_name/2 (see Section 7.1.5 [Stream Pred], page 89) is
le name user is reserved and denotes the standard input stream.
used to look up the les. The
These predicates are available in Runtime Systems with the following limitations:
The compiler is not available, so compiling is replaced by consulting.
The redefine_warnings and single_var_warnings Prolog ags have no eect.
Progress and warning messages are not issued.
The user is not prompted in the event of name clashes etc.
load_files(:Files )
load_files(:Files, +Options )
A generic predicate for loading les with a list of options to provide extra control.
This predicate in fact subsumes the other predicates except use_module/3 which also
returns the name of the loaded module. Options is a list of zero or more of the following:
74
SICStus Prolog
if(X )
(the default) to always load, or changed to load only if the le has
not yet been loaded or if it has been modied since it was last loaded. A
non-module le is not considered to have been previously loaded if it was
loaded into a dierent module. The le user is never considered to have
been previously loaded.
true
when(When)
always
(the default) to always load, or compile_time to load only if the
goal is not in the scope of another load_files/(1,2) goal loading object
code.
The latter is intended for use when the le only denes predicates that are
needed for proper term or goal expansion during compilation of other les.
load_type(LoadType)
source to load source code only, object to load object code only, or latest
(the default) to load source or object code, whichever is newest. If the le
is user, source is forced.
imports(Imports )
all (the default) to import all exported predicates if the le is a module
le, or a list of predicates to import.
compilation_mode(Mode )
compile to translate into compiled code, consult to translate into static,
interpreted code, or assert_all to translate into dynamic, interpreted
code.
The default is the compilation mode of any ancestor load_files/(1,2)
goal, or compile otherwise. Note that Mode has no eect when an object
le is loaded, and that it is recommended to use assert_all in conjunction
with load_type(source), to ensure that the source le will be loaded even
in the presence of an object le.
consult(:Files )
reconsult(:Files )
[]
[:File |+Files ]
Consults the source le or list of les specied by File and Files. Same as load_
files(Files, [load_type(source),compilation_mode(consult)]).
compile(:Files )
Compiles the source le or list of les specied by Files. The compiled code is placed
in-core, i.e. is added incrementally to the Prolog database. Same as load_files(Files,
[load_type(source),compilation_mode(compile)]).
load(:Files )
Loads the object le or list of les specied by Files. Same as load_files(Files,
[load_type(object)]).
Chapter 7: Built-In Predicates
ensure_loaded(:Files )
75
Compiles or loads the le or les specied by Files that have been modied after the
le was last loaded, or that have not yet been loaded. The recommended style is to use
this predicate for non-module les only, but if any module les are encountered, their
public predicates are imported. Same as load_files(Files, [if(changed)]).
use_module(:File )
Compiles or loads the module le specied by File if it has been modied after it was last
loaded, or not yet been loaded. Its public predicates are imported. The recommended
style is to use this predicate for module les only, but any non-module les encountered
are simply compiled or loaded. Same as load_files(File, [if(changed)]).
use_module(:File, +Imports )
Loads the module le File like ensure_loaded/1 and imports the predicates in Imports.
If any of these are not public, a warning is issued. Imports may also be set to the
atom all in which case all public predicates are imported. Same as load_files(File,
[if(changed),imports(Imports)]).
use_module(?Module, :File, +Imports )
This is equivalent to use_module/2 with the addition that Module is unied with the
loaded module after the loading.
fcompile(:Files )
Compiles the source le or list of les specied by Files. If Files are prexed by a
module name, that module name will be used for module name expansion during the
compilation (see Section 5.3 [Considerations], page 58). The sux `.pl' is added to
the given lenames to yield the real source lenames. The compiled code is placed on
the object le or list of les formed by adding the sux `.ql' to the given lenames.
(This predicate is not available in Runtime Systems.)
source_file(?File )
File is the absolute name of a source le currently in the system.
source_file(:Head,?File )
source_file(-Head,?File )
Head is the most general goal for a predicate loaded from File.
require(:PredSpecOrSpecs )
PredSpecOrSpecs is a predicate spec or a list or a conjunction of such. The predicate
will check if the specied predicates are loaded and if not, will try to load or import
them using use_module/2. The le containing the predicate denitions will be located
in the following way:
The directories specied with user:library_directory/1 are searched for a le
`INDEX.pl'. This le is taken to contain relations between all exported predicates
of the module les in the library directory and its subdirectories. If an `INDEX.pl' is
not found, require/1 will try to create one by loading the library package mkindex
and calling make_index:make_library_index(Directory ) (see Chapter 11 [The
Prolog Library], page 187).
The rst index entry for the requested predicate will be used to determine the le
to load. An exception is raised if the predicate can't be located.
76
SICStus Prolog
Once an `INDEX.pl' is read, it is cached internally for use in subsequent calls to
require/1.
Not available in Runtime Systems.
7.1.2 Term and Goal Expansion
When a program is being read in, SICStus Prolog provides hooks that enable the terms being read
in to be source-to-source transformed before the usual processing of clauses or directives. The hooks
consist in user-dened predicates that dene the transformations. One transformation is always
available, however: denite clause grammars, a convenient notation for expressing grammar rules.
See [Colmerauer 75] and [Pereira & Warren 80].
Denite clause grammars are an extension of the well-known context-free grammars. A grammar
rule in Prolog takes the general form
head --> body.
meaning \a possible form for head is body". Both body and head are sequences of one or more
items linked by the standard Prolog conjunction operator `,'.
Denite clause grammars extend context-free grammars in the following ways:
1. A non-terminal symbol may be any Prolog term (other than a variable or number).
2. A terminal symbol may be any Prolog term. To distinguish terminals from non-terminals, a
sequence of one or more terminal symbols is written within a grammar rule as a Prolog list.
An empty sequence is written as the empty list `[]'. If the terminal symbols are character
codes, such lists can be written (as elsewhere) as strings. An empty sequence is written as the
empty list, `[]' or `""'.
3. Extra conditions, in the form of Prolog procedure calls, may be included in the right-hand side
of a grammar rule. Such procedure calls are written enclosed in `{}' brackets.
4. The left-hand side of a grammar rule consists of a non-terminal, optionally followed by a
sequence of terminals (again written as a Prolog list).
5. Disjunction, if-then, if-then-else, and not-provable may be stated explicitly in the right-hand
side of a grammar rule, using the operators `;' (`|'), `->', and `\+' as in a Prolog clause.
6. The cut symbol may be included in the right-hand side of a grammar rule, as in a Prolog
clause. The cut symbol does not need to be enclosed in `{}' brackets.
As an example, here is a simple grammar which parses an arithmetic expression (made up of digits
and operators) and computes its value.
Chapter 7: Built-In Predicates
77
expr(Z) --> term(X), "+", expr(Y), {Z is X + Y}.
expr(Z) --> term(X), "-", expr(Y), {Z is X - Y}.
expr(X) --> term(X).
term(Z) --> number(X), "*", term(Y), {Z is X * Y}.
term(Z) --> number(X), "/", term(Y), {Z is X / Y}.
term(Z) --> number(Z).
number(C) --> "+", number(C).
number(C) --> "-", number(X), {C is -X}.
number(X) --> [C], {"0"=<C, C=<"9", X is C - "0"}.
In the last rule, C is the character code of some digit.
The query
| ?- expr(Z, "-2+3*5+1", []).
will compute Z=14. The two extra arguments are explained below.
Now, in fact, grammar rules are merely a convenient \syntactic sugar" for ordinary Prolog clauses.
Each grammar rule takes an input string, analyses some initial portion, and produces the remaining
portion (possibly enlarged) as output for further analysis. The arguments required for the input
and output strings are not written explicitly in a grammar rule, but the syntax implicitly denes
them. We now show how to translate grammar rules into ordinary clauses by making explicit the
extra arguments.
A rule such as
p(X) --> q(X).
translates into
p(X, S0, S) :- q(X, S0, S).
If there is more than one non-terminal on the right-hand side, as in
p(X, Y) -->
q(X),
r(X, Y),
s(Y).
then corresponding input and output arguments are identied, as in
p(X, Y, S0, S) :q(X, S0, S1),
r(X, Y, S1, S2),
r(Y, S2, S).
Terminals are translated using the built-in predicate 'C'(S1, X, S2 ), read as \point S1 is connected by terminal X to point S2", and dened by the single clause
78
SICStus Prolog
'C'([X|S], X, S).
(This predicate is not normally useful in itself; it has been given the name upper-case c simply to
avoid using up a more useful name.) Then, for instance
p(X) --> [go,to], q(X), [stop].
is translated by
p(X, S0, S) :'C'(S0, go, S1),
'C'(S1, to, S2),
q(X, S2, S3),
'C'(S3, stop, S).
Extra conditions expressed as explicit procedure calls naturally translate as themselves, e.g.
p(X) --> [X], {integer(X), X>0}, q(X).
translates to
p(X, S0, S) :'C'(S0, X, S1),
integer(X),
X>0,
q(X, S1, S).
Similarly, a cut is translated literally.
Terminals are translated using the built-in predicate 'C'(S1, X, S2 ), read as \point S1 is connected by terminal X to point S2", and dened by the single clause
Terminals on the left-hand side of a rule are also translated using 'C'/3, connecting them to the
output argument of the head non-terminal, e.g.
is(N), [not] --> [aint].
becomes
is(N, S0, S) :'C'(S0, aint, S1),
'C'(S, not, S1).
Disjunction has a fairly obvious translation, e.g.
args(X, Y) -->
(
dir(X), [to], indir(Y)
;
indir(Y), dir(X)
).
translates to
Chapter 7: Built-In Predicates
79
args(X, Y, S0, S) :(
dir(X, S0, S1),
'C'(S1, to, S2),
indir(Y, S2, S)
;
indir(Y, S0, S1),
dir(X, S1, S)
).
Similarly for if-then, if-then-else, and not-provable.
The built-in predicates which are concerned with grammar rules and other compile/consult time
transformations are as follows:
expand_term(+Term1,?Term2 )
If Term1 is a term that can be transformed, Term2 is the result. Otherwise Term2
is just Term1 unchanged. This transformation takes place automatically when grammar rules are read in, but sometimes it is useful to be able to perform it explicitly.
Grammar rule expansion is not the only transformation available; the user may dene
clauses for the predicate user:term_expansion/(2,4) to perform other transformations. user:term_expansion(Term1 [,Layout1 ],Term2 [,Layout2 ]) is called rst,
and only if it fails is the standard expansion used.
term_expansion(+Term1,?TermOrTerms )
user:term_expansion(+Term1,?TermOrTerms )
term_expansion(+Term1,+Layout1,?TermOrTerms,?Layout2 )
user:term_expansion(+Term1,+Layout1,?TermOrTerms,?Layout2 )
A hook predicate, which denes transformations on terms read while a program is
consulted or compiled. It is called for every Term1 read, including at end of le,
represented as the term end_of_file. If it succeeds, TermOrTerms is used for further
processing, otherwise the default grammar rule expansion is attempted. It is often
useful to let a term expand to a list of commands and clauses, which will then be
processed sequentially.
The 4 arguments version also denes transformations on the layout of the term read,
so that the source-linked debugger can display accurate source code lines if the transformed code needs debugging. Layout1 is the layout corresponding to Term1, and Layout2 should be a valid layout of TermOrTerms (see Section 7.1.3 [Term I/O], page 80).
For accessing aspects of the load context, e.g. the name of the le being compiled, the
predicate prolog_load_context/2 (see Section 7.6 [State Info], page 104) can be used.
user:term_expansion/(2,4) may also be used to transform queries entered at the
terminal in response to the `| ?- ' prompt. In this case, it will be called with Term1
= ?-(Query ) and should succeed with TermOrTerms = ?-(ExpandedQuery ).
goal_expansion(+Goal,+Module,?NewGoal )
user:goal_expansion(+Goal,+Module,?NewGoal )
A hook predicate. This predicate denes transformations on goals while clauses
are being consulted, compiled or asserted, after any processing by user:term_
expansion/(2,4) of the terms being read in. It is called for every simple Goal encountered in the calling context Module. If it succeeds, Goal is replaced by NewGoal,
80
SICStus Prolog
otherwise Goal is left unchanged. NewGoal may be an arbitrarily complex goal, and
user:goal_expansion/3 is recursively applied to its subgoals.
This predicate is also used to resolve meta-calls to Goal at runtime via the same mechanism. If the transformation succeeds, NewGoal is simply called instead of Goal. Otherwise, if Goal is a goal of an existing predicate, that predicate is invoked. Otherwise,
error recovery is attempted by user:unknown_predicate_handler/3 as described below.
user:goal_expansion/3 can be regarded as a macro expansion facility. It is used for
this purpose to support the interface to attributed variables in library(atts), which
denes the predicates M :get_atts/2 and M :put_atts/2 to access module-specic
variable attributes. These \predicates" are actually implemented via the user:goal_
expansion/3 mechanism. This has the eect that calls to the interface predicates are
expanded at compile time to ecient code.
For accessing aspects of the load context, e.g. the name of the le being compiled, the
predicate prolog_load_context/2 (see Section 7.6 [State Info], page 104) can be used.
phrase(:Phrase,?List)
phrase(:Phrase,?List,+Remainder )
The list List is a phrase of type Phrase (according to the current grammar rules), where
Phrase is either a non-terminal or more generally a grammar rule body. Remainder is
what remains of the list after a phrase has been found. If called with 2 arguments, the
remainder has to be the empty list.
'C'(?S1,?Terminal,?S2 )
Not normally of direct use to the user, this built-in predicate is used in the expansion
of grammar rules (see above). It is dened as if by the clause 'C'([X|S], X, S).
7.1.3 Input and Output of Terms
Most of the following predicates come in two versions, with or without a stream argument. Predicates without a stream argument operate on the current input or output stream, depending on
context.
Some of these predicates support a notation for terms containing multiple occurrences of the same
subterm (cycles and DAGs). The notation is @(Template,Substitution) where Substitution is a
list of Var=Term pairs where the Var occurs in Template or in one of the Terms. This notation
stands for the instance of Template obtained by binding each Var to its corresponding Term. The
purpose of this notation is to provide a nite printed representation of cyclic terms. This notation
is not used by default, and @/2 has no special meaning except in this context.
read(?Term) [ISO]
read(+Stream,?Term)
[ISO]
The next term, delimited by a full-stop (i.e. a ., possibly followed by layout text), is
read from Stream and is unied with Term. The syntax of the term must agree with
current operator declarations. If a call read(Stream, Term) causes the end of Stream
to be reached, Term is unied with the term end_of_file. Further calls to read/2
Chapter 7: Built-In Predicates
81
for the same stream will then raise an exception, unless the stream is connected to the
terminal.
read_term(?Term,+Options ) [ISO]
read_term(+Stream,?Term,+Options ) [ISO]
Same as read/(1-2) with a list of options to provide extra control or information about
the term. Options is a list of zero or more of:
syntax_errors(+Val )
Controls what action to take on syntax errors. Val must be one of the
values allowed for the syntax_errors Prolog ag. The default is set by
that ag.
variable_names(?Names )
Names is bound to a list of Name=Var pairs, where each Name is an atom
indicating the name of a non-anonymous variable in the term, and Var is
the corresponding variable.
singletons(?Names )
Names is bound to a list of Name=Var pairs, one for each variable appearing only once in the term and whose name does not begin with _.
cycles(+Boolean)
Boolean must be true or false. If selected, any occurrences of @/2 in
the term read in are replaced by the potentially cyclic terms they denote
as described above. Otherwise (the default), Term is just unied with the
term read in.
layout(?Layout)
Layout is bound to a layout term corresponding to Term. The layout Y of
a term X is one of:
If X is a variable or atomic term, Y is the number of the line where
X occurs.
If X is a compound term, Y is a list whose head is the number of the
line where the rst token of X occurs, and whose remaining elements
are the layouts of the arguments of X.
[], if no line number information is available for X.
| ?- read_term(T, [layout(L), variable_names(Va), singletons(S)]).
|: [
foo(X),
X = Y
].
L = [35,[36,36],[36,[37,37,37],38]],
S = ['Y'=_A],
T = [foo(_B),_B=_A],
Va = ['X'=_B,'Y'=_A]
82
write(?Term) [ISO]
write(+Stream,?Term)
SICStus Prolog
[ISO]
The term Term is written onto Stream according to current operator declarations.
Same as write_term([Stream,] Term, [numbervars(true)]).
display(?Term)
The term Term is displayed onto the standard output stream (which is not necessarily
the current output stream) in standard parenthesized prex notation. Same as write_
term(user, Term, [ignore_ops(true)]).
write_canonical(?Term) [ISO]
write_canonical(+Stream,?Term) [ISO]
Similar to write(Stream,Term). The term will be written according to the standard
syntax. The output from write_canonical/2 can be parsed by read/2 even if the
term contains special characters or if operator declarations have changed. Same as
write_term([Stream,] Term, [quoted(true),ignore_ops(true)]).
writeq(?Term) [ISO]
writeq(+Stream,?Term) [ISO]
Similar to write(Stream,Term), but the names of atoms and functors are quoted
where necessary to make the result acceptable as input to read/2, provided the
same operator declarations are in eect. Same as write_term([Stream,] Term,
[quoted(true),numbervars(true)]).
print(?Term)
print(+Stream,?Term)
Hookable. Prints Term onto Stream. This predicate provides a handle for user dened
pretty printing:
If Term is a variable then it is output using write(Stream,Term).
If Term is non-variable then a call is made to the user dened predicate
user:portray/1. If this succeeds then it is assumed that Term has been output.
Otherwise print/2 is called recursively on the components of Term, unless Term
is atomic in which case it is written via write/2.
In particular, the debugging package prints the goals in the tracing messages, and the
top-level prints the nal values of variables. Thus you can vary the forms of these
messages if you wish.
Note that on lists ([_|_]), print/2 will rst give the whole list to user:portray/1,
but if this fails it will only give each of the (top level) elements to user:portray/1.
That is, user:portray/1 will not be called on all the tails of the list.
Same as write_term([Stream,] Term, [portrayed(true),numbervars(true)]).
portray(+Term)
user:portray(+Term)
A hook predicate. This should either print the Term and succeed, or do nothing and
fail. In the latter case, the default printer (write/1) will print the Term.
Chapter 7: Built-In Predicates
portray_clause(+Clause )
portray_clause(+Stream,+Clause )
83
Writes the clause Clause onto Stream exactly as listing/(0-1) would have written
it.
Same
as
write_term([Stream,]
Term, [quoted(true),numbervars(true),indented(true)]) followed by a period
and a newline, removing redundant module prexes and binding variables to terms of
the form '$VAR'(N ) yielding friendlier variable names.
write_term(+Term,+Options ) [ISO]
write_term(+Stream,+Term,+Options ) [ISO]
Same as write/(1-2) etc. with a list of options to provide extra control. This predicate in fact subsumes the above output predicates except portray_clause/(1,2)
which additionally prints a period and a newline, and removes module prexes that
are redundant wrt. the current type-in module. Options is a list of zero or more of the
following, where Boolean must be true or false (false is the default).
quoted(+Boolean)
If selected, functors are quoted where necessary to make the result acceptable as input to read/1. write_canonical/1, writeq/1, and portray_
clause/1 select this.
ignore_ops(+Boolean)
If selected, Term is written in standard parenthesized notation instead of
using operators. write_canonical/1 and display/1 select this.
portrayed(+Boolean)
If selected, user:portray/1 is called for each subterm. print/1 selects
this.
numbervars(+Boolean)
If selected, occurrences of '$VAR'(N ) where N is an integer >= 0 are
treated specially (see numbervars/3). print/1, write/1, writeq/1, and
portray_clause/1 select this.
cycles(+Boolean)
If selected, the potentially cyclic term is printed in nite @/2 notation, as
discussed above.
indented(+Boolean)
If selected, the term is printed with the same indentation as is used by
portray_clause/1 and listing/(0-1).
max_depth(N )
Depth limit on printing. N is an integer. 0 (the default) means no limit.
format(+Format,+Arguments )
format(+Stream,+Format,+Arguments )
Prints Arguments onto Stream according to format Format. Format is a list of formatting characters. If Format is an atom then name/2 (see Section 7.7 [Meta Logic],
page 110) will be used to translate it into a list of characters. Thus
| ?- format("Hello world!", []).
has the same eect as
84
SICStus Prolog
| ?- format('Hello world!', []).
and format/3 is a Prolog interface to the C stdio function printf(). It is
modeled after and compatible with Quintus Prolog.
Arguments is a list of items to be printed. If there are no items then an empty list
should be supplied.
The default action on a format character is to print it. The character ~ introduces a
control sequence. To print a ~ repeat it:
format/2
| ?- format("Hello ~~world!", []).
will result in
Hello ~world!
The escape sequence (see [Escape Sequences], page 401) \c (c for continue) is useful when formatting a string for readability. It causes all characters up to, but not
including, the next non-layout character to be ignored.
| ?- format("Hello \c
world!", []).
will result in
Hello world!
The general format of a control sequence is `~NC'. The character C determines the
type of the control sequence. N is an optional numeric argument. An alternative form
of N is `*'. `*' implies that the next argument in Arguments should be used as a
numeric argument in the control sequence. Example:
and
| ?- format("Hello~4cworld!", [0'x]).
| ?- format("Hello~*cworld!", [4,0'x]).
both produce
Helloxxxxworld!
The following control sequences are available.
`~a'
The argument is an atom. The atom is printed without quoting.
`~N c'
(Print character.) The argument is a number that will be interpreted as
a character code. N defaults to one and is interpreted as the number of
times to print the character.
`~N e'
`~N E'
`~N f'
`~N g'
`~N G'
(Print oat). The argument is a oat. The oat and N will be passed to
the C printf() function as
printf("%.N e", Arg )
printf("%.N E", Arg )
printf("%.N f", Arg )
printf("%.N g", Arg )
printf("%.N G", Arg )
Chapter 7: Built-In Predicates
`~N d'
85
respectively.
If N is not supplied the action defaults to
printf("%e", Arg )
printf("%E", Arg )
printf("%f", Arg )
printf("%g", Arg )
printf("%G", Arg )
respectively.
(Print decimal.) The argument is an integer. N is interpreted as the
number of digits after the decimal point. If N is 0 or missing, no decimal
point will be printed. Example:
| ?- format("Hello ~1d world!", [42]).
Hello 4.2 world!
| ?- format("Hello ~d world!", [42]).
Hello 42 world!
`~N D'
(Print decimal.) The argument is an integer. Identical to `~N d' except
that `,' will separate groups of three digits to the left of the decimal point.
Example:
| ?- format("Hello ~1D world!", [12345]).
Hello 1,234.5 world!
`~N r'
(Print radix.) The argument is an integer. N is interpreted as a radix, 2
=< N =< 36. If N is missing the radix defaults to 8. The letters `a-z' will
denote digits larger than 9. Example:
| ?- format("Hello ~2r world!", [15]).
Hello 1111 world!
| ?- format("Hello ~16r world!", [15]).
Hello f world!
`~N R'
(Print radix.) The argument is an integer. Identical to `~N r' except that
the letters `A-Z' will denote digits larger than 9. Example:
| ?- format("Hello ~16R world!", [15]).
Hello F world!
`~N s'
(Print string.) The argument is a list of character codes. Exactly N characters will be printed. N defaults to the length of the string. Example:
| ?- format("Hello ~4s ~4s!", ["new","world"]).
Hello new worl!
| ?- format("Hello ~s world!", ["new"]).
Hello new world!
`~i'
(Ignore.) The argument, which may be of any type, is ignored. Example:
| ?- format("Hello ~i~s world!", ["old","new"]).
Hello new world!
86
SICStus Prolog
`~k'
(Print canonical.) The argument may be of any type. The argument will
be passed to write_canonical/1 (see Section 7.1.3 [Term I/O], page 80).
Example:
| ?- format("Hello ~k world!", [[a,b,c]]).
Hello .(a,.(b,.(c,[]))) world!
`~p'
(Print.) The argument may be of any type. The argument will be passed
to print/1 (see Section 7.1.3 [Term I/O], page 80). Example:
| ?- assert((portray([X|Y]) :- print(cons(X,Y)))).
| ?- format("Hello ~p world!", [[a,b,c]]).
Hello cons(a,cons(b,cons(c,[]))) world!
`~q'
(Print quoted.) The argument may be of any type. The argument will be
passed to writeq/1 (see Section 7.1.3 [Term I/O], page 80). Example:
| ?- format("Hello ~q world!", [['A','B']]).
Hello ['A','B'] world!
`~w'
(Write.) The argument may be of any type. The argument will be passed
to write/1 (see Section 7.1.3 [Term I/O], page 80). Example:
| ?- format("Hello ~w world!", [['A','B']]).
Hello [A,B] world!
`[email protected]'
(Call.) The argument is a goal, which will be called and expected to print
on the current output stream. If the goal is not a built-in predicate, it
should be module prexed, as format/(2-3) are not meta-predicates. If
the goal performs other side-eects or does not succeed deterministically,
the behavior is undened. Example:
| ?- format("Hello [email protected] world!", [write(new)]).
Hello new world!
`~~'
(Print tilde.) Takes no argument. Prints `~'. Example:
| ?- format("Hello ~~ world!", []).
Hello ~ world!
`~N n'
(Print newline.) Takes no argument. Prints N newlines. N defaults to 1.
Example:
| ?- format("Hello ~n world!", []).
Hello
world!
`~N'
(Print Newline.) Prints a newline if not at the beginning of a line.
The following control sequences set column boundaries and specify padding. A column
is dened as the available space between two consecutive column boundaries on the
same line. A boundary is initially assumed at line position 0. The specications only
apply to the line currently being written.
When a column boundary is set (`~|' or `~+') and there are fewer characters written
in the column than its specied width, the remaining space is divided equally amongst
the pad sequences (`~t') in the column. If there are no pad sequences, the column is
space padded at the end.
Chapter 7: Built-In Predicates
87
If `~|' or `~+' species a position preceding the current position, the boundary is set at
the current position.
`~N |'
Set a column boundary at line position N. N defaults to the current position.
`~N +'
Set a column boundary at N positions past the previous column boundary.
N defaults to 8.
`~N t'
Specify padding in a column. N is the ll character code. N may also be
specied as `C where C is the ll character. The default ll character is
SPC. Any (`~t') after the last column boundary on a line is ignored.
Example:
| ?format("~`*t NICE TABLE ~`*t~61|~n", []),
format("*~t*~61|~n", []),
format("*~t~a~20|~t~a~t~20+~a~t~20+~t*~61|~n",
['Right aligned','Centered','Left aligned']),
format("*~t~d~20|~t~d~t~20+~d~t~20+~t*~61|~n",
[123,45,678]),
format("*~t~d~20|~t~d~t~20+~d~t~20+~t*~61|~n",
[1,2345,6789]),
format("~`*t~61|~n", []).
************************ NICE TABLE *************************
*
*
*
Right aligned
Centered
Left aligned
*
*
123
45
678
*
*
1
2345
6789
*
*************************************************************
7.1.4 Character Input/Output
There are two sets of character I/O predicates. The rst set uses the current input and output
streams, while the second set always uses the standard input and output streams. The rst set
is available in an alternative version where the stream is specied explicitly. The rule is that the
stream is the rst argument, which defaults to the current input or output stream, depending on
context.
nl [ISO]
nl(+Stream)
[ISO]
A new line is started on Stream by printing an LFD. If Stream is connected to the
terminal, its buer is ushed.
get0(?N ) [ISO]
get0(+Stream,?N ) [ISO]
N is the character code of the next character read from Stream. If all characters of
Stream have been read, N is -1, and further calls to get0/2 for the same stream will
then raise an exception, unless the stream is connected to the terminal.
88
SICStus Prolog
These predicates are called get_code/(1-2) in the ISO Prolog standard.
peek_char(?N ) [ISO]
peek_char(+Stream,?N ) [ISO]
N is the character code of the next character from Stream, or -1, if all characters of
Stream have been read, N. The character is not actually read, it is only looked at and
is still available for subsequent input.
These predicates are called peek_code/(1-2) in the ISO Prolog standard.
get(?N )
get(+Stream,?N )
Same as get0/2, except N is the character code of the next character that is not a
layout-char (see [Token String], page 398) read from Stream.
skip(+N )
skip(+Stream,+N )
Skips just past the next character code N from Stream. N may be an arithmetic
expression.
skip_line
skip_line(+Stream)
Skips just past the next LFD from Stream.
put(+N ) [ISO]
put(+Stream,+N ) [ISO]
Character code N is output onto Stream. N may be an arithmetic expression.
These predicates are called put_code/(1-2) in the ISO Prolog standard.
tab(+N )
tab(+Stream,+N )
N spaces are output onto Stream. N may be an arithmetic expression.
The above predicates are the ones which are the most commonly used, as they can refer to any
streams. The predicates which follow always refer to the standard input and output streams.
They are provided for compatibility DEC-10 character I/O, and are actually redundant and easily
recoded in terms of the above predicates.
Same as nl(user).
ttyflush Same as flush_output(user).
ttyget0(?N )
Same as get0(user, N ).
ttyget(?N )
Same as get(user, N ).
ttyput(+N )
Same as put(user, N ).
ttyskip(+N )
Same as skip(user, N ).
ttytab(+N )
Same as tab(user, N ).
ttynl
Chapter 7: Built-In Predicates
89
7.1.5 Stream I/O
The following predicates manipulate streams. Character and line counts are maintained per stream.
All streams connected to the terminal, however, share the same set of counts. For example, writing
to user_output will advance the counts for user_input, if both are connected to the terminal.
Bidirectional streams use the same counters for input and output.
open(+FileName,+Mode,-Stream) [ISO]
open(+FileName,+Mode,-Stream,+Options )
[ISO]
If FileName is a valid le name, the le is opened in mode Mode (invoking the UNIX
function fopen) and the resulting stream is unied with Stream. Mode is one of:
read
Open the le for input.
write
Open the le for output. The le is created if it does not already exist, the
le will otherwise be truncated.
append
Open the le for output. The le is created if it does not already exist, the
le will otherwise be appended to.
If FileName is an integer, it is assumed to be a le descriptor passed to Prolog from
C. The le descriptor is connected to a Prolog stream (invoking the POSIX function
fdopen) which is unied with Stream.
Options is a list of zero or more of:
type(+T ) Species whether the stream is a text or binary stream. Default is text.
close(+X ) [ISO]
If X is a stream, the stream is closed. If X is the name of a le opened by see/1 or
tell/1, the corresponding stream is closed.
absolute_file_name(+RelativeName,-AbsoluteName )
True if RelativeName can be expanded to an absolute le name (an atom) AbsoluteName, according to the lename syntax rules (see Section 7.1 [Input Output], page 71).
This predicate will rst search for a le with the sux `.pl' added to the name given
as an argument. If this fails, it will look for a le with no extra sux added. If a le is
found AbsoluteName is its absolute le name is returned. Otherwise, AbsoluteName
is a valid expansion of RelativeName,
absolute_file_name/2 does not produce alternative expansions via backtracking.
If RelativeName is user, then AbsoluteName is also unied with user; this \lename"
stands for the standard input or output stream, depending on context.
Variants of this predicate are used by all predicates that refer to lenames for resolving
these. Predicates that load code require that the specied le exist, possibly with a
`.pl' or `.ql' extension.
file_search_path(Alias,?Expansion)
user:file_search_path(Alias,?Expansion)
A hook predicate. This predicate species a set of possible le name expansions to
be tried when a le specication of the form Alias (Name ) is used in a predicate that
accepts a lename argument.
Alias and Name must be atoms. There are two possible cases for Expansion.
90
SICStus Prolog
Expansion is an atom. In this case, the lename expands to Expansion/Name,
which is subject to further expansion if the resulting atom begins with `~' or `$'
(see Section 7.1 [Input Output], page 71).
Expansion is a compound term NewAlias(NewName). In this case, the lename
expands to NewAlias (NewName /Name ), which is subject to further expansion
via a recursive call to user:file_search_path/2.
Since it is possible to have multiple denitions for the same alias, predicates such as
compile/1 may have to explore several alternative expansions before they locate the
le to compile. Predicates such as compile/1 look for a le with a `.pl' sux as well
as for a le without the sux. load/1 looks for a le with a `.ql' sux.
Aliases are useful in writing portable code, as they minimize the number of places
where a program has to be changed if the program or its environment is moved to a
dierent location.
file_search_path/2 is always called in the user module. The predicate exists as a
dynamic, multile predicate at startup with a single clause dening an expansion for
the library/1 alias:
file_search_path(library,Path) :- library_directory(Path).
library_directory(?Directory )
user:library_directory(?Directory )
A hook predicate. This predicate species a set of directories to be searched when a le
specication of the form library(Name ) is used. The predicate exists as a dynamic,
multile predicate at startup.
Directories to be searched may be added by using asserta/1 or assertz/1 (see Section 7.9 [Modify Prog], page 113):
| ?- assertz(user:file_search_path(home, '$HOME')).
| ?- assertz(user:file_search_path(demo, home('prolog/demo'))).
| ?- assertz(user:library_directory(home('prolog/lib'))).
With
these
declarations,
the le name demo(mydemo) would expand to '$HOME/prolog/demo/mydemo', where
'$HOME' is interpreted as an environment variable (the user's home directory). File
names of the form library(mymodule) would be looked up in '$HOME/prolog/lib' if
they cannot be found in the default library directory.
current_input(?Stream) [ISO]
Stream is the current input stream. The current input stream is also accessed by the
C variable SP_curin.
current_output(?Stream) [ISO]
Stream is the current output stream. The current output stream is also accessed by
the C variable SP_curout.
current_stream(?FileName,?Mode,?Stream)
Stream is a stream which was opened in mode Mode and which is connected to the
absolute le name Filename (an atom) or to the le descriptor Filename (an integer). This predicate can be used for enumerating all currently open streams through
backtracking.
Chapter 7: Built-In Predicates
set_input(+Stream)
91
[ISO]
Sets the current input stream to Stream.
set_output(+Stream) [ISO]
Sets the current output stream to Stream.
flush_output [ISO]
flush_output(+Stream) [ISO]
Flushes all internally buered characters for Stream to the operating system.
open_null_stream(-Stream)
Opens an output stream. Everything written to this stream will be thrown away.
character_count(+Stream,?N )
N is the number of characters read/written on stream Stream.
line_count(+Stream,?N )
N is the number of lines read/written on stream Stream.
line_position(+Stream,?N )
N is the number of characters read/written on the current line of Stream.
stream_position(+Stream,?Position)
Position is a term representing the current position of Stream. The relative order of
stream position terms can be tested with standard term comparison predicates such as
compare/3, but you should not otherwise rely on their internal representation. This
operation is available for any Prolog stream.
set_stream_position(+Stream,+Position) [ISO]
Position is a term representing a new position of Stream, which is then set to the new
position. This operation is only available for Prolog streams connected to \seekable
devices" (disk les, usually).
seek(+Stream,+Oset,+Method,-NewLocation)
True if the stream Stream can be set to the byte oset Oset relative to Method, and
NewLocation is the new byte oset from the beginning of the le after the operation.
Method must be one of:
bof
Seek from the beginning of the le stream.
current
Seek from the current position of the le stream.
eof
Seek from the end of the le stream.
This operation is only available for Prolog streams connected to \seekable devices"
(disk les, usually) and is an interface to the stdio functions fseek and ftell.
at_end_of_stream [ISO]
at_end_of_stream(+Stream) [ISO]
The end of stream has been reached for Stream. An input stream reaches end of stream
when all characters (except EOF) of the stream have been read. These predicates
peek ahead for next input character if there is no character available on the buer
of Stream. Unless the stream is to be treated as connected to the terminal (see SP_
force_interactive, Section 8.8.1 [Initializing the Prolog Engine], page 151), a stream
remains at end of stream after EOF has been read, and any further attempt to read from
the stream will raise an existence error (see Section 7.5 [Exception], page 101).
92
SICStus Prolog
at_end_of_line
at_end_of_line(+Stream)
The end of stream or end of line has been reached for Stream. An input stream reaches
end of line when all the characters except LFD of the current line have been read. These
predicates peek ahead for next input character if there is no character available on the
buer of Stream.
fileerrors
Undoes the eect of nofileerrors/0.
nofileerrors
After a call to this predicate, failure to locate or open a le will cause the operation to
fail instead of the default action, which is to raise an exception with an error message.
stream_select(+Streams,+TimeOut,-ReadStreams )
The list of streams in Streams is checked for readable characters. A stream can be any
stream associated with an I/O descriptor. The list ReadStreams returns the streams
with readable data. If TimeOut is instantiated to off, the predicate waits until something is available. If TimeOut is S:U the predicate waits at most S seconds and U
microseconds. Both S and U must be integers >=0. If there is a timeout, ReadStreams
is [].
Not available in Muse. Not available in operating systems that do not support the
system() system call.
stream_interrupt(?Stream,?OldHandler,?NewHandler )
Installs NewHandler as an interrupt-handler which is invoked when something is readable on Stream. OldHandler is the current interrupt handler Stream must be associated
with an I/O descriptor. Interrupt handlers are specied as atoms. The atom off indicates that the interrupt mechanism is turned o for Stream. Any other atom is
the name of a predicate invoked when something is readable on Stream. The handler
predicate has one argument, the stream that is readable. For example,
stream_interrupt(Stream, _, int_handler).
will enable the interrupt mechanism. Given the predicate
int_handler(Stream) :read(Stream, Data),
write(Data), nl.
the term read from Stream will be written to the current output. Note: there is no
guarantee that a complete Prolog term is available yet. If not, read/2 will suspend as
usual.
Not available in Muse. Not available in operating systems that do not provide the
ability to generate signals when new data becomes available on a le descriptor.
Chapter 7: Built-In Predicates
93
7.1.6 DEC-10 Prolog File I/O
The following predicates manipulate les.
see(+File )
The le File becomes the current input stream. File may be a stream previously opened
by see/1 or a lename. If it is a lename, the following action is taken: If there is
a stream opened by see/1 associated with the same le already, then it becomes the
current input stream. Otherwise, the le File is opened for input and made the current
input stream.
seeing(?FileName )
FileName is unied with the name of the current input le, if it was opened by see/1,
with the current input stream, if it is not user_input, otherwise with user.
seen
Closes the current input stream, and resets it to user_input.
tell(+File )
The le File becomes the current output stream. File may be a stream previously
opened by tell/1 or a lename. If it is a lename, the following action is taken:
If there is a stream opened by tell/1 associated with the same le already, then it
becomes the current output stream. Otherwise, the le File is opened for output and
made the current output stream.
telling(?FileName )
FileName is unied with the name of the current output le, if it was opened by tell/1,
with the current output stream, if it is not user_output, otherwise with user.
told
Closes the current output stream, and resets it to user_output.
7.1.7 An Example
Here is an example of a common form of le processing:
process_file(F) :seeing(OldInput),
see(F),
repeat,
read(T),
process_term(T),
T == end_of_file,
!,
seen,
see(OldInput).
% Open file F
% Read a term
% Process it
% Loop back if not at end of file
% Close the file
The above is an example of a repeat loop. Nearly all sensible uses of repeat/0 follow the above
pattern. Note the use of a cut to terminate the loop.
94
SICStus Prolog
7.2 Arithmetic
Arithmetic is performed by built-in predicates which take as arguments arithmetic expressions and
evaluate them. An arithmetic expression is a term built from numbers, variables, and functors that
represent arithmetic functions. At the time of evaluation, each variable in an arithmetic expression
must be bound to a non-variable expression. An expression evaluates to a number, which may be
an integer or a oat.
The range of integers is [-2^2147483616, 2^2147483616). Thus for all practical purposes, the
range of integers can be considered innite.
The range of oats is the one provided by the C double type, typically [4.9e-324, 1.8e+308]
(plus or minus).
Only certain functors are permitted in an arithmetic expression. These are listed below, together
with an indication of the functions they represent. X and Y are assumed to be arithmetic expressions. Unless stated otherwise, the arguments of an expression may be any numbers and its value
is a oat if any of its arguments is a oat, otherwise the value is an integer. Any implicit coercions
are performed with the integer/1 and float/1 functions.
+(X )
-X
X +Y
X-Y
X *Y
X /Y
X //Y
X mod Y
The value is X.
The value is the negative of X.
The value is the sum of X and Y.
The value is the dierence of X and Y.
The value is the product of X and Y.
The value is the oat quotient of X and Y.
The value is the integer quotient of X and Y.
The value is the integer remainder after dividing X by Y, i.e.
integer(Y )*(X //Y ).
integer(X )
integer(X )-
The value is the closest integer between X and 0, if X is a oat, otherwise to X itself.
float(X ) The value is the oat equivalent of X, if X is an integer, otherwise to X itself.
X /\Y
The value is the bitwise conjunction of the integers X and Y.
X \/Y
The value is the bitwise disjunction of the integers X and Y.
X #Y
The value is the bitwise exclusive or of the integers X and Y.
\(X )
The value is the bitwise negation of the integer X.
X <<Y
The value is the integer X shifted left by Y places.
X>>Y
The value is the integer X shifted right by Y places.
[X ]
A list of just one number X evaluates to X. Since a quoted string is just a list of
integers, this allows a quoted character to be used in place of its character code; e.g.
"A" behaves within arithmetic expressions as the integer 65.
Chapter 7: Built-In Predicates
95
SICStus Prolog also includes an extra set of functions listed below. These may not be supported
by other Prologs. All trigonometric and transcendental functions take oat arguments and deliver
oat values. The trigonometric functions take arguments or deliver values in radians.
abs(X )
gcd(X,Y )
min(X,Y )
max(X,Y )
The value is the absolute value of X.
The value is the greatest common divisor of the two integers X and Y.
The value is the lesser value of X and Y.
The value is the greater value of X and Y.
msb(X )
The value is the most signicant bit position of the integer X. It is equivalent to, but
more ecient than, integer(log(2,X)).
round(X ) The value is the oat that is the closest integral value to X. If X is exactly half-way
between two integers, it must be rounded to the closest even integral value.
truncate(X )
The value is the oat that is the closest integer between X and 0.
floor(X ) The value is the oat that is the greatest integral value less or equal to X.
ceiling(X )
The value is the oat that is the least integral value greater or equal to X.
sin(X )
The value is the sine of X.
cos(X )
The value is the cosine of X.
tan(X )
The value is the tangent of X.
cot(X )
The value is the cotangent of X.
sinh(X ) The value is the hyperbolic sine of X.
cosh(X ) The value is the hyperbolic cosine of X.
tanh(X ) The value is the hyperbolic tangent of X.
coth(X ) The value is the hyperbolic cotangent of X.
asin(X ) The value is the arc sine of X.
acos(X ) The value is the arc cosine of X.
atan(X ) The value is the arc tangent of X.
atan2(X,Y )
The value is the four-quadrant arc tangent of X and Y.
acot(X ) The value is the arc cotangent of X.
acot2(X,Y )
The value is the four-quadrant arc cotangent of X and Y.
asinh(X ) The value is the hyperbolic arc sine of X.
96
SICStus Prolog
acosh(X )
The value is the hyperbolic arc cosine of X.
atanh(X ) The value is the hyperbolic arc tangent of X.
acoth(X ) The value is the hyperbolic arc cotangent of X.
sqrt(X ) The value is the square root of X.
log(X )
The value is the natural logarithm of X.
log(Base,X )
The value is the logarithm of X in the base Base.
exp(X )
The value is the natural exponent of X.
exp(X,Y )
The value is X raised to the power of Y.
inf
The value is innity as dened in the IEEE standard.
nan
The value is not-a-number as dened in the IEEE standard.
Variables in an arithmetic expression which is to be evaluated may be bound to other arithmetic
expressions rather than just numbers, e.g.
evaluate(Expression, Answer) :- Answer is Expression.
| ?- evaluate(24*9, Ans).
Ans = 216 ?
yes
Arithmetic expressions, as described above, are just data structures. If you want one evaluated
you must pass it as an argument to one of the built-in predicates listed below. Note that it only
evaluates one of its arguments, whereas all the comparison predicates evaluate both of theirs. In
the following, X and Y stand for arithmetic expressions, and Z for some term.
Z
X
X
X
X
X
X [ISO]
X, which must be an arithmetic expression, is evaluated and the result is unied with
Z.
=:= Y [ISO]
The numeric values of X and Y are equal.
=\= Y [ISO]
The numeric values of X and Y are not equal.
< Y [ISO]
The numeric value of X is less than the numeric value of Y.
> Y [ISO]
The numeric value of X is greater than the numeric value of Y.
=< Y [ISO]
The numeric value of X is less than or equal to the numeric value of Y.
is
Chapter 7: Built-In Predicates
X
>=
97
Y [ISO]
The numeric value of X is greater than or equal to the numeric value of Y.
7.3 Comparison of Terms
These built-in predicates are meta-logical. They treat uninstantiated variables as objects with
values which may be compared, and they never instantiate those variables. They should not be
used when what you really want is arithmetic comparison (see Section 7.2 [Arithmetic], page 94)
or unication.
The predicates make reference to a standard total ordering of terms, which is as follows:
Variables, by age (oldest rst|the order is not related to the names of variables).
Floats, in numeric order (e.g. -1.0 is put before 1.0).
Integers, in numeric order (e.g. -1 is put before 1).
Atoms, in alphabetical (i.e. character code) order.
Compound terms, ordered rst by arity, then by the name of the principal functor, then by
age for mutables and by the arguments in left-to-right order for other terms. Recall that lists
are equivalent to compound terms with principal functor ./2.
For example, here is a list of terms in standard order:
[ X, -1.0, -9, 1, fie, foe, X = Y, foe(0,2), fie(1,1,1) ]
Note: the standard order is only well-dened for nite (acyclic) terms. There are innite (cyclic)
terms for which no order relation holds. Furthermore, blocking goals (see Section 2.3 [Procedural],
page 34) on variables or modifying their attributes (see Chapter 14 [Attributes], page 193) does
not preserve their order.
These are the basic predicates for comparison of arbitrary terms:
Term1 == Term2 [ISO]
The terms currently instantiating Term1 and Term2 are literally identical (in particular, variables in equivalent positions in the two terms must be identical). For example,
the query
| ?- X == Y.
fails (answers `no') because X and Y are distinct uninstantiated variables. However,
the query
| ?- X = Y, X == Y.
succeeds because the rst goal unies the two variables (see Section 7.17 [Misc Pred],
page 124).
Term1 \== Term2 [ISO]
The terms currently instantiating Term1 and Term2 are not literally identical.
98
SICStus Prolog
Term1 @< Term2 [ISO]
The term Term1 is before the term Term2 in the standard order.
Term1 @> Term2 [ISO]
The term Term1 is after the term Term2 in the standard order.
Term1 @=< Term2 [ISO]
The term Term1 is not after the term Term2 in the standard order.
Term1 @>= Term2 [ISO]
The term Term1 is not before the term Term2 in the standard order.
Some further predicates involving comparison of terms are:
?=(?X,?Y )
X and Y are either syntactically identical or syntactically non-uniable.
compare(?Op,?Term1,?Term2 )
The result of comparing terms Term1 and Term2 is Op, where the possible values for
Op are:
=
if Term1 is identical to Term2,
<
if Term1 is before Term2 in the standard order,
>
if Term1 is after Term2 in the standard order.
Thus compare(=,Term1,Term2) is equivalent to Term1 == Term2.
sort(+List1,?List2 )
The elements of the list List1 are sorted into the standard order (see Section 7.3 [Term
Compare], page 97) and any identical elements are merged, yielding the list List2. (The
time and space complexity of this operation is at worst O(N lg N) where N is the length
of List1.)
keysort(+List1,?List2 )
The list List1 must consist of items of the form Key-Value. These items are sorted into
order according to the value of Key, yielding the list List2. No merging takes place.
This predicate is stable, i.e. if K-A occurs before K-B in the input, then K-A will occur
before K-B in the output. (The time and space complexity of this operation is at worst
O(N lg N) where N is the length of List1.)
Chapter 7: Built-In Predicates
99
7.4 Control
+P , +Q
[ISO]
P and Q.
+P ; +Q [ISO]
P or Q.
! [ISO]
See Section 2.5 [Cut], page 36.
\+ +P [ISO]
Fails if the goal P has a solution, and succeeds otherwise. This is not real negation (\P
is false"), but a kind of pseudo-negation meaning \P is not provable". It is dened as
if by
\+(P ) :- P, !, fail.
\+(_).
No cuts are allowed in P.
Remember that with prex operators such as this one it is necessary to be careful about
spaces if the argument starts with a (. For example:
| ?- \+ (P,Q ).
is this operator applied to the conjunction of P and Q, but
| ?- \+(P,Q ).
would require a predicate \+ /2 for its solution. The prex operator can however be
written as a functor of one argument; thus
| ?- \+((P,Q )).
is also correct.
+P -> +Q ; +R [ISO]
Analogous to
if P then Q else R
and dened as if by
(P -> Q ; R) :- P, !, Q.
(P -> Q ; R) :- R.
except the scope of any cut in Q or R extends beyond the if-then-else construct. No
cuts are allowed in P.
Note that this form of if-then-else only explores the rst solution to the goal P.
Note also that the ; is not read as a disjunction operator in this case; instead, it is
part of the if-then-else construction.
The precedence of -> is less than that of ; (see Section 2.6 [Operators], page 37), so
the expression is read as
;(->(P,Q ),R)
+P -> +Q [ISO]
When occurring as a goal, this construction is read as equivalent to
(P -> Q ; fail)
100
SICStus Prolog
if(+P,+Q,+R)
Analogous to
if P then Q else R
but diers from P -> Q ; R in that if(P, Q, R) explores all solutions to the goal P.
There is a small time penalty for this|if P is known to have only one solution of
interest, the form P -> Q ; R should be preferred.
No cuts are allowed in P.
otherwise
true [ISO]
These always succeed. Use of otherwise/0 is discouraged, because it is not as portable
as true/0, and because the former may suggest a completely dierent semantics than
the latter.
false
fail [ISO]
These always fail. Use of false/0 is discouraged, because it is not as portable as
fail/0, and because the latter has a more procedural avor to it.
repeat [ISO]
Generates an innite sequence of backtracking choices. In sensible code, repeat/0 is
hardly ever used except in repeat loops. A repeat loop has the structure
Head :...
save (OldState),
repeat,
generate (Datum),
action(Datum),
test(Datum),
!,
restore (OldState),
...
The purpose is to repeatedly perform some action on elements which are somehow
generated, e.g. by reading them from a stream, until some test becomes true. Usually,
generate, action, and test are all determinate. Repeat loops cannot contribute to the
logic of the program. They are only meaningful if the action involves side-eects.
The only reason for using repeat loops instead of a more natural tail-recursive formulation is eciency: when the test fails back, the Prolog engine immediately reclaims
any working storage consumed since the call to repeat/0.
call(:Term) [ISO]
incore(:Term)
:Term
If Term is instantiated to a term which would be acceptable as the body of a clause,
then the goal call(Term) is executed exactly as if that term appeared textually in its
place, except that any cut (!) occurring in Term only cuts alternatives in the execution
of Term. Use of incore/1 is not recommended.
If Term is not instantiated as described above, an error message is printed and the call
fails.
Chapter 7: Built-In Predicates
101
call_cleanup(:Goal,:Cleanup )
This construction can be used to ensure that Cleanup is executed as soon as Goal has
completed execution, no matter how it nishes. In more detail:
When call_cleanup/2 with a continuation C is called or backtracked into, rst Goal
is called or backtracked into. Then there are four possibilities:
1. Goal succeeds deterministically, possibly leaving some blocked subgoals. Cleanup
is executed with continuation C.
2. Goal succeeds with some alternatives outstanding. Execution proceeds to C. If a
cut that removes the outstanding alternatives is encountered, Cleanup is executed
with continuation to proceed after the cut. Also, if an exception E that will be
caught by an ancestor of the call_cleanup/2 Goal is raised, Cleanup is executed
with continuation raise_exception(E ).
3. Goal fails. Cleanup is executed with continuation fail.
4. Goal raises an exception E. Cleanup is executed with continuation raise_
exception(E ).
In a typical use of call_cleanup/2, Cleanup succeeds deterministically after performing some side-eect; otherwise, unexpected behavior may result.
Note that the Prolog top level operates as a read-execute-fail loop, which backtracks
into or cuts the query when the user types ; or RET respectively. Also, the predicates
halt/0, abort/0, and reinitialise/0 are implemented in terms of exceptions. All
of these circumstances can trigger the execution of Cleanup.
7.5 Error and Exception Handling
The two built-in predicates on_exception/3 and raise_exception/1 are used to alter the control
ow to meet exception and error conditions. The equivalent of a raise_exception/1 is also
executed by the built-in predicates when errors occur.
on_exception(?Pattern,:ProtectedGoal,:Handler ) [ISO]
raise_exception(+Exception) [ISO]
on_exception/3 calls ProtectedGoal. If this succeeds or fails, so does the call to
on_exception/3. If however, during the execution of ProtectedGoal, there is a call
to raise_exception(Exception), then Exception is copied and the stack is unwound
back to the call to on_exception/3, whereupon the copy of Exception is unied with
Pattern. If this unication succeeds, then on_exception/3 calls the goal Handler in
order to determine the success or failure of on_exception/3. Otherwise, the stack
keeps unwinding, looking for an earlier invocation of on_exception/3. Exception may
be any term.
Certain built-in and library predicates rely on the exception mechanism, so it is usually
a bad idea to let Pattern be a variable, matching any exception. If it must be a variable,
the Handler should examime the exception and pass it on if it is not relevant to the
current invocation.
102
SICStus Prolog
Instead of the above two predicates, the ISO Prolog standard prescribes the following
two. They are functionality equivalent to the above two predicates, but beware of the
dierent argument order:
catch(Goal, Catcher, Recovery) :on_exception(Catcher, Goal, Recovery).
throw(Ball) :raise_exception(Ball).
In a Development System, any previously uncaught exception is caught and an appropriate error
message is printed before returning to the top level. In recursive calls to Prolog from C, uncaught
exceptions are returned back to C instead. The printing of these and most other messages in a
Development System is handled by the predicate print_message/2. The behavior of this predicate
can be overridden by dening user:portray_message/2, so as to suppress or alter the format of
certain messages. These predicates work as follows:
print_message(+Severity, +Message )
Hookable. Most messages from the system are printed by calling this predicate. Before
anything is printed, however, print_message/2 calls user:portray_message/2 with
the same arguments, so as to give the user a means of intercepting the message before it
is actually printed. If user:portray_message/2 succeeds, nothing is printed, otherwise
Message is formatted and printed using the default method.
Message is a term that encodes the message to be printed. The format of message terms
is subject to change, but can be inspected in the le `Bips/msgs.pl' of the SICStus
Prolog distribution. Severity is a term denoting the severity of the message, and is one
of:
force(Severity )
Message should be printed without calling the user:portray_message/2
hook. This is useful if user:portray_message/2 has intercepted the message, and now wants to print a reformatted version of it using print_
message/2.
error
Message is an uncaught exception. The execution will normally be aborted
and return to the top level. Syntax errors and exceptions that occur while
loading les do not necessarily abort the execution, however.
warning
Message is a warning (e.g. singleton variables).
informational
Message provides information e.g. about les being loaded.
help
Message is normally a response to a query.
portray_message(+Severity, +Message )
user:portray_message(+Severity, +Message )
A hook predicate. Called by print_message/2 before printing the message. If this
succeeds, the default message for printing Message is overridden, and nothing more is
printed.
Chapter 7: Built-In Predicates
103
The built-in predicates may raise exceptions as follows on errors. Usually the goal and the argument
number causing the error are contained in the exception.
instantiation_error(Goal,ArgNo )
Goal was called with insuciently instantiated variables.
type_error(Goal,ArgNo,TypeName,Culprit)
Goal was called with the wrong type of argument(s). TypeName is the expected type
and Culprit what was actually found.
domain_error(Goal,ArgNo,Domain,Culprit)
Goal was called with argument(s) of the right type but with illegal value(s). Domain
is the expected domain and Culprit what was actually found.
existence_error(Goal,ArgNo,ObjectType,Culprit,Reserved )
Something does not exist as indicated by the arguments. If the unknown-ag (see
prolog_flag/3) is set to error, this error is raised with ArgNo set to 0 when an
undened predicate is called.
permission_error(Goal,Operation,ObjectType,Culprit,Reserved )
The Operation is not permitted on Culprit of the ObjectType.
context_error(Goal,ContextType,CommandType )
The CommandType is not permitted in ContextType.
syntax_error(Goal,Position,Message,Tokens,AfterError )
A syntax error was found when reading a term with read/(1-2). This error is raised
only if the syntax_errors ag (see prolog_flag/3) is set to error.
representation_error(Goal,ArgNo,ErrorType )
A representation error occurs when the program tries to compute some well-dened
value which cannot be represented, such as a compound term with arity > 255.
consistency_error(Goal,Culprit1,Culprit2,Message )
A consistency error occurs when two otherwise valid values or operations have been
specied which are inconsistent with each other.
system_error(Message )
An error occurred while dealing with the operating system.
It is possible to handle a particular kind of existence errors locally: calls to undened predicates.
This can be done by dening clauses for:
unknown_predicate_handler(+Goal,+Module,-NewGoal )
user:unknown_predicate_handler(+Goal,+Module,-NewGoal )
A hook predicate. This predicate is called as a result of a call to an undened predicate.
Goal is bound to the goal of the undened predicate and Module to the module where
the call was made. If this predicate succeeds, Module:NewGoal is called; otherwise,
the action taken is governed by the unknown Prolog ag.
The following example shows an auto-loader for library packages:
104
SICStus Prolog
user:unknown_predicate_handler(Goal, Module, Goal) :functor(Goal, Name, Arity),
require(Module:(Name/Arity)).
7.6 Information about the State of the Program
Lists onto the current output stream all the clauses in the current interpreted program
(in the type-in module; see Section 4.2 [Module Spec], page 48). Clauses listed onto a
le can be consulted back.
listing(:Spec )
Lists all interpreted predicates covered by Spec, which has the same for as for spy/1
(see Section 6.3 [Spy-Point], page 62). For example:
listing
| ?- listing([concatenate/3, reverse, m:go/(2-3), bar:_]).
current_atom(?Atom)
Atom is an atom known to SICStus Prolog. Can be used to enumerate (through
backtracking) all currently known atoms, and return each one as Atom.
current_predicate(?Name,:Head ) [ISO]
current_predicate(?Name,-Head ) [ISO]
Name is the name of a user dened or library predicate, and Head is the most general
goal for that predicate, possibly prexed by a module name. This predicate can be
used to enumerate all user dened or library predicates through backtracking.
predicate_property(:Head,?Property )
predicate_property(-Head,?Property )
Head is the most general goal for an existing predicate, possibly prexed by a module
name, and Property is a property of that predicate, where the possible properties are
one of the atoms built_in (for built-in predicates) or compiled or interpreted
(for user dened predicates) or fd_constraint for FD predicates see Section 30.9
[Dening Primitive Constraints], page 290.
the atom dynamic for predicates that have been declared dynamic (see Section 5.2.2
[Dynamic Declarations], page 54),
the atom multifile for predicates that have been declared multile (see Section 5.2.1 [Multile Declarations], page 54),
the atom volatile for predicates that have been declared volatile (see Section 5.2.3
[Volatile Declarations], page 55),
one or more terms (block Term) for predicates that have block declarations (see
Section 5.2.4 [Block Declarations], page 55),
the atom exported or terms imported_from(ModuleFrom) for predicates exported or imported from modules (see Chapter 4 [Module Intro], page 47),
the term (meta_predicate Term) for predicates that have meta-predicate declarations (see Section 4.6 [Meta Decl], page 50).
in Muse only, the atom sync for predicates that perform side eects that must be
synchronized to preserve standard semantics.
Chapter 7: Built-In Predicates
105
in Muse only, the atom parallel for predicates that may be executed in parallel.
This predicate can be used to enumerate all existing predicates and their properties
through backtracking.
current_module(?Module )
Module is a module in the system. It can be used to backtrack through all modules
present in the system.
current_module(?Module, ?File )
Module is the module dened in File.
module(+Module )
The type-in module is set to Module.
prolog_flag(+FlagName,?OldValue,?NewValue )
OldValue is the value of the Prolog ag FlagName, and the new value of FlagName is
set to NewValue. The possible Prolog ag names and values are:
agc_margin
argv
An integer Margin. The atoms will be garbage collected when Margin new
atoms have been created since the last atom garbage collection. Initially
10000.
A read-only ag. The value is a list of atoms of the program arguments supplied when the current SICStus Prolog process was started. For example,
if SICStus Prolog were invoked with:
% sicstus -a hello world 2001
then the value will be [hello,world,'2001'].
compiling
Governs the mode in which compile/1 and fcompile/1 operate (see Chapter 5 [Load Intro], page 51).
compactcode
fastcode
Compilation produces byte-coded abstract instructions (the
default).
Compilation produces native machine instructions. Currently
only available for 680x0, Sparc, and MIPS platforms.
profiledcode
Compilation produces byte-coded abstract instructions instrumented to produce execution proling data.
debugcode
debugging
Compiling is replaced by consulting.
Corresponds to the predicates debug/0, nodebug/0, trace/0, notrace/0,
zip/0, nozip/0 (see Section 7.14 [Debug Pred], page 120).
trace
Turns on trace mode.
debug
Turns on the debugger.
106
SICStus Prolog
Turns o trace mode and the debugger (the default).
(This ag is not available in Runtime Systems.)
off
character_escapes
on or off.
If this ag is on, a backslash occurring inside integers in `0''
notation or inside quoted atoms or strings has special meaning, and indicates the start of an escape sequence (see [Escape Sequences], page 401).
This ag is relevant when reading as well as when writing terms, and is
initially on.
fileerrors
or off. Turns raising of exception on le errors on or o. Equivalent to fileerrors/0 and nofileerrors/0, respectively (see Section 7.1.5
[Stream Pred], page 89). Initially on.
on or off. Turns garbage collection of the global stack on or o. Initially
on.
on
gc
gc_margin
gc_trace
Margin: Number of kilobytes. If less than Margin kilobytes are reclaimed
in a garbage collection of the global stack then the size of the global stack
should be increased. Also, no garbage collection is attempted unless the
global stack is at least Margin kilobytes. Initially 500.
Governs global stack garbage collection trace messages.
verbose
Turn on verbose tracing of garbage collection.
terse
Turn on terse tracing of garbage collection.
off
Turn o tracing of garbage collection (the default).
redefine_warnings
on or off.
Enable or disable warning messages when a predicate is being
redened from a dierent le than its previous denition.
imported to the user module and it was previously locally dened.
redened locally and it was previously imported.
imported to the user module from another module than it was previously imported from.
Initially on. (This warning is always disabled in Runtime Systems.)
single_var_warnings
on or off.
unknown
Enable or disable warning messages when a clause containing
variables not beginning with _ occurring once only is compiled or consulted.
Initially on.
Corresponds to the predicate unknown/2 (see Section 7.14 [Debug Pred],
page 120).
trace
Causes calls to undened predicates to be reported and the debugger to be entered at the earliest opportunity. (This setting
is not possible in Runtime Systems.)
Chapter 7: Built-In Predicates
fail
error
107
Causes calls to such predicates to fail.
Causes calls to such predicates to raise an exception (the default). See Section 7.5 [Exception], page 101.
syntax_errors
Controls what action is taken upon syntax errors in read/(1-2).
dec10
The syntax error is reported and the read is repeated.
error
An exception is raised. See Section 7.5 [Exception], page 101.
(the default).
fail
The syntax error is reported and the read fails.
quiet
The read quietly fails.
system_type
A read-only ag. The value is development in Development Systems and
runtime in Runtime Systems.
typein_module
Permitted values are atoms. Controls the current type-in module (see Section 4.2 [Module Spec], page 48). Corresponds to the predicate module/1.
user_input
Permitted values are any stream opened for reading. Controls which stream
is referenced by user_input and SP_stdin. It is initially set to a stream
connected to UNIX stdin.
user_output
Permitted values are any stream opened for writing. Controls which stream
is referenced by user_output and SP_stdout. It is initially set to a stream
connected to UNIX stdout.
user_error
version
Permitted values are any stream opened for writing. Controls which stream
is referenced by user_error and SP_stderr. It is initially set to a stream
connected to UNIX stderr.
A read-only ag. The value is an atom containing the banner text displayed on startup and reinitialization, such as 'SICStus 3 #0: Wed Mar 15
12:29:29 MET 1995'.
toplevel_print_options
The value is a list of options for write_term/3 (see Section 7.1.3
[Term I/O], page 80), to be used when the top level displays variable bindings, answer constraints, and uncaught exceptions. Not available in Runtime Systems. The initial value is
[quoted(true),numbervars(true),portrayed(true),max_depth(10)].
108
SICStus Prolog
debugger_print_options
The value is a list of options for write_term/3 (see Section 7.1.3 [Term
I/O], page 80), to be used in the debugger's messages. Not available in
Runtime
Systems.
The
initial
value
is
[quoted(true),numbervars(true),portrayed(true),max_depth(10)].
source_info
on or off.
If on while code is being loaded, information about line numbers
and lenames are stored with the loaded code. If on while debugging, this
information is used to display the source code location while prompting for
a debugger command. Initially off. Currently, this facility is only useful
with the GNU Emacs interface.
prolog_flag(?FlagName,?Value )
Value is the current value of the Prolog ag FlagName. Can be used to enumerate all
Prolog ags and their values by backtracking.
prolog_load_context(?Key,?Value )
This predicate gives access to context variables during compilation and loading of
Prolog les. It unies Value with the value of the variable identied by Key. Possible
keys are:
file
The absolute path name of the le being compiled. During loading of a
ql-le, the corresponding source le name is returned.
directory
module
stream
The absolute path name of the directory of the le being compiled/loaded.
The source module (see Section 4.5 [Meta Exp], page 49). This is useful
for example if you are dening clauses for user:term_expansion/(2,4)
and need to access the source module at compile time.
The stream being compiled or loaded from.
term_position
A term representing the position of the last clause read (see Section 7.1.5
[Stream Pred], page 89).
statistics
Displays on the standard error stream statistics relating to memory usage, run time,
garbage collection of the global stack and stack shifts.
statistics(?Key,?Value )
This allows a program to gather various execution statistics. For each of the possible
keys Key, Value is unied with a list of values, as follows:
global_stack
[size
used,free ]
This refers to the global stack, where compound terms are stored. The
values are gathered before the list holding the answers is allocated.
Chapter 7: Built-In Predicates
109
local_stack
[size
trail
choice
used,free ]
This refers to the local stack, where recursive predicate environments are
stored.
[size used,free ]
This refers to the trail stack, where conditional variable bindings are
recorded.
[size used,free ]
This refers to the choicepoint stack, where partial states are stored for
backtracking purposes.
core
memory
[size
heap
program
[size
runtime
walltime
used,0]
These refer to the amount of memory actually allocated by the process.
used,0]
These refer to the amount of memory allocated for compiled and interpreted
clauses, symbol tables, and the like.
[since start of Prolog,since previous statistics ] These refer to CPU time
used while executing, excluding time spent garbage collecting, stack shifting, or in system calls. In Muse, these numbers refer to the worker that
happens to be executing the call to statistics/2, and so normally are
not meaningful.
[since start of Prolog,since previous statistics ] These refer to absolute
time elapsed.
garbage_collection
[no. of GCs,bytes
freed,time spent]
stack_shifts
[no.
atoms
of local shifts,no. of trail shifts,time spent]
[no. of atoms,bytes used,bytes free ]
atom_garbage_collection
[no. of AGCs,bytes
freed,time spent]
Times are in milliseconds, sizes of areas in bytes.
110
SICStus Prolog
7.7 Meta-Logic
The predicates in this section are meta-logical and perform operations that require reasoning about
the current instantiation of terms or decomposing terms into their constituents. Such operations
cannot be expressed using predicate denitions with a nite number of clauses.
var(?X )
[ISO]
Tests whether X is currently uninstantiated (var is short for variable). An uninstantiated variable is one which has not been bound to anything, except possibly another
uninstantiated variable. Note that a structure with some components which are uninstantiated is not itself considered to be uninstantiated. Thus the directive
| ?- var(foo(X, Y )).
always fails, despite the fact that X and Y are uninstantiated.
nonvar(?X ) [ISO]
Tests whether X is currently instantiated. This is the opposite of var/1.
ground(?X )
Tests whether X is completely instantiated, i.e. free of unbound variables. In this
context, mutable terms are treated as nonground, so as to make ground/1 a monotone
predicate.
atom(?X ) [ISO]
Checks that X is currently instantiated to an atom (i.e. a non-variable term of arity 0,
other than a number).
float(?X ) [ISO]
Checks that X is currently instantiated to a oat.
integer(?X ) [ISO]
Checks that X is currently instantiated to an integer.
number(?X ) [ISO]
Checks that X is currently instantiated to a number.
atomic(?X ) [ISO]
Checks that X is currently instantiated to an atom or number.
simple(?X )
Checks that X is currently uninstantiated or instantiated to an atom or number.
compound(?X ) [ISO]
Checks that X is currently instantiated to a term of arity > 0 i.e. a list or a structure.
callable(?X )
Checks that X is currently instantiated to a term valid as a goal i.e. a compound term
or an atom.
is_mutable(?X )
Checks that X is currently instantiated to a mutable term (see Section 7.8 [Modify
Term], page 113).
Chapter 7: Built-In Predicates
111
functor(+Term,?Name,?Arity ) [ISO]
functor(?Term,+Name,+Arity ) [ISO]
The principal functor of term Term has name Name and arity Arity, where Name
is either an atom or, provided Arity is 0, a number. Initially, either Term must be
instantiated, or Name and Arity must be instantiated to, respectively, either an atom
and an integer in [0,255] or an atomic term and 0. In the case where Term is initially
uninstantiated, the result of the call is to instantiate Term to the most general term
having the principal functor indicated.
arg(+ArgNo,+Term,?Arg ) [ISO]
Arg is the argument ArgNo of the compound term Term. The arguments are numbered
from 1 upwards, ArgNo must be instantiated to a positive integer and Term to a
compound term.
+Term =.. ?List [ISO]
?Term =.. +List [ISO]
List is a list whose head is the atom corresponding to the principal functor of Term,
and whose tail is a list of the arguments of Term. e.g.
| ?- product(0, n, n-1) =.. L.
L = [product,0,n,n-1]
| ?- n-1 =.. L.
L = [-,n,1]
| ?- product =.. L.
L = [product]
If Term is uninstantiated, then List must be instantiated either to a list of determinate
length whose head is an atom, or to a list of length 1 whose head is a number. Note
that this predicate is not strictly necessary, since its functionality can be provided by
arg/3 and functor/3, and using the latter two is usually more ecient.
name(+Const,?CharList)
name(?Const,+CharList)
If Const is an atom or number, CharList is a list of the character codes of the characters
comprising the name of Const. e.g.
| ?- name(product, L).
L = [112,114,111,100,117,99,116]
| ?- name(product, "product").
| ?- name(1976, L).
L = [49,57,55,54]
112
SICStus Prolog
| ?- name('1976', L).
L = [49,57,55,54]
| ?- name((:-), L).
L = [58,45]
If Const is uninstantiated, CharList must be instantiated to a list of character codes. If
CharList can be interpreted as a number, Const is unied with that number, otherwise
with the atom whose name is CharList. E.g.
| ?- name(X, [58,45]).
X = :| ?- name(X, ":-").
X = :| ?- name(X, [49,50,51]).
X = 123
Note that there atoms are for which name(Const,CharList) is true, but which will not
be constructed if name/2 is called with Const uninstantiated. One such atom is the
atom '1976'. It is recommended that new programs use atom_chars/2 or number_
chars/2, as these predicates do not have this inconsistency.
atom_chars(+Const,?CharList) [ISO]
atom_chars(?Const,+CharList) [ISO]
The same as name(Const,CharList), but Const is constrained to be an atom.
This predicate is called atom_codes/2 in the ISO Prolog standard.
number_chars(+Const,?CharList) [ISO]
number_chars(?Const,+CharList) [ISO]
The same as name(Const,CharList), but Const is constrained to be a number.
This predicate is called number_codes/2 in the ISO Prolog standard.
copy_term(?Term,?CopyOfTerm) [ISO]
CopyOfTerm is a renaming of Term, such that brand new variables have been substituted for all variables in Term. If any of the variables of Term have goals blocked on
them, the copied variables will have copies of the goals blocked on them as well. Similarly, independent copies are substituted for any mutable terms in term. It behaves as
if dened by:
copy_term(X, Y) :assert('copy of'(X)),
retract('copy of'(Y)).
The implementation of copy_term/2 conserves space by not copying ground subterms.
Chapter 7: Built-In Predicates
113
7.8 Modication of Terms
One of the tenets of logic programming is that terms are immutable objects of the Herbrand
universe, and the only sense in which they can be modied is by means of instantiating non-ground
parts. There are, however, algorithms where destructive assignment is essential for performance.
Although alien to the ideals of logic programming, this feature can be defended on practical grounds.
SICStus Prolog provides an abstract datatype and three operations for ecient backtrackable destructive assignment. In other words, any destructive assignments are transparently undone on
backtracking. Modications that are intended to survive backtracking must be done by asserting or retracting dynamic program clauses instead. Unlike previous releases of SICStus Prolog,
destructive assignment of arbitrary terms is not allowed.
A
mutable
term
is
represented
as
a
compound
terms
with a reserved functor: '$mutable'(Value,Timestamp) where Value is the current value and
Timestamp is reserved for bookkeeping purposes.
Any copy of a mutable term created by copy_term/2, assert, retract, an internal database
predicate, or an all solutions predicate, is an independent copy of the original mutable term. Any
destructive assignment done to one of the copies will not aect the other copy.
The following operations are provided:
create_mutable(+Datum,-Mutable )
Mutable is a new mutable term with initial value Datum. Datum must not be an
unbound variable.
get_mutable(?Datum,+Mutable )
Datum is the current value of the mutable term Mutable.
update_mutable(+Datum,+Mutable )
Updates the current value of the mutable term Mutable to become Datum. Datum
must not be an unbound variable.
is_mutable(?Mutable )
Checks that Mutable is currently instantiated to a mutable term (see Section 7.8 [Modify Term], page 113).
7.9 Modication of the Program
The predicates dened in this section allow modication of dynamic predicates. Dynamic clauses
can be added (asserted) or removed from the program (retracted).
For these predicates, the argument Head must be instantiated to an atom or a compound term,
with an optional module prex. The argument Clause must be instantiated either to a term Head
:- Body or, if the body part is empty, to Head, with an optional module prex. An empty body
part is represented as true.
114
SICStus Prolog
Note that a term Head :- Body must be enclosed in parentheses when it occurs as an argument
of a compound term, as `:-' is a standard inx operator with precedence greater than 1000 (see
Section 2.6 [Operators], page 37), e.g.:
| ?- assert((Head :- Body )).
Like recorded terms (see Section 7.10 [Database], page 115), the clauses of dynamic predicates
have a unique implementation-dened identier. Some of the predicates below have an additional
argument which is this identier. This identier makes it possible to access clauses directly instead
of requiring a normal database (hash-table) lookup.
assert(:Clause )
assert(:Clause,-Ref )
The current instance of Clause is interpreted as a clause and is added to the current
interpreted program. The predicate concerned must currently be dynamic or undened
and the position of the new clause within it is implementation-dened. Ref is a unique
identier of the asserted clause. Any uninstantiated variables in the Clause will be
replaced by new private variables, along with copies of any subgoals blocked on these
variables (see Section 2.3 [Procedural], page 34).
asserta(:Clause ) [ISO]
asserta(:Clause,-Ref )
Like assert/2, except that the new clause becomes the rst clause for the predicate
concerned.
assertz(:Clause ) [ISO]
assertz(:Clause,-Ref )
Like assert/2, except that the new clause becomes the last clause for the predicate
concerned.
clause(:Head,?Body ) [ISO]
clause(:Head,?Body,?Ref )
clause(?Head,?Body,+Ref )
The clause (Head :- Body ) exists in the current interpreted program, and is uniquely
identied by Ref. The predicate concerned must currently be dynamic. At the time of
call, either Ref must be instantiated to a valid identier, or Head must be instantiated
to an atom or a compound term. Thus clause/3 can have two dierent modes of use.
retract(:Clause ) [ISO]
The rst clause in the current interpreted program that matches Clause is erased.
The predicate concerned must currently be dynamic. retract/1 may be used in a
non-determinate fashion, i.e. it will successively retract clauses matching the argument
through backtracking. If reactivated by backtracking, invocations of the predicate
whose clauses are being retracted will proceed unaected by the retracts. This is also
true for invocations of clause/(2-3) for the same predicate. The space occupied by a
retracted clause will be recovered when instances of the clause are no longer in use.
Chapter 7: Built-In Predicates
115
retractall(:Head )
Erases all clauses whose head matches Head, where Head must be instantiated to an
atom or a compound term. The predicate concerned must currently be dynamic. The
predicate denition is retained.
Note: all predicates mentioned above rst look for a predicate that is visible in the module in which
the call textually appears. If no predicate is found, a new dynamic predicate (with no clauses) is
created automatically. During OR-parallel execution, however, predicates must not be dened on
the y like this. It is recommended to declare as dynamic predicates for which clauses will be
asserted.
abolish(:Spec ) [ISO]
abolish(:Name,+Arity )
Erases all clauses of the predicate specied by Spec or Name /Arity. Spec has the same
form as for spy/1 (see Section 6.3 [Spy-Point], page 62) and Name may be prexed by
a module name (see Section 4.2 [Module Spec], page 48).
The predicate denition and all associated information such as spy-points is also erased.
The predicates concerned must all be user dened.
erase(+Ref )
The dynamic clause or recorded term (see Section 7.10 [Database], page 115) whose
implementation-dened identier is Ref is eectively erased from the internal database
or interpreted program.
instance(+Ref,?Term)
A (most general) instance of the dynamic clause or recorded term whose
implementation-dened identier is Ref is unied with Term. Ref must be instantiated to a legal identier.
7.10 Internal Database
The predicates described in this section were introduced in early implementations of Prolog to
provide ecient means of performing operations on large quantities of data. The introduction
of indexed dynamic predicates have rendered these predicates obsolete, and the sole purpose of
providing them is to support existing code. There is no reason whatsoever to use them in new
code.
These predicates store arbitrary terms in the database without interfering with the clauses which
make up the program. The terms which are stored in this way can subsequently be retrieved via
the key on which they were stored. Many terms may be stored on the same key, and they can
be individually accessed by pattern matching. Alternatively, access can be achieved via a special
identier which uniquely identies each recorded term and which is returned when the term is
stored.
116
SICStus Prolog
recorded(?Key,?Term,?Ref )
The internal database is searched for terms recorded under the key Key. These terms
are successively unied with Term in the order they occur in the database. At the same
time, Ref is unied with the implementation-dened identier uniquely identifying the
recorded item. If the key is instantiated to a compound term, only its principal functor
is signicant. If the key is uninstantiated, all terms in the database are successively
unied with Term in the order they occur.
recorda(+Key,?Term,-Ref )
The term Term is recorded in the internal database as the rst item for the key Key,
where Ref is its implementation-dened identier. The key must be given, and only
its principal functor is signicant. Any uninstantiated variables in the Term will be
replaced by new private variables, along with copies of any subgoals blocked on these
variables (see Section 2.3 [Procedural], page 34).
recordz(+Key,?Term,-Ref )
Like recorda/3, except that the new term becomes the last item for the key Key.
current_key(?KeyName,?KeyTerm)
KeyTerm is the most general form of the key for a currently recorded term, and KeyName is the name of that key. This predicate can be used to enumerate in undened
order all keys for currently recorded terms through backtracking.
7.11 Blackboard Primitives
The predicates described in this section store arbitrary terms in a per-module repository known as
the \blackboard". The main purpose of the blackboard is to provide a means for communication
between branches executing in parallel, but the blackboard works equally well during sequential
execution. The blackboard implements a mapping from keys to values. Keys are restricted to being
atoms or integers in the range [-33554432, 33554431], whereas values are arbitrary terms. In
contrast to the predicates described in the previous sections, a given key can map to at most a
single term.
Each Prolog module maintains its own blackboard, so as to avoid name clashes if dierent modules
happen to use the same keys. The \key" arguments of these predicates are subject to module name
expansion, so the module name does not have to be explicitly given unless multiple Prolog modules
are supposed to share a single blackboard.
The predicates below implement atomic blackboard actions. In Muse, these predicates are cavalier
(see Section 3.5 [Programming Considerations], page 45).
bb_put(:Key, +Term)
A copy of Term is stored under Key. Any previous term stored under the same Key is
simply deleted.
bb_get(:Key, ?Term)
If a term is currently stored under Key, a copy of it is unied with Term. Otherwise,
bb_get/2 silently fails.
Chapter 7: Built-In Predicates
117
bb_delete(:Key,
?Term)
If a term is currently stored under Key, the term is deleted, and a copy of it is unied
with Term. Otherwise, bb_delete/2 silently fails.
bb_update(:Key, ?OldTerm, ?NewTerm)
If a term is currently stored under Key and unies with OldTerm, the term is replaced
by a copy of NewTerm. Otherwise, bb_update/3 silently fails. This predicate provides
an atomic swap operation.
The following example illustrates how these primitives may be used to implement a \maxof" predicate that nds the maximum value computed by some non-deterministic goal, which may execute
in parallel. We use a single key max. Note the technique of using bb_update/3 in a repeat-fail
loop, since other execution branches may be competing for updating the value, and we only want
to store a new value if it is greater than the old value.
We assume that Goal does not produce any \false" solutions that would be eliminated by cuts in a
sequential execution. Thus, Goal may need to include redundant checks to ensure that its solutions
are valid, as discussed above.
maxof(Value, Goal, _) :bb_put(max, -1),
call(Goal),
update_max(Value),
fail.
maxof(_, _, Max) :bb_delete(max, Max),
Max > 1.
% initialize max-so-far
update_max(New):repeat,
bb_get(max, Old),
compare(C, Old, New),
update_max(C, Old, New), !.
update_max(<, Old, New) :- bb_update(max, Old, New).
update_max(=, _, _).
update_max(>, _, _).
7.12 All Solutions
When there are many solutions to a problem, and when all those solutions are required to be
collected together, this can be achieved by repeatedly backtracking and gradually building up a list
of the solutions. The following built-in predicates are provided to automate this process.
Note that the Goal argument to the predicates listed below is called as if by call/1 at runtime.
Thus if Goal is complex and if performance is an issue, dene an auxiliary predicate which can
then be compiled, and let Goal call it.
118
SICStus Prolog
setof(?Template,:Goal,?Set)
[ISO]
Read this as \Set is the set of all instances of Template such that Goal is satised, where
that set is non-empty". The term Goal species a goal or goals as in call(Goal ) (see
Section 7.4 [Control], page 99). Set is a set of terms represented as a list of those terms,
without duplicates, in the standard order for terms (see Section 7.3 [Term Compare],
page 97). If there are no instances of Template such that Goal is satised then the
predicate fails.
The variables appearing in the term Template should not appear anywhere else in the
clause except within the term Goal. Obviously, the set to be enumerated should be
nite, and should be enumerable by Prolog in nite time. It is possible for the provable
instances to contain variables, but in this case the list Set will only provide an imperfect
representation of what is in reality an innite set.
If there are uninstantiated variables in Goal which do not also appear in Template,
then a call to this built-in predicate may backtrack, generating alternative values for
Set corresponding to dierent instantiations of the free variables of Goal. (It is to cater
for such usage that the set Set is constrained to be non-empty.) Two instantiations are
dierent i no renaming of variables can make them literally identical. For example,
given the clauses:
likes(bill, cider).
likes(dick, beer).
likes(harry, beer).
likes(jan, cider).
likes(tom, beer).
likes(tom, cider).
the query
| ?- setof(X, likes(X,Y), S).
might produce two alternative solutions via backtracking:
S = [dick,harry,tom],
Y = beer ? ;
S = [bill,jan,tom],
Y = cider ? ;
The query:
| ?- setof((Y,S), setof(X, likes(X,Y), S), SS).
would then produce:
SS = [(beer,[dick,harry,tom]),(cider,[bill,jan,tom])]
Variables occurring in Goal will not be treated as free if they are explicitly bound
within Goal by an existential quantier. An existential quantication is written:
Y ^Q
meaning \there exists a Y such that Q is true", where Y is some Prolog variable.
For example:
| ?- setof(X, Y^(likes(X,Y)), S).
would produce the single result:
Chapter 7: Built-In Predicates
119
S = [bill,dick,harry,jan,tom]
in contrast to the earlier example.
bagof(?Template,:Goal,?Bag ) [ISO]
This is exactly the same as setof/3 except that the list (or alternative lists) returned
will not be ordered, and may contain duplicates. The eect of this relaxation is to save
a call to sort/2, which is invoked by setof/3 to return an ordered list.
?X ^:P
The all solution predicates recognize this as meaning \there exists an X such that P
is true", and treats it as equivalent to P (see Section 7.4 [Control], page 99). The use
of this explicit existential quantier outside the setof/3 and bagof/3 constructs is
superuous and discouraged.
findall(?Template,:Goal,?Bag ) [ISO]
Bag is a list of instances of Template in all proofs of Goal found by Prolog. The
order of the list corresponds to the order in which the proofs are found. The list may
be empty and all variables are taken as being existentially quantied. This means
that each invocation of findall/3 succeeds exactly once, and that no variables in
Goal get bound. Avoiding the management of universally quantied variables can save
considerable time and space.
findall(?Template,:Goal,?Bag,?Remainder )
Same as findall/3, except Bag is the list of solution instances appended with Remainder, which is typically unbound.
7.13 Coroutining
The coroutining facility can be accessed by a number of built-in predicates. This makes it possible
to use coroutines in a dynamic way, without having to rely on block declarations:
when(+Condition,:Goal )
Blocks Goal until the Condition is true, where Condition is a Prolog goal with the
restricted syntax:
nonvar(X )
ground(X )
?=(X,Y )
Condition,Condition
Condition;Condition
For example:
| ?- when(((nonvar(X);?=(X,Y)),ground(T)), process(X,Y,T)).
freeze(?X,:Goal )
Blocks Goal until nonvar(X ) (see Section 7.7 [Meta Logic], page 110) holds. This is
dened as if by:
or
freeze(X, Goal) :- when(nonvar(X), Goal).
120
SICStus Prolog
:- block freeze(-, ?).
freeze(_, Goal) :- Goal.
frozen(-Var,?Goal )
If some goal is blocked on the variable Var, or Var has attributes that can be interpreted
as a goal (see Chapter 14 [Attributes], page 193), then that goal is unied with Goal.
If no goals are blocked, Goal is unied with the atom true. If more than one goal is
blocked, a conjunction is unied with Goal.
dif(?X,?Y )
Constrains X and Y to represent dierent terms i.e. to be non-uniable. Calls to dif/2
either succeed, fail, or are blocked depending on whether X and Y are suciently
instantiated. It is dened as if by:
dif(X, Y) :- when(?=(X,Y), X\==Y).
call_residue(:Goal,?Residue )
The Goal is executed as if by call/1. If after the execution there are still some
subgoals of Goal that are blocked on some variables, then Residue is unied with a list
of VariableSet-Goal pairs, and those subgoals are no longer blocked on any variables.
Otherwise, Residue is unied with the empty list [].
VariableSet is a set of variables such that when any of the variables is bound, Goal gets
unblocked. Usually, a goal is blocked on a single variable, in which case VariableSet is
a singleton.
Goal is an ordinary goal, sometimes module prexed. For example:
| ?- call_residue((dif(X,f(Y)), X=f(Z)), Res).
X = f(Z),
Res = [[Y,Z]-(prolog:dif(f(Z),f(Y)))]
7.14 Debugging
Debugging predicates are not available in Runtime Systems.
unknown(?OldState,?NewState )
debug
OldState is the current state of the \Action on unknown predicates" ag, and sets
the ag to NewState. This ag determines whether or not the system is to catch
calls to undened predicates (see Section 1.6 [Undened Predicates], page 15), when
user:unknown_predicate_handler/3 cannot handle the goal. The possible states of
the ag are:
trace
Causes calls to undened predicates to be reported and the debugger to be
entered at the earliest opportunity.
fail
Causes calls to such predicates to fail.
error
Causes calls to such predicates to raise an exception (the default). See
Section 7.5 [Exception], page 101.
The debugger is switched on in leap mode. See Section 6.2 [Basic Debug], page 61.
Chapter 7: Built-In Predicates
trace
zip
121
The debugger is switched on in creep mode. See Section 6.2 [Basic Debug], page 61.
The debugger is switched on in zip mode. See Section 6.2 [Basic Debug], page 61.
nodebug
notrace
nozip
The debugger is switched o. See Section 6.2 [Basic Debug], page 61.
leash(+Mode )
Leashing Mode is set to Mode. See Section 6.2 [Basic Debug], page 61.
spy :Spec Spy-points are placed on all the predicates given by Spec. See Section 6.3 [Spy-Point],
page 62.
nospy :Spec
Spy-points are removed from all the predicates given by Spec. See Section 6.3 [SpyPoint], page 62.
nospyall Removes all the spy-points that have been set.
debugging
Displays information about the debugger. See Section 6.2 [Basic Debug], page 61.
7.15 Execution Proling
Execution proling is a common aid for improving software performance. The SICStus Prolog
compiler has the capability of instrumenting compiled code with counters which are initially zero
and incremented whenever the ow of control passes a given point in the compiled code. This way
the number of calls, backtracks, choicepoints created, etc., can be counted for the instrumented
predicates, and an estimate of the time spent in individual clauses and disjuncts can be calculated.
Gauge is a graphical user interface for inspecting execution proles. It is available as a library
module (see Chapter 35 [Gauge Intro], page 367).
The original version of the proling package was written by M.M. Gorlick and C.F. Kesselman at
the Aerospace Corporation [Gorlick & Kesselman 87].
Proling is not available in Muse.
Only compiled code can be instrumented. To get an execution prole of a program, the compiler
must rst be told to produce instrumented code. This is done by issuing the directive:
| ?- prolog_flag(compiling,_,profiledcode).
after which the program to be analyzed can be compiled as usual. Any new compiled code will be
instrumented while the compilation mode ag has the value profiledcode.
The proling data is generated by simply running the program. The predicate profile_data/4 (see
below) makes available a selection of the data as a Prolog term. The predicate profile_reset/1
zeroes the proling counters for a selection of the currently instrumented predicates.
122
SICStus Prolog
profile_data(:Spec,?Selection,?Resolution,-Data)
Data is proling data collected from the predicates covered by Spec, which has the
same form as for spy/1 (see Section 6.3 [Spy-Point], page 62).
The Selection argument determines the kind of proling data to be collected. If uninstantiated, the predicate will backtrack over its possible values, which are:
calls
All instances of entering a clause by a procedure call are counted. This is
equivalent to counting all procedure calls that have not been determined
to fail by indexing on the rst argument.
backtracks
All instances of entering a clause by backtracking are counted.
choice_points
All instances of creating a choicepoint are counted. This occurs, roughly,
when the implementation determines that there are more than one possibly
matching clauses for a procedure call, and when a disjunction is entered.
shallow_fails
Failures in the \if" part of if-then-else statements, and in the \guard"
part of guarded clauses, are counted as shallow failures. See Section 10.4
[If-Then-Else], page 178.
deep_fails
Any failures that do not classify as shallow as above are counted as deep
failures. The reason for distinguishing shallow and deep failures is that the
former are considerably cheaper to execute than the latter.
execution_time
The execution time for the selected predicates, clauses, or disjuncts is estimated in articial units.
The Resolution argument determines the level of resolution of the proling data to be
collected. If uninstantiated, the predicate will backtrack over its possible values, which
are:
predicate
Data is a list of Module:PredName -Count, where Count is a sum of the
corresponding counts per clause.
clause
Data is a list of Module:ClauseName -Count, where Count includes counts
for any disjunctions occurring inside that clause. Note, however, that the
selections calls and backtracks do not include counts for disjunctions.
all
Data is a list of Module:InternalName -Count. This is the nest resolution
level, counting individual clauses and disjuncts.
Above, PredName is a predicate spec, ClauseName is a compound term PredName /ClauseNumber, and InternalName is either
ClauseName|corresponding to a clause, or
(ClauseName -DisjNo )/Arity /AltNo|corresponding to a disjunct.
profile_reset(:Spec )
Zeroes all counters for predicates covered by Spec, which has the same form as for
spy/1 (see Section 6.3 [Spy-Point], page 62).
Chapter 7: Built-In Predicates
123
7.16 Muse
The following predicates are only dened in the Muse Development System.
muse_sync
A built-in predicate that can be used to synchronize for being leftmost in the parallel
search tree. Its main use is in combination with foreign language interface calls.
muse_flags
This built-in predicate lists all current Muse ag settings.
muse_flag(?Flag,-Old )
muse_flag(?Flag,-Old,+New )
These examine and/or adjust parts of the Muse scheduler state. The current value is
returned in Old, and a new value may be specied in New.
The state cannot be changed when Muse is executing parallel work. It is therefore
recommended to perform such changes as the rst (or best single) goal in a top level
query. If used elsewhere, the execution may be aborted if the system cannot perform
the change.
Possible ag values are:
num_workers
Returns in Old the current number of workers, which is adjusted to New.
max_workers
The maximum number of Muse workers is returned. This ag is read-only.
worker_id
The identity of the currently executing Muse workers is returned. This ag
is read-only.
version
The Muse version is returned. The version can be plain or trace. This
ag is read-only.
The following ags also exist, but understanding them requires deep knowledge about
the Muse scheduler and execution model, so they are probably not useful for the average
user.
vol_susp Used to turn voluntary suspension on (1) or o (0).
vol_susp_time
Voluntary suspension interval, in milliseconds,
vol_susp_num
Number of workers to voluntarily suspend.
vol_susp_avoid
Controls the eagerness of voluntary suspension after resumption.
eager_cut
Used to turn eager pruning on (1) or o (0).
max_sch_loops
Maximum number of scheduler loops.
124
SICStus Prolog
max_suspended
Maximum number of megabytes for suspended branches.
muse_trace(:Goal )
muse_trace(:Goal,+FilePrex )
The trace version generates a trace le for the goal Goal. The le may be prexed with
FilePrex. The le is created in the current working directory if it can be written there,
or otherwise in the directory that is the value of the TMPDIR environment variable.
Cannot be executed in parallel when the execution changes the scheduler state.
7.17 Miscellaneous
?X = ?Y [ISO]
Dened as if by the clause Z=Z.; i.e. X and Y are unied.
length(?List,?Length)
If List is instantiated to a list of determinate length, then Length will be unied with
this length.
If List is of indeterminate length and Length is instantiated to an integer, then List
will be unied with a list of length Length. The list elements are unique variables.
If Length is unbound then Length will be unied with all possible lengths of List.
numbervars(?Term,+N,?M )
Unies each of the variables in term Term with a special term, so that write(Term)
(or writeq(Term)) (see Section 7.1.3 [Term I/O], page 80) prints those variables as
(A + (i mod 26))(i/26) where i ranges from N to M-1. N must be instantiated to an
integer. If it is 0 you get the variable names A, B, , Z, A1, B1, etc. This predicate
is used by listing/(0-1) (see Section 7.6 [State Info], page 104).
halt [ISO]
Causes Prolog to exit back to the shell. (In recursive calls to Prolog from C, this
predicate will return back to C instead.)
halt(+Code ) [ISO]
Causes the Prolog process to immediately exit back to the shell with the return code
Code, even if it occurs in a recursive call from C.
op(+Precedence,+Type,+Name ) [ISO]
Declares the atom Name to be an operator of the stated Type and Precedence (see
Section 2.6 [Operators], page 37). Name may also be a list of atoms in which case all
of them are declared to be operators. If Precedence is 0 then the operator properties
of Name (if any) are cancelled.
current_op(?Precedence,?Type,?Op ) [ISO]
The atom Op is currently an operator of type Type and precedence Precedence. Neither
Op nor the other arguments need be instantiated at the time of the call; i.e. this
predicate can be used to generate as well as to test.
:::
Chapter 7: Built-In Predicates
125
Invokes a recursive top-level. See Section 1.9 [Nested], page 16. (This predicate is not
available in Runtime Systems nor in Muse.)
abort
Aborts the current execution. See Section 1.9 [Nested], page 16. (In recursive calls to
Prolog from C, this predicate will return back to C instead.) (In Muse, will adjust the
system to one worker.)
save_program(+File )
save_program(+File, :Goal )
The system saves the program state into le File. When the program state is restored,
Goal is executed. Goal defaults to true. See Section 1.10 [Saving], page 17. (Requires
Muse to be adjusted to one worker.)
restore(+File )
The system is returned to the program state previously saved to le File with start-up
goal Goal. restore/1 may succeed, fail or raise an exception depending on Goal. See
Section 1.10 [Saving], page 17. (Requires Muse to be adjusted to one worker.)
break
reinitialise
This predicate can be used to force the reinitialization behavior to take place at any
time. When SICStus Prolog is reinitialized it:
calls any user dened C function pointed at by SP_reinit_hook (see Section 8.7
[Hooks], page 150).
calls version/0 to write banners. %
% looks for a le `~/.sicstusrc' or `~/.sicstus.ini' and % consults it, if it
exists. %
% ensures a le File is loaded if -l File was supplied as % program arguments.
calls initialization/0 to run user-dened initializations.
(In recursive calls to Prolog from C, this predicate will return back to C instead.) (In
Muse, will adjust the system to one worker.)
garbage_collect
Performs a garbage collection of the global stack immediately.
garbage_collect_atoms
Performs a garbage collection of the atoms immediately.
gc
Enables garbage collection of the global stack (the default).
nogc
Disables garbage collection of the global stack.
prompt(?Old,?New )
The sequence of characters (prompt) which indicates that the system is waiting for
user input is represented as an atom, and unied with Old; the atom bound to New
species the new prompt. In particular, the goal prompt(X, X) unies the current
prompt with X, without changing it. Note that this predicate only aects the prompt
given when a user's program is trying to read from the standard input stream (e.g. by
calling read/1). Note also that the prompt is reset to the default `|: ' on return to
top-level.
126
SICStus Prolog
Displays the introductory messages for all the component parts of the current system.
Prolog will display its own introductory message when initially run and on reinitialization by calling version/0. If this message is required at some other time it can be
obtained using this predicate which displays a list of introductory messages; initially
this list comprises only one message (Prolog's), but you can add more messages using
version/1. (This predicate is not available in Runtime Systems.)
version(+Message )
Appends Message to the end of the message list which is output by version/0. Message
must be an atom. (This predicate is not available in Runtime Systems.)
The idea of this message list is that, as systems are constructed on top of other systems,
each can add its own identication to the message list. Thus version/0 should always
indicate which modules make up a particular package. It is not possible to remove
messages from the list.
version
initialization
Executes goals dened by initialization/1. Only the rst solution is investigated.
initialization/0 is called at system (re)initialization. (This predicate is not available
in Runtime Systems.)
initialization(:Goal )
Appends Goal to the goal list which is executed by initialization/0. It is not
possible to remove goals from the list. (This predicate is not available in Runtime
Systems.)
initialization/(0-1) provide a mechanism which is similar but more general than
version/(0-1). It can, for example, be used by systems constructed on top of SICStus
Prolog to load their own initialization les.
help
Hookable, displays basic information, or a user dened help message. It rst calls
user:user_help/0, and only if that call fails is a default help message printed on the
current output stream. (This predicate is not available in Runtime Systems.)
user_help
user:user_help
A hook predicate. This may be dened by the user to print a help message on the
current output stream.
Chapter 8: Mixing C and Prolog
127
8 Mixing C and Prolog
SICStus Prolog provides a bi-directional, procedural interface for program parts written in C and
Prolog. The C side of the interface denes a number of functions and macros for various operations.
On the Prolog side, you have to supply declarations specifying the names and argument/value types
of C functions being called as Prolog predicates. These declarations are used by the predicate load_
foreign_resource/1, which performs the actual binding of C functions to Prolog predicates.
In most cases, the argument/value type declaration suce for making the necessary conversions of
data automatically as they are passed between C and Prolog. However, it is possible to declare
the type of an argument to be a Prolog term, in which case the receiving function will see it as a
\handle" object, called an SP_term_ref, for which access functions are provided.
The C support routines are available in a Development System as well as in Runtime Systems. The
support routines include:
Static and dynamic linking of C code into the Prolog environment.
Automatic conversion between Prolog terms and C data with foreign/(2-3) declarations.
Functions for accessing and creating Prolog terms, and for creating and manipulating
SP term refs.
The Prolog system may call C predicates which may call Prolog back without limits on recursion.
Possibility to create stand-alone applications, Runtime Systems.
Possibility to create user dened Prolog streams.
Functions to read and write on Prolog streams from C.
Possibility to install interrupt handlers that can safely call Prolog.
User hooks that can be used to perform user dened actions on a number of occasions e.g.
before reading a character from the standard input stream, upon reinitialization, etc.
8.1 Notes
ANSI Conformance
Throughout this chapter, void * in the function denitions may be changed to char *
on non ANSI conforming C compilers.
The SP PATH variable
It is normally not necessary to set this environment variable, but its value will be
used at runtime if no explicit boot path is given when initializing a Runtime or Development System. In this chapter, the environment variable SP_PATH is used as a
shorthand for the SICStus Prolog installation directory, whose default UNIX location
is `/usr/local/lib/sicstus37'). See Section 1.1.2 [Environment Variables], page 10.
Denitions and declarations
Type denitions and function declarations for the interface are found in the header le
`<sicstus/sicstus.h>'.
128
SICStus Prolog
Error Codes
The value of many support functions is a return code which is one of SP_SUCCESS for
success, SP_FAILURE for failure, SP_ERROR if an error condition occurred. In particular,
uncaught exceptions resulting from calls from C to Prolog raise an error condition. In
error situations, the macro SP_errno will return a value describing the error condition:
int SP_errno
The function SP_error_message returns a pointer to the diagnostic message corresponding to a specied error number:
char *SP_error_message(int errno)
8.2 Calling C from Prolog
Functions written in the C language may be called from Prolog using an interface in which automatic
type conversions between Prolog terms and common C types are declared as Prolog facts. Calling
without type conversion can also be specied, in which case the arguments and values are passed
as SP term refs. This interface is partly modeled after Quintus Prolog.
The functions installed using this foreign language interface may invoke Prolog code and use the
support functions described in the other sections of this chapter.
Functions, or their equivalent, in any other language having C compatible calling conventions may
also be interfaced using this interface. When referring to C functions in the following, we also
include such other language functions. Note however that a C compiler is needed since a small
amount of glue code (in C) must be generated for interfacing purposes. Note also that on some
platforms dierent calling conventions may exist for C functions. Check the Release Notes to see
which one is used by this interface.
8.2.1 Foreign Resource and Conversion Declarations
A foreign resource is a set of C functions, dened in one or more les, installed as an atomic
operation. The name of a foreign resource, the resource name, is an atom, which should uniquely
identify the resource. Thus, two foreign resources with the same name cannot be installed at the
same time.
For each foreign resource, a foreign_resource/2 fact is used to declare the interfaced functions.
For each of these functions, a foreign/(2,3) fact is used to specify conversions between predicate
arguments and C-types. These conversion declarations are used for creating the necessary interface
between Prolog and C.
The functions making up the foreign resource, the automatically generated interface code, and
any libraries, are compiled and linked to form a linked foreign resource, which can be statically
or dynamically linked to a Development or Runtime System. In all cases, the declared predicates
are installed by the built-in predicate load_foreign_resource/1. The resource name of a linked
foreign resource is derived from its le specication by deleting any leading path and the sux.
Chapter 8: Mixing C and Prolog
129
8.2.2 Static and Dynamic linking
The interface can be used in two ways.
With static linking, the linked foreign resource is statically linked to a Development or Runtime
System. load_foreign_resource/1 determines if a resource has been statically linked, in which
case only the binding of predicate names to functions needs to be done. Otherwise, dynamic linking
is attempted.
With dynamic linking, the linked foreign resource can be linked and unlinked to a Development or
Runtime System, as needed.
A linked foreign resource may be implemented, depending on the hardware and operating system
platform, as e.g. a shared object or a dynamic link library.
8.2.3 Conversion Declarations
Conversion declaration predicates:
foreign_resource(+ResourceName,+Functions )
A hook predicate. Species that a set of foreign functions, to be called from Prolog,
are to be found in the resource named by ResourceName. Functions is a list of functions exported by the resource. Only functions that are to be called from Prolog and
optionally one init function and one deinit function should be listed. The init and
deinit functions are specied as init(Function) and deinit(Function) respectively
(see Section 8.2.6 [Init and Deinit Functions], page 134). This predicate should be
dened entirely in terms of facts (unit clauses). For example:
foreign_resource('terminal', [scroll,pos_cursor,ask]).
species that functions scroll(), pos_cursor() and ask() are to be found in the
resource `terminal'.
foreign(+CFunctionName, +Predicate )
foreign(+CFunctionName, +Language, +Predicate )
Hook predicates, specify the Prolog interface to a C function. Language is at present
constrained to the atom c. CFunctionName is the name of a C function. Predicate species the name of the Prolog predicate that will be used to call CFunction().
Predicate also species how the predicate arguments are to be translated into the corresponding C arguments. This predicate should be dened entirely in terms of facts
(unit clauses). For example:
foreign(pos_cursor, c, move_cursor(+integer, +integer)).
The above example says that the C function pos_cursor() has two integer value
arguments and that we will use the predicate move_cursor/2 to call this function. A
goal move_cursor(5, 23) would translate into the C call pos_cursor(5,23);.
The third argument of the predicate foreign/3 species how to translate between
Prolog arguments and C arguments. A call to a foreign predicate will raise an exception
if an input arguments is uninstantiated (instantiation_error/2) or has the wrong
130
SICStus Prolog
type (type_error/4) or domain (domain_error/4). The call will fail upon return from
the function if the output arguments do not unify with the actual arguments.
The available conversions are listed in the next subsection.
8.2.4 Conversions between Prolog Arguments and C Types
The following table lists the possible values for the arguments in the predicate specication of
foreign/(2,3). The value declares which conversion between corresponding Prolog argument and
C type will take place.
Prolog: +integer
C: long
The argument should be a number. It is converted to a C long and passed to the C
function.
Prolog: +float
C: double
The argument should be a number. It is converted to a C double and passed to the C
function.
Prolog: +atom
C: unsigned long
The argument should be an atom. Its canonical representation is passed to the C
function.
Prolog: +chars
C: char *
The argument should be a list of character codes. The C function will be passed the
address of an array of characters containing these characters. The array is subject to
reuse by other support functions, so if the value is going to be used on a more than
temporary basis, it must be moved elsewhere.
Prolog: +string
The argument should be an atom. The C function will be passed the address of a text
string containing the printed representation of the atom. The C function should not
overwrite the string.
Prolog: +string(N )
C: char * The argument should be an atom. The printable representation of the string will be
copied into a newly allocated buer. The string will be truncated if it is longer than
N characters. The string will be blank padded on the right if it is shorter than N
characters. The C function will be passed the address of the buer. The C function
may overwrite the buer, but should not assume that it remains valid after returning.
C: char *
Prolog: +address
The argument should be an integer which value is constrained according to (see Section 8.3.2 [Creating Prolog Terms], page 137, SP put address()). The value passed will
be a void * pointer.
Prolog: +address(TypeName )
C: void *
Chapter 8: Mixing C and Prolog
C:
131
TypeName *
The argument should be an integer which value is constrained according to (see Section 8.3.2 [Creating Prolog Terms], page 137, SP put address()). The value passed will
be a TypeName * pointer.
Prolog: +term
C: SP_term_ref
The argument could be any term. The value passed will be the internal representation
of the term.
Prolog: -integer
C: long *
The C function is passed a reference to an uninitialized long. The value returned will
be converted to a Prolog integer.
Prolog: -float
C: double *
The C function is passed a reference to an uninitialized double. The value returned
will be converted to a Prolog oat.
Prolog: -atom
C: unsigned long *
The C function is passed a reference to an uninitialized unsigned long. The value
returned should be the canonical representation of a Prolog atom.
Prolog: -chars
C: char **
The C function is passed the address of an uninitialized char *. The returned string
will be converted to a Prolog list of character codes.
Prolog: -string
C: char **
The C function is passed the address of an uninitialized char *. The returned string
will be converted to a Prolog atom. Prolog will copy the string to a safe place, so
the memory occupied by the returned string may be reused during subsequent calls to
foreign code.
Prolog: -string(N )
C: char * The C function is passed a reference to a character buer large enough to store an N
character string. The returned string will be stripped of trailing blanks and converted
to a Prolog atom.
Prolog: -address
C: void **
The C function is passed the address of an uninitialized void *. The returned value,
which is constrained according to (see Section 8.3.2 [Creating Prolog Terms], page 137,
SP put address()), will be converted to a Prolog integer.
Prolog: -address(TypeName )
132
C:
SICStus Prolog
TypeName **
The C function is passed the address of an uninitialized TypeName *. The returned
value, which is constrained according to (see Section 8.3.2 [Creating Prolog Terms],
page 137, SP put address()), will be converted to a Prolog integer.
Prolog: -term
C: SP_term_ref
The C function is passed a new SP term ref, and is expected to set its value to a
suitable Prolog term. Prolog will try to unify the value with the actual argument.
Prolog: [-integer]
C: long
F ()
The C function should return a long. The value returned will be converted to a Prolog
integer.
Prolog: [-float]
C: double
F ()
The C function should return a
Prolog oat.
double.
The value returned will be converted to a
Prolog: [-atom]
F ()
The C function should return an unsigned long. The value returned must be the
canonical representation of a Prolog atom.
C: unsigned long
Prolog: [-chars]
C: char *F ()
The C function should return a
Prolog list of character codes.
char *.
The returned string will be converted to a
Prolog: [-string]
C: char *F ()
The C function should return a char *. The returned string will be converted to a
Prolog atom. Prolog will copy the string to a safe place, so the memory occupied by
the returned string may be reused during subsequent calls to foreign code.
Prolog: [-string(N )]
C: char *F ()
The C function should return a char *. The rst N characters of the string will be
copied and the copied string will be stripped of trailing blanks. The stripped string
will be converted to a Prolog atom. C may reuse or destroy the string buer during
later calls.
Prolog: [-address]
C: void *F ()
The C function should return a void *. The returned value, which is constrained
according to (see Section 8.3.2 [Creating Prolog Terms], page 137, SP put address()),
will be converted to a Prolog integer.
Prolog: [-address(TypeName )]
Chapter 8: Mixing C and Prolog
C:
133
TypeName *F ()
The C function should return a TypeName *. The returned value, which is constrained
according to (see Section 8.3.2 [Creating Prolog Terms], page 137, SP put address()),
will be converted to a Prolog integer.
Prolog: [-term]
F ()
The C function should return an SP term ref. Prolog will try to unify its value with
the actual argument.
C: SP_term_ref
8.2.5 Interface Predicates
In the following descriptions, the Resource argument is an unsuxed le specication of a linked
foreign resource.
link_foreign_resource(+Resource,+SourceFile,+Option,+CFiles,+ObjectFiles,+Libraries )
Builds a linked foreign resource. Option can be either dynamic or static as described in
the alternative form shown below. This predicate is not available in Runtime Systems.
The work of this predicate is done by a command located in `$SP_PATH/bin'. This
command is congurable as described in the Release Notes. It can also be invoked
directly from a shell as an alternative to running it from Prolog:
splfr Resource SourceFile [+dynamic | +static] [+c CFiles ] [+o ObjectFiles ] [+l Libraries ]
[+copt COptions ]
Reads terms from SourceFile, applying term expansion and extracting any foreign_
resource/2 fact with rst argument matching the name of Resource and all
foreign/(2,3) facts. Compiles CFiles and the interface code derived from these facts
and links the resulting object les with ObjectFiles and Libraries to form the linked
foreign resource Resource. The option +dynamic (+static) species that the linked
foreign resource is suitable for dynamic (static) linking. The default is dynamic. The
option +copt can be used to pass options to the C-compiler.
load_foreign_resource(:Resource )
Unless a foreign resource with the same name as Resource has been statically linked,
the linked foreign resource specied by Resource is linked into the Prolog load image.
In both cases, the predicates dened by Resource are installed, and any init function
is called. Dynamic linking is not possible if the foreign resource was linked using the
+static option.
If a resource with the same name has been previously loaded, it will be unloaded as if
unload_foreign_resource(Resource ) was called, before Resource is loaded.
In Muse, the system is required to be adjusted to one worker rst, and the predicates
dened become cavalier (see Section 3.5 [Programming Considerations], page 45).
unload_foreign_resource(:Resource )
Any deinit function associated with Resource is called, and the predicates dened by
Resource are uninstalled. If Resource has been dynamically linked, it is unlinked from
the Prolog load image.
134
SICStus Prolog
In Muse, the system is required to be adjusted to one worker rst, and the predicates
dened become cavalier
The following predicates are provided for backwards compatibility and should be avoided in new
code:
foreign_file(+File,+Functions )
A hook predicate. Species that a set of foreign functions, to be called from Prolog,
are to be found in File. This predicate is only called from load_foreign_files/2.
load_foreign_files(:ObjectFiles,+Libraries )
Hookable, a resource name is derived from the rst le name in ObjectFiles by stripping
o the sux. If this resource has been statically linked, the predicates dened by it
are installed; otherwise, a linked foreign resource containing the declared functions is
created and loaded. Not available in Runtime Systems.
8.2.6 Init and Deinit Functions
An init function and/or a deinit function can be declared by foreign_resource/2. If this is the
case, these functions should have the prototype:
void
FunctionName (int when)
The init function is called by load_foreign_resource/1 after the resource has been loaded and
the interfaced predicates have been installed.
The deinit function is called by unload_foreign_resource/1 before the interfaced predicates have
been uninstalled and the resource has been unloaded.
The init and deinit functions may use the C-interface to call Prolog etc.
Foreign resources are unloaded before saving states, and reloaded afterwards or when the saved state
is restored; see Section 1.10 [Saving], page 17. Foreign resources are also unloaded when exiting
Prolog execution. The parameter when reects the context of the (un)load_foreign_resource/1
and is set as follows for init functions:
SP_WHEN_EXPLICIT
Explicit call to load_foreign_resource/1.
SP_WHEN_RESTORE
Resource is reloaded after save or restore.
For deinit functions:
SP_WHEN_EXPLICIT
Explicit call to unload_foreign_resource/1.
SP_WHEN_SAVE
Resource is unloaded before save.
Chapter 8: Mixing C and Prolog
135
SP_WHEN_EXIT
Resource is unloaded before exiting Prolog.
8.2.7 Creating the Linked Foreign Resource
Suppose we have a Prolog source le ex.pl containing:
foreign(f1, p1(+integer,[-integer])).
foreign(f2, p2(+integer,[-integer])).
foreign_resource(ex, [f1,f2]).
:- load_foreign_resource(ex).
and a C source le ex.c with denitions of the functions f1 and f2, both returning long and
having a long as only parameter. The conversion declarations in `ex.pl' state that these functions
form the foreign resource ex.
To create the linked foreign resource, simply type (to Prolog):
| ?- link_foreign_resource(ex,'ex.pl',dynamic,['ex.c'],[],[]).
or alternatively (to the Shell):
% splfr ex ex.pl +c ex.c
The linked foreign resource `ex.so' (le sux `.so' is system dependent) has been created. It will
be dynamically linked by the directive :- load_foreign_resource(ex). when the le `ex.pl' is
loaded. Linked foreign resources can also be created manually (see Chapter 38 [Runtime Utilities],
page 373).
Dynamic linking of foreign resources can also be used by Runtime Systems. On some platforms,
however, the executable must not be stripped for dynamic linking to work, i.e. its symbol table
must remain.
8.3 Support Functions
The support functions include functions to manipulate SP term refs, functions to convert data
between the basic C types and Prolog terms, functions to test whether a term can be converted to
a specic C type, and functions to unify or compare two terms.
136
SICStus Prolog
8.3.1 Creating and Manipulating SP term refs
Normally, C functions only have indirect access to Prolog terms via SP term refs. C functions may
receive arguments as unconverted Prolog terms, in which case the actual arguments received will
have the type SP_term_ref. Also, a C function may return an unconverted Prolog term, in which
case it must create an SP term ref. Finally, any temporary Prolog terms created by C code must
be handled as SP term refs.
SP term refs are motivated by the fact that SICStus Prolog's memory manager must have a means
of reaching all live Prolog terms for memory management purposes, including such terms that are
being manipulated by the user's C code. Previous releases of SICStus Prolog provided direct access
to Prolog terms and the ability to tell the memory manager that a given memory address points
to a Prolog term, but this approach was too low level and highly error-prone. The current design
is modeled after and largely compatible with Quintus Prolog release 3.
SP term refs are created dynamically. At any given time, an SP term ref has a value (a Prolog
term). This value can be examined, accessed, and updated by the support functions described in
this section.
It is important to understand the rules governing the scope of SP term refs in conjunction with
calls from Prolog to C and vice versa:
When a C function called from Prolog returns, all SP term refs passed to the function or
dynamically created by the function become invalid.
When terms are passed to C as a result of calling Prolog, those terms and any SP term refs
created since the start of the query are only valid up to the request for the next solution. They
also become invalid when the query is closed.
A new SP term ref whose value is [] is created by calling
SP_term_ref SP_new_term_ref(void)
The value of the SP term ref to is set to the value of the SP term ref
The previous value of to is lost:
term(to,from).
from
by calling SP_put_
void SP_put_term(SP_term_ref to, SP_term_ref from)
Each Prolog atom is represented internally by a unique integer, represented in C as an unsigned
long. This mapping between atoms and integers depends on the execution history. Certain functions require this representation as opposed to an SP pred ref. It can be obtain by a special
argument type declaration when calling C from Prolog, by calling SP_get_atom(), or by looking
up a string s in the Prolog symbol table by calling SP_atom_from_string(s):
unsigned long SP_atom_from_string(char *s)
The print name of a Prolog atom a can be obtained by calling:
char *SP_string_from_atom(unsigned long a)
The print name length of a Prolog atom a can be obtained by calling:
Chapter 8: Mixing C and Prolog
137
int SP_atom_length(unsigned long a)
Same as strlen(SP_string_from_atom(a)) but runs in O(1) time.
Prolog atoms, and the space occupied by their print names, are subject to garbage collection when
the number of atoms has reached a certain threshold, under the control of the agc_margin Prolog
ag (see Section 7.6 [State Info], page 104), or when the atom garbage collector is called explicitly.
The atom garbage collector will nd all references to atoms from the Prolog specic memory areas,
including SP term refs and arguments passed from Prolog to foreign language functions. However,
atoms created by SP_atom_from_string and merely stored in a local variable are endangered by
garbage collection. The following functions make it possible to protect an atom while it is in use.
The operations are implemented using reference counters to cater for multiple, independent use of
the same atom in dierent foreign resources:
int SP_register_atom(unsigned long a)
Registers the atom a with the Prolog memory manager by incrementing its reference counter.
Returns a nonzero value if the operation succeeds.
int SP_unregister_atom(unsigned long a)
Unregisters the atom a with the Prolog memory manager by decrementing its reference counter.
Returns a nonzero value if the operation succeeds.
8.3.2 Creating Prolog Terms
These functions create a term and store it as the value of an SP term ref, which must exist prior to
the call. They return zero if the conversion fails (as far as failure can be detected), and a nonzero
value otherwise, assigning to t the converted value.
int SP_put_variable(SP_term_ref t)
Assigns to t a new Prolog variable.
int SP_put_integer(SP_term_ref t, long l)
Assigns to t a Prolog integer from a
C long integer.
int SP_put_float(SP_term_ref t, double d)
Assigns to t a Prolog oat from a C
double.
int SP_put_atom(SP_term_ref t, unsigned long a)
Assigns to t a Prolog atom from a, which
must be the canonical representation of a
Prolog atom. (see Section 8.2 [Calling C], page 128).
int SP_put_string(SP_term_ref t, char *name)
Assigns to t a Prolog atom from a C string.
int SP_put_address(SP_term_ref t, void *pointer)
Assigns to t a Prolog integer from a C pointer.
The pointer must be NULL or an address having the four most signicant bits in common
with addresses returned by malloc(). (This excludes pointers to automatic C variables,
138
SICStus Prolog
i.e. addresses to the C stack). Furthermore, the address must be aligned on a four bytes
boundary.
NULL is converted to the integer 0.
int SP_put_list_chars(SP_term_ref t, SP_term_ref tail, char *s)
Assigns to t a Prolog list of the character codes in s. The list
value of tail.
is terminated by the
int SP_put_list_n_chars(SP_term_ref t, SP_term_ref tail, long n, char *s)
Assigns to t a Prolog list of the rst n character codes or zeroes in s.
terminated by the value of tail.
int SP_put_number_chars(SP_term_ref t, char *s)
Assigns to t a Prolog number by parsing the
The list is
string in s.
int SP_put_functor(SP_term_ref t, unsigned long name, int arity)
Assigns to t a Prolog compound term with all the arguments unbound variables. If
arity is 0, assigns the Prolog atom whose canonical representation is name to t. This
is similar to calling functor/3 with the rst argument unbound and the second and
third arguments bound to an atom and an integer, respectively.
int SP_put_list(SP_term_ref t)
Assigns to t a Prolog list
whose head and tail are both unbound variables.
int SP_cons_functor(SP_term_ref t, unsigned long name, int arity, SP_term_ref arg,
...)
Assigns to t a Prolog compound term whose arguments are the values of arg... If
arity is 0, assigns the Prolog atom whose canonical representation is name to t. This
is similar to calling =../2 with the rst argument unbound and the second argument
bound.
int SP_cons_list(SP_term_ref t, SP_term_ref head, SP_term_ref tail)
Assigns to t a Prolog list whose head and tail are the values of head
and tail.
8.3.3 Accessing Prolog Terms
These functions will take an SP term ref and convert it to C data. They return zero if the conversion
fails, and a nonzero value otherwise, and (except the last one) store the C data in output arguments.
int SP_get_integer(SP_term_ref t, long *l)
Assigns to *l the C long corresponding
for the operation to succeed.
to a Prolog number. The value must t in *l
int SP_get_float(SP_term_ref t, double *d)
Assigns to *d the C double corresponding
to a Prolog number.
int SP_get_atom(SP_term_ref t, unsigned long *a)
Assigns to *a the canonical representation of
int SP_get_string(SP_term_ref t, char **name)
Assigns to *name a pointer to the string that
must not be modied.
a Prolog atom.
is the name of a Prolog atom. This string
Chapter 8: Mixing C and Prolog
139
int SP_get_address(SP_term_ref t, void **pointer)
Assigns to *pointer a C pointer from a Prolog
term. The term should be an integer
which value is constrained according to (see Section 8.3.2 [Creating Prolog Terms],
page 137, SP put address()).
int SP_get_list_chars(SP_term_ref t, char **s)
Assigns to *s a zero-terminated array of
characters corresponding to a Prolog list of
character codes. The array is subject to reuse by other support functions, so if the
value is going to be used on a more than temporary basis, it must be moved elsewhere.
int SP_get_list_n_chars(SP_term_ref t, SP_term_ref tail, long n, long *w, char *s)
Copies into s at most n character codes or zeroes from the list t. The number of
character codes actually written is assigned to *w. tail is set to the remainder of the
list. The array s must have room for at least n characters.
int SP_get_number_chars(SP_term_ref t, char **s)
Assigns to *s a zero-terminated array of characters
corresponding to the printed representation of a Prolog number. The array is subject to reuse by other support functions,
so if the value is going to be used on a more than temporary basis, it must be moved
elsewhere.
int SP_get_functor(SP_term_ref t, unsigned long *name, int *arity)
Assigns to *name and *arity the canonical representation and arity of the principal
functor of a Prolog compound term. If the value of t is an atom, then that atom is
assigned to *name and 0 is assigned to *arity. This is similar to calling functor/3
with the rst argument bound to a compound term or an atom and the second and
third arguments unbound.
int SP_get_list(SP_term_ref t, SP_term_ref head, SP_term_ref tail)
Assigns to head and tail the head and tail of a Prolog list.
int SP_get_arg(int i, SP_term_ref t, SP_term_ref arg)
Assigns to arg the i:th argument of a Prolog compound
arg/3 with the third argument unbound.
term. This is similar to calling
8.3.4 Testing Prolog Terms
There is one general function for type testing of Prolog terms and a set of specialized, more ecient,
functions, one for each term type.
int SP_term_type(SP_term_ref t)
Depending on the type of the term t, one of SP_TYPE_VARIABLE, SP_TYPE_INTEGER,
SP_TYPE_FLOAT, SP_TYPE_ATOM, or SP_TYPE_COMPOUND is returned.
int SP_is_variable(SP_term_ref t)
Returns nonzero if the term is a Prolog variable, zero otherwise.
int SP_is_integer(SP_term_ref t)
Returns nonzero if the term is a Prolog integer, zero otherwise.
140
SICStus Prolog
int SP_is_float(SP_term_ref t)
Returns nonzero if the term is a Prolog oat, zero otherwise.
int SP_is_atom(SP_term_ref t)
Returns nonzero if the term is a Prolog atom, zero otherwise.
int SP_is_compound(SP_term_ref t)
Returns nonzero if the term is a Prolog compound term, zero otherwise.
int SP_is_list(SP_term_ref t)
Returns nonzero if the term is a Prolog list, zero otherwise.
int SP_is_atomic(SP_term_ref t)
Returns nonzero if the term is an atomic Prolog term, zero otherwise.
int SP_is_number(SP_term_ref t)
Returns nonzero if the term is a Prolog number, zero otherwise.
8.3.5 Unifying and Comparing Terms
int SP_unify(SP_term_ref x, SP_term_ref y)
Unies two terms, returning zero on failure and nonzero on success.
int SP_compare(SP_term_ref x, SP_term_ref y)
Returns -1 if x @< y, 0 if x == y and 1
if x @> y
8.3.6 Memory Allocation
The usual C library memory allocation functions (malloc, realloc, and free) may not work
properly in foreign code. The following functions provide these services from SICStus Prolog's
memory manager:
void *SP_malloc(unsigned int size)
Returns a properly aligned pointer to a block of at least
blocks are allocated in shared memory.
size
bytes. In Muse, the
void *SP_realloc(void *ptr, unsigned int size)
Changes the size of the block referenced by ptr to size bytes and returns a pointer
to the (possibly moved) block. The contents will be unchanged up to the lesser of the
new and old sizes. The block referenced by ptr must have been obtained by a call to
SP_malloc or SP_realloc, and must not have been released by a call to SP_free or
SP_realloc. In Muse, the blocks are allocated in shared memory.
void SP_free(void *ptr)
Releases the block referenced by ptr, which must have been obtained by a call to
SP_malloc or SP_realloc, and must not have been released by a call to SP_free or
SP_realloc. In Muse, the blocks are allocated in shared memory.
Chapter 8: Mixing C and Prolog
141
8.4 Calling Prolog from C
In the sequential Development System and in Runtime Systems, Prolog and C code may call each
other to arbitrary depths. The ability to call Prolog from C is not available in the Muse Development
System in this release.
Before calling a predicate from C you must look up the predicate denition by module, name, and
arity. The function SP_predicate() will return a pointer to this denition or return NULL if the
predicate is not visible in the module. This denition could be used in more than one call to the
same predicate. The module specication is optional. If NULL or "" (the empty string) is given
then the default type-in module (see Section 4.2 [Module Spec], page 48) is assumed:
SP_pred_ref SP_predicate(char *name_string,
long arity,
char *module_string)
The function SP_pred() may be used as an alternative to the above. The only dierence is that
the name and module arguments are passed as Prolog atoms rather than strings, and the module
argument is mandatory. This saves the cost of looking up the two arguments in the Prolog symbol
table. This cost dominates the cost of SP_predicate().
SP_pred_ref SP_pred(unsigned long name_atom,
long arity,
unsigned long module_atom)
8.4.1 Finding One Solution of a Call
The easiest way to call a predicate if you are only interested in the rst solution is to call the
function SP_query(). It will create a goal from the predicate denition and the arguments, and
try to prove it. The call will return SP_SUCCESS if the goal succeeded, SP_FAILURE if it failed, and
SP_ERROR if an error condition occurred.
int SP_query(SP_pred_ref predicate, SP_term_ref arg1, ...)
If you are only interested in the side eects of a predicate you can call SP_query_cut_fail(). It
will try to prove the predicate, cut away the rest of the solutions, and nally fail. This will reclaim
the storage used after the call:
int SP_query_cut_fail(SP_pred_ref predicate, SP_term_ref arg1, ...)
8.4.2 Finding Multiple Solutions of a Call
If you are interested in more than one solution a more complicated scheme is used. You nd the
predicate denition as above but you don't call the predicate directly.
1. Set up a call with SP_open_query()
142
SICStus Prolog
2. Call SP_next_solution() to nd a solution. Call this predicate again to nd more solutions
if there are any.
3. Terminate the call with SP_close_query() or SP_cut_query()
The function SP_open_query() will return an identier of type SP_qid that you use in successive
calls, or NULL, if given an invalid predicate reference. Note that if a new query is opened while
another is already open, the new query must be terminated before performing any action on the
old one. That is, queries must be strictly nested:
SP_qid SP_open_query(SP_pred_ref predicate, SP_term_ref arg1, ...)
The function SP_next_solution() will cause the Prolog engine to nd solutions of the open query.
The SP term refs that you sent with the call to SP_open_query() will be assigned new values.
SP_next_solution() will return SP_SUCCESS for success, SP_FAILURE for failure, SP_ERROR if an
error condition occurred.
int SP_next_solution(SP_qid query)
You can terminate a query in two ways. The function SP_cut_query() will only take away the
choices created since the corresponding SP_open_query(). The data created in the call are still
valid and could be passed to another call. The function SP_close_query() will restore the state to
what it was before the call to SP_open_query(). If the call created data that you want to keep, it
must be converted to C data before calling SP_close_query(). Both functions return SP_SUCCESS
for success and SP_ERROR for invalid usage:
int SP_cut_query(SP_qid query)
int SP_close_query(SP_qid query)
8.4.3 Calling Prolog Asynchronously
A Prolog execution may be interrupted by signals or similar asynchronous events. If you wish to
call Prolog back from a signal handler you cannot use SP_query() etc. directly. The call to Prolog
has to be delayed until a time when the Prolog execution can accept an interrupt. The function
SP_event() serves this purpose, and installs the function func to be called from Prolog when the
execution can accept a callback. Returns non-zero i installation succeeded. func is called with
arg as rst argument.
A queue of functions, with corresponding arguments, is maintained; that is, if several calls to SP_
event() occur before Prolog can a accept an interrupt, the functions are queued and executed in
turn at the next possible opportunity. Note that the queuing facility is only safe for signal handlers
installed using SP_signal() (see below).
Depending on the value returned from func, the interrupted Prolog execution will just continue (SP_
SUCCESS) or backtrack (SP_FAILURE or SP_ERROR). An exception raised by func will be processed
in the interrupted Prolog execution. In case of fail or exception the event queue is ushed:
Chapter 8: Mixing C and Prolog
143
int SP_event(int (*func)(), void *arg)
A signal handler having called SP_event() should call SP_continue() as its last action, to ensure
that the interrupt is processed as early as possible:
void SP_continue()
To install a function, func, as a handler for the signal sig, call:
void (*SP_signal (int sig, void (*func)()))()
will also, if permitted by the operating system, add sig to a set of signals which are
all blocked during the handling of the event queue. Some operating systems require that:
SP_signal()
void (*SP_reinstall_signal (int sig, void (*func)()))()
be called from a signal handler to unblock or reinstall the handler. This function should be called
before SP_continue().
The following piece of C code illustrates these facilities. The function signal_init() installs the
function signal_handler() as the primary signal handler for the signals USR1 and USR2. That
function invokes the predicate prolog_handler/1 as the actual signal handler, passing the signal
number as an argument to the predicate.
SP_pred_ref event_pred;
static int signal_event(signal_no)
void *signal_no;
{
SP_term_ref x=SP_new_term_ref();
int rc;
}
SP_put_integer(x, (int)signal_no);
rc = SP_query(event_pred, x);
if (rc == SP_ERROR && SP_exception_term(x))
SP_raise_exception(x);
/* Propagate any raised exception */
return rc;
static void signal_handler(sig)
int sig;
{
SP_event(signal_event, (void *)sig);
SP_reinstall_signal(sig, signal_handler);
SP_continue();
}
void signal_init()
{
event_pred = SP_predicate("prolog_handler",1,"");
144
SICStus Prolog
}
SP_signal(SIGUSR1, signal_handler);
SP_signal(SIGUSR2, signal_handler);
8.4.4 Exception Handling in C
When an exception has been raised, the functions SP_query(), SP_query_cut_fail() and SP_
next_solution() return SP_ERROR. To access the exception term (the argument of the call to
raise_exception/1), which is asserted when the exception is raised, the function SP_exception_
term() is used. As a side eect, the exception term is retracted, so if your code wants to pass
the exception term back to Prolog, it must use the SP_raise_exception() function below. If an
exception term exists, SP_exception_term() retracts it and stores it as the value of an SP term ref
which must exist prior to the call and returns nonzero. Otherwise, it returns zero.
int SP_exception_term(SP_term_ref t)
To raise an exception from a C function called from Prolog, just call SP_raise_exception(t)
where t is the SP term ref whose value is the exception term. The glue code will detect that an
exception has been raised, any value returned from the function will be ignored, and the exception
will be passed back to Prolog.
void SP_raise_exception(SP_term_ref t)
8.5 SICStus Streams
With the SICStus Prolog C interface, the user can dene his/her own streams as well as from C
read or write on the predened streams. The stream interface is modeled after Quintus Prolog
release 2. It provides:
C functions to perform I/O on Prolog streams. This way you can use the same stream from
Prolog and C code.
User dened streams. You can dene your own Prolog streams in C.
Bidirectional streams. A SICStus stream supports reading or writing or both.
Hookable standard input/output/error streams.
Chapter 8: Mixing C and Prolog
145
8.5.1 Prolog Streams
From the Prolog level there is a unique number that identies a stream. This identier can be
converted from/to a Prolog stream:
stream_code(+Stream,?StreamCode )
stream_code(?Stream,+StreamCode )
StreamCode is the C stream identier (an integer) corresponding to the Prolog stream
Stream. This predicate is only useful when streams are passed between Prolog and C.
Note that StreamCode no longer has any relation to the le descriptor.
The StreamCode is a Prolog integer representing a SP_stream * pointer as described in (see Section 8.3.2 [Creating Prolog Terms], page 137, SP put address()).
To write on a Prolog stream from C, special versions of the most common standard C I/O functions
are used:
int SP_getc(void)
int SP_fgetc(SP_stream *s)
void SP_putc(int c)
void SP_fputc(int c, SP_stream *s)
void SP_puts(char *string)
void SP_fputs(char *string, SP_stream *s)
int SP_printf(char *format, ...)
int SP_fprintf(SP_stream *s, char *format, ...)
int SP_fflush(SP_stream *s)
int SP_fclose(SP_stream *s)
There are three predened streams accessible from C:
SP_stdin
SP_stdout
SP_stderr
SP_curin
SP_curout
Standard input. Refers to the same stream as user_input in Prolog. Which stream is
referenced by user_input is controlled by the ag user_input (see prolog_flag/3) .
Standard output. Refers to the same stream as user_output in Prolog. Which stream
is referenced by user_output is controlled by the ag user_output (see prolog_
flag/3).
Standard error. Refers to the same stream as user_error in Prolog. Which stream is
referenced by user_error is controlled by the ag user_error (see prolog_flag/3).
Current input. It is initially set equal to SP_stdin. It can be changed with the
predicates see/1 and set_input/1.
Current output. It is initially set equal to
predicates tell/1 and set_output/1.
SP_stdout.
It can be changed with the
146
SICStus Prolog
Note that these variables are read only. They are set but never read by the stream handling.
8.5.2 Dening a New Stream
The following steps are required to dene a new stream in C:
Dene low level functions (character reading, writing etc).
Initialize and open your stream.
Allocate memory needed for your particular stream.
Initialize and install a Prolog stream with SP_make_stream().
Initialize additional elds. Some streams may require additional changes to the elds in the
SP_stream structure than the default values set by SP_make_stream().
8.5.2.1 Low Level I/O Functions
For each new stream the appropriate low level I/O functions have to be dened. Error handling,
prompt handling and character counting is handled in a layer above these functions. They all
operate on a user dened private data structure pointed out by user_handle in SP_stream.
User dened low level I/O functions may invoke Prolog code and use the support functions described
in the other sections of this chapter.
my_fgetc (void *handle)
Should return the character read or -1 on end of le.
int my_fputc (char c, int handle)
Should write the character c and return the character written.
int my_ush(void *handle)
Flush the stream. Should return 0 on success, EOF on error.
int my_eof (void *handle)
Should return 1 on end of le, else 0.
void my_clrerr (void *handle)
Clear the error level.
int my_close (void *handle)
Close the stream. Should return zero.
int
Chapter 8: Mixing C and Prolog
147
8.5.2.2 Installing a New Stream
A new stream is made accessible to Prolog with the function SP_make_stream().
int SP_make_stream(
void *handle,
int (*sgetc)(),
int (*sputc)(),
int (*sflush)(),
int (*seof)(),
void (*sclrerr)(),
int (*sclose)(),
SP_stream **stream)
The function will:
Allocate a SP_stream structure
Install your low level I/O functions. For those not supplied default functions are installed.
Determine if the stream is for input or output or both from the functions supplied.
Fill in elds in the SP_stream structure with default values
The handle pointer will be supplied as the handle argument in the calls to the low level functions.
A stream without a close function will be treated as not closable i.e. close/1 will not have any
eect on it.
8.5.2.3 Internal Representation
For most streams you don't have to know anything about the internal representation but there may
be occasions when you have to set some elds manually or do some processing on all streams of a
particular type. SICStus Prolog maintain a circular list of stream objects of type SP_stream.
SP_stream *backward;
SP_stream *forward;
Used for linking streams together. The insertion is done by SP_make_stream() and
the deletion is done from the Prolog predicate close/1.
char *filename;
This eld is set to the empty string, "", by SP_make_stream(). May be set to a suitable
string, provided the string will not be overwritten until the stream is closed.
unsigned long mode;
A bit vector that contains information about the access modes supported, if the stream
is a TTY stream etc. It is not available to the user but the TTY property can be set
by the function:
void SP_set_tty(SP_stream *s)
148
SICStus Prolog
int fd;
The I/O descriptor if the stream is associated with a le, socket etc. Otherwise a
negative number.
void *user_handle;
This is the pointer to the user supplied private data for the stream. In the case of
SICStus Prolog predened le streams the user_handle could be a pointer to the
standard I/O FILE.
There is no standard way to tell if a stream is user dened. You have to save pointers to the streams
created or check if one of the stream functions installed is user dened, i.e:
int is_my_stream(SP_stream *s)
{
return (s->sclose == my_close);
}
8.5.3 Hookable Standard Streams
As of SICStus release 3.7, the standard I/O streams (input, output, and error) are hookable, i.e.
the streams can be redened by the user.
SP_UserStreamHook *SP_set_user_stream_hook(SP_UserStreamHook *hook)
Sets the user-stream hook to hook.
SP_UserStreamPostHook *SP_set_user_stream_post_hook(SP_UserStreamPostHook *hook)
Sets the user-stream post-hook to hook.
8.5.3.1 Writing User-stream Hooks
The user-stream hook is, if dened, called during SP_initialize(). SP_set_user_stream_hook()
and SP_set_user_stream_post_hook() must therefore be called before SP_initialize() is called.
It has the following prototype:
SP_stream *user_stream_hook(int which)
If the hook is not dened, SICStus will attempt to open the standard TTY/console versions of
these streams. If they are unavailable (such as for windowed applications under Windows), the
result is undened.
It is called three times, one for each stream. The which argument indicates which stream it is
called for. The value of which is one of:
SP_STREAMHOOK_STDIN
Create stream for standard input.
SP_STREAMHOOK_STDOUT
Create stream for standard output.
Chapter 8: Mixing C and Prolog
149
SP_STREAMHOOK_STDERR
Create stream for standard error.
The hook should return a standard SICStus I/O stream, as described in Section 8.5.2 [Dening a
New Stream], page 146.
8.5.3.2 Writing User-stream Post-hooks
The user-stream post-hook is, if dened, called after all the streams have been dened, once for
each of the three standard streams. It has a slightly dierent prototype:
void user_stream_post_hook(int which, SP_stream *str)
where str is a pointer to the corresponding SP_stream structure. There are no requirements as to
what this hook must do; the default behavior is to do nothing at all.
The post-hook is intended to be used to do things which may require that all streams have been
created.
8.5.3.3 User-stream Hook Example
This section contains an example of how to create and install a set of user-streams.
The hook is set by calling SP_set_user_stream_hook() in the main program like this:
SP_set_user_stream_hook((SP_UserStreamHook *)user_strhook);
Remember: SP_set_user_stream_hook() and
called before SP_initialize().
SP_set_user_stream_post_hook()
must be
The hook user_strhook() is dened like this:
SP_stream *user_strhook(int std_fd)
{
SP_stream *s;
SP_make_stream(NULL, my_getc, my_putc, my_flush, my_eof, my_clrerr, NULL, &s);
}
return s;
See Section 8.5.2.2 [Installing a New Stream], page 147 for a description on the parameters to
SP_make_stream().
150
SICStus Prolog
8.6 Muse Support Functions
Dynamic loading of foreign language functions must be done when the system is adjusted to one
worker. If you use dynamic linking, it is not recommended to use C-global or static local variables.
It is platform dependent if the variables are shared among workers or private. If static linking is
used, the variables become private.
Some useful Muse C-functions:
int muse_max_workers(void)
Returns the maximum number of workers. Also available as a read-only Muse ag.
int muse_num_workers(void)
Returns the actual number of allocated workers. Also available as a writable Muse ag.
int muse_worker_id(void)
Returns the worker identity as a non-negative integer. Also available as a read-only
Muse ag.
void muse_init_lock(int *lock)
void muse_lock(int *lock)
void muse_un_lock(int *lock)
The muse_lock and muse_un_lock
functions is used to implement mutual exclusion
regions. Note: the actual locks in Muse may not be int but they are of equal or smaller
size. A Muse lock must be initialized with muse_init_lock() before it can be used.
In Muse, dynamic memory allocation is performed in shared memory. To allocate worker private
memory, it is recommended (as worker_counters in Section 10.5.9 [Muse FLI Example], page 184)
to allocate an array of size muse_max_workers() that is indexed by the worker id (muse_worker_
id()).
8.7 Hooks
The user may dene functions to be called at certain occasions by the Prolog system. This is
accomplished by passing the functions as arguments to the following set-hook-functions. The
functions can be removed by passing a NULL.
typedef int (SP_ReadHookProc) (int fd)
SP_ReadHookProc SP_set_read_hook (SP_ReadHookProc *)
The installed function is called before reading a character from fd provided it is associated with a terminal device. This function shall return nonzero when there is input
available at fd. It is called repeatedly until it returns nonzero. Not available in Muse.
typedef void (SP_VoidFun) (void)
SP_VoidFun * SP_set_reinit_hook (SP_VoidFun *)
The installed function is called upon abort and reinitialization. The call is made
after SICStus Prolog's signal handler installation but before the built-in predicates
version/0 and initialization/0 are called. Calling Prolog from functions invoked
through this hook is not supported. (This hook is not available in Runtime Systems.)
Chapter 8: Mixing C and Prolog
151
typedef void (SP_VoidFun) (void);
SP_VoidFun * SP_set_interrupt_hook (SP_VoidFun *)
The installed function is called on occasions like expansion of stacks, garbage collection
and printouts, in order to yield control to special consoles etc. for interrupt checking.
Calling Prolog from functions invoked through this hook is not supported. (This hook
is not available in Runtime Systems.)
8.8 Runtime Systems
It is possible to mix C and Prolog code to create stand-alone applications, Runtime Systems. In a
Runtime System there are some dierences in the behavior of the Prolog engine and many built-in
predicates are omitted or have limited functionality:
No top level
No debugger
No compiler
No progress or error messages while loading les
No proling
No OR-parallelism
No redenition warnings
No signal handling except as installed by SP_signal()
Uncaught exceptions are returned to C
The predicates halt/0, abort/0 and reinitialise/0 will return to C
You have to supply a main program and explicitly initialize the Prolog engine with
initialize().
SP_
8.8.1 Initializing the Prolog Engine
You must call SP_initialize() before calling any other interface function. The function will
allocate data areas used by Prolog, initialize command line arguments so that they can be accessed
by the argv Prolog ag, and load the Runtime Library:
int SP_initialize(int argc, char **argv, char *boot_path)
boot_path should be the name of a directory, equivalent to `$SP_PATH/bin'. If boot_path is NULL,
SP_initialize() will look up the value of the environment variable SP_PATH and look for the le
`$SP_PATH/bin/sprt.sav' which contains the Runtime Library.
It returns SP_SUCCESS if initialization was successful, and SP_ERROR otherwise.
Optionally, you may also call SP_force_interactive() before calling SP_initialize(). This will
force the I/O built-in predicates to treat the standard input stream as a terminal, even if it does
not appear to be a terminal. Same as the `-i' option in Development Systems. (see Section 1.1
[Start], page 7).
152
SICStus Prolog
void SP_force_interactive(void)
Optionally, you may also call SP_set_memalloc_hooks() before calling SP_initialize(). This
will dene the bottom layer of Prolog's memory manager, in case your application has special
requirements.
typedef void *(SP_AllocHook)(unsigned int size,
unsigned int align,
unsigned int *actual_sizep);
typedef void *(SP_ReAllocHook)(void *ptr,
unsigned int oldsize,
unsigned int newsize,
unsigned int align,
unsigned int *actual_sizep);
typedef int (SP_FreeHook)(void *ptr,
unsigned int size);
void SP_set_memalloc_hooks(int usage,
SP_AllocHook *init_alloc_hook,
SP_AllocHook *alloc_hook,
SP_ReAllocHook *realloc_hook,
SP_FreeHook *free_hook)
The eect of SP_set_memalloc_hooks is controlled by the value of usage, which should be one of:
MM_USE_MALLOC
The bottom layer will be based on malloc()/free(). The other arguments are ignored.
Same as the `-m' option in Development Systems. (see Section 1.1 [Start], page 7).
MM_USE_SBRK
The bottom layer will be based on sbrk(). The default for UNIX; not available for
Windows. The other arguments are ignored.
MM_USE_SPARSE
The bottom layer will be based on VirtualAlloc()/VirtualFree(). The default for
Windows; not available for UNIX. The other arguments are ignored.
MM_USE_OTHER
The bottom layer will be based on the other arguments. Their meaning is explained
below.
In the latter case, the other arguments should be functions as specied below:
alloc_hook
must allocate and return a pointer to a piece of memory that has at least size bytes
aligned at align in it. align is guaranteed to be a power of 2. The actual size of
the piece of memory should be returned in *actual_sizep. Should return NULL if it
cannot allocate any more memory.
Chapter 8: Mixing C and Prolog
153
init_alloc_hook
is a special case of alloc_hook. It will be called initially whereas alloc_hook will
be called subsequently. It can do whatever initialization that this layer of memory
management wants to do.
realloc_hook
is called with a piece of memory to be resized and possibly moved. ptr is the pointer,
oldsize its current size. The function must allocate and return a pointer to a piece of
memory that has at least newsize bytes aligned at align in it, and that has the same
contents as the old block up to the lesser of oldsize and newsize. align is guaranteed
to be a power of 2. The actual size of the piece of memory should be returned in
*actual_sizep. Should return NULL if it cannot allocate any more memory, or if it
cannot reclaim the old block in a meaningful way. In that case, Prolog will use the
other functions.
free_hook
is called with a pointer to the piece of memory to be freed and its size. Should return
non-zero i the function was able to free this piece of memory. Otherwise, Prolog will
keep using the memory as if it were not freed.
The default bottom layers look at the environment variables PROLOGINITSIZE, PROLOGINCSIZE,
PROLOGKEEPSIZE and PROLOGMAXSIZE. They are useful to customize the default memory manager.
If users redene the bottom layer, they can choose to ignore these environment variables. See
Section 1.1.2 [Environment Variables], page 10.
8.8.2 Loading Prolog Code
You can load your Prolog code compiled to Prolog object les into the system with the call SP_
load(). This is the C equivalent of the Prolog predicate load/1:
int SP_load(char *filename)
Alternatively, you can restore a saved state with the call SP_restore(), which is the C equivalent
of the Prolog predicate restore/1:
int SP_restore(char *filename)
SP_load()
occurred.
and
SP_restore()
return
SP_SUCCESS
for success or
SP_ERROR
if an error condition
Prolog error handling is mostly done by raising and catching exceptions. However, some faults are
of a nature such that when they occur, the internal program state may be corrupted, and it is not
safe to merely raise an exception. Memory allocation failures are examples of faults. In Runtime
Systems, the following C macro provides an environment for handling faults:
int SP_on_fault(Stmt, Message, Cleanup )
which should occur in the scope of a char *Message declaration. Stmt is run, and if a fault
occurs, Stmt is aborted, Message gets assigned a value explaining the fault, the Prolog internal
154
SICStus Prolog
state is cleaned, all queries and SP term refs become invalid, and Cleanup run. If Stmt terminates
normally, Message is left unchanged. For example, a \fault-proof" Runtime System could have the
structure:
int main(int argc, char **argv)
{
char *message;
SP_initialize(argc, argv, "/usr/local/lib/sicstus37/bin");
loop:
SP_on_fault(main_loop(), message,
{printf("ERROR: %s\n",message); goto loop;});
exit(0);
}
main_loop()
{...}
Faults that occur outside the scope of SP_on_fault() cause the Runtime System to halt with an
error message.
The following function can be used to raise a fault. For example, it can be used in a signal handler
for SIGSEGV to prevent the program from dumping core in the event of a segmentation violation
(Runtime Systems have no predened signal handling):
void SP_raise_fault(char *message)
8.8.3 Creating the Executable
This describes how to create a Runtime System with a set of statically linked foreign resources.
For the SICStus library modules containing C-code, such linked foreign resources already exist in
`$SP_PATH/library'. For user foreign code, you must rst create linked foreign resources for them.
A utility command is provided for the generation of such systems. This command, located in
`$SP_PATH/bin', is congurable as described in the Release Notes.
spmkrs [--help] [-S]
Resources ]
Executable [+c SourceFiles ] [+o ObjectFiles ] [+l Libraries ] [+r
Generates a Runtime System with any foreign resources mentioned in Resources statically linked. The resources must be created with the +static option. Note however
that a statically linked dynamic resource is not, in general, statically linked from the
operating system's point of view. It must therefore be accessible at runtime as well as
at build-time.
Compiles SourceFiles and links the resulting object les with ObjectFiles, Libraries,
and the foreign resources to produce Executable. The main program of the Runtime
System must be provided by SourceFiles or ObjectFiles.
Chapter 8: Mixing C and Prolog
155
The option -S causes the SICStus Runtime Kernel to be statically linked into the
executable, thus eliminating the need to load these at runtime. This produces a significantly larger executable. Observe that this option has no eect on the resources and
libraries speed after by +r and +l. Note: This option is not available on Win32.
The option --help prints out usage info.
For example, to compile and link a Runtime System `ex_rt' with the linked foreign resource ex
statically linked, rst create the linked foreign resource (see Section 8.2.7 [Creating the Linked
Foreign Resource], page 135) using the +static option. Assuming the Runtime System's main
program is in the le `main.c', then type:
% spmkrs ex_rt +c main.c +r ex
If you prefer not to use the utility commands, an alternative method is described in Chapter 38
[Runtime Utilities], page 373.
8.8.4 Running the Executable
To execute a Runtime System on a target machine you need:
the executable
This is your application program created as described above.
the runtime kernel
This is a shared object or a DLL, usually `$SP_PATH/../libsprt37.so' under UNIX,
or `%SP_PATH%\..\sprt37.dll' under Windows.
the runtime library
The saved state `$SP_PATH/bin/sprt.sav' contains the built-in predicates written in
Prolog. It is restored into the program at runtime by the function SP_initialize().
your Prolog code
Either as a saved state, object code (`.ql' les), or source code (`.pl' les). They must
be explicitly loaded by the program at runtime.
your linked foreign resources
Any linked foreign resources which are not statically linked to the executable,
including any linked foreign resources for library modules which are located in
`$SP_PATH/library'.
Also, on most UNIX platforms, it is necessary to set an environment variable, typically LD_LIBRARY_
PATH, for shared objects to be found at runtime. See Section 1.1.1 [Finding Libraries at Runtime],
page 9.
156
SICStus Prolog
8.9 Development Systems
This describes how to create a Development System with a set of linked foreign resources. For
the SICStus library modules containing C-code, such linked foreign resources already exist in
`$SP_PATH/library'. For user foreign code, you must rst create linked foreign resources for
them.
A utility command is provided for the generation of such systems. This command, located in
`$SP_PATH/bin', is congurable as described in the Release Notes.
Executable [Resources ]
Generates a Development System with any foreign resources mentioned in Resources
statically linked. The resources must be created with the +static option. Note however
that a statically linked dynamic resource is not, in general, statically linked from the
operating system's point of view. It must therefore be accessible at runtime as well as
at build-time.
The option -S causes the SICStus Runtime Kernel and development system extensions
to be statically linked into the executable, thus eliminating the need to load these at
runtime. This produces a signicantly larger executable. Observe that this option has
no eect on the resources. Note: This option is not available on Win32.
The option --help prints out usage info.
spmkds [--help] [-S]
For example, to compile and link a Development System `ex_stat' with the linked foreign resource
ex statically linked, rst create the linked foreign resource (see Section 8.2.7 [Creating the Linked
Foreign Resource], page 135) using the +static option. Then type:
% spmkds ex_stat ex
The executable can then be run as:
% sicstus -base ./ex_stat
and optionally more command-line options as for the standard sicstus command.
The generated executable will run the usual Prolog top level, and the full develoment environment
is available as usual.
8.10 Examples
Chapter 8: Mixing C and Prolog
157
8.10.1 Train Example (connections)
This is an example of how to create a Runtime System. The Prolog program `train.pl' will display
a route from one train station to another. The C program `train.c' calls the Prolog code and
writes out all the routes found between two stations:
% train.pl
connected(From, To, Path) :connected(From, To, Path, [From]).
connected(To, To, [To], _).
connected(From, To, [From|Path], Visited) :(
connection(From, Via)
;
connection(Via, From)
),
not_visited(Visited, Via),
connected(Via, To, Path, [Via|Visited]).
connection('Stockholm', 'Katrineholm').
connection('Stockholm', 'Vasteras').
connection('Stockholm', 'Uppsala').
connection('Uppsala', 'Vasteras').
connection('Katrineholm', 'Hallsberg').
connection('Katrineholm', 'Linkoping').
connection('Hallsberg', 'Kumla').
connection('Hallsberg', 'Goteborg').
connection('Orebro', 'Vasteras').
connection('Orebro', 'Kumla').
not_visited([], _).
not_visited([X|Visited], Y) :- X \== Y, not_visited(Visited, Y).
/* train.c */
#include <stdio.h>
#include <sicstus/sicstus.h>
void write_path(SP_term_ref path)
{
char *text = NULL;
SP_term_ref
tail = SP_new_term_ref(),
via = SP_new_term_ref();
SP_put_term(tail,path);
while (SP_get_list(tail,via,tail))
{
158
SICStus Prolog
if (text)
printf(" -> ");
SP_get_string(via, &text);
printf("%s",text);
}
}
printf("\n");
int main(int argc, char **argv)
{
int rval;
SP_pred_ref pred;
SP_qid goal;
SP_term_ref from, to, path;
/* Initialize Prolog engine. This call looks up SP_PATH in order to
* find the Runtime Library. */
if (SP_FAILURE == SP_initialize(argc, argv, NULL))
{
fprintf(stderr, "SP_initialize failed: %s\n", SP_error_message(SP_errno));
exit(1);
}
rval = SP_restore("train.sav");
if (rval == SP_ERROR || rval == SP_FAILURE)
{
fprintf(stderr, "Could not restore \"train.sav\".\n");
exit(1);
}
/* Look up connected/3. */
if (!(pred = SP_predicate("connected",3,"")))
{
fprintf(stderr, "Could not find connected/3.\n");
exit(1);
}
/* Create the three arguments to connected/3. */
SP_put_string(from = SP_new_term_ref(), "Stockholm");
SP_put_string(to = SP_new_term_ref(), "Orebro");
SP_put_variable(path = SP_new_term_ref());
/* Open the query. In a development system, the query would look like:
*
* | ?- connected('Stockholm','Orebro',X).
*/
Chapter 8: Mixing C and Prolog
if (!(goal = SP_open_query(pred,from,to,path)))
{
fprintf(stderr, "Failed to open query.\n");
exit(1);
}
/*
* Loop through all the solutions.
*/
while (SP_next_solution(goal))
{
printf("Path: ");
write_path(path);
}
SP_close_query(goal);
}
exit(0);
Create the saved-state containing the Prolog code:
% sicstus
SICStus 3.7: Tue Jun 02 12:29:02 MET DST 1998
| ?- compile(train).
{compiling /home/jojo/sicstus/examples/train.pl...}
{/home/jojo/sicstus/examples/train.pl compiled, 40 msec 2848 bytes}
yes
| ?- save_program('train.sav').
{SICStus state saved in /home/jojo/sicstus/examples/train.sav}
yes
| ?- halt.
Create the executable using spmkrs:
% spmkrs train +c train.c
SICStus 3.7: Tue Jun 02 12:29:02 MET DST 1998
{/var/tmp/aaa0ch9LZ.c generated, 0 msec}
yes
And nally, run the executable:
% ./train
Path: Stockholm -> Katrineholm -> Hallsberg -> Kumla -> Orebro
Path: Stockholm -> Vasteras -> Orebro
Path: Stockholm -> Uppsala -> Vasteras -> Orebro
159
160
SICStus Prolog
8.10.2 I/O on Lists of Character Codes
This example is taken from the SICStus Prolog library (simplied, but operational). A stream for
writing is opened where the written characters are placed in a buer. When the stream is closed a
list of character codes is made from the contents of the buer. The example illustrates the use of
user denable streams.
The open_buf_stream() function opens a stream where the characters are put in a buer. The
stream is closed by stream_to_chars() which returns the list constructed on the heap.
The Prolog code (simplied):
foreign(open_buf_stream, '$open_buf_stream'(-address('SP_stream'))).
foreign(stream_to_chars, '$stream_to_chars'(+address('SP_stream'),
-term)).
foreign_resource(example, [open_buf_stream,stream_to_chars]).
:- load_foreign_resource(example).
%% with_output_to_chars(+Goal, -Chars)
%% runs Goal with current_output set to a list of characters
with_output_to_chars(Goal, Chars) :'$open_buf_stream'(StreamCode),
stream_code(Stream, StreamCode),
current_output(CurrOut),
set_output(Stream),
call_and_reset(Goal, Stream, CurrOut, StreamCode, Chars).
call_and_reset(Goal, Stream, CurrOut, StreamCode, Chars) :call(Goal), !,
put(0),
'$stream_to_chars'(StreamCode, Chars),
reset_stream(Stream, CurrOut).
call_and_reset(_, Stream, CurrOut, _, _) :reset_stream(Stream, CurrOut).
reset_stream(Stream, CurrOut) :set_output(CurrOut),
close(Stream).
The C code:
#include <sicstus/sicstus.h>
struct open_chars {
char *chars;
int index;
/* character buffer */
/* current insertion point */
Chapter 8: Mixing C and Prolog
int size;
};
#define INIT_BUFSIZE 512
static int lputc(c, buf)
int c;
struct open_chars *buf;
{
if (buf->index == buf->size) /* grow buffer if necessary */
{
buf->size *= 2;
buf->chars = (char *)realloc(buf->chars, buf->size);
}
return (buf->chars[buf->index++] = c);
}
static int lwclose(buf)
struct open_chars *buf;
{
free(buf->chars);
free(buf);
return 0;
}
void open_buf_stream(streamp)
SP_stream **streamp;
{
struct open_chars *buf;
/* Allocate buffer, create stream & return stream code */
buf = (struct open_chars *)malloc(sizeof(struct open_chars));
SP_make_stream(buf, NULL, lputc, NULL, NULL, NULL, lwclose,
streamp);
}
buf->chars = (char *)malloc(INIT_BUFSIZE);
buf->size = INIT_BUFSIZE;
buf->index = 0;
void stream_to_chars(streamp, head)
SP_stream *streamp;
SP_term_ref head;
{
SP_term_ref tail = SP_new_term_ref();
struct open_chars *buf = (struct open_chars *)streamp->user_handle;
161
162
SICStus Prolog
/* Make a list of character codes out of the buffer */
}
SP_put_string(tail, "[]");
SP_put_list_chars(head, tail, buf->chars);
8.10.3 Exceptions from C
Consider, for example, a function which returns the square root of its argument after checking that
the argument is valid. If the argument is invalid, the function should raise an exception instead.
/* math.c */
#include <math.h>
#include <stdio.h>
#include <sicstus/sicstus.h>
double sqrt_check(d)
double d;
{
if (d < 0.0)
{
/* build a domain_error/4 exception term */
SP_term_ref culprit=SP_new_term_ref();
SP_term_ref argno=SP_new_term_ref();
SP_term_ref expdomain=SP_new_term_ref();
SP_term_ref t1=SP_new_term_ref();
SP_put_float(culprit, d);
SP_put_integer(argno, 1);
SP_put_string(expdomain, ">=0.0");
SP_cons_functor(t1, SP_atom_from_string("sqrt"), 1, culprit);
SP_cons_functor(t1, SP_atom_from_string("domain_error"), 4,
t1, argno, expdomain, culprit);
SP_raise_exception(t1);
/* raise the exception */
return 0.0;
}
}
return sqrt(d);
The Prolog interface to this function is dened in a le `math.pl'. The function uses the sqrt()
library function, and so the math library `-lm' has to be included:
/* math.pl */
foreign_resource(math, [sqrt]).
foreign(sqrt_check, c, sqrt(+float, [-float])).
Chapter 8: Mixing C and Prolog
163
:- load_foreign_resource(math).
A linked foreign resource is created:
% splfr math math.pl +c math.c +l -lm
A simple session using this function could be:
% sicstus
SICStus 3 #5: Sat Feb 24 00:35:37 MET 1996
| ?- [math].
{consulting /home/san/pl/math.pl...}
{/home/san/pl/math.pl consulted, 10 msec 816 bytes}
yes
| ?- sqrt(5.0,X).
X = 2.23606797749979 ?
yes
| ?- sqrt(a,X).
{TYPE ERROR: sqrt(a,_30) - arg 1: expected number, found a}
| ?- sqrt(-5,X).
{DOMAIN ERROR: sqrt(-5.0) - arg 1: expected '>=0.0', found -5.0}
The above example used the foreign language interface with dynamic linking. To statically link
`math.o' with the Prolog emulator, the following steps would have been taken:
% splfr math math.pl +static +c math.c +l -lm
SICStus 3 #5: Sat Feb 24 00:35:37 MET 1996
{/tmp/00249aaa.c generated, 10 msec}
yes
% spmkds mathsp math
SICStus 3 #5: Sat Feb 24 00:35:37 MET 1996
{/tmp/00274aaa.c generated, 0 msec}
yes
% sicstus -base ./mathsp
SICStus 3 #5: Sat Feb 24 00:35:37 MET 1996
| ?- [math].
{consulting /home/san/pl/math.pl...}
{/home/san/pl/math.pl consulted, 0 msec 864 bytes}
yes
| ?- sqrt(5.0,X).
X = 2.23606797749979 ?
164
SICStus Prolog
yes
8.10.4 Stream Example
This is a small example how to initialize a bidirectional socket stream (error handling omitted):
typedef struct {
int fd;
FILE *r_stream;
FILE *w_stream;
} SocketData;
/* socket number */
/* For reading */
/* For writing */
int socket_sgetc(SocketData *socket)
{
return fgetc(socket->r_stream);
}
int socket_sputc(char c, SocketData *socket)
{
return fputc(c, socket->w_stream);
}
int socket_sflush(SocketData *socket)
{
return fflush(socket->w_stream);
}
int socket_seof(SocketData *socket)
{
return feof(socket->r_stream);
}
void socket_sclrerr(SocketData *socket)
{
clearerr(socket->r_stream);
clearerr(socket->w_stream);
}
int socket_sclose(SocketData *socket)
{
fclose(socket->r_stream);
fclose(socket->w_stream);
close(socket->fd);
free(socket);
return 0;
}
Chapter 8: Mixing C and Prolog
SP_stream *new_socket_stream(int fd)
{
SP_stream *stream;
SocketData *socket;
/* Allocate and initialize data local to socket */
socket = (SocketData *)malloc(sizeof(SocketData));
socket->fd = fd;
socket->r_stream = fdopen(fd,"r");
socket->w_stream = fdopen(fd,"w");
/* Allocate and initialize Prolog stream */
SP_make_stream(
socket,
socket_sgetc,
socket_sputc,
socket_sflush,
socket_seof,
socket_sclrerr,
socket_sclose,
&stream);
/* Allocate and copy string */
stream->filename = "socket";
stream->fd = fd;
}
return stream;
165
166
SICStus Prolog
Chapter 9: Mixing Java and Prolog
167
9 Mixing Java and Prolog
Jasper is a bi-directional interface between programs written in Java and programs written in
Prolog. The Java-side of the interface constists of a Java package (jasper) containing classes
representing the SICStus emulator. The Prolog part is designed as an extension to the foreign
language interface, which means that foreign/3 declarations are used to map Java-methods on
Prolog predicates and provide automatic conversion of arguments.
Jasper can be operated in two dierent ways, depending on which emulator is used as top-level
application; the Java Runtime System or the SICStus Development System. When the Java Runtime System is used as a top-level application, the SICStus runtime kernel is loaded into the Java
Runtime System. When the Java Runtime System is loaded into the SICStus Development System,
it is loaded as any other foreign resource, i.e. by using load_foreign_resource/1. This distinction
is not visible to the programmer, but it is useful to have a picture of how the interface works.
Note: Some of the information in this chapter is a recapitulation of the information in the chapter
Chapter 8 [Mixing C and Prolog], page 127. The intention is that this chapter should be possible
to read fairly independently.
9.1 Prerequisites
The low-level interface uses the JNI (Java Native Interface) to call C functions from Java. The
JNI is a native interface standard developed by Sun Microsystems. See Section 9.7 [Java Resources
Online], page 176. Since the interface uses the JNI, it is necessary that the Java VM which is to be
used has support for JNI. Even though the JNI is intended as a standard, not all vendors support
it. If your Java installation does not support the JNI, Sun's Java Runtime Environment provides
a minimal execution enviroment for running Java code. It can be downloaded from
http://java.sun.com/products/jdk/1.1/jre/index.html
The rest of this chapter assumes that there is a Java installation with JNI support available.
9.2 Calling Java from Prolog
Java methods are called from Prolog much in the same way as C functions are called (see Section 8.2
[Calling C], page 128); by creating a foreign resource. When loaded, this resource installs a set of
predicates which are mapped onto Java-methods and when invoked, converts the Prolog arguments
to the corresponding Java-types before calling the Java method itself.
In fact, a foreign resource (as dened in Section 8.2.1 [Foreign Resource], page 128) is not language
specic itself. The language is instead specied in the second argument to the foreign/3 fact and
it is possible to mix foreign C functions with foreign Java methods.
How a foreign resource is created in general is described in detail in Section 8.2.7 [Creating the
Linked Foreign Resource], page 135. The following section(s) will focus on the Java-specic parts
of foreign resources.
168
SICStus Prolog
9.2.1 Static and Dynamic Linking
There is no support for static linking of foreign resources containing Java declarations, since Java
implementations usually do not support static linking.
9.2.2 Declarating Java-methods
Java-methods are declared similarly to C-functions. There are two major dierences. The rst
is how methods are identied. It is not enough to simply use an atom as the C interface does.
Instead, a term method/3 is introduced:
method(+ClassName,+MethodName,+Flags )
Used as rst argument to foreign/3 when declaring Java
methods. The rst argument
is an atom containing the Fully Qualied Classname of the class. The second argument
is the method name. The third argument is a list of ags. Possible ags are instance
or static, indicating whether or not the method is static or non-static. Non-static
methods must have an object-reference as their rst argument. This is a reference to
the object on which the method will be invoked.
This term is then used to identify the method in the foreign_resource/2 predicate.
So, to dene a foreign resource exporting the non-static Java method getFactors in
the class PrimeNumber in the package numbers, the method/3 term would look like
method('numbers/PrimeNumber','getFactors',[instance])
The syntax for foreign/3 is the basically the same as for C-functions:
foreign(+MethodIdentier, java, +Predicate )
A hook predicate, species the Prolog interface to a Java method. MethodIdentier is
method/3 term as described above. Predicate species the name of the Prolog predicate
that will be used to call MethodIdentier. Predicate also species how the predicate
arguments are to be translated into the corresponding Java arguments.
9.3 Conversions between Prolog Arguments and Java Types
The following table lists the possible values of arguments of the predicate specication to foreign/3.
The value declares which conversion between corresponding Prolog argument and Java type will
take place.
Note: The conversion declarations (composed of the declarators specied below) together with the
method/3 term are used by the glue-code generator to create the method's type signature, i.e. a
string which can uniquely identify a method within a class. This means that unlike the C interface,
the conversion declarations for a Java method will aect the lookup of the method-name (in the C
interface, only the function name is relevant). So, if a method is declared as foo(+integer), there
must be a method which has the name foo and takes one argument of type int, or an argument
which can be automatically converted to an int (a short, for example).
Chapter 9: Mixing Java and Prolog
169
Prolog: +integer
Java: int
The argument should be a number. It is converted to a Java int.
Prolog: +byte
Java: byte
The argument should be a number. It is converted to a Java byte.
Prolog: +short
Java: short
The argument should be a number. It is converted to a Java short.
Prolog: +long
Java: long
The argument should be a number. It is converted to a Java long.
Note: Since Java's long type is 64 bits wide and there is no standardized support for
64 bits integers in C, the value will be truncated. So, this declaration is really only
useful in order to indicate which method should be used. For example:
class Bar
{
void foo(int x)
{ ... }
}
void foo(long x)
{ ... }
In order to be able to indicate that the latter of the foo methods should be called, a
+long declaration must be used, even if the value itself will be truncated in the call.
Prolog: +float
Java: float
The argument should be a number. It is converted to a Java float.
Prolog: +double
Java: double
The argument should be a number. It is converted to a Java double.
Prolog: +term
Java: SPTerm
The argument can be any term. It is passed to Java as an object of the class SPTerm.
Prolog: +object
Java: SPTerm
The argument should be a the Prolog representation of a Java object. Unless it is the
rst argument in a non-static method (in which case is it treated as the object on which
the method should be invoked), it is passed to the Java method as an object of class
SPTerm.
Prolog: +atom
170
SICStus Prolog
Java: SPTerm
The argument should be an atom. The Java method will be passed an object of class
SPTerm.
Prolog: +boolean
Java: boolean
The argument should be an atom in {true,false}. The Java method will receive a
boolean.
Prolog: +chars
Java: String
The argument should be a list of character codes. The Java method will receive an
object of class String.
Prolog: +string
Java: String
The argument should be an atom. The Java method will receive an object of class
String.
Prolog: -atom
Java: SPTerm
The argument should be an unbound variable. The Java method will receive an atom
of class SPTerm which can be modied. The argument will be bound to the value of
the atom when the method returns.
Prolog: -chars
Java: StringBuffer
The argument should be an unbound variable. The Java method will receive an object
of type StringBuffer which can be modied. The argument will be bound to a list of
the character codes of the StringBuffer object.
Prolog: -string
Java: StringBuffer
The argument should be an unbound variable. The Java method will receive an object
of type StringBuffer which can be modied. The argument will be bound to an atom
converted from the StringBuffer object.
Prolog: [-integer]
Java: int
M ()
The Java method should return an int. The value will be converted to a Prolog integer.
Prolog: [-byte]
Java: byte
M ()
The Java method should return a byte. The value will be converted to a Prolog integer.
Prolog: [-short]
M ()
The Java method should return a
integer.
Java: short
short.
The value will be converted to a Prolog
Chapter 9: Mixing Java and Prolog
171
Prolog: [-long]
Java: long
M ()
The Java method should return a
truncated to a Prolog integer.
long.
The value will be converted and possibly
Prolog: [-float]
M ()
The Java method should return a float. The value will be converted to a Prolog oat.
Java: float
Prolog: [-double]
M ()
The Java method should return a double. The value will be converted to a Prolog
oat.
Java: double
Prolog: [-term]
M ()
The Java method should return an object of class SPTerm which will be converted to a
Prolog term.
Java: SPTerm
Prolog: [-object]
M ()
The Java method should return an object of class SPTerm which will be converted to
the internal Prolog representation of a Java object.
Java: SPTerm
Prolog: [-atom]
M ()
The Java method should return an object of class SPTerm which will be converted to a
Prolog atom.
Java: SPTerm
Prolog: [-boolean]
M ()
The Java should return a boolean. The value will be converted to a Prolog atom in
{true,false}.
Java: boolean
Prolog: [-chars]
M ()
The Java method should return an object of class String which will be converted to a
list of character codes.
Java: String
Prolog: [-string]
M ()
The Java method should return an object of class String which will be converted to
an atom.
Java: String
172
SICStus Prolog
9.3.1 Calling Java from Prolog: An Example
The following is an simple, but complete example of how a Java method can be called from Prolog.
First, we must write the resource le. Let us call it `simple.pl'.
% File: simple.pl
:- module(simple, [simple/2]).
:- use_module(library(jasper)).
:- load_foreign_resource(simple).
foreign(method('Simple', 'simpleMethod', [static]), java,
simple(+integer,[-integer])).
foreign_resource(simple,
[
method('Simple', 'simpleMethod', [static])
]).
This le is the processed with the script splfr (see Section 8.2.5 [Interface Predicates], page 133)
to produce a foreign resource:
% splfr simple simple.pl
SICStus 3.7: Mon Mar 2 19:22:44 MET 1998
{/var/tmp/aaaa004Cd.c generated, 20 msec}
yes
Note that we do not specify any Java les to splfr as we would specify C les when building
foreign resources for C code. This is because the C code can be compiled into the resource itself,
while the Java code must be loaded at runtime into the JVM. This means that the resource will
only contain the glue-code for calling the JVM, and no actual Java code. Hence, these resources
are usually quite small.
Note also that the use of +dynamic is implicit and obligatory; resources with Java-calls must be
dynamic.
Now, we need some Java code to call:
Simple.java:
public class Simple
{
static int simpleMethod(int value)
{
return value*42;
}
}
Chapter 9: Mixing Java and Prolog
173
This Java code must now be compiled. Refer to the documentation of your Java implementation
exactly how to do this. On Solaris, this might look like:
% javac Simple.java
Now we are ready to call the method simple/2 from inside SICStus.
% sicstus
SICStus 3.7: Mon Mar 2 19:22:44 MET 1998
| ?- compile(simple).
{compiling ...}
[...]
{compiled ... simple.pl in module simple, 160 msec 48640 bytes}
yes
| ?- simple(17,X).
X = 714 ?
yes
| ?-
What has happened is that the predicate simple/2 has been installed as a predicate dened in
Java (this is not exactly true; the predicate is dened as a C-function which calls the Java method).
When we load the simple module, we will rst load the jasper module (and thereby the JVM)
and then load the simple foreign resource, which denes the simple/2 predicate.
9.4 Calling Prolog from Java
Calling Prolog from Java is done by using the Java package jasper. This package contains a set
of Java classes which can be used to create and manipulate terms, ask queries and request one
or more solutions. The functionality provided by this set of classes is basically the same as the
functionality provided by the C-Prolog interface (see Chapter 8 [Mixing C and Prolog], page 127).
The usage is easiest described by an example. The following is a Java version of the train example.
See Section 8.10.1 [Train], page 157.
import jasper.*;
public class Simple
{
public static void main(String argv[]) {
SICStus sp;
SPPredicate pred;
SPTerm from, to, way;
SPQuery query;
int i;
174
SICStus Prolog
try
{
sp = new SICStus(argv,null);
sp.load("train.ql");
pred = new SPPredicate(sp, "connected", 4, "");
to = new SPTerm(sp, "Orebro");
from = new SPTerm(sp, "Stockholm");
way = new SPTerm(sp).putVariable();
query = sp.openQuery(pred, new SPTerm[] { from, to, way, way });
while (query.nextSolution())
{
System.out.println(way.toString());
}
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
This is how it works:
1. First, we import all the jasper-classes. To nd them, the class-path should be set to contain
the path to the SICStus libraries ($SP_PATH/library). How this is done depends on the operating system and which Java implementation is used, but typically there is a environment
variable CLASSPATH which contains the path. The path can often also be set on the commandline using an option such as `-classpath'. Refer to your Java documentation for more
details.
2. Before any predicates can be called, the SICStus emulator must be initialized. This is done by
instantiating the SICStus class. NOTE: This class must only be instantiated once per Java
process. Multiple SICStus-objects are not supported.
Most methods take a reference to this object as their rst argument. This is implicit in the
rest of this chapter, unless otherwise stated.
3. The next step is to load the Prolog code. This is done by the method load. Corresponds to
SP_load() in the C-interface. See Section 8.8.2 [Loading Prolog Code], page 153.
4. Now, everything is set up to start making queries. In order to make a query, the actual query
term must be created. This is done by creating an object of the SPPredicate class:
SPPredicate pred = new SPPredicate(sp, "connected", 4, "");
Chapter 9: Mixing Java and Prolog
175
5. At this point, we have created a predicate object for the predicate connected/4. It is now
time to create the arguments for the query. The arguments are placed in an array which is
passed to a suitable method to make the query.
The arguments consist of objects of the class SPTerm. For example, if we need two atoms and
a variable for the query
| ?- connected('Stockholm', 'Orebro', X, X).
the following Java code will do it for us:
to = new SPTerm(sp, "Orebro");
from = new SPTerm(sp, "Stockholm");
way = new SPTerm(sp).putVariable();
6. Now it is time to make the query. As in the C-Prolog interface, there are three ways of making
a query.
query(pred, args)
This method is useful if you are only interested in nding the rst solution to a
goal. In the case of connected/4 this is not the case; there are more than one
solution.
queryCutFail(pred, args)
This method is useful if you are only interested in the side-eects of the query. As
query() it only nds the rst solution, and then it cuts away all other solutions
and fails.
openQuery(pred, args)
This method is useful when you are interested in some or all solutions to the query.
Since the connected/4 may give us multiple solution, this is what we will use.
SPQuery query;
query = sp.openQuery(pred, new SPTerm[] { from, to, way, way });
while (query.nextSolution())
System.out.println(way.toString());
The openQuery method returns a reference to the query, an object of the SPQuery
class. To obtain solutions, the method nextSolution() is called with no arguments. nextSolution returns true as long as there are more solutions, the
example above will print the value of way until there are no more solutions.
9.5 Jasper Package Class Reference
Detailed documentation of the classes in the jasper package can be found at
http://www.sics.se/isl/sicstus/jasper/Package-jasper.html
This documentation will most probably be included in a future version of the manual.
176
SICStus Prolog
9.6 Exception Handling
Exceptions are handled seamlessly between Java and Prolog. This means that exceptions can be
thrown in Prolog and caught in Java and the other way around. For example, if a predicate called
from Java raises an exception by raise_exception/1 and the predicate itself does not catch the
exception, the Java-method which performed the query, queryCutFail() for example, will throw an
exception containing the exception term. Symmetrically, a Java-exception thrown (and not caught)
in a method called from Prolog will cause the corresponding predicate (simple/2 in the example
above) to raise an exception containing the exception object (in the internal Prolog representation
of a Java object).
9.7 Resources
There are almost innitely many Java resources on the Internet. Here is a list of a few which are
related to Jasper:
The SICStus Prolog Release Notes contains information useful in order to get Jasper working.
There is a collection of Java-to-Prolog examples in
library('jasper/examples')
See the le `README' for details on how to use the examples.
The main Java resource is Sun's own Java homepage:
http://java.sun.com
Yahoo:
http://www.yahoo.com/Computers_and_Internet/Programming_Languages/Java/
Information about the JNI can be found at:
http://java.sun.com/products/jdk/1.1/docs/guide/jni/index.html
The ACM student magazine Crossroads has published an article on the JNI, available online
at:
http://www1.acm.org:82/crossroads/xrds4-2/jni.html
Chapter 10: Programming Tips and Examples
177
10 Programming Tips and Examples
This chapter describes how to write clean programs that will execute eciently. To some extent,
writing ecient code in any language requires basic knowledge of its compiler, and we will mention
some important properties of the SICStus Prolog compiler. A number of simple examples of Prolog
programming are also given.
10.1 Programming Guidelines
A lot of clarity and eciency is gained by sticking to a few basic rules. This list is necessarily very
incomplete. The reader is referred to textbooks such as [O'Keefe 90] for a thorough exposition of
the elements of Prolog programming style and techniques.
Don't write code in the rst place if there is a library predicate that will do the job.
Write clauses representing base case before clauses representing recursive cases.
Input arguments before output arguments in clause heads and goals.
Use pure data structures instead of data base changes.
Use cuts sparingly, and only at proper places (see Section 2.5 [Cut], page 36). A cut should
be placed at the exact point that it is known that the current choice is the correct one: no
sooner, no later.
Make cuts as local in their eect as possible. If a predicate is intended to be deterministic,
dene it as such; do not rely on its callers to prevent unintended backtracking.
Binding output arguments before a cut is a common source of programming errors, so don't
do it.
Replace cuts by if-then-else constructs if the test is simple enough (see Section 10.4 [If-ThenElse], page 178).
Use disjunctions sparingly, always put parentheses around them, never put parentheses around
the individual disjuncts, never put the `;' at the end of a line.
Write the clauses of a predicate so that they discriminate on the principal functor of the rst
argument (see below). For maximum eciency, avoid \defaulty" programming (\catch-all"
clauses).
Don't use lists ([...]), \round lists" ((...)), or braces ({...}) to represent compound terms,
or \tuples", of some xed arity. The name of a compound term comes for free.
178
SICStus Prolog
10.2 Indexing
The clauses of any predicate are indexed according to the principal functor of the rst argument in
the head of the clause. This means that the subset of clauses which match a given goal, as far as the
rst step of unication is concerned, is found very quickly, in practically constant time. This can
be very important where there is a large number of clauses for a predicate. Indexing also improves
the Prolog system's ability to detect determinacy|important for conserving working storage, and
strongly related to last call optimization (see below).
Indexing applies to interpreted clauses as well as to compiled clauses.
10.3 Last Call Optimization
The compiler incorporates last call optimization to improve the speed and space eciency of determinate predicates.
When execution reaches the last goal in a clause belonging to some predicate, and provided there
are no remaining backtrack points in the execution so far of that predicate, all of the predicate's
local working storage is reclaimed before the nal call, and any structures it has created become
eligible for garbage collection. This means that programs can now recurse to arbitrary depths
without necessarily exceeding core limits. For example:
cycle(State) :- transform(State, State1), cycle(State1).
where transform/2 is a determinate predicate, can continue executing indenitely, provided each
individual structure, State, is not too large. The predicate cycle is equivalent to an iterative loop
in a conventional language.
To take advantage of last call optimization one must ensure that the Prolog system can recognize
that the predicate is determinate at the point where the recursive call takes place. That is, the
system must be able to detect that there are no other solutions to the current goal to be found by
subsequent backtracking. In general this involves reliance on the Prolog compiler's indexing and/or
use of cut; see Section 2.5 [Cut], page 36.
10.4 If-Then-Else Compilation
Ordinary disjunction, (P ;Q ), is treated by the compiler as an anonymous predicate with two
clauses, and the execution of a disjunction relies on backtracking to explore the two disjuncts.
If-then-else statements of the form:
(If -> Then; Else )
are recognized by the compiler and are under certain conditions compiled to code that is much
more ecient than the corresponding disjunction, essentially turning the If test to a conditional
jump and often avoiding costly backtracking altogether.
Chapter 10: Programming Tips and Examples
179
For this optimization to be eective, the test must be a conjunction of a restricted set of built-in
predicates (roughly, arithmetic tests, type tests and term comparisons).
This optimization is actually somewhat more general than what is described above. A sequence of
guarded clauses:
Head1 :- Guard1, !, Body1.
...
Headm :- Guardm,
Headn :- Bodym.
!,
Bodym.
is eligible for the same optimization, provided that the arguments of the clause heads are all unique
variables and that the \guards" are simple tests as described above.
10.5 Programming Examples
The rest of this chapter contains a number of simple examples of Prolog programming, illustrating
some of the techniques described above.
10.5.1 Simple List Processing
The goal concatenate(L1,L2,L3 ) is true if list L3 consists of the elements of list L1 concatenated
with the elements of list L2. The goal member(X,L) is true if X is one of the elements of list L.
The goal reverse(L1,L2 ) is true if list L2 consists of the elements of list L1 in reverse order.
concatenate([], L, L).
concatenate([X|L1], L2, [X|L3]) :- concatenate(L1, L2, L3).
member(X, [X|_]).
member(X, [_|L]) :- member(X, L).
reverse(L, L1) :- reverse_concatenate(L, [], L1).
reverse_concatenate([], L, L).
reverse_concatenate([X|L1], L2, L3) :reverse_concatenate(L1, [X|L2], L3).
10.5.2 Family Example (descendants)
The goal descendant(X,Y ) is true if Y is a descendant of X.
180
SICStus Prolog
descendant(X, Y) :- offspring(X, Y).
descendant(X, Z) :- offspring(X, Y), descendant(Y, Z).
offspring(abraham, ishmael).
offspring(abraham, isaac).
offspring(isaac, esau).
offspring(isaac, jacob).
If for example the query
| ?- descendant(abraham, X).
is executed, Prolog's backtracking results in dierent descendants of Abraham being returned as
successive instances of the variable X, i.e.
X
X
X
X
=
=
=
=
ishmael
isaac
esau
jacob
10.5.3 Association List Primitives
These predicates implement \association list" primitives. They use a binary tree representation.
Thus the time complexity for these predicates is O(lg N), where N is the number of keys. These
predicates also illustrate the use of compare/3 (see Section 7.3 [Term Compare], page 97) for case
analysis.
The goal get_assoc(Key, Assoc, Value ) is true when Key is identical to one of the keys in Assoc,
and Value unies with the associated value.
get_assoc(Key, t(K,V,L,R), Val) :compare(Rel, Key, K),
get_assoc(Rel, Key, V, L, R, Val).
get_assoc(=, _, Val, _, _, Val).
get_assoc(<, Key, _, Tree, _, Val) :get_assoc(Key, Tree, Val).
get_assoc(>, Key, _, _, Tree, Val) :get_assoc(Key, Tree, Val).
The goal put_assoc(Key, OldAssoc, Val, NewAssoc ) is true when OldAssoc and NewAssoc dene the same mapping for all keys other than Key, and get_assoc(Key, NewAssoc, Val ) is true.
Chapter 10: Programming Tips and Examples
181
put_assoc(Key, t, Val, Tree) :- !, Tree = t(Key,Val,t,t).
put_assoc(Key, t(K,V,L,R), Val, New) :compare(Rel, Key, K),
put_assoc(Rel, Key, K, V, L, R, Val, New).
put_assoc(=, Key, _, _, L, R, Val, t(Key,Val,L,R)).
put_assoc(<, Key, K, V, L, R, Val, t(K,V,Tree,R)) :put_assoc(Key, L, Val, Tree).
put_assoc(>, Key, K, V, L, R, Val, t(K,V,L,Tree)) :put_assoc(Key, R, Val, Tree).
10.5.4 Dierentiation
The goal d(E1, X, E2 ) is true if expression E2 is a possible form for the derivative of expression
E1 with respect to X.
:- op(300, xfy, **). /* binds tighter than * */
d(X, X, D) :- atomic(X), !, D = 1.
d(C, X, D) :- atomic(C), !, D = 0.
d(U+V, X, DU+DV) :- d(U, X, DU), d(V, X, DV).
d(U-V, X, DU-DV) :- d(U, X, DU), d(V, X, DV).
d(U*V, X, DU*V+U*DV) :- d(U, X, DU), d(V, X, DV).
d(U**N, X, N*U**N1*DU) :- integer(N), N1 is N-1, d(U, X, DU).
d(-U, X, -DU) :- d(U, X, DU).
10.5.5 Use of Meta-Logical Predicates
This example illustrates the use of the meta-logical predicates var/1, arg/3, and functor/3 (see
Section 7.7 [Meta Logic], page 110). The procedure call variables(Term, L, []) instantiates
variable L to a list of all the variable occurrences in the term Term. e.g.
| ?- variables(d(U*V, X, DU*V+U*DV), L, []).
L = [U,V,X,DU,V,U,DV]
182
SICStus Prolog
variables(X, [X|L0], L) :- var(X), !, L = L0.
variables(T, L0, L) :%
nonvar(T),
functor(T, _, A),
variables(0, A, T, L0, L).
variables(A, A, _, L0, L) :- !, L = L0.
variables(A0, A, T, L0, L) :%
A0<A,
A1 is A0+1,
arg(A1, T, X),
variables(X, L0, L1),
variables(A1, A, T, L1, L).
10.5.6 Use of Term Expansion
This example illustrates the use of user:term_expansion/(2,4) to augment the built-in predicate
expand_term/2 which works as a lter on the input to compile and consult. The code below will
allow the declaration `:- wait f/3' as an alias for `:- block f(-,?,?)'. Wait declarations were
used in previous versions of SICStus Prolog.
Note the multifile declaration, which prevents this user:term_expansion/(2,4) clause from
erasing any other clauses for the same predicate that might have been loaded.
:- op(1150, fx, [wait]).
:- multifile user:term_expansion/2.
user:term_expansion((:- wait F/N), (:- block Head)) :functor(Head, F, N),
wb_args(N, Head).
wb_args(1, Head) :- !, arg(1, Head, -).
wb_args(N, Head) :%
N>1,
arg(N, Head, ?),
N1 is N-1,
wb_args(N1, Head).
10.5.7 Prolog in Prolog
This example shows how simple it is to write a Prolog interpreter in Prolog, and illustrates the use
of a variable goal. In this mini-interpreter, goals and clauses are represented as ordinary Prolog data
structures (i.e. terms). Terms representing clauses are specied using the predicate my_clause/1,
e.g.
Chapter 10: Programming Tips and Examples
183
my_clause( (grandparent(X, Z) :- parent(X, Y), parent(Y, Z)) ).
A unit clause will be represented by a term such as
my_clause( (parent(john, mary) :- true) ).
The mini-interpreter consists of three clauses:
execute((P,Q)) :- !, execute(P), execute(Q).
execute(P) :- predicate_property(P, built_in), !, P.
execute(P) :- my_clause((P :- Q)), execute(Q).
The second clause enables the mini-interpreter to cope with calls to ordinary Prolog predicates,
e.g. built-in predicates. The mini-interpreter needs to be extended to cope with the other control
structures, i.e. !, (P;Q), (P->Q), (P->Q;R), (\+ P), and if(P,Q,R).
10.5.8 Translating English Sentences into Logic Formulae
The following example of a denite clause grammar denes in a formal way the traditional mapping
of simple English sentences into formulae of classical logic. By way of illustration, if the sentence
Every man that lives loves a woman.
is parsed as a sentence by the call
| ?- phrase(sentence(P ),
[every,man,that,lives,loves,a,woman]).
then P will get instantiated to
all(X):(man(X)&lives(X) => exists(Y):(woman(Y)&loves(X,Y)))
where :, & and => are inx operators dened by
:- op(900, xfx, =>).
:- op(800, xfy, &).
:- op(300, xfx, :).
The grammar follows:
184
SICStus Prolog
sentence(P) --> noun_phrase(X, P1, P), verb_phrase(X, P1).
noun_phrase(X, P1, P) -->
determiner(X, P2, P1, P), noun(X, P3), rel_clause(X, P3, P2).
noun_phrase(X, P, P) --> name(X).
verb_phrase(X, P) --> trans_verb(X, Y, P1), noun_phrase(Y, P1, P).
verb_phrase(X, P) --> intrans_verb(X, P).
rel_clause(X, P1, P1&P2) --> [that], verb_phrase(X, P2).
rel_clause(_, P, P) --> [].
determiner(X, P1, P2, all(X):(P1=>P2)) --> [every].
determiner(X, P1, P2, exists(X):(P1&P2)) --> [a].
noun(X, man(X)) --> [man].
noun(X, woman(X)) --> [woman].
name(john) --> [john].
trans_verb(X, Y, loves(X,Y)) --> [loves].
intrans_verb(X, lives(X)) --> [lives].
10.5.9 Muse FLI Example
This example illustrates the techniques used in foreign language interface programming in Muse.
Let us assume we want to count the number of solutions of some problem, as well as the number of
solutions produced by each worker. The following C le, `count.c', denes some procedures that
can be used for this purpose.
#include <sicstus/sicstus.h>
static int *overall_counter_lock;
static int *worker_counters, *overall_counter;
void allocate_counters()
{
overall_counter_lock = (int *)SP_malloc(sizeof(int));
overall_counter = (int *)SP_malloc(sizeof(int));
worker_counters = (int *)SP_malloc(muse_max_workers()*sizeof(int));
}
Chapter 10: Programming Tips and Examples
185
void init_counters()
{
int i;
muse_init_lock(overall_counter_lock);
*overall_counter = 0;
}
for (i=0; i<muse_num_workers(); i++)
worker_counters[i]=0;
long get_counter(c)
long c;
{
if (c >= 0) return worker_counters[c];
else
return *overall_counter;
}
long incr_counter()
{
int i;
muse_lock(overall_counter_lock);
i = ++(*overall_counter);
muse_un_lock(overall_counter_lock);
++(worker_counters[muse_worker_id()]);
}
return i;
We have dened here an overall_counter and an array of worker_counters. There is a lock,
overall_counter_lock, ensuring the atomicity of incrementing the overall_counter. The code
allocate_counter allocates the counters in shared memory. The init_counter procedure initializes all counters, while get_counters retrieves the current value of a counter associated with a
worker (non-negative argument), or the value of the overall_counter (negative argument). The
incr_counter procedure increments the counter for the worker, as well as the overall counter.
The following Muse code, `count.pl', makes use of the above C routines to accomplish the task of
counting the solutions of some goal.
foreign_resource(count, [allocate_counters,init_counters,
incr_counter,get_counter]).
foreign(allocate_counters, c, allocate_counters).
foreign(init_counters, c, init_counters).
foreign(incr_counter, c, incr_counter([-integer])).
foreign(get_counter, c, get_counter(+integer, [-integer])).
186
SICStus Prolog
:- load_foreign_resource(count).
:- allocate_counters.
count(Goal) :statistics(walltime, [Start,_]),
count_solutions(Goal),
statistics(walltime, [End,_]),
display_counts(N),
Time is End - Start,
format('Total ~d solutions in ~3d seconds.~n', [N,Time]).
count_solutions(Goal) :init_counters,
Goal,
incr_counter(_),
fail.
count_solutions(_).
display_counts(N) :get_counter(-1, N),
muse_num_workers(M),
display_worker_counts(0, M).
display_worker_counts(M, M) :- !.
display_worker_counts(W, M) :get_counter(W, C),
(
C==0 -> true ;
format('Solutions by worker ~d: ~d ~n', [W,C])
),
W1 is W+1, display_worker_counts(W1, M).
This example is compiled and executed as follows to count the number of solutions to the 8 queens
problem (see Section 3.2 [Run Example], page 41):
% gcc -c count.c
% sicstus -P
| ?- compile([count,queens]).
| ?- muse_flag(num_workers,_,3).
| ?- count(queens(8,_)).
Solutions by worker 0: 39
Solutions by worker 1: 31
Solutions by worker 2: 22
Total 92 solutions in 0.070 seconds.
yes
| ?-
Chapter 11: The Prolog Library
187
11 The Prolog Library
The Prolog library comprises a number of packages which are thought to be useful in a number of
applications. Note that the predicates in the Prolog library are not built-in predicates. One has to
explicitly load each package to get access to its predicates. The following packages are provided:
arrays
assoc
atts
heaps
lists
terms
ordsets
queues
random
system
trees
ugraphs
wgraphs
sockets
provides an implementation of extendible arrays with logarithmic access time.
uses AVL trees to implement \association lists", i.e. extendible nite mappings from
terms to terms.
provides a means of associating with variables arbitrary attributes, i.e. named properties that can be used as storage locations as well as hooks into Prolog's unication.
implements binary heaps, the main application of which are priority queues.
provides basic operations on lists.
provides a number of operations on terms.
denes operations on sets represented as lists with the elements ordered in Prolog
standard order.
denes operations on queues (FIFO stores of information).
provides a random number generator.
provides access to operating system services.
uses binary trees to represent non-extendible arrays with logarithmic access time.
The functionality is very similar to that of library(arrays), but library(trees)
is slightly more ecient if the array does not need to be extendible.
provides an implementation of directed and undirected graphs with unlabeled edges.
provides an implementation of directed and undirected graphs where each edge has an
integral weight.
provides an interface to system calls for manipulating sockets.
linda/client
linda/server
db
clpb
clpq
clpr
clpfd
chr
objects
provides an implementation of the Linda concept for process communication.
provides storage and retrieval of terms on disk les with user-dened multiple indexing.
provides constraint solving over Booleans.
provides constraint solving over Q (Rationals) or R (Reals).
provides constraint solving over Finite (Integer) Domains
provides Constraint Handling Rules
provides the combination of the logic programming and the object-oriented programming paradigms.
188
SICStus Prolog
gcla
tcltk
gauge
charsio
jasper
flinkage
timeout
xref
is a specication tool that is based on Generalized Horn Clause Language, a generalization of Prolog.
An interface to the Tcl/Tk language and toolkit.
is a proling tool for Prolog programs with a graphical interface based on tcltk.
denes I/O predicates that read from, or write to, a list of character codes.
An interface to the Java language.
is a utility program for generating glue code for the Foreign Language Interface when
building statically linked Runtime Systems or Development Systems.
provides a way of running goals with an execution time limit.
provides a cross reference producer for debugging and program analysis.
To load a library package Package, you will normally enter a query
| ?- use_module(library(Package )).
A library package normally consists of one or more hidden modules.
An alternative way of loading from the library is using the built-in predicate require/1 (see
Section 7.1.1 [Read In], page 73). The index le `INDEX.pl' needed by require/1 can be created
by the make_index program. This program is loaded as:
| ?- use_module(library(mkindex)).
make_index:make_library_index(+LibraryDirectory )
Creates a le `INDEX.pl' in LibraryDirectory. All `*.pl' les in the directory and all
its subdirectories are scanned for module/2 declarations. From these declarations, the
exported predicates are entered into the index.
Chapter 12: Array Operations
189
12 Array Operations
This package provides an implementation of extendible arrays with logarithmic access time.
Beware: the atom $ is used to indicate an unset element, and the functor $ /4 is used to indicate
a subtree. In general, array elements whose principal function symbol is $ will not work.
To load the package, enter the query
| ?- use_module(library(arrays)).
new_array(-Array )
Binds Array to a new empty array. Example:
| ?- new_array(A).
A = array($($,$,$,$),2) ?
yes
is_array(+Array )
Is true when Array actually is an array.
aref(+Index, +Array, ?Element)
Element is the element at position Index in Array. It fails if Array [Index ] is undened.
arefa(+Index, +Array, ?Element)
Is like aref/3 except that Element is a new array if Array [Index ] is undened. Example:
| ?- arefa(3, array($($,$,$,$),2), E).
E = array($($,$,$,$),2) ?
yes
arefl(+Index, +Array, ?Element)
Is as aref/3 except that
Element is [] for undened cells. Example:
| ?- arefl(3, array($($,$,$,$),2), E).
E = [] ?
yes
array_to_list(+Array,
-List)
List is a list with the pairs Index-Element of all the elements of Array. Example:
| ?- array_to_list(array($(a,b,c,d),2), List).
List = [0-a,1-b,2-c,3-d] ?
yes
190
SICStus Prolog
aset(+Index, +Array, +Element,
-NewArray )
NewArray is the result of setting Array [Index ] to Element. Example:
| ?- aset(3,array($($,$,$,$),2), a, Newarr).
Newarr = array($($,$,$,a),2) ?
yes
Chapter 13: Association Lists
191
13 Association Lists
In this package, nite mappings (\association lists") are represented by AVL trees, i.e. they are
subject to the Adelson-Velskii-Landis balance criterion:
A tree is balanced i for every node the heights of its two subtrees dier by at most 1.
The empty tree is represented as t. A tree with key K, value V, and left and right subtrees L and R
is represented as t(K,V,|R|-|L|,L,R), where |T| denotes the height of T.
The advantage of this representation is that lookup, insertion and deletion all become|in the worst
case|O(log n) operations.
The algorithms are from [Wirth 76], section 4.4.6{4.4.8.
To load the package, enter the query
| ?- use_module(library(assoc)).
empty_assoc(?Assoc )
Assoc is an empty AVL tree.
assoc_to_list(+Assoc, ?List)
List is a list of Key-Value pairs in ascending order with no duplicate Keys specifying
the same nite function as the association tree Assoc. Use this to convert an association
tree to a list.
is_assoc(+Assoc )
Assoc is a (proper) AVL tree. It checks both that the keys are in ascending order and
that Assoc is properly balanced.
min_assoc(+Assoc, ?Key, ?Val )
Key is the smallest key in Assoc and Val is its value.
max_assoc(+Assoc, ?Key, ?Val )
Key is the greatest key in Assoc and Val is its value.
gen_assoc(?Key, +Assoc, ?Value )
Key is associated with Value in the association tree Assoc. Can be used to enumerate
all Values by ascending Keys.
get_assoc(+Key, +Assoc, ?Value )
Key is identical (==) to one of the keys in the association tree Assoc, and Value unies
with the associated value.
get_assoc(+Key, +OldAssoc, ?OldValue, ?NewAssoc, ?NewValue )
OldAssoc and NewAssoc are association trees of the same shape having the same
elements except that the value for Key in OldAssoc is OldValue and the value for Key
in NewAssoc is NewValue.
get_next_assoc(+Key, +Assoc, ?Knext, ?Vnext)
Knext and Vnext is the next key and associated value after Key in Assoc.
192
get_prev_assoc(+Key, +Assoc,
SICStus Prolog
?Kprev, ?Vprev )
Kprev and Vprev is the previous key and associated value after Key in Assoc.
list_to_assoc(+List, ?Assoc )
List is a proper list of Key-Value pairs (in any order) and Assoc is an association tree
specifying the same nite function from Keys to Values.
ord_list_to_assoc(+List, ?Assoc )
List is a proper list of Key-Value pairs (keysorted) and Assoc is an association tree
specifying the same nite function from Keys to Values.
map_assoc(:Pred, ?Assoc )
Assoc is an association tree, and for each Key, if Key is associated with Value in Assoc,
Pred(Value) is true.
map_assoc(:Pred, ?OldAssoc, ?NewAssoc )
OldAssoc and NewAssoc are association trees of the same shape, and for each Key, if
Key is associated with Old in OldAssoc and with New in NewAssoc, Pred(Old,New)
is true.
put_assoc(+Key, +OldAssoc, +Val, ?NewAssoc )
OldAssoc and NewAssoc dene the same nite function, except that NewAssoc associates Val with Key. OldAssoc need not have associated any value at all with Key.
del_assoc(+Key, +OldAssoc, ?Val, ?NewAssoc )
OldAssoc and NewAssoc dene the same nite function except that OldAssoc associates Key with Val and NewAssoc doesn't associate Key with any value.
del_min_assoc(+OldAssoc, ?Key, ?Val, ?NewAssoc )
OldAssoc and NewAssoc dene the same nite function except that OldAssoc associates Key with Val and NewAssoc doesn't associate Key with any value and Key
precedes all other keys in OldAssoc.
del_max_assoc(+OldAssoc, ?Key, ?Val, -NewAssoc )
OldAssoc and NewAssoc dene the same nite function except that OldAssoc associates Key with Val and NewAssoc doesn't associate Key with any value and Key is
preceded by all other keys in OldAssoc.
Chapter 14: Attributed Variables
193
14 Attributed Variables
This package implements attributed variables. It provides a means of associating with variables
arbitrary attributes, i.e. named properties that can be used as storage locations as well as to
extend the default unication algorithm when such variables are unied with other terms or with
each other. This facility was primarily designed as a clean interface between Prolog and constraint
solvers, but has a number of other uses as well. The basic idea is due to Christian Holzbaur and he
was actively involved in the nal design. For background material, see the dissertation [Holzbaur
90].
To load the package, enter the query
| ?- use_module(library(atts)).
The package provides a means to declare and access named attributes of variables. The attributes
are compound terms whose arguments are the actual attribute values. The attribute names are
private to the module in which they are dened. They are dened with a declaration
:- attribute AttributeSpec, ..., AttributeSpec.
where each AttributeSpec has the form (Name/Arity). There must be at most one such declaration
in a module Module.
Having declared some attribute names, these attributes can now be added, updated and deleted
from unbound variables. For each declared attribute name, any variable can have at most one such
attribute (initially it has none).
The declaration causes the following two access predicates to become dened by means of the
user:goal_expansion/3 mechanism. They take a variable and an AccessSpec as arguments where
an AccessSpec is either +(Attribute ), -(Attribute ), or a list of such. The + prex may be dropped
for convenience. The meaning of the +/- prex is documented below:
Module :get_atts(-Var, ?AccessSpec )
Gets the attributes of Var according to AccessSpec. If AccessSpec is unbound, it will
be bound to a list of all set attributes of Var. Non-variable terms cause a type error to
be raised. The prexes in the AccessSpec have the following meaning:
+(Attribute )
The corresponding actual attribute must be present and is unied with
Attribute.
-(Attribute )
The corresponding actual attribute must be absent. The arguments of
Attribute are ignored, only the name and arity are relevant.
Module :put_atts(-Var, +AccessSpec )
Sets the attributes of Var according to AccessSpec. Non-variable terms cause a type
error to be raised. The eects of put_atts/2 are undone on backtracking.
194
SICStus Prolog
+(Attribute )
The corresponding actual attribute is set to Attribute. If the actual attribute was already present, it is simply replaced.
-(Attribute )
The corresponding actual attribute is removed. If the actual attribute was
already absent, nothing happens.
A module that contains an attribute declaration has an opportunity to extend the default unication
algorithm by dening the following predicate:
Module :verify_attributes(-Var, +Value, -Goals )
A hook predicate. This predicate is called whenever a variable Var that might have
attributes in Module is about to be bound to Value (it might have none). The unication resumes after the call to verify_attributes/3. Value is a non-variable term,
or another attributed variable. Var might have no attributes present in Module; the
unication extension mechanism is not sophisticated enough to lter out exactly the
variables that are relevant for Module.
verify_attributes/3 is called before Var has actually been bound to Value. If it
fails, the unication is deemed to have failed. It may succeed non-deterministically, in
which case the unication might backtrack to give another answer. It is expected to
return, in Goals, a list of goals to be called after Var has been bound to Value.
verify_attributes/3 may invoke arbitrary Prolog goals, but Var should not be bound
by it. Binding Var will result in undened behavior.
If Value is a non-variable term, verify_attributes/3 will typically inspect the attributes of Var and check that they are compatible with Value and fail otherwise. If
Value is another attributed variable, verify_attributes/3 will typically copy the attributes of Var over to Value, or merge them with Value's, in preparation for Var to be
bound to Value. In either case, verify_attributes/3 may determine Var's current
attributes by calling get_atts(Var,List) with an unbound List.
An important use for attributed variables is in implementing coroutining facilities as an alternative
or complement to the built-in coroutining mechanisms. In this context it might be useful to be able
to interpret some of the attributes of a variable as a goal that is blocked on that variable. Certain
built-in predicates (frozen/2, call_residue/2) and the Prolog top level need to access blocked
goals, and so need a means of getting the goal interpretation of attributed variables by calling:
Module :attribute_goal(-Var, -Goal )
A hook predicate. This predicate is called in each module that contains an attribute
declaration, when an interpretation of the attributes as a goal is needed. It should
unify Goal with the interpretation, or merely fail if no such interpretation is available.
An important use for attributed variables is to provide an interface to constraint solvers. An
important function for a constraint solver in the constraint logic programming paradigm is to be
able to perform projection of the residual constraints onto the variables that occurred in the toplevel query. A module that contains an attribute declaration has an opportunity to perform such
projection of its residual constraints by dening the following predicate:
Chapter 14: Attributed Variables
195
Module :project_attributes(+QueryVars, +AttrVars )
A hook predicate. This predicate is called by the Prolog top level and by the builtin predicate call_residue/2 in each module that contains an attribute declaration.
QueryVars is the list of variables occurring in the query, or in terms bound to such
variables, and AttrVars is a list of possibly attributed variables created during the
execution of the query. The two lists of variables may or may not be disjoint.
If the attributes on AttrVars can be interpreted as constraints, this predicate will typically \project" those constraints onto the relevant QueryVars. Ideally, the residual
constraints will be expressed entirely in terms of the QueryVars, treating all other variables as existentially quantied. Operationally, project_attributes/2 must remove
all attributes from AttrVars, and add transformed attributes representing the projected
constraints to some of the QueryVars.
Projection has the following eect on the Prolog top level. When the top level query
has succeeded, project_attributes/2 is called rst. The top level then prints the
answer substition and residual constraints. While doing so, it searches for attributed
variables created during the execution of the query. For each such variable, it calls
attribute_goal/2 to get a printable representation of the constraint encoded by the
attribute. Thus, project_attributes/2 is a mechanism for controlling how the residual constraints should be displayed at top level.
Similarly during the execution of call_residue(Goal,Residue ), when Goal has succeeded, project_attributes/2 is called. After that, all attributed variables created
during the execution of Goal are located. For each such variable, attribute_goal/2
produces a term representing the constraint encoded by the attribute, and Residue is
unied with the list of all such terms.
The exact denition of project_attributes/2 is constraint system dependent, but
see Section 29.5 [Projection], page 259 for details about projection in clp(Q,R).
In the following example we sketch the implementation of a nite domain \solver". Note that an
industrial strength solver would have to provide a wider range of functionality and that it quite
likely would utilize a more ecient representation for the domains proper. The module exports
a single predicate domain(-Var,?Domain) which associates Domain (a list of terms) with Var. A
variable can be queried for its domain by leaving Domain unbound.
We do not present here a denition for
straints happens to be dicult.
project_attributes/2.
:- module(domain, [domain/2]).
:- use_module(library(atts)).
:- use_module(library(ordsets), [
ord_intersection/3,
ord_intersect/2,
list_to_ord_set/2
]).
:- attribute dom/1.
Projecting nite domain con-
196
SICStus Prolog
verify_attributes(Var, Other, Goals) :get_atts(Var, dom(Da)), !,
%
(
var(Other) ->
%
(
get_atts(Other, dom(Db)) -> %
ord_intersection(Da, Db, Dc),
Dc = [El|Els],
%
(
Els = [] ->
%
Goals = [Other=El]
%
;
Goals = [],
put_atts(Other, dom(Dc))%
)
;
Goals = [],
put_atts(Other, dom(Da))
%
)
;
Goals = [],
ord_intersect([Other], Da)
%
).
verify_attributes(_, _, []).
%
%
%
attribute_goal(Var, domain(Var,Dom)) :get_atts(Var, dom(Dom)).
domain(X, Dom) :var(Dom), !,
get_atts(X, dom(Dom)).
domain(X, List) :list_to_ord_set(List, Set),
Set = [El|Els],
(
Els = [] ->
X = El
;
put_atts(Fresh, dom(Set)),
X = Fresh
).
are we involved?
must be attributed then
has a domain?
at least one element
exactly one element
implied binding
rescue intersection
rescue the domain
value in domain?
unification triggered
because of attributes
in other modules
% interpretation as goal
% at least one element
% exactly one element
% implied binding
% may call
% verify_attributes/3
Note that the \implied binding" Other=El was deferred until after the completion of verify_
attribute/3. Otherwise, there might be a danger of recursively invoke verify_attribute/3,
which might bind Var, which is not allowed inside the scope of verify_attribute/3. Deferring unications into the third argument of verify_attribute/3 eectively serializes th calls to
verify_attribute/3.
Assuming that the code resides in the le `domain.pl', we can use it via:
| ?- use_module(domain).
Let's test it:
Chapter 14: Attributed Variables
197
| ?- domain(X,[5,6,7,1]), domain(Y,[3,4,5,6]), domain(Z,[1,6,7,8]).
domain(X,[1,5,6,7]),
domain(Y,[3,4,5,6]),
domain(Z,[1,6,7,8]) ?
yes
| ?- domain(X,[5,6,7,1]), domain(Y,[3,4,5,6]), domain(Z,[1,6,7,8]),
X=Y.
Y = X,
domain(X,[5,6]),
domain(Z,[1,6,7,8]) ?
yes
| ?- domain(X,[5,6,7,1]), domain(Y,[3,4,5,6]), domain(Z,[1,6,7,8]),
X=Y, Y=Z.
X = 6,
Y = 6,
Z = 6
To demonstrate the use of the Goals argument of verify_attributes/3, we give an implementation
of freeze/2. We have to name it myfreeze/2 in order to avoid a name clash with the built-in
predicate of the same name.
:- module(myfreeze, [myfreeze/2]).
:- use_module(library(atts)).
:- attribute frozen/1.
verify_attributes(Var, Other, Goals) :get_atts(Var, frozen(Fa)), !,
% are we involved?
(
var(Other) ->
% must be attributed then
(
get_atts(Other, frozen(Fb)) % has a pending goal?
-> put_atts(Other, frozen((Fa,Fb))) % rescue conjunction
;
put_atts(Other, frozen(Fa)) % rescue the pending goal
),
Goals = []
;
Goals = [Fa]
).
verify_attributes(_, _, []).
attribute_goal(Var, Goal) :get_atts(Var, frozen(Goal)).
myfreeze(X, Goal) :put_atts(Fresh, frozen(Goal)),
% interpretation as goal
198
SICStus Prolog
Fresh = X.
Assuming that this code lives in le `myfreeze.pl', we would use it via:
| ?- use_module(myfreeze).
| ?- myfreeze(X,print(bound(x,X))), X=2.
bound(x,2)
X = 2
% side effect
% bindings
The two solvers even work together:
| ?- myfreeze(X,print(bound(x,X))), domain(X,[1,2,3]),
domain(Y,[2,10]), X=Y.
bound(x,2)
X = 2,
Y = 2
% side effect
% bindings
The two example solvers interact via bindings to shared attributed variables only. More complicated
interactions are likely to be found in more sophisticated solvers. The corresponding verify_
attributes/3 predicates would typically refer to the attributes from other known solvers/modules
via the module prex in Module :get_atts/2.
Chapter 15: Heap Operations
199
15 Heap Operations
A binary heap is a tree with keys and associated values that satises the heap codition: the key of
every node is greater than or equal to the key of its parent, if it has one. The main application of
binary heaps are priority queues. To load the package, enter the query
| ?- use_module(library(heaps)).
add_to_heap(+OldHeap, +Key, +Datum,
?NewHeap)
Inserts the new Key-Datum pair into the current heap OldHeap producing the new
heap NewHeap. The insertion is not stable, that is, if you insert several pairs with the
same Key it is not dened which of them will come out rst, and it is possible for any
of them to come out rst depending on the history of the heap. Example:
| ?- add_to_heap(t(0,[],t),3,678,N).
N = t(1,[],t(3,678,t,t)) ?
yes
get_from_heap(+OldHeap,
?Key, ?Datum, ?NewHeap)
Returns the Key-Datum pair in OldHeap with the smallest Key, and also a NewHeap
which is the OldHeap with that pair deleted. Example:
get_from_heap(t(1,[],t(1,543,t,t)),K,D,N).
D = 543,
K = 1,
N = t(0,[1],t) ?
yes
empty_heap(?Heap )
is true when Heap is the empty heap.
heap_size(+Heap, ?Size )
Size is the number of elements in the heap Heap.
heap_to_list(+Heap, -List)
Returns the current set of Key-Datum pairs in the Heap as a keysorted List.
is_heap(+Heap )
is true when Heap is a valid heap.
list_to_heap(+List, -Heap )
Takes a list List of Key-Datum pairs and forms them into a heap Heap. Example:
| ?- list_to_heap([1-34,2-345,5-678],H).
H = t(3,[],t(1,34,t(2,345,t,t),t(5,678,t,t))) ?
yes
200
min_of_heap(+Heap,
SICStus Prolog
?Key, ?Datum)
Returns the Key-Datum pair at the top of the heap Heap without removing it. Fails if
the heap is empty.
min_of_heap(+Heap, ?Key1, ?Datum1, ?Key2, ?Datum2 )
Returns the smallest (Key1-Datum1) and second smallest (Key2-Datum2) pairs in the
Heap, without deleting them. It fails if the heap does not have at least two elements.
delete_from_heap(+OldHeap, +Key, ?Datum, ?NewHeap )
deletes a single Key-Datum pair in OldHeap producing NewHeap. This is useful if you
want to e.g. change the priority of Datum. Beware: this operation needs to search the
whole heap in the worst case.
Chapter 16: List Operations
201
16 List Operations
This package denes operations on lists. Lists are a very basic data structure, but nevertheless
certain very frequent operations are provided in this package.
To load the package, enter the query
| ?- use_module(library(lists)).
append(?Prex,
?Sux, ?Combined )
Combined is the combined list of the elements in Prex followed by the elements in
Sux. It can be used to form Combined or it can be used to nd Prex and/or Sux
from a given Combined.
delete(+List, +Element, ?Residue )
Residue is the result of removing all identical occurrences of Element in List.
is_list(+List)
List is a proper list.
last(?List, ?Last)
Last is the last element in List. Example:
| ?- last([x,y,z], Z).
Z = z ?
yes
max_list(+ListOfNumbers,
?Max )
Max is the largest of the elements in ListOfNumbers.
member(?Element, ?List)
Element is a member of List. It may be used to test for membership in a list, but it
can also be used to enumerate all the elements in List. Example:
| ?- member(X, [a,b,c]).
X = a ? ;
X = b ? ;
X = c ?
yes
memberchk(+Element, +List)
Element is a member of List, but memberchk/2 only succeeds once and can therefore
not be used to enumerate the elements in List. Example:
202
SICStus Prolog
| ?- memberchk(X, [a,b,c]).
X = a ? ;
no
min_list(+ListOfNumbers,
?Min)
Min is the smallest of the numbers in the list ListOfNumbers.
nextto(?X, ?Y, ?List)
X and Y appears side-by-side in List. Example:
| ?- nextto(X, Y, [1,2,3]).
X = 1,
Y = 2 ? ;
X = 2,
Y = 3 ? ;
no
no_doubles(?List)
List contains no duplicated elements. This is true when dif(X, Y ) holds for all pairs
of members X and Y of the list.
non_member(?Element, ?List)
Element does not occur in List. This is true when dif(Element, Y ) holds for all
members Y of the list.
nth(?N, ?List, ?Element)
Element is the Nth element of List. The rst element is number 1. Example:
| ?- nth(N, [a,b,c,d,e,f,g,h,i], f).
N = 6 ?
yes
nth(?N,
?List, ?Element, ?Rest)
Element is in position N in the List and Rest is all elements in List except Element.
nth0(?N, ?List, ?Element)
Element is the Nth element of List, counting the rst element as 0.
nth0(?N, ?List, ?Element, ?Rest)
Element is the Nth element of List, counting the rst element as 0. Rest is all the
other elements in List. Example:
| ?- nth0(N, [a,b,c,d,e,f,g,h,i,j], f, R).
N = 5,
R = [a,b,c,d,e,g,h,i,j] ?
yes
Chapter 16: List Operations
203
permutation(?List,
?Perm)
Perm is a permutation of List.
prefix(?Prex, ?List)
Prex is a prex of List. Example:
| ?- prefix([1,2,3], [1,2,3,4,5,6]).
yes
remove_duplicates(+List,
?Pruned )
Pruned is the result of removing all identical duplicate elements in List. Example:
| ?- remove_duplicates([1,2,3,2,3,1], P).
P = [1,2,3] ? ;
no
reverse(?List,
?Reversed )
Reversed has the same elements as List but in a reversed order.
same_length(?List1, ?List2 )
List1 and List2 have the same number of elements.
same_length(?List1, ?List2, ?Length)
List1 and List2 have the same number of elements and that number is Length. Example:
| ?- same_length([1,2,3], [9,8,7], N).
N = 3 ? ;
no
select(?Element,
?List, ?List2 )
The result of removing an occurrence of Element in List is List2.
sublist(?Sub, ?List)
Sub contains some of the elements of List, in the same order.
substitute(+X, +Xlist, +Y, ?Ylist)
Xlist and Ylist are equal except for replacing identical occurrences of X by Y. Example:
| ?- substitute(1, [1,2,3,4], 5, X).
X = [5,2,3,4] ?
yes
sum_list(+ListOfNumbers,
?Sum)
Sum is the result of adding the ListOfNumbers together.
suffix(?Sux, ?List)
Sux is a sux of List.
204
SICStus Prolog
Chapter 17: Term Utilities
205
17 Term Utilities
This package denes operations on terms for subsumption checking, \anti-unication", unication
with occurs-check, testing acyclicity, and getting the variables.
To load the package, enter the query
| ?- use_module(library(terms)).
subsumes_chk(?General,
?Specic )
Specic is an instance of General, i.e. if there is a substitution that leaves Specic
unchanged and makes General identical to Specic. It doesn't bind any variables.
subsumes_chk(f(X), f(a)).
true
| ?- subsumes_chk(f(a), f(X)).
no
| ?- subsumes_chk(A-A, B-C).
no
| ?- subsumes_chk(A-B, C-C).
true
subsumes(?General,
?Specic )
Specic is an instance of General. It will bind variables in General (but not those in
Specic) so that General becomes identical to Specic.
variant(?Term, ?Variant)
Term and Variant are identical modulo renaming of variables, provided Term and
Variant have no variables in common.
term_subsumer(?Term1, ?Term2, ?General )
General is the most specic term that generalizes Term1 and Term2. This process is
sometimes called anti-unication, as it is the dual of unication.
| ?- term_subsumer(f(g(1,h(_))), f(g(_,h(1))), T).
T = f(g(_B,h(_A)))
| ?- term_subsumer(f(1+2,2+1), f(3+4,4+3), T).
T = f(_A+_B,_B+_A)
206
SICStus Prolog
term_hash(?Term, ?Hash)
term_hash(?Term, +Depth, +Range,
?Hash)
If Term is instantiated up to the given Depth, an integer hash value in the range
[0,Range) as a function of Term is unied with Hash. Otherwise, the goal just succeeds,
leaving Hash uninstantiated.
If Term contains oats or integers outside the range [-33554432,33554431], the hash
value will be platform dependent. Otherwise, the hash value will be identical across
runs and platforms.
The depth of a term is dened as follows: the (principal functor of) the term itself has
depth 1, and an argument of a term with depth i has depth i+1.
Depth should be an integer >= -1. If Depth = -1 (the default), Term must be ground,
and all subterms of Term are relevant in computing Hash. Otherwise, only the subterms
up to depth Depth of Term are used in the computation.
Range should be an integer >= 1. The default will give hash values in a range appropriate for all platforms.
| ?- term_hash([a,b,_], 3, 4, H).
H = 2
| ?- term_hash([a,b,_], 4, 4, H).
true
| ?- term_hash(f(a,f(b,f(_,[]))), 2, 4, H).
H = 2
is provided primarily as a tool for the construction of sophisticated
Prolog clause access schemes. Its intended use is to generate hash values for terms
that will be used with rst argument clause indexing, yielding compact and ecient
multi-argument or deep argument indexing.
term_variables(?Term, ?Variables )
Variables is the set of variables occurring in Term.
unify_with_occurs_check(?X, ?Y ) [ISO]
True if X and Y unify to a nite (acyclic) term. Runs in almost linear time.
acyclic_term(?X )
True if X is nite (acyclic). Runs in linear time.
cyclic_term(?X )
True if X is innite (cyclic). Runs in linear time.
term_hash/(2,4)
Chapter 18: Ordered Set Operations
207
18 Ordered Set Operations
This package denes operations on ordered sets. Ordered sets are sets represented as lists with the
elements ordered in a standard order. The ordering is dened by the @< family of term comparison
predicates and it is the ordering produced by the built-in predicate sort/2 (see Section 7.3 [Term
Compare], page 97).
To load the package, enter the query
| ?- use_module(library(ordsets)).
is_ordset(+Set)
Set is an ordered set.
list_to_ord_set(+List, ?Set)
Set is the ordered representation of the set denoted by the unordered representation
List. Example:
| ?- list_to_ord_set([p,r,o,l,o,g], P).
P = [g,l,o,p,r] ?
yes
ord_add_element(+Set1, +Element
?Set2 )
Set2 is Set1 with Element inserted in it, preserving the order. Example:
| ?- ord_add_element([a,c,d,e,f], b, N).
N = [a,b,c,d,e,f] ?
yes
ord_del_element(+Set1, +Element,
?Set2 )
Set2 is like Set1 but with Element removed.
ord_disjoint(+Set1, +Set2 )
The two ordered sets have no elements in common.
ord_intersect(+Set1, +Set2 )
The two ordered sets have at least one element in common.
ord_intersection(+Set1, +Set2, ?Intersect)
Intersect is the ordered set representation of the intersection between Set1 and Set2.
ord_intersection(+Set1, +Set2, ?Intersect, ?Di )
Intersect is the intersection between Set1 and Set2, and Di is the dierence between
Set2 and Set1.
ord_intersection(+Sets, ?Intersection)
Intersection is the ordered set representation of the intersection of all the sets in Sets.
Example:
208
SICStus Prolog
| ?- ord_intersection([[1,2,3],[2,3,4],[3,4,5]], I).
I = [3] ?
yes
ord_member(+Elt, +Set)
is true when Elt is a member of Set.
ord_seteq(+Set1, +Set2 )
Is true when the two arguments represent the same set. Since they are assumed to be
ordered representations, they must be identical.
ord_setproduct(+Set1, +Set2, ?SetProduct)
SetProduct is the Cartesian Product of the two Sets. The product is represented as
pairs: Elem1-Elem2 where Elem1 is an element from Set1 and Elem2 is an element
from Set2. Example
| ?- ord_setproduct([1,2,3], [4,5,6], P).
P = [1-4,1-5,1-6,2-4,2-5,2-6,3-4,3-5,3-6] ?
yes
ord_subset(+Set1, +Set2 )
Every element of the ordered set Set1 appears in the ordered set Set2.
ord_subtract(+Set1, +Set2, ?Dierence )
Dierence contains all and only the elements of Set1 which are not also in Set2. Example:
| ?- ord_subtract([1,2,3,4], [3,4,5,6], S).
S = [1,2] ?
yes
ord_symdiff(+Set1, +Set2,
?Dierence )
Dierence is the symmetric dierence of Set1 and Set2. Example:
| ?- ord_symdiff([1,2,3,4], [3,4,5,6], D).
D = [1,2,5,6] ?
yes
ord_union(+Set1, +Set2,
?Union)
Union is the union of Set1 and Set2.
ord_union(+Set1, +Set2, ?Union, ?New )
Union is the union of Set1 and Set2, and New is the dierence between Set2 and Set1.
This is useful if you are accumulating members of a set and you want to process new
elements as they are added to the set.
Chapter 18: Ordered Set Operations
ord_union(+Sets,
?Union)
Union is the union of all the sets in Sets. Example:
| ?- ord_union([[1,2,3],[2,3,4],[3,4,5]], U).
U = [1,2,3,4,5] ?
yes
209
210
SICStus Prolog
Chapter 19: Queue Operations
211
19 Queue Operations
A queue is a rst-in, rst-out store of information. This implementation of queues uses dierencelists, the head of the dierence-list represents the beginning of the queue and the tail represents
the end of the queue. The members of the dierence-list are the elements in the queue. The rst
argument in the queue-representation is the number of elements in the queue in unary representation.
Thus, a queue with n elements is represented as follows:
q(s(...s(0)...), [X1,...,Xn,Y1,...,Ym], [Y1,...,Ym])
where n is the length of the queue and X1...Xn are the elements of the queue.
To load the package, enter the query
| ?- use_module(library(queues)).
empty_queue(?Queue )
Is true if Queue has no elements.
is_queue(+Queue )
is true when Queue is a valid queue.
queue(?X, ?Queue )
Is true if Queue has one element and that is X.
queue_head(?Head, ?Queue1, ?Queue2 )
Queue1 and Queue2 are the same queues except that Queue2 has Head inserted in the
front. It can be used to enqueue the rst element in Queue2. Example:
| ?- queue_head(Head, Nq,
q(s(s(s(s(0)))),[1,2,3,4|R],R)).
Head = 1,
Nq = q(s(s(s(0))),[2,3,4|_193],_193),
R = _193 ?
yes
queue_head_list(+HeadList,
?Queue1, ?Queue2 )
Queue1 and Queue2 have the same elements except that Queue2 has HeadList inserted
in the front.
queue_last(?Last, ?Queue1, ?Queue2 )
Queue2 is like Queue1 but have Last as the last element in the queue.
queue_last_list(+LastList, ?Queue1, ?Queue2 )
Queue1 and Queue2 are the same queues except that Queue2 has the list of elements
LastList last in the queue. Example:
212
SICStus Prolog
| ?- queue_last_list([5,6], q(s(s(0)))), [1,2|R], R), NQ).
NQ = q(s(s(s(s(0)))))),[1,2,5,6|_360],_360),
R = [5,6|_360] ?
yes
list_queue(+List,
?Queue )
Queue is the queue representation of the elements in List. Example:
| ?- list_queue([1,2,3,4], Q).
Q = q(s(s(s(s(0)))),[1,2,3,4|_138],_138) ?
yes
| ?-
queue_length(+Queue,
?Length)
Length is the number of elements in Queue. Example:
| ?- queue_length(q(s(s(s(s(s(0))))),[a,b,c,d,e|R],R), L).
L = 5,
R = _155 ?
yes
Chapter 20: Random Number Generator
213
20 Random Number Generator
This package provides a random number generator. To load the package, enter the query
| ?- use_module(library(random)).
random(-Number )
Binds Number to a random oat in the interval [0.0, 1.0). Note that 1.0 will never be
generated.
random(+Lower, +Upper, -Number )
Binds Number to a random integer in the interval [Lower,Upper) if Lower and Upper
are integers. Otherwise Number is bound to a random oat between Lower and Upper.
Upper will never be generated.
randseq(+K, +N, -RandomSeq)
Generates a unordered set of K unique integers, chosen randomly in the range 1..N.
RandomSeq is not returned in any particular order.
randset(+K, +N, -RandomSet)
Generates an ordered set of K unique integers, chosen randomly in the range 1..N. The
set is returned in standard order.
getrand(?State )
Tries to unify State with the term rand(X,Y,Z ) where X, Y, and Z are integers describing the state of the random generator.
setrand(rand(+X,+Y,+Z ))
Sets the state of the random generator. X, Y, and Z must be integers in the ranges
[1,30269), [1,30307), and [1,30323), respectively.
214
SICStus Prolog
Chapter 21: Operating System Utilities
215
21 Operating System Utilities
This package contains utilities for invoking services from the operating system. To load the package,
enter the query
| ?- use_module(library(system)).
Certain predicates described below take names of les or directories as arguments. These must be
given as atoms, and the predicates below will not call absolute_file_name/2 on them.
Some predicates are described as invoking the default shell. Specically this means invoking
`/bin/sh' on UNIX platforms. On MSDOS, Windows and OS/2, the command interpreter given
by the environment variable COMSPEC is invoked.
datime(-Datime )
Datime is a timestamp of the form datime(Year,Month,Day,Hour,Min,Sec ) containing the current date and time. All elds are integers.
delete_file(+FileName,+Options )
FileName is the name of an existing le or directory. Options is a list of options.
Possible options are directory, recursive or ignore. If FileName is not a directory
it is deleted, otherwise if the option directory is specied but not recursive, the
directory will be deleted if it is empty. If recursive is specied and FileName is
a directory, the directory and all its subdirectories and les will be deleted. If the
operation fails, an exception is raised unless the ignore option is specied.
delete_file(+FileName )
Equivalent to delete_file(FileName,[recursive]).
directory_files(+Directory,-FileList)
FileList is the list of entries (les, directories, etc.) in Directory.
make_directory(+DirectoryName )
Makes a new directory.
environ(?Var, ?Value )
Var is the name of an environment variable, and Value is its value. Both are atoms.
Can be used to enumerate all current environment variables.
exec(+Command, [+Stdin,+Stdout,+Stderr ], -Pid )
Passes Command to a new default shell process for execution. The standard I/O
streams of the new process are connected according to what is specied by the terms
+Stdin, +Stdout, and +Stderr respectively. Possible values are:
null
Connected to `/dev/null' or equivalent.
std
The standard stream is shared with the calling process. Note that the
standard stream may not be referring to a console if the calling process
is "windowed". To portably print the output from the subprocess on the
Prolog console, pipe/1 must be used and the program must explicitly read
the pipe and write to the console. Similarly for the input to the subprocess.
216
SICStus Prolog
pipe(-Stream)
A pipe is created which connects the Prolog stream Stream to the standard
stream of the new process. It must be closed using close/1; it is not closed
automatically when the process dies.
Pid is the process identier of the new process.
On UNIX, the subprocess will be detached provided none of its standard streams is
specied as std. This means it will not receive an interruption signal as a result of ^C
being typed.
file_exists(+FileName )
FileName is the name of an existing le or directory.
file_exists(+FileName, +Permissions )
FileName is the name of an existing le or directory which can be accessed according to
Permissions. Permissions is an atom, an integer (see access(2)), or a list of atoms and/or
integers. The atoms must be drawn from the list [read,write,search,exists].
file_property(+FileName, ?Property )
FileName has the property Property. The possible properties are:
type(Type )
Type is one of regular, directory, fifo, symlink, socket or unknown.
size(Size )
Size is the size of FileName.
mod_time(ModTime )
ModTime is the time of the last modication of FileName.
If Property is uninstantiated, the predicate will enumerate the properties on backtracking.
host_id(-HID )
HID is the unique identier, represented by an atom, of the host executing the current
SICStus Prolog process.
host_name(-HostName )
HostName is the standard host name of the host executing the current SICStus Prolog
process.
pid(-PID )
PID is the identier of the current SICStus Prolog process.
kill(+Pid, +Signal )
Sends the signal Signal to process Pid.
mktemp(+Template, -FileName )
Interface to the UNIX function mktemp(3). A unique le name is created and unied
with FileName. Template should contain a le name with six trailing Xs. The unique
le name is that template with the six Xs replaced by a character string.
Chapter 21: Operating System Utilities
popen(+Command, +Mode,
217
?Stream)
Interface to the UNIX function popen(3). Passes Command to a new default shell
process for execution. Mode may be either read or write. In the former case the output
from the process is piped to Stream. In the latter case the input to the process is piped
from Stream. Stream may be read/written using the ordinary StreamIO predicates. It
must be closed using close/1; it is not closed automatically when the process dies.
rename_file(+OldName, +NewName )
OldName is the name of an existing le or directory, which will be renamed to NewName. If the operation fails, an exception is raised.
shell
Starts a new interactive shell named by the environment variable SHELL. The control
is returned to Prolog upon termination of the shell process.
shell(+Command )
Passes Command to a new shell named by the environment variable SHELL for execution. Succeeds if the return status value is 0.
On MSDOS, Windows or OS/2, if SHELL is dened it is expected to name a UNIX like
shell which will be invoked with the argument -c Command. If SHELL is undened,
the shell named by COMSPEC will be invoked with the argument /C Command.
shell(+Command, -Status )
Passes Command to a new shell named by the environment variable SHELL for execution. The status value is returned in Status. See also shell/1 above.
sleep(+Seconds )
Puts the SICStus Prolog process asleep for Second seconds.
system
Starts a new interactive default shell process. The control is returned to Prolog upon
termination of the shell process.
system(+Command )
Passes Command to a new default shell process for execution. Succeeds if the return
status value is 0.
system(+Command, -Status )
Passes Command to a new default shell process for execution. The status value is
returned in Status.
tmpnam(-FileName )
Interface to the ANSI C function tmpnam(3). A unique le name is created and unied
with FileName.
wait(+Pid, -Status )
Waits for the child process Pid to terminate. The exit status is returned in Status.
The function is similar to that of the UNIX function waitpid(3).
working_directory(?OldDirectory, ?NewDirectory )
OldDirectory is the current working directory, and the working directory is set to
NewDirectory. In particular, the goal working_directory(Dir,Dir ) unies Dir with
the current working directory without changing anything.
218
SICStus Prolog
Chapter 22: Updatable Binary Trees
219
22 Updatable Binary Trees
This package uses binary trees to represent arrays of N elements where N is xed, unlike
library(arrays). To load the package, enter the query
| ?- use_module(library(trees)).
Binary trees have the following representation: t denotes the empty tree, and t(Label,Left,Right)
denotes the binary tree with label Label and children Left and Right.
gen_label(?Index, +Tree,
?Label )
Label labels the Index-th element in the Tree. Can be used to enumerate all Labels by
ascending Index. Use get_label/3 instead if Index is instantiated.
get_label(+Index, +Tree, ?Label )
Label labels the Index-th element in the Tree.
list_to_tree(+List, -Tree )
Constructs a binary Tree from List where get_label(K,Tree,Lab) i Lab is the Kth
element of List.
map_tree(:Pred, ?OldTree, ?NewTree )
OldTree and NewTree are binary trees of the same shape and Pred(Old,New) is true
for corresponding elements of the two trees.
put_label(+I, +OldTree, +Label, -NewTree )
Constructs NewTree which has the same shape and elements as OldTree, except that
the I-th element is Label.
put_label(+I, +OldTree, +Label, -NewTree, +Label )
Constructs NewTree which has the same shape and elements as OldTree, except that
the I-th element is changed from OldLabel to NewLabel.
tree_size(+Tree, ?Size )
Calculates as Size the number of elements in the Tree.
tree_to_list(+Tree, ?List)
Is the converse operation to list_to_tree/2. Any mapping or checking operation can
be done by converting the tree to a list, mapping or checking the list, and converting
the result, if any, back to a tree.
220
SICStus Prolog
Chapter 23: Unweighted Graph Operations
221
23 Unweighted Graph Operations
Directed and undirected graphs are fundamental data structures representing arbitrary relationships between data objects. This package provides a Prolog implementation of directed graphs,
undirected graphs being a special case of directed graphs.
An unweighted directed graph (ugraph) is represented as a list of (vertex-neighbors) pairs, where
the pairs are in standard order (as produced by keysort with unique keys) and the neighbors of
each vertex are also in standard order (as produced by sort), every neighbor appears as a vertex
even if it has no neighbors itself, and no vertex is a neighbor to itself.
An undirected graph is represented as a directed graph where for each edge (U,V) there is a
symmetric edge (V,U).
An edge (U,V) is represented as the term U-V. U and V must be distinct.
A vertex can be any term. Two vertices are distinct i they are not identical (==).
A path from u to v is represented as a list of vertices, beginning with u and ending with v. A
vertex cannot appear twice in a path. A path is maximal in a graph if it cannot be extended.
A tree is a tree-shaped directed graph (all vertices have a single predecessor, except the root node,
which has none).
A strongly connected component of a graph is a maximal set of vertices where each vertex has a
path in the graph to every other vertex.
Sets are represented as ordered lists (see Chapter 18 [Ordsets], page 207).
To load the package, enter the query
| ?- use_module(library(ugraphs)).
The following predicates are dened for directed graphs.
vertices_edges_to_ugraph(+Vertices, +Edges,
-Graph)
Is true if Vertices is a list of vertices, Edges is a list of edges, and Graph is a graph
built from Vertices and Edges. Vertices and Edges may be in any order. The vertices
mentioned in Edges do not have to occur explicitly in Vertices. Vertices may be used
to specify vertices that are not connected by any edges.
vertices(+Graph, -Vertices )
Unies Vertices with the vertices in Graph.
edges(+Graph, -Edges )
Unies Edges with the edges in Graph.
add_vertices(+Graph1, +Vertices, -Graph2 )
Graph2 is Graph1 with Vertices added to it.
222
del_vertices(+Graph1, +Vertices,
SICStus Prolog
-Graph2 )
Graph2 is Graph1 with Vertices and all edges to and from them removed from it.
add_edges(+Graph1, +Edges, -Graph2 )
Graph2 is Graph1 with Edges and their \to" and \from" vertices added to it.
del_edges(+Graph1, +Edges, -Graph2 )
Graph2 is Graph1 with Edges removed from it.
transpose(+Graph, -Transpose )
Transpose is the graph computed by replacing each edge (u,v) in Graph by its symmetric edge (v,u). Takes O(N^2) time.
neighbors(+Vertex, +Graph, -Neighbors )
neighbours(+Vertex, +Graph, -Neighbors )
Vertex is a vertex in Graph and Neighbors are its neighbors.
complement(+Graph, -Complement)
Complement is the complement graph of Graph, i.e. the graph that has the same
vertices as Graph but only the edges that are not in Graph.
compose(+G1, +G2, -Composition)
Computes Composition as the composition of two graphs, which need not have the
same set of vertices.
transitive_closure(+Graph, -Closure )
Computes Closure as the transitive closure of Graph in O(N^3) time.
symmetric_closure(+Graph, -Closure )
Computes Closure as the symmetric closure of Graph, i.e. for each edge (u,v) in Graph,
add its symmetric edge (v,u). Takes O(N^2) time. This is useful for making a directed
graph undirected.
top_sort(+Graph, -Sorted )
Finds a topological ordering of a Graph and returns the ordering as a list of Sorted
vertices. Fails i no ordering exists, i.e. i the graph contains cycles. Takes O(N^2)
time.
max_path(+V1, +V2, +Graph, -Path, -Cost)
Path is a longest path of cost Cost from V1 to V2 in Graph, there being no cyclic
paths from V1 to V2. Takes O(N^2) time.
min_path(+V1, +V2, +Graph, -Path, -Cost)
Path is a shortest path of cost Cost from V1 to V2 in Graph. Takes O(N^2) time.
min_paths(+Vertex, +Graph, -Tree )
Tree is a tree of all the shortest paths from Vertex to every other vertex in Graph.
This is the single-source shortest paths problem.
path(+Vertex, +Graph, -Path)
Given a Graph and a Vertex of Graph, returns a maximal Path rooted at Vertex,
enumerating more paths on backtracking.
Chapter 23: Unweighted Graph Operations
223
reduce(+Graph,
-Reduced )
Reduced is the reduced graph for Graph. The vertices of the reduced graph are the
strongly connected components of Graph. There is an edge in Reduced from u to v i
there is an edge in Graph from one of the vertices in u to one of the vertices in v.
reachable(+Vertex, +Graph, -Reachable )
Given a Graph and a Vertex of Graph, returns the set of vertices that are reachable
from that Vertex, including Vertex itself. Takes O(N^2) time.
random_ugraph(+P, +N, -Graph)
Where P is a probability, unies Graph with a random graph of vertices 1..N where
each possible edge is included with probability P.
The following predicates are dened for undirected graphs only.
min_tree(+Graph,
-Tree, -Cost)
Tree is a spanning tree of Graph with cost Cost, if it exists.
clique(+Graph, +K, -Clique )
Clique is a maximal clique (complete subgraph) of N vertices of Graph, where N>=K.
N is not necessarily maximal.
independent_set(+Graph, +K, -Set)
Set is a maximal independent (unconnected) set of N vertices of Graph, where N>=K.
N is not necessarily maximal.
coloring(+Graph, +K, -Coloring )
colouring(+Graph, +K, -Coloring )
Coloring is a mapping from vertices to colors 1..N of Graph such that all edges have
distinct end colors, where N=<K. The mapping is represented as an ordered list of
Vertex-Color pairs. N is not necessarily minimal.
224
SICStus Prolog
Chapter 24: Weighted Graph Operations
225
24 Weighted Graph Operations
A weighted directed graph (wgraph) is represented as a list of (vertex-edgelist) pairs, where the
pairs are in standard order (as produced by keysort with unique keys), the edgelist is a list of
(neighbor-weight) pair also in standard order (as produced by keysort with unique keys), every
weight is a nonnegative integer, every neighbor appears as a vertex even if it has no neighbors itself,
and no vertex is a neighbor to itself.
An undirected graph is represented as a directed graph where for each edge (U,V) there is a
symmetric edge (V,U).
An edge (U,V) with weight W is represented as the term U-(V-W). U and V must be distinct.
A vertex can be any term. Two vertices are distinct i they are not identical (==).
A path from u to v is represented as a list of vertices, beginning with u and ending with v. A
vertex cannot appear twice in a path. A path is maximal in a graph if it cannot be extended.
A tree is a tree-shaped directed graph (all vertices have a single predecessor, except the root node,
which has none).
A strongly connected component of a graph is a maximal set of vertices where each vertex has a
path in the graph to every other vertex.
Sets are represented as ordered lists (see Chapter 18 [Ordsets], page 207).
To load the package, enter the query
| ?- use_module(library(wgraphs)).
The following predicates are dened for directed graphs.
wgraph_to_ugraph(+WeightedGraph,
-Graph)
Graph has the same vertices and edges as WeightedGraph, except the edges of Graph
are unweighted.
ugraph_to_wgraph(+Graph, -WeightedGraph)
WeightedGraph has the same vertices and edges as Graph, except the edges of WeightedGraph all have weight 1.
vertices_edges_to_wgraph(+Vertices, +Edges, -WeightedGraph)
Vertices is a list of vertices, Edges is a list of edges, and WeightedGraph is a graph
built from Vertices and Edges. Vertices and Edges may be in any order. The vertices
mentioned in Edges do not have to occur explicitly in Vertices. Vertices may be used
to specify vertices that are not connected by any edges.
vertices(+WeightedGraph, -Vertices )
Unies Vertices with the vertices in WeightedGraph.
edges(+WeightedGraph, -Edges )
Unies Edges with the edges in WeightedGraph.
226
add_vertices(+WeightedGraph1, +Vertices,
SICStus Prolog
-WeightedGraph2 )
WeightedGraph2 is WeightedGraph1 with Vertices added to it.
del_vertices(+WeightedGraph1, +Vertices, -WeightedGraph2 )
WeightedGraph2 is WeightedGraph1 with Vertices and all edges to and from them
removed from it.
add_edges(+WeightedGraph1, +Edges, -WeightedGraph2 )
WeightedGraph2 is WeightedGraph1 with Edges and their \to" and \from" vertices
added to it.
del_edges(+WeightedGraph1, +Edges, -WeightedGraph2 )
WeightedGraph2 is WeightedGraph1 with Edges removed from it.
transpose(+WeightedGraph, -Transpose )
Transpose is the graph computed by replacing each edge (u,v) in WeightedGraph by
its symmetric edge (v,u). It can only be used one way around. Takes O(N^2) time.
neighbors(+Vertex, +WeightedGraph, -Neighbors )
neighbours(+Vertex, +WeightedGraph, -Neighbors )
Vertex is a vertex in WeightedGraph and Neighbors are its weighted neighbors.
transitive_closure(+WeightedGraph, -Closure )
Computes Closure as the transitive closure of WeightedGraph in O(N^3) time.
symmetric_closure(+WeightedGraph, -Closure )
Computes Closure as the symmetric closure of WeightedGraph, i.e. for each edge (u,v)
in WeightedGraph, add its symmetric edge (v,u). Takes O(N^2) time. This is useful
for making a directed graph undirected.
top_sort(+WeightedGraph, -Sorted )
Finds a topological ordering of a WeightedGraph and returns the ordering as a list of
Sorted vertices. Fails i no ordering exists, i.e. i the graph contains cycles. Takes
O(N^2) time.
max_path(+V1, +V2, +WeightedGraph, -Path, -Cost)
Path is a maximum-cost path of cost Cost from V1 to V2 in WeightedGraph, there
being no cyclic paths from V1 to V2. Takes O(N^2) time.
min_path(+V1, +V2, +WeightedGraph, -Path, -Cost)
Path is a minimum-cost path of cost Cost from V1 to V2 in WeightedGraph. Takes
O(N^2) time.
min_paths(+Vertex, +WeightedGraph, -Tree )
Tree is a tree of all the minimum-cost paths from Vertex to every other vertex in
WeightedGraph. This is the single-source minimum-cost paths problem.
path(+Vertex, +WeightedGraph, -Path)
Given a WeightedGraph and a Vertex of WeightedGraph, returns a maximal Path
rooted at Vertex, enumerating more paths on backtracking.
Chapter 24: Weighted Graph Operations
227
reduce(+WeightedGraph,
-Reduced )
Reduced is the reduced graph for WeightedGraph. The vertices of the reduced graph
are the strongly connected components of WeightedGraph. There is an edge in Reduced
from u to v i there is an edge in WeightedGraph from one of the vertices in u to one
of the vertices in v.
reachable(+Vertex, +WeightedGraph, -Reachable )
Given a WeightedGraph and a Vertex of WeightedGraph, returns the set of vertices
that are reachable from that Vertex. Takes O(N^2) time.
random_wgraph(+P, +N, +W, -WeightedGraph)
Where P is a probability, unies WeightedGraph with a random graph of vertices 1..N
where each possible edge is included with probability P and random weight in 1..W.
The following predicate is dened for undirected graphs only.
min_tree(+WeightedGraph,
-Tree, -Cost)
Tree is a minimum-cost spanning tree of WeightedGraph with cost Cost, if it exists.
228
SICStus Prolog
Chapter 25: Socket I/O
229
25 Socket I/O
This library package denes a number of predicates manipulating sockets. They are all rather
straight-forward interfaces to the corresponding BSD-type socket functions with the same name
(except current_host/1). The reader should therefore study the appropriate documents for a
deeper description.
The Domain is either the atom 'AF_INET' or 'AF_UNIX'. They correspond directly to the same
domains in BSD-type sockets. 'AF_UNIX' may not be available on non-UNIX platforms.
An Address is either 'AF_INET'(Host,Port) or 'AF_UNIX'(SocketName). Host is an atomic hostname, Port is a portnumber and SocketName is an atom denoting a socket. A reader familiar with
BSD sockets will understand this immediately.
All streams below can be both read from and written on. All I/O-predicates operating on streams
can be used, for example read/2, write/2, format/3, current_stream/3, etc. Socket streams are
unbuered on reads and block buered on writes.
To load the package, enter the query
| ?- use_module(library(sockets)).
socket(+Domain,
-Socket)
A socket Socket in the domain Domain is created.
socket_close(+Socket)
Socket is closed. Sockets used in socket_connect/2 should not be closed by socket_
close/1 as they will be closed when the corresponding stream is closed.
socket_bind(+Socket, 'AF_UNIX'(+SocketName ))
socket_bind(+Socket, 'AF_INET'(?Host,?Port))
The socket Socket is bound to the address. If Port is uninstantiated, the operative
system picks a port number to which Port is bound.
socket_connect(+Socket, 'AF_UNIX'(+SocketName ), -Stream)
socket_connect(+Socket, 'AF_INET'(+Host,+Port), -Stream)
The socket Socket is connected to the address. Stream is a special stream on which
items can be both read and written.
socket_listen(+Socket, +Length)
The socket Socket is dened to have a maximum backlog queue of Length pending
connections.
socket_accept(+Socket, -Stream)
socket_accept(+Socket, -Client, -Stream)
The rst connection to socket Socket is extracted. The stream Stream is opened for
read and write on this connection. For the 'AF_INET' domain, Client will unied with
an atom containing the Internet host address of the connecting entity in numbers-anddots notation. For other domains, Client will not be used.
230
socket_select(+TermsSockets,
SICStus Prolog
-NewTermsStreams, +TimeOut, +Streams, -ReadStreams )
The list of streams in Streams is checked for readable characters. A stream can be any
stream associated with an I/O descriptor. The list ReadStreams returns the streams
with readable data.
socket_select/5 also waits for connections to the sockets specied by TermsSockets. This argument should be a list of Term-Socket pairs, where Term, which
can be any term, is used as an identier. NewTermsStreams is a list of Termconnection(Client,Stream) pairs, where Stream is a new stream open for communicating with a process connecting to the socket identied with Term, Client is the
client host address (see socket_accept/3).
If TimeOut is instantiated to off, the predicate waits until something is available. If
TimeOut is S:U the predicate waits at most S seconds and U microseconds. Both S and
U must be integers >=0. If there is a timeout, ReadStreams and NewTermsStreams are
[].
socket_select(+Sockets, -NewStreams, +TimeOut, +Streams, -ReadStreams )
socket_select(+Socket, -NewStream, +TimeOut, +Streams, -ReadStreams )
socket_select(+Sockets, -NewStreams, -NewClients, +TimeOut, +Streams, -ReadStreams )
socket_select(+Socket, -NewStream, -NewClient, +TimeOut, +Streams, -ReadStreams )
These forms, which are provided for backward compatibility only, diers in how sockets
are specied and new streams returned.
socket_select/(5,6) also wait for connections to the sockets in the list Sockets.
NewStreams is the list of new streams opened for communicating with the connecting
processes. NewClients is the corresponding list of client host addresses (see socket_
accept/3).
The second form requires one socket (not a list) for the rst argument and returns a
stream, NewStream, if a connection is made.
current_host(?HostName )
HostName is unied with the fully qualied name of the machine the process is executing on. The call will also succeed if HostName is instantiated to the unqualied name
of the machine.
hostname_address(+HostName, -HostAddress )
hostname_address(-HostName, +HostAddress )
The Internet host is resolved given either the host name or address. HostAddress
should be an atom containing the Internet host address in numbers-and-dots notation.
The predicate will fail if the host name or address cannot be resolved.
Chapter 26: Linda|Process Communication
231
26 Linda|Process Communication
Linda is a concept for process communication.
For an introduction and a deeper description, see [Carreiro & Gelernter 89a] or [Carreiro & Gelernter
89b], respectively.
One process is running as a server and one or more processes are running as clients. The processes
are communicating with sockets and supports networks.
The server is in principle a blackboard on which the clients can write (out/1), read (rd/1) and
remove (in/1) data. If the data is not present on the blackboard, the predicates suspend the
process until they are available.
There are some more predicates besides the basic out/1, rd/1 and in/1. The in_noblock/1 and
rd_noblock/1 does not suspend if the data is not available|they fail instead. A blocking fetch of
a conjunction of data can be done with in/2 or rd/2.
Example: A simple producer-consumer. In client 1:
producer :produce(X),
out(p(X)),
producer.
produce(X) :- .....
In client 2:
consumer :in(p(A)),
consume(A),
consumer.
consume(A) :- .....
Example: Synchronization
...,
in(ready),
...,
%Waits here until someone does out(ready)
Example: A critical region
...,
in(region_free), % wait for region to be free
critical_part,
out(region_free), % let next one in
...,
232
SICStus Prolog
Example: Reading global data
...,
rd(data(Data)),
...,
or, without blocking:
...,
rd_noblock(data(Data)) ->
do_something(Data)
;
write('Data not available!'),nl
),
...,
Example: Waiting for one of several events
%
...,
in([e(1),e(2),...,e(n)], E),
Here is E instantiated to the first tuple that became available
...,
26.1 Server
The server is the process running the "blackboard process". It is an ordinary SICStus process
which can be run on a separate machine if necessary.
To load the package, enter the query
| ?- use_module(library('linda/server')).
and start the server with linda/0 or linda/1.
Starts a Linda-server in this SICStus. The network address is written to current output
stream as Host:PortNumber.
linda(+Hook )
Starts a Linda-server in this SICStus. When it is started, a goal passed in Hook is
evaluated. Hook must have the form Address-Goal where Address must be uniable
with Host:Port and Goal must be instantiated to a goal. Example:
linda
| ?- linda((Host:Port)-(my_module:mypred(Host,Port))).
will call mypred/2 in module my_module when the server is started. mypred/2 could
start the client-processes, save the address for the clients etc. Note that the module
must be present in Goal.
Chapter 26: Linda|Process Communication
233
26.2 Client
The clients are one or more sicstus processes which have connection(s) to the server.
To load the package, enter the query
| ?- use_module(library('linda/client')).
Some of the following predicates fail if they don't receive an answer from the Linda-server in a
reasonable amount of time. That time is set with the predicate linda_timeout/2.
linda_client(+Address )
Establishes a connection to a Linda-server specied by Address. The Address is of the
format Host:PortNumber as given by linda/0 and linda/1.
It is not possible to be connected to two Linda-servers in the same time.
This predicate can fail due to a timeout.
close_client
Closes the connection to the Linda-server.
linda_timeout(?OldTime, ?NewTime )
This predicate controls Linda's timeout. OldTime is unied with the old timeout
and then timeout is set to NewTime. The value is either off or of the form Seconds:Milliseconds. The former value indicates that the timeout mechanism is disabled,
that is, eternal waiting. The latter form is the timeout-time.
out(+Tuple )
Places the tuple Tuple in Linda's tuple-space.
in(?Tuple )
Removes the tuple Tuple from Linda's tuple-space if it is there. If not, the predicate
blocks until it is available (that is, someone performs an out/1).
in_noblock(?Tuple )
Removes the tuple Tuple from Linda's tuple-space if it is there. If not, the predicate
fails.
This predicate can fail due to a timeout.
in(+TupleList, ?Tuple )
As in/1 but succeeds when either of the tuples in TupleList is available. Tuple is
unied with the fetched tuple. If that unication fails, the tuple is not reinserted in
the tuple-space.
rd(?Tuple )
Succeeds if Tuple is available in the tuple-space, suspends otherwise until it is available.
Compare this with in/1: the tuple is not removed.
rd_noblock(?Tuple )
Succeeds if Tuple is available in the tuple-space, fails otherwise.
This predicate can fail due to a timeout.
234
SICStus Prolog
rd(+TupleList, ?Tuple )
As in/2 but
does not remove any tuples.
bagof_rd_noblock(?Template, +Tuple, ?Bag )
Bag is the list of all instances of Template such that Tuple exists in the tuple-space.
The behavior of variables in Tuple and Template is as in bagof/3. The variables could
be existentially quantied with ^/2 as in bagof/3.
The operation is performed as an atomic operation.
This predicate can fail due to a timeout.
Example: Assume that only one client is connected to the server and that the tuplespace initially is empty.
| ?- out(x(a,3)), out(x(a,4)), out(x(b,3)), out(x(c,3)).
yes
| ?- bagof_rd_noblock(C-N, x(C,N), L).
C = _32,
L = [a-3,a-4,b-3,c-3],
N = _52 ?
yes
| ?- bagof_rd_noblock(C, N^x(C,N), L).
C = _32,
L = [a,a,b,c],
N = _48 ?
yes
Chapter 27: External Storage of Terms (External Database)
235
27 External Storage of Terms (External Database)
This library handles storage and retrieval of terms on les. By using indexing, the store/retrieve
operations are ecient also for large data sets.
The package is loaded by the query
| ?- use_module(library(db)).
27.1 Basics
The idea is to get a behavior similar to assert/1,
stored on les instead of in primary memory.
retract/1
and
clause/2
but the terms are
The dierences compared with the internal database are:
A database must be opened before any access and closed after the last access. (There are
special predicates for this: db_open and db_close.)
The indexing is specied when the database is created. It is possible to index on other parts
of the term than just the functor and rst argument.
Changes aect the database immediately.
The database will store variables with frozen goals as ordinary variables.
Some commercial databases can't store non-ground terms or more than one instance of a term.
The SICStus database can however store terms of either kind.
The database is kept in a secure state and the last stored term is safe even if the SICStus process
dies (machine rebooted, process killed, halt/0, power failure...) (but see Section 27.2 [Current
Limitations], page 235).
27.2 Current Limitations
Only one process can open the same database.
The terms are not necessarily fetched in the same order as they were stored.
If the process dies during the db_store the database could be inconsistent.
At the user's option, a database can be operated in an insecure state where modied pages
are not immediately ushed to disk. This is useful e.g. when a database is created by storing
a large number of terms. See db_buffering/(2-3).
236
SICStus Prolog
27.3 The DB-Spec|Informal Description
The db-spec denes which parts of a term that is used for indexing in a database. It is a structure
with the functor on or off. The arguments are on, off or the same kind of structure.
The db-spec is compared with the indexed term and every argument where there is an on in the
db-spec is indexed.
If the db-spec is of lower arity than the indexed term, the last part of the indexed term is skipped
or vice versa.
The idea of a db-spec are illustrated with a few examples. (A section further down explains the
db-spec in a more formal way).
DB-Spec
Term (the parts with indexing are underlined)
on(on)
a(b)
- -
a(b,c)
- -
a
-
[a,b(c),d]
--
/* as Prolog */
on(on,on)
a(b)
- -
a(b,c)
- - -
a(b,c,d)
- - -
a(b,c(d))
- - -
on(off,on(on))
a(b)
-
a(b,c)
-
a(b,c,d)
-
a(b,c(d)) [a,b(c),d]
- - --
[a,b(c),d]
---
27.4 Predicates
The following conventions are used in the predicate descriptions below.
Mode is either update or read
DBRef is a reference to an open database. The reference is returned when the database is
opened. The reference becomes invalid after the database has been closed.
TermRef is a reference to a term in a given database. The reference is returned when a term
is stored. The reference stays valid even after the database is closed and hence can be stored
permanently as part of another term.
A term generally corresponds to several alias references. db_canonical/(2,3) can be used to
check whether two TermRef s refer to the same term.
Spec is a description of the indexing, see the description of db-specs below.
Term is any Prolog term.
db_open(+Name, +Mode,
db_open(+Name, +Mode,
-DBref )
?Spec, -DBref )
Opens a database with the name Name. The database physically consists of a subdirectory with the same name, containing the les that make up the database. If the
subdirectory does not exist, it is created. In that case Mode must be update.
Chapter 27: External Storage of Terms (External Database)
db_open/4:
db_open/3:
db_close
db_close(+DBref )
237
The db-spec Spec must be ground if opening a new database. If an existing database is opened, Spec is unied with the db-spec given when the
database was created. If the unication fails the predicate fails and an
error is raised.
On creating a new database, the db-spec is on(on), the same kind of
indexing as in the internal database. When opening an existing database,
any db-spec from the database is accepted.
Closes a database. db_close/0 closes the default database. Note that after db_close/0
there is no default database. abort/0 does not close databases.
set_default_db(+DBref )
set_default_db(+Name )
Sets the database Name or DBref to be the default database regardless of whether
there already is one or not.
get_default_db(?DBref )
Unies DBref with the default database.
current_db(?Name, ?Mode, ?Spec, ?DBref )
Unies the arguments with the open databases. This predicate can be used for enumerating all currently open databases through backtracking.
db_store(+Term, -TermRef )
db_store(+DBref, +Term, -TermRef )
Stores Term in the database DBref, which defaults to the default database. TermRef
is unied with a corresponding term reference.
db_fetch(?Term, ?TermRef )
db_fetch(+DBref, ?Term, ?TermRef )
Unies Term with a term from the database DBref, which defaults to the default
database. At the same time, TermRef is unied with a corresponding term reference.
Backtracking over the predicate unies with all terms matching Term. If you simply
want to nd all matching terms, it is more ecient to use db_findall/(2-3).
If TermRef and DBref are instantiated (and the referenced term is not erased), the
referenced term is read and unied with Term.
db_findall(?Term, ?TermList)
db_findall(+DBref, ?Term, ?TermList)
Unies TermList with the list of all terms matching Term from the database DBref,
which defaults to the default database. The list is guaranteed to be free of duplicates,
except in the cases where the same term has actually been stored more than once.
db_erase(+TermRef )
db_erase(+DBref, +TermRef )
Deletes the term in the database DBref, which defaults to the default database, that
is referenced by TermRef if it is not already deleted.
238
SICStus Prolog
db_canonical(+TermRef, -TermID )
db_canonical(+DBref, +TermRef, -TermID )
Returns the canonical term identier for the term in DBref, which defaults to the
default database, that is referenced by TermRef.
db_buffering(?Old, ?New )
db_buffering(+DBref, ?Old, ?New )
Unies Old with the current buering mode of DBref, which defaults to the defaults
database, and sets its buering mode to New, which must be on or off. Buering
is initially off. When it is on, modied pages are not immediately ushed to disk,
enabling faster execution but with a higher risk of inconsistencies if a crash occurs.
db_close always ushes any modied pages.
27.5 An Example Session
| ?- db_open(my_db,update,on(on),R), set_default_db(R).
R = '$db'(1411928) ?
yes
| ?- db_store(a(b),_).
yes
| ?- db_store(a(c),_).
yes
| ?- db_fetch(X,_).
X = a(b) ? ;
X = a(c) ? ;
no
| ?- current_db(A,B,C,D).
A
B
C
D
=
=
=
=
my_db,
update,
on(on),
'$db'(1411928) ? ;
no
| ?- db_close.
yes
Chapter 27: External Storage of Terms (External Database)
239
27.6 The Db-Spec
A db-spec is on of the following terms:
the atom on, or
the atom off, or
a structure with functor on or off and every argument being a db-spec.
The following table denes the way indices are calculated a bit more formally. The table denes
the index as a function INDEX of the db-spec and the indexed term.
The column Index Calculation describes the procedure: a \yes" means that some primitive function
is used, such as a hash function; \INDEX(s,t)" means simply that the function denition (the table)
is applied again, but with a new db-spec s and a new term t; I means on or off.
DB-Spec
Indexed Term
Index Calculation
off
on
I (...)
I (...)
I (S1,S2...Sn)
any
no
any
yes (on principal functor)
atomic
yes
variable
yes
F (A1,A2...Am) yes (on principal functor)
INDEX(Si,Ai) for 1 =< i =< min(n,m)
Every term is stored together with a set of \keywords" for indexing purposes. The space overhead
is approximately 16 bytes per keyword per term. The number of keywords stored depends on the
db-spec and on the term being indexed. The following table denes the number of keywords as a
function K of the db-spec and the indexed term.
DB-Spec
Indexed Term
Number of keywords
off
on
I (...)
I (...)
I (S1,...,Sn)
any
any
atomic
variable
F (A1,...,Am)
0
2
2
2
1+K(S1,A1)*...*K(Sj,Aj), j=min(n,m)
240
SICStus Prolog
Chapter 28: Boolean Constraint Solver
241
28 Boolean Constraint Solver
The clp(B) system provided by this library module is an instance of the general Constraint Logic
Programming scheme introduced in [Jaar & Michaylov 87]. It is a solver for constraints over the
Boolean domain, i.e. the values 0 and 1. This domain is particularly useful for modeling digital
circuits, and the constraint solver can be used for verication, design, optimization etc. of such
circuits.
To load the solver, enter the query:
| ?- use_module(library(clpb)).
The solver contains predicates for checking the consistency and entailment of a constraint wrt.
previous constraints, and for computing particular solutions to the set of previous constraints.
The underlying representation of Boolean functions is based on Boolean Decision Diagrams [Bryant
86]. This representation is very ecient, and allows many combinatorial problems to be solved with
good performance.
Boolean expressions are composed from the following operands: the constants 0 and 1 (FALSE and
TRUE), logical variables, and symbolic constants, and from the following connectives. P and Q
are Boolean expressions, X is a logical variable, Is is a list of integers or integer ranges, and Es is
a list of Boolean expressions:
True if P is false.
P*Q
True if P and Q are both true.
P+Q
True if at least one of P and Q is true.
P#Q
True if exactly one of P and Q is true.
X ^P
True if there exists an X such that P is true. Same as P [X /0] + P [X /1].
P =:= Q Same as ~P # Q.
P =\= Q Same as P # Q.
P =< Q Same as ~P + Q.
P >= Q Same as P + ~Q.
P<Q
Same as ~P * Q.
P>Q
Same as P * ~Q.
card(Is, Es )
True if the number of true expressions in Es is a member of the set denoted by Is.
~
P
Symbolic constants (Prolog atoms) denote parametric values and can be viewed as all-quantied
variables whose quantiers are placed outside the entire expression. They are useful for forcing
certain variables of an equation to be treated as input parameters.
242
SICStus Prolog
28.1 Solver Interface
The following predicates are dened:
sat(+Expression)
Expression is a Boolean expression. This checks the consistency of the expression wrt.
the accumulated constraints, and, if the check succeeds, tells the constraint that the
expression be true.
If a variable X, occurring in the expression, is subsequently unied with some term T,
this is treated as a shorthand for the constraint
?- sat(X=:=T).
taut(+Expression,
?Truth)
Expression is a Boolean expression. This asks whether the expression is now entailed
by the accumulated constraints (Truth=1), or whether its negation is entailed by the
accumulated constraints (Truth=0). Otherwise, it fails.
labeling(+Variables )
Variables is a list of variables. The variables are instantiated to a list of 0s and 1s, in
a way that satises any accumulated constraints. Enumerates all solutions by backtracking, but creates choicepoints only if necessary.
28.2 Examples
28.2.1 Example 1
| ?- sat(X + Y).
sat(X=\=_A*Y#Y) ?
illustrates three facts. First, any accumulated constraints aecting the top-level variables are
displayed as oundered goals, since the query is not true for all X and Y. Secondly, accumulated
constraints are displayed as sat(V =:=Expr ) or sat(V =\=Expr ) where V is a variable and Expr
is a \polynomial", i.e. an exclusive or of conjunctions of variables and constants. Thirdly, _A had
to be introduced as an articial variable, since Y cannot be expressed as a function of X. That is,
X + Y is true i there exists an _A such that X=\=_A*Y#Y. Let's check it!
| ?- taut(_A ^ (X=\=_A*Y#Y) =:= X + Y, T).
T = 1 ?
veries the above answer. Notice that the formula in this query is a tautology, and so it is entailed
by an empty set of constraints.
Chapter 28: Boolean Constraint Solver
243
28.2.2 Example 2
| ?- taut(A =< C, T).
no
| ?- sat(A =< B), sat(B =< C), taut(A =< C, T).
T = 1,
sat(A=:=_A*_B*C),
sat(B=:=_B*C) ?
| ?- taut(a, T).
T = 0 ?
yes
| ?- taut(~a, T).
T = 0 ?
illustrates the entailment predicate. In the rst query, the expression \A implies C" is neither
known to be true nor false, so the query fails. In the second query, the system is told that \A
implies B" and \B implies C", so \A implies C" is entailed. The expressions in the third and fourth
queries are to be read \for each a, a is true" and \for each a, a is false", respectively, and so T
= 0 in both cases since both are unsatisable. This illustrates the fact that the implicit universal
quantiers introduced by symbolic constants are placed in front of the entire expression.
28.2.3 Example 3
| ?- [user].
| adder(X, Y, Sum, Cin, Cout) :sat(Sum =:= card([1,3],[X,Y,Cin])),
sat(Cout =:= card([2-3],[X,Y,Cin])).
| {user consulted, 40 msec 576 bytes}
yes
| ?- adder(x, y, Sum, cin, Cout).
sat(Sum=:=cin#x#y),
sat(Cout=:=x*cin#x*y#y*cin) ?
yes
| ?- adder(x, y, Sum, 0, Cout).
sat(Sum=:=x#y),
sat(Cout=:=x*y) ?
yes
244
SICStus Prolog
| ?- adder(X, Y, 0, Cin, 1), labeling([X,Y,Cin]).
Cin = 0,
X = 1,
Y = 1 ? ;
Cin = 1,
X = 0,
Y = 1 ? ;
Cin = 1,
X = 1,
Y = 0 ? ;
illustrates the use of cardinality constraints and models a one-bit adder circuit. The rst query
illustrates how representing the input signals by symbolic constants forces the output signals to be
displayed as functions of the inputs and not vice versa. The second query computes the simplied
functions obtained by setting carry-in to 0. The third query asks for particular input values
satisfying sum and carry-out being 0 and 1, respectively.
28.2.4 Example 4
The predicate fault/3 below describes a 1-bit adder consisting of ve gates, with at most one
faulty gate. If one of the variables Fi is equal to 1, the corresponding gate is faulty, and its output
signal is undened (i.e., the constraint representing the gate is relaxed).
Assuming that we have found some incorrect output from a circuit, we are interesting in nding
the faulty gate. Two instances of incorrect output are listed in fault_ex/2:
fault([F1,F2,F3,F4,F5], [X,Y,Cin], [Sum,Cout]) :sat(
card([0-1],[F1,F2,F3,F4,F5]) *
(F1 + (U1 =:= X * Cin)) *
(F2 + (U2 =:= Y * U3)) *
(F3 + (Cout =:= U1 + U2)) *
(F4 + (U3 =:= X # Cin)) *
(F5 + (Sum =:= Y # U3))
).
fault_ex(1, Faults) :- fault(Faults, [1,1,0], [1,0]).
fault_ex(2, Faults) :- fault(Faults, [1,0,1], [0,0]).
To nd the faulty gates, we run the query
| ?- fault_ex(I,L), labeling(L).
I = 1,
L = [0,0,0,1,0] ? ;
Chapter 28: Boolean Constraint Solver
245
I = 2,
L = [1,0,0,0,0] ? ;
I = 2,
L = [0,0,1,0,0] ? ;
no
Thus for input data [1,1,0], gate 4 must be faulty. For input data [1,0,1], either gate 1 or gate
3 must be faulty.
To get a symbolic representation of the outputs interms of the input, we run the query
| ?- fault([0,0,0,0,0], [x,y,cin], [Sum,Cout]).
sat(Cout=:=x*cin#x*y#y*cin),
sat(Sum=:=cin#x#y)
which shows that the sum and carry out signals indeed compute the intended functions if no gate
is faulty.
246
SICStus Prolog
Chapter 29: Constraint Logic Programming over Rationals or Reals
247
29 Constraint Logic Programming over Rationals or
Reals
29.1 Introduction
The clp(Q,R) system described in this document is an instance of the general Constraint Logic
Programming scheme introduced by [Jaar & Michaylov 87].
The implementation is at least as complete as other existing clp(R) implementations: It solves
linear equations over rational or real valued variables, covers the lazy treatment of nonlinear equations, features a decision algorithm for linear inequalities that detects implied equations, removes
redundancies, performs projections (quantier elimination), allows for linear dis-equations, and
provides for linear optimization.
The full clp(Q,R) distribution, including a stand-alone manual and an examples directory that is
possibly more up to date than the version in the SICStus Prolog distribution, is available from:
http://www.ai.univie.ac.at/clpqr/.
Referencing this Software
When referring to this implementation of clp(Q,R) in publications, you should use the following
reference:
Holzbaur C.: OFAI clp(q,r) Manual, Edition 1.3.3, Austrian Research Institute for
Articial Intelligence, Vienna, TR-95-09, 1995.
Acknowledgments
The development of this software was supported by the Austrian Fonds zur Foerderung der Wissenschaftlichen Forschung under grant P9426-PHY. Financial support for the Austrian Research
Institute for Articial Intelligence is provided by the Austrian Federal Ministry for Science and
Research.
We include a collection of examples that has been distributed with the Monash University version
of clp(R) [Heintze et al. 87], and its inclusion into this distribution was kindly permitted by Roland
Yap.
248
SICStus Prolog
29.2 Solver Interface
Until rational numbers become rst class citizens in SICStus Prolog, rational arithmetics has to
be emulated. Because of the emulation it is too expensive to support arithmetics with automatic
coercion between all sorts of numbers, like you nd it in CommonLisp, for example.
You must choose whether you want to operate in the eld of Q (Rationals) or R (Reals):
| ?- use_module(library(clpq)).
or
| ?- use_module(library(clpr)).
You can also load both modules, but the exported predicates listed below will name-clash (see Section 4.4 [Importation], page 49). You can avoid the interactive resolution dialog if the importation
is skipped, e.g. via: use_module(library(clpq),[]),use_module(library(clpr),[]).
Notational Conventions
Throughout this chapter, the prompts clp(q) ?- and clp(r) ?- are used to dierentiate between
clp(Q) and clp(R) in exemplary interactions.
In general there are many ways to express the same linear relationship. This degree of freedom is
manifest in the fact that the printed manual and an actual interaction with the current version of
clp(Q,R) may show syntactically dierent answer constraints, despite the fact the same semantic
relationship is being expressed. There are means to control the presentation; see Section 29.5.1
[Variable Ordering], page 260. The approximative nature of oating point numbers may also
produce numerical dierences between the text in this manual and the actual results of clp(R), for
a given edition of the software.
29.2.1 Solver Predicates
The solver interface for both Q and R consists of the following predicates which are exported from
module(linear).
{+Constraint}
Constraint is a term accepted by the the grammar below. The corresponding constraint
is added to the current constraint store and checked for satisability. Use the module
prex to distinguish the solvers if both clp(Q) and clp(R) were loaded
| ?- clpr:{Ar+Br=10}, Ar=Br, clpq:{Aq+Bq=10}, Aq=Bq.
Aq
Ar
Bq
Br
=
=
=
=
5,
5.0,
5,
5.0
Chapter 29: Constraint Logic Programming over Rationals or Reals
249
Although clp(Q) and clp(R) are independent modules, you are asking for trouble if you
(accidently) share variables between them:
| ?- clpr:{A+B=10}, clpq:{A=B}.
{TYPE ERROR: _118=5.0 - arg 2: expected 'a rational number', found 5.0}
This is because both solvers eventually compute values for the variables and Reals are
incompatible with Rationals.
Here is the constraint grammar:
Constraint -->
C
| C , C
conjunction
C
-->
Expr
|
|
|
|
|
|
Expr =:= Expr
Expr = Expr
Expr < Expr
Expr > Expr
Expr =< Expr
Expr >= Expr
Expr =\= Expr
equation
equation
strict inequation
strict inequation
nonstrict inequation
nonstrict inequation
disequation
Prolog variable
oating point or integer
unary plus
unary minus
addition
subtraction
multiplication
division
absolute value
trigonometric sine
trigonometric cosine
trigonometric tangent
raise to the power
raise to the power
minimum of the two arguments
maximum of the two arguments
symbolic numerical constants
Conjunctive constraints {C,C} have been made part of the syntax to control the granularity of constraint submission, which will be exploited by future versions of this
software. Symbolic numerical constants are provided for compatibility only; see Section 29.7.1 [Monash Examples], page 266.
entailed(+Constraint)
Succeeds i the linear Constraint is entailed by the current constraint store. This
predicate does not change the state of the constraint store.
-->
variable
| number
| + Expr
| - Expr
| Expr + Expr
| Expr - Expr
| Expr * Expr
| Expr / Expr
| abs(Expr)
| sin(Expr)
| cos(Expr)
| tan(Expr)
| pow(Expr,Expr)
| exp(Expr,Expr)
| min(Expr,Expr)
| max(Expr,Expr)
| #(Const)
clp(q) ?- {A =< 4}, entailed(A=\=5).
{A=<4}
yes
250
SICStus Prolog
clp(q) ?- {A =< 4}, entailed(A=\=3).
no
inf(+Expr,
-Inf )
Computes the inmum of the linear expression Expr and unies it with Inf. Failure
indicates unboundedness.
sup(+Expr, -Sup )
Computes the supremum of the linear expression Expr and unies it with Sup. Failure
indicates unboundedness.
clp(q) ?- { 2*X+Y =< 16, X+2*Y =< 11,
X+3*Y =< 15, Z = 30*X+50*Y
}, sup(Z, Sup).
Sup = 310,
{Z=30*X+50*Y},
{X+1/2*Y=<8},
{X+3*Y=<15},
{X+2*Y=<11}
minimize(+Expr )
Computes the inmum of the linear expression Expr and equates it with the expression,
i.e. as if dened as:
minimize(Expr) :- inf(Expr, Expr).
maximize(+Expr )
Computes the supremum of the linear expression Expr and equates it with the expression.
clp(q) ?- { 2*X+Y =< 16, X+2*Y =< 11,
X+3*Y =< 15, Z = 30*X+50*Y
}, maximize(Z).
X = 7,
Y = 2,
Z = 310
bb_inf(+Ints, +Expr,
-Inf )
Computes the inmum of the linear expression Expr under the additional constraint
that all of variables in the list Ints assume integral values at the inmum. This allows
for the solution of mixed integer linear optimization problems; see Section 29.8 [MIP],
page 268.
clp(q) ?- {X >= Y+Z, Y > 1, Z > 1}, bb_inf([Y,Z],X,Inf).
Inf = 4,
{Y>1},
{Z>1},
{X-Y-Z>=0}
Chapter 29: Constraint Logic Programming over Rationals or Reals
251
bb_inf(+Ints, +Expr,
-Inf, -Vertex, +Eps )
Computes the inmum of the linear expression Expr under the additional constraint
that all of variables in the list Ints assume integral values at the inmum. Eps is a
positive number between 0 and 0.5 that species how close a number X must be to
the next integer to be considered integral: abs(round(X)-X) < Eps. The predicate
bb_inf/3 uses Eps = 0.001. With clp(Q), Eps = 0 makes sense. Vertex is a list of the
same length as Ints and contains the (integral) values for Ints, such that the inmum
is produced when assigned. Note that this will only generate one particular solution,
which is dierent from the situation with minimize/1, where the general solution is
exhibited.
ordering(+Spec )
Provides a means to control one aspect of the presentation of the answer constraints;
see Section 29.5.1 [Variable Ordering], page 260.
dump(+Target, -NewVars, -CodedAnswer )
Reects the constraints on the target variables into a term, where Target and NewVars
are lists of variables of equal length and CodedAnswer is the term representation of
the projection of constraints onto the target variables where the target variables are
replaced by the corresponding variables from NewVars (see Section 29.5.2 [Turning
Answers into Terms], page 261).
clp(q) ?- {A+B =< 10, A>=4},
dump([A,B],Vs,Cs),
dump([B],Bp,Cb).
Cb = [_A=<6],
Bp = [_A],
Cs = [_B>=4,_C+_B=<10],
Vs = [_C,_B],
{A>=4},
{A+B=<10}
The current version of dump/3 is incomplete with respect to nonlinear constraints. It
only reports nonlinear constraints that are connected to the target variables. The
following example has no solution. From the top level's report we have a chance to
deduce this fact, but dump/3 currently has no means to collect global constraints ...
q(X) :{X>=10},
{sin(Z)>3}.
clp(r) ?- q(X), dump([X],V,C).
C = [_A>=10.0],
V = [_A],
clpr:{3.0-sin(_B)<0.0},
{X>=10.0}
252
SICStus Prolog
29.2.2 Unication
Equality constraints are added to the store implicitly each time variables that have been mentioned
in explicit constraints are bound - either to another such variable or to a number.
clp(r) ?- {2*A+3*B=C/2}, C=10.0, A=B.
A = 1.0,
B = 1.0,
C = 10.0
Is equivalent modulo rounding errors to
clp(r) ?- {2*A+3*B=C/2, C=10, A=B}.
A = 1.0,
B = 0.9999999999999999,
C = 10.0
The shortcut bypassing the use of {}/1 is allowed and makes sense because the interpretation of
this equality in Prolog and clp(R) coincides. In general, equations involving interpreted functors,
+/2 in this case, must be fed to the solver explicitly:
clp(r) ?- X=3.0+1.0, X=4.0.
no
Further, variables known by clp(R) may be bound directly to oats only. Likewise, variables
known by clp(Q) may be bound directly to rational numbers only; see Section 29.9.1.1 [Rationals],
page 269. Failing to do so is rewarded with an exception:
clp(q) ?- {2*A+3*B=C/2}, C=10.0, A=B.
{TYPE ERROR: _165=10.0 - arg 2: expected 'a rational number', found 10.0}
This is because 10.0 is not a rational constant. To make clp(Q) happy you have to say:
clp(q) ?- {2*A+3*B=C/2}, C=rat(10,1), A=B.
A = 1,
B = 1,
C = 10
If you use {}/1, you don't have to worry about such details. Alternatively, you may use the
automatic expansion facility, check Section 29.7 [Syntactic Sugar], page 265.
Chapter 29: Constraint Logic Programming over Rationals or Reals
253
29.2.3 Feedback and Bindings
What was covered so far was how the user populates the constraint store. The other direction of
the information ow consists of the success and failure of the above predicates and the binding of
variables to numerical values and the aliasing of variables. Example:
clp(r) ?- {A-B+C=10, C=5+5}.
B = A,
C = 10.0
The linear constraints imply A=B and the solver consequently exports this binding to the Prolog
world, which is manifest in the fact that the test A==B will succeed. More about answer presentation
in Section 29.5 [Projection], page 259.
29.3 Linearity and Nonlinear Residues
The clp(Q,R) system is restricted to deal with linear constraints because the decision algorithms
for general nonlinear constraints are prohibitively expensive to run. If you need this functionality
badly, you should look into symbolic algebra packages. Although the clp(Q,R) system cannot
solve nonlinear constraints, it will collect them faithfully in the hope that through the addition of
further (linear) constraints they might get simple enough to solve eventually. If an answer contains
nonlinear constraints, you have to be aware of the fact that success is qualied modulo the existence
of a solution to the system of residual (nonlinear) constraints:
clp(r) ?- {sin(X) = cos(X)}.
clpr:{sin(X)-cos(X)=0.0}
There are indeed innitely many solutions to this constraint (X = 0.785398 + n*Pi), but clp(Q,R)
has no direct means to nd and represent them.
The systems goes through some lengths to recognize linear expressions as such. The method is
based on a normal form for multivariate polynomials. In addition, some simple isolation axioms,
that can be used in equality constraints, have been added. The current major limitation of the
method is that full polynomial division has not been implemented. Examples:
This is an example where the isolation axioms are sucient to determine the value of X.
clp(r) ?- {sin(cos(X)) = 1/2}.
X = 1.0197267436954502
If we change the equation into an inequation, clp(Q,R) gives up:
clp(r) ?- {sin(cos(X)) < 1/2}.
clpr:{sin(cos(X))-0.5<0.0}
254
SICStus Prolog
The following is easy again:
clp(r) ?- {sin(X+2+2)/sin(4+X) = Y}.
Y = 1.0
And so is this:
clp(r) ?- {(X+Y)*(Y+X)/X = Y*Y/X+99}.
{Y=49.5-0.5*X}
An ancient symbol manipulation benchmark consists in rising the expression X+Y+Z+1 to the 15th
power:
clp(q) ?- {exp(X+Y+Z+1,15)=0}.
clpq:{Z^15+Z^14*15+Z^13*105+Z^12*455+Z^11*1365+Z^10*3003+...
... polynomial continues for a few pages ...
=0}
Computing its roots is another story.
29.3.1 How Nonlinear Residues are made to disappear
Binding variables that appear in nonlinear residues will reduce the complexity of the nonlinear
expressions and eventually results in linear expressions:
clp(q) ?- {exp(X+Y+1,2) = 3*X*X+Y*Y}.
clpq:{Y*2-X^2*2+Y*X*2+X*2+1=0}
Equating X and Y collapses the expression completely and even determines the values of the two
variables:
clp(q) ?- {exp(X+Y+1,2) = 3*X*X+Y*Y}, X=Y.
X = -1/4,
Y = -1/4
29.3.2 Isolation Axioms
These axioms are used to rewrite equations such that the variable to be solved for is moved to
the left hand side and the result of the evaluation of the right hand side can be assigned to the
variable. This allows, for example, to use the exponentiation operator for the computation of roots
and logarithms, see below.
A=B*C
Residuates unless B or C is ground or A and B or C are ground.
Chapter 29: Constraint Logic Programming over Rationals or Reals
A=B/C
255
Residuates unless C is ground or A and B are ground.
X = min(Y,Z)
Residuates unless Y and Z are ground.
X = max(Y,Z)
Residuates unless Y and Z are ground.
X = abs(Y)
Residuates unless Y is ground.
X = pow(Y,Z), X = exp(Y,Z)
Residuates unless any pair of two of the three variables is ground. Example:
clp(r) ?- { 12=pow(2,X) }.
X = 3.5849625007211565
clp(r) ?- { 12=pow(X,3.585) }.
X = 1.9999854993443926
clp(r) ?- { X=pow(2,3.585) }.
X = 12.000311914286545
X = sin(Y)
Residuates unless X or Y is ground. Example:
clp(r) ?- { 1/2 = sin(X) }.
X = 0.5235987755982989
X = cos(Y)
X = tan(Y)
Residuates unless X or Y is ground.
Residuates unless X or Y is ground.
29.4 Numerical Precision and Rationals
The fact that you can switch between clp(R) and clp(Q) should solve most of your numerical
problems regarding precision. Within clp(Q), oating point constants will be coerced into rational
numbers automatically. Transcendental functions will be approximated with rationals. The precision of the approximation is limited by the oating point precision. These two provisions allow you
to switch between clp(R) and clp(Q) without having to change your programs.
What is to be kept in mind however is the fact that it may take quite big rationals to accommodate
the required precision. High levels of precision are for example required if your linear program
is ill-conditioned, i.e., in a full rank system the determinant of the coecient matrix is close to
256
SICStus Prolog
zero. Another situation that may call for elevated levels of precision is when a linear optimization
problem requires exceedingly many pivot steps before the optimum is reached.
If your application approximates irrational numbers, you may be out of space particularly soon.
The following program implements N steps of Newton's approximation for the square root function
at point 2.
%
% from file: library('clpqr/examples/root')
%
root(N, R) :root(N, 1, R).
root(0, S, R) :- !, S=R.
root(N, S, R) :N1 is N-1,
{ S1 = S/2 + 1/S },
root(N1, S1, R).
It is known that this approximation converges quadratically, which means that the number of correct
digits in the decimal expansion roughly doubles with each iteration. Therefore the numerator and
denominator of the rational approximation have to grow likewise:
clp(q) ?- use_module(library('clpqr/examples/root')).
clp(q) ?- root(3,R),print_decimal(R,70).
1.4142156862 7450980392 1568627450 9803921568 6274509803 9215686274
5098039215
R = 577/408
clp(q) ?- root(4,R),print_decimal(R,70).
1.4142135623 7468991062 6295578890 1349101165 5962211574 4044584905
0192000543
R = 665857/470832
clp(q) ?- root(5,R),print_decimal(R,70).
1.4142135623 7309504880 1689623502 5302436149 8192577619 7428498289
4986231958
R = 886731088897/627013566048
clp(q) ?- root(6,R),print_decimal(R,70).
1.4142135623 7309504880 1688724209 6980785696 7187537723 4001561013
1331132652
R = 1572584048032918633353217/1111984844349868137938112
clp(q) ?- root(7,R),print_decimal(R,70).
Chapter 29: Constraint Logic Programming over Rationals or Reals
257
1.4142135623 7309504880 1688724209 6980785696 7187537694 8073176679
7379907324
R = 4946041176255201878775086487573351061418968498177 /
3497379255757941172020851852070562919437964212608
Iterating for 8 steps produces no further change in the rst 70 decimal digits of sqrt(2). After
15 steps the approximating rational number has a numerator and a denominator with 12543 digits
each, and the next step runs out of memory.
Another irrational number that is easily computed is e. The following program implements an
alternating series for 1/e, where the absolute value of last term is an upper bound on the error.
%
% from file: library('clpqr/examples/root')
%
e(N, E) :{ Err =:= exp(10,-(N+2)), Half =:= 1/2 },
inv_e_series(Half, Half, 3, Err, Inv_E),
{ E =:= 1/Inv_E }.
inv_e_series(Term, S0, _, Err, Sum) :{ abs(Term) =< Err }, !,
S0 = Sum.
inv_e_series(Term, S0, N, Err, Sum) :N1 is N+1,
{ Term1 =:= -Term/N, S1 =:= Term1+S0 },
inv_e_series(Term1, S1, N1, Err, Sum).
The computation of the rational number E that approximates e up to at least 1000 digits in
its decimal expansion requires the evaluation of 450 terms of the series, i.e. 450 calls of inv_e_
series/5.
clp(q) ?- e(1000,E).
E = 7149056228932760213666809592072842334290744221392610955845565494
3708750229467761730471738895197792271346693089326102132000338192
0131874187833985420922688804220167840319199699494193852403223700
5853832741544191628747052136402176941963825543565900589161585723
4023097417605004829991929283045372355639145644588174733401360176
9953973706537274133283614740902771561159913069917833820285608440
3104966899999651928637634656418969027076699082888742481392304807
9484725489080844360397606199771786024695620205344042765860581379
3538290451208322129898069978107971226873160872046731879753034549
3130492167474809196348846916421782850086985668680640425192038155
4902863298351349469211627292865440876581064873866786120098602898
8799130098877372097360065934827751120659213470528793143805903554
7928682131082164366007016698761961066948371407368962539467994627
1374858249110795976398595034606994740186040425117101588480000000
258
SICStus Prolog
0000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000
/
2629990810403002651095959155503002285441272170673105334466808931
6863103901346024240326549035084528682487048064823380723787110941
6809235187356318780972302796570251102928552003708556939314795678
1978390674393498540663747334079841518303636625888963910391440709
0887345797303470959207883316838346973393937778363411195624313553
8835644822353659840936818391050630360633734935381528275392050975
7271468992840907541350345459011192466892177866882264242860412188
0652112744642450404625763019639086944558899249788084559753723892
1643188991444945360726899532023542969572584363761073528841147012
2634218045463494055807073778490814692996517359952229262198396182
1838930043528583109973872348193806830382584040536394640895148751
0766256738740729894909630785260101721285704616818889741995949666
6303289703199393801976334974240815397920213059799071915067856758
6716458821062645562512745336709063396510021681900076680696945309
3660590933279867736747926648678738515702777431353845466199680991
73361873421152165477774911660108200059
The decimal expansion itself looks like this:
clp(q) ?- e(1000, E),
2.
7182818284 5904523536
6277240766 3035354759
8174135966 2904357290
8298807531 9525101901
7614606680 8226480016
5517027618 3860626133
7093287091 2744374704
4637721112 5238978442
9316368892 3009879312
6680331825 2886939849
3012381970 6841614039
7825098194 5581530175
5988885193 4580727386
4841984443 6346324496
3043699418 4914631409
7683964243 7814059271
1718986106 8739696552
print_decimal(E, 1000).
0287471352
4571382178
0334295260
1573834187
8477411853
1384583000
7230696977
5056953696
7736178215
6465105820
7019837679
6717361332
6738589422
8487560233
3431738143
4563549061
1267154688
6624977572
5251664274
5956307381
9307021540
7423454424
7520449338
2093101416
7707854499
4249992295
9392398294
3206832823
0698112509
8792284998
6248270419
6405462531
3031072085
9570350354
4709369995
2746639193
3232862794
8914993488
3710753907
2656029760
9283681902
6996794686
7635148220
8879332036
7646480429
9618188159
9208680582
7862320900
5209618369
1038375051
9574966967
2003059921
3490763233
4167509244
7744992069
6737113200
5515108657
4454905987
8269895193
2509443117
5311802328
3041690351
5749279610
2160990235
0888707016
0115747704
Chapter 29: Constraint Logic Programming over Rationals or Reals
259
29.5 Projection and Redundancy Elimination
Once a derivation succeeds, the Prolog system presents the bindings for the variables in the query.
In a CLP system, the set of answer constraints is presented in analogy. A complication in the
CLP context are variables and associated constraints that were not mentioned in the query. A
motivating example is the familiar mortgage relation:
%
% from file: library('clpqr/examples/mg')
%
mg(P,T,I,B,MP):{
T = 1,
B + MP = P * (1 + I)
}.
mg(P,T,I,B,MP):{
T > 1,
P1 = P * (1 + I) - MP,
T1 = T - 1
},
mg(P1, T1, I, B, MP).
A sample query yields:
clp(r) ?- use_module(library('clpqr/examples/mg')).
clp(r) ?- mg(P,12,0.01,B,Mp).
{B=1.1268250301319698*P-12.682503013196973*Mp}
Without projection of the answer constraints onto the query variables we would observe the following interaction:
clp(r) ?- mg(P,12,0.01,B,Mp).
{B=12.682503013196973*_A-11.682503013196971*P},
{Mp= -(_A)+1.01*P},
{_B=2.01*_A-1.01*P},
{_C=3.0301*_A-2.0301*P},
{_D=4.060401000000001*_A-3.0604009999999997*P},
{_E=5.101005010000001*_A-4.10100501*P},
{_F=6.152015060100001*_A-5.152015060099999*P},
{_G=7.213535210701001*_A-6.213535210700999*P},
{_H=8.285670562808011*_A-7.285670562808009*P},
{_I=9.368527268436091*_A-8.36852726843609*P},
{_J=10.462212541120453*_A-9.46221254112045*P},
{_K=11.566834666531657*_A-10.566834666531655*P}
260
SICStus Prolog
The variables A K are not part of the query, they originate from the mortgage program proper.
Although the latter answer is equivalent to the former in terms of linear algebra, most users would
prefer the former.
:::
29.5.1 Variable Ordering
In general, there are many ways to express the same linear relationship between variables. clp(Q,R)
does not care to distinguish between them, but the user might. The predicate ordering(+Spec )
gives you some control over the variable ordering. Suppose that instead of B, you want Mp to be
the dened variable:
clp(r) ?- mg(P,12,0.01,B,Mp).
{B=1.1268250301319698*P-12.682503013196973*Mp}
This is achieved with:
clp(r) ?-
mg(P,12,0.01,B,Mp), ordering([Mp]).
{Mp= -0.0788487886783417*B+0.08884878867834171*P}
One could go one step further and require P to appear before (to the left of) B in a addition:
clp(r) ?- mg(P,12,0.01,B,Mp), ordering([Mp,P]).
{Mp=0.08884878867834171*P-0.0788487886783417*B}
Spec in ordering(+Spec ) is either a list of variables with the intended ordering, or of the form A<B.
The latter form means that A goes to the left of B. In fact, ordering([A,B,C,D]) is shorthand
for:
ordering(A < B), ordering(A < C), ordering(A < D),
ordering(B < C), ordering(B < D),
ordering(C < D)
The ordering specication only aects the nal presentation of the constraints. For all other
operations of clp(Q,R), the ordering is immaterial. Note that ordering/1 acts like a constraint:
you can put it anywhere in the computation, and you can submit multiple specications.
clp(r) ?- ordering(B < Mp), mg(P,12,0.01,B,Mp).
{B= -12.682503013196973*Mp+1.1268250301319698*P}
yes
clp(r) ?- ordering(B < Mp), mg(P,12,0.01,B,Mp), ordering(P < Mp).
{P=0.8874492252651537*B+11.255077473484631*Mp}
Chapter 29: Constraint Logic Programming over Rationals or Reals
261
29.5.2 Turning Answers into Terms
In meta-programming applications one needs to get a grip on the results computed by the clp(Q,R)
solver. You can use the predicates dump/3 and/or call_residue/2 for that purpose:
clp(r) ?- {2*A+B+C=10,C-D=E,A<10}, dump([A,B,C,D,E],[a,b,c,d,e],Constraints).
Constraints = [e<10.0,a=10.0-c-d-2.0*e,b=c+d],
{C=10.0-2.0*A-B},
{E=10.0-2.0*A-B-D},
{A<10.0}
clp(r) ?- call_residue({2*A+B+C=10,C-D=E,A<10}, Constraints).
Constraints = [
]
[A]-{A<10.0},
[B]-{B=10.0-2.0*A-C},
[D]-{D=C-E}
29.5.3 Projecting Inequalities
As soon as linear inequations are involved, projection gets more demanding complexity wise. The
current clp(Q,R) version uses a Fourier-Motzkin algorithm for the projection of linear inequalities.
The choice of a suitable algorithm is somewhat dependent on the number of variables to be eliminated, the total number of variables, and other factors. It is quite easy to produce problems of
moderate size where the elimination step takes some time. For example, when the dimension of the
projection is 1, you might be better o computing the supremum and the inmum of the remaining
variable instead of eliminating n-1 variables via implicit projection.
In order to make answers as concise as possible, redundant constraints are removed by the system
as well. In the following set of inequalities, half of them are redundant.
%
% from file: library('clpqr/examples/eliminat')
%
example(2, [X0,X1,X2,X3,X4]) :{
+87*X0 +52*X1 +27*X2 -54*X3 +56*X4 =<
+33*X0 -10*X1 +61*X2 -28*X3 -29*X4 =<
-68*X0
+8*X1 +35*X2 +68*X3 +35*X4 =<
+90*X0 +60*X1 -76*X2 -53*X3 +24*X4 =<
-95*X0 -10*X1 +64*X2 +76*X3 -24*X4 =<
+43*X0 -22*X1 +67*X2 -68*X3 -92*X4 =<
+39*X0
+7*X1 +62*X2 +54*X3 -26*X4 =<
+48*X0 -13*X1
+7*X2 -61*X3 -59*X4 =<
+49*X0 -23*X1 -31*X2 -76*X3 +27*X4 =<
-50*X0 +58*X1
-1*X2 +57*X3 +20*X4 =<
-93,
63,
-85,
-68,
33,
-97,
-27,
-2,
3,
6,
262
SICStus Prolog
}.
-13*X0
+20*X0
-81*X0
-43*X0
+16*X0
+2*X0
-65*X0
+93*X0
-63*X1
+67*X1
-44*X1
-9*X1
+83*X1
+40*X1
-11*X1
-73*X1
+81*X2
-23*X2
+19*X2
+14*X2
+89*X2
+65*X2
+10*X2
+91*X2
-3*X3
-41*X3
-22*X3
+27*X3
+25*X3
+59*X3
-13*X3
-1*X3
+70*X4
-66*X4
-73*X4
+40*X4
+55*X4
-32*X4
+91*X4
+23*X4
=<
=<
=<
=<
=<
=<
=<
=<
64,
52,
-17,
39,
36,
13,
49,
-87
Consequently, the answer consists of the system of nine non-redundant inequalities only:
clp(q) ?- use_module(library('clpqr/examples/elimination')).
clp(q) ?- example(2, [X0,X1,X2,X3,X4]).
{X0-2/17*X1-35/68*X2-X3-35/68*X4>=5/4},
{X0-73/93*X1+91/93*X2-1/93*X3+23/93*X4=<-29/31},
{X0-29/25*X1+1/50*X2-57/50*X3-2/5*X4>=-3/25},
{X0+7/39*X1+62/39*X2+18/13*X3-2/3*X4=<-9/13},
{X0+2/19*X1-64/95*X2-4/5*X3+24/95*X4>=-33/95},
{X0+2/3*X1-38/45*X2-53/90*X3+4/15*X4=<-34/45},
{X0-23/49*X1-31/49*X2-76/49*X3+27/49*X4=<3/49},
{X0+44/81*X1-19/81*X2+22/81*X3+73/81*X4>=17/81},
{X0+9/43*X1-14/43*X2-27/43*X3-40/43*X4>=-39/43}
The projection (the shadow) of this polyhedral set into the X0,X1 space can be computed via the
implicit elimination of non-query variables:
clp(q) ?- example(2, [X0,X1|_]).
{X0+2619277/17854273*X1>=-851123/17854273},
{X0+6429953/16575801*X1=<-12749681/16575801},
{X0+19130/1213083*X1>=795400/404361},
{X0-1251619/3956679*X1>=21101146/3956679},
{X0+601502/4257189*X1>=220850/473021}
Projection is quite a powerful concept that leads to surprisingly terse executable specications of
nontrivial problems like the computation of the convex hull from a set of points in an n-dimensional
space: Given the program
%
% from file: library('clpqr/examples/elimination')
%
conv_hull(Points, Xs) :lin_comb(Points, Lambdas, Zero, Xs),
zero(Zero),
polytope(Lambdas).
polytope(Xs) :-
Chapter 29: Constraint Logic Programming over Rationals or Reals
263
positive_sum(Xs, 1).
positive_sum([], Z) :- {Z=0}.
positive_sum([X|Xs], SumX) :{ X >= 0, SumX = X+Sum },
positive_sum(Xs, Sum).
zero([]).
zero([Z|Zs]) :- {Z=0}, zero(Zs).
lin_comb([],
[],
S1, S1).
lin_comb([Ps|Rest], [K|Ks], S1, S3) :lin_comb_r(Ps, K, S1, S2),
lin_comb(Rest, Ks, S2, S3).
lin_comb_r([],
_, [],
[]).
lin_comb_r([P|Ps], K, [S|Ss], [Kps|Ss1]) :{ Kps = K*P+S },
lin_comb_r(Ps, K, Ss, Ss1).
we can post the following query:
clp(q) ?- conv_hull([ [1,1], [2,0], [3,0], [1,2], [2,2] ], [X,Y]).
{Y=<2},
{X+1/2*Y=<3},
{X>=1},
{Y>=0},
{X+Y>=2}
This answer is easily veried graphically:
|
2 *
*
|
|
1 *
|
|
0 -----|----*----*---1
2
3
The convex hull program directly corresponds to the mathematical denition of the convex hull.
What does the trick in operational terms is the implicit elimination of the Lambdas from the
program formulation. Please note that this program does not limit the number of points or the
dimension of the space they are from. Please note further that quantier elimination is a computationally expensive operation and therefore this program is only useful as a benchmark for the
projector and not so for the intended purpose.
264
SICStus Prolog
29.6 Why Disequations
A beautiful example of disequations at work is due to [Colmerauer 90]. It addresses the task of
tiling a rectangle with squares of all-dierent, a priori unknown sizes. Here is a translation of the
original Prolog-III program to clp(Q,R):
%
% from file: library('clpqr/examples/squares')
%
filled_rectangle(A, C) :{ A >= 1 },
distinct_squares(C),
filled_zone([-1,A,1], _, C, []).
distinct_squares([]).
distinct_squares([B|C]) :{ B > 0 },
outof(C, B),
distinct_squares(C).
outof([],
_).
outof([B1|C], B) :{ B =\= B1 },
outof(C, B).
% *** note disequation ***
filled_zone([V|L], [W|L], C0, C0) :{ V=W,V >= 0 }.
filled_zone([V|L], L3, [B|C], C2) :{ V < 0 },
placed_square(B, L, L1),
filled_zone(L1, L2, C, C1),
{ Vb=V+B },
filled_zone([Vb,B|L2], L3, C1, C2).
placed_square(B, [H,H0,H1|L], L1) :{ B > H, H0=0, H2=H+H1 },
placed_square(B, [H2|L], L1).
placed_square(B, [B,V|L], [X|L]) :{ X=V-B }.
placed_square(B, [H|L], [X,Y|L]) :{ B < H, X= -B, Y=H-B }.
There are no tilings with less than nine squares except the trivial one where the rectangle equals
the only square. There are eight solutions for nine squares. Six further solutions are rotations of
the rst two.
Chapter 29: Constraint Logic Programming over Rationals or Reals
265
clp(q) ?- use_module(library('clpqr/examples/squares')).
clp(q) ?- filled_rectangle(A, Squares).
A = 1,
Squares = [1] ? ;
A = 33/32,
Squares = [15/32,9/16,1/4,7/32,1/8,7/16,1/32,5/16,9/32] ? ;
A = 69/61,
Squares = [33/61,36/61,28/61,5/61,2/61,9/61,25/61,7/61,16/61]
Depending on your hardware, the above query may take a few minutes. Supplying the knowledge
about the minimal number of squares beforehand cuts the computation time by a factor of roughly
four:
clp(q) ?- length(Squares, 9), filled_rectangle(A, Squares).
A = 33/32,
Squares = [15/32,9/16,1/4,7/32,1/8,7/16,1/32,5/16,9/32] ? ;
A = 69/61,
Squares = [33/61,36/61,28/61,5/61,2/61,9/61,25/61,7/61,16/61]
29.7 Syntactic Sugar
There is a package that transforms programs and queries from a eval-quote variant of clp(Q,R) into
corresponding programs and queries in a quote-eval variant. Before you use it, you need to know
that in an eval-quote language, all symbols are interpreted unless explicitly quoted. This means
that interpreted terms cannot be manipulated syntactically directly. Meta-programming in a CLP
context by denition manipulates interpreted terms, therefore you need quote/1 (just as in LISP)
and some means to put syntactical terms back to their interpreted life: {}/1.
In a quote-eval language, meta-programming is (pragmatically) simpler because everything is implicitly quoted until explicitly evaluated. On the other hand, now object programming suers from
the dual inconvenience.
We chose to make our version of clp(Q,R) of the quote-eval type because this matches the intended
use of the already existing boolean solver of SICStus. In order to keep the users of the eval-quote
variant happy, we provide a source transformation package. It is activated via:
| ?- use_module(library('clpqr/expand')).
Loading the package puts you in a mode where the arithmetic functors like +/2, */2 and all numbers
(functors of arity 0) are interpreted semantically.
266
SICStus Prolog
clp(r) ?- 2+2=X.
X = 4.0
The package works by purifying programs and queries in the sense that all references to interpreted
terms are made explicit. The above query is expanded prior to evaluation into:
{2.0+2.0=X}
The same mechanism applies when interpreted terms are nested deeper:
some_predicate(10, f(A+B/2), 2*cos(A))
Expands into:
{Xc=2.0*cos(A)},
{Xb=A+B/2},
{Xa=10.0},
some_predicate(Xa, f(Xb), Xc)
This process also applies when les are consulted or compiled. In fact, this is the only situation
where expansion can be applied with relative safety. To see this, consider what happens when the
top level evaluates the expansion, namely some calls to the clp(Q,R) solver, followed by the call
of the puried query. As we learned in Section 29.2.3 [Feedback], page 253, the solver may bind
variables, which produces a goal with interpreted functors in it (numbers), which leads to another
stage of expansion, and so on.
We recommend that you only turn on expansion temporarily while consulting or compiling les
needing expansion with expand/0 and noexpand/0.
29.7.1 Monash Examples
This collection of examples has been distributed with the Monash University Version of clp(R)
[Heintze et al. 87], and its inclusion into this distribution was kindly permitted by Roland Yap.
In order to execute the examples, a small compatibility package has to be loaded rst:
clp(r) ?- use_module(library('clpqr/monash')).
Then, assuming you are using clp(R):
clp(r) ?- expand, [library('clpqr/examples/monash/rkf45')],
noexpand.
clp(r) ?- go.
Point
0.00000 :
Point
0.50000 :
Point
1.00000 :
Point
1.50000 :
0.75000
0.61969
0.29417
-0.10556
0.00000
0.47793
0.81233
0.95809
Chapter 29: Constraint Logic Programming over Rationals or Reals
Point
Point
Point
2.00000 :
2.50000 :
3.00000 :
-0.49076
-0.81440
-1.05440
267
0.93977
0.79929
0.57522
Iteration finished
-----------------439 derivative evaluations
29.7.1.1 Compatibility Notes
The Monash examples have been written for clp(R). Nevertheless, all but rkf45 complete nicely in
clp(Q). With rkf45, clp(Q) runs out of memory. This is an instance of the problem discussed in
Section 29.4 [Numerical Precision], page 255.
The Monash University clp(R) interpreter features a dump/n predicate. It is used to print the
target variables according to the given ordering. Within this version of clp(Q,R), the corresponding
functionality is provided via ordering/1. The dierence is that ordering/1 does only specify the
ordering of the variables and no printing is performed. We think Prolog has enough predicates to
perform output already. You can still run the examples referring to dump/n from the Prolog top
level:
clp(r) ?- expand, [library('clpqr/examples/monash/mortgage')], noexpand.
% go2
%
clp(r) ?- mg(P,120,0.01,0,MP), dump([P,MP]).
{P=69.7005220313972*MP}
% go3
%
clp(r) ?- mg(P,120,0.01,B,MP), dump([P,B,MP]).
{P=0.30299477968602706*B+69.7005220313972*MP}
% go4
%
clp(r) ?- mg(999, 3, Int, 0, 400), dump.
clpr:{_B-_B*Int+_A+400.0=0.0},
clpr:{_A-_A*Int+400.0=0.0},
{_B=599.0+999.0*Int}
268
SICStus Prolog
29.8 A Mixed Integer Linear Optimization Example
The predicates bb_inf/3, bb_inf/5 implement a simple Branch and Bound search algorithm for
Mixed Integer Linear (MIP) Optimization examples. Serious MIP is not trivial. The implementation library('clpqr/bb.pl') is to be understood as a starting point for more ambitious users
who need control over branching, or who want to add cutting planes, for example.
Anyway, here is a small problem from miplib, a collection of MIP models, housed at Rice University:
NAME:
ROWS:
COLUMNS:
INTEGER:
NONZERO:
BEST SOLN:
LP SOLN:
SOURCE:
APPLICATION:
COMMENTS:
flugpl
18
18
11
46
1201500 (opt)
1167185.73
Harvey M. Wagner
John W. Gregory (Cray Research)
E. Andrew Boyd (Rice University)
airline model
no integer variables are binary
%
% from file: library('clpqr/examples/mip')
%
example(flugpl, Obj, Vs, Ints, []) :Vs = [ Anm1,Anm2,Anm3,Anm4,Anm5,Anm6,
Stm1,Stm2,Stm3,Stm4,Stm5,Stm6,
UE1,UE2,UE3,UE4,UE5,UE6],
Ints = [Stm6, Stm5, Stm4, Stm3, Stm2,
Anm6, Anm5, Anm4, Anm3, Anm2, Anm1],
Obj =
+
+
+
+
+
2700*Stm1
2700*Stm2
2700*Stm3
2700*Stm4
2700*Stm5
2700*Stm6
+
+
+
+
+
+
1500*Anm1
1500*Anm2
1500*Anm3
1500*Anm4
1500*Anm5
1500*Anm6
+
+
+
+
+
+
30*UE1
30*UE2
30*UE3
30*UE4
30*UE5
30*UE6,
allpos(Vs),
{ Stm1 = 60, 0.9*Stm1 +1*Anm1 -1*Stm2 = 0,
0.9*Stm2 +1*Anm2 -1*Stm3 = 0, 0.9*Stm3 +1*Anm3 -1*Stm4 = 0,
0.9*Stm4 +1*Anm4 -1*Stm5 = 0, 0.9*Stm5 +1*Anm5 -1*Stm6 = 0,
150*Stm1 -100*Anm1 +1*UE1 >= 8000,
150*Stm2 -100*Anm2 +1*UE2 >= 9000,
150*Stm3 -100*Anm3 +1*UE3 >= 8000,
150*Stm4 -100*Anm4 +1*UE4 >= 10000,
150*Stm5 -100*Anm5 +1*UE5 >= 9000,
150*Stm6 -100*Anm6 +1*UE6 >= 12000,
Chapter 29: Constraint Logic Programming over Rationals or Reals
269
-20*Stm1 +1*UE1 =< 0, -20*Stm2 +1*UE2 =< 0, -20*Stm3 +1*UE3 =< 0,
-20*Stm4 +1*UE4 =< 0, -20*Stm5 +1*UE5 =< 0, -20*Stm6 +1*UE6 =< 0,
Anm1 =< 18, 57 =< Stm2, Stm2 =< 75, Anm2 =< 18,
57 =< Stm3, Stm3 =< 75, Anm3 =< 18, 57 =< Stm4,
Stm4 =< 75, Anm4 =< 18, 57 =< Stm5, Stm5 =< 75,
Anm5 =< 18, 57 =< Stm6, Stm6 =< 75, Anm6 =< 18
}.
allpos([]).
allpos([X|Xs]) :- {X >= 0}, allpos(Xs).
We can rst check whether the relaxed problem has indeed the quoted inmum:
clp(r) ?- example(flugpl, Obj, _, _, _), inf(Obj, Inf).
Inf = 1167185.7255923203
Computing the inmum under the additional constraints that Stm6, Stm5, Stm4, Stm3, Stm2, Anm6,
Anm5, Anm4, Anm3, Anm2, Anm1 assume integer values at the inmum is computationally harder, but
the query does not change much:
clp(r) ?- example(flugpl, Obj, _, Ints, _),
bb_inf(Ints, Obj, Inf, Vertex, 0.001).
Inf = 1201500.0000000005,
Vertex = [75.0,70.0,70.0,60.0,60.0,0.0,12.0,7.0,16.0,6.0,6.0]
29.9 Implementation Architecture
The system consists roughly of the following components:
A polynomial normal form expression simplication mechanism.
A solver for linear equations [Holzbaur 92].
A simplex algorithm to decide linear inequalities [Holzbaur 94].
29.9.1 Fragments and Bits
29.9.1.1 Rationals
The internal data structure for rational numbers is rat(Num,Den). Den is always positive, i.e. the
sign of the rational number is the sign of Num. Further, Num and Den are relative prime. Note
that integer N looks like rat(N,1) in this representation. You can control printing of terms with
portray/1.
270
SICStus Prolog
29.9.1.2 Partial Evaluation, Compilation
Once one has a working solver, it is obvious and attractive to run the constraints in a clause
denition at read time or compile time and proceed with the answer constraints in place of the
original constraints. This gets you constant folding and in fact the full algebraic power of the solver
applied to the avoidance of computations at runtime. The mechanism to realize this idea is to use
dump/3, call_residue/2 for the expansion of {}/1, via hook predicate user:goal_expansion/3).
29.9.1.3 Asserting with Constraints
If you use the dynamic data base, the clauses you assert might have constraints on the variables
occurring in the clause. This works as expected:
clp(r) ?- {A < 10}, assert(p(A)).
{A<10.0}
yes
clp(r) ?- p(X).
{X<10.0}
29.9.2 Bugs
The fuzzy comparison of oats is the source for all sorts of weirdness. If a result in R surprises
you, try to run the program in Q before you send me a bug report.
The projector for oundered nonlinear relations keeps too many variables. Its output is rather
unreadable.
Disequations are not projected properly.
This list is probably incomplete.
Please send bug reports to <[email protected]>.
Chapter 30: Constraint Logic Programming over Finite Domains
271
30 Constraint Logic Programming over Finite Domains
30.1 Introduction
The clp(FD) solver described in this chapter is an instance of the general Constraint Logic Programming scheme introduced in [Jaar & Michaylov 87]. This constraint domain is particularly
useful for modeling discrete optimization and verication problems such as scheduling, planning,
packing, timetabling etc. The treatise [Van Hentenryck 89] is an excellent exposition of the theoretical and practical framework behind constraint solving in nite domains, and summarizes the
work up to 1989.
This solver has the following highlights:
Two classes of constraints are handled internally: primitive constraints and global constraints.
The constraints described in this chapter are automatically translated to conjunctions of primitive and global library constraints.
The truth value of a primitive constraint can be reected into a 0/1-variable (reication).
New primitive constraints can be added by writing so-called indexicals.
New global constraints can be written in Prolog, by means of a programming interface.
The rest of this chapter is organized as follows: How to load the solver and how to write simple programs is explained in Section 30.2 [CLPFD Interface], page 272. A description of all constraints that
the solver provides is contained in Section 30.3 [Available Constraints], page 275. The predicates
for searching for solution are documented in Section 30.4 [Enumeration Predicates], page 280. The
predicates for getting execution statistics are documented in Section 30.5 [Statistics Predicates],
page 282. A few example programs are given in Section 30.10 [Example Programs], page 298.
Finally, Section 30.11 [Syntax Summary], page 301 contains syntax rules for all expressions.
The following sections discuss advanced features and are probably only relevant to experienced
users: How to control the amount of information presented in answers to queries is explained in
Section 30.6 [Answer Constraints], page 283. The solver's execution mechanism and primitives are
described in Section 30.7 [The Constraint System], page 283. How to add new global constraints via
a programming interface is described in Section 30.8 [Dening Global Constraints], page 285. How
to dene new primitive constraints with indexicals is described in Section 30.9 [Dening Primitive
Constraints], page 290.
Referencing this Software
When referring to this implementation of clp(FD) in publications, please use the following reference:
Carlsson M., Ottosson G., Carlson B. \An Open-Ended Finite Domain Constraint
Solver" Proc. Programming Languages: Implementations, Logics, and Programs, 1997.
272
SICStus Prolog
Acknowledgments
The rst version of this solver was written as part of Key Hyckenberg's MSc thesis in 1995. The
code was later rewritten by Mats Carlsson, with contributions from Greger Ottosson at the Computing Science Department, Uppsala University. Peter Szeredi contributed material for this manual
chapter.
The development of this software was supported by the Swedish National Board for Technical and
Industrial Development (NUTEK) under the auspices of Advanced Software Technology (ASTEC)
Center of Competence at Uppsala University.
We include a collection of examples, some of which have been distributed with the INRIA implementation of clp(FD) [Diaz & Codognet 93].
30.2 Solver Interface
The solver is available as a library module and can be loaded with a query
:- use_module(library(clpfd)).
The solver contains predicates for checking the consistency and entailment of nite domain constraints, as well as solving for solution values for your problem variables.
In the context of this constraint solver, a nite domain is a subset of the integers, and a nite domain
constraint denotes a relation over one or more nite domains. All domain variables, i.e. variables
that occur as arguments to nite domain constraints get associated with a nite domain, either
explicitly declared by the program, or implicitly imposed by the constraint solver. Temporarily,
the domain of a variable may actually be innite, if it does not have a nite lower or upper bound.
The domain of all variables gets narrower and narrower as more constraints are added. If a domain
becomes empty, the accumulated constraints are unsatisable, and the current computation branch
fails. At the end of a successful computation, all domains have usually become singletons, i.e. the
domain variables have become assigned.
The domains do not become singletons automatically. Usually, it takes some amount of search to
nd an assignment that satises all constraints. It is the programmer's responsibility to do so. If
some domain variables are left unassigned in a computation, the garbage collector will preserve all
constraint data that is attached to them. Furthermore, the behavior of the predicates assert/1,
findall/3, copy_term/2 and friends is undened on non-ground terms containing domain variables.
The heart of the constraint solver is a scheduler for indexicals [Van Hentenryck et al. 92] and global
constraints. Both entities act as coroutines performing incremental constraint solving or entailment
checking. They wake up by changes in the domains of its arguments. All constraints provided by
this package are implemented as indexicals or global constraints. New constraints can be dened
by the user.
Chapter 30: Constraint Logic Programming over Finite Domains
273
Indexicals are reactive functional rules that take part in the solver's basic constraint solving algorithm, whereas each global constraint is associated with its particular constraint solving algorithm.
The solver maintains two scheduling queues, giving priority to the queue of indexicals.
The feasibility of integrating the indexical approach with a Prolog based on the WAM was clearly
demonstrated by Diaz's clp(FD) implementation [Diaz & Codognet 93], one of the fastest nite
domains solvers around.
30.2.1 Posting Constraints
A constraint is called as any other Prolog predicate. When called, the constraint is posted to the
store. For example:
| ?- X in 1..5, Y in 2..8, X+Y #= T.
X in 1..5,
Y in 2..8,
T in 3..13 ?
yes
| ?- X in 1..5, T in 3..13, X+Y #= T.
X in 1..5,
T in 3..13,
Y in -2..12 ?
yes
Note that the answer constraint shows the domains of nonground query variables, but not any
constraints that may be attached to them.
30.2.2 A Constraint Satisfaction Problem
Constraint satisfaction problems (CSPs) are a major class of problems for which this solver is
ideally suited. In a CSP, the goal is to pick values from pre-dened domains for certain variables
so that the given constraints on the variables are all satised.
As a simple CSP example, let us consider the Send More Money puzzle. In this problem, the
variables are the letters S, E, N, D, M, O, R, and Y. Each letter represents a digit between 0 and
9. The problem is to assign a value to each digit, such that SEND + MORE equals MONEY.
A program which solves the puzzle is given below. The program contains the typical three steps of
a clp(FD) program:
1. declare the domains of the variables
2. post the problem constraints
274
SICStus Prolog
3. look for a feasible solution via backtrack search, or look for an optimal solution via branchand-bound search
Sometimes, an extra step precedes the search for a solution: the posting of surrogate constraints to
break symmetries or to otherwise help prune the search space. No surrogate constraints are used
in this example.
The domains of this puzzle are stated via the domain/3 goal and by requiring that S and M be
greater than zero. The two problem constraint of this puzzle are the equation (sum/8) and the
constraint that all letters take distinct values (all_different/1). Finally, the backtrack search is
performed by labeling/2. Dierent search strategies can be encoded in the Type parameter. In
the example query, the default search strategy is used (select the leftmost variable, try values in
ascending order).
:- use_module(library(clpfd)).
mm([S,E,N,D,M,O,R,Y], Type) :domain([S,E,N,D,M,O,R,Y], 0, 9),
S#>0, M#>0,
all_different([S,E,N,D,M,O,R,Y]),
sum(S,E,N,D,M,O,R,Y),
labeling(Type, [S,E,N,D,M,O,R,Y]).
sum(S, E, N, D, M, O, R,
1000*S
+
1000*M
#= 10000*M + 1000*O
% step 1
% step 2
% step 3
Y) :+ 100*E + 10*N + D
+ 100*O + 10*R + E
+ 100*N + 10*E + Y.
| ?- mm([S,E,N,D,M,O,R,Y], []).
D
E
M
N
O
R
S
Y
=
=
=
=
=
=
=
=
7,
5,
1,
6,
0,
8,
9,
2 ?
30.2.3 Reied Constraints
Instead of merely posting constraints it is often useful to reect its truth value into a 0/1-variable
B, so that:
the constraint is posted if B is set to 1
the negation of the constraint is posted if B is set to 0
B is set to 1 if the constraint becomes entailed
Chapter 30: Constraint Logic Programming over Finite Domains
275
B is set to 0 if the constraint becomes disentailed
This mechanism is known as reication. Several frequently used operations can be dened in terms
of reied constraints, such as blocking implication [Saraswat 90] and the cardinality operator [Van
Hentenryck & Deville 91], to name a few. A reied constraint is written:
| ?- Constraint #<=> B.
where Constraint is reiable. As an example of a constraint that uses reication, consider
exactly(X,L,N) which is true if X occurs exactly N times in the list L. It can be dened thus:
exactly(_, [], 0).
exactly(X, [Y|L], N) :X #= Y #<=> B,
N #= M+B,
exactly(X, L, M).
30.3 Available Constraints
This section describes the classes of constraints that can be used with this solver.
30.3.1 Arithmetic Constraints
?Expr RelOp ?Expr
denes an arithmetic constraint. The syntax for Expr and RelOp is dened by a
grammar (see [Syntax of Arithmetic Expressions], page 303). Note that the expressions
are not restricted to being linear. Constraints over non-linear expressions, however, will
usually yield less constraint propagation than constraints over linear expressions.
Arithmetic constraints can be reied as e.g.
| ?- X in 1..2, Y in 3..5, X#=<Y #<=> B.
B = 1,
X in 1..2,
Y in 3..5 ?
Linear arithmetic constraints maintain (at least) interval-consistency and their reied versions
detect (at least) interval-entailment and -disentailment; see Section 30.7 [The Constraint System],
page 283.
The following constraints are among the library constraints that general arithmetic constraints
compile to. They express a relation between a sum or a scalar product and a value, using a
dedicated algorithm that avoids creating any temporary variables holding intermediate values. If
you are computing a sum or a scalar product, it can be much more ecient to compute lists of
coecients and variables and post a single sum or scalar product constraint than to post a sequence
of elementary constraints.
276
SICStus Prolog
sum(+Xs, +RelOp,
?Value )
where Xs is a list of integers or domain variables, RelOp is a relational symbol as
above, and Value is an integer or a domain variable. True if Xs RelOp Value. Cannot
be reied.
scalar_product(+Coes, +Xs, +RelOp, ?Value )
where Coes is a list of length n of integers, Xs is a list of length n of integers or domain
variables, RelOp is a relational symbol as above, and Value is an integer or a domain
variable. True if Coes*Xs RelOp Value. Cannot be reied.
30.3.2 Membership Constraints
domain(+Variables, +Min, +Max )
where Variables is a list of domain variables or integers, Min is an integer or the atom
inf (minus innity), and Max is an integer or the atom sup (plus innity). True if the
variables all are elements of the range Min..Max. Cannot be reied.
?X in +Range
denes a membership constraint. X is an integer or a domain variable and Range is a
ConstantRange (see [Syntax of Indexicals], page 301). True if X is an element of the
range.
?X in_set +FDSet
denes a membership constraint. X is an integer or a domain variable and FDSet is
an FD set term (see Section 30.8.3 [FD Set Operations], page 287). True if X is an
element of the FD set.
and in_set/2 constraints can be reied. They maintain domain-consistency and their reied
versions detect domain-entailment and -disentailment; see Section 30.7 [The Constraint System],
page 283.
in/2
30.3.3 Propositional Constraints
Propositional combinators can be used to combine reiable constraints into propositional formulae
over such constraints. Such formulae are goal expanded by the system into sequences of reied
constraints and arithmetic constraints. For example,
X #= 4 #\/ Y #= 6
expresses the disjunction of two equality constraints.
The leaves of propositional formulae can be reiable constraints, the constants 0 and 1, or 0/1variables. New primitive, reiable constraints can be dened with indexicals as described in Section 30.9 [Dening Primitive Constraints], page 290. The following propositional combinators are
available:
#\
:Q
True if the constraint Q is false.
Chapter 30: Constraint Logic Programming over Finite Domains
:P #/\ :Q
:P #\ :Q
:P #\/ :Q
:P #=> :Q
:Q #<= :P
:P #<=> :Q
277
True if the constraints P and Q are both true.
True if exactly one of the constraints P and Q is true.
True if at least one of the constraints P and Q is true.
True if the constraint Q is true or the constraint P is false.
True if the constraints P and Q are both true or both false.
Note that the reication scheme introduced in Section 30.2.3 [Reied Constraints], page 274 is a
special case of a propositional constraint.
30.3.4 Combinatorial Constraints
The constraints listed here are sometimes called symbolic constraints. They are currently not
reiable.
count(+Val,+List,+RelOp,?Count)
where Val is an integer, List is a list of integers or domain variables, Count an integer
or a domain variable, and RelOp is a relational symbol as in Section 30.3.1 [Arithmetic
Constraints], page 275. True if N is the number of elements of List that are equal
to Val and N RelOp Count. Thus, count/4 is a generalization of exactly/3 (not an
exported constraint) that was used in an example earlier.
count/4 maintains domain-consistency; see Section 30.7 [The Constraint System],
page 283.
element(?X,+List,?Y )
where X and Y are integers or domain variables and List is a list of integers or domain
variables. True if the X:th element of List is Y. Operationally, the domains of X and
Y are constrained so that for every element in the domain of X, there is a compatible
element in the domain of Y, and vice versa.
This constraint uses an optimized algorithm for the special case where List is ground.
element/3 maintains domain-consistency; see Section 30.7 [The Constraint System],
page 283.
relation(?X,+MapList,?Y )
where X and Y are integers or domain variables and MapList is a list of integer ConstantRange pairs, where the integer keys occur uniquely (see [Syntax of Indexicals],
page 301). True if MapList contains a pair X -R and Y is in the range denoted by R.
Operationally, the domains of X and Y are constrained so that for every element in
the domain of X, there is a compatible element in the domain of Y, and vice versa.
If MapList is not ground, the constraint must be wrapped in call/1 to postpone goal
expansion until runtime.
An arbitrary binary constraint can be dened with relation/3. relation/3 maintains
domain-consistency; see Section 30.7 [The Constraint System], page 283.
278
SICStus Prolog
all_different(+Variables )
where Variables is a list of domain variables or integers. Each variable is constrained
to take a value that is unique among the variables. Operationally, this is equivalent to
posting an inequality constraint for each pair of variables.
This constraint wakes up each time a variable becomes ground, pruning the domains of
the other variables. The implementation is incomplete, i.e. domain elements for Variables for which the constraint has no solutions are not always eliminated. A complete
version is provided by the all_distinct/1 constraint (see below).
The following constraint is a complete version of the all_different/1 constraint. The algorithm
is due to Regin [Regin 94].
all_distinct(+Variables )
where Variables is a list of domain variables or integers. Each variable is constrained to
take a value that is unique among the variables. The implementation is complete, i.e.
it maintains domain-consistency; see Section 30.7 [The Constraint System], page 283.
The following is a constraint over two lists of length n of variables. Each variable is constrained to
take a value in 1,...,n that is unique for its list. Furthermore, the lists are dual in a sense described
below.
assignment(+Xs, +Ys )
where Xs and Ys are lists of domain variables or integers, both of length n. True if all
Xi, Yi in 1,...,n and Xi=j i Yj=i.
The following constraint can be thought of as constraining n nodes in a graph to form a Hamiltonian
circuit. The nodes are numbered from 1 to n. The circuit starts in node 1, visits each node, and
returns to the origin.
circuit(+Succ )
circuit(+Succ, +Pred )
where Succ is a list of length n of domain variables or integers. The i:th element of
Succ (Pred) is the successor (predecessor) of i in the graph. True if the values form a
Hamiltonian circuit.
The following four constraints can be thought of as constraining n tasks, each with a start time Sj
and a duration Dj, so that no tasks ever overlap. The tasks can be seen as competing for some
exclusive resource.
serialized(+Starts,+Durations )
where Starts = [S1,...,Sn] and Durations = [D1,...,Dn] are lists of domain variables
with nite bounds or integers. True if:
Si +Di =< Sj OR Sj+Dj =< Si, for all 1 =< i <j =< n
The serialized/2 constraint is merely a special case of cumulative/4 (see below).
Chapter 30: Constraint Logic Programming over Finite Domains
279
serialized_resource(+Starts,+Durations,-Resource )
expresses the same constraint, returning in Resource a term which can be passed to
order_resource/2 (see Section 30.4 [Enumeration Predicates], page 280) in order to
nd a consistent ordering of the tasks.
serialized_precedence(+Starts,+Durations,+Precedences )
serialized_precedence_resource(+Starts,+Durations,+Precedences,-Resource )
express the same constraint, combined with some extra precedence constraint as encoded in Precedences which should be a list of terms of the form d(i,j,d ). Here, i and
j should be task numbers, and d should be a positive integer or sup. Each term adds
the constraint:
Si +d =< Sj OR Sj =< Si, if d is an integer
Sj =< Si, if d is sup
serialized_precedence/3 can model a set of tasks to be serialized with sequencedependent setup times. For example, the following constraint models three tasks, all
with duration 5, where task 1 must precede task 2 and task 3 must either complete
before task 2 or start at least 10 time units after task 2 started:
?- domain([S1,S2,S3], 0, 20),
serialized_precedence([S1,S2,S3], [5,5,5], [d(2,1,sup),d(2,3,10)]).
S1 in 0..15,
S2 in 5..20,
S3 in 0..20 ?
The bounds of S1 and S2 changed because of the precedence constraint. Setting S2 to
5 will propagate S1=0 and S3 in 15..20.
The following constraint can be thought of as constraining n tasks, each with a start time Sj, a
duration Dj, and a resource amount Rj, so that the total resource consumption does not exceed
Limit at any time:
cumulative(+Starts,+Durations,+Resources,?Limit)
where Starts = [S1,...,Sn], Durations = [D1,...,Dn], Resource = [R1,...,Rn] are lists of
domain variables with nite bounds or integers, and Limit is a domain variable with
nite bounds or an integer. Let
a = min(S1,...,Sn),
b = max(S1 +D1,...,Sn+Dn)
Rij = Rj, if Sj =< i < Sj+Dj
Rij = 0 otherwise
The constraint holds if:
Ri1 +...+Rin =< Limit, for all a =< i < b
The cumulative/4 constraint is due to Aggoun and Beldiceanu [Aggoun & Beldiceanu
92]. The current implementation is incomplete and takes O(n^2) space.
280
SICStus Prolog
30.3.5 User-Dened Constraints
New, primitive constraints can be added dened by the user on two dierent levels. On a higher
level, constraints can be dened using the global constraint programming interface; see Section 30.8
[Dening Global Constraints], page 285. Such constraints can embody specialized algorithms and
use the full power of Prolog. They cannot be reied.
On a lower level, new primitive constraints can be dened with indexicals. In this case, they take
part in the basic constraint solving algorithm and express custom designed rules for special cases
of the overall local propagation scheme. Such constraints are called FD predicates; see Section 30.9
[Dening Primitive Constraints], page 290. They can optionally be reied.
30.4 Enumeration Predicates
As is usually the case with nite domain constraint solvers, this solver is not complete. That
is, it does not ensure that the set of posted constraints is satisable. One must resort to search
(enumeration) to check satisability and get particular solutions.
The following predicates provide several variants of search:
indomain(?X )
where X is a domain variable with nite bounds or an integer. Assigns, in increasing
order via backtracking, a feasible value to X.
minimize(:Goal,?X )
maximize(:Goal,?X )
Uses a branch-and-bound algorithm with restart to nd an assignment that minimizes
(maximizes) the domain variable X. Goal should be a Prolog goal that constrains X to
become assigned, and could be a labeling/2 goal. The algorithm calls Goal repeatedly
with a progressively tighter upper (lower) bound on X until a proof of optimality is
obtained, at which time Goal and X are unied with values corresponding to the
optimal solution.
labeling(+Options, +Variables )
where Variables is a list of domain variables or integers and Options is a list of search
options. True if an assignment of the variables can be found which satises the posted
constraints.
The search options control the order in which variables are selected for assignment (variable choice
heuristic), the way in which choices are made for the selected variable (value choice heuristic), and
whether all solutions or a single, optimal solution should be found. The search options are divided
into four groups. One option may be selected per group. Finally, the number of assumptions
(choices) made during the search can be collected.
The following options control the order in which the next variable is selected for assignment. None of these methods will select a variable with an unbounded domain.
Thus labeling/2 may succeed with such variables still unassigned.
Chapter 30: Constraint Logic Programming over Finite Domains
281
The leftmost variable is selected. This is the default.
min
The leftmost variable with the smallest lower bound is selected.
max
The leftmost variable with the greatest upper bound is selected.
ff
The rst-fail principle is used: the leftmost variable with the smallest domain is selected.
ffc
The most constrained heuristic is used: a variable with the smallest domain
is selected, breaking ties by (a) selecting the variable that has the most
constraints suspended on it and (b) selecting the leftmost one.
The following options control the way in which choices are made for the selected variable
X:
step
Makes a binary choice between X #= B and X #\= B, where B is the lower
or upper bound of X. This is the default.
enum
Makes a multiple choice for X corresponding to the values in its domain.
bisect
Makes a binary choice between X #=< M and X #> M, where M is the
midpoint of the domain of X. This strategy is also known as domain splitting.
The following options control the order in which the choices are made for the selected
variable X:
up
The domain is explored in ascending order. This is the default.
down
The domain is explored in descending order.
The following options control whether all solutions should be enumerated by backtracking or whether a single solution that minimizes (maximizes) X is returned, if one
exists.
all
All solutions are enumerated. This is the default.
minimize(X )
maximize(X )
Uses a branch-and-bound algorithm to nd an assignment that minimizes
(maximizes) the domain variable X. The labeling should constrain X to
become assigned for all assignments of Variables.
Finally, the following option counts the number of assumptions (choices) made during
the search:
statistics(K )
When a solution is found, K is unied with the number of choices made.
leftmost
For example, to enumerate solutions using a static variable ordering, use:
| ?- constraints(Variables),
labeling([], Variables).
%same as [leftmost,step,up,all]
To minimize a cost function using branch-and-bound search, a dynamic variable ordering using the
rst-fail principle, and domain splitting exploring the upper part of domains rst, use:
282
SICStus Prolog
| ?- constraints(Variables, Cost),
labeling([ff,bisect,down,minimize(Cost)], Variables).
As opposed to the predicates above which search for consistent assignments to domain variables,
the following predicate searches for a consistent ordering among tasks competing for an exclusive
resource, without necessarily xing their start times:
order_resource(+Options, +Resource )
where Options is a list of search options and Resource represents a resource as returned
by serialized_resource/3 (see Section 30.3.4 [Combinatorial Constraints], page 277)
on which tasks must be serialized. True if a total ordering can be imposed on the tasks,
enumerating all such orderings via backtracking.
The search options control the construction of the total ordering. It may contain at
most one of the following atoms, selecting a strategy:
first
The ordering is built by repetitively selecting some task to be placed before
all others.
last
The ordering is built by repetitively selecting some task to be placed after
all others.
and at most one of the following atoms, controlling which task to select at each step.
If first is chosen (the default), the task with the smallest value is selected, otherwise
the task with the greatest value is selected.
est
The tasks are ordered by earliest start time.
lst
The tasks are ordered by latest start time.
ect
The tasks are ordered by earliest completion time.
lct
The tasks are ordered by latest completion time.
[first,est] (the default) and [last,lct] can be good heuristics.
30.5 Statistics Predicates
The following predicates can be used to get execution statistics.
fd_statistics(?Key,
?Value )
This allows a program to access execution statistics specic to this solver. General
statistics about CPU time and memory consumption etc. is available from the built-in
predicate statistics/2.
For each of the possible keys Key, Value is unied with the current value of a counter
which is simultaneously zeroed. The following counters are maintained. See Section 30.7 [The Constraint System], page 283, for details of what they all mean:
resumptions
The number of times a constraint was resumed.
Chapter 30: Constraint Logic Programming over Finite Domains
283
entailments
prunings
backtracks
The number of times a (dis)entailment was detected by a constraint.
The number of times a domain was pruned.
The number of times a contradiction was found by a domain being wiped
out, or by a global constraint signalling failure. Other causes of backtracking, such as failed Prolog tests, are not covered by this counter.
constraints
The number of constraints created.
fd_statistics
Displays on the user_error stream a summary of the above statistics. All counters
are zeroed.
30.6 Answer Constraints
By default, the answer constraint only shows the projection of the store onto the variables that
occur in the query, but not any constraints that may be attached to these variables, nor any domains
or constraints attached to other variables. This is a conscious decision, as no ecient algorithm for
projecting answer constraints onto the query variables is known for this constraint system.
It is possible, however, to get a complete answer constraint including all variables that took part
in the computation and their domains and attached constraints. This is done by asserting a clause
for the following predicate:
clpfd:full_answer
A dynamic hook predicate. If false (the default), the answer constraint only contains
the domains of the query variables. If true, the answer constraints contains the domains
and any attached constraints of all variables.
30.7 The Constraint System
30.7.1 Denitions
The constraint system is based on domain constraints and indexicals. A domain constraint is an
expression X ::I, where X is a domain variable and I is a nonempty set of integers.
A set S of domain constraints is called a store. D(X,S), the domain of X in S, is dened as the
intersection of all I such that X ::I belongs to S. The store is contradictory if the domain of some
variable is empty; otherwise, it is consistent. A consistent store S' is an extension of a store S i,
for all variables X, D(X,S') is contained in D(X,S).
The following denitions, adapted from [Van Hentenryck et al. 95], dene important notions of
consistency and entailment of constraints wrt. stores.
284
SICStus Prolog
A ground constraint is true if it holds and false otherwise.
A constraint C is domain-consistent wrt. S i, for each variable Xi and value Vi in D(Xi,S), there
exist values Vj in D(Xj,S), 1 =< j =< n, i\=j, such that C(V1,...,Vn) is true.
A constraint C is domain-entailed by S i, for all values Vj in D(Xj,S), 1 =< j =< n, C(V1,...,Vn)
is true.
Let D'(X,S) denote the interval min(D(X,S))..max(D(X,S)).
A constraint C is interval-consistent wrt. S i, for each variable Xi there exist values Vj in D'(Xj,S),
1 =< j =< n, i\=j, such that C(V1,...,min(D(Xi,S)),...,Vn) and C(V1,...,max(D(Xi,S)),...,Vn) are
both true.
A constraint C is interval-entailed by S i, for all values Vj in D'(Xj,S), 1 =< j =< n, C(V1,...,Vn)
is true.
Finally, a constraint is domain-disentailed (interval-disentailed) by S i its negation is domainentailed (interval-entailed) by S.
30.7.2 Pitfalls of Interval Reasoning
In most circumstances, arithmetic constraints only maintain interval-consistency and only detect
interval-entailment and -disentailment. Note that there are cases where an interval-consistency
maintaining constraint may detect a contradiction when the constraint is not yet intervaldisentailed, as the following example illustrates. Note that X #\= Y maintains domain consistency
if both arguments are constants or variables:
| ?- X+Y #= Z, X=1, Z=6, Y in 1..10, Y #\= 5.
no
| ?- X+Y #= Z #<=> B, X=1, Z=6, Y in 1..10, Y #\= 5.
X
Z
Y
B
= 1,
= 6,
in(1..4)\/(6..10),
in 0..1
Since 1+5#=6 holds, X+Y #= Z is not interval-disentailed, although any attempt to make it intervalconsistent wrt. the store results in a contradictory store.
Chapter 30: Constraint Logic Programming over Finite Domains
285
30.8 Dening Global Constraints
30.8.1 The Global Constraint Programming Interface
This section describes a programming interface by means of which new constraints can be written.
The interface consists of a set of predicates provided by this library module. Constraints dened in
this way can take arbitrary arguments and may use any constraint solving algorithm, provided it
makes sense. Reication cannot be expressed in this interface; instead, reication may be achieved
by explicitly passing a 0/1-variable to the constraint in question.
Global constraints have state which may be updated each time the constraint is resumed. The
state information may be used e.g. in incremental constraint solving.
The following two predicates are the principal entrypoints for dening and posting new global
constraints:
clpfd:dispatch_global(+Constraint, +State0,
-State, -Actions )
A multile hook predicate, telling the solver how to solve constraints of the form
Constraint.
When dening a new constraint, a clause of this predicate must be added. Its body
denes a constraint solving method and should always succeed deterministically. When
a global constraint is called or resumed, the solver will call this predicate to deal with
the constraint.
State0 and State are the old and new state respectively.
The constraint solving method must not invoke the constraint solver recursively e.g. by
binding variables or posting new constraints; instead, Actions should be unied with a
list of requests to the solver. Each request should be of the following form:
exit
The constraint has become entailed, and ceases to exist.
fail
The constraint has become disentailed, causing the solver to backtrack.
X =V
The solver binds X to V.
X in R The solver constrains X to be a member of the ConstantRange R (see
[Syntax of Indexicals], page 301).
X in_set S
The solver constrains X to be a member of the FD set S (see Section 30.8.3
[FD Set Operations], page 287).
call(Goal )
The solver calls the goal or constraint Goal, which should be module prexed unless it is a built-in predicate or an exported predicate of the clpfd
module.
286
SICStus Prolog
fd_global(+Constraint, +State, +Susp )
where Constraint is a constraint goal, State is its initial state, and Susp is a term
encoding how the constraint should wake up in response to domain changes. This
posts the constraint.
Susp is a list of F(Var) terms where Var is a variable to suspend on and F is a functor
encoding when to wake up:
dom(X )
wake up when the domain of X has changed
min(X )
wake up when the lower bound of X has changed
max(X )
wake up when the upper bound of X has changed
minmax(X )
wake up when the lower or upper of X has changed
val(X )
wake up when X has become ground
For an example of usage, see Section 30.8.4 [A Global Constraint Example], page 288.
30.8.2 Reection Predicates
The constraint solving method needs access to information about the current domains of variables.
This is provided by the following predicates, which are all constant time operations.
fd_min(?X,
?Min)
where X is a domain variable (or an integer). Min is unied with the smallest value in
the current domain of X, i.e. an integer or the atom inf denoting minus innity.
fd_max(?X, ?Max )
where X is a domain variable (or an integer). Max is unied with the upper bound of
the current domain of X, i.e. an integer or the atom sup denoting innity.
fd_size(?X, ?Size )
where X is a domain variable (or an integer). Size is unied with the size of the current
domain of X, if the domain is nite, or the atom sup otherwise.
fd_set(?X, ?Set)
where X is a domain variable (or an integer). Set is unied with an FD set term
denoting the internal representation of the current domain of X; see below.
fd_dom(?X, ?Range )
where X is a domain variable (or an integer). Range is unied with a ConstantRange
(see [Syntax of Indexicals], page 301) denoting the the current domain of X.
The following predicate can be used for computing the set of variables that are transitively connected
via constraints to some given variables.
fd_closure(+Vars,
-Closure )
Given a list Vars of domain variables, Closure is the set of variables (including Vars)
that can be transitively reached via constraints posted so far.
Chapter 30: Constraint Logic Programming over Finite Domains
287
30.8.3 FD Set Operations
The domains of variables are internally represented compactly as FD set terms. The details of
this representation are subject to change and should not be relied on. Therefore, a number of
operations on FD sets are provided, as such terms play an important role in the interface. The
following operations are the primitive ones:
is_fdset(+Set)
Set is a valid FD set.
empty_fdset(?Set)
Set is the empty FD set.
fdset_parts(?Set, ?Min, ?Max, ?Rest)
Set is an FD set which is a union of the non-empty interval Min..Max and the FD set
Rest, and all elements of Rest are greater than Max +1. Min and Max are both integers
or the atoms inf and sup, denoting minus and plus innity, respectively. Either Set
or all the other arguments must be ground.
The following operations can all be dened in terms of the primitive ones, but in most cases, a
more ecient implementation is used:
empty_interval(+Min, +Max )
Min..Max is an empty interval.
fdset_interval(?Set, ?Min, ?Max )
Set is an fdset which is the non-empty interval Min..Max.
fdset_singleton(?Set, ?Elt)
Set is an FD set containing Elt only. At least one of the arguments must be ground.
fdset_min(+Set, -Min)
Min is the lower bound of Set.
fdset_max(+Set, -Min)
Max is the upper bound of Set. This operation is linear in the number of intervals of
Set.
fdset_size(+Set, -Size )
Size is the cardinality of Set, represented as sup if Set is innite. This operation is
linear in the number of intervals of Set.
list_to_fdset(+List, -Set)
Set is the FD set containing the elements of List. Slightly more ecient if List is
ordered.
fdset_to_list(+Set, -List)
List is an ordered list of the elements of Set, which must be nite.
range_to_fdset(+Range, -Set)
Set is the FD set containing the elements of the ConstantRange (see [Syntax of Indexicals], page 301) Range.
288
SICStus Prolog
fdset_to_range(+Set,
-Range )
Range is a constant interval, a singleton constant set, or a union of such, denoting the
same set as Set.
fdset_add_element(+Set1, +Elt -Set2 )
Set2 is Set1 with Elt inserted in it.
fdset_del_element(+Set1, +Elt, -Set2 )
Set2 is like Set1 but with Elt removed.
fdset_disjoint(+Set1, +Set2 )
The two FD sets have no elements in common.
fdset_intersect(+Set1, +Set2 )
The two FD sets have at least one element in common.
fdset_intersection(+Set1, +Set2, -Intersection)
Intersection is the intersection between Set1 and Set2.
fdset_intersection(+Sets, -Intersection)
Intersection is the intersection of all the sets in Sets.
fdset_member(?Elt, +Set)
is true when Elt is a member of Set. If Elt is unbound, Set must be nite.
fdset_eq(+Set1, +Set2 )
Is true when the two arguments represent the same set i.e. they are identical.
fdset_subset(+Set1, +Set2 )
Every element of Set1 appears in Set2.
fdset_subtract(+Set1, +Set2, -Dierence )
Dierence contains all and only the elements of Set1 which are not also in Set2.
fdset_union(+Set1, +Set2, -Union)
Union is the union of Set1 and Set2.
fdset_union(+Sets, -Union)
Union is the union of all the sets in Sets.
fdset_complement(+Set, -Complement)
Complement is the complement of Set wrt. inf..sup.
30.8.4 A Global Constraint Example
The following example denes a new global constraint exactly(X,L,N ) which is true if X occurs
exactly N times in the list L of integers and domain variables. A version dened in terms of reied
equalities was presented earlier; see Section 30.2.3 [Reied Constraints], page 274.
This example illustrates the use of state information. The state has two components: the list of
variables that could still be X, and the number of variables still required to be X.
The constraint is dened to wake up on any domain change.
Chapter 30: Constraint Logic Programming over Finite Domains
289
/*
An implementation of exactly(I, X[1]...X[m], N):
Necessary condition: 0 =< N =< m.
Rewrite rules:
[1] |= X[i]=I ==> exactly(I, X[1]...X[i-1],X[i+1]...X[m], N-1):
[2] |= X[i]\=I ==> exactly(I, X[1]...X[i-1],X[i+1]...X[m], N):
[3] |= N=0
==> X[1]\=I ... X[m]\=I
[4] |= N=m
==> X[1]=I ... X[m]=I
*/
:- use_module(library(clpfd)).
% the entrypoint
exactly(I, Xs, N) :dom_suspensions(Xs, Susp),
fd_global(exactly(I,Xs,N), state(Xs,N), Susp).
dom_suspensions([], []).
dom_suspensions([X|Xs], [dom(X)|Susp]) :dom_suspensions(Xs, Susp).
% the solver method
:- multifile clpfd:dispatch_global/4.
clpfd:dispatch_global(exactly(I,_,_), state(Xs0,N0), state(Xs,N), Actions) :exactly_solver(I, Xs0, Xs, N0, N, Actions).
exactly_solver(I, Xs0, Xs, N0, N, Actions) :ex_filter(Xs0, Xs, N0, N, I),
length(Xs, M),
(
N=:=0 -> Actions = [exit|Ps], ex_neq(Xs, I, Ps)
;
N=:=M -> Actions = [exit|Ps], ex_eq(Xs, I, Ps)
;
N>0, N<M -> Actions = []
;
Actions = [fail]
).
% rules [1,2]: filter the X's, decrementing N
ex_filter([], [], N, N, _).
ex_filter([X|Xs], Ys, L, N, I) :- X==I, !,
M is L-1,
ex_filter(Xs, Ys, M, N, I).
ex_filter([X|Xs], Ys0, L, N, I) :fd_set(X, Set),
fdset_member(I, Set), !,
Ys0 = [X|Ys],
ex_filter(Xs, Ys, L, N, I).
ex_filter([_|Xs], Ys, L, N, I) :ex_filter(Xs, Ys, L, N, I).
290
SICStus Prolog
% rule [3]: all must be neq I
ex_neq(Xs, I, Ps) :fdset_singleton(Set0, I),
fdset_complement(Set0, Set),
eq_all(Xs, Set, Ps).
% rule [4]: all must be eq I
ex_eq(Xs, I, Ps) :fdset_singleton(Set, I),
eq_all(Xs, Set, Ps).
eq_all([], _, []).
eq_all([X|Xs], Set, [X in_set Set|Ps]) :eq_all(Xs, Set, Ps).
end_of_file.
% sample queries:
| ?- exactly(5,[A,B,C],1), A=5.
A = 5,
B in(inf..4)\/(6..sup),
C in(inf..4)\/(6..sup)
| ?- exactly(5,[A,B,C],1), A in 1..2, B in 3..4.
C = 5,
A in 1..2,
B in 3..4
30.9 Dening Primitive Constraints
Indexicals are the principal means of dening constraints, but it is usually not necessary to resort
to this level of programming|most commonly used constraints are available in a library and/or via
macro-expansion. The key feature about indexicals is that they give the programmer precise control
over aspects of the operational semantics of the constraints. Trade-os can be made between the
computational cost of the constraints and their pruning power. The indexical language provides
many degrees of freedom for the user to select the level of consistency to be maintained depending
on application-specic needs.
Chapter 30: Constraint Logic Programming over Finite Domains
291
30.9.1 Indexicals
An indexical is a reactive functional rule of the form X in R, where R is a set valued range
expression (see below). See [Syntax of Indexicals], page 301, for a grammar dening indexicals
and range expressions.
Indexicals can play one of two roles: propagating indexicals are used for constraint solving, and
checking indexicals are used for entailment checking. When a propagating indexical res, R is
evaluated in the current store S, which is extended by adding the new domain constraint X ::S(R)
to the store, where S(R) denotes the value of R in S. When a checking indexical res, it checks if
D(X,S) is contained in S(R), and if so, the constraint corresponding to the indexical is detected as
entailed.
30.9.2 Range Expressions
A range expression has one of the following forms, where Ri denote range expressions, Ti denote
integer valued term expressions, S(Ti) denotes the integer value of Ti in S, X denotes a variable, I
denotes an integer, and S denotes the current store.
dom(X )
evaluates to D(X,S)
{T1,...,Tn}
evaluates to {S(T1 ),...,S(Tn)}. Any term expression containing a subexpression
which is a variable that is not \quantied" by unionof/3 will only be evaluated when
this variable has been assigned.
T1..T2 evaluates to the interval between S(T1) and S(T2).
R1 /\R2 evaluates to the intersection of S(R1) and S(R2)
R1 \/R2 evaluates to the union of S(R1) and S(R2)
\R2
evaluates to the complement of S(R2)
R1 +R2
R1 +T2
evaluates to S(R2) or S(T2) added pointwise to S(R1)
-R2
evaluates to S(R2) negated pointwise
R1-R2
R1-T2
T1-R2
evaluates to S(R2) or S(T2) subtracted pointwise from S(R1) or S(T1)
R1 mod R2
R1 mod T2
evaluates to S(R1) pointwise modulo S(R2) or S(T2)
R1 ? R2 evaluates to S(R2) if S(R1) is a non-empty set; otherwise, evaluates to the empty set.
This expression is commonly used in the context (R1 ? (inf..sup) \/ R3 ), which
evaluates to S(R3) if S(R1) is an empty set; otherwise, evaluates to inf..sup. As an
optimization, R3 is not evaluated while the value of R1 is a non-empty set.
292
SICStus Prolog
unionof(X,R1,R2 )
evaluates to the union of S(Expr 1)...S(Expr N), where each Expr I has been formed by
substituting K for X in R2, where K is the I:th element of S(R1). See Section 30.10.2
[N Queens], page 298, for an example of usage. N.B. If S(R1) is innite, the evaluation
of the indexical will be abandoned, and the indexical will simply suspend.
switch(T1,MapList)
evaluates to S(Expr) if S(T1) equals Key and MapList contains a pair Key -Expr.
Otherwise, evaluates to the empty set.
When used in the body of an FD predicate (see Section 30.9.8 [Goal Expanded Constraints],
page 297), a relation/3 expression expands to two indexicals, each consisting of a switch/2
expression nested inside a unionof/3 expression. Thus, the following constraints are equivalent:
p(X, Y) +: relation(X, [1-{1},2-{1,2},3-{1,2,3}], Y).
q(X, Y) +:
X in unionof(B,dom(Y),switch(B,[1-{1,2,3},2-{2,3},3-{3}])),
Y in unionof(B,dom(X),switch(B,[1-{1},2-{1,2},3-{1,2,3}])).
30.9.3 Term Expressions
A term expression has one of the following forms, where T1 and T2 denote term expressions, X
denotes a variable, I denotes an integer, and S denotes the current store.
min(X )
max(X )
card(X )
X
I
inf
sup
-T1
T1 +T2
T1-T2
T1 *T2
T1 />T2
T1 /<T2
T1 mod T2
evaluates to the minimum of D(X,S)
evaluates to the maximum of D(X,S)
evaluates to the size of D(X,S)
evaluates to the integer value of X. A subexpression of this form, not \quantied" by
unionof/3, will cause the evaluation to suspend until the variable is assigned.
an integer
minus innity
plus innity
evaluates to S(T1) negated
evaluates to the sum of S(T1) and S(T2)
evaluates to the dierence of S(T1) and S(T2)
evaluates to the product of S(T1) and S(T2), where S(T2) must not be negative
evaluates to the quotient of S(T1) and S(T2), rounded up, where S(T2) must be positive
evaluates to the quotient of S(T1) and S(T2), rounded down, where S(T2) must be
positive
evaluates to the modulo of S(T1) and S(T2)
Chapter 30: Constraint Logic Programming over Finite Domains
293
30.9.4 Monotonicity of Indexicals
A range R is monotone in S i the value of R in S' is contained in the value of R in S, for every
extension S' of S. A range R is anti-monotone in S i the value of R in S is contained in the
value of R in S', for every extension S' of S. By abuse of notation, we will say that X in R is
(anti-)monotone i R is (anti-)monotone.
The consistency or entailment of a constraint C expressed as indexicals X in R in a store S
is checked by considering the relationship between D(X,S) and S(R), together with the (anti)monotonicity of R in S. The details are given in Section 30.9.6 [Execution of Propagating Indexicals], page 296 and Section 30.9.7 [Execution of Checking Indexicals], page 296.
The solver checks (anti-)monotonicity by requiring that certain variables occurring in the indexical
be ground. This sucient condition can sometimes be false for an (anti-)monotone indexical, but
such situations are rare in practice.
30.9.5 FD predicates
The following example denes the constraint X+Y=T as an FD predicate in terms of three indexicals. Each indexical is a rule responsible for removing values detected as incompatible from one
particular constraint argument. Indexicals are not Prolog goals; thus, the example does not express
a conjunction. However, an indexical may make the store contradictory, in which case backtracking
is triggered:
plus(X,Y,T) +:
X in min(T) - max(Y) .. max(T) - min(Y),
Y in min(T) - max(X) .. max(T) - min(X),
T in min(X) + min(Y) .. max(X) + max(Y).
The above denition contains a single clause used for constraint solving. The rst indexical wakes
up whenever the bounds of S(T) or S(Y) are updated, and removes from D(X,S) any values that
are not compatible with the new bounds of T and Y. Note that in the event of \holes" in the
domains of T or Y, D(X,S) may contain some values that are incompatible with X+Y=T but
go undetected. Like most built-in arithmetic constraints, the above denition maintains intervalconsistency, which is signicantly cheaper to maintain than domain-consistency and suces in most
cases. The constraint could for example be used as follows:
| ?- X in 1..5, Y in 2..8, plus(X,Y,T).
X in 1..5,
Y in 2..8,
T in 3..13 ?
yes
Thus, when an FD predicate is called, the `+:' clause is activated.
294
SICStus Prolog
The denition of a user constraint has to specify what domain constraints should be added to
the constraint store when the constraint is posted. Therefore the FD predicate contains a set of
indexicals, each representing a domain constraint to be added to the constraint store. The actual
domain constraint depends on the constraint store itself. For example, the third indexical in the
above FD predicate prescribes the domain constraint `T :: 3..13' if the store contains `X :: 1..5,
Y :: 2..8'. As the domain of some variables gets narrower, the indexical may enforce a new, stricter
constraint on some other variables. Therefore such an indexical (called a propagating indexical)
can be viewed as an agent reacting to the changes in the store by enforcing further changes in the
store.
In general there are three stages in the lifetime of a propagating indexical. When it is posted it
may not be evaluated immediately (e.g. has to wait until some variables are ground before being
able to modify the store). Until the preconditions for the evaluation are satised, the agent does
not enforce any constraints. When the indexical becomes evaluable the resulting domain constraint
is added to the store. The agent then waits and reacts to changes in the domains of variables
occurring in the indexical by re-evaluating it and adding the new, stricter constraint to the store.
Eventually the computation reaches a phase when no further renement of the store can result in
a more precise constraint (the indexical is entailed by the store), and then the agent can cease to
exist.
A necessary condition for the FD predicate to be correctly dened is the following: for any store
mapping each variable to a singleton domain the execution of the indexicals should succeed without
contradiction exactly when the predicate is intended to be true.
There can be several alternative denitions for the same user constraint with dierent strengths in
propagation. For example, the denition of plusd below encodes the same X+Y=T constraint as the
plus predicate above, but maintaining domain consistency:
plusd(X,Y,T)
X in
Y in
T in
+:
dom(T) - dom(Y),
dom(T) - dom(X),
dom(X) + dom(Y).
| ?- X in {1}\/{3}, Y in {10}\/{20}, plusd(X, Y, T).
X in{1}\/{3},
Y in{10}\/{20},
T in{11}\/{13}\/{21}\/{23} ?
yes
This costs more in terms of execution time, but gives more precise results. For singleton domains
plus and plusd behave in the same way.
In our design, general indexicals can only appear in the context of FD predicate denitions. The
rationale for this restriction is the need for general indexicals to be able to suspend and resume,
and this ability is only provided by the FD predicate mechanism.
Chapter 30: Constraint Logic Programming over Finite Domains
295
If the program merely posts a constraint, it suces for the denition to contain a single clause for
solving the constraint. If a constraint is reied or occurs in a propositional formula, the denition
must contain four clauses for solving and checking entailment of the constraint and its negation.
The role of each clause is reected in the \neck" operator. The following table summarizes the
dierent forms of indexical clauses corresponding to a constraint C. In all cases, Head should be a
compound term with all arguments being distinct variables:
Head +: Indexicals.
The clause consists of propagating indexicals for solving C.
Head -: Indexicals.
The clause consists of propagating indexicals for solving the negation of C.
Head +? Indexical.
The clause consists of a single checking indexical for testing entailment of C.
Head -? Indexical.
The clause consists of a single checking indexical for testing entailment of the negation
of C.
When a constraint is reied, the solver spawns two reactive agents corresponding to detecting
entailment and disentailment. Eventually, one of them will succeed in this and consequently will
bind B to 0 or 1. A third agent is spawned, waiting for B to become assigned, at which time the
constraint (or its negation) is posted. In the mean time, the constraint may have been detected as
(dis)entailed, in which case the third agent is dismissed. The waiting is implemented by means of
the coroutining facilities of SICStus Prolog.
As an example of a constraint with all methods dened, consider the following library constraint
dening a disequation between two domain variables:
'x\\=y'(X,Y)
X in
Y in
'x\\=y'(X,Y)
X in
Y in
'x\\=y'(X,Y)
X in
'x\\=y'(X,Y)
X in
+:
\{Y},
\{X}.
-:
dom(Y),
dom(X).
+?
\dom(Y).
-?
{Y}.
The following sections provide more precise coding rules and operational details for indexicals. X
in R denotes an indexical corresponding to a constraint C. S denotes the current store.
296
SICStus Prolog
30.9.6 Execution of Propagating Indexicals
Consider the denition of a constraint C containing a propagating indexical X in R. Let TV(X,C,S)
denote the set of values for X that can make C true in some ground extension of the store S. Then
the indexical should obey the following coding rules:
all arguments of C except X should occur in R
if R is ground in S, S(R) = TV(X,C,S)
If the coding rules are observed, S(R) can be proven to contain TV(X,C,S) for all stores in which
R is monotone. Hence it is natural for the implementation to wait until R becomes monotone
before admitting the propagating indexical for execution. The execution of X in R thus involves
the following:
If D(X,S) is disjoint from S(R), a contradiction is detected.
If D(X,S) is contained in S(R), D(X,S) does not contain any values known to be incompatible
with C, and the indexical suspends, unless R is ground in S, in which case C is detected as
entailed.
Otherwise, D(X,S) contains some values that are known to be incompatible with C. Hence,
X ::S(R) is added to the store (X is pruned), and the indexical suspends, unless R is ground
in S, in which case C is detected as entailed.
A propagating indexical is scheduled for execution as follows:
it is evaluated initially as soon as it has become monotone
it is re-evaluated when one of the following conditions occurs:
1. the domain of a variable Y that occurs as dom(Y ) or card(Y ) in R has been updated
2. the lower bound of a variable Y that occurs as min(Y ) in R has been updated
3. the upper bound of a variable Y that occurs as max(Y ) in R has been updated
30.9.7 Execution of Checking Indexicals
Consider the denition of a constraint C containing a checking indexical X in R. Let FV(X,C,S)
denote the set of values for X that can make C false in some ground extension of the store S. Then
the indexical should obey the following coding rules:
all arguments of C except X should occur in R
if R is ground in S, S(R) = TV(X,C,S)
If the coding rules are observed, S(R) can be proven to exclude FV(X,C,S) for all stores in which R
is anti-monotone. Hence it is natural for the implementation to wait until R becomes anti-monotone
before admitting the checking indexical for execution. The execution of X in R thus involves the
following:
If D(X,S) is contained in S(R), none of the possible values for X can make C false, and so C
is detected as entailed.
Chapter 30: Constraint Logic Programming over Finite Domains
297
Otherwise, if D(X,S) is disjoint from S(R) and R is ground in S, all possible values for X will
make C false, and so C is detected as disentailed.
Otherwise, D(X,S) contains some values that could make C true and some that could make C
false, and the indexical suspends.
A checking indexical is scheduled for execution as follows:
it is evaluated initially as soon as it has become anti-monotone
it is re-evaluated when one of the following conditions occurs:
1. the domain of X has been pruned, or X has been assigned
2. the domain of a variable Y that occurs as dom(Y ) or card(Y ) in R has been pruned
3. the lower bound of a variable Y that occurs as min(Y ) in R has been increased
4. the upper bound of a variable Y that occurs as max(Y ) in R has been decreased
30.9.8 Goal Expanded Constraints
The arithmetic, membership, and propositional constraints described earlier are transformed at
compile time to conjunctions of goals of library constraints.
Sometimes it is necessary to postpone the expansion of a constraint until runtime, e.g. if the
arguments are not instantiated enough. This can be achieved by wrapping call/1 around the
constraint.
Although space economic (linear in the size of the source code), the expansion of a constraint to
library goals can have an overhead compared to expressing the constraint in terms of indexicals.
Temporary variables holding intermediate values may have to be introduced, and the grain size of
the constraint solver invocations can be rather small. The translation of constraints to library goals
has been greatly improved in the current version, so these problems have virtually disappeared.
However, for backward compatibility, an implementation by compilation to indexicals of the same
constraints is also provided. An FD predicate may be dened by a single clause:
Head +: Constraint.
where Constraint is an arithmetic constraint or an element/3 or a relation/3 constraint. This
translation is only available for `+:' clauses; thus, Head cannot be reied.
In the case of arithmetic constraints, the constraint must be over linear terms (see [Syntax of
Indexicals], page 301). The memory consumption of the FD predicate will be quadratic in the
size of the source code. The alternative version of sum/8 in Section 30.10.1 [Send More Money],
page 298 illustrates this technique.
In the case of element(X,L,Y) or relation(X,L,Y), the memory consumption of the FD predicate
will be linear in the size of the source code. The execution time of the initial evaluation of the FD
predicate will be linear in the size of the initial domains for X and Y ; if these domains are innite,
no propagation will take place.
298
SICStus Prolog
30.10 Example Programs
This section contains a few example programs. The rst two programs are included in a benchmark
suite that comes with the distribution. The benchmark suite is run by typing:
| ?- compile(library('clpfd/examples/bench')).
| ?- bench.
30.10.1 Send More Money
Let us return briey to the Send More Money problem (see Section 30.2.2 [A Constraint Satisfaction
Problem], page 273). Its sum/8 predicate will expand to a space-ecient conjunction of library
constraints. A faster but more memory consuming version is dened simply by changing the neck
symbol of sum/8 from `:-' to `+:', thus turning it into an FD predicate:
sum(S, E, N, D, M, O, R,
1000*S
+
1000*M
#= 10000*M + 1000*O
Y) +:
+ 100*E + 10*N + D
+ 100*O + 10*R + E
+ 100*N + 10*E + Y.
30.10.2 N Queens
The problem is to place N queens on an NxN chess board so that no queen is threatened by another
queen.
The variables of this problem are the N queens. Each queen has a designated row. The problem is
to select a column for it.
The main constraint of this problem is that no queen threaten another. This is encoded by the
no_threat/3 constraint and holds between all pairs (X,Y) of queens. It could be dened as
no_threat(X, Y,
X
#\=
X+I #\=
X-I #\=
I) :Y,
Y,
Y.
However, this formulation introduces new temporary domain variables and creates twelve negrained indexicals. Worse, the arithmetic constraints are only guaranteed to maintain intervalconsistency and so may miss some opportunities for pruning elements in the middle of domains.
A better idea is to formulate no_threat/3 as an FD predicate with two indexicals, as shown in
the program below. This constraint will not re until one of the queens has been assigned (the
corresponding indexical does not become monotone until then). Hence, the constraint is still not
as strong as it could be.
For example, if the domain of one queen is (2..3), then it will threaten any queen placed in column
2 or 3 on an adjacent row, no matter which of the two open positions is chosen for the rst queen.
Chapter 30: Constraint Logic Programming over Finite Domains
299
The commented out formulation of the constraint captures this reasoning, and illustrates the use of
the unionof/3 operator. This stronger version of the constraint indeed gives less backtracking, but
is computationally more expensive and does not pay o in terms of execution time, except possibly
for very large chess boards.
It is clear that no_threat/3 cannot detect any incompatible values for a queen with domain of size
greater than three. This observation is exploited in the third version of the constraint.
The rst-fail principle is appropriate in the enumeration part of this problem.
:- use_module(library(clpfd)).
queens(N, L, LabelingType) :length(L, N),
domain(L, 1, N),
constrain_all(L),
labeling(LabelingType, L).
constrain_all([]).
constrain_all([X|Xs]) :constrain_between(X, Xs, 1),
constrain_all(Xs).
constrain_between(_X, [], _N).
constrain_between(X, [Y|Ys], N) :no_threat(X, Y, N),
N1 is N+1,
constrain_between(X, Ys, N1).
% version 1: weak but efficient
no_threat(X, Y, I) +:
X in \({Y} \/ {Y+I} \/ {Y-I}),
Y in \({X} \/ {X+I} \/ {X-I}).
/*
% version 2: strong but very inefficient version
no_threat(X, Y, I) +:
X in unionof(B,dom(Y),\({B} \/ {B+I} \/ {B-I})),
Y in unionof(B,dom(X),\({B} \/ {B+I} \/ {B-I})).
% version 3: strong but somewhat inefficient version
no_threat(X, Y, I) +:
X in (4..card(Y)) ? (inf..sup) \/
unionof(B,dom(Y),\({B} \/ {B+I} \/ {B-I})),
Y in (4..card(X)) ? (inf..sup) \/
unionof(B,dom(X),\({B} \/ {B+I} \/ {B-I})).
*/
300
SICStus Prolog
| ?- queens(8, L, [ff]).
L = [1,5,8,6,3,7,2,4] ?
30.10.3 Cumulative Scheduling
This example is a very small scheduling problem. We consider seven tasks where each task has a
xed duration and a xed amount of used resource:
TASK
====
t1
t2
t3
t4
t5
t6
t7
DURATION
========
16
6
13
7
5
18
4
RESOURCE
========
2
9
3
7
10
1
11
The goal is to nd a schedule that minimizes the completion time for the schedule while not
exceeding the capacity 13 of the resource. The resource constraint is succinctly captured by a
cumulative/4 constraint. Branch-and-bound search is used to nd the minimal completion time.
This example was borrowed from [Beldiceanu & Contejean 94].
:- use_module(library(clpfd)).
schedule(Ss, End) :length(Ss, 7),
Ds = [16, 6,13, 7, 5,18, 4],
Rs = [ 2, 9, 3, 7,10, 1,11],
domain(Ss, 1, 30),
domain([End], 1, 50),
after(Ss, Ds, End),
cumulative(Ss, Ds, Rs, 13),
labeling([minimize(End)], [End|Ss]).
after([], [], _).
after([S|Ss], [D|Ds], E) :- E #>= S+D, after(Ss, Ds, E).
%% End of file
| ?- schedule(Ss, End).
Ss = [1,17,10,10,5,5,1],
End = 23 ?
Chapter 30: Constraint Logic Programming over Finite Domains
30.11 Syntax Summary
Syntax of Indexicals
301
302
SICStus Prolog
X
variable
-->
Constant
|
|
Term
|
|
|
|
|
|
|
|
|
|
|
-->
inf
sup
-->
X
Range
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
domain variable }
integer
Constant
min(X )
max(X )
card(X )
- Term
Term + Term
Term - Term
Term * Term
Term /> Term
Term /< Term
Term mod Term
TermSet
{
minus innity }
plus innity }
{
{
{
{
{
{
suspend until assigned }
min. of domain of X }
max. of domain of X }
size of domain of X }
{
{
division rounded up }
division rounded down }
--> {Term,...,Term}
--> TermSet
dom(X )
Term..Term
Range /\Range
Range \/Range
\Range
- Range
Range + Range
Range - Range
Range mod Range
Range + Term
Range - Term
Term - Range
Range mod Term
Range ? Range
unionof(X,Range,Range )
switch(Term,MapList)
ConstantSet
--> {integer,...,integer }
ConstantRange --> ConstantSet
|
Constant..Constant
|
ConstantRange /\ConstantRange
|
ConstantRange \/ConstantRange
|
\ConstantRange
MapList
|
CList
|
--> []
[integer-ConstantRange |MapList]
--> []
[integer |CList]
{ domain of X }
{ interval }
{ intersection }
{ union }
{ complement }
{ pointwise negation }
{ pointwise addition }
{ pointwise subtraction }
{ pointwise modulo }
{ pointwise addition }
{ pointwise subtraction }
{ pointwise subtraction }
{ pointwise modulo }
Chapter 30: Constraint Logic Programming over Finite Domains
Indexical
-->
X
in
Range
Indexicals --> Indexical
|
Indexical, Indexicals
ConstraintBody --> Indexicals
| LinExpr RelOp LinExpr
| element(X,CList,X )
| relation(X,MapList,X )
Head
-->
term
{
a compound term with unique variable args }
TellPos --> Head +: ConstraintBody.
TellNeg --> Head -: ConstraintBody.
AskPos --> Head +? Indexical.
AskNeg --> Head -? Indexical.
ConstraintDef -->
TellPos. [TellNeg.] [AskPos.] [AskNeg.]
Syntax of Arithmetic Expressions
X
-->
variable
N
-->
integer
LinExpr --> N
|
X
|
N *X
|
N *N
|
LinExpr + LinExpr
|
LinExpr - LinExpr
Expr
|
|
|
|
|
|
|
|
LinExpr
Expr + Expr
Expr - Expr
Expr * Expr
Expr / Expr
Expr mod Expr
min(Expr,Expr )
max(Expr,Expr )
abs(Expr )
{
domain variable }
{
linear expression }
-->
RelOp
{
integer division }
--> #= | #\= | #< | #=< | #> | #>=
303
304
SICStus Prolog
Operator Declarations
::::::::::::::-
op(1200,
op(760,
op(750,
op(750,
op(740,
op(730,
op(720,
op(710,
op(700,
op(700,
op(550,
op(500,
op(490,
op(400,
xfx,
yfx,
xfy,
yfx,
yfx,
yfx,
yfx,
fy,
xfx,
xfx,
xfx,
fy,
yfx,
yfx,
[+:,-:,+?,-?]).
#<=>).
#=>).
#<=).
#\/).
#\).
#/\).
#\).
[in,in_set]).
[#=,#\=,#<,#=<,#>,#>=]).
..).
\).
?).
[/>,/<]).
Chapter 31: Constraint Handling Rules
305
31 Constraint Handling Rules
Copyright
c 1996-98 LMU
This chapter is Copyright LMU (Ludwig-Maximilians-University)
Munich, Germany
Permission is granted to make and distribute verbatim copies of this chapter provided the copyright
notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modied versions of this chapter under the conditions
for verbatim copying, provided that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this chapter into another language,
under the above conditions for modied versions, except that this permission notice may be stated
in a translation approved by LMU.
31.1 Introduction
Experience from real-life applications using constraint-based programming has shown that typically,
one is confronted with a heterogeneous mix of dierent types of constraints. To be able to express
constraints as they appear in the application and to write and combine constraint systems, a
special purpose language for writing constraint systems called constraint handling rules (CHR)
was developed. CHR have been used to encode a wide range of constraint handlers (solvers),
including new domains such as terminological and temporal reasoning. Several CHR libraries
exist in declarative languages such as Prolog and LISP, worldwide more than 20 projects use
CHR. You can nd more information about CHR at URL: http://www.pst.informatik.unimuenchen.de/personen/fruehwir/chr-intro.html
The high-level CHR are an excellent tool for rapid prototyping and implementation of constraint
handlers. The usual abstract formalism to describe a constraint system, i.e. inference rules, rewrite
rules, sequents, formulas expressing axioms and theorems, can be written as CHR in a straightforward way. Starting from this executable specication, the rules can be rened and adapted to the
specics of the application.
The CHR library includes a compiler, which translates CHR programs into Prolog programs on
the y, and a runtime system, which includes a stepper for debugging. Many constraint handlers
are provided in the example directory of the library.
CHR are essentially a committed-choice language consisting of guarded rules that rewrite constraints into simpler ones until they are solved. CHR dene both simplication of and propagation
306
SICStus Prolog
over constraints. Simplication replaces constraints by simpler constraints while preserving logical equivalence (e.g. X>Y,Y>X <=> fail). Propagation adds new constraints which are logically
redundant but may cause further simplication (e.g. X>Y,Y>Z ==> X>Z). Repeatedly applying CHR
incrementally simplies and nally solves constraints (e.g. A>B,B>C,C>A leads to fail.
With multiple heads and propagation rules, CHR provide two features which are essential for nontrivial constraint handling. The declarative reading of CHR as formulas of rst order logic allows
one to reason about their correctness. On the other hand, regarding CHR as a rewrite system on
logical formulas allows one to reason about their termination and conuence.
In case the implementation of CHR disagrees with your expectations based on this chapter, drop a
line to the current maintainer: [email protected] (Christian Holzbaur).
31.2 Introductory Examples
We dene a CHR constraint for less-than-or-equal, leq, that can handle variable arguments. This
handler can be found in the library as the le leq.pl. (The code works regardless of options
switched on or o.)
:- use_module(library(chr)).
handler leq.
constraints leq/2.
:- op(500, xfx, leq).
reflexivity
antisymmetry
idempotence
transitivity
@
@
@
@
X
X
X
X
leq
leq
leq
leq
Y
Y
Y
Y
<=>
, Y
\ X
, Y
X=Y
leq
leq
leq
|
X
Y
Z
true.
<=> X=Y.
<=> true.
==> X leq Z.
The CHR specify how leq simplies and propagates as a constraint. They implement reexivity,
idempotence, antisymmetry and transitivity in a straightforward way. CHR reflexivity states
that X leq Y simplies to true, provided it is the case that X=Y. This test forms the (optional)
guard of a rule, a precondition on the applicability of the rule. Hence, whenever we see a constraint
of the form A leq A we can simplify it to true.
The rule antisymmetry means that if we nd X leq Y as well as Y leq X in the constraint store, we
can replace it by the logically equivalent X=Y. Note the dierent use of X=Y in the two rules: In the
reflexivity rule the equality is a precondition (test) on the rule, while in the antisymmetry rule
it is enforced when the rule res. (The reexivity rule could also have been written as reflexivity
X leq X <=> true.)
The rules reflexivity and antisymmetry are simplication CHR. In such rules, the constraints
found are removed when the rule applies and res. The rule idempotence is a simpagation CHR,
only the constraints right of '\' will be removed. The rule says that if we nd X leq Y and another
X leq Y in the constraint store, we can remove one.
Chapter 31: Constraint Handling Rules
307
Finally, the rule transitivity states that the conjunction X leq Y, Y leq Z implies X leq Z. Operationally, we add X leq Z as (redundant) constraint, without removing the constraints X leq Y,
Y leq Z. This kind of CHR is called propagation CHR.
Propagation CHR are useful, as the query A leq B,C leq A,B leq C illustrates: The rst two constraints cause CHR transitivity to re and add C leq B to the query. This new constraint
together with B leq C matches the head of CHR antisymmetry, X leq Y, Y leq X. So the two constraints are replaced by B=C. Since B=C makes B and C equivalent, CHR antisymmetry applies to
the constraints A leq B, C leq A, resulting in A=B. The query contains no more CHR constraints,
the simplication stops. The constraint handler we built has solved A leq B, C leq A, B leq C and
produced the answer A=B, B=C:
A leq B,C leq A,B leq C.
% C leq A, A leq B propagates C leq B by transitivity.
% C leq B, B leq C simplifies to B=C by antisymmetry.
% A leq B, C leq A simplifies to A=B by antisymmetry since B=C.
A=B,B=C.
Note that multiple heads of rules are essential in solving these constraints. Also note that this
handler implements a (partial) order constraint over any constraint domain, this generality is only
possible with CHR.
As another example, we can implement the sieve of Eratosthenes to compute primes simply as (for
variations see the handler `primes.pl'):
:- use_module(library(chr)).
handler eratosthenes.
constraints primes/1,prime/1.
primes(1) <=> true.
primes(N) <=> N>1 | M is N-1,prime(N),primes(M). % generate candidates
absorb(J) @ prime(I) \ prime(J) <=> J mod I =:= 0 | true.
The constraint primes(N) generates candidates for prime numbers, prime(M), where M is between
1 and N. The candidates react with each other such that each number absorbs multiples of itself.
In the end, only prime numbers remain.
Looking at the two rules dening primes/1, note that head matching is used in CHR, so the rst
rule will only apply to primes(1). The test N>1 is a guard (precondition) on the second rule. A call
with a free variable, like primes(X), will delay (suspend). The third, multi-headed rule absorb(J)
reads as follows: If there is a constraint prime(I) and some other constraint prime(J) such that J
mod I =:= 0 holds, i.e. J is a multiple of I, then keep prime(I) but remove prime(J) and execute
the body of the rule, true.
308
SICStus Prolog
31.3 CHR Library
CHR extend the Prolog syntax by a few constructs introduced in the next sections. Technically,
the extension is achieved through the user:term_expansion/2 mechanism. A le that contains
a constraint handler may also contain arbitrary Prolog code. Constraint handling rules can be
scattered across a le. Declarations and options should precede rules. There can only be at most
one constraint handler per module.
31.3.1 Loading the Library
Before you can load or compile any le containing a constraint handler (solver) written in CHR,
the chr library module has to be imported:
| ?- use_module(library(chr)).
It is recommended to include the corresponding directive at the start of your les containing
handlers:
:- use_module(library(chr)).
31.3.2 Declarations
Declarations in les containing CHR aect the compilation and thus the behavior of the rules at
runtime.
The mandatory handler declaration precedes any other CHR specic code. Example:
handler minmax.
A handler name must be a valid Prolog
dened.
atom.
Per module, only one constraint handler can be
The constraints must be declared before they are used by rules. With this mandatory declaration
one lists the constraints the rules will later talk about. The declaration can be used more than
once per handler. Example:
constraints
leq/2, minimum/3, maximum/3.
The following optional declaration allows for conditional rule compilation. Only the rules mentioned
get compiled. Rules are referred to by their names (see Section 31.3.3 [CHR Syntax], page 309).
The latest occurrence takes precedence if used more than once per handler. Although it can be put
anywhere in the handler le, it makes sense, as with other declarations, to use it early. Example:
rules antisymmetry, transitivity.
To simplify the handling of operator declarations, in particular during fcompile/1, operator/3
declarations with the same denotation as op/3, but taking eect during compilation and loading,
are helpful. Example:
Chapter 31: Constraint Handling Rules
309
operator(700, xfx, ::).
operator(600, xfx, :).
31.3.3 Constraint Handling Rules, Syntax
A constraint handling rule has one or more heads, an optional guard, a body and an optional
name. A Head is a Constraint. A constraint is a callable Prolog term, whose functor is a declared
constraint. The Guard is a Prolog goal. The Body of a rule is a Prolog goal (including constraints).
A rule can be named with a Name which can be any Prolog term (including variables from the
rule).
There are three kinds of constraint handling rules:
Rule
--> [Name @]
(Simplification | Propagation | Simpagation)
[pragma Pragma].
Simplification --> Heads
<=> [Guard '|'] Body
Propagation
--> Heads
==> [Guard '|'] Body
Simpagation
--> Heads \ Heads <=> [Guard '|'] Body
Heads
Head
Constraint
Id
-->
-->
-->
-->
Head | Head, Heads
Constraint | Constraint # Id
a callable term declared as constraint
a unique variable
Guard
Ask
Tell
Goal
-->
-->
-->
-->
Ask | Ask & Tell
Goal
Goal
a callable term, including conjunction and disjunction etc.
Body
--> Goal
Pragma
--> a conjunction of terms usually referring to
one or more heads identified via #/2
The symbol `|' separates the guard (if present) from the body of a rule. Since `|' is read as `;'
(disjunction) by the reader, care has to be taken when using disjunction in the guard or body of
the rule. The top level disjunction will always be interpreted as guard-body separator `|', so proper
bracketing has to be used, e.g. a <=> (b;c) | (d;e) instead of a <=> b;c | d;e and a <=> true |
(d;e) instead of a <=> (d;e).
In simpagation rules, `\' separates the heads of the rule into two parts.
Individual head constraints may be tagged with variables via `#', which may be used as identiers in
pragma declarations, for example. Constraint identiers must be distinct variables, not occurring
elsewhere in the heads.
310
SICStus Prolog
Guards test the applicability of a rule. Guards come in two parts, tell and ask, separated by `&'. If
the `&' operator is not present, the whole guard is assumed to be of the ask type.
Declaratively, a rule relates heads and body provided the guard is true. A simplication rule means
that the heads are true if and only if the body is true. A propagation rule means that the body
is true if the heads are true. A simpagation rule combines a simplication and a propagation
rule. The rule Heads1 \ Heads2 <=> Body is equivalent to the simplication rule Heads1, Heads2
<=> Heads1, Body. However, the simpagation rule is more compact to write, more ecient to
execute and has better termination behavior than the corresponding simplication rule, since the
constraints comprising Heads1 will not be removed and inserted again.
31.3.4 How CHR work
Each CHR constraint is associated with all rules in whose heads it occurs by the CHR compiler.
Every time a CHR constraint is executed (called) or woken and reconsidered, it checks itself the
applicability of its associated CHR by trying each CHR. By default, the rules are tried in textual
order, i.e. in the order they occur in the dening le. To try a CHR, one of its heads is matched
against the constraint. Matching succeeds if the constraint is an instance of the head. If a CHR
has more than one head, the constraint store is searched for partner constraints that match the
other heads. Heads are tried from left to right, except that in simpagation rules, the heads to be
removed are tried before the head constraints to be kept (this is done for eciency reasons). If the
matching succeeds, the guard is executed. Otherwise the next rule is tried.
The guard either succeeds or fails. A guard succeeds if the execution of its Ask and Tell parts
succeeds and in the ask part no variable that occurs also in the heads was touched or the cause of
an instantiation error. The ask guard will fail otherwise. A variable is touched if it is unied with
a term (including other variables from other constraints) dierent from itself. Tell guards, on the
contrary, are trusted and not checked for that property. If the guard succeeds, the rule applies.
Otherwise the next rule is tried.
If the ring CHR is a simplication rule, the matched constraints are removed from the store and
the body of the CHR is executed. Similarly for a ring simpagation rule, except that the constraints
that matched the heads preceding `\' are kept. If the ring CHR is a propagation rule the body of
the CHR is executed without removing any constraints. It is remembered that the propagation rule
red, so it will not re again with the same constraints if the constraint is woken and reconsidered.
If the currently active constraint has not been removed, the next rule is tried.
If the current constraint has not been removed and all rules have been tried, it delays until a
variable occurring in the constraint is touched. Delaying means that the constraint is inserted into
the constraint store. When a constraint is woken, all its rules are tried again. (This process can be
watched and inspected with the CHR debugger, see below.)
Chapter 31: Constraint Handling Rules
311
31.3.5 Pragmas
Pragmas are annotations to rules and constraints that enable the compiler to generate more specic,
more optimized code. A pragma can be a conjunction of the following terms:
already_in_heads
The intention of simplication and simpagation rules is often to combine the heads into
a stronger version of one of them. Depending on the strength of the guard, the new
constraint may be identical to one of the heads to removed by the rule. This removal
followed by addition is inecient and may even cause termination problems. If the
pragma is used, this situation is detected and the corresponding problems are avoided.
The pragma applies to all constraints removed by the rule.
already_in_head(Id)
Shares the intention of the previous pragma, but aects only the constraint indicated
via Id. Note that one can use more than one pragma per rule.
passive(Id)
No code will be generated for the specied constraint in the particular head position.
This means that the constraint will not see the rule, it is passive in that rule. This
changes the behavior of the CHR system, because normally, a rule can be entered
starting from each head constraint. Usually this pragma will improve the eciency of
the constraint handler, but care has to be taken in order not to lose completeness.
For example, in the handler leq, any pair of constraints, say A leq B, B leq A, that
matches the head X leq Y , Y leq X of the antisymmetry rule, will also match it when
the constraints are exchanged, B leq A, A leq B. Therefore it is enough if a currently
active constraint enters this rule in the rst head only, the second head can be declared
to be passive. Similarly for the idempotence rule. For this rule, it is more ecient to
declare the rst head passive, so that the currently active constraint will be removed
when the rule res (instead of removing the older constraint and redoing all the propagation with the currently active constraint). Note that the compiler itself detects the
symmetry of the two head constraints in the simplication rule antisymmetry, thus it
is automatically declared passive and the compiler outputs CHR eliminated code for
head 2 in antisymmetry.
antisymmetry
idempotence
transitivity
X leq Y , Y leq X # Id <=> X=Y pragma passive(Id).
X leq Y # Id \ X leq Y <=> true pragma passive(Id).
X leq Y # Id , Y leq Z ==> X leq Z pragma passive(Id).
Declaring the rst head of rule transitivity passive changes the behavior of the
handler. It will propagate less depending on the order in which the constraints arrive:
?- X leq Y, Y leq Z.
X leq Y,
Y leq Z,
X leq Z ?
?- Y leq Z, X leq Y.
Y leq Z,
X leq Y ?
312
SICStus Prolog
?- Y leq Z, X leq Y, Z leq X.
Y = X,
Z = X ?
The last query shows that the handler is still complete in the sense that all circular
chains of leq-relations are collapsed into equalities.
31.3.6 Options
Options parametrise the rule compilation process. Thus they should precede the rule denitions.
Example:
option(check_guard_bindings, off).
The format below lists the names of the recognized options together with the acceptable values.
The rst entry in the lists is the default value.
option(debug_compile, [off,on]).
Instruments the generated code such that the execution of the rules may be traced (see
Section 31.4 [CHR Debugging], page 316).
option(check_guard_bindings, [on,off]).
Per default, for guards of type ask the CHR runtime system makes sure that no variables
are touched or the cause of an instantiation error. These checks may be turned o with
this option, i.e. all guards are treated as if they were of the tell variety. The option was
kept for backward compatibility. Tell and ask guards oer better granularity.
option(already_in_store, [off,on]).
If this option is on, the CHR runtime system checks for the presence of an identical
constraint upon the insertion into the store. If present, the attempted insertion has no
eect. Since checking for duplicates for all constraints costs, duplicate removal specic
to individual constraints, using a few simpagation rules of the following form instead,
may be a better solution.
Constraint \ Constraint <=> true.
option(already_in_heads, [off,on]).
The intention of simplication and simpagation rules is often to combine the heads
into a stronger version of one of them. Depending on the strength of the guard, the
new constraint may be identical to one of the heads removed by the rule. This removal
followed by addition is inecient and may even cause termination problems. If the
option is enabled, this situation is detected and the corresponding problems are avoided.
This option applies to all constraints and is provided mainly for backward compatibility.
Better grained control can be achieved with corresponding pragmas. (see Section 31.3.5
[CHR Pragmas], page 311).
The remaining options are meant for CHR implementors only:
option(flatten, [on,off]).
option(rule_ordering, [canonical,heuristic]).
Chapter 31: Constraint Handling Rules
313
option(simpagation_scheme, [single,multi]).
option(revive_scheme, [new,old]).
option(dead_code_elimination, [on,off]).
31.3.7 Built-In Predicates
This table lists the predicates made available by the CHR library. They are meant for advanced
users, who want to tailor the CHR system towards their specic needs.
current_handler(?Handler, ?Module)
Nondeterministically enumerates the dened handlers with the module they are dened
in.
current_constraint(?Handler, ?Constraint)
Nondeterministically enumerates the dened constraints in the form Functor/Arity and
the handlers they are dened in.
insert_constraint(+Constraint, -Id)
Inserts Constraint into the constraint store without executing any rules. The constraint
will be woken and reconsidered when one of the variables in Constraint is touched. Id
is unied with an internal object representing the constraint. This predicate only
gets dened when a handler and constraints are declared (see Section 31.3.2 [CHR
Declarations], page 308).
insert_constraint(+Constraint, -Id, ?Term)
Inserts Constraint into the constraint store without executing any rules. The constraint
will be woken and reconsidered when one of the variables in Term is touched. Id
is unied with an internal object representing the constraint. This predicate only
gets dened when a handler and constraints are declared (see Section 31.3.2 [CHR
Declarations], page 308).
find_constraint(?Pattern, -Id)
Nondeterministically enumerates constraints from the constraint store that match Pattern, i.e. which are instances of Pattern. Id is unied with an internal object representing the constraint.
find_constraint(-Var, ?Pattern, -Id)
Nondeterministically enumerates constraints from the constraint store that delay on
Var and match Pattern, i.e. which are instances of Pattern. The identier Id can be
used to refer to the constraint later, e.g. for removal.
findall_constraints(?Pattern, ?List)
Unies List with a list of Constraint # Id
pairs from the constraint store that match
findall_constraints(-Var, ?Pattern, ?List)
Unies List with a list of Constraint # Id
pairs from the constraint store that delay
Pattern.
on Var and match Pattern.
314
SICStus Prolog
remove_constraint(+Id)
Removes the constraint Id, obtained with one of the previous predicates, from the
constraint store.
unconstrained(?Var)
Succeeds if no CHR constraint delays on Var. Dened as:
unconstrained(X) :find_constraint(X, _, _), !, fail.
unconstrained(_).
notify_constrained(?Var)
Leads to the reconsideration of the constraints associated with Var. This mechanism
allows solvers to communicate reductions on the set of possible values of variables prior
to making bindings.
31.3.8 Consulting and Compiling Constraint Handlers
The CHR compilation process has been made as transparent as possible. The user deals with
les containing CHR just as with les containing ordinary Prolog predicates. Thus CHR may be
consulted, compiled with various compilation modes, and compiled to le (see Chapter 5 [Load
Intro], page 51).
31.3.9 Compiler-generated Predicates
Besides predicates for the dened constraints, the CHR compiler generates some support predicates
in the module containing the handler. To avoid naming conicts, the following predicates must not
be dened or referred to by user code in the same module:
verify_attributes/3
attribute_goal/2
attach_increment/2
'attach_F/A'/2
for every dened constraint F/A.
'F/A_N_M_...'/Arity
for every dened constraint F/A. N,M is are integers, Arity > A.
For the prime number example that is:
attach_increment/2
attach_prime/1/2
attach_primes/1/2
attribute_goal/2
goal_expansion/3
prime/1
prime/1_1/2
Chapter 31: Constraint Handling Rules
315
prime/1_1_0/3
prime/1_2/2
primes/1
primes/1_1/2
verify_attributes/3
If an author of a handler wants to avoid naming conicts with the code that uses the handler, it
is easy to encapsulate the handler. The module declaration below puts the handler into module
primes, which exports only selected predicates - the constraints in our example.
:- module(primes, [primes/1,prime/1]).
:- use_module(library(chr)).
handler eratosthenes.
constraints primes/1,prime/1.
...
31.3.10 Operator Declarations
This table lists the operators as used by the CHR library:
::::::::::::-
op(1200,
op(1190,
op(1180,
op(1180,
op(1180,
op(1150,
op(1150,
op(1150,
op(1100,
op(1100,
op(1050,
op( 500,
xfx,
xfx,
xfx,
fy,
fy,
fx,
fx,
fx,
xfx,
xfx,
xfx,
yfx,
@).
pragma).
[==>,<=>]).
chr_spy).
chr_nospy).
handler).
constraints).
rules).
'|').
\ ).
&).
#).
31.3.11 Exceptions
The CHR runtime system reports instantiation and type errors for the predicates:
find_constraint/2
findall_constraints/3
insert_constraint/2
remove_constraint/1
notify_constrained/1
The only other CHR specic runtime error is:
316
SICStus Prolog
{CHR ERROR: registering <New>, module <Module> already hosts <Old>}
An attempt to load a second handler New into module <Module>
handler <Old> was made.
already hosting
The following exceptional conditions are detected by the CHR compiler:
{CHR Compiler ERROR: syntax rule <N>: <Term>}
If the N-th <Term> in the le being loaded
[CHR Syntax], page 309).
violates the CHR syntax (see Section 31.3.3
{CHR Compiler ERROR: too many general heads in <Name>}
Unspecic heads in denitions like C \ C <=> true
heads in rule <Name>.
{CHR Compiler ERROR: bad pragma <Pragma> in <Name>}
The pragma <Pragma> used in rule <Name>
happens if <Pragma> is unbound.
must not be combined with other
does not qualify. Currently this only
{CHR Compiler ERROR: found head <F/A> in <Name>, expected one of: <F/A list>}
Rule <Name> has a head of given F/A which is not among the dened constraints.
{CHR Compiler ERROR: head identifiers in <Name> are not unique variables}
The identiers to refer to individual constraints (heads) via `#' in rule <Name>
meet the indicated requirements.
do not
{CHR Compiler ERROR: no handler defined}
CHR specic language elements, declarations or rules for example, are used before a
handler was dened. This error is usually reported a couple of times, i.e. as often as
there are CHR forms in the le expecting the missing denition.
{CHR Compiler ERROR: compilation failed}
Not your fault. Send us a bug report.
31.4 Debugging CHR Programs
Use option(debug_compile,on) preceding any rules in the le containing the handler to enable
CHR debugging. The CHR debugging mechanism works by instrumenting the code generated
by the CHR compiler. Basically, the CHR debugger works like the Prolog debugger. The main
dierences are: there are extra ports specic to CHR, and the CHR debugger provides no means
for the user to change the ow of control, i.e. there are currently no retry and fail options available.
Chapter 31: Constraint Handling Rules
317
31.4.1 Control Flow Model
The entities reected by the CHR debugger are constraints and rules. Constraints are treated
like ordinary Prolog goals with the usual ports: [call,exit,redo,fail]. In addition, constraints
may get inserted into or removed from the constraint store (ports: insert,remove), and stored
constraints containing variables will be woken and reconsidered (port: wake) when variables are
touched.
The execution of a constraint consists of trying to apply the rules mentioning the constraint in
their heads. Two ports for rules reect this process: At a try port the active constraint matches
one of the heads of the rule, and matching constraints for the remaining heads of the rule, if any,
have been found as well. The transition from a try port to an apply port takes place when the
guard has been successfully evaluated, i.e. when the rule commits. At the apply port, the body of
the rule is just about to be executed. The body is a Prolog goal transparent to the CHR debugger.
If the rule body contains CHR constraints, the CHR debugger will track them again. If the rules
were consulted, the Prolog debugger can be used to study the evaluations of the other predicates
in the body.
31.4.2 CHR Debugging Predicates
The following predicates control the operation of the CHR debugger:
chr_trace
chr_debug
Switches the CHR debugger on and ensures that the next time control enters a CHR
port, a message will be produced and you will be asked to interact.
At this point you have a number of options. See Section 31.4.5 [CHR Debugging
Options], page 320. In particular, you can just type CR (Return) to creep (or singlestep) into your program. You will notice that the CHR debugger stops at many ports.
If this is not what you want, the predicate chr_leash gives full control over the ports
at which you are prompted.
Switches the CHR debugger on and ensures that the next time control enters a CHR
port with a spy-point set, a message will be produced and you will be asked to interact.
chr_nodebug
Switches the CHR debugger o. If there are any spy-points set then they will be kept.
chr_notrace
Equivalent to chr_nodebug.
chr_debugging
Prints onto the standard error stream information about the current CHR debugging
state. This will show:
1. Whether the CHR debugger is switched on.
2. What spy-points have been set (see below).
3. What mode of leashing is in force (see below).
318
SICStus Prolog
chr_leash(+Mode )
The leashing mode is set to Mode. It determines the CHR ports at which you are
to be prompted when you creep through your program. At unleashed ports a tracing
message is still output, but program execution does not stop to allow user interaction.
Note that the ports of spy-points are always leashed (and cannot be unleashed). Mode
is a list containing none, one or more of the following port names:
call
Prompt when a constraint is executed for the rst time.
exit
Prompt when the constraint is successfully processed, i.e. the applicable
rules have applied.
redo
Prompt at subsequent exits generated by non-deterministic rule bodies.
fail
Prompt when a constraint fails.
wake
Prompt when a constraint from the constraint store is woken and reconsidered because one of its variables has been touched.
try
Prompt just before the guard evaluation of a rule, after constraints matching the heads have been found.
apply
Prompt upon the application of a rule, after the successful guard evaluation, when the rule commits and res, just before evaluating the body.
insert
Prompt when a constraint gets inserted into the constraint store, i.e. after
all rules have been tried.
remove
Prompt when a constraint gets removed from the constraint store, e.g.
when a simplication rule applies.
The initial value of the CHR leashing mode is [call,exit,fail,wake,apply]. Predened shortcuts are:
chr_leash(none), chr_leash(off)
To turn leashing o.
chr_leash(all)
To prompt at every port.
chr_leash(default)
Same as chr_leash([call,exit,fail,wake,apply]).
chr_leash(call)
No need to use a list if only a singular port is to be leashed.
Chapter 31: Constraint Handling Rules
319
31.4.3 CHR Spy-points
For CHR programs of any size, it is clearly impractical to creep through the entire program. Spypoints make it possible to stop the program upon an event of interest. Once there, one can set
further spy-points in order to catch the control ow a bit further on, or one can start creeping.
Setting a spy-point on a constraint or a rule indicates that you wish to see all control ow through
the various ports involved, except during skips. When control passes through any port with a
spy-point set on it, a message is output and the user is asked to interact. Note that the current
mode of leashing does not aect spy-points: user interaction is requested on every port.
Spy-points are set and removed by the following predicates, which are declared as prex operators:
chr_spy
Spec
Sets spy-points on constraints and rules given by Spec, which is is of the form:
(variable)
denoting all constraints and rules, or:
constraints Cs
where Cs is one of
(variable)
denoting all constraints
C,...,C
denoting a list of constraints C
Name
denoting all constraints with this functor, regardless of arity
Name/Arity
denoting the constraint of that name and arity
rules Rs where Rs is one of:
(variable)
denoting all rules
R,...,R
denoting a list of rules R
Name
where Name is the name of a rule in any handler.
already in store
The name of a rule implicitly dened by the system when the
option already_in_store is in eect.
already in heads
The name of a rule implicitly dened by the system when the
option already_in_heads or the corresponding pragmas are
in eect.
Handler:Name
where Handler is the name of a constraint handler and Name
is the name of a rule in that handler
Examples:
320
SICStus Prolog
| ?- chr_spy rules rule(3), transitivity, already_in_store.
| ?- chr_spy constraints prime/1.
If you set spy-points, the CHR debugger will be switched on.
chr_nospy Spec
Removes spy-points on constraints and rules given by Spec, where Spec is of the form
as described for chr_spy Spec. There is no chr_nospyall/0. To remove all CHR
spy-points use chr_nospy _.
The options available when you arrive at a spy-point are described later. See Section 31.4.5 [CHR
Debugging Options], page 320.
31.4.4 CHR Debugging Messages
All trace messages are output to the standard error stream. This allows you to trace programs
while they are performing le I/O. The basic format is as follows:
S 3 1 try
eratosthenes:absorb(10) @ prime(9)#<c4>, prime(10)#<c2> ?
S is a spy-point indicator. It is printed as ` ' if there is no spy-point, as `r', indicating that there
is a spy-point on this rule, or as `c' if one of the involved constraints has a spy-point.
The rst number indicates the current depth of the execution; i.e. the number of direct ancestors
the currently active constraint has.
The second number indicates the head position of the currently active constraint at rule ports.
The next item tells you which port is currently traced.
A constraint or a matching rule are printed next. Constraints print as Term#Id, where Id is a
unique identier pointing into the constraint store. Rules are printed as Handler:Name @, followed
by the constraints matching the heads.
The nal `?' is the prompt indicating that you should type in one of the debug options (see
Section 31.4.5 [CHR Debugging Options], page 320).
31.4.5 CHR Debugging Options
This section describes the options available when the system prompts you after printing out a
debugging message. Most of them you know from the standard Prolog debugger. All the options
are one letter mnemonics, some of which can be optionally followed by a decimal integer. They
are read from the standard input stream up to the end of the line (Return, <CR>). Blanks will be
ignored.
The only option which you really have to remember is `h'. This provides help in the form of the
following list of available options.
Chapter 31: Constraint Handling Rules
CHR debugging options:
<cr>
creep
l
leap
s
skip
g
ancestors
&
constraints
n
nodebug
+
spy this
nospy this
<
reset printdepth
a
abort
?
help
c
<CR>
l
s
si
c
creep
s <i>
skip (ancestor i)
& <i>
=
constraints (details)
debugging
.
< <n>
b
h
show rule
set printdepth
break
help
creep causes the debugger to single-step to the very next port and print a message.
Then if the port is leashed, the user is prompted for further interaction. Otherwise, it
continues creeping. If leashing is o, creep is the same as leap (see below) except that
a complete trace is printed on the standard error stream.
leap causes the debugger to resume running your program, only stopping when a spypoint is reached (or when the program terminates). Leaping can thus be used to follow
the execution at a higher level than exhaustive tracing.
skip over the entire execution of the constraint. That is, you will not see anything
until control comes back to this constraint (at either the exit port or the fail port).
This includes ports with spy-points set; they will be masked out during the skip.
The command can be used with a numeric argument to skip the execution up to and
including the ancestor indicated by the argument. Example:
...
4
Ancestors:
1
1
2
1
3
1
4
4
2
g
321
exit
prime(8)#<c6> ? g
apply
apply
apply
call
eratosthenes:rule(2) @ primes(10)#<c1>
eratosthenes:rule(2) @ primes(9)#<c3>
eratosthenes:rule(2) @ primes(8)#<c5>
prime(8)#<c6>
- exit
- exit
prime(8)#<c6> ? s 2
primes(9)#<c3> ?
print ancestors provides you with a list of ancestors to the currently active constraint,
i.e. all constraints not yet exited that led to the current constraint in the derivation
sequence. The format is the same as with trace messages. Constraints start with call
entries in the stack. The subsequent application of a rule replaces the call entry in the
stack with an apply entry. Later the constraint shows again as redo or fail entry.
Example:
322
SICStus Prolog
0
1
- call
1 try
Ancestors:
1
- call
1
1
1
2
2
1
1
-
try
apply
call
insert
exit
Ancestors:
1
1 apply
2
- call
&
n
=
primes(10)#<c1> ?
eratosthenes:rule(2) @ primes(10)#<c1> ? g
primes(10)#<c1>
eratosthenes:rule(2) @ primes(10)#<c1> ?
eratosthenes:rule(2) @ primes(10)#<c1> ?
prime(10)#<c2> ?
prime(10)#<c2>
prime(10)#<c2> ? g
eratosthenes:rule(2) @ primes(10)#<c1>
prime(10)#<c2>
print constraints prints a list of the constraints in the constraint store. With a numeric
argument, details relevant primarily to CHR implementors are shown.
nodebug switches the CHR debugger o.
debugging outputs information concerning the status of the CHR debugger as via chr_
debugging/0
+
.
spy this sets a spy-point on the current constraint or rule.
nospy this removes the spy-point from the current constraint or rule, if it exists.
show rule prints the current rule instantiated by the matched constraints. Example:
8
1 apply
era:absorb(8) @ prime(4)#<c14> \ prime(8)#<c6> ? .
absorb(8) @
prime(4)#<c14> \
prime(8)#<c6> <=>
8 mod 4=:=0
|
true.
<
<n
a
b
While in the debugger, a printdepth is in eect for limiting the subterm nesting level
when printing rules and constraints. The limit is initially 10. This command, without
arguments, resets the limit to 10. With an argument of n, the limit is set to n, treating
0 as innity.
abort calls the built-in predicate abort/0.
break calls the built-in predicate break/0, thus putting you at a recursive top-level.
When you end the break (entering ^D) you will be re-prompted at the port at which
you broke. The CHR debugger is temporarily switched o as you call the break and
will be switched on again when you nish the break and go back to the old execution.
Any changes to the CHR leashing or to spy-points during the break will remain in
eect.
Chapter 31: Constraint Handling Rules
?
h
323
help displays the table of options given above.
31.5 Programming Hints
This section gives you some programming hints for CHR. For maximum eciency of your constraint
handler, see also the previous subsections on declarations and options.
Constraint handling rules for a given constraint system can often be derived from its denition
in formalisms such as inference rules, rewrite rules, sequents, formulas expressing axioms and
theorems. CHR can also be found by rst considering special cases of each constraint and then
looking at interactions of pairs of constraints sharing a variable. Cases that do not occur in the
application can be ignored.
It is important to nd the right granularity of the constraints. Assume one wants to express
that n variables are dierent from each other. It is more ecient to have a single constraint
all_different(List_of_n_Vars) than n*n inequality constraints between each pair of dierent
variables. However, the extreme case of having a single constraint modeling the whole constraint
store will usually be inecient.
Starting from an executable specication, the rules can then be rened and adapted to the specics
of the application. Eciency can be improved by weakening the guards to perform simplication
as early as needed and by strengthening the guards to do the just right amount of propagation.
Propagation rules can be expensive, because no constraints are removed.
The more heads a rule has, the more expensive it is. Rules with several heads are more ecient,
if the heads of the rule share a variable (which is usually the case). Then the search for a partner
constraint has to consider less candidates. In the current implementation, constraints are indexed
by their functors, so that the search is only performed among the constraints containing the shared
variable. Moreover, two rules with identical (or suciently similar) heads can be merged into one
rule so that the search for a partner constraint is only performed once instead of twice.
As guards are tried frequently, they should be simple tests not involving side-eects. Head matching
is more ecient than explicitly checking equalities in the ask-part of the guard. In the tell part of a
guard, it should be made sure that variables from the head are never touched (e.g. by using nonvar
or ground if necessary). For eciency and clarity reasons, one should also avoid using constraints
in guards. Besides conjunctions, disjunctions are allowed in the guard, but they should be used
with care. The use of other control built-in predicates in the guard is discouraged. Negation and
if-then-else in the ask part of a guard can give wrong results, since e.g. failure of the negated goal
may be due to touching its variables.
Several handlers can be used simultaneously if they do not share constraints with the same name.
The implementation will not work correctly if the same constraint is dened in rules of dierent
handlers that have been compiled separately. In such a case, the handlers must be merged by
hand. This means that the source code has to be edited so that the rules for the shared constraint
are together (in one module). Changes may be necessary (like strengthening guards) to avoid
divergence or loops in the computation.
324
SICStus Prolog
31.6 Constraint Handlers
The CHR library comes with plenty of constraint handlers written in CHR:
`arc.pl' classical arc-consistency over nite domains
`bool.pl' simple Boolean constraints
`cft.pl' feature term constraints according to the CFT theory
`domain.pl'
nite domains over arbitrary ground terms and interval domains over integers and reals,
but without arithmetic functions
`gcd.pl' elegant two-liner for the greatest common divisor
`interval.pl'
straightforward interval domains over integers and reals, with arithmetic functions
`kl-one.pl'
terminological reasoning similar to KL-ONE or feature trees
`leq.pl' standard introductory CHR example handler for less-than-or-equal
`list.pl' equality constraints over concatenations of lists (or strings)
`math-elim.pl'
solves linear polynomial equations and inequations using variable elimination, several
variations possible
`math-fougau.pl'
solves linear polynomial equations and inequations by combining variable elimination
for equations with Fourier's algorithm for inequations, several variations possible
`math-gauss.pl'
a straightforward, elegant implementation of variable elimination for equations in one
rule
`minmax.pl'
simple less-than and less-than-or-equal ordering constraints together with minimum
and maximum constraints
`modelgenerator.pl'
example of how to use CHR for model generation in theorem proving
`monkey.pl'
classical monkey and banana problem, illustrates how CHR can be used as a fairly
ecient production rule system
`osf.pl' constraints over order sorted feature terms according to the OSF theory
`pathc.pl'
the most simple example of a handler for path consistency - two rules
Chapter 31: Constraint Handling Rules
325
`primes.pl'
elegant implementations of the sieve of Eratosthenes reminiscent of the chemical abstract machine model, also illustrates use of CHR as a general purpose concurrent
constraint language
`scheduling.pl'
simple classical constraint logic programming scheduling example on building a house
`tarski.pl'
most of Tarski's axiomatization of geometry as constraint system
`term.pl' Prolog term manipulation built-in predicates functor/3, arg/3, =../2 as constraints
`time-pc.pl'
grand generic handler for path-consistency over arbitrary constraints, load via
`time.pl' to get a powerful solver for temporal constraints based on Meiri's unifying
framework. `time-rnd.pl' contains a generator for random test problems.
`time-point.pl'
quantitative temporal constraints over time points using path-consistency
`tree.pl' equality and disequality over nite and innite trees (terms)
`type.pl' equalities and type constraints over nite and innite trees (terms)
You can consult or compile a constraint handler from the CHR library using e.g.:
?- [library('chr/examples/gcd')].
?- compile(library('chr/examples/gcd')).
If you want to learn more about the handlers, look at their documented source code.
In addition, there are les with example queries for some handlers, their le name starts with
`examples-' and the le extension indicates the handler, e.g. `.bool':
examples-adder.bool
examples-benchmark.math
examples-deussen.bool
examples-diaz.bool
examples-fourier.math
examples-holzbaur.math
examples-lim1.math
examples-lim2.math
examples-lim3.math
examples-puzzle.bool
examples-queens.bool
examples-queens.domain
examples-stuckey.math
examples-thom.math
326
SICStus Prolog
31.7 Backward Compatibility
In this section, we discuss backward compatibility with the CHR library of Eclipse Prolog.
1. The restriction on at most two heads in a rule has been abandoned. A rule can have as many
heads as you like. Note however, that searching for partner constraints can be expensive.
2. By default, rules are compiled in textual order. This gives the programmer more control over
the constraint handling process. In the Eclipse library of CHR, the compiler was optimizing
the order of rules. Therefore, when porting a handler, rules may have to be reordered. A good
heuristic is to prefer simplication to simpagation and propagation and to prefer rules with
single heads to rules with several heads. Instead of manually rearranging an old handler one
may also use the following combination of options to get the corresponding eect:
option(rule_ordering,heuristic).
option(revive_scheme,old).
3. For backward compatibility, the already_in_store, already_in_head and guard_bindings
options are still around, but there are CHR syntax extensions: Section 31.3.3 [CHR Syntax],
page 309 and pragmas Section 31.3.5 [CHR Pragmas], page 311 oering better grained control.
4. The Eclipse library of CHR provided automatic built-in labeling through the label_with declaration. Since it was not widely used and can be easily simulated, built-in labeling was dropped.
The same eect can be achieved by replacing the declaration label_with Constraint if
Guard by the simplication rule chr_labeling, Constraint <=> Guard | Constraint', chr_
labeling and by renaming the head in each clause Constraint :- Body into Constraint'
:- Body where Constraint' is a new predicate. Eciency can be improved by declaring Constraint to be passive: chr_labeling, Constraint#Id <=> Guard | Constraint',
chr_labeling pragma passive(Id). This translation will not work if option(already_in_
heads,on). In that case use e.g. chr_labeling(_), Constraint <=> Guard | Constraint',
chr_labeling(_) to make the new call to chr_labeling dier from the head occurrence.
5. The set of built-in predicates for advanced CHR users is now larger and better designed.
Also the debugger has been improved. The Opium debugging environment is not available in
SICStus Prolog.
Chapter 32: Prolog Objects
327
32 Prolog Objects
Prolog Objects is an extension to SICStus Prolog for exible structuring, sharing and reuse of
knowledge in large logic programming applications. Prolog Objects enhances Prolog with an expressive and ecient object-oriented programming component.
Prolog Objects is based on the notion of prototypes. In object-oriented programming a prototype
is an object that represents a typical behavior of a certain concept. A prototype can be used as is
or as a model to construct other objects that share some of the characteristics of the prototypical
object. These specialized objects can themselves become prototypes used to construct other objects
and so forth. The basic mechanism for sharing is by inheritance and delegation. Inheritance is
known for most readers. By using the delegation mechanism an object can forward a message to
another object to invoke a method dened by the recipient but interpreted in the context of the
sender.
In Prolog Objects, an object is a named collection of predicate denitions. In this sense an object
is similar to a Prolog module. The object system can be seen as an extension of SICStus Prolog's
module system. In addition an object may have attributes that are modiable. Predicate denitions
belonging to an object are called methods. So, an object is conceptually a named collection of
methods and attributes. Some of the methods dened for an object need not be stored explicitly
within the object, but are rather shared with other objects by the inheritance mechanism.
The Object system allows objects to be dened in a le, or dynamically created during the execution
of a program. Objects dened in a le are integrated into SICStus Prolog in a way similar to denite
clause grammars. That is to say, objects have a specic syntax as Prolog terms, and can be loaded
and expanded into Prolog code. When an object is created, during load-time, or run-time, it
inherits the methods and attributes of its prototypical object(s). Objects dened in a le can be
either static or dynamic. Also, methods can be either dynamic or static. these properties are
inherited by sub-objects. Objects created during execution are dynamic.
The inheritance mechanism is implemented using the importation mechanism of the module system.
The default inheritance is an inheritance by overriding mechanism, which means that if a method
is dened locally, and the same method is dened in a super-object, then the clauses of the supermethod are not part of the denition of the local one. As usual in Prolog, methods can be nondeterminately dened, and alternative answers can be retrieved through backtracking. Using the
delegation mechanism, other methods for knowledge sharing can be implemented by the user. In
Objects, there is an initial prototypical proto-object called object, from which other objects may
be constructed, directly or indirectly.
328
SICStus Prolog
32.1 Getting Started
To load the Prolog Objects library, enter the query:
| ?- use_module(library(objects)).
Prolog Objects denes some new inx and prex operators, and redenes some of the built-in ones.
The following operators become installed:
:::::-
op(1200, xfy, [ & ]).
op(1198, xfx, [ :- ]).
op(1198, fx, [ :- ]).
op(550, xfx, [ ::, <: ]).
op(550,
fx, [ ::, <: ]).
32.2 Declared Objects
Declared objects are created when the les dening them are loaded into the system.
32.2.1 Object Declaration
An object object-identier is declared by writing it in the following form:
object-identier :: {
sentence-1 &
sentence-2 &
:
sentence-n
}.
where object-identier is a Prolog term that is either an atom or a compound term of the form
functor(V1,...,Vn), where V1,...,Vn are distinct variables. The object body consists of a number of
sentences, possibly none, surrounded by braces, where each sentence is either a method-directive,
to be executed when the object is created, or a method-clause. A method is a number of methodclauses with the same principal functor. A method-clause has a clausal syntax similar to that
of Prolog, but instead of usual predicate calls in the body of a clause there are method-calls.
Ordinary Prolog goals are also allowed in a prexed form, using ':' as a prex. A method-directive
is a directive which contains method-calls.
All sentences are subject to term expansion (see Section 7.1.2 [Denite], page 76, built-in expand_
term/2) before further processing, so in particular denite clause grammar syntax can be used
in method-clauses. In addition, before expand_term/2, sentences are expanded by the predicate
user:method_expansion/3.
Chapter 32: Prolog Objects
329
method_expansion(+Term1,+ObjectIdentier,?Term2 )
user:method_expansion(+Term1,+ObjectIdentier,?Term2 )
A hook predicate, which denes transformations on methods similarly as user:term_
expansion/(2,4). At the end of an object denition, user:method_expansion/3 is
called with end_of_object.
32.2.2 Method Declarations
Method-clauses are declared similarly to Prolog clauses. Thus a method-clause can be either a
unit-clause or a non-unit-clause. We also allow a default catch-all method-clause as the last clause
in an object body. The catch-all clause has as its head a Prolog variable, in order to match messages
that are not previously dened or inherited in the object. It can be used to implement alternative
inheritance mechanisms.
Goals in the body of a non-unit clause have the normal control structures of Prolog:
:P, :Q
:P ; :Q
!
\+
:P
Conjunction
Disjunction
Cut
Negation
:P -> :Q
:P -> :Q ; :R
if(:P, :Q, :R)
If-then[-else]
?A = ?B Unication
Atomic goals in the body of a method-clause may be one of the following:
:goal
m:goal
goal
::goal
<:goal
object::goal
object<:goal
to call the Prolog predicate goal in the source module.
to call the Prolog predicate goal in module m.
to send the message goal to the object Self.
to send the message goal to a method that may be dened locally or inherited by the
object.
to delegate the message goal to a method that may be dened locally or inherited by
the object.
to send the message goal to object object.
to delegate the message goal to object object.
330
SICStus Prolog
Message sending and delegation will be explained later (see Section 32.3 [Obj Self], page 332).
The following is a denition for the object list_object. It is constructed from three methods:
append/3, member/2, and length/2. Note that the calls to append/3 and length/2 are to the
local denition, whereas the member/2 call is to the predicate imported from the Prolog library
module lists.
list_object :: {
:- :use_module(library(lists), [append/3,member/2]) &
append([], L, L) &
append([X|L1], L2, [X|L3]) ::: append(L1, L2, L3) &
member(X, L) ::member(X,L) &
length([], 0) &
length([_|L], N) ::: length(L, N1),
:(N is N1+1)
}.
The following object apt_1 could be part of a larger database about free apartments in a real-estate
agency:
apt_1 :: {
super(apartment) &
street_name('York') &
street_number(100) &
wall_color(white) &
floor_surface(wood)
}.
Another way to dene apt_1 is by using attributes. These can be retrieved and modied eciently
by the methods get/1 and set/1 respectively.
apt_1 :: {
super(apartment) &
attributes([
street_name('York'),
street_number(100),
wall_color(white),
floor_surface(wood)])
}.
Chapter 32: Prolog Objects
331
32.2.3 Generic Objects for Easy Reuse
Dening objects for easy reuse is a very important property for reducing the cost of large projects.
One important technique is to dene prototypes in a parameterized way, so that various instantiations of a prototype correspond to dierent uses. Parameterized or generic objects have been used
for this purpose in other object-oriented systems. An object-identier can be a compound term.
The arguments of the term are parameters that are visible in the object-body. Here we show one
example. Other examples and techniques that use this facility has been investigated extensively in
[McCabe 92].
The following is an object sort that sorts lists of dierent types. sort has a parameter that denes
the type of the elements of the list. Notice that Type is visible to all methods in the body of sort,
and is used in the method partition/4. In the query, we use sort(rat) to sort a list of terms
denoting rational numbers. We must therefore dene a rat object and its < method also:
rat :: {
(P/Q < R/S) :- :(P*S < Q*R)
}.
sort(Type) :: {
:- :use_module(library(lists), [append/3]) &
qsort([], []) &
qsort([P|L], S) :partition(L, P, Small, Large),
qsort(Small, S0),
qsort(Large, S1),
:append(S0, [P|S1], S) &
partition([], _P, [], []) &
partition([X|L1], P, Small, Large) :(
Type :: (X < P) ->
Small = [X|Small1], Large = Large1
;
Small = Small1, Large = [X|Large1]
),
partition(L1, P, Small1, Large1)
}.
| ?- sort(rat) :: qsort([23/3, 34/11, 45/17], L).
L = [45/17,34/11,23/3]
Parameterized objects are interesting in their own right in Prolog even if one is not interested in
the object-oriented paradigm. They provide global context variables in a Prolog program without
having to add such variables as additional context arguments to each clause that potentially uses
the context.
332
SICStus Prolog
32.3 Self, Message Sending, and Message Delegation
In Prolog Objects, each method is executed in the context of an object. This object may not
be the static object where the method is declared. The current contextual object is used to
determine dynamically which attributes are accessed, and which methods are called. This leads to
a mechanism known as dynamic binding. This object can be retrieved using the universal method
self(S ), where S will be bound to the current contextual object.
When a message is sent to an object, the corresponding method will be executed in the context of
the target object. A message delegated to an object will invoke a method that is executed in the
context of the message-delegation operation.
object :: message
:: message Message sending. Sends message to object, setting Self of the recipient to the recipient,
i.e. object. If object is omitted, the recipient is the object in which the goal textually
appears.
object <: message
<: message
Message delegation. Sends message to object, setting Self of the recipient to Self of
the sender. If object is omitted, the recipient is the object in which the goal textually
appears. Delegation preserves Self .
The following objects physical_object, a, and b are written using the default notations for sending
and delegation, hiding the contextual variable Self :
physical_object :: {
volume(50) &
density(100) &
weight(X) :volume(V),
density(D),
:(X is V*D)
}.
a :: {
volume(5) &
density(10) &
Method :physical_object <: Method
}.
b :: {
volume(5) &
density(10) &
Method :physical_object :: Method
Chapter 32: Prolog Objects
333
}.
Notice that the dierence between the objects a and b is that a delegates any message except
volume(_) and density(_) to physical_object while b sends the message to physical_object.
We may now ask
| ?- a :: weight(X), b :: weight(Y).
X = 50
Y = 5000
To get hold of the current contextual object, the universal method self(S ) is provided. Another
way to send a message to Self is to use the constant self. So the following two alternative denition
of physical_object are equivalent to the previous one:
physical_object :: {
volume(50) &
density(100) &
weight(X) :self(S),
S::volume(V),
S::density(D),
:(X is V*D)
}.
physical_object :: {
volume(50) &
density(100) &
weight(X) :self::volume(V),
self::density(D),
:(X is V*D)
}.
32.4 Object Hierarchies, Inheritance, and Modules
The Prolog Objects system implements a default inheritance mechanism. By declaring within an
object which objects are super-objects, the hierarchy of objects are maintained. The system also
maintains for each object its immediate sub-objects (i.e. immediate children). Each object may
also call Prolog predicates. At the top of the hierarchy, the proto-object object provides various
services for other objects. If object is not used at the top of the hierarchy many services will not
be available for other objects (check what methods are available in object by sending the message
method/1 to object).
334
SICStus Prolog
32.4.1 Inheritance
Immediate super-objects are declared by dening the method super/2 within the object. (Any
denition super(Super ) is transformed to super(Super,[])). The objects declared by super/2
are the immediate objects from which a method is inherited if not dened within the object. This
implies that the inheritance mechanism is an overriding one. One could possibly have a union
inheritance, whereby all clauses dening a method are collected from the super hierarchy and
executed in a Prolog fashion. This can easily be programmed in Prolog Objects, using delegation
to super objects.
The following example shows some objects used for animal classication.
animal :: {}.
bird :: {
super(animal) &
skin(feather) &
habitat(tree) &
motions(fly)
}.
penguin :: {
super(bird) &
habitat(land) &
motions(walk) &
motions(swim) &
size(medium)
}.
| ?- penguin :: motions(M).
M = walk ;
M = swim ;
no
| ?- penguin :: skin(S).
S = feather ;
no
The following is an example of multiple inheritance: an object john is both a sportsman and a
professor:
john :: {
super(sportsman) &
super(professor) &
:
}.
Inheritance will give priority to the super-objects by the order dened in the super/2 method.
Therefore in the above example John's characteristics of being a sportsman will dominate those
Chapter 32: Prolog Objects
335
of being professor. Other kinds of hierarchy traversal can be programmed explicitly using the
delegation mechanism.
32.4.2 Dierential Inheritance
It is possible to be selective about what is inherited by using the method super/2. Its rst argument
is the super object, and its second is a list of the methods that will not be inherited from the super
object.
32.4.3 Use of Modules
In Prolog Objects, the visible predicates of the source module (context) for the object denition
may be called in the body of a method. (The : prex is used to distinguish such calls from method
calls.) Any (: prexed) directives occurring among the method-clauses are also executed in the
same source module. For example, to import into the source module and call the public predicates
of a module, the built-in predicate use_module/2 and its variants may be used:
some_object :: {
:- :use_module(library(lists), [append/3]) &
double_list(X, XX) :- :append(X,X,XX)
}.
32.4.4 Super and Sub
Two methods provided by the initial object object are super/1 and sub/1. (Note that any
denition of super/1, except the one in object, is transformed to super/2). super/1 if sent to an
object will return the immediate parents of the object. sub/1 will return the immediate children
of the object if any. It is important to note that this service is provided only for objects that have
object as their initial ancestor.
| ?- john :: super(S), S :: sub(john).
S = sportsman ;
S = professor ;
no
The sub/1 property allows programs to traverse object hierarchies from a root object object down
to the leaves.
336
SICStus Prolog
32.4.5 The Keyword Super
To be able to send or delegate messages to the super-objects in a convenient way while following
the inheritance protocol, the keyword super is provided. The calls:
super :: method, or
super <: method
mean: send or delegate (respectively) method to the super-objects according to the inheritance
protocol. A simple example illustrates this concept: assume that john in the above example has
three id-cards, one stored in his sportsman prototype identifying the club he is member of, one
stored in his professor prototype identifying the university he works in, and nally one stored locally
identifying his social-security number. Given the following methods in the object john:
m1(X) :super <: id_card(X) &
m2(X) :-
super(S), S <: id_card(X) &
one may ask the following:
| ?- john :: m1(X).
% will follow the default
X = johns_club ;
inheritance and returns:
| ?- john :: m2(X).
% will backtrack through
X = johns_club ;
X = johns_university ;
the possible supers returning:
32.4.6 Semantic Links to Other Objects
Some object-oriented languages have syntactic constructs for redirecting the inheritance chain for
certain methods to completely other objects which are not dened in the object's inheritance
hierarchy. This is not needed in Prolog Objects due to delegation. Assume that the method m/n is
linked to object some_object, we just add a method for this:
m(X1, ..., Xn) :- some_object <: m(X1, ..., Xn) &
32.4.7 Dynamically Declared Objects
When an object is declared and compiled into Prolog Objects, its methods cannot be changed
during execution. Such an object is said to be static. To be able to update any method in an
object, the object has to be declared dynamic. There is one exception, the inheritance hierarchy
declared by super/(1-2) cannot be changed. By including the fact dynamic as part of the object
body, the object becomes dynamic:
Chapter 32: Prolog Objects
337
dynamic_object :: {
dynamic &
:
}.
32.4.8 Dynamic Methods
To be able to change a method with functor F and arity N in a static object, the method has to
be declared dynamic by storing the following fact in the object:
some_object :: {
dynamic F/N &
:
}.
Each book in a library can be represented as an object, in which the name of the book is stored,
the authors, and a borrowing history indicating when a book is borrowed and when it is returned.
A history item may have the form history_item(Person,Status,Date ) where Status is either
borrowed or returned, and Date has the form YY-MM-DD, for YY year, MM month, DD day.
A typical book book_12 could have the following status. Note that history_item/3 is dynamic:
book_12 :: {
super(book) &
title('The Art of Prolog') &
authors(['Leon Sterling', 'Ehud Shapiro']) &
dynamic history_item/3 &
history_item('Dan Sahlin', returned, 92-01-10) &
history_item('Dan Sahlin', borrowed, 91-06-10) &
:
}.
Dynamic methods that are stored in an object can be updated, as in usual Prolog programs, by
sending assert and retract messages directly to the object.
For example, to borrow a book the following method could be dened in the object book. We
assume that the top most history_item fact is the latest transaction, and there is an object date
from which we can get the current date.
borrow(Person) :history_item(_Person0, Status, _Date0), !,
(
Status = returned ->
date::current(Date),
asserta(history_item(Person, borrowed, Date))
;
:display('book not available'), :ttynl
) &
338
SICStus Prolog
32.4.9 Inheritance of Dynamic Behavior
When an object is created, it will inherit from its parents their dynamic behavior. Methods that are
declared dynamic in a parent, will be copied into the object, and its dynamic behavior preserved.
a:: {
}
b :: {
}
super(object) &
dynamic p/1 &
p(1) &
p(2)
super(a)
| ?- b::p(X).
X = 1 ? ;
X = 2 ? ;
no
| ?- b::asserta(p(3)).
yes
| ?- b::p(X).
X = 3 ? ;
X = 1 ? ;
X = 2 ? ;
no
Notice that by redeclaring a method to be dynamic in a sub-object, amounts to redening the
method, and overriding of the parent denition will take eect.
c :: {
}
super(a) &
dynamic p/1
| ?- c::p(X).
no
32.5 Creating Objects Dynamically
As with dynamically declared objects, the full exibility of Prolog Objects is achieved when objects
are created at runtime. Anything, except the inheritance hierarchy, can be changed: methods can
be added or deleted. The services for object creation, destruction, and method modication are
dened in the proto-object object.
Chapter 32: Prolog Objects
339
32.5.1 Object Creation
+SomeObject :: new(?NewObject)
NewObject is created with SomeObject as super. NewObject could be an atom, variable, or compound term whose arguments are distinct variables.
+SomeObject :: new(?NewObject,+Supers )
NewObject is created with Supers specifying the super objects (prototypes). Supers is a
list containing super specications. A super specication is either an object identier or
a pair Object-NotInheritList where NotInheritList species methods not to inherit from
Object. NewObject could be an atom, variable, or compound term whose arguments
are distinct variables.
The object
moving_van
vehicle is created having the proto-object object
with vehicle as super, followed by creating truck.
as super, followed by creating
| ?- object :: new(vehicle),
vehicle :: new(moving_van),
moving_van :: new(truck).
yes
| ?- truck :: super(X), vehicle :: sub(X).
X = moving_van ;
no
32.5.2 Method Additions
+SomeObject :: asserta(+SomeMethod )
+SomeObject :: assertz(+SomeMethod )
+SomeObject :: assert(+SomeMethod )
Asserts SomeMethod in SomeObject with normal Prolog semantics.
Add some facts to vehicle and truck with initial value equal to [].
| ?- vehicle :: assert(fuel_level([])),
vehicle :: assert(oil_level([])),
vehicle :: assert(location([])),
truck :: assert(capacity([])),
truck :: assert(total_weight([])).
yes
340
SICStus Prolog
32.5.3 Parameter Passing to New Objects
When new objects are created, it is possible to pass parameters. The following example shows:
How general methods are asserted
In the previous examples one could pass parameters to an object as follows, using the method
augment/1.
| ?- vehicle :: augment({
new_attrs(Instance, Attribute_list) :self :: new(Instance),
:: assign_list(Attribute_list, Instance) &
assign_list([], Instance) &
assign_list([Att|List], Instance) ::: assign(Att, Instance),
:: assign_list(List, Instance) &
yes
assign(P, Instance) :Instance :: assert(P)
}).
% create a new 'truck'
| ?- vehicle :: new_attrs(truck, [capacity([]),total_weight([])]).
yes
32.6 Access Driven Programming|Daemons
Access based programming is a paradigm where certain actions are performed, or some constraints
are checked, when \access operations" are invoked. Access operations for updates (i.e. assert,
retract) can be redened in an object by redening these operations and delegating the same
operation to super. Notice that without a delegation mechanism this would not be possible, since
the Self would have changed. So assume that we want to print on the screen \p is augmented"
whenever the fact p(X) is asserted in an object foo, we just redene assert/1:
foo :: {
super(object) &
dynamic p/1 &
p(0) &
p(1) &
assert(p(X)) :- !,
/* assert/1 is redefined for p(X) */
super <: assert(p(X)),
:display('p is augmented'), :ttynl &
Chapter 32: Prolog Objects
341
assert(M) :/* delegating assert(_) messages */
super <: assert(M) &
:
}.
32.7 Instances
Objects are relatively heavy weight. To be able to create eciently light weight objects, we introduce the notion of instances. An instance is an object with restricted capability. It is created from
an object that is considered its class. It gets a copy of the attributes of its class. These can be
modied by get/1 and set/1. An instance cannot be a class for other instances. Instances are in
general very ecient, both in space and access/modication time. The attribute '$class'/1 will
store the identity of the class of the instance including parameters.
32.8 Built-In Objects and Methods
32.8.1 Universal Methods
The following methods are \universal", i.e. they are dened locally, if appropriate, for every object:
super(?Object,?NotInheritList)
Object is a parent (a super-object) of Self. NotInheritList species methods of Object explicitly not inherited by Self. The denition super(Object) is translated to
super(Object,[]).
attributes(+Attributes )
Attributes is a list of compound terms specifying the local attributes of Self and the
initial values.
32.8.2 Inlined Methods
The following methods are compiled inline i.e. calls are replaced by denitions. This implies (in
the current implementation) that they have a xed semantics an can not be redened. There are
also denitions for these methods in object covering the cases of unexpanded calls.
self(?Self )
Unies Self with "self".
get(+Attribute )
Gets the attribute value(s) of the attribute specied by the head functor of Attribute.
The value(s) are unied with the argument(s) of Attribute.
set(+Attribute )
Sets the attribute value(s) of the attribute specied by the head functor of Attribute.
The value(s) are taken from the argument(s) of Attribute.
342
SICStus Prolog
32.8.3 The Proto-Object "object"
The proto-object object provides basic methods that are available to all other objects by delegation:
super(?Object)
Object is a parent (a super-object) of Self. Note that any other denition of
super(Object) are translated to the universal method super/2.
sub(?Object)
Object is a child (a sub-object) of Self.
self(?Self )
Unies Self with "self". Note: this method is inlined when possible.
object(?Object)
One of the dened objects in the system is Object.
dynamic
Self is a dynamic object.
static
Self is a static object.
dynamic ?Name/?Arity
Name/Arity is a dynamic method of Self.
static ?Name/?Arity
Name/Arity is a static method of Self.
new(?Object)
Creates a new dynamic Object. Self will be the prototype of Object. Object can be a
compound term, an atom, or a variable. In the last case the method generates a unique
name for Object.
+SomeObject :: new(?NewObject,+Supers )
NewObject is created with Supers specifying the super objects (prototypes). Supers is a
list containing super specications. A super specication is either an object identier or
a pair Object-NotInheritList where NotInheritList species methods not to inherit from
Object. NewObject could be an atom, variable, or compound term whose arguments
are distinct variables.
instance(?Instance )
Creates a new instance Instance. Self will be the class of Instance. Instance can be a
compound term, an atom, or a variable. In the last case the method generates a unique
name for Instance.
has_instance(?Instance )
Self has the instance Instance.
has_attribute(?AttributeSpec )
Self has the attribute AttributeSpec, locally dened or inherited. AttributeSpec is on
the format Name /Arity.
get(+Attribute )
Gets the attribute value(s) of the attribute specied by the head functor of Attribute.
The value(s) are unied with the argument(s) of Attribute. Note: this method is
inlined when possible.
Chapter 32: Prolog Objects
343
set(+Attribute )
Sets the attribute value(s) of the attribute specied by the head functor of Attribute.
The value(s) are taken from the argument(s) of Attribute. Note: this method is inlined
when possible.
assert(+Fact)
assert(+Fact, -Ref )
asserta(+Fact)
asserta(+Fact, -Ref )
assertz(+Fact)
assertz(+Fact, -Ref )
Asserts a new Fact in Self. If Self is static, the name and arity of Fact must be declared
as a dynamic method. asserta places Fact before any old facts. The other forms place
it after any old facts. A pointer to the asserted fact is returned in the optional argument
Ref, and can be used by the Prolog built-in predicates erase/1 and instance/2.
retract(+Fact)
Retracts a Fact from Self. If Self is static, the name and arity of Fact must be declared
as a dynamic method.
update(+Fact)
Replaces the rst fact with the same name and arity as Fact in Self by Fact. If Self is
static, the name and arity of Fact must be declared as a dynamic method.
retractall(?Head )
Removes facts from Self that unify with Head. If Self is static, the name and arity of
Fact must be declared as a dynamic method.
abolish
Abolishes Self if dynamic.
augment(?ObjectBody )
augmenta(?ObjectBody )
augmentz(?ObjectBody )
ObjectBody, having the form { sentence-1 & ... & sentence-n }, is added to Self.
augmenta places the new clauses before any old clauses. The other forms place it after
any old clauses.
32.8.4 The built-in object "utility"
The base object utility provides methods that could be used in user programs.
object as its super-object.
subs(?Objects )
Gives a list of all the children of Self.
supers(?Objects )
Gives a list of all parents of Self.
objects(?Objects )
Gives a list of all objects.
utility
has
344
dynamic_objects(?Objects )
SICStus Prolog
Gives a list of all dynamic objects.
static_objects(?Objects )
Gives a list of all static objects.
methods(?Methods )
Gives a list of all the methods of Self.
dynamic_methods(?Methods )
Gives a list of all dynamic methods of Self.
static_methods(?Methods )
Gives a list of all static methods of Self.
descendant(?Object)
One of the descendants of Self is Object.
descendant(?Object, ?Level )
Object a descendant at depth Level of Self. A child of Self is at level 1.
descendants(?Objects )
The list of all descendants of Self is Objects.
descendants(?Objects, ?Level )
Objects is the list of descendants at depth Level of Self.
ancestor(?Object)
One of the ancestors of Self is Object.
ancestor(?Object, ?Level )
Object is an ancestor of Self at height Level. A super-object of Self has level 1.
ancestors(?Object)
The list of all ancestors of Self is Objects.
ancestors(?Object, ?Level )
Objects is the list of ancestors at height Level of Self.
restart
Removes all dynamic objects. Note that dynamic methods added to static objects are
not removed.
and_cast(+Objects, ?Message )
Sends the same message Message to all objects in the list Objects.
or_cast(+Objects, ?Message )
Sends the same message Message to one of the objects in the list Objects, backtracking
through the alternative objects.
Chapter 32: Prolog Objects
345
32.9 Expansion to Prolog Code
As already mentioned, object denitions are expanded to Prolog clauses much as denite clause
grammars. This expansion is usually transparent to the user. While debugging a Prolog Objects
program, however, the expanded representation may become exposed. This section will explain
in detail the source expansion, so as to give the user the possibility to relate back to the source
code during a debugging session. The inheritance mechanism, based on module importation, is also
described.
First of all, every statically dened object will translate to several Prolog clauses belonging to
a unique object module with the same identity as the object-identier. Object modules are signicantly cheaper to create than ordinary modules, as they do not import the built-in Prolog
predicates.
The module will contain predicates implementing an object declaration, the method code, imported
methods and parameter transfer predicates. These predicates will be described in detail below,
using the notational convention that variable names in italics are syntactic variables that will be
replaced by something else in the translation process.
32.9.1 The Inheritance Mechanism
The inheritance mechanism is based on the importation mechanism of the Prolog module system.
When an object is created, whether loaded from le or at runtime by new/(1-2), the method
predicates (i.e. predicates implementing the methods) visible in the immediate supers are collected.
After subtracting from this set the method predicates which are locally dened, and those that are
specied in the don't-inherit-list , the resulting set is made visible in the module of the inheriting
object by means of importation. This implies that inherited methods are shared, expect dynamic
methods.
Dynamic methods are inherited in a similar way with the big dierence that they are not imported
but copied. Even dynamic declarations (methods without clauses) are inherited.
Inheritance from dynamic objects diers in one aspect: Static predicates visible in a dynamic object
are not imported directly from the dynamic object but from the static object from where it was
imported to the dynamic object. This makes an inheriting object independent of any dynamic
ancestor object after its creation.
32.9.2 Object Attributes
Attributes are based on an ecient term storage associated to modules. The attributes for an object
is collected from its ancestors and itself at compile time and used for initialization at load time.
The methods for accessing attributes, get/1 and set/1, are inlined to primitive calls whenever
possible. They should hence not be redened.
346
SICStus Prolog
32.9.3 Object Instances
Instances are dierent from other objects in that they do not inherit. Instead they share the
predicate name space with its class object. They do however have their own attributes. At creation,
an instance gets a copy of its class objects attributes. The reserved attribute '$class'/1, which
is present in any object, is used for an instance to hold its class object identier. The purpose of
this is mainly to store the parameters of the class object when the instance is created.
32.9.4 The Object Declaration
The object declaration is only used by certain meta-programming operations. It consists of a fact
'$so_type'(Object, Type ).
where Object is the object-identier, and Type is either static or dynamic. If the type is static,
the other generated predicates will be static, otherwise they will be dynamic.
32.9.5 The Method Code
Each method clause translates to a Prolog clause with two extra arguments: Self (a variable) and
Myself. The latter argument is needed to cater for passing object parameters to the method body
which is desribed further in next section.
The method body is translated to a Prolog-clause body as follows. The code is traversed, and
the goals are transformed according to the following transformation patterns and rules. In the
transformation rules, the notation Msg (X,Y ) denotes the term produced by augmenting Msg by
the two arguments X and Y :
Goal
where Goal is a variable, is translated to
objects:call_from_body(Goal,Self,Myself,Src ) where Src is the source module.
objects:call_from_body/4 will meta-interpret Goal at runtime.
:: Msg
is translated to Myself :Msg (Myself,Myself ) if Msg is a non variable. Otherwise, it
is translated to objects:call_object(Myself, Msg, Myself ).
<: Msg
is translated to Myself :Msg (Self,Myself ) if Msg is a non variable. Otherwise, it is
translated to objects:call_object(Myself, Msg, Self ).
super :: Msg is translated to
objects:call_super_exp(Myself,Msg (Super,Myself ),Super ) if Msg is a non variable. call_super_exp/3 searches the supers of Myself. Super is bound to the super
object where the method is found. If Msg is a variable, the goal is translated to
objects:call_super(Myself,Msg,Super,Super ) which expands Msg and performs
otherwise the same actions as call_super_exp/3.
Chapter 32: Prolog Objects
347
Msg
is translated to objects:call_super_exp(Myself,Msg (Self,Myself ),Super ) if Msg
is a non variable. call_super_exp/3 searches the supers of Myself. Super is bound to
the super object where the method is found. If Msg is a variable, the goal is translated
to objects:call_super(Myself,Msg,Self,Super ) which expands Msg and performs
otherwise the same actions as call_super_exp/3.
Obj :: Msg
* If Msg is non-variable, this is translated to Obj:Msg (Obj,Obj).
* Otherwise, it is translated to objects:call_object(Obj,Msg,Obj).
Obj <: Msg
* If Msg is non-variable, this is translated to Obj:Msg (Self,Obj).
* Otherwise, if Msg is a non-variable, it is translated to functor(Obj,O,_),
O :Msg (Self,Obj).
* Otherwise, it is translated to objects:call_object(Obj,Msg,Self ).
self <: Msg
self :: Msg
Msg
are all translated like Self :: Msg.
Module :Goal
is translated to Module :Goal.
:Goal
is translated to Src :Goal where Src is the source module.
super <:
To illustrate the expansion, consider the object
history_point module:
history_point
directives, all executed in the
:-objects:create_object(history_point,
[point-[]],
[attributes/3,display/3,move/4,new/4,print_history/3,super/4],
[],
[y(0),x(0),history([])],
tree(history_point,[tree(point,[tree(object,[])])])).
history_point:super(point, [], _, history_point).
history_point:attributes([history([])], _, _).
history_point:display(A, B, _) :objects:call_super_exp(history_point, display(A,B,C), C),
history_point:print_history(A, B, history_point).
history_point:'$so_type'(history_point, static).
history_point:move(A, B, C, _) :objects:call_super_exp(history_point, move(A,B,C,E), E),
prolog:'$get_module_data'(C, history, D),
prolog:'$set_module_data'(C, history, [(A,B)|D]).
348
SICStus Prolog
history_point:print_history(A, B, _) :prolog:'$get_module_data'(B, history, C),
A:format('with location history ~w~n', [C], A, A).
history_point:new(A, xy(D,E), B, _) :objects:call_super_exp(history_point, new(A,xy(D,E),B,C), C),
prolog:'$set_module_data'(A, history, [(D,E)]).
The directive create_object/6 creates the object, performs the inheritance by importation, and
initializes attributes. The last argument is a tree representing the ancestor hierarchy during compilation. It is used to check that the load time and compile time environments are consistent.
32.9.6 Parameter Transfer
As can be seen in the expanded methods above, the second additional argument is simply ignored
if the object has no parameter. In contrast regard the following objects:
ellipse(RX,RY,Color) :: {
color(Color) &
area(A) ::(A is RX*RY*3.14159265)
}.
circle(R,Color) :: {
super(ellipse(R,R,Color))
}.
red_circle(R) :: {
super(circle(R,red))
}.
... and their expansions:
ellipse(_, _, _):'$so_type'(ellipse(_,_,_), static).
ellipse(_, _, _):area(A, _, B) :B:'$fix_param'(ellipse(C,D,_), B),
user:(A is C*D*3.14159265).
ellipse(_, _, _):color(A, _, B) :B:'$fix_param'(ellipse(_,_,A), B).
ellipse(_, _, _):'$fix_param'(ellipse(B,C,D), A) :objects:object_class(ellipse(B,C,D), A).
circle(_, _):'$so_type'(circle(_,_), static).
Chapter 32: Prolog Objects
349
circle(_, _):super(ellipse(A,A,B), [], _, circle(A,B)).
circle(_, _):'$fix_param'(circle(B,C), A)
objects:object_class(circle(B,C),
circle(_, _):'$fix_param'(ellipse(B,B,C),
objects:object_class(circle(B,C),
:A).
A) :A).
red_circle(_):'$so_type'(red_circle(_), static).
red_circle(_):super(circle(A,red), [], _, red_circle(A)).
red_circle(_):'$fix_param'(red_circle(B), A) :objects:object_class(red_circle(B), A).
red_circle(_):'$fix_param'(circle(B,red), A) :objects:object_class(red_circle(B), A).
red_circle(_):'$fix_param'(ellipse(B,B,red), A) :objects:object_class(red_circle(B), A).
The second additional argument contains the receiver of a method call. If the method makes use
of any parameter of the object where it is dened, it places a call to the reserved predicate $fix_
param/2 in the module of the receiver. The purpose of this call is to bind the parameters used in
the method to appropriate values given by the receiver. The receiver may be the object where the
method is dened or any of its subs. In order to service these calls, a clause of $fix_param/2 is
generated for each ancestor having parameters. Such a clause may be regarded as the collapsed
chain of super/(1-2) denitions leading up to the ancestor.
The call objects:object_class(Class,Object) serves to pick up the
Object is an instance, otherwise Class is unied with Object.
'$class'/1
attribute if
The following trace illustrates how parameters are transfered:
| ?- red_circle(2.5)::area(A).
1 1 Call: red_circle(2.5)::area(_A) ?
2 2 Call: ellipse(_,_,_):area(_A,red_circle(2.5),red_circle(2.5)) ?
3 3 Call: red_circle(_):$fix_param(ellipse(_B,_,_),red_circle(2.5)) ?
4 4 Call: objects:object_class(red_circle(_B),red_circle(2.5)) ?
4 4 Exit: objects:object_class(red_circle(2.5),red_circle(2.5)) ?
3 3 Exit: red_circle(_):$fix_param(ellipse(2.5,2.5,red),red_circle(2.5)) ?
5 3 Call: _A is 2.5*2.5*3.14159265 ?
5 3 Exit: 19.6349540625 is 2.5*2.5*3.14159265 ?
2 2 Exit: ellipse(_,_,_):area(19.6349540625,red_circle(2.5),red_circle(2.5)) ?
1 1 Exit: red_circle(2.5)::area(19.6349540625) ?
A = 19.6349540625 ?
350
SICStus Prolog
32.10 Examples
32.10.1 Classication of Birds
This example illustrates how Prolog object can be used in classication of certain concepts. This
style is common in expert system application for describing its domain.
animal :: {
super(object) &
relative_size(S) :size(Obj_size),
super(Obj_prototype),
Obj_prototype :: size(Prototype_size),
:(S is Obj_size/Prototype_size * 100)
}.
bird :: {
super(animal) &
moving_method(fly) &
active_at(daylight)
}.
albatross :: {
super(bird) &
color(black_and_white) &
size(115)
}.
kiwi :: {
super(bird) &
moving_method(walk) &
active_at(night) &
size(40) &
color(brown)
}.
albert :: {
super(albatross) &
size(120)
}.
ross :: {
super(albatross) &
size(40)
}.
| ?- ross :: relative_size(R).
Chapter 32: Prolog Objects
351
R = 34.78
32.10.2 Inheritance and Delegation
The following example illustrates a number of concepts. Firstly, how to use Prolog Objects for
dening traditional classes a la Smalltalk, or other traditional object oriented languages. Secondly,
how to create instances of these classes. Finally, how to access instance variables.
The concept of instance variables is readily available as the variables belonging to the instances
created dynamically and not to the class of the instances. For example, each instance of the class
point will have two instance variables, x and y, represented by the attributes x/1 and y/1. The
traditional class variables are easily available by accessing the same attributes in the associated
class.
Another issue is the pattern used to create new instances. For example, to create an instance of
the class history_point, the following code is used:
new(Instance, xy(IX,IY)) :super <: new(Instance, xy(IX,IY)),
Instance :: set(history([(IX,IY)])) &
Note that the delegation of new/2 to super is necessary in order to create an object whose super
is history_point and not point.
The example shows how delegation can be eective as a tool for exible sharing of concepts in
multiple inheritance. Four prototypes are dened: point, history_point, bounded_point, and
bh_point. The latter is a bounded history point.
An instance of the point class is a point that moves in 2-D space and that can be displayed. An
instance of the history_point class is similar to an instance of the point class but also keeps a
history of all the moves made so far. An instance of bounded_point is similar to an instance of
point but moves only in a region of the 2-D space. Finally an instance of bh_point inherits most
of the features of a bounded_point and a history_point.
The default inheritance does not work for the methods display/1 and move/2 in bh_point. Inheritance by delegating messages to both supers of bh_point results in redundant actions, (moving
and displaying the point twice). Selective delegation solves the problem. Taken from [Elshiewy 90].
point :: {
super(object) &
attributes([x(0),y(0)]) &
xy(X, Y) :- get(x(X)), get(y(Y)) &
new(Instance, xy(IX,IY)) :super <: instance(Instance),
Instance :: set(x(IX)),
352
SICStus Prolog
Instance :: set(y(IY)) &
location((X,Y)) :- <: xy(X,Y) &
move_horizontal(X) :set(x(X)) &
move_vertical(Y) :set(y(Y)) &
move(X, Y) :<: move_horizontal(X),
<: move_vertical(Y) &
display(Terminal) :<: xy(X, Y),
Terminal :: format('point at (~d,~d)~n',[X,Y])
}.
history_point :: {
super(point) &
attributes([history([])]) &
new(Instance, xy(IX,IY)) :super <: new(Instance, xy(IX,IY)),
Instance :: set(history([(IX,IY)])) &
move(X, Y) :super <: move(X, Y),
get(history(History)),
set(history([(X,Y)|History])) &
display(Terminal) :super <: display(Terminal),
<: print_history(Terminal) &
print_history(Terminal) :get(history(History)),
Terminal :: format('with location history ~w~n',
[History])
}.
bounded_point :: {
super(point) &
attributes([bounds(0,0,0,0)]) &
Chapter 32: Prolog Objects
new(Instance, Coords, Bounds) :super <: new(Instance, Coords),
Instance :: set_bounds(Bounds) &
set_bounds(Bounds) :set(Bounds) &
move(X, Y) :<: bound_constraint(X, Y), !,
super <: move(X, Y) &
move(_, _) &
bound_constraint(X, Y) :get(bounds(X0, X1, Y0, Y1)),
:(X >= X0),
:(X =< X1),
:(Y >= Y0),
:(Y =< Y1) &
display(Terminal) :super <: display(Terminal),
<: print_bounds(Terminal) &
print_bounds(Terminal) :get(bounds(X0, X1, Y0, Y1)),
Terminal :: format('xbounds=(~d,~d), \c
ybounds=(~d,~d)~n',
[X0,X1,Y0,Y1])
}.
bh_point :: {
super(history_point) &
super(bounded_point) &
new(Instance, Coords, Bounds) :history_point <: new(Instance, Coords),
Instance :: set_bounds(Bounds) &
move(X, Y) :bounded_point <: bound_constraint(X, Y), !,
history_point <: move(X, Y) &
move(_, _) &
display(Terminal) :bounded_point <: display(Terminal),
history_point <: print_history(Terminal)
}.
353
354
SICStus Prolog
tty :: {
format(X, Y) :- :format(X, Y)
}.
point at (8,12)
xbounds=(5,15), ybounds=(5,15)
with location history [(8,12),(9,11)]
32.10.3 Prolog++ programs
Prolog++ is a product by LPA Associates for object-oriented programming extensions of LPA Prolog. Most Prolog++ programs can be easily converted into Prolog Objects programs. The following
is a translation of a program for fault diagnosis in LPA's Prolog++ manual, page 83. The program
illustrates a top-down diagnosis method starting from general objects to more specic objects. The
problem is fault diagnosis for car maintenance. The objects have the following structure:
- faults
- electrical
|
- lights
|
- starting
|
- starter_motor
|
- sparking
|
- plugs
|
- distributer
- fuel_system
- mechanical
The general diagnosis method is dened in the object faults, whereas the cause-eect relationships
are dened in the specic objects e.g. the object distributor.
This program heavily uses the
original formulation.
sub/1
method. We have tried to be as close as possible to the
faults :: {
super(utility) &
dynamic(told/2) &
/* no fault is the default */
fault(_, _) :- :fail &
findall :<: restart,
:: sub(Sub),
Sub :: find(Where, Fault),
<: print(Where, Fault),
:fail &
Chapter 32: Prolog Objects
findall &
print(Where, Fault) ::writeseqnl('Location
: ', [Where]),
:writeseqnl('Possible Fault : ', [Fault]),
:nl &
find(Where, Fault) :self(Where),
fault(FaultNum, Fault),
\+ (effect(FaultNum, S),
contrary(S, S1),
exhibited(S1)
),
\+ (effect(FaultNum, SymptomNum),
\+ exhibited(SymptomNum)) &
find(Where, Fault) :sub(Sub),
Sub :: find(Where, Fault) &
exhibited(S) ::: told(S, R), !,
R = yes &
exhibited(S) :symptom(S,Text),
(
:yesno([Text]) -> R = yes
;
R = no
),
:: asserta(told(S,R)),
R = yes &
restart ::: retractall(told(_,_))
}.
electrical :: {
super(faults)
}.
fuel_system :: {
super(faults)
}.
mechanical :: {
super(faults)
}.
355
356
SICStus Prolog
lights :: {
super(electrical)
}.
sparking :: {
super(electrical)
}.
starting :: {
super(electrical)
}.
starter_motor :: {
super(electrical)
}.
plugs :: {
super(sparking)
}.
engine :: {
super(mechanical)
}.
cylinders :: {
super(engine)
}.
distributor :: {
super(sparking) &
/* faults */
fault('F1001', 'Condensation in distributor cap') &
fault('F1002', 'Faulty distributor arm') &
fault('F1003', 'Worn distributor brushes') &
/* symptoms */
symptom('S1001',
symptom('S1002',
symptom('S1003',
symptom('S1004',
'Starter turns, but engine does not fire') &
'Engine has difficulty starting') &
'Engine cuts out shortly after starting') &
'Engine cuts out at speed') &
/* symptoms contrary to each other */
contrary('S1002', 'S1001') &
contrary('S1003', 'S1001') &
/* causal-effect relationship */
Chapter 32: Prolog Objects
effect('F1001',
effect('F1002',
effect('F1002',
effect('F1003',
effect('F1003',
357
'S1001')
'S1001')
'S1004')
'S1002')
'S1003')
&
&
&
&
}.
yesno(Value) :- write(Value), nl, read(yes).
writeseqnl(Prompt, L) :- write(Prompt), write_seq(L).
write_seq([]).
write_seq([X|L]) :- write(X), write(' '), write_seq(L), nl.
faults :- faults :: findall.
| ?- faults.
[Starter turns, but engine does not fire]
|: yes.
Location
: distributor
Possible Fault : Condensation in distributor cap
[Engine cuts out at speed]
|: yes.
Location
: distributor
Possible Fault : Faulty distributor arm
yes
| ?- faults.
[Starter turns, but engine does not fire]
|: no.
[Engine has difficulty starting]
|: yes.
[Engine cuts out shortly after starting]
|: yes.
Location
: distributor
Possible Fault : Worn distributor brushes
358
SICStus Prolog
Chapter 33: Generalized Horn Clause Language
359
33 Generalized Horn Clause Language
GCLA is a logic programming language (specication tool) that is based on a generalization of
Prolog. This generalization is unusual in that it takes a quite dierent view of the meaning of a
logic program|a \denitional" view rather than the traditional logical view.
GCLA has a number of noteworthy properties, for instance the possibility to do hypothetical
reasoning and non-monotonic reasoning. This makes implementation of reasoning in knowledgebased systems more direct in GCLA than in many other formalisms. GCLA is also general enough
to incorporate functional programming as a special case.
A GCLA program is partitioned into two parts; a declarative part and a control part that species
the inference rules. For an introduction to GCLA and its properties see [Kreuger 91]. For methodology and programming techniques see [Aronsson 91]. Further reading can be found in [Aronsson
93] and in [Falkman & Torgersson 94].
The
GCLA
package
is
documented
in
its
own
User's
Manual,
located in `library/gcla/doc/gcla.tex' (also available as technical report SICS T91:21A). To
load the package, enter the query:
| ?- use_module(library('gcla/gcla')).
360
SICStus Prolog
Chapter 34: Tcl/Tk Interface
361
34 Tcl/Tk Interface
The tcltk library package is a bidirectional interface to the Tcl (pronounced Tickle) language and
the Tk toolkit. Tcl is an interpreted scripting language with many extension packages, in particular
the graphical interface toolkit Tk.
You can read about Tcl/Tk in [Ousterhout 94] or in various articles. The articles and the Tcl/Tk
system can be found by anonymous FTP, see the Release Notes for up-to-date information. This
le also contains information on how to include Tcl/Tk extensions.
To load the package, enter the query:
| ?- use_module(library(tcltk)).
34.1 Prolog to Tcl
To use Tcl, you must create a Tcl interpreter object and send commands to it. The following
predicate executes a single Tcl command:
test_command(Command, Result) :tcl_new(Interp),
tcl_eval(Interp, Command, Result),
tcl_delete(Interp).
The Tcl command and its arguments is specied in Command (see Section 34.1.1 [Command
Format], page 362), and the result will be returned as a string (list of character codes) in Result.
These are the predicates to use Tcl/Tk from Prolog:
tcl_new(-TclInterpreter )
Creates a new interpreter, initializes it and returns a reference to it. If you don't have
a standard Tcl installation you can tell Tcl where to look for the initialization les by
setting the environment variable TCL_LIBRARY.
tcl_delete(+TclInterpreter )
Deletes an interpreter and the memory used by it.
tcl_eval(+TclInterpreter, +Command, -Result)
Lets TclInterpreter interpret the command specied by Command. The result will be
stored as a string (list of character codes) in Result.
tcl_event(+TclInterpreter, +Command, -Events )
This call is similar to tcl_eval/3. Events is a list of terms stored from Tcl by the
prolog_event command (see Section 34.2 [Tcl to Prolog], page 363).
362
SICStus Prolog
34.1.1 Command Format
A Tcl command is specied as follows:
Command
--> Atom
{ other than [] }
| Number
| chars(PrologString )
| write(Term)
| format(Fmt,Args )
| dq(Command )
| br(Command )
| sqb(Command )
| min(Command )
| ListOfCommands
ListOfCommands
ListExpr
--> []
-->
| [ListExpr ]
Command
| Command,ListExpr
| Command |ListOfCommands
where
Atom
Number denote their printed representations.
chars(PrologString )
denotes the string represented by PrologString (a list of character codes).
write(Term)
denotes the string that is printed by the corresponding built-in predicate.
format(Fmt,Args )
denotes the string that is printed by the corresponding built-in predicate.
dq(Command )
denotes the string specied by Command, enclosed in double quotes.
br(Command )
denotes the string specied by Command, enclosed in braces.
sqb(Command )
denotes the string specied by Command, enclosed in square brackets.
min(Command )
denotes the string specied by Command, immediately preceded by a hyphen.
ListOfCommands
denotes the strings denoted by each element, separated by spaces.
Examples of command specications and corresponding translations:
Chapter 34: Tcl/Tk Interface
363
'set x 32'
==> set x 32
[set,x,br([a,b,c])]
==> set x {a b c}
['.panel.value_info.name',configure,min(text),br(write('$display'/1))]
==> .panel.value_info.name configure -text {$display/1}
34.2 Tcl to Prolog
There are two new Tcl commands dened in this library:
prolog
Goal
Goal is a string containing the printed representation of a Prolog goal. The goal will
be called in the user module unless it is prexed with another module name. The call
is always deterministic. The result of the call is returned as:
"1"
if execution succeeds. The value of any of the variables in the goal that is
bound to a term will be returned to Tcl in the array prolog_variables
with the variable name as index.
The term is converted to Tcl using the same conversion as used for Tcl
commands (see Section 34.1.1 [Command Format], page 362).
"0"
if the execution fails.
Tcl exception
if a Prolog exception is raised. The error message will be "Prolog
Exception: " appended with a string representation of the Prolog exception.
An example:
test_callback(Result) :tcl_new(Interp),
tcl_eval(Interp,
'if {[prolog "foo(X,Y,Z)"] == 1} \\
{lappend res $prolog_variables(X) \\
$prolog_variables(Y) \\
$prolog_variables(Z)}',
Result),
tcl_delete(Interp).
foo(1, bar, [a,b,c]).
The query
| ?- test_callback(Result).
will succeed, binding Result to:
"1 bar {a b c}"
364
SICStus Prolog
Terms...
Terms are strings that contain the printed representation of Prolog terms. These
are stored in a queue and retrieved as Prolog terms by tcl_event/3 or tk_next_
event/(2,3).
prolog_event
An example:
test_event(Event) :tcl_new(Interp),
tcl_event(Interp, [prolog_event,dq(write(zap(42)))], Event),
tcl_delete(Interp).
The query
| ?- test_event(Event).
will succeed, binding Event to the list [zap(42)]
34.3 Tk
The following example is a simple \Hello, world" program:
tk_hello_world :tk_new([], Interp),
tcl_eval(Interp,
'button .b -text "Hello" -command "destroy ."', _),
tcl_eval(Interp, 'pack .b', _),
tk_main_loop,
tcl_delete(Interp).
The program brings up a tiny window with a button labeled \Hello" and is terminated by pressing
the button.
These are the predicates to use Tk from Prolog:
tk_new(+Options,
-TclInterpreter )
Performs basic Tcl and Tk initialization and creates the main window of a Tk application. If you don't have a standard Tk installation you can tell Tk where to look for
the initialization les by setting the environment variable TK_LIBRARY. Options is a
list of optional elements according to:
top_level_events
Event handling will be installed in the Prolog top-level loop. This means
that Tk windows and the Prolog prompt can be simultaneously active.
name(+ApplicationName )
Sets the Tk application name. The application name will be displayed in
the main window and is also used for communicating between applications
in Tk. Default name is an empty string.
Chapter 34: Tcl/Tk Interface
365
display(+Display )
Gives the name of the screen on which to create the main window. Default
is normally determined by the DISPLAY environment variable.
tk_main_window(+TclInterpreter, -TkWindow )
Gets a reference to the main window of the application using this interpreter.
tk_make_window_exist(+TkWindow )
This will force a window to be mapped to the screen, i.e. it will force it to show up
immediately. Normally, mapping is delayed as long as possible.
tk_destroy_window(+TkWindow )
Destroys a window or widget.
tk_num_main_windows(-NumberOfWindows )
Returns the number of main windows currently in use.
tk_do_one_event
tk_do_one_event(+ListOrBitmask )
Passes control to Tk for handling a single event. This predicate may be called to give
Tk time for handling the windows. You can specify what events to handle in a List of
event ags. Alternatively a Bitmask as specied in the Tcl/Tk documentation can be
used but should be avoided for portability between Tcl/Tk versions. The ags you can
have in List are:
tk_dont_wait
Don't wait for new events, process only events that are ready.
tk_x_events
tk_window_events
Process window events.
tk_file_events
Process le events.
tk_timer_events
Process timer events.
tk_idle_events
Process Tk DoWhenIdle callbacks.
tk_all_events
Process any event.
tk_do_one_event/0 is equivalent to tk_do_one_event/1 with all ags set. If the
tk_dont_wait ag is set and there is no event to handle, the call will fail.
It is straight-forward to dene a predicate which handles all events in the queue and
then returns:
tk_do_all_events :tk_do_one_event, !,
tk_do_all_events.
tk_do_all_events.
tk_do_one_event/(0,1)
TclDoOneEvent() in later
is an interface to the C-function
Tcl/Tk versions).
TkDoOneEvent()
(or
366
SICStus Prolog
tk_next_event(+TclInterpreter, -Event)
tk_next_event(+ListOrBitmask, +TclInterpreter,
-Event)
Processes events until there is at least one Prolog event associated with TclInterpreter.
Event is the term corresponding to the head of a queue of events (strings representing
terms) stored from Tcl with the prolog_event command (see Section 34.2 [Tcl to
Prolog], page 363). Only events associated with TclInterpreter are returned; other
events remain in the queue. If there are no windows left the term [] will be returned.
This predicate does not correspond directly to any Tcl/Tk C function.
tk_main_loop
Passes control to Tk until all windows are gone.
There are basically two alternatives for invoking Prolog actions on user events. The rst is to
directly call Prolog by means of the Tcl command prolog. Tcl/Tk must then be repeatedly
invoked, either by calling tk_main_loop/0 or using the option top_level_events, possibly in
conjunction with calling tk_do_one_event/(0,1) in lengthy computations.
The second alternative is using the Tcl command prolog_event. The user program then, by calling
tk_next_event/(2,3), passes control to Tcl/Tk until one or more invocations of prolog_event
has occurred. The Prolog event determines the Prolog action to take. This approach has the
advantage that context variables might be passed around in the event loop.
Chapter 35: The Gauge Proling Tool
367
35 The Gauge Proling Tool
The Gauge library package is a graphical interface to the Sicstus built-in predicates profile_
data/4 and profile_reset/1. See Section 7.15 [Proling], page 121, for more information about
execution proling. The interface is based on Tcl/Tk (see Chapter 34 [TclTk], page 361). Gauge
loads Tcl/Tk if it is not previously loaded.
To use the gauge package, enter the query
| ?- use_module(library(gauge)).
view(:Spec )
Creates a graphical user interface for viewing the prole data for the predicates covered
by Spec, which has the same form as for spy/1 (see Section 6.3 [Spy-Point], page 62).
For example, the call view([user:_,m2:_]), will bring up the graphical user interface
on the predicates contained in the modules user and m2. When the display rst comes
up it is blank except for the control panel.
The graphical interface is pretty much self explanatory. The on-line help function gives
all necessary information.
368
SICStus Prolog
Chapter 36: I/O on Lists of Character Codes
369
36 I/O on Lists of Character Codes
This package denes I/O predicates that read from, or write to, a list of character codes (a string).
There are also predicates to open a stream referring to a list of character codes. The stream may
be used with general Stream I/O predicates.
To load the package, enter the query
| ?- use_module(library(charsio)).
format_to_chars(+Format, +Arguments,
format_to_chars(+Format, +Arguments,
-Chars )
?S0, ?S )
Prints Arguments into a list of character codes using format/3 (see Section 7.1.3 [Term
I/O], page 80). Chars is unied with the list, alternatively S0 and S are unied with
the head and tail of the list, respectively.
write_to_chars(+Term, -Chars )
write_to_chars(+Term, ?S0, ?S )
A specialized format_to_chars/(3-4). Writes Term into a list of character codes
using write/2 (see Section 7.1.3 [Term I/O], page 80). Chars is unied with the list,
alternatively S0 and S are unied with the head and tail of the list, respectively.
atom_to_chars(+Atom, -Chars )
atom_to_chars(+Atom, ?S0, ?S )
A specialized format_to_chars/(3-4). Converts Atom to the list of characters comprising its name. Chars is unied with the list, alternatively S0 and S are unied with
the head and tail of the list, respectively.
number_to_chars(+Number, -Chars )
number_to_chars(+Number, ?S0, ?S )
A specialized format_to_chars/(3-4). Converts Number to the list of characters
comprising its name. Chars is unied with the list, alternatively S0 and S are unied
with the head and tail of the list, respectively.
read_from_chars(+Chars, -Term)
Reads Term from Chars using read/2. The Chars must, as usual, be terminated by a
full-stop, i.e. a ., possibly followed by layout-text.
open_chars_stream(+Chars, -Stream)
Stream is opened as an input stream to an existing list of character codes. The stream
may be read with the Stream I/O predicates and must be closed using close/1. The
list is copied to an internal buer when the stream is opened and must therefore be a
ground list of character codes at that point.
with_output_to_chars(+Goal, -Chars )
with_output_to_chars(+Goal, ?S0, ?S )
370
SICStus Prolog
with_output_to_chars(+Goal,
-Stream, ?S0, ?S )
Goal is called with the current_output stream set to a new stream. This stream
writes to an internal buer which is, after the successful execution of Goal, converted
to a list of character codes. Chars is unied with the list, alternatively S0 and S are
unied with the head and tail of the list, respectively. with_output_to_chars/4 also
passes the stream in the Stream argument. It can be used only by Goal for writing.
Chapter 37: Jasper
371
37 Jasper
The Jasper library module provides functionality for accessing the Java VM from Prolog. The
current functionality is limited to calling methods using the standard foreign language interface.
See Chapter 9 [Mixing Java and Prolog], page 167. However, this will most likely be extended in
the future.
The library does not yet dene any predicates. However, it must be loaded to make sure that the
JVM (the Java Virtual Machine) is loaded correctly and that the interface code has access to all
relevant references to the JVM. It is loaded by the query
| ?- use_module(library(jasper)).
This will dynamically load the JVM into memory and initialize the JNI Invocation API.
There is also an directory of examples of how to use Jasper:
library('jasper/examples').
372
SICStus Prolog
Chapter 38: Glue Code Generator
373
38 Glue Code Generator
This chapter describes utilities useful for generating glue code for the Foreign Language Interface when building statically linked Runtime Systems or Development Systems (see Section 8.2.5
[Interface Predicates], page 133).
This library module can be loaded by the query
| ?- use_module(library(flinkage)).
and refers to the following shared object les or DLLs:
runtime kernel
The SICStus runtime kernel, usually `$SP_PATH/../libsprt37.so' under UNIX, or
`%SP_PATH%\..\sprt37.dll' under Windows, and
development kernel
The SICStus development kernel, usually `$SP_PATH/../libspds37.so' under UNIX,
or `%SP_PATH%\..\spds37.dll' under Windows.
The following predicate can be used to generate a glue code le `flinkage.c' le out of any old-style
foreign_file/2 and foreign/(2,3) declarations found in a set of Prolog les for use in a statically
linked executable. Note: this is provided for backwards compatibility only. We recommend using
foreign resources instead, which can be dynamically linked:
generate_flinkage(+Files )
Files are loaded as by use_module/1. Calls to load_foreign_files/2 are intercepted
by means of the goal expansion mechanism. The le `flinkage.c' is generated out of
the declarations found. Finally, compile and link the `flinkage.c' le with the user
code, libraries and the runtime and optionally the development kernel.
The following predicates, available in Development Systems only, provide alternative methods to
building executables or linked foreign resources compared to the methods described in Section 8.8.3
[Creating the Executable], page 154, Section 8.9 [Development Systems], page 156 and Section 8.2.7
[Creating the Linked Foreign Resource], page 135. If the default method doesn't work as expected
for some reason or you wish to use an Integrated Development Environment etc., the following
outlines the necessary steps. To build an executable:
First, decide which foreign resources are to be statically linked and create a resource table le as
follows:
prepare_resource_table(+ResourceNames,+CFile )
where +ResourceNames is a list of the resource
write the generated code.
names and +CFile is the le where to
The next step is to create linked foreign resources from the foreign resources which are to be
statically linked. Produce, for each foreign resource, a glue code le using:
374
SICStus Prolog
prepare_foreign_resource(+ResourceName,+SourceFile,+CFile )
where ResourceName is the name of the resource, SourceFile is the Prolog source le
containing the resource conversion declarations and CFile is the le where to write the
generated code.
Finally, compile and link the glue code les with the user code, libraries and the runtime and
optionally the development kernel. Further information (platform specic) is provided with the
distribution.
To build a dynamic linked foreign resource:
Produce a glue code le using prepare_foreign_resource/3. Check the provided platform specic
notes for compiler options needed and compile the glue code le and the user code les. Likewise
check the notes on how a dynamic linked foreign resource is implemented on this platform and link
the object les to an appropriate object.
Chapter 39: Timeout Predicate
375
39 Timeout Predicate
The timeout package, which may be loaded by the query
| ?- use_module(library(timeout)).
contains the predicate:
time_out(:Goal, +Time,
?Result)
The Goal is executed as if by call/1. If computing any solution takes more than Time
milliseconds, process virtual time, the goal will fail and Result is unied with the atom
time_out. If the goal succeeds within the specied time Result is unied with the atom
success. Time must be a number between (not including) 0 and 2147483647.
time_out/3 is implemented by
raising and handling time_out exceptions, so any exception handler
in the scope of Goal must be prepared to pass on time_out exceptions. The following incorrect
example shows what can happen otherwise:
| ?- time_out(on_exception(Q,(repeat,false),true), 1000, Res).
Q = time_out,
Res = success
376
SICStus Prolog
Chapter 40: Cross Reference Producer
377
40 Cross Reference Producer
This package provides a cross reference producer is a useful tool for debugging and program analysis.
It can be used to produce the call graph of a program, and to produce a list of predicates that are
used but not dened or vice versa. The graph processing utilities described in previous chapters
may be used to analyze the call graphs.
To load the package, enter the query
| ?- use_module(library(xref)).
call_graph(:Files,
-Graph)
Graph is the call graph (see Chapter 23 [UGraphs], page 221) of the predicates in Files.
The vertices of Graph are terms of the following types:
def
There is an edge from def to every predicate that is explicitly dened,
declared, imported, or implicitly dened in a foreign/(2-3) fact.
use
There is an edge from use to every predicate that is used in a directive, exported, or declared as public. If a \hook" predicate such as
user:portray/1 is dened, there is an edge from use to it too.
Module :Name /Arity
Denotes a predicate occurring in one of the Files. There is an edge from
predicate P to predicate Q if P calls Q and Q is not a built-in predicate.
xref(:Files )
Files is a single le or a list of les, as accepted by consult/1 and friends. The
predicate prints on the current output stream a list of predicates which are reachable
from use in the call graph but not dened, followed by a list of predicates which are
dened but not reachable from use.
There may be references not found by xref because of the dynamic nature of the Prolog language.
Note that xref does not consider a predicate used unless it is reachable from a directive, an export
list, or a public declaration. In non-module les, it is therefore recommended that the entry
point predicates be declared as public. Predicates reached by meta-calls only may also have to be
declared public to be considered used.
If a module le (le containing a module declaration) is encountered, the module specied is used
for that le and subsequent les up to the next module le. This enables packages consisting of
several les, which are to be loaded in the same module, to be correctly treated. The type-in
module is the default module.
378
SICStus Prolog
Summary of Built-In Predicates
Summary of Built-In Predicates
Commits to any choices taken in the current predicate.
(+P,+Q ) P and Q.
(Head --> Body )
Not a built-in predicate; reserved syntax for grammar rules.
(+P -> +Q ; +R)
If P then Q else R, using rst solution of P only.
(+P -> +Q )
If P then Q else false, using rst solution of P only.
!
[]
[:File |+Files ]
Updates the program with interpreted clauses from File and Files.
(:- Command )
Not a built-in predicate; reserved syntax for commands and declarations.
(?- Query )
Not a built-in predicate; reserved syntax for queries.
(Head :- Body )
Not a built-in predicate; reserved syntax for clauses.
(+P ;+Q ) P or Q.
?X = ?Y The terms X and Y are unied.
+Term =.. ?List
?Term =.. +List
The functor and arguments of the term Term comprise the list List.
+X =:= +Y
X is numerically equal to Y.
?Term1 == ?Term2
The terms Term1 and Term2 are strictly identical.
+X =\= +Y
X is not numerically equal to Y.
+X =< +Y X is less than or equal to Y.
+X > +Y
X is greater than Y.
+X >= +Y X is greater than or equal to Y.
?X ^ :P Executes the procedure call P.
\+ +P
Goal P is not provable.
?Term1 \== ?Term2
The terms Term1 and Term2 are not strictly identical.
379
380
SICStus Prolog
+X < +Y
X is less than Y.
?Term1 @=< ?Term2
The term Term1 precedes or is identical to the term Term2 in the standard order.
?Term1 @> ?Term2
The term Term1 follows the term Term2 in the standard order.
?Term1 @>= ?Term2
The term Term1 follows or is identical to the term Term2 in the standard order.
?Term1 @< ?Term2
The term Term1 precedes the term Term2 in the standard order.
?=(?X,?Y )
X and Y are either syntactically identical or syntactically non-uniable.
abolish(:Preds )
Makes the predicate(s) specied by Preds undened.
abolish(:Atom,+Arity )
Makes the predicate specied by Atom/Arity undened.
abort
Aborts execution of the current directive (returns to C in recursive calls to Prolog from
C).
absolute_file_name(+RelativeName,?AbsoluteName )
AbsoluteName is the full pathname of RelativeName.
arg(+ArgNo,+Term,?Arg )
The argument ArgNo of the term Term is Arg.
assert(:Clause )
assert(:Clause,-Ref )
Asserts clause Clause with unique identier Ref.
asserta(:Clause )
asserta(:Clause,-Ref )
Asserts Clause as rst clause with unique identier Ref.
assertz(:Clause )
assertz(:Clause,-Ref )
Asserts Clause as last clause with unique identier Ref.
at_end_of_line
at_end_of_line(Stream)
The end of stream or end of line has been reached for Stream or from the current input
stream.
at_end_of_stream
at_end_of_stream(Stream)
atom(?X )
The end of stream has been reached for Stream or from the current input stream.
X is currently instantiated to an atom.
Summary of Built-In Predicates
381
atom_chars(+Atom,?CharList)
atom_chars(?Atom,+CharList)
The name of the atom Atom is the list of characters CharList.
atomic(?X )
X is currently instantiated to an atom or a number.
bagof(?Template,:Goal,?Bag )
Bag is the bag of instances of Template such that Goal is satised (not just provable).
block Specs
Not a built-in predicate; block declaration.
bb_delete(+Key,?Term)
Delete from the blackboard Term stored under Key.
bb_get(+Key,?Term)
Get from the blackboard Term stored under Key.
bb_put(+Key,+Term)
Store the term Term under Key on the blackboard.
bb_update(:Key, ?OldTerm, ?NewTerm)
Replace the term OldTerm by the term NewTerm under Key on the blackboard.
break
Invokes a recursive top-level. (not available in Runtime Systems).
'C'(?S1,?Terminal,?S2 )
Grammar rules. S1 is connected by the terminal Terminal to S2.
call(:Term)
(Module ::Term)
Executes the procedure call Term in Module.
call_cleanup(:Goal,:Cleanup )
Executes the procedure call Goal. When Goal succeeds deterministically, is cut, fails,
or raises an exception, Cleanup is executed.
call_residue(:Goal,?Residue )
Executes the procedure call Goal. Any oundered goals and the variables they are
blocked on occur as VarSet-Goal pairs in Residue.
callable(?X )
X is currently instantiated to a compound term or an atom.
character_count(+Stream,?Count)
Count characters have been read from or written to the stream Stream.
clause(:Head,?Body )
clause(:Head,?Body,?Ref )
clause(?Head,?Body,+Ref )
There is an interpreted clause whose head is Head, whose body is Body, and whose
unique identier is Ref.
close(+Stream)
Closes stream Stream.
382
SICStus Prolog
compare(?Op,?Term1,?Term2 )
Op is the result of comparing the terms Term1 and Term2.
compile(:Files )
Compiles in-core the clauses in text le(s) Files.
compound(?X )
X is currently instantiated to a term of arity > 0.
consult(:Files )
Updates the program with interpreted clauses from le(s) Files.
copy_term(?Term,?CopyOfTerm)
CopyOfTerm is an independent copy of Term.
create_mutable(+Datum,-Mutable )
Mutable is a new mutable term with current value Datum.
current_atom(?Atom)
One of the currently dened atoms is Atom.
current_input(?Stream)
Stream is the current input stream.
current_key(?KeyName,?KeyTerm)
There is a recorded item in the internal database whose key is KeyTerm, the name of
which is KeyName.
current_module(?Module )
Module is a module currently in the system.
current_module(?Module,?File )
Module is a module currently in the system, loaded from File.
current_op(?Precedence,?Type,?Op )
Atom Op is an operator type Type precedence Precedence.
current_output(?Stream)
Stream is the current output stream.
current_predicate(?Name,:Head )
current_predicate(?Name,-Head )
A user dened or library predicate is named Name, most general goal Head.
current_stream(?AbsFileName,?Mode,?Stream)
There is a stream Stream associated with the le AbsFileName and opened in mode
Mode.
debug
Switches on debugging in leap mode (not available in Runtime Systems).
debugging
Displays debugging status information (not available in Runtime Systems).
dif(?X,?Y )
The terms X and Y are dierent.
Summary of Built-In Predicates
383
display(?Term)
Displays the term Term on the standard output stream.
dynamic Specs
Not a built-in predicate; dynamic declaration.
ensure_loaded(:Files )
Compiles or loads the le(s) Files if need be.
erase(+Ref )
Erases the clause or record whose unique identier is Ref.
error_exception(+Exception)
user:error_exception(+Exception)
A hook predicate, Exception is an exception that traps to the debugger if it is switched
on.
expand_term(+Term1,?Term2 )
The term Term1 is a shorthand which expands to the term Term2.
fail
false
False.
fcompile(:Files )
Compiles le-to-le the clauses in text le(s) Files (not available in Runtime Systems).
file_search_path(Alias,?Expansion)
user:file_search_path(Alias,?Expansion)
A hook predicate, telling how to expand Alias (File ) le names.
fileerrors
Enables reporting of le errors.
findall(?Template,:Goal,?Bag )
findall(?Template,:Goal,?Bag,?Remainder )
A prex of Bag is the list of instances of Template such that Goal is provable. The
rest of Bag is Remainder or the empty list.
float(?X )
X is currently instantiated to a oat.
flush_output
flush_output(+Stream)
Flushes the buers associated with Stream.
foreign(+CFunctionName,+Predicate )
foreign(+CFunctionName,+Language,+Predicate )
Hook predicates, tell Prolog how to dene Predicate to invoke CFunctionName.
foreign_file(+ObjectFile,+Functions )
A hook predicate, tells Prolog that foreign functions Functions are in le ObjectFile.
Obsolete, use foreign_resource/2 instead.
foreign_resource(+ResourceName,+Functions )
A hook predicate, tells Prolog that foreign functions Functions are in resource ResourceName.
384
SICStus Prolog
format(+Format,+Arguments )
format(+Stream,+Format,+Arguments )
Writes Arguments according to Format on the stream Stream or on the current output
stream.
freeze(?Var,:Goal )
Blocks Goal until nonvar(Var ) holds.
frozen(-Var,?Goal )
The goal Goal is blocked on the variable Var.
functor(+Term,?Name,?Arity )
functor(?Term,+Name,+Arity )
The principal functor of the term Term has name Name and arity Arity.
garbage_collect
Performs a garbage collection of the global stack.
garbage_collect_atoms
gc
Performs a garbage collection of the atoms.
Enables garbage collection of the global stack.
get(?C )
get(+Stream,?C )
The next printing character from the stream Stream or from the current input stream
is C.
get0(?C )
get0(+Stream,?C )
The next character from the stream Stream or from the current input stream is C.
get_mutable(?Datum,+Mutable )
The current value of the mutable term Mutable is Datum.
goal_expansion(+Goal,+Module,-NewGoal )
user:goal_expansion(+Goal,+Module,-NewGoal )
A hook predicate. Denes a transformation from Goal in module Module to NewGoal.
ground(?X )
X is currently free of unbound variables.
halt
Halts Prolog. (returns to C in recursive calls to Prolog from C).
halt(Code )
Halts Prolog immediately, returning Code.
help
Hookable, prints a help message (not available in Runtime Systems).
if(+P,+Q,+R)
If P then Q else R, exploring all solutions of P.
incore(+Term)
Executes the procedure call Term.
Summary of Built-In Predicates
385
initialization
Executes a list of user dened goals (not available in Runtime Systems).
initialization(:Goal )
Adds Goal to the list of goals executed by initialization/0 (not available in Runtime
Systems).
instance(+Ref,?Term)
Term is a most general instance of the record or clause uniquely identied by Ref.
integer(?X )
X is currently instantiated to an integer.
?Y is +X Y is the value of the arithmetic expression X.
is_mutable(?X )
X is currently instantiated to a mutable term.
keysort(+List1,?List2 )
The list List1 sorted by key yields List2.
leash(+Mode )
Sets leashing mode to Mode (not available in Runtime Systems).
length(?List,?Length)
The length of list List is Length.
library_directory(?Directory )
user:library_directory(?Directory )
A hook predicate, telling how to expand library(File ) le names.
line_count(+Stream,?N )
N is the number of lines read/written on stream Stream.
line_position(+Stream,?N )
N is the number of characters read/written on the current line of Stream.
link_foreign_resource(+Resource,+SourceFile,+Option,+CFiles,+ObjectFiles,+Libraries )
Builds a linked foreign resource (not available in Runtime Systems).
listing
listing(:Specs )
Lists the interpreted predicate(s) specied by Specs or all interpreted predicates in the
type-in module. Any variables in the listed clauses are internally bound to ground
terms before printing. If this causes any blocked goals to be executed, the behavior is
undened.
load(:Files )
Loads object le(s) Files.
load_files(:Files )
load_files(:Files, +Options )
Loads source or object le(s) Files obeying Options.
386
SICStus Prolog
load_foreign_files(:ObjectFiles,+Libraries )
Hookable, links object les ObjectFiles into Prolog. Obsolete, use link_foreign_
resource/6 and load_foreign_resource/1 instead.
load_foreign_resource(:Resource )
Loads foreign resource Resource into Prolog.
unload_foreign_resource(:Resource )
Unloads foreign resource Resource from Prolog.
meta_predicate Specs
Not a built-in predicate; meta-predicate declaration.
method_expansion(+Method1,?Object,?Method2 )
user:method_expansion(+Method1,?Object,?Method2 )
A hook predicate, denes transformations on Prolog Object methods.
mode Specs
Not a built-in predicate; mode declaration.
module(+Module )
Sets the type-in module to Module; see Section 4.1 [Basic Concepts], page 47.
module(+Module, +ExportList)
module(+Module, +ExportList, +Options )
Not a built-in predicate; module declaration.
multifile Specs
Not a built-in predicate; multile declaration.
muse_flag(?FlagName,?Value )
Value is the current value of the Muse ag FlagName.
muse_flag(+FlagName,?OldValue,?NewValue )
OldValue and NewValue are the old and new values of the Muse ag FlagName.
muse_flags
Display all Muse ag settings.
muse_sync
Synchronize for being leftmost in Muse.
muse_trace(:Goal )
muse_trace(:Goal,+FilePrex )
Generate a Muse trace le for Goal.
name(+Const,?CharList)
name(?Const,+CharList)
The name of atom or number Const is the string CharList.
nl
nl(+Stream)
nodebug
Outputs a new line on stream Stream or on the current output stream.
Switches o debugging (not available in Runtime Systems).
Summary of Built-In Predicates
387
nofileerrors
Disables reporting of le errors.
Disables garbage collection of the global stack.
nogc
nonvar(?X )
nospy
:Spec
X is a non-variable.
nospyall
Removes spy-points from the predicate(s) specied by Spec (not available in Runtime
Systems).
Removes all spy-points (not available in Runtime Systems).
notrace
nozip
Switches o debugging (not available in Runtime Systems).
number(?X )
X is currently instantiated to a number.
number_chars(+Number,?CharList)
number_chars(?Number,+CharList)
The name of the number Number is the list of characters CharList.
numbervars(?Term,+N,?M )
Number the variables in the term Term from N to M-1.
on_exception(?Pattern,:ProtectedGoal,:Handler )
Executes the procedure call ProtectedGoal. If during the execution raise_
exception(Exception) is called, and Exception matches Pattern, the execution of
ProtectedGoal abort, Pattern is unied with a copy of Exception and Handler is called.
op(+Precedence,+Type,+Name )
Makes atom(s) Name an operator of type Type precedence Precedence.
open(+FileName,+Mode,-Stream)
open(+FileName,+Mode,-Stream,+Options ) [ISO]
Opens le FileName in mode Mode with options Options as stream Stream.
open_null_stream(-Stream)
Opens an output stream to the null device.
otherwise
parallel
parallel
True.
Specs
Not a built-in predicate; parallel declaration.
peek_char(?N )
peek_char(+Stream,?N )
N is the character code of the next character peeked at from Stream or from the current
input stream.
388
phrase(:Phrase,?List)
phrase(:Phrase,?List,?Remainder )
SICStus Prolog
Grammar rules. The list List can be parsed as a phrase of type Phrase. The rest of
the list is Remainder or empty.
portray(+Term)
user:portray(+Term)
A hook predicate, tells print/1 what to do.
portray_clause(+Clause )
portray_clause(+Stream,+Clause )
Pretty prints Clause on the stream Stream or on the current output stream.
portray_message(+Severity,+Message )
user:portray_message(+Severity,+Message )
A hook predicate, tells print_message/2 what to do.
predicate_property(:Head,?Prop )
predicate_property(-Head,?Prop )
Head is the most general goal of a currently dened predicate that has the property
Prop.
print(?Term)
print(+Stream,?Term)
Hookable, portrays or else writes the term Term on the stream Stream or on the current
output stream.
print_message(+Severity,+Message )
Hookable, portrays or else writes Message of a given Severity on the standard error
stream.
profile_data(:Files,?Selection,?Resolution,-Data)
Data is the proling data collected from the instrumented predicates dened in the
le(s) Files with selection and resolution Selection and Resolution respectively (not
available in Runtime Systems nor in Muse).
profile_reset(:Files )
The proling counters for the instrumented predicates in the le(s) Files are zeroed
(not available in Runtime Systems nor in Muse).
prolog_flag(?FlagName,?Value )
Value is the current value of FlagName.
prolog_flag(+FlagName,?OldValue,?NewValue )
OldValue and NewValue are the old and new values of FlagName.
prolog_load_context(?Key,?Value )
Value is the value of the compilation/loading context variable identied by Key.
prompt(?Old,?New )
Changes the prompt from Old to New.
public Specs
Not a built-in predicate; public declaration.
Summary of Built-In Predicates
389
put(+C )
put(+Stream,+C )
The next character sent to the stream Stream or to the current output stream is C.
raise_exception(+Exception)
Causes abortion of a part of the execution tree scoped by the closest enclosing on_
exception/3 invocation with its rst argument matching Exception.
read(?Term)
read(+Stream,?Term)
Reads the term Term from the stream Stream or from the current input stream.
read_term(?Term,+Options )
read_term(+Stream,?Term,+Options )
Reads the term Term from the stream Stream or from the current input stream with
extra Options.
reconsult(:Files )
Updates the program with interpreted clauses from le(s) Files.
recorda(+Key,?Term,-Ref )
Makes the term Term the rst record under key Key with unique identier Ref.
recorded(?Key,?Term,?Ref )
The term Term is currently recorded under key Key with unique identier Ref.
recordz(+Key,?Term,-Ref )
Makes the term Term the last record under key Key with unique identier Ref.
reinitialise
Initializes Prolog (returns to C in recursive calls to Prolog from C).
repeat
Succeeds repeatedly.
require(:PredSpecs )
Tries to locate and load library les that export the specied predicates. Creates index
les if necessary (not available in Runtime Systems).
restore(+File )
Restores the state saved in le File.
retract(:Clause )
Erases repeatedly the next interpreted clause of form Clause.
retractall(:Head )
Erases all clauses whose head matches Head.
save_program(+File )
save_program(+File, :Goal )
Saves the current state of the Prolog data base in le File. Upon restore, Goal is
executed.
see(+File )
Makes le File the current input stream.
390
SICStus Prolog
seeing(?File )
The current input stream is named File.
seek(+Stream,+Oset,+Method,-NewLocation)
Sets the stream Stream to the byte oset Oset relative to Method, and NewLocation
is the new byte oset from the beginning of the le after the operation.
seen
Closes the current input stream.
sequential
sequential
Specs
Not a built-in predicate; sequential declaration.
set_input(+Stream)
Sets the current input stream to Stream.
set_output(+Stream)
Sets the current output stream to Stream.
set_stream_position(+Stream,+Position)
Position is a term representing a new position of Stream, which is then set to the new
position.
setof(?Template,:Goal,?Set)
Set is the set of instances of Template such that Goal is satised (not just provable).
simple(?X )
X is currently uninstantiated or atomic.
skip(+C )
skip(+Stream,+C )
Skips characters from Stream or from the current input stream until after character C.
skip_line
skip_line(+Stream)
Skips characters from Stream or from the current input stream until the next LFD.
sort(+List1,?List2 )
The list List1 sorted into order yields List2.
source_file(?File )
source_file(:Pred,?File )
source_file(-Pred,?File )
The predicate Pred is dened in the le File.
spy :Spec Sets spy-points on the predicate(s) specied by Spec (not available in Runtime Systems).
spypoint_condition(:Goal,?Port,+Test)
Sets conditional spy-point on the predicate for Goal (not available in Runtime Systems).
statistics
Outputs various execution statistics.
statistics(?Key,?Value )
The execution statistics key Key has value Value.
Summary of Built-In Predicates
stream_code(+Stream,?StreamCode )
stream_code(?Stream,+StreamCode )
391
StreamCode is an integer representing a pointer to the internal representation of
Stream.
stream_interrupt(?Stream,?OldHandler,?NewHandler )
Sets/reads the interrupt handler connected to the stream Stream (not available in
Muse).
stream_position(+Stream,?Position)
Position is a term representing the current position of Stream.
stream_select(+Streams,+TimeOut,-ReadStreams )
Returns a list ReadStreams containing streams with pending characters. Only the
streams in the list Streams are checked. TimeOut species a timeout on the form off
or Sec:MicroSec (not available in Muse).
tab(+N )
tab(+Stream,+N )
Outputs N spaces to the stream Stream or to the current output stream.
tell(+File )
Makes le File the current output stream.
telling(?File )
The current output stream is named File.
term_expansion(+Term1,?TermOrTerms )
term_expansion(+Term1,+Layout1,?TermOrTerms,?Layout2 )
user:term_expansion(+Term1,?TermOrTerms )
user:term_expansion(+Term1,+Layout1,?TermOrTerms,?Layout2 )
A hook predicate, tells expand_term/2 what to do.
told
Closes the current output stream.
trace
Switches on debugging in creep mode (not available in Runtime Systems).
true
Succeeds.
ttyflush Flushes the standard output stream buer.
ttyget(?C )
The next printing character input from the standard input stream is C.
ttyget0(?C )
The next character input from the standard input stream is C.
ttynl
Outputs a new line on the standard output stream.
ttyput(+C )
The next character output to the standard output stream is C.
ttyskip(+C )
Skips characters from the standard input stream until after character C.
392
SICStus Prolog
ttytab(+N )
Outputs N spaces to the standard output stream.
unknown(?OldState,?NewState )
Changes action on undened predicates from OldState to NewState (not available in
Runtime Systems).
unknown_predicate_handler(+Goal,+Module,-NewGoal )
user:unknown_predicate_handler(+Goal,+Module,-NewGoal )
A hook predicate. Denes an alternative goal to be called in place of a call to an
unknown predicate.
update_mutable(+Datum,+Mutable )
Updates the current value of the mutable term Mutable to become Datum.
use_module(:Files )
Loads the module le(s) Files if necessary and import all public predicates.
use_module(:File,+Imports )
Loads the module le File if necessary and imports the predicates in Imports.
use_module(+Module,?File,+Imports )
use_module(?Module,:File,+Imports )
Equivalent to use_module/2 plus unies Module to the module dened by the le.
user_help
user:user_help
A hook predicate, tells help/0 what to do.
var(?X ) X is currently uninstantiated.
version
Displays introductory and/or system identication messages (not available in Runtime
Systems).
version(+Message )
Adds the atom Message to the list of introductory messages (not available in Runtime
Systems).
volatile Specs
Not a built-in predicate; volatile declaration.
when(+Condition,:Goal )
Blocks Goal until the Condition is true.
write(+Term)
write(+Stream,+Term)
Writes the term Term on the stream Stream or on the current output stream.
write_canonical(+Term)
write_canonical(+Stream,+Term)
Writes Term on the stream Stream or on the current output stream so that it may be
read back.
Summary of Built-In Predicates
write_term(+Term,+Options )
write_term(+Stream,+Term,+Options )
393
Writes the term Term on the stream Stream or on the current output stream with extra
Options.
writeq(+Term)
writeq(+Stream,+Term)
Writes the term Term on the stream Stream or on the current output stream, quoting
names where necessary.
zip
Switches on debugging in zip mode (not available in Runtime Systems).
394
SICStus Prolog
Full Prolog Syntax
395
Full Prolog Syntax
A Prolog program consists of a sequence of sentences or lists of sentences. Each sentence is a Prolog
term. How terms are interpreted as sentences is dened below (see [Sentence], page 396). Note
that a term representing a sentence may be written in any of its equivalent syntactic forms. For
example, the 2-ary functor `:-' could be written in standard prex notation instead of as the usual
inx operator.
Terms are written as sequences of tokens. Tokens are sequences of characters which are treated
as separate symbols. Tokens include the symbols for variables, constants and functors, as well as
punctuation characters such as brackets and commas.
We dene below how lists of tokens are interpreted as terms (see [Term Token], page 397). Each
list of tokens which is read in (for interpretation as a term or sentence) has to be terminated by a
full-stop token. Two tokens must be separated by a layout-text token if they could otherwise be
interpreted as a single token. Layout-text tokens are ignored when interpreting the token list as a
term, and may appear at any point in the token list.
We dene below denes how tokens are represented as strings of characters (see [Token String],
page 398). But we start by describing the notation used in the formal denition of Prolog syntax
(see [Syntax Notation], page 395).
Notation
1. Syntactic categories (or non-terminals) are written thus: item. Depending on the section, a
category may represent a class of either terms, token lists, or character strings.
2. A syntactic rule takes the general form
C --> F1 | F2 | F3
which states that an entity of category C may take any of the alternative forms F1, F2, F3,
etc.
3. Certain denitions and restrictions are given in ordinary English, enclosed in { } brackets.
4. A category written as C... denotes a sequence of one or more Cs.
5. A category written as ?C denotes an optional C. Therefore ?C... denotes a sequence of zero
or more Cs.
6. A few syntactic categories have names with arguments, and rules in which they appear may
contain meta-variables looking thus: X. The meaning of such rules should be clear from analogy
with the denite clause grammars (see Section 7.1.2 [Denite], page 76).
7. In the section describing the syntax of terms and tokens (see [Term Token], page 397) particular tokens of the category name are written thus: name, while tokens which are individual
punctuation characters are written literally.
396
SICStus Prolog
Syntax of Sentences as Terms
sentence
module : sentence
list
{ where list is a list of sentence }
clause
directive
grammar-rule
-->
|
|
|
|
clause
non-unit-clause
-->
directive
-->
command
non-unit-clause
-->
head
unit-clause
-->
head
command
query
head
body
:-
{
--> :--> ?-->
|
|
|
unit-clause
query
body
where head is not otherwise a sentence }
body
body
module : head
goal
{ where goal is not a variable }
--> module : body
| body -> body ;
| body -> body
| \+ body
| body ; body
| body , body
| goal
body
goal
-->
term
grammar-rule
-->
gr-head
-->
|
|
module : gr-head
gr-head , terminals
non-terminal
{ where non-terminal is not a variable }
gr-head
gr-body
{
where term is not otherwise a body }
-->
gr-body
--> module : gr-body
| gr-body -> gr-body ;
| gr-body -> gr-body
| \+ gr-body
| gr-body ; gr-body
gr-body
Full Prolog Syntax
397
gr-body , gr-body
non-terminal
terminals
gr-condition
|
|
|
|
non-terminal
-->
term
terminals
-->
list
gr-condition
module
string
|
--> ! | {
-->
where term is not otherwise a gr-body }
{
body
}
atom
Syntax of Terms as Tokens
term-read-in
-->
subterm(1200) full-stop
subterm(N)
-->
term(N)
-->
|
|
|
|
|
|
term(1000)
-->
term(0)
-->
|
|
|
|
|
|
op(N,T)
-->
term(M)
{ where M is less than or equal to N
}
op(N,fx) subterm(N-1)
{ except in the case of a number }
{ if subterm starts with a (,
op must be followed by layout-text }
op(N,fy) subterm(N)
{ if subterm starts with a (,
op must be followed by layout-text }
subterm(N-1) op(N,xfx) subterm(N-1)
subterm(N-1) op(N,xfy) subterm(N)
subterm(N) op(N,yfx) subterm(N-1)
subterm(N-1) op(N,xf)
subterm(N) op(N,yf)
subterm(999)
,
subterm(1000)
functor ( arguments )
{ provided there is no layout-text between
the functor and the ( }
( subterm(1200) )
{ subterm(1200) }
list
string
constant
variable
name
398
SICStus Prolog
{
arguments
list
subterm(999)
subterm(999)
-->
|
--> []
| [
listexpr
constant
number
unsigned-number
atom
listexpr
,
-->
atom
-->
|
|
|
arguments
listexpr
subterm(999)
number
unsigned-number
sign unsigned-number
sign inf
sign nan
natural-number
-->
-->
|
}
]
--> subterm(999)
| subterm(999) ,
| subterm(999) |
-->
functor
where name has been declared as an
operator of type T and precedence N
|
unsigned-oat
name
name
Syntax of Tokens as Character Strings
By default, SICStus Prolog uses the ISO 8859/1 character set standard, but will alternatively support the EUC (Extended UNIX Code) standard. This is governed by the value of the environment
variable SP_CTYPE (see Section 1.1 [Start], page 7).
The character categories used below are dened as follows in the two standards:
layout-char
small-letter
In ISO 8859/1, these are character codes 0..32 and 127..159. In EUC, these are character
codes 0..32 and 127. The common subset includes characters such as TAB, LFD, and
SPC.
In ISO 8859/1, these are character codes 97..122, 223..246, and 248..255. In EUC, these
are character codes 97..122 and 128..255. The common subset includes the letters a
through z.
capital-letter
In ISO 8859/1, these are character codes 65..90, 192..214, and 216..222. In EUC, these
are character codes 65..90. The common subset is the letters A through Z.
Full Prolog Syntax
399
digit
In both standards, these are character codes 48..57, i.e. the digits 0 through 9.
symbol-char
In ISO 8859/1, these are character codes 35, 36, 38, 42, 43, 45..47, 58, 60..64, 92, 94,
96, 126, 160..191, 215, and 247. In EUC, these are character codes 35, 36, 38, 42, 43,
45..47, 58, 60..64, 92, 94, 96, and 126. The common subset is
+-*/\^<>=`~:[email protected]#$&.
solo-char In both standards, these are character codes 33 and 59 i.e. the characters ! and ;.
punctuation-char
In both standards, these are character codes 37, 40, 41, 44, 91, 93, and 123..125, i.e.
the characters %(),[]{|}.
quote-char
In both standards, these are character codes 34 and 39 i.e. the characters " and '.
underline In both standards, this is character code 95 i.e. the character _.
token
--> name
| natural-number
| unsigned-oat
| variable
| string
| punctuation-char
| layout-text
| full-stop
name
quoted-name
quoted-item
--> quoted-name
| word
| symbol
| solo-char
| [ ?layout-text ]
| { ?layout-text }
--> '
-->
|
|
char
-->
symbol
-->
-->
|
{
'
other than ' or \ }
''
\ escape-sequence
word
natural-number
?quoted-item...
small-letter ?alpha...
symbol-char...
{ except in the case of a full-stop
or where the rst 2 chars are /* }
digit...
base ' alpha...
{ where each alpha must be less than the base,
treating a,b,... and A,B,... as 10,11,... }
400
SICStus Prolog
|
0 '
{
char-item
base
-->
-->
simple-oat
-->
digit...
digit...
.
--> e
exponent
-->
sign
--> - | +
string
string-item
-->
|
digit...
E
|
sign digit...
?string-item...
--> "
"
--> char { other than "
| ""
| \ escape-sequence
-->
--> .
|
comment
{
{
the following token, if any, must be layout-text}
any character, i.e. }
layout-char
alpha
symbol-char
solo-char
punctuation-char
quote-char
--> {
|
|
|
|
|
layout-char
?char... */
where ?char... must not contain */ }
?char... LFD
{ where ?char... must not contain LFD }
--> /*
%
or \ }
layout-text-item...
-->
|
char
in the range [2..36] }
underline ?alpha...
capital-letter ?alpha...
layout-text-item
full-stop
|
digit...
layout-text
comment
{
simple-oat
| simple-oat exp exponent
exp
variable
}
char { other than \ }
\ escape-sequence
-->
|
unsigned-oat
char-item
yielding the character code for char
Full Prolog Syntax
401
alpha
-->
escape-sequence
--> b
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
capital-letter
|
small-letter
|
digit
|
underline
backspace, character code 8 }
{ horizontal tab, character code 9 }
{ newline, character code 10 }
{ vertical tab, character code 11 }
{ form feed, character code 12 }
{ carriage return, character code 13 }
{ escape, character code 27 }
{ delete, character code 127 }
{ alarm, character code 7 }
alpha alpha
{treating a,b,... and A,B,... as 10,11,... }
{ in the range [0..15], hex character code }
digit ?digit ?digit
{ in the range [0..7], octal character code }
^ ?
{ delete, character code 127 }
^ capital-letter
^ small-letter
{ the control character alpha mod 32 }
c ?layout-char... { ignored }
layout-char { ignored }
char
{ other than the above, represents itself }
t
n
v
f
r
e
d
a
x
{
Escape Sequences
A backslash occurring inside integers in `0'' notation or inside quoted atoms or strings has special
meaning, and indicates the start of an escape sequence. Character escaping can be turned o for
compatibility with old code. The following escape sequences exist:
\b
\t
\n
\v
\f
\r
\e
\d
\^?
\a
\xCD
\octal
backspace (character code 8)
horizontal tab (character code 9)
newline (character code 10)
vertical tab (character code 11)
form feed (character code 12)
carriage return (character code 13)
escape (character code 27)
delete (character code 127)
alarm (character code 7)
the character code CD (two hexadecimal digits)
the character code octal base 8, where octal is up to 3 octal digits
402
SICStus Prolog
\^char
the character code char mod 32, where char is a letter.
\layout-char
A single layout character, for example a newline, is ignored.
\c
All characters up to, but not including, the next non-layout character are ignored.
\other
A character not mentioned in this table stands for itself. For example, `\\' inserts a
single backslash and `\'' inserts a single quote.
Notes
1. The expression of precedence 1000 (i.e. belonging to syntactic category term(1000)) which is
written
X,Y
denotes the term ','(X,Y ) in standard syntax.
2. The parenthesized expression (belonging to syntactic category term(0))
(X )
denotes simply the term X.
3. The curly-bracketed expression (belonging to syntactic category term(0))
{X }
denotes the term {}(X ) in standard syntax.
4. Note that, for example, -3 denotes a number whereas -(3) denotes a compound term which
has the 1-ary functor - as its principal functor.
5. The character " within a string must be written duplicated. Similarly for the character '
within a quoted atom.
6. Backslashes in strings, quoted atoms, and integers written in `0'' notation denote escape
sequences.
7. A name token declared to be a prex operator will be treated as an atom only if no term-read-in
can be read by treating it as a prex operator.
8. A name token declared to be both an inx and a postx operator will be treated as a postx
operator only if no term-read-in can be read by treating it as an inx operator.
Standard Operators
Standard Operators
:- op( 1200, xfx, [ :-, --> ]).
:- op( 1200, fx, [ :-, ?- ]).
:- op( 1150, fx, [ mode, public, dynamic, volatile,
multifile, block, meta_predicate,
parallel, sequential ]).
:- op( 1100, xfy, [ ; ]).
:- op( 1050, xfy, [ -> ]).
:- op( 1000, xfy, [ ',' ]).
:- op( 900, fy, [ \+, spy, nospy ]).
:- op( 700, xfx, [ =, is, =.., ==, \==, @<, @>, @=<, @>=,
=:=, =\=, <, >, =<, >= ]).
:- op( 550, xfy, [ : ]).
:- op( 500, yfx, [ +, -, #, /\, \/ ]).
:- op( 500, fx, [ +, - ]).
:- op( 400, yfx, [ *, /, //, <<, >> ]).
:- op( 300, xfx, [ mod ]).
:- op( 200, xfy, [ ^ ]).
403
404
SICStus Prolog
References
405
References
[Aggoun & Beldiceanu 92]
A. Aggoun and N. Beldiceanu, Extending CHIP in order to Solve Complex Scheduling and Placement Problems, Mathl. Comput. Modelling, vol. 17, no. 7, pp. 57{73,
Pergamon Press Ltd., 1993.
[Ali & Karlsson 92]
K.A.M. Ali and R. Karlsson, Scheduling Speculative work in MUSE and Performance
Results, International Journal of Parallel Programming, Vol 21, No.6, December 1992.
[Aronsson 91]
M. Aronsson, Methodology and Programming Techniques in GCLAII. In Proceedings
of the second Workshop on Extensions of Logic Programming (ELP), by Eriksson,
Hallnas, and Schroeder-Heister (eds.), held at SICS, Stockholm, Sweden 1991, to be
published in Lecture Notes in Articial Intelligence, Springer-Verlag. Also available as
research report SICS R92:05.
[Aronsson 93]
M. Aronsson, Implementational Issues in GCLA: A-Suciency and the Deniens operation, in Proceedings of the third Workshop on Extensions of Logic Programming
(ELP), LNAI 660, Springer Verlag, 1993.
[Beldiceanu & Contejean 94]
N. Beldiceanu and E. Contejean, Introducing Global Constraints in CHIP, Mathl. Comput. Modelling, vol. 20, no. 12, pp. 97{123, Pergamon Press Ltd., 1994.
[Bryant 86]
R.E. Bryant, Graph-Based Logarithms for Boolean Function Manipulation, IEEE
Trans. on Computers, August, 1986.
[Carlsson 90]
M. Carlsson, Design and Implementation of an OR-Parallel Prolog Engine, SICS Dissertation Series 02, 1990.
[Carreiro & Gelernter 89a]
N. Carreiro and D. Gelernter, Linda in Context, Comm. of the ACM, 32(4) 1989.
[Carreiro & Gelernter 89b]
N. Carreiro and D. Gelernter, How to Write Parallel Programs: A Guide to the Perplexed, ACM Computing Surveys, September 1989.
[Clocksin & Mellish 81]
W.F. Clocksin and C.S. Mellish, Programming in Prolog, Springer-Verlag, 1981.
[Colmerauer 75]
A. Colmerauer, Les Grammaires de Metamorphos, Technical Report, Groupe
d'Intelligence Articielle, Marseille-Luminy, November, 1975.
[Colmerauer 90]
Colmerauer A.: An Introduction to Prolog III, Communications of the ACM, 33(7),
69-90, 1990.
406
[Conery 83]
SICStus Prolog
J. Conery, The AND/OR Process Model for Parallel Interpretation of Logic Programs,
PhD thesis, University of California at Irvine, 1983.
[Diaz & Codognet 93]
D. Diaz and P. Codognet, A Minimal Extension of the WAM for clp(FD), Proceedings
of the International Conference on Logic Programming, MIT Press, 1993.
[Elshiewy 90]
N.A. Elshiewy, Robust Coordinated Reactive Computing in Sandra, SICS Dissertation
Series 03, 1990.
[Falkman & Torgersson 94]
G. Falkman and O. Torgersson, Programming Methodologies in GCLA, in Proceedings of the fourth Workshop on Extensions of Logic Programming (ELP), LNAI 798,
Springer Verlag, 1994.
[Gorlick & Kesselman 87]
M.M. Gorlick and C.F. Kesselman, Timing Prolog Programs Without Clocks, Proc.
Symposium on Logic Programming, pp. 426{432, IEEE Computer Society, 1987.
[Hausman 90]
B. Hausman, Pruning and Speculative Work in OR-Parallel PROLOG, PhD thesis,
Department of Telecommunication and Computer Systems, The Royal Institute of
Technology, Stockholm, 1990.
[Heintze et al. 87]
N. Heintze, J. Jaar, S. Michaylov, P. Stuckey, R. Yap, The CLP(R) Programmers
Manual, Monash University, Clayton, Victoria, Australia, Department of Computer
Science, 1987.
[Hermenegildo and Greene 90]
M.V. Hermenegildo and K.J. Greene, &-Prolog and its performance: Exploiting Independent And-Parallelism, Proceedings of the Seventh ICLP, Jerusalem, MIT Press,
1990.
[Holzbaur 90]
C. Holzbaur, Specication of Constraint Based Inference Mechanism through Extended
Unication, dissertation, Dept. of Medical Cybernetics & AI, University of Vienna,
1990.
[Holzbaur 92]
C. Holzbaur, A High-Level Approach to the Realization of CLP Languages, Proceedings of the JICSLP92 Post-Conference Workshop on Constraint Logic Programming
Systems, Washington D.C., 1992.
[Holzbaur 92]
C. Holzbaur, Metastructures vs. Attributed Variables in the Context of Extensible
Unication, in M. Bruynooghe & M. Wirsing (eds.), Programming Language Implementation and Logic Programming, Springer-Verlag, LNCS 631, pp. 260-268, 1992.
References
407
[Holzbaur 94]
C. Holzbaur, A Specialized, Incremental Solved Form Algorithm for Systems of Linear
Inequalities, Austrian Research Institute for Articial Intelligence, Vienna, TR-94-07,
1994.
[Jaar & Michaylov 87]
J. Jaar, S. Michaylov, Methodology and Implementation of a CLP System, in J.L.
Lassez (ed.), Logic Programming - Proceedings of the 4th International Conference Volume 1, MIT Press, Cambridge, MA, 1987.
[Karlsson 92]
R. Karlsson, A High Performance OR-parallel Prolog System, PhD thesis, Department of Telecommunication and Computer Systems, The Royal Institute of Technology,
Stockholm, 1992.
[Kowalski 74]
R.A. Kowalski, Logic for Problem Solving, DCL Memo 75, Dept of Articial Intelligence, University of Edinburgh, March, 1974.
[Kowalski 79]
R.A. Kowalski, Articial Intelligence: Logic for Problem Solving. North Holland, 1979.
[Kreuger 91]
P. Kreuger, GCLAII|A denitional approach to control. In Proceedings of the second Workshop on Extensions of Logic Programming (ELP), by Eriksson, Hallnas, and
Schroeder-Heister (eds.), held at SICS, Stockholm, Sweden 1991, to be published in
Lecture Notes in Articial Intelligence, Springer-Verlag. Also available as research
report SICS R92:09.
[Lusk et al. 90]
E. Lusk, D.H.D. Warren, S. Haridi et al., The Aurora Or-Parallel System, New Generation Computing vol. 7 nos. 2{3 pp. 243{271, 1990.
[McCabe 92]
F. McCabe, Logic and Objects, Prentice Hall, 1992.
[O'Keefe 90]
R.A. O'Keefe, The Craft of Prolog, MIT Press, 1990.
[Ousterhout 94]
John K. Ousterhout, Tcl and the Tk Toolkit. Addison-Wesley, 1994.
[Pereira & Warren 80]
F.C.N. Pereira and D.H.D. Warren, Denite clause grammars for language analysis|
a survey of the formalism and a comparison with augmented transition networks in
Articial Intelligence 13:231-278, 1980.
[Regin 94] J.-C. Regin, A ltering algorithm for constraints of dierence in CSPs, Proc. of the
Twelfth National Conference on Articial Intelligence (AAAI-94), pp. 362{367, 1994
[Robinson 65]
J.A. Robinson, A Machine-Oriented Logic Based on the Resolution Principle, Journal
of the ACM 12:23-44, January 1965.
408
[Roussel 75]
SICStus Prolog
P. Roussel, Prolog : Manuel de Reference et d'Utilisation, Groupe d'Intelligence Articielle, Marseille-Luminy, 1975.
[Saraswat 90]
V. Saraswat, Concurrent Constraint Programming Languages. PhD thesis, CarnegieMellon University, 1990.
[Sterling & Shapiro 86]
L. Sterling and E. Shapiro, The Art of Prolog. The MIT Press, Cambridge MA, 1986.
[Van Hentenryck 89]
P. Van Hentenryck, Constraint Satisfaction in Logic Programming, Logic Programming
Series, The MIT Press, 1989.
[Van Hentenryck et al. 92]
P. Van Hentenryck, V. Saraswat and Y. Deville, Constraint processing in cc(FD),
unpublished manuscript, 1992.
[Van Hentenryck & Deville 91]
P. Van Hentenryck and Y. Deville, The Cardinality Operator: a new logical connective
and its application to constraint logic programming, In: Eighth International Conference on Logic Programming, 1991.
[Van Hentenryck et al. 95]
P. Van Hentenryck, V. Saraswat and Y. Deville, Design, implementation and evaluation
of the constraint language cc(FD). In A. Podelski, ed., Constraints: Basics and Trends,
volume 910 of Lecture Notes in Computer Science. Springer-Verlag, 1995.
[Warren 77]
D.H.D. Warren, Applied Logic|Its Use and Implementation as a Programming Tool,
PhD thesis, Edinburgh University, 1977. Available as Technical Note 290, SRI International.
[Warren 83]
D.H.D. Warren, An Abstract Prolog Instruction Set, Technical Note 309, SRI International, 1983.
[Wirth 76] N. Wirth, Algorithms + Data Structures = Programs, Prentice-Hall, 1976.
Predicate Index
409
Predicate Index
:
!
:- /1, command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
!/0, cut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#
36, 99
:/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
::/1 (message sending) . . . . . . . . . . . . . . . . . . . . . . . . . .
94
241
277
275
277
275
275
275
276
277
277
275
275
277
275, 277
# /2, bitwise exclusive or . . . . . . . . . . . . . . . . . . . . . . .
# /2, boolean eor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#/\ /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#= /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#=> /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#=< /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#>= /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#\ /1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#\ /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#\/ /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#\= /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#< /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#<= /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#<=> /2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
*
* /2, boolean and . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
* /2, multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
,
,/2, conjunction
241
94
.................................
-
- /1, negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
- /2, subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
--> /2, grammar rule
.............................
-> /2 ;/2, if then else . . . . . . . . . . . . . . . . . . . . . . . . . . .
-> /2, if then
:- /2, clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
....................................
.
. /2, consult . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. /2, identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/
/ /2, floating division . . . . . . . . . . . . . . . . . . . . . . . . . .
// /2, integer division . . . . . . . . . . . . . . . . . . . . . . . . . .
/\ /2, bitwise conjunction . . . . . . . . . . . . . . . . . . . . . . .
99
94
94
76
99
99
::/2 (message sending) . . . . . . . . . . . . . . . . . . . . . . . . . .
;
;/2, disjunction
94
94
94
99
.................................
=
= /2, unification
...............................
=../2, univ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
=:= /2, arithmetic equal . . . . . . . . . . . . . . . . . . . . . . . . .
=:= /2, boolean equal . . . . . . . . . . . . . . . . . . . . . . . . . . .
== /2, equality of terms
.........................
124
111
96
241
97
96
241
96
241
=\= /2, arithmetic not equal . . . . . . . . . . . . . . . . . . . . .
=\= /2, boolean not equal . . . . . . . . . . . . . . . . . . . . . . .
=< /2, arithmetic less or equal . . . . . . . . . . . . . . . . . .
=< /2, boolean less or equal . . . . . . . . . . . . . . . . . . . .
?
?- /1, query
12
98
......................................
?= /2, terms identical or cannot unify . . . . . . . . . .
[
[]/0, consult . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@
@=< /2, term less or equal
@> /2, term greater than
.......................
.........................
@>= /2, term greater or equal . . . . . . . . . . . . . . . . . . . .
@< /2, term less than . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
{
{}/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
74
94
13
31
100
332
332
~
~ /1, boolean not . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
+
+ /1, identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
+ /2, addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
+ /2, boolean ior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
74
98
98
98
98
248
241
94
94
241
410
SICStus Prolog
>
96
241
97
241
94
> /2, arithmetic greater than . . . . . . . . . . . . . . . . . . . .
> /2, boolean greater . . . . . . . . . . . . . . . . . . . . . . . . . . .
>= /2, arithmetic greater or equal . . . . . . . . . . . . . .
>= /2, boolean greater or equal . . . . . . . . . . . . . . . . .
>> /2, right shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
array to list/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
^
aset/4
^ /2, boolean existential quantifier
..........
^ /2, existential quantifier . . . . . . . . . . . . . . . . . . .
241
119
\
\ /1, bitwise negation . . . . . . . . . . . . . . . . . . . . . . . . . . .
\/ /2, bitwise disjunction . . . . . . . . . . . . . . . . . . . . . . .
\== /2, inequality of terms . . . . . . . . . . . . . . . . . . . . . .
\+ /1, not provable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
<
94
94
97
99
...........................................
asin/1, function
.................................
asinh/1, function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
assert/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
assert/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
asserta/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
asserta/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
assertz/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
assertz/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
assignment/2
....................................
assoc to list/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
at end of line/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
< /2, arithmetic less than . . . . . . . . . . . . . . . . . . . . . . .
< /2, boolean less . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
<:/1 (message delegation) . . . . . . . . . . . . . . . . . . . . . .
<:/2 (message delegation) . . . . . . . . . . . . . . . . . . . . . .
<< /2, left shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A
96
241
332
332
94
at end of line/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
at end of stream/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
at end of stream/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
atan/1, function
115
115
67
17, 125
95
89
95
96
95
95
96
206
222, 226
199
221, 225
105
278
278
67
201
189
189
189
111
105
abolish/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
abort (debugger command)
abort/0
........................
......................................
abs/1, absolute value . . . . . . . . . . . . . . . . . . . . . . . . . . . .
absolute file name/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
acos/1, function
.................................
acosh/1, function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
acot/1, function
.................................
acot2/2, function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
acoth/1, function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
acyclic term/1
..................................
add edges/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.................................
atan2/2, function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
atanh/1, function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
atom/1
abolish/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
...........................................
atom chars/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
atom garbage collection (statistics/2 option)
..............................................
atom to chars/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
atom to chars/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
atomic/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
atoms (statistics/2 option) . . . . . . . . . . . . . . . . . . . .
attribute/1 (declaration) . . . . . . . . . . . . . . . . . . . . . .
attribute goal/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
B
67
122
119
234
117
116
250
251
116
117
55
67
68
16, 125
backtracks (profile data/4 option) . . . . . . . . . . . .
bagof/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
add vertices/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
bagof rd noblock/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
agc margin (prolog flag) . . . . . . . . . . . . . . . . . . . . . . . .
bb delete/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
all different/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
bb get/2
.........................................
all distinct/1
bb inf/3
.........................................
ancestors (debugger command) . . . . . . . . . . . . . . . . . . . .
bb inf/5
.........................................
append/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
bb put/2
.........................................
aref/3
bb update/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
...........................................
109
369
369
110
109
193
194
backtrace (debugger command) . . . . . . . . . . . . . . . . . . . .
add to heap/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..................................
189
190
95
95
114
114
114
114
114
114
278
191
92
92
91
91
95
95
96
110
112
arefa/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
block/1 (declaration) . . . . . . . . . . . . . . . . . . . . . . . . . . . .
arefl/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
blocked goals (debugger command) . . . . . . . . . . . . . . . .
arg/3
break (debugger command)
............................................
argv (prolog flag)
..............................
break/0
........................
......................................
Predicate Index
411
C
80
100
101
377
120, 261
110
122
241
95
91
106
109
122
10
317
317
318
317
320
317
319
317
278
278
114
114
223
89
233
285
283
223
223
68
98
20
20
20
20
52, 74
105
222
222
110
103
20
20
20
20
C/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
call/1
...........................................
call cleanup/2
..................................
call graph/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
call residue/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
callable/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
calls (profile data/4 option) . . . . . . . . . . . . . . . . . .
card/2, boolean cardinality . . . . . . . . . . . . . . . . . . . .
ceiling/1, function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
character count/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
character escapes (prolog flag) . . . . . . . . . . . . . . . .
choice (statistics/2 option) . . . . . . . . . . . . . . . . . . .
choice points (profile data/4 option) . . . . . . . . .
CHOICESTKSIZE (environment) . . . . . . . . . . . . . . . . . . . . .
chr debug/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
chr debugging/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
chr leash/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
chr nodebug/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
chr nospy/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
chr notrace/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
chr spy/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
chr trace/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
circuit/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
circuit/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
clause/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
clause/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
clique/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
close/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
close client/0
consult/1
.....................................
52, 74
103
112
109
95
95
95
95
277
113
65
279
104
313
237
313
230
90
116
105
105
124
90
104
90
206
context error/3 (error class) . . . . . . . . . . . . . . . . . .
copy term/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
core (statistics/2 option) . . . . . . . . . . . . . . . . . . . . .
cos/1, function
..................................
cosh/1, function
cot/1, function
.................................
..................................
coth/1, function
.................................
count/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
create mutable/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
creep (debugger command)
cumulative/4
........................
....................................
current atom/1
..................................
current constraint/2
...........................
current db/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
current handler/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
current host/1
..................................
current input/1
..................................
current key/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
current module/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
current module/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
current op/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
current output/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
current predicate/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
current stream/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cyclic term/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
D
215
238
238
238
238
237
237
237
237
237
237
237
237
236
236
237
237
61, 120
108
67
105
62, 121
datime/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..................................
db buffering/2
..................................
clpfd:dispatch global/4 . . . . . . . . . . . . . . . . . . . . . . . .
db buffering/3
..................................
clpfd:full answer/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
db canonical/2
..................................
coloring/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
db canonical/3
..................................
colouring/3
db close/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.....................................
command (debugger command) . . . . . . . . . . . . . . . . . . . . . .
db close/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
compare/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
db erase/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
compile-buffer (emacs command) . . . . . . . . . . . . . . . . . .
db erase/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
compile-file (emacs command) . . . . . . . . . . . . . . . . . . . .
db fetch/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
compile-predicate (emacs command) . . . . . . . . . . . . . .
db fetch/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
compile-region (emacs command) . . . . . . . . . . . . . . . . . .
db findall/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
compile/1
db findall/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.....................................
compiling (prolog flag)
........................
db open/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
....................................
db open/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
compose/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
db store/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
compound/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
db store/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
consistency error/4 (error class)
debug/0
complement/2
.............
......................................
consult-buffer (emacs command) . . . . . . . . . . . . . . . . . .
debugger print options (prolog flag) . . . . . . . . . .
consult-file (emacs command) . . . . . . . . . . . . . . . . . . . .
debugging (debugger command) . . . . . . . . . . . . . . . . . . . .
consult-predicate (emacs command) . . . . . . . . . . . . . .
debugging (prolog flag)
consult-region (emacs command) . . . . . . . . . . . . . . . . . .
debugging/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
........................
412
SICStus Prolog
122
192
222, 226
192
192
221, 226
201
215
215
200
120
215
66
82
276
103
251
54
deep fails (profile data/4 option) . . . . . . . . . . . . .
del assoc/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
del edges/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
del max assoc/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
del min assoc/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
del vertices/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
delete/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
delete file/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
delete file/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
delete from heap/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
dif/2
............................................
directory files/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
display (debugger command) . . . . . . . . . . . . . . . . . . . . . .
display/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
domain/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
domain error/4 (error class) . . . . . . . . . . . . . . . . . . .
dump/3
...........................................
dynamic/1 (declaration) . . . . . . . . . . . . . . . . . . . . . . . . .
E
286
286
283
282
288
288
288
288
288
288
288
288
287
287
288
287
287
287
287
288
288
287
288
288
288
216
216
216
89
106
92
67
313
313
119
119
313
313
110
94
95
91
91
129
129, 168
134
129
83
83
369
.........................................
fd size/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fd statistics/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fd statistics/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset add element/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset complement/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset del element/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset disjoint/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset eq/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset intersect/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset intersection/2
...........................
fdset intersection/3
...........................
fdset interval/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset max/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset member/2
..................................
fdset min/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset parts/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset singleton/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset size/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
edges/2
.....................................
221, 225
277
191
287
199
287
211
53, 75
249
215
115
69
215
122
103
96
96
266
79
element/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
empty assoc/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
empty fdset/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
empty heap/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
empty interval/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
empty queue/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ensure loaded/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
entailed/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
environ/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
erase/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
error exception/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
exec/3
fd set/2
...........................................
execution time (profile data/4 option) . . . . . . . .
existence error/5 (error class) . . . . . . . . . . . . . . . .
fdset subset/2
..................................
fdset subtract/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset to list/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset to range/2
................................
fdset union/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fdset union/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
file exists/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
file exists/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
file property/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
file search path/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fileerrors (prolog flag)
fileerrors/0
.......................
.....................................
find this (debugger command)
....................
find constraint/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
find constraint/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
findall/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
exp/1, exponent
..................................
exp/2, exponent
findall/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..................................
findall constraints/2
..........................
findall constraints/3
..........................
expand/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
expand term/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
F
float/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
float/1, coercion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
100
100
52, 75
286
286
286
286
286
floor/1, function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fail (debugger command) . . . . . . . . . . . . . . . . . . . . . . . . . .
flush output/0
...................................
fail/0
flush output/1
...................................
...........................................
false/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
foreign/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fcompile/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
foreign/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fd closure/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
foreign file/2
fd dom/2
foreign resource/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.........................................
..................................
fd global/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
format/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fd max/2
.........................................
format/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fd min/2
.........................................
format to chars/3
...............................
Predicate Index
413
I
100
233
233, 276
233
276
100
223
280
96
250
126
126
313
313
115
103
110
94
96
189
191
287
199
201
110, 113
207
211
if/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
format to chars/4
...............................
freeze/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
frozen/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
functor/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
G
369
119
120
111
in/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
in/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
in noblock/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
in set/2
.........................................
incore/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
independent set/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
garbage collect/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
garbage collect atoms/0
........................
garbage collection (statistics/2 option)
.....
gc (prolog flag) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
gc/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
gc margin (prolog flag) . . . . . . . . . . . . . . . . . . . . . . . . .
gc trace (prolog flag) . . . . . . . . . . . . . . . . . . . . . . . . . .
gcd/2, greatest common divisor . . . . . . . . . . . . . . . . . .
gen assoc/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
gen label/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
generate flinkage/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
125
125
109
106
125
106
106
95
191
219
373
88
88
191
191
193
237
199
219
113
191
192
87
87
213
108
10
79
110
indomain/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
inf/0, infinity
inf/2
..................................
............................................
initialization/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
initialization/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
insert constraint/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
insert constraint/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
instance/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
instantiation error/2 (error class) . . . . . . . . . . .
integer/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
integer/1, coercion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
is/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
get/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
is array/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
get/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
is assoc/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
get assoc/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
is fdset/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
get assoc/5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
get atts/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
get default db/1
................................
get from heap/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
get label/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
get mutable/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
get next assoc/4
................................
get prev assoc/4
................................
get0/1
............................................
get0/2
............................................
getrand/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
global stack (statistics/2 option) . . . . . . . . . . . .
GLOBALSTKSIZE (environment) . . . . . . . . . . . . . . . . . . . . .
goal expansion/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ground/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
H
is heap/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
is list/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
is mutable/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
is ordset/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
is queue/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
K
keysort/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
kill/2
...........................................
L
98
216
242
280
201
9, 155, 156
9
65
62, 121
124
90
9
232
232
233
233
91
91
labeling/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
labeling/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
last/2
...........................................
LD LIBRARY PATH (environment) . . . . . . . . . . .
LD RUN PATH (environment) . . . . . . . . . . . . . . . . . . . . . . . . .
halt/0
...........................................
halt/1
...........................................
heap (statistics/2 option) . . . . . . . . . . . . . . . . . . . . .
heap size/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
heap to list/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
124
124
109
199
199
68
126
216
216
230
leap (debugger command) . . . . . . . . . . . . . . . . . . . . . . . . . .
leash/1
......................................
length/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
library directory/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
LIBRARY PATH (environment) . . . . . . . . . . . . . . . . . . . . . . .
linda/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
help (debugger command) . . . . . . . . . . . . . . . . . . . . . . . . . .
linda/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
help/0
linda client/1
...........................................
host id/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
host name/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
hostname address/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..................................
linda timeout/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
line count/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
line position/2
..................................
414
SICStus Prolog
link foreign resource/6
133
212
192
287
199
207
219
104
104
53, 74
73
73
134
133
109
10
96
96
........................
56
105
48, 56
48, 56
95
54
123
123
123
150
150
150
150
123
124
124
150
150
mode/1 (declaration) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
list queue/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
module/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
list to assoc/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
module/2 (declaration) . . . . . . . . . . . . . . . . . . . . . . .
list to fdset/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
module/3 (declaration) . . . . . . . . . . . . . . . . . . . . . . .
list to heap/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
msb/1, most significant bit
list to ord set/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
multifile/1 (declaration) . . . . . . . . . . . . . . . . . . . . . . .
.....................
list to tree/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
muse flag/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
listing/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
muse flag/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
listing/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
muse flags/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
load/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
muse init lock() (C function)
..................
load files/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
muse lock() (C function) . . . . . . . . . . . . . . . . . . . . . . . .
load files/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
muse max workers() (C function) . . . . . . . . . . . . . . . .
load foreign files/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
muse num workers() (C function) . . . . . . . . . . . . . . . .
load foreign resource/1
muse sync/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
........................
local stack (statistics/2 option)
.............
muse trace/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
LOCALSTKSIZE (environment) . . . . . . . . . . . . . . . . . . . . . .
muse trace/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
log/1, logarithm
.................................
muse un lock() (C function) . . . . . . . . . . . . . . . . . . . . .
log/2, logarithm
.................................
muse worker id() (C function)
M
215
188
192
192
219
95
191
201
222, 226
123
250
280
201
201
109
50, 56
168
329
95
191
202
200
200
222, 226
222, 226
223, 227
250
280
216
94
make directory/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
make index:make library index/1
...............
..................
N
name/2
111
96
222, 226
222, 226
189
202
87
87
202
67
62, 121
266
92
125
202
110
67
63, 121
63, 121
314
62, 121
62, 121
202
202
202
202
123
110
112
369
...........................................
nan/0, not-a-number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
map assoc/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
neighbors/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
map assoc/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
neighbours/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
map tree/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
new array/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
max/2, maximum value . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nextto/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
max assoc/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nl/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
max list/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nl/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
max path/5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
no doubles/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
max workers (muse flag option) . . . . . . . . . . . . . . . . .
nodebug (debugger command) . . . . . . . . . . . . . . . . . . . . . .
maximize/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nodebug/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
maximize/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
noexpand/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
member/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nofileerrors/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
memberchk/2
nogc/0
.....................................
...........................................
memory (statistics/2 option) . . . . . . . . . . . . . . . . . . .
non member/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
meta predicate/1 (declaration) . . . . . . . . . . . . . .
nonvar/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
method/3 (Java method identifier)
nospy this (debugger command) . . . . . . . . . . . . . . . . . . .
.............
method expansion/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nospy/1
min/2, minimum value . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nospyall/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
......................................
min assoc/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
notify constrained/1
min list/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
notrace/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
min of heap/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nozip/0
min of heap/5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nth/3
............................................
min path/5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nth/4
............................................
min paths/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
min tree/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
...........................
......................................
nth0/3
...........................................
nth0/4
...........................................
minimize/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
num workers (muse flag option) . . . . . . . . . . . . . . . . .
minimize/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
number/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
mktemp/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
number chars/2
mod/2, integer remainder
number to chars/2
........................
..................................
...............................
Predicate Index
number to chars/3
numbervars/3
415
...............................
....................................
O
on exception/3
369
124
101
37, 124, 403
89
89
369
91
207
207
207
207
207
207
207
192
208
208
208
208
208
208
209
208
208
282
251, 260
100
66
233
..................................
op/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
predicate property/2
...........................
prefix/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
prepare foreign resource/3 . . . . . . . . . . . . . . . . . . . . .
prepare resource table/2 . . . . . . . . . . . . . . . . . . . . . . .
print (debugger command)
........................
print/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
open/3
............................................
print/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
open/4
............................................
print message/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
open chars stream/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
open null stream/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
profile data/4
..................................
profile reset/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord add element/3
...............................
program (statistics/2 option) . . . . . . . . . . . . . . . . . .
ord del element/3
...............................
project attributes/2
...........................
..................................
prolog flag/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord intersect/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
prolog flag/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord disjoint/2
ord intersection/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord intersection/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord intersection/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord list to assoc/2
.............................
ord member/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord seteq/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord setproduct/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord subset/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord subtract/3
..................................
ord symdiff/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord union/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord union/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ord union/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
order resource/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ordering/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
otherwise/0
.....................................
prolog load context/2 . . . . . . . . . . . . . . . . . . . . . . . . . . .
PROLOGINCSIZE (environment) . . . . . . . . . . . . . . . . . . . . .
PROLOGINITSIZE (environment)
...................
PROLOGKEEPSIZE (environment)
...................
PROLOGMAXSIZE (environment) . . . . . . . . . . . . . . . . . . . . .
prompt/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
public/1 (declaration)
..........................
put/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
put/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
put assoc/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
put atts/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
put label/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
put label/5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q
queue/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
out/1
queue head/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
P
57
57
222, 226
88
88
103
203
80
80
216
217
82
269
83
83
102
parallel/0 (declaration) . . . . . . . . . . . . . . . . . . . . . . . .
parallel/1 (declaration) . . . . . . . . . . . . . . . . . . . . . . . .
path/3
......................................
peek char/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
peek char/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
permission error/5 (error class)
66
211
211
211
211
211
212
quasi-skip (debugger command) . . . . . . . . . . . . . . . . . . .
out (debugger command) . . . . . . . . . . . . . . . . . . . . . . . . . . .
............................................
104
203
374
373
66
82
82
102
122
122
109
195
108
105
108
10
10
10
10
125
56
88
88
192
193
219
219
queue head list/3
...............................
queue last/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
queue last list/3
queue length/2
...............................
..................................
R
raise exception (debugger command)
.............
..............
raise exception/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
permutation/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
random/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
phrase/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
random/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
phrase/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
random ugraph/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pid/1
............................................
random wgraph/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
popen/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
randseq/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
portray/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
randset/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
portray/1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
range to fdset/2
portray clause/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
rd/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
portray clause/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
rd/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
portray message/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
rd noblock/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
................................
68
101
213
213
223
227
213
213
287
233
234
233
416
SICStus Prolog
reachable/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
223, 227
80
80
369
81
81
74
116
116
116
106
222, 226
125
277
314
203
217
100
103
75
68
68
17, 125
114
115
66
203
95
109
set default db/1
................................
237
91
91
91
118
213
122
217
217
217
9
110
95
106
95
66
88
88
88
88
217
229
229
229
229
229
229
229
230
230
98
75
75
108
139
136
136
142
140
139
137
137
143
10
142
128
128
128
142
144
read/1
............................................
read/2
set input/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
............................................
set output/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
read from chars/2
...............................
read term/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
read term/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
reconsult/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
recorda/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
recorded/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
recordz/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
redefine warnings (prolog flag) . . . . . . . . . . . . . . . .
reduce/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
reinitialise/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
relation/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
remove constraint/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
remove duplicates/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
rename file/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
repeat/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
representation error/3 (error class) . . . . . . . . . .
require/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
reset printdepth (debugger command)
............
reset subterm (debugger command) . . . . . . . . . . . . . . . .
restore/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
retract/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
retractall/1
....................................
retry (debugger command)
........................
reverse/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
round/1, function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
runtime (statistics/2 option) . . . . . . . . . . . . . . . . . .
S
set stream position/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
setof/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
setrand/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
shallow fails (profile data/4 option) . . . . . . . . .
shell/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
shell/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
shell/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SHLIB PATH (environment) . . . . . . . . . . . . . . . . . . . . . . . . . .
simple/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
sin/1, function
..................................
single var warnings (prolog flag) . . . . . . . . . . . . . .
sinh/1, function
.................................
skip (debugger command) . . . . . . . . . . . . . . . . . . . . . . . . . .
skip/1
............................................
skip/2
............................................
skip line/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
skip line/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
sleep/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
socket/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
socket accept/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
socket accept/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
socket bind/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
socket close/1
..................................
socket connect/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
socket listen/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
socket select/5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
socket select/6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
203
203
242
17, 125
17, 125
276
93
93
91
93
203
57
57
278
279
279
279
68
68
sort/2
............................................
same length/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
source file/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
same length/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
source file/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
sat/1
source info (prolog flag) . . . . . . . . . . . . . . . . . . . . . . .
............................................
save program/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
save program/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SP ATOM (C macro) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SP atom from string() (C function) . . . . . . . . . . . . .
scalar product/4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SP atom length() (C function)
..................
see/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SP close query() (C function)
..................
seeing/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SP compare() (C function) . . . . . . . . . . . . . . . . . . . . . . .
seek/4
............................................
SP COMPOUND (C macro) . . . . . . . . . . . . . . . . . . . . . . . . . . .
seen/0
............................................
SP cons functor() (C function) . . . . . . . . . . . . . . . . .
select/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SP cons list() (C function) . . . . . . . . . . . . . . . . . . . . .
sequential/0 (declaration) . . . . . . . . . . . . . . . . . . . . . .
SP continue() (C function)
sequential/1 (declaration) . . . . . . . . . . . . . . . . . . . . . .
SP CTYPE (environment) . . . . . . . . . . . . . . . . . . . . . . . . . . .
serialized/2
.....................
....................................
SP cut query() (C function) . . . . . . . . . . . . . . . . . . . . .
serialized precedence/3 . . . . . . . . . . . . . . . . . . . . . . . .
SP errno (C macro) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
serialized precedence resource/4 . . . . . . . . . . . . . .
SP ERROR (C macro) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
serialized resource/3
..........................
SP error message() (C function) . . . . . . . . . . . . . . . .
set printdepth (debugger command) . . . . . . . . . . . . . . .
SP event() (C function) . . . . . . . . . . . . . . . . . . . . . . . . .
set subterm (debugger command) . . . . . . . . . . . . . . . . . .
SP exception term() (C function) . . . . . . . . . . . . . . .
Predicate Index
SP FAILURE (C macro)
417
128
145
145
145
139
151
145
145
145
140
138
138
138
138
138
138
138
138
138
138
138
145
151
139
139
139
139
139
139
139
139
139
153
147
140
136
142
153
142
10, 127
141
141
145
137
137
137
137
137
137
137
............................
SP fclose() (C function) . . . . . . . . . . . . . . . . . . . . . . . .
SP fflush() (C function) . . . . . . . . . . . . . . . . . . . . . . . .
SP fgetc() (C function) . . . . . . . . . . . . . . . . . . . . . . . . .
SP FLOAT (C macro) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SP force interactive() (C function)
...........
SP fprintf() (C function) . . . . . . . . . . . . . . . . . . . . . . .
SP fputc() (C function) . . . . . . . . . . . . . . . . . . . . . . . . .
SP fputs() (C function) . . . . . . . . . . . . . . . . . . . . . . . . .
SP free() (C function) . . . . . . . . . . . . . . . . . . . . . . . . . .
SP get address() (C function)
..................
SP get arg() (C function) . . . . . . . . . . . . . . . . . . . . . . .
SP get atom() (C function) . . . . . . . . . . . . . . . . . . . . . .
SP get float() (C function) . . . . . . . . . . . . . . . . . . . . .
SP get functor() (C function)
..................
SP get integer() (C function)
..................
SP get list() (C function) . . . . . . . . . . . . . . . . . . . . . .
SP get list chars() (C function) . . . . . . . . . . . . . . .
SP get list n chars() (C function) . . . . . . . . . . . . .
SP get number chars() (C function) . . . . . . . . . . . . .
SP get string() (C function)
...................
SP getc() (C function) . . . . . . . . . . . . . . . . . . . . . . . . . .
SP initialize() (C function) . . . . . . . . . . . . . . . . . . .
SP INTEGER (C macro)
............................
SP is atom() (C function) . . . . . . . . . . . . . . . . . . . . . . .
SP is atomic() (C function) . . . . . . . . . . . . . . . . . . . . .
SP is compound() (C function)
..................
SP is float() (C function) . . . . . . . . . . . . . . . . . . . . . .
SP is integer() (C function)
...................
SP is list() (C function) . . . . . . . . . . . . . . . . . . . . . . .
SP is number() (C function) . . . . . . . . . . . . . . . . . . . . .
SP is variable() (C function)
..................
SP load() (C function) . . . . . . . . . . . . . . . . . . . . . . . . . .
SP make stream() (C function)
..................
SP malloc() (C function) . . . . . . . . . . . . . . . . . . . . . . . .
SP new term ref() (C function)
.................
SP next solution() (C function) . . . . . . . . . . . . . . . .
SP on fault() (C macro) . . . . . . . . . . . . . . . . . . . . . . . . .
SP open query() (C function)
...................
SP PATH (environment) . . . . . . . . . . . . . . . . . . . . . . .
SP pred() (C function) . . . . . . . . . . . . . . . . . . . . . . . . . .
SP predicate() (C function) . . . . . . . . . . . . . . . . . . . .
SP printf() (C function) . . . . . . . . . . . . . . . . . . . . . . . .
SP put address() (C function)
..................
SP put atom() (C function) . . . . . . . . . . . . . . . . . . . . . .
SP put float() (C function) . . . . . . . . . . . . . . . . . . . . .
SP put functor() (C function)
..................
SP put integer() (C function)
..................
SP put list() (C function) . . . . . . . . . . . . . . . . . . . . . .
SP put list chars() (C function) . . . . . . . . . . . . . . .
137
137
SP put string() (C function)
137
SP put term() (C function)
136
SP put variable() (C function)
137
SP putc() (C function)
145
SP puts() (C function)
145
SP qid (C type)
142
141
SP query() (C function)
SP query cut fail() (C function)
141
SP raise exception() (C function)
144
SP raise fault() (C function)
154
SP realloc() (C function)
140
SP register atom() (C function)
137
SP reinstall signal() (C function)
143
SP restore() (C function)
153
150
SP set interrupt hook (C function)
SP set memalloc hooks() (C function)
152
SP set read hook (C function)
150
SP set reinit hook (C function)
150
SP set tty() (C function)
147
SP set user stream hook() (C function)
148
SP set user stream post hook() (C function)
148
SP signal() (C function)
143
SP stream (C type)
145
SP string from atom() (C function)
136
SP SUCCESS (C macro)
128
SP term ref (C type)
127
SP term type() (C function)
139
SP unify() (C function)
140
SP unregister atom() (C function)
137
SP VARIABLE (C macro)
139
splfr (utility)
133
spmkds (utility)
156
spmkrs (utility)
154
spy this (debugger command)
67
spy/1
62, 121
spypoint condition/3
63
sqrt/1, square root
96
stack shifts (statistics/2 option)
109
statistics/0
108
statistics/2
108
stream code/2
145
stream interrupt/3
92
stream position/2
91
stream select/3
92
sublist/2
203
substitute/4
203
subsumes/2
205
subsumes chk/2
205
SP put list n chars() (C function) . . . . . . . . . . . . .
SP put number chars() (C function) . . . . . . . . . . . . .
...................
......................
.................
..........................
..........................
..................................
.........................
...............
..............
..................
.......................
................
.............
.......................
.............
...........
...................
................
.......................
.........
...
........................
...............................
.............
............................
.............................
.....................
.........................
..............
...........................
.................................
................................
................................
.....................
.........................................
.............................
..............................
............
....................................
....................................
....................................
...............................
................................
..................................
........................................
....................................
.......................................
..................................
418
SICStus Prolog
203
276
203
250
222, 226
103
107
217
217
217
103
107
suffix/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
told/0
sum/3
top sort/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
............................................
93
222, 226
107
61, 121
109
10
222, 226
222, 226
219
219
100
95
88
88
88
88
88
88
88
103
107
............................................
sum list/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
toplevel print options (prolog flag) . . . . . . . . . .
sup/2
trace/0
............................................
symmetric closure/2 . . . . . . . . . . . . . . . . . . . . . . . .
......................................
trail (statistics/2 option) . . . . . . . . . . . . . . . . . . . .
syntax error/5 (error class) . . . . . . . . . . . . . . . . . . .
TRAILSTKSIZE (environment) . . . . . . . . . . . . . . . . . . . . . .
syntax errors (prolog flag) . . . . . . . . . . . . . . . . . . . .
transitive closure/2 . . . . . . . . . . . . . . . . . . . . . . .
system/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
transpose/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
system/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
tree size/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
system/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
tree to list/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
system error/1 (error class) . . . . . . . . . . . . . . . . . . .
true/0
system type (prolog flag) . . . . . . . . . . . . . . . . . . . . . . .
truncate/1, function . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
T
ttyflush/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
88
tab/2
88
tan/1, function
95
tanh/1, function
95
taut/2
242
361
tcl delete/1
tcl eval/3
361
tcl new/1
361
tell/1
93
telling/1
93
term expansion/2
79
term expansion/4
79
term hash/2
206
term hash/4
206
term subsumer/3
205
term variables/2
206
time out/3
375
tk all events (tk do one event/1 option)
365
tk destroy window/1
365
tk do one event/0
365
tk do one event/1
365
tk dont wait (tk do one event/1 option)
365
tk file events (tk do one event/1 option)
365
tk idle events (tk do one event/1 option)
365
tk main loop/0
366
tk main window/1
365
tk make window exist/1
365
tk new/2
364
tk next event/2
366
tk next event/3
366
tk num main windows/1
365
tk timer events (tk do one event/1 option)
365
tk window events (tk do one event/1 option)
365
tk x events (tk do one event/1 option)
365
TMPDIR (environment)
10, 124
tmpnam/1
217
tab/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..............................................
...........................................
ttyget/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ttyget0/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ttynl/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..................................
ttyput/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.................................
ttyskip/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
...........................................
.....................................
.......................................
........................................
............................................
ttytab/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
type error/4 (error class)
.....................
typein module (prolog flag) . . . . . . . . . . . . . . . . . . . .
U
225
314
68
206
106
15, 120
103
133
113
75
75
75
69
89
79
90
329
82
102
79
79
103
126
107
72
126
107
.........................................
ugraph to wgraph/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.................................
unconstrained/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.................................
unify (debugger command)
........................
......................................
unify with occurs check/2
......................................
unknown (prolog flag) . . . . . . . . . . . . . . . . . . . . . . . . . . .
.................................
......................
unknown/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
................................
unknown predicate handler/3 . . . . . . . . . . . . . . . . . . . .
.......................................
unload foreign resource/1 . . . . . . . . . . . . . . . . . . . . . .
.......
update mutable/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.............................
use module/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
................................
use module/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
................................
use module/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
........
user:error exception/1
..........................
.....
user:file search path/2
.....
user:goal expansion/3
...................................
................................
..........................
.........................................
.........................
...........................
user:library directory/1 . . . . . . . . . . . . . . . . . . . . . . . .
user:method expansion/3 . . . . . . . . . . . . . . . . . . . . . . . .
user:portray/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
user:portray message/2 . . . . . . . . . . . . . . . . . . . . . . . . .
..................................
user:term expansion/2
...........................
..................................
user:term expansion/4
...........................
...........................
user:unknown predicate handler/3 . . . . . . . . . . . . . .
....
user:user help/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
...
user error (prolog flag) . . . . . . . . . . . . . . . . . . . . . . . .
.........
........................
.........................................
user error (stream alias) . . . . . . . . . . . . . . . . . . . . . . . .
user help/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
user input (prolog flag) . . . . . . . . . . . . . . . . . . . . . . . .
Predicate Index
419
72
107
72
user input (stream alias) . . . . . . . . . . . . . . . . . . . . . . . .
with output to chars/2 . . . . . . . . . . . . . . . . . . . . . . . . . .
user output (prolog flag) . . . . . . . . . . . . . . . . . . . . . . .
with output to chars/3 . . . . . . . . . . . . . . . . . . . . . . . . . .
user output (stream alias)
......................
V
var/1
with output to chars/4 . . . . . . . . . . . . . . . . . . . . . . . . . .
worker id (muse flag option)
110
205
194
123
107
126
126
221, 225
221
225
367
55
............................................
variant/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
verify attributes/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
...................
working directory/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
write (debugger command)
........................
write/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
write/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.....................
write canonical/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
version (prolog flag) . . . . . . . . . . . . . . . . . . . . . . . . . . .
write canonical/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
version (muse flag option)
version/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
version/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
vertices/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
vertices edges to ugraph/3 . . . . . . . . . . . . . . . . . . . . .
vertices edges to wgraph/3 . . . . . . . . . . . . . . . . . . . . .
view/1
...........................................
volatile/1 (declaration) . . . . . . . . . . . . . . . . . . . . . . . .
W
wait/2
...........................................
walltime (statistics/2 option)
................
wgraph to ugraph/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
when/2
...........................................
217
109
225
119
370
370
370
123
217
67
82
82
82
82
83
83
369
369
82
82
write term/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
write term/3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
write to chars/2
................................
write to chars/3
................................
writeq/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
writeq/2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
X
xref/1
...........................................
Z
377
65
61, 121
zip (debugger command) . . . . . . . . . . . . . . . . . . . . . . . . . . .
zip/0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
420
SICStus Prolog
Prolog Objects Method Index
421
Prolog Objects Method Index
N
new/1 (object method) . . . . . . . . . . . . . . . . . . . . . .
A
new/2 (object method) . . . . . . . . . . . . . . . . . . . . . .
343
344
344
344
344
344
339, 343
343
339, 343
343
339, 343
343
341
343
343
343
abolish/0 (object method) . . . . . . . . . . . . . . . . . . . . . .
ancestor/1 (utility method) . . . . . . . . . . . . . . . . . . . .
ancestor/2 (utility method) . . . . . . . . . . . . . . . . . . . .
ancestors/1 (utility method) . . . . . . . . . . . . . . . . . . .
ancestors/2 (utility method) . . . . . . . . . . . . . . . . . . .
and cast/2 (utility method) . . . . . . . . . . . . . . . . . . . .
assert/1 (object method) . . . . . . . . . . . . . . . . . . .
assert/2 (object method)
.......................
asserta/1 (object method)
.................
asserta/2 (object method) . . . . . . . . . . . . . . . . . . . . . .
assertz/1 (object method)
.................
assertz/2 (object method) . . . . . . . . . . . . . . . . . . . . . .
attributes/1 (universal method)
...............
augment/1 (object method) . . . . . . . . . . . . . . . . . . . . . .
augmenta/1 (object method) . . . . . . . . . . . . . . . . . . . . .
augmentz/1 (object method) . . . . . . . . . . . . . . . . . . . . .
D
O
339, 342
339, 342
object (built-in object)
.......................
object/1 (object method)
.......................
objects/1 (utility method) . . . . . . . . . . . . . . . . . . . . .
or cast/2 (utility method)
.....................
P
342
342
343
344
pltrace-port-arrow-assoc . . . . . . . . . . . . . . . . . . . . . . . .
prolog-align-comments-flag
.....................
prolog-electric-dot-flag . . . . . . . . . . . . . . . . . . . . . . . .
prolog-electric-newline-flag . . . . . . . . . . . . . . . . . . .
prolog-electric-underscore-flag . . . . . . . . . . . . . . . .
prolog-hungry-delete-key-flag . . . . . . . . . . . . . . . . . .
prolog-imenu-flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
prolog-indent-mline-comments-flag
prolog-indent-width
.............
.............................
prolog-info-predicate-index . . . . . . . . . . . . . . . . . . . .
344
344
344
344
336, 342
337, 342
344
344
prolog-keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
descendant/1 (utility method) . . . . . . . . . . . . . . . . . .
prolog-object-end-to-0-flag . . . . . . . . . . . . . . . . . . . .
descendant/2 (utility method) . . . . . . . . . . . . . . . . . .
prolog-old-sicstus-keys-flag . . . . . . . . . . . . . . . . . . .
descendants/1 (utility method)
................
prolog-paren-indent
descendants/2 (utility method)
................
prolog-parse-mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
dynamic/0 (object method)
.................
dynamic/1 (object method)
.................
prolog-system
.............................
....................................
prolog-system-version . . . . . . . . . . . . . . . . . . . . . . . . . . .
dynamic methods/1 (utility method) . . . . . . . . . . . .
prolog-underscore-wordchar-flag . . . . . . . . . . . . . . . .
dynamic objects/1 (utility method) . . . . . . . . . . . .
prolog-use-prolog-tokenizer-flag
G
prolog-use-sicstus-sd . . . . . . . . . . . . . . . . . . . . . . . . . . .
get/1 (inlined method) . . . . . . . . . . . . . . . . . . . . . . . . . .
get/1 (object method) . . . . . . . . . . . . . . . . . . . . . . . . . . .
H
has attribute/1 (object method) . . . . . . . . . . . . . . . .
has instance/1 (object method) . . . . . . . . . . . . . . . . .
I
instance/1 (object method) . . . . . . . . . . . . . . . . . . . . .
M
methods/1 (utility method) . . . . . . . . . . . . . . . . . . . . .
341
342
..............
R
restart/0 (utility method) . . . . . . . . . . . . . . . . . . . . .
retract/1 (object method) . . . . . . . . . . . . . . . . . . . . . .
retractall/1 (object method) . . . . . . . . . . . . . . . . . . .
342
342
S
self/1 (inlined method)
........................
self/1 (object method) . . . . . . . . . . . . . . . . . . . . . . . . . .
342
344
set/1 (inlined method) . . . . . . . . . . . . . . . . . . . . . . . . . .
set/1 (object method) . . . . . . . . . . . . . . . . . . . . . . . . . . .
static/0 (object method)
.......................
static/1 (object method)
.......................
static methods/1 (utility method)
.............
22
23
23
23
23
23
24
23
22
24
23
23
23
22
23
22
22
24
23
22
344
343
343
341
342
341
342
342
342
344
422
SICStus Prolog
static objects/1 (utility method)
sub/1
.............
............................................
sub/1 (object method) . . . . . . . . . . . . . . . . . . . . . . . . . . .
subs/1 (utility method)
super (keyword)
........................
.................................
super/1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
344
342
335
343
336
342
super/2 (universal method)
................
334, 341
343
supers/1 (utility method) . . . . . . . . . . . . . . . . . . . . . .
U
update/1 (object method)
.......................
utility (built-in object) . . . . . . . . . . . . . . . . . . . . . .
343
343
Concept Index
423
Concept Index
A
abort
access driven programming
alias, le name
all solutions
ancestor goal
anonymous variable
ANSI conformance
anti-unications
API
arguments, command-line
arithmetic
arity
arrays
assignment, destructive
association lists
asynchronous predicate
asynchronously, calling Prolog
atom
attribute declaration
attributed variables
attributes, object
attributes, object, implementation
17
340
72, 89
117
64
28
6, 127
205
127
7
94
28
189
113
191
46
142
28
193
193
327
345
..............................................
.......................
.................................
......................................
......................................
................................
.............................
..................................
..............................................
...........................
.........................................
...............................................
............................................
...........................
...................................
............................
....................
..............................................
..............................
...............................
.................................
................
B
CHR debugging predicates
CHR spy-points
clause
clause, guarded
command
command-line arguments
communication
comparing terms
compilation
compiling
compound term
computation rule
conformance, ANSI
considerations for fcompile
consistent store
constant
constraint
constraint store
constraint, global
consulting
context, Directory
context, File
context, Module
contradictory store
conversions, term
coroutining
counter
cross reference
current input stream
current output stream
cut
cut, green
cut, red
cyclic term
317
319
30
179
8
7
231
97
73
7
27, 28
35
6, 127
58
283
27
272
283
285
7, 11, 52, 73
108
108
108
283
135
119
121
377
72
72
36, 44
37
37
36, 81, 83
........................
..................................
.............................................
...................................
...........................................
............................
...................................
...................................
........................................
...........................................
................................
..................................
.............................
.........................
...................................
...........................................
........................................
...................................
.................................
...............................
................................
......................................
..................................
...............................
backtracking
binary trees
blackboard
block declaration
blocked goal
body
break
built-in predicate
35
219
46, 116
55
64
30
16
32
.......................................
......................................
....................................
..................................
.......................................
..............................................
..............................................
..................................
C
.................................
.......................................
...........................................
....................................
...............................
.............................
.............................................
..........................................
............................................
.................................
call, last
calling Prolog asynchronously
calling Prolog from C
cavalier predicate
character I/O
character set
choicepoint
CHR control ow model
CHR debugging messages
CHR debugging options
178
142
141
46
87
10, 398
43
317
320
320
..........................................
.....................
.............................
..................................
......................................
..................................
........................................
..........................
.........................
...........................
D
daemon
database
database, default
db-spec
DCG
debug options
debugging
debugging messages
debugging predicates
340
115, 235
237
236
76
65
59
63
61
...........................................
.....................................
.................................
...........................................
..............................................
.....................................
.........................................
................................
...............................
424
SICStus Prolog
declaration, attribute
declaration, block
declaration, dynamic
declaration, meta-predicate
declaration, mode
declaration, module
declaration, multile
declaration, operator
declaration, predicate
declaration, public
declaration, volatile
declarations
declarative semantics
deep failure
default database
denite clause
delegation
delegation, message
destructive assignment
development kernel
development system
dierential inheritance
directive
domain variable
domain, nite
domain-consistent
domain-disentailed
domain-entailed
dynamic declaration
dynamic linking
dynamic method
dynamic object
dynamic predicate
193
55
54
50, 56
56
56
52, 54
53
53
56
55
53
33
122, 178
237
27
327
332
113
373
6, 156
335
8, 12
272
272
284
284
284
54
129
337
327, 336, 338
54
.............................
..................................
...............................
.....................
..................................
................................
...........................
...............................
..............................
.................................
................................
.......................................
..............................
..................................
..................................
.....................................
........................................
...............................
............................
...............................
............................
............................
........................................
..................................
.....................................
................................
................................
..................................
...............................
..................................
..................................
..........................
.................................
E
emacs interface
end of le
end of line
end of stream
EOF
error handling
error, syntax
escape sequence
exception handling
exception handling in C
execution
execution proling
execution, nested
exiting
expansion, macro
18
5
92
91
5
69, 101
14
106, 401
69, 101
144
15
121
16
16
79
....................................
...........................................
.........................................
......................................
................................................
................................
.......................................
..............................
............................
...........................
..........................................
................................
..................................
.............................................
..................................
expansion, module name
exported predicate
external database
external storage
.......................
49, 56
47
235
235
.................................
.................................
..................................
F
failure, deep
failure, shallow
fcompile, considerations for
FD predicate
FD set
le
le name alias
le, module
lename
Finding Libraries
Finding Shared Objects
nite domain
ag, Prolog
oat
oundering
foreign language interface
foreign resource
foreign resource, linked
function prototype
functor
.................................
...............................
122, 178
122, 178
58
293
287
71
72, 89
48
72
9
9
272
105, 108
27
35
127, 128
128
128, 135
6
28
........................
.....................................
...........................................
................................................
..................................
........................................
...........................................
...................................
.............................
.....................................
..................................
...............................................
........................................
....................
...................................
.......................
..................................
............................................
G
garbage collection
gauge
GCLA
generalized Horn clause language
generic object
generic object, implementation
global constraint
goal
goal, ancestor
goal, blocked
grammar rule
graphical tracing
graphs, unweighted
graphs, weighted
green cut
guarded clause
.................................
.............................................
............................................
..................
....................................
....................
..................................
...............................................
106
367
359
359
331
348
285
30
64
64
76
44
221
225
37
179
......................................
.......................................
......................................
...................................
...............................
..................................
..........................................
....................................
Concept Index
425
H
handling, interrupt
handling, signal
head
heaps
hidden module
hierarchy, object
hook functions for I/O
hook functions for reinitialization
hook predicate
Horn clause
...............................
...................................
142
142
30
199
48
333
150
150
54
27
...............................................
.............................................
.....................................
..................................
............................
.................
.....................................
........................................
L
last call
layout term
library
Linda
line, end of
linked foreign resource
linking, dynamic
linking, static
list
lists
loading
logic programming
178
81
187
231
92
128, 135
129
129, 154
29
201
51, 73
1
..........................................
........................................
............................................
............................................
........................................
.......................
..................................
................................
................................................
..............................................
I
.........................................
I/O hook functions
if-then-else
implicit parallelism
importation
imported predicate
indexed term
indexical
indexing
information, source
inheritance
inheritance by overriding
inheritance, dierential
inheritance, multiple
input
input stream, current
instance variable
instances
instances, implementation
integer
interface, emacs
interface, foreign language
interoperability
interrupt handling
interrupt, stream
interval-consistent
interval-disentailed
interval-entailed
150
178
41
49
47
236
291
178
19, 108
327, 333, 334
327
335
334
71
72
351
341
346
27
18
127, 128
127
142
92
284
284
284
...............................
.......................................
................................
........................................
.................................
.....................................
.........................................
..........................................
............................
..............................
..........................
...........................
..............................
..............................................
..............................
..................................
.........................................
.........................
.............................................
....................................
....................
...................................
................................
..................................
................................
................................
..................................
K
kernel, development
kernel, runtime
keyboard
...............................
...................................
373
373
5
...........................................
..................................
M
macro expansion
message delegation
message sending
messages, suppressing
meta-logical predicate
meta-predicate declaration
method
method, dynamic
mixing C and Prolog
mode declaration
mode spec
module declaration
module le
module name expansion
module system
module, hidden
module, object
module, source
module, type-in
multile declaration
multiple inheritance
Muse model
Must tracing
mutable term
79
332
332
101
110
50, 56
327
337
127
56
5
56
48
49, 56
47
48
345
47
47, 107
52, 54
334
43
45
113
...................................
...............................
..................................
.............................
.............................
.....................
...........................................
.................................
..............................
..................................
..........................................
................................
........................................
........................
.....................................
....................................
....................................
.....................................
...............................
............................
..............................
.......................................
.......................................
.....................................
N
nested execution
non-unit clause
notation
...................................
....................................
............................................
16
31
5
426
SICStus Prolog
O
104
1
340
327
105, 108
327
6
56
47
....................................
object
object hierarchy
object module
object, dynamic
object, generic
object, generic, implementation
object, parameterized
object, parameterized, implementation
object, static
object-oriented programming
occurs-check
operating system
operator declaration
operators
ordered sets
output
output stream, current
overriding, inheritance by
327
333
345
327, 336, 338
331
348
331
348
327, 336
327
36, 205
215
53
37, 124, 403
207
71
72
327
............................................
..................................
....................................
.........................
....................................
...................
.............................
............
.................................
.....................
..................................
.................................
...............................
.................................
......................................
.............................................
.............................
.........................
P
program state
programming in logic
programming, access driven
programming, object-oriented
Prolog ag
prototype
prototype, function
public declaration
public predicate
...............................
.......................
.....................
...................................
.........................................
.................................
..................................
....................................
Q
query
queues
8
211
...............................................
............................................
R
random numbers
reading in
reconsult
recursion, tail
red cut
reference, term
reication
reinitialization
reinitialization hook functions
repeat loop
resource, foreign
restoring
rule, computation
rule, grammar
rule, search
running
runtime kernel
runtime system
213
11
68
178
37
127
275
125, 126
150
93
128
17
35
76
35
7
373
6, 151
..................................
.........................................
..........................................
.....................................
............................................
...................................
parallel declaration
parallelism, implicit
parameterized object
parameterized object, implementation
post, to
predicate
predicate declaration
predicate spec
predicate, asynchronous
predicate, cavalier
predicate, dynamic
predicate, exported
predicate, FD
predicate, hook
predicate, imported
predicate, private
predicate, public
predicate, undened
predicate, volatile
priority queues
private predicate
procedural semantics
procedure box
procedure call
procedure denition
process communication
proling
proling, execution
program
57
41
331
348
273
30, 32
53
5
46
46
54
47
293
54
47
47
47
15, 106
55
199
47
34
59
35
34
229, 231
367
121
30
................................
................................
.............................
.............
...........................................
.......................................
...............................
......................................
............................
.................................
.................................
................................
.....................................
....................................
................................
..................................
...................................
...........................
..................................
...................................
...................................
...............................
.....................................
.....................................
................................
.......................
..........................................
...............................
...........................................
........................................
...............................
.....................
........................................
..................................
...........................................
..................................
.....................................
........................................
.............................................
....................................
................................
S
saved state
saving
scheduler
scheduling
search rule
self
semantics
sending, message
sentence
sequence, escape
sequential declaration
sets
shallow failure
side eects
signal handling
17
17
43
43
35
332
33, 34
332
30
106, 401
57
207
122, 178
44
142
........................................
.............................................
..........................................
.........................................
.........................................
...............................................
......................................
.................................
...........................................
.............................
..............................
..............................................
...............................
.........................................
...................................
Concept Index
427
sockets
solutions, all
source information
source module
SP term ref (C type)
speculative work
spy-point
stand-alone application
standard order
state, program
state, saved
static linking
static object
store, consistent
store, constraint
store, contradictory
stream
stream, end of
string
subsumption
suppressing messages
suspension
suspension, voluntary
synchronization
synchronized
syntax error
system, development
system, operating
system, runtime
229
117
19, 108
47
136
44
62
151
97
104
17
129, 154
327, 336
283
283
283
71
91
30
205
101
44
44
231
44
14
6
215
6
...........................................
......................................
............................
.....................................
.............................
...................................
..........................................
...........................
.....................................
....................................
........................................
.................................
.................................
..................................
..................................
...............................
.............................................
.....................................
..............................................
......................................
.............................
.........................................
..............................
...................................
.......................................
.......................................
................................
.................................
.....................................
T
tail recursion
term
term comparison
term conversions
term I/O
term reference
.....................................
178
27
97
135
80
127
...............................................
...................................
..................................
..........................................
....................................
term, compound
term, cyclic
term, indexed
term, layout
term, mutable
terms
top level
tracing, graphical
trees
type-in module
28
36, 81, 83
236
81
113
205
8
44
219
47, 107
...................................
.................................
.....................................
.......................................
....................................
.............................................
............................................
..................................
.............................................
................................
U
ugraph
undened predicate
unication
unit clause
unweighted graphs
user
221
15, 103, 106
35
30
221
12
...........................................
.......................
.........................................
.........................................
................................
...............................................
V
variable
variable, domain
variable, instance
variables, attributed
Visandor tracing
volatile declaration
volatile predicate
voluntary suspension
........................................
27, 28
272
351
193
45
55
55
44
..................................
.................................
..............................
...................................
.................................
..................................
...............................
W
WAM
weighted graphs
wgraph
work
work, speculative
worker
...........................................
1, 43
225
225
43
44
43
..................................
...........................................
...............................................
..................................
.............................................
428
SICStus Prolog
i
Table of Contents
Introduction
Acknowledgments
Notational Conventions
...........................................
......................................
................................
Keyboard Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Mode Spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Predicate Spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Development and Runtime Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ISO Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1 How to Run Prolog
................................
1
3
5
5
5
5
6
6
6
7
1.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1.1 Finding Libraries at Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.2 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2 Reading in Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3 Inserting Clauses at the Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4 Directives: Queries and Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.1 Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.2 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.5 Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.6 Undened Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.7 Program Execution And Interruption . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.8 Exiting From The Top-Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.9 Nested Executions|Break and Abort . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.10 Saving and Restoring Program States . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.11 Emacs Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.11.1 Customizing Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.11.2 Basic Conguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.11.3 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.11.4 Mode Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.11.5 Conguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.11.6 Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.11.6.1 Font-locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.11.6.2 Auto-ll mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.11.6.3 Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.11.6.4 Changing Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
ii
2 The Prolog Language
SICStus Prolog
.............................
27
2.1 Syntax, Terminology and Informal Semantics . . . . . . . . . . . . . . . . . . . . 27
2.1.1 Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1.1.1 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1.1.2 Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1.1.3 Atoms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.1.1.4 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.1.1.5 Compound Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.1.2 Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.2 Declarative Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.3 Procedural Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.4 Occurs-Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.5 The Cut Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.6 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.7 Syntax Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.8 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3 Running Prolog in Parallel
.......................
41
..............................
47
................................
51
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Running a Parallel Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3 Main Issues of Parallel Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3.1 The Muse Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3.2 Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3.3 Cut, Side Eects and Suspension . . . . . . . . . . . . . . . . . . . . . .
3.4 Visualization Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.1 Must . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.2 Visandor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.3 Vimust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5 Programming Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4 The Module System
4.1
4.2
4.3
4.4
4.5
4.6
41
41
43
43
43
44
44
45
45
45
45
Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Module Prexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Dening Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Importation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Module Name Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Meta-Predicate Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5 Loading Programs
5.1 Predicates which Load Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.1 Multile Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.2 Dynamic Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.3 Volatile Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.4 Block Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.5 Meta-Predicate Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.6 Module Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.7 Public Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
52
53
54
54
55
55
56
56
56
iii
5.2.8 Mode Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.2.9 Parallel and Sequential Declarations . . . . . . . . . . . . . . . . . . . 57
5.3 Considerations for File-To-File Compilation . . . . . . . . . . . . . . . . . . . . . 58
6 Debugging
6.1
6.2
6.3
6.4
6.5
6.6
6.7
.........................................
59
The Procedure Box Control Flow Model . . . . . . . . . . . . . . . . . . . . . . . .
Basic Debugging Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Spy-points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Format of Debugging messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Options Available during Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Consulting during Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Catching Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7 Built-In Predicates
................................
59
61
62
63
65
68
69
71
7.1 Input / Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
7.1.1 Reading-in Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.1.2 Term and Goal Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
7.1.3 Input and Output of Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
7.1.4 Character Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.1.5 Stream I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
7.1.6 DEC-10 Prolog File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
7.1.7 An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
7.2 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
7.3 Comparison of Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
7.4 Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
7.5 Error and Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
7.6 Information about the State of the Program . . . . . . . . . . . . . . . . . . . . 104
7.7 Meta-Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
7.8 Modication of Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
7.9 Modication of the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
7.10 Internal Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
7.11 Blackboard Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
7.12 All Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
7.13 Coroutining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
7.14 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
7.15 Execution Proling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
7.16 Muse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
7.17 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
8 Mixing C and Prolog
............................
127
8.1 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
8.2 Calling C from Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
8.2.1 Foreign Resource and Conversion Declarations . . . . . . . . . 128
8.2.2 Static and Dynamic linking . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
8.2.3 Conversion Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
8.2.4 Conversions between Prolog Arguments and C Types . . . 130
8.2.5 Interface Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
8.2.6 Init and Deinit Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
iv
SICStus Prolog
8.2.7 Creating the Linked Foreign Resource . . . . . . . . . . . . . . . . . 135
8.3 Support Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
8.3.1 Creating and Manipulating SP term refs . . . . . . . . . . . . . . 136
8.3.2 Creating Prolog Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
8.3.3 Accessing Prolog Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
8.3.4 Testing Prolog Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
8.3.5 Unifying and Comparing Terms . . . . . . . . . . . . . . . . . . . . . . . 140
8.3.6 Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
8.4 Calling Prolog from C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
8.4.1 Finding One Solution of a Call . . . . . . . . . . . . . . . . . . . . . . . 141
8.4.2 Finding Multiple Solutions of a Call . . . . . . . . . . . . . . . . . . . 141
8.4.3 Calling Prolog Asynchronously . . . . . . . . . . . . . . . . . . . . . . . 142
8.4.4 Exception Handling in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
8.5 SICStus Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
8.5.1 Prolog Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
8.5.2 Dening a New Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
8.5.2.1 Low Level I/O Functions . . . . . . . . . . . . . . . . . . . . 146
8.5.2.2 Installing a New Stream . . . . . . . . . . . . . . . . . . . . . 147
8.5.2.3 Internal Representation . . . . . . . . . . . . . . . . . . . . . . 147
8.5.3 Hookable Standard Streams . . . . . . . . . . . . . . . . . . . . . . . . . . 148
8.5.3.1 Writing User-stream Hooks . . . . . . . . . . . . . . . . . . 148
8.5.3.2 Writing User-stream Post-hooks . . . . . . . . . . . . . . 149
8.5.3.3 User-stream Hook Example . . . . . . . . . . . . . . . . . . 149
8.6 Muse Support Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
8.7 Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
8.8 Runtime Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
8.8.1 Initializing the Prolog Engine . . . . . . . . . . . . . . . . . . . . . . . . . 151
8.8.2 Loading Prolog Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
8.8.3 Creating the Executable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
8.8.4 Running the Executable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
8.9 Development Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
8.10 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
8.10.1 Train Example (connections) . . . . . . . . . . . . . . . . . . . . . . . . 157
8.10.2 I/O on Lists of Character Codes . . . . . . . . . . . . . . . . . . . . . 160
8.10.3 Exceptions from C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
8.10.4 Stream Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
9 Mixing Java and Prolog
.........................
167
9.1 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.2 Calling Java from Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.2.1 Static and Dynamic Linking . . . . . . . . . . . . . . . . . . . . . . . . . .
9.2.2 Declarating Java-methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.3 Conversions between Prolog Arguments and Java Types . . . . . . . . .
9.3.1 Calling Java from Prolog: An Example . . . . . . . . . . . . . . . .
9.4 Calling Prolog from Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.5 Jasper Package Class Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.6 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.7 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
167
167
168
168
168
172
173
175
176
176
10 Programming Tips and Examples
10.1
10.2
10.3
10.4
10.5
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
..............
177
Programming Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Last Call Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
If-Then-Else Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Programming Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
10.5.1 Simple List Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
10.5.2 Family Example (descendants) . . . . . . . . . . . . . . . . . . . . . . 179
10.5.3 Association List Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . 180
10.5.4 Dierentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
10.5.5 Use of Meta-Logical Predicates . . . . . . . . . . . . . . . . . . . . . . 181
10.5.6 Use of Term Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
10.5.7 Prolog in Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
10.5.8 Translating English Sentences into Logic Formulae . . . . 183
10.5.9 Muse FLI Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
The Prolog Library
Array Operations
Association Lists
Attributed Variables
Heap Operations
List Operations
Term Utilities
Ordered Set Operations
Queue Operations
Random Number Generator
Operating System Utilities
Updatable Binary Trees
Unweighted Graph Operations
Weighted Graph Operations
Socket I/O
.............................
...............................
................................
............................
................................
.................................
...................................
........................
..............................
....................
.....................
........................
.................
....................
......................................
187
189
191
193
199
201
205
207
211
213
215
219
221
225
229
v
vi
26 Linda|Process Communication
SICStus Prolog
................
231
26.1 Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
26.2 Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
27 External Storage of Terms (External Database)
.................................................
27.1
27.2
27.3
27.4
27.5
27.6
235
Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Current Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The DB-Spec|Informal Description . . . . . . . . . . . . . . . . . . . . . . . . . .
Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
An Example Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Db-Spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28 Boolean Constraint Solver
......................
235
235
236
236
238
239
241
28.1 Solver Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28.2.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28.2.2 Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28.2.3 Example 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28.2.4 Example 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
242
242
242
243
243
244
29 Constraint Logic Programming over Rationals or
Reals
247
............................................
29.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Referencing this Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
29.2 Solver Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Notational Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
29.2.1 Solver Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
29.2.2 Unication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
29.2.3 Feedback and Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
29.3 Linearity and Nonlinear Residues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
29.3.1 How Nonlinear Residues are made to disappear . . . . . . . 254
29.3.2 Isolation Axioms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
29.4 Numerical Precision and Rationals . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
29.5 Projection and Redundancy Elimination . . . . . . . . . . . . . . . . . . . . . . 259
29.5.1 Variable Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
29.5.2 Turning Answers into Terms . . . . . . . . . . . . . . . . . . . . . . . . . 261
29.5.3 Projecting Inequalities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
29.6 Why Disequations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
29.7 Syntactic Sugar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
29.7.1 Monash Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
29.7.1.1 Compatibility Notes . . . . . . . . . . . . . . . . . . . . . . . . 267
29.8 A Mixed Integer Linear Optimization Example . . . . . . . . . . . . . . . . 268
29.9 Implementation Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
29.9.1 Fragments and Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
29.9.1.1 Rationals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
29.9.1.2 Partial Evaluation, Compilation . . . . . . . . . . . . . 270
vii
29.9.1.3 Asserting with Constraints . . . . . . . . . . . . . . . . . . 270
29.9.2 Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
30 Constraint Logic Programming over Finite
Domains
........................................
271
30.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Referencing this Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
30.2 Solver Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
30.2.1 Posting Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
30.2.2 A Constraint Satisfaction Problem . . . . . . . . . . . . . . . . . . . 273
30.2.3 Reied Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
30.3 Available Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
30.3.1 Arithmetic Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
30.3.2 Membership Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
30.3.3 Propositional Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
30.3.4 Combinatorial Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . 277
30.3.5 User-Dened Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
30.4 Enumeration Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
30.5 Statistics Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
30.6 Answer Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
30.7 The Constraint System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
30.7.1 Denitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
30.7.2 Pitfalls of Interval Reasoning . . . . . . . . . . . . . . . . . . . . . . . . 284
30.8 Dening Global Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
30.8.1 The Global Constraint Programming Interface . . . . . . . . 285
30.8.2 Reection Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
30.8.3 FD Set Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
30.8.4 A Global Constraint Example . . . . . . . . . . . . . . . . . . . . . . . 288
30.9 Dening Primitive Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
30.9.1 Indexicals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
30.9.2 Range Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
30.9.3 Term Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
30.9.4 Monotonicity of Indexicals . . . . . . . . . . . . . . . . . . . . . . . . . . 293
30.9.5 FD predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
30.9.6 Execution of Propagating Indexicals . . . . . . . . . . . . . . . . . 296
30.9.7 Execution of Checking Indexicals . . . . . . . . . . . . . . . . . . . . 296
30.9.8 Goal Expanded Constraints . . . . . . . . . . . . . . . . . . . . . . . . . 297
30.10 Example Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
30.10.1 Send More Money . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
30.10.2 N Queens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
30.10.3 Cumulative Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
30.11 Syntax Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Syntax of Indexicals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Syntax of Arithmetic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Operator Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
viii
31 Constraint Handling Rules
SICStus Prolog
.....................
305
..................................
327
Copyright . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
31.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
31.2 Introductory Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
31.3 CHR Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
31.3.1 Loading the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
31.3.2 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
31.3.3 Constraint Handling Rules, Syntax . . . . . . . . . . . . . . . . . . . 309
31.3.4 How CHR work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
31.3.5 Pragmas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
31.3.6 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
31.3.7 Built-In Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
31.3.8 Consulting and Compiling Constraint Handlers . . . . . . . 314
31.3.9 Compiler-generated Predicates . . . . . . . . . . . . . . . . . . . . . . . 314
31.3.10 Operator Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
31.3.11 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
31.4 Debugging CHR Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
31.4.1 Control Flow Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
31.4.2 CHR Debugging Predicates . . . . . . . . . . . . . . . . . . . . . . . . . 317
31.4.3 CHR Spy-points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
31.4.4 CHR Debugging Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
31.4.5 CHR Debugging Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
31.5 Programming Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
31.6 Constraint Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
31.7 Backward Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
32 Prolog Objects
32.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
32.2 Declared Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
32.2.1 Object Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
32.2.2 Method Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
32.2.3 Generic Objects for Easy Reuse . . . . . . . . . . . . . . . . . . . . . . 331
32.3 Self, Message Sending, and Message Delegation . . . . . . . . . . . . . . . . 332
32.4 Object Hierarchies, Inheritance, and Modules . . . . . . . . . . . . . . . . . 333
32.4.1 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
32.4.2 Dierential Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
32.4.3 Use of Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
32.4.4 Super and Sub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
32.4.5 The Keyword Super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
32.4.6 Semantic Links to Other Objects . . . . . . . . . . . . . . . . . . . . 336
32.4.7 Dynamically Declared Objects . . . . . . . . . . . . . . . . . . . . . . . 336
32.4.8 Dynamic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
32.4.9 Inheritance of Dynamic Behavior . . . . . . . . . . . . . . . . . . . . 338
32.5 Creating Objects Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
32.5.1 Object Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
32.5.2 Method Additions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
32.5.3 Parameter Passing to New Objects . . . . . . . . . . . . . . . . . . . 340
32.6 Access Driven Programming|Daemons . . . . . . . . . . . . . . . . . . . . . . . 340
32.7 Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
ix
32.8 Built-In Objects and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.8.1 Universal Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.8.2 Inlined Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.8.3 The Proto-Object "object" . . . . . . . . . . . . . . . . . . . . . . . . . .
32.8.4 The built-in object "utility" . . . . . . . . . . . . . . . . . . . . . . . . .
32.9 Expansion to Prolog Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.9.1 The Inheritance Mechanism . . . . . . . . . . . . . . . . . . . . . . . . .
32.9.2 Object Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.9.3 Object Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.9.4 The Object Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.9.5 The Method Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.9.6 Parameter Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.10 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.10.1 Classication of Birds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32.10.2 Inheritance and Delegation . . . . . . . . . . . . . . . . . . . . . . . . .
32.10.3 Prolog++ programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33 Generalized Horn Clause Language
34 Tcl/Tk Interface
............
................................
359
361
34.1 Prolog to Tcl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
34.1.1 Command Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
34.2 Tcl to Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
34.3 Tk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
35 The Gauge Proling Tool
36 I/O on Lists of Character Codes
37 Jasper
38 Glue Code Generator
39 Timeout Predicate
40 Cross Reference Producer
Summary of Built-In Predicates
Full Prolog Syntax
......................
...............
...........................................
...........................
..............................
......................
....................
..................................
341
341
341
342
343
345
345
345
346
346
346
348
350
350
351
354
361
362
363
364
367
369
371
373
375
377
379
395
Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Syntax of Sentences as Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Syntax of Terms as Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Syntax of Tokens as Character Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
395
396
397
398
401
402
x
Standard Operators
References
Predicate Index
Prolog Objects Method Index
Concept Index
SICStus Prolog
.................................
...........................................
.....................................
.......................
.......................................
403
405
409
421
423
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertisement