Search code examples
prologswi-prolog

How to get a listing of a specific knowledge base?


Suppose that the file foobar.pl in the current working directory contains the following minimal knowledgebase:

foo(bar).
foo(baz).
frobozz.

If I start swi-prolog (by running swipl at the command), and immediately run

?- [foobar].
% foobar compiled 0.00 sec, 4 clauses
true.

?- listing.

...the contents of foobar are lost in a sea of >100 lines of unrelated output.


How can I limit listing's output to foobar?

Alternatively, how can I limit it to contents of those knowledgebases I have explicitly consulted?


I did look at the docs for listing/1 and listing/0, but I could not find anything helpful:

listing/1 List predicates specified by Pred. Pred may be a predicate name (atom), which lists all predicates with this name, regardless of their arity. It can also be a predicate indicator (/ or //), possibly qualified with a module. For example: ?- listing(lists:member/2)..

A listing is produced by enumerating the clauses of the predicate using clause/2 and printing each clause using portray_clause/1. This implies that the variable names are generated (A, B, ... ) and the layout is defined by rules in portray_clause/1.

listing/0 List all predicates from the calling module using listing/1. For example, ?- listing. lists clauses in the default user module and ?- lists:listing. lists the clauses in the module lists.


Of course, I did try the following useless idea:

?- foobar:listing.
true.

Solution

  • You can load the contents of a plain Prolog file into a module easily. For example:

    ?- fb:consult(foobar).
    true
    

    And then call:

    ?- fb:listing.
    
    foo(bar).
    foo(baz).
    
    frobozz.
    true.
    

    Or list just a specific predicate:

    ?- fb:listing(foo/1).
    foo(bar).
    foo(baz).
    
    true.