2001-04-09 20:54:03 +01:00
/*************************************************************************
* *
2015-01-05 12:21:58 +00:00
* YAP Prolog *
2001-04-09 20:54:03 +01:00
* *
2014-12-15 08:54:23 +00:00
* Yap Prolog was developed at NCCUP - Universidade do Porto *
2001-04-09 20:54:03 +01:00
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: consult.yap *
* Last rev: 8/2/88 *
* mods: *
* comments: Consulting Files in YAP *
* *
*************************************************************************/
2015-11-18 15:06:25 +00:00
/**
* @file consult.yap
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
* @date Wed Nov 18 14:01:10 2015
2015-12-15 09:28:43 +00:00
*
2015-11-18 15:06:25 +00:00
* @brief loading programs into YAP
2015-12-15 09:28:43 +00:00
*
*
2015-11-18 15:06:25 +00:00
*/
2014-04-09 12:39:29 +01:00
:- system_module( '$_consult', [compile/1,
consult/1,
2015-04-13 13:28:17 +01:00
db_files/1,
2014-04-09 12:39:29 +01:00
ensure_loaded/1,
exists_source/1,
exo_files/1,
(initialization)/2,
load_files/2,
make/0,
make_library_index/1,
module/2,
prolog_load_context/2,
reconsult/1,
source_file/1,
source_file/2,
source_file_property/2,
2015-04-13 13:35:37 +01:00
use_module/3],
2014-09-22 18:13:35 +01:00
['$add_multifile'/3,
2014-04-09 12:39:29 +01:00
'$csult'/2,
'$do_startup_reconsult'/1,
'$elif'/2,
'$else'/1,
'$endif'/1,
'$if'/2,
'$include'/2,
'$initialization'/1,
'$initialization'/2,
'$lf_opt'/3,
'$load_files'/3,
'$require'/2,
'$set_encoding'/1,
'$use_module'/3]).
:- use_system_module( '$_absf', ['$full_filename'/3]).
:- use_system_module( '$_boot', ['$clear_reconsulting'/0,
'$init_system'/0,
'$init_win_graphics'/0,
'$loop'/2,
'$system_catch'/4]).
:- use_system_module( '$_errors', ['$do_error'/2]).
:- use_system_module( '$_load_foreign', ['$import_foreign'/3]).
:- use_system_module( '$_modules', ['$add_to_imports'/3,
'$convert_for_export'/7,
'$extend_exports'/3]).
2014-11-25 12:03:48 +00:00
:- use_system_module( '$_preds', ['$current_predicate'/4]).
2001-04-09 20:54:03 +01:00
2015-04-13 13:35:37 +01:00
%% @{
2015-01-04 23:58:23 +00:00
2014-07-27 01:14:15 +01:00
/**
2015-01-04 23:58:23 +00:00
@defgroup YAPConsulting Loading files into YAP
@ingroup consult
2014-07-27 01:14:15 +01:00
We present the main predicates and directives available to load
files and to set-up the Prolog environment. We discuss
+ @ref YAPReadFiles
2014-12-24 15:32:29 +00:00
+ @ref YAPCompilerSettings
2014-07-27 01:14:15 +01:00
2014-12-24 15:32:29 +00:00
@{
2014-07-27 01:14:15 +01:00
@defgroup YAPReadFiles The Predicates that Read Source Files
2015-01-05 08:15:24 +00:00
@ingroup YAPConsulting
2014-07-27 01:14:15 +01:00
2015-04-13 13:35:37 +01:00
2014-07-27 01:14:15 +01:00
*/
/**
2016-01-20 22:36:16 +00:00
@pred load_files(+_Files_, +_Options_)
2014-07-27 01:14:15 +01:00
2015-08-07 22:57:53 +01:00
Implementation of the consult/1 family. Execution is controlled by the
2014-07-27 01:14:15 +01:00
following flags:
2014-09-22 18:13:35 +01:00
+ consult(+ _Mode_)
This extension controls the type of file to load. If _Mode_ is:
2015-04-13 13:35:37 +01:00
2014-09-22 18:13:35 +01:00
`consult`, clauses are added to the data-base, unless from the same file;
`reconsult`, clauses are recompiled,
`db`, these are facts that need to be added to the data-base,
`exo`, these are facts with atoms and integers that can be stored in a compact representation (see load_exo/1).
+ silent(+ _Bool_)
If true, load the file without printing a message. The specified
value is the default for all files loaded as a result of loading
the specified files.
+ stream(+ _Input_)
This SWI-Prolog extension compiles the data from the stream
_Input_. If this option is used, _Files_ must be a single atom
which is used to identify the source-location of the loaded
clauses as well as remove all clauses if the data is re-consulted.
This option is added to allow compiling from non-file locations
such as databases, the web, the user (see consult/1) or other
servers.
+ compilation_mode(+ _Mode_)
This extension controls how procedures are compiled. If _Mode_ is
`compact` clauses are compiled and no source code is stored; if it
is `source` clauses are compiled and source code is stored; if it
is `assert_all` clauses are asserted into the data-base.
+ encoding(+ _Encoding_)
Character encoding used in consulting files. Please (see
[Encoding](@ref Encoding)) for supported encodings.
2015-04-13 13:35:37 +01:00
+ expand(+ _Bool_)
2014-09-22 18:13:35 +01:00
If `true`, run the filenames through expand_file_name/2 and load
the returned files. Default is false, except for consult/1 which
is intended for interactive use.
+ if(+ _Condition_)
Load the file only if the specified _Condition_ is satisfied. The
value `true` the file unconditionally, `changed` loads the file if
it was not loaded before, or has been modified since it was loaded
the last time, `not_loaded` loads the file if it was not loaded
before.
+ imports(+ _ListOrAll_)
2015-04-13 13:35:37 +01:00
2014-09-22 18:13:35 +01:00
If `all` and the file is a module file, import all public
predicates. Otherwise import only the named predicates. Each
predicate is referred to as `\<name\>/\<arity\>`. This option has
no effect if the file is not a module file.
+ must_be_module(+ _Bool_)
2015-04-13 13:35:37 +01:00
2014-09-22 18:13:35 +01:00
If true, raise an error if the file is not a module file. Used by
` use_module/1 and use_module/2.
2014-10-02 14:57:50 +01:00
+ qcompile(+ _Value_)
2015-04-13 13:35:37 +01:00
2014-10-02 14:57:50 +01:00
SWI-Prolog flag that controls whether loaded files should be also
compiled into `qly` files. The default value is obtained from the flag
`qcompile`:
`never`, no `qly` file is generated unless the user calls
qsave_file/1 and friends, or sets the qcompile option in
2015-04-13 13:35:37 +01:00
load_files/2;
2014-10-02 14:57:50 +01:00
`auto`, all files are qcompiled.
`large`, files above 100KB are qcompiled.
`part`, not supported in YAP.
2014-09-22 18:13:35 +01:00
+ autoload(+ _Autoload_)
2015-04-13 13:35:37 +01:00
2015-04-21 23:08:58 +01:00
SWI-compatible option where if _Autoload_ is `true` undefined
2014-09-22 18:13:35 +01:00
predicates are loaded on first call.
+ derived_from(+ _File_)
SWI-compatible option to control make/0. Currently not supported.
2014-07-27 01:14:15 +01:00
*/
2005-10-18 18:04:43 +01:00
%
% SWI options
% autoload(true,false)
% derived_from(File) -> make
2007-11-06 17:02:13 +00:00
% encoding(Encoding) => implemented
2015-11-05 17:21:19 +00:00
% expand(true,false)
2007-11-06 17:02:13 +00:00
% if(changed,true,not_loaded) => implemented
% imports(all,List) => implemented
2014-10-02 14:57:50 +01:00
% qcompile() => implemented
2005-10-18 18:04:43 +01:00
% silent(true,false) => implemented
% stream(Stream) => implemented
2013-01-15 11:18:09 +00:00
% consult(consult,reconsult,exo,db) => implemented
2008-07-11 18:02:10 +01:00
% compilation_mode(compact,source,assert_all) => implemented
2013-11-04 01:14:48 +00:00
% register(true, false) => implemented
2005-10-18 18:04:43 +01:00
%
load_files(Files,Opts) :-
'$load_files'(Files,Opts,load_files(Files,Opts)).
2013-11-10 11:18:00 +00:00
'$lf_option'(autoload, 1, _).
2013-11-08 00:02:38 +00:00
'$lf_option'(derived_from, 2, false).
'$lf_option'(encoding, 3, default).
'$lf_option'(expand, 4, false).
'$lf_option'(if, 5, true).
'$lf_option'(imports, 6, all).
2014-10-02 14:57:50 +01:00
'$lf_option'(qcompile, 7, Current) :-
'$nb_getval'('$qcompile', Current, Current = never).
2013-11-08 00:02:38 +00:00
'$lf_option'(silent, 8, _).
2014-12-15 08:54:23 +00:00
'$lf_option'(skip_unix_header, 9, true).
2014-10-05 10:18:54 +01:00
'$lf_option'(compilation_mode, 10, Flag) :-
2015-06-19 01:11:30 +01:00
current_prolog_flag(source, YFlag),
( YFlag == false -> Flag = compact ; Flag = source ).
2013-11-08 00:02:38 +00:00
'$lf_option'(consult, 11, reconsult).
'$lf_option'(stream, 12, _).
'$lf_option'(register, 13, true).
'$lf_option'('$files', 14, _).
'$lf_option'('$call', 15, _).
'$lf_option'('$use_module', 16, _).
'$lf_option'('$consulted_at', 17, _).
'$lf_option'('$options', 18, _).
'$lf_option'('$location', 19, _).
'$lf_option'(dialect, 20, yap).
'$lf_option'(format, 21, source).
2015-06-19 01:11:30 +01:00
'$lf_option'(redefine_module, 22, Warn) :-
2015-08-18 21:08:52 +01:00
( var(Warn) -> current_prolog_flag( redefine_warnings, Redefine ), Redefine = Warn ; true ).
2013-11-08 00:02:38 +00:00
'$lf_option'(reexport, 23, false).
'$lf_option'(sandboxed, 24, false).
'$lf_option'(scope_settings, 25, false).
'$lf_option'(modified, 26, _).
'$lf_option'('$context_module', 27, _).
'$lf_option'('$parent_topts', 28, _).
'$lf_option'(must_be_module, 29, false).
2014-10-07 01:35:41 +01:00
'$lf_option'('$source_pos', 30, _).
2015-10-22 00:40:13 +01:00
'$lf_option'(initialization, 31, Ref) :-
nb:nb_queue(Ref).
2013-11-08 00:02:38 +00:00
2015-10-22 00:40:13 +01:00
'$lf_option'(last_opt, 31).
2013-11-04 01:14:48 +00:00
'$lf_opt'( Op, TOpts, Val) :-
2013-11-08 00:02:38 +00:00
'$lf_option'(Op, Id, _),
2013-11-04 01:14:48 +00:00
arg( Id, TOpts, Val ).
2014-10-07 01:35:41 +01:00
'$set_lf_opt'( Op, TOpts, Val) :-
'$lf_option'(Op, Id, _),
setarg( Id, TOpts, Val ).
2013-11-04 01:14:48 +00:00
'$load_files'(Files, Opts, Call) :-
2013-11-10 11:18:00 +00:00
( '$nb_getval'('$lf_status', OldTOpts, fail), nonvar(OldTOpts) ->
2015-04-15 11:21:15 +01:00
'$lf_opt'(autoload, OldTOpts, OldAutoload)
2013-11-10 11:18:00 +00:00
;
2015-04-15 11:21:15 +01:00
'$lf_option'(last_opt, LastOpt),
functor( OldTOpts, opt, LastOpt ),
'$lf_opt'('$context_module', OldTOpts, user)
),
2010-03-02 23:13:39 +00:00
'$check_files'(Files,load_files(Files,Opts)),
2013-11-08 00:02:38 +00:00
'$lf_option'(last_opt, LastOpt),
2013-11-04 01:14:48 +00:00
functor( TOpts, opt, LastOpt ),
( source_location(ParentF, Line) -> true ; ParentF = user_input, Line = -1 ),
'$lf_opt'('$location', TOpts, ParentF:Line),
'$lf_opt'('$files', TOpts, Files),
'$lf_opt'('$call', TOpts, Call),
'$lf_opt'('$options', TOpts, Opts),
2013-11-08 00:02:38 +00:00
'$lf_opt'('$parent_topts', TOpts, OldTOpts),
2013-11-04 01:14:48 +00:00
'$process_lf_opts'(Opts,TOpts,Files,Call),
2013-11-08 00:02:38 +00:00
'$lf_default_opts'(1, LastOpt, TOpts),
2005-10-28 18:38:50 +01:00
'$check_use_module'(Call,UseModule),
2013-11-04 01:14:48 +00:00
'$lf_opt'('$use_module', TOpts, UseModule),
2001-11-15 00:01:43 +00:00
'$current_module'(M0),
2013-11-10 11:18:00 +00:00
( '$lf_opt'(autoload, TOpts, Autoload),
var(Autoload) ->
Autoload = OldAutoload
;
true
),
2013-09-09 22:11:12 +01:00
% make sure we can run consult
'$init_system',
2013-11-04 01:14:48 +00:00
'$lf'(Files, M0, Call, TOpts).
2005-10-18 18:04:43 +01:00
2013-11-04 01:14:48 +00:00
'$check_files'(Files, Call) :-
2010-03-02 23:13:39 +00:00
var(Files), !,
2013-11-04 01:14:48 +00:00
'$do_error'(instantiation_error, Call).
'$check_files'(M:Files, Call) :- !,
2010-03-02 23:13:39 +00:00
(var(M)
->
2013-11-04 01:14:48 +00:00
'$do_error'(instantiation_error, Call)
2010-03-02 23:13:39 +00:00
;
atom(M)
->
'$check_files'(Files,Call)
;
2013-11-04 01:14:48 +00:00
'$do_error'(type_error(atom,M), Call)
2010-03-02 23:13:39 +00:00
).
2013-11-04 01:14:48 +00:00
'$check_files'(Files, Call) :-
( ground(Files)
2010-03-02 23:13:39 +00:00
->
true
;
2013-11-04 01:14:48 +00:00
'$do_error'(instantiation_error, Call)
2010-03-02 23:13:39 +00:00
).
2013-11-04 01:14:48 +00:00
'$process_lf_opts'(V, _, _, Call) :-
2005-10-18 18:04:43 +01:00
var(V), !,
'$do_error'(instantiation_error,Call).
2013-11-04 01:14:48 +00:00
'$process_lf_opts'([], _, _, _).
'$process_lf_opts'([Opt|Opts],TOpt,Files,Call) :-
Opt =.. [Op, Val],
ground(Val),
'$lf_opt'(Op, TOpt, Val),
2015-04-13 13:35:37 +01:00
'$process_lf_opt'(Op, Val,Call), !,
2013-11-04 01:14:48 +00:00
'$process_lf_opts'(Opts, TOpt, Files, Call).
'$process_lf_opts'([Opt|_],_,_,Call) :-
2005-10-18 18:04:43 +01:00
'$do_error'(domain_error(unimplemented_option,Opt),Call).
2013-11-04 01:14:48 +00:00
'$process_lf_opt'(autoload, Val, Call) :-
( Val == false -> true ;
Val == true -> true ;
'$do_error'(domain_error(unimplemented_option,autoload(Val)),Call) ).
'$process_lf_opt'(derived_from, File, Call) :-
( atom(File) -> true ; '$do_error'(type_error(atom,File),Call) ).
2013-11-08 00:02:38 +00:00
'$process_lf_opt'(encoding, Encoding, _Call) :-
2011-09-02 21:03:41 +01:00
atom(Encoding).
2013-11-04 01:14:48 +00:00
'$process_lf_opt'(expand, Val, Call) :-
( Val == true -> '$do_error'(domain_error(unimplemented_option,expand),Call) ;
Val == false -> true ;
'$do_error'(domain_error(unimplemented_option,expand(Val)),Call) ).
'$process_lf_opt'(if, If, Call) :-
( If == changed -> true ;
If == true -> true ;
If == not_loaded -> true ;
'$do_error'(domain_error(unimplemented_option,if),Call) ).
2013-11-08 00:02:38 +00:00
'$process_lf_opt'(imports, Val, Call) :-
2013-11-04 01:14:48 +00:00
( Val == all -> true ;
2013-11-08 00:02:38 +00:00
var(Val) -> Val = all ;
is_list(Val) -> ( ground(Val) -> true ; '$do_error'(instantiation_error,Call) ) ;
2013-11-04 01:14:48 +00:00
'$do_error'(domain_error(unimplemented_option,imports(Val)),Call) ).
'$process_lf_opt'(qcompile, Val,Call) :-
2014-10-02 14:57:50 +01:00
( Val == part -> '$do_error'(domain_error(unimplemented_option,expand),Call) ;
Val == never -> true ;
Val == auto -> true ;
Val == large -> true ;
'$do_error'(domain_error(unknown_option,qcompile(Val)),Call) ).
2013-11-04 01:14:48 +00:00
'$process_lf_opt'(silent, Val, Call) :-
2016-05-10 15:23:22 +01:00
( Val == false -> yap_flag(verbose_load, full) ;
Val == true -> yap_flag(verbose_load, silent) ;
'$do_error'(domain_error(out_of_domain_option,silent(Val)),Call) ).
2013-11-04 01:14:48 +00:00
'$process_lf_opt'(skip_unix_header, Val, Call) :-
( Val == false -> true ;
Val == true -> true ;
'$do_error'(domain_error(unimplemented_option,skip_unix_header(Val)),Call) ).
'$process_lf_opt'(compilation_mode, Val, Call) :-
2014-10-02 14:57:50 +01:00
( Val == source -> true ;
Val == compact -> true ;
Val == assert_all -> true ;
'$do_error'(domain_error(unimplemented_option,compilation_mode(Val)),Call) ).
2013-11-04 01:14:48 +00:00
'$process_lf_opt'(consult, Val , Call) :-
2014-10-02 14:57:50 +01:00
( Val == reconsult -> true ;
Val == consult -> true ;
Val == exo -> true ;
Val == db -> true ;
'$do_error'(domain_error(unimplemented_option,consult(Val)),Call) ).
2013-11-08 00:02:38 +00:00
'$process_lf_opt'(reexport, Val , Call) :-
( Val == true -> true ;
2014-10-02 14:57:50 +01:00
Val == false -> true ;
2013-11-08 00:02:38 +00:00
'$do_error'(domain_error(unimplemented_option,reexport(Val)),Call) ).
'$process_lf_opt'(must_be_module, Val , Call) :-
( Val == true -> true ;
Val == false -> true ;
'$do_error'(domain_error(unimplemented_option,must_be_module(Val)),Call) ).
2013-11-04 01:14:48 +00:00
'$process_lf_opt'(stream, Val, Call) :-
2014-04-06 17:05:17 +01:00
( current_stream(_,_,Val) -> true ;
2013-11-08 00:02:38 +00:00
'$do_error'(type_error(stream,Val),Call) ).
2013-11-04 01:14:48 +00:00
'$process_lf_opt'(register, Val, Call) :-
( Val == false -> true ;
Val == true -> true ;
'$do_error'(domain_error(unimplemented_option,register(Val)),Call) ).
2014-10-07 01:35:41 +01:00
'$process_lf_opt'('$context_module', Mod, Call) :-
( atom(Mod) -> true ; '$do_error'(type_error(atom,Mod),Call) ).
2014-05-06 09:34:25 +01:00
2013-11-04 01:14:48 +00:00
2013-11-08 00:02:38 +00:00
'$lf_default_opts'(I, LastOpt, _TOpts) :- I > LastOpt, !.
'$lf_default_opts'(I, LastOpt, TOpts) :-
I1 is I+1,
arg(I, TOpts, A),
( nonvar(A) -> true ;
'$lf_option'(_Name, I, A)
),
'$lf_default_opts'(I1, LastOpt, TOpts).
2015-04-13 13:35:37 +01:00
2013-11-04 01:14:48 +00:00
'$check_use_module'(use_module(_), use_module(_)) :- !.
'$check_use_module'(use_module(_,_), use_module(_)) :- !.
'$check_use_module'(use_module(M,_,_), use_module(M)) :- !.
'$check_use_module'(_, load_files) :- !.
'$lf'(V,_,Call, _ ) :- var(V), !,
2005-10-18 18:04:43 +01:00
'$do_error'(instantiation_error,Call).
2013-11-04 01:14:48 +00:00
'$lf'([], _, _, _) :- !.
'$lf'(M:X, _, Call, TOpts) :- !,
2005-10-18 18:04:43 +01:00
(
atom(M)
->
2013-11-04 01:14:48 +00:00
'$lf'(X, M, Call, TOpts)
2005-10-18 18:04:43 +01:00
;
'$do_error'(type_error(atom,M),Call)
).
2013-11-04 01:14:48 +00:00
'$lf'([F|Fs], Mod, Call, TOpts) :- !,
% clean up after each consult
( '$lf'(F,Mod,Call, TOpts), fail ;
2014-09-22 18:13:35 +01:00
'$lf'(Fs, Mod, Call, TOpts), fail;
true
).
2013-11-04 01:14:48 +00:00
'$lf'(user, Mod, _, TOpts) :- !,
2016-01-04 17:18:43 +00:00
b_setval('$user_source_file', user),
2015-04-21 23:08:58 +01:00
'$do_lf'(Mod, user_input, user_input, user_input, TOpts).
2013-11-04 01:14:48 +00:00
'$lf'(user_input, Mod, _, TOpts) :- !,
2016-01-04 14:11:09 +00:00
b_setval('$user_source_file', user_input),
2015-04-21 23:08:58 +01:00
'$do_lf'(Mod, user_input, user_input, user_input, TOpts).
2014-10-08 00:28:14 +01:00
'$lf'(File, Mod, Call, TOpts) :-
2015-11-09 11:27:46 +00:00
'$lf_opt'(stream, TOpts, Stream),
2016-01-04 14:11:09 +00:00
b_setval('$user_source_file', File),
2014-10-08 00:28:14 +01:00
( var(Stream) ->
/* need_to_open_file */
2015-11-09 11:27:46 +00:00
( '$full_filename'(File, Y, Call) -> true ; '$do_error'(existence_error(source_sink,File),Call) ),
2016-01-04 14:11:09 +00:00
( open(Y, read, Stream) -> true ; '$do_error'(permission_error(input,stream,Y),Call) )
2014-10-08 00:28:14 +01:00
;
stream_property(Stream, file_name(Y))
), !,
2015-06-19 01:11:30 +01:00
( file_size(Stream, Pos) -> true ; Pos = 0),
2014-10-08 00:28:14 +01:00
'$set_lf_opt'('$source_pos', TOpts, Pos),
'$lf_opt'(reexport, TOpts, Reexport),
'$lf_opt'(if, TOpts, If),
( var(If) -> If = true ; true ),
'$lf_opt'(imports, TOpts, Imports),
2015-04-21 23:08:58 +01:00
'$start_lf'(If, Mod, Stream, TOpts, File, Y, Reexport, Imports),
2015-04-13 13:35:37 +01:00
close(Stream).
2015-09-29 23:49:03 +01:00
2014-10-08 00:28:14 +01:00
2015-04-21 23:08:58 +01:00
'$start_lf'(not_loaded, Mod, _Stream, TOpts, UserFile, File, Reexport,Imports) :-
'$file_loaded'(File, Mod, Imports, TOpts), !,
'$lf_opt'('$options', TOpts, Opts),
'$lf_opt'('$location', TOpts, ParentF:Line),
'$loaded'(File, UserFile, Mod, ParentF, Line, not_loaded, _, _Dir, Opts),
'$reexport'( TOpts, ParentF, Reexport, Imports, File ).
'$start_lf'(changed, Mod, _Stream, TOpts, UserFile, File, Reexport, Imports) :-
2015-11-05 17:21:19 +00:00
'$file_unchanged'(File, Mod, Imports, TOpts), !,
'$lf_opt'('$options', TOpts, Opts),
'$lf_opt'('$location', TOpts, ParentF:Line),
'$loaded'(File, UserFile, Mod, ParentF, Line, changed, _, _Dir, Opts),
'$reexport'( TOpts, ParentF, Reexport, Imports, File ).
2015-04-21 23:08:58 +01:00
'$start_lf'(_, Mod, PlStream, TOpts, _UserFile, File, Reexport, ImportList) :-
2014-10-08 00:28:14 +01:00
% check if there is a qly file
2015-06-19 01:11:30 +01:00
% start_low_level_trace,
2015-04-21 23:08:58 +01:00
'$absolute_file_name'(File,[access(read),file_type(qly),file_errors(fail),solutions(first),expand(true)],F,qload_file(File)),
2016-01-08 20:43:14 +00:00
open( F, read, Stream , [type(binary)] ),
2015-11-05 17:21:19 +00:00
(
'$q_header'( Stream, Type ),
2016-01-08 20:43:14 +00:00
writeln(File:Type),
2015-11-05 17:21:19 +00:00
Type == file
->
!
;
close(Stream),
fail
),
H0 is heapused, '$cputime'(T0,_),
2015-04-13 13:35:37 +01:00
time_file64(F, T0F),
2014-10-08 00:28:14 +01:00
stream_property(PlStream, file_name(FilePl)),
2014-10-02 14:57:50 +01:00
time_file64(FilePl, T0Fl),
T0F >= T0Fl,
!,
file_directory_name(F, Dir),
working_directory(OldD, Dir),
2014-10-08 00:28:14 +01:00
'$qload_file'(Stream, Mod, F, FilePl, File, ImportList, TOpts),
2014-10-02 14:57:50 +01:00
close( Stream ),
H is heapused-H0, '$cputime'(TF,_), T is TF-T0,
'$current_module'(M, Mod),
working_directory( _, OldD),
2014-10-08 00:28:14 +01:00
'$lf_opt'('$location', TOpts, ParentF:_Line),
'$reexport'( TOpts, ParentF, Reexport, ImportList, File ),
2016-05-10 15:23:22 +01:00
'$early_print'(informational, loaded( loaded, F, M, T, H)),
2015-12-15 09:28:43 +00:00
working_directory( _, OldD),
'$exec_initialization_goals',
'$current_module'(_M, Mod).
2015-04-21 23:08:58 +01:00
'$start_lf'(_, Mod, Stream, TOpts, UserFile, File, _Reexport, _Imports) :-
'$do_lf'(Mod, Stream, UserFile, File, TOpts).
2005-10-28 18:38:50 +01:00
2014-07-27 01:14:15 +01:00
/**
2015-04-21 23:08:58 +01:00
@pred ensure_loaded(+ _F_) is iso
2014-07-27 01:14:15 +01:00
When the files specified by _F_ are module files,
ensure_loaded/1 loads them if they have note been previously
loaded, otherwise advertises the user about the existing name clashes
2015-04-21 23:08:58 +01:00
and prompts about importing or not those predicates. Predicates which
2014-07-27 01:14:15 +01:00
are not public remain invisible.
When the files are not module files, ensure_loaded/1 loads them
2015-06-19 01:11:30 +01:00
if they have not been loaded before, and does nothing otherwise.
2014-07-27 01:14:15 +01:00
_F_ must be a list containing the names of the files to load.
*/
2005-10-20 00:16:49 +01:00
ensure_loaded(Fs) :-
2013-11-08 00:02:38 +00:00
'$load_files'(Fs, [if(not_loaded)],ensure_loaded(Fs)).
2005-10-18 18:04:43 +01:00
compile(Fs) :-
'$load_files'(Fs, [], compile(Fs)).
2001-04-09 20:54:03 +01:00
2014-07-27 01:14:15 +01:00
/**
@pred [ _F_ ]
@pred consult(+ _F_)
Adds the clauses written in file _F_ or in the list of files _F_
to the program.
In YAP consult/1 does not remove previous clauses for
the procedures defined in other files than _F_, but since YAP-6.4.3 it will redefine all procedures defined in _F_.
All code in YAP is compiled, and the compileer generates static
procedures by default. In case you need to manipulate the original
code, the expanded version of the original source code is available by
calling source/0 or by enabling the source flag.
*/
2010-07-31 00:32:54 +01:00
% consult(Fs) :-
% '$has_yap_or',
% '$do_error'(context_error(consult(Fs),clause),query).
2004-02-12 18:23:29 +00:00
consult(V) :-
var(V), !,
'$do_error'(instantiation_error,consult(V)).
consult(M0:Fs) :- !,
'$consult'(Fs, M0).
2001-04-09 20:54:03 +01:00
consult(Fs) :-
2004-02-12 18:23:29 +00:00
'$current_module'(M0),
'$consult'(Fs, M0).
2001-04-09 20:54:03 +01:00
2005-10-18 18:04:43 +01:00
'$consult'(Fs,Module) :-
2015-06-19 01:11:30 +01:00
current_prolog_flag(language_mode, iso), % SICStus Prolog compatibility
2005-10-18 18:04:43 +01:00
!,
2010-03-03 19:08:59 +00:00
'$load_files'(Module:Fs,[],consult(Fs)).
2007-01-24 14:20:04 +00:00
'$consult'(Fs, Module) :-
2010-03-03 19:08:59 +00:00
'$load_files'(Module:Fs,[consult(consult)],consult(Fs)).
2005-10-18 18:04:43 +01:00
2014-07-27 01:14:15 +01:00
/**
@pred [ - _F_ ]
@pred reconsult(+ _F_ )
2015-04-21 23:08:58 +01:00
@pred compile(+ _F_ )
2014-07-27 01:14:15 +01:00
Updates the program by replacing the
previous definitions for the predicates defined in _F_. It differs from consult/1
in that it only multifile/1 predicates are not reset in a reconsult. Instead, consult/1
sees all predicates as multifile.
YAP also offers no difference between consult/1 and compile/1. The two
are implemented by the same exact code.
Example:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
?- [file1, -file2, -file3, file4].
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-04-21 23:08:58 +01:00
will consult `file1` `file4` and reconsult `file2` and
2014-07-27 01:14:15 +01:00
`file3`. That is, it could be written as:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
?- consult(file1),
reconsult( [file2, file3],
consult( [file4] ).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
2001-04-09 20:54:03 +01:00
reconsult(Fs) :-
2005-10-18 18:04:43 +01:00
'$load_files'(Fs, [], reconsult(Fs)).
2001-04-09 20:54:03 +01:00
2014-07-27 01:14:15 +01:00
/* exo_files(+ _Files_)
Load compactly a database of facts with equal structure, see @cite
x. Useful when wanting to read in a very compact way database tables,
it saves space by storing data, not a compiled program. The idea was
introduced in @cite y, but never implemented because often indexing
just takes more room. It was redefined recebtly by exploiting
different forms of indexing, as shown in @cite x.
@note implementation
The function Yap_ExoLookup() is the mai interface betwwen the WAM
and exo components. The algorithms are straightforward, that is,
mostly hash-tables but have close to linear performance..
*/
2013-01-15 11:18:09 +00:00
exo_files(Fs) :-
'$load_files'(Fs, [consult(exo), if(not_loaded)], exo_files(Fs)).
2014-07-27 01:14:15 +01:00
/**
2015-04-21 23:08:58 +01:00
@pred load_db(+ _Files_)
2014-07-27 01:14:15 +01:00
Load a database of ground facts. All facts must take up the same amount of storage, so that
2015-04-13 13:35:37 +01:00
a fact $I$ can be accessed at position _P[I-1]_. This representation thus stores the facts as a huge continuous array, the so-called mega clause.
2014-07-27 01:14:15 +01:00
See \cite for a motivation for this technique. YAP implements this
optimization by default whenever it loads a large number of facts (see
Yap_BuildMegaClause(PredEntry *ap) ) for the details. On the other
hand, loading the data-base will cause fragmentation because
individual facts facts need extra headers ands tails, and because
often new atoms will be stored in the Symbol Table, see
LookupAtom(const char *atom). The main advantage of load_db/1 is
that it allocates the necessary memory only once. Just doing this
may halve total memory usage in large in-memory database-oriented applications.
2015-04-13 13:35:37 +01:00
@note Implementation
2014-07-27 01:14:15 +01:00
YAP implements load_db/1 as a two-step non-optimised process. First,
it counts the nmuber of facts and checks their size. Second, it
allocates and fills the memory. The first step of the algorithm is
implemented by dbload_get_space(), and the second by
dbload_add_facts().
2015-04-13 13:35:37 +01:00
db_files/1 itself is just a call to load_files/2.
2014-07-27 01:14:15 +01:00
*/
2013-01-15 11:18:09 +00:00
db_files(Fs) :-
'$load_files'(Fs, [consult(db), if(not_loaded)], exo_files(Fs)).
2001-04-09 20:54:03 +01:00
2013-11-04 01:14:48 +00:00
'$csult'(Fs, M) :-
'$extract_minus'(Fs, MFs), !,
'$load_files'(M:MFs,[],[M:Fs]).
'$csult'(Fs, M) :-
'$load_files'(M:Fs,[consult(consult)],[M:Fs]).
'$extract_minus'([], []).
'$extract_minus'([-F|Fs], [F|MFs]) :-
'$extract_minus'(Fs, MFs).
2005-10-19 02:47:43 +01:00
2015-04-21 23:08:58 +01:00
'$do_lf'(ContextModule, Stream, UserFile, File, TOpts) :-
2015-12-15 09:28:43 +00:00
prompt1(': '), prompt(_,' '),
2015-06-19 01:11:30 +01:00
stream_property(OldStream, alias(loop_stream) ),
'$lf_opt'(encoding, TOpts, Encoding),
2016-02-18 12:10:58 +00:00
set_stream( Stream, [alias(loop_stream), encoding(Encoding)] ),
'$lf_opt'('$context_module', TOpts, ContextModule),
2014-08-23 20:47:40 +01:00
'$lf_opt'(reexport, TOpts, Reexport),
2014-10-02 14:57:50 +01:00
'$lf_opt'(qcompile, TOpts, QCompiling),
'$nb_getval'('$qcompile', ContextQCompiling, ContextQCompiling = never),
nb_setval('$qcompile', QCompiling),
2013-11-10 11:18:00 +00:00
% format( 'I=~w~n', [Verbosity=UserFile] ),
2013-11-04 01:14:48 +00:00
% export to process
b_setval('$lf_status', TOpts),
2012-02-27 16:37:31 +00:00
'$reset_if'(OldIfLevel),
2013-11-04 01:14:48 +00:00
% take care with [a:f], a is the ContextModule
'$current_module'(SourceModule, ContextModule),
2014-04-24 11:40:35 +01:00
'$lf_opt'(consult, TOpts, Reconsult0),
2013-11-04 01:14:48 +00:00
'$lf_opt'('$options', TOpts, Opts),
'$lf_opt'('$location', TOpts, ParentF:Line),
2016-04-18 13:25:23 +01:00
'$loaded'(File, UserFile, SourceModule, ParentF, Line, Reconsult0, Reconsult, Dir, Opts),
2015-04-21 23:08:58 +01:00
working_directory(OldD, Dir),
2002-01-22 17:11:36 +00:00
H0 is heapused, '$cputime'(T0,_),
2015-06-19 01:11:30 +01:00
current_prolog_flag(generate_debug_info, GenerateDebug),
2013-11-04 01:14:48 +00:00
'$lf_opt'(compilation_mode, TOpts, CompMode),
2015-04-13 13:35:37 +01:00
'$comp_mode'(OldCompMode, CompMode),
2014-04-24 11:40:35 +01:00
( Reconsult \== consult ->
2006-02-17 15:56:36 +00:00
'$start_reconsulting'(File),
2005-10-18 18:04:43 +01:00
'$start_consult'(Reconsult,File,LC),
'$remove_multifile_clauses'(File),
StartMsg = reconsulting,
EndMsg = reconsulted
;
'$start_consult'(Reconsult,File,LC),
2014-04-24 11:40:35 +01:00
( File \= user_input, File \= [] -> '$remove_multifile_clauses'(File) ; true ),
2005-10-18 18:04:43 +01:00
StartMsg = consulting,
EndMsg = consulted
),
2016-05-10 15:23:22 +01:00
'$early_print'(informational, loading(StartMsg, UserFile)),
2013-11-04 01:14:48 +00:00
'$lf_opt'(skip_unix_header , TOpts, SkipUnixHeader),
2014-10-02 14:57:50 +01:00
( SkipUnixHeader == true
->
2013-11-04 01:14:48 +00:00
'$skip_unix_header'(Stream)
2014-10-02 14:57:50 +01:00
;
2006-03-03 23:11:30 +00:00
true
2014-10-07 01:35:41 +01:00
),
'$loop'(Stream,Reconsult),
'$lf_opt'(imports, TOpts, Imports),
2016-02-28 19:32:55 +00:00
'$import_to_current_module'(File, ContextModule, Imports, _, TOpts),
2013-11-04 01:14:48 +00:00
'$current_module'(Mod, SourceModule),
2014-10-07 01:35:41 +01:00
H is heapused-H0, '$cputime'(TF,_), T is TF-T0,
2016-05-10 15:23:22 +01:00
'$early_print'(informational, loaded(EndMsg, File, Mod, T, H)),
2014-10-07 01:35:41 +01:00
'$end_consult',
'$q_do_save_file'(File, UserFile, TOpts ),
2015-04-13 13:35:37 +01:00
(
2005-10-19 02:47:43 +01:00
Reconsult = reconsult ->
2014-10-07 01:35:41 +01:00
'$clear_reconsulting'
;
2005-10-19 02:47:43 +01:00
true
),
2015-06-19 01:11:30 +01:00
set_stream( OldStream, alias(loop_stream) ),
set_prolog_flag(generate_debug_info, GenerateDebug),
2013-11-08 08:24:06 +00:00
'$comp_mode'(_CompMode, OldCompMode),
2011-05-12 22:26:10 +01:00
working_directory(_,OldD),
2007-10-21 09:48:06 +01:00
% surely, we were in run mode or we would not have included the file!
nb_setval('$if_skip_mode',run),
% back to include mode!
2012-02-27 16:37:31 +00:00
nb_setval('$if_level',OldIfLevel),
2013-11-04 01:14:48 +00:00
'$lf_opt'('$use_module', TOpts, UseModule),
2005-10-28 18:38:50 +01:00
'$bind_module'(Mod, UseModule),
2014-08-23 20:47:40 +01:00
'$reexport'( TOpts, ParentF, Reexport, Imports, File ),
2014-10-02 14:57:50 +01:00
nb_setval('$qcompile', ContextQCompiling),
2002-11-19 14:43:17 +00:00
( LC == 0 -> prompt(_,' |: ') ; true),
2015-11-05 17:21:19 +00:00
'$exec_initialization_goals',
2014-02-09 18:06:37 +00:00
% format( 'O=~w~n', [Mod=UserFile] ),
2001-04-09 20:54:03 +01:00
!.
2014-10-07 01:35:41 +01:00
'$q_do_save_file'(File, UserF, TOpts ) :-
2015-04-13 13:35:37 +01:00
'$lf_opt'(qcompile, TOpts, QComp),
2014-10-07 01:35:41 +01:00
'$lf_opt'('$source_pos', TOpts, Pos),
2014-10-08 00:28:14 +01:00
( QComp == auto ; QComp == large, Pos > 100*1024),
2014-10-02 14:57:50 +01:00
'$absolute_file_name'(UserF,[file_type(qly),solutions(first),expand(true)],F,load_files(File)),
!,
'$qsave_file_'( File, UserF, F ).
2014-10-07 01:35:41 +01:00
'$q_do_save_file'(_File, _, _TOpts ).
2014-10-02 14:57:50 +01:00
2012-02-27 16:37:31 +00:00
'$reset_if'(OldIfLevel) :-
2012-12-07 08:08:32 +00:00
'$nb_getval'('$if_level', OldIfLevel, fail), !,
2012-02-27 16:37:31 +00:00
nb_setval('$if_level',0).
'$reset_if'(0) :-
2015-10-22 00:40:13 +01:00
nb_setval('$if_le1vel',0).
2012-02-27 16:37:31 +00:00
2012-03-04 10:22:40 +00:00
'$get_if'(Level0) :-
2012-12-07 08:08:32 +00:00
'$nb_getval'('$if_level', Level, fail), !,
2012-03-04 10:22:40 +00:00
Level0 = Level.
'$get_if'(0).
2005-10-28 18:38:50 +01:00
'$bind_module'(_, load_files).
'$bind_module'(Mod, use_module(Mod)).
2014-10-07 01:35:41 +01:00
'$import_to_current_module'(File, ContextModule, _Imports, _RemainingImports, _TOpts) :-
2014-10-02 14:57:50 +01:00
\+ recorded('$module','$module'(File, _Module, _, _ModExports, _),_),
2014-02-09 18:06:37 +00:00
% enable loading C-predicates from a different file
recorded( '$load_foreign_done', [File, M0], _),
'$import_foreign'(File, M0, ContextModule ),
fail.
2013-11-17 21:41:41 +00:00
'$import_to_current_module'(File, ContextModule, Imports, RemainingImports, TOpts) :-
2014-09-22 18:13:35 +01:00
recorded('$module','$module'(File, Module, _Source, ModExports, _),_),
2013-11-08 00:02:38 +00:00
Module \= ContextModule, !,
2014-10-07 01:35:41 +01:00
'$lf_opt'('$call', TOpts, Goal),
2013-11-17 21:41:41 +00:00
'$convert_for_export'(Imports, ModExports, Module, ContextModule, TranslationTab, RemainingImports, Goal),
2013-11-08 00:02:38 +00:00
'$add_to_imports'(TranslationTab, Module, ContextModule).
2013-11-17 21:41:41 +00:00
'$import_to_current_module'(_, _, _, _, _).
2005-10-18 18:04:43 +01:00
2001-04-09 20:54:03 +01:00
'$start_reconsulting'(F) :-
2003-08-27 14:37:10 +01:00
recorda('$reconsulted','$',_),
recorda('$reconsulting',F,_).
2001-04-09 20:54:03 +01:00
2015-11-05 17:21:19 +00:00
'$exec_initialization_goals' :-
2006-03-03 23:11:30 +00:00
recorded('$blocking_code',_,R),
erase(R),
fail.
2015-04-13 13:35:37 +01:00
% system goals must be performed first
2015-11-05 17:21:19 +00:00
'$exec_initialization_goals' :-
recorded('$system_initialization',G,R),
2006-03-03 23:11:30 +00:00
erase(R),
G \= '$',
2015-12-15 09:28:43 +00:00
strip_module(user:G, M0, G0),
( catch(M0:G0, Error, user:'$LoopError'(Error, top))
2015-11-05 17:21:19 +00:00
->
true
;
format(user_error,':- ~w failed.~n',[G])
),
2015-10-22 00:40:13 +01:00
fail.
2015-11-05 17:21:19 +00:00
'$exec_initialization_goals' :-
2006-03-03 23:11:30 +00:00
'$current_module'(M),
2015-12-15 09:28:43 +00:00
'$nb_getval'('$lf_status', TOpts, fail),
2015-11-05 17:21:19 +00:00
'$lf_opt'( initialization, TOpts, Ref),
nb:nb_queue_close(Ref, Answers, []),
lists:member(G, Answers),
2015-12-15 09:28:43 +00:00
strip_module( M:G, M0, G0),
2015-11-05 17:21:19 +00:00
(
2015-12-15 09:28:43 +00:00
catch(M0:G0, Error, user:'$LoopError'(Error, top))
2015-11-05 17:21:19 +00:00
->
true
2016-02-13 03:07:48 +00:00
;
format(user_error,':- ~w:~w failed.~n',[M,G])
2015-11-05 17:21:19 +00:00
),
fail.
'$exec_initialization_goals'.
2012-12-07 08:08:32 +00:00
2014-07-27 01:14:15 +01:00
/**
2014-12-24 15:32:29 +00:00
@pred include(+ _F_) is directive
The `include` directive adds the text files or sequence of text
files specified by _F_ into the files being currently consulted.
It may be used
as an replacement to consult/1 by allowing the programmer to include a
data-base
split into several files.
2014-07-27 01:14:15 +01:00
*/
2014-12-24 15:32:29 +00:00
2001-04-09 20:54:03 +01:00
'$include'(V, _) :- var(V), !,
2002-09-09 18:40:12 +01:00
'$do_error'(instantiation_error,include(V)).
2001-04-09 20:54:03 +01:00
'$include'([], _) :- !.
'$include'([F|Fs], Status) :- !,
'$include'(F, Status),
'$include'(Fs, Status).
2002-02-08 22:19:24 +00:00
'$include'(X, Status) :-
2013-11-04 01:14:48 +00:00
b_getval('$lf_status', TOpts),
'$full_filename'(X, Y , ( :- include(X)) ),
2015-09-21 23:05:36 +01:00
'$including'(Old, Y),
2013-11-04 01:14:48 +00:00
'$lf_opt'(stream, TOpts, OldStream),
2004-09-30 20:51:54 +01:00
'$current_module'(Mod),
2013-11-04 01:14:48 +00:00
( open(Y, read, Stream) ->
2015-04-13 13:35:37 +01:00
true ;
2011-02-15 07:39:27 +00:00
'$do_error'(permission_error(input,stream,Y),include(X))
2001-04-09 20:54:03 +01:00
),
2015-07-06 12:04:42 +01:00
file_directory_name(Y, Dir),
2015-09-21 23:05:36 +01:00
H0 is heapused, '$cputime'(T0,_),
2015-07-06 12:04:42 +01:00
working_directory(Dir0, Dir),
2015-06-19 01:11:30 +01:00
'$lf_opt'(encoding, TOpts, Encoding),
2016-02-18 12:10:58 +00:00
set_stream(Stream, [encoding(Encoding),alias(loop_stream)] ),
'$loaded'(Y, X, Mod, _OldY, _L, include, _, Dir, []),
( '$nb_getval'('$included_file', OY, fail ) -> true ; OY = [] ),
2013-11-04 01:14:48 +00:00
nb_setval('$included_file', Y),
2016-05-10 15:23:22 +01:00
'$early_print'(informational, loading(including, Y)),
2013-11-04 01:14:48 +00:00
'$loop'(Stream,Status),
2015-06-19 01:11:30 +01:00
set_stream(OldStream, alias(loop_stream) ),
2015-07-06 12:04:42 +01:00
close(Stream),
2004-09-30 20:51:54 +01:00
H is heapused-H0, '$cputime'(TF,_), T is TF-T0,
2016-05-10 15:23:22 +01:00
'$early_print'(informational, loaded(included, Y, Mod, T, H)),
2015-07-06 12:04:42 +01:00
working_directory(_Dir, Dir0),
2015-09-21 23:05:36 +01:00
'$including'(Y, Old),
2015-07-06 12:04:42 +01:00
nb_setval('$included_file',OY).
2001-04-09 20:54:03 +01:00
2014-07-17 18:19:38 +01:00
2014-03-16 18:59:54 +00:00
%
% reconsult at startup...
%
'$do_startup_reconsult'(_X) :-
2015-04-22 21:21:20 +01:00
'$init_win_graphics',
2015-11-09 11:27:46 +00:00
fail.
2001-04-09 20:54:03 +01:00
'$do_startup_reconsult'(X) :-
2016-01-03 02:06:09 +00:00
catch(load_files(user:X, [silent(true)]), Error, '$LoopError'(Error, consult)),
2006-12-13 16:10:26 +00:00
!,
2015-08-07 22:57:53 +01:00
( current_prolog_flag(halt_after_consult, false) -> true ; halt).
2011-09-07 21:28:13 +01:00
'$do_startup_reconsult'(_).
2001-04-09 20:54:03 +01:00
2013-11-04 01:14:48 +00:00
'$skip_unix_header'(Stream) :-
2011-05-12 22:34:44 +01:00
peek_code(Stream, 0'#), !, % 35 is ASCII for '#
2011-02-15 14:43:28 +00:00
skip(Stream, 10),
2013-11-04 01:14:48 +00:00
'$skip_unix_header'(Stream).
'$skip_unix_header'(_).
2001-04-09 20:54:03 +01:00
2010-02-28 22:24:51 +00:00
source_file(FileName) :-
2015-04-21 23:08:58 +01:00
recorded('$source_file','$source_file'(FileName, _, _),_).
2010-02-28 22:24:51 +00:00
source_file(Mod:Pred, FileName) :-
current_module(Mod),
Mod \= prolog,
2015-07-23 01:31:03 +01:00
'$current_predicate'(_,Mod,Pred,all),
2010-02-28 22:24:51 +00:00
'$owned_by'(Pred, Mod, FileName).
'$owned_by'(T, Mod, FileName) :-
'$is_multifile'(T, Mod),
functor(T, Name, Arity),
setof(FileName, Ref^recorded('$multifile_defs','$defined'(FileName,Name,Arity,Mod), Ref), L),
lists:member(FileName, L).
'$owned_by'(T, Mod, FileName) :-
'$owner_file'(T, Mod, FileName).
2015-04-13 13:35:37 +01:00
/** @pred prolog_load_context(? _Key_, ? _Value_)
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
Obtain information on what is going on in the compilation process. The
following keys are available:
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
+ directory (prolog_load_context/2 option)
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
Full name for the directory where YAP is currently consulting the
file.
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
+ file (prolog_load_context/2 option)
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
Full name for the file currently being consulted. Notice that included
filed are ignored.
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
+ module (prolog_load_context/2 option)
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
Current source module.
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
+ `source` (prolog_load_context/2 option)
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
Full name for the file currently being read in, which may be consulted,
reconsulted, or included.
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
+ `stream` (prolog_load_context/2 option)
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
Stream currently being read in.
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
+ `term_position` (prolog_load_context/2 option)
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
Stream position at the stream currently being read in. For SWI
compatibility, it is a term of the form
'$stream_position'(0,Line,0,0).
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
+ `source_location(? _File Name_, ? _Line_)` (prolog_load_context/2 option)
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
SWI-compatible predicate. If the last term has been read from a physical file (i.e., not from the file user or a string), unify File with an absolute path to the file and Line with the line-number in the file. Please use prolog_load_context/2.
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
+ `source_file(? _File_)` (prolog_load_context/2 option)
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
SWI-compatible predicate. True if _File_ is a loaded Prolog source file.
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
+ `source_file(? _ModuleAndPred_ ,? _File_)` (prolog_load_context/2 option)
2014-09-11 20:06:57 +01:00
2014-12-24 15:32:29 +00:00
SWI-compatible predicate. True if the predicate specified by _ModuleAndPred_ was loaded from file _File_, where _File_ is an absolute path name (see `absolute_file_name/2`).
2014-09-11 20:06:57 +01:00
2014-10-02 14:57:50 +01:00
*/
2014-09-11 20:06:57 +01:00
2015-01-04 23:58:23 +00:00
%% @}
%% @{
2014-12-24 15:32:29 +00:00
/** @addtogroup YAPLibraries Library Predicates
2014-09-11 20:06:57 +01:00
Library files reside in the library_directory path (set by the
`LIBDIR` variable in the Makefile for YAP). Currently,
2015-04-13 13:35:37 +01:00
most files in the library are from the Edinburgh Prolog library.
2014-09-11 20:06:57 +01:00
2015-01-04 23:58:23 +00:00
*/
2015-04-13 13:35:37 +01:00
prolog_load_context(directory, DirName) :-
2014-12-02 02:34:58 +00:00
( source_location(F, _)
-> file_directory_name(F, DirName) ;
working_directory( DirName, DirName )
).
2015-04-13 13:35:37 +01:00
prolog_load_context(file, FileName) :-
2014-12-02 02:34:58 +00:00
( source_location(FileName, _)
->
true
;
FileName = user_input
).
2001-04-09 20:54:03 +01:00
prolog_load_context(module, X) :-
2014-12-02 02:34:58 +00:00
'$nb_getval'('$consulting_file', _, fail),
'$current_module'(X).
2013-11-04 01:14:48 +00:00
prolog_load_context(source, F0) :-
2014-12-02 02:34:58 +00:00
( source_location(F0, _) /*,
'$input_context'(Context),
'$top_file'(Context, F0, F) */
->
true
;
F0 = user_input
).
2015-04-13 13:35:37 +01:00
prolog_load_context(stream, Stream) :-
2015-08-18 21:08:52 +01:00
stream_property(Stream, alias(loop_stream) ).
2001-04-09 20:54:03 +01:00
2005-10-18 18:04:43 +01:00
% if the file exports a module, then we can
% be imported from any module.
2015-04-21 23:08:58 +01:00
'$file_loaded'(F0, M, Imports, TOpts) :-
%format( 'L=~w~n', [(F0)] ),
2015-04-13 13:35:37 +01:00
(
2015-04-21 23:08:58 +01:00
atom_concat(Prefix, '.qly', F0 ),
'$absolute_file_name'(Prefix,[access(read),file_type(prolog),file_errors(fail),solutions(first),expand(true)],F,load_files(Prefix))
2014-10-02 14:57:50 +01:00
;
F0 = F
2015-04-21 23:08:58 +01:00
),
'$ensure_file_loaded'(F, M),
!,
'$import_to_current_module'(F, M, Imports, _, TOpts).
2005-10-18 18:04:43 +01:00
2015-04-21 23:08:58 +01:00
'$ensure_file_loaded'(F, M) :-
% loaded from the same module, but does not define a module.
2015-04-22 21:21:20 +01:00
recorded('$source_file','$source_file'(F, _Age, NM), _R),
2015-04-21 23:08:58 +01:00
% make sure: it either defines a new module or it was loaded in the same context
( M == NM -> true ; recorded('$module','$module'(F,NM,_Source,_P,_),_) ), !.
2003-11-29 23:41:28 +00:00
2015-04-21 23:08:58 +01:00
% if the file exports a module, then we can
2003-11-29 23:41:28 +00:00
% be imported from any module.
2015-04-21 23:08:58 +01:00
'$file_unchanged'(F, M, Imports, TOpts) :-
'$ensure_file_unchanged'(F, M),
2013-11-17 21:41:41 +00:00
% format( 'IU=~w~n', [(F1:Imports->M)] ),
2015-04-21 23:08:58 +01:00
'$import_to_current_module'(F, M, Imports, _, TOpts).
2005-10-18 18:04:43 +01:00
2014-10-07 01:35:41 +01:00
% module can be reexported.
2015-04-21 23:08:58 +01:00
'$ensure_file_unchanged'(F, M) :-
2015-08-07 22:57:53 +01:00
% loaded from the same module, but does not define a module.
2015-04-22 21:21:20 +01:00
recorded('$source_file','$source_file'(F, Age, NM), R),
2015-08-07 22:57:53 +01:00
% make sure: it either defines a new module or it was loaded in the same context
2015-04-21 23:08:58 +01:00
'$file_is_unchanged'(F, R, Age),
2015-08-07 22:57:53 +01:00
!,
% ( F = '/usr/local/share/Yap/rbtrees.yap' ->start_low_level_trace ; true),
2015-08-18 21:08:52 +01:00
recorded('$module','$module'(F,NM,_ASource,_P,_),_),
2015-04-21 23:08:58 +01:00
( M == NM -> true ; recorded('$module','$module'(F,NM,_Source,_P,_),_) ), !.
2003-11-29 23:41:28 +00:00
2005-10-18 18:04:43 +01:00
'$file_is_unchanged'(F, R, Age) :-
2013-06-07 18:00:27 +01:00
time_file64(F,CurrentAge),
( (Age == CurrentAge ; Age = -1) -> true; erase(R), fail).
2001-04-09 20:54:03 +01:00
2015-04-21 23:08:58 +01:00
% inform the file has been loaded and is now available.
'$loaded'(F, UserFile, M, OldF, Line, Reconsult0, Reconsult, Dir, Opts) :-
2013-11-04 01:14:48 +00:00
( F == user_input -> working_directory(Dir,Dir) ; file_directory_name(F, Dir) ),
nb_setval('$consulting_file', F ),
2015-04-13 13:35:37 +01:00
(
2015-08-07 22:57:53 +01:00
% if we are reconsulting, always start from scratch
Reconsult0 \== consult,
Reconsult0 \== not_loaded,
Reconsult0 \== changed,
2015-04-21 23:08:58 +01:00
recorded('$source_file','$source_file'(F, _,_),R),
erase(R),
2015-08-07 22:57:53 +01:00
fail
;
var(Reconsult0)
->
Reconsult = consult
;
Reconsult = Reconsult0
2014-10-07 01:35:41 +01:00
),
2015-04-13 13:35:37 +01:00
(
Reconsult \== consult,
2014-10-07 01:35:41 +01:00
recorded('$lf_loaded','$lf_loaded'(F, _, _, _, _, _, _),R),
2015-04-13 13:35:37 +01:00
erase(R),
fail
;
2014-10-07 01:35:41 +01:00
var(Reconsult)
->
2015-04-13 13:35:37 +01:00
Reconsult = consult
2014-10-07 01:35:41 +01:00
;
Reconsult = Reconsult0
),
2013-11-04 01:14:48 +00:00
( F == user_input -> Age = 0 ; time_file64(F, Age) ),
2015-04-22 21:21:20 +01:00
% modules are logically loaded only once
2015-07-06 12:04:42 +01:00
2015-04-21 23:08:58 +01:00
( recorded('$module','$module'(F,_DonorM,_SourceF, _AllExports, _Line),_) -> true ;
recordaifnot('$source_file','$source_file'( F, Age, M), _) -> true ;
true ),
2013-11-04 01:14:48 +00:00
recorda('$lf_loaded','$lf_loaded'( F, M, Reconsult, UserFile, OldF, Line, Opts), _).
2005-10-19 02:47:43 +01:00
2014-07-27 01:14:15 +01:00
/** @pred make is det
SWI-Prolog originally included this built-in as a Prolog version of the Unix `make`
utility program. In this case the idea is to reconsult all source files that have been
changed since they were originally compiled into Prolog. YAP has a limited implementation of make/0 that
just goes through every loaded file and verifies whether reloading is needed.
*/
make :-
recorded('$lf_loaded','$lf_loaded'(F1,_M,reconsult,_,_,_,_),_),
'$load_files'(F1, [if(changed)],make),
fail.
make.
make_library_index(_Directory).
'$fetch_stream_alias'(OldStream,Alias) :-
stream_property(OldStream, alias(Alias)), !.
'$require'(_Ps, _M).
'$store_clause'('$source_location'(File, _Line):Clause, File) :-
assert_static(Clause).
exists_source(File) :-
'$full_filename'(File, _AbsFile, exists_source(File)).
% reload_file(File) :-
% ' $source_base_name'(File, Compile),
% findall(M-Opts,
% source_file_property(File, load_context(M, _, Opts)),
% Modules),
% ( Modules = [First-OptsFirst|Rest]
% -> Extra = [ silent(false),
% register(false)
% ],
% merge_options([if(true)|Extra], OptsFirst, OFirst),
% % debug(make, 'Make: First load ~q', [load_files(First:Compile, OFirst)]),
% load_files(First:Compile, OFirst),
% forall(member(Context-Opts, Rest),
% ( merge_options([if(not_loaded)|Extra], Opts, O),
% % debug(make, 'Make: re-import: ~q',
% % [load_files(Context:Compile, O)]),
% load_files(Context:Compile, O)
% ))
% ; load_files(user:Compile)
% ).
% ' $source_base_name'(File, Compile) :-
% file_name_extension(Compile, Ext, File),
% user:prolog_file_type(Ext, prolog), !.
% ' $source_base_name'(File, File).
source_file_property( File0, Prop) :-
( nonvar(File0) -> absolute_file_name(File0,File) ; File = File0 ),
'$source_file_property'( File, Prop).
'$source_file_property'( OldF, includes(F, Age)) :-
recorded('$lf_loaded','$lf_loaded'( F, _M, include, _File, OldF, _Line, _), _),
2015-04-21 23:08:58 +01:00
recorded('$source_file','$source_file'( F, Age, _), _).
2014-07-27 01:14:15 +01:00
'$source_file_property'( F, included_in(OldF, Line)) :-
recorded('$lf_loaded','$lf_loaded'( F, _M, include, _File, OldF, Line, _), _).
'$source_file_property'( F, load_context(OldF, Line, Options)) :-
recorded('$lf_loaded','$lf_loaded'( F, _M, V, _File, OldF, Line, Options), _), V \== include.
'$source_file_property'( F, modified(Age)) :-
2015-04-21 23:08:58 +01:00
recorded('$source_file','$source_file'( F, Age, _), _).
2014-07-27 01:14:15 +01:00
'$source_file_property'( F, module(M)) :-
2014-09-22 18:13:35 +01:00
recorded('$module','$module'(F,M,_,_,_),_).
2014-07-27 01:14:15 +01:00
2014-10-02 14:57:50 +01:00
unload_file( F0 ) :-
absolute_file_name( F0, F1, [expand(true),file_type(prolog)] ),
'$unload_file'( F1, F0 ).
% eliminate multi-files;
% get rid of file-only predicataes.
'$unload_file'( FileName, _F0 ) :-
2014-11-25 12:03:48 +00:00
current_module(Mod),
2015-07-23 01:31:03 +01:00
'$current_predicate'(_A,Mod,P,all),
2014-11-25 12:03:48 +00:00
'$owner_file'(P,Mod,FileName),
\+ '$is_multifile'(P,Mod),
functor( P, Na, Ar),
abolish(Mod:Na/Ar),
fail.
2014-10-02 14:57:50 +01:00
%next multi-file.
'$unload_file'( FileName, _F0 ) :-
2015-04-21 23:08:58 +01:00
recorded('$source_file','$source_file'( FileName, _Age, _), R),
2014-10-02 14:57:50 +01:00
erase(R),
fail.
'$unload_file'( FileName, _F0 ) :-
recorded('$mf','$mf_clause'(FileName,_Name,_Arity,_Module,ClauseRef), R),
erase(R),
2015-12-15 09:28:43 +00:00
'$erase_clause'(ClauseRef),
2014-10-02 14:57:50 +01:00
fail.
'$unload_file'( FileName, _F0 ) :-
2014-10-07 01:35:41 +01:00
recorded('$multifile_dynamic'(_,_,_), '$mf'(_Na,_A,_M,FileName,R), R1),
2014-10-02 14:57:50 +01:00
erase(R1),
erase(R),
fail.
'$unload_file'( FileName, _F0 ) :-
2014-10-07 01:35:41 +01:00
recorded('$multifile_defs','$defined'(FileName,_Name,_Arity,_Mod), R),
2014-10-02 14:57:50 +01:00
erase(R),
fail.
'$unload_file'( FileName, _F0 ) :-
recorded('$module','$module'( FileName, Mod, _SourceF, _, _), R),
erase( R ),
2015-04-13 13:35:37 +01:00
unload_module(Mod),
2014-10-02 14:57:50 +01:00
fail.
'$unload_file'( FileName, _F0 ) :-
recorded('$directive','$d'( FileName, _M:_G, _Mode, _VL, _Pos ), R),
erase(R),
fail.
2015-01-04 23:58:23 +00:00
%% @}
2014-07-27 01:14:15 +01:00
/**
2015-01-04 23:58:23 +00:00
@{
2014-07-27 01:14:15 +01:00
@addtogroup YAPModules
**/
%
% stub to prevent modules defined within the prolog module.
%
module(Mod, Decls) :-
'$current_module'(prolog, Mod), !,
'$export_preds'(Decls).
'$export_preds'([]).
'$export_preds'([N/A|Decls]) :-
functor(S, N, A),
'$sys_export'(S, prolog),
'$export_preds'(Decls).
% prevent modules within the kernel module...
/** @pred use_module(? _M_,? _F_,+ _L_) is directive
SICStus compatible way of using a module
If module _M_ is instantiated, import the procedures in _L_ to the
current module. Otherwise, operate as use_module/2, and load the files
specified by _F_, importing the predicates specified in the list _L_.
2015-04-13 13:35:37 +01:00
*/
2014-07-27 01:14:15 +01:00
use_module(M,F,Is) :- '$use_module'(M,F,Is).
2015-04-13 13:35:37 +01:00
'$use_module'(M,F,Is) :-
2014-07-27 01:14:15 +01:00
var(Is), !,
'$use_module'(M,F,all).
'$use_module'(M,F,Is) :-
nonvar(F), !,
strip_module(F, M0, F0),
'$load_files'(M0:F0, [if(not_loaded),must_be_module(true),imports(Is)], use_module(M,F,Is)),
( var(M) -> true
;
absolute_file_name( F0, F1, [expand(true),file_type(prolog)] ),
2014-09-22 18:13:35 +01:00
recorded('$module','$module'(F1,M,_,_,_),_)
2014-07-27 01:14:15 +01:00
).
'$use_module'(M,F,Is) :-
nonvar(M), !,
strip_module(F, M0, F0),
(
2014-09-22 18:13:35 +01:00
recorded('$module','$module'(F1,M,_,_,_),_)
2014-07-27 01:14:15 +01:00
->
'$load_files'(M0:F1, [if(not_loaded),must_be_module(true),imports(Is)], use_module(M,F,Is))
),
(var(F0) -> F0 = F1 ; absolute_file_name( F1, F2, [expand(true),file_type(prolog)] ) -> F2 = F0 ).
'$use_module'(M,F,Is) :-
'$do_error'(instantiation_error,use_module(M,F,Is)).
/**
@pred reexport(+F) is directive
@pred reexport(+F, +Decls ) is directive
allow a module to use and export predicates from another module
Export all predicates defined in list _F_ as if they were defined in
the current module.
Export predicates defined in file _F_ according to _Decls_. The
declarations should be of the form:
<ul>
A list of predicate declarations to be exported. Each declaration
may be a predicate indicator or of the form `` _PI_ `as`
_NewName_'', meaning that the predicate with indicator _PI_ is
to be exported under name _NewName_.
2015-04-13 13:35:37 +01:00
`except`( _List_)
2014-07-27 01:14:15 +01:00
In this case, all predicates not in _List_ are exported. Moreover,
if ` _PI_ `as` _NewName_` is found, the predicate with
indicator _PI_ is to be exported under name _NewName_ as
before.
Re-exporting predicates must be used with some care. Please, take into
account the following observations:
<ul>
2015-04-13 13:35:37 +01:00
2014-12-24 15:32:29 +00:00
<li> The `reexport` declarations must be the first declarations to
follow the `module` declaration. </li>
2015-04-13 13:35:37 +01:00
2014-12-24 15:32:29 +00:00
<li> It is possible to use both `reexport` and `use_module`, but all
predicates reexported are automatically available for use in the
current module. </li>
2015-04-13 13:35:37 +01:00
2014-12-24 15:32:29 +00:00
<li> In order to obtain efficient execution, YAP compiles
dependencies between re-exported predicates. In practice, this means
that changing a `reexport` declaration and then *just* recompiling
the file may result in incorrect execution. </li>
2015-04-13 13:35:37 +01:00
2014-07-27 01:14:15 +01:00
</ul>
2014-12-24 15:32:29 +00:00
*/
2014-08-23 20:47:40 +01:00
'$reexport'( TOpts, File, Reexport, Imports, OldF ) :-
( Reexport == false -> true ;
2014-07-27 01:14:15 +01:00
'$lf_opt'('$parent_topts', TOpts, OldTOpts),
'$lf_opt'('$context_module', OldTOpts, OldContextModule),
'$import_to_current_module'(File, OldContextModule, Imports, _, TOpts),
'$extend_exports'(File, Imports, OldF )
).
/**
@}
**/
2015-04-13 13:35:37 +01:00
%% @{
2015-01-04 23:58:23 +00:00
2014-07-27 01:14:15 +01:00
/** @defgroup YAPCompilerSettings Directing and Configuring the Compiler
2015-01-04 23:58:23 +00:00
@ingroup YAPProgramming
2014-07-27 01:14:15 +01:00
The YAP system also includes a number of primitives designed to set
compiler parameters and to track the state of the compiler. One
important example is the number of directivees that allow setting up
properties of predicates. It is also possible to enable or disable
waraanings about possible issues with the code in the program, sich
as the occurrence .
2015-04-13 13:35:37 +01:00
This section presents a set of built-ins predicates designed to set the
2014-07-27 01:14:15 +01:00
environment for the compiler.
2014-09-22 18:13:35 +01:00
*/
2014-07-27 01:14:15 +01:00
2015-04-13 13:35:37 +01:00
2014-09-22 18:13:35 +01:00
/** @pred prolog_to_os_filename(+ _PrologPath_,- _OsPath_)
2014-07-27 01:14:15 +01:00
This is an SWI-Prolog built-in. Converts between the internal Prolog
pathname conventions and the operating-system pathname conventions. The
internal conventions are Unix and this predicates is equivalent to =/2
(unify) on Unix systems. On DOS systems it will change the
directory-separator, limit the filename length map dots, except for the
last one, onto underscores.
*/
% add_multifile_predicate when we start consult
'$add_multifile'(Name,Arity,Module) :-
source_location(File,_),
'$add_multifile'(File,Name,Arity,Module).
'$add_multifile'(File,Name,Arity,Module) :-
recorded('$multifile_defs','$defined'(File,Name,Arity,Module), _), !.
2015-08-18 21:08:52 +01:00
% '$early_print'(warning,declaration((multifile Module:Name/Arity),ignored)).
2014-07-27 01:14:15 +01:00
'$add_multifile'(File,Name,Arity,Module) :-
recordz('$multifile_defs','$defined'(File,Name,Arity,Module),_), !,
fail.
'$add_multifile'(File,Name,Arity,Module) :-
recorded('$mf','$mf_clause'(File,Name,Arity,Module,Ref),R),
erase(R),
'$erase_clause'(Ref,Module),
fail.
'$add_multifile'(_,_,_,_).
% retract old multifile clauses for current file.
'$remove_multifile_clauses'(FileName) :-
recorded('$multifile_defs','$defined'(FileName,_,_,_),R1),
erase(R1),
fail.
'$remove_multifile_clauses'(FileName) :-
recorded('$mf','$mf_clause'(FileName,_,_,Module,Ref),R),
'$erase_clause'(Ref, Module),
erase(R),
fail.
'$remove_multifile_clauses'(_).
/** @pred initialization(+ _G_) is iso
2015-12-15 09:28:43 +00:00
2014-07-27 01:14:15 +01:00
The compiler will execute goals _G_ after consulting the current
file.
2015-04-13 13:35:37 +01:00
Notice that the goal will execute in the calling context, not within the file context,
2014-07-27 01:14:15 +01:00
In other words, the source module and execution directory will be the ones of the parent
environment. Use initialization/2 for more flexible behavior.
*/
'$initialization'(G) :-
2015-12-15 09:28:43 +00:00
'$initialization'( G, after_load ).
'$initialization_queue'(G) :-
2015-11-05 17:21:19 +00:00
b_getval('$lf_status', TOpts),
'$lf_opt'( initialization, TOpts, Ref),
nb:nb_queue_enqueue(Ref, G),
2014-07-27 01:14:15 +01:00
fail.
2015-12-15 09:28:43 +00:00
'$initialization_queue'(_).
2014-07-27 01:14:15 +01:00
/** @pred initialization(+ _Goal_,+ _When_)
Similar to initialization/1, but allows for specifying when
_Goal_ is executed while loading the program-text:
+ now
2015-04-13 13:35:37 +01:00
Execute _Goal_ immediately.
2014-07-27 01:14:15 +01:00
+ after_load
2015-04-13 13:35:37 +01:00
Execute _Goal_ after loading program-text. This is the same as initialization/1.
2014-07-27 01:14:15 +01:00
+ restore
2015-04-13 13:35:37 +01:00
Do not execute _Goal_ while loading the program, but only when restoring a state (not implemented yet).
2014-07-27 01:14:15 +01:00
*/
2015-12-15 09:28:43 +00:00
initialization(G0,OPT) :-
expand_goal(G0, G),
2016-01-03 02:06:09 +00:00
catch('$initialization'(G, OPT), Error, '$LoopError'( Error, consult ) ),
2015-12-15 09:28:43 +00:00
fail.
2016-01-03 02:06:09 +00:00
initialization(_G,_OPT) :-
stop_low_level_trace.
2014-07-27 01:14:15 +01:00
'$initialization'(G,OPT) :-
2016-03-05 12:36:54 +00:00
must_be_of_type(callable, G, initialization(G,OPT)),
must_be_of_type(oneof([after_load, now, restore]),
2016-01-31 10:45:42 +00:00
OPT, initialization(G,OPT)),
2016-01-03 02:06:09 +00:00
(
2015-04-22 21:21:20 +01:00
OPT == now
2014-07-27 01:14:15 +01:00
->
2016-02-13 03:07:48 +00:00
( call(G) -> true ; format(user_error,':- ~w failed.~n',[G]) )
2014-07-27 01:14:15 +01:00
;
2015-04-22 21:21:20 +01:00
OPT == after_load
2014-07-27 01:14:15 +01:00
->
2015-12-15 09:28:43 +00:00
'$initialization_queue'(G)
2015-04-22 21:21:20 +01:00
;
OPT == restore
->
2015-12-15 09:28:43 +00:00
recordz('$call_at_restore', G, _ )
).
2016-01-03 02:06:09 +00:00
:- .
2014-07-27 01:14:15 +01:00
/**
@}
*/
2015-04-13 13:35:37 +01:00
%% @{
2015-01-04 23:58:23 +00:00
2014-07-27 01:14:15 +01:00
/**
@defgroup Conditional_Compilation Conditional Compilation
@ingroup YAPCompilerSettings
2015-01-04 23:58:23 +00:00
Conditional compilation builds on the same principle as
2014-07-27 01:14:15 +01:00
term_expansion/2, goal_expansion/2 and the expansion of
grammar rules to compile sections of the source-code
conditionally. One of the reasons for introducing conditional
compilation is to simplify writing portable code.
Note that these directives can only be appear as separate terms in the
2015-04-22 21:21:20 +01:00
input. Typical usage scenarios include:
2014-07-27 01:14:15 +01:00
Load different libraries on different dialects
Define a predicate if it is missing as a system predicate
Realise totally different implementations for a particular
part of the code due to different capabilities.
Realise different configuration options for your software.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:- if(test1).
section_1.
:- elif(test2).
section_2.
:- elif(test3).
section_3.
:- else.
section_else.
2015-01-04 23:58:23 +00:00
:- endif.
2014-07-27 01:14:15 +01:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-04-13 13:35:37 +01:00
2014-07-27 01:14:15 +01:00
*/
2015-04-13 13:35:37 +01:00
/** @pred if( : _Goal_)
2014-07-27 01:14:15 +01:00
2015-04-22 21:21:20 +01:00
Compile subsequent code only if _Goal_ succeeds. For enhanced
2014-07-27 01:14:15 +01:00
portability, _Goal_ is processed by `expand_goal/2` before execution.
If an error occurs, the error is printed and processing proceeds as if
_Goal_ has failed.
*/
%
% This is complicated because of embedded ifs.
%
'$if'(_,top) :- !, fail.
'$if'(_Goal,_) :-
'$get_if'(Level0),
Level is Level0 + 1,
nb_setval('$if_level',Level),
( '$nb_getval'('$endif', OldEndif, fail) -> true ; OldEndif=top),
2013-10-30 09:41:45 +00:00
( '$nb_getval'('$if_skip_mode', Mode, fail) -> true ; Mode = run ),
2007-10-21 09:48:06 +01:00
nb_setval('$endif',elif(Level,OldEndif,Mode)),
fail.
% we are in skip mode, ignore....
2013-11-08 00:02:38 +00:00
'$if'(_Goal,_) :-
2013-10-29 12:43:31 +00:00
'$nb_getval'('$endif',elif(Level, OldEndif, skip), fail), !,
2015-04-13 13:35:37 +01:00
nb_setval('$endif',endif(Level, OldEndif, skip)).
2007-10-21 09:48:06 +01:00
% we are in non skip mode, check....
'$if'(Goal,_) :-
('$if_call'(Goal)
->
% we will execute this branch, and later enter skip
2013-10-29 12:43:31 +00:00
'$nb_getval'('$endif', elif(Level,OldEndif,Mode), fail),
2007-10-21 09:48:06 +01:00
nb_setval('$endif',endif(Level,OldEndif,Mode))
2013-10-30 09:41:45 +00:00
2007-10-21 09:48:06 +01:00
;
% we are now in skip, but can start an elif.
nb_setval('$if_skip_mode',skip)
).
2015-04-13 13:35:37 +01:00
/**
2014-07-27 01:14:15 +01:00
@pred else
Start `else' branch.
*/
2007-10-21 09:48:06 +01:00
'$else'(top) :- !, fail.
'$else'(_) :-
2012-03-04 10:22:40 +00:00
'$get_if'(0), !,
2007-10-21 09:48:06 +01:00
'$do_error'(context_error(no_if),(:- else)).
% we have done an if, so just skip
'$else'(_) :-
2013-10-30 09:41:45 +00:00
nb_getval('$endif',endif(_Level,_,_)), !,
2007-10-21 09:48:06 +01:00
nb_setval('$if_skip_mode',skip).
% we can try the elif
'$else'(_) :-
2012-03-04 10:22:40 +00:00
'$get_if'(Level),
2007-10-21 09:48:06 +01:00
nb_getval('$endif',elif(Level,OldEndif,Mode)),
nb_setval('$endif',endif(Level,OldEndif,Mode)),
nb_setval('$if_skip_mode',run).
2014-07-27 01:14:15 +01:00
/** @pred elif(+ _Goal_)
Equivalent to `:- else. :-if(Goal) ... :- endif.` In a sequence
as below, the section below the first matching elif is processed, If
no test succeeds the else branch is processed.
*/
2007-10-21 09:48:06 +01:00
'$elif'(_,top) :- !, fail.
'$elif'(Goal,_) :-
2012-03-04 10:22:40 +00:00
'$get_if'(0),
2007-10-21 09:48:06 +01:00
'$do_error'(context_error(no_if),(:- elif(Goal))).
% we have done an if, so just skip
'$elif'(_,_) :-
nb_getval('$endif',endif(_,_,_)), !,
nb_setval('$if_skip_mode',skip).
% we can try the elif
'$elif'(Goal,_) :-
2012-03-04 10:22:40 +00:00
'$get_if'(Level),
2015-12-15 09:28:43 +00:00
'$nb_getval'('$endif',elif(Level,OldEndif,Mode),fail),
2007-10-21 09:48:06 +01:00
('$if_call'(Goal)
->
% we will not skip, and we will not run any more branches.
nb_setval('$endif',endif(Level,OldEndif,Mode)),
nb_setval('$if_skip_mode',run)
;
% we will (keep) on skipping
nb_setval('$if_skip_mode',skip)
).
'$elif'(_,_).
2014-07-27 01:14:15 +01:00
/** @pred endif
End of conditional compilation.
*/
2007-10-21 09:48:06 +01:00
'$endif'(top) :- !, fail.
'$endif'(_) :-
% unmmatched endif.
2012-03-04 10:22:40 +00:00
'$get_if'(0),
2007-10-21 09:48:06 +01:00
'$do_error'(context_error(no_if),(:- endif)).
'$endif'(_) :-
% back to where you belong.
2012-03-04 10:22:40 +00:00
'$get_if'(Level),
2007-10-21 09:48:06 +01:00
nb_getval('$endif',Endif),
Level0 is Level-1,
nb_setval('$if_level',Level0),
arg(2,Endif,OldEndif),
arg(3,Endif,OldMode),
nb_setval('$endif',OldEndif),
nb_setval('$if_skip_mode',OldMode).
'$if_call'(G) :-
2015-08-18 21:08:52 +01:00
catch('$eval_if'(G), E, ('$early_print'(error, E), fail)).
2007-10-21 09:48:06 +01:00
'$eval_if'(Goal) :-
expand_term(Goal,TrueGoal),
once(TrueGoal).
'$if_directive'((:- if(_))).
'$if_directive'((:- else)).
'$if_directive'((:- elif(_))).
'$if_directive'((:- endif)).
2007-11-08 11:22:05 +00:00
2014-10-05 10:18:54 +01:00
'$comp_mode'( OldCompMode, CompMode) :-
var(CompMode), !,
'$fetch_comp_status'( OldCompMode ).
2008-07-11 18:02:10 +01:00
'$comp_mode'(OldCompMode, assert_all) :-
'$fetch_comp_status'(OldCompMode),
nb_setval('$assert_all',on).
'$comp_mode'(OldCompMode, source) :-
'$fetch_comp_status'(OldCompMode),
2015-06-19 01:11:30 +01:00
set_prolog_flag(source, true).
2008-07-11 18:02:10 +01:00
'$comp_mode'(OldCompMode, compact) :-
'$fetch_comp_status'(OldCompMode),
2015-06-19 01:11:30 +01:00
set_prolog_flag(source, false).
2008-07-11 18:02:10 +01:00
'$fetch_comp_status'(assert_all) :-
2012-12-07 08:08:32 +00:00
'$nb_getval'('$assert_all',on, fail), !.
2008-07-11 18:02:10 +01:00
'$fetch_comp_status'(source) :-
2015-06-19 01:11:30 +01:00
current_prolog_flag(source, true), !.
2008-07-11 18:02:10 +01:00
'$fetch_comp_status'(compact).
2007-11-08 11:22:05 +00:00
2014-09-15 19:10:49 +01:00
consult_depth(LV) :- '$show_consult_level'(LV).
2014-07-27 01:14:15 +01:00
/**
2014-12-24 15:32:29 +00:00
@}
@}
2014-07-27 01:14:15 +01:00
*/