src/Pure/Tools/thy_deps.ML
author wenzelm
Thu, 16 Apr 2015 17:18:48 +0200
changeset 60099 73c260342704
parent 60097 d20ca79d50e4
child 60948 b710a5087116
permissions -rw-r--r--
formal Theory.check, with markup and completion;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60093
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Tools/thy_deps.ML
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
     3
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
     4
Visualization of theory dependencies.
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
     5
*)
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
     6
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
     7
signature THY_DEPS =
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
     8
sig
60099
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
     9
  val thy_deps: Proof.context -> theory list option * theory list option -> Graph_Display.entry list
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    10
  val thy_deps_cmd: Proof.context ->
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    11
    (string * Position.T) list option * (string * Position.T) list option -> unit
60093
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    12
end;
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    13
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    14
structure Thy_Deps: THY_DEPS =
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    15
struct
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    16
60099
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    17
fun gen_thy_deps _ ctxt (NONE, NONE) =
60093
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    18
      let
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    19
        val parent_session = Session.get_name ();
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    20
        val parent_loaded = is_some o Thy_Info.lookup_theory;
60099
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    21
      in Present.session_graph parent_session parent_loaded (Proof_Context.theory_of ctxt) end
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    22
  | gen_thy_deps prep_thy ctxt bounds =
60093
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    23
      let
60099
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    24
        val (upper, lower) = apply2 ((Option.map o map) (prep_thy ctxt)) bounds;
60093
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    25
        val rel = Theory.subthy o swap;
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    26
        val pred =
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    27
          (case upper of
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    28
            SOME Bs => (fn thy => exists (fn B => rel (thy, B)) Bs)
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    29
          | NONE => K true) andf
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    30
          (case lower of
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    31
            SOME Bs => (fn thy => exists (fn B => rel (B, thy)) Bs)
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    32
          | NONE => K true);
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    33
        fun node thy =
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    34
          ((Context.theory_name thy, Graph_Display.content_node (Context.theory_name thy) []),
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    35
            map Context.theory_name (filter pred (Theory.parents_of thy)));
60099
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    36
      in map node (filter pred (Theory.nodes_of (Proof_Context.theory_of ctxt))) end;
60093
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    37
60099
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    38
val thy_deps =
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    39
  gen_thy_deps (fn ctxt => fn thy =>
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    40
    let val thy0 = Proof_Context.theory_of ctxt
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    41
    in if Theory.subthy (thy, thy0) then thy else raise THEORY ("Bad theory", [thy, thy0]) end);
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    42
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    43
val thy_deps_cmd = Graph_Display.display_graph oo gen_thy_deps Theory.check;
60093
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    44
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    45
val theory_bounds =
60099
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    46
  Parse.position Parse.theory_xname >> single ||
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    47
  (@{keyword "("} |-- Parse.enum "|" (Parse.position Parse.theory_xname) --| @{keyword ")"});
60093
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    48
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    49
val _ =
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    50
  Outer_Syntax.command @{command_keyword thy_deps} "visualize theory dependencies"
60097
d20ca79d50e4 discontinued pointless warnings: commands are only defined inside a theory context;
wenzelm
parents: 60093
diff changeset
    51
    (Scan.option theory_bounds -- Scan.option theory_bounds >>
60099
73c260342704 formal Theory.check, with markup and completion;
wenzelm
parents: 60097
diff changeset
    52
      (fn args => Toplevel.keep (fn st => thy_deps_cmd (Toplevel.context_of st) args)));
60093
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    53
c48d536231fe clarified thy_deps;
wenzelm
parents:
diff changeset
    54
end;