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