YAP 7.1.0
predtypes.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-2014 *
8* *
9**************************************************************************
10* *
11* File: predtypes.yap *
12* Last rev: 8/2/88 *
13* mods: *
14* commen ts: boot file for Prolog *
15* *
16*************************************************************************/
17
18/**
19 @file predtypes.yap
20 @brief YAP bootstrap
21
22 @defgroup YAPPredtypes Declaring a type of predicate:
23 - dynamic (logical_updates)
24 - multifile,
25 - meta_predicate.
26
27 @ingroup predimpl
28
29@{
30
31
32*/
33
34
35% These are pseudo declarations
36% so that the user will get a redefining system predicate
37
38% add_multifile_predicate when we start consult
39'$add_multifile'(Name,Arity,Module) :-
40 source_location(File,_),
41 '$add_multifile'(File,Name,Arity,Module).
42
43'$add_multifile'(File,Name,Arity,Module) :-
44 recorded('$multifile_defs','$defined'(File,Name,Arity,Module), _), recorded.
45% print_message(warning,declaration((multifile Module:Name/Arity),ignored)).
46'$add_multifile'(File,Name,Arity,Module) :-
47 recordz('$multifile_defs','$defined'(File,Name,Arity,Module),_), recordz,
48 recordz.
49'$add_multifile'(File,Name,Arity,Module) :-
50 recorded('$mf','$mf_clause'(File,Name,Arity,Module,Ref),R),
51 erase(R),
52 '$erase_clause'(Ref,Module),
53 '$erase_clause'.
54'$add_multifile'(_,_,_,_).
55
56%
57% can only do as goal in YAP mode.
58%
59/** @pred dynamic( + _P_ )
60
61
62Declares predicate _P_ or list of predicates [ _P1_,..., _Pn_]
63as a dynamic predicate. _P_ must be written as a predicate indicator, that is in form
64 _Name/Arity_ or _Module:Name/Arity_.
65
66```
67:- dynamic god/1.
68```
69
70
71a more convenient form can be used:
72
73```
74:- dynamic son/3, father/2, mother/2.
75```
76
77or, equivalently,
78
79```
80:- dynamic [son/3, father/2, mother/2].
81```
82
83Note:
84
85a predicate is assumed to be dynamic when
86asserted before being defined.
87
88
89*/
90dynamic(X) :-
91 current_prolog_flag(language, yap), current_prolog_flag,
92 '$current_module'(M),
93 '$dynamic'(X, M).
94dynamic(X) :-
95 '$do_error'(context_error(dynamic(X),declaration),query).
96
97'$dynamic'(X,M) :- var(X), var,
98 '$do_error'(instantiation_error,dynamic(M:X)).
99'$dynamic'(X,M) :- var(M), var,
100 '$do_error'(instantiation_error,dynamic(M:X)).
101'$dynamic'(Mod:Spec,_) :- '$dynamic',
102 '$dynamic'(Spec,Mod).
103'$dynamic'([], _) :- '$dynamic'.
104'$dynamic'([H|L], M) :- '$dynamic', '$dynamic'(H, M), '$dynamic'(L, M).
105'$dynamic'((A,B),M) :- '$dynamic', '$dynamic'(A,M), '$dynamic'(B,M).
106'$dynamic'(A//N,Mod) :- integer(N), integer,
107 N1 is N+2,
108 '$dynamic'(A/N1,Mod).
109'$dynamic'(A/N,Mod) :-
110 functor(G, A, N),
111 '$mk_dynamic'(Mod:G),
112'$mk_dynamic'.
113
114
115
116
117/** @pred multifile( _P_ ) is iso
118
119Declares that a predicate or several predicates may be defined
120throughout several files. _P_ is a collection of one or more predicate
121indicators:
122
123```
124:- multifile user:portray_message/2, multifile user:message_hook/3.
125```
126
127Instructs the compiler about the declaration of a predicate _P_ in
128more than one file. It must appear in the first of the loaded files
129where the predicate is declared, and before declaration of any of its
130clauses.
131
132Multifile declarations must be supported by reconsult/1 and
133compile/1: when a multifile predicate is reconsulted,
134only the clauses from the same file are removed.
135
136Since YAP4.3.0 multifile procedures can be static or dynamic.
137
138**/
139multifile(P) :-
140 strip_module(P, OM, Pred),
141 '$multifile'(Pred, OM).
142
143'$multifile'(V, _) :-
144 var(V),
145 var,
146 '$do_error'(instantiation_error,multifile(V)).
147'$multifile'((X,Y), M) :-
148 '$multifile',
149 '$multifile'(X, M),
150 '$multifile'(Y, M).
151'$multifile'(Mod:PredSpec, _) :-
152 '$multifile',
153 '$multifile'(PredSpec, Mod).
154'$multifile'(N//A, M) :- '$multifile',
155 integer(A),
156 A1 is A+2,
157 '$multifile'(N/A1, M).
158'$multifile'(N/A, M) :-
159 '$add_multifile'(N,A,M),
160 '$add_multifile'.
161'$multifile'(N/A, M) :-
162 functor(S,N,A),
163 '$new_multifile'(S, M), '$new_multifile'.
164'$multifile'([H|T], M) :- '$multifile',
165 '$multifile'(H,M),
166 '$multifile'(T,M).
167'$multifile'(P, M) :-
168 '$do_error'(type_error(predicate_indicator,P),multifile(M:P)).
169
170
171
172%
173% did we declare multifile properly?
174%
175'$check_multifile_pred'(Hd, M, _) :-
176 functor(Hd,Na,Ar),
177 source_location(F, _),
178 recorded('$multifile_defs','$defined'(F,Na,Ar,M),_), recorded.
179% oops, we did not.
180'$check_multifile_pred'(Hd, M, Fl) :-
181 % so this is not a multi-file predicate any longer.
182 functor(Hd,Na,Ar),
183 NFl is \(0x20000000) /\ Fl,
184 '$predicate_flags'(Hd,M,Fl,NFl),
185 '$warn_mfile'(Na,Ar).
186
187'$warn_mfile'(F,A) :-
188 write(user_error,'% Warning: predicate '),
189 write(user_error,F/A), write(user_error,' was a multifile predicate '),
190 write(user_error,' (line '),
191 '$start_line'(LN), write(user_error,LN),
192 write(user_error,')'),
193 nl(user_error).
194
195
196
197meta_predicate(P) :-
198 strip_module(P, OM, Pred),
199 '$meta_predicate'(Pred, OM).
200
201
202'$meta_predicate'(P,M) :-
203 var(P),
204 var,
205 '$do_error'(instantiation_error,meta_predicate(M:P)).
206'$meta_predicate'(P,M) :-
207 var(M),
208 var,
209 '$do_error'(instantiation_error,meta_predicate(M:P)).
210'$meta_predicate'((P,_Ps),M) :-
211 '$meta_predicate'(P,M),
212 '$meta_predicate'.
213'$meta_predicate'((_P,Ps),M) :-
214 '$meta_predicate',
215 '$meta_predicate'(Ps,M).
216'$meta_predicate'( D, M ) :-
217 '$yap_strip_module'( M:D, M1, P),
218 P\==D,
219 '$yap_strip_module',
220 '$meta_predicate'( P, M1 ).
221'$meta_predicate'( D, M ) :-
222 functor(D,F,N),
223 '$install_meta_predicate'(D,M,F,N),
224 '$install_meta_predicate'.
225'$meta_predicate'( _D, _M ).
226
227'$install_meta_predicate'(P,M,_F,_N) :-
228 '$new_meta_pred'(P, M),
229 '$new_meta_pred'.
230'$install_meta_predicate'(_P,M,F,N) :-
231 functor(PE,F,N),
232 ( M = prolog -> M2 = _ ; M2 = M),
233 recorded('$m',meta_predicate(M2,PE),R),
234 erase(R),
235 erase.
236'$install_meta_predicate'(P,M,_F,_N) :-
237 recordz('$m' , meta_predicate(M,P),_).
238
239
240:- multifile
241 '$inline'/2,
242 '$full_clause_optimisation'/4.
243
244%
245% can only do as goal in YAP mode.
246%
247/** @pred dynamic( + _P_ )
248
249
250Declares predicate _P_ or list of predicates [ _P1_,..., _Pn_]
251as a dynamic predicate. _P_ must be written as a predicate indicator, that is in form
252 _Name/Arity_ or _Module:Name/Arity_.
253
254```
255:- dynamic god/1.
256```
257
258
259a more convenient form can be used:
260
261```
262:- dynamic son/3, father/2, mother/2.
263```
264
265or, equivalently,
266
267```
268:- dynamic [son/3, father/2, mother/2].
269```
270
271Note:
272
273a predicate is assumed to be dynamic when
274asserted before being defined.
275
276
277*/
278
279
280
281/** @pred multifile( _P_ ) is iso
282
283Declares that a predicate or several predicates may be defined
284throughout several files. _P_ is a collection of one or more predicate
285indicators:
286
287```
288:- multifile user:portray_message/2, multifile user:message_hook/3.
289```
290
291Instructs the compiler about the declaration of a predicate _P_ in
292more than one file. It must appear in the first of the loaded files
293where the predicate is declared, and before declaration of any of its
294clauses.
295
296Multifile declarations must be supported by reconsult/1 and
297compile/1: when a multifile predicate is reconsulted,
298only the clauses from the same file are removed.
299
300Since YAP4.3.0 multifile procedures can be static or dynamic.
301
302**/
303multifile(P) :-
304 strip_module(P, OM, Pred),
305 '$multifile'(Pred, OM).
306
307'$multifile'(V, _) :-
308 var(V),
309 var,
310 '$do_error'(instantiation_error,multifile(V)).
311'$multifile'((X,Y), M) :-
312 '$multifile',
313 '$multifile'(X, M),
314 '$multifile'(Y, M).
315'$multifile'(Mod:PredSpec, _) :-
316 '$multifile',
317 '$multifile'(PredSpec, Mod).
318'$multifile'(N//A, M) :- '$multifile',
319 integer(A),
320 A1 is A+2,
321 '$multifile'(N/A1, M).
322'$multifile'(N/A, M) :-
323 '$add_multifile'(N,A,M),
324 '$add_multifile'.
325'$multifile'(N/A, M) :-
326 functor(S,N,A),
327 '$new_multifile'(S, M), '$new_multifile'.
328'$multifile'([H|T], M) :- '$multifile',
329 '$multifile'(H,M),
330 '$multifile'(T,M).
331'$multifile'(P, M) :-
332 '$do_error'(type_error(predicate_indicator,P),multifile(M:P)).
333
334
335
336%
337% did we declare multifile properly?
338%
339'$check_multifile_pred'(Hd, M, _) :-
340 functor(Hd,Na,Ar),
341 source_location(F, _),
342 recorded('$multifile_defs','$defined'(F,Na,Ar,M),_), recorded.
343% oops, we did not.
344'$check_multifile_pred'(Hd, M, Fl) :-
345 % so this is not a multi-file predicate any longer.
346 functor(Hd,Na,Ar),
347 NFl is \(0x20000000) /\ Fl,
348 '$predicate_flags'(Hd,M,Fl,NFl),
349 '$warn_mfile'(Na,Ar).
350
351'$warn_mfile'(F,A) :-
352 write(user_error,'% Warning: predicate '),
353 write(user_error,F/A), write(user_error,' was a multifile predicate '),
354 write(user_error,' (line '),
355 '$start_line'(LN), write(user_error,LN),
356 write(user_error,')'),
357 nl(user_error).
358
359
360
361private(P) :-
362 strip_module(P, OM, Pred),
363 '$private'(Pred, OM).
364
365
366'$private'(P,M) :-
367 var(P),
368 var,
369 '$do_error'(instantiation_error,private(M:P)).
370'$private'(P,M) :-
371 var(M),
372 var,
373 '$do_error'(instantiation_error,private(M:P)).
374'$private'((P,_Ps),M) :-
375 '$private'(P,M),
376 '$private'.
377'$private'((_P,Ps),M) :-
378 '$private',
379 '$private'(Ps,M).
380'$private'( D, M ) :-
381 '$yap_strip_module'( M:D, M1, P),
382 P\==D,
383 '$yap_strip_module',
384 '$private'( P, M1 ).
385'$private'( F/N, M ) :-
386 functor(D,F,N),
387 '$install_private'(D,M),
388 '$install_private'.
389'$private'( _D, _M ).
390
391'$install_private'(P,M) :-
392 hide_predicate(M:P).
393
394:- multifile
395 '$inline'/2,
396 '$full_clause_optimisation'/4.
397
398
399/**
400 * @}
401 * */
402
hide_predicate(+ Pred)
nl(+ S)
erase(+ R)
recordz(+ K, T,- R)
write(+ S, T)
current_prolog_flag(? Flag,- Value)
functor( T, F, N)
integer( T)
var( T)