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
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.
ps: make sure your c/2 succeeds once for an instantiated query.
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?
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
Thank you very much!