This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/pl/imports.yap

161 lines
4.2 KiB
Plaintext
Raw Normal View History

2018-06-05 20:51:49 +01:00
/**
2019-02-13 09:44:24 +00:00
** @file imports.yap
2018-06-05 20:51:49 +01:00
*
* @brief Module systemm code to import predicates
*
* This code does not provide visible builtins.
*/
/**
2019-01-30 11:17:53 +00:00
* @addtogroup ModuleBuiltins
2018-06-05 20:51:49 +01:00
* @{
2018-07-11 19:26:02 +01:00
*
* YAP follows the following protovol:
* - predicate is in current module;
* - predicate is in user
* - predicate will be autoloaded, SWI style.
2018-06-05 20:51:49 +01:00
*/
2019-01-30 11:17:53 +00:00
2018-01-18 14:47:27 +00:00
:- '$mk_dynamic'('$parent_module'(_,_),prolog).
2019-01-30 11:17:53 +00:00
/** @pred mimp
debug import table
*/
2019-01-09 09:32:09 +00:00
mimp :-
2019-01-30 11:17:53 +00:00
recorded('$import',I,_),
%'$import'(ExportingMod,ImportingMod,G0,G,_,_),_),
2019-02-27 04:23:21 +00:00
writeln(I),
2019-01-09 09:32:09 +00:00
%(ImportingMod:G :- ExportingMod:G0)),
fail.
2018-03-19 15:41:06 +00:00
2019-01-09 09:32:09 +00:00
%:- start_low_level_trace.
% parent module mechanism
2019-03-04 15:49:53 +00:00
%% system has priority
'$get_predicate_definition'(_ImportingMod:G,prolog:G) :-
2019-03-12 10:51:39 +00:00
nonvar(G),
2019-03-04 15:49:53 +00:00
'$pred_exists'(G,prolog).
%% I am there, no need to import
'$get_predicate_definition'(Mod:Pred,Mod:Pred) :-
2019-03-12 10:51:39 +00:00
nonvar(Pred),
2019-03-04 15:49:53 +00:00
'$pred_exists'(Pred, Mod).
%% export table
'$get_predicate_definition'(ImportingMod:G,ExportingMod:G0) :-
recorded('$import','$import'(ExportingMod,ImportingMod,G0,G,_,_),_).
%% parent/user
'$get_predicate_definition'(ImportingMod:G,ExportingMod:G0) :-
( '$parent_module'(ImportingMod, PMod) ; PMod = user ),
ImportingMod \= PMod,
'$get_predicate_definition'(PMod:G, ExportingMod:G0).
2019-03-04 15:49:53 +00:00
%% autoload`
2019-03-12 10:51:39 +00:00
%'$get_predicate_definition'(ImportingMod:G,ExportingMod:G) :-
% current_prolog_flag(autoload, true),
% '$autoload'(G, ImportingMod, ExportingMod, swi).
2019-03-04 15:49:53 +00:00
'$predicate_definition'(Imp:Pred,Exp:NPred) :-
'$predicate_definition'(Imp:Pred,[],Exp:NPred),
%writeln((Imp:Pred -> Exp:NPred )).
!.
2018-01-18 14:47:27 +00:00
2019-03-04 15:49:53 +00:00
'$one_predicate_definition'(Imp:Pred,Exp:NPred) :-
'$predicate_definition'(Imp:Pred,[],Exp:NPred),
%writeln((Imp:Pred -> Exp:NPred )).
2019-01-09 09:32:09 +00:00
!.
2019-03-04 15:49:53 +00:00
'$one_predicate_definition'(Exp:Pred,Exp:Pred).
'$predicate_definition'(M0:Pred0,Path,ModF:PredF) :-
'$get_predicate_definition'(M0:Pred0, Mod:Pred),
\+ lists:member(Mod:Pred,Path),
(
'$predicate_definition'(Mod:Pred,[Mod:Pred|Path],ModF:PredF)
;
Mod = ModF, Pred = PredF
).
2019-01-09 09:32:09 +00:00
%
2019-03-04 15:49:53 +00:00
'$get_undefined_predicate'(ImportingMod:G, ExportingMod:G0) :-
2019-05-24 15:26:21 +01:00
must_be_callable( ImportingMod:G ),
2019-03-04 15:49:53 +00:00
'$predicate_definition'(ImportingMod:G,[], ExportingMod:G0),
ImportingMod:G \= ExportingMod:G0,
!.
2019-01-09 09:32:09 +00:00
% be careful here not to generate an undefined exception.
'$imported_predicate'(ImportingMod:G, ExportingMod:G0) :-
2019-03-04 15:49:53 +00:00
( var(ImportingMod) ->
current_module(ImportingMod)
;
true
),
(
var(G) ->
'$current_predicate'(_,G,ImportingMod,_)
;
true
),
(
'$undefined'(G, ImportingMod)
->
'$predicate_definition'(ImportingMod:G, ExportingMod:G0),
ExportingMod \= ImportingMod
;
ExportingMod = ImportingMod, G = G0
).
2019-01-09 09:32:09 +00:00
% check if current module redefines an imported predicate.
% and remove import.
%
'$not_imported'(H, Mod) :-
recorded('$import','$import'(NM,Mod,NH,H,_,_),R),
NM \= Mod,
functor(NH,N,Ar),
print_message(warning,redefine_imported(Mod,NM,N/Ar)),
erase(R),
fail.
'$not_imported'(_, _).
'$autoload'(G, _mportingMod, ExportingMod, Dialect) :-
functor(G, Name, Arity),
'$pred_exists'(index(Name,Arity,ExportingMod,_),Dialect),
call(Dialect:index(Name,Arity,ExportingMod,_)),
!.
'$autoload'(G, ImportingMod, ExportingMod, _Dialect) :-
functor(G, N, K),
functor(G0, N, K),
'$autoloader_find_predicate'(G0,ExportingMod),
ExportingMod \= ImportingMod,
2019-01-21 01:11:42 +00:00
(recordzifnot('$import','$import'(ExportingMod,ImportingMod,G0,G0, N ,K),_),
2019-02-27 04:23:21 +00:00
\+ '$is_system_predicate'(G0, ExportingMod)
2019-01-21 01:11:42 +00:00
->
'$compile'((G:-ExportingMod:G0), reconsult ,(ImportingMod:G:-ExportingMod:G0), ImportingMod, _)
;
true
).
2019-01-09 09:32:09 +00:00
'$autoloader_find_predicate'(G,ExportingMod) :-
'__NB_getval__'('$autoloader_set', true, false), !,
autoloader:find_predicate(G,ExportingMod).
'$autoloader_find_predicate'(G,ExportingMod) :-
yap_flag(autoload, true, false),
yap_flag( unknown, Unknown, fail),
yap_flag(debug, Debug, false), !,
2019-01-21 01:11:42 +00:00
load_files([library(autoloader)],[silent(true)]),
2019-01-09 09:32:09 +00:00
nb_setval('$autoloader_set', true),
yap_flag(autoload, _, true),
yap_flag( unknown, _, Unknown),
yap_flag( debug, _, Debug),
2019-02-27 04:23:21 +00:00
setup_autoloader:find_predicate(G,ExportingMod).
2019-01-09 09:32:09 +00:00
/**
2018-06-05 20:51:49 +01:00
*
* @}
*/