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