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