src/Pure/Thy/thm_deps.ML
author wenzelm
Sat Aug 20 19:21:03 2011 +0200 (2011-08-20)
changeset 44333 cc53ce50f738
parent 44331 aa9c1e9ef2ce
child 49561 26fc70e983c2
permissions -rw-r--r--
reverted to join_bodies/join_proofs based on fold_body_thms to regain performance (escpecially of HOL-Proofs) -- see also aa9c1e9ef2ce and 4e2abb045eac;
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@28826
    41
               parents = parents};
wenzelm@28826
    42
          in cons entry end;
wenzelm@44333
    43
    val deps = Proofterm.fold_body_thms add_dep (Thm.proof_bodies_of thms) [];
wenzelm@28826
    44
  in Present.display_graph (sort_wrt #ID deps) end;
wenzelm@26697
    45
wenzelm@26697
    46
wenzelm@26697
    47
(* unused_thms *)
wenzelm@26697
    48
wenzelm@26697
    49
fun unused_thms (base_thys, thys) =
berghofe@26185
    50
  let
wenzelm@33170
    51
    fun add_fact space (name, ths) =
wenzelm@39557
    52
      if exists (fn thy => Global_Theory.defined_fact thy name) base_thys then I
wenzelm@33170
    53
      else
wenzelm@33170
    54
        let val {concealed, group, ...} = Name_Space.the_entry space name in
wenzelm@33170
    55
          fold_rev (fn th =>
wenzelm@36744
    56
            (case Thm.derivation_name th of
wenzelm@33170
    57
              "" => I
wenzelm@33170
    58
            | a => cons (a, (th, concealed, group)))) ths
wenzelm@33170
    59
        end;
wenzelm@33170
    60
    fun add_facts facts = Facts.fold_static (add_fact (Facts.space_of facts)) facts;
wenzelm@33170
    61
wenzelm@33769
    62
    val new_thms =
wenzelm@39557
    63
      fold (add_facts o Global_Theory.facts_of) thys []
wenzelm@26699
    64
      |> sort_distinct (string_ord o pairself #1);
wenzelm@28810
    65
wenzelm@33769
    66
    val used =
wenzelm@32810
    67
      Proofterm.fold_body_thms
berghofe@41565
    68
        (fn (a, _, _) => a <> "" ? Symtab.update (a, ()))
wenzelm@44333
    69
        (map Proofterm.strip_thm (Thm.proof_bodies_of (map (#1 o #2) new_thms)))
wenzelm@44331
    70
        Symtab.empty;
wenzelm@33170
    71
berghofe@41565
    72
    fun is_unused a = not (Symtab.defined used a);
wenzelm@28810
    73
berghofe@26185
    74
    (* groups containing at least one used theorem *)
berghofe@41565
    75
    val used_groups = fold (fn (a, (_, _, group)) =>
berghofe@41565
    76
      if is_unused a then I
wenzelm@33170
    77
      else
wenzelm@33170
    78
        (case group of
wenzelm@33170
    79
          NONE => I
wenzelm@33769
    80
        | SOME grp => Inttab.update (grp, ()))) new_thms Inttab.empty;
wenzelm@33769
    81
wenzelm@33769
    82
    val (thms', _) = fold (fn (a, (th, concealed, group)) => fn q as (thms, seen_groups) =>
wenzelm@33769
    83
      if not concealed andalso
wenzelm@42473
    84
        (* FIXME replace by robust treatment of thm groups *)
wenzelm@42473
    85
        member (op =) [Thm.theoremK, Thm.lemmaK, Thm.corollaryK] (Thm.legacy_get_kind th) andalso
berghofe@41565
    86
        is_unused a
wenzelm@33769
    87
      then
wenzelm@33170
    88
        (case group of
wenzelm@33769
    89
           NONE => ((a, th) :: thms, seen_groups)
berghofe@26185
    90
         | SOME grp =>
wenzelm@33769
    91
             if Inttab.defined used_groups grp orelse
wenzelm@33769
    92
               Inttab.defined seen_groups grp then q
wenzelm@33769
    93
             else ((a, th) :: thms, Inttab.update (grp, ()) seen_groups))
wenzelm@33769
    94
      else q) new_thms ([], Inttab.empty);
berghofe@26185
    95
  in rev thms' end;
berghofe@26185
    96
berghofe@7765
    97
end;
berghofe@7765
    98