- Reference manual
- The SWI-Prolog syntax
- Reference manual
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.
- Quoted atoms and strings
Any character of any script can be used in quoted atoms and strings. The escape sequences
\UXXXXXXXX(see section 184.108.40.206) 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.30http://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.31After 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.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) ).