1 % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(closures,[construct_closure/4, is_closure/4, %is_closure_x/5,
6 construct_closure_if_necessary/4,
7 get_domain_range_for_closure_types/3,
8 construct_member_closure/5,
9 %construct_not_member_closure/4,
10 construct_complement_closure/3,
11 is_member_closure/5, is_member_closure_with_info/6,
12 is_not_member_closure/5,
13 is_not_member_value_closure/3,
14 is_not_member_value_closure_or_integerset/3,
15 construct_less_equal_closure/2, construct_greater_equal_closure/2,
16 is_lambda_value_domain_closure/5, % checks for special memoization closure
17 is_lambda_value_domain_normal_closure/5, % performs no check for memoization closures
18 is_lambda_closure/7,
19 is_lambda_comprehension_set/4,
20 select_equality/6,
21 is_special_infinite_closure/3,
22 is_id_closure_over/5,
23 is_full_id_closure/3,
24 is_closure_or_integer_set/4,
25 is_infinite_non_injective_closure/1,
26 is_symbolic_closure/1, is_symbolic_closure/3,
27 is_recursive_closure/1, is_recursive_closure/3,
28 get_recursive_identifier_of_closure/2, get_recursive_identifier_of_closure_body/2,
29 mark_closure_as_symbolic/2, mark_closure_as_recursive/2,
30 mark_closure_as_force/2, mark_closure/3
31 ]).
32
33 :- use_module(module_information,[module_info/2]).
34 :- module_info(group,kernel).
35 :- module_info(description,'This module provides various utility functions to analyse ProB closures.').
36
37 construct_closure(Parameters, ParameterTypes, Body, Res) :-
38 Res = closure(Parameters, ParameterTypes, Body).
39 % Res = closure_x(Parameters, ParameterTypes, Body,_). %% STILL HAS PROBLEMS with delay, e.g. inside b_test_exists !!
40
41
42 % an optimized version of construct_closure, which will try to produce explicit values if possible
43 construct_closure_if_necessary(_,_,b(falsity,pred,_),Res) :- !, Res=[].
44 construct_closure_if_necessary([ID], [T1], b(Pred,pred,_), Res) :-
45 construct_unary_closure(Pred,ID,T1,SET),!,
46 Res = SET.
47 construct_closure_if_necessary(Parameters, ParameterTypes, Body, Res) :-
48 Res = closure(Parameters, ParameterTypes, Body).
49
50 :- use_module(b_global_sets,[try_b_type2global_set/2]).
51 :- use_module(custom_explicit_sets,[try_expand_and_convert_to_avl/2]).
52 construct_unary_closure(member(b(identifier(ID),T1,_),b(value(SET),set(T1),_)),ID,T1,Res) :- Res=SET.
53 construct_unary_closure(truth,_,T1,Res) :- try_b_type2global_set(T1,Res).
54 construct_unary_closure(equal(b(identifier(ID),T1,_),b(value(SET),T1,_)),ID,T1,Res) :-
55 try_expand_and_convert_to_avl([SET],Res).
56
57
58 :- use_module(self_check).
59 :- assert_must_succeed( closures:is_closure(closure([x],[integer],body),[x],[integer],body)).
60 %is_closure(closure_x(Parameters, ParameterTypes, Body, _Exp), Parameters, ParameterTypes, Body).
61 is_closure(closure(Parameters, ParameterTypes, Body), Parameters, ParameterTypes, Body).
62
63
64 :- use_module(btypechecker,[couplise_list/2]).
65 get_domain_range_for_closure_types(Types,Domain,Range) :-
66 couplise_list(Types,couple(Domain,Range)).
67
68
69 :- use_module(bsyntaxtree,[create_texpr/4, safe_create_texpr/4, extract_pos_infos/2]).
70 % following not useful: construct_member_closure currently always called where the construction is needed
71 %construct_member_closure(ID,_Type,ClosureSetExpression,Result) :-
72 % nonvar(ClosureSetExpression),ClosureSetExpression = value(S),!,
73 % print(construct_member_closure_value(ID,S)),nl, %%
74 % Result=S.
75 construct_member_closure(ID,Type,Info,ClosureSetExpression,Result) :-
76 check_result_instantiation(Result,construct_member_closure(ID)),
77 create_texpr(identifier(ID),Type,[],TIdentifier), % used to be [generated]
78 extract_pos_infos(Info,PosInfo), % Note: safe_create_texpr will copy WD info
79 safe_create_texpr(ClosureSetExpression,set(Type),PosInfo,TClosureSet), % TODO: we could store whether sub_expression_contains_wd_condition for next call
80 safe_create_texpr(member(TIdentifier,TClosureSet),pred,PosInfo,TPred),
81 construct_closure([ID],[Type],TPred,Result).
82
83 construct_not_member_closure(ID,Type,Info,ClosureSetExpression,Result) :-
84 check_result_instantiation(Result,construct_not_member_closure(ID)),
85 Type==integer,
86 interval_up_to_inf(ClosureSetExpression,Limit),
87 !,
88 construct_less_equal_closure(ID,Limit,Info,Result). % construct an interval closure; better support in kernel for it
89 construct_not_member_closure(ID,Type,Info,ClosureSetExpression,Result) :-
90 create_texpr(identifier(ID),Type,[],TIdentifier), % used to be [generated]
91 safe_create_texpr(ClosureSetExpression,set(Type),Info,TClosureSet),
92 safe_create_texpr(not_member(TIdentifier,TClosureSet),pred,Info,TPred),
93 construct_closure([ID],[Type],TPred,Result).
94
95 interval_up_to_inf(global_set('NATURAL'),-1).
96 interval_up_to_inf(global_set('NATURAL1'),0).
97 interval_up_to_inf(value(global_set('NATURAL')),-1).
98 interval_up_to_inf(value(global_set('NATURAL1')),0).
99
100
101 construct_less_equal_closure(X,Res) :-
102 construct_less_equal_closure('_zzzz_unary',X,[],Res).
103 construct_less_equal_closure(ID,X,Info,Res) :-
104 construct_closure([ID],[integer],
105 b(less_equal(b(identifier(ID),integer,[]),
106 b(value(int(X)),integer,[])), pred,Info),Res).
107
108 construct_greater_equal_closure(X,Res) :-
109 construct_closure(['_zzzz_unary'],[integer],
110 b(greater_equal(b(identifier('_zzzz_unary'),integer,[]),
111 b(value(int(X)),integer,[])), pred,[]),Res).
112
113 :- use_module(error_manager,[add_internal_error/2]).
114 % check that we do not instantiate result too early (rather than using equal_object)
115 check_result_instantiation(X,_) :- var(X),!.
116 check_result_instantiation(closure(_,_,_),_PP) :- !.
117 check_result_instantiation(X,PP) :-
118 add_internal_error('Result already instantiated in incompatible way: ',check_result_instantiation(X,PP)).
119
120 is_member_closure_with_info([ID],[TYPE],b(PRED,_Pred,Info), TYPE,Info,SET) :-
121 is_member_closure_aux(PRED, ID,TYPE,SET).
122 is_member_closure([ID],[TYPE],b(PRED,_Pred,_), TYPE,SET) :-
123 is_member_closure_aux(PRED, ID,TYPE,SET).
124
125 :- use_module(bsyntaxtree,[is_set_type/2]).
126 is_member_closure_aux(member(TID,TSET), ID,TYPE,SET) :-
127 TID = b(identifier(ID),IDTYPE,Info),
128 check_types_unify(IDTYPE,TYPE,Info), % see test 1948
129 TSET = b(SET,_SETTYPE,_).
130 is_member_closure_aux(subset(TID,BSET), ID,TYPE,SET) :-
131 TID = b(identifier(ID),IDTYPE,Info),
132 check_types_unify(IDTYPE,TYPE,Info),
133 SET = pow_subset(BSET).
134 % can we also detect pow1_subset ? {x| x/= {} & x<: BSET}
135
136
137 % detect not_member closures + integerset as special not_member_closures
138 is_not_member_value_closure_or_integerset(global_set(X),TYPE,SET) :- !,
139 is_not_member_global_set(X,TYPE,SET).
140 is_not_member_value_closure_or_integerset(C,TYPE,SET) :- is_not_member_value_closure(C,TYPE,SET).
141
142 is_not_member_global_set('INTEGER',integer,[]).
143 is_not_member_global_set('NATURAL',integer,X) :-
144 construct_less_equal_closure(-1,X). % {x|x<0}.
145 is_not_member_global_set('NATURAL1',integer,X) :-
146 construct_less_equal_closure(0,X). %X = {x|x<1}.
147
148 is_not_member_value_closure(closure(Par,T,B),TYPE,SET) :-
149 is_not_member_closure(Par,T,B,TYPE,value(SET)).
150 is_not_member_closure([ID],[TYPE],b(PRED,_Pred,_),TYPE,SET) :-
151 is_not_member_closure_aux(PRED,ID,TYPE,SET).
152
153 :- use_module(kernel_tools,[ground_value/1]).
154 is_not_member_closure_aux(not_member(b(identifier(ID),IDType,Info),b(SET,SType,_)),ID,TYPE,SET)
155 :-
156 check_types_unify(IDType,TYPE,Info),
157 is_set_type(SType,IDType).
158 is_not_member_closure_aux(not_equal(b(identifier(ID),IDType,Info),ONE),ID,TYPE,SET) :-
159 (ONE = b(value(Val),_,_),
160 ground_value(Val)
161 -> custom_explicit_sets:construct_one_element_custom_set(Val,SetVal), SET = value(SetVal)
162 ; SET = set_extension([ONE])),
163 check_types_unify(IDType,TYPE,Info).
164 %is_not_member_closure_aux(PRED,ID,TYPE,SET) :- print(check_not_mem(PRED,ID,TYPE,SET)),nl,fail.
165
166 :- use_module(btypechecker, [unify_types_strict/2]).
167 check_types_unify(IDType,TYPE,_) :- unify_types_strict(IDType,TYPE),!.
168 check_types_unify(IDType,TYPE,Info) :- add_error(closures,'Types do not match:',IDType-TYPE,Info),fail.
169
170
171 construct_complement_closure(Delta,Type,Closure) :-
172 % print(generating_complement_closure(GlobalSet,Delta,Type)),nl,
173 construct_not_member_closure('_zzzz_unary',Type,[],value(Delta),Closure).
174
175
176
177 /* lambda abstractions */
178 :- assert_must_succeed((closures:is_lambda_closure([x,y],[integer,integer],b(conjunct(b(member(b(identifier(x),integer,[nodeid(pos(0,0,0,0,0,0))]),b(value(global_set('NATURAL')),set(integer),[])),pred,[]),b(equal(b(identifier(y),integer,[]),b(multiplication(b(identifier(x),integer,[]),b(identifier(x),integer,[])),integer,[])),pred,[])),pred,[]),OtherIDs,OtherTypes,_DOMAINPRED,_Res), OtherIDs=[x], OtherTypes=[integer])).
179 :- assert_must_fail((closures:is_lambda_closure([x,y],[integer,integer],b(conjunct(b(conjunct(b(member(b(identifier(x),integer,[]),b(value(global_set('NATURAL')),set(integer),[])),pred,[]),b(equal(b(identifier(y),integer,[]),b(multiplication(b(identifier(x),integer,[]),b(identifier(x),integer,[])),integer,[])),pred,[])),pred,[]),b(less(b(identifier(y),integer,[]),b(value(int(10)),integer,[])),pred,[])),pred,[]),_,_,_D,_Res)
180 )).
181
182 :- use_module(bsyntaxtree,[conjunction_to_list/2,conjunct_predicates/2]).
183 is_lambda_closure(Args,Types,ClosurePred, OtherIDs, OtherTypes, DOMAINPRED,Res) :-
184 % TO DO: do this more efficiently: if LambdaID occurs in any non-equal predicate : stop searching
185 % TO DO: check if is_infinite_equality_closure is not a special case of lambda closure ?
186 append(OtherTypes,[LambdaType],Types), OtherTypes \= [],
187 append(OtherIDs,[LambdaID],Args),
188 Res=b(EXPR,LambdaType,EXPRINFO),
189 %used to call: b_interpreter:member_conjunct(EQ,ClosurePred,DOMAINPRED), ; but inlined below for efficiency
190 select_equality(ClosurePred,LambdaID,EXPR,LambdaType,EXPRINFO,DOMAINPRED),
191 !. % avoid backtracking member_conjunct
192 % tools:print_bt_message(is_lambda_closure(LambdaID)).
193 % Note: LAMBDA is usually '_lambda_result_'
194
195 identifier_equality(b(equal(b(LHS,Type,LHSInfo),b(RHS,_TypeRHS,RHSInfo)),pred,_),ID,Type,EXPR,EXPRINFO) :-
196 % no need to unify with TypeRHS; actually Prolog unification could fail due to seq types ?
197 identifier_equality_aux(LHS,LHSInfo,RHS,RHSInfo,ID,EXPR,EXPRINFO).
198 identifier_equality_aux(identifier(ID),_,EXPR,EXPRINFO,ID,EXPR,EXPRINFO) :- !.
199 identifier_equality_aux(EXPR,EXPRINFO,identifier(ID),_,ID,EXPR,EXPRINFO).
200
201 % find an equality ID = RHSExpr so that ID does not occur in RHSExpr nor in RestPred
202 % the identifier should be provided as input (for the cut below)
203 select_equality(ClosurePred,ID,RHSExpr,Type,Info,RestPred) :-
204 conjunction_to_list(ClosurePred,List),
205 ? select(EQ,List,RestList),
206 identifier_equality(EQ,ID,Type,RHSExpr,Info),
207 !, % once we find a first equality : no need to look for a second one as then does_not_occur in RestPred will always fail !
208 (ID='_lambda_result_',EQ=b(_,_,I),
209 member(prob_annotation('LAMBDA-EQUALITY'),I)
210 -> true % no need to perform occurs check in RHS, but in RestPred, cf test 1874
211 ; %format('Check occurs ~w : ',[ID]), translate:print_bexpr(b(RHSExpr,Type,Info)),nl,
212 does_not_occur_in(ID,b(RHSExpr,Type,Info))
213 ),
214 conjunct_predicates(RestList,RestPred),
215 does_not_occur_in(ID,RestPred).
216
217
218 :- use_module(memoization,[is_lambda_value_domain_memoization_closure/5]).
219
220 % check whether we have a lambda closure and whether we can compute its domain
221 is_lambda_value_domain_closure(P,T,Pred, DomainValue,Expr) :-
222 is_lambda_value_domain_memoization_closure(P,T,Pred, DV,E),!,
223 DV \= fail, E\= fail,
224 DomainValue=DV, Expr=E.
225 is_lambda_value_domain_closure(Args,Types,B, DomainValue, EXPR) :-
226 is_lambda_value_domain_normal_closure(Args,Types,B, DomainValue, EXPR).
227
228 is_lambda_value_domain_normal_closure(Args,Types,B, DomainValue, EXPR) :-
229 % tools_printing:print_term_summary(try_is_lambda_domain(Args,Types,B)), %
230 is_lambda_closure(Args,Types,B, OtherIDs,OtherTypes, DomainPred, EXPR),!,
231 %print(lambda_closure(OtherIDs)), translate:print_bexpr(EXPR),nl,
232 construct_closure_if_necessary(OtherIDs,OtherTypes,DomainPred,DomClosure),
233 (is_symbolic_closure(Args,Types,B)
234 -> mark_closure_as_symbolic(DomClosure,DomainValue)
235 ; DomainValue = DomClosure).
236 %print(lambda_domain(Args)),nl, (IDs=[_,_|_] -> trace ; true),
237 %translate:print_bvalue(DomainValue),nl.
238
239 % LAMBDARES is usually _lambda_result_, LAMBDARES cannot occur in DOMAIN (is value)
240
241 :- use_module(library(lists),[maplist/4]).
242 is_lambda_comprehension_set(b(comprehension_set(Parameters,Body),_,_),LambdaParas,DomainPred,EXPR) :-
243 maplist(get_names_and_types,Parameters,Args,Types),
244 is_lambda_closure(Args,Types,Body, OtherIDs,OtherTypes, DomainPred, EXPR),
245 maplist(combine_names_and_types,OtherIDs,OtherTypes,LambdaParas).
246
247 get_names_and_types(b(identifier(ID),Type,_),ID,Type).
248 combine_names_and_types(ID,Type,b(identifier(ID),Type,[])).
249
250 :- assert_must_succeed(closures:is_special_infinite_closure([x],[integer],b(truth,pred,[]))).
251 :- assert_must_succeed((X=b(identifier(x),integer,[]),N=b(integer(3),integer,[]),
252 closures:is_special_infinite_closure([x],[integer],b(greater(X,N),pred,[])))).
253 :- assert_must_succeed((X=b(identifier(x),integer,[]),N=b(integer(3),integer,[]),
254 closures:is_special_infinite_closure([x],[integer],b(greater(X,N),pred,[])))).
255 :- assert_must_succeed((X=b(identifier(x),integer,[]),N=b(integer(3),integer,[]),
256 closures:is_special_infinite_closure([x],[integer],b(less(X,N),pred,[])))).
257 :- assert_must_succeed((X=b(identifier(x),integer,[]),N=b(integer(3),integer,[]),
258 closures:is_special_infinite_closure([x],[integer],b(not_equal(X,N),pred,[])))).
259 :- assert_must_fail((X=b(identifier(x),integer,[]),N=b(integer(3),integer,[]),
260 closures:is_special_infinite_closure([x],[integer],b(equal(X,N),pred,[])))).
261 :- assert_must_succeed((X=b(identifier(x),integer,[]),N=b(integer(3),integer,[]),
262 closures:is_special_infinite_closure([x,y],[integer,integer],b(equal(X,N),pred,[])))).
263 :- assert_must_succeed((Y=b(identifier(y),integer,[]),N=b(integer(3),integer,[]),
264 closures:is_special_infinite_closure([x,y],[integer,integer],b(equal(Y,N),pred,[])))).
265
266 :- use_module(typing_tools,[is_infinite_type/1]).
267 /* checking for infinite closures */
268 %is_special_infinite_closure(_Par,T,b(truth,_Pred,_)) :- !, % now dealt with below
269 % member(Type,T), is_infinite_type(Type),!.
270 is_special_infinite_closure(Par,T,Body) :-
271 ? is_infinite_equality_closure(Par,T,Body),!.
272 %is_special_infinite_closure(Par,T,Body) :- is_full_id_closure(Par,T,Body,TYPE), is_infinite_type(TYPE).
273 %is_special_infinite_closure(Par,T,Body) :- is_prj1_closure(Par,T,Body,T1,_T2), is_infinite_type(T1).
274 %is_special_infinite_closure(Par,T,Body) :- is_prj2_closure(Par,T,Body,_T1,T2), is_infinite_type(T2).
275 is_special_infinite_closure(Par,T,Body) :-
276 is_not_member_closure(Par,T,Body,Type,value(_)), is_infinite_type(Type).
277
278 :- use_module(library(lists)).
279
280 greater_typing(greater(b(identifier(ID),integer,_),b(VUP,integer,_)),ID,UP) :- is_integer_val(VUP,UP).
281 greater_typing(greater_equal(b(identifier(ID),integer,_),b(VUP,integer,_)),ID,UP) :- is_integer_val(VUP,UP).
282 greater_typing(less(b(VUP,integer,_),b(identifier(ID),integer,_)),ID,UP) :- is_integer_val(VUP,UP).
283 greater_typing(less_equal(b(VUP,integer,_),b(identifier(ID),integer,_)),ID,UP) :- is_integer_val(VUP,UP).
284
285 less_typing(less(b(identifier(ID),integer,_),b(VUP,integer,_)),ID,UP) :- is_integer_val(VUP,UP).
286 less_typing(less_equal(b(identifier(ID),integer,_),b(VUP,integer,_)),ID,UP) :- is_integer_val(VUP,UP).
287 less_typing(greater(b(VUP,integer,_),b(identifier(ID),integer,_)),ID,UP) :- is_integer_val(VUP,UP).
288 less_typing(greater_equal(b(VUP,integer,_),b(identifier(ID),integer,_)),ID,UP) :- is_integer_val(VUP,UP).
289
290 is_integer_val(integer(UP),UP).
291 is_integer_val(value(V),UP) :- nonvar(V),V=int(UP).
292
293 is_static_expr_of_infinite_type(b(E,Type,_)) :- is_static_expr_of_infinite_type2(E,Type).
294 is_static_expr_of_infinite_type2(integer(_),_).
295 is_static_expr_of_infinite_type2(string(_),_).
296 is_static_expr_of_infinite_type2(real(_),_).
297 is_static_expr_of_infinite_type2(value(V),Type) :- is_val_of_infinite_type(V,Type).
298 is_static_expr_of_infinite_type2(empty_set,Type) :- is_infinite_type(Type).
299 is_static_expr_of_infinite_type2(empty_sequence,Type) :- is_infinite_type(Type).
300 % TODO: more typical expressions, set/sequence extension pairs
301
302 is_val_of_infinite_type(V,_) :- var(V),!,fail.
303 is_val_of_infinite_type(int(_),_).
304 is_val_of_infinite_type(string(_),_).
305 is_val_of_infinite_type(term(floating(_)),_).
306 is_val_of_infinite_type((A,B),couple(TA,TB)) :- (is_val_of_infinite_type(A,TA) -> true ; is_val_of_infinite_type(B,TB)).
307 is_val_of_infinite_type([],Type) :- is_infinite_type(Type).
308 is_val_of_infinite_type(avl_set(_),Type) :- is_infinite_type(Type).
309
310
311
312
313 % the following also translates global_set(NATURAL(1)) into closures
314 % TO DO: probably better to remove global_set(INTSET) all together and rewrite in ast_cleanup to closure
315 is_closure_or_integer_set(closure(P,T,B),P,T,B).
316 is_closure_or_integer_set(global_set(INTSET),
317 ['_zzzz_unary'],[integer],
318 b(greater_equal(
319 b(identifier('_zzzz_unary'),integer,[]),
320 b(integer(BOUND),integer,[])
321 ),
322 pred,
323 [prob_annotation('SYMBOLIC')])
324 ) :-
325 get_bound(INTSET,BOUND).
326 get_bound('NATURAL',0).
327 get_bound('NATURAL1',1).
328 % TO DO: allow INTEGER / maximal sets ? -> truth; could get rid of complement sets?
329
330 % to do: extend; could be value(infinite_closure)...
331
332
333
334 /* Equality closures {x1,x2,...|id=E2}, where id does not occur in E2 and id =xi */
335 % should cover id, prj1, prj2
336 % {x,y|y:BOOL & x=f(y) } or %x.(x:NATURAL|Expr(x))
337 % would not be infinite {x,y|x:BOOL & x=f(g(x)*y)} , g={FALSE|->0, TRUE|->1}, f = ...
338 % we assume Well-Definedness
339 % we also accept closures without equality now
340
341 is_infinite_equality_closure(IDs, TYPES, Body) :-
342 %IDs = [_,_|_], % we used to require at least at least two variables
343 \+ Body = b(member(_,_),_,_), % simple member closure; we deal with it separately (and avoid blow-up of checks, cf test 2283)
344 ? check_inf_cl_body(Body,[],OutConstrained),
345 % if we arrive here, we know that the body constraint is satisfiable
346 (member(_ID/infinite,OutConstrained) -> true
347 ; contains_infinite_type(IDs,TYPES,OutConstrained)).
348
349 contains_infinite_type([ID|IT],[H|T],OutConstrained) :-
350 (is_infinite_type(H),
351 ? \+ member(ID/_,OutConstrained)
352 -> true ; contains_infinite_type(IT,T,OutConstrained)).
353
354 :- use_module(library(ordsets),[ord_member/2]).
355 is_quantified(SIds,ID/_) :- ord_member(ID,SIds).
356 is_lambda_equality(b(equal(_,_),_,Infos)) :- member(prob_annotation('LAMBDA-EQUALITY'),Infos).
357
358 :- use_module(b_ast_cleanup,[definitely_not_empty_and_finite/1, definitely_infinite/1]).
359 :- use_module(external_functions,[external_pred_always_true/1]).
360 :- use_module(bsyntaxtree, [get_texpr_id/2, get_texpr_type/2, definitely_not_empty_set/1]).
361 check_inf_cl_body(b(B,pred,_),InConstrained,OutConstrained) :-
362 ? check_bdy_aux(B,InConstrained,OutConstrained).
363
364 l_check_inf_cl_body([]) --> [].
365 l_check_inf_cl_body([H|T]) --> check_inf_cl_body(H), l_check_inf_cl_body(T).
366
367 :- use_module(probsrc(tools),[split_list/4]).
368
369 check_bdy_aux(conjunct(A,B),InConstrained,OutConstrained) :- !,
370 conjunction_to_list(b(conjunct(A,B),pred,[]),List),
371 split_list(is_lambda_equality,List,LambdaEq,Rest),
372 l_check_inf_cl_body(LambdaEq,InConstrained,OutConstrained1), % treat lambda equalities first
373 % reason: avoid failing due to rhs_safe checks; ideally we should do topological sorting
374 % or keep track of really used ids in each equality
375 l_check_inf_cl_body(Rest,OutConstrained1,OutConstrained).
376 check_bdy_aux(disjunct(A,B),InConstrained,OutConstrained) :- !,
377 (check_inf_cl_body(A,InConstrained,OutConstrained) -> true
378 ; check_inf_cl_body(B,InConstrained,OutConstrained)).
379 check_bdy_aux(exists(TIds,ExistsBody), Constrained, OutConstrained) :- !,
380 bsyntaxtree:get_texpr_ids(TIds,Ids), sort(Ids,SIds),
381 % happens in Event-B style compr. sets, cf test 2514
382 % Example: bap={i,x,y• i:INTEGER & x=y & x:INTEGER|i|->(x|->y)} & bap[{100}][1..2]=res
383 exclude(is_quantified(SIds),Constrained,C1), % remove any var that is clashing with exists ids
384 check_inf_cl_body(ExistsBody,C1,C2),
385 exclude(is_quantified(SIds),C2,OutConstrained).
386 % remove quantified ids; should not count towards outer infinity, unless linked via equal to an outer id
387 % example, UNION(x,y,z).(x:INTEGER & y=x*x & z=x+x|{x|->(y|->z)}) = s & not(s:FIN(s))
388
389 check_bdy_aux(equal(LHS,RHS), Constrained, OutConstrained) :- !,
390 (check_bdy_equal(LHS,RHS,Constrained,OutConstrained) -> true
391 ; check_bdy_equal(RHS,LHS,Constrained,OutConstrained)),!.
392 check_bdy_aux(member(b(identifier(ID),TYPE,_),SET),Constrained,[ID/INFINITE|Constrained]) :- !,
393 \+ member(ID/_,Constrained),
394 rhs_safe(Constrained,SET), % avoid ID : {ID+1, ID+2}
395 (is_infinite_type(TYPE)
396 -> %check that SET is infinite; otherwise remove from IDs
397 (definitely_infinite(SET) -> INFINITE=infinite;
398 definitely_not_empty_and_finite(SET) -> INFINITE = finite
399 )
400 ; definitely_not_empty_and_finite(SET), % otherwise we may have no solution and the entire closure is empty
401 INFINITE = finite
402 ).
403 check_bdy_aux(member(LHS,RHS),Constrained,[ID/infinite|Constrained]) :- !, % ID'Field : SET
404 record_field_access_with_infinite_rest_type(LHS,RHS,Constrained,ID), % should we also cater for finite rest?
405 definitely_not_empty_set(RHS).
406 check_bdy_aux(not_equal(A,B),Constrained,[ID/infinite|TC]) :- !,
407 (get_texpr_id(A,ID)
408 -> is_static_expr_of_infinite_type(B) % we have to be careful that B does not directly or indirectly reference A
409 ; get_texpr_id(B,ID),
410 is_static_expr_of_infinite_type(A)
411 ),
412 (select(ID/Kind,Constrained,TC)
413 -> Kind=infinite % if it was infinite it will remain infinite, we only discard a single static value
414 ; TC=Constrained).
415 check_bdy_aux(truth,Constrained,OutConstrained) :- !, OutConstrained=Constrained.
416 check_bdy_aux(external_pred_call(FunName,_Args),Constrained,Constrained) :- !,
417 external_pred_always_true(FunName).
418 check_bdy_aux(EXPR,Constrained,[ID/infinite|Constrained]) :-
419 %% TO DO: store bounds in ID/... list to check if the domain remains infinite!
420 greater_typing(EXPR,ID,_UP),
421 \+ member(ID/_,Constrained).
422 check_bdy_aux(EXPR,Constrained,[ID/infinite|Constrained]) :-
423 less_typing(EXPR,ID,_UP),
424 \+ member(ID/_,Constrained).
425
426
427 check_bdy_equal(LHS,RHS, Constrained, [ID/equal|Constrained]) :-
428 get_texpr_id(LHS,ID),
429 \+ member(ID/_,Constrained), % no constraints on ID so far
430 does_not_occur_in(ID,RHS), % we have no direct equation like ID=ID+1
431 rhs_safe(Constrained,RHS),
432 !. % the equation must have a solution; assuming well-definedness
433 check_bdy_equal(LHS,RHS, Constrained, [ID/infinite|Constrained]) :- % ID'FieldName = RHS
434 % detect closures like {x|x:struct(a:INTEGER,b:BOOL) & x'b=FALSE}, see test 2483
435 record_field_access_with_infinite_rest_type(LHS,RHS,Constrained,ID),!.
436
437 % check if the RHS is sufficiently unconstrained so that equality with a new variable will succeed
438 % see test 2483, card({x,y,v|x:INTEGER & y:{v+1,v+2} & v=y}) = 0
439 % TODO: maybe faster to find_identifier_uses once rather than calling does_not_occur_in repeatedly
440 % TODO: detect if an identifier is used only once, then we do not need to check RHS (LAMBDA-EQUALITY)
441 rhs_safe([],_).
442 rhs_safe([HID/Kind|TConstrained],RHS) :-
443 (Kind=infinite -> true % CHECK! happens for HID=b in 2283
444 ; %HID could be bound to new ID, e.g., via HID=NewID+1 and equation NewID=HID would have no solution
445 does_not_occur_in(HID,RHS)
446 ),
447 rhs_safe(TConstrained,RHS).
448
449 % see if we have LHS = ID'FieldName and ID does not occur in RHS and other fields are still infinite
450 record_field_access_with_infinite_rest_type(LHS,RHS,Constrained,ID) :-
451 LHS = b(record_field(TID,FieldName),_,_),
452 get_texpr_id(TID,ID),
453 \+ member(ID/_,Constrained), % no constraints on ID so far
454 get_texpr_type(TID,record(FieldTypes)),
455 select(field(FieldName,_),FieldTypes,RestTypes),
456 % we now have only a single value for field FieldName
457 is_infinite_type(record(RestTypes)), % there are still infinitely many values left given the other fields
458 does_not_occur_in(ID,RHS),
459 rhs_safe(Constrained,RHS),!.
460
461
462 :- use_module(bsyntaxtree,[occurs_in_expr/2]).
463 does_not_occur_in(ID,EXPR) :- \+ occurs_in_expr(ID,EXPR).
464
465
466
467 % check if we have a closure of type id(SetValue)
468
469 is_id_closure_over([ID1,ID2], [TYPE,TYPE],Body, ID_Domain, Full) :- nonvar(Body),
470 Body=b(equal(b(identifier(ID1),TYPE,_),b(identifier(ID2),TYPE,_)),pred,_),
471 !,
472 convert_type_to_value(TYPE,ID_Domain), Full=true.
473 is_id_closure_over(Par,Types,Body,ID_Domain,Full) :- nonvar(Par),nonvar(Body),
474 is_member_closure(Par,Types,Body,_,Set), % print(member_closure(Set)),nl,
475 nonvar(Set),
476 Set = identity(b(VAL,SType,_)),
477 is_set_type(SType,_),
478 nonvar(VAL), VAL=value(ID_Domain),
479 (custom_explicit_sets:is_definitely_maximal_set(ID_Domain) -> Full=true ; Full=false).
480
481 %:- use_module(kernel_objects,[all_strings_wf/2]).
482 convert_type_to_value(integer,global_set('INTEGER')).
483 convert_type_to_value(global(G),global_set(G)).
484 convert_type_to_value(boolean,BS) :- BS=[pred_true /* bool_true */,pred_false /* bool_false */]. % TO DO: generate AVL ?
485 convert_type_to_value(string,global_set('STRING')). % :- all_strings_wf(S,WF).
486 %convert_type_to_value(Type,closure([x],[Type],TRUTH)) :- ... TO DO
487
488
489
490 /* Event-B id closure over full Type */
491
492 is_full_id_closure(P,T,B) :- is_id_closure_over(P,T,B,_,true).
493
494
495 % currently commented out in is_special_infinite_closure
496 %is_prj1_closure([ID1,_ID2,RESID],[Type1,Type2,Type1],
497 % b(equal(b(identifier(RESID),Type1,_),b(identifier(ID1),Type1,_)),pred,_),Type1,Type2).
498 %is_prj2_closure([_ID1,ID2,RESID],[Type1,Type2,Type2],
499 % b(equal(b(identifier(RESID),Type2,_),b(identifier(ID2),Type2,_)),pred,_),Type1,Type2).
500
501
502 % ---- SYMBOLIC and RECURSIVE annotations
503
504 get_recursive_identifier_of_closure(V,RID) :- nonvar(V), V=closure(_P,_T,B),
505 get_recursive_identifier_of_closure_body(B,RID).
506 get_recursive_identifier_of_closure_body(b(_,_,BodyInfo),RID) :- member(prob_annotation(recursive(RID)),BodyInfo).
507
508 is_recursive_closure(V) :- nonvar(V), V=closure(P,T,B),
509 is_recursive_closure(P,T,B).
510
511 is_recursive_closure(_P,_T,b(_,_,INFO)) :-
512 member(prob_annotation('RECURSIVE'),INFO).
513 % we also have prob_annotation(recursive(TID)) annotation
514
515 is_symbolic_closure(V) :- nonvar(V), V=closure(P,T,B),
516 ? is_symbolic_closure(P,T,B).
517
518 is_symbolic_closure(_P,_T,b(_,_,INFO)) :-
519 ? member(prob_annotation('SYMBOLIC'),INFO).
520
521 % see also is_converted_lambda_closure
522
523
524 :- use_module(error_manager,[add_internal_error/2, add_error/3]).
525 :- use_module(debug,[debug_println/2]).
526
527 % mark a closure as symbolic by marking the info field of the body predicate
528 mark_closure_as_symbolic(C,R) :-
529 mark_closure3(C,['SYMBOLIC'],R).
530 mark_closure_as_recursive(C,R) :-
531 mark_closure3(C,['SYMBOLIC','RECURSIVE'],R).
532 mark_closure_as_force(C,R) :-
533 mark_closure3(C,['FORCE'],R).
534 mark_closure3(_,ANN,R) :- nonvar(R), % we could use equal_object
535 add_internal_error('Result already instantiated: ',mark_closure3(_,ANN,R)),fail.
536 mark_closure3(C,ANN,R) :- var(C), % we could use equal_object
537 !,
538 debug_println(19,not_marking_var_closure(C,ANN)),
539 R=C.
540 mark_closure3(C,ANN,R) :- mark_closure(C,ANN,R).
541 %:- block mark_closure(-,?,?).
542 mark_closure(closure(P,T,B),ANN,R) :- !, mark_aux(P,T,B,ANN,R).
543 mark_closure(A,_,Res) :- A=Res. % not a closure
544 %:- block mark_aux(?,?,-,?,?).
545 mark_aux(P,T,b(Pred,pred,INFO),ANN,Res) :-
546 (ground(INFO)
547 -> mark_info(ANN,INFO,RINFO)
548 ; add_error(mark_aux,'Info field not set: ',closure(P,T,b(Pred,pred,INFO))),
549 RINFO=INFO),
550 Res = closure(P,T,b(Pred,pred,RINFO)).
551
552 mark_info([],INFO,INFO).
553 mark_info([ANN|T],INFO,Res) :-
554 (member(prob_annotation(ANN),INFO) -> Res=TRes ; Res = [prob_annotation(ANN)|TRes]),
555 mark_info(T,INFO,TRes).
556
557
558
559 % this will detect prj1/prj2 style functions which project away an infinite number of args
560 % such non-injective functions/relations remain infinite when composed with a finite set
561 is_infinite_non_injective_closure(closure(P,T,Body)) :-
562 %see also bsyntaxtree:get_lambda_equality(Body,LambdaID,RestBody,ResultExpr),
563 Body = b(equal(LHS,RHS),pred,_),
564 get_texpr_id(LHS,LambdaID),
565 get_texpr_id(RHS,ProjID), % TODO: get used ids and see if the rest is infinite
566 append(Args,[LambdaID],P), Args = [_,_|_],
567 nth1(Nr,Args,ProjID),
568 nth1(Nr2,T,NonProjectedType),
569 Nr2 \= Nr,
570 is_infinite_type(NonProjectedType).