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