1 % (c) 2014-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(clpfd_lists,[
6 lazy_fd_value_check/4,
7 try_in_fd_value_list_check/4,
8 avl_fd_value_check/4,
9 in_fd_value_list_wf/4, in_fd_value_list_wf/3,
10 get_fd_value_list/5,
11 try_get_fd_value_list/4,
12 get_fd_value/3,
13
14 get_fdset_information/2, get_finite_fdset_information/2,
15 combine_fdset_information/3,
16 assert_fdset_information/2
17 ]).
18
19 :- use_module(preferences).
20 :- use_module(error_manager).
21 :- use_module(self_check).
22
23 :- use_module(module_information,[module_info/2]).
24 :- module_info(group,kernel).
25 :- module_info(description,'This module provides a way to call the CLPFD clpfd_inlist and clpfd:element predicates for lists of FD values.').
26
27 % see clpfd_tables test category
28
29 % -----------------------------------------------------
30
31 :- block complete_type(-).
32 % check if we have a complete value type or if we extract only part of the value
33 complete_type(global(_)).
34 complete_type(integer).
35 %complete_type(couple(A,B)) :- fail. % THIS IS NOT COMPLETE !! -> the element checks are done for each component in isolation ! (there is no guarantee that the pair is in the list of pairs)
36
37 % -----------------------------------------------------
38
39 :- block try_in_fd_value_list_check(-,?,?,?).
40 try_in_fd_value_list_check([],_,_,_) :- fail.
41 try_in_fd_value_list_check([H|T],El,Type,WF) :-
42 ? try_get_fd_value_type(El,H,Type), % print(fd_val_type(El,H,T)),nl,
43 !,
44 get_fd_value_list([H|T],Type,FDList,GroundList,El),
45 get_fd_value(Type,El,ElFD),
46 in_fd_value_list_wf(GroundList,ElFD,FDList,WF).
47 try_in_fd_value_list_check(S,El,Type,WF) :-
48 (S=[_|_] -> true
49 ; add_internal_error('Illegal call: ',try_in_fd_value_list_check(S,El,Type,WF))).
50
51 :- use_module(kernel_freetypes,[get_freeval_type/3]).
52
53 % try get fd_value type from two values; one from element one from set
54 ?try_get_fd_value_type(V1,V2,T) :- var(V1),!, try_get_fd_value(T,V2,_).
55 try_get_fd_value_type((A1,_),(A2,_),T) :- nonvar(T),T=couple_left(TA),!,
56 ? try_get_fd_value_type(A1,A2,TA). % useful for domain checks
57 try_get_fd_value_type((A1,B1),(A2,B2),Type) :- !,
58 ? (try_get_fd_value_type(A1,A2,TA)
59 ? -> (try_get_fd_value_type(B1,B2,TB)
60 -> Type = couple(TA,TB) % we have two values
61 ; %print(failed_right(B1,B2,TB)),nl,
62 Type = couple_left(TA))
63 ; Type = couple_right(TB),
64 ? try_get_fd_value_type(B1,B2,TB)).
65 try_get_fd_value_type(freeval(ID,Case,Val1),V2, freeval_case(ID,Case,Type)) :- !,
66 nonvar(Case), ground(ID),
67 (nonvar(V2),V2 = freeval(ID2,Case2,Val2),
68 (ID,Case) == (ID2,Case2)
69 -> try_get_fd_value_type(Val1,Val2,Type)
70 ; % TO DO: if the cases do not match and if Val1 is a variable then we may miss FD values
71 %print(case_mismatch(ID,Case,V2)),nl,
72 get_freeval_type(ID,Case,_CaseType),
73 %print(freeval_case_type(ID,Case,CaseType,Val1)),nl,
74 % TO DO: use CaseType in case Val1 is not instantiated enough
75 try_get_fd_value(Type,Val1,_)).
76 try_get_fd_value_type(V1,V2,T) :-
77 ? (var(V2) -> try_get_fd_value(T,V1,_) ; try_get_fd_value(T,V2,_)).
78
79 % TO DO: merge both versions of try_get_fd_value
80 try_get_fd_value(T,V,_) :- var(V),var(T),!,fail.
81 try_get_fd_value(TV,(A,B),FD) :- var(TV),!,
82 ? (try_get_fd_value(TA,A,FDA)
83 ? -> (try_get_fd_value(TB,B,FDB) % What if this fails because B is still a variable !? --> ideally, typing info would help
84 -> TV = couple(TA,TB), FD=fd_pair(FDA,FDB) % we have two values
85 ; TV = couple_left(TA), FD=FDA)
86 ; TV = couple_right(TB),
87 ? try_get_fd_value(TB,B,FD)).
88 try_get_fd_value(couple(TA,TB),(A,B),fd_pair(FDA,FDB)) :-
89 try_get_fd_value(TA,A,FDA),try_get_fd_value(TB,B,FDB).
90 try_get_fd_value(couple_left(T),(A,_),FDA) :- try_get_fd_value(T,A,FDA). % useful for domain checks
91 % to do: try and get both and do multiple in_list/element calls later
92 try_get_fd_value(couple_right(T),(_,A),FDA) :- try_get_fd_value(T,A,FDA). % useful for range checks
93 try_get_fd_value(rec_fields(T),rec(Fields),fd_rec(FD)) :-
94 try_get_field_fd_values(T,Fields,FD),
95 T \= [].
96 try_get_fd_value(global(GS),fd(X,GS),X).
97 try_get_fd_value(integer,int(X),X).
98
99 try_get_field_fd_values(_,Fields,_) :- var(Fields),!,fail. % we don't know all the fields; better do nothing
100 try_get_field_fd_values(Types,[],FDVals) :- !,Types=[],FDVals=[].
101 try_get_field_fd_values(_,[FN|_],_) :- var(FN),!,fail.
102 try_get_field_fd_values(_,[field(Name,_)|_],_) :- var(Name),!,fail.
103 try_get_field_fd_values(Types,[field(Name,Value)|TF],FDVals) :-
104 ? try_get_fd_value(T,Value,FDVal),
105 !,
106 Types = [field(Name,T)|TTypes],
107 FDVals = [FDVal|TFD],
108 try_get_field_fd_values(TTypes,TF,TFD).
109 try_get_field_fd_values([field_no_fd(Name)|Types],[field(Name,_)|TF],FDVals) :-
110 try_get_field_fd_values(Types,TF,FDVals).
111
112
113 :- use_module(b_global_sets,[get_global_type_value/3]).
114
115 get_fd_value(T,V,E) :- var(V),var(T),!,
116 add_internal_error('Variable arguments: ',get_fd_value(V,T,E)),fail.
117 get_fd_value(couple(TA,TB),(A,B),fd_pair(FDA,FDB)) :-
118 ? get_fd_value(TA,A,FDA), get_fd_value(TB,B,FDB).
119 get_fd_value(couple_left(T),(A,_),FDA) :- get_fd_value(T,A,FDA).
120 get_fd_value(couple_right(T),(_,A),FDA) :- get_fd_value(T,A,FDA).
121 ?get_fd_value(rec_fields(T),rec(Fields),fd_rec(FD)) :- get_field_fd_values(T,Fields,FD).
122 get_fd_value(freeval_case(ID,Case,T),freeval(ID,Case,A),FDA) :-
123 get_fd_value(T,A,FDA).
124 get_fd_value(global(GS),FD,X) :- get_global_type_value(FD,GS,X).
125 get_fd_value(integer,int(X),X).
126
127 get_field_fd_values([],[],[]).
128 get_field_fd_values([field_no_fd(Name)|TTypes],[field(Name,_)|TF],FDVals) :-
129 ? get_field_fd_values(TTypes,TF,FDVals).
130 get_field_fd_values([field(Name,T)|TTypes],[field(Name,Value)|TF],[FDVal|TFD]) :-
131 get_fd_value(T,Value,FDVal),
132 get_field_fd_values(TTypes,TF,TFD).
133
134 :- use_module(library(lists),[maplist/2]).
135 check_valid_fd_type(Type) :-
136 (valid_type(Type) -> true
137 ; add_internal_error('Illegal FD type:',check_valid_fd_type(Type)),fail).
138 valid_type(couple(TA,TB)) :- valid_type(TA),valid_type(TB).
139 valid_type(couple_left(TA)) :- valid_type(TA).
140 valid_type(couple_right(TA)) :- valid_type(TA).
141 valid_type(global(GS)) :- atomic(GS).
142 valid_type(rec_fields(Fs)) :- maplist(valid_rec_type,Fs).
143 valid_type(freeval_case(_,_,TA)) :- valid_type(TA).
144 valid_type(integer).
145
146 valid_rec_type(field(_,T)) :- valid_type(T).
147 valid_rec_type(field_no_fd(_)).
148
149 % -----------------------------------------------------
150
151 % extract from a Set (List) representation a list of FDValues which can be used by the clpfd library
152 try_get_fd_value_list(Set,Type,FDList,GroundList) :-
153 ? try_get_fd_value_list5(Set,Type,FDList,true,GroundList).
154 try_get_fd_value_list5(V,_,_,_,_) :- var(V),!,fail.
155 try_get_fd_value_list5([],_,[],G,G). % Note if this is the base case then the type could be that of an empty list of pairs !
156 try_get_fd_value_list5([H|T],Type,FDList,GroundIn,GroundOut) :-
157 ? try_get_fd_value(Type,H,Value),!, FDH=Value,
158 (GroundIn=true,integer(FDH) -> Ground1=true ; Ground1=false),
159 %%(ground(T) -> true ; print(get_fd_val_list(H,T)),nl), %%
160 check_non_var_list(T), % do not instantiate any free variable ! otherwise we get full domain probably anyway ?
161 ? if(get_fd_value_list5(T,Type,FDT,Ground1,GroundOut,_NoFilterEl,1),
162 FDList = [FDH|FDT],
163 FDList = []). % obtaining FD value triggered inconsistency
164 try_get_fd_value_list5(avl_set(A),_,_,_,_) :-
165 add_internal_error('Not a list: ',try_get_fd_value_list5(avl_set(A),_,_,_,_)),fail.
166 try_get_fd_value_list5(node(A,B,C,D,E),_,_,_,_) :-
167 add_internal_error('Not a list: ',try_get_fd_value_list5(node(A,B,C,D,E),_,_,_,_)),fail.
168 try_get_fd_value_list5(closure(A,B,C),_,_,_,_) :-
169 add_internal_error('Not a list: ',try_get_fd_value_list5(closure(A,B,C),_,_,_,_)),fail.
170
171 check_non_var_list(V) :- var(V),!,fail. %print(check_non_var_failed),nl,fail.
172 check_non_var_list([]).
173 check_non_var_list([H|T]) :- !, nonvar(H), check_non_var_list(T).
174
175 % -----------------------------------------------------
176
177 % remove from a list of FDValues those that belong to objects that definitely cannot match Element
178 % this improves the precision of a later in_fd_value_list_wf call
179 % cannot_match: quick check to see if the (AVL or List) element could match
180
181 :- use_module(kernel_tools,[cannot_match/2, cannot_match_aggressive/2]).
182
183 % gets a (filtered) FD Value list (El used for filtering)
184 get_fd_value_list(Set,Type,FDList,GroundList,El) :-
185 ? get_fd_value_list5(Set,Type,FDList,true,GroundList,El,0).
186 get_fd_value_list5(V,_,_,_,_,_,_) :- var(V),!,fail.
187 get_fd_value_list5([],_,[],G,G,_,_).
188 get_fd_value_list5([H|T],Type,FDValList,GroundIn,GroundOut,El,NrMatch) :-
189 (NrMatch<2 -> cannot_match_aggressive(H,El) % there is still hope of obtaining something deterministic; try more aggressive version; important for test 34; see also test 985 where too much propagation hurts
190 ; cannot_match(H,El)),
191 !, % we can filter this FD Value out
192 ? get_fd_value_list5(T,Type,FDValList,GroundIn,GroundOut,El,NrMatch).
193 get_fd_value_list5([H|T],Type,FDValList,GroundIn,GroundOut,El,NrMatch) :-
194 ? if(get_fd_value(Type,H,FDH),
195 (FDValList = [FDH|FDT], N1 is NrMatch+1,
196 (GroundIn=true,number(FDH) -> Ground1=true ; Ground1=false)),
197 % Failure can happen when a variable gets instantiated (eg. X -> fd(_,t))
198 % and triggers co-routines which fail (happens for SelfGrandpas.als for scope 3)
199 % TO DO: review that this is the only possibility of failure
200 (debug:debug_println(9,get_fd_value_failed(Type,H,_)),
201 check_valid_fd_type(Type),fail)
202 ),
203 ? get_fd_value_list5(T,Type,FDT,Ground1,GroundOut,El,N1).
204
205 % -----------------------------------------------------
206
207 :- use_module(library(clpfd)).
208 :- use_module(clpfd_interface).
209 :- use_module(kernel_waitflags,[get_enumeration_starting_wait_flag/3]).
210
211 in_fd_value_list_wf(Element,List,WF) :-
212 (ground_integer_list(List) -> Gr=true ; Gr=false),
213 in_fd_value_list_wf(Gr,Element,List,WF).
214
215 ground_integer_list(Var) :- var(Var),!,fail.
216 ground_integer_list([]).
217 ground_integer_list([H|T]) :- integer(H), ground_integer_list(T).
218
219 % assert that an FD Value is in a list of FD Values
220 % in_fd_value_list(ListIsGroundOfSimpleIntegers, FDElement, ListOfFDValues)
221 in_fd_value_list_wf(true,Element,List,_WF) :-
222 List \= [], % avoid calling in case Element is compound (as try_get_fd_value_list will return ground for empty list)
223 ? clpfd_inlist(Element,List). % element should not be a compound like fd_pair!
224 in_fd_value_list_wf(false,Element,List,WF) :-
225 %get_enumeration_finished_wait_flag(WF,EWF),
226 get_enumeration_starting_wait_flag(in_fd_value_list_wf,WF,EWF), % relevant for test 2070 to avoid enum warning
227 ? in_non_ground_fd_value_lists(Element,List,EWF,top).
228
229 in_non_ground_fd_value_lists(FDP,List,EWF,_) :- nonvar(FDP), FDP = fd_pair(A,B), !,
230 split_fd_pair_list(List,ListA,ListB),
231 ? in_non_ground_fd_value_lists(A,ListA,EWF,left),
232 ? in_non_ground_fd_value_lists(B,ListB,EWF,right).
233 in_non_ground_fd_value_lists(FDR,List,EWF,_) :- nonvar(FDR), FDR = fd_rec(Fields), !,
234 %print(treat_fd_rec_fields(Fields,List)),nl,
235 treat_fd_rec_fields(Fields,List,EWF).
236 in_non_ground_fd_value_lists(Element,List,_EWF,Path) :-
237 Path \= top, % otherwise we know List not to be ground
238 ground(List), %ground_number_list(List,Min,Max,Len), List can be a large term
239 !,
240 ? clpfd_interface:clpfd_inlist(Element,List). % this is considerably faster; especially in compiled version (see SLOT-PERFORMANCE2)
241 % TODO: use force_clpfd_inlist ? clpfd_inlist uses try_post_constraint
242 in_non_ground_fd_value_lists(Element,List,_EWF,_Path) :-
243 tools:exact_member(Element,List), % avoid generating choice point for element position below
244 !.
245 in_non_ground_fd_value_lists(Element,List,EWF,_Path) :-
246 % asserts that Element is the _Nr-th element of List; in contrast to clpfd_inlist List do not have to be numbers
247 post_clpfd_element(List,Element,EWF).
248
249
250 % we cannot use new element/2 constraint in SICStus 4.7.0, because this will not enumerate the Idx before infinite enumeration starts (cf test 2070)
251 post_clpfd_element(List,Element,EWF) :-
252 catch_clpfd_overflow_call3(
253 (element(Nr,List,Element), Posted=true),
254 silent,
255 Posted=false), % List or Element contain an integer which is too large for CLPFD
256 (Posted=true -> label_el_nr(Nr,Element,List,EWF)
257 ; true). % we do not propagate; callers must not rely on this constraint as full membership check in this case
258
259
260
261 /* using this + Element in Min..Max
262 does not seem to buy anything/ a lot to comparing calling clpfd_inlist for large lists
263 ground_number_list([],0,0,0).
264 ground_number_list([H|T],Min,Max,Len) :- number(H),
265 ground_number_list_acc(T,H,H,1,Min,Max,Len).
266 ground_number_list_acc([],Min,Max,Len,Min,Max,Len).
267 ground_number_list_acc([H|T],Min0,Max0,Len0,Min,Max,Len) :- number(H),
268 (H<Min0 -> Min1 = H ; Min1 = Min0),
269 (H>Max0 -> Max1 = H ; Max1 = Max0),
270 L1 is Len0+1,
271 ground_number_list_acc(T,Min1,Max1,L1,Min,Max,Len).
272 */
273
274 treat_fd_rec_fields([],_,_).
275 treat_fd_rec_fields([V|TF],List,EWF) :-
276 get_next_fd_rec_field(List,FieldFDList,RemainingList),
277 %print(check(V,FieldFDList)),nl,
278 in_non_ground_fd_value_lists(V,FieldFDList,EWF,rec),
279 treat_fd_rec_fields(TF,RemainingList,EWF).
280
281 get_next_fd_rec_field([],FieldList,RemList) :- !, FieldList=[], RemList=[].
282 get_next_fd_rec_field([fd_rec([FDV|Rem]) | TList], [FDV|TFieldList], [ fd_rec(Rem) | TRemList]) :- !,
283 get_next_fd_rec_field(TList, TFieldList, TRemList).
284 get_next_fd_rec_field(List,FieldList,RemainingList) :-
285 add_internal_error('Illegal call: ',get_next_fd_rec_field(List,FieldList,RemainingList)),
286 FieldList=[], RemainingList=[].
287
288 % label ElementNr of element/3 clpfd constraint if necessary
289 % Note: ElementNr is a local variable not used outside of in_non_ground_fd_value_lists
290 :- block label_el_nr(?,-,?,?), label_el_nr(-,?,?,-).
291 label_el_nr(ElementNr,Element,List,_) :-
292 (number(ElementNr) -> true % clpfd:element already has a solution
293 ; exact_member_nr(Element,List,1,Nr) -> ElementNr=Nr
294 ; \+ fd_var(ElementNr) -> print(already_enumerating(Element,ElementNr)),nl
295 % probably co-routine got activated while enumerating Element and clpfd has not yet set ElementNr,
296 % but clpfd has removed the fd domain
297 ; ground(List) -> (labeling([],[ElementNr]) -> true) % avoid pending co-routines in case the Element appears multiple times in the list by picking an arbitrary solution !)
298 ; %translate:print_clpfd_variable(ElementNr),nl,
299 labeling([],[ElementNr]) % enumerate ElementNr -> before infinite enumeration we chose possible values
300 % we cannot use cut after labeling, labeling ElementNr can instantiate List
301 % example where this is needed, {r|3 : {r,r+1} } in CLPFD=TRUE mode
302 ).
303
304 exact_member_nr(X,[Y|_],Acc,R) :- X==Y, !, Acc=R.
305 exact_member_nr(X,[_|T],Acc,R) :- A1 is Acc+1, exact_member_nr(X,T,A1,R).
306
307 split_fd_pair_list([],[],[]).
308 split_fd_pair_list([fd_pair(A,B)|T],[A|TA],[B|TB]) :- split_fd_pair_list(T,TA,TB).
309
310
311 % -----------------------------------------------------
312
313 :- use_module(kernel_objects,[equal_object/2]).
314
315 % a lazy, blocking version which waits for Set to become sufficiently instantiated and then
316 % calls in_fd_value_list_wf
317 % Trys to transform E:Set into calls to clpfd library: ;
318 % When FullyChecked becomes true, then there is no need to perform other processing (apart from labeling/enumeration of the involved values)
319 % When FullyChecked becomes false
320 :- assert_must_succeed((kernel_waitflags:init_wait_flags(WF,[unit_test]),
321 clpfd_lists:lazy_fd_value_check([int(2),int(3)],int(X),WF,FullyChecked),
322 (preferences:preference(use_clpfd_solver,true) -> FullyChecked==true ; true),
323 X=3, kernel_waitflags:ground_wait_flags(WF))).
324 :- assert_must_fail((kernel_waitflags:init_wait_flags(WF,[unit_test]),
325 clpfd_lists:lazy_fd_value_check([int(2),int(3)],int(4),WF,FullyChecked),
326 (preferences:preference(use_clpfd_solver,true) -> true ; FullyChecked==false -> fail ; true))).
327 % integers which are too large for CLPFD must not raise an overflow and must not be treated as fully checked:
328 :- assert_must_succeed_any((kernel_waitflags:init_wait_flags(WF,[unit_test]), clpfd:in(Y,1..2),
329 clpfd_lists:lazy_fd_value_check([int(Y),int(1152921504606846977)],int(_),WF,_))).
330 :- assert_must_succeed_any((kernel_waitflags:init_wait_flags(WF,[unit_test]),
331 clpfd_lists:lazy_fd_value_check([int(_),int(_)],int(1152921504606846977),WF,FC), FC==false)).
332 :- assert_must_succeed_any((kernel_waitflags:init_wait_flags(WF,[unit_test]),
333 clpfd_lists:in_fd_value_list_wf(false,_,[_,1152921504606846977],WF))).
334 lazy_fd_value_check(Set,E,_,FC) :- nonvar(Set), Set=[H|T], T==[],
335 !, % we have a one-element singleton set
336 %tools_printing:print_term_summary(equal_object(H,E)),nl,
337 ? equal_object(H,E),
338 FC=true.
339 lazy_fd_value_check(Set,E,WF,FullyChecked) :- preferences:preference(use_clpfd_solver,true),!,
340 ? lazy_in_list_propagation_block(Set,E,WF,FullyChecked).
341 lazy_fd_value_check(_Set,_E,_WF,false).
342
343 :- block lazy_in_list_propagation_block(-,?,?,?).
344 lazy_in_list_propagation_block([],_,_,_) :- fail. % nothing can be element of empty set
345 lazy_in_list_propagation_block([H|T],E,WF,FullyChecked) :- contains_fd_element(H,Res,WF),
346 ? lazy_in_list_propagation_block_2(Res,H,T,E,WF,FullyChecked).
347
348 :- block lazy_in_list_propagation_block_2(-,?,?,?,?,?).
349 lazy_in_list_propagation_block_2(pred_false,_H,_T,_E,_WF,false). % nothing to propagate
350 lazy_in_list_propagation_block_2(pred_true,H,T,E,WF,FullyChecked) :- % print(bound_h(H)),nl,
351 bound_list(T,Bound),
352 ? lazy_in_list_propagation_block_3(Bound,H,T,E,WF,FullyChecked).
353
354 :- block lazy_in_list_propagation_block_3(-,?,?,?,?,?).
355 lazy_in_list_propagation_block_3(pred_false,_H,_T,_E,_WF,FC) :- !, FC=false. % nothing to propagate
356 lazy_in_list_propagation_block_3(pred_true,H,T,E,WF,FullyChecked) :-
357 ? try_get_fd_value_type(E,H,Type), % print(fd_val_type(E,H,Type)),nl,
358 ? get_fd_value_list([H|T],Type,FDVals,GroundList,E), % try and extract values which can be used for CLPFD
359 ? get_fd_value(Type,E,EFD),!,
360 FDVals \= [], % if we have the empty list then membership fails
361 (complete_type(Type),
362 \+ integer_too_large_for_fd_set(EFD) % in this case the CLPFD constraints below cannot be posted (overflow)
363 -> FullyChecked = true ; FullyChecked = false),
364 % print(lazy_in_element(FullyChecked,GroundList,E,[H|T],WF)),nl,
365 % print(in_fd_value_list_wf(GroundList,EFD,FDVals,WF,FullyChecked)),nl,
366 ? in_fd_value_list_wf(GroundList,EFD,FDVals,WF).
367 lazy_in_list_propagation_block_3(pred_true,_H,_T,_E,_WF,FullyChecked) :- !,
368 % should only happen for freeval with no fd type inside or for freevalues if element is not instantiated (we don't know which freval case we have)
369 FullyChecked=false.
370 lazy_in_list_propagation_block_3(P,H,T,E,WF,false) :-
371 add_internal_error('Illegal call: ',lazy_in_list_propagation_block_3(P,H,T,E,WF,false)).
372
373 :- block contains_fd_element(-,?,?).
374 contains_fd_element(int(_),R,_WF) :- !, R=pred_true.
375 contains_fd_element(fd(_,_),R,_WF) :- !, R=pred_true.
376 contains_fd_element((A,B),R,WF) :- !,contains_fd_element(A,RA,WF), contains_fd_element(B,RB,WF),
377 b_interpreter_check:disjoin(RA,RB,R,priority(16384),priority(16384),WF). % TO DO: examine priority to be used
378 contains_fd_element(rec(Fields),R,WF) :- !, contains_field_fd_el(Fields,R,WF).
379 contains_fd_element(freeval(_ID,_Case,_Val),R,_WF) :- !,
380 % We assume that a freetype can contain FD elements; TO DO: really check !
381 % ID is something of the form 'List'(integer) ; we use get_freeval_type(ID,_Case) from kernel_freetypes to get subtypes
382 % we could look at the value; but we could be unlucky in that this particular Case has no fd subtype but others do
383 % anyway: try_get_fd_value_type will fail if there is no fd type
384 R=pred_true.
385 contains_fd_element(_,pred_false,_).
386
387 :- block contains_field_fd_el(-,?,?).
388 contains_field_fd_el([],R,_WF) :- !, R=pred_false.
389 contains_field_fd_el([field(_,V)|_],R,WF) :- contains_fd_element(V,R,WF). % TO DO: look at other fields
390 % deal with freetypes: freeval(ID,Case,Value); but here we need a different scheme: we need to find the case of the element and only look at elements in the list with the same constructor
391 % we could also deal with set types {x,y} : { {1,2}, {2,3} } --> x/y : 1..3
392 % TO DO: maybe filter out non-matching elements also here (see filter_non_matching_elements for quick_propagate)
393
394 % set output variable as soon as we can determine if we have a bound list
395 :- block bound_list(-,?).
396 bound_list(List,BoundList) :-
397 (List = [] -> BoundList=pred_true
398 ? ; List = [_|T] -> bound_list(T,BoundList)
399 ; BoundList=pred_false). % we have avl_set or global_set or closure; TO DO: also allow avl_set
400
401
402 % -----------------------------------------------------
403
404 % generate an in_fd_value_list_wf call for checking that E is a member of an AVL set encoding
405 :- use_module(library(avl),[avl_domain/2]).
406 avl_fd_value_check(AVL,E,WF,FullyChecked) :-
407 AVL = node(TopEl,_True,_,_,_),!,
408 contains_fd_element(TopEl,Res,WF),
409 (Res==pred_true
410 -> avl_domain(AVL,DomainList),
411 DomainList = [H|T],
412 ? lazy_in_list_propagation_block_3(pred_true,H,T,E,WF,FullyChecked)
413 ; FullyChecked=false).
414 avl_fd_value_check(AVL,E,WF,FullyChecked) :-
415 add_internal_error('Illegal call: ',avl_fd_value_check(AVL,E,WF,FullyChecked)),
416 FullyChecked=false.
417
418
419
420
421 % ------------------------------------------------------------------------
422
423 % alternative utilities for collecting fd_set information, combine it and then use it
424
425 % FD-SET
426
427 % succeeds if we can obtain FD information for a B data value
428 :- assert_must_succeed((clpfd_lists:get_fdset_information(_,I), I==no_fdset_info)).
429 :- assert_must_succeed((clpfd_lists:get_fdset_information(int(3),I), I=int_fdset(F), clpfd:fdset_singleton(F,3))).
430 % note: we do not compare with a concrete FD set term, as SWI uses another representation than SICStus
431 :- assert_must_succeed((clpfd_lists:get_fdset_information(int(_),I), I=int_fdset(F), clpfd:fdset_interval(F,inf,sup))).
432 :- assert_must_succeed((clpfd_lists:get_fdset_information(pred_true,I), I==single_value(pred_true))).
433 % integers which are too large for CLPFD must not raise an overflow:
434 :- assert_must_succeed((clpfd_lists:get_fdset_information(int(1152921504606846977),I), I==no_fdset_info)).
435 :- assert_must_succeed((clpfd_lists:get_fdset_information(int(-1152921504606846978),I), I==no_fdset_info)).
436 :- assert_must_succeed((clpfd_lists:get_finite_fdset_information(int(3),I), I=int_fdset(F), clpfd:fdset_singleton(F,3))).
437 :- assert_must_succeed_any((clpfd:in(X,1..3),clpfd_lists:get_finite_fdset_information(int(X),I), I=int_fdset(F), clpfd:fdset_interval(F,1,3))).
438 :- assert_must_fail(clpfd_lists:get_finite_fdset_information(int(_),_)).
439 :- assert_must_fail(clpfd_lists:get_finite_fdset_information(_,_)).
440 :- assert_must_fail(clpfd_lists:get_finite_fdset_information(int(1152921504606846977),_)).
441 :- assert_must_fail(clpfd_lists:get_finite_fdset_information(int(-1152921504606846978),_)).
442
443 :- use_module(tools_platform, [is_tagged_integer/1]).
444 % check if a number is too large for CLPFD; fd_set/2 raises a representation error for such numbers
445 integer_too_large_for_fd_set(V) :- integer(V), \+ is_tagged_integer(V).
446
447 get_fdset_information(V,Info) :- var(V),!,Info=no_fdset_info.
448 get_fdset_information(int(V),Info) :- integer_too_large_for_fd_set(V),!, Info=no_fdset_info.
449 get_fdset_information(int(V),int_fdset(FDSET)) :- fd_set(V,FDSET).
450 get_fdset_information(fd(V,GS),fd_fdset(FDSET,GS)) :- fd_set(V,FDSET).
451 get_fdset_information(pred_true,single_value(pred_true)).
452 get_fdset_information(pred_false,single_value(pred_false)).
453 get_fdset_information(string(S),single_value(string(S))) :- ground(S).
454 % TO DO: add more: couples, records, ...
455
456 get_finite_fdset_information(V,_Info) :- var(V),!,fail.
457 get_finite_fdset_information(int(V),int_fdset(FDSET)) :- finite_fd_set(V,FDSET).
458 get_finite_fdset_information(fd(V,GS),fd_fdset(FDSET,GS)) :- finite_fd_set(V,FDSET).
459 get_finite_fdset_information(pred_true,single_value(pred_true)).
460 get_finite_fdset_information(pred_false,single_value(pred_false)).
461 get_finite_fdset_information(string(S),single_value(string(S))) :- ground(S).
462
463 finite_fd_set(V,_) :- integer_too_large_for_fd_set(V),!,fail. % we cannot represent the value as fd_set; treat like no finite info
464 finite_fd_set(V,FDSET) :- fd_set(V,FDSET), fdset_size(FDSET,N), number(N). %FDSET \= [[inf|sup]].
465
466 % will combine (union) two FD infos; have to be of compatible type
467 combine_fdset_information(no_fdset_info,I,R) :- !, R=I.
468 combine_fdset_information(I,no_fdset_info,R) :- !, R=I.
469 combine_fdset_information(int_fdset(A),int_fdset(B),int_fdset(C)) :- !,
470 %print(un(A,B,C)),nl,
471 fdset_union(A,B,C).
472 combine_fdset_information(fd_fdset(A,GS),fd_fdset(B,GS),fd_fdset(C,GS)) :- !,
473 fdset_union(A,B,C).
474 combine_fdset_information(single_value(V),single_value(W),R) :- !,
475 (V==W -> R=single_value(V) ; R=no_fdset_info).
476 combine_fdset_information(A,B,C) :-
477 add_internal_error('Illegal FD info: ',combine_fdset_information(A,B,C)),fail.
478
479 % assert that a variable/value matches collected FD information
480 assert_fdset_information(int_fdset(FDSet),X) :- !,
481 (ground(X) -> true ; X=int(XX),in_set(XX,FDSet)).
482 assert_fdset_information(fd_fdset(FDSet,GS),X) :- !,
483 (ground(X) -> true ; X=fd(XX,GS),in_set(XX,FDSet)).
484 assert_fdset_information(single_value(V),X) :- !, equal_object(V,X).
485 assert_fdset_information(_,_).
486
487
488