src/Pure/Thy/thm_deps.ML
author wenzelm
Mon Nov 17 21:28:46 2008 +0100 (2008-11-17)
changeset 28826 3b460b6eadae
parent 28817 c8cc94a470d4
child 29606 fedb8be05f24
permissions -rw-r--r--
simplified thm_deps -- no need to build a graph datastructure;
berghofe@7765
     1
(*  Title:      Pure/Thy/thm_deps.ML
berghofe@7765
     2
    ID:         $Id$
berghofe@7765
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@7765
     4
berghofe@7765
     5
Visualize dependencies of theorems.
berghofe@7765
     6
*)
berghofe@7765
     7
berghofe@7765
     8
signature THM_DEPS =
berghofe@7765
     9
sig
wenzelm@26697
    10
  val thm_deps: thm list -> unit
wenzelm@26697
    11
  val unused_thms: theory list * theory list -> (string * thm) list
berghofe@7765
    12
end;
berghofe@7765
    13
wenzelm@26697
    14
structure ThmDeps: THM_DEPS =
berghofe@7765
    15
struct
berghofe@7765
    16
wenzelm@26697
    17
(* thm_deps *)
wenzelm@26697
    18
berghofe@7765
    19
fun thm_deps thms =
wenzelm@28810
    20
  let
wenzelm@28826
    21
    fun add_dep ("", _, _) = I
wenzelm@28826
    22
      | add_dep (name, _, PBody {thms = thms', ...}) =
wenzelm@28826
    23
          let
wenzelm@28826
    24
            val prefix = #1 (Library.split_last (NameSpace.explode name));
wenzelm@28826
    25
            val session =
wenzelm@28826
    26
              (case prefix of
wenzelm@28826
    27
                a :: _ =>
wenzelm@28826
    28
                  (case ThyInfo.lookup_theory a of
wenzelm@28826
    29
                    SOME thy =>
wenzelm@28826
    30
                      (case Present.session_name thy of
wenzelm@28826
    31
                        "" => []
wenzelm@28826
    32
                      | session => [session])
wenzelm@28826
    33
                  | NONE => [])
wenzelm@28826
    34
               | _ => ["global"]);
wenzelm@28826
    35
            val parents = filter_out (fn s => s = "") (map (#1 o #2) thms');
wenzelm@28826
    36
            val entry =
wenzelm@28826
    37
              {name = Sign.base_name name,
wenzelm@28826
    38
               ID = name,
wenzelm@28826
    39
               dir = space_implode "/" (session @ prefix),
wenzelm@28826
    40
               unfold = false,
wenzelm@28826
    41
               path = "",
wenzelm@28826
    42
               parents = parents};
wenzelm@28826
    43
          in cons entry end;
wenzelm@28826
    44
    val deps = Proofterm.fold_body_thms add_dep (map Thm.proof_body_of thms) [];
wenzelm@28826
    45
  in Present.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@26697
    52
    fun add_fact (name, ths) =
wenzelm@26697
    53
      if exists (fn thy => PureThy.defined_fact thy name) base_thys then I
wenzelm@26697
    54
      else fold_rev (fn th => (case Thm.get_name th of "" => I | a => cons (a, th))) ths;
wenzelm@26699
    55
    val thms =
wenzelm@26699
    56
      fold (Facts.fold_static add_fact o PureThy.facts_of) thys []
wenzelm@26699
    57
      |> sort_distinct (string_ord o pairself #1);
wenzelm@28810
    58
wenzelm@28810
    59
    val tab = Proofterm.fold_body_thms
wenzelm@28817
    60
      (fn (name, prop, _) => name <> "" ? Symtab.insert_list (op =) (name, prop))
wenzelm@28814
    61
      (map (Proofterm.strip_thm o Thm.proof_body_of o snd) thms) Symtab.empty;
wenzelm@28810
    62
    fun is_unused (name, th) =
wenzelm@28810
    63
      not (member (op aconv) (Symtab.lookup_list tab name) (Thm.prop_of th));
wenzelm@28810
    64
berghofe@26185
    65
    (* groups containing at least one used theorem *)
berghofe@26185
    66
    val grps = fold (fn p as (_, thm) => if is_unused p then I else
wenzelm@27865
    67
      case Thm.get_group thm of
berghofe@26185
    68
        NONE => I | SOME grp => Symtab.update (grp, ())) thms Symtab.empty;
berghofe@26185
    69
    val (thms', _) = fold (fn p as (s, thm) => fn q as (thms, grps') =>
wenzelm@27865
    70
      if member (op =) [Thm.theoremK, Thm.lemmaK, Thm.corollaryK, Thm.axiomK] (Thm.get_kind thm)
berghofe@26185
    71
        andalso is_unused p
berghofe@26185
    72
      then
wenzelm@27865
    73
        (case Thm.get_group thm of
berghofe@26185
    74
           NONE => (p :: thms, grps')
berghofe@26185
    75
         | SOME grp =>
berghofe@26185
    76
             (* do not output theorem if another theorem in group has been used *)
wenzelm@26697
    77
             if Symtab.defined grps grp then q
berghofe@26185
    78
             (* output at most one unused theorem per group *)
wenzelm@26697
    79
             else if Symtab.defined grps' grp then q
berghofe@26185
    80
             else (p :: thms, Symtab.update (grp, ()) grps'))
berghofe@26185
    81
      else q) thms ([], Symtab.empty)
berghofe@26185
    82
  in rev thms' end;
berghofe@26185
    83
berghofe@7765
    84
end;
berghofe@7765
    85