Unexpected behaviour in user module

#1

With no module already defined, is the following an expected behaviour?

?- assert(p).
true.

?- foo:p.
true.

Since foo is not defined, I’d expect an error message about either a non-existent predicate or module. (Running in SWI-Prolog 8.0.2)

#2

From a fresh startup of swipl. (I am using SWI-Prolog AMD64, Multi-threaded, version 8.0.0)

?- current_module(X).
X = prolog_history ;
X = pce_expansion ;
X = prolog_clause ;
X = prolog_stack ;
X = broadcast ;
X = predicate_options ;
X = pce_swi_hooks ;
X = read_util ;
X = arithmetic ;
X = prolog_debug ;
X = pairs ;
X = win_menu ;
X = qsave ;
X = link_xpce ;
X = error ;
X = ansi_term ;
X = prolog_listing ;
X = occurs ;
X = prolog_source ;
X = settings ;
X = user ;
X = listing ;
X = prolog_operator ;
X = shlib ;
X = system ;
X = lists ;
X = prolog ;
X = swi_option ;
X = apply ;
X = sandbox ;
X = base32 ;
false.

Notice that in the above list foo is not present.

?- listing(p).
ERROR: procedure `p' does not exist (DWIM could not correct goal)
^  Call: (13) call(prolog_listing:close_sources) ? creep
^  Exit: (13) call(prolog_listing:close_sources) ? creep
?- assert(p).
true.

?- listing(p).
:- dynamic p/0.

p.

true.

?- foo:p.
true.

?- current_module(foo).
true.

Notice that now foo is a valid module that was dynamically created.

See Dynamic Modules

However this will give an error:

?- listing(foo:p).
ERROR: procedure `foo:p' does not exist (DWIM could not correct goal)
^  Call: (13) call(prolog_listing:close_sources) ? creep
^  Exit: (13) call(prolog_listing:close_sources) ? creep
?- 

Hopefully this gives you enough info to answer your question.

1 Like
#3

I think you’ll find the answer in Dynamic importing using import modules

1 Like
#4

Thanks! Both answers clarify what is going on with dynamic modules.

#5

For other finding this and looking for more information about SWI-Prolog modules.

See:
Using SWI-Prolog’s modules

Modules in SWI Prolog