|Did you know ...||Search Documentation:|
|The SWI-Prolog syntax|
SWI-Prolog syntax is close to ISO-Prolog standard syntax, which is based on the Edinburgh Prolog syntax. A formal description can be found in the ISO standard document. For an informal introduction we refer to Prolog text books (see section 1) and online tutorials. In addition to the differences from the ISO standard documented here, SWI-Prolog offers several extensions, some of which also extend the syntax. See section 5 for more information.
This section lists various extensions w.r.t. the ISO Prolog syntax.
SWI-Prolog allows for nesting
/* ... */ comments. Where
the ISO standard accepts
/* ... /* ... */ as a comment,
SWI-Prolog will search for a terminating
*/. This is useful
if some code with
/* ... */ comment statements in it should
be commented out. This modification also avoids unintended commenting in
the example below, where the closing
*/ of the first
comment has been forgotten.25Recent
copies of GCC give a style warning if
/* is encountered in
a comment, which suggests that this problem has been recognised more
/* comment code /* second comment */ code
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.26Up to SWI-Prolog 6.1.9,
undefined escape characters were copied verbatim, i.e., removing the
format('This is a long line that looks better if it was \c split across multiple physical lines in the input')
\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.27Future 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')
Note that SWI-Prolog also allows unescaped newlines to appear in quoted material. This is not allowed by the ISO standard, but used to be common practice before.
0'\sto get the character code of the space character. Not ISO.
\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
\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.
'\\'is an atom consisting of a single
''''both describe the atom with a single
'\'' == ''''is true.
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
where <radix> is a number between 2 and 36. ISO defines
binary, octal and hexadecimal numbers using
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.
As of version 8.1.22, SWI-Prolog supports rational numbers as a
primary citizen atomic data type if SWI-Prolog is compiled with the GMP
library. This can be tested using the bounded
Prolog flag. An atomic type also requires a syntax. Unfortunately there
are few options for adding rational numbers without breaking the ISO
standard.28ECLiPSe uses numerator_denominator.
This syntax conflicts with SWI-Prolog digit groups (see section
22.214.171.124) and does not have a recognised link to rational numbers.
1/3R have also been
1/3r is compatible to Ruby, but is hard to
parse due to the required look-ahead and not very natural. See also https://en.wikipedia.org/wiki/Rational_data_type.
ECLiPSe and SWI-Prolog have agreed to define the canonical syntax for
rational numbers to be e.g.,
1r3. In addition, ECLiPSe
1_3 and SWI-Prolog can be asked to accept
using the module sensitive Prolog flag rational_syntax,
which has the values below. Note that write_canonical/1
always uses the compatible
|1 000 000/33 000||1000/33|
We expect very few programs to have text parsed into a rational
number while a term was expected. Note that for rationals appearing in
an arithmetic expression the only difference is that evaluation moves
from runtime to compiletime. The utility list_rationals/0
may be used on a loaded program to check whether the program contains
rational numbers inside clauses and thus may be subject to compatibility
issues. If a term is intended this can be written as
1 / 2 or some variation thereof.
1r3. In other words, this adheres to the same rules as
naturalabove, but using the‘
’. Note that this may conflict with traditional Prolog as‘
r’can be defined as an infix operator. The same argument holds for
0x23and similar syntax for numbers that are part of the ISO standard.
While the syntax is controlled by the flag rational_syntax, behavior on integer division and exponentiation is controlled by the flag prefer_rationals. See section section 126.96.36.199 for arithmetic on rational numbers.
SWI-Prolog supports reading and printing‘special' floating point values according to Proposal for Prolog Standard core update wrt floating point arithmetic by Joachim Schimpf and available in ECLiPSe Prolog. In particular,
-1.0Inf. Any sequence matching the regular expression
[+-]?\sd+[.]\sd+Infis mapped to plus or minus infinity.
NaN(Not a Number) is printed as
1.xxxNaN, where 1.xxx is the float after replacing the exponent by‘1'. Such numbers are read, resulting in the same
NaNconstant can also be produced using the function nan/0, e.g.,
?- A is nan. A = 1.5NaN.
By default SWI-Prolog arithmetic (see section 4.27) follows the ISO standard with describes that floating point operations either produce a normal floating point number or raise an exception. section 188.8.131.52 describes the Prolog flags that can be used to support the IEEE special float values. The ability to create, read and write such values facilitates the exchange of data with languages that can represent the full range of IEEE doubles.
According to the ISO standard and most Prolog systems, identifiers
that start with an uppercase letter or an underscore are variables. In
the past, Prolog by BIM provided an alternative syntax, where
only the underscore (
_) introduces a variable. As of
SWI-Prolog 7.3.27 SWI-Prolog supports this alternative syntax,
controlled by the Prolog flag var_prefix.
As the character_escapes
flag, this flag is maintained per module, where the default is
false, supporting standard syntax.
Having only the underscore introduce a variable is particularly useful if code contains identifiers for case sensitive external languages. Examples are the RDF library where code frequently specifies property and class names29Samer Abdallah suggested this feature based on experience with non-Prolog users using the RDF library. and the R interface for specifying functions or variables that start with an uppercase character. Lexical databases where part of the terms start with an uppercase letter is another category were the readability of the code improves using this option.
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.
\UXXXXXXXX(see section 184.108.40.206) 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
: 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.31After 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.32Some 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) ).