gprolog
gprolog copied to clipboard
Feature request ensure_loaded/1 [ISO 7.4.2.8]
Isn't ensure_loaded/1 a directive defined in the ISO core standard [ISO 7.4.2.8]. Currently it seems to be implemented by GNU Prolog as follows:
handle_directive(ensure_loaded, _, _) :-
!, warn('ensure_loaded directive not supported - directive ignored', []).
Wouldn't it be possible to bring ensure_loaded/1 to GNU Prolog. I understand that it might be difficult, because of the various stages and artefact transformations involved in GNU Prolog. But maybe a little separate compilation and later linking
would do the job. So that an ensure_loaded/1 directive would spawn a separate compilation, put the artefact on a list, and at the end of a consult batch all separate artefacts are linked together or otherwise integrated.
ensure_loaded/1 is not yet implemented (it has some issues for a native compiler). gprolog proposes the ensure_linked/1 directive which could help you.
Currently the work around is to use include/1. But include/1 is not smart, it cannot deal with an import graph. The main benefit of ensure_loaded/1 would be to avoid duplicate
include/1, which is not part of the requirement of include/1. So one workaround would be if one could define user directives, like:
:- dynamic(visited/1).
ensure_loaded(Path) :-
visited(Path), !.
ensure_loaded(Path) ;-
assertz(visited(Path)),
include(Path).
ensure_loaded/1 is not yet implemented (it has some issues for a native compiler). gprolog proposes the ensure_linked/1 directive which could help you.
As a side note, Logtalk provides the functionality of the ensure_loaded/1
directive. See:
https://logtalk.org/manuals/userman/programming.html?highlight=reload#flag-reload
Logtalk is not ISO core standard compliant in the way it deals with Prolog texts. The minimum Logtalk would need to provide would be include/1 and ensure_loaded/1. But it begs the question whether
any useful semantic can be made, Logtalk compilation units have a name braket. On the other hand the ISO core standard indeed specifies ensure_loaded/1 based on include/1. It mentions include explicitly:
But there is a small gap, the standard doesn't elaborate when
Prolog text designators denote the same Prolog text. So I have
a new proposal, assuming a predicate absolute_file_name/2
that
then is responsible for the sameness:
ensure_loaded(Path) :-
absolute_file_name(Path, AbsPath),
\+ visited(AbsPath), !,
assertz(visited(AbsPath)),
include(Path).
ensure_loaded(_).
But a challenge can be to have visited/1 automatically flushed sometimes, so as to regain re-consult semantics.