Adding rules to a temporary module ?

55 views
Skip to first unread message

Kuniaki Mukai

unread,
Jul 5, 2018, 8:56:42 PM7/5/18
to SWI-Prolog
The manual says in 2.1.2 Adding rules from the console
(http://www.swi-prolog.org/man/quickstart.html)

?- [user].
|: hello :- format('Hello world~n').
|: ^D
true.

?- hello.
Hello world
true.

Is it possible in a similar way to add rules to a *temporary* module
like this ?

?- module(temp).

?- [temp].
|: hello :- format('Hello world~n').
|: ^D
true.

?- hello.
Hello world
true.

I tried but to see ERROR as follow:

?- module(temp).
Warning: temp is not a current module (created)
true.

temp: ?- [temp].
ERROR: source_sink `temp' does not exist
ERROR: In:
ERROR: [19] throw(error(existence_error(source_sink,temp),_3322))
ERROR: [15] '$load_file'(temp,temp,[expand(false),...]) at /Users/cantor/build/lib/swipl-7.7.17/boot/init.pl:2039
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
temp: ?-

I appreciate any hint in advance.

Kuniaki Mukai


Paulo Moura

unread,
Jul 6, 2018, 1:56:11 AM7/6/18
to Kuniaki Mukai, SWI-Prolog
Hi,

You can use:

?- module(temp).
Warning: temp is not a current module (created)
true.

temp: ?- [user].
|: hello :- format('Hello world~n').
|: ^D% user://1 compiled into temp 0.00 sec, 1 clauses
true.

temp: ?- hello.
Hello world
true.

temp: ?- module(user).
true.

?- temp:hello.
Hello world
true.

?- hello.
Correct to: "temp:hello"? yes
Hello world
true.

Note that [user] in this case just means read terms from the top-level; is not a reference to the "user" module.

Cheers,
Paulo
> --
> 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.


Kuniaki Mukai

unread,
Jul 6, 2018, 3:57:14 AM7/6/18
to Paulo Moura, SWI-Prolog


Thanks Paulo.

I think I got your explanation. Thanks.

However still I did not get what I want to do in (edi)prolog-mode
on emacs buffer.

That works at least when the module is "user":

?- module(user).
%@ true.

a :- writeln(hello).
b :- writeln(world).

( "region-consulted" in the mini-buffer,
after M-X ediprolog-consult)

?-listing(a/0).
%@ a :-
%@ writeln(hello).
%@
%@ true.
?-listing(b/0).
%@ b :-
%@ writeln(world).
%@
%@ true.

?- a.
%@ hello
%@ true.
?- b.
%@ world
%@ true.


I expected the directive "?- module(user)" above could be
replaced with "?- module(temp)" to work fine for the module "temp".
But it did not work:

?- module(temp).
%@ Warning: temp is not a current module (created)
%@ true.

a :- writeln(hello).
b :- writeln(world).

(*ediprolog-conulst* buffer)

ERROR: No permission to load source `'/Users/cantor/tmp/ediprologeRom6T'' (Non-module file already loaded into module user; trying to load into temp)
ERROR: In:
ERROR: [22] throw(error(permission_error(load,source,'/Users/cantor/tmp/ediprologeRom6T'),context(...,'Non-module file already loaded into module user; trying to load into temp')))
ERROR: [20] '$assert_load_context_module'('/Users/cantor/tmp/ediprologeRom6T',temp,[expand(false),...]) at /Users/cantor/build/lib/swipl-7.7.17/boot/init.pl:2537
ERROR: [19] '$mt_do_load'(<clause>(0x7fc69a23b570),'/Users/cantor/tmp/ediprologeRom6T','/Users/cantor/tmp/ediprologeRom6T',temp,[expand(false),...]) at /Users/cantor/build/lib/swipl-7.7.17/boot/init.pl:2156
ERROR: [18] setup_call_catcher_cleanup(system:with_mutex('$load_file',...),system:'$mt_do_load'(<clause>(0x7fc69a23b570),'/Users/cantor/tmp/ediprologeRom6T','/Users/cantor/tmp/ediprologeRom6T',temp,...),_5022,system:'$mt_end_load'(<clause>(0x7fc69a23b570))) at /Users/cantor/build/lib/swipl-7.7.17/boot/init.pl:444
ERROR: [15] '$load_file'('/Users/cantor/tmp/ediprologeRom6T',temp,[expand(false),...]) at /Users/cantor/build/lib/swipl-7.7.17/boot/init.pl:2044
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
^ Call: (20) call(system:'$mt_end_load'(<clause>(0x7fc69a23b570))) ?


I tried to find a way for years, but so far it seems work only
for the "user" module. I wanted it works for temporary modules
because of avoiding possible name coflicts of predicates
in the user module.

Kuniaki Mukai

Paulo Moura

unread,
Jul 6, 2018, 5:03:03 AM7/6/18
to Kuniaki Mukai, SWI-Prolog
Hi,

> On 6 Jul 2018, at 08:57, Kuniaki Mukai <kuniak...@gmail.com> wrote:
>
>
>
> Thanks Paulo.
>
> I think I got your explanation. Thanks.

You're most welcome.
It seems that the same temporary file is reused, hence the error (which is the same as when you tryin to e.g. include the same file in two modules. It seems likely that the problem would need to be fixed in the Emacs mode itself.

Cheers,
Paulo

Kuniaki Mukai

unread,
Jul 6, 2018, 6:31:51 AM7/6/18
to Paulo Moura, SWI-Prolog

Thanks Paulo again.

As a last resort, following your diagnostics  I will take time  to find 
relevant codes in Emacs lisp for Prolog mode. region-consult is worth 
for me to investigate, though I am not sure to be able to read emacs-lisp codes.

Kuniaki Mukai

Kuniaki Mukai

unread,
Jul 6, 2018, 8:52:52 AM7/6/18
to Paulo Moura, SWI-Prolog
Hi Paulo,

After your diagnostics, as a first try before reading in
ediprolog.el for prolog mode, I have inserted a line of module
directive at the top of a prolog file, which is passed to start
ediprolog-mode process like this:

:- module(zdddevel, []).
:- set_prolog_flag(qcompile, auto).
:- ['pack-pac'/prolog/pac].
:- [misc('zdd-load')].

This one line  insertion has fixed the trouble
annoying me for years like a magic !

To be honest, it is still unclear for me how the
module directive got rid off the trouble.

Thank you very much.

Kuaniaki Mukai
Reply all
Reply to author
Forward
0 new messages