Re: how to get predicats from facts?



corps wrote:
suppose we have

person(tom).
person(mike).
cat(tom).
cat(lisa).

how can we make a function
get_predicates(tom). to get [cat,person]?


get_predicates(Name,L) :-
findall(X, X(Name), L).
doesn't work.

get_predicates(Name,L) :-
findall(X, call(X, Name), L).
doesn't work either.

Thanks for any reply.

take a look at =.. and functor/3 :)

unarypredicate_solutions(Name,S) :-
Pred =.. [Name,A],
findall(A, Pred, S).

if you take into account predicates with arity >= 2 it looks something like:

predicate_solutions(Name/Arity,S) :-
length(L,Arity),
append([Name],L,P),
Pred =.. P,
findall(L, Pred, S).


Please take into account that this is what came to my mind first - there
is no check, if the predcate exists and it is certainly not the most
elegant solution.

hth, Martin

.