0

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
Community
  • 1
  • 1
kjo
  • 33,683
  • 52
  • 148
  • 265

1 Answers1

0
  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.

Jim Ashworth
  • 765
  • 6
  • 17
  • 1
    To query the current directory without changing it, the usual idiom is `working_directory(X,X)`. – Paulo Moura Feb 22 '17 at 20:37
  • @PauloMoura Aha - that makes sense, kinda. – Jim Ashworth Feb 22 '17 at 20:57
  • Thanks for the pointer to the SWI-Prolog docs. I tried both `working_directory(X, '').` and `working_directory(X, X).`, but they both result in `no.`. (I've updated my post accordingly.) – kjo Feb 23 '17 at 14:42
  • @kjo Are you certain that your installation is a-ok, including all the packages shared with SWI? – Jim Ashworth Feb 23 '17 at 15:04
  • @JimAshworth: I installed `yap` with `apt-get install yap`... For good measure, I just re-installed `yap` from scratch (`apt-get purge swi-prolog yap; apt-get autoremove --purge; apt-get install yap`); I still get the same results I showed before. FWIW, when I installed `yap` for the second time just now, `apt-get` did not report installing any additional packages. How can I troubleshoot my installation further? – kjo Feb 23 '17 at 15:39
  • @kjo Getting out of my comfort zone right now (never actually installed Yap, and not near Unix at the moment), but you could try http://stackoverflow.com/questions/12886179/prolog-how-to-check-if-a-predicate-exists to check that working_directory/2 exists? – Jim Ashworth Feb 23 '17 at 15:47