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