Workaround ensure_loaded/1 B-Prolog?

145 views Asked by At

Is there a workaround to make ensure_loaded/1 work in B-Prolog as it works in many other Prolog systems? The goal is to have a preamble so that the rest of code can use ensure_loaded/1 independent of whether which Prolog system I use.

  • It seems that it does not resolve a relative path to the currently consulted file, as many Prolog systems do.
  • It seems that it does not allow a Prolog text but expects some byte code, which would force me to compile stuff.

So I tried the following:

:- set_prolog_flag(redefine_builtin, on).
ensure_loaded(X) :-
    atom_concat('<base>\\',X,Y),
    consult(Y).
:- set_prolog_flag(redefine_builtin, off).

But when a Prolog text with the following directive is consulted, I wont work:

:- ensure_loaded('suite.p').

It still doesn't find suite.p.

What could I do?

Bye

2

There are 2 answers

2
Paulo Moura On

Regarding the expansion of paths, in the Logtalk adapter file for B-Prolog I (must) use:

% '$lgt_expand_path'(+nonvar, -atom)
%
% expands a file path to a full path

'$lgt_expand_path'(Path, ExpandedPath) :-
    % first expand any environment variable
    expand_environment(Path, ExpandedPath0),
    (   (   sub_atom(ExpandedPath0, 0, 1, _, '/')
            % assume POSIX full path 
        ;   sub_atom(ExpandedPath0, 1, 1, _, ':')
            % assume Windows full Path starting with a drive letter followed by ":"
        ) ->
        % assume full path
        ExpandedPath = ExpandedPath0
    ;   % assume path relative to the current directory
        working_directory(Current),
        atom_concat(Current, '/', Directory),
        atom_concat(Directory, ExpandedPath0, ExpandedPath)
    ).

It's basically an hack (that can be improved by e.g. trying to find first in which OS you're running) for missing functionality that should be provided by B-Prolog itself.

0
AudioBubble On

I could only arrive at the following analysis and workaround.

The set_prolog_flag(redefine_builtin, on) doesn't work inside a Prolog text for B-Prolog. I get:

B-Prolog Version 8.1
?- consult('<base>\\bprolog.p').
consulting::<base>\bprolog.p
** Error  : Trying to redefine built-
     in:'<base>\\bprolog.p',18::ensure_loaded/1
*** error(file_not_found,suite.p)

When I do the set_prolog_flag(redefine_builtin, on) in the top-level, things are fine:

?- set_prolog_flag(redefine_builtin, on).
?- consult('<base>\\bprolog.p').
consulting::<base>\bprolog.p
consulting::<base>\suite.p
Etc.. 

Bye