# My mess with modules and undefined predicates, I.e. current\_directory/1

**URL:** <https://swi-prolog.discourse.group/t/my-mess-with-modules-and-undefined-predicates-i-e-current-directory-1/4174>\
**Category:** Help!\
**Tags:** error\_message\
**Created:** [July 19, 2021, 10:06am UTC](https://swi-prolog.discourse.group/t/my-mess-with-modules-and-undefined-predicates-i-e-current-directory-1/4174 "2021-07-19T10:06:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BlondMammuth](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@BlondMammuth](https://swi-prolog.discourse.group/u/BlondMammuth)\
**Post date:** [July 19, 2021, 10:06am UTC](https://swi-prolog.discourse.group/t/my-mess-with-modules-and-undefined-predicates-i-e-current-directory-1/4174/1 "2021-07-19T10:06:50Z")

</div>

I’m using: SWI-Prolog version (threaded, 64 bits, version 7.6.4)

I want the code to: use the predicate current\_directory/1

But what I’m getting is:  
ERROR: Undefined procedure: file\_systems:current\_directory/1

My code (currently, after many experiments and changes) looks like this:

```prolog
test :-
	file_systems:current_directory(CD),
	write(CD).

```

Some explanation: I keep getting errors of “undefined procedures” (if I remember correctly), in other words, the interpreter does not seem to find the definitions of the predicates I want to use.

It is quite probable that I am making a stupid systematic mistake I fail to see, but anyway, nothing seems to work. I have tried use\_module(library(file\_systems)), and got the message that this library cannot be found. I have tried the predicate with and without a module specifier in front. the whole code within and without a module itself, I have tried the same on two different computers. Nothing seems to work, and I am half expecting a hint towards a very stupid and embarrassing mistake on my side, given that I am not a beginner, but a SWIPL user of decades. So please, enlighten/embarrass me if you can. Thanks in advance!

---

<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:** [July 19, 2021, 10:32am UTC](https://swi-prolog.discourse.group/t/my-mess-with-modules-and-undefined-predicates-i-e-current-directory-1/4174/2 "2021-07-19T10:32:17Z")

</div>

It seems you want to list the files in the current directory.

The way to do that with SWI-Prolog is using [directory\_files/2](https://www.swi-prolog.org/pldoc/doc_for?object=directory_files/2).  
The documentation for [directory\_files/2](https://www.swi-prolog.org/pldoc/doc_for?object=directory_files/2) shows

**directory\_files** (+Directory, -Entries)

The `+` means the value for Directory must be ground upon calling.

The way to get the current current directory with SWI\_Prolog is to use [working\_directory/2](https://www.swi-prolog.org/pldoc/man?predicate=working_directory/2).  
The documentation for [working\_directory/2](https://www.swi-prolog.org/pldoc/man?predicate=working_directory/2) shows

**working\_directory** (-Old, +New)

but the associated text

> Unify Old with an absolute path to the current working directory and change working directory to New. Use the pattern `working_directory(CWD, CWD)` to get the current directory. See also [absolute\_file\_name/2](https://www.swi-prolog.org/pldoc/man?predicate=absolute_file_name/2) and [chdir/1](https://www.swi-prolog.org/pldoc/man?predicate=chdir/1).bug Note that the working directory is shared between all threads.

notes that `working_directory(CWD, CWD)` returns the current directory.

* * *

Here is example code demonstrating the predicates. The example shows that SWI-Prolog top level is started and then that the predicates are available without loading any modules.

```prolog
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.26)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- working_directory(Directory,Directory).
Directory = 'c:/users/groot/documents/prolog/'.

?- working_directory(Directory,Directory),directory_files(Directory,Entries).
Directory = 'c:/users/groot/documents/prolog/',
Entries = ['.', ..].

```

HTH

* * *

**EDIT**

If you are using the Prolog top level and just want to quickly show the name of the current directory then [pwd/0](https://www.swi-prolog.org/pldoc/doc_for?object=shell%3Apwd/0) works.

```prolog
?- pwd.
% c:/users/groot/documents/prolog/
true.

```

---

<div class="post-metadata">

**Author:** ![BlondMammuth](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@BlondMammuth](https://swi-prolog.discourse.group/u/BlondMammuth)\
**Post date:** [July 19, 2021, 11:16am UTC](https://swi-prolog.discourse.group/t/my-mess-with-modules-and-undefined-predicates-i-e-current-directory-1/4174/3 "2021-07-19T11:16:09Z")

</div>

Thanks for the hint! In fact I wanted to continue an old, half finished project, and it did not find a file. So I tried to get the current directory using the mentioned current\_directory. Just tried out, working\_directory works as described. So thank you, this is at least a workaround for noiw, but of course it would still be interesting in its own right what mistake I’ve made before.

---

<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:** [July 19, 2021, 11:17am UTC](https://swi-prolog.discourse.group/t/my-mess-with-modules-and-undefined-predicates-i-e-current-directory-1/4174/4 "2021-07-19T11:17:36Z")

</div>

If you watch carefully in the docs you can see that current\_directory/1 is part of the SICStus Prolog emulation layer. Normally, do not use stuff from the emulation layers unless you want to write code that is compatible with the emulated dialect. 99% has a simple replacement. In any case, just using `Module:Goal` generally does not work. You need to use use\_module/1,2 to load and import the module, after which you can simply call is as `Goal`.

I think the docs should make it more explicit that predicates come from an emulation layer (and which).

---

<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:** [July 19, 2021, 11:21am UTC](https://swi-prolog.discourse.group/t/my-mess-with-modules-and-undefined-predicates-i-e-current-directory-1/4174/5 "2021-07-19T11:21:36Z")

</div>

> [@jan](#):
>
> If you watch carefully in the docs you can see that current\_directory/1 is part of the SICStus Prolog emulation layer.

Thanks.

I initially could not find current\_directory/1 but after reading your reply used the search feature of the SWI-Prolog documentation to find the predicate.

![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/4/4263c4515d27c2a9ad2dd8f5706f265f769c7621.png)

[https://www.swi-prolog.org/search?for=current\_directory](https://www.swi-prolog.org/search?for=current_directory)

---

<div class="post-metadata">

**Author:** ![BlondMammuth](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@BlondMammuth](https://swi-prolog.discourse.group/u/BlondMammuth)\
**Post date:** [July 19, 2021, 11:33am UTC](https://swi-prolog.discourse.group/t/my-mess-with-modules-and-undefined-predicates-i-e-current-directory-1/4174/6 "2021-07-19T11:33:01Z")

</div>

Ah, thanks, this makes at least clear why the direct call has not worked, and no, I do not require compatibility. So that settles a lot and works for me.

Just for completeness: I have tried the use\_module approach, both with  
use\_module(library(file\_systems))  
and  
use\_module(file\_systems),

none of them worked, the interpreter answered with

```
    source_sink `library(file_systems)' does not exist

```

and

```
    source_sink `file_systems' does not exist

```

respectively

I have used the following lines there:

```prolog

:- module(moduletest, [test/0]).
:- use_module(library(file_systems)).

test :-
	current_directory(CD),
	write(CD).

```

(with and without '`library(..)'`)

Concerning the docmentation, from my experience I can agree that sometimes it is not immediately clear for me to determine where a predicate comes from. In this case, I have seen its origin library first from the URL of the help page. I might have seen it from the content list on the left side, too. Long term it would probably be better to keep that information and more about the origin closer to the text.

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [July 19, 2021, 1:22pm UTC](https://swi-prolog.discourse.group/t/my-mess-with-modules-and-undefined-predicates-i-e-current-directory-1/4174/7 "2021-07-19T13:22:50Z")

</div>

> [@BlondMammuth](#):
>
> Just for completeness: I have tried the use\_module approach, both with  
> use\_module(library(file\_systems))  
> and  
> use\_module(file\_systems),
> 
> none of them worked

The correct way to do it would have been:

```prolog
use_module(library(dialect/sicstus4/file_systems))

```

Admittedly this is also not immediately obvious from the docs.

---

<div class="post-metadata">

**Author:** ![BlondMammuth](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@BlondMammuth](https://swi-prolog.discourse.group/u/BlondMammuth)\
**Post date:** [July 19, 2021, 4:49pm UTC](https://swi-prolog.discourse.group/t/my-mess-with-modules-and-undefined-predicates-i-e-current-directory-1/4174/8 "2021-07-19T16:49:37Z")

</div>

Thanks for this information. This settles the whole phenomenon. And yes, I agree - I was already wondering whether it would be a nested module name, but did not find indication thereof in the docs (or didn’t search thoroughly enough).
