| 1 | % (c) 2014-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(b_simplifier, [simplify_b_predicate/2, tcltk_simplify_b_predicate/2, tcltk_simplify_b_predicate_error/2]). | |
| 6 | ||
| 7 | /* Modules and Infos for the code coverage analysis */ | |
| 8 | :- use_module(probsrc(module_information)). | |
| 9 | :- module_info(group,model_checker). | |
| 10 | :- module_info(description,'This module provides predicates for simplifying B predicates'). | |
| 11 | ||
| 12 | % Classical B prolog modules | |
| 13 | :- use_module(probsrc(bsyntaxtree), [get_texpr_expr/2, get_texpr_type/2, create_texpr/4, | |
| 14 | same_texpr/2, different_texpr_values/2, | |
| 15 | is_truth/1, is_falsity/1, is_negation_of/2, | |
| 16 | is_a_conjunct/3, is_a_disjunct/3, is_an_implication/3, | |
| 17 | conjunct_predicates/2, disjunct_predicates/2, | |
| 18 | %% extract_info/3, | |
| 19 | create_negation/2, | |
| 20 | create_implication/3, create_equivalence/3, | |
| 21 | create_forall/3, create_or_merge_exists/3]). | |
| 22 | :- use_module(probsrc(bmachine),[b_parse_machine_predicate/2]). | |
| 23 | :- use_module(probsrc(kernel_objects),[element_of_global_set/2]). | |
| 24 | ||
| 25 | %% :- use_module(probsrc(parsercall), [parse_predicate/2]). | |
| 26 | ||
| 27 | :- use_module(probsrc(preferences),[temporary_set_preference/3, reset_temporary_preference/2]). | |
| 28 | :- use_module(probsrc(translate), [translate_bexpression/2, generate_typing_predicates/2]). | |
| 29 | %% :- use_module(probsrc(debug), [debug_print/2, debug_println/2, debug_mode/1]). | |
| 30 | :- use_module(probsrc(error_manager),[add_internal_error/2]). | |
| 31 | ||
| 32 | % Importing unit tests predicates | |
| 33 | :- use_module(probsrc(self_check)). | |
| 34 | ||
| 35 | % SICSTUS libraries | |
| 36 | :- use_module(library(avl)). | |
| 37 | :- use_module(library(plunit)). | |
| 38 | ||
| 39 | ||
| 40 | tcltk_simplify_b_predicate_error(String,StringResult) :- | |
| 41 | if(tcltk_simplify_b_predicate(String,StringResult),true, | |
| 42 | (StringResult = 'Error or User-Interrupt')). | |
| 43 | ||
| 44 | % exported for the Tcl/Tk interface | |
| 45 | tcltk_simplify_b_predicate(String,StringResult) :- | |
| 46 | b_parse_machine_predicate(String,Tree), | |
| 47 | simplify_b_predicate(Tree,Res), | |
| 48 | translate_bexpression(Res,StringResult). | |
| 49 | ||
| 50 | %% simplify_b_predicate(+Pred, -Res). | |
| 51 | simplify_b_predicate(Pred,Res) :- | |
| 52 | get_texpr_expr(Pred,Expr), | |
| 53 | get_texpr_type(Pred,Type), | |
| 54 | simplify_b_predicate2(Expr,Type,Res). | |
| 55 | ||
| 56 | simplify_b_predicate2(truth,Type,Res) :- !, | |
| 57 | create_texpr(truth,Type,[],Res). | |
| 58 | simplify_b_predicate2(falsity,Type,Res) :- !, | |
| 59 | create_texpr(falsity,Type,[],Res). | |
| 60 | simplify_b_predicate2(negation(BExpr),_Type,Res) :- !, | |
| 61 | simplify_negation(BExpr,Res). | |
| 62 | simplify_b_predicate2(conjunct(LHS,RHS),Type,Res) :- !, | |
| 63 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 64 | ( is_false_constant(SimplifiedLHS) -> % FALSE & f == FALSE | |
| 65 | create_texpr(falsity,Type,[],Res) | |
| 66 | ; is_true_constant(SimplifiedLHS) -> % TRUE & f == f | |
| 67 | simplify_b_predicate(RHS,Res) | |
| 68 | ; | |
| 69 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 70 | ( is_false_constant(SimplifiedRHS) -> % f & FALSE == FALSE | |
| 71 | create_texpr(falsity,Type,[],Res) | |
| 72 | ; is_true_constant(SimplifiedRHS) -> % f & TRUE == f | |
| 73 | Res = SimplifiedLHS | |
| 74 | ; same_texpr(SimplifiedLHS,SimplifiedRHS) -> % f & f == f (idempotence) | |
| 75 | Res = SimplifiedLHS | |
| 76 | ; is_absorption_rule_conjunct(SimplifiedLHS,SimplifiedRHS,Res) -> % f & (f or g) == f (absorption) | |
| 77 | true | |
| 78 | % note: (f => g) & (g => h) is not equivalent to (f => h) (it only implies it), so it is not rewritten | |
| 79 | ; | |
| 80 | conjunct_predicates([SimplifiedLHS,SimplifiedRHS],Res) | |
| 81 | ) | |
| 82 | ). | |
| 83 | simplify_b_predicate2(disjunct(LHS,RHS),Type,Res) :- !, | |
| 84 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 85 | ( is_true_constant(SimplifiedLHS) -> % TRUE or f == TRUE | |
| 86 | create_texpr(truth,Type,[],Res) | |
| 87 | ; is_false_constant(SimplifiedLHS) -> % FALSE or f == f | |
| 88 | simplify_b_predicate(RHS,Res) | |
| 89 | ; | |
| 90 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 91 | ( is_true_constant(SimplifiedRHS) -> % f or TRUE == TRUE | |
| 92 | create_texpr(truth,Type,[],Res) | |
| 93 | ; is_false_constant(SimplifiedRHS) -> % f or FALSE == f | |
| 94 | Res = SimplifiedLHS | |
| 95 | ; same_texpr(SimplifiedLHS,SimplifiedRHS) -> % f or f == f (idempotence) | |
| 96 | Res = SimplifiedLHS | |
| 97 | ; is_absorption_rule_disjunct(LHS,RHS,Res) -> % f or (f & g) == f (absorption) | |
| 98 | true | |
| 99 | ; | |
| 100 | disjunct_predicates([SimplifiedLHS,SimplifiedRHS],Res) | |
| 101 | ) | |
| 102 | ). | |
| 103 | simplify_b_predicate2(implication(LHS,RHS),_Type,Res) :- !, | |
| 104 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 105 | simplify_implication(LHS,SimplifiedRHS,Res). | |
| 106 | simplify_b_predicate2(equivalence(LHS,RHS),Type,Res) :- !, | |
| 107 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 108 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 109 | ( same_texpr(SimplifiedLHS,SimplifiedRHS) -> % f <=> f == TRUE | |
| 110 | create_texpr(truth,Type,[],Res) | |
| 111 | ; is_negation_of(SimplifiedLHS,SimplifiedRHS) -> % f <=> not(f) == FALSE | |
| 112 | create_texpr(falsity,Type,[],Res) | |
| 113 | ; | |
| 114 | create_equivalence(SimplifiedLHS,SimplifiedRHS,Res) | |
| 115 | ). | |
| 116 | simplify_b_predicate2(equal(LHS,RHS),Type,Res) :- !, | |
| 117 | print(equal(LHS,RHS)),nl, | |
| 118 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 119 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 120 | (same_texpr(SimplifiedLHS,SimplifiedRHS) -> | |
| 121 | create_texpr(truth,Type,[],Res) | |
| 122 | ; different_texpr_values(SimplifiedLHS,SimplifiedRHS) -> | |
| 123 | create_texpr(falsity,Type,[],Res) | |
| 124 | ; | |
| 125 | compare('==',SimplifiedLHS,SimplifiedRHS,equal,Type,Res) | |
| 126 | %% extract_info(SimplifiedLHS,SimplifiedRHS,Info), | |
| 127 | %% create_texpr(equal(SimplifiedLHS,SimplifiedRHS),Type,Info,Res) | |
| 128 | ). | |
| 129 | simplify_b_predicate2(not_equal(LHS,RHS),Type,Res) :- !, | |
| 130 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 131 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 132 | (same_texpr(SimplifiedLHS,SimplifiedRHS) -> | |
| 133 | create_texpr(falsity,Type,[],Res) | |
| 134 | ; different_texpr_values(SimplifiedLHS,SimplifiedRHS) -> | |
| 135 | create_texpr(truth,Type,[],Res) | |
| 136 | ; | |
| 137 | compare('=\\=',SimplifiedLHS,SimplifiedRHS,not_equal,Type,Res) | |
| 138 | %% extract_info(SimplifiedLHS,SimplifiedRHS,Info), | |
| 139 | %% create_texpr(not_equal(SimplifiedLHS,SimplifiedRHS),Type,Info,Res) | |
| 140 | ). | |
| 141 | simplify_b_predicate2(greater(LHS,RHS),Type,Res) :- !, | |
| 142 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 143 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 144 | compare('>',SimplifiedLHS,SimplifiedRHS,greater,Type,Res). | |
| 145 | simplify_b_predicate2(greater_equal(LHS,RHS),Type,Res) :- !, | |
| 146 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 147 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 148 | compare('>=',SimplifiedLHS,SimplifiedRHS,greater_equal,Type,Res). | |
| 149 | simplify_b_predicate2(less(LHS,RHS),Type,Res) :- !, | |
| 150 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 151 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 152 | compare('<',SimplifiedLHS,SimplifiedRHS,less,Type,Res). | |
| 153 | simplify_b_predicate2(less_equal(LHS,RHS),Type,Res) :- !, | |
| 154 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 155 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 156 | compare('=<',SimplifiedLHS,SimplifiedRHS,less_equal,Type,Res). | |
| 157 | /* Add more sophisticated implementations for simplifying predicates with quantifiers: | |
| 158 | E.g.: !(x).(x:S => x<21) == max(S) < 21 | |
| 159 | !(x).(x:S => x/=3) == 3/:S */ | |
| 160 | % Quantified predcates | |
| 161 | % !(x).(P => Q) | |
| 162 | simplify_b_predicate2(forall(Ids,LHS,RHS),Type,Res) :- !, | |
| 163 | simplify_b_predicate(RHS,SimplifiedRHS), | |
| 164 | (is_true_constant(SimplifiedRHS) -> | |
| 165 | create_texpr(truth,Type,[],Res) | |
| 166 | ; | |
| 167 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 168 | create_implication(SimplifiedLHS,SimplifiedRHS,ImplicationRes), | |
| 169 | create_forall(Ids,ImplicationRes,Res) | |
| 170 | ). | |
| 171 | % #(x).(P) | |
| 172 | simplify_b_predicate2(exists(Ids,Pred),Type,Res) :- !, | |
| 173 | simplify_b_predicate(Pred,SimplifiedPred), | |
| 174 | (is_true_constant(SimplifiedPred) -> | |
| 175 | create_texpr(truth,Type,[],Res) | |
| 176 | ; is_false_constant(SimplifiedPred) -> | |
| 177 | create_texpr(falsity,Type,[],Res) | |
| 178 | ; | |
| 179 | create_or_merge_exists(Ids,SimplifiedPred,Res) | |
| 180 | ). | |
| 181 | % Simplifying membership relations | |
| 182 | simplify_b_predicate2(member(Element,Set),Type,Res) :- !, | |
| 183 | simplify_membership_predicate(Element,Set,Type,Res). | |
| 184 | simplify_b_predicate2(not_member(Element,Set),Type,Res) :- !, | |
| 185 | simplify_non_membership_predicate(Element,Set,Type,Res). | |
| 186 | % TODO: do we need to safe additional information about the predicates | |
| 187 | simplify_b_predicate2(BExpr,Type,Res) :- | |
| 188 | create_texpr(BExpr,Type,[],Res). | |
| 189 | %% (debug_mode(on) -> | |
| 190 | %% print('Construct not covered yet (b_simplifier):'),nl, | |
| 191 | %% print(BExpr),nl, | |
| 192 | %% translate:print_bexpr(Res),nl | |
| 193 | %% ; true). | |
| 194 | ||
| 195 | simplify_negation(b(negation(BExpr),pred,_), Res) :- !, % double negation "not not f == f" | |
| 196 | simplify_b_predicate(BExpr, Res). | |
| 197 | simplify_negation(Impl, Res) :- | |
| 198 | Impl = b(implication(Lhs,Rhs),pred,Info), | |
| 199 | !, | |
| 200 | simplify_b_predicate(Impl, SImpl), | |
| 201 | ( SImpl \= b(implication(_,_),pred,_) | |
| 202 | -> % implication could be simplified to TRUTH, FALSITY or a single negation | |
| 203 | simplify_b_predicate(b(negation(SImpl),pred,Info), Res) | |
| 204 | ; % "not (a => b) == a and not b" | |
| 205 | NRhs = b(negation(Rhs),pred,[]), | |
| 206 | simplify_b_predicate(b(conjunct(Lhs,NRhs),pred,Info), Res) | |
| 207 | ). | |
| 208 | simplify_negation(b(conjunct(Lhs,Rhs),pred,Info), Res) :- !, | |
| 209 | NLhs = b(negation(Lhs),pred,[]), | |
| 210 | NRhs = b(negation(Rhs),pred,[]), | |
| 211 | simplify_b_predicate(b(disjunct(NLhs,NRhs),pred,Info), Res). | |
| 212 | simplify_negation(b(disjunct(Lhs,Rhs),pred,Info),Res) :- !, | |
| 213 | NLhs = b(negation(Lhs),pred,[]), | |
| 214 | NRhs = b(negation(Rhs),pred,[]), | |
| 215 | simplify_b_predicate(b(conjunct(NLhs,NRhs),pred,Info), Res). | |
| 216 | simplify_negation(b(forall(Ids,Lhs,Rhs),pred,Info),Res) :- !, | |
| 217 | simplify_b_predicate(b(negation(b(implication(Lhs,Rhs),pred,Info)),pred,[]), Body), | |
| 218 | Res = b(exists(Ids,Body),pred,Info). | |
| 219 | simplify_negation(b(exists(Ids,Body),pred,Info),Res) :- !, | |
| 220 | simplify_b_predicate(b(negation(Body),pred,[]), NBody), | |
| 221 | generate_typing_predicates(Ids, TypingList), | |
| 222 | conjunct_predicates(TypingList, Typing), | |
| 223 | Res = b(forall(Ids,Typing,NBody),pred,Info). | |
| 224 | simplify_negation(b(Node,pred,Info),Res) :- | |
| 225 | Node =.. [Functor,Lhs,Rhs], | |
| 226 | negated_binary_pred(Functor, NegFunctor), | |
| 227 | !, | |
| 228 | NegNode =.. [NegFunctor,Lhs,Rhs], | |
| 229 | simplify_b_predicate(b(NegNode,pred,Info), Res). | |
| 230 | simplify_negation(b(Node,pred,Info),Res) :- | |
| 231 | Node =.. [Functor,Lhs,Rhs], | |
| 232 | negate_and_swap(Functor, NegFunctor), | |
| 233 | !, | |
| 234 | NegNode =.. [NegFunctor,Rhs,Lhs], | |
| 235 | simplify_b_predicate(b(NegNode,pred,Info), Res). | |
| 236 | simplify_negation(BExpr, Res) :- | |
| 237 | simplify_b_predicate(BExpr, SimplifiedBExpr), | |
| 238 | % Simplifications like "not(FALSE) == TRUE" and "not(TRUE) == FALSE" will be applied by create_negation/2 | |
| 239 | create_negation(SimplifiedBExpr, Res). | |
| 240 | ||
| 241 | negated_binary_pred(member, not_member). | |
| 242 | negated_binary_pred(not_member, member). | |
| 243 | negated_binary_pred(less, greater_equal). | |
| 244 | negated_binary_pred(less_equal, greater). | |
| 245 | negated_binary_pred(greater, less_equal). | |
| 246 | negated_binary_pred(greater_equal, less). | |
| 247 | negated_binary_pred(equal, not_equal). | |
| 248 | negated_binary_pred(not_equal, equal). | |
| 249 | negated_binary_pred(subset, not_subset). | |
| 250 | negated_binary_pred(not_subset, subset). | |
| 251 | negated_binary_pred(subset_strict, not_subset_strict). | |
| 252 | negated_binary_pred(not_subset_strict, subset_strict). | |
| 253 | ||
| 254 | negate_and_swap(less_real,less_equal_real). | |
| 255 | negate_and_swap(less_equal_real,less_real). | |
| 256 | ||
| 257 | simplify_implication(_LHS,SimplifiedRHS,Res) :- | |
| 258 | is_true_constant(SimplifiedRHS),!, % f => TRUE == TRUE | |
| 259 | create_texpr(truth,pred,[],Res). | |
| 260 | simplify_implication(LHS,SimplifiedRHS,Res) :- | |
| 261 | is_false_constant(SimplifiedRHS),!, % f => FALSE == neg(f) | |
| 262 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 263 | create_negation(SimplifiedLHS,Res). | |
| 264 | simplify_implication(LHS,SimplifiedRHS,Res) :- | |
| 265 | simplify_b_predicate(LHS,SimplifiedLHS), | |
| 266 | (is_false_constant(SimplifiedLHS) -> % FALSE => f == TRUE | |
| 267 | create_texpr(truth,pred,[],Res) | |
| 268 | ; is_true_constant(SimplifiedLHS) -> % TRUE => f == f | |
| 269 | Res = SimplifiedRHS | |
| 270 | ; same_texpr(SimplifiedLHS,SimplifiedRHS) -> % f => f == TRUE | |
| 271 | create_texpr(truth,pred,[],Res) | |
| 272 | ; | |
| 273 | create_implication(SimplifiedLHS,SimplifiedRHS,Res) | |
| 274 | ). | |
| 275 | ||
| 276 | ||
| 277 | %%%%%%%%% This is too trivial, we should consider the absorption and transition rules w.r.t. the origin predicate %%%%%%%%% | |
| 278 | % P or (P & Q) == P | |
| 279 | is_absorption_rule_disjunct(LHS,RHS,Res) :- | |
| 280 | is_a_conjunct(RHS,P,Q), | |
| 281 | (same_texpr(LHS,P); same_texpr(LHS,Q)),!, | |
| 282 | Res = LHS. | |
| 283 | % (P & Q) or P == P | |
| 284 | is_absorption_rule_disjunct(LHS,RHS,Res) :- | |
| 285 | is_a_conjunct(LHS,P,Q), | |
| 286 | (same_texpr(RHS,P); same_texpr(RHS,Q)),!, | |
| 287 | Res = RHS. | |
| 288 | ||
| 289 | % P & (P or Q) == P | |
| 290 | is_absorption_rule_conjunct(LHS,RHS,Res) :- | |
| 291 | is_a_disjunct(RHS,P,Q), | |
| 292 | (same_texpr(LHS,P); same_texpr(LHS,Q)),!, | |
| 293 | Res = LHS. | |
| 294 | % (P or Q) & P == P | |
| 295 | is_absorption_rule_conjunct(LHS,RHS,Res) :- | |
| 296 | is_a_disjunct(LHS,P,Q), | |
| 297 | (same_texpr(RHS,P); same_texpr(RHS,Q)),!, | |
| 298 | Res = RHS. | |
| 299 | ||
| 300 | ||
| 301 | ||
| 302 | simplify_non_membership_predicate(Element,Set,Type,Res) :- | |
| 303 | b_integer_value(Element,IsIntValue,EX,_NewElement), | |
| 304 | IsIntValue, | |
| 305 | test_integer_membership(Set,EX,Type,Res1),!, | |
| 306 | (is_true_constant(Res1) -> | |
| 307 | create_texpr(falsity,Type,[],Res) | |
| 308 | ; is_false_constant(Res1) -> | |
| 309 | create_texpr(truth,Type,[],Res) | |
| 310 | ; | |
| 311 | add_internal_error('Internal error: Unexpected result from test_integer_membership/4 predicate in simplify_membership_predicate/4: ',Res1) | |
| 312 | ). | |
| 313 | simplify_non_membership_predicate(Element,Set,Type,Res) :- | |
| 314 | b_integer_value(Element,_IsIntValue,_EX,NewElement), | |
| 315 | create_texpr(not_member(NewElement,Set),Type,[],Res). | |
| 316 | ||
| 317 | simplify_membership_predicate(Element,Set,Type,Res) :- | |
| 318 | b_integer_value(Element,IsIntValue,EX,_NewElement), | |
| 319 | IsIntValue, | |
| 320 | test_integer_membership(Set,EX,Type,Res),!. | |
| 321 | simplify_membership_predicate(Element,Set,Type,Res) :- | |
| 322 | b_integer_value(Element,_IsIntValue,_EX,NewElement), | |
| 323 | create_texpr(member(NewElement,Set),Type,[],Res). | |
| 324 | ||
| 325 | ||
| 326 | test_integer_membership(b(empty_set,set(integer),_),_X,Type,Res) :- !, | |
| 327 | create_texpr(falsity,Type,[],Res). | |
| 328 | test_integer_membership(b(value(avl_set(AVLSet)),set(integer),_),X,Type,Res) :- !, | |
| 329 | (avl_fetch(int(X),AVLSet) -> | |
| 330 | create_texpr(truth,Type,[],Res) | |
| 331 | ; | |
| 332 | create_texpr(falsity,Type,[],Res) | |
| 333 | ). | |
| 334 | test_integer_membership(Set,X,Type,Res) :- | |
| 335 | is_interval_set(Set,Low,Up),!, | |
| 336 | ((Low=<X,X=<Up) -> | |
| 337 | create_texpr(truth,Type,[],Res) | |
| 338 | ; | |
| 339 | create_texpr(falsity,Type,[],Res) | |
| 340 | ). | |
| 341 | test_integer_membership(Set,X,Type,Res) :- | |
| 342 | is_global_integer_set(Set,GlobalSet),!, | |
| 343 | (element_of_global_set(int(X),GlobalSet) -> | |
| 344 | create_texpr(truth,Type,[],Res) | |
| 345 | ; | |
| 346 | create_texpr(falsity,Type,[],Res) | |
| 347 | ). | |
| 348 | ||
| 349 | is_interval_set(b(interval(b(Low,integer,_),b(Up,integer,_)),set(integer),_),Low,Up). | |
| 350 | ||
| 351 | is_global_integer_set(Set,GlobalSet) :- | |
| 352 | get_texpr_expr(Set,SetExpr), | |
| 353 | SetExpr = integer_set(GlobalSet), | |
| 354 | memberchk(GlobalSet,['NATURAL','NAT','INTEGER','INT']). | |
| 355 | ||
| 356 | is_true_constant(Expr) :- | |
| 357 | (is_truth(Expr); Expr=boolean_true). | |
| 358 | ||
| 359 | is_false_constant(Expr) :- | |
| 360 | (is_falsity(Expr); Expr=boolean_false). | |
| 361 | ||
| 362 | compare(Operator,LHS,RHS,Functor,Type,Res) :- | |
| 363 | b_integer_value(LHS,IsIntValueX,EX,NewLHS), | |
| 364 | b_integer_value(RHS,IsIntValueY,EY,NewRHS), | |
| 365 | ((IsIntValueX,IsIntValueY) -> | |
| 366 | (memberchk(Operator,['<','>','=<','>=','==','=\\=']) -> | |
| 367 | Call =.. [Operator,EX,EY], | |
| 368 | (Call -> create_texpr(truth,Type,[],Res); create_texpr(falsity,Type,[],Res)) | |
| 369 | ; | |
| 370 | add_internal_error('Internal error: Unexpected arithmetic comparison Operator: ', Operator), | |
| 371 | BExpr =.. [Functor,NewLHS,NewRHS], | |
| 372 | create_texpr(BExpr,Type,[],Res) | |
| 373 | ) | |
| 374 | ; | |
| 375 | BExpr =.. [Functor,NewLHS,NewRHS], | |
| 376 | create_texpr(BExpr,Type,[],Res) | |
| 377 | ). | |
| 378 | ||
| 379 | b_integer_value(BExpr,IsIntValue,Res,NewBExpr) :- | |
| 380 | b_arithmetic_expression(BExpr,X),!, | |
| 381 | Res is X,IsIntValue=true, | |
| 382 | create_texpr(integer(Res),integer,[],NewBExpr). | |
| 383 | b_integer_value(BExpr,IsIntValue,Res,NewBExpr) :- | |
| 384 | Res = BExpr,IsIntValue=false, | |
| 385 | NewBExpr=BExpr. | |
| 386 | ||
| 387 | b_arithmetic_expression(BExpr,Res) :- | |
| 388 | get_texpr_expr(BExpr,Expr), | |
| 389 | get_texpr_type(BExpr,integer), % only an integer expression | |
| 390 | b_arithmetic_expression1(Expr,Res). | |
| 391 | ||
| 392 | b_arithmetic_expression1(integer(X),X) :- !, | |
| 393 | number(X). | |
| 394 | b_arithmetic_expression1(unary_minus(E),Res) :- !, | |
| 395 | b_arithmetic_expression(E,ERes), | |
| 396 | Res = '-'(ERes). | |
| 397 | b_arithmetic_expression1(add(E1,E2),Res) :- !, | |
| 398 | b_arithmetic_expression(E1,E1Res), | |
| 399 | b_arithmetic_expression(E2,E2Res), | |
| 400 | Res = '+'(E1Res,E2Res). | |
| 401 | b_arithmetic_expression1(minus(E1,E2),Res) :- !, | |
| 402 | b_arithmetic_expression(E1,E1Res), | |
| 403 | b_arithmetic_expression(E2,E2Res), | |
| 404 | Res = '-'(E1Res,E2Res). | |
| 405 | b_arithmetic_expression1(multiplication(E1,E2),Res) :- !, | |
| 406 | b_arithmetic_expression(E1,E1Res), | |
| 407 | b_arithmetic_expression(E2,E2Res), | |
| 408 | Res = '*'(E1Res,E2Res). | |
| 409 | % it is not the same div as in Prolog X/Y will be rounded down | |
| 410 | %% b_arithmetic_expression1(div(E1,E2),Res) :- !, | |
| 411 | %% b_arithmetic_expression(E1,E1Res), | |
| 412 | %% b_arithmetic_expression(E2,E2Res), | |
| 413 | %% Res = '//'(E1Res,E2Res). | |
| 414 | ||
| 415 | ||
| 416 | :- begin_tests(simplify_b_predicate). | |
| 417 | ||
| 418 | test(simplify_neg_conj, [all(Simplified = [b(less_equal(b(identifier(x),integer,[]),b(integer(1),integer,[])),pred,[])])]) :- | |
| 419 | IntOne = b(integer(1),integer,[]), | |
| 420 | Id = b(identifier(x),integer,[]), | |
| 421 | Greater = b(greater(Id,IntOne),pred,[]), | |
| 422 | Pred = b(negation(b(conjunct(Greater,Greater),pred,[])),pred,[]), | |
| 423 | simplify_b_predicate(Pred, Simplified), | |
| 424 | ground(Simplified). | |
| 425 | ||
| 426 | test(simplify_neg_disj, [all(Simplified = [b(less_equal(b(identifier(x),integer,[]),b(integer(1),integer,[])),pred,[])])]) :- | |
| 427 | IntOne = b(integer(1),integer,[]), | |
| 428 | Id = b(identifier(x),integer,[]), | |
| 429 | Greater = b(greater(Id,IntOne),pred,[]), | |
| 430 | Pred = b(negation(b(disjunct(Greater,Greater),pred,[])),pred,[]), | |
| 431 | simplify_b_predicate(Pred, Simplified), | |
| 432 | ground(Simplified). | |
| 433 | ||
| 434 | test(simplify_neg_equal, [all(Simplified = [b(not_equal(b(identifier(x),integer,[]),b(integer(1),integer,[])),pred,_)])]) :- | |
| 435 | IntOne = b(integer(1),integer,[]), | |
| 436 | Id = b(identifier(x),integer,[]), | |
| 437 | Pred = b(negation(b(equal(Id,IntOne),pred,[])),pred,[]), | |
| 438 | simplify_b_predicate(Pred, Simplified), | |
| 439 | ground(Simplified). | |
| 440 | ||
| 441 | test(simplify_neg_not_equal, [all(Simplified = [b(equal(b(identifier(x),integer,[]),b(integer(1),integer,[])),pred,_)])]) :- | |
| 442 | IntOne = b(integer(1),integer,[]), | |
| 443 | Id = b(identifier(x),integer,[]), | |
| 444 | Pred = b(negation(b(not_equal(Id,IntOne),pred,[])),pred,[]), | |
| 445 | simplify_b_predicate(Pred, Simplified), | |
| 446 | ground(Simplified). | |
| 447 | ||
| 448 | test(simplify_impl_to_truth, [all(Simplified = [b(truth,pred,[])])]) :- | |
| 449 | IntOne = b(integer(1),integer,[]), | |
| 450 | Id = b(identifier(x),integer,[]), | |
| 451 | Greater = b(greater(Id,IntOne),pred,[]), | |
| 452 | Pred = b(implication(Greater,Greater),pred,[]), | |
| 453 | simplify_b_predicate(Pred, Simplified), | |
| 454 | ground(Simplified). | |
| 455 | ||
| 456 | test(simplify_impl_to_falsity, [all(Simplified = [b(falsity,pred,[])])]) :- | |
| 457 | IntOne = b(integer(1),integer,[]), | |
| 458 | Id = b(identifier(x),integer,[]), | |
| 459 | Greater = b(greater(Id,IntOne),pred,[]), | |
| 460 | Pred = b(negation(b(implication(Greater,Greater),pred,[])),pred,[]), | |
| 461 | simplify_b_predicate(Pred, Simplified), | |
| 462 | ground(Simplified). | |
| 463 | ||
| 464 | test(simplify_impl_same, [all(Simplified = [b(implication(b(greater(b(identifier(x),integer,[]),b(integer(1),integer,[])),pred,[]),b(less(b(identifier(y),integer,[]),b(integer(1),integer,[])),pred,[])),pred,[])])]) :- | |
| 465 | IntOne = b(integer(1),integer,[]), | |
| 466 | Id1 = b(identifier(x),integer,[]), | |
| 467 | Id2 = b(identifier(y),integer,[]), | |
| 468 | Greater = b(greater(Id1,IntOne),pred,[]), | |
| 469 | Less = b(less(Id2,IntOne),pred,[]), | |
| 470 | Pred = b(implication(Greater,Less),pred,[]), | |
| 471 | simplify_b_predicate(Pred, Simplified), | |
| 472 | ground(Simplified). | |
| 473 | ||
| 474 | test(simplify_neg_impl, [all(Simplified = [b(conjunct(b(greater(b(identifier(x),integer,[]),b(integer(1),integer,[])),pred,[]),b(greater_equal(b(identifier(y),integer,[]),b(integer(1),integer,[])),pred,[])),pred,[])])]) :- | |
| 475 | IntOne = b(integer(1),integer,[]), | |
| 476 | Id1 = b(identifier(x),integer,[]), | |
| 477 | Id2 = b(identifier(y),integer,[]), | |
| 478 | Greater = b(greater(Id1,IntOne),pred,[]), | |
| 479 | Less = b(less(Id2,IntOne),pred,[]), | |
| 480 | Pred = b(negation(b(implication(Greater,Less),pred,[])),pred,[]), | |
| 481 | simplify_b_predicate(Pred, Simplified), | |
| 482 | ground(Simplified). | |
| 483 | ||
| 484 | :- end_tests(simplify_b_predicate). | |
| 485 | ||
| 486 | % (a=TRUE => b=TRUE) & (b=TRUE => c=TRUE) must not become a=TRUE => c=TRUE (false for a=FALSE,b=TRUE,c=FALSE) | |
| 487 | :- assert_must_succeed((simplifier_test_bool_eq(a,A), simplifier_test_bool_eq(b,B), simplifier_test_bool_eq(c,C), | |
| 488 | create_texpr(implication(A,B),pred,[],I1), create_texpr(implication(B,C),pred,[],I2), | |
| 489 | create_texpr(conjunct(I1,I2),pred,[],Conj), | |
| 490 | simplify_b_predicate(Conj,Res), Res = b(conjunct(_,_),pred,_))). | |
| 491 | simplifier_test_bool_eq(Id,Eq) :- | |
| 492 | create_texpr(identifier(Id),boolean,[],TId), create_texpr(boolean_true,boolean,[],True), | |
| 493 | create_texpr(equal(TId,True),pred,[],Eq). |