r/prolog•
fredmorcos

New to Prolog using SWI-Prolog: Question about a many-to-many predicate on a list that works as both a test and a solution generator.

Hello,

(I'm almost giving up, you guys are my last resort).

I'm trying to implement:

  • A predicate p(L), where L is a list, ordering must not be important
  • p/1 tells me whether all elements of L are compatible together based on a c(A, B) predicate
  • c/2 tells me whether A and B are items and are compatible together
  • I have a bunch of item(foo), item(bar), etc... in my database
  • Furthermore, p/1 should answer me without duplicates if I ask it what are all the possible lists that contain items that are compatible together
  • p/1 must be usable from other predicates and as the "user interfacing API" from the prompt
    • So if the user asks for a test, and the list they pass is out-of-order, then it should still be valid
    • But if the user asks for a list, they shouldn't get the list of all permutations for a single answer
    • So essentially, [foo, bar] and [bar, foo] should be considered the same answer

My implementation so far (note that I've had many trial-and-error implementations, but this is the "best" one until now):

p([X]) :- item(X).
p([H|T]) :- item(H), p_helper(H, T), p(T).

p_helper/2 tells me if a single item H is compatible with all items in T without all items in T being necessarily compatible with each other:

p_helper(H, T) :- item(H), findall(X, c(H, X), Xs), powerset(Xs, Xp), permutation(Xp, T).

Obviously this implementation violates the last requirement above (due to the permutations), but it works. It just isn't very user-friendly to have it answer with all possible permutations to a human, and to every other predicate that uses it.

Some further thoughts and notes:

  • foreach(member(X, L), foreach(member(Y, L), ...)) could be helpful? I am not sure how to proceed with this. I am trying to implement it this way now. The problem is that member/2 is a one-directional predicate.
  • Using subset/2 would solve the permutation problem, but is again a one-directional predicate, which would work for the test side of things, but not for the generator side of things.
  • library(clpfd): this could possibly be useful, but it doesn't work with atoms. I would have to assign integers to each item/1 I have and integer ranges for sets of items which is something I would like to avoid since everytime I add an item to my database, a bunch of values have to be updated.
  • library(nb_set): it seems like this is welcoming to the idea that a predicate should be implemented as two parts: a generator and a tester, which seems like a good fit for my problem here. Couldn't get it to work because I don't understand what a non-backtrackable set is or how it works. I randomly dabbled with it but still couldn't get anything out of it. I did implement two different tester and generator predicates for my problem.
  • library(aggregate), library(solution_sequences): I have a gut feeling these might be useful, but I am not sure yet.

I would really appreciate any guidance here.

Thank you very much,

Fred

EDIT: Some more notes.

Posted Jul 15, 2015, 8:59 AM UTC · edited

48
walrusesarecool•
:- meta_predicate symm(2, ?,  ?).

symm(P_2, A, B) :-
   call(P_2, A, B).
symm(P_2, A, B) :-
   call(P_2, B, A).

item(walrus).
item(duck).
item(manatee).
item(elephant).
item(goat).

c(walrus, duck).
c(walrus, manatee).
c(manatee,elephant).
c(goat,duck).
c(manatee,duck).

p(List):-
 var(List),
 longest_unique(All),!,
 member(List,All).
p(List):-
 nonvar(List),
 check_all_compitable(List).

generate(List):-
 findall(I,item(I),Is),
 length(Is,MaxList),
 between(1,MaxList,Size),
 length(List,Size),
 generate_h(List).

generate_h([A,B]):-
 c(A,B).
generate_h([New|List]):-
 generate_h(List),
 item(I),
     \+member(I,List),
 forall(member(M,List),symm(c,M,I)),
 New =I.

generate_all(ListOfSols):-
 setof(Sol,generate(Sol),ListOfSols).

build_unique([]).
build_unique([New|Rest]):-
 build_unique(Rest),
 generate_all(ListOfSols),
 member(New,ListOfSols),
 forall(member(Exist,Rest),\+is_permutation(New,Exist)).

build_unique_size(Unique,Size):-
 length(Unique,Size),
 build_unique(Unique).

longest_unique(List):-
 generate_all(Sol),
 length(Sol,L),
 longest_unique_h(List,L).

longest_unique_h(Unique,Size):-
     build_unique_size(Unique,Size).

longest_unique_h(Unique,Size0):-
 Size1 is Size0 -1,
 Size1 >=2,
     longest_unique_h(Unique,Size1).

 check_all_compitable([]).
 check_all_compitable([H|T]):-
  forall(member(M,T),symm(c,H,M)),
  check_all_compitable(T).


is_permutation(Xs, Ys) :-
    msort(Xs, Sorted),
    msort(Ys, Sorted).
2
walrusesarecool•

Trying to show code in reddit is annoying.

This works but is a bit horrible lol. It is slow and does a lot of repetitive work. I am sure you can work out a way to make it more efficient.

1
walrusesarecool•

ps: make sure your c/2 succeeds once for an instantiated query.

1
fredmorcosOP•

Thanks for all of this!

While reading through, I realized all I needed were var/1 and nonvar/1, now I have two similar implementations, the one with var/1 simply provides the powerset (the generator version), the one with nonvar/1 provides the permutations of the powerset (the tester version).

An unrelated question though, what is a meta-predicate and why did you need to implement symm?

1
walrusesarecool•

Great :) you need a meta predicate symm to make c/2 behave nicely as symmetric predicate. You could list 2 copies of each fact alternatively but that would get boring quickly.

Note that something like. c(foo,bar). c(X,Y):-c(Y,X).

Will cause you problems...

Some info on meta predicates here: http://yfl.bahmanm.com/Members/ttmrichter/yfl-blog/meta-predicates-in-swi-prolog

1
fredmorcosOP•

Thank you very much!

1
walrusesarecool•

is c(A,B) symmetric?

1
fredmorcosOP•

Do you mean that when c(foo, bar) is true, c(bar, foo) is also true? Yes.

1