Where is SWI-Prolog searching for a library?

This is based on a reply by Jan W.

Prolog code often contains directives like :- use_module(library(clpfd)).

To see the all of the possible places that library will use run this query.

absolute_file_name(library('.'), Dir, [file_type(directory), access(write), solutions(all)]).
Example output (Click triangle to expand)

Note: I have a lot of SWI-Prolog packs installed so this list is probably much larger than your result.

?- absolute_file_name(library('.'), Dir, [file_type(directory), access(write), solutions(all)]).
Dir = 'c:/program files/swipl/library' ;
Dir = 'c:/program files/swipl/library/clp' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/bio_analytics/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/bio_db/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/bio_db_repo/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/body_reordering/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/callgraph/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/chess_db/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/clitable/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/cli_table/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/dcg4pt/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/dcgutils/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/debug_call/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/delay/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/dictoo/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/djson/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/dpath/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/edcg/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/fileutils/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/fnotation/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/func/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/function_expansion/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/genutils/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/graphql/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/gvar_syntax/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/gvterm/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/instant_prolog_docs/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/interpolate/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/lambda/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/lib/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/list_util/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/logicmoo_utils/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/markdown/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/mathml/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/matrix/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/mtx/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/multimodal_dcg/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/options/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/os_lib/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/pack_errors/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/pfc/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/pljulia/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/pl_omdb/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/predicate_streams/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/prolog_graphviz/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/prolog_imagemagick/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/prolog_library_collection/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/prolog_rdf/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/prolog_uriparser/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/prosqlite/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/pubmed/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/quickcheck/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/rdet/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/rdfs2pl/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/real/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/refactor/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/regex/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/reif/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/resp/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/rocksdb/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/slack_prolog/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/sldnfdraw/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/sparqlprog/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/stoics_lib/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/swipe/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/switex/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/tap/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/typedef/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/uri_qq/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/wgraph/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/xlibrary/prolog' ;
Dir = 'c:/users/groot/appdata/local/swi-prolog/pack/xtools/prolog' ;
Dir = 'c:/program files/swipl/xpce/prolog/lib'.

As many of us know Operating Systems have environment variables such as PATH that are used to find executable files.

SWI-Prolog has something similar with absolute_file_name/2,3.

This is most commonly seen and used with use_module/1,2 as a directive (:-), e.g.

:- use_module(library(clpfd)).

What many new to using SWI-Prolog don’t know is that the alias library is like the environment variable PATH.

From absolute_file_name/3

Convert the given file specification into an absolute path. Spec is a term Alias(Relative) (e.g., (library(lists) ), a relative filename or an absolute filename. The primary intention of this predicate is to resolve files specified as Alias(Relative). This predicate only returns non-directories , unless the option file_type(directory) is specified. Option is a list of options to guide the conversion:


The see the current set of values for Alias use the query

setof(Name,(Dir^file_search_path(Name,Dir)),Names).
Example output (Click triangle to expand)

Note: This query adds ;true so that the whole answer can be seen.
See: Help: I want the whole answer

From SWI-Prolog installed on Windows 10

?- setof(Name,(Dir^file_search_path(Name,Dir)),Names);true.
Names = [app_config, app_data, app_preferences, autoload, common_app_config, common_app_data, foreign, library, pack, path, pce, swi, user_app_config, user_app_data, user_profile] ;
true.

From SWI-Prolog installed on Ubuntu 20.04 running on WSL 2.

?- setof(Name,(Dir^file_search_path(Name,Dir)),Names);true.
Names = [app_config, app_data, app_preferences, autoload, foreign, library, pack, path, pce, swi, user_app_config, user_profile] ;
true.

To add items to an Alias use file_search_path/2.


Another very useful Alias to add is for your own app which is typically named myapp.
(ref 1) (ref 2)

1 Like

don’t you think that it would be useful added in standard features ? (at minimum in SWI Prolog internet site examples)