2
votes

I have a Prolog file where I'm consulting a knowledge base and adding my own predicates. I want to have a predicate that saves the entire updated knowledge base to a text file.

I've tried doing this with listing/0, but it seems I can't change its output stream. I could, technically, write each clause separately to the file with portray_clause/2, but I'd like to achieve this in a more elegant way.

Is there a predicate/predicate combination that will allow me to do this?

EDIT: I managed to output the entire knowledge base to a file like so:

tell('newkb.txt'), listing, told.

This achieves what I wanted to do, with a slight drawback: the new newkb.txt file contains a few automatically generated clauses that I'd rather not keep, if possible.

If this is the only/best solution, so be it. But, if there's a way to export the knowledge base, and only the knowledge base, I'd really like to hear it.

Thank you.

1
It is not really clear to me what you aim to do. Can you provide an example?Willem Van Onsem
I have a Prolog file, let's call it Project.pl. Inside it I have: :- ['knowledgebase.txt']. predicate_1(...) :- ... . predicate_2(...) :- ... ., etc. I want to write a predicate that will output both the knowledge base that is being consulted and the predicates I've written in the Project.pl file to another file. Actually, though I've been looking for a solution for almost an hour, as soon as I posted this question, I managed to do (almost exactly) this in the following way: tell('newkb.txt'), listing, told.. This does add a lot of other generated predicates, however.André
You should edit your question and add all that information there rather than in the comments. Meanwhile... How many predicates are in your knowledge base? If not too many, you could list them separately: tell('newkb.txt'), listing(pred1), listing(pred2), ..., listing(pred-n), told.lurker
The stuff SWI includes is weird. Interestingly, GNU does not include a bunch of weird stuff by default.Daniel Lyons
@DanielLyons haha well I was just following the OP's lead on that one. And I agree about SWI including weird (extraneous) stuff that doesn't belong to the user when doing a listing. Maybe they need to provide a listing_of_just_my_stuff predicate.lurker

1 Answers

2
votes

listing/0 lists the knowledge base (well, by default from the global user module. There are several hooks in the user module that obscure this somehow. Some are there by de-facto Prolog practice and some are SWI-prolog extensions. They could of course be omitted from the listing, but that violates the most important use case of listing/0: find what is in the knowledge base and may thus lead to confusion in its own right.

You could switch to another module using e.g. (empty is just a name)

?- module(empty).

Now listing/0 will work as you expect.

It is not very clear how to other facts and rules come in the knowledge base. If you just add them by hand, it might be easier to edit the file and run ?- make. to reload the file. If they are updated using assert/retract, you either need a module as above or some way to enumerate the parts you want saved.