Using SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.5), we proceed step by step:
Define dcg nonterminal
a//1
in moduledcgAux
(pronounced: "di-SEE-goh")::- module(dcgAux,[a//1]). a(0) --> []. a(s(N)) --> [a], a(N).
Run the following queries—using
phrase/2
andapply:foldl/4
:?- use_module([library(apply),dcgAux]). true. ?- phrase( foldl( a,[s(0),s(s(0))]),[a,a,a]). true. ?- phrase( foldl(dcgAux:a,[s(0),s(s(0))]),[a,a,a]). true. ?- phrase(apply:foldl(dcgAux:a,[s(0),s(s(0))]),[a,a,a]). true. ?- phrase(apply:foldl( a,[s(0),s(s(0))]),[a,a,a]). ERROR: apply:foldl_/4: Undefined procedure: apply:a/3
нет! Quite a surprise—and not a good one. Have we been missing some unknown unknowns?
To get rid of above irritating behavior, we must first find out the reason(s) causing it:
?- import_module(apply,M), M=user. false. ?- phrase(apply:foldl(a,[s(0),s(s(0))]),[a,a,a]). ERROR: apply:foldl_/4: Undefined procedure: apply:a/3 ?- add_import_module(apply,user,end). true. ?- import_module(apply,M), M=user. % sic! M = user. % `?- import_module(apply,user).` fails! ?- phrase(apply:foldl(a,[s(0),s(s(0))]),[a,a,a]). true.
What's going on? The way I see it is this:
- Module expansion of the goal passed to
foldl/4
is limited. Quoting from the SWI-Prolog manual page on
import_module/2
:All normal modules only import from user, which imports from system.
SWI's
library(apply)
only "inherits" fromsystem
, but notuser
.If we clone module
apply
toapplY
(and propagate the new module name), we observe:?- use_module(applY). true. ?- phrase(applY:foldl(a,[s(0),s(s(0))]),[a,a,a]). % was: ERROR true. % now: OK!
Please share your ideas on how I could/should proceed!
(I have not yet run a similar experiment with other Prolog processors.)
library(apply)
andapplY
behave differently ifadd_import_module/3
is not used. – Insupportable