1 % (c) 2009-2024 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/4, % 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
17 :- use_module(module_information,[module_info/2]).
18 :- module_info(group,ast).
19 :- module_info(description,'Compute guard predicates of operations.').
20
21
22 :- use_module(tools).
23 :- use_module(bsyntaxtree).
24 :- use_module(bmachine,[b_top_level_operation/1,
25 b_get_machine_operation_for_animation/6, b_get_machine_operation/6, bmachine_is_precompiled/0]).
26 :- use_module(b_interpreter_components,[construct_optimized_exists/3,construct_optimized_exists/4]).
27 :- use_module(specfile,[animation_minor_mode/1]).
28 :- use_module(external_functions,[external_subst_enabling_condition/3]).
29 :- use_module(b_ast_cleanup, [clean_up/3]).
30 :- use_module(debug).
31 :- use_module(error_manager).
32
33 :- use_module(library(lists)).
34
35
36
37 :- dynamic obtain_action_enabling_conditions/0, simplify_enabling_condition/1.
38 simplify_enabling_condition(true).
39 set_simplify_enabling_condition(X) :- retractall(simplify_enabling_condition(_)),
40 assertz(simplify_enabling_condition(X)).
41
42 obtain_action_enabling_conditions.
43 set_obtain_action_enabling_conditions(auto_obtain) :- !,
44 (animation_minor_mode(eventb) -> GetFromAction=false
45 ; GetFromAction=true), set_obtain_action_enabling_conditions(GetFromAction).
46 set_obtain_action_enabling_conditions(true) :- !,
47 (obtain_action_enabling_conditions -> true ; assertz(obtain_action_enabling_conditions)).
48 set_obtain_action_enabling_conditions(_) :- retractall(obtain_action_enabling_conditions).
49
50
51 get_unsimplified_operation_guard(OpName,Guard) :-
52 get_unsimplified_operation_enabling_condition(OpName,Parameters,EnablingCondition,_BecomesSuchVars,_Precise),
53 construct_optimized_exists(Parameters,EnablingCondition,Guard).
54
55 get_unsimplified_operation_enabling_condition(OpName,Parameters,EnablingCondition,BecomesSuchVars,Precise) :-
56 Simplify=false,
57 ? get_operation_enabling_condition(OpName,Parameters,EnablingCondition,
58 BecomesSuchVars,Precise,auto_obtain,Simplify).
59
60 get_simplified_operation_enabling_condition(OpName,Parameters,EnablingCondition,BecomesSuchVars,Precise) :-
61 Simplify=true,
62 get_operation_enabling_condition(OpName,Parameters,EnablingCondition,
63 BecomesSuchVars,Precise,auto_obtain,Simplify).
64
65
66 % get a quantified predicate
67 get_quantified_operation_enabling_condition(OpName, BExpr, BecomesSuchVars,Simplify) :-
68 get_operation_enabling_condition(OpName,IDs,Condition,
69 BecomesSuchVars,_Precise,auto_obtain,Simplify),
70 create_or_merge_exists(IDs, Condition, BExpr).
71
72
73 get_operation_enabling_condition(OpName,Parameters,EnablingCondition,BecomesSuchVars,IsPrecise,GetAct,Simplify) :-
74 set_obtain_action_enabling_conditions(GetAct), % specify whether we want also conditions stemming from actions, such as :() or ::
75 set_simplify_enabling_condition(Simplify), % specify whether we want to simplify the EnablingCondition if possible
76 %%nonvar(OpName), %% I am not sure why this test is here ??
77 %% print(getting_enabling(OpName,GetAct)),nl,%%
78 ? b_get_machine_operation_for_animation(OpName,_Results,Parameters,TBody,_OType,true),
79 get_operation_enabling_condition1(TBody,EnablingCondition,BecomesSuchVars,IsPreciseV),
80 (var(IsPreciseV) -> IsPrecise = precise ; IsPrecise = imprecise).
81
82 get_operation_enabling_condition1(TBody,EnablingCondition,BecomesSuchVars,IsPrecise) :-
83 get_texpr_expr(TBody,Body),
84 (get_operation_enabling_condition2(Body,EnablingCondition,BecomesSuchVars,IsPrecise) -> true
85 ; add_internal_error('Getting enabling condition failed: ',Body),
86 create_texpr(truth,pred,[],EnablingCondition),
87 BecomesSuchVars=[]).
88
89 /* TO DO: recursively expand the code below for more complicated SELECT,ANY,... */
90 get_operation_enabling_condition2(precondition(PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % PRE
91 get_operation_enabling_condition1(TBody,RC,BecomesSuchVars,IsPrecise),
92 conjunct_predicates_with_pos_info(PreCond,RC,Res).
93 get_operation_enabling_condition2(assertion(_PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % ASSERT
94 get_operation_enabling_condition1(TBody,Res,BecomesSuchVars,IsPrecise).
95 get_operation_enabling_condition2(block(TBody),Res,BecomesSuchVars,IsPrecise) :- !, % BEGIN ... END
96 get_operation_enabling_condition1(TBody,Res,BecomesSuchVars,IsPrecise).
97 get_operation_enabling_condition2(choice(ChoiceList),Res,BecomesSuchVars,IsPrecise) :- !, % CHOICE ... OR ...
98 get_disjunction_of_enabling_conditions(ChoiceList,Res,BecomesSuchVars,IsPrecise).
99 get_operation_enabling_condition2(var(_Parameters,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % VAR
100 % should the Parameters be added somewhere ? In principle the enabling condition should always be true, as we have a low-level construct
101 get_operation_enabling_condition1(TBody,Res,BecomesSuchVars,IsPrecise).
102 get_operation_enabling_condition2(sequence([TBody1|Tail]),Res,BecomesSuchVars,IsPrecise) :- !, % ;
103 get_operation_enabling_condition1(TBody1,Res,BecomesSuchVars,IsPrecise),
104 (nonvar(IsPrecise) -> true
105 ; maplist(has_no_enabling_condition,Tail) -> true
106 ; IsPrecise = imprecise
107 ),
108 (debug_mode(on), nonvar(IsPrecise)
109 -> print(ignoring_potential_enabling_condition_in_tail_of_sequence),nl ; true).
110 % TO DO: compute before-after predicate of TBody1 ? before_after_predicate_list_conjunct_with_equalities ?
111 get_operation_enabling_condition2(lazy_let_subst(ID,ExprOrPred,TBody),Res,BecomesSuchVars,IsPrecise) :- !,
112 Res = b(lazy_let_pred(ID,ExprOrPred,BodyRes),pred,[]),
113 get_operation_enabling_condition1(TBody,BodyRes,BecomesSuchVars,IsPrecise).
114 get_operation_enabling_condition2(let(Parameters,PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % LET
115 % for the moment: treat like ANY, but we could avoid introducing existential quantifier ?!
116 get_operation_enabling_condition2(any(Parameters,PreCond,TBody),Res,BecomesSuchVars,IsPrecise).
117 get_operation_enabling_condition2(any(Parameters,PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !, % ANY
118 get_operation_enabling_condition1(TBody,RC,BecomesSuchVars,IsPrecise),
119 conjunct_predicates_with_pos_info(PreCond,RC,Res1),
120 simplify_enabling_condition(Simplify),
121 construct_optimized_exists(Parameters,Res1,Res,Simplify).
122 %get_operation_enabling_condition2(select([b(select_when(PreCond, TBody),_,_)]),Res,BecomesSuchVars) :- !,
123 % get_texpr_expr(TBody,Body),
124 % get_operation_enabling_condition2(Body,RC,BecomesSuchVars),
125 % conjunct_predicates([PreCond,RC],Res).
126 %get_operation_enabling_condition2(select([b(select_when(PreCond, TBody),_,_)],b(skip,_,_)),Res,BecomesSuchVars) :- !,
127 % get_texpr_expr(TBody,Body),
128 % get_operation_enabling_condition2(Body,RC,BecomesSuchVars),
129 % conjunct_predicates([PreCond,RC],Res).
130 get_operation_enabling_condition2(select_when(PreCond,TBody),Res,BecomesSuchVars,IsPrecise) :- !,
131 get_operation_enabling_condition1(TBody,RC,BecomesSuchVars,IsPrecise),
132 conjunct_predicates_with_pos_info(PreCond,RC,Res).
133 get_operation_enabling_condition2(select(ListOfWhens),Res,BecomesSuchVars,IsPrecise) :- !,
134 get_disjunction_of_enabling_conditions(ListOfWhens,Res,BecomesSuchVars,IsPrecise).
135 get_operation_enabling_condition2(select(ListOfWhens,Else),Res,BecomesSuchVars,IsPrecise) :- !,
136 get_texpr_exprs(ListOfWhens,ListOfSelectWhens),
137 maplist(get_operation_enabling_condition3(IsPrecise),ListOfSelectWhens,Res1,BecomesSuchVars1),
138 get_operation_enabling_condition1(Else,ResElse,ElseBecomesSuchVars,IsPrecise),
139 disjunct_predicates([ResElse|Res1],Res),
140 append([ElseBecomesSuchVars|BecomesSuchVars1],BecomesSuchVars).
141 get_operation_enabling_condition2(parallel([TH|T]),Res,BecomesSuchVars,IsPrecise) :- !,
142 get_operation_enabling_condition1(TH,E1,BecomesSuchVarsH,IsPrecise),
143 (T=[] -> Res=E1, BecomesSuchVarsH=BecomesSuchVars
144 ; get_operation_enabling_condition2(parallel(T),E2,BecomesSuchVarsT,IsPrecise),
145 append(BecomesSuchVarsH,BecomesSuchVarsT,BecomesSuchVars),
146 conjunct_predicates_with_pos_info(E1,E2,Res)
147 ).
148 get_operation_enabling_condition2(becomes_element_of(_LHS,RHS),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 -> %print(def_not_empty(RHSExpr)),nl,
154 create_texpr(truth,pred,[],Res)
155 ; create_texpr(empty_set,Type,[],EmptySet),
156 safe_create_texpr(not_equal(RHS,EmptySet),pred,Res2),
157 %print(not_equal_empty(RHSExpr,Type)),nl,
158 clean_up(Res2,[],Res)
159 ).
160 get_operation_enabling_condition2(becomes_such(Vars,Condition),Res,Vars,_Precise) :- % Vars : (Condition)
161 % example x: (x$0 >= 1 & x=x$0+1)
162 obtain_action_enabling_conditions,!,
163 simplify_enabling_condition(Simplify),
164 construct_optimized_exists(Vars,Condition,Res1,Simplify),
165 %translate:print_bexpr(Res1),nl,
166 % e.g, for example above we have #x.(x$0 >= 1 & x=x$0+1) -> x$0 >= 1
167 % now rename $0 variables to act on current state to obtain the condition:
168 findall(rename(BeforeId,Id),
169 (member(b(identifier(Id),_,Infos),Vars),
170 member(before_substitution(_,BeforeId),Infos)),
171 RenameList),
172 rename_bt(Res1,RenameList,Res). % for example above: Res is x >= 1
173 get_operation_enabling_condition2(rlevent(_Name,_Section,_Status,_Params,Guard,_Theorems,Actions,_VWitnesses,_PWitnesses,_Unmod,_AbstractEvents),Res,BecomesSuchVars,IsPrecise) :- !,
174 %print(actions(Actions)),nl,
175 % TO DO: have a look at get_full_eventb_guard. Do we want to recurse through the abstractions ?
176 (obtain_action_enabling_conditions
177 -> get_operation_enabling_for_event_b_actions(Actions,Guard,Res,BecomesSuchVars,IsPrecise)
178 ; Res=Guard,BecomesSuchVars=[]).
179 get_operation_enabling_condition2(case(A,_,_),Truth,[],imprecise) :- !, % CASE is now translated to LET + IF-THEN-ELSE in b_ast_cleanup
180 print('Not computing enabling for CASE: '), translate:print_bexpr(A),nl,
181 create_texpr(truth,pred,[],Truth).
182 get_operation_enabling_condition2(while(_,_,_,_),Truth,[],imprecise) :- !,
183 debug_println(9,enabling_for_while_assumed_true),
184 create_texpr(truth,pred,[],Truth).
185 get_operation_enabling_condition2(if(IfList),Res,BecomesSuchVars,IsPrecise) :- !,
186 maplist5(get_if_condition(IsPrecise),IfList,Tests,Conds,BecomesSuchVars1),
187 append(BecomesSuchVars1,BecomesSuchVars),
188 (member(X,Conds), \+ is_truth(X)
189 -> disjoin_ifs(Tests,Conds,[],L),
190 conjunct_predicates_with_pos_info(L,Res)
191 ; create_texpr(truth,pred,[],Res) % all branches have no enabling condition
192 ).
193 get_operation_enabling_condition2(external_subst_call(Pred,Args),Res,[],_Precise) :-
194 external_subst_enabling_condition(Pred,Args,Cond),!, Res=Cond.
195 get_operation_enabling_condition2(operation_call(Operation,_OpCallResults,OpCallParas),Res,BecomeSuchVars,IsPrecise) :-
196 def_get_texpr_id(Operation,op(OperationName)),
197 b_get_machine_operation_for_animation(OperationName,_OpResults,OpParameters,Body,_OType,_TopLevel),
198 bsyntaxtree:replace_ids_by_exprs(Body,OpParameters,OpCallParas,Body2),
199 !,
200 %print(get_enabling_for_opcall),nl,translate:print_subst(Body2),nl,
201 get_operation_enabling_condition1(Body2,Res,BecomeSuchVars,IsPrecise).
202 get_operation_enabling_condition2(Subst,Truth,[],_Prcise) :- has_no_enabling_condition(Subst),!,
203 create_texpr(truth,pred,[],Truth).
204 get_operation_enabling_condition2(X,Truth,[],imprecise) :- %print(cannot_obtain(X)),nl,
205 (obtain_action_enabling_conditions
206 -> functor(X,F,A),debug_println(9,cannot_obtain_enabling(F/A,X)) ; true),
207 create_texpr(truth,pred,[],Truth).
208
209 % check if an substitution obviously has no guard/pre condition
210 has_no_enabling_condition(assign(_LHS,_RHS)).
211 has_no_enabling_condition(assign_single_id(_LHS,_RHS)).
212 has_no_enabling_condition(skip).
213
214 get_if_condition(IsPrecise,b(if_elsif(Test,TBody),_,_),Test,Condition,BecomesSuchVars) :-
215 get_operation_enabling_condition1(TBody,Condition,BecomesSuchVars,IsPrecise).
216
217 :- use_module(bsyntaxtree, [create_implication/3]).
218 disjoin_ifs([],[],_,[]).
219 disjoin_ifs([Test|TT],[EnableCond|TC],NegSoFar,[Res1|TR]) :-
220 append(NegSoFar,[Test],L),
221 conjunct_predicates_with_pos_info(L,BranchTest),
222 create_implication(BranchTest,EnableCond,Res1), % ELSEIF Test THEN Body ... ---> NegSoFar & Test => EnableCond
223 create_negation(Test,NTest),
224 append(NegSoFar,[NTest],NegSoFar1), % add negation of test as additional test for rest
225 disjoin_ifs(TT,TC,NegSoFar1,TR).
226
227
228 % for a list of substitutions: get enabling conditions and disjoin them
229 get_disjunction_of_enabling_conditions(ListOfWhens,Res,BecomesSuchVars,IsPrecise) :-
230 get_texpr_exprs(ListOfWhens,ListOfSelectWhens),
231 maplist(get_operation_enabling_condition3(IsPrecise),ListOfSelectWhens,Res1,BecomesSuchVars1),
232 disjunct_predicates(Res1,Res),
233 append(BecomesSuchVars1,BecomesSuchVars).
234 get_operation_enabling_condition3(Precise,Exp,Res,BV) :- get_operation_enabling_condition2(Exp,Res,BV,Precise).
235
236 definitely_not_empty(bool_set,_).
237 definitely_not_empty(integer_set(_),_).
238 definitely_not_empty(identifier(X),set(global(X))).
239 definitely_not_empty(string_set,_).
240
241
242
243 % get operation enabling condition in context of an event-b action list:
244 get_operation_enabling_for_event_b_actions([],Res,Res,[],_Precise).
245 get_operation_enabling_for_event_b_actions([H|T],InRes,OutRes,BecomesSuchVars,IsPrecise) :-
246 get_operation_enabling_condition1(H,ResH,HBecomesSuchVars,IsPrecise),
247 conjunct_predicates_with_pos_info(InRes,ResH,IntRes),
248 get_operation_enabling_for_event_b_actions(T,IntRes,OutRes,TBecomesSuchVars,IsPrecise),
249 append(HBecomesSuchVars,TBecomesSuchVars,BecomesSuchVars).
250
251 % ---------------
252 :- use_module(bmachine,[b_top_level_operation/1]).
253 % LTSMIN style guards: a guard that does not depend on parameters
254 get_operation_propositional_guards(OpName,Guards,RestBody) :-
255 b_top_level_operation(OpName),
256 b_get_machine_operation_for_animation(OpName,TResults,TParameters,TBody,_OType,true), % requires bmachine to be precompiled
257 get_operation_propositional_guards(OpName,TResults,TParameters,TBody,Guards,RestBody).
258
259 :- use_module(translate,[print_bexpr/1]).
260 % the following can be called directly; does not require bmachine to be pre-compiled
261 get_operation_propositional_guards(OpName,TResults,TParameters,TBody,Guards,RestBody) :-
262 get_texpr_ids(TParameters,Ids1),
263 get_texpr_ids(TResults,Ids2),
264 append(Ids1,Ids2,Ids), sort(Ids,Parameters),
265 get_operation_guards_aux(TBody,Parameters,top,Guards,RestBody),
266 (debug_mode(off) -> true
267 ; format('OPERATION Guard Splitting ~w (~w)~n',[OpName,Parameters]),
268 print('LTSMin Guards: '), maplist(print_bexpr,Guards),nl,
269 print('LTSMin Body: '), translate:print_subst(RestBody),nl,nl
270 ).
271
272 :- use_module(bsyntaxtree, [conjunction_to_list/2,find_identifier_uses/3]).
273
274
275 get_operation_guards_aux(Subst,Parameters,Top,Guards,OpBody) :-
276 get_guards(Subst,Top,TIds,Guard,InnerBody,Infos),
277 get_texpr_ids(TIds,Ids), sort(Ids,NewIds),
278 ord_union(Parameters,NewIds,Parameters2),
279 !,
280 get_parameter_independent_guards(Guard,Parameters2,Indep,Dep),
281 (Dep = []
282 -> OpBody = InnerOpBody % no need to keep b
283 ; conjunct_predicates_with_pos_info(Dep,DepCond),
284 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)
285 ),
286 get_operation_guards_aux(InnerBody,Parameters2,inner,InnerGuards,InnerOpBody),
287 append(Indep,InnerGuards,Guards).
288
289 get_operation_guards_aux(b(rlevent(Name,Sect,Status,Params,EvGuard,Theorems,Act,VWit,PWit,Unmod,AbsEvts),subst,Info),
290 Parameters,_,Guards,OpBody) :- !,
291 get_parameter_independent_guards(EvGuard,Parameters,InDepGuards,Dep),
292 (get_variant_pred(Status,VariantPred)
293 -> %print(op_variant(_Name)),nl,translate:print_bexpr(VariantPred),nl,
294 % 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
295 % TO DO: check if we need to add the witnesses as well !
296 Guards = [VariantPred|InDepGuards]
297 ; Guards = InDepGuards),
298 conjunct_predicates_with_pos_info(Dep,DepG),
299 OpBody = b(rlevent(Name,Sect,Status,Params,DepG,Theorems,Act,VWit,PWit,Unmod,AbsEvts),subst,Info).
300 get_operation_guards_aux(TB,_,_,[],TB).
301
302
303 :- use_module(bsyntaxtree, [safe_create_texpr/3]).
304 % we integrate the Variant check into the guard to ensure the correct read matrix is produced
305 % currently the Variant is checked upon event entry and for convergent events upon exit for decrease
306 % in principle this should be more of an assertion_predicate or assertion_expression
307 get_variant_pred(b(Status,status,_),Res) :- get_variant_pred_aux(Status,Res).
308 get_variant_pred_aux(anticipated(Variant),Res) :- NATURAL = b(integer_set('NATURAL'),set(integer),[]),
309 safe_create_texpr(member(Variant,NATURAL),pred,Res).
310 get_variant_pred_aux(convergent(Variant),Res) :- NATURAL1 = b(integer_set('NATURAL1'),set(integer),[]),
311 safe_create_texpr(member(Variant,NATURAL1),pred,Res).
312
313
314 get_guards(b(precondition(Guard,TBody),subst,Info), top, [],Guard, TBody, Info) :-
315 preferences:get_preference(treat_outermost_pre_as_select,true).
316 get_guards(b(select([b(select_when(Guard, TBody),subst,_Info1)]),subst,Info2), _, [],Guard, TBody, Info2).
317 % TO DO: for ANY try and extract propositional parts, e.g., for ANY pp WHERE pp:1..xx & z=1/yy THEN …
318 get_guards(b(any(TIds,Guard,TBody),subst,Info),_, TIds,Guard,TBody,Info).
319 get_guards(b(operation_call(Operation,[],Parameters),subst,Info),_, InnerParas,Guard, TBody, Info) :-
320 Parameters=[], % TO DO: improve, will require substituting Parameters inside OpRealBody
321 bmachine_is_precompiled,
322 def_get_texpr_id(Operation,op(OpName)),
323 b_get_machine_operation(OpName,[],RealParameters,OpRealBody,_OType,_OpPos), RealParameters=[],
324 get_guards(OpRealBody, inner, InnerParas, Guard, TBody, _ ).
325
326 construct_select([],Guard,TBody, Infos, Res) :- !,
327 Res = b(select([b(select_when(Guard, TBody),subst,Infos)]),subst,Infos).
328 construct_select(TIds,Guard,TBody,Infos, b(any(TIds,Guard,TBody),subst,Infos)).
329
330
331 get_parameter_independent_guards(Guard,Parameters,Indep,Dep) :-
332 conjunction_to_list(Guard,Gs),
333 l_get_parameter_independent_guards(Gs,Parameters,at_front,Indep,Dep).
334
335 :- use_module(library(ordsets)).
336 :- use_module(bsyntaxtree,[always_well_defined/1]).
337 l_get_parameter_independent_guards([],_,_,[],[]).
338 l_get_parameter_independent_guards([G|Gs],Parameters,AtFront,Indep,Dep) :-
339 find_identifier_uses(G,[],Ids),
340 ( ord_disjoint(Ids,Parameters),
341 (AtFront=at_front -> true ; always_well_defined(G))
342 -> Indep=[G|I1], Dep=D1, AtFront1=AtFront
343 ; Indep=I1, Dep=[G|D1],
344 AtFront1=not_at_front % we have skipped one guard; the next guard is not guaranteed to be at the front (relevant for well-definedness)
345 ),
346 l_get_parameter_independent_guards(Gs,Parameters,AtFront1,I1,D1).
347
348 % for synthesis of sequential programs
349 % not yet used
350 /*
351 % find events which have common guards
352 get_common_guards(Ev1,Ev2,Common,Rest1,Rest2) :-
353 get_unsimplified_operation_guard(Ev1,G1), conjunction_to_list(G1,GL1),
354 get_unsimplified_operation_guard(Ev2,G2), Ev1 @< Ev2, conjunction_to_list(G2,GL2),
355 find_common(GL1,GL2,Common,Rest1,Rest2),
356 format('~nCommon guard for ~w <-> ~w~n Common: ',[Ev1,Ev2]), translate:l_print_bexpr_or_subst(Common),nl,
357 format(' Rest1: ',[]),translate:l_print_bexpr_or_subst(Rest1),nl,
358 format(' Rest2: ',[]),translate:l_print_bexpr_or_subst(Rest2),nl.
359
360 % check if abrial merge rule 15.3 from Bee-Book for if/while is applicable:
361 get_abrial_rule(Ev1,Ev2,Common,Rest1) :-
362 get_common_guards(Ev1,Ev2,Common,Rest1,Rest2),
363 conjunct_predicates(Rest1,R1),
364 conjunct_predicates(Rest2,R2),
365 is_negated_predicate(R1,R2),
366 format('*** Can be merged ~w <-> ~w !~n~n',[Ev1,Ev2]).
367
368 find_common([],GL2,[],[],GL2).
369 find_common([G1|T1],GL2,[G1|TCommon],Rest1,Rest2) :-
370 select(G2,GL2,T2), % TODO: check WD
371 same_predicate(G1,G2),!, % we could also use norm_expr
372 find_common(T1,T2,TCommon,Rest1,Rest2).
373 find_common([G1|T1],GL2,TCommon,[G1|Rest1],Rest2) :-
374 find_common(T1,GL2,TCommon,Rest1,Rest2).
375
376 same_predicate(G1,G2) :- bsyntaxtree:same_texpr(G1,G2).
377 same_predicate(G1,G2) :- b_interpreter_check:norm_pred_check(G1,N1), b_interpreter_check:norm_pred_check(G2,N2), N2=N1.
378
379 is_negated_predicate(G1,G2) :- bsyntaxtree:is_negation_of(G1,G2).
380 */
381