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