src/Pure/Tools/thy_deps.ML
author wenzelm
Sun, 04 Sep 2016 21:09:18 +0200
changeset 63781 af9fe0b6b78e
parent 62848 e4140efe699e
child 65491 7fb81fa1d668
permissions -rw-r--r--
support for (single) primary key; more operations;
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;
60948
b710a5087116 prefer theory_id operations;
wenzelm
parents: 60099
diff changeset
    25
        val rel = Context.subthy o swap;
60093
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
60948
b710a5087116 prefer theory_id operations;
wenzelm
parents: 60099
diff changeset
    41
    in if Context.subthy (thy, thy0) then thy else raise THEORY ("Bad theory", [thy, thy0]) end);
60099
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
end;