src/Pure/Thy/thm_deps.ML
author wenzelm
Tue Sep 25 15:40:41 2012 +0200 (2012-09-25)
changeset 49561 26fc70e983c2
parent 44333 cc53ce50f738
permissions -rw-r--r--
separate module Graph_Display;
tuned signature;
berghofe@7765
     1
(*  Title:      Pure/Thy/thm_deps.ML
berghofe@7765
     2
    Author:     Stefan Berghofer, TU Muenchen
berghofe@7765
     3
berghofe@7765
     4
Visualize dependencies of theorems.
berghofe@7765
     5
*)
berghofe@7765
     6
berghofe@7765
     7
signature THM_DEPS =
berghofe@7765
     8
sig
wenzelm@37870
     9
  val thm_deps: theory -> thm list -> unit
wenzelm@26697
    10
  val unused_thms: theory list * theory list -> (string * thm) list
berghofe@7765
    11
end;
berghofe@7765
    12
wenzelm@33391
    13
structure Thm_Deps: THM_DEPS =
berghofe@7765
    14
struct
berghofe@7765
    15
wenzelm@26697
    16
(* thm_deps *)
wenzelm@26697
    17
wenzelm@37870
    18
fun thm_deps thy thms =
wenzelm@28810
    19
  let
wenzelm@28826
    20
    fun add_dep ("", _, _) = I
wenzelm@28826
    21
      | add_dep (name, _, PBody {thms = thms', ...}) =
wenzelm@28826
    22
          let
wenzelm@41489
    23
            val prefix = #1 (split_last (Long_Name.explode name));
wenzelm@28826
    24
            val session =
wenzelm@28826
    25
              (case prefix of
wenzelm@28826
    26
                a :: _ =>
wenzelm@37870
    27
                  (case try (Context.get_theory thy) a of
wenzelm@28826
    28
                    SOME thy =>
wenzelm@28826
    29
                      (case Present.session_name thy of
wenzelm@28826
    30
                        "" => []
wenzelm@28826
    31
                      | session => [session])
wenzelm@28826
    32
                  | NONE => [])
wenzelm@28826
    33
               | _ => ["global"]);
wenzelm@28826
    34
            val parents = filter_out (fn s => s = "") (map (#1 o #2) thms');
wenzelm@28826
    35
            val entry =
wenzelm@30364
    36
              {name = Long_Name.base_name name,
wenzelm@28826
    37
               ID = name,
wenzelm@28826
    38
               dir = space_implode "/" (session @ prefix),
wenzelm@28826
    39
               unfold = false,
wenzelm@28826
    40
               path = "",
wenzelm@49561
    41
               parents = parents,
wenzelm@49561
    42
               content = []};
wenzelm@28826
    43
          in cons entry end;
wenzelm@44333
    44
    val deps = Proofterm.fold_body_thms add_dep (Thm.proof_bodies_of thms) [];
wenzelm@49561
    45
  in Graph_Display.display_graph (sort_wrt #ID deps) end;
wenzelm@26697
    46
wenzelm@26697
    47
wenzelm@26697
    48
(* unused_thms *)
wenzelm@26697
    49
wenzelm@26697
    50
fun unused_thms (base_thys, thys) =
berghofe@26185
    51
  let
wenzelm@33170
    52
    fun add_fact space (name, ths) =
wenzelm@39557
    53
      if exists (fn thy => Global_Theory.defined_fact thy name) base_thys then I
wenzelm@33170
    54
      else
wenzelm@33170
    55
        let val {concealed, group, ...} = Name_Space.the_entry space name in
wenzelm@33170
    56
          fold_rev (fn th =>
wenzelm@36744
    57
            (case Thm.derivation_name th of
wenzelm@33170
    58
              "" => I
wenzelm@33170
    59
            | a => cons (a, (th, concealed, group)))) ths
wenzelm@33170
    60
        end;
wenzelm@33170
    61
    fun add_facts facts = Facts.fold_static (add_fact (Facts.space_of facts)) facts;
wenzelm@33170
    62
wenzelm@33769
    63
    val new_thms =
wenzelm@39557
    64
      fold (add_facts o Global_Theory.facts_of) thys []
wenzelm@26699
    65
      |> sort_distinct (string_ord o pairself #1);
wenzelm@28810
    66
wenzelm@33769
    67
    val used =
wenzelm@32810
    68
      Proofterm.fold_body_thms
berghofe@41565
    69
        (fn (a, _, _) => a <> "" ? Symtab.update (a, ()))
wenzelm@44333
    70
        (map Proofterm.strip_thm (Thm.proof_bodies_of (map (#1 o #2) new_thms)))
wenzelm@44331
    71
        Symtab.empty;
wenzelm@33170
    72
berghofe@41565
    73
    fun is_unused a = not (Symtab.defined used a);
wenzelm@28810
    74
berghofe@26185
    75
    (* groups containing at least one used theorem *)
berghofe@41565
    76
    val used_groups = fold (fn (a, (_, _, group)) =>
berghofe@41565
    77
      if is_unused a then I
wenzelm@33170
    78
      else
wenzelm@33170
    79
        (case group of
wenzelm@33170
    80
          NONE => I
wenzelm@33769
    81
        | SOME grp => Inttab.update (grp, ()))) new_thms Inttab.empty;
wenzelm@33769
    82
wenzelm@33769
    83
    val (thms', _) = fold (fn (a, (th, concealed, group)) => fn q as (thms, seen_groups) =>
wenzelm@33769
    84
      if not concealed andalso
wenzelm@42473
    85
        (* FIXME replace by robust treatment of thm groups *)
wenzelm@42473
    86
        member (op =) [Thm.theoremK, Thm.lemmaK, Thm.corollaryK] (Thm.legacy_get_kind th) andalso
berghofe@41565
    87
        is_unused a
wenzelm@33769
    88
      then
wenzelm@33170
    89
        (case group of
wenzelm@33769
    90
           NONE => ((a, th) :: thms, seen_groups)
berghofe@26185
    91
         | SOME grp =>
wenzelm@33769
    92
             if Inttab.defined used_groups grp orelse
wenzelm@33769
    93
               Inttab.defined seen_groups grp then q
wenzelm@33769
    94
             else ((a, th) :: thms, Inttab.update (grp, ()) seen_groups))
wenzelm@33769
    95
      else q) new_thms ([], Inttab.empty);
berghofe@26185
    96
  in rev thms' end;
berghofe@26185
    97
berghofe@7765
    98
end;
berghofe@7765
    99