2.15 Syntax Notes

SWI-Prolog syntax is close to ISO-Prolog standard syntax, which is closely compatible with 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:

2.15.1 ISO Syntax Support

SWI-Prolog offers ISO compatible extensions to the Edinburgh syntax.

2.15.1.1 Processor Character Set

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

2.15.1.2 Character Escape Syntax

Within quoted atoms (using single quotes: '<atom>') 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 syntax_error exception.15Up to SWI-Prolog 6.1.9, undefined escape characters were copied verbatim, i.e., removing the backslash.

\a
Alert character. Normally the ASCII character 7 (beep).
\b
Backspace character.
\c
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')
\<NEWLINE>
When in ISO mode (see the Prolog flag iso), only skip this sequence. In native mode, white-space that follows the newline are skipped as well and a warning is printed, indicating that this construct is deprecated and advicing to use \c. We advise using \c or putting the layout before the \, as shown below. Using \c is supported by various other Prolog implementations and will remain supported by SWI-Prolog. The style shown below is the most compatible solution.16Future 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')

instead of

format('This is a long line that looks better if it was\
 split across multiple physical lines in the input')
\e
Escape character (ASCII 27). Not ISO, but widely supported.
\f
Form-feed character.
\n
Next-line character.
\r
Carriage-return only (i.e., go back to the start of the line).
\s
Space character. Intended to allow writing 0'\s to get the character code of the space character. Not ISO.
\t
Horizontal tab-character.
\v
Vertical tab-character (ASCII 11).
\xXX..\
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\3 emits the character 10 (hexadecimal `a') followed by `3'. Characters specified this way are interpreted as Unicode characters. See also \u.
\uXXXX
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 of all, where \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
Same as \uXXXX, but using 8 digits to cover the whole Unicode set.
\40
Octal character specification. The rules and remarks for hexadecimal specifications apply to octal specifications as well.
\\
Escapes the backslash itself. This, '\\' is an atom consisting of a single \.
\quote
If the current quote (" or ') is preceeded by a backslash, it is copied verbatim. Thus, '\'' and '''' 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 sequence \l is illegal. 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').

2.15.1.3 Syntax for non-decimal numbers

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.

2.15.1.4 Unicode Prolog source

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.

2.15.1.5 Singleton variable checking

A singleton variable is a variable that appears only one time in a clause. It can always be replaced by _, the 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. _Name. Please note that any variable, except plain _, shares with variables of the same name. The term t(_X, _X) is equivalent to t(X, X), which is different from t(_, _).

As Unicode requires variables to start with an underscore in many languages this schema needs to be extended.18After a proposal by Richard O'Keefe. First we define the two classes of named variables.

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(_).
test(_a).Singleton variables: [_a]
test(_12).Singleton variables: [_12]
test(A).Singleton variables: [A]
test(_A).
test(__a).
test(_, _).
test(_a, _a).
test(__a, __a).Singleton-marked variables appearing more than once: [__a]
test(_A, _A).Singleton-marked variables appearing more than once: [_A]
test(A, A).