listprologcallclause

Converting list of clauses to a query?


let say i have the following facts :

book(65).
own(named('Peter'), 65).

now got the query as a list of clauses :

 [what(A), own(named('Peter'), A)] 

or

 [who(X), book(A), own(X, A)] .

how do I make a rule that accept this list and return the result. Keep in mind that the question could be Why,When,Who...

I went the usual way :

query_lst([]).
%% query_lst([what(Q)|T], Q) :- query_lst(T). 
query_lst([H|T]) :- write('?- '),writeln(H), 
    call(H), query_lst(T).

but this does not allow binding of Q in wh(Q) to the answer which could be in any of the facts that are called by call()


Additional complication I did not forsee is that the query :

(what(A), own(named('Peter'), A).

would fail, because there is no what(X), fact in the DB. I have to just bind somehow the variable A /that is in what()/ to query_lst(Goals,A) and of course remove what(X) from the list /which i can do with select/3 /

any idea how to bind list-Wh-var to query_lst result ?


my current solution (assumes Q is first element):

 query_lst([G|Gs],Res) :- G =.. [Q,Res], member(Q,[what,why,who,when]), lst2conj(Gs,Conj), call(Conj).

Solution

  • Simply convert the list of goals into a conjunction and call it:

    list_to_conjunction([], true).
    list_to_conjunction([Goal| Goals], Conjunction) :-
        list_to_conjunction(Goals, Goal, Conjunction).
    
    list_to_conjunction([], Conjunction, Conjunction).
    list_to_conjunction([Next| Goals], Goal, (Goal,Conjunction)) :-
        list_to_conjunction(Goals, Next, Conjunction).
    

    Then:

    query_list(Goals) :-
        list_to_conjunction(Goals, Conjunction),
        call(Conjunction).