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