Jorge Padilla
Jorge Padilla

Reputation: 67

How to get rid of Singleton variables errors?

I'm supposed to implement a little prolog program out of the Monty Python movie where the people are arguing whether a woman is a witch. Based on what the say, witches are burned, but wood is also burned, and wood floats, but ducks also float, so, if someone weighs the same as a duck, she's made of wood, therefore, she's a witch.

Based on that, I've come up with this:

witch(X) :- burns(X), female(X).
burns(X) :- wooden(X).
wooden(X) :- floats(X).
floats(X) :- sameWeight(duck, X).

female(X). 
sameweight(duck, X).

But when I want to check if X is a witch, by trying witch(X). It actually prints "true", confiming the woman is a witch, but I also get a Singleton variables: [X] error message. So clearly I have a bug somewhere and I would like to fix it.

Upvotes: 1

Views: 600

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476977

Those are warnings. It specifies that you use a variable once in clause. This is the case for X in:

female(X). 
sameweight(duck, X).

Now this is rather "odd". Variables are typically used for passing values from head to body, or between two predicate calls in the body. But here you only use X once.

In Prolog one uses an underscore (_) if you "do not care" about the value. The underscore is an "anonymous variable": if you use two underscores in the same clause, those are two different variables.

So you can fix it like:

female(_). 
sameweight(duck, _).

Note that now you have written that everything is a female, and that everything has the same weight as a duck.

Upvotes: 3

Related Questions