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
6 :- module(prover_utils,[member_hyps/2, equal_terms/2,
7 rewrite/4, with_ids/2, rewrite_pairwise/4, rewrite_pairwise_rev/4,
8 member_of/3, member_of_bin_op/3,
9 select_op/4, select_op/5,
10 op_to_list/3, list_to_op/3,
11 remove_duplicates_for_op/3, without_duplicates/2,
12 negate/2,
13 new_identifier/2, used_identifiers/2, used_identifiers_list/2, possible_identifier/1,
14 range_ids/3, new_identifiers/3, new_aux_identifier/2,
15 free_identifiers/2,
16 no_bound_ids_in_set/2,
17 is_fun/4, is_rel/4, is_surj/3, is_inj/3,
18 is_natural_set/1, is_natural1_set/1, is_integer_set/1, is_identity/1, is_prj1/1, is_prj2/1,
19 is_less_eq/3, is_less/3, is_equality/3, is_inequality/3,
20 comparison/1,
21 sym_unify/4, is_inter/3, is_minus/2, is_empty/2, is_negation/2,
22 of_integer_type/2, singleton_set/2,
23 split_composition/3,
24 type_expression/2, get_type_expression/2, is_deferred_or_enumerated_set/2,
25 check_expr_type/3, check_expr_type/4,
26 get_typed_ast/2, get_typed_identifiers/2, get_typed_identifiers/3, any_type/2,
27 get_meta_info/3, update_meta_infos/4, add_meta_info/4, remove_meta_info/4,
28 translate_norm_expr_term_no_limit/2, translate_norm_expr_term/2, create_descr/3, create_descr/4]).
29
30
31 :- use_module(probsrc(module_information),[module_info/2]).
32 :- module_info(group,sequent_prover).
33 :- module_info(description,'This module provides hyp managament and other utilities on sequents and hyps').
34
35 :- use_module(library(lists)).
36 :- use_module(probsrc(self_check),[assert_must_succeed/1]).
37 :- use_module(probsrc(tools),[ajoin/2, list_difference/3, list_intersection/3]).
38
39 member_hyps(Goal,Hyps) :-
40 ? (member(Goal,Hyps) -> true
41 ; equiv(Goal,G2), member(G2,Hyps) -> true), !.
42 member_hyps(Goal,Hyps) :-
43 ground(Goal),
44 ? member(G2,Hyps),
45 ? comparable(Goal,G2),
46 list_representation(Goal,L1),
47 list_representation(G2,L2),
48 equal_length(L1,L2),
49 ? stronger_list(L2,L1).
50
51
52 % replace by normalisation
53 equiv(equal(A,B),equal(B,A)).
54 equiv(not_equal(A,B),not_equal(B,A)).
55 equiv(greater(A,B),less(B,A)).
56 equiv(less(A,B),greater(B,A)).
57 equiv(greater_equal(A,B),less_equal(B,A)).
58 equiv(less_equal(A,B),greater_equal(B,A)).
59
60 % ----------------
61
62 :- assert_must_succeed(( rewrite('$'(x),add('$'(y),1),forall(['$'(y),'$'(z)],member('$'(y),integer_set),equal('$'(x),add('$'(y),1))),R),
63 R = forall([Y1,'$'(z)],member(Y1,integer_set),equal(add('$'(y),1),add(Y1,1))), Y1 \== '$'(y) )).
64 :- assert_must_succeed(( rewrite(function('$'(f),'$'(x)),3,forall(['$'(x)],member('$'(x),domain('$'(f))),equal(function('$'(f),'$'(x)),3)),R),
65 R == forall(['$'(x)],member('$'(x),domain('$'(f))),equal(function('$'(f),'$'(x)),3)) )).
66
67 % rewrite X to Y in E (capture-avoiding substitution of free occurrences of X)
68 rewrite(X,Y,E,NewE) :- equal_terms(X,E),!, NewE=Y.
69 rewrite(_X,_Y,E,NewE) :- atomic(E),!, NewE=E.
70 rewrite(_X,_Y,'$'(E),NewE) :- atomic(E),!, NewE='$'(E).
71 rewrite(X,_,E,NewE) :- with_ids(E,Ids), member(X,Ids),!, NewE=E.
72 rewrite(X,_,E,NewE) :- % X refers to an identifier bound in E, i.e. there are no free occurrences of X in E to replace
73 ground(X), with_ids(E,Ids), free_identifiers(X,FX), \+ list_intersection(Ids,FX,[]),!, NewE=E.
74 rewrite(X,Y,E,NewE) :- % rename the bound ids that would capture free ids of Y
75 ground(Y), with_ids(E,Ids), free_identifiers(Y,FY), list_intersection(Ids,FY,Clash), Clash \= [],!,
76 E=..[Op,Ids|Body], length(Clash,N),
77 new_identifiers([X,Y,E],N,NewClash), % fresh w.r.t. X, Y and E (incl. all bound ids of E)
78 rewrite_pairwise(Clash,NewClash,Ids,NewIds),
79 rewrite_pairwise(Clash,NewClash,Body,Body1),
80 l_rewrite(Body1,X,Y,NewBody),
81 NewE=..[Op,NewIds|NewBody].
82 rewrite(X,Y,C,NewC) :- C=..[Op|Args],
83 l_rewrite(Args,X,Y,NewArgs),
84 NewC =.. [Op|NewArgs].
85
86 l_rewrite([],_,_,[]).
87 l_rewrite([H|T],X,Y,[RH|RT]) :- rewrite(X,Y,H,RH), l_rewrite(T,X,Y,RT).
88
89 with_ids(exists(Ids,_),Ids).
90 with_ids(event_b_comprehension_set(Ids,_,_),Ids).
91 with_ids(forall(Ids,_,_),Ids).
92 with_ids(quantified_union(Ids,_,_),Ids).
93 with_ids(quantified_intersection(Ids,_,_),Ids).
94
95
96 rewrite_pairwise_rev(Ids1,Ids2,P,NewP) :-
97 reverse(Ids1,RevIds1),
98 reverse(Ids2,RevIds2),
99 rewrite_pairwise(RevIds1,RevIds2,P,NewP).
100
101 rewrite_pairwise([],[],E,E).
102 rewrite_pairwise([X|TX],[Y|TY],E,Res) :-
103 rewrite(X,Y,E,NewE),
104 rewrite_pairwise(TX,TY,NewE,Res).
105
106 % ----------------
107
108 equal_terms(X,Y) :- nonvar(X),simple_term_to_unify(X), !, Y=X. % avoid normalising Y
109 equal_terms(X,Y) :- nonvar(Y),simple_term_to_unify(Y), !, X=Y. % ditto for X
110 equal_terms(X,X) :- !.
111 equal_terms(X,Y) :- % both must be nonvar; otherwise unification would have succeeded
112 ? (cannot_change_functor(X,F) -> functor(Y,F,_) ; cannot_change_functor(Y,F) -> functor(X,F,_) ; true),
113 ground(X), ground(Y),
114 list_representation(X,LX),
115 list_representation(Y,LY),
116 equal_op(LX,LY),
117 equal_length(LX,LY),
118 equal_lists(LX,LY).
119
120 % true if list_representation cannot change functor and no other functor can generate it
121 % useful to quickly check top-level functor before normalising full terms
122 % TODO: extend; basically only arithmetic operators are simplified, and can be changed into numbers
123 % TODO: not sure the exclude zero/one rules should be part of equal_terms? can Rodin replay those?
124 cannot_change_functor(implication(_,_),implication).
125 cannot_change_functor(equal(_,_),equal).
126 cannot_change_functor(member(_,_),member).
127 cannot_change_functor(negation(_),negation).
128 cannot_change_functor(truth,truth).
129 cannot_change_functor(C,F) :- comm_binop(C,F,_,_).
130
131 % simple terms where we can simply use Prolog unification
132 simple_term_to_unify('$'(_)).
133 %simple_term(Nr) :- integer(Nr). % numbers require evaluation of other part!
134
135 equal_lists(L1,L2) :- sort_list(L1,SL1), sort_list(L2,SL2), SL1 = SL2.
136
137 ?stronger_list(L1,L2) :- sort_list(L1,SL1), sort_list(L2,SL2), list_implies(SL1,SL2).
138
139 list_implies([],[]).
140 list_implies([H1|T1], [H2|T2]) :-
141 ? implies(H1,H2),
142 list_implies(T1,T2).
143
144 % a hypothesis "a > b" is a sufficient condition to discharge the goal "a >= b" with HYP
145 implies(X,X).
146 implies(c(L,equal),c(L,greater_equal)).
147 implies(c(L,equal),c(RL,greater_equal)) :- reverse(L,RL).
148 implies(c(L,greater),c(L,greater_equal)).
149 implies(c(L,greater),c(L,not_equal)).
150
151 comparable(G1,G2) :- functor(G1,T1,2), functor(G2,T2,2), comparison(T1), comparison(T2).
152 comparable(G1,G2) :- G1=..[F,P], G2=..[F,Q], !, comparable(P,Q).
153 comparable(G1,G2) :- functor(G1,F,_), functor(G2,F,_). % TODO: why is this req. weaker than in clause above for arity 1?
154
155 comparison(equal).
156 comparison(greater).
157 comparison(greater_equal).
158 comparison(less).
159 comparison(less_equal).
160 comparison(not_equal).
161
162 list_representation(A,B,OList,L3) :-
163 list_representation(A,OList,L1),
164 list_representation(B,[],L2),
165 append(L1,L2,L3).
166
167 list_representation(C,L) :- list_representation(C,[],L).
168 list_representation('$'(X),L,Res) :- !, Res=[X|L].
169 list_representation(less_equal(A,B),OList,Res) :- !, list_representation(B,A,OList,NList), Res=[c(NList,greater_equal)].
170 list_representation(greater_equal(A,B),OList,Res) :- !, list_representation(A,B,OList,NList), Res=[c(NList,greater_equal)].
171 list_representation(less(A,B),OList,Res) :- !, list_representation(B,A,OList,NList), Res=[c(NList,greater)].
172 list_representation(greater(A,B),OList,Res) :- !, list_representation(A,B,OList,NList), Res=[c(NList,greater)].
173 list_representation(minus(A,B),OList,NList) :- !,
174 collect_subtrahend(minus(A,B),Subtraction),
175 Subtraction = [Minuend, Subtrahend],
176 exclude_nr(Subtrahend, 0, NSubtrahend), % exclude(zero, Subtrahend, NSubtrahend),
177 (NSubtrahend == [] -> NList = [Minuend] ;
178 append(OList,[c([Minuend, NSubtrahend],minus)],NList)).
179 list_representation(div(A,B),OList,NList) :- !,
180 collect_divisor(div(A,B),Division),
181 Division = [Divident, Divisor],
182 exclude_nr(Divisor, 1, NDivisor), %exclude(one, Divisor, NDivisor),
183 (NDivisor == [] -> NList = [Divident] ;
184 append(OList,[c([Divident, NDivisor],div)],NList)).
185 list_representation(C,OList,Res) :-
186 comm_binop(C,F,A,B), !,
187 collect_components(A,B,OList,NList,F),
188 Res=[c(NList,F)].
189 list_representation(add(A,B),OList,Res) :- !,
190 collect_components(A,B,OList,NList,add),
191 exclude_nr(NList,0,NList2), %exclude(zero, NList, NList2),
192 (NList2 == [] -> Res = [0] ;
193 NList2 \= [_]-> Res = [c(NList2,add)] ;
194 Res = NList2).
195 list_representation(multiplication(A,B),OList,Res) :- !,
196 collect_components(A,B,OList,NList,multiplication),
197 exclude_nr(NList,1,NList2), %exclude(one, NList, NList2),
198 (member(0, NList2) -> Res = [0] ;
199 NList2 == [] -> Res = [1] ;
200 NList2 \= [_]-> Res = [c(NList2,multiplication)] ;
201 Res = NList2).
202 list_representation(set_extension(A),OList,Res) :- !,
203 list_representation_of_list(A,OList,NList), sort(NList,S), Res=[c(S,set_extension)].
204 list_representation([A|T],OList,Res) :- !, list_representation_of_list([A|T],OList,NList), Res=[c(NList,list)].
205 list_representation(C,OList,Res) :- C=..[F,A], !, list_representation(A,OList,NList), Res=[c(NList,F)].
206 list_representation(C,OList,Res) :-
207 C=..[F,A,B],!,
208 list_representation(A,B,OList,NList), Res=[c(NList,F)].
209 list_representation(X,L,[X|L]) :- atomic(X).
210
211 comm_binop(conjunct(A,B),conjunct,A,B).
212 comm_binop(disjunct(A,B),disjunct,A,B).
213 comm_binop(intersection(A,B),intersection,A,B).
214 comm_binop(union(A,B),union,A,B).
215
216 list_representation_of_list([],OList,OList).
217 list_representation_of_list([A|T],OList,NList) :- list_representation(A,OList,L1), list_representation_of_list(T,[],L2),append(L1,L2,NList).
218
219 one(X) :- X is 1.
220 zero(X) :- X is 0.
221 exclude_nr([],_,[]).
222 exclude_nr([H|T],Nr,Res) :-
223 (H==Nr -> exclude_nr(T,Nr,Res) ; Res=[H|TR], exclude_nr(T,Nr,TR)).
224
225 collect_components(A,B,OList,NList,T) :-
226 collect_component(A,OList,L1,T),
227 collect_component(B,L1,NList,T).
228
229 collect_component('$'(X),OList,NList,_) :- !,
230 append(OList,[X],NList).
231 collect_component(X,OList,NList,_) :-
232 atomic(X),!,
233 append(OList,[X],NList).
234 collect_component(C,OList,NList,T) :-
235 C=..[T,A,B],!,
236 collect_components(A,B,OList,NList,T).
237 collect_component(X,OList,NList,_) :-
238 list_representation(X,[],L1),
239 append(OList,L1,NList).
240
241 collect_subtrahend(minus(A, B), [LA, Sub]) :-
242 A \= minus(_,_), !,
243 list_representation(A,[LA]),
244 (B = 0 -> Sub = [] ;
245 list_representation(B,[LB]), Sub = [LB]).
246 collect_subtrahend(minus(A, B), [Minuend, NSubtrahends]) :-
247 collect_subtrahend(A, [Minuend, Subtrahends]),
248 list_representation(B,LB),
249 append(Subtrahends, LB, NSubtrahends).
250
251 collect_divisor(div(A, B), [LA, Div]) :-
252 A \= div(_,_), !,
253 list_representation(A,[LA]),
254 (B = 1 -> Div = [] ;
255 list_representation(B,[LB]), Div = [LB]).
256 collect_divisor(div(A, B), [Divident, NDivisors]) :-
257 collect_divisor(A, [Divident, Divisors]),
258 list_representation(B,LB),
259 append(Divisors, LB, NDivisors).
260
261 :- use_module(library(samsort)).
262
263 sort_list(L,R) :- sort_components(L,S), samsort(S,R).
264 sort_components([],[]).
265 sort_components([c(L,T)|Rest], Res) :-
266 commutative_op(T), !,
267 sort_list(L,SL),
268 sort_components(Rest,R),
269 Res=[c(SL,T)|R].
270 sort_components([c(L,T)|Rest], Res) :- !, % non-commutative
271 sort_components(L,L2),
272 sort_components(Rest,R),
273 Res=[c(L2,T)|R].
274 % sort list of subtrahends or divisors
275 sort_components([E|Rest], [E|R]) :- atomic(E), !, sort_components(Rest,R).
276 sort_components([E|Rest], [F|R]) :- sort_list(E,F), sort_components(Rest,R).
277
278 commutative_op(disjunct). commutative_op(conjunct). commutative_op(equivalence).
279 commutative_op(union). commutative_op(intersection).
280 commutative_op(add). commutative_op(multiplication).
281 commutative_op(equal). commutative_op(not_equal).
282 commutative_op(negation). commutative_op(unary_minus). % these are unary ?!
283 commutative_op(set_extension). commutative_op(list).
284
285
286 equal_op(L1,L2) :- L1 = [c(_,T1)], L2 = [c(_,T2)], !, T1 = T2.
287 equal_op([E1],[E2]) :- E1 \= [c(_,_)], E2 \= [c(_,_)].
288
289 equal_length(L1,L2) :- L1 = [c(A1,_)], L2 = [c(A2,_)], !, same_length(A1, A2).
290 equal_length([_],[_]).
291
292
293 % -------------------------
294
295
296 % check if X is either equal to Term or is a sub-term, traversing all binary operators Op
297 member_of(Op,X,Term) :- functor(Term,Op,2), !,
298 ? (arg(1,Term,A),member_of(Op,X,A) ; arg(2,Term,B), member_of(Op,X,B)).
299 % TODO: this will not find P if P uses top-level functor Op itself! Review all uses and see if this is ok
300 member_of(_,P,P).
301
302 % difference to member_of: this requires top-level of Term to be binary operator
303 member_of_bin_op(Op,X,Term) :- functor(Term,Op,2), !,
304 ? (arg(1,Term,A),member_of(Op,X,A) ; arg(2,Term,B), member_of(Op,X,B)).
305
306 % select and remove (first occurrence of) X:
307 select_op(X,C,Rest,Op) :- C=..[Op,A,B],
308 ? (select_op(X,A,RA,Op), conjoin4(RA,B,Rest,Op)
309 ;
310 ? select_op(X,B,RB,Op), conjoin4(A,RB,Rest,Op)), !.
311 select_op(X,X,E,Op) :- neutral_element(Op,E).
312 select_op(X,Y,E,Op) :- neutral_element(Op,E), equal_terms(X,Y).
313
314 % replace (first occurrence of) X by Z:
315 select_op(X,C,Z,New,Op) :- C=..[Op,A,B],
316 (select_op(X,A,Z,RA,Op), conjoin4(RA,B,New,Op) ;
317 select_op(X,B,Z,RB,Op), conjoin4(A,RB,New,Op)), !.
318 select_op(X,X,Z,Z,_) :- !.
319 select_op(X,Y,Z,Z,_) :- equal_terms(X,Y).
320
321 conjoin4(E,X,R,Op) :- neutral_element(Op,E), !, R=X.
322 conjoin4(X,E,R,Op) :- neutral_element(Op,E), !, R=X.
323 conjoin4(X,Y,C,Op) :- C=..[Op,X,Y].
324
325 neutral_element(conjunct,true) :- !.
326 neutral_element(disjunct,false) :- !.
327 neutral_element(add,0) :- !.
328 neutral_element(multiplication,1) :- !.
329 neutral_element(_,placeholder).
330
331 % -------------------------
332
333 remove_duplicates_for_op(C,Res,Op) :-
334 functor(C,Op,_), % ensure that we have the desired functor as top-level
335 op_to_list(C,List,Op),
336 without_duplicates(List,ResL),
337 list_to_op(ResL,Res,Op).
338
339 without_duplicates(L,Res) :- without_duplicates(L,[],Res).
340
341 without_duplicates([],List,List).
342 without_duplicates([E|R],Filtered,Res) :-
343 ? member(F,Filtered),
344 equal_terms(E,F),!,
345 without_duplicates(R,Filtered,Res).
346 without_duplicates([E|R],Filtered, Res) :-
347 \+ member(E,Filtered),
348 append(Filtered,[E],New),
349 without_duplicates(R,New,Res).
350
351
352 op_to_list(Term,NList,Op) :-
353 op_to_list(Term,[],NList,Op).
354 op_to_list(Term,OList,NList,Op) :-
355 Term=..[Op,A,B],!,
356 op_to_list(B,OList,L1,Op),
357 op_to_list(A,L1,NList,Op).
358 op_to_list(Term,L,[Term|L],_). % Note: if L=[] then we did not have Op as top-level functor!
359
360 list_to_op([A],Res,_) :- !, Res=A.
361 list_to_op([A,B|L],Res,Op) :- C=..[Op,A,B], list_to_op(L,C,Res,Op).
362 list_to_op([],C,C,_).
363 list_to_op([X|T],C,Res,Op) :- NC=..[Op,C,X], list_to_op(T,NC,Res,Op).
364
365 % ------------------
366
367 negate(truth,Res) :- !, Res=falsity.
368 negate(falsity,Res) :- !, Res=truth.
369 negate(equal(X,Y),R) :- !, R=not_equal(X,Y).
370 negate(not_equal(X,Y),R) :- !, R=equal(X,Y).
371 negate(greater(X,Y),R) :- !, R=less_equal(X,Y).
372 negate(less(X,Y),R) :- !, R=greater_equal(X,Y).
373 negate(greater_equal(X,Y),R) :- !, R=less(X,Y).
374 negate(less_equal(X,Y),R) :- !, R=greater(X,Y).
375 negate(disjunct(X,Y),R) :- !, R=conjunct(NX,NY), negate(X,NX), negate(Y,NY).
376 negate(conjunct(X,Y),R) :- !, R=disjunct(NX,NY), negate(X,NX), negate(Y,NY).
377 negate(implication(X,Y),R) :- !, R=conjunct(X,NY), negate(Y,NY).
378 negate(negation(X),R) :- !, R=X.
379 negate(P,negation(P)).
380
381 % ------------------------
382
383
384 new_identifier(Expr,I) :-
385 used_identifiers(Expr,L),
386 ? possible_identifier(X),
387 I = '$'(X),
388 ? \+ member(I,L),!.
389
390 % -------------
391
392 used_identifiers_list(List,UsedIds) :-
393 used_identifiers(List,AllIds),
394 sort(AllIds,UsedIds).
395
396 used_identifiers(Term,AllIds) :- used_identifiers(Term,AllIds,[]).
397
398 used_identifiers(Term) --> {atomic(Term)}, !, [].
399 used_identifiers(Term) --> {Term = '$'(E), atomic(E)}, !, [Term].
400 used_identifiers(C) -->
401 {C =.. [_|Args]},
402 l_used_identifiers(Args).
403
404 l_used_identifiers([]) --> [].
405 l_used_identifiers([H|T]) --> used_identifiers(H), l_used_identifiers(T).
406
407 % -------------
408
409 :- assert_must_succeed(( free_identifiers(exists(['$'(x)],equal('$'(z),0)),FreeIds),
410 FreeIds == ['$'(z)] )).
411 :- assert_must_succeed(( free_identifiers(forall(['$'(x),'$'(y)],greater('$'(x),0),greater('$'(z),0)),FreeIds),
412 FreeIds == ['$'(z)] )).
413 :- assert_must_succeed(( free_identifiers(conjunct(forall(['$'(t)],truth,equal('$'(t),'$'(u))),greater('$'(z),0)),FreeIds),
414 FreeIds == ['$'(u),'$'(z)] )).
415 :- assert_must_succeed(( free_identifiers(implication(less('$'(x),0),greater('$'(y),0)),FreeIds),
416 FreeIds == ['$'(x),'$'(y)] )).
417 :- assert_must_succeed(( free_identifiers(forall(['$'(x)],member('$'(x),natural_set),greater_equal('$'(x),'$'(y))),FreeIds),
418 FreeIds == ['$'(y)] )). % x occurs twice, only y is free
419
420 free_identifiers(Term,AllIds) :- free_identifiers(Term,AllIds,[]).
421
422 free_identifiers(exists(Ids,P),Res1,EL) :- !,
423 free_identifiers(P,Free), % TODO: pass FreeImp as parameter and filter
424 list_difference(Free,Ids,Res),
425 append(Res,EL,Res1).
426 free_identifiers(Term,Res1,EL) :-
427 (Term = forall(Ids,P,E)
428 ; Term = event_b_comprehension_set(Ids,E,P)
429 ; Term = quantified_intersection(Ids,P,E)
430 ; Term = quantified_union(Ids,P,E)), !,
431 free_identifiers(implication(P,E),FreeImp), % TODO: pass FreeImp as parameter and filter
432 list_difference(FreeImp,Ids,Res),
433 append(Res,EL,Res1).
434 free_identifiers(Term) --> {atomic(Term)}, !, [].
435 free_identifiers(Term) --> {Term = '$'(E), atomic(E)}, !, [Term].
436 free_identifiers(C) -->
437 {C =.. [_|Args]},
438 l_free_identifiers(Args).
439
440 l_free_identifiers([]) --> [].
441 l_free_identifiers([H|T]) --> free_identifiers(H), l_free_identifiers(T).
442
443 % -------------------------
444
445 % the set S must not refer to the bound identifiers of #b.!x.(x:S => ...)
446 % corresponds to S.isWellFormed() in Rodin's FiniteMin/FiniteMax/FiniteHypBoundedGoal
447 no_bound_ids_in_set(BoundIds,S) :-
448 free_identifiers(S,Free),
449 ? \+ (member(Id,BoundIds), memberchk(Id,Free)).
450
451 % -------------------------
452
453 :- assert_must_succeed(( new_identifiers(implication(less('$'(x),0),greater('$'(y),0)),2,NewIds),
454 NewIds == ['$'(x1),'$'(x2)] )).
455 :- assert_must_succeed(( new_identifiers(implication(less('$'(x),0),greater('$'(x1),0)),3,NewIds),
456 NewIds == ['$'(y1),'$'(y2),'$'(y3)] )).
457 :- assert_must_succeed(( new_identifiers(implication(less('$'(x1),0),greater('$'(y1),0)),1,NewIds),
458 NewIds == ['$'(x)] )).
459 :- assert_must_succeed(( new_identifiers(implication(less('$'(x1),0),greater('$'(y1),0)),2,NewIds),
460 NewIds == ['$'(z1),'$'(z2)] )).
461
462 identifier(x).
463 identifier(y).
464 identifier(z).
465
466 ?possible_identifier(Z) :- identifier(Z).
467 ?possible_identifier(Z) :- identifier(X), identifier(Y), atom_concat(X,Y,Z).
468
469 new_identifiers(Expr,1,[Id]) :- !, new_identifier(Expr,Id).
470 new_identifiers(Expr,Nr,Ids) :-
471 used_identifiers(Expr,L),
472 ? possible_identifier(X),
473 ? range_ids(X,Nr,Ids),
474 ? \+ (member(I,Ids), member(I,L)), !.
475
476 ?range_ids(X,Nr,L) :- range(X,1,Nr,L).
477 range(X,I,I,['$'(XI)]) :- atom_int_concat(X,I,XI).
478 ?range(X,I,K,['$'(XI)|L]) :- I < K, I1 is I + 1, atom_int_concat(X,I,XI), range(X,I1,K,L).
479
480 atom_int_concat(X,I,XI) :-
481 atom_codes(X,CX),
482 number_codes(I,CI),
483 append(CX,CI,COut),
484 atom_codes(XI,COut).
485
486 new_aux_identifier(IdsTypes,X) :-
487 ? possible_identifier(X),
488 ? \+ member(b(identifier(X),_,_),IdsTypes),!.
489
490 % -------------------------
491
492 is_fun(partial_function(DOM,RAN),partial,DOM,RAN).
493 is_fun(partial_injection(DOM,RAN),partial,DOM,RAN).
494 is_fun(partial_surjection(DOM,RAN),partial,DOM,RAN).
495 is_fun(partial_bijection(DOM,RAN),partial,DOM,RAN).
496 is_fun(total_function(DOM,RAN),total,DOM,RAN).
497 is_fun(total_injection(DOM,RAN),total,DOM,RAN).
498 is_fun(total_surjection(DOM,RAN),total,DOM,RAN).
499 is_fun(total_bijection(DOM,RAN),total,DOM,RAN).
500
501 is_rel(F,Type,DOM,RAN) :- is_fun(F,Type,DOM,RAN).
502 is_rel(relations(DOM,RAN),partial,DOM,RAN).
503 is_rel(total_relation(DOM,RAN),total,DOM,RAN).
504 is_rel(surjection_relation(DOM,RAN),partial,DOM,RAN).
505 is_rel(total_surjection_relation(DOM,RAN),total,DOM,RAN).
506
507 is_surj(total_bijection(DOM,RAN),DOM,RAN).
508 is_surj(total_surjection(DOM,RAN),DOM,RAN).
509 is_surj(partial_surjection(DOM,RAN),DOM,RAN).
510 is_surj(surjection_relation(DOM,RAN),DOM,RAN).
511 is_surj(total_surjection_relation(DOM,RAN),DOM,RAN).
512
513 is_inj(partial_injection(DOM,RAN),DOM,RAN).
514 is_inj(partial_bijection(DOM,RAN),DOM,RAN).
515 is_inj(total_injection(DOM,RAN),DOM,RAN).
516 is_inj(total_bijection(DOM,RAN),DOM,RAN).
517
518
519 is_natural_set(natural_set).
520 is_natural_set('NATURAL').
521
522 is_natural1_set(natural1_set).
523 is_natural1_set('NATURAL1').
524
525 is_integer_set(integer_set).
526 is_integer_set('INTEGER').
527
528 is_identity(typeof(event_b_identity,_)).
529 is_identity(event_b_identity).
530
531 is_prj1(typeof(event_b_first_projection_v2,_)).
532 is_prj1(event_b_first_projection_v2).
533
534 is_prj2(typeof(event_b_second_projection_v2,_)).
535 is_prj2(event_b_second_projection_v2).
536
537 is_less_eq(less_equal(P,Q),P,Q).
538 is_less_eq(greater_equal(Q,P),P,Q).
539
540 is_less(less(P,Q),P,Q).
541 is_less(greater(Q,P),P,Q).
542
543 is_equality(equal(A,B),X,Y) :- ((X,Y) = (A,B) ; (X,Y) = (B,A)).
544
545 % check if we have an inequality between two terms
546 ?is_inequality(negation(Eq),X,Y) :- is_equality(Eq,X,Y).
547 is_inequality(not_equal(A,B),X,Y) :- ((X,Y) = (A,B) ; (X,Y) = (B,A)).
548
549 %is_equivalence(equivalence(A,B),A,B).
550 %is_equivalence(equivalence(B,A),A,B).
551
552
553 % symmetric / commutative unification; useful for commutative operators
554 sym_unify(A,B,A,B).
555 sym_unify(A,B,B,A).
556
557 is_inter(intersection(A,B),A,B). % TODO: extend to nested inter
558 is_inter(intersection(B,A),A,B).
559
560 is_minus(NE,E) :- number(NE), NE < 0, E is abs(NE) ; NE = unary_minus(E).
561
562 ?is_empty(Expr,Term) :- is_equality(Expr,Term,empty_set).
563 is_empty(Expr,Term) :- Expr = subset(Term,empty_set).
564
565 is_negation(Q,negation(P)) :- equal_terms(P,Q).
566
567 of_integer_type('$'(E),Info) :-
568 atomic(E),
569 of_integer_type(E,Info).
570 of_integer_type(E,Info) :-
571 get_meta_info(ids_types,Info,STypes),
572 member(b(identifier(E),integer,_),STypes).
573
574 singleton_set(set_extension([X]),X).
575
576
577 split_composition(Comp,LComp,RComp) :-
578 Comp = composition(_,_),
579 op_to_list(Comp,List,composition),
580 append(L,R,List),
581 list_to_op(L,LComp,composition),
582 list_to_op(R,RComp,composition).
583
584 % ----------------------------
585
586 % check if something is a pure type expression
587 type_expression(bool_set,_).
588 type_expression(Z,_) :- is_integer_set(Z).
589 type_expression(S,Info) :- is_deferred_or_enumerated_set(S,Info).
590 ?type_expression(pow_subset(Ty),Info) :- type_expression(Ty,Info).
591 ?type_expression(cartesian_product(Ty1,Ty2),Info) :- type_expression(Ty1,Info), type_expression(Ty2,Info).
592 % there are more type expressions not in Event-B:
593 type_expression(string_set,_).
594 type_expression(real_set,_).
595 type_expression(typeset,_).
596
597 get_type_expression(integer,'INTEGER').
598 get_type_expression(boolean,bool_set).
599 get_type_expression(set(S),pow_subset(T)) :- get_type_expression(S,T).
600 get_type_expression(couple(A,B),cartesian_product(S,T)) :- get_type_expression(A,S), get_type_expression(B,T).
601
602 % ------------------------
603
604 check_expr_type(_,'$'(T),IdsTypes,Type) :- !, member(b(identifier(T),Type,_),IdsTypes).
605 check_expr_type(S,_,IdsTypes,Type) :- check_expr_type(S,IdsTypes,Type).
606
607 ?check_expr_type('$'(S),IdsTypes,Type) :- !, member(b(identifier(S),Type,_),IdsTypes).
608 check_expr_type(S,IdsTypes,Type) :-
609 S \= '$'(_),
610 typecheckable_expression(S),
611 unique_aux_identifier(X),
612 get_typed_identifiers(equal('$'(X),S),[identifier(IdsTypes)],L),
613 member(b(identifier(X),Type,_),L).
614
615 typecheckable_expression(E) :-
616 \+ atomic(E),
617 \+ is_list(E),
618 is_expression(E).
619
620 is_expression(Expr) :-
621 get_typed_ast(Expr,TExpr),
622 TExpr \= b(_,pred,_).
623
624 unique_aux_identifier(XI) :-
625 atom_int_concat(x,14825791,XI).
626
627 % ------------------------
628
629 :- use_module(probsrc(translate),[transform_raw/2]).
630 :- use_module(wdsrc(well_def_hyps),[convert_norm_expr_to_raw/2]).
631
632 get_typed_ast(NormExpr,TExpr) :-
633 convert_norm_expr_to_raw(NormExpr,RawExpr),
634 transform_raw(RawExpr,TExpr).
635
636 get_typed_identifiers(NormExpr,List) :- get_typed_identifiers(NormExpr,[],List).
637
638 get_typed_identifiers(NormExpr,Scope,List) :-
639 convert_norm_expr_to_raw(NormExpr,ParsedRaw),
640 Quantifier = forall,
641 bmachine:b_type_open_predicate(Quantifier,ParsedRaw,Scope,TypedPred,[]), !,
642 (TypedPred = b(forall(List0,_,_),_,_) -> List=List0 ; List=[]).
643 % already known IDs from scope are ignored, no forall is generated if there are no new IDs
644 get_typed_identifiers(NormExpr,_,List) :-
645 used_identifiers(NormExpr,Ids),
646 maplist(any_type,Ids,List).
647
648 any_type('$'(X),b(identifier(X),any,[])).
649
650 % ------------------------
651
652 is_deferred_or_enumerated_set('$'(Set),Info) :-
653 get_meta_info(rawsets,Info,RawSets), % also covers enumerated sets in Event-B; TODO: adapt for classical B??
654 member(deferred_set(_,Set),RawSets).
655
656 get_meta_info(Key,Info,Content) :- El=..[Key,Content], memberchk(El,Info), !.
657 get_meta_info(_,_,[]).
658
659 update_meta_infos(Key,Info,Values,Info1) :-
660 get_meta_info(Key,Info,Content),
661 Content \= [], !,
662 Old=..[Key,Content],
663 New=..[Key,Values],
664 select(Old,Info,New,Info1).
665 update_meta_infos(Key,Info,Values,[El|Info]) :- El=..[Key,Values].
666
667 add_meta_info(Key,Info,Value,Info1) :- add_meta_infos(Key,Info,[Value],Info1).
668
669 add_meta_infos(Key,Info,Values,Info1) :-
670 get_meta_info(Key,Info,Content),
671 Content \= [], !,
672 Old=..[Key,Content],
673 append(Values,Content,NewContent),
674 New=..[Key,NewContent],
675 select(Old,Info,New,Info1).
676 add_meta_infos(Key,Info,Values,[El|Info]) :- El=..[Key,Values].
677
678 remove_meta_info(Key,Info,Value,Info1) :-
679 get_meta_info(Key,Info,Content),
680 Old=..[Key,Content],
681 select(Value,Content,Content0),
682 New=..[Key,Content0],
683 select(Old,Info,New,Info1).
684
685
686 % ----------------------------
687
688 :- use_module(probsrc(translate),[with_translation_mode/2]).
689 :- use_module(wdsrc(well_def_hyps),[convert_norm_expr_to_raw/2,translate_norm_expr/2,translate_norm_expr_with_limit/3]).
690 translate_norm_expr_term_no_limit(Term,Str) :-
691 remove_typeof(Term,CTerm),
692 catch(with_translation_mode(unicode,translate_norm_expr(CTerm,Str)),Exc,
693 (format(user_output,'Cannot translate ~w due to ~w~n',[Term,Exc]),Str='???')).
694 translate_norm_expr_term(Term,Str) :-
695 remove_typeof(Term,CTerm),
696 catch(with_translation_mode(unicode,translate_norm_expr_with_limit(CTerm,300,Str)),Exc,
697 (format(user_output,'Cannot translate ~w due to ~w~n',[Term,Exc]),Str='???')).
698
699 remove_typeof(Term,Res) :- atomic(Term), !, Res=Term.
700 remove_typeof(typeof(Term,_),Res) :- !, Res=Term.
701 remove_typeof(Term,Result) :-
702 Term=..[F|Args],
703 maplist(remove_typeof,Args,NewArgs),
704 Result=..[F|NewArgs].
705
706 create_descr(Rule,T,Descr) :-
707 Rule=..[Name,_],
708 create_descr(Name,T,Descr).
709 create_descr(Rule,T,Descr) :- atomic(Rule), translate_norm_expr_term(T,TS),
710 ajoin([Rule,' (',TS,')'],Descr).
711 create_descr(Rule,S,T,Descr) :- translate_norm_expr_term(T,TS),
712 ajoin([Rule,' (',S,',',TS,')'],Descr).