# Using consult in a directive to load source code; it puzzles me?

**URL:** <https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081>\
**Category:** Help!\
**Created:** [March 31, 2020, 12:03pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081 "2020-03-31T12:03:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 31, 2020, 12:03pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/1 "2020-03-31T12:03:19Z")

</div>

In the EDCG package ([src](https://github.com/mndrix/edcg/blob/82cf8c93a64a706d91b2e8a1907b92437d634d43/prolog/edcg.pl)) are the directives to load dependent source code files

```prolog
:- ['acc-pass.pl'].
:- ['special-util.pl'].
:- ['generic-util.pl'].

```

Normally I would expect the code to be either in the same source code file or loaded using [use\_module](https://www.swi-prolog.org/pldoc/man?section=import).

Since the code is loaded this way my editor [(VSC)](https://code.visualstudio.com/) with plug-in [(VSC-Prolog)](https://marketplace.visualstudio.com/items?itemName=arthurwang.vsc-prolog) is flagging many of the predicates as not recognized. This is more of an annoyance than a concern for now.

I am trying to reason why the code was loaded like this; is it something I should learn about or something that I can just know exist and move on without worries?

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [March 31, 2020, 12:13pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/2 "2020-03-31T12:13:11Z")

</div>

> [@EricGT](#):
>
> ```prolog
> :- ['acc-pass.pl']. :- ['special-util.pl']. :- ['generic-util.pl'].
> 
> ```

Coding style guideline:  
Never ever use **top-level interpreter** shortcuts in source files.

P.S. I know that you are not the author of that package. In that particular case, the `include/1` directive should have been used.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 31, 2020, 12:26pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/3 "2020-03-31T12:26:07Z")

</div>

> [@pmoura](#):
>
> the `include/1` directive should have been used.

So changing

```prolog
:- ['acc-pass.pl'].
:- ['special-util.pl'].
:- ['generic-util.pl'].

```

to

```prolog
:- include('acc-pass.pl').
:- include('special-util.pl').
:- include('generic-util.pl').

```

fixed all the warning messages and the code loads and works.

* * *

**EDIT**

Since some of the files had redundant include commands, refactored them all into the first file as

```prolog
:- include('messages.pl').
:- include('generic-util.pl').
:- include('special-util.pl').
:- include('acc-pass.pl').

```

and commented out the includes in the included files. The code still loads and works, but the editor is now showing some `not defined predicate` warnings, but not as many as before. Since the code is small I will just leave all the includes in the included files active and not be concerned that the predicates are being overwritten a few times. 🙂

Thanks Paulo.

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [March 31, 2020, 12:44pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/4 "2020-03-31T12:44:36Z")

</div>

> [@EricGT](#):
>
> The code still loads and works, but the editor is now showing some `not defined predicate` warnings, but not as many as before. Since the code is small I will just leave all the includes in the included files active and not be concerned that the predicates are being overwritten a few times. 🙂

Looked into the source files. The `include/1` directives are only required in the main source file, `edcg.pl`. The VSC plug-in is likely looking into each file in isolation, hence the warnings. This is plug-in issue, not a Prolog issue.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 31, 2020, 12:51pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/5 "2020-03-31T12:51:47Z")

</div>

> [@pmoura](#):
>
> This is plug-in issue, not a Prolog issue.

Totally agree. 🙂

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 31, 2020, 1:05pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/6 "2020-03-31T13:05:48Z")

</div>

The clean solution is probably to turn the utilities into their own module and use use\_module/1,2. IMO include has very little reason to exist. In some cases you can use it to share e.g., multifile declarations.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 31, 2020, 4:25pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/7 "2020-03-31T16:25:55Z")

</div>

Hmmm … that package has been [“archived” in github](https://github.com/mndrix/edcg) and hence can’t be modified. (The original code was written ~30 years ago, so no surprise that it doesn’t use modules.)

I don’t mind updating the code to use modules; what’s the best way of handling this situation? Presumably, I’d clone the code into my github repository and update it; but can I replace the SWI-Prolog “pack”?

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [March 31, 2020, 5:32pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/8 "2020-03-31T17:32:18Z")

</div>

The code already uses a module, `edcg`. The code in the other files is not useful on their own. This makes the `include/1` solution preferable to converting those other files into modules and then use `use_module/1-2` directives. If you look into e.g. the `acc-pass.pl` file, there’s no point in having it as a module as the exports are only meaningful for the `edcg` module. Same for the other files (except `edcg.pl`, of course).

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 31, 2020, 5:40pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/9 "2020-03-31T17:40:30Z")

</div>

> [@pmoura](#):
>
> The code in the other files is not useful on their own.

I have to agree with Paulo on this one. As I see it there really only needs to be one source code file and one module.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 31, 2020, 6:31pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/10 "2020-03-31T18:31:27Z")

</div>

Agreed – the total line count is only 319, which is small enough for a single file.

So, if I copy this to my own github repository, I presume I can replace the existing “pack”? Are there any downsides from me taking ownership? (I’ll send a message to the current owner, to tell him what I’m doing; and will acknowledge his work, of course.)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 31, 2020, 8:28pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/11 "2020-03-31T20:28:21Z")

</div>

I need to change the location. Just send me the new location when ready.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 31, 2020, 9:25pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/12 "2020-03-31T21:25:52Z")

</div>

It is your call, but when I first started using this the use of the [library(tap)](https://www.swi-prolog.org/pack/list?p=tap) seemed a bit dated.

If you are updating the tests, please also consider completing the test using `castor` and `pollux` or removing them. I only figured out what they meant by reading about them somewhere, and I can not even find that somewhere again. 🤔

* * *

**EDIT**

My test for `castor` and `pollux`

> **Click triangle to reveal test**
>
> ```prolog
> % NB Example from https://www.swi-prolog.org/pack/file_details/edcg/t/examples.pl is not complete
> % Complete example exits in https://www2.eecs.berkeley.edu/Pubs/TechRpts/1990/CSD-90-583.pdf
> 
> % Declare accumulators
> edcg:acc_info(castor,_,_,_,true).
> 
> % Declare passed arguments
> edcg:pass_info(pollux).
> 
> % Declare predicates using these hidden arguments
> edcg:pred_info(p,1,[castor,pollux]). % 1 - X
> edcg:pred_info(q,1,[castor,pollux]). % 1 - Y
> edcg:pred_info(r,1,[castor,pollux]). % 1 - Y
> 
> % The program
> p(X) -->>
> Y is X + 1,
> q(Y),
> r(Y).
> 
> q(Y,_,_,P) :-
> format('q - Y: ~w, P: ~w~n',[Y,P]).
> r(Y,_,_,P) :-
> format('r - Y: ~w, P: ~w~n',[Y,P]).
> 
> % ?- listing(p).
> % p(X, A, B, C) :-
> % Y is X+1,
> % q(Y, A, D, C),
> % r(Y, D, B, C).
> 
> % Variables names changed for clarity
> %
> % p(X, Castor_1, Castor_3, Pollux) :-
> % Y is X+1,
> % q(Y, Castor_1, Castor_2, Pollux),
> % r(Y, Castor_2, Castor_3, Pollux).
> 
> % -------------------------------------
> 
> :- begin_tests(p).
> 
> test(p) :-
> with_output_to(string(Output),p(1,a,a,c)),
> 
> assertion( Output == "q - Y: 2, P: c\nr - Y: 2, P: c\n" ).
> 
> :- end_tests(p).
> 
> ```

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [March 31, 2020, 9:45pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/13 "2020-03-31T21:45:10Z")

</div>

> [@EricGT](#):
>
> > [@pmoura](#):
> >
> > This is plug-in issue, not a Prolog issue.
> 
> Totally agree. 🙂

I assume that the VSC Prolog plug-in you mentioned is the one wrote by @ArthurWang? He also wrote a nice VSC Logtalk plug-in that is affected by a similar issue. The solution could be, in both cases, to have the plug-in calling the make tools if a file in a project is modified. This, of course, implies that the code was loaded at least once and that the Prolog process is kept alive. If that can be done, it would help if there is some plug-in interface to select a file in the project as the project loader file (assuming such a file exists; but that’s often the case).

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [March 31, 2020, 10:31pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/14 "2020-03-31T22:31:44Z")

</div>

> [@EricGT](#):
>
> My test for `castor` and `pollux`

That drove my attention that I was also missing a test for that example in the Logtalk port of EDCGs. Added as:

```logtalk
test(gemini_1) :-
	gemini::p(1, S0, S, _),
	S0 == S.

```

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [April 1, 2020, 12:34am UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/15 "2020-04-01T00:34:21Z")

</div>

Please file missing test cases, bugs, etc. for pack(edcg) against [https://github.com/kamahen/edcg](https://github.com/kamahen/edcg)  
(assuming that I’ll take it over and update the pack accordingly)

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [April 8, 2020, 7:46pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/16 "2020-04-08T19:46:44Z")

</div>

I’ve taken over maintenance of EDCG and have added issues for updating the tests.

> **[kamahen/edcg](https://github.com/kamahen/edcg/issues)**
>
> Extended DCG syntax for Prolog by Peter Van Roy. Contribute to kamahen/edcg development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [April 8, 2020, 7:48pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/17 "2020-04-08T19:48:53Z")

</div>

Does making the code work with library plunit imply that the code would be modified to be work with modules, or should that be another issue?

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [April 8, 2020, 8:06pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/18 "2020-04-08T20:06:42Z")

</div>

Yes, I should make EDCG work better with modules. You might want to open an issue about that as well. (It’ll probably be a little while before I get to it, though …).

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [April 8, 2020, 8:18pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/19 "2020-04-08T20:18:20Z")

</div>

I wasn’t expecting you to make the changes.

I plan to give it a try myself but suspect I will do it wrong, post to seek feedback, nicely be shown my mistakes, learn and then rinse and repeat.

The primary reason I see for adding it as an issue is so that those who try to use EDCG with modules and fail will know that it was not their fault but an issue with the code. 🙂

---

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [April 8, 2020, 9:21pm UTC](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081/20 "2020-04-08T21:21:07Z")

</div>

I just reinstalled the pack and I got release 0.9.0.2, but looking at github the release is 0.9.2.

I noticed you updated the download link, my guess is that Jan would have to manually update the download link also due to the security measures?

[Next page](https://swi-prolog.discourse.group/t/using-consult-in-a-directive-to-load-source-code-it-puzzles-me/2081.md?page=2)
