src/Pure/Thy/thm_deps.ML
author wenzelm
Thu Oct 07 14:32:18 1999 +0200 (1999-10-07)
changeset 7783 9ace4017ead8
parent 7765 fa28bac7903c
child 7785 c06825c396e8
permissions -rw-r--r--
$ISATOOL;
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
berghofe@7765
    10
  val thm_deps: thm list -> unit
berghofe@7765
    11
end;
berghofe@7765
    12
berghofe@7765
    13
structure ThmDeps : THM_DEPS =
berghofe@7765
    14
struct
berghofe@7765
    15
berghofe@7765
    16
fun get_sess thy = if Theory.eq_thy (thy, ProtoPure.thy) then ["Pure"]
berghofe@7765
    17
  else
berghofe@7765
    18
    (case #session (Present.get_info thy) of
berghofe@7765
    19
        [x] => [x, "base"]
berghofe@7765
    20
      | xs => xs);
berghofe@7765
    21
berghofe@7765
    22
fun put_graph gr path =
berghofe@7765
    23
    File.write path (cat_lines (map (fn {name, ID, dir, unfold, path, parents} =>
berghofe@7765
    24
      "\"" ^ name ^ "\" \"" ^ ID ^ "\" \"" ^ dir ^ (if unfold then "\" + \"" else "\" \"") ^
berghofe@7765
    25
      path ^ "\" > " ^ space_implode " " (map quote parents) ^ " ;") gr));
berghofe@7765
    26
berghofe@7765
    27
fun is_thm_axm (Theorem _) = true
berghofe@7765
    28
  | is_thm_axm (Axiom _) = true
berghofe@7765
    29
  | is_thm_axm _ = false;
berghofe@7765
    30
berghofe@7765
    31
fun get_name (Theorem (s, _)) = s
berghofe@7765
    32
  | get_name (Axiom (s, _)) = s
berghofe@7765
    33
  | get_name _ = "";
berghofe@7765
    34
berghofe@7765
    35
fun make_deps_graph ((gra, parents), Join (ta, ders)) =
berghofe@7765
    36
  let
berghofe@7765
    37
    val name = get_name ta;
berghofe@7765
    38
  in
berghofe@7765
    39
    if is_thm_axm ta then
berghofe@7765
    40
      if is_none (Symtab.lookup (gra, name)) then
berghofe@7765
    41
        let
berghofe@7765
    42
          val (gra', parents') = foldl make_deps_graph ((gra, []), ders);
berghofe@7765
    43
          val prefx = rev (tl (rev (NameSpace.unpack name)));
berghofe@7765
    44
          val session = (case prefx of
berghofe@7765
    45
               (x :: _) => (case ThyInfo.lookup_theory x of
berghofe@7765
    46
                     (Some thy) => get_sess thy
berghofe@7765
    47
                   | None => [])
berghofe@7765
    48
             | _ => ["global"])
berghofe@7765
    49
        in
berghofe@7765
    50
          (Symtab.update_new ((name,
berghofe@7765
    51
            {name = Sign.base_name name, ID = name,
berghofe@7765
    52
             dir = space_implode "/" (session @ prefx),
berghofe@7765
    53
             unfold = false, path = "", parents = parents'}), gra'), name ins parents)
berghofe@7765
    54
        end
berghofe@7765
    55
      else (gra, name ins parents)
berghofe@7765
    56
    else
berghofe@7765
    57
      foldl make_deps_graph ((gra, parents), ders)
berghofe@7765
    58
  end;
berghofe@7765
    59
berghofe@7765
    60
fun thm_deps thms =
berghofe@7765
    61
  let
berghofe@7765
    62
    val _ = writeln "Generating graph ...";
berghofe@7765
    63
    val gra = map snd (Symtab.dest (fst (foldl make_deps_graph ((Symtab.empty, []),
berghofe@7765
    64
      map (#der o rep_thm) thms))));
berghofe@7765
    65
    val path = File.tmp_path (Path.unpack "theorems.graph");
berghofe@7765
    66
    val _ = put_graph gra path;
wenzelm@7783
    67
    val _ = execute ("$ISATOOL browser -d " ^ Path.pack (Path.expand path) ^ " &");
berghofe@7765
    68
  in () end;
berghofe@7765
    69
berghofe@7765
    70
end;
berghofe@7765
    71
berghofe@7765
    72
open ThmDeps;