1 % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4 :- module(kernel_tools,[
5 ground_value_check/2,
6 ground_value_opt_check/3, % version with an additional flag to discard check if no longer needed
7 bexpr_variables/2,
8 ground_bexpr/1,
9 ground_bexpr_check/2,
10 value_variables/2, value_variables/3,
11 ground_value/1,
12 ground_value_or_field/1,
13 ground_state_check/2, ground_state/1,
14 ground_typedvals_check/2,
15 %discard_ground_value_check/1,
16 %map_over_bvalue/3,
17 filter_cannot_match/4, get_template_for_filter_cannot_match/2,
18 cannot_match/2, can_match/2,
19 cannot_match_aggressive/2,
20 quick_same_value/2,
21 add_call_residue_error/3,
22 check_residue_for_call/2, check_residue_for_call_wf/5,
23 clpfd_residue_ok/1, portray_residue/1
24 ]).
25
26
27 :- use_module(module_information,[module_info/2]).
28 :- module_info(group,kernel).
29 :- module_info(description,'This module provides utilities for term variables for B expressions and values.').
30
31 :- use_module(self_check).
32 :- use_module(error_manager).
33 :- use_module(bsyntaxtree,[safe_syntaxelement_det/5]).
34
35 % use when ground value check no longer need to instantiate result variable:
36 % seems to have performance issues
37 %discard_ground_value_check(X) :- nonvar(X),!,
38 % (X==ground_value -> true ; print(illegal_ground_value_check(X)),nl).
39 %discard_ground_value_check(ground_value).
40
41 :- assert_must_succeed((kernel_tools:ground_value_check(int(X),V), var(V), X=1, nonvar(V))).
42 :- assert_must_succeed((kernel_tools:ground_value_check(string(X),V), var(V), X=a, nonvar(V))).
43 :- assert_must_succeed((kernel_tools:ground_value_check([int(1),int(X)],V), var(V), X=1, nonvar(V))).
44 % instantiate second argument to atomic-nonvar term if first argument ground B value
45 :- block ground_value_check(-,?).
46 ground_value_check([],R) :- !, R=ground_value.
47 ground_value_check(pred_true,R) :- !, R=ground_value.
48 ground_value_check(pred_false,R) :- !, R=ground_value.
49 ground_value_check(int(X),R) :- !, ground_atom(X,R).
50 ground_value_check(global_set(X),R) :- !, ground_atom(X,R).
51 ground_value_check(fd(X,_T),R) :- !,ground_atom(X,R). % avoid waking up co-routines with R=X.
52 ?ground_value_check(string(X),R) :- !, ground_atom(X,R).
53 ground_value_check((A,B),R) :- !, ground_value_check(A,GA), ground_value_check_aux(GA,B,R).
54 ?ground_value_check([H|T],R) :- !, ground_value_check(H,GA), ground_value_check_aux(GA,T,R).
55 ground_value_check(avl_set(A),R) :- !, % assume avl_set grounded
56 check_avl_set(A),
57 R = ground_value.
58 ground_value_check(rec(F),R) :- !, ground_fields(F,R).
59 ground_value_check(freeval(ID,Case,Val),R) :- !,
60 ? ground_freeval_check(ID,Case,Val,R).
61 ground_value_check(closure(_P,_T,B),R) :- !,
62 bexpr_variables(B,Vars),
63 ground_value_check(Vars,R).
64 %when(ground(Vars),R=ground_value).
65 ?ground_value_check(term(X),R) :- !, ground_term(X,R).
66 ground_value_check(Val,R) :- %print(uncov(Val,R)),nl,trace,
67 (atomic(Val) -> R=ground_value ; when(ground(Val),R=ground_value)).
68
69 :- block ground_term(-,?).
70 ?ground_term(floating(F),R) :- !, ground_atom(F,R).
71 ?ground_term(Term,R) :- when(ground(Term),R=ground_value).
72
73 :- block ground_atom(-,?).
74 ground_atom(_,ground_value). %we could do a check ground_atom(X,R) :- (nonvar(R) -> nl,print(illegal_grval(X,R)),nl,nl ; R=X).
75 % we use this co-routine rather than direct unification to avoid unnecessarily waking up pending nonvar/ground checks
76 % indeed: if we unify X with another variable Y, this will wake up all when(nonvar(X) checks
77 % in test 1101 for prob_examples/examples/B/Systerel/C578.EML.014/612_001.mch this caused performance problems
78 % we woke up another pending co-routine with the same waitflag priority, it had a check when((nonvar(X);ground(WF1),
79 % WF1 was already ground: this meant we started evaluating another Prolog kernel call before finishing the current one
80
81 :- block ground_value_check_aux(-,?,?).
82 % :- block ground_value_check_aux(-,?,-). % this slows down test 884, 292, 293, 1456, 1737
83 %ground_value_check_aux(_,_,R) :- nonvar(R),!. % groundness check no longer needed
84 ?ground_value_check_aux(_,V,R) :- ground_value_check(V,R).
85
86 :- block ground_fields(-,?).
87 ground_fields([],R) :- !, R=ground_value.
88 ground_fields([field(F,V)|T],R) :- ground_fields_aux(F,V,T,R).
89
90 :- block ground_fields_aux(-,?,?,?).
91 ground_fields_aux(_,V,T,R) :- ground_fields(T,GrT), ground_value_check_aux(GrT,V,R).
92
93 :- block ground_freeval_check(-,?,?,?), ground_freeval_check(?,-,?,?).
94 ground_freeval_check(_ID,_,Val,R) :-
95 % Note: for polymorphic types the ID can contain type variables, but should be ground
96 ? ground_value_check(Val,R).
97 % -------------------------------
98
99 :- block ground_state_check(-,?).
100 ground_state_check([],R) :- !, R=ground_value.
101 ground_state_check([bind(F,Val)|T],R) :- !, ground_state_check_aux(F,Val,T,R).
102 ground_state_check(BL,R) :- add_internal_error('Illegal bind list:',ground_state_check(BL,R)).
103
104 :- block ground_state_check_aux(-,?,?,?).
105 ground_state_check_aux(_,Val,T,R) :- ground_state_check(T,GrT), ground_value_check_aux(GrT,Val,R).
106
107
108 :- block ground_typedvals_check(-,?).
109 ground_typedvals_check([],R) :- !, R=ground_value.
110 ground_typedvals_check([typedval(Val,_Type,VarID,_EnumWarning)|T],R) :- !, ground_typedvals_check_aux(VarID,Val,T,R).
111 ground_typedvals_check(TL,R) :- add_internal_error('Illegal typedval list:',ground_typedvals_check(TL,R)).
112
113 :- block ground_typedvals_check_aux(-,?,?,?).
114 ground_typedvals_check_aux(_,Val,T,R) :- ground_typedvals_check(T,GrT), ground_value_check_aux(GrT,Val,R).
115
116
117 :- block ground_bexpr_check(-,?).
118 ground_bexpr_check(BExpr,R) :-
119 bexpr_variables(BExpr,Vars),
120 ground_value_check(Vars,R).
121
122 % -------------------------------
123
124
125 % attempt at a more efficient version of term_variables; avoiding traversing big avl_set's
126 %bexpr_variables(T,V) :- !, term_variables(T,V).
127 bexpr_variables(b(P,_,_),Vars) :-
128 bexpr_variables_aux(P,[],Vars), !.
129 bexpr_variables(T,V) :- var(V),
130 add_internal_error('Computation of vars failed: ',bexpr_variables(T,V)),
131 term_variables(T,V).
132
133 % check if a B expression or predicate is ground
134 ground_bexpr(b(P,_,_)) :- !,
135 bexpr_variables_aux(P,[],[]).
136 ground_bexpr(T) :- add_internal_error('Illegal call: ',ground_bexpr(T)),
137 ground(T).
138
139 % get free variables inside a B value
140 value_variables(V,Vars) :- var(V),!, Vars=[V]. % optimize a few frequent cases
141 value_variables(avl_set(A),Vars) :- !, check_avl_set(A),Vars=[].
142 value_variables([],Vars) :- !, Vars=[].
143 value_variables(Val,Vars) :-
144 value_variables(Val,[],Vars).
145
146 :- load_files(library(system), [when(compile_time), imports([environ/2])]).
147
148 % check if a B value is ground
149 %version below seems a bit faster than ground_value(Val) :- value_variables(Val,[],[]).
150
151 :- if(environ(prob_safe_mode,true)).
152 ground_value(Val) :- ground_value1(Val),!,
153 (ground(Val) -> true
154 ; add_internal_error('Value incorrectly marked as ground by:',ground_val(Val)),fail).
155 ground_value(Val) :- ground(Val), !,add_internal_error('Value incorrectly marked as non-ground:',ground_val(Val)).
156 :- else.
157 ground_value(Val) :-ground_value1(Val).
158 :- endif.
159
160 ground_value1(V) :- var(V),!,fail.
161 ground_value1(pred_true) :- !.
162 ground_value1(pred_false) :- !.
163 ground_value1(int(N)) :- !, nonvar(N).
164 ground_value1(global_set(N)) :- !, ground(N).
165 ground_value1(freetype(N)) :- !, ground(N).
166 ground_value1(string(S)) :- !, ground(S).
167 ground_value1(fd(X,T)) :- !, nonvar(X),nonvar(T).
168 ground_value1([]) :- !.
169 ground_value1(avl_set(A)) :- !,check_avl_set(A).
170 ground_value1([H|T]) :- !, ground_value1(H),ground_value1(T).
171 ground_value1((A,B)) :- !, ground_value1(A),ground_value1(B).
172 ground_value1(freeval(ID,Case,Value)) :- !,
173 ground(ID),
174 ground(Case), % Case can be non-ground, ID should be nonvar, but could contain type variables!
175 ground_value1(Value).
176 ground_value1(rec(F)) :- !, ground_fields(F).
177 ground_value1(closure(_P,_T,B)) :- !, ground_bexpr(B). %% DO WE NEED TO CHECK P ? Probably not
178 ground_value1(term(T)) :- !, ground(T).
179 ground_value1(T) :- functor(T,F,N),
180 add_internal_error('Uncovered term in ground_value: ',F/N),
181 ground(T).
182
183 ground_fields(V) :- var(V),!,fail.
184 ground_fields([]).
185 ground_fields([field(Name,Val)|T]) :- ground(Name), ground_value1(Val), ground_fields(T).
186
187 % a version of ground_value that also accepts record fields and lists of record fields without printing warnings
188 ground_value_or_field(V) :- var(V),!,fail.
189 ground_value_or_field(field(F,Value)) :- !, ground(F), ground_value(Value).
190 ground_value_or_field([H|T]) :- !, ground_value_or_field(H),ground_value_or_field(T).
191 ground_value_or_field(V) :- ground_value(V).
192
193 % check if a B state is ground
194 ground_state(V) :- var(V),!,fail.
195 ground_state([]) :- !.
196 ground_state([bind(N,V)|T]) :- !, ground(N),
197 ground_value(V), ground_state(T).
198 ground_state(S) :- add_internal_error('Illegal state: ',ground_state(S)),fail.
199
200 :- use_module(library(aggregate),[term_variables/3]).
201
202 bexpr_variables(b(P,_,_)) --> bexpr_variables_aux(P).
203 % following clause not really needed !?, bexpr_variables_bin/l_bexpr_variables perform propagation
204 %bexpr_variables(b(P,_,_),In,Out) :-
205 % (Out==[] -> In=[] ; true), % useful to propagate empty set to output for ground_bexpr checks
206 % bexpr_variables_aux(P,In,Out).
207
208 value_variables(V) --> {var(V)},!,term_variables_atomic(V). % slightly unnecessary to do two var checks
209 value_variables(avl_set(A)) --> !,{check_avl_set(A)},[].
210 value_variables((A,B)) --> !, value_variables(A),value_variables(B).
211 value_variables([H|T]) --> !, value_variables(H),value_variables(T).
212 value_variables(closure(_P,_T,B)) --> !, bexpr_variables(B). %% DO WE NEED TO CHECK P ? Probably not
213 value_variables(pred_false) --> !,[].
214 value_variables(pred_true) --> !,[].
215 value_variables(fd(X,T)) --> !, {check_nonvar(T)},term_variables_atomic(X).
216 value_variables([]) --> !,[].
217 value_variables(rec(F)) --> !, record_variables(F).
218 value_variables(freeval(ID,Case,Value)) --> !,
219 value_variables(ID),
220 value_variables(Case), % Case can be a variable
221 value_variables(Value).
222 value_variables(string(S)) --> !,term_variables_atomic(S). % not sure this is useful
223 value_variables(int(I)) --> !,term_variables_atomic(I).
224 value_variables(global_set(S)) --> !, term_variables_atomic(S). % normally global_sets should always be ground
225 value_variables(T) --> %{ functor(T,F,N), print(uncov_value_variables(F,N)),nl},
226 term_variables(T).
227
228 check_nonvar(T) :- (var(T) -> add_internal_error('Unexpected variable: ',T) ; true).
229
230 record_variables(V) --> {var(V)},!,term_variables(V).
231 record_variables([]) --> [].
232 record_variables([field(F,V)|T]) -->
233 term_variables_atomic(F), % is this necessary ?
234 value_variables(V), record_variables(T).
235
236 :- use_module(tools, [exact_member/2]).
237 % an optimized version of term_variables/3 which assumes argument is either var or ground
238 term_variables_atomic(VarOrGround,Vars,NewVars) :-
239 var(VarOrGround),
240 \+ exact_member(VarOrGround,Vars),
241 !,
242 NewVars = [VarOrGround|Vars].
243 term_variables_atomic(_,Vars,Vars).
244
245 %bexpr_variables_aux(value(V)) --> !,value_variables(V).
246 bexpr_variables_aux(value(V),In,Out) :- !,
247 (Out==[] -> In=[], ground_value(V) % avoid collecting variables after first variable found, useful for ground_bexpr
248 ; value_variables(V,In,Out)).
249 % 0-ary
250 bexpr_variables_aux(boolean_false) --> !,[].
251 bexpr_variables_aux(boolean_true) --> !,[].
252 bexpr_variables_aux(bool_set) --> !,[].
253 bexpr_variables_aux(empty_set) --> !,[].
254 bexpr_variables_aux(empty_sequence) --> !,[].
255 bexpr_variables_aux(falsity) --> !,[].
256 bexpr_variables_aux(max_int) --> !,[].
257 bexpr_variables_aux(min_int) --> !,[].
258 bexpr_variables_aux(float_set) --> !,[].
259 bexpr_variables_aux(real_set) --> !,[].
260 bexpr_variables_aux(string_set) --> !,[].
261 bexpr_variables_aux(truth) --> !,[].
262 %unary:
263 bexpr_variables_aux(card(A)) --> !, bexpr_variables(A).
264 bexpr_variables_aux(closure(A)) --> !, bexpr_variables(A).
265 %bexpr_variables_aux(closure1(A)) --> !, bexpr_variables(A).
266 bexpr_variables_aux(convert_bool(A)) --> !, bexpr_variables(A).
267 bexpr_variables_aux(convert_real(A)) --> !, bexpr_variables(A).
268 bexpr_variables_aux(convert_int_floor(A)) --> !, bexpr_variables(A).
269 bexpr_variables_aux(convert_int_ceiling(A)) --> !, bexpr_variables(A).
270 bexpr_variables_aux(domain(A)) --> !, bexpr_variables(A).
271 bexpr_variables_aux(identifier(_)) --> !,[].
272 bexpr_variables_aux(integer(_)) --> !,[].
273 bexpr_variables_aux(integer_set(_)) --> !,[].
274 bexpr_variables_aux(first(A)) --> !, bexpr_variables(A).
275 bexpr_variables_aux(first_of_pair(A)) --> !, bexpr_variables(A).
276 bexpr_variables_aux(last(A)) --> !, bexpr_variables(A).
277 bexpr_variables_aux(max(A)) --> !, bexpr_variables(A).
278 bexpr_variables_aux(min(A)) --> !, bexpr_variables(A).
279 bexpr_variables_aux(perm(A)) --> !, bexpr_variables(A).
280 bexpr_variables_aux(pow_subset(A)) --> !, bexpr_variables(A).
281 bexpr_variables_aux(pow1_subset(A)) --> !, bexpr_variables(A).
282 bexpr_variables_aux(range(A)) --> !, bexpr_variables(A).
283 bexpr_variables_aux(real(_)) --> !,[].
284 bexpr_variables_aux(reflexive_closure(A)) --> !, bexpr_variables(A).
285 bexpr_variables_aux(rev(A)) --> !, bexpr_variables(A).
286 bexpr_variables_aux(reverse(A)) --> !, bexpr_variables(A).
287 bexpr_variables_aux(second_of_pair(A)) --> !, bexpr_variables(A).
288 bexpr_variables_aux(seq(A)) --> !, bexpr_variables(A).
289 bexpr_variables_aux(seq1(A)) --> !, bexpr_variables(A).
290 bexpr_variables_aux(size(A)) --> !, bexpr_variables(A).
291 bexpr_variables_aux(string(_)) --> !, [].
292 bexpr_variables_aux(unary_minus(A)) --> !, bexpr_variables(A).
293 bexpr_variables_aux(unary_minus_real(A)) --> !, bexpr_variables(A).
294 % binary:
295 bexpr_variables_aux(add(A,B)) --> !, bexpr_variables_bin(A,B).
296 bexpr_variables_aux(add_real(A,B)) --> !, bexpr_variables_bin(A,B).
297 bexpr_variables_aux(conjunct(A,B)) --> !, bexpr_variables_bin(A,B).
298 bexpr_variables_aux(disjunct(A,B)) --> !, bexpr_variables_bin(A,B).
299 bexpr_variables_aux(div(A,B)) --> !, bexpr_variables_bin(A,B).
300 bexpr_variables_aux(div_real(A,B)) --> !, bexpr_variables_bin(A,B).
301 bexpr_variables_aux(domain_restriction(A,B)) --> !, bexpr_variables_bin(A,B).
302 bexpr_variables_aux(domain_subtraction(A,B)) --> !, bexpr_variables_bin(A,B).
303 bexpr_variables_aux(cartesian_product(A,B)) --> !, bexpr_variables_bin(A,B).
304 %bexpr_variables_aux(comprehension_set(A,B)) --> !, bexpr_variables_bin(A,B).
305 bexpr_variables_aux(couple(A,B)) --> !, bexpr_variables_bin(A,B).
306 bexpr_variables_aux(equal(A,B)) --> !, bexpr_variables_bin(A,B).
307 bexpr_variables_aux(equivalence(A,B)) --> !, bexpr_variables_bin(A,B).
308 bexpr_variables_aux(function(A,B)) --> !, bexpr_variables_bin(A,B).
309 bexpr_variables_aux(greater_equal(A,B)) --> !, bexpr_variables_bin(A,B).
310 bexpr_variables_aux(greater(A,B)) --> !, bexpr_variables_bin(A,B).
311 bexpr_variables_aux(image(A,B)) --> !, bexpr_variables_bin(A,B).
312 bexpr_variables_aux(implication(A,B)) --> !, bexpr_variables_bin(A,B).
313 bexpr_variables_aux(insert_front(A,B)) --> !, bexpr_variables_bin(A,B).
314 bexpr_variables_aux(insert_tail(A,B)) --> !, bexpr_variables_bin(A,B).
315 bexpr_variables_aux(intersection(A,B)) --> !, bexpr_variables_bin(A,B).
316 bexpr_variables_aux(interval(A,B)) --> !, bexpr_variables_bin(A,B).
317 bexpr_variables_aux(lazy_lookup_expr(_)) --> !,[].
318 bexpr_variables_aux(lazy_lookup_pred(_)) --> !,[].
319 bexpr_variables_aux(less_equal(A,B)) --> !, bexpr_variables_bin(A,B).
320 bexpr_variables_aux(less(A,B)) --> !, bexpr_variables_bin(A,B).
321 bexpr_variables_aux(less_equal_real(A,B)) --> !, bexpr_variables_bin(A,B).
322 bexpr_variables_aux(less_real(A,B)) --> !, bexpr_variables_bin(A,B).
323 bexpr_variables_aux(member(A,B)) --> !, bexpr_variables_bin(A,B).
324 bexpr_variables_aux(minus(A,B)) --> !, bexpr_variables_bin(A,B).
325 bexpr_variables_aux(minus_real(A,B)) --> !, bexpr_variables_bin(A,B).
326 bexpr_variables_aux(modulo(A,B)) --> !, bexpr_variables_bin(A,B).
327 bexpr_variables_aux(multiplication(A,B)) --> !, bexpr_variables_bin(A,B).
328 bexpr_variables_aux(multiplication_real(A,B)) --> !, bexpr_variables_bin(A,B).
329 bexpr_variables_aux(negation(A)) --> !, bexpr_variables(A).
330 bexpr_variables_aux(not_equal(A,B)) --> !, bexpr_variables_bin(A,B).
331 bexpr_variables_aux(not_member(A,B)) --> !, bexpr_variables_bin(A,B).
332 bexpr_variables_aux(not_subset(A,B)) --> !, bexpr_variables_bin(A,B).
333 bexpr_variables_aux(not_subset_strict(A,B)) --> !, bexpr_variables_bin(A,B).
334 bexpr_variables_aux(overwrite(A,B)) --> !, bexpr_variables_bin(A,B).
335 bexpr_variables_aux(partial_function(A,B)) --> !, bexpr_variables_bin(A,B).
336 bexpr_variables_aux(partition(A,BL)) --> !, bexpr_variables(A), l_bexpr_variables(BL).
337 bexpr_variables_aux(range_restriction(A,B)) --> !, bexpr_variables_bin(A,B).
338 bexpr_variables_aux(range_subtraction(A,B)) --> !, bexpr_variables_bin(A,B).
339 bexpr_variables_aux(record_field(A,_FieldName)) --> !, bexpr_variables(A).
340 bexpr_variables_aux(restrict_front(A,B)) --> !, bexpr_variables_bin(A,B).
341 bexpr_variables_aux(restrict_tail(A,B)) --> !, bexpr_variables_bin(A,B).
342 bexpr_variables_aux(set_subtraction(A,B)) --> !, bexpr_variables_bin(A,B).
343 bexpr_variables_aux(subset(A,B)) --> !, bexpr_variables_bin(A,B).
344 bexpr_variables_aux(subset_strict(A,B)) --> !, bexpr_variables_bin(A,B).
345 bexpr_variables_aux(union(A,B)) --> !, bexpr_variables_bin(A,B).
346 % ternary
347 bexpr_variables_aux(if_then_else(A,B,C)) --> !, l_bexpr_variables([A,B,C]).
348 % with new local variables:
349 bexpr_variables_aux(comprehension_set(_Ids,A)) --> !, bexpr_variables(A).
350 bexpr_variables_aux(exists(_Ids,A)) --> !, bexpr_variables(A).
351 bexpr_variables_aux(event_b_comprehension_set(_Ids,A,B)) --> !, bexpr_variables_bin(A,B).
352 bexpr_variables_aux(forall(_Ids,A,B)) --> !, bexpr_variables_bin(A,B).
353 bexpr_variables_aux(lambda(_Ids,A,B)) --> !, bexpr_variables_bin(A,B).
354 bexpr_variables_aux(lazy_let_expr(_ID,A,B)) --> !, bexpr_variables_bin(A,B).
355 bexpr_variables_aux(lazy_let_pred(_ID,A,B)) --> !, bexpr_variables_bin(A,B).
356 bexpr_variables_aux(lazy_let_subst(_ID,A,B)) --> !, bexpr_variables_bin(A,B).
357 %bexpr_variables_aux(let_predicate(_Ids,E,A)) --> !, bexpr_variables(E), bexpr_variables(A).
358 %bexpr_variables_aux(let_expression(_Ids,E,A)) --> !, bexpr_variables(E), bexpr_variables(A).
359 bexpr_variables_aux(general_sum(_Ids,A,B)) --> !, bexpr_variables_bin(A,B).
360 bexpr_variables_aux(general_product(_Ids,A,B)) --> !, bexpr_variables_bin(A,B).
361 bexpr_variables_aux(quantified_union(_Ids,A,B)) --> !, bexpr_variables_bin(A,B).
362 bexpr_variables_aux(quantified_intersection(_Ids,A,B)) --> !, bexpr_variables_bin(A,B).
363 % with lists:
364 bexpr_variables_aux(sequence_extension(A)) --> !, l_bexpr_variables(A).
365 bexpr_variables_aux(set_extension(A)) --> !, l_bexpr_variables(A).
366 bexpr_variables_aux(struct(A)) --> !, bexpr_variables(A).
367 bexpr_variables_aux(tail(A)) --> !, bexpr_variables(A).
368 bexpr_variables_aux(total_function(A,B)) --> !, bexpr_variables_bin(A,B).
369 bexpr_variables_aux(external_function_call(_FunName,A)) --> !, l_bexpr_variables(A).
370 bexpr_variables_aux(external_pred_call(_FunName,A)) --> !, l_bexpr_variables(A).
371 % TODO: rec(Fields)
372 % general case (could be used for all above; but is considerably slower)
373 bexpr_variables_aux(Expr) -->
374 %{ functor(Expr,F,N), print(uncov_var_aux(F,N)),nl },
375 {safe_syntaxelement_det(Expr,Subs,_Names,_List,_Constant)},
376 l_bexpr_variables(Subs).
377
378 bexpr_variables_bin(A,B,In,Out) :-
379 (Out==[] -> In=[],Int=[] ; true), % propagate for ground_bexpr use case where Out is []
380 bexpr_variables(A,In,Int), bexpr_variables(B,Int,Out).
381
382 l_bexpr_variables([]) --> [].
383 %l_bexpr_variables([H]) --> !, bexpr_variables(H). % propagate empty result list for ground_bexpr
384 l_bexpr_variables([H|T],In,Out) :-
385 (Out==[] -> In=[], Int=[] ; true), % propagate for ground_bexpr use case where Out is []
386 bexpr_variables(H,In,Int), l_bexpr_variables(T,Int,Out).
387
388
389
390
391 %:- use_module(custom_explicit_sets, [expand_custom_set_to_list_now/2]).
392 % dispatch. TODO: closure/3 and freetype
393 %:- meta_predicate map_over_bvalue(2,?,?).
394 %map_over_bvalue(_,[],[]) :- !.
395 %map_over_bvalue(Pred,[H|T],[Res|Tail]) :- !,
396 % map_over_bvalue(Pred,H,Res),
397 % map_over_bvalue(Pred,T,Tail).
398 %map_over_bvalue(Pred,(A,B),(ResA,ResB)) :- !,
399 % map_over_bvalue(Pred,A,ResA),
400 % map_over_bvalue(Pred,B,ResB).
401 %map_over_bvalue(Pred,rec(L),rec(Res)) :- !,
402 % map_over_bvalue(Pred,L,Res).
403 %map_over_bvalue(Pred,avl_set(A),Res) :- !,
404 % custom_explicit_sets:expand_custom_set_to_list_now(avl_set(A),L),
405 % map_over_bvalue(Pred,L,Res).
406 %map_over_bvalue(Pred,global_set(A), Res) :- !,
407 % custom_explicit_sets:expand_custom_set_to_list_now(global_set(A), L),
408 % map_over_bvalue(Pred,L,Res).
409 %map_over_bvalue(Pred,field(Name,Value),field(Name,TValue)) :- !,
410 % map_over_bvalue(Pred,Value,TValue).
411 %% apply
412 %map_over_bvalue(Pred,BValue,OutValue) :- !,
413 % call(Pred,BValue,OutValue).
414
415 % ----------------------------------
416 :- assert_must_succeed((kernel_tools:filter_cannot_match([int(X),int(11),Y],int(12),R,F), R==[int(X),Y],F==true)).
417
418 % filter out from the set Set (list part) all elements that cannot match X
419 % Filtered=true means at least one element was filtered out
420 filter_cannot_match(Set,X,NewSet,Filtered) :- instantiated_enough(X),!,
421 filter_cannot_match_aux(Set,X,NewSet,Filtered).
422 filter_cannot_match(S,_,S,false).
423
424 :- use_module(library(avl),[avl_domain/2, avl_height/2]).
425 :- use_module(custom_explicit_sets,[sorted_ground_normalised_list_to_avlset/3]).
426 :- use_module(performance_messages,[cond_perfmessage/2]).
427 filter_cannot_match_aux(Var,_,Res,Filtered) :- var(Var),!,Res=Var,Filtered=false.
428 filter_cannot_match_aux([],_,Res,Filtered) :- !, Res=[],Filtered=false.
429 filter_cannot_match_aux([H|T],X,Res,Filtered) :- !,
430 (cannot_match(X,H) -> Filtered=true,filter_cannot_match_aux(T,X,Res,_)
431 ; Res=[H|RT], filter_cannot_match_aux(T,X,RT,Filtered)).
432 filter_cannot_match_aux(avl_set(Avl),X,R,Filtered) :- check_avl_set(Avl),
433 avl_height(Avl,H),
434 (H<8 -> true % TODO: we could add Solver Strength here
435 ; cond_perfmessage([data_validation_mode/false],avl_set_not_filtered(H,X)),fail),
436 !,
437 avl_domain(Avl,List),
438 filter_cannot_match_aux(List,X,L2,Filtered),
439 (Filtered=true
440 -> sorted_ground_normalised_list_to_avlset(L2,R,filter_cannot_match_aux)
441 ; R=avl_set(Avl)).
442 filter_cannot_match_aux(S,_,S,false).
443
444 :- use_module(probsrc(tools_lists),[member_nonvar_list/2]).
445 % does it make sense to filter Set, or will cannot_match always fail
446 instantiated_enough(V) :- var(V),!,fail.
447 instantiated_enough(pred_true) :- !.
448 instantiated_enough(pred_false) :- !.
449 instantiated_enough(int(X)) :- !, nonvar(X).
450 instantiated_enough(fd(X,T)) :- !, nonvar(X),nonvar(T).
451 instantiated_enough(string(X)) :- !, nonvar(X).
452 instantiated_enough((A,B)) :- instantiated_enough(A) -> true ; instantiated_enough(B).
453 instantiated_enough(rec(Fs)) :- (member_nonvar_list(field(N,V),Fs),nonvar(N),instantiated_enough(V) -> true).
454 % TO DO: free-types, ...
455
456
457 %delay_filter_cannot_match(Set,X,NewSet) :-
458 % block_filter_cannot_match_aux(Set,X,NewSet).
459 %
460 %:- block block_filter_cannot_match_aux(-,?,?).
461 %block_filter_cannot_match_aux([],_,Res) :- !, Res=[].
462 %block_filter_cannot_match_aux([H|T],X,Res) :-
463 % (cannot_match(X,H) -> block_filter_cannot_match_aux(T,X,Res)
464 % ; Res=[H|RT], block_filter_cannot_match_aux(T,X,RT)).
465 %block_filter_cannot_match_aux(S,_,S).
466
467
468 % convert a typed expression into a value that can be used to filter a list of values with filter_cannot_match
469 get_template_for_filter_cannot_match(TExpr,ValueForFiltering) :-
470 get_template_for_filter3(TExpr,ValueForFiltering,NonVar), nonvar(NonVar).
471 get_template_for_filter3(b(E,_,_),Val,NonVar) :-
472 get_template_for_filter_aux(E,Val,NonVar).
473 get_template_for_filter_aux(value(Val),Val,NonVar) :- !, (nonvar(Val) -> NonVar=nonvar ; true).
474 get_template_for_filter_aux(couple(A,B),(VA,VB),NonVar) :- !,
475 get_template_for_filter3(A,VA,NonVar),
476 get_template_for_filter3(B,VB,NonVar).
477 %get_template_for_filter_aux(identifier(_),_,_).
478 get_template_for_filter_aux(_,_,_).
479
480
481 :- assert_must_succeed((kernel_tools:can_match(int(1),_V))).
482 :- assert_must_fail((kernel_tools:cannot_match(int(1),_V))).
483 :- assert_must_succeed((kernel_tools:can_match(int(12),int(12)))).
484 :- assert_must_succeed((kernel_tools:cannot_match(int(1),int(2)))).
485 :- assert_must_succeed((kernel_tools:cannot_match(string(a),string(b)))).
486 :- assert_must_succeed((kernel_tools:can_match(string(a),string(a)))).
487 :- assert_must_succeed(( \+ kernel_tools:cannot_match(string(a),string(_)) )). % phrased with \+ as self_check drops tests unifying with earlier ones
488 :- assert_must_succeed((kernel_tools:can_match(string(_),string(b)))).
489 :- assert_must_succeed((kernel_tools:cannot_match([],[string(b)]))).
490 :- assert_must_succeed((kernel_tools:cannot_match([_],[]))).
491 :- assert_must_succeed((kernel_tools:cannot_match((int(11),string(a)),(int(11),string(b))))).
492 :- assert_must_succeed((kernel_tools:can_match((int(11),string(a)),(int(11),string(_))))).
493 :- assert_must_succeed((kernel_tools:cannot_match(pred_false,pred_true))).
494 :- assert_must_succeed((kernel_tools:can_match(pred_true,pred_true))).
495 :- assert_must_succeed((kernel_tools:can_match(term(_),term(foo)))).
496 :- assert_must_succeed(( \+ kernel_tools:cannot_match(term(foo),term(_)) )).
497 :- assert_must_succeed((kernel_tools:can_match(term(_),term(floating(1.0))))).
498 :- assert_must_succeed(( \+ kernel_tools:cannot_match(term(floating(1.0)),term(_)) )).
499 :- assert_must_succeed((kernel_tools:can_match(term(floating(F)),term(floating(2.0))), var(F))).
500 :- assert_must_succeed(( \+ kernel_tools:cannot_match(term(floating(3.0)),term(floating(3.0))) )).
501 :- assert_must_succeed((kernel_tools:cannot_match(term(floating(1.0)),term(floating(2.0))))).
502 :- assert_must_succeed((kernel_tools:cannot_match(term(foo),term(bar)))).
503 :- assert_must_succeed((kernel_tools:can_match(term(T),term(foo)), var(T))).
504 :- assert_must_succeed((kernel_tools:can_match(rec([field(a,pred_true)]),rec([field(a,_)])))).
505 :- assert_must_succeed((kernel_tools:cannot_match(rec([field(a,pred_false)]),rec([field(a,pred_true)])))).
506 :- assert_must_succeed(( \+ kernel_tools:cannot_match(rec([field(a,pred_true)]),rec([field(a,pred_true)])) )).
507 :- assert_must_succeed((kernel_tools:can_match(rec([field(a,pred_true)]),rec(R)), var(R))).
508 :- assert_must_succeed((kernel_tools:can_match(rec(R),rec([field(a,pred_false)])), var(R))).
509 :- assert_must_succeed((kernel_tools:can_match(rec([field(a,pred_true)|_]),rec([field(a,pred_true),field(b,int(1))])))).
510 :- assert_must_succeed((kernel_tools:can_match(rec([field(a,int(1)),field(b,_)]),rec([field(a,int(1)),field(b,int(2))])))).
511 :- assert_must_succeed((kernel_tools:cannot_match(rec([field(a,int(1)),field(b,int(3))]),rec([field(a,int(1)),field(b,int(2))])))).
512 :- assert_must_succeed((kernel_tools:cannot_match((int(1),rec([field(a,string(x))])),(int(1),rec([field(a,string(y))]))))).
513 :- assert_must_succeed((kernel_tools:filter_cannot_match([rec([field(a,int(1)),field(b,X)]),rec([field(a,int(2)),field(b,Y)])],
514 rec([field(a,int(2)),field(b,_)]),R,F), R==[rec([field(a,int(2)),field(b,Y)])],F==true, var(X))).
515
516 % can_match: quick check to see if the (AVL or List) element could match
517
518 can_match(A,B) :- \+ cannot_match(A,B).
519 cannot_match(A,B) :- \+ can_match_aux(A,B). % Note: probably better to wrap can_match into negation to avoid instantiations
520
521 :- use_module(clpfd_interface,[clpfd_can_match/2]).
522
523 can_match_aux(A,_) :- var(A),!. % TO DO: we could check for not_empty_set co-routine if A or B = []?
524 can_match_aux(_,B) :- var(B),!.
525 can_match_aux((A,B),(ElA,ElB)) :- !,can_match_aux(A,ElA), can_match_aux(B,ElB).
526 can_match_aux(int(A),R) :- !,
527 % (\+ ground(R) -> true,print(ok(int(A),R)),nl ; print(unify(R,int(A))),nl,R=int(A)). % unification could trigger other constraint propagations and be expensive; we could just check that A is in domain
528 (var(R) -> true
529 ; R=int(B), clpfd_can_match(A,B)). %,(B=A -> true ; print(ko(A,B)),nl,fail)).%,fail,trace,B=A)).
530 can_match_aux(fd(NrA,GS),R) :- !,
531 %(\+ ground(R) -> true ; R=fd(Nr,GS)). % ditto (see, e.g., test 985 - graph iso)
532 (var(R) -> true ; R=fd(NrB,GS), clpfd_can_match(NrA,NrB)).
533 can_match_aux(pred_false,R) :- !, R=pred_false.
534 can_match_aux(pred_true,R) :- !, R=pred_true.
535 can_match_aux(string(A),R) :- !, %(\+ ground(R) -> true ; R=string(A)). % may instantiate LHS
536 (var(R) -> true ; R=string(B), can_match_atomic(A,B)).
537 can_match_aux(term(A),R) :- !, %(\+ ground(R) -> true ; R=term(A)). % ditto
538 (var(R) -> true ; R=term(B), can_match_term(A,B)).
539 can_match_aux(freeval(ID,Case,Value),R) :- !,
540 (var(R) -> true
541 ; R=freeval(IDR,CaseR,VR),
542 (var(IDR) ; var(CaseR) -> true
543 ; (ID,Case)=(IDR,CaseR),
544 can_match_aux(Value,VR))).
545 can_match_aux([],R) :- !, R \= avl_set(node(_,_,_,_,_)), R\= [_|_].
546 can_match_aux(avl_set(A),R) :- !, check_avl_set(A),
547 can_match_avl_set(R,A).
548 can_match_aux(global_set(_),R) :- !, R \= []. % global sets must be non-empty; TO DO: maybe quick check in case global_set is infinite; TO DO: check if two global_sets are identical
549 can_match_aux([_|_],R) :- !, R \= [].
550 can_match_aux(rec(Fields1),R) :- !, % R is nonvar
551 (R=rec(Fields2) -> can_match_fields(Fields1,Fields2) ; true).
552 can_match_aux(_,_). % catch-all other types
553
554 can_match_fields(A,B) :- (var(A);var(B)),!.
555 can_match_fields([],[]).
556 can_match_fields([F1|T1],[F2|T2]) :- can_match_field(F1,F2), can_match_fields(T1,T2).
557
558 can_match_field(A,B) :- (var(A);var(B)),!.
559 can_match_field(field(F1,_),field(F2,_)) :- (var(F1) ; var(F2)),!. % field names not yet known
560 can_match_field(field(F1,V1),field(F2,V2)) :- F1==F2,!, can_match_aux(V1,V2).
561 can_match_field(F1,F2) :- add_internal_error('Non-matching fields: ',can_match_field(F1,F2)).
562
563 can_match_term(T1,_) :- var(T1),!. % otherwise the next clause would instantiate T1 to floating(_)
564 can_match_term(floating(F1),FF2) :- !,
565 (var(FF2) -> true ; FF2=floating(F2), can_match_atomic(F1,F2)). % floating numbers / reals
566 can_match_term(T1,T2) :- can_match_atomic(T1,T2).
567
568 :- use_module(kernel_dif,[frozen_dif/2]).
569 can_match_atomic(A,B) :- var(A),!, \+ frozen_dif(A,B).
570 can_match_atomic(A,B) :- var(B),!, \+ frozen_dif(B,A).
571 can_match_atomic(A,B) :- A=B.
572
573 :- use_module(library(avl),[avl_min/2, avl_max/2, avl_size/2]).
574
575 can_match_avl_set([],A) :- !, A=empty.
576 can_match_avl_set([H|T],A) :- !, list_can_match_avl_set([H|T],0,A).
577 can_match_avl_set(avl_set(B),A) :- !, avl_min(A,Min), avl_min(B,Min), avl_max(A,Max),avl_max(B,Max).
578 can_match_avl_set(_,_).
579
580 list_can_match_avl_set(T,_,_) :- var(T),!.
581 list_can_match_avl_set([],Sze,A) :- !, avl_size(A,Sze).
582 list_can_match_avl_set([H|T],Sze,A) :- !,
583 (custom_explicit_sets:quick_test_avl_membership(A,H,PredRes)
584 -> PredRes=pred_true, S1 is Sze+1,
585 list_can_match_avl_set(T,S1,A)
586 ; true). % TO DO: maybe also check T ?
587 list_can_match_avl_set(_,_,_).
588
589 % ------------------------
590
591 ?cannot_match_aggressive(A,B) :- \+ can_match_aggr(A,B).
592
593 % a more aggressive version; which may trigger co-routines for left-side A
594 can_match_aggr(A,_) :- var(A),!.
595 can_match_aggr(_,B) :- var(B),!.
596 can_match_aggr((A,B),(ElA,ElB)) :- !,can_match_aggr(A,ElA), can_match_aggr(B,ElB).
597 can_match_aggr(int(A),R) :- !, % R=int(A). % can trigger co-routines both sides; causes problem for 985
598 (var(R) -> true
599 ; R=int(B), (var(B) -> clpfd_can_match(A,B) ; A=B)). % only triggers co-routines for left side
600 can_match_aggr(fd(NrA,GS),R) :- !, % R=fd(Nr,GS). % ditto
601 (var(R) -> true
602 ; R=fd(NrB,GS), (var(NrB) -> clpfd_can_match(NrA,NrB) ; NrA=NrB)).
603 can_match_aggr(string(A),R) :- !, %R=string(A). % ditto
604 (var(R) -> true
605 ; R=string(B), (var(B) -> can_match_atomic(A,B) ; A=B)).
606 can_match_aggr(pred_false,R) :- !, R=pred_false.
607 can_match_aggr(pred_true,R) :- !, R=pred_true.
608 can_match_aggr(A,B) :- can_match_aux(A,B).
609
610
611 % ----------------------------------------
612
613
614
615 /* COMPARING TWO CLOSURES */
616
617
618
619 :- use_module(probsrc(avl_tools),[avl_height_less_than/2]).
620 :- use_module(probsrc(btypechecker),[l_unify_types_strict/2, unify_types_strict/2]).
621
622 % a quick check, use instead of == to compare values
623 quick_same_value(V1,V2) :- var(V1),!,V1==V2.
624 quick_same_value(_,V2) :- var(V2),!,fail.
625 quick_same_value(closure(P,T1,B1),closure(P,T2,B2)) :- !,
626 l_unify_types_strict(T1,T2),
627 quick_same_texpr_body(B1,B2).
628 quick_same_value(avl_set(A),AB) :-
629 !, % == for avl_set can be expensive ! triggered e.g. by x = (1..100000) \ {55} & (x \ {33}) <: x
630 % top-level avl_set usually handled by subset_of_explicit_set or similar code
631 AB=avl_set(B),
632 avl_height_less_than(A,3), % what depth value to use? maybe we should simply not compare at all
633 % TODO: what about other values pairs (avl_set(),_), rec(Fields), ...
634 A==B.
635 quick_same_value(V1,V2) :- V1==V2.
636
637 :- use_module(probsrc(custom_explicit_sets),[quick_definitely_maximal_set_avl/1]).
638 inner_quick_same_value(V1,V2) :- var(V1),!,V1==V2.
639 inner_quick_same_value(_,V2) :- var(V2),!,fail.
640 inner_quick_same_value(closure(P,T1,B1),closure(P,T2,B2)) :- !,
641 l_unify_types_strict(T1,T2), % should never fail
642 quick_same_texpr_body(B1,B2).
643 inner_quick_same_value((V1a,V1b),(V2a,V2b)) :- !,
644 inner_quick_same_value(V1a,V2a),
645 inner_quick_same_value(V1b,V2b).
646 inner_quick_same_value(rec(F1),rec(F2)) :- !,
647 inner_quick_same_fields(F1,F2).
648 inner_quick_same_value(avl_set(AVL),global_set(_)) :- !,
649 quick_definitely_maximal_set_avl(AVL).
650 inner_quick_same_value(global_set(_),avl_set(AVL)) :- !,
651 quick_definitely_maximal_set_avl(AVL).
652 inner_quick_same_value(V1,V2) :- % TODO: we could compare avl_sets
653 V1==V2.
654
655 inner_quick_same_fields(V1,V2) :- var(V1),!,V1==V2.
656 inner_quick_same_fields(_,V2) :- var(V2),!,fail.
657 inner_quick_same_fields([],[]).
658 inner_quick_same_fields([F1|T1],[F2|T2]) :- % TODO: check nonvar(Name...)
659 inner_quick_same_field(F1,F2),
660 inner_quick_same_fields(T1,T2).
661
662 inner_quick_same_field(V1,V2) :- var(V1),!,V1==V2.
663 inner_quick_same_field(_,V2) :- var(V2),!,fail.
664 inner_quick_same_field(field(Name1,V1),field(Name2,V2)) :- Name1==Name2,
665 inner_quick_same_value(V1,V2).
666
667 quick_same_texpr_body(b(E1,Type1,_),b(E2,Type2,_)) :-
668 check_unify_types_strict(Type1,Type2), % types should always match
669 qsame_texpr2(E1,E2).
670
671 :- load_files(library(system), [when(compile_time), imports([environ/2])]).
672 :- if(true). %environ(prob_safe_mode,true)).
673 check_unify_types_strict(T1,T2) :- unify_types_strict(T1,T2),!.
674 check_unify_types_strict(T1,T2) :-
675 add_warning(check_unify_types_strict,'Unexpected difference in types: ',T1 \= T2),fail.
676 :- else.
677 check_unify_types_strict(_,_).
678 :- endif.
679
680 qsame_texpr2(value(V1),value(V2)) :- !,
681 inner_quick_same_value(V1,V2).
682 qsame_texpr2(E1,E2) :-
683 functor(E1,F,Arity),
684 functor(E2,F,Arity),
685 safe_syntaxelement_det(E1,Subs1,_Names1,_List1,Constant1),
686 safe_syntaxelement_det(E2,Subs2,_Names2,_List2,Constant2), Constant2==Constant1,
687 quick_same_sub_expressions(Subs1,Subs2).
688
689 quick_same_sub_expressions([],[]).
690 quick_same_sub_expressions([H1|T1],[H2|T2]) :-
691 quick_same_texpr_body(H1,H2),
692 quick_same_sub_expressions(T1,T2).
693
694 % --------------------
695
696 :- load_files(library(system), [when(compile_time), imports([environ/2])]).
697 :- if(environ(prob_safe_mode,true)).
698 :- use_module(avl_tools,[check_is_non_empty_avl/1]).
699 check_avl_set(V) :- check_is_non_empty_avl(V).
700 :- else.
701 check_avl_set(_).
702 :- endif.
703
704 % ------------------------------------------
705
706
707 :- assert_must_succeed((kernel_tools:ground_value_opt_check(int(X),_,V), var(V), X=1, nonvar(V))).
708 :- assert_must_succeed((kernel_tools:ground_value_opt_check(string(X),_,V), var(V), X=a, nonvar(V))).
709 :- assert_must_succeed((kernel_tools:ground_value_opt_check([int(1),int(X)],_,V), var(V), X=1, nonvar(V))).
710 % instantiate third argument to atomic-nonvar term if first argument ground B value
711 % if second argument is instantiated the check is no longer required, co-routines should be discarded
712 % useful to ensure e.g. that enumeration_only_fdvar or similar checks work
713
714 % a version with an additional Needed flag that can discard the ground value check if no longer needed
715 :- block ground_value_opt_check(-,-,?).
716 ground_value_opt_check(_,Needed,_) :- nonvar(Needed),!,
717 true. % R=ground_value. Warning: instantiating R can change order of firing co-routines in kernel_mappings
718 % This makes test 1720 fail: int_plus instantiates a variable to int(_) -> which triggers ground_value_opt_check
719 % this in turn instantiates R and then triggers another co-routine (convert_list_of_expressions_into_set_wf)
720 % before finishing int_plus (which should have run until int_plus4, frozen_dif in a subsidiary call sees not pending int_plus4 co-routine)
721 % Other tests which fail: 983, 1380
722 ground_value_opt_check([],_,R) :- !, R=ground_value.
723 ground_value_opt_check(pred_true,_,R) :- !, R=ground_value.
724 ground_value_opt_check(pred_false,_,R) :- !, R=ground_value.
725 ground_value_opt_check(int(X),Needed,R) :- !, opt_ground_atom(X,Needed,R).
726 ground_value_opt_check(global_set(X),Needed,R) :- !, opt_ground_atom(X,Needed,R).
727 ground_value_opt_check(fd(X,_T),Needed,R) :- !,opt_ground_atom(X,Needed,R). % avoid waking up co-routines with R=X.
728 ground_value_opt_check(string(X),Needed,R) :- !, opt_ground_atom(X,Needed,R).
729 ground_value_opt_check((A,B),Needed,R) :- !,
730 ground_value_opt_check(A,Needed,GA), ground_value_opt_check_aux(GA,B,Needed,R).
731 ground_value_opt_check([H|T],Needed,R) :- !,
732 ground_value_opt_check(H,Needed,GA), ground_value_opt_check_aux(GA,T,Needed,R).
733 ground_value_opt_check(avl_set(A),_,R) :- !, % assume avl_set grounded
734 check_avl_set(A),
735 R = ground_value.
736 ground_value_opt_check(rec(F),Needed,R) :- !, opt_ground_fields(F,Needed,R).
737 ground_value_opt_check(closure(_P,_T,B),Needed,R) :- !,
738 bexpr_variables(B,Vars),
739 ground_value_opt_check(Vars,Needed,R).
740 %when(ground(Vars),R=ground_value).
741 ground_value_opt_check(term(X),Needed,R) :- !, opt_ground_term(X,Needed,R).
742 ground_value_opt_check(Val,Needed,R) :- %print(uncov(Val,R)),nl,trace,
743 (atomic(Val) -> R=ground_value ; when((ground(Val);nonvar(Needed)),R=ground_value)).
744
745
746 :- block opt_ground_term(-,-,?).
747 opt_ground_term(_,Needed,_) :- nonvar(Needed),!.
748 opt_ground_term(floating(F),Needed,R) :- !, opt_ground_atom(F,Needed,R).
749 opt_ground_term(X,Needed,R) :-
750 when((ground(X); nonvar(Needed)),R=ground_value).
751
752 :- block opt_ground_atom(-,-,?).
753 opt_ground_atom(A,_,Res) :- nonvar(A),!, Res=ground_value.
754 opt_ground_atom(_,_,_).
755
756 :- block ground_value_opt_check_aux(-,?,-,?).
757 ground_value_opt_check_aux(_,V,Needed,R) :- ground_value_opt_check(V,Needed,R).
758 % :- block ground_value_check_aux(-,?,-). % this slows down test 884, 292, 293, 1456, 1737
759
760
761 :- block opt_ground_fields(-,-,?).
762 opt_ground_fields(_,Needed,_) :- nonvar(Needed),!.
763 opt_ground_fields([],_,R) :- !, R=ground_value.
764 opt_ground_fields([field(F,V)|T],Needed,R) :- opt_ground_fields_aux(F,V,T,Needed,R).
765
766 :- block opt_ground_fields_aux(-,?,?,-,?).
767 opt_ground_fields_aux(_,V,T,Needed,R) :- opt_ground_fields(T,Needed,GrT), ground_value_opt_check_aux(GrT,V,Needed,R).
768
769 % ------------------------------------------
770
771 :- use_module(tools_meta, [residue_has_only_clpfd_in_constraints/1]).
772 :- use_module(kernel_reals,[use_clpfd_real_solver/0, do_not_double_check_solution/0]).
773 :- use_module(debug, [debug_mode/1]).
774 add_call_residue_error(Source,Msg,Residue) :-
775 clpfd_residue_ok(Residue),
776 !,
777 ajoin([Msg,' (probably due to use of CLP(FD) for reals): '],Msg2),
778 add_message(Source,Msg2,Residue),
779 (debug_mode(on) -> portray_residue(Residue) ; true).
780 add_call_residue_error(Source,Msg,Residue) :-
781 add_error(Source,Msg,Residue),
782 portray_residue(Residue).
783
784 clpfd_residue_ok(Residue) :-
785 residue_has_only_clpfd_in_constraints(Residue),
786 use_clpfd_real_solver,
787 do_not_double_check_solution. % at the moment CLP(FD) leaves uninstantiated intermediate vars
788 % TODO: check if we should also check get_preference(use_chr_solver,true)
789
790 portray_residue(L) :- gen_clause(L,C), portray_clause((residue :- C)).
791 gen_clause([],true).
792 gen_clause([X],X) :- !.
793 gen_clause([H|T],(H,R)) :- gen_clause(T,R).
794
795 get_functor(Mod:E,Mod:F,N) :- !, get_functor(E,F,N).
796 get_functor((E,_),(EF,'...'),N) :- !, get_functor(E,EF,N).
797 get_functor(E,F,N) :- functor(E,F,N).
798
799 :- use_module(probsrc(tools_strings),[ajoin/2]).
800 :- use_module(tools_printing, [print_goal/1]).
801
802 check_residue_for_call([],_) :- !.
803 check_residue_for_call(Residue,Call) :-
804 check_residue_for_call_wf(Residue,Call,unknown,no_wf_available,_).
805
806 :- use_module(probsrc(preferences),[get_preference/2]).
807 :- use_module(kernel_waitflags,[add_warning_wf/5, add_internal_error_wf/5, add_message_wf/5]).
808 check_residue_for_call_wf([],_,_,_,Res) :- !, Res=residue_ok.
809 check_residue_for_call_wf(Residue,Call,Span,WF,Res) :-
810 get_functor(Call,F,N),
811 (clpfd_residue_ok(Residue)
812 -> ajoin(['Prolog residue for call to ',F,'/',N,' (probably due to use of CLP(FD) for reals): '],Msg),
813 add_message_wf(kernel_tools,Msg,Residue,Span,WF),
814 (debug_mode(on) -> portray_residue(Residue) ; true),
815 Res=residue_ok
816 ; get_preference(use_chr_solver,true) ->
817 ajoin(['Prolog residue for call to ',F,'/',N,' (possibly due to call_residue imprecise for CHR): '],Msg),
818 add_warning_wf(kernel_tools,Msg,Residue,Span,WF),
819 portray_residue(Residue), Res=residue_warning
820 ; ajoin(['Prolog residue for call to ',F,'/',N,':'],Msg),
821 add_internal_error_wf(kernel_tools,Msg,Residue,Span,WF),
822 portray_residue(Residue), print_goal(Call),nl,
823 Res=residue_error
824 ).