Getting a list of solutions in Prolog

I study prologue, and I read a book called "Prolog Programming for Artificial Intelligence." As a practice, I want to learn how to expand one of the examples in this book. Can anybody help?

Say that you have the following facts:

parent(pam, bob). %pam is a parent of bob parent(george, bob). %george is a parent of bob 

How do I write a prologue predicate that gives me a list of bean parents? For example:

 list_parents(bob, L). L = [pam, george] ; L = [george, pam] ; true. 
+6
prolog
source share
2 answers

An all-solutions predicate such as findall/3 can do the trick:

 list_parents(P, L) :- findall(Parent, parent(Parent, P), L). 

Simply put, findall/3 finds all the bindings for Parent in the target with the return traffic parent(Parent, P) and puts all the bindings of Parent on the L list. Note that this will not remove duplicates, but you can do sort/2 to L before returning it to create a set. Doing this:

 ?- list_parents(bob, L). L = [pam, george]. 

If you do not have findall/3 in your PROLOG implementation, you can do it manually as follows:

 list_parents(P, L) :- list_parents(P, [], L). list_parents(P, Acc, L) :- parent(Parent, P), \+ member(Parent, Acc), !, list_parents(P, [Parent|Acc], L). list_parents(_, L, L). 

This version sends calls to list_parents/2 to the battery version, list_parents/3 . The latter also tries to collect Parent bindings, if we have not seen them before (therefore, checking \+ member ), and returns a list in which new Parent bindings copied to the Acc list can be found. Doing this gives us the same result as the first option:

 ?- list_parents(bob, L). L = [pam, george]. 
+12
source share

Try the following:

 parent(pam, bob). %pam is a parent of bob parent(george, bob). %george is a parent of bob list_parents(A, Es, [X|Xs]) :- parent(X, A), \+ member(X, Es), list_parents(A, [X|Es], Xs). list_parents(A, Es, []). 

It was an ineffective method, for a better method you need a “predicate” of a higher order of “decisions”.

list_parents (X, Ys): - solutions (parent, [X, W], 1, Ys)

+2
source share

All Articles