I'm writing a Prolog program for a simple skill, coloring a graph. I have done everything that I had on my checklist to do: make a graph using a list of edges, make color facts, make color bindings, assign colors, check for conflicts, etc. It always returns false! I keep trying to tweak it slightly , but nothing is working:
color(red).
color(green).
color(blue).
color(yellow).
color(purple).
color(brown).
edge(A,A) :- [A,A].
cb(Item,Color).
graph([edge(A,A)]).
vertex(A,B) :- edge(A,B) ; edge(B,A).
colorItem([],[]).
colorItem([Item|Items], [Binding|Bindings]) :-
colorItem(Items, Bindings),
Binding = cb(Item, Color),
color(Color),
noconflict(Binding, Bindings).
colorGraph(Graph, Bindings) :- colorItem(Graph,Bindings).
noconflict(_,[]).
noconflict(Cb1,[Cb2|Cbs]) :- not(conflict(Cb1,Cb2)), noconflict(Cb1,Cbs).
conflict(cb(N1,Color), cb(N2,Color)) :- vertex(N1,N2).
It should look like something like this IIRC:
?- colorGraph([edge(ny,nj)], Bindings).
Bindings = [cb(nj, green), cb(ny, red)] ;
Bindings = [cb(nj, blue), cb(ny, red)] ;
Bindings = [cb(nj, yellow), cb(ny, red)] .
But I get this:
?- colorGraph([edge(ny,nj)], Bindings).
Bindings = [cb(edge(ny, nj), red)]
Bindings = [cb(edge(ny, nj), green)]
Bindings = [cb(edge(ny, nj), blue)]
Bindings = [cb(edge(ny, nj), yellow)]
Bindings = [cb(edge(ny, nj), purple)]
Bindings = [cb(edge(ny, nj), brown)].
There's something obvious and important that I'm missing with my edges and graph. Any advice or tips would be appreciated.
Replace:
colorGraph(graph, bindings) :- colorItem(graph,bindings).
With:
colorGraph(Graph, Bindings) :- colorItem(Graph, Bindings).
Variables start with either an underscore or an upper case letter in Prolog. Same issue apparently with some of the other predicates.