I was testing my new version of SWI prolog and keep coming across the error :singleton variable.
Example:
member(X,[X|T]). member(X,[X|T]) :- member(X,T).
finds the member of a list such as :
member(yolands,[yolanda,tim]) X = yes
but instead I get a singleton variables error for X and T
if I do the following:
member(X,[X|_]). member(X,[_|T]) :- member(X,T).
It works but looks ugly!
Can anyone explain why single variables ar enot allowed and if this ANSI standard?
You can set the Prolog flag singleton to off to get rid of the warnings. A better way to get rid of the warnings is to rename singleton variables such that they all start with the underscore _.
Singleton error is one of the most common errors in Odoo faced by many developers. Singleton can be defined as the recordset with only one record. Singleton error usually occurs when a method requires a single record to invoke, but it is called by multiple records or a recordset instead.
You can turn off these warnings with style_check using :-style_check(-discontiguous). . For example, you can also put off the warning about singleton variables with :-style_check(-singleton).
Because of the problems of negation-as-failure, negation in Prolog is represented in modern Prolog interpreters using the symbol \+ , which is supposed to be a mnemonic for not provable with the \ standing for not and the + for provable.
Singleton variables are useless in Prolog, and are easily introduced by editing typos.
The warning is welcome to me, as it allows to easily spot such frequent cause of error.
Being a warning, you can run code containing singletons, but any value these eventually will assume will be lost.
I don't think that ISO standard (never heard about ANSI) forbids such variables.
You could rewrite your example in this way
member(X, [Y|T]) :- X = Y ; member(X, T).
and then forget about the singleton.
You have a bug here:
member(X,[X|T]) :- member(X,T).
What you're actually saying (as opposed to what you think you're saying) is that member/2
holds if X is at the head of the list and present in the tail of the list. This predicate will only ever be true for the first N copies of the same thing at the beginning of a list, so it's a very strange thing to say!
?- member(X, [a,a,c]). X = a ; X = a ; false. ?- member(X, [b,a,a]). X = b ; false.
Now, you could correct the bug and still have a singleton warning by doing something like this:
member(X, [Y|T]) :- member(X, T).
But this is neither as good as the conventional definition with two heads or @CapelliC's version (+1) with an explicit OR. I think you should wait until you understand Prolog a little better before putting much stock in your sense of Prolog code aesthetics. If you stick with it for a while you'll come to appreciate this warning as well as the use of anonymous variables.
What makes singleton variables useless in Prolog is that they're named but nothing is known about them and they have no effect on the rest of the computation. The underscore highlights that absolutely anything could go in there without affecting the meaning. What makes
member(X, [X|T]).
true is that the X is position 1 is the same as the X at the head of the list in position 2. Lists must either be empty or have a head and a tail, but what's in the tail is not relevant here, what matters is that X is also the head. The T could be the rest of the list, or it could be an improper list, or it could be a breadbox or lightning or the smell of the air on a spring day. It has no bearing on the truth of member(X, [X|T])
.
The singleton warning tells you "you've reserved a name for something here, but you never call anything by that name." The first thing I do when I get this message and it isn't an obvious typo is replace the name with _ and see if my code still makes sense. If it doesn't, I have a logic error. If it does, it was probably unnecessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With