author | wenzelm |
Tue, 20 Aug 2019 11:38:48 +0200 | |
changeset 70588 | 35a4ef9c6d80 |
parent 70570 | d94456876f2d |
child 70595 | 2ae7e33c950f |
permissions | -rw-r--r-- |
70556 | 1 |
(* Title: Pure/Thy/thm_deps.ML |
7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
2 |
Author: Stefan Berghofer, TU Muenchen |
70556 | 3 |
Author: Makarius |
7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
4 |
|
70556 | 5 |
Dependencies of theorems wrt. internal derivation. |
7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
6 |
*) |
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
7 |
|
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
8 |
signature THM_DEPS = |
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
9 |
sig |
70557 | 10 |
val all_oracles: thm list -> Proofterm.oracle list |
70566
fb3d06d7dd05
more thorough check, using full dependency graph of finished proofs;
wenzelm
parents:
70560
diff
changeset
|
11 |
val has_skip_proof: thm list -> bool |
70560 | 12 |
val pretty_thm_oracles: Proof.context -> thm list -> Pretty.T |
70570 | 13 |
val thm_deps_cmd: theory -> thm list -> unit |
14 |
val unused_thms_cmd: theory list * theory list -> (string * thm) list |
|
7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
15 |
end; |
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
16 |
|
33391 | 17 |
structure Thm_Deps: THM_DEPS = |
7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
18 |
struct |
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
19 |
|
70557 | 20 |
(* oracles *) |
21 |
||
22 |
fun all_oracles thms = |
|
70588 | 23 |
let |
24 |
fun collect (PBody {oracles, thms, ...}) = |
|
25 |
(if null oracles then I else apfst (cons oracles)) #> |
|
26 |
(tap Proofterm.join_thms thms |> fold (fn (i, thm_node) => fn (res, seen) => |
|
27 |
if Inttab.defined seen i then (res, seen) |
|
28 |
else |
|
29 |
let val body = Future.join (Proofterm.thm_node_body thm_node) |
|
30 |
in collect body (res, Inttab.update (i, ()) seen) end)); |
|
31 |
val bodies = map Thm.proof_body_of thms; |
|
32 |
in fold collect bodies ([], Inttab.empty) |> #1 |> Proofterm.unions_oracles end; |
|
70557 | 33 |
|
70566
fb3d06d7dd05
more thorough check, using full dependency graph of finished proofs;
wenzelm
parents:
70560
diff
changeset
|
34 |
fun has_skip_proof thms = |
fb3d06d7dd05
more thorough check, using full dependency graph of finished proofs;
wenzelm
parents:
70560
diff
changeset
|
35 |
all_oracles thms |> exists (fn (name, _) => name = \<^oracle_name>\<open>skip_proof\<close>); |
fb3d06d7dd05
more thorough check, using full dependency graph of finished proofs;
wenzelm
parents:
70560
diff
changeset
|
36 |
|
70560 | 37 |
fun pretty_thm_oracles ctxt thms = |
38 |
let |
|
70567 | 39 |
val thy = Proof_Context.theory_of ctxt; |
70560 | 40 |
fun prt_oracle (name, NONE) = [Thm.pretty_oracle ctxt name] |
41 |
| prt_oracle (name, SOME prop) = |
|
70567 | 42 |
[Thm.pretty_oracle ctxt name, Pretty.str ":", Pretty.brk 1, |
43 |
Syntax.pretty_term_global thy prop]; |
|
70560 | 44 |
in Pretty.big_list "oracles:" (map (Pretty.item o prt_oracle) (all_oracles thms)) end; |
45 |
||
70557 | 46 |
|
70570 | 47 |
(* thm_deps_cmd *) |
26697 | 48 |
|
70570 | 49 |
fun thm_deps_cmd thy thms = |
28810 | 50 |
let |
60084
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
51 |
fun make_node name directory = |
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
52 |
Graph_Display.session_node |
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
53 |
{name = Long_Name.base_name name, directory = directory, unfold = false, path = ""}; |
64572 | 54 |
fun add_dep {name = "", ...} = I |
55 |
| add_dep {name = name, body = PBody {thms = thms', ...}, ...} = |
|
28826
3b460b6eadae
simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset
|
56 |
let |
41489
8e2b8649507d
standardized split_last/last_elem towards List.last;
wenzelm
parents:
39557
diff
changeset
|
57 |
val prefix = #1 (split_last (Long_Name.explode name)); |
28826
3b460b6eadae
simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset
|
58 |
val session = |
3b460b6eadae
simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset
|
59 |
(case prefix of |
3b460b6eadae
simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset
|
60 |
a :: _ => |
68482 | 61 |
(case try (Context.get_theory {long = false} thy) a of |
28826
3b460b6eadae
simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset
|
62 |
SOME thy => |
66037 | 63 |
(case Present.theory_qualifier thy of |
28826
3b460b6eadae
simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset
|
64 |
"" => [] |
3b460b6eadae
simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset
|
65 |
| session => [session]) |
3b460b6eadae
simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset
|
66 |
| NONE => []) |
60084
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
67 |
| _ => ["global"]); |
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
68 |
val node = make_node name (space_implode "/" (session @ prefix)); |
64573 | 69 |
val deps = filter_out (fn s => s = "") (map (Proofterm.thm_node_name o #2) thms'); |
60084
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
70 |
in Symtab.update (name, (node, deps)) end; |
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
71 |
val entries0 = Proofterm.fold_body_thms add_dep (Thm.proof_bodies_of thms) Symtab.empty; |
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
72 |
val entries1 = |
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
73 |
Symtab.fold (fn (_, (_, deps)) => deps |> fold (fn d => fn tab => |
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
74 |
if Symtab.defined tab d then tab |
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
75 |
else Symtab.update (d, (make_node d "", [])) tab)) entries0 entries0; |
59210
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents:
59208
diff
changeset
|
76 |
in |
60084
2a066431a814
ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents:
59936
diff
changeset
|
77 |
Symtab.fold (fn (name, (node, deps)) => cons ((name, node), deps)) entries1 [] |
60089
8bd5999133d4
let the system choose Graph_Display.display_graph_old: thm_deps needs tree hierarchy, code_deps needs cycles (!?);
wenzelm
parents:
60084
diff
changeset
|
78 |
|> Graph_Display.display_graph_old |
59210
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents:
59208
diff
changeset
|
79 |
end; |
26697 | 80 |
|
81 |
||
70570 | 82 |
(* unused_thms_cmd *) |
26697 | 83 |
|
70570 | 84 |
fun unused_thms_cmd (base_thys, thys) = |
26185 | 85 |
let |
61507 | 86 |
fun add_fact transfer space (name, ths) = |
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
37870
diff
changeset
|
87 |
if exists (fn thy => Global_Theory.defined_fact thy name) base_thys then I |
33170 | 88 |
else |
89 |
let val {concealed, group, ...} = Name_Space.the_entry space name in |
|
90 |
fold_rev (fn th => |
|
36744
6e1f3d609a68
renamed Thm.get_name -> Thm.derivation_name and Thm.put_name -> Thm.name_derivation, to emphasize the true nature of these operations;
wenzelm
parents:
33769
diff
changeset
|
91 |
(case Thm.derivation_name th of |
33170 | 92 |
"" => I |
61507 | 93 |
| a => cons (a, (transfer th, concealed, group)))) ths |
33170 | 94 |
end; |
61507 | 95 |
fun add_facts thy = |
96 |
let |
|
97 |
val transfer = Global_Theory.transfer_theories thy; |
|
98 |
val facts = Global_Theory.facts_of thy; |
|
99 |
in Facts.fold_static (add_fact transfer (Facts.space_of facts)) facts end; |
|
33170 | 100 |
|
33769
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
101 |
val new_thms = |
61507 | 102 |
fold add_facts thys [] |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58893
diff
changeset
|
103 |
|> sort_distinct (string_ord o apply2 #1); |
28810 | 104 |
|
33769
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
105 |
val used = |
32810
f3466a5645fa
back to simple fold_body_thms and fulfill_proof/thm_proof (reverting a900d3cd47cc) -- the cycle check is implicit in the future computation of join_proofs;
wenzelm
parents:
32726
diff
changeset
|
106 |
Proofterm.fold_body_thms |
64572 | 107 |
(fn {name = a, ...} => a <> "" ? Symtab.update (a, ())) |
44333
cc53ce50f738
reverted to join_bodies/join_proofs based on fold_body_thms to regain performance (escpecially of HOL-Proofs) -- see also aa9c1e9ef2ce and 4e2abb045eac;
wenzelm
parents:
44331
diff
changeset
|
108 |
(map Proofterm.strip_thm (Thm.proof_bodies_of (map (#1 o #2) new_thms))) |
44331 | 109 |
Symtab.empty; |
33170 | 110 |
|
41565
9718c32f9c4e
unused_thms no longer compares propositions, since this is no longer needed
berghofe
parents:
41489
diff
changeset
|
111 |
fun is_unused a = not (Symtab.defined used a); |
28810 | 112 |
|
57934
5e500c0e7eca
tuned signature -- prefer self-contained user-space tool;
wenzelm
parents:
49561
diff
changeset
|
113 |
(*groups containing at least one used theorem*) |
41565
9718c32f9c4e
unused_thms no longer compares propositions, since this is no longer needed
berghofe
parents:
41489
diff
changeset
|
114 |
val used_groups = fold (fn (a, (_, _, group)) => |
9718c32f9c4e
unused_thms no longer compares propositions, since this is no longer needed
berghofe
parents:
41489
diff
changeset
|
115 |
if is_unused a then I |
33170 | 116 |
else |
117 |
(case group of |
|
118 |
NONE => I |
|
33769
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
119 |
| SOME grp => Inttab.update (grp, ()))) new_thms Inttab.empty; |
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
120 |
|
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
121 |
val (thms', _) = fold (fn (a, (th, concealed, group)) => fn q as (thms, seen_groups) => |
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
122 |
if not concealed andalso |
42473 | 123 |
(* FIXME replace by robust treatment of thm groups *) |
61336 | 124 |
Thm.legacy_get_kind th = Thm.theoremK andalso is_unused a |
33769
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
125 |
then |
33170 | 126 |
(case group of |
33769
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
127 |
NONE => ((a, th) :: thms, seen_groups) |
26185 | 128 |
| SOME grp => |
33769
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
129 |
if Inttab.defined used_groups grp orelse |
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
130 |
Inttab.defined seen_groups grp then q |
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
131 |
else ((a, th) :: thms, Inttab.update (grp, ()) seen_groups)) |
6d8630fab26a
unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents:
33642
diff
changeset
|
132 |
else q) new_thms ([], Inttab.empty); |
26185 | 133 |
in rev thms' end; |
134 |
||
7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset
|
135 |
end; |