src/Pure/Thy/thm_deps.ML
author wenzelm
Fri Jan 27 19:03:02 2006 +0100 (2006-01-27)
changeset 18799 f137c5e971f5
parent 17412 e26cb20ef0cc
child 20578 f26c8408a675
permissions -rw-r--r--
moved theorem tags from Drule to PureThy;
     1 (*  Title:      Pure/Thy/thm_deps.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer, TU Muenchen
     4 
     5 Visualize dependencies of theorems.
     6 *)
     7 
     8 signature BASIC_THM_DEPS =
     9 sig
    10   val thm_deps : thm list -> unit
    11 end;
    12 
    13 signature THM_DEPS =
    14 sig
    15   include BASIC_THM_DEPS
    16   val enable : unit -> unit
    17   val disable : unit -> unit
    18 end;
    19 
    20 structure ThmDeps : THM_DEPS =
    21 struct
    22 
    23 open Proofterm;
    24 
    25 fun enable () = if ! proofs = 0 then proofs := 1 else ();
    26 fun disable () = proofs := 0;
    27 
    28 fun dest_thm_axm (PThm (nt, prf, _, _)) = (nt, prf)
    29   | dest_thm_axm (PAxm (n, _, _)) = ((n ^ " (Ax)", []), MinProof ([], [], []))
    30   | dest_thm_axm _ = (("", []), MinProof ([], [], []));
    31 
    32 fun make_deps_graph (AbsP (_, _, prf)) = make_deps_graph prf
    33   | make_deps_graph (Abst (_, _, prf)) = make_deps_graph prf
    34   | make_deps_graph (prf1 %% prf2) = make_deps_graph prf1 #> make_deps_graph prf2
    35   | make_deps_graph (prf % _) = make_deps_graph prf
    36   | make_deps_graph (MinProof (thms, axms, _)) =
    37       fold (make_deps_graph o Proofterm.proof_of_min_thm) thms #>
    38       fold (make_deps_graph o Proofterm.proof_of_min_axm) axms
    39   | make_deps_graph prf = (fn p as (gra, parents) =>
    40       let val ((name, tags), prf') = dest_thm_axm prf
    41       in
    42         if name <> "" andalso not (PureThy.has_internal tags) then
    43           if not (Symtab.defined gra name) then
    44             let
    45               val (gra', parents') = make_deps_graph prf' (gra, []);
    46               val prefx = #1 (Library.split_last (NameSpace.unpack name));
    47               val session =
    48                 (case prefx of
    49                   (x :: _) =>
    50                     (case ThyInfo.lookup_theory x of
    51                       SOME thy =>
    52                         let val name = #name (Present.get_info thy)
    53                         in if name = "" then [] else [name] end
    54                     | NONE => [])
    55                  | _ => ["global"]);
    56             in
    57               if name mem parents' then (gra', parents union parents')
    58               else (Symtab.update (name,
    59                 {name = Sign.base_name name, ID = name,
    60                  dir = space_implode "/" (session @ prefx),
    61                  unfold = false, path = "", parents = parents'}) gra',
    62                name ins parents)
    63             end
    64           else (gra, name ins parents)
    65         else
    66           make_deps_graph prf' (gra, parents)
    67       end);
    68 
    69 fun thm_deps thms =
    70   let
    71     val _ = writeln "Generating graph ...";
    72     val gra = map snd (Symtab.dest (fst
    73       (fold make_deps_graph (map Thm.proof_of thms) (Symtab.empty, []))));
    74     val path = File.tmp_path (Path.unpack "theorems.graph");
    75     val _ = Present.write_graph gra path;
    76     val _ = File.isatool ("browser -d " ^ File.shell_path path ^ " &");
    77   in () end;
    78 
    79 end;
    80 
    81 structure BasicThmDeps : BASIC_THM_DEPS = ThmDeps;
    82 open BasicThmDeps;