| Did you know ... | Search Documentation: |
| 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:
?x is named '?x'. Both identifiers starting
with an uppercase letter and identifiers starting with an underscore are
atoms.? and
?- are atoms.X=?y
reads as X = ?y.?_) is the
anonymous variable. Variables that start with the prefix followed by an
underscore, e.g., ?_x or ?_X, are handled as
_X in 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).?_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.