Absolute_file_name(home(some), F) failing?

In one of my programs, I try to locate a file using:

absolute_file_name(home(some), Some).
false

This is in SWI-Prolog (threaded, 64 bits, version 9.0.2).
It looks like the alias ‘home’ is not defined, though I believe it was in a previous version of Prolog.
I know there is a work around by adding :

file_search_path(home, Home) :- getenv('HOME', Home).

to my program.

But shouldn’t this important alias be defined out of the box?
Any insights would be appreciated.

A user could have countless files & directories for other purposes, under $HOME. I’d call it bad practice to not put Prolog files in some sort of subdirectory order.

user_app_data looks like a sensible place to put Prolog files, as seen at file_search_path/2

I do agree with you, and actually this is what I do.
But as starting point, I want an alias for home
If you prefer, my example should have been:

absolute_file_name(home('some/some_more/some_evenmore'), Some).

The point is, that I have to add a file_search/2 clause for home, which I didn’t need in previous versions of Prolog!

Have you seen this Wiki page?

Using absolute_file_name/3 and user:file_search_path/2

Some alternatives:

?- getenv('HOME', Home), time(absolute_file_name('a/b/c', F, [relative_to(Home)])).
% 73 inferences, 0.000 CPU in 0.000 seconds (80% CPU, 1350077 Lips)
F = '/home/me/a/b/c'.
?- getenv('HOME', Home), time(atom_concat(Home, '/a/b/c', F)).
% 1 inferences, 0.000 CPU in 0.000 seconds (79% CPU, 120977 Lips)
F = '/home/me/a/b/c'.

Thanks for this link.
I am actually aware of how absolute_file_name/2 should work in relation with file_search_path/2.
My point, again, is that, unless I am wrong, swipl knew about a file_search_path of home.
i.e. in a previous version, the following query yielded Home= instead of failing.

Welcome to SWI-Prolog (threaded, 64 bits, version 9.0.2)
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).

?- file_search_path(home, Home).
false.

When I wrote that Wiki the SWI-Prolog documentation noted in absolute_file_name/3 for the option expand(Boolean)

This is a SWI-Prolog extension intended to minimise porting effort after SWI-Prolog stopped expanding environment variables and the ~ by default.

which would seem to confirm your suspicions.


When I started using user:file_search_path/N and absolute_file_name/3 I too thought that most code would be referenced from home or in my case on Windows C:\Users\Groot. But after having learned about prolog_load_context/2 I now add a directive like

:- prolog_load_context(directory, Dir),
   asserta(user:file_search_path(myapp, Dir)).

to any of my code that is more than one file. This also makes the code much more portable as it can transition to other users, other install locations and other operating systems, just works and is no longer dependent on environment variables such as home. :slightly_smiling_face:


FWIW, ~ is understood by absolute_file_name/2:

?- absolute_file_name('~/src/advent-of-code-2022/7.pl', Z).
Z = '/home/peter/src/advent-of-code-2022/~/src/advent-of-code-2022/7.pl'.

?- absolute_file_name(home(src/advent-of-code-2022/'7.pl'), Z).
false.

I don’t have an old version of swipl to see if home(...) used to be defined; but it’s not given in the definition of file_search_path/2.

Needs the expand option (which defaults to false):

$ cd ~/temp
$ swipl
?- absolute_file_name('~/a/b/c', F).
% Should not contain 'temp/'
F = '/home/me/temp/~/a/b/c'.

?- time(absolute_file_name('~/a/b/c', F, [expand(true)])).
% 67 inferences
F = '/home/me/a/b/c'.

Just seems a bit like overkill.