Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning in prolog

I wrote this predicate in prolog :

list([]).
list([X|L]) :- list(L).

it works well, but I got this warning :

    **Warning: /Users/hw6.pl:2:  
           Singleton variables: [X]** % 

what I can do to avoid it ?

like image 939
tech-ref Avatar asked Jan 21 '11 14:01

tech-ref


People also ask

How do you delete a warning in Prolog?

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 _.

What is a singleton error?

Singleton error usually occurs when a method requires a single record to invoke, but it is called by multiple records or a recordset instead. Then the method will not be able to recognize for which record it should process and raises the expected singleton error.

What is underscore in Prolog?

A single underscore ( _ ) denotes an anonymous variable and means "any term". Unlike other variables, the underscore does not represent the same value everywhere it occurs within a predicate definition. A compound term is composed of an atom called a "functor" and a number of "arguments", which are again terms.


2 Answers

The warning tells you that you have a variable used only once in that clause of the predicate list (in this case the second clause).

Why does it warns you of this ? Because it is more than often that you have misspelled the variable name. The resulting code when you misspell a variable is also a valid prolog program, so debugging would be painful if it does not warn you.

If you are not going to use that variable (X), you can use an anonymous variable instead. To use an anonymous variable you have to use _ as the term instead of a variable name.

In your example it would be:

list([]).
list([_|L]) :- list(L).
like image 138
gusbro Avatar answered Nov 16 '22 04:11

gusbro


Gusbro is exactly right. When you use a variable only once you will get a singleton variable. Your program is still syntactically correct, but prolog assumes you made a mistake typing your code. The underscore variable will always unify as true if it is given any answer.

like image 24
element11 Avatar answered Nov 16 '22 02:11

element11