author  wenzelm 
Sun, 25 Oct 2009 19:19:41 +0100  
changeset 33170  dd6d8d1f70d2 
parent 32810  f3466a5645fa 
child 33391  91b9da2a7b44 
permissions  rwrr 
7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

1 
(* Title: Pure/Thy/thm_deps.ML 
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

2 
Author: Stefan Berghofer, TU Muenchen 
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

3 

fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

4 
Visualize dependencies of theorems. 
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

5 
*) 
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 
signature THM_DEPS = 
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

8 
sig 
26697  9 
val thm_deps: thm list > unit 
10 
val unused_thms: theory list * theory list > (string * thm) list 

7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

11 
end; 
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

12 

26697  13 
structure ThmDeps: THM_DEPS = 
7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

14 
struct 
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

15 

26697  16 
(* thm_deps *) 
17 

7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

18 
fun thm_deps thms = 
28810  19 
let 
28826
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

20 
fun add_dep ("", _, _) = I 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

21 
 add_dep (name, _, PBody {thms = thms', ...}) = 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

22 
let 
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset

23 
val prefix = #1 (Library.split_last (Long_Name.explode name)); 
28826
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

24 
val session = 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

25 
(case prefix of 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

26 
a :: _ => 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

27 
(case ThyInfo.lookup_theory a of 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

28 
SOME thy => 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

29 
(case Present.session_name thy of 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

30 
"" => [] 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

31 
 session => [session]) 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

32 
 NONE => []) 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

33 
 _ => ["global"]); 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

34 
val parents = filter_out (fn s => s = "") (map (#1 o #2) thms'); 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

35 
val entry = 
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset

36 
{name = Long_Name.base_name name, 
28826
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

37 
ID = name, 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

38 
dir = space_implode "/" (session @ prefix), 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

39 
unfold = false, 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

40 
path = "", 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

41 
parents = parents}; 
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

42 
in cons entry end; 
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

43 
val deps = Proofterm.fold_body_thms add_dep (map Thm.proof_body_of thms) []; 
28826
3b460b6eadae
simplified thm_deps  no need to build a graph datastructure;
wenzelm
parents:
28817
diff
changeset

44 
in Present.display_graph (sort_wrt #ID deps) end; 
26697  45 

46 

47 
(* unused_thms *) 

48 

49 
fun unused_thms (base_thys, thys) = 

26185  50 
let 
33170  51 
fun add_fact space (name, ths) = 
26697  52 
if exists (fn thy => PureThy.defined_fact thy name) base_thys then I 
33170  53 
else 
54 
let val {concealed, group, ...} = Name_Space.the_entry space name in 

55 
fold_rev (fn th => 

56 
(case Thm.get_name th of 

57 
"" => I 

58 
 a => cons (a, (th, concealed, group)))) ths 

59 
end; 

60 
fun add_facts facts = Facts.fold_static (add_fact (Facts.space_of facts)) facts; 

61 

26699  62 
val thms = 
33170  63 
fold (add_facts o PureThy.facts_of) thys [] 
26699  64 
> sort_distinct (string_ord o pairself #1); 
28810  65 

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

66 
val tab = 
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

67 
Proofterm.fold_body_thms 
33170  68 
(fn (a, prop, _) => a <> "" ? Symtab.insert_list (op =) (a, prop)) 
69 
(map (Proofterm.strip_thm o Thm.proof_body_of o #1 o #2) thms) Symtab.empty; 

70 

71 
fun is_unused (a, th) = 

72 
not (member (op aconv) (Symtab.lookup_list tab a) (Thm.prop_of th)); 

28810  73 

26185  74 
(* groups containing at least one used theorem *) 
33170  75 
val used_groups = fold (fn (a, (th, _, group)) => 
76 
if is_unused (a, th) then I 

77 
else 

78 
(case group of 

79 
NONE => I 

80 
 SOME grp => Inttab.update (grp, ()))) thms Inttab.empty; 

81 
val (thms', _) = fold (fn (a, (th, concealed, group)) => fn q as (thms, grps') => 

82 
if member (op =) [Thm.theoremK, Thm.generatedK, Thm.lemmaK, Thm.corollaryK, Thm.axiomK] 

83 
(Thm.get_kind th) andalso not concealed andalso is_unused (a, th) 

26185  84 
then 
33170  85 
(case group of 
86 
NONE => ((a, th) :: thms, grps') 

26185  87 
 SOME grp => 
88 
(* do not output theorem if another theorem in group has been used *) 

33170  89 
if Inttab.defined used_groups grp then q 
26185  90 
(* output at most one unused theorem per group *) 
33170  91 
else if Inttab.defined grps' grp then q 
92 
else ((a, th) :: thms, Inttab.update (grp, ()) grps')) 

93 
else q) thms ([], Inttab.empty) 

26185  94 
in rev thms' end; 
95 

7765
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

96 
end; 
fa28bac7903c
New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff
changeset

97 