author | blanchet |
Tue, 31 Aug 2010 20:23:32 +0200 | |
changeset 38941 | e2c95e3263a4 |
parent 38938 | 2b93dbc07778 |
child 38988 | 483879af0643 |
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 |
||
38902 | 8 |
val relevance_filter_args = |
38938
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset
|
9 |
[("worse_irrel_freq", Sledgehammer_Fact_Filter.worse_irrel_freq), |
38941 | 10 |
("higher_order_irrel_weight", |
11 |
Sledgehammer_Fact_Filter.higher_order_irrel_weight), |
|
38938
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset
|
12 |
("abs_rel_weight", Sledgehammer_Fact_Filter.abs_rel_weight), |
38902 | 13 |
("abs_irrel_weight", Sledgehammer_Fact_Filter.abs_irrel_weight), |
14 |
("skolem_irrel_weight", Sledgehammer_Fact_Filter.skolem_irrel_weight), |
|
38937
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38905
diff
changeset
|
15 |
("intro_bonus", Sledgehammer_Fact_Filter.intro_bonus), |
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38905
diff
changeset
|
16 |
("elim_bonus", Sledgehammer_Fact_Filter.elim_bonus), |
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38905
diff
changeset
|
17 |
("simp_bonus", Sledgehammer_Fact_Filter.simp_bonus), |
38902 | 18 |
("local_bonus", Sledgehammer_Fact_Filter.local_bonus), |
19 |
("chained_bonus", Sledgehammer_Fact_Filter.chained_bonus), |
|
38905 | 20 |
("max_imperfect", Sledgehammer_Fact_Filter.max_imperfect), |
21 |
("max_imperfect_exp", Sledgehammer_Fact_Filter.max_imperfect_exp), |
|
38902 | 22 |
("threshold_divisor", Sledgehammer_Fact_Filter.threshold_divisor), |
38905 | 23 |
("ridiculous_threshold", Sledgehammer_Fact_Filter.ridiculous_threshold)] |
38902 | 24 |
|
38892 | 25 |
structure Prooftab = |
26 |
Table(type key = int * int val ord = prod_ord int_ord int_ord); |
|
27 |
||
28 |
val proof_table = Unsynchronized.ref Prooftab.empty |
|
29 |
||
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
30 |
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
|
31 |
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
|
32 |
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
|
33 |
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
|
34 |
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
|
35 |
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
|
36 |
|
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
37 |
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
|
38 |
fun add id c n = |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
39 |
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
|
40 |
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
|
41 |
| NONE => (id, n) :: !c) |
38894 | 42 |
|
43 |
fun init proof_file _ thy = |
|
38892 | 44 |
let |
45 |
fun do_line line = |
|
46 |
case line |> space_explode ":" of |
|
47 |
[line_num, col_num, proof] => |
|
48 |
SOME (pairself (the o Int.fromString) (line_num, col_num), |
|
49 |
proof |> space_explode " " |> filter_out (curry (op =) "")) |
|
50 |
| _ => NONE |
|
38894 | 51 |
val proofs = File.read (Path.explode proof_file) |
38892 | 52 |
val proof_tab = |
53 |
proofs |> space_explode "\n" |
|
54 |
|> map_filter do_line |
|
55 |
|> AList.coalesce (op =) |
|
56 |
|> Prooftab.make |
|
57 |
in proof_table := proof_tab; thy end |
|
58 |
||
38894 | 59 |
fun percentage a b = if b = 0 then "N/A" else string_of_int (a * 100 div b) |
60 |
fun percentage_alt a b = percentage a (a + b) |
|
61 |
||
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
62 |
fun done id ({log, ...} : Mirabelle.done_args) = |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
63 |
if get id num_successes + get id num_failures > 0 then |
38897 | 64 |
(log ""; |
65 |
log ("Number of overall successes: " ^ |
|
66 |
string_of_int (get id num_successes)); |
|
67 |
log ("Number of overall failures: " ^ string_of_int (get id num_failures)); |
|
38894 | 68 |
log ("Overall success rate: " ^ |
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
69 |
percentage_alt (get id num_successes) (get id num_failures) ^ "%"); |
38897 | 70 |
log ("Number of found proofs: " ^ string_of_int (get id num_found_proofs)); |
71 |
log ("Number of lost proofs: " ^ string_of_int (get id num_lost_proofs)); |
|
38894 | 72 |
log ("Proof found rate: " ^ |
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
73 |
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
|
74 |
"%"); |
38897 | 75 |
log ("Number of found facts: " ^ string_of_int (get id num_found_facts)); |
76 |
log ("Number of lost facts: " ^ string_of_int (get id num_lost_facts)); |
|
38894 | 77 |
log ("Fact found rate: " ^ |
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
78 |
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
|
79 |
"%")) |
38894 | 80 |
else |
81 |
() |
|
38892 | 82 |
|
83 |
val default_max_relevant = 300 |
|
84 |
||
38897 | 85 |
fun with_index (i, s) = s ^ "@" ^ string_of_int i |
86 |
||
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
87 |
fun action args id ({pre, pos, log, ...} : Mirabelle.run_args) = |
38892 | 88 |
case (Position.line_of pos, Position.column_of pos) of |
89 |
(SOME line_num, SOME col_num) => |
|
90 |
(case Prooftab.lookup (!proof_table) (line_num, col_num) of |
|
91 |
SOME proofs => |
|
92 |
let |
|
93 |
val {context = ctxt, facts, goal} = Proof.goal pre |
|
94 |
val thy = ProofContext.theory_of ctxt |
|
38902 | 95 |
val args = |
96 |
args |
|
97 |
|> filter (fn (key, value) => |
|
98 |
case AList.lookup (op =) relevance_filter_args key of |
|
99 |
SOME rf => (rf := the (Real.fromString value); false) |
|
100 |
| NONE => true) |
|
101 |
||
38892 | 102 |
val {relevance_thresholds, full_types, max_relevant, theory_relevant, |
103 |
...} = Sledgehammer_Isar.default_params thy args |
|
104 |
val subgoal = 1 |
|
105 |
val (_, hyp_ts, concl_t) = Sledgehammer_Util.strip_subgoal goal subgoal |
|
106 |
val facts = |
|
107 |
Sledgehammer_Fact_Filter.relevant_facts ctxt full_types |
|
108 |
relevance_thresholds |
|
109 |
(the_default default_max_relevant max_relevant) |
|
110 |
(the_default false theory_relevant) |
|
111 |
{add = [], del = [], only = false} facts hyp_ts concl_t |
|
112 |
|> map (fst o fst) |
|
38894 | 113 |
val (found_facts, lost_facts) = |
38892 | 114 |
List.concat proofs |> sort_distinct string_ord |
38897 | 115 |
|> map (fn fact => (find_index (curry (op =) fact) facts, fact)) |
116 |
|> List.partition (curry (op <=) 0 o fst) |
|
117 |
|>> sort (prod_ord int_ord string_ord) ||> map snd |
|
38892 | 118 |
val found_proofs = filter (forall (member (op =) facts)) proofs |
38894 | 119 |
val n = length found_proofs |
38892 | 120 |
val _ = |
38894 | 121 |
if n = 0 then |
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
122 |
(add id num_failures 1; log "Failure") |
38894 | 123 |
else |
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
124 |
(add id num_successes 1; |
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
125 |
add id num_found_proofs n; |
38897 | 126 |
log ("Success (" ^ string_of_int n ^ " of " ^ |
127 |
string_of_int (length proofs) ^ " proofs)")) |
|
38896
b36ab8860748
allow multiple invocations of "mirabele_sledgehammer_filter", by multiplexing on ID
blanchet
parents:
38894
diff
changeset
|
128 |
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
|
129 |
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
|
130 |
val _ = add id num_lost_facts (length lost_facts) |
38902 | 131 |
val _ = |
132 |
if null found_facts then |
|
133 |
() |
|
134 |
else |
|
135 |
let |
|
136 |
val found_weight = |
|
137 |
Real.fromInt (fold (fn (n, _) => |
|
138 |
Integer.add (n * n)) found_facts 0) |
|
139 |
/ Real.fromInt (length found_facts) |
|
140 |
|> Math.sqrt |> Real.ceil |
|
141 |
in |
|
142 |
log ("Found facts (among " ^ string_of_int (length facts) ^ |
|
143 |
", weight " ^ string_of_int found_weight ^ "): " ^ |
|
144 |
commas (map with_index found_facts)) |
|
145 |
end |
|
146 |
val _ = if null lost_facts then |
|
38897 | 147 |
() |
148 |
else |
|
38902 | 149 |
log ("Lost facts (among " ^ string_of_int (length facts) ^ |
150 |
"): " ^ commas lost_facts) |
|
38892 | 151 |
in () end |
38894 | 152 |
| NONE => log "No known proof") |
38892 | 153 |
| _ => () |
154 |
||
38894 | 155 |
val proof_fileK = "proof_file" |
156 |
||
157 |
fun invoke args = |
|
158 |
let |
|
159 |
val (pf_args, other_args) = |
|
160 |
args |> List.partition (curry (op =) proof_fileK o fst) |
|
161 |
val proof_file = case pf_args of |
|
162 |
[] => error "No \"proof_file\" specified" |
|
163 |
| (_, s) :: _ => s |
|
164 |
in Mirabelle.register (init proof_file, action other_args, done) end |
|
38892 | 165 |
|
166 |
end; |
|
38894 | 167 |
|
168 |
(* Workaround to keep the "mirabelle.pl" script happy *) |
|
169 |
structure Mirabelle_Sledgehammer_filter = Mirabelle_Sledgehammer_Filter; |