1 % (c) 2009-2024 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(b_interpreter_check,[imply_test_boolean_expression/7,
6 equiv_test_boolean_expression/7,
7 equiv_bidrectional_test_boolean_expression/7,
8 get_priority_of_boolean_expression/2, get_priority_of_boolean_expression2/2,
9 b_check_boolean_expression/5,b_check_boolean_expression/7,
10
11 /* some lower-level propagation predicates */
12 imply/3, imply_true/2,
13 b_check_forall_wf/8, b_check_exists_wf/7,
14
15 reify_closure_with_small_cardinality/5,
16
17 register_predicate/6, norm_pred_check/2, norm_expr_check/2,
18
19 conjoin/6, disjoin/6,
20 check_less_than_equal/3
21 ]).
22
23 /* A version for checking the truth value of boolean-expressions,
24 the result is instantiated with pred_true/pred_false as soon as the result is known */
25 /* Warning: does not cover all expressions */
26 /* It provides SMT-like performance, for predicates involving arithmetic comparisons (<,...),
27 equality, disequality and membership/not_membership
28 It is not restricted to CNF
29 */
30
31 :- meta_predicate wd_delay(0,-,-,-).
32 :- meta_predicate wd_delay_block(0,-,-,-,-,-).
33 :- meta_predicate wd_delay_until_needed(-,0).
34 :- meta_predicate wd_delay_until_needed_block(-,-,0).
35
36 :- use_module(debug).
37 :- use_module(self_check).
38 :- use_module(error_manager).
39 :- use_module(b_interpreter,[b_compute_expression/5, b_not_test_boolean_expression/6, b_test_boolean_expression/6, b_test_boolean_expression/4]).
40 :- use_module(kernel_waitflags).
41
42
43 :- use_module(kernel_objects,[less_than_direct/2, less_than_equal_direct/2]).
44
45 :- use_module(kernel_equality).
46
47 :- use_module(tools).
48
49 :- use_module(module_information,[module_info/2]).
50 :- module_info(group,interpreter).
51 :- module_info(description,'This module provides a reified interpreter for certain predicates.').
52
53
54
55
56 :- block imply_test_boolean_expression(-, ?,?,?,?,?,?). % TO DO: pass at least Ai to it
57 imply_test_boolean_expression(PredRes1,PredRes2, RHS,LocalState,State,WF,Ai) :-
58 (PredRes1=PredRes2
59 ? -> b_test_boolean_expression(RHS,LocalState,State,WF,Ai,_)
60 ; true
61 ).
62
63 :- block equiv_test_boolean_expression(-, ?,?,?,?,?,?).
64 equiv_test_boolean_expression(PredRes,PredRes, RHS,LocalState,State,WF,Ai) :- !,
65 b_test_boolean_expression(RHS,LocalState,State,WF,Ai,_).
66 equiv_test_boolean_expression(_PredRes,_, RHS,LocalState,State,WF,Ai) :-
67 ? b_not_test_boolean_expression(RHS,LocalState,State,WF,Ai,_).
68
69 equiv_bidrectional_test_boolean_expression(PredResLHS,PredResRHS, _LHS,_RHS,_LocalState,_State,WF) :-
70 PredResLHS=PredResRHS,
71 ? (var(PredResLHS)
72 -> % create a choice point to enumerate two possible solutions
73 % important, e.g., for not((y > 0 & y * y > 20) <=> (y * y > 25 & y > 0))
74 get_last_wait_flag(equivalence,WF,LWF),
75 enum_bool(PredResLHS,LWF)
76 ; true).
77 :- block enum_bool(-,-).
78 enum_bool(pred_true,_).
79 enum_bool(pred_false,_).
80 /*
81 :- block equiv_bidrectional_test_boolean_expression(-,-, ?,?,?,?,?).
82 equiv_bidrectional_test_boolean_expression(PredResLHS,PredResRHS, LHS,RHS,LocalState,State,WF) :-
83 ( PredResLHS == pred_true -> b_test_boolean_expression(RHS,LocalState,State,WF)
84 ; PredResLHS == pred_false -> b_not_test_boolean_expression(RHS,LocalState,State,WF)
85 ; PredResRHS == pred_true -> b_test_boolean_expression(LHS,LocalState,State,WF)
86 ; PredResRHS == pred_false -> b_not_test_boolean_expression(LHS,LocalState,State,WF)
87 ; add_error_fail(equiv,'Illegal values: ',equiv_bidrectional_test_boolean_expression(PredResLHS,PredResRHS))
88 ).
89 */
90
91 % return starting priority for binary choice points; should be power of 2
92 get_priority_of_boolean_expression(priority(P),Prio) :- !,
93 % case generated for disjoin by contains_fd_element, and not_in_difference_set_wf,not_in_intersection_set_wf,in_union_set_wf
94 Prio=P.
95 get_priority_of_boolean_expression(b(Expr,_,_Infos),Prio) :- !,
96 % try to estimate a priority for performing a case split upon a predicate
97 % i.e., forcing a predicate Expr to be true/false
98 get_priority_of_boolean_expression2(Expr,Prio).
99 get_priority_of_boolean_expression(E,Prio) :-
100 add_internal_error('Boolean expression not properly wrapped: ',get_priority_of_boolean_expression(E,Prio)),
101 get_priority_of_boolean_expression2(E,Prio).
102
103 :- use_module(bsyntaxtree).
104 get_priority_of_boolean_expression2(truth,1) :- !. %, nl,nl,print('TRUTH in disjunct/conjunct'),nl.
105 get_priority_of_boolean_expression2(falsity,1) :- !. %, nl,nl,print('FALSITY in disjunct/conjunct'),nl.
106 get_priority_of_boolean_expression2(_,R) :-
107 preferences:preference(data_validation_mode,true), % in data validation mode we want to drive enumeration from data values only
108 !, R=4096.
109 get_priority_of_boolean_expression2(_,R) :- !, R=4. % force SMT style case-splitting; was 3 before using get_binary_choice_wait_flag_exp_backoff; raising this to 4 makes test 1358, 49 behave better (baload_R07 recognised possible)
110
111
112
113
114 count_number_of_conjuncts(b(Expr,_,_Infos),Prio) :- !,
115 count_number_of_conjuncts2(Expr,Prio).
116 count_number_of_conjuncts(priority(_),Prio) :- !, Prio=1.
117 count_number_of_conjuncts(B,Prio) :-
118 add_internal_error('Expression not wrapped: ',count_number_of_conjuncts(B,Prio)),Prio=1.
119 count_number_of_conjuncts2(conjunct(A,B),Nr) :- !, count_number_of_conjuncts(A,NA),
120 count_number_of_conjuncts(B,NB), Nr is NA+NB.
121 count_number_of_conjuncts2(norm_conjunct(_,RHS),Res) :- length(RHS,Len),!,
122 Res is Len+1.
123 count_number_of_conjuncts2(negation(A),Nr) :- !, count_number_of_disjuncts(A,Nr).
124 count_number_of_conjuncts2(_,1).
125
126 :- public count_number_of_disjuncts/2. %currently commented out above
127 count_number_of_disjuncts(b(Expr,_,_Infos),Prio) :- !,
128 count_number_of_disjuncts2(Expr,Prio).
129 count_number_of_disjuncts(priority(_),Prio) :- !, Prio=1.
130 count_number_of_disjuncts(B,Prio) :-
131 add_internal_error('Expression not wrapped: ',count_number_of_disjuncts(B,Prio)),Prio=1.
132 count_number_of_disjuncts2(disjunct(A,B),Nr) :- !, count_number_of_disjuncts(A,NA),
133 count_number_of_disjuncts(B,NB), Nr is NA+NB.
134 count_number_of_disjuncts2(norm_disjunct(_,RHS),Res) :- length(RHS,Len),!,
135 Res is Len+1.
136 count_number_of_disjuncts2(negation(A),Nr) :- !, count_number_of_conjuncts(A,Nr).
137 count_number_of_disjuncts2(_,1).
138
139
140
141 % we need to ensure that b_check_boolean_expression does not create a choice point on its own
142
143 b_check_boolean_expression(b(Expr,_,Infos),LS,S,WF,Res) :-
144 (composed(Expr) -> empty_avl(Ai)
145 ; Ai = no_avl), % simple expression: no sharing is possible: no need to register expressions
146 create_wfwd_needed(WF,WFD),
147 b_check_boolean_expression2(Expr,Infos,LS,S,WFD,Res,Ai,_).
148
149 composed(negation(_)).
150 composed(conjunct(_,_)).
151 composed(disjunct(_,_)).
152 composed(implication(_,_)).
153 composed(equivalence(_,_)).
154 composed(let_predicate(_,_,_)).
155 composed(lazy_let_pred(_,_,_)).
156
157 b_check_boolean_expression(E,LS,S,WF,Res,Ai,Ao) :-
158 % WFD adds information about WD context: wfwd(WF_store, ExpectedVal, Val,Infos)
159 % when Val becomes nonvar: if Val==ExpectedVal we need the value of E, otherwise it should be discarded
160 create_wfwd_needed(WF,WFD),
161 ? b_check_boolean_expression1(E,LS,S,WFD,Res,Ai,Ao).
162
163 b_check_boolean_expression0(WDE,WDV,Expr,LS,S,WF,Res,Ai,Ao) :-
164 create_wfwd(WF,WDE,WDV,WFD),
165 ? b_check_boolean_expression1(Expr,LS,S,WFD,Res,Ai,Ao).
166
167
168 b_check_boolean_expression1(b(Expr,_,Infos),LS,S,WFD,Res,Ai,Ao) :- get_wd(WFD,WDE,WDV),!,
169 % print('check : '), translate:print_bexpr(b(Expr,pred,Infos)),nl,
170 (nonvar(WDV),WDE \= WDV % the expression is not needed
171 -> Ai=Ao,
172 (var(Res)
173 -> Res=pred_false % set it to false, value does not matter; note: predicate is not reused
174 ; true)
175 ? ; b_check_boolean_expression2(Expr,Infos,LS,S,WFD,Res,Ai,Ao)).
176 b_check_boolean_expression1(E,LS,S,WFD,Res,Ai,Ao) :-
177 add_internal_error('Boolean expression not properly wrapped: ',b_check_boolean_expression1(E,LS,S,WFD,Res,Ai,Ao)),
178 b_check_boolean_expression2(E,[],LS,S,WFD,Res,Ai,Ao).
179
180 % normalise conjunction into flat list of conjuncts
181 normalise_conjunct(b(E,_,Info)) --> normalise_conjunct2(E,Info).
182 normalise_conjunct2(conjunct(A,B),_) --> !,normalise_conjunct(A),normalise_conjunct(B).
183 normalise_conjunct2(F,Info) --> [b(F,pred,Info)].
184
185 construct_norm_conjunct(A,b(B,pred,Info)) :- construct_norm_conjunct2(A,B,Info).
186 construct_norm_conjunct2([],truth,[]).
187 construct_norm_conjunct2([H|T],Res,Info) :-
188 (T==[] -> H=b(Res,pred,Info) ; Res=norm_conjunct(H,T),Info=[]).
189 % TO DO: build up member(contains_wd_condition,Infos)
190
191
192 % normalise disjunction into flat list of disjuncts
193 normalise_disjunct(b(E,_,Info)) --> normalise_disjunct2(E,Info).
194 normalise_disjunct2(disjunct(A,B),_) --> !,normalise_disjunct(A),normalise_disjunct(B).
195 normalise_disjunct2(F,Info) --> [b(F,pred,Info)].
196
197 construct_norm_disjunct(A,b(B,pred,Info)) :- construct_norm_disjunct2(A,B,Info).
198 construct_norm_disjunct2([],falsity,[]).
199 construct_norm_disjunct2([H|T],Res,Info) :-
200 (T==[] -> H=b(Res,pred,Info) ; Res=norm_disjunct(H,T),Info=[]).
201
202 can_negate_expression(b(Expr,pred,I),b(NExpr,pred,I)) :- can_negate2(Expr,NExpr).
203 can_negate2(equal(A,B),not_equal(A,B)).
204 can_negate2(not_equal(A,B),equal(A,B)).
205 can_negate2(member(A,B),not_member(A,B)).
206 can_negate2(not_member(A,B),member(A,B)).
207 can_negate2(subset(A,B),not_subset(A,B)).
208 can_negate2(not_subset(A,B),subset(A,B)).
209 can_negate2(subset_strict(A,B),not_subset_strict(A,B)).
210 can_negate2(not_subset_strict(A,B),subset_strict(A,B)).
211 can_negate2(greater_equal(A,B),less(A,B)).
212 can_negate2(less(A,B),greater_equal(A,B)).
213 can_negate2(less_equal(A,B),greater(A,B)).
214 can_negate2(greater(A,B),less_equal(A,B)).
215 can_negate2(less_real(A,B),less_equal_real(B,A)).
216 can_negate2(less_equal_real(A,B),less_real(B,A)).
217
218 b_check_boolean_expression2(truth,_,_,_,_WFD,Res,Ai,Ao) :- !,Res=pred_true, Ai=Ao.
219 b_check_boolean_expression2(falsity,_,_,_,_WFD,Res,Ai,Ao) :- !,Res=pred_false, Ai=Ao.
220 b_check_boolean_expression2(negation(BExpr),_,LocalState,State,WFD,Res,Ai,Ao) :- !,
221 (can_negate_expression(BExpr,NBExpr)
222 -> /* avoid introducing negate propagator; maybe not necessary */
223 b_check_boolean_expression1(NBExpr,LocalState,State,WFD,Res,Ai,Ao)
224 ; negate(NR,Res),
225 b_check_boolean_expression1(BExpr,LocalState,State,WFD,NR,Ai,Ao)).
226 b_check_boolean_expression2(conjunct(LHS,RHS),CInfo,LocalState,State,WFD,Res,Ai,Ao) :- !,
227 normalise_conjunct2(conjunct(LHS,RHS),CInfo,NormRes,[]),
228 construct_norm_conjunct2(NormRes,NC,Info),
229 ? b_check_boolean_expression2(NC,Info,LocalState,State,WFD,Res,Ai,Ao).
230 b_check_boolean_expression2(norm_conjunct(LHS,RHS),_,LocalState,State,wfwd(WF,WDE,WDV,_),Res,Ai,Ao) :- !,
231 construct_norm_conjunct(RHS,NC),
232 conjoin(LR,RR,Res,LHS,NC,WF),
233 create_wfwd(WF,WDE,WDV,WFD),
234 ? b_check_boolean_expression1(LHS,LocalState,State,WFD,LR,Ai,Aii),
235 propagagate_wfwd(WDE,WDV,GuardFlag,LR,pred_false), % if WDE/=WDV then set GuardFlag to pred_false; indicating to RHS that it is not needed also
236 ? b_check_boolean_expression0(pred_true,GuardFlag,NC,LocalState,State,WF,RR,Aii,Ao).
237 b_check_boolean_expression2(implication(LHS,RHS),_,LocalState,State,wfwd(WF,WDE,WDV,_),Res,Ai,Ao) :- !,
238 imply(LR,RR,Res),
239 create_wfwd(WF,WDE,WDV,WFD),
240 b_check_boolean_expression1(LHS,LocalState,State,WFD,LR,Ai,Aii),
241 propagagate_wfwd(WDE,WDV,GuardFlag,LR,pred_false),
242 ? b_check_boolean_expression0(pred_true,GuardFlag,RHS,LocalState,State,WF,RR,Aii,Ao).
243 b_check_boolean_expression2(equivalence(LHS,RHS),_,LocalState,State,WFD,Res,Ai,Ao) :- !, equiv(LR,RR,Res),
244 b_check_boolean_expression1(LHS,LocalState,State,WFD,LR,Ai,Aii),
245 b_check_boolean_expression1(RHS,LocalState,State,WFD,RR,Aii,Ao).
246 b_check_boolean_expression2(disjunct(LHS,RHS),CInfo,LocalState,State,WFD,Res,Ai,Ao) :- !,
247 normalise_disjunct2(disjunct(LHS,RHS),CInfo,NormRes,[]),
248 construct_norm_disjunct2(NormRes,NC,Info),
249 b_check_boolean_expression2(NC,Info,LocalState,State,WFD,Res,Ai,Ao).
250 b_check_boolean_expression2(norm_disjunct(LHS,RHS),_,LocalState,State,wfwd(WF,WDE,WDV,_),Res,Ai,Ao) :- !,
251 construct_norm_disjunct(RHS,NC),
252 disjoin(LR,RR,Res,LHS,NC,WF),
253 create_wfwd(WF,WDE,WDV,WFD),
254 b_check_boolean_expression1(LHS,LocalState,State,WFD,LR,Ai,Aii),
255 propagagate_wfwd(WDE,WDV,GuardFlag,LR,pred_true),
256 b_check_boolean_expression0(pred_false,GuardFlag,NC,LocalState,State,WF,RR,Aii,Ao).
257 b_check_boolean_expression2(let_predicate(Ids,AssignmentExprs,Pred),_Info,LocalState,State,WFD,Res,Ai,Ao) :- !,
258 wd_set_up_localstate_for_let(Ids,AssignmentExprs,LocalState,State,LetState,WFD),
259 Ao=Ai, % anything cached inside the LET may depend on Ids and should not be reused outside of LET, see test 2397
260 empty_avl(InnerAi), % we can only reuse predicates inside if Ids are fresh, see test 2398
261 b_check_boolean_expression1(Pred,LetState,State,WFD,Res,InnerAi,_).
262 b_check_boolean_expression2(lazy_let_pred(Id,AssignmentExpr,Pred),_I,LocalState,State,wfwd(WF,WDE,WDV,_),Res,Ai,Ao) :- !,
263 set_up_localstate([Id],[(Trigger,IdValue)],LocalState,LetState),
264 b_interpreter:lazy_compute_expression(Trigger,AssignmentExpr,LocalState,State,IdValue,WF,Ai),
265 create_wfwd(WF,WDE,WDV,WFD),
266 b_check_boolean_expression1(Pred,LetState,State,WFD,Res,Ai,Ao). % Lazy lets always unique, we can pass Ai
267 b_check_boolean_expression2(lazy_lookup_pred(Id),Info,LocalState,_State,WFD,Res,Ai,Ao) :- !, Ai=Ao,
268 store:lookup_value_for_existing_id(Id,LocalState,(Trigger,Value)), % should normally only occur in LocalState; value introduced by lazy_let
269 wd_delay(((Trigger,Value) = (pred_true,Res)),
270 Res,b(lazy_lookup_pred(Id),pred,Info),WFD).
271 b_check_boolean_expression2(value(V),_Info,_LocalState,_State,_WFD,Res,Ai,Ao) :- !, % this can occur when lazy_lookup_pred gets compiled by b_compiler
272 Res=V,Ai=Ao.
273 b_check_boolean_expression2(not_equal(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao) :- !,
274 (negate_equal_false(LHS,RHS,LHS1,RHS1)
275 -> /* X/=FALSE equivalent to X=TRUE */
276 b_check_boolean_expression3_pos(equal(LHS1,RHS1),Info,LocalState,State,WFD,Res,Ai,Ao)
277 ? ; b_check_boolean_expression3_neg(equal(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao)
278 ).
279 b_check_boolean_expression2(equal(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao) :- !,
280 (negate_equal_false(LHS,RHS,LHS1,RHS1)
281 -> RHS1=b(boolean_true,boolean,[]), /* X/=FALSE equivalent to X=TRUE */
282 b_check_boolean_expression3_neg(equal(LHS1,RHS1),Info,LocalState,State,WFD,Res,Ai,Ao)
283 ? ; b_check_boolean_expression3_pos(equal(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao)
284 ).
285 b_check_boolean_expression2(not_member(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao) :- !,
286 b_check_boolean_expression3_neg(member(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao).
287 b_check_boolean_expression2(not_subset(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao) :- !,
288 b_check_boolean_expression3_neg(subset(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao).
289 b_check_boolean_expression2(not_subset_strict(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao) :- !,
290 b_check_boolean_expression3_neg(subset_strict(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao).
291 b_check_boolean_expression2(greater(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao) :- !,
292 b_check_boolean_expression3_pos(less(RHS,LHS),Info,LocalState,State,WFD,Res,Ai,Ao).
293 b_check_boolean_expression2(greater_equal(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao) :- !,
294 b_check_boolean_expression3_neg(less(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao).
295 b_check_boolean_expression2(less_equal(LHS,RHS),Info,LocalState,State,WFD,Res,Ai,Ao) :- !,
296 b_check_boolean_expression3_neg(less(RHS,LHS),Info,LocalState,State,WFD,Res,Ai,Ao).
297 b_check_boolean_expression2(Pred,Infos,LocalState,State,WFD,Res,Ai,Ao) :-
298 ? b_check_boolean_expression3_pos(Pred,Infos,LocalState,State,WFD,Res,Ai,Ao).
299
300
301 % check whether one of the two arguments is FALSE
302 % translate X/=FALSE -> X=TRUE for normalisation purposes
303 negate_equal_false(b(boolean_false,boolean,_),E,E,b(boolean_true,boolean,[])).
304 negate_equal_false(E,b(boolean_false,boolean,_),E,b(boolean_true,boolean,[])).
305
306 b_check_boolean_expression3_pos(Pred,Infos,LocalState,State,WFD,Res,Ai,Ao) :-
307 %print(register_predicate(WFD)),nl,portray_avl(Ai),nl,
308 register_predicate_wfd(WFD,Pred,Infos,Res,Reused,Ai,Ai2),
309 (Reused=true
310 -> Ao=Ai2 % , print(reused_pred(Res,WFD,Infos)),nl
311 ? ; b_check_boolean_expression4(Pred,Infos,LocalState,State,WFD,Ok,Res),
312 (Ok=ok_to_store -> Ao=Ai2 ; Ao=Ai)
313 ).
314
315 b_check_boolean_expression3_neg(Pred,Infos,LocalState,State,WFD,NegRes,Ai,Ao) :-
316 %print(register_neg_predicate(WFD,Infos,Pred)),nl,
317 register_predicate_wfd(WFD,Pred,Infos,Res,Reused,Ai,Ai2), negate(Res,NegRes),
318 (Reused=true
319 -> Ao=Ai2
320 ? ; b_check_boolean_expression4(Pred,Infos,LocalState,State,WFD,Ok,Res),
321 (Ok=ok_to_store -> Ao=Ai2 ; Ao=Ai)
322 ).
323
324 % Register a new predicate in the AVL tree if either forced or non-WD condition inside
325 register_predicate_wfd(_,_,_,_,false,Ai,Ao) :- Ai = no_avl,!, Ai=Ao.
326 register_predicate_wfd(_,_Pred,_Infos,_,false,Ai,Ao) :-
327 preferences:preference(use_common_subexpression_elimination,true),
328 preferences:preference(use_common_subexpression_also_for_predicates,true),
329 preferences:preference(disprover_mode,false), % there are still a few things detected here that CSE does not detect (=FALSE,=TRUE,...)
330 !, % CSE already statically detects these ( all of the time !?)
331 Ai=Ao.
332 register_predicate_wfd(WFWD,Pred,Infos,PredTruthVar,Reused,Ai,Ao) :-
333 get_wd(WFWD,A,B),
334 (A==B -> register_predicate_aux(Pred,PredTruthVar,Reused,Ai,Ao) % it will be evaluated; we can share it
335 ; nonmember(contains_wd_condition,Infos) -> register_predicate_aux(Pred,PredTruthVar,Reused,Ai,Ao)
336 % Pred is not guaranteed to be evaluated: WD-condition + not in forced WFD context (beware of exists!)
337 ; register_predicate_aux(Pred,PredTruthVar,true,Ai,Ao) -> Reused=true % the predicate is already stored
338 % Note: as predicate is already stored; no problem with WD; relevant for test 1959
339 ; Ai=Ao, Reused=false % not guaranteed to be evaluated: WD-condition + not in forced WFD context
340 % ,print('NOT REGISTERING: '), translate:print_bexpr(Pred),nl
341 ).
342
343 :- assert_must_succeed((E=empty, %avl:empty_avl(E),
344 A=b(value(_VAR),integer,[]),
345 b_interpreter_check:register_predicate(equal(A,A),[],pred_true,Reused,E,A1),
346 Reused==false, A1==empty)). % ensure we do not store non-var expressions
347 :- assert_must_succeed((E=empty, %avl:empty_avl(E),
348 A=b(value(int(1)),integer,[]),
349 b_interpreter_check:register_predicate(equal(A,A),[],pred_true,Reused,E,A1),
350 Reused==false, A1 \= empty, B=b(value(_),integer,[]),
351 b_interpreter_check:register_predicate(equal(B,B),[],pred_true,Reuse2,A1,A2),
352 Reuse2==false, A2==A1)). % ensure we do not look up non-var expressions
353 :- assert_must_succeed((E=empty, %avl:empty_avl(E),
354 A=b(value(int(1)),integer,[]),
355 b_interpreter_check:register_predicate(equal(A,A),[info1],pred_true,Reused,E,A1),
356 Reused==false,
357 b_interpreter_check:register_predicate(equal(A,A),[info2],pred_true,Reuse2,A1,A2),
358 Reuse2==true, A2==A1)). % ensure registering works
359
360 % Register a new predicate in the AVL tree (for outside callers like b_interpreter.pl)
361 register_predicate(Pred,_Infos,NegPredTruthVar,Reused,Ai,Ao) :- negate_pred(Pred,NegPred),!,
362 %print(negating),nl,
363 negate(PredTruthVar,NegPredTruthVar),
364 register_predicate_aux(NegPred,PredTruthVar,Reused,Ai,Ao).
365 register_predicate(Pred,_Infos,PredTruthVar,Reused,Ai,Ao) :-
366 register_predicate_aux(Pred,PredTruthVar,Reused,Ai,Ao).
367
368 negate_typed_pred(b(A,pred,_),NegA) :- negate_pred(A,NegA).
369 negate_pred(equal(A,B),equal(AA,TRUE)) :- negate_equal_false(A,B,AA,TRUE).
370 negate_pred(not_equal(A,B),equal(A,B)). % TO DO: we should check negate_equal_false
371 negate_pred(not_member(A,B),member(A,B)).
372 negate_pred(not_subset(A,B),subset(A,B)).
373 negate_pred(not_subset_strict(A,B),subset_strict(A,B)).
374 negate_pred(greater_equal(A,B),less(A,B)).
375 negate_pred(less_equal(A,B),less(B,A)).
376 negate_pred(negation(b(A,pred,_)),A).
377
378 % to do: detect convert_bool(Pred) = X and register Pred?
379 register_predicate_aux(Pred,_PredTruthVar,Reused,Ai,Ao) :- do_not_store_pred(Pred),
380 !,
381 Ai=Ao, Reused=false.
382 register_predicate_aux(Pred,PredTruthVar,Reused,Ai,Ao) :- check_pred_truth_var(PredTruthVar),
383 norm_pred_check(Pred,NPred), % We could store this information in the info field computed by ast_cleanup ?
384 (%too_simple(NPred) -> Reused=false, Ao=Ai ; %% even for simple equalities it actually pays off !
385 reuse_predicate(NPred,Var,Ai)
386 -> % nl,print(reusing(NPred,Var)),nl, %%
387 PredTruthVar=Var,Ao=Ai, Reused=true
388 ; add_predicate(NPred,PredTruthVar,Ai,Ao), Reused=false
389 %,nl,print(not_reusing(NPred)),nl
390 ).
391
392 check_pred_truth_var(X) :- var(X),!.
393 check_pred_truth_var(pred_true) :- !.
394 check_pred_truth_var(pred_false) :- !.
395 check_pred_truth_var(X) :- add_internal_error('Illegal Predicate Truth Value: ',check_pred_truth_var(X)).
396
397 :- use_module(kernel_tools,[ground_bexpr/1]).
398 do_not_store_pred(external_pred_call(_P,_)) :- !. % expcept maybe LESS, CHOOSE,... we could check performs_io
399 do_not_store_pred(B) :-
400 (ground_bexpr(b(B,pred,[]))
401 % TO DO: improve performance: marking bexpr with potential non-ground value(.) terms inside
402 % maybe avoid registering predicates with very large values inside
403 % avoid registering predicate if it is the only one in a closure
404 -> fail
405 ; true %print('-> Not storing: '),translate:print_bexpr(b(B,pred,[])),nl
406 ).
407
408
409 % Quantifier Expansion
410
411 b_check_forall_wf(Parameters,LHS,RHS,Info,LocalState,State,WF,PredRes) :-
412 create_wfwd_needed(WF,WFD), % we expect it to be in a context where the value will be needed
413 b_check_forall_wfwd(Parameters,LHS,RHS,Info,LocalState,State,WFD,PredRes).
414 b_check_forall_wfwd(_Parameters,LHS,RHS,_Info,_LocalState,_State,_WFD,PredRes) :-
415 (is_falsity(LHS) ; is_truth(RHS)),!, % quantifier always true
416 PredRes = pred_true.
417 b_check_forall_wfwd(Parameters,LHS,RHS,Info,LocalState,State,WFD,PredRes) :-
418 ? small_quantifier_cardinality(Parameters,LHS,LHS1,LHSRest),
419 %print(expand(forall(Parameters))),nl,
420 expand_quantifier(Parameters,LHS1,List,forall,Info), %print(List),nl,
421 Body = b(implication(LHSRest,RHS),pred,Info),% translate:print_bexpr(Body),nl,
422 get_wf(WFD,WF),
423 check_expanded_forall_quantifier(List,Body, LocalState, State,WF,WFD,PredRes).
424 % TO DO: if not small_quantifier_cardinality: b_check_boolean_expression4_delay
425
426
427 b_check_exists_wf(Parameters,Body,Info,LocalState,State,WF,PredRes) :-
428 create_wfwd_needed(WF,WFD), % we expect it to be in a context where the value will be needed
429 b_check_exists_wfwd(Parameters,Body,Info,LocalState,State,WFD,_,PredRes).
430 b_check_exists_wfwd(Parameters,Body,Info,LocalState,State,WFD,ok_to_store,PredRes) :-
431 % could be generalised to take into consideration domain as restricted by Body
432 ? small_quantifier_cardinality(Parameters,Body,LHS,RHS),!,
433 % print(expanding_check_exists(_Card,Parameters)),nl, % portray_waitflags(WF),
434 expand_quantifier(Parameters,LHS,List,exists,Info),
435 % now compute a priority for the disjunction based on the number of case splits
436 % relevant for tests 1358, 1746
437 length(List,Len), % Note: if Len=1: the body must be true; no disjoin will be set up
438 get_pow2_binary_choice_priority(Len,Prio),
439 % if Len=2 -> we actually have just two possibilities T,_ and F,T; but we want Prio to start at 4 ?
440 % if Len=3 -> we have T,_,_ ; F,T,_ ; F,F,T less possibilites if disjoin enumerated from left-to-right; TO DO: should we lower the priority taking this into account ?
441 % TO DO: maybe we could directly set up an n-ary disjoin predicate;
442 % if one disjunct true; remove case-splits on other disjuncts
443 get_wf(WFD,WF),
444 check_expanded_exists_quantifier(List,Prio,RHS, LocalState, State,WF,WFD,PredRes).
445 b_check_exists_wfwd(Parameters,Body,Infos,LocalState,State,wfwd(WF,WDE,WDV,ContextInfo),do_not_store,PredRes) :-
446 % the above reification has not worked; we now "pretend" that reification worked
447 % and introduce a delayed choice point
448 % do_not_store means that the predicate result should not be re-used somewhere else, because
449 % as the predicate evaluation is delayed it may later not be needed and not evaluated, cf test 2404
450 ContextInfo \= outer_wfwd_context, % at the outer-level interpreter expects reification succeeds only if
451 % at least top-level operator was reified deterministically,
452 % important for tests 1074, 1338, 1358, 1915 with this clause enabled
453 % test 305 #x.(x + x = 1000) now works, but not 1739 (timeout)
454 reify_inner_exists_non_deterministically, % hence we currently only use it in data validation mode
455 % here we enumerate reification variables with a much lower priority (data driven enumeration)
456 % (see get_binary_choice_wait_flag_exp_backoff)
457 % this clause relevant for 0323/CCSL/TYPES_AUTORISES_RVF3_GEN__MRGA.mch
458 Pred = exists(Parameters,Body),
459 perfmessage(reify,reifiying_inner_exists_non_deterministically(Parameters),Infos),
460 b_check_boolean_expression4_delay(WDE,WDV,Pred,Infos,LocalState,State,WF,PredRes).
461
462 % true if we allow reification of exists which cannot be expanded
463 % by delayed non-det enumeration (of pred_false, pred_true) if exists is not at top-level
464 reify_inner_exists_non_deterministically :- preferences:preference(data_validation_mode,true).
465
466
467 :- use_module(b_enumerate, [b_tighter_enumerate_values_in_ctxt/3]).
468 expand_quantifier(Parameters,Pred,ListOfNewLocalStates,QuantKind,Span) :-
469 % at the moment LS,State not really needed; only necessary if non-compiled Pred can be used
470 % also: feeding in any non-bound variables in LocalState or State would cause problems in findall !
471 findall(ParLocalState,
472 (b_interpreter:set_up_typed_localstate(Parameters,ParaValues,ParamTypedVals,
473 [],ParLocalState,all_solutions),
474 kernel_waitflags:init_wait_flags_with_call_stack(WF,
475 [quantifier_call(QuantKind,Parameters,ParaValues,Span)]),
476 b_test_boolean_expression(Pred,[],ParLocalState,WF),
477 b_tighter_enumerate_values_in_ctxt(ParamTypedVals,Pred,WF),
478 kernel_waitflags:ground_wait_flags(WF)),
479 ListOfNewLocalStates).
480
481 % a version which ensures that we have unique solutions of the bindings
482 expand_quantifier_normalised(Parameters,Pred,ListOfNewLocalStates,QuantKind,Span) :-
483 expand_quantifier(Parameters,Pred,List,QuantKind,Span),
484 normalise_local_states(List,NList),
485 sort(NList,ListOfNewLocalStates). % will remove duplicates
486
487 normalise_local_states([],[]).
488 normalise_local_states([State|T],[NS|NT]) :-
489 convert_bindings_to_avl(State,NS),
490 normalise_local_states(T,NT).
491
492 :- use_module(custom_explicit_sets,[convert_to_avl/2]).
493 convert_bindings_to_avl([],[]).
494 convert_bindings_to_avl([bind(Var,Val)|T],[bind(Var,NVal)|NT]) :-
495 (convert_to_avl(Val,NVal) -> true ; add_internal_error('Cannot normalise:',Val),fail),
496 convert_bindings_to_avl(T,NT).
497
498 check_expanded_forall_quantifier([], _Body, _LS, _State,_WF,_WFD,Res) :-
499 Res=pred_true.
500 check_expanded_forall_quantifier([LS1|TLS], Body, LS, State,WF,WFD,Res) :-
501 conjoin(Res1,TRes,Res,Body,Body,WF),
502 empty_avl(InnerAi), % TO DO: maybe use no_avl ?
503 append(LS1,LS,InnerLS),
504 % Note: we do not need to guard against wd-definition from other instances inside a quantified expression
505 % either all conjuncts can be evaluated or none
506 % print(expand_forall(WFD)), translate:print_bexpr(Body),nl,
507 b_check_boolean_expression1(Body,InnerLS,State,WFD,Res1,InnerAi,_Aii),
508 %instantiate_wfwd_result(WDE,WDV,Res1),
509 check_expanded_forall_quantifier(TLS,Body,LS,State,WF,WFD,TRes).
510
511 /*
512 :- block instantiate_wfwd_result(?,-,-).
513 % instantiate a boolean variable in case it is no longer needed and not set by something else
514 instantiate_wfwd_result(WDE,WDV,Res) :-
515 (nonvar(Res) -> true
516 ; WDE==WDV -> true
517 ; Res = pred_false). */
518
519 check_expanded_exists_quantifier([], _, _Body, _LS, _State,_WF,_WFD,Res) :-
520 Res=pred_false.
521 check_expanded_exists_quantifier([LS1|TLS], Priority, Body, LS, State,WF,WFD,Res) :-
522 (TLS = [] -> Res=Res1
523 ; disjoin(Res1,TRes,Res,priority(Priority),priority(Priority),WF)), % was using Body instead of priority(Priority)
524 empty_avl(InnerAi), % TO DO: maybe use no_avl ?
525 append(LS1,LS,InnerLS),
526 b_check_boolean_expression1(Body,InnerLS,State,WFD,Res1,InnerAi,_Aii),
527 % Note: we do not need to guard against wd-definition from other instances inside a quantified expression
528 check_expanded_exists_quantifier(TLS,Priority,Body,LS,State,WF,WFD,TRes).
529
530
531 :- use_module(library(lists),[maplist/4]).
532 % try and convert a closure into a list of 0/1 variables for each potential element
533 reify_closure_with_small_cardinality(P,T,Body, WF,ReifiedList) :- %print(try(P)),nl,
534 maplist(create_typed_id,P,T,Parameters),
535 small_quantifier_cardinality(Parameters,Body,LHS,RHS,350,25000), % TO DO: how to choose these parameters ?
536 % for card({x|x:1..n & x mod 3=0})=c & n=24000 -> 340 ms with reification; 320 ms without; n=74000 : 1020 ms without, 1160 with reification
537 % but there is a big difference for card({x|x:1..n & x mod 3=0 & x<10}) with n=74000 : 0 ms without reification, 1580 with; n=500: 20 ms with reification; n=250: 10 ms with reification
538 expand_quantifier_normalised(Parameters,LHS,List,comprehension_set,Body),
539 % important to normalise and have unique solutions for cardinality reification,
540 % see tests 639, 640 for card(POW(SS)-{{}}) with SS full set
541 create_wfwd_needed(WF,WFD), % is this ok ??
542 ? reifiy_list(List,RHS,WFD,ReifiedList).
543
544
545
546 reifiy_list([], _Body,_WFD,[]).
547 reifiy_list([LS1|TLS], Body,WFD,[Res_01|TRes]) :-
548 empty_avl(InnerAi),
549 ? b_check_boolean_expression1(Body,LS1,[],WFD,Res1_pred,InnerAi,_Aii),
550 prop_pred_01(Res1_pred,Res_01),
551 %format(' reify ~w : ~w~n',[Res_01,LS1]),
552 % Note: we do not need to guard against wd-definition from other instances inside a quantified expression
553 reifiy_list(TLS,Body,WFD,TRes).
554
555
556
557 :- use_module(library(lists),[select/3]).
558
559 % check if Body produces a small cardinality for the paramters Par
560 % if yes: the predicates constraining Par are put into LHS, the rest into RHS
561 % also: LHS must ensure that ground values are produced for Par and that we can enumerate with a separate WF (in expand_quantifier)
562 small_quantifier_cardinality(Par,Body,LHS,RHS) :-
563 %preferences:preference(solver_strength,SS), NL is 10+SS, SMTL is 40+SS,
564 NL=10,SMTL=40,
565 ? small_quantifier_cardinality(Par,Body,LHS,RHS,NL,SMTL). % was 10,35; raising it to 10,41 makes tests 1441, 1442 fail due to expansion of exists; raising it to 16,50 makes test 1112 fail; TO DO: investigate
566 small_quantifier_cardinality(Par,Body,LHS,RHS,NormalLimit,SMTLimit) :-
567 conjunction_to_list(Body,BodyList),
568 def_get_texpr_ids(Par,AllParas),
569 ? small_quantifier_cardinality_aux(Par,AllParas,BodyList,_UpBoundOnSize,LLHS,LRHS,NormalLimit,SMTLimit),
570 conjunct_predicates_with_pos_info(LLHS,LHS),
571 conjunct_predicates_with_pos_info(LRHS,RHS).
572
573 is_membership_or_eq(b(P,pred,Info),TLHS,RHS,Info) :- is_mem_aux(P,TLHS,RHS).
574 is_mem_aux(member(TLHS,b(RHS,_,_)),TLHS,RHS).
575 %is_mem_aux(subset(SONE,b(RHS,_,_)),TLHS,RHS) :- singleton_set_extension(SONE,TLHS).
576 is_mem_aux(equal(TLHS,b(value(V),_,_)),TLHS,value([V])). % x = V is the same as x:{V}
577 %TODO: use :- use_module(bsyntaxtree,[is_membership_or_equality/3]). % will create set_extension
578
579 % do not rely on size for anything: it is just an upper bound on the size; the actual size could be smaller
580 small_quantifier_cardinality_aux([],_,Body,Size,LHS,RHS,_,_) :- !,
581 LHS=[],RHS=Body,Size=1.
582 small_quantifier_cardinality_aux(Parameters,AllParas,[LHS|TBody],FullSize,FullLHS,FullRHS,NormalLimit,SMTLimit) :-
583 is_membership_or_eq(LHS,SID,MemRHS,Info),
584 ? constrains_ID(SID,AllParas,Parameters,RestParameters,SkelVal,SkelToUnify,BindList), % we could check RestParameters /= Parameters
585 % TO DO: we could also allow parameter to be constrained twice x: 1..100 & x: {...} ?
586 (is_small_set(MemRHS,Size,NormalLimit,SMTLimit,Info) % we have a small set of ground values: we can evaluate LHS to expand the quantifier/set_comprehension for the parameters occuring in LHS
587 -> FullLHS = [LHS|RestLHS],FullRHS = RestRHS
588 ; infer_ground_membership(MemRHS,SID,SkelVal,SkelToUnify,BindList,NormalLimit,SMTLimit, Size,InferredLHS) ->
589 % we have inferred a superset InferredLHS of MemRHS which is small and known
590 FullLHS = [InferredLHS|RestLHS], % we have added an inferred membership constraint
591 FullRHS = [LHS|RestRHS]), % the original membership LHS still needs to be checked later, after expansion of the quantifier
592 !,
593 % we select LHS to be included in FullLHS and mark parameter ID as constrained
594 small_quantifier_cardinality_aux(RestParameters,AllParas,TBody,RestSize,RestLHS,RestRHS,NormalLimit,SMTLimit),
595 FullSize is Size*RestSize,
596 is_small_size(FullSize,NormalLimit,SMTLimit, Parameters).
597 small_quantifier_cardinality_aux(Parameters,AllParas,[H|Rest],FullSize,RestLHS,[H|RestRHS],NormalLimit,SMTLimit) :- !,
598 % skip the conjunct H
599 ? small_quantifier_cardinality_aux(Parameters,AllParas,Rest,FullSize,RestLHS,RestRHS,NormalLimit,SMTLimit).
600 small_quantifier_cardinality_aux(Parameters,_AllParas,Body,ParCard,[],Body,NormalLimit,SMTLimit) :-
601 % if the remaining parameter type cardinality is small: just use "truth" as body; will instantiate parameters
602 ? b_interpreter:parameter_list_cardinality(Parameters,ParCard),
603 is_small_size(ParCard,NormalLimit,SMTLimit, Parameters).
604
605 % try and extract a ground superset (InferredLHS) of the RHS (ID:RHS) which constrains ID
606 infer_ground_membership(value(List),SID,SkelVal,SkelToUnify,BindList,NormalLimit,SMTLimit, Size,InferredLHS) :-
607 !,
608 % the List is probably not ground; let's try and see if we can extract ground matches for the parameters at least; see test 1627 (s=1..20 & x: s-->BOOL & card({t|t|->TRUE:x}):18..19)
609 extract_bind_list(BindList,LHSTerm,RHSValue),
610 has_bounded_ground_matches(List,SkelVal,SkelToUnify,RHSValue,MatchedValues,1,NrMatches), % TO DO: provide SMTLimit as upper limit
611 NrMatches = Size,
612 is_small_size(Size,NormalLimit,SMTLimit, SID),
613 get_texpr_type(LHSTerm,LHSTermType),
614 InferredLHS = b(member(LHSTerm,b(value(MatchedValues),set(LHSTermType),[])),pred,[generated]).
615 infer_ground_membership(Set,SID,SkelVal,SkelToUnify,BindList,NormalLimit,SMTLimit,Size,InferredLHS) :-
616 ? superset(Set,SuperSet),
617 % e.g., if ID: {1} /\ x -> InferredLHS = {1} and we will add ID : {1} to the LHS of the quantifier and keep ID : {1} /\ x as the RHS
618 infer_ground_mem_aux(SuperSet,SID,SkelVal,SkelToUnify,BindList,NormalLimit,SMTLimit,Size,InferredLHS).
619
620
621 superset(intersection(A,B),Set) :- (Set=A ; Set=B). % Set /\ X <: Set
622 superset(set_subtraction(Set,_),Set). % Set \ X <: Set
623 superset(domain_restriction(_,Set),Set). % X <| Set <: Set
624 superset(domain_subtraction(_,Set),Set). % X <<| Set <: Set
625 superset(range_subtraction(Set,_),Set). % Set |> X <: Set
626 superset(range_restriction(Set,_),Set). % Set |>> X <: Set
627
628 infer_ground_mem_aux(b(Set,T,I),SID,SkelVal,SkelToUnify,BindList,NormalLimit,SMTLimit, Size,InferredLHS) :-
629 (is_small_set(Set,Size,NormalLimit,SMTLimit,I)
630 -> InferredLHS = b(member(SID,b(Set,T,I)),pred,[generated])
631 ; infer_ground_membership(Set,SID,SkelVal,SkelToUnify,BindList,NormalLimit,SMTLimit, Size,InferredLHS)).
632
633 :- use_module(bsyntaxtree, [create_couple/3]).
634 % extract result of constrains_ID BindList into an Expression-Tuple for new membership constraint and a value that will be put into a value(List)
635 extract_bind_list([TID/Val],TID,Val) :- !.
636 extract_bind_list([TID/Val|BList],Couple,(Val,RestVal)) :-
637 create_couple(TID,RestExpr,Couple),
638 extract_bind_list(BList,RestExpr,RestVal).
639
640 :- use_module(kernel_tools,[can_match/2]).
641 % check if we can find a bounded / fixed number of ground matches for Skeleton Value in List
642 has_bounded_ground_matches(Var,_,_,_, _,_,_) :- var(Var),!.
643 has_bounded_ground_matches([],_SkelVal,_SkelToUnify,_,[],LenAcc,LenAcc).
644 has_bounded_ground_matches([H|T],SkelVal,SkelToUnify,ValueToStore,Matches,LenAcc,LenRes) :-
645 (can_match(H,SkelVal) -> copy_term((SkelToUnify,ValueToStore),(H,HValueToStore)),
646 ground_value(HValueToStore),
647 Matches = [HValueToStore|MT],
648 A1 is LenAcc+1 ; Matches=MT,A1=LenAcc),
649 has_bounded_ground_matches(T,SkelVal,SkelToUnify,ValueToStore,MT,A1,LenRes).
650
651 % check if the Expr contains ID, i.e., matching Expr with an element of a set will also instantiate and determine TID
652 % TO DO: also accept other patterns: records, sets?,....
653
654 % SkeletonToUnify: a skeleton that can be used to unify with any value and extracts value in BindList
655 constrains_ID(b(E,_,_),AllParas,Parameters,RestParameters,SkeletonValue,SkeletonToUnify,BindList) :-
656 ? constrains_ID_aux(E,AllParas,Parameters,RestParameters,SkeletonValue,SkeletonToUnify,BindList).
657 constrains_ID_aux(couple(A,B),AllParas,Parameters,RestParameters,(V1,V2),(Skel1,Skel2),Bind) :-
658 constrains_ID(A,AllParas,Parameters,Rest1,V1,Skel1,Bind1),
659 constrains_ID(B,AllParas,Rest1,RestParameters,V2,Skel2,Bind2),
660 append(Bind1,Bind2,Bind). % TO DO: use DCGs
661 constrains_ID_aux(rec(F),AllParas,Parameters,RestParameters,record(SkelV),record(SkelU),Bind) :-
662 ? constrains_ID_fields(F,AllParas,Parameters,RestParameters,SkelV,SkelU,Bind).
663 constrains_ID_aux(identifier(ID),AllParas,Parameters,RestParameters,_SkelV,SkelU,Bind) :-
664 % TO DO: allow same id to appear multiple times in expression + allow to re-use parameters in another conjunct
665 ? (select(TID,Parameters,RestParameters), get_texpr_id(TID,ID)
666 -> Bind = [TID/SkelU]
667 ? ; member(ID,AllParas), % we have already used/bound ID; we will use first occurence for skeleton/bind; this one is simply ignored
668 % TO DO: we could try and see whether using the second occurence gives a better result
669 RestParameters=Parameters, Bind=[]
670 ).
671 constrains_ID_aux(value(V),_AllParas,P,P,V,_,[]) :- ground_value(V).% if not ground value we may not be able to compute all possible values for ID
672 constrains_ID_aux(boolean_true,_AllParas,P,P,pred_true,pred_true,[]). % needed ?? everything is compiled anway ?
673 constrains_ID_aux(boolean_false,_AllParas,P,P,pred_false,pred_false,[]). % needed ?? everything is compiled anway ?
674 % Below: allow any other expression as long as it only uses AllParas
675 % e.g. x+1 in : s: 1..20 --> (BOOL*(1..20)) & card({x|x|->(TRUE|->x):s})=10 & card({x|x|->(FALSE|->x+1):s})=10
676 constrains_ID_aux(add(A,B),AllParas,P,P,_,_,[]) :- % basically allow other Parameters or ground values
677 ? constrains_ID(A,AllParas,[],[],_,_,_), constrains_ID(B,AllParas,[],[],_,_,_).
678 constrains_ID_aux(minus(A,B),AllParas,P,P,_,_,[]) :- % TO DO: allow other binary/unary operators ?
679 constrains_ID(A,AllParas,[],[],_,_,_), constrains_ID(B,AllParas,[],[],_,_,_).
680 %constrains_ID_aux(Other,All,P,P,_,_,[]) :- print(other(Other)),nl,fail.
681
682 constrains_ID_fields([],_AllParas,P,P,[],[],[]).
683 constrains_ID_fields([field(Field,Val)|TF],AllParas,Parameters,RestParameters,
684 [field(Field,SkelVal)|TSkelV],[field(Field,SkelUnify)|TSkelU],Bind) :-
685 ? constrains_ID(Val,AllParas,Parameters,Rest1,SkelVal,SkelUnify,Bind1),
686 ? constrains_ID_fields(TF,AllParas,Rest1,RestParameters,TSkelV,TSkelU,Bind2),
687 append(Bind1,Bind2,Bind). % TO DO: use DCGs
688
689 :- use_module(custom_explicit_sets,[efficient_card_for_set/3]).
690
691
692 is_small_set(Val,Size,NormalLimit,SMTLimit,SrcLoc) :-
693 get_small_set_size(Val,Size),
694 is_small_size(Size,NormalLimit,SMTLimit,SrcLoc).
695
696 :- use_module(kernel_tools,[ground_value/1]).
697 get_small_set_size(value(S),Size) :- !,
698 ground_value(S), % otherwise we could have S=[X] and expand_quantifier will erroneously create multiple solutions for parameter=X
699 efficient_card_for_set(S,Size,C),
700 call(C).
701 get_small_set_size(interval(From,To),Size) :- !,
702 custom_explicit_sets:is_interval_with_integer_bounds(interval(From,To),Low,Up),
703 number(Low), number(Up),
704 (Low > Up -> Size = 1 ; Size is 1+Up-Low).
705 % we provide dom/ran here explicitly as this is often used {i|i:dom(f) ...}
706 get_small_set_size(bool_set,Size) :- !, Size=2.
707 get_small_set_size(domain(b(Val,_,_)),Size) :- !,
708 get_small_domain_set_size(Val,Size). % this is only an upper bound on the size !!
709 get_small_set_size(range(b(Val,_,_)),Size) :- !,
710 get_small_set_size(Val,Size). % this is only an upper bound on the size !!
711 get_small_set_size(cartesian_product(b(A,_,_),b(B,_,_)),Size) :- !,
712 get_small_set_size(A,SizeA), number(SizeA),
713 get_small_set_size(B,SizeB), number(SizeB),
714 kernel_objects:safe_mul(SizeA,SizeB,Size), number(Size).
715
716 %get_small_set_size(set_extension(L),Size,_,_,_) :- !, length(L,Size), is_small_size(Size). what if elements itself notknown ?? probably the case as otherwise this would have been compiled into a value
717 %get_small_set_size(sequence_extension(L),Size,_,_,_) :- !, length(L,Size), is_small_size(Size). ditto
718 %get_small_set_size(interval...
719 %get_small_set_size(domain(value([....]))...
720 % for this to work efficiently one should call b_compiler:compile on the predicates before sending them
721 % to b_interpreter_check; otherwise the sets will not yet be in value(_) form
722
723 % TO DO: the same for range or find more principled solution
724 %get_small_domain_set_size(value(S),Size) :- var(S), frozen(S,Frozen), print(var_value(S,Frozen)),nl,fail.
725 get_small_domain_set_size(value(Val),Size) :- nonvar(Val), Val=[H|T],!,
726 efficient_card_for_set([H|T],Size,C),
727 ground_domain([H|T]), % rather than requiring ground of entire list; we only require ground for domain (see test 1272)
728 call(C).
729 get_small_domain_set_size(Val,Size) :-
730 get_small_set_size(Val,Size).
731 ground_domain(V) :- var(V),!,fail.
732 ground_domain([]).
733 ground_domain([H|T]) :-
734 nonvar(H), H=(D,_),
735 ground_value(D), ground_domain(T).
736
737 :- use_module(performance_messages).
738 is_small_size(Size,NormalLimit,SMTLimit,SrcLoc) :- Size \= inf,
739 (Size < NormalLimit -> true
740 ; preferences:preference(use_smt_mode,true)
741 -> preference(solver_strength,SS),
742 (Size < SMTLimit + SS
743 -> true
744 ; perfmessage(reify,'Not reifiying quantifier, try increasing SOLVER_STRENGTH: ','>'(Size,'+'(SMTLimit,SS)),SrcLoc),
745 fail
746 )
747 ; perfmessage(reify,'Not reifiying quantifier, try setting SMT preference: ','>'(Size,limit(NormalLimit)),SrcLoc),
748 fail
749 ).
750
751 % -------------------------------------
752 % EXPRESSIONS
753
754 :- use_module(store,[set_up_localstate/4]).
755
756 wd_set_up_localstate_for_let(Ids,Exprs,LocalState,State,LetState,WFD) :-
757 set_up_localstate(Ids,Vars,LocalState,LetState),
758 wd_compute_let_expressions(Exprs,Vars,LetState,State,WFD).
759 wd_compute_let_expressions([],[],_,_,_).
760 wd_compute_let_expressions([Expr|RestExprs],[Var|RestVars],LocalState,State,WFD) :-
761 b_wd_compute_expression(Expr,LocalState,State,Value,WFD),
762 kernel_objects:equal_object_optimized(Var,Value,compute_let_expressions),
763 wd_compute_let_expressions(RestExprs,RestVars,LocalState,State,WFD).
764
765
766 % compute a Prolog list of expressions:
767 b_wd_compute_expressions([], _, _, [],_WFD).
768 b_wd_compute_expressions([EXPRsHd|EXPRsTl],LocalState,State,[ValHd|ValTl],WFD) :-
769 b_wd_compute_expression(EXPRsHd,LocalState,State,ValHd,WFD),
770 b_wd_compute_expressions(EXPRsTl,LocalState,State,ValTl,WFD).
771
772 % we have to avoid trying to compute certain expressions: evaluation can fail if not well-defined !
773 b_wd_compute_expression(Expr,LocalState,State,Value,wfwd(WF,WDE,WDV,_)) :- !,
774 (nonvar(WDV)
775 -> (WDE==WDV
776 -> % print('REQUIRED: '), translate:print_bexpr(Expr),nl,
777 ? if(b_compute_expression(Expr,LocalState,State,Value,WF), % TO DO: we could use fresh variable for Value
778 true,
779 (kernel_objects:unbound_value(Value), % we have a WD error, if nonvar it could be because we expect a wrong value
780 add_wd_error_span('Well-definedness error evaluating expression: ',Expr,span_predicate(Expr,LocalState,State),WF)
781 %Value = term(undefined),
782 )
783 )
784 ; instantiate_to_any_value(Value,Expr,WF))
785 ; always_wd_no_fail_nor_error(Expr)
786 -> % print('ALWAYS WD: '), translate:print_bexpr(Expr),nl, %
787 ? b_compute_expression(Expr,LocalState,State,Value,WF)
788 ; b_compiler:b_optimize(Expr,[],LocalState,State,CExpr,WF),
789 % try compiling; this will inline values and may make the expression well_defined; relevant for test 2013
790 (always_wd_no_fail_nor_error(CExpr)
791 % it is important that this computation cannot fail and cannot raise any errors
792 % an example showing this is :wde f=[2,4] & xx:1..3 & (xx=1 or f(xx-1)=4) with -p TRY_FIND_ABORT TRUE
793 % even though the whole expression is well-defined, the function call f(xx-1) does lead
794 % to an error with xx=1
795 -> b_compute_expression(CExpr,LocalState,State,Value,WF)
796 ; % print('DELAYING DUE TO WD: '), translate:print_bexpr(CExpr),nl, %%
797 b_compute_expression_delay(WDE,WDV, CExpr,LocalState,State,Value,WF)
798 )
799 ).
800 b_wd_compute_expression(Expr,LocalState,State,Value,WFD) :-
801 add_internal_error('Illegal WFD value: ', b_wd_compute_expression(Expr,LocalState,State,Value,WFD)),fail.
802
803
804 always_wd_no_fail_nor_error(Expr) :-
805 always_well_defined(Expr).
806 % should not use always_well_defined_or_disprover_mode or WD discharged information!
807
808 :- block b_compute_expression_delay(?,-, ?,?,?,?,?).
809 b_compute_expression_delay(WDE,WDV,Expr,LocalState,State,Value,WF) :-
810 (WDE==WDV
811 -> % print('WD Evaluation: '), print(WDE),print(' =?= '), print(WDV), print(' '),translate:print_bexpr(Expr),nl,
812 b_compute_expression(Expr,LocalState,State,Value,WF)
813 ; %print(instantiate_to_any_value(Value,Expr)),nl,
814 instantiate_to_any_value(Value,Expr,WF) % does not matter anyway; but there can be pending co-routines :-(
815 ).
816
817 % WARNING: the variable could be used in another context, where it is relevant !
818
819
820 wd_delay(WDCall,Res, Expr,wfwd(WF,WDExpected,WDV,_)) :-
821 (WDExpected==WDV -> call(WDCall) % WDV truth value on left is ok: we can evaluate
822 ; nonvar(WDV) -> instantiate_to_any_value(Res,Expr,WF) % truth value not ok; we do not need the value
823 ; always_wd_no_fail_nor_error(Expr) -> call(WDCall)
824 ; wd_delay_block(WDCall,Res,Expr,WDExpected,WDV,WF)).
825 :- block wd_delay_block(?,?,?,?,-,?).
826 wd_delay_block(WDCall,Res,Expr,WDExpected,WDV,WF) :-
827 (WDExpected==WDV -> call(WDCall)
828 ; instantiate_to_any_value(Res,Expr,WF)).
829
830
831 :- use_module(typing_tools,[any_value_for_type/2]).
832 :- use_module(kernel_tools,[ground_value_check/2]).
833 %instantiate_to_any_value(V,E,_) :- print(instantiate_to_any_value(V)),nl,translate:print_bexpr(E),nl,nl,fail.
834 instantiate_to_any_value(V,_,_) :- ground_value(V),!.
835 instantiate_to_any_value(V,b(_B,TYPE,_I),WF) :-
836 get_enumeration_finished_wait_flag(WF,EWF),
837 ground_value_check(V,GV),
838 blocking_any_value_for_type(EWF,GV,TYPE,V).
839
840 :- block blocking_any_value_for_type(-,-,?,?).
841 blocking_any_value_for_type(_,_,TYPE,V) :- any_value_for_type(TYPE,V). % , print(inst2(TYPE,V)),translate:print_bexpr(b(_B,TYPE,_I)),nl.
842
843
844 :- block propagagate_wfwd(-,?,?,-,?), propagagate_wfwd(?,-,?,-,?).
845 % propagate expected and actual value guarding left with actual predicate value obtained for left
846 propagagate_wfwd(WDE,WDV,Res,F1,F2) :-
847 (F1==F2 -> Res=F1 % then value of WDE does not matter at all; Res always the same
848 ; propagagate_wfwd2(WDE,WDV,Res,F1,F2)).
849
850 :- block propagagate_wfwd2(-,?,?,?,?), propagagate_wfwd2(?,-,?,?,?).
851 propagagate_wfwd2(WDE,WDV,Res,F1,F2) :- (WDE==WDV -> Res=F1 ; Res=F2).
852
853 :- use_module(external_functions,[external_fun_has_wd_condition/1]).
854 :- use_module(preferences).
855 b_check_boolean_expression4(exists(Parameters,Body),Info,LocalState,State,WFD,OkToStore,Res) :- !,
856 if(b_check_exists_wfwd(Parameters,Body,Info,LocalState,State,WFD,OkToStore,Res),true,
857 (perfmessagecall(reify,cannot_reify_exists(Parameters),translate:print_bexpr(Body),Body),
858 fail)).
859 b_check_boolean_expression4(Pred,Infos,LocalState,State,WFD,ok_to_store,Res) :-
860 ? b_check_boolean_expression4_ok(Pred,Infos,LocalState,State,WFD,Res).
861
862 b_check_boolean_expression4_ok(equal(LHS,RHS),_,LocalState,State,WFD,EqRes) :- !,
863 ? b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
864 b_wd_compute_expression(RHS,LocalState,State,RHValue,WFD),
865 get_texpr_type(LHS,Type), get_wf(WFD,WF),
866 equality_objects_with_type_wf(Type,LHValue,RHValue,EqRes,WF).
867 % we may need to improve not_member for closure to invert symbolic operators
868 b_check_boolean_expression4_ok(member(LHS,RHS),Info,LocalState,State,WFD,Res) :-
869 %member_check_should_be_reified(LHS,RHS), % no longer need this: symbolic operators will be kept as closures if large ?!
870 !,
871 get_texpr_expr(RHS,ERHS),
872 ? b_check_member_expression(ERHS,RHS,LHS,Info,LocalState,State,WFD,Res).
873 % TO DO: compile forall, exists + setup choice point if expansion fails + remove compile calls in b_interpreter
874 b_check_boolean_expression4_ok(forall(Parameters,LHS,RHS),Info,LocalState,State,WFD,Res) :- !,
875 ? if(b_check_forall_wfwd(Parameters,LHS,RHS,Info,LocalState,State,WFD,Res),true,
876 (perfmessagecall(reify,cannot_reify_forall(Parameters),translate:print_bexpr(LHS),LHS),
877 fail)).
878 b_check_boolean_expression4_ok(subset(LHS,RHS),_,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
879 ? b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
880 ? b_wd_compute_expression(RHS,LocalState,State,RHValue,WFD),
881 subset_test(LHValue,RHValue,Res,WF).
882 b_check_boolean_expression4_ok(subset_strict(LHS,RHS),_,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
883 b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
884 b_wd_compute_expression(RHS,LocalState,State,RHValue,WFD),
885 subset_strict_test(LHValue,RHValue,Res,WF).
886 b_check_boolean_expression4_ok(external_pred_call(FunName,Args),Info,LocalState,State,WFD,Res) :-
887 !,
888 (external_fun_has_wd_condition(FunName)
889 -> wd_delay_until_needed(WFD,b_check_external_pred_call(FunName,Args,Info,LocalState,State,WFD,Res))
890 ; b_check_external_pred_call(FunName,Args,Info,LocalState,State,WFD,Res)).
891 b_check_boolean_expression4_ok(freetype_case(FT,IsCase,Expr),_Infos,LocalState,State,WFD,Res) :- !,
892 b_wd_compute_expression(Expr,LocalState,State,freeval(FT,ActualCase,_A),WFD),
893 eq_atomic(IsCase,ActualCase,freeval_case,Res).
894 b_check_boolean_expression4_ok(finite(Expr),_Infos,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
895 b_wd_compute_expression(Expr,LocalState,State,ExprVal,WFD),
896 kernel_objects:test_finite_set_wf(ExprVal,Res,WF).
897 b_check_boolean_expression4_ok(partition(Expr,ListOfSets),_Infos,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
898 b_wd_compute_expression(Expr,LocalState,State,ExprVal,WFD),
899 b_wd_compute_expressions(ListOfSets,LocalState,State,PartitionList,WFD), % arg is a Prolog list, not a set
900 ? kernel_objects:test_partition_wf(ExprVal,PartitionList,Res,WF).
901 b_check_boolean_expression4_ok(Pred,_,LocalState,State,WFD,Res) :-
902 arithmetic_op(Pred,Op,LHS,RHS),!,
903 ? b_wd_compute_expression(LHS,LocalState,State,int(LHValue),WFD),
904 ? b_wd_compute_expression(RHS,LocalState,State,int(RHValue),WFD),
905 check_arithmetic_operator(Op,LHValue,RHValue,Res).
906 b_check_boolean_expression4_ok(Pred,_,LocalState,State,WFD,Res) :-
907 real_arithmetic_op(Pred,Op,LHS,RHS),!,
908 b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
909 b_wd_compute_expression(RHS,LocalState,State,RHValue,WFD),
910 get_wf(WFD,WF),
911 real_comp_wf(Op,LHValue,RHValue,Res,WF).
912 % TO DO ???: use idea of b_artihmetic_expression to avoid intermediate CLPFD variables
913 % TO DO: add other operators, ...
914 /* use_smt_mode = full is never set; at some point we should enable the following clause by default
915 b_check_boolean_expression4_ok(Pred,Infos,LocalState,State,wfwd(WF,WDE,WDV,ContextInfos),Res) :-
916 % preferences:preference(use_smt_mode,full), %% comment out to enable check testing of complicated predicates inside;
917 % caused slowdowns of cbtc/actions_cbtc.mch (test 1751); but no longer the case
918 functor(Pred,F,N),format('~n Cannot reify ~w/~w~n~n',[F,N]),fail,
919 %ContextInfo \= outer_wfwd_context,
920 b_check_boolean_expression4_delay(WDE,WDV,Pred,Infos,LocalState,State,WF,Res).
921 */
922
923 :- use_module(library(lists),[maplist/3]).
924 % TO DO: add member(,pow_subset, fin_subset) --> subset_test
925 % we could distribute RHS=union -> disjunction, LHS=intersection -> conjunction ,... ?
926 %b_check_member_expression(EHRS,_RHS,_LHS,_,_LocalState,_State,_WFD,_Res) :-
927 % print('member : '), print(EHRS),nl,fail.
928 %b_check_member_expression(union(A,B),LHS,_,LocalState,State,WFD,Res) :-
929 b_check_member_expression(pow_subset(RHS),_,LHS,_Info,LocalState,State,WFD,Res) :- !,
930 b_check_boolean_expression4_ok(subset(LHS,RHS),[],LocalState,State,WFD,Res).
931 %b_check_member_expression(NotContainingEmptySet,_TRHS,LHS,LocalState,State,WFD,Res) :-
932 % non_empty_set_version_of(NotContainingEmptySet,RHS_With_EmptySet),
933 % % translate x:seq1(RHS) -> x /= {} & x:seq(RHS), ...
934 % % this improves propagation, in particular in light of WD issues
935 % % TO DO: avoid computing LHS twice !
936 % !,
937 % get_texpr_type(LHS,LType),
938 % EMPTYVERSION = b(member(LHS,b(RHS_With_EmptySet,set(LType),[])),pred,[]),
939 % NOTEMPTYPRED = b(not_equal(LHS,b(empty_set,LType,[])),pred,[]),
940 % print('Translating : '), translate:print_bexpr(EMPTYVERSION), print(' & '), translate:print_bexpr(NOTEMPTYPRED),nl,
941 % empty_avl(Ai), Infos=[], % Infos not important for conjunction
942 % b_check_boolean_expression2(conjunct(EMPTYVERSION,NOTEMPTYPRED),Infos,LocalState,State,WFD,Res,Ai,_).
943 b_check_member_expression(interval(Low,Up),_,LHS,_Info,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
944 % to do: should we also match interval value closure
945 b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
946 b_wd_compute_expression(Low,LocalState,State,LowValue,WFD),
947 b_wd_compute_expression(Up,LocalState,State,UpValue,WFD),
948 kernel_objects:test_in_nat_range_wf(LHValue,LowValue,UpValue,Res,WF).
949 b_check_member_expression(set_extension(SetExt),_RHS,LHS,_Info,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
950 % rewrite x:{a,b,c} into x=a or x=b or x=c (is the opposite of rewrite_disjunct_to_member)
951 % The rewrite_disjunct_to_member is good when we know a membership to be true; the disjunct is better for reification
952 % Note: the problem is in particular when the result of the membership is not needed and an uninstantiated
953 % variable is used in the set extension, example y:dom(f) => (x:{f(y),0} or f(y)=0)
954 % print(mem_check_set_extension),print(' '),translate:print_bexpr(b(member(LHS,_RHS),pred,[])),nl,
955 b_wd_compute_expressions(SetExt,LocalState,State,SetExtValues,WFD),
956 b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
957 get_texpr_type(LHS,Type),
958 NewLHS = b(value(LHValue),Type,[]),
959 (ground_value(SetExtValues)
960 -> % better to use normal treatment, we can compute the entire set and translate it into an AVL tree
961 maplist(construct_value(Type),SetExtValues,Vals),
962 b_wd_compute_expression(b(set_extension(Vals),set(Type),[]),LocalState,State,RHValue,WFD),
963 membership_test_wf(RHValue,LHValue,Res,WF),
964 force_membership_test(Res,LHValue,RHValue,WF)
965 ; maplist(construct_equality(NewLHS,Type),SetExtValues,Disjuncts),
966 construct_norm_disjunct2(Disjuncts,NC,InfoNC),
967 empty_avl(Ai),
968 b_check_boolean_expression2(NC,InfoNC,LocalState,State,WFD,Res,Ai,_)
969 ).
970 b_check_member_expression(closure(Relation),_RHS,LHS,Info,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
971 b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
972 b_wd_compute_expression(Relation,LocalState,State,RelValue,WFD),
973 opt_push_wait_flag_call_stack_info(WF,b_operator_call(member,
974 [LHValue,b_operator(closure,[RelValue])],Info),WF2), % this is closure1
975 bsets_clp:in_closure1_membership_test_wf(LHValue,RelValue,Res,WF2).
976 b_check_member_expression(partial_function(Dom,Range),_RHS,LHS,Info,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
977 b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
978 b_wd_compute_expression(Dom,LocalState,State,DomValue,WFD),
979 b_wd_compute_expression(Range,LocalState,State,RangeValue,WFD),
980 opt_push_wait_flag_call_stack_info(WF,b_operator_call(member,
981 [LHValue,b_operator(partial_function,[DomValue,RangeValue])],Info),WF2),
982 bsets_clp:partial_function_test_wf(LHValue,DomValue,RangeValue,Res,WF2).
983 b_check_member_expression(total_function(Dom,Range),_RHS,LHS,Info,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
984 b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
985 b_wd_compute_expression(Dom,LocalState,State,DomValue,WFD),
986 b_wd_compute_expression(Range,LocalState,State,RangeValue,WFD),
987 opt_push_wait_flag_call_stack_info(WF,b_operator_call(member,
988 [LHValue,b_operator(total_function,[DomValue,RangeValue])],Info),WF2),
989 bsets_clp:total_function_test_wf(LHValue,DomValue,RangeValue,Res,WF2).
990 b_check_member_expression(partial_surjection(Dom,Range),_RHS,LHS,Info,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
991 b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
992 b_wd_compute_expression(Dom,LocalState,State,DomValue,WFD),
993 b_wd_compute_expression(Range,LocalState,State,RangeValue,WFD),
994 opt_push_wait_flag_call_stack_info(WF,b_operator_call(member,
995 [LHValue,b_operator(partial_surjection,[DomValue,RangeValue])],Info),WF2),
996 bsets_clp:partial_surjection_test_wf(LHValue,DomValue,RangeValue,Res,WF2).
997 b_check_member_expression(seq1(SeqType),_RHS,LHS,Info,LocalState,State,WFD,Res) :- !, get_wf(WFD,WF),
998 b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
999 b_wd_compute_expression(SeqType,LocalState,State,SeqTValue,WFD),
1000 opt_push_wait_flag_call_stack_info(WF,b_operator_call(member,
1001 [LHValue,b_operator(seq1,[SeqTValue])],Info),WF2),
1002 ? bsets_clp:test_finite_non_empty_sequence(LHValue,SeqTValue,Res,WF2).
1003 % TODO: more sequence checks
1004 b_check_member_expression(_Arg,RHS,LHS,_Info,LocalState,State,WFD,Res) :- get_wf(WFD,WF),
1005 b_wd_compute_expression(LHS,LocalState,State,LHValue,WFD),
1006 b_wd_compute_expression(RHS,LocalState,State,RHValue,WFD),
1007 membership_test_wf(RHValue,LHValue,Res,WF),
1008 %(var(Res) -> add_message(reify,member,LHS,_Info) ; true),
1009 force_membership_test(Res,LHValue,RHValue,WF).
1010
1011 % -------------------------
1012
1013 % wfwd/4 add information about WD context to a WF store: wfwd(WF_store, ExpectedVal, Val,Infos)
1014 % when Val becomes nonvar: if Val==ExpectedVal we need the value of E, otherwise it should be discarded
1015
1016 % get WF store from WFD record
1017 get_wf(wfwd(WF,_,_,_),Res) :- !, Res=WF.
1018 get_wf(WFD,WF) :- add_internal_error('Illegal WFD store: ',get_wf(WFD,WF)),
1019 WF = no_wf_available.
1020
1021 % get expected PredicateResult and actual value; if both identical the associated expression is needed
1022 get_wd(wfwd(_,WDExpected,WDVal,_),WDE,WDV) :- !, (WDExpected,WDVal)=(WDE,WDV).
1023 get_wd(WFWD,WDE,WDV) :- add_internal_error('Illegal WFWD info:',get_wd(WFWD,WDE,WDV)), WDE=pred_true,WDV=pred_true.
1024
1025 % create a WFWF construct for an expression that is needed and where reification should only succeed
1026 % if the top-level construct at least can be fully reified (without non-determinism)
1027 create_wfwd_needed(WF,wfwd(WF,pred_true,pred_true,outer_wfwd_context)).
1028
1029 create_wfwd(WF,WDExpected,WDVal,wfwd(WF,WDExpected,WDVal,inner_wfwd_context)).
1030
1031 :- public portray_wfwd/1.
1032 portray_wfwd(wfwd(_,WDExpected,WDVal,Ctxt)) :-
1033 format('Sub-formula required if ~w is expected ~w (ctxt: ~w)~n',[WDVal,WDExpected,Ctxt]).
1034
1035 % ----------------------
1036
1037 construct_value(Type,Val,b(value(Val),Type,[])).
1038
1039 :- use_module(bsyntaxtree, [safe_create_texpr/3]).
1040 construct_equality(NewLHS,Type,ElementV,Equality) :-
1041 Element = b(value(ElementV),Type,[]),
1042 safe_create_texpr(equal(NewLHS,Element),pred,Equality). %, translate:print_bexpr(Equality),nl.
1043
1044 %non_empty_set_version_of(seq1(RHS),seq(RHS)).
1045 %non_empty_set_version_of(iseq1(RHS),iseq(RHS)).
1046 %non_empty_set_version_of(fin1_subset(RHS),fin_subset(RHS)).
1047 %non_empty_set_version_of(pow1_subset(RHS),pow_subset(RHS)).
1048
1049
1050 :- block b_check_boolean_expression4_delay(?,-,?,?,?,?,?,?).
1051 % currently only used for existential quantified predicates in data_validation_mode
1052 b_check_boolean_expression4_delay(WDE,WDV,_Pred,_Infos,_,_,_WF,Res) :- WDE \= WDV,
1053 % no need to check reify_inner_exists_non_deterministically, as we have marked _Pred as do_not_store
1054 % ignoring (not evaluating) predicate
1055 !,
1056 Res=pred_false. % does not matter here (but Res could have been in another context, see above and test 2404)
1057 b_check_boolean_expression4_delay(_WDE,_WDV,Pred,Infos,LocalState,State,WF,Res) :-
1058 % we currently have not yet implemented a way to check the Pred; wait until Result is known
1059 (preferences:preference(use_smt_mode,false)
1060 -> get_last_wait_flag(b_check_test_boolean_expression,WF,WF2)
1061 ; get_binary_choice_wait_flag(b_check_test_boolean_expression,WF,WF2)
1062 ),
1063 (debug:debug_mode(on) -> print(' Check Testing: '),translate:print_bexpr(Pred),nl ; true),
1064 b_check_test_boolean_expression(Res,WF2,b(Pred,pred,Infos),LocalState,State,WF).
1065
1066 :- block b_check_test_boolean_expression(-,-,?,?,?,?).
1067 %b_check_test_boolean_expression(P,LWF,Pred,LocalState,State,WF) :- write(check_test4(P,LWF)),nl,fail.
1068 b_check_test_boolean_expression(pred_true,_,Pred,LocalState,State,WF) :-
1069 b_test_boolean_expression(Pred,LocalState,State,WF).
1070 b_check_test_boolean_expression(pred_false,_,Pred,LocalState,State,WF) :-
1071 b_interpreter:b_not_test_boolean_expression(Pred,LocalState,State,WF).
1072
1073
1074 /*
1075 :- use_module(kernel_mappings).
1076 member_check_should_be_reified(_,b(RHS,_,_)) :- functor(RHS,BOP,Arity), print(check(BOP,Arity)),nl,!.
1077 member_check_should_be_reified(_,_) :- \+ preferences:preference(use_smt_mode,false),!.
1078 member_check_should_be_reified(_LHS,b(RHS,_,_)) :- functor(RHS,BOP,Arity),
1079 % check if we have optimized treatments available, for which we do not yet have reified versions
1080 (Arity=1 -> \+ kernel_mappings:unary_in_boolean_type(BOP,_)
1081 ; Arity=2 -> \+ kernel_mappings:binary_in_boolean_type(BOP,_)
1082 ; Arity=0 -> \+ kernel_mappings:cst_in_boolean_type(BOP,_)
1083 ; true).
1084 */
1085
1086
1087 :- block force_membership_test(-,?,?,?).
1088 % currently required for ensuring that following fails:
1089 % kernel_objects:union(closure(['_zzzz_unit_tests'],[integer],b(member(b(identifier('_zzzz_unit_tests'),integer,[generated]),b(value([int(3),int(4)]),set(integer),[])),pred,[])),closure(['_zzzz_unit_tests'],[integer],b(member(b(identifier('_zzzz_unit_tests'),integer,[generated]),b(value([int(2),int(1)]),set(integer),[])),pred,[])),[int(1),int(3),int(2)])
1090 % Reason: membership_test does not on its own enumerate
1091 force_membership_test(pred_true,X,Set,WF) :-
1092 Set \= [],
1093 (ground_value(X) -> true
1094 ; nonvar(Set),no_use_forcing(Set) -> true % no use in forcing membership, will call same element_of_avl_set_wf
1095 ; kernel_objects:check_element_of_wf(X,Set,WF)
1096 ).
1097 force_membership_test(pred_false,_X,_Set,_WF).
1098
1099 % forcing is sometimes useful because we can transmit a WF, currently some of the reification predicates
1100 % do not have a WF-Store and can thus do limited enumeration ! in particular true for CLPFD = FALSE mode
1101 no_use_forcing(avl_set(_)) :- preferences:preference(use_clpfd_solver,true).
1102 no_use_forcing(global_set(_)).
1103 %no_use_forcing(closure(_,_,B)) :- preferences:preference(use_clpfd_solver,true).
1104
1105
1106
1107 :- use_module(external_functions,[call_external_predicate/8, do_not_evaluate_args/1]).
1108 b_check_external_pred_call(FunName,Args,Info,LocalState,State,WFD,Res) :-
1109 get_wf(WFD,WF),
1110 (do_not_evaluate_args(FunName) -> EvaluatedArgs=[]
1111 ; b_wd_compute_expressions(Args, LocalState,State, EvaluatedArgs, WFD)),
1112 push_wait_flag_call_stack_info(WF,external_call(FunName,EvaluatedArgs,Info),WF2),
1113 call_external_predicate(FunName,Args,EvaluatedArgs,LocalState,State,Res,Info,WF2).
1114
1115 wd_delay_until_needed(WFWD,Call) :-
1116 get_wd(WFWD,WDExpected,WDV),
1117 wd_delay_until_needed_block(WDExpected,WDV,Call).
1118 :- block wd_delay_until_needed_block(-,?,?), wd_delay_until_needed_block(?,-,?).
1119 wd_delay_until_needed_block(WDExpected,WDV,Call) :- WDExpected==WDV,!,
1120 call(Call).
1121 wd_delay_until_needed_block(_,_,_). % first call not needed
1122
1123
1124 :- use_module(library(avl)).
1125 reuse_predicate(_,_,no_avl) :- !,fail.
1126 reuse_predicate(Pred,Var,AVL) :-
1127 avl_fetch(Pred,AVL,Var),!. %pred_var(Var)).
1128 reuse_predicate(Pred,Var,AVL) :- %print(check(Pred)),nl, portray_avl(AVL),nl,
1129 preferences:preference(use_smt_mode,true), % it does not seem very expensive; we could always enable it
1130 ? implied_by(Pred,Val,OtherPred,OVal),
1131 avl_fetch(OtherPred,AVL,OVar), OVar==OVal,!,
1132 %print(reused_due_to_implication(Pred)),nl,nl,
1133 Var=Val.
1134
1135 add_predicate(_Pred,_Var,no_avl,NewAVL) :- !, NewAVL=no_avl.
1136 add_predicate(Pred,Var,AVL,NewAVL) :-
1137 % we could compute terms:term_hash(Pred,H) and add pred(H,Pred) to AVL to avoid comparing terms during avl_fetch
1138 (avl_store(Pred,AVL,Var,NewAVL) -> true ; NewAVL=no_avl). %pred_var(Var),NewAVL).
1139
1140 % detect whether predicate implied by some registered predicate
1141 % detects inconsistency in x:INTEGER & x>y & y>x
1142 % very lightweight propagation also achieved by CHR for less
1143 implied_by(less(A,B),pred_false,less(B,A),pred_true). % A>B => not( A<B ) <=> A>=B
1144 implied_by(subset_strict(A,B),pred_false,subset_strict(B,A),pred_true). % B <<: A => not( A<<:B )
1145 implied_by(subset_strict(A,B),pred_false,subset(B,A),pred_true). % B <: A => not( A<<:B )
1146 implied_by(subset(A,B),pred_false,subset_strict(B,A),pred_true). % B <<: A => not( A<:B )
1147 % TO DO: add more rules; e.g., less(x,10) implied by less(x,9)
1148
1149 :- use_module(translate,[print_bexpr/1]).
1150 % normalises a typed predicate by removing position information and ordering commutative operators in a canonical way
1151 norm_pred_check(B,Res) :-
1152 ( norm_pred(B,Res)
1153 -> true
1154 ; bget_functor(B,F,N),
1155 print(norm_pred_failed(F/N)), nl,
1156 print_bexpr(B), nl,
1157 Res=B
1158 ).
1159
1160 bget_functor(b(B,_,_),F,N) :- functor(B,F,N).
1161 bget_functor(B,F,N) :- functor(B,F,N).
1162
1163 %% :-(+List, -UntypedConj).
1164 conjunct_untyped([], Res) :- !, Res=truth.
1165 conjunct_untyped([P|Rest],Result) :- conjunct2(Rest,P,Result).
1166 conjunct2([],P,P).
1167 conjunct2([Q|Rest],P,Result) :- conjunct2(Rest,conjunct(P,Q),Result).
1168
1169 %% disjunct_untyped(+List, -UntypedDisj).
1170 disjunct_untyped([], Res) :- !, Res=falsity.
1171 disjunct_untyped([P|Rest],Result) :- disjunct2(Rest,P,Result).
1172 disjunct2([],P,P).
1173 disjunct2([Q|Rest],P,Result) :- disjunct2(Rest,disjunct(P,Q),Result).
1174
1175 :- assert_must_succeed((I=b(identifier(i),integer,[]),P1=b(greater_equal(I,I),pred,[]),norm_pred(P1,N1),
1176 P2=b(greater_equal(I,I),pred,[info]),norm_pred(P2,N2), N2==N1)). % info ignored
1177 :- assert_must_succeed((I1=b(identifier(i1),integer,[]),I2=b(identifier(i2),integer,[]),
1178 P1=b(equal(I1,I2),pred,[]),norm_pred(P1,N1),
1179 P2=b(equal(I2,I1),pred,[info]),norm_pred(P2,N2), N2==N1)). % equal re-ordered
1180 :- assert_must_succeed((I1=b(identifier(i1),integer,[]),I2=b(identifier(i2),integer,[]),
1181 P1=b(greater_equal(I1,I2),pred,[]),norm_pred(P1,N1),
1182 P2=b(less_equal(I2,I1),pred,[info]),norm_pred(P2,N2), N2==N1)). % <= and >= re-ordered
1183 :- assert_must_succeed((I1=b(identifier(i1),integer,[]),I2=b(identifier(i2),integer,[]),
1184 P1=b(greater(I1,I2),pred,[]),norm_pred(P1,N1),
1185 P2=b(less(I2,I1),pred,[info]),norm_pred(P2,N2), N2==N1)). % < and > re-ordered
1186 :- assert_must_succeed((I1=b(identifier(i1),integer,[]),I2=b(identifier(i2),integer,[]),
1187 P1=b(greater(I1,I2),pred,[]),norm_pred(P1,N1),
1188 P2=b(less_equal(I2,I1),pred,[]),norm_pred(P2,N2), N2 \= N1)). % > and <= not made equal
1189 :- assert_must_succeed((I=b(identifier(i),integer,[]),P1=b(greater_equal(I,I),pred,[was(test1)]),
1190 P2=b(not_equal(I,I),pred,[was(test2)]),
1191 conjunct_predicates_with_pos_info([P1,P2],C1),norm_pred(C1,N1),
1192 conjunct_predicates_with_pos_info([P2,P1],C2),norm_pred(C2,N2), N2==N1)). % conjunct re-ordered
1193 :- assert_must_succeed((I=b(identifier(i),integer,[]),P1=b(greater_equal(I,I),pred,[was(test1)]),
1194 P2=b(not_equal(I,I),pred,[was(test2)]), P3=b(equal(I,I),pred,[was(test3)]),
1195 conjunct_predicates_with_pos_info([P1,P2,P3],C1),norm_pred(C1,N1),
1196 conjunct_predicates_with_pos_info([P2,P3,P1],C2),norm_pred(C2,N2), N2==N1)).
1197
1198 %% norm_pred(+AstOrExpr, -Norm).
1199 norm_pred(X,Res) :- var(X),!,Res=X.
1200 norm_pred(b(B,_,_),Res) :- !, norm_pred(B,Res).
1201 norm_pred(falsity,Res) :- !, Res=falsity.
1202 norm_pred(truth,Res) :- !, Res=truth.
1203 norm_pred(conjunct(A,B),Res) :-
1204 !,
1205 flatten_conjunctions([A,B],CList),
1206 % sort nested conjunctions and disjunctions instead of only single ones
1207 % e.g., difference for '#i.(i : NATURAL & (i > `max`(self) & num′ = num <+ {self |-> i}))' if removing nested parentheses
1208 l_norm_pred(CList, NormedList),
1209 sort(NormedList,SortedList), % better to sort after normalisation
1210 conjunct_untyped(SortedList, Res).
1211 norm_pred(disjunct(A,B),Res) :-
1212 !,
1213 disjunction_to_list(b(disjunct(A,B),pred,[]), CList), % it would be more efficient not to re-construct the b/3 term
1214 l_norm_pred(CList, NormedList),
1215 sort(NormedList,SortedList),
1216 disjunct_untyped(SortedList, Res).
1217 norm_pred(equal(A,B),Res) :- !,norm_expr(A,AA),norm_expr(B,BB),
1218 (BB @< AA -> Res = equal(AA,BB) ; Res= equal(BB,AA)).
1219 norm_pred(equivalence(A,B),Res) :- !, norm_pred(A,AA), norm_pred(B,BB),
1220 (BB @< AA -> Res = equivalence(AA,BB) ; Res= equivalence(BB,AA)).
1221 norm_pred(exists(A,B),Res) :- !, Res=exists(AA,BB),l_norm_expr(A,AA), norm_pred(B,BB).
1222 norm_pred(finite(A),finite(AA)) :- !,norm_expr(A,AA).
1223 norm_pred(forall(A,B,C),Res) :- !, Res=forall(AA,BB,CC),l_norm_expr(A,AA), norm_pred(B,BB), norm_pred(C,CC).
1224 norm_pred(greater(A,B),Less) :- !,norm_expr(A,AA),norm_expr(B,BB), norm_less(BB,AA,Less).
1225 norm_pred(greater_equal(A,B),Leq) :- !,norm_expr(A,AA),norm_expr(B,BB), norm_less_equal(BB,AA,Leq).
1226 norm_pred(implication(A,B),Res) :- !, % we could rewrite this to disjunct(not(A),B); but check ok for WD prover
1227 Res=implication(AA,BB),norm_pred(A,AA), norm_pred(B,BB).
1228 norm_pred(less(A,B),Less) :- !,norm_expr(A,AA),norm_expr(B,BB), norm_less(AA,BB,Less).
1229 norm_pred(less_real(A,B),less_real(AA,BB)) :- !,norm_expr(A,AA),norm_expr(B,BB).
1230 norm_pred(less_equal(A,B),Leq) :- !,norm_expr(A,AA),norm_expr(B,BB), norm_less_equal(AA,BB,Leq).
1231 norm_pred(less_equal_real(A,B),less_equal_real(AA,BB)) :- !,norm_expr(A,AA),norm_expr(B,BB).
1232 norm_pred(let_predicate(A,B,C),Res) :- !,
1233 Res=let_predicate(AA,BB,CC),
1234 l_norm_expr(A,AA), l_norm_expr(B,BB),norm_pred(C,CC).
1235 norm_pred(lazy_let_pred(A,B,C),Res) :- !,
1236 Res = lazy_let_pred(AA,BB,CC),
1237 norm_expr(A,AA),
1238 norm_pred_or_expr(B,BB),norm_pred(C,CC).
1239 norm_pred(lazy_lookup_pred(A),Res) :- !, Res = lazy_lookup_pred(A).
1240 norm_pred(member(A,B),member(AA,BB)) :- !,norm_expr(A,AA),norm_expr(B,BB).
1241 norm_pred(negation(A),Res) :- !,
1242 (negate_typed_pred(A,NegA) -> norm_pred(NegA,Res)
1243 ; Res=negation(AA), norm_pred(A,AA)).
1244 norm_pred(not_equal(A,B),Res) :- !,norm_expr(A,AA),norm_expr(B,BB),
1245 (BB @< AA -> Res = not_equal(AA,BB) ; Res= not_equal(BB,AA)).
1246 norm_pred(not_member(A,B),not_member(AA,BB)) :- !,norm_expr(A,AA),norm_expr(B,BB).
1247 norm_pred(not_subset(A,B),not_subset(AA,BB)) :- !,norm_expr(A,AA),norm_expr(B,BB).
1248 norm_pred(not_subset_strict(A,B),not_subset_strict(AA,BB)) :- !,norm_expr(A,AA),norm_expr(B,BB).
1249 norm_pred(partition(A,L),partition(AA,LL)) :- !,norm_expr(A,AA),l_norm_expr(L,LL).
1250 norm_pred(subset(A,B),subset(AA,BB)) :- !,norm_expr(A,AA),norm_expr(B,BB).
1251 norm_pred(subset_strict(A,B),subset_strict(AA,BB)) :- !,norm_expr(A,AA),norm_expr(B,BB).
1252 norm_pred(freetype_case(Type,Case,A),freetype_case(Type,Case,AA)) :- !,norm_expr(A,AA).
1253 norm_pred(X,X). % :- print(norm_pred(X)),nl.
1254
1255
1256 l_norm_pred([],[]).
1257 l_norm_pred([H|T],[NH|NT]) :- norm_pred(H,NH), l_norm_pred(T,NT).
1258
1259 norm_pred_or_expr(b(B,pred,_),Res) :- norm_pred(B,Res).
1260 norm_pred_or_expr(B,Res) :- norm_expr(B,Res).
1261
1262 norm_less(unary_minus(A),MB,Res) :- apply_unary_minus(MB,B), !,norm_less(B,A,Res). % -A < -B => A > B
1263 % we could also move unary_minus to B if not present
1264 norm_less(MA,unary_minus(B),Res) :- apply_unary_minus(MA,A), !,norm_less(B,A,Res).
1265 norm_less(A,B,less(A,B)).
1266
1267 apply_unary_minus(unary_minus(A),A).
1268 apply_unary_minus(Nr,MNr) :- number(Nr), MNr is -Nr.
1269
1270 norm_less_equal(unary_minus(A),MB,Res) :- apply_unary_minus(MB,B), !,norm_less_equal(B,A,Res). % -A <= -B => A >= B
1271 norm_less_equal(MA,unary_minus(B),Res) :- apply_unary_minus(MA,A), !,norm_less_equal(B,A,Res).
1272 norm_less_equal(A,B,less_equal(A,B)).
1273
1274 norm_expr_check(X,Res) :- var(X),!,Res=X.
1275 norm_expr_check(b(B,_,_),Res) :- !, norm_expr_check2(B,Res).
1276 norm_expr_check(X,X).
1277
1278 norm_expr_check2(B,Res) :-
1279 (norm_expr2(B,Res) -> true
1280 ; functor(B,F,N),print(norm_expr2_failed(F/N)),nl,
1281 Res=B).
1282
1283 norm_expr(X,Res) :- var(X),!,Res=X.
1284 norm_expr(b(B,_,_),Res) :- !, norm_expr2(B,Res).
1285 %norm_expr_check2(B,Res). %% comment in to obtain details about failed normalisation for expressions
1286 norm_expr(X,X). % :- add_internal_error('Expr not wrapped:',norm_expr(X,X)).
1287
1288 norm_expr2(assertion_expression(Cond,E,Expr),assertion_expression(AA,E,BB)) :- norm_pred(Cond,AA),norm_expr(Expr,BB).
1289 norm_expr2(add(A,B),Res) :- norm_expr(A,AA), norm_expr(B,BB), (BB @< AA -> Res = add(AA,BB) ; Res= add(BB,AA)).
1290 norm_expr2(add_real(A,B),add_real(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1291 norm_expr2(bag_items(A),bag_items(AA)) :- norm_expr(A,AA).
1292 norm_expr2(boolean_false,boolean_false).
1293 norm_expr2(boolean_true,boolean_true).
1294 norm_expr2(bool_set,bool_set).
1295 norm_expr2(card(A),card(AA)) :- norm_expr(A,AA).
1296 norm_expr2(cartesian_product(A,B),cartesian_product(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1297 norm_expr2(closure(A),closure(AA)) :- norm_expr(A,AA). % this is closure1
1298 norm_expr2(compaction(A),compaction(AA)) :- norm_expr(A,AA).
1299 norm_expr2(composition(A,B),composition(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1300 norm_expr2(comprehension_set(A,B),comprehension_set(AA,BB)) :- l_norm_expr(A,AA), norm_pred(B,BB).
1301 norm_expr2(concat(A,B),concat(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1302 norm_expr2(convert_bool(A),convert_bool(AA)) :- norm_pred_check(A,AA).
1303 norm_expr2(convert_real(A),convert_real(AA)) :- norm_expr(A,AA).
1304 norm_expr2(convert_int_floor(A),convert_int_floor(AA)) :- norm_expr(A,AA).
1305 norm_expr2(convert_int_ceiling(A),convert_int_ceiling(AA)) :- norm_expr(A,AA).
1306 norm_expr2(couple(A,B),couple(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1307 norm_expr2(direct_product(A,B),direct_product(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1308 norm_expr2(div(A,B),div(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1309 norm_expr2(div_real(A,B),div_real(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1310 norm_expr2(floored_div(A,B),floored_div(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1311 norm_expr2(domain_restriction(A,B),domain_restriction(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1312 norm_expr2(domain_subtraction(A,B),domain_subtraction(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1313 norm_expr2(domain(A),domain(AA)) :- norm_expr(A,AA).
1314 norm_expr2(empty_sequence,empty_sequence).
1315 norm_expr2(empty_set,empty_set).
1316 norm_expr2(event_b_identity,event_b_identity).
1317 norm_expr2(external_function_call(A,B),external_function_call(A,BB)) :- l_norm_expr(B,BB).
1318 norm_expr2(fin_subset(A),fin_subset(AA)) :- norm_expr(A,AA).
1319 norm_expr2(fin1_subset(A),fin1_subset(AA)) :- norm_expr(A,AA).
1320 norm_expr2(first(A),first(AA)) :- norm_expr(A,AA).
1321 norm_expr2(first_of_pair(A),first_of_pair(AA)) :- norm_expr(A,AA).
1322 norm_expr2(first_projection(A,B),first_projection(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1323 norm_expr2(float_set,float_set).
1324 norm_expr2(front(A),front(AA)) :- norm_expr(A,AA).
1325 norm_expr2(function(A,B),function(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1326 norm_expr2(general_concat(A),general_concat(AA)) :- norm_expr(A,AA).
1327 norm_expr2(general_intersection(A),general_intersection(AA)) :- norm_expr(A,AA).
1328 norm_expr2(general_product(A,B,C),Res) :- !,
1329 Res=general_product(AA,BB,CC),l_norm_expr(A,AA), norm_pred(B,BB), norm_expr(C,CC).
1330 norm_expr2(general_sum(A,B,C),Res) :- !,
1331 Res=general_sum(AA,BB,CC),l_norm_expr(A,AA), norm_pred(B,BB), norm_expr(C,CC).
1332 norm_expr2(general_union(A),general_union(AA)) :- norm_expr(A,AA).
1333 norm_expr2(identifier(A),'$'(A)). % need wrapper to avoid confusion with other terms !
1334 norm_expr2(identity(A),identity(AA)) :- norm_expr(A,AA).
1335 norm_expr2(if_then_else(P,A,B),if_then_else(PP,AA,BB)) :- norm_pred(P,PP),norm_expr(A,AA), norm_expr(B,BB).
1336 norm_expr2(image(A,B),image(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1337 norm_expr2(insert_front(A,B),insert_front(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1338 norm_expr2(insert_tail(A,B),insert_tail(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1339 norm_expr2(integer_set(A),A).
1340 norm_expr2(integer(A),A). % integer represented as number
1341 norm_expr2(intersection(A,B),Res) :- norm_expr(A,AA), norm_expr(B,BB),
1342 (BB @< AA -> Res = intersection(AA,BB) ; Res= intersection(BB,AA)).
1343 norm_expr2(interval(A,B),interval(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1344 norm_expr2(iseq(A),iseq(AA)) :- norm_expr(A,AA).
1345 norm_expr2(iseq1(A),iseq1(AA)) :- norm_expr(A,AA).
1346 norm_expr2(iteration(A,B),iteration(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1347 norm_expr2(last(A),last(AA)) :- norm_expr(A,AA).
1348 norm_expr2(lazy_let_expr(A,B,C),lazy_let_expr(AA,BB,CC)) :-
1349 norm_expr(A,AA),norm_pred_or_expr(B,BB),norm_expr(C,CC).
1350 norm_expr2(lazy_lookup_expr(A),lazy_lookup_expr(A)) :- !.
1351 norm_expr2(let_expression(A,B,C),let_expression(AA,BB,CC)) :- l_norm_expr(A,AA), l_norm_expr(B,BB),norm_expr(C,CC).
1352 norm_expr2(let_expression_global(A,B,C),let_expression_global(AA,BB,CC)) :- l_norm_expr(A,AA), l_norm_expr(B,BB),norm_pred(C,CC).
1353 norm_expr2(max(A),max(AA)) :- norm_expr(A,AA).
1354 norm_expr2(max_real(A),max_real(AA)) :- norm_expr(A,AA).
1355 norm_expr2(max_int,max_int).
1356 norm_expr2(min(A),min(AA)) :- norm_expr(A,AA).
1357 norm_expr2(min_real(A),min_real(AA)) :- norm_expr(A,AA).
1358 norm_expr2(min_int,min_int).
1359 norm_expr2(minus(A,B),minus(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1360 norm_expr2(minus_real(A,B),minus_real(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1361 norm_expr2(modulo(A,B),modulo(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1362 norm_expr2(mu(A),mu(AA)) :- norm_expr(A,AA).
1363 norm_expr2(multiplication(A,B),Res) :-
1364 norm_expr(A,AA), norm_expr(B,BB), (BB @< AA -> Res = multiplication(AA,BB) ; Res= multiplication(BB,AA)).
1365 norm_expr2(multiplication_real(A,B),multiplication_real(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1366 norm_expr2(operation_call_in_expr(A,B),operation_call_in_expr(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1367 norm_expr2(overwrite(A,B),overwrite(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1368 norm_expr2(parallel_product(A,B),parallel_product(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1369 norm_expr2(partial_bijection(A,B),partial_bijection(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1370 norm_expr2(partial_function(A,B),partial_function(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1371 norm_expr2(partial_injection(A,B),partial_injection(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1372 norm_expr2(partial_surjection(A,B),partial_surjection(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1373 norm_expr2(perm(A),perm(AA)) :- norm_expr(A,AA).
1374 norm_expr2(power_of(A,B),power_of(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1375 norm_expr2(power_of_real(A,B),power_of_real(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1376 norm_expr2(pow_subset(A),pow_subset(AA)) :- norm_expr(A,AA).
1377 norm_expr2(pow1_subset(A),pow1_subset(AA)) :- norm_expr(A,AA).
1378 norm_expr2(predecessor,predecessor).
1379 norm_expr2(range_restriction(A,B),range_restriction(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1380 norm_expr2(range_subtraction(A,B),range_subtraction(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1381 norm_expr2(range(A),range(AA)) :- norm_expr(A,AA).
1382 norm_expr2(real(Atom),real(Atom)). % we could use the atom? or convert it to a real number using construct_real
1383 norm_expr2(real_set,real_set).
1384 norm_expr2(rec(A),rec(AA)) :- norm_fields(A,AA).
1385 norm_expr2(record_field(A,Field),record_field(AA,Field)) :- norm_expr(A,AA).
1386 norm_expr2(relations(A,B),relations(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1387 norm_expr2(restrict_front(A,B),restrict_front(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1388 norm_expr2(restrict_tail(A,B),restrict_tail(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1389 norm_expr2(reflexive_closure(A),reflexive_closure(AA)) :- norm_expr(A,AA). % this is rewritten in ast_cleanup
1390 norm_expr2(rev(A),rev(AA)) :- norm_expr(A,AA).
1391 norm_expr2(reverse(A),reverse(AA)) :- norm_expr(A,AA).
1392 norm_expr2(second_of_pair(A),second_of_pair(AA)) :- norm_expr(A,AA).
1393 norm_expr2(second_projection(A,B),second_projection(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1394 norm_expr2(seq(A),seq(AA)) :- norm_expr(A,AA).
1395 norm_expr2(seq1(A),seq1(AA)) :- norm_expr(A,AA).
1396 norm_expr2(sequence_extension(L),sequence_extension(NL)) :- l_norm_expr(L,NL).
1397 norm_expr2(set_extension(L),set_extension(NL)) :- l_norm_expr(L,NL).
1398 norm_expr2(set_subtraction(A,B),set_subtraction(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB). % set difference
1399 norm_expr2(size(A),size(AA)) :- norm_expr(A,AA).
1400 norm_expr2(string(A),string(A)). % need wrapper to avoid confusion with other terms !
1401 norm_expr2(string_set,string_set).
1402 norm_expr2(struct(A),struct(AA)) :- norm_expr(A,AA).
1403 norm_expr2(successor,successor).
1404 norm_expr2(surjection_relation(A,B),surjection_relation(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1405 norm_expr2(tail(A),tail(AA)) :- norm_expr(A,AA).
1406 norm_expr2(total_bijection(A,B),total_bijection(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1407 norm_expr2(total_function(A,B),total_function(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1408 norm_expr2(total_injection(A,B),total_injection(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1409 norm_expr2(total_relation(A,B),total_relation(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1410 norm_expr2(total_surjection(A,B),total_surjection(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1411 norm_expr2(total_surjection_relation(A,B),total_surjection_relation(AA,BB)) :- norm_expr(A,AA), norm_expr(B,BB).
1412 norm_expr2(typeset,typeset).
1413
1414 norm_expr2(unary_minus(A),unary_minus(AA)) :- norm_expr(A,AA).
1415 norm_expr2(unary_minus_real(A),unary_minus_real(AA)) :- norm_expr(A,AA).
1416 norm_expr2(union(A,B),Res) :- norm_expr(A,AA), norm_expr(B,BB), (BB @< AA -> Res = union(AA,BB) ; Res= union(BB,AA)).
1417 norm_expr2(value(A),NA) :- norm_value(A,NA).
1418
1419 norm_expr2(freetype_set(T),freetype_set(T)).
1420 norm_expr2(freetype_constructor(FT,Case,A), freetype_constructor(FT,Case,AA)) :- norm_expr(A,AA).
1421 norm_expr2(freetype_destructor(FT,Case,A),freetype_destructor(FT,Case,AA)) :- norm_expr(A,AA).
1422 norm_expr2(recursive_let(A,B),recursive_let(AA,BB)) :- norm_expr(A,AA),norm_expr(B,BB).
1423
1424
1425 norm_fields([],[]).
1426 norm_fields([field(Name,H)|T],[field(Name,NH)|NT]) :- norm_expr(H,NH), norm_fields(T,NT).
1427
1428 l_norm_expr([],[]).
1429 l_norm_expr([H|T],[NH|NT]) :- norm_expr(H,NH), l_norm_expr(T,NT).
1430
1431 norm_value(V,R) :- var(V),!,R=value(V).
1432 norm_value(int(Nr),R) :- number(Nr),!,R=Nr.
1433 norm_value([],R) :- !, R=empty_set.
1434 norm_value(pred_false,R) :- !, R=boolean_false.
1435 norm_value(pred_true,R) :- !, R=boolean_true.
1436 norm_value(closure(P,T,B),R) :- norm_pred_check(B,NB),
1437 !, % normalising relevant for test 1544 with position info added by construct_member_closure
1438 R=value(closure(P,T,NB)).
1439 norm_value((A,B),R) :- !, R=(NA,NB), norm_value(A,NA), norm_value(B,NB).
1440 norm_value(V,value(V)). % we could normalise AVL, or pairs
1441
1442 arithmetic_op(less(LHS,RHS),'<',LHS,RHS).
1443 arithmetic_op(less_equal(LHS,RHS),'<=',LHS,RHS).
1444 arithmetic_op(greater(LHS,RHS),'<',RHS,LHS).
1445 arithmetic_op(greater_equal(LHS,RHS),'<=',RHS,LHS).
1446
1447 :- use_module(probsrc(kernel_reals),[real_comp_wf/5]).
1448 % these two predicates can be checked by real_comp_wf:
1449 real_arithmetic_op(less_real(LHS,RHS),'<',LHS,RHS).
1450 real_arithmetic_op(less_equal_real(LHS,RHS),'=<',LHS,RHS).
1451
1452 :- use_module(clpfd_interface).
1453 :- use_module(library(clpfd), [(#<=>)/2]).
1454 check_arithmetic_operator('<',X,Y,Res) :- check_less(X,Y,Res),
1455 (nonvar(Res) -> true
1456 ; clpfd_interface:try_post_constraint((X#<Y) #<=> R01), prop_pred_01(Res,R01)).
1457 check_arithmetic_operator('<=',X,Y,Res) :- check_less_than_equal(X,Y,Res),
1458 (nonvar(Res) -> true
1459 ; clpfd_interface:try_post_constraint((X#=<Y) #<=> R01), prop_pred_01(Res,R01)).
1460
1461
1462 :- block prop_pred_01(-,-).
1463 prop_pred_01(A,B) :- B==1,!,A=pred_true. % cut ok: either pred_true or 1 set
1464 prop_pred_01(pred_true,1).
1465 prop_pred_01(pred_false,0).
1466
1467 :- block check_less(-,?,-), check_less(?,-,-).
1468 check_less(X,Y,Res) :- nonvar(Res),!, /* truth value known: enforce it */
1469 (Res=pred_true -> less_than_direct(X,Y) ; less_than_equal_direct(Y,X)).
1470 check_less(X,Y,Res) :-
1471 X < Y,!,/* we could call safe_less_than(X,Y), */
1472 Res=pred_true.
1473 check_less(_,_,pred_false).
1474 :- block check_less_than_equal(-,?,-), check_less_than_equal(?,-,-).
1475 check_less_than_equal(X,Y,Res) :- nonvar(Res),!, /* truth value known: enforce it */
1476 (Res=pred_true -> less_than_equal_direct(X,Y) ; less_than_direct(Y,X)).
1477 check_less_than_equal(X,Y,Res) :- X =< Y,!,Res=pred_true.
1478 check_less_than_equal(_,_,pred_false).
1479
1480
1481
1482 :- use_module(bool_pred).
1483
1484 :- use_module(kernel_objects,[exhaustive_kernel_check_wf/2,
1485 exhaustive_kernel_check/1, exhaustive_kernel_check/2, exhaustive_kernel_fail_check/1]).
1486
1487 :- assert_must_succeed(exhaustive_kernel_check_wf(b_interpreter_check:conjoin(pred_false,pred_false,pred_false,b(truth,pred,[]),b(truth,pred,[]),WF),WF)).
1488 :- assert_must_succeed(exhaustive_kernel_check_wf(b_interpreter_check:conjoin(pred_false,pred_true,pred_false,b(truth,pred,[]),b(truth,pred,[]),WF),WF)).
1489 :- assert_must_succeed(exhaustive_kernel_check_wf(b_interpreter_check:conjoin(pred_true,pred_false,pred_false,b(truth,pred,[]),b(truth,pred,[]),WF),WF)).
1490 :- assert_must_succeed(exhaustive_kernel_check_wf(b_interpreter_check:conjoin(pred_true,pred_true,pred_true,b(truth,pred,[]),b(truth,pred,[]),WF),WF)).
1491 :- assert_must_fail(b_interpreter_check:conjoin(pred_true,pred_false,pred_true,b(truth,pred,[]),b(truth,pred,[]),_WF)).
1492 :- assert_must_fail(b_interpreter_check:conjoin(pred_true,pred_true,pred_false,b(truth,pred,[]),b(truth,pred,[]),_WF)).
1493
1494
1495 % same as and_equality/3 but for pred_false/pred_true rather than eq_obj/pred_false
1496 :- block conjoin(-,-,-,?,?,?).
1497 conjoin(X,Y,Res,LHS,RHS,WF) :- % print(conjoin(X,Y,Res)),nl, translate:print_bexpr(LHS),nl,%
1498 ( Res==pred_true -> X=pred_true,Y=pred_true % on SWI these propagations happen one after the other, see test 2202
1499 ; X==pred_true -> Res=Y
1500 ; X==pred_false -> Res=pred_false
1501 ; Y==pred_true -> Res=X
1502 ; Y==pred_false -> Res=pred_false
1503 ? ; Res==pred_false -> conjoin_false0(X,Y,LHS,RHS,WF)
1504 ; add_error_fail(conjoin,'Illegal values: ', conjoin(X,Y,Res,LHS,RHS,WF))
1505 ).
1506 conjoin_false0(X,Y,_LHS,_RHS,_WF) :- X==Y,!,
1507 %print(conjoin_false_eqeq(X,Y)),nl, translate:print_bexpr(_LHS),nl,
1508 X=pred_false.
1509 conjoin_false0(X,Y,_LHS,_RHS,_WF) :- % X & not(X) -> always false
1510 bool_negate_check(X,Y),!
1511 . %,print(conjoin_false_neqeq(X,Y)),nl,translate:print_bexpr(_LHS),nl.
1512 conjoin_false0(X,Y,LHS,RHS,WF) :-
1513 %%Prio=1, %%
1514 %%(preferences:preference(use_smt_mode,full) -> FullPrio=1.5 ;
1515 get_priority_of_boolean_expression(LHS,Prio),
1516 (preferences:preference(use_clpfd_solver,true) ->
1517 % relevant for tests 349, 362:
1518 count_number_of_conjuncts(RHS,NrC),
1519 FullPrio is Prio+(NrC-1)/10,
1520 get_wait_flag(FullPrio,conjoin,WF,LWF) %%
1521 ; % in non-clpfd mode: much less propagation going on, avoid explosion of choice points
1522 % tests 349, 362 fail with the following for CLPFD: TO DO investigate and use this also in CLPFD mode
1523 get_binary_choice_wait_flag_exp_backoff(Prio,not_conjunct,WF,LWF)
1524 ),
1525 ? conjoin_false(X,Y,LHS,RHS,LWF).
1526 % missing rule: if X==Y -> X=pred_true
1527 :- block conjoin_false(-,-,?,?,-).
1528 conjoin_false(X,Y,LHS,_RHS,_LWF) :-
1529 ( X==pred_true -> pred_false=Y
1530 ; X==pred_false -> true
1531 ; Y==pred_true -> pred_false=X
1532 ; Y==pred_false -> true
1533 ; useless_to_force(LHS) ->
1534 ( % print(forcing_conjoin_rhs_false(X,Y,_LWF)), translate:print_bexpr(_RHS), print(' == FALSE '),nl,
1535 Y=pred_false
1536 ;
1537 (Y,X)=(pred_true,pred_false)
1538 )
1539 ; ( % print(forcing_conjoin_lhs_false(X,Y,_LWF)), translate:print_bexpr(LHS), print(' == FALSE '),nl,
1540 X=pred_false
1541 ; % print(forcing_conjoin_rhs_false(X,Y,_LWF)), translate:print_bexpr(LHS), print(' == TRUE ; '), nl,
1542 (X,Y)=(pred_true,pred_false)
1543 )
1544 ).
1545
1546 % determine when forcing a predicate to true/false does not really help; better choose something else
1547 useless_to_force(b(B,T,I)) :- useless_to_force3(B,T,I).
1548 useless_to_force3(finite(_),_,_). % forcing finite to be false usually not a good idea; we cannot propagate this
1549 % what are other predicates useless to force: some foralls/exists ? some external_pred_call
1550 % in principle a conjunction of useless predicates is also useless: but could be more expensive to check
1551
1552 :- assert_must_succeed(exhaustive_kernel_check(b_interpreter_check:disjoin(pred_false,pred_false,pred_false,_,_,_WF))).
1553 :- assert_must_succeed(exhaustive_kernel_check([commutative],b_interpreter_check:disjoin(pred_false,pred_true,pred_true,_,_,_WF))).
1554 :- assert_must_succeed(exhaustive_kernel_check(b_interpreter_check:disjoin(pred_true,pred_true,pred_true,_,_,_WF))).
1555 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:disjoin(pred_true,pred_true,pred_false,_,_,_WF))).
1556 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:disjoin(pred_true,pred_false,pred_false,_,_,_WF))).
1557 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:disjoin(pred_false,pred_true,pred_false,_,_,_WF))).
1558 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:disjoin(pred_false,pred_false,pred_true,_,_,_WF))).
1559
1560 :- block disjoin(-,-,-,?,?,?).
1561 disjoin(X,Y,Res,LHS,RHS,WF) :-
1562 %print(disjoin(X,Y,Res,WF)),nl, %% translate:print_bexpr(LHS),print(' or '),translate:print_bexpr(RHS),nl,%%
1563 ( Res==pred_false -> X=pred_false,Y=pred_false
1564 ; X==pred_true -> Res=pred_true
1565 ; X==pred_false -> Res=Y
1566 ; Y==pred_true -> Res=pred_true
1567 ; Y==pred_false -> Res=X
1568 ? ; Res==pred_true -> disjoin_true0(X,Y,LHS,RHS,WF)
1569 ; add_error_fail(disjoin,'Illegal values: ',disjoin(X,Y,Res,LHS,RHS,WF))
1570 ).
1571 disjoin_true0(X,Y,_LHS,_RHS,_WF) :- X==Y,!,
1572 X=pred_true.
1573 %disjoin_true0(X,Y,LHS,_,WF) :- !, disjoin_true(X,Y,_).
1574 disjoin_true0(X,Y,_LHS,_RHS,_WF) :- % X or not(X) -> always true
1575 bool_negate_check(X,Y),!.
1576
1577 disjoin_true0(X,Y,LHS,_RHS,WF) :-
1578 %%(preferences:preference(use_smt_mode,full) -> FullPrio=2.5 ;
1579 % poses problem for test 1096:
1580 % count_number_of_disjuncts(RHS,NrC),
1581 %FullPrio is Prio+(NrC-1)/10,
1582 %get_wait_flag(FullPrio,disjoin,WF,LWF), %%
1583 get_priority_of_boolean_expression(LHS,StartPrio),
1584 get_binary_choice_wait_flag_exp_backoff(StartPrio,disjunct,WF,LWF),
1585 % TO DO: extract FD information from LHS and RHS and assert, e.g. x:1..2 or x:4..5
1586 ? disjoin_true(X,Y,LHS,LWF).
1587 % missing rule: if X==Y -> X=pred_true ; if X==~Y -> no need to setup choice point
1588 :- block disjoin_true(-,-,?,-).
1589 disjoin_true(X,Y,LHS,_LWF) :-
1590 ( X==pred_true -> true
1591 ; X==pred_false -> pred_true=Y
1592 ; Y==pred_true -> true
1593 ; Y==pred_false -> pred_true=X
1594 ; useless_to_force(LHS) ->
1595 ( %% print(forcing_disjoin_true(X,Y,_LWF)),nl, %%
1596 Y=pred_true
1597 ;
1598 %%print(forcing_disjoin_false(X,Y,_LWF)),nl,
1599 (Y,X) = (pred_false,pred_true) % these two unifications will happen atomically !
1600 )
1601 ; ( %% print(forcing_disjoin_true(X,Y,_LWF)),nl, %%
1602 X=pred_true
1603 ;
1604 %%print(forcing_disjoin_false(X,Y,_LWF)),nl,
1605 (X,Y) = (pred_false,pred_true) % these two unifications will happen atomically !
1606 )
1607 %add_error_fail(disjoin_true,'Illegal values: ',disjoin_true(X,Y))
1608 ).
1609
1610 :- assert_must_succeed(exhaustive_kernel_check(b_interpreter_check:imply(pred_false,pred_false,pred_true))).
1611 :- assert_must_succeed(exhaustive_kernel_check(b_interpreter_check:imply(pred_false,pred_true,pred_true))).
1612 :- assert_must_succeed(exhaustive_kernel_check(b_interpreter_check:imply(pred_true,pred_false,pred_false))).
1613 :- assert_must_succeed(exhaustive_kernel_check(b_interpreter_check:imply(pred_true,pred_true,pred_true))).
1614 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:imply(pred_false,pred_false,pred_false))).
1615 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:imply(pred_false,pred_true,pred_false))).
1616 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:imply(pred_true,pred_false,pred_true))).
1617 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:imply(pred_true,pred_true,pred_false))).
1618
1619 imply(X,Y,Res) :-
1620 (var(X),var(Y),var(Res) -> bool_equality(X,Y,EqXY) ; true /* impl4 will not block anyway */),
1621 impl4(X,Y,EqXY,Res).
1622 :- block impl4(-,-,-,-).
1623 impl4(X,Y,EqXY,Res) :-
1624 ( Res==pred_false -> X=pred_true,Y=pred_false
1625 ; Res==pred_true -> imply_true3(X,Y,EqXY)
1626 ; X==pred_false -> Res=pred_true
1627 ; X==pred_true -> Res=Y
1628 ; Y==pred_true -> Res=pred_true
1629 ; Y==pred_false -> negate(X,Res)
1630 ; EqXY==pred_true -> Res=pred_true % X => X is always true
1631 ; EqXY==pred_false -> Y=Res % not(Y) => Y is true iff Y is true
1632 ; add_error_fail(impl,'Illegal values: ',imply(X,Y,EqXY,Res))
1633 ).
1634
1635 % assert X=pred_true => Y=pred_true
1636 imply_true(X,Y) :-
1637 (var(X),var(Y) -> bool_equality(X,Y,EqXY) ; true /* imply_true will not block anyway */),
1638 ? imply_true3(X,Y,EqXY).
1639 :- block imply_true3(-,-,-).
1640 imply_true3(X,Y,EqXY) :-
1641 ( X==pred_false -> true
1642 ; X==pred_true -> Y=pred_true
1643 ; Y==pred_true -> true
1644 ; Y==pred_false -> X=pred_false
1645 ; EqXY==pred_true -> true
1646 ; EqXY==pred_false -> X=pred_false % X => not(X) ===> X=pred_false
1647 ; add_error_fail(imply_true,'Illegal values: ',imply_true3(X,Y,EqXY))
1648 ).
1649
1650
1651 :- assert_must_succeed(exhaustive_kernel_check(b_interpreter_check:equiv(pred_false,pred_false,pred_true))).
1652 :- assert_must_succeed(exhaustive_kernel_check(b_interpreter_check:equiv(pred_false,pred_true,pred_false))).
1653 :- assert_must_succeed(exhaustive_kernel_check(b_interpreter_check:equiv(pred_true,pred_false,pred_false))).
1654 :- assert_must_succeed(exhaustive_kernel_check(b_interpreter_check:equiv(pred_true,pred_true,pred_true))).
1655 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:equiv(pred_false,pred_false,pred_false))).
1656 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:equiv(pred_false,pred_true,pred_true))).
1657 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:equiv(pred_true,pred_false,pred_true))).
1658 :- assert_must_succeed(exhaustive_kernel_fail_check(b_interpreter_check:equiv(pred_true,pred_true,pred_false))).
1659
1660 % b_interpreter_check:equiv(X,Y,Res),X=Y, Res==pred_true
1661 equiv(X,Y,Res) :-
1662 bool_equality(X,Y,Res).
1663
1664