Prolog warning

I wrote this predicate in the prologue:

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

It works well, but I got this warning:

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

What can I do to avoid this?

+8
warnings prolog
source share
2 answers

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

Why does he warn you about this? Because most often you mistakenly wrote the name of the variable. The resulting code when skipping a variable is also a valid prolog program, so debugging will be painful if it does not warn you.

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

In your example, this would be:

 list([]). list([_|L]) :- list(L). 
+16
source share

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

+2
source share

Source: https://habr.com/ru/post/651352/


All Articles