ciao-lang / ciao

Ciao is a modern Prolog implementation that builds up from a logic-based simple kernel designed to be portable, extensible, and modular.
https://ciao-lang.org
GNU Lesser General Public License v3.0
267 stars 21 forks source link

Cannot reexport module from package? #97

Open aherranz opened 6 days ago

aherranz commented 6 days ago

I am loading the rfuzzy package and I get an error message "reexport/1 directive not allowed in shell"

Ciao 1.23.0 [LINUXx86_64]
?- use_package(rfuzzy).
{Using package /home/angel/.ciao/RFuzzy/lib/rfuzzy/rfuzzy.pl
{Using package /home/angel/.ciaoroot/v1.23.0-m1/core/lib/hiord/hiord.pl
Note: module hiord_rt already in executable, just made visible
}
ERROR: (lns 5-6) reexport/1 directive not allowed in shell
Note: module aggregates already in executable, just made visible
Note: module terms already in executable, just made visible
Note: module basic_props already in executable, just made visible
{Including /home/angel/.ciao/RFuzzy/lib/rfuzzy/rfuzzy_ops.pl
}

INFO: Rfuzzy (Ciao Prolog package to compile Rfuzzy programs into a pure Prolog programs):  compiling ...    

}

yes
?- 

The main module of the library is this one:

:- package(rfuzzy).

:- use_package(hiord).

:- use_module(library(rfuzzy/rfuzzy_rt)). 
:- reexport(library(rfuzzy/rfuzzy_rt)).

:- use_module(library(aggregates), [findall/3]).
:- use_module(library(terms),[copy_args/3]).
:- use_module(engine(basic_props), [list/1]).
:- include(library(rfuzzy/rfuzzy_ops)).

:- load_compilation_module(library(rfuzzy/rfuzzy_tr)).
:- add_sentence_trans(rfuzzy_tr:rfuzzy_trans_sentence/3, 730).

I don't know the implication of the error, I am not sure if predicates are reexported or not. Can I avoid such a message? (I can update the rfuzzy library).

Regards.

jfmc commented 6 days ago

Hi @aherranz. Some declarations, like reexport, do not make sense from the toplevel/REPL/shell. To avoid that it is possible to deactivate that code using conditional compilation as follows:

:- if(defined('SHELL')).
% put here what is required in a toplevel (it can be empty)
:- else.
% and here what cannot run in a toplevel
:- endif.

for example:

:- if(defined('SHELL')).
:- else.
:- reexport(library(rfuzzy/rfuzzy_rt)).
:- endif.

Another issue is whether reexport for a package is really required (usually we never need it but I'm not familiar with rfuzzy).

Let us know if you need to modify the goal expansion when run in a toplevel context. This is also possible but not really well documented.

aherranz commented 6 days ago

But reexport is not being used from the REPL, in the interpreter I am just doing use_package(rfuzzy). The reexport is in the main module of the library.

I am not familiar with the implementation of rfuzzy, I am just trying to use it. Thanks for your orientation.

jfmc commented 6 days ago

Actually doing a use_package from the toplevel inserts the package contents, like an include. Thus adding the conditional compilation to the rfuzzy package source will fix that problem.

Edit: More details on this. Packages in Ciao are not really modules, but source files that are directly included in the context the use them. Most of the time, the source is a collection of directives like use_module, other use_package, some operator definitions, and other directives to enable user-defined code expansions (like add_sentence_trans). This modified rfuzzy.pl package may fix the issues with reexport in a toplevel:

:- package(rfuzzy).

:- use_package(hiord).

:- use_module(library(rfuzzy/rfuzzy_rt)). 
:- if(defined('SHELL')).
:- else.
:- reexport(library(rfuzzy/rfuzzy_rt)).
:- endif.

:- use_module(library(aggregates), [findall/3]).
:- use_module(library(terms),[copy_args/3]).
:- use_module(engine(basic_props), [list/1]).
:- include(library(rfuzzy/rfuzzy_ops)).

:- load_compilation_module(library(rfuzzy/rfuzzy_tr)).
:- add_sentence_trans(rfuzzy_tr:rfuzzy_trans_sentence/3, 730).