1 % (c) 2019-2026 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 :- module(cdclt_preprocessing, [preprocess_predicate/6,
5 optimize_clause_size_by_rewriting/5,
6 simplify_negation/2,
7 reset_artificial_id_counter/0,
8 remove_subsumed_clauses/2,
9 get_wd_theory_implications/3]).
10
11 :- use_module(library(sets)).
12 :- use_module(library(lists)).
13 :- use_module(library(plunit)).
14 :- use_module(library(samsort)).
15 :- use_module(library(terms), [term_size/2]).
16 :- use_module(cdclt_solver(cdclt_settings)).
17 :- use_module(cdclt_solver('difference_logic/ast_to_difference_logic')).
18 :- use_module(probsrc(bsyntaxtree), [find_identifier_uses/3,
19 remove_all_infos/2,
20 syntaxtransformation/5,
21 disjunct_predicates/2,
22 conjunct_predicates/2,
23 safe_create_texpr/4,
24 create_negation/2,
25 create_implication/3,
26 get_texpr_expr/2,
27 get_texpr_type/2]).
28 :- use_module(probsrc(bsyntaxtree),[map_over_bexpr/2]).
29 :- use_module(probsrc('well_def/well_def_analyser'), [prove_sequent/1]).
30 :- use_module(probsrc(kernel_tools),[ground_bexpr/1]).
31 :- use_module(probsrc(tools_meta), [safe_time_out/3]).
32 :- use_module(probsrc(error_manager), [add_internal_error/2]).
33 :- use_module(probsrc(module_information), [module_info/2]).
34 :- use_module(wdsrc(well_def_hyps), [empty_hyps/1]).
35 :- use_module(wdsrc(well_def_analyser), [compute_wd/4]).
36 :- use_module(extension('banditfuzz/welldef'), [ensure_wd/2]).
37
38 :- module_info(group, cdclt).
39 :- module_info(description,'This module provides several preprocessing steps for the CDCL(T) based solver such as lifting negations from B operators.').
40
41 :- dynamic artificial_id_counter/1.
42
43 clause_size_sort(Clause1, Clause2) :-
44 length(Clause1, L1),
45 length(Clause2, L2),
46 L1 < L2.
47
48 %% remove_subsumed_clauses(+Clauses, -ReducedClauses).
49 % Remove clauses that are subsumed by another clause. For instance, (A or B) subsumes (A or B or C).
50 remove_subsumed_clauses(Clauses, ReducedClauses) :-
51 \+ preprocessing(subsuming),
52 \+ preprocessing(resolution_subsuming),
53 !,
54 ReducedClauses = Clauses.
55 remove_subsumed_clauses(Clauses, ReducedClauses) :-
56 samsort(clause_size_sort, Clauses, SClauses),
57 safe_time_out(remove_subsumed_clauses_sorted(SClauses, TReducedClauses),
58 5000,
59 TimeOutRes),
60 ( TimeOutRes == time_out
61 -> ReducedClauses = SClauses
62 ; ReducedClauses = TReducedClauses
63 ).
64
65 remove_subsumed_clauses_sorted([], []).
66 remove_subsumed_clauses_sorted([Clause|T], Out) :-
67 remove_clauses_subsumed_by(Clause, T, false, RemoveThis, NT),
68 ( RemoveThis == true
69 -> Out = ReducedClauses
70 ; Out = [Clause|ReducedClauses]
71 ),
72 remove_subsumed_clauses_sorted(NT, ReducedClauses).
73
74 remove_clauses_subsumed_by(_, [], RemoveThis, RemoveThis, []).
75 remove_clauses_subsumed_by(Clause, [Clause2|T], RemoveThisAcc, RemoveThis, NT) :-
76 preprocessing(subsuming),
77 subsumes(Clause, Clause2),
78 !,
79 %format("Remove clause ~w due to clause ~w~n", [Clause2,Clause]),
80 remove_clauses_subsumed_by(Clause, T, RemoveThisAcc, RemoveThis, NT).
81 remove_clauses_subsumed_by(Clause, [Clause2|T], RemoveThisAcc, RemoveThis, Out) :-
82 preprocessing(resolution_subsuming),
83 boolean_resolution(Clause, Clause2, Resolved),
84 subsumes(Resolved, Clause2),
85 !,
86 %format("Resolution of ~n~w and ~n~w ~nresults in ~n~w~n~n", [Clause,Clause2,Resolved]),
87 ( subsumes(Resolved, Clause)
88 -> NRemoveThisAcc = true
89 ; NRemoveThisAcc = RemoveThisAcc
90 ),
91 Out = [Resolved|NT],
92 remove_clauses_subsumed_by(Clause, T, NRemoveThisAcc, RemoveThis, NT).
93 remove_clauses_subsumed_by(Clause, [Clause2|T], RemoveThisAcc, RemoveThis, [Clause2|NT]) :-
94 remove_clauses_subsumed_by(Clause, T, RemoveThisAcc, RemoveThis, NT).
95
96 subsumes([], _).
97 subsumes([Lit|T], Clause2) :-
98 select(Lit, Clause2, Rest),
99 subsumes(T, Rest).
100
101 %% boolean_resolution(+Clause1, +Clause2, -Resolved).
102 % Fail if no possible boolean resolution.
103 boolean_resolution(Clause1, Clause2, Resolved) :-
104 boolean_resolution(Clause1, Clause2, false, Changed, TResolved),
105 Changed == true,
106 sort(TResolved, Resolved).
107
108 boolean_resolution([], Clause2, ChangedAcc, ChangedAcc, Clause2).
109 boolean_resolution([StackInfo-Pol-Var-Name|T], Clause2, _, Changed, Resolved) :-
110 ( Pol == pred_true
111 -> NPol = pred_false
112 ; NPol = pred_true
113 ),
114 select(StackInfo-NPol-Var-Name, Clause2, Rest),
115 !,
116 append(T, Rest, Resolved),
117 Changed = true.
118 boolean_resolution([Lit|T], Clause2, ChangedAcc, Changed, [Lit|Resolved]) :-
119 boolean_resolution(T, Clause2, ChangedAcc, Changed, Resolved).
120
121 %%%
122 % Theory deduction for well-definedness to support the theory solver.
123 % For instance, deduce x:Dom for arr: Dom --> Ran & x:dom(arr).
124 % TO DO: improve code
125 get_wd_theory_implications(CandidateImpls, WDCandidateImpls, WDTheoryImpls) :-
126 CandidateImpls = candidate_impls(global(Global),integer(Integer),integer_ground(_),set(Set),set_ground(_)),
127 WDCandidateImpls = candidate_impls(global(WDGlobal),integer(WDInteger),integer_ground(_),set(WDSet),set_ground(_)),
128 append(WDInteger, WDSet, WDCandidates),
129 get_wd_theory_implications_for_candidates(Global, WDGlobal, GlobalImpls),
130 get_wd_theory_implications_for_candidates(Integer, WDCandidates, IntegerImpls),
131 get_wd_theory_implications_for_candidates(Set, WDCandidates, SetImpls),
132 conjunct_predicates([GlobalImpls,IntegerImpls,SetImpls], WDTheoryImpls).
133
134 b_function(total_function(Domain,_), Domain).
135 b_function(total_surjection(Domain,_), Domain).
136 b_function(total_injection(Domain,_), Domain).
137 b_function(total_bijection(Domain,_), Domain).
138 b_function(partial_function(Domain,_), Domain).
139 b_function(partial_surjection(Domain,_), Domain).
140 b_function(partial_injection(Domain,_), Domain).
141 b_function(partial_bijection(Domain,_), Domain).
142 b_function(surjection_relation(Domain,_), Domain).
143 b_function(total_relation(Domain,_), Domain).
144 b_function(relations(Domain,_), Domain).
145
146 b_function_membership(b(member(Id,Function),pred,_), Id, Domain) :-
147 get_texpr_expr(Function, Expr),
148 b_function(Expr, Domain).
149
150 get_wd_theory_implications_for_candidates(_, [], Out) :-
151 !,
152 Out = b(truth,pred,[]).
153 get_wd_theory_implications_for_candidates(Candidates, WDCandidates, ImplsConj) :-
154 collect_function_constraints(Candidates, FunctionConstraints),
155 get_wd_theory_implications_of_function_memberships(FunctionConstraints, WDCandidates, Impls),
156 conjunct_predicates(Impls, ImplsConj).
157
158 get_wd_theory_implications_of_function_memberships([], _, []).
159 get_wd_theory_implications_of_function_memberships([FunMem-Id-Domain|T], WDCandidates, Impls) :-
160 remove_all_infos(Id, CId),
161 memberchk(b(member(DomId,b(domain(CId),DType,DInfo)),pred,MInfo), WDCandidates),
162 !,
163 safe_create_texpr(member(DomId,b(domain(CId),DType,DInfo)), pred, MInfo, DomMember),
164 safe_create_texpr(disjunct(b(disjunct(b(negation(DomMember),pred,[]),b(negation(FunMem),pred,[])),pred,[]),b(member(DomId,Domain),pred,[])), pred, [], Impl),
165 Impls = [Impl|NT],
166 get_wd_theory_implications_of_function_memberships(T, WDCandidates, NT).
167 get_wd_theory_implications_of_function_memberships([_|T], WDCandidates, NT) :-
168 get_wd_theory_implications_of_function_memberships(T, WDCandidates, NT).
169
170 collect_function_constraints([], []).
171 collect_function_constraints([Candidate|T], FunctionConstraints) :-
172 b_function_membership(Candidate, Id, Domain),
173 !,
174 FunctionConstraints = [Candidate-Id-Domain|NT],
175 collect_function_constraints(T, NT).
176 collect_function_constraints([_|T], NT) :-
177 collect_function_constraints(T, NT).
178 %%%
179
180 negate_bool_expr(boolean_true, boolean_false).
181 negate_bool_expr(boolean_false, boolean_true).
182 negate_bool_expr(value(pred_true), boolean_false).
183 negate_bool_expr(value(pred_false), boolean_true).
184
185 is_boolean_true(b(boolean_true,boolean,_)).
186 is_boolean_true(b(value(pred_true),boolean,_)).
187
188 % convenience predicates:
189 %% create_wd_negation(+Pred, -NegPred).
190 % Negate an atomic predicate P with top-level WD condition WD as WD & not(P), not as not(P).
191 % The SAT variable of P represents WD & P (see normal_make_wd in cdclt_pred_to_sat);
192 % a plain not(P) could thus be satisfied by violating WD: e.g., for y=0 & (1/y=2 => y=7)
193 % the rewritten not(1/y=2) or y=7 would be true.
194 % Compound predicates are negated as before; their parts are handled when lifting the negation.
195 % We use the same WD condition as the SAT encoding (ensure_wd); it does not depend on hypotheses
196 % for atoms, in contrast to the POs of compute_wd/4 for a conjunction.
197 create_wd_negation(Pred, NegPred) :-
198 Pred = b(Expr,pred,_),
199 \+ is_compound_pred(Expr),
200 ensure_wd(Pred, b(conjunct(WD,_),pred,WDInfo)),
201 memberchk(fuzzed_with_wd_po_included, WDInfo),
202 !,
203 create_negation(Pred, NPred),
204 conjunct_predicates([WD,NPred], NegPred).
205 create_wd_negation(Pred, NegPred) :-
206 create_negation(Pred, NegPred).
207
208 is_compound_pred(Expr) :- functor(Expr, F, N), compound_pred_functor(F, N).
209 compound_pred_functor(conjunct, 2).
210 compound_pred_functor(disjunct, 2).
211 compound_pred_functor(implication, 2).
212 compound_pred_functor(equivalence, 2).
213 compound_pred_functor(negation, 1).
214 compound_pred_functor(exists, 2).
215 compound_pred_functor(forall, 3).
216 compound_pred_functor(let_predicate, 3).
217 compound_pred_functor(if_then_else, 3).
218
219 disjunct_two_preds(A,B,Expr) :-
220 disjunct_predicates([A,B],b(Expr,_,_)).
221 conjunct_two_preds(A,B,Expr) :-
222 conjunct_predicates([A,B],b(Expr,_,_)).
223
224 %% simplify_negation(+Ast, -Simplified).
225 simplify_negation(Ast, Simplified) :-
226 Ast = b(Expr,Type,Info),
227 simplify_negation_e(Expr, SimplifiedExpr),
228 safe_create_texpr(SimplifiedExpr, Type, Info, Simplified).
229
230 simplify_negation_e(negation(A), SimplifiedExpr) :-
231 A = b(truth,pred,_),
232 !,
233 SimplifiedExpr = falsity.
234 simplify_negation_e(negation(A), SimplifiedExpr) :-
235 A = b(falsity,pred,_),
236 !,
237 SimplifiedExpr = truth.
238 simplify_negation_e(negation(A), SimplifiedExpr) :-
239 A = b(Expr,pred,_),
240 negated_b_operator(Expr, NExpr),
241 !,
242 SimplifiedExpr = NExpr.
243 simplify_negation_e(negation(BoolEq), SimplifiedExpr) :-
244 % important for SAT constraints such as x=TRUE & not(x=TRUE)
245 ( BoolEq = b(equal(Bool,Id),pred,_)
246 ; BoolEq = b(equal(Id,Bool),pred,_)
247 ),
248 Id = b(identifier(_),boolean,_),
249 Bool = b(Expr,boolean,Info),
250 negate_bool_expr(Expr, NExpr),
251 safe_create_texpr(NExpr, boolean, Info, NBool),
252 !,
253 SimplifiedExpr = equal(NBool,Id).
254 simplify_negation_e(negation(Neg), SimplifiedExpr) :-
255 Neg = b(negation(A),pred,_),
256 !,
257 A = b(SimplifiedExpr,_,_).
258 simplify_negation_e(negation(Conj), SimplifiedExpr) :-
259 Conj = b(conjunct(A,B),pred,_),
260 !,
261 simplify_negation(b(negation(A),pred,[]), SimplifiedA),
262 simplify_negation(b(negation(B),pred,[]), SimplifiedB),
263 disjunct_two_preds(SimplifiedA,SimplifiedB,SimplifiedExpr).
264 simplify_negation_e(negation(Disj), SimplifiedExpr) :-
265 Disj = b(disjunct(A,B),pred,_),
266 !,
267 simplify_negation(b(negation(A),pred,[]), SimplifiedA),
268 simplify_negation(b(negation(B),pred,[]), SimplifiedB),
269 conjunct_two_preds(SimplifiedA,SimplifiedB,SimplifiedExpr).
270 simplify_negation_e(negation(Impl), SimplifiedExpr) :-
271 Impl = b(implication(A,B),pred,_),
272 !,
273 simplify_negation_e(conjunct(A,b(negation(B),pred,[])), SimplifiedExpr).
274 simplify_negation_e(negation(Equi), SimplifiedExpr) :-
275 Equi = b(equivalence(A,B),pred,_),
276 !,
277 simplify_negation_e(disjunct(b(negation(b(implication(A,B),pred,[])),pred,[]),
278 b(negation(b(implication(B,A),pred,[])),pred,[])), SimplifiedExpr).
279 simplify_negation_e(Conn, SimplifiedExpr) :-
280 ( Conn = conjunct(A,B)
281 ; Conn = disjunct(A,B)
282 ; Conn = implication(A,B)
283 ; Conn = equivalence(A,B)
284 ),
285 !,
286 simplify_negation(A, SimplifiedA),
287 simplify_negation(B, SimplifiedB),
288 ( Conn = conjunct(A,B) ->
289 SimplifiedExpr = conjunct(SimplifiedA,SimplifiedB)
290 ; Conn = disjunct(A,B) ->
291 SimplifiedExpr = disjunct(SimplifiedA,SimplifiedB)
292 ; Conn = implication(A,B) ->
293 SimplifiedExpr = implication(SimplifiedA,SimplifiedB)
294 ; Conn = equivalence(A,B) ->
295 SimplifiedExpr = equivalence(SimplifiedA,SimplifiedB)
296 ).
297 simplify_negation_e(Expr, Expr).
298
299 is_literal(b(truth,pred,[])).
300 is_literal(b(falsity,pred,[])).
301 is_literal(b(equal(b(_,boolean,_),b(identifier(_),boolean,_)),pred,[])).
302 is_literal(b(equal(b(identifier(_),boolean,_),b(_,boolean,_)),pred,[])).
303
304 %% optimize_clause_size_by_rewriting(+BoolFormula, +SatVars, -OptBoolFormula, -NewSatVars, -NewVarConjList).
305 % CNF construction can suffer from exponential blowup in size.
306 % We thus rewrite
307 % - nested equivalences TO DO
308 % - equivalences under disjunctions TO DO
309 % - conjunctions which are directly under a disjunction
310 % Rewriting means to introduce a new boolean variable to break nested distributivity.
311 % For instance, A <-> (B <-> (C <-> D)) is rewritten to A <-> (B <-> R) & R <-> (C <-> D).
312 % A or (B <-> C) is rewritten to A or R & (R <-> (B <-> C))
313 % (A & B & C) or (B & C & D) is rewritten to (A & B & C) or R & (R <-> (B & C & D))
314 % Note: Assumes that negation has been pushed to literals.
315 % TO DO: use only "obvious" rewritings and not all
316 optimize_clause_size_by_rewriting(BoolFormula, SatVars, OptBoolFormula, NewSatVars, NewVarConjList) :-
317 BoolFormula = b(Expr,Type,Info),
318 optimize_clause_size_by_rewriting_expr(Expr, SatVars, OptExpr, NewSatVars, NewVarConjList),
319 safe_create_texpr(OptExpr, Type, Info, OptBoolFormula).
320
321 %% get_larger_conj(+C1, +C2, -Larger, -Other).
322 % Return the conjunction with the larger term size
323 % but at least one conjunction has to be nested.
324 get_larger_conj(C1, C2, Larger, Other) :-
325 C1 = b(conjunct(C1A,C1B),pred,_),
326 C2 \= b(conjunct(_,_),pred,_),
327 ( \+ is_literal(C1A); \+ is_literal(C1B)),
328 !,
329 Larger = C1,
330 Other = C2.
331 get_larger_conj(C1, C2, Larger, Other) :-
332 C2 = b(conjunct(C2A,C2B),pred,_),
333 C1 \= b(conjunct(_,_),pred,_),
334 ( \+ is_literal(C2A); \+ is_literal(C2B)),
335 !,
336 Larger = C2,
337 Other = C1.
338 get_larger_conj(C1, C2, Larger, Other) :-
339 C1 = b(conjunct(C1A,C1B),pred,_),
340 C2 = b(conjunct(C2A,C2B),pred,_),
341 ( \+ is_literal(C1A)
342 ; \+ is_literal(C1B)
343 ; \+ is_literal(C2A)
344 ; \+ is_literal(C2B)
345 ),
346 term_size(C1, S1),
347 term_size(C2, S2),
348 ( S1 > S2
349 -> Larger = C1,
350 Other = C2
351 ; Larger = C2,
352 Other = C1
353 ).
354
355 artificial_id_counter(0).
356
357 %% reset_artificial_id_counter.
358 reset_artificial_id_counter :-
359 retractall(artificial_id_counter(_)),
360 asserta(artificial_id_counter(0)).
361
362 get_next_artificial_id(Ast) :-
363 retract(artificial_id_counter(Id)),
364 number_codes(Id, IdCodes),
365 atom_codes(IdAtom, IdCodes),
366 atom_concat('_cnf_opt', IdAtom, IdName),
367 Ast = b(identifier(IdName),boolean,[]),
368 Id1 is Id + 1,
369 asserta(artificial_id_counter(Id1)).
370
371 optimize_clause_size_by_rewriting_expr(disjunct(D1,D2), SatVars, OptExpr, NewSatVars, NewVarConjL) :-
372 D1 = b(conjunct(_,_),pred,_),
373 D2 \= b(conjunct(_,_),pred,_),
374 !,
375 optimize_clause_size_by_rewriting_expr(disjunct(D2,D1), SatVars, OptExpr, NewSatVars, NewVarConjL).
376 optimize_clause_size_by_rewriting_expr(disjunct(D1,D2), SatVars, OptExpr, NewSatVars, NewVarConjL) :-
377 get_larger_conj(D1, D2, ToReplace, ToKeep),
378 !,
379 get_next_artificial_id(NewSatVar),
380 optimize_clause_size_by_rewriting(ToReplace, SatVars, NToReplace, SatVars1, NewVarConjL1),
381 safe_create_texpr(equal(NewSatVar,b(boolean_true,boolean,[])), pred, [], NewVarTrue),
382 safe_create_texpr(equal(NewSatVar,b(boolean_false,boolean,[])), pred, [], NewVarFalse),
383 safe_create_texpr(disjunct(NewVarFalse,NToReplace), pred, [], NewVarImpl1),
384 safe_create_texpr(negation(NToReplace), pred, [], ToReplaceNeg),
385 negate_bool_formula(ToReplaceNeg, ToReplaceNegClean),
386 optimize_clause_size_by_rewriting(ToReplaceNegClean, SatVars1, NToReplaceNegClean, SatVars2, NewVarConjL2),
387 safe_create_texpr(disjunct(NewVarTrue,NToReplaceNegClean), pred, [], NewVarImpl2),
388 optimize_clause_size_by_rewriting(ToKeep, SatVars2, NToKeep, SatVars3, NewVarConjL3),
389 OptExpr = disjunct(NToKeep,NewVarTrue),
390 NewSatVars = [NewSatVar|SatVars3],
391 append([NewVarConjL1, NewVarConjL2, NewVarConjL3], TNewVarConjL),
392 safe_create_texpr(conjunct(NewVarImpl1,NewVarImpl2), pred, [], Conj),
393 NewVarConjL = [Conj|TNewVarConjL].
394 optimize_clause_size_by_rewriting_expr(Binary, SatVars, NBinary, NewSatVars, NewVarConjL) :-
395 functor(Binary, Functor, 2),
396 (Functor = conjunct; Functor = disjunct; Functor = implication; Functor = equivalence),
397 !,
398 arg(1, Binary, Arg1),
399 arg(2, Binary, Arg2),
400 optimize_clause_size_by_rewriting(Arg1, SatVars, NArg1, SatVars1, NewVarConjL1),
401 optimize_clause_size_by_rewriting(Arg2, SatVars1, NArg2, NewSatVars, NewVarConjL2),
402 functor(NBinary, Functor, 2),
403 arg(1, NBinary, NArg1),
404 arg(2, NBinary, NArg2),
405 append(NewVarConjL1, NewVarConjL2, NewVarConjL).
406 optimize_clause_size_by_rewriting_expr(Expr, SatVars, Expr, SatVars, []).
407
408 %% preprocess_predicate(+PerformStaticAnalysis, +RewriteToIdl, +Pred, -LiftedPred, -FilteredCandidateImplsConj, -CandidateImpls).
409 preprocess_predicate(PerformStaticAnalysis, RewriteToIdl, Pred, LiftedPred, FilteredCandidateImplsConj, CandidateImpls) :-
410 empty_candidate_impls_acc(CandidateAcc),
411 lift_negations_find_impls(Pred, RewriteToIdl, CandidateAcc, TLiftedPred, CandidateImpls), !,
412 LiftedPred = TLiftedPred,
413 ( PerformStaticAnalysis = true
414 -> process_candidate_impls(CandidateImpls, FilteredCandidateImplsConj)
415 ; FilteredCandidateImplsConj = b(truth,pred,[])
416 ).
417
418 %% process_candidate_impls(+CandidateImpls, -FilteredCandidateImplsConj).
419 % Given a candidate like x>y, get partner candidates like y<x, y=<x and x=y.
420 % Filter those partner candidates that exist in the given formula (prevent introducing redundant SAT variables)
421 % and create the implication, e.g., x>y => (not(y<x) & not(y=<x) & not(x=y)).
422 process_candidate_impls(CandidateImpls, FilteredCandidateImplsConj) :-
423 CandidateImpls = candidate_impls(global(Global),integer(Integer),integer_ground(IntegerGrnd),set(Set),set_ground(SetGrnd)),
424 process_candidate_impls_list(true, Global, GlobalImplsConj),
425 process_candidate_impls_list(false, Integer, IntImplsConj),
426 process_candidate_impls_list(false, IntegerGrnd, IntGrndImplsConj),
427 process_candidate_impls_list(false, Set, SetImplsConj),
428 process_candidate_impls_list(false, SetGrnd, SetGrndImplsConj),
429 conjunct_predicates([GlobalImplsConj,IntImplsConj,IntGrndImplsConj,SetImplsConj,SetGrndImplsConj], FilteredCandidateImplsConj).
430
431 process_candidate_impls_list(IsGlobalType, GrndCandidateImpls, ImplsConj) :-
432 process_candidate_impls_list(IsGlobalType, GrndCandidateImpls, [], Impls),
433 conjunct_predicates(Impls, ImplsConj).
434
435 process_candidate_impls_list(_, [], Acc, ImplsConj) :- !, ImplsConj=Acc.
436 process_candidate_impls_list(IsGlobalType, [GrndCandidateImpl|T], Acc, ImplsConj) :-
437 ( is_true(IsGlobalType)
438 -> get_global_type_partner_candidates(GrndCandidateImpl, T, Partners, _RestT)
439 ; get_partner_candidates(GrndCandidateImpl, T, Partners, _RestT)
440 ),
441 Partners \== [],
442 !,
443 build_implications_from_ground_candidates(GrndCandidateImpl, Partners, PartialImplsConj),
444 process_candidate_impls_list(IsGlobalType, T, [PartialImplsConj|Acc], ImplsConj).
445 process_candidate_impls_list(IsGlobalType, [_|T], Acc, ImplsConj) :-
446 process_candidate_impls_list(IsGlobalType, T, Acc, ImplsConj).
447
448 %% build_implications_from_ground_candidates(+GrndCandidateImpl, +Partners, -PartialImplsConj).
449 % Given GrndCandidateImpl and some matching partner candidates, collect proven implications:
450 % p in Partners, add one of 'GrndCandidateImpl => p', 'p => GrndCandidateImpl' or nothing
451 build_implications_from_ground_candidates(GrndCandidateImpl, Partners, PartialImplsConj) :-
452 build_implications_from_ground_candidates(GrndCandidateImpl, Partners, [], PartialImplsConj).
453 % TO DO: improve, sometimes too much overhead, see Generated1000_Enumerated.mch
454 %build_conj_implications_from_ground_candidates(GrndCandidateImpl, Partners, Partners, [], PartialImplsConj2),
455 %conjunct_predicates([PartialImplsConj1,PartialImplsConj2], PartialImplsConj).
456
457 %build_conj_implications_from_ground_candidates(_, [], _, Acc, PartialImplsConj) :-
458 % conjunct_predicates(Acc, PartialImplsConj).
459 %build_conj_implications_from_ground_candidates(GrndCandidateImpl, [Partner|T], Partners, Acc, PartialImplsConj) :-
460 % select(Partner, Partners, Rest),
461 % NegConj = b(disjunct(b(negation(GrndCandidateImpl),pred,[]),b(negation(Partner),pred,[])),pred,[]),
462 % remove_zero_var(NegConj, NegConjNoZero),
463 % build_conj_implications_from_ground_conj(NegConj, NegConjNoZero, Rest, Acc, NewAcc),
464 % build_conj_implications_from_ground_candidates(GrndCandidateImpl, T, Partners, NewAcc, PartialImplsConj).
465 %
466 %build_conj_implications_from_ground_conj(_, _, [], Acc, Acc).
467 %build_conj_implications_from_ground_conj(NegConj, NegConjNoZero, [Partner|T], Acc, Impls) :-
468 % remove_zero_var(Partner, PartnerNoZero),
469 % Impl1 = b(disjunct(NegConj,Partner),pred,[]),
470 % Impl1NoZero = b(disjunct(NegConjNoZero,PartnerNoZero),pred,[]),
471 % Impl2 = b(disjunct(NegConj,b(negation(Partner),pred,[])),pred,[]),
472 % Impl2NoZero = b(disjunct(NegConjNoZero,b(negation(PartnerNoZero),pred,[])),pred,[]),
473 % ( %nl,write('Try Impl1: '), nl, translate:print_bexpr(Impl1NoZero), nl,
474 % prove_sequent(Impl1NoZero)%, write('True'),nl
475 % -> NewAcc = [Impl1|Acc]
476 % ; %nl,write('Try Impl2: '), nl, translate:print_bexpr(Impl2NoZero), nl,
477 % ( prove_sequent(Impl2NoZero),%, write('True'),nl,
478 % NewAcc = [Impl2|Acc]
479 % ; NewAcc = Acc
480 % )
481 % ),
482 % build_conj_implications_from_ground_conj(NegConj, NegConjNoZero, T, NewAcc, Impls).
483
484 /*
485 prove_with_prob(Constraint) :-
486 find_typed_identifier_uses(Constraint, [], TypedIds),
487 translate:generate_typing_predicates(TypedIds, TypingPreds),
488 conjunct_predicates(TypingPreds, TypingPred),
489 Forall = b(forall(TypedIds,TypingPred,Constraint),pred,[]),
490 solver_interface:solve_predicate(Forall, _, Res),
491 Res == solution([]).
492 */
493
494 build_implications_from_ground_candidates(_, [], Acc, PartialImplsConj) :-
495 conjunct_predicates(Acc, PartialImplsConj).
496 build_implications_from_ground_candidates(GrndCandidateImpl, [Partner|T], Acc, PartialImplsConj) :-
497 safe_create_texpr(negation(GrndCandidateImpl), pred, [], NegGrndCandidateImpl),
498 safe_create_texpr(negation(Partner), pred, [], NegPartner),
499 % possibly remove zero var from idl solver
500 safe_create_texpr(disjunct(NegGrndCandidateImpl,Partner), pred, [], Impl1),
501 remove_zero_var(Impl1, Impl1NoZero),
502 safe_create_texpr(disjunct(NegPartner,GrndCandidateImpl), pred, [], Impl2),
503 remove_zero_var(Impl2, Impl2NoZero),
504 safe_create_texpr(disjunct(NegGrndCandidateImpl,b(negation(Partner),pred,[])), pred, [], Impl3),
505 remove_zero_var(Impl3, Impl3NoZero),
506 safe_create_texpr(disjunct(NegPartner,b(negation(GrndCandidateImpl),pred,[])), pred, [], Impl4),
507 remove_zero_var(Impl4, Impl4NoZero),
508 ( %nl,write('Try Impl1: '), nl, translate:print_bexpr(Impl1NoZero), nl,
509 prove_sequent(Impl1NoZero)% write('True'),nl
510 -> NewAcc = [Impl1|Acc]
511 ; %nl,write('Try Impl2: '), nl, translate:print_bexpr(Impl2NoZero), nl,
512 prove_sequent(Impl2NoZero)% write('True'),nl
513 -> NewAcc = [Impl2|Acc]
514 ; %nl,write('Try Impl3: '), nl, translate:print_bexpr(Impl3NoZero), nl,
515 prove_sequent(Impl3NoZero)% write('True'),nl
516 -> NewAcc = [Impl3|Acc]
517 ; %nl,write('Try Impl4: '), nl, translate:print_bexpr(Impl4NoZero), nl,
518 prove_sequent(Impl4NoZero)% write('True'),nl
519 -> NewAcc = [Impl4|Acc]
520 ; NewAcc = Acc
521 ),
522 build_implications_from_ground_candidates(GrndCandidateImpl, T, NewAcc, PartialImplsConj).
523
524 %create_var_bindings([], []).
525 %create_var_bindings([Id|T], [bind(Id,_)|NT]) :-
526 % create_var_bindings(T, NT).
527 %
528 %get_var_bindings_for_ast(Ast, Bindings) :-
529 % find_identifier_uses(Ast, [], Ids),
530 % create_var_bindings(Ids, Bindings).
531
532 uses_exactly_same_ids(A, UsedIds) :-
533 find_identifier_uses(A, [], UsedA),
534 sets:subset(UsedA, UsedIds),
535 sets:subset(UsedIds, UsedA), !.
536
537 uses_same_id(A, UsedIds) :-
538 find_identifier_uses(A, [], UsedA),
539 sets:intersection(UsedA, UsedIds, Inter),
540 Inter \== [].
541
542 %% get_partner_candidates(+Id, +Rest, -Partners, -RestRest).
543 % ASTs in Rest have the same type.
544 % idea: given, e.g., 1<x: search for all comparisons between x and a ground value like 0<x, x<10 etc.
545 % afterwards, evaluate the constraints and try to build implications like 1<x => 0<x
546 get_partner_candidates(GrndCandidateImpl, Rest, Partners, RestRest) :-
547 find_identifier_uses(GrndCandidateImpl, [], UsedIds),
548 get_partner_candidates(UsedIds, Rest, [], Partners, [], RestRest),!.
549
550 get_partner_candidates(_, [], PartnersAcc, PartnersAcc, RestAcc, RestAcc).
551 get_partner_candidates(UsedIds, Rest, PartnersAcc, Partners, RestAcc, RestRest) :-
552 select(Partner, Rest, TRest),
553 ( uses_exactly_same_ids(Partner, UsedIds)
554 -> NPartnersAcc = [Partner|PartnersAcc],
555 NRestAcc = RestAcc
556 ; NPartnersAcc = PartnersAcc,
557 NRestAcc = [Partner|RestAcc]
558 ),
559 !,
560 get_partner_candidates(UsedIds, TRest, NPartnersAcc, Partners, NRestAcc, RestRest).
561
562 get_global_type_partner_candidates(GrndCandidateImpl, Rest, Partners, RestRest) :-
563 find_identifier_uses(GrndCandidateImpl, [], UsedIds),
564 get_global_type_partner_candidates(UsedIds, Rest, [], Partners, [], RestRest),!.
565
566 get_global_type_partner_candidates(_, [], PartnersAcc, PartnersAcc, RestAcc, RestAcc).
567 get_global_type_partner_candidates(UsedIds, Rest, PartnersAcc, Partners, RestAcc, RestRest) :-
568 select(Partner, Rest, TRest),
569 ( uses_same_id(Partner, UsedIds)
570 -> NPartnersAcc = [Partner|PartnersAcc],
571 NRestAcc = RestAcc
572 ; NPartnersAcc = PartnersAcc,
573 NRestAcc = [Partner|RestAcc]
574 ),
575 !,
576 get_global_type_partner_candidates(UsedIds, TRest, NPartnersAcc, Partners, NRestAcc, RestRest).
577
578 candidate_impl_binary_operator(equal, global).
579 candidate_impl_binary_operator(member, global).
580 candidate_impl_binary_operator(equal, integer).
581 candidate_impl_binary_operator(member, integer).
582 candidate_impl_binary_operator(equal, set).
583 candidate_impl_binary_operator(less, integer).
584 candidate_impl_binary_operator(less_equal, integer).
585 candidate_impl_binary_operator(greater, integer).
586 candidate_impl_binary_operator(greater_equal, integer).
587 candidate_impl_binary_operator(member, set).
588 candidate_impl_binary_operator(subset, set).
589 candidate_impl_binary_operator(subset_strict, set).
590
591 %% extend_candidate_impls_acc(+Type, +Functor, +Arg1, +Arg2, +Ast, +CandidateAcc, -NewCandidateAcc) :-
592 % Collect ASTs with functor in candidate_impl_binary_operator/2 and both arguments being
593 % an identifier or ground value.
594 extend_candidate_impls_acc(Type, Functor, Arg1, Arg2, Ast, CandidateAcc, NewCandidateAcc) :-
595 Type == pred,
596 Arg1 = b(_,AType,_),
597 functor(AType, FType, _),
598 candidate_impl_binary_operator(Functor, FType),
599 ( (ground_bexpr(Arg1),
600 is_id_or_pred_containing_id(Arg2))
601 ;
602 (ground_bexpr(Arg2),
603 is_id_or_pred_containing_id(Arg1))
604 ),
605 remove_all_infos(Ast, AstClean),
606 ast_not_in_candidate_acc(ground, FType, AstClean, CandidateAcc),
607 !,
608 extend_candidate_impls_acc_safe_ground(FType, CandidateAcc, Ast, NewCandidateAcc).
609 extend_candidate_impls_acc(Type, Functor, Arg1, Arg2, Ast, CandidateAcc, NewCandidateAcc) :-
610 Type == pred,
611 Arg1 = b(_,AType,_),
612 functor(AType, FType, _),
613 candidate_impl_binary_operator(Functor, FType),
614 is_id_or_pred_containing_id(Arg1),
615 is_id_or_pred_containing_id(Arg2),
616 remove_all_infos(Ast, AstClean),
617 ast_not_in_candidate_acc(var, FType, AstClean, CandidateAcc),
618 !,
619 extend_candidate_impls_acc_safe(FType, CandidateAcc, Ast, NewCandidateAcc).
620 extend_candidate_impls_acc(_, _, _, _, _, CandidateAcc, CandidateAcc).
621
622 is_identifier(identifier(_)).
623
624 is_id_or_pred_containing_id(TExpr) :-
625 get_texpr_expr(TExpr, Expr),
626 functor(Expr, Functor, _),
627 % no ASTs with local identifiers
628 \+ member(Functor, [forall,exists,comprehension_set,lambda,general_sum,general_product,event_b_comprehension_set,quantified_union,quantified_intersection]),
629 map_over_bexpr(is_identifier, TExpr).
630
631 %% ast_not_in_candidate_acc(+VarOrGround, +Type, +AstClean, +CandidateAcc).
632 ast_not_in_candidate_acc(var, global, AstClean, candidate_impls(global(Global),integer(_),integer_ground(_),set(_),set_ground(_))) :-
633 \+ member(AstClean, Global).
634 ast_not_in_candidate_acc(var, integer, AstClean, candidate_impls(global(_),integer(Integer),integer_ground(_),set(_),set_ground(_))) :-
635 \+ member(AstClean, Integer).
636 ast_not_in_candidate_acc(ground, integer, AstClean, candidate_impls(global(_),integer(_),integer_ground(IntegerGrnd),set(_),set_ground(_))) :-
637 \+ member(AstClean, IntegerGrnd).
638 ast_not_in_candidate_acc(var, set, AstClean, candidate_impls(global(_),integer(_),integer_ground(_),set(Set),set_ground(_))) :-
639 \+ member(AstClean, Set).
640 ast_not_in_candidate_acc(ground, set, AstClean, candidate_impls(global(_),integer(_),integer_ground(_),set(_),set_ground(SetGrnd))) :-
641 \+ member(AstClean, SetGrnd).
642
643 % TO DO: use get_sorted_equality/3 for equalities
644 extend_candidate_impls_acc_safe_ground(integer, candidate_impls(global(Global),integer(Integer),integer_ground(IntegerGrnd),set(Set),set_ground(SetGrnd)), Ast, candidate_impls(global(Global),integer(Integer),integer_ground([Ast|IntegerGrnd]),set(Set),set_ground(SetGrnd))).
645 extend_candidate_impls_acc_safe_ground(set, candidate_impls(global(Global),integer(Integer),integer_ground(IntegerGrnd),set(Set),set_ground(SetGrnd)), Ast, candidate_impls(global(Global),integer(Integer),integer_ground(IntegerGrnd),set(Set),set_ground([Ast|SetGrnd]))).
646
647 extend_candidate_impls_acc_safe(global, candidate_impls(global(Global),integer(Integer),integer_ground(IntegerGrnd),set(Set),set_ground(SetGrnd)), Ast, candidate_impls(global([Ast|Global]),integer(Integer),integer_ground(IntegerGrnd),set(Set),set_ground(SetGrnd))).
648 extend_candidate_impls_acc_safe(integer, candidate_impls(global(Global),integer(Integer),integer_ground(IntegerGrnd),set(Set),set_ground(SetGrnd)), Ast, candidate_impls(global(Global),integer([Ast|Integer]),integer_ground(IntegerGrnd),set(Set),set_ground(SetGrnd))).
649 extend_candidate_impls_acc_safe(set, candidate_impls(global(Global),integer(Integer),integer_ground(IntegerGrnd),set(Set),set_ground(SetGrnd)), Ast, candidate_impls(global(Global),integer(Integer),integer_ground(IntegerGrnd),set([Ast|Set]),set_ground(SetGrnd))).
650
651 empty_candidate_impls_acc(candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([]))).
652
653 %% lift_negations_find_impls(+Pred, +RewriteToIdl, +CandidateAcc, -LPred, -CandidateImpls).
654 lift_negations_find_impls(Pred, RewriteToIdl, CandidateAcc, LPred, CandidateImpls) :-
655 Pred = b(Expr,Type,Info),
656 !,
657 lift_negations_find_impls_e(Expr, Type, Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls),
658 safe_create_texpr(LExpr, Type, Info, LPred).
659 lift_negations_find_impls(Pred, _, CandidateAcc, Pred, CandidateAcc).
660
661 %% lift_negations_find_impls_e(+Expr, +Type, +RewriteToIdl, +CandidateAcc, -LExpr, -CandidateImpls).
662 % Unfold negated operators like not_equal to detect equal ASTs when introducing
663 % boolean variables for the SAT formula. For instance, it improves performance
664 % to transform 'x={} & x/={}' to 'x={} & not(x={})' first, since we can then translate to
665 % 'A & not(A)' in SAT rather than 'A & B'.
666 % Collect specific operators that might imply each other like 'x>0' and 'x>1' result to 'x>1 => x>0' (see candidate_impl_binary_operator/2).
667 % Note: only binary operators are checked recursively since we transform SMT to SAT on the level of conjunct, disjunct and implication
668 % Note: CandidateImpls contains true asts of lifted predicates, e.g., 'x:{1,2}' is stored for 'x/:{1,2}'
669 lift_negations_find_impls_e(Expr, _, _, _, _, _, _) :- var(Expr),!,
670 add_internal_error('Illegal var B AST: ',lift_negations_find_impls_e(Expr, _, _, _, _, _, _)),fail.
671 lift_negations_find_impls_e(Expr, _, _, _, CandidateAcc, LPred, CandidateImpls) :-
672 % important to use the same SAT variables, e.g., for x=TRUE & x=FALSE
673 (Expr = equal(Bool,BoolId) ; Expr = equal(BoolId,Bool)),
674 BoolId = b(identifier(_),boolean,_),
675 is_boolean_true(Bool),
676 !,
677 LPred = negation(b(equal(b(boolean_false,boolean,[]),BoolId),pred,[])),
678 CandidateImpls = CandidateAcc.
679 lift_negations_find_impls_e(Expr, _, Info, RewriteToIdl, CandidateAcc, LPred, CandidateImpls) :-
680 Expr = equivalence(Lhs,Rhs),
681 !,
682 create_implication(Lhs,Rhs,Impl1),
683 create_implication(Rhs,Lhs,Impl2),
684 Rewritten = conjunct(Impl1,Impl2),
685 /*Disj1 = b(conjunct(Lhs,Rhs),pred,[]),
686 Disj2 = b(conjunct(b(negation(Lhs),pred,[]),b(negation(Rhs),pred,[])),pred,[]),
687 Rewritten = b(disjunct(Disj1,Disj2),pred,[]),*/
688 lift_negations_find_impls_e(Rewritten, pred, Info, RewriteToIdl, CandidateAcc, LPred, CandidateImpls).
689 lift_negations_find_impls_e(Expr, _, Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls) :-
690 Expr = implication(Lhs,Rhs),
691 !,
692 create_wd_negation(Lhs,NLhs),
693 Rewritten = disjunct(NLhs,Rhs),
694 lift_negations_find_impls_e(Rewritten, pred, Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls).
695 lift_negations_find_impls_e(Expr, _, Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls) :-
696 Expr = negation(b(negation(Pos),pred,_)),
697 get_texpr_expr(Pos, PosExpr),
698 get_texpr_type(Pos, PosType),
699 !,
700 lift_negations_find_impls_e(PosExpr, PosType, Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls).
701 lift_negations_find_impls_e(Expr, _, _, _, CandidateAcc, LExpr, CandidateImpls) :-
702 Expr = negation(b(truth,pred,_)),
703 !,
704 LExpr = falsity,
705 CandidateImpls = CandidateAcc.
706 lift_negations_find_impls_e(Expr, _, _, _, CandidateAcc, LExpr, CandidateImpls) :-
707 Expr = negation(b(falsity,pred,_)),
708 !,
709 LExpr = truth,
710 CandidateImpls = CandidateAcc.
711 lift_negations_find_impls_e(Inequality, _, _, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls) :-
712 is_true(RewriteToIdl),
713 ( Inequality = not_equal(b(_,integer,_),_)
714 %IEQ = Inequality
715 ; Inequality = negation(b(equal(b(_,integer,_),_),pred,_))
716 %IEQ = b(not_equal(Arg1,Arg2),pred,EQInfo)
717 ),
718 rewrite_inequality_to_idl_disj_no_zero(b(Inequality,pred,[]), DLConstraint),
719 !,
720 DLConstraint = b(disjunct(TLhs,TRhs),pred,_),
721 remove_idl_origin_from_info(TLhs, Lhs),
722 remove_idl_origin_from_info(TRhs, Rhs),
723 Lhs = b(negation(LhsPos),_,_),
724 Rhs = b(negation(RhsPos),_,_),
725 LhsPos = b(less_equal(LArg1,LArg2),pred,_),
726 RhsPos = b(less_equal(RArg1,RArg2),pred,_),
727 disjunct_two_preds(Lhs, Rhs, LExpr),
728 %extend_candidate_impls_acc(pred, not_equal, Arg1, Arg2, IEQ, CandidateAcc, CandidateAcc1),
729 extend_candidate_impls_acc(pred, less_equal, LArg1, LArg2, LhsPos, CandidateAcc, CandidateAcc1),
730 extend_candidate_impls_acc(pred, less_equal, RArg1, RArg2, RhsPos, CandidateAcc1, CandidateImpls).
731 lift_negations_find_impls_e(Equality, _, _, RewriteToIdl, CandidateAcc, LPred, CandidateImpls) :-
732 is_true(RewriteToIdl),
733 ( Equality = equal(b(_,integer,_),_)
734 %EQ = Equality
735 ; Equality = negation(b(not_equal(b(_,integer,_),_),pred,_))
736 %EQ = b(equal(Arg1,Arg2),pred,EQInfo)
737 ),
738 rewrite_to_idl_no_zero(b(Equality,pred,[]), ConjList),
739 !,
740 ConjList = [TLhs,TRhs],
741 % don't create _zero var from idl solver in SAT formula
742 remove_idl_origin_from_info(TLhs, Lhs),
743 remove_idl_origin_from_info(TRhs, Rhs),
744 Lhs = b(less_equal(LArg1,LArg2),pred,_),
745 Rhs = b(less_equal(RArg1,RArg2),pred,_),
746 conjunct_two_preds(Lhs, Rhs, LPred),
747 extend_candidate_impls_acc(pred, less_equal, LArg1, LArg2, Lhs, CandidateAcc, CandidateAcc1),
748 extend_candidate_impls_acc(pred, less_equal, RArg1, RArg2, Rhs, CandidateAcc1, CandidateImpls).
749 lift_negations_find_impls_e(Expr, Type, Info, _, CandidateAcc, LExpr, CandidateImpls) :-
750 negated_b_operator(Expr, TrueNode),
751 % don't lift negations for predicates with a WD condition, e.g., x:INT & min(p)/=x
752 % otherwise, this would be transformed to x:INT & not(not(p = {}) & min(p) = x) allowing p={}
753 empty_hyps(Hyps),
754 \+ compute_wd(b(Expr,Type,Info), Hyps, [discharge_po,skip_finite_po], _),
755 !,
756 safe_create_texpr(TrueNode,Type,[],TrueAst),
757 TrueNode =.. [NFunctor, Arg1, Arg2],
758 extend_candidate_impls_acc(Type, NFunctor, Arg1, Arg2, TrueAst, CandidateAcc, CandidateImpls),
759 LExpr = negation(TrueAst).
760 % simplify negated conjunction or disjunction possibly introduced by rewriting equivalence and implication
761 lift_negations_find_impls_e(Expr, _, Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls) :-
762 Expr = negation(Arg),
763 Arg = b(conjunct(Conj1,Conj2),pred,_),
764 !,
765 create_wd_negation(Conj1,NConj1),
766 create_wd_negation(Conj2,NConj2),
767 disjunct_two_preds(NConj1,NConj2,Rewritten),
768 lift_negations_find_impls_e(Rewritten, pred, Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls).
769 lift_negations_find_impls_e(Expr, _, Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls) :-
770 Expr = negation(Arg),
771 Arg = b(disjunct(Conj1,Conj2),pred,_),
772 !,
773 create_wd_negation(Conj1,NConj1),
774 create_wd_negation(Conj2,NConj2),
775 Rewritten = conjunct(NConj1,NConj2),
776 lift_negations_find_impls_e(Rewritten, pred, Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls).
777 lift_negations_find_impls_e(Expr, pred, _Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls) :-
778 Expr = negation(Arg),
779 Arg = b(ArgExpr,ArgType,ArgInfo),
780 lift_negations_find_impls_e(ArgExpr, ArgType, ArgInfo, RewriteToIdl, CandidateAcc, LArgExpr, CandidateImpls),
781 safe_create_texpr(LArgExpr,pred,ArgInfo,TLA),
782 create_negation(TLA,b(LExpr,_,_)).
783 lift_negations_find_impls_e(Expr, pred, Info, RewriteToIdl, CandidateAcc, LExpr, CandidateImpls) :-
784 functor(Expr, Functor, _),
785 Names = [], % quantifications (comprehension_set, exists) will be abstracted by a single SAT variable
786 syntaxtransformation(Expr,[Arg1,Arg2],Names,[LLhs,LRhs],TLExpr),
787 !,
788 LExpr = TLExpr,
789 extend_candidate_impls_acc(pred, Functor, Arg1, Arg2, b(Expr,pred,Info), CandidateAcc, CandidateAcc1),
790 lift_negations_find_impls(Arg1, RewriteToIdl, CandidateAcc1, LLhs, CandidateAcc2),
791 lift_negations_find_impls(Arg2, RewriteToIdl, CandidateAcc2, LRhs, CandidateImpls).
792 lift_negations_find_impls_e(Expr, _, _, _, CandidateAcc, Expr, CandidateAcc).
793
794 is_true(X) :-
795 ( X == true
796 -> true
797 ; X == false
798 -> fail
799 ; add_internal_error('Illegal call:',is_true(X)),
800 fail
801 ).
802
803 %get_sorted_equality(Lhs, Rhs, Equality) :-
804 % Lhs @> Rhs,
805 % !,
806 % safe_create_texpr(equal(Rhs,Lhs), pred, [], Equality).
807 %get_sorted_equality(Lhs, Rhs, b(equal(Lhs,Rhs),pred,[])).
808
809 %binary_connective(conjunct).
810 %binary_connective(disjunct).
811 %binary_connective(implication).
812 %binary_connective(equivalence).
813
814 negated_b_operator(not_equal(A,B), equal(A,B)).
815 negated_b_operator(not_member(A,B), member(A,B)).
816 negated_b_operator(not_subset(A,B), subset(A,B)).
817 negated_b_operator(not_subset_strict(A,B), subset_strict(A,B)).
818
819 negate_bool_formula(b(negation(b(truth,pred,Info)),pred,_), b(falsity,pred,Info)).
820 negate_bool_formula(b(negation(b(falsity,pred,Info)),pred,_), b(truth,pred,Info)).
821 negate_bool_formula(b(negation(b(negation(Ast))),pred,_), NAst) :-
822 !,
823 negate_bool_formula(Ast, NAst).
824 negate_bool_formula(b(negation(Eq),pred,_), b(equal(Id,Negated),pred,EqInfo)) :-
825 ( Eq = b(equal(Bool,Id),pred,EqInfo)
826 ; Eq = b(equal(Id,Bool),pred,EqInfo)
827 ),
828 Bool = b(Expr,boolean,BoolInfo),
829 negate_bool_expr(Expr, NExpr),
830 safe_create_texpr(NExpr, boolean, BoolInfo, Negated),
831 Id = b(identifier(_),_,_).
832 negate_bool_formula(b(negation(b(conjunct(A,B),pred,I)),pred,_), New) :-
833 negate_bool_formula(b(negation(A),pred,[]), NewA),
834 negate_bool_formula(b(negation(B),pred,[]), NewB),
835 safe_create_texpr(disjunct(NewA,NewB), pred, I, New).
836 negate_bool_formula(b(negation(b(disjunct(A,B),pred,I)),pred,_), New) :-
837 negate_bool_formula(b(negation(A),pred,[]), NewA),
838 negate_bool_formula(b(negation(B),pred,[]), NewB),
839 safe_create_texpr(conjunct(NewA,NewB), pred, I, New).
840
841 %%%%%%%%%%%%%%%%%%%%% Unit Tests %%%%%%%%%%%%%%%%%%%%%
842 :- begin_tests(extend_candidate_impls_acc).
843
844 test(extend_candidate_impls_acc_less, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([b(less(b(identifier(x),integer,[]),b(integer(7),integer,[])),pred,[])]),set([]),set_ground([])))]) :-
845 empty_candidate_impls_acc(EmptyAcc),
846 Arg1 = b(identifier(x),integer,[]),
847 Arg2 = b(integer(7),integer,[]),
848 Ast = b(less(Arg1,Arg2),pred,[]),
849 extend_candidate_impls_acc(pred, less, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
850
851 test(extend_candidate_impls_acc_less_non_ground, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
852 empty_candidate_impls_acc(EmptyAcc),
853 Arg1 = b(identifier(x),set(set(integer)),[]),
854 Arg2 = b(set_extension([_]),set(set(integer)),[]),
855 Ast = b(less(Arg1,Arg2),pred,[]),
856 extend_candidate_impls_acc(pred, less, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
857
858 test(extend_candidate_impls_acc_less_no_identifier, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
859 empty_candidate_impls_acc(EmptyAcc),
860 Arg1 = b(comprehension_set([b(identifier(x),integer,[])],b(member(b(identifier(x),integer,[]), b(set_extension([b(integer(234),integer,[]),b(integer(34),integer,[])]),set(integer),[])),pred,[])),set(integer),[some,info]),
861 Arg2 = b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[]),
862 Ast = b(less(Arg1,Arg2),pred,[]),
863 extend_candidate_impls_acc(pred, less, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
864
865 test(extend_candidate_impls_acc_less_eq, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([b(less_equal(b(identifier(x),integer,[]),b(integer(7),integer,[])),pred,[])]),set([]),set_ground([])))]) :-
866 empty_candidate_impls_acc(EmptyAcc),
867 Arg1 = b(identifier(x),integer,[]),
868 Arg2 = b(integer(7),integer,[]),
869 Ast = b(less_equal(Arg1,Arg2),pred,[]),
870 extend_candidate_impls_acc(pred, less_equal, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
871
872 test(extend_candidate_impls_acc_less_eq_non_ground, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
873 empty_candidate_impls_acc(EmptyAcc),
874 Arg1 = b(identifier(x),set(set(integer)),[]),
875 Arg2 = b(set_extension([_]),set(set(integer)),[]),
876 Ast = b(less_equal(Arg1,Arg2),pred,[]),
877 extend_candidate_impls_acc(pred, less_equal, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
878
879 test(extend_candidate_impls_acc_less_eq_no_identifier, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
880 empty_candidate_impls_acc(EmptyAcc),
881 Arg1 = b(comprehension_set([b(identifier(x),integer,[])],b(member(b(identifier(x),integer,[]), b(set_extension([b(integer(234),integer,[]),b(integer(34),integer,[])]),set(integer),[])),pred,[])),set(integer),[some,info]),
882 Arg2 = b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[]),
883 Ast = b(less_equal(Arg1,Arg2),pred,[]),
884 extend_candidate_impls_acc(pred, less_equal, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
885
886 test(extend_candidate_impls_acc_subset, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([b(subset(b(identifier(x),set(set(integer)),[]),b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[])),pred,[])])))]) :-
887 empty_candidate_impls_acc(EmptyAcc),
888 Arg1 = b(identifier(x),set(set(integer)),[]),
889 Arg2 = b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[]),
890 Ast = b(subset(Arg1,Arg2),pred,[]),
891 extend_candidate_impls_acc(pred, subset, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
892
893 test(extend_candidate_impls_acc_subset_non_ground, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
894 empty_candidate_impls_acc(EmptyAcc),
895 Arg1 = b(identifier(x),set(set(integer)),[]),
896 Arg2 = b(set_extension([_]),set(set(integer)),[]),
897 Ast = b(subset(Arg1,Arg2),pred,[]),
898 extend_candidate_impls_acc(pred, subset, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
899
900 test(extend_candidate_impls_acc_subset_no_identifier, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
901 empty_candidate_impls_acc(EmptyAcc),
902 Arg1 = b(comprehension_set([b(identifier(x),integer,[])],b(member(b(identifier(x),integer,[]), b(set_extension([b(integer(234),integer,[]),b(integer(34),integer,[])]),set(integer),[])),pred,[])),set(integer),[some,info]),
903 Arg2 = b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[]),
904 Ast = b(subset(Arg1,Arg2),pred,[]),
905 extend_candidate_impls_acc(pred, subset, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
906
907 test(extend_candidate_impls_acc_subset_strict, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([b(subset_strict(b(identifier(x),set(set(integer)),[]),b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[])),pred,[])])))]) :-
908 empty_candidate_impls_acc(EmptyAcc),
909 Arg1 = b(identifier(x),set(set(integer)),[]),
910 Arg2 = b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[]),
911 Ast = b(subset_strict(Arg1,Arg2),pred,[]),
912 extend_candidate_impls_acc(pred, subset_strict, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
913
914 test(extend_candidate_impls_acc_subset_strict_non_ground, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
915 empty_candidate_impls_acc(EmptyAcc),
916 Arg1 = b(identifier(x),set(set(integer)),[]),
917 Arg2 = b(set_extension([_]),set(set(integer)),[]),
918 Ast = b(subset_strict(Arg1,Arg2),pred,[]),
919 extend_candidate_impls_acc(pred, subset_strict, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
920
921 test(extend_candidate_impls_acc_subset_strict_no_identifier, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
922 empty_candidate_impls_acc(EmptyAcc),
923 Arg1 = b(comprehension_set([b(identifier(x),integer,[])],b(member(b(identifier(x),integer,[]), b(set_extension([b(integer(234),integer,[]),b(integer(34),integer,[])]),set(integer),[])),pred,[])),set(integer),[some,info]),
924 Arg2 = b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[]),
925 Ast = b(subset_strict(Arg1,Arg2),pred,[]),
926 extend_candidate_impls_acc(pred, subset_strict, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
927
928 test(extend_candidate_impls_acc_equal, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([b(equal(b(identifier(x),set(set(integer)),[]),b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[])),pred,[])])))]) :-
929 empty_candidate_impls_acc(EmptyAcc),
930 Arg1 = b(identifier(x),set(set(integer)),[]),
931 Arg2 = b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[]),
932 Ast = b(equal(Arg1,Arg2),pred,[]),
933 extend_candidate_impls_acc(pred, equal, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
934
935 test(extend_candidate_impls_acc_member, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([b(member(b(identifier(x),set(integer),[]),b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[])),pred,[])])))]) :-
936 empty_candidate_impls_acc(EmptyAcc),
937 Arg1 = b(identifier(x),set(integer),[]),
938 Arg2 = b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[]),
939 Ast = b(member(Arg1,Arg2),pred,[]),
940 extend_candidate_impls_acc(pred, member, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
941
942 test(extend_candidate_impls_acc_member_non_ground_type, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
943 empty_candidate_impls_acc(EmptyAcc),
944 Arg1 = b(identifier(x),set(integer),[]),
945 Arg2 = b(set_extension([b(set_extension([b(integer(27),integer,[])]),set(integer),[])]),set(set(integer)),[]),
946 Ast = b(member(Arg1,Arg2),pred,[]),
947 extend_candidate_impls_acc(_Type, member, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
948
949 test(extend_candidate_impls_acc_add, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
950 empty_candidate_impls_acc(EmptyAcc),
951 Arg1 = b(integer(1),integer,[]),
952 Arg2 = b(integer(1),integer,[]),
953 Ast = b(add(Arg1,Arg2),integer,[]),
954 extend_candidate_impls_acc(integer, add, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
955
956 test(extend_candidate_impls_acc_overwrite, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
957 empty_candidate_impls_acc(EmptyAcc),
958 Arg1 = b(set_extension([b(couple(b(integer(1),integer,[]),b(integer(2),integer,[])),couple(integer,integer),[info(o),o])]),set(couple(integer,integer)),[]),
959 Arg2 = b(set_extension([b(couple(b(integer(1),integer,[]),b(integer(2),integer,[])),couple(integer,integer),[ein(e),wei(t,e(re)),info])]),set(couple(integer,integer)),[]),
960 Ast = b(overwrite(Arg1,Arg2),set(couple(integer,integer)),[]),
961 extend_candidate_impls_acc(integer, add, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
962
963 test(extend_candidate_impls_acc_overwrite_non_ground_type, [true(NewAcc == candidate_impls(global([]),integer([]),integer_ground([]),set([]),set_ground([])))]) :-
964 empty_candidate_impls_acc(EmptyAcc),
965 Arg1 = b(set_extension([b(couple(b(integer(1),integer,[]),b(integer(2),integer,[])),couple(integer,integer),[info(o),o])]),set(couple(integer,integer)),[]),
966 Arg2 = b(set_extension([b(couple(b(integer(1),integer,[]),b(integer(2),integer,[])),couple(integer,integer),[ein(e),wei(t,e(re)),info])]),set(couple(integer,integer)),[]),
967 Ast = b(overwrite(Arg1,Arg2),set(couple(integer,integer)),[]),
968 extend_candidate_impls_acc(_Type, add, Arg1, Arg2, Ast, EmptyAcc, NewAcc).
969
970 :- end_tests(extend_candidate_impls_acc).
971