SWI-Prolog uses ISO-Prolog standard syntax, which is closely compatible to Edinburgh Prolog syntax. A description of this syntax can be found in the Prolog books referenced in the introduction. Below are some non-standard or non-common constructs that are accepted by SWI-Prolog:
/* ... /* ... */ ... */
/* ... */comment statement can be nested. This is useful if some code with
/* ... */comment statements in it should be commented out.
SWI-Prolog offers ISO compatible extensions to the Edinburgh syntax.
processor character set specifies the class of each character used for
parsing Prolog source text. Character classification is fixed to use
UCS/Unicode as provided by the C-library
primitives. See also section 2.17.
Within quoted atoms (using single quotes:
special characters are represented using escape-sequences. An escape
sequence is lead in by the backslash (
character. The list of escape sequences is compatible with the ISO
standard, but contains one extension and the interpretation of
numerically specified characters is slightly more flexible to improve
format('This is a long line that would look better if it was \c split across multiple physical lines in the input')
\cbut ISO compatible.
\is obligatory according to the ISO standard, but optional in SWI-Prolog to enhance compatibility to the older Edinburgh standard. The code
\xa\3emits the character 10 (hexadecimal `a') followed by `3'. Characters specified this way are interpreted as Unicode characters. See also
\xdefines a numeric character code, it doesn't specify the character set in which the character should be interpreted. Second, it is not needed to use the idiosyncratic closing
ISO Prolog syntax.
\uXXXX, but using 8 digits to cover the whole Unicode set.
and not covered by the above escape sequences is copied verbatim. Thus,
'\\'is an atom consisting of a single
''''both describe the atom with a single
Character escaping is only available if the
current_prolog_flag(character_escapes, true) is active
(default). See current_prolog_flag/2.
Character escapes conflict with writef/2
in two ways:
\40 is interpreted as decimal 40 by writef/2,
but character escapes handling by read has already interpreted as 32 (40
\l is translated to a single `l'. It is
advised to use the more widely supported format/[2,3]
predicate instead. If you insist upon using writef/2,
either switch character_escapes
false, or use double
\\, as in
SWI-Prolog implements both Edinburgh and ISO representations for
non-decimal numbers. According to Edinburgh syntax, such numbers are
<radix>'<number>, where <radix>
is a number between 2 and 36. ISO defines binary, octal and hexadecimal
0[bxo]<number>. For example:
A is 0b100 \/ 0xf00
is a valid expression. Such numbers are always unsigned.
The ISO standard specifies the Prolog syntax in ASCII characters. As SWI-Prolog supports Unicode in source files we must extend the syntax. This section describes the implication for the source files, while writing international source files is described in section 3.1.3.
The SWI-Prolog Unicode character classification is based on version 4.1.0 of the Unicode standard. Please note that char_type/2 and friends, intended to be used with all text except Prolog source code is based on the C-library locale-based classification routines.
\UXXXXXXXX(see section 188.8.131.52) were introduced to specify Unicode code points in ASCII files.
ID_Startfollowed by a sequence of
ID_Continuecodes. Such sequences are handled as a single token in SWI-Prolog. The token is a variable iff it starts with an uppercase character or an underscore (
_). Otherwise it is an atom. Note that many languages do not have the notion of character-case. In such languages variables must be written as
variable is a variable that appears only one time in a clause. It
can always be replaced by
anonymous variable. In some cases however people prefer to give
the variable a name. As mistyping a variable is a common mistake, Prolog
systems generally give a warning (controlled by style_check/1)
if a variable is used only once. The system can be informed a variable
is known to appear once by starting it with an underscore. E.g.
Please note that any variable, except plain
_ shares with
variables of the same name. The term
t(_X, _X) is
t(X, X), which is different from
As Unicode requires variables to start with an underscore in many languages this schema needs to be extended.9After a proposal by Richard O'Keefe. First we define the two classes of named variables.
__) or a single underscore followed by an uppercase letter. E.g.
_vara normal variable.10Some Prolog dialects write variables this way.
Any normal variable appearing exactly ones in the clause and any named singleton variables appearing more than once are reported. Below are some examples with warnings in the right column. Singleton messages can be suppressed using the style_check/1 directive.
|test(_a).||Singleton variables: [_a]|
|test(A).||Singleton variables: [A]|
|test(__a, __a).||Singleton-marked variables appearing more than once: [__a]|
|test(_A, _A).||Singleton-marked variables appearing more than once: [_A]|