1 % (c) 2020-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html
4
5 :- module(well_def_hyps, [empty_hyps/1,
6 portray_hyps/1,
7 get_hyp_vars/2,
8 get_hyp_var_type/3,
9 push_hyp/4, push_hyps/4,
10 push_hyps_wo_renaming/4,
11 push_normalized_hyp/3,
12 push_and_rename_normalized_hyp/3,
13 hyps_inconsistent/1,
14 add_new_hyp_variables/3,
15 add_new_hyp_any_vars/3,
16 copy_hyp_variables/3,
17 is_hyp_var/2,
18 get_clash_renaming_subst/2,
19 get_renamed_expression/3,
20 get_normalized_and_renamed_predicate/4,
21 translate_norm_expr/2, translate_norm_expr_with_limit/3,
22 negate_hyp/2,
23 negate_op/2,
24 is_finite_type_for_wd/2,
25
26 normalize_expression/2, normalize_predicate/2,
27 convert_norm_expr_to_raw/2,
28 extract_assert_predicate/2
29 ]).
30
31 :- use_module(probsrc(module_information),[module_info/2]).
32 :- module_info(group,well_def_prover).
33 :- module_info(description,'This module provides hypotheses stack management.').
34
35
36
37 :- use_module(wdsrc(well_def_tools), [not_occurs/2]).
38 :- use_module(probsrc(error_manager)).
39 :- use_module(probsrc(debug)).
40 :- use_module(library(avl)).
41 :- use_module(library(ordsets)).
42 :- use_module(probsrc(avl_tools),[avl_fetch_bin/4]).
43
44 % ------------------------------
45
46 % Hypotheses stack management:
47
48
49 % create an empty hyp stack
50 empty_hyps(hyp_rec(E,HI2)) :- empty_avl(E),
51 avl_store(hyp_typed_vars,E,[],HI1), % typed variables of the hypotheses (implicitly universally quantified)
52 avl_store(hyp_clash_vars,HI1,clash_rec(0,E),HI2). % variables which are currently in clash
53
54 hyps_inconsistent(hyp_rec(AVL,_)) :- avl_fetch(falsity,AVL).
55
56 :- use_module(probsrc(bsyntaxtree), [conjunct_predicates/2]).
57 % display the hypotheses stack:
58 portray_hyps(H) :- var(H), !, format('** ILLEGAL VAR Hypotheses: ~w~n',[H]).
59 portray_hyps(hyp_rec(AVL,HInfos)) :- fetch_hyp_vars(HInfos,Vars),
60 get_clashed_vars(HInfos,CVars),
61 (debug_mode(on) -> portray_hyp_vars(hyp_rec(AVL,HInfos)),nl ; true),
62 %b_global_sets:portray_global_sets,
63 !,
64 format('Hypotheses over ~w (clashes: ~w):~n',[Vars,CVars]),
65 %avl_domain(AVL,D), lists:maplist(well_def_hyps:println_nhyp,D),
66 avl_range(AVL,Hyp),
67 conjunct_predicates(Hyp,HypC),
68 translate:nested_print_bexpr(HypC),nl,nl.
69 portray_hyps(H) :- !, format('** ILLEGAL Hypotheses: ~w~n',[H]).
70
71 print_tvar(b(identifier(ID),Type,_)) :- format(' ~w : ~w~n',[ID,Type]).
72 :- use_module(library(lists),[maplist/2]).
73 portray_hyp_vars(hyp_rec(_,HInfos)) :- fetch_hyp_typed_vars(HInfos,TVars),!,
74 length(TVars,Len),
75 format('Typed vars in hyps (~w):~n',[Len]),
76 maplist(print_tvar,TVars).
77 portray_hyp_vars(H) :- !, format('** ILLEGAL Hypotheses: ~w~n',[H]).
78
79
80 %println_nhyp(NH) :- format(' --> ~w~n',[NH]).
81
82
83 % ---------------------
84
85 % for debugging:
86 :- public hyp_portray_hook/1.
87 hyp_portray_hook(X) :- nonvar(X), X= hyp_rec(AVL,HInfos),
88 avl_size(AVL,Size),
89 avl_size(HInfos,ISize),
90 format('hyp_rec(#~w,#~w)',[Size,ISize]).
91
92 :- public install_hyp_portray_hook/0.
93 install_hyp_portray_hook :- % mainly for the Prolog debugger
94 assertz(( user:portray(X) :- well_def_hyps:hyp_portray_hook(X) )).
95
96 %:- install_hyp_portray_hook.
97
98
99 % ------------------------
100
101 % get the variable ids currently in scope
102 get_hyp_vars(hyp_rec(_,HInfos),Res) :- get_hyp_vars(HInfos,Vars),!,Res=Vars.
103 get_hyp_vars(H,R) :- add_internal_error('Illegal hyps: ',get_hyp_vars(H,R)), R=[].
104
105 :- use_module(probsrc(bsyntaxtree), [def_get_texpr_ids/2]).
106 fetch_hyp_vars(HInfos,Vars) :- avl_fetch(hyp_typed_vars,HInfos,TVars),
107 def_get_texpr_ids(TVars,Vars).
108 fetch_hyp_typed_vars(HInfos,Vars) :-
109 avl_fetch(hyp_typed_vars,HInfos,Vars).
110 get_clashed_vars(HInfos,Vars) :- avl_fetch(hyp_clash_vars,HInfos,clash_rec(_,AVL)),
111 avl_domain(AVL,Vars).
112 get_clash_renaming(HInfos,Renamings) :- avl_fetch(hyp_clash_vars,HInfos,clash_rec(_,AVL)),
113 findall(rename(ID,FreshID), avl_member(ID,AVL,FreshID), Renamings).
114
115 % check if a variable id is currently in the scope of the hypotheses
116 % if not, it is a global identifier (e.g., enumerated or deferred set)
117 is_hyp_var(Var,hyp_rec(_,HInfos)) :- atomic(Var), nonvar(HInfos),!,
118 fetch_hyp_vars(HInfos,Vars),
119 ord_member(Var,Vars).
120 is_hyp_var(V,H) :- add_internal_error('Illegal call: ',is_hyp_var(V,H)),fail.
121
122 :- use_module(probsrc(tools_lists),[ord_member_nonvar_chk/2]).
123 get_hyp_var_type(Var,hyp_rec(_,HInfos),Type) :- atomic(Var),!,
124 fetch_hyp_typed_vars(HInfos,TVars),
125 TVar = b(identifier(Var),Type,_),
126 ord_member_nonvar_chk(TVar,TVars).
127 get_hyp_var_type(V,H,T) :- add_internal_error('Illegal call: ',is_hyp_var_type(V,H,T)),fail.
128
129 :- use_module(probsrc(bsyntaxtree), [conjunction_to_list/2]).
130 % push a new Hypothesis H on the hyp stack
131 push_hyp(Hyps,H,Options,NewHyps) :-
132 check_valid_hyp_rec(Hyps,push_hyp),
133 conjunction_to_list(H,Hs),
134 push_hyps(Hyps,Hs,Options,NewHyps).
135
136 check_valid_hyp_rec(Hyps,PP) :- var(Hyps),!,
137 add_internal_error('Illegal variable hyp_rec: ',check_hyp_rec(Hyps,PP)),fail.
138 check_valid_hyp_rec(Hyps,PP) :- Hyps \= hyp_rec(_,_),!,
139 add_internal_error('Illegal hyp_rec: ',check_valid_hyp_rec(Hyps,PP)),fail.
140 check_valid_hyp_rec(_,_).
141
142 % push a list of hypotheses
143 push_hyps(hyp_rec(NHyps,HInfos),Hs,Options,hyp_rec(NewNHyps,HInfos)) :- !,
144 get_clash_renaming(HInfos,ClashRenaming),
145 push_hyp_aux(Hs,ClashRenaming,Options,NHyps,NewNHyps).
146 push_hyps(A,B,C,D) :- add_internal_error('Illegal call: ', push_hyps(A,B,C,D)),fail.
147
148 % useful if renaming done outside, e.g., for treating x:=x-1 in WD analyser
149 push_hyps_wo_renaming(hyp_rec(NHyps,HInfos),Hs,Options,hyp_rec(NewNHyps,HInfos)) :- !, ClashRenaming=[],
150 push_hyp_aux(Hs,ClashRenaming,Options,NHyps,NewNHyps).
151 push_hyps_wo_renaming(A,B,C,D) :- add_internal_error('Illegal call: ', push_hyps(A,B,C,D)),fail.
152
153 push_hyp_aux(Hyps,_,_,_,_) :- var(Hyps),!, add_internal_error('Unbound hyps: ',push_hyps(Hyps)),fail.
154 push_hyp_aux([],_,_,NH,NH).
155 push_hyp_aux([H|T],ClashRenaming,Options,NHyps,NewNHyps) :-
156 ((var(NHyps) ; NHyps=hyp_rec(_,_)) -> add_internal_error('Illegal AVL: ',NHyps),fail ; true),
157 push_individual_hyp(H,ClashRenaming,Options,NHyps,NHyps3),
158 push_hyp_aux(T,ClashRenaming,Options,NHyps3,NewNHyps).
159
160 % sometimes we still have conjuncts in the list of hypotheses (e.g., coming from Rodin)
161 push_individual_hyp(b(conjunct(H1,H2),_,_),ClashRenaming,Options,NHyps,NHyps3) :- !,
162 push_individual_hyp(H1,ClashRenaming,Options,NHyps,NHyps2),
163 push_individual_hyp(H2,ClashRenaming,Options,NHyps2,NHyps3).
164 push_individual_hyp(H,ClashRenaming,Options,NHyps,NHyps3) :-
165 normalize_and_rename_predicate(ClashRenaming,H,RenH,NH),
166 % print('PUSH: '),nl, debug:print_quoted_with_max_depth(NH,6), print(' '), error_manager:print_message_span(H),nl,
167 push_normalized_hyp_aux(NH,RenH,Options,NHyps,NHyps3).
168
169 % utility: used to push already normalized and renamed hyp from within prover for normalized sub-goals
170 % should normally be renamed with get_clash_renaming_subst result
171 push_normalized_hyp(NH,hyp_rec(NHyps,I),hyp_rec(NHyps3,I)) :-
172 norm_aux(NH,NormPred),
173 unknown_source_term(NormPred,CorrespondingTExpr),
174 push_normalized_hyp_aux(NormPred,CorrespondingTExpr,[],NHyps,NHyps3).
175
176
177 push_normalized_hyp_aux(NH0,RenH,Options,NHyps,NHyps2) :-
178 simplify_hyp(NH0,NHyps,NH),
179 ((useful_hyp(NH) ; safe_ord_member(create_full_po,Options)
180 ; potentially_useful_for_hyp_rule(NH), safe_ord_member(push_more_hyps,Options)
181 ; useful_implication(NH,Options),
182 true %safe_ord_member(push_more_hyps,Options) % seems useful for Event-B benchmark models, enable by default?
183 )
184 -> avl_store_with_commutes_if_new(NH,NHyps,RenH,NHyps2,Options)
185 ; push_commutative_hyps(NH,RenH,Options,NHyps,NHyps2)
186 % hypothesis not directly used by prover, but there could be alternatives e.g., for disjunct
187 %,functor(NH,FF,NN), print(not_pushing(FF,NN)),nl
188 ).
189
190 :- use_module(wdsrc(well_def_tools), [rename_norm_term/3]).
191 % rename and push an already normalized hyp; also splitting conjuncts
192 push_and_rename_normalized_hyp(Pred,Hyps,NewHyps) :-
193 get_clash_renaming_subst(Hyps,Renaming),
194 rename_norm_term(Pred,Renaming,RenamedPred), %write(push(RenamedPred)),nl,
195 push_conj(RenamedPred,Hyps,NewHyps).
196 push_conj(conjunct(A,B),Hyps,NewHyps) :-
197 push_conj(A,Hyps,Hyps1), push_conj(B,Hyps1,NewHyps).
198 push_conj(A,Hyps,NewHyps) :- push_normalized_hyp(A,Hyps,NewHyps).
199
200
201 % push equivalent or implied hypotheses on the stack:
202 push_commutative_hyps(NH,RenH,Options,NHyps1,NHyps2) :-
203 ? commute_bin_op(NH,_,Options), % somehow faster than using findall directly
204 !,
205 findall(NH3,commute_bin_op(NH,NH3,Options),NH3s),
206 l_avl_store_nhyps(NH3s,NHyps1,RenH,NHyps2,Options).
207 push_commutative_hyps(_,_,_,NHyps,NHyps).
208
209 safe_ord_member(El,List) :- var(List),!, add_internal_error('Illegal call: ',safe_ord_member(El,List)),fail.
210 safe_ord_member(El,List) :- ord_member(El,List).
211
212 l_avl_store_nhyps([],NHyps,_,NHyps,_Options).
213 l_avl_store_nhyps([NH1|TNH],NHyps1,RenH,NHyps3,Options) :-
214 simplify_hyp(NH1,NHyps1,NH1s),
215 avl_store_if_new(NH1s,NHyps1,RenH,NHyps2,Options),
216 l_avl_store_nhyps(TNH,NHyps2,RenH,NHyps3,Options).
217
218 % store a hypothesis if new (without storing commutative versions of it)
219 avl_store_if_new(NH,H,_,H2,_) :- avl_fetch(NH,H),!, H2=H.
220 avl_store_if_new(NH,H1,RH,H3,Options) :- %write(prop_new(NH)),nl, avl_domain(H1,H1D), write(H1D),nl,nl,
221 propagate_resolution_with_hyp(NH,H1,H2,Options),
222 avl_store(NH,H2,RH,H3).
223
224 % propagate new hyp by applying (simple) resolution: Hyp & not(Hyp) -> add false as hypothesis
225 % also propagates implications Hyp => Q -> add Q as hypothesis
226 propagate_resolution_with_hyp(NormHyp,Hyps,H2,_) :- negate_norm_op(NormHyp,NegNormHyp),
227 avl_fetch(NegNormHyp,Hyps),!,
228 debug_println(9,contradiction_found_in_hypotheses(NormHyp)),
229 avl_store(falsity,Hyps,b(falsity,pred,[neg_hyp]),H2). % false_hyp rule can later trigger
230 propagate_resolution_with_hyp(NH,Hyps,H2,Options) :-
231 %write(fetch_impl(NH)),nl, avl_domain(Hyps,D), write(hyps(D)),nl,
232 findall(NRHS,avl_fetch_bin(NH,implication,Hyps,NRHS),TriggeredImplications),
233 propagate_implications(TriggeredImplications,NH,Hyps,H2,Options).
234
235
236
237 negate_norm_op(NormHyp,NegNormHyp) :- negate_op(NormHyp,NegNH),
238 norm_aux(NegNH,NegNormHyp).
239
240 propagate_implications([],_,Hyps,Hyps,_).
241 propagate_implications([NRHS|TR],NLHS,NHyps1,NHyps4,Options) :-
242 (avl_delete(implication(NLHS,NRHS),NHyps1,TE,NHyps2)
243 -> % write('propagate : '),translate:print_bexpr(TE),nl,
244 (TE=b(implication(_,RHS),_,_) -> true
245 ; TE=b(disjunct(_,RHS),_,_) -> true
246 ; unknown_source_term(NRHS,RHS),
247 true %add_warning(wd_prover,'Unexpected un-normalised hyp: ',TE)
248 ),
249 simplify_hyp(NRHS,NHyps2,NRHS2),
250 avl_store_with_commutes_if_new(NRHS2,NHyps2,RHS,NHyps3,Options)
251 ; % implication has already been triggered by processing a previous NRHS in the list
252 NHyps3=NHyps1
253 ),
254 propagate_implications(TR,NLHS,NHyps3,NHyps4,Options).
255
256 unknown_source_term(NormPred,b(unknown_truth_value(NormPred),pred,[trigger_implication])).
257
258 avl_store_with_commutes_if_new(NH,H,_,H2,_) :- avl_fetch(NH,H),!, H2=H.
259 avl_store_with_commutes_if_new(conjunct(NH1,NH2),H0,TE,H2,Options) :- !,
260 (TE=b(conjunct(TE1,TE2),_,_) -> true ; unknown_source_term(NH1,TE1), unknown_source_term(NH2,TE2)),
261 simplify_hyp(NH1,H0,SNH1),
262 avl_store_with_commutes_if_new(SNH1,H0,TE1,H1,Options),
263 simplify_hyp(NH2,H1,SNH2),
264 avl_store_with_commutes_if_new(SNH2,H1,TE2,H2,Options).
265 avl_store_with_commutes_if_new(NH,H0,RH,H3,Options) :- %write(prop_new(NH)),nl, avl_domain(H,H1D), write(H1D),nl,nl,
266 avl_store(NH,H0,RH,H1),
267 propagate_resolution_with_hyp(NH,H1,H2,Options),
268 push_commutative_hyps(NH,RH,Options,H2,H3).
269
270 normalize_expression(Expr,NormExpr) :-
271 (Expr \= b(_,pred,_) -> true ; add_error(well_def_hyps,'Expected expression, but got predicate')),
272 b_interpreter_check:norm_expr_check(Expr,NormExpr).
273
274 :- use_module(probsrc(bsyntaxtree), [rename_bt/3]).
275 normalize_and_rename_predicate(_,H,_,_) :- var(H),!,
276 add_internal_error('Unbound predicate: ',normalize_and_rename_predicate(H)),fail.
277 normalize_and_rename_predicate([],H,RenH,NH) :- !, RenH=H,
278 normalize_predicate(H,NH).
279 normalize_and_rename_predicate(ClashRenaming,H,RenH,NH) :- !,
280 %format('Rename Hyp: ~w ',[ClashRenaming]),translate:print_bexpr(H),nl,
281 rename_bt(H,ClashRenaming,RenH),
282 %print(' > renamed Hyp: '),translate:print_bexpr(RenH),nl,
283 normalize_predicate(RenH,NH).
284
285 % :- use_module(probsrc(bsyntaxtree),[expand_all_lets/2]).
286 % TO DO: expand lets; but can be very expensive; e.g., B/Tickets/Schneider3_Trees/NewSolver_v2.mch -wd-check
287 normalize_predicate(Pred,NormPred) :-
288 b_interpreter_check:norm_pred_check(Pred,NP),
289 norm_aux(NP,NormPred).
290
291
292 % put identifiers first, so that we can more efficiently do lookups;
293 % hence we try and replace less/greater by less_equal/greater_equal when possible
294 norm_aux(equal(A,B),equal(NA,NB)) :- !, norm_equal(A,B,NA,NB).
295 norm_aux(greater(Val,Nr),Res) :- integer(Nr),!, N1 is Nr+1, norm_aux(greater_equal(Val,N1),Res).
296 norm_aux(greater(Nr,Val),Res) :- integer(Nr),!, N1 is Nr-1, norm_aux(greater_equal(N1,Val),Res).
297 norm_aux(greater(A,B),less(B,A)) :- !. % we only look up less (when both args are known)
298 norm_aux(less(Val,Nr),Res) :- integer(Nr),!, N1 is Nr-1, norm_aux(less_equal(Val,N1),Res).
299 norm_aux(less(Nr,Val),Res) :- integer(Nr),!, N1 is Nr+1, norm_aux(less_equal(N1,Val),Res).
300 norm_aux(less_equal(A,Nr),less_equal(A2,Nr2)) :-
301 integer(Nr), peel_constant_offset(A,A2,Offset),!, Nr2 is Nr-Offset.
302 norm_aux(less_equal(Nr,A),less_equal(Nr2,A2)) :-
303 integer(Nr), peel_constant_offset(A,A2,Offset),!, Nr2 is Nr-Offset.
304 norm_aux(greater_equal(A,Nr),greater_equal(A2,Nr2)) :-
305 integer(Nr), peel_constant_offset(A,A2,Offset),!, Nr2 is Nr-Offset.
306 norm_aux(greater_equal(Nr,A),greater_equal(Nr2,A2)) :-
307 integer(Nr), peel_constant_offset(A,A2,Offset),!, Nr2 is Nr-Offset.
308 norm_aux(not_equal(Val,EMPTY),not_equal(Val,empty_set)) :- is_empty_set_alternative(EMPTY),!.
309 norm_aux(not_equal(EMPTY,Val),not_equal(Val,empty_set)) :- is_empty_set_alternative(EMPTY),!.
310 norm_aux(negation(Pred),NormPred) :- negate_op(Pred,NP),!, norm_aux(NP,NormPred).
311 norm_aux(implication(Pred1,Pred2),NormPred) :- !,
312 norm_implication(Pred1,Pred2,NormPred).
313 norm_aux(disjunct(Pred1,Pred2),disjunct(NormPred1,NormPred2)) :- !,
314 norm_aux(Pred1,NormPred1),norm_aux(Pred2,NormPred2).
315 norm_aux(equivalence(Pred1,Pred2),equivalence(NormPred1,NormPred2)) :- !,
316 norm_aux(Pred1,NormPred1),norm_aux(Pred2,NormPred2).
317 %norm_aux(Term,NormPred) :- print(Term),nl,functor(Term,union,2),flatten(Term,union,List,[]), print(union(List)),nl,
318 % sort(List,SL),print(sorted(SL)),nl,fail.
319 norm_aux(external_pred_call('ASSERT_TRUE',[BOOL_EXPR,_ErrString]),NormPred) :-
320 extract_assert_predicate(BOOL_EXPR,Pred), !,
321 normalize_predicate(Pred,NormPred).
322 norm_aux(V,V).
323 % TO DO: subset_strict -> subset and not_equal
324 % TO DO: normalize value(X) terms -> value(int(Nr)) -> Nr, ...
325 % TO DO: maybe process a few rules here x<: dom(f) or x = dom(f) - other
326 % TO DO: what about negation of ASSERT_TRUE, treat ASSERT_EXPR
327
328 % translate boolean expression argument of ASSERT_TRUE, ... into a predicate that must be true
329 extract_assert_predicate(b(convert_bool(Pred),_,_),Res) :- !, Res=Pred.
330 extract_assert_predicate(BOOL_EXPR,b(equal(BOOL_EXPR,b(boolean_true,boolean,[])),pred,[])).
331
332 norm_equal(A,B,RA,RB) :- peel_eq(A,B,SA,SB),
333 (SB='$'(_), SA \= '$'(_) -> RA=SB,RB=SA ; RA=SA, RB=SB).
334
335 % peel constant +/- offsets used for less_equal/greater_equal normalisation:
336 peel_constant_offset(add(X,Y),Res,Offset) :- ((X,Y)=(A,Nr) ; (X,Y)=(Nr,A)), integer(Nr),!,
337 peel_constant_offset(A,Res,OffsetA), Offset is Nr+OffsetA.
338 peel_constant_offset(minus(X,Y),Res,Offset) :- ((X,Y)=(A,Nr) ; (X,Y)=(Nr,A)), integer(Nr),!,
339 peel_constant_offset(A,Res,OffsetA), Offset is OffsetA-Nr.
340 peel_constant_offset(A,A,0).
341
342 peel_eq(reverse(A),reverse(B),SA,SB) :- !, peel_eq(A,B,SA,SB).
343 % TODO: add other injective/reversible operators; also cf. simplify_hyp
344 peel_eq(A,B,A,B).
345
346 norm_implication(conjunct(A,B),Pred2,Implication) :- !,
347 % A & B => C ---> A => (B => C) (so that we can use avl_fetch on LHS of implication)
348 norm_implication(B,Pred2,Implication2),
349 norm_implication(A,Implication2,Implication).
350 norm_implication(Pred1,Pred2,implication(NormPred1,NormPred2)) :-
351 norm_aux(Pred1,NormPred1),norm_aux(Pred2,NormPred2).
352
353
354 % TO DO: flatten and sort union and possibly other operators:
355 %flatten(Term,BOP) --> {functor(Term,BOP,2), arg(1,Term,B1), arg(2,Term,B2)},!,
356 % flatten(B1,BOP), flatten(B2,BOP).
357 %flatten(Term,_) --> [Term].
358
359 is_empty_set_alternative(empty_sequence).
360 is_empty_set_alternative(value(V)) :- V==[]. % should now be handled in norm_expr / norm_value
361
362 negate_op(truth,falsity).
363 negate_op(falsity,truth).
364 negate_op(equal(A,B),not_equal(A,B)).
365 negate_op(not_equal(A,B),equal(A,B)).
366 negate_op(less(A,B),less_equal(B,A)).
367 negate_op(greater(A,B),less_equal(A,B)).
368 negate_op(less_equal(A,B),less(B,A)).
369 negate_op(greater_equal(A,B),less(A,B)).
370 negate_op(less_real(A,B),less_equal_real(B,A)).
371 negate_op(less_equal_real(A,B),less_real(B,A)).
372 negate_op(negation(P),P).
373 negate_op(not_member(A,B),member(A,B)).
374 negate_op(member(A,B),not_member(A,B)). % should we do this?
375 negate_op(not_subset(A,B),subset(A,B)).
376 negate_op(subset(A,B),not_subset(A,B)).
377 negate_op(not_subset_strict(A,B),subset_strict(A,B)).
378 negate_op(subset_strict(A,B),not_subset_strict(A,B)).
379 % should we negate_op(conjunct ...), we also treat negation in prove_po/prove_negated_po
380
381 % for commutative binary operators: also store commutative version to enable lookup on either argument
382 commute_bin_op(OpTerm,CommutativeOrDerivedVersion,_Options) :-
383 ? commute_bin_op(OpTerm,CommutativeOrDerivedVersion).
384 commute_bin_op(OpTerm,CommutativeOrDerivedVersion,Options) :-
385 safe_ord_member(push_more_hyps,Options),
386 ? commute_bin_op_aggressive(OpTerm,CommutativeOrDerivedVersion,Options).
387
388 ?commute_bin_op(equal(A,B),Pred) :- compute_bin_op_equal(A,B,Pred).
389 % not_equal: no need to reverse: we always know both values when doing a lookup
390 commute_bin_op(greater_equal(A,B),less_equal(B,A)) :- can_be_used_for_lookups(B).
391 commute_bin_op(greater(A,B),Pred) :- compute_bin_op_less(B,A,Pred).
392 ?commute_bin_op(less_equal(A,B),Pred) :- compute_bin_op_less_equal(A,B,Pred).
393 ?commute_bin_op(less(A,B),Pred) :- compute_bin_op_less(A,B,Pred).
394 commute_bin_op(less_real(A,B),not_equal(A,B)). % TO DO: extend
395 ?commute_bin_op(subset_strict(A,B),Pred) :- gen_subset(A,B,Pred).
396 commute_bin_op(subset_strict(A,B),not_equal(A,B)).
397 commute_bin_op(subset(A,B),superset(B,A)) :- % new operator, for efficient lookups !
398 can_be_used_for_lookups(B).
399 commute_bin_op(subset(A,cartesian_product(Dom,Ran)),member(A,relations(Dom,Ran))) :-
400 can_be_used_for_lookups(A).
401 commute_bin_op(subset_strict(A,cartesian_product(Dom,Ran)),member(A,relations(Dom,Ran))) :-
402 can_be_used_for_lookups(A).
403 commute_bin_op(not_subset(A,B),not_equal(A,B)). % also implies not_subset_strict
404 commute_bin_op(member(_,Set),not_equal(Set,empty_set)). % x:Set ==> Set /= {}
405 commute_bin_op(member(couple(A,B),C),NewHyp) :-
406 ( NewHyp = member(A,domain(C)) % A|->B : C ==> A : dom(C)
407 ; NewHyp = member(B,range(C)) ). % A|->B : C ==> B : ran(C)
408 commute_bin_op(member(X,interval(Low,Up)),NewHyp) :-
409 (NewHyp = less_equal(Low,Up) % x : Low..Up => Low <= Up
410 ; NewHyp = less_equal(Low,X) % Low <= X if X: Low..UP
411 ; can_be_used_for_lookups(X), NewHyp = greater_equal(X,Low)
412 ; NewHyp = less_equal(X,Up) % X <= UP if X: Low..UP
413 ; can_be_used_for_lookups(Up), NewHyp = greater_equal(Up,X)
414 ).
415 commute_bin_op(member(X,Rel),NewHyp) :- is_total_relation(Rel,Domain),
416 % we cannot efficiently lookup this info from Domain
417 can_be_used_for_lookups(Domain),
418 NewHyp = equal(Domain,domain(X)).
419 commute_bin_op(member(X,Rel),NewHyp) :- is_surjective_relation(Rel,Range),
420 % we cannot efficiently lookup this info from Range
421 can_be_used_for_lookups(Range),
422 NewHyp = equal(Range,range(X)).
423 commute_bin_op(member(card(X),_),NewHyp) :- can_be_used_for_lookups(X),
424 NewHyp=finite(X).
425 commute_bin_op(member(X,union(A,B)),NewHyp) :- can_be_used_for_lookups(X),
426 (NewHyp=implication(not_member(X,A),member(X,B)) % x:A\/B & x/:A => x:B
427 ; NewHyp=implication(not_member(X,B),member(X,A))). % ditto for B
428 commute_bin_op(member(X,set_subtraction(A,B)),NewHyp) :- can_be_used_for_lookups(X),
429 NewHyp=implication(not_member(X,B),member(X,A)). % x:A\B & x/:B => x:A
430 commute_bin_op(member(X,intersection(A,B)),NewHyp) :- can_be_used_for_lookups(X),
431 (NewHyp=member(X,A) ; NewHyp=member(X,B)).
432 ?commute_bin_op(disjunct(LHS,RHS),NewHyp) :- get_member_pred(LHS,X,A), get_member_pred(RHS,X,B),
433 NewHyp = member(X,union(A,B)).
434 commute_bin_op(disjunct(LHS,RHS),NewHyp) :- get_subset_pred(LHS,X,A), get_subset_pred(RHS,X,B),
435 NewHyp = subset(X,union(A,B)).
436 commute_bin_op(partition(A,List),equal(A,UNION)) :- gen_union(List,UNION).
437 % TO DO: is there a use in the all_disjoint feature?
438 commute_bin_op(forall(['$'(X)],LHSPred,RHSPred), Pred) :-
439 get_member_lhs(LHSPred,'$'(X),Set),
440 ? get_member_rhs(RHSPred,'$'(X),SET2),
441 useful_forall_superset(SET2),
442 % !x.(x:SET => x:dom(F)) => SET <: dom(F)
443 % !x.(x:SET => x:SET2) => SET <: SET2
444 not_occurs(Set,X),
445 not_occurs(SET2,X), %print(subset1(Set,SET2)),nl,
446 ? gen_subset(Set,SET2,Pred).
447 commute_bin_op(forall(['$'(X),'$'(Y)],LHSPred,RHSPred), Pred) :- % TO DO: generalise
448 get_member_lhs(LHSPred,couple('$'(X),'$'(Y)),Set), %TO DO: generalise -> domain/range
449 get_member_rhs(RHSPred,'$'(X),SET2),
450 useful_forall_superset(SET2),
451 % !x,y.(x|->y:SET => x:dom(F)) => dom(SET) <: dom(F)
452 % !x,y.(x|->y:SET => x:SET2) => dom(SET) <: SET2
453 not_occurs(Set,X),
454 not_occurs(Set,Y),
455 not_occurs(SET2,X), %print(subset2(Set,SET2)),nl,
456 ? gen_subset(domain(Set),SET2,Pred).
457 commute_bin_op(equal(A,reverse(B)),equal(B,reverse(A))).
458 commute_bin_op(not_equal(A,B),equal(A,NB)) :- negate_boolean_like_value(B,NB).
459 commute_bin_op(not_equal(intersection(Set1,Set2),empty_set), Pred) :-
460 % Set /\ {a} /= {} => a : Set
461 (Set1=set_extension([A]),B=Set2 -> true ; Set2=set_extension([A]),B=Set1),
462 Pred = member(A,B).
463 %commute_bin_op(X,_) :- print(binop(X)),nl,fail.
464
465 % transform disjuncts/equivalences/... into implications that we propagate:
466 commute_bin_op_aggressive(disjunct(LHS,RHS),implication(NegLHS,RHS),Options) :-
467 negate_norm_op(LHS,NegLHS), useful_hyp_or_imp(RHS,Options).
468 commute_bin_op_aggressive(disjunct(RHS,LHS),implication(NegLHS,RHS),Options) :-
469 negate_norm_op(LHS,NegLHS), useful_hyp_or_imp(RHS,Options).
470 commute_bin_op_aggressive(implication(LHS,RHS),implication(NegRHS,NegLHS),_) :- % contra-positive implication
471 negate_norm_op(LHS,NegLHS),
472 negate_norm_op(RHS,NegRHS).
473 commute_bin_op_aggressive(equivalence(LHS,RHS),implication(LHS,RHS),Options) :-
474 useful_hyp_or_imp(RHS,Options).
475 commute_bin_op_aggressive(equivalence(RHS,LHS),implication(LHS,RHS),Options) :-
476 useful_hyp_or_imp(RHS,Options).
477
478 % extract a membership predicate
479 get_member_pred(member(X,A),X,A).
480 get_member_pred(equal(X,A),X,set_extension([A])).
481 get_member_pred(equal(A,X),X,set_extension([A])).
482 ?get_member_pred(disjunct(LHS,RHS),X,union(A,B)) :- get_member_pred(LHS,X,A), get_member_pred(RHS,X,B).
483 % TO DO: same for subset?
484 get_subset_pred(subset(X,A),X,A).
485 get_subset_pred(subset_strict(X,A),X,A).
486 %get_subset_pred(member(X,power_set(A)),X,A).
487 get_subset_pred(disjunct(LHS,RHS),X,union(A,B)) :- get_subset_pred(LHS,X,A), get_subset_pred(RHS,X,B).
488
489 % for which supersets is it useful to derive informations from forall quantifier:
490 useful_forall_superset(domain(_)).
491 useful_forall_superset(range(_)).
492 useful_forall_superset(finite(_)).
493 useful_forall_superset(seq(_)).
494 useful_forall_superset(seq1(_)).
495 useful_forall_superset(iseq(_)).
496 useful_forall_superset(iseq1(_)).
497 useful_forall_superset(perm(_)).
498 useful_forall_superset(partial_function(_,_)).
499 useful_forall_superset(total_function(_,_)).
500 useful_forall_superset(total_injection(_,_)).
501 useful_forall_superset(total_surjection(_,_)).
502 useful_forall_superset('$'(_)).
503 useful_forall_superset(pow1_subset(_)). % not empty
504 useful_forall_superset(fin1_subset(_)). % not empty and finite
505 useful_forall_superset(fin_subset(_)). % finite info
506 % TO DO: more
507
508 is_total_relation(total_function(A,_),A).
509 is_total_relation(total_injection(A,_),A).
510 is_total_relation(total_surjection(A,_),A).
511 is_total_relation(total_bijection(A,_),A).
512 is_total_relation(total_surjection_relation(A,_),A).
513
514
515 is_surjective_relation(partial_surjection(_,B),B).
516 is_surjective_relation(surjection_relation(_,B),B).
517 is_surjective_relation(total_surjection(_,B),B).
518 is_surjective_relation(total_bijection(_,B),B).
519 is_surjective_relation(total_surjection_relation(_,B),B).
520 is_surjective_relation(perm(B),B).
521
522 negate_boolean_like_value(boolean_true,boolean_false).
523 negate_boolean_like_value(boolean_false,boolean_true).
524 % TO DO: also treat enumerated sets with exactly two values
525
526 % must match completely
527 get_member_lhs(member(X,Set),X,Set).
528 get_member_lhs(truth,_,typeset).
529
530 % must be an conjunct in rhs
531 get_member_rhs(member(X,Set),X,Set).
532 ?get_member_rhs(conjunct(A,B),X,Set) :- get_member_rhs(A,X,Set) ; get_member_rhs(B,X,Set).
533 get_member_rhs(not_equal(empty_set,X),X,pow1_subset(typeset)).
534 get_member_rhs(not_equal(X,empty_set),X,pow1_subset(typeset)).
535 get_member_rhs(finite(X),X,fin_subset(typeset)).
536
537
538 compute_bin_op_less_equal(A,B,greater_equal(B,A)) :- can_be_used_for_lookups(B).
539 compute_bin_op_less_equal(card(X),_,finite(X)) :- can_be_used_for_lookups(X).
540
541 compute_bin_op_less(A,B,less_equal(A,B)).
542 compute_bin_op_less(A,B,greater_equal(B,A)) :- can_be_used_for_lookups(B). % we do not lookup greater
543 compute_bin_op_less(A,B,not_equal(A,B)). % for not_equal we only need to store one direction
544 compute_bin_op_less(card(X),_,finite(X)) :- can_be_used_for_lookups(X). % actually card(X)>1 also implies finite(X)
545
546 compute_bin_op_equal(A,B,equal(B,A)) :-
547 can_be_used_for_lookups(B).
548 compute_bin_op_equal(A,B,falsity) :- % sometimes we have FALSE=TRUE as an alternative to falsity
549 is_explicit_value(A,VA),
550 is_explicit_value(B,VB),
551 VA \= VB.
552 compute_bin_op_equal(Set,A,Pred) :-
553 % e.g., A = B \ C => A <: B, useful for examples/B/Alstom/etcs/actions_scn_f6_372_bis.mch
554 ? derive_superset(Set,B), B \= A,
555 gen_superset(B,A,Pred). % only generate superset rule; for subset there are rules to treat set_subtraction
556 compute_bin_op_equal(A,Set,Pred) :- % interchange args
557 ? derive_superset(Set,B), B \= A,
558 gen_superset(B,A,Pred).
559 compute_bin_op_equal(A,Set,subset(B,A)) :- % A = B \/ C => B <: A ; useful to allow lookups of B
560 ? derive_subset(Set,B),
561 can_be_used_for_lookups(B), B \= A.
562 compute_bin_op_equal(A,Add,Res) :- is_add_with_nr(Add,B,Nr),
563 % A = B+Nr => B < A
564 ? (Nr>0 -> compute_bin_op_less(B,A,Res)
565 ? ; Nr<0 -> compute_bin_op_less(A,B,Res)
566 ; Res = equal(A,B)).
567 compute_bin_op_equal(A,B,finite(X)) :-
568 (A=card(X);B=card(X)), can_be_used_for_lookups(X). % actually: if any sub-expression uses card(.) we could add it?
569
570 % cf is_explicit_value/3 in well_def_prover
571 % explicit value that can be compared using Prolog unification:
572 is_explicit_value(boolean_true,pred_true).
573 is_explicit_value(boolean_false,pred_false).
574 is_explicit_value(string(A),A).
575 is_explicit_value(Nr,Nr) :- number(Nr).
576
577 is_add_with_nr(add(A,B),X,Nr) :- (number(B) -> (X,Nr)=(A,B) ; number(A) -> (X,Nr)=(B,A)).
578 is_add_with_nr(minus(A,B),A,Nr) :- number(B), Nr is -B.
579
580 derive_superset(set_subtraction(B,_),B). % B \ C <: B
581 derive_superset(intersection(B,_),B). % B /\ C <: B
582 derive_superset(intersection(_,C),C). % B /\ C <: C
583
584 derive_subset(union(B,_),B). % B <: B \/ C
585 derive_subset(union(_,C),C). % C <: B /\ C
586
587 gen_subset(A,B,subset(A,B)) :- can_be_used_for_lookups(A).
588 gen_subset(A,B,superset(B,A)) :- can_be_used_for_lookups(B).
589
590 gen_superset(A,B,superset(A,B)) :- can_be_used_for_lookups(A).
591
592 gen_union([],emptyset).
593 gen_union([X],R) :- !, R=X.
594 gen_union([X|T],union(X,UT)) :- gen_union(T,UT).
595
596 % true if we are likely to need looking up these kinds of terms
597 can_be_used_for_lookups('$'(_)).
598 %can_be_used_for_lookups(Nr) :- number(Nr).
599 can_be_used_for_lookups(domain(_)). % lookup domain of a function
600 can_be_used_for_lookups(range(_)).
601 can_be_used_for_lookups(card(_)).
602 can_be_used_for_lookups(size(_)). % TO DO: normalize size to card, we assume hyps are WD; so no difference
603 can_be_used_for_lookups(interval(_,_)).
604 % ADD: records,...
605
606 useful_hyp(finite(_)).
607 %useful_hyp(partition(_,_)). % now rewritten
608 useful_hyp(member(_,_)).
609 useful_hyp(subset(_,_)).
610 useful_hyp(equal(_,_)).
611 useful_hyp(greater_equal(_,_)).
612 useful_hyp(less_equal(_,_)).
613 useful_hyp(less_equal_real(_,_)).
614 %useful_hyp(less(_,_)). % less is now no longer looked up; we look up not_equal
615 useful_hyp(not_equal(_,_)).
616 useful_hyp(not_member(_,_)). % used in check_not_member_of_set
617 %useful_hyp(equal(A,B)) :- check if A is ID which occurs in B; e.g, x = x*1 not useful
618
619 useful_implication(implication(_,RHS),Options) :-
620 useful_hyp_or_imp(RHS,Options).
621 useful_hyp_or_imp(RHS,Options) :-
622 (useful_hyp(RHS) -> true
623 ; useful_implication_body(RHS,Options)). % useful upon pushing hyps in propagate_resolution_with_hyp
624
625 % implication or similar which could be useful (i.e., triggered so that it produces a really useful hypothesis)
626 useful_implication_body(implication(_,RHS),Options) :-
627 useful_hyp_or_imp(RHS,Options).
628 useful_implication_body(equivalence(_,_),Options) :- safe_ord_member(push_more_hyps,Options).
629 useful_implication_body(disjunct(_,_),Options) :- safe_ord_member(push_more_hyps,Options).
630 useful_implication_body(conjunct(LHS,RHS),Options) :-
631 (useful_hyp_or_imp(LHS,Options) -> true ; useful_hyp_or_imp(RHS,Options)).
632
633 % check if we can simplify the hypothesis
634 simplify_hyp(implication(LHS,RHS),Hyps,Res) :- % true => RHS --> RHS
635 %write(check_imp_lhs_hyp(LHS)),nl, avl_domain(Hyps,D), write(dom(D)),nl,
636 avl_fetch(LHS,Hyps),!, % LHS is in the hyps
637 %write(simplify_imp(LHS,RHS)),nl,
638 simplify_hyp(RHS,Hyps,Res).
639 % TODO: disjunction, ...
640 simplify_hyp(Hyp,_,Hyp).
641
642
643 % a few more binary operations that are potentially useful for :prove, particularly if negation in goal
644 potentially_useful_for_hyp_rule(less(_,_)).
645 potentially_useful_for_hyp_rule(less_real(_,_)).
646 potentially_useful_for_hyp_rule(not_subset(_,_)).
647 potentially_useful_for_hyp_rule(not_subset_strict(_,_)).
648 potentially_useful_for_hyp_rule(subset_strict(_,_)).
649 potentially_useful_for_hyp_rule(partition(_,_)).
650
651 get_clash_renaming_subst(hyp_rec(_,HInfos),ClashRenaming) :- !,
652 get_clash_renaming(HInfos,ClashRenaming).
653 get_clash_renaming_subst(H,R) :- add_internal_error('Illegal hyps:',get_clash_renaming_subst(H,R)),fail.
654
655 % rename an expression or predicate given the current variable clashes
656 get_renamed_expression(Expr,Hyps,RenExpr) :-
657 get_clash_renaming_subst(Hyps,ClashRenaming),
658 rename_bt(Expr,ClashRenaming,RenExpr).
659
660 get_normalized_and_renamed_predicate(Pred,Hyps,RenPred,NormPred) :-
661 get_clash_renaming_subst(Hyps,ClashRenaming),
662 normalize_and_rename_predicate(ClashRenaming,Pred,RenPred,NormPred).
663
664 :- use_module(library(lists),[maplist/3]).
665 % add new quantified $ untyped variables to the hyp stack
666 create_any_type($(ID),b(identifier(ID),any,[])).
667 add_new_hyp_any_vars(H,DollarIDs,H2) :-
668 maplist(create_any_type,DollarIDs,TVars),!,
669 add_new_hyp_variables(H,TVars,H2).
670 add_new_hyp_any_vars(H,I,H2) :- add_internal_error('Illegal Ids:',add_new_hyp_any_vars(H,I,H)),
671 H2=H.
672
673 % add new quantified typed variables to the hyp stack
674 add_new_hyp_variables(H,[],R) :- !, R=H.
675 add_new_hyp_variables(hyp_rec(NH,HInfos1),NewAddedTVars,hyp_rec(NH,HInfos3)) :-
676 fetch_hyp_typed_vars(HInfos1,TVars),
677 list_to_ord_set(NewAddedTVars,SortedNewTVars),
678 add_new_hyp_vars(SortedNewTVars,TVars,NewTVars2,ClashTVars),
679 (ClashTVars=[] -> HInfos2=HInfos1, NewTVars3=NewTVars2
680 ; (debug_mode(off) -> true
681 ; add_message(well_def_analyser,'Variable clash, will rename future predicates: ', ClashTVars,ClashTVars)
682 ),
683 avl_fetch(hyp_clash_vars,HInfos1,clash_rec(GenSymCount,OldClashAVL)),
684 ren_clash_variables(ClashTVars,RenClashTVars,GenSymCount,NewGSC,OldClashAVL,NewClashAVL),
685 avl_store(hyp_clash_vars,HInfos1,clash_rec(NewGSC,NewClashAVL),HInfos2),
686 list_to_ord_set(RenClashTVars,SRenClashTVars),
687 ord_union(SRenClashTVars,NewTVars2,NewTVars3)
688 ),
689 avl_store(hyp_typed_vars,HInfos2,NewTVars3,HInfos3).
690
691 % add_new_typed_vars(AddedTVars,OldTVars,NewTVars,ClashVars)
692 add_new_hyp_vars([],TVars,NewTVars,[]) :- !, NewTVars=TVars.
693 add_new_hyp_vars(AddedTVars,[],NewTVars,[]) :- !,NewTVars=AddedTVars.
694 add_new_hyp_vars([b(identifier(ID1),Type1,I1)|T1],[b(identifier(ID2),Type2,I2)|T2],NewTVars,Clash) :- !,
695 (ID1 @> ID2
696 -> NewTVars = [b(identifier(ID2),Type2,I2)|NewT],
697 add_new_hyp_vars([b(identifier(ID1),Type1,I1)|T1],T2,NewT,Clash)
698 ; ID1 @< ID2
699 -> NewTVars = [b(identifier(ID1),Type1,I1)|NewT],
700 add_new_hyp_vars(T1,[b(identifier(ID2),Type2,I2)|T2],NewT,Clash)
701 ; NewTVars = [b(identifier(ID2),Type2,I2)|NewT],
702 Clash = [b(identifier(ID1),Type1,I1)|NewClash],
703 add_new_hyp_vars(T1,T2,NewT,NewClash)
704 ).
705 add_new_hyp_vars(T1,T2,_,_) :- add_internal_error('Illegal call: ',add_new_hyp_vars(T1,T2,_,_)),fail.
706
707 % add clash ids and their renaming to the clash AVL
708 ren_clash_variables([],[],C,C,Avl,Avl).
709 ren_clash_variables([b(identifier(ID1),Type1,I1)|T1],
710 [b(identifier(RenamedID),Type1,[was(ID1)|I1])|T2], Cin,Cout,AvlIn,AvlOut) :-
711 number_codes(Cin,NC), atom_codes(Ain,NC),
712 atom_concat('$wd_rename_',Ain,RenamedID), % print(rename(ID,RenamedID)),nl,
713 C1 is Cin+1,
714 avl_store(ID1,AvlIn,RenamedID,Avl2),
715 ren_clash_variables(T1,T2,C1,Cout,Avl2,AvlOut).
716
717 % make a fresh copy of existing variables (the variables are not typed but atomic ids)
718 copy_hyp_variables(hyp_rec(NH,HInfos1),ExistingVars,Hyp2) :-
719 fetch_hyp_typed_vars(HInfos1,TVars),
720 list_to_ord_set(ExistingVars,SortedIds),
721 get_existing_tids(SortedIds,TVars,ResTVars),
722 add_new_hyp_variables(hyp_rec(NH,HInfos1),ResTVars,Hyp2).
723
724 get_existing_tids([],_,[]).
725 get_existing_tids([ID|TI],TIDs,Res) :- get_aux(TIDs,ID,TI,Res).
726 :- use_module(probsrc(bsyntaxtree), [get_texpr_id/2]).
727 get_aux([],ID,_,Res) :- add_internal_error('Cannot find existing hyp variable:',ID), Res=[].
728 get_aux([TID|TT],ID,TI,Res) :-
729 (get_texpr_id(TID,ID) -> Res=[TID|ResT], get_existing_tids(TI,TT,ResT)
730 ; get_aux(TT,ID,TI,Res)
731 ).
732
733
734 % similar to create_negation in bsyntaxtree but more rules adapted for hypotheses and WD prover
735
736 :- use_module(probsrc(bsyntaxtree),[extract_info/2]).
737 negate_hyp(b(P,pred,I),Res) :- create_negation_aux(P,I,R),!,Res=R.
738 negate_hyp(Pred,b(negation(Pred),pred,Infos)) :-
739 extract_info(Pred,Infos).
740
741 create_negation_aux(truth,I,R) :- !, R=b(falsity,pred,I).
742 create_negation_aux(falsity,I,R) :- !, R=b(truth,pred,I).
743 create_negation_aux(disjunct(A,B),I,R) :- !,
744 negate_hyp(A,NA), negate_hyp(B,NB), R = b(conjunct(NA,NB),pred,I).
745 create_negation_aux(implication(A,B),I,R) :- !, % not(A=>B) <===> A & not(B)
746 negate_hyp(B,NB), R = b(conjunct(A,NB),pred,I).
747 create_negation_aux(negation(Pred),_,R) :- !, R=Pred.
748 create_negation_aux(BOP,I,R) :- negate_op_aux(BOP,NBOP), R=b(NBOP,pred,I).
749 % no rule for conjunct(A,B)
750
751 % TODO: should we use negate_op ??
752 negate_op_aux(equal(A,B),not_equal(A,B)).
753 negate_op_aux(not_equal(A,B),equal(A,B)).
754 negate_op_aux(less(A,B),greater_equal(A,B)).
755 negate_op_aux(less_equal(A,B),greater(A,B)).
756 negate_op_aux(greater(A,B),less_equal(A,B)).
757 negate_op_aux(greater_equal(A,B),less(A,B)).
758
759 % --------------------
760
761 :- use_module(probsrc(preferences), [get_preference/2]).
762 :- use_module(probsrc(typing_tools),[is_finite_type_in_context/2]).
763 is_finite_type_for_wd(Type,_) :-
764 get_preference(wd_analysis_for_animation,true),!,
765 is_finite_type_in_context(animation,Type).
766 is_finite_type_for_wd(Type,_Hyps) :-
767 is_finite_type_in_context(proving,Type).
768
769
770 % -------------------
771
772 % convert a normalized expression to a raw expression (e.g., for pretty printing translate:print_raw_bexpr
773 % or translate:transform_raw)
774
775 :- use_module(library(lists),[is_list/1]).
776 convert_norm_expr_to_raw('$'(ID),Res) :- !, Res=identifier(unknown,ID).
777 convert_norm_expr_to_raw(Int,Res) :- integer(Int),!,Res=integer(unknown,Int).
778 convert_norm_expr_to_raw(Nr,Res) :- float(Nr),!,Res=real(unknown,Nr).
779 convert_norm_expr_to_raw(X,Res) :- integer_set_name_to_raw(X,Res), !.
780 convert_norm_expr_to_raw(set_extension(List),Res) :- !,
781 Res = set_extension(unknown,RList),
782 l_convert_norm(List,RList).
783 convert_norm_expr_to_raw(sequence_extension(List),Res) :- !,
784 Res = sequence_extension(unknown,RList),
785 l_convert_norm(List,RList).
786 convert_norm_expr_to_raw(Term,Res) :-
787 Term =.. [Functor,List,LHS,RHS],
788 ? member(Functor, [forall,event_b_comprehension_set,quantified_union,quantified_intersection]),!,
789 Res =.. [Functor,unknown,RList,RLHS,RRHS],
790 l_convert_norm(List,RList),
791 convert_norm_expr_to_raw(LHS,RLHS),
792 convert_norm_expr_to_raw(RHS,RRHS).
793 convert_norm_expr_to_raw(exists(List,Pred),Res) :- !,
794 Res = exists(unknown,RList,RPred),
795 l_convert_norm(List,RList),
796 convert_norm_expr_to_raw(Pred,RPred).
797 convert_norm_expr_to_raw(function(Functor,Args),Res) :- !,
798 Res = function(unknown,RFunctor,RList),
799 (is_list(Args) -> List=Args ; List=[Args]),
800 l_convert_norm(List,RList),
801 convert_norm_expr_to_raw(Functor,RFunctor).
802 convert_norm_expr_to_raw(partition(Set,Args),Res) :- !,
803 Res = partition(unknown,RSet,RArgs),
804 l_convert_norm(Args,RArgs),
805 convert_norm_expr_to_raw(Set,RSet).
806 % TODO: more special cases where generic code below does not work:
807 convert_norm_expr_to_raw(Term,Res) :- Term =.. [Functor|Args],
808 l_convert_norm(Args,RawArgs),
809 Res =.. [Functor,unknown|RawArgs].
810
811 l_convert_norm([],[]).
812 l_convert_norm([H|T],[RH|RT]) :- convert_norm_expr_to_raw(H,RH), l_convert_norm(T,RT).
813
814 integer_set_name_to_raw('INTEGER',integer_set(unknown)).
815 integer_set_name_to_raw('NATURAL',natural_set(unknown)).
816 integer_set_name_to_raw('NATURAL1',natural1_set(unknown)).
817 integer_set_name_to_raw('INT',int_set(unknown)).
818 integer_set_name_to_raw('NAT',nat_set(unknown)).
819 integer_set_name_to_raw('NAT1',nat1_set(unknown)).
820
821 :- use_module(probsrc(translate),[translate_raw_bexpr/2,translate_raw_bexpr_with_limit/3]).
822 translate_norm_expr(NormExpr,Str) :-
823 (convert_norm_expr_to_raw(NormExpr,RawExpr)
824 -> translate_raw_bexpr(RawExpr,Str)
825 ; add_error(translate_norm_expr,'Cannot translate norm expression:',NormExpr),
826 Str = '???'
827 ).
828 translate_norm_expr_with_limit(NormExpr,Limit,Str) :-
829 (convert_norm_expr_to_raw(NormExpr,RawExpr)
830 -> translate_raw_bexpr_with_limit(RawExpr,Limit,Str)
831 ; add_error(translate_norm_expr,'Cannot translate norm expression:',NormExpr),
832 Str = '???'
833 ).