author | wenzelm |
Sun, 20 May 2012 11:34:33 +0200 | |
changeset 47884 | 21c42b095c84 |
parent 46961 | 5c6955f487e5 |
child 50286 | e8b29ddbb61f |
permissions | -rw-r--r-- |
46589 | 1 |
(* Title: HOL/Tools/Quickcheck/find_unused_assms.ML |
2 |
Author: Lukas Bulwahn, TU Muenchen |
|
3 |
||
4 |
Finding unused assumptions in lemmas (using Quickcheck) |
|
5 |
*) |
|
6 |
||
7 |
signature FIND_UNUSED_ASSMS = |
|
8 |
sig |
|
9 |
val find_unused_assms : Proof.context -> string -> (string * int list list option) list |
|
10 |
val print_unused_assms : Proof.context -> string option -> unit |
|
11 |
end; |
|
12 |
||
13 |
structure Find_Unused_Assms : FIND_UNUSED_ASSMS = |
|
14 |
struct |
|
15 |
||
16 |
fun all_unconcealed_thms_of thy = |
|
17 |
let |
|
18 |
val facts = Global_Theory.facts_of thy |
|
19 |
in |
|
20 |
Facts.fold_static |
|
21 |
(fn (s, ths) => |
|
22 |
if Facts.is_concealed facts s then I else append (map (`(Thm.get_name_hint)) ths)) |
|
23 |
facts [] |
|
24 |
end; |
|
25 |
||
26 |
fun thms_of thy thy_name = all_unconcealed_thms_of thy |
|
27 |
|> filter (fn (_, th) => Context.theory_name (theory_of_thm th) = thy_name); |
|
28 |
||
29 |
fun do_while P f s = if P s then (let val s' = f s in (do_while P f s') o (cons s') end) else I |
|
30 |
||
31 |
fun drop_indexes is xs = fold_index (fn (i, x) => if member (op =) is i then I else cons x) xs [] |
|
32 |
||
33 |
fun find_max_subsets [] = [] |
|
34 |
| find_max_subsets (ss :: sss) = ss :: |
|
35 |
(find_max_subsets (map (filter_out (fn s => exists (fn s' => subset (op =) (s, s')) ss)) sss)) |
|
36 |
||
37 |
(* main functionality *) |
|
38 |
||
39 |
fun find_unused_assms ctxt thy_name = |
|
40 |
let |
|
41 |
val ctxt' = ctxt |
|
42 |
|> Config.put Quickcheck.abort_potential true |
|
43 |
|> Config.put Quickcheck.quiet true |
|
46711
f745bcc4a1e5
more explicit Long_Name operations (NB: analyzing qualifiers is inherently fragile);
wenzelm
parents:
46589
diff
changeset
|
44 |
val all_thms = filter (fn (s, _) => length (Long_Name.explode s) = 2) (* FIXME !? *) |
46589 | 45 |
(thms_of (Proof_Context.theory_of ctxt) thy_name) |
46 |
fun check_single conjecture = |
|
47 |
case try (Quickcheck.test_terms ctxt' (true, true) []) [(conjecture, [])] of |
|
48 |
SOME (SOME _) => false |
|
49 |
| SOME NONE => true |
|
50 |
| NONE => false |
|
51 |
fun build X Ss = |
|
52 |
fold (fn S => fold |
|
53 |
(fn x => if member (op =) S x then I |
|
54 |
else insert (eq_set (op =)) (insert (op =) x S)) X) Ss [] |
|
55 |
fun check (s, th) = |
|
56 |
case Logic.strip_horn (prop_of (Thm.unvarify_global th)) of |
|
57 |
([], _) => cons (s, NONE) |
|
58 |
| (ts, t) => |
|
59 |
let |
|
60 |
fun mk_conjecture is = (Logic.list_implies (drop_indexes is ts, t)) |
|
61 |
val singles = filter (check_single o mk_conjecture o single) (map_index fst ts) |
|
62 |
val multiples = do_while (not o null) |
|
63 |
(fn I => filter (check_single o mk_conjecture) (build singles I)) |
|
64 |
(map single singles) [(map single singles)] |
|
65 |
val maximals = flat (find_max_subsets multiples) |
|
66 |
in |
|
67 |
cons (s, SOME maximals) |
|
68 |
end |
|
69 |
in |
|
70 |
fold check all_thms [] |
|
71 |
end |
|
72 |
||
73 |
(* printing results *) |
|
74 |
||
75 |
fun pretty_indexes is = |
|
76 |
Pretty.block (separate (Pretty.str " and ") |
|
77 |
(map (fn x => Pretty.str (string_of_int (x + 1))) (sort int_ord is)) |
|
78 |
@ [Pretty.brk 1]) |
|
79 |
||
80 |
fun pretty_thm (s, SOME set_of_indexes) = |
|
81 |
Pretty.block (Pretty.str s :: Pretty.str ":" :: Pretty.brk 1 :: |
|
82 |
Pretty.str "unnecessary assumption(s) " :: |
|
83 |
separate (Pretty.str ", or ") (map pretty_indexes set_of_indexes)) |
|
84 |
||
85 |
fun print_unused_assms ctxt opt_thy_name = |
|
86 |
let |
|
87 |
val thy_name = the_default (Context.theory_name (Proof_Context.theory_of ctxt)) opt_thy_name |
|
88 |
val results = find_unused_assms ctxt thy_name |
|
89 |
val total = length results |
|
90 |
val with_assumptions = length (filter (is_some o snd) results) |
|
91 |
val with_superfluous_assumptions = filter_out (fn (_, r) => r = SOME []) |
|
92 |
(filter (is_some o snd) results) |
|
93 |
||
94 |
val msg = "Found " ^ string_of_int (length with_superfluous_assumptions) |
|
95 |
^ " theorem(s) with (potentially) superfluous assumptions" |
|
96 |
val end_msg = "Checked " ^ string_of_int with_assumptions ^ " theorem(s) with assumptions" |
|
97 |
^ " in the theory " ^ quote thy_name |
|
98 |
^ " with a total of " ^ string_of_int total ^ " theorem(s)" |
|
99 |
in |
|
100 |
([Pretty.str (msg ^ ":"), Pretty.str ""] @ |
|
101 |
map pretty_thm with_superfluous_assumptions |
|
102 |
@ [Pretty.str "", Pretty.str end_msg]) |
|
103 |
end |> Pretty.chunks |> Pretty.writeln; |
|
104 |
||
105 |
||
106 |
val _ = |
|
46961
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
46716
diff
changeset
|
107 |
Outer_Syntax.improper_command @{command_spec "find_unused_assms"} |
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
46716
diff
changeset
|
108 |
"find theorems with superfluous assumptions" |
46589 | 109 |
(Scan.option Parse.name |
110 |
>> (fn opt_thy_name => |
|
111 |
Toplevel.no_timing o Toplevel.keep (fn state => |
|
112 |
print_unused_assms (Toplevel.context_of state) opt_thy_name))); |
|
113 |
||
114 |
end; |