How to get current working directory

656 views Asked by At

I'm new with yap (and with Prolog in general), and I can't figure out how to get the current directory.

In fact, I can't make sense of the documentation:

working_directory(-CurDir,?NextDir)

Fetch the current directory at CurDir. If NextDir is bound to an atom, make its value the current working directory.

I don't understand what's meant by "at CurDir".

Also, I can't find documentation on the - and ? preceding the formal arguments. (I guess that the ? means that the argument is optional, but I have no clue about the -.)

Bottom line, I can't figure out how to use this information to query for the current working directory.


I've tried many blind guesses, and always get either no. or an error in response. E.g.:

$ yap
% Restoring file /usr/lib/Yap/startup.yss
YAP 6.2.2 (x86_64-linux): Sat Nov 23 17:51:47 UTC 2013
   ?- working_directory(CurDir).
no
   ?- working_directory(-CurDir).
no
   ?- working_directory().
     SYNTAX ERROR at user, near line 7:
 working_directory(
<==== HERE ====>
 ).
   ?- working_directory.
no
   ?- CurDir.
     ERROR!!
     INSTANTIATION ERROR- meta_call(_131099): expected bound value
   ?- -CurDir.
no
   ?- working_directory('.').
no
   ?- working_directory(-'.').
no

I have two questions:

  1. Where can I find meta-documentation on the -, ?, and + that appear in front of formal arguments in the yap documentation?
  2. How do I get the current working directory?

UPDATE: The following also fail:

$ yap
% Restoring file /usr/lib/Yap/startup.yss
YAP 6.2.2 (x86_64-linux): Sat Nov 23 17:51:47 UTC 2013
   ?- working_directory(X, '').
no
   ?- working_directory(X, X).
no
1

There are 1 answers

6
Jim Ashworth On
  1. Meta-documentation can be found in the SWI-Prolog manual here, but will be the same for Yap (couldn't find any reference in the Yap manual for notation at a glance).

  2. Current working directory (CWD) can be retrieved like so:

working_directory(X,'').

... which means unify X with the CWD and change it to nothing (counter-intuitive, I know, but I didn't make it), or:

working_directory(X,X).

... as pointed out by @PauloMoura, which means unify X with the CWD and change it to itself (which also seems weird to me, but c'est la vie).

CWD can be changed like so:

working_directory(X,'Some New Directory').

... which will unify X with the CWD then change the CWD to 'Some New Directory', so X will be the Previous Working Directory.