author | blanchet |
Fri, 03 Dec 2010 18:29:14 +0100 | |
changeset 40941 | a3e6f8634a11 |
parent 40369 | 53dca3bd4250 |
child 41087 | d7b5fd465198 |
permissions | -rw-r--r-- |
38892 | 1 |
(* Title: HOL/Mirabelle/Tools/mirabelle_sledgehammer_filter.ML |
2 |
Author: Jasmin Blanchette, TU Munich |
|
3 |
*) |
|
4 |
||
5 |
structure Mirabelle_Sledgehammer_Filter : MIRABELLE_ACTION = |
|
6 |
struct |
|
7 |
||
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
8 |
fun get args name default_value = |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
9 |
case AList.lookup (op =) args name of |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
10 |
SOME value => the (Real.fromString value) |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
11 |
| NONE => default_value |
38992
542474156c66
introduce fudge factors to deal with "theory const"
blanchet
parents:
38988
diff
changeset
|
12 |
|
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
13 |
fun extract_relevance_fudge args |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
14 |
{worse_irrel_freq, higher_order_irrel_weight, abs_rel_weight, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
15 |
abs_irrel_weight, skolem_irrel_weight, theory_const_rel_weight, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
16 |
theory_const_irrel_weight, intro_bonus, elim_bonus, simp_bonus, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
17 |
local_bonus, assum_bonus, chained_bonus, max_imperfect, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
18 |
max_imperfect_exp, threshold_divisor, ridiculous_threshold} = |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
19 |
{worse_irrel_freq = get args "worse_irrel_freq" worse_irrel_freq, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
20 |
higher_order_irrel_weight = |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
21 |
get args "higher_order_irrel_weight" higher_order_irrel_weight, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
22 |
abs_rel_weight = get args "abs_rel_weight" abs_rel_weight, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
23 |
abs_irrel_weight = get args "abs_irrel_weight" abs_irrel_weight, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
24 |
skolem_irrel_weight = get args "skolem_irrel_weight" skolem_irrel_weight, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
25 |
theory_const_rel_weight = |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
26 |
get args "theory_const_rel_weight" theory_const_rel_weight, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
27 |
theory_const_irrel_weight = |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
28 |
get args "theory_const_irrel_weight" theory_const_irrel_weight, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
29 |
intro_bonus = get args "intro_bonus" intro_bonus, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
30 |
elim_bonus = get args "elim_bonus" elim_bonus, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
31 |
simp_bonus = get args "simp_bonus" simp_bonus, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
32 |
local_bonus = get args "local_bonus" local_bonus, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
33 |
assum_bonus = get args "assum_bonus" assum_bonus, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
34 |
chained_bonus = get args "chained_bonus" chained_bonus, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
35 |
max_imperfect = get args "max_imperfect" max_imperfect, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
36 |
max_imperfect_exp = get args "max_imperfect_exp" max_imperfect_exp, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
37 |
threshold_divisor = get args "threshold_divisor" threshold_divisor, |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
38 |
ridiculous_threshold = get args "ridiculous_threshold" ridiculous_threshold} |
38902 | 39 |
|
38892 | 40 |
structure Prooftab = |
41 |
Table(type key = int * int val ord = prod_ord int_ord int_ord); |
|
42 |
||
43 |
val proof_table = Unsynchronized.ref Prooftab.empty |
|
44 |
||
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
45 |
val num_successes = Unsynchronized.ref ([] : (int * int) list) |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
46 |
val num_failures = Unsynchronized.ref ([] : (int * int) list) |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
47 |
val num_found_proofs = Unsynchronized.ref ([] : (int * int) list) |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
48 |
val num_lost_proofs = Unsynchronized.ref ([] : (int * int) list) |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
49 |
val num_found_facts = Unsynchronized.ref ([] : (int * int) list) |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
50 |
val num_lost_facts = Unsynchronized.ref ([] : (int * int) list) |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
51 |
|
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
52 |
fun get id c = the_default 0 (AList.lookup (op =) (!c) id) |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
53 |
fun add id c n = |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
54 |
c := (case AList.lookup (op =) (!c) id of |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
55 |
SOME m => AList.update (op =) (id, m + n) (!c) |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
56 |
| NONE => (id, n) :: !c) |
38894 | 57 |
|
58 |
fun init proof_file _ thy = |
|
38892 | 59 |
let |
60 |
fun do_line line = |
|
61 |
case line |> space_explode ":" of |
|
62 |
[line_num, col_num, proof] => |
|
63 |
SOME (pairself (the o Int.fromString) (line_num, col_num), |
|
64 |
proof |> space_explode " " |> filter_out (curry (op =) "")) |
|
65 |
| _ => NONE |
|
38894 | 66 |
val proofs = File.read (Path.explode proof_file) |
38892 | 67 |
val proof_tab = |
68 |
proofs |> space_explode "\n" |
|
69 |
|> map_filter do_line |
|
70 |
|> AList.coalesce (op =) |
|
71 |
|> Prooftab.make |
|
72 |
in proof_table := proof_tab; thy end |
|
73 |
||
38894 | 74 |
fun percentage a b = if b = 0 then "N/A" else string_of_int (a * 100 div b) |
75 |
fun percentage_alt a b = percentage a (a + b) |
|
76 |
||
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
77 |
fun done id ({log, ...} : Mirabelle.done_args) = |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
78 |
if get id num_successes + get id num_failures > 0 then |
38897 | 79 |
(log ""; |
80 |
log ("Number of overall successes: " ^ |
|
81 |
string_of_int (get id num_successes)); |
|
82 |
log ("Number of overall failures: " ^ string_of_int (get id num_failures)); |
|
38894 | 83 |
log ("Overall success rate: " ^ |
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
84 |
percentage_alt (get id num_successes) (get id num_failures) ^ "%"); |
38897 | 85 |
log ("Number of found proofs: " ^ string_of_int (get id num_found_proofs)); |
86 |
log ("Number of lost proofs: " ^ string_of_int (get id num_lost_proofs)); |
|
38894 | 87 |
log ("Proof found rate: " ^ |
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
88 |
percentage_alt (get id num_found_proofs) (get id num_lost_proofs) ^ |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
89 |
"%"); |
38897 | 90 |
log ("Number of found facts: " ^ string_of_int (get id num_found_facts)); |
91 |
log ("Number of lost facts: " ^ string_of_int (get id num_lost_facts)); |
|
38894 | 92 |
log ("Fact found rate: " ^ |
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
93 |
percentage_alt (get id num_found_facts) (get id num_lost_facts) ^ |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
94 |
"%")) |
38894 | 95 |
else |
96 |
() |
|
38892 | 97 |
|
40220 | 98 |
val default_prover = ATP_Systems.eN (* arbitrary ATP *) |
38892 | 99 |
|
38897 | 100 |
fun with_index (i, s) = s ^ "@" ^ string_of_int i |
101 |
||
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
102 |
fun action args id ({pre, pos, log, ...} : Mirabelle.run_args) = |
38892 | 103 |
case (Position.line_of pos, Position.column_of pos) of |
104 |
(SOME line_num, SOME col_num) => |
|
105 |
(case Prooftab.lookup (!proof_table) (line_num, col_num) of |
|
106 |
SOME proofs => |
|
107 |
let |
|
108 |
val {context = ctxt, facts, goal} = Proof.goal pre |
|
40220 | 109 |
val prover = AList.lookup (op =) args "prover" |
110 |
|> the_default default_prover |
|
111 |
val default_max_relevant = |
|
40941
a3e6f8634a11
replace "smt" prover with specific SMT solvers, e.g. "z3" -- whatever the SMT module gives us
blanchet
parents:
40369
diff
changeset
|
112 |
Sledgehammer.default_max_relevant_for_prover ctxt prover |
40369
53dca3bd4250
use the SMT integration's official list of built-ins
blanchet
parents:
40220
diff
changeset
|
113 |
val is_built_in_const = |
53dca3bd4250
use the SMT integration's official list of built-ins
blanchet
parents:
40220
diff
changeset
|
114 |
Sledgehammer.is_built_in_const_for_prover ctxt default_prover |
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
115 |
val relevance_fudge = |
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
116 |
extract_relevance_fudge args |
40941
a3e6f8634a11
replace "smt" prover with specific SMT solvers, e.g. "z3" -- whatever the SMT module gives us
blanchet
parents:
40369
diff
changeset
|
117 |
(Sledgehammer.relevance_fudge_for_prover ctxt prover) |
40071
658a37c80b53
generalized the relevance filter so that it takes the list of "irrelevant constants" as argument (since the ATP and SMT preprocessing are different)
blanchet
parents:
40070
diff
changeset
|
118 |
val relevance_override = {add = [], del = [], only = false} |
38997 | 119 |
val {relevance_thresholds, full_types, max_relevant, ...} = |
40069 | 120 |
Sledgehammer_Isar.default_params ctxt args |
38892 | 121 |
val subgoal = 1 |
122 |
val (_, hyp_ts, concl_t) = Sledgehammer_Util.strip_subgoal goal subgoal |
|
123 |
val facts = |
|
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
40069
diff
changeset
|
124 |
Sledgehammer_Filter.relevant_facts ctxt full_types |
38892 | 125 |
relevance_thresholds |
40369
53dca3bd4250
use the SMT integration's official list of built-ins
blanchet
parents:
40220
diff
changeset
|
126 |
(the_default default_max_relevant max_relevant) is_built_in_const |
53dca3bd4250
use the SMT integration's official list of built-ins
blanchet
parents:
40220
diff
changeset
|
127 |
relevance_fudge relevance_override facts hyp_ts concl_t |
38892 | 128 |
|> map (fst o fst) |
38894 | 129 |
val (found_facts, lost_facts) = |
38892 | 130 |
List.concat proofs |> sort_distinct string_ord |
38897 | 131 |
|> map (fn fact => (find_index (curry (op =) fact) facts, fact)) |
132 |
|> List.partition (curry (op <=) 0 o fst) |
|
133 |
|>> sort (prod_ord int_ord string_ord) ||> map snd |
|
38892 | 134 |
val found_proofs = filter (forall (member (op =) facts)) proofs |
38894 | 135 |
val n = length found_proofs |
38892 | 136 |
val _ = |
38894 | 137 |
if n = 0 then |
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
138 |
(add id num_failures 1; log "Failure") |
38894 | 139 |
else |
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
140 |
(add id num_successes 1; |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
141 |
add id num_found_proofs n; |
38897 | 142 |
log ("Success (" ^ string_of_int n ^ " of " ^ |
143 |
string_of_int (length proofs) ^ " proofs)")) |
|
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
144 |
val _ = add id num_lost_proofs (length proofs - n) |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
145 |
val _ = add id num_found_facts (length found_facts) |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
146 |
val _ = add id num_lost_facts (length lost_facts) |
38902 | 147 |
val _ = |
148 |
if null found_facts then |
|
149 |
() |
|
150 |
else |
|
151 |
let |
|
152 |
val found_weight = |
|
153 |
Real.fromInt (fold (fn (n, _) => |
|
154 |
Integer.add (n * n)) found_facts 0) |
|
155 |
/ Real.fromInt (length found_facts) |
|
156 |
|> Math.sqrt |> Real.ceil |
|
157 |
in |
|
158 |
log ("Found facts (among " ^ string_of_int (length facts) ^ |
|
159 |
", weight " ^ string_of_int found_weight ^ "): " ^ |
|
160 |
commas (map with_index found_facts)) |
|
161 |
end |
|
162 |
val _ = if null lost_facts then |
|
38897 | 163 |
() |
164 |
else |
|
38902 | 165 |
log ("Lost facts (among " ^ string_of_int (length facts) ^ |
166 |
"): " ^ commas lost_facts) |
|
38892 | 167 |
in () end |
38894 | 168 |
| NONE => log "No known proof") |
38892 | 169 |
| _ => () |
170 |
||
38894 | 171 |
val proof_fileK = "proof_file" |
172 |
||
173 |
fun invoke args = |
|
174 |
let |
|
175 |
val (pf_args, other_args) = |
|
176 |
args |> List.partition (curry (op =) proof_fileK o fst) |
|
177 |
val proof_file = case pf_args of |
|
178 |
[] => error "No \"proof_file\" specified" |
|
179 |
| (_, s) :: _ => s |
|
180 |
in Mirabelle.register (init proof_file, action other_args, done) end |
|
38892 | 181 |
|
182 |
end; |
|
38894 | 183 |
|
184 |
(* Workaround to keep the "mirabelle.pl" script happy *) |
|
185 |
structure Mirabelle_Sledgehammer_filter = Mirabelle_Sledgehammer_Filter; |