# Multiple modules definitions in one file?

**URL:** <https://swi-prolog.discourse.group/t/multiple-modules-definitions-in-one-file/5549>\
**Category:** Predicate\
**Tags:** module\
**Created:** [June 23, 2022, 8:42am UTC](https://swi-prolog.discourse.group/t/multiple-modules-definitions-in-one-file/5549 "2022-06-23T08:42:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chansey97](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@chansey97](https://swi-prolog.discourse.group/u/chansey97)\
**Post date:** [June 23, 2022, 8:42am UTC](https://swi-prolog.discourse.group/t/multiple-modules-definitions-in-one-file/5549/1 "2022-06-23T08:42:21Z")

</div>

Reading the [DCG tutorial](https://www.amzi.com/manuals/amzi/pro/ref_dcg.htm), it seems that he can define multiple modules in one file by `end_module/1`:

```prolog
%% module1
:- module(english).
:- export(sentence/3).

% ...

:- end_module(english).

%% module2
:- module(spanish).
:- export(sentence/3).

% ...

:- end_module(spanish).

```

```prolog
translate(LANG_IN, LANG_OUT, SENTENCE, TRANSLATION) :-
  call( LANG_IN:sentence(MEANING, SENTENCE, []) ),
  call( LANG_OUT:sentence(MEANING, TRANSLATION, []) ).

?- translate(english, spanish, [the,dog,chases,the,cow], T).
T = [el, perro, caza, la, vaca] 
yes

```

Does SWI-Prolog support the similar feature? I can’t found it in [SWI-Prolog module manual](https://www.swi-prolog.org/pldoc/man?section=defmodule).

Also, I can’t run his example in an elegant way.

english.pl

```prolog
:- module(english, [sentence/3]).
sentence(s(S,V,O)) --> subject(S), verb(V), object(O).
subject(sb(M,N)) --> modifier(M), noun(N).
object(ob(M,N)) --> modifier(M), noun(N).

modifier(m(the)) --> [the].
noun(n(dog)) --> [dog].
noun(n(cow)) --> [cow].
verb(v(chases)) --> [chases].
verb(v(eats)) --> [eats].

```

spanish.pl

```prolog
:- module(spanish, [sentence/3]).
sentence(s(S,V,O)) --> subject(S), verb(V), object(O).
subject(sb(M,N)) --> modifier(M, G), noun(N, G).
object(ob(M,N)) --> modifier(M, G), noun(N, G).

modifier(m(the),m) --> [el].
modifier(m(the),f) --> [la].
noun(n(dog),m) --> [perro].
noun(n(cow),f) --> [vaca].
verb(v(chases)) --> [caza].
verb(v(eats)) --> [come].

```

translate\_module\_conflict.pl

```prolog
:- ['english.pl'].
:- ['spanish.pl'].

translate(LANG_IN, LANG_OUT, SENTENCE, TRANSLATION) :-
  call( LANG_IN:sentence(MEANING, SENTENCE, []) ),
  call( LANG_OUT:sentence(MEANING, TRANSLATION, []) ).

```

```prolog
?- consult(["translate_module_conflict.pl "]).
ERROR: e:/work-pl/prolog/code/dcg/amzi/language_translation/translate_module_conflict.pl :8:
ERROR: import/1: No permission to import spanish:sentence/3 into user (already imported from english)

```

Thanks.

---

<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:** [June 23, 2022, 9:31am UTC](https://swi-prolog.discourse.group/t/multiple-modules-definitions-in-one-file/5549/2 "2022-06-23T09:31:29Z")

</div>

> [@chansey97](#):
>
> `ERROR: import/1: No permission to import spanish:sentence/3 into user (already imported from english)`

Use

```
:- use_module(english, []).

```

Multiple modules in a file are supported with the low level primitives. For example library(plunit) uses this to place the test sets in what is actually a submodule of the module it is contained in.

---

<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:** [June 23, 2022, 10:20am UTC](https://swi-prolog.discourse.group/t/multiple-modules-definitions-in-one-file/5549/3 "2022-06-23T10:20:44Z")

</div>

> [@jan](#):
>
> For example library(plunit) uses this to place the test sets in what is actually a submodule of the module it is contained in.

For others looking for the source code to see how this is done.

If one expects to find the source by going to GitHub for the [SWI-Prolog account](https://github.com/SWI-Prolog) and look at the [development repository](https://github.com/SWI-Prolog/swipl-devel) of GitHub one will find the [library subdirectory](https://github.com/SWI-Prolog/swipl-devel/tree/master/library) but plunit is not there.

The code for plunit is actually in the repository [packages-plunit](https://github.com/SWI-Prolog/packages-plunit). The use of `library` in alias `library(plunit)` is for the hook file\_search\_path/2 when the code is loaded. Understanding that is an acquired skill.

The code dealing with unit test, test modules is [here](https://github.com/SWI-Prolog/packages-plunit/blob/9659d914b0518d61ba91fe8ec6aba5e717ebe513/plunit.pl#L264-L332).
