# Term Expansion issue when consult is called from a Module (was: How to get "Consult('Test.pl'), predicateFromInsideTest." to run)

**URL:** <https://swi-prolog.discourse.group/t/term-expansion-issue-when-consult-is-called-from-a-module-was-how-to-get-consult-test-pl-predicatefrominsidetest-to-run/3788>\
**Category:** Help!\
**Created:** [March 31, 2021, 10:26pm UTC](https://swi-prolog.discourse.group/t/term-expansion-issue-when-consult-is-called-from-a-module-was-how-to-get-consult-test-pl-predicatefrominsidetest-to-run/3788 "2021-03-31T22:26:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericzinda](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@ericzinda](https://swi-prolog.discourse.group/u/ericzinda)\
**Post date:** [March 31, 2021, 10:26pm UTC](https://swi-prolog.discourse.group/t/term-expansion-issue-when-consult-is-called-from-a-module-was-how-to-get-consult-test-pl-predicatefrominsidetest-to-run/3788/1 "2021-03-31T22:26:25Z")

</div>

I am trying to implement a simple Prolog server with a JSON interface using [this gist](https://gist.github.com/JanWielemaker/d9837fb1858d76d5dfd9caa3851db057) from @jan  
[from this thread](https://swi-prolog.discourse.group/t/multi-session-multi-tenant-web-app-with-swi-prolog/1469/11). I’m running into a problem when using consult/1 to load files. I think it stems from the fact that the server is acting like one big, long running query that never halts. Here’s a simplification of the important code from the server:

```prolog
% Pass in the TCP/IP Stream that commands get sent upon
serve_loop(ReadStream) :-
    read_term(ReadStream, Command, []),
    call(Command),
    serve_loop(ReadStream).

```

Here’s the problem: If I call the server once to consult a file and then once to call a predicate from inside that file, it fails with “ERROR: Unknown procedure”. I _suspect_ it could be related to the behavior below. It appears as though running a single query that includes a consult/1 along with references to terms inside it doesn’t work:

```prolog
% Test.pl
foo(X) :- writeln(X).

```

```prolog
?- consult("/.../Test.pl"), foo(a).
ERROR: Unknown procedure: foo/1 (DWIM could not correct goal)

?- consult("/.../Test.pl").
true.

?- foo(a).
a
true.

```

Any clues as to what is going on and how to fix it?

---

<div class="post-metadata">

**Author:** ![ericzinda](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@ericzinda](https://swi-prolog.discourse.group/u/ericzinda)\
**Post date:** [March 31, 2021, 11:23pm UTC](https://swi-prolog.discourse.group/t/term-expansion-issue-when-consult-is-called-from-a-module-was-how-to-get-consult-test-pl-predicatefrominsidetest-to-run/3788/2 "2021-03-31T23:23:50Z")

</div>

OK, it turns out the Test.pl scenario above _does_ work. Operator error on my part made it fail. What doesn’t work is a more complicated example using term expansion within the files that are consulted. Writing up that scenario now…

---

<div class="post-metadata">

**Author:** ![ericzinda](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@ericzinda](https://swi-prolog.discourse.group/u/ericzinda)\
**Post date:** [April 1, 2021, 12:31am UTC](https://swi-prolog.discourse.group/t/term-expansion-issue-when-consult-is-called-from-a-module-was-how-to-get-consult-test-pl-predicatefrominsidetest-to-run/3788/3 "2021-04-01T00:31:08Z")

</div>

Turns out I think the issue is related to the issue described in [this thread](https://swi-prolog.discourse.group/t/term-expansion-module-x-predicate-x-true-doesnt-expand-if-included-in-module-y/3681/3).

The server is running in a module called “prologServer”:

```prolog
% PrologServer.pl
:- module(prologServer,
          [...]).
...
serve_loop(ReadStream) :-
    read_term(ReadStream, Command, []),
    call(Command),
    serve_loop(ReadStream).
...

```

I first ask the server to `consult("TermExpansion.pl")` which defines a term expansion:

```prolog
% TermExpansion.pl
term_expansion(expandMe(X), [(writeMe(X) :- (writeln(X)))]).

```

Then I then ask the server to `consult("ExpandedTerms")` which defines a term to expand. This file is a module and exports the term that is created by expansion:

```prolog
% ExpandedTerms.pl
:- module(expandedTerms,
          [ writeMe/1
          ]).
expandMe(X).

```

The error I get when I run that sequence is: “ERROR: Exported procedure expandedTerms:writeMe/1 is not defined”.

I _believe_ that when the code in the prologServer module processes `consult("TermExpansion.pl")`, the term\_expansion/2 term is not being loaded into the `user:` module. I suspect it is loaded into the `prologServer` module? Since that is where the code that does `call(Command)` is? Maybe?

If I simply consult the two files from the Prolog top level, it works fine, since the term\_expansion/2 term is put in the `user:` module and found.  
[edit: also note that moving the server code out of a module also fixes it…]

Adding `user:` in front of term\_expansion/2 fixed it when running in the server. Like this:

```prolog
% TermExpansion.pl
user:term_expansion(expandMe(X), [(writeMe(X) :- (writeln(X)))]).

```

Does consult/1 process files differently based on where it is called from? Or is there some other explanation here?
