author | blanchet |
Tue, 03 Jun 2014 11:43:07 +0200 | |
changeset 57165 | 7b1bf424ec5f |
parent 57164 | eb5f27ec3987 |
child 57229 | 489083abce44 |
permissions | -rw-r--r-- |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
1 |
(* Title: HOL/Tools/SMT2/smt2_solver.ML |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
2 |
Author: Sascha Boehme, TU Muenchen |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
3 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
4 |
SMT solvers registry and SMT tactic. |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
5 |
*) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
6 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
7 |
signature SMT2_SOLVER = |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
8 |
sig |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
9 |
(*configuration*) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
10 |
datatype outcome = Unsat | Sat | Unknown |
57163 | 11 |
|
12 |
type parsed_proof = |
|
13 |
{outcome: SMT2_Failure.failure option, |
|
14 |
fact_ids: (int * ((string * ATP_Problem_Generate.stature) * thm)) list, |
|
15 |
atp_proof: unit -> (term, string) ATP_Proof.atp_step list} |
|
16 |
||
17 |
type solver_config = |
|
18 |
{name: string, |
|
19 |
class: Proof.context -> SMT2_Util.class, |
|
20 |
avail: unit -> bool, |
|
21 |
command: unit -> string list, |
|
22 |
options: Proof.context -> string list, |
|
23 |
default_max_relevant: int, |
|
24 |
outcome: string -> string list -> outcome * string list, |
|
57164 | 25 |
parse_proof: (Proof.context -> SMT2_Translate.replay_data -> |
26 |
((string * ATP_Problem_Generate.stature) * thm) list -> term list -> term -> string list -> |
|
27 |
parsed_proof) option, |
|
57163 | 28 |
replay: (Proof.context -> SMT2_Translate.replay_data -> string list -> thm) option} |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
29 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
30 |
(*registry*) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
31 |
val add_solver: solver_config -> theory -> theory |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
32 |
val default_max_relevant: Proof.context -> string -> int |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
33 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
34 |
(*filter*) |
57165
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
35 |
val smt2_filter: Proof.context -> thm -> ((string * ATP_Problem_Generate.stature) * thm) list -> |
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
36 |
int -> Time.time -> parsed_proof |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
37 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
38 |
(*tactic*) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
39 |
val smt2_tac: Proof.context -> thm list -> int -> tactic |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
40 |
val smt2_tac': Proof.context -> thm list -> int -> tactic |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
41 |
end |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
42 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
43 |
structure SMT2_Solver: SMT2_SOLVER = |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
44 |
struct |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
45 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
46 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
47 |
(* interface to external solvers *) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
48 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
49 |
local |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
50 |
|
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
51 |
fun make_command command options problem_path proof_path = |
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
52 |
"(exec 2>&1;" :: map File.shell_quote (command () @ options) @ |
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
53 |
[File.shell_path problem_path, ")", ">", File.shell_path proof_path] |
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
54 |
|> space_implode " " |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
55 |
|
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
56 |
fun with_trace ctxt msg f x = |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
57 |
let val _ = SMT2_Config.trace_msg ctxt (fn () => msg) () |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
58 |
in f x end |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
59 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
60 |
fun run ctxt name mk_cmd input = |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
61 |
(case SMT2_Config.certificates_of ctxt of |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
62 |
NONE => |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
63 |
if not (SMT2_Config.is_available ctxt name) then |
56131 | 64 |
error ("The SMT solver " ^ quote name ^ " is not installed") |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
65 |
else if Config.get ctxt SMT2_Config.debug_files = "" then |
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
66 |
with_trace ctxt ("Invoking SMT solver " ^ quote name ^ " ...") (Cache_IO.run mk_cmd) input |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
67 |
else |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
68 |
let |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
69 |
val base_path = Path.explode (Config.get ctxt SMT2_Config.debug_files) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
70 |
val in_path = Path.ext "smt2_in" base_path |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
71 |
val out_path = Path.ext "smt2_out" base_path |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
72 |
in Cache_IO.raw_run mk_cmd input in_path out_path end |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
73 |
| SOME certs => |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
74 |
(case Cache_IO.lookup certs input of |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
75 |
(NONE, key) => |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
76 |
if Config.get ctxt SMT2_Config.read_only_certificates then |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
77 |
error ("Bad certificate cache: missing certificate") |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
78 |
else |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
79 |
Cache_IO.run_and_cache certs key mk_cmd input |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
80 |
| (SOME output, _) => |
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
81 |
with_trace ctxt ("Using cached certificate from " ^ |
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
82 |
File.shell_path (Cache_IO.cache_path_of certs) ^ " ...") I output)) |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
83 |
|
56087 | 84 |
(* Z3 returns 1 if "get-model" or "get-model" fails *) |
85 |
val normal_return_codes = [0, 1] |
|
86 |
||
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
87 |
fun run_solver ctxt name mk_cmd input = |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
88 |
let |
57164 | 89 |
fun pretty tag lines = Pretty.string_of (Pretty.big_list tag (map Pretty.str lines)) |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
90 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
91 |
val _ = SMT2_Config.trace_msg ctxt (pretty "Problem:" o split_lines) input |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
92 |
|
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
93 |
val {redirected_output = res, output = err, return_code} = |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
94 |
SMT2_Config.with_timeout ctxt (run ctxt name mk_cmd) input |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
95 |
val _ = SMT2_Config.trace_msg ctxt (pretty "Solver:") err |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
96 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
97 |
val output = fst (take_suffix (equal "") res) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
98 |
val _ = SMT2_Config.trace_msg ctxt (pretty "Result:") output |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
99 |
|
56087 | 100 |
val _ = member (op =) normal_return_codes return_code orelse |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
101 |
raise SMT2_Failure.SMT (SMT2_Failure.Abnormal_Termination return_code) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
102 |
in output end |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
103 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
104 |
fun trace_assms ctxt = |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
105 |
SMT2_Config.trace_msg ctxt (Pretty.string_of o |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
106 |
Pretty.big_list "Assertions:" o map (Display.pretty_thm ctxt o snd)) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
107 |
|
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
108 |
fun trace_replay_data ({context = ctxt, typs, terms, ...} : SMT2_Translate.replay_data) = |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
109 |
let |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
110 |
fun pretty_eq n p = Pretty.block [Pretty.str n, Pretty.str " = ", p] |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
111 |
fun p_typ (n, T) = pretty_eq n (Syntax.pretty_typ ctxt T) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
112 |
fun p_term (n, t) = pretty_eq n (Syntax.pretty_term ctxt t) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
113 |
in |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
114 |
SMT2_Config.trace_msg ctxt (fn () => |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
115 |
Pretty.string_of (Pretty.big_list "Names:" [ |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
116 |
Pretty.big_list "sorts:" (map p_typ (Symtab.dest typs)), |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
117 |
Pretty.big_list "functions:" (map p_term (Symtab.dest terms))])) () |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
118 |
end |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
119 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
120 |
in |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
121 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
122 |
fun invoke name command ithms ctxt = |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
123 |
let |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
124 |
val options = SMT2_Config.solver_options_of ctxt |
56112 | 125 |
val comments = [space_implode " " options] |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
126 |
|
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
127 |
val (str, replay_data as {context = ctxt', ...}) = |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
128 |
ithms |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
129 |
|> tap (trace_assms ctxt) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
130 |
|> SMT2_Translate.translate ctxt comments |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
131 |
||> tap trace_replay_data |
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
132 |
in (run_solver ctxt' name (make_command command options) str, replay_data) end |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
133 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
134 |
end |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
135 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
136 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
137 |
(* configuration *) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
138 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
139 |
datatype outcome = Unsat | Sat | Unknown |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
140 |
|
57163 | 141 |
type parsed_proof = |
142 |
{outcome: SMT2_Failure.failure option, |
|
143 |
fact_ids: (int * ((string * ATP_Problem_Generate.stature) * thm)) list, |
|
144 |
atp_proof: unit -> (term, string) ATP_Proof.atp_step list} |
|
145 |
||
146 |
type solver_config = |
|
147 |
{name: string, |
|
148 |
class: Proof.context -> SMT2_Util.class, |
|
149 |
avail: unit -> bool, |
|
150 |
command: unit -> string list, |
|
151 |
options: Proof.context -> string list, |
|
152 |
default_max_relevant: int, |
|
153 |
outcome: string -> string list -> outcome * string list, |
|
57164 | 154 |
parse_proof: (Proof.context -> SMT2_Translate.replay_data -> |
155 |
((string * ATP_Problem_Generate.stature) * thm) list -> term list -> term -> string list -> |
|
156 |
parsed_proof) option, |
|
57163 | 157 |
replay: (Proof.context -> SMT2_Translate.replay_data -> string list -> thm) option} |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
158 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
159 |
|
56106 | 160 |
(* check well-sortedness *) |
161 |
||
162 |
val has_topsort = Term.exists_type (Term.exists_subtype (fn |
|
163 |
TFree (_, []) => true |
|
164 |
| TVar (_, []) => true |
|
165 |
| _ => false)) |
|
166 |
||
167 |
(* top sorts cause problems with atomization *) |
|
168 |
fun check_topsort ctxt thm = |
|
169 |
if has_topsort (Thm.prop_of thm) then (SMT2_Normalize.drop_fact_warning ctxt thm; TrueI) else thm |
|
170 |
||
171 |
||
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
172 |
(* registry *) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
173 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
174 |
type solver_info = { |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
175 |
command: unit -> string list, |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
176 |
default_max_relevant: int, |
57164 | 177 |
parse_proof: Proof.context -> SMT2_Translate.replay_data -> |
178 |
((string * ATP_Problem_Generate.stature) * thm) list -> term list -> term -> string list -> |
|
179 |
parsed_proof, |
|
57157 | 180 |
replay: Proof.context -> SMT2_Translate.replay_data -> string list -> thm} |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
181 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
182 |
structure Solvers = Generic_Data |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
183 |
( |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
184 |
type T = solver_info Symtab.table |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
185 |
val empty = Symtab.empty |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
186 |
val extend = I |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
187 |
fun merge data = Symtab.merge (K true) data |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
188 |
) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
189 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
190 |
local |
57164 | 191 |
fun parse_proof outcome parse_proof0 outer_ctxt replay_data xfacts prems concl output = |
57157 | 192 |
(case outcome output of |
57164 | 193 |
(Unsat, lines) => |
194 |
(case parse_proof0 of |
|
195 |
SOME pp => pp outer_ctxt replay_data xfacts prems concl lines |
|
196 |
| NONE => {outcome = NONE, fact_ids = [], atp_proof = K []}) |
|
57158 | 197 |
| (result, _) => raise SMT2_Failure.SMT (SMT2_Failure.Counterexample (result = Sat))) |
57157 | 198 |
|
199 |
fun replay outcome replay0 oracle outer_ctxt |
|
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
200 |
(replay_data as {context = ctxt, ...} : SMT2_Translate.replay_data) output = |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
201 |
(case outcome output of |
57164 | 202 |
(Unsat, lines) => |
57157 | 203 |
if not (Config.get ctxt SMT2_Config.oracle) andalso is_some replay0 |
57164 | 204 |
then the replay0 outer_ctxt replay_data lines |
57157 | 205 |
else oracle () |
57158 | 206 |
| (result, _) => raise SMT2_Failure.SMT (SMT2_Failure.Counterexample (result = Sat))) |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
207 |
|
57157 | 208 |
val cfalse = Thm.cterm_of @{theory} @{prop False} |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
209 |
in |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
210 |
|
57164 | 211 |
fun add_solver ({name, class, avail, command, options, default_max_relevant, outcome, |
212 |
parse_proof = parse_proof0, replay = replay0} : solver_config) = |
|
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
213 |
let |
57157 | 214 |
fun solver oracle = { |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
215 |
command = command, |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
216 |
default_max_relevant = default_max_relevant, |
57157 | 217 |
parse_proof = parse_proof (outcome name) parse_proof0, |
218 |
replay = replay (outcome name) replay0 oracle} |
|
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
219 |
|
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
220 |
val info = {name = name, class = class, avail = avail, options = options} |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
221 |
in |
57157 | 222 |
Thm.add_oracle (Binding.name name, K cfalse) #-> (fn (_, oracle) => |
223 |
Context.theory_map (Solvers.map (Symtab.update_new (name, solver oracle)))) #> |
|
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
224 |
Context.theory_map (SMT2_Config.add_solver info) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
225 |
end |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
226 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
227 |
end |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
228 |
|
56125 | 229 |
fun get_info ctxt name = the (Symtab.lookup (Solvers.get (Context.Proof ctxt)) name) |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
230 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
231 |
fun name_and_info_of ctxt = |
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
232 |
let val name = SMT2_Config.solver_of ctxt |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
233 |
in (name, get_info ctxt name) end |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
234 |
|
57163 | 235 |
val default_max_relevant = #default_max_relevant oo get_info |
57157 | 236 |
|
57165
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
237 |
fun apply_solver_and_replay ctxt thms0 = |
57157 | 238 |
let |
57165
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
239 |
val thms = map (check_topsort ctxt) thms0 |
57157 | 240 |
val (name, {command, replay, ...}) = name_and_info_of ctxt |
57165
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
241 |
val (output, replay_data) = invoke name command (SMT2_Normalize.normalize ctxt thms) ctxt |
57157 | 242 |
in replay ctxt replay_data output end |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
243 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
244 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
245 |
(* filter *) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
246 |
|
57165
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
247 |
fun smt2_filter ctxt0 goal xfacts i time_limit = |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
248 |
let |
57163 | 249 |
val ctxt = ctxt0 |> Config.put SMT2_Config.timeout (Time.toReal time_limit) |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
250 |
|
56132
64eeda68e693
delayed construction of command (and of noncommercial check) + tuning
blanchet
parents:
56131
diff
changeset
|
251 |
val ({context = ctxt, prems, concl, ...}, _) = Subgoal.focus ctxt i goal |
56106 | 252 |
fun negate ct = Thm.dest_comb ct ||> Thm.apply @{cterm Not} |-> Thm.apply |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
253 |
val cprop = |
56082 | 254 |
(case try negate (Thm.rhs_of (SMT2_Normalize.atomize_conv ctxt concl)) of |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
255 |
SOME ct => ct |
56094 | 256 |
| NONE => raise SMT2_Failure.SMT (SMT2_Failure.Other_Failure "goal is not a HOL term")) |
56082 | 257 |
|
57165
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
258 |
val conjecture = Thm.assume cprop |
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
259 |
val facts = map snd xfacts |
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
260 |
val thms = conjecture :: prems @ facts |
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
261 |
val thms' = map (check_topsort ctxt) thms |
57159 | 262 |
|
57164 | 263 |
val (name, {command, parse_proof, ...}) = name_and_info_of ctxt |
57165
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
264 |
val (output, replay_data) = invoke name command (SMT2_Normalize.normalize ctxt thms') ctxt |
57164 | 265 |
in |
266 |
parse_proof ctxt replay_data xfacts (map Thm.prop_of prems) (Thm.term_of concl) output |
|
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
267 |
end |
57159 | 268 |
handle SMT2_Failure.SMT fail => {outcome = SOME fail, fact_ids = [], atp_proof = K []} |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
269 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
270 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
271 |
(* SMT tactic *) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
272 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
273 |
local |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
274 |
fun str_of ctxt fail = |
57158 | 275 |
"Solver " ^ SMT2_Config.solver_of ctxt ^ ": " ^ SMT2_Failure.string_of_failure fail |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
276 |
|
57165
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
277 |
fun safe_solve ctxt facts = SOME (apply_solver_and_replay ctxt facts) |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
278 |
handle |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
279 |
SMT2_Failure.SMT (fail as SMT2_Failure.Counterexample _) => |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
280 |
(SMT2_Config.verbose_msg ctxt (str_of ctxt) fail; NONE) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
281 |
| SMT2_Failure.SMT (fail as SMT2_Failure.Time_Out) => |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
282 |
error ("SMT: Solver " ^ quote (SMT2_Config.solver_of ctxt) ^ ": " ^ |
57158 | 283 |
SMT2_Failure.string_of_failure fail ^ " (setting the " ^ |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
284 |
"configuration option " ^ quote (Config.name_of SMT2_Config.timeout) ^ " might help)") |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
285 |
| SMT2_Failure.SMT fail => error (str_of ctxt fail) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
286 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
287 |
fun resolve (SOME thm) = rtac thm 1 |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
288 |
| resolve NONE = no_tac |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
289 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
290 |
fun tac prove ctxt rules = |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
291 |
CONVERSION (SMT2_Normalize.atomize_conv ctxt) |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
292 |
THEN' rtac @{thm ccontr} |
57165
7b1bf424ec5f
removed SMT weights -- their impact was very inconclusive anyway
blanchet
parents:
57164
diff
changeset
|
293 |
THEN' SUBPROOF (fn {context = ctxt, prems, ...} => resolve (prove ctxt (rules @ prems))) ctxt |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
294 |
in |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
295 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
296 |
val smt2_tac = tac safe_solve |
57157 | 297 |
val smt2_tac' = tac (SOME oo apply_solver_and_replay) |
56078
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
298 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
299 |
end |
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
300 |
|
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
blanchet
parents:
diff
changeset
|
301 |
end |