YAP 7.1.0
preddyns.yap
Go to the documentation of this file.
1 % The next predicates are applicable only
2 % to dynamic code
3
4/** @file preddyns.yap */
5
6/**
7 * @addtogroup Database
8 * @{
9
10Next follow the main operations on dynamic predicates.
11
12*/
13
14/** @pred asserta(+ _C_) is iso
15
16
17Adds clause _C_ to the beginning of the program. If the predicate is
18undefined, it is declared dynamic (see dynamic/1).
19
20*/
21asserta(Clause) :-
22 '$assert'(Clause, asserta, _Ref).
23
24
25/** @pred assertz(+ _C_) is iso
26
27
28Adds clause _C_ to the end of the program. If the predicate is
29undefined, it is declared dynamic (see dynamic/1).
30
31Most Prolog systems only allow asserting clauses for dynamic
32predicates. This is also as specified in the ISO standard. YAP also allows
33asserting clauses for static predicates, under the restriction that the static predicate may not be live in the stacks.
34*/
35assertz(Clause) :-
36 '$assert'(Clause, assertz, _).
37
38/** @pred assert(+ _C_)
39
40Same as assertz/1. Adds clause _C_ to the program. If the predicate is undefined,
41declare it as dynamic. New code should use assertz/1 for better portability.
42
43Most Prolog systems only allow asserting clauses for dynamic
44predicates. This is also as specified in the ISO standard. YAP allows
45asserting clauses for static predicates, as long as the predicate is not
46in use and the language flag is <tt>cprolog</tt>. Note that this feature is
47deprecated, if you want to assert clauses for static procedures you
48should use assert_static/1.
49
50*/
51assert(Clause) :-
52 '$assert'(Clause, assertz, _).
53
54'$assert'(Clause, Where, R) :-
55 '$yap_strip_clause'(Clause, M, MH, H, B),
56 '$mk_dynamic'(MH:H),
57 '$mk_dynamic',
58 (M==MH->MB=B;MB=M:B),
59 '$compile'((H :-MB), Where, (H :-MB), MH, R).
60'$assert'(Clause, Where, R) :-
61 '$expand_clause'(Clause,C,C0),
62 '$$compile'(C, Where, C0, R).
63
64/** @pred asserta(+ _C_,- _R_)
65
66The same as `asserta(C)` but unifying _R_ with
67the database reference that identifies the new clause, in a
68one-to-one way. Note that `asserta/2` only works for dynamic
69predicates. If the predicate is undefined, it will automatically be
70declared dynamic.
71
72*/
73 asserta(Clause, Ref) :-
74 '$assert'(Clause, asserta, Ref).
75
76
77
78/** @pred assertz(+ _C_,- _R_)
79
80The same as `assertz(C)` but unifying _R_ with
81the database reference that identifies the new clause, in a
82one-to-one way. Note that `asserta/2` only works for dynamic
83predicates. If the predicate is undefined, it will automatically be
84declared dynamic.
85
86
87*/
88assertz(Clause, Ref) :-
89 '$assert'(Clause, assertz, Ref).
90
91/** @pred assert(+ _C_,- _R_)
92
93The same as `assert(C)` ( (see Modifying the Database)) but
94unifies _R_ with the database reference that identifies the new
95clause, in a one-to-one way. Note that `asserta/2` o>ly works for dynamic
96predicates. If the predicate is undefined, it will automatically be
97declared dynamic.
98
99
100*/
101assert(Clause, Ref) :-
102 '$assert'(Clause, assertz, Ref).
103
104
105'$assertz_dynamic'(X, C, C0, Mod) :-
106 (X/\4)=:=0,
107 ,
108 '$head_and_body'(C,H,B),
109 '$assertat_d'(assertz,H,B,C0,Mod,_).
110'$assertz_dynamic'(X,C,C0,Mod) :-
111 '$head_and_body'(C,H,B),
112 functor(H,N,A),
113 ('$check_if_reconsulted'(N,A) ->
114 '$check_if_reconsulted'
115 ;
116 (X/\8)=:=0 ->
117 '$inform_as_reconsulted'(N,A),
118 '$remove_all_d_clauses'(H,Mod)
119 ;
120 '$remove_all_d_clauses'
121 ),
122 '$assertat_d'(assertz,H,B,C0,Mod,_).
123
124
125'$remove_all_d_clauses'(H,M) :-
126 '$is_multifile'(H, M), '$is_multifile',
127 functor(H, Na, A),
128 '$erase_all_mf_dynamic'(Na,A,M).
129'$remove_all_d_clauses'(H,M) :-
130 '$recordedp'(M:H,_,R), erase(R), erase.
131'$remove_all_d_clauses'(_,_).
132
133'$erase_all_mf_dynamic'(Na,A,M) :-
134 source_location( F , _),
135 recorded('$multifile_dynamic'(_,_,_), '$mf'(Na,A,M,F,R), R1),
136 erase(R1),
137 erase(R),
138 erase.
139'$erase_all_mf_dynamic'(_,_,_).
140
141'$assertat_d'(asserta,Head,Body,C0,Mod,R) :- '$assertat_d',
142 '$compile_dynamic'((Head:-Body), asserta, C0, Mod, CR),
143 ( get_value('$abol',true)
144 ->
145 '$predicate_flags'(Head,Mod,Fl,Fl),
146 ( Fl /\ 0x20000000 =\= 0 -> '$check_multifile_pred'(Head,Mod,Fl) ; '$check_multifile_pred' )
147 ;
148 true
149 ),
150 '$head_and_body'(C0, H0, B0),
151 '$recordap'(Mod:Head,(H0 :- B0),R,CR),
152 ( '$is_multifile'(Head, Mod) ->
153 source_location(F, _, _),
154 functor(H0, Na, Ar),
155 recorda('$multifile_dynamic'(_,_,_), '$mf'(Na,Ar,Mod,F,R), _)
156 ;
157 recorda
158 ).
159'$assertat_d'(assertz,Head,Body,C0,Mod,R) :-
160 '$compile_dynamic'((Head:-Body), assertz, C0, Mod, CR),
161 ( get_value('$abol',true)
162 ->
163 '$predicate_flags'(Head,Mod,Fl,Fl),
164 ( Fl /\ 0x20000000 =\= 0 -> '$check_multifile_pred'(Head,Mod,Fl) ; '$check_multifile_pred' )
165 ;
166 true
167 ),
168 '$head_and_body'(C0, H0, B0),
169 '$recordzp'(Mod:Head,(H0 :- B0),R,CR),
170 ( '$is_multifile'(H0, Mod) ->
171 source_location(F,_),
172 functor(H0, Na, Ar),
173 recordz('$multifile_dynamic'(_,_,_), '$mf'(Na,Ar,Mod,F,R), _)
174 ;
175 recordz
176 ).
177
178/** @pred retract(+ _C_) is iso
179
180
181Erases the first clause in the program that matches _C_. This
182predicate may also be used for the static predicates that have been
183compiled when the source mode was `on`. For more information on
184source/0 ( (see Setting the Compiler)).
185
186
187*/
188retract( C ) :-
189 strip_module( C, M, C0),
190 '$check_head_and_body'(M:C0,M1,H,B,retract(M:C)),
191 '$predicate_flags'(H, M1, F, F),
192 '$retract2'(F, H, M1, B,_).
193
194'$retract2'(F, H, M, B, R) :-
195 F /\ 0'$retract2' =:= 0'$retract2', '$retract2',
196
197 % '$is_log_updatable'(H, M), !,
198 '$log_update_clause'(H,M,B,R),
199 ( F /\ 0x20000000 =:= 0x20000000, recorded('$mf','$mf_clause'(_,_,_,_,R),MR), erase(MR), erase ; erase),
200 erase(R).
201'$retract2'(F, H, M, B, R) :-
202 % '$is_dynamic'(H,M), !,
203 F /\ 0'$retract2' =:= 0'$retract2', '$retract2',
204 '$recordedp'(M:H,(H:-B),R),
205 ( F /\ 0x20000000 =:= 0x20000000, recorded('$mf','$mf_clause'(_,_,_,_,MRef),MR), erase(MR), erase(MRef), erase ; erase),
206 erase(R).
207'$retract2'(_, H,M,_,_) :-
208 '$undefined'(H,M), '$undefined',
209 functor(H,Na,Ar),
210 '$dynamic'(Na/Ar,M),
211 '$dynamic'.
212'$retract2'(_, H,M,B,_) :-
213 functor(H,Na,Ar),
214 \+ '$dynamic'(Na/Ar,M),
215 '$do_error'(permission_error(modify,static_procedure,Na/Ar),retract(M:(H:-B))).
216
217/** @pred retract(+ _C_,- _R_)
218
219Erases from the program the clause _C_ whose
220database reference is _R_. The predicate must be dynamic.
221
222
223
224
225*/
226retract(M0C,R) :-
227 var(R), var,
228 '$check_head_and_body'(M0C,M,H,B,retract(M0C,R)),
229 '$retract2'(H, M, B, R).
230retract(M0C,R) :- retract,
231 db_reference(R),
232 '$check_head_and_body'(M0C,M,H,B,retract(M0C,R)),
233 dynamic(H,M),
234 dynamic,
235 instance(R,(H:-B)),
236 erase(R).
237retract(M0C,_) :-
238 '$yap_strip_module'(M0C, M, C),
239 '$fetch_predicate_indicator_from_clause'(C, M, PI),
240 \+ '$dynamic'(PI),
241 '$do_error'(permission_error(modify,static_procedure,PI),retract(M:C)).
242
243'$fetch_predicate_indicator_from_clause'((C :- _), M:Na/Ar) :-
244'$fetch_predicate_indicator_from_clause',
245 '$yap_strip_module'(C, M, C1),
246 functor(C1, Na, Ar).
247'$fetch_predicate_indicator_from_clause'(C, M:Na/Ar) :-
248 '$yap_strip_module'(C, M, C1),
249 functor(C1, Na, Ar).
250
251
252/** @pred retractall(+ _G_) is iso
253
254
255Retract all the clauses whose head matches the goal _G_. Goal
256 _G_ must be a call to a dynamic predicate.
257
258*/
259retractall(MT) :- retractall,
260 '$yap_strip_module'(MT,M,T),
262 functor(T,Na,Ar),
263 (
264 '$is_log_updatable'(T, M) ->
265 ( '$is_multifile'(T, M) ->
266 '$retractall_lu_mf'(T,M,Na,Ar)
267 ;
268 '$retractall_lu'(T,M)
269 )
270 ;
271 '$undefined'(T,M) ->
272 '$dynamic'(Na/Ar,M), '$dynamic'
273 ;
274 '$is_dynamic'(T,M) ->
275 '$erase_all_clauses_for_dynamic'(T, M)
276 ;
277 '$do_error'(permission_error(modify,static_procedure,Na/Ar),retractall(T))
278 ).
279
280'$retractall_lu'(T,M) :-
281 '$free_arguments'(T), '$free_arguments',
282 ( '$purge_clauses'(T,M), '$purge_clauses' ; '$purge_clauses' ).
283'$retractall_lu'(T,M) :-
284 '$log_update_clause'(T,M,_,R),
285 erase(R),
286 erase.
287'$retractall_lu'(_,_).
288
289'$retractall_lu_mf'(T,M,Na,Ar) :-
290 '$log_update_clause'(T,M,_,R),
291 functor(M,Na,Ar),
292 ( recorded('$mf','$mf_clause'(_,Na,Ar,M,R),MR), erase(MR), erase ; erase),
293 erase(R),
294 erase.
295'$retractall_lu_mf'(_,_,_,_).
296
297'$erase_all_clauses_for_dynamic'(T, M) :-
298 '$log_update_clause'(T,M,_,R),
299 functor(T,Na,Ar),
300 ( recorded('$mf','$mf_clause'(_,Na,Ar,M,R),MR), erase(MR), erase ; erase),
301 erase(R),
302 erase.
303'$erase_all_clauses_for_dynamic'(_,_).
304
305/* support for abolish/1 */
306'$abolishd'(T, M) :-
307 '$is_multifile'(T,M),
308 functor(T,Name,Arity),
309 recorded('$mf','$mf_clause'(_,Name,Arity,M,Ref),R),
310 erase(R),
311 erase(Ref),
312 erase.
313'$abolishd'(T, M) :-
314 recorded('$import','$import'(_,M,_,T,_,_),R),
315 erase(R),
316 erase.
317'$abolishd'(T, M) :-
318 '$purge_clauses'(T,M), '$purge_clauses'.
319'$abolishd'(T, M) :-
320 '$kill_dynamic'(T,M), '$kill_dynamic'.
321'$abolishd'(_, _).
322
323
324/** @pred dynamic_predicate(+ _P_,+ _Semantics_)
325
326
327Declares predicate _P_ or list of predicates [ _P1_,..., _Pn_]
328as a dynamic predicate following either `logical` or
329`immediate` semantics.
330
331
332*/
333dynamic_predicate(P,Sem) :-
334 '$bad_if_is_semantics'(Sem, dynamic(P,Sem)).
335dynamic_predicate(P,Sem) :-
336 '$log_upd'(OldSem),
337 ( Sem = logical -> '$switch_log_upd'(1) ; '$switch_log_upd'(0) ),
338 '$current_module'(M),
339 '$dynamic'(P, M),
340 '$switch_log_upd'(OldSem).
341
342'$bad_if_is_semantics'(Sem, Goal) :-
343 var(Sem), var,
344 '$do_error'(instantiation_error,Goal).
345'$bad_if_is_semantics'(Sem, Goal) :-
346 Sem \= '$bad_if_is_semantics', Sem \= '$bad_if_is_semantics', '$bad_if_is_semantics',
347 '$do_error'(domain_error(semantics_indicator,Sem),Goal).
must_be_callable( ?_Goal_ )
start input for a meta-clause
asserta(+ C,- R)
Definition: preddyns.yap:59
retract(+ C)
retract(+ C,- R)
retractall(+ G)
erase(+ R)
instance(+ R,- T)
recordz(+ K, T,- R)
get_value(+ A,- V)
db_reference( T)
functor( T, F, N)
var( T)