Node:Top, Next:, Previous:(dir), Up:(dir)

SICStus Prolog

This manual documents SICStus Prolog 3.10.0, December 2002.

Prolog is a simple but powerful programming language developed at the University of Marseille, as a practical tool for programming in logic. 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.


Node:Intro, Next:, Previous:Top, Up:Top

Introduction

Prolog is a simple but powerful programming language developed at the University of Marseille [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 benefit 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 field of Logic Programming see [Kowalski 79]. See Prolog Intro.

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 file-to-file. 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. As of release 3.8, SICStus Prolog provides two execution modes: the iso mode, which is fully compliant with the International Standard ISO/IEC 13211-1 (PROLOG: Part 1--General Core); and the sicstus mode, which is largely compatible with e.g. C-Prolog and Quintus Prolog, supports code written in earlier versions of SICStus Prolog.


Node:Acknowledgments, Next:, Previous:Intro, Up:Top

Acknowledgments

The following people have contributed to the development of SICStus Prolog:

Jonas Almgren, Johan Andersson, Stefan Andersson, Nicolas Beldiceanu,
Tamás Benko'', Kent Boortz, Dave Bowen, Per Brand, Göran Båge,
Mats Carlsson, Per Danielsson, Jesper Eskilson, Lena Flood, György
Gyaraki, Dávid Hanák, Seif Haridi, Ralph Haygood, Christian
Holzbaur, Tom Howland, Key Hyckenberg, Per Mildner, Richard O'Keefe,
Greger Ottosson, László Péter, Dan Sahlin, Peter Schachte, Rob
Scott, Thomas Sjöland, Péter Szeredi, Tamás Szeredi, Peter Van
Roy, Johan Widén, David Warren, and Emil Åström.

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

See CLPQR, for acknowledgments relevant to the clp(Q,R) constraint solver.

See CLPFD, for acknowledgments relevant to the clp(FD) constraint solver.

UNIX is a trademark of Bell Laboratories. MSDOS and Windows is a trademark of Microsoft Corp. OS/2 is a trademark of IBM Corp.


Node:Notation, Next:, Previous:Acknowledgments, Up:Top

Notational Conventions


Node:Keyboard Characters, Next:, Previous:Notation, Up:Notation

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.

Throughout, we will assume that ^D is the EOF character (it's usually ^Z under Windows) and that ^C is the interrupt character. In most contexts, the term end_of_file terminated by a full stop (.) can be typed instead of the EOF character.


Node:Mode Spec, Next:, Previous:Keyboard Characters, Up:Notation

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 prefixes. The argument is subject to module name expansion (see Meta Exp).
+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 ?.


Node:Development and Runtime Systems, Next:, Previous:Mode Spec, Up:Notation

Development and Runtime Systems

The full Prolog system with top-level, compiler, debugger etc. is known as the development system.

It is possible to link user-written C code with a subset of SICStus Prolog to create runtime systems. When introducing a built-in predicate, any limitations on its use in runtime systems will be mentioned.


Node:Function Prototypes, Next:, Previous:Development and Runtime Systems, Up:Notation

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.


Node:ISO Compliance, Previous:Function Prototypes, Up:Notation

ISO Compliance

SICStus Prolog provides two execution modes: the iso mode, which is fully compliant with the International Standard ISO/IEC 13211-1 (PROLOG: Part 1--General Core), and the sicstus mode, which supports code written in earlier versions of SICStus Prolog. The execution mode can be changed using the Prolog flag language; see State Info. Note, however, that SICStus Prolog does not offer a strictly conforming mode which rejects uses of implementation specific features.

To aid programmers who wish to write standard compliant programs, built-in predicates that are part of the ISO Prolog Standard are annotated with [ISO] in this manual. If such a predicate behaves differently in sicstus mode, an appropriate clarification is given. For the few predicates that have a completely different meaning in the two modes, two separate descriptions are given. The one for the iso mode is annotated with [ISO only], while the sicstus mode version is annotated with [SICStus only].


Node:Glossary, Next:, Previous:Notation, Up:Top

Glossary

abolish
To abolish a predicate is to retract all the predicate's clauses and to remove all information about it from the Prolog system, to make it as if that predicate had never existed.
advice-point
A special case of breakpoint, the advice breakpoint. It is distinguished from spypoints in that it is intended for non-interactive debugging, such as checking of program invariants, collecting information, profiling, etc.
alphanumeric
An alphanumeric character is any of the lowercase characters from a to z, the uppercase characters from A to Z, the numerals from 0 to 9, or underscore (_).
ancestors
An ancestor of a goal is any goal which the system is trying to solve when it calls that goal. The most distant ancestor is the goal which was typed at the top-level prompt.
anonymous
An anonymous variable is one which has no unique name, and whose value is therefore inaccessible. An anonymous variable is denoted by an underscore (_).
argument
See predicate, structure, and arity.
arity
The arity of a structure is its number of arguments. For example, the structure customer(jones,85) has an arity of 2.
atom
A character sequence used to uniquely denote some entity in the problem domain. A number is not an atom. Unless character escapes have been switched off, examples of legal atoms are:
hello   *   :=   '#$%'   'New York'   'don\'t'

See Atoms. Atoms are recognized by the built-in predicate atom/1. Each Prolog atom is represented internally by a unique integer, represented in C as an SP_atom.

atomic term
Synonym for constant.
backtrace
A collection of information on the control flow of the program, gathered by the debugger. Also the display of this information produced by the debugger. The backtrace includes data on goals that were called but not exited and also on goals that exited nondeterministically.
backtracking
The process of reviewing the goals that have been satisfied and attempting to resatisfy these goals by finding alternative solutions.
binding
The process of assigning a value to a variable; used in unification.
blocked goal
A goal which is suspended because it is not instantiated enough.
body
The body of a clause consists of the part of a Prolog clause following the :- symbol.
breakpoint
A description of certain invocations in the program where the user wants the debugger to stop, or to perform some other actions. A breakpoint is specific if it applies to the calls of a specific predicate, possibly under some conditions; otherwise, it is generic. Depending on the intended usage, breakpoints can be classified as debugger breakpoints, also known as spypoints, or advice breakpoints, also called advice-points; see Advanced Debugging.
breakpoint spec
A term describing a breakpoint. Composed of a test part, specifying the conditions under which the breakpoint should be applied, and an action part, specifying the effects of the breakpoint on the execution.
buffer
A temporary workspace in Emacs that contains a file being edited.
built-in predicate
A predicate that comes with the system and which does not have to be explicitly loaded before it is used.
callable term
A callable term is either a compound term or an atom. Callable terms are recognized by the built-in predicate callable/1.
character code
An integer which is the numeric representation of a character. SICStus Prolog supports character codes in the range 0..2147483647 (i.e. 2^31-1). However, to be able to input or output character codes larger than 255, one needs to use the appropriate wide character external encoding.
character code set
A subset of the set {0, ..., 2^31-1} that can be handled by the external encoding. SICStus Prolog assumes that the character code set is an extension of the ASCII code set, i.e. it includes codes 0..127, and these codes are interpreted as ASCII characters
character-conversion mapping
SICStus Prolog maintains a character-conversion mapping which is used while reading terms and programs. Initially, the mapping prescribes no character conversions. It can be modified by the built-in predicate char_conversion(In, Out), following which In will be converted to Out. Character coversion can be switched off by the char_conversion Prolog flag.
character-type mapping
A function mapping each element of the character code set to one of the character categories (layout, letter, symbol-char, etc.), required for parsing tokens.
choicepoints
A memory block representing outstanding choices for some goals or disjunctions.
clause
A fact or a rule. A rule comprises a head and a body. A fact consists of a head only, and is equivalent to a rule with the body true.
conjunction
A series of goals connected by the connective "and" (that is, a series of goals whose principal operator is ,).
compactcode
Virtual code representation of compiled code. A reasonable compromise between performance and space requirement. A valid value for the compiling Prolog flag.
compile
To load a program (or a portion thereof) into Prolog through the compiler. Compiled code runs more quickly than interpreted code, but you cannot debug compiled code in as much detail as interpreted code.
compound term
A compound term is a name which is an atom together with one or more arguments. For example, in the term father(X), father is the name, and X is the first and only argument. The argument to a compound term can be another compound term, as in father(father(X)). Compound terms are recognized by the built-in predicate compound/1.
console-based executable
An executable which inherits the standard streams from the process that invoked it, e.g. a UNIX shell or a DOS-prompt.
constant
An integer (for example: 1, 20, -10), a floating-point number (for example: 12.35), or an atom. Constants are recognized by the built-in predicate atomic/1.
consult
To load a program (or a portion thereof) into Prolog through the interpreter. Interpreted code runs more slowly than compiled code, but you can debug interpreted code in more detail than compiled code.
creep
What the debugger does in trace mode, also known as single-stepping. It goes to the next port of a procedure box and prints the goal, then prompts you for input. See Basic Debug.
cursor
The point on the screen at which typed characters appear. This is usually highlighted by a line or rectangle the size of one space, which may or may not blink.
cut
Written as !. A built-in predicate that succeeds when encountered; if backtracking should later return to the cut, the goal that matched the head of the clause containing the cut fails immediately.
database
The Prolog database comprises all of the clauses which have been loaded or asserted into the Prolog system or which have been asserted, except those clauses which have been retracted or abolished.
database reference
A compound term denoting a unique reference to a dynamic clause.
debug
A mode of program execution in which the debugger stops to print the current goal only at procedures which have spypoints set on them (see leap).
debugcode
Interpreted representation of compiled code. A valid value for the compiling Prolog flag.
declaration
A declaration looks like a directive, but is not executed but conveys information about procedures about to be loaded.
deinit function
A function in a foreign resource which is called prior to unloading the resource.
determinate
A procedure is determinate if it can supply only one answer.
development system
A stand-alone executable with the full programming environment, including top-level, compiler, debugger etc. The default sicstus executable is a development system; new development systems containing pre-linked foreign resources can also be created.
directive
A directive is a goal preceded by the prefix operator :-, whose intuitive meaning is "execute this as a query, but do not print out any variable bindings."
disjunction
A series of goals connected by the connective "or" (that is, a series of goals whose principal operator is ;).
dynamic predicate
A predicate that can be modified while a program is running. A predicate must explicitly be declared to be dynamic or it must be added to the database via one of the assertion predicates.
encoded string
A sequence of bytes representing a sequence of possibly wide character codes, using the UTF-8 encoding.
escape sequence
A sequence of characters beginning with \ inside certain syntactic tokens (see Escape Sequences).
export
A module exports a procedure so that other modules can import it.
external encoding (of wide characters)
A way of encoding sequences of wide characters as sequences of (8-bit) bytes, used in stream input and output.
fact
A clause with no conditions--that is, with an empty body. A fact is a statement that a relationship exists between its arguments. Some examples, with possible interpretations, are:
king(louis, france).   % Louis was king of France.
have_beaks(birds).     % Birds have beaks.
employee(nancy, data_processing, 55000).
                     % Nancy is an employee in the
                     % data processing department.

fastcode
Native code representation of compiled code. The fastest, but also the most space consuming representation. Only available for Sparc platforms. A valid value for the compiling Prolog flag.
file specification
An atom or a compound term denoting the name of a file. The rules for mapping such terms to absolute file names are described in Input Output.
floundered query
A query where all unsolved goals are blocked.
foreign predicate
A predicate that is defined in a language other than Prolog, and explicitly bound to Prolog predicates by the Foreign Language Interface.
foreign resource
A named set of foreign predicates.
functor
The functor of a compound term is its name and arity. For example, the compound term foo(a,b) is said to have "the functor foo of arity two", which is generally written foo/2.

The functor of a constant is the term itself paired with zero. For example, the constant nl is said to have "the functor nl of arity zero", which is generally written nl/0.

garbage collection
The freeing up of space for computation by making the space occupied by terms which are no longer available for use by the Prolog system.
generalized predicate spec
A generalized predicate spec is a term of one of the following forms. It is always interpreted wrt. a given module context:
Name
all predicates called Name no matter what arity, where Name is an atom for a specific 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
Name/[Arity,...,Arity]
the predicates of that name with one of the given arities, or
Module:Spec
specifying a particular module Module instead of the default module, where Module is an atom for a specific module or a variable for all modules, or
[Spec,...,Spec]
the set of all predicates covered by the Specs.

glue code
Interface code between the Prolog engine and foreign predicates. Automatically generated by the foreign language interface as part of building a linked foreign resource.
goal
A simple goal is a predicate call. When called, it will either succeed or fail.

A compound goal is a formula consisting of simple goals connected by connectives such as "and" (,) or "or" (;).

A goal typed at the top-level is called a query.

ground
A term is ground when it is free of (unbound) variables. Ground terms are recognized by the built-in predicate ground/1.
head
The head of a clause is the single goal which will be satisfied if the conditions in the body (if any) are true; the part of a rule before the :- symbol. The head of a list is the first element of the list.
hook predicate
A hook predicate is a procedure that somehow alters or customizes the behavior of a hookable predicate.
hookable predicate
A hookable predicate is a built-in predicate whose behavior is somehow altered or customized by a hook predicate.
import
Exported procedures in a module can be imported by other modules. Once a procedure has been imported by a module, it can be called, or exported, as if it were defined in that module.

There are two kinds of importation: procedure-importation, in which only specified procedures are imported from a module; and module-importation, in which all the predicates made exported by a module are imported.

indexing
The process of filtering a set of potentially matching clauses of a procedure given a goal. For interpreted and compiled code, indexing is done on the principal functor of the first argument. Indexing is coarse w.r.t. big integers and floats.
init function
A function in a foreign resource which is called upon loading the resource.
initialization
An initialization is a goal that is executed when the file in which the initialization is declared is loaded, or upon reinitialization. A initialization is declared as a directive :- initialization Goal.
instantiation
A variable is instantiated if it is bound to a non-variable term; that is, to an atomic term or a compound term.
internal encoding (of wide characters)
A way of encoding wide character sequences internally within the Prolog system. SICStus Prolog uses a technique known as the UTF-8 encoding for this purpose.
interpret
Load a program or set of clauses into Prolog through the interpreter (also known as consulting). Interpreted code runs more slowly than compiled code, but more extensive facilities are available for debugging interpreted code.
invocation box
Same as procedure box.
leap
What the debugger does in debug mode. The debugger shows only the ports of procedures that have spypoints on them. It then normally prompts you for input, at which time you may leap again to the next spypoint (see trace).
leashing
Determines how frequently the debugger will stop and prompt you for input when you are tracing. A port at which the debugger stops is called a "leashed port".
linked foreign resource
A foreign resource that is ready to be installed in an atomic operation, normally represented as a shared object or DLL.
list
A list is written as a set of zero or more terms between square brackets. If there are no terms in a list, it is said to be empty, and is written as []. In this first set of examples, all members of each list are explicitly stated:
[aa, bb,cc]  [X, Y]  [Name]  [[x, y], z]

In the second set of examples, only the first several members of each list are explicitly stated, while the rest of the list is represented by a variable on the right-hand side of the "rest of" operator, |:

[X | Y]  [a, b, c | Y]  [[x, y] | Rest]

| is also known as the "list constructor." The first element of the list to the left of | is called the head of the list. The rest of the list, including the variable following | (which represents a list of any length), is called the tail of the list.

load
To load a Prolog clause or set of clauses, in source or binary form, from a file or set of files.
meta-call
The process of interpreting a callable term as a goal. This is done e.g. by the built-in predicate call/1.
meta-predicate
A meta-predicate is one which calls one or more of its arguments; more generally, any predicate which needs to assume some module in order to operate is called a meta-predicate. Some arguments of a meta-predicate are subject to module name expansion.
mode spec
A term name(arg, ..., arg) where each arg denotes how that argument should be instantiated in goals. See Mode Spec.
module
A module is a set of procedures in a module-file. The name of a module is an atom. Some procedures in a module are exported. The default module is user.
module name expansion
The process by which certain arguments of meta-predicates get prefixed by the source module. See Meta Exp.
module-file
A module-file is a file that is headed with a module declaration of the form:
:- module(ModuleName, ExportedPredList).

which must appear as the first term in the file.

multifile predicate
A predicate whose definition is to be spread over more than one file. Such a predicate must be preceded by an explicit multifile declaration in all files containing clauses for it.
mutable term
A special form of compound term which is subject to destructive assignment. See Modify Term. Mutable terms are recognized by the built-in predicate is_mutable/1.
name clash
A name clash occurs when a module attempts to define or import a procedure that it has already defined or imported.
occurs-check
A test to ensure that binding a variable does not bind it to a term where that variable occurs.
one-char atom
An atom which consists of a single character.
operator
A notational convenience that allows you to express any compound term in a different format. For example, if likes in
| ?- likes(sue, cider).

is declared an infix operator, the query above could be written:

| ?- sue likes cider.

An operator does not have to be associated with a predicate. However, certain built-in predicates are declared as operators. For example,

| ?- =..(X, Y).

can be written as

| ?- X =.. Y.

because =.. has been declared an infix operator.

Those predicates which correspond to built-in operators are written using infix notation in the list of built-in predicates at the beginning of the part that contains the reference pages.

Some built-in operators do not correspond to built-in predicates; for example, arithmetic operators. See Standard Operators for a list of built-in operators.

pair
A compound term K-V. Pairs are used by the built-in predicate keysort/2 and by many library modules.
parent
The parent of the current goal is a goal which, in its attempt to obtain a successful solution to itself, is calling the current goal.
port
One of the five key points of interest in the execution of a Prolog procedure. See Procedure Box for a definition.
pre-linked foreign resource
A linked foreign resource that is linked into a stand-alone executable as part of building the executable.
precedence
A number associated with each Prolog operator, which is used to disambiguate the structure of the term represented by an expression containing a number of operators. Operators of lower precedence are applied before those of higher precedence; the operator with the highest precedence is considered the principal functor of the expression. To disambiguate operators of the same precedence, the associativity type is also necessary. See Operators.
predicate
A functor that specifies some relationship existing in the problem domain. For example, < /2 is a built-in predicate specifying the relationship of one number being less than another. In contrast, the functor + /2 is not (normally used as) a predicate.

A predicate is either built-in or is implemented by a procedure.

predicate spec
A compound term name/arity or module:name/arity denoting a predicate.
procedure
A set of clauses in which the head of each clause has the same predicate. For instance, a group of clauses of the following form:
connects(san_francisco, oakland, bart_train).
connects(san_francisco, fremont, bart_train).
connects(concord, daly_city, bart_train).

is identified as belonging to the procedure connects/3.

procedure box
A way of visualizing the execution of a Prolog procedure, A procedure box is entered and exited via ports.
profiledcode
Virtual code representation of compiled code, instrumented for profiling. A valid value for the compiling Prolog flag.
profiling
The process of gathering execution statistics of parts of the program, essentially counting the times selected program points have been reached.
program
A set of procedures designed to perform a given task.
PO file
A PO (Prolog object) file contains a binary representation of a set of modules, predicates, clauses and directives. They are portable between different platforms, except between 32-bit and 64-bit platforms. They are created by save_files/2, save_modules/2, and save_predicates/2.
QL file
A QL (quick load) file contains an intermediate representation of a compiled source code file. They are portable between different platforms, but less efficient than PO files, and are therefore obsolescent. They are created by fcompile/1.
query
A query is a question put by the user to the Prolog system. A query is written as a goal followed by a full-stop in response to the Prolog system prompt. For example,
| ?- father(edward, ralph).

refers to the predicate father/2. If a query has no variables in it, the system will respond either yes or no. If a query contains variables, the system will try to find values of those variables for which the query is true. For example,

| ?- father(edward, X).
X = ralph

After the system has found one answer, the user can direct the system to look for additional answers to the query by typing ;.

recursion
The process in which a running procedure calls itself, presumably with different arguments and for the purpose of solving some subset of the original problem.
region
The text between the cursor and a previously set mark in an Emacs buffer.
rule
A clause with one or more conditions. For a rule to be true, all of its conditions must also be true. For example,
has_stiff_neck(ralph) :-
   hacker(ralph).

This rule states that if the individual ralph is a hacker, then he must also have a stiff neck. The constant ralph is replaced in

has_stiff_neck(X) :-
   hacker(X).

by the variable X. X unifies with anything, so this rule can be used to prove that any hacker has a stiff neck.

runtime kernel
A shared object or DLL containing the SICStus virtual machine and other runtime support for stand-alone executables.
runtime system
A stand-alone executable with a restricted set of built-in predicates and no top-level. Stand-alone applications containing debugged Prolog code and destined for end-users are typically packaged as runtime systems.
extended runtime system
A stand-alone executable. In addition to the normal set of built-in runtime system predicates, extended runtime systems include the compiler. Extended runtime systems require the extended runtime library, available from SICS as an add-on product.
saved-state
A snapshot of the state of Prolog saved in a file by save_program/[1,2].
semantics
The relation between the set of Prolog symbols and their combinations (as Prolog terms and clauses), and their meanings. Compare syntax.
sentence
A clause or directive.
side-effect
A predicate which produces a side-effect is one which has any effect on the "outside world" (the user's terminal, a file, etc.), or which changes the Prolog database.
simple term
A simple term is a constant or a variable. Simple terms are recognized by the built-in predicate simple/1.
small integer
An integer in the range [-2^25,2^25-1] on 32-bit platforms, or [-2^56,2^56-1] on 64-bit platforms.
source code
The human-readable, as opposed to the machine-executable, representation of a program.
source module
The module which is the context of a file being loaded. For module-files, the source module is named in the file's module declaration. For other files, the source module is inherited from the context.
SP_term_ref
A "handle" object providing an interface from C to Prolog terms.
spypoint
A special case of breakpoint, the debugger breakpoint, intended for interactive debugging. Its simplest form, the plain spypoint instructs the debugger to stop at all ports of all invocations of a specified predicate. Conditional spypoints apply to a single predicate, but are more selective: the user can supply applicability tests and prescribe the actions to be carried out by the debugger. A generic spypoint is like a conditional spypoint, but not restricted to a single predicate. See Advanced Debugging.
stand-alone executable
A binary program which can be invoked from the operating system, containing the SICStus runtime kernel. A stand-alone executable is a development system (e.g. the default sicstus executable), or a runtime system. Both kinds are created by the application builder. A stand-alone executable does not itself contain any Prolog code; all Prolog code must be loaded upon startup.
static predicate
A predicate that can be modified only by being reloaded or by being abolished. See dynamic predicate.
stream
An input/output channel. See Input Output.
stream alias
A name assigned to a stream at the time of opening, which can be referred to in I/O predicates. Must be an atom. There are also three predefined aliases for the standard streams: user_input, user_output and user_error.
stream position
A term representing the current position of a stream. This position is determined by the current byte, character and line counts and line position. Standard term comparison on stream position terms works as expected. When SP1 and SP2 refer to positions in the same stream, SP1@<SP2 if and only if SP1 is before SP2 in the stream. You should not otherwise rely on their internal representation.
string
A special syntactic notation which is, by default, equivalent to a list of character codes e.g.
"SICStus"

By setting the Prolog flag double_quotes, the meaning of strings can be changed. With an appropriate setting, a string can be made equivalent to a list of one-char atoms, or to an atom. Strings are not a separate data type.

subterm selector
A list of argument positions selecting a subterm within a term (i.e. the subterm can be reached from the term by successively selecting the argument positions listed in the selector). Example: within the term q, (r, s; t) the subterm s is selected by the selector [2, 1, 2].
syntax
The part of Prolog grammar dealing with the way in which symbols are put together to form legal Prolog terms. Compare semantics.
system encoding (of wide characters)
A way of encoding wide character strings, used or required by the operating system environment.
term
A basic data object in Prolog. A term can be a constant, a variable, or a compound term.
trace
A mode of program execution in which the debugger creeps to the next port and prints the goal.
type-in module
The module which is the context of queries.
unblocked goal
A goal which is not blocked.
unbound
A variable is unbound if it has not yet been instantiated.
unification
The process of matching a goal with the head of a clause during the evaluation of a query, or of matching arbitrary terms with one another during program execution.

The rules governing the unification of terms are:


unit clause
See fact.
UTF-8 encoding
See internal encoding
variable
A logical variable is a name that stands for objects that may or may not be determined at a specific point in a Prolog program. When the object for which the variable stands is determined in the Prolog program, the variable becomes instantiated. A logical variable may be unified with a constant, a compound term, or another variable. Variables become uninstantiated when the procedure they occur in backtracks past the point at which they were instantiated.

Variables may be written as any sequence of alphanumeric characters starting with either a capital letter or _; e.g.

X   Y   Z   Name   Position   _c   _305  One_stop

See Variables.

volatile
Predicate property. The clauses of a volatile predicate are not saved in saved-states.
windowed executable
An executable which pops up its own window when run, and which directs the standard streams to that window.
zip
Same as leap mode, except no debugging information is collected while zipping.


Node:Run Intro, Next:, Previous:Glossary, Up:Top

How to Run Prolog

SICStus Prolog offers 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 file or a number of files using one of the standard text editors. The Prolog interpreter can then be instructed to read in programs from these files; this is called consulting the file. Alternatively, the Prolog compiler can be used for compiling the file.


Node:Start, Next:, Previous:Run Intro, Up:Run Intro

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 Emacs Interface). From a shell, SICStus Prolog is started by typing:

% sicstus [options] [-a argument...]

where flags have the following meaning:


-f
Fast start. Don't read any initialization file (~/.sicstusrc or ~/.sicstus.ini) on startup. If the flag is omitted and this file exists, SICStus Prolog will consult it on startup after running any initializations and printing the version banners.
-i
Forced interactive. Prompt for user input, even if the standard input stream does not appear to be a terminal.
--iso
--sicstus
Start up in ISO Prolog mode or SICStus Prolog mode respectively. The language mode is set before any prolog-file or initialization file is loaded and any saved-state is restored.
-m
For compatibility with previous versions. Ignored.
-l prolog-file
Ensure that the file prolog-file is loaded on startup. This is done before any initialization file is loaded. Only one -l argument is allowed.
-r saved-state
Restore the saved state saved-state on startup. This is done before any prolog-file or initialization file is loaded. Only one -r argument is allowed.
--goal Goal
Read a term from the text Goal and pass the resulting term to call/1 after all files have been loaded. As usual Goal should be terminated by a full stop (.). Only one --goal argument is allowed.
-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.
-B[abspath]
Creates a saved state for a development system. This option is not needed for normal use. If abspath is given, it specifies 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.

Under UNIX, a saved state file can be executed directly by typing:

% file argument...

This is equivalent to:

% sicstus -r file [-a argument...]

NOTE: As of release 3.7, saved-states do not store the complete path of the binary sp.exe. Instead, they call the main executable 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 flags are not available when executing saved states--all the command-line arguments are treated as Prolog arguments.

The development system checks that a valid SICStus license exists and responds with a message of identification and the prompt | ?- as soon as it is ready to accept input, thus:

SICStus 3.9.1 ...
Licensed to SICS
| ?-

At this point the top-level is expecting input of a query. You cannot type in clauses or directives immediately (see Inserting Clauses). While typing in a query, the prompt (on following lines) becomes . That is, the | ?- appears only for the first line of the query, and subsequent lines are indented.


Node:Environment Variables, Previous:Start, Up:Start

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_CSETLEN
Selects the sub-code-set lengths when the EUC character set is used. For the details, see WCX Environment Variables.
SP_CTYPE
Selects the appropriate character set standard: The supported values are euc (for EUC), utf8 (for Unicode) and iso_8859_1 (for ISO 8859/1). The latter is the default. For the details, see WCX Environment Variables.
SP_PATH
This environment variable can be used to specify the location of the Runtime Library (corresponding to the third argument to SP_initialize()). In most cases there is no need to use it. See Setting SP_PATH under UNIX, for more information.
TMPDIR
If set, indicates the pathname where temporary files 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
Defines 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.

In addition the following environment variables are set automatically on startup.

SP_APP_DIR
The absolute path to the directory that contains the executable. Also available as the application file search alias.
SP_RT_DIR
The full path to the directory that contains the SICStus run-time. If the application has linked statically to the SICStus run-time then SP_RT_DIR is the same as SP_APP_DIR. Also available as the runtime file search alias.
SP_LIBRARY_DIR
The absolute path to the directory that contains the SICStus library files. Also available as the initial value of the library file search alias.

Send bug reports to sicstus-support@sics.se or use the form at http://www.sics.se/sicstus/bugreport/bugreport.html. Bugs tend actually to be fixed 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 sicstus-users@sics.se is a mailing list for communication among users and implementors. To subscribe, write a message to majordomo@sics.se with the following line in the message body:

subscribe sicstus-users


Node:Reading In, Next:, Previous:Start, Up:Run Intro

Reading in Programs

A program is made up of a sequence of clauses and directives. The clauses of a predicate do not have to be immediately consecutive, but remember that their relative order may be important (see Procedural).

To input a program from a file file, just type the filename inside list brackets (followed by . and <RET>), thus:

| ?- [file].

This instructs the interpreter to read in (consult) the program. Note that it may be necessary to enclose the filename file in single quotes to make it a legal Prolog atom; e.g.

| ?- ['myfile.pl'].

| ?- ['/usr/prolog/somefile'].

The specified file is then read in. Clauses in the file are stored so that they can later be interpreted, while any directives are obeyed as they are encountered. When the end of the file is found, the system displays on the standard error stream the time spent. This indicates the completion of the query.

Predicates that expect the name of a Prolog source file as an argument use absolute_file_name/3 (see Stream Pred) to look up the file. If no explicit extension is given, this predicate will look for a file with the default extension .pl added as well as for a file without extension. There is also support for libraries.

In general, this query can be any list of filenames, such as:

| ?- [myprog,extras,tests].

In this case all three files would be consulted.

The clauses for all the predicates in the consulted files 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.


Node:Inserting Clauses, Next:, Previous:Reading In, Up:Run Intro

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 query:

| ?- [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:

% consulted user in module user, 20 msec 200 bytes


Node:Queries and Directives, Next:, Previous:Inserting Clauses, Up:Run Intro

Queries and Directives

Queries and directives are ways of directing the system to execute some goal or goals.

In the following, suppose that list membership has been defined by loading the following clauses from a file:

member(X, [X|_]).
member(X, [_|L]) :- member(X, L).

(Notice the use of anonymous variables written _.)


Node:Queries, Next:, Previous:Queries and Directives, Up:Queries and Directives

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) specified in a query can be satisfied, 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 final value of each variable is displayed (except for variables whose names begin with _). Thus the query

| ?- 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 Procedural) looking for alternative solutions. If no further solutions can be found it outputs no.

While the variable bindings are displayed, all variables occurring in the values are replaced by terms of the form '$VAR'(N) to yield friendlier variable names. Such names come out as a sequence of letters and digits preceded by _. The outcome of some queries is shown below.

| ?- 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
| ?-

Directives are like queries except that:

  1. Variable bindings are not displayed if and when the directive succeeds.
  2. You are not given the chance to backtrack through other solutions.


Node:Directives, Previous:Queries, Up:Queries and Directives

Directives

Directives start with the symbol :-. Any required output must be programmed explicitly; e.g. the directive:

:- member(3, [1,2,3]), write(ok).

asks the system to check whether 3 belongs to the list [1,2,3]. Execution of a directive terminates when all the goals in the directive have been successfully executed. Other alternative solutions are not sought. If no solution can be found, the system prints:

* Goal - goal failed

as a warning.

The principal use for directives (as opposed to queries) is to allow files 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 effect, i.e. you don't want terminal interaction in the middle of consulting the file. A useful example would be the use of a directive in a file which consults a whole list of other files, e.g.

:- [ bits, bobs, main, tests, data, junk ].

If a directive like this were contained in the file 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 directives is not normally very important. At top-level you should just type queries normally. In a file, queries are in fact treated as directives, i.e. if you wish to execute some goals then the directive in the file must be preceded by :- or ?-; otherwise, it would be treated as a clause.


Node:Syntax Errors, Next:, Previous:Queries and Directives, Up:Run Intro

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
! , or ) expected in arguments
! in line 5
! member ( X , X
! <<here>>
! $ L ) .

if $ has not been declared as an infix 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.

| ?- listing(member/2).

NOTE: The built in predicates read/[1,2] normaly raise an exception on syntax errors (see Exception). The behavior is controlled by the flag syntax_errors (see prolog_flag/3).


Node:Undefined Predicates, Next:, Previous:Syntax Errors, Up:Run Intro

Undefined Predicates

There is a difference between predicates that have no definition and predicates that have no clauses. The latter case is meaningful e.g. for dynamic predicates (see Declarations) that clauses are being added to or removed from. There are good reasons for treating calls to undefined predicates as errors, as such calls easily arise from typing errors.

The system can optionally catch calls to predicates that have no definition. First the user defined predicate user:unknown_predicate_handler/3 (see Exception) is called. If undefined or if the call fails the action is governed by the state of the unknown/2 flag which can be:

trace
which causes calls to undefined predicates to be reported and the debugger to be entered at the earliest opportunity.
error
which causes calls to such predicates to raise an exception (the default state). See Exception.
warning
which causes calls to such predicates to display a warning message and then fail.
fail
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) unifies 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 flag unknown (see prolog_flag/3).


Node:Execution, Next:, Previous:Undefined Predicates, Up:Run Intro

Program Execution And Interruption

Execution of a program is started by giving the system a query which contains a call to one of the program's predicates.

Only when execution of one query is complete does the system become ready for another query. However, one may interrupt the normal execution of a query by typing ^C. This ^C interruption has the effect 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 development systems are:

a
aborts the current computation.
c
continues the execution.
e
exits from SICStus Prolog, closing all files.
h
?
lists available commands.
b
invokes a recursive top-level.
d
z
t
switch on the debugger. See Debug Intro.

If the standard input stream is not connected to the terminal, e.g. by redirecting standard input to a file 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.


Node:Exiting, Next:, Previous:Execution, Up:Run Intro

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.


Node:Nested, Next:, Previous:Exiting, Up:Run Intro

Nested Executions--Break

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 queries to solve goals etc. This is achieved by issuing the query (see Execution):

| ?- 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:

% End break


Node:Saving, Next:, Previous:Nested, Up:Run Intro

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 current character-conversion mapping, the values of all writable Prolog flags except debugging, source_info, and the user_* stream aliases (see State Info), any blackboard data (see Blackboard Primitives), internal database data (see Database), and profiling data (see Profiling), but no information for source-linked debugging.

To save a program into a file File, type the following query. On UNIX platforms, the file 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 file File, the following query 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 files needed upon restore are typically different at restore time from their save time values. To solve this problem, certain atoms will be relocated during restore as follows:

The purpose of this procedure is to be able to build and deploy an application consisting of a saved state and other files as a directory tree with the saved state at the root: as long as the other files maintain their relative position in the deployed copy, they can still be found upon restore.

NOTE: Foreign resources, see Calling C, 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 define init and deinit functions to take special action upon loading and unloading; see Init and Deinit Functions.

As of SICStus Prolog 3.8, partial saved states corresponding to a set of source files, modules, and predicates can be created by the built-in predicates save_files/2, save_modules/2, and save_predicates/2 respectively. These predicates create files in a binary format, by default with the prefix .po (for Prolog object file), which can be loaded by load_files/[1,2]. For example, to compile a program split into several source files into a single object file, type:

| ?- compile(Files), save_files(Files, Object).

For each filename given, the first goal will try to locate a source file with the default suffix .pl and compile it into memory. The second goal will save the program just compiled into an object file whose default suffix is .po. Thus the object file will contain a partial memory image.

NOTE: Prolog object files can be created with any suffix, but cannot be loaded unless the suffix is .po!


Node:Emacs Interface, Previous:Saving, Up:Run Intro

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.

Emacs is a powerful programmable editor especially suitable for program development. It is available for free for many platforms, including various UNIX dialects, Windows and MacOS. For information on obtaining Emacs, see http://www.emacs.org. For information specific to GNU Emacs or XEmacs, see http://www.gnu.org and http://www.xemacs.org respectively.

The advantages of using SICStus in the Emacs environment are source-linked debugging, auto indentation, syntax highlighting, help on predefined predicates (requires the SICStus info files to be installed), loading code from inside Emacs, auto-fill mode, and more.

The Emacs interface is not part of SICStus Prolog proper, but is included in the distribution for convenience. It was written by Emil Ĺström 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, Henrik Bĺkman, and Tamás Rozmán. 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.


Node:Installation, Next:, Previous:Emacs Interface, Up:Emacs Interface

Installation

See The Emacs Interface, for more information about installing the Emacs interface.

There are some differences between GNU Emacs and XEmacs. This will be indicated with Emacs-Lisp comments in the examples.

Customizing Emacs

Version 20 of GNU Emacs and XEmacs introduced a new method for editing and storing user settings. This feature is available from the menu bar as Customize and particular Emacs variables can be customized with M-x customize-variable. Using Customize is the preferred way to modify the settings for emacs and the appropriate customize commands will be indicated below, sometimes together with the old method of directly setting Emacs variables.

Enabling Emacs Support for SICStus

Assuming the Emacs interface for SICStus Prolog has been installed in the default location, inserting the following lines in your ~/.emacs will make Emacs use this mode automatically when editing files with a .pl extension:

(setq load-path
   (cons (expand-file-name "/usr/local/lib/sicstus-3.9.1/emacs")
         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 first line is the file system path to prolog.el (the generic Prolog mode) and sicstus-support.el (SICStus specific code). For example, ~/emacs means that the file is in the user's home directory, in directory emacs. Windows paths can be written like C:/Program Files/SICStus Prolog 3.9.1/emacs.

The last line above makes sure that files ending with .pl are assumed to be Prolog files 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 file, for an alternative approach, see Mode Line.

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

Enabling Emacs Support for SICStus Documentation

It is possible to look up the documentation for any built in or library predicate from within Emacs (using C-c ? or the menu). For this to work Emacs must be told about the location of the info-files that make up the documentation.

The default location for the info-files are <prefix>/lib/sicstus-3.9.1/doc/info/ on UNIX platforms and C:/Program Files/SICStus Prolog 3.9.1/doc/info/ on Windows.

Add the following to your ~/.emacs file, assuming INFO is the path to the info files, e.g. C:/Program Files/SICStus Prolog 3.9.1/doc/info/

(setq Info-default-directory-list
   (append Info-default-directory-list '("INFO")))
for GNU Emacs, or
(setq Info-directory-list
   (append Info-directory-list '("INFO")))

for XEmacs. You can also use M-x customize-group <RET> info <RET> if your Emacs is new enough. You may have to quit and restart Emacs for these changes to take effect.


Node:Basic Configuration, Next:, Previous:Installation, Up:Emacs Interface

Basic Configuration

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)       ; GNU Emacs
(setq font-lock-auto-fontify t) ; XEmacs
(setq font-lock-maximum-decoration t)

These settings and more are also available through M-x customize-group <RET> font-lock.

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)

Similarly, to turn it off only for prolog mode use:

(add-hook 'prolog-mode-hook 'turn-off-font-lock)


Node:Usage, Next:, Previous:Basic Configuration, Up:Emacs Interface

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* buffer. The most common debugging predicates are available from the menu or via key-bindings.

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 flag source_info to emacs. Its value should be emacs 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 modified since it was last loaded, Prolog's line number information may be invalid. If this happens, just reload the relevant buffer.

Consultation and compilation is either done via the menu or with the following key-bindings:

C-c C-f
Consult file.
C-c C-b
Consult buffer.
C-c C-r
Consult region.
C-c C-p
Consult predicate.
C-c C-c f
Compile file.
C-c C-c b
Compile buffer.
C-c C-c r
Compile region.
C-c C-c p
Compile predicate.

The boundaries used when consulting and compiling predicates are the first and last clauses of the predicate the cursor is currently in.

Other useful key-bindings are:

M-n
Go to the next clause.
M-p
Go to the previous clause.
M-a
Go to beginning of clause.
M-e
Go to end of clause.
M-C-c
Mark clause.
M-C-a
Go to beginning of predicate.
M-C-e
Go to end of predicate.
M-C-h
Mark predicate.
M-{
Go to the previous paragraph (i.e. empty line).
M-}
Go to the next paragraph (i.e. empty line).
M-h
Mark paragraph.
M-C-n
Go to matching right parenthesis.
M-C-p
Go to matching left parenthesis.
M-;
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
Enable and disable zipping, respectively.
C-x SPC
C-u C-x SPC
Set and remove a line breakpoint. This uses the advanced debugger features introduced in SICStus 3.8; see Advanced Debugging.
C-c C-s
Insert the PredSpec of the current predicate into the code.
C-c C-n
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-electric-dot-flag variable below.
C-c C-v a
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 prolog-electric-underscore-flag Emacs variable.
C-c ?
Help on predicate. This requires the SICStus info files to be installed. If the SICStus info files are installed in a nonstandard way, you may have to change the Emacs variable prolog-info-predicate-index.


Node:Mode Line, Next:, Previous:Usage, Up:Emacs Interface

Mode Line

If working with an application split into several modules, it is often useful to let files begin with a "mode line":

%%% -*- Mode: Prolog; Module: ModuleName; -*-

The Emacs interface will look for the mode line and notify the SICStus Prolog module system that code fragments being incrementally reconsulted or recompiled should be imported into the module ModuleName. If the mode line is missing, the code fragment will be imported into the type-in module. An additional benefit of the mode line is that it tells Emacs that the file contains Prolog code, regardless of the setting of the Emacs variable auto-mode-alist. A mode line can be inserted by choosing Insert/Module modeline in the Prolog menu.


Node:Configuration, Next:, Previous:Mode Line, Up:Emacs Interface

Configuration

The behavior of the Emacs interface can be controlled by a set of user-configurable settings. Some of these can be changed on the fly, while some require Emacs to be restarted. To set a variable on the fly, 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)

Note that the Emacs interface is presently not using the Customize functionality to edit the settings.

The available settings are:

prolog-system
The Prolog system to use. Defaults to 'sicstus, which will be assumed for the rest of this chapter. See the on-line documentation for the meaning of other settings. For other settings of prolog-system the variables below named sicstus-something will not be used, in some cases corresponding functionality is available through variables named prolog-something.
sicstus-version
The version of SICStus that is used. Defaults to '(3 . 8). Note that the spaces are significant!
prolog-use-sicstus-sd
Set to t (the default) to enable the source-linked debugging extensions by default. The debugging can be enabled via the Prolog menu even if this variable is nil. Note that the source-linked debugging only works if sicstus-version is set correctly.
pltrace-port-arrow-assoc [Obsolescent]
Only relevant for source-linked debugging, this controls how the various ports of invocation boxes (see Procedure Box) map to arrows that point into the current line of code in source code buffers. Initialized as:
'(("call" . ">>>") ("exit" . "+++") ("ndexit" . "?++")
  ("redo" . "<<<") ("fail" . "---") ("exception" . "==>"))

where ndexit is the nondeterminate variant of the Exit port. Do not rely on this variable. It will change in future releases.

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.

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 } of an object definition to prolog-indent-width. Defaults to t.
sicstus-keywords
This is a list with keywords that are highlighted in a special color when used as directives (i.e. as :- keyword). Defaults to
'((sicstus
     ("block" "discontiguous" "dynamic" "initialization"
      "meta_predicate" "mode" "module" "multifile" "public" "volatile")))

prolog-electric-newline-flag
Set to nil to prevent Emacs from automatically indenting the next line when pressing <RET>. Defaults to t.
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 first is faster but may result in erroneous indentation in /* ... */ comments. The default is 'beg-of-line.
prolog-imenu-flag
Set to t to enable a new Predicate menu which contains all predicates of the current file. 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.


Node:Tips, Previous:Configuration, Up:Emacs Interface

Tips

Some general tips and tricks for using the SICStus mode and Emacs in general are given here. Some of the methods may not work in all versions of Emacs.


Node:Font-locking, Next:, Previous:Tips, Up:Tips

Font-locking

When editing large files, it might happen that font-locking is not done because the file is too large. Typing M-x lazy-lock-mode results in only the visible parts of the buffer being highlighted, which is much faster, see its Emacs on-line documentation for details.

If the font-locking seems to be incorrect, choose Fontify Buffer from the Prolog menu.


Node:Auto-fill mode, Next:, Previous:Font-locking, Up:Tips

Auto-fill mode

Auto-fill 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


Node:Speed, Next:, Previous:Auto-fill mode, Up:Tips

Speed

There are several things to do if the speed of the Emacs environment is a problem:


Node:Changing colors, Previous:Speed, Up:Tips

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 buffer. The default settings of the colors may not agree with your preferences, so here is how to change them.

If your emacs support it, use Customize, M-x customize-group <RET> font-lock <RET> will show the Customize settings for font locking and also contains pointers to the Customize group for the font lock (type)faces. The rest of this section outlines the more involved methods needed in older versions of Emacs.

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 buffers are not updated according to the new settings, then refontify the buffer using the Fontify Buffer menu entry in the Prolog menu.

Colors are specified 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")
             ))


Node:Prolog Intro, Next:, Previous:Run Intro, Up:Top

The Prolog Language

This chapter provides a brief introduction to the syntax and semantics of a certain subset of logic (definite clauses, also known as Horn clauses), and indicates how this subset forms the basis of Prolog.


Node:Syntax, Next:, Previous:Prolog Intro, Up:Prolog Intro

Syntax, Terminology and Informal Semantics


Node:Terms, Next:, Previous:Syntax, Up:Syntax

Terms

The data objects of the language are called terms. A term is either a constant, a variable or a compound term.


Node:Integers, Next:, Previous:Terms, Up:Terms

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 other base notations. In sicstus mode, any base from 2 to 36 can be specified, while in iso mode bases 2 (binary), 8 (octal), and 16 (hex) can be used. Letters A through Z (upper or lower case) are used for bases greater than 10. E.g.

15   2'1111   8'17  16'f  % sicstus mode
15   0b1111   0o17  0xf   % iso mode

all represent the integer fifteen. Except for the first, decimal, notation, the forms in the first line are only acceptable in sicstus mode, while those in the second line are only valid in iso mode.

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. Unless character escapes have been switched off, if 0' is followed by \, the \ denotes the start of an escape sequence with special meaning (see Escape Sequences).


Node:Floats, Next:, Previous:Integers, Up:Terms

Floats

Constants also include floats such as

1.0   -3.141   4.5E7   -0.12e+8   12.0e-9

Note that there must be a decimal point in floats written with an exponent, and that there must be at least one digit before and after the decimal point.


Node:Atoms, Next:, Previous:Floats, Up:Terms

Atoms

Constants also include atoms such as

a   void   =   :=   'Algol-68'   []

Atoms are definite 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:
    +-*/\^<>=~:.?@#$&
    This set can in fact be larger; see Token String for a precise definition.
  3. Any sequence of characters delimited by single quotes. Unless character escapes have been switched off, backslashes in the sequence denote escape sequences (see Escape Sequences), and if the single quote character is included in the sequence it must be escaped, e.g. 'can\'t'.
  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,[]).


Node:Variables, Next:, Previous:Atoms, Up:Terms

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 definite but unidentified 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.


Node:Compound Terms, Previous:Variables, Up:Terms

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

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 fields 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 compound term

       s
     /   \
  np       vp
  |       /  \
john     v     np
         |     |
       likes  mary

Sometimes it is convenient to write certain functors as operators--2-ary functors may be declared as infix operators and 1-ary functors as prefix or postfix operators. Thus it is possible to write, e.g.

X+Y     (P;Q)     X<Y      +X     P;

as optional alternatives to

+(X,Y)   ;(P,Q)   <(X,Y)   +(X)   ;(P)

The use of operators is described fully below (see Operators).

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 first three natural numbers is the compound term

  .
 / \
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

[1,2,3]

The special list notation in the case when the tail of a list is a variable is exemplified 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 or one-char atoms. Lists written in this notation are called strings. E.g.

"SICStus"

which, by default, represents exactly the same list as

[83,73,67,83,116,117,115]

The Prolog flag double_quotes can be used to change the way strings are interpreted. The default value of the flag is codes, which implies the above interpretation. If the flag is set to chars, a string is transformed to a list of one-char atoms. E.g. with this setting the above string represents the list:

['S','I','C','S',t,u,s]

Finally if double_quotes has the value atom, then the string is made equivalent to the atom formed from its characters: the above sample string is then the same as the atom 'SICStus'.

Unless character escapes have been switched off, backslashes in the sequence denote escape sequences (see Escape Sequences). As for quoted atoms, if a double quote character is included in the sequence it must be escaped, e.g. "can\"t".


Node:Programs, Previous:Terms, Up:Syntax

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 identifies 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.

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 satisfied.

If the body of a clause is non-empty, the clause is called a rule, 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 Directives), 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 different 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 find whether a person X is employed, find whether any Y employs X."

  2. derivative(X,X,1).

    "For any X, the derivative of X with respect to X is 1."

    "The goal of finding a derivative for the expression X with respect to X itself is satisfied 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 first.

In Prolog, several predicates may have the same name but different arities. Therefore, when it is important to specify a predicate unambiguously, the form name/arity is used; e.g. concatenate/3.

Certain predicates are predefined 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 defining 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.

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).


Node:Declarative, Next:, Previous:Syntax, Up:Prolog Intro

Declarative Semantics

The semantics of definite clauses should be fairly clear from the informal interpretations already given. However it is useful to have a precise definition. The declarative semantics of definite clauses tells us which goals can be considered true according to a given program, and is defined 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 first 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.


Node:Procedural, Next:, Previous:Declarative, Up:Prolog Intro

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 definite clauses. The procedural semantics defines 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 effect of executing a goal is to enumerate, one by one, its true instances. Here then is an informal definition of the procedural semantics. We first illustrate the semantics by the simple query

?- concatenate(X, Y, [a,b]).

We find that it matches the head of the first 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 first 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 first clause.

Thus, in the procedural semantics, the set of clauses

H :- B1, ..., Bm.
H' :- B1', ..., Bm'.
...

are regarded as a procedure definition for some predicate H, and in a query

?- G1, ..., 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 unification algorithm (see [Robinson 65] which computes the most general unifier, 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 find 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 first 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 floundered. 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 satisfied.

A goal is blocked if certain arguments are uninstantiated and its predicate definition is annotated with a matching block declaration (see Block Declarations). Goals of certain built-in may also be blocked if their arguments are not sufficiently instantiated.

When this mechanism is used, the control structure resembles that of coroutines, suspending and resuming different 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.


Node:Occur, Next:, Previous:Procedural, Up:Prolog Intro

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 variabl