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(b_operation_guards,[
6
7 get_quantified_operation_enabling_condition/5, % unsimplified or simplified, fully quantified
8 get_operation_enabling_condition/7,
9 get_unsimplified_operation_guard/2,
10 get_unsimplified_operation_enabling_condition/5,
11 get_simplified_operation_enabling_condition/5,
12
13 get_operation_propositional_guards/3,
14 get_operation_propositional_guards/6,
15
16 get_substitution_enabling_condition/4, % get the enabling condition of a particular substitution
17 with_eventb_refinement_assumed_correct/1
18 ]).
19
20 :- use_module(module_information,[module_info/2]).
21 :- module_info(group,ast).
22 :- module_info(description,'Compute guard predicates of operations.').
23
24
25 :- use_module(tools).
26 :- use_module(bsyntaxtree).
27 :- use_module(bmachine,[b_top_level_operation/1,
28 b_get_machine_operation_for_animation/6, b_get_machine_operation_for_animation/4,
29 b_get_machine_operation/6, bmachine_is_precompiled/0]).
30 :- use_module(b_interpreter_components,[construct_optimized_exists/3,construct_optimized_exists/4]).
31 :- use_module(specfile,[animation_minor_mode/1]).
32 :- use_module(external_functions,[external_subst_enabling_condition/3]).
33 :- use_module(b_ast_cleanup, [clean_up/3]).
34 :- use_module(debug).
35 :- use_module(error_manager).
36
37 :- use_module(library(lists)).
38 :- use_module(library(ordsets)).
39
40
41
42 :- dynamic obtain_action_enabling_conditions/0, simplify_enabling_condition/1.
43 simplify_enabling_condition(true).
44 set_simplify_enabling_condition(X) :- retractall(simplify_enabling_condition(_)),
45 assertz(simplify_enabling_condition(X)).
46
47 obtain_action_enabling_conditions.
48 set_obtain_action_enabling_conditions(auto_obtain) :- !,
49 (animation_minor_mode(eventb) -> GetFromAction=false
50 ; GetFromAction=true), set_obtain_action_enabling_conditions(GetFromAction).
51 set_obtain_action_enabling_conditions(true) :- !,
52 (obtain_action_enabling_conditions -> true ; assertz(obtain_action_enabling_conditions)).
53 set_obtain_action_enabling_conditions(_) :- retractall(obtain_action_enabling_conditions).
54
55 % run Goal (once) with the given flags and restore the previous flags afterwards,
56 % so that direct calls to get_substitution_enabling_condition/4 are not influenced by earlier calls
57 with_enabling_flags(GetAct,Simplify,Goal) :-
58 (obtain_action_enabling_conditions -> OldGetAct=true ; OldGetAct=false),
59 simplify_enabling_condition(OldSimplify),
60 set_obtain_action_enabling_conditions(GetAct),
61 set_simplify_enabling_condition(Simplify),
62 call_cleanup(once(Goal),
63 (set_obtain_action_enabling_conditions(OldGetAct),
64 set_simplify_enabling_condition(OldSimplify))).
65
66 % run Goal (once) assuming that the guard strengthening and witness feasibility proof obligations
67 % of Event-B refinements hold: abstract guards and witnesses then do not make the guards imprecise
68 % (useful, e.g., for constraint-based deadlock checking, which assumes the invariant)
69 :- dynamic assume_eventb_refinement_correct/0.
70 :- meta_predicate with_eventb_refinement_assumed_correct(0).
71 with_eventb_refinement_assumed_correct(Goal) :-
72 (assume_eventb_refinement_correct -> once(Goal)
73 ; assertz(assume_eventb_refinement_correct),
74 call_cleanup(once(Goal), retractall(assume_eventb_refinement_correct))).
75
76 get_unsimplified_operation_guard(OpName,Guard) :-
77 get_unsimplified_operation_enabling_condition(OpName,Parameters,EnablingCondition,_BecomesSuchVars,_Precise),
78 construct_optimized_exists(Parameters,EnablingCondition,Guard).
79
80 get_unsimplified_operation_enabling_condition(OpName,Parameters,EnablingCondition,BecomesSuchVars,Precise) :-
81 Simplify=false,
82 ? get_operation_enabling_condition(OpName,Parameters,EnablingCondition,
83 BecomesSuchVars,Precise,auto_obtain,Simplify).
84
85 get_simplified_operation_enabling_condition(OpName,Parameters,EnablingCondition,BecomesSuchVars,Precise) :-
86 Simplify=true,
87 get_operation_enabling_condition(OpName,Parameters,EnablingCondition,
88 BecomesSuchVars,Precise,auto_obtain,Simplify).
89
90
91 % get a quantified predicate, Precise is either precise or imprecise
92 get_quantified_operation_enabling_condition(OpName, BExpr, BecomesSuchVars, Precise, Simplify) :-
93 get_operation_enabling_condition(OpName,IDs,Condition,
94 BecomesSuchVars,Precise,auto_obtain,Simplify),
95 create_or_merge_exists(IDs, Condition, BExpr).
96
97 :- use_module(bmachine,[b_operation_reads_output_variables/3]). % ALLOW_OUTPUT_READING
98
99 get_operation_enabling_condition(OpName,Parameters,EnablingCondition,BecomesSuchVars,IsPrecise,GetAct,Simplify) :-
100 ? b_get_machine_operation_for_animation(OpName,_Results,Parameters,TBody),
101 % GetAct: specify whether we want also conditions stemming from actions, such as :() or ::
102 % Simplify: specify whether we want to simplify the EnablingCondition if possible
103 with_enabling_flags(GetAct,Simplify,
104 get_substitution_enabling_condition(TBody,EnablingCondition0,BecomesSuchVars,IsPreciseV)),
105 (b_operation_reads_output_variables(OpName,ReadOutputVariablesIds,_)
106 -> debug_format(19,'Filtering guard of operation ~w which reads output variables ~w~n',
107 [OpName,ReadOutputVariablesIds]),
108 conjunction_to_list(EnablingCondition0,EL),
109 exclude(guard_reads_output(ReadOutputVariablesIds),EL,EL2),
110 (EL2=[] -> get_texpr_pos_infos(TBody,PosInfos),EnablingCondition = b(truth,pred,PosInfos)
111 ; conjunct_predicates_with_pos_info(EL2,EnablingCondition)
112 ),
113 IsPrecise=imprecise
114 ; var(IsPreciseV) -> IsPrecise = precise, EnablingCondition=EnablingCondition0
115 ; IsPrecise = imprecise, EnablingCondition=EnablingCondition0
116 ).
117
118 guard_reads_output(OutputVars,Guard) :-
119 find_identifier_uses(Guard,[],Ids),
120 ord_intersect(Ids,OutputVars).
121
122 get_substitution_enabling_condition(TBody,EnablingCondition,BecomesSuchVars,IsPrecise) :-
123 get_texpr_expr(TBody,Body), get_texpr_pos_infos(TBody,PosInfos),
124 ? (get_operation_enabling_condition1(Body,PosInfos,Cond,BecomesSuchVars,IsPrecise)
125 -> (Cond = b(truth,_,_)
126 -> create_texpr(truth,pred,PosInfos,EnablingCondition) % ensure bvisual2 will report reasonable location
127 % otherwise we may get the location within a sequential composition within a subsidiary operation call
128 ; EnablingCondition = Cond)
129 ; add_internal_error('Getting enabling condition failed: ',Body),
130 create_texpr(truth,pred,PosInfos,EnablingCondition),
131 BecomesSuchVars=[]).
132
133 get_operation_enabling_condition1(if(IfList),PosInfos,Res,BecomesSuchVars,IsPrecise) :- !,
134 ? maplist5(get_if_condition(IsPrecise),IfList,Tests,Conds,BecomesSuchVars1),
135 append(BecomesSuchVars1,BecomesSuchVars),
136 ? (member(X,Conds), \+ is_truth(X)
137 -> disjoin_ifs(Tests,Conds,[],L),
138 conjunct_predicates_with_pos_info(L,Res)
139 ; create_texpr(truth,pred,PosInfos,Res) % all branches have no enabling condition
140 ).
141 get_operation_enabling_condition1(Subst,PosInfos,Truth,[],_Prcise) :-
142 basic_has_no_enabling_condition(Subst),!,
143 create_texpr(truth,pred,PosInfos,Truth).
144 get_operation_enabling_condition1(case(A,_,_),PosInfos,Truth,[],imprecise) :- !,
145 % CASE is now translated to LET + IF-THEN-ELSE in b_ast_cleanup
146 (debug_mode(on) -> print('Not computing enabling for CASE: '), translate:print_bexpr(A),nl ; true),
147 create_texpr(truth,pred,PosInfos,Truth).
148 get_operation_enabling_condition1(becomes_element_of(_LHS,RHS),PosInfos,Res,[],_Precise) :-
149 obtain_action_enabling_conditions,
150 get_texpr_expr(RHS,RHSExpr),get_texpr_type(RHS,Type),!,
151 (simplify_enabling_condition(true),
152 definitely_not_empty(RHSExpr,Type)
153 -> create_texpr(truth,pred,PosInfos,Res)
154 ; create_texpr(empty_set,Type,[],EmptySet),
155 safe_create_texpr(not_equal(RHS,EmptySet),pred,PosInfos,Res2),
156 clean_up(Res2,[],Res)
157 ).
158 get_operation_enabling_condition1(Body,_,EnablingCondition,BecomesSuchVars,IsPrecise) :-
159 ? get_operation_enabling_condition2(Body,EnablingCondition,BecomesSuchVars,IsPrecise),!.
160 get_operation_enabling_condition1(X,PosInfos,Truth,[],imprecise) :- %print(cannot_obtain(X)),nl,
161 (obtain_action_enabling_conditions
162 -> functor(X,F,A),debug_println(9,cannot_obtain_enabling(F/A,X)) ; true),
163 create_texpr(truth,pred,PosInfos,Truth).
164
165 /* TO DO: recursively expand the code below for more complicated SELECT,ANY,... */
166 get_operation_enabling_condition2(precondition(PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % PRE
167 get_substitution_enabling_condition(TBody,RC,BecomesSuchVars,IsPrecise),
168 conjunct_predicates_with_pos_info(PreCond,RC,Res).
169 get_operation_enabling_condition2(assertion(_PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % ASSERT
170 get_substitution_enabling_condition(TBody,Res,BecomesSuchVars,IsPrecise).
171 get_operation_enabling_condition2(witness_then(_PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % WITNESS
172 get_substitution_enabling_condition(TBody,Res,BecomesSuchVars,IsPrecise).
173 get_operation_enabling_condition2(block(TBody),Res,BecomesSuchVars,IsPrecise) :- !, % BEGIN ... END
174 get_substitution_enabling_condition(TBody,Res,BecomesSuchVars,IsPrecise).
175 get_operation_enabling_condition2(choice(ChoiceList),Res,BecomesSuchVars,IsPrecise) :- !, % CHOICE ... OR ...
176 % the branches can be arbitrary substitutions (assignments, IF, ...): use the full treatment for each one
177 maplist(get_subst_enabling_condition(IsPrecise),ChoiceList,Res1,BecomesSuchVars1),
178 disjunct_predicates(Res1,Res),
179 append(BecomesSuchVars1,BecomesSuchVars).
180 get_operation_enabling_condition2(var(_Parameters,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % VAR
181 % should the Parameters be added somewhere ? should we create an exists
182 % In principle the enabling condition should be true, as we have a low-level construct
183 get_substitution_enabling_condition(TBody,Res,BecomesSuchVars,IsPrecise).
184 get_operation_enabling_condition2(sequence(TSeq),Res,BecomesSuchVars,IsPrecise) :- !, % sequential composition ;
185 get_enabling_cond_for_sequence(TSeq,Res,BecomesSuchVars,IsPrecise).
186 get_operation_enabling_condition2(lazy_let_subst(ID,ExprOrPred,TBody),Res,BecomesSuchVars,IsPrecise) :- !,
187 get_substitution_enabling_condition(TBody,BodyRes,BecomesSuchVars,IsPrecise),
188 (is_truth(BodyRes) -> Res = BodyRes % the LET is not needed for a guard-free body
189 ; Res = b(lazy_let_pred(ID,ExprOrPred,BodyRes),pred,[])).
190 get_operation_enabling_condition2(let(Parameters,PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % LET
191 % for the moment: treat like ANY, but we could avoid introducing existential quantifier ?!
192 ? get_operation_enabling_condition2(any(Parameters,PreCond,TBody),Res,BecomesSuchVars,IsPrecise).
193 get_operation_enabling_condition2(any(Parameters,PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % ANY
194 get_substitution_enabling_condition(TBody,RC,BecomesSuchVars,IsPrecise),
195 conjunct_predicates_with_pos_info(PreCond,RC,Res1),
196 simplify_enabling_condition(Simplify),
197 ? construct_optimized_exists(Parameters,Res1,Res,Simplify).
198 %get_operation_enabling_condition2(select([b(select_when(PreCond, TBody),_,_)]),Res,BecomesSuchVars) :- !,
199 % get_texpr_expr(TBody,Body),
200 % get_operation_enabling_condition2(Body,RC,BecomesSuchVars),
201 % conjunct_predicates([PreCond,RC],Res).
202 %get_operation_enabling_condition2(select([b(select_when(PreCond, TBody),_,_)],b(skip,_,_)),Res,BecomesSuchVars) :- !,
203 % get_texpr_expr(TBody,Body),
204 % get_operation_enabling_condition2(Body,RC,BecomesSuchVars),
205 % conjunct_predicates([PreCond,RC],Res).
206 get_operation_enabling_condition2(select_when(PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !,
207 get_substitution_enabling_condition(TBody,RC,BecomesSuchVars,IsPrecise),
208 conjunct_predicates_with_pos_info(PreCond,RC,Res).
209 get_operation_enabling_condition2(select(ListOfWhens),Res,BecomesSuchVars,IsPrecise) :- !,
210 get_disjunction_of_enabling_conditions(ListOfWhens,Res,BecomesSuchVars,IsPrecise).
211 get_operation_enabling_condition2(select(ListOfWhens,Else),Res,BecomesSuchVars,IsPrecise) :- !,
212 % SELECT G1 THEN S1 WHEN ... ELSE SE END is enabled iff (G1 & C1) or ... or (not(G1) & ... & not(Gn) & CE),
213 % where Ci is the enabling condition of Si: the ELSE branch can only be taken if no guard is true
214 get_texpr_exprs(ListOfWhens,ListOfSelectWhens),
215 maplist(get_operation_enabling_condition3(IsPrecise),ListOfSelectWhens,Res1,BecomesSuchVars1),
216 get_substitution_enabling_condition(Else,ResElse,ElseBecomesSuchVars,IsPrecise),
217 maplist(get_negated_select_guard,ListOfSelectWhens,NegGuards),
218 append(NegGuards,[ResElse],ElseConds),
219 conjunct_predicates_with_pos_info(ElseConds,ElseRes),
220 append(Res1,[ElseRes],AllRes),
221 disjunct_predicates(AllRes,Res),
222 append([ElseBecomesSuchVars|BecomesSuchVars1],BecomesSuchVars).
223 get_operation_enabling_condition2(parallel([TH|T]),Res,BecomesSuchVars,IsPrecise) :- !,
224 get_substitution_enabling_condition(TH,E1,BecomesSuchVarsH,IsPrecise),
225 (T=[] -> Res=E1, BecomesSuchVarsH=BecomesSuchVars
226 ; get_operation_enabling_condition2(parallel(T),E2,BecomesSuchVarsT,IsPrecise),
227 append(BecomesSuchVarsH,BecomesSuchVarsT,BecomesSuchVars),
228 conjunct_predicates_with_pos_info(E1,E2,Res)
229 ).
230 get_operation_enabling_condition2(becomes_such(Vars,Condition),Res,Vars,_Precise) :- % Vars : (Condition)
231 % example x: (x$0 >= 1 & x=x$0+1)
232 obtain_action_enabling_conditions,!,
233 simplify_enabling_condition(Simplify),
234 ? construct_optimized_exists(Vars,Condition,Res1,Simplify),
235 %translate:print_bexpr(Res1),nl,
236 % e.g, for example above we have #x.(x$0 >= 1 & x=x$0+1) -> x$0 >= 1
237 % now rename $0 variables to act on current state to obtain the condition:
238 findall(rename(BeforeId,Id),
239 (member(b(identifier(Id),_,Infos),Vars),
240 member(before_substitution(_,BeforeId),Infos)),
241 RenameList),
242 rename_bt(Res1,RenameList,Res). % for example above: Res is x >= 1
243 get_operation_enabling_condition2(rlevent(_Name,_Section,_Status,_Params,Guard,_Theorems,Actions,VWitnesses,PWitnesses,_Unmod,AbstractEvents),Res,BecomesSuchVars,IsPrecise) :- !,
244 % Guard theorems and variants are only checked by b_interpreter_eventb: if they do not hold
245 % the event still occurs, with an error; hence they are not part of the enabling condition.
246 % Abstract guards which are not repeated in the refinement and witnesses which cannot be satisfied
247 % prevent the event from occurring (simulation_error, no_witness_found); we do not include them, as they
248 % may refer to abstract variables and parameters, but mark the result as imprecise:
249 % if guard strengthening and witness feasibility are assumed to be proven, the guard is precise
250 (assume_eventb_refinement_correct -> true
251 ; eventb_refinement_can_block(VWitnesses,PWitnesses,AbstractEvents) -> IsPrecise = imprecise
252 ; true),
253 (obtain_action_enabling_conditions
254 -> get_operation_enabling_for_event_b_actions(Actions,Guard,Res,BecomesSuchVars,IsPrecise)
255 ; Res=Guard,BecomesSuchVars=[]).
256 get_operation_enabling_condition2(while(Cond,_,_,_),Truth,[],imprecise) :- !,
257 debug_println(9,enabling_for_while_assumed_true),
258 get_texpr_pos_infos(Cond,PosInfos),
259 create_texpr(truth,pred,PosInfos,Truth).
260 get_operation_enabling_condition2(external_subst_call(Pred,Args),Res,[],_Precise) :-
261 external_subst_enabling_condition(Pred,Args,Cond),!, Res=Cond.
262 get_operation_enabling_condition2(operation_call(Operation,OpCallResults,OpCallParas),Res,BecomeSuchVars,IsPrecise) :-
263 def_get_texpr_id(Operation,op(OperationName)), TopLevel=false,
264 b_get_machine_operation_for_animation(OperationName,OpResults,OpParameters,Body,_OType,TopLevel),
265 bsyntaxtree:replace_ids_by_exprs(Body,OpParameters,OpCallParas,Body2),
266 (b_operation_reads_output_variables(OperationName,[_|_],_)
267 -> bsyntaxtree:replace_ids_by_exprs(Body2,OpResults,OpCallResults,Body3)
268 ; Body3=Body2
269 ),
270 !,
271 %print(get_enabling_for_opcall),nl,translate:print_subst(Body2),nl,
272 get_substitution_enabling_condition(Body3,Res,BecomeSuchVars,IsPrecise).
273
274 % check if an substitution obviously has no guard/pre condition
275 basic_has_no_enabling_condition(assign(_LHS,_RHS)).
276 basic_has_no_enabling_condition(assign_single_id(_LHS,_RHS)).
277 basic_has_no_enabling_condition(skip).
278
279 % recursive version; also traversing other constructs
280 has_no_enabling_condition(E) :- basic_has_no_enabling_condition(E).
281 has_no_enabling_condition(parallel(L)) :- maplist(has_no_enabling_t_cond,L).
282 has_no_enabling_condition(sequence(L)) :- maplist(has_no_enabling_t_cond,L).
283 has_no_enabling_condition(choice(L)) :- maplist(has_no_enabling_t_cond,L).
284 has_no_enabling_condition(block(T)) :- has_no_enabling_t_cond(T).
285 has_no_enabling_condition(var(_,T)) :- has_no_enabling_t_cond(T).
286 has_no_enabling_condition(if(L)) :- maplist(has_no_enabling_ifte_cond,L).
287 % TODO: SELECT WITH ELSE, becomes_such with full type, certain external_subst_call, operation_call
288
289 has_no_enabling_t_cond(b(E,_,_)) :- !, has_no_enabling_condition(E).
290 has_no_enabling_t_cond(E) :- add_internal_error('Cond not wrappred:',has_no_enabling_t_cond(E)),
291 has_no_enabling_condition(E).
292 has_no_enabling_ifte_cond(b(if_elsif(_Test,TBody),_,_)) :- has_no_enabling_t_cond(TBody).
293
294
295 get_enabling_cond_for_sequence([],b(truth,pred,[]),[],_Precise).
296 get_enabling_cond_for_sequence([b(assign_single_id(LHS,RHS),subst,_)|T],Res,BecomesSuchVars,IsPrecise) :-
297 get_texpr_id(LHS,ID),
298 !, % TODO: also support f(X) := ...
299 get_enabling_cond_for_sequence(T,TRes,BecomesSuchVars,IsPrecise),
300 replace_id_by_expr(TRes,ID,RHS,Res).
301 get_enabling_cond_for_sequence([b(assign(LHS,RHS),subst,_)|T],Res,BecomesSuchVars,IsPrecise) :-
302 get_texpr_ids(LHS,IDs),
303 !, % TODO: also support f(X), := ...
304 get_enabling_cond_for_sequence(T,TRes,BecomesSuchVars,IsPrecise),
305 replace_ids_by_exprs(TRes,IDs,RHS,Res).
306 get_enabling_cond_for_sequence([TBody1|Tail],Res,BecomesSuchVars,IsPrecise) :-
307 get_substitution_enabling_condition(TBody1,Res,BecomesSuchVars,IsPrecise),
308 (nonvar(IsPrecise) -> true
309 ; maplist(has_no_enabling_t_cond,Tail) -> true
310 ; IsPrecise = imprecise
311 ),
312 (debug_level_active_for(19), nonvar(IsPrecise), Tail = [Tail1|_]
313 -> add_message(b_operation_guards,'Ignoring potential enabling condition in sequence after: ',TBody1,Tail1)
314 ; true).
315 % TO DO: compute before-after predicate of TBody1 ? before_after_predicate_list_conjunct_with_equalities ?
316
317 get_if_condition(IsPrecise,b(if_elsif(Test,TBody),_,_),Test,Condition,BecomesSuchVars) :-
318 get_substitution_enabling_condition(TBody,Condition,BecomesSuchVars,IsPrecise).
319
320 :- use_module(bsyntaxtree, [create_implication/3]).
321 disjoin_ifs([],[],_,[]).
322 disjoin_ifs([Test|TT],[EnableCond|TC],NegSoFar,[Res1|TR]) :-
323 append(NegSoFar,[Test],L),
324 conjunct_predicates_with_pos_info(L,BranchTest),
325 create_implication(BranchTest,EnableCond,Res1), % ELSEIF Test THEN Body ... ---> NegSoFar & Test => EnableCond
326 create_negation(Test,NTest),
327 append(NegSoFar,[NTest],NegSoFar1), % add negation of test as additional test for rest
328 disjoin_ifs(TT,TC,NegSoFar1,TR).
329
330
331 % for a list of substitutions: get enabling conditions and disjoin them
332 get_disjunction_of_enabling_conditions(ListOfWhens,Res,BecomesSuchVars,IsPrecise) :-
333 get_texpr_exprs(ListOfWhens,ListOfSelectWhens),
334 maplist(get_operation_enabling_condition3(IsPrecise),ListOfSelectWhens,Res1,BecomesSuchVars1),
335 disjunct_predicates(Res1,Res),
336 append(BecomesSuchVars1,BecomesSuchVars).
337 get_operation_enabling_condition3(Precise,Exp,Res,BV) :- get_operation_enabling_condition2(Exp,Res,BV,Precise).
338 :- use_module(self_check).
339 % test substitutions: X>0, Y>0, x:=1, x:=2, skip
340 test_subst(gx,b(greater(b(identifier(x),integer,[]),b(integer(0),integer,[])),pred,[])).
341 test_subst(gy,b(greater(b(identifier(y),integer,[]),b(integer(0),integer,[])),pred,[])).
342 test_subst(x1,b(assign([b(identifier(x),integer,[])],[b(integer(1),integer,[])]),subst,[])).
343 test_subst(x2,b(assign([b(identifier(x),integer,[])],[b(integer(2),integer,[])]),subst,[])).
344 test_subst(skip,b(skip,subst,[])).
345 % CHOICE x:=1 OR x:=2 END is always enabled, and this is precise
346 :- assert_must_succeed((b_operation_guards:test_subst(x1,A1), b_operation_guards:test_subst(x2,A2),
347 get_substitution_enabling_condition(b(choice([A1,A2]),subst,[]),Res,[],Precise),
348 is_truth(Res), var(Precise))).
349 % CHOICE x:=1 OR SELECT y>0 THEN skip END END
350 :- assert_must_succeed((b_operation_guards:test_subst(x1,A1), b_operation_guards:test_subst(gy,GY),
351 b_operation_guards:test_subst(skip,Skip),
352 get_substitution_enabling_condition(b(choice([A1,b(select([b(select_when(GY,Skip),subst,[])]),subst,[])]),subst,[]),
353 Res,[],Precise),
354 is_truth(Res), var(Precise))).
355 % BEGIN x :: {1,2} ; IF y>0 THEN x:=1 ELSE x:=2 END END: the IF in the sequence has no enabling condition
356 :- assert_must_succeed((b_operation_guards:test_subst(x1,A1), b_operation_guards:test_subst(x2,A2),
357 b_operation_guards:test_subst(gy,GY),
358 X = b(identifier(x),integer,[]), One=b(integer(1),integer,[]), Two=b(integer(2),integer,[]),
359 BecomesElem = b(becomes_element_of([X],b(set_extension([One,Two]),set(integer),[])),subst,[]),
360 If = b(if([b(if_elsif(GY,A1),subst,[]),b(if_elsif(b(truth,pred,[]),A2),subst,[])]),subst,[]),
361 get_substitution_enabling_condition(b(sequence([BecomesElem,If]),subst,[]),_Res,_,Precise),
362 var(Precise))).
363 % the flags set by get_operation_enabling_condition/7 do not influence later direct calls:
364 % x :: BOOL is enabled, and this is precise if conditions from actions are obtained (the default)
365 test_subst(bbool,b(becomes_element_of([b(identifier(x),boolean,[])],b(bool_set,set(boolean),[])),subst,[])).
366 :- assert_must_succeed((\+ get_operation_enabling_condition('$no_such_operation',_,_,_,_,false,false),
367 b_operation_guards:test_subst(bbool,S),
368 get_substitution_enabling_condition(S,Res,_,Precise), is_truth(Res), var(Precise))).
369 :- assert_must_succeed((b_operation_guards:with_enabling_flags(false,false,true),
370 b_operation_guards:test_subst(bbool,S),
371 get_substitution_enabling_condition(S,Res,_,Precise), is_truth(Res), var(Precise))).
372 :- assert_must_succeed((b_operation_guards:with_enabling_flags(false,true,
373 (b_operation_guards:test_subst(bbool,S), get_substitution_enabling_condition(S,_,_,Precise))),
374 Precise==imprecise)).
375 % SELECT x>0 THEN SELECT y>0 THEN skip END ELSE skip END is enabled iff (x>0 & y>0) or not(x>0)
376 :- assert_must_succeed((b_operation_guards:test_subst(gx,GX), b_operation_guards:test_subst(gy,GY),
377 b_operation_guards:test_subst(skip,Skip),
378 Inner = b(select([b(select_when(GY,Skip),subst,[])]),subst,[]),
379 get_substitution_enabling_condition(b(select([b(select_when(GX,Inner),subst,[])],Skip),subst,[]),Res,[],Precise),
380 var(Precise),
381 Res = b(disjunct(b(conjunct(G1,G2),pred,_),b(negation(G3),pred,_)),pred,_),
382 G1==GX, G2==GY, G3==GX)).
383 % a lazy LET with a guard-free body has the enabling condition btrue
384 :- assert_must_succeed((b_operation_guards:test_subst(skip,Skip),
385 Z=b(identifier(z),integer,[]), One=b(integer(1),integer,[]),
386 get_substitution_enabling_condition(b(lazy_let_subst(Z,One,Skip),subst,[]),Res,[],Precise),
387 is_truth(Res), var(Precise))).
388 % refined Event-B events: abstract guards and non-deterministic witnesses make the guard imprecise
389 test_event(Grd,VWit,AbsEvents,b(rlevent(ev,sec,b(ordinary,status,[]),[],Grd,[],[],VWit,[],[],AbsEvents),subst,[])).
390 :- assert_must_succeed((b_operation_guards:test_subst(gx,GX), b_operation_guards:test_subst(gy,GY),
391 b_operation_guards:test_event(b(truth,pred,[]),[],[],AbsEv),
392 b_operation_guards:test_event(GX,[],[AbsEv],Ev),
393 get_substitution_enabling_condition(Ev,Res,[],Precise), Res==GX, var(Precise),
394 b_operation_guards:test_event(GY,[],[],AbsEv2),
395 b_operation_guards:test_event(GX,[],[AbsEv2],Ev2),
396 get_substitution_enabling_condition(Ev2,Res2,[],Precise2), Res2==GX, Precise2==imprecise)).
397 :- assert_must_succeed((b_operation_guards:test_subst(gx,GX),
398 Z=b(identifier(z),integer,[]), X=b(identifier(x),integer,[]),
399 W1 = b(witness(Z,b(equal(Z,X),pred,[])),witness,[]),
400 W2 = b(witness(Z,b(greater(Z,X),pred,[])),witness,[]),
401 b_operation_guards:test_event(b(truth,pred,[]),[],[],AbsEv),
402 b_operation_guards:test_event(GX,[W1],[AbsEv],Ev1),
403 get_substitution_enabling_condition(Ev1,_,[],Precise1), var(Precise1),
404 b_operation_guards:test_event(GX,[W2],[AbsEv],Ev2),
405 get_substitution_enabling_condition(Ev2,_,[],Precise2), Precise2==imprecise)).
406 % ... unless the refinement is assumed to be correct
407 :- assert_must_succeed((b_operation_guards:test_subst(gx,GX), b_operation_guards:test_subst(gy,GY),
408 b_operation_guards:test_event(GY,[],[],AbsEv),
409 b_operation_guards:test_event(GX,[],[AbsEv],Ev),
410 with_eventb_refinement_assumed_correct(get_substitution_enabling_condition(Ev,Res,[],Precise)),
411 Res==GX, var(Precise),
412 \+ b_operation_guards:assume_eventb_refinement_correct)).
413 get_subst_enabling_condition(Precise,TSubst,Res,BV) :- get_substitution_enabling_condition(TSubst,Res,BV,Precise).
414 get_negated_select_guard(select_when(Guard,_),NegGuard) :- create_negation(Guard,NegGuard).
415
416 % see more powerful definitely_not_empty_set
417 definitely_not_empty(bool_set,_).
418 definitely_not_empty(integer_set(_),_).
419 definitely_not_empty(float_set,_).
420 definitely_not_empty(real_set,_).
421 definitely_not_empty(identifier(X),set(global(X))). % assume no local version of X overrides global X
422 definitely_not_empty(string_set,_).
423
424
425
426 % get operation enabling condition in context of an event-b action list:
427 get_operation_enabling_for_event_b_actions([],Res,Res,[],_Precise).
428 get_operation_enabling_for_event_b_actions([H|T],InRes,OutRes,BecomesSuchVars,IsPrecise) :-
429 get_substitution_enabling_condition(H,ResH,HBecomesSuchVars,IsPrecise),
430 conjunct_predicates_with_pos_info(InRes,ResH,IntRes),
431 get_operation_enabling_for_event_b_actions(T,IntRes,OutRes,TBecomesSuchVars,IsPrecise),
432 append(HBecomesSuchVars,TBecomesSuchVars,BecomesSuchVars).
433
434 % check if the abstract events (whose guards repeated in the refinement have been removed by bmachine_eventb)
435 % or the witnesses can prevent a refined Event-B event from occurring
436 eventb_refinement_can_block(VWitnesses,PWitnesses,_) :-
437 ? (member(W,VWitnesses) ; member(W,PWitnesses)),
438 \+ deterministic_witness(W), !.
439 eventb_refinement_can_block(_,_,AbstractEvents) :-
440 ? member(b(rlevent(_,_,_,_,AbsGuard,_,_,AVWit,APWit,_,AbsAbsEvents),_,_),AbstractEvents),
441 (is_truth(AbsGuard) -> eventb_refinement_can_block(AVWit,APWit,AbsAbsEvents) ; true),
442 !.
443
444 % a witness of the form x = E, where x does not occur in E, can always be satisfied
445 deterministic_witness(b(witness(TId,Pred),_,_)) :-
446 get_texpr_id(TId,Id),
447 get_texpr_expr(Pred,equal(L,R)),
448 (get_texpr_id(L,Id) -> E=R ; get_texpr_id(R,Id) -> E=L),
449 find_identifier_uses(E,[],Used),
450 \+ ord_member(Id,Used).
451
452 % ---------------
453 :- use_module(bmachine,[b_top_level_operation/1]).
454 % LTSMIN style guards: a guard that does not depend on parameters
455 get_operation_propositional_guards(OpName,Guards,RestBody) :-
456 b_top_level_operation(OpName),
457 b_get_machine_operation_for_animation(OpName,TResults,TParameters,TBody), % requires bmachine to be precompiled
458 get_operation_propositional_guards(OpName,TResults,TParameters,TBody,Guards,RestBody).
459
460 :- use_module(translate,[print_bexpr/1]).
461 % the following can be called directly; does not require bmachine to be pre-compiled
462 % ANY p WHERE x>0 THEN y := p END: the guard does not depend on p, but the ANY must be kept for the body
463 :- assert_must_succeed((P=b(identifier(p),integer,[]), Y=b(identifier(y),integer,[]),
464 b_operation_guards:test_subst(gx,GX),
465 get_operation_propositional_guards(op,[],[],b(any([P],GX,b(assign([Y],[P]),subst,[])),subst,[]),Guards,RestBody),
466 Guards==[GX], RestBody = b(any([P2],Truth,_),subst,_), P2==P, is_truth(Truth))).
467 % ANY p WHERE p=x & p/=0 THEN SELECT 10/x>1 & y>0 THEN y := p END END:
468 % 10/x>1 relies on the dependent guard p/=0 and must not be moved out; y>0 is always well-defined
469 :- assert_must_succeed((P=b(identifier(p),integer,[]), X=b(identifier(x),integer,[]), Y=b(identifier(y),integer,[]),
470 b_operation_guards:test_subst(gy,GY),
471 Zero=b(integer(0),integer,[]), One=b(integer(1),integer,[]), Ten=b(integer(10),integer,[]),
472 PX = b(equal(P,X),pred,[]), PNZ = b(not_equal(P,Zero),pred,[]),
473 DivG = b(greater(b(div(Ten,X),integer,[contains_wd_condition]),One),pred,[contains_wd_condition]),
474 get_operation_propositional_guards(op,[],[],
475 b(any([P],b(conjunct(PX,PNZ),pred,[]),
476 b(select([b(select_when(b(conjunct(DivG,GY),pred,[]),b(assign([Y],[P]),subst,[])),subst,[])]),subst,[])),subst,[]),
477 Guards,_RestBody),
478 Guards==[GY])).
479 get_operation_propositional_guards(OpName,TResults,TParameters,TBody,Guards,RestBody) :-
480 get_texpr_ids(TParameters,Ids1),
481 get_texpr_ids(TResults,Ids2),
482 append(Ids1,Ids2,Ids), sort(Ids,Parameters),
483 get_operation_guards_aux(TBody,Parameters,top,at_front,Guards,RestBody),
484 (debug_mode(off) -> true
485 ; format('OPERATION Guard Splitting ~w (~w)~n',[OpName,Parameters]),
486 print('LTSMin Guards: '), maplist(print_bexpr,Guards),nl,
487 print('LTSMin Body: '), translate:print_subst(RestBody),nl,nl
488 ).
489
490 :- use_module(bsyntaxtree, [conjunction_to_list/2,find_identifier_uses/3]).
491
492
493 % AtFront is at_front if all guards of the enclosing levels have been moved out;
494 % otherwise inner guards are only moved out if they are always well-defined,
495 % as they may rely on a dependent guard which is kept in the body
496 get_operation_guards_aux(Subst,Parameters,Top,AtFront,Guards,OpBody) :-
497 ? get_guards(Subst,Top,TIds,Guard,InnerBody,Infos),
498 get_texpr_ids(TIds,Ids), sort(Ids,NewIds),
499 ord_union(Parameters,NewIds,Parameters2),
500 !,
501 get_parameter_independent_guards(Guard,Parameters2,AtFront,Indep,Dep,AtFront2),
502 (Dep = [], TIds = []
503 -> OpBody = InnerOpBody % no need to keep b
504 ; conjunct_predicates_with_pos_info(Dep,DepCond),
505 construct_select(TIds,DepCond,InnerOpBody,Infos,OpBody) % we always produce a SELECT; even if we had a PRE as it will no longer be innermost ! we assume treat_outermost_pre_as_select is set to true for PRE (checked below)
506 ),
507 get_operation_guards_aux(InnerBody,Parameters2,inner,AtFront2,InnerGuards,InnerOpBody),
508 append(Indep,InnerGuards,Guards).
509
510 get_operation_guards_aux(b(rlevent(Name,Sect,Status,Params,EvGuard,Theorems,Act,VWit,PWit,Unmod,AbsEvts),subst,Info),
511 Parameters,_,AtFront,Guards,OpBody) :- !,
512 get_parameter_independent_guards(EvGuard,Parameters,AtFront,InDepGuards,Dep,_),
513 (get_variant_pred(Status,VariantPred)
514 -> %print(op_variant(_Name)),nl,translate:print_bexpr(VariantPred),nl,
515 % we virtually include the Variant expression in the read info; ensure that LTS Min knows that this will be read by the Event-B interpreter
516 % TO DO: check if we need to add the witnesses as well !
517 Guards = [VariantPred|InDepGuards]
518 ; Guards = InDepGuards),
519 conjunct_predicates_with_pos_info(Dep,DepG),
520 OpBody = b(rlevent(Name,Sect,Status,Params,DepG,Theorems,Act,VWit,PWit,Unmod,AbsEvts),subst,Info).
521 get_operation_guards_aux(TB,_,_,_,[],TB).
522
523
524 :- use_module(bsyntaxtree, [safe_create_texpr/3]).
525 % we integrate the Variant check into the guard to ensure the correct read matrix is produced
526 % currently the Variant is checked upon event entry and for convergent events upon exit for decrease
527 % in principle this should be more of an assertion_predicate or assertion_expression
528 get_variant_pred(b(Status,status,_),Res) :- get_variant_pred_aux(Status,Res).
529 get_variant_pred_aux(anticipated(Variant),Res) :- NATURAL = b(integer_set('NATURAL'),set(integer),[]),
530 safe_create_texpr(member(Variant,NATURAL),pred,Res).
531 get_variant_pred_aux(convergent(Variant),Res) :- NATURAL1 = b(integer_set('NATURAL1'),set(integer),[]),
532 safe_create_texpr(member(Variant,NATURAL1),pred,Res).
533
534
535 get_guards(b(precondition(Guard,TBody),subst,Info), top, [],Guard, TBody, Info) :-
536 preferences:get_preference(treat_outermost_pre_as_select,true).
537 get_guards(b(select([b(select_when(Guard, TBody),subst,_Info1)]),subst,Info2), _, [],Guard, TBody, Info2).
538 % TO DO: for ANY try and extract propositional parts, e.g., for ANY pp WHERE pp:1..xx & z=1/yy THEN …
539 get_guards(b(any(TIds,Guard,TBody),subst,Info),_, TIds,Guard,TBody,Info).
540 get_guards(b(operation_call(Operation,[],Parameters),subst,Info),_, InnerParas,Guard, TBody, Info) :-
541 Parameters=[], % TO DO: improve, will require substituting Parameters inside OpRealBody
542 bmachine_is_precompiled,
543 def_get_texpr_id(Operation,op(OpName)),
544 b_get_machine_operation(OpName,[],RealParameters,OpRealBody,_OType,_OpPos), RealParameters=[],
545 get_guards(OpRealBody, inner, InnerParas, Guard, TBody, _ ).
546
547 construct_select([],Guard,TBody, Infos, Res) :- !,
548 Res = b(select([b(select_when(Guard, TBody),subst,Infos)]),subst,Infos).
549 construct_select(TIds,Guard,TBody,Infos, b(any(TIds,Guard,TBody),subst,Infos)).
550
551
552 % AtFront0: whether all guards before Guard have been moved out; AtFront: same after Guard
553 get_parameter_independent_guards(Guard,Parameters,AtFront0,Indep,Dep,AtFront) :-
554 conjunction_to_list(Guard,Gs),
555 l_get_parameter_independent_guards(Gs,Parameters,AtFront0,Indep,Dep,AtFront).
556
557 :- use_module(bsyntaxtree,[always_well_defined/1]).
558 l_get_parameter_independent_guards([],_,AtFront,[],[],AtFront).
559 l_get_parameter_independent_guards([G|Gs],Parameters,AtFront,Indep,Dep,AtFrontRes) :-
560 find_identifier_uses(G,[],Ids),
561 ( ord_disjoint(Ids,Parameters),
562 (AtFront=at_front -> true ; always_well_defined(G))
563 -> Indep=[G|I1], Dep=D1, AtFront1=AtFront
564 ; Indep=I1, Dep=[G|D1],
565 AtFront1=not_at_front % we have skipped one guard; the next guard is not guaranteed to be at the front (relevant for well-definedness)
566 ),
567 l_get_parameter_independent_guards(Gs,Parameters,AtFront1,I1,D1,AtFrontRes).
568
569 % for synthesis of sequential programs
570 % not yet used
571 /*
572 % find events which have common guards
573 get_common_guards(Ev1,Ev2,Common,Rest1,Rest2) :-
574 get_unsimplified_operation_guard(Ev1,G1), conjunction_to_list(G1,GL1),
575 get_unsimplified_operation_guard(Ev2,G2), Ev1 @< Ev2, conjunction_to_list(G2,GL2),
576 find_common(GL1,GL2,Common,Rest1,Rest2),
577 format('~nCommon guard for ~w <-> ~w~n Common: ',[Ev1,Ev2]), translate:l_print_bexpr_or_subst(Common),nl,
578 format(' Rest1: ',[]),translate:l_print_bexpr_or_subst(Rest1),nl,
579 format(' Rest2: ',[]),translate:l_print_bexpr_or_subst(Rest2),nl.
580
581 % check if abrial merge rule 15.3 from Bee-Book for if/while is applicable:
582 get_abrial_rule(Ev1,Ev2,Common,Rest1) :-
583 get_common_guards(Ev1,Ev2,Common,Rest1,Rest2),
584 conjunct_predicates(Rest1,R1),
585 conjunct_predicates(Rest2,R2),
586 is_negated_predicate(R1,R2),
587 format('*** Can be merged ~w <-> ~w !~n~n',[Ev1,Ev2]).
588
589 find_common([],GL2,[],[],GL2).
590 find_common([G1|T1],GL2,[G1|TCommon],Rest1,Rest2) :-
591 select(G2,GL2,T2), % TODO: check WD
592 same_predicate(G1,G2),!, % we could also use norm_expr
593 find_common(T1,T2,TCommon,Rest1,Rest2).
594 find_common([G1|T1],GL2,TCommon,[G1|Rest1],Rest2) :-
595 find_common(T1,GL2,TCommon,Rest1,Rest2).
596
597 same_predicate(G1,G2) :- bsyntaxtree:same_texpr(G1,G2).
598 same_predicate(G1,G2) :- b_interpreter_check:norm_pred_check(G1,N1), b_interpreter_check:norm_pred_check(G2,N2), N2=N1.
599
600 is_negated_predicate(G1,G2) :- bsyntaxtree:is_negation_of(G1,G2).
601 */
602