listing/0-problem

50 views
Skip to first unread message

Marcus Åkerman

unread,
Sep 27, 2016, 5:18:44 PM9/27/16
to swi-p...@googlegroups.com
Hi!

I'm new to this forum and also to Prolog. I'm going through the Learn Prolog Now!-book and in the first chapter it learns you about the command listing/0. When I use this I expect something like...

   listens2Music(mia). 
   happy(yolanda). 
   playsAirGuitar(mia):- 
         listens2Music(mia). 
   playsAirGuitar(yolanda):- 
         listens2Music(yolanda). 
   listens2Music(yolanda):- 
         happy(yolanda).

... but instead it also prints something like this between the different predicates and such:


:- dynamic expand_answer/2.

:- multifile expand_answer/2.


expand_answer(A, B) :-

toplevel_variables:expand_answer(A, B).


:- dynamic expand_query/4.

:- multifile expand_query/4.


expand_query(A, B, C, D) :-

toplevel_variables:expand_query(A, B, C, D).

...


It says  "Once again, you may get a little more than this, such as the locations of various libraries that have been loaded." in the text and I guess my problem lies somewhere in here. I've checked the file listing.pl and settings.pl but couldn't find exactly where I should eventually make any changes.


Somebody who knows how to get output with just what is defined within the "knowledge bases" I've loaded?


Thanks in advance,

Marcus


-------

EDIT:


When running listing/0 on an empty .pl file I get this in return:


?- listing.


:- dynamic expand_answer/2.

:- multifile expand_answer/2.


expand_answer(A, B) :-

toplevel_variables:expand_answer(A, B).


:- dynamic expand_query/4.

:- multifile expand_query/4.


expand_query(A, B, C, D) :-

toplevel_variables:expand_query(A, B, C, D).


:- multifile prolog_list_goal/1.



:- dynamic prolog_event_hook/1.

:- multifile prolog_event_hook/1.



:- dynamic exception/3.

:- multifile exception/3.



:- dynamic file_search_path/2.

:- multifile file_search_path/2.


file_search_path(library, A) :-

library_directory(A).

file_search_path(swi, A) :-

system:current_prolog_flag(home, A).

file_search_path(foreign, swi(B)) :-

    system:

    (   current_prolog_flag(arch, A),

atom_concat('lib/', A, B)

    ).

file_search_path(foreign, swi(A)) :-

    system:

    (   (   current_prolog_flag(windows, true)

->  A=bin

;   A=lib

)

    ).

file_search_path(path, C) :-

    system:

    (   getenv('PATH', A),

(   current_prolog_flag(windows, true)

->  atomic_list_concat(B, ;, A)

;   atomic_list_concat(B, :, A)

),

'$member'(C, B),

'$no-null-bytes'(C)

    ).

file_search_path(user_profile, app_preferences('.')).

file_search_path(app_data, A) :-

'$toplevel':catch(expand_file_name('~/lib/swipl', [A]), _, fail).

file_search_path(app_preferences, A) :-

'$toplevel':catch(expand_file_name(~, [A]), _, fail).

file_search_path(autoload, library('.')).

file_search_path(pack, app_data(pack)).

file_search_path(pack, swi(pack)).

file_search_path(library, A) :-

'$pack':pack_dir(_, prolog, A).

file_search_path(foreign, A) :-

'$pack':pack_dir(_, foreign, A).


:- thread_local thread_message_hook/3.

:- dynamic thread_message_hook/3.

:- volatile thread_message_hook/3.



:- dynamic portray/1.

:- multifile portray/1.



:- dynamic prolog_file_type/2.

:- multifile prolog_file_type/2.


prolog_file_type(pl, prolog).

prolog_file_type(prolog, prolog).

prolog_file_type(qlf, prolog).

prolog_file_type(qlf, qlf).

prolog_file_type(A, executable) :-

system:current_prolog_flag(shared_object_extension, A).


:- dynamic prolog_load_file/2.

:- multifile prolog_load_file/2.



:- multifile message_property/2.



:- dynamic library_directory/1.

:- multifile library_directory/1.


library_directory(B) :-

    '$parms':

    (   cached_library_directory(local, A=lib, A),

B=A

    ).

library_directory(B) :-

    '$parms':

    (   cached_library_directory(user,

expand_file_name('~/lib/prolog', [A]),

A),

B=A

    ).

library_directory(B) :-

    '$parms':

    (   cached_library_directory(system,

absolute_file_name(swi(library), A),

A),

B=A

    ).

library_directory(B) :-

    '$parms':

    (   cached_library_directory(clp,

absolute_file_name(swi('library/clp'), A),

A),

B=A

    ).


:- dynamic resource/3.

:- multifile resource/3.



:- dynamic message_hook/3.

:- multifile message_hook/3.



true.


?-


------

Jan Wielemaker

unread,
Sep 28, 2016, 3:01:58 AM9/28/16
to Marcus Åkerman, SWI-Prolog
That is not possible. Traditionally Prolog systems define hooks in the
default user space. SWI-Prolog has some of these traditional hooks and
invented some more. More recent hooks are defined in modules and no
longer affect the user space.

Note that in general Prolog systems do not even allow listing `static'
code. What is wrong with looking at the file you loaded the knowledge
base from? You can portably use e.g., ?- listing(mydata/2). to list
dynamic predicates you have created. In SWI-Prolog you can use listing/0
and listing/1 to verify that the compilation went right. This is notably
interesting if you use the term_expansion/2 and/or goal_expansion/2 hooks
that pre-process the source code after reading and before compilation.

Cheers --- Jan

>
>
> Thanks in advance,
>
> Marcus
>
> --
> You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.

Boris Vassilev

unread,
Sep 28, 2016, 3:16:51 AM9/28/16
to SWI-Prolog
I realized I had the same question long ago (before the list moved to google groups?), I got pretty much the same answer from Jan:

> These are things that historically reside in the user module and listing
> lists what is defined in the user module.  It is not that easy to find
> out what the user added and even if we could, what should it list?  What
> is there or what the user added?  Listing the real content makes a lot
> of sense.  What if the user added clauses for library_directory/1 for
> its own application need (i.e., unaware of the hook) and observes strange
> behaviour?  Using listing/1 might give a clue as in `hey, there are also
> other clauses??  may be this is something special'.
>
> Most new hooks are defined in the module `prolog`, which avoids these
> issues ...
>
>        Cheers --- Jan


If you use modules, it becomes easier to list the contents of a module only. For example, if I define a module with two exported predicates and a private predicate:

==
$ cat foo.pl
:- module(foo, [bar/0, baz/0]).

bar.
baz.
foobar.

$ swipl -q foo.pl
?- listing(foo:_).

bar.

foobar.

baz.
true.
==

I use the compound term <module_name>:_, and I get all predicates defined in this module, including the private ones.


Is that something that might help?

Cheers,
Boris


Save our in-boxes! http://emailcharter.org

--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+unsubscribe@googlegroups.com.

Marcus Åkerman

unread,
Sep 28, 2016, 8:52:35 AM9/28/16
to SWI-Prolog
Thank you! This helped me. I used ":- module(myKnowledgeBase, [])." to get the output I wanted. Again, thank you!

--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.

Marcus Åkerman

unread,
Sep 28, 2016, 9:11:49 AM9/28/16
to SWI-Prolog
Although I just noticed I need to write the procedure and its arity in the list so I won't need to specify "myModuleName:procedure(term,term)" every time I wan't to query it.

Boris Vassilev

unread,
Sep 28, 2016, 9:20:30 AM9/28/16
to SWI-Prolog
Well, I guess you might as well try to read through the documentation on modules:

http://eu.swi-prolog.org/pldoc/man?section=modules

In short, you can "export" a predicate by adding its indicator just as you did: then, it is visible without the module qualifier. You can always pick out a "private" predicate from a module by using the module specifier, and you can resolve name conflicts if you have two modules with the same predicates in them.


Save our in-boxes! http://emailcharter.org


--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.
Visit this group at https://groups.google.com/group/swi-prolog.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages