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.18.
Within quoted atoms (using single quotes:
special characters are represented using escape sequences. An escape
sequence is led in by the backslash (
character. The list of escape sequences is compatible with the ISO
standard but contains some extensions, and the interpretation of
numerically specified characters is slightly more flexible to improve
compatibility. Undefined escape characters raise a
exception.19Up to SWI-Prolog 6.1.9,
undefined escape characters were copied verbatim, i.e., removing the
- Alert character. Normally the ASCII character 7 (beep).
- Backspace character.
- No output. All input characters up to but not including the first
non-layout character are skipped. This allows for the specification of
pretty-looking long lines. Not supported by ISO. Example:
format('This is a long line that looks better if it was \c split across multiple physical lines in the input')
- When in ISO mode (see the Prolog flag iso),
only skip this sequence. In native mode, white space that follows the
newline is skipped as well and a warning is printed, indicating that
this construct is deprecated and advising to use
\c. We advise using
\cor putting the layout before the
, as shown below. Using
\cis supported by various other Prolog implementations and will remain supported by SWI-Prolog. The style shown below is the most compatible solution.20Future versions will interpret
<return> according to ISO.
format('This is a long line that looks better if it was \ split across multiple physical lines in the input')
format('This is a long line that looks better if it was\ split across multiple physical lines in the input')
- Escape character (ASCII 27). Not ISO, but widely supported.
- Form-feed character.
- Next-line character.
- Carriage-return only (i.e., go back to the start of the line).
- Space character. Intended to allow writing
0'\sto get the character code of the space character. Not ISO.
- Horizontal tab character.
- Vertical tab character (ASCII 11).
- Hexadecimal specification of a character. The closing
\is obligatory according to the ISO standard, but optional in SWI-Prolog to enhance compatibility with 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
- Unicode character specification where the character is specified using
exactly 4 hexadecimal digits. This is an extension to the ISO
standard, fixing two problems. First, where
\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.
- Same as
\uXXXX, but using 8 digits to cover the whole Unicode set.
- Octal character specification. The rules and remarks for hexadecimal specifications apply to octal specifications as well.
- Escapes the backslash itself. Thus,
'\\'is an atom consisting of a single
- If the current quote (
') is preceded by a backslash, it is copied verbatim. Thus,
''''both describe the atom with a single
Character escaping is only available if
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 as octal 40 (decimal 32) by
read. Also, the writef/2
\l is illegal. It is advised to use the more widely
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.
SWI-Prolog supports splitting long integers into digit groups. Digit groups can be separated with the sequence <underscore>, <optional white space>. If the <radix> is 10 or lower, they may also be separated with exactly one space. The following all express the integer 1 million:
1_000_000 1 000 000 1_000_/*more*/000
Integers can be printed using this notation with format/2,
~I format specifier. For example:
?- format('~I', ). 1_000_000
The current syntax has been proposed by Ulrich Neumerkel on the SWI-Prolog mailinglist.
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 6.0.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.
- Quoted atoms and strings
Any character of any script can be used in quoted atoms and strings. The escape sequences
\UXXXXXXXX(see section 220.127.116.11) were introduced to specify Unicode code points in ASCII files.
- Atoms and Variables
We handle them in one item as they are closely related. The Unicode standard defines a syntax for identifiers in computer languages.21http://www.unicode.org/reports/tr31/ In this syntax identifiers start with
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
- White space
All characters marked as separators (Z*) in the Unicode tables are handled as layout characters.
- Control and unassigned characters
Control and unassigned (C*) characters produce a syntax error if encountered outside quoted atoms/strings and outside comments.
- Other characters
The first 128 characters follow the ISO Prolog standard. Unicode symbol and punctuation characters (general category S* and P*) act as glueing symbol characters (i.e., just like
: an unquoted sequence of symbol characters are combined into an atom).
Other characters (this is mainly
No: a numeric character of other type) are currently handled as `solo'.
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 that a
variable is meant to appear once by starting it with an
_Name. Please note that any variable, except plain
shares with variables of the same name. The term
is equivalent to
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.22After a proposal by Richard O'Keefe. First we define the two classes of named variables.
- Named singleton variables
Named singletons start with a double underscore (
__) or a single underscore followed by an uppercase letter, e.g.,
- Normal variables
All other variables are `normal' variables. Note this makes
_vara normal variable.23Some Prolog dialects write variables this way.
Any normal variable appearing exactly once 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(_12).||Singleton variables: [_12]|
|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]|
Starting with version 6.5.1, SWI-Prolog has syntactic singletons
and semantic singletons. The first are checked by
using the option
singletons(warning)). The latter are generated by the
compiler for variables that appear alone in a branch. For
example, in the code below the variable X is not a syntactic
singleton, but the variable X does not communicate any
bindings and replacing
X with _ does not change the semantics.
test :- ( test_1(X) ; test_2(X) ).