| 1 | | % (c) 2009-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 | | |
| 6 | | :- module(b_interpreter, |
| 7 | | [b_test_boolean_expression/4, b_test_boolean_expression/6, |
| 8 | | %b_test_boolean_expression_wf/3, |
| 9 | | b_test_boolean_expression_cs/5, |
| 10 | | b_test_boolean_expression_for_ground_state/4, b_test_boolean_expression_for_ground_state/5, |
| 11 | | % b_det_test_boolean_expression/6, |
| 12 | | b_not_test_boolean_expression/4, b_not_test_boolean_expression/6, |
| 13 | | %b_not_test_boolean_expression_wf/3, |
| 14 | | b_not_test_boolean_expression_cs/5, |
| 15 | | |
| 16 | | b_convert_bool/5, |
| 17 | | b_convert_bool_timeout/7, |
| 18 | | b_compute_expression/5, |
| 19 | | |
| 20 | | b_execute_top_level_statement/7, |
| 21 | | b_execute_statement_nowf/5, |
| 22 | | |
| 23 | | b_compute_expression_nowf/4, b_compute_expression_nowf/6, b_compute_expression_nowf/7, |
| 24 | | b_compute_explicit_epression_no_wf/6, b_compute_explicit_epression_no_wf/7, |
| 25 | | |
| 26 | | b_set_up_concrete_constants/2, b_partial_set_up_concrete_constants/1, |
| 27 | | b_initialise_machine/4, |
| 28 | | |
| 29 | | b_execute_top_level_operation_update/5, b_execute_top_level_operation_wf/8, |
| 30 | | |
| 31 | | convert_list_of_expressions_into_set_wf/4, |
| 32 | | %state_satisfies_negation_of_invariant/1, |
| 33 | | |
| 34 | | b_state_violates_invariant/2, |
| 35 | | state_violates_assertions/2, |
| 36 | | analyse_invariant_for_state/1, |
| 37 | | |
| 38 | | insert_before_substitution_variables/5, % add $0 values to a store |
| 39 | | |
| 40 | | try_lookup_value_in_store_and_global_sets/3, lookup_value_in_store_and_global_sets_wf/7, |
| 41 | | set_up_typed_localstate/6, set_up_typed_localstate2/8, |
| 42 | | %set_up_localstate_for_let/7, |
| 43 | | |
| 44 | | set_projection_on_static_assertions/1, |
| 45 | | |
| 46 | | tcltk_unsatisfiable_components_exist/0, |
| 47 | | tcltk_quick_describe_unsat_properties/2, |
| 48 | | |
| 49 | | get_unsat_component_predicate/3, |
| 50 | | properties_were_filtered/1, % true if PROPERTIES / axioms filtered due to prob-ignore pragmas |
| 51 | | b_generate_for_all_list_domain_nolwf/8 % for predicate_evaluator |
| 52 | | ]). |
| 53 | | |
| 54 | | :- meta_predicate not_with_enum_warning_and_possible_delay(0,-,?,?,?). |
| 55 | | :- meta_predicate not_with_enum_warning_delay(0,-,?,?,?,?). |
| 56 | | |
| 57 | | %:- use_module('../extensions/profiler/profiler.pl'). |
| 58 | | %:- use_module('../extensions/profiler/profiler_te.pl'). |
| 59 | | %:- enable_profiling(b_test_boolean_expression/4). |
| 60 | | %:- enable_profiling(b_test_boolean_expression_cs/5). |
| 61 | | |
| 62 | | :- use_module(kernel_objects). |
| 63 | | :- use_module(bsets_clp). |
| 64 | | :- use_module(delay). |
| 65 | | |
| 66 | | |
| 67 | | :- use_module(kernel_mappings). |
| 68 | | :- use_module(b_global_sets). |
| 69 | | :- use_module(store). |
| 70 | | :- use_module(library(lists)). |
| 71 | | :- use_module(library(ordsets)). |
| 72 | | :- use_module(library(avl)). |
| 73 | | |
| 74 | | :- use_module(b_global_sets). |
| 75 | | :- use_module(bmachine). |
| 76 | | :- use_module(b_enumerate, [b_enumerate_values_in_store/5, |
| 77 | | b_tighter_enumerate_all_values/2, b_tighter_enumerate_values_in_ctxt/3]). |
| 78 | | :- use_module(debug). |
| 79 | | :- use_module(performance_messages). |
| 80 | | :- use_module(self_check). |
| 81 | | :- use_module(tools). |
| 82 | | |
| 83 | | :- use_module(module_information,[module_info/2]). |
| 84 | | :- module_info(group,interpreter). |
| 85 | | :- module_info(description,'This module provides the basic interpreter for expressions, predicates and substitutions.'). |
| 86 | | |
| 87 | | :- use_module(preferences). |
| 88 | | :- use_module(error_manager). |
| 89 | | :- use_module(typechecker). |
| 90 | | :- use_module(specfile,[state_corresponds_to_initialised_b_machine/2 |
| 91 | | ,state_corresponds_to_set_up_constants_only/2]). |
| 92 | | |
| 93 | | :- use_module(bsyntaxtree). |
| 94 | | |
| 95 | | :- use_module(translate). |
| 96 | | |
| 97 | | :- use_module(kernel_waitflags). |
| 98 | | :- use_module(kernel_reals). |
| 99 | | :- use_module(kernel_records). |
| 100 | | |
| 101 | | :- use_module(kodkodsrc(kodkod),[kodkod_request/5]). |
| 102 | | |
| 103 | | :- use_module(custom_explicit_sets). |
| 104 | | :- use_module(closures,[is_symbolic_closure/1, |
| 105 | | mark_closure_as_symbolic/2, mark_closure_as_recursive/2, mark_closure/3]). |
| 106 | | |
| 107 | | :- use_module(state_space). |
| 108 | | :- use_module(b_interpreter_eventb). |
| 109 | | :- use_module(tools_printing,[print_term_summary/1,print_functor/1]). |
| 110 | | :- use_module(value_persistance,[load_partial_constants/3,lookup_cached_transitions/4]). |
| 111 | | :- use_module(extrasrc(b_expression_sharing),[is_lazy_let_identifier/1]). |
| 112 | | :- use_module(bool_pred,[negate/2]). |
| 113 | | :- use_module(smt_solvers_interface(smt_solvers_interface),[smt_add_predicate/5]). |
| 114 | | |
| 115 | | :- use_module(debug). |
| 116 | | :- use_module(external_functions). |
| 117 | | |
| 118 | | :- use_module(static_ordering,[sort_ids_by_usage/4,reorder_state/3]). |
| 119 | | %:- use_module(bsyntaxtree,[predicate_has_ignore_pragma/1]). |
| 120 | | :- use_module(tools_lists,[exclude_count/4, optimized_nth1/3]). |
| 121 | | :- use_module(tools_meta, [safe_time_out/3]). |
| 122 | | |
| 123 | | :- set_prolog_flag(double_quotes, codes). |
| 124 | | |
| 125 | | /* ----------------------------------*/ |
| 126 | | |
| 127 | | /* For partial evaluation */ |
| 128 | | |
| 129 | | %:- use_module('~/cvs_root/cogen2/logen_source/runtime/logen_dispatcher.pl'). |
| 130 | | |
| 131 | | % new profiler |
| 132 | | %:- use_module('../extensions/profiler/profiler.pl'). |
| 133 | | %:- use_module('../extensions/profiler/profiler_te.pl'). |
| 134 | | %:- enable_profiling_naming(b_interpreter:b_state_violates_invariant_aux/2,name). |
| 135 | | |
| 136 | | %name(b_interpreter:b_state_violates_invariant_aux,_,INVERIANT). |
| 137 | | |
| 138 | | %b_state_violates_invariant(ID,State) :- |
| 139 | | % b_intepreter:b_state_violates_invariant_aux(ID,State). |
| 140 | | |
| 141 | | % old profiler |
| 142 | | :- use_module(runtime_profiler,[profile_single_call/3]). |
| 143 | | |
| 144 | | % called by force_check_invariantKO and indirectly by model_checker (State was prepare_state_for_specfile_trans ) |
| 145 | | b_state_violates_invariant(ID,State) :- |
| 146 | | b_state_violates_invariant_aux(ID,State). |
| 147 | | |
| 148 | | b_state_violates_invariant_aux(ID,CurBState) :- |
| 149 | | retract(specialized_inv(ID,BitSetMask)), % Note: when we assert this we checked that invariant holds in predecessor state |
| 150 | | intersect_invs(BitSetMask,SPO), !, |
| 151 | | (SPO = b(truth,_,_) -> fail |
| 152 | | ; profile_single_call('INVARIANT-Specialized',ID,b_interpreter:b_state_violates_spec_invariant(ID,SPO,CurBState))). |
| 153 | | b_state_violates_invariant_aux(ID,CurBState) :- |
| 154 | | profile_single_call('INVARIANT',ID,b_interpreter:state_violates_invariant(ID,CurBState)). |
| 155 | | |
| 156 | | b_state_violates_spec_invariant(ID,SPO,CurBState) :- |
| 157 | | \+ check_invariant_predicate_with_time_out(ID,SPO,CurBState). |
| 158 | | |
| 159 | | :- use_module(probsrc(bit_sets),[member_bitset/2]). |
| 160 | | intersect_invs(invariant_preserved,Inv) :- !, Inv= b(truth,pred,[]). |
| 161 | | intersect_invs(Mask,InvList) :- integer(Mask), !, |
| 162 | | findall(nth1_invariant(Nr), member_bitset(Nr,Mask), InvList). |
| 163 | | intersect_invs(M,Inv) :- !, add_internal_error('Illegal empty specialized inv list:', intersect_invs(M,Inv)), |
| 164 | | Inv = b(falsity,pred,[]). |
| 165 | | |
| 166 | | |
| 167 | | /* ----------------------------------*/ |
| 168 | | |
| 169 | | :- load_files(library(system), [when(compile_time), imports([environ/2])]). |
| 170 | | |
| 171 | | |
| 172 | | state_satisfies_invariant_aux(ID,State) :- |
| 173 | | check_invariant_with_time_out(ID,State). % version without partial evaluation |
| 174 | | |
| 175 | | |
| 176 | | :- use_module(library(timeout)). |
| 177 | | |
| 178 | | :- use_module(tools_printing,[format_with_colour_nl/4]). |
| 179 | | :- use_module(probsrc(specfile),[prepare_state_for_specfile_trans/3]). |
| 180 | | % called by prob_cli when invariant violation found: |
| 181 | | % we no longer have access to the specialized_inv facts |
| 182 | | analyse_invariant_for_state(ID) :- |
| 183 | | b_invariant_number_list(InvList), |
| 184 | | visited_expression(ID,State), |
| 185 | | prepare_state_for_specfile_trans(State,ID,PreparedCurState), |
| 186 | | maplist(check_invariant(ID,PreparedCurState),InvList). |
| 187 | | check_invariant(ID,State,InvariantNr) :- |
| 188 | | enter_new_error_scope(L,check_invariant), |
| 189 | | check_invariant_predicate_with_time_out_res(InvariantNr,State,TimeOutRes), |
| 190 | | (TimeOutRes=success -> true |
| 191 | | ; InvariantNr = nth1_invariant(Nr), |
| 192 | | b_nth1_invariant(Nr,Invariant,_UsedIds), |
| 193 | | translate_bexpression_with_limit(Invariant,TS), |
| 194 | | translate_span(Invariant,SpanStr), |
| 195 | | (TimeOutRes=failure -> InvRes=false ; InvRes=unknown), |
| 196 | | format_with_colour_nl(user_output,[red,bold], |
| 197 | | '*** Invariant ~w ~w is ~w (in state with id ~w):~n ~w',[Nr,SpanStr,InvRes,ID,TS]), |
| 198 | | % add_message(check_invariant,'Invariant false: ',Nr,TS), |
| 199 | | (debug_mode(off) -> true |
| 200 | | ; translate:print_bstate(State),nl |
| 201 | | ) |
| 202 | | % TODO: pass check dot_file_prefix or similar to analyse_invariant_for_state and call: |
| 203 | | % bvisual:get_tree_from_expr(Tree, Invariant, [], State), |
| 204 | | % bvisual:write_dot_graph_to_file(Tree,'invariant.dot') |
| 205 | | ), exit_error_scope(L,_ErrOcc,check_invariant). |
| 206 | | |
| 207 | | |
| 208 | | |
| 209 | | check_invariant_with_time_out(ID,State) :- |
| 210 | | %b_get_invariant_from_machine(Invariant), |
| 211 | | b_invariant_number_list(InvList), % ensures operation caching can be applied |
| 212 | | % TO DO: we could just get unproven invariants; unless we are in an initial state |
| 213 | | check_invariant_predicate_with_time_out(ID,InvList,State). |
| 214 | | |
| 215 | | |
| 216 | | check_invariant_predicate_with_time_out(ID,Invariant,State) :- |
| 217 | | (check_invariant_predicate_with_time_out_res(Invariant,State,TimeOutRes) |
| 218 | | -> check_time_out_res(TimeOutRes,ID)). |
| 219 | | |
| 220 | | check_invariant_predicate_with_time_out_res(b(truth,_,_),_State,TimeOutRes) :- !, % only typing in invariant |
| 221 | | TimeOutRes = success. |
| 222 | | check_invariant_predicate_with_time_out_res(Invariant,State,TimeOutRes) :- |
| 223 | | set_error_context(checking_invariant), |
| 224 | | preferences:get_computed_preference(debug_time_out,DebugTimeOut), |
| 225 | | call_cleanup( |
| 226 | | (time_out_with_enum_warning_one_solution_no_new_error_scope( |
| 227 | | b_interpreter:check_invariant_predicate_aux(Invariant,State ), |
| 228 | | DebugTimeOut,TimeOutRes) |
| 229 | | -> true |
| 230 | | ; TimeOutRes = failure), |
| 231 | | clear_error_context). |
| 232 | | |
| 233 | | % process typed predicates, lists and special invariant(Nr) terms |
| 234 | | check_invariant_predicate_aux(nth1_invariant(Nr),State) :- !, |
| 235 | | ((get_preference(operation_reuse_setting,false) ; |
| 236 | | get_preference(try_operation_reuse_for_invariants,false)) |
| 237 | | -> check_nth1_invariant(Nr,State) |
| 238 | | ; \+ b_operation_cache:check_invariant_violated_cache(Nr,State) % will call check_nth1_invariant if necessary |
| 239 | | % note : for performance it is important that the State is prepared but not expanded |
| 240 | | % (e.g. it should be expanded_const_and_vars with the info field containing the packed state) |
| 241 | | ). |
| 242 | | check_invariant_predicate_aux([],_) :- !. |
| 243 | | check_invariant_predicate_aux([H|T],State) :- !, |
| 244 | | check_invariant_predicate_aux(H,State), |
| 245 | | check_invariant_predicate_aux(T,State). |
| 246 | | check_invariant_predicate_aux(Invariant,State) :- |
| 247 | | b_test_boolean_expression_for_ground_state(Invariant,[],State,'INVARIANT',0). |
| 248 | | |
| 249 | | % check the nth invariant |
| 250 | | check_nth1_invariant(Nr,State) :- |
| 251 | | if(b_nth1_invariant(Nr,Invariant,_UsedIds), |
| 252 | ? | b_test_boolean_expression_for_ground_state(Invariant,[],State,'INVARIANT',Nr), % add Nr to call_stack |
| 253 | | add_internal_error('Invariant does not exist:',Nr)). |
| 254 | | |
| 255 | | |
| 256 | | check_time_out_res(time_out,ID) :- !, |
| 257 | | format('**** TIMEOUT during invariant evaluation in state ~w!~n',[ID]), |
| 258 | | assert_time_out_for_invariant(ID), |
| 259 | | fail. |
| 260 | | check_time_out_res(virtual_time_out(_WARNING),ID) :- !, |
| 261 | | format('**** VIRTUAL TIMEOUT during invariant evaluation in state ~w!~n',[ID]), |
| 262 | | assert_time_out_for_invariant(ID), |
| 263 | | fail. |
| 264 | | check_time_out_res(failure,_) :- !, fail. |
| 265 | | check_time_out_res(_,_). |
| 266 | | |
| 267 | | % b_test_boolean_expression_for_ground_state: |
| 268 | | % idea: check each conjunct individually; avoids multiplication of choice points between conjuncts |
| 269 | | |
| 270 | | :- use_module(specfile,[expand_const_and_vars_to_full_store/2]). |
| 271 | | b_test_boolean_expression_for_ground_state(b(Expr,_,Infos),LS,S,PredKind) :- |
| 272 | | b_test_boolean_expression_for_ground_state(b(Expr,_,Infos),LS,S,PredKind,0). |
| 273 | | b_test_boolean_expression_for_ground_state(b(Expr,_,Infos),LS,S,PredKind,Nr) :- |
| 274 | | expand_const_and_vars_to_full_store(S,FS), % ensure we have full store |
| 275 | ? | b_test_boolean_expression_for_ground_state2(Expr,Infos,LS,FS,PredKind,Nr). |
| 276 | | |
| 277 | | b_test_boolean_expression_for_ground_state1(b(Expr,_,Infos),LS,S,Kind,Nr) :- !, |
| 278 | ? | b_test_boolean_expression_for_ground_state2(Expr,Infos,LS,S,Kind,Nr). |
| 279 | | |
| 280 | | b_test_boolean_expression_for_ground_state2(conjunct(LHS,RHS),_,LocalState,State,Kind,Nr) :- !, |
| 281 | | % usually LHS is the nested conjunct and RHS atomic: |
| 282 | | %it would be better to swap LHS and RHS: but does not seem to buy anything |
| 283 | ? | b_test_boolean_expression_for_ground_state1(LHS,LocalState,State,Kind,Nr), |
| 284 | | !, % do we need this cut; it makes partial evaluation more tricky |
| 285 | ? | b_test_boolean_expression_for_ground_state1(RHS,LocalState,State,Kind,Nr). |
| 286 | | % TO DO: treate lazy_let_pred ? |
| 287 | | b_test_boolean_expression_for_ground_state2(BE,Infos,LS,S,Kind,Nr) :- |
| 288 | ? | b_test_boolean_expression_cs(b(BE,pred,Infos),LS,S,Kind,Nr). |
| 289 | | |
| 290 | | state_violates_invariant(ID,State) :- |
| 291 | | State \== root, |
| 292 | | State \= concrete_constants(_), % machine not yet initialised (cf state_corresponds_to_initialised_b_machine) |
| 293 | | \+(state_satisfies_invariant_aux(ID,State)). |
| 294 | | |
| 295 | | |
| 296 | | % new profiler |
| 297 | | %:- use_module('../extensions/profiler/profiler.pl'). |
| 298 | | %:- use_module('../extensions/profiler/profiler_te.pl'). |
| 299 | | %:- enable_profiling_naming(b_interpreter:state_violates_assertions_aux/2,name2). |
| 300 | | |
| 301 | | %name2(b_interpreter:state_violates_assertions_aux,_,ASSERTIONS). |
| 302 | | |
| 303 | | %state_violates_assertions(ID,State) :- |
| 304 | | % b_interpreter:state_violates_assertions_aux(ID,State). |
| 305 | | |
| 306 | | :- use_module(bmachine,[get_assertions_from_machine/2, b_machine_has_dynamic_assertions/0, b_machine_has_static_assertions/0]). |
| 307 | | state_violates_assertions(ID,State) :- |
| 308 | | profile_single_call('ASSERTIONS',ID,b_interpreter:state_violates_assertions_aux(ID,State)). |
| 309 | | state_violates_assertions_aux(ID,State) :- |
| 310 | | b_machine_has_dynamic_assertions, |
| 311 | | state_corresponds_to_initialised_b_machine(State,BState), !, |
| 312 | | \+(state_satisfies_assertions(ID,dynamic,BState)). |
| 313 | | state_violates_assertions_aux(ID,State) :- |
| 314 | | b_machine_has_static_assertions, |
| 315 | | state_corresponds_to_set_up_constants_only(State,BState), |
| 316 | | \+(state_satisfies_assertions(ID,static,BState)). |
| 317 | | state_satisfies_assertions(ID,Type,State) :- |
| 318 | | ( get_assertions_from_machine(Type,Ass) -> |
| 319 | | set_error_context(checking_assertions), |
| 320 | | %enter_new_error_scope(Level,checking_assertions), |
| 321 | | preferences:get_computed_preference(debug_time_out,DebugTimeOut), |
| 322 | | %print(checking_assertions(ID)),nl, |
| 323 | | time_out_with_enum_warning_one_solution( |
| 324 | | b_test_list_of_boolean_expression_for_ground_state(Ass,'ASSERTION ',[],State), |
| 325 | | DebugTimeOut,TimeOutRes, clear_errors), |
| 326 | | % TO DO: in case of a virtual time-out (cf UML-B/drone_2_error.eventb) we could try and continue with the next assertion |
| 327 | | clear_error_context, |
| 328 | | (nonvar(TimeOutRes), |
| 329 | | (TimeOutRes == time_out, Kind='TIMEOUT' ; TimeOutRes = virtual_time_out(_), Kind = 'Virtual TIMEOUT') |
| 330 | | -> format('~n*** ~w during assertion checking for state id ~w!~n',[Kind,ID]), |
| 331 | | assert_time_out_for_assertions(ID) |
| 332 | | ; true |
| 333 | | ) |
| 334 | | ; true |
| 335 | | ). |
| 336 | | |
| 337 | | |
| 338 | | |
| 339 | | /* ----------------------------------*/ |
| 340 | | /* b_test_boolean_expression */ |
| 341 | | /* ----------------------------------*/ |
| 342 | | |
| 343 | | |
| 344 | | :- type variable_id_list +--> list(type(variable_id)). |
| 345 | | |
| 346 | | :- type boolean_expression +--> call(bsyntaxtree:check_if_typed_predicate). |
| 347 | | |
| 348 | | % used for testing ASSERTIONS on a fully ground state |
| 349 | | b_test_list_of_boolean_expression_for_ground_state(List,PredKind,LS,S) :- |
| 350 | | b_test_list_of_boolean_expression_aux(List,PredKind,1,LS,S). |
| 351 | | |
| 352 | | b_test_list_of_boolean_expression_aux([],_,_,_,_). |
| 353 | | b_test_list_of_boolean_expression_aux([B1|T],PredKind,Nr,LS,S) :- %nl,print(nr(PredKind,Nr)),nl, |
| 354 | | (b_test_boolean_expression_for_ground_state1(B1,LS,S,PredKind,Nr) |
| 355 | | -> N1 is Nr+1, b_test_list_of_boolean_expression_aux(T,PredKind,N1,LS,S) |
| 356 | | ; silent_mode(off), |
| 357 | | (critical_enumeration_warning_occured_in_error_scope -> Res='UNKNOWN' |
| 358 | | ; abort_error_occured_in_error_scope -> Res ='NOT-WELL-DEFINED' |
| 359 | | ; Res='false'), |
| 360 | | ajoin([PredKind, Nr, ' is ', Res,': '],Msg), |
| 361 | | add_message(b_interpreter,Msg,B1,B1), % TO DO: get just label/location? |
| 362 | | fail). |
| 363 | | |
| 364 | | |
| 365 | | :- use_module(b_interpreter_components). |
| 366 | | |
| 367 | | |
| 368 | | :- assert_pre(b_interpreter:b_test_boolean_expression_cs(E,LS,S,_,_), |
| 369 | | (bsyntaxtree:check_if_typed_predicate(E),type_check(LS,store),type_check(S,store) )). |
| 370 | | :- assert_post(b_interpreter:b_test_boolean_expression_cs(_,_,_,_,_), true ). |
| 371 | | |
| 372 | | |
| 373 | | % --------------------- BOOLEAN EXPRESSIONS ---------------------------- |
| 374 | | |
| 375 | | :- use_module(b_interpreter_check). |
| 376 | | :- use_module(kernel_propagation, [do_not_enumerate_binary_boolean_operator/3]). |
| 377 | | |
| 378 | | % a version where we can provide an initial call stack entry for context |
| 379 | | b_test_boolean_expression_cs(E,LS,S,PredKind,Nr) :- |
| 380 | | init_wait_flags_cs(E,PredKind,Nr,b_test_boolean_expression_cs,WF), |
| 381 | | % TO DO: call b_trace_test_components ? |
| 382 | ? | b_test_boolean_expression(E,LS,S,WF), |
| 383 | ? | ground_wait_flags(WF). |
| 384 | | |
| 385 | | init_wait_flags_cs(E,PredKind,Nr,PP,WF) :- |
| 386 | | (PredKind=none -> init_wait_flags(WF,[PP]) |
| 387 | | ; get_texpr_pos(E,Pos), |
| 388 | | CallStackEntry = prob_command_context(check_pred_command(PredKind,Nr),Pos), |
| 389 | | init_wait_flags_and_push_call_stack(no_wf_available,CallStackEntry,WF) |
| 390 | | ). |
| 391 | | |
| 392 | | |
| 393 | | b_test_boolean_expression(b(Expr,_,Infos),LS,S,WF) :- !, |
| 394 | | (b_interpreter_check:composed(Expr) -> empty_avl(Ai) |
| 395 | | ; Ai = no_avl), % simple expression: no sharing is possible: no need to register expressions |
| 396 | ? | b_test_boolean_expression2(Expr,Infos,LS,S,WF,Ai,_). |
| 397 | | b_test_boolean_expression(E,LS,S,WF) :- % will generate error message |
| 398 | | empty_avl(Ai), b_test_boolean_expression(E,LS,S,WF,Ai,_). |
| 399 | | |
| 400 | | :- assert_pre(b_interpreter:b_test_boolean_expression(E,LS,S,WF), |
| 401 | | (nonvar(E),bsyntaxtree:check_if_typed_predicate(E),type_check(LS,store),type_check(S,store), |
| 402 | | type_check(WF,wait_flag))). |
| 403 | | :- assert_post(b_interpreter:b_test_boolean_expression(_,_,_,WF), type_check(WF,wait_flag)). |
| 404 | | |
| 405 | | |
| 406 | | %b_test_boolean_expression(Expr,_,_,_) :- print('test: '),translate:print_bexpr(Expr),nl,print(Expr),nl,fail. %% |
| 407 | | |
| 408 | | :- load_files(library(system), [when(compile_time), imports([environ/2])]). |
| 409 | | :- if(environ(prob_debug_watch_flag,true)). |
| 410 | | b_test_boolean_expressiond(b(Expr,_,Infos),LS,S,WF,Ai,Ao) :- !, |
| 411 | | b_test_boolean_expression2(Expr,Infos,LS,S,WF,Ai,Ao). |
| 412 | | b_test_boolean_expression(Expr,LS,S,WF,Ai,Ao) :- !, |
| 413 | | (waitflag0_is_set(WF) |
| 414 | | -> debug:watch(300,b_interpreter:b_test_boolean_expressiond(Expr,LS,S,WF,Ai,Ao)) |
| 415 | | ; debug:watch_det(300,b_interpreter:b_test_boolean_expressiond(Expr,LS,S,WF,Ai,Ao)) ). |
| 416 | | :- else. |
| 417 | | b_test_boolean_expression(b(Expr,Type,Infos),LS,S,WF,Ai,Ao) :- !, |
| 418 | | (preference(smt_supported_interpreter,true) |
| 419 | | -> b_test_boolean_expression2(Expr,Infos,LS,S,WF,Ai,Ao), |
| 420 | | get_wait_flag1(smt_call,WF,BeforeEnumWF), |
| 421 | | gensym:gensym(smt_assertion_name,Symbol), |
| 422 | | smt_add_predicate(BeforeEnumWF,b(Expr,Type,Infos),LS,S,Symbol) |
| 423 | | ; |
| 424 | ? | b_test_boolean_expression2(Expr,Infos,LS,S,WF,Ai,Ao)). |
| 425 | | :- endif. |
| 426 | | b_test_boolean_expression(E,LS,S,WF,Ai,Ao) :- |
| 427 | | add_internal_error('Boolean expression not properly wrapped: ', |
| 428 | | b_test_boolean_expression(E,LS,S,WF,Ai,Ao)), |
| 429 | | b_test_boolean_expression2(E,[],LS,S,WF,Ai,Ao). |
| 430 | | |
| 431 | | |
| 432 | | b_test_boolean_expression2(truth,_,_,_,_WF,Ai,Ao) :- !,Ai=Ao. |
| 433 | | b_test_boolean_expression2(falsity,_,_,_,_WF,_Ai,_Ao) :- !,fail. |
| 434 | | b_test_boolean_expression2(negation(BExpr),_,LocalState,State,WF,Ai,Ao) :- !, |
| 435 | ? | b_not_test_boolean_expression(BExpr,LocalState,State,WF,Ai,Ao). |
| 436 | | b_test_boolean_expression2(conjunct(LHS,RHS),_,LocalState,State,WF,Ai,Ao) :- !, |
| 437 | ? | b_test_boolean_expression(LHS,LocalState,State,WF,Ai,Aii), |
| 438 | ? | b_test_boolean_expression(RHS,LocalState,State,WF,Aii,Ao). |
| 439 | | b_test_boolean_expression2(implication(LHS,RHS),Info,LocalState,State,WF,Ai,Ao) :- |
| 440 | | %b_compiler:b_compile(LHS,[],LocalState,State,CLHS), % this slows down Cruise benchmark from 3.75 s to 4.3 |
| 441 | | !, |
| 442 | ? | b_test_implication(LHS,RHS,Info,LocalState,State,WF,Ai,Ao). |
| 443 | | b_test_boolean_expression2(equivalence(LHS,RHS),_,LocalState,State,WF,Ai,Ao) :- !, |
| 444 | ? | b_test_equivalence(LHS,RHS,LocalState,State,WF,Ai,Ao). |
| 445 | | b_test_boolean_expression2(disjunct(LHS,RHS),Info,LocalState,State,WF,Ai,Ao) :- !, |
| 446 | | % TO DO: normalise disjunct first ? |
| 447 | | copy_wf_start(WF,disjunct,CWF), |
| 448 | | if(b_check_boolean_expression(LHS,LocalState,State,CWF,PredRes,Ai,Aii), |
| 449 | | % we can check lhs |
| 450 | | if(b_interpreter_check:b_check_boolean_expression0(pred_false,PredRes,RHS,LocalState,State,CWF,RR,Aii,Ao), |
| 451 | | ( /* we can also check rhs */ |
| 452 | ? | b_interpreter_check:disjoin(PredRes,RR,pred_true,LHS,RHS,WF), |
| 453 | | copy_wf_finish(WF,CWF) |
| 454 | | ), |
| 455 | | ( |
| 456 | | Ao=Aii, |
| 457 | | copy_wf_finish(WF,CWF), |
| 458 | | imply_test_boolean_expression(PredRes,pred_false,RHS,LocalState,State,WF,Aii), |
| 459 | | perfmessagecall(choice_point,reification_of_disjunction_rhs_failed,translate:print_bexpr(RHS),Info), |
| 460 | | enumerate_bool(PredRes,15,WF) |
| 461 | | ) |
| 462 | | ), |
| 463 | | % we cannot check lhs |
| 464 | | if((always_well_defined(RHS), |
| 465 | | b_check_boolean_expression(RHS,LocalState,State,CWF,PredRes,Ai,Aii)), |
| 466 | | ( |
| 467 | | Ao=Aii, |
| 468 | | copy_wf_finish(WF,CWF), |
| 469 | | imply_test_boolean_expression(PredRes,pred_false,LHS,LocalState,State,WF,Aii), |
| 470 | | perfmessagecall(choice_point,reification_of_disjunction_lhs_failed,translate:print_bexpr(LHS),Info), |
| 471 | | enumerate_bool(PredRes,15,WF) |
| 472 | | ), |
| 473 | | % we cannot check neither lhs nor rhs: |
| 474 | | (Ai=Ao, |
| 475 | | get_priority_of_boolean_expression2(disjunct(LHS,RHS),StartPrio), |
| 476 | | %get_wait_flag(Prio,disjunct,WF,WF2), |
| 477 | | get_binary_choice_wait_flag_exp_backoff(StartPrio,disjunct,WF,WF2), |
| 478 | | % be sure that e.g. for (x=2 or x=3) both branches will be tested once deterministic propagation is over |
| 479 | | %print(prio(Prio)), print(' : '),print_bexpr(LHS), print(' or '), print_bexpr(RHS),nl, |
| 480 | | perfmessagecall(choice_point,reification_of_disjunction_failed,translate:print_bexpr(b(disjunct(LHS,RHS),pred,Info)),Info), |
| 481 | | b_test_disjunction(LHS,RHS,LocalState,State,WF,WF2,Ai) |
| 482 | | ) |
| 483 | | ) |
| 484 | | ). |
| 485 | | b_test_boolean_expression2(exists(Parameters,RHS),Infos,LocalState,State,WF,Ai,Ao) :- !,Ai=Ao, |
| 486 | | b_test_exists(Parameters,RHS,Infos,LocalState,State,WF). |
| 487 | | %b_test_boolean_expression2(forall(PARS,LHS,RHS),_Inf,LocalState,State,WF) :- |
| 488 | | % PARS = [b(identifier(R1),integer,_),b(identifier(R2),integer,_)], |
| 489 | | % LHS = b(conjunct(b(conjunct(MEM1,MEM2),pred,_I1),NEQ),pred,_I2), |
| 490 | | % NEQ = b(not_equal(b(identifier(RR1),integer,_I3),b(identifier(RR2),integer,_I4)),pred,_I5), |
| 491 | | % (RR1=R1, RR2=R2 ; RR1=R2, RR2=R1), |
| 492 | | % MEM1 = b(member(b(identifier(RRR1),integer,_),b(SET,set(integer),_)),pred,_), |
| 493 | | % MEM2 = b(member(b(identifier(RRR2),integer,_),b(SET,set(integer),_)),pred,_), |
| 494 | | % (RRR1=R1, RRR2=R2 ; RRR1=R2, RRR2=R1), |
| 495 | | % /* TO DO: check that RHS is symmetric ! */ /* TO DO : generalize symmetry breaking */ |
| 496 | | % !, % print(symmetry_breaking_forall(R1,R2)),nl, |
| 497 | | % GT = b(less(b(identifier(R1),integer,_I3),b(identifier(R2),integer,_I4)),pred,_I5), |
| 498 | | % NewLHS = b(conjunct(b(conjunct(MEM1,MEM2),pred,_I1),GT),pred,_I2), |
| 499 | | % b_test_boolean_expression2(forall(PARS,NewLHS,RHS),_Inf,LocalState,State,WF). |
| 500 | | b_test_boolean_expression2(forall(Parameters,LHS,RHS),Infos,LocalState,State,WF,Ai,Ao) :- !,Ai=Ao, |
| 501 | | /* !ID.(LHS => RHS) */ |
| 502 | | % print_message(forall(Parameters,LHS,RHS)), |
| 503 | | % get_wait_flag0(WF,WF0), when(nonvar(WF0), |
| 504 | | b_for_all(Parameters,Infos,LHS,RHS,LocalState,State,WF). %). |
| 505 | | b_test_boolean_expression2(let_predicate(Ids,AssignmentExprs,Pred),Infos,LocalState,State,WF,Ai,Ao) :- !, |
| 506 | | Ai=Ao, % any expression inside the LET cannot be safely reused |
| 507 | | (is_truth(Pred) |
| 508 | | -> % useless let, only sense of computations below is in detecting WD errors |
| 509 | | get_enumeration_starting_wait_flag(useless_let_predicate(Ids),WF,ESWF), |
| 510 | | when(nonvar(ESWF),set_up_localstate_for_let(Ids,_,AssignmentExprs,LocalState,State,_LetState,WF)) |
| 511 | | ; set_up_localstate_for_let(Ids,ParaValues,AssignmentExprs,LocalState,State,LetState,WF), |
| 512 | | opt_push_wait_flag_call_stack_quantifier_info(WF,let_quantifier,Ids,ParaValues,Infos,WF2), |
| 513 | | b_test_boolean_expression(Pred,LetState,State,WF2)). % we could pass Ai in if Ids fresh |
| 514 | | |
| 515 | | b_test_boolean_expression2(lazy_let_pred(Id,AssignmentExpr,Expr),_I,LocalState,State,WF,Ai,Ao) :- !, |
| 516 | | add_lazy_let_id_and_expression(Id,AssignmentExpr,LocalState,State,NewLocalState,WF,Ai), |
| 517 | | b_test_boolean_expression(Expr,NewLocalState,State,WF,Ai,Ao). % we could use Ai,Ao version as lazy lets never interfere with each other and never invalidate existing expressions |
| 518 | | b_test_boolean_expression2(lazy_lookup_pred(Id),_Info,LocalState,_State,WF,Ai,Ao) :- !, Ai=Ao, |
| 519 | | lookup_value_for_existing_id_wf(Id,LocalState,(Trigger,Value),WF), % should normally only occur in LocalState; value introduced by lazy_let |
| 520 | | (Trigger,Value) = (pred_true,pred_true). % force evaluation |
| 521 | | b_test_boolean_expression2(value(PredVal),_Info,_LocalState,_State,_WF,Ai,Ao) :- !, % this can occur when lazy_lookup_pred gets compiled by b_compiler |
| 522 | | is_pred_true(PredVal),Ai=Ao. |
| 523 | | b_test_boolean_expression2(Pred,Info,LocalState,State,WF,Ai,Ao) :- |
| 524 | | % TO DO: we could put some of the operators into the Ai store (comparison operators) |
| 525 | | b_interpreter_check:register_predicate(Pred,Info,pred_true,Reused,Ai,Ao), |
| 526 | | (Reused==true |
| 527 | | -> %% print('REUSED: '), translate:print_bexpr(Pred),nl |
| 528 | | true |
| 529 | ? | ; b_test_atomic_boolean_expression2(Pred,Info,LocalState,State,WF)). |
| 530 | | |
| 531 | | :- block is_pred_true(-). |
| 532 | | is_pred_true(X) :- (X=pred_true -> true ; X=pred_false -> fail ; add_internal_error('Illegal call: ',is_pred_true(X)),fail). |
| 533 | | :- block is_pred_false(-). |
| 534 | | is_pred_false(X) :- (X=pred_false -> true ; X=pred_true -> fail ; add_internal_error('Illegal call: ',is_pred_false(X)),fail). |
| 535 | | |
| 536 | | |
| 537 | | % we could simply call disjunct(NegLHS,RHS) ? |
| 538 | | % A => B has more of a feeling of a passive constraint than A or B |
| 539 | | % hence we use a lower priority here for enumerate_bool below and do not |
| 540 | | % try to reify rhs if lhs reification failed (but maybe we should) |
| 541 | | b_test_implication(LHS,RHS,_Info,LocalState,State,WF,Ai,Ao) :- |
| 542 | | copy_wf_start(WF,implication,CWF), |
| 543 | ? | if(b_check_boolean_expression(LHS,LocalState,State,CWF,PredRes,Ai,Aii), |
| 544 | | %% print('checking => '), print_bexpr(LHS), print(' '), print(PredRes),nl, |
| 545 | | %% The following improves constraint propagation; but can be tricky with undefinedness |
| 546 | | if((var(PredRes),always_well_defined(RHS), |
| 547 | | b_check_boolean_expression(RHS,LocalState,State,CWF,RHSPredRes,Aii,Ao)), |
| 548 | | (/* also propagate from right to left */ |
| 549 | ? | b_interpreter_check:imply_true(PredRes,RHSPredRes), |
| 550 | | perfmessage_bexpr(good(reify),'Fully reified implication with LHS: ',LHS), |
| 551 | | copy_wf_finish(WF,CWF) |
| 552 | | ), |
| 553 | | ( Aii=Ao, |
| 554 | | copy_wf_finish(WF,CWF), |
| 555 | ? | imply_test_boolean_expression(PredRes,pred_true,RHS,LocalState,State,WF,Aii), |
| 556 | | (nonvar(PredRes) -> true |
| 557 | | ; performance_monitoring_on, |
| 558 | | \+always_well_defined(RHS) -> perfmessage_bexpr(reify,'Cannot reify RHS of => due to WD condition, creating choice point:',RHS) |
| 559 | | ; perfmessage_bexpr(reify,'Cannot reify RHS of =>, creating choice point:',RHS) |
| 560 | | ), |
| 561 | | enumerate_bool(PredRes,10000,WF) % lower priority than disjunction (15); we could use last_finite_priority ? |
| 562 | | ) |
| 563 | | ), |
| 564 | | ( Ai=Ao, |
| 565 | | perfmessage_bexpr(reify,'Cannot reify LHS of =>, creating binary choice point:',LHS), |
| 566 | | get_binary_choice_wait_flag(implication,WF,WF2), |
| 567 | | block_test_implication(LHS,RHS,LocalState,State,WF,WF2) |
| 568 | | ) |
| 569 | | ). |
| 570 | | |
| 571 | | % ------------ |
| 572 | | |
| 573 | | :- discontiguous b_test_atomic_boolean_expression2/5. |
| 574 | | %b_test_atomic_boolean_expression2(Expr,T,LocalState,State,WF) :- print(eval(Expr,T)),nl,fail. |
| 575 | | b_test_atomic_boolean_expression2(partition(LHS,RHS),_,LocalState,State,WF) :- !, |
| 576 | | b_compute_expression(LHS,LocalState,State,SV1,WF), %print(lhs(SV1)),nl, |
| 577 | | l_compute_expression(RHS,LocalState,State,SV2,WF), % RHS is Prolog list of expressions |
| 578 | ? | kernel_call_predicate(partition_wf(SV1,SV2,WF),kernel_objects,WF,partition(LHS,RHS)). |
| 579 | | b_test_atomic_boolean_expression2(member(TElem,TSet),_,LocalState,State,WF) :- !, |
| 580 | ? | b_test_member_expression(TElem,TSet,LocalState,State,WF). |
| 581 | | b_test_atomic_boolean_expression2(not_member(TElem,TSet),Info,LocalState,State,WF) :- !, |
| 582 | | get_texpr_expr(TSet,Set), |
| 583 | ? | b_test_notmember_expression(Set,TElem,TSet,Info,LocalState,State,WF). |
| 584 | | b_test_atomic_boolean_expression2(freetype_case(Freetype,Case,Expr),_,LocalState,State,WF) :- |
| 585 | | !,equal_object_wf(Value,freeval(Freetype,Case,_),b_test_atomic_boolean_expression2,WF), |
| 586 | | b_compute_expression(Expr,LocalState,State,Value,WF). % NOT COVERED (16) |
| 587 | | b_test_atomic_boolean_expression2(finite(SetExpr),_,LocalState,State,WF) :- |
| 588 | | !, b_compute_expression(SetExpr,LocalState,State,Value,WF), |
| 589 | | kernel_call_predicate(is_finite_set_wf(Value,WF),kernel_objects,WF,finite(SetExpr)). |
| 590 | | /* Extension for Kodkod */ |
| 591 | | b_test_atomic_boolean_expression2(kodkod(ProblemId,_),_,LocalState,State,WF) :- |
| 592 | | !,get_wait_flag1(kodkod_call,WF,WF0), |
| 593 | | kodkod_request(ProblemId,pos,LocalState,State,WF0). % NOT COVERED (17) |
| 594 | | %b_test_atomic_boolean_expression2(equal(Arg1,Arg2),_,LocalState,State,WF) :- get_texpr_type(Arg1,integer),!, |
| 595 | | % b_compute_expression(Arg1,LocalState,State,ExprRes,WF), |
| 596 | | % b_compute_expression(Arg2,LocalState,State,ExprRes,WF). /* avoids one extra variable compared to code below + could fail earlier ; but problem as b_compute_expression still uses = in some places */ |
| 597 | | |
| 598 | | :- include(b_arithmetic_expressions). |
| 599 | | %The following clause improves Nqueens performance by about 50 %; by avoiding intermediate variables |
| 600 | | % TO DO: do the same for >, <, >= ... |
| 601 | | b_test_atomic_boolean_expression2(equal(Arg1,Arg2),Info,LocalState,State,WF) :- !, |
| 602 | ? | b_test_atomic_equal_boolean_expression(Arg1,Arg2,Info,LocalState,State,WF). |
| 603 | | b_test_atomic_boolean_expression2(not_equal(Arg1,Arg2),Info,LocalState,State,WF) :- !, |
| 604 | | b_test_atomic_not_equal_boolean_expression(Arg1,Arg2,Info,LocalState,State,WF). |
| 605 | | b_test_atomic_boolean_expression2(less_equal(Arg1,Arg2),_Info,LocalState,State,WF) :- |
| 606 | | %get_texpr_type(Arg1,integer), % type checker already checks this |
| 607 | | preferences:preference(use_clpfd_solver,true), |
| 608 | | !, |
| 609 | ? | b_compute_arith_expression(Arg1,LocalState,State,CLPFD_Expr1,WF), |
| 610 | ? | b_compute_arith_expression(Arg2,LocalState,State,CLPFD_Expr2,WF), |
| 611 | | clpfd_interface:clpfd_leq_expr(CLPFD_Expr1,CLPFD_Expr2). |
| 612 | | b_test_atomic_boolean_expression2(greater_equal(Arg1,Arg2),_Info,LocalState,State,WF) :- |
| 613 | | %get_texpr_type(Arg1,integer), % type checker already checks this |
| 614 | | preferences:preference(use_clpfd_solver,true), |
| 615 | | !, |
| 616 | ? | b_compute_arith_expression(Arg1,LocalState,State,CLPFD_Expr1,WF), |
| 617 | ? | b_compute_arith_expression(Arg2,LocalState,State,CLPFD_Expr2,WF), |
| 618 | | clpfd_interface:clpfd_leq_expr(CLPFD_Expr2,CLPFD_Expr1). |
| 619 | | b_test_atomic_boolean_expression2(less(Arg1,Arg2),_Info,LocalState,State,WF) :- |
| 620 | | %get_texpr_type(Arg1,integer), % type checker already checks this |
| 621 | | preferences:preference(use_clpfd_solver,true), |
| 622 | | !, |
| 623 | ? | b_compute_arith_expression(Arg1,LocalState,State,CLPFD_Expr1,WF), |
| 624 | ? | b_compute_arith_expression(Arg2,LocalState,State,CLPFD_Expr2,WF), |
| 625 | | clpfd_interface:clpfd_lt_expr(CLPFD_Expr1,CLPFD_Expr2). |
| 626 | | b_test_atomic_boolean_expression2(greater(Arg1,Arg2),_Info,LocalState,State,WF) :- |
| 627 | | %get_texpr_type(Arg1,integer), % type checker already checks this |
| 628 | | preferences:preference(use_clpfd_solver,true), |
| 629 | | !, |
| 630 | ? | b_compute_arith_expression(Arg1,LocalState,State,CLPFD_Expr1,WF), |
| 631 | | b_compute_arith_expression(Arg2,LocalState,State,CLPFD_Expr2,WF), |
| 632 | | clpfd_interface:clpfd_lt_expr(CLPFD_Expr2,CLPFD_Expr1). |
| 633 | | b_test_atomic_boolean_expression2(external_pred_call(FunName,Args),Info,LocalState,State,WF) :- |
| 634 | | !, |
| 635 | | (do_not_evaluate_args(FunName) -> EvaluatedArgs=[] |
| 636 | | ; b_compute_expressions(Args, LocalState,State, EvaluatedArgs, WF)), |
| 637 | | push_wait_flag_call_stack_info(WF,external_call(FunName,EvaluatedArgs,Info),WF2), |
| 638 | | call_external_predicate(FunName,Args,EvaluatedArgs,LocalState,State,pred_true,Info,WF2). |
| 639 | | b_test_atomic_boolean_expression2(Expression,Info,LocalState,State,WF) :- |
| 640 | | preferences:preference(use_clpfd_solver,false), |
| 641 | | special_binary_predicate(Expression,Module:Kernel_predicate,Arg1,T1,Arg2,T2), |
| 642 | | (nonmember(contains_wd_condition,Info) -> true ; preference(disprover_mode,true)), |
| 643 | | !, |
| 644 | | % print('Special call: '), print_bexpr(Expression),nl, print(Kernel_predicate),print(' : '),nl, |
| 645 | | %(Arg1==list_of_expressions -> b_compute_expressions ... % no longer needed |
| 646 | | b_compute_expression(Arg1,LocalState,State,SV1,WF), |
| 647 | | b_compute_expression(Arg2,LocalState,State,SV2,WF), |
| 648 | | KernelCall =.. [Kernel_predicate,SV1,T1,SV2,T2,WF], |
| 649 | | kernel_call_predicate(KernelCall,Module,WF,Expression). |
| 650 | | b_test_atomic_boolean_expression2(Expression,_,LocalState,State,WF) :- |
| 651 | | functor(Expression,BOP,2), |
| 652 | | kernel_mappings:binary_boolean_operator(BOP,Module:Kernel_predicate,WFEnabled),!, |
| 653 | | arg(1,Expression,Arg1), arg(2,Expression,Arg2), |
| 654 | ? | b_compute_expression(Arg1,LocalState,State,SV1,WF), % TODO: symbolic for some BOP: <:, <<: ? |
| 655 | | (binary_boolean_operator_trivially_true(BOP,SV1) |
| 656 | | -> check_well_defined(Arg2,LocalState,State,WF) |
| 657 | ? | ; b_compute_expression(Arg2,LocalState,State,SV2,WF), |
| 658 | | ( WFEnabled==yes -> |
| 659 | | KernelCall =.. [Kernel_predicate,SV1,SV2,WF2], |
| 660 | | (do_not_enumerate_binary_boolean_operator(BOP,Arg1,Arg2) % we assume: this is only true for WFEnabled predicates |
| 661 | | -> add_wait_flag_info(WF,wfx_no_enumeration,WF2) |
| 662 | | ; WF2=WF) |
| 663 | | ; KernelCall =.. [Kernel_predicate,SV1,SV2], WF2=WF % note: WF2 still used in kernel_call_predicate |
| 664 | | ), |
| 665 | ? | kernel_call_predicate(KernelCall,Module,WF2,Expression) |
| 666 | | ). |
| 667 | | b_test_atomic_boolean_expression2(E,Info,_LS,_S,_WF) :- |
| 668 | | add_internal_error('Uncovered boolean expression: ',b(E,pred,Info)), |
| 669 | | print_functor(E),nl, |
| 670 | | fail. |
| 671 | | |
| 672 | | |
| 673 | | binary_boolean_operator_trivially_true(subset,X) :- X==[]. |
| 674 | | |
| 675 | | check_well_defined(Arg,_LocalState,_State,_WF) :- |
| 676 | | always_well_defined(Arg),!. |
| 677 | | check_well_defined(Arg,LocalState,State,WF) :- |
| 678 | | b_compute_expression(Arg,LocalState,State,_,WF). |
| 679 | | |
| 680 | | %:- use_module(specfile,[animation_minor_mode/1]). |
| 681 | | :- use_module(b_machine_hierarchy,[abstract_constant/2, concrete_constant/2]). |
| 682 | | b_test_atomic_equal_boolean_expression(Arg1,Arg2,_Info,LocalState,State,WF) :- |
| 683 | | Arg1 = b(_,integer,_), %get_texpr_type(Arg1,integer), |
| 684 | | preferences:preference(use_clpfd_solver,true),!, |
| 685 | | (Arg2 = b(_,integer,_) -> true ; add_internal_error('Type error in AST, not wrapped integer: ',Arg2)), |
| 686 | ? | b_test_arith_equal_boolean_expression(Arg1,Arg2,LocalState,State,WF). |
| 687 | | b_test_atomic_equal_boolean_expression(Arg1,Arg2,Info,LocalState,State,WF) :- |
| 688 | ? | member(prob_annotation('LAMBDA-EQUALITY'),Info), |
| 689 | | \+ simple_expression(Arg2), |
| 690 | | is_lambda_result_id(Arg1), %get_texpr_id(Arg1,'_lambda_result_'), % ensure we have not renamed it to was_lambda_result ? |
| 691 | | !, % this is an equality for the result of a lambda abstraction; only evaluate at the end |
| 692 | | b_compute_expression(Arg1,LocalState,State,SV1,WF), % this is the lambda result identifier _lambda_result_ |
| 693 | | %get_enumeration_finished_wait_flag(WF,EWF), |
| 694 | | %get_wait_flag(10000,lambda_result,WF,EWF), |
| 695 | | % compute deterministic parts of Arg2 in wf0 phase: avoid computing this stuff over again |
| 696 | | %print('DELAY: '), translate:print_bexpr(Arg2),nl, |
| 697 | | kernel_waitflags:copy_wf01e_wait_flags(WF,WF2), b_compute_expression(Arg2,LocalState,State,SV2,WF2), |
| 698 | | get_last_wait_flag(lambda_result,WF,EWF), |
| 699 | | ground_value_check(SV2,G2), |
| 700 | | when((nonvar(G2);nonvar(SV1);nonvar(EWF)),b_lambda_result_equal_boolean_expression(SV1,SV2,WF,EWF,WF2)). |
| 701 | | b_test_atomic_equal_boolean_expression(Arg1,Arg2,Info,LocalState,State,WF) :- |
| 702 | | (LocalState = [] |
| 703 | | -> true % only check if we are at the outer level |
| 704 | | % (not inside a quantifier,...) or if we have CSE introduzed lazy_lets: |
| 705 | | ; preferences:preference(use_common_subexpression_elimination,true) |
| 706 | | % TO DO: check that we have only added @nr to local state from lazy_let; ADD INFO FIELD |
| 707 | | ), |
| 708 | | get_texpr_type(Arg1,Type1), is_set_type(Type1,SetType1), |
| 709 | | % previously we required SetType1 = couple(_,_) |
| 710 | | get_texpr_id(Arg1,ID), |
| 711 | | % we could check: \+ get_texpr_id(Arg2,_), we do not need to treat simple equations like ID=ID2; unless for memo? |
| 712 | | (abstract_constant(ID,_) % check if ABSTRACT_CONSTANT |
| 713 | | -> Kind=abstract, |
| 714 | | kernel_objects:max_cardinality(Type1,MaxCard1), |
| 715 | | (number(MaxCard1) % check that type is large enough to warrant recursive or symbolic treatment |
| 716 | | -> (MaxCard1>100 -> true ; constant_variable_marked_as_memo(ID)) |
| 717 | | ; true) |
| 718 | | ; %fail, |
| 719 | | concrete_constant(ID,_) |
| 720 | | -> Kind=concrete, |
| 721 | | (constant_variable_marked_as_expand(ID) |
| 722 | | -> true % note: we also check for expand annotation in store (expand_closure_value), |
| 723 | | % but it is useful to expand ID already here to ease checking PROPERTIES |
| 724 | | ; constant_variable_marked_as_memo(ID)) |
| 725 | | %; b_is_unused_constant(ID) -> ABSTRACT=true % TO DO: check if ID not used somewhere else in Properties ??!! |
| 726 | | ), |
| 727 | | % in future: we may do size change analysis, or something like that to check that we have properly defined |
| 728 | | % recursive function |
| 729 | | debug_format(19,'Equation defining ~w constant: ~w~n',[Kind,ID]), |
| 730 | | !, |
| 731 | | (constant_variable_marked_as_expand(ID) -> SymArg2 = Arg2 ; mark_bexpr_as_symbolic(Arg2,SymArg2)), |
| 732 | | b_add_constant_definition(Kind,ID,Info,Arg1,SetType1,SymArg2,LocalState,State,WF). |
| 733 | | b_test_atomic_equal_boolean_expression(Arg1,Arg2,Info,LocalState,State,WF) :- |
| 734 | | constants_profiling_on, |
| 735 | | get_texpr_id(Arg1,ID),b_is_constant(ID), !, |
| 736 | | % Note: does not yet keep track of arithmetic equalities above |
| 737 | | b_compute_expression(Arg1,LocalState,State,SV1,WF), |
| 738 | | push_constant_def_equality_call_stack(WF,ID,Info,WF2), |
| 739 | | start_profile(ID,T1), |
| 740 | | b_compute_expression(Arg2,LocalState,State,SV2,WF2), |
| 741 | | get_waitflags_phase(WF,Phase), |
| 742 | | stop_constant_profile(SV2,Phase,ID,[],T1), % normal constant, not symbolic, memoized or recursive |
| 743 | | start_propagation_profile(ID,T2,CstL1), |
| 744 | | kernel_call_predicate_equal_object_optimized(SV1,SV2,WF), |
| 745 | | stop_propagation_profile(SV2,ID,T2,CstL1). |
| 746 | | b_test_atomic_equal_boolean_expression(Arg1,Arg2,_Info,LocalState,State,WF) :- |
| 747 | ? | b_compute_expression(Arg1,LocalState,State,SV1,WF), |
| 748 | ? | b_compute_expression(Arg2,LocalState,State,SV2,WF), |
| 749 | ? | kernel_call_predicate_equal_object_optimized(SV1,SV2,WF). |
| 750 | | |
| 751 | | |
| 752 | | |
| 753 | | % treat equality ID = Symbolic Function (or relation/set) |
| 754 | | b_add_constant_definition(Kind,ID,Info,Arg1,SetType1,SymArg2,LocalState,State,WF) :- |
| 755 | | % observe CTRL-C: e.g., in data validation models these abstract functions can be very large and costly to compute |
| 756 | | observe_user_interrupt_signal(computing_value_for_abstract_constant(ID),SymArg2, |
| 757 | | b_add_constant_definition_aux(Kind,ID,Info,Arg1,SetType1,SymArg2,LocalState,State,WF)). |
| 758 | | |
| 759 | | :- use_module(runtime_profiler,[start_profile/2, |
| 760 | | stop_constant_profile/5, constants_profiling_on/0]). |
| 761 | | b_add_constant_definition_aux(Kind,ID,_Info,Arg1,SetType1,SymArg2,LocalState,State,WF) :- |
| 762 | | preference(use_function_memoization,MemoPref), % true, false or default |
| 763 | | MemoPref \= false, % turned off |
| 764 | | ( MemoPref = true, Kind=abstract ; |
| 765 | | constant_variable_marked_as_memo(ID)), % description(memo) as info field using @desc memo pragma; see |
| 766 | | !, |
| 767 | | start_profile(ID,T1), |
| 768 | | b_compute_expression(Arg1,LocalState,State,SV1,WF), % the identifier |
| 769 | | (SymArg2 = b(recursive_let(TID,Body),_,_) |
| 770 | | -> get_texpr_id(TID,RecID), |
| 771 | | b_compute_expression(Body,[bind(RecID,RedIDVal)|LocalState],State,SV2,WF), |
| 772 | | RecValue='$recursion_value'(RedIDVal), IDInfo = [recursive_memo] |
| 773 | | ; b_compute_expression(SymArg2,LocalState,State,SV2,WF), |
| 774 | | RecValue='$no_recursion', IDInfo = [memoized] |
| 775 | | ), |
| 776 | | get_waitflags_phase(WF,Phase), stop_constant_profile(SV2,Phase,ID,IDInfo,T1), |
| 777 | | (nonvar(SV2), SV2 \= closure(_,_,_) |
| 778 | | -> % value fully computed; no need to memoize |
| 779 | | debug_println(19,not_memoizing(ID)), |
| 780 | | kernel_call_predicate_equal_object_optimized(SV1,SV2,WF) |
| 781 | | ; (nonvar(SV2) -> mark_closure_as_symbolic(SV2,SV3) ; SV3=SV2), |
| 782 | | memoization:register_memoization_function(ID,SV3,SetType1,RecValue,MEMOID,SV4), |
| 783 | | debug_println(19,marking_for_memoization(ID,MEMOID)), |
| 784 | | kernel_call_predicate_equal_object_optimized(SV1,SV4,WF) |
| 785 | | ). |
| 786 | | b_add_constant_definition_aux(_Kind,ID,Info,Arg1,_,SymArg2,LocalState,State,WF) :- |
| 787 | | start_profile(ID,T1), |
| 788 | | b_compute_expression(Arg1,LocalState,State,SV1,WF), % the identifier ID |
| 789 | | push_constant_def_equality_call_stack(WF,ID,Info,WF2), |
| 790 | | b_compute_expression(SymArg2,LocalState,State,SV2,WF2), |
| 791 | | (constant_variable_marked_as_expand(ID) |
| 792 | | -> IDInfo = [marked_as_expand], |
| 793 | | b_expand_compute_comprehension_set(SV2,Info,SV3,WF) |
| 794 | | ; definitely_expand_this_explicit_set(SV2) % check for a few patterns which should definitely be expanded; |
| 795 | | % in case user puts a set accidentally into the ABSTRACT_CONSTANTS section |
| 796 | | -> IDInfo = [automatic_expand], |
| 797 | | b_expand_compute_comprehension_set(SV2,Info,SV3,WF) |
| 798 | | ; var(SV2) -> IDInfo = [], SV3=SV2 |
| 799 | | ; IDInfo = [], % it is from ABSTRACT_CONSTANTS, but this info is already stored |
| 800 | | mark_closure_as_symbolic(SV2,SV3) |
| 801 | | ), |
| 802 | | get_waitflags_phase(WF,Phase), stop_constant_profile(SV3,Phase,ID,IDInfo,T1), |
| 803 | | start_propagation_profile(ID,T2,CstL1), |
| 804 | | kernel_call_predicate_equal_object_optimized(SV1,SV3,WF), |
| 805 | | % profile propagation time and what constants are grounded |
| 806 | | stop_propagation_profile(SV3,ID,T2,CstL1). |
| 807 | | |
| 808 | | push_constant_def_equality_call_stack(WF,ID,Pos,WF2) :- |
| 809 | | (constants_profiling_on |
| 810 | | -> push_wait_flag_call_stack_info(WF, |
| 811 | | id_equality_evaluation(ID,constant,Pos),WF2) |
| 812 | | ; WF2=WF). |
| 813 | | |
| 814 | | start_propagation_profile(ID,T1,CstL1) :- |
| 815 | | (constants_profiling_on -> findall(Cst1,det_solution_for_constant_was_stored(Cst1),CstL1) ; CstL1=[]), |
| 816 | | start_profile(ID,T1). |
| 817 | | stop_propagation_profile(SV3,ID,T1,CstL1) :- |
| 818 | | (constants_profiling_on |
| 819 | | -> findall(Cst2,det_solution_for_constant_was_stored(Cst2),L2), |
| 820 | | append(CstL1,New,L2), debug_format(9,'Propagation of value for ~w instantiated ~w~n',[ID,New]), |
| 821 | | IDInfo2 = [instantiates(New)] |
| 822 | | ; IDInfo2 = []), |
| 823 | | stop_constant_profile(SV3,propagate_value,ID,IDInfo2,T1). |
| 824 | | |
| 825 | | % treat not(Arg1 = Arg2) predicates |
| 826 | | b_test_atomic_not_equal_boolean_expression(Arg1,Arg2,_Info,LocalState,State,WF) :- |
| 827 | | get_texpr_type(Arg1,boolean),!, % special treatment : not_equal_object has no type information |
| 828 | | b_compute_expression(Arg1,LocalState,State,B1,WF), |
| 829 | | b_compute_expression(Arg2,LocalState,State,B2,WF), |
| 830 | | negate(B1,B2). % from bool_pred |
| 831 | | b_test_atomic_not_equal_boolean_expression(Arg1,Arg2,_Info,LocalState,State,WF) :- |
| 832 | | get_texpr_type(Arg1,integer), |
| 833 | | preferences:preference(use_clpfd_solver,true), |
| 834 | | !, |
| 835 | | % Note: calling b_compute_arith_expression will also instantiate a variable to at least the int(_) skeleton; thereby enabling propagation |
| 836 | | % this is potentially better than calling the default versions of the predicates, which may wait until the int(_) skeleton is set up before propagation |
| 837 | | b_compute_arith_expression(Arg1,LocalState,State,CLPFD_Expr1,WF), |
| 838 | | b_compute_arith_expression(Arg2,LocalState,State,CLPFD_Expr2,WF), |
| 839 | | clpfd_interface:clpfd_neq_expr(CLPFD_Expr1,CLPFD_Expr2). |
| 840 | | b_test_atomic_not_equal_boolean_expression(Arg1,Arg2,_Info,LocalState,State,WF) :- |
| 841 | ? | b_compute_expression_symbolic(Arg1,LocalState,State,SV1,WF), |
| 842 | ? | b_compute_expression_symbolic(Arg2,LocalState,State,SV2,WF), |
| 843 | | kernel_call_predicate_not_equal_object(SV1,SV2,WF,not_equal(Arg1,Arg2)). |
| 844 | | % Note: symbolic case occurs for symbolic abstract constants f when using DOUBLE_EVALUATION |
| 845 | | % in the evaluation view which are defined by f = %x... when checking f /= |
| 846 | | |
| 847 | | % a version of b_compute_expression which does not try to expand comprehension sets |
| 848 | | % TODO: deal with other constructs which merit symbolic treatment; union of comprehension sets, ... |
| 849 | | % (see line 147 in AMASS_mchs/oriented_abscissa.mch) |
| 850 | | b_compute_expression_symbolic(Arg,LocalState,State,Val,WF) :- |
| 851 | | (Arg = b(comprehension_set(PP,CC),_,InfoCS) % second arg is a comprehension set; |
| 852 | | -> % print(symbolic(PP)),nl, |
| 853 | | b_compute_comprehension_set_symbolic(PP,CC,InfoCS,LocalState,State,Val,WF) |
| 854 | ? | ; b_compute_expression(Arg,LocalState,State,Val,WF) |
| 855 | | ). |
| 856 | | % ditto for already de-constructed b/3 expression: |
| 857 | | b_compute_expression2_symbolic(comprehension_set(PP,CC),_,InfoCS,LocalState,State,Val,WF) :- !, |
| 858 | | b_compute_comprehension_set_symbolic(PP,CC,InfoCS,LocalState,State,Val,WF). |
| 859 | | b_compute_expression2_symbolic(Arg,Type,Info,LocalState,State,Val,WF) :- |
| 860 | | b_compute_expression2(Arg,Type,Info,LocalState,State,Val,WF). |
| 861 | | |
| 862 | | simple_expression(b(A,_,_)) :- simple_expression_aux(A). |
| 863 | | simple_expression_aux(boolean_true). |
| 864 | | simple_expression_aux(boolean_false). |
| 865 | | simple_expression_aux(empty_set). |
| 866 | | simple_expression_aux(empty_sequence). |
| 867 | | simple_expression_aux(integer(_)). |
| 868 | | simple_expression_aux(max_int). |
| 869 | | simple_expression_aux(min_int). |
| 870 | | simple_expression_aux(string(_)). |
| 871 | | simple_expression_aux(value(_)). |
| 872 | | |
| 873 | | |
| 874 | | is_lambda_result_id(b(identifier('_lambda_result_'),_,_)). |
| 875 | | % at the moment we only detect those identifiers; the info field is currently not updated for _was_lambda_result_ |
| 876 | | % and not available in other treatmens of _lambda_result_ (in b_enumerate) |
| 877 | | %is_lambda_result_id(b(identifier(X),_,Info)) :- |
| 878 | | % memberchk(lambda_result,Info). |
| 879 | | % (X=='_lambda_result_' -> (memberchk(lambda_result,Info) -> true ; format('~n*** _lambda_result_ without info: ~w~n~N',[Info])) ; memberchk(lambda_result,Info)). |
| 880 | | |
| 881 | | %:- block b_lambda_result_equal_boolean_expression(-,?,?, -,?). % we now use when above |
| 882 | | % only compute lambda result when finished finding domain element, i.e., _EWF is grounded |
| 883 | | b_lambda_result_equal_boolean_expression(SV1,SV2,WF,EWF,WF2) :- |
| 884 | | kernel_call_predicate_equal_object_optimized(SV1,SV2,WF), |
| 885 | | % print(' LAMBDA : '), print((SV1,_EWF)),nl, |
| 886 | | % the inner waitflags are for evaluating the lambda_result Expression; unless there is a WD issue this should |
| 887 | | % never fail; we can delay the enumeration as much as possible |
| 888 | | % NOTE: even if SV2 is ground, Arg2 may not be (e.g., if Arg2 = prj2(..,..)(NotGr,Ground)) |
| 889 | | blocking_ground_copied_wait_flags(WF2,EWF). % without when we will ground WF0 which is shared with outer WF-store |
| 890 | | :- block blocking_ground_copied_wait_flags(?,-). |
| 891 | | blocking_ground_copied_wait_flags(WF2,_) :- |
| 892 | | ground_constraintprop_wait_flags(WF2). % do not ground EF flag, as shared with outer WF and WF2 is copied |
| 893 | | |
| 894 | | % TO DO: add avl_set+closure expansion |
| 895 | | %b_test_member_expression(Expr,El,_Set,_LocalState,_State,_WF) :- |
| 896 | | % print('test member: '),print_bexpr(Expr),nl,fail. |
| 897 | | b_test_member_expression(TElem,b(Set,Type,Info),LocalState,State,WF) :- |
| 898 | ? | b_test_member_expression2(Set,Type,Info,TElem,LocalState,State,WF). |
| 899 | | |
| 900 | | % new profiler |
| 901 | | %:- use_module('../extensions/profiler/profiler.pl'). |
| 902 | | %:- use_module('../extensions/profiler/profiler_te.pl'). |
| 903 | | %:- enable_profiling(b_test_member_expression2/7). |
| 904 | | |
| 905 | | % old profiler |
| 906 | | %:- use_module(covsrc(hit_profiler)). |
| 907 | | |
| 908 | | b_test_member_expression2(comprehension_set(Par,Cond),_Type,_Info,El,LocalState,State,WF) :- |
| 909 | | % used to call in next clause below: b_generate_closure_if_necessary(Par,Cond,LocalState,State,SetValue,WF) /* will not expand closure */ |
| 910 | | /* optimized case for dealing with comprehension_set ; however, does not seem to buy anything over older version with b_generate_closure_if_necessary in test 1079 */ |
| 911 | | /* new version buys about 6 % for {x| x:1..400000 & x:{n|n >= 9000 & n<= 600000 & n mod 100 = 0}} (Sep 3 2014) */ |
| 912 | | set_up_typed_localstate(Par,ValueList,_TypedVals,LocalState,NewLocalState,positive), |
| 913 | | !, |
| 914 | | convert_list_into_pairs(ValueList,ElementVal), |
| 915 | | b_compute_expression(El,LocalState,State,ElementVal,WF), |
| 916 | | b_test_boolean_expression(Cond,NewLocalState,State,WF). |
| 917 | | b_test_member_expression2(SymbolicSetConstant,_Type,_Info,El,LocalState,State,WF) :- |
| 918 | | %hit_profiler:add_profile_hit(SymbolicSetConstant), |
| 919 | | cst_in_boolean_type(SymbolicSetConstant,Module:Kernel_predicate),!, % two cases : string_set and integer_set |
| 920 | | b_compute_expression(El,LocalState,State,ElValue,WF), |
| 921 | | functor(KernelCall,Kernel_predicate,2), |
| 922 | | arg(1,KernelCall,ElValue), |
| 923 | | arg(2,KernelCall,WF), |
| 924 | | % print_message(cst_in_boolean_type_call(KernelCall)), |
| 925 | | kernel_call_predicate(KernelCall,Module,WF,SymbolicSetConstant). % should be member(... SymbolicSetConstant) |
| 926 | | b_test_member_expression2(Expression,_Type,Info,El,LocalState,State,WF) :- |
| 927 | | functor(Expression,BOP,2), |
| 928 | | binary_in_boolean_type(BOP,Module:Kernel_predicate),!, |
| 929 | | b_compute_expression(El,LocalState,State,ElValue,WF), |
| 930 | | arg(1,Expression,Arg1),arg(2,Expression,Arg2), |
| 931 | | (binary_in_definitely_true(BOP,ElValue) |
| 932 | | -> % no need to compute arguments; unless required for WD: |
| 933 | | check_well_defined(Arg1,LocalState,State,WF), |
| 934 | | check_well_defined(Arg2,LocalState,State,WF) |
| 935 | | ; b_compute_expression(Arg1,LocalState,State,SV1,WF), |
| 936 | | b_compute_expression(Arg2,LocalState,State,SV2,WF), |
| 937 | | opt_push_wait_flag_call_stack_info(WF,b_operator_call(member,[ElValue,b_operator(BOP,[SV1,SV2])],Info),WF2), |
| 938 | | % for constant profiling: we could catch partial/total_fun checks here translate:print_bexpr(El),nl,translate:print_bexpr(b(Expression,_Type,Info)),nl,nl, |
| 939 | | binary_in_kernel_call(Module,Kernel_predicate,ElValue,SV1,SV2,WF2,Expression) |
| 940 | | ). |
| 941 | | b_test_member_expression2(Expression,_Type,Info,El,LocalState,State,WF) :- |
| 942 | | functor(Expression,UnOP,1), |
| 943 | | unary_in_boolean_type(UnOP,Module:Kernel_predicate),!, |
| 944 | | b_compute_expression(El,LocalState,State,ElValue,WF), |
| 945 | | arg(1,Expression,Arg1), |
| 946 | | (unary_in_definitely_true(UnOP,ElValue) |
| 947 | | -> % no need to compute arguments; unless required for WD: |
| 948 | | check_well_defined(Arg1,LocalState,State,WF) |
| 949 | | ; b_compute_expression(Arg1,LocalState,State,SV1,WF), |
| 950 | | opt_push_wait_flag_call_stack_info(WF,b_operator_call(member,[ElValue,b_operator(UnOP,[SV1])],Info),WF2), |
| 951 | | unary_in_kernel_call(Module,Kernel_predicate,ElValue,SV1,WF2,Expression) |
| 952 | | ). |
| 953 | | b_test_member_expression2(SetExpression,Type,Info,El,LocalState,State,WF) :- |
| 954 | | b_compute_expression2(SetExpression,Type,Info,LocalState,State,SetValue,WF), |
| 955 | | (always_well_defined_or_disprover_mode(El) |
| 956 | | -> SetValue \== [] % if we have the empty set: check_element will always fail |
| 957 | | ; true % we could check SetValue \== [] if find_abort_values=false ?? |
| 958 | | ), |
| 959 | | b_compute_expression(El,LocalState,State,Element,WF), |
| 960 | | Span = El, % Info is quite often the empty list |
| 961 | | (not_invertible(El) % then no sense in setting up member constraint f(...) : SetValue ; |
| 962 | | % we cannot used the info anyway; but we could enumerate |
| 963 | | -> % we wait until Element is fully known before doing the check |
| 964 | | ground_value_check(Element,GrEl), |
| 965 | | when(nonvar(GrEl),kernel_call_predicate_check_element_of_wf(Element,SetValue,WF,Span)) |
| 966 | ? | ; kernel_call_predicate_check_element_of_wf(Element,SetValue,WF,Span)). |
| 967 | | |
| 968 | | :- use_module(external_functions,[external_fun_can_be_inverted/1]). |
| 969 | | not_invertible(b(E,_,_)) :- not_inv_aux(E). |
| 970 | | not_inv_aux(external_function_call(FunName,_Args)) :- \+ external_fun_can_be_inverted(FunName). |
| 971 | | % TO DO: capture other cases like function applications |
| 972 | | % TO DO: also do this in b_interpreter_check ? |
| 973 | | % TO DO: We should compute this information in ast_cleanup; e.g., what if we do STRING_TO_INT(s)+1 : SET |
| 974 | | |
| 975 | | % TO DO: >>> 2:{x,v} --> special case for set_extension([b(identifier(x),integer,[nodeid(none)]),b(identifier(v),integer,[nodeid(none)])]) --> 2=x or 2=v --> evaluate [x,v] -> sort and then use list member (or should b_ast_cleanup do this) ? |
| 976 | | |
| 977 | | :- use_module(kernel_lists,[not_element_of_list_wf/3]). |
| 978 | | b_test_notmember_expression(set_extension(Ex),El,_Set,_,LocalState,State,WF) :- !, |
| 979 | | b_compute_expressions(Ex,LocalState,State,ExValueList,WF), |
| 980 | | b_compute_expression(El,LocalState,State,ElementVal,WF), |
| 981 | | not_element_of_list_wf(ElementVal,ExValueList,WF). |
| 982 | | b_test_notmember_expression(SymbolicSetCst,El,_Set,_,LocalState,State,WF) :- |
| 983 | | kernel_mappings:cst_not_in_boolean_type(SymbolicSetCst,Module:Kernel_predicate),!, |
| 984 | | b_compute_expression(El,LocalState,State,ElValue,WF), |
| 985 | | functor(KernelCall,Kernel_predicate,1), |
| 986 | | arg(1,KernelCall,ElValue), |
| 987 | | % debug_print(cst_not_in_boolean_type(9,KernelCall)), |
| 988 | | kernel_call_predicate(KernelCall,Module,WF,SymbolicSetCst). |
| 989 | | b_test_notmember_expression(Expression,El,_Set,Info,LocalState,State,WF) :- |
| 990 | | functor(Expression,BOP,2), |
| 991 | | kernel_mappings:binary_not_in_boolean_type(BOP,Module:Kernel_predicate),!, |
| 992 | | b_compute_expression(El,LocalState,State,ElValue,WF), |
| 993 | | \+ binary_in_definitely_true(BOP,ElValue), |
| 994 | | arg(1,Expression,Arg1),arg(2,Expression,Arg2), |
| 995 | | b_compute_expression(Arg1,LocalState,State,SV1,WF), |
| 996 | | b_compute_expression(Arg2,LocalState,State,SV2,WF), |
| 997 | | % KernelCall =.. [Kernel_predicate,ElValue,SV1,SV2,WF], |
| 998 | | opt_push_wait_flag_call_stack_info(WF,b_operator_call(not_member,[ElValue,b_operator(BOP,[SV1,SV2])],Info),WF2), |
| 999 | ? | kernel_call_predicate3(Kernel_predicate,ElValue,SV1,SV2,Module,WF2,Expression). |
| 1000 | | b_test_notmember_expression(Expression,El,_Set,Info,LocalState,State,WF) :- |
| 1001 | | functor(Expression,UnOP,1), |
| 1002 | | unary_not_in_boolean_type(UnOP,Module:Kernel_predicate),!, |
| 1003 | | b_compute_expression(El,LocalState,State,ElValue,WF), |
| 1004 | | \+ unary_in_definitely_true(UnOP,ElValue), |
| 1005 | | arg(1,Expression,Arg1), |
| 1006 | | b_compute_expression(Arg1,LocalState,State,SV1,WF), |
| 1007 | | %KernelCall =.. [Kernel_predicate,ElValue,SV1,WF], |
| 1008 | | %kernel_call_predicate(KernelCall,Module,WF,Expression). |
| 1009 | | opt_push_wait_flag_call_stack_info(WF,b_operator_call(not_member,[ElValue,b_operator(UnOP,[SV1])],Info),WF2), |
| 1010 | | kernel_call_predicate2(Kernel_predicate,ElValue,SV1,Module,WF2,Expression). |
| 1011 | | b_test_notmember_expression(Expr,Elem,Set,_Info,LocalState,State,WF) :- |
| 1012 | | (Expr=comprehension_set(Par,Cond) |
| 1013 | | -> b_generate_closure_if_necessary(Par,Cond,LocalState,State,SetValue,WF) /* will not expand closure */ |
| 1014 | | ; b_compute_expression(Set,LocalState,State,SetValue,WF) |
| 1015 | | ), |
| 1016 | | (SetValue == [] |
| 1017 | | -> /* nothing can be member of empty set */ |
| 1018 | | check_well_defined(Elem,LocalState,State,WF) |
| 1019 | | % we could disable this check if find_abort_values=true |
| 1020 | | ; b_compute_expression(Elem,LocalState,State,ElemValue,WF), |
| 1021 | | %kernel_call_predicate2(not_element_of_wf,ElemValue,SetValue,kernel_objects,WF,Expr) |
| 1022 | | %kernel_call_predicate(not_element_of_wf(ElemValue,SetValue,WF),kernel_objects,WF,Expr) |
| 1023 | | kernel_call_predicate_not_element_of_wf(ElemValue,SetValue,WF) |
| 1024 | | ). |
| 1025 | | |
| 1026 | | :- block block_test_implication(?,?,?,?,?,-). |
| 1027 | | block_test_implication(LHS,RHS,LocalState,State,WF,_WF2) :- |
| 1028 | ? | b_test_inner_boolean_expression(LHS,LocalState,State,WF), |
| 1029 | | b_test_inner_boolean_expression(RHS,LocalState,State,WF). |
| 1030 | | block_test_implication(LHS,_RHS,LocalState,State,WF,_WF2) :- |
| 1031 | ? | b_not_test_inner_boolean_expression(LHS,LocalState,State,WF). |
| 1032 | | |
| 1033 | | |
| 1034 | | b_test_equivalence(LHS,RHS,LocalState,State,WF,Ai,Ao) :- |
| 1035 | | copy_wf_start(WF,equivalence,CWF), |
| 1036 | ? | if(b_check_boolean_expression(LHS,LocalState,State,CWF,PredRes,Ai,Aii), |
| 1037 | ? | (b_check_test_equivalence(PredRes,LHS,RHS,LocalState,State,CWF,Aii,Ao), |
| 1038 | | copy_wf_finish(WF,CWF)), |
| 1039 | | (% check LHS fails, We could check if RHS can be checked with b_check_boolean_expression |
| 1040 | | Ai=Ao, get_binary_choice_wait_flag(equivalence,WF,WF2), |
| 1041 | | b_enum_test_equivalence(LHS,RHS,LocalState,State,WF,WF2,Ai))). |
| 1042 | | b_not_test_equivalence(LHS,RHS,LocalState,State,WF,Ai,Ao) :- |
| 1043 | | copy_wf_start(WF,not_equivalence,CWF), |
| 1044 | | if(b_check_boolean_expression(LHS,LocalState,State,CWF,PredRes,Ai,Aii), |
| 1045 | | (negate(PredRes,NegPredRes), % from bool_pred |
| 1046 | | b_check_test_equivalence(NegPredRes,LHS,RHS,LocalState,State,CWF,Aii,Ao), |
| 1047 | | copy_wf_finish(WF,CWF)), |
| 1048 | | (% check LHS fails, We could check if RHS can be checked with b_check_boolean_expression |
| 1049 | | Ai=Ao, get_binary_choice_wait_flag(not_equivalence,WF,WF2), |
| 1050 | | b_enum_not_test_equivalence(LHS,RHS,LocalState,State,WF,WF2,Ai))). |
| 1051 | | b_check_test_equivalence(PredRes,LHS,RHS,LocalState,State,WF,Aii,Ao) :- |
| 1052 | | % print('checking <=> '), print_bexpr(LHS), print(' '), print(PredRes),nl, |
| 1053 | ? | (var(PredRes),b_check_boolean_expression(RHS,LocalState,State,WF,PredRes2,Aii,Ao) % TO DO <---- REPLACE CUT |
| 1054 | | -> /* also propagate from right to left */ |
| 1055 | | equiv_bidrectional_test_boolean_expression(PredRes,PredRes2,LHS,RHS,LocalState,State,WF) |
| 1056 | | ; Aii=Ao, |
| 1057 | ? | equiv_test_boolean_expression(PredRes,pred_true,RHS,LocalState,State,WF,Aii) |
| 1058 | | ). % should we also add a binary choice flag ?? what about a=2 <=> b/=3 ?? |
| 1059 | | |
| 1060 | | :- block b_enum_test_equivalence(?,?,?,?,?,-,?). |
| 1061 | | b_enum_test_equivalence(LHS,RHS,LocalState,State,WF,_WF2,Ai) :- |
| 1062 | | b_test_boolean_expression(LHS,LocalState,State,WF,Ai,Aii), % TO DO: we could call b_test_inner_boolean_expression with Ai |
| 1063 | | b_test_boolean_expression(RHS,LocalState,State,WF,Aii,_). |
| 1064 | | b_enum_test_equivalence(LHS,RHS,LocalState,State,WF,_WF2,Ai) :- |
| 1065 | | b_not_test_boolean_expression(LHS,LocalState,State,WF,Ai,Aii), |
| 1066 | | b_not_test_boolean_expression(RHS,LocalState,State,WF,Aii,_). |
| 1067 | | |
| 1068 | | %enumerate_bool(X,WF) :- !. |
| 1069 | | enumerate_bool(X,Prio,WF) :- (nonvar(X) -> true /* already set */ |
| 1070 | | ; get_wait_flag0(WF,WF0), |
| 1071 | | enumerate_bool0(X,Prio,WF,WF0)). |
| 1072 | | |
| 1073 | | :- block enumerate_bool0(?,?,?,-). |
| 1074 | | enumerate_bool0(X,Prio,WF,_) :- (nonvar(X) -> true /* already set */ |
| 1075 | | ; %print(enum_bool(X)), translate:print_bexpr(LHS),nl, |
| 1076 | | % get_binary_choice_wait_flag(enumerate_bool,WF,WF2), |
| 1077 | | get_wait_flag(Prio,enumerate_bool,WF,WF2), % what priority should we use ?? give time for other enumerators to decide the lhs of the disjunct; actions_cbtc is slower to setup with low priority |
| 1078 | | enumerate_bool_aux(X,WF2)). |
| 1079 | | :- block enumerate_bool_aux(-,-). |
| 1080 | | %enumerate_bool_aux(P,WF) :- var(P),print(forcing_bool(P,WF)),nl,fail. % |
| 1081 | | enumerate_bool_aux(pred_true,_). |
| 1082 | | enumerate_bool_aux(pred_false,_). |
| 1083 | | |
| 1084 | | :- block b_test_disjunction(?,?,?,?,?,-,?). |
| 1085 | | b_test_disjunction(LHS,_RHS,LocalState,State,WF,_WF2,Ai) :- |
| 1086 | | b_test_boolean_expression(LHS,LocalState,State,WF,Ai,_). |
| 1087 | | b_test_disjunction(LHS,RHS,LocalState,State,WF,_WF2,Ai) :- |
| 1088 | | b_not_test_boolean_expression(LHS,LocalState,State,WF,Ai,Aii), |
| 1089 | | b_test_boolean_expression(RHS,LocalState,State,WF,Aii,_). |
| 1090 | | |
| 1091 | | :- block b_not_test_conjunction(?,?,?,?,?,-,?). |
| 1092 | | b_not_test_conjunction(LHS,_RHS,LocalState,State,WF,_WF2,Ai) :- |
| 1093 | ? | b_not_test_boolean_expression(LHS,LocalState,State,WF,Ai,_). |
| 1094 | | b_not_test_conjunction(LHS,RHS,LocalState,State,WF,_WF2,Ai) :- |
| 1095 | ? | b_test_boolean_expression(LHS,LocalState,State,WF,Ai,Aii), |
| 1096 | | b_not_test_boolean_expression(RHS,LocalState,State,WF,Aii,_). |
| 1097 | | |
| 1098 | | :- block b_enum_not_test_equivalence(?,?,?,?,?,-,?). |
| 1099 | | b_enum_not_test_equivalence(LHS,RHS,LocalState,State,WF,_WF2,Ai) :- |
| 1100 | | b_test_boolean_expression(LHS,LocalState,State,WF,Ai,Aii), % we could call b_test_inner_boolean_expression with Ai |
| 1101 | | b_not_test_boolean_expression(RHS,LocalState,State,WF,Aii,_). |
| 1102 | | b_enum_not_test_equivalence(LHS,RHS,LocalState,State,WF,_WF2,Ai) :- |
| 1103 | | b_not_test_boolean_expression(LHS,LocalState,State,WF,Ai,Aii), |
| 1104 | | b_test_boolean_expression(RHS,LocalState,State,WF,Aii,_). |
| 1105 | | |
| 1106 | | |
| 1107 | | |
| 1108 | | /* --------------------------------------*/ |
| 1109 | | /* b_not_test_boolean_expression */ |
| 1110 | | /* --------------------------------------*/ |
| 1111 | | |
| 1112 | | b_not_test_list_of_boolean_expression([],_,_,_WF). |
| 1113 | | b_not_test_list_of_boolean_expression([B1|T],LS,S,WF) :- |
| 1114 | | b_not_test_boolean_expression(B1,LS,S,WF), |
| 1115 | | b_not_test_list_of_boolean_expression(T,LS,S,WF). |
| 1116 | | |
| 1117 | | :- assert_pre(b_interpreter:b_not_test_boolean_expression_cs(E,LS,S,_,_), |
| 1118 | | (bsyntaxtree:check_if_typed_predicate(E),type_check(LS,store),type_check(S,store))). |
| 1119 | | :- assert_post(b_interpreter:b_not_test_boolean_expression_cs(_,_,_,_,_), true). |
| 1120 | | |
| 1121 | | |
| 1122 | | % a version where we can provide an initial call stack entry for context |
| 1123 | | b_not_test_boolean_expression_cs(E,LS,S,PredKind,Nr) :- |
| 1124 | | init_wait_flags_cs(E,PredKind,Nr,b_not_test_boolean_expression_cs,WF), % TODO: we could register negation info |
| 1125 | | % TO DO: call b_trace_test_components ? |
| 1126 | | b_not_test_boolean_expression(E,LS,S,WF), |
| 1127 | | ground_wait_flags(WF). |
| 1128 | | |
| 1129 | | b_not_test_boolean_expression(E,LS,S,WF) :- |
| 1130 | ? | empty_avl(Ai), b_not_test_boolean_expression(E,LS,S,WF,Ai,_). |
| 1131 | | |
| 1132 | | :- assert_pre(b_interpreter:b_not_test_boolean_expression(E,LS,S,WF), |
| 1133 | | (type_check(E,boolean_expression),type_check(LS,store),type_check(S,store), type_check(WF,wait_flag))). |
| 1134 | | :- assert_post(b_interpreter:b_not_test_boolean_expression(_,_,_,_), true). |
| 1135 | | |
| 1136 | | :- if(environ(prob_debug_watch_flag,true)). |
| 1137 | | b_not_test_boolean_expression(b(Expr,_,Infos),LS,S,WF,Ai,Ao) :- !, |
| 1138 | | (waitflag0_is_set(WF) |
| 1139 | | -> debug:watch(400,b_interpreter:b_not_test_boolean_expression2(Expr,Infos,LS,S,WF,Ai,Ao)) |
| 1140 | | ; debug:watch_det(400,b_interpreter:b_not_test_boolean_expression2(Expr,Infos,LS,S,WF,Ai,Ao)) ). |
| 1141 | | :- else. |
| 1142 | | b_not_test_boolean_expression(b(Expr,Type,Infos),LS,S,WF,Ai,Ao) :- !, |
| 1143 | | (preference(smt_supported_interpreter,true) |
| 1144 | | -> b_not_test_boolean_expression2(Expr,Infos,LS,S,WF,Ai,Ao),create_negation(b(Expr,Type,Infos),Neg), |
| 1145 | | get_wait_flag1(smt_call,WF,BeforeEnumWF), |
| 1146 | | gensym:gensym(smt_assertion_name,Symbol), |
| 1147 | | smt_add_predicate(BeforeEnumWF,Neg,LS,S,Symbol) |
| 1148 | ? | ; b_not_test_boolean_expression2(Expr,Infos,LS,S,WF,Ai,Ao)). |
| 1149 | | :- endif. |
| 1150 | | b_not_test_boolean_expression(E,LS,S,WF,Ai,Ao) :- |
| 1151 | | add_internal_error('Boolean (not) expression not properly wrapped: ', |
| 1152 | | b_not_test_boolean_expression(E,LS,S,WF,Ai,Ao)), |
| 1153 | | b_not_test_boolean_expression2(E,[],LS,S,WF,Ai,Ao). |
| 1154 | | |
| 1155 | | b_not_test_boolean_expression2(truth,_,_,_,_WF,_Ai,_Ao) :- !,fail. |
| 1156 | | b_not_test_boolean_expression2(falsity,_,_,_,_WF,Ai,Ao) :- !,Ai=Ao. % NOT COVERED (2) |
| 1157 | | b_not_test_boolean_expression2(negation(BExpr),_,LocalState,State,WF,Ai,Ao) :- !, |
| 1158 | | b_test_boolean_expression(BExpr,LocalState,State,WF,Ai,Ao). |
| 1159 | | % following makes test 349 fail: |
| 1160 | | %b_not_test_boolean_expression2(conjunct(LHS,RHS),Infos,LocalState,State,WF,Ai,Ao) :- !, |
| 1161 | | % nl, print(' NOT: '),translate:print_bexpr(b(conjunct(LHS,RHS),pred,Infos)),nl, |
| 1162 | | % create_negation(LHS,NegLHS), % TO DO: avoid this construction by generalising code for disjunct |
| 1163 | | % create_negation(RHS,NegRHS), |
| 1164 | | % b_test_boolean_expression2(disjunct(NegLHS,NegRHS),Infos,LocalState,State,WF,Ai,Ao). |
| 1165 | | b_not_test_boolean_expression2(conjunct(LHS,RHS),Infos,LocalState,State,WF,Ai,Ao) :- !, |
| 1166 | | b(conjunct(LHS,RHS),pred,Infos) = Conj, |
| 1167 | | copy_wf_start(WF,not_conjunct,CWF), |
| 1168 | ? | if(b_check_boolean_expression(Conj,LocalState,State,CWF,PredRes,Ai,Ao), |
| 1169 | | (PredRes=pred_false, |
| 1170 | | copy_wf_finish(WF,CWF) |
| 1171 | | ), |
| 1172 | ? | if(b_check_boolean_expression(LHS,LocalState,State,CWF,LHSPredRes,Ai,Ao), % some redundant work with call above: TODO: avoid ! |
| 1173 | | (copy_wf_finish(WF,CWF), |
| 1174 | | get_last_wait_flag(not_conjunct_rhs,WF,WF2), % before starting to enumerate infinite types: better try both possibilities: |
| 1175 | ? | b_not_test_conjunction_rhs(LHSPredRes,RHS,LocalState,State,WF,Ai,WF2) |
| 1176 | | ), |
| 1177 | | (Ai=Ao, |
| 1178 | | get_priority_of_boolean_expression2(disjunct(LHS,RHS),StartPrio), |
| 1179 | | %get_wait_flag(Prio,not_conjunct,WF,WF2), |
| 1180 | | get_binary_choice_wait_flag_exp_backoff(StartPrio,not_conjunct,WF,WF2), |
| 1181 | | % TO DO: we could check if LHS or RHS are registered in Ai as pred_true/pred_false ?? |
| 1182 | | b_not_test_conjunction(LHS,RHS,LocalState,State,WF,WF2,Ai)) |
| 1183 | | ) |
| 1184 | | ). |
| 1185 | | |
| 1186 | | b_not_test_boolean_expression2(implication(LHS,RHS),_,LocalState,State,WF,Ai,Ao) :- !, |
| 1187 | | b_test_boolean_expression(LHS,LocalState,State,WF,Ai,Aii), |
| 1188 | | b_not_test_boolean_expression(RHS,LocalState,State,WF,Aii,Ao). |
| 1189 | | b_not_test_boolean_expression2(equivalence(LHS,RHS),_,LocalState,State,WF,Ai,Ao) :- !, |
| 1190 | | b_not_test_equivalence(LHS,RHS,LocalState,State,WF,Ai,Ao). |
| 1191 | | b_not_test_boolean_expression2(disjunct(LHS,RHS),_,LocalState,State,WF,Ai,Ao) :- !, |
| 1192 | | b_not_test_boolean_expression(LHS,LocalState,State,WF,Ai,Aii), |
| 1193 | | b_not_test_boolean_expression(RHS,LocalState,State,WF,Aii,Ao). |
| 1194 | | b_not_test_boolean_expression2(lazy_let_pred(Id,AssignmentExpr,Expr),_I,LocalState,State,WF,Ai,Ao) :- !, |
| 1195 | | add_lazy_let_id_and_expression(Id,AssignmentExpr,LocalState,State,NewLocalState,WF,Ai), |
| 1196 | | b_not_test_boolean_expression(Expr,NewLocalState,State,WF,Ai,Ao). % we could use Ai,Ao version as lazy lets never interfere with each other and never invalidate existing expressions |
| 1197 | | b_not_test_boolean_expression2(lazy_lookup_pred(Id),_Info,LocalState,_State,WF,Ai,Ao) :- !, Ai=Ao, |
| 1198 | | lookup_value_for_existing_id_wf(Id,LocalState,(Trigger,Value),WF), % should normally only occur in LocalState; value introduced by lazy_let |
| 1199 | | (Trigger,Value) = (pred_true,pred_false). % force evaluation |
| 1200 | | b_not_test_boolean_expression2(value(PredVal),_Info,_LocalState,_State,_WF,Ai,Ao) :- !, % this can occur when lazy_lookup_pred gets compiled by b_compiler |
| 1201 | | is_pred_false(PredVal),Ai=Ao. |
| 1202 | | b_not_test_boolean_expression2(Expr,Info,LocalState,State,WF,Ai,Ao) :- |
| 1203 | | % TO DO: we could put some of the operators into the Ai store (comparison operators) |
| 1204 | ? | b_not_test_atomic_boolean_expression2(Expr,Info,LocalState,State,WF,Ai,Ao). |
| 1205 | | |
| 1206 | | % Other boolean expressions, not involving boolean connectives |
| 1207 | | b_not_test_atomic_boolean_expression2(exists(Parameters,RHS),Infos,LocalState,State,WF,Ai,Ao) :- !, |
| 1208 | | /* #ID.(RHS) */ |
| 1209 | | Ai=Ao, |
| 1210 | | b_not_test_exists(Parameters,RHS,Infos,LocalState,State,compile,WF). |
| 1211 | | b_not_test_atomic_boolean_expression2(forall(Parameters,LHS,RHS),Infos,LocalState,State,WF,Ai,Ao) :- !, |
| 1212 | | /* !ID.(LHS => RHS) */ |
| 1213 | | Ai=Ao, |
| 1214 | | safe_create_texpr(negation(RHS),pred,Negation), |
| 1215 | | conjunct_predicates_with_pos_info(LHS,Negation,Condition), |
| 1216 | | % conjunct_predicates merges the used_ids Info in Condition |
| 1217 | ? | b_test_exists(Parameters,Condition,Infos,LocalState,State,WF). |
| 1218 | | /* TODO(DP, 2.8.2008): Extension for Z: a let-statement as predicate */ |
| 1219 | | b_not_test_atomic_boolean_expression2(let_predicate(Ids,AssignmentExprs,Pred),Infos,LocalState,State,WF,Ai,Ao) :- |
| 1220 | | !,Ai=Ao, % any expression inside the LET cannot be safely reused |
| 1221 | | set_up_localstate_for_let(Ids,ParaValues,AssignmentExprs,LocalState,State,LetState,WF), |
| 1222 | | opt_push_wait_flag_call_stack_quantifier_info(WF,let_quantifier,Ids,ParaValues,Infos,WF2), |
| 1223 | | b_not_test_boolean_expression(Pred,LetState,State,WF2). % NOT COVERED (10) |
| 1224 | | /* TODO(DP, 2.8.2008): */ |
| 1225 | | b_not_test_atomic_boolean_expression2(freetype_case(Freetype,Case,Expr),_,LocalState,State,WF,Ai,Ao) :- |
| 1226 | | !,Ai=Ao,b_compute_expression(Expr,LocalState,State,freeval(Freetype,RealCase,_),WF), |
| 1227 | | dif(RealCase,Case). % NOT COVERED (11) |
| 1228 | | b_not_test_atomic_boolean_expression2(finite(SetExpr),_,LocalState,State,WF,Ai,Ao) :- |
| 1229 | | !, Ai=Ao,b_compute_expression(SetExpr,LocalState,State,Value,WF), |
| 1230 | | kernel_call_predicate(is_infinite_set_wf(Value,WF),kernel_objects,WF,finite(SetExpr)). |
| 1231 | | /* Extension for Kodkod */ |
| 1232 | | b_not_test_atomic_boolean_expression2(kodkod(ProblemId,_),_,LocalState,State,WF,Ai,Ao) :- |
| 1233 | | !,Ai=Ao,get_wait_flag1(kodkod_negative_call,WF,WF0), |
| 1234 | | kodkod_request(ProblemId,neg,LocalState,State,WF0). % NOT COVERED (12) |
| 1235 | | b_not_test_atomic_boolean_expression2(partition(LHS,RHS),Info,LocalState,State,WF,Ai,Ao) :- !, |
| 1236 | | b_interpreter_check:register_predicate(partition(LHS,RHS),Info,pred_false,Reused,Ai,Ao), |
| 1237 | | (Reused==true -> true |
| 1238 | | ; b_compute_expression(LHS,LocalState,State,SV1,WF), |
| 1239 | | l_compute_expression(RHS,LocalState,State,SV2,WF), % RHS is Prolog list of expressions |
| 1240 | | kernel_call_predicate(not_partition_wf(SV1,SV2,WF),kernel_objects,WF,partition(LHS,RHS)) |
| 1241 | | ). |
| 1242 | | b_not_test_atomic_boolean_expression2(external_pred_call(FunName,Args),Info,LocalState,State,WF,Ai,Ao) :- |
| 1243 | | !, Ai=Ao, |
| 1244 | | (do_not_evaluate_args(FunName) -> EvaluatedArgs=[] |
| 1245 | | ; b_compute_expressions(Args, LocalState,State, EvaluatedArgs, WF)), |
| 1246 | | push_wait_flag_call_stack_info(WF,external_call(FunName,EvaluatedArgs,Info),WF2), |
| 1247 | | call_external_predicate(FunName,Args,EvaluatedArgs,LocalState,State,pred_false,Info,WF2). |
| 1248 | | b_not_test_atomic_boolean_expression2(Pred,Info,LocalState,State,WF,Ai,Ao) :- |
| 1249 | | b_interpreter_check:register_predicate(Pred,Info,pred_false,Reused,Ai,Ao), |
| 1250 | | (Reused==true |
| 1251 | | -> %% print('REUSED (not): '), translate:print_bexpr(Pred),nl, |
| 1252 | | true |
| 1253 | ? | ; b_not_test_atomic_boolean_expression3(Pred,Info,LocalState,State,WF)). |
| 1254 | | b_not_test_atomic_boolean_expression3(Expression,Info,LocalState,State,WF) :- |
| 1255 | | functor(Expression,BOP,2), |
| 1256 | ? | kernel_mappings:negate_binary_boolean_operator(BOP,NBOP), |
| 1257 | | arg(1,Expression,Arg1),arg(2,Expression,Arg2),!, |
| 1258 | | NExpression =.. [NBOP,Arg1,Arg2], |
| 1259 | ? | b_test_atomic_boolean_expression2(NExpression,Info,LocalState,State,WF). |
| 1260 | | b_not_test_atomic_boolean_expression3(Expression,Info,LocalState,State,WF) :- |
| 1261 | | functor(Expression,BOP,2), |
| 1262 | | kernel_mappings:negate_binary_boolean_operator_swap(BOP,NBOP), |
| 1263 | | arg(1,Expression,Arg1),arg(2,Expression,Arg2),!, |
| 1264 | | NExpression =.. [NBOP,Arg2,Arg1], |
| 1265 | | b_test_atomic_boolean_expression2(NExpression,Info,LocalState,State,WF). |
| 1266 | | b_not_test_atomic_boolean_expression3(E,_,_,_,_WF) :- |
| 1267 | | add_internal_error('Uncovered boolean expression (not): ',E), |
| 1268 | | print_functor(E),nl,fail. |
| 1269 | | |
| 1270 | | |
| 1271 | | % test RHS of not-conjunction depending on outcome of LHS |
| 1272 | | :- block b_not_test_conjunction_rhs(-,?,?,?,?,?,-). |
| 1273 | | b_not_test_conjunction_rhs(pred_false,_RHS,_LocalState,_State,_WF,_Ai,_LWF). |
| 1274 | | b_not_test_conjunction_rhs(pred_true,RHS,LocalState,State,WF,Ai,_LWF) :- |
| 1275 | ? | b_not_test_boolean_expression(RHS,LocalState,State,WF,Ai,_). |
| 1276 | | |
| 1277 | | /* -----------------------------*/ |
| 1278 | | /* b_compute_expression */ |
| 1279 | | /* -----------------------------*/ |
| 1280 | | |
| 1281 | | |
| 1282 | | |
| 1283 | | :- assert_must_succeed((b_interpreter:b_compute_expression(b(pow_subset( |
| 1284 | | b(identifier(nm),set(integer),[])),set(set(integer)),[]), |
| 1285 | | [], [bind(nm,[])],R,_WF), |
| 1286 | | nonvar(R), custom_explicit_sets:expand_custom_set_to_list(R,[[]]))). |
| 1287 | | :- assert_must_succeed((b_interpreter:b_compute_expression(b(pow_subset( |
| 1288 | | b(identifier(nm),set(boolean),[])),set(set(boolean)),[]),[], |
| 1289 | | [bind(nm,[pred_false /* bool_false */])],R,_WF), |
| 1290 | | nonvar(R), custom_explicit_sets:expand_custom_set_to_list(R,ER), |
| 1291 | | kernel_objects:equal_object(ER,[[pred_false /* bool_false */],[]]))). |
| 1292 | | |
| 1293 | | :- assert_pre(b_interpreter:b_compute_expression(Exp,LS,State,_Val,WF), |
| 1294 | | (nonvar(Exp), |
| 1295 | | bsyntaxtree:check_if_typed_expression(Exp), |
| 1296 | | type_check(LS,store),type_check(State,store), type_check(WF,wait_flag))). |
| 1297 | | :- assert_post(b_interpreter:b_compute_expression(_E,_LS,_State,Val,WF), |
| 1298 | | (b_interpreter:value_type(Val), type_check(WF,wait_flag))). %, nonvar(Val) |
| 1299 | | |
| 1300 | | b_compute_expression_nowf(E,LS,S,R) :- |
| 1301 | | b_compute_expression_nowf(E,LS,S,R,none,0). |
| 1302 | | |
| 1303 | | b_compute_expression_nowf(E,LS,S,R,FormulaKind,Nr) :- |
| 1304 | | get_texpr_info(E,Info), |
| 1305 | | b_compute_expression_nowf(E,LS,S,R,FormulaKind,Nr,Info). |
| 1306 | | b_compute_expression_nowf(E,LS,S,R,FormulaKind,Nr,Span) :- |
| 1307 | | b_compute_expression_no_wf8(E,LS,S,R,FormulaKind,Nr,default,Span). |
| 1308 | | % a variation where we tell ProB to expect non-symbolic / explicit values |
| 1309 | | b_compute_explicit_epression_no_wf(E,LS,S,R,FormulaKind,Nr) :- |
| 1310 | | get_texpr_info(E,Info), |
| 1311 | | b_compute_explicit_epression_no_wf(E,LS,S,R,FormulaKind,Nr,Info). |
| 1312 | | b_compute_explicit_epression_no_wf(E,LS,S,R,FormulaKind,Nr,Span) :- |
| 1313 | | b_compute_expression_no_wf8(E,LS,S,R,FormulaKind,Nr,expect_explicit_value,Span). |
| 1314 | | b_compute_expression_no_wf8(E,LS,S,R,FormulaKind,Nr,Expect,Span) :- |
| 1315 | | %add_message(b_compute_expression,'Called Version wo WFLAGS: ', b_compute_expression(E,LS,S,R)), |
| 1316 | | (Expect=expect_explicit_value -> WFInfos = [expect_explicit_value] ; WFInfos = []), |
| 1317 | | (FormulaKind = none -> init_wait_flags(WF,WFInfos) |
| 1318 | | ; CallStackEntry = prob_command_context(eval_expr_command(FormulaKind,Nr),Span), |
| 1319 | | init_wait_flags_and_push_call_stack(no_wf_available,CallStackEntry,WFInfos,WF) |
| 1320 | | ), |
| 1321 | | b_compute_expression(E,LS,S,R,WF), |
| 1322 | | ground_wait_flags(WF), % this will add abort errors and fail before enumerating the result: |
| 1323 | | init_wait_flags(WF2,[b_compute_expression_nowf2]), |
| 1324 | | get_texpr_type(E,Type), |
| 1325 | | b_enumerate:b_tighter_enumerate_single_value(R,Type,Span,b_compute_expression_nowf,WF2), %should rarely be necessary |
| 1326 | | % a non-ground result only occurs when WD error appears and in this case ground_wait_flags will fail after |
| 1327 | | % adding the abort error (and thus no value is returned) |
| 1328 | | % exceptions are test 1066 with a complicated set comprehension used in a function application |
| 1329 | | % test 1861 relies on two phase enumeration |
| 1330 | | ground_wait_flags(WF2). |
| 1331 | | |
| 1332 | | l_compute_expression([],_LS,_S,[],_WF). |
| 1333 | | l_compute_expression([H|T],LS,S,[CH|CT],WF) :- |
| 1334 | | b_compute_expression(H,LS,S,CH,WF), |
| 1335 | | l_compute_expression(T,LS,S,CT,WF). |
| 1336 | | |
| 1337 | | :- if(environ(prob_debug_watch_flag,true)). |
| 1338 | | b_compute_expressiond(b(Expr,Type,Info),LS,S,R,WF) :- !, |
| 1339 | | ((ground(Type), Type \== pred, Type \== subst) -> true |
| 1340 | | ; add_error_wf(b_compute_expression,'Expression has illegal type: ',b(Expr,Type,Info),WF)), |
| 1341 | | check_value(R,b(Expr,Type,Info)), |
| 1342 | | b_compute_expression2(Expr,Type,Info,LS,S,R,WF). |
| 1343 | | :- block check_value(-,?). |
| 1344 | | check_value(pred_true,_) :- !. check_value(pred_false,_) :- !. |
| 1345 | | check_value(string(_),_) :- !. |
| 1346 | | check_value([],_) :- !. |
| 1347 | | check_value([H|T],E) :- !, check_value(H,E), check_value(T,E). |
| 1348 | | check_value((A,B),E) :- !, check_value(A,E), check_value(B,E). |
| 1349 | | check_value(fd(N,T),E) :- !, (nonvar(T) -> check_fd(N,T,E) ; check_err(fd(N,T),E)). |
| 1350 | | check_value(global_set(GS),E) :- !,(nonvar(GS) -> true ; check_err(global_set(GS),E)). |
| 1351 | | check_value(avl_set(AS),E) :- !,((nonvar(AS),AS=node(_,_,_,_,_)) -> true ; check_err(global_set(AS),E)). |
| 1352 | | check_value(int(X),E) :- !,check_int(X,E). |
| 1353 | | check_value(closure(P,T,B),E) :- !, |
| 1354 | | (ground(P),ground(T),nonvar(B) |
| 1355 | | -> (same_length(P,T) -> bsyntaxtree:check_ast(B) ; check_err(closure_not_same_length(P,T))) |
| 1356 | | ; check_err(closure(P,T,B),E)). |
| 1357 | | check_value(rec(_),_) :- !. %TODO: check |
| 1358 | | check_value(X,E) :- check_err(uncovered(X),E). |
| 1359 | | % TO DO: add records + more thorough checking of global_set and closures and avl_sets |
| 1360 | | :- block check_int(-,?). |
| 1361 | | check_int(X,E) :- (number(X) -> true ; check_err(int(X),E)). |
| 1362 | | :- block check_fd(-,?,?). |
| 1363 | | check_fd(X,T,E) :- (number(X),b_get_fd_type_bounds(T,Low,Up),X>=Low,X=<Up -> true ; check_err(fd(X,T),E)). |
| 1364 | | |
| 1365 | | check_err(V,E) :- add_internal_error('Illegal value when computing expression: ',V:E). |
| 1366 | | b_compute_expression(E,LS,S,R,WF) :- !, |
| 1367 | | (waitflag0_is_set(WF) |
| 1368 | | -> debug:watch(100,b_interpreter:b_compute_expressiond(E,LS,S,R,WF)) |
| 1369 | | ; debug:watch_det(100,b_interpreter:b_compute_expressiond(E,LS,S,R,WF))). |
| 1370 | | :- else. |
| 1371 | | b_compute_expression(b(Expr,Type,Info),LS,S,R,WF) :- !, |
| 1372 | | ((ground(Type), Type \== pred, Type \== subst) -> true |
| 1373 | | ; Type==pred -> add_internal_error_wf(b_interpreter,'Expected expression, not predicate: ',b(Expr,Type,Info),Info,WF) |
| 1374 | | ; Type==subst -> add_internal_error_wf(b_interpreter,'Expected expression, not substitution: ',b(Expr,Type,Info),Info,WF) |
| 1375 | | ; add_internal_error_wf(b_interpreter,'Expression has non-ground type: ',b(Expr,Type,Info),Info,WF) |
| 1376 | | ), |
| 1377 | ? | b_compute_expression2(Expr,Type,Info,LS,S,R,WF). |
| 1378 | | :- endif. |
| 1379 | | b_compute_expression(Expr,LS,S,R,WF) :- |
| 1380 | | add_internal_error('Expression not properly wrapped: ',b_compute_expression(Expr,LS,S,R,WF)), |
| 1381 | | b_compute_expression2(Expr,unknown,[],LS,S,R,WF). |
| 1382 | | |
| 1383 | | |
| 1384 | | :- use_module(bsyntaxtree,[get_texpr_set_type/2]). |
| 1385 | | |
| 1386 | | %:- use_module(b_global_sets,[b_type2_set/2]). |
| 1387 | | %b_compute_expression2(V,T,I,LS,S,R,_WF) :- nonvar(R), print('result_instantiated: '(R:V)),nl,fail. |
| 1388 | | b_compute_expression2(value(Val),_T,_I,_LS,_S,V,WF) :- !, equal_object_wf(Val,V,WF). %Val=V. |
| 1389 | | b_compute_expression2(boolean_true,_T,_I,_LState,_State,Res,_WF) :- !, Res = pred_true /* bool_true */. % for simple types we can use ordinary unification |
| 1390 | | b_compute_expression2(boolean_false,_T,_I,_LState,_State,Res,_WF) :- !, Res = pred_false /* bool_false */. |
| 1391 | | b_compute_expression2(integer_set(NSET),_T,_I,_LState,_State,Res,WF) :- !, equal_object_wf(Res,global_set(NSET),WF). %Res = global_set(NSET). |
| 1392 | | b_compute_expression2(bool_set,_T,_I,_LState,_State,Res,WF) :- !, % b_type2_set(bool,Res). |
| 1393 | | equal_object_wf(Res,avl_set(node(pred_false,true,1,empty,node(pred_true,true,0,empty,empty))),WF). |
| 1394 | | % was equal_object(Res,[pred_true /* bool_true */,pred_false /* bool_false */]) % changing this to avl used to break test 800 Bosch Deadlock v9; probably because of identity closure detection instantiation |
| 1395 | | b_compute_expression2(float_set,_T,_I,_LState,_State,Res,WF) :- !, equal_object_wf(Res,global_set('FLOAT'),WF). |
| 1396 | | b_compute_expression2(real_set,_T,_I,_LState,_State,Res,WF) :- !, equal_object_wf(Res,global_set('REAL'),WF). |
| 1397 | | b_compute_expression2(string_set,_T,_I,_LState,_State,Res,WF) :- !, equal_object_wf(Res,global_set('STRING'),WF). |
| 1398 | | % was all_strings(Res). %% NOT COVERED (6) |
| 1399 | | b_compute_expression2(string(S),_T,_I,_LState,_State,Res,_WF) :- !, |
| 1400 | | % TODO(ML,26.8.2008): Check if we should use a different functor for syntax tree vs data |
| 1401 | | Res = string(S). |
| 1402 | | b_compute_expression2(typeset,Type,_I,_LState,_State,Res,WF) :- !, |
| 1403 | | is_set_type(Type,ElType), % we could generate global_set('STRING'),... for certain types |
| 1404 | | equal_object_wf(Res,closure('_zzzz_unary',[ElType],b(truth,pred,[])),WF). |
| 1405 | | b_compute_expression2(empty_set,_T,_I,_LState,_State,Res,_WF) :- !, empty_set(Res). |
| 1406 | | b_compute_expression2(empty_sequence,_T,_I,_LState,_State,Res,_WF) :- !, empty_sequence(Res). |
| 1407 | | b_compute_expression2(event_b_identity,EType,_I,_LState,_State,Res,WF) :- |
| 1408 | | get_set_type(EType,couple(Type,Type)),!, |
| 1409 | | event_b_identity_for_type(Type,Res,WF). |
| 1410 | | b_compute_expression2(integer(Val),_T,_I,_LocalState,_State,Res,_WF) :- !, Res = int(Val). |
| 1411 | | b_compute_expression2(max_int,_T,_I,_LS,_S,Res,_WF) :- |
| 1412 | | preferences:get_preference(maxint,CVal),!, Res = int(CVal). |
| 1413 | | b_compute_expression2(min_int,_T,_I,_LS,_S,Res,_WF) :- |
| 1414 | | preferences:get_preference(minint,CVal),!, Res = int(CVal). |
| 1415 | | b_compute_expression2(identifier(Id),Type,Info,LocalState,State,Res,WF) :- !, |
| 1416 | | lookup_value_in_store_and_global_sets_wf(Id,Type,LocalState,State,Value,Info,WF), |
| 1417 | | equal_object_wf(Res,Value,WF). |
| 1418 | | b_compute_expression2(lazy_lookup_expr(Id),_Type,_Info,LocalState,_State,Res,WF) :- !, |
| 1419 | | lookup_value_for_existing_id_wf(Id,LocalState,(Trigger,Value),WF), % should normally only occur in LocalState; value introduced by lazy_let |
| 1420 | | Trigger = pred_true, % force evaluation |
| 1421 | | equal_object_wf(Res,Value,WF). |
| 1422 | | b_compute_expression2(lazy_let_expr(Id,AssignmentExpr,Expr),_T,_I,LocalState,State,Value,WF) :- |
| 1423 | | !, |
| 1424 | | add_lazy_let_id_and_expression(Id,AssignmentExpr,LocalState,State,NewLocalState,WF), |
| 1425 | | %Trigger = pred_true, % expressions cannot be delayed !? (TO DO: more refined check) |
| 1426 | | %block_lazy_compute_expression(Trigger,_,AssignmentExpr,LocalState,State,IdValue,WF,Ai), |
| 1427 | | b_compute_expression(Expr,NewLocalState,State,Value,WF). |
| 1428 | | b_compute_expression2(real(Atom),_T,_I,_LocalState,_State,Res,_WF) :- !, construct_real(Atom,Res). |
| 1429 | | b_compute_expression2(rec(Fields),_T,_I,LocalState,State,Record,WF) :- !, |
| 1430 | | l_compute_field(Fields,LocalState,State,FieldValues,WF), |
| 1431 | | construct_record_wf(FieldValues,Record,WF). |
| 1432 | | b_compute_expression2(record_field(RecEx,Name),_T,_I,LocalState,State,Value,WF) :- !, |
| 1433 | | b_compute_expression(RecEx,LocalState,State,RecValue,WF), |
| 1434 | | access_record_wf(RecValue,Name,Value,WF). |
| 1435 | | b_compute_expression2(freetype_set(Id),_T,_I,_LState,_State,Val,_WF) :- !, Val=freetype(Id). %% NOT COVERED (17) |
| 1436 | | b_compute_expression2(function(Function,FunArg),Type,Info,LocalState,State,Res,WF) :- !, |
| 1437 | | b_compute_expression_function(Function,FunArg,Type,Info,LocalState,State,Res,WF). |
| 1438 | | b_compute_expression2(card(b(Arg1,Type,Info)),integer,OuterInfo,LocalState,State,Card,WF) :- !, |
| 1439 | ? | b_compute_card(Arg1,Type,Info,OuterInfo,LocalState,State,Card,WF). |
| 1440 | | b_compute_expression2(max(b(Arg1,Type,Info)),integer,_I,LocalState,State,Max,WF) :- !, |
| 1441 | | b_compute_max(Arg1,Type,Info,LocalState,State,Max,WF). |
| 1442 | | b_compute_expression2(min(b(Arg1,Type,Info)),integer,_I,LocalState,State,Min,WF) :- !, |
| 1443 | ? | b_compute_min(Arg1,Type,Info,LocalState,State,Min,WF). |
| 1444 | | b_compute_expression2(set_extension(Ex),Type,_I,LocalState,State,Res,WF) :- !, |
| 1445 | | b_compute_expressions(Ex,LocalState,State,ExValue,WF), |
| 1446 | | kernel_call(b_interpreter:convert_list_of_expressions_into_set_wf(ExValue,ValueSet,Type,WF),ExValue,WF, set_extension(Ex)), |
| 1447 | | equal_object_wf(Res,ValueSet,WF). |
| 1448 | | %kernel_call_convert_list_of_expressions_into_set_wf(ExValue,ValueSet,Type, WF). |
| 1449 | | %b_compute_expression('ListTermes'(ListOfEx),LocalState,State,ValueSet,WF) :- !, |
| 1450 | | % /* convert list of expressions of CASE statement into set of elements */ |
| 1451 | | % b_compute_expression(ListOfEx,LocalState,State,Value,WF), |
| 1452 | | % init_wait_flags(WF),convert_list_of_expressions_into_set_wf(Value,ValueSet,set(any),WF),ground_wait_flags(WF). |
| 1453 | | b_compute_expression2(sequence_extension(Ex),_T,_I,LocalState,State,ValueSeq,WF) :- !, |
| 1454 | | b_compute_expressions(Ex,LocalState,State,Value,WF), |
| 1455 | | kernel_call(b_interpreter:convert_list_of_expressions_into_sequence(Value,ValueSeq),Value,WF,sequence_extension(Ex)). |
| 1456 | | b_compute_expression2(convert_bool(PRED),_T,_I,LocalState,State,Val,WF) :- !, /* B bool(.) operator */ |
| 1457 | | b_convert_bool(PRED,LocalState,State,WF,Val). |
| 1458 | | b_compute_expression2(couple(El1,El2),_T,_I,LocalState,State,Result,WF) :- !, Result = (Val1,Val2), |
| 1459 | | b_compute_expression(El1,LocalState,State,Val1,WF), |
| 1460 | | b_compute_expression(El2,LocalState,State,Val2,WF). |
| 1461 | | b_compute_expression2(comprehension_set(Parameters,Condition),_T,Info,LocalState,State,Result,WF) :- !, |
| 1462 | ? | b_compute_comprehension_set(Parameters,Condition,Info,LocalState,State,Result,WF). |
| 1463 | | b_compute_expression2(recursive_let(TId,TSet),OrigType,OrigInfo,LocalState,State,Result,WF) :- !, |
| 1464 | | % LET TId = TSet in TSet |
| 1465 | | ( get_texpr_expr(TSet,comprehension_set(Parameters,Condition)) -> |
| 1466 | | true % if the argument TSet is a comprehension set, |
| 1467 | | % we just add a recursive parameter |
| 1468 | | ; get_texpr_set_type(TSet,Type) -> |
| 1469 | | % if not, we construct a new comprehension set {x | x:TSet} |
| 1470 | | unique_typed_id("_tmparg_",Type,TArg), |
| 1471 | | Parameters=[TArg], |
| 1472 | | safe_create_texpr(member(TArg,TSet),pred,Condition) |
| 1473 | | ; |
| 1474 | | add_internal_error('Expected set as argument to recursive_let', |
| 1475 | | b_compute_expression2(recursive_let(TId,TSet),OrigType,OrigInfo,LocalState,State,Result,WF)),fail |
| 1476 | | ), |
| 1477 | | % Generate closure where the references to the recursion are kept |
| 1478 | | % add recursion ID to parameters to prevent removal of references during compilation |
| 1479 | | generate_recursive_closure(TId,recursion,[TId|Parameters],Condition,LocalState,State,Closure1,WF), |
| 1480 | | % add_message(b_interpreter,'Recursive Let for: ',TId,TId), |
| 1481 | | % remove recursion ID from parameters after compilation |
| 1482 | | Closure1=closure([_|P],[_|T],Cond),RClosure=closure(P,T,Cond), |
| 1483 | | % Generate closure where the references to the recursion are kept |
| 1484 | | generate_recursive_closure(TId,RClosure,Parameters,Condition,LocalState,State,Result,WF). |
| 1485 | | b_compute_expression2(general_sum(Ids,Condition,Expression),Type,Info,LocalState,State,SumResult,WF) :- !, |
| 1486 | | b_general_sum_or_mul(Ids,Condition,Expression,Type,Info,LocalState,State,SumResult,WF,sum). |
| 1487 | | b_compute_expression2(general_product(Ids,Condition,Expression),Type,Info,LocalState,State,MulResult,WF) :- !, |
| 1488 | | b_general_sum_or_mul(Ids,Condition,Expression,Type,Info,LocalState,State,MulResult,WF,mul). |
| 1489 | | /* Begin: Extensions for Z */ |
| 1490 | | b_compute_expression2(if_then_else(If,Then,Else),T,_I,LocalState,State,Value,WF) :- !, |
| 1491 | | opt_push_wait_flag_call_stack_info(WF,b_expr_call(if_test,If),WF2), |
| 1492 | | b_try_check_boolean_expression_wf(If,LocalState,State,WF2,PredRes), |
| 1493 | | (var(PredRes), can_get_full_fd_value(T), preferences:preference(use_clpfd_solver,true), |
| 1494 | | always_well_defined(Then), always_well_defined(Else) |
| 1495 | | % Note: always_well_defined_or_disprover_mode probably not ok, as we evaluate the expressions unconditionally |
| 1496 | | -> b_clpfd_if_then_else(PredRes,T,Then,Else,LocalState,State,Value,WF) |
| 1497 | | ; b_compute_if_then_else(PredRes,Then,Else,LocalState,State,Value,WF) |
| 1498 | | ). |
| 1499 | | b_compute_expression2(let_expression_global(Ids,AssignmentExprs,Expr),_T,Info,LocalState,State,Value,WF) :- |
| 1500 | | debug_println(4,global_let(Ids,Info)), |
| 1501 | | % store variables globally (not in LocalState) to be visible to subsidiary operation_calls |
| 1502 | | !, set_up_localstate_for_global_let(Ids,AssignmentExprs,LocalState,State,LetState,WF), |
| 1503 | | b_compute_expression(Expr,LocalState,LetState,Value,WF). |
| 1504 | | b_compute_expression2(let_expression(Ids,AssignmentExprs,Expr),_T,_I,LocalState,State,Value,WF) :- |
| 1505 | | !, set_up_localstate_for_let(Ids,_ParaValues,AssignmentExprs,LocalState,State,LetState,WF), |
| 1506 | | b_compute_expression(Expr,LetState,State,Value,WF). %% NOT COVERED (30) |
| 1507 | | b_compute_expression2(freetype_constructor(Id,Case,Expr),_T,_I,LocalState,State,FreeValue,WF) :- |
| 1508 | | !, FreeValue = freeval(Id,Case,Value), |
| 1509 | | b_compute_expression(Expr,LocalState,State,Value,WF). %% NOT COVERED (32) |
| 1510 | | b_compute_expression2(freetype_destructor(Id,Case,Expr),_T,Info,LocalState,State,Value,WF) :- |
| 1511 | | !, b_compute_expression(Expr,LocalState,State,freeval(Id,VCase,VValue),WF), |
| 1512 | | check_freetype_case(Id,Case,VCase,VValue,Value,Info,WF). |
| 1513 | | /* End: Extensions for Z */ |
| 1514 | | b_compute_expression2(assertion_expression(Cond,ErrMsg,Expr),T,Info,LocalState,State,Value,WF) :- !, |
| 1515 | | opt_push_wait_flag_call_stack_info(WF, |
| 1516 | | span_predicate(b(assertion_expression(Cond,ErrMsg,b(value(string('')),string,[])),T,Info),LocalState,State),WF2), |
| 1517 | | % should we use another term for call stack? |
| 1518 | | (get_preference(disprover_mode,true) -> PredRes = pred_true ; true), % in Disprover mode we know Cond must be true |
| 1519 | | b_try_check_boolean_expression_no_enum_wf(Cond,LocalState,State,WF2,PredRes), % DO NOT ENUM, unless we cannot reify |
| 1520 | | % does not make sense to enumerate PredRes as with pred_false we get no value anyway |
| 1521 | | % however, we may get pending co-routines if Cond cannot be reified |
| 1522 | | ( PredRes==pred_false -> |
| 1523 | | translate_bexpression_with_limit(Cond,100,CS), |
| 1524 | | add_wd_error_span(ErrMsg,CS,span_predicate(Cond,LocalState,State),WF2), |
| 1525 | | get_last_wait_flag(assertion_expression,WF2,WFC), |
| 1526 | | when(nonvar(WFC), |
| 1527 | | (ground_value(Value) -> true % we have a WD Error, any Value goes |
| 1528 | | ; b_compute_expression(Expr,LocalState,State,Value,WF2) % compute Value; avoid infinite enumeration of possible values; useful e.g. for prj1/prj2 assertion expressions |
| 1529 | | )) |
| 1530 | | ; b_compute_expression(Expr,LocalState,State,Value,WF), |
| 1531 | | add_wd_error_if_false(PredRes,ErrMsg,Cond,span_predicate(Cond,LocalState,State),WF2) |
| 1532 | | ). %% NOT COVERED (34) |
| 1533 | | b_compute_expression2(multiplication(Arg1,Arg2),integer,Info,LocalState,State,Value,WF) :- |
| 1534 | | same_texpr(Arg1,Arg2), !, % is less useful when CLPFD is already turned on, calls square(Arg1,Value,WF) |
| 1535 | | b_compute_expression(Arg1,LocalState,State,SV1,WF), |
| 1536 | | unary_kernel_call(kernel_objects,square,SV1,Value,WF,multiplication(Arg1,Arg1),integer,Info). |
| 1537 | | b_compute_expression2(image(Relation,Set),_EType,_Info,LocalState,State,Value,WF) :- |
| 1538 | | special_operator_for_image(Relation,Kind,Args), |
| 1539 | | % we have closure1(Rel)[Set] or similar -> avoid computing full closure |
| 1540 | | !, |
| 1541 | | b_compute_expression(Set,LocalState,State,SV,WF), |
| 1542 | | b_compute_expressions(Args,LocalState,State,EArgs,WF), |
| 1543 | ? | kernel_call(bsets_clp:image_for_special_operator(Kind,EArgs,SV,Value,WF),[SV|EArgs],WF,image(Relation,Set)). |
| 1544 | | b_compute_expression2(external_function_call(FunName,Args),EType,Info,LocalState,State,Value,WF) :- |
| 1545 | | !, |
| 1546 | | b_compute_expressions(Args, LocalState,State, EvaluatedArgs, WF), |
| 1547 | | push_wait_flag_call_stack_info(WF,external_call(FunName,EvaluatedArgs,Info),WF2), |
| 1548 | | call_external_function(FunName,Args,EvaluatedArgs,Value,EType,Info,WF2). |
| 1549 | | b_compute_expression2(Expression,EType,Info,LocalState,State,Res,WF) :- |
| 1550 | | is_set_type(EType,Type), |
| 1551 | | /* avoid evaluating certain expensive expressions: |
| 1552 | | convert them into symbolic closures and expand only if really required */ |
| 1553 | | functor(Expression,UnOp,1), |
| 1554 | | symbolic_closure_unary_operator(UnOp), |
| 1555 | | /* TO DO: UNFOLD THIS OR WRAP in CONSTRUCTOR */ |
| 1556 | | arg(1,Expression,BType), |
| 1557 | | !, %preferences:get_preference(convert_types_into_closures,true),!, |
| 1558 | | % print_message(delayed_un_op(UnOp,BType)), |
| 1559 | ? | b_compute_expression(BType,LocalState,State,ArgValue,WF), |
| 1560 | | ( do_not_keep_symbolic_unary(UnOp,ArgValue), |
| 1561 | | unary_function(UnOp,Module,KernelFunction) -> |
| 1562 | | /* special treatment for empty set or other situations: no need to keep symbolic */ |
| 1563 | ? | unary_kernel_call(Module,KernelFunction,ArgValue,Res,WF,Expression,EType,Info) |
| 1564 | | ; |
| 1565 | | get_texpr_type(BType,ArgType), |
| 1566 | | create_texpr(value(ArgValue),ArgType,[],TValue), |
| 1567 | | ClosureSetExpression =.. [UnOp,TValue], |
| 1568 | | construct_member_closure('_zzzz_unary',Type,Info,ClosureSetExpression,Value), |
| 1569 | ? | ((unop_to_be_marked_as_symbolic(UnOp,ArgValue) ; member(prob_annotation('SYMBOLIC'),Info)) -> |
| 1570 | | mark_closure_as_symbolic(Value,SValue), |
| 1571 | | equal_object_wf(Res,SValue,WF) |
| 1572 | | ; equal_object_wf(Res,Value,WF)) |
| 1573 | | |
| 1574 | | ). |
| 1575 | | b_compute_expression2(Expression,EType,Info,LocalState,State,Res,WF) :- |
| 1576 | | is_set_type(EType,Type), |
| 1577 | | /* avoid evaluating certain expensive expressions: |
| 1578 | | convert them into symbolic closures and expand only if really required */ |
| 1579 | | functor(Expression,BinOp,2), |
| 1580 | | arg(1,Expression,BType1), |
| 1581 | | arg(2,Expression,BType2), |
| 1582 | | kernel_mappings:symbolic_closure_binary_operator(BinOp), |
| 1583 | | !, |
| 1584 | | %preferences:get_preference(convert_types_into_closures,true),!, |
| 1585 | | b_compute_expression(BType1,LocalState,State,ArgValue1,WF), |
| 1586 | | (binary_arg1_determines_value(BinOp,ArgValue1,DetResult) |
| 1587 | | -> /* print(det(BinOp,ArgValue1)),nl, */ |
| 1588 | | equal_object_wf(Res,DetResult,b_compute_expression2(BinOp),WF) |
| 1589 | | ; b_compute_expression(BType2,LocalState,State,ArgValue2,WF), |
| 1590 | | % print_message(delayed_bin_op(BinOp,ArgValue1,ArgValue2)), |
| 1591 | | (is_definitely_empty(BinOp,ArgValue1,ArgValue2) |
| 1592 | | -> empty_set(Res) |
| 1593 | | ; if(do_not_keep_symbolic(BinOp,ArgValue1,ArgValue2,ERes,WF), |
| 1594 | | % then: |
| 1595 | | equal_object_wf(Res,ERes,b_compute_expression2(BinOp),WF), |
| 1596 | | % else: |
| 1597 | | (get_texpr_type(BType1,TypeArg1), create_texpr(value(ArgValue1),TypeArg1,[],TArg1), |
| 1598 | | get_texpr_type(BType2,TypeArg2), create_texpr(value(ArgValue2),TypeArg2,[],TArg2), |
| 1599 | | ClosureSetExpression =.. [BinOp,TArg1,TArg2], |
| 1600 | | construct_member_closure('_zzzz_unary',Type,Info,ClosureSetExpression,Value), |
| 1601 | | % print('Constructed Symbolic Closure : '), translate:print_bvalue(Value),nl, |
| 1602 | ? | (binop_to_be_marked_as_symbolic(BinOp,ArgValue1,ArgValue2) -> |
| 1603 | | mark_closure_as_symbolic(Value,SValue), |
| 1604 | | equal_object_wf(Res,SValue,WF) |
| 1605 | | ; equal_object_wf(Res,Value,WF)) |
| 1606 | | ) |
| 1607 | | ) % if |
| 1608 | | ) |
| 1609 | | ). |
| 1610 | | b_compute_expression2(Expression,Type,Info,LocalState,State,Value,WF) :- |
| 1611 | | functor(Expression,Op,1), |
| 1612 | | unary_function(Op,Module,KernelFunction), |
| 1613 | | arg(1,Expression,Arg1),!, |
| 1614 | | %opt_push_wait_flag_call_stack_info(WF,b_operator_arg_evaluation(Op,1,[SV1],Arg1),WF1), |
| 1615 | ? | b_compute_expression(Arg1,LocalState,State,SV1,WF), |
| 1616 | ? | unary_kernel_call(Module,KernelFunction,SV1,Value,WF,Expression,Type,Info). |
| 1617 | | b_compute_expression2(Expression,Type,Info,LocalState,State,Value,WF) :- |
| 1618 | | functor(Expression,Op,2), |
| 1619 | | binary_function(Op,Module,KernelFunction), |
| 1620 | | arg(1,Expression,Arg1), |
| 1621 | | arg(2,Expression,Arg2),!, |
| 1622 | | %opt_push_wait_flag_call_stack_info(WF,b_operator_arg_evaluation(Op,1,[SV1,_],Arg1),WF1), |
| 1623 | ? | b_compute_expression(Arg1,LocalState,State,SV1,WF), |
| 1624 | | (binary_arg1_determines_value(Op,SV1,Result) % we could disable this if find_abort_values=true or if SV1 not guaranteed to be wd |
| 1625 | | -> |
| 1626 | | equal_object_wf(Value,Result,b_compute_expression2(Op),WF) |
| 1627 | ? | ; b_compute_expression(Arg2,LocalState,State,SV2,WF), |
| 1628 | | binary_kernel_call(Module,KernelFunction,SV1,SV2,Value,WF,Expression,Type,Info) |
| 1629 | | ). |
| 1630 | | b_compute_expression2(operation_call_in_expr(Operation,Parameters),_T,Info,LocalState,State,Value,WF) :- |
| 1631 | | !, def_get_texpr_id(Operation,op(OperationName)), |
| 1632 | | % TODO: check that this is a query operation; probably done in type checker |
| 1633 | | b_execute_operation_in_expression(OperationName,LocalState,State,Parameters,Value,Info,WF). |
| 1634 | | b_compute_expression2(E,T,Info,_L,_S,_R,_WF) :- |
| 1635 | | (T==pred -> add_internal_error('Uncovered expression; looks like a predicate in the wrong place: ',b(E,T,Info)) |
| 1636 | | ; T==subst -> add_internal_error('Uncovered expression; looks like a substitution in the wrong place: ',b(E,T,Info)) |
| 1637 | | ; requires_cleanup_pre(E) -> add_internal_error('Non-cleaned up ambiguous expression: ',b(E,T,Info)) |
| 1638 | | ; add_internal_error('Uncovered expression: ',b(E,T,Info)) |
| 1639 | | ), |
| 1640 | | print_functor(E), print(' : '), print(T),nl,fail. |
| 1641 | | |
| 1642 | | requires_cleanup_pre(mult_or_cart(_,_)). |
| 1643 | | requires_cleanup_pre(minus_or_set_subtract(_,_)). |
| 1644 | | |
| 1645 | | make_couplise([A],R) :- !,R=A. |
| 1646 | | make_couplise([A,B],R) :- !,R=(A,B). |
| 1647 | | make_couplise([A,B|Rest],Result) :- !, |
| 1648 | | make_couplise([(A,B)|Rest],Result). |
| 1649 | | make_couplise(A,_) :- add_internal_error('Illegal operation call result:',A),fail. |
| 1650 | | |
| 1651 | | % --- |
| 1652 | | |
| 1653 | | |
| 1654 | | % check if a symbolic_closure_unary_operator result should be marked with prob_annotation(SYMBOLIC) |
| 1655 | | % because direct sub-args are marked SYMBOLIC |
| 1656 | | unop_to_be_marked_as_symbolic(UnOp,Value) :- nonvar(Value), |
| 1657 | ? | to_be_marked_as_symbolic_aux(UnOp,Value). |
| 1658 | | to_be_marked_as_symbolic_aux(struct,rec(Fields)) :- !, |
| 1659 | ? | member(field(_,SymbolicClosure),Fields), |
| 1660 | | is_symbolic_closure(SymbolicClosure). |
| 1661 | | %to_be_marked_as_symbolic_aux(pow_subset,SymbolicClosure) :- is_symbolic_closure(SymbolicClosure). |
| 1662 | | %to_be_marked_as_symbolic_aux(pow1_subset,SymbolicClosure) :- is_symbolic_closure(SymbolicClosure). |
| 1663 | ? | to_be_marked_as_symbolic_aux(_,SymbolicClosure) :- is_symbolic_closure(SymbolicClosure). |
| 1664 | | |
| 1665 | | % check if a symbolic_closure_binary_operator result should be marked with prob_annotation(SYMBOLIC) |
| 1666 | | % because direct sub-args are marked SYMBOLIC |
| 1667 | | binop_to_be_marked_as_symbolic(interval,_,_) :- !, fail. |
| 1668 | | %binop_to_be_marked_as_symbolic(overwrite,_,_) :- !, true. % TO DO: fix by detecting infinite <+ closures |
| 1669 | ? | binop_to_be_marked_as_symbolic(set_subtraction,ArgVal1,_) :- !, is_symbolic_closure(ArgVal1). |
| 1670 | | binop_to_be_marked_as_symbolic(intersection,ArgVal1,ArgVal2) :- |
| 1671 | | (finite_reasonably_sized_set(ArgVal1) ; finite_reasonably_sized_set(ArgVal2)), !, |
| 1672 | | fail. % intersection is finite and reasonably sized |
| 1673 | ? | binop_to_be_marked_as_symbolic(_,ArgVal1,_) :- is_symbolic_closure(ArgVal1). |
| 1674 | ? | binop_to_be_marked_as_symbolic(_,_,ArgVal2) :- is_symbolic_closure(ArgVal2). |
| 1675 | | % --- |
| 1676 | | |
| 1677 | | % check if we have a finite known set that can be reasonably represented |
| 1678 | | % see small_known_set/1 is kernel_mappings |
| 1679 | | finite_reasonably_sized_set(AVL) :- nonvar(AVL), AVL=avl_set(_). % already expanded; so it can be represented in memory |
| 1680 | | finite_reasonably_sized_set(Closure) :- nonvar(Closure), |
| 1681 | | custom_explicit_sets:is_interval_closure(Closure,From,To), integer(From), integer(To), |
| 1682 | | 1+To-From < 250000. % what value should we use here? |
| 1683 | | |
| 1684 | | lazy_compute_expression(TRIGGER,AssignmentExpr,LocalState,State,IdValue,WF,Ai) :- |
| 1685 | | get_enumeration_finished_wait_flag(WF,F), |
| 1686 | | block_lazy_compute_expression(TRIGGER,F,AssignmentExpr,LocalState,State,IdValue,WF,Ai). |
| 1687 | | |
| 1688 | | :- block block_lazy_compute_expression(-,-,?,?,?, ?,?,?). |
| 1689 | | block_lazy_compute_expression(TRIGGER,_F,_AssignmentExpr,_LocalState,_State,_IdValue,_WF,_Ai) :- |
| 1690 | | var(TRIGGER),!. |
| 1691 | | block_lazy_compute_expression(pred_true,_,AssignmentExpr,LocalState,State,IdValue,WF,Ai) :- |
| 1692 | | AssignmentExpr = b(Expr,Type,Infos), |
| 1693 | | (Type \== pred -> b_compute_expression2(Expr,Type,Infos,LocalState,State,IdValue,WF) |
| 1694 | | ; IdValue == pred_true -> b_test_boolean_expression2(Expr,Infos,LocalState,State,WF,Ai,_) |
| 1695 | | ; IdValue == pred_false -> b_not_test_boolean_expression2(Expr,Infos,LocalState,State,WF,Ai,_) |
| 1696 | | ; b_check_boolean_expression(AssignmentExpr,LocalState,State,WF,PredVal,Ai,_) -> IdValue = PredVal |
| 1697 | | ; % reification of check_boolean_expression failed: |
| 1698 | | b_try_check_failed(AssignmentExpr,LocalState,State,'$ENUMERATION',WF,IdValue,'$GENERATE_ON_DEMAND') |
| 1699 | | ). |
| 1700 | | block_lazy_compute_expression(pred_false,_,_AssignmentExpr,_LocalState,_State,[],_WF,_Ai). % maybe generate any value ?? |
| 1701 | | |
| 1702 | | % --- |
| 1703 | | |
| 1704 | | :- block add_wd_error_if_false(-,?,?,?,?). |
| 1705 | | add_wd_error_if_false(pred_true,_,_,_,_). |
| 1706 | | add_wd_error_if_false(pred_false,ErrMsg,Cond,Span,WF) :- |
| 1707 | | translate_bexpression_with_limit(Cond,100,CS), |
| 1708 | | %(Span=span_predicate(P,LS,State) -> tcltk_interface:write_dot_file_for_pred_expr_and_state(P, LS, State,'/tmp/prob_wd_error.dot') ; true), |
| 1709 | | add_wd_error_span(ErrMsg,CS,Span,WF). |
| 1710 | | |
| 1711 | | % --- |
| 1712 | | |
| 1713 | | |
| 1714 | | :- use_module(kernel_tools,[ground_value/1]). |
| 1715 | | % compute function application: |
| 1716 | | b_compute_expression_function(Function,FunArg,Type,Info,LocalState,State,Res,WF) :- |
| 1717 | | preferences:preference(find_abort_values,false), % below we do not check, e.g., if set extension is really a function |
| 1718 | | is_extension_function(Function,FInfo,ExFun), !, |
| 1719 | | /* a set extension applied to an argument: in some cases we can can first evaluate the argument, |
| 1720 | | and then avoid having to compute the set_extension for all non-matching args */ |
| 1721 | | opt_push_wait_flag_call_stack_info(WF,function_call(Function,ArgValue,Info),WF2), % only push call_stack in TRACE_INFO mode |
| 1722 | | b_compute_expression(FunArg,LocalState,State,ArgValue,WF2), |
| 1723 | | % TO DO : should we only do this if ArgValue is known ?? otherwise the standard treatment is better ?? |
| 1724 | | % Note: b_compiler has a special rule to not remove this optimisation |
| 1725 | | Span = span_predicate(b(function(Function,FunArg),Type,Info),LocalState,State), |
| 1726 | | ((memberchk(contains_wd_condition,FInfo) ; % relevant for test 1302; but machine seems to contain real WD error |
| 1727 | | preferences:preference(use_clpfd_solver,false) ; |
| 1728 | | ground_value(ArgValue)) |
| 1729 | | -> b_apply_function_set_extension(ExFun,ArgValue,LocalState,State,Res,WF2,Span) |
| 1730 | | ; /* the standard treatment can infer information about the result,... in clpfd_mode */ |
| 1731 | | b_compute_expression(Function,LocalState,State,FValue,WF), |
| 1732 | | get_texpr_type(Function,FunctionType), |
| 1733 | | kernel_call_apply_to(FValue,ArgValue,Res,FunctionType,Span,WF2)). |
| 1734 | | %b_compute_expression_function(b(comprehension_set(Parameters,Condition),_,SInfo),Arg,Type,Info,LocalState,State,Value,WF) :- !, |
| 1735 | | % b_compute_comprehension_set_symbolic(Parameters,Condition,SInfo,LocalState,State,FValue,WF), |
| 1736 | | % b_compute_expression(Arg,LocalState,State,ArgValue,WF), |
| 1737 | | % Span = span_predicate(b(function(Function,Arg),Type,Info),LocalState,State), |
| 1738 | | % get_texpr_type(Function,FunctionType), |
| 1739 | | % kernel_call_apply_to(FValue,ArgValue,Value,FunctionType,Span,WF). |
| 1740 | | b_compute_expression_function(Function,FunArg,_Type,Info,LocalState,State,Value,WF) :- |
| 1741 | | special_operator_for_image(Function,Kind,Args),!, % detect e.g. iterate(rel,k)(x) or closure1 |
| 1742 | | opt_push_wait_flag_call_stack_info(WF,function_call(Function,ArgValue,Info),WF2), |
| 1743 | | b_compute_expression(FunArg,LocalState,State,ArgValue,WF2), |
| 1744 | | b_compute_expressions(Args,LocalState,State,EArgs,WF2), |
| 1745 | | kernel_call(bsets_clp:apply_fun_for_special_operator(Kind,EArgs,ArgValue,Value,WF2,Info), |
| 1746 | | [ArgValue|EArgs],WF2,function(Function,FunArg)). |
| 1747 | | b_compute_expression_function(Function,Arg,Type,Info,LocalState,State,Value,WF) :- |
| 1748 | | opt_push_wait_flag_call_stack_info(WF,function_call(Function,ArgValue,Info),WF2), |
| 1749 | | b_compute_expression(Function,LocalState,State,FValue,WF2), |
| 1750 | | b_compute_expression(Arg,LocalState,State,ArgValue,WF2), |
| 1751 | | Span = span_predicate(b(function(Function,Arg),Type,Info),LocalState,State), |
| 1752 | | get_texpr_type(Function,FunctionType), |
| 1753 | | % TODO: do we still need the span predicate when we have the function_call call_stack infos? |
| 1754 | | kernel_call_apply_to(FValue,ArgValue,Value,FunctionType,Span,WF2). |
| 1755 | | |
| 1756 | | |
| 1757 | | :- use_module(bsyntaxtree,[get_set_type/2,is_set_type/2]). |
| 1758 | | :- use_module(kernel_cardinality_attr,[finite_cardinality_as_int_with_type_wf/5]). |
| 1759 | | b_compute_card(Arg1,Type,Info,OuterInfo,LocalState,State,Card,WF) :- |
| 1760 | ? | b_compute_card2(Arg1,Type,Info,OuterInfo,LocalState,State,Card,WF), |
| 1761 | | % maybe we should move this code below inside kernel_objects ?? |
| 1762 | | (ground(Card) -> true |
| 1763 | | ; get_set_type(Type,SetElType), % TO DO: only needed for cardinality_of_set_extension_list |
| 1764 | | kernel_objects:max_cardinality(SetElType,MaxCard), % this could be precomputed statically |
| 1765 | | (number(MaxCard) -> kernel_objects:in_nat_range(Card,int(0),int(MaxCard)) |
| 1766 | | ; kernel_objects:greater_than_equal(Card,int(0)) % not sure this is required |
| 1767 | | ) |
| 1768 | | ). |
| 1769 | | b_compute_card2(set_extension(Ex),_,_,_,LocalState,State,Card,WF) :- !, |
| 1770 | | b_compute_expressions(Ex,LocalState,State,ExValue,WF), |
| 1771 | | kernel_objects:cardinality_of_set_extension_list(ExValue,Card,WF). |
| 1772 | | b_compute_card2(range(F),_,_,_,LocalState,State,Card,WF) :- !, |
| 1773 | | b_compute_expression(F,LocalState,State,SV1,WF), |
| 1774 | | kernel_objects:cardinality_of_range(SV1,Card,WF). |
| 1775 | | b_compute_card2(comprehension_set(Parameters,Condition),Type,Info,OuterInfo,LocalState,State,Card,WF) :- !, |
| 1776 | | get_set_type(Type,SetElType), |
| 1777 | | % ensure that we keep the closure and don't expand it: |
| 1778 | ? | b_compute_comprehension_set_symbolic(Parameters,Condition,Info,LocalState,State,SV1,WF), |
| 1779 | ? | kernel_mappings:must_succ_kernel_call( |
| 1780 | | kernel_cardinality_attr:finite_cardinality_as_int_with_type_wf(SV1,SetElType,OuterInfo,Card,WF),SV1,WF,card). |
| 1781 | | % TO DO: treat comprehension set + things like card({x|x>1 & x<2**e & x mod 2 = 0}) = 0 |
| 1782 | | b_compute_card2(Arg1,Type,Info,OuterInfo,LocalState,State,Card,WF) :- |
| 1783 | | get_set_type(Type,SetElType), |
| 1784 | | b_compute_expression2(Arg1,Type,Info,LocalState,State,SV1,WF), |
| 1785 | ? | kernel_mappings:must_succ_kernel_call( |
| 1786 | | kernel_cardinality_attr:finite_cardinality_as_int_with_type_wf(SV1,SetElType,OuterInfo,Card,WF),SV1,WF,card). |
| 1787 | | |
| 1788 | | b_compute_max(set_extension(Ex),_,Info,LocalState,State,Max,WF) :- !, |
| 1789 | | b_compute_expressions(Ex,LocalState,State,ExValue,WF), |
| 1790 | | kernel_objects:maximum_of_set_extension_list(ExValue,Max,Info,WF). |
| 1791 | | b_compute_max(Arg1,Type,Info,LocalState,State,Max,WF) :- |
| 1792 | | b_compute_expression2_symbolic(Arg1,Type,Info,LocalState,State,SV1,WF), |
| 1793 | | kernel_mappings:must_succ_kernel_call(kernel_objects:maximum_of_set(SV1,Max,Info,WF),SV1,WF,max). |
| 1794 | | |
| 1795 | | b_compute_min(set_extension(Ex),_,Info,LocalState,State,Min,WF) :- !, |
| 1796 | ? | b_compute_expressions(Ex,LocalState,State,ExValue,WF), |
| 1797 | | kernel_objects:minimum_of_set_extension_list(ExValue,Min,Info,WF). |
| 1798 | | /* doesnot really buy a lot: */ |
| 1799 | | /*b_compute_min(union(Arg1,Arg2),_,Info,LocalState,State,Min,WF) :- |
| 1800 | | Arg1 = b(set_extension(Ex),Type1,Info1), |
| 1801 | | !, |
| 1802 | | b_compute_min(set_extension(Ex),Type1,Info1,LocalState,State,Min1,WF), |
| 1803 | | b_compute_expression(Arg2,LocalState,State,SV2,WF), |
| 1804 | | b_compute_min2(SV2,Min1,Min,Info,WF). |
| 1805 | | :- block b_compute_min2(-,?,?,?,?). |
| 1806 | | b_compute_min2([],Min1,Res,_,_WF) :- !, Res=Min1. |
| 1807 | | b_compute_min2(SV1,Min1,Res,Info,WF) :- |
| 1808 | | kernel_mappings:must_succ_kernel_call(kernel_objects:minimum_of_set(SV1,Min2,Info,WF),SV1,WF,min), |
| 1809 | | min(Min1,Min2,Res). |
| 1810 | | min(int(A),int(B),int(Res)) :- kernel_objects:minimum(A,B,Res). */ |
| 1811 | | b_compute_min(Arg1,Type,Info,LocalState,State,Min,WF) :- |
| 1812 | | b_compute_expression2_symbolic(Arg1,Type,Info,LocalState,State,SV1,WF), |
| 1813 | | kernel_mappings:must_succ_kernel_call(kernel_objects:minimum_of_set(SV1,Min,Info,WF),SV1,WF,min). |
| 1814 | | |
| 1815 | | generate_recursive_closure(TRecId,RecValue,Parameters,Condition,LocalState,State,Result,WF) :- |
| 1816 | | get_texpr_id(TRecId,RecId), |
| 1817 | | add_var_to_localstate(RecId,RecValue,LocalState,State1), |
| 1818 | | debug_println(9,generating_recursive_closure(RecId,Parameters)), |
| 1819 | | b_generate_rec_closure_aux(RecId,Parameters,Condition,State1,State,RClosure1,WF), |
| 1820 | | mark_closure_as_recursive(RClosure1,RClosure2), |
| 1821 | | mark_closure(RClosure2,[recursive(TRecId)],Result), |
| 1822 | | ( debug_mode(off) -> true |
| 1823 | | ; ground_value(Result) -> true |
| 1824 | | ; term_variables(Result,TV), |
| 1825 | | add_message(b_interpreter,'non-ground closure generated: ',TV:Result,Condition) |
| 1826 | | ). |
| 1827 | | % a variation of b_generate_closure |
| 1828 | | b_generate_rec_closure_aux(RecId,Parameters,Condition,LocalState,State,Result,WF) :- |
| 1829 | | split_names_and_types(Parameters,Names,Types), |
| 1830 | | construct_closure(Names,Types,ClosurePred,Result), |
| 1831 | | % now add RecId to parameters to prevent looking up recursive calls |
| 1832 | | b_compiler:b_compile(Condition,[RecId|Names],LocalState,State,ClosurePred,WF). |
| 1833 | | |
| 1834 | | % check if a freetype value has the expected case, if not |
| 1835 | | % generate a WD-error |
| 1836 | | check_freetype_case(Id,Case,VCase,Inner,Value,Info,WF) :- |
| 1837 | | get_enumeration_finished_wait_flag(WF,F), |
| 1838 | | check_freetype_case2(Id,Case,VCase,Inner,Value,Info,F,WF). |
| 1839 | | :- block check_freetype_case2(?,?,-,?,?,?,-,?). |
| 1840 | | check_freetype_case2(Id,Case,VCase,Inner,Value,Info,_F,WF) :- |
| 1841 | | ( nonvar(VCase) -> |
| 1842 | | ( Case=VCase -> equal_object_wf(Inner,Value,check_freetype_case2,WF) |
| 1843 | | ; |
| 1844 | | ajoin(['cannot apply destructor for data type ',Id, |
| 1845 | | ', expected constructor ',Case,', but got: '],Msg), |
| 1846 | | add_wd_error_set_result(Msg,VCase,Inner,Value,Info,WF)) |
| 1847 | | ; % the computation has finished without value, |
| 1848 | | true). % just drop the check |
| 1849 | | |
| 1850 | | % check if we have a set_extension argument representing an explicit function |
| 1851 | | % {a |-> f1, b |-> f2, ...} or [f1,f2] |
| 1852 | | is_extension_function(b(A,_,Info),Info,C) :- is_extension_function_aux(A,C). |
| 1853 | | is_extension_function_aux(set_extension(Ex),CaseList) :- is_set_extension_function(Ex,CaseList). |
| 1854 | | is_extension_function_aux(sequence_extension(Ex),CaseList) :- is_sequence_extension_function(Ex,1,CaseList). |
| 1855 | | is_set_extension_function([],[]). |
| 1856 | | is_set_extension_function([b(Couple,TC,_)|T],[case(A,B)|ST]) :- is_couple(Couple,TC,A,B), |
| 1857 | | is_set_extension_function(T,ST). |
| 1858 | | is_sequence_extension_function([],_,[]). |
| 1859 | | is_sequence_extension_function([B|T],Nr,[case(b(value(int(Nr)),integer,[]),B)|ST]) :- N1 is Nr+1, |
| 1860 | | is_sequence_extension_function(T,N1,ST). |
| 1861 | | |
| 1862 | | is_couple(couple(A,B),_,A,B). |
| 1863 | | is_couple(value((VA,VB)),couple(TA,TB), b(value(VA),TA,[]), b(value(VB),TB,[])). |
| 1864 | | |
| 1865 | | :- use_module(kernel_equality,[equality_objects_wf/4]). |
| 1866 | | b_apply_function_set_extension([],FunArgVal,_LocalState,_State,_Res,WF,Span) :- |
| 1867 | | add_wd_error_span('Function applied outside of domain (#8): ','@fun'(FunArgVal,[]),Span,WF). |
| 1868 | | b_apply_function_set_extension([case(A,B)|T],FunArgVal,LocalState,State,Res,WF,Span) :- |
| 1869 | | b_compute_expression(A,LocalState,State,AVal,WF), |
| 1870 | | equality_objects_wf(AVal,FunArgVal,EqRes,WF), |
| 1871 | | % TO DO: if Res is known, we can call equality_objects(BVal,Res,ResEqRes) |
| 1872 | | b_apply_function_set_extension_aux(EqRes,B,T,FunArgVal,LocalState,State,Res,WF,Span). |
| 1873 | | |
| 1874 | | :- block b_apply_function_set_extension_aux(-,?,?,?, ?,?,?,?,?). |
| 1875 | | b_apply_function_set_extension_aux(pred_true,B,_T,_FunArgVal,LocalState,State,Res,WF,_Span) :- |
| 1876 | | b_compute_expression(B,LocalState,State,BVal,WF), %print(value(BVal)),nl, |
| 1877 | | equal_object_wf(BVal,Res,b_apply_function_set_extension_aux,WF). |
| 1878 | | b_apply_function_set_extension_aux(pred_false,_B,T,FunArgVal,LocalState,State,Res,WF,Span) :- |
| 1879 | | b_apply_function_set_extension(T,FunArgVal,LocalState,State,Res,WF,Span). |
| 1880 | | |
| 1881 | | |
| 1882 | | % --------------------- SIGMA & PI ------------------------ |
| 1883 | | :- use_module(bsyntaxtree,[is_membership_or_equality/3,same_id/3]). |
| 1884 | | /* first clause: try to do a specific treatment for formulas of the form SIGMA(ID).(ID:SET|EXPR) */ |
| 1885 | | /* Advantage: will not delay as long before computing comprehension_set SET ; see test 1312 */ |
| 1886 | | b_general_sum_or_mul([TID],Condition,Expression,Type,_Info,LocalState,State,SumResult,WF,SUMorMUL) :- |
| 1887 | | is_membership_or_equality(Condition,MID,SET), |
| 1888 | | same_id(TID,MID,_),!, |
| 1889 | | b_compute_expression(SET,LocalState,State,SETResult,WF), |
| 1890 | | b_general_sum_or_mul_over_set([TID],SETResult,Expression,Type,LocalState,State,SumResult,WF,SUMorMUL). |
| 1891 | | b_general_sum_or_mul(Ids,Condition,Expression,Type,Info,LocalState,State,SumMulResult,WF,SUMorMUL) :- |
| 1892 | | b_compute_comprehension_set(Ids,Condition,Info,LocalState,State,SETResult,WF), |
| 1893 | | (var(SETResult) -> perfmessagecall(sigma_pi_not_expanded_or_reified(SUMorMUL), |
| 1894 | | translate:print_bexpr(Condition),Condition) ; true), |
| 1895 | | b_general_sum_or_mul_over_set(Ids,SETResult,Expression,Type,LocalState,State,SumMulResult,WF,SUMorMUL). |
| 1896 | | |
| 1897 | | |
| 1898 | | :- block b_general_sum_or_mul_over_set(?,-, ?,?,?,?, ?,?,?). |
| 1899 | | b_general_sum_or_mul_over_set([TID],SETResult,Expression,Type,_LS,_S,SumResult,WF,SUMorMUL) :- |
| 1900 | | Type=integer, % TO DO: also for real |
| 1901 | | get_texpr_id(Expression,ID), % check if we just sum the elements of the list SIGMA(x).(x:S|x) |
| 1902 | | get_texpr_id(TID,ID), |
| 1903 | ? | sum_or_mul_of_explicit_set(SETResult,SUMorMUL,R),!, |
| 1904 | | equal_object_wf(SumResult,R,b_general_sum_or_mul_over_set,WF). |
| 1905 | | b_general_sum_or_mul_over_set(IDs,SETResult,Expression,Type,LocalState,State,SumResult,WF,SUMorMUL) :- |
| 1906 | | expand_custom_set_to_list_wf(SETResult,ESETResult,_Done,b_general_sum_or_mul_over_set,WF), |
| 1907 | | b_sum_or_mul_over_list(ESETResult,IDs,Expression,Type,LocalState,State,SumResult,WF,SUMorMUL). |
| 1908 | | |
| 1909 | | b_sum_or_mul_over_list(L,IDs,Expr,Type,LS,State,Result,WF,SUMorMUL) :- |
| 1910 | | get_acc_base(SUMorMUL,Type,Acc), |
| 1911 | | (Type = integer -> get_wait_flag0(WF,WF0) ; true), % for real: delay until values known |
| 1912 | | b_sum_or_mul_over_list_acc(L,IDs,Expr,Type,LS,State,Acc,Result,WF,SUMorMUL,WF0). |
| 1913 | | |
| 1914 | | :- block b_sum_or_mul_over_list_acc(-, ?, ?,?,?,?,?, ?,?,?,?). |
| 1915 | | % better if list is fully known: no constraint propagation performed during computation ! |
| 1916 | | % for example: SIGMA(x).(x:{-1} \/ 1..30000|x+1) goes down from 2050 ms to 1210 ms |
| 1917 | | b_sum_or_mul_over_list_acc([],_,_,Type,_,_,Acc,Result,_WF,_SUMorMUL,_) :- |
| 1918 | | get_acc_result(Type,Acc,Result). |
| 1919 | | b_sum_or_mul_over_list_acc([H|T],IDs,Expr,Type,LocalState,State,Acc,Result,WF,SUMorMUL,WF0) :- |
| 1920 | | add_typed_vars_to_localstate(IDs,H,LocalState,NewLocalState), |
| 1921 | | b_compute_expression(Expr,NewLocalState,State,HExprVal,WF), |
| 1922 | | b_sum_or_mul_over_list_acc2(HExprVal,T,IDs,Expr,Type,LocalState,State,Acc,Result,WF,SUMorMUL,WF0). |
| 1923 | | |
| 1924 | | :- block b_sum_or_mul_over_list_acc2(?,-, ?, ?,?,?,?,?, ?,?,?,?), |
| 1925 | | b_sum_or_mul_over_list_acc2(?,?, ?, ?,?,?,?,-, ?,?,?,-), |
| 1926 | | b_sum_or_mul_over_list_acc2(-,?, ?, ?,?,?,?,?, ?,?,?,-). % wait until T nonvar and either Acc & HExprVal known or WF0 set |
| 1927 | | % purpose: try and remain in non-CLPFD mode as long as possible to avoid overflows,... (see eg. test 1642, 1708) |
| 1928 | | b_sum_or_mul_over_list_acc2(HExprVal,T,IDs,Expr,Type,LocalState,State,Acc, Result,WF,SUMorMUL,WF0) :- |
| 1929 | | (nonvar(T),nonvar(Acc),ground(HExprVal) |
| 1930 | | -> % we can continue computing the system without constraint propagation; is faster |
| 1931 | | get_value(Type,HExprVal,HVal), |
| 1932 | | compose_acc(SUMorMUL,Acc,HVal,NewAcc), |
| 1933 | | b_sum_or_mul_over_list_acc(T,IDs,Expr,Type,LocalState,State,NewAcc,Result,WF,SUMorMUL,WF0) |
| 1934 | | ; T==[], number(Acc), get_acc_base(SUMorMUL,Type,Acc) -> |
| 1935 | | /* we have just a single element left and no operator has to be applied -> no need for clpfd treatment */ |
| 1936 | | equal_object_wf(HExprVal,Result,b_sum_or_mul_over_list_acc) |
| 1937 | | ; Type=real -> % no CLP(FD) treatment possible yet |
| 1938 | | when(ground(HExprVal), |
| 1939 | | b_sum_or_mul_over_list_acc2(HExprVal,T,IDs,Expr,Type,LocalState,State,Acc, Result,WF,SUMorMUL,WF0)) |
| 1940 | | ; init_clp_acc(SUMorMUL,HExprVal,int(Acc),CLPAccumulator), |
| 1941 | | b_sum_or_mul_over_list_clp(T,IDs,Expr,LocalState,State,CLPAccumulator,Result,WF,SUMorMUL) |
| 1942 | | ). |
| 1943 | | |
| 1944 | | % add set comprehension solution ValueForTypedIDs as individual values for every TypedIDs to LocalState |
| 1945 | | add_typed_vars_to_localstate(TypedIDs,ValueForTypedIDs,LocalState,NewLocalState) :- |
| 1946 | | (flatten_pairs(TypedIDs,ValueForTypedIDs,ValAsList), |
| 1947 | | add_typed_vars_to_localstate_aux(TypedIDs,ValAsList,LocalState,R) |
| 1948 | | -> NewLocalState = R |
| 1949 | | ; add_internal_error('Call failed: ',add_typed_vars_to_localstate(TypedIDs,ValueForTypedIDs,LocalState,NewLocalState)), |
| 1950 | | fail |
| 1951 | | ). |
| 1952 | | % set comprehension results for {a,b,c|P} are nested like this ((a,b),c) |
| 1953 | | % first argument only used for length (to know how deep the pairs are nested) |
| 1954 | | flatten_pairs(L,Pairs,FlatList) :- flatten_pairs(L,Pairs,FlatList,[]). |
| 1955 | | flatten_pairs([_],A) --> !, [A]. |
| 1956 | | flatten_pairs([_|T],(A,B)) --> flatten_pairs(T,A) ,[B]. |
| 1957 | | add_typed_vars_to_localstate_aux([],_,LS,LS). |
| 1958 | | add_typed_vars_to_localstate_aux([TID],[H],LocalState,NewLocalState) :- !, |
| 1959 | | get_texpr_id(TID,ID), get_texpr_type(TID,TYPE), |
| 1960 | | add_typed_var_to_localstate(ID,H,TYPE,LocalState,NewLocalState). |
| 1961 | | add_typed_vars_to_localstate_aux([TID1|Ids],[H1|Hs],LocalState,NewLocalState) :- !, |
| 1962 | | get_texpr_id(TID1,ID1), get_texpr_type(TID1,TYPE), |
| 1963 | | add_typed_var_to_localstate(ID1,H1,TYPE,LocalState,LS1), |
| 1964 | | add_typed_vars_to_localstate_aux(Ids,Hs,LS1,NewLocalState). |
| 1965 | | |
| 1966 | | |
| 1967 | | :- block b_sum_or_mul_over_list_clp(-, ?, ?,?,?, ?, ?,?,?). |
| 1968 | | b_sum_or_mul_over_list_clp([],_, _,_,_, Acc, Result,_WF,SUMorMUL) :- |
| 1969 | | finalise_clp(SUMorMUL,Acc,Result). |
| 1970 | | b_sum_or_mul_over_list_clp([H|T],IDs,Expr,LocalState,State,Acc, Result,WF,SUMorMUL) :- |
| 1971 | | add_typed_vars_to_localstate(IDs,H,LocalState,NewLocalState), |
| 1972 | | b_compute_expression(Expr,NewLocalState,State,HExprVal,WF), |
| 1973 | | compose_clp(SUMorMUL,HExprVal,Acc,NewAcc), |
| 1974 | | b_sum_or_mul_over_list_clp(T,IDs,Expr,LocalState,State,NewAcc,Result,WF,SUMorMUL). |
| 1975 | | |
| 1976 | | :- use_module(kernel_reals,[is_real/2]). |
| 1977 | | get_acc_base(sum,integer,R) :- !, R=0. |
| 1978 | | get_acc_base(sum,real,0.0). |
| 1979 | | get_acc_base(mul,integer,R) :- !, R=1. |
| 1980 | | get_acc_base(mul,real,1.0). |
| 1981 | | get_acc_result(integer,Nr,int(Nr)). |
| 1982 | | get_acc_result(real,Nr,Real) :- is_real(Real,Nr). |
| 1983 | | get_value(integer,int(Val),Val) :- !. |
| 1984 | | get_value(real,Real,Nr) :- is_real(Real,Nr). |
| 1985 | | |
| 1986 | | compose_acc(sum,A,B,C) :- C is A+B. |
| 1987 | | compose_acc(mul,A,B,C) :- C is A*B. |
| 1988 | | init_clp_acc(sum,int(A),int(B),Res) :- (B==0 -> Res=[A] ; Res=[A,B]). % A always not ground |
| 1989 | | init_clp_acc(mul,A,B,C) :- times(A,B,C). |
| 1990 | | compose_clp(sum,int(A),Acc,NewAcc) :- NewAcc = [A|Acc]. |
| 1991 | | compose_clp(mul,A,B,C) :- times(A,B,C). |
| 1992 | | :- use_module(clpfd_interface,[clpfd_sum/2]). |
| 1993 | | finalise_clp(sum,Acc,int(Result)) :- |
| 1994 | | clpfd_sum(Acc,Result). % using clpfd_sum can be much more efficient: no intermediate variables set up |
| 1995 | | finalise_clp(mul,int(Acc),int(Acc)). |
| 1996 | | |
| 1997 | | /* for if-then-else in Z: compute Then or Else, |
| 1998 | | depending on the outcome of the predicate */ |
| 1999 | | :- block b_compute_if_then_else(-,?,?,?,?,?,?). |
| 2000 | | b_compute_if_then_else(pred_true,Then,_Else,LocalState,State,Value,WF) :- |
| 2001 | | opt_push_wait_flag_call_stack_info(WF,b_expr_call(if_then_body,Then),WF2), |
| 2002 | | b_compute_expression(Then,LocalState,State,Value,WF2). |
| 2003 | | b_compute_if_then_else(pred_false,_Then,Else,LocalState,State,Value,WF) :- |
| 2004 | | opt_push_wait_flag_call_stack_info(WF,b_expr_call(if_else_body,Else),WF2), |
| 2005 | | b_compute_expression(Else,LocalState,State,Value,WF2). |
| 2006 | | |
| 2007 | | :- use_module(clpfd_interface,[clpfd_if_then_else/4]). |
| 2008 | | % a more sophisticated treatment, can also work backwards forcing PredRes |
| 2009 | | % examples: |
| 2010 | | % x = IF a<10 THEN 0 ELSE 5 END & x:6..10 & a:1..23 |
| 2011 | | % see must_fail_clpfd_det 132 and 133 |
| 2012 | | % problematic if Else contains recursion; but then we should have WD condition |
| 2013 | | b_clpfd_if_then_else(PredRes,Type,Then,Else,LocalState,State,Value,WF) :- |
| 2014 | | get_wait_flag0(WF,WF0), |
| 2015 | | b_clpfd_if_then_else_block(PredRes,WF0,Type,Then,Else,LocalState,State,Value,WF). |
| 2016 | | :- block b_clpfd_if_then_else_block(-,-,?,?,?,?,?,?,?). |
| 2017 | | b_clpfd_if_then_else_block(PredRes,_,Type,Then,Else,LocalState,State,Value,WF) :- var(PredRes),!, |
| 2018 | | get_full_fd_value(Type,Value,FDValue), |
| 2019 | | b_compute_expression(Then,LocalState,State,ThenValue,WF), |
| 2020 | | get_full_fd_value(Type,ThenValue,FDThenValue), |
| 2021 | | b_compute_expression(Else,LocalState,State,ElseValue,WF), |
| 2022 | | get_full_fd_value(Type,ElseValue,FDElseValue), |
| 2023 | | % TO DO: catch CLPFD overflow and use equality_objects |
| 2024 | | clpfd_if_then_else(PredRes,FDThenValue,FDElseValue,FDValue). |
| 2025 | | b_clpfd_if_then_else_block(pred_true,_,_Type,Then,_Else,LocalState,State,Value,WF) :- |
| 2026 | | b_compute_expression(Then,LocalState,State,Value,WF). |
| 2027 | | b_clpfd_if_then_else_block(pred_false,_,_Type,_Then,Else,LocalState,State,Value,WF) :- |
| 2028 | | b_compute_expression(Else,LocalState,State,Value,WF). |
| 2029 | | |
| 2030 | | :- use_module(b_global_sets,[get_global_type_value/3]). |
| 2031 | | % try and get full_fd_value; true if full value can be translated to CLP(FD) value |
| 2032 | | get_full_fd_value(global(T),FY,X) :- get_global_type_value(FY,T,X). |
| 2033 | | get_full_fd_value(integer,int(X),X). |
| 2034 | | get_full_fd_value(boolean,B,FD) :- kernel_equality:prop_eq_01(B,FD). |
| 2035 | | % record with single field ? |
| 2036 | | |
| 2037 | | can_get_full_fd_value(global(_)). |
| 2038 | | can_get_full_fd_value(integer). |
| 2039 | | can_get_full_fd_value(boolean). |
| 2040 | | |
| 2041 | | % a slightly improved version over set_up_localstate (not sure it is really much faster): |
| 2042 | | add_lazy_let_id_to_local_state(b(identifier(ID),_,_),Trigger,IdValue,LocalState,NewState) :- |
| 2043 | | % should we delete any old occurence of ID in LocalState? |
| 2044 | | NewState = [bind(ID,(Trigger,IdValue))|LocalState]. |
| 2045 | | |
| 2046 | | add_lazy_let_id_and_expression(Id,IdExpr,LocalState,InState,NewLocalState,WF) :- |
| 2047 | | empty_avl(Ai), |
| 2048 | | add_lazy_let_id_and_expression(Id,IdExpr,LocalState,InState,NewLocalState,WF,Ai). |
| 2049 | | add_lazy_let_id_and_expression(Id,IdExpr,LocalState,InState,NewLocalState,WF,Ai) :- |
| 2050 | | add_lazy_let_id_to_local_state(Id,Trigger,IdValue,LocalState,NewLocalState), |
| 2051 | | lazy_compute_expression(Trigger,IdExpr,LocalState,InState,IdValue,WF,Ai). |
| 2052 | | |
| 2053 | | /* for let-statements in Z and now in B: expand localstate by new variables and assign |
| 2054 | | values to them ; |
| 2055 | | now Expressions are evaluated in state *with* the new identifiers ! |
| 2056 | | */ |
| 2057 | | %set_up_localstate_for_let_no_reuse(Ids,Exprs,LocalState,State,LetState,WF) :- |
| 2058 | | % set_up_localstate(Ids,Vars,LocalState,LetState), |
| 2059 | | % compute_let_expressions(Exprs,Vars,LocalState,State,WF). |
| 2060 | | set_up_localstate_for_let(Ids,Vars,Exprs,LocalState,State,LetState,WF) :- |
| 2061 | | set_up_localstate(Ids,Vars,LocalState,LetState), |
| 2062 | | %external_functions:observe_variables(Ids,Vars), |
| 2063 | | compute_let_expressions(Exprs,Vars,LetState,State,WF). |
| 2064 | | compute_let_expressions([],[],_,_,_). |
| 2065 | | compute_let_expressions([Expr|RestExprs],[Var|RestVars],LocalState,State,WF) :- |
| 2066 | | b_compute_expression(Expr,LocalState,State,Value,WF), |
| 2067 | | equal_object_optimized(Var,Value,compute_let_expressions), |
| 2068 | | compute_let_expressions(RestExprs,RestVars,LocalState,State,WF). |
| 2069 | | |
| 2070 | | set_up_localstate_for_global_let(Ids,Exprs,LocalState,State,LetState,WF) :- |
| 2071 | | set_up_localstate(Ids,Vars,State,LetState), |
| 2072 | | compute_let_expressions(Exprs,Vars,LocalState,LetState,WF). |
| 2073 | | |
| 2074 | | |
| 2075 | | l_compute_field([],_LS,_S,[],_WF). |
| 2076 | | l_compute_field([Field|T],LS,S,[FVal|CT],WF) :- |
| 2077 | | b_compute_field(Field,LS,S,FVal,WF), |
| 2078 | | l_compute_field(T,LS,S,CT,WF). |
| 2079 | | |
| 2080 | | b_compute_field(field(Name,TypeOrVal),LocalState,State,field(Name,TypeOrFieldValue),WF) :- |
| 2081 | | b_compute_expression(TypeOrVal,LocalState,State,TypeOrFieldValue,WF). |
| 2082 | | |
| 2083 | | |
| 2084 | | |
| 2085 | | :- use_module(b_compiler). |
| 2086 | | |
| 2087 | | /* :- type bsets_closure +--> closure(list(type(variable_id)), |
| 2088 | | list(type(basic_type_descriptor)),type(boolean_expression)). */ |
| 2089 | | |
| 2090 | | :- assert_pre(b_interpreter:b_compute_comprehension_set(Parameters,Cond,_Info,LS,State,_R,_WF), |
| 2091 | | (ground_check(Parameters), |
| 2092 | | bsyntaxtree:check_if_typed_predicate(Cond),type_check(LS,store),type_check(State,store)) ). |
| 2093 | | |
| 2094 | | :- assert_post(b_interpreter:b_compute_comprehension_set(_P,_C,_Info,_LS,_State,Val,_WF), |
| 2095 | | (b_interpreter:value_type(Val))). %, nonvar(Val) |
| 2096 | | |
| 2097 | | |
| 2098 | | |
| 2099 | | b_compute_comprehension_set_symbolic(Parameters,Condition,Info,LocalState,State,Result,WF) :- |
| 2100 | | % a version which never expands the generated closure |
| 2101 | ? | b_generate_closure_if_necessary(Parameters,Condition,LocalState,State,CResult,WF), |
| 2102 | ? | (member(prob_annotation('SYMBOLIC'),Info) |
| 2103 | | -> mark_closure_as_symbolic(CResult,CRS),Result=CRS |
| 2104 | | ; Result=CResult). % TODO: check if we need definitely_expand_this_explicit_set |
| 2105 | | % we could catch Parameters = [TID], Condition = b(member(b(identifier(ID),_TYPE,_),SET) |
| 2106 | | b_compute_comprehension_set(Parameters,Condition,Info,LocalState,State,Result,WF) :- |
| 2107 | ? | b_generate_closure_if_necessary(Parameters,Condition,LocalState,State,CResult,WF), |
| 2108 | | b_compute_comprehension_set_aux(CResult,Info,Result,WF). |
| 2109 | | |
| 2110 | | :- use_module(clpfd_interface,[catch_clpfd_overflow_call2/2]). |
| 2111 | | b_compute_comprehension_set_aux(CResult,_,Result,_WF) :- |
| 2112 | | (var(CResult) ; \+ functor(CResult,closure,3) ),!, |
| 2113 | | Result=CResult. |
| 2114 | | b_compute_comprehension_set_aux(CResult,Info,Result,_WF) :- |
| 2115 | ? | member(prob_annotation('SYMBOLIC'),Info),!, |
| 2116 | | mark_closure_as_symbolic(CResult,CRS),Result=CRS. |
| 2117 | | b_compute_comprehension_set_aux(CResult,Info,Result,WF) :- |
| 2118 | | (preferences:get_preference(convert_comprehension_sets_into_closures,true) |
| 2119 | | -> \+ definitely_expand_this_explicit_set(CResult) |
| 2120 | | ; dont_expand_symbolic_explicit_set(CResult), % Note: also detects FORCE annotation |
| 2121 | | perfmessage_wf(symbolic,'Keeping comprehension-set symbolic (use FORCE to override this or increase SYMBOLIC_LIMIT)','',Info,WF) |
| 2122 | | ),!, |
| 2123 | | Result=CResult. |
| 2124 | | b_compute_comprehension_set_aux(CResult,Info,Result,WF) :- |
| 2125 | | b_expand_compute_comprehension_set(CResult,Info,Result,WF). |
| 2126 | | |
| 2127 | | b_expand_compute_comprehension_set(CResult,Info,Result,WF) :- |
| 2128 | | % using just get_wait_flag1(b_compute_comprehension_set,WF,WF0), makes many benchmarks faster ; but e.g., test 268 fails; maybe we sholud pre-compute bexpr_variables ? and pass this to expand_only_custom_closure_global |
| 2129 | | (preferences:preference(use_smt_mode,false) |
| 2130 | | -> get_wait_flag(1000,b_compute_comprehension_set,WF,WF0) % ideally we should say WF0 = _ ; but for some benchmarks Rule_DB_SIGAREA_0024_ori.mch this is important to avoid expanding closures before other stuff fails |
| 2131 | | % only in SMT mode can there be any benefit in expanding a non-ground closure (when it is a lambda closure) |
| 2132 | | % => hence delay expansion longer, possibly detecting enumeration warnings while expanding |
| 2133 | | % TO DO: for lambda closures with known domain: expand earlier ! |
| 2134 | | ; get_wait_flag(2,b_compute_comprehension_set,WF,WF0)), |
| 2135 | | kernel_objects:mark_as_to_be_computed(Result), % avoid that we instantiate this set (e.g., in check_element_of_wf: we will get a full description of the set later; see test 1353 |
| 2136 | | (var(WF0),var(Result) -> ground_value_check(CResult,CGr) ; true), |
| 2137 | | block_compute_comprehension_set_aux(CResult,Info,Result,WF,WF0,CGr). |
| 2138 | | |
| 2139 | | :- block block_compute_comprehension_set_aux(?,?,-,?,-,-). |
| 2140 | | block_compute_comprehension_set_aux(CResult,Info,Result,WF,_WF0,_CGr) :- |
| 2141 | | ( |
| 2142 | | nonvar(Result) |
| 2143 | | -> equal_object_wf(CResult,Result,WF) % this may expand CResult or do other tests; |
| 2144 | | % like emptyness test or symbolic equality; |
| 2145 | | % if Result=[_|_] then the closure needs to be expanded anyhow, so need to try and keep symbolic |
| 2146 | | ; |
| 2147 | | catch_clpfd_overflow_call2( |
| 2148 | | on_enumeration_warning_with_continue(expand_only_custom_closure_global(CResult,Expansion,check_no_inf,WF), |
| 2149 | | % check_no_inf: we know it is not definitely infinite; and we do not want warnings for virtual time-outs |
| 2150 | | % an enumeration warning occured in the expansion scope: keep closure symbolic after all |
| 2151 | | % NOTE: for this detection to work it is better to delay until the body is ground (CGr) |
| 2152 | | % and not use a priority too low for WF0, otherwise Expansion will be var and enum warning may occur later |
| 2153 | | b_compute_comprehension_set_sym_msg(CResult,Info,Result,WF) % Backup Call in case of enum warning |
| 2154 | | , |
| 2155 | | equal_object_wf(Result,Expansion,WF) % TrueContinuation |
| 2156 | | % instantiating Result can trigger other code which fails and confuses on_enumeration_warning |
| 2157 | | ), |
| 2158 | | b_compute_comprehension_set_sym(CResult,Result,WF) |
| 2159 | | ) % catch_clpfd_overflow_call2 |
| 2160 | | ). |
| 2161 | | |
| 2162 | | b_compute_comprehension_set_sym_msg(CResult,Info,Result,WF) :- CResult = closure(P,_T,B), |
| 2163 | | silent_mode(off), |
| 2164 | | % TO DO: should we check that this closure has not been constructed by ProB, e.g., for UNION ?? |
| 2165 | | % e.g., P = ['__RANGE_LAMBDA__'] what about '_zzzz_unary', '_zzzz_binary', '_prj_arg1__', ... |
| 2166 | | % also: what if the enumeration warning has a pending_abort_error |
| 2167 | | !, |
| 2168 | | Msg = 'Keeping comprehension-set symbolic (you may want to use the /*@symbolic*/ pragma to prevent this message, unless it was due to a WD-Error), identifiers: ', |
| 2169 | | (contains_info_pos(Info) -> Span=Info ; Span=B), |
| 2170 | | add_message(b_compute_comprehension_set,Msg,P,Span), |
| 2171 | | b_compute_comprehension_set_sym(CResult,Result,WF). |
| 2172 | | b_compute_comprehension_set_sym_msg(CResult,_,Result,WF) :- |
| 2173 | | b_compute_comprehension_set_sym(CResult,Result,WF). |
| 2174 | | |
| 2175 | | |
| 2176 | | b_compute_comprehension_set_sym(CResult,Result,WF) :- |
| 2177 | | mark_closure_as_symbolic(CResult,CRS), % prevent doing the same mistake again |
| 2178 | | equal_object_wf(Result,CRS,b_compute_comprehension_set_sym,WF). |
| 2179 | | |
| 2180 | | |
| 2181 | | :- use_module(closures,[construct_closure_if_necessary/4]). |
| 2182 | | b_generate_forall_closure(Parameters,Condition,LocalState,State,Result,WF) :- |
| 2183 | | split_names_and_types(Parameters,Names,Types), |
| 2184 | | construct_closure(Names,Types,ClosurePred1,Result), |
| 2185 | | (LocalState==[],State==[] |
| 2186 | | -> ClosurePred = Condition % no lookups can be performed anyway; no need to compile |
| 2187 | | ; b_compiler:b_compile(Condition,Names,LocalState,State,ClosurePred,WF) ), |
| 2188 | | add_texpr_infos(ClosurePred,[quantifier_kind(forall)],ClosurePred1). % info for call_stack |
| 2189 | | |
| 2190 | | |
| 2191 | | |
| 2192 | | b_generate_closure_if_necessary(Parameters,Condition,LocalState,State,Result,WF) :- |
| 2193 | | split_names_and_types(Parameters,Names,Types), |
| 2194 | ? | b_compiler:b_compile(Condition,Names,LocalState,State,ClosurePred,WF), |
| 2195 | | %print('COMPILED: '),translate:print_bexpr(ClosurePred),nl, |
| 2196 | | construct_closure_if_necessary(Names,Types,ClosurePred,Result). |
| 2197 | | |
| 2198 | | |
| 2199 | | :- public convert_list_of_expressions_into_sequence/2. |
| 2200 | | convert_list_of_expressions_into_sequence(List,SeqValue) :- |
| 2201 | | convert_list_of_expressions_into_sequence(List,1,Seq), |
| 2202 | ? | equal_object_optimized(Seq,SeqValue,convert_list_of_expressions_into_sequence). |
| 2203 | | |
| 2204 | | convert_list_of_expressions_into_sequence([],_,[]). |
| 2205 | | convert_list_of_expressions_into_sequence([H|T],Cur,[(int(Cur),H)|ConvT]) :- |
| 2206 | | C1 is Cur+1, |
| 2207 | | convert_list_of_expressions_into_sequence(T,C1,ConvT). |
| 2208 | | |
| 2209 | | convert_list_of_expressions_into_set_wf(ListOfElements,Set,Type,WF) :- |
| 2210 | | list_of_expressions_is_ground(ListOfElements,Ground,OrderedListOfElements), |
| 2211 | ? | convert_list_of_expressions_into_set1(Ground,OrderedListOfElements,Set,Type,WF). |
| 2212 | | convert_list_of_expressions_into_set1(true,ListOfElements,Set,_Type,WF) :- %var(Set), |
| 2213 | ? | \+ (ListOfElements = [H|_], is_symbolic_closure(H)), % list contains symbolic closures, do not try to expand to avl |
| 2214 | | % see should_be_converted_to_avl_from_lists ? |
| 2215 | | !, construct_avl_from_lists_wf(ListOfElements,AVLSet,WF), |
| 2216 | | equal_object_wf(AVLSet,Set,convert_list_of_expressions_into_set1,WF). |
| 2217 | | convert_list_of_expressions_into_set1(_,Seq,Set,Type,WF) :- |
| 2218 | ? | kernel_objects:max_cardinality(Type,TypeMaxCard), |
| 2219 | ? | convert_list_of_expressions_into_set2(Seq,1,[],Set,TypeMaxCard,WF). |
| 2220 | | |
| 2221 | | % TO DO: sort elements : ground elements first |
| 2222 | | convert_list_of_expressions_into_set2([],_,Set,Res,_,WF) :- |
| 2223 | ? | equal_object_wf(Set,Res,convert_list_of_expressions_into_set2,WF). |
| 2224 | | convert_list_of_expressions_into_set2([H|T],MaxSze,SetSoFar,OutSet,TypeMaxCard,WF) :- |
| 2225 | | %kernel_objects:basic_type_set(Type,IntSet,MaxSze), |
| 2226 | | check_set_lists_bounded(IntSet,TypeMaxCard,MaxSze), |
| 2227 | | % TO DO: optimize; avoid traversing or typing elements over and over again |
| 2228 | | % maybe add a max. sze argument to add_element |
| 2229 | | add_element_wf(H,SetSoFar,IntSet,WF), % print(add_el(H,SetSoFar,IntSet)),nl, |
| 2230 | | % TO DO: check if H in OutSet and keep track of which elements have already been covered in OutSet |
| 2231 | | %check_element_of_wf(H,OutSet,WF), makes tests 114, 115, 118-120, 1063 fail ! % we could also call lazy_try_check_element_of if this turns out to be too expensive |
| 2232 | | kernel_objects:lazy_try_check_element_of(H,OutSet,WF), |
| 2233 | | M1 is MaxSze+1, |
| 2234 | ? | convert_list_of_expressions_into_set2(T,M1,IntSet,OutSet,TypeMaxCard,WF). |
| 2235 | | |
| 2236 | | |
| 2237 | | :- use_module(inf_arith). |
| 2238 | | |
| 2239 | | % check that a set does not exceed a certain bound (to prevent infinite |
| 2240 | | % or overeager enumeration of sets); Warning: will not check custom_explicit_set sizes |
| 2241 | | check_set_lists_bounded(Set,TypeMaxCard,Bound) :- |
| 2242 | | inf_arith:infmin(TypeMaxCard,Bound,Card), |
| 2243 | | (is_inf_or_overflow_card(Card) -> true |
| 2244 | | ; Card<1 -> empty_set(Set) |
| 2245 | | ; check_set_bounded_aux(Set,Card)). |
| 2246 | | |
| 2247 | | :- block check_set_bounded_aux(-,?). |
| 2248 | | check_set_bounded_aux(CustomSet,_) :- is_custom_explicit_set(CustomSet,check_set_bounded_aux),!. |
| 2249 | | check_set_bounded_aux([],_). |
| 2250 | | check_set_bounded_aux([_|T],Card) :- |
| 2251 | | C1 is Card-1, |
| 2252 | | (C1=0 -> empty_set(T) ; check_set_bounded_aux(T,C1)). |
| 2253 | | |
| 2254 | | |
| 2255 | | |
| 2256 | | % check if the list can be converted into AVL form; if not: re-order list so that ground elements are present first |
| 2257 | | list_of_expressions_is_ground([],true,[]). |
| 2258 | | list_of_expressions_is_ground([E|T],Ground,ResList) :- |
| 2259 | | (should_be_converted_to_avl(E) -> ResList=[E|RT], list_of_expressions_is_ground(T,Ground,RT) |
| 2260 | | ; Ground=false, |
| 2261 | | bring_ground_elements_forward(T,[E],ResList) |
| 2262 | | ). |
| 2263 | | |
| 2264 | | bring_ground_elements_forward([],Acc,Acc). |
| 2265 | | bring_ground_elements_forward([H|T],Acc,Res) :- |
| 2266 | | (should_be_converted_to_avl(H) -> Res=[H|RT], bring_ground_elements_forward(T,Acc,RT) |
| 2267 | | ; bring_ground_elements_forward(T,[H|Acc],Res)). |
| 2268 | | |
| 2269 | | :- use_module(kernel_card_arithmetic,[is_inf_or_overflow_card/1, safe_mul/3]). |
| 2270 | | % compute cardinality of a list of parameters, such as for forall |
| 2271 | | parameter_list_cardinality([],1). |
| 2272 | | parameter_list_cardinality([b(identifier(_),BasicType,_)|T],Res) :- |
| 2273 | ? | parameter_list_cardinality(T,TCard), |
| 2274 | | (is_inf_or_overflow_card(TCard) -> Res=TCard |
| 2275 | ? | ; kernel_objects:max_cardinality(BasicType,Card), |
| 2276 | | safe_mul(Card,TCard,Res) |
| 2277 | | ). |
| 2278 | | % see also basic_type_list_cardinality, now moved to custom_explicit_sets |
| 2279 | | |
| 2280 | | b_for_all(Parameters,Infos,LHS,RHS,LocalState,State,WF) :- |
| 2281 | | get_wait_flag1(for_all(Parameters),WF,LWF), % hack: give total_function,... priority to set up domain/range |
| 2282 | | b_for_all_aux(LWF,Parameters,Infos,LHS,RHS,LocalState,State,WF). |
| 2283 | | |
| 2284 | | :- block b_for_all_aux(-,?,?,?,?,?,?,?). |
| 2285 | | b_for_all_aux(_,Parameters,Infos,LHS,RHS,LocalState,State,WF) :- |
| 2286 | | split_names_and_types(Parameters,Names,Types), |
| 2287 | | b_compile(LHS,Names,LocalState,State,CLHS,WF), |
| 2288 | | b_compile(RHS,Names,LocalState,State,CRHS,WF), |
| 2289 | ? | b_for_all_aux2(Parameters,Names,Types,Infos,CLHS,CRHS,WF). |
| 2290 | | |
| 2291 | | :- use_module(kernel_tools,[ground_bexpr/1, ground_bexpr_check/2]). |
| 2292 | | b_for_all_aux2(_,_,_,_,LHS,RHS,_) :- |
| 2293 | | (is_falsity(LHS) ; is_truth(RHS)), |
| 2294 | | !. % quantifier always true |
| 2295 | | b_for_all_aux2(Parameters,CParameters,CParameterTypes,Infos,LHS,RHS,WF) :- |
| 2296 | | ground_bexpr(LHS), |
| 2297 | ? | \+ obvious_enumeration(LHS,CParameters), % if we have an obvious (small) enumeration in the LHS it is often better simply to enumerate and check the RHS for every instance |
| 2298 | | ground_bexpr(RHS), %print(not_exists_forall(Parameters,LHS,RHS)),nl, |
| 2299 | | !, |
| 2300 | | b_for_all_find_counter_example(Parameters,CParameters,CParameterTypes,Infos,LHS,RHS,WF). |
| 2301 | | b_for_all_aux2(Parameters,CParameters,CParameterTypes,Infos,LHS,RHS,WF) :- |
| 2302 | ? | parameter_list_cardinality(Parameters,ParCard), |
| 2303 | | peel_implications(Parameters,ParCard,LHS,RHS, LHS2,RHS2), |
| 2304 | ? | b_for_all_aux3(Parameters,ParCard,CParameters,CParameterTypes,Infos,LHS2,RHS2,WF). |
| 2305 | | |
| 2306 | | b_for_all_aux3(Parameters,ParCard,CParameters,CParameterTypes,Infos,LHS,RHS,WF) :- |
| 2307 | | %large_domain(Parameters,ParCard,LHS), |
| 2308 | | is_infinite_or_very_large_explicit_set(closure(CParameters,CParameterTypes,LHS),10000000), |
| 2309 | | % the domain is so large that we cannot possibly check all instances |
| 2310 | | % in this case: we prefer to look for a counter example in the hope that the constraint solver can narrow down the search by combining LHS and not(RHS) |
| 2311 | | !, |
| 2312 | | perfmessage(forall,'Large forall domain: ',ParCard,Infos), |
| 2313 | | ground_bexpr_check(LHS,LG), |
| 2314 | | ground_bexpr_check(RHS,RG), |
| 2315 | | when((nonvar(LG),nonvar(RG)), |
| 2316 | | b_for_all_find_counter_example(Parameters,CParameters,CParameterTypes,Infos,LHS,RHS,WF) |
| 2317 | | ). |
| 2318 | | b_for_all_aux3(Parameters,ParCard,_CP,_,Infos,LHS,RHS,WF) :- |
| 2319 | | if(expand_forall_quantifier(Parameters,LHS,RHS,Infos,WF,ReificationVariable), |
| 2320 | | ( |
| 2321 | | perfmessage(good(forall),'Reified forall by expanding into conjunction: ',LHS), |
| 2322 | | ReificationVariable=pred_true), |
| 2323 | | ( % print(normal_forall(Parameters)),nl, |
| 2324 | | b_generate_for_all_list_domain1(Parameters,ParCard,LHS,RHS,[],[],AllSolList,NewRHS,WF), |
| 2325 | ? | expand_forall1(AllSolList,NewRHS,Parameters,WF) |
| 2326 | | )). |
| 2327 | | |
| 2328 | | |
| 2329 | | % a treatment of for all where we look for counter examples satisfying LHS & not(RHS) |
| 2330 | | b_for_all_find_counter_example(Parameters,CParameters,CParameterTypes,Infos,LHS,RHS,WF) :- |
| 2331 | | % if LHS is Parameters: avl_set(V) then we probably should not apply this rule ? |
| 2332 | | create_negation(RHS,NegRHS), |
| 2333 | | conjunct_predicates_with_pos_info(LHS,NegRHS,Pred0), |
| 2334 | | % format('looking for: ~w ',[CParameters]), translate:print_bexpr(LHS),nl, translate:print_bexpr(NegRHS),nl, |
| 2335 | | perfmessage(good(forall),translating_forall_into_not_exists(CParameters),LHS), |
| 2336 | | add_texpr_infos(Pred0,[quantifier_kind(forall)],ClosureBody1), % for call_stack |
| 2337 | | init_quantifier_wait_flag(WF,not(forall),Parameters,ParValues,Infos,LocalWF), % relevant for test 1904 |
| 2338 | | not_with_enum_warning_and_possible_delay( |
| 2339 | | (custom_explicit_sets:b_test_closure(CParameters,CParameterTypes,ClosureBody1,ParValues,negative,LocalWF), |
| 2340 | | perfmessage_bexpr(forall,forall_counter_example(CParameters,ParValues),LHS) |
| 2341 | | ),LocalWF, WF, forall(CParameters), Infos). |
| 2342 | | |
| 2343 | | :- use_module(library(timeout),[time_out/3]). |
| 2344 | | not_with_enum_warning_and_possible_delay(C,WF,OuterWF,PP,Span) :- |
| 2345 | | get_preference(disprover_mode,true), |
| 2346 | | % in disprover mode we do not track WD errors, no need to start_attach_inner_abort_errors |
| 2347 | | !, |
| 2348 | | % delay potentially expensive quantifications in the hope of finding other contradictions earlier |
| 2349 | | if( (time_out(not_with_enum_warning(C,WF,PP,Span),20,TRes),TRes \== time_out), |
| 2350 | | copy_wfe_from_inner_if_necessary(WF,OuterWF), |
| 2351 | | (get_wait_flag(2,not_with_enum_warning_and_possible_delay,OuterWF,LWF), % which priority to use ?? |
| 2352 | | debug_println(19,delay_not_in_disprover_mode), |
| 2353 | | not_with_enum_warning_delay(C,LWF,WF,OuterWF,PP,Span))). |
| 2354 | | not_with_enum_warning_and_possible_delay(C,WF,OuterWF,PP,Span) :- |
| 2355 | | start_attach_inner_abort_errors(Level,not_with_enum_warning), |
| 2356 | | call_cleanup(not_with_enum_warning(C,WF,PP,Span), |
| 2357 | | re_attach_pending_inner_abort_errors(Level,OuterWF,_)), |
| 2358 | | copy_wfe_from_inner_if_necessary(WF,OuterWF). |
| 2359 | | |
| 2360 | | |
| 2361 | | :- block not_with_enum_warning_delay(?,-,?,?,?,?). |
| 2362 | | not_with_enum_warning_delay(C,_,WF,OuterWF,PP,Span) :- |
| 2363 | | debug_println(19,trigger_not_with_enum_warning_delay), |
| 2364 | | not_with_enum_warning(C,no_wf_available,PP,Span), |
| 2365 | | copy_wfe_from_inner_if_necessary(WF,OuterWF). |
| 2366 | | |
| 2367 | | :- use_module(avl_tools,[quick_avl_approximate_size/2]). |
| 2368 | | obvious_enumeration(LHS,[Id]) :- % TO DO : allow multiple ids |
| 2369 | | % TO DO: unify with is_for_all_set_membership_predicate code below |
| 2370 | | TLHSMembership = b(LHSMembership,_,_), |
| 2371 | ? | member_conjunct(TLHSMembership,LHS,_), |
| 2372 | | obvious_enum_aux(LHSMembership,Id). |
| 2373 | | |
| 2374 | | obvious_enum_aux(member(TID,b(value(VAL),_,_)),ID) :- get_texpr_id(TID,ID), |
| 2375 | | nonvar(VAL), VAL = avl_set(AVL), % TO DO: also support interval closures ? |
| 2376 | | quick_avl_approximate_size(AVL,Size), |
| 2377 | | Size<256. |
| 2378 | | %obvious_enum_aux(equal(LHS,RHS),ID) :- % stillposes problem for test 231, investigate |
| 2379 | | % ( get_texpr_id(LHS,ID) -> get_texpr_expr(RHS,value(_)) |
| 2380 | | % ; get_texpr_id(RHS,ID) -> get_texpr_expr(LHS,value(_))). |
| 2381 | | % % TODO: do we need to accept integer(.),... literals? |
| 2382 | | |
| 2383 | | % expands forall quantifier and thus enables that we also propagate from right to left |
| 2384 | | % This helps for test 1397 (HandshakeSimple_TotFun) |
| 2385 | | % TO DO: directly call the code which tries to expand a forall quantifier |
| 2386 | | expand_forall_quantifier(Parameters,LHS,RHS,Infos,WF,ReificationVariable) :- |
| 2387 | | (preferences:preference(use_smt_mode,true) -> true |
| 2388 | | ; preferences:preference(solver_strength,SS), SS>9), % sometimes contrapositive checking maybe not useful; see test 1368 |
| 2389 | | % TO DO: investigate and provide more principled heuristic |
| 2390 | | b_interpreter_check:b_check_forall_wf(Parameters,LHS,RHS,Infos,[],[],WF,ReificationVariable). |
| 2391 | | |
| 2392 | | % rewrite !(x,..).( TRUTH => (LHS2 => RHS2)) into !(x,...).(LHS2 => RHS2) |
| 2393 | | peel_implications(Parameters,ParCard,LHS,RHS, NewLHS,NewRHS) :- |
| 2394 | | %print(forall),nl, print(Parameters),nl,print(LHS),nl, |
| 2395 | | % this patterns occurs often in Event-B translations to classical B |
| 2396 | | LHS=b(truth,_,_), |
| 2397 | | RHS=b(implication(LHS2,RHS2),_,_), % TO DO: maybe also accept nested forall and then add to Parameters? |
| 2398 | | (is_inf_or_overflow_card(ParCard) -> true ; ParCard>50), |
| 2399 | | % larger bound than below: maybe user explicitly wants to expand it |
| 2400 | | !, |
| 2401 | | peel_implications(Parameters,ParCard,LHS2,RHS2, NewLHS,NewRHS). |
| 2402 | | peel_implications(_,_,LHS,RHS, LHS,RHS). |
| 2403 | | |
| 2404 | | b_generate_for_all_list_domain_nolwf(Parameters,LHS,RHS,LS,S,ForAllDomain,NewRHS,WF) :- |
| 2405 | | parameter_list_cardinality(Parameters,ParCard), |
| 2406 | | peel_implications(Parameters,ParCard,LHS,RHS, LHS2,RHS2), |
| 2407 | | b_generate_for_all_list_domain1(Parameters,ParCard,LHS2,RHS2,LS,S,OForAllDomain,NewRHS,WF), |
| 2408 | | optlist_to_list(OForAllDomain,ForAllDomain). % resolve any optional_value/2 items |
| 2409 | | |
| 2410 | | % generate the domain of a Universally Quantified Formula as a list |
| 2411 | | b_generate_for_all_list_domain1(Parameters,_ParCard,LHS,RHS,LS,S,ForAllDomain,NewRHS,WF) :- |
| 2412 | | % optimized treatment for formulas of the form !x.(x:S => RHS) or !(x,y).(x|->y:S => RHS) |
| 2413 | | % to do: expand to more formulas, such as !(x,y).(x:S & y:T => RHS) |
| 2414 | | % or !y.(y:1..(n-1) & f(y)=TRUE => f(y+1)=TRUE) --> !y.(y:1..(n-1) => (f(y)=TRUE => f(y+1)=TRUE)) |
| 2415 | | % or we could try and reify set membership for small domains and trigger implication |
| 2416 | | is_for_all_set_membership_predicate(LHS,Parameters,Set,Pattern,ParameterValues),!, |
| 2417 | | perfmessages_bexpr_call(good(forall),['Detected forall set membership over ',Ps,': '], |
| 2418 | | LHS,bsyntaxtree:def_get_texpr_ids(Parameters,Ps)), |
| 2419 | | NewRHS=RHS, |
| 2420 | | b_generate_set_for_all_domain(Set,LS,S,Pattern,ParameterValues,ForAllDomain,WF). |
| 2421 | | b_generate_for_all_list_domain1(Parameters,ParCard,LHS,RHS,LS,S,ForAllDomain,NewRHS,WF) :- |
| 2422 | | b_generate_forall_closure(Parameters,LHS,LS,S,Closure,WF), % POSSIBLY NO NEED TO COMPILE |
| 2423 | | b_generate_for_all_domain2(Closure,Parameters,ParCard,LHS,RHS,ForAllDomain,NewRHS,WF). |
| 2424 | | |
| 2425 | | b_generate_for_all_domain2(Closure,_Parameters,_,LHS,RHS,ForAllDomain,NewRHS,WF) :- |
| 2426 | | (LHS=b(truth,pred,_) ; ground_value(Closure)), |
| 2427 | | !, NewRHS=RHS, % nl,print('FORALL EXPAND: '),(ground_value(Closure) -> print(ground(_Parameters)),nl, print_bexpr(LHS),nl ; print('NONGROUND!!'),nl,print_bexpr(RHS),nl), %% |
| 2428 | | b_expand_closure_forall_to_list(Closure,ForAllDomain,WF). |
| 2429 | | b_generate_for_all_domain2(_,Parameters,_,LHS,RHS,ForAllDomain,NewRHS,WF) :- |
| 2430 | | is_a_conjunct(LHS,_,_), % otherwise we have already checked for set_membership above ! |
| 2431 | | LHS_Membership = b(member(_,_),_,_), % TO DO: also allow conjuncts |
| 2432 | | member_conjunct(LHS_Membership,LHS,RestLHS), |
| 2433 | | % !(x). ( x:Set & RestLHS => RHS) ---> !(x).(x:Set => (RestLHS => RHS)) |
| 2434 | | is_for_all_set_membership_predicate(LHS_Membership,Parameters,Set,Pattern,ParameterValues), |
| 2435 | | max_cardinality_ok_for_expansion(Set,100,[],[]), % TO DO: simplify LS,S argument <---- |
| 2436 | | !, |
| 2437 | | % as we expand Set without RHS predicate, check if this is ok |
| 2438 | | % indeed maximum cardinality of Set could be infinite !! !x.(x:NATURAL1 & x<N => P) |
| 2439 | | % print('Splitting for all conjunct: '), print(Parameters),nl, print_bexpr(LHS_Membership),nl,print_bexpr(RestLHS),nl, %% |
| 2440 | | perfmessage_bexpr(good(forall),'Splitting LHS of forall: ',LHS), |
| 2441 | | NewRHS = b(implication(RestLHS,RHS),pred,[]), % TO DO: extract & join info |
| 2442 | | b_generate_set_for_all_domain(Set,[],[],Pattern,ParameterValues,ForAllDomain,WF). |
| 2443 | | b_generate_for_all_domain2(Closure,_Parameters,ParCard,LHS,RHS,ForAllDomain,NewRHS,WF) :- |
| 2444 | | (is_inf_or_overflow_card(ParCard) -> true ; ParCard>8), |
| 2445 | | !, |
| 2446 | | NewRHS=RHS, |
| 2447 | | perfmessage_bexpr(forall,'No optimisations applicable to forall (will delay until all variables are bound): ',LHS), % maybe we should generate this message only if ForAllDomain is var ? |
| 2448 | | b_expand_closure_forall_to_list(Closure,ForAllDomain,WF). |
| 2449 | | b_generate_for_all_domain2(_Closure,Parameters,ParCard,LHS,RHS,ForAllDomain,NewRHS,WF) :- |
| 2450 | | NewLHS=b(truth,pred,[]), NewRHS=b(implication(LHS,RHS),pred,[]), |
| 2451 | | b_generate_forall_closure(Parameters,NewLHS,[],[],Closure,WF), |
| 2452 | | perfmessages_bexpr(good(forall),['Expanding forall with finite parameter type, card = ',ParCard,': '],LHS), |
| 2453 | | b_expand_closure_forall_to_list(Closure,ForAllDomain,WF). |
| 2454 | | |
| 2455 | | % code to decide whether a for all should be expanded if the |
| 2456 | | % predicate contains a set membership predicate : !x.( (P & x:S & P') => Q) |
| 2457 | | max_cardinality_ok_for_expansion(b(Expr,Type,_Info),Limit,LS,S) :- % which value to choose for Limit ?? |
| 2458 | | kernel_objects:max_cardinality(Type,TypeCard), |
| 2459 | | ( (number(TypeCard),TypeCard =< Limit) -> true ; |
| 2460 | | max_cardinality_ok_for_expansion2(Expr,Limit,TypeCard,LS,S)). |
| 2461 | | |
| 2462 | | max_cardinality_ok_for_expansion1(b(E,_,_),Limit,TC,LS,S) :- |
| 2463 | | max_cardinality_ok_for_expansion2(E,Limit,TC,LS,S). |
| 2464 | | |
| 2465 | | :- use_module(custom_explicit_sets,[efficient_card_for_set/3]). |
| 2466 | | |
| 2467 | | max_cardinality_ok_for_expansion2(set_extension(E),Limit,_TC,_,_) :- !, length(E,Len), Len =< Limit. |
| 2468 | | max_cardinality_ok_for_expansion2(interval(Up,Low),Limit,_TC,LS,S) :- !, |
| 2469 | | get_integer(UpI,Up,LS,S), get_integer(LowI,Low,LS,S), |
| 2470 | | UpI-LowI<Limit. |
| 2471 | | max_cardinality_ok_for_expansion2(integer_set(GS),Limit,_TC,_LS,_S) :- !, b_global_set_cardinality(GS,Card), |
| 2472 | | number(Card), Card =< Limit. |
| 2473 | | max_cardinality_ok_for_expansion2(set_subtraction(A,_),Limit,TC,LS,S) :- !, |
| 2474 | | max_cardinality_ok_for_expansion1(A,Limit,TC,LS,S). |
| 2475 | | max_cardinality_ok_for_expansion2(image(RelA,_),Limit,TC,LS,S) :- !, |
| 2476 | | max_cardinality_ok_for_expansion1(RelA,Limit,TC,LS,S). |
| 2477 | | max_cardinality_ok_for_expansion2(function(_RelA,_),_Limit,_TC,_LS,_S) :- !, |
| 2478 | | fail. % TO DO: we should look at the size of the range elements |
| 2479 | | % max_cardinality_ok_for_expansion1(_RelA,_Limit,_TC,_LS,_S). |
| 2480 | | max_cardinality_ok_for_expansion2(intersection(A,B),Limit,TC,LS,S) :- !, |
| 2481 | | (max_cardinality_ok_for_expansion1(A,Limit,TC,LS,S) -> true ; max_cardinality_ok_for_expansion1(B,Limit,TC,LS,S)). |
| 2482 | | max_cardinality_ok_for_expansion2(union(A,B),Limit,TC,LS,S) :- !, |
| 2483 | | max_cardinality_ok_for_expansion1(A,Limit,TC,LS,S), max_cardinality_ok_for_expansion1(B,Limit,TC,LS,S). |
| 2484 | | max_cardinality_ok_for_expansion2(range(A),Limit,TC,LS,S) :- !, |
| 2485 | | max_cardinality_ok_for_expansion1(A,Limit,TC,LS,S). |
| 2486 | | max_cardinality_ok_for_expansion2(domain(A),Limit,TC,LS,S) :- !, |
| 2487 | | max_cardinality_ok_for_expansion1(A,Limit,TC,LS,S). |
| 2488 | | max_cardinality_ok_for_expansion2(domain_restriction(_A,B),Limit,TC,LS,S) :- !, |
| 2489 | | max_cardinality_ok_for_expansion1(B,Limit,TC,LS,S). % note: finite A does not guarantee finite result |
| 2490 | | max_cardinality_ok_for_expansion2(domain_subtraction(_A,B),Limit,TC,LS,S) :- !, |
| 2491 | | max_cardinality_ok_for_expansion1(B,Limit,TC,LS,S). |
| 2492 | | max_cardinality_ok_for_expansion2(range_restriction(A,_B),Limit,TC,LS,S) :- !, |
| 2493 | | max_cardinality_ok_for_expansion1(A,Limit,TC,LS,S). % note: finite B does not guarantee finite result |
| 2494 | | max_cardinality_ok_for_expansion2(range_subtraction(A,_B),Limit,TC,LS,S) :- !, |
| 2495 | | max_cardinality_ok_for_expansion1(A,Limit,TC,LS,S). |
| 2496 | | max_cardinality_ok_for_expansion2(direct_product(A,B),Limit,TC,LS,S) :- !, |
| 2497 | | max_cardinality_ok_for_expansion2(cartesian_product(A,B),Limit,TC,LS,S). |
| 2498 | | max_cardinality_ok_for_expansion2(parallel_product(A,B),Limit,TC,LS,S) :- !, |
| 2499 | | max_cardinality_ok_for_expansion2(cartesian_product(A,B),Limit,TC,LS,S). |
| 2500 | | max_cardinality_ok_for_expansion2(cartesian_product(A,B),Limit,TC,LS,S) :- !, |
| 2501 | | ILimit is Limit//2, ILimit>0, %use sqrt ? |
| 2502 | | max_cardinality_ok_for_expansion1(A,ILimit,TC,LS,S), |
| 2503 | | max_cardinality_ok_for_expansion1(B,ILimit,TC,LS,S). |
| 2504 | | max_cardinality_ok_for_expansion2(value(S),Limit,_TC,_LS,_S) :- !, |
| 2505 | | get_max_card_for_value(S,MaxCard), |
| 2506 | | MaxCard =< Limit. |
| 2507 | | max_cardinality_ok_for_expansion2(identifier(_ID),_Limit,TC,_LS,_S) :- !, number(TC). |
| 2508 | | % TO DO: check if ID is fully defined; if so use card else accept as it will |
| 2509 | | % probably be represented by a list and the forall constraints could be useful in finding ID's value |
| 2510 | | % we have to store/compute ID anyway (could be infinite closure; hence check that TC is a number); a bit of a hack; is there a more principled way? |
| 2511 | | max_cardinality_ok_for_expansion2(external_function_call(_,_),_,_,_,_) :- !,fail. |
| 2512 | | max_cardinality_ok_for_expansion2(lazy_lookup_expr(_),_,_,_,_) :- !,fail. |
| 2513 | | max_cardinality_ok_for_expansion2(record_field(_,_),_Limit,_TC,_LS,_S) :- !, fail. |
| 2514 | | max_cardinality_ok_for_expansion2(E,Limit,TC,_LS,_S) :- functor(E,F,N), |
| 2515 | | print(unknown_max_card(F/N,Limit,TC)),nl,fail. |
| 2516 | | |
| 2517 | | |
| 2518 | | :- use_module(kernel_cardinality_attr,[clpfd_card_domain_for_var/3]). |
| 2519 | | |
| 2520 | | get_max_card_for_value(S,MaxCard) :- var(S), !, |
| 2521 | | clpfd_card_domain_for_var(S,_,Max), number(Max), |
| 2522 | | MaxCard=Max. |
| 2523 | | get_max_card_for_value([],R) :- !, R=0. |
| 2524 | | get_max_card_for_value([_|T],R) :- !,get_max_card_for_value(T,TMax), R is TMax+1. |
| 2525 | | get_max_card_for_value(S,Card) :- efficient_card_for_set(S,Card,C),!, |
| 2526 | | call(C), number(Card). |
| 2527 | | |
| 2528 | | |
| 2529 | | get_integer(X, b(Expr,integer,_),LS,S) :- get_integer2(Expr,X,LS,S). |
| 2530 | | get_integer2(integer(X),X,_,_) :- number(X). |
| 2531 | | get_integer2(identifier(Id),Val,LS,S) :- lookup_value(Id,LS,S,int(Val)), number(Val). |
| 2532 | | |
| 2533 | | member_conjunct(Predicate,Conjunction,Rest) :- |
| 2534 | | conjunction_to_list(Conjunction,List), |
| 2535 | ? | select(Predicate,List,RestList), |
| 2536 | | conjunct_predicates(RestList,Rest). |
| 2537 | | % member_conjunct(Conj,Conj,b(truth,pred,[])). covered by case above |
| 2538 | | |
| 2539 | | b_generate_set_for_all_domain(Set,LS,S,Pattern,ParameterValues,ForAllDomain,WF) :- |
| 2540 | | b_compute_expression(Set,LS,S,EvSet,WF), |
| 2541 | | expand_custom_set_to_list_wf(EvSet,ESet,_Done,b_generate_set_for_all_domain,WF), |
| 2542 | | filter_and_convert_list_to_parameter_tuples(ESet,Pattern,ParameterValues,ForAllDomain). |
| 2543 | | |
| 2544 | | |
| 2545 | | % check if it is a predicate of the form x:S or x:Sx & y:Sy ... which we can optimize |
| 2546 | | % (i.e. compute SET before all values inside it are fully known) |
| 2547 | | is_for_all_set_membership_predicate(LHS,Parameters,Set,Pattern,ParameterValues) :- |
| 2548 | | % print('CHECK FORALL : '), translate:print_bexpr(LHS),nl,nl, |
| 2549 | | get_texpr_ids(Parameters,ParIDs), % TO DO: call get_texpr_ids before |
| 2550 | | same_length(ParIDs,ParameterValues), |
| 2551 | | is_for_all_set_membership_predicate2(LHS,ParIDs,ParIDs,UnmatchedParIDs,Set,Pattern,ParameterValues,_UPV), |
| 2552 | | UnmatchedParIDs=[]. |
| 2553 | | |
| 2554 | | % TODO: maybe also deal with existential quantifiers #x.(x|->y:Set) -> in Pattern x becomes free var |
| 2555 | | is_for_all_set_membership_predicate2(LHS,All,ParIDs,UnmatchedParIDs,Set,Pattern,ParameterValues,UPV) :- |
| 2556 | | is_member_test(LHS,Element,Set), |
| 2557 | | !, |
| 2558 | | identifiers_not_used_in_bexp(All,Set), |
| 2559 | | convert_to_prolog_pattern_match(Element,ParIDs,UnmatchedParIDs,Pattern,ParameterValues,UPV). |
| 2560 | | % TO DO: also allow failing pattern matches ?!; at least when splitting |
| 2561 | | is_for_all_set_membership_predicate2(LHS,All,ParIDs,ParIDs2,Set, |
| 2562 | | (Pat1,Pat2),ParameterValues,UPV) :- |
| 2563 | | is_a_conjunct(LHS,LHS1,LHS2), % we could have something of the form x:Sx & y:Sy ... |
| 2564 | | Set = b(cartesian_product(Set1,Set2),couple(T1,T2),[]), |
| 2565 | | is_for_all_set_membership_predicate2(LHS1,All,ParIDs, ParIDs1,Set1,Pat1,ParameterValues,PV1), |
| 2566 | | get_texpr_type(Set1,T1), |
| 2567 | | !, |
| 2568 | | is_for_all_set_membership_predicate2(LHS2,All,ParIDs1,ParIDs2,Set2,Pat2,PV1,UPV), |
| 2569 | | get_texpr_type(Set2,T2). |
| 2570 | | |
| 2571 | | % TO DO: maybe use a more general test to be reused in other places |
| 2572 | | is_member_test(b(P,_,_),Element,Set) :- is_member_test_aux(P,Element,Set). |
| 2573 | | is_member_test_aux(member(Element,Set),Element,Set). |
| 2574 | | % also detect equalities: important for tests 28,29 after b_compiler can introduce equalities for singleton set memberships |
| 2575 | | is_member_test_aux(equal(Element,SetVal),Element,Set) :- % TO DO : also other direction + other kinds of values ? |
| 2576 | | SetVal = b(value(SV),T,I), |
| 2577 | | Set = b(value([SV]),set(T),I). |
| 2578 | | |
| 2579 | | identifiers_not_used_in_bexp(IDList,Expr) :- |
| 2580 | | find_identifier_uses(Expr,[],Used), |
| 2581 | | id_check(IDList,Used). |
| 2582 | | id_check([],_). |
| 2583 | | id_check([ID|T],Used) :- |
| 2584 | | nonmember(ID,Used), !, |
| 2585 | | id_check(T,Used). |
| 2586 | | |
| 2587 | | % check if an expression can be converted to a Prolog linear unification/pattern match |
| 2588 | | % give an list of free identifiers IDS |
| 2589 | | convert_to_prolog_pattern_match(b(E,_,_),IDS,OutIDS,Pattern,VarsIn,VarsOut) :- |
| 2590 | | convert_to_prolog_pattern_match2(E,IDS,OutIDS,Pattern,VarsIn,VarsOut). |
| 2591 | | |
| 2592 | | convert_to_prolog_pattern_match2(identifier(ID),IDsIn,IDsOut,PatVariable,VarsIn,VarsOut) :- |
| 2593 | | % check that the identifier is in the list of quantified variables and thus unbound |
| 2594 | | !, |
| 2595 | ? | (nth1(Nr,IDsIn,ID,IDsOut) |
| 2596 | | -> nth1(Nr,VarsIn,PatVariable,VarsOut) % put fresh PatVariable in the Pattern |
| 2597 | | ),!. |
| 2598 | | convert_to_prolog_pattern_match2(couple(ID1,ID2),IDS,OutIDS,Pattern,VarsIn,VarsOut) :- |
| 2599 | | Pattern = (Pat1,Pat2), |
| 2600 | | convert_to_prolog_pattern_match(ID1,IDS,RemIDS,Pat1,VarsIn,VarsInt), |
| 2601 | | convert_to_prolog_pattern_match(ID2,RemIDS,OutIDS,Pat2,VarsInt,VarsOut). |
| 2602 | | convert_to_prolog_pattern_match2(value(V),IDsIn,IDsOut,Pattern,VarsIn,VarsOut) :- |
| 2603 | | can_be_used_for_unification(V), % check if we can use Prolog unification for equality |
| 2604 | | !, Pattern=V, IDsOut=IDsIn, VarsIn=VarsOut. |
| 2605 | | % TO DO: records? |
| 2606 | | |
| 2607 | | % a pattern match code to be used with above; ensure we do not instantiate values |
| 2608 | | pattern_match(PatVar,Val,Res) :- var(PatVar),!,PatVar=Val, Res=pred_true. % we have a free pattern variable |
| 2609 | | pattern_match((Pat1,Pat2),(V1,V2),Res) :- !, |
| 2610 | | pattern_match(Pat1,V1,R1), conjoin_simple(R1,R2,Res), |
| 2611 | | pattern_match(Pat2,V2,R2). |
| 2612 | | pattern_match(GroundPatVal,Val,Res) :- % we have a value that can_be_used_for_unification |
| 2613 | | when( ?=(GroundPatVal,Val), (GroundPatVal=Val -> Res=pred_true ; Res=pred_false)). |
| 2614 | | |
| 2615 | | :- block conjoin_simple(-,?,?). % TO DO: also allow second arg to unblock |
| 2616 | | conjoin_simple(pred_true,X,X). |
| 2617 | | conjoin_simple(pred_false,_,pred_false). |
| 2618 | | |
| 2619 | | % similar to get_template(V,T,[]) |
| 2620 | | % but more restricted; we only accept those values where equality corresponds to Prolog unification |
| 2621 | | % get_template is different and can assume normalized values, e.g., for empty set |
| 2622 | | % pairs are treated above in convert_to_prolog_pattern_match2 |
| 2623 | | can_be_used_for_unification(V) :- var(V),!,fail. |
| 2624 | | can_be_used_for_unification(int(V)) :- nonvar(V). |
| 2625 | | can_be_used_for_unification(string(S)) :- ground(S). |
| 2626 | | can_be_used_for_unification(fd(S,T)) :- ground(S), ground(T). |
| 2627 | | can_be_used_for_unification(pred_false). |
| 2628 | | can_be_used_for_unification(pred_true). |
| 2629 | | |
| 2630 | | |
| 2631 | | |
| 2632 | | % take a list and a pattern and filter the values that do not match the Pattern |
| 2633 | | % Unification with the Pattern also instantiates the ParameterValues |
| 2634 | | % TODO: Instead of converting to a Prolog Unification we could use kernel_equality instead of pattern_match |
| 2635 | | % but this is not obvious, as the pattern contains free variables and equality would never return true |
| 2636 | | % we would need to apply kernel_equality only to the ground pattern parts |
| 2637 | | :- block filter_and_convert_list_to_parameter_tuples(-,?,?,?). |
| 2638 | | filter_and_convert_list_to_parameter_tuples([],_,_,[]). |
| 2639 | | filter_and_convert_list_to_parameter_tuples([H|T],Pattern,ParameterValues,Res) :- |
| 2640 | | copy_term((Pattern,ParameterValues),(CPat,CPV)), |
| 2641 | | pattern_match(CPat,H,PredRes), % check if pattern matches new element H in set; careful not to instantiate H |
| 2642 | ? | filter_and_convert_list_to_parameter_tuples_aux(PredRes,CPV,T,Pattern,ParameterValues,Res). |
| 2643 | | |
| 2644 | | :- block filter_and_convert_list_to_parameter_tuples_aux(-,?,-,?,?,?). |
| 2645 | | filter_and_convert_list_to_parameter_tuples_aux(PredRes,CPV,T,Pattern,ParameterValues,Res) :- |
| 2646 | | var(PredRes),!, % we cannot decide yet whether the optional value is to be used or not |
| 2647 | | Res = [optional_value(CPV,PredRes)|RT], % create constructor for map_optlist or similar |
| 2648 | | filter_and_convert_list_to_parameter_tuples(T,Pattern,ParameterValues,RT). |
| 2649 | | filter_and_convert_list_to_parameter_tuples_aux(PredRes,CPV,T,Pattern,ParameterValues,Res) :- |
| 2650 | | (PredRes = pred_true % we have a match, universal quantifier applies to H |
| 2651 | | -> Res = [CPV|RT] |
| 2652 | | ; % element of set does not match pattern; universal quantifier does not apply |
| 2653 | | % ProB used to only accept non-failing pattern matches; |
| 2654 | | % now we also accept tuples with static parts where Prolog unification can fail |
| 2655 | | Res=RT |
| 2656 | | ), |
| 2657 | ? | filter_and_convert_list_to_parameter_tuples(T,Pattern,ParameterValues,RT). |
| 2658 | | |
| 2659 | | :- use_module(tools,[map_optlist/2,optlist_to_list/2]). |
| 2660 | | :- use_module(library(lists),[maplist/3]). |
| 2661 | | expand_forall1(OList,RHS,Parameters,WF) :- |
| 2662 | | ground_bexpr(RHS), % because compiled this means we cannot do any constraint propagation on variables in RHS |
| 2663 | | optlist_to_list(OList,List), |
| 2664 | | ground_value(List), % ditto here: no variables on which we could do constraint propagation |
| 2665 | | %% preferences:get_preference(double_evaluation_when_analysing,false), % causes test 1112 to fail TO DO:fix |
| 2666 | | % otherwise we do not have a real redundancy wrt to not exists check |
| 2667 | | debug_println(15,ground_forall_translating_into_not_exists(Parameters)), |
| 2668 | | !, |
| 2669 | | %kernel_waitflags:init_wait_flags(LocalWF,[expansion_context(forall,Parameters)]), |
| 2670 | | init_quantifier_wait_flag(WF,forall,Parameters,ParResult,unknown,LocalWF), |
| 2671 | | % Advantage over treatment below: NegRHS is interpreted only once |
| 2672 | | % but we cannot provide any instantiations and there must not be any links with variables outside of the forall !! |
| 2673 | | create_negation(RHS,NegRHS), |
| 2674 | | not_with_enum_warning( ( |
| 2675 | | b_interpreter:test_forall_instance(NegRHS,Parameters,LocalWF,negative,ParResult), % can fail; if inconsistency detected in WF0 |
| 2676 | | kernel_waitflags:ground_det_wait_flag(LocalWF), |
| 2677 | | member(ParResultInList,List), % check the body for every solution |
| 2678 | | lists:maplist(kernel_objects:equal_object,ParResultInList,ParResult), % match parameters with the solutions of the domain |
| 2679 | | kernel_waitflags:ground_wait_flags(LocalWF) % do not ground EF? ground_constraintprop_wait_flags ground_inner_wait_flags |
| 2680 | | ), LocalWF, forall(Parameters), RHS |
| 2681 | | ). |
| 2682 | | expand_forall1(List,RHS,Parameters,WF) :- |
| 2683 | ? | map_optlist(b_interpreter:test_forall_instance(RHS,Parameters,WF,positive),List). |
| 2684 | | |
| 2685 | | |
| 2686 | | test_forall_instance(RHS,Parameters,WF,NegationContext,ParResult) :- |
| 2687 | | LocalState=[],State=[], |
| 2688 | ? | if(set_up_typed_localstate(Parameters,ParResult,_TypedVals,LocalState,NewLocalState,NegationContext), |
| 2689 | | true, |
| 2690 | | (nl,print(set_up_typed_localstate_failed(Parameters,ParResult,_,LocalState,NewLocalState)),nl, |
| 2691 | | % it can happen that ParResult has constraints on it and that setting up the typing info detects the error |
| 2692 | | % add_internal_error('Call failed: ', set_up_typed_localstate(Parameters,ParResult,_,LocalState,NewLocalState)), |
| 2693 | | fail)), |
| 2694 | ? | b_optimized_b_test_instance(RHS,NewLocalState,State,WF). |
| 2695 | | |
| 2696 | | reification_relevant_for_op(implication(_,_)). |
| 2697 | | reification_relevant_for_op(equivalence(_,_)). |
| 2698 | | reification_relevant_for_op(disjunct(_,_)). |
| 2699 | | % TODO: maybe check if a conjunct contains implication |
| 2700 | | % relevant for example for: n=2000 & f:1..n-->BOOL & !i.(i:2..n => (f(i)=TRUE =>f(i-1)=FALSE)) & f(1)=TRUE |
| 2701 | | |
| 2702 | | b_optimized_b_test_instance(RHS,NewLocalState,State,WF) :- |
| 2703 | | get_texpr_expr(RHS,RE), reification_relevant_for_op(RE), % we will attempt reification |
| 2704 | | attempt_reification, |
| 2705 | | \+ always_well_defined(RHS), % this will prevent reification |
| 2706 | | !, |
| 2707 | | % try and compile in the hope that now the WD issue disappears: |
| 2708 | | b_compiler:b_optimize(RHS,[],NewLocalState,State,NewRHS,WF), |
| 2709 | | b_test_boolean_expression(NewRHS,NewLocalState,State,WF). |
| 2710 | | b_optimized_b_test_instance(RHS,NewLocalState,State,WF) :- |
| 2711 | ? | b_test_boolean_expression(RHS,NewLocalState,State,WF). |
| 2712 | | % using b_test_inner_boolean_expression considerably slows down NQueens, Sudoku,... |
| 2713 | | % WF=InnerWF. /* give the boolean expression a chance to do ground propagations first */ |
| 2714 | | |
| 2715 | | |
| 2716 | | % use this when the test boolean expression could be called with WF0 already set |
| 2717 | | % give the inner expression a chance to do the deterministic stuff first |
| 2718 | | b_test_inner_boolean_expression(Pred,LocalState,State,WF) :- |
| 2719 | | kernel_waitflags:clone_wait_flags_from1(WF,GroundUponFinish,WF2), |
| 2720 | | % set WF0 to variable to propagate deterministic infos first: |
| 2721 | | b_test_boolean_expression(Pred,LocalState,State,WF2), |
| 2722 | ? | kernel_waitflags:clone_wait_flags_from1_finish(WF,GroundUponFinish,WF2). |
| 2723 | | % the same for testing negation of a predicate: |
| 2724 | | b_not_test_inner_boolean_expression(Pred,LocalState,State,WF) :- |
| 2725 | | kernel_waitflags:clone_wait_flags_from1(WF,GroundUponFinish,WF2), |
| 2726 | | % set WF0 to variable to propagate deterministic infos first: |
| 2727 | | b_not_test_boolean_expression(Pred,LocalState,State,WF2), |
| 2728 | ? | kernel_waitflags:clone_wait_flags_from1_finish(WF,GroundUponFinish,WF2). |
| 2729 | | |
| 2730 | | /* now the same as above; but without converting list of values into pairs */ |
| 2731 | | :- assert_pre(b_interpreter:b_expand_closure_forall_to_list(Closure,_Result,_WF), |
| 2732 | | (nonvar(Closure), custom_explicit_sets:is_closure(Closure,_Parameters,_ParameterTypes,ClosureBody), |
| 2733 | | nonvar(ClosureBody), |
| 2734 | | bsyntaxtree:check_if_typed_predicate(ClosureBody))). |
| 2735 | | :- assert_post(b_interpreter:b_expand_closure_forall_to_list(_Closure,Result,_WF), |
| 2736 | | b_interpreter:value_type(Result)). |
| 2737 | | b_expand_closure_forall_to_list(Closure,Result,WF) :- nonvar(Closure), |
| 2738 | | Closure = closure(Parameters,ParameterTypes,ClosureBody), |
| 2739 | | !, |
| 2740 | | % print('Expand closure forall: '), translate:print_bexpr(ClosureBody),nl, |
| 2741 | | % at the moment we may wait too long before expanding this closure |
| 2742 | | % what if we have f(1) in the ClosureBody: in principle we only need to wait until f(1) known or even f sekeleton set up ? compilation now solves this issue ? |
| 2743 | | (performance_monitoring_on, |
| 2744 | | term_variables(ClosureBody,Vars), |
| 2745 | | Vars \= [] |
| 2746 | | -> |
| 2747 | | perfmessagecall(delaying_forall(Parameters,vars(Vars),out(ParValues)), |
| 2748 | | translate:print_bexpr(ClosureBody),ClosureBody) |
| 2749 | | ; true), |
| 2750 | | ground_bexpr_check(ClosureBody,BodyGround), |
| 2751 | | add_texpr_infos(ClosureBody,[quantifier_kind(forall)],ClosureBody1), % for call_stack |
| 2752 | | delay_setof_list_wf( ParValues, |
| 2753 | | %% (print_bt_message(expanding_forall_closure(Parameters,ClosureBody,ParValues)), %% |
| 2754 | | custom_explicit_sets:b_test_closure(Parameters,ParameterTypes,ClosureBody1, |
| 2755 | | ParValues,all_solutions,WF) |
| 2756 | | %% ,print_bt_message(expanded(Parameters,ParValues)) ) %% |
| 2757 | | , |
| 2758 | | Result, |
| 2759 | | BodyGround,WF). |
| 2760 | | b_expand_closure_forall_to_list(Closure,Result,WF) :- |
| 2761 | | add_internal_error('Illegal Call: ', b_expand_closure_forall_to_list(Closure,Result,WF)), |
| 2762 | | fail. |
| 2763 | | |
| 2764 | | |
| 2765 | | :- assert_pre(b_interpreter:set_up_typed_localstate(V,S,_), (list_skeleton(V),list_skeleton(S))). |
| 2766 | | :- assert_post(b_interpreter:set_up_typed_localstate(_,_,R),list_skeleton(R)). |
| 2767 | | |
| 2768 | | set_up_typed_localstate(Identifiers,InState,OutState) :- |
| 2769 | | set_up_typed_localstate(Identifiers,_FreshVars,_TypedVals,InState,OutState,positive). |
| 2770 | | |
| 2771 | | :- use_module(b_enumerate,[construct_typedval_infos/4]). |
| 2772 | | set_up_typed_localstate([],[],[],State,State,_). |
| 2773 | | set_up_typed_localstate([Identifier|IdRest],[Val|ValRest],[typedval(Val,Type,Var,TINFO)|TRest], |
| 2774 | | % TO DO: add info field to typedval: source_span, and lambda_result/do_not_enumerate infos |
| 2775 | | InState,OutState,NegationContext) :- |
| 2776 | | % The other way round (first add Identfier, then IdRest) would be more efficient, |
| 2777 | | % but we do it this way to keep compatibility with a previous version. |
| 2778 | | % In specfile:compute_operation_effect, the arguments of an operation are in the same order than in the store |
| 2779 | ? | set_up_typed_localstate(IdRest,ValRest,TRest,InState,InterState,NegationContext), |
| 2780 | | def_get_texpr_id(Identifier,Var), get_texpr_type(Identifier,Type), |
| 2781 | | get_texpr_info(Identifier,Info), |
| 2782 | | triggers_enum_warning(Var,Type,Info,NegationContext,TriggersEnumWarning), |
| 2783 | | get_texpr_info(Identifier,Infos),construct_typedval_infos(Var,Infos,TriggersEnumWarning,TINFO), |
| 2784 | ? | add_typed_var_to_localstate(Var,Val,Type,InterState,OutState). |
| 2785 | | |
| 2786 | | % same version; but with list of atomic ids and list of types |
| 2787 | | set_up_typed_localstate2([],[],_,[],[],State,State,_). |
| 2788 | | set_up_typed_localstate2([Var|IdRest],[Type|TyRest],Infos,[Val|ValRest],[typedval(Val,Type,Var,TINFO)|TRest], |
| 2789 | | InState,OutState,NegationContext) :- |
| 2790 | | % The other way round (first add Identfier, then IdRest) would be more efficient, |
| 2791 | | % but we do it this way to keep compatibility with a previous version. |
| 2792 | | % In specfile:compute_operation_effect, the arguments of an operation are in the same order than in the store |
| 2793 | ? | set_up_typed_localstate2(IdRest,TyRest,Infos,ValRest,TRest,InState,InterState,NegationContext), |
| 2794 | | triggers_enum_warning(Var,Type,Infos,NegationContext,TriggersEnumWarning), |
| 2795 | | construct_typedval_infos(Var,Infos,TriggersEnumWarning,TINFO), |
| 2796 | ? | add_typed_var_to_localstate(Var,Val,Type,InterState,OutState). |
| 2797 | | |
| 2798 | | % possible values for NegationContext: positive, negative, all_solutions |
| 2799 | | triggers_enum_warning(Id,Type,Info,all_solutions,Res) :- !, |
| 2800 | | Res = trigger_throw(b(identifier(Id),Type,Info)). |
| 2801 | | %triggers_enum_warning(Id,_Type,Info,NegationContext,trigger_false(Id)) :- % no longer used |
| 2802 | | % NegationContext=positive, % when negated then success will occur if all possibilities have been tried out |
| 2803 | | % memberchk(introduced_by(exists),Info), |
| 2804 | | % %%((preferences:get_preference(disprover_mode,true),type_contains_unfixed_deferred_set(Type))-> fail % otherwise %finding a solution may depend on the size of the deferred set; true), |
| 2805 | | % %% The Disprover now checks whether unfixed_deferred sets were involved; keeping track of implicit enumerations of %deferred sets is just too difficult |
| 2806 | | % !. |
| 2807 | | triggers_enum_warning(Id,Type,Info,_,trigger_true(b(identifier(Id),Type,Info))). |
| 2808 | | |
| 2809 | | |
| 2810 | | add_typed_var_to_localstate(Var,Val,Type,InState,OutState) :- |
| 2811 | | add_var_to_localstate(Var,Val,InState,OutState), |
| 2812 | | %%when(ground(Val), (print(' ++ '),print(Var),print(' ---> '), print(Val),nl)), |
| 2813 | ? | kernel_objects:basic_type(Val,Type). %% ,print(ok(Val)),nl. |
| 2814 | | |
| 2815 | | /* |
| 2816 | | xxb_test_exists(Parameters,Condition,Infos,LocalState,State,WF) :- |
| 2817 | | b_check_boolean_expression(b(exists(Parameters,Condition),pred,Infos),LocalState,State,WF,NR), |
| 2818 | | get_exists_used_ids(Parameters,Condition,Infos,Used),lookup_values_if_vars(Used,LocalState,State,WaitVars), |
| 2819 | | \+ ground(WaitVars), |
| 2820 | | !, |
| 2821 | | debug_println(9,unfolding_exists(Parameters)), print(unfolding(Parameters)),nl, |
| 2822 | | %translate:print_bexpr(Condition),nl, print(State),nl, print(LocalState),nl,nl, |
| 2823 | | NR=pred_true. %, print(set_res(pred_true)),nl. |
| 2824 | | % if enabled it slows down: ./probcli examples/EventBPrologPackages/SET_Game/SET_GAM_Sym_NoSet20_mch.eventb -mc 10000000 -p SYMMETRY_MODE hash -p TIME_OUT 7000 -p CLPFD TRUE -df -goal "n=18" -p MAX_OPERATIONS 20 -strict -expcterr goal_found |
| 2825 | | */ |
| 2826 | | |
| 2827 | | b_test_exists(Parameters,Condition,Infos,LocalState,State,WF) :- |
| 2828 | | get_wait_flag0(WF,WF0), |
| 2829 | ? | b_test_exists(WF0,Parameters,Condition,Infos,LocalState,State,WF). |
| 2830 | | |
| 2831 | | :- block b_test_exists(-,?,?, ?,?,?,?). |
| 2832 | | b_test_exists(_LWF,Parameters,Condition,Infos,LocalState,State,WF) :- |
| 2833 | | if((attempt_reification, % what if there is a wd_condition attached to Condition? |
| 2834 | | b_interpreter_check:b_check_exists_wf(Parameters,Condition,Infos,LocalState,State,WF,ReificationVariable)), |
| 2835 | | % try and expand quantifiers of small cardinality; see also expand_forall_quantifier |
| 2836 | | % tested in test 1452 |
| 2837 | | (debug_println(9,expanded_exists(Parameters)), |
| 2838 | | ReificationVariable=pred_true), |
| 2839 | ? | b_test_exists_wo_expansion(Parameters,Condition,Infos,LocalState,State,WF)). |
| 2840 | | |
| 2841 | | %attempt_reification :- preferences:preference(use_clpfd_solver,true),!. % all tests seem to pass |
| 2842 | | attempt_reification :- preferences:preference(use_smt_mode,true),!. |
| 2843 | | attempt_reification :- preferences:preference(solver_strength,SS), SS>9. |
| 2844 | | % TO DO: maybe take context into account: setup constants, becomes such, ... allow_skipping_over_components/reset_component_info(true) |
| 2845 | | |
| 2846 | | :- use_module(kernel_tools,[bexpr_variables/2, value_variables/3]). |
| 2847 | | |
| 2848 | | % we treat the existential quantifier without expansion into a disjuncition aka reification |
| 2849 | | b_test_exists_wo_expansion(Parameters,Condition,Infos,LocalState,State,WF) :- |
| 2850 | | %print(test_exists(Parameters,Infos)),nl,portray_waitflags(WF),nl, |
| 2851 | ? | (preference(lift_existential_quantifiers,true) ; member(allow_to_lift_exists,Infos)), |
| 2852 | | !, % we enumerate the exists normally; treat it just like an ordinary predicate |
| 2853 | | % lifting may generate multiple solutions; but avoids delaying enumeration of quantified parameters |
| 2854 | | % print('*Lifting existential quantifier: '), translate:print_bexpr(Condition),nl, |
| 2855 | | set_up_typed_localstate(Parameters,ParaValues,TypedVals,LocalState,NewLocalState,positive), |
| 2856 | | copy_wf_start(WF,b_test_exists_wo_expansion,CWF), |
| 2857 | | opt_push_wait_flag_call_stack_quantifier_info(CWF,exists,Parameters,ParaValues,Infos,CWF2), |
| 2858 | | b_test_boolean_expression(Condition,NewLocalState,State,CWF2), |
| 2859 | | % often we do not need to enumerate TypedVals; they can be derived, but we could have #y.(y>x) and need to enum y |
| 2860 | | b_tighter_enumerate_values_in_ctxt(TypedVals,Condition,CWF2), |
| 2861 | | copy_wf_finish(WF,CWF2). % moving this before tighter_enum may mean that we have more precise ranges; but this can be counter-productive (e.g., for test 1162 unless we strengthen the useless enum analysis) |
| 2862 | | b_test_exists_wo_expansion(Parameters,Condition,Infos,LocalState,State,WF) :- |
| 2863 | | get_exists_used_ids(Parameters,Condition,Infos,Used), |
| 2864 | | % IT IS IMPORTANT THAT THE USED INFO IS CORRECT ; otherwise the WaitVars will be incorrect |
| 2865 | | % The used_ids info does not include the Parameters of the existential quantifier ! |
| 2866 | ? | set_up_typed_localstate(Parameters,ParaValues,TypedVals,LocalState,NewLocalState,positive), |
| 2867 | | lookup_values_if_vars(Used,LocalState,State,WaitVarsState), % TO DO: we could use b_compiler instead |
| 2868 | | % What if the Condition was compiled and now contains a previously used_variable inside a closure/value ? |
| 2869 | | %term_variables((Condition,WaitVarsState),WaitVars), % this can be expensive when we have large data values (see rule_RVF219/rule_RVF219_compo.mch) |
| 2870 | | bexpr_variables(Condition,CondVars), |
| 2871 | | value_variables(WaitVarsState,CondVars,WaitVars), |
| 2872 | | % now determine which WaitVars are really worth waiting for, e.g., do not wait for res in #x.(x:E & ... res=min(f(x))) |
| 2873 | | % relevant tests: 1868, 1194, 383, 1642, 1708, 1003, 1080, 1081, 1943 |
| 2874 | | (remove_ids_defined_by_equality(WaitVars,LocalState,State,Infos,RealWaitVars) -> RemovedVars=true |
| 2875 | | ; RealWaitVars = WaitVars, RemovedVars=false), |
| 2876 | | opt_push_wait_flag_call_stack_quantifier_info(WF,exists,Parameters,ParaValues,Infos,WF2), |
| 2877 | | create_inner_wait_flags(WF2,expansion_context(b_test_exists,Parameters),LocalWF), % we are creating inner waitflags here as the exist will wait anyway; LocalWF shares just WFE flag with WF |
| 2878 | | b_test_boolean_expression(Condition,NewLocalState,State,LocalWF), /* check Condition */ |
| 2879 | ? | ground_det_wait_flag(LocalWF), % print('det no contradiction'),nl,flush_output, |
| 2880 | | b_tighter_enumerate_values_in_ctxt(TypedVals,Condition,LocalWF), % will not yet enumerate |
| 2881 | | (% we could treat allow_to_lift_exists also here by setting ESWF=1; but 1162 then fails |
| 2882 | | get_preference(data_validation_mode,true), |
| 2883 | | not_generated_exists_paras(Parameters) % generated exists, e.g., for relational composition can lead to virtual timeouts, cf Thales_All/rule_zcpa2.mch in test 2287 |
| 2884 | | -> get_wait_flag(50000,b_test_exists(Parameters),WF,ESWF) % 100000 sufficient for test 1945; 869 for rule_CHAMPS in private_examples/ClearSy/2023 |
| 2885 | | ; get_enumeration_starting_wait_flag(b_test_exists(Parameters),WF,ESWF)), %% TO DO: get flag when enumeration of infinite type starts; maybe if domain of exists is small we can start enumerating earlier ?? <-> relation with lifting exists in closure expansion heuristic |
| 2886 | | when((nonvar(ESWF);ground(RealWaitVars)),b_enumerate_exists(ESWF,RemovedVars,WaitVars,Parameters,Infos,LocalWF,WF)). |
| 2887 | | |
| 2888 | | |
| 2889 | | :- use_module(tools,[remove_variables/3]). |
| 2890 | | :- use_module(kernel_tools,[value_variables/2]). |
| 2891 | | % if we have #x.( P & y = E(x)) we do not have to wait for y to be known |
| 2892 | | remove_ids_defined_by_equality(WaitVars,LocalState,State,Infos,RealWaitVars) :- WaitVars \== [], |
| 2893 | | memberchk(used_ids_defined_by_equality(Ideq),Infos), |
| 2894 | | Ideq = [ID1|_], % currently we only look at one used_id defined by equality, |
| 2895 | | % we could have #x.(P & y=E(z) & z=E(y)): TODO: improve computation in ast_cleanup |
| 2896 | | (memberchk(bind(ID1,VAL),LocalState) -> true ; memberchk(bind(ID1,VAL),State)), |
| 2897 | | value_variables(VAL,VARS), |
| 2898 | | remove_variables(WaitVars,VARS,RealWaitVars), |
| 2899 | | debug_println(9,removed_ids_def_by_equality(WaitVars,ID1,RealWaitVars)). |
| 2900 | | |
| 2901 | | |
| 2902 | | b_enumerate_exists(ESWF,RemovedVars,WaitVars,Parameters,Infos,LocalWF,WF) :- |
| 2903 | | %print(enum_exists(ESWF,RemovedVars,WaitVars,Parameters,LocalWF,WF)),nl, |
| 2904 | | ((RemovedVars=false,var(ESWF) ; % WaitVars guaranteed to be ground |
| 2905 | | ground(WaitVars)) % TODO: replace this by an optimised version of ground check |
| 2906 | | -> get_idle_flag(Parameters,WF,LWF), |
| 2907 | | /* allow all other co-routines waiting on WaitVars to complete */ |
| 2908 | | /* Note however: if WF grounding as triggered two non-deterministic co-routines this |
| 2909 | | will allow the other non-determinism to run first, duplicating the number of calls ! |
| 2910 | | This has been fixed by storing seperate waitflag variables for same prio in kernel_waitflags |
| 2911 | | TODO: However, for enumeration of FD variables this might still happen and lead to duplication of |
| 2912 | | calls to b_enumerate_exists_aux_ground |
| 2913 | | */ |
| 2914 | | %print(get_idle(Parameters,WaitVars)),nl, %kernel_waitflags:portray_waitflags(WF),nl,nl, |
| 2915 | | b_enumerate_exists_aux_ground(LWF,WaitVars,Parameters,Infos,LocalWF,WF) |
| 2916 | | ; debug_println(9,semi_lifting_exists(ESWF,Parameters)), |
| 2917 | | % add_message_wf(exists,'Semi-lifting exists:',Parameters,Parameters,WF),portray_waitflags(LocalWF),nl, |
| 2918 | | kernel_waitflags:copy_waitflag_store(LocalWF,WF) % outer WF will now drive enumeration |
| 2919 | | % Note: in case a LET has been moved inside and replaced this may lead to duplication |
| 2920 | | ). |
| 2921 | | |
| 2922 | | :- if(environ(prob_safe_mode,true)). |
| 2923 | | get_idle_flag(Parameters,WF,LWF) :- kernel_waitflags:get_idle_wait_flag(b_enumerate_exists(Parameters),WF,LWF), |
| 2924 | | (var(LWF) -> true ; add_internal_error('Illegal idle waitflag for exists:',get_idle_flag(Parameters,WF,LWF))). |
| 2925 | | :- else. |
| 2926 | | get_idle_flag(Parameters,WF,LWF) :- kernel_waitflags:get_idle_wait_flag(b_enumerate_exists(Parameters),WF,LWF). |
| 2927 | | :- endif. |
| 2928 | | |
| 2929 | | :- block b_enumerate_exists_aux_ground(-,?,?,?,?,?). |
| 2930 | | b_enumerate_exists_aux_ground(_,_WaitVars,Parameters,Infos,LocalWF,_WF) :- |
| 2931 | | /* Note: this does a local cut: so it is important |
| 2932 | | that all conditions are fully evaluated before performing the cut ; otherwise |
| 2933 | | solutions will be lost */ |
| 2934 | | % print_bt_message('ENUMERATE EXISTS'(_Parameters)),nl, % portray_waitflags(_WF), |
| 2935 | | if(ground_wait_flags_for_exists(Parameters,LocalWF),true, |
| 2936 | | (perfmessage(exists,'Late bound exists failed: ',Parameters,Infos),fail)). |
| 2937 | | |
| 2938 | | |
| 2939 | | ground_wait_flags_for_exists(Parameters,LocalWF) :- |
| 2940 | | \+ enumeration_warning_occured_in_error_scope, |
| 2941 | | get_current_error_scope(Level), |
| 2942 | ? | \+ error_manager:throw_error(Level,_,_), |
| 2943 | | !, % current scope is already clean, no need to enter a fresh one |
| 2944 | | ground_wait_flags_for_exists2(Parameters,LocalWF,Level). |
| 2945 | | ground_wait_flags_for_exists(Parameters,LocalWF) :- |
| 2946 | | enter_new_clean_error_scope(Level), |
| 2947 | | % Note: see {x|x:1..10 & #y.(y>x & y*y >10)} in test 2210: |
| 2948 | | % we do not want to throw enum warnings even if the outer scope does in case a solution is found |
| 2949 | | % NOTE: if this engenders an enumeration warning: it may have been better to enumerate the outer WF first ?! : but as WaitVars are ground, so only failure of outer WF could help us here in preventing enumeration warning |
| 2950 | | call_cleanup(ground_wait_flags_for_exists2(Parameters,LocalWF,Level), |
| 2951 | | exit_error_scope(Level,_,ground_wait_flags_for_exists)). |
| 2952 | | |
| 2953 | | ground_wait_flags_for_exists2(_Parameters,LocalWF,Level) :- |
| 2954 | ? | (ground_constraintprop_wait_flags(LocalWF) |
| 2955 | | -> % local cut: one solution is sufficient. |
| 2956 | | clear_enumeration_warning_in_error_scope(Level) |
| 2957 | | % ; %print(fail),nl, % do not clear enumeration warnings fail |
| 2958 | | ). |
| 2959 | | |
| 2960 | | |
| 2961 | | :- use_module(b_ast_cleanup,[check_used_ids_info/4]). |
| 2962 | | get_exists_used_ids(Parameters,Condition,Infos,Used) :- |
| 2963 | | preference(prob_safe_mode,true),!, |
| 2964 | | (select(used_ids(Used),Infos,Rest) |
| 2965 | | -> check_used_ids_info(Parameters,Condition,Used,exists), %% comment in to check used_ids field |
| 2966 | | (member(used_ids(_),Rest) |
| 2967 | | -> add_internal_error('Multiple used_ids info fields:',Parameters:Infos) ; true) |
| 2968 | | ; |
| 2969 | | add_internal_error( |
| 2970 | | 'Expected information of used identifiers in exists information',Parameters:Infos), |
| 2971 | | bsyntaxtree:find_identifier_uses(Condition, [], Used) |
| 2972 | | ). |
| 2973 | | get_exists_used_ids(Parameters,Condition,Infos,Used) :- |
| 2974 | ? | member(used_ids(Used),Infos) -> true |
| 2975 | | ; |
| 2976 | | add_internal_error( |
| 2977 | | 'Expected information of used identifiers in exists information',Parameters:Infos), |
| 2978 | | bsyntaxtree:find_identifier_uses(Condition, [], Used). |
| 2979 | | % what if we call test_exists for a negated universal quantifier ?? |
| 2980 | | |
| 2981 | | |
| 2982 | | |
| 2983 | | |
| 2984 | | :- use_module(kernel_tools,[ground_state/1]). |
| 2985 | | /* the following causes performance problems with examples/B/Mathematical/GraphIso/CheckGraphIsomorphism |
| 2986 | | and with lausanne.mch in SMT mode */ |
| 2987 | | b_not_test_exists(Parameters,Condition,Infos,LocalState,State,_,WF) :- |
| 2988 | | is_a_conjunct(Condition,LHS,RHS), |
| 2989 | | preferences:preference(use_smt_mode,SMT), |
| 2990 | | (SMT=true -> true |
| 2991 | | ; is_for_all_set_membership_predicate(LHS,Parameters,_,_,_) |
| 2992 | | -> true % optimised processing is available in forall; important for test 1753 |
| 2993 | | ), |
| 2994 | | (\+ ground_state(LocalState) ; \+ ground_state(State)), |
| 2995 | | % TO DO: check if any of the variables in Condition are not ground |
| 2996 | | % TO DO: check if used_ids exist in Infos ? memberchk(used_ids(_),Infos), |
| 2997 | | !, |
| 2998 | | %print(translating_not_exists_into_forall),nl,print(' '),print_bexpr(LHS),nl, print('=> '),print_bexpr(RHS),nl, |
| 2999 | | (SMT=true, |
| 3000 | | is_for_all_set_membership_predicate(RHS,Parameters,_Set,_Pattern,_ParameterValues), |
| 3001 | | \+ is_for_all_set_membership_predicate(LHS,Parameters,_,_,_) |
| 3002 | | -> LLHS=RHS, RRHS=LHS % swap LHS and RHS; TO DO: search for membership predicate inside LHS,RHS !! |
| 3003 | | ; LLHS=LHS, RRHS=RHS |
| 3004 | | ), |
| 3005 | | safe_create_texpr(negation(RRHS),pred,NegRHS), |
| 3006 | | b_for_all(Parameters,Infos,LLHS,NegRHS,LocalState,State,WF). |
| 3007 | | b_not_test_exists(Parameters,Condition,Infos,[],[],no_compile,WF) :- !, |
| 3008 | | % already compiled in is_empty_closure_wf |
| 3009 | | ground_bexpr_check(Condition,Ground), |
| 3010 | | b_not_test_exists_aux(Ground,Parameters,Condition, Infos, WF). |
| 3011 | | b_not_test_exists(Parameters,Condition,Infos,LocalState,State,_,WF) :- |
| 3012 | | % compile to remove dependency on unused parts of State+LocalState; will make it delay less below |
| 3013 | | def_get_texpr_ids(Parameters,ParaIDs), |
| 3014 | | %% print(b_not_test_exists(ParaIDs)),nl, print(state(LocalState,State)),nl, %% |
| 3015 | | b_compiler:b_compile(Condition,ParaIDs,LocalState,State,CompiledCond,WF), |
| 3016 | | (is_truth(CompiledCond) -> fail |
| 3017 | | ; is_falsity(CompiledCond) -> true |
| 3018 | | % TO DO: we could detect equalities that will always succeed (but be careful for WD issues) |
| 3019 | | ; ground_bexpr_check(CompiledCond,Ground), |
| 3020 | | b_not_test_exists_aux(Ground,Parameters,CompiledCond, Infos, WF) |
| 3021 | | ). |
| 3022 | | |
| 3023 | | :- block b_not_test_exists_aux(-,?,?,?,?). |
| 3024 | | b_not_test_exists_aux(_,Parameters,CompiledCond,Infos,WF) :- |
| 3025 | | % getting an idle waitflag considerably slows down test 1739; see also ClearSy/2019_Aug/call_residue/rule_erreur.mch |
| 3026 | | % slow down no longer the case with the new waitflag implementation |
| 3027 | | kernel_waitflags:get_idle_wait_flag(b_not_test_exists_aux(Parameters),WF,LWF), |
| 3028 | | % allow pending co-routines to complete |
| 3029 | | b_not_test_exists_aux2(LWF,Parameters,CompiledCond,Infos,WF). |
| 3030 | | :- block b_not_test_exists_aux2(-,?,?,?,?). |
| 3031 | | b_not_test_exists_aux2(_,Parameters,CompiledCond,Infos,WF) :- |
| 3032 | | % no need to create inner waitflag: we enumerate fully below: create_inner_wait_flags(WF,b_not_test_exists(Parameters),LocalWF), |
| 3033 | | %init_wait_flags(LocalWF,[expansion_context(b_not_not_test_exists,Parameters)]), |
| 3034 | | init_quantifier_wait_flag(WF,not(exists),Parameters,ParaResult,Infos,LocalWF), |
| 3035 | | not_with_enum_warning( |
| 3036 | | b_interpreter:b_not_not_test_exists_aux(Parameters,ParaResult,CompiledCond,LocalWF), |
| 3037 | | LocalWF, CompiledCond, % was not_exists(Parameters), the not_exists quantifier call is on call_stack now |
| 3038 | | CompiledCond), |
| 3039 | | copy_wfe_from_inner_if_necessary(LocalWF,WF). % for the case that the negated call has found abort errors and WFE was not grounded |
| 3040 | | b_not_not_test_exists_aux(Parameters,ParaResult,CompiledCond,LocalWF) :- |
| 3041 | ? | b_interpreter:set_up_typed_localstate(Parameters,ParaResult,TypedVals,[],NewLocalState,negated), |
| 3042 | | % delay generation of WD error messages until outer WFE flag set |
| 3043 | | b_interpreter:b_test_boolean_expression(CompiledCond,NewLocalState,[],LocalWF), % check Condition |
| 3044 | | b_tighter_enumerate_values_in_ctxt(TypedVals,CompiledCond,LocalWF), |
| 3045 | ? | ground_inner_wait_flags(LocalWF). % does not always ground WFE if abort is pending in which case not_with_enum_warning also does not fail and keeps the co-routines |
| 3046 | | % pending co-routines will be discarded by negation ! |
| 3047 | | |
| 3048 | | |
| 3049 | | /* --------------------------------- */ |
| 3050 | | |
| 3051 | | value_type(_X). % only used in post_conditions (assert_post) |
| 3052 | | /* |
| 3053 | | value_type(X) :- nonvar(X), |
| 3054 | | (X = value(_) ; X = global_set(_) ; |
| 3055 | | X = global_constant(_) ; X = term(_)).*/ |
| 3056 | | |
| 3057 | | |
| 3058 | | % look up identifiers in store, if the identifier |
| 3059 | | % is not in the store, assume it's a constant and ignore it |
| 3060 | | % used for determining wait variables for existential quantifier |
| 3061 | | % TO DO: better deal with CSE @identifiers ? |
| 3062 | | lookup_values_if_vars([],_,_,[]). |
| 3063 | | lookup_values_if_vars([Id|IRest],LocalStore,Store,Values) :- |
| 3064 | | ( lookup_value(Id,LocalStore,Store,Val) |
| 3065 | | -> (force_evaluation_of_lazy_value(Id,Val,RealVal) |
| 3066 | | -> Values = [RealVal|VRest] |
| 3067 | | ; Values = [Val|VRest]) |
| 3068 | | ; Values = VRest), |
| 3069 | | lookup_values_if_vars(IRest,LocalStore,Store,VRest). |
| 3070 | | |
| 3071 | | force_evaluation_of_lazy_value(Id,Value,RealVal) :- nonvar(Value), Value=(Trigger,RealVal), |
| 3072 | | var(Trigger), |
| 3073 | | get_preference(use_common_subexpression_elimination,true), |
| 3074 | | is_lazy_let_identifier(Id), |
| 3075 | | !, |
| 3076 | | Trigger=pred_true. % force evaluation, as the exists will otherwise delay/flounder |
| 3077 | | % Warning: this means that outer lazy_let_expressions inside an exists should be well-defined (WD) |
| 3078 | | |
| 3079 | | |
| 3080 | | /* will not generate error messages; used for CSP||B when we don't know if something is a B variable or not */ |
| 3081 | | /* TO DO: add peel_prefix for global_sets and constants ?? */ |
| 3082 | | try_lookup_value_in_store_and_global_sets(Id,State,Val) :- |
| 3083 | | (lookup_value(Id,State,RVal) -> RVal=Val |
| 3084 | | ; b_global_sets:lookup_global_constant(Id,VGC) -> Val = VGC |
| 3085 | | ; b_global_set_or_free_type(Id,_AnyType,Value) -> Val=Value /* global set evaluates to itself */ |
| 3086 | | ). |
| 3087 | | |
| 3088 | | |
| 3089 | | lookup_value_in_store_and_global_sets_wf(Id,Type,LocalState,State,Val,Span,WF) :- |
| 3090 | | (lookup_value_with_span_wf(Id,LocalState,State,RVal,Span,WF) |
| 3091 | | -> RVal=Val |
| 3092 | | ; lookup_value_in_global_sets_wf(Id,Type,Val,LocalState,State,Span,WF) |
| 3093 | | ). |
| 3094 | | |
| 3095 | | lookup_value_in_global_sets_wf(Id,Type,Val,LocalState,State,Span,WF) :- |
| 3096 | | (b_global_sets:lookup_global_constant(Id,VGC) |
| 3097 | | -> Val=VGC |
| 3098 | ? | ; (b_global_set_or_free_type(Id,Type,Value) |
| 3099 | | -> Val = Value /* global set evaluates to itself */ |
| 3100 | | ; ((member(bind(Id,_),LocalState) ; member(bind(Id,_),State)) |
| 3101 | | -> Msg1 = 'Cannot determine value for identifier ' |
| 3102 | | ; Msg1 = 'Cannot find identifier ' |
| 3103 | | ), |
| 3104 | | append(LocalState,State,LSS), |
| 3105 | | translate:translate_bstate_limited(LSS,StateInfo), |
| 3106 | | ajoin([Msg1,Id,':'],Msg), |
| 3107 | | add_error_wf(identifier_not_found,Msg,StateInfo,Span,WF), |
| 3108 | | fail % Val = term(Id) /* Val=fail */ |
| 3109 | | ) |
| 3110 | | ). |
| 3111 | | |
| 3112 | | :- use_module(probsrc(kernel_freetypes),[registered_freetype/2, registered_freetype_case_value/3]). |
| 3113 | ? | b_global_set_or_free_type(Id,_,global_set(Id)) :- b_global_set(Id). |
| 3114 | | b_global_set_or_free_type(Id,_,freetype(Id)) :- registered_freetype(Id,_). |
| 3115 | | b_global_set_or_free_type(Id,Type,Value) :- |
| 3116 | | registered_freetype_case_value(Id,Type,Value). % to instantiate the type parameters we also need the TypeId |
| 3117 | | % TODO: we could also register Event-B destructors and look them up here |
| 3118 | | |
| 3119 | | /* -----------------------------*/ |
| 3120 | | /* b_compute_expressions */ |
| 3121 | | /* -----------------------------*/ |
| 3122 | | |
| 3123 | | b_compute_expressions([], _, _, [],_WF). |
| 3124 | | b_compute_expressions([EXPRsHd|EXPRsTl],LocalState,InState,[ValHd|ValTl],WF) :- |
| 3125 | ? | b_compute_expression(EXPRsHd,LocalState,InState,ValHd,WF), |
| 3126 | ? | b_compute_expressions(EXPRsTl,LocalState,InState,ValTl,WF). |
| 3127 | | |
| 3128 | | % a variation of above that delays computing the expressions until Waitflag 1/2 is set |
| 3129 | | b_compute_assign_expressions_when(EXPRsTl,LocalState,InState,ValTl,WF,OR) :- |
| 3130 | | get_assign_expr_priority_wf(OR,WF,LWF), |
| 3131 | | b_compute_expressions_when2(EXPRsTl,LocalState,InState,ValTl,WF,LWF). |
| 3132 | | b_compute_expressions_when2([], _, _, [],_WF,_). |
| 3133 | | b_compute_expressions_when2([EXPRsHd|EXPRsTl],LocalState,InState,[ValHd|ValTl],WF,LWF) :- |
| 3134 | | b_compute_expression_when2(EXPRsHd,LocalState,InState,ValHd,WF,LWF), |
| 3135 | | b_compute_expressions_when2(EXPRsTl,LocalState,InState,ValTl,WF,LWF). |
| 3136 | | |
| 3137 | | % TO DO: add step number in OR |
| 3138 | | get_assign_expr_priority_wf(output_required,_WF,LWF) :- !, |
| 3139 | | % the Output of the statement is required; do not delay computing it |
| 3140 | | LWF=0. % this would delay computation until WF0 is set: get_wait_flag0(WF,LWF); cf. initialisation for test 2104 |
| 3141 | | get_assign_expr_priority_wf(_,WF,LWF) :- |
| 3142 | | % the Output is not directly required; compute it later (when we are sure that the statement actually succeeds) |
| 3143 | | (preferences:preference(use_smt_mode,true) |
| 3144 | | -> Prio = 2.0 % SMT Mode: we are probably doing CBC checking, we may have set up constraints on ValTl |
| 3145 | | ; Prio=4000), % unless the expression is inside an sequential composition: nothing can depend on ValTl; TO DO: check if we are inside a sequential composition; if not use inf priority; otherwise use 2.0 or 4 |
| 3146 | | get_wait_flag(Prio,get_assign_expr_priority_wf,WF,LWF). |
| 3147 | | |
| 3148 | | :- block b_compute_expression_when2(?,?,?,?,?,-). |
| 3149 | | b_compute_expression_when2(eval_must_not_fail(ErrMsg,EXPRsHd),LocalState,InState,Res,WF,_) :- !, |
| 3150 | | % catch failure; in case the interpreter fails to catch wd-error itself (e.g., try_find_abort is false) |
| 3151 | | if(b_compute_expression(EXPRsHd,LocalState,InState,ValHd,WF),equal_object_wf(ValHd,Res,WF), |
| 3152 | | (translate:translate_bexpression_with_limit(EXPRsHd,TS), |
| 3153 | | add_wd_error_span(ErrMsg,TS,span_predicate(EXPRsHd,LocalState,InState),WF))). |
| 3154 | | b_compute_expression_when2(EXPRsHd,LocalState,InState,ValHd,WF,_) :- |
| 3155 | | copy_wf_start(WF,b_compute_expression_when2,CWF), % not sure this is useful for expressions: |
| 3156 | | b_compute_expression(EXPRsHd,LocalState,InState,ValHd,CWF), |
| 3157 | | copy_wf_finish(WF,CWF). |
| 3158 | | |
| 3159 | | |
| 3160 | | /* -----------------------------*/ |
| 3161 | | /* b_execute_statement */ |
| 3162 | | /* -----------------------------*/ |
| 3163 | | /* b_execute_statement(XMLStatements:input, StateofLocalVariables:input, |
| 3164 | | StateOfGlobalVariables:input, |
| 3165 | | ListOfUpdateBindingsForGlobalVariablesAndResultVariables:output) */ |
| 3166 | | |
| 3167 | | :- assert_pre(b_interpreter:b_execute_statement(Stmt,LS,IS,_OS,_WF,_Path,_OR), |
| 3168 | | (bsyntaxtree:check_if_typed_substitution(Stmt),type_check(LS,store),type_check(IS,store) )). |
| 3169 | | :- assert_post(b_interpreter:b_execute_statement(_Stmt,_LS,_IS,OS,_WF,_Path,_OR), |
| 3170 | | (type_check(OS,store))). |
| 3171 | | |
| 3172 | | b_execute_inner_statement(Body,LocalState,InState,OutState,WF,Path,output_required) :- |
| 3173 | | get_texpr_expr(Body,BE), |
| 3174 | | BE \= while(_COND,_STMT,_INV,_VARIANT), % also other non-det substitutions ?? |
| 3175 | | BE \= select(_), |
| 3176 | | BE \= select(_,_), |
| 3177 | | BE \= if(_), % case(_,_,_) statements are now translated to if-then-else in ast_cleanup |
| 3178 | | !, |
| 3179 | | get_wait_flag0(WF,WF0), |
| 3180 | | block_execute_statement(WF0,Body,LocalState,InState,OutState,WF,Path). |
| 3181 | | b_execute_inner_statement(Body,LocalState,InState,OutState,WF,Path,OR) :- |
| 3182 | | get_wait_flag(2,inner_stmt,WF,LWF), % was 2.0 |
| 3183 | | clone_wait_flags_from1(WF,GroundUponFinish,InnerWF), % copy everything but WF0 |
| 3184 | | b_execute_inner_statement(Body,LocalState,InState,OutState,GroundUponFinish,InnerWF,Path,LWF,OR). |
| 3185 | | |
| 3186 | | :- block block_execute_statement(-, ?,?,?, ?,?,?). |
| 3187 | | block_execute_statement(_,Body,LocalState,InState,OutState,WF,Path) :- |
| 3188 | | b_execute_statement(Body,LocalState,InState,OutState,WF,Path,output_required). |
| 3189 | | |
| 3190 | | :- block b_execute_inner_statement(?,?,?,?,?,?,?,-,?). |
| 3191 | | b_execute_inner_statement(Body,LocalState,InState,OutState,GUF,InnerWF,Path,_,OR) :- |
| 3192 | | b_execute_statement(Body,LocalState,InState,OutState,InnerWF,Path,OR), |
| 3193 | | (GUF=ground_upon_finish -> ground_wait_flags(InnerWF) ; ground_wait_flag0(InnerWF)). |
| 3194 | | |
| 3195 | | :- use_module(source_profiler,[opt_add_source_location_hits/2]). |
| 3196 | | :- use_module(b_operation_guards,[get_substitution_enabling_condition/4]). |
| 3197 | | |
| 3198 | | |
| 3199 | | b_execute_statement_nowf(Subst,LS,S,OutS,Ctxt) :- |
| 3200 | | init_wait_flags_with_call_stack(WF,[prob_command_context(Ctxt,unknown)]), |
| 3201 | | b_interpreter:b_execute_statement(Subst,LS,S,OutS,WF,_Path), |
| 3202 | | ground_wait_flags(WF). |
| 3203 | | |
| 3204 | | b_execute_statement(Stmt, LS, S, OS, WF,Path) :- |
| 3205 | ? | b_execute_statement(Stmt, LS, S, OS, WF,Path,output_not_required). |
| 3206 | | b_execute_statement(b(Stmt,_,Infos), LS, S, OS, WF,Path,OR) :- !, |
| 3207 | ? | b_execute_statement2(Stmt, Infos, LS, S, OS, WF,Path,OR). |
| 3208 | | b_execute_statement(Stmt, LS, S, OS, WF,Path,OR) :- |
| 3209 | | add_internal_error('Statement not properly wrapped: ',b_execute_statement(Stmt, LS, S, OS, WF,Path,OR)), |
| 3210 | | b_execute_statement2(Stmt, [], LS, S, OS, WF,Path,OR). |
| 3211 | | |
| 3212 | | % blocks should be removed by type-checker |
| 3213 | | b_execute_statement2(block(Stmt),_,LS,IS,OS,WF,Path,OR) :- !, |
| 3214 | | b_execute_statement(Stmt,LS,IS,OS,WF,Path,OR). %% NOT COVERED |
| 3215 | | b_execute_statement2(assign(LHS,Exprs),Info,LocalState,InState,OutState,WF,assign(LHS,VALs),OR) :- !, |
| 3216 | | /* Var1, ..., VarN := Expr1, ..., ExprN */ |
| 3217 | | /* also: Var1, fun1(arg), ... := Expr1, Expr2, ... */ |
| 3218 | | opt_add_source_location_hits(Info,1), |
| 3219 | | b_compute_assign_expressions_when(Exprs,LocalState,InState,VALs,WF,OR),% VALs - a list of computed values |
| 3220 | | b_assign_values_or_functions(LHS,VALs,LocalState,InState,OutState,WF,OR). |
| 3221 | | b_execute_statement2(assign_single_id(IDE,Expr),Info,LocalState,InState,OutState,WF,assign_single_id(ID,Value),_OR) :- |
| 3222 | | get_texpr_id(IDE,ID),!, |
| 3223 | | opt_add_source_location_hits(Info,1), |
| 3224 | ? | b_compute_expression(Expr,LocalState,InState,Value,WF), % b_compute_assign_expressions_when |
| 3225 | | %b_compute_assign_expressions_when([Expr],LocalState,InState,[Value],WF), |
| 3226 | | OutState=[bind(ID,Value)]. |
| 3227 | | b_execute_statement2(choice(ChoiceList),_,LocalState,InState,OutState,WF,Path,OR) :- !, |
| 3228 | | /* CHOICE LHS OR RHS OR ... END */ |
| 3229 | | get_wait_flag(2,choice,WF,WFC), % % was 2.0, maybe use length of ChoiceList as Priority ?? |
| 3230 | | b_execute_choice(ChoiceList,LocalState,InState,OutState,WF,Path,WFC,OR). |
| 3231 | | b_execute_statement2(becomes_element_of(LHS,RHS), /* LHS :: RHS */ |
| 3232 | | Info,LocalState,InState,OutState,WF,becomes_element_of(LHS,SingleValue),_OR) :- !, |
| 3233 | | % LHS must be of the form Var1,..,Vark where all variables are disjoint |
| 3234 | | opt_add_source_location_hits(Info,1), |
| 3235 | | b_compute_expression(RHS,LocalState,InState,ValueSet,WF), |
| 3236 | | convert_pairs_into_list(LHS,SingleValue,ValList), |
| 3237 | | check_element_of_wf(SingleValue,ValueSet,WF), |
| 3238 | | ground_det_wait_flag(WF), %% TODO: remove this |
| 3239 | | set_up_typed_localstate(LHS,ValList,TypedVals,[],OutState,positive), |
| 3240 | | b_tighter_enumerate_all_values(TypedVals,WF). |
| 3241 | | b_execute_statement2(becomes_such(Vars,Condition),Info,LocalState,InState,OutState,WF,Path,OR) :- !, |
| 3242 | | /* Vars : (Condition) */ |
| 3243 | | opt_add_source_location_hits(Info,1), |
| 3244 | | (OR=output_required, get_preference(expand_becomes_such_into_set,true) % see test 1770 |
| 3245 | | -> % compute all solutions of the Condition predicate and then choose amongst them; |
| 3246 | | % possibly good for complex preds, ensures we solve local constraints in isolation in context of sequence(.) |
| 3247 | | % runs into time_out for test 2093; TODO: detect special patterns like partial_function, ... |
| 3248 | | insert_before_substitution_variables(Vars,LocalState,InState,LocalState,TempLocalState), |
| 3249 | | add_debug_message(b_interpreter,'Expanding become_such predicate into set: ',Condition,Condition), |
| 3250 | | b_compute_comprehension_set(Vars,Condition,Info,TempLocalState,InState,CResult,WF), |
| 3251 | | RHS = b(value(CResult),any,[was(becomes_such)]), |
| 3252 | | b_execute_statement2(becomes_element_of(Vars,RHS), Info,LocalState,InState,OutState,WF,Path,OR) |
| 3253 | | ; % create Vars in State and assert that Condition is true; probably good for cbc checking |
| 3254 | | b_execute_becomes_such(Vars,Condition,Info,LocalState,InState,OutState,_,Path,WF) |
| 3255 | | ). |
| 3256 | | b_execute_statement2(sequence(Seq), /* LHS ; RHS */ |
| 3257 | | _,LocalState,InState,OutUpdates,WF,Path,OR) :- !, %pe_print(sequence(Seq,InState)), |
| 3258 | | b_execute_sequence(Seq,LocalState,InState,OutUpdates,WF,Path,OR). |
| 3259 | | b_execute_statement2(precondition(PreCond,Body),_,LocalState,InState,OutState,WF,Path,OR) :- !, |
| 3260 | | %print('*** Non-outermost PRE construct'),nl, |
| 3261 | | Path=pre(PreRes,IPath), |
| 3262 | | b_try_check_boolean_expression(PreCond,LocalState,InState,WF,PreRes), |
| 3263 | | %used to be: ground_det_wait_flag(WF), |
| 3264 | | check_precondition_result(PreRes,PreCond,LocalState,InState,WF), |
| 3265 | | (PreRes == pred_true -> b_execute_statement(Body,LocalState,InState,OutState,WF,IPath,OR) |
| 3266 | | ; PreRes == pred_false -> IPath = not_executed |
| 3267 | | ; b_execute_inner_statement(Body,LocalState,InState,OutState,WF,IPath,OR) % with extra waitflag |
| 3268 | | ). |
| 3269 | | b_execute_statement2(assertion(PreCond,Body),_,LocalState,InState,OutState,WF,Path,OR) :- !, |
| 3270 | | b_execute_assertion(PreCond,Body,LocalState,InState,OutState,WF,Path,OR). |
| 3271 | | b_execute_statement2(witness_then(PreCond,Body),_,LocalState,InState,OutState,WF,witness(Path),OR) :- !, |
| 3272 | | b_test_boolean_expression(PreCond,LocalState,InState,WF), |
| 3273 | | b_execute_inner_statement(Body,LocalState,InState,OutState,WF,Path,OR). |
| 3274 | | b_execute_statement2(select(Whens),_, LocalState, InState, OutState,WF,select(Nr,Path),OR) :- !, |
| 3275 | | get_texpr_expr(TExpr,select_when(PreCond, Body)), % info field not used for select_when |
| 3276 | ? | optimized_nth1(Nr, Whens, TExpr), % THIS IS A CHOICE POINT if length(Whens)>1 ! <-------- TO DO guard |
| 3277 | | b_test_boolean_expression(PreCond,LocalState,InState,WF), |
| 3278 | | %used to call: ground_det_wait_flag(WF), now execute inner statement delays |
| 3279 | | b_execute_inner_statement(Body,LocalState,InState,OutState,WF,Path,OR). |
| 3280 | | b_execute_statement2(select(Whens,Else), Infos, LocalState, InState, OutState,WF,Path,OR) :- !, |
| 3281 | | ( |
| 3282 | ? | b_execute_statement2(select(Whens), Infos, LocalState, InState, OutState,WF,Path,OR) |
| 3283 | | ; % THIS IS A CHOICE POINT ! <-------- TO DO guard |
| 3284 | | findall(PC, |
| 3285 | | (member(SW,Whens),get_texpr_expr(SW,select_when(PC,_))), AllPreconds), |
| 3286 | | b_not_test_list_of_boolean_expression(AllPreconds,LocalState,InState,WF), |
| 3287 | | Path = select(else,IPath), |
| 3288 | | b_execute_inner_statement(Else,LocalState,InState,OutState,WF,IPath,OR) |
| 3289 | | ). |
| 3290 | | b_execute_statement2(var(Parameters,Body), /* VAR _ IN _ substitution */ |
| 3291 | | _,LocalState,InState,NewOutState,WF,var(Names,Path),OR) :- !, |
| 3292 | | /* introduces a new variable that can be assigned to */ |
| 3293 | | split_names_and_types(Parameters,Names,_), |
| 3294 | | delete_variables_from_state(Names,InState,InState1), % in case any local parameter name is also a constant/variable: remove it % but if we do an operation_call: we leave context and it becomes visible again !! |
| 3295 | | % otherwise we may think there are two updates, e.g., in WHILE loop; see Rule_DB_SIGAREA_0030 from test 1303 |
| 3296 | | set_up_undefined_localstate(Parameters,InState1,NewInState), |
| 3297 | | delete_variables_from_state(Names,LocalState,NewLocalState), |
| 3298 | | %check_valid_store(InState,var_in_state), check_valid_store(LocalState,var_local_state), |
| 3299 | | b_execute_statement(Body,NewLocalState,NewInState,OutState,WF,Path,OR), |
| 3300 | | % check_valid_store(OutState,var_out_state), |
| 3301 | | filter_results(Parameters,OutState,NewOutState,WF) |
| 3302 | | . |
| 3303 | | /* it can be ok not to assign to a local variable */ |
| 3304 | | |
| 3305 | | %TODO(DP, 5.8.2008): |
| 3306 | | b_execute_statement2(let(Parameters,Defs,Body), /* LET _ BE _ IN _ */ |
| 3307 | | _,LocalState,InState,OutState,WF,let(Path),OR) :- !, |
| 3308 | | set_up_typed_localstate(Parameters,LocalState,NewLocalState), |
| 3309 | | b_execute_let_definitions(Defs,NewLocalState,InState,WF), |
| 3310 | | b_execute_statement(Body,NewLocalState,InState,OutState,WF,Path,OR). |
| 3311 | | b_execute_statement2(lazy_let_subst(Id,IdExpr,Body),_,LocalState,InState,OutState,WF,lazy_let(Path),OR) :- |
| 3312 | | !, |
| 3313 | | add_lazy_let_id_and_expression(Id,IdExpr,LocalState,InState,NewLocalState,WF), |
| 3314 | | b_execute_statement(Body,NewLocalState,InState,OutState,WF,Path,OR). |
| 3315 | | b_execute_statement2(any(Parameters,PreCond,Body), /* ANY _ WHERE */ |
| 3316 | | Info,LocalState,InState,OutState,WF0,any(Bindings,Path),OR) :- !, %statistics(runtime,_), |
| 3317 | | %print('ANY: '), print_bexpr(PreCond),nl, %% |
| 3318 | | set_up_typed_localstate(Parameters,FreshVars,TypedVals,LocalState,NewLocalState,positive), |
| 3319 | | create_any_bindings(Parameters,FreshVars,Bindings), |
| 3320 | | push_wait_flag_call_stack_info(WF0,quantifier_call('ANY',Parameters,FreshVars,Info),WF), |
| 3321 | | b_test_inner_boolean_expression(PreCond,NewLocalState,InState,WF), /* check WHERE */ |
| 3322 | | %%observe_state(TypedVals,WF,0), b_tracetest_boolean_expression(PreCond,NewLocalState,InState,WF,one), %% comment in for debugging |
| 3323 | | b_tighter_enumerate_all_values(TypedVals,WF), % moved enumeration after test; in case WF already instantiated |
| 3324 | | %TODO: check if we can use b_tighter_enumerate_values_in_ctxt |
| 3325 | | b_execute_statement(Body,NewLocalState,InState,OutState,WF,Path,OR). /* should we use b_execute_inner_statement/LWF here ?? */ |
| 3326 | | b_execute_statement2(skip, /* SKIP */ |
| 3327 | | Info,_LocalState,_InState,OutState,_WF,skip,_OR) :- !, |
| 3328 | | opt_add_source_location_hits(Info,1), |
| 3329 | | OutState = []. |
| 3330 | | b_execute_statement2(parallel(Statements), /* LHS || RHS */ %@@@ |
| 3331 | | _,LocalState,InState,OutState,WF,parallel(Paths),OR) :- !, |
| 3332 | | b_execute_statements_in_parallel(Statements,LocalState,InState,OutState,WF,Paths,OR). |
| 3333 | | b_execute_statement2(init_statement(Stmt),_,LocalState,InState,OutState,WF,Path,OR) :- !, |
| 3334 | | /* Like block: but |
| 3335 | | writes error messages if statement fails */ %@@@ |
| 3336 | | (preferences:get_preference(provide_trace_information,true) |
| 3337 | | -> nl,print(' =INIT=> '),translate:print_subst(Stmt),nl, flush_output(user_output), |
| 3338 | | statistics(walltime,[Tot1,_]) |
| 3339 | | ; true), |
| 3340 | ? | if(b_execute_initialisation_statement(Stmt,LocalState,InState,OutState,WF,Path,OR), |
| 3341 | | ( |
| 3342 | | (preferences:get_preference(provide_trace_information,true) |
| 3343 | | -> statistics(walltime,[Tot2,_]), Tot is Tot2-Tot1, |
| 3344 | | format(' [=OK= ~w ms]~n',[Tot]), flush_output(user_output) |
| 3345 | | ; true) |
| 3346 | | ), |
| 3347 | | ( translate:translate_substitution(Stmt,TStmtStr), |
| 3348 | | (bmachine:b_machine_temp_predicate(_TempPred) |
| 3349 | | -> Msg = 'INITIALISATION statement cannot be executed with extra predicate: ', |
| 3350 | | add_error_wf(initialisation_fails,Msg,TStmtStr,Stmt,WF) |
| 3351 | | % do not add as state error; user requested transition |
| 3352 | | ; Msg = 'INITIALISATION statement fails: ', |
| 3353 | | (get_substitution_enabling_condition(Stmt,SpanPred,_BSVars,_IsPreciseV) |
| 3354 | | -> %insert_custom_predicate_into_guard(EnablingCondition,TempPred,SpanPred), |
| 3355 | | ErrPos = span_predicate(SpanPred,LocalState,InState) % for visualising by user |
| 3356 | | % Note: user can also visualise by chooise execute by predicate (in TK) |
| 3357 | | ; ErrPos = Stmt |
| 3358 | | ), |
| 3359 | | % add as state error so that we can visualise span predicate |
| 3360 | | add_state_error_wf(initialisation_fails,Msg,TStmtStr,ErrPos,WF) % cf test 1736 and 278 |
| 3361 | | %add_error_wf(initialisation_fails,Msg,TStmtStr,ErrPos,WF) |
| 3362 | | ), |
| 3363 | | fail |
| 3364 | | ) |
| 3365 | | ). |
| 3366 | | b_execute_statement2(if(IfList),_,LocalState,InState,OutState,WF,Path,OR) :- !, |
| 3367 | | LWF='$GENERATE_ON_DEMAND', |
| 3368 | | b_execute_else_list(IfList,LocalState,InState,OutState,WF,1,Path,LWF,OR). |
| 3369 | | b_execute_statement2(operation_call(Operation,Results,Parameters),Info,LocalState,InState,OutState,WF, |
| 3370 | | operation_call(OperationName,Results,ParamValues,ResultValues,InnerPath),OR) :- !, |
| 3371 | | def_get_texpr_id(Operation,op(OperationName)), |
| 3372 | | b_execute_operation_with_parameters(OperationName,LocalState,InState,Results, |
| 3373 | | Parameters,OpOutState,ParamValues,ResultValues,InnerPath,Info,WF,OR), |
| 3374 | | opt_add_source_location_hits(Info,1), |
| 3375 | | split_names_and_types(Results,ResultingIDs,_), |
| 3376 | | store_values(ResultingIDs, ResultValues, ResultsOutState), |
| 3377 | | combine_updates(ResultsOutState,OpOutState,OutState). |
| 3378 | | |
| 3379 | | b_execute_statement2(while(COND,STMT,INV,VARIANT), |
| 3380 | | Info,LocalState,InState,OutState,WF0,while(VarVal,BodyPath),_OR) :- !, |
| 3381 | | %hit_profiler:add_profile_hit(while(COND,WF),2), |
| 3382 | | % we may go around the loop many times: avoid carrying around unused variables |
| 3383 | | get_while_reads_write_info_and_filter_state(Info,LocalState,InState,LS,IS,ModVars), |
| 3384 | | push_wait_flag_call_stack_info(WF0,b_operator_call('WHILE',[VarVal],Info),WF), % TODO: provide subst. call stack entry |
| 3385 | | %print_message(execute_while(LocalState,InState)), |
| 3386 | | b_compiler:b_optimize(INV,ModVars,LS,IS,CINV,WF), |
| 3387 | | b_compiler:b_optimize(COND,ModVars,LS,IS,CCOND,WF), |
| 3388 | | b_compiler:b_optimize(VARIANT,ModVars,LS,IS,CVARIANT,WF), |
| 3389 | | %,b_compiler:b_compile(STMT,ModVars,LS,IS,CSTMT,WF) |
| 3390 | | % TO DO: maybe exclude in one go; use ordered approach ? |
| 3391 | | %exclude(unused_variable(ModVars),LS,LS2), % only keep modified vars; all other compiled |
| 3392 | | %exclude(unused_variable(ModVars),IS,IS2), % only keep modified vars; all other compiled |
| 3393 | | b_compute_expression(CVARIANT,LS,IS,VarVal,WF), |
| 3394 | | b_execute_while(CCOND,STMT,CINV,CVARIANT,ModVars,VarVal,LS,IS,OutState,WF,BodyPath). |
| 3395 | | b_execute_statement2(external_subst_call(FunName,Args),Info,LocalState,State,OutState,WF,external_subst(FunName),_OR) :- |
| 3396 | | !, b_compute_expressions(Args, LocalState,State, EvaluatedArgs, WF), |
| 3397 | | push_wait_flag_call_stack_info(WF,external_call(FunName,EvaluatedArgs,Info),WF2), |
| 3398 | | call_external_substitution(FunName,EvaluatedArgs,State,OutState,Info,WF2). |
| 3399 | | b_execute_statement2(E,I,LS,S,O,WF,Path,OR) :- |
| 3400 | | add_internal_error('Uncovered statement: ',b_execute_statement2(E,I,LS,S,O,WF,Path,OR)), |
| 3401 | | print_functor(E),nl,fail. |
| 3402 | | |
| 3403 | | % ------------------- |
| 3404 | | |
| 3405 | | b_execute_becomes_such(Vars,Condition,Info,LocalState,InState,OutState,Values,becomes_such(Ids,Values),WF) :- !, |
| 3406 | | % create the LHS variables in the local state |
| 3407 | | set_up_typed_localstate(Vars,Values,TypedVals,LocalState,TempLocalState1,positive), |
| 3408 | | get_texpr_ids(Vars,Ids), |
| 3409 | | % store those values also into the outgoing state as bind/2 pairs |
| 3410 | | store_values(Ids,Values,OutState), |
| 3411 | | % there might be some variables of the form x$0 to access the |
| 3412 | | % values before the substitution. We put them into the local state |
| 3413 | | insert_before_substitution_variables(Vars,LocalState,InState,TempLocalState1,TempLocalState), |
| 3414 | | push_wait_flag_call_stack_info(WF,quantifier_call(':(_)',Vars,Values,Info),WF1), |
| 3415 | | b_test_inner_boolean_expression(Condition,TempLocalState,InState,WF1), |
| 3416 | | b_tighter_enumerate_values_in_ctxt(TypedVals,Condition,WF1). |
| 3417 | | % moved enumeration after test, to avoid overhead in case Condition is deterministic |
| 3418 | | % (see e.g. WhileAssignTupleChoose_v2) |
| 3419 | | |
| 3420 | | |
| 3421 | | b_execute_sequence([],_LocalState,_InState,[],_WF,[],_OR). |
| 3422 | | b_execute_sequence([First|Rest],LocalState,InState,OutUpdates,WF,Path,OR) :- |
| 3423 | | (Rest == [] |
| 3424 | | -> b_execute_statement(First,LocalState,InState,OutUpdates,WF,Path,OR) |
| 3425 | | ; Path = sequence(Path1,RestPath), |
| 3426 | | b_execute_statement(First,LocalState,InState,LHSUpdates,WF,Path1,output_required), |
| 3427 | | ground_det_wait_flag(WF), % maybe better to delay next execute_statement ? TODO |
| 3428 | | store_intermediate_updates_wf(InState,LHSUpdates,IntermediateState,First,WF), |
| 3429 | | % TO DO: statically determine what the Rest can modify and copy over LHSUpdates not modified by Rest into OutUpdates |
| 3430 | | b_execute_statement_list_when_state_set(Rest,LocalState,IntermediateState, |
| 3431 | | LHSUpdates,OutUpdates,WF,RestPath,OR) |
| 3432 | | ). |
| 3433 | | |
| 3434 | | |
| 3435 | | :- block check_precondition_result(-,?,?,?,?). |
| 3436 | | check_precondition_result(pred_true,_,_,_,_). |
| 3437 | | check_precondition_result(pred_false,PreCond,LS,S,WF) :- |
| 3438 | | translate_bexpression(PreCond,PreString), |
| 3439 | | add_abort_error_span(precondition_error,'Precondition violated: ',PreString,span_predicate(PreCond,LS,S),WF). |
| 3440 | | |
| 3441 | | create_any_bindings([],[],[]). |
| 3442 | | create_any_bindings([Param|Prest],[Value|Vrest],[bind(Id,Value)|Brest]) :- |
| 3443 | | get_texpr_id(Param,Id), |
| 3444 | | create_any_bindings(Prest,Vrest,Brest). |
| 3445 | | |
| 3446 | | % insert the values of variables before the substitution under an |
| 3447 | | % alternative name. The alternative name is given as an optional extra |
| 3448 | | % information in the variable (see type-checking of becomes_such for |
| 3449 | | % details). Usually (i.e. always) the alternative name is x$0 for the variable x. |
| 3450 | | insert_before_substitution_variables([],_LocalState,_InState,State,State). |
| 3451 | | insert_before_substitution_variables([Var|VRest],LocalState,InState,StateA,ResultStateB) :- |
| 3452 | | get_texpr_info(Var,Infos), |
| 3453 | ? | ( member(before_substitution(_,Alternative),Infos) -> |
| 3454 | | def_get_texpr_id(Var,Id), |
| 3455 | | lookup_value_for_existing_id(Id,LocalState,InState,Value), |
| 3456 | | store_value(Alternative,Value,StateA,StateC) |
| 3457 | | ; |
| 3458 | | StateA = StateC), |
| 3459 | | insert_before_substitution_variables(VRest,LocalState,InState,StateC,ResultStateB). |
| 3460 | | |
| 3461 | | |
| 3462 | | |
| 3463 | | % treat the bool(.) operator |
| 3464 | | b_convert_bool(Pred,LocalState,State,WF,PredRes) :- |
| 3465 | | get_wait_flag0(WF,WF0), % first give a chance for PredRes to be instantiated |
| 3466 | | b_convert_bool_wf0(Pred,LocalState,State,WF,PredRes,WF0). |
| 3467 | | |
| 3468 | | :- block b_convert_bool_wf0(?,?,?,?,-,-). |
| 3469 | | b_convert_bool_wf0(Pred,LocalState,State,WF,PredRes,_) :- PredRes==pred_true,!, |
| 3470 | | %% print('bool(.)=TRUE '), translate:print_bexpr(Pred),nl, %% |
| 3471 | | b_test_inner_boolean_expression(Pred,LocalState,State,WF). |
| 3472 | | b_convert_bool_wf0(Pred,LocalState,State,WF,PredRes,_) :- PredRes==pred_false,!, |
| 3473 | | %% print('bool(.)=FALSE '), translate:print_bexpr(Pred),nl, %% |
| 3474 | | b_not_test_inner_boolean_expression(Pred,LocalState,State,WF). |
| 3475 | | b_convert_bool_wf0(Pred,LocalState,State,WF,PredRes,_) :- |
| 3476 | | %% print('bool(.) check : '), translate:print_bexpr(Pred),nl, % obsv(LocalState), |
| 3477 | | b_try_check_boolean_expression_wf(Pred,LocalState,State,WF,PredRes ). |
| 3478 | | %% print('finished bool(.) check : '), translate:print_bexpr(Pred),nl,translate:print_bstate(LocalState),nl. |
| 3479 | | |
| 3480 | | % Additional time limit +TO in ms, e.g., used for CDCL(T). +TORes is time_out if a timeout occurred. |
| 3481 | | b_convert_bool_timeout(Pred,LocalState,State,WF,PredRes,TO,TORes) :- |
| 3482 | | get_wait_flag0(WF,WF0), |
| 3483 | | b_convert_bool_wf0_timeout(Pred,LocalState,State,WF,PredRes,WF0,TO,TORes). |
| 3484 | | |
| 3485 | | :- block b_convert_bool_wf0_timeout(?,?,?,?,-,-,?,?). |
| 3486 | | b_convert_bool_wf0_timeout(Pred,LocalState,State,WF,PredRes,_,TO,TORes) :- |
| 3487 | | PredRes == pred_true, |
| 3488 | | !, |
| 3489 | | safe_time_out(b_test_inner_boolean_expression(Pred,LocalState,State,WF), |
| 3490 | | TO, |
| 3491 | | TORes). |
| 3492 | | b_convert_bool_wf0_timeout(Pred,LocalState,State,WF,PredRes,_,TO,TORes) :- |
| 3493 | | PredRes == pred_false, |
| 3494 | | !, |
| 3495 | | safe_time_out(b_not_test_inner_boolean_expression(Pred,LocalState,State,WF), |
| 3496 | | TO, |
| 3497 | | TORes). |
| 3498 | | b_convert_bool_wf0_timeout(Pred,LocalState,State,WF,PredRes,_,TO,TORes) :- |
| 3499 | | safe_time_out(b_try_check_boolean_expression_wf(Pred,LocalState,State,WF,PredRes ), |
| 3500 | | TO, |
| 3501 | | TORes). |
| 3502 | | |
| 3503 | | %%obsv([]). |
| 3504 | | %%obsv([bind(Var,Val) | T] ) :- when(ground(Val), format("~w = ~w~n",[Var,Val])), obsv(T). |
| 3505 | | |
| 3506 | | % check boolean expression without enumeration predicate result (unless WFE is set) |
| 3507 | | b_try_check_boolean_expression_no_enum_wf(Pred,LocalState,State,WF,PredRes) :- |
| 3508 | | b_try_check_boolean_expression_lwf(Pred,LocalState,State,WF,'$NO_ENUMERATION',PredRes,_). |
| 3509 | | |
| 3510 | | b_try_check_boolean_expression_wf(Pred,LocalState,State,WF,PredRes) :- |
| 3511 | | b_try_check_boolean_expression_lwf(Pred,LocalState,State,WF,'$ENUMERATION',PredRes,'$GENERATE_ON_DEMAND' ). |
| 3512 | | % try to do check_boolean expression; if not possible create choice point if LWF is grounded |
| 3513 | | b_try_check_boolean_expression_lwf(Pred,LocalState,InState,WF,DoEnumeration,PredRes,LWF) :- |
| 3514 | | b_compiler:b_optimize(Pred,[],LocalState,InState,CPRED,WF), % this increases chance that check can be applied+avoids multiple computations in two branches |
| 3515 | | % still: some computations may not be pre-computed, e.g., union of closures, ..., see Rule_DB_PSR_0003_C |
| 3516 | | b_try_check_boolean_expression_lwf_c(CPRED,LocalState,InState,WF,DoEnumeration,PredRes,LWF). |
| 3517 | | |
| 3518 | | b_try_check_boolean_expression_lwf_c(b(truth,_,_),_LocalState,_InState,_WF,_,PredRes,_LWF) :- !, |
| 3519 | | % typical case in e.g. IF-THEN-ELSE |
| 3520 | | PredRes= pred_true. |
| 3521 | | b_try_check_boolean_expression_lwf_c(Pred,LocalState,InState,WF,DoEnumeration,PredRes,LWF) :- |
| 3522 | | copy_wf_start(WF,try_check,CWF), |
| 3523 | | if(b_check_boolean_expression_with_enum(Pred,LocalState,InState,CWF,DoEnumeration,Res), |
| 3524 | | % Note: we do not use a cut here ! usually b_check will not enumerate; but on rare occasions it can (apply function in inverse,...) |
| 3525 | | (PredRes=Res, |
| 3526 | | copy_wf_finish(WF,CWF) |
| 3527 | | ), |
| 3528 | | b_try_check_failed(Pred,LocalState,InState,WF,DoEnumeration,PredRes,LWF) |
| 3529 | | ). |
| 3530 | | b_try_check_failed(Pred,LocalState,InState,WF,DoEnumeration,PredRes,LWF) :- |
| 3531 | | % print(choice_point(LWF)),nl, translate:print_bexpr(Pred),nl, |
| 3532 | | perfmessagecall(choice_point,reification_of_check_predicate_failed,translate:print_bexpr(Pred),Pred), |
| 3533 | | (LWF=='$GENERATE_ON_DEMAND' -> get_binary_choice_wait_flag('$GENERATE_ON_DEMAND',WF,WF1) |
| 3534 | | ; var(LWF),DoEnumeration=='$NO_ENUMERATION' -> get_enumeration_finished_wait_flag(WF,WF1) % nothing will enum LWF |
| 3535 | | ; WF1=LWF), |
| 3536 | | b_try_check_boolean_expression_lwf_block(Pred,LocalState,InState,WF,PredRes,WF1). |
| 3537 | | |
| 3538 | | % a version of b_check_boolean_expression which will enumerate its result as last resort |
| 3539 | | % should we only do this in SMT mode : preferences:preference(use_smt_mode,true) ? |
| 3540 | | b_check_boolean_expression_with_enum(Pred,LocalState,State,WF,DoEnumeration,Res) :- |
| 3541 | | b_check_boolean_expression(Pred,LocalState,State,WF,Res), |
| 3542 | | (nonvar(Res) -> true |
| 3543 | | ; DoEnumeration=='$NO_ENUMERATION' -> get_enumeration_finished_wait_flag(WF,EWF), enum_pred_result(Res,EWF,WF) |
| 3544 | | ; get_wait_flag0(WF,LWF0), |
| 3545 | | enum_pred_result0(Res,LWF0,WF)). |
| 3546 | | |
| 3547 | | % first set up with wait_flag0: easy to get, avoid putting unnecessary waitflags into store |
| 3548 | | % relevant for DataValidationTest.mch with n>20000 |
| 3549 | | :- block enum_pred_result0(-,-,?). |
| 3550 | | enum_pred_result0(Res,_,WF) :- var(Res),!, |
| 3551 | | get_last_wait_flag(b_check_boolean_expression_with_enum,WF,LWF), |
| 3552 | | enum_pred_result(Res,LWF,WF). % before doing infinite enumerations: better do case-distinction here |
| 3553 | | enum_pred_result0(_,_,_). |
| 3554 | | |
| 3555 | | :- block enum_pred_result(-,-,?). |
| 3556 | | enum_pred_result(pred_true,_,_). |
| 3557 | | enum_pred_result(pred_false,_,_). |
| 3558 | | |
| 3559 | | :- block b_try_check_boolean_expression_lwf_block(?,?,?, ?,-,-). |
| 3560 | | b_try_check_boolean_expression_lwf_block(Pred,LocalState,InState,WF,PREDRES,_) :- |
| 3561 | | PREDRES \== pred_false, |
| 3562 | | % print(' TRUE +++++> '), translate:print_bexpr(Pred),nl, |
| 3563 | ? | b_test_inner_boolean_expression(Pred,LocalState,InState,WF), |
| 3564 | | PREDRES = pred_true. % set it only after we have actually checked the predicate; e.g., for IF we may execute rest of while loop etc... before actually having set up the constraint Pred; See Hansen23_WhilePerformance |
| 3565 | | b_try_check_boolean_expression_lwf_block(Pred,LocalState,InState,WF,PREDRES,_) :- |
| 3566 | | PREDRES \== pred_true, |
| 3567 | | % print(' FALSE +++++> '), translate:print_bexpr(Pred),nl, |
| 3568 | | b_not_test_inner_boolean_expression(Pred,LocalState,InState,WF), |
| 3569 | | PREDRES = pred_false. |
| 3570 | | |
| 3571 | | % try to do check_boolean expression; if not possible create choice point *directly* |
| 3572 | | b_try_check_boolean_expression(Pred,LocalState,InState,WF,PredRes) :- |
| 3573 | | b_compiler:b_optimize(Pred,[],LocalState,InState,CPRED,WF), % this increases chance that check can be applied+avoids multiple computations in two branches |
| 3574 | | b_try_check_boolean_expression_c(CPRED,LocalState,InState,WF,PredRes). |
| 3575 | | b_try_check_boolean_expression_c(Pred,LocalState,InState,WF,PredRes) :- |
| 3576 | | copy_wf_start(WF,try_check_c,CWF), |
| 3577 | | if(b_check_boolean_expression(Pred,LocalState,InState,CWF,Res), |
| 3578 | | (PredRes=Res,copy_wf_finish(WF,CWF)), |
| 3579 | | b_try_check_boolean_expression_c2(Pred,LocalState,InState,WF,PredRes) |
| 3580 | | ). |
| 3581 | | b_try_check_boolean_expression_c2(Pred,LocalState,InState,WF,pred_true) :- |
| 3582 | | %hit_profiler:add_profile_hit(b_try_check_failed(Pred),3), |
| 3583 | | b_test_inner_boolean_expression(Pred,LocalState,InState,WF). |
| 3584 | | b_try_check_boolean_expression_c2(Pred,LocalState,InState,WF,pred_false) :- |
| 3585 | | b_not_test_inner_boolean_expression(Pred,LocalState,InState,WF). |
| 3586 | | |
| 3587 | | |
| 3588 | | % WHILE SUBSTITUTION TREATMENT: |
| 3589 | | |
| 3590 | | :- if(environ(prob_noopt_mode,true)). |
| 3591 | | get_while_reads_write_info_and_filter_state(Info,LocalState,InState,LS,IS,ModVars) :- |
| 3592 | | member(modifies(ModVars),Info),!, |
| 3593 | | LS=LocalState, IS=InState. % do not filter state |
| 3594 | | :- endif. |
| 3595 | | get_while_reads_write_info_and_filter_state(Info,LocalState,InState,LS,IS,ModVars) :- |
| 3596 | | member(reads(ReadVars),Info), |
| 3597 | ? | member(modifies(ModVars),Info),!, |
| 3598 | | % we may go around the loop many times: avoid carrying around unused variables |
| 3599 | | ord_union(ReadVars,ModVars,ReadOrModVars), |
| 3600 | | filter_states(ReadOrModVars,LocalState,InState,LS,IS). |
| 3601 | | get_while_reads_write_info_and_filter_state(Info,LocalState,InState,LS,IS,ModVars) :- |
| 3602 | | add_internal_error('No reads & modifies info for while loop: ',get_while_reads_write_info_and_filter_state(Info,LocalState,InState,LS,IS,ModVars)), |
| 3603 | | ModVars=_, % all variables assumed to be modified |
| 3604 | | LS=LocalState, IS=InState. |
| 3605 | | |
| 3606 | | unused_variable(List,bind(Var,_)) :- |
| 3607 | | % b_is_variable(Var), % TO DO: also include constants in reads Info ? |
| 3608 | | ord_nonmember(Var,List). |
| 3609 | | unused_localvariable(List,bind(Var,Val)) :- ord_nonmember(Var,List), |
| 3610 | | (nonvar(Val),Val=(_,_) -> atom_codes(Var,Codes), Codes \= [64|_] % not a lazy_let ID starting with @ |
| 3611 | | % TO DO: either ensure lazy-let ids are also included in reads/writes ! or mark Val not as pair but using a special functor in add_lazy_let_id_to_local_state ? |
| 3612 | | ; true). |
| 3613 | | |
| 3614 | | filter_states(ReadOrModVars,LocalState,InState,LS,IS) :- |
| 3615 | | exclude(unused_localvariable(ReadOrModVars),LocalState,LS), |
| 3616 | | exclude(unused_variable(ReadOrModVars),InState,IS). |
| 3617 | | % TO DO: investigate whether we should use ord_intersect; ReadVars is already sorted |
| 3618 | | |
| 3619 | | :- block lazy_exclude(-,?,?). |
| 3620 | | lazy_exclude([],_,[]). |
| 3621 | | lazy_exclude([bind(Var,Val)|T],ModVars,Res) :- lazy_exclude_aux(Var,Val,T,ModVars,Res). |
| 3622 | | :- block lazy_exclude_aux(-,?,?,?,?). |
| 3623 | | lazy_exclude_aux(Var,Val,T,ModVars,Res) :- |
| 3624 | | (member(Var,ModVars) -> Res = [bind(Var,Val)|RT] |
| 3625 | | ; Res = RT), |
| 3626 | | lazy_exclude(T,ModVars,RT). |
| 3627 | | |
| 3628 | | |
| 3629 | | :- use_module(kernel_tools,[ground_state_check/2]). |
| 3630 | | % outermost while code:; at next iteration b_execute_while1 will be called. |
| 3631 | | :- block b_execute_while(?,?,?,?,?,-, ?,?,?, ?,?). % we block on Variant Value int(VarVal) |
| 3632 | | b_execute_while(COND,STMT,INV,VARIANT,ModVars,int(VarVal),LocalState,InState,OutState,WF,BodyPath) :- |
| 3633 | | %kernel_waitflags:get_idle_wait_flag(b_execute_while,WF,LWF), |
| 3634 | | (number(VarVal),VarVal =< 50 |
| 3635 | | -> LWF=VarVal % we will wait anyway on GrInState below; no danger for WD issues in in b_optimize |
| 3636 | | ; no_pending_waitflags(WF) -> get_wait_flag0(WF,LWF) %important for e.g. PerformanceTests/While/LiftExec_LIM.mch |
| 3637 | | ; kernel_waitflags:get_last_wait_flag(b_execute_while1,WF,LWF) % to do: maybe get largest waitflag to avoid as much as possible that new choice points get enumerated before while triggers? |
| 3638 | | % see also tests 979, 981 |
| 3639 | | ), |
| 3640 | | % before entering while loop, we wait for co-routines to complete that may have activated b_execute_while |
| 3641 | | b_execute_while_idle(COND,STMT,INV,VARIANT,ModVars,int(VarVal),LocalState,InState,OutState,WF,LWF,BodyPath). |
| 3642 | | |
| 3643 | | :- block b_execute_while_idle(?,?,?,?,?,?, ?,?,?, ?,-,?). % we block on enumeration starting wait_flag |
| 3644 | | b_execute_while_idle(COND,STMT,INV,VARIANT,ModVars,int(VarVal),LocalState,InState,OutState,WF,_,BodyPath) :- |
| 3645 | | %ground_constraintprop_wait_flags(WF), |
| 3646 | | (number(VarVal), VarVal>50, preference(compile_while_body,true) |
| 3647 | | % if VarVal > 50 -> worthwhile to compile STMT |
| 3648 | | % TO DO: also take size of body into account, and what if only a small part of the body is reachable in each iteration |
| 3649 | | -> b_compiler:b_optimize(STMT,ModVars,LocalState,InState,CSTMT,WF), |
| 3650 | | % print(compiled_while(ModVars)),nl, translate:print_subst(CSTMT),nl, |
| 3651 | | OutState=WhileOutState, |
| 3652 | | exclude(unused_variable(ModVars),LocalState,LS), % only keep modified vars; all other compiled |
| 3653 | | exclude(unused_variable(ModVars),InState,IS) % only keep modified vars; all other compiled |
| 3654 | | ; CSTMT=STMT, LS=LocalState, IS=InState, |
| 3655 | | lazy_exclude(WhileOutState,ModVars,OutState) % only copy over those variables (potentially) modified by While loop |
| 3656 | | % Note: it is important that we only have one copy of each modified variable in OutState |
| 3657 | | ), |
| 3658 | | ground_state_check(InState,GrInState), |
| 3659 | | b_execute_while_loop_block(GrInState,COND,CSTMT,INV,VARIANT,none,VarVal,LS,IS,WhileOutState,WF,BodyPath). |
| 3660 | | |
| 3661 | | :- block b_execute_while_loop_block(-,?,?,?,?,?,?,?,?,?,?,?). |
| 3662 | | b_execute_while_loop_block(_,COND,CSTMT,INV,VARIANT,none,VarVal,LS,IS,WhileOutState,WF,BodyPath) :- |
| 3663 | | % the information passed to the outside is the number of iterations |
| 3664 | | b_execute_while_loop(COND,CSTMT,INV,VARIANT,none,VarVal,LS,IS,WhileOutState,WF,while_bpath(0,none),BodyPath). |
| 3665 | | |
| 3666 | | :- block b_execute_while1(?,?,?,?,?,-,?,?,?,?,?,?). |
| 3667 | | b_execute_while1(COND,STMT,INV,VARIANT,PrevVariantValue,int(VarVal),LocalState,InState,OutState,WF,InPath,OutPath) :- |
| 3668 | | % get_last_wait_flag(while(VarVal),WF,LWF), |
| 3669 | | deref_wf(WF,FreshWF), |
| 3670 | | %ground_constraintprop_wait_flags(FreshWF), % this is not a good idea, co-routine may be triggered while grounding WF; this subsidiary call will prevent grounding from completing and may trigger later waitflags before earlier ones are finished |
| 3671 | | % this can lead to enumeration warnings; the same applies to other calls to ground_constraintprop_wait_flags below |
| 3672 | | b_execute_while_loop(COND,STMT,INV,VARIANT,PrevVariantValue,VarVal,LocalState,InState,OutState,FreshWF,InPath,OutPath). |
| 3673 | | |
| 3674 | | % block on PrevVarVal and VarVal not yet needed here; but we need it for b_execute_while_body below |
| 3675 | | :- block b_execute_while_loop(?,?,?,?,-,?,?,?,?,?,?,?), b_execute_while_loop(?,?,?,?,?,-,?,?,?,?,?,?). |
| 3676 | | b_execute_while_loop(COND,STMT,INV,VARIANT,PrevVarVal,VarVal,LocalState,InState,OutState,WF,InPath,OutPath) :- |
| 3677 | | b_try_check_boolean_expression_c(INV,LocalState,InState,WF,INVRes), |
| 3678 | | %ground_constraintprop_wait_flags(WF), % seems to slow down interpretation |
| 3679 | | b_check_execute_while_loop1(INVRes,COND,STMT,INV,VARIANT,PrevVarVal,VarVal,LocalState,InState,OutState,WF,InPath,OutPath). |
| 3680 | | |
| 3681 | | :- block b_check_execute_while_loop1(-,?,?,?,?,?,?,?,?,?,?, ?,?). |
| 3682 | | b_check_execute_while_loop1(pred_false,_COND,_STMT,INV,_VAR,_PrevVarVal,VarVal,LocalState,InState,_OutState,WF,P,P) :- |
| 3683 | | add_abort_error_span(while_invariant_violation, |
| 3684 | | 'While INVARIANT VIOLATED, current VARIANT value: ',VarVal,span_predicate(INV,LocalState,InState),WF). |
| 3685 | | b_check_execute_while_loop1(pred_true,COND,STMT,INV,VARIANT,PrevVarVal,VarVal,LocalState,InState,OutState,WF,InPath,OutPath) :- |
| 3686 | | b_try_check_boolean_expression_c(COND,LocalState,InState,WF,CondRes), |
| 3687 | | %ground_constraintprop_wait_flags(WF), % seems to slow down interpretation |
| 3688 | | b_check_execute_while_loop2(CondRes,COND,STMT,INV,VARIANT,PrevVarVal,VarVal,LocalState,InState,OutState,WF,InPath,OutPath). |
| 3689 | | |
| 3690 | | :- block b_check_execute_while_loop2(-,?,?,?,?,?,?,?,?,?,?, ?,?). |
| 3691 | | b_check_execute_while_loop2(pred_false,_COND,_STMT,_INV,_VARIANT,_PrevVarVal,_VarVal,_LS,InState,OutState,_WF,P,P) :- |
| 3692 | | OutState=InState. % we copy over the while InState: the updates will be computed at the end of the while loop |
| 3693 | | b_check_execute_while_loop2(pred_true,COND,STMT,INV,VARIANT,PrevVarVal,VarVal,LocalState,InState,OutState,WF,InPath,OutPath) :- |
| 3694 | | |
| 3695 | | %hit_profiler:add_profile_hit(b_execute_while_body(STMT,VarVal,PrevVarVal,LocalState),2), |
| 3696 | | % TO DO: maybe only compute VARIANT now and not above ?? |
| 3697 | | % b_compute_expression(VARIANT,LocalState,InState,VarVal,WF), |
| 3698 | | % ground_constraintprop_wait_flags(WF), %% moved here because VARIANT computed later |
| 3699 | | inc_while_path(InPath,P1), |
| 3700 | | b_execute_while_body(COND,STMT,INV,VARIANT,PrevVarVal,VarVal,LocalState,InState,OutState,WF,P1,OutPath). |
| 3701 | | |
| 3702 | | % register in the event path that we have entered one more while iteration: |
| 3703 | | inc_while_path(while_bpath(Cnt,LastIterPath),while_bpath(C1,LastIterPath)) :- C1 is Cnt+1. |
| 3704 | | add_while_body_path(while_bpath(Cnt,_),Path1,Res) :- |
| 3705 | | get_preference(eventtrace,true),!, |
| 3706 | | Res = while_bpath(Cnt,Path1). % TODO: we could given a setting construct a sequence |
| 3707 | | add_while_body_path(P,_,P). |
| 3708 | | |
| 3709 | | %:- block b_execute_while_body(?,?,?,?,-,?,?,?,?,?,?,?), b_execute_while_body(?,?,?,?,?,-,?,?,?,?,?,?). |
| 3710 | | % we must ensure VarVal and PrevVaraintValue known |
| 3711 | | % the block declaration above for b_execute_while_loop already ensures this |
| 3712 | | b_execute_while_body(_COND,_STMT,_INVARIANT,VAR,_PV,VarVal,LocalState,InState,_OutState,WF,Path,Path) :- |
| 3713 | | VarVal<0, !, % more efficient than is_not_natural(int(VarVal)), |
| 3714 | | %ground_constraintprop_wait_flags(WF), |
| 3715 | | % add_abort_error_span will fail; hence no need to set _OutState |
| 3716 | | add_abort_error_span(while_variant_error,'While VARIANT not NATURAL:',VarVal,span_predicate(VAR,LocalState,InState),WF). |
| 3717 | | b_execute_while_body(_COND,_STMT,_INVARIANT,VAR,PrevVariantValue,VarVal,LocalState,InState,_OutState,WF,Path,Path) :- |
| 3718 | | % check no longer required VarVal>=0,%is_natural(int(VarVal),WF), |
| 3719 | | PrevVariantValue \= none, |
| 3720 | | PrevVariantValue =< VarVal, !, %less_than_equal(int(PrevVariantValue),int(VarVal)), |
| 3721 | | %ground_constraintprop_wait_flags(WF), |
| 3722 | | % add_abort_error_span will fail; hence no need to set _OutState |
| 3723 | | ajoin(['VARIANT = ',VarVal,', previous value = ',PrevVariantValue],Details), |
| 3724 | | add_abort_error_span(while_variant_error,'While VARIANT not decreasing:', |
| 3725 | | Details,span_predicate(VAR,LocalState,InState),WF). |
| 3726 | | b_execute_while_body(COND,STMT,INV,VARIANT,_PrevVariantValue,VarVal,LocalState,InState,OutState,WF,InPath,OutPath) :- |
| 3727 | | % check no longer required due to cuts above %VarVal>=0, %is_natural(int(VarVal),WF), |
| 3728 | | %(PrevVariantValue==none -> true ; less_than(int(VarVal),int(PrevVariantValue))), |
| 3729 | | %ground_constraintprop_wait_flags(WF), % seems to slow down interpretation |
| 3730 | | copy_wf_start(WF,while_body,CWF), |
| 3731 | | b_execute_statement(STMT,LocalState,InState,LHSUpdates,CWF,Path1,output_required), |
| 3732 | | add_while_body_path(InPath,Path1,P1), |
| 3733 | | copy_wf_finish(WF,CWF), |
| 3734 | | ground_det_wait_flag(WF), % TODO: remove this |
| 3735 | | store_intermediate_updates_wf(InState,LHSUpdates,IntermediateState,STMT,WF), |
| 3736 | | check_state_skeleton_bound(LHSUpdates,Bound), % otherwise variable lookups will cause problems; ensures IntermediateState skeleton bound |
| 3737 | | b_execute_while_when_state_set(COND,STMT,INV,VARIANT,VarVal,LocalState,IntermediateState,OutState,WF,Bound,P1,OutPath). |
| 3738 | | |
| 3739 | | :- block b_execute_while_when_state_set(?,?,?,?,?,?,?,?,?,-,?,?). |
| 3740 | | b_execute_while_when_state_set(COND,STMT,INV,VARIANT,PrevVariantValue,LocalState,State,Res,WF,_,InPath,OutPath) :- |
| 3741 | | copy_wf_start(WF,while2,CWF), |
| 3742 | | b_compute_expression(VARIANT,LocalState,State,VarVal,CWF), |
| 3743 | | copy_wf_finish(WF,CWF), |
| 3744 | | b_execute_while1(COND,STMT,INV,VARIANT,PrevVariantValue,VarVal,LocalState,State,Res,WF,InPath,OutPath). |
| 3745 | | |
| 3746 | | % ------------- IF-THEN-ELSE |
| 3747 | | |
| 3748 | | :- block b_execute_else_list2(-,?,?,?, ?,?,?,?,?,-,?). % should we also unblock if LWF set ? needed for seq. composition to set up state ?? |
| 3749 | | b_execute_else_list2(PredRes,Body,Rest,LocalState,InState,OutState,InnerWF,CaseNr,Path,LWF,OR) :- |
| 3750 | | %(var(PredRes) -> print('IF-THEN-ELSE ENUMERATION: '), translate:print_subst(Body),nl ; true), |
| 3751 | | b_execute_else_list3(PredRes,Body,Rest,LocalState,InState,OutState,InnerWF,CaseNr,Path,LWF,OR). |
| 3752 | | |
| 3753 | | b_execute_else_list3(pred_true,Body,_,LocalState,InState,OutState,WF,CaseNr,if(CaseNr,Path),_,OR) :- |
| 3754 | | push_wait_flag_call_stack_info(WF,b_expr_call(if_then_body,Body),WF2), |
| 3755 | | b_execute_statement(Body,LocalState,InState,OutState,WF2,Path,OR). |
| 3756 | | b_execute_else_list3(pred_false,_Body,Rest,LocalState,InState,OutState,WF,CaseNr,Path,LWF,OR) :- |
| 3757 | | CaseNr1 is CaseNr+1, |
| 3758 | | b_execute_else_list(Rest,LocalState,InState,OutState,WF,CaseNr1,Path,LWF,OR). |
| 3759 | | |
| 3760 | | b_execute_else_list([],_,_State,[],_WF,_,if_skip,_LWF,_OR). /* skip */ |
| 3761 | | b_execute_else_list([TExpr|Rest],LocalState,InState,OutState,WF,CaseNr,Path,LWF,OR) :- |
| 3762 | | get_texpr_expr(TExpr,if_elsif(Test,Body)), % Info field not used |
| 3763 | | % get_enumeration_finished_wait_flag(WF,LWF), |
| 3764 | | InnerWF=WF, %clone_wait_flags_from1(WF,InnerWF), |
| 3765 | | push_wait_flag_call_stack_info(InnerWF,b_expr_call(if_elsif_test,Test),WF2), |
| 3766 | | b_try_check_boolean_expression_lwf(Test,LocalState,InState,WF2,'$ENUMERATION',PredRes,LWF), |
| 3767 | | %ground_wait_flag0(InnerWF), |
| 3768 | | (nonvar(PredRes) |
| 3769 | | -> b_execute_else_list3(PredRes,Body,Rest,LocalState,InState,OutState,InnerWF,CaseNr,Path,LWF,OR) |
| 3770 | | ; (LWF == '$GENERATE_ON_DEMAND' -> |
| 3771 | | get_wait_flag0(WF,LWF1), |
| 3772 | | get_wait_flag_if_necessary(PredRes,LWF1,OR,WF,LWF2) |
| 3773 | | ; LWF2=LWF), |
| 3774 | | b_execute_else_list2(PredRes,Body,Rest,LocalState,InState,OutState,InnerWF,CaseNr,Path,LWF2,OR) |
| 3775 | | ). |
| 3776 | | |
| 3777 | | % avoid calling get_wait_flag if possible (expensive) |
| 3778 | | :- block get_wait_flag_if_necessary(-,-,?,?,?). |
| 3779 | | get_wait_flag_if_necessary(PredRes,_,OR,WF,LWF2) :- var(PredRes), !, % we need waitflag |
| 3780 | | (preferences:preference(use_smt_mode,true) -> Prio = 2 % was 2.0 |
| 3781 | | ; OR == output_required -> Prio = 2000 |
| 3782 | | ; Prio = 200000 %last_finite_priority(Prio) |
| 3783 | | ), % TO DO: check if there is a guard inside; if not we can wait much longer; unless we are in CBC mode |
| 3784 | | get_wait_flag(Prio,if_else,WF,LWF2). |
| 3785 | | get_wait_flag_if_necessary(_,_,_,_,_). |
| 3786 | | |
| 3787 | | :- block b_execute_choice(?,?,?,?,?,?,-,?). |
| 3788 | | b_execute_choice(ChoiceList,LocalState,InState,OutState,WF,choice(Nr,Path),_WFC,OR) :- |
| 3789 | ? | nth1(Nr,ChoiceList,Choice), |
| 3790 | | b_execute_statement(Choice,LocalState,InState,OutState,WF,Path,OR). |
| 3791 | | |
| 3792 | | % execute ASSERT PreCond THEN Body |
| 3793 | | b_execute_assertion(_PreCond,Body,LocalState,InState,OutState,WF,Path,OR) :- |
| 3794 | | preference(do_assert_checking,false), % ASSERT checking disabled |
| 3795 | | !, |
| 3796 | | b_execute_statement(Body,LocalState,InState,OutState,WF,Path,OR). |
| 3797 | | b_execute_assertion(PreCond,Body,LocalState,InState,OutState,WF,Path,OR) :- |
| 3798 | | b_try_check_boolean_expression_lwf(PreCond,LocalState,InState,WF,'$ENUMERATION',PredRes,WFC), |
| 3799 | | % enumeration of PredRes before WFE set make sense, Body can instantiate values |
| 3800 | | (var(PredRes) -> get_last_wait_flag(assertion,WF,WFC) ; true), |
| 3801 | | b_execute_assertion_block(PredRes,PreCond,Body,LocalState,InState,OutState,WF,Path,OR). |
| 3802 | | |
| 3803 | | :- block b_execute_assertion_block(-, ?,?,?,?, ?,?,?,?). |
| 3804 | | b_execute_assertion_block(pred_true,_PreCond,Body,LocalState,InState,OutState,WF,Path,OR) :- |
| 3805 | | Path = assertion(IPath), |
| 3806 | | b_execute_statement(Body,LocalState,InState,OutState,WF,IPath,OR). |
| 3807 | | b_execute_assertion_block(pred_false,PreCond,_Body,LocalState,InState,_OutState,WF,Path,_OR) :- |
| 3808 | | Path = assertion_violated, |
| 3809 | | translate_bexpression(PreCond,PreString), |
| 3810 | | add_abort_error_span(assert_error,'ASSERT violated: ',PreString,span_predicate(PreCond,LocalState,InState),WF). |
| 3811 | | |
| 3812 | | :- block check_state_skeleton_bound(-,?). |
| 3813 | | check_state_skeleton_bound([],true). |
| 3814 | | check_state_skeleton_bound([bind(Var,_)|T],Res) :- |
| 3815 | | check_state_skeleton_bound_aux(Var,T,Res). |
| 3816 | | |
| 3817 | | :- block check_state_skeleton_bound_aux(-,?,?). |
| 3818 | | check_state_skeleton_bound_aux(Var,T,Res) :- |
| 3819 | | (atomic(Var) -> true ; print('*** variable name not atomic: '), print(Var),nl), |
| 3820 | | check_state_skeleton_bound(T,Res). |
| 3821 | | |
| 3822 | | |
| 3823 | | b_execute_statement_list_when_state_set(StmtList,LS,State,USoFar,Res,WF,Path,OR) :- |
| 3824 | | check_state_skeleton_bound(State,Bound), % otherwise variable lookups will cause problems |
| 3825 | | b_execute_statement_when_state_set2(StmtList,LS,State,USoFar,Res,WF,Path,Bound,OR). |
| 3826 | | :- block b_execute_statement_when_state_set2(?,?,?,?,?,?,?,-,?). |
| 3827 | | b_execute_statement_when_state_set2(StmtList,LS,State,UpdatesSofar,Res,WF,Path,_,OR) :- |
| 3828 | | copy_wf_start(WF,while2,CWF), |
| 3829 | | b_execute_sequence(StmtList,LS,State,StmtUpdates,CWF,Path,OR), |
| 3830 | | copy_wf_finish(WF,CWF), |
| 3831 | | merge_updates(UpdatesSofar,StmtUpdates,Res). |
| 3832 | | |
| 3833 | | |
| 3834 | | store_values([], [], []). |
| 3835 | | store_values([IDsHd|IDsTl], [VALsHd|VALsTl], [bind(IDsHd,VALsHd)|OutStateTl]) :- |
| 3836 | | store_values(IDsTl, VALsTl, OutStateTl). |
| 3837 | | |
| 3838 | | |
| 3839 | | |
| 3840 | | |
| 3841 | | /* the B syntax does allow multiple definitions in a LET: */ |
| 3842 | | |
| 3843 | | b_execute_let_definitions(Pred,LocalState,State,WF) :- |
| 3844 | | is_a_conjunct(Pred,LHS,RHS),!, |
| 3845 | | b_execute_let_definitions(LHS,LocalState,State,WF), |
| 3846 | | b_execute_let_definitions(RHS,LocalState,State,WF). |
| 3847 | | b_execute_let_definitions(b(equal(LHS,RHS),_,_),LocalState,State,WF) :- !, |
| 3848 | | b_compute_expression(LHS,LocalState,State,LHSVal,WF), |
| 3849 | | b_compute_expression(RHS,LocalState,State,RHSVal,WF), |
| 3850 | | % TO DO: ideally we should check if LHS is a declared identifier and is only defined once |
| 3851 | | kernel_objects:equal_object_optimized(LHSVal,RHSVal,b_execute_let_definitions). |
| 3852 | | b_execute_let_definitions(b(lazy_let_pred(Id,AssignmentExpr,Body),_,_),LocalState,State,WF) :- !, |
| 3853 | | add_lazy_let_id_and_expression(Id,AssignmentExpr,LocalState,State,NewLocalState,WF), |
| 3854 | | b_execute_let_definitions(Body,NewLocalState,State,WF). |
| 3855 | | b_execute_let_definitions(P,_L,_S,WF) :- |
| 3856 | | add_internal_error_wf(b_interpreter,'Let contains a predicate which is not an equality: ',P,P,WF),fail. |
| 3857 | | |
| 3858 | | |
| 3859 | | /* -----------------------------------------*/ |
| 3860 | | /* b_assign_function */ |
| 3861 | | /* -----------------------------------------*/ |
| 3862 | | |
| 3863 | | b_assign_values_or_functions([],[],_LS,_S,[],_WF,_OR). |
| 3864 | | b_assign_values_or_functions([TExpr|ERest],[Value|VRest],LS,S,[bind(Id,New)|NRest],WF,OR) :- |
| 3865 | | get_texpr_expr(TExpr,Expr), |
| 3866 | | b_assign_value_or_function(Expr,Value,LS,S,Id,New,WF,OR), |
| 3867 | | b_assign_values_or_functions(ERest,VRest,LS,S,NRest,WF,OR). |
| 3868 | | |
| 3869 | | :- use_module(kernel_tools,[ground_value_check/2]). |
| 3870 | | :- use_module(kernel_records,[overwrite_record_wf/5]). |
| 3871 | | b_assign_value_or_function(identifier(Id),Value,_LS,_S,LHS_Id,FullValue,WF,_OR) :- !, |
| 3872 | | LHS_Id=Id, |
| 3873 | | equal_object_wf(FullValue,Value,b_assign_value_or_function(Id),WF). |
| 3874 | | b_assign_value_or_function(function(Fun,Arg),Value,LocalState,InState,Id,FullValue,WF,OR) :- !, |
| 3875 | | %b_compute_expression(Arg,LocalState,InState,ArgVal,WF), |
| 3876 | | %expand_global_sets_but_not_closures(ArgVal,EArgVal), |
| 3877 | | FunMNF = eval_must_not_fail('Assignment left-hand-side (or part thereof) could not be evaluated: ',Fun), % could be f(..)(..) |
| 3878 | | b_compute_assign_expressions_when([Arg,FunMNF],LocalState,InState,[ArgVal,FunVal],WF,OR), % or should we pass the OR info around ? |
| 3879 | | /* we do not examine local state: these vars cannot be modified by operations !? */ |
| 3880 | | (OR=output_required -> override(FunVal,ArgVal,Value,NewFun,WF) |
| 3881 | | ; ground_value_check((FunVal,ArgVal,Value),GRV), |
| 3882 | | % wait until all arguments known so that we can effiently compute override |
| 3883 | | blocking_override(GRV,FunVal,ArgVal,Value,NewFun,WF)), |
| 3884 | | get_texpr_expr(Fun,Lhs), |
| 3885 | | b_assign_value_or_function(Lhs,NewFun,LocalState,InState,Id,FullValue,WF,OR). |
| 3886 | | b_assign_value_or_function(record_field(RecordExpr,FieldName),Value,LocalState,InState,LHS_Id,FullValue,WF,OR) :- |
| 3887 | | !, |
| 3888 | | % we allow nested record assignments: x'Field1'Field2 := Value |
| 3889 | | b_compute_expression(RecordExpr,LocalState,InState,OldRecVal,WF), |
| 3890 | | overwrite_record_wf(OldRecVal,FieldName,Value,NewRecVal,WF), |
| 3891 | | get_texpr_expr(RecordExpr,Lhs), |
| 3892 | | b_assign_value_or_function(Lhs,NewRecVal,LocalState,InState,LHS_Id,FullValue,WF,OR). |
| 3893 | | b_assign_value_or_function(LHS,_Value,_LS,_InState,_Id,_FullValue,WF,_OR) :- |
| 3894 | | add_internal_error_wf(b_interpreter,'Illegal LHS for assignment (must be ID(.) or ID\'Field or ID): ',LHS,LHS,WF), |
| 3895 | | fail. |
| 3896 | | |
| 3897 | | :- block blocking_override(-,?,?,?,?,?). |
| 3898 | | blocking_override(_,FunVal,ArgVal,Value,NewFun,WF) :- override(FunVal,ArgVal,Value,NewFun,WF). |
| 3899 | | |
| 3900 | | |
| 3901 | | /* -----------------------------------------*/ |
| 3902 | | /* b_execute_statements_in_parallel */ |
| 3903 | | /* -----------------------------------------*/ |
| 3904 | | /* execute a parallel composition of statements */ |
| 3905 | | :- assert_pre(b_interpreter:b_execute_statements_in_parallel(Ss,LS,State,_Os,WF,_Paths,_OR), |
| 3906 | | (ground_check(Ss),type_check(LS,store), |
| 3907 | | type_check(State,store),type_check(WF,wait_flag))). |
| 3908 | | :- assert_post(b_interpreter:b_execute_statements_in_parallel(_Ss,_LS,_State,Os,WF,_Paths,_OR), |
| 3909 | | (type_check(Os,store),type_check(WF,wait_flag))). |
| 3910 | | |
| 3911 | | b_execute_statements_in_parallel([],_,_,[],_WF,[],_OR). |
| 3912 | | b_execute_statements_in_parallel([Stmt|TS],LocalState,InState,OutState,WF,[Path1|TPath],OR) :- |
| 3913 | | b_execute_statement(Stmt,LocalState,InState,OutState1,WF,Path1,OR), |
| 3914 | | combine_updates(OutState1,OutState2,OutState), /* merge updates */ |
| 3915 | | b_execute_statements_in_parallel(TS,LocalState,InState,OutState2,WF,TPath,OR). |
| 3916 | | |
| 3917 | | |
| 3918 | | %:- block combine_updates(-,-,?). |
| 3919 | | %combine_updates(A,B,C) :- (B==[] -> A=C ; combine_updates_aux(A,B,C)). |
| 3920 | | |
| 3921 | | % TO DO: investigate whether we could also unblock on second argument and copy it to result |
| 3922 | | :- block combine_updates(-,?,?). % can also be used to merge states |
| 3923 | | combine_updates([],X,R) :- !,R=X. |
| 3924 | | combine_updates([H|T],Y,Res) :- !, Res=[H|Z], combine_updates(T,Y,Z). |
| 3925 | | combine_updates(X,Y,Z) :- add_internal_error('Illegal call: ',combine_updates(X,Y,Z)),fail. |
| 3926 | | |
| 3927 | | |
| 3928 | | |
| 3929 | | b_execute_initialisation_statement(Stmt,LS,In,Out,_OUTERWF,Path,OR) :- |
| 3930 | | init_wait_flags(WF,[b_execute_initialisation_statement]), |
| 3931 | | % each initialisation can be executed in isolation (except in CBC mode) |
| 3932 | | b_execute_statement(Stmt,LS,In,Out,WF,Path,OR), |
| 3933 | ? | ground_wait_flags(WF). |
| 3934 | | |
| 3935 | | % replace certain parallels by init_parallel for better error reporting in INITIALISATION |
| 3936 | | % will not be used in CBC mode (by set_up_initialisation or tc_initialise) |
| 3937 | | translate_init_statements(b(Stmt,subst,I),b(TStmt,subst,I)) :- |
| 3938 | | tp2(Stmt,I,TStmt). |
| 3939 | | tp2(assign(LHS,RHS),I,parallel(S)) :- LHS=[_,_|_], |
| 3940 | | % translate multiple assignment into parallel assignments |
| 3941 | | maplist(generate_assignment(I),LHS,RHS,S), |
| 3942 | | !. |
| 3943 | | tp2(Stmt,I,init_statement(b(Stmt,subst,I))) :- %get_preference(provide_trace_information,true), |
| 3944 | | wrap_stmt(Stmt), |
| 3945 | | !. |
| 3946 | | % TO DO: cover WHILE |
| 3947 | | tp2(parallel(S),_,parallel(TS)) :- maplist(translate_init_statements,S,TS), |
| 3948 | | !. |
| 3949 | | tp2(sequence(A),_,sequence(LA)) :- !,maplist(translate_init_statements,A,LA). |
| 3950 | | tp2(if(A),_,if(LA)) :- !, maplist(translate_init_statements,A,LA). |
| 3951 | | tp2(if_elsif(Test,Body),_,if_elsif(Test,LBody)) :- !, |
| 3952 | | translate_init_statements(Body,LBody). |
| 3953 | | tp2(X,_,X). % :- functor(X,F,A), nl,nl,print(uncovered(F/A)),nl,nl. |
| 3954 | | |
| 3955 | | wrap_stmt(assign(_,_)). |
| 3956 | | wrap_stmt(becomes_element_of(_,_)). |
| 3957 | | wrap_stmt(becomes_such(_,_)). |
| 3958 | | wrap_stmt(assign_single_id(_,_)). |
| 3959 | | |
| 3960 | | generate_assignment(I,V,RHS,b(init_statement(b(Assign,subst,I)),subst,I)) :- |
| 3961 | | Assign = assign([V],[RHS]). |
| 3962 | | |
| 3963 | | |
| 3964 | | |
| 3965 | | /* ---------------------------------------*/ |
| 3966 | | /* b_execute_top_level_statement */ |
| 3967 | | /* ---------------------------------------*/ |
| 3968 | | |
| 3969 | | /* this is just like b_execute_statement with the |
| 3970 | | only difference that a PRE-condition is treated like |
| 3971 | | a select, i.e., it does not generate an abort if used |
| 3972 | | outside of its condition */ |
| 3973 | | b_execute_top_level_statement(TExpr,LocalState,InState,OutState,WF,Path,OR) :- |
| 3974 | | get_texpr_expr(TExpr,Stmt), |
| 3975 | ? | b_execute_top_level_statement2(Stmt,TExpr,LocalState,InState,OutState,WF,Path,OR). |
| 3976 | | b_execute_top_level_statement2(precondition(PreCond,Body),TExpr, |
| 3977 | | LocalState,InState,OutState,WF,Path,OR) :- |
| 3978 | | preferences:preference(treat_outermost_pre_as_select,true),!, |
| 3979 | | %debug_println(9,'Top level PRE treated as SELECT'), |
| 3980 | | preference(ltsmin_do_not_evaluate_top_level_guards,NO_EVAL), |
| 3981 | | (NO_EVAL=true, % do not evaluate guards detected by guard splitting |
| 3982 | | get_texpr_info(TExpr,Info),member(prob_annotation('LTSMIN-GUARD'),Info) |
| 3983 | | -> debug_println(9,ltsmin_guard_will_not_be_evaluated) |
| 3984 | ? | ; b_test_boolean_expression(PreCond,LocalState,InState,WF) |
| 3985 | | ), |
| 3986 | | % b_tracetest_boolean_expression(PreCond,LocalState,InState,WF,one), % comment in for debugging |
| 3987 | | b_execute_statement(Body,LocalState,InState,OutState,WF,Path,OR). |
| 3988 | | b_execute_top_level_statement2(lazy_let_subst(Id,IdExpr,Body),_, |
| 3989 | | LocalState,InState,OutState,WF,lazy_let(Path),OR) :- |
| 3990 | | !, |
| 3991 | | add_lazy_let_id_and_expression(Id,IdExpr,LocalState,InState,NewLocalState,WF), |
| 3992 | | b_execute_top_level_statement(Body,NewLocalState,InState,OutState,WF,Path,OR). |
| 3993 | | b_execute_top_level_statement2(_,TExpr,LocalState,InState,OutState,WF,Path,OR) :- |
| 3994 | ? | b_execute_statement(TExpr,LocalState,InState,OutState,WF,Path,OR). |
| 3995 | | |
| 3996 | | |
| 3997 | | /* -----------------------------*/ |
| 3998 | | /* b_execute_operation */ |
| 3999 | | /* -----------------------------*/ |
| 4000 | | /* execute a single operation */ |
| 4001 | | |
| 4002 | | try_get_op_name(FullOperation,_Name) :- var(FullOperation),!. |
| 4003 | | try_get_op_name('-->'(FO,_),Name) :- !,try_get_op_name(FO,Name). |
| 4004 | | try_get_op_name(FullOperation,Name) :- functor(FullOperation,Name,_). |
| 4005 | | |
| 4006 | | |
| 4007 | | |
| 4008 | | :- assert_pre(b_interpreter:b_execute_top_level_operation_update(_Name,_FullOperation,InState,_NewState,_P), |
| 4009 | | (ground_check(InState),type_check(InState,store))). |
| 4010 | | :- assert_post(b_interpreter:b_execute_top_level_operation_update(Name,_FullOperation,_InState,NewState,_Path), |
| 4011 | | (type_check(NewState,store),ground_check(Name))). |
| 4012 | | |
| 4013 | | |
| 4014 | | b_execute_top_level_operation_update(Name,FullOperation,InState,Updates,Path) :- |
| 4015 | ? | b_execute_top_level_operation_update_wf(Name,FullOperation,InState,Updates,Path,WF), |
| 4016 | ? | ground_wait_flags(WF). |
| 4017 | | %store:store_updates_and_normalise(Updates,InState,NewState). |
| 4018 | | |
| 4019 | | |
| 4020 | | b_execute_top_level_operation_update_wf(Name,FullOperation,InState,Updates,Path,WF) :- |
| 4021 | | init_wait_flags(WF,[]), % now treated by call_stack: [expansion_context(op(Name),[])]), |
| 4022 | | % b_execute_operation2 below will push_wait_flag_call_stack_info |
| 4023 | | try_get_op_name(FullOperation,Name), b_top_level_operation(Name), |
| 4024 | | setup_result_input_values(Name,RIV), |
| 4025 | | Info=unknown, % Info extracted later for top_level |
| 4026 | ? | b_execute_operation2(Name,FullOperation,InState,Updates,_,RIV,_,true,Path,Info,WF,output_not_required). |
| 4027 | | |
| 4028 | | % set up value of read output variables *before* operation call to some dummy value |
| 4029 | | setup_result_input_values(Name,ResultInputValues) :- |
| 4030 | | (get_preference(allow_operations_to_read_outputs,true), |
| 4031 | | b_operation_reads_output_variables(Name,_Reads,TReadsInfo) -> |
| 4032 | | maplist(setup_read_output_variable,TReadsInfo,ResultInputValues) |
| 4033 | | %translate_bstate_limited(ResultInputValues,TS), |
| 4034 | | %ajoin(['Warning: setting read output variables in ',Name,' to default values: ',TS],Msg), |
| 4035 | | %add_warning(b_interpreter,Msg), |
| 4036 | | %store_error_for_context_state(eventerror(Name,event_other_error(Msg),[]),_Id) |
| 4037 | | ; ResultInputValues=[]). |
| 4038 | | |
| 4039 | | :- use_module(typing_tools,[any_value_for_type/2]). |
| 4040 | | setup_read_output_variable(reads_result(_,Name,Type),bind(Name,Val)) :- any_value_for_type(Type,Val). |
| 4041 | | |
| 4042 | | % for use by CBC: |
| 4043 | | b_execute_top_level_operation_wf(Name,FullOperation,ParaVals,ResultVals,InState,NewState,Path,WF) :- |
| 4044 | | try_get_op_name(FullOperation,Name), |
| 4045 | | if(b_top_level_operation(Name),true, |
| 4046 | | (add_internal_error('Unknown operation name for b_execute_top_level_operation_wf: ',Name),fail) |
| 4047 | | ), |
| 4048 | | % call before execute operation to copy over results immediately in case b_execute_operation2 generates choice points: |
| 4049 | | store_intermediate_updates_wf(InState,Updates,NewState,operation(Name),WF), % no normalising is better for constraint propagation |
| 4050 | | % TO DO?: copy over unmodified variables b_get_machine_variables(Variables),copy_unmodified_variables(Variables,OpName,InState,NewState) |
| 4051 | | setup_result_input_values(Name,RIV), |
| 4052 | | Info=unknown, % Info extracted later for top_level |
| 4053 | | b_execute_operation2(Name,FullOperation,InState,Updates,ParaVals,RIV,ResultVals,true,Path,Info,WF,output_required). |
| 4054 | | % we should ensure that Operation part is executed to instantiate NewOutState skeleton |
| 4055 | | |
| 4056 | | |
| 4057 | | |
| 4058 | | % Note : if the argument OR (OutputRequired) has value output_required this indicates that the output |
| 4059 | | % an operation or its effect is required (e.g., because of sequential composition) and the constraint solver |
| 4060 | | % should not delay computing the effect of the operation |
| 4061 | | % Atelier-B Handbook 6.16: [R <- op (E)]P = [X := E ; S ; R :=Y] P where op defined by X <- op(Y) = S |
| 4062 | | b_execute_operation_with_parameters(Name,LocalState,InState,Results,Parameters,OutState,ParamValues,ResultValues, |
| 4063 | | Path,Info,WF,OR) :- |
| 4064 | | (b_is_operation_name(Name) |
| 4065 | | -> b_compute_expressions(Parameters,LocalState,InState,ParamValues,WF), |
| 4066 | | % TO DO : remove local vars from InState |
| 4067 | | (b_operation_reads_output_variables(Name,_Reads,TReadsInfo) |
| 4068 | | -> % format('Transferring outputs ~w ~w~n ~w~n',[Name,_Reads,TReadsInfo]), |
| 4069 | | maplist(output_parameters_value(LocalState,InState,Results,Info,WF),TReadsInfo,ResultInputValues) |
| 4070 | | ; ResultInputValues=[]), |
| 4071 | | call_b_execute_operation2(Name,InState,OutState,ParamValues,ResultInputValues,ResultValues,false, |
| 4072 | | Path,Info,WF,OR) |
| 4073 | | ; add_internal_error('Unknown B operation, cannot call: ', |
| 4074 | | b_execute_operation_with_parameters(Name,LocalState,InState,Results,Parameters, |
| 4075 | | OutState,ParamValues,ResultValues,Path,Info,WF,OR)), |
| 4076 | | fail |
| 4077 | | ). |
| 4078 | | |
| 4079 | | |
| 4080 | | %b_execute_operation_in_expression(Name,_LocalState,_InState,_Parameters,_Value,Info,_WF) :- |
| 4081 | | % % This clause is disabled: sometimes variables are marked as read even though they are not: |
| 4082 | | % % the analysis is not precise due to sequential composition, CASE statements, ... |
| 4083 | | % b_operation_reads_output_variables(Name,_Reads,_TReadsInfo),!, |
| 4084 | | % add_error(b_interpreter,'Operation that reads output cannot be called in expression: ',Name,Info), |
| 4085 | | % fail. |
| 4086 | | b_execute_operation_in_expression(Name,LocalState,InState,Parameters,Value,Info,WF) :- |
| 4087 | | % TODO: check that this is a query operation; probably done in type checker |
| 4088 | | OR=output_required, % alternative: OR=output_not_required, |
| 4089 | | setup_result_input_values(Name,ResultInputValues), |
| 4090 | | b_compute_expressions(Parameters,LocalState,InState,ParamValues,WF), |
| 4091 | | (b_get_operation_normalized_read_write_info(Name,Read,_Modified) |
| 4092 | | -> true % Modified should be [] |
| 4093 | | ; member(reads(Read),Info) |
| 4094 | | -> add_message(b_interpeter,'Unregistered operation in expression: ',Name) % prefixed operation not found |
| 4095 | | ; add_error(b_interpeter,'Cannot obtain read-write info for operation in expression:',Name), |
| 4096 | | Read=[] |
| 4097 | | ), |
| 4098 | | create_inner_wait_flags(WF,operation_call_in_expr,OpWF), |
| 4099 | | ground_value_check(ParamValues,ParaGround), |
| 4100 | | exclude(unused_variable(Read),InState,IS), |
| 4101 | | ground_state_check(IS,StateGround), |
| 4102 | | % TO DO: in case of recursion it may be good to delay executing sub-operation until some WF is set |
| 4103 | | call_b_execute_operation2(Name,IS,_OutState,ParamValues,ResultInputValues,ResultValues,false,_Path,Info,OpWF,OR), |
| 4104 | | ground_inner_wf(ParaGround,StateGround,OpWF), |
| 4105 | | make_couplise(ResultValues,Value). |
| 4106 | | |
| 4107 | | % ground inner waitflags when parameters and relevant read variables fully known |
| 4108 | | :- block ground_inner_wf(-,?,?), ground_inner_wf(?,-,?). |
| 4109 | | ground_inner_wf(_,_,WF) :- |
| 4110 | | ground_inner_wait_flags(WF). |
| 4111 | | |
| 4112 | | call_b_execute_operation2(Name,InState,OutState,ParamValues,ResultInputValues,ResultValues,false,Path,Info,WF,OR) :- |
| 4113 | | runtime_profiler:profile_single_call(Name,unknownStateId, b_interpreter: |
| 4114 | | b_execute_operation2(Name,_,InState,OutState,ParamValues,ResultInputValues,ResultValues,false,Path,Info,WF,OR) |
| 4115 | | ). |
| 4116 | | |
| 4117 | | output_parameters_value(LocalState,InState,Results,_,WF,reads_result(Index,ID,_Type),bind(ID,Val)) :- |
| 4118 | | nth1(Index,Results,ReadResultTID),!, |
| 4119 | | def_get_texpr_id(ReadResultTID,ReadID), |
| 4120 | | (lookup_value_without_check(ReadID,LocalState,InState,Val) |
| 4121 | | -> true %check_not_undefined(Val,ReadID,ReadResultTID) |
| 4122 | | ; add_error_wf(output_parameters,'Could not obtain value for read output parameter: ',ReadID,ReadResultTID,WF), |
| 4123 | | Val = term(undefined)). |
| 4124 | | output_parameters_value(_LocalState,_InState,_Results,Info,WF,reads_result(_Index,ID,_Type),bind(ID,Val)) :- |
| 4125 | | add_error_wf(output_parameters,'Could not obtain value for read output parameter: ',ID,Info,WF), |
| 4126 | | Val = term(undefined). |
| 4127 | | |
| 4128 | | % TO DO: use this ? |
| 4129 | | %:- block check_not_undefined(-,?,?). |
| 4130 | | %check_not_undefined(term(undefined),ReadID,ReadResultTID) :- !, |
| 4131 | | % add_error(output_parameters_value,'Output parameter not assigned to: ',ReadID,ReadResultTID). |
| 4132 | | %check_not_undefined(_,_,_). |
| 4133 | | |
| 4134 | | :- use_module(probsrc(succeed_max),[assert_max_reached/1]). |
| 4135 | | % ResultInputValues: in Atelier-B result parameters can also be read ! |
| 4136 | | b_execute_operation2(Name,Operation,InState,Updates,ParaValues,ResultInputValues,ResultValues,_TopLvl,TransInfo,_,_WF,_OR) :- |
| 4137 | | ResultInputValues = [], % in this case the operation's behaviour also depends on current value of output variables |
| 4138 | | lookup_cached_transitions(Name,InState,Info,Solutions),!, |
| 4139 | | (member(max_reached,Info) -> assert_max_reached(Name) ; true), |
| 4140 | | member(trans_cached(ParaValues,ResultValues,Updates,TransInfo),Solutions), |
| 4141 | | create_full_operation(Name,ResultValues,ParaValues,ResultValues,Operation). |
| 4142 | | % TODO: call operation with ParaValues if not found and max_reached is true !! |
| 4143 | | |
| 4144 | | b_execute_operation2(Name,Operation,InState,NewOutState,ParaValues,ResultInputValues,ResultValues, |
| 4145 | | TopLevel,TransInfo,Info,WF,OutputRequired) :- |
| 4146 | | if(b_get_machine_operation_for_animation(Name,Results,Parameters,Body,OType,TopLevel,OpPos), |
| 4147 | | true, |
| 4148 | | (add_error_wf(b_interpreter,'Cannot find operation:',Name,Info,WF),fail)), |
| 4149 | | %% % we do not want skip the evaluation of a guard of an operation with parameters |
| 4150 | | (Info=unknown, TopLevel==true -> CallInfo=OpPos ; CallInfo=Info), |
| 4151 | | push_wait_flag_call_stack_info(WF,operation_call(Name,ParaValues,CallInfo),WF2), |
| 4152 | ? | b_execute_operation3(OType,Name,Operation, |
| 4153 | | InState,NewOutState, |
| 4154 | | Body,Parameters,ParaValues,Results,ResultInputValues,ResultValues,TopLevel,TransInfo,WF2,OutputRequired). |
| 4155 | | |
| 4156 | | create_full_operation(Name,Results,ParaValues,ResultValues,FullOperation) :- |
| 4157 | | Operation =.. [Name|ParaValues], % TO DO: catch if > 255 args (max_arity) |
| 4158 | | ( Results == [] -> /* we have an ordinary operation */ |
| 4159 | | FullOperation = Operation |
| 4160 | | ; FullOperation = '-->'(Operation,ResultValues) |
| 4161 | | ). |
| 4162 | | |
| 4163 | | b_execute_operation3(classic,Name,Operation,InState,NewOutState, |
| 4164 | | Body,Parameters,ParaValues,Results,ResultInputValues,NormalisedResultValues,TopLevel, |
| 4165 | | [path(Path)], % as path is currently filtered out anyway and not stored ! |
| 4166 | | WF,OR) :- |
| 4167 | | %print('Attempting Operation ----> '),print(Name),nl, |
| 4168 | | empty_state(EmptyState), |
| 4169 | ? | set_up_typed_localstate(Parameters,ParaValues,ParamTypedVals,EmptyState,LocalState,positive), |
| 4170 | | l_expand_and_normalise_values(ParaValues,NormalisedParaValues,Parameters), |
| 4171 | | % not useful for call_b_execute_operation2 |
| 4172 | | create_full_operation(Name,Results,NormalisedParaValues, % ditto |
| 4173 | | NormalisedResultValues,Operation), |
| 4174 | | set_up_undefined_localstate_with_predefined(Results,ResultInputValues,InState,NewInState), |
| 4175 | | /* to avoid error messages with sequential composition (store_intermediate_updates) */ |
| 4176 | | check_additional_guard(TopLevel,LocalState,NewInState,WF), |
| 4177 | | (TopLevel==true |
| 4178 | ? | -> b_execute_top_level_statement(Body,LocalState,NewInState,OutState,WF,Path,OR) |
| 4179 | | ; b_execute_statement(Body,LocalState,NewInState,OutState,WF,Path,OR) |
| 4180 | | ), |
| 4181 | | b_tighter_enumerate_all_values(ParamTypedVals,WF), |
| 4182 | | % moved after execute for performance reasons; see PerformanceTests/ParameterEnumeration.mch |
| 4183 | | % print_bt_message('Operation Successful ------> '), |
| 4184 | | (preferences:preference(treat_outermost_pre_as_select,true), |
| 4185 | | preferences:preference(require_operations_to_assign_all_outputs,true) |
| 4186 | | -> ReportError = true ; ReportError = false), |
| 4187 | | get_results(Results,OutState,NormalisedResultValues,NewOutState,ReportError,normalise_results,WF). |
| 4188 | | b_execute_operation3(eventb_operation(ChangeSet,ParaValues,Operation), |
| 4189 | | _Name,Operation, InState,Updates, |
| 4190 | | TBody,Parameters,ParaValues,[],[],[],true,TransInfo,WF,OutputRequired) :- |
| 4191 | | % TO DO: check if need to pass _OutputRequired to b_event |
| 4192 | | b_execute_event_with_change_set_wf(TBody,Parameters,ParaValues,InState,ChangeSet,Updates,TransInfo,WF,OutputRequired). |
| 4193 | | |
| 4194 | | :- use_module(b_global_sets,[add_prob_deferred_set_elements_to_store/3]). |
| 4195 | | % additional guards can be set by the user to constrain/direct the execution of operations |
| 4196 | | check_additional_guard(TopLevel,LocalState,State,WF) :- |
| 4197 | | (TopLevel==true, |
| 4198 | | bmachine:b_machine_temp_predicate(TempPred) |
| 4199 | | /* an additional precondition typed in by the user */ |
| 4200 | | -> check_state_skeleton_bound(LocalState,LB), |
| 4201 | | check_state_skeleton_bound(State,BB), |
| 4202 | | check_additional_guard_when_bound(LB,BB,TempPred,LocalState,State,WF) |
| 4203 | | ; true |
| 4204 | | ). |
| 4205 | | |
| 4206 | | :- block check_additional_guard_when_bound(-,?,?, ?,?,?), check_additional_guard_when_bound(?,-,?, ?,?,?). |
| 4207 | | check_additional_guard_when_bound(_,_,TempPred,LocalState,State,WF) :- |
| 4208 | | (debug_mode(on) -> print(check_additional_guard),nl, print_bexpr(TempPred),nl ; true), |
| 4209 | | add_prob_deferred_set_elements_to_store(State,BState1,visible), |
| 4210 | | b_test_boolean_expression(TempPred,LocalState,BState1,WF). |
| 4211 | | |
| 4212 | | % additional properties can either be set by user or by command-line or by VALUES clause |
| 4213 | | add_additional_properties(OldProperty,NewProperty) :- |
| 4214 | | (bmachine:b_machine_temp_predicate(TempPred) % this overrides additional property ! |
| 4215 | | -> conjunct_predicates([TempPred,OldProperty],NewProperty) |
| 4216 | | ; findall(AddPred,b_machine_additional_property(AddPred),L), |
| 4217 | | (L=[] -> NewProperty = OldProperty |
| 4218 | | ; conjunct_predicates([OldProperty|L],NewProperty) |
| 4219 | | ) |
| 4220 | | ). |
| 4221 | | |
| 4222 | | % get removes results from OutState |
| 4223 | | filter_results(Results,OutState,NewOutState,WF) :- |
| 4224 | | get_results(Results,OutState,_ResultValues,NewOutState,false,do_not_normalise,WF). |
| 4225 | | get_results(Results,OutState,ResultValues,NewOutState,ReportError,Normalise,WF) :- |
| 4226 | | (Results==[] -> (ResultValues,NewOutState) = ([],OutState) |
| 4227 | | ; get_results1(Results,OutState,ResultValues,NewOutState,ReportError,Normalise,WF)). |
| 4228 | | :- block get_results1(?,-,?,?,?,?,?). |
| 4229 | | get_results1([],OutState,[],OutState,_ReportError,_Normalise,_WF). |
| 4230 | | get_results1([R|Results],OutState,[NRV|ResultValues],NewOutState,ReportError,Normalise,WF) :- |
| 4231 | | def_get_texpr_id(R,ResultId), |
| 4232 | | lookup_and_delete_value(ResultId,RV,OutState,OutState2,ReportError,WF,Finished), |
| 4233 | | get_results2(Finished,R,Results,RV,NRV,OutState2,ResultValues,NewOutState,ReportError,Normalise,WF). |
| 4234 | | :- block get_results2(-,?,?,?,?,?,?,?,?,?,?). |
| 4235 | | get_results2(_,R,Results,RV,NRV,OutState2,ResultValues,NewOutState,ReportError,Normalise,WF) :- |
| 4236 | | (Normalise==normalise_results -> normalise_value_for_var(R,true,RV,NRV) |
| 4237 | | ; |
| 4238 | | NRV=RV), % Maybe we should not normalise in CBC mode ? |
| 4239 | | get_results1(Results,OutState2,ResultValues,NewOutState,ReportError,Normalise,WF). |
| 4240 | | |
| 4241 | | |
| 4242 | | |
| 4243 | | /* ------------------------------------------------------------------ */ |
| 4244 | | /* SET UP CONSTANTS */ |
| 4245 | | /* ------------------------------------------------------------------ */ |
| 4246 | | |
| 4247 | | :- use_module(static_ordering). |
| 4248 | | |
| 4249 | | :- volatile no_solution_found_for_constants/0, properties_were_filtered/1, |
| 4250 | | unsat_properties_component/2, unsat_properties_enumeration_warning/1, unsat_properties_abort_error/1, |
| 4251 | | unsat_properties_conjunct_inside_component/4. |
| 4252 | | :- dynamic no_solution_found_for_constants/0, |
| 4253 | | properties_were_filtered/1, |
| 4254 | | unsat_properties_component/2, unsat_properties_enumeration_warning/1, |
| 4255 | | unsat_properties_abort_error/1, |
| 4256 | | unsat_properties_conjunct_inside_component/4. |
| 4257 | | |
| 4258 | | reset_unsat_component_infos :- |
| 4259 | | retractall(unsat_properties_component(_,_)), |
| 4260 | | retractall(unsat_properties_enumeration_warning(_)), |
| 4261 | | retractall(unsat_properties_abort_error(_)), |
| 4262 | | retractall(unsat_properties_conjunct_inside_component(_,_,_,_)). |
| 4263 | | |
| 4264 | | :- use_module(b_interpreter_components,[unsat_conjunct_inside_component/4, unsat_component_abort_error/1, |
| 4265 | | unsat_component_enumeration_warning/1]). |
| 4266 | | % store information about unsatisfiable components for later usage |
| 4267 | | % will only be called when no solution found for components |
| 4268 | | assert_unsat_component_infos(_) :- |
| 4269 | | unsat_component(X,FalseOrUnknown), % from b_interpreter_components |
| 4270 | | assertz(unsat_properties_component(X,FalseOrUnknown)),fail. |
| 4271 | | assert_unsat_component_infos(_) :- |
| 4272 | | unsat_component_enumeration_warning(X), % from b_interpreter_components |
| 4273 | | assertz(unsat_properties_enumeration_warning(X)),fail. |
| 4274 | | assert_unsat_component_infos(_) :- |
| 4275 | | unsat_component_abort_error(X), % from b_interpreter_components |
| 4276 | | assertz(unsat_properties_abort_error(X)),fail. |
| 4277 | | assert_unsat_component_infos(_) :- |
| 4278 | | unsat_conjunct_inside_component(X,BE,Status,Reason), % from b_interpreter_components |
| 4279 | | assertz(unsat_properties_conjunct_inside_component(X,BE,Status,Reason)),fail. |
| 4280 | | assert_unsat_component_infos(Status) :- |
| 4281 | | (unsat_conjunct_inside_component(_,_,false,_) -> Status = false |
| 4282 | | ; unsat_component(X,false), \+ unsat_component_abort_error(X) -> Status = false % what about enumeration warnings? |
| 4283 | | ; Status=unknown). |
| 4284 | | |
| 4285 | | |
| 4286 | | all_unsat_components_marked_prob_ignore :- |
| 4287 | | unsat_properties_conjunct_inside_component(_,_,unknown,'prob-ignore'), % at least one prob-ignore was used |
| 4288 | | !, |
| 4289 | | \+ non_ignore_unsat_component. |
| 4290 | | |
| 4291 | | non_ignore_unsat_component :- |
| 4292 | | unsat_properties_conjunct_inside_component(_,_,Status,Reason), |
| 4293 | | \+ (Status = unknown, Reason = 'prob-ignore'). |
| 4294 | | |
| 4295 | | tcltk_unsatisfiable_components_exist :- unsat_or_unknown_component_exists,!. |
| 4296 | | |
| 4297 | | %tcltk_unsatisfiable_components(list(UnsatComponents)) :- |
| 4298 | | % findall(C,unsat_properties_component(C),UnsatComponents), UnsatComponents\=[]. |
| 4299 | | |
| 4300 | | get_property_components(Components) :- |
| 4301 | | b_machine_has_constants_or_properties, |
| 4302 | | b_get_properties_from_machine(Properties), |
| 4303 | | add_additional_properties(Properties,NewProperty), |
| 4304 | | bsyntaxtree:predicate_components(NewProperty,Components),!. |
| 4305 | | get_property_components([]). |
| 4306 | | |
| 4307 | | :- dynamic uses_implementable_integers/0. |
| 4308 | | :- dynamic min_max_integer_value_used/2. |
| 4309 | | |
| 4310 | | |
| 4311 | | tcltk_quick_describe_unsat_properties(list(FullDescr),STATUS) :- |
| 4312 | | retractall(uses_implementable_integers), |
| 4313 | | retractall(min_max_integer_value_used(_,_)), assertz(min_max_integer_value_used(none,none)), |
| 4314 | | get_property_components(Components), |
| 4315 | | (unsat_properties_conjunct_inside_component(_,_,false,_) |
| 4316 | | -> STATUS='FALSE' |
| 4317 | | ; all_unsat_components_marked_prob_ignore |
| 4318 | | -> STATUS = 'prob-ignore' |
| 4319 | | ; STATUS='UNKNOWN' |
| 4320 | | ), |
| 4321 | | findall(TP, (unsat_properties_conjunct_inside_component(CompNr,P,_Status,_ReasonMsg), % find individual conjuncts |
| 4322 | | % Status can be false or unknown |
| 4323 | | nth1(CompNr,Components,component(FullPred,_Vars)), |
| 4324 | | check_uses_implementable_integers(FullPred), % for the min-max one may need to look at the entire component ! |
| 4325 | | translate_bexpression_with_limit(P,TrP), |
| 4326 | | get_component_enum_warning_message(CompNr,EnumWarningAbortMsg), |
| 4327 | | (EnumWarningAbortMsg = '' -> Prefix = '' ; Prefix = '\n '), |
| 4328 | | get_span_msg(P,'\n ',SpanMsg), |
| 4329 | | ajoin([' * ',TrP,Prefix,EnumWarningAbortMsg,SpanMsg,'\n'],TP)), |
| 4330 | | D1), |
| 4331 | | findall([Msg1,PS|DescrVars], |
| 4332 | | (get_unsat_component_description(Components,CompNr,MaxCompNr, |
| 4333 | | Size,PS,DescrVars,EnumWarningAbortMsg,SpanMsg), |
| 4334 | | ajoin([' * Predicate Component ',EnumWarningAbortMsg,CompNr,'/',MaxCompNr, |
| 4335 | | ' (with ',Size,' conjunct(s))',SpanMsg, ':'],Msg1)), |
| 4336 | | UC), |
| 4337 | | append(UC,UCL), % join all component descriptions |
| 4338 | | (D1=[] |
| 4339 | | % TO DO: adapt message for enumeration warning |
| 4340 | | -> Descr = D2, |
| 4341 | | (UCL=[] -> D2 = ['The properties were satisfiable'] |
| 4342 | | ; D2 = ['No solution found for the following predicates: '|UCL] |
| 4343 | | ) |
| 4344 | | ; Descr = ['The following properties prevented finding a solution:\n'|D12], |
| 4345 | | % Comment: the predicate can trigger the computation of another earlier registered predicate |
| 4346 | | % Example f = {1|->2} & f: A --> B & A = 1..5; A=1..5 triggers the check for f |
| 4347 | | (UCL=[] -> D2=[] |
| 4348 | | ; D2 = ['\nIn addition no solution was found for the following predicates: '|UCL] |
| 4349 | | ), |
| 4350 | | append(D1,D2,D12)), |
| 4351 | | findall(W,other_warning(W),Warnings), |
| 4352 | | (Warnings = [] -> FullDescr=Descr ; append(Descr,['\nWARNINGS/MESSAGES:'|Warnings],FullDescr)). |
| 4353 | | |
| 4354 | | % called by predicate_debugger: |
| 4355 | | get_unsat_component_predicate(CompNr,Predicate,Vars) :- |
| 4356 | | unsat_properties_component(CompNr,_FalseOrUnknown), |
| 4357 | | get_property_components(Components), |
| 4358 | | nth1(CompNr,Components,component(Predicate,Vars)). |
| 4359 | | |
| 4360 | | get_unsat_component_description(Components,CompNr,MaxCompNr,Size,PS,DescrVars,EnumWarningAbortMsg,SpanMsg) :- |
| 4361 | | unsat_properties_component(CompNr,_), |
| 4362 | | \+ unsat_properties_conjunct_inside_component(CompNr,_,_,_), % we have not yet dealt with this component in first findall above |
| 4363 | | nth1(CompNr,Components,component(Pred,Vars)), |
| 4364 | | length(Components,MaxCompNr), |
| 4365 | | size_of_conjunction(Pred,Size), |
| 4366 | | describe_constants(Vars,DescrVars), |
| 4367 | | check_uses_implementable_integers(Pred), % store information in case it does use implementable integers for later user messages |
| 4368 | | (debug_mode(on) -> Lim = 1500 ; Lim = 250), |
| 4369 | | translate_bexpression_with_limit(Pred,Lim,PS), |
| 4370 | | get_component_enum_warning_message(CompNr,EnumWarningAbortMsg), |
| 4371 | | % note: sometimes we have a conjunction with empty span info, but different conjuncts (possibly from different files): |
| 4372 | | get_span_msg(Pred,'\n ',SpanMsg). |
| 4373 | | |
| 4374 | | get_span_msg(Pred,Prefix,SpanMsg) :- |
| 4375 | | (extract_span_description(Pred,SpanStr) -> ajoin([Prefix,SpanStr],SpanMsg) ; SpanMsg = ''). |
| 4376 | | |
| 4377 | | get_component_enum_warning_message(CompNr,EnumWarningAbortMsg) :- |
| 4378 | | (unsat_properties_enumeration_warning(CompNr) |
| 4379 | | ->(unsat_properties_abort_error(CompNr) |
| 4380 | | -> EnumWarningAbortMsg = '[** with Enumeration Warning and WD-ERROR **] ' |
| 4381 | | ; EnumWarningAbortMsg = '[* with Enumeration Warning *] ') |
| 4382 | | ; (unsat_properties_abort_error(CompNr) |
| 4383 | | -> EnumWarningAbortMsg = '[** with WD-ERROR **] ' |
| 4384 | | ; EnumWarningAbortMsg = '') |
| 4385 | | ). |
| 4386 | | |
| 4387 | | describe_constants([],R) :- !,R=[]. |
| 4388 | | describe_constants(Vars,['* over identifiers : '|Vars]). |
| 4389 | | |
| 4390 | | check_uses_implementable_integers(_) :- specfile:eventb_mode,!. % cannot use implementable integers |
| 4391 | | check_uses_implementable_integers(_) :- uses_implementable_integers,!. % no need to check another component |
| 4392 | | check_uses_implementable_integers(Pred) :- |
| 4393 | | (uses_implementable_integers(Pred) |
| 4394 | | -> assertz(uses_implementable_integers), |
| 4395 | | retract(min_max_integer_value_used(Min,Max)), |
| 4396 | | min_max_integer_value_used(Pred,Min,Max,NewMin,NewMax), |
| 4397 | | assertz(min_max_integer_value_used(NewMin,NewMax)) |
| 4398 | | ; true). |
| 4399 | | |
| 4400 | | other_warning(W) :- uses_implementable_integers, |
| 4401 | | preferences:get_preference(minint,MININT), |
| 4402 | | preferences:get_preference(maxint,MAXINT), |
| 4403 | | min_max_integer_value_used(Min,Max), |
| 4404 | | ( (number(Min),Min<MININT ; number(Max),Max>MAXINT) |
| 4405 | | -> ajoin(['\n MININT...MAXINT only set to ',MININT,'..',MAXINT,' but integers used at least from ',Min,'..',Max],W) |
| 4406 | | ; (MAXINT < 127 ; MININT > -128) |
| 4407 | | -> ajoin(['\n MININT...MAXINT possibly too small: ',MININT,'..',MAXINT],W)). |
| 4408 | | % TO DO: we could also extract explicit integer values used in the component and compare them against MININT/MAXINT |
| 4409 | | other_warning(W) :- bmachine_eventb:deferred_set_equality_without_enumeration_axioms(X,_Set), |
| 4410 | | translate_bexpression_with_limit(X,XS), |
| 4411 | | ajoin(['\n Set not recognized as enumerated: ',XS],W). |
| 4412 | | % TO DO: generate warning if MAXINT/MININT small and NAT,NAT1 or INT was used in component(s) above |
| 4413 | | |
| 4414 | | % call if tcltk_quick_describe_unsat_properties fails: |
| 4415 | | % TODO: maxint warning above |
| 4416 | | generate_unsat_properties_warnings :- uses_implementable_integers, |
| 4417 | | preferences:get_preference(minint,MININT), |
| 4418 | | preferences:get_preference(maxint,MAXINT), |
| 4419 | | min_max_integer_value_used(Min,Max), |
| 4420 | | (number(Min),Min<MININT ; number(Max),Max>MAXINT), |
| 4421 | | ajoin(['MININT...MAXINT only set to ',MININT,'..',MAXINT,' but integers used at least from ',Min,'..',Max],Msg), |
| 4422 | | add_warning(setup_constants_unknown,Msg), |
| 4423 | | fail. |
| 4424 | | generate_unsat_properties_warnings :- |
| 4425 | | bmachine_eventb:deferred_set_equality_without_enumeration_axioms(X,Set), |
| 4426 | | translate_bexpression_with_limit(X,TX), |
| 4427 | | Eq = b(equal(X,Set),pred,[]), |
| 4428 | | ajoin(['Deferred SET ',TX,' was not recognized as an enumerated set, you may want to use partition instead of the equality: '],Msg), |
| 4429 | | add_warning(setup_constants_unknown,Msg,Eq,X), |
| 4430 | | fail. |
| 4431 | | generate_unsat_properties_warnings. |
| 4432 | | |
| 4433 | | % ------------- |
| 4434 | | |
| 4435 | | % Complete is either complete_properties or partial_properties if properties were marked as ignore |
| 4436 | | b_set_up_concrete_constants(NormalisedConstantsState,Complete) :- |
| 4437 | | reset_unsat_component_infos, |
| 4438 | | retractall(properties_were_filtered(_)), |
| 4439 | | (no_solution_found_for_constants -> true ; assertz(no_solution_found_for_constants)), |
| 4440 | | % preference(allow_incomplete_partial_setup_constants,AllowSkipping), |
| 4441 | | reset_component_info(true), |
| 4442 | | b_machine_has_constants_or_properties, |
| 4443 | | b_get_machine_constants(UnsortedConstants), |
| 4444 | | !, |
| 4445 | | silent_mode(SilentMode), |
| 4446 | | (SilentMode=on -> true ; bt_start_ms_timer('SETUP_CONSTANTS')), % TO DO: store time for unsat core |
| 4447 | | b_get_properties_from_machine(MProperties), |
| 4448 | | (get_preference(use_ignore_pragmas,true) % filter out prob-ignore predicates here |
| 4449 | | -> conjunction_to_list(MProperties,LMP), % TO DO avoid this conversion and back |
| 4450 | | exclude_count(predicate_has_ignore_pragma, LMP,LMP2,Excluded), % exclude props marked as prob-ignore |
| 4451 | | conjunct_predicates(LMP2,MProperties2), |
| 4452 | | %format('FILTERED (~w): ',[Excluded]),translate:print_bexpr(MProperties2),nl, |
| 4453 | | (Excluded=0 -> Complete=complete_properties |
| 4454 | | ; assertz(properties_were_filtered(Excluded)), Complete=partial_properties) |
| 4455 | | ; MProperties2=MProperties, |
| 4456 | | Complete=complete_properties |
| 4457 | | ), |
| 4458 | | add_additional_properties(MProperties2,Properties), |
| 4459 | | project_onto_static_assertions(Properties,UnsortedConstants,PProperties,PUnsortedConstants), |
| 4460 | ? | solve_properties(PProperties,PUnsortedConstants,ConstantsState), |
| 4461 | | (SilentMode=on -> true ; bt_stop_ms_timer('SETUP_CONSTANTS')), |
| 4462 | | normalise_store(ConstantsState,NormalisedConstantsState), |
| 4463 | | retractall(no_solution_found_for_constants). /* ensure that b_partial_set_up_concrete_constants fails */ |
| 4464 | | b_set_up_concrete_constants([],complete). |
| 4465 | | |
| 4466 | | |
| 4467 | | % solve already projected and filtered properties and constants, return state with solution for constants |
| 4468 | | % cbc_solve_timed almost does what we want; but looks at current state; see solve_free_aux |
| 4469 | | % TODO: move this to a separate module and add cdclt |
| 4470 | | solve_properties(PProperties,PUnsortedConstants,ConstantsState) :- |
| 4471 | | get_preference(use_solver_on_load,Solver), |
| 4472 | ? | solve_properties(Solver,PProperties,PUnsortedConstants,ConstantsState). |
| 4473 | | |
| 4474 | | solve_properties(sat,Properties,_TypedConstants,ConstantsState) :- !, |
| 4475 | | b2sat:solve_predicate_with_satsolver_free(Properties, ConstantsState,Result,[]), |
| 4476 | | Result=solution(_). |
| 4477 | | solve_properties('sat-z3',Properties,_TypedConstants,ConstantsState) :- !, |
| 4478 | | b2sat:solve_predicate_with_satsolver_free(Properties, ConstantsState,Result,[use_satsolver(z3)]), |
| 4479 | | Result=solution(_). |
| 4480 | | solve_properties(cdclt,Properties,_TypedConstants,ConstantsState) :- !, |
| 4481 | | cdclt_solver:cdclt_solve_predicate(Properties,_,Result), |
| 4482 | | Result=solution(ConstantsState). |
| 4483 | | solve_properties(clingo,Properties,_TypedConstants,ConstantsState) :- !, |
| 4484 | | get_preference(maxNrOfInitialisations,MaxNrSols), |
| 4485 | | b2asp:solve_pred_with_clingo(Properties,MaxNrSols,_,Result,_Exhaustive), |
| 4486 | | % TODO: check if non_exhaustive and assert max_reached ? |
| 4487 | | Result=solution(ConstantsState). |
| 4488 | | solve_properties(kodkod,PProperties,PUnsortedConstants,ConstantsState) :- !, |
| 4489 | ? | solve_properties(prob,PProperties,PUnsortedConstants,ConstantsState). % Kodkod processing is done elswehere |
| 4490 | | solve_properties(prob,PProperties,PUnsortedConstants,ConstantsState) :- !, |
| 4491 | | (preferences:preference(use_static_ordering,true) |
| 4492 | | -> sort_ids_by_usage(PUnsortedConstants,PProperties,Constants,no_warnings), % no_warnings as Ids could be used in operations, assertions, ... |
| 4493 | | set_up_typed_localstate(Constants,_FreshVars,TypedVals,[],SortedState,positive), |
| 4494 | | reorder_state(PUnsortedConstants,SortedState,ConstantsState) |
| 4495 | | ; Constants = PUnsortedConstants, |
| 4496 | | set_up_typed_localstate(Constants,_FreshVars,TypedVals,[],ConstantsState,positive) |
| 4497 | | ), |
| 4498 | | % now try and load saved constants from file for sub-machines when -cache is used: |
| 4499 | | load_partial_constants(ConstantsState,PProperties,LPProperties), % Note: this can backtrack |
| 4500 | | reset_component_info(true), % reset in case load_partial_constants backtracks! relevant for test 2112 |
| 4501 | | b_global_sets:static_symmetry_reduction_for_global_sets(ConstantsState), |
| 4502 | ? | b_trace_test_components_full(LPProperties,ConstantsState,TypedVals). |
| 4503 | | solve_properties(SolverName,Properties,_TypedConstants,ConstantsState) :- |
| 4504 | | temporary_set_preference(z3_solve_for_animation, true,Chng), |
| 4505 | | call_cleanup(smt_solvers_interface:smt_solve_predicate_free(SolverName, Properties, ConstantsState), |
| 4506 | | reset_temporary_preference(z3_solve_for_animation,Chng)). |
| 4507 | | |
| 4508 | | |
| 4509 | | b_trace_test_components_full(LPProperties,ConstantsState,TypedVals) :- |
| 4510 | | init_wait_flags_and_push_call_stack(no_wf_available,operation_call('SETUP_CONSTANTS',[],unknown),WF), |
| 4511 | ? | b_trace_test_components_wf(LPProperties,ConstantsState,TypedVals,WF), |
| 4512 | | ground_wait_flags(WF), % WF is just a dummy waitflag to pass call stack info |
| 4513 | | (unsat_or_unknown_component_exists -> (!,fail) ; true). % otherwise some expressions were skipped; use partial_set_up |
| 4514 | | |
| 4515 | | :- volatile project_properties_on_identifiers/1. |
| 4516 | | :- dynamic project_properties_on_identifiers/1. |
| 4517 | | set_projection_on_static_assertions(ALL) :- |
| 4518 | | % set interpreter in mode which projects out all constants not needed |
| 4519 | | % for static assertion checking; all variables are also projected out |
| 4520 | | retractall(project_properties_on_identifiers(_)), |
| 4521 | | (b_get_assertions(ALL,dynamic,[]) -> true |
| 4522 | | ; nl,print('*** WARNING: DYNAMIC B ASSERTIONS NOT EMPTY'),nl,nl), |
| 4523 | ? | b_get_assertions(ALL,static,L), |
| 4524 | | (L=[] -> nl,print('*** WARNING: NO STATIC B ASSERTIONS ***'),nl,nl ; true), |
| 4525 | | conjunct_predicates(L,Conj), |
| 4526 | | (debug_mode(on) -> print('Static Assertion: '),translate:print_bexpr(Conj),nl ; true), |
| 4527 | | bsyntaxtree:predicate_identifiers(Conj,IDs), |
| 4528 | | (debug_mode(on) -> print('IDS: '), print(IDs),nl ; true), |
| 4529 | | assertz(project_properties_on_identifiers(IDs)). |
| 4530 | | |
| 4531 | | % optionally project properties onto those required for checking assertions |
| 4532 | | project_onto_static_assertions(Properties,Constants,NewProperties,NewConstants) :- |
| 4533 | | %% set_projection_on_static_assertions(main), %% main or all %% set by probcli if we_need_only_static_assertions |
| 4534 | | project_properties_on_identifiers(ProjIDs), |
| 4535 | | bsyntaxtree:project_predicate_on_identifiers(Properties,ProjIDs,NewProperties,PIDs,_), |
| 4536 | | sort(PIDs,SIPDs), |
| 4537 | | debug_println(9,projecting(SIPDs)), |
| 4538 | | include(keep_constant(SIPDs),Constants,NewConstants),!. % Constants is a typed identifier list. |
| 4539 | | project_onto_static_assertions(Properties,Constants,Properties,Constants). |
| 4540 | | |
| 4541 | | keep_constant(SIPDs,TypedID) :- get_texpr_id(TypedID,ID),ord_member(ID,SIPDs). |
| 4542 | | |
| 4543 | | |
| 4544 | | |
| 4545 | | /* can be called if b_set_up_concrete_constants failed; will partially set up the state using values found in first ground value propagation phase */ |
| 4546 | | :- use_module(specfile,[get_specification_description/2]). |
| 4547 | | b_partial_set_up_concrete_constants(NormalisedConstantsState) :- |
| 4548 | | assert_unsat_component_infos(Status), % Status is false or unknown |
| 4549 | | no_solution_found_for_constants, |
| 4550 | | (b_partial_set_up_concrete_constants2(NormalisedConstantsState,Status) |
| 4551 | | -> generate_unsat_properties_warnings |
| 4552 | | ; generate_unsat_properties_warnings,fail). |
| 4553 | | |
| 4554 | | b_partial_set_up_concrete_constants2(NormalisedConstantsState,Status) :- |
| 4555 | | b_get_machine_constants(Constants), |
| 4556 | | Constants \= [], |
| 4557 | | (det_solution_for_constant_was_stored(Cst1) -> true), % we have found a solution for at least one constant |
| 4558 | | %set_up_typed_localstate(Constants,[],ConstantsState), -> can create call_residue due to typing fd/2 values |
| 4559 | | findall(bind(ID,_),(member(TID,Constants),def_get_texpr_id(TID,ID)),ConstantsState), |
| 4560 | | formatsilent('SETUP_CONSTANTS not successful, but solutions were found for some constants (e.g., ~w):~n',[Cst1]), |
| 4561 | | (fill_in_det_solutions(ConstantsState,FC) |
| 4562 | | -> All=all, TMSG = [')'] |
| 4563 | | ; All=some, TMSG = [', e.g., ',Cst1,')'] |
| 4564 | | ), |
| 4565 | | (preference(allow_incomplete_partial_setup_constants,true) |
| 4566 | | -> true |
| 4567 | | ; % fill_in_det_solutions will fail if not all constants valued and |
| 4568 | | % if allow_incomplete_partial_setup_constants is false |
| 4569 | | get_specification_description(properties,PS), |
| 4570 | | % TO DO: determine if time-out occurred or not |
| 4571 | | (Status=false |
| 4572 | | -> ajoin([PS,' are unsatisfiable (but ',All,' CONSTANTS valued'|TMSG],Msg), |
| 4573 | | add_error(setup_constants_inconsistent,Msg) |
| 4574 | | ; unsat_properties_abort_error(_) |
| 4575 | | -> ajoin([PS,' are unknown due to well-definedness error(s) (but ',All,' CONSTANTS valued'|TMSG],Msg), |
| 4576 | | add_error(setup_constants_unknown,Msg) |
| 4577 | | ; unsat_component_enumeration_warning(_) |
| 4578 | | -> ajoin([PS,' are unknown due to enumeration warning(s) (but ',All,' CONSTANTS valued'|TMSG],Msg), |
| 4579 | | add_error(setup_constants_unknown,Msg) |
| 4580 | | ; all_unsat_components_marked_prob_ignore |
| 4581 | | -> ajoin([PS,' are unknown due to prob-ignore pragmas (but ',All,' CONSTANTS valued'|TMSG],Msg), |
| 4582 | | add_message(setup_constants_unknown,Msg) |
| 4583 | | ; |
| 4584 | | ajoin([PS,' are unknown (but ',All,' CONSTANTS valued'|TMSG],Msg), |
| 4585 | | % typical message: but some CONSTANTS valued |
| 4586 | | add_error(setup_constants_unknown,Msg) |
| 4587 | | ), |
| 4588 | | All=all % only continue if all constants valued |
| 4589 | | ), |
| 4590 | | normalise_store(FC,NormalisedConstantsState). |
| 4591 | | |
| 4592 | | |
| 4593 | | :- use_module(store,[no_value_for_variable/2]). |
| 4594 | | fill_in_det_solutions([],[]). |
| 4595 | | fill_in_det_solutions([bind(Var,Val)|T],[bind(Var,FVal)|FT]) :- |
| 4596 | | (det_solution_for_constant(Var,Val) |
| 4597 | | -> (silent_mode(on) -> true ; write(' --det--> '),print_term_summary(bind(Var,Val))), |
| 4598 | | FVal=Val |
| 4599 | | ; %format('No (deterministic) solution was found or stored for constant: ~w~n',[Var]), % message printed by det_solution_for_constant |
| 4600 | | (preference(allow_incomplete_partial_setup_constants,true) |
| 4601 | | -> no_value_for_variable(FVal,Var) |
| 4602 | | ; format('Aborting SETUP_CONSTANTS (set preference ALLOW_INCOMPLETE_SETUP_CONSTANTS to TRUE to allow ProB to proceed without having values for all CONSTANTS)~n',[]), |
| 4603 | | fail |
| 4604 | | ) |
| 4605 | | ), |
| 4606 | | fill_in_det_solutions(T,FT). |
| 4607 | | |
| 4608 | | |
| 4609 | | |
| 4610 | | /* --------------------------------- */ |
| 4611 | | |
| 4612 | | :- use_module(b_state_model_check, [b_check_valid_state/1]). |
| 4613 | | |
| 4614 | | % note: set_up_initialisation for CBC checks does not use this predicate |
| 4615 | | b_initialise_machine(ConstantsState,NormalisedInitialVarsState,FullInitialState,Path) :- |
| 4616 | ? | if(b_initialise_machine2(ConstantsState,NormalisedInitialVarsState,FullInitialState,Path), |
| 4617 | | true, |
| 4618 | | (\+ logged_error(initialisation_fails,_,_,_), % we have already provided a more precise error message |
| 4619 | | \+ abort_error_exists_in_context_state(initialisation_fails), % ditto as a state error |
| 4620 | | (bmachine:b_machine_temp_predicate(TempPred) |
| 4621 | | -> add_message(initialisation_fails,'INITIALISATION FAILS with provided predicate: ',TempPred,TempPred) |
| 4622 | | ; b_get_initialisation_from_machine(Stmt,_OType) |
| 4623 | | -> add_error(initialisation_fails,'INITIALISATION FAILS','',Stmt) |
| 4624 | | ; add_error(initialisation_fails,'MACHINE has no INITIALISATION clause') |
| 4625 | | ), |
| 4626 | | fail) |
| 4627 | | ). |
| 4628 | | |
| 4629 | | b_initialise_machine2(ConstantsState,NormalisedInitialVarsState,FullInitialState,Path) :- |
| 4630 | | /* InitialVarsState is an ouput */ |
| 4631 | | b_get_initialisation_from_machine(InitStatement,OType), |
| 4632 | | ( InitStatement=[] -> |
| 4633 | | Path = [], |
| 4634 | | add_message(b_initialise_machine,'Machine has no INITIALISATION statement(s)!'), |
| 4635 | | NormalisedInitialVarsState = [], |
| 4636 | | FullInitialState = ConstantsState |
| 4637 | | ; OType == classic -> |
| 4638 | | Path = [path(PathInfo)], |
| 4639 | | b_get_machine_variables(DeclaredVars), |
| 4640 | | set_up_undefined_localstate(DeclaredVars,ConstantsState,NewInState), |
| 4641 | | /* this is if the INITIALISATION has a sequential composition; to avoid error messages */ |
| 4642 | | get_texpr_info(InitStatement,PosInfo), |
| 4643 | | init_wait_flags_with_call_stack(WF,[operation_call('INITIALISATION',[],PosInfo)]), |
| 4644 | | (bmachine:b_machine_temp_predicate(_) |
| 4645 | | -> InitStatement2=InitStatement /* avoid enumerating each init statement in parallel in case additional guard links them */ |
| 4646 | | ; translate_init_statements(InitStatement,InitStatement2) |
| 4647 | | ), |
| 4648 | | check_additional_guard(true,InitialVarsState,ConstantsState,WF), |
| 4649 | ? | b_execute_statement(InitStatement2,[],NewInState,InitialVarsState,WF,PathInfo), |
| 4650 | | % Warning: in Z mode (e.g. test 565) the variable skeleton may not yet be set up ! |
| 4651 | | sort_variable_binding(InitialVarsState,NormalisedInitialVarsState), |
| 4652 | | append(NormalisedInitialVarsState,ConstantsState,FullInitialState), % put constants at end to make sharing of complete tail-list easier for successor states; should be compatible with order in expand_const_and_vars |
| 4653 | | split_names_and_types(DeclaredVars,DeclaredNames,DeclaredTypes), |
| 4654 | | ground_wait_flags(WF), |
| 4655 | | |
| 4656 | | b_enumerate_values_in_store(DeclaredNames,DeclaredTypes,_,InitialVarsState,WF) %% MOVED HERE TO AVOID that target values are enumerated before initialisation effect computed |
| 4657 | | ; OType = eventb_operation(_ChangeSet,ParaValues,_Operation) -> |
| 4658 | | prepare_eventb_initialisation(ConstantsState,InitialVarsUnNormState,FullInitialUnNormState), |
| 4659 | | get_texpr_expr(InitStatement, |
| 4660 | | rlevent(_Name,_Sec,_Stat,Parameters,_Grd,_Thm,_Act,_VWit,_PWit,_Unmod,_AbsEv)), |
| 4661 | | b_execute_event('initialisation',InitStatement,Parameters,ParaValues,ConstantsState,FullInitialUnNormState,Path), |
| 4662 | | normalise_store(InitialVarsUnNormState,NormalisedInitialVarsState), |
| 4663 | | normalise_store(FullInitialUnNormState,FullInitialState) % a bit of redundant work here; we do the work twice and try to normalise constants again ! |
| 4664 | | ), |
| 4665 | | b_check_valid_state(FullInitialState). |
| 4666 | | |
| 4667 | | % ensure that the variables always appear in the same order as reported by b_get_machine_variables: |
| 4668 | | % this is important for e.g., state_packing TO DO: we could sort them according to the variable names, but would there be a gain ?? |
| 4669 | | sort_variable_binding(Store,SortedStore) :- %print(sort_variable_binding(Store,SortedStore)),nl, |
| 4670 | | b_get_machine_variables(Variables), |
| 4671 | | sort_aux(Variables,Store,[],SortedStore). |
| 4672 | | sort_aux([],S,Acc,NormRes) :- reverse(Acc,Res), |
| 4673 | | normalise_store(Res,NormRes), |
| 4674 | | (S=[] -> true ; add_internal_error('Unknown variable bindings: ',S)). |
| 4675 | | sort_aux([TVar|T],Store,Acc,Res) :- def_get_texpr_id(TVar,ID), |
| 4676 | | bselect(ID,Val,Store,Rest), |
| 4677 | | sort_aux(T,Rest,[bind(ID,Val)|Acc],Res). |
| 4678 | | |
| 4679 | | :- block bselect(?,?,-,?). % in Z mode (e.g. test 565) the variable skeleton may not yet be set up ! |
| 4680 | | bselect(ID,Val,[bind(ID1,Val1)|T],Rest) :- % we assume ID1 is nonvar |
| 4681 | | (ID=ID1 -> Val=Val1,T=Rest |
| 4682 | | ; Rest = [bind(ID1,Val1)|RT], |
| 4683 | | bselect(ID,Val,T,RT)). |
| 4684 | | |
| 4685 | | prepare_eventb_initialisation(ConstantsState,InitialVarsState,FullInitialState) :- |
| 4686 | | b_get_machine_variables(Variables), |
| 4687 | | empty_state(Empty), |
| 4688 | | set_up_typed_localstate(Variables,_,_,Empty,InitialVarsState,positive), |
| 4689 | | % NormalisedInitialVarsState = InitialVarsState, % TO DO : check that b_event normalises this |
| 4690 | | combine_updates(InitialVarsState,ConstantsState,FullInitialState). |
| 4691 | | % put constants at end, to be compatible with b_initialise_machine2 and expand_const_and_vars order and e.g. ltsmin |
| 4692 | | |
| 4693 | | |
| 4694 | | |
| 4695 | | reset_b_interpreter :- |
| 4696 | | %reset_partial_evaluator, |
| 4697 | | retractall(project_properties_on_identifiers(_)), |
| 4698 | | reset_unsat_component_infos, |
| 4699 | | retractall(no_solution_found_for_constants). |
| 4700 | | |
| 4701 | | :- use_module(eventhandling,[register_event_listener/3]). |
| 4702 | | :- register_event_listener(clear_specification,reset_b_interpreter, |
| 4703 | | 'Reset B-Interpreter Memo Table & Unsat Components.'). |