1 % (c) 2013-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
5 :- module(disprover, [disprove/5, disprove_with_opts/6, get_disprover_default_opts/1,
6 print_prover_result_stats/0,
7 get_prover_result_stats/2, reset_prover_result_stats/0,
8 prover_result_stats_available/0, prover_result_stats_table/1,
9 disprover_counter_example_dot/5]).
10
11 :- use_module(probsrc(module_information)).
12 :- module_info(group,cbc).
13 :- module_info(description,'Rodin Prover / Disprover').
14
15 % provide access to the Disprover algorithm with all hypotheses, selected hypotheses and goal
16
17 :- use_module(extension('user_signal/user_signal'), [user_interruptable_call_det/2]).
18
19 :- use_module(probsrc(solver_interface), [solve_predicate/5,type_check_in_machine_context/2]).
20 :- use_module(probsrc(preferences), [temporary_set_preference/3, reset_temporary_preference/2,
21 get_preference/2]).
22 :- use_module(probsrc(bsyntaxtree), [conjunct_predicates/2, create_negation/2,
23 find_typed_identifier_uses/3,
24 find_typed_identifier_uses_l/3,
25 get_texpr_types/2]).
26 :- use_module(probsrc(error_manager), [reset_errors/0, add_message/3,
27 add_warning/3, add_internal_error/2, add_error_and_fail/2]).
28 :- use_module(library(lists)).
29 :- use_module(probsrc(tools),[start_ms_timer/1, stop_ms_timer/1,stop_ms_timer_with_debug_msg/2,
30 get_elapsed_timer/2, combiner_timer/3]).
31 :- use_module(probsrc(debug), [debug_println/2,debug_mode/1, debug_format/3]).
32 :- use_module(probsrc(b_global_sets),[contains_unfixed_deferred_set/1]).
33 :- use_module(probsrc(external_functions),[observe_state/1]).
34 :- use_module(extrasrc(unsat_cores),[unsat_core/2]).
35 :- use_module(probsrc(pathes_lib), [unavailable_extension/2]).
36
37 % disprove(Goal,AllHypotheses,SelectedHypotheses,Timeout,OutResult)
38 % Goal can either already be type checked or a raw AST; ditto for lists of hypotheses
39 % OutResult possible values:
40 % contradiction_found
41 % contradiction_in_hypotheses
42 % solution(_) % Counter-Example for all hyps
43 % solution_on_selected_hypotheses(_) % Counter-Example to Goal for selected Hypotheses found
44 % time_out
45 % no_solution_found(_).
46
47 disprove(Goal,AllHypotheses,SelectedHypotheses,Timeout,OutResult) :-
48 get_disprover_default_opts(Opts),
49 % Note: disprover_mode is always set to true
50 disprove_with_opts(Goal,AllHypotheses,SelectedHypotheses,Timeout,Opts,OutResult).
51
52 get_disprover_default_opts(Opts) :-
53 (unavailable_extension(chr_extension,_Reason)
54 -> CHR=false, format('Disabling CHR in disprover as extension not available~n',[]) ; CHR=true),
55 % default options:
56 Opts = [use_smt_mode/true,use_clpfd_solver/true,use_chr_solver/CHR].
57
58 % ----------
59
60 disprove_with_opts(Goal,AllHypotheses,SelectedHypotheses,Timeout,Opts,OutResult) :-
61 retractall(last_prover_stats(_,_,_,_)), % the statistics of this call only
62 check_options(Opts),
63 temporary_set_preference(disprover_mode,true,DC),
64 temporary_set_preference(time_out,Timeout,TC),
65 start_ms_timer(Timer),
66 user_interruptable_call_det(disprove1(Goal,AllHypotheses,SelectedHypotheses,Opts,Result),InterruptResult),
67 %write(ir(InterruptResult)), nl,
68 (InterruptResult = interrupted -> OutResult = interrupted ; OutResult = Result),
69 %catch_internal_timeout(InterruptResult,Result,OutResult),
70 (debug_mode(on) -> write(disprove_result(OutResult)),nl,stop_ms_timer(Timer),nl ; true),
71 reset_temporary_preference(disprover_mode,DC),
72 reset_temporary_preference(time_out,TC).
73
74 check_options([]) :- !.
75 check_options([O|T]) :- !,
76 ? (valid_option(O) -> true
77 ; valid_disprover_option(O) -> add_warning(disprover,'Missing disprover_option wrapper for: ',O)
78 ; add_warning(disprover,'Illegal disprover option: ',O)),
79 check_options(T).
80 check_options(O) :- add_internal_error('Illegal disprover options: ',check_options(O)).
81
82 :- use_module(probsrc(solver_interface), [valid_solver_option/1]).
83 % Valid options
84 valid_option(disprover_option(X)) :- !,valid_disprover_option(X).
85 ?valid_option(X) :- valid_solver_option(X).
86
87 valid_disprover_option(observe_state).
88 valid_disprover_option(unsat_core).
89 valid_disprover_option(unsat_core_as_machine).
90 valid_disprover_option(export_po_as_machine(_)).
91 valid_disprover_option(setlog_prover_timeout(_)).
92 valid_disprover_option(wd_prover_timeout(_)).
93 valid_disprover_option(po_label(_)).
94 valid_disprover_option(no_constraint_solving). % run only WD prover
95
96 % ----------
97
98 :- use_module(probsrc(error_manager),[add_all_perrors/3]).
99 disprove1(Goal,AllHypotheses,SelectedHypotheses,Opts,Result) :-
100 catch(
101 (disprove2(Goal,AllHypotheses,SelectedHypotheses,Opts,Result) -> true
102 ; add_internal_error('Disprover failed on goal:',Goal),
103 fail),
104 type_errors(E),
105 (add_all_perrors(E,[],disprover), Result=type_error)).
106
107 %catch_internal_timeout(ok,contradiction_found,Out) :-
108 % error_manager:check_warning_occured(warning(_X),Y),
109 % is_time_out_message(Y), % Sebastian: why do we catch this ?? This is not necessarily a problem ??
110 % !, % Might be a problem for proofs, invalidate only if Result = contradiction_found
111 % Out = time_out. % However, this might be old code that is superseded by the enum warnings
112 %catch_internal_timeout(ok,Result,Result).
113
114 % is_time_out_message(Y) :- atom(Y), atom_codes(Y,Codes),
115 % append("TIME-OUT occurred while ProB was expanding", _, Codes).
116
117 % ProverContext: just a name for statistics
118 solve_disprover_predicate(ProverContext,Predicate,Opts,CBCResult) :-
119 TimeoutFactor = 1,
120 exclude(disprover_option,Opts,SOpts),
121 (member(disprover_option(observe_state),Opts) -> observe_state(State) ; true),
122 start_ms_timer(Timer),
123 (solve_predicate(Predicate,State,TimeoutFactor,SOpts,CBCResult)
124 -> register_prover_result(ProverContext,CBCResult,Timer)
125 ; register_prover_result(ProverContext,failed,Timer),
126 fail
127 ).
128 disprover_option(disprover_option(_)).
129
130
131 :- use_module(wdsrc(well_def_analyser),[prove_sequent/4]).
132 :- use_module(probsrc(tools_meta),[safe_time_out/3]).
133 :- use_module(extrasrc(b2setlog),[prove_using_setlog/5]).
134 prove_using_alternative_prover(setlog_prover,Goal,Hypotheses,Opts,TimeOutMs,Res) :-
135 start_ms_timer(Timer),
136 (prove_using_setlog(Goal,Hypotheses,Opts,TimeOutMs,Res)
137 -> (Res = contradiction_found
138 -> register_prover_result(setlog_prover,contradiction_found,Timer)
139 ; register_prover_result(setlog_prover,Res,Timer),
140 fail
141 )
142 ; register_prover_result(setlog_prover,failed,Timer),
143 fail
144 ).
145 prove_using_alternative_prover(wd_prover,Goal,Hypotheses,Opts,TimeOutMs,contradiction_found) :-
146 % quite often 0 or 1 ms are enough for TimeOutMs
147 ? (member(disprover_option(po_label(Label)),Opts) -> WDOpts=[po_label(Label)] ; WDOpts=[]),
148 debug_format(19,'Starting WD Prover (timeout ~w ms) for \'~w\' ~n',[TimeOutMs,Label]),
149 start_ms_timer(Timer),
150 (safe_time_out(prove_sequent(proving,Goal,Hypotheses,WDOpts),TimeOutMs,TimeOutRes)
151 -> (TimeOutRes=time_out
152 -> format('Timeout in WD Prover (~w ms)~n',[TimeOutMs]),
153 register_prover_result(wd_prover,time_out,Timer), fail
154 ; register_prover_result(wd_prover,contradiction_found,Timer))
155 ; register_prover_result(wd_prover,failed,Timer),
156 fail
157 ).
158
159
160 :- dynamic prover_stats/4, last_prover_stats/4.
161
162 % register the result of a prover for statistics / user feedback
163 % prover_stats/4: all calls since the start or the last reset_prover_result_stats
164 % last_prover_stats/4: the calls of the last (or current) disprove_with_opts call
165 register_prover_result(Prover,Result,Timer) :-
166 functor(Result,RF,_),
167 get_elapsed_timer(Timer,DeltaTimer),
168 (retract(prover_stats(Prover,RF,PrevNr,PrevTime)) -> true ; PrevNr=0, PrevTime=0),
169 NewNr is PrevNr+1,
170 combiner_timer(PrevTime,DeltaTimer,NewTimer),
171 assert(prover_stats(Prover,RF,NewNr,NewTimer)),
172 (retract(last_prover_stats(Prover,RF,LPrevNr,LPrevTime)) -> true ; LPrevNr=0, LPrevTime=0),
173 LNewNr is LPrevNr+1,
174 combiner_timer(LPrevTime,DeltaTimer,LNewTimer),
175 assert(last_prover_stats(Prover,RF,LNewNr,LNewTimer)),
176 stop_ms_timer_with_debug_msg(Timer,prover_result(Prover,RF)).
177
178 % get_prover_result_stats(-Total,-LastCall):
179 % lists of prover_stats(Prover,Result,NrCalls,timer(Runtime,TotRuntime,Walltime)), times in ms;
180 % Total for all calls since the start or the last reset, LastCall for the last disprove_with_opts call
181 get_prover_result_stats(Total,LastCall) :-
182 findall(prover_stats(P,R,N,T),prover_stats(P,R,N,T),Total),
183 findall(prover_stats(P,R,N,T),last_prover_stats(P,R,N,T),LastCall).
184
185 reset_prover_result_stats :-
186 retractall(prover_stats(_,_,_,_)),
187 retractall(last_prover_stats(_,_,_,_)).
188
189 prover_result_stats_available :- \+ \+ prover_stats(_,_,_,_).
190
191 % the statistics as a table (for meta_interface): per prover and result the calls and times
192 % (in ms) of all calls, and those of the last disprove_with_opts call
193 prover_result_stats_table(list([list(['Prover','Result','Calls','Runtime','TotRuntime','Walltime',
194 'LastCalls','LastRuntime','LastTotRuntime','LastWalltime'])|Rows])) :-
195 findall(list([Prover,Result,Nr,Runtime,TotRuntime,Walltime,LNr,LRuntime,LTotRuntime,LWalltime]),
196 (prover_stats(Prover,Result,Nr,timer(Runtime,TotRuntime,Walltime)),
197 (last_prover_stats(Prover,Result,LNr,timer(LRuntime,LTotRuntime,LWalltime)) -> true
198 ; LNr=0, LRuntime=0, LTotRuntime=0, LWalltime=0)),
199 Rows).
200
201 print_prover_result_stats :-
202 \+ \+ prover_stats(_,_,_,_), % at least one call made
203 format('* Disprover statistics about individual prover calls and results:~n',[]),
204 format('~w,~w,~w,~w,~w,~w~n',['Prover','Result','Calls','Runtime','TotRuntime','Walltime']),
205 prover_stats(Prover,Result,Nr,timer(Runtime,TotRuntime,Walltime)),
206 format('~w,~w,~w,~w,~w,~w~n',[Prover,Result,Nr,Runtime,TotRuntime,Walltime]),
207 fail.
208 print_prover_result_stats.
209
210 :- public disprove2/5. % it is called by user_interruptable_call_det above; we should also add a meta_predicate annotation
211 :- use_module(probsrc(bsyntaxtree), [predicate_identifiers/2]).
212 disprove2(TGoal,TAllHypotheses,TSelectedHypotheses,Opts,ResultOut) :-
213 TGoal=(_,pred,_),!, % input is already type-checked
214 disprove3(TGoal,TAllHypotheses,TSelectedHypotheses,Opts,ResultOut).
215 disprove2(Goal,AllHypotheses,SelectedHypotheses,Opts,ResultOut) :-
216 (type_check_in_machine_context([Goal|AllHypotheses],[TGoal|TAllHypotheses]) -> true
217 ; add_error_and_fail(disprover,'Disprover typechecking goal failed')),
218 (type_check_in_machine_context(SelectedHypotheses,TSelectedHypotheses) -> true
219 ; add_error_and_fail(disprover,'Disprover typechecking selected hyps failed')),
220 ? disprove3(TGoal,TAllHypotheses,TSelectedHypotheses,Opts,ResultOut).
221
222
223 disprove3(TGoal,TAllHypotheses,TSelectedHypotheses,Opts,ResultOut) :-
224 % build predicate H1 & H2 .... & not Goal
225 create_negation(TGoal,NegatedGoal),
226 %(debug_mode(on) -> write(NegatedGoal), nl ; true),
227 (debug_mode(on) -> nl,write('Negated Goal:'), nl,
228 translate:nested_print_bexpr_as_classicalb(NegatedGoal),
229 write_hypothesis(TAllHypotheses)
230 ; true),
231 conjunct_predicates([NegatedGoal|TAllHypotheses],Predicate),
232 %tools:print_bt_message(solving),
233 ProofInfos = [disprover_result(final,all_and_selected,ResultOut),options(Opts)|PI1],
234 ( get_alternate_prover_timeout(Opts,Prover,Timeout),
235 Timeout>0,
236 prove_using_alternative_prover(Prover,TGoal,TAllHypotheses,Opts,Timeout,ResultWithAll)
237 % adds 5 extra PO proven in test 1800, and 4 more in test 1447
238 -> % DISCHARGED by WD Prover
239 (debug_mode(on) -> add_message(disprover,'Discharged by alternate prover: ',Prover:Timeout) ; true),
240 PI1 = [disprover_result(Prover,all,ResultWithAll)|PI2]
241 ; member(disprover_option(no_constraint_solving),Opts)
242 % explicitly requested not to constraint-based disprover
243 -> ResultWithAll=no_solution_found(wd_prover_unsuccessful),
244 PI1 = [disprover_result(prob,all,ResultWithAll)|PI2]
245 ; solve_disprover_predicate(prob_all_hyps,Predicate,Opts,ResultWithAll),
246 PI1 = [disprover_result(prob,all,ResultWithAll)|PI2]
247 ),
248 %tools:print_bt_message(solver_result(CBCResult)),
249 debug_println(10,disprover_result_with_all_hypotheses(ResultWithAll)),
250 ? disprove_double_check(ResultWithAll,NegatedGoal,TAllHypotheses,TSelectedHypotheses,Predicate,Opts,ResultOut,PI2),
251 ? (member(disprover_option(export_po_as_machine(File)),Opts)
252 -> export_po_as_machine(File,TAllHypotheses,TSelectedHypotheses,TGoal,ProofInfos)
253 ; true).
254
255 get_alternate_prover_timeout(Opts,Prover,Timeout) :-
256 member(disprover_option(setlog_prover_timeout(TO)),Opts),!,
257 (Prover=wd_prover,member(disprover_option(wd_prover_timeout(Timeout)),Opts) % try WD prover first
258 ;
259 Prover=setlog_prover, TO=Timeout).
260 get_alternate_prover_timeout(Opts,wd_prover,Timeout) :-
261 member(disprover_option(wd_prover_timeout(Timeout)),Opts),!.
262 get_alternate_prover_timeout(_,wd_prover,WDTimeout) :-
263 get_preference(time_out,GlobalTO),
264 WDTimeout is 20 + GlobalTO // 100.
265
266 % res <— solve( All Hyp & not G )
267 % if res = solution then
268 % return CounterExample Found, Goal definitely not provable
269 % else if unfixed deferred set used in selected Hypotheses or Goal then
270 % return Unknown // WE COULD TRY and find counter example here in case there was a time-out?
271 % else if res = fail (without time-out & without enumeration warning & no unfixed deferred set in All Hypotheses) then
272 % return Proof Found, Goal proven
273 % else
274 % res <— solve (Selected Hyp & not G) /* maybe use smaller time-out here ?? */
275 % if res = fail (without time-out & without enumeration warning) then
276 % return Proof Found, Goal proven
277 % else if res = solution then
278 % return CounterExample for selected Hypotheses found, Goal not provable from selected Hypotheses (but may be provable with all Hyp)
279 % else
280 % return Unknown
281 % fi
282 % fi
283
284 % we found a solution / counter example to all hyps no need to double check here.
285 % just clear up the resulting state, keeping only variables occuring in the goal.
286 % although: sometimes it is useful to have other IDs as well, at least in the same component as the Goal !
287 disprove_double_check(solution(X),NegatedGoal,_Hypotheses,_SelectedHypotheses,_,_,ResultOut,ProofInfos) :-
288 predicate_identifiers(NegatedGoal,GoalIdentifiers),
289 findall(binding(Id,Value,PP), get_binding_from_solution(X,GoalIdentifiers,0,Id,Value,PP), FilteredState),
290 ResultOut = solution(FilteredState),
291 ProofInfos = ['Counter example to all hypotheses found'].
292 % check if the unfixed deferred sets occur in selected hyps or goal
293 disprove_double_check(no_solution_found(unfixed_deferred_sets),NegatedGoal,_Hypotheses,SelectedHypotheses,_,_,
294 no_solution_found(unfixed_deferred_sets),ProofInfos) :-
295 find_typed_identifier_uses(NegatedGoal,[],GoalIdentifiers),
296 get_texpr_types(GoalIdentifiers,GoalTypes),
297 find_typed_identifier_uses_l(SelectedHypotheses,[],HypIdentifiers),
298 get_texpr_types(HypIdentifiers,HypTypes),
299 append(GoalTypes,HypTypes,Types),
300 % exclude all types that are not unfixed
301 include(contains_unfixed_deferred_set,Types,UnfixedTypes),
302 % see if an unfixed type is left
303 UnfixedTypes = [_|_],
304 !,
305 ProofInfos = ['Goal or selected hypotheses contain unfixed deferred sets', unfixed(UnfixedTypes)],
306 !.
307 % contradiction was found, i.e. there was neither a timeout nor an enumeration warning
308 disprove_double_check(contradiction_found,NegatedGoal,Hypotheses,SelectedHypotheses,FullPredicate,Opts,
309 FinalResult,ProofInfos) :-
310 ProofInfos = ['PO is proven using all hypotheses, no timeout or enumeration warning occured'],
311 %add_hit(po_discharged,prob_all),
312 (get_preference(double_evaluation_when_analysing,true),
313 nonmember(disprover_option(no_constraint_solving),Opts) % double check done with constraint solver
314 -> % check if we can also prove negation of goal, meaning Hypotheses are inconsistent
315 check_negated(NegatedGoal,Hypotheses,SelectedHypotheses,FullPredicate,Opts,FinalResult)
316 ; FinalResult = contradiction_found).
317 disprove_double_check(Result,_NegatedGoal,Hypotheses,SelectedHypotheses,_,_Opts,ResultOut,ProofInfos) :-
318 same_conjunction_list(Hypotheses,SelectedHypotheses),!,
319 ProofInfos = ['Selected hypotheses identical to all hypotheses'],
320 Result=ResultOut.
321 % see what solving only selected hypotheses & not G gives us
322 disprove_double_check(no_solution_found(wd_prover_unsuccessful),_NegGoal,_Hyp,_SHyp,_,_Opts,ResultOut,ProofInfos) :- !,
323 ResultOut=no_solution_found(wd_prover_unsuccessful), ProofInfos=[]. % do not run solver on selected hyps
324 disprove_double_check(_OldResult,NegatedGoal,Hypotheses,SelectedHypotheses,_,Opts,ResultOut,ProofInfos) :-
325 % No proof found with all hypotheses, now try selected ones only
326 % reset existing enumeration warnings,
327 % that might have been generated in a previous run
328 reset_errors,
329 (debug_mode(on)
330 -> nl,write('Trying to use only selected hypotheses.\nNegated Goal:'), nl,
331 translate:nested_print_bexpr_as_classicalb(NegatedGoal),
332 write_hypothesis(SelectedHypotheses)
333 ; true),
334 conjunct_predicates([NegatedGoal|SelectedHypotheses],Predicate),
335 %external_functions:observe_state(State),
336 solve_disprover_predicate(prob_selected_hyps,Predicate,Opts,Result),
337 ProofInfos = [disprover_result(prob,selected,Result)],
338 !,
339 disprove_triple_check(Result,NegatedGoal,Hypotheses,SelectedHypotheses,Predicate,Opts,ResultOut).
340
341
342 % we still have a contradiction -> double check it if preference set
343 disprove_triple_check(contradiction_found,NegatedGoal,Hypotheses,SelectedHypotheses,FullPredicate,Opts,FinalResult) :-
344 %add_hit(po_discharged,prob_selected),
345 (get_preference(double_evaluation_when_analysing,true) % preference set by Rodin Disprover
346 -> check_negated(NegatedGoal,Hypotheses,SelectedHypotheses,FullPredicate,Opts,FinalResult)
347 ; FinalResult = contradiction_found,
348 compute_unsat_core_if_requested(FullPredicate,Opts)
349 ).
350 disprove_triple_check(solution(X),NegatedGoal,_,_,_FullPredicate,_,ResultOut) :-
351 % Counter-Example to Goal for selected Hypotheses found
352 predicate_identifiers(NegatedGoal,GoalIdentifiers),
353 findall(binding(Id,Value,PP), get_binding_from_solution(X,GoalIdentifiers,10,Id,Value,PP), FilteredState),
354 ResultOut = solution_on_selected_hypotheses(FilteredState).
355 disprove_triple_check(time_out,_,_,_,_,_,time_out). % TODO: We could try Kodkod here!
356 disprove_triple_check(no_solution_found(X),_,_,_,_,_,no_solution_found(X)). % Or here?
357
358 :- use_module(library(ordsets),[ord_member/2]).
359 % get bindings from solution; showing all identifiers from goal and a few others as indicated by MaxIndexNonGoal
360 get_binding_from_solution(Solution,GoalIdentifiers,MaxIndexNonGoal,Id,Value,PP) :- MaxIndexNonGoal>=0,
361 sort(GoalIdentifiers,SG),
362 ? nth1(Nr,Solution,binding(Id,Value,PP)),
363 (Nr =< MaxIndexNonGoal -> true ; ord_member(Id,SG)).
364 % TO DO: rather than limiting this way, we could analyse the variables which are in the same component
365 % either by calling predicate_components or by storing component info in
366
367 check_negated(NegatedGoal,Hypotheses,_,FullOrigPredicate,Opts,ResultOut) :-
368 create_negation(NegatedGoal,Goal),
369 conjunct_predicates([Goal|Hypotheses],Predicate),
370 %tools:print_bt_message(check_negated),
371 debug_println(20,'Check whether we can also prove negation of goal'),
372 solve_disprover_predicate(prob_check_negated_goal,Predicate,Opts,Result),
373 %% tools:print_bt_message(solver_double_check_result(Result)),
374 !,
375 (Result = contradiction_found % proven both (H & not G) and (H & G)
376 -> ResultOut = contradiction_in_hypotheses,
377 translate:translate_bexpression_with_limit(NegatedGoal,GS),
378 add_warning(disprover_inconsistent_hypotheses,
379 'Disprover double check result: Contradiction in hypotheses (or Bug) detected for goal: ',
380 GS), %% check_negated(NegatedGoal,Hypotheses))
381 % translate:nested_print_bexpr_as_classicalb(Hypotheses),nl,nl,
382 compute_unsat_core_if_requested(Predicate,Opts)
383 % ; Result=solution(Bindings), format('Solution for negated goal:~n',[]), member(B,Bindings), print_binding(B),fail
384 ; ResultOut = contradiction_found,
385 compute_unsat_core_if_requested(FullOrigPredicate,Opts)
386 ).
387
388 %print_binding(binding(Var,_,PPV)) :- format(' ~w = ~w~n',[Var,PPV]).
389
390 % ----------
391 % visualising a counter-example (for ProB for Rodin's disprover notice)
392
393 :- use_module(probsrc(bsyntaxtree), [create_implication/3, create_equality/3, create_texpr/4,
394 get_texpr_id/2, get_texpr_type/2]).
395 :- use_module(dotsrc(bvisual),[get_tree_from_expr/4, write_dot_graph_to_file/2]).
396
397 % disprover_counter_example_dot(+Goal,+Hypotheses,+Bindings,+Timeout,+DotFile)
398 % writes the formula tree of Hypotheses => Goal, evaluated in a counter-example, as a dot file.
399 % Goal and Hypotheses are raw ASTs (as for disprove/5) or typed predicates;
400 % Bindings are the binding(Id,Value,PP) terms of a counter-example found by disprove/5, i.e.
401 % solution(Bindings) or solution_on_selected_hypotheses(Bindings); they are filtered there
402 % (e.g. only the goal's identifiers), so the other identifiers' values are found by solving
403 % Hypotheses & not(Goal) again with these values fixed (with the time-out Timeout in ms).
404 % If that fails, only the given Bindings are used.
405 disprover_counter_example_dot(Goal,Hypotheses,Bindings,Timeout,DotFile) :-
406 (Goal = b(_,pred,_) -> TGoal=Goal, THyps=Hypotheses
407 ; type_check_in_machine_context([Goal|Hypotheses],[TGoal|THyps]) -> true
408 ; add_error_and_fail(disprover,'Disprover typechecking for the counter-example visualisation failed')),
409 conjunct_predicates(THyps,HypPred),
410 create_implication(HypPred,TGoal,Sequent),
411 create_negation(TGoal,NegatedGoal),
412 find_typed_identifier_uses_l([TGoal|THyps],[],TIds),
413 findall(Eq,(member(TId,TIds), get_texpr_id(TId,Id),
414 member(binding(Id,Val,_),Bindings),
415 get_texpr_type(TId,Type), create_texpr(value(Val),Type,[],TVal),
416 create_equality(TId,TVal,Eq)), Eqs),
417 append(Eqs,[NegatedGoal|THyps],Conjuncts),
418 conjunct_predicates(Conjuncts,Pred),
419 temporary_set_preference(time_out,Timeout,TC),
420 call_cleanup(counter_example_solution(Pred,Bindings,Solution),
421 reset_temporary_preference(time_out,TC)),
422 findall(bind(Id,Val),member(binding(Id,Val,_),Solution),LocalState),
423 get_tree_from_expr(Tree,Sequent,LocalState,[]),
424 write_dot_graph_to_file(Tree,DotFile).
425
426 counter_example_solution(Pred,_,Solution) :-
427 get_disprover_default_opts(Opts),
428 solve_predicate(Pred,_State,1,Opts,Result),
429 Result = solution(Solution),!.
430 counter_example_solution(_,Bindings,Bindings) :-
431 add_message(disprover,'Could not complete the counter-example for the visualisation, showing only the values found by the disprover','').
432
433 compute_unsat_core_if_requested(Predicate,Opts) :-
434 (member(disprover_option(unsat_core),Opts) -> compute_unsat_core(Opts,Predicate) ; true).
435 :- use_module(probsrc(preferences),[call_with_preference/3]).
436 :- use_module(probsrc(translate),[translate_predicate_into_machine/3,nested_print_bexpr_as_classicalb/1]).
437 compute_unsat_core(Opts,Predicate) :-
438 (member(unsat_core_algorithm/P,Opts)
439 -> call_with_preference(compute_unsat_core_aux(Predicate,CorePredicate),unsat_core_algorithm,P)
440 ; compute_unsat_core_aux(Predicate,CorePredicate)),
441 !,
442 print('UNSAT CORE: '),nl,print('--------'),nl,
443 (member(disprover_option(unsat_core_as_machine),Opts)
444 -> translate_predicate_into_machine(CorePredicate,'UnsatCore',ResultAtom),
445 write(ResultAtom),nl
446 ; nested_print_bexpr_as_classicalb(CorePredicate)),
447 nl, print('--------'),nl.
448
449 compute_unsat_core_aux(Predicate,CorePredicate) :-
450 print('Computing UNSAT CORE'),nl,
451 unsat_core(Predicate,CorePredicate). % from unsat_cores
452
453 :- use_module(probsrc(bsyntaxtree),[same_texpr/2]).
454 % check if two lists of conjuncts represent the same conjunction (modulo info fields)
455 same_conjunction_list(List1,List2) :-
456 same_length(List1,List2,_), % avoid possibly expensive comparisons if lists clearly different
457 % we could strip info field and the sort; but here we just want to catch the obvious case where the disprover is called with SelectedHyp = AllHyp
458 maplist(same_texpr,List1,List2).
459
460 :- use_module(probsrc(translate),[nested_print_sequent_as_classicalb/6,nested_print_bexpr_as_classicalb/1]).
461 write_hypothesis([]).
462 write_hypothesis([Hyp|Hyps]) :-
463 write(' & /* Hypothesis: */ '), nl,
464 %(debug_mode(on) -> write(Hyp), nl ; true),
465 nested_print_bexpr_as_classicalb(Hyp),
466 write_hypothesis(Hyps).
467
468 :- use_module(probsrc(tools_io),[safe_intelligent_open_file/3]).
469 export_po_as_machine(Filename,THypothesesList,TSelectedHypothesesList,TGoal,Infos) :-
470 %nested_print_sequent_as_classicalb(Stream,THypothesesList,TGoal,'ProofObligation_AllHyps',Infos),
471 format(user_output,'Exporting selected hypotheses to B machine: ~w~n',[Filename]),
472 safe_intelligent_open_file(Filename,write,Stream),
473 nested_print_sequent_as_classicalb(Stream,TSelectedHypothesesList,TGoal,
474 THypothesesList,'ProB_Rodin_PO_SelectedHyps',Infos),
475 close(Stream).
476