1 % (c) 2009-2025 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
5
6 :- module(custom_explicit_sets,[is_set_value/2,
7 is_custom_explicit_set/1, is_custom_explicit_set/2, is_custom_explicit_set_nonvar/1,
8 %equal_explicit_sets/2,
9 equal_explicit_sets_wf/3,
10 not_equal_explicit_sets_wf/3,
11 equality_explicit_sets_wf/4, same_texpr_body/2, same_closure/2,
12 is_empty_explicit_set/1, is_empty_explicit_set_wf/2, is_empty_closure_wf/4,
13 is_non_empty_explicit_set/1, is_non_empty_explicit_set_wf/2,
14 is_non_empty_closure_wf/4,
15 test_empty_explicit_set_wf/3, test_empty_closure_wf/5,
16 is_definitely_maximal_set/1,
17 explicit_set_cardinality/2, explicit_set_cardinality_wf/3,
18 explicit_set_cardinality_for_wf/2,
19 card_for_specific_custom_set/3, % only succeeds if we can compute it efficiently
20 card_for_specific_closure/4,
21 efficient_card_for_set/3, % same, but also for lists
22 quick_custom_explicit_set_approximate_size/2,
23 avl_approximate_size/2, avl_approximate_size/3,
24 is_infinite_explicit_set/1, is_infinite_closure/3,
25 is_infinite_global_set/2, is_simple_infinite_set/1,
26 dont_expand_this_explicit_set/1, dont_expand_this_explicit_set/2,
27 dont_expand_symbolic_explicit_set/1,
28 definitely_expand_this_explicit_set/1,
29 is_infinite_or_very_large_explicit_set/1,
30 is_infinite_or_very_large_explicit_set/2,
31 is_cartesian_product_closure/3,
32 expand_custom_set/2, expand_custom_set_wf/4,
33 try_expand_custom_set/2, try_expand_custom_set_with_catch/3,
34 try_expand_custom_set_wf/4,
35 expand_custom_set_to_list/2, expand_custom_set_to_list/4,
36 expand_custom_set_to_list_wf/5,
37 try_expand_custom_set_to_list_wf/5,
38 expand_custom_set_to_list_no_dups_wf/5,
39 expand_custom_set_to_list_gg/4,
40 try_expand_custom_set_to_list/4,
41 expand_interval_closure_to_avl/3,
42 expand_custom_set_to_list_now/2,
43 expand_closure_to_avl_or_list/6,
44 expand_closure_to_list/7,
45 expand_only_custom_closure_global/4, %try_expand_only_custom_closure_global/2,
46 expand_and_convert_to_avl_set/4,
47 ord_list_to_avlset_direct/3, sorted_ground_normalised_list_to_avlset/3,
48 try_expand_and_convert_to_avl/2, convert_to_avl/2,
49 should_be_converted_to_avl_from_lists/1, should_be_converted_to_avl/1,
50 try_expand_and_convert_to_avl_with_check/3,
51 try_expand_and_convert_to_avl_with_check/4,
52 try_expand_and_convert_to_avl_unless_large_wf/3,
53 %try_expand_and_convert_to_avl_unless_large_wf/3,
54 try_expand_and_convert_to_avl_if_smaller_than/3,
55 is_small_specific_custom_set/2,
56 quick_propagation_element_information/4,
57 element_of_custom_set/2, element_of_custom_set_wf/3,
58 element_of_closure/5,
59 check_element_of_function_closure/6,
60 not_element_of_custom_set_wf/3,
61 membership_custom_set/3, membership_custom_set_wf/4, membership_avl_set_wf/4,
62 quick_test_avl_membership/3,
63 lazy_check_elements_of_closure/6,
64
65 is_efficient_custom_set/1,
66 remove_minimum_element_custom_set/3,
67
68 is_maximal_global_set/1, quick_is_definitely_maximal_set/1,
69 quick_definitely_maximal_set_avl/1,
70 is_one_element_custom_set/2, singleton_set/2, construct_singleton_avl_set/2,
71 is_one_element_avl/2,
72 construct_one_element_custom_set/2,
73 avl_is_interval/3,
74
75 %closure0_for_explicit_set/2,
76 closure1_for_explicit_set/2, closure1_for_explicit_set_from/3,
77 check_in_domain_of_avlset/2, check_unique_in_domain_of_avlset/2,
78 domain_of_explicit_set_wf/3, range_of_explicit_set_wf/3,
79 is_avl_partial_function/1, is_not_avl_partial_function/2,
80 is_avl_total_function_over_domain/2,
81 quick_definitely_maximal_total_function_avl/1,
82 is_avl_relation/1,
83 is_avl_relation_over_domain/3,
84 is_avl_relation_over_range/3,
85 is_not_avl_relation_over_domain_range/4, is_not_avl_relation_over_range/3,
86 is_avl_sequence/1, safe_is_avl_sequence/1,
87 get_avl_sequence/2,
88 is_injective_avl_relation/2,
89 invert_explicit_set/2, union_of_explicit_set/3,
90 union_generalized_explicit_set/3,
91 difference_of_explicit_set_wf/4,
92 intersection_of_explicit_set_wf/4, intersection_with_interval_closure/3,
93 disjoint_intervals_with_inf/4,
94 image_for_id_closure/3, image_for_explicit_set/4,
95 rel_composition_for_explicit_set/3,
96 element_can_be_added_or_removed_to_avl/1,
97 add_element_to_explicit_set_wf/4, remove_element_from_explicit_set/3,
98 delete_element_from_explicit_set/3,
99 at_most_one_match_possible/3,
100 apply_to_avl_set/5, try_apply_to_avl_set/3,
101 min_of_explicit_set_wf/3, max_of_explicit_set_wf/3,
102 sum_or_mul_of_explicit_set/3,
103 %sum_of_range_custom_explicit_set/2, mul_of_range_custom_explicit_set/2,
104 domain_restriction_explicit_set_wf/4,
105 range_restriction_explicit_set_wf/4,
106 domain_subtraction_explicit_set_wf/4,
107 range_subtraction_explicit_set_wf/4,
108 override_pair_explicit_set/4,
109 direct_product_explicit_set/3,
110 override_custom_explicit_set_wf/4,
111 symbolic_functionality_check_closure/2, symbolic_injectivity_check_closure/2,
112
113 subset_of_explicit_set/4, not_subset_of_explicit_set/4,
114 test_subset_of_explicit_set/5,
115
116 conc_custom_explicit_set/2,
117 prefix_of_custom_explicit_set/4, suffix_of_custom_explicit_set/4,
118 concat_custom_explicit_set/4, prepend_custom_explicit_set/3,
119 append_custom_explicit_set/4,
120 tail_sequence_custom_explicit_set/5,
121 last_sequence_explicit_set/2, %first_sequence_explicit_set/2,
122 front_sequence_custom_explicit_set/3,
123 reverse_custom_explicit_set/2,
124 size_of_custom_explicit_set/3,
125
126 get_first_avl_elements/4,
127 construct_avl_from_lists/2, construct_avl_from_lists_wf/3,
128 equal_avl_tree/2,
129 check_avl_in_interval/3, check_interval_in_custom_set/4,
130 check_avl_subset/2,
131 construct_closure/4, is_closure/4, % from closures
132 construct_member_closure/5, % from closures
133
134 construct_interval_closure/3,
135 is_interval_closure/3, % checks if we have a finite interval closure Low..Up (but bounds can be variables)
136 is_interval_closure/5,
137 is_interval_closure_or_integerset/3, is_interval_closure_or_integerset/4,
138 is_interval_with_integer_bounds/3, % checks that bounds are known
139
140 is_powerset_closure/3,
141
142 dom_range_for_specific_closure/5,
143 dom_for_specific_closure/4,
144 dom_for_lambda_closure/2,
145 portray_custom_explicit_set/1,
146 closure_occurs_check/4
147 ]).
148
149 :- meta_predicate call_card_for_relations(-,-,0).
150
151 :- use_module(error_manager).
152 :- use_module(self_check).
153 :- use_module(library(avl)).
154 :- use_module(kernel_waitflags).
155 :- use_module(kernel_tools).
156 :- use_module(delay).
157 :- use_module(tools).
158 :- use_module(avl_tools).
159 :- use_module(library(clpfd)).
160
161 :- use_module(module_information,[module_info/2]).
162 :- module_info(group,kernel).
163 :- module_info(description,'This module provides customised operations for the custom explicit set representations of ProB (closures, avl_sets and global_sets).').
164
165 :- use_module(tools_printing,[print_term_summary/1, print_error/1]).
166 :- use_module(preferences,[preference/2]).
167 :- use_module(kernel_objects,[equal_object/2, equal_object/3]).
168 :- use_module(kernel_freetypes,[enumerate_freetype_wf/4,freetype_cardinality/2,
169 is_infinite_freetype/1, is_empty_freetype/1,
170 is_non_empty_freetype/1, test_empty_freetype/2]).
171
172 :- use_module(clpfd_interface,[try_post_constraint/1, clpfd_reify_inlist/4]).
173 :- use_module(closures).
174 :- use_module(b_compiler).
175
176 :- load_files(library(system), [when(compile_time), imports([environ/2])]).
177
178 /* These meta_predicate declarations do not seem to have the right effect;
179 the predicates below return code, they do not get passed code
180 :- meta_predicate card_for_specific_custom_set(*,*,0).
181 :- meta_predicate card_for_specific_closure(*,*,0).
182 :- meta_predicate is_a_relation(*,*,*,*,*,*,0).
183 :- meta_predicate subset_of_explicit_set(*,*,0,*).
184 :- meta_predicate not_subset_of_explicit_set(*,*,0,*).
185 */
186
187 construct_avl_from_lists(S,Res) :-
188 (convert_to_avl(S,CS) -> true ; print(convert_to_avl_failed(S,CS)),nl,CS=S),
189 Res = CS.
190
191 % version with WF to see call stack in case of virtual time-outs due to expansions
192 construct_avl_from_lists_wf(S,Res,WF) :-
193 (convert_to_avl_wf(S,CS,WF) -> true ; print(convert_to_avl_wf_failed(S,CS)),nl,CS=S),
194 Res = CS.
195
196 :- use_module(tools,[safe_sort/3]).
197 :- block normalised_list_to_avl_when_ground(-,?).
198 normalised_list_to_avl_when_ground(S,R) :- % call if you are not sure that S will be ground; e.g. after closure expansion
199 ground_value_check(S,GS),
200 blocking_normalised_list_to_avl(GS,S,R).
201 :- block blocking_normalised_list_to_avl(-,?,?).
202 blocking_normalised_list_to_avl(_,S,R) :- normalised_list_to_avl(S,R).
203
204 normalised_list_to_avl(S,R) :- safe_sort(normalised_list_to_avl,S,SS),
205 ord_list_to_avlset_direct(SS,AVL,normalised_list_to_avl),
206 equal_object(AVL,R). % due to co-routine, R can now be instantiated
207
208 %set_to_avl(List,R) :- empty_avl(A), add_to_avl(List,A,AR), R=avl_set(AR).
209 add_to_avl([],R,R).
210 add_to_avl([H|T],AVL,AVLOUT) :- avl_store(H,AVL,true,AVL1),
211 add_to_avl(T,AVL1,AVLOUT).
212
213
214 % get only the first x elements of an AVL tree
215 get_first_avl_elements(empty,_,R,all) :- !,R=[].
216 get_first_avl_elements(AVL,X,FirstXEls,CutOff) :-
217 avl_min(AVL,Min), get_first_els(X,Min,AVL,FirstXEls,CutOff).
218
219 get_first_els(X,_,_AVL,R,CutOff) :- X<1,!,R=[], CutOff=not_all.
220 get_first_els(X,Cur,AVL,[Cur|T],CutOff) :-
221 (avl_next(Cur,AVL,Nxt) -> X1 is X-1,get_first_els(X1,Nxt,AVL,T,CutOff)
222 ; T=[],CutOff=all).
223
224 %expand_and_try_convert_to_avl(C,R) :- is_closure(C,_,_,_), expand_custom_set(C,EC), expand_and_convert_to_avl
225 %expand_and_convert_to_avl(C,R) :- convert_to_avl(C,R).
226
227 /* convert all list data-values (with all-sub-values) into avl-form */
228 /* assumption: the value is ground when convert_to_avl is called */
229
230 :- load_files(library(system), [when(compile_time), imports([environ/2])]).
231 :- if(environ(prob_safe_mode,true)).
232 convert_to_avl(X,R) :- \+ ground_value(X), !, add_error(convert_to_avl,'Non-ground argument: ',convert_to_avl(X,R)), R=X.
233 :- endif.
234 convert_to_avl(X,R) :- var(X), !, add_error(convert_to_avl,'Variable argument: ',convert_to_avl(X,R)), R=X.
235 ?convert_to_avl(Term,R) :- no_conversion_necessary(Term),!,
236 R=Term.
237 convert_to_avl(closure(P,T,B),R) :- !,
238 R=closure(P,T,B).
239 convert_to_avl(avl_set(A),R) :- !,(A==empty -> add_warning(convert_to_avl,'Emtpy avl_set'), R=[]
240 ; R=avl_set(A)).
241 convert_to_avl((A,B),(CA,CB)) :- !,convert_to_avl(A,CA), convert_to_avl(B,CB).
242 convert_to_avl(freetype(X),R) :- !, R=freetype(X).
243 convert_to_avl(freeval(ID,Case,Value),R) :- !, R=freeval(ID,Case,CValue),convert_to_avl(Value,CValue).
244 convert_to_avl(rec(Fields),R) :- !, convert_fields(Fields,CFields), R=rec(CFields).
245 convert_to_avl(global_set(GS),R) :- !, R=global_set(GS).
246 ?convert_to_avl([H|T],R) :- !, convert_cons_to_avl_inside_set_wf(H,T,R,no_wf_available).
247 %convert_to_avl(abort(X),_R) :- print(deprecetated_convert_to_avl(abort(X))),nl,!, fail.
248 convert_to_avl(X,R) :- add_internal_error('Unknown term: ',convert_to_avl(X,R)), R=X.
249
250 % pass WF for call stack in case of expansions TODO: complete
251 convert_to_avl_wf((A,B),(CA,CB),WF) :- !,convert_to_avl_wf(A,CA,WF), convert_to_avl_wf(B,CB,WF).
252 convert_to_avl_wf([H|T],R,WF) :- !, convert_cons_to_avl_inside_set_wf(H,T,R,WF).
253 convert_to_avl_wf(X,R,_) :- convert_to_avl(X,R).
254
255 convert_fields(Var,R) :- var(Var),!,
256 add_internal_error('Var arg: ',convert_fields(Var,R)),fail.
257 convert_fields([],[]).
258 convert_fields([field(FieldName,Value)|T],[field(FieldName,CValue)|CT]) :-
259 convert_to_avl_inside_set(Value,CValue),
260 convert_fields(T,CT).
261
262 l_convert_to_avl_wf(Var,_,WF) :- var(Var),!,
263 add_warning_wf(l_convert_to_avl_wf,'Cannot expand variable to avl: ',Var,unknown,WF),
264 fail.
265 l_convert_to_avl_wf([],[],_).
266 l_convert_to_avl_wf(avl_set(A),R,WF) :-
267 expand_custom_set_wf(avl_set(A),ES,l_convert_to_avl,WF),
268 l_convert_to_avl_wf(ES,R,WF).
269 l_convert_to_avl_wf(closure(P,T,B),R,WF) :-
270 expand_custom_set_wf(closure(P,T,B),ES,l_convert_to_avl,WF),
271 l_convert_to_avl_wf(ES,R,WF).
272 l_convert_to_avl_wf([H|T],[CH-true|CT],WF) :-
273 ? convert_to_avl_inside_set_wf(H,CH,WF), l_convert_to_avl_wf(T,CT,WF).
274
275 :- assert_must_succeed((X=(fd(1,'Name'),fd(2,'Name')),
276 custom_explicit_sets:convert_to_avl_inside_set(X,R), R==X)).
277
278 convert_to_avl_inside_set(Var,R) :- var(Var),!,
279 add_internal_error('Var arg: ',convert_to_avl_inside_set(Var,R)),fail.
280 :- if(environ(prob_safe_mode,true)).
281 convert_to_avl_inside_set(fd(A,T),R) :- var(A),!,
282 add_error(convert_to_avl,'Non-ground FD-Term: ',convert_to_avl_inside_set(fd(A,T),R)), R=fd(A,T).
283 convert_to_avl_inside_set(int(X),R) :- var(X),!,
284 add_error(convert_to_avl,'Non-ground integer: ',convert_to_avl_inside_set(int(X),R)), R=int(X).
285 convert_to_avl_inside_set(string(X),R) :- var(X),!,
286 add_error(convert_to_avl,'Non-ground string: ',convert_to_avl_inside_set(string(X),R)), R=string(X).
287 convert_to_avl_inside_set(term(X),R) :- (var(X) ; X=floating(F), var(F)), !,
288 add_error(convert_to_avl,'Non-ground term: ',convert_to_avl_inside_set(term(X),R)), R=term(X).
289 :- endif.
290 ?convert_to_avl_inside_set(Term,R) :- no_conversion_necessary(Term),!,R=Term.
291 convert_to_avl_inside_set(closure(P,T,B),R) :- !,
292 % inside a set, closures need to be expanded to check against other elements
293 expand_closure_to_avl_wf(P,T,B,R,no_wf_available).
294 %convert_to_avl_inside_set(closure_x(_P,_T,_B,E),R) :- !, convert_to_avl_inside_set(E,R).
295 convert_to_avl_inside_set(avl_set(A),R) :- !, normalise_avl_set(A,R). %AVL's inside other AVL's need to be normalised !
296 convert_to_avl_inside_set((A,B),(CA,CB)) :- !,convert_to_avl_inside_set(A,CA), convert_to_avl_inside_set(B,CB).
297 convert_to_avl_inside_set(freetype(X),R) :- !,
298 expand_custom_set(freetype(X),EC,check), convert_to_avl_inside_set(EC,R).
299 convert_to_avl_inside_set(freeval(ID,Case,Value),R) :- !,
300 R=freeval(ID,Case,CValue),convert_to_avl_inside_set(Value,CValue).
301 convert_to_avl_inside_set(rec(Fields),R) :- !, convert_fields(Fields,CFields), R=rec(CFields).
302 convert_to_avl_inside_set(global_set(GS),R) :- !,
303 % first check if GS infinite integer set: in this case do not expand; there can be no confusion with finite avl_sets
304 ? (is_infinite_global_set(GS,_) -> R = global_set(GS)
305 ; expand_only_custom_closure_global(global_set(GS),EC,check,no_wf_available), convert_to_avl_inside_set(EC,R)).
306 convert_to_avl_inside_set([H|T],R) :- !,convert_cons_to_avl_inside_set_wf(H,T,R,no_wf_available).
307 convert_to_avl_inside_set(X,R) :-
308 add_internal_error('Unknown or non-ground argument: ',convert_to_avl_inside_set(X,R)),
309 fail.
310
311 ?convert_to_avl_inside_set_wf(Term,R,_WF) :- no_conversion_necessary(Term),!,R=Term.
312 convert_to_avl_inside_set_wf(closure(P,T,B),R,WF) :- !,
313 ? expand_closure_to_avl_wf(P,T,B,R,WF). % inside a set, closures need to be expanded to check against other elements
314 convert_to_avl_inside_set_wf((A,B),(CA,CB),WF) :- !,
315 ? convert_to_avl_inside_set_wf(A,CA,WF), convert_to_avl_inside_set_wf(B,CB,WF).
316 convert_to_avl_inside_set_wf([H|T],R,WF) :- !,convert_cons_to_avl_inside_set_wf(H,T,R,WF).
317 convert_to_avl_inside_set_wf(V,CV,_WF) :- % use version without WF; TO DO: adapt fully
318 convert_to_avl_inside_set(V,CV).
319
320 % true when we have a simple value that does not need to be converted for use within an avl_set:
321 no_conversion_necessary([]).
322 no_conversion_necessary(pred_false). /* bool_false */
323 no_conversion_necessary(pred_true). /* bool_true */
324 no_conversion_necessary(fd(FD,_)) :- nonvar(FD).
325 no_conversion_necessary(int(I)) :- nonvar(I).
326 no_conversion_necessary(string(S)) :- nonvar(S).
327 ?no_conversion_necessary(term(T)) :- nonvar(T), no_conversion_nec_term(T).
328
329 no_conversion_nec_term(floating(T)) :- nonvar(T).
330 no_conversion_nec_term(T) :- atom(T).
331
332 normalise_avl_set(A,R) :- A=node(_,_,0,empty,empty), !,R=avl_set(A).
333 normalise_avl_set(A,R) :-
334 avl_to_list(A,L),
335 ord_list_to_avlset_direct(L,R,convert_to_avl_inside_set). %AVL's inside other AVL's need to be normalised !
336
337 convert_cons_to_avl_inside_set_wf(H,T,R,WF) :- T==[], !,
338 convert_to_avl_inside_set_wf(H,CH,WF),
339 R = avl_set(node(CH,true,0,empty,empty)).
340 ?convert_cons_to_avl_inside_set_wf(H,T,R,WF) :- l_convert_to_avl_wf([H|T],S,WF),
341 sort(S,SS),
342 ord_list_to_avlset_direct(SS,R,convert_to_avl_inside_set).
343
344 construct_singleton_avl_set(Val,avl_set(node(Val,true,0,empty,empty))).
345
346
347 is_set_value(X,Origin) :- var(X), !,print(is_set_value(Origin)),nl,fail.
348 is_set_value([],_) :- !.
349 is_set_value([_|_],_) :- !.
350 is_set_value(X,_) :- is_custom_explicit_set(X).
351
352 is_custom_explicit_set(X,Origin) :- var(X), !,print(var_is_custom_explicit_set(Origin)),nl,fail.
353 is_custom_explicit_set(X,_) :- is_custom_explicit_set(X).
354
355 is_custom_explicit_set(X) :- var(X), !,print(var_is_custom_explicit_set),nl,fail.
356 is_custom_explicit_set(global_set(_)).
357 is_custom_explicit_set(freetype(_)).
358 %is_custom_explicit_set(integer_global_set(_)).
359 is_custom_explicit_set(avl_set(_)).
360 is_custom_explicit_set(closure(_Parameters,_PT,_Cond)).
361
362 % use if you know the argument to be nonvar
363 is_custom_explicit_set_nonvar(global_set(_)).
364 is_custom_explicit_set_nonvar(freetype(_)).
365 is_custom_explicit_set_nonvar(avl_set(_)).
366 is_custom_explicit_set_nonvar(closure(_Parameters,_PT,_Cond)).
367
368 %:- assert_must_succeed(( custom_explicit_sets:portray_custom_explicit_set(avl_set(empty)) )). % now generates error
369 :- use_module(translate,[translate_bvalue/2]).
370 portray_custom_explicit_set(S) :- translate_bvalue(S,A), format(A,[]),nl.
371
372 /* a predicate to check equality of two custom explicit sets */
373
374 %equal_explicit_sets(A,B) :- equal_explicit_sets_wf(A,B,no_wf_available).
375
376 %equal_explicit_sets(X,Y) :- print_term_summary(equal_explicit_sets(X,Y)),fail.
377 :- block equal_explicit_sets_wf(-,?,?), equal_explicit_sets_wf(?,-,?).
378 ?equal_explicit_sets_wf(A,B,WF) :- equal_explicit_sets4(A,B,allow_expansion,WF).
379
380 equal_explicit_sets4(global_set(X),global_set(Y),_,_WF) :- !,X=Y.
381 equal_explicit_sets4(global_set(B),avl_set(A),E,WF) :- !,equal_explicit_sets4(avl_set(A),global_set(B),E,WF).
382 equal_explicit_sets4(freetype(X),freetype(Y),_,_WF) :- !,X=Y.
383 equal_explicit_sets4(avl_set(A),avl_set(B),_,_WF) :- !,
384 equal_avl_tree(A,B). % alternatively, we could normalise avl_trees and only store normalised versions
385 equal_explicit_sets4(avl_set(A),I2,_,_WF) :-
386 is_interval_closure_or_integerset(I2,L2,U2,Finite2),!, % also covers I2=global_set(...)
387 Finite2=finite, % only a finite interval can be equal to an AVL set
388 avl_equal_to_interval(A,L2,U2).
389 equal_explicit_sets4(avl_set(A),global_set(B),_,WF) :- \+ b_global_sets:b_integer_set(B), !, % integersets dealt with above
390 explicit_set_cardinality_wf(global_set(B),Card,WF),
391 is_finite_card(Card), % Card \= inf as avl_set must be finite
392 explicit_set_cardinality_wf(avl_set(A),Card,WF). /* the sets must be identical as global_set contains all values */
393 equal_explicit_sets4(avl_set(A),CPB,_,WF) :-
394 is_cartesian_product_closure(CPB,B1,B2),!,
395 decompose_avl_set_into_cartesian_product_wf(A,A1,A2,WF),
396 kernel_objects:equal_object_wf(A1,B1,equal_explicit_sets4,WF),
397 kernel_objects:equal_object_wf(A2,B2,equal_explicit_sets4,WF).
398 ?equal_explicit_sets4(closure(P,T,B),avl_set(A),E,WF) :- !, equal_explicit_sets4(avl_set(A),closure(P,T,B),E,WF).
399 equal_explicit_sets4(I1,I2,_,_WF) :- is_interval_closure_or_integerset(I1,L1,U1,Finite1),
400 is_interval_closure_or_integerset(I2,L2,U2,Finite2), !,
401 Finite1=Finite2, % either both finite or infinite
402 L1=L2, U1=U2.
403 equal_explicit_sets4(CPA,CPB,_,WF) :-
404 ? is_cartesian_product_closure(CPA,A1,A2),
405 is_cartesian_product_closure(CPB,B1,B2),!,
406 equal_cartesian_product_wf(A1,A2,B1,B2,WF).
407 % what if both subset or relations or functions ... closure: TO DO: add support
408 equal_explicit_sets4(S1,S2,_,WF) :-
409 is_not_member_value_closure_or_integerset(S1,TYPE,MS1),
410 is_not_member_value_closure_or_integerset(S2,TYPE,MS2),
411 !,
412 kernel_objects:equal_object_wf(MS1,MS2,equal_explicit_sets4,WF).
413 equal_explicit_sets4(closure(P1,T1,B1),closure(P2,T2,B2),_,_WF) :-
414 same_closure_body(P1,T1,B1,P2,T2,B2),!.
415 %equal_explicit_sets4(X,Y) :- X==Y,!.
416 equal_explicit_sets4(Set1,Set2,allow_expansion,WF) :-
417 %kernel_objects:test_finite_set_wf(Set1,F1,WF), kernel_objects:test_finite_set_wf(Set2,F2,WF), equal_expansions(F1,F2,Set1,Set2)
418 card_for_specific_custom_set(Set1,Card1,Code1), % TO DO: do not throw info away if Set2 cannot be determined
419 card_for_specific_custom_set(Set2,Card2,Code2),
420 !,
421 call(Code1), call(Code2),
422 % TO DO: if one of the two sets is infinite, then it would be enough to know that the other is not infinite for failure without expansion
423 equal_expansions(Card1,Card2,Set1,Set2,WF).
424 ?equal_explicit_sets4(Set1,Set2,allow_expansion,WF) :- equal_expansions(0,0,Set1,Set2,WF).
425
426
427 :- use_module(btypechecker, [unify_types_strict/2]).
428 % detect e.g. when one closure has seq(Type) and the other one set(integer,Type)
429 same_types([],[]).
430 same_types([H1|T1],[H2|T2]) :- unify_types_strict(H1,H2), same_types(T1,T2).
431
432 :- block equal_expansions(-,?,?,?,?).
433 equal_expansions(F1,F2,Set1,Set2,WF) :- (number(F1);number(F2)),!,
434 % NOTE: sometimes we get inf for finite but very large sets
435 F1=F2, % unify; can propagate info back to closure; e.g. prj2(BOOL,NAT) = prj2(BOOL,0..n)
436 ? equal_expansions2(F1,F2,Set1,Set2,WF).
437 equal_expansions(F1,F2,Set1,Set2,WF) :-
438 equal_expansions2(F1,F2,Set1,Set2,WF).
439
440 :- block equal_expansions2(-,?,?,?,?), equal_expansions2(?,-,?,?,?).
441 %equal_expansions(0,0,avl_set(A),closure(P,T,B)) :- check_subset ?? in both directions ?
442 %equal_expansions2(inf,inf,Set1,Set2,WF) :- WF \= no_wf_available, !, % symbolic treatment
443 equal_expansions2(F,F,Set1,Set2,WF) :-
444 % only expand if both sets have same cardinality
445 % print_term_summary(equal_expansions3(F,Set1,Set2)),nl,
446 ? equal_expansions3(F,Set1,Set2,WF).
447
448 % TO DO: check if this brings something:
449 %equal_expansions3(avl_set(A),closure(P,T,B),_WF) :- !,
450 % expand_closure_to_avl_or_list(P,T,B,E2,check), % in case E2 is avl_set; we can use equal_avl_tree
451 % ((nonvar(E2),E2=avl_set(B2))
452 % -> print(eql_avl),nl, print_term_summary(equal_avl_tree(A,B2)),nl, equal_avl_tree(A,B2)
453 % ; print(eql_non_avl),nl,equal_object(avl_set(A),E2,equal_expansions3)
454 % ).
455 %:- use_module(library(lists),[perm2/4]).
456 %equal_expansions3(F,Set1,Set2,_WF) :- number(F), F>100, % test with: {{},{TRUE},{FALSE},{TRUE,FALSE}} = /*@symbolic */ {x|x<:BOOL} or
457 % {x|x<:POW(BOOL*BOOL) & (x={} or card(x)>0)} = /*@symbolic */ {x|x<:POW(BOOL*BOOL)} 26 sec -> 14 sec
458 % case does not seem to appear very often
459 % perm2(Set1,Set2,avl_set(_),Set),
460 % is_definitely_maximal_set(Set),
461 %Set2 is maximal and has the same cardinality as F, hence Set1 must be identical to Set2
462 % !,
463 % debug_println(9,equal_to_maximal_closure(F)).
464 equal_expansions3(F,Set1,Set2,WF) :-
465 ? get_identity_as_equivalence(F,Set1,Set2,EQUIV),
466 !,% translate:print_bexpr(EQUIV),nl,
467 copy_wf_start(WF,equal_expansions,CWF),
468 b_test_boolean_expression(EQUIV,[],[],CWF),
469 copy_wf_finish(WF,CWF).
470 % Alternative could be, if difference were to be fully treated symbolically:
471 % difference_of_explicit_set_wf(Set1,Set2,R12,WF), difference_of_explicit_set_wf(Set2,Set1,R21,WF),
472 % kernel_objects:empty_set_wf(R12,WF), kernel_objects:empty_set_wf(R21,WF).
473 equal_expansions3(_,Set1,Set2,WF) :-
474 expand_custom_set_wf(Set1,E1,equal_expansions1,WF),
475 expand_custom_set_wf(Set2,E2,equal_expansions2,WF),
476 E1=E2. /* ensure that ordering and normalization is same for all representations ! */
477
478
479 :- use_module(b_ast_cleanup, [clean_up/3]).
480 get_identity_as_equivalence(F,Set1,Set2,CleanedEQUIV) :-
481 (F=inf %; is_infinite_explicit_set(Set1) ; is_infinite_explicit_set(Set2)
482 ; Set1 \= avl_set(_),Set2 \= avl_set(_), % if one of the two sets is an AVL Set: better compute the other set explicitly instead of using this symbolic treatment
483 (dont_expand_this_explicit_set(Set1,100000) ;
484 dont_expand_this_explicit_set(Set2,100000)
485 )
486 % avl_test check for test 1081; TO DO: instead of test try to expand set and if this leads to enum warning use symbolic check
487 ),
488 ? get_identity_as_equivalence_aux(Set1,Set2,EQUIV),
489 clean_up(EQUIV,[],CleanedEQUIV).
490 % can be useful to replace x : {v|P(v)} --> x:P(x) (remove_member_comprehension) and reuse predicates, see 2483
491 get_identity_as_equivalence_aux(Set1,Set2,EQUIV) :-
492 ? kernel_objects:infer_value_type(Set1,SType),
493 is_set_type(SType,Type),
494 % Construct: !x.(x:Set1 <=> x:Set2) ??
495 get_pos_infos_for_explicit_set(Set1,I1),
496 get_pos_infos_for_explicit_set(Set2,I2),
497 I12 = I1, % we could merge position_info; but two sets could be very far apart
498 TID = b(identifier('_equality_sets_'),Type,[]),
499 EQUIV = b(forall([TID],b(truth,pred,[used_ids([])]),
500 b(equivalence(
501 b(member(TID,b(value(Set1),SType,I1)),pred,I1),
502 b(member(TID,b(value(Set2),SType,I2)),pred,I2)
503 ) ,pred,I12)
504 ),pred,[used_ids([]),I12]).
505
506 :- use_module(bsyntaxtree, [get_texpr_pos/2]).
507 get_pos_infos_for_explicit_set(closure(_,_,Body),[Pos]) :- get_texpr_pos(Body,Pos),!.
508 get_pos_infos_for_explicit_set(_,[]).
509
510 :- use_module(kernel_equality,[eq_atomic/4, equality_objects/3,
511 equality_objects_wf_no_enum/4, equality_objects_with_type_wf/5]).
512 /* maybe rewrite equal_explicit_sets and not_... to use this to avoid maintaining multiple versions */
513 equality_explicit_sets_wf(global_set(X),global_set(Y),R,_WF) :- !, eq_atomic(X,Y,set,R).
514 equality_explicit_sets_wf(global_set(B),avl_set(A),R,WF) :- !,equality_explicit_sets_wf(avl_set(A),global_set(B),R,WF).
515 equality_explicit_sets_wf(freetype(X),freetype(Y),R,_) :- !, eq_atomic(X,Y,set,R).
516 equality_explicit_sets_wf(avl_set(A),avl_set(B),R,_) :- !,
517 (equal_avl_tree(A,B) -> R=pred_true ; R=pred_false). % alternatively, we could normalise avl_trees and only store normalised versions
518 equality_explicit_sets_wf(avl_set(A),I2,R,WF) :- is_interval_closure_or_integerset(I2,L2,U2),!,
519 % also covers I2=global_set(...)
520 avl_equality_to_interval(A,L2,U2,R,WF).
521 equality_explicit_sets_wf(avl_set(A),global_set(B),R,WF) :- \+ b_global_sets:b_integer_set(B), !,
522 explicit_set_cardinality_wf(global_set(B),Card,WF),
523 (is_finite_card(Card), % Card \= inf, %as avl_set must be finite
524 explicit_set_cardinality_wf(avl_set(A),Card,WF)
525 -> R=pred_true /* the sets must be identical as global_set contains all values */
526 ; R=pred_false).
527 equality_explicit_sets_wf(avl_set(A),CPB,R,WF) :-
528 is_cartesian_product_closure(CPB,B1,B2),!,
529 if(decompose_avl_set_into_cartesian_product_wf(A,A1,A2,WF), % should not produce pending co-routines
530 equality_cartesian_product_wf(A1,A2,B1,B2,R,WF),
531 R=pred_false % no cartesian product can be equal to this avl_set
532 ).
533 equality_explicit_sets_wf(closure(P,T,B),avl_set(A),R,WF) :- !,
534 equality_explicit_sets_wf(avl_set(A),closure(P,T,B),R,WF).
535 equality_explicit_sets_wf(I1,I2,R,WF) :- is_interval_closure_or_integerset(I1,L1,U1,Finite1),
536 is_interval_closure_or_integerset(I2,L2,U2,Finite2), !,
537 (Finite1=Finite2 -> equality_objects_wf_no_enum((int(L1),int(U1)),(int(L2),int(U2)),R,WF)
538 ; R = pred_false).
539 equality_explicit_sets_wf(CPA,CPB,R,WF) :-
540 is_cartesian_product_closure(CPA,A1,A2), is_cartesian_product_closure(CPB,B1,B2),!,
541 equality_cartesian_product_wf(A1,A2,B1,B2,R,WF).
542 equality_explicit_sets_wf(S1,S2,R,WF) :-
543 is_not_member_value_closure_or_integerset(S1,TYPE,MS1),
544 is_not_member_value_closure_or_integerset(S2,TYPE,MS2),!,
545 equality_objects_with_type_wf(TYPE,MS1,MS2,R,WF).
546 equality_explicit_sets_wf(closure(P,T,B),closure(P,T,B2),R,_) :-
547 same_texpr_body(B,B2),!,R=pred_true.
548 equality_explicit_sets_wf(Set1,Set2,R,WF) :-
549 Set1 \= [_|_], Set2 \= [_|_], % below we check for avl_set; i.e., useful are only closure/3, global_set/1, ...
550 ? get_identity_as_equivalence(unknown,Set1,Set2,EQUIV),!,
551 copy_wf_start(WF,equal_expansions,CWF),
552 % EQUIV is a universal quantification, usually over an infinite domain
553 b_interpreter_check:b_force_check_boolean_expression(EQUIV,[],[],CWF,R), % we know EQUIV cannot be reified
554 copy_wf_finish(WF,CWF).
555 % TO DO: add complement sets,
556
557 /* Cartesian Product Comparison */
558 :- use_module(kernel_equality,[empty_cartesian_product_wf/4]).
559 % A1*A2 = B1*B2 <=> (((A1={} or A2={}) & (B1={} or B2={})) or (A1=B1 & A2=B2))
560 equal_cartesian_product_wf(A1,A2,B1,B2,WF) :-
561 equality_cartesian_product_wf(A1,A2,B1,B2,pred_true,WF).
562 not_equal_cartesian_product_wf(A1,A2,B1,B2,WF) :-
563 equality_cartesian_product_wf(A1,A2,B1,B2,pred_false,WF).
564
565 equality_cartesian_product_wf(A1,A2,B1,B2,R,_WF) :-
566 nonvar(A1), A1=closure(P,T,BdyA1),
567 nonvar(B1), B1=closure(P,T,BdyB1),
568 nonvar(A2), A2=closure(P2,T2,BdyA2),
569 nonvar(B2), B2=closure(P2,T2,BdyB2),
570 % they have the same names; probably we are comparing identical values (e.g., in bvisual2)
571 same_texpr_body(BdyA1,BdyB1),
572 % note: we cannot simply call equality of A2 and B2 as cartesian products can be empty, see test 2072
573 same_texpr_body(BdyA2,BdyB2),
574 !,
575 R=pred_true.
576 equality_cartesian_product_wf(A1,A2,B1,B2,R,WF) :-
577 empty_cartesian_product_wf(A1,A2,EmptyA,WF),
578 equality_cart_product2(EmptyA,A1,A2,B1,B2,R,WF).
579 :- block equality_cart_product2(-, ?,?,?,?,?,?).
580 equality_cart_product2(pred_true,_,_,B1,B2,R,WF) :- empty_cartesian_product_wf(B1,B2,R,WF).
581 equality_cart_product2(pred_false,A1,A2,B1,B2,R,WF) :- equality_objects_wf_no_enum((A1,A2),(B1,B2),R,WF).
582
583 /* COMPARING AVL-SET with INTERVAL */
584
585 % check if an avl tree is equal to an interval range
586 avl_equal_to_interval(_A,L2,U2) :-
587 infinite_interval(L2,U2),!,fail. % otherwise infinite & avl_set is finite
588 % we can now assume L2, U2 are numbers (but could not yet be instantiated)
589 avl_equal_to_interval(A,L2,U2) :-
590 avl_min(A,int(L2)), avl_max(A,int(U2)),
591 Card is 1+U2-L2,
592 explicit_set_cardinality(avl_set(A),Card). % sets are equal: same size + same lower & upper bound
593
594 avl_not_equal_to_interval(A,L2,U2,WF) :- avl_equality_to_interval(A,L2,U2,pred_false,WF).
595
596 avl_equality_to_interval(_A,L2,U2,R,_WF) :-
597 infinite_interval(L2,U2),!,R=pred_false. % interval infinite & avl_set is finite
598 % we can now assume L2, U2 are numbers (but could not yet be instantiated)
599 avl_equality_to_interval(A,L2,U2,R,WF) :-
600 avl_min(A,int(AL)), avl_max(A,int(AU)),
601 Card is 1+AU-AL,
602 explicit_set_cardinality_wf(avl_set(A),ACard,WF),
603 equality_objects_wf_no_enum((int(ACard),(int(AL),int(AU))),
604 (int(Card),(int(L2),int(U2))),R,WF).
605 % sets are equal if same size + same lower & upper bound
606
607 /* COMPARING TWO CLOSURES */
608
609 % a variation of equal_explicit_sets which tries not expand and just compares two closures
610
611 same_closure(I1,I2) :-
612 is_interval_closure_or_integerset(I1,L1,U1,Finite1),
613 is_interval_closure_or_integerset(I2,L2,U2,Finite2), !,
614 Finite1=Finite2,
615 L1=L2, U1=U2.
616 same_closure(CPA,CPB) :-
617 is_cartesian_product_closure(CPA,A1,A2),
618 is_cartesian_product_closure(CPB,B1,B2),!,
619 equal_cartesian_product_wf(A1,A2,B1,B2,no_wf_available). % could be expensive
620 same_closure(S1,S2) :-
621 is_not_member_value_closure_or_integerset(S1,TYPE,MS1),
622 is_not_member_value_closure_or_integerset(S2,TYPE,MS2),
623 !,
624 kernel_objects:equal_object(MS1,MS2,same_closure). % could be expensive
625 same_closure(closure(P1,T1,B1),closure(P2,T2,B2)) :- same_closure_body_with_parameter_renaming(P1,T1,B1,P2,T2,B2).
626
627 same_closure_body(P,T1, B1, P,T2,B2) :-
628 same_types(T1,T2),
629 same_texpr_body(B1,B2).
630
631 % a version of same_closure_body which allows renaming of the parameters
632 same_closure_body_with_parameter_renaming(P1,T1, B1, P2,T2,B2) :-
633 same_types(T1,T2),
634 create_renaming(P1,P2,Renaming),
635 % TO DO: pass Renaming in AVL tree and rename on the fly
636 rename_bt(B2,Renaming,RenamedB2),
637 same_texpr_body(B1,RenamedB2).
638
639 create_renaming([],[],[]).
640 create_renaming([ID|T1],[ID|T2],TR) :- !, create_renaming(T1,T2,TR).
641 create_renaming([ID1|T1],[ID2|T2],[rename(ID2,ID1)|TR]) :-
642 create_renaming(T1,T2,TR).
643
644
645 % check if two wrapped expressions are equal (modulo associated Info, e.g. source loc info)
646 % and checking inserted values for equality (sometimes storing a closure will convert small inner closures into AVL sets)
647 same_texpr_body(E1,E2) :- empty_avl(E),same_texpr_body(E1,E,E2).
648 same_texpr_body(b(E1,Type1,_),AVL,b(E2,Type2,_)) :-
649 unify_types_strict(Type1,Type2), % check in principle redundant
650 same_texpr2(E1,AVL,E2).
651
652 :- use_module(bsyntaxtree,[safe_syntaxelement_det/5, is_set_type/2,get_texpr_ids/2,
653 get_texpr_expr/2, get_negated_operator_expr/2]).
654 same_texpr2(value(V1),AVL,RHS) :- !,same_texpr_value2(RHS,AVL,V1).
655 same_texpr2(LHS,AVL,value(V2)) :- !,same_texpr_value2(LHS,AVL,V2).
656 same_texpr2(lazy_let_expr(ID,LHS,RHS),AVL,lazy_let_expr(ID2,LHS2,RHS2)) :- !,
657 same_texpr_body(LHS,AVL,LHS2),
658 avl_store(ID,AVL,ID2,NewAVL),
659 same_texpr_body(RHS,NewAVL,RHS2).
660 same_texpr2(lazy_let_pred(ID,LHS,RHS),AVL,lazy_let_pred(ID2,LHS2,RHS2)) :- !,
661 same_texpr_body(LHS,AVL,LHS2),
662 avl_store(ID,AVL,ID2,NewAVL),
663 same_texpr_body(RHS,NewAVL,RHS2).
664 same_texpr2(lazy_lookup(ID1), AVL,lazy_lookup(ID2)) :- !, avl_fetch(ID1,AVL,ID2).
665 same_texpr2(E1,AVL,E2) :- % Should we only enable this for same_closure_body_with_parameter_renaming?
666 quantifier_construct(E1,Functor,TParas1,Body1),
667 quantifier_construct(E2,Functor,TParas2,Body2),
668 !,
669 same_quantified_expression(TParas1,Body1,AVL,TParas2,Body2).
670 same_texpr2(E1,AVL,E2) :-
671 functor(E1,F,Arity),
672 functor(E2,F,Arity),!,
673 safe_syntaxelement_det(E1,Subs1,_Names1,_List1,Constant1),
674 safe_syntaxelement_det(E2,Subs2,_Names2,_List2,Constant2),
675 Constant2==Constant1,
676 same_sub_expressions(Subs1,AVL,Subs2).
677 same_texpr2(E1,AVL,E2) :- same_texpr_with_rewrite(E1,AVL,E2),!.
678 same_texpr2(E1,AVL,E2) :- same_texpr_with_rewrite(E2,AVL,E1).
679 %same_texpr2(E1,_,E2) :-
680 % functor(E1,F1,Arity1),
681 % functor(E2,F2,Arity2), print(not_eq(F1,Arity1,F2,Arity2)),nl, print(E1),nl, print(E2),nl,nl,fail.
682 % some differences: assertion_expression/3 and function/2, ...
683
684 % some rewrite rules from ast_cleanup; but we cannot replicate all rules here
685 same_texpr_with_rewrite(negation(TE1),AVL,E2) :-
686 get_negated_operator_expr(b(E2,pred,[]),NegE2),!,
687 get_texpr_expr(TE1,E1),
688 same_texpr2(E1,AVL,NegE2).
689 same_texpr_with_rewrite(member(X1,b(value(Set1),_,_)),AVL,equal(X2,b(El2,_,_))) :-
690 singleton_set(Set1,El1), !,
691 % X : {El} <===> X = El ; required for JSON trace replay of test 1491
692 same_texpr_body(X1,X2),
693 same_texpr_value2(El2,AVL,El1).
694 same_texpr_with_rewrite(not_member(X1,b(value(Set1),_,_)),AVL,not_equal(X2,b(El2,_,_))) :-
695 singleton_set(Set1,El1), !,
696 % X /: {El} <===> X /= El ; required for JSON trace replay of test 1491
697 same_texpr_body(X1,X2),
698 same_texpr_value2(El2,AVL,El1).
699
700 % constructs with local quantified parameters:
701 quantifier_construct(comprehension_set(TParas,Body),comprehension_set,TParas,Body).
702 quantifier_construct(exists(TParas,Body),exists,TParas,Body).
703 quantifier_construct(forall(TParas,LHS,RHS),forall,TParas,Body) :-
704 Body = b(implication(LHS,RHS),pred,[]).
705 % TODO?: SIGMA, PI, UNION, INTER
706
707 :- use_module(bsyntaxtree,[split_names_and_types/3]).
708 same_quantified_expression(TParas1,Body1,AVL,TParas2,Body2) :-
709 split_names_and_types(TParas1,P1,T1),
710 split_names_and_types(TParas2,P2,T2),
711 same_types(T1,T2),
712 create_renaming(P1,P2,Renaming),
713 rename_bt(Body2,Renaming,RenamedB2), % TODO: store renaming in AVL and lookup on the fly
714 same_texpr_body(Body1,AVL,RenamedB2).
715
716 same_texpr_value2(E2,_,V2) :- var(V2),!,V2==E2.
717 same_texpr_value2(interval(Min,Max),_,avl_set(A)) :- !, % occurs in JSON trace replay for test 268
718 avl_equal_to_interval(A,Min,Max). % TODO: also compare the other way around above; only apply if Card not too large?
719 same_texpr_value2(value(V2),_,V1) :- !,
720 same_value_inside_closure(V1,V2).
721 %(same_value_inside_closure(V1,V2) -> true ; print(not_eq_vals(V1,V2)),nl,fail).
722 same_texpr_value2(comprehension_set(Paras,B2),AVL,closure(P,_,B1)) :- !,
723 get_texpr_ids(Paras,P),!,
724 same_texpr_body(B1,AVL,B2).
725 same_texpr_value2(cartesian_product(TB1,TB2),AVL,V1) :-
726 decompose_value_into_cartesian_product(V1,A1,A2), !,
727 %print(cart(A1,A2)),nl,
728 get_texpr_expr(TB1,B1),
729 same_texpr_value2(B1,AVL,A1),
730 get_texpr_expr(TB2,B2),
731 same_texpr_value2(B2,AVL,A2).
732 same_texpr_value2(StaticExpr,_,int(Nr)) :- number(Nr),
733 b_ast_cleanup:pre_compute_static_int_expression(StaticExpr,Nr),!.
734 % TO DO: maybe also check if both sides can be evaluated
735 % TO DO: move pre_compute_static_int_expression to another module
736 same_texpr_value2(E2,AVL,V1) :- rewrite_value(V1,E2,NewE1),!,
737 same_texpr2(NewE1,AVL,E2).
738 %same_texpr_value2(E1,_,E2) :-
739 % functor(E1,F1,Arity1),
740 % functor(E2,F2,Arity2), print(not_eq_val(F1,Arity1,F2,Arity2)),nl, fail,print(E1),nl, print(E2),nl,nl,fail.
741
742 decompose_value_into_cartesian_product(avl_set(A),A1,A2) :- !,
743 decompose_avl_set_into_cartesian_product_wf(A,A1,A2,no_wf_available).
744 decompose_value_into_cartesian_product(Closure,A1,A2) :- is_cartesian_product_closure(Closure,A1,A2).
745
746
747 % rewrite values back to AST nodes
748 rewrite_value(value(V),OtherVal,New) :- nonvar(V),
749 rewrite_value_aux(V,OtherVal,New).
750 %rewrite_value(function(Lambda,Argument),assertion_expression(_,_,_),assertion_expression(Cond,Msg,Expr)) :- b_ast_cleanup:rewrite_function_application(Lambda,Argument,[],assertion_expression(Cond,Msg,Expr)).
751 rewrite_value_aux(closure(P,T,B),_,Set) :-
752 is_member_closure(P,T,B,_,Set). % TO DO: ensure that ast_cleanup does not generate useless member closures ?
753 rewrite_value_aux(global_set(GS),_,AST) :-
754 rewrite_glob_set(GS,AST).
755 rewrite_value_aux(avl_set(A),interval(_,_),interval(TLow,TUp)) :-
756 avl_equal_to_interval(A,Low,Up),
757 TLow = b(integer(Low),integer,[]), TUp = b(integer(Up),integer,[]).
758 rewrite_value_aux(int(A),integer(_),integer(A)) :- number(A).
759 rewrite_value_aux(pred_true,_,boolean_true).
760 rewrite_value_aux(pred_false,_,boolean_false).
761 rewrite_value_aux(string(A),integer(_),string(A)) :- % value(string(A)) rewritten to AST node string(A)
762 atom(A).
763
764
765 rewrite_glob_set('REAL',real_set).
766 rewrite_glob_set('FLOAT',float_set).
767 rewrite_glob_set('STRING',string_set).
768 rewrite_glob_set(I,integer_set(I)) :-
769 kernel_objects:integer_global_set(I).
770
771 allow_expansion(avl_set(_),closure(P,T,B)) :-
772 is_small_specific_custom_set(closure(P,T,B),100).
773 allow_expansion(closure(P,T,B),avl_set(_)) :-
774 is_small_specific_custom_set(closure(P,T,B),100).
775
776 same_sub_expressions([],_,[]).
777 same_sub_expressions([H1|T1],AVL,[H2|T2]) :-
778 same_texpr_body(H1,AVL,H2),
779 same_sub_expressions(T1,AVL,T2).
780
781 same_value_inside_closure(V1,V2) :- var(V1),!, V1==V2.
782 same_value_inside_closure(_,V2) :- var(V2),!,fail.
783 same_value_inside_closure(rec(Fields1),rec(Fields2)) :- !,
784 % sets of records come in this form: struct(b(value(rec(FIELDS)),record(_),_))
785 same_fields_inside_closure(Fields1,Fields2).
786 same_value_inside_closure(V1,V2) :-
787 % we could attempt this only if the outer closure was large/infinite ??
788 is_custom_explicit_set(V1), is_custom_explicit_set(V2),
789 !,
790 (allow_expansion(V1,V2) -> EXP=allow_expansion ; EXP = no_expansion),
791 equal_explicit_sets4(V1,V2,EXP,no_wf_available). % usually only sets compiled differently inside closures
792 same_value_inside_closure([H1|T1],avl_set(A2)) :- !, % relevant for JSON trace replay for test 1263
793 try_convert_to_avl([H1|T1],V1), V1=avl_set(A1),
794 equal_avl_tree(A1,A2).
795 same_value_inside_closure(avl_set(A2),[H1|T1]) :- !,
796 try_convert_to_avl([H1|T1],V1), V1=avl_set(A1),
797 equal_avl_tree(A1,A2).
798 same_value_inside_closure(V1,V2) :- V1==V2.
799
800 same_fields_inside_closure(V1,V2) :- var(V1),!, V1==V2.
801 same_fields_inside_closure(_,V2) :- var(V2),!,fail.
802 same_fields_inside_closure([],[]).
803 same_fields_inside_closure([field(Name,V1)|T1],[field(Name,V2)|T2]) :-
804 same_value_inside_closure(V1,V2),
805 same_fields_inside_closure(T1,T2).
806
807 /*
808 same_texpr_body_debug(H1,H2) :-
809 (same_texpr_body(H1,H2) -> true
810 ; print('FAIL: '),nl,
811 translate:print_bexpr(H1),nl, translate:print_bexpr(H2),nl, print(H1),nl, print(H2),nl, fail). */
812
813 %test(Y2,Z2) :- empty_avl(X), avl_store(1,X,2,Y), avl_store(2,X,3,Z),
814 % avl_store(2,Y,3,Y2), avl_store(1,Z,2,Z2), equal_avl_tree(Y2,Z2).
815
816 %equal_avl_tree(A,B) :- avl_min(A,Min), avl_min(B,Min), cmp(Min,A,B).
817 %cmp(El,A,B) :-
818 % (avl_next(El,A,Nxt) -> (avl_next(El,B,Nxt), cmp(Nxt,A,B))
819 % ; \+ avl_next(El,B,Nxt) ).
820
821 % The following is faster than using avl_next
822 equal_avl_tree(A,B) :-
823 % statistics(walltime,[WT1,_]),if(equal_avl_tree2(A,B),true,(statistics(walltime,[_,W]),print(wall(W)),nl)).
824 %equal_avl_tree2(A,B) :-
825 avl_min(A,Min),
826 !,
827 avl_min(B,Min),
828 avl_max(A,Max), avl_max(B,Max),
829 % maybe also check avl_height +/- factor of 1.4405 (page 460, Knuth 3) ? but it seems this would trigger only extremely rarely
830 %avl_height(A,H1), avl_height(A,H2), log(check(Min,Max,H1,H2)),
831 avl_domain(A,L), avl_domain(B,L).
832 equal_avl_tree(empty,_) :- !, format(user_error,'*** Warning: empty AVL tree in equal_avl_tree~n',[]).
833 equal_avl_tree(A,B) :- add_internal_error('Illegal AVL tree: ',equal_avl_tree(A,B)),fail.
834
835 /* a predicate to check equality of two custom explicit sets */
836
837 % TO DO: deal with second set being a variable with kernel_cardinality_attr attribute
838 :- block not_equal_explicit_sets_wf(-,?,?), not_equal_explicit_sets_wf(?,-,?).
839 not_equal_explicit_sets_wf(global_set(X),global_set(Y),_) :- !,dif(X,Y).
840 not_equal_explicit_sets_wf(global_set(B),avl_set(A),WF) :- !,
841 \+ equal_explicit_sets4(avl_set(A),global_set(B),allow_expansion,WF).
842 not_equal_explicit_sets_wf(freetype(X),freetype(Y),_) :- !,dif(X,Y).
843 not_equal_explicit_sets_wf(avl_set(A),avl_set(B),_) :- !,
844 \+ equal_avl_tree(A,B).
845 %not_equal_explicit_sets_wf(X,Y,_) :- X==Y,!,fail.
846 not_equal_explicit_sets_wf(avl_set(A),I2,WF) :- is_interval_closure_or_integerset(I2,L2,U2),!, % also covers I2=global_set(...)
847 avl_not_equal_to_interval(A,L2,U2,WF).
848 not_equal_explicit_sets_wf(avl_set(A),global_set(B),WF) :- !,
849 \+ equal_explicit_sets4(avl_set(A),global_set(B),allow_expansion,WF).
850 not_equal_explicit_sets_wf(avl_set(A),CPB,WF) :-
851 is_cartesian_product_closure(CPB,B1,B2),!,
852 if(decompose_avl_set_into_cartesian_product_wf(A,A1,A2,WF), % should not produce pending co-routines, but better safe
853 kernel_objects:not_equal_object_wf((A1,A2),(B1,B2),WF),
854 true % no cartesian product can be equal to this avl_set
855 ).
856 not_equal_explicit_sets_wf(closure(P,T,B),avl_set(A),WF) :- !,
857 not_equal_explicit_sets_wf(avl_set(A),closure(P,T,B),WF).
858 not_equal_explicit_sets_wf(I1,I2,_) :- is_interval_closure_or_integerset(I1,L1,U1,Finite1),
859 is_interval_closure_or_integerset(I2,L2,U2,Finite2), !,
860 dif((Finite1,L1,U1),(Finite2,L2,U2)). % maybe we should call not_equal_objects on integers (not on inf values)?
861 not_equal_explicit_sets_wf(CPA,CPB,WF) :-
862 ? is_cartesian_product_closure(CPA,A1,A2), is_cartesian_product_closure(CPB,B1,B2),!,
863 not_equal_cartesian_product_wf(A1,A2,B1,B2,WF).
864 not_equal_explicit_sets_wf(S1,S2,WF) :-
865 is_not_member_value_closure_or_integerset(S1,TYPE,MS1),
866 is_not_member_value_closure_or_integerset(S2,TYPE,MS2),!,
867 kernel_objects:not_equal_object_wf(MS1,MS2,WF).
868 not_equal_explicit_sets_wf(closure(P,T,B),closure(P,T,B2),_) :-
869 same_texpr_body(B,B2),!,fail.
870 % TO DO: maybe support interval & avl_set comparison
871 not_equal_explicit_sets_wf(Set1,Set2,WF) :-
872 card_for_specific_custom_set(Set1,Card1,Code1), card_for_specific_custom_set(Set2,Card2,Code2),
873 call(Code1), call(Code2),!,
874 ? not_equal_expansions(Card1,Card2,Set1,Set2,WF).
875 ?not_equal_explicit_sets_wf(Set1,Set2,WF) :- not_equal_expansions(0,0,Set1,Set2,WF).
876
877
878 :- block not_equal_expansions(-,?,?,?,?), not_equal_expansions(?,-,?,?,?).
879 not_equal_expansions(F1,F2,_,_,_) :- F1 \= F2,!. % sets guaranteed to be different
880 not_equal_expansions(F,F,Set1,Set2,WF) :-
881 ? get_identity_as_equivalence(F,Set1,Set2,EQUIV),
882 !, %write(not),nl,translate:print_bexpr(EQUIV),nl,
883 copy_wf_start(WF,equal_expansions,CWF),
884 b_not_test_boolean_expression(EQUIV,[],[],CWF),
885 ? copy_wf_finish(WF,CWF).
886 not_equal_expansions(F,F,Set1,Set2,WF) :-
887 % only expand if both sets have same cardinality
888 expand_custom_set_wf(Set1,E1,not_equal_expansions1,WF),
889 expand_custom_set_wf(Set2,E2,not_equal_expansions2,WF),
890 dif(E1,E2). /* TO DO: ensure that ordering and normalization is same for all representations ! */
891
892
893
894
895 :- use_module(b_global_sets,[b_empty_global_set/1, b_non_empty_global_set/1, b_global_set_cardinality/2]).
896 is_empty_explicit_set_wf(closure(P,T,B),WF) :- !,
897 is_empty_closure_wf(P,T,B,WF).
898 is_empty_explicit_set_wf(S,_WF) :- is_empty_explicit_set(S).
899
900 % with WF we can delay computing Card; see test 1272 / card({x|x:1..10 & x*x<i}) = 0 & i>1
901 % TO DO: ideally we could just write this: is_empty_closure_wf(P,T,B,WF) :- closure_cardinality(P,T,B,0,WF). ; but empty_set / not_exists optimisation not triggered in closure_cardinality (yet); would avoid duplicate code
902 is_empty_closure_wf(P,T,B,WF) :-
903 is_lambda_value_domain_closure(P,T,B, DomainValue,_Expr),!,
904 kernel_objects:empty_set_wf(DomainValue,WF).
905 is_empty_closure_wf(P,T,B,WF) :- is_cartesian_product_closure_aux(P,T,B,A1,A2),!,
906 very_approximate_cardinality(A1,C1,WF),
907 very_approximate_cardinality(A2,C2,WF),
908 blocking_safe_mul(C1,C2,0).
909 is_empty_closure_wf(P,T,B,_WF) :-
910 card_for_specific_closure2(P,T,B,CC,Code),
911 !,
912 call(Code),CC=0.
913 is_empty_closure_wf(P,T,Body,WF) :-
914 WF \== no_wf_available, % only do this if we have a WF store; see comments for closure_cardinality ; code relevant for test 1272; card({x|x:1..10 & x*x<i}) = 0 & i>1
915 \+ ground_bexpr(Body), % otherwise better to use not_test_exists below (e.g., Bosch v6 Codespeed benchmark)
916 b_interpreter_check:reify_closure_with_small_cardinality(P,T,Body, WF, ReifiedList),
917 !,
918 domain(ReifiedList,0,1),
919 sum(ReifiedList,'#=',0).
920 is_empty_closure_wf(P,T,B,WF) :-
921 ? get_recursive_identifier_of_closure_body(B,TRID),!,
922 def_get_texpr_id(TRID,RID),
923 gen_typed_ids(P,T,TypedParas),
924 % now add Recursive ID's value to local state:
925 b_interpreter:b_not_test_exists(TypedParas,B,[used_ids([RID])],[bind(RID,closure(P,T,B))],[],no_compile,WF).
926 is_empty_closure_wf(P,T,B,WF) :- !, % try and check that not(#(P).(B)); i.e., there is no solution for the Body B; solves tests 1542, detecting that {x|x>100 & x mod 102 = 2} = {} is false
927 gen_typed_ids(P,T,TypedParas),
928 b_interpreter:b_not_test_exists(TypedParas,B,[used_ids([])],[],[],no_compile,WF). % used_ids are empty, as all variables already compiled into values
929
930 % very_approximate_cardinality: only required to return 0 for empty set, and number or inf for non-empty set, tested in 1893
931 :- block very_approximate_cardinality(-,?,?).
932 very_approximate_cardinality(avl_set(A),C,_) :- !, (A=empty -> print(empty_avl),nl,C=0 ; C=1).
933 very_approximate_cardinality([],C,_) :- !, C=0.
934 very_approximate_cardinality([_|_],C,_) :- !, C=1.
935 very_approximate_cardinality(Set,C,WF) :- kernel_objects:cardinality_as_int_wf(Set,int(C),WF).
936 % TO DO: maybe call is_empty_closure or similar for closures
937
938 gen_typed_ids([],[],R) :- !, R=[].
939 gen_typed_ids([ID|IT],[Type|TT],[b(identifier(ID),Type,[])|TTT]) :- !,
940 % TO DO: add Info field from outer set comprehension
941 gen_typed_ids(IT,TT,TTT).
942 gen_typed_ids(I,T,TI) :- add_internal_error('Call failed: ',gen_typed_ids(I,T,TI)),fail.
943
944 % version with WF can also deal with closures via exists !
945 is_empty_explicit_set(global_set(GS)) :- !, b_empty_global_set(GS).
946 is_empty_explicit_set(freetype(ID)) :- !, is_empty_freetype(ID).
947 is_empty_explicit_set(avl_set(A)) :- !,
948 (var(A) -> add_warning(is_empty_explicit_set,'Variable avl_set')
949 ; empty_avl(A), add_warning(is_empty_explicit_set,'Empty avl_set')
950 ).
951 is_empty_explicit_set(C) :- card_for_specific_closure(C,CC,Code),!,call(Code),CC=0.
952 is_empty_explicit_set(ES) :- expand_custom_set(ES,[],is_empty_explicit_set).
953
954
955 is_non_empty_explicit_set(CS) :- is_non_empty_explicit_set_wf(CS,no_wf_available).
956
957 is_non_empty_explicit_set_wf(global_set(GS),_WF) :- !, b_non_empty_global_set(GS).
958 is_non_empty_explicit_set_wf(freetype(ID),_WF) :- !, is_non_empty_freetype(ID).
959 is_non_empty_explicit_set_wf(avl_set(A),_WF) :- !,
960 (empty_avl(A) -> print('### Warning: empty avl_set (2)'),nl,fail
961 ; true).
962 ?is_non_empty_explicit_set_wf(closure(P,T,B),WF) :- !, is_non_empty_closure_wf(P,T,B,WF).
963 %is_non_empty_explicit_set_wf(ES,_WF) :- expand_custom_set(ES,[_|_],is_non_empty_explicit_set).
964
965
966 % TO DO: this code is a bit redundant with is_empty_closure_wf
967 is_non_empty_closure_wf(P,T,B,WF) :-
968 is_lambda_value_domain_closure(P,T,B, DomainValue,_Expr),!,
969 ? kernel_objects:not_empty_set_wf(DomainValue,WF).
970 is_non_empty_closure_wf(P,T,B,WF) :- is_cartesian_product_closure_aux(P,T,B,A1,A2),!,
971 very_approximate_cardinality(A1,C1,WF),
972 very_approximate_cardinality(A2,C2,WF),
973 blocking_safe_mul(C1,C2,CC),gt0(CC).
974 is_non_empty_closure_wf(P,T,B,_WF) :-
975 card_for_specific_closure2(P,T,B,CC,Code),!,call(Code),gt0(CC).
976 % TO DO: reify_closure_with_small_cardinality
977 is_non_empty_closure_wf(P,T,B,WF) :- WF \== no_wf_available,
978 ? get_recursive_identifier_of_closure_body(B,TRID),!,
979 def_get_texpr_id(TRID,RID),
980 gen_typed_ids(P,T,TypedParas),
981 % now add Recursive ID's value to local state:
982 b_interpreter:b_test_exists(TypedParas,B,[used_ids([RID])],[bind(RID,closure(P,T,B))],[],WF).
983 is_non_empty_closure_wf(P,T,B,WF) :- WF \== no_wf_available,
984 % otherwise enumeration of test_exists will behave strangely; leading to enumeration warnings,... [TO DO: ensure we always have a WF or fix this below ?]
985 % try and check that not(#(P).(B)); i.e., there is no solution for the Body B; solves tests 1542; test 1146 also triggers this code
986 (debug_mode(off) -> true ; print(non_empty_closure_test(P)),nl, translate:print_bexpr(B),nl),
987 gen_typed_ids(P,T,TypedParas),
988 !,
989 ? b_interpreter:b_test_exists(TypedParas,B,[used_ids([])],[],[],WF). % used_ids are empty, as all variables already compiled into values
990 % some rules for set_subtraction, ... closures ?? if left part infinite and right part finite it must be infinite
991 is_non_empty_closure_wf(P,T,B,WF) :-
992 expand_custom_set_wf(closure(P,T,B),[_|_],is_non_empty_closure_wf,WF).
993
994
995 % TO DO: expand cart / reify and use for pf_test
996 test_empty_closure_wf(P,T,B,Res,WF) :-
997 is_lambda_value_domain_closure(P,T,B, DomainValue,_Expr),!,
998 kernel_equality:empty_set_test_wf(DomainValue,Res,WF).
999 %test_empty_closure_wf(P,T,B,WF) :- is_cartesian_product_closure_aux(P,T,B,A1,A2),!,
1000 test_empty_closure_wf(P,T,B,Res,_WF) :-
1001 card_for_specific_closure2(P,T,B,CC,Code),!,call(Code),leq0(CC,Res).
1002 test_empty_closure_wf(P,T,B,Res,WF) :-
1003 \+ is_memoization_closure(P,T,B,_MemoID),
1004 preferences:preference(use_closure_expansion_memoization,false),
1005 !,
1006 bexpr_variables(B,ClosureWaitVars),
1007 % this does not perform a few optimisations of expand_normal closure:
1008 % memoization, stored_memo_expansion, is_closure1_value_closure, is_lambda_closure
1009 % print(test_empty_closure_wf),nl, translate:print_bexpr(B),nl,
1010 when((ground(ClosureWaitVars) ; nonvar(Res)),
1011 test_empty_closure_wf2(P,T,B,Res,WF)).
1012 test_empty_closure_wf(P,T,B,Res,WF) :- % print(expand_test(P)),nl,
1013 % was expand_custom_set_wf(closure(P,T,B),ExpES,test_empty_closure_wf,WF), in turn calls expand_closure_to_list
1014 expand_normal_closure(P,T,B,ExpES,_CDone,check(test_empty_closure_wf),WF),
1015 kernel_equality:empty_set_test_wf(ExpES,Res,WF).
1016 % /*@symbolic */ {x|x:1..100000000 & x mod 22=1} = x & (x={} <=> B=TRUE)
1017
1018 test_empty_closure_wf2(P,T,B,Res,WF) :-
1019 Res == pred_true,!,
1020 is_empty_closure_wf(P,T,B,WF).
1021 test_empty_closure_wf2(P,T,B,Res,WF) :- Res == pred_false,!,
1022 is_non_empty_closure_wf(P,T,B,WF).
1023 test_empty_closure_wf2(P,T,B,Res,WF) :-
1024 (is_empty_closure_now(P,T,B,WF) % we need to force expansion here to be able to use local cut ->
1025 % expand_normal_closure would now also expand the closure;
1026 -> Res=pred_true
1027 ; Res=pred_false).
1028
1029 % check if closure now; ground everything except WFE
1030 is_empty_closure_now(P,T,B,OuterWF) :-
1031 create_inner_wait_flags(OuterWF,is_empty_closure_now,WF),
1032 debug_opt_push_wait_flag_call_stack_info(WF,
1033 external_call('Check if empty set',[closure(P,T,B)],unknown),WF2),
1034 is_empty_closure_wf(P,T,B,WF2),
1035 ground_inner_wait_flags(WF2). % does not ground WFE in case WD errors are pending
1036
1037 :- block leq0(-,?).
1038 leq0(inf,Res) :- !, Res=pred_false.
1039 leq0(inf_overflow,Res) :- !, Res=pred_false.
1040 leq0(CC,Res) :- (CC>0 -> Res=pred_false ; Res=pred_true).
1041
1042 test_empty_explicit_set_wf(V,Res,_) :- var(V),!,
1043 add_internal_error('Illegal call: ',test_empty_explicit_set(V,Res,_)),fail.
1044 test_empty_explicit_set_wf(global_set(GS),Res,_WF) :- !,
1045 (b_empty_global_set(GS) -> Res=pred_true ; Res=pred_false).
1046 test_empty_explicit_set_wf(freetype(ID),Res,_WF) :- !, test_empty_freetype(ID,Res).
1047 test_empty_explicit_set_wf(avl_set(A),Res,_WF) :- !,
1048 (var(A) -> add_warning(test_empty_explicit_set_wf,'Variable avl_set'), Res=pred_true
1049 ; empty_avl(A), add_warning(test_empty_explicit_set_wf,'Empty avl_set'), Res = pred_true
1050 ; Res=pred_false).
1051 test_empty_explicit_set_wf(closure(P,T,B),Res,WF) :- !,
1052 test_empty_closure_wf(P,T,B,Res,WF).
1053 test_empty_explicit_set_wf(ES,Res,WF) :-
1054 expand_custom_set(ES,ExpES,test_empty_explicit_set),
1055 kernel_equality:empty_set_test_wf(ExpES,Res,WF).
1056
1057 :- block gt0(-).
1058 gt0(CC) :- (CC=inf -> true ; CC=inf_overflow -> true ; CC>0).
1059
1060 % a version to compute cardinality for
1061 explicit_set_cardinality_for_wf(closure(P,T,B),Card) :-
1062 (is_symbolic_closure_or_symbolic_mode(P,T,B) ; \+ ground_bexpr(B)),
1063 !,
1064 Card = inf. % assume card is infinite for WF computation; it may be finite!
1065 %explicit_set_cardinality_for_wf(avl_set(AVL),Size) :- !, quick_avl_approximate_size(AVL,Size).
1066 explicit_set_cardinality_for_wf(CS,Card) :- card_for_specific_custom_set(CS,Card,Code),!,
1067 on_enumeration_warning(call(Code),Card=inf). % see test 1519 for relevance
1068 explicit_set_cardinality_for_wf(_,inf). % assume card is infinite for WF computation; it may be finite!
1069 % TO DO: maybe never expand closures here !? -> closure_cardinality can expand closure !!!!!!
1070 %explicit_set_cardinality_for_wf(CS,Card) :-
1071 % on_enumeration_warning(
1072 % explicit_set_cardinality(CS,Card),
1073 % (debug_println(assuming_inf_card_for_wf), % see test 1519 for relevance
1074 % Card = inf)). % assume card is infinite for WF computation; it may be finite!
1075
1076 explicit_set_cardinality(CS,Card) :-
1077 % init_wait_flags(WF,[explicit_set_cardinality]), % there are a few checks for no_wf_available below
1078 explicit_set_cardinality_wf(CS,Card,no_wf_available).
1079 % ground_wait_flags(WF).
1080
1081 explicit_set_cardinality_wf(global_set(GS),Card,_) :- !,b_global_set_cardinality(GS,Card).
1082 explicit_set_cardinality_wf(freetype(GS),Card,_WF) :- !, freetype_cardinality(GS,Card).
1083 explicit_set_cardinality_wf(avl_set(S),Card,_WF) :- !,avl_size(S,Card).
1084 ?explicit_set_cardinality_wf(closure(P,T,B),Card,WF) :- closure_cardinality(P,T,B,Card,WF).
1085
1086 :- use_module(performance_messages).
1087 closure_cardinality(P,T,B,Card,WF) :-
1088 is_lambda_value_domain_closure(P,T,B, DomainValue,_Expr),!,
1089 kernel_objects:cardinality_as_int_wf(DomainValue,int(Card),WF). % always compute it; card_for_specific_closure will only compute it if it can be done efficiently
1090 closure_cardinality(P,T,B,Card,WF) :- is_cartesian_product_closure_aux(P,T,B,A1,A2),!,
1091 kernel_objects:cardinality_as_int_wf(A1,int(C1),WF),
1092 kernel_objects:cardinality_as_int_wf(A2,int(C2),WF),
1093 blocking_safe_mul(C1,C2,Card).
1094 % TO DO: card_for_specific_closure2 calls is_lambda_value_domain_closure and is_cartesian_product_closure_aux again !
1095 closure_cardinality(P,T,B,Card,_WF) :-
1096 card_for_specific_closure2(P,T,B,CC,Code),
1097 !,
1098 call(Code),Card=CC.
1099 closure_cardinality(P,T,Body,Card,WF) :-
1100 (WF == no_wf_available -> CBody=Body
1101 ? ; b_compiler:b_compile(Body,P,[],[],CBody)
1102 ),
1103 % reify will work better if we used b_compiler:compile so that more sets can be detected as small
1104 closure_cardinality2(P,T,CBody,Card,WF).
1105 closure_cardinality2(P,T,Body,Card,WF) :-
1106 WF \== no_wf_available, % only do this if we have a WF store
1107 ? if(b_interpreter_check:reify_closure_with_small_cardinality(P,T,Body, WF, ReifiedList),
1108 true,
1109 (perfmessagecall(reify,reification_of_closure_for_card_failed(P),translate:print_bexpr(Body),Body),fail)),
1110 !,
1111 domain(ReifiedList,0,1),
1112 sum(ReifiedList,'#=',Card),
1113 % in this case we know card to be finite ! TO DO: ensure that check_finite propagates Card variable
1114 debug_println(9,reified_cardinality_sum(ReifiedList,Card)). % fd_dom(Card,Dom),print(dom(Card,Dom)),nl.
1115 % should we add a special check if Card=0 ? usually Card not instantiated at this point !
1116 %closure_cardinality(P,T,B,Card,WF) :- Card==0, %is_symbolic_closure(P,T,B),
1117 % !, is_empty_closure_wf(P,T,B,WF).
1118 closure_cardinality2(P,T,B,Card,WF) :-
1119 % TO DO: bexpr_variables(ClosureBody,ClosureWaitVars) and wait until those are bound; if Card = 0 -> empty_set; we can try to reifiy again
1120 expand_custom_set_wf(closure(P,T,B),Expansion,closure_cardinality,WF),
1121 my_length(Expansion,0,Card).
1122
1123 :- block my_length(-,?,?).
1124 my_length([],A,A).
1125 my_length([_|T],A,R) :- A1 is A+1, my_length(T,A1,R).
1126
1127 % compute domain and range for specific relations;
1128 % not the closure is total over the domain and surjective over the range
1129 % WARNING: this should never enumerate on its own, it is often called with
1130 % a cut straight after it; if some enumeration happens then only first solution
1131 % will be pursued (e.g., cond_assign_eq_obj)
1132 dom_range_for_specific_closure([],[],[],function(bijection),_WF).
1133 dom_range_for_specific_closure(closure(P,T,Pred),Domain,Range,Functionality,WF) :-
1134 dom_range_for_specific_closure2(P,T,Pred, Domain,Range,dom_and_range,Functionality,WF).
1135
1136 dom_range_for_specific_closure2(Par,Typ,Body, Domain,Range,Required,Functionality,WF) :-
1137 is_member_closure(Par,Typ,Body,TYPE,SET),
1138 dom_range_for_member_closure(SET,TYPE,Domain,Range,Required,Functionality,WF),!.
1139 dom_range_for_specific_closure2(Par,Typ,Body, DOMAIN,RANGE,_,Functionality,WF) :-
1140 is_cartesian_product_closure_aux(Par,Typ,Body,SET1,SET2),!,
1141 (singleton_set(SET2,_) % checks nonvar
1142 -> Functionality = function(total) % function if card(SET2)=1
1143 ; Functionality=relation),
1144 kernel_equality:empty_set_test_wf(SET1,EqRes1,WF),
1145 cond_assign_eq_obj_wf(EqRes1,RANGE,[],SET2,WF), % if SET1=[] then Range=[]
1146 kernel_equality:empty_set_test_wf(SET2,EqRes2,WF),
1147 cond_assign_eq_obj_wf(EqRes2,DOMAIN,[],SET1,WF). %if SET2=[] then Domain=[]
1148 dom_range_for_specific_closure2(Par,Typ,Body, DomainRange,DomainRange,_,function(bijection),_WF) :-
1149 is_id_closure_over(Par,Typ,Body,DomainRange,_).
1150
1151
1152 dom_range_for_member_closure(identity(b(value(SET1),ST1,_)),_SEQT,SET1,SET1,_,function(bijection),_) :-
1153 is_set_type(ST1,_). /* _SEQT=id(T1) */
1154 dom_range_for_member_closure(closure(V),_SEQT,Domain,Range,Required,Functionality,WF) :- % closure1 transitive closure
1155 % rx : A <-> B <=> closure1(rx) : A <-> B means we can simply remove closure1(.) wrapper
1156 V = b(value(VAL),_,_), nonvar(VAL),
1157 %write(peel_clos1_dom_range(Required)),nl, tools_printing:print_term_summary(closure(V)),nl,
1158 (VAL = closure(P,T,B)
1159 -> dom_range_for_specific_closure2(P,T,B,Domain,Range,Required,Functionality,WF)
1160 ; Functionality = relation, % we do not know if this is going to be a function
1161 (Required=domain_only -> true ; range_of_explicit_set_wf(VAL,Range,WF)),
1162 (Required=range_only -> true ; domain_of_explicit_set_wf(VAL,Domain,WF))
1163 ).
1164
1165 % not sure if we need this: memoized functions are infinite usually and range can never be computed anyway
1166 %dom_range_for_member_closure(Expr,_,Domain,Range,Func) :-
1167 % expand_memoize_stored_function_reference(Expr,ExpandedValue),
1168 % dom_range_for_specific_closure(ExpandedValue,Domain,Range,Func,no_wf_available).
1169
1170 :- block cond_assign_eq_obj_wf(-,?,?,?,?).
1171 %cond_assign_eq_obj_wf(PTF,R,A,B,_) :- var(PTF), add_error(cond_assign_eq_obj,'block declaration bug warning: ',cond_assign_eq_obj(PTF,R,A,B)),fail. % comment in to detect if affected by block declaration bug
1172 cond_assign_eq_obj_wf(pred_true,Res,A,_,WF) :- equal_object_wf(Res,A,cond_assign_eq_obj_wf_1,WF).
1173 cond_assign_eq_obj_wf(pred_false,Res,_,B,WF) :- equal_object_wf(Res,B,cond_assign_eq_obj_wf_2,WF).
1174
1175 is_cartesian_product_closure(closure(Par,Typ,Body),SET1,SET2) :-
1176 ? is_cartesian_product_closure_aux(Par,Typ,Body,SET1,SET2).
1177 is_cartesian_product_closure_aux(Par,Types,b(truth,pred,Info),SET1,SET2) :- Par=[_,_|_],!,
1178 append(LPar,[RParID],Par), append(LTypes,[RType],Types),
1179 construct_closure_if_necessary(LPar,LTypes,b(truth,pred,Info),SET1),
1180 construct_closure_if_necessary([RParID],[RType],b(truth,pred,Info),SET2).
1181 is_cartesian_product_closure_aux(Par,Types,Body,SET1,SET2) :- Par=[_,_|_],!,
1182 append(LPar,[RParID],Par), append(LTypes,[RType],Types),!,
1183 split_conjunct(Body,[RParID], RConjL, LPar, LConjL),
1184 bsyntaxtree:conjunct_predicates(RConjL,RConj), bsyntaxtree:conjunct_predicates(LConjL,LConj),
1185 construct_closure_if_necessary(LPar,LTypes,LConj,SET1),
1186 construct_closure_if_necessary([RParID],[RType],RConj,SET2).
1187 is_cartesian_product_closure_aux(Par,Typ,Body,SET1,SET2) :-
1188 SET = cartesian_product(b(value(SET1),ST1,_), b(value(SET2),ST2,_)),
1189 is_member_closure(Par,Typ,Body,couple(T1m,T2m),SET),
1190 is_set_type(ST1,T1),unify_types_strict(T1,T1m),
1191 is_set_type(ST2,T2),unify_types_strict(T2,T2m),!.
1192 %is_cartesian_product_closure_aux([ID1,ID2],[T1,T2],FBody,SET1,SET2) :- % is this not redundant wrt split ??
1193 % % a closure of the form {ID1,ID2|ID1 : SET1 & ID2 : SET2} ;
1194 % % can get generated when computing domain symbolically of lambda abstraction
1195 % FBody = b(Body,pred,_),
1196 % is_cartesian_product_body(Body,ID1,ID2,T1,T2,SET1,SET2). % ,print(cart_res(SET1,SET2)),nl.
1197
1198 % try and split conjunct into two disjoint parts (for detecting cartesian products)
1199 % on the specified variables
1200 % fails if it cannot be done
1201 split_conjunct(b(PRED,pred,Info),Vars1,C1,Vars2,C2) :-
1202 split_conjunct_aux(PRED,Info,Vars1,C1,Vars2,C2).
1203 split_conjunct_aux(truth,_Info,_Vars1,C1,_Vars2,C2) :- !,C1=[],C2=[].
1204 split_conjunct_aux(conjunct(A,B),_Info,Vars1,C1,Vars2,C2) :- !, % TO DO: use DCG
1205 split_conjunct(B,Vars1,CB1,Vars2,CB2), !, % Note: conjunct_predicates will create inner conjunct in A and atomic Expression in B
1206 split_conjunct(A,Vars1,CA1,Vars2,CA2),!,
1207 append(CA1,CB1,C1), append(CA2,CB2,C2).
1208 split_conjunct_aux(E,Info,Vars1,C1,_Vars2,C2) :- unique_id_comparison(E,ID),!,
1209 (member(ID,Vars1) -> C1=[b(E,pred,Info)], C2=[] ; C1=[], C2=[b(E,pred,Info)]).
1210
1211 unique_id_comparison(less(b(L,_,_),b(R,_,_)), ID) :- unique_id_comparison_aux(L,R,ID).
1212 unique_id_comparison(less_equal(b(L,_,_),b(R,_,_)), ID) :- unique_id_comparison_aux(L,R,ID).
1213 unique_id_comparison(greater(b(L,_,_),b(R,_,_)), ID) :- unique_id_comparison_aux(L,R,ID).
1214 unique_id_comparison(greater_equal(b(L,_,_),b(R,_,_)), ID) :- unique_id_comparison_aux(L,R,ID).
1215 unique_id_comparison(member(b(identifier(ID),_,_),b(V,_,_)), ID) :- explicit_value(V).
1216 unique_id_comparison(subset(b(identifier(ID),_,_),b(V,_,_)), ID) :- explicit_value(V).
1217 unique_id_comparison(equal(b(L,_,_),b(R,_,_)), ID) :- unique_id_comparison_aux(L,R,ID). % means we also detect something like %x.(x : INTEGER|0) as cartesian product
1218 % what about not_equal
1219
1220 unique_id_comparison_aux(identifier(ID),V,ID) :- !,explicit_value(V).
1221 unique_id_comparison_aux(V,identifier(ID),ID) :- explicit_value(V).
1222
1223 explicit_value(value(_)) :- !.
1224 explicit_value(integer(_)) :- !.
1225 explicit_value(unary_minus(TV)) :- !, explicit_tvalue(TV).
1226 explicit_value(interval(TV1,TV2)) :- !,
1227 explicit_tvalue(TV1), explicit_tvalue(TV2).
1228 %explicit_value(seq(B)) :- !, explicit_tvalue(B). % are encoded as values by b_compile
1229 %explicit_value(seq1(B)) :- !, explicit_tvalue(B).
1230 %explicit_value(iseq(B)) :- !, explicit_tvalue(B).
1231 %explicit_value(iseq1(B)) :- !, explicit_tvalue(B).
1232 %explicit_value(struct(B)) :- !, explicit_tvalue(B).
1233 %explicit_value(rec(Fields)) :- !, explicit_tfields(Fields).
1234 explicit_value(total_bijection(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B). % see test 1897 for cases below
1235 explicit_value(total_injection(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B).
1236 explicit_value(total_function(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B).
1237 explicit_value(total_surjection(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B).
1238 explicit_value(partial_function(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B).
1239 explicit_value(partial_injection(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B).
1240 explicit_value(partial_surjection(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B).
1241 explicit_value(relations(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B).
1242 explicit_value(total_relation(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B).
1243 explicit_value(surjection_relation(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B).
1244 explicit_value(total_surjection_relation(A,B)) :- !, explicit_tvalue(A),explicit_tvalue(B).
1245 explicit_value(real_set) :- !.
1246 explicit_value(string_set) :- !.
1247
1248 explicit_tvalue(b(B,_,_)) :- !, explicit_value(B).
1249
1250 %explicit_tfields(V) :- var(V),!,fail.
1251 %explicit_tfields([]).
1252 %explicit_tfields([field(N,V)|T]) :- ground(N),explicit_tvalue(V),explicit_tfields(T).
1253
1254 % conjunct_predicates([CA1,CB1],C1),
1255 % conjunct_predicates([CA2,CB2],C2).
1256
1257 /* *********
1258 is_cartesian_product_body(conjunct(A,B),ID1,ID2,_T1,_T2,SET1,SET2) :- !,
1259 member_pred_value(A,CID1,CSET1),
1260 member_pred_value(B,CID2,CSET2),
1261 (ID1=CID1,ID2=CID2,SET1=CSET1,SET2=CSET2 ; ID1=CID2,ID2=CID1,SET1=CSET2,SET2=CSET1).
1262 is_cartesian_product_body(A,ID1,ID2,T1,T2,SET1,SET2) :-
1263 member_pred_value2(A,AID,ASET),
1264 ( AID=ID1 -> SET1=ASET, construct_closure_if_necessary([ID2],[T2],b(truth,pred,[]),SET2)
1265 ; AID=ID2 -> SET2=ASET, construct_closure_if_necessary([ID1],[T1],b(truth,pred,[]),SET1)).
1266
1267 member_pred_value(b(B,pred,_), ID,VAL) :- print(member_pred_value2(B,ID,VAL)),nl,
1268 member_pred_value2(B,ID,VAL).
1269 member_pred_value2(member(b(identifier(ID),_CT1,_),b(value(VAL),_SCT1,_)), ID,VAL). %_SCT1 = set(CT1)
1270 */
1271
1272 % check if we have POW(SET1) or SET1<->SET2 (equiv. to POW(SET1*SET2))
1273 is_full_powerset_or_relations_or_struct_closure(closure(Par,Typ,Body),SUBSETS) :-
1274 %TYPE = set(T),
1275 is_member_closure(Par,Typ,Body,TYPE,SET),
1276 is_full_powset_aux(SET,TYPE,SUBSETS).
1277
1278 :- use_module(library(lists),[maplist/3, maplist/4]).
1279 is_full_powset_aux(pow_subset(b(value(SET1),set(T1),_)),set(T1),[SET1]).
1280 is_full_powset_aux(relations(S1,S2),set(couple(T1,T2)),[SET1,SET2]) :-
1281 S1 = b(value(SET1),set(T1),_), S2 = b(value(SET2),set(T2),_).
1282 is_full_powset_aux(struct(b(value(rec(FIELDS)),record(_),_)),record(_),FieldValueSets) :-
1283 maplist(get_field_val,FIELDS,FieldValueSets).
1284
1285 get_field_val(field(_,Val),Val).
1286
1287 %[field(duration,global_set('INTEGER')),field(rhythm,global_set('INTEGER')),field(slot,avl_set(...))]
1288
1289 is_powerset_closure(closure(Par,Typ,Body),PType,Subset) :-
1290 ? is_set_type(TYPE,T),
1291 is_member_closure(Par,Typ,Body,TYPE,SET),
1292 nonvar(SET),
1293 is_powset_aux(SET,PType,b(VS,set(T),_)) ,
1294 nonvar(VS), VS = value(Subset). %,print(powerset(Subset)),nl.
1295 is_powset_aux(pow_subset(A),pow,A).
1296 is_powset_aux(pow1_subset(A),pow1,A).
1297 is_powset_aux(fin_subset(A),fin,A).
1298 is_powset_aux(fin1_subset(A),fin1,A).
1299
1300 % group together closures which can be treated like cartesian products in the sense that:
1301 % Closure is empty if either Set1 or Set2 (could also be empty in other conditions though)
1302 % Closure is subset of other Closure if same Constructor and both sets are subsets
1303 /* is_cartesian_product_like_closure(Closure,Constructor,Set1,Set2) :-
1304 is_cartesian_product_closure(Closure,S11,S12),!,
1305 Constructor = cartesian_product,Set1=S11,Set2=S12.
1306 is_cartesian_product_like_closure(closure(Par,Typ,Body),Constructor,Set1,Set2) :-
1307 is_member_closure(Par,Typ,Body,TYPE,SET),
1308 is_cart_like_relation(SET,Constructor,b(value(Set1),set(_T1),_), b(value(Set1),set(_T2),_)).
1309 is_cart_like_relation(relations(A,B),relations,A,B).
1310 is_cart_like_relation(partial_function(A,B),partial_function,A,B).
1311 is_cart_like_relation(partial_injection(A,B),partial_injection,A,B). */
1312
1313 % (closure([_zzzz_unary],[set(couple(integer,string))],b(member(b(identifier(_zzzz_unary),set(couple(integer,string)),[]),b(relations(b(value(global_set(INTEGER)),set(integer),[]),b(value(global_set(STRING)),set(string),[])),set(set(couple(integer,string))),[])),pred,[])))
1314 % 1 1 Fail: custom_explicit_sets:is_powset_aux(relations(b(value(global_set('INTEGER')),set(integer),[]),b(value(global_set('STRING')),set(string),[])),couple(integer,string),_19584) ?
1315
1316 % card_for_specific_custom_set(+Set,-Cardinality,-CodeToComputeCardinality)
1317 % succeeds if card can be computed efficiently
1318 card_for_specific_custom_set(CS,C,Cd) :- var(CS),!,
1319 add_internal_error('Internal error: var ',card_for_specific_custom_set(CS,C,Cd)),fail.
1320 card_for_specific_custom_set(global_set(GS),Card,true) :- !, b_global_set_cardinality(GS,Card).
1321 card_for_specific_custom_set(freetype(Id),Card,true) :- !, freetype_cardinality(Id,Card).
1322 card_for_specific_custom_set(avl_set(S),Card,true) :- !,avl_size(S,Card).
1323 card_for_specific_custom_set(closure(P,T,B),Card,CodeToComputeCard) :-
1324 card_for_specific_closure3(_,P,T,B,Card,CodeToComputeCard).
1325
1326 card_for_specific_closure(closure(P,T,Pred),Card,CodeToComputeCard) :-
1327 card_for_specific_closure3(_ClosureKind,P,T,Pred,Card,CodeToComputeCard).
1328 card_for_specific_closure(closure(P,T,Pred),ClosureKind,Card,CodeToComputeCard) :-
1329 card_for_specific_closure3(ClosureKind,P,T,Pred,Card,CodeToComputeCard).
1330
1331 :- use_module(btypechecker,[couplise_list/2]).
1332 :- use_module(bsyntaxtree,[is_truth/1]).
1333 card_for_specific_closure2(Par,Typ,Body, Card,Code) :-
1334 card_for_specific_closure3(_ClosureKind,Par,Typ,Body, Card,Code).
1335
1336 % first argument for debugging purposes or filtering
1337 card_for_specific_closure3(special_closure,Par,Typ,Body, Card,Code) :-
1338 is_special_infinite_closure(Par,Typ,Body),!,Card=inf, Code=true.
1339 card_for_specific_closure3(truth_closure,_,Types,Body,Card,Code) :- is_truth(Body),!,
1340 % TO DO: also treat multiple parameters
1341 couplise_list(Types,Type),
1342 Code=kernel_objects:max_cardinality(Type,Card).
1343 card_for_specific_closure3(interval_closure,Par,Typ,Body, Card,Code) :-
1344 ? is_geq_leq_interval_closure(Par,Typ,Body,Low,Up), !,
1345 card_of_interval_inf(Low,Up,Card),
1346 Code=true. % should we return card_of_interval_inf as code ?
1347 % TO DO: deal with non-infinite not_member_closures, prj1, prj2, id, ...
1348 card_for_specific_closure3(lambda_closure,Par,Typ,Body, Card,Code) :-
1349 is_lambda_value_domain_closure(Par,Typ,Body, DomainValue,_Expr),!, nonvar(DomainValue),
1350 efficient_card_for_set(DomainValue,Card,Code).
1351 card_for_specific_closure3(cartesian_product,Par,Typ,Body, Card,Code) :-
1352 is_cartesian_product_closure_aux(Par,Typ,Body,A1,A2),!, nonvar(A1), nonvar(A2),
1353 efficient_card_for_set(A1,Card1,Code1),
1354 efficient_card_for_set(A2,Card2,Code2),
1355 Code = (Code1,Code2, custom_explicit_sets:blocking_safe_mul(Card1,Card2,Card)).
1356 card_for_specific_closure3(member_closure,Par,Typ,Body, Card,Code) :-
1357 is_member_closure(Par,Typ,Body,TYPE,SET),
1358 nonvar(SET),!,
1359 card_for_member_closure(SET,TYPE,Card,Code).
1360 % Note: _ExprInfo could have: contains_wd_condition,
1361 % but if lambda is well-defined we compute the correct card ; if not then card is not well-defined anyway
1362 % maybe we should check contains_wd_condition produce a warning msg ?
1363
1364 % inner values can sometimes be a list, e.g., [pred_true,pred_false] for BOOL
1365 efficient_card_for_set(VAR,_,_) :- var(VAR),!,fail.
1366 efficient_card_for_set([],Card,Code) :- !, Card=0,Code=true.
1367 efficient_card_for_set([_|T],Card,Code) :- known_length(T,1,C), !, Card = C, Code=true.
1368 efficient_card_for_set(CS,Card,Code) :- card_for_specific_custom_set(CS,Card,Code).
1369 known_length(X,_,_) :- var(X),!,fail.
1370 known_length([],A,A).
1371 known_length([_|T],A,R) :- A1 is A+1, known_length(T,A1,R).
1372 known_length(avl_set(S),Acc,Res) :- avl_size(S,Card),
1373 Res is Acc+Card.
1374 % TO DO: also support closures
1375
1376 card_for_member_closure(parallel_product(b(value(A1),ST1,_),b(value(A2),ST1,_)),_T,Card,CodeToComputeCard) :- !,
1377 nonvar(A1), nonvar(A2),
1378 efficient_card_for_set(A1,Card1,Code1),
1379 CodeToComputeCard = (Code1,Code2, custom_explicit_sets:blocking_safe_mul(Card1,Card2,Card)),
1380 % cardinality computed like for cartesian_product.
1381 efficient_card_for_set(A2,Card2,Code2).
1382 card_for_member_closure(seq(b(value(SET1),ST1,_)),_SEQT,Card,CodeToComputeCard) :- !, /* _SEQT=seq(T1) */
1383 is_set_type(ST1,_T1),
1384 CodeToComputeCard = custom_explicit_sets:seq_card(SET1,Card).
1385 card_for_member_closure(seq1(b(value(SET1),ST1,_)),_SEQT,Card,CodeToComputeCard) :- !, /* _SEQT=seq1(T1) */
1386 is_set_type(ST1,_T1),
1387 CodeToComputeCard = custom_explicit_sets:seq1_card(SET1,Card).
1388 card_for_member_closure(perm(b(value(SET1),ST1,_)),_SEQT,Card,CodeToComputeCard) :- !, /* _SEQT=perm(T1) */
1389 is_set_type(ST1,_T1),
1390 CodeToComputeCard = (kernel_objects:cardinality_as_int(SET1,int(SCard)),
1391 custom_explicit_sets:blocking_factorial(SCard,Card)).
1392 card_for_member_closure(iseq(b(value(SET1),ST1,_)),_SEQT,Card,CodeToComputeCard) :- !, /* _SEQT=iseq(T1) */
1393 is_set_type(ST1,_T1),
1394 CodeToComputeCard = (kernel_objects:cardinality_as_int(SET1,int(SCard)),
1395 kernel_card_arithmetic:blocking_nr_iseq(SCard,Card)).
1396 card_for_member_closure(iseq1(b(value(SET1),ST1,_)),_SEQT,Card,CodeToComputeCard) :- !, /* _SEQT=iseq1(T1) */
1397 is_set_type(ST1,_T1),
1398 CodeToComputeCard = (kernel_objects:cardinality_as_int(SET1,int(SCard)),
1399 kernel_card_arithmetic:blocking_nr_iseq1(SCard,Card)).
1400 card_for_member_closure(identity(b(value(SET1),ST1,_)),_SEQT,Card,CodeToComputeCard) :- !, /* _SEQT=id(T1) */
1401 is_set_type(ST1,_T1),
1402 CodeToComputeCard =
1403 kernel_objects:cardinality_as_int(SET1,int(Card)).
1404 card_for_member_closure(struct(b(RecVal,record(_FieldSetTypes),_)), record(_FieldTypes), % set of records
1405 Card,CodeToComputeCard) :-
1406 !,
1407 (RecVal=value(RECF), nonvar(RECF), RECF=rec(FIELDS) % value has been computed:
1408 -> CodeToComputeCard = custom_explicit_sets:get_field_cardinality(FIELDS,Card)
1409 ; RecVal = rec(TypedFields), % we still have a typed AST
1410 maplist(get_field_val_type,TypedFields,Exprs,Types),
1411 l_card_for_member_closure(Exprs,Types,Card, CodeToComputeCard)
1412 ).
1413 % now dealt with separately above: card_for_member_closure(cartesian_product(b(value(SET1),set(T1),_), b(value(SET2),set(T2),_)),
1414 % couple(T1,T2), Card,CodeToComputeCard) :- !,
1415 % CodeToComputeCard =
1416 % (kernel_objects:cardinality_as_int(SET1,int(SCard1)),
1417 % kernel_objects:cardinality_as_int(SET2,int(SCard2)),
1418 % custom_explicit_sets:blocking_safe_mul(SCard1,SCard2,Card) ).
1419 card_for_member_closure(POW,TYPE, Card,CodeToComputeCard) :-
1420 (POW = pow_subset(b(value(SET),TYPE,_)) ;
1421 POW = fin_subset(b(value(SET),TYPE,_))),!,
1422 CodeToComputeCard =
1423 (kernel_objects:cardinality_as_int(SET,int(SCard)),
1424 custom_explicit_sets:blocking_safe_pow2(SCard,Card)
1425 ).
1426 card_for_member_closure(POW,TYPE, Card,CodeToComputeCard) :-
1427 (POW = pow1_subset(b(value(SET),TYPE,_)) ;
1428 POW = fin1_subset(b(value(SET),TYPE,_))),!,
1429 CodeToComputeCard =
1430 (kernel_objects:cardinality_as_int(SET,int(SCard)),
1431 custom_explicit_sets:blocking_safe_pow2(SCard,C1),
1432 custom_explicit_sets:safe_dec(C1,Card)
1433 ).
1434 card_for_member_closure(RELEXPR,SType, Card,CodeToComputeCard) :-
1435 is_set_type(SType,couple(T1,T2)),
1436 is_a_relation(RELEXPR, b(value(DOM),set(T1),_),
1437 b(value(RAN),set(T2),_), DCard,RCard,Card,RELCODE),!,
1438 CodeToComputeCard =
1439 (
1440 kernel_objects:cardinality_as_int(DOM,int(DCard)),
1441 kernel_objects:cardinality_as_int(RAN,int(RCard)),
1442 custom_explicit_sets:call_card_for_relations(DCard,RCard,RELCODE)
1443 ).
1444 card_for_member_closure(BODY, integer, Card,CodeToComputeCard) :-
1445 is_interval_with_integer_bounds(BODY,Low,Up),!,
1446 CodeToComputeCard = custom_explicit_sets:card_of_interval_inf(Low,Up,Card).
1447 card_for_member_closure(value(Value), _Type, Card,CodeToComputeCard) :-
1448 % we have a closure of the type {x|x:S}; equivalent to S
1449 (nonvar(Value),
1450 Value=closure(P,T,B)
1451 -> % cardinality_as_int may expand it ! is bad if e.g. we called this code to check if a closure is infinite
1452 card_for_specific_closure2(P,T,B,Card,CodeToComputeCard) % will not expand, but fail if cannot be computed
1453 % TO DO: provide an argument: precise_or_efficient
1454 ; CodeToComputeCard = kernel_objects:cardinality_as_int(Value,int(Card))
1455 ).
1456 %card_for_member_closure(BODY, Type, Card,CodeToComputeCard) :- print(try_card(BODY,Type)),nl,fail.
1457 % TO DO: add maybe other common closures ? simple value closure
1458 % also: what if subexpressions are not of value() type ?
1459
1460 :- public call_card_for_relations/3.
1461 :- block call_card_for_relations(-,?,?), call_card_for_relations(?,-,?).
1462 call_card_for_relations(_,_,RELCODE) :- call(RELCODE).
1463
1464 get_field_val_type(field(_F1,b(Expr1,Type1,_)),Expr1,Type1).
1465
1466 l_card_for_member_closure([Expr1],[Type1],Card,CodeToComputeCard) :- !,
1467 card_for_member_closure(Expr1,Type1,Card, CodeToComputeCard).
1468 l_card_for_member_closure([Expr1|ET],[Type1|TT],Card,CodeToComputeCard) :-
1469 CodeToComputeCard = (Code1,Code2, custom_explicit_sets:blocking_safe_mul(Card1,Card2,Card)),
1470 card_for_member_closure(Expr1,Type1,Card1, Code1),
1471 l_card_for_member_closure(ET,TT,Card2,Code2).
1472
1473 :- public safe_dec/2. % used in card_for_member_closure
1474 :- block safe_dec(-,?).
1475 safe_dec(inf,R) :- !, R=inf.
1476 safe_dec(inf_overflow,R) :- !, R=inf_overflow.
1477 safe_dec(X,R) :- R is X-1.
1478
1479 :- use_module(kernel_equality,[empty_set_test/2]).
1480 :- public seq_card/2. % used in card_for_member_closure
1481 :- block seq_card(-,?).
1482 seq_card([],R) :- !,R=1.
1483 seq_card([_|_],R) :- !,R=inf.
1484 seq_card(X,Res) :- empty_set_test(X,EqRes),
1485 set_card(EqRes,1,Res).
1486
1487 :- block set_card(-,?,?).
1488 set_card(pred_true,Nr,Nr).
1489 set_card(pred_false,_,inf).
1490 % card(seq({n|n>10 & (n mod 20=3 & n mod 20 = 4) }))
1491
1492 :- public seq1_card/2. % used in card_for_member_closure
1493 :- block seq1_card(-,?).
1494 seq1_card([],R) :- !,R=0.
1495 seq1_card([_|_],R) :- !,R=inf.
1496 seq1_card(X,Res) :- empty_set_test(X,EqRes), set_card(EqRes,0,Res).
1497
1498 :- public get_field_cardinality/2. % used in card_for_member_closure
1499 get_field_cardinality([],1).
1500 get_field_cardinality([field(_Name,Value)|T],ResCard) :-
1501 kernel_objects:cardinality_as_int(Value,int(SCard1)),
1502 get_field_cardinality(T,RestCard), blocking_safe_mul(SCard1,RestCard,ResCard).
1503
1504 :- use_module(kernel_card_arithmetic).
1505
1506 :- block blocking_safe_mul(-,-,?).
1507 blocking_safe_mul(A,B,R) :-
1508 ( A==0 -> R=0
1509 ; B==0 -> R=0
1510 ; A==1 -> R=B
1511 ; B==1 -> R=A
1512 ; blocking_safe_mul2(A,B,R) ).
1513
1514 :- block blocking_safe_mul2(-,?,?), blocking_safe_mul2(?,-,?).
1515 blocking_safe_mul2(A,B,Res) :-
1516 (safe_mul(A,B,AB) -> Res=AB
1517 ; add_warning(blocking_safe_mul2,'Call failed: ',blocking_safe_mul2(A,B,Res)),
1518 % could happen for something like prj2(BOOL,NAT) = prj2(BOOL,0..n)
1519 fail).
1520
1521 :- public blocking_safe_pow2/2. % used in card_for_member_closure above
1522 :- block blocking_safe_pow2(-,?).
1523 blocking_safe_pow2(A,Res) :-
1524 (safe_pow2(A,A2) -> Res=A2
1525 ; add_warning(blocking_safe_pow2,'Call failed: ',safe_pow2(A,Res)),fail).
1526
1527
1528
1529
1530 :- assert_must_succeed((custom_explicit_sets:card_for_specific_closure2(['_zzzz_binary'],[integer],
1531 b(member(b(identifier('_zzzz_binary'),integer,[generated]),
1532 b(interval(b(value(int(1)),integer,[]),b(value(int(10)),integer,[])),set(integer),[])),pred,[]),R,C),
1533 call(C),
1534 R=10)).
1535
1536 %! is_interval_closure_or_integerset(+I,-L,-U)
1537 is_interval_closure_or_integerset(Var,_,_) :- var(Var),!,fail.
1538 is_interval_closure_or_integerset(global_set(X),Low,Up) :- !, get_integer_set_interval(X,Low,Up).
1539 is_interval_closure_or_integerset(Set,El,El) :- singleton_set(Set,ELX),
1540 nonvar(ELX), ELX=int(El),!. % new, useful??
1541 is_interval_closure_or_integerset(closure(P,T,B),Low,Up) :-
1542 ? (is_geq_leq_interval_closure(P,T,B,Low,Up) -> true ; is_interval_closure(P,T,B,Low,Up)).
1543
1544 % with an additional argument to know if the set is finite or infinite:
1545 is_interval_closure_or_integerset(Set,Low,Up,Finite) :-
1546 is_interval_closure_or_integerset(Set,Low,Up),
1547 % if we obtain Low, Up as variables then these must be finite numbers; they cannot stand for inf
1548 (infinite_interval(Low,Up) -> Finite=infinite ; Finite=finite).
1549
1550
1551 get_integer_set_interval('NAT',0,MAXINT) :- (preferences:preference(maxint,MAXINT)->true).
1552 get_integer_set_interval('NAT1',1,MAXINT) :- (preferences:preference(maxint,MAXINT)->true).
1553 get_integer_set_interval('INT',MININT,MAXINT) :-
1554 ((preferences:preference(maxint,MAXINT),preferences:preference(minint,MININT))->true).
1555 get_integer_set_interval('NATURAL',0,inf).
1556 get_integer_set_interval('NATURAL1',1,inf).
1557 get_integer_set_interval('INTEGER',minus_inf,inf).
1558 % TO DO: add minus_inf to kernel_objects !
1559
1560 :- block geq_inf(-,?), geq_inf(?,-).
1561 geq_inf(inf,_) :- !.
1562 geq_inf(minus_inf,B) :- !, B=minus_inf.
1563 geq_inf(_,minus_inf) :- !.
1564 geq_inf(A,inf) :- !, A=inf.
1565 geq_inf(inf_overflow,_) :- !.
1566 geq_inf(A,inf_overflow) :- !, A=inf_overflow.
1567 geq_inf(A,B) :- A >= B.
1568
1569 :- block minimum_with_inf(-,-,?).
1570 % in the first three cases we can determine outcome without knowing both args
1571 minimum_with_inf(A,B,R) :- (A==minus_inf ; B==minus_inf),!,R=minus_inf.
1572 minimum_with_inf(A,B,R) :- A==inf,!,R=B.
1573 minimum_with_inf(A,B,R) :- B==inf,!,R=A.
1574 minimum_with_inf(A,B,R) :- minimum_with_inf1(A,B,R), geq_inf(A,R), geq_inf(B,R).
1575 :- block minimum_with_inf1(-,?,?), minimum_with_inf1(?,-,?).
1576 minimum_with_inf1(minus_inf,_,R) :- !, R=minus_inf.
1577 minimum_with_inf1(_,minus_inf,R) :- !, R=minus_inf.
1578 minimum_with_inf1(inf,B,R) :- !, R=B.
1579 minimum_with_inf1(A,inf,R) :- !, R=A.
1580 minimum_with_inf1(inf_overflow,B,R) :- !, R=B.
1581 minimum_with_inf1(A,inf_overflow,R) :- !, R=A.
1582 minimum_with_inf1(A,B,R) :- (A<B -> R=A ; R=B).
1583
1584 :- block maximum_with_inf(-,-,?).
1585 % in the first three cases we can determine outcome without knowing both args
1586 maximum_with_inf(A,B,R) :- (A==inf ; B==inf),!,R=inf.
1587 maximum_with_inf(A,B,R) :- A==minus_inf,!,R=B.
1588 maximum_with_inf(A,B,R) :- B==minus_inf,!,R=A.
1589 maximum_with_inf(A,B,R) :- maximum_with_inf1(A,B,R), geq_inf(R,A), geq_inf(R,B).
1590 :- block maximum_with_inf1(-,?,?), maximum_with_inf1(?,-,?).
1591 maximum_with_inf1(inf,_,R) :- !, R=inf.
1592 maximum_with_inf1(_,inf,R) :- !, R=inf.
1593 maximum_with_inf1(minus_inf,B,R) :- !, R=B.
1594 maximum_with_inf1(A,minus_inf,R) :- !, R=A.
1595 maximum_with_inf1(inf_overflow,_,R) :- !, R=inf_overflow.
1596 maximum_with_inf1(_,inf_overflow,R) :- !, R=inf_overflow.
1597 maximum_with_inf1(A,B,R) :- (A>B -> R=A ; R=B).
1598
1599 /* utilities for detecting interval closures */
1600 construct_interval_closure(Low,Up,Res) :- (Low==inf;Up==minus_inf),!,Res=[].
1601 construct_interval_closure(Low,Up,Res) :- number(Low),number(Up), Low>Up,!,Res=[].
1602 construct_interval_closure(Low,Up,Res) :- Up==inf,!,
1603 ( Low==0 -> Res = global_set('NATURAL')
1604 ; Low==1 -> Res = global_set('NATURAL1')
1605 ; Low==minus_inf -> Res = global_set('INTEGER')
1606 ; Low==inf -> Res = []
1607 ; construct_greater_equal_closure(Low,Res)
1608 ).
1609 construct_interval_closure(Low,Up,Res) :- Low==minus_inf,!,
1610 construct_less_equal_closure(Up,Res).
1611 construct_interval_closure(Low,Up,Res) :- Low==Up,!,
1612 (number(Low) -> construct_one_element_custom_set(int(Low),Res)
1613 ; Res = [int(Low)]).
1614 construct_interval_closure(Low,Up,Res) :-
1615 construct_interval_set(Low,Up,Set),
1616 construct_member_closure('_zzzz_unary',integer,[],Set,Res).
1617
1618 transform_global_sets_into_closure(closure(P,T,B),closure(P,T,B)).
1619 transform_global_sets_into_closure(global_set(X),Res) :-
1620 transform_global_set_into_closure_aux(X,Res).
1621 transform_global_set_into_closure_aux('NATURAL',Res) :-
1622 construct_greater_equal_closure(0,Res).
1623 transform_global_set_into_closure_aux('NATURAL1',Res) :-
1624 construct_greater_equal_closure(1,Res).
1625 % TO DO: add INTEGER
1626
1627
1628
1629 is_geq_leq_interval_closure([Par],[integer],b(Body,pred,Span),Low,Up) :-
1630 (var(Par)
1631 -> add_internal_error('Non-ground closure: ',closure([Par],[integer],b(Body,pred,Span))),fail
1632 ? ; get_geq_leq_bounds(Body,Par,Low,Up)).
1633
1634 infinite_interval(Low,Up) :- (Low==minus_inf -> true ; Up==inf).
1635
1636 :- assert_must_succeed((card_of_interval_inf(1,10,10))).
1637 :- assert_must_succeed((card_of_interval_inf(1,inf,R),R==inf)).
1638 :- assert_must_succeed((card_of_interval_inf(minus_inf,0,R),R==inf)).
1639 :- assert_must_succeed((card_of_interval_inf(2,2,R), R==1)).
1640 :- assert_must_succeed((card_of_interval_inf(12,2,R), R==0)).
1641 :- assert_must_succeed((card_of_interval_inf(2,B,10), B==11)).
1642 :- assert_must_succeed((card_of_interval_inf(A,12,10), A==3)).
1643 :- assert_must_succeed((card_of_interval_inf(A,12,0), A=222)).
1644 :- assert_must_succeed((card_of_interval_inf(12,B,0), B=11)).
1645 :- assert_must_fail((card_of_interval_inf(A,12,0), A=12)).
1646 % compute cardinality of interval; allow bounds to be inf and minus_inf (but if so, they must be bound straightaway)
1647 card_of_interval_inf(A,B,Card) :-
1648 ? at_least_two_vars(A,B,Card), % initially this will usually be true, if only one variable we can compute result
1649 preferences:preference(use_clpfd_solver,true),
1650 !,
1651 clpfd_interface:post_constraint(Card #= max(0,1+B-A),custom_explicit_sets:block_card_of_interval_inf(A,B,Card)).
1652 card_of_interval_inf(A,B,Card) :- block_card_of_interval_inf(A,B,Card).
1653
1654 ?at_least_two_vars(A,B,C) :- var(A),!,(var(B) -> not_infinite_bound(C) ; number(B),var(C)).
1655 at_least_two_vars(A,B,C) :- number(A), var(B),var(C).
1656 not_infinite_bound(A) :- (var(A) ; number(A)). % inf can only appear immediately, not for variables
1657
1658 :- block block_card_of_interval_inf(-,?,-),block_card_of_interval_inf(?,-,-).
1659 block_card_of_interval_inf(A,_,Card) :- A==minus_inf,!, Card=inf.
1660 block_card_of_interval_inf(_,B,Card) :- B==inf,!, Card=inf.
1661 block_card_of_interval_inf(From,To,Card) :- number(From),number(To),!,
1662 (From>To -> Card=0 ; Card is (To-From)+1).
1663 block_card_of_interval_inf(A,B,C) :- number(C),!, number_card_of_interval_inf_aux(C,A,B).
1664 block_card_of_interval_inf(A,B,C) :- C==inf,!,
1665 % probably this should systematically fail; if A and B are not inf/minus_inf now they will never be
1666 print(infinite_interval_requested(A,B,C)),nl,
1667 when((nonvar(A),nonvar(B)), block_card_of_interval_inf(A,B,C)).
1668 block_card_of_interval_inf(A,B,C) :- add_internal_error('Illegal call: ',card_of_interval_inf(A,B,C)).
1669 :- use_module(inf_arith,[block_inf_greater/2]).
1670 number_card_of_interval_inf_aux(0,A,B) :- !, % empty interval
1671 % if A and B are variables then they will not become inf later ??
1672 % inf can only be set directly for sets such as {x|x>4} or NATURAL1
1673 (((var(A);number(A)),(var(B);number(B)))
1674 % hence we can use ordinary comparison (with CLPFD) here
1675 -> kernel_objects:less_than_direct(B,A)
1676 % TO DO: we could do this even if both A and B are variables !! ex : {n,m|n..m = {} & m..100={} & 103..n={}}
1677 ; block_inf_greater(A,B)).
1678 %number_card_of_interval_inf_aux(Card,From,B) :- number(From),!, B is (From+Card)-1.
1679 %number_card_of_interval_inf_aux(Card,A,To) :- number(To),!, A is 1+To-Card.
1680 number_card_of_interval_inf_aux(Card,A,B) :-
1681 Card>0, C1 is Card-1,
1682 kernel_objects:int_minus(int(B),int(A),int(C1)).
1683
1684
1685 get_geq_leq_bounds(conjunct(b(LEFT,pred,_),b(RIGHT,pred,_)), Par,Low,Up) :-
1686 ? get_geq_leq_bounds(LEFT,Par,From1,To1),
1687 ? get_geq_leq_bounds(RIGHT,Par,From2,To2),
1688 intersect_intervals_with_inf(From1,To1,From2,To2,Low,Up).
1689 get_geq_leq_bounds(member(b(identifier(Par),integer,_),
1690 b(Value,set(integer),_)),Par,Low,Up) :-
1691 get_value_bounds(Value,Low,Up).
1692 ?get_geq_leq_bounds(greater_equal(b(A,_,_),b(B,_,_)),Par,Low,Up) :- get_bounds2(greater_equal,A,B,Par,Low,Up).
1693 ?get_geq_leq_bounds( less_equal(b(A,_,_),b(B,_,_)),Par,Low,Up) :- get_bounds2(less_equal,A,B,Par,Low,Up).
1694 ?get_geq_leq_bounds( greater(b(A,_,_),b(B,_,_)),Par,Low,Up) :- get_bounds2(greater,A,B,Par,Low,Up).
1695 ?get_geq_leq_bounds( less(b(A,_,_),b(B,_,_)),Par,Low,Up) :- get_bounds2(less,A,B,Par,Low,Up).
1696
1697 get_value_bounds(value(GS),Low,Up) :- is_interval_closure_or_integerset(GS,Low,Up). % recursive call
1698 % nonvar(GS), GS=global_set(ISET), get_integer_set_interval(ISET,Low,Up).
1699 get_value_bounds(interval(b(TLow,_,_),b(TUp,_,_)),Low,Up) :-
1700 integer_value(TLow,Low),
1701 integer_value(TUp,Up).
1702
1703 get_bounds2(greater_equal,identifier(Par),V,Par,X,inf) :- integer_value(V,X).
1704 get_bounds2(greater_equal,V,identifier(Par),Par,minus_inf,X) :- integer_value(V,X).
1705 get_bounds2(less_equal,identifier(Par),V,Par,minus_inf,X) :- integer_value(V,X).
1706 get_bounds2(less_equal,V,identifier(Par),Par,X,inf) :- integer_value(V,X).
1707 get_bounds2(greater,identifier(Par),V,Par,X1,inf) :- integer_value(V,X), kernel_objects:int_plus(int(X),int(1),int(X1)). %, X1 is X+1.
1708 get_bounds2(greater,V,identifier(Par),Par,minus_inf,X1) :- integer_value(V,X), kernel_objects:int_minus(int(X),int(1),int(X1)). %X1 is X-1.
1709 get_bounds2(less,V,identifier(Par),Par,X1,inf) :- integer_value(V,X), kernel_objects:int_plus(int(X),int(1),int(X1)). %X1 is X+1.
1710 get_bounds2(less,identifier(Par),V,Par,minus_inf,X1) :- integer_value(V,X),
1711 kernel_objects:int_minus(int(X),int(1),int(X1)). %X1 is X-1.
1712 % to do: add negation thereof ??
1713
1714 integer_value(V,_) :- var(V),!, print(var_integer_value(V)),nl,fail.
1715 integer_value(integer(X),R) :- !, R=X.
1716 integer_value(unary_minus(b(X,_,_)),R) :- !, integer_value(X,RM),
1717 number(RM), % if RM is not a number we could setup CLPFD constraint ?!
1718 R is -(RM).
1719 integer_value(minus(b(X,_,_),b(Y,_,_)),R) :- !, % some AST compilation rules generate X-1, X+1 ...
1720 integer_value(X,RMX),
1721 integer_value(Y,RMY),
1722 kernel_objects:int_minus(int(RMX),int(RMY),int(R)).
1723 integer_value(plus(b(X,_,_),b(Y,_,_)),R) :- !, % some AST compilation rules generate X-1, X+1 ...
1724 integer_value(X,RMX),
1725 integer_value(Y,RMY),
1726 kernel_objects:int_plus(int(RMX),int(RMY),int(R)).
1727 integer_value(value(V),R) :- !, V=int(R).
1728
1729 is_interval_closure(closure(Par,[integer],Pred),Low,Up) :-
1730 is_interval_closure_aux(Par,Pred,Low,Up).
1731 is_interval_closure(Par,[integer],Pred,Low,Up) :-
1732 is_interval_closure_aux(Par,Pred,Low,Up).
1733 is_interval_closure_aux(Par,Pred,Low,Up) :-
1734 is_member_closure(Par,[integer],Pred,integer,Set),
1735 is_interval_with_integer_bounds(Set,Low,Up).
1736 %is_interval_closure(closure_x(Par,[integer],Pred,_),Low,Up) :-
1737 % is_interval_closure(closure(Par,[integer],Pred),Low,Up).
1738
1739 is_interval_closure_body(Body,ID,Low,Up) :-
1740 is_member_closure([ID],[integer],Body,integer,Set),!,
1741 is_interval_with_integer_bounds(Set,Low,Up).
1742 is_interval_closure_body(Body,ID,Low,Up) :-
1743 ? is_geq_leq_interval_closure([ID],[integer],Body,Low,Up),
1744 number(Low), number(Up).
1745
1746 :- use_module(bsyntaxtree,[get_texpr_info/2,get_texpr_id/2]).
1747 % do a single check if we have interval, member or not-member closure, avoiding redundant checking
1748 % TO DO: move this and related predicates to closures module ?
1749 is_special_closure(_Ids,_Types,Pred,Result) :-
1750 get_texpr_info(Pred,Info),memberchk(prob_annotation(recursive(RId)),Info),!,
1751 Result = recursive_special_closure(RId).
1752 is_special_closure(Ids,Types,Pred,Result) :-
1753 ? is_memoization_closure(Ids,Types,Pred,MemoID),!,
1754 Result = memoization_closure(MemoID).
1755 is_special_closure([ID],[TYPE],b(PRED,_,_), Result) :-
1756 ( closures:is_member_closure_aux(PRED, ID,TYPE,SET) ->
1757 ( (TYPE=integer, is_interval_with_integer_bounds(SET,Low,Up)) ->
1758 Result = interval(Low,Up)
1759 ; Result = member_closure(ID,TYPE,SET))
1760 ; closures:is_not_member_closure_aux(PRED,ID,TYPE,SET) ->
1761 Result = not_member_closure(ID,TYPE,SET)
1762 ? ; (TYPE=integer,get_geq_leq_bounds(PRED,ID,Low,Up),number(Low), number(Up)) ->
1763 Result = interval(Low,Up)
1764 ).
1765
1766
1767 construct_interval_set(Low,Up,Res) :-
1768 Res = interval(b(value(int(Low)),integer,[]),
1769 b(value(int(Up)), integer,[])).
1770 is_interval_with_integer_bounds(X,L,U) :- var(X),!,
1771 add_internal_error('var arg: ',is_interval_with_integer_bounds(X,L,U)),fail.
1772 is_interval_with_integer_bounds(interval(b(TLOW,integer,_),b(TUP, integer,_)),Low,Up) :-
1773 integer_value(TLOW,Low), integer_value(TUP,Up).
1774
1775
1776 is_a_relation(relations(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '<->'
1777 Code = (kernel_card_arithmetic:safe_mul(DCard,RCard,Exp), kernel_card_arithmetic:safe_pow2(Exp,Card)).
1778 is_a_relation(partial_function(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '+->'
1779 Code = (kernel_card_arithmetic:safe_add_card(RCard,1,R1),kernel_card_arithmetic:safe_pown(R1,DCard,Card)).
1780 is_a_relation(total_function(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '-->'
1781 Code = (kernel_card_arithmetic:safe_pown(RCard,DCard,Card)).
1782 is_a_relation(partial_bijection(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '>+>>'
1783 Code = (kernel_card_arithmetic:partial_bijection_card(DCard,RCard,Card)).
1784 is_a_relation(total_bijection(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '>->>'
1785 Code = (kernel_card_arithmetic:total_bijection_card(DCard,RCard,Card)).
1786 is_a_relation(total_injection(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '>->'
1787 Code = (kernel_card_arithmetic:blocking_factorial_k(RCard,DCard,Card)).
1788 is_a_relation(partial_injection(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '>+>'
1789 Code = (kernel_card_arithmetic:partial_injection_card(DCard,RCard,Card)).
1790 is_a_relation(total_surjection(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '-->>'
1791 Code = (kernel_card_arithmetic:total_surjection_card(DCard,RCard,Card)).
1792 is_a_relation(partial_surjection(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '+->>'
1793 Code = (kernel_card_arithmetic:partial_surjection_card(DCard,RCard,Card)).
1794 is_a_relation(total_relation(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '<<->'
1795 Code = (kernel_card_arithmetic:total_relation_card(DCard,RCard,Card)).
1796 is_a_relation(surjection_relation(Domain,Range),Domain,Range,DCard,RCard,Card,Code) :- %% '<->>'
1797 % just swap args: card(A<->>B) = card(B<<->A)
1798 Code = (kernel_card_arithmetic:total_relation_card(RCard,DCard,Card)).
1799 % TO DO: total_surjection_relation <<->>
1800
1801
1802
1803 :- use_module(b_global_sets,[infinite_global_set/1]).
1804
1805 :- block is_infinite_global_set(-,?).
1806 is_infinite_global_set('NATURAL',integer).
1807 is_infinite_global_set('NATURAL1',integer).
1808 is_infinite_global_set('INTEGER',integer).
1809 is_infinite_global_set('FLOAT',real).
1810 is_infinite_global_set('REAL',real).
1811 is_infinite_global_set('STRING',string).
1812 is_infinite_global_set(G,global(G)) :- infinite_global_set(G).
1813
1814 %is_finite_integer_global_set('NAT').
1815 %is_finite_integer_global_set('NAT1').
1816 %is_finite_integer_global_set('INT').
1817
1818 % detects (certain) infinite explict sets
1819 is_infinite_explicit_set(X) :- var(X),!, add_internal_error(is_infinite_explicit_set,var(X)),fail.
1820 ?is_infinite_explicit_set(global_set(X)) :- is_infinite_global_set(X,_).
1821 is_infinite_explicit_set(freetype(X)) :- is_infinite_freetype(X).
1822 is_infinite_explicit_set(closure(Par,T,Body)) :- is_infinite_closure(Par,T,Body).
1823
1824 % detect some closure that we should definitely expand; even in SYMBOLIC mode or for ABSTRACT_CONSTANTS
1825 definitely_expand_this_explicit_set(Var) :- var(Var),!,fail.
1826 definitely_expand_this_explicit_set(closure(P,T,B)) :-
1827 (B = b(Body,_,_), definitely_expand(Body,P) -> true
1828 ; is_interval_closure(P,T,B,Low,Up), do_expand_interval(Low,Up)).
1829 % some lambda functions have small domain, but are very complicated to compute (test 1078, 1376)
1830 % hence the following is not sufficient:
1831 % ;is_small_specific_custom_set(closure(P,T,B),100), print(exp(T)),nl,translate:print_bexpr(B),nl,fail).
1832
1833 definitely_expand(Body,_) :- avl_mem_construct(Body,_).
1834 definitely_expand(exists(TEIDS,Body),P) :- P = [ID], TEIDS = [TEID], % TO DO: detect multiple ids
1835 % detect {res|#y.(y:AVL & res=Expr(y))} % test 1101
1836 Body = b(conjunct(b(Mem,pred,_),Eq),pred,_),
1837 Eq = b(equal(EqA,EqB),pred,_),
1838 avl_mem_construct(Mem,LHS), get_texpr_id(LHS,EID), get_texpr_id(TEID,EID),
1839 (get_texpr_id(EqA,ID) -> true ; get_texpr_id(EqB,ID)).
1840
1841 avl_mem_construct(member(LHS,RHS),LHS) :- RHS = b(value(V),_,_), nonvar(V), V=avl_set(_).
1842
1843 % dont_expand_this_explicit_set with default limit (20000)
1844 dont_expand_this_explicit_set(closure(P,T,B)) :- !,
1845 dont_expand_this_closure(P,T,B).
1846 dont_expand_this_explicit_set(S) :-
1847 is_infinite_or_very_large_explicit_set(S).
1848
1849 % dont_expand_this_explicit_set with extra limit argument:
1850 dont_expand_this_explicit_set(closure(P,T,B),Limit) :- !, dont_expand_this_closure(P,T,B,Limit).
1851 dont_expand_this_explicit_set(S,_) :- is_infinite_or_very_large_explicit_set(S).
1852
1853 % true if we have a closure / global_set that should not be expanded
1854 % TO DO: we could detect finite (is_lambda_value_domain_closure) closures which contain infinite elements such as %p.(p : BOOL|%t.(t : NATURAL|t .. t + 7))
1855 dont_expand_symbolic_explicit_set(closure(P,T,B)) :- !,
1856 dont_expand_this_closure(P,T,B).
1857 dont_expand_symbolic_explicit_set(avl_set(_)) :- !,
1858 fail. % already expanded
1859 dont_expand_symbolic_explicit_set(S) :-
1860 is_infinite_or_very_large_explicit_set(S).
1861
1862
1863 dont_expand_this_closure(P,T,B) :-
1864 dont_expand_this_closure(P,T,B,20000). % % increase limit in Data valid mode?
1865
1866 dont_expand_this_closure(_P,_T,b(_,_,INFO),_Limit) :-
1867 ? member(prob_annotation(KIND),INFO),
1868 (KIND = 'SYMBOLIC' -> ! % cf is_symbolic_closure in closures
1869 ; KIND = 'FORCE' -> !, fail). % was wrapped in FORCE external_function
1870 dont_expand_this_closure(P,T,B,_Limit) :-
1871 is_interval_closure_or_integerset(closure(P,T,B),Low,Up), !,
1872 % interval closures are quite efficient for certain manipulations
1873 (number(Low), number(Up)
1874 -> dont_expand_interval(Low,Up)
1875 ; true % we have a closure with inf/minus_inf or variables as bounds; in both cases keep the closure
1876 ).
1877 dont_expand_this_closure(P,T,B,Limit) :-
1878 is_infinite_or_very_large_closure(P,T,B,Limit).
1879 %% TODO: also prevent expansion of things like ff = %x.(x:STRING & REGEX_MATCH(x,"[a-z]+")=TRUE|TRUE)
1880
1881 % do not automatically expand these intervals
1882 dont_expand_interval(Low,Up) :-
1883 Up+1-Low > 100. % another magic constant ; which value to choose ??
1884 do_expand_interval(Low,Up) :- Up+1-Low =< 100.
1885
1886 is_converted_lambda_closure(_P,_T,b(_,_,INFO)) :-
1887 ? member(prob_annotation('LAMBDA'),INFO).
1888
1889 is_symbolic_closure_or_symbolic_mode(P,T,B) :-
1890 ? (is_symbolic_closure(P,T,B) -> true
1891 ; preference(convert_comprehension_sets_into_closures,true)
1892 % by default suppose closures should be dealt with symbolically
1893 ).
1894 /*
1895 % check both LAMBDA + not RECURSIVE
1896 is_converted_non_recursive_lambda_closure(_,_,b(_,_,INFO)) :- is_conv_lambda_nonrec(INFO).
1897 is_conv_lambda_nonrec([prob_annotation(A)|T]) :- !,
1898 (A='LAMBDA' -> \+ memberchk(prob_annotation('RECURSIVE'),T)
1899 ; A\='RECURSIVE' -> is_conv_lambda_nonrec(T)).
1900 is_conv_lambda_nonrec([_|T]) :- is_conv_lambda_nonrec(T). */
1901
1902
1903
1904 % a set that is so large that expanding it would probably cause problems
1905 is_infinite_or_very_large_explicit_set(S) :-
1906 is_infinite_or_very_large_explicit_set(S,20000). % increase limit in Data valid mode?
1907
1908 :- use_module(inf_arith,[infgreater/2]).
1909
1910 is_infinite_or_very_large_explicit_set(X,_) :- var(X),!,print(var_is_infinite_check(X)),nl,fail.
1911 is_infinite_or_very_large_explicit_set(closure(P,T,B),Limit) :- !,
1912 % treat closure separately here; some special rules
1913 is_infinite_or_very_large_closure(P,T,B,Limit).
1914 is_infinite_or_very_large_explicit_set(avl_set(A),Limit) :- !, % we could compute log and use avl_height_less_than
1915 quick_avl_approximate_size(A,Size), Size >= Limit.
1916 is_infinite_or_very_large_explicit_set(X,Limit) :- % closures are checked above
1917 explicit_set_cardinality(X,Card),
1918 nonvar(Card),infgreater(Card,Limit).
1919
1920
1921 is_very_large_or_symbolic_closure(P,T,B,Limit) :-
1922 ? (is_symbolic_closure(P,T,B) -> true ; is_infinite_or_very_large_closure(P,T,B,Limit)).
1923 :- use_module(bsyntaxtree,[is_a_disjunct/3]).
1924 is_infinite_or_very_large_closure(P,T,B,Limit) :-
1925 is_a_disjunct(B,D1,D2), % Assumption: there is no card_for_specific_closure code for disjuncts
1926 !,
1927 (is_infinite_or_very_large_closure(P,T,D1,Limit) -> true
1928 ; is_infinite_or_very_large_closure(P,T,D2,Limit)).
1929 is_infinite_or_very_large_closure(Par,T,Body,Limit) :-
1930 is_closure1_value_closure(Par,T,Body,VAL),!,
1931 nonvar(VAL), % it could still be large or infinite
1932 (Limit>1 -> NLimit is Limit/2 ; NLimit = Limit), % reduce limit as closure1 usually blows up
1933 is_infinite_or_very_large_explicit_set(VAL,NLimit).
1934 is_infinite_or_very_large_closure(P,T,B,Limit) :-
1935 card_for_specific_closure3(Kind,P,T,B,Card,Code),
1936 on_enumeration_warning(call(Code),
1937 (debug_println(9,cannot_expand_specific_closure_for_card(Kind,Limit)),
1938 % see test 1519 for relevance
1939 Card=inf)), % assume it is large
1940 !,
1941 nonvar(Card),infgreater(Card,Limit).
1942
1943
1944 is_infinite_or_symbolic_closure(P,T,B) :-
1945 ? (is_symbolic_closure(P,T,B) -> true ; is_infinite_closure(P,T,B)).
1946 is_infinite_closure(P,T,B) :-
1947 is_a_disjunct(B,D1,D2), % Assumption: there is no card_for_specific_closure code for disjuncts
1948 !,
1949 (is_infinite_closure(P,T,D1) -> true ; is_infinite_closure(P,T,D2)).
1950 is_infinite_closure(Par,T,Body) :-
1951 is_closure1_value_closure(Par,T,Body,VAL),!, % TO DO: also check if closure1 is large this way
1952 nonvar(VAL), % if var: it could still be infinite !! TO DO fix
1953 is_infinite_explicit_set(VAL).
1954 is_infinite_closure(Par,T,Body) :-
1955 card_for_specific_closure(closure(Par,T,Body),Card,Code),
1956 call(Code), % TO DO: catch enumeration exceptions (see is_infinite_or_very_large_closure above)
1957 Card == inf. % TODO: instantiate inf before to avoid computing huge numbers
1958
1959
1960 :- use_module(memoization,[compute_memo_hash/2, get_stored_memo_expansion/3, store_memo_expansion/3]).
1961 /* transitive closure */
1962 closure1_for_explicit_set(avl_set(A),Res) :-
1963 preferences:preference(use_closure_expansion_memoization,true),!,
1964 compute_memo_hash(closure1_for_explicit_set(A),Hash),
1965 (get_stored_memo_expansion(Hash,closure1_for_explicit_set(A),StoredResult)
1966 -> Res = StoredResult
1967 ; closure1_for_explicit_set_direct(avl_set(A),Result),
1968 store_memo_expansion(Hash,closure1_for_explicit_set(A),Result),
1969 Res = Result
1970 ).
1971 closure1_for_explicit_set(avl_set(A),Res) :- closure1_for_explicit_set_direct(avl_set(A),Res).
1972
1973 % sometimes faster, but can also be considerably slower:
1974 %:- use_module(extrasrc(avl_ugraphs),[avl_transitive_closure/2]).
1975 %closure1_for_explicit_set_direct(avl_set(A),Res) :-
1976 % avl_transitive_closure(A,TC),
1977 % construct_avl_set(TC,Res).
1978 closure1_for_explicit_set_direct(avl_set(A),Res) :-
1979 avl_domain(A,AList),
1980 iterate_closure(AList,A,A,IterationRes),
1981 construct_avl_set(IterationRes,Res).
1982
1983 /* transitive closure starting from some initial set */
1984 /* not sure if we should do this:
1985 closure1_for_explicit_set_from(avl_set(A),StartFrom,Res) :-
1986 preferences:preference(use_closure_expansion_memoization,true),
1987 compute_memo_hash(closure1_for_explicit_set(A),Hash),
1988 stored_expansion(Hash,closure1_for_explicit_set(A),StoredResult),!,
1989 domain_restriction_explicit_set(StartFrom,StoredResult,Res). */
1990 % StartFrom can be avl_set(empty)
1991 closure1_for_explicit_set_from(avl_set(A),StartFrom,Res) :-
1992 avl_domain(A,AList),
1993 filter_start_relation(AList,StartFrom,FAList),
1994 (FAList = [] -> Res=[]
1995 ; convert_to_avl(FAList,avl_set(Start)),
1996 iterate_closure(FAList,A,Start,IterationRes),
1997 construct_avl_set(IterationRes,Res)).
1998 filter_start_relation([],_,[]).
1999 filter_start_relation([(X,Y)|T],StartSet,Res) :-
2000 (element_of_custom_set(X,StartSet) -> Res = [(X,Y)|RT] ; Res=RT),
2001 filter_start_relation(T,StartSet,RT).
2002
2003 iterate_closure([],_,Res,Res).
2004 iterate_closure([(X,Y)|T],InitialRelation,Relation,Res) :-
2005 %(Key = (X,Y) -> true ; add_error_and_fail(iterate_closure,'Not a relation element: ',Key)),
2006 add_tuples(X,Y,InitialRelation,Relation,NewRelation,AddedTuples),
2007 % better: do added tuples straight away ?
2008 iterate_closure(T,InitialRelation,NewRelation,NewRelation2),
2009 iterate_closure(AddedTuples,InitialRelation,NewRelation2,Res).
2010
2011 add_tuples(X,Y,AVL,AVLClosureSoFar,Res,NewTuples) :-
2012 findall((X,Z), (avl_fetch_pair(Y,AVL,Z), %ok instead of safe_avl_member((Y,Z),AVL),; Y in AVL form, Z var
2013 %Y \= Z, % self-loops are already in initial AVLClosure, this will never add a new pair
2014 % if we use AVLClosureSoFar instead of AVL: considerably slower
2015 \+ avl_fetch((X,Z),AVLClosureSoFar)), NewTuples),
2016 add_to_avl(NewTuples,AVLClosureSoFar,Res).
2017
2018 :- use_module(bsyntaxtree,[create_negation/2]).
2019 % SUBSET_OF <:
2020 % subset_of_explicit_set: returns code to be executed if this subset check can be done in an optimized way
2021 % TO DO: add strict_subset <<: + more cases, e.g., interval & avl_set, ...
2022 % interval & interval already handled in check_subset_of_global_sets
2023 subset_of_explicit_set(AVL,Closure,Code,_WF) :- nonvar(AVL),AVL=avl_set(A),
2024 is_interval_closure_or_integerset(Closure,Low,Up),!,
2025 Code=custom_explicit_sets:check_avl_in_interval(A,Low,Up).
2026 subset_of_explicit_set(Closure,CS,Code,WF) :- nonvar(CS), is_custom_explicit_set(CS),
2027 is_interval_closure_or_integerset(Closure,Low,Up),!,
2028 Code=custom_explicit_sets:check_interval_in_custom_set(Low,Up,CS,WF).
2029 subset_of_explicit_set(AVL1,AVL2,Code,_WF) :-
2030 nonvar(AVL1),AVL1=avl_set(A1), nonvar(AVL2),AVL2=avl_set(A2),!,
2031 Code = custom_explicit_sets:check_avl_subset(A1,A2).
2032 subset_of_explicit_set(C1,AVL2,Code,_WF) :- nonvar(C1),
2033 simple_finite_set(AVL2),
2034 ? is_simple_infinite_set(C1),!, % infinite set cannot be subset of finite one
2035 Code = fail.
2036 subset_of_explicit_set(C1,C2,Code,WF) :- nonvar(C1),
2037 is_cartesian_product_closure(C1,S11,S12),!,
2038 ((S11==[] ; S12==[]) -> Code=true /* we always have a subset */
2039 ; is_definitely_not_empty(S11),
2040 is_definitely_not_empty(S12), % only use optimisation if we know S11, S12 to be non-empty
2041 nonvar(C2), is_cartesian_product_closure(C2,S21,S22),
2042 Code = (kernel_objects:check_subset_of_wf(S11,S21,WF),
2043 kernel_objects:check_subset_of_wf(S12,S22,WF) )
2044 ).
2045 subset_of_explicit_set(Set1,Set2,Code,WF) :-
2046 nonvar(Set2),is_cartesian_product_closure(Set2,S21,S22),!,
2047 % TO DO: maybe don't do this if Set1 is avl_set ??
2048 debug_println(9,'Applying C <: S21*S22 <=> C : S21 <-> S22'),
2049 Code = bsets_clp:relation_over_wf(Set1,S21,S22,WF).
2050 subset_of_explicit_set(C1,C2,Code,WF) :- nonvar(C1), nonvar(C2),
2051 ? is_powerset_closure(C1,Constructor1,Set1),
2052 ? is_powerset_closure(C2,Constructor2,Set2),
2053 subset_constructor(Constructor1,Constructor2,R),
2054 !,
2055 Code = (R=pred_true, kernel_objects:check_subset_of_wf(Set1,Set2,WF)).
2056 subset_of_explicit_set(Set1,Set2,Code,WF) :-
2057 AllowRegularClosure=false,
2058 symbolic_subset_of_explicit_set(Set1,Set2,AllowRegularClosure,Code,WF).
2059
2060 symbolic_subset_of_explicit_set(Set1,Set2,AllowRegularClosure,Code,WF) :-
2061 %print_term_summary(subset(Set1,Set2)),nl,
2062 get_subset_counter_example_closure(Set1,Set2,NewP,NewT,NewB,AllowRegularClosure,DefResult),
2063 % {x|P1} <: {x|P2} <=> {x|P1 & not(P2)}={}
2064 !, %translate:print_bexpr(NewB),nl,
2065 (DefResult==definitely_non_empty -> Code = fail
2066 ; clean_up(NewB,[],CNewB), % can be useful to apply remove_member_comprehension
2067 Code = custom_explicit_sets:is_empty_closure_wf(NewP,NewT,CNewB,WF)).
2068
2069 % get closure representing the counter examples to Set1 <: Set2: i.e. elements in Set1 and not in Set2
2070 % used for symbolic treatment of subset, not_subset and test_subset
2071 % note: in case this fails subset_test1 will expand Set1
2072 % DefiniteResultFlag may return the information that the generated closure is definitely not empty
2073 % AllowRegularClosure=false means it will only be applied for symbolic or infinite closures
2074 get_subset_counter_example_closure(Set1,Set2,NewP,NewT,NewB,AllowRegularClosure,DefiniteResultFlag) :-
2075 get_closure(Set1,P1,T1,B1),
2076 get_subset_counter_aux(P1,T1,B1,Set2,NewP,NewT,NewB,AllowRegularClosure,DefiniteResultFlag).
2077
2078 get_subset_counter_aux(P1,T1,B1,Set2,NewP,NewT,NewB,AllowRegularClosure,DefRes) :-
2079 nonvar(Set2), is_definitely_finite(Set2), !,
2080 create_couple_term(P1,T1,P1Couple), % can currently still fail for more than 2 args
2081 (is_infinite_closure(P1,T1,B1)
2082 -> DefRes=definitely_non_empty % there are definitely counter examples as Set2 is finite
2083 ; AllowRegularClosure=true -> DefRes = unknown
2084 ? ; is_symbolic_closure(P1,T1,B1) -> DefRes=unknown
2085 ),
2086 NewP=P1, NewT=T1,
2087 % {x|P1} <: {a1,...} <=> {x|P1 & x /: {a1,...}}={}
2088 get_texpr_type(P1Couple,CoupleType1),
2089 VSet2 = b(value(Set2),set(CoupleType1),[]),
2090 create_texpr(not_member(P1Couple,VSet2),pred,[],NegPred2),
2091 conjunct_predicates([B1,NegPred2],NewB).
2092 get_subset_counter_aux(P1,T1,B1,Set2,NewP,NewT,NewB,AllowRegularClosure,unknown) :-
2093 get_closure(Set2,P2,T2,B2),
2094 (AllowRegularClosure=true -> true
2095 ; is_infinite_or_symbolic_closure(P1,T1,B1) -> true
2096 % should we also allow ??
2097 % ; is_symbolic_closure(P2,T2,B2)
2098 ),
2099 % not necessary maybe as subset_test1 only expands Set1
2100 % {x|P1} <: {x|P2} <=> {x|P1 & not(P2)}={}
2101 unify_closure_predicates(P1,T1,B1, P2,T2,B2 , NewP,NewT, NewB1,NewB2),
2102 create_negation(NewB2,NegNewB2),
2103 bsyntaxtree:conjunct_predicates([NewB1,NegNewB2],NewB).
2104
2105
2106 % get_closure or infinite global set:
2107 get_closure(V,_,_,_) :- var(V),!,fail.
2108 get_closure(closure(P,T,B),P,T,B).
2109 ?get_closure(global_set(G),P,T,B) :- is_infinite_global_set(G,Type),!,
2110 ID = '_zzzz_unary',
2111 TID = b(identifier(ID),Type,[]),
2112 TSet = b(value(global_set(G)),set(Type),[]),
2113 P = [ID], T=[Type], B= b(member(TID,TSet),pred,[prob_annotation('SYMBOLIC')]).
2114
2115
2116 subset_constructor(X,X,R) :- !,R=pred_true.
2117 subset_constructor(fin1,_,R) :- !,R=pred_true.
2118 subset_constructor(fin,pow,R) :- !,R=pred_true.
2119 subset_constructor(X,Y,R) :- strict_subset_constructor(X,Y),!,R=pred_true.
2120 subset_constructor(X,Y,R) :- strict_subset_constructor(Y,X),!,R=pred_false.
2121 % pow1,fin1 ; pow,fin ; and pow1,fin only ok if type infinite
2122 strict_subset_constructor(pow1,pow).
2123 strict_subset_constructor(fin1,fin).
2124
2125 % more rules for <->, +->, ...
2126 % what if same closure: then we also know it is a subset
2127
2128 % to be completed:
2129 % code that instantiates R to subset or not_subset, may have to delay
2130 test_subset_of_explicit_set(Set1,_,_,_,_) :- var(Set1),!,fail.
2131 test_subset_of_explicit_set(avl_set(A),Closure,R,WF,Code) :-
2132 is_interval_closure_or_integerset(Closure,Low,Up),!,
2133 Code=custom_explicit_sets:test_avl_in_interval(A,Low,Up,R,WF).
2134 test_subset_of_explicit_set(_,Set2,_,_,_) :- var(Set2),!,fail.
2135 test_subset_of_explicit_set(avl_set(A1),avl_set(A2),R,_WF,Code) :-
2136 Code = (custom_explicit_sets:check_avl_subset(A1,A2) -> R=pred_true ; R=pred_false).
2137 test_subset_of_explicit_set(global_set(G),Set2,R,_WF,Code) :-
2138 ? is_infinite_global_set(G,_), % TODO: we could extend this to other infinite sets
2139 is_definitely_finite(Set2), !,
2140 Code =(R=pred_false).
2141 test_subset_of_explicit_set(Set1,Set2,Res,WF,Code) :-
2142 AllowRegular=false,
2143 get_subset_counter_example_closure(Set1,Set2,NewP,NewT,NewB,AllowRegular,DefResult),
2144 % {x|P1} <: {x|P2} <=> {x|P1 & not(P2)}={}
2145 !,
2146 (DefResult==definitely_non_empty -> Code = (Res=pred_false)
2147 ; Code = custom_explicit_sets:test_empty_closure_wf(NewP,NewT,NewB,Res,WF)
2148 ).
2149 % TO DO: add is_cartesian_product_closure case
2150 is_definitely_finite([]).
2151 is_definitely_finite(avl_set(_)).
2152
2153 :- use_module(kernel_equality,[test_interval_subset_wf/6]).
2154
2155 :- public test_avl_in_interval/5. % used in test_subset_of_explicit_set
2156 % see also check_avl_in_interval(A,Low,Up), check_avl_not_in_interval(A,Low,Up).
2157 test_avl_in_interval(A,Low2,Up2,Res,WF) :-
2158 avl_min(A,int(Min)), % not needed if Low2==minus_inf
2159 avl_max(A,int(Max)), % not needed if Up2==inf
2160 test_interval_subset_wf(Min,Max,Low2,Up2,Res,WF).
2161
2162 % ----------------------
2163
2164 is_definitely_not_empty(X) :- nonvar(X),
2165 (X=[_|_] -> true
2166 ; is_custom_explicit_set(X), is_non_empty_explicit_set(X)).
2167
2168 % check if defnitely not empty and provide a witness
2169 is_definitely_not_empty_with_witness(X,El) :- nonvar(X),
2170 get_witness_element(X,El).
2171 get_witness_element([H|_],H).
2172 get_witness_element(avl_set(node(H,_True,_,_,_)),H).
2173 % TO DO: add global_set(GS),...
2174
2175 check_avl_subset(A1,A2) :- avl_max(A1,Max1), avl_max(A2,Max2),
2176 Max1@>Max2,!, % then A1 cannot be subset of A2
2177 fail.
2178 check_avl_subset(A1,A2) :-
2179 avl_min(A1,Cur1), avl_min(A2,Cur2),
2180 check_avl_subset_loop(Cur1,A1,Cur2,A2).
2181 check_avl_subset_loop(Cur1,AVL1,Cur2,AVL2) :-
2182 (Cur1 @> Cur2 -> avl_next(Cur2,AVL2,NC2), check_avl_subset_loop(Cur1,AVL1,NC2,AVL2)
2183 ; Cur1=Cur2 -> (avl_next(Cur1,AVL1,NC1)
2184 -> avl_next(Cur2,AVL2,NC2),
2185 check_avl_subset_loop(NC1,AVL1,NC2,AVL2)
2186 ; true /* all objects of AVL1 inspected */)
2187 ).
2188
2189 % check A <: Low..Up
2190 check_avl_in_interval(A,Low,Up) :- % does not have to delay: if we have minus_inf & inf they will be known straightaway
2191 (Low==minus_inf -> true
2192 ; avl_min(A,Min), kernel_objects:less_than_equal(int(Low),Min)),
2193 (Up==inf -> true
2194 ; avl_max(A,Max), kernel_objects:less_than_equal(Max,int(Up))).
2195
2196 % some experiments:
2197 % 1..x <: {1,2,3,5} & x>1 & !y.(y>x & y<10 => 1..y /<: {1,2,3,5})
2198 % {ss | ss <: 0..0 & ss /= {} & ss=0..max(ss)}
2199 % {ss | ss <: 0..0 & ss /= {} & ss=min(ss)..max(ss)} // does not work yet
2200 % x..x+1 <: {0,2,3,5}
2201 % x..x+2 <: {0,2,3,5} // does not work yet
2202 % r = {x|x:1..400 & x mod 3/=0} & res={v|v:0..1300 & v..v+1 <: r}
2203 % check Low..Up <: Avl
2204
2205 check_interval_in_custom_set(Low,Up,CS,WF) :-
2206 Low \== minus_inf,
2207 Up \== inf,
2208 b_interpreter_check:check_arithmetic_operator('<=',Low,Up,LeqRes),
2209 (var(LeqRes) -> get_binary_choice_wait_flag_exp_backoff(16,check_interval_in_custom_set,WF,WF2) ; true),
2210 check_interval_in_custom_set_aux(LeqRes,Low,Up,CS,WF2).
2211
2212 :- block check_interval_in_custom_set_aux(-,?,?,?,-).
2213 check_interval_in_custom_set_aux(pred_true,Low,Up,CS,_WF2) :-
2214 element_of_custom_set_wf(int(Low),CS,WF),
2215 element_of_custom_set_wf(int(Up),CS,WF),
2216 interval_in_avl_block(Low,Up,CS,WF).
2217 check_interval_in_custom_set_aux(pred_false,_Low,_Up,_CS,_WF2). % Interval is empty; but infinitely many solutions for Low and Up exist in principle
2218
2219 :- block interval_in_avl_block(-,?,?,?), interval_in_avl_block(?,-,?,?).
2220 interval_in_avl_block(Low,Up,CS,WF) :-
2221 Low1 is Low+1, interval_in_avl_loop(Low1,Up,CS,WF).
2222 interval_in_avl_loop(Low,Up,_CS,_WF) :- Low>=Up,!. % Lower bound and upper bound already checked
2223 interval_in_avl_loop(Low,Up,CS,WF) :-
2224 element_of_custom_set_wf(int(Low),CS,WF), L1 is Low+1,
2225 interval_in_avl_loop(L1,Up,CS,WF).
2226
2227
2228 :- public not_check_avl_subset/2. % used in not_subset_of_explicit_set_aux
2229 not_check_avl_subset(A1,A2) :- \+ check_avl_subset(A1,A2).
2230
2231 not_subset_of_explicit_set(S1,S2,Code,WF) :- nonvar(S1),
2232 ? not_subset_of_explicit_set_aux(S1,S2,Code,WF).
2233 not_subset_of_explicit_set_aux(avl_set(A),Closure,Code,_WF) :-
2234 is_interval_closure_or_integerset(Closure,Low,Up),!,
2235 Code=custom_explicit_sets:check_avl_not_in_interval(A,Low,Up).
2236 not_subset_of_explicit_set_aux(avl_set(A1),AVL2,Code,_WF) :-
2237 nonvar(AVL2),AVL2=avl_set(A2),
2238 Code = custom_explicit_sets:not_check_avl_subset(A1,A2).
2239 not_subset_of_explicit_set_aux(CS,AVL,Code,_WF) :-
2240 ? is_simple_infinite_set(CS),
2241 % TO DO: provide code for interval/NAT/INT /<: AVL
2242 simple_finite_set(AVL),
2243 !,
2244 Code = true. % G cannot be subset of finite set
2245 not_subset_of_explicit_set_aux(C1,C2,Code,WF) :- is_cartesian_product_closure(C1,S11,S12),
2246 ((S11==[] ; S12==[]) -> Code=fail /* we always have a subset */
2247 ; is_definitely_not_empty(S11),
2248 is_definitely_not_empty(S12), % only use optimisation if we know S11, S12 to be non-empty
2249 nonvar(C2), is_cartesian_product_closure(C2,S21,S22),
2250 Code = (kernel_objects:not_both_subset_of(S11,S12, S21,S22, WF))
2251 ), !.
2252 not_subset_of_explicit_set_aux(C1,C2,Code,WF) :- nonvar(C2),
2253 ? is_powerset_closure(C1,Constructor1,Set1),
2254 ? is_powerset_closure(C2,Constructor2,Set2),
2255 subset_constructor(Constructor1,Constructor2,R),!,
2256 Code = (R=pred_false -> true ; kernel_objects:not_subset_of_wf(Set1,Set2,WF)).
2257 not_subset_of_explicit_set_aux(Set1,Set2,Code,WF) :-
2258 AllowRegular=false,
2259 get_subset_counter_example_closure(Set1,Set2,NewP,NewT,NewB,AllowRegular,DefResult),
2260 % {x|P1} <: {x|P2} <=> {x|P1 & not(P2)}={}
2261 !,
2262 (DefResult==definitely_non_empty -> Code = true
2263 ; Code = custom_explicit_sets:is_non_empty_closure_wf(NewP,NewT,NewB,WF)
2264 ).
2265
2266
2267 :- public check_avl_not_in_interval/3. % used in not_subset_of_explicit_set_aux
2268 :- block check_avl_not_in_interval(?,-,?). % TO DO: use non-blocking version, minus_inf, and inf set directly
2269 check_avl_not_in_interval(A,Low,Up) :- avl_min(A,int(Min)),
2270 check_avl_not_in_interval4(Low,Up,A,Min).
2271
2272 check_avl_not_in_interval4(Low,_Up,_A,Min) :- Low \== minus_inf, Min < Low,!.
2273 check_avl_not_in_interval4(_Low,Up,A,_Min) :-
2274 Up \== inf, avl_max(A,Max),
2275 kernel_objects:less_than(int(Up),Max). % Up could still be a variable
2276
2277
2278 % checks for simple infinite sets, without Cartesian Product, ... decomposition
2279 ?is_simple_infinite_set(global_set(X)) :- !, is_infinite_global_set(X,_).
2280 is_simple_infinite_set(CS) :- is_interval_closure_or_integerset(CS,Low,Up), infinite_interval(Low,Up).
2281
2282 simple_finite_set(AVL) :- nonvar(AVL), (AVL=avl_set(_) -> true ; AVL = []).
2283
2284 % IMAGE [.]
2285 image_for_id_closure(closure(Par,Types,Body),Set,Res) :-
2286 is_full_id_closure(Par,Types,Body),!,
2287 Res=Set.
2288
2289 image_for_explicit_set(closure(Par,Types,Body),Set,Res,WF) :-
2290 ? image_for_closure(Par,Types,Body,Set,Res,WF).
2291 image_for_explicit_set(avl_set(A),Set,Res,WF) :- nonvar(Set),
2292 image_for_explicit_avl_set(A,Set,Res,WF).
2293
2294
2295 image_for_closure(Par,Types,Body,Set,Res,_WF) :-
2296 is_id_closure_over(Par,Types,Body,ID_Domain,Full),!,
2297 (Full=true -> Res=Set ; kernel_objects:intersection(ID_Domain,Set,Res)).
2298 % infinite function case dealt with in image1 in bsets_clp
2299 % TO DO: other closure(); Maybe special case if Set is an interval ?
2300 image_for_closure(Par,Types,Body,Set,Res,WF) :-
2301 is_closure1_value_closure(Par,Types,Body,VAL), % TODO: also detect reflexive closure, iteration (iterate(rel,k))
2302 % compute closure1(VAL)[Set]
2303 ? bsets_clp:image_for_closure1_wf(VAL,Set,Res,WF).
2304
2305 is_closure1_value_closure(Par,Types,Body,VAL) :-
2306 is_member_closure(Par,Types,Body,couple(A,A),MemSET), nonvar(MemSET),
2307 MemSET = closure(V), % this is the closure1 B operator !
2308 nonvar(V), V=b(value(VAL),_,_).
2309
2310 image_for_explicit_avl_set(A,Set,Res,_WF) :- % Set is nonvar
2311 is_interval_closure_or_integerset(Set,From1,To1),!,
2312 % Note: if From1, To1 not yet known we will block and not revert to other image calculation code
2313 % Important e.g. for performance of San Juan (AdaptedBModelPropCheck/acs_as_env_cfg_ipart.mch)
2314 %we used to check for: ground(From1),ground(To1),
2315 interval_image_for_explicit_avl_set(From1,To1,A,Set,Res).
2316 image_for_explicit_avl_set(A,Set,Res,WF) :-
2317 \+ bsets_clp:keep_symbolic(Set), % in this case we fall back to treatment in bsets_clp (image1)
2318 expand_custom_set_to_list_gg(Set,ESet,GG,image_for_explicit_avl_set),
2319 empty_avl(Empty),
2320 (GG=guaranteed_ground -> image_explicit_ground(ESet,A,Empty,Res,WF)
2321 ; image_explicit(ESet,A,Empty,Res,WF)).
2322
2323 :- block interval_image_for_explicit_avl_set(-,?,?,?,?),
2324 interval_image_for_explicit_avl_set(?,-,?,?,?).
2325 interval_image_for_explicit_avl_set(From1,To1,_A,_Set,Res) :-
2326 number(From1), number(To1), From1>To1,!,
2327 kernel_objects:empty_set(Res).
2328 interval_image_for_explicit_avl_set(From1,To1,A,_Set,Res) :-
2329 findall(Image-true, avl_image_interval(From1,To1, A,Image),ImageList),
2330 normalised_list_to_avl(ImageList,ImageAvl),
2331 ? equal_object(ImageAvl,Res).
2332
2333
2334 %! singleton_set(+Set,-Element).
2335 singleton_set(X,_) :- var(X),!,fail.
2336 singleton_set([H|T],R) :- T==[], R=H.
2337 singleton_set(avl_set(node(Y,_,_,empty,empty)),Y). % same as is_one_element_custom_set
2338
2339 is_one_element_custom_set(avl_set(node(Y,_,_,empty,empty)),Y).
2340 is_one_element_avl(node(Y,_,_,empty,empty),Y).
2341
2342 % requires El to be ground
2343 construct_one_element_custom_set(El,avl_set(AVL)) :-
2344 empty_avl(E),avl_store(El,E,true,AVL).
2345
2346 construct_avl_set(Avl,Res) :- empty_avl(Avl) -> Res = [] ; Res = avl_set(Avl).
2347
2348 :- block image_explicit(-,?,?,?,?).
2349 image_explicit([],_,Acc,Res,WF) :- !,
2350 construct_avl_set(Acc,AVLS),
2351 ? kernel_objects:equal_object_wf(Res,AVLS,image_explicit,WF).
2352 image_explicit([D1|T],AVLRelation,In,Out,WF) :- !,
2353 ground_value_check(D1,G1),
2354 ((var(T);T==[]) % TO DO: see below, make propagation also interesting in other circumstances
2355 -> must_be_in_domain_check(G1,D1,T,AVLRelation,In,Out,WF)
2356 ; true),
2357 ? image_explicit_aux(G1,D1,AVLRelation,T,In,Out,WF).
2358 image_explicit(Set,_,_,_,_) :- add_error_and_fail(image_explicit,'Unknown set: ',Set).
2359
2360 % a version of image_explicit where the list is guaranteed to be ground
2361 image_explicit_ground([],_,Acc,Res,WF) :- !,
2362 construct_avl_set(Acc,AVLS),
2363 kernel_objects:equal_object_wf(Res,AVLS,image_explicit,WF).
2364 image_explicit_ground([D1|T],AVLRelation,In,Out,WF) :- !,
2365 image_explicit_aux_ground(D1,AVLRelation,T,In,Out,WF).
2366 image_explicit_ground(Set,_,_,_,_) :- add_error_and_fail(image_explicit_ground,'Unknown set: ',Set).
2367
2368 :- block must_be_in_domain_check(-,?,?,?,?,-,?),
2369 must_be_in_domain_check(-,?,-,?,?,?,?).
2370 % if result requires at least one more element, then D must be in domain of Relation
2371 % ensures that we get a domain for j in x = {1|->2,2|->4, 4|->8} & x[{j}]={8}
2372 % we could even propagate using inverse of AVLRelation ?!
2373 must_be_in_domain_check(GroundD,D,T,AVLRelation,In,Out,WF) :-
2374 T==[], % apart from D, there are no more elements to be added
2375 var(GroundD), % otherwise we already have a value for D
2376 delta_witness(In,Out,Witness), % obtain at least one value that D must map to
2377 !,
2378 quick_propagation_element_information(avl_set(AVLRelation),(D,Witness),WF,_). % Witness avoids pending co-routines
2379 % TO DO: we could check that *all* elements of Out have this value
2380 % TO DO: below we could check that In is a subset of Out; e.g., for x = %i.(i:1..10|i+i) & x[{5,j,k}]={16,11}; we could also check that Out is subset of range of relation
2381 must_be_in_domain_check(_,_D,_T,_,_In,_Out,_). % :- print(must_be(D,T,In,Out)),nl.
2382
2383 % provide, if possible, a witness element in Out not in In
2384 delta_witness(In,Out,_Witness) :- (var(In) ; var(Out)),!,fail.
2385 %delta_witness(empty,Out,Witness) :- is_definitely_not_empty_with_witness(Out,Witness).
2386 delta_witness(In,Out,Witness) :-
2387 is_custom_explicit_set(Out,delta_witness),
2388 difference_of_explicit_set(Out,avl_set(In),Diff), % could be expensive to compute !? delay ? print(delta(Diff)),nl,
2389 is_definitely_not_empty_with_witness(Diff,Witness).
2390
2391
2392 :- block image_explicit_aux(-,?,?, ?,?,?,?). % we know that D1 is ground
2393 image_explicit_aux(_,D1,AVLRelation,T,In,Out,WF) :-
2394 all_images(D1,AVLRelation,NewImages), % compute AVLRelation[{D1}]
2395 add_to_avl(NewImages,In,In2),
2396 ? image_explicit(T,AVLRelation,In2,Out,WF).
2397 image_explicit_aux_ground(D1,AVLRelation,T,In,Out,WF) :-
2398 all_images(D1,AVLRelation,NewImages), % compute AVLRelation[{D1}]
2399 add_to_avl(NewImages,In,In2),
2400 image_explicit_ground(T,AVLRelation,In2,Out,WF).
2401
2402 all_images(From,AVLRelation,Images) :-
2403 findall(AY,avl_member_pair_arg1_ground(From,AY,AVLRelation),Images). % we know From ground and AY free variable
2404 % findall(AY,safe_avl_member_pair(From,AY,AVLRelation),Images). %
2405
2406 % compute relational composition ( ; ) if second arg is an AVL set
2407 % TO DO: add support for infinite closures; avoid expanding them [currently handled by symbolic composition in bsets_clp]
2408 rel_composition_for_explicit_set(Rel1,Rel2,Comp) :- nonvar(Rel2),
2409 Rel2=avl_set(A2), % TO DO: see if we can maybe convert Rel2 to AVL ?
2410 % \+ bsets_clp:keep_symbolic(Rel1), check already done in bsets
2411 expand_custom_set_to_list_gg(Rel1,Relation1,GG,rel_composition_for_explicit_set),
2412 empty_avl(In),
2413 (GG=guaranteed_ground
2414 -> rel_avl_compose2_ground(Relation1,A2,In,Comp)
2415 ; rel_avl_compose2(Relation1,A2,In,Comp)).
2416
2417 :- block rel_avl_compose2(-,?,?,?).
2418 rel_avl_compose2([],_,In,Res) :- construct_avl_set(In,A),
2419 ? equal_object(Res,A,rel_avl_compose2). % as we delay; we need to use equal_object at the end
2420 rel_avl_compose2([(X,Y)|T],A2,In,Out) :-
2421 when((ground(X),ground(Y)),
2422 (all_image_pairs_ground(X,Y,A2,ImagePairs),
2423 add_to_avl(ImagePairs,In,In2),
2424 rel_avl_compose2(T,A2,In2,Out))).
2425
2426 % a version where argument is guaranteed to be ground; no when-ground checks
2427 rel_avl_compose2_ground([],_,In,Res) :- construct_avl_set(In,A),
2428 equal_object(Res,A,rel_avl_compose2). % as we delay; we need to use equal_object at the end
2429 rel_avl_compose2_ground([(X,Y)|T],A2,In,Out) :-
2430 all_image_pairs_ground(X,Y,A2,ImagePairs),
2431 add_to_avl(ImagePairs,In,In2),
2432 rel_avl_compose2_ground(T,A2,In2,Out).
2433
2434 %all_image_pairs(From,To,AVLRelation,ImagePairs) :-
2435 % findall((From,AY),safe_avl_member_pair(To,AY,AVLRelation),ImagePairs).
2436 all_image_pairs_ground(From,To,AVLRelation,ImagePairs) :-
2437 findall((From,AY),avl_member_pair_arg1_ground(To,AY,AVLRelation),ImagePairs).
2438 % To: already in AVL format; AY is variable -> we could use avl_fetch_pair directly : findall((From,AY),avl_fetch_pair(To,AVLRelation,AY),ImagePairs).
2439
2440 /* succeeds if it can compute domain by some clever way */
2441 domain_of_explicit_set_wf(global_set(GS),_R,_) :- !,
2442 add_error_and_fail(domain_of_explicit_set_wf,'Cannot compute domain of global set: ',GS).
2443 domain_of_explicit_set_wf(freetype(GS),_R,_) :- !,
2444 add_error_and_fail(domain_of_explicit_set_wf,'Cannot compute domain of freetype: ',GS).
2445 domain_of_explicit_set_wf(avl_set(A),Res,_) :- !,
2446 domain_of_avl_set(A,Res).
2447 domain_of_explicit_set_wf(C,R,WF) :- dom_for_specific_closure(C,Dom,_,WF),!,
2448 Dom=R.
2449 domain_of_explicit_set_wf(C,R,_) :-
2450 ? dom_symbolic(C,CC),!,
2451 R=CC.
2452 domain_of_explicit_set_wf(closure(P,T,B),Res,WF) :-
2453 % does not seem to be reached, as dom_symbolic now seems to cover all cases
2454 expand_custom_set_wf(closure(P,T,B),EC,domain_of_explicit_set,WF),
2455 domain_of_list_blocking(EC,R),
2456 normalised_list_to_avl_when_ground(R,Res).
2457
2458 % avl tree is a relation with an integer domain
2459 %avl_integer_domain(node((int(_From),_KeyTo),_True,_,_L,_R)).
2460
2461 % the first clause is in principle faster
2462 % but we don't gain time compared to treatment in second clause; we just avoid building up the domain list
2463 %domain_of_avl_set(A,Res) :- avl_integer_domain(A),
2464 % \+ avl_tools:avl_height_less_than(A,10), % try and detect interval if height >= 10
2465 % avl_is_pf_with_interval_domain(A,First,Last),!,
2466 % construct_interval_closure(First,Last,Res).
2467 domain_of_avl_set(A,Res) :-
2468 avl_domain(A,EC), % -> expand_custom_set(avl_set(A),EC),
2469 domain_of_sorted_list(EC,SizeRes,R), % size of list can be smaller than A if we have a relation
2470 (SizeRes=size_res(Size,int(Last)), R=[int(First)-true|_],
2471 Size>1000,
2472 Size is Last+1-First % we have an interval; quite common that we have functions with intervals as domain
2473 -> debug_println(19,constructing_interval_for_domain(First,Last)),
2474 construct_interval_closure(First,Last,Res)
2475 ; ord_list_to_avlset(R,Res,domain)
2476 ).
2477
2478 % check if an AVL tree represents a function with an interval domain
2479 %avl_is_pf_with_interval_domain(AVL,Min,Max) :-
2480 % avl_min(AVL,(int(Min),_)),avl_max(AVL,(int(Max),_)),
2481 % Size is 1+Max-Min, avl_size_possible(AVL,Size),
2482 % is_avl_partial_function(AVL),
2483 % % now check real size
2484 % avl_size(AVL,Size).
2485
2486 % check if an avl represents a set of integers:
2487 avl_integer_set(node(int(_TOP),_True,_,_L,_R)).
2488
2489 % check if an avl set is an interval:
2490 avl_is_interval(AVL,Min,Max) :-
2491 avl_integer_set(AVL),
2492 avl_min(AVL,int(Min)),avl_max(AVL,int(Max)),
2493 Size is 1+Max-Min,
2494 avl_size_possible(AVL,Size),
2495 avl_size(AVL,Size).
2496
2497
2498
2499 :- use_module(bsyntaxtree,[create_typed_id/3]).
2500 dom_symbolic(closure(Paras,Types,Pred), Res) :-
2501 expand_pair_closure(Paras,Types,Pred,[X,Y],[TX,TY],NewPred),
2502 !, % single argument which is a pair
2503 % simply call code for range ; inverting arguments
2504 bsyntaxtree:check_used_ids_in_ast(Pred),
2505 bsyntaxtree:check_used_ids_in_ast(NewPred),
2506 ran_symbolic_closure(Y,[X],TY,[TX],NewPred,Res).
2507 dom_symbolic(closure(Paras,Types,Pred), Res) :-
2508 append(Xs,[Y],Paras), Xs \= [],
2509 append(TXs,[TY],Types),
2510 % simply call code for range ; inverting arguments
2511 ran_symbolic_closure(Y,Xs,TY,TXs,Pred,Res).
2512 % TO DO: allow computation if Paras is a single argument and more than pair
2513
2514 % just computes domain: it can also be successful for lambda closures
2515 dom_for_specific_closure(closure(P,T,Pred),Domain,Functionality,WF) :-
2516 dom_for_specific_closure_aux(P,T,Pred,Domain,Functionality,WF).
2517 dom_for_specific_closure_aux(P,T,Pred,Domain,Functionality,_WF) :-
2518 is_lambda_value_domain_closure(P,T,Pred, DomainValue,Expr),
2519 (preference(find_abort_values,full) -> bsyntaxtree:always_well_defined_or_disprover_mode(Expr)
2520 ; true),
2521 % Warning: this will lead to dom(%x.(x:1..3|1/0)) = 1..3 to be true; discarding WD condition
2522 % this is not as bad as {1|->2}(0) = 3 to be silently failing though; hence only done if TRY_FIND_ABORT = full
2523 !,
2524 Domain=DomainValue,
2525 Functionality=function(total).
2526 %dom_for_specific_closure_aux([ID],[Type],Pred,Domain,Functionality,_WF) :- Functionality=relation,
2527 % Pred = b(exists(Paras,ClosurePred),pred,Info1),
2528 % % dom({res|#(paras).(.... & res= domVal|->ran)}) = {res|#(paras).(.... & res= domVal)}
2529 % closures:select_equality(ClosurePred,ID,RHSExpr,Type,Info,RestPred),
2530 % RHSExpr = couple(DomValue,_),
2531 % closures:does_not_occur_in(ID,RestPred),
2532 % Type = couple(DomT,_),
2533 % TID = b(identifier(ID),DomT,[]),
2534 % % safe_create_texpr
2535 % conjunct_predicates([RestPred,b(equal(TID,DomValue),pred,[])],NewClosurePred),
2536 % NewPred = b(exists(Paras,NewClosurePred),pred,Info1),
2537 % Domain = closure([ID],[DomT],NewPred).
2538 dom_for_specific_closure_aux(P,T,Pred,Domain,Functionality,WF) :-
2539 dom_range_for_specific_closure2(P,T,Pred, Domain,_Range,domain_only,Functionality,WF).
2540 %TO DO treat overwrite closure dom(F1<+F2) = dom(F1) \/ dom(F2)
2541
2542 dom_for_lambda_closure(closure(P,T,Pred),Domain) :-
2543 is_lambda_value_domain_closure(P,T,Pred, DomainValue,_Expr),
2544 Domain=DomainValue.
2545
2546 % TO DO: add total functions
2547 %dom_for_specific_closure2([F],[T],
2548 % b(member(b(identifier(F),T,_), b(total_function(value(A),B),set(couple(DOM,RAN)),_)), pred,_) ,
2549 % A).
2550
2551 :- block domain_of_list_blocking(-,?).
2552 % the list will be sorted according to the term ordering for (_,_); hence it will
2553 % already be sorted for the projection onto the first element
2554 % maybe the speed difference is not worth it ??
2555 domain_of_list_blocking([],[]).
2556 domain_of_list_blocking([(A,_B)|T],[A-true|DT]) :- domain_blocking_aux(T,A,DT).
2557 :- block domain_blocking_aux(-,?,?).
2558 domain_blocking_aux([],_,[]).
2559 domain_blocking_aux([(A,_B)|T],Prev,Res) :-
2560 compare(Comp,A,Prev),
2561 (Comp = '='
2562 -> domain_blocking_aux(T,Prev,Res)
2563 ; Res = [A-true|DT],
2564 (Comp = '<' -> add_error_fail(custom_explicit_sets,'Domain list not_sorted: ',(A,Prev)) ; true),
2565 domain_blocking_aux(T,A,DT) ).
2566
2567 % and now a non-blocking version:
2568 domain_of_sorted_list([],size_res(0,'$none'),[]).
2569 domain_of_sorted_list([(A,_B)|T],Size,[A-true|DT]) :- domain_aux(T,A,DT,1,Size).
2570
2571 % TO DO: count length and determine when we have an interval
2572 domain_aux([],Prev,[],Acc,size_res(Acc,Prev)).
2573 domain_aux([(A,_B)|T],Prev,Res,SizeAcc,Size) :- SA1 is SizeAcc+1,
2574 compare(Comp,A,Prev),
2575 (Comp = '='
2576 -> domain_aux(T,Prev,Res,SA1,Size)
2577 ; Res = [A-true|DT],
2578 (Comp = '<' -> add_error_fail(custom_explicit_sets,'Domain list not_sorted: ',(A,Prev)) ; true),
2579 domain_aux(T,A,DT,SA1,Size) ).
2580
2581 /* succeeds if it can compute domain by some clever way */
2582 range_of_explicit_set_wf(global_set(GS),_R,_) :- !,
2583 add_error_and_fail(range_of_explicit_set_wf,'Cannot compute domain of global set: ',GS).
2584 range_of_explicit_set_wf(freetype(GS),_R,_) :- !,
2585 add_error_and_fail(range_of_explicit_set_wf,'Cannot compute domain of freetype: ',GS).
2586 range_of_explicit_set_wf(avl_set(A),Res,_) :- !,
2587 avl_domain(A,EC), % -> expand_custom_set(avl_set(A),EC),
2588 range(EC,R),
2589 normalised_list_to_avl(R,Res).
2590 range_of_explicit_set_wf(C,R,WF) :-
2591 ran_for_specific_closure(C,Ran,WF),!,
2592 Ran=R.
2593 range_of_explicit_set_wf(C,R,_) :-
2594 ran_symbolic(C,CC),!,
2595 R=CC.
2596 range_of_explicit_set_wf(closure(P,T,B),Res,WF) :-
2597 expand_custom_set_wf(closure(P,T,B),EC,range_of_explicit_set_wf,WF),
2598 % TO DO: it would be more useful here to directly just expand the projection onto the last component of P
2599 range_blocking(EC,R),
2600 normalised_list_to_avl_when_ground(R,Res).
2601
2602 % TO DO: in future it is maybe better to add an in_range_wf kernel predicate
2603 ran_symbolic(closure(Paras,Types,Pred), Res) :-
2604 ? (is_memoization_closure(Paras,Types,Pred,_)
2605 -> !,fail % memoization closures can never be dealt with symbolically; we need expansion
2606 ; true),
2607 expand_pair_closure(Paras,Types,Pred,[Y,X],[TY,TX],NewPred),!,
2608 % following test (1541) works with this: 2 : ran({y|#(x).(y = x |-> x + 2 & x : NATURAL)})
2609 ran_symbolic_closure(Y,[X],TY,[TX],NewPred,Res). %, print('res: '),translate:print_bvalue(Res),nl.
2610 ran_symbolic(closure([Y,X],[TY,TX],Pred), Res) :-
2611 ran_symbolic_closure(Y,[X],TY,[TX],Pred,Res).
2612 % TO DO: treat closures with more arguments: we need to quantify Y1,...Yn [Y1,...,Yn,X]
2613
2614 % Replace single Identifier YX of type pair by pair (Y,X) where Y,X are (fresh) variables not occuring in Pred
2615 % example: {y| #(x).(y = x |-> x + 2 & x : NATURAL)} --> {y__1,y__2|#(x).(y__1 |-> y__2 = x |-> x + 2 & x : NATURAL)}
2616 expand_pair_closure([YX],[TYX],Pred,[Y,X],[TY,TX],NewPred) :- TYX = couple(TY,TX),
2617 % Replace single ID YX of type pair by pair (Y,X) where Y,X are (fresh) variables not occuring in Pred
2618 % example: {y| #(x).(y = x |-> x + 2 & x : NATURAL)} --> {y__1,y__2|#(x).(y__1 |-> y__2 = x |-> x + 2 & x : NATURAL)}
2619 % following test (1541) works with this: 2 : ran({y|#(x).(y = x |-> x + 2 & x : NATURAL)})
2620 gensym:gensym(YX,Y),gensym:gensym(YX,X),
2621 create_typed_id(Y,TY,YTID), create_typed_id(X,TX,XTID),
2622 Pair = b(couple(YTID,XTID),TYX,[]),
2623 bsyntaxtree:replace_id_by_expr(Pred,YX,Pair,NewPred).
2624
2625 :- use_module(bsyntaxtree,[create_exists_opt_liftable/3]).
2626 %:- use_module(bsyntaxtree,[add_texpr_info_if_new/3]).
2627 ran_symbolic_closure(Y,Xs,TY,TXs,Pred,Res) :-
2628 % create closure for {Xs | #Y.(Pred)} where Pred uses Y|->Xs
2629 rename_ran_ids(Xs,Pred,[],XIDs,Pred2),
2630 create_typed_id(Y,TY,YTID),
2631 create_exists_opt_liftable([YTID],Pred2,Exists), % Y is liftable as the source is a closure with all ids
2632 %bsyntaxtree:check_used_ids_in_ast(Exists),
2633 %bsyntaxtree:create_exists_opt([YTID],[Pred2],Exists), %or
2634 %b_interpreter_components:create_and_simplify_exists([YTID],Pred2,Exists),
2635 %bsyntaxtree:add_texpr_info_if_new(Exists,allow_to_lift_exists,Exists2), % leads to pending co-routines in self_checks for bsets for apply_to;
2636 % Reason: the tests ground only det WF; without lifting the exists is fully evaluated (and its waitflags with prio 2 and higher grounded) as the wait arguments are ground; with lifting only the det WF is grounded leading to pending coroutines
2637 Res = closure(XIDs,TXs,Exists).
2638
2639
2640
2641 :- use_module(library(lists),[select/3]).
2642
2643 % rename lambda_results :
2644 rename_ran_ids([],Pred,_,[],Pred).
2645 rename_ran_ids([X|TX],Pred,Acc,[XID|TTX],Pred2) :-
2646 % in case X is _lambda_result_ we need to rename it as it then would not get enumerated !
2647 (X == '_lambda_result_'
2648 -> get_fresh_id('_was_lambda_result_',TX,Acc,XID),
2649 % we could remove lambda_result info field, but it will no longer match new id anyway
2650 ? rename_bt(Pred,[rename(X,XID)],Pred2),
2651 TTX=TX
2652 % TODO: maybe we should also remove the prob_annotation('LAMBDA-EQUALITY') info inside Pred for the ids and equality !?
2653 ? ; XID = X, rename_ran_ids(TX,Pred,[X|Acc],TTX,Pred2)
2654 ).
2655
2656 :- use_module(b_ast_cleanup,[get_unique_id/2]).
2657 get_fresh_id(ID,List1,List2,Res) :- nonmember(ID,List1), nonmember(ID,List2),!, Res=ID.
2658 get_fresh_id(ID,_,_,FRESHID) :- nl,print('*** VARIABLE_CLASH PREVENTED: '), print(ID),nl,
2659 get_unique_id(ID,FRESHID).
2660
2661 :- block range_blocking(-,?).
2662 range_blocking([],[]).
2663 range_blocking([(_A,B)|T],[B-true|DT]) :- range_blocking(T,DT).
2664 % and a non-blocking version:
2665 range([],[]).
2666 range([(_A,B)|T],[B-true|DT]) :- range(T,DT).
2667
2668 ran_for_specific_closure(closure(P,T,Pred),Range,WF) :-
2669 dom_range_for_specific_closure2(P,T,Pred, _Domain,Range,range_only,_Functionality,WF).
2670 %ran_for_specific_closure(closure_x(P,T,Pred,_Exp),Card,_) :- ran_for_specific_closure2(P,T,Pred,Card).
2671
2672 :- use_module(bsyntaxtree,[conjunct_predicates/2, disjunct_predicates/2, create_typed_id/3, get_texpr_type/2]).
2673 override_custom_explicit_set_wf(R,S,Res,WF) :- /* R <+ S */
2674 ? nonvar(R),override_custom_explicit_set_aux(R,S,Res,WF).
2675 override_custom_explicit_set_aux(CL,Rel2,Res,_WF) :-
2676 CL=closure(P0,T,B0),
2677 % TO DO: maybe call keep_symbolic in bsets_clp ??
2678 ( preferences:get_preference(convert_comprehension_sets_into_closures,true),
2679 (var(Rel2) -> true ; Rel2 \= avl_set(_)) % if Rel2 is avl_set then maybe better to compute explicitly; unless infinite
2680 ; quick_size_check_larger_than(Rel2,Size2,133) ->
2681 % if we have a large AVL set anyway; then allow expansion up to a larger limit; cf machine 670_002.mch
2682 % a lot of machines use A*B*C <+ {....} to more compactly define large explicit sets
2683 (Size2=inf -> Limit = 200000
2684 ; Limit is min(200000,Size2*150)), dont_expand_this_closure(P0,T,B0,Limit)
2685 ; dont_expand_this_closure(P0,T,B0) % use default limit
2686 ),
2687 !,
2688 ? rename_ran_ids(P0,B0,[],P,B), % any '_lambda_result_' id is no longer guaranteed to be assigned a value in all cases
2689 NewClosure=closure(P,T,NewBody),
2690 % B <+ Rel2 ---> NewBody = P:Rel2 or (prj1(P) /: dom(Rel2) & B)
2691 % TODO better? : %x.(x:Domain|IF x:dom(SFF) THEN SFF(x) ELSE DEFAULT)?
2692 generate_typed_id_pairs(P,T,NestedPairs),
2693 get_texpr_type(NestedPairs,PairsType),
2694 RelPairsType = set(PairsType),
2695 ValS = b(value(Rel2),RelPairsType,[]),
2696 MemS = b(member(NestedPairs,ValS),pred,[]), % P:Rel2
2697 get_prj1(NestedPairs,DomExpr),
2698 get_texpr_type(DomExpr,DomType),
2699 Domain = b(domain(ValS),set(DomType),[]), % TO DO: perform some optimisations like dom(%x.(P|E)) --> {x|P}
2700 %bsets_clp:domain_wf(Rel2,DomainOfRel2,WF), Domain = b(value(DomainOfRel2),DomType,[]), % this DOES NOT work for 1619, 1706 where override is used for infinite functions
2701 NotMemDomS = b(not_member(DomExpr,Domain),pred,[]), % prj1(P) /: dom(Rel2)
2702 conjunct_predicates([NotMemDomS,B],RHS),
2703 disjunct_predicates([MemS,RHS],NewBody),
2704 %print(override),nl, bsyntaxtree:check_used_ids_in_ast(NewBody),
2705 mark_closure_as_symbolic(NewClosure,Res).
2706 % TO DO: add a case where for second set we have: dont_expand_this_closure
2707 override_custom_explicit_set_aux(R,S,Res,WF) :-
2708 is_custom_explicit_set(R,override_custom_explicit_set),
2709 nonvar(S), is_custom_explicit_set(S,override_custom_explicit_set),
2710 %% hit_profiler:add_profile_hit(override(R,S),3), %%
2711 override_custom_explicit_set2(R,S,Res,WF).
2712
2713 override_custom_explicit_set2(R,S,Res,_WF) :- is_one_element_custom_set(S,(X,Y)),
2714 override_pair_explicit_set(R,X,Y,NewR),!,
2715 Res=NewR.
2716 % TO DO: if R is very large and S relatively small : iterate by calling override_pair_explicit_set
2717 override_custom_explicit_set2(R,S,Res,WF) :-
2718 expand_custom_set_wf(R,ER,override_custom_explicit_set_aux1,WF),
2719 expand_custom_set_wf(S,ES,override_custom_explicit_set_aux2,WF),
2720 override_list(ER,ES,LRes,Done),
2721 finish_restriction(Done,LRes,Res).
2722
2723 quick_size_check_larger_than(Set,Size,Limit) :-
2724 quick_custom_explicit_set_approximate_size(Set,Size),
2725 (is_inf_or_overflow_card(Size) -> true ; Size > Limit).
2726 get_prj1(b(couple(DomExpr,_),_,_),Prj1) :- !, Prj1 = DomExpr.
2727 get_prj1(BE,b(first_of_pair(BE),DT,[])) :- % some closures have a single identifier; we need to apply prj1
2728 BE = b(_E,couple(DT,_RT),_I).
2729
2730 % translate a parameter name and type list into a nested-pair value
2731 generate_typed_id_pairs([ID|IT],[Type|TT],Res) :- create_typed_id(ID,Type,TypedID),
2732 conv2(IT,TT,TypedID,Res).
2733 conv2([],[],X,X).
2734 conv2([ID|IT],[Type|TT],Acc,Res) :- create_typed_id(ID,Type,TypedID),
2735 get_texpr_type(Acc,AccType),
2736 Couple = b(couple(Acc,TypedID),couple(AccType,Type),[]),
2737 conv2(IT,TT,Couple,Res).
2738
2739 :- block override_list(-,?,?,?), override_list(?,-,?,?).
2740 override_list([],S,Res,Done) :- !, copy_to_true_list(S,Res,Done).
2741 override_list(R,[],Res,Done) :- !, copy_to_true_list(R,Res,Done).
2742 override_list([(From1,To1)|T1],[(From2,To2)|T2],Res,Done) :-
2743 (From1 @< From2
2744 -> Res = [(From1,To1)-true|TR], override_list(T1,[(From2,To2)|T2],TR,Done)
2745 ; From2 @< From1
2746 -> Res = [(From2,To2)-true|TR], override_list([(From1,To1)|T1],T2,TR,Done)
2747 ; override_list(T1,[(From2,To2)|T2],Res,Done)).
2748
2749 :- block copy_to_true_list(-,?,?).
2750 % add -true to get lists that can be converted to avl
2751 copy_to_true_list([],[],true).
2752 copy_to_true_list([H|T],[H-true|CT],Done) :- copy_to_true_list(T,CT,Done).
2753
2754 :- use_module(closures,[get_domain_range_for_closure_types/3]).
2755 % compute a closure with the functionality violations of a closure
2756 symbolic_functionality_check_closure(closure(P,T,B),closure([DID,ID1,ID2],[DomType,RanType,RanType],Body)) :-
2757 % construct {d,z_,z__| (d,z_):R & (d,z__):R & z_\= z__}
2758 generate_typed_id_pairs(P,T,NestedPairs),
2759 get_texpr_type(NestedPairs,PairsType),
2760 RelPairsType = set(PairsType),
2761 TRel = b(value(closure(P,T,B)),RelPairsType,[]),
2762 DID = '_domain', ID1 = '_zzzz_unary', ID2 = '_zzzz_binary',
2763 TDID = b(identifier(DID),DomType,[]),
2764 TID1 = b(identifier(ID1),RanType,[]),
2765 TID2 = b(identifier(ID2),RanType,[]),
2766 Mem1 = b(member( b(couple(TDID,TID1),PairsType,[]),TRel),pred,[]),
2767 Mem2 = b(member( b(couple(TDID,TID2),PairsType,[]),TRel),pred,[]),
2768 get_domain_range_for_closure_types(T,DomType,RanType),
2769 NeqRan = b(not_equal(TID1,TID2), pred, []),
2770 conjunct_predicates([Mem1,Mem2,NeqRan],Body),
2771 bsyntaxtree:check_used_ids_in_ast(Body).
2772 %bsyntaxtree:check_ast(Body).
2773
2774 % compute a closure with the injectivity violations of a closure
2775 symbolic_injectivity_check_closure(closure(P,T,B),closure([RID,ID1,ID2],[RanType,DomType,DomType],Body)) :-
2776 % construct {r,z_,z__| (z_,r):R & (z__,r):R & z_\= z__}
2777 generate_typed_id_pairs(P,T,NestedPairs),
2778 get_texpr_type(NestedPairs,PairsType),
2779 RelPairsType = set(PairsType),
2780 TRel = b(value(closure(P,T,B)),RelPairsType,[]), % what if closure body B has WD condition?
2781 RID = '_range', ID1 = '_zzzz_unary', ID2 = '_zzzz_binary',
2782 TRID = b(identifier(RID),RanType,[]),
2783 TID1 = b(identifier(ID1),DomType,[]),
2784 TID2 = b(identifier(ID2),DomType,[]),
2785 Mem1 = b(member( b(couple(TID1,TRID),PairsType,[]),TRel),pred,[]),
2786 Mem2 = b(member( b(couple(TID2,TRID),PairsType,[]),TRel),pred,[]),
2787 get_domain_range_for_closure_types(T,DomType,RanType),
2788 NeqRan = b(not_equal(TID1,TID2), pred, []),
2789 conjunct_predicates([Mem1,Mem2,NeqRan],Body),
2790 bsyntaxtree:check_used_ids_in_ast(Body).
2791 %bsyntaxtree:check_ast(Body).
2792
2793 % -------------------------
2794
2795
2796 % check whether we have a partial function
2797 is_avl_partial_function(empty) :- !.
2798 is_avl_partial_function(node((KeyFrom,_KeyTo),_True,_,L,R)) :- !,
2799 is_avl_partial_function2(L,'$$MIN$$',KeyFrom),
2800 is_avl_partial_function2(R,KeyFrom,'$$MAX$$').
2801 is_avl_partial_function(X) :- add_internal_error('Not avl_set or relation: ',is_avl_partial_function(X)),fail.
2802
2803 % we traverse the tree from top to bottom, keeping track of possible upper- and lower-bounds
2804 % if any value matches the upper or lower bound, the we do not have a partial function
2805 is_avl_partial_function2(empty,_,_).
2806 is_avl_partial_function2(node((KeyFrom,_KeyTo),_True,_,L,R),ParentFrom,ParentTo) :-
2807 KeyFrom \= ParentFrom, KeyFrom \= ParentTo,
2808 is_avl_partial_function2(L,ParentFrom,KeyFrom),
2809 is_avl_partial_function2(R,KeyFrom,ParentTo).
2810
2811 % the dual of the above, returning a counter example
2812 is_not_avl_partial_function(node((KeyFrom,_KeyTo),_True,_,L,R),DuplicateKey) :- !,
2813 (is_not_avl_partial_function2(L,'$$MIN$$',KeyFrom,DuplicateKey) -> true
2814 ; is_not_avl_partial_function2(R,KeyFrom,'$$MAX$$',DuplicateKey)).
2815 is_not_avl_partial_function2(node((KeyFrom,_KeyTo),_True,_,L,R),ParentFrom,ParentTo,DuplicateKey) :-
2816 ( KeyFrom = ParentFrom -> DuplicateKey=KeyFrom
2817 ; KeyFrom = ParentTo -> DuplicateKey=KeyFrom
2818 ; is_not_avl_partial_function2(L,ParentFrom,KeyFrom,DuplicateKey) -> true
2819 ; is_not_avl_partial_function2(R,KeyFrom,ParentTo,DuplicateKey) -> true).
2820
2821
2822 % check whether we have a function which is total over a given domain; both as AVL sets
2823 is_avl_total_function_over_domain(empty,empty) :- !.
2824 is_avl_total_function_over_domain(AVLFun,AVLDom) :-
2825 avl_domain(AVLFun,FunList),
2826 avl_domain(AVLDom,DomList),
2827 is_avl_total_fun2(FunList,DomList).
2828
2829 is_avl_total_fun2([],[]).
2830 is_avl_total_fun2([(From,_To)|FT],[From|DomT]) :- is_avl_total_fun2(FT,DomT).
2831
2832
2833 %not_is_avl_partial_function(AVLF) :- \+ is_avl_partial_function(AVLF).
2834
2835 :- use_module(kernel_equality,[membership_test_wf/4]).
2836 % check whether an AVL Relation is not over a specific domain & range
2837 is_not_avl_relation_over_domain_range(AVLRel,Domain,Range,WF) :- AVLRel \= empty,
2838 avl_min_pair(AVLRel,RFrom,RTo),
2839 membership_test_wf(Domain,RFrom,MemRes,WF),
2840 is_not_avl_rel_dom1(MemRes,RFrom,RTo,AVLRel,Domain,Range,WF).
2841
2842 :- block is_not_avl_rel_dom1(-, ?,?,?,?,?,?).
2843 is_not_avl_rel_dom1(pred_false,_,_,_,_,_,_WF).
2844 is_not_avl_rel_dom1(pred_true,RFrom,RTo,AVLRel,Domain,Range,WF) :-
2845 membership_test_wf(Range,RTo,MemRes,WF),
2846 is_not_avl_rel_dom2(MemRes,RFrom,RTo,AVLRel,Domain,Range,WF).
2847
2848 :- block is_not_avl_rel_dom2(-, ?,?,?,?,?,?).
2849 is_not_avl_rel_dom2(pred_false,_,_,_,_,_,_WF).
2850 is_not_avl_rel_dom2(pred_true,RFrom,RTo,AVLRel,Domain,Range,WF) :-
2851 avl_next((RFrom,RTo),AVLRel,(RFrom2,RTo2)),
2852 membership_test_wf(Domain,RFrom2,MemRes,WF),
2853 is_not_avl_rel_dom1(MemRes,RFrom2,RTo2,AVLRel,Domain,Range,WF).
2854
2855 % check whether an AVL Relation is not over a specific range
2856 is_not_avl_relation_over_range(AVLRel,Range,WF) :- AVLRel \= empty,
2857 avl_min_pair(AVLRel,RFrom,RTo),
2858 membership_test_wf(Range,RTo,MemRes,WF),
2859 is_not_avl_rel_ran2(MemRes,RFrom,RTo,AVLRel,Range,WF).
2860
2861 :- block is_not_avl_rel_ran2(-, ?,?,?,?,?).
2862 is_not_avl_rel_ran2(pred_false,_,_,_,_,_WF).
2863 is_not_avl_rel_ran2(pred_true,RFrom,RTo,AVLRel,Range,WF) :-
2864 avl_next((RFrom,RTo),AVLRel,(RFrom2,RTo2)),
2865 kernel_equality:membership_test_wf(Range,RTo2,MemRes,WF),
2866 is_not_avl_rel_ran2(MemRes,RFrom2,RTo2,AVLRel,Range,WF).
2867
2868 % check whether we have a relation
2869 is_avl_relation(node((_KeyFrom,_KeyTo),_True,_,_,_)).
2870
2871 % check whether a Relation has all its range elments in a certain Range (not necessarily AVL)
2872 % TO DO: if Domain is an interval: we could take avl_min and avl_max and rely on lexicographic ordering
2873 is_avl_relation_over_domain(AVL,IntervalClosure,_WF) :-
2874 is_interval_closure_or_integerset(IntervalClosure,Low,Up),!,
2875 ((avl_min(AVL,(int(ALow),_)), avl_max(AVL,(int(AUp),_)))
2876 -> cs_greater_than_equal(ALow,Low), cs_greater_than_equal(Up,AUp) %,print(ok),nl
2877 ; (AVL=empty -> true ; add_error_and_fail(is_avl_relation_over_domain,'Not a relation with integer domain: ',AVL))).
2878 is_avl_relation_over_domain(_,Domain,_) :-
2879 quick_is_definitely_maximal_set(Domain),!.
2880 %is_definitely_maximal_set(Domain),!.
2881 is_avl_relation_over_domain(AVL,Domain,WF) :- is_avl_relation_over_domain2(AVL,Domain,WF).
2882 is_avl_relation_over_domain2(empty,_,_).
2883 is_avl_relation_over_domain2(node((KeyFrom,_KeyTo),_,_,L,R), Domain,WF) :-
2884 is_avl_relation_over_domain2(L, Domain,WF),
2885 is_avl_relation_over_domain2(R, Domain,WF),
2886 kernel_objects:check_element_of_wf(KeyFrom,Domain,WF).
2887
2888 % : faster to check than is_definitely_maximal_set
2889 quick_is_definitely_maximal_set(X) :- nonvar(X),
2890 quick_is_definitely_maximal_set_aux(X).
2891 quick_is_definitely_maximal_set_aux(global_set(GS)) :-
2892 nonvar(GS),is_maximal_global_set(GS).
2893 quick_is_definitely_maximal_set_aux(avl_set(AVL)) :-
2894 quick_definitely_maximal_set_avl(AVL).
2895
2896 % check whether a Relation has all its range elments in a certain Range (not necessarily AVL)
2897
2898
2899
2900 is_avl_relation_over_range(empty,_,_) :- !.
2901 is_avl_relation_over_range(_,Range,_) :-
2902 %quick_is_definitely_maximal_set(Range),
2903 is_definitely_maximal_set(Range),
2904 !.
2905 is_avl_relation_over_range(AVL,Range,WF) :- is_avl_relation_over_range2(AVL,Range,WF).
2906
2907 is_avl_relation_over_range2(empty,_,_).
2908 is_avl_relation_over_range2(node((_KeyFrom,KeyTo),_,_,L,R), Range,WF) :-
2909 is_avl_relation_over_range(L, Range,WF),
2910 kernel_objects:check_element_of_wf(KeyTo,Range,WF),
2911 is_avl_relation_over_range2(R, Range,WF).
2912
2913 % safe version of is_avl_sequence, does not throw error when type cannot be a sequence
2914 safe_is_avl_sequence(empty) :- !.
2915 safe_is_avl_sequence(node((int(KeyFrom),_KeyTo),_True,_,L,R)) :- !,
2916 is_avl_sequence2(L,0,KeyFrom),
2917 is_avl_sequence2(R,KeyFrom,'$$MAX$$').
2918
2919 is_avl_sequence(empty) :- !.
2920 is_avl_sequence(node((int(KeyFrom),_KeyTo),_True,_,L,R)) :- !,
2921 is_avl_sequence2(L,0,KeyFrom),
2922 is_avl_sequence2(R,KeyFrom,'$$MAX$$').
2923 is_avl_sequence(X) :- add_error_and_fail(is_avl_sequence,'Not avl_set or sequence: ',X).
2924
2925 % we traverse the tree from top to bottom, keeping track of possible upper- and lower-bounds
2926 % if any value matches the upper or lower bound, the we do not have a partial function
2927 is_avl_sequence2(empty,X,Y) :-
2928 (Y=='$$MAX$$' -> true ; Y is X+1). % otherwise there is a gap in the sequence
2929 is_avl_sequence2(node((int(KeyFrom),_KeyTo),_,_,L,R),ParentFrom,ParentTo) :-
2930 KeyFrom > ParentFrom, KeyFrom \= ParentTo,
2931 is_avl_sequence2(L,ParentFrom,KeyFrom),
2932 is_avl_sequence2(R,KeyFrom,ParentTo).
2933
2934 % for performance: it is not worthwhile to make a version that checks that
2935 % we have a sequence over a range using a single traversal
2936
2937
2938 % get avl_sequence elements as sorted list (without indices)
2939 % used by external function REPLACE
2940 get_avl_sequence(AVL,SeqList) :-
2941 get_avl_sequence_dcg(AVL,SeqList,[]).
2942
2943 get_avl_sequence_dcg(empty) --> [].
2944 get_avl_sequence_dcg(node((int(_),SeqEl),_True,_,L,R)) -->
2945 get_avl_sequence_dcg(L),
2946 [SeqEl],
2947 get_avl_sequence_dcg(R).
2948
2949
2950 % ---------------------------
2951 prefix_of_custom_explicit_set(avl_set(A),MinIndex,Result,WF) :-
2952 size_of_avl_sequence(A,Size,WF),
2953 (MinIndex > Size
2954 -> add_wd_error('index larger than size of sequence in prefix_sequence (/|\\)! ', '>'(MinIndex,Size),WF)
2955 % ; MinIndex = 0 -> Result = [] % case already treated in bsets_clp
2956 ; MinIndex = Size -> Result=avl_set(A)
2957 ; prefix_of_custom_explicit_set2(A,MinIndex,OrdList,[]),
2958 ord_list_to_avlset(OrdList,Result,prefix_of_custom_explicit_set)
2959 ).
2960 prefix_of_custom_explicit_set2(empty,_MaxIndex) --> {true}.
2961 prefix_of_custom_explicit_set2(node((int(KeyFrom),KeyTo),_True,_,L,R),MaxIndex) -->
2962 ({KeyFrom = MaxIndex}
2963 -> prefix_of_custom_explicit_set2(L,MaxIndex), [((int(KeyFrom),KeyTo)-true)]
2964 ; {KeyFrom > MaxIndex} -> prefix_of_custom_explicit_set2(L,MaxIndex)
2965 ; prefix_of_custom_explicit_set2(L,MaxIndex), [((int(KeyFrom),KeyTo)-true)],
2966 prefix_of_custom_explicit_set2(R,MaxIndex)
2967 ).
2968
2969 % size is only well-defined for sequences:
2970 size_of_custom_explicit_set(avl_set(AVL),int(Size),WF) :- size_of_avl_sequence(AVL,Size,WF).
2971 size_of_custom_explicit_set(closure(P,T,B),Res,WF) :-
2972 is_lambda_value_domain_closure(P,T,B, DomainValue,_Expr),
2973 kernel_cardinality_attr:finite_cardinality_as_int_wf(DomainValue,Res,WF).
2974 size_of_avl_sequence(AVL,Size,WF) :-
2975 preference(find_abort_values,true),
2976 \+ is_avl_sequence(AVL),!,
2977 avl_max_pair(AVL,int(Sz),_),
2978 add_wd_error('Applying size to a value which is not a sequence',b(value(avl_set(AVL)),seq(any),[]),WF),
2979 Size=Sz. % other calls to size_of_avl_sequence currently expect a value
2980 size_of_avl_sequence(AVL,Size,WF) :-
2981 % TO DO: checking minimum is 1?
2982 avl_max_pair(AVL,int(Sz),_),
2983 avl_height(AVL,H), % we cannot compute the height together with max; we need the longest path!
2984 get_min_max_card(H,MinSize,MaxSize),
2985 %avl_size(AVL,Real),format('AVL SeqSize: ~w, height: ~w, real size:~w, min: ~w, max: ~w~n',[Sz,H,Real,MinSize,MaxSize]),
2986 (Sz > MaxSize
2987 -> add_wd_error('Applying size to a value which is not a sequence (maximum index too large)',b(value(avl_set(AVL)),seq(any),[]),WF),
2988 avl_size(AVL,Size)
2989 % triggered by e.g. size({0|->1,0|->2,1|->3}) or size({0|->1,1|->2,3|->3,1|->22,1|->23,1|->24,1|->25,1|->26})
2990 ; Sz < MinSize
2991 -> add_wd_error('Applying size to a value which is not a sequence (maximum index too small)',b(value(avl_set(AVL)),seq(any),[]),WF),
2992 avl_size(AVL,Size)
2993 % triggered by e.g. size([0,2,2,2] |> {2})
2994 ; Size=Sz).
2995
2996 get_min_max_card(Height,MinCard,MaxCard) :-
2997 % page 460, Knuth 3: The height of a balanced tree with N internal nodes always lies between lg(N+1) and 1.4405 lg(N+2) - 0.3277
2998 MaxCard is 2^Height - 1,
2999 % 1.618034 is golden ration phi 0.5(1+sqrt(5)) , 2.236068 is sqrt(5)
3000 % proof in Knuth uses fact: N > phi^(h+2)/sqrt(5) - 2
3001 MinCard is ceiling((1.61803398875**(Height+2)) / 2.2360679775 - 2).
3002
3003 % check if a candidate size is possible given height:
3004 avl_size_possible(AVL,SizeCandidate) :-
3005 avl_height(AVL,Height), % TO DO: restrict to something like log2 of Height
3006 get_min_max_card(Height,MinCard,MaxCard),
3007 MinCard =< SizeCandidate,
3008 SizeCandidate =< MaxCard.
3009
3010
3011 suffix_of_custom_explicit_set(avl_set(A),MinIndex,Result,WF) :-
3012 size_of_avl_sequence(A,Size,WF),
3013 (MinIndex > Size
3014 -> add_wd_error('index larger than size of sequence in suffix_sequence (\\|/)! ', '>'(MinIndex,Size),WF)
3015 % ; MinIndex = 0 -> Result = avl_set(A) % case already treated in bsets_clp
3016 ; MinIndex = Size -> Result=[]
3017 ; suffix_of_custom_explicit_set2(A,MinIndex,OrdList,[]),
3018 ord_list_to_avlset(OrdList,Result,suffix_of_custom_explicit_set)
3019 ).
3020 suffix_of_custom_explicit_set2(empty,_MinIndex) --> {true}.
3021 suffix_of_custom_explicit_set2(node((int(KeyFrom),KeyTo),_True,_,L,R),MinIndex) -->
3022 ({KeyFrom =< MinIndex} -> suffix_of_custom_explicit_set2(R,MinIndex)
3023 ; {ShiftedKeyFrom is KeyFrom-MinIndex},
3024 ({KeyFrom =:= MinIndex+1}
3025 -> {true} ; suffix_of_custom_explicit_set2(L,MinIndex)),
3026 [((int(ShiftedKeyFrom),KeyTo)-true)],
3027 suffix_of_custom_explicit_set2(R,MinIndex)
3028 ).
3029
3030 shift_avl_sequence_to_ord_list(AVL,Offset,ShiftedOrdList) :-
3031 avl_to_list(AVL,List),shift_seq(List,Offset,ShiftedOrdList).
3032 % it does not seem to be worth to use avl_to_list_dcg_offset or a variation thereof
3033 % it is not really slower to do two traversals (avl_to_list and shift_seq)
3034
3035 shift_seq([],_,[]).
3036 shift_seq([(int(I),Val)-true|T],Offset,[(int(NI),Val)-true|ST]) :- NI is I+Offset,
3037 shift_seq(T,Offset,ST).
3038
3039 :- use_module(debug).
3040 concat_custom_explicit_set(avl_set(S1),Seq2,Res,WF) :- nonvar(Seq2), Seq2=avl_set(S2),
3041 size_of_avl_sequence(S1,Size1,WF),
3042 shift_avl_sequence_to_ord_list(S2,Size1,OL2),
3043 % if OL2 is small we could use avl_store like in append_custom_explicit_set
3044 %avl_to_list(S1,OL1),
3045 avl_to_list_dcg(S1,NewOrdList,OL2), % use OL2 rather than [] as tail
3046 %append(OL1,OL2,NewOrdList), % we could avoid traversing OL1 again by doing a custom avl_to_list/3 which specifies tail
3047 ord_list_to_avlset(NewOrdList,Res,concat). % , print_term_summary(res_concat(Res)).
3048
3049 % a DCG version of avl_to_list; allows to call it with something else than [] as tail
3050 avl_to_list_dcg(empty) --> [].
3051 avl_to_list_dcg(node(Key,Val,_,L,R)) -->
3052 avl_to_list_dcg(L), [(Key-Val)],
3053 avl_to_list_dcg(R).
3054
3055 /* conc: concatenation of sequence of sequences (general_concat) */
3056 conc_custom_explicit_set(avl_set(AVL),Res) :-
3057 avl_min_pair(AVL,int(ONE),First),
3058 conc2_cs(First,ONE,AVL,0,NewOrdList),
3059 ord_list_to_avlset(NewOrdList,Res,conc).
3060
3061 conc2_cs(Seq,NrSeq,AVL,Offset,OrdList) :-
3062 add_seq(Seq,Offset,OrdList,NewOffset,TailOrd),
3063 (avl_next((int(NrSeq),Seq),AVL,(int(N2),Seq2))
3064 -> conc2_cs(Seq2,N2,AVL,NewOffset,TailOrd)
3065 ; TailOrd=[]).
3066
3067 add_seq([],Offset,OrdRes,NewOffset,TailOrdRes) :- NewOffset=Offset, TailOrdRes=OrdRes.
3068 add_seq(avl_set(ASeq),Offset,OrdRes,NewOffset,TailOrd) :-
3069 avl_to_list_dcg_offset(ASeq,Offset,NrEls,OrdRes,TailOrd), NewOffset is Offset+NrEls.
3070
3071 % a version of avl_to_list for sequences which autmatically adds an offset
3072 avl_to_list_dcg_offset(empty,_,0) --> [].
3073 avl_to_list_dcg_offset(node((int(Idx),El),Val,_,L,R),Offset,NrEls) -->
3074 {NIdx is Idx+Offset},
3075 avl_to_list_dcg_offset(L,Offset,N1),
3076 [((int(NIdx),El)-Val)],
3077 avl_to_list_dcg_offset(R,Offset,N2), {NrEls is N1+N2+1}.
3078
3079 prepend_custom_explicit_set(avl_set(S1),ObjectToPrepend,Res) :-
3080 %hit_profiler:add_profile_hit(prepend_custom_explicit_set(avl_set(S1),ObjectToPrepend,Res)),
3081 element_can_be_added_or_removed_to_avl(ObjectToPrepend),
3082 shift_avl_sequence_to_ord_list(S1,1,OL1),
3083 ord_list_to_avlset([(int(1),ObjectToPrepend)-true|OL1],Res).
3084
3085 append_custom_explicit_set(avl_set(S1),ObjectToAppend,Res,WF) :-
3086 element_can_be_added_or_removed_to_avl(ObjectToAppend), % implies that ObjectToAppend is ground
3087 size_of_avl_sequence(S1,Size1,WF), NewSize is Size1+1,
3088 add_ground_element_to_explicit_set_wf(avl_set(S1),(int(NewSize),ObjectToAppend),Res,WF).
3089
3090 % compute tail of a sequence and also return first element
3091 tail_sequence_custom_explicit_set(avl_set(S1),First,Res,Span,WF) :-
3092 shift_avl_sequence_to_ord_list(S1,-1,NewOrdList),
3093 (NewOrdList = [(int(0),First)-true|TailOL] -> ord_list_to_avlset(TailOL,Res)
3094 ; add_wd_error_span('tail argument is not a sequence!', avl_set(S1),Span,WF)
3095 % add_error_fail(tail_sequence,'tail applied to ', NewOrdList))
3096 ).
3097 last_sequence_explicit_set(avl_set(AVL),Last) :-
3098 avl_max_pair(AVL,int(_Sz),Last).
3099 % TO DO: we could compute height of the path to max H, then check that Sz is in 2**(H-1)+1 .. 2**(H+1)-1 ?
3100 %first_sequence_explicit_set(avl_set(AVL),First) :- % not used anymore; apply_to used instead
3101 % avl_min_pair(AVL,int(_One),First).
3102
3103 % compute front and return last element at the same time
3104 front_sequence_custom_explicit_set(avl_set(AVL),Last,Res) :-
3105 avl_max_pair(AVL,int(Size),Last),
3106 direct_remove_element_from_avl(AVL, (int(Size),Last), Res). % we know Last is already in AVL-converted format
3107
3108
3109 reverse_custom_explicit_set(avl_set(AVL),Res) :-
3110 avl_to_list_dcg_offset(AVL,0,Size,List,[]),
3111 S1 is Size+1,
3112 reverse_list(List,S1,[],RevList),
3113 ord_list_to_avl(RevList,RevAVL),
3114 Res=avl_set(RevAVL).
3115
3116 reverse_list([],_,Acc,Acc).
3117 reverse_list([(int(Idx),El)-V|T],S1,Acc,Res) :-
3118 NewIdx is S1 - Idx,
3119 reverse_list(T,S1,[(int(NewIdx),El)-V|Acc],Res).
3120
3121 % check if a relation is injective ; compute range at the same time; note AVL can be empty
3122 is_injective_avl_relation(AVL,RangeRes) :-
3123 avl_domain(AVL,ElList),
3124 empty_avl(EmptyAcc),
3125 is_avl_inj_list(ElList,EmptyAcc,Range),
3126 construct_avl_set(Range,RangeRes).
3127
3128 is_avl_inj_list([],Range,Range).
3129 is_avl_inj_list([(_From,To)|T],InRange,OutRange) :-
3130 (avl_fetch(To,InRange) -> fail /* this is not an injection; a range element is repeated */
3131 ; avl_store(To,InRange,true,InRange1),
3132 is_avl_inj_list(T,InRange1,OutRange)
3133 ).
3134
3135 % Example predicates that work with code below:
3136 % card(id((1..1000)*(1..1000))~)=1000*1000
3137 % card(((1..1000)*(1..1000))~)=1000*1000
3138 invert_explicit_set(global_set(GS),_R) :- !,
3139 add_error_and_fail(invert_explicit_set,'Cannot compute inverse of global set: ',GS).
3140 invert_explicit_set(freetype(GS),_R) :- !,
3141 add_error_and_fail(invert_explicit_set,'Cannot compute inverse of freetype: ',GS).
3142 invert_explicit_set(closure([P1,P2],[T1,T2],Clo),R) :- !,
3143 % TODO: also invert closures with single argument or more arguments
3144 % e.g., {a,b,c|a=1 & b=1 &c:1..10}~ = {c,ab|ab=(1,1) & c:1..10}
3145 R = closure([P2,P1],[T2,T1],Clo).
3146 invert_explicit_set(closure([P1],[T1],Clo),R) :-
3147 is_member_closure_with_info([P1],[T1],Clo,_Type,Info,MEM),
3148 invert_member_predicate(MEM,T1,InvMEM,InvT1),!,
3149 construct_member_closure(P1,InvT1,Info,InvMEM,R).
3150 invert_explicit_set(C,AVL) :- expand_custom_set(C,EC,invert_explicit_set), %% convert to AVL ?
3151 inv_and_norm(EC,AVL).
3152
3153 invert_member_predicate(cartesian_product(A,B),couple(TA,TB),
3154 cartesian_product(B,A),couple(TB,TA)).
3155 invert_member_predicate(identity(A),TA,identity(A),TA).
3156
3157
3158 :- block inv_and_norm(-,?).
3159 inv_and_norm(EC,AVL) :- inv(EC,R,Done), norm(Done,R,AVL).
3160
3161 :- block norm(-,?,?).
3162 norm(_,R,AVL) :- normalised_list_to_avl(R,AVL).
3163
3164 :- block inv(-,?,?).
3165 inv([],[],done).
3166 inv([(A,B)|T],[(B,A)-true|DT],Done) :- inv(T,DT,Done).
3167
3168
3169
3170 % checks whether a ground value is in the domain of an AVL relation
3171 check_in_domain_of_avlset(X,AVL) :- convert_to_avl_inside_set(X,AX),!,
3172 ? (avl_fetch_pair(AX,AVL,_) -> true ; fail).
3173 check_in_domain_of_avlset(X,AVL) :-
3174 print('### could not convert arg for check_in_domain_of_avlset'),nl,
3175 print(X),nl,
3176 safe_avl_member_pair(X,_,AVL).
3177
3178 % checks whether a ground value is in the domain of an AVL relation and has only one solution
3179 check_unique_in_domain_of_avlset(X,AVL) :- convert_to_avl_inside_set(X,AX),!,
3180 ? avl_fetch_pair(AX,AVL,AY1),!,
3181 ? (avl_fetch_pair(AX,AVL,AY2), AY1 \= AY2 -> fail
3182 ; true).
3183
3184
3185 % utility to check if for a value there is at most one matching element in an AVL set
3186 % optimized for function application
3187 at_most_one_match_possible(Element,AVL,Matches) :- nonvar(Element),
3188 Element=(Index,_Rest), % Function Application; TO DO: does this cover all func. appl ?
3189 element_can_be_added_or_removed_to_avl(Index),
3190 convert_to_avl_inside_set(Index,AX), % is ground and normalised ?
3191 % TO DO: check AVL size ? Check other patterns ?
3192 findall((AX,Match),avl_tools:avl_fetch_pair(AX,AVL,Match),Matches),
3193 Matches \= [_,_|_].
3194
3195
3196
3197 apply_to_avl_set(A,X,Y,Span,WF) :-
3198 ground_value_check(X,GroundX),
3199 ? apply_to_avl_set_aux(A,X,Y,GroundX,Span,WF).
3200
3201 apply_to_avl_set_aux(A,X,Y,GroundX,Span,WF) :- nonvar(GroundX),!,
3202 ? apply_check_tuple(X,Y,A,Span,WF). % we could call apply_check_tuple_ground to avoid one ground test
3203 % We know that A is a function: we can deterministically apply if X is ground;
3204 % if Y is ground this is only the cases for injective functions
3205 apply_to_avl_set_aux(A,X,Y,GroundX,Span,WF) :-
3206 %(preference(data_validation_mode,true); % we now reduce priority of backpropagation below
3207 preference(find_abort_values,true),
3208 % do not try inverse propagation onto argument X of function application A(X) = Y
3209 !,
3210 avl_approximate_size(A,3,ApproxSizeA),
3211 apply_check_tuple_delay(X,Y,A,ApproxSizeA,Span,WF,GroundX,_,_).
3212 apply_to_avl_set_aux(A,X,Y,GroundX,Span,WF) :-
3213 ground_value_check(Y,GroundY),
3214 avl_approximate_size(A,3,ApproxSizeA), % exact size for height <= 3; approximate size above
3215 (ApproxSizeA < 4 -> SPrio=ApproxSizeA ; SPrio is ApproxSizeA * 10), % magic number; ideally we want X or Y to be known beforehand; if none are known we may miss WD errors and may enumerate useless intermediate variables
3216 get_bounded_wait_flag(SPrio,apply_to_explicit(X,Y),WF,WF1), % this only makes sense if X is a domain variable to be enumerated
3217 %propagate_avl_element_information((X,Y),A,ApproxSizeA,WF), % could be done; but would prevent WD problems from being detected
3218 % this waitflag is used when neither X nor Y are ground;
3219 % quite often not much is gained by enumerating possible values; unless X or Y are constrained or trigger other computations
3220 % WSz is 10*ApproxSizeA, % magic value
3221 %(ApproxSizeA > 100 -> InversePrioSize = 4
3222 % ; avl_range_size_and_propagate_element_info(A,X,Y,RSize), InversePrioSize is ApproxSizeA // RSize), % we could probably compute the exact worst case with the same complexity
3223 % delay_get_wait_flag(GroundY,GroundX,WF1,InversePrioSize,apply_to_explicit_inverse(X,Y),WF,WF2),
3224 %(ApproxSizeA<4000 -> propagate_apply(X,Y,A,ApproxSizeA,WF,GroundX,GroundY) ; true),
3225 apply_check_tuple_delay(X,Y,A,ApproxSizeA,Span,WF,GroundX,WF1,GroundY),
3226 (preference(use_clpfd_solver,false) -> true
3227 % should we also check: preference(find_abort_values,true)?
3228 ; get_wait_flag0(WF,WF0),
3229 ? propagate_apply(X,Y,A,ApproxSizeA,WF,WF0,GroundX,WF1,GroundY)).
3230
3231 :- block propagate_apply(?,?,?,?,?,-,?,?,?).
3232 propagate_apply(X,Y,AVL,ApproxSizeA,WF,_,GroundX,WF1,GroundY) :-
3233 var(GroundX), var(WF1), var(GroundY),
3234 (preference(disprover_mode,true)
3235 -> XX=X % this will also instantiate X and prevent finding WD errors
3236 ; (ApproxSizeA<128 -> true
3237 ; preference(solver_strength,SS), ApproxSizeA < 128+SS*100), % up until 4000 it may make sense to constrain Y
3238 preference(data_validation_mode,false), % note: this can slow down ProB, e.g., test 1105; hence allow disabling it
3239 preference(find_abort_values,false), % TO DO: v = %x.(x:1..20|x+x) & {y,z|y<4 & z=v(y) & (y:{-1,2})} =res: no WD ERROR found
3240 propagate_value(X,XX) % only instantiate X, propagation only makes sense for propagate_avl_element_information_small, as otherwise only X will be bounded
3241 ),
3242 !,
3243 ? propagate_avl_element_information_direct((XX,Y),AVL,ApproxSizeA,WF).
3244 propagate_apply(_,_,_,_,_,_,_,_,_).
3245
3246 % only propagate in one direction to allow to find WD errors but also prevent pending co-routines/constraints
3247 :- block propagate_value(-,?).
3248 propagate_value(int(X),R) :- !,
3249 (
3250 %%integer(X) -> R=int(X) ; % relevant for SWI 8.5.10 and older where fd_set fails for integers, see test 788; should be fixed in next release
3251 propagate_fd_dom(X,RX), R=int(RX), propagate_atomic_value(X,RX)
3252 ).
3253 propagate_value(fd(X,T),R) :- !,
3254 (
3255 %%integer(X) -> R=fd(X,T) ; % for SWI 8.5.10 and older, see above
3256 propagate_fd_dom(X,RX), R=fd(RX,T), propagate_atomic_value(X,RX)
3257 ).
3258 propagate_value((X1,X2),R) :- !, R=(RX1,RX2), propagate_value(X1,RX1), propagate_value(X2,RX2).
3259 propagate_value(pred_true,R) :- !, if(R=pred_true,true,debug_println(9,function_arg_outside_domain(pred_true))).
3260 propagate_value(pred_false,R) :- !, if(R=pred_false,true,debug_println(9,function_arg_outside_domain(pred_false))).
3261 propagate_value(string(X),R) :- !, R=string(RX),propagate_atomic_value(X,RX).
3262 propagate_value(X,RX) :- equal_object(X,RX). % TO DO: get rid of this: this propagates and prevents finding WD errors
3263 :- block propagate_atomic_value(-,?).
3264 propagate_atomic_value(X,Y) :-
3265 if(X=Y,true,debug_println(9,function_arg_outside_domain(X))).
3266
3267 %propagate_fd_dom(X,RX) :- integer(X),!,RX=X. % relevant for SWI 8.5.10 and older where fd_set fails for integers
3268 propagate_fd_dom(X,RX) :- fd_set(X,Dom),in_set(RX,Dom).
3269
3270
3271 /*
3272 :- block propagate_apply(-,?,?,?,?,-,-).
3273 % call propagate as soon as we know something about the function argument and we do not propgagate completely using GroundX/Y anyway
3274 propagate_apply(X,Y,AVL,Size,WF,GroundX,GroundY) :- print(prop_apply(Size,GroundX,GroundY,X,Y)),nl,
3275 (nonvar(GroundX) -> true ; nonvar(GroundY) -> true
3276 ; propagate_avl_element_information((X,Y),AVL,Size,WF)).
3277
3278 % get the waitflag when first WF set and other two not
3279 :- block delay_get_wait_flag(-,-,-,?,?,?,?).
3280 delay_get_wait_flag(_,WF1,WF2, _,_,_,_) :- (nonvar(WF1);nonvar(WF2)),!. % DO NOTHING
3281 delay_get_wait_flag(_,_,_,Prio,Info,WF,WF2) :- get_wait_flag(Prio,Info,WF,WF2).
3282 */
3283
3284 :- block apply_check_tuple_delay(?,?,?, ?,?,?, -,-,-).
3285 apply_check_tuple_delay(X,Y,AVL,_ApproxSizeA,Span,WF,GroundX,WF1,_) :-
3286 (nonvar(GroundX);nonvar(WF1)),!,
3287 ? apply_check_tuple(X,Y,AVL,Span,WF).
3288 apply_check_tuple_delay(X,Y,AVL,ApproxSizeA,Span,WF,_GroundX,_WF1,_GroundY) :-
3289 % Y is ground; try to do an inverse function lookup
3290 ? inverse_apply_ok(Y,X,AVL,ApproxSizeA,Span),
3291 !,
3292 % print(inverse_apply(Y,X,ApproxSizeA,_GroundX)),nl,
3293 inverse_get_possible_values(X,Y,AVL,Res),
3294 Res=avl_set(InvAVL), % if empty set : we fail
3295 (preference(data_validation_mode,true),
3296 avl_approximate_size(InvAVL,10,ApproxSize),
3297 ApproxSize>1
3298 -> A2 is (ApproxSize*15*ApproxSize)//ApproxSizeA, % used to be A2 is ApproxSize*100,
3299 A22 is max(A2,ApproxSize),
3300 ? (get_inversion_penalty(Span)
3301 -> A23 is A22 * 100 %, add_message(f,'Inversion Penalty: ',Y:A22,Span)
3302 ; A23=A22),
3303 % give lower priority for backwards propagation, upto 15 times if no reduction from backwards propagation
3304 % but also take into account how much we reduce the size by inverting
3305 % relevant for, e.g., Machines_perf_0111/Thales_All/rule_OPS_SDS_3940.mch
3306 % or rule_OPS_SDS_6496 -> 15 instead of 150 improves performance
3307 get_bounded_wait_flag(A23,element_of_avl_inverse_apply_ok(X),WF,WF2),
3308 % does not call propagate_avl_element_information(X,InvAVL,ApproxSize,WF) or avl_to_table
3309 element_of_avl_set_wf3(X,InvAVL,ApproxSize,WF2,WF) % TODO: pass GroundX
3310 %apply_check_tuple_delay(X,Y,AVL,ApproxSizeA,Span,WF,GroundX,WF1,_) % now wait on WF1 or GroundX
3311 ? ; element_of_avl_set_wf(InvAVL,X,WF)
3312 ).
3313 apply_check_tuple_delay(X,Y,AVL,ApproxSizeA,Span,WF,GroundX,WF1,_GroundY) :-
3314 apply_check_tuple_delay(X,Y,AVL,ApproxSizeA,Span,WF,GroundX,WF1,_). % now wait on WF1 or GroundX
3315
3316 % check if the function call was annotated as not suitable for backwards inverse function lookup propagation
3317 get_inversion_penalty(span_predicate(b(_Function,_,Info),_LS,_S)) :- !,
3318 ? get_inversion_penalty(Info).
3319 get_inversion_penalty(Info) :-
3320 ? member(prob_annotation('INVERSION_PENALTY'),Info).
3321
3322 inverse_get_possible_values(X,Y,AVL,Res) :-
3323 get_template(X,XX,_),
3324 copy_term(XX,XX_Copy), % avoid that findall instantiates X
3325 % TODO: copy_value_term similar to ground_value to avoid traversing avl_sets; but usually X is not a set
3326 findall(XX_Copy, safe_avl_member_default((XX_Copy,Y),AVL), PossibleValues),
3327 PossibleValues \= [], % fail straightaway
3328 sort(PossibleValues,SPV),
3329 % length(SPV,Len),print(inverse_image(Y,Len)),nl, print_term_summary(apply_check_tuple_delay(X,Y,AVL)),nl,
3330 convert_to_avl(SPV,Res).
3331
3332 % is it ok to compute inverse ? only makes sense if AVL tree not too big and quite functional
3333 inverse_apply_ok(pred_true,_,_AVL,ApproxSizeA,_) :- !, % only two values possible, probably half of AVL will be returned
3334 ApproxSizeA < 1023. % corresponds to avl_height < 10
3335 inverse_apply_ok(pred_false,_,_AVL,ApproxSizeA,_) :- !,ApproxSizeA < 1023.
3336 % TO DO: other small types, such as fd(_,_)
3337 inverse_apply_ok(_,_,_AVL,ApproxSizeA,_) :- ApproxSizeA < 255,!.
3338 inverse_apply_ok(_,X,_AVL,ApproxSizeA,Span) :- ApproxSizeA < 65535, % corresponds Height < 16
3339 (preference(data_validation_mode,true) ->
3340 (preference(solver_strength,SS), ApproxSizeA < 16383+SS -> true
3341 ; perfmessage(inverse,'Function call not inverted (increase SOLVER_STRENGTH to enable this), approximate function size: ',ApproxSizeA,Span),
3342 fail
3343 )
3344 ; true),
3345 ? quick_non_ground_check(X).
3346 %inverse_apply_ok(_,_,_,_).
3347
3348 % sometimes the ground_value_check co-routine hasn't grounded GroundX yet ! so do a quick check
3349 quick_non_ground_check(X) :- var(X),!.
3350 quick_non_ground_check([]) :- !,fail.
3351 quick_non_ground_check(avl_set(_)) :- !,fail.
3352 quick_non_ground_check(pred_true) :- !,fail.
3353 quick_non_ground_check(pred_false) :- !,fail.
3354 quick_non_ground_check(int(X)) :- !,var(X).
3355 quick_non_ground_check(string(X)) :- !,var(X).
3356 quick_non_ground_check(fd(X,T)) :- !,(var(X) ; var(T)).
3357 quick_non_ground_check((A,B)) :- !, (quick_non_ground_check(A) -> true ; quick_non_ground_check(B)).
3358 quick_non_ground_check(_). % assume it is non ground
3359
3360
3361
3362 % apply_check_tuple is allowed to enumerate: either X is ground or Y is ground
3363 apply_check_tuple(X,Y,A,Span,WF) :-
3364 ground_value(X),
3365 convert_to_avl_inside_set_wf(X,AX,WF),!, % we can do optimized lookup + checking in one go (but avl_apply only does partial check)
3366 avl_apply(AX,A,XY,Span,WF),
3367 ? kernel_objects:equal_object_wf(XY,Y,apply_check_tuple,WF).
3368 :- if(environ(no_wd_checking, true)).
3369 apply_check_tuple(X,Y,A,_Span,WF) :- safe_avl_member_default_wf((X,Y),A,WF).
3370 :- else.
3371 apply_check_tuple(X,Y,A,_Span,WF) :- preferences:preference(find_abort_values,false), !,
3372 ? safe_avl_member_default_wf((X,Y),A,WF).
3373 apply_check_tuple(X,Y,A,Span,WF) :- !,
3374 if(safe_avl_member_default_wf((X,XY),A,WF), % does not detect abort errors if X unbound
3375 kernel_objects:equal_object_wf(XY,Y,apply_check_tuple_avl,WF),
3376 add_wd_error_span('function applied outside of domain (#4): ','@fun'(X,avl_set(A)),Span,WF)).
3377 :- endif.
3378
3379
3380 % ------------------------------------------
3381
3382
3383 :- use_module(b_global_sets,[b_type2_set/2]).
3384 :- use_module(bsyntaxtree,[rename_bt/3]).
3385 union_of_explicit_set(global_set(GS),_,R) :- is_maximal_global_set(GS), !,
3386 R= global_set(GS). /* global_set is already maximal */
3387 union_of_explicit_set(freetype(GS),_,R) :- !, R= freetype(GS). /* freetype is already maximal */
3388 union_of_explicit_set(closure(P,T,B),_,R) :- is_definitely_maximal_closure(P,T,B), !,
3389 R= closure(P,T,B). /* global_set is already maximal */
3390 union_of_explicit_set(_,S2,R) :- is_definitely_maximal_set(S2),!, % will also look at AVL set
3391 R=S2.
3392 union_of_explicit_set(S1,S2,R) :- nonvar(S2), S2 = [], !, R=S1.
3393 union_of_explicit_set(S1,S2,_) :- (var(S1);var(S2)),!,fail. % then we cannot compute it here
3394 union_of_explicit_set(S2,S1,R) :-
3395 is_not_member_value_closure(S1,TYPE,MS1), nonvar(MS1), is_efficient_custom_set(MS1),
3396 % also works if S2 is complement closure
3397 difference_of_explicit_set(MS1,S2,Diff),!,
3398 construct_complement_closure_if_necessary(Diff,TYPE,R).
3399 union_of_explicit_set(avl_set(A1),S2,R) :- !, union_of_avl_set(S2,A1,R).
3400 union_of_explicit_set(S1,S2,R) :-
3401 ? is_not_member_value_closure(S1,TYPE,MS1), nonvar(MS1), is_efficient_custom_set(MS1),
3402 difference_of_explicit_set(MS1,S2,Diff),!,
3403 construct_complement_closure_if_necessary(Diff,TYPE,R).
3404 union_of_explicit_set(S1,avl_set(A2),R) :- !, union_of_avl_set(S1,A2,R).
3405 union_of_explicit_set(I1,I2,R) :- is_interval_closure_or_integerset(I1,From1,To1), ground(From1), ground(To1),
3406 is_interval_closure_or_integerset(I2,From2,To2), ground(From2), ground(To2),
3407 !,
3408 (union_of_interval(From1,To1,From2,To2,FromRes,ToRes)
3409 -> construct_interval_closure(FromRes,ToRes,R)
3410 ; small_enough_for_expansion(From1,To1),small_enough_for_expansion(From2,To2) ->
3411 % do not attempt union_of_closure below
3412 expand_interval_closure_to_avl(From1,To1,R1), R1=avl_set(A1), % empty interval already dealt with above !?
3413 expand_interval_closure_to_avl(From2,To2,R2), R2=avl_set(A2), % Note: unification after call as expand_interval calls equal_object (which gets confused by partially instantiated avl_set(_))
3414 union_of_avl(A1,A2,ARes),R=avl_set(ARes) /* AVL not normalised */
3415 ; transform_global_sets_into_closure(I1,closure(Par,T,Body)),
3416 union_of_closure(I2,Par,T,Body,R)
3417 ).
3418 union_of_explicit_set(closure(P,T,B),C2,Res) :-
3419 union_of_closure(C2,P,T,B,Res).
3420
3421 small_enough_for_expansion(From1,To1) :- number(To1), number(From1), To1-From1<250.
3422
3423 :- use_module(bsyntaxtree,[extract_info/2, extract_info_wo_used_ids/2, extract_info/3, rename_bt/3, replace_id_by_expr/4]).
3424
3425 union_of_closure(global_set(X),P,T,B,Res) :- !, transform_global_sets_into_closure(global_set(X),C),
3426 union_of_closure(C,P,T,B,Res).
3427 union_of_closure(closure(P2,T2,B2),P,T,B,Res) :- !,
3428 % T2 should be equal to T, module seq(_) <-> set(couple(integer,_))
3429 unify_closure_predicates(P,T,B, P2,T2,B2 , NewP,NewT, NewB1,NewB2),
3430 debug:debug_println(9,union_of_two_closures(P,P2,NewP,NewT)),
3431 extract_info(B,B2,NewInfo),
3432 construct_disjunct(NewB1,NewB2,Disj),
3433 Res = closure(NewP,NewT,b(Disj,pred,NewInfo)).
3434
3435 % rename predicates of two closures so that they work on common closure parameter ids
3436 % and can then be either joined by conjunction or disjunction
3437 unify_closure_predicates(P,T,B, P2,T2,B2 , NewP,NewT, NewB1,NewB2) :-
3438 length(P,Len1), length(P2,Len2),
3439 (Len1=Len2
3440 -> generate_renaming_list(P,P2,RL),
3441 rename_bt(B2,RL,NewB2),
3442 NewP=P, NewT=T, NewB1 = B
3443 ; Len1 < Len2 -> unify_clos_lt(P,T,B, P2,T2,B2 , NewP,NewT, NewB1,NewB2)
3444 ; unify_clos_lt(P2,T2,B2, P,T,B , NewP,NewT, NewB2,NewB1) % inverted the predicate
3445 ).
3446
3447 % TO DO: generalize: currently only works for single identifier on left
3448 % but works for id(NATURAL) \/ %x.(x<0|-x) or abs = id(NATURAL) \/ %x.(x<0|-x) & abs(2)=a2 & abs(-2)=am2
3449 unify_clos_lt([ID1],[couple(_,_)],B, P2,T2,B2 , NewP,NewT, NewB1,NewB2) :-
3450 rename_lambda_result_id(P2,B2,P3,B3),
3451 create_couple_term(P3,T2,Pair),
3452 replace_id_by_expr(B,ID1,Pair,NewB1),
3453 NewP=P3, NewT=T2, NewB2=B3.
3454
3455 % _lambda_result_ id is not enumerated, hence we have to avoid inserting such ids into NewB1 as part of the pPair
3456 rename_lambda_result_id(['_lambda_result_',ID2],B2,[FRESHID,ID2],B3) :- !,get_unique_id('_RANGE_',FRESHID),
3457 rename_bt(B2,[rename('_lambda_result_',FRESHID)],B3).
3458 rename_lambda_result_id([ID1,'_lambda_result_'],B2,[ID1,FRESHID],B3) :- !,get_unique_id('_RANGE_',FRESHID),
3459 rename_bt(B2,[rename('_lambda_result_',FRESHID)],B3).
3460 rename_lambda_result_id(P2,B2,P2,B2).
3461
3462 % translate a list of atomic ids and a list of types into a couple-term
3463 create_couple_term([ID1],[T1],Res) :- !,
3464 create_texpr(identifier(ID1),T1,[],Res).
3465 create_couple_term([ID1,ID2],[T1,T2],Res) :-
3466 bsyntaxtree:create_couple(b(identifier(ID1),T1,[]),b(identifier(ID2),T2,[]),Res).
3467 % TODO: extend for more than two args
3468
3469 generate_renaming_list([],[],[]).
3470 generate_renaming_list([ID|T],[ID2|T2],RL) :-
3471 (ID==ID2 -> generate_renaming_list(T,T2,RL)
3472 ; RL = [rename(ID2,ID)|RL2],
3473 generate_renaming_list(T,T2,RL2)).
3474
3475
3476 % a more clever way of constructing a disjunct; factor out common prefixes
3477 % (A & B1) or (A1 & B2) <=> A1 & (B1 or B2)
3478 % TO DO: we should try and get the leftmost basic conjunct !
3479 /* construct_disjunct(b(conjunct(A1,A2),pred,IA), b(conjunct(B1,B2),pred,_IB), Res) :-
3480
3481 print('TRY DISJUNCT FACTOR: '), translate:print_bexpr(A1),nl,
3482 translate:print_bexpr(B1),nl,
3483 same_texpr_body(A1,B1),!,
3484 print('DISJUNCT FACTOR: '), translate:print_bexpr(A1),nl,
3485 Res = conjunct(A1,b(Disj,pred,IA)),
3486 construct_disjunct(A2,B2,Disj).
3487 */
3488 construct_disjunct(A,B,disjunct(A,B)).
3489
3490 :- use_module(btypechecker,[couplise_list/2]).
3491 % TO DO: quick_check if AVL A1 is maximal ?
3492 union_of_avl_set(avl_set(A2),A1,R) :- !, union_of_avl(A1,A2,ARes), R=avl_set(ARes). /* AVL not normalised */
3493 union_of_avl_set(I2,A1,R) :- is_interval_closure_or_integerset(I2,From2,To2), !,
3494 ground(From2), ground(To2), % we can only compute it if bounds known
3495 (avl_min(A1,int(Min)), low_border(From2,Min,FromRes), avl_max(A1,int(Max)), up_border(To2,Max,ToRes)
3496 -> /* AVL contained (almost) in Interval */
3497 construct_interval_closure(FromRes,ToRes,R)
3498 ; \+ small_interval(From2,To2) ->
3499 transform_global_sets_into_closure(I2,closure(Par,T,Body)), % we may have something like NATURAL1,...
3500 union_of_avl_set_with_closure(Par,T,Body,A1,R)
3501 ; expand_and_convert_to_avl_set(I2,A2,union_of_avl_set,'? \\/ ARG'), % can generate ARel=empty; will fail if not possible to convert
3502 union_of_avl(A1,A2,ARes), R=avl_set(ARes)
3503 ).
3504 union_of_avl_set(closure(Par,T,Body),A1,Res) :- is_infinite_or_symbolic_closure(Par,T,Body),!,
3505 % TO DO: what if we are in SYMBOLIC mode and the type of T is infinite; maybe we should also keep the union symbolic ?? (cf Ticket/Georghe1)
3506 union_of_avl_set_with_closure(Par,T,Body,A1,Res).
3507 union_of_avl_set(S2,A1,Res) :-
3508 S2 \= freetype(_),
3509 ground_value(S2), % could be a closure
3510 !,
3511 (try_expand_and_convert_to_avl_set(S2,A2,union)
3512 -> union_of_avl(A1,A2,ARes), Res=avl_set(ARes) /* AVL not normalised */
3513 ; S2=closure(Par,T,Body),
3514 union_of_avl_set_with_closure(Par,T,Body,A1,Res)).
3515
3516 try_expand_and_convert_to_avl_set(S2,A2,Source) :-
3517 % false: do not add enumeration warning events as errors
3518 catch_enumeration_warning_exceptions(expand_and_convert_to_avl_set(S2,A2,Source,''),fail,false,ignore(Source)).
3519
3520 % try expanding to list, but catch enumeration warnings and fail if they do occur
3521 % used by override(...)
3522 %try_expand_custom_set_to_list(CS,_,_,_) :- nonvar(CS),CS=global_set(GS),is_infinite_global_set(GS,_),
3523 % !,
3524 % fail.
3525 try_expand_custom_set_to_list(CS,_,_,_) :- nonvar(CS),
3526 (is_symbolic_closure(CS) ; is_infinite_explicit_set(CS)),
3527 !, % we could also check is_symbolic_closure
3528 fail.
3529 try_expand_custom_set_to_list(CS,List,Done,Source) :-
3530 % false: do not add enumeration warning events as errors
3531 catch_enumeration_warning_exceptions(expand_custom_set_to_list(CS,List,Done,Source),fail,false,ignore(Source)).
3532
3533
3534 small_interval(From,To) :- number(From), number(To), To-From < 10000.
3535
3536 union_of_avl_set_with_closure(Par,T,Body,A1,Res) :-
3537 Body = b(_,BodyT,_),
3538 setup_typed_ids(Par,T,TypedPar),
3539 btypechecker:couplise_list(TypedPar,TypedCPar),
3540 generate_couple_types(TypedCPar,ParExpr,ParType),
3541 debug:debug_println(9,union_of_avl_and_infinite_closure(Par,T,BodyT)),
3542 BodyAvl = b(member(ParExpr,b(value(avl_set(A1)),set(ParType),[])),pred,[]),
3543 extract_info_wo_used_ids(Body,NewInfo),
3544 Res = closure(Par,T,b(disjunct(BodyAvl,Body),pred,NewInfo)).
3545 % mark_closure_as_symbolic(closure(Par,T,b(disjunct(BodyAvl,Body),pred,NewInfo)),Res).
3546
3547 low_border(Low,AVLMin,R) :- geq_inf(AVLMin,Low),!,R=Low.
3548 low_border(Low,AVLMin,R) :- number(Low),AVLMin is Low-1,R=AVLMin. % extend lower border by one
3549 up_border(Up,AVLMax,R) :- geq_inf(Up,AVLMax),!,R=Up.
3550 up_border(Up,AVLMax,R) :- number(Up),AVLMax is Up+1,R=AVLMax. % extend upper border by one
3551
3552
3553 setup_typed_ids([],[],[]).
3554 setup_typed_ids([ID|TI],[Type|TT],[b(identifier(ID),Type,[])|BT]) :- setup_typed_ids(TI,TT,BT).
3555
3556 generate_couple_types(couple(A,B),b(couple(TA,TB),Type,[]),Type) :- !, Type = couple(TTA,TTB),
3557 generate_couple_types(A,TA,TTA),
3558 generate_couple_types(B,TB,TTB).
3559 generate_couple_types(b(X,T,I),b(X,T,I),T).
3560
3561
3562 % try to see if two intervals can be unioned into a new interval
3563 union_of_interval(F1,T1,F2,T2,FR,TR) :-
3564 geq_inf(F2,F1), geq_inf(T1,T2),!,FR=F1,TR=T1. % interval [F2,T2] contained in [F1,T1]
3565 union_of_interval(F2,T2,F1,T1,FR,TR) :- geq_inf(F2,F1), geq_inf(T1,T2),!,FR=F1,TR=T1. % see above
3566 union_of_interval(F1,T1,F2,T2,FR,TR) :- number(F2),
3567 geq_inf(F2,F1), number(T1),T11 is T1+1,geq_inf(T11,F2), geq_inf(T2,F2),!,FR=F1,TR=T2. % intervals can be joined
3568 union_of_interval(F2,T2,F1,T1,FR,TR) :- number(F2),
3569 geq_inf(F2,F1), number(T1),T11 is T1+1,geq_inf(T11,F2), geq_inf(T2,F2),!,FR=F1,TR=T2. % see above
3570
3571 :- use_module(library(ordsets),[ord_union/3]).
3572 union_of_avl(A1,A2,ARes) :-
3573 avl_height(A2,Sz2),
3574 (Sz2 < 2 % we have something like Set := Set \/ {x}; no need to compute height of A1
3575 -> union_of_avl1(A1,99999,A2,Sz2,ARes)
3576 ; avl_height(A1,Sz1), % TODO: we could call avl_height_less_than or avl_height_compare
3577 (Sz1<Sz2 -> union_of_avl1(A2,Sz2,A1,Sz1,ARes) ; union_of_avl1(A1,Sz1,A2,Sz2,ARes))
3578 ).
3579 union_of_avl1(A1,Sz1,A2,Sz2,ARes) :- Sz2>2, Sz1 =< Sz2+3, % difference not too big; Sz2 at least a certain size
3580 !,
3581 avl_to_list(A2,List2), % get all members
3582 avl_to_list(A1,List1),
3583 ord_union(List1,List2,L12),
3584 ord_list_to_avl(L12,ARes).
3585 union_of_avl1(A1,_Sz1,A2,_Sz2,ARes) :- % this version is better when A2 is small compared to A1
3586 avl_domain(A2,List2), % get all members
3587 add_to_avl(List2,A1,ARes).
3588
3589 :- use_module(library(lists),[reverse/2]).
3590 % a custom version for union(A) where A is AVL set; avoid converting/expanding accumulators and computing avl_height
3591 % runtime of e.g., UNION(x).(x:1000..1514|0..x) 0.65 sec or UNION(n).(n:10000..10010|UNION(x).(x:n..n+1000|n..x)) 4.8 sec is considerably smaller with this version
3592 union_generalized_explicit_set(avl_set(SetsOfSets),Res,WF) :-
3593 expand_custom_set_to_list_wf(avl_set(SetsOfSets),ESetsOfSets,_,union_generalized_wf,WF),
3594 % length(ESetsOfSets,Len),print(union_gen(Len)),nl,
3595 (ESetsOfSets=[OneSet]
3596 -> Res=OneSet % avoid converting to list and back to Avl
3597 ; reverse(ESetsOfSets,RESetsOfSets), % be sure to insert larger values first, so that ord_union has less work to do below; useful if you have many small singleton sets, for example union(ran(%x.(x : 1 .. 10000|{x * x}))) 2.37 secs --> 0.15 secs
3598 % note: dom({r,x|x:1..50000 & r:{x*x}}) is still 3 times faster
3599 union_of_avls(RESetsOfSets,[],Res)).
3600
3601 % take the union of a list of avl_sets
3602 union_of_avls([],Acc,Res) :- ord_list_to_avl(Acc,ARes), construct_avl_set(ARes,Res).
3603 union_of_avls([H|T],Acc,Res) :-
3604 union_of_avl_with_acc(H,Acc,NewAcc),
3605 union_of_avls(T,NewAcc,Res).
3606
3607 union_of_avl_with_acc(avl_set(H),Acc,NewAcc) :- !,
3608 avl_to_list(H,HList),
3609 ord_union(Acc,HList,NewAcc).
3610 union_of_avl_with_acc([],Acc,Res) :- !,Res=Acc.
3611 % other custom sets should normally not appear, we obtain the list as elements stored in an avl_set
3612 union_of_avl_with_acc(G,_,_) :- add_internal_error('Uncovered element: ',union_of_avl_with_acc(G,_,_)),fail.
3613
3614
3615
3616 % TO DO: there are no rules for is_not_member_value_closure for intersection below
3617 intersection_of_explicit_set_wf(global_set(GS),S2,R,_WF) :- is_maximal_global_set(GS), !, R=S2.
3618 intersection_of_explicit_set_wf(freetype(_GS),S2,R,_WF) :- !, R=S2.
3619 intersection_of_explicit_set_wf(_,S2,_,_WF) :- var(S2),!,fail. % code below may instantiate S2
3620 intersection_of_explicit_set_wf(S1,S2,R,_WF) :- is_definitely_maximal_set(S2), !, R=S1.
3621 intersection_of_explicit_set_wf(_S1,[],R,_WF) :-!, R=[].
3622 intersection_of_explicit_set_wf(avl_set(A1),I2,R,_WF) :-
3623 is_interval_closure_or_integerset(I2,From1,To1),
3624 !,
3625 intersect_avl_interval(A1,From1,To1,R).
3626 intersection_of_explicit_set_wf(I1,I2,R,_WF) :-
3627 intersection_with_interval_closure(I1,I2,R),!.
3628 intersection_of_explicit_set_wf(S1,S2,R,_WF) :-
3629 get_avl_sets(S1,S2,A1,A2),
3630 !, % if too large: better to apply normal intersection code ?
3631 % if one of the args is an interval this is already caught in kernel_objects calling intersection_with_interval_closure; see SetIntersectionBig.mch
3632 avl_domain(A1,ES), % A1 has the smaller height; important for e.g. SetIntersectionBig2.mch
3633 inter2(ES,A2,IRes),
3634 ord_list_to_avlset(IRes,R,intersection). % we have generated the elements in the right order already
3635 intersection_of_explicit_set_wf(Set1,Set2,R,WF) :-
3636 transform_global_sets_into_closure(Set1,closure(P1,T1,B1)),
3637 transform_global_sets_into_closure(Set2,closure(P2,T2,B2)),
3638 % gets called, e.g., for {x|x /: NATURAL1} /\ NATURAL1
3639 unify_closure_predicates(P1,T1,B1, P2,T2,B2 , NewP,NewT, NewB1,NewB2),
3640 debug:debug_println(9,intersection_of_two_closures(P1,P2,NewP,NewT)),
3641 conjunct_predicates([NewB1,NewB2],BI),
3642 % create a conjunction: can be much more efficient than seperately expanding;
3643 % also works well if one of the closures is infinite
3644 C = closure(NewP,NewT,BI),
3645 expand_custom_set_wf(C,R,intersection_of_explicit_set_wf,WF). % we could keep it symbolic; maybe use SYMBOLIC pref
3646 % to do: also use above for closure and AVL set with member(P,value(avl_set(A)))
3647 % we could also apply the same principle to difference_of_explicit_set
3648 % currently we enable intersection to be treated symbolically (not_symbolic_binary(intersection) commented out)
3649 % This means the above clause for intersection_of_explicit_set_wf is less useful
3650 % a special case; just for interval closures
3651 intersection_with_interval_closure(I1,I2,R) :-
3652 is_interval_closure_or_integerset(I1,From1,To1), nonvar(I2),
3653 intersection_with_interval_closure_aux(I2,From1,To1,R).
3654 intersection_with_interval_closure(avl_set(A1),I2,R) :-
3655 is_interval_closure_or_integerset(I2,From1,To1),
3656 !,
3657 intersect_avl_interval(A1,From1,To1,R).
3658
3659 % try and get AVL sets from two args; first AVL set is smaller one according to height
3660 get_avl_sets(avl_set(A1),S2,AA1,AA2) :- nonvar(S2), S2=avl_set(A2),
3661 ? (avl_height_compare(A1,A2,R), R=lt
3662 -> (AA1,AA2)=(A1,A2)
3663 ; (AA1,AA2)=(A2,A1)).
3664 %get_avl_sets(S1,S2,AA1,AA2) :- nonvar(S2),S2=avl_set(A2), get_avl_set_arg(S1,A1),
3665 % (avl_height_compare(A1,A2,R),R=gt -> (AA1,AA2)=(A2,A1) ; (AA1,AA2)=(A1,A2)).
3666
3667
3668 %intersection_with_interval_closure_aux(avl_set(A),...
3669 intersection_with_interval_closure_aux(I2,From1,To1,R) :-
3670 is_interval_closure_or_integerset(I2,From2,To2),!,
3671 intersect_intervals_with_inf(From1,To1,From2,To2,FromRes,ToRes),
3672 construct_interval_closure(FromRes,ToRes,R).
3673 % (is_interval_closure_or_integerset(R,F,T) -> print(ok(F,T)),nl ; print(ko),nl).
3674 intersection_with_interval_closure_aux(avl_set(A2),From1,To1,R) :-
3675 intersect_avl_interval(A2,From1,To1,R).
3676
3677 % intersect avl with interval
3678 % TO DO: expand interval if small (or small intersection with AVL) and use avl intersection
3679 intersect_avl_interval(_,From2,To2,_) :- (var(From2) ; var(To2)),!,fail.
3680 intersect_avl_interval(A1,From2,To2,R) :- avl_min(A1,int(Min)),
3681 geq_inf(Min,From2),
3682 geq_inf(To2,Min), avl_max(A1,int(Max)),
3683 geq_inf(To2,Max),
3684 % AVL fully contained in interval; no need to expand to list and back again
3685 !,
3686 construct_avl_set(A1,R).
3687 intersect_avl_interval(A1,From2,To2,R) :-
3688 avl_domain(A1,ES),
3689 inter_interval(ES,From2,To2,IRes),
3690 ord_list_to_avlset(IRes,R,intersect_avl_interval).
3691
3692 inter_interval([],_,_, []).
3693 inter_interval([IH|T],From2,To2, Res) :- IH = int(H),
3694 (geq_inf(To2,H) ->
3695 (geq_inf(H,From2) -> Res = [IH-true|Res2] ; Res = Res2),
3696 inter_interval(T,From2,To2,Res2)
3697 ; Res = [] % we have exceeded the upper limit of the interval
3698 ).
3699
3700 intersect_intervals_with_inf(From1,To1,From2,To2,FromRes,ToRes) :-
3701 minimum_with_inf(To1,To2,ToRes),
3702 maximum_with_inf(From1,From2,FromRes).
3703
3704 % check if two intervals are disjoint
3705 disjoint_intervals_with_inf(From1,To1,From2,To2) :-
3706 intersect_intervals_with_inf(From1,To1,From2,To2,Low,Up),
3707 number(Up), number(Low), Low > Up.
3708
3709 inter2([],_, []).
3710 inter2([H|T],A1, Res) :-
3711 (avl_fetch(H,A1) -> Res = [H-true|Res2] ; Res = Res2), inter2(T,A1,Res2).
3712
3713 ord_list_to_avlset(OL,R) :- ord_list_to_avlset(OL,R,unknown).
3714 ord_list_to_avlset(OrdList,Res,Origin) :-
3715 % assumes that we have generated the elements in the right order already
3716 (OrdList=[] -> Res=[]
3717 ; check_sorted(OrdList,Origin),
3718 ord_list_to_avl(OrdList,ARes), Res=avl_set(ARes)).
3719
3720 % a version which accepts a list of values without -true
3721 % values have to be ground and already converted for use in avl_set
3722 sorted_ground_normalised_list_to_avlset(List,Res,PP) :-
3723 add_true_to_list(List,LT),
3724 ord_list_to_avlset_direct(LT,Res,PP).
3725
3726 add_true_to_list([],[]).
3727 add_true_to_list([H|T],[H-true|TT]) :- add_true_to_list(T,TT).
3728
3729 % the same, but without checking sorted (only use if you are really sure the list is sorted)
3730 ord_list_to_avlset_direct([],[],_).
3731 ord_list_to_avlset_direct([H|T],Res,_):-
3732 (T==[] -> H=Key-Val, Res = avl_set(node(Key,Val,0,empty,empty)) % slightly faster than calling ord_list_to_avl
3733 ; ord_list_to_avl([H|T],ARes), Res = avl_set(ARes)).
3734
3735 check_sorted([],_) :- !.
3736 check_sorted([H-_|T],Origin) :- !, check_sorted2(T,H,Origin).
3737 check_sorted(X,Origin) :- add_error_and_fail(ord_list_to_avlset,'Not a list of -/2 pairs: ',Origin:X).
3738
3739 check_sorted2([],_,_) :- !.
3740 check_sorted2([H-_|T],PH,Origin) :- PH @< H,!, check_sorted2(T,H,Origin).
3741 check_sorted2(X,Prev,Origin) :-
3742 add_error_and_fail(ord_list_to_avlset,'Not a sorted list of -/2 pairs: ',Origin:(X,Prev)).
3743
3744 % ------------------
3745
3746 :- use_module(kernel_freetypes,[is_maximal_freetype/1]).
3747 is_definitely_maximal_set(S) :- nonvar(S),
3748 is_definitely_maximal_set2(S).
3749 is_definitely_maximal_set2(freetype(ID)) :- is_maximal_freetype(ID).
3750 is_definitely_maximal_set2(global_set(GS)) :- is_maximal_global_set(GS).
3751 is_definitely_maximal_set2(closure(P,T,B)) :- is_definitely_maximal_closure(P,T,B).
3752 is_definitely_maximal_set2(avl_set(S)) :- quick_definitely_maximal_set_avl(S).
3753 is_definitely_maximal_set2([H|T]) :- nonvar(H), is_definitely_maximal_list(H,T). %, nl,print(maximal(H,T)),nl,nl.
3754 %H==pred_true, T == [pred_false]. % for some reason BOOL is sometimes presented this way
3755 is_definitely_maximal_set2(empty) :- % detect unwrapped AVL sets
3756 add_internal_error('Not a set: ',is_definitely_maximal_set2(empty)),fail.
3757 is_definitely_maximal_set2(node(A,B,C,D,E)) :-
3758 add_internal_error('Not a set: ',is_definitely_maximal_set2(node(A,B,C,D,E))),fail.
3759
3760 is_definitely_maximal_list(pred_true,T) :- nonvar(T), T=[_|_]. %
3761 is_definitely_maximal_list(pred_false,T) :- nonvar(T), T=[_|_].%
3762 is_definitely_maximal_list(fd(_,Type),T) :- nonvar(T),b_global_set_cardinality(Type,Card),
3763 % check if we have the same number of elements as the type: then the set must me maximal
3764 length_at_least(T,Card).
3765 % We could try and and also treat pairs
3766
3767 length_at_least(1,_) :- !. % we have already removed 1 element; T can be nil
3768 length_at_least(N,T) :- nonvar(T), T=[_|TT], N1 is N-1, length_at_least(N1,TT).
3769
3770 is_definitely_maximal_closure(_,_,b(truth,_Pred,_)) :- !.
3771 is_definitely_maximal_closure(P,T,B) :- is_cartesian_product_closure_aux(P,T,B,S1,S2),!,
3772 is_definitely_maximal_set(S1),is_definitely_maximal_set(S2).
3773 is_definitely_maximal_closure(P,T,B) :-
3774 is_full_powerset_or_relations_or_struct_closure(closure(P,T,B),Sets),
3775 l_is_definitely_maximal_set(Sets).
3776
3777 l_is_definitely_maximal_set([]).
3778 l_is_definitely_maximal_set([H|T]) :- is_definitely_maximal_set(H), l_is_definitely_maximal_set(T).
3779
3780 % check if we have an AVL tree covering all elements of the underlying type
3781 quick_definitely_maximal_set_avl(AVL) :-
3782 AVL=node(El,_True,_,_Left,_Right),
3783 quick_definitely_maximal_set_avl_aux(El,AVL).
3784 quick_definitely_maximal_set_avl_aux(El,AVL) :-
3785 try_get_finite_max_card_from_ground_value(El,Card),
3786 % this could fail if El contains empty sets !
3787 % also: it must fail if Card is infinite (no avl_set can be maximal)
3788 (Card < 1000 -> true
3789 ; preferences:preference(solver_strength,SS), Card < 1000+SS*100
3790 ), % otherwise too expensive a check avl_size
3791 quick_avl_approximate_size(AVL,MaxSize),
3792 MaxSize >= Card, % otherwise no sense in computing avl_size, which is linear in size of AVL
3793 avl_size(AVL,Size),
3794 %(MaxSize>=Size -> print(ok(Size,all(Card))),nl ; print('**** ERROR: '), print(Size),nl,trace),
3795 Size=Card.
3796
3797 % check if we have an AVL function with domain covering all elements of the underlying type
3798 quick_definitely_maximal_total_function_avl(AVL) :-
3799 AVL=node(El,_True,_,_Left,_Right),
3800 El=(DomEl,_),
3801 quick_definitely_maximal_set_avl_aux(DomEl,AVL), % the size is exactly the size of the domain
3802 is_avl_partial_function(AVL).
3803
3804 % ----------------------
3805 % set_subtraction /
3806 difference_of_explicit_set(S1,S2,R) :-
3807 difference_of_explicit_set_wf(S1,S2,R,no_wf_available).
3808 % this is called with first argument nonvar (for set_subtraction operator):
3809 difference_of_explicit_set_wf(_S1,S2,R,_) :-
3810 is_definitely_maximal_set(S2), !, R=[].
3811 difference_of_explicit_set_wf(S1,S2,R,_) :- nonvar(S2), S2=[],!, R=S1.
3812 difference_of_explicit_set_wf(S1,S2,R,_) :-
3813 %nonvar(S1),
3814 ? is_very_large_maximal_global_set(S1,Type), !, % TO DO: also for freetype ? cartesian products,...
3815 /* we have a complement-set */
3816 complement_set(S2,Type,R).
3817 difference_of_explicit_set_wf(S1,S2,Result,_) :-
3818 is_not_member_value_closure(S1,Type,MS1),
3819 nonvar(MS1), is_custom_explicit_set(MS1,difference_of_explicit_set_wf),!,
3820 union_complement_set(MS1,S2,Type,Result).
3821 difference_of_explicit_set_wf(_,S2,_,_) :- var(S2), !, fail. % then we cannot do anything below
3822 difference_of_explicit_set_wf(S1,S2,R,WF) :-
3823 is_not_member_value_closure(S2,_Type,MS2), nonvar(MS2),
3824 intersection_of_explicit_set_wf(MS2,S1,R,WF),!.
3825 difference_of_explicit_set_wf(I1,I2,R,_) :-
3826 is_interval_closure_or_integerset(I1,From1,To1),
3827 is_interval_closure_or_integerset(I2,From2,To2),
3828 difference_interval(From1,To1,From2,To2,FromRes,ToRes),
3829 % TO DO: also treat case when difference yields two disjoint intervals
3830 % i.e., do not fail and forget info about interval bounds in case we cannot compute difference as a an interval, e.g., INT - {0}
3831 !,
3832 construct_interval_closure(FromRes,ToRes,R).
3833 difference_of_explicit_set_wf(avl_set(A1),S2,R,WF) :-
3834 (S2=avl_set(A2) ;
3835 ground_value(S2), expand_and_convert_to_avl_set_unless_very_large(S2,A2,WF)),!,
3836 avl_height(A2,H2),
3837 %avl_min(A1,Min1),avl_max(A1,Max1), avl_min(A2,Min2),avl_max(A2,Max2), avl_height(A1,H1),nl,print(diff(avl(H1,Min1,Max1),avl(H2,Min2,Max2))),nl,
3838 avl_height(A1,H1),
3839 ((H2<2 -> true ; H1 > H2+1) % then it is more efficient to expand A2 and remove the A2 elements from A1;
3840 % note that difference_of_explicit_set2 now also sometimes expands both:
3841 % exact threshold when it is beneficial: difference_of_explicit_set2/3
3842 % for {x|x:1..200000 & x mod 2 = 0} - {y|y:2500..29010 & y mod 2 = 0} -> 150 ms vs 80 ms avl(17,int(2),int(200000)),avl(14,int(2500),int(29010)
3843 % {x|x:1..200000 & x mod 2 = 0} - {y|y:2500..59010 & y mod 2 = 0} -> 180 ms vs 80 ms avl(17,int(2),int(200000)),avl(15,int(2500),int(59010))
3844 % {x|x:1..200000 & x mod 2 = 0} - {y|y:500..159010 & y mod 2 = 0} -> 180 ms vs 250 ms avl(17,int(2),int(200000)),avl(17,int(500),int(159010))
3845 -> expand_custom_set_to_sorted_list(S2,ES,_,difference_of_explicit_set1,WF),
3846 difference_of_explicit_set3(ES,A1,R)
3847 ; expand_custom_set_to_sorted_list(avl_set(A1),ES,Done,difference_of_explicit_set2,WF),
3848 difference_of_explicit_set2(ES,H1,A2,H2,R,Done)).
3849 difference_of_explicit_set_wf(S1,S2,R,WF) :-
3850 (S2=avl_set(A2) ;
3851 ground_value(S2), expand_and_convert_to_avl_set_unless_very_large(S2,A2,WF)),!,
3852 avl_height(A2,A2Height),
3853 difference_with_avl(S1,A2,A2Height,R,WF).
3854 % to do: we could detect same_texpr_body for two closures and return R=[]
3855
3856 :- use_module(avl_tools,[avl_approximate_size_from_height/2]).
3857 :- use_module(bsyntaxtree,[safe_create_texpr/4, create_texpr/4, conjunct_predicates/2, mark_bexpr_as_symbolic/2]).
3858 difference_with_avl(S1,A2,A2Height,R,_) :-
3859 is_closure_or_integer_set(S1,[ID],[T],B),
3860 % check if the first argument is infinite; then do the difference set symbolically
3861 % this could supersed the complement set construction and be generalised to other sets apart from avl_sets as A2
3862 avl_approximate_size_from_height(A2Height,A2Size),
3863 Limit is max(A2Size*10,1000000), % if A2 is more than 10% size of S1, probably better to compute difference explicitly
3864 is_very_large_or_symbolic_closure([ID],[T],B,Limit),
3865 !, % TO DO: also allow multiple identifiers
3866 create_texpr(identifier(ID),T,[],TID),
3867 create_texpr(value(avl_set(A2)),set(T),[],A2Value),
3868 create_texpr(not_member(TID,A2Value),pred,[],NotMemA2),
3869 conjunct_predicates([B,NotMemA2],NewBody),
3870 mark_bexpr_as_symbolic(NewBody,NewBodyS),
3871 R = closure([ID],[T],NewBodyS).
3872 difference_with_avl(S1,A2,A2Height,R,WF) :-
3873 (nonvar(S1),S1=avl_set(A1) -> avl_height(A1,H1) ; H1=unknown),
3874 expand_custom_set_to_sorted_list(S1,ES,Done,difference_of_explicit_set3,WF),
3875 difference_of_explicit_set2(ES,H1,A2,A2Height,R,Done).
3876
3877
3878 % construct complement of a set
3879 union_complement_set(S1,S2,Type,Result) :-
3880 ground_value_check(S2,G2),
3881 when(nonvar(G2),union_complement_set2(S1,S2,Type,Result)).
3882 union_complement_set2(S1,S2,Type,Result) :-
3883 union_of_explicit_set(S1,S2,S12),
3884 construct_complement_closure_if_necessary(S12,Type,R),
3885 kernel_objects:equal_object(R,Result,union_complement_set2).
3886
3887 % construct complement of a set
3888 complement_set(S2,Type,Result) :-
3889 ground_value_check(S2,G2),
3890 when(nonvar(G2),complement_set2(S2,Type,Result)).
3891 complement_set2(S2,Type,Result) :-
3892 is_not_member_value_closure(S2,Type,MS2),!, % complement of complement
3893 kernel_objects:equal_object(MS2,Result,complement_set2).
3894 complement_set2(S2,Type,Result) :-
3895 try_expand_and_convert_to_avl_with_check(S2,ExpandedS2,difference_complement_set),
3896 construct_complement_closure_if_necessary(ExpandedS2,Type,R),
3897 kernel_objects:equal_object(R,Result,complement_set2).
3898
3899 :- block construct_complement_closure_if_necessary(-,?,?).
3900 construct_complement_closure_if_necessary(Set,TYPE,R) :-
3901 (Set=[] -> b_type2_set(TYPE,R)
3902 ; is_not_member_value_closure(Set,TYPE,MS) -> R=MS % complement of complement
3903 ; construct_complement_closure(Set,TYPE,R)).
3904
3905 % succeeds if difference of two intervals is also an interval
3906 % SourceLow..SourceUp \ DiffLow..DiffUp
3907 :- assert_must_succeed(custom_explicit_sets:difference_interval(1,10,9,11,1,8)).
3908 :- assert_must_succeed(custom_explicit_sets:difference_interval(1,10,9,inf,1,8)).
3909 :- assert_must_succeed(custom_explicit_sets:difference_interval(1,10,10,12,1,9)).
3910 :- assert_must_succeed(custom_explicit_sets:difference_interval(1,10,11,12,1,10)).
3911 :- assert_must_succeed(custom_explicit_sets:difference_interval(1,10,12,13,1,10)).
3912 :- assert_must_succeed(custom_explicit_sets:difference_interval(1,10,11,inf,1,10)).
3913 :- assert_must_succeed(custom_explicit_sets:difference_interval(1,inf,11,inf,1,10)).
3914 % :- assert_must_succeed(custom_explicit_sets:difference_interval(1,10,9,8,1,10)). % 9..8 empty not detected
3915 :- assert_must_succeed(custom_explicit_sets:difference_interval(1,10,1,8,9,10)).
3916 :- assert_must_succeed(custom_explicit_sets:difference_interval(1,10,1,10,11,10)). % empty
3917 :- assert_must_succeed(custom_explicit_sets:difference_interval(1,10,1,inf,inf,10)).
3918 :- assert_must_succeed(custom_explicit_sets:difference_interval(3,10,1,2,3,10)).
3919 :- assert_must_succeed(custom_explicit_sets:difference_interval(3,inf,1,2,3,inf)).
3920 :- assert_must_succeed(custom_explicit_sets:difference_interval(3,10,1,3,4,10)).
3921 :- assert_must_succeed(custom_explicit_sets:difference_interval(3,10,1,9,10,10)).
3922 :- assert_must_succeed(custom_explicit_sets:difference_interval(3,10,1,10,11,10)).
3923 difference_interval(SourceLow,SourceUp,DiffLow,DiffUp,ResLow,ResUp) :-
3924 (nonvar(SourceLow),nonvar(DiffLow),nonvar(DiffUp),
3925 geq_inf(SourceLow,DiffLow)
3926 -> % DiffLow is to left of SourceLow
3927 inc(DiffUp,D1),
3928 maximum_with_inf(D1,SourceLow,ResLow),
3929 ResUp=SourceUp % also works if SourceUp is a variable
3930 ; nonvar(DiffUp),nonvar(SourceUp),nonvar(DiffLow),
3931 geq_inf(DiffUp,SourceUp)
3932 -> % DiffUp is to right of SourceUp
3933 ResLow=SourceLow, % also works if SourceLow is a variable
3934 dec(DiffLow,D1),
3935 minimum_with_inf(SourceUp,D1,ResUp)).
3936
3937 inc(N,R) :- N==inf,!,R=inf.
3938 inc(N,N1) :- N1 is N+1.
3939 dec(N,R) :- N==inf,!,R=inf.
3940 dec(N,N1) :- N1 is N-1.
3941
3942 :- use_module(library(ordsets), [ord_subtract/3]).
3943 :- block difference_of_explicit_set2(?,?,?,?,?,-).
3944 difference_of_explicit_set2(ES,A1Height,A2,A2Height,R,_) :-
3945 (number(A1Height), A1Height+4 >= A2Height -> true
3946 ; A2Height < 5
3947 ; Limit is 2**(A2Height-4),
3948 length_larger_than(ES,Limit)
3949 % TO DO: we could try and pass sizes from specific closures to this predicate
3950 ),
3951 % A1 is not much larger than A2, then it is probably faster to use ord_subtract on expanded A2
3952 % {x|x mod 2 =0 & x:1..10000} - {y|y mod 3 =0 & y : 1..200000} : still more efficient with ord_subtract
3953 !,
3954 avl_domain(A2,A2Expanded),
3955 ord_subtract(ES,A2Expanded,OrdRes),
3956 sorted_ground_normalised_list_to_avlset(OrdRes,AVL,difference_of_explicit_set2),
3957 equal_object(AVL,R).
3958 difference_of_explicit_set2(ES,_A1Height,A2,_A2Height,R,_) :-
3959 avl_min(A2,Min),
3960 diff1(ES,Min,A2,IRes),
3961 ord_list_to_avlset(IRes,AVL,difference), % we have generated the elements in the right order already
3962 equal_object(AVL,R). % due to delays in expansion the result could be instantiated
3963
3964
3965 length_larger_than([_|T],Limit) :-
3966 (Limit<1 -> true
3967 ; L1 is Limit-1, length_larger_than(T,L1)).
3968
3969 diff1([],_, _,[]).
3970 diff1([H|T],Min,A1, Res) :-
3971 (H @< Min -> Res = [H-true|Res2],diff1(T,Min,A1,Res2)
3972 ; diff2([H|T],A1,Res)).% TO DO: compute avl_max
3973
3974 diff2([],_, []).
3975 diff2([H|T],A1, Res) :-
3976 (avl_fetch(H,A1) -> Res = Res2 ; Res = [H-true|Res2]), diff2(T,A1,Res2).
3977
3978 % another version to be used when second set small in comparison to first set
3979 difference_of_explicit_set3([],A1,Res) :- construct_avl_set(A1,AVL),
3980 equal_object(AVL,Res). % due to delay in expansion, Res could now be instantiated
3981 difference_of_explicit_set3([H|T],A1,ARes) :-
3982 (avl_delete(H,A1,_True,A2) -> true ; A2=A1),
3983 difference_of_explicit_set3(T,A2,ARes).
3984
3985 % -------------------------
3986
3987 % a version of add_element_to_explicit_set where we have already done the groundness check
3988 add_ground_element_to_explicit_set_wf(avl_set(A),Element,R,WF) :- !,
3989 convert_to_avl_inside_set_wf(Element,AEl,WF),
3990 avl_store(AEl,A,true,A2),!,R=avl_set(A2).
3991 add_ground_element_to_explicit_set_wf(Set,Element,R,WF) :-
3992 add_element_to_explicit_set_wf(Set,Element,R,WF).
3993
3994 add_element_to_explicit_set_wf(global_set(GS),_,R,_) :- is_maximal_global_set(GS), !, R=global_set(GS).
3995 add_element_to_explicit_set_wf(freetype(ID),_,R,_) :- is_maximal_freetype(ID),!, R=freetype(ID).
3996 add_element_to_explicit_set_wf(avl_set(A),Element,R,WF) :-
3997 ground_value(Element), %% was element_can_be_added_or_removed_to_avl(Element),
3998 convert_to_avl_inside_set_wf(Element,AEl,WF),
3999 avl_store(AEl,A,true,A2),!,R=avl_set(A2). /* AVL not normalised */
4000 /* do we need to add support for (special) closures ??
4001 add_element_to_explicit_set_wf(Clos,Element,R,_) :- nonvar(Element),Element=int(X), nonvar(X),
4002 is_interval_closure_or_integerset(Clos,Low,Up), ground(Low), ground(Up),
4003 union_of_interval(X,X,Low,Up,FromRes,ToRes),
4004 !,
4005 construct_interval_closure(FromRes,ToRes,R).
4006 % not-member closure not dealt with here
4007 */
4008
4009 element_can_be_added_or_removed_to_avl(Element) :-
4010 ground_value(Element),
4011 does_not_contain_closure(Element).
4012 ground_element_can_be_added_or_removed_to_avl(Element) :- /* use if you know the element to be ground */
4013 does_not_contain_closure(Element).
4014
4015 % does not contain closure or infinite other sets
4016 does_not_contain_closure([]).
4017 does_not_contain_closure([H|T]) :-
4018 (simple_value(H) -> true /* TO DO: check if we could have a closure at the end ?? */
4019 ; does_not_contain_closure(H),list_does_not_contain_closure(T)).
4020 does_not_contain_closure(fd(_,_)).
4021 does_not_contain_closure(pred_true /* bool_true */).
4022 does_not_contain_closure(pred_false /* bool_false */).
4023 does_not_contain_closure(int(_)).
4024 does_not_contain_closure(string(_)).
4025 does_not_contain_closure(term(_)). % real/floating number
4026 does_not_contain_closure((X,Y)) :- does_not_contain_closure(X), does_not_contain_closure(Y).
4027 does_not_contain_closure(avl_set(_)).
4028 ?does_not_contain_closure(global_set(G)) :- \+ is_infinite_global_set(G,_).
4029 %does_not_contain_closure(freetype(_)).
4030 does_not_contain_closure(freeval(_,_,Value)) :- does_not_contain_closure(Value).
4031 does_not_contain_closure(rec(Fields)) :- does_not_contain_closure_fields(Fields).
4032
4033 does_not_contain_closure_fields([]).
4034 does_not_contain_closure_fields([field(_,Val)|T]) :- does_not_contain_closure(Val),
4035 does_not_contain_closure_fields(T).
4036
4037 list_does_not_contain_closure([]).
4038 list_does_not_contain_closure([H|T]) :-
4039 does_not_contain_closure(H),list_does_not_contain_closure(T).
4040 list_does_not_contain_closure(avl_set(_)).
4041 list_does_not_contain_closure(global_set(G)) :- \+ is_infinite_global_set(G,_).
4042
4043 simple_value(fd(_,_)).
4044 simple_value(pred_true /* bool_true */).
4045 simple_value(pred_false /* bool_false */).
4046 simple_value(int(_)).
4047 simple_value((A,B)) :- simple_value(A), simple_value(B).
4048 simple_value(string(_)).
4049
4050
4051 % a version of the above which throws error if element cannot be added
4052 % assumes element_can_be_added_or_removed_to_avl has been checked
4053 remove_element_from_explicit_set(avl_set(A),Element,R) :-
4054 element_can_be_added_or_removed_to_avl(Element), % remove check?
4055 convert_to_avl_inside_set(Element,AEl), !,
4056 direct_remove_element_from_avl(A,AEl,R).
4057 remove_element_from_explicit_set(ES,Element,R) :-
4058 add_internal_error('Cannot remove element from explicit set:',remove_element_from_explicit_set(ES,Element,R)).
4059
4060 direct_remove_element_from_avl(A,AEl,R) :-
4061 avl_delete(AEl,A,_True,A2),
4062 construct_avl_set(A2,R). /* AVL not normalised */
4063
4064 /* same as remove but element can be absent */
4065 delete_element_from_explicit_set(avl_set(A),Element,R) :-
4066 element_can_be_added_or_removed_to_avl(Element),
4067 convert_to_avl_inside_set(Element,AEl), !,
4068 (avl_delete(AEl,A,_True,A2)
4069 -> construct_avl_set(A2,R)
4070 ; R = avl_set(A)
4071 ). /* AVL not normalised */
4072
4073 is_maximal_global_set(GS) :- is_maximal_global_set(GS,_Type).
4074 is_maximal_global_set(GS,_) :- var(GS),!,fail.
4075 is_maximal_global_set('INTEGER',Type) :- !, Type=integer.
4076 is_maximal_global_set('REAL',Type) :- !, Type=real.
4077 is_maximal_global_set('FLOAT',_) :- !, fail.
4078 is_maximal_global_set('STRING',Type) :- !, Type=string.
4079 is_maximal_global_set(GS,global(GS)) :-
4080 \+ kernel_objects:integer_global_set(GS).
4081
4082 % To do: maybe get rid of all complement set code; add in_difference_set as symbolic binary operator
4083 %is_very_large_maximal_global_set(X,_) :- print(very(X)),nl,fail.
4084 is_very_large_maximal_global_set(closure(P,T,B),Type) :- is_definitely_maximal_closure(P,T,B),
4085 couplise_list(T,Type).
4086 is_very_large_maximal_global_set(global_set('INTEGER'),integer).
4087 is_very_large_maximal_global_set(global_set('STRING'),string).
4088 is_very_large_maximal_global_set(global_set('REAL'),string).
4089 is_very_large_maximal_global_set(freetype(ID),freetype(ID)) :- is_infinite_freetype(ID).
4090
4091
4092
4093 remove_minimum_element_custom_set(avl_set(S),X,RES) :- !,
4094 avl_del_min(S,X,_True,Res0),
4095 (empty_avl(Res0) -> RES=[] ; RES = avl_set(Res0)).
4096 %remove_minimum_element_custom_set(closure(P,T,B),X,RES) :-
4097 % is_interval_closure_or_integerset(Clos,Low,Up),!,
4098 % X = Low, TO DO: construct new interval closure
4099 remove_minimum_element_custom_set(CS,X,RES) :-
4100 expand_custom_set_to_list(CS,ECS,Done,remove_minimum_element_custom_set),
4101 remove_minimum_element_custom_set2(ECS,X,RES,Done).
4102
4103 :- block remove_minimum_element_custom_set2(?,?,?,-).
4104 % wait until Done: otherwise the Tail of the list could be instantiated by somebody else; interfering with expand_custom_set_to_list
4105 remove_minimum_element_custom_set2([H|T],X,RES,_) :- equal_object((H,T),(X,RES)).
4106
4107
4108 min_of_explicit_set_wf(avl_set(S),Min,_) :- !, avl_min(S,Min).
4109 min_of_explicit_set_wf(Clos,Min,WF) :-
4110 is_interval_closure_or_integerset(Clos,Low,Up),
4111 (Low == minus_inf
4112 -> add_wd_error('minimum of unbounded infinite set not defined:',Clos,WF)
4113 ; cs_greater_than_equal(Up,Low),
4114 Min=int(Low)).
4115
4116 cs_greater_than_equal(X,Y) :-
4117 ((X==inf;Y==minus_inf) -> true ; kernel_objects:less_than_equal_direct(Y,X)).
4118
4119
4120 max_of_explicit_set_wf(avl_set(S),Max,_) :- !,avl_max(S,Max).
4121 max_of_explicit_set_wf(Clos,Max,WF) :-
4122 is_interval_closure_or_integerset(Clos,Low,Up),
4123 (Up==inf
4124 -> add_wd_error('maximum of unbounded infinite set not defined:',Clos,WF)
4125 ; cs_greater_than_equal(Up,Low),
4126 Max=int(Up)).
4127
4128 % ------------- SIGMA/PI --------------
4129
4130 % compute sum or product of an integer set:
4131 sum_or_mul_of_explicit_set(avl_set(S),SUMorMUL,Result) :-
4132 avl_domain(S,Dom),
4133 (SUMorMUL=sum -> simple_sum_list(Dom,0,R) ; simple_mul_list(Dom,1,R)),
4134 Result = int(R).
4135 sum_or_mul_of_explicit_set(CS,SUMorMUL,Result) :- SUMorMUL == sum,
4136 is_interval_closure(CS,Low,Up),
4137 sum_interval(Low,Up,Result),
4138 sum_interval_clpfd_prop(Low,Up,Result).
4139
4140 :- block sum_interval(-,?,?), sum_interval(?,-,?).
4141 sum_interval(Low,Up,_) :- (\+ number(Low) ; \+ number(Up)),!,
4142 add_error(sum_interval,'Cannot compute sum of interval: ',Low:Up),fail.
4143 sum_interval(Low,Up,Result) :- Low>Up,!, Result=int(0).
4144 sum_interval(Low,Up,Result) :-
4145 R is ((1+Up-Low)*(Low+Up)) // 2, % generalisation of Gauss formula k*(k+1)//2
4146 Result = int(R).
4147
4148 sum_interval_clpfd_prop(Low,Up,Result) :-
4149 preferences:preference(use_clpfd_solver,true), Result=int(R),
4150 var(R), % we haven't computed the result yet; the bounds are not known; set up constraint propagation rules
4151 !,
4152 try_post_constraint((Low #>= 0) #=> (R #> 0)), % we could provide better bounds here for negative numbers
4153 try_post_constraint(((Low #=< Up) #\/ (R #\= 0)) #=> (R #= ((1+Up-Low)*(Low+Up))//2)),
4154 try_post_constraint((Low #> Up) #=> (R #= 0)).
4155 % not working yet: x = SIGMA(i).(i:-3..n|i) & x=0 & n< -1
4156 sum_interval_clpfd_prop(_,_,_).
4157
4158 simple_sum_list([],A,A).
4159 simple_sum_list([int(H)|T],Acc,R) :- NA is Acc+H, simple_sum_list(T,NA,R).
4160 simple_mul_list([],A,A).
4161 simple_mul_list([int(H)|T],Acc,R) :- NA is Acc*H, simple_mul_list(T,NA,R).
4162
4163
4164 /*
4165 direct_product_symbolic(S,R,Res) :- % NOT YET FINISHED
4166 nonvar(S), S=closure(PS,[T1,TS2],RS),
4167 nonvar(R), R=closure(PR,[T1,TR1],RR),
4168 is_lambda_value_domain_closure(PS,TS,RS, SDomainValue,SExpr),
4169 is_lambda_value_domain_closure(PR,TR,RR, RDomainValue,RExpr),
4170 construct_closure(['zzz','_lambda_result_'],[T1,couple(TR1,TR2)],
4171 member(zzz,SDomainValue) , member(zzz,RDomainValue), eq(lambda,pair(SExpr,RExpr))).
4172 */
4173
4174 % we assume that try_expand_and_convert_to_avl_unless_very_large already called on arguments
4175 direct_product_explicit_set(S,R,Res) :-
4176 nonvar(R), %is_custom_explicit_set(R,direct_product),
4177 nonvar(S), %is_custom_explicit_set(S,direct_product),
4178 direct_product_explicit_set_aux(S,R,Res).
4179 %direct_product_explicit_set_aux(S,R,Res) :- (S = closure(_,_,_) ; R = closure(_,_,_)),
4180 % print_term_summary(direct_product_explicit_set_aux(S,R,Res)),nl,
4181 % % TO DO: generate closure
4182 % fail.
4183 direct_product_explicit_set_aux(avl_set(AS),avl_set(AR),Res) :-
4184 % the expansion guarantees that we have the lists ES and ER then in sorted order
4185 avl_domain(AS,ES), % -> expand_custom_set(avl_set(AS),ES),
4186 avl_domain(AR,ER), % -> expand_custom_set(avl_set(AR),ER),
4187 direct_product3(ES,ER,DPList),
4188 ord_list_to_avlset(DPList,DPAVL,direct_product), % is it really ordered ? findall must also return things ordered!
4189 equal_object(DPAVL,Res,direct_product_explicit_set).
4190
4191 direct_product3([],_Rel2,[]).
4192 direct_product3([(From,To1)|T1],Rel2,Res) :-
4193 get_next_mapped_to_eq(T1,From,TTo,Tail1), ToList1 = [To1|TTo],
4194 get_next_mapped_to(Rel2,From,ToList2,Tail2),
4195 calc_direct_product(ToList1,From,ToList2,Res,Rest),
4196 (Tail2=[] -> Rest=[] ; direct_product3(Tail1,Tail2,Rest)).
4197
4198 % get all elements which map to From, supposing that the list is sorted & we have already had a match
4199 get_next_mapped_to_eq([],_,[],[]).
4200 get_next_mapped_to_eq([(From2,To2)|T],From,Result,Tail) :-
4201 (From=From2 -> Result = [To2|RR], get_next_mapped_to_eq(T,From,RR,Tail)
4202 ; Result = [], Tail = [(From2,To2)|T]
4203 ).
4204
4205 % get all elements which map to From, supposing the list is sorted
4206 get_next_mapped_to([],_,[],[]).
4207 get_next_mapped_to([(From2,To2)|T],From,Result,Tail) :-
4208 (From=From2 -> Result = [To2|RR], get_next_mapped_to_eq(T,From,RR,Tail)
4209 ; From2 @> From -> Result = [], Tail = [(From2,To2)|T]
4210 ; get_next_mapped_to(T,From,Result,Tail)
4211 ).
4212
4213 calc_direct_product([],_From,_,Tail,Tail).
4214 calc_direct_product([To1|T1],From,To2List,Result,Tail) :-
4215 findall((From,(To1,To2))-true,member(To2,To2List),Result,ResResult),
4216 calc_direct_product(T1,From,To2List,ResResult,Tail).
4217
4218 % TO DO: maybe also add a special rule for infinite R such as event_b_identity ?
4219 domain_restriction_explicit_set_wf(S,R,Res,WF) :- /* S <| R */
4220 nonvar(R),
4221 (nonvar(S),is_one_element_custom_set(S,El),R \= closure(_,_,_) ->
4222 domain_restrict_singleton_element(El,R,Res)
4223 ; restriction_explicit_set_wf(S,R,Res,domain,pred_true,WF)).
4224 domain_subtraction_explicit_set_wf(S,R,Res,WF) :- /* S <<| R */
4225 (nonvar(S),is_one_element_custom_set(S,El), nonvar(R), R=avl_set(AVL) ->
4226 avl_domain_subtraction_singleton(AVL,El,ARes),
4227 construct_avl_set(ARes,Res) % TO DO: use this also when S is small and R large
4228 ; restriction_explicit_set_wf(S,R,Res,domain,pred_false,WF)).
4229 range_restriction_explicit_set_wf(R,S,Res,WF) :- /* R |> S */
4230 restriction_explicit_set_wf(S,R,Res,range,pred_true,WF).
4231 range_subtraction_explicit_set_wf(R,S,Res,WF) :- /* R |>> S */
4232 restriction_explicit_set_wf(S,R,Res,range,pred_false,WF).
4233
4234
4235 domain_restrict_singleton_element(El,R,Res) :- /* {El} <| R ; TO DO maybe apply this technique for "small" sets as well */
4236 nonvar(R), is_custom_explicit_set(R,domain_restrict_singleton_element),
4237 expand_and_convert_to_avl_set(R,AR,domain_restrict_singleton_element,''), % can generate ARel=empty; will fail if not possible to convert
4238 findall((El,Z)-true, avl_fetch_pair(El,AR,Z), RTuples),
4239 ord_list_to_avlset(RTuples,Res,domain_restrict_singleton_element).
4240
4241 restriction_explicit_set_wf(Set,Rel,Res,_RanOrDom,AddWhen,WF) :- Set==[],!,
4242 (AddWhen=pred_false
4243 -> equal_object_wf(Rel,Res,restriction_explicit_set_wf,WF) % {} <<| Rel = Rel |>> {} = Rel
4244 ; kernel_objects:empty_set_wf(Res,WF)
4245 ).
4246 restriction_explicit_set_wf(Set,Rel,Res,_RanOrDom,AddWhen,WF) :- is_definitely_maximal_set(Set),!,
4247 (AddWhen=pred_true
4248 -> equal_object_wf(Rel,Res,restriction_explicit_set_wf,WF) % TYPE <| Rel = Rel |> TYPE = Rel
4249 ; kernel_objects:empty_set_wf(Res,WF)
4250 ).
4251 restriction_explicit_set_wf(_,Rel,_,_,_,_) :- var(Rel),!,fail.
4252 restriction_explicit_set_wf(Set,closure(Paras,Types,Body),Res,RanOrDom,AddWhen,WF) :-
4253 % perform symbolic treatment by adding restriction predicate to Body
4254 !,
4255 (RanOrDom=domain
4256 -> get_domain_id_or_expr(Paras,Types,TID,TT)
4257 ; get_range_id_or_expr(Paras,Types,TID,TT)
4258 ),
4259 TSet=b(value(Set),set(TT),[]),
4260 (AddWhen = pred_true
4261 -> PRED = member(TID,TSet)
4262 ; PRED = not_member(TID,TSet) ),
4263 conjunct_predicates([b(PRED,pred,[]),Body],NewBody),
4264 % translate:print_bexpr(NewBody),nl,
4265 try_expand_and_convert_to_avl_with_catch_wf(closure(Paras,Types,NewBody),Res,restriction_explicit_set_wf,WF).
4266 restriction_explicit_set_wf(Set,Rel,Res,RanOrDom,AddWhen,WF) :-
4267 is_custom_explicit_set(Rel,restriction_explicit_set_wf),
4268 expand_and_convert_to_avl_set(Rel,ARel,restriction_explicit_set_wf,''), % can generate ARel=empty; will fail if not possible to convert
4269 avl_domain(ARel,ERel), % -> expand_custom_set(avl_set(ARel),ERel),
4270 %try_expand_and_convert_to_avl_unless_large_wf(Set,ES,WF),
4271 (nonvar(Set),Set=avl_set(AVLS)
4272 -> restrict2_avl(ERel,AVLS,DRes,RanOrDom,AddWhen,Done)
4273 ; restrict2(ERel,Set,DRes,RanOrDom,AddWhen,Done,WF)
4274 ),
4275 finish_restriction(Done,DRes,Res).
4276
4277 % extract domain expression for domain restriction/subtraction predicate:
4278 get_domain_id_or_expr([DR],[couple(TD,TR)], PRJ1, TD) :- !, % special case: just one parameter in closure
4279 TID = b(identifier(DR),couple(TD,TR),[]),
4280 PRJ1 = b(first_of_pair(TID),TD,[]).
4281 get_domain_id_or_expr([D1|Paras],[TD1|Types],Expr,Type) :-
4282 get_dom_couple_aux(Paras,Types, b(identifier(D1),TD1,[]), TD1, Expr,Type).
4283
4284 get_dom_couple_aux([_RangeID],[_], AccExpr, AccType, Expr, Type) :- !, Expr=AccExpr, Type=AccType.
4285 get_dom_couple_aux([D2|TParas],[TD2|Types], AccExpr, AccType, Expr, Type) :-
4286 TID2 = b(identifier(D2),TD2,[]),
4287 NewAccType = couple(AccType,TD2),
4288 NewAcc = b(couple(AccExpr,TID2),NewAccType,[]),
4289 get_dom_couple_aux(TParas,Types,NewAcc,NewAccType,Expr,Type).
4290
4291 :- use_module(library(lists),[last/2]).
4292 % extract range expression for range restriction/subtraction predicate:
4293 get_range_id_or_expr( [DR],[CType], PRJ2, TR) :- !, % special case: just one parameter in closure
4294 CType = couple(TD,TR),
4295 TID = b(identifier(DR),CType,[]),
4296 PRJ2 = b(second_of_pair(TID),TD,[]).
4297 get_range_id_or_expr( [_|Paras],[_|Types], b(identifier(R),TR,[]), TR) :-
4298 last(Paras,R), last(Types,TR).
4299
4300 :- block finish_restriction(-,?,?).
4301 finish_restriction(_,DRes,Res) :-
4302 ord_list_to_avlset(DRes,Restriction,restriction),
4303 ? equal_object(Restriction,Res,finish_restriction). % as we may block below: we need to use equal_object
4304
4305 restrict2([],_,[],_,_,done,_WF).
4306 restrict2([(From,To)|T],S,Res,RanOrDom,AddWhen,Done,WF) :-
4307 (RanOrDom==domain -> El=From ; El=To),
4308 kernel_equality:membership_test_wf(S,El,MemRes,WF), % TO DO: WF Version !!
4309 /* this only makes sense once we have the full result as argument:
4310 (nonvar(MemRes) -> true % it is already decided
4311 ; AddWhen=pred_true -> kernel_equality:membership_test_wf(Res,(From,To),MemRes,WF)
4312 ; kernel_equality:membership_test_wf(Res,(From,To),InResult,WF), bool_pred:negate(InResult,MemRes)
4313 ), */
4314 ? restrict3(MemRes,From,To,T,S,Res,RanOrDom,AddWhen,Done,WF).
4315 :- block restrict3(-, ?,?, ?,?,?, ?,?,?,?).
4316 restrict3(MemRes, From,To, T,S,Res, RanOrDom,AddWhen,Done,WF) :-
4317 (AddWhen=MemRes -> Res = [(From,To)-true|TRes]
4318 ; Res=TRes),
4319 ? restrict2(T,S,TRes,RanOrDom,AddWhen,Done,WF).
4320
4321 % optimised version when second set is also an AVL tree: less blocking,...
4322 restrict2_avl([],_,[],_,_,done).
4323 restrict2_avl([(From,To)|T],AVLS,Res,RanOrDom,AddWhen,Done) :-
4324 fetch(RanOrDom,From,To,AVLS,MemRes),
4325 (AddWhen=MemRes -> Res = [(From,To)-true|TRes]
4326 ; Res=TRes),
4327 restrict2_avl(T,AVLS,TRes,RanOrDom,AddWhen,Done).
4328
4329 fetch(domain,El,_,AVLS,MemRes) :- (avl_fetch(El,AVLS) -> MemRes=pred_true ; MemRes = pred_false).
4330 fetch(range,_,El,AVLS,MemRes) :- (avl_fetch(El,AVLS) -> MemRes=pred_true ; MemRes = pred_false).
4331
4332 % override R(X) := Y
4333 override_pair_explicit_set(avl_set(S),X,Y,avl_set(NewAVL)) :- element_can_be_added_or_removed_to_avl(X),
4334 element_can_be_added_or_removed_to_avl(Y),
4335 convert_to_avl_inside_set(X,AX),
4336 convert_to_avl_inside_set(Y,AY),
4337 avl_domain_subtraction_singleton(S,AX,AVL2),
4338 avl_store((AX,AY), AVL2, true, NewAVL).
4339
4340 avl_domain_subtraction_singleton(AVL,AX,NewAVL) :-
4341 avl_delete_pair(AX,AVL,_True,AVL2),
4342 !, % recurse, in case we have multiple entries
4343 % this recursion could be avoided if we know AVL to be a function
4344 avl_domain_subtraction_singleton(AVL2,AX,NewAVL).
4345 avl_domain_subtraction_singleton(AVL,_,AVL).
4346
4347 % try and decompose an AVL set into a cartesian product
4348 % AVL = Set1 * Set2
4349 % much faster e.g. for let xx = ((1..10)*(3..1000)\/ {0}*(3..1000)) and then xx = AA*BB
4350 % should not produce pending co-routines
4351 decompose_avl_set_into_cartesian_product_wf(AVL,DomainSet,RangeSet,WF) :-
4352 avl_domain(AVL,Expansion),
4353 decompose_cart(Expansion,'$none',DomainList,[],RangeList),
4354 construct_avl_from_lists_wf(DomainList,DomainSet,WF),
4355 construct_avl_from_lists_wf(RangeList,RangeSet,WF).
4356
4357 decompose_cart([],_,[],[],_).
4358 decompose_cart([(A,B)|T],Prev,Domain,Range,FullRange) :-
4359 (A=Prev
4360 -> Range = [B|TRange],
4361 decompose_cart(T,Prev,Domain,TRange,FullRange)
4362 ; Domain = [A|TDom], Range=[],
4363 FullRange = [B|TRange],
4364 decompose_cart(T,A,TDom,TRange,FullRange)
4365 ).
4366
4367 /* --------- */
4368 /* EXPANSION */
4369 /* --------- */
4370
4371 :- use_module(b_global_sets,[all_elements_of_type_wf/3, all_elements_of_type_rand_wf/3]).
4372 :- use_module(kernel_freetypes,[expand_freetype/3]).
4373
4374 expand_custom_set(X,R) :- expand_custom_set_wf(X,R,expand_custom_set,no_wf_available).
4375 expand_custom_set(X,R,Src) :- expand_custom_set_wf(X,R,Src,no_wf_available).
4376 expand_custom_set_wf(X,R,Source,WF) :- var(X), !,
4377 add_error_and_fail(expand_custom_set_wf, 'Variable as argument: ',expand_custom_set_wf(X,R,Source,WF)).
4378 expand_custom_set_wf(global_set(GS),ExpandedSet,_,WF) :- !,
4379 all_elements_of_type_wf(GS,ExpandedSet,WF). % they are generated in order
4380 expand_custom_set_wf(freetype(GS),ValueList,_,WF) :- !,
4381 expand_freetype(GS,ValueList,WF).
4382 expand_custom_set_wf(avl_set(AVL),ExpandedSet,_,_) :- !,
4383 avl_domain(AVL,ExpandedSet).
4384 expand_custom_set_wf(closure(Parameters,PTypes,Cond),Res,Source,WF) :- !,
4385 ? expand_closure_to_list(Parameters,PTypes,Cond,Res,_Done,Source,WF).
4386 %wait_try_expand_custom_set(Res1,Res). % could be in AVL form; no longer the case !
4387 expand_custom_set_wf(Set,_,Source,_) :-
4388 add_error_and_fail(expand_custom_set(Source),'Cannot expand custom set: ',Set).
4389
4390
4391
4392 %try_expand_only_custom_closure_global(X,Y) :-
4393 % (var(X) -> X=Y ; expand_only_custom_closure_global(X,Y,check)).
4394
4395 expand_only_custom_closure_global(X,R,C,_WF) :- var(X), !,
4396 add_error_and_fail(expand_only_custom_closure_global, 'Variable as argument: ',expand_only_custom_closure_global(X,R,C)).
4397 expand_only_custom_closure_global(global_set(GS),ExpandedSet,_,WF) :- !,all_elements_of_type_wf(GS,ExpandedSet,WF).
4398 expand_only_custom_closure_global(freetype(GS),ExpandedSet,_,_WF) :- !,ExpandedSet=freetype(GS).
4399 expand_only_custom_closure_global(avl_set(AVL),ExpandedSet,_,_WF) :- !, ExpandedSet=avl_set(AVL).
4400 expand_only_custom_closure_global(closure(Parameters,PTypes,Cond),Res,CheckTimeOuts,WF) :- !,
4401 (Res==[] -> is_empty_explicit_set(closure(Parameters,PTypes,Cond)) % TO DO: think about other special cases
4402 ; expand_closure_to_avl_or_list(Parameters,PTypes,Cond,Res,CheckTimeOuts,WF)).
4403 expand_only_custom_closure_global(Set,Set,_CheckTimeOuts,_WF).
4404 %:- add_error_and_fail(expand_only_custom_closure_global,'Cannot expand custom set: ',Set).
4405
4406
4407 try_expand_custom_set_with_catch(CS,Expansion,PP) :-
4408 on_enumeration_warning(try_expand_custom_set_wf(CS,Expansion,PP,no_wf_available),
4409 Expansion=CS).
4410
4411 try_expand_custom_set(CS,Expansion) :-
4412 try_expand_custom_set_wf(CS,Expansion,try_expand_custom_set,no_wf_available).
4413
4414
4415 try_expand_custom_set_wf(CS,Res,_,_) :- var(CS),!,Res=CS.
4416 try_expand_custom_set_wf([],Res,_,_) :- !, Res=[].
4417 try_expand_custom_set_wf([H|T],Res,_,_) :- !, Res=[H|T].
4418 try_expand_custom_set_wf(CS,Res,Src,WF) :-
4419 expand_custom_set_wf(CS,Res,Src,WF). % will generate error message for illegal sets
4420
4421
4422 :- assert_must_succeed((expand_custom_set_to_list(closure(['_zzzz_unit_tests'],
4423 [couple(integer,integer)],
4424 b(member(b(identifier('_zzzz_unit_tests'),couple(integer,integer),[generated]),
4425 b(value([(int(1),int(22))]),set(couple(integer,integer)),[])),pred,[])),R),R==[(int(1),int(22))])).
4426
4427 expand_custom_set_to_list(CS,List) :- expand_custom_set_to_list(CS,List,_Done,unknown).
4428
4429 % a version of expansion which returns guaranteed_ground if the List is guaranteed to be ground
4430 expand_custom_set_to_list_gg(CS,List,GuaranteedGround,_PP) :-
4431 nonvar(CS), CS=avl_set(AVL), var(List),
4432 !,
4433 GuaranteedGround = guaranteed_ground,
4434 avl_domain(AVL,List).
4435 expand_custom_set_to_list_gg(CS,List,not_guaranteed_ground,PP) :-
4436 expand_custom_set_to_list(CS,List,_Done,PP).
4437
4438 % a version where the expansion should happen straightaway and should not block:
4439 expand_custom_set_to_list_now(CS,List) :- expand_custom_set_to_list(CS,List,Done,unknown),
4440 (Done==true -> true ; print_error(expand_custom_set_to_list_not_done(CS,List))).
4441
4442 :- block expand_custom_set_to_sorted_list(-,-,?,?,?).
4443 % sorts the resulting list if needed
4444 % due to random enumeration
4445 expand_custom_set_to_sorted_list(From,To,Done,Source,WF) :-
4446 expand_custom_set_to_list(From,UnsortedTo,Done,Source),
4447 (preferences:get_preference(randomise_enumeration_order,true)
4448 -> sort_when_done(Done,UnsortedTo,To,WF) ; UnsortedTo = To).
4449
4450 :- block sort_when_done(-,?,?,?).
4451 sort_when_done(_,Unsorted,Res,WF) :- sort(Unsorted,Sorted),
4452 equal_object_wf(Sorted,Res,sort_when_done,WF).
4453
4454 expand_custom_set_to_list(From,To,Done,Source) :-
4455 expand_custom_set_to_list_wf(From,To,Done,Source,no_wf_available).
4456
4457 :- use_module(kernel_objects,[equal_object_wf/4]).
4458
4459 % try expand custom set to list; on enumeration warning set Done to enumeration_warning
4460 try_expand_custom_set_to_list_wf(From,To,Done,Source,WF) :-
4461 on_enumeration_warning(expand_custom_set_to_list_wf(From,To,Done,Source,WF),
4462 (Done=enumeration_warning)).
4463
4464 expand_custom_set_to_list_wf(From,To,Done,Source,WF) :-
4465 expand_custom_set_to_list_k_wf(From,To,Done,_Kind,Source,WF).
4466
4467 % a variation of expand_custom_set_to_list which also checks that there are no duplicates in the list
4468 expand_custom_set_to_list_no_dups_wf(From,To,Done,Source,WF) :-
4469 expand_custom_set_to_list_k_wf(From,To,Done,Kind,Source,WF),
4470 check_dups(Kind,To,WF).
4471
4472 :- block check_dups(-,?,?).
4473 check_dups(unsorted_list,List,WF) :- !,
4474 kernel_objects:check_no_duplicates_in_list(List,[],WF).
4475 check_dups(_,_,_).
4476
4477 % warn if duplicates in list; to do: use in prob_safe mode
4478 %:- block warn_dups(-,?,?,?).
4479 %warn_dups(unsorted_list,List,Src,WF) :- !,
4480 % kernel_objects:warn_if_duplicates_in_list(List,Src,WF).
4481 %warn_dups(_,_,_,_).
4482
4483
4484
4485 :- block expand_custom_set_to_list_k_wf(-,-,?,?,?,?).
4486 % ensures that the output is a pure list; the list skeleton should not be instantiated by anybody else
4487 expand_custom_set_to_list_k_wf(From,To,Done,Kind,Source,WF) :-
4488 (var(From) ->
4489 (is_list_skeleton(To)
4490 ? -> equal_object_wf(To,From,Source,WF), Done=true, Kind=unsorted_list
4491 ? ; expand_custom_set_to_list2(To,From,Done,Kind,Source,WF))
4492 ; var(To),is_list_skeleton(From)
4493 -> To=From, Done=true, Kind=unsorted_list % equal_object_wf will also to a Prolog unification
4494 ? ; expand_custom_set_to_list2(From,To,Done,Kind,Source,WF)).
4495
4496 expand_custom_set_to_list2([],ExpandedSet,Done,Kind,_Source,WF) :- !,
4497 ? equal_object_wf([],ExpandedSet,expand_custom_set_to_list2,WF),Done=true,Kind=empty_set.
4498 expand_custom_set_to_list2([H|T],ExpandedSet,Done,Kind,Source,WF) :- !, Kind=unsorted_list,
4499 ? equal_object_wf([H|ET],ExpandedSet,expand_custom_set_to_list2,WF),
4500 ? expand_custom_set_to_list3(T,ET,Done,Source,WF).
4501 expand_custom_set_to_list2(global_set(GS),ExpandedSet,Done,Kind,_Source,WF) :- !,
4502 all_elements_of_type_rand_wf(GS,R,WF),
4503 check_list(R,expand_custom_set_to_list2),
4504 equal_object_wf(R,ExpandedSet,expand_custom_set_to_list2,WF),Done=true,Kind=sorted_list.
4505 expand_custom_set_to_list2(avl_set(AVL),ExpandedSet,Done,Kind,_Source,WF) :- !,
4506 avl_domain(AVL,R),
4507 ? equal_object_wf(R,ExpandedSet,expand_custom_set_to_list2,WF), Done=true,Kind=sorted_list.
4508 expand_custom_set_to_list2(closure(Parameters,PTypes,Cond),ExpandedSet,Done,Kind,Source,WF) :- !,
4509 expand_closure_to_list(Parameters,PTypes,Cond,ExpandedSet,Done,Source,WF),
4510 Kind=sorted_list.
4511 %assign_expand_result(CDone,Res,ExpandedSet,Done).
4512 expand_custom_set_to_list2(freetype(ID),ExpandedSet,Done,Kind,_Source,WF) :- !,
4513 expand_freetype(ID,R,WF),
4514 equal_object_wf(R,ExpandedSet,expand_custom_set_to_list2,WF),
4515 Done=true,Kind=sorted_list.
4516 % missing avl_set wrapper:
4517 expand_custom_set_to_list2(node(A,B,C,D,E),ExpandedSet,Done,Kind,Source,WF) :- !,
4518 add_internal_error('Illegal argument: ',expand_custom_set_to_list2(node(A,B,C,D,E),ExpandedSet,Done,Source)),
4519 expand_custom_set_to_list2(avl_set(node(A,B,C,D,E)),ExpandedSet,Done,Kind,Source,WF).
4520 expand_custom_set_to_list2(E,ES,Done,Kind,Source,WF) :-
4521 add_internal_error('Illegal argument: ',expand_custom_set_to_list2(E,ES,Done,Kind,Source,WF)),fail.
4522
4523 :- block expand_custom_set_to_list3(-,-,?,?,?). % we are no longer sure which was From and which is To
4524 expand_custom_set_to_list3(From,To,Done,Source,WF) :-
4525 ? (var(From) -> expand_custom_set_to_list2(To,From,Done,_,Source,WF) ;
4526 ? expand_custom_set_to_list2(From,To,Done,_,Source,WF)).
4527
4528
4529 is_list_skeleton(X) :- var(X),!,fail.
4530 is_list_skeleton([]).
4531 is_list_skeleton([_|T]) :- is_list_skeleton(T).
4532
4533 % true if it is more efficient to keep this, rather than expand into list
4534 is_efficient_custom_set(avl_set(_)).
4535 is_efficient_custom_set(closure(P,T,B)) :-
4536 (is_interval_closure(closure(P,T,B),_,_) -> true ; is_infinite_or_symbolic_closure(P,T,B)).
4537 ?is_efficient_custom_set(global_set(X)) :- is_infinite_global_set(X,_).
4538 is_efficient_custom_set(freetype(_)).
4539
4540 % tries to expand & convert to avl_set; fails if not possible: NOTE: also generates empty AVL
4541 expand_and_convert_to_avl_set(R,AER,Origin,Source) :-
4542 try_expand_and_convert_to_avl(R,ER,Origin,Source),
4543 nonvar(ER),(ER==[] -> AER=empty ; ER=avl_set(AER)).
4544
4545
4546 expand_and_convert_to_avl_set_unless_very_large(R,AER,WF) :-
4547 try_expand_and_convert_to_avl_unless_very_large_wf(R,ER,WF),
4548 nonvar(ER),(ER==[] -> AER=empty ; ER=avl_set(AER)).
4549
4550
4551 % similar to unless_large version, but will only expand if it is guaranteed to be small
4552
4553 try_expand_and_convert_to_avl_if_smaller_than(freetype(GS),Res,_) :- !, Res = freetype(GS).
4554 try_expand_and_convert_to_avl_if_smaller_than([H|T],Res,_) :- !, try_expand_and_convert_to_avl([H|T],Res).
4555 try_expand_and_convert_to_avl_if_smaller_than(avl_set(A),Res,_) :- !, Res=avl_set(A).
4556 try_expand_and_convert_to_avl_if_smaller_than(CS,Res,Limit) :-
4557 (is_small_specific_custom_set(CS,Limit)
4558 -> try_expand_and_convert_to_avl(CS,Res,try_expand_and_convert_to_avl_if_smaller_than,'')
4559 ; Res = CS % TO DO: maybe look at cardinality of types and determine max. cardinality
4560 ).
4561 is_small_specific_custom_set(CS,Limit) :- card_for_specific_custom_set(CS,Card,Code),
4562 call(Code), is_finite_card(Card), Card<Limit.
4563 get_card_for_specific_custom_set(CS,Card) :-
4564 card_for_specific_custom_set(CS,Card,Code),
4565 call(Code), ground(Card).
4566
4567 try_expand_and_convert_to_avl_unless_very_large_wf(CS,Res,WF) :-
4568 try_expand_and_convert_to_avl_unless_large_wf(CS,Res,10000,WF).
4569
4570 try_expand_and_convert_to_avl_unless_large_wf(CS,Res,WF) :-
4571 try_expand_and_convert_to_avl_unless_large_wf(CS,Res,2000,WF).
4572
4573 try_expand_and_convert_to_avl_unless_large_wf(CS,Res,_,_WF) :- var(CS), !, CS=Res.
4574 try_expand_and_convert_to_avl_unless_large_wf(global_set(GS),Res,_,_WF) :- !, Res = global_set(GS).
4575 try_expand_and_convert_to_avl_unless_large_wf(freetype(GS),Res,_,_WF) :- !, Res = freetype(GS).
4576 %try_expand_and_convert_to_avl_unless_large_wf(CS,Res,_WF) :- is_interval_closure(CS,Low,Up),!,
4577 % ((ground(Low),ground(Up),Size is 1+Up-Low, Size<2000)
4578 %% -> try_expand_and_convert_to_avl(CS,Res)
4579 % ; Res = CS
4580 % ).
4581 try_expand_and_convert_to_avl_unless_large_wf(closure(P,T,B),Res,Limit,_WF) :-
4582 is_very_large_or_symbolic_closure(P,T,B,Limit),!, % is explicitly marked as SYMBOLIC
4583 Res=closure(P,T,B).
4584 try_expand_and_convert_to_avl_unless_large_wf(CS,Res,_Limit,WF) :-
4585 % TO DO: check if maybe we cannot determine card explicitly, but have a large lower-bound
4586 try_expand_and_convert_to_avl_wf(CS,Res,try_expand_and_convert_to_avl_unless_large,'',WF).
4587
4588
4589
4590 % calls try_expand_and_convert_to_avl and returns original value if enumeration warning occured
4591 try_expand_and_convert_to_avl_with_catch_wf(CS,Res,Origin,WF) :-
4592 on_enumeration_warning(try_expand_and_convert_to_avl_wf(CS,Res,Origin,'',WF),
4593 Res=CS).
4594
4595 /* tries to generate an avl-structure, if possible */
4596 try_expand_and_convert_to_avl(CS,Res) :-
4597 try_expand_and_convert_to_avl_wf(CS,Res,try_expand_and_convert_to_avl,'',no_wf_available).
4598
4599 try_expand_and_convert_to_avl(CS,Res,Origin,Source) :-
4600 try_expand_and_convert_to_avl_wf(CS,Res,Origin,Source,no_wf_available).
4601
4602 try_expand_and_convert_to_avl_wf(CS,Res,_,_,_WF) :- var(CS), !, CS=Res.
4603 try_expand_and_convert_to_avl_wf(avl_set(A),R,_,_,_WF) :- !, R=avl_set(A).
4604 try_expand_and_convert_to_avl_wf([],R,_,_,_WF) :- !, R=[].
4605 try_expand_and_convert_to_avl_wf([H|T],R,_,_,WF) :- !, try_convert_to_avl_wf([H|T],R,WF).
4606 try_expand_and_convert_to_avl_wf(closure(P,T,B),Res,Origin,_Source,WF) :- !,
4607 debug_opt_push_wait_flag_call_stack_info(WF,
4608 external_call('TRY EXPANDING',[closure(P,T,B)],unknown),WF2),
4609 expand_only_custom_closure_global(closure(P,T,B),Expansion,check(Origin),WF2),
4610 try_convert_to_avl_wf(Expansion,Res,WF).
4611 try_expand_and_convert_to_avl_wf(CS,Res,Origin,_Source,WF) :-
4612 (\+ is_custom_explicit_set(CS,try_expand_and_convert_to_avl_wf)
4613 -> Expansion = CS
4614 ; expand_only_custom_closure_global(CS,Expansion,check(Origin),WF)
4615 ),
4616 try_convert_to_avl_wf(Expansion,Res,WF).
4617
4618 try_convert_to_avl(Expansion,Res) :-
4619 (should_be_converted_to_avl_from_lists(Expansion) -> construct_avl_from_lists(Expansion,Res) ; Res=Expansion).
4620 try_convert_to_avl_wf(Expansion,Res,WF) :-
4621 (should_be_converted_to_avl_from_lists(Expansion) -> construct_avl_from_lists_wf(Expansion,Res,WF) ; Res=Expansion).
4622
4623 should_be_converted_to_avl_from_lists(Value) :- var(Value),!,fail.
4624 should_be_converted_to_avl_from_lists(Value) :-
4625 \+ is_custom_explicit_set(Value,should_be_converted_to_avl_from_lists), % already avl_set, global_set or closure
4626 ? \+ do_not_convert_aux(Value),
4627 ground_value(Value).
4628
4629 do_not_convert_aux(V) :- var(V),!.
4630 do_not_convert_aux((A,B)) :- !,
4631 ? (do_not_convert_aux(A) -> true ; do_not_convert_aux(B)).
4632 do_not_convert_aux([H|T]) :- !, % do not convert a set containing a symbolic closure
4633 ? (var(T) -> true ; do_not_convert_aux(H)).
4634 do_not_convert_aux(rec(Fields)) :- !,
4635 (var(Fields) -> true
4636 ? ; member(field(_,V),Fields), do_not_convert_aux(V) -> true).
4637 do_not_convert_aux(H) :-
4638 ? is_symbolic_closure(H).
4639
4640 should_be_converted_to_avl(Value) :- %preference(use_avl_trees_for_sets,true),
4641 ground_value(Value).
4642
4643 try_expand_and_convert_to_avl_with_check(CS,Res,Origin) :-
4644 try_expand_and_convert_to_avl_with_check(CS,Res,do_not_keep_intervals,Origin).
4645
4646 try_expand_and_convert_to_avl_with_check(CS,Res,_,_Origin) :- var(CS),!, Res = CS.
4647 try_expand_and_convert_to_avl_with_check([],Res,_,_Origin) :- !, Res=[].
4648 try_expand_and_convert_to_avl_with_check(avl_set(A),Res,_,_Origin) :- !, Res=avl_set(A).
4649 try_expand_and_convert_to_avl_with_check([H|T],Res,_,Origin) :- !, try_expand_and_convert_to_avl([H|T],Res,Origin,'').
4650 %try_expand_and_convert_to_avl_with_check(CS,Res,_Origin) :-
4651 % \+ is_custom_explicit_set(CS,try_expand_and_convert_to_avl),!, Res = CS.
4652 try_expand_and_convert_to_avl_with_check(CS,Res,KeepIntervals,_Origin) :-
4653 is_interval_closure(CS,Low,Up),
4654 (var(Low) -> true ; var(Up) -> true % better keep this symbolic as we may be able to do constraint propagation
4655 ; KeepIntervals=keep_intervals(Size) -> Up-Low >= Size
4656 ),
4657 !, % TO DO: see if we should do this check in try_expand_and_convert_to_avl above instead
4658 Res=CS.
4659 try_expand_and_convert_to_avl_with_check(CS,Res,_,Origin) :-
4660 get_card_for_specific_custom_set(CS,Size), % TO DO: avoid checking for special closures twice (below in try_expand_and_convert_to_avl ?)
4661 !,
4662 try_expconv_to_avl_with_size(Size,CS,Res,Origin).
4663 try_expand_and_convert_to_avl_with_check(CS,Res,_,Origin) :-
4664 try_expand_and_convert_to_avl(CS,Res,Origin,'').
4665
4666 try_expconv_to_avl_with_size(inf,CS,Res,Origin) :- !,
4667 debug_format(9,'### Not expanding infinite set~n### ORIGIN: ~w~n',[Origin]),
4668 Res=CS.
4669 try_expconv_to_avl_with_size(inf_overflow,CS,Res,Origin) :- !,
4670 debug_format(9,'### Not expanding very large set~n### ORIGIN: ~w~n',[Origin]),
4671 Res=CS.
4672 try_expconv_to_avl_with_size(Size,CS,Res,Origin) :- Size>=10000000, !,
4673 /* will probably never terminate */
4674 debug_format(9,'### Not expanding very large set with cardinality ~w~n### ORIGIN: ~w~n',[Size,Origin]),
4675 Res=CS.
4676 try_expconv_to_avl_with_size(Size,CS,Res,Origin) :- Size>=50000, !,
4677 print('### WARNING: expanding very large comprehension set, size = '), print(Size),nl,
4678 print('### ORIGIN: '), print(Origin),nl,
4679 try_expand_and_convert_to_avl(CS,Res,Origin,'').
4680 try_expconv_to_avl_with_size(_Size,CS,Res,Origin) :-
4681 try_expand_and_convert_to_avl(CS,Res,Origin,'').
4682
4683 /* underlying assumption for var case: if G is a global set: we get back the
4684 global_set tag immediately: no need to use when to wait;
4685 better: ensure that b_compute_expression always returns a nonvar term */
4686
4687
4688 :- assert_must_succeed((custom_explicit_sets:try_expand_custom_set(closure([xx],[integer],b(falsity,pred,[])),R),R = [])).
4689 :- assert_must_succeed((custom_explicit_sets:test_closure(X),custom_explicit_sets:expand_custom_set(X,EX),
4690 EX = [(fd(1,'Name'),_),(fd(3,'Name'),_)])).
4691
4692 test_closure(X) :- X = closure(['_zzzz_binary'],[couple(global('Name'),set(global('Name')))],
4693 b(member(b(identifier('_zzzz_binary'),couple(global('Name'),set(global('Name'))),[generated]),
4694 b(cartesian_product(b(value([fd(1,'Name'),fd(3,'Name')]),set(global('Name')),[]),
4695 b(value([[fd(2,'Name'),fd(3,'Name')]]),set(set(global('Name'))),[])),
4696 set(couple(global('Name'),set(global('Name')))),[])),pred,[])).
4697
4698
4699 /* --------- */
4700 /* ELEMENT_OF */
4701 /* --------- */
4702
4703
4704 /* A function that instantiates last argument when membership test can be decided */
4705
4706 membership_custom_set(CS,X,R) :- print(warning_deprecated_non_wf_version(CS,X,R)),nl,
4707 membership_custom_set_wf(CS,X,R,_WF).
4708
4709 ?membership_custom_set_wf(avl_set(A),X,R,WF) :- !, membership_avl_set_wf(A,X,R,WF).
4710 membership_custom_set_wf(freetype(_GS),_X,R,_WF) :- !, R=pred_true. % should be covered by clause above
4711 membership_custom_set_wf(CS,X,R,WF) :- R==pred_true,!, element_of_custom_set_wf(X,CS,WF).
4712 membership_custom_set_wf(CS,X,R,WF) :- R==pred_false,!, not_element_of_custom_set_wf(X,CS,WF).
4713 membership_custom_set_wf(CS,_X,R,_WF) :-
4714 is_definitely_maximal_set(CS),!,
4715 R=pred_true.
4716 membership_custom_set_wf(closure(Par,Types,Body),X,R,WF) :- !,
4717 ? closure_membership_wf(X,Par,Types,Body,R,WF).
4718 %membership_custom_set_wf(CS,X,R,WF) :- is_one_element_custom_set(CS,Y),!, % only succeeds for AVL
4719 % kernel_equality:equality_objects_wf_no_enumr(X,Y,R,WF).
4720 membership_custom_set_wf(global_set(GS),X,R,WF) :- !,
4721 membership_global_set(GS,X,R,WF).
4722 membership_custom_set_wf(CS,X,R,WF) :-
4723 add_internal_error('Illegal custom set: ',membership_custom_set_wf(CS,X,R,WF)),fail.
4724
4725 membership_avl_set_wf(A,X,R,WF) :- R==pred_true,!, element_of_avl_set_wf(A,X,WF).
4726 membership_avl_set_wf(A,X,R,WF) :- R==pred_false,!, not_element_of_custom_set_wf(X,avl_set(A),WF).
4727 membership_avl_set_wf(A,X,R,WF) :- is_one_element_avl(A,Y),!,
4728 ? kernel_equality:equality_objects_wf_no_enum(X,Y,R,WF).
4729 membership_avl_set_wf(A,_X,R,_WF) :-
4730 quick_definitely_maximal_set_avl(A),!,
4731 R=pred_true.
4732 membership_avl_set_wf(A,X,R,WF) :- reify_avl_membership(A,X,R,FullReification),
4733 (FullReification==true
4734 -> true %print_term_summary(full_reification(A,X,R)),nl,nl %% did slow down e.g. Bosch Deadlock v9, seems no longer the case
4735 ? ; when((ground(X);nonvar(R)),membership_avl_set_wf2(A,X,R,WF))).
4736
4737 ?membership_avl_set_wf2(A,X,R,WF) :- R==pred_true,!, element_of_avl_set_wf(A,X,WF).
4738 membership_avl_set_wf2(A,X,R,WF) :- R==pred_false,!, not_element_of_custom_set_wf(X,avl_set(A),WF).
4739 membership_avl_set_wf2(AVL,X,R,_WF) :-
4740 ground_element_can_be_added_or_removed_to_avl(X), !,
4741 (safe_avl_member(X,AVL) %safe_avl_member_ground(X,AVL)
4742 -> R=pred_true ; R=pred_false).
4743 membership_avl_set_wf2(AVL,X,Res,WF) :- % X is ground but cannot be added
4744 (Res \== pred_false, element_of_avl_set_wf(AVL,X,WF), Res=pred_true
4745 ;
4746 Res \== pred_true, not_element_of_custom_set_wf(X,avl_set(AVL),WF), Res=pred_false).
4747
4748 membership_global_set(GS,_X,R,_WF) :- is_maximal_global_set(GS),!,
4749 R=pred_true.
4750 membership_global_set(GS,X,R,WF) :- ground(X),!,
4751 (element_of_global_set_wf(X,GS,WF) -> R=pred_true ; R=pred_false).
4752 membership_global_set(GS,X,R,_WF) :- get_integer_set_interval(GS,Low,Up),!,
4753 membership_interval(X,Low,Up,R).
4754 membership_global_set(GS,X,R,WF) :- % this case should probably never apply
4755 (GS=='FLOAT' -> true % currently it actually is also treated like REAL
4756 ; print(uncovered_membership(GS,X,R,WF)),nl),
4757 when(ground(X), (element_of_global_set_wf(X,GS,WF) -> R=pred_true ; R=pred_false)).
4758
4759 membership_interval(X,Low,Up,Res) :- nonvar(Up),Up=inf,!,X=int(IX),
4760 b_interpreter_check:check_arithmetic_operator('<=',Low,IX,Res).
4761 membership_interval(X,Low,Up,Res) :- kernel_equality:in_nat_range_test(X,int(Low),int(Up),Res).
4762
4763 :- use_module(bool_pred).
4764 closure_membership_wf(X,[ZZZZ],[integer],CondClosure,Res,_WF) :-
4765 is_interval_closure_body(CondClosure,ZZZZ,LOW,UP),!,
4766 kernel_equality:in_nat_range_test(X,int(LOW),int(UP),Res).
4767 % TO DO: deal with open intervals 0..inf ...
4768 closure_membership_wf(X,Par,Types,Body,Res,WF) :-
4769 is_member_closure(Par,Types,Body,_Type,VAL),
4770 (VAL=value(_) ; VAL = cartesian_product(b(value(A),_,_),b(value(B),_,_))),!,
4771 (VAL=value(Set)
4772 -> kernel_objects:membership_test_wf(Set,X,Res,WF)
4773 ? ; kernel_equality:cartesian_pair_test_wf(X,A,B,Res,WF)).
4774 closure_membership_wf(X,Par,Typ,Body,Res,WF) :-
4775 is_not_member_closure(Par,Typ,Body,_Type,value(Set)),!,
4776 bool_pred:negate(ResXSet,Res), % was kernel_equality:inv_mem_obj(ResXSet,Res),
4777 kernel_objects:membership_test_wf(Set,X,ResXSet,WF).
4778 % TO DO: if closure = POW closure -> translate into subset_test pow_subset
4779 % TO DO: support a few other closures related to symbolic unary/binary operators: closure1, POW(..), ... ?
4780 % TO DO: expand if set is small
4781 closure_membership_wf(X,Par,Types,Body,Res,WF) :- ground_value(X),!,
4782 closure_membership_ground_wf(X,closure(Par,Types,Body),Res,WF).
4783 closure_membership_wf(X,Par,Types,Body,Res,WF) :-
4784 CS = closure(Par,Types,Body),
4785 is_small_specific_custom_set(CS,100),
4786 try_expand_and_convert_to_avl_wf(CS,Expanded,closure_membership_wf,'',WF),
4787 nonvar(Expanded), Expanded=avl_set(_),
4788 !,
4789 membership_custom_set_wf(Expanded,X,Res,WF).
4790 closure_membership_wf(X,Par,Types,Body,Res,WF) :-
4791 Body \= b(member(_,_),_,_), % otherwise we may have an infinite loop; b_check_boolean_expression will generate a closure which will call closure_membership_wf again; TO DO: refine to allow certain memberships to go through
4792 get_texpr_info(Body,BodyInfo),
4793 \+ member(prob_annotation(recursive(_RID)),BodyInfo), % otherwise we can get errors as recursive identifier _RID needs to be added to local state ! (test 1151 fails otherwise)
4794 % TO DO: add recursive parameter below in set_up_typed_localstate2; + in which other circumstances do we need to set up recursion identifier !
4795 % Try reifiyng the body
4796 NegationContext=positive,
4797 copy_wf_start(WF,closure_membership_wf,CWF),
4798 b_interpreter:set_up_typed_localstate2(Par,Types,BodyInfo,ParValues,TypedVals,[],State,NegationContext),
4799 %couplise_list(Types,XType),
4800 convert_list_into_pairs(ParValues,SingleParValue),
4801 kernel_objects:equal_object(X,SingleParValue,closure_membership_wf),
4802 b_interpreter_check:b_check_boolean_expression(Body,[],State,CWF,PredRes),
4803 !,
4804 (debug_mode(on) -> print('REIFICATION of closure: '), translate:print_bexpr(Body),nl, print(pred_res(X,PredRes)),nl ; true),
4805 b_enumerate:b_tighter_enumerate_all_values(TypedVals,WF), % not necessary ?? as X should get enumerated
4806 Res=PredRes,
4807 copy_wf_finish(WF,CWF).
4808 closure_membership_wf(X,Par,Types,Body,Res,WF) :-
4809 when( (ground(X);nonvar(Res)), %%
4810 % used to be ground(X), % with (ground(X);nonvar(Res)), test 292 failed {x,t|t : BOOL & (x : POW(1024 .. 1025) & bool(x : POW(NATURAL1)) = t)} = {{} |-> TRUE,{1024} |-> TRUE,{1024,1025} |-> TRUE,{1025} |-> TRUE} and test 1088 failed
4811 closure_membership_ground_wf(X,closure(Par,Types,Body),Res,WF)).
4812
4813 closure_membership_ground_wf(X,CS,Res,WF) :- nonvar(Res),!,
4814 % this optimization is checked in test 1452
4815 (Res==pred_true -> element_of_custom_set_wf(X,CS,WF) ; not_element_of_custom_set_wf(X,CS,WF)).
4816 closure_membership_ground_wf(X,CS,Res,WF) :-
4817 % to ensure that we leave no choice point behind we have to force full evaluation of element/not_element calls:
4818 % hence we do not call element_of_custom_set_wf or not_element_of_custom_set_wf below !!
4819 kernel_waitflags:get_idle_wait_flag(closure_membership_ground_wf,WF,LWF), % enable other triggered co-routines to fire first; some maybe much more efficient to deal with than closure expansion;
4820 % used to be important for test 1146, but this is no longer the case
4821 %term_variables(CS,Vars),print(closure_membership_ground_wf_aux(LWF,vars(Vars),CS)),nl,
4822 ground_value_check(CS,CSGr),
4823 %when((nonvar(LWF),(nonvar(CSGr);nonvar(Res))),closure_membership_ground_wf_aux(X,CS,Res)).
4824 block_closure_membership_ground_wf_aux(X,CS,Res,CSGr,LWF,WF). % Note: wrong block in commit 332cb17487017d819e9140427b1017a3045b3685 caused problem for test 1162
4825
4826 :- block block_closure_membership_ground_wf_aux(?,?,?,?,-,?),
4827 block_closure_membership_ground_wf_aux(?,?,-,-,?,?).
4828 block_closure_membership_ground_wf_aux(X,CS,Res, _,_,WF) :-
4829 ? closure_membership_ground_wf_aux(X,CS,Res,WF).
4830
4831 % X & CS are ground or Res is known
4832 closure_membership_ground_wf_aux(X,CS,Res,WF) :- Res==pred_true,!,
4833 element_of_custom_set_wf(X,CS,WF).
4834 closure_membership_ground_wf_aux(X,CS,Res,WF) :- Res==pred_false,!,
4835 not_element_of_custom_set_wf(X,CS,WF).
4836 closure_membership_ground_wf_aux(X,CS,Res,_WF) :-
4837 % we know that X is a ground value and CS is ground: we can determine completely whether X is element of CS or not
4838 ? if(element_of_custom_set(X,CS),Res=pred_true, Res=pred_false).
4839 /* used to be: (Res \== pred_false, element_of_custom_set(X,CS), Res=pred_true
4840 ; Res \== pred_true, not_element_of_custom_set(X,CS), Res=pred_false)).
4841 */
4842
4843
4844
4845 :- use_module(kernel_objects,[element_of_global_set/2,element_of_global_set_wf/3]).
4846 element_of_custom_set_wf(X,CS,WF) :-
4847 ? element_of_custom_set_wf2(CS,X,WF). %, print(check_ok(X)),nl.
4848
4849 element_of_custom_set_wf2(node(A,B,C,D,E),X,WF) :-
4850 add_internal_error('Unwrapped avl_set: ',element_of_custom_set_wf2(node(A,B,C,D,E),X,WF)),fail.
4851 element_of_custom_set_wf2(global_set(GS),X,WF) :- element_of_global_set_wf(X,GS,WF).
4852 element_of_custom_set_wf2(freetype(ID),X,WF) :-
4853 (is_maximal_freetype(ID) -> true
4854 ; add_internal_error('Uncovered case: ',element_of_custom_set_wf2(freetype(ID),X,WF))
4855 ). % we assume freetypes to be maximal !
4856 ?element_of_custom_set_wf2(avl_set(AVL),X,WF) :- element_of_avl_set_wf(AVL,X,WF).
4857 element_of_custom_set_wf2(closure(Parameters,PT,Cond),X,WF) :-
4858 ? element_of_closure(X,Parameters,PT,Cond,WF).
4859
4860 element_of_avl_set_wf(node(Y,_,_,empty,empty),X,WF) :- !,
4861 ? kernel_objects:equal_object_wf(X,Y,element_of_custom_set_wf2,WF).
4862 element_of_avl_set_wf(AVL,X,_WF) :- ground_value(X),!, safe_avl_member(X,AVL). %safe_avl_member_ground(X,AVL).
4863 element_of_avl_set_wf(AVL,X,WF) :-
4864 avl_approximate_size(AVL,10,ApproxSize),
4865 ? element_of_avl_set_wf(AVL,ApproxSize,X,WF).
4866
4867 :- use_module(clpfd_tables).
4868
4869 element_of_avl_set_wf(AVL,ApproxSize,X,WF) :-
4870 % first check if worthwhile to attempt table treatment
4871 % after fixing table/2 bug runtimes have slowed down and test 1753 became much slower
4872 % for test 1753 a threshold of < 63 would be ideal; but test 1716 requires size 91
4873 % TODO: re-evaluate when SICStus 4.8 available
4874 preferences:preference(use_clpfd_solver,true),
4875 preferences:preference(solver_strength,SS),
4876 ApproxSize < 100+SS,
4877 (var(X) -> true
4878 ; X = (X1,_X2) -> (ground_value(X1) -> ApproxSize < 10+SS ; true)
4879 ; X=rec(_) -> true
4880 %; X=int(_) -> true ; X=fd(_,_) -> true % for scalar values we already use in_fd_value_list_wf via avl_fd_value_check
4881 ),
4882 can_translate_avl_to_table(AVL,SkeletonType),
4883 !,
4884 ? check_element_of_avl_with_table(X,SkeletonType,AVL,WF).
4885 element_of_avl_set_wf(AVL,ApproxSize,X,WF) :-
4886 ? propagate_avl_element_information(X,AVL,ApproxSize,WF), %translate:translate_bvalue(avl_set(AVL),SS),
4887 get_bounded_wait_flag(ApproxSize,element_of_avl(X),WF,WF1),
4888 ? element_of_avl_set_wf3(X,AVL,ApproxSize,WF1,WF).
4889
4890
4891 % compute an approximate size (small sets are computed exactly)
4892 avl_approximate_size(AVL,Size) :- avl_approximate_size(AVL,10,Size).
4893
4894 avl_approximate_size(AVL,HeightBound,Size) :- var(AVL),!,
4895 add_internal_error('AVL Set is variable: ', avl_approximate_size(AVL,HeightBound,Size)),
4896 Size=1000000.
4897 avl_approximate_size(AVL,HeightBound,Size) :- % when the AVL gets too large; not so important that we have a precise estimation anyway
4898 % so: save some time and just compute height
4899 avl_height(AVL,Height),
4900 (Height>HeightBound
4901 -> Size is floor(2**Height-1)
4902 ; avl_size(AVL,Size)).
4903
4904 :- block element_of_avl_set_wf3(-,?,?,-,?).
4905 ?element_of_avl_set_wf3(X,AVL,_ApproxSize,_WF1,_WF) :- var(X), !, safe_avl_member(X,AVL).
4906 % TO DO: if randomise_enumeration_order is true then choose elements in random order
4907 :- if(environ(prob_data_validation_mode,xxxtrue)). % currently disabled due to bug related to 14082013/435_002.mch TO DO: investigate
4908 element_of_avl_set_wf3((X,Y),AVL,ApproxSize,WF1,WF) :- !,
4909 %% ((var(WF1), \+ ground(X)) -> print(avl_relation_check(X,Y)),nl, %%
4910 %% copy_term((X,Y),Copy), findall(Copy,safe_avl_member(Copy,AVL),Cs), print(Cs),nl, Cs \=[] %% check that at least one element exists
4911 %% ; true),
4912 couple_element_of_avl_set_wf(X,Y,AVL,ApproxSize,WF1,WF).
4913 :- else.
4914 element_of_avl_set_wf3((X,Y),AVL,ApproxSize,WF1,WF) :- !,
4915 ground_value_check(X,GrX),
4916 ? block_couple_element_of_avl_set_grX_wf1(X,Y,AVL,ApproxSize,GrX,WF1,WF).
4917 %when((nonvar(WF1) ; ground(X)), couple_element_of_avl_set(X,Y,AVL,GrX,WF1,WF)).
4918 :- endif.
4919 element_of_avl_set_wf3(X,AVL,_ApproxSize,WF1,_WF) :-
4920 ground_value_check(X,GrX),
4921 safe_avl_member_block(X,AVL,GrX,WF1).
4922
4923 :- block safe_avl_member_block(?,?,-,-).
4924 safe_avl_member_block(X,AVL,_,_) :-
4925 ? safe_avl_member(X,AVL).
4926
4927 :- if(environ(prob_data_validation_mode,true)).
4928 :- public couple_element_of_avl_set_wf/6. % used in conditional if above
4929 :- block couple_element_of_avl_set_wf(-,?,?,?,-,?).
4930 couple_element_of_avl_set_wf(X,Y,AVL,ApproxSize,WF1,WF) :-
4931 ground_value_check(X,GrX),
4932 ((nonvar(WF1);nonvar(GrX)) -> couple_element_of_avl_set(X,Y,AVL,GrX,WF1,WF)
4933 %; true -> when((nonvar(WF1) ; ground(X)), couple_element_of_avl_set(X,Y,AVL,WF1,WF))
4934 ; nonvar(X),X=(X1,X2),ground(X1) -> triple_element_of_avl_set(X1,X2,Y,AVL,WF)
4935 ; nonvar(X),X=(X1,X2) ->
4936 avl_member_blocking((X,Y),AVL),
4937 (ground(Y),ground(X1) -> safe_avl_member_pair_wf(X,Y,AVL,WF)
4938 ; when(ground(X1),(\+ ground(X2) -> triple_element_of_avl_set(X1,X2,Y,AVL,WF) ; true % avl_member_blocking will have done its work
4939 )),
4940 block_couple_element_of_avl_set(X,Y,AVL,WF1,WF)
4941 )
4942 ; %when((nonvar(WF1) ; ground(X)), couple_element_of_avl_set(X,Y,AVL,WF1,WF))
4943 block_couple_element_of_avl_set_grX_wf1(X,Y,AVL,ApproxSize,GrX,WF1,WF)
4944 /* ; (simple_avl_type(AVL)
4945 -> avl_member_blocking((X,Y),AVL) % TO DO: don't call couple_element_of_avl_set ! avoid double traversal !!
4946 ; true),
4947 block_couple_element_of_avl_set_grX_wf1(X,Y,AVL,GrX,WF1,WF) */
4948 ).
4949
4950 :- block block_couple_element_of_avl_set(?,?,?,-,?).
4951 block_couple_element_of_avl_set(X,Y,_AVL,_WF1,_WF) :- ground(X),ground(Y),!.
4952 block_couple_element_of_avl_set(X,Y,AVL,_WF1,WF) :- safe_avl_member_pair_wf(X,Y,AVL,WF).
4953
4954 triple_element_of_avl_set(X1,X2,Y,AVLRelation,WF) :- % X1 must be ground
4955 copy_term((X2,Y),(CX2,CY)),
4956 findall((CX2,CY),safe_avl_member_pair((X1,CX2),CY,AVLRelation),Images),
4957 % we pass no WF to safe_avl_member_pair; we need to fully evaluate all unifications due to findall
4958 Images \= [],
4959 construct_avl_from_lists_wf(Images,AVL,WF),
4960 element_of_custom_set_wf2(AVL,(X2,Y),WF). % will set up waitflag if necessary
4961 :- endif.
4962
4963 % ---------------------------------------------------
4964
4965 test_avl_set(node(((int(2),int(3)),int(6)),true,0,node(((int(1),int(2)),int(2)),true,0,empty,empty),node(((int(3),int(4)),int(12)),true,0,empty,empty))).
4966
4967 %simple_avl_type(node(K,_,_,_,_)) :- simple_value(K). % we can index directly on AVL, without having to normalise inner values
4968 % in particular, we can apply avl_member_blocking
4969
4970 :- assert_must_succeed(( custom_explicit_sets:test_avl_set(A), custom_explicit_sets:avl_member_blocking(((X,Y),Z),A), X=int(2), Y==int(3),Z==int(6) )).
4971 :- assert_must_succeed(( custom_explicit_sets:test_avl_set(A), custom_explicit_sets:avl_member_blocking(((X,Y),Z),A), X=int(3), Y==int(4),Z==int(12) )).
4972 :- assert_must_succeed(( custom_explicit_sets:test_avl_set(A), custom_explicit_sets:avl_member_blocking(((X,Y),Z),A), X=int(1), Y==int(2),Z==int(2) )).
4973 :- assert_must_fail(( custom_explicit_sets:test_avl_set(A), custom_explicit_sets:avl_member_blocking(((X,_Y),_Z),A), X=int(5) )).
4974 % a blocking version of avl_member; will not instantiate the element; just prune
4975
4976 avl_member_blocking(Key, AVL) :- AVL=node(K,_,_,L,R),
4977 %avl_height(AVL,Height),
4978 avl_member_blocking4(Key,K,L,R).
4979
4980 avl_member_blocking4(Key,Kavl,L,R) :- L=empty,R=empty,!,
4981 Key=Kavl. % we could do equal_object
4982 avl_member_blocking4(Key,Kavl,L,R) :-
4983 match_possible(Key,Kavl,MatchPossible), % check if in principle a match could occur
4984 (Kavl=(_,_) ->
4985 (avl_min(R,Knext) -> true ; dif(O,>), Knext=no_match,
4986 force_comp(MatchPossible,O,'<')),
4987 (avl_max(L,Kprev) -> true ; dif(O,<), Kprev=no_match,
4988 force_comp(MatchPossible,O,'>'))
4989 ; Knext = no_match, Kprev = no_match
4990 ),
4991 (nonvar(O) -> true
4992 /* ; (MatchPossible==pred_false, avl_height(L,Height), Height < 8,
4993 copy_term(Key,CKey), \+ safe_avl_member(CKey,L), \+ safe_avl_member(CKey,R))
4994 -> print(cannot_match(Key)),nl,fail */
4995 ; compare_blocking(O, Key, Kavl, Kprev,Knext)),
4996 avl_member_blocking_aux(O, Key, Kavl, L, R).
4997
4998 %force_comp(V,_,_) :- var(V),!.
4999 :- block force_comp(-,?,?).
5000 force_comp(pred_true,_,_).
5001 force_comp(pred_false,R,R).
5002
5003 :- block avl_member_blocking_aux(-,?,?,?,?).
5004 avl_member_blocking_aux(<, Key, _K, AVL, _) :- avl_member_blocking(Key, AVL).
5005 avl_member_blocking_aux(=, Key, Key, _L, _R). % we could use equal_object
5006 avl_member_blocking_aux(>, Key, _K, _, AVL) :- avl_member_blocking(Key, AVL).
5007
5008 % a blocking version of compare
5009 compare_blocking(Res,A,Kavl, Kprev, Knext) :- block_compare(A,Kavl,Res, Kprev, Knext).
5010
5011 :- block block_compare(-,?,?,?,?), block_compare(?,-,?,?,?).
5012 block_compare((A,B),Kavl,Res, Kprev, Knext) :- !,
5013 (Kavl=(RA,RB) ->
5014 match_key(Kprev,RA,PA,PB),
5015 match_key(Knext,RA,NA,NB),
5016 block_compare(A,RA,ACRes,PA,NA),
5017 block_compare_aux(ACRes,B,RB,Res,PB,NB)
5018 ; add_internal_error('Illegal type: ',block_compare((A,B),Kavl,Res, Kprev, Knext)),fail).
5019 % TO DO: same for records; but currently not used anyway
5020 block_compare(int(A),int(B),Res,_,_) :- !, block_compare_atomic(A,B,Res).
5021 block_compare(pred_false,B,Res,_,_) :- !, block_compare_atomic(pred_false,B,Res).
5022 block_compare(pred_true,B,Res,_,_) :- !, block_compare_atomic(pred_true,B,Res).
5023 block_compare(string(A),string(B),Res,_,_) :- !, block_compare_atomic(A,B,Res).
5024 block_compare(fd(A,T),fd(B,T),Res,_,_) :- !, block_compare_atomic(A,B,Res).
5025 block_compare(avl_set(A),Kavl,Res,_,_) :- !,
5026 convert_to_avl_inside_set(avl_set(A),ConvertedA),compare(Res,ConvertedA,Kavl).
5027 block_compare([],[],Res,_,_) :- !, Res = '='.
5028 block_compare([],_,Res,_,_) :- !, Res = '<'.
5029 block_compare(A,Kavl,Res,_,_) :-
5030 % does deal with various representations of sets !! closure/global_set/...
5031 when(ground(A),
5032 (convert_to_avl_inside_set(A,ConvertedA),compare(Res,ConvertedA,Kavl))).
5033
5034 match_key((KeyA,KeyB),Key,ResA,ResB) :- !, ResA=KeyA,
5035 (Key==KeyA -> ResB=KeyB ; ResB = no_match).
5036 match_key(_,_,no_match,no_match).
5037
5038 :- block block_compare_atomic(-,?,?), block_compare_atomic(?,-,?).
5039 block_compare_atomic(A,B,Res) :- compare(Res,A,B).
5040
5041 :- block block_compare_aux(-,?,?,?, ?,?).
5042 block_compare_aux(ACRes,B,D,Res, Kprev,Knext) :-
5043 (ACRes='<' -> Res = '<'
5044 ; ACRes = '>' -> Res = '>'
5045 ; Kprev=no_match, Knext=no_match ->
5046 Res = '=' % we cannot match neither previous nor next key: force match
5047 ; block_compare(B,D,Res,Kprev,Knext)). % TO DO: check with prev & next value: if no match possible force Res='='
5048
5049 % check if a match is possible between two terms
5050 :- block match_possible(-,?,?), match_possible(?,-,?).
5051 match_possible([],[],Possible) :- !, Possible=pred_true.
5052 match_possible([],avl_set(_),Possible) :- !, Possible=pred_false.
5053 match_possible(avl_set(_),[],Possible) :- !, Possible=pred_false.
5054 match_possible(int(A),int(B),Possible) :- !, match_possible_atomic(A,B,Possible).
5055 match_possible(fd(A,T),fd(B,T),Possible) :- !, match_possible_atomic(A,B,Possible).
5056 match_possible(string(A),string(B),Possible) :- !, match_possible_atomic(A,B,Possible).
5057 match_possible((A1,A2),(B1,B2),Possible) :- !, match_possible(A1,B1,P1),
5058 match_possible(A2,B2,P2), kernel_equality:conjoin_test(P1,P2,Possible,_WF). %% WF <--- TO DO
5059 match_possible(_,_,pred_true).
5060
5061 :- block match_possible_atomic(-,?,?), match_possible_atomic(?,-,?).
5062 match_possible_atomic(A,B,Res) :- (A==B -> Res=pred_true ; Res=pred_false).
5063
5064 % --------------------------------------------
5065
5066 :- block block_couple_element_of_avl_set_grX_wf1(?, - ,?,?,-,-,?).
5067 block_couple_element_of_avl_set_grX_wf1(X,Y,AVL,ApproxSize,GrX,WF1,WF) :-
5068 var(GrX), var(WF1),
5069 !,
5070 % we know the result Y but not yet fully the input value X
5071 (ApproxSize < 129 % TO DO: improve this; unify with inverse_apply_ok(Y,X,AVL,ApproxSize) ?
5072 -> ground_value_check(Y,GrY) % wait until Y is fully known
5073 ; (preference(solver_strength,SS), ApproxSize < 129+SS)
5074 -> ground_value_check(Y,GrY)
5075 % TO DO: we could look at avl_min and avl_max and estimate spread of range keys
5076 ; cond_perfmessage([data_validation_mode/false],no_inverse_avl_lookup(ApproxSize,Y)) % do not bind GrY; we wait until GrX or WF1 is bound
5077 ),
5078 block_couple_element_of_avl_set_grX_grY_wf1(X,Y,AVL,ApproxSize,GrX,GrY,WF1,WF).
5079 block_couple_element_of_avl_set_grX_wf1(X,Y,AVL,_ApproxSize,GrX,WF1,WF) :-
5080 ? couple_element_of_avl_set(X,Y,AVL,GrX,WF1,WF).
5081
5082 :- block block_couple_element_of_avl_set_grX_grY_wf1(?,?,?,?, -,-,-,?).
5083 block_couple_element_of_avl_set_grX_grY_wf1(X,Y,AVL,_ApproxSize, GrX,_GrY,WF1,WF) :-
5084 var(GrX), var(WF1), % i.e., Y is known
5085 % we know the result Y but not yet fully the input value X
5086 %inverse_apply_ok(Y,X,AVL,ApproxSize),
5087 !,
5088 inverse_get_possible_values(X,Y,AVL,Res),
5089 Res = avl_set(InvAVL),
5090 element_of_avl_set_wf(InvAVL,X,WF).
5091 %couple_element_of_avl_set(X,Y,AVL,GrX,1,WF).
5092 block_couple_element_of_avl_set_grX_grY_wf1(X,Y,AVL,_ApproxSize,GrX,_GrY,WF1,WF) :-
5093 ? couple_element_of_avl_set(X,Y,AVL,GrX,WF1,WF).
5094
5095
5096 % special treatment for relations: if the first component is known: then we can check how many images there are
5097 couple_element_of_avl_set(X,Y,AVL,GrX,WF1,WF) :-
5098 nonvar(WF1), var(GrX), %\+ground(X),
5099 !,
5100 ? safe_avl_member_default_wf((X,Y),AVL,WF).
5101 couple_element_of_avl_set(X,Y,AVLRelation,_GrX,_,WF) :- % X must be ground
5102 get_template(Y,TY,_ToUnifyAfter), % was copy_term(Y,CY) but could cause issues with closures with variables
5103 copy_term(TY,CY), % avoid that we instantiate Y and trigger co-routines
5104 findall(CY,avl_member_pair_arg1_ground(X,CY,AVLRelation),Images), % should we use Y instead of CY
5105 Images \= [],
5106 construct_avl_from_lists_wf(Images,AVL,WF),
5107 ? element_of_custom_set_wf2(AVL,Y,WF). % will set up waitflag if necessary
5108
5109
5110 % set Res -> pred_true or pred_false if membership can be decided early
5111 % interval closures already dealt with by closure_membership
5112 % maximal sets are also already dealt with by membership_custom_set
5113 reify_avl_membership(AVL,Element,Res,FullReification) :-
5114 is_avl_simple_set(AVL,Type),
5115 preferences:preference(use_clpfd_solver,true), % to do: require maybe only for integer type !?
5116 \+ ground_value(Element),
5117 !,
5118 reify_avl_mem2(Type,Element,AVL,Res,FullReification).
5119 reify_avl_membership(_,_,_,false).
5120
5121
5122 is_avl_simple_set(node(El,_True,_,_,_),Type) :- simple_type(El,Type).
5123 simple_type(int(_),integer).
5124 simple_type(fd(_,GS),global(GS)).
5125
5126
5127 reify_avl_mem2(integer,int(El),AVL,Res,FullReification) :-
5128 avl_min(AVL,int(Min)), avl_max(AVL,int(Max)),
5129 (reify_integer_avl_mem(AVL,Min,Max) % reify if AVL small enough
5130 -> avl_domain(AVL,R),project_avl_domain_on_fd(R,FDList),
5131 clpfd_reify_inlist(El,FDList,FDRes,Posted),
5132 propagate_fd_membership(FDRes,Res,inlist(El,FDList)),
5133 FullReification=Posted
5134 ; clpfd_interface:try_post_constraint((El in Min..Max) #<=> FDRes),
5135 propagate_not_membership(FDRes,Res,int(El,Min,Max)),
5136 FullReification=false
5137 ).
5138 % this could also be enabled with CLPFD = FALSE ?? no overflows are possible
5139 reify_avl_mem2(global(GS),fd(El,GS),AVL,Res,FullReification) :-
5140 avl_domain(AVL,R),project_avl_domain_on_fd(R,FDList),
5141 b_global_sets:b_get_fd_type_bounds(GS,Low,Up),
5142 (is_full_fdlist(FDList,Low,Up)
5143 -> Res=pred_true, % all the values are in the list; it must be a member
5144 % normally this should also be detected by clpfd_reify_inlist, unless no constraint was set up for El
5145 % it seems to have an effect for test 426: probcli examples/EventBPrologPackages/SSF/Bepi_Soton/M1_mch.eventb -cbc all -strict -p CLPFD TRUE -p SMT TRUE -strict -p STRICT_RAISE_WARNINGS TRUE
5146 FullReification=true
5147 ; clpfd_reify_inlist(El,FDList,FDRes,Posted),
5148 propagate_fd_membership(FDRes,Res,inlist(El,FDList)),
5149 FullReification=Posted
5150 ).
5151 %reify_avl_mem2(global(GS),fd(El,GS),AVL,Res) :-
5152 % avl_min(AVL,fd(Min,GS)), avl_max(AVL,fd(Max,GS)),
5153 % clpfd_interface:try_post_constraint((El in Min..Max) #<=> FDRes),
5154 % propagate_not_membership(FDRes,Res,fd(El,GS,Min,Max)).
5155
5156 % assumes list is sorted
5157 is_full_fdlist(List,Low,Up) :- integer(Up), is_full_fdlist2(List,Low,Up).
5158 is_full_fdlist2([],Low,Up) :- Low>Up.
5159 is_full_fdlist2([Low|T],Low,Up) :- L1 is Low+1, is_full_fdlist2(T,L1,Up).
5160
5161 % check if avl small enough to call clpfd_reify_inlist
5162 reify_integer_avl_mem(_AVL,Min,Max) :- MaxSizeM1 is Max-Min, MaxSizeM1 =< 20,!.
5163 reify_integer_avl_mem(AVL,_Min,_Max) :- avl_height_less_than_with_solver_strength(AVL,5).
5164
5165
5166
5167 project_avl_domain_on_fd([],[]).
5168 project_avl_domain_on_fd([H|T],[PH|PT]) :- project_avl_domain(H,PH), project_avl_domain_on_fd(T,PT).
5169 project_avl_domain(int(X),X).
5170 project_avl_domain(fd(X,_),X).
5171
5172
5173 :- block propagate_fd_membership(-,-,?).
5174 % if we make it propagate_fd_membership(-,-?) Bosch examples becomes much slower ?
5175 % Indeed: membership_custom_set will already force membership or non-membership !
5176 %propagate_fd_membership(X,M,Info) :- var(X),!, print(propagate_fd(X,M,Info)),nl, (M=pred_true ->X=1 ; X=0).
5177 propagate_fd_membership(1,pred_true,_Info).
5178 propagate_fd_membership(0,pred_false,_Info).
5179
5180 :- block propagate_not_membership(-,?,?).
5181 propagate_not_membership(1,_,_). % there could be elements in the interval which are not in the set
5182 propagate_not_membership(0,Res,_Info) :-
5183 Res=pred_false.
5184
5185 % -----------------
5186
5187 % fails if not possible to quickly compute approximate size
5188 quick_custom_explicit_set_approximate_size(V,_) :- var(V),!,fail.
5189 quick_custom_explicit_set_approximate_size(avl_set(AVL),Size) :- !,
5190 quick_avl_approximate_size(AVL,Size).
5191 quick_custom_explicit_set_approximate_size(CS,Size) :-
5192 card_for_specific_custom_set(CS,Size,Code),
5193 on_enumeration_warning(call(Code),fail),
5194 atomic(Size). % inf or number; sometimes card_for_specific_custom_set can return a variable
5195
5196 :- use_module(clpfd_lists,[try_get_fd_value_list/4, get_fd_value/3, in_fd_value_list_wf/4]).
5197 % a membership propagation, but only done if it can be done quickly
5198
5199
5200 % quick_propagation_element_information(Set, Element, WF, PossiblyCompiledSet)
5201 % use last element for next iteration if you call quick_propagation_element_information in a loop
5202 :- block quick_propagation_element_information(-,?,?,?).
5203 quick_propagation_element_information(Set,_El,_,R) :-
5204 preferences:preference(use_clpfd_solver,false),
5205 !, R=Set.
5206 quick_propagation_element_information(avl_set(AVL),Element,WF,NewSet) :- !,
5207 quick_avl_approximate_size(AVL,Size),
5208 NewSet=avl_set_with_size(AVL,Size),
5209 propagate_avl_element_information_direct(Element,AVL,Size,WF).
5210 quick_propagation_element_information(avl_set_with_size(AVL,Size),Element,WF,NewSet) :- !,
5211 NewSet = avl_set_with_size(AVL,Size),
5212 propagate_avl_element_information_direct(Element,AVL,Size,WF).
5213 quick_propagation_element_information(closure(P,T,B),Element,WF,NewSet) :- !,
5214 NewSet = closure(P,T,B),
5215 ? element_of_closure(Element,P,T,B,WF).
5216 quick_propagation_element_information(fd_value_list(FDList,GroundList,Type),El,WF,NewSet) :- !,
5217 NewSet = fd_value_list(FDList,GroundList,Type),
5218 get_fd_value(Type,El,ElFD),
5219 in_fd_value_list_wf(GroundList,ElFD,FDList,WF).
5220 quick_propagation_element_information(Set,El,WF,NewSet) :-
5221 ? try_get_fd_value_list(Set,Type,FDList,GroundList),!,
5222 FDList \= [], % if list is empty membership fails
5223 NewSet = fd_value_list(FDList,GroundList,Type),
5224 % clpfd_inlist requires list of integers as second argument
5225 ? get_fd_value(Type,El,ElFD),
5226 % We could apply filter_non_matching_elements here
5227 in_fd_value_list_wf(GroundList,ElFD,FDList,WF).
5228 quick_propagation_element_information(Set,_,_,Set).
5229
5230 % -----------------
5231
5232 % infer information about an element of an AVL set
5233 propagate_avl_element_information(Element,AVL,Size,WF) :-
5234 (preferences:preference(use_clpfd_solver,true)
5235 ? -> propagate_avl_element_information_direct(Element,AVL,Size,WF)
5236 ; true).
5237
5238 propagate_avl_element_information_direct(Element,AVL,Size,WF) :-
5239 (Size<100 -> %30 which magic constant to use here; use larger value in SMT mode ?
5240 ? propagate_avl_element_information_small(Element,AVL,WF)
5241 ; is_avl_fd_index_set(AVL,Type) ->
5242 propagate_avl_element_information_large(Type,Element,AVL),
5243 (Size < 4000, nonvar(Element), Element = (_,_) % another magic constant
5244 -> Prio is Size // 60,
5245 get_wait_flag(Prio,propagate_avl_element_information(Element),WF,LWF),
5246 propagate_avl_el_large_block(Element,AVL,WF,LWF) % will do precise propagation
5247 ; true)
5248 ; true).
5249 % TO DO: we could call in_nat_range_wf; this way it would also work in non-CLPFD mode
5250
5251 :- block propagate_avl_el_large_block(?,?,?,-).
5252 propagate_avl_el_large_block((A,B),_,_,_) :-
5253 (ground(A); ground_value(B)), % in first: case we will apply AVL set ; in second case probably no benefit as propagate_avl_element_information_large already propagated first element
5254 !.
5255 propagate_avl_el_large_block(Element,AVL,WF,_LWF) :-
5256 % TO DO: maybe look if we should not use clpfd_list, but only upper & lower bound
5257 ? propagate_avl_element_information_small(Element,AVL,WF). % will do precise propagation.
5258
5259 :- use_module(clpfd_lists,[avl_fd_value_check/4]).
5260 :- use_module(clpfd_interface,[catch_and_ignore_clpfd_overflow/2]).
5261 propagate_avl_element_information_small(Element,AVL,WF) :-
5262 ? catch_and_ignore_clpfd_overflow(propagate_avl_element_information_small, % relevant test e.g. 1708 (with used_ids_defined_by_equality)
5263 avl_fd_value_check(AVL,Element,WF,_FullyChecked)).
5264
5265 propagate_avl_element_information_large(Type,El,AVL) :-
5266 avl_min(AVL,Min), avl_max(AVL,Max),
5267 % if Size small enough and smaller than Max-Min we call clpfd_inlist on domain
5268 % Note: overflows should be caught below; we could check that Min/Max are within CLPFD range
5269 couple_prj1_in_range(Type,El,Min,Max).
5270
5271 couple_prj1_in_range(integer,int(El),int(Min),int(Max)) :- clpfd_interface:clpfd_inrange(El,Min,Max).
5272 couple_prj1_in_range(global(GS),fd(El,GS),fd(Min,GS),fd(Max,GS)) :- clpfd_interface:clpfd_inrange(El,Min,Max).
5273 couple_prj1_in_range(couple_prj1(T),(El,_),(Min,_),(Max,_)) :- couple_prj1_in_range(T,El,Min,Max).
5274 couple_prj1_in_range(rec_first_field(Name,T),rec([field(Name,El)|TF]),
5275 rec([field(Name,Min)|TMin]),rec([field(Name,Max)|_])) :-
5276 (var(TF)
5277 -> copy_field_names(TMin,TF) % if Fields not yet instantiated: copy over all fields
5278 ; true),
5279 couple_prj1_in_range(T,El,Min,Max).
5280
5281 copy_field_names([],[]).
5282 copy_field_names([field(N,_)|T],[field(N,_)|CT]) :- copy_field_names(T,CT).
5283
5284 % check if the first component of the AVL elements of a type such that we can propagate FD information
5285 is_avl_fd_index_set(node(El,_True,_,_,_),Type) :-
5286 simple_index_type(El,Type).
5287 simple_index_type((El,_),couple_prj1(T)) :- simple_index_type(El,T).
5288 simple_index_type(int(_),integer).
5289 simple_index_type(fd(_,GS),global(GS)).
5290 simple_index_type(rec(Fields),rec_first_field(Name,T)) :- nonvar(Fields),
5291 Fields = [field(Name,El)|_],
5292 simple_index_type(El,T).
5293 %simple_index_type((int(_),_),couple_integer).
5294 %simple_index_type(((int(_),_),_),couple_couple_integer).
5295 %simple_index_type((fd(_,GS),_),couple_global(GS)).
5296
5297
5298 /* avoid instantiating non-normalised with normalised values leading to failure */
5299 :- assert_must_succeed((X=(fd(1,'Name'),fd(2,'Name')), A=node(X,true,0,empty,empty),
5300 custom_explicit_sets:safe_avl_member(X,A) )).
5301
5302 ?safe_avl_member(X,AVL) :- var(X), !, my_avl_member(X,AVL).
5303 %safe_avl_member((X,Y),AVL) :- !, safe_avl_member_pair(X,Y,AVL).
5304 safe_avl_member(Value,AVL) :- decompose_index(Value,Key,RestVal), !,
5305 ? avl_fetch_indexed(Value,Key,RestVal,AVL).
5306 safe_avl_member(X,AVL) :- ground_value(X), convert_to_avl_inside_set(X,AX), !, avl_fetch(AX,AVL).
5307 ?safe_avl_member(X,AVL) :- safe_avl_member_default_wf(X,AVL,no_wf_available).
5308
5309
5310 % this is a generalisation of safe_avl_member_pair
5311 % check if a value can be decomposed into an index and the rest of a value and the key is ground
5312 % it also works for records indexing on first field
5313 avl_fetch_indexed(Value,Key,RestVal,AVL) :-
5314 ground_value_or_field(Key),
5315 convert_value_or_field(Key,NormKey),
5316 !,
5317 (ground_value_or_field(RestVal),
5318 convert_to_avl_inside_set(Value,NormValue)
5319 -> avl_fetch(NormValue,AVL)
5320 ? ; avl_fetch_with_index(NormKey,AVL,RestValLookup),
5321 ? kernel_objects:equal_object(RestValLookup,RestVal,avl_fetch_indexed)
5322 ).
5323 avl_fetch_indexed(Value,_,_,AVL) :-
5324 ? safe_avl_member_default_wf(Value,AVL,no_wf_available).
5325
5326 convert_value_or_field(field(Name,Val),field(Name,NVal)) :- !,
5327 convert_to_avl_inside_set(Val,NVal).
5328 convert_value_or_field(Key,NormKey) :-
5329 convert_to_avl_inside_set(Key,NormKey).
5330
5331 % a version of safe_avl_member where the first argument is guaranteed to be ground
5332 % somehow using this seems to slow-down evaluation for vesg_Dec12; Caching ??
5333 %safe_avl_member_ground(X,AVL) :-
5334 % convert_to_avl_inside_set(X,AX), !, avl_fetch(AX,AVL).
5335 %safe_avl_member_ground((X,Y),AVL) :- !, avl_member_pair_arg1_ground(X,Y,AVL).
5336 %safe_avl_member_ground(X,AVL) :- safe_avl_member_default_wf(X,AVL,no_wf_available).
5337
5338
5339 safe_avl_member_pair(X,Y,AVL) :- safe_avl_member_pair_wf(X,Y,AVL,no_wf_available).
5340
5341 safe_avl_member_pair_wf(X,Y,AVL,_WF) :- ground_value(X),!,
5342 ( ground_value(Y),
5343 convert_to_avl_inside_set((X,Y),AX)
5344 -> avl_fetch(AX,AVL)
5345 ; avl_member_pair_arg1_ground(X,Y,AVL)). % TODO: pass WF
5346 safe_avl_member_pair_wf(X,Y,AVL,WF) :- safe_avl_member_default_wf((X,Y),AVL,WF).
5347
5348 % can be used to try and lookup a function value without creating WD errors, ...
5349 % used in b_compiler to compile function applications
5350 try_apply_to_avl_set(X,Y,AVL) :- ground_value(X),
5351 ? avl_member_pair_arg1_ground(X,Y,AVL).
5352
5353 %safe_avl_member_pair_ground(X,Y,AVL) :- convert_to_avl_inside_set((X,Y),AX),!, avl_fetch(AX,AVL).
5354 %safe_avl_member_pair_ground(X,Y,AVL) :- avl_member_pair_arg1_ground(X,Y,AVL).
5355
5356 avl_member_pair_arg1_ground(X,Y,AVL) :- convert_to_avl_inside_set(X,AX), !,
5357 get_template(Y,RY,ToUnifyAfter),
5358 ? avl_fetch_pair(AX,AVL,RY),
5359 unify_after_wf(ToUnifyAfter,no_wf_available). %kernel_objects:equal_object(RY,Y).
5360 avl_member_pair_arg1_ground(X,Y,AVL) :-
5361 safe_avl_member_default((X,Y),AVL).
5362
5363 ?safe_avl_member_default(X,AVL) :- safe_avl_member_default_wf(X,AVL,no_wf_available).
5364 %safe_avl_member_default(PP,X,AVL) :-
5365 % debug:timer_call(safe_avl_member_default(PP),custom_explicit_sets:safe_avl_member_default1(X,AVL)).
5366 safe_avl_member_default_wf(X,AVL,WF) :- %statistics(runtime,_),
5367 get_template(X,Template,ToUnifyAfter),
5368 ? my_avl_member(Template,AVL),
5369 % statistics(runtime,[_,T2]), print(avl_member(Template,T2)),nl,
5370 ? unify_after_wf(ToUnifyAfter,WF). % kernel_objects:equal_object(Template,X)).
5371
5372 unify_after_wf([],_).
5373 ?unify_after_wf([A/B|T],WF) :- kernel_objects:equal_object_wf(A,B,unify_after,WF),
5374 ? unify_after_wf(T,WF).
5375
5376
5377
5378 get_template(A,R,ToUnifyAfter) :-
5379 (var(A) -> ToUnifyAfter=[A/R]
5380 ; get_template2(A,R,ToUnifyAfter) -> true
5381 ; add_internal_error('Could_not_get_template: ',get_template(A,R,_))).
5382
5383 get_template2((A,B),(TA,TB),ToUnifyAfter) :- get_template(A,TA,ToUnifyAfter1), get_template(B,TB,ToUnifyAfter2),
5384 append(ToUnifyAfter1,ToUnifyAfter2,ToUnifyAfter). % TO DO: use DifferenceLists / DCG
5385 get_template2(int(X),int(X),[]).
5386 get_template2(fd(A,B),fd(A,B),[]).
5387 get_template2([],[],[]).
5388 get_template2(pred_false /* bool_false */,pred_false /* bool_false */,[]).
5389 get_template2(pred_true /* bool_true */,pred_true /* bool_true */,[]).
5390 get_template2([H|T],R,ToUnifyAfter) :-
5391 (ground_value(H),ground_value(T)
5392 -> convert_to_avl_inside_set([H|T],R),ToUnifyAfter=[]
5393 ; ToUnifyAfter=[[H|T]/R]).
5394 % ; R=avl_set(A), ToUnifyAfter=[[H|T]/avl_set(A)]).
5395 get_template2(closure(P,T,B),R,[]) :- ground_value(closure(P,T,B)),
5396 expand_closure_to_avl_wf(P,T,B,R,no_wf_available),!.
5397 get_template2(closure(P,T,B),AVL_OR_EMPTY_OR_GS,[closure(P,T,B)/AVL_OR_EMPTY_OR_GS]). % closure could be empty or an infinite global set ?
5398 %get_template2(closure_x(_,_,_),_AVL_OR_EMPTY).
5399 get_template2(avl_set(A),avl_set(NA),[]) :- convert_to_avl_inside_set(avl_set(A),avl_set(NA)). % do we need to normalise here ??
5400 get_template2(string(X),string(X),[]).
5401 get_template2(term(X),term(X),[]).
5402 get_template2(freetype(X),R,[]) :- convert_to_avl_inside_set(freetype(X),R).
5403 get_template2(rec(Fields),rec(TFields),ToUnifyAfter) :- get_fields_template(Fields,TFields,ToUnifyAfter).
5404 get_template2(freeval(ID,Case,Value),freeval(ID,Case,TValue),ToUnifyAfter) :- get_template(Value,TValue,ToUnifyAfter).
5405 get_template2(global_set(GS),R,[]) :- convert_to_avl_inside_set(global_set(GS),R).
5406
5407
5408 get_fields_template(A,R,[rec(A)/rec(R)]) :- var(A),!.
5409 get_fields_template([],[],ToUnifyAfter) :- !, ToUnifyAfter=[].
5410 get_fields_template([field(Name,Val)|T],[field(Name,TVal)|TT],ToUnifyAfter) :- nonvar(Name),!,
5411 get_template(Val,TVal,ToUnifyAfter1),
5412 get_fields_template(T,TT,ToUnifyAfter2), append(ToUnifyAfter1,ToUnifyAfter2,ToUnifyAfter).
5413 get_fields_template(A,R,[rec(A)/rec(R)]).
5414
5415
5416 % succeed if we can decide membership of an avl_set on the spot
5417 quick_test_avl_membership(AVL,X,Res) :-
5418 element_can_be_added_or_removed_to_avl(X),
5419 convert_to_avl_inside_set(X,AX),
5420 (avl_fetch(AX,AVL) -> Res=pred_true ; Res=pred_false).
5421
5422 % ---------------------
5423
5424 % a dispatch predicate
5425 my_avl_member(Key,AVL) :-
5426 (preferences:preference(randomise_enumeration_order,true)
5427 ? -> random_avl_member(Key,AVL) ; avl_member_opt(Key,AVL)).
5428 :- use_module(library(random),[random/3]).
5429 ?random_avl_member(Key,AVL) :- avl_height(AVL,Height), H1 is Height+1, random_avl_member(Key,H1,AVL).
5430 % TO DO: make more intelligent; this is not really a very uniform way of randomly enumerating an AVL set (e.g., Key never occurs between L and R)
5431 random_avl_member(Key, H, node(K,_,_,L,R)) :-
5432 random(1,H,1), !, H1 is H-1,
5433 ? (Key=K ; random_avl_member(Key,H1,L) ; random_avl_member(Key,H1,R)).
5434 random_avl_member(Key, H, node(K,_,_,L,R)) :- random(1,3,1), !, H1 is H-1,
5435 ? (random_avl_member(Key,H1,L) ; random_avl_member(Key,H1,R) ; Key=K).
5436 random_avl_member(Key, H, node(K,_,_,L,R)) :- H1 is H-1,
5437 ? (random_avl_member(Key,H1,R) ; random_avl_member(Key,H1,L) ; Key=K).
5438
5439 % a variation of avl_member from library(avl) which tries to avoid leaving choice points behind
5440 avl_member_opt(Key, node(K,_,_,L,R)) :-
5441 ? ( avl_member_opt(Key, L)
5442 ; R=empty -> Key = K % avoid trailing choice_point
5443 ? ; (Key=K ; avl_member_opt(Key, R))
5444 ).
5445
5446 % ---------------------
5447
5448 :- use_module(kernel_objects,[check_element_of_wf/3,not_element_of_wf/3]).
5449 :- use_module(memoization,[element_of_memoization_closure/6]).
5450 element_of_special_closure(interval(LOW,UP),X,WF,_,_,_) :- !,
5451 %hit_profiler:add_profile_hit(in_nat_range(X,LOW,UP,CondClosure)),
5452 kernel_objects:in_nat_range_wf(X,int(LOW),int(UP),WF).
5453 element_of_special_closure(member_closure(_ID,_Type,VAL),X,WF,_,_,_) :-
5454 (VAL=value(_) ; VAL = cartesian_product(b(value(A),_,_),b(value(B),_,_))),!,
5455 %hit_profiler:add_profile_hit(in_member_closure(X,Par,Typ,Body)),
5456 (VAL=value(Set) -> check_element_of_wf(X,Set,WF)
5457 ; X=(XA,XB),
5458 ? kernel_objects:check_element_of_wf(XA,A,WF),
5459 kernel_objects:check_element_of_wf(XB,B,WF)).
5460 element_of_special_closure(not_member_closure(_ID,_Type,value(Set)),X,WF,_,_,_) :- !,
5461 %hit_profiler:add_profile_hit(in_not_member_closure(X,Par,Typ,Set)),
5462 not_element_of_wf(X,Set,WF).
5463 % we used to have to add enumerator, as not_element_of does not instantiate; e.g. relevant when doing X :: GS - {y}
5464 % This is no longer required
5465 % see test 6 (../prob_examples/public_examples/B/FeatureChecks/NotMemberCheck.mch)
5466 element_of_special_closure(recursive_special_closure(RId),X,WF,Parameters,PT,CondClosure) :- !,
5467 add_recursive_parameter(Parameters,PT,X,RId,CondClosure,NewParameters,NewPT,Value,WF),
5468 ? element_of_normal_closure(Value,NewParameters,NewPT,CondClosure,WF).
5469 element_of_special_closure(memoization_closure(MemoID),X,WF,P,T,B) :- !,
5470 element_of_memoization_closure(MemoID,X,WF,P,T,B).
5471 element_of_special_closure(_,X,WF,Parameters,PT,CondClosure) :-
5472 % none of the special cases above apply after all
5473 ? element_of_normal_closure(X,Parameters,PT,CondClosure,WF).
5474
5475 :- block element_of_closure(?,-,?,?,?), element_of_closure(?,?,?,-,?).
5476 % element_of_closure(X,Para,T,Body,_WF): check if X is a member of closure(Para,T,Body)
5477 element_of_closure(X,Parameters,PT,CondClosure,WF) :-
5478 is_special_closure(Parameters,PT,CondClosure, SpecialClosure),!,
5479 %print_term_summary(element_of_special_closure(SpecialClosure,X,WF,Parameters,PT,CondClosure)), trace_in_debug_mode,
5480 ? element_of_special_closure(SpecialClosure,X,WF,Parameters,PT,CondClosure).
5481 element_of_closure(X,Parameters,PT,CondClosure,WF) :-
5482 %print_term_summary(element_of_normal_closure(X,Parameters,PT,CondClosure,WF)), trace_in_debug_mode,
5483 ? element_of_normal_closure(X,Parameters,PT,CondClosure,WF).
5484 element_of_normal_closure(X,Parameters,PT,CondClosure,WF) :-
5485 %hit_profiler:add_profile_hit(element_of_closure(X,Parameters,PT,CondClosure)),
5486 same_length(Parameters,ParValues),
5487 convert_list_into_pairs(ParValues,X),
5488 ? b_test_closure_wo_enum(Parameters,PT,CondClosure,ParValues,WF).
5489
5490 :- use_module(store,[set_up_localstate/4]).
5491 :- block b_test_closure_wo_enum(?,?,-,?,?).
5492 b_test_closure_wo_enum(Parameters,ParameterTypes,ClosurePred,ParValues,WF) :-
5493 % same_length(Parameters,ParValues), % not necessary
5494 set_up_localstate(Parameters,ParValues,[],LocalState),
5495 ? b_enumerate:b_type_values_in_store(Parameters,ParameterTypes,LocalState),
5496 copy_wf_start(WF,b_test_closure_wo_enum(Parameters),InnerWF),
5497 % avoid that WF0 actions triggered before we have had a chance to traverse the expression
5498 ? b_test_boolean_expression(ClosurePred,LocalState,[],InnerWF),
5499 ? copy_wf_finish(WF,InnerWF).
5500
5501 % recursive identifier to list of parameters with body as value
5502 % NewValue is the Value that should be checked for membership in the adapted closure; it has one argument more
5503 add_recursive_parameter(Parameters,Types,Value,TId,CondClosure,NewParameters,NewTypes,NewValue,WF) :-
5504 TId = b(identifier(RId),SetType,_), % unification replaces: get_texpr_id(TId,RId), get_texpr_type(TId,SetType),
5505 append(Parameters,[RId],NewParameters),
5506 append(Types,[SetType],NewTypes),
5507 %tools_printing:print_term_summary(recursion(Value)),nl,
5508 % TO DO check some variant decreases
5509 (kernel_waitflags:pending_abort_error(WF)
5510 -> NewValue = (_,_) % prevent further expansion of recursion, in case WD error in recursive function
5511 % TO DO: detect whether WD error occurs within recursive function,
5512 % indeed, the expansion of the recursive function could be unrelated to WD error and be important to detect inconsistency which prevents WD error: e.g., 1/x=res & recfun(x) \= 0
5513 ,debug_println(19,stopping_recursion_due_to_wd_error)
5514 ; NewValue = (Value,closure(Parameters,Types,CondClosure))
5515 ).
5516
5517
5518 % same as above, but without a waitflag
5519 ?element_of_custom_set(X,CS) :- element_of_custom_set2(CS,X).
5520
5521 element_of_custom_set2(global_set(GS),X) :- !,element_of_global_set(X,GS).
5522 element_of_custom_set2(freetype(ID),_) :- is_maximal_freetype(ID),!. % freetypes are always maximal at the moment
5523 element_of_custom_set2(avl_set(AVL),X) :- !,
5524 safe_avl_member(X,AVL).
5525 element_of_custom_set2(CS,X) :- init_wait_flags(WF,[element_of_custom_set2]),
5526 element_of_custom_set_wf2(CS,X,WF),
5527 ? ground_wait_flags(WF).
5528
5529 % ---------------
5530
5531 % function application for closure
5532
5533 % same as check_element_of_wf but does not wait on Y:
5534 % should also work for relation ??
5535
5536 check_element_of_function_closure(X,Y,Parameters,PT,CondClosure,WF) :-
5537 is_special_closure(Parameters,PT,CondClosure, SpecialClosure),!, % this covers recursive closures
5538 ? element_of_special_closure(SpecialClosure,(X,Y),WF,Parameters,PT,CondClosure).
5539 check_element_of_function_closure(X,Y, P,T,ClosureBody, WF) :-
5540 % affects test 1312, unless we add s:seq(0..9) before calling num
5541 % a special rule which tries and avoid enumerating solutions to arguments of function application
5542 % usually a function application will either be given all arguments or maybe be used in inverse
5543 ? is_converted_lambda_closure(P,T,ClosureBody), %is_converted_non_recursive_lambda_closure(P,T,ClosureBody),
5544 % TO DO: also make this work for recursive closures by adding recursive args (see e.g. test 1302)
5545 is_lambda_closure(P,T,ClosureBody, OtherIDs, OtherTypes, DomainPred, EXPR),
5546 (debug:debug_level_active_for(4) ->
5547 print('Apply Fun : '), translate:print_bexpr(DomainPred), print(' | '), translate:print_bexpr(EXPR),nl,
5548 get_texpr_info(ClosureBody,I), print(info(I,WF)),nl,
5549 print_term_summary((X,Y)),nl %,trace
5550 ; true),
5551 !,
5552 % alternative: annotate X,Y as inner variable ?
5553 get_texpr_info(ClosureBody,BInfo),
5554 ? b_interpreter:set_up_typed_localstate2(OtherIDs,OtherTypes,BInfo,ParValues,_TypedVals,[],LocalState,positive),
5555 convert_list_into_pairs(ParValues,SingleParValue),
5556 ? kernel_objects:equal_object_wf(X,SingleParValue,check_element_of_function_closure,WF),
5557 (is_truth(DomainPred) -> true
5558 ; init_wait_flags(InnerWF,[check_element_of_function_closure]),
5559 %copy_wf01e_wait_flags(WF,InnerWF), % we could delay copying WF0 until after test_boolean_expression of DomainPred ?
5560 b_test_boolean_expression(DomainPred,LocalState,[],InnerWF),
5561 ? get_wait_flag0(WF,WF0), get_wait_flag0(InnerWF,WF0), % was: ground_wait_flag0(InnerWF), but this can result in inner WF0 being set when outer is not yet set; see test 1948
5562 ground_value_check(X,GrX),
5563 (nonvar(GrX) -> copy_waitflag_store(InnerWF,WF) % block would trigger already
5564 ; ground_value_check(Y,GrY),
5565 (nonvar(GrY) -> copy_waitflag_store(InnerWF,WF) % block would trigger already
5566 ; get_last_wait_flag(check_element_of_function_closure(OtherIDs),WF,LastWF),
5567 block_copy_waitflag_store(InnerWF,WF,GrX,GrY,LastWF)
5568 )
5569 )
5570 ),
5571 ? b_interpreter:b_compute_expression(EXPR,LocalState,[],Y,WF).
5572 check_element_of_function_closure(X,Y, P,T,ClosureBody, WF) :-
5573 ? element_of_normal_closure((X,Y),P,T,ClosureBody,WF).
5574 % we could memoize on X here if /*@symbolic-memo */ pragma used and closure has special ID associated with it
5575
5576 :- block block_copy_waitflag_store(?,?,-,-,-).
5577 block_copy_waitflag_store(InnerWF,WF,_GrX,_GrY,_LWF) :-
5578 % copy waitflags from InnerWF store to WF
5579 copy_waitflag_store(InnerWF,WF).
5580
5581 /* -------------- */
5582 /* NOT_ELEMENT_OF */
5583 /* -------------- */
5584
5585 :- use_module(kernel_objects,[not_element_of_global_set/2]).
5586
5587 not_element_of_custom_set_wf(X,CS,WF) :-
5588 ? not_element_of_custom_set_wf2(CS,X,WF).
5589
5590 not_element_of_custom_set_wf2(global_set(GS),X,_WF) :- not_element_of_global_set(X,GS).
5591 not_element_of_custom_set_wf2(freetype(_),_,_) :- !,fail. % TO DO: what if we have List(1..3) ? can that occur ??
5592 not_element_of_custom_set_wf2(avl_set(node(Y,_,_,empty,empty)),X,WF) :- !,
5593 % X /: {Y} <=> X /= Y
5594 ? kernel_objects:not_equal_object_wf(X,Y,WF). % improve if X is ground
5595 not_element_of_custom_set_wf2(avl_set(AVL),X,_WF) :- !,
5596 ground_value_check(X,GrX),
5597 ? propagate_avl_not_element_information(X,GrX,AVL),
5598 not_element_of_avl_set_block(GrX,X,AVL).
5599 not_element_of_custom_set_wf2(closure(Parameters,PT,Cond),X,WF) :-
5600 closure_not_member(X,Parameters,PT,Cond,WF).
5601
5602 :- block not_element_of_avl_set_block(-,?,?).
5603 not_element_of_avl_set_block(_,X,AVL) :-
5604 convert_to_avl_inside_set(X,CX),
5605 \+ avl_fetch(CX,AVL). %% IMPROVE ??
5606
5607 propagate_avl_not_element_information(_,GrEl,_) :- nonvar(GrEl),!.
5608 propagate_avl_not_element_information(Element,_,AVL) :- preferences:preference(use_clpfd_solver,true),
5609 is_avl_simple_set(AVL,Type), % integer or global(GS) \+ground(Element) ,
5610 ((Type=integer -> avl_height_less_than_with_solver_strength(AVL,6) % 16-31 elements - was: avl_size<20
5611 ; true)
5612 -> !,
5613 ? propagate_avl_not_element_information3(Type,Element,AVL) % uses clpfd_not_inlist
5614 ; Type=integer, avl_height_less_than_with_solver_strength(AVL,15),
5615 avl_is_interval(AVL,Min,Max)
5616 -> !,
5617 kernel_objects:not_in_nat_range(Element,int(Min),int(Max)) % WF not used anyway in _wf version
5618 ).
5619 propagate_avl_not_element_information(_Element,_,AVL) :-
5620 quick_definitely_maximal_set_avl(AVL),
5621 !, % we require something not to be an element of the full set; impossible
5622 fail.
5623 % to do: check if all but one element is in set
5624 propagate_avl_not_element_information(_,_,_).
5625
5626 avl_height_less_than_with_solver_strength(AVL,Limit) :- preference(solver_strength,SS),
5627 RealLimit is Limit + SS/100,
5628 avl_height_less_than(AVL,RealLimit).
5629
5630 % try and compute a small finite cardinality for a ground value; fail if not possible
5631 try_get_finite_max_card_from_ground_value(pred_true,2).
5632 try_get_finite_max_card_from_ground_value(pred_false,2).
5633 try_get_finite_max_card_from_ground_value(fd(_,Type),Card) :-
5634 b_global_sets:b_fd_card(Type,Card), integer(Card).
5635 try_get_finite_max_card_from_ground_value((A,B),Card) :-
5636 try_get_finite_max_card_from_ground_value(A,CA),
5637 try_get_finite_max_card_from_ground_value(B,CB),
5638 Card is CA*CB,
5639 Card < 20000.
5640 try_get_finite_max_card_from_ground_value(rec(Fields),Card) :-
5641 try_get_finite_max_card_from_fields(Fields,Card).
5642 try_get_finite_max_card_from_ground_value(freeval(FreetypeId,_CaseId,_EArgs),Card) :-
5643 freetype_cardinality(FreetypeId,Card), number(Card), Card < 20000.
5644 try_get_finite_max_card_from_ground_value(avl_set(node(El,_True,_,_,_)),Card) :-
5645 try_get_finite_max_card_from_ground_value(El,CEl),
5646 CEl < 16,
5647 safe_pow2(CEl,Card).
5648 % int(_), term(floating(_)), string(_) are all infinite
5649
5650 try_get_finite_max_card_from_fields([],1).
5651 try_get_finite_max_card_from_fields([field(_,A)|TF],Card) :-
5652 try_get_finite_max_card_from_ground_value(A,CA),
5653 try_get_finite_max_card_from_fields(TF,CB),
5654 Card is CA*CB,
5655 Card < 20000.
5656
5657 :- use_module(b_global_sets,[get_global_type_value/3]).
5658 propagate_avl_not_element_information3(integer,int(El),AVL) :-
5659 avl_domain(AVL,R),project_avl_domain_on_fd(R,FDList),
5660 clpfd_interface:clpfd_not_inlist(El,FDList).
5661 propagate_avl_not_element_information3(global(GS),FD,AVL) :-
5662 get_global_type_value(FD,GS,El), % sets up the FD constraint if var; maybe we can detect inconsistency straightaway below
5663 avl_domain(AVL,R),project_avl_domain_on_fd(R,FDList), % maybe we can compute directly the complement ?
5664 ? clpfd_interface:clpfd_not_inlist(El,FDList).
5665
5666
5667 :- block closure_not_member(?,-,?,?,?).
5668 %, closure_not_member(-,?,?,?,?). /* El is unlikely to be instantiated by not_element_of test , but test 6 requires commenting out block declaration */
5669
5670 closure_not_member(X,Parameters,Types,Body,WF) :-
5671 is_special_closure(Parameters,Types,Body,SpecialClosure),!,
5672 not_element_of_special_closure(SpecialClosure,X,WF,Parameters,Types,Body).
5673 closure_not_member(El,Parameters,PT,Cond,WF) :-
5674 normal_closure_not_member(El,Parameters,PT,Cond,WF).
5675
5676 :- use_module(memoization,[not_element_of_memoization_closure/6]).
5677 not_element_of_special_closure(interval(LOW,UP),X,_WF,_Parameters,_Types,_Body) :-
5678 !,kernel_objects:not_in_nat_range(X,int(LOW),int(UP)).
5679 not_element_of_special_closure(member_closure(_ID,_Type,VAL),X,WF,_Parameters,_Types,_Body) :-
5680 ( VAL = value(_)
5681 ; VAL = cartesian_product(b(value(A),_,_),b(value(B),_,_))),!,
5682 %hit_profiler:add_profile_hit(member(X,Par,Typ,Body)),
5683 ( VAL=value(Set) -> kernel_objects:not_element_of_wf(X,Set,WF)
5684 ; kernel_objects:not_is_cartesian_pair(X,A,B,WF)).
5685 not_element_of_special_closure(not_member_closure(_ID,_Type,value(Set)),X,WF,_Parameters,_Types,_Body) :-
5686 !,kernel_objects:check_element_of_wf(X,Set,WF).
5687 not_element_of_special_closure(memoization_closure(MemoID),X,WF,P,T,B) :- !,
5688 not_element_of_memoization_closure(MemoID,X,WF,P,T,B).
5689 not_element_of_special_closure(recursive_special_closure(RId),X,WF,Parameters,Types,Body) :-
5690 !,
5691 add_recursive_parameter(Parameters,Types,X,RId,Body,NewParameters,NewPT,Value,WF),
5692 normal_closure_not_member(Value,NewParameters,NewPT,Body,WF).
5693
5694 not_element_of_special_closure(SC,_X,_WF,Parameters,Types,Body) :-
5695 SC \= interval(_,_),
5696 SC \= not_member_closure(_,_,_),
5697 is_definitely_maximal_closure(Parameters,Types,Body),
5698 !,
5699 fail.
5700 not_element_of_special_closure(_,X,WF,Parameters,Types,Body) :-
5701 % falling back to normal test
5702 normal_closure_not_member(X,Parameters,Types,Body,WF).
5703
5704 :- use_module(library(lists),[same_length/2]).
5705
5706 normal_closure_not_member(El,Parameters,PT,Cond,WF) :-
5707 %hit_profiler:add_profile_hit(closure_not_member(El,Parameters,PT,Cond,WF)),
5708 same_length(Parameters,ParValues),
5709 convert_list_into_pairs(ParValues,El),
5710 b_not_test_closure_wf(Parameters,PT,Cond,ParValues,WF).
5711
5712
5713
5714
5715 /* -------------------------- */
5716 /* VARIOUS CLOSURE PREDICATES */
5717 /* -------------------------- */
5718
5719
5720 :- use_module(tools,[convert_list_into_pairs/2]).
5721 :- use_module(b_interpreter,[b_test_boolean_expression/4, b_not_test_boolean_expression/4]).
5722 :- use_module(b_enumerate).
5723
5724 :- assert_pre(custom_explicit_sets:expand_closure_to_list(_,_,ClosureBody,_Result,_Done,_,_WF),
5725 (nonvar(ClosureBody),
5726 bsyntaxtree:check_if_typed_predicate(ClosureBody))).
5727 :- assert_post(custom_explicit_sets:expand_closure_to_list(_,_,_,Result,_Done,_,_WF),
5728 b_interpreter:value_type(Result)).
5729
5730 :- block expand_interval_closure_to_avl(-,?,?), expand_interval_closure_to_avl(?,-,?).
5731 expand_interval_closure_to_avl(Low,Up,Result) :-
5732 Delta is Up-Low,
5733 (Delta>9999 -> perfmessage(expanding_interval(Low,Up)) ; true),
5734 construct_interval_ord_list(Low,Up,OL),
5735 ord_list_to_avlset_direct(OL,ARes,expand_interval),
5736 ? equal_object(ARes,Result,expand_interval_closure_to_avl).
5737 construct_interval_ord_list(Low,Up,Res) :-
5738 (Low>Up -> Res = []
5739 ; Res = [int(Low)-true|T], L1 is Low+1, construct_interval_ord_list(L1,Up,T)
5740 ).
5741
5742 :- block expand_interval_closure_to_list(-,?,?,?), expand_interval_closure_to_list(?,-,?,?).
5743 expand_interval_closure_to_list(Low,Up,Result,Done) :-
5744 construct_interval_list(Low,Up,OL),
5745 ? equal_object(OL,Result,expand_interval_closure_to_list),
5746 Done=true.
5747 construct_interval_list(Low,Up,Res) :-
5748 (Low>Up -> Res = []
5749 ; Res = [int(Low)|T], L1 is Low+1, construct_interval_list(L1,Up,T)
5750 ).
5751
5752 expand_closure_to_list([X],[integer],Body,Result,Done,_,_) :-
5753 ? is_interval_closure_body(Body,X,Low,Up),!,
5754 expand_interval_closure_to_list(Low,Up,Result,Done).
5755 expand_closure_to_list(Par,Types,Body,Result,Done,Source,WF) :-
5756 ? expand_normal_closure(Par,Types,Body,CResult,CDone,expand_closure_to_list(Source),WF),
5757 expand_if_avl(CResult,Result,CDone,Done,Source),
5758 lazy_check_elements_of_closure(Result,CDone, Par,Types,Body,WF).
5759
5760 % Note: does slow down test 1306 (91ms mc time becomes 918 ms)
5761 % as long as a closure has not been fully expanded, lazily check elements
5762 % that are instantiated from the outside satisfy the closure predicate
5763 % Note: this can also instantiate unknown values used inside the closure body
5764 lazy_check_elements_of_closure(Result,CDone, Par,Types,Body,WF) :-
5765 (WF==no_wf_available -> true
5766 ; lazy_check_elements6(Result,CDone, Par,Types,Body,WF),
5767 propagate_closure_body_value_set(Par,Types,Body,Result,CDone,WF)
5768 ).
5769 % TODO: check if closure is a non-ground projection-member closure and check elements
5770 :- block lazy_check_elements6(-,-, ?,?,?,?).
5771 lazy_check_elements6(_Result,CDone, _Par,_Types,_Body,_WF) :- nonvar(CDone),!.
5772 lazy_check_elements6([H|T],CDone, Par,Types,Body,WF) :- !,
5773 ? element_of_closure(H,Par,Types,Body,WF),
5774 ? lazy_check_elements6(T,CDone, Par,Types,Body,WF).
5775 lazy_check_elements6(avl_set(A),_CDone, Par,Types,Body,WF) :- !,
5776 avl_max(A,X),
5777 element_of_closure(X,Par,Types,Body,WF).
5778 % TO DO: also check avl_min or even all elements ?
5779 lazy_check_elements6(_,_,_,_,_,_).
5780
5781 :- use_module(probsrc(bsyntaxtree),[create_typed_ids/3]).
5782 % lazy check elements from non-var closure body against a result
5783 % for example if we have {x| TRUE |-> x : Value } = Result and Value is not-ground,
5784 % we can check that for all elements TRUE|->x of Value the corresponding x is in Result, see test 2466
5785 % slows down test 1987
5786 :- block propagate_closure_body_value_set(?,?,?,-,-,?).
5787 % we delay until the result is known, possibly in SMT mode it could be useful to propagate earlier
5788 propagate_closure_body_value_set(ParIDs,Types,Body,Result,CDone,WF) :-
5789 var(CDone), % the closure has not yet been fully expanded
5790 % check if this closure can profit from set membership propagation:
5791 b_interpreter:is_for_all_set_membership_predicate2(Body,ParIDs,ParIDs,UnmatchedIDs,Set,_Pattern,_ParValues,_),
5792 UnmatchedIDs=[],
5793 Set = b(value(_Value),_,_), % check that the set is a value; it must be non-ground, otherwise CDone would be true
5794 create_couple_term(ParIDs,Types,CoupleTerm),
5795 SetTerm=b(value(Result),any,[]),
5796 safe_create_texpr(member(CoupleTerm,SetTerm),pred,[],RHS),
5797 create_typed_ids(ParIDs,Types,TIDs),
5798 !,
5799 propagate_closure_body_for_all(TIDs,Body,RHS,Result,CDone,WF).
5800 propagate_closure_body_value_set(_,_,_,_,_,_WF).
5801
5802 :- block propagate_closure_body_for_all(?,?,?,-,-,?).
5803 propagate_closure_body_for_all(TIDs,Body,RHS,_,CDone,WF) :- var(CDone),!,
5804 add_debug_message(closure,'Propagating from closure body to result: ',Body,Body),
5805 Infos=[],
5806 b_interpreter:b_for_all(TIDs,Infos,Body,RHS,[],[],WF).
5807 propagate_closure_body_for_all(_,_,_,_Result,_CDone,_WF). % propagation not required; closure expanded, cf test 1987
5808
5809 %check_valid_avl(AVL,Origin) :-
5810 % (nonvar(AVL) -> true
5811 % ; add_internal_error('Var avl_set: ', check_valid_avl(AVL,Origin)),fail).
5812
5813 :- block expand_if_avl(?,?,-,?,?).
5814 expand_if_avl(avl_set(S),Result,_,Done,Source) :- !, % we could transmit a flag to expand_normal_closure so that transform_result_into_set does not expand to avl
5815 ? expand_custom_set_to_list2(avl_set(S),Result,Done,_,expand_if_avl(Source),no_wf_available).
5816 expand_if_avl(Res,Result,_,Done,Source) :- check_list(Res,expand_if_avl(Source)),
5817 ? equal_object(Res,Result), Done=true.
5818
5819 check_list(Res,_) :- nonvar(Res), is_list(Res),!.
5820 check_list(Res,Src) :- add_error(Src,'Could not expand to list: ',Res).
5821 is_list([]). is_list([_|_]).
5822
5823 expand_closure_to_avl_or_list([X],[integer],Body,Result,_CheckTimeouts,_WF) :-
5824 ? is_interval_closure_body(Body,X,Low,Up),!,
5825 expand_interval_closure_to_avl(Low,Up,Result).
5826 %expand_closure_to_avl_or_list(P,T,Body,Result,_WF) :- is_member_closure(P,T,Body,TS,Set),
5827 % print(expand_member_closure(P,T,Body,TS,Set)),nl,fail.
5828 expand_closure_to_avl_or_list(Par,Types,Body,Result,CheckTimeouts,WF) :-
5829 expand_normal_closure(Par,Types,Body,CResult,_Done,CheckTimeouts,WF),
5830 kernel_objects:equal_object(Result,CResult,expand_closure_to_avl_or_list). % may convert to AVL, should we wait for _Done?
5831
5832
5833 % use WF just for call stack messages; we should not delay creating result
5834 expand_closure_to_avl_wf([X],[integer],Body,Result,_WF) :-
5835 is_interval_closure_body(Body,X,Low,Up),!,
5836 expand_interval_closure_to_avl(Low,Up,Result). % we could pass WF
5837 expand_closure_to_avl_wf(Par,Types,Body,Result,WF) :-
5838 ? expand_normal_closure(Par,Types,Body,S,Done,check(expand_closure_to_avl),WF),
5839 (ground_value(S) % ground value is sufficient to proceed; we do not need to check Done
5840 -> convert_to_avl_inside_set(S,R),equal_object(R,Result,expand_closure_to_avl)
5841 ; print(cannot_convert_closure_value_to_avl(closure(Par,Types),done(Done))),nl,
5842 translate:print_bexpr(Body),nl,trace,
5843 fail).
5844
5845
5846 % possible values for CheckTimeouts: check, check_no_inf, no_check, ...
5847 % Note: we no longer check is_infinite_explicit_set(closure(Parameters,ParameterTypes,ClosureBody))
5848 % and no longer raise add_closure_warning(Source,Parameters,ParameterTypes,ClosureBody,'### WARNING: expanding infinite comprehension set: ')
5849 % and no longer use preference warn_when_expanding_infinite_closures
5850 % this is relevant for e.g., test 1291
5851 expand_normal_closure(Parameters,ParameterTypes,ClosureBody,Result,Done,CheckTimeouts,WF) :-
5852 ? expand_normal_closure_memo(CheckTimeouts,Parameters,ParameterTypes,ClosureBody,Result,Done,WF).
5853
5854 :- public add_closure_warning_wf/6.
5855 add_closure_warning_wf(Source,Parameters,_ParameterTypes,_ClosureBody,_MSG,_WF) :-
5856 preference(provide_trace_information,false),preference(strict_raise_warnings,false),!,
5857 format('### TIME-OUT raised during closure expansion (~w,~w).~n### set TRACE_INFO preference to TRUE for more details.~n',[Parameters,Source]).
5858 add_closure_warning_wf(Source,Parameters,ParameterTypes,ClosureBody,MSG,WF) :-
5859 (debug_mode(on) -> Limit = 2500, AvlLim=10 ; Limit = 500, AvlLim=5),
5860 preferences:temporary_set_preference(expand_avl_upto,AvlLim,CHNG),
5861 call_cleanup(translate:translate_bvalue_with_limit(closure(Parameters,ParameterTypes,ClosureBody),Limit,CT),
5862 preferences:reset_temporary_preference(expand_avl_upto,CHNG)),
5863 bsyntaxtree:get_texpr_info(ClosureBody,Infos),
5864 add_warning_wf(Source,MSG,CT,Infos,WF), debug_print(19,'! infos: '), debug_println(Infos). %,trace.
5865
5866
5867 :- use_module(memoization,[is_memoization_closure/4,get_complete_memoization_expansion/6]).
5868
5869 % a version of closure expansion which memoizes its results; stored_expansion needs to be cleared when new machine loaded
5870 expand_normal_closure_memo(CHECK,Parameters,ParameterTypes,ClosureBody,FullResult,Done,WF) :-
5871 ? is_memoization_closure(Parameters,ParameterTypes,ClosureBody,MemoID),
5872 !, Span=ClosureBody,
5873 % MemoID can be a variable
5874 (var(MemoID) -> perfmessage(CHECK,'Getting full value of a memoized function',ClosureBody) ; true),
5875 get_complete_memoization_expansion(MemoID,FullResult,Done,Span,expand_normal_closure_memo(CHECK),WF).
5876 expand_normal_closure_memo(CHECK,Parameters,ParameterTypes,ClosureBody,FullResult,Done,WF) :-
5877 preferences:preference(use_closure_expansion_memoization,false),!,
5878 ? expand_normal_closure2(CHECK,Parameters,ParameterTypes,ClosureBody,FullResult,Done,WF).
5879 expand_normal_closure_memo(CHECK,Parameters,ParameterTypes,ClosureBody,FullResult,Done,WF) :-
5880 % maybe we should only memo when ClosureWaitVars are ground ?
5881 MemoLookupTerm = closure(Parameters,ParameterTypes,ClosureBody),
5882 compute_memo_hash(MemoLookupTerm,Hash),
5883 % idea: maybe store expansion only on second hit ?
5884 (get_stored_memo_expansion(Hash,MemoLookupTerm,StoredResult)
5885 -> %print_term_summary(reusing_expansion(Hash,Parameters,ParameterTypes,ClosureBody,StoredResult)),nl,
5886 UPV=StoredResult, %state_packing:unpack_value(StoredResult,UPV),
5887 FullResult = UPV, Done=true
5888 ; %statistics(runtime,[T1,_]), %%
5889 expand_normal_closure2(CHECK,Parameters,ParameterTypes,ClosureBody,FullResult,Done,WF),
5890 %statistics(runtime,[T2,_]), Time is T2-T1, store_memo_computation_time(Hash,Time),
5891 (Done==true/* ,T2-T1>0*/
5892 -> PackedValue=FullResult, %state_packing:pack_value(FullResult,PackedValue),
5893 store_memo_expansion(Hash,MemoLookupTerm,PackedValue)
5894 ; true)
5895 ).
5896
5897
5898 expand_normal_closure2(_CHECK,Parameters,ParameterTypes,ClosureBody,FullResult,Done,WF) :-
5899 % TO DO: add more symbolic member closures who have expression computation code
5900 is_closure1_value_closure(Parameters,ParameterTypes,ClosureBody,VAL),!,
5901 ? bsets_clp:relational_trans_closure_wf(VAL,FullResult,WF),
5902 ground_value_check(FullResult,FRGr),
5903 when(nonvar(FRGr),Done=true).
5904 expand_normal_closure2(CHECK,Parameters,ParameterTypes,ClosureBody,FullResult,Done,WF) :-
5905 % special treatment for lambda closures: Advantage: we don't have to wait for variables in EXPR body of closure
5906 % Disadvantage: EXPR only gets evaluated after a solution has been found for args: can mean repeated computations !
5907 % (cf pas_as_env_inv_cv_sui, negated version of !(cv_i).(cv_i : t_cv_pas => closure(%cv_o2.((...|>> {cv_i} : t_cv_pas <-> t_cv_pas) ASSERTION
5908 % Advantage: it can solve constraints such as f = %x.(x:1..10|x+y) & f(5)=1005 (finding y without enumeration); see test 1168
5909 \+ preferences:preference(use_smt_mode,false),
5910 is_lambda_closure(Parameters,ParameterTypes,ClosureBody, OtherIDs,OtherTypes, DomainPred, EXPR),
5911 \+ ground_bexpr(EXPR), % if EXPR is ground, there is nothing to be gained by special treatment here
5912 WF \= no_wf_available, % otherwise we may have to enumerate EXPR result leading to choice points, e.g. in phase 0
5913 !,
5914 bexpr_variables(DomainPred,ClosureWaitVars),
5915 (CHECK=no_check -> TIMEOUTCODE = true ;
5916 TIMEOUTCODE = add_closure_warning_wf(CHECK,Parameters,ParameterTypes,ClosureBody,
5917 'TIME-OUT occurred while ProB was expanding: ',WF)),
5918 (CHECK=check_no_inf -> VIRTUALTIMEOUTCODE=true ; VIRTUALTIMEOUTCODE=TIMEOUTCODE),
5919 delay_setof_check_wf( ParTuple,
5920 (custom_explicit_sets:b_test_closure(OtherIDs,OtherTypes,DomainPred,OtherValues,all_solutions,WF),
5921 convert_list_into_pairs(OtherValues,ParTuple)
5922 % TO DO: compile EXPR when we start expanding the closure: to avoid repeated re-computation of expressions for every instance
5923 ),
5924 Result, ClosureWaitVars, __Done,
5925 TIMEOUTCODE,VIRTUALTIMEOUTCODE,WF,DomainPred),
5926 (WF = no_wf_available
5927 -> init_wait_flags(WF1,[expansion_context(lambda_function_result,Parameters)])
5928 ; WF1=WF
5929 ),
5930 evaluate_result_expr(Result,EXPR,OtherIDs,EvResult,EvDone,WF1),
5931 when(nonvar(EvDone),(
5932 (WF = no_wf_available -> ground_wait_flags(WF1) ; true),
5933 kernel_objects:equal_object_wf(EvResult,FullResult,expand_normal_closure2,WF),
5934 Done=true)).
5935 expand_normal_closure2(no_check,Parameters,ParameterTypes,ClosureBody,Result,Done,WF) :- !,
5936 expand_normal_closure_direct(Parameters,ParameterTypes,ClosureBody,Result,Done,WF).
5937 expand_normal_closure2(CHECK,Parameters,ParameterTypes,ClosureBody,Result,Done,WF) :-
5938 bexpr_variables(ClosureBody,ClosureWaitVars),
5939 TIMEOUTCODE = add_closure_warning_wf(CHECK,Parameters,ParameterTypes,ClosureBody,
5940 'TIME-OUT occurred while ProB was expanding: ',WF),
5941 (CHECK=check_no_inf -> VIRTUALTIMEOUTCODE=true ; VIRTUALTIMEOUTCODE=TIMEOUTCODE),
5942 % Note: delay_setof_check_wf will throw enumeration warning for virtual timeouts, after VIRTUALTIMEOUTCODE
5943 delay_setof_check_wf( ParTuple,
5944 custom_explicit_sets:test_closure_and_convert(Parameters,ParameterTypes,ClosureBody, ParTuple,WF),
5945 Result, ClosureWaitVars, Done, TIMEOUTCODE, VIRTUALTIMEOUTCODE,WF,ClosureBody).
5946
5947 expand_normal_closure_direct(Parameters,ParameterTypes,ClosureBody,Result,Done,WF) :-
5948 bexpr_variables(ClosureBody,ClosureWaitVars),
5949 Span = ClosureBody,
5950 delay_setof_wf( ParTuple,
5951 % TO DO: refresh waitflag in outer WF store to let pending code run to completion and avoid spurious WD errors ?
5952 custom_explicit_sets:test_closure_and_convert(Parameters,ParameterTypes,ClosureBody, ParTuple,WF),
5953 Result, ClosureWaitVars, Done,WF, Span).
5954
5955
5956
5957 :- block evaluate_result_expr(-,?,?,?,?,?).
5958 evaluate_result_expr(avl_set(AVL),EXPR,OtherIDs,Res,Done,WF) :-
5959 avl_domain(AVL,R),
5960 evaluate_result_expr(R,EXPR,OtherIDs,Res,Done,WF).
5961 evaluate_result_expr([],_EXPR,_OtherIDs,[],Done,_WF) :-
5962 %ground_wait_flags(WF),
5963 Done=true.
5964 evaluate_result_expr([ParTuple|T],EXPR,OtherIDs,[FullTuple|ET],Done,WF) :-
5965 % same_length(OtherIDs,ParValues), % not necessary
5966 set_up_localstate(OtherIDs,ParValues,[],LocalState),
5967 convert_list_into_pairs(ParValues,ParTuple), % bind values in ParTuple to LocalState
5968 b_interpreter:b_compute_expression(EXPR,LocalState,[],EXPRVALUE,WF),
5969 append(ParValues,[EXPRVALUE],FullValues),
5970 convert_list_into_pairs(FullValues,FullTuple),
5971 evaluate_result_expr(T,EXPR,OtherIDs,ET,Done,WF).
5972
5973 :- use_module(bsyntaxtree,[split_names_and_types/3]).
5974 :- use_module(probsrc(bsyntaxtree), [def_get_texpr_id/2]).
5975 %:- use_module(library(lists),[prefix_length/3, suffix_length/3]).
5976 % test a closure and convert into pairs; assume we want all solutions
5977 test_closure_and_convert(Parameters,ParameterTypes,ClosureBody, ParTuple, WF) :-
5978 ? is_recursive_closure(Parameters,ParameterTypes,ClosureBody),
5979 ? get_recursive_identifier_of_closure_body(ClosureBody,TRID),!,
5980 def_get_texpr_id(TRID,RID), get_texpr_type(TRID,RType),
5981 %print(test_recursion(RID)),nl, translate:nested_print_bexpr(ClosureBody),nl,
5982 RecVal = closure(Parameters,ParameterTypes,ClosureBody), % Recursive Value added to parameters
5983 same_length(Parameters,ParValues),
5984 reset_closure_solution_counter(Parameters),
5985 ? b_test_closure([RID|Parameters],[RType|ParameterTypes],ClosureBody,[RecVal|ParValues],all_solutions,WF),
5986 convert_sol_list_into_pairs(ParValues,Parameters,ParTuple). % convert tuple without recursive value to ParTuple
5987 test_closure_and_convert(Parameters,ParameterTypes,b(exists(EParAndTypes,ClosureBody),pred,OuterInfo), ParTuple, WF) :-
5988 % Motivation: enumerating Parameters can be quite inefficient
5989 % if for example we have something like {x|#y.(y:SmallSet & x=f(y))}
5990 % Problem: the existential quantifier will be delayed until the Parameters are instantiated !
5991 % relevant test: 1162
5992 % Note: this is duplicating to some extent the code in b_test_exists_wo_expansion
5993 % However, here we can also apply lambda_closure optimisation in b_test_closure below, this is
5994 % relevant for private_examples/2023/.../rule_FICHIER_MRGATKSAATPAR_RVF219_MRGA_DE.mch
5995 ? exists_should_be_lifted(Parameters,ParameterTypes,OuterInfo,ClosureBody),
5996 split_names_and_types(EParAndTypes,EPar,ETypes),
5997 !,
5998 % print(' Lifting existential quantifier (i.e., enumerating paras with closure paras): '), print(EPar),nl,
5999 % print(outer_paras(Parameters)),nl,
6000 % append Parameters at end; in case we have a lambda function
6001 append(EPar,Parameters,FullPar), length(Parameters,NrParas),
6002 append(ETypes,ParameterTypes,FullTypes),
6003 length(EPar,NrExistsParas),
6004 length(IrrelevantParas,NrExistsParas), length(Suffix,NrParas),
6005 append(IrrelevantParas,Suffix,FullParList),
6006 copy_identifier_infos(OuterInfo,ClosureBody,ClosureBody2),
6007 reset_closure_solution_counter(Parameters),
6008 % bsyntaxtree:check_used_ids_in_ast(ClosureBody2),
6009 ? b_test_closure(FullPar,FullTypes,ClosureBody2, FullParList,all_solutions,WF),
6010 convert_sol_list_into_pairs(Suffix,Parameters,ParTuple).
6011 test_closure_and_convert(Parameters,ParameterTypes,ClosureBody, ParTuple, WF) :-
6012 reset_closure_solution_counter(Parameters),
6013 % print(test),nl, translate:nested_print_bexpr(ClosureBody),nl,
6014 length(Parameters,Len), length(ParValues,Len),
6015 %(annotate_exists(Parameters,ParameterTypes,ClosureBody,Body2) -> true ; Body2=ClosureBody),
6016 ? b_test_closure(Parameters,ParameterTypes,ClosureBody,ParValues,all_solutions,WF),
6017 convert_sol_list_into_pairs(ParValues,Parameters,ParTuple). % ,print(solution(ParTuple)),nl,nl.
6018
6019 % Lifting existential quantifier was previously done here, but was duplicating code in b_test_exists_wo_expansion
6020 % we now simply generate the allow_to_lift_exists annotation here and let b_test_exists_wo_expansion do its job
6021 %annotate_exists(Parameters,ParameterTypes,
6022 % b(exists(EParAndTypes,ClosureBody),pred,OuterInfo),
6023 % b(exists(EParAndTypes,ClosureBody),pred,[allow_to_lift_exists|OuterInfo])) :-
6024 % exists_should_be_lifted(Parameters,ParameterTypes,OuterInfo,ClosureBody).
6025
6026 % check if a top-level exists with body ExistsClosureBody should be lifted
6027 % within a closure with paras Parameters of type ParameterTypes:
6028 exists_should_be_lifted(Parameters,ParameterTypes,OuterInfo,ExistsClosureBody) :-
6029 (Parameters == ['_was_lambda_result_'] % here we are quite sure that we gain by this optimisation
6030 ? ; member(allow_to_lift_exists,OuterInfo) % parameters were originally from a set comprehension,
6031 % see test 306: in this case existential quantifier is lifted in b_interpreter anyway;
6032 % Note we counter the rewrite ran({x1,...xn|P}) ---> {xn| #(x1,...).(P)} and similarly for dom({...})
6033 ; ExistsClosureBody = b(member(_,_),_,_) % we have a simple projection closure
6034 % TO DO: maybe support other ones as well
6035 ? ; basic_type_list_cardinality(ParameterTypes,Card),
6036 (Card=inf -> true ; Card>10000)
6037 % if here are only a few parameter values: do not lift existential quantified variables
6038 ).
6039
6040 % we need to copy important infos about the outer Parameters to ClosureBody
6041 copy_identifier_infos(Info,b(InnerPred,T,II),b(InnerPred,T,II2)) :-
6042 findall(I,identifier_info(I,Info),ToCopy),
6043 append(ToCopy,II,II2).
6044 identifier_info(I,Info) :- I=prob_annotation('DO_NOT_ENUMERATE'(ID)),
6045 ? member(I,Info), ID \= '$$NONE$$'.
6046
6047 convert_sol_list_into_pairs(ParaValues,Parameters,ParTuple) :-
6048 convert_list_into_pairs(ParaValues,ParTuple),
6049 update_closure_solution_counter(Parameters,ParTuple).
6050
6051 :- if(environ(prob_debug_flag,true)).
6052 :- dynamic closure_solution_counter/3.
6053 % debugging long expansions of comprehension_set / closures
6054 reset_closure_solution_counter(Parameters) :- retractall(closure_solution_counter(Parameters,_,_)).
6055
6056 update_closure_solution_counter(Parameters,ParTuple) :-
6057 retract(closure_solution_counter(Parameters,OldCount,OldTime)),!,
6058 statistics(walltime,[W2,_]), Delta is W2-OldTime,
6059 NewCount is OldCount+1,
6060 ((Delta > 5000 ; NewCount mod 1000 =:= 0)
6061 -> format('--> Solution ~w for expansion of closure ~w (delta ~w ms): ',[NewCount,Parameters,Delta]),
6062 translate:print_bvalue(ParTuple),nl,
6063 assert(closure_solution_counter(Parameters,NewCount,W2))
6064 ; assert(closure_solution_counter(Parameters,NewCount,OldTime))
6065 ).
6066 update_closure_solution_counter(Parameters,_ParTuple) :-
6067 statistics(walltime,[W2,_]),
6068 assert(closure_solution_counter(Parameters,1,W2)).
6069 :- else.
6070 reset_closure_solution_counter(_).
6071 update_closure_solution_counter(_,_).
6072 :- endif.
6073
6074
6075
6076 % compute cardinality of a list of basic types
6077 basic_type_list_cardinality([],1).
6078 basic_type_list_cardinality([BasicType|T],Res) :-
6079 ? basic_type_list_cardinality(T,TCard),
6080 (TCard=inf -> Res=inf
6081 ? ; kernel_objects:max_cardinality(BasicType,Card),
6082 safe_mul(Card,TCard,Res)
6083 ).
6084
6085 % for lambda closures we can set up a second waitflag for the expression and only ground it when body enumeration finished
6086 % idea is to avoid perturbation of constraint solving of main closure predicate by lambda expression, see test 1737
6087 % something like %(x,y).(x:1..200 & y:1..100 & y+x<259 & y*x>10|(y+x*x+y) mod 100) is faster
6088 % this is slower : %(x,y).(x:1..200 & y:1..100 |(y+x*x+y))
6089 % currently this slows down test 1336
6090 :- block b_test_closure(?,?,-,?,?,?).
6091 b_test_closure(Parameters,ParameterTypes,ClosureBody, FullParValues, NegationContext, OuterWF) :-
6092 (preference(data_validation_mode,true)
6093 -> true % avoids ineraction between domain and range expression enumeration; see
6094 % private_examples/ClearSy/2019_May/perf_3264/rule_186.mch or
6095 % computation of 631 ic___DMI_MRGATKSAAT___Parametre_Identifiant_indices_function in rule_FICHIER_MRGATKSAATPAR_RVF219_MRGA_DE.mch
6096 % however, as b_optimize below does *not* evaluate nested set comprehensions, there can be a slowdown:
6097 % the nested set comprehension gets re-evaluated for every soluiton of the lambda parameters !
6098 % this was the case of private_examples/ClearSy/2019_Nov/rule_Regle_31C_0005/rule.mch before using SORT
6099 ; \+ preferences:preference(use_smt_mode,false)), % TO DO: enable in normal mode when performance of 1336 fixed
6100 % print(test_closure(Parameters,FullParValues)),nl,
6101 is_lambda_closure(Parameters,ParameterTypes,ClosureBody, OtherIDs,OtherTypes, DomainPred, EXPR),
6102 % TO DO: detect not only equalities at end, but any equality which is irrelevant for the rest
6103 % nl,print(lambda_closure(OtherIDs)),nl, translate:print_bexpr(EXPR),nl,
6104 append(ParValues,[LambdaResult],FullParValues),
6105 !,
6106 get_texpr_info(ClosureBody,BInfo),
6107 ? b_interpreter:set_up_typed_localstate2(OtherIDs,OtherTypes,BInfo,ParValues,TypedVals,[],LocalState,NegationContext),
6108 simplify_span(ClosureBody,BSpan), % sometimes BInfo no longer contains a position info, but first_sub_expr does
6109 init_quantifier_wait_flag(OuterWF,comprehension_set(NegationContext),OtherIDs,ParValues,BSpan,WF),
6110 ? b_test_boolean_expression(DomainPred,LocalState,[],WF),
6111 %print('PRED: '),translate:print_bexpr(ClosureBody),nl,
6112 b_tighter_enumerate_values_in_ctxt(TypedVals,DomainPred,WF), % also does: project_away_useless_enumeration_values
6113 init_quantifier_wait_flag(OuterWF,comprehension_set(NegationContext),OtherIDs,ParValues,BSpan,WF2),
6114 ? b_compiler:b_optimize(EXPR,[],LocalState,[],CEXPR,WF), % already pre-compile lookup, without constraint processing; is not sufficient for test 1336
6115 ? ground_wait_flags(WF), % TODO: also call ground inner WF in context
6116 ? b_interpreter:b_compute_expression(CEXPR,LocalState,[],LambdaResult,WF2),
6117 ground_inner_wait_flags_in_context(NegationContext,WF2).
6118 b_test_closure(Parameters,ParameterTypes,ClosureBody,ParValues,NegationContext, OuterWF) :-
6119 % tools:print_bt_message(b_test_closure_testing_closure(Parameters,ParValues)), %%
6120 get_texpr_info(ClosureBody,BInfo),
6121 ? b_interpreter:set_up_typed_localstate2(Parameters,ParameterTypes,BInfo,
6122 ParValues,TypedVals,[],LocalState,NegationContext),
6123 % print_message(b_interpreter:b_test_boolean_expression(ClosureBody,LocalState,[],WF)),
6124 simplify_span(ClosureBody,BSpan), % sometimes BInfo no longer contains a position info, but first_sub_expr does
6125 init_quantifier_wait_flag(OuterWF,comprehension_set(NegationContext),Parameters,ParValues,BSpan,WF),
6126 %external_functions:observe_parameters(Parameters,LocalState), %%
6127 ? b_test_boolean_expression(ClosureBody,LocalState,[],WF),
6128 % tools:print_bt_message(tested_bool_expr), translate:print_bexpr(ClosureBody),nl,
6129 b_enumerate:b_tighter_enumerate_values_in_ctxt(TypedVals,ClosureBody,WF), % also detects useless enumeration ids
6130 ? ground_inner_wait_flags_in_context(NegationContext,WF).
6131
6132
6133
6134 :- block b_not_test_closure_wf(?,?,?,-,?).
6135 b_not_test_closure_wf(Parameters,ParameterTypes,Closure,ParValues,WF) :-
6136 % same_length(Parameters,ParValues), % not necessary
6137 set_up_localstate(Parameters,ParValues,[],LocalState),
6138 b_enumerate:b_type_values_in_store(Parameters,ParameterTypes,LocalState),
6139 b_not_test_boolean_expression(Closure,LocalState,[],WF),
6140 get_last_wait_flag(b_not_test_closure_wf(Parameters),WF,WF2),
6141 get_texpr_info(Closure,Infos),
6142 b_not_test_closure_enum(Parameters,ParameterTypes,Infos,LocalState,WF,WF2).
6143
6144 :- block b_not_test_closure_enum(-,?,?,?,?,?).
6145 b_not_test_closure_enum(Parameters,ParameterTypes,Infos,LocalState,WF,WF2) :-
6146 b_enumerate:b_extract_typedvalc(Parameters,ParameterTypes,Infos,LocalState,TypedVals),
6147 (var(WF2) -> ground_typedvals_check(TypedVals,GrVals) ; true),
6148 b_not_test_closure_enum_aux(GrVals,WF2,TypedVals,WF).
6149
6150 :- block b_not_test_closure_enum_aux(-,-,?,?).
6151 b_not_test_closure_enum_aux(_,_,TypedVals,WF) :-
6152 b_enumerate:b_tighter_enumerate_all_values(TypedVals,WF).
6153 % , print(finished_enum(Parameters)),nl.
6154
6155
6156 :- use_module(library(terms)).
6157 % check whether a VARIABLE occurs inside a closure
6158 closure_occurs_check(VARIABLE,_Par,_ParTypes,ClosureBody) :- expression_contains_setvar(ClosureBody,VARIABLE).
6159 % /* occurs check; x = closure1(x) ; for other closures this cannot happen ???!!! TO DO: Check */
6160 % custom_explicit_sets:is_closure1_value_closure(Par,ParTypes,ClosureBody,Val),
6161 % contains_var(VARIABLE,Val).
6162
6163 expression_contains_setvar(b(E,_,_),Variable) :- !,
6164 expression_contains_setvar_aux(E,Variable).
6165 expression_contains_setvar(E,V) :- add_internal_error('Illegal Expression: ', expression_contains_setvar(E,V)),
6166 contains_var(V,E).
6167
6168 expression_contains_setvar_aux(value(Val),Variable) :- !,value_contains_setvar(Val,Variable).
6169 % a few very common cases for performance; currently this predicate is often called for recursive functions
6170 expression_contains_setvar_aux(identifier(_),_) :- !,fail.
6171 expression_contains_setvar_aux(equal(A,B),Variable) :- !,
6172 (expression_contains_setvar(A,Variable) -> true ; expression_contains_setvar(B,Variable)).
6173 expression_contains_setvar_aux(conjunct(A,B),Variable) :- !,
6174 (expression_contains_setvar(A,Variable) -> true ; expression_contains_setvar(B,Variable)).
6175 expression_contains_setvar_aux(function(A,B),Variable) :- !,
6176 (expression_contains_setvar(A,Variable) -> true ; expression_contains_setvar(B,Variable)).
6177 expression_contains_setvar_aux(union(A,B),Variable) :- !,
6178 (expression_contains_setvar(A,Variable) -> true ; expression_contains_setvar(B,Variable)).
6179 expression_contains_setvar_aux(couple(A,B),Variable) :- !,
6180 (expression_contains_setvar(A,Variable) -> true ; expression_contains_setvar(B,Variable)).
6181 % the rest via safe_syntaxelement:
6182 expression_contains_setvar_aux(Expr,V) :-
6183 safe_syntaxelement_det(Expr,Subs,_Names,_,_),!,
6184 ? member(Sub,Subs), expression_contains_setvar(Sub,V),!.
6185 expression_contains_setvar_aux(E,V) :- add_internal_error('Illegal Expression: ', expression_contains_setvar_aux(E,V)),
6186 contains_var(V,E).
6187
6188 value_contains_setvar(Val,V) :- var(Val),!,Val==V.
6189 value_contains_setvar(avl_set(_),_V) :- !, fail. % assume avl_set always properly grounded; avoid looking inside
6190 value_contains_setvar(closure(_,_,Body),V) :- !,
6191 expression_contains_setvar(Body,V).
6192 value_contains_setvar(int(_),_) :- !,fail. % we check for set variables
6193 value_contains_setvar(global_set(_),_) :- !,fail. % we check for set variables
6194 value_contains_setvar(freetype(_),_) :- !,fail. % we check for set variables
6195 value_contains_setvar(freeval(_ID,_Case,Val),V) :- !, value_contains_setvar(Val,V).
6196 value_contains_setvar(string(_),_) :- !,fail. % we check for set variables
6197 value_contains_setvar(fd(_,_),_) :- !,fail. % we check for set variables
6198 value_contains_setvar((A,B),V) :- !, (value_contains_setvar(A,V) ; value_contains_setvar(B,V)).
6199 value_contains_setvar([A|B],V) :- !, (value_contains_setvar(A,V) ; value_contains_setvar(B,V)).
6200 value_contains_setvar(Val,V) :-
6201 contains_var(V,Val).
6202
6203 % ------------------