src/Pure/Tools/class_deps.ML
author wenzelm
Wed Nov 26 20:05:34 2014 +0100 (2014-11-26)
changeset 59058 a78612c67ec0
parent 58893 9e0ecb66d6a7
child 59206 36808125e00f
permissions -rw-r--r--
renamed "pairself" to "apply2", in accordance to @{apply 2};
haftmann@58201
     1
(*  Title:      Pure/Tools/class_deps.ML
haftmann@58201
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@58201
     3
haftmann@58201
     4
Visualization of class dependencies.
haftmann@58201
     5
*)
haftmann@58201
     6
haftmann@58201
     7
signature CLASS_DEPS =
haftmann@58201
     8
sig
haftmann@58202
     9
  val visualize: Proof.context -> sort -> sort option -> unit
haftmann@58202
    10
  val visualize_cmd: Proof.context -> string -> string option -> unit
haftmann@58201
    11
end;
haftmann@58201
    12
haftmann@58201
    13
structure Class_deps: CLASS_DEPS =
haftmann@58201
    14
struct
haftmann@58201
    15
haftmann@58202
    16
fun gen_visualize prep_sort ctxt raw_super raw_sub =
haftmann@58201
    17
  let
haftmann@58202
    18
    val super = prep_sort ctxt raw_super;
haftmann@58202
    19
    val sub = Option.map (prep_sort ctxt) raw_sub;
haftmann@58202
    20
    val {classes = (space, original_algebra), ...} = Type.rep_tsig (Proof_Context.tsig_of ctxt);
haftmann@58202
    21
    fun le_super class = Sorts.sort_le original_algebra ([class], super);
haftmann@58202
    22
    val sub_le = case sub of
haftmann@58202
    23
      NONE => K true |
haftmann@58202
    24
      SOME sub => fn class => Sorts.sort_le original_algebra (sub, [class]);
haftmann@58202
    25
    val (_, algebra) = Sorts.subalgebra (Context.pretty ctxt)
haftmann@58202
    26
      (le_super andf sub_le) (K NONE) original_algebra;
haftmann@58201
    27
    val classes = Sorts.classes_of algebra;
haftmann@58201
    28
    fun entry (c, (i, (_, cs))) =
haftmann@58201
    29
      (i, {name = Name_Space.extern ctxt space c, ID = c, parents = Graph.Keys.dest cs,
haftmann@58201
    30
            dir = "", unfold = true, path = "", content = []});
haftmann@58201
    31
    val gr =
haftmann@58201
    32
      Graph.fold (cons o entry) classes []
wenzelm@59058
    33
      |> sort (int_ord o apply2 #1) |> map #2;
haftmann@58202
    34
  in Graph_Display.display_graph gr end;
haftmann@58202
    35
haftmann@58202
    36
val visualize = gen_visualize (Type.cert_sort o Proof_Context.tsig_of);
haftmann@58202
    37
val visualize_cmd = gen_visualize Syntax.read_sort;
haftmann@58201
    38
haftmann@58201
    39
val _ =
wenzelm@58893
    40
  Outer_Syntax.command @{command_spec "class_deps"} "visualize class dependencies"
haftmann@58202
    41
    ((Scan.optional Parse.sort "{}" -- Scan.option Parse.sort) >> (fn (raw_super, raw_sub) =>
haftmann@58202
    42
      ((Toplevel.unknown_theory oo Toplevel.keep) (fn st => visualize_cmd (Toplevel.context_of st) raw_super raw_sub))));
haftmann@58201
    43
haftmann@58201
    44
end;