src/Pure/Thy/thm_deps.ML
author wenzelm
Sat, 17 Aug 2019 11:23:20 +0200
changeset 70557 5d6e9c65ea67
parent 70556 038ed9b76c2b
child 70560 7714971a58b5
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
70556
038ed9b76c2b clarified modules;
wenzelm
parents: 68482
diff changeset
     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
038ed9b76c2b clarified modules;
wenzelm
parents: 68482
diff changeset
     3
    Author:     Makarius
7765
fa28bac7903c New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff changeset
     4
70556
038ed9b76c2b clarified modules;
wenzelm
parents: 68482
diff changeset
     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
5d6e9c65ea67 clarified signature;
wenzelm
parents: 70556
diff changeset
    10
  val all_oracles: thm list -> Proofterm.oracle list
37870
dd9cfc512b7f thm_deps/unused_thms: Context.get_theory based on proper theory ancestry, not accidental theory loader state;
wenzelm
parents: 37216
diff changeset
    11
  val thm_deps: theory -> thm list -> unit
26697
3b9eede40608 removed obsolete BASIC_THM_DEPS;
wenzelm
parents: 26185
diff changeset
    12
  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
    13
end;
fa28bac7903c New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff changeset
    14
33391
91b9da2a7b44 structure Thm_Deps;
wenzelm
parents: 33170
diff changeset
    15
structure Thm_Deps: THM_DEPS =
7765
fa28bac7903c New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff changeset
    16
struct
fa28bac7903c New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff changeset
    17
70557
5d6e9c65ea67 clarified signature;
wenzelm
parents: 70556
diff changeset
    18
(* oracles *)
5d6e9c65ea67 clarified signature;
wenzelm
parents: 70556
diff changeset
    19
5d6e9c65ea67 clarified signature;
wenzelm
parents: 70556
diff changeset
    20
fun all_oracles thms =
5d6e9c65ea67 clarified signature;
wenzelm
parents: 70556
diff changeset
    21
  Proofterm.all_oracles_of (map Thm.proof_body_of thms);
5d6e9c65ea67 clarified signature;
wenzelm
parents: 70556
diff changeset
    22
5d6e9c65ea67 clarified signature;
wenzelm
parents: 70556
diff changeset
    23
26697
3b9eede40608 removed obsolete BASIC_THM_DEPS;
wenzelm
parents: 26185
diff changeset
    24
(* thm_deps *)
3b9eede40608 removed obsolete BASIC_THM_DEPS;
wenzelm
parents: 26185
diff changeset
    25
37870
dd9cfc512b7f thm_deps/unused_thms: Context.get_theory based on proper theory ancestry, not accidental theory loader state;
wenzelm
parents: 37216
diff changeset
    26
fun thm_deps thy thms =
28810
e915ab11fe52 retrieve thm deps from proof_body;
wenzelm
parents: 27865
diff changeset
    27
  let
60084
2a066431a814 ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents: 59936
diff changeset
    28
    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
    29
      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
    30
       {name = Long_Name.base_name name, directory = directory, unfold = false, path = ""};
64572
cec07f7249cd tuned signature;
wenzelm
parents: 62848
diff changeset
    31
    fun add_dep {name = "", ...} = I
cec07f7249cd tuned signature;
wenzelm
parents: 62848
diff changeset
    32
      | 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
    33
          let
41489
8e2b8649507d standardized split_last/last_elem towards List.last;
wenzelm
parents: 39557
diff changeset
    34
            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
    35
            val session =
3b460b6eadae simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents: 28817
diff changeset
    36
              (case prefix of
3b460b6eadae simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents: 28817
diff changeset
    37
                a :: _ =>
68482
cb84beb84ca9 clarified signature;
wenzelm
parents: 66037
diff changeset
    38
                  (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
    39
                    SOME thy =>
66037
58d2e41afbfe more official session qualifier;
wenzelm
parents: 64573
diff changeset
    40
                      (case Present.theory_qualifier thy of
28826
3b460b6eadae simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents: 28817
diff changeset
    41
                        "" => []
3b460b6eadae simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents: 28817
diff changeset
    42
                      | session => [session])
3b460b6eadae simplified thm_deps -- no need to build a graph datastructure;
wenzelm
parents: 28817
diff changeset
    43
                  | NONE => [])
60084
2a066431a814 ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents: 59936
diff changeset
    44
              | _ => ["global"]);
2a066431a814 ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents: 59936
diff changeset
    45
            val node = make_node name (space_implode "/" (session @ prefix));
64573
e6aee01da22d tuned signature -- more abstract type thm_node;
wenzelm
parents: 64572
diff changeset
    46
            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
    47
          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
    48
    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
    49
    val entries1 =
2a066431a814 ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents: 59936
diff changeset
    50
      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
    51
        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
    52
        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
    53
  in
60084
2a066431a814 ensure that deps are defined in entries, to prevent crash of Graph_View.build_graph;
wenzelm
parents: 59936
diff changeset
    54
    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
    55
    |> 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
    56
  end;
26697
3b9eede40608 removed obsolete BASIC_THM_DEPS;
wenzelm
parents: 26185
diff changeset
    57
3b9eede40608 removed obsolete BASIC_THM_DEPS;
wenzelm
parents: 26185
diff changeset
    58
3b9eede40608 removed obsolete BASIC_THM_DEPS;
wenzelm
parents: 26185
diff changeset
    59
(* unused_thms *)
3b9eede40608 removed obsolete BASIC_THM_DEPS;
wenzelm
parents: 26185
diff changeset
    60
3b9eede40608 removed obsolete BASIC_THM_DEPS;
wenzelm
parents: 26185
diff changeset
    61
fun unused_thms (base_thys, thys) =
26185
e53165319347 Added function for finding unused theorems.
berghofe
parents: 26138
diff changeset
    62
  let
61507
6865140215ef proper transfer of stored facts;
wenzelm
parents: 61336
diff changeset
    63
    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
    64
      if exists (fn thy => Global_Theory.defined_fact thy name) base_thys then I
33170
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
    65
      else
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
    66
        let val {concealed, group, ...} = Name_Space.the_entry space name in
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
    67
          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
    68
            (case Thm.derivation_name th of
33170
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
    69
              "" => I
61507
6865140215ef proper transfer of stored facts;
wenzelm
parents: 61336
diff changeset
    70
            | a => cons (a, (transfer th, concealed, group)))) ths
33170
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
    71
        end;
61507
6865140215ef proper transfer of stored facts;
wenzelm
parents: 61336
diff changeset
    72
    fun add_facts thy =
6865140215ef proper transfer of stored facts;
wenzelm
parents: 61336
diff changeset
    73
      let
6865140215ef proper transfer of stored facts;
wenzelm
parents: 61336
diff changeset
    74
        val transfer = Global_Theory.transfer_theories thy;
6865140215ef proper transfer of stored facts;
wenzelm
parents: 61336
diff changeset
    75
        val facts = Global_Theory.facts_of thy;
6865140215ef proper transfer of stored facts;
wenzelm
parents: 61336
diff changeset
    76
      in Facts.fold_static (add_fact transfer (Facts.space_of facts)) facts end;
33170
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
    77
33769
6d8630fab26a unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents: 33642
diff changeset
    78
    val new_thms =
61507
6865140215ef proper transfer of stored facts;
wenzelm
parents: 61336
diff changeset
    79
      fold add_facts thys []
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58893
diff changeset
    80
      |> sort_distinct (string_ord o apply2 #1);
28810
e915ab11fe52 retrieve thm deps from proof_body;
wenzelm
parents: 27865
diff changeset
    81
33769
6d8630fab26a unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents: 33642
diff changeset
    82
    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
    83
      Proofterm.fold_body_thms
64572
cec07f7249cd tuned signature;
wenzelm
parents: 62848
diff changeset
    84
        (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
    85
        (map Proofterm.strip_thm (Thm.proof_bodies_of (map (#1 o #2) new_thms)))
44331
aa9c1e9ef2ce clarified fulfill_norm_proof: no join_thms yet;
wenzelm
parents: 42473
diff changeset
    86
        Symtab.empty;
33170
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
    87
41565
9718c32f9c4e unused_thms no longer compares propositions, since this is no longer needed
berghofe
parents: 41489
diff changeset
    88
    fun is_unused a = not (Symtab.defined used a);
28810
e915ab11fe52 retrieve thm deps from proof_body;
wenzelm
parents: 27865
diff changeset
    89
57934
5e500c0e7eca tuned signature -- prefer self-contained user-space tool;
wenzelm
parents: 49561
diff changeset
    90
    (*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
    91
    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
    92
      if is_unused a then I
33170
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
    93
      else
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
    94
        (case group of
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
    95
          NONE => I
33769
6d8630fab26a unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents: 33642
diff changeset
    96
        | 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
    97
6d8630fab26a unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents: 33642
diff changeset
    98
    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
    99
      if not concealed andalso
42473
aca720fb3936 mark thm tag "kind" as legacy;
wenzelm
parents: 41565
diff changeset
   100
        (* FIXME replace by robust treatment of thm groups *)
61336
fa4ebbd350ae just one theorem kind, which is legacy anyway;
wenzelm
parents: 60103
diff changeset
   101
        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
   102
      then
33170
dd6d8d1f70d2 maintain group via name space, not tags;
wenzelm
parents: 32810
diff changeset
   103
        (case group of
33769
6d8630fab26a unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents: 33642
diff changeset
   104
           NONE => ((a, th) :: thms, seen_groups)
26185
e53165319347 Added function for finding unused theorems.
berghofe
parents: 26138
diff changeset
   105
         | SOME grp =>
33769
6d8630fab26a unused_thms: show only results from 'theorem(s)' package (via old-style kinds);
wenzelm
parents: 33642
diff changeset
   106
             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
   107
               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
   108
             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
   109
      else q) new_thms ([], Inttab.empty);
26185
e53165319347 Added function for finding unused theorems.
berghofe
parents: 26138
diff changeset
   110
  in rev thms' end;
e53165319347 Added function for finding unused theorems.
berghofe
parents: 26138
diff changeset
   111
7765
fa28bac7903c New function thm_deps for visualizing dependencies of theorems.
berghofe
parents:
diff changeset
   112
end;