2.15.1.8 Use a prefix character to introduce a variable
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 names33Samer 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 value of the flag is the character that introduces a variable.
Using
set_prolog_flag(var_prefix, ’_’) selects the
syntax described above. Alternatively, the flag can be set to a symbol
character, for example , which allows for
variables such as ??x, as used in e.g., SPARQL. With a
symbol character prefix, the following rules apply:
- Only the prefix character, immediately followed by an identifier
character (letter, digit or underscore), introduces a variable. The
prefix character is part of the variable name, i.e.,
?xis named'?x'. Both identifiers starting with an uppercase letter and identifiers starting with an underscore are atoms. - The prefix character that is not followed by an identifier character
is a normal symbol character. For example,
?and?-are atoms. - A sequence of symbol characters ends before the prefix character if
the prefix is followed by an identifier character. For example,
X=?yreads asX = ?y. - The prefix followed by an underscore (e.g.,
?_) is the anonymous variable. Variables that start with the prefix followed by an underscore, e.g.,?_xor?_X, are handled as_Xin standard Prolog: they do not cause a singleton warning, but cause a warning if they appear more than once (see section 2.15.1.10). - When writing, variables are written with the prefix, e.g.,
?_123, and numbered variables are written as?A, etc.
Note that the symbol character loses its role as a symbol character
if it is followed by an identifier character. For example, if
is defined as an infix operator, ?a?x must be written as
a? x. Choosing a character that starts common operators
such as , - or +$
is not advised.