1 % Heinrich Heine Universitaet Duesseldorf
2 % (c) 2025-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 % Implementation of a Sequent Prover
6 % where we can use the ProB animator to perform proof steps
7 % proof rules taken/adapted from https://wiki.event-b.org/index.php/Inference_Rules
8 % rewrite rules taken/adapted from https://wiki.event-b.org/index.php/All_Rewrite_Rules
9 % the term representation is the one from ProB's well_definedness prover
10
11 :- module(sequent_prover,[initialise_for_po_file/1,
12 cont_length/2,
13 get_continuations/2,
14 get_scope/3,
15 parse_input/3,
16 translate_norm_expr_term/2,
17 used_identifiers/2,
18
19 po_nr_label/2, normalised_hyps/3, normalised_goal/3
20 ]).
21
22 :- use_module(probsrc(module_information),[module_info/2]).
23 :- module_info(group,sequent_prover).
24 :- module_info(description,'This module provides rewrite and inference rules for the sequent_prover').
25
26
27 :- meta_predicate rewrite_subterm(2,-,-).
28 :- meta_predicate transform_args(2,-,-).
29
30 :- use_module(library(lists)).
31 :- use_module(probsrc(error_manager),[add_error/3, add_internal_error/2]).
32 :- use_module(probsrc(tools),[ajoin/2, flatten/2, list_intersection/3, list_difference/3]).
33 :- use_module(probsrc(translate),[transform_raw/2]).
34 :- use_module(wdsrc(well_def_hyps),[convert_norm_expr_to_raw/2,
35 normalize_expression/2, normalize_predicate/2]).
36 :- use_module(seqproversrc(sequent_auto_prover),[bounded_find_proof/5]).
37 :- use_module(seqproversrc(simplification_rules),[simplification_rule/6, is_true/1]).
38 :- use_module(seqproversrc(prover_interface)).
39 :- use_module(seqproversrc(prover_utils)).
40
41 % ------------------------
42 :- dynamic normalised_hyps/3, normalised_goal/3, po_nr_label/2. % for BPR export
43
44 initialise_for_po_file(File) :-
45 retractall(normalised_hyps(_,_,_)),
46 retractall(normalised_goal(_,_,_)),
47 retractall(po_nr_label(_,_)),
48 bb_put(po_counter,1),
49 load_from_po_file(File,Hyps,Goal,PO,PODesc,Info),
50 sort(Hyps,SHyps),
51 % for ProB XTL Mode: specifying the start states:
52 bb_get(po_counter,PONR),
53 assertz(po_nr_label(PONR,PO)),
54 assertz(xtl_interface:start(state(sequent(SHyps,Goal,success(PONR)),Info),[description(PODesc)])),
55 P1 is PONR+1, bb_put(po_counter,P1),
56 fail.
57 initialise_for_po_file(_) :-
58 % assert other xtl_interface predicates:
59 assertz((xtl_interface:trans(A,B,C) :- sequent_prover:sequent_prover_trans_start(A,B,C))),
60 assertz((xtl_interface:trans(A,B,C,D) :- sequent_prover:sequent_prover_trans_start_desc(A,B,C,D))),
61 assertz((xtl_interface:trans_prop(A,B) :- sequent_prover:trans_prop(A,B))),
62 assertz((xtl_interface:prop(A,B) :- sequent_prover:prop(A,B))),
63 assertz((xtl_interface:symb_trans(A,B,C,D) :- sequent_prover:symb_trans(A,B,C,D))),
64 assertz((xtl_interface:symb_trans_enabled(A,B) :- sequent_prover:symb_trans_enabled(A,B))),
65 assertz((xtl_interface:animation_function_result(A,B) :- sequent_prover:animation_function_result(A,B))),
66 assertz((xtl_interface:animation_image_right_click_transition(A,B,C) :- sequent_prover:animation_image_right_click_transition(A,B,C))),
67 assertz((xtl_interface:animation_image_right_click_transition(A,B,C,D) :- sequent_prover:animation_image_right_click_transition(A,B,C,D))),
68 assertz(xtl_interface:nr_state_properties(30)). % TODO: a dynamic solution would be nice.
69
70 :- use_module(probsrc(preferences),[get_preference/2]).
71 rodin_mode :- get_preference(sequent_prover_rodin_mode,true).
72
73 % declare these public as we will assert calls to them in the code above for initialise_for_po_file:
74 :- public prop/2, sequent_prover_trans/3, sequent_prover_trans_desc/4, symb_trans/4, symb_trans_enabled/2.
75 :- public animation_function_result/2, animation_image_right_click_transition/3, animation_image_right_click_transition/4.
76 :- public sequent_prover_trans_start/3, sequent_prover_trans_start_desc/4, trans_prop/2.
77
78 % ------------------------
79
80 get_normalised_bexpr(Expr,NormExpr) :-
81 transform_raw(Expr,TExpr),
82 normalize_predicate(TExpr,NormExpr).
83
84 :- use_module(probsrc(xtl_interface),[get_disprover_po/6]).
85 load_from_po_file(_File,Hyps,Goal,POLabel,FullPOLabel,[rawsets(RawSets),des_hyps(OtherHyps)]) :-
86 % we use the disprover_po facts already loaded in xtl_interface, load_po_file(File) not necessary
87 get_disprover_po(POLabel,Context,RawGoal,RawAllHyps,RawSelHyps,RodinStatus),
88 ajoin([POLabel,' (',RodinStatus,')'],FullPOLabel),
89 get_normalised_bexpr(RawGoal,Goal),
90 list_difference(RawAllHyps,RawSelHyps,RawNotSelHyps),
91 maplist(get_normalised_bexpr,RawSelHyps,Hyps),
92 maplist(get_normalised_bexpr,RawNotSelHyps,OtherHyps),
93 append(RawSelHyps,RawNotSelHyps,RawPreds), append(Hyps,OtherHyps,NormPreds),
94 assertz(normalised_hyps(POLabel,RawPreds,NormPreds)),
95 assertz(normalised_goal(POLabel,RawGoal,Goal)),
96 bmachine_eventb:extract_ctx_sections(Context,_Name,_Extends,RawSets,_Constants,_AbstractConstants,_Axioms,_Theorems).
97
98
99 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs) :-
100 get_scope(Hyps,Info,Scope),
101 get_identifier_types(Scope,IdsTypes),
102 new_aux_identifier(IdsTypes,Y), !,
103 get_wd_pos2(equal('$'(Y),NormExpr),Scope,POs).
104
105 get_wd_pos(NormExpr,Hyps,Info,POs) :-
106 get_scope(Hyps,Info,Scope),
107 get_wd_pos2(NormExpr,Scope,POs).
108
109 get_wd_pos2(NormExpr,Scope,POs) :-
110 convert_norm_expr_to_raw(NormExpr,ParsedRaw),
111 bmachine:b_type_check_raw_expr(ParsedRaw,Scope,TypedPred,open(_)),
112 well_def_hyps:empty_hyps(H),
113 well_def_analyser:compute_all_wd_pos(TypedPred,H,[],TPOs),
114 maplist(rewrite_type_set,TPOs,TPOsT),
115 maplist(normalize_predicate,TPOsT,POs).
116
117 get_scope(Hyps,Info,[identifier([])]) :-
118 get_meta_info(des_hyps,Info,DHyps),
119 append(Hyps,DHyps,[]), !.
120 get_scope(Hyps,Info,[identifier(Res)]) :-
121 get_meta_info(des_hyps,Info,DHyps),
122 append(Hyps,DHyps,AllHyps),
123 used_identifiers(AllHyps,Ids),
124 sort(Ids,SortedIds),
125 maplist(replace_bounded_ids(SortedIds),Hyps,HypsWithFreshBIds),!,
126 (HypsWithFreshBIds=[] -> HypsConj=truth
127 ; list_to_op(HypsWithFreshBIds,HypsConj,conjunct)),
128 get_set_types(Ids,Info,SetIds),
129 get_typed_identifiers(HypsConj,[identifier(SetIds)],List),
130 append(SetIds,List,AllIds),
131 select_types(AllIds,Res).
132
133 replace_bounded_ids(_,E,Res) :- atomic(E),!,Res=E.
134 replace_bounded_ids(_,'$'(E),Res) :- atomic(E),!,Res='$'(E).
135 replace_bounded_ids(Ids,E,F) :-
136 with_ids(E,BIds), % expression has bounded ids
137 length(BIds,Len),
138 ? possible_identifier(X),
139 ? range_ids(X,Len,NewIds), % generate X_1, X_2, ... as NewIds
140 list_intersection(Ids,NewIds,[]), !, % check if these are fresh
141 rewrite_pairwise_ids(BIds,NewIds,E,F).
142 replace_bounded_ids(Ids,E,F) :- % E is a compound term not introducing bounded ids
143 E=..[Op|Args],
144 maplist(replace_bounded_ids(Ids),Args,NewArgs),
145 F=..[Op|NewArgs].
146
147 rewrite_pairwise_ids([],[],E,E).
148 rewrite_pairwise_ids([X|TX],[Y|TY],E,Res) :-
149 replace_id(X,Y,E,NewE),
150 rewrite_pairwise_ids(TX,TY,NewE,Res).
151
152 replace_id('$'(X),'$'(Y),'$'(X),'$'(Y)) :- atomic(X), !.
153 replace_id(X,Y,C,NewC) :- C=..[Op|Args],
154 maplist(replace_id(X,Y),Args,NewArgs),
155 NewC=..[Op|NewArgs].
156
157 %filter_hyps([],[]).
158 %filter_hyps([Expr|Hyps],Res) :- with_ids(Expr,_), !, filter_hyps(Hyps,Res).
159 %filter_hyps([Expr|Hyps],[Expr|Res]) :- filter_hyps(Hyps,Res).
160
161 get_set_types([],_,[]).
162 get_set_types(['$'(SetId)|T],Info,[b(identifier(SetId),set(global(SetId)),[])|R]) :-
163 is_deferred_or_enumerated_set('$'(SetId),Info),!,
164 get_set_types(T,Info,R).
165 get_set_types([_|T],Info,R) :- get_set_types(T,Info,R).
166
167 rewrite_type_set(X,Res) :- \+ compound(X),!, Res=X.
168 rewrite_type_set(b(typeset,set(global(SET)),I),b(identifier(SET),set(global(SET)),I)) :- !.
169 rewrite_type_set(C,NewC) :- C=..[Op|Args],
170 maplist(rewrite_type_set,Args,NewArgs),
171 NewC =.. [Op|NewArgs].
172
173 %in_scope(Id,[identifier(List)]) :- memberchk(b(identifier(Id),_,[]),List).
174
175
176
177 select_types(Types,Selected) :- sort(Types,Sorted), select_types_aux(Sorted,Selected).
178
179 select_types_aux([],[]).
180 select_types_aux([b(identifier(X),any,_)|R],Res) :- member(b(identifier(X),T,_),R), T \= any, !, select_types_aux(R,Res).
181 select_types_aux([Id|R],[Id|Res]) :- select_types_aux(R,Res).
182
183 get_identifier_types([identifier(IdsTypes)],IdsTypes).
184
185 prove_predicate(Hyps,Goal) :-
186 get_typed_ast(Goal,TGoal),
187 maplist(get_typed_ast,Hyps,THyps),
188 atelierb_provers_interface:prove_predicate(THyps,TGoal,proved).
189
190
191
192 get_po_description(PONR,PO) :-
193 xtl_interface:start(state(sequent(_SHyps,_Goal,success(PONR)),_Info),[description(PO)]).
194
195
196 :- use_module(library(timeout), [time_out/3]).
197 % specifying properties that appear in the State Properties view:
198 prop(success(_Nr),goal).
199 prop(success(Nr),'='('DISCHARGED',PO)) :- get_po_description(Nr,PO).
200 prop(counter_example(_),'COUNTER_EXAMPLE_FOUND').
201 prop(counter_example(C),'='(ID,S)) :- member(binding(ID,_,S),C).
202 prop(sequent(_,X,_), '='('GOAL',XS)) :- translate_norm_expr_term(X,XS).
203 prop(sequent(Hyps,_,_),'='(HypNr,XS)) :- nth1(Nr,Hyps,X),
204 translate_norm_expr_term(X,XS), ajoin(['HYP',Nr],HypNr).
205 prop(sequent(_,_,Cont),'='('CONTINUATION',P)) :- cont_length(Cont,P).
206 prop(state(sequent(Hyps,Goal,_),Info),'='(AutoProofStepNr,ProofStep)) :-
207 (time_out(bounded_find_proof(sequent(Hyps,Goal,success(0)),Info,3,[],Proof),1500,TORes),
208 TORes = success
209 -> nth1(Nr,Proof,ProofStep),
210 ajoin(['AUTO-PROOF-',Nr],AutoProofStepNr)).
211
212 prop(state(S,_),Val) :- prop(S,Val).
213
214
215 cont_length(sequent(_,_,C),R) :- !, cont_length(C,R1), R is R1+1.
216 cont_length(success(_),R) :- !, R=0.
217 cont_length(C,R) :- add_internal_error('Illegal continuation:',cont_length(C,R)),R=0.
218
219 % ------------------------
220
221 :- discontiguous trans_prop/2, symb_trans_rule/4, symb_trans_rule/5, symb_trans_enabled/2.
222
223 sequent_prover_trans_start(Rule,state(Sequent,Info),NewState) :-
224 update_info(Sequent,Info,Info1),
225 ? sequent_prover_trans(Rule,state(Sequent,Info1),NewState).
226 sequent_prover_trans_start_desc(Rule,state(Sequent,Info),NewState,Descr) :-
227 update_info(Sequent,Info,Info1),
228 ? sequent_prover_trans_desc(Rule,state(Sequent,Info1),NewState,Descr).
229
230 update_info(Sequent,Info,Info1) :-
231 Sequent = sequent(Hyps,_,_),
232 get_scope(Hyps,Info,[identifier(IdsTypes)]),
233 update_meta_infos(ids_types,Info,IdsTypes,Info1).
234
235 % transition rules without description information:
236 sequent_prover_trans(Rule,state(Sequent,Info),state(NewSequent,Info)) :-
237 ? trans_with_info(Rule,Sequent,NewSequent,Info).
238
239 sequent_prover_trans(lasso,state(sequent(Hyps,Goal,Cont),Info),state(sequent(NewHyps,Goal,Cont),NewInfo)) :-
240 get_meta_info(des_hyps,Info,DesHyps),
241 used_identifiers_list([Goal|Hyps],UsedIds),
242 find_hyps_with(UsedIds,DesHyps,SelectedHyps),
243 SelectedHyps \= [],
244 add_hyps(SelectedHyps,Hyps,NewHyps),
245 list_difference(DesHyps,SelectedHyps,LeftDesHyps),
246 update_meta_infos(des_hyps,Info,LeftDesHyps,NewInfo).
247
248 % transition rules with description information:
249 sequent_prover_trans_desc(Rule,state(Sequent,Info),state(NewSequent,Info),Descr) :-
250 ? trans_desc_with_info(Rule,Sequent,NewSequent,Info,Descr).
251 sequent_prover_trans_desc(RuleArg,state(Sequent,Info),state(NewSequent,Info1),[description(Descr)]) :-
252 ? (trans_with_args(RuleArg,Sequent,NewSequent), Info1 = Info
253 ? ; trans_with_args_info(RuleArg,state(Sequent,Info),state(NewSequent,Info1))
254 ),
255 RuleArg=..[Rule,Arg], create_descr(Rule,Arg,Descr).
256 sequent_prover_trans_desc(mon_deselect(Nr),state(sequent(Hyps,Goal,Cont),Info),
257 state(sequent(Hyps0,Goal,Cont),Info1),[description(Descr)]) :-
258 nth1(Nr,Hyps,Hyp,Hyps0),
259 add_meta_info(des_hyps,Info,Hyp,Info1),
260 create_descr(mon_deselect,Hyp,Descr).
261
262 % transition rules requiring Infos and returning descriptions
263 trans_desc_with_info(Rule,Sequent,NewSequent,_Info,Desc) :-
264 ? trans_desc_wo_info(Rule,Sequent,NewSequent,Desc).
265 trans_desc_with_info(Rule,Sequent,NewSequent,Info,Desc) :-
266 ? trans_desc_simplification_rule(Rule,Sequent,NewSequent,Info,Desc).
267
268 trans_desc_simplification_rule(simplify_goal(Rule),sequent(Hyps,Goal,Cont),
269 NewSequent,Info,[description(Descr)]) :-
270 simplification_rule(Goal,NewGoal,Hyps,Rule,Descr,Info),
271 (rodin_mode, NewGoal=conjunct(_,_)
272 -> op_to_list(NewGoal,ListOfGoals,conjunct),
273 add_goals(Hyps,ListOfGoals,Cont,NewSequent)
274 ; NewSequent=sequent(Hyps,NewGoal,Cont)).
275 trans_desc_simplification_rule(simplify_hyp(Rule,Hyp),sequent(Hyps,Goal,Cont),
276 sequent(SHyps,Goal,Cont),Info,[description(Descr)]) :-
277 ? select(Hyp,Hyps,NewHyp,NewHyps),
278 ? simplification_rule(Hyp,NewHyp,Hyps,Rule,Descr,Info),
279 (rodin_mode, NewHyp=conjunct(_,_)
280 -> append(PH,[NewHyp|TH],NewHyps),
281 op_to_list(NewHyp,NewConjHyps,conjunct),
282 append(NewConjHyps,TH,NTH),
283 append(PH,NTH,AddHyps)
284 ; AddHyps=NewHyps),
285 without_duplicates(AddHyps,SHyps).
286
287
288 % transition rules without descriptions but with Info being passed
289 ?trans_with_info(Rule,Sequent,NewSequent,_Info) :- trans_wo_info(Rule,Sequent,NewSequent).
290 trans_with_info(deriv_le_card,sequent(Hyps,Goal,Cont),sequent(Hyps,subset(S,T),Cont),Info) :- % covers DERIV_GE_CARD too
291 is_less_eq(Goal,card(S),card(T)),
292 get_meta_info(ids_types,Info,IdsTypes),
293 ? same_type(S,T,IdsTypes).
294 trans_with_info(deriv_lt_card,sequent(Hyps,Goal,Cont),sequent(Hyps,subset_strict(S,T),Cont),Info) :- % / DERIV_GT_CARD
295 is_less(Goal,card(S),card(T)),
296 get_meta_info(ids_types,Info,IdsTypes),
297 same_type(S,T,IdsTypes).
298 trans_with_info(deriv_equal_card,sequent(Hyps,equal(card(S),card(T)),Cont),sequent(Hyps,equal(S,T),Cont),Info) :-
299 get_meta_info(ids_types,Info,IdsTypes),
300 ? same_type(S,T,IdsTypes).
301 trans_with_info(sim_rel_image_r,sequent(Hyps,Goal,Cont),NewSequent,Info) :-
302 rewrite_once(image(F,set_extension([E])),set_extension([function(F,E)]),Goal,NewGoal),
303 get_wd_pos(NewGoal,Hyps,Info,POs),
304 add_wd_pos(Hyps,POs,sequent(Hyps,NewGoal,Cont),NewSequent).
305 trans_with_info(sim_rel_image_l,sequent(Hyps,Goal,Cont),NewSequent,Info) :-
306 select(Hyp,Hyps,Hyps0),
307 rewrite_once(image(F,set_extension([E])),set_extension([function(F,E)]),Hyp,NewHyp),
308 add_hyp(NewHyp,Hyps0,Hyps1),
309 get_wd_pos(NewHyp,Hyps0,Info,POs),
310 add_wd_pos(Hyps0,POs,sequent(Hyps1,Goal,Cont),NewSequent).
311 trans_with_info(sim_fcomp_l,sequent(Hyps,Goal,Cont),NewSequent,Info) :-
312 select(Hyp,Hyps,Hyps0),
313 rewrite_subterm(sim_fcomp,Hyp,NewHyp),
314 add_hyp(NewHyp,Hyps0,Hyps1),
315 get_wd_pos(NewHyp,Hyps0,Info,POs),
316 add_wd_pos(Hyps0,POs,sequent(Hyps1,Goal,Cont),NewSequent).
317 trans_with_info(sim_fcomp_r,sequent(Hyps,Goal,Cont),NewSequent,Info) :-
318 rewrite_subterm(sim_fcomp,Goal,NewGoal),
319 get_wd_pos(NewGoal,Hyps,Info,POs),
320 add_wd_pos(Hyps,POs,sequent(Hyps,NewGoal,Cont),NewSequent).
321 trans_with_info(xst_l,sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,Cont),Info) :-
322 Hyp = exists(Ids,P),
323 ? select(Hyp,Hyps,NewP,Hyps1),
324 get_meta_info(ids_types,Info,THypIds), % ids of the hypotheses
325 free_identifiers(Goal,GoalIds), maplist(any_type,GoalIds,TGoalIds), % new ids must also avoid ids of the goal
326 append(THypIds,TGoalIds,TIds),
327 replace_used_ids(Ids,TIds,NewIds),
328 rewrite_pairwise_rev(Ids,NewIds,P,NewP).
329 trans_with_info(all_r,sequent(Hyps,forall(Ids,P,Q),Cont),sequent(Hyps,NewImp,Cont),Info) :-
330 get_meta_info(ids_types,Info,THypIds), % ids of the hypotheses
331 free_identifiers(forall(Ids,P,Q),GoalIds), maplist(any_type,GoalIds,TGoalIds), % new ids must also avoid ids of the goal
332 append(THypIds,TGoalIds,TIds),
333 replace_used_ids(Ids,TIds,NewIds),
334 rewrite_pairwise_rev(Ids,NewIds,implication(P,Q),NewImp).
335 ?trans_with_info(Rule,sequent(Hyps,Goal,Cont),Cont,Info) :- axiom_with_info(Rule,Hyps,Goal,Info).
336 ?trans_with_info(Rule,sequent(Hyps,Goal,Cont),Cont,Info) :- (member(Hyp,Hyps), mb_goal(Rule,Hyp,Goal,Info)) ; mb_goal(Rule,Hyps,Goal).
337
338 % transitions which do not require Infos (about typed ids)
339 ?trans_wo_info(Rule,sequent(Hyps,Goal,Cont),Cont) :- axiom(Rule,Hyps,Goal).
340 trans_wo_info(dbl_hyp,sequent(Hyps,Goal,Cont),sequent(SHyps,Goal,Cont)) :-
341 % not really necessary if we remove duplicates in Hyps everywhere else
342 without_duplicates(Hyps,SHyps), SHyps \= Hyps.
343 trans_wo_info(or_r,sequent(Hyps,disjunct(GA,GB),Cont),sequent(Hyps1,GB,Cont)) :-
344 % Hyps |- G1 or G2 ===> Hyps,not(G1) |- G2
345 negate(GA,NotGA),
346 add_hyp(NotGA,Hyps,Hyps1).
347 trans_wo_info(imp_r,sequent(Hyps,implication(G1,G2),Cont),
348 sequent(Hyps1,G2,Cont)) :-
349 % Hyps |- G1 => G2 ===> Hyps,G1 |- G2
350 add_hyp(G1,Hyps,Hyps1).
351 trans_wo_info(and_r,sequent(Hyps,conjunct(G1,G2),Cont),NewSequent) :-
352 % Hyps |- G1 & G2 ===> Hyps |- G1 + Hyps |- G2
353 op_to_list(conjunct(G1,G2),ListOfGoals,conjunct),
354 add_goals(Hyps,ListOfGoals,Cont,NewSequent).
355 trans_wo_info(neg_in,sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,Cont)) :- % covers NEG_IN_L and NEG_IN_R
356 member(member(E,set_extension(L)),Hyps),
357 member(InEq,Hyps),
358 is_inequality(InEq,E,B),
359 member(C,L),
360 equal_terms(B,C),
361 select(C,L,R),
362 select(member(E,set_extension(L)),Hyps,member(E,set_extension(R)),Hyps1).
363 trans_wo_info(contradict_r,sequent(Hyps,Goal,Cont),sequent(Hyps0,falsity,Cont)) :-
364 Goal \= falsity, % proof rule is useless here; we just add not(falsity) to hyp
365 add_hyp(negation(Goal),Hyps,Hyps0).
366 trans_wo_info(lower_bound_l,sequent(Hyps,Goal,Cont),sequent(Hyps,finite(Set),Cont)) :-
367 Goal = exists([B],forall([X],member(X,Set),less_equal(B,X))),
368 no_bound_ids_in_set([B,X],Set).
369 trans_wo_info(lower_bound_r,sequent(Hyps,Goal,Cont),sequent(Hyps,finite(Set),Cont)) :-
370 Goal = exists([B],forall([X],member(X,Set),greater_equal(X,B))),
371 no_bound_ids_in_set([B,X],Set).
372 trans_wo_info(upper_bound_l,sequent(Hyps,Goal,Cont),sequent(Hyps,finite(Set),Cont)) :-
373 Goal = exists([B],forall([X],member(X,Set),greater_equal(B,X))),
374 no_bound_ids_in_set([B,X],Set).
375 trans_wo_info(upper_bound_r,sequent(Hyps,Goal,Cont),sequent(Hyps,finite(Set),Cont)) :-
376 Goal = exists([B],forall([X],member(X,Set),less_equal(X,B))),
377 no_bound_ids_in_set([B,X],Set).
378 trans_wo_info(fin_lt_0,sequent(Hyps,finite(S),Cont),sequent(Hyps,Goal1,sequent(Hyps,Goal2,Cont))):-
379 new_identifier(S,X),
380 new_identifier(member(S,X),N),
381 Goal1 = exists([N],forall([X],member(X,S),less_equal(N,X))),
382 Goal2 = subset(S,set_subtraction(integer_set,natural1_set)).
383 trans_wo_info(fin_ge_0,sequent(Hyps,finite(S),Cont),sequent(Hyps,Goal1,sequent(Hyps,Goal2,Cont))):-
384 new_identifier(S,X),
385 new_identifier(member(S,X),N),
386 Goal1 = exists([N],forall([X],member(X,S),less_equal(X,N))),
387 Goal2 = subset(S,natural_set).
388 trans_wo_info(fin_binter_r,sequent(Hyps,finite(I),Cont),sequent(Hyps,Disj,Cont)) :-
389 I = intersection(_,_),
390 finite_intersection(I,Disj).
391 trans_wo_info(fin_kinter_r,sequent(Hyps,finite(general_intersection(S)),Cont),
392 sequent(Hyps,exists([X],conjunct(member(X,S),finite(X))),Cont)) :- % #s.(s:S & finite(s))
393 new_identifier(S,X).
394 trans_wo_info(fin_qinter_r,sequent(Hyps,finite(quantified_intersection(Ids,P,E)),Cont),
395 sequent(Hyps,exists(Ids,conjunct(P,finite(E))),Cont)). % #x.(P & finite(E))
396 trans_wo_info(fin_kunion_r,sequent(Hyps,finite(general_union(S)),Cont),
397 sequent(Hyps,conjunct(finite(S),forall([X],member(X,S),finite(X))),Cont)) :- new_identifier(S,X).
398 trans_wo_info(fin_qunion_r,sequent(Hyps,finite(quantified_union([X],P,E)),Cont),
399 sequent(Hyps,conjunct(finite(event_b_comprehension_set([X],E,P)),forall([X],P,finite(E))),Cont)).
400 trans_wo_info(fin_setminus_r,sequent(Hyps,finite(set_subtraction(S,_)),Cont),sequent(Hyps,finite(S),Cont)).
401 trans_wo_info(fin_rel_img_r,sequent(Hyps,finite(image(F,_)),Cont),sequent(Hyps,finite(F),Cont)).
402 trans_wo_info(fin_rel_ran_r,sequent(Hyps,finite(range(F)),Cont),sequent(Hyps,finite(F),Cont)).
403 trans_wo_info(fin_rel_dom_r,sequent(Hyps,finite(domain(F)),Cont),sequent(Hyps,finite(F),Cont)).
404 trans_wo_info(fin_fun_dom,sequent(Hyps,finite(Fun),Cont),sequent(Hyps,truth,Cont1)) :-
405 member_hyps(member(Fun,FunType),Hyps),
406 is_fun(FunType,_,Dom,_),
407 (member_hyps(finite(Dom),Hyps)
408 -> Cont1 = Cont
409 ; Cont1 = sequent(Hyps,finite(Dom),Cont)).
410 trans_wo_info(fin_fun_ran,sequent(Hyps,finite(Fun),Cont),sequent(Hyps,truth,Cont1)) :-
411 member_hyps(member(Fun,FunType),Hyps),
412 is_inj(FunType,_,Ran),
413 (member_hyps(finite(Ran),Hyps)
414 -> Cont1 = Cont
415 ; Cont1 = sequent(Hyps,finite(Ran),Cont)).
416 trans_wo_info(sim_ov_rel,sequent(Hyps,member(overwrite(Fun,set_extension([couple(X,Y)])),relations(A,B)),Cont),
417 sequent(Hyps,member(X,A),sequent(Hyps,member(Y,B),Cont))) :-
418 member_hyps(member(Fun,FunType),Hyps),
419 is_rel(FunType,_,A,B).
420 trans_wo_info(sim_ov_trel,sequent(Hyps,member(overwrite(Fun,set_extension([couple(X,Y)])),total_relation(A,B)),Cont),
421 sequent(Hyps,member(X,A),sequent(Hyps,member(Y,B),Cont))) :-
422 member_hyps(member(Fun,FunType),Hyps),
423 is_rel(FunType,total,A,B).
424 trans_wo_info(sim_ov_pfun,sequent(Hyps,member(overwrite(Fun,set_extension([couple(X,Y)])),partial_function(A,B)),Cont),
425 sequent(Hyps,member(X,A),sequent(Hyps,member(Y,B),Cont))) :-
426 member_hyps(member(Fun,FunType),Hyps),
427 is_fun(FunType,_,A,B).
428 trans_wo_info(sim_ov_tfun,sequent(Hyps,member(overwrite(Fun,set_extension([couple(X,Y)])),total_function(A,B)),Cont),
429 sequent(Hyps,member(X,A),sequent(Hyps,member(Y,B),Cont))) :-
430 member_hyps(member(Fun,FunType),Hyps),
431 is_fun(FunType,total,A,B).
432 trans_wo_info(fun_image_goal,sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,Cont)) :-
433 wd_strict_term(Goal),
434 ? is_subterm(Hyp,Goal),
435 Hyp = function(F,E),
436 member(member(F,RType),Hyps),
437 is_rel(RType,_,_,Ran),
438 \+ member(member(function(F,E),Ran),Hyps),
439 add_hyp(member(function(F,E),Ran),Hyps,Hyps1).
440 trans_wo_info(ov_setenum_l,sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,sequent(Hyps2,Goal,Cont))) :-
441 select(Hyp,Hyps,Hyps0),
442 wd_strict_term(Hyp),
443 Fun = function(overwrite(F,set_extension([couple(E,V)])),G),
444 rewrite_once(Fun,V,Hyp,Hyp1),
445 add_hyps([equal(G,E),Hyp1],Hyps0,Hyps1),
446 rewrite_once(Fun,function(domain_subtraction(set_extension([E]),F),G),Hyp,Hyp2),
447 add_hyps([negation(equal(G,E)),Hyp2],Hyps0,Hyps2).
448 trans_wo_info(ov_setenum_r,sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal1,sequent(Hyps2,Goal2,Cont))) :-
449 wd_strict_term(Goal),
450 Fun = function(overwrite(F,set_extension([couple(E,V)])),G),
451 rewrite_once(Fun,V,Goal,Goal1),
452 add_hyp(equal(G,E),Hyps,Hyps1),
453 rewrite_once(Fun,function(domain_subtraction(set_extension([E]),F),G),Goal,Goal2),
454 add_hyp(negation(equal(G,E)),Hyps,Hyps2).
455 trans_wo_info(ov_l,sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,sequent(Hyps2,Goal,Cont))) :-
456 select(Hyp,Hyps,Hyps0),
457 wd_strict_term(Hyp),
458 Fun = function(overwrite(F,S),G),
459 rewrite_once(Fun,function(S,G),Hyp,Hyp1),
460 add_hyps([member(G,domain(S)),Hyp1],Hyps0,Hyps1),
461 rewrite_once(Fun,function(domain_subtraction(domain(S),F),G),Hyp,Hyp2),
462 add_hyps([negation(member(G,domain(S))),Hyp2],Hyps0,Hyps2).
463 trans_wo_info(ov_r,sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal1,sequent(Hyps2,Goal2,Cont))) :-
464 wd_strict_term(Goal),
465 Fun = function(overwrite(F,S),G),
466 rewrite_once(Fun,function(S,G),Goal,Goal1),
467 add_hyp(member(G,domain(S)),Hyps,Hyps1),
468 rewrite_once(Fun,function(domain_subtraction(domain(S),F),G),Goal,Goal2),
469 add_hyp(negation(member(G,domain(S))),Hyps,Hyps2).
470 trans_wo_info(subset_inter,sequent(Hyps,Goal,Cont),sequent(Hyps,NewGoal,Cont)) :-
471 member(subset(T,U),Hyps),
472 free_identifiers(Goal,Ids),
473 ? member(T,Ids), % T and U are not bound
474 ? member(U,Ids),
475 ? rewrite_subterm(subset_inter_rw(T,U),Goal,NewGoal).
476 trans_wo_info(in_inter,sequent(Hyps,Goal,Cont),sequent(Hyps,NewGoal,Cont)) :-
477 member(member(E,T),Hyps),
478 free_identifiers(Goal,Ids),
479 ? member(E,Ids),
480 ? member(T,Ids),
481 ? rewrite_subterm(subset_inter_rw(set_extension([E]),T),Goal,NewGoal).
482 trans_wo_info(notin_inter,sequent(Hyps,Goal,Cont),sequent(Hyps,NewGoal,Cont)) :-
483 member(negation(member(E,T)),Hyps),
484 free_identifiers(Goal,Ids),
485 ? member(E,Ids),
486 ? member(T,Ids),
487 ? rewrite_subterm(notin_inter_rw(E,T),Goal,NewGoal).
488 trans_wo_info(card_interv,sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal1,sequent(Hyps2,Goal2,Cont))) :-
489 wd_strict_term(Goal),
490 rewrite_once(card(interval(A,B)),add(minus(B,A),1),Goal,Goal1),
491 rewrite_once(card(interval(A,B)),0,Goal,Goal2),
492 add_hyp(less_equal(A,B),Hyps,Hyps1),
493 add_hyp(less(B,A),Hyps,Hyps2).
494 trans_wo_info(card_empty_interv,sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,sequent(Hyps2,Goal,Cont))) :-
495 select(Hyp,Hyps,Hyps0),
496 wd_strict_term(Hyp),
497 rewrite_once(card(interval(A,B)),add(minus(B,A),1),Hyp,Hyp1),
498 rewrite_once(card(interval(A,B)),0,Hyp,Hyp2),
499 add_hyps([less_equal(A,B),Hyp1],Hyps0,Hyps1),
500 add_hyps([less(B,A),Hyp2],Hyps0,Hyps2).
501 trans_wo_info(simp_card_setminus_l,sequent(Hyps,Goal,Cont),sequent(Hyps,finite(S),sequent(Hyps1,Goal,Cont))) :-
502 select(Hyp,Hyps,Hyps0),
503 rewrite_once(card(set_subtraction(S,T)),minus(card(S),card(intersection(S,T))),Hyp,Hyp1),
504 add_hyp(Hyp1,Hyps0,Hyps1).
505 trans_wo_info(simp_card_setminus_r,sequent(Hyps,Goal,Cont),sequent(Hyps,finite(S),sequent(Hyps,NewGoal,Cont))) :-
506 rewrite_once(card(set_subtraction(S,T)),minus(card(S),card(intersection(S,T))),Goal,NewGoal).
507 trans_wo_info(simp_card_cprod_l,sequent(Hyps,Goal,Cont),sequent(Hyps,finite(S),sequent(Hyps,finite(T),sequent(NewHyps,Goal,Cont)))) :-
508 select(Hyp,Hyps,NewHyp,NewHyps),
509 rewrite_once(card(cartesian_product(S,T)),multiplication(card(S),card(T)),Hyp,NewHyp).
510 trans_wo_info(simp_card_cprod_r,sequent(Hyps,Goal,Cont),sequent(Hyps,finite(S),sequent(Hyps,finite(T),sequent(Hyps,NewGoal,Cont)))) :-
511 rewrite_once(card(cartesian_product(S,T)),multiplication(card(S),card(T)),Goal,NewGoal).
512 trans_wo_info(skip_to_cont,sequent(Hyps,Goal,Cont),NewState) :-
513 Cont \= success(_),
514 append_sequents(Cont,sequent(Hyps,Goal,success(PO_Nr)),NewState,PO_Nr).
515
516 finite_intersection(intersection(A,B),Disj) :- finite_intersection(A,DisjA), finite_intersection(B,DisjB), !, Disj = disjunct(DisjA,DisjB).
517 finite_intersection(S,finite(S)).
518
519 % trans_desc_wo_info/4
520 trans_desc_wo_info(eq(Dir,X,Y),sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal1,Cont),[description(Descr)]) :-
521 ? select(equal(X,Y),Hyps,Hyps0),
522 (Dir=lr,
523 maplist(rewrite(X,Y),Hyps0,Hyps1),
524 rewrite(X,Y,Goal,Goal1)
525 ; Dir=rl,
526 maplist(rewrite(Y,X),Hyps0,Hyps1),
527 rewrite(Y,X,Goal,Goal1)),
528 create_descr(eq,Dir,equal(X,Y),Descr).
529 trans_desc_wo_info(eqv(Dir,X,Y),sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal1,Cont),[description(Descr)]) :-
530 select(equivalence(X,Y),Hyps,Hyps0),
531 (Dir=lr,
532 maplist(rewrite(X,Y),Hyps0,Hyps1),
533 rewrite(X,Y,Goal,Goal1)
534 ; Dir=rl,
535 maplist(rewrite(Y,X),Hyps0,Hyps1),
536 rewrite(Y,X,Goal,Goal1)),
537 create_descr(eqv,Dir,equivalence(X,Y),Descr).
538
539 trans_with_args(and_l(Hyp),sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,Cont)) :-
540 % P & Q,... |- Goal ===> P,Q,... |- GOAL
541 Hyp = conjunct(P,Q),
542 select(Hyp,Hyps,Hyps0),
543 add_hyps(P,Q,Hyps0,Hyps1).
544 trans_with_args(imp_l1(Imp),sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,Cont)) :- % covers auto_mh
545 Imp = implication(P,Q),
546 ? select(Imp,Hyps,Res,Hyps1),
547 ? select_conjunct(PP,P,NewP),
548 ? member_hyps(PP,Hyps),
549 (NewP = truth -> Res = Q ; Res = implication(NewP,Q)).
550 trans_with_args(imp_and_l(Hyp),sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,Cont)) :-
551 Hyp = implication(P,conjunct(Q,R)),
552 select(Hyp,Hyps,Hyps0),
553 add_hyps(implication(P,Q),implication(P,R),Hyps0,Hyps1).
554 trans_with_args(imp_or_l(Hyp),sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,Cont)) :-
555 Hyp = implication(disjunct(P,Q),R),
556 select(Hyp,Hyps,Hyps0),
557 add_hyps(implication(P,R),implication(Q,R),Hyps0,Hyps1).
558 trans_with_args(contradict_l(P),sequent(Hyps,Goal,Cont),sequent(Hyps1,NotP,Cont)):-
559 select(P,Hyps,Hyps0),
560 negate(P,NotP),
561 negate(Goal,NotGoal),
562 add_hyp(NotGoal,Hyps0,Hyps1).
563 trans_with_args(or_l(Hyp),sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,sequent(Hyps2,Goal,Cont))) :-
564 Hyp = disjunct(P,Q),
565 select(Hyp,Hyps,Hyps0),
566 add_hyp(P,Hyps0,Hyps1),
567 add_hyp(Q,Hyps0,Hyps2).
568 trans_with_args(case(D),sequent(Hyps,Goal,Cont),NewState) :-
569 member(D,Hyps),
570 D = disjunct(_,_),
571 select(D,Hyps,Hyps0),
572 extract_disjuncts(D,Hyps0,Goal,Cont,NewState).
573 trans_with_args(imp_case(Imp),sequent(Hyps,Goal,Cont),sequent(Hyps1,Goal,sequent(Hyps2,Goal,Cont))) :-
574 Imp = implication(P,Q),
575 select(Imp,Hyps,Hyps0),
576 add_hyp(negation(P),Hyps0,Hyps1),
577 add_hyp(Q,Hyps0,Hyps2).
578 trans_with_args(mh(Imp),sequent(Hyps,Goal,Cont),sequent(Hyps0,P,sequent(Hyps1,Goal,Cont))) :-
579 Imp = implication(P,Q), % we prove P first and then can add Q as hypothesis
580 select(Imp,Hyps,Hyps0),
581 add_hyp(Q,Hyps0,Hyps1).
582 trans_with_args(hm(Imp),sequent(Hyps,Goal,Cont),sequent(Hyps0,negation(Q),sequent(Hyps1,Goal,Cont))) :-
583 Imp = implication(P,Q), % we prove not(Q) and then can add not(P) as hypothesis
584 select(Imp,Hyps,Hyps0),
585 add_hyp(negation(P),Hyps0,Hyps1).
586 trans_with_args(def_expn_step(power_of(E,P)),sequent(Hyps,Goal,Cont),sequent(Hyps,not_equal(P,0),sequent(NewHyps,Goal,Cont))) :-
587 select(Hyp,Hyps,Hyps0),
588 rewrite_once(power_of(E,P),multiplication(E,power_of(E,minus(P,1))),Hyp,NewHyp),
589 add_hyp(NewHyp,Hyps0,NewHyps).
590 trans_with_args(def_expn_step(power_of(E,P)),sequent(Hyps,Goal,Cont),sequent(Hyps,not_equal(P,0),sequent(Hyps,NewGoal,Cont))) :-
591 rewrite_once(power_of(E,P),multiplication(E,power_of(E,minus(P,1))),Goal,NewGoal).
592
593 % trans_with_args on entire state with info:
594 trans_with_args_info(reselect_hyp(Hyp),state(sequent(Hyps,Goal,Cont),Info),state(sequent(Hyps1,Goal,Cont),Info1)) :-
595 remove_meta_info(des_hyps,Info,Hyp,Info1),
596 add_hyp(Hyp,Hyps,Hyps1).
597 trans_with_args_info(one_point_l(Eq),state(sequent(Hyps,Goal,Cont),Info),state(NewSequent,Info)) :-
598 ? select(forall(Ids,P,Q),Hyps,Hyps0),
599 ? select_conjunct(Eq,P,P0),
600 ? is_equality(Eq,E,Y),
601 ? select(Y,Ids,Ids0),
602 no_bound_ids_in_set([Y],E), % Y does not occur free in E
603 rewrite(Y,E,P0,P1),
604 rewrite(Y,E,Q,Q1),
605 (Ids0 = [] -> NewHyp = implication(P1,Q1) ; NewHyp = forall(Ids0,P1,Q1)),
606 add_hyp(NewHyp,Hyps0,Hyps1),
607 get_wd_pos_of_expr(E,Hyps,Info,POs),
608 add_wd_pos(Hyps0,POs,sequent(Hyps1,Goal,Cont),NewSequent).
609 trans_with_args_info(one_point_r(Eq),state(sequent(Hyps,forall(Ids,P,Q),Cont),Info),state(NewSequent,Info)) :-
610 ? select_conjunct(Eq,P,P0),
611 ? is_equality(Eq,E,Y),
612 ? select(Y,Ids,Ids0),
613 no_bound_ids_in_set([Y],E), % Y does not occur free in E
614 rewrite(Y,E,P0,P1),
615 rewrite(Y,E,Q,Q1),
616 (Ids0 = [] -> NewGoal = implication(P1,Q1) ; NewGoal = forall(Ids0,P1,Q1)),
617 get_wd_pos_of_expr(E,Hyps,Info,POs),
618 add_wd_pos(Hyps,POs,sequent(Hyps,NewGoal,Cont),NewSequent).
619 trans_with_args_info(induc_nat(X),state(sequent(Hyps,Goal,Cont),Info),
620 state(sequent(Hyps,member(X,natural_set),sequent(Hyps1,Goal,sequent(Hyps2,GoalN1,Cont))),Info)) :-
621 free_identifiers(Goal,Ids),
622 member(X,Ids),
623 of_integer_type(X,Info),
624 new_identifier(Goal,N),
625 add_hyp(equal(X,0),Hyps,Hyps1),
626 rewrite(X,N,Goal,GoalN),
627 add_hyps([member(N,natural_set),GoalN],Hyps,Hyps2),
628 rewrite(X,add(N,1),Goal,GoalN1).
629 trans_with_args_info(induc_nat_compl(X),state(sequent(Hyps,Goal,Cont),Info),
630 state(sequent(Hyps,member(X,natural_set),sequent(Hyps,Goal0,sequent(Hyps2,GoalN,Cont))),Info)) :-
631 free_identifiers(Goal,Ids),
632 member(X,Ids),
633 of_integer_type(X,Info),
634 new_identifiers(Goal,2,[K,N]),
635 rewrite(X,0,Goal,Goal0),
636 rewrite(X,N,Goal,GoalN),
637 rewrite(X,K,Goal,GoalK),
638 add_hyps([member(N,natural_set),forall([K],conjunct(less_equal(0,K),less(K,N)),GoalK)],Hyps,Hyps2).
639
640
641 /*
642 sequent_prover_trans(auto_simplify,state(Sequent,Info),state(NewSequent,Info)) :-
643 apply_simp_rules(Sequent,Info,NewSequent),
644 Sequent \= NewSequent.
645
646 apply_simp_rules(Sequent,Info,Res) :-
647 ( sequent_prover_trans_desc(simplify_goal(Rule),state(Sequent,Info),state(NewSequent,Info),_)
648 ; sequent_prover_trans_desc(simplify_hyp(Rule,_),state(Sequent,Info),state(NewSequent,Info),_)),
649 trivial_rule(Rule), !,
650 apply_simp_rules(NewSequent,Info,Res).
651 % apply_simp_rules(sequent(Hyps,Goal,Cont),_,Cont) :- axiom(Rule,Hyps,Goal), !.
652 apply_simp_rules(Sequent,_,Sequent).
653
654 trivial_rule(Rule) :- sub_atom(Rule,_,_,_,'MULTI').
655 trivial_rule(Rule) :- sub_atom(Rule,_,_,_,'SPECIAL').
656 trivial_rule(Rule) :- sub_atom(Rule,_,_,_,'TYPE').
657 trivial_rule('Evaluate tautology').
658
659 */
660
661 symb_trans(Rule,state(Sequent,StateInfo),state(NewSequent,StateInfo),[]) :- symb_trans_rule(Rule,Sequent,NewSequent,StateInfo).
662 symb_trans(Rule,state(Sequent,StateInfo),state(NewSequent,StateInfo),TransInfo) :- symb_trans_rule(Rule,Sequent,NewSequent,StateInfo,TransInfo).
663 symb_trans_enabled(Rule,state(Sequent,_)) :- symb_trans_enabled(Rule,Sequent).
664
665 symb_trans_rule(add_hyp(Hyp),sequent(Hyps0,Goal,Cont),NewSequent,Info,[description(Desc)]) :- % CUT
666 parse_input(Hyp,add_hyp,TExpr),
667 normalize_predicate(TExpr,NormExpr),
668 get_wd_pos(NormExpr,Hyps0,Info,POs),
669 add_hyps(POs,Hyps0,Hyps1),
670 translate_finite_expr(NormExpr,NewExpr),
671 add_hyps([NewExpr|POs],Hyps0,Hyps2),
672 add_wd_pos(Hyps0,POs,sequent(Hyps1,NewExpr,sequent(Hyps2,Goal,Cont)),NewSequent),
673 translate_norm_expr_term(NewExpr,PrettyExpr),
674 ajoin(['add hypothesis: \'',PrettyExpr,'\''],Desc).
675 trans_prop(add_hyp,param_names(['Hyp'])).
676 symb_trans_enabled(add_hyp,sequent(_,_,_)).
677
678 symb_trans_rule(distinct_case(Pred),sequent(Hyps0,Goal,Cont),NewSequent,Info) :-
679 parse_input(Pred,distinct_case,TExpr),
680 normalize_predicate(TExpr,NormExpr),
681 get_wd_pos(NormExpr,Hyps0,Info,POs),
682 translate_finite_expr(NormExpr,NewExpr),
683 add_hyps([NewExpr|POs],Hyps0,Hyps1),
684 add_hyps([negation(NewExpr)|POs],Hyps0,Hyps2),
685 add_wd_pos(Hyps0,POs,sequent(Hyps1,Goal,sequent(Hyps2,Goal,Cont)),NewSequent).
686 trans_prop(distinct_case,param_names(['Pred'])).
687 symb_trans_enabled(distinct_case,sequent(_,_,_)).
688
689 symb_trans_rule(exists_inst(Inst),sequent(Hyps,exists([X|Ids],Pred),Cont),NewSequent,Info) :-
690 parse_input(Inst,exists_inst,TExpr),
691 normalize_expression(TExpr,NormExpr),
692 % rewrite the remaining quantifier as a whole, so that remaining bound ids are renamed if they occur in NormExpr
693 (Ids = [] -> rewrite(X,NormExpr,Pred,Goal) ; rewrite(X,NormExpr,exists(Ids,Pred),Goal)),
694 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
695 add_wd_pos(Hyps,POs,sequent(Hyps,Goal,Cont),NewSequent).
696 trans_prop(exists_inst,param_names(['Inst'])).
697 symb_trans_enabled(exists_inst,sequent(_,exists(_,_),_)).
698
699 symb_trans_rule(forall_inst(HypNr,Inst),sequent(Hyps,Goal,Cont),NewSequent,Info,[description(Desc)]) :-
700 get_hyp(Hyps,HypNr,SelHyp),
701 SelHyp = forall([X|Ids],P,Q), % TODO: is this compatible with Rodin?
702 parse_input(Inst,forall_inst,TExpr),
703 normalize_expression(TExpr,NormExpr),
704 select(SelHyp,Hyps,NewHyp,NewHyps),
705 % rewrite the remaining quantifier as a whole, so that remaining bound ids are renamed if they occur in NormExpr
706 (Ids = [] -> rewrite(X,NormExpr,implication(P,Q),NewHyp) ; rewrite(X,NormExpr,forall(Ids,P,Q),NewHyp)),
707 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
708 add_wd_pos(Hyps,POs,sequent(NewHyps,Goal,Cont),NewSequent),
709 translate_norm_expr_term(NormExpr,PrettyExpr),
710 X='$'(XName),
711 ajoin(['\x2200\ instantiation in hyp ',HypNr,' for ID ',XName,': \'',PrettyExpr,'\''],Desc).
712 trans_prop(forall_inst,param_names(['HypNr','Inst'])).
713 symb_trans_enabled(forall_inst(HypNr,none),sequent(Hyps,_,_)) :- nth1(HypNr,Hyps,forall(_,_,_)).
714
715 symb_trans_rule(forall_inst_mp(HypNr,Inst),sequent(Hyps,Goal,Cont),NewSequent,Info) :-
716 get_hyp(Hyps,HypNr,SelHyp),
717 SelHyp = forall([X],P,Q),
718 parse_input(Inst,forall_inst_mp,TExpr),
719 normalize_expression(TExpr,NormExpr),
720 rewrite(X,NormExpr,P,P1),
721 rewrite(X,NormExpr,Q,Q1),
722 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
723 select(SelHyp,Hyps,Hyps0),
724 add_hyps(POs,Hyps0,Hyps1),
725 add_hyps([Q1|POs],Hyps0,Hyps2),
726 add_wd_pos(Hyps,POs,sequent(Hyps1,P1,sequent(Hyps2,Goal,Cont)),NewSequent).
727 trans_prop(forall_inst_mp,param_names(['HypNr','Inst'])).
728 symb_trans_enabled(forall_inst_mp,sequent(Hyps,_,_)) :- memberchk(forall([_],_,_),Hyps).
729
730 symb_trans_rule(forall_inst_mt(HypNr,Inst),sequent(Hyps,Goal,Cont),NewSequent,Info) :-
731 get_hyp(Hyps,HypNr,SelHyp),
732 SelHyp = forall([X],P,Q),
733 parse_input(Inst,forall_inst_mt,TExpr),
734 normalize_expression(TExpr,NormExpr),
735 rewrite(X,NormExpr,negation(P),P1),
736 rewrite(X,NormExpr,negation(Q),Q1),
737 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
738 select(SelHyp,Hyps,Hyps0),
739 add_hyps(POs,Hyps0,Hyps1),
740 add_hyps([P1|POs],Hyps0,Hyps2),
741 add_wd_pos(Hyps,POs,sequent(Hyps1,Q1,sequent(Hyps2,Goal,Cont)),NewSequent).
742 trans_prop(forall_inst_mt,param_names(['HypNr','Inst'])).
743 symb_trans_enabled(forall_inst_mt,sequent(Hyps,_,_)) :- memberchk(forall([_],_,_),Hyps).
744
745 symb_trans_rule(Cmd,sequent(Hyps,Goal,Cont),Cont,_) :-
746 maplist(convert_norm_expr_to_raw,Hyps,RawHyps),
747 convert_norm_expr_to_raw(Goal,RawGoal),
748 use_prover(Cmd,RawHyps,RawGoal).
749 trans_prop(Cmd,param_names([])) :- prover_command(Cmd,_).
750 symb_trans_enabled(Cmd,sequent(_,_,_)) :- prover_command(Cmd,_).
751
752 symb_trans_rule(prob_disprover,sequent(Hyps,Goal,Cont),NewState,Info) :-
753 convert_norm_expr_to_raw(Goal,RawGoal),
754 maplist(convert_norm_expr_to_raw,Hyps,RawHyps),
755 use_prover(prob_disprover,RawHyps,RawGoal,OutResult),
756 format(user_output,'ProB Disprover Result = ~w (Info=~w)~n',[OutResult,Info]),
757 dispatch_result(OutResult,Cont,NewState).
758 trans_prop(prob_disprover,param_names([])).
759 symb_trans_enabled(prob_disprover,sequent(_,_,_)).
760
761 symb_trans_rule(z3,sequent(Hyps,Goal,Cont),NewState,Info) :-
762 maplist(convert_norm_expr_to_raw,Hyps,RawHyps),
763 convert_norm_expr_to_raw(Goal,RawGoal),
764 use_prover(z3,RawHyps,RawGoal,Res),
765 format(user_output,'z3 Result = ~w (Info=~w)~n',[Res,Info]),
766 dispatch_result(Res,Cont,NewState).
767 trans_prop(z3,param_names([])).
768 symb_trans_enabled(z3,sequent(_,_,_)).
769
770 dispatch_result(contradiction_found,Cont,Cont).
771 dispatch_result(contradiction_in_hypotheses,Cont,Cont).
772 dispatch_result(solution_on_selected_hypotheses(S),_,counter_example(S)).
773 dispatch_result(solution(S),_,counter_example(S)).
774 % other results are no_solution_found(_) and time_out
775
776 symb_trans_rule(rewrite_hyp(HypNr,NewHyp),sequent(Hyps,Goal,Cont),sequent(NewHyps,Goal,Cont),Info) :-
777 get_hyp(Hyps,HypNr,SelHyp),
778 parse_input(NewHyp,rewrite_hyp,TExpr),
779 b_interpreter_check:norm_pred_check(TExpr,RewrittenHyp),
780 select(SelHyp,Hyps,Hyps0),
781 get_wd_pos(RewrittenHyp,Hyps,Info,[]),
782 prove_predicate([SelHyp],RewrittenHyp),
783 translate_finite_expr(RewrittenHyp,NewExpr),
784 add_hyp(NewExpr,Hyps0,NewHyps).
785 trans_prop(rewrite_hyp,param_names(['HypNr','NewHyp'])).
786 symb_trans_enabled(rewrite_hyp,sequent(_,_,_)).
787
788 :- use_module(probsrc(tools),[split_atom/3]).
789 symb_trans_rule(derive_hyp(HypNrs,NewHyp),sequent(Hyps,Goal,Cont),sequent(NewHyps,Goal,Cont),Info) :-
790 ground(HypNrs),
791 split_atom(HypNrs,[','],Indices),
792 maplist(get_hyp(Hyps),Indices,SelHyps),
793 parse_input(NewHyp,derive_hyp,TExpr),
794 b_interpreter_check:norm_pred_check(TExpr,NormExpr),
795 get_wd_pos(NormExpr,Hyps,Info,[]),
796 prove_predicate(SelHyps,NormExpr),
797 translate_finite_expr(NormExpr,NewExpr),
798 add_hyp(NewExpr,Hyps,NewHyps).
799 trans_prop(derive_hyp,param_names(['HypNrs','NewHyp'])).
800 symb_trans_enabled(derive_hyp,sequent(_,_,_)).
801
802 symb_trans_rule(dis_binter_r(PFun),sequent(Hyps,Goal,Cont),sequent(Hyps,NormExpr,sequent(Hyps,NewGoal,Cont)),Info) :-
803 parse_input(PFun,dis_binter_r,TExpr),
804 normalize_predicate(TExpr,NormExpr),
805 NormExpr = member(reverse(F),partial_function(A,B)),
806 type_expression(A,Info),
807 type_expression(B,Info),
808 rewrite_subterm(dis_binter(F),Goal,NewGoal).
809 trans_prop(dis_binter_r,param_names(['PFun'])).
810 symb_trans_enabled(dis_binter_r,sequent(_,Goal,_)) :- is_subterm(image(_,Inter),Goal), Inter = intersection(_,_).
811
812 symb_trans_rule(dis_binter_l(PFun),sequent(Hyps,Goal,Cont),sequent(Hyps0,NormExpr,sequent(Hyps1,Goal,Cont)),Info) :-
813 parse_input(PFun,dis_binter_l,TExpr),
814 normalize_predicate(TExpr,NormExpr),
815 NormExpr = member(reverse(F),partial_function(A,B)),
816 type_expression(A,Info),
817 type_expression(B,Info),
818 select(Hyp,Hyps,Hyps0),
819 rewrite_subterm(dis_binter(F),Hyp,NewHyp),
820 add_hyp(NewHyp,Hyps0,Hyps1).
821 trans_prop(dis_binter_l,param_names(['PFun'])).
822 symb_trans_enabled(dis_binter_l,sequent(Hyps,_,_)) :- member(Hyp,Hyps), is_subterm(image(_,Inter),Hyp), Inter = intersection(_,_).
823
824 symb_trans_rule(dis_setminus_r(PFun),sequent(Hyps,Goal,Cont),sequent(Hyps,NormExpr,sequent(Hyps,NewGoal,Cont)),Info) :-
825 parse_input(PFun,dis_setminus_r,TExpr),
826 normalize_predicate(TExpr,NormExpr),
827 NormExpr = member(reverse(F),partial_function(A,B)),
828 type_expression(A,Info),
829 type_expression(B,Info),
830 rewrite_subterm(dis_setminus(F),Goal,NewGoal).
831 trans_prop(dis_setminus_r,param_names(['PFun'])).
832 symb_trans_enabled(dis_setminus_r,sequent(_,Goal,_)) :- is_subterm(image(_,set_subtraction(_,_)),Goal).
833
834 symb_trans_rule(dis_setminus_l(PFun),sequent(Hyps,Goal,Cont),sequent(Hyps0,NormExpr,sequent(Hyps1,Goal,Cont)),Info) :-
835 parse_input(PFun,dis_setminus_l,TExpr),
836 normalize_predicate(TExpr,NormExpr), !,
837 NormExpr = member(reverse(F),partial_function(A,B)),
838 type_expression(A,Info),
839 type_expression(B,Info),
840 select(Hyp,Hyps,Hyps0),
841 rewrite_subterm(dis_setminus(F),Hyp,NewHyp),
842 add_hyp(NewHyp,Hyps0,Hyps1).
843 trans_prop(dis_setminus_l,param_names(['PFun'])).
844 symb_trans_enabled(dis_setminus_l,sequent(Hyps,_,_)) :- member(Hyp,Hyps), is_subterm(image(_,set_subtraction(_,_)),Hyp).
845
846 symb_trans_rule(fin_subseteq_r(Set),sequent(Hyps,finite(S),Cont),NewSequent,Info) :-
847 parse_input(Set,fin_subseteq_r,TExpr),
848 normalize_expression(TExpr,NormExpr),
849 get_meta_info(ids_types,Info,IdsTypes), % NormExpr has to be a set
850 same_type(S,NormExpr,IdsTypes),
851 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
852 add_wd_pos(Hyps,POs,sequent(Hyps,subset(S,NormExpr),sequent(Hyps,finite(NormExpr),Cont)),NewSequent).
853 trans_prop(fin_subseteq_r,param_names(['Set'])).
854 symb_trans_enabled(fin_subseteq_r,sequent(_,finite(_),_)).
855
856 symb_trans_rule(fin_fun1_r(PFun),sequent(Hyps,finite(F),Cont),NewSequent,Info) :-
857 parse_input(PFun,fin_fun1_r,TExpr),
858 normalize_expression(TExpr,NormExpr),
859 NormExpr = partial_function(S,T),
860 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
861 add_wd_pos(Hyps,POs,sequent(Hyps,member(F,partial_function(S,T)),sequent(Hyps,finite(S),Cont)),NewSequent).
862 trans_prop(fin_fun1_r,param_names(['PFun'])).
863 symb_trans_enabled(fin_fun1_r,sequent(_,finite(_),_)).
864
865 symb_trans_rule(fin_fun2_r(PFun),sequent(Hyps,finite(F),Cont),NewSequent,Info) :-
866 parse_input(PFun,fin_fun2_r,TExpr),
867 normalize_expression(TExpr,NormExpr),
868 NormExpr = partial_function(S,T),
869 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
870 add_wd_pos(Hyps,POs,sequent(Hyps,member(reverse(F),partial_function(S,T)),sequent(Hyps,finite(S),Cont)),NewSequent).
871 trans_prop(fin_fun2_r,param_names(['PFun'])).
872 symb_trans_enabled(fin_fun2_r,sequent(_,finite(_),_)).
873
874 symb_trans_rule(fin_fun_img_r(PFun),sequent(Hyps,finite(image(F,Set)),Cont),NewSequent,Info) :-
875 parse_input(PFun,fin_fun_img_r,TExpr),
876 normalize_expression(TExpr,NormExpr),
877 NormExpr = partial_function(S,T),
878 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
879 add_wd_pos(Hyps,POs,sequent(Hyps,member(F,partial_function(S,T)),sequent(Hyps,finite(Set),Cont)),NewSequent).
880 trans_prop(fin_fun_img_r,param_names(['PFun'])).
881 symb_trans_enabled(fin_fun_img_r,sequent(_,finite(image(_,_)),_)).
882
883 symb_trans_rule(fin_fun_ran_r(PFun),sequent(Hyps,finite(range(F)),Cont),NewSequent,Info) :-
884 parse_input(PFun,fin_fun_ran_r,TExpr),
885 normalize_expression(TExpr,NormExpr),
886 NormExpr = partial_function(S,T),
887 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
888 add_wd_pos(Hyps,POs,sequent(Hyps,member(F,partial_function(S,T)),sequent(Hyps,finite(S),Cont)),NewSequent).
889 trans_prop(fin_fun_ran_r,param_names(['PFun'])).
890 symb_trans_enabled(fin_fun_ran_r,sequent(_,finite(range(_)),_)).
891
892 symb_trans_rule(fin_fun_dom_r(PFun),sequent(Hyps,finite(domain(F)),Cont),NewSequent,Info) :-
893 parse_input(PFun,fin_fun_dom_r,TExpr),
894 normalize_expression(TExpr,NormExpr),
895 NormExpr = partial_function(S,T),
896 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
897 add_wd_pos(Hyps,POs,sequent(Hyps,member(reverse(F),partial_function(S,T)),sequent(Hyps,finite(S),Cont)),NewSequent).
898 trans_prop(fin_fun_dom_r,param_names(['PFun'])).
899 symb_trans_enabled(fin_fun_dom_r,sequent(_,finite(domain(_)),_)).
900
901 symb_trans_rule(fin_rel_r(Rel),sequent(Hyps,finite(R),Cont),NewSequent,Info) :-
902 parse_input(Rel,fin_rel_r,TExpr),
903 normalize_expression(TExpr,NormExpr), !,
904 NormExpr = relations(S,T),
905 get_wd_pos_of_expr(NormExpr,Hyps,Info,POs),
906 add_wd_pos(Hyps,POs,sequent(Hyps,member(R,relations(S,T)),sequent(Hyps,finite(S),sequent(Hyps,finite(T),Cont))),NewSequent).
907 trans_prop(fin_rel_r,param_names(['Rel'])).
908 symb_trans_enabled(fin_rel_r,sequent(_,finite(_),_)).
909
910 %parse_input(Param,Res) :- parse_input(Param,'?',Res).
911 parse_input(Param,Context,Res) :-
912 ground(Param),
913 (number(Param) -> number_codes(Param,CParam)
914 ; Param = [Nr|_], number(Nr) -> CParam = Param % already codes list
915 ; atom(Param) -> atom_codes(Param,CParam)
916 ; ajoin(['Illegal input to proof rule ',Context,' (must be Prolog atom or string):'],Msg),
917 add_error(sequent_prover,Msg,Param), fail
918 ),
919 bmachine:b_parse_only(formula,CParam,Parsed,_,_Error),
920 transform_raw(Parsed,TExpr),
921 adapt_for_eventb(TExpr,Res).
922
923 adapt_for_eventb(Term,Res) :- atomic(Term), !, Res=Term.
924 adapt_for_eventb(Term,Result) :-
925 Term=..[F|Args],
926 replace_functor(F,NewFunctor),
927 maplist(adapt_for_eventb,Args,NewArgs),
928 Result=..[NewFunctor|NewArgs].
929
930 replace_functor(F,NF) :-
931 replace_functor_eventb(F,EF), !, NF=EF.
932 replace_functor(F,F).
933
934 replace_functor_eventb(concat,power_of).
935 replace_functor_eventb(power_of,cartesian_product).
936 replace_functor_eventb(minus_or_set_subtract,minus).
937 replace_functor_eventb(mult_or_cart,multiplication).
938
939 translate_finite_expr(X,Y) :- rewrite(member(S,fin_subset(S)),finite(S),X,Y).
940
941 get_hyp(Hyps,Nr,Hyp) :-
942 number(Nr),
943 nth1(Nr,Hyps,Hyp).
944
945 add_hyp(Hyp,Hyps0,NewHyps) :- append(Hyps0,[Hyp],Hyps1), without_duplicates(Hyps1,NewHyps).
946 add_hyps(Hyp1,Hyp2,Hyps0,NewHyps) :- append(Hyps0,[Hyp1,Hyp2],Hyps1), without_duplicates(Hyps1,NewHyps).
947 add_hyps(NewHyps,Hyps,SHyps) :- append(Hyps,NewHyps,All), without_duplicates(All,SHyps).
948
949 % select and remove a conjunct:
950 select_conjunct(X,conjunct(A,B),Rest) :- !,
951 ? (select_conjunct(X,A,RA), conjoin(RA,B,Rest)
952 ;
953 select_conjunct(X,B,RB), conjoin(A,RB,Rest)).
954 select_conjunct(X,X,truth).
955
956 conjoin(truth,X,R) :- !, R=X.
957 conjoin(X,truth,R) :- !, R=X.
958 conjoin(X,Y,conjunct(X,Y)).
959
960
961
962
963 % axioms: proof rules without antecedent, discharging goal directly
964 ?axiom(hyp,Hyps,Goal) :- member_hyps(Goal,Hyps).
965 axiom(hyp,Hyps,member(X,Nat)) :-
966 is_natural_set(Nat),
967 (member_hyps(greater_equal(X,0),Hyps) ; member_hyps(greater_equal(X,1),Hyps)).
968 axiom(hyp_or,Hyps,Goal) :-
969 ? member_of_bin_op(disjunct,P,Goal), % TODO: will not select a disjunction for P
970 member_hyps(P,Hyps).
971 axiom(false_hyp,Hyps,_Goal) :- member_hyps(falsity,Hyps).
972 axiom(true_goal,_,truth).
973 axiom(Rule,_Hyps,Goal) :- axiom_wo_hyps(Goal,Rule).
974 axiom(cntr,Hyps,_) :-
975 ? member(P,Hyps),
976 (NotP = negation(P) ; negate(P,NotP)),
977 ? select(P,Hyps,Hyps0),
978 ? member_hyps(NotP,Hyps0).
979 axiom(fin_rel,Hyps,finite(Fun)) :-
980 member_hyps(member(Fun,FunType),Hyps),
981 is_rel(FunType,_,Dom,Ran),
982 member_hyps(finite(Dom),Hyps),
983 member_hyps(finite(Ran),Hyps).
984 axiom(fin_l_lower_bound,Hyps,exists([N],forall([X],member(X,S),LessEq))) :-
985 is_less_eq(LessEq,N,X),
986 no_bound_ids_in_set([N,X],S),
987 member_hyps(finite(S),Hyps).
988 axiom(fin_l_upper_bound,Hyps,exists([N],forall([X],member(X,S),LessEq))) :-
989 is_less_eq(LessEq,X,N),
990 no_bound_ids_in_set([N,X],S),
991 member_hyps(finite(S),Hyps).
992 axiom(derive_goal,Hyps,Goal) :- derive_goal(Hyps,Goal), \+ is_true(Goal), \+ axiom(hyp,Hyps,Goal).
993
994 % from chapter 2, "Modeling in Event-B"
995 axiom(p2,Hyps,member(Add,Nat)) :-
996 is_natural_set(Nat),
997 member(member(N,Nat),Hyps),
998 equal_terms(Add,add(N,1)).
999 axiom(p2_,Hyps,member(Sub,Nat)) :-
1000 is_natural_set(Nat),
1001 member(L,Hyps),
1002 is_less(L,0,N),
1003 equal_terms(Sub,minus(N,1)).
1004 axiom(inc,Hyps,Goal) :-
1005 member(L,Hyps),
1006 is_less(L,N,M),
1007 equal_terms(Goal,less_equal(add(N,1),M)).
1008 axiom(dec,Hyps,Goal) :-
1009 member(L,Hyps),
1010 is_less_eq(L,N,M),
1011 equal_terms(Goal,less(minus(N,1),M)).
1012
1013 axiom_with_info(fun_goal,Hyps,member(F,partial_function(Ty1,Ty2)),Info) :-
1014 ? type_expression(Ty1,Info),
1015 ? type_expression(Ty2,Info),
1016 member_hyps(member(F,FType),Hyps),
1017 is_fun(FType,_,_,_).
1018 axiom_with_info(fun_goal_rec,Hyps,member(function(Fun,En),partial_function(Ty1,Ty2)),Info) :-
1019 ? type_expression(Ty1,Info),
1020 ? type_expression(Ty2,Info),
1021 function_of(Fun,F),
1022 member(member(F,FType),Hyps),
1023 ? fun_goal_check(FType,function(Fun,En)).
1024
1025 axiom_wo_hyps(true,true_goal).
1026 axiom_wo_hyps(eq(E,E),eql).
1027
1028
1029 % rules implemented MembershipGoal reasoner
1030 % mb_goal(Rule,Hyp,Goal,Info).
1031 mb_goal('SUBSET_SUBSETEQ',subset_strict(A,B),subset(A,B),_).
1032 mb_goal('DOM_SUBSET',subset(A,B),subset(domain(A),domain(B)),_).
1033 mb_goal('RAN_SUBSET',subset(A,B),subset(range(A),range(B)),_).
1034 mb_goal('EQUAL_SUBSETEQ',Eq,subset(A,B),Info) :-
1035 is_equality(Eq,A,B),
1036 get_meta_info(ids_types,Info,IdsTypes),
1037 check_expr_type(A,B,IdsTypes,set(_)).
1038 mb_goal('IN_DOM_CPROD',member(X,domain(cartesian_product(A,_))),member(X,A),_).
1039 mb_goal('IN_RAN_CPROD',member(Y,range(cartesian_product(_,B))),member(Y,B),_).
1040 mb_goal('IN_DOM_REL',member(couple(X,_),F),member(X,domain(F)),_).
1041 mb_goal('IN_RAN_REL',member(couple(_,Y),F),member(Y,range(F)),_).
1042 ?mb_goal('SETENUM_SUBSET',subset(set_extension(L),A),member(X,A),_) :- member(X,L).
1043 mb_goal('OVR_RIGHT_SUBSET',subset(LHS1,A),subset(LHS2,A),_) :-
1044 op_to_list(LHS1,List1,overwrite),
1045 op_to_list(LHS2,List2,overwrite),
1046 append(L,List2,List1),
1047 L \= [].
1048 mb_goal('RELSET_SUBSET_CPROD',member(F,Rel),subset(F,cartesian_product(Dom,Ran)),_) :- is_rel(Rel,_,Dom,Ran).
1049
1050 % mb_goal(Rule,Hyps,Goal).
1051 ?mb_goal('DERIV_IN_SUBSET',Hyps,member(X,B)) :- member(member(X,A),Hyps), member(subset(A,B),Hyps).
1052
1053 derive_goal(Hyps,Goal) :-
1054 member(Goal,Hyps).
1055 %functor(Goal,F,2),
1056 %(comparison(F) ; F = member),
1057 %used_identifiers(Goal,GoalIds),
1058 %find_hyps_with(GoalIds,Hyps,SelectedHyps),
1059 %use_prover(prob_wd_prover,SelectedHyps,Goal).
1060
1061 find_hyps_with(_,[],[]).
1062 find_hyps_with(Ids,[Hyp|T],[Hyp|R]) :-
1063 used_identifiers(Hyp,HypIds),
1064 \+ list_intersection(Ids,HypIds,[]), !,
1065 find_hyps_with(Ids,T,R).
1066 find_hyps_with(Ids,[_|T],R) :- find_hyps_with(Ids,T,R).
1067
1068 /* not used:
1069 is_transitive(Hyps,Goal) :-
1070 Goal=..[F,L,R],
1071 comparison(F),
1072 F \= not_equal,
1073 is_transitive_aux(Hyps,F,L,R,[]).
1074
1075 is_transitive_aux(Hyps,F,L,R,_) :-
1076 Ex=..[F,L,R],
1077 (member(Ex,Hyps) ; equiv(Ex,G2), member(G2,Hyps)).
1078 is_transitive_aux(Hyps,F,L,R,Visited) :-
1079 Ex=..[F,L,M],
1080 (member(Ex,Hyps) ; equiv(Ex,G2), member(G2,Hyps)),
1081 \+ member(M,Visited),
1082 is_transitive_aux(Hyps,F,M,R,[L|Visited]).
1083
1084 lower_bound(Hyps,B,X) :- member(L,Hyps), is_less_eq(L,B,X), number(B).
1085 lower_bound(Hyps,B1,X) :- member(L,Hyps), is_less(L,B,X), number(B), B1 is B+1.
1086 lower_bound(Hyps,B,X) :- member(Eq,Hyps), is_equality(Eq,B,X), number(B).
1087
1088 upper_bound(Hyps,X,B) :- member(L,Hyps),is_less_eq(L,X,B), number(B).
1089 upper_bound(Hyps,X,B1) :- member(L,Hyps), is_less(L,X,B), number(B), B1 is B-1.
1090 upper_bound(Hyps,X,B) :- member(Eq,Hyps), is_equality(Eq,X,B), number(B).
1091 */
1092
1093
1094 add_wd_pos(Hyps,POs,Sequent,NewSequent) :-
1095 rodin_mode, !,
1096 (POs=[] -> Conj=truth ; list_to_op(POs,Conj,conjunct)),
1097 % Rodin requires true goal for empty list and conjunction of WD conditions
1098 add_pos(Hyps,Sequent,NewSequent,[Conj]).
1099 add_wd_pos(Hyps,POs,Sequent,NewSequent) :-
1100 add_goals(Hyps,POs,Sequent,NewSequent).
1101
1102 add_goals(Hyps,POs,Sequent,NewSequent) :-
1103 reverse(POs,List),
1104 add_pos(Hyps,Sequent,NewSequent,List).
1105
1106 add_pos(_,Sequent,Sequent,[]) :- !.
1107 add_pos(Hyps,InnerSequent,NewSequent,[PO|R]) :- add_pos(Hyps,sequent(Hyps,PO,InnerSequent),NewSequent,R).
1108
1109 % add a sequent per disjunct
1110 extract_disjuncts(Q,Hyps0,Goal,Cont,sequent(Hyps,Goal,Cont)) :- Q \= disjunct(_,_), add_hyp(Q,Hyps0,Hyps).
1111 extract_disjuncts(disjunct(L,R),Hyps0,Goal,Cont,LRSequent) :-
1112 extract_disjuncts(L,Hyps0,Goal,success(PO_Nr),LSequent),
1113 extract_disjuncts(R,Hyps0,Goal,Cont,RSequent),
1114 append_sequents(LSequent,RSequent,LRSequent,PO_Nr).
1115
1116 % add sequents as last continuation
1117 append_sequents(sequent(Hyps,Goal,Cont),InnerCont,sequent(Hyps,Goal,Sequent),PO_Nr) :-
1118 (Cont = success(PO_Nr) -> Sequent = InnerCont
1119 ; append_sequents(Cont,InnerCont,Sequent,PO_Nr)).
1120
1121 animation_function_result(state(counter_example(_),_),[((1,1),'Counter example found.')]).
1122 animation_function_result(state(success(Nr),_),[((1,1),Txt)]) :-
1123 (get_po_description(Nr,PO) -> ajoin(['Proof of ',PO,' succeeded.'],Txt)
1124 ; Txt = 'Proof succeeded.').
1125 animation_function_result(state(Sequent,_),Matrix) :-
1126 Sequent = sequent(_,_,Cont),
1127 cont_length(Cont,LCont),
1128 findall(((RowNr,ColNr),Cell), (cell_content(RowNr,ColNr,Sequent,Cell,LCont)), Matrix).
1129
1130 cell_content(Row,Col,Sequent,Cell,_) :- % Auto-Proof buttons
1131 max_hyp_length(Sequent,0,LHyps),
1132 member(Col,[1,2,3]),D is Col+1,
1133 (Row is LHyps + 3, ajoin(['auto-',D],Cell)
1134 ; Row is LHyps + 4, ajoin(['pure-',D],Cell)).
1135 cell_content(RowNr,ColNr,Sequent,Cell,LCont) :- cell_content2(RowNr,ColNr,Sequent,Cell,LCont).
1136
1137 max_hyp_length(sequent(Hyps,_,Cont),CurMax,Res) :- length(Hyps,Len),!,
1138 (Len>CurMax -> max_hyp_length(Cont,Len,Res) ; max_hyp_length(Cont,CurMax,Res)).
1139 max_hyp_length(_,Max,Max).
1140
1141 cell_content2(Row,1,sequent(Hyps,_,_),Cell,_) :- nth1(Row,Hyps,RowHyp), translate_norm_expr_term(RowHyp,Cell).
1142 cell_content2(Row,1,sequent(Hyps,_,_),'\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\\x2500\',_) :-
1143 length(Hyps,LHyps), Row is LHyps + 1.
1144 cell_content2(Row,1,sequent(Hyps,Goal,_),Cell,_) :-
1145 length(Hyps,LHyps), Row is LHyps + 2, translate_norm_expr_term(Goal,Cell).
1146 cell_content2(Row,Col,Sequent,Cell,LCont) :-
1147 extract_continuations(Sequent,LCont,Cont,Col),
1148 cell_content2(Row,1,Cont,Cell,LCont).
1149
1150 extract_continuations(sequent(_,_,Cont),LCont,Cont,ColNr) :-
1151 cont_length(Cont,ActualLCont),
1152 ColNr is LCont - ActualLCont + 2.
1153 extract_continuations(sequent(_,_,Cont),LCont,FoundCont,ColNr) :-
1154 extract_continuations(Cont,LCont,FoundCont,ColNr).
1155
1156 get_continuations(success(_),[]).
1157 get_continuations(sequent(_,_,Cont),Conts) :- % ignore current sequent
1158 get_continuations2(Cont,Conts).
1159 get_continuations2(success(_),[]).
1160 get_continuations2(sequent(Hyps,Goal,NxtCont),[sequent(Hyps,Goal)|CT]) :-
1161 get_continuations2(NxtCont,CT).
1162
1163 find_transitions(Sequent,Info,Trans) :-
1164 findall(T,( sequent_prover_trans(T,state(Sequent,Info),_)
1165 ; sequent_prover_trans_desc(T,state(Sequent,Info),_,_)),TransL),
1166 remove_dups(TransL,Trans).
1167
1168 % animation_image_right_click_transition(RowX,ColY,OperationTemplate,State)
1169 animation_image_right_click_transition(Row,1,Trans,state(sequent(Hyps,Goal,Cont),Info)) :- nth1(Row,Hyps,Hyp),
1170 select(Hyp,Hyps,Hyps0),
1171 find_transitions(sequent(Hyps,Goal,Cont),Info,AllTransitions),
1172 find_transitions(sequent(Hyps0,Goal,Cont),Info,Transitions0),
1173 member(Trans,AllTransitions),
1174 Trans \= mon_deselect(_),
1175 \+ member(Trans,Transitions0).
1176 animation_image_right_click_transition(Row,1,Trans,state(sequent(Hyps,Goal,Cont),Info)) :-
1177 length(Hyps,LHyps),
1178 Row is LHyps + 2, % click on goal
1179 find_transitions(sequent([],Goal,Cont),Info,Transitions),
1180 member(Trans,Transitions),
1181 Trans \= reselect_hyp(_).
1182 animation_image_right_click_transition(Row,_C,Proof,state(sequent(Hyps,Goal,_),Info)) :-
1183 length(Hyps,LHyps), Row is LHyps + 1, % click on divider line
1184 (bounded_find_proof(sequent(Hyps,Goal,success(0)),Info,3,[],Proof) -> true ; Proof = 'NO AUTO-PROOF-FOUND').
1185 animation_image_right_click_transition(Row,Col,Proof,state(sequent(Hyps,Goal,_),Info)) :-
1186 length(Hyps,LHyps), Row > LHyps + 2, % click on Auto-Proof buttons below goal
1187 Depth is Col+1,
1188 Opts = [use_eager_simplification|TOpts],
1189 (Row =:= LHyps+3 -> TOpts = [] ; TOpts = [disregard_rule(derive_goal)]),
1190 (bounded_find_proof(sequent(Hyps,Goal,success(0)),Info,Depth,Opts,Proof) -> true ; Proof = 'NO AUTO-PROOF-FOUND').
1191
1192 % animation_image_right_click_transition(X,Y,OperationTemplate)
1193 animation_image_right_click_transition(Row,1,mon_deselect(Row)).
1194
1195 % ------------------------
1196
1197
1198
1199
1200 function_of(F,F) :- F \= function(_,_).
1201 function_of(function(Fun,_),F) :- function_of(Fun,F).
1202
1203 fun_goal_check(Fun,F) :-
1204 F \= function(_,_),
1205 is_fun(Fun,_,_,_).
1206 fun_goal_check(Rel,function(F,_)) :-
1207 is_rel(Rel,_,_,Ran),
1208 ? fun_goal_check(Ran,F).
1209
1210
1211 ?rewrite_once(X,Y,E,NewE) :- is_subterm(X,E), rewrite_once2(X,Y,E,NewE), E \= NewE.
1212
1213 rewrite_once2(X,Y,E,NewE) :- equal_terms(X,E),!, NewE=Y.
1214 rewrite_once2(_X,_Y,E,NewE) :- atomic(E),!, NewE=E.
1215 rewrite_once2(_X,_Y,'$'(E),NewE) :- atomic(E),!, NewE='$'(E).
1216 rewrite_once2(X,Y,C,NewC) :- C=..[Op|Args],
1217 ? select(Arg,Args,NewArg,NewArgs),
1218 rewrite_once2(X,Y,Arg,NewArg),
1219 NewC =.. [Op|NewArgs].
1220
1221 wd_strict_term(T) :- atomic(T), !.
1222 wd_strict_term('$'(E)) :- atomic(E), !.
1223 wd_strict_term(T) :-
1224 \+ with_ids(T,_), % quantified_union, quantified_intersection, event_b_comprehension_set, forall, exists -> not WD strict
1225 T=..[F|Args],
1226 \+ not_wd_strict(F),
1227 wd_strict_args(Args).
1228
1229 wd_strict_args([]).
1230 wd_strict_args([T|Args]) :- wd_strict_term(T), wd_strict_args(Args).
1231
1232 not_wd_strict(implication).
1233 not_wd_strict(conjunct).
1234 not_wd_strict(disjunct).
1235
1236 is_subterm(Term,Term).
1237 is_subterm(Subterm,Term) :-
1238 Term=..[_|Args],
1239 ? member(Arg,Args),
1240 ? is_subterm(Subterm,Arg).
1241
1242 rewrite_subterm(Rewrite,Term,Res) :-
1243 ? call(Rewrite,Term,Res),
1244 Term \= Res.
1245
1246 subset_inter_rw(T,U,Inter,Inter0) :-
1247 Inter = intersection(_,_),
1248 select_op(U,Inter,Inter0,intersection),
1249 member_of(intersection,T,Inter0).
1250 ?subset_inter_rw(T,U,Term,Res) :- transform_args(subset_inter_rw(T,U),Term,Res).
1251
1252 notin_inter_rw(E,T,Inter,empty_set) :-
1253 Inter = intersection(_,_),
1254 ? member_of(intersection,T,Inter),
1255 member_of(intersection,set_extension([E]),Inter).
1256 ?notin_inter_rw(E,T,Term,Res) :- transform_args(notin_inter_rw(E,T),Term,Res).
1257
1258 sim_fcomp(function(Comp,X),function(G,function(F,X))) :- split_composition(Comp,F,G).
1259 sim_fcomp(Term,Res) :- transform_args(sim_fcomp,Term,Res).
1260
1261 dis_binter(F,image(F,Inter),Res) :-
1262 Inter = intersection(_,_),
1263 image_intersection(F,Inter,Res).
1264 dis_binter(F,Term,Res) :- transform_args(dis_binter(F),Term,Res).
1265
1266 dis_setminus(F,image(F,set_subtraction(S,T)),set_subtraction(image(F,S),image(F,T))).
1267 dis_setminus(F,Term,Res) :- transform_args(dis_setminus(F),Term,Res).
1268
1269 transform_args(Transform,Term,Res) :-
1270 Term=..[F|Args],
1271 ? maplist(call(Transform),Args,NewArgs),
1272 Res=..[F|NewArgs].
1273
1274 replace_used_ids([],_,[]).
1275 replace_used_ids(['$'(X)|T],IdsTypes,['$'(Y)|R]) :-
1276 ? member(b(identifier(X),_,_),IdsTypes), !,
1277 new_aux_identifier(IdsTypes,Y),
1278 replace_used_ids(T,[b(identifier(Y),any,[])|IdsTypes],R).
1279 replace_used_ids(['$'(X)|T],IdsTypes,['$'(X)|R]) :-
1280 replace_used_ids(T,[b(identifier(X),any,[])|IdsTypes],R).
1281
1282
1283
1284 same_type('$'(S),'$'(T),List) :-
1285 ? member(b(identifier(S),Type,_),List),
1286 member(b(identifier(T),Type,_),List),
1287 Type \= any.
1288
1289
1290
1291 image_intersection(F,intersection(A,B),Inter) :- image_intersection(F,A,L), image_intersection(F,B,R), !, Inter = intersection(L,R).
1292 image_intersection(F,A,image(F,A)).
1293
1294
1295
1296
1297
1298
1299
1300 :- load_files(library(system), [when(compile_time), imports([environ/2])]).
1301 :- if(environ(prob_release,true)).
1302 % Don't include tests in release mode.
1303 :- else.
1304 :- use_module(test_sequent_prover).
1305 :- endif.