skip to Main Content

I’m going through the book Programming for Artificial Intelligence by Ivan Bratko, I’m stuck on a basic problem and running into an error and the previous answers on stack overflow don’t seem to be helping.

I’m stuck trying to write a rule using a previous fact as an argument and getting the error

Single variables: [Y]

the code I’m trying to run is this

parent(myfather,me).
parent(mymother,me).

happy(X) :- 
  parent(X,Y).

I’ve managed to make rules like this in the past and I think i’m just missing something very obvious as to why this isn’t working. I think that when this compiles and I run

happy(myfather).

It will return true as it will replace X in the happy rule with myfather, then check parent(X,Y) with parent(myfather,Y). and try to then see if there is a fact that says parent(myfather,somethingelse….).

I’m also using swipl on macOS if that is relevant, thanks.

EDIT:

I hadn’t checked but the program actaully works as it should but still gives the warning, it makes sense however is there any way of getting rid of the error or an understanding of why the error is there?

2

Answers


  1. Singleton variables is a warning, not an error.

    It is meant to remind you that you have a named variable occurring only once within a rule.

    If you want to suppress the warning for that particular variable, rename it to a name beginning with underscore (e.g. _Y), e.g.:

    happy(X) :- parent(X, _Y).
    

    or rename it to _ (anonymous variable), e.g.:

    happy(X) :- parent(X, _).
    

     

    This type of warning is very useful for spotting typos, such a mistyped variable name:

    happy(Child) :- parent(Chidl, Parent), happy(Parent).
    

    Warning: Singleton variables: [Child,Chidl]

    or other kind of typos, such as a period typed in place of a comma:

    happy(Child) :- parent(Child, Parent). happy(Parent).
    

    Warning: Singleton variables: [Parent]

    Warning: Singleton variables: [Parent]

    or other logical errors.

     

    Without the singleton variable warning, these errors would go unnoticed and would be more difficult to track down.

    So, seeing this warning usually rings a bell for looking for other type of errors as well. If there are no other errors, then just fix the singleton variable by making it anonymous.

     

    If you know what you are doing, you can globally disable the warning with:

    :- style_check(-singleton).
    
    Login or Signup to reply.
  2. I know this is an old question, but i get the solution for that, if you got Singleton variables: [Y] error message, please check your code and your Queries is it mixed with upper-case, lower-case or not. Because Prolog is case sensitive.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search