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:
0'<char>
C
is a lower case character one can use
between(0'a, 0'z, C)
.
/* ... /* ... */ ... */
/* ... */
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.
The
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 wchar_t
based
primitives. See also section 2.17.
Within quoted atoms (using single quotes: '<atom>'
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
compatibility.
\
\a
\b
\c
format('This is a long line that would look better if it was \c split across multiple physical lines in the input') |
\<RETURN>
\c
but ISO compatible.
\f
\n
\r
\t
\v
\xXX..\
\
is
obligatory according to the ISO standard, but optional in SWI-Prolog to
enhance compatibility to the older Edinburgh standard. The code
\xa\3
emits the character 10 (hexadecimal `a') followed by
`3'. Characters specified this way are interpreted as Unicode
characters. See also \u
.
\uXXXX
\x
defines 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.
\UXXXXXXXX
\uXXXX
, but using 8 digits to cover the whole
Unicode set.
\40
\<character>
\
and
not covered by the above escape sequences is copied verbatim. Thus, '\\'
is an atom consisting of a single \
and '\''
and ''''
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
octal). Also, \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
to
false
, or use double \\
, as in writef('\\l')
.
SWI-Prolog implements both Edinburgh and ISO representations for
non-decimal numbers. According to Edinburgh syntax, such numbers are
written as <radix>'<number>
,
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.