Search code examples
reflectionmoduleprologswi-prolog

How to list the clauses inside a module in SWI-Prolog?


SWI-Prolog has for example the library(dcgbasics) for use with DCGs.

While referencing the module is easy with use_module/1, e.g.

:- use_module(library(dcg/basics)).

trying to use listing/1 with it is not so easy.

?- listing(dcg:_).
true.

?- listing(dcgbasics:_).
true.

?- basics:listing.
true.

What is the correct way to get a listing of the clauses in library(dcg/basics)?


Follow up after answer given.

To list a specific clause, e.g. blanks//0 the query is

?- listing(dcg_basics:blanks).
blanks(A, B) :-
    blank(A, C),
    !,
    D=C,
    blanks(D, B).
blanks(A, A).

true.

Solution

  • Use either:

    ?- dcg_basics:listing.
    

    Or:

    ?- listing(dcg_basics:_).
    

    The first argument of use_module/1-2 is a file specification, not a module name. But listing the module contents requires the actual module name, which may be different (as it is the case here) from the module file basename. But how to find the module name from the file specification? In the particular case of SWI-Prolog:

    ?- absolute_file_name(library(dcg/basics), Path, [extensions([pl])]),
       module_property(Module, file(Path)).
    Path = '/Users/pmoura/lib/swipl/library/dcg/basics.pl',
    Module = dcg_basics.