I am working on a SWI-Prolog project that has modules in prolog/ and a script (also Prolog code) that I start in bin/foo. How can I add prolog/ to load path in this script, so that I can reference modules there without prefixing them? I have this at the beginning of my bin/foo:
But this requires me to use use_module(library(my_module)), whereas I want to be able to use use_module(my_module). What is the file_search_path/2 prefix that represents the “root”?
This always refers to a direct file name that is resolved relative to the file in which it appears. So, if you want to use that notation, your only way is to put your “script” also in the prolog directory and replace the script in bin with a simple one-liner that loads the thing from prolog.
The approach I now tend to take to set relative file search paths is something along these lines. Using here/0 in a module is fine, of course it is not a great name without modules.
here.
user:file_search_path(myapp, Dir) :-
source_file(here, ThisFile),
<compute Dir from ThisFile>.
P.s. alternative to bin, you can make a directory app in a pack. A file myapp.pl can then be started using