src/Pure/Thy/thm_deps.ML
author haftmann
Mon May 18 09:48:06 2009 +0200 (2009-05-18)
changeset 31177 c39994cb152a
parent 31174 f1f1e9b53c81
child 32726 a900d3cd47cc
permissions -rw-r--r--
introduced Thm.generatedK
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@26697
     9
  val thm_deps: thm list -> unit
wenzelm@26697
    10
  val unused_thms: theory list * theory list -> (string * thm) list
berghofe@7765
    11
end;
berghofe@7765
    12
wenzelm@26697
    13
structure ThmDeps: THM_DEPS =
berghofe@7765
    14
struct
berghofe@7765
    15
wenzelm@26697
    16
(* thm_deps *)
wenzelm@26697
    17
berghofe@7765
    18
fun thm_deps 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@30364
    23
            val prefix = #1 (Library.split_last (Long_Name.explode name));
wenzelm@28826
    24
            val session =
wenzelm@28826
    25
              (case prefix of
wenzelm@28826
    26
                a :: _ =>
wenzelm@28826
    27
                  (case ThyInfo.lookup_theory 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@28826
    43
    val deps = Proofterm.fold_body_thms add_dep (map Thm.proof_body_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@26697
    51
    fun add_fact (name, ths) =
wenzelm@26697
    52
      if exists (fn thy => PureThy.defined_fact thy name) base_thys then I
wenzelm@26697
    53
      else fold_rev (fn th => (case Thm.get_name th of "" => I | a => cons (a, th))) ths;
wenzelm@26699
    54
    val thms =
wenzelm@26699
    55
      fold (Facts.fold_static add_fact o PureThy.facts_of) thys []
wenzelm@26699
    56
      |> sort_distinct (string_ord o pairself #1);
wenzelm@28810
    57
wenzelm@28810
    58
    val tab = Proofterm.fold_body_thms
wenzelm@28817
    59
      (fn (name, prop, _) => name <> "" ? Symtab.insert_list (op =) (name, prop))
wenzelm@28814
    60
      (map (Proofterm.strip_thm o Thm.proof_body_of o snd) thms) Symtab.empty;
wenzelm@28810
    61
    fun is_unused (name, th) =
wenzelm@28810
    62
      not (member (op aconv) (Symtab.lookup_list tab name) (Thm.prop_of th));
wenzelm@28810
    63
berghofe@26185
    64
    (* groups containing at least one used theorem *)
berghofe@26185
    65
    val grps = fold (fn p as (_, thm) => if is_unused p then I else
wenzelm@27865
    66
      case Thm.get_group thm of
berghofe@26185
    67
        NONE => I | SOME grp => Symtab.update (grp, ())) thms Symtab.empty;
berghofe@26185
    68
    val (thms', _) = fold (fn p as (s, thm) => fn q as (thms, grps') =>
haftmann@31177
    69
      if member (op =) [Thm.theoremK, Thm.generatedK, Thm.lemmaK, Thm.corollaryK, Thm.axiomK] (Thm.get_kind thm)
berghofe@26185
    70
        andalso is_unused p
berghofe@26185
    71
      then
wenzelm@27865
    72
        (case Thm.get_group thm of
berghofe@26185
    73
           NONE => (p :: thms, grps')
berghofe@26185
    74
         | SOME grp =>
berghofe@26185
    75
             (* do not output theorem if another theorem in group has been used *)
wenzelm@26697
    76
             if Symtab.defined grps grp then q
berghofe@26185
    77
             (* output at most one unused theorem per group *)
wenzelm@26697
    78
             else if Symtab.defined grps' grp then q
berghofe@26185
    79
             else (p :: thms, Symtab.update (grp, ()) grps'))
berghofe@26185
    80
      else q) thms ([], Symtab.empty)
berghofe@26185
    81
  in rev thms' end;
berghofe@26185
    82
berghofe@7765
    83
end;
berghofe@7765
    84