YAP 7.1.0
directives.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: directives.yap *
12* Last rev: *
13* mods: *
14* comments: directing system execution *
15* *
16*************************************************************************/
17
18/**
19 * @file directives.yap
20 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
21 * @date Thu Oct 19 11:47:38 2017
22 *
23 * @brief Control File Loading
24 %
25 % @defgroup Directives Prolog Directives
26 @ @ingroup consult
27 *
28 *
29*/
30
31
32:- system_module( '$_directives', [user_defined_directive/2], ['$all_directives'/1,
33 '$exec_directives'/5]).
34
35:- '$command'/4'$system_catch'/4use_system_module( '$_boot', [,
36 ]).
37
38:- '$elif'/2'$else'/1'$endif'/1'$if'/2'$include'/2'$initialization'/1'$initialization'/2'$require'/2'$set_encoding'/1'$use_module'/3use_system_module( '$_consult', [,
39 ,
40 ,
41 ,
42 ,
43 ,
44 ,
45 ,
46 ,
47 ]).
48
49:- '$meta_predicate'/2'$module'/3'$module'/4'$module_transparent'/2use_system_module( '$_modules', [,
50 ,
51 ,
52 ]).
53
54:- '$discontiguous'/2'$dynamic'/2use_system_module( '$_preddecls', [,
55 ]).
56
57:- '$noprofile'/2'$public'/2use_system_module( '$_preds', [,
58 ]).
59
60:- '$thread_local'/2use_system_module( '$_threads', []).
61
62'$all_directives'(_:G1) :- '$all_directives',
63 '$all_directives'(G1).
64'$all_directives'((G1,G2)) :- '$all_directives',
65 '$all_directives'(G1),
66 '$all_directives'(G2).
67'$all_directives'(G) :- '$all_directives',
68 '$directive'(G).
69
70%:- '$multifile'( '$directive'/1, prolog ).
71%:- multifile prolog:'$exec_directive'/5, prolog:'$directive'/1.
72:- '$new_multifile'('$exec_directive'(_,_,_,_,_), prolog).
73:- '$new_multifile'('$directive'(_), prolog).
74
75
76
77'$directive'(block(_)).
78'$directive'(char_conversion(_,_)).
79'$directive'(compile(_)).
80'$directive'(consult(_)).
81'$directive'(discontiguous(_)).
82'$directive'(dynamic(_)).
83'$directive'(elif(_)).
84'$directive'(else).
85'$directive'(encoding(_)).
86'$directive'(endif).
87'$directive'(ensure_loaded(_)).
88'$directive'(expects_dialect(_)).
89'$directive'(if(_)).
90'$directive'(include(_)).
91'$directive'(initialization(_)).
92'$directive'(initialization(_,_)).
93'$directive'(license(_)).
94'$directive'(meta_predicate(_)).
95'$directive'(module(_,_)).
96'$directive'(module(_,_,_)).
97'$directive'(module_transparent(_)).
98'$directive'(multifile(_)).
99'$directive'(noprofile(_)).
100'$directive'(public(_)).
101'$directive'(op(_,_,_)).
102'$directive'(require(_)).
103'$directive'(set_prolog_flag(_,_)).
104'$directive'(reconsult(_)).
105'$directive'(reexport(_)).
106'$directive'(reexport(_,_)).
107'$directive'(predicate_options(_,_,_)).
108'$directive'(thread_initialization(_)).
109'$directive'(thread_local(_)).
110'$directive'(uncutable(_)).
111'$directive'(use_module(_)).
112'$directive'(use_module(_,_)).
113'$directive'(use_module(_,_,_)).
114'$directive'(wait(_)).
115
116'$exec_directives'((G1,G2), Mode, M, VL, Pos) :-
117 '$exec_directives',
118 '$exec_directives'(G1, Mode, M, VL, Pos),
119 '$exec_directives'(G2, Mode, M, VL, Pos).
120'$exec_directives'(G, Mode, M, VL, Pos) :-
121 '$exec_directive'(G, Mode, M, VL, Pos).
122
123
124'$exec_directive'(multifile(D), _, M, _, _) :-
125 '$system_catch'('$multifile'(D, M), M,
126 Error,
127 user:'$LoopError'(Error, top)).
128'$exec_directive'(discontiguous(D), _, M, _, _) :-
129 '$discontiguous'(D,M).
130/** @pred initialization
131
132
133Execute the goals defined by initialization/1. Only the first answer is
134considered.
135
136
137*/
138'$exec_directive'(M:A, Status, _M, VL, Pos) :-
139 '$exec_directives'(A, Status, M, VL, Pos).
140'$exec_directive'(initialization(D), _, M, _, _) :-
141 '$initialization'(M:D).
142'$exec_directive'(initialization(D,OPT), _, M, _, _) :-
143 '$initialization'(M:D, OPT).
144'$exec_directive'(thread_initialization(D), _, M, _, _) :-
145 '$thread_initialization'(M:D).
146'$exec_directive'(expects_dialect(D), _, _, _, _) :-
147 expects_dialect(D).
148'$exec_directive'(encoding(Enc), _, _, _, _) :-
149 '$set_encoding'(Enc).
150'$exec_directive'(include(F), Status, _, _, _) :-
151 '$include'(F, Status).
152% don't declare modules into Prolog Module
153'$exec_directive'(module(N,P), Status, _, _, _) :-
154 '$module'(Status,N,P).
155'$exec_directive'(module(N,P,Op), Status, _, _, _) :-
156 '$module'(Status,N,P,Op).
157'$exec_directive'(meta_predicate(P), _, M, _, _) :-
158 '$meta_predicate'(P,M).
159'$exec_directive'(module_transparent(P), _, M, _, _) :-
160 '$module_transparent'(P, M).
161'$exec_directive'(noprofile(P), _, M, _, _) :-
162 '$noprofile'(P, M).
163'$exec_directive'(require(Ps), _, M, _, _) :-
164 '$require'(Ps, M).
165'$exec_directive'(dynamic(P), _, M, _, _) :-
166 '$dynamic'(P, M).
167'$exec_directive'(thread_local(P), _, M, _, _) :-
168 '$thread_local'(P, M).
169'$exec_directive'(op(P,OPSEC,OP), _, _, _, _) :-
170 '$current_module'(M),
171 op(P,OPSEC,M:OP).
172'$exec_directive'(set_prolog_flag(F,V), _, _, _, _) :-
173 set_prolog_flag(F,V).
174'$exec_directive'(ensure_loaded(Fs), _, M, _, _) :-
175 load_files(M:Fs, [if(changed)]).
176'$exec_directive'(char_conversion(IN,OUT), _, _, _, _) :-
177 char_conversion(IN,OUT).
178'$exec_directive'(public(P), _, M, _, _) :-
179 '$public'(P, M).
180'$exec_directive'(compile(Fs), _, M, _, _) :-
181 load_files(M:Fs, []).
182'$exec_directive'(reconsult(Fs), _, M, _, _) :-
183 load_files(M:Fs, []).
184'$exec_directive'(consult(Fs), _, M, _, _) :-
185 load_files(M:Fs, [consult(consult)]).
186'$exec_directive'(use_module(F), _, M, _, _) :-
187 use_module(M:F).
188'$exec_directive'(reexport(F), _, M, _, _) :-
189 load_files(M:F, [if(changed), silent(true), reexport(true),must_be_module(true)]).
190'$exec_directive'(reexport(F,Spec), _, M, _, _) :-
191 load_files(M:F, [if(changed), silent(true), imports(Spec), reexport(true),must_be_module(true)]).
192'$exec_directive'(use_module(F, Is), _, M, _, _) :-
193 use_module(M:F, Is).
194'$exec_directive'(use_module(Mod,F,Is), _,M, _, _) :-
195 use_module(Mod,M:F,Is).
196'$exec_directive'(block(BlockSpec), _, _, _, _) :-
197 '$block'(BlockSpec).
198'$exec_directive'(wait(BlockSpec), _, _, _, _) :-
199 '$wait'(BlockSpec).
200'$exec_directive'(table(PredSpec), _, M, _, _) :-
201 '$table'(PredSpec, M).
202'$exec_directive'(uncutable(PredSpec), _, M, _, _) :-
203 '$uncutable'(PredSpec, M).
204'$exec_directive'(if(Goal), Context, M, _, _) :-
205 '$if'(M:Goal, Context).
206'$exec_directive'(else, Context, _, _, _) :-
207 '$else'(Context).
208'$exec_directive'(elif(Goal), Context, M, _, _) :-
209 '$elif'(M:Goal, Context).
210'$exec_directive'(endif, Context, _, _, _) :-
211 '$endif'(Context).
212'$exec_directive'(license(_), Context, _, _, _) :-
213 Context \= '$exec_directive'.
214'$exec_directive'(predicate_options(PI, Arg, Options), Context, Module, VL, Pos) :-
215 Context \= '$exec_directive',
216 '$exec_directive':expand_predicate_options(PI, Arg, Options, Clauses),
217 '$assert_list'(Clauses, Context, Module, VL, Pos).
218
219'$assert_list'([], _Context, _Module, _VL, _Pos).
220'$assert_list'([Clause|Clauses], Context, Module, VL, Pos) :-
221 '$command'(Clause, VL, Pos, Context),
222 '$assert_list'(Clauses, Context, Module, VL, Pos).
223
224
225%
226% allow users to define their own directives.
227%
228user_defined_directive(Dir,_) :-
229 '$directive'(Dir), '$directive'.
230user_defined_directive(Dir,Action) :-
231 functor(Dir,Na,Ar),
232 functor(NDir,Na,Ar),
233 '$current_module'(M, prolog),
234 assert_static(prolog:'$directive'(NDir)),
235 assert_static(prolog:('$exec_directive'(Dir, _, _, _, _) :- Action)),
236 '$current_module'(_, M).
237
238'$thread_initialization'(M:D) :-
239 eraseall('$thread_initialization'),
240 %writeln(M:D),
241 recorda('$thread_initialization',M:D,_),
242 recorda.
243'$thread_initialization'(M:D) :-
244 '$initialization'(M:D).
245
246%
247 % This command is very different depending on the language mode we are in.
248 %
249 % ISO only wants directives in files
250 % SICStus accepts everything in files
251 % YAP accepts everything everywhere
252 %
253 '$process_directive'(G, top, M, VL, Pos) :-
254 current_prolog_flag(language_mode, yap), current_prolog_flag, /* strict_iso on */
255 '$process_directive'(G, consult, M, VL, Pos).
256 '$process_directive'(G, top, M, _, _) :-
257 '$process_directive',
258 '$do_error'(context_error((:-M:G),clause),query).
259 %
260 % default case
261 %
262 '$process_directive'(Gs, Mode, M, VL, Pos) :-
263 '$all_directives'(Gs), '$all_directives',
264 '$exec_directives'(Gs, Mode, M, VL, Pos).
265
266 %
267 % ISO does not allow goals (use initialization).
268 %
269'$process_directive'(D, _, M, _VL, _Pos) :-
270 current_prolog_flag(language_mode, iso),
271 current_prolog_flag, % ISO Prolog mode, go in and do it,
272 '$do_error'(context_error((:- M:D),query),directive).
273 %
274 % but YAP and SICStus do.
275 %
276'$process_directive'(G, _Mode, M, _VL, _Pos) :-
277 '$execute'(M:G),
278 '$execute'.
279'$process_directive'(G, M , M, _VL, _Pos) :-
280 format(user_error,':- ~w failed.~n',[M,G]).
281'$process_directive'(G, _Mode, M, _VL, _Pos) :-
282 format(user_error,':- ~w:~w failed.~n',[M,G]).
283
284
285
286
287
288
289
290
assert_static(: C)
op(+ P,+ T,+ A)
eraseall(+ K)
use_module( +Files )
use_module(+Files, +Imports)
use_module(? M,? F,+ L)
current_prolog_flag(? Flag,- Value)
set_prolog_flag(+ Flag,+ Value)
load_files(+_Files_, +_Options_)
functor( T, F, N)