1 % (c) 2025-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(bounds_analysis,[infer_bounds/3, infer_bounds/4]).
6 :- use_module(probsrc(module_information),[module_info/2]).
7 :- module_info(group,b2asp).
8 :- module_info(description,'Perform bounds analysis on predicates for integer values.').
9
10 % we use CLP(FD) to implement the bounds propagation
11 % bint(FDVAR) : the possible values of an integer
12 % binterval(FDVAR1,FDVAR2,NonEmptyVar) :
13 % all values of the set must lie within FDVAR1 and FDVAR2
14 % NonEmptyVar is a reification of NonEmptyVar #<=> FDVAR1 #=< FDVAR2
15
16
17 % bound_id_info(ID,Type,Bounds) : information provided to the outside users of the module
18 % bound_internal_info(ID,Type,InternalBoundsRepresentation) : internal bounds info and representation
19
20 :- use_module(probsrc(error_manager)).
21 :- use_module(probsrc(debug),[debug_format/3, debug_mode/1]).
22 :- use_module(library(clpfd)).
23 :- use_module(library(lists)).
24 :- use_module(probsrc(bsyntaxtree),[definitely_not_empty_set/1, create_cartesian_product/3]).
25 :- use_module(clingo_interface,[get_string_nr/2]).
26
27 infer_bounds(Paras,Pred,Res) :- infer_bounds(Paras,Pred,[],Res).
28
29 % infer bounds for quantified typed ids inside predicate Pred
30 % valid options are labeling: which forces a CLP(FD) labeling of the bounds variables to check for consistency
31 infer_bounds(Paras,Pred,Options,_Res) :-
32 new_env(Env,Options),
33 debug_format(19,'Inferring bounds for: ~w~n',[Paras]),
34 bb_put(infer_bounds_result,contradiction_found),
35 (add_typed_ids(Paras,LocalBoundsInfo,BAR,Env,Env2),
36 (BAR==bounds_analysis_required -> infer_pred_bounds(Pred,Env2) ; debug_format(19,'No Bounds Analysis required',[]))
37 -> (debug_mode(on) -> portray_env(Env2) ; true),
38 (label_env(Env2,Options)
39 -> bb_put(infer_bounds_result,LocalBoundsInfo)
40 ; format(user_output,'No consistent labelled solution exists, predicate unsatisfiable~n',[])
41 )
42 ; format(user_output,'No consistent solution exists, predicate unsatisfiable~n',[])
43 ),
44 fail. % to avoid pending co-routines / CLPFD variables we fail and recover the result with bb_get:
45 infer_bounds(_,_,_,Res) :- bb_get(infer_bounds_result,Res).
46
47
48 infer_pred_bounds(b(Pred,pred,_Infos),Env) :- !,
49 % format(user_output,' pred --> ~w~n',[Pred]),
50 infer_pred_bounds(Pred,Env).
51 infer_pred_bounds(conjunct(A,B),Env) :- !,
52 infer_pred_bounds(A,Env),
53 infer_pred_bounds(B,Env).
54 % TODO: disjunct: copy env, and then perform LUB
55 infer_pred_bounds(truth,_) :- !.
56 infer_pred_bounds(SubsetAB,Env) :- is_subset(SubsetAB,A,B,EmptyA,EmptyB),
57 infer_set_bounds(A,Env,SetBoundsA),
58 infer_set_bounds(B,Env,SetBoundsB), !,
59 force_non_empty(EmptyA,SetBoundsA),
60 force_non_empty(EmptyB,SetBoundsB),
61 subset_bounds(SetBoundsA,SetBoundsB).
62 infer_pred_bounds(member(A,B),Env) :-
63 infer_scalar_bounds(A,Env,SetBoundsA),
64 infer_set_bounds(B,Env,SetBoundsB), !,
65 mem_bounds(SetBoundsA,SetBoundsB).
66 infer_pred_bounds(equal(A,B),Env) :- is_scalar(A),
67 infer_scalar_bounds(A,Env,ScBoundsA),
68 infer_scalar_bounds(B,Env,ScBoundsB), !,
69 eq_bounds(ScBoundsA,ScBoundsB).
70 infer_pred_bounds(equal(A,B),Env) :- is_set(A),
71 infer_set_bounds(A,Env,SetBoundsA),
72 infer_set_bounds(B,Env,SetBoundsB), !,
73 eq_bounds(SetBoundsA,SetBoundsB).
74 infer_pred_bounds(BOP,Env) :-
75 scalar_binary_pred(BOP,A,B,ClpfdOp),
76 infer_scalar_bounds(A,Env,BoundsA),
77 infer_scalar_bounds(B,Env,BoundsB), !,
78 apply_binary_pred(ClpfdOp,BoundsA,BoundsB).
79 infer_pred_bounds(Uncov,_Env) :- functor(Uncov,F,N),write(user_output,uncovered_pred(F/N)), nl(user_output).
80
81 is_scalar(A) :- get_texpr_type(A,integer).
82 is_scalar(A) :- get_texpr_type(A,string).
83 is_scalar(A) :- get_texpr_type(A,couple(_,_)).
84 is_set(A) :- get_texpr_type(A,TA), is_set_type(TA,_).
85
86 %% true if the bounds of sub-expressions cannot be larger than resulting bound
87 %is_monotonic(b(E,_,_)) :- is_monotonic2(E).
88 %is_monotonic2(interval(_,_)).
89 %is_monotonic2(value(_)).
90 %is_monotonic2(empty_set).
91 %is_monotonic2(union(_,_)).
92 %is_monotonic2(identifier(_)).
93 %is_monotonic2(set_extension(_)). % TODO: check
94 %is_monotonic2(cartesian_product(A,B)) :- is_monotonic(A), is_monotonic(B).
95 %is_monotonic2(image(A,B)) :- is_monotonic(A), is_monotonic(B).
96 %is_monotonic2(union(A,B)) :- is_monotonic(A), is_monotonic(B).
97 %is_monotonic2(iteration(A,_)) :- is_monotonic(A).
98 %is_monotonic2(closure(A)) :- is_monotonic(A).
99 %is_monotonic2(reverse(A)) :- is_monotonic(A).
100 %is_monotonic2(intersection(A,B)) :- pure_value(A), pure_value(B).
101 %is_monotonic2(set_subtraction(A,B)) :- pure_value(A), pure_value(B).
102 %%is_monotonic2(NM) :- write(user_output,non_mon(NM)),nl(user_output),fail.
103 %
104 %% value which does not contain an identifier (which can be instantiated somewhere else)
105 %pure_value(b(E,_,_)) :- pure_value2(E).
106 %pure_value2(interval(_,_)).
107 %pure_value2(value(_)).
108 %pure_value2(empty_set).
109 %pure_value2(cartesian_product(A,B)) :- pure_value(A), pure_value(B).
110 %pure_value2(image(A,B)) :- pure_value(A), pure_value(B).
111 %pure_value2(intersection(A,B)) :- pure_value(A), pure_value(B).
112 %pure_value2(set_subtraction(A,B)) :- pure_value(A), pure_value(B).
113 %pure_value2(union(A,B)) :- pure_value(A), pure_value(B).
114
115 % check if we have a predicate that we should treat like subset
116 is_subset(subset(A,B),A,B, can_be_empty,can_be_empty).
117 is_subset(subset_strict(A,B),A,B,can_be_empty,non_empty).
118 is_subset(member(A,PB),A,B,EmptyA,EmptyB) :- is_pow(PB,B,EmptyA,EmptyB).
119 is_subset(member(A,RelFun),A,Cart,can_be_empty,can_be_empty) :- is_rel_fun(RelFun,Dom,Ran),
120 create_cartesian_product(Dom,Ran,Cart).
121
122 is_rel_fun(b(P,_,_),Dom,Ran) :- is_rel_fun(P,Dom,Ran).
123 is_rel_fun(relations(A,B),A,B).
124 is_rel_fun(total_relation(A,B),A,B).
125 is_rel_fun(total_surjection_relation(A,B),A,B).
126 is_rel_fun(surjection_relation(A,B),A,B).
127 is_rel_fun(partial_function(A,B),A,B).
128 is_rel_fun(partial_injection(A,B),A,B).
129 is_rel_fun(partial_surjection(A,B),A,B).
130 is_rel_fun(partial_bijection(A,B),A,B).
131 is_rel_fun(total_function(A,B),A,B).
132 is_rel_fun(total_injection(A,B),A,B).
133 is_rel_fun(total_surjection(A,B),A,B).
134 is_rel_fun(total_bijection(A,B),A,B).
135 is_rel_fun(perm(B),A,B) :- iset(A,'NATURAL1').
136 is_rel_fun(seq(B),A,B) :- iset(A,'NATURAL1').
137 is_rel_fun(iseq(B),A,B) :- iset(A,'NATURAL1').
138 is_rel_fun(seq1(B),A,B) :- iset(A,'NATURAL1').
139 is_rel_fun(iseq1(B),A,B) :- iset(A,'NATURAL1').
140
141 iset(b(integer_set(SET),set(integer),[]),SET).
142
143 is_pow(b(P,_,_),B,EmptyA,EmptyB) :- is_pow(P,B,EmptyA,EmptyB).
144 is_pow(pow_subset(B),B, can_be_empty,can_be_empty).
145 is_pow(pow1_subset(B),B,non_empty,non_empty).
146 is_pow(fin_subset(B),B, can_be_empty,can_be_empty).
147 is_pow(fin1_subset(B),B,non_empty,non_empty).
148
149 force_non_empty(non_empty,binterval(_,_,NonEmpty)) :- !, NonEmpty=1.
150 force_non_empty(_,_).
151
152 scalar_binary_pred(less(A,B),A,B,'#<').
153 scalar_binary_pred(greater(A,B),A,B,'#>').
154 scalar_binary_pred(less_equal(A,B),A,B,'#=<').
155 scalar_binary_pred(greater_equal(A,B),A,B,'#>=').
156
157 apply_binary_pred(Pred,bint(A),bint(B)) :- !,
158 if(call(Pred,A,B),true, % TODO: catch overflows
159 (format(user_output,'Inconsistent ~w ~w ~w constraint!~n',[A,Pred,B]),fail)).
160 apply_binary_pred(ClpfdOp,BoundsA,BoundsB) :-
161 add_internal_error('Illegal call: ',apply_binary_pred(ClpfdOp,BoundsA,BoundsB)), fail.
162
163
164 mem_bounds(BoundsInfo,SetBounds) :- debug_format(19,'member bounds ~w ~w~n',[BoundsInfo,SetBounds]),
165 if(mem_bounds2(BoundsInfo,SetBounds),true,
166 (format(user_output,'Inconsistent ~w : ~w constraint!~n',[BoundsInfo,SetBounds]),fail)).
167 mem_bounds2(bint(X),binterval(A,B,NonEmpty)) :- !,
168 NonEmpty = 1, % set must be non-empty to contain an element
169 (number(A),number(B) -> X in A..B ; X #>=A #/\ X #=< B).
170 mem_bounds2(bcouple(X,Y),bcart(BoundsA,BoundsB,NonEmpty)) :- !,
171 NonEmpty = 1,
172 mem_bounds2(X,BoundsA), mem_bounds2(Y,BoundsB).
173 mem_bounds2(A,B) :- format(user_output,'Uncovered mem_bounds ~w : ~w~n',[A,B]).
174
175 %:- block subset_list(-,?).
176 %subset_list([],_).
177 %subset_list([H|T],List2) :- member(H,List2), !, % will instantiate List2 if necessary
178 % subset_list(T,List2).
179
180 % equal_bounds(BoundsInfo1,BoundsInfo2)
181 % TODO: check the logic and maybe have another precision flag in the bounds-info rather than this monotonic flag
182 eq_bounds(BoundsInfo,BoundsInfo2) :-
183 debug_format(19,'eq bounds: ~w = ~w~n',[BoundsInfo,BoundsInfo2]),
184 if(eq_bounds2(BoundsInfo,BoundsInfo2),true,
185 (format(user_output,'Inconsistent ~w = ~w constraint!~n',[BoundsInfo,BoundsInfo2]),fail)).
186 eq_bounds2(binterval(A,B,NonEmpty),binterval(A2,B2,NonEmpty2)) :- !,
187 NonEmpty=NonEmpty2, % true for non-monotonic ??
188 eq_interval(NonEmpty,A,B,A2,B2).
189 eq_bounds2(bint(A),bint(B)) :- !, A=B.
190 eq_bounds2(bcouple(A1,A2),bcouple(B1,B2)) :- !, eq_bounds2(A1,B1), eq_bounds2(A2,B2).
191 eq_bounds2(bcart(A1,A2,NonEmptyA),bcart(B1,B2,NonEmptyB)) :- !,
192 NonEmptyA=NonEmptyB, % should we do this also for non-monotonic ?
193 eq_cart(NonEmptyA,A1,B1,A2,B2).
194 eq_bounds2(_,_).
195
196 :- block eq_interval(-,?,?,?,?).
197 eq_interval(0,_,_,_,_). % both empty
198 eq_interval(1,Low1,Up1,Low2,Up2) :-
199 (Low1,Up1) = (Low2,Up2).
200 % Note: in case of x /\ 1..3 = {2} --> we could have a solution of x={2,4}
201 % this is now dealt with in intersection
202 :- block eq_cart(-,?,?,?,?).
203 %eq_cart(NE,A1,B1,A2,B2) :- write(user_output,eq_cart(NE,A1,B1,A2,B2)),nl(user_output),fail.
204 eq_cart(0, _,_,_,_). % both cartesian products empty
205 eq_cart(1, A1,B1,A2,B2) :- eq_bounds2(A1,B1), eq_bounds2(A2,B2).
206 %:- block eq_list(-,?,?).
207 %eq_list(0,_,_). % both cartesian products empty
208 %eq_list(1,A,B) :- subset_list(A,B), subset_list(B,A).
209
210 subset_bounds(BoundsInfo,SetBounds) :- %debug_format(9,'subset_bounds ~w ~w~n',[BoundsInfo,SetBounds]),
211 if(subset_bounds2(BoundsInfo,SetBounds),true,
212 (format(user_output,'Inconsistent ~w <: ~w constraint!~n',[BoundsInfo,SetBounds]),fail)).
213 subset_bounds2(binterval(X,Y,NonEmptyXY),binterval(A,B,NonEmptyAB)) :- !,
214 NonEmptyXY #=< NonEmptyAB, % if RHS A..B is empty then so is LHS X..Y
215 subset_interval(X,Y,NonEmptyXY,A,B,NonEmptyAB).
216 subset_bounds2(bcart(X,Y,NonEmptyXY),bcart(A,B,NonEmptyAB)) :- !,
217 NonEmptyXY #=< NonEmptyAB, % if RHS is empty then so is LHS
218 subset_cart(X,Y,NonEmptyXY,A,B,NonEmptyAB).
219 subset_bounds2(A,B) :- format(user_output,'Uncovered subset_bounds2 ~w : ~w~n',[A,B]).
220
221 :- block subset_interval(?,?,-,?,?,?).
222 subset_interval(_,_,0,_,_,_). % first set empty
223 subset_interval(X,Y,1,A,B,1) :- (X#>= A #/\ Y #=< B).
224
225 :- block subset_cart(?,?,-,?,?,?).
226 subset_cart(_,_,0,_,_,_). % first set empty
227 subset_cart(X,Y,1,A,B,1) :- subset_bounds2(X,A), subset_bounds2(Y,B).
228
229 % --------
230 % SETS
231 % --------
232
233 :- use_module(library(avl),[avl_min/2, avl_max/2, avl_member/2]).
234
235 infer_set_bounds(b(E,Type,_Infos),Env,Bounds) :- !,
236 (finite_type(Type) -> Bounds = Type % we could try and infer bounds for fd(_,_) global set values
237 ; infer_set_bounds(E,Type,Env,Bounds)).
238 infer_set_bounds(empty_set,set(integer),_,Bounds) :- !, Bounds = binterval(1,0,0).
239 infer_set_bounds(integer_set('NATURAL'),set(integer),_,Bounds) :- !, init_binterval(0,_,Bounds,1).
240 infer_set_bounds(integer_set('NATURAL1'),set(integer),_,Bounds) :- !, init_binterval(1,_,Bounds,1).
241 infer_set_bounds(value(AVL),set(Type),Env,Bounds) :- nonvar(AVL), AVL=avl_set(A), !,
242 infer_avl_set_bounds(Type,A,Env,Bounds).
243 infer_set_bounds(value(CS),set(integer),_Env,Bounds) :- nonvar(CS), is_interval_closure(CS,Low,Up),
244 number(Low), number(Up), !, init_binterval(Low,Up,Bounds,_).
245 % TODO: maybe cartesian product closure ?
246 infer_set_bounds(interval(A,B),_,Env,Bounds) :- !,
247 infer_scalar_bounds(A,Env,bint(BA)),
248 infer_scalar_bounds(B,Env,bint(BB)),
249 init_binterval(BA,BB,Bounds,_).
250 infer_set_bounds(intersection(A,B),_,Env,Bounds) :- !,
251 % Note: we assume that Bounds of result are unconstrained; and will only be constrained later by eq_bounds
252 infer_set_bounds(A,Env,BoundsA),
253 infer_set_bounds(B,Env,BoundsB), % TODO: treat if one of the two calls fails
254 %tools_printing:print_term_summary_user_output(inter1(BoundsA,BoundsB,Bounds)),nl,
255 intersect_bounds(BoundsA,BoundsB,Bounds).
256 infer_set_bounds(union(A,B),_,Env,Bounds) :- !,
257 % Note: we assume that Bounds of result are unconstrained; and will only be constrained later by eq_bounds
258 infer_set_bounds(A,Env,BoundsA),
259 infer_set_bounds(B,Env,BoundsB),
260 %tools_printing:print_term_summary_user_output(union1(BoundsA,BoundsB,Bounds)),nl,
261 union_bounds(BoundsA,BoundsB,Bounds).
262 infer_set_bounds(set_subtraction(A,B),_,Env,Bounds) :- !,
263 % Note: we assume that Bounds of result are unconstrained; and will only be constrained later by eq_bounds
264 infer_set_bounds(A,Env,BoundsA),
265 infer_set_bounds(B,Env,BoundsB), % TODO: treat if one of the two calls fails
266 %tools_printing:print_term_summary_user_output(inter1(BoundsA,BoundsB,Bounds)),nl,
267 set_subtract_bounds(BoundsA,BoundsB,Bounds).
268 infer_set_bounds(set_extension(List),_,Env,Bounds) :- !,
269 (List = [A], infer_scalar_bounds(A,Env,bint(BA))
270 -> Bounds = binterval(BA,BA,1)
271 ; maplist(infer_set_ext_el(Env,Bounds),List)
272 % TODO: use union code instead? we loose info that these are all the elements of the set
273 ).
274 infer_set_bounds(identifier(A),Type,Env,Bounds) :- !,
275 lookup_id_bounds(A,Env,Type,Bounds).
276 infer_set_bounds(cartesian_product(A,B),_,Env,Bounds) :- !,
277 infer_set_bounds(A,Env,BoundsA),
278 infer_set_bounds(B,Env,BoundsB),
279 construct_bcart(A,B,BoundsA,BoundsB,Bounds).
280 infer_set_bounds(domain(A),_,Env,DomBounds) :- !,
281 infer_set_bounds(A,Env,bcart(DomBounds,_,NE)),
282 imply_non_empty(DomBounds,NE). % if domain is non-empty, then full relation must be non-empty
283 infer_set_bounds(range(A),_,Env,RanBounds) :- !,
284 infer_set_bounds(A,Env,bcart(_,RanBounds,NE)),
285 imply_non_empty(RanBounds,NE). % if range is non-empty, then full relation must be non-empty
286 infer_set_bounds(image(Rel,_Set),Type,Env,ImgBounds) :- !,
287 infer_set_bounds(range(Rel),Type,Env,RanBounds),
288 subset_bounds(ImgBounds,RanBounds).
289 infer_set_bounds(reverse(Rel),_Type,Env,IBounds) :- !, % relational inverse
290 infer_set_bounds(Rel,Env,Bounds),
291 Bounds = bcart(BA,BB,NonEmptyAB),
292 IBounds = bcart(BB,BA,NonEmptyAB).
293 infer_set_bounds(closure(Rel),_Type,Env,Bounds) :- !, % transitive closure1
294 infer_set_bounds(Rel,Env,Bounds),
295 Bounds = bcart(_,_,_). % dom(closure1(r)) = dom(r), ditto for ran
296 infer_set_bounds(iteration(Rel,_),_Type,Env,Bounds) :- !, % iterate operator
297 infer_set_bounds(Rel,Env,RelBounds),
298 RelBounds = bcart(_,_,_), % dom(iterate(r,n)) <: dom(r), for n>0, ditto for ran; subset
299 % for n=0 basp translation deviates from B Book and uses elements in domain/range only; so it also holds for n=0
300 subset_bounds(Bounds,RelBounds).
301 infer_set_bounds(Term,_Type,Env,Bounds) :- relational_operator_domran_subset_of_relation(Term,Rel),!,
302 infer_set_bounds(Rel,Env,RelBounds),
303 RelBounds = bcart(_,_,_),
304 subset_bounds(Bounds,RelBounds).
305 infer_set_bounds(composition(A,B),Type,Env,Bounds) :- !,
306 is_set_type(Type,couple(TA,TB)),
307 infer_set_bounds(domain(A),TA,Env,BoundsA),
308 infer_set_bounds(range(B),TB,Env,BoundsB),
309 construct_bcart(domain(A),range(B),BoundsA,BoundsB,CartBounds), % dom(A;B) <: dom(A) and ran(A;B) <: ran(B)
310 subset_bounds(Bounds,CartBounds).
311 infer_set_bounds(S,_,_,_) :- functor(S,F,N), write(user_output,uncovered_set(F,N,S)), nl(user_output),fail.
312
313 % relational operator term whose domain/range are subsets of the relation argument
314 relational_operator_domran_subset_of_relation(domain_restriction(_,R),R). % dom(S <| R) <: dom(R), ditto for ran
315 relational_operator_domran_subset_of_relation(domain_subtraction(_,R),R). % dom(S <<| R) <: dom(R)
316 relational_operator_domran_subset_of_relation(range_restriction(R,_),R).
317 relational_operator_domran_subset_of_relation(range_subtraction(R,_),R).
318 relational_operator_domran_subset_of_relation(front(R),R). % dom(front(R)) <: dom(R), ran(front(R)) <: ran(R)
319 relational_operator_domran_subset_of_relation(tail(R),R).
320 relational_operator_domran_subset_of_relation(restrict_front(R,_),R).
321 relational_operator_domran_subset_of_relation(restrict_tail(R,_),R).
322
323 :- use_module(probsrc(custom_explicit_sets),[domain_of_explicit_set_wf/3,range_of_explicit_set_wf/3,
324 is_interval_closure/3]).
325 infer_avl_set_bounds(integer,A,_,Bounds) :-
326 avl_min(A,int(Min)), %min_of_explicit_set_wf(Val,int(Min),no_wf_available),
327 avl_max(A,int(Max)), Bounds = binterval(Min,Max,1).
328 infer_avl_set_bounds(string,A,_,Bounds) :-
329 findall(Nr,(avl_member(string(S),A), get_string_nr(S,Nr)),Nrs),
330 min_member(Min,Nrs), max_member(Max,Nrs), Bounds = binterval(Min,Max,1).
331 infer_avl_set_bounds(couple(TA,TB),A,Env,Bounds) :-
332 domain_of_explicit_set_wf(avl_set(A),Domain,no_wf_available), DA=b(value(Domain),set(TA),[]),
333 range_of_explicit_set_wf(avl_set(A),Range,no_wf_available), RA=b(value(Range),set(TB),[]),
334 infer_set_bounds(cartesian_product(DA,RA),set(couple(TA,TB)),Env,Bounds).
335
336
337 infer_set_ext_el(Env,Bounds,A) :-
338 (infer_scalar_bounds(A,Env,BoundsA)
339 -> mem_bounds(BoundsA,Bounds)
340 ; format(user_output,'Cannot infer bounds for set-ext element:~w~n',[A])
341 ).
342
343 intersect_bounds(binterval(Low1,Up1,NE1),binterval(Low2,Up2,NE2),Bounds) :-
344 init_binterval(Low,Up,Bounds,NE),
345 NE #=< NE1, % if set1 is empty the the intersection is empty
346 NE #=< NE2, % ditto for set 2
347 % Note: binterval(Low,Up) does not mean that all values are present
348 % Hence the resulting interval can be smaller than the naive interval intersection
349 % e.g., {2,4} /\ 1..3 = {2} and not 2..3
350 Low #>= max(Low1,Low2), % Hence: we do not set Low to be exactly max(Low1,Low2)
351 Up #=< min(Up1,Up2). % Ditto for Up and min(Up1,Up2)
352 % TODO: we could have a precision flag, detecting when all values are present
353 union_bounds(binterval(Low1,Up1,NE1),binterval(Low2,Up2,NE2),Bounds) :-
354 init_binterval(Low,Up,Bounds,NE),
355 NE1 #=< NE, % if union empty then set1 empty
356 NE2 #=< NE, % ditto for set 2
357 NE #=< NE1+NE2, % if set1 & set2 empty then union is empty
358 (NE1 #= 0) #=> (Low #= Low2 #/\ Up #= Up2), % if set1 empty we copy set2 to result
359 (NE1 #= 1 #/\ NE2 #= 1) #=> (Low #= min(Low1,Low2) #/\ Up #= max(Up1,Up2)).
360 set_subtract_bounds(binterval(Low1,Up1,NE1),binterval(Low2,Up2,NE2),Bounds) :- % Set1 \ Set2 = Result
361 init_binterval(Low,Up,Bounds,NE),
362 NE #=< NE1, % if set1 is empty the the set difference is empty
363 NE2 #= 0 #=> NE #= NE1, % if set2 is empty then set1 is the result
364 Low #>= Low1, Up #=< Up1, % result is contained in Set1, and we do not really know much more unless Set2 is disjoint to Set1
365 Low1 #>= min(Low,Low2), % if Low2>Low then Low1=Low and if Low2<=Low then smallest possible value in Set1 is Low1
366 Up1 #=< max(Up,Up2). % ditto for upper bound
367 % Low2 #> Low #=> Low #= Low1, % then the lower boundary value Low1 cannot have been removed by set subtraction
368 % Up2 #< Up #=> Up #= Up1. % ditto for upper boundary value
369
370
371 init_binterval(Low,Up,binterval(Low,Up,NonEmpty),NonEmpty) :-
372 NonEmpty #<=> (Low #=< Up).
373
374 % construct a bcart/3 term; setting up non-empty flag
375 construct_bcart(A,B,BA,BB,bcart(BA,BB,NonEmptyAB)) :- NonEmptyAB in 0..1,
376 (get_non_empty_flag(A,BA,NonEmptyA),
377 get_non_empty_flag(B,BB,NonEmptyB)
378 -> NonEmptyAB #= NonEmptyA*NonEmptyB % or minimum of both; if one set empty (0) then cartesian product empty
379 ; format(user_output,'Could not get non-empty-flags: ~w * ~w~n',[A,B])
380 ).
381
382 get_non_empty_flag(Expr,Bounds,NonEmpty) :-
383 (definitely_not_empty_set(Expr) -> NonEmpty=1
384 , debug_format(4,'Definitely non-empty: ~w~n',[Expr])
385 ; get_non_empty_flag(Bounds,NonEmpty)).
386
387 get_non_empty_flag(binterval(_,_,NE),R) :- !, R=NE.
388 get_non_empty_flag(bcart(_,_,NE),R) :- !, R=NE.
389 get_non_empty_flag(_,NE) :-
390 NE in 0..1. % we don't know if set is empty or not; TODO: use definitely_not_empty !?
391
392 % if bounds are non-empty force another non-empty flag to be 1
393 imply_non_empty(Bounds,NonEmptyFlag) :- get_non_empty_flag(Bounds,NE),
394 imply_block(NE,NonEmptyFlag).
395 :- block imply_block(-,?).
396 imply_block(1,1).
397 imply_block(0,_).
398
399 % SCALARS
400 % --------
401
402 :- use_module(probsrc(bsyntaxtree),[is_set_type/2]).
403 :- use_module(probsrc(kernel_objects),[max_cardinality/2]).
404 infer_scalar_bounds(b(E,T,_Infos),Env,Bounds) :- !,
405 (finite_type(T) -> Bounds = T % we could try and infer bounds for fd(_,_) global set values
406 ; infer_scalar_bounds2(E,T,Env,Bounds)).
407 infer_scalar_bounds2(integer(A),_,_Env,Bounds) :- !, Bounds = bint(A).
408 infer_scalar_bounds2(string(A),_,_Env,Bounds) :- !, get_string_nr(A,Nr),Bounds = bint(Nr).
409 infer_scalar_bounds2(identifier(A),Type,Env,Bounds) :- !, lookup_id_bounds(A,Env,Type,Bounds).
410 infer_scalar_bounds2(card(A),integer,_Env,bint(Card)) :-
411 %infer_set_bounds(A,Env,binterval(Low,Up)),
412 !,
413 % TODO: if Low <= Up -> Card #= 1+Up-Low else = 0
414 (get_texpr_type(A,AType),is_set_type(AType,SType),
415 max_cardinality(SType,MaxCard),number(MaxCard)
416 -> Card in 0..MaxCard ; Card #>= 0).
417 infer_scalar_bounds2(couple(A,B),couple(_TA,_TB),Env,bcouple(BA,BB)) :- !,
418 infer_scalar_bounds(A,Env,BA), % TODO: treat if one of them fails / is finite
419 infer_scalar_bounds(B,Env,BB).
420 infer_scalar_bounds2(function(Rel,_Arg),Type,Env,Bounds) :- !,
421 infer_set_bounds(range(Rel),set(Type),Env,RanBounds), % we ignore Arg
422 %convert_set_bounds_to_scalar(RanBounds,Bounds). % we could use this if TRY_FIND_ABORT is TRUE
423 if(mem_bounds(Bounds,RanBounds),
424 true,
425 create_dummy_value(RanBounds,Bounds)). % empty range probably meaning WD error; just return a concrete dummy value
426 infer_scalar_bounds2(div(A,B),integer,Env,Bounds) :-
427 infer_scalar_bounds(B,Env,BoundsB), BoundsB=bint(BB), integer(BB), BB \= 0, !,
428 infer_scalar_bounds(A,Env,BoundsA),
429 apply_binary_op('/',Bounds,BoundsA,BoundsB).
430 infer_scalar_bounds2(BOP,_,Env,Bounds) :-
431 scalar_binary_op(BOP,A,B,ClpfdOp), !,
432 infer_scalar_bounds(A,Env,BoundsA),
433 infer_scalar_bounds(B,Env,BoundsB),
434 apply_binary_op(ClpfdOp,Bounds,BoundsA,BoundsB).
435 infer_scalar_bounds2(BOP,_,_,_) :- functor(BOP,F,N),write(user_output,uncov_scalar(F/N)),nl(user_output),fail.
436
437 %convert_set_bounds_to_scalar(binterval(A,B,NonEmpty),bint(X)) :-
438 % (NonEmpty#=1) #=> (X #>= A #/\ X #=< B).
439
440 % create a dummy element for given bounds
441 create_dummy_value(binterval(A,_,_),bint(DA)) :- !, (var(A),fd_min(A,Min),number(Min) -> DA=Min ; DA=0).
442 create_dummy_value(bcart(A,B,_),bcouple(DA,DB)) :- !, create_dummy_value(A,DA), create_dummy_value(B,DB).
443 create_dummy_value(T,T) :- finite_type(T).
444
445 scalar_binary_op(add(A,B),A,B,'+').
446 scalar_binary_op(multiplication(A,B),A,B,'*').
447 scalar_binary_op(minus(A,B),A,B,'-').
448 apply_binary_op(Op,bint(Res),bint(A),bint(B)) :- !, RHS =.. [Op,A,B],
449 if(call('#=',Res,RHS),true, % TODO: catch overflows
450 (format(user_output,'Inconsistent ~w #= (~w ~w ~w) constraint!',[Res, A,Op,B]),fail)).
451 apply_binary_op(ClpfdOp,Bounds,BoundsA,BoundsB) :-
452 add_internal_error('Illegal call: ',apply_binary_op(ClpfdOp,Bounds,BoundsA,BoundsB)), fail.
453
454 :- use_module(probsrc(bsyntaxtree), [def_get_texpr_id/2, get_texpr_id/2, get_texpr_type/2]).
455
456 %relevant_identifier(TID,Env,ID,BoundsInfo) :- get_texpr_id(TID,ID),
457 % lookup_id_bounds(ID,Env,_,BoundsInfo).
458
459
460 % environment utilities:
461
462 new_env(env(E,_),Opts) :-
463 (member(outer_bounds(OB),Opts)
464 -> maplist(add_outer_bound_info,OB,E)
465 % outer bounds are already ground and need no labeling; they provide bounds for outer variables
466 ; member(open,Opts) -> true ; E=[]).
467
468 lookup_id_bounds(ID,env(Env,_),Type,BoundsInfo) :-
469 (member(bound_internal_info(ID,Type,StoredBounds),Env)
470 -> BoundsInfo=StoredBounds
471 ; format(user_output,'Could not find identifier ~w !!~n',[ID]),
472 bounds_type(Type,BoundsInfo,_)). % set up unconstrained bounds
473
474 % add typed ids to environment, also returns list of bounds information for added ids
475 add_typed_ids([],[],_) --> [].
476 add_typed_ids([TID|T],[BoundsInfo|TB],BA_Required) -->
477 add_typed_id(TID,BoundsInfo,BA_Required), add_typed_ids(T,TB,BA_Required).
478
479 add_typed_id(TID,bound_id_info(ID,Type,Bounds),BA_Required,env(Env,Flags),env(NewEnv,Flags)) :-
480 def_get_texpr_id(TID,ID),
481 get_texpr_type(TID,Type),
482 NewEnv = [bound_internal_info(ID,Type,Fresh)|Env],
483 (bounds_type(Type,Fresh,_)
484 -> compute_bound_info(ID,Type,Fresh,Bounds,Flags),
485 BA_Required = bounds_analysis_required
486 ; debug_format(9,'Ignoring identifier ~w in analysis (type either bounded or too complex)~n',[ID]),
487 Fresh=Type, Bounds=Type
488 ),
489 label_id(ID,Type,Fresh,Flags).
490
491 % add information from outer variables (e.g., computed by infer_bounds for outer predicate)
492 add_outer_bound_info(bound_id_info(ID,Type,Bounds),bound_internal_info(ID,Type,InternalType)) :-
493 convert_bounds_to_internal(Bounds,InternalType).
494
495 convert_bounds_to_internal(integer_in_range(From,To,_),bint(X)) :- !, X in From..To.
496 convert_bounds_to_internal(set(integer_in_range(From,To,_)),binterval(From2,To2,_NonEmpty)) :- !,
497 (number(From) -> From2=From ; true), (number(To) -> To2=To ; true).
498 % we do not know if set is empty or not;
499 % see test 2519 :clingo-double-check x<:1..3 & !y.(y:0..3 & x*(1..2)=(1..2)*(1..y) => y=2)
500 convert_bounds_to_internal(string,bint(Nr)) :- !, Nr #>= 0. % strings number start at 0
501 convert_bounds_to_internal(set(string),binterval(From,_To,_NonEmpty)) :- !,
502 From #>= 0.
503 convert_bounds_to_internal(set(couple(A,B)),BCart) :- !,
504 convert_bounds_to_internal(set(A),BA),
505 convert_bounds_to_internal(set(B),BB),
506 Dummy = b(empty_set,any,[]),
507 construct_bcart(Dummy,Dummy,BA,BB,BCart).
508 convert_bounds_to_internal(X,X).
509
510 finite_type(boolean).
511 finite_type(global(_GS)).
512 finite_type(set(X)) :- finite_type(X).
513 finite_type(couple(X,Y)) :- finite_type(X), finite_type(Y).
514
515 % a type for which we can determine bounds:
516 % it also returns a 0..1 CLP(FD) flag for non-emptyness; useful for sets only
517 bounds_type(integer,bint(_),1).
518 bounds_type(string,bint(_),1).
519 bounds_type(set(integer),binterval(_,_,NonEmpty),NonEmpty).
520 bounds_type(set(string),binterval(_,_,NonEmpty),NonEmpty).
521 bounds_type(couple(A,B),bcouple(BA,BB),1) :-
522 bounds_of_pair(A,B,BA,BB,_). % at least one part requires bounds
523 bounds_type(set(couple(A,B)),bcart(BA,BB,NonEmpty),NonEmpty) :-
524 bounds_of_pair(set(A),set(B),BA,BB,NonEmpty).
525
526 % get bounds of two types, ensuring at least one of them requires bounds inference
527 bounds_of_pair(A,B,BA,BB,NonEmpty) :-
528 (bounds_type(A,BA,NEA)
529 -> (bounds_type(B,BB,NEB) -> NonEmpty #= min(1,NEA+NEB)
530 ; BB=B, NonEmpty=NEA)
531 ; BA=A, bounds_type(B,BB,NonEmpty)).
532
533 :- block compute_bound_info(?,?,?,?,-).
534 compute_bound_info(ID,Type,Fresh,Bounds,_) :-
535 get_bounds(Fresh,Type,Bounds),
536 debug_format(19,'Computed bounds ~w : ~w --> ~w~n',[ID,Type,Bounds]).
537
538 % get bounds of an internal representation into format suitable for b2asp / other tools
539 % it creates a type term, using integer_in_range/2 in place of integer
540 get_bounds(bint(X),Type,integer_in_range(Min,Max,Type)) :- !, fd_min(X,Min), fd_max(X,Max).
541 get_bounds(binterval(X,Y,NonEmpty),set(Type),set(integer_in_range(Min,Max,Type))) :- !,
542 ( NonEmpty == 0 -> Min=1, Max=0
543 ; NonEmpty == 1 -> fd_min(X,Min), fd_max(Y,Max)
544 ; get_non_empty_interval_bounds(X,Y,NonEmpty,Min,Max) % we do not know if set empty or not
545 ).
546 get_bounds(bcouple(A,B),couple(TA,TB),couple(BA,BB)) :- !, get_bounds(A,TA,BA), get_bounds(B,TB,BB).
547 get_bounds(bcart(A,B,NonEmpty),set(couple(TA,TB)),set(couple(BA,BB))) :- !,
548 ( NonEmpty == 0 -> get_bounds(A,set(TA),set(BA)), get_bounds(B,set(TB),set(BB)) % we could return empty_set for BA/BB
549 ; NonEmpty == 1 -> get_bounds(A,set(TA),set(BA)), get_bounds(B,set(TB),set(BB))
550 ; get_non_empty_cart_bounds(A,B,TA,TB,NonEmpty,BA,BB) % we do not know if cartesian product empty or not
551 ).
552 get_bounds(B,_,R) :- finite_type(B),!, R=B.
553 get_bounds(B,Type,R) :- format(user_output,'Unknown bound: ~w (type ~w)~n',[B,Type]), R=B.
554
555 % try get bounds assuming set is non-empty; these bounds will be used for enumeration in clingo
556 get_non_empty_interval_bounds(X,Y,NonEmpty,_,_) :-
557 bb_put(bounds_analysis_min_max,(1,0)), % if propagation fails the set must be empty
558 (NonEmpty=1 % force non-empty and check to see in which range the values must be
559 -> fd_min(X,Min2), fd_max(Y,Max2),
560 bb_put(bounds_analysis_min_max,(Min2,Max2))
561 ; format(user_output,'Bounds interval cannot be non-empty~n',[])
562 ),
563 fail.
564 get_non_empty_interval_bounds(_,_,_,Min,Max) :- bb_get(bounds_analysis_min_max,(Min,Max)).
565
566 % try get bounds assuming cartesian product is non-empty; these bounds will be used for enumeration in clingo
567 get_non_empty_cart_bounds(A,B,TA,TB,NonEmpty,_,_) :-
568 bb_put(bounds_analysis_cart,(empty_set,empty_set)), % if propagation fails the set must be empty
569 (NonEmpty=1 % force non-empty and check to see in which range the values must be
570 -> get_bounds(A,set(TA),set(BA)), get_bounds(B,set(TB),set(BB)),
571 bb_put(bounds_analysis_cart,(BA,BB))
572 ; format(user_output,'Bounds cartesian product cannot be non-empty~n',[])
573 ),
574 fail.
575 get_non_empty_cart_bounds(_,_,_,_,_,BA,BB) :- bb_get(bounds_analysis_cart,(BA,BB)).
576
577
578 :- block label_id(?,?,?,-).
579 label_id(_ID,_Type,Fresh,copy_bounds(Flag)) :- %format(user_output,'Labeling ~w : ~w~n',[ID,Fresh]),
580 label_bounds(Fresh,Flag).
581
582 % TODO: check if finite:
583 :- block label_bounds(?,-).
584 label_bounds(_,no_labeling) :- !.
585 label_bounds(bint(X),_) :- !, label_fd_var(X).
586 label_bounds(binterval(X,Y,Empty),_) :- !, (Empty=0 ; Empty=1), label_fd_var(X), label_fd_var(Y).
587 label_bounds(bcouple(X,Y),F) :- !, label_bounds(X,F), label_bounds(Y,F).
588 label_bounds(bcart(X,Y,Empty),F) :- !, (Empty=0 ; Empty=1), label_bounds(X,F), label_bounds(Y,F).
589 label_bounds(Term,_) :- ground(Term),finite_type(Term),!.
590 label_bounds(Term,_) :- add_internal_error('Unknown bounds info to label:', label_bounds(Term)).
591
592 label_fd_var(X) :- fd_size(X,Sz), (number(Sz) -> indomain(X) ; true).
593
594
595 portray_env(env(NE,_)) :- portray_env2(NE).
596 portray_env2(X) :- var(X),!, write(user_output,' - ... '),nl(user_output).
597 portray_env2([]) :- !.
598 portray_env2([bound_internal_info(ID,Type,BoundsInfo)|TT]) :- !,
599 format(user_output,' - ~w (~w) : ',[ID,Type]), portray_bounds(BoundsInfo), nl(user_output),
600 portray_env2(TT).
601 portray_env2(E) :-
602 format(user_output,' *** ILLEGAL ENV *** ~w~n',[E]).
603
604 portray_bounds(bint(X)) :- !, portray_int(X).
605 portray_bounds(binterval(X,Y,_E)) :- !,
606 write(user_output,'('), portray_int(X), write(user_output,' .. '), portray_int(Y), write(user_output,')').
607 portray_bounds(bcouple(X,Y)) :- !, portray_bounds(X), write(user_output,' , '), portray_bounds(Y).
608 portray_bounds(bcart(X,Y,_E)) :- !, portray_bounds(X), write(user_output,' * '), portray_bounds(Y).
609 portray_bounds(T) :- finite_type(T), !, write(user_output,T).
610 portray_bounds(U) :- write(user_output,'*** UNKNOWN '), write(user_output,U), write(user_output,' ***').
611
612 portray_int(X) :- nonvar(X),!, write(user_output,X).
613 portray_int(X) :- fd_dom(X,Dom), write(user_output,Dom).
614
615 label_env(env(_,Flags),Options) :- !,Flags=copy_bounds(F2),
616 (member(label,Options) -> F2=label_now ; F2=no_labeling).
617 label_env(E,_) :- add_internal_error('Illegal env: ', label_env(E)).
618
619
620
621 /*
622
623 Encode set union constraints using single fd variable:
624
625 | ?- X in 1..3, Y in 2..5, element([X,Y],Z).
626 X in 1 .. 3,
627 Y in 2 .. 5,
628 Z in 1 .. 5 ?
629
630 Intersection:
631 | ?- X in 1..3, Y in 2..5, element([X],Z), element([Y],Z).
632 X in 2 .. 3,
633 Y in 2 .. 3,
634 Z in 2 .. 3 ?
635 yes
636
637 But how do we encode empty set?
638 | ?- X in 1..3, Y in 4..5, element([X],Z), element([Y],Z).
639 no
640
641 */