64389
|
1 |
(* Title: HOL/Nunchaku/Tools/nunchaku.ML
|
|
2 |
Author: Jasmin Blanchette, Inria Nancy, LORIA, MPII
|
|
3 |
Copyright 2015, 2016
|
|
4 |
|
|
5 |
The core of the Nunchaku integration in Isabelle.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature NUNCHAKU =
|
|
9 |
sig
|
|
10 |
type isa_model = Nunchaku_Reconstruct.isa_model
|
|
11 |
|
|
12 |
datatype mode = Auto_Try | Try | Normal
|
|
13 |
|
|
14 |
type mode_of_operation_params =
|
|
15 |
{falsify: bool,
|
|
16 |
assms: bool,
|
|
17 |
spy: bool,
|
|
18 |
overlord: bool,
|
|
19 |
expect: string}
|
|
20 |
|
|
21 |
type scope_of_search_params =
|
|
22 |
{wfs: ((string * typ) option * bool option) list,
|
|
23 |
whacks: (term option * bool) list,
|
|
24 |
cards: (typ option * (int option * int option)) list,
|
|
25 |
monos: (typ option * bool option) list}
|
|
26 |
|
|
27 |
type output_format_params =
|
|
28 |
{verbose: bool,
|
|
29 |
debug: bool,
|
|
30 |
max_potential: int,
|
|
31 |
max_genuine: int,
|
|
32 |
evals: term list,
|
|
33 |
atomss: (typ option * string list) list}
|
|
34 |
|
|
35 |
type optimization_params =
|
|
36 |
{specialize: bool,
|
|
37 |
multithread: bool}
|
|
38 |
|
|
39 |
type timeout_params =
|
|
40 |
{timeout: Time.time,
|
|
41 |
wf_timeout: Time.time}
|
|
42 |
|
|
43 |
type params =
|
|
44 |
{mode_of_operation_params: mode_of_operation_params,
|
|
45 |
scope_of_search_params: scope_of_search_params,
|
|
46 |
output_format_params: output_format_params,
|
|
47 |
optimization_params: optimization_params,
|
|
48 |
timeout_params: timeout_params}
|
|
49 |
|
|
50 |
val genuineN: string
|
|
51 |
val quasi_genuineN: string
|
|
52 |
val potentialN: string
|
|
53 |
val noneN: string
|
|
54 |
val unknownN: string
|
|
55 |
val no_nunchakuN: string
|
|
56 |
|
|
57 |
val run_chaku_on_prop: Proof.state -> params -> mode -> int -> term list -> term ->
|
|
58 |
string * isa_model option
|
|
59 |
val run_chaku_on_subgoal: Proof.state -> params -> mode -> int -> string * isa_model option
|
|
60 |
end;
|
|
61 |
|
|
62 |
structure Nunchaku : NUNCHAKU =
|
|
63 |
struct
|
|
64 |
|
|
65 |
open Nunchaku_Util;
|
|
66 |
open Nunchaku_Collect;
|
|
67 |
open Nunchaku_Problem;
|
|
68 |
open Nunchaku_Translate;
|
|
69 |
open Nunchaku_Model;
|
|
70 |
open Nunchaku_Reconstruct;
|
|
71 |
open Nunchaku_Display;
|
|
72 |
open Nunchaku_Tool;
|
|
73 |
|
|
74 |
datatype mode = Auto_Try | Try | Normal;
|
|
75 |
|
|
76 |
type mode_of_operation_params =
|
|
77 |
{falsify: bool,
|
|
78 |
assms: bool,
|
|
79 |
spy: bool,
|
|
80 |
overlord: bool,
|
|
81 |
expect: string};
|
|
82 |
|
|
83 |
type scope_of_search_params =
|
|
84 |
{wfs: ((string * typ) option * bool option) list,
|
|
85 |
whacks: (term option * bool) list,
|
|
86 |
cards: (typ option * (int option * int option)) list,
|
|
87 |
monos: (typ option * bool option) list};
|
|
88 |
|
|
89 |
type output_format_params =
|
|
90 |
{verbose: bool,
|
|
91 |
debug: bool,
|
|
92 |
max_potential: int,
|
|
93 |
max_genuine: int,
|
|
94 |
evals: term list,
|
|
95 |
atomss: (typ option * string list) list};
|
|
96 |
|
|
97 |
type optimization_params =
|
|
98 |
{specialize: bool,
|
|
99 |
multithread: bool};
|
|
100 |
|
|
101 |
type timeout_params =
|
|
102 |
{timeout: Time.time,
|
|
103 |
wf_timeout: Time.time};
|
|
104 |
|
|
105 |
type params =
|
|
106 |
{mode_of_operation_params: mode_of_operation_params,
|
|
107 |
scope_of_search_params: scope_of_search_params,
|
|
108 |
output_format_params: output_format_params,
|
|
109 |
optimization_params: optimization_params,
|
|
110 |
timeout_params: timeout_params};
|
|
111 |
|
|
112 |
val genuineN = "genuine";
|
|
113 |
val quasi_genuineN = "quasi_genuine";
|
|
114 |
val potentialN = "potential";
|
|
115 |
val noneN = "none";
|
|
116 |
val unknownN = "unknown";
|
|
117 |
|
|
118 |
val no_nunchakuN = "no_nunchaku";
|
|
119 |
|
|
120 |
fun str_of_mode Auto_Try = "Auto_Try"
|
|
121 |
| str_of_mode Try = "Try"
|
|
122 |
| str_of_mode Normal = "Normal";
|
|
123 |
|
|
124 |
fun none_true assigns = forall (curry (op <>) (SOME true) o snd) assigns;
|
|
125 |
|
|
126 |
fun has_lonely_bool_var (@{const Pure.conjunction} $ (@{const Trueprop} $ Free _) $ _) = true
|
|
127 |
| has_lonely_bool_var _ = false;
|
|
128 |
|
|
129 |
val syntactic_sorts =
|
|
130 |
@{sort "{default,zero,one,plus,minus,uminus,times,inverse,abs,sgn,ord,equal}"} @ @{sort numeral};
|
|
131 |
|
|
132 |
fun has_tfree_syntactic_sort (TFree (_, S as _ :: _)) = subset (op =) (S, syntactic_sorts)
|
|
133 |
| has_tfree_syntactic_sort _ = false;
|
|
134 |
|
|
135 |
val has_syntactic_sorts = exists_type (exists_subtype has_tfree_syntactic_sort);
|
|
136 |
|
|
137 |
(* Give the soft timeout a chance. *)
|
|
138 |
val timeout_slack = seconds 1.0;
|
|
139 |
|
|
140 |
fun run_chaku_on_prop state
|
|
141 |
({mode_of_operation_params = {falsify, assms, spy, overlord, expect},
|
|
142 |
scope_of_search_params = {wfs, whacks, cards, monos},
|
|
143 |
output_format_params = {verbose, debug, evals, atomss, ...},
|
|
144 |
optimization_params = {specialize, ...},
|
|
145 |
timeout_params = {timeout, wf_timeout}})
|
|
146 |
mode i all_assms subgoal =
|
|
147 |
let
|
|
148 |
val ctxt = Proof.context_of state;
|
|
149 |
|
|
150 |
val timer = Timer.startRealTimer ()
|
|
151 |
|
|
152 |
val print = writeln;
|
|
153 |
val print_n = if mode = Normal then writeln else K ();
|
|
154 |
fun print_v f = if verbose then writeln (f ()) else ();
|
|
155 |
fun print_d f = if debug then writeln (f ()) else ();
|
|
156 |
|
|
157 |
val das_wort_Model = if falsify then "Countermodel" else "Model";
|
|
158 |
val das_wort_model = if falsify then "countermodel" else "model";
|
|
159 |
|
|
160 |
val tool_params = {overlord = overlord, debug = debug, specialize = specialize,
|
|
161 |
timeout = timeout};
|
|
162 |
|
|
163 |
fun run () =
|
|
164 |
let
|
|
165 |
val outcome as (outcome_code, _) =
|
|
166 |
let
|
|
167 |
val (poly_axioms, isa_problem as {sound, complete, ...}) =
|
|
168 |
isa_problem_of_subgoal ctxt falsify wfs whacks cards debug wf_timeout evals
|
|
169 |
(if assms then all_assms else []) subgoal;
|
|
170 |
val _ = print_d (fn () => "*** Isabelle problem ***\n" ^
|
|
171 |
str_of_isa_problem ctxt isa_problem);
|
|
172 |
val ugly_nun_problem = nun_problem_of_isa ctxt isa_problem;
|
|
173 |
val _ = print_d (fn () => "*** Ugly Nunchaku problem ***\n" ^
|
|
174 |
str_of_nun_problem ugly_nun_problem);
|
|
175 |
val (nice_nun_problem, pool) = nice_nun_problem ugly_nun_problem;
|
|
176 |
val _ = print_d (fn () => "*** Nice Nunchaku problem ***\n" ^
|
|
177 |
str_of_nun_problem nice_nun_problem);
|
|
178 |
|
|
179 |
fun print_any_hints () =
|
|
180 |
if has_lonely_bool_var subgoal then
|
|
181 |
print "Hint: Maybe you forgot a colon after the lemma's name?"
|
|
182 |
else if has_syntactic_sorts subgoal then
|
|
183 |
print "Hint: Maybe you forgot a type constraint?"
|
|
184 |
else
|
|
185 |
();
|
|
186 |
|
|
187 |
fun get_isa_model_opt output =
|
|
188 |
let
|
|
189 |
val nice_nun_model = nun_model_of_str output;
|
|
190 |
val _ = print_d (fn () => "*** Nice Nunchaku model ***\n" ^
|
|
191 |
str_of_nun_model nice_nun_model);
|
|
192 |
val ugly_nun_model = ugly_nun_model pool nice_nun_model;
|
|
193 |
val _ = print_d (fn () => "*** Ugly Nunchaku model ***\n" ^
|
|
194 |
str_of_nun_model ugly_nun_model);
|
|
195 |
|
|
196 |
val pat_completes = pat_completes_of_isa_problem isa_problem;
|
|
197 |
val isa_model = isa_model_of_nun ctxt pat_completes atomss ugly_nun_model;
|
|
198 |
val _ = print_d (fn () => "*** Isabelle model ***\n" ^
|
|
199 |
str_of_isa_model ctxt isa_model);
|
|
200 |
in
|
|
201 |
isa_model
|
|
202 |
end;
|
|
203 |
|
|
204 |
fun isa_model_opt output =
|
|
205 |
if debug then SOME (get_isa_model_opt output) else try get_isa_model_opt output;
|
|
206 |
|
|
207 |
val model_str = isa_model_opt #> pretty_of_isa_model_opt ctxt #> Pretty.string_of;
|
|
208 |
|
|
209 |
fun unsat_means_theorem () =
|
|
210 |
null whacks andalso null cards andalso null monos;
|
|
211 |
|
|
212 |
fun unknown () =
|
|
213 |
(print_n ("No " ^ das_wort_model ^ " can be found \<midarrow> the problem lies \
|
|
214 |
\outside Nunchaku's fragment");
|
|
215 |
(unknownN, NONE));
|
|
216 |
|
|
217 |
fun unsat_or_unknown complete =
|
|
218 |
if complete then
|
|
219 |
(print_n ("No " ^ das_wort_model ^ " exists" ^
|
|
220 |
(if falsify andalso unsat_means_theorem () then
|
|
221 |
" \<midarrow> the goal is a theorem"
|
|
222 |
else
|
|
223 |
""));
|
|
224 |
(noneN, NONE))
|
|
225 |
else
|
|
226 |
unknown ();
|
|
227 |
|
|
228 |
fun sat_or_maybe_sat sound output =
|
|
229 |
let val header = if sound then das_wort_Model else "Potential " ^ das_wort_model in
|
|
230 |
(case (null poly_axioms, none_true wfs) of
|
|
231 |
(true, true) =>
|
|
232 |
(print (header ^ ":\n" ^
|
|
233 |
model_str output); print_any_hints ();
|
|
234 |
(genuineN, isa_model_opt output))
|
|
235 |
| (no_poly, no_wf) =>
|
|
236 |
let
|
|
237 |
val ignorings = []
|
|
238 |
|> not no_poly ? cons "polymorphic axioms"
|
|
239 |
|> not no_wf ? cons "unchecked well-foundedness";
|
|
240 |
in
|
|
241 |
(print (header ^ " (ignoring " ^ space_implode " and " ignorings ^ "):\n" ^
|
|
242 |
model_str output ^
|
|
243 |
(if no_poly then
|
|
244 |
""
|
|
245 |
else
|
|
246 |
"\nIgnored axioms:\n" ^
|
|
247 |
cat_lines (map (prefix " " o Syntax.string_of_term ctxt) poly_axioms)));
|
|
248 |
print_any_hints ();
|
|
249 |
(quasi_genuineN, isa_model_opt output))
|
|
250 |
end)
|
|
251 |
end;
|
|
252 |
in
|
|
253 |
(case solve_nun_problem tool_params nice_nun_problem of
|
|
254 |
Unsat => unsat_or_unknown complete
|
|
255 |
| Sat (output, _) => sat_or_maybe_sat sound output
|
|
256 |
| Unknown NONE => unknown ()
|
|
257 |
| Unknown (SOME (output, _)) => sat_or_maybe_sat false output
|
|
258 |
| Timeout => (print_n "Time out"; (unknownN, NONE))
|
|
259 |
| Nunchaku_Var_Not_Set =>
|
|
260 |
(print_n ("Variable $" ^ nunchaku_env_var ^ " not set"); (unknownN, NONE))
|
|
261 |
| Nunchaku_Cannot_Execute =>
|
|
262 |
(print_n "External tool \"nunchaku\" cannot execute"; (unknownN, NONE))
|
|
263 |
| Nunchaku_Not_Found =>
|
|
264 |
(print_n "External tool \"nunchaku\" not found"; (unknownN, NONE))
|
|
265 |
| CVC4_Cannot_Execute =>
|
|
266 |
(print_n "External tool \"cvc4\" cannot execute"; (unknownN, NONE))
|
|
267 |
| CVC4_Not_Found => (print_n "External tool \"cvc4\" not found"; (unknownN, NONE))
|
|
268 |
| Unknown_Error (code, msg) =>
|
|
269 |
(print_n ("Unknown error: " ^ msg ^
|
|
270 |
(if code = 0 then "" else " (code " ^ string_of_int code ^ ")"));
|
|
271 |
(unknownN, NONE)))
|
|
272 |
end
|
|
273 |
handle
|
|
274 |
CYCLIC_DEPS () =>
|
|
275 |
(print_n "Cyclic dependencies (or bug in Nunchaku)"; (unknownN, NONE))
|
|
276 |
| TOO_DEEP_DEPS () =>
|
|
277 |
(print_n "Too deep dependencies (or bug in Nunchaku)"; (unknownN, NONE))
|
|
278 |
| TOO_META t =>
|
|
279 |
(print_n ("Formula too meta for Nunchaku:\n" ^ Syntax.string_of_term ctxt t);
|
|
280 |
(unknownN, NONE))
|
|
281 |
| UNEXPECTED_POLYMORPHISM t =>
|
|
282 |
(print_n ("Unexpected polymorphism in term\n" ^ Syntax.string_of_term ctxt t);
|
|
283 |
(unknownN, NONE))
|
|
284 |
| UNEXPECTED_VAR t =>
|
|
285 |
(print_n ("Unexpected schematic variables in term\n" ^ Syntax.string_of_term ctxt t);
|
|
286 |
(unknownN, NONE))
|
|
287 |
| UNSUPPORTED_FUNC t =>
|
|
288 |
(print_n ("Unsupported low-level constant in problem: " ^ Syntax.string_of_term ctxt t);
|
|
289 |
(unknownN, NONE));
|
|
290 |
in
|
|
291 |
if expect = "" orelse outcome_code = expect then outcome
|
|
292 |
else error ("Unexpected outcome: " ^ quote outcome_code)
|
|
293 |
end;
|
|
294 |
|
|
295 |
val _ = spying spy (fn () => (state, i, "starting " ^ str_of_mode mode ^ " mode"));
|
|
296 |
|
|
297 |
val outcome as (outcome_code, _) =
|
|
298 |
Timeout.apply (Time.+ (timeout, timeout_slack)) run ()
|
|
299 |
handle Timeout.TIMEOUT _ => (print_n "Time out"; (unknownN, NONE));
|
|
300 |
|
|
301 |
val _ = print_v (fn () => "Total time: " ^ string_of_time (Timer.checkRealTimer timer));
|
|
302 |
|
|
303 |
val _ = spying spy (fn () => (state, i, "outcome: " ^ outcome_code));
|
|
304 |
in
|
|
305 |
if expect = "" orelse outcome_code = expect then outcome
|
|
306 |
else error ("Unexpected outcome: " ^ quote outcome_code)
|
|
307 |
end;
|
|
308 |
|
|
309 |
fun run_chaku_on_subgoal state params mode i =
|
|
310 |
let
|
|
311 |
val ctxt = Proof.context_of state;
|
|
312 |
val goal = Thm.prop_of (#goal (Proof.raw_goal state));
|
|
313 |
in
|
|
314 |
if Logic.count_prems goal = 0 then
|
|
315 |
(writeln "No subgoal!"; (noneN, NONE))
|
|
316 |
else
|
|
317 |
let
|
|
318 |
val subgoal = fst (Logic.goal_params goal i);
|
|
319 |
val all_assms = map Thm.term_of (Assumption.all_assms_of ctxt);
|
|
320 |
in
|
|
321 |
run_chaku_on_prop state params mode i all_assms subgoal
|
|
322 |
end
|
|
323 |
end;
|
|
324 |
|
|
325 |
end;
|