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