1
votes

Is there a way of querying a prolog knowledge base dynamically?

I have the prolog logic in a file family.pl (an example that I found here http://www.techytalk.info/prolog-programming-gprolog-linux/). Here is its content:

mother_child(trude, sally).

father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).

sibling(X, Y)      :- parent_child(Z, X), parent_child(Z, Y).

parent_child(X, Y) :- father_child(X, Y).
parent_child(X, Y) :- mother_child(X, Y).

I want to be able to make queries without getting inside the prolog interpreter.

This command does not work for me:

swipl -f family.pl -g "father_child(Father, Child)"

Thanks

1

1 Answers

4
votes

The query does work: It's just that you do not see the results if you invoke the program in this way.

So, you can print the results yourself, using for example:

swipl -f family.pl -g "father_child(Father,Child), \
                       portray_clause(father_child(Father, Child))"

This yields:

father_child(tom, sally).
?-

That's of course not all there is to it, so you can use false/0 to force backtracking:

swipl -f family.pl -g "father_child(Father,Child), \
                       portray_clause(father_child(Father, Child)), \
                       false"

and that yields:

father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).
?-

Use -t halt to halt the program instead of returning to the toplevel:

swipl -f family.pl -g "father_child(Father,Child), \
                       portray_clause(father_child(Father, Child)), \
                       false" -t halt

And now at last we have:

father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).

P.S.: That's a very nice naming convention for predicates!