ivanka georgieva
ivanka georgieva

Reputation: 21

warning: singleton variables error message in Prolog

The task here is to write a color map in prolog. Here is my code

/* Neighboring relationship*/
edge(wa, or). /* washington is a neighbor of oregon */
edge(wa, id). /* washington is a neighbor of idaho */
edge(or, ca). /* oregon is a neighbor of california */
edge(or, ne). /* oregon is a neighbor of nevada */
edge(or, id). /* oregon is a neighbor of idaho */
edge(id, mo). /* idaho is a neighbor of montana */
edge(id, wy). /* idaho is a neighbor of wyoming */
edge(id, ne). /* idaho is a neighbor of nevada */
edge(id, ut). /* idaho is a neighbor of utah */
edge(mo, wy). /* montana is a neighbor of wyoming */
edge(wy, ut). /* wyoming is a neighbor of utah */
edge(wy, co). /* wyoming is a neighbor of colorado */
edge(ca, ne). /* california is a neighbor of nevada */
edge(ca, ar). /* california is a neighbor of arizona */
edge(ne, ut). /* nevada is a neighbor of utah */
edge(ne, ar). /* nevada is a neighbor of arizona */
edge(ut, nm). /* utah is a neighbor of new mexico */
edge(ut, ar). /* utah is a neighbor of arizona */
edge(ut, co). /* utah is a neighbor of colorado */

/* Colors of the states*/
color(wy, yellow). /* wyoming is painted yellow */
color(wa, yellow). /* washington is painted yellow */
color(ne, yellow). /* nevada is painted yellow */
color(mo, green). /* montana is painted green */
color(ca, green). /* california is painted green */
color(nm, green). /* new mexico is painted green */
color(ut, red).   /* utah is painted red */
color(or, red).   /* oregon is painted red */
color(ha, red).   /* hawaii is painted red */
color(al, red).   /* alaska is painted red */
color(co, orange). /* colorado is painted orange */
color(ar, orange). /* arizona is painted orange */
color(id, orange). /* idaho is painted orange */


/*  adjacent rule*/
adjacent(X, Y) :- edge(X, Y); edge(Y, X).
connected(Node1, Node2) :- edge(Node1, Node2).
connected(Node1, Node2) :- edge(Node1, X), connected(X, Node2).

/*  miscolor rule*/
miscolor(S1, S2, Color) :-
    adjacent(S1, S2), color(S1, Color), color(S2, Color).

/* condition rule to test the miscolor rule */  
q :- miscolor(S1, S2, Color).

This is what bothers me most: Write a rule called q :- condition. When testing, typing q will test your miscolor rule.

The code actually works but the compiler gives an error message: question1.pl:62: warning: singleton variables [S1,S2,Color] for q/0. Please help me with the last condition which causes the error.

Upvotes: 0

Views: 987

Answers (1)

Armatorix
Armatorix

Reputation: 1128

This warning is totally proper. From swi-prolog manual

A singleton variable is a variable that appears only one time in a clause

https://apps.nms.kcl.ac.uk/reactome-pengine/documentation/man?section=singleton

so you should correct the definition like below

q :- miscolor(_, _, _).

as your defining some variables which are not used anywhere.

If you would like to display all the miscolor pairs, you should just use sth like

miscolor(A,B,C).

Upvotes: 1

Related Questions