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.8After 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.__varor_Var. - Normal variables
All other variables are `normal' variables. Note this makes_vara normal variable.9Some 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(_). | |
| 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). |