apply_kodkod_to_counterexample_search(Variables,Assumption,Assertion,KPred,Rest) :-
get_preference(try_kodkod_on_load,true),
% The predicates may already contain Kodkod calls, replace
% them by their original B predicate
undo_kodkod_replacements(Assumption,Assumption1),
undo_kodkod_replacements(Assertion,Assertion1),
% first we do a predicate analysis to determine ranges for integers
analysis_for_counterexample(Variables,Assumption1,Assertion1,
TaggedVariables,TaggedAssumption,TaggedAssertions),
% analyse assumptions
split_assumptions(TaggedAssumption,TaggedVariables,
TAssumptions,NTAssumptions,AssumptionsComponents),
% analyse assertions
translate_assertions(TaggedAssertions,TaggedVariables,TAssumptions,NTAssumptions,
UsedAssumptionNumbers,TAssertions,NTAssertions,UsedVariables),
% if we do not find a translatalbe assertion, fail and do not apply Kodkod at all
TAssertions = [_|_],
findall( UP,
(member(N,UsedAssumptionNumbers),nth1(N,AssumptionsComponents,component(UP,_))),
UsedAssumptions),
findall( UP,
(nth1(N,AssumptionsComponents,component(UP,_)),nonmember(N,UsedAssumptionNumbers)),
UnusedAssumptions),
conjunct_predicates(TAssertions,TAssertion),
create_texpr(negation(TAssertion),pred,[],Negation),
append(UsedAssumptions,[Negation],ToTranslate),
apply_transformation(UsedVariables,ToTranslate,KPred),
( NTAssertions = [] ->
conjunct_predicates(UnusedAssumptions,Rest)
;
conjunct_predicates(NTAssertions,RestAssertion),
create_texpr(negate(RestAssertion),pred,[],RestNegated),
conjunct_predicates([Assumption,RestNegated],Rest)).