src/Pure/Thy/thy_info.ML
author paulson
Sat Mar 26 18:20:29 2005 +0100 (2005-03-26)
changeset 15633 741deccec4e3
parent 15570 8d8c70b41bab
child 15801 d2f5ca3c048d
permissions -rw-r--r--
new display of theory stamps
berghofe@3604
     1
(*  Title:      Pure/Thy/thy_info.ML
berghofe@3604
     2
    ID:         $Id$
wenzelm@6211
     3
    Author:     Markus Wenzel, TU Muenchen
berghofe@3604
     4
wenzelm@6362
     5
Theory loader database, including theory and file dependencies.
wenzelm@3976
     6
*)
berghofe@3604
     7
wenzelm@5209
     8
signature BASIC_THY_INFO =
wenzelm@5209
     9
sig
wenzelm@5209
    10
  val theory: string -> theory
wenzelm@6211
    11
  val theory_of_sign: Sign.sg -> theory
wenzelm@6211
    12
  val theory_of_thm: thm -> theory
wenzelm@6241
    13
(*val use: string -> unit*)             (*exported later*)
wenzelm@6241
    14
  val time_use: string -> unit
wenzelm@6211
    15
  val touch_thy: string -> unit
wenzelm@6211
    16
  val use_thy: string -> unit
wenzelm@6211
    17
  val update_thy: string -> unit
wenzelm@6666
    18
  val remove_thy: string -> unit
wenzelm@6211
    19
  val time_use_thy: string -> unit
wenzelm@6527
    20
  val use_thy_only: string -> unit
wenzelm@7099
    21
  val update_thy_only: string -> unit
wenzelm@6211
    22
end;
wenzelm@5209
    23
berghofe@3604
    24
signature THY_INFO =
berghofe@3604
    25
sig
wenzelm@5209
    26
  include BASIC_THY_INFO
wenzelm@7099
    27
  datatype action = Update | Outdate | Remove
wenzelm@7099
    28
  val str_of_action: action -> string
wenzelm@7099
    29
  val add_hook: (action -> string -> unit) -> unit
wenzelm@6211
    30
  val names: unit -> string list
wenzelm@7910
    31
  val known_thy: string -> bool
wenzelm@7935
    32
  val check_known_thy: string -> bool
wenzelm@7935
    33
  val if_known_thy: (string -> unit) -> string -> unit
wenzelm@7288
    34
  val lookup_theory: string -> theory option
wenzelm@6211
    35
  val get_theory: string -> theory
berghofe@6654
    36
  val get_preds: string -> string list
wenzelm@7935
    37
  val loaded_files: string -> Path.T list
wenzelm@7910
    38
  val touch_child_thys: string -> unit
wenzelm@7099
    39
  val touch_all_thys: unit -> unit
wenzelm@7941
    40
  val load_file: bool -> Path.T -> unit
wenzelm@6241
    41
  val use: string -> unit
wenzelm@7952
    42
  val quiet_update_thy: bool -> string -> unit
wenzelm@9822
    43
  val pretend_use_thy_only: string -> unit
berghofe@15158
    44
  val begin_theory: (Path.T option -> string -> string list ->
berghofe@15158
    45
      (Path.T * bool) list -> theory -> theory) ->
wenzelm@8805
    46
    bool -> string -> string list -> (Path.T * bool) list -> theory
wenzelm@6211
    47
  val end_theory: theory -> theory
wenzelm@6666
    48
  val finish: unit -> unit
wenzelm@6211
    49
  val register_theory: theory -> unit
paulson@15633
    50
  val pretty_theory: theory -> Pretty.T
paulson@15633
    51
  val pprint_theory: theory -> pprint_args -> unit
berghofe@3604
    52
end;
berghofe@3604
    53
wenzelm@6362
    54
structure ThyInfo: THY_INFO =
wenzelm@6211
    55
struct
berghofe@3604
    56
berghofe@3604
    57
wenzelm@7099
    58
(** theory loader actions and hooks **)
wenzelm@7099
    59
wenzelm@7099
    60
datatype action = Update | Outdate | Remove;
wenzelm@7099
    61
val str_of_action = fn Update => "Update" | Outdate => "Outdate" | Remove => "Remove";
wenzelm@7099
    62
wenzelm@7099
    63
local
wenzelm@7099
    64
  val hooks = ref ([]: (action -> string -> unit) list);
wenzelm@7099
    65
in
wenzelm@7099
    66
  fun add_hook f = hooks := f :: ! hooks;
skalberg@15570
    67
  fun perform action name = List.app (fn f => (try (fn () => f action name) (); ())) (! hooks);
wenzelm@7099
    68
end;
wenzelm@7099
    69
wenzelm@7099
    70
wenzelm@7099
    71
wenzelm@6211
    72
(** thy database **)
wenzelm@6211
    73
wenzelm@6211
    74
(* messages *)
wenzelm@6211
    75
wenzelm@6211
    76
fun gen_msg txt [] = txt
wenzelm@6211
    77
  | gen_msg txt names = txt ^ " " ^ commas_quote names;
wenzelm@6211
    78
wenzelm@6211
    79
fun loader_msg txt names = gen_msg ("Theory loader: " ^ txt) names;
wenzelm@6211
    80
wenzelm@6211
    81
val show_path = space_implode " via " o map quote;
wenzelm@9332
    82
fun cycle_msg names = loader_msg ("cyclic dependency of " ^ show_path names) [];
berghofe@3604
    83
berghofe@3604
    84
wenzelm@6666
    85
(* derived graph operations *)
berghofe@3604
    86
wenzelm@9327
    87
fun add_deps name parents G = Graph.add_deps_acyclic (name, parents) G
wenzelm@9332
    88
  handle Graph.CYCLES namess => error (cat_lines (map cycle_msg namess));
berghofe@3604
    89
wenzelm@7952
    90
fun upd_deps name entry G =
skalberg@15570
    91
  Library.foldl (fn (H, parent) => Graph.del_edge (parent, name) H) (G, Graph.imm_preds G name)
wenzelm@7952
    92
  |> Graph.map_node name (K entry);
wenzelm@3976
    93
wenzelm@6211
    94
fun update_node name parents entry G =
wenzelm@7952
    95
  (if can (Graph.get_node G) name then upd_deps name entry G else Graph.new_node (name, entry) G)
wenzelm@7952
    96
  |> add_deps name parents;
wenzelm@7952
    97
   
berghofe@3604
    98
berghofe@3604
    99
wenzelm@6211
   100
(* thy database *)
wenzelm@6211
   101
wenzelm@6211
   102
type deps =
wenzelm@6211
   103
  {present: bool, outdated: bool,
wenzelm@7941
   104
    master: ThyLoad.master option, files: (Path.T * (Path.T * File.info) option) list};
wenzelm@6211
   105
wenzelm@6241
   106
fun make_deps present outdated master files =
wenzelm@6241
   107
  {present = present, outdated = outdated, master = master, files = files}: deps;
berghofe@3604
   108
skalberg@15531
   109
fun init_deps master files = SOME (make_deps false true master (map (rpair NONE) files));
wenzelm@7211
   110
wenzelm@7211
   111
wenzelm@6362
   112
type thy = deps option * theory option;
berghofe@3604
   113
wenzelm@6211
   114
local
wenzelm@6362
   115
  val database = ref (Graph.empty: thy Graph.T);
wenzelm@6211
   116
in
wenzelm@6362
   117
  fun get_thys () = ! database;
wenzelm@9137
   118
  val change_thys = Library.change database;
berghofe@3604
   119
end;
wenzelm@5209
   120
wenzelm@5209
   121
wenzelm@6211
   122
(* access thy graph *)
wenzelm@6211
   123
wenzelm@6211
   124
fun thy_graph f x = f (get_thys ()) x;
wenzelm@9417
   125
wenzelm@9417
   126
(*theory names in topological order*)
wenzelm@9417
   127
fun get_names () =
wenzelm@9417
   128
  let val G = get_thys ()
wenzelm@14774
   129
  in Graph.all_succs G (Graph.minimals G) end;
wenzelm@6211
   130
wenzelm@6211
   131
wenzelm@6211
   132
(* access thy *)
wenzelm@6211
   133
wenzelm@7935
   134
fun lookup_thy name =
skalberg@15531
   135
  SOME (thy_graph Graph.get_node name) handle Graph.UNDEF _ => NONE;
wenzelm@7935
   136
skalberg@15570
   137
val known_thy = isSome o lookup_thy;
wenzelm@7935
   138
fun check_known_thy name = known_thy name orelse (warning ("Unknown theory " ^ quote name); false);
wenzelm@7935
   139
fun if_known_thy f name = if check_known_thy name then f name else ();
wenzelm@6211
   140
wenzelm@6211
   141
fun get_thy name =
wenzelm@6211
   142
  (case lookup_thy name of
skalberg@15531
   143
    SOME thy => thy
skalberg@15531
   144
  | NONE => error (loader_msg "nothing known about theory" [name]));
wenzelm@6211
   145
wenzelm@6211
   146
fun change_thy name f = (get_thy name; change_thys (Graph.map_node name f));
wenzelm@6211
   147
wenzelm@6211
   148
wenzelm@6211
   149
(* access deps *)
wenzelm@6211
   150
skalberg@15570
   151
val lookup_deps = Option.map #1 o lookup_thy;
wenzelm@6211
   152
val get_deps = #1 o get_thy;
wenzelm@6211
   153
fun change_deps name f = change_thy name (fn (deps, x) => (f deps, x));
wenzelm@6211
   154
wenzelm@6666
   155
fun is_finished name = is_none (get_deps name);
skalberg@15531
   156
fun get_files name = (case get_deps name of SOME {files, ...} => files | _ => []);
wenzelm@7191
   157
wenzelm@7191
   158
fun loaded_files name =
wenzelm@7191
   159
  (case get_deps name of
skalberg@15531
   160
    NONE => []
skalberg@15531
   161
  | SOME {master, files, ...} =>
skalberg@15531
   162
      (case master of SOME m => [#1 (ThyLoad.get_thy m)] | NONE => []) @
skalberg@15570
   163
      (List.mapPartial (Option.map #1 o #2) files));
wenzelm@6211
   164
berghofe@6654
   165
fun get_preds name =
wenzelm@9327
   166
  (thy_graph Graph.imm_preds name) handle Graph.UNDEF _ =>
berghofe@6654
   167
    error (loader_msg "nothing known about theory" [name]);
berghofe@6654
   168
wenzelm@6211
   169
paulson@15633
   170
(* pretty printing a theory: omit finished theories *)
paulson@15633
   171
paulson@15633
   172
fun unfinished_names stamps =
paulson@15633
   173
    List.last (List.filter is_finished stamps) :: List.filter (not o is_finished) stamps;
paulson@15633
   174
paulson@15633
   175
fun pretty_sg sg = 
paulson@15633
   176
      Pretty.str_list "{" "}" 
paulson@15633
   177
        (unfinished_names (List.filter known_thy (Sign.stamp_names_of sg)));
paulson@15633
   178
paulson@15633
   179
val pretty_theory = pretty_sg o Theory.sign_of;
paulson@15633
   180
val pprint_theory = Pretty.pprint o pretty_theory;
paulson@15633
   181
paulson@15633
   182
wenzelm@6211
   183
(* access theory *)
wenzelm@6211
   184
wenzelm@7687
   185
fun lookup_theory name =
wenzelm@7687
   186
  (case lookup_thy name of
skalberg@15531
   187
    SOME (_, SOME thy) => SOME thy
skalberg@15531
   188
  | _ => NONE);
wenzelm@7288
   189
wenzelm@6211
   190
fun get_theory name =
wenzelm@7288
   191
  (case lookup_theory name of
skalberg@15531
   192
    (SOME theory) => theory
wenzelm@6211
   193
  | _ => error (loader_msg "undefined theory entry for" [name]));
wenzelm@6211
   194
wenzelm@6211
   195
val theory_of_sign = get_theory o Sign.name_of;
wenzelm@6211
   196
val theory_of_thm = theory_of_sign o Thm.sign_of_thm;
wenzelm@6211
   197
skalberg@15531
   198
fun put_theory name theory = change_thy name (fn (deps, _) => (deps, SOME theory));
wenzelm@6211
   199
wenzelm@6211
   200
wenzelm@6211
   201
wenzelm@6211
   202
(** thy operations **)
wenzelm@6211
   203
wenzelm@6241
   204
(* maintain 'outdated' flag *)
wenzelm@6211
   205
wenzelm@7099
   206
local
wenzelm@7099
   207
wenzelm@6241
   208
fun is_outdated name =
wenzelm@6241
   209
  (case lookup_deps name of
skalberg@15531
   210
    SOME (SOME {outdated, ...}) => outdated
wenzelm@6241
   211
  | _ => false);
wenzelm@6211
   212
wenzelm@6241
   213
fun outdate_thy name =
wenzelm@7099
   214
  if is_finished name orelse is_outdated name then ()
skalberg@15570
   215
  else (change_deps name (Option.map (fn {present, outdated = _, master, files} =>
wenzelm@7099
   216
    make_deps present true master files)); perform Outdate name);
wenzelm@7099
   217
wenzelm@7910
   218
fun check_unfinished name =
skalberg@15531
   219
  if is_finished name then (warning (loader_msg "tried to touch finished theory" [name]); NONE)
skalberg@15531
   220
  else SOME name;
wenzelm@7910
   221
wenzelm@7099
   222
in
wenzelm@6211
   223
wenzelm@7910
   224
fun touch_thys names =
skalberg@15570
   225
  List.app outdate_thy (thy_graph Graph.all_succs (List.mapPartial check_unfinished names));
wenzelm@7910
   226
wenzelm@7910
   227
fun touch_thy name = touch_thys [name];
wenzelm@7910
   228
fun touch_child_thys name = touch_thys (thy_graph Graph.imm_succs name);
wenzelm@6211
   229
skalberg@15570
   230
fun touch_all_thys () = List.app outdate_thy (get_names ());
wenzelm@6211
   231
wenzelm@7099
   232
end;
wenzelm@6211
   233
wenzelm@6211
   234
wenzelm@7244
   235
(* check 'finished' state *)
wenzelm@7244
   236
wenzelm@7244
   237
fun check_unfinished fail name =
wenzelm@7910
   238
  if known_thy name andalso is_finished name then
wenzelm@7288
   239
    fail (loader_msg "cannot update finished theory" [name])
wenzelm@7244
   240
  else ();
wenzelm@7244
   241
wenzelm@7244
   242
wenzelm@7941
   243
(* load_file *)
wenzelm@6211
   244
skalberg@15570
   245
val opt_path = Option.map (Path.dir o fst o ThyLoad.get_thy);
skalberg@15570
   246
fun opt_path' (d : deps option) = getOpt (Option.map (opt_path o #master) d, NONE);
skalberg@15570
   247
fun opt_path'' d = getOpt (Option.map opt_path' d, NONE);
berghofe@15158
   248
wenzelm@7191
   249
local
wenzelm@7191
   250
skalberg@15531
   251
fun provide path name info (deps as SOME {present, outdated, master, files}) =
wenzelm@7941
   252
     (if exists (equal path o #1) files then ()
wenzelm@7941
   253
      else warning (loader_msg "undeclared dependency of theory" [name] ^
wenzelm@7941
   254
        " on file: " ^ quote (Path.pack path));
skalberg@15531
   255
      SOME (make_deps present outdated master (overwrite (files, (path, SOME info)))))
skalberg@15531
   256
  | provide _ _ _ NONE = NONE;
wenzelm@7941
   257
wenzelm@7941
   258
fun run_file path =
skalberg@15570
   259
  (case Option.map PureThy.get_name (Context.get_context ()) of
skalberg@15531
   260
    NONE => (ThyLoad.load_file NONE path; ())
skalberg@15531
   261
  | SOME name => (case lookup_deps name of
skalberg@15531
   262
      SOME deps => change_deps name (provide path name
berghofe@15158
   263
        (ThyLoad.load_file (opt_path' deps) path))
skalberg@15531
   264
    | NONE => (ThyLoad.load_file NONE path; ())));
wenzelm@6211
   265
wenzelm@7191
   266
in
wenzelm@7191
   267
wenzelm@7941
   268
fun load_file time path =
wenzelm@7941
   269
  if time then
wenzelm@7244
   270
    let val name = Path.pack path in
wenzelm@7244
   271
      timeit (fn () =>
wenzelm@9778
   272
       (priority ("\n**** Starting file " ^ quote name ^ " ****");
wenzelm@9036
   273
        run_file path;
wenzelm@9778
   274
        priority ("**** Finished file " ^ quote name ^ " ****\n")))
wenzelm@7244
   275
    end
wenzelm@7941
   276
  else run_file path;
wenzelm@7941
   277
wenzelm@7941
   278
val use = load_file false o Path.unpack;
wenzelm@7941
   279
val time_use = load_file true o Path.unpack;
wenzelm@7191
   280
wenzelm@7191
   281
end;
wenzelm@6233
   282
wenzelm@6233
   283
wenzelm@7099
   284
(* load_thy *)
wenzelm@7099
   285
wenzelm@9490
   286
fun required_by _ [] = ""
wenzelm@9490
   287
  | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
wenzelm@7099
   288
berghofe@15065
   289
fun load_thy really ml time initiators dir name =
wenzelm@7099
   290
  let
wenzelm@9822
   291
    val _ =
wenzelm@9822
   292
      if really then priority ("Loading theory " ^ quote name ^ required_by " " initiators)
wenzelm@9822
   293
      else priority ("Registering theory " ^ quote name);
wenzelm@7099
   294
wenzelm@7099
   295
    val _ = touch_thy name;
wenzelm@9822
   296
    val master =
wenzelm@9822
   297
      if really then ThyLoad.load_thy dir name ml time
wenzelm@9822
   298
      else #1 (ThyLoad.deps_thy dir name ml);
wenzelm@7099
   299
wenzelm@7099
   300
    val files = get_files name;
skalberg@15570
   301
    val missing_files = List.mapPartial (fn (path, NONE) => SOME (Path.pack path) | _ => NONE) files;
wenzelm@7099
   302
  in
wenzelm@7099
   303
    if null missing_files then ()
wenzelm@7099
   304
    else warning (loader_msg "unresolved dependencies of theory" [name] ^
wenzelm@7223
   305
      " on file(s): " ^ commas_quote missing_files);
skalberg@15531
   306
    change_deps name (fn _ => SOME (make_deps true false (SOME master) files));
wenzelm@7099
   307
    perform Update name
wenzelm@7099
   308
  end;
wenzelm@7099
   309
wenzelm@7099
   310
wenzelm@6211
   311
(* require_thy *)
wenzelm@6211
   312
berghofe@15065
   313
fun base_of_path s = Path.pack (Path.base (Path.unpack s));
berghofe@15065
   314
wenzelm@7211
   315
local
wenzelm@6211
   316
wenzelm@7941
   317
fun load_deps ml dir name =
wenzelm@7941
   318
  let val (master, (parents, files)) = ThyLoad.deps_thy dir name ml
skalberg@15531
   319
  in (SOME (init_deps (SOME master) files), parents) end;
wenzelm@6211
   320
skalberg@15531
   321
fun file_current master (_, NONE) = false
berghofe@15158
   322
  | file_current master (path, info) =
berghofe@15158
   323
      (info = ThyLoad.check_file (opt_path master) path);
wenzelm@6211
   324
wenzelm@7941
   325
fun current_deps ml strict dir name =
wenzelm@6211
   326
  (case lookup_deps name of
skalberg@15531
   327
    NONE => (false, load_deps ml dir name)
skalberg@15531
   328
  | SOME deps =>
berghofe@15158
   329
      let
skalberg@15531
   330
        fun get_name s = (case opt_path'' (lookup_deps s) of NONE => s
skalberg@15531
   331
          | SOME p => Path.pack (Path.append p (Path.basic s)));
skalberg@15531
   332
        val same_deps = (NONE, map get_name (thy_graph Graph.imm_preds name))
berghofe@15158
   333
      in
wenzelm@6211
   334
        (case deps of
skalberg@15531
   335
          NONE => (true, same_deps)
skalberg@15531
   336
        | SOME {present, outdated, master, files} =>
wenzelm@6211
   337
            if present andalso not strict then (true, same_deps)
wenzelm@6211
   338
            else
skalberg@15531
   339
              let val master' = SOME (ThyLoad.check_thy dir name ml) in
wenzelm@7941
   340
                if master <> master' then (false, load_deps ml dir name)
berghofe@15158
   341
                else (not outdated andalso forall (file_current master') files, same_deps)
wenzelm@6211
   342
              end)
wenzelm@6211
   343
      end);
wenzelm@6211
   344
wenzelm@9822
   345
fun require_thy really ml time strict keep_strict initiators prfx (visited, str) =
wenzelm@6211
   346
  let
wenzelm@7066
   347
    val path = Path.expand (Path.unpack str);
wenzelm@6484
   348
    val name = Path.pack (Path.base path);
wenzelm@7066
   349
  in
wenzelm@9332
   350
    if name mem_string initiators then error (cycle_msg initiators) else ();
wenzelm@7952
   351
    if known_thy name andalso is_finished name orelse name mem_string visited then
wenzelm@7952
   352
      (visited, (true, name))
wenzelm@7066
   353
    else
wenzelm@7066
   354
      let
wenzelm@7066
   355
        val dir = Path.append prfx (Path.dir path);
wenzelm@9822
   356
        val req_parent = require_thy true true time (strict andalso keep_strict) keep_strict
berghofe@15065
   357
          (name :: initiators);
wenzelm@6484
   358
wenzelm@7941
   359
        val (current, (new_deps, parents)) = current_deps ml strict dir name handle ERROR =>
wenzelm@7099
   360
          error (loader_msg "the error(s) above occurred while examining theory" [name] ^
wenzelm@9490
   361
            (if null initiators then "" else required_by "\n" initiators));
skalberg@15570
   362
        val dir' = getOpt (opt_path'' new_deps, dir);
berghofe@15065
   363
        val (visited', parent_results) = foldl_map (req_parent dir') (name :: visited, parents);
wenzelm@6211
   364
skalberg@15531
   365
        val thy = if not really then SOME (get_theory name) else NONE;
wenzelm@7066
   366
        val result =
wenzelm@7066
   367
          if current andalso forall #1 parent_results then true
wenzelm@7066
   368
          else
wenzelm@7066
   369
            ((case new_deps of
skalberg@15531
   370
              SOME deps => change_thys (update_node name (map base_of_path parents) (deps, thy))
skalberg@15531
   371
            | NONE => ());
berghofe@15065
   372
             load_thy really ml (time orelse ! Output.timing) initiators dir name;
wenzelm@7099
   373
             false);
wenzelm@7066
   374
      in (visited', (result, name)) end
wenzelm@7066
   375
  end;
wenzelm@6484
   376
berghofe@15158
   377
fun gen_use_thy' req prfx s =
berghofe@15158
   378
  let val (_, (_, name)) = req [] prfx ([], s)
wenzelm@7066
   379
  in Context.context (get_theory name) end;
wenzelm@6241
   380
berghofe@15158
   381
fun gen_use_thy req = gen_use_thy' req Path.current;
berghofe@15158
   382
wenzelm@7244
   383
fun warn_finished f name = (check_unfinished warning name; f name);
wenzelm@7244
   384
wenzelm@7211
   385
in
wenzelm@7211
   386
berghofe@15158
   387
val weak_use_thy         = gen_use_thy' (require_thy true true false false false);
berghofe@15158
   388
fun quiet_update_thy' ml = gen_use_thy' (require_thy true ml false true true);
berghofe@15158
   389
fun quiet_update_thy ml  = gen_use_thy (require_thy true ml false true true);
wenzelm@7244
   390
wenzelm@9822
   391
val use_thy          = warn_finished (gen_use_thy (require_thy true true false true false));
wenzelm@9822
   392
val time_use_thy     = warn_finished (gen_use_thy (require_thy true true true true false));
wenzelm@9822
   393
val use_thy_only     = warn_finished (gen_use_thy (require_thy true false false true false));
wenzelm@9822
   394
val update_thy       = warn_finished (gen_use_thy (require_thy true true false true true));
wenzelm@9822
   395
val update_thy_only  = warn_finished (gen_use_thy (require_thy true false false true true));
wenzelm@9822
   396
val pretend_use_thy_only = warn_finished (gen_use_thy (require_thy false false false true false));
wenzelm@6211
   397
wenzelm@7211
   398
end;
wenzelm@7211
   399
wenzelm@6211
   400
wenzelm@6666
   401
(* remove_thy *)
wenzelm@6666
   402
wenzelm@6666
   403
fun remove_thy name =
wenzelm@7910
   404
  if is_finished name then error (loader_msg "cannot remove finished theory" [name])
wenzelm@6666
   405
  else
wenzelm@6666
   406
    let val succs = thy_graph Graph.all_succs [name] in
wenzelm@9778
   407
      priority (loader_msg "removing" succs);
skalberg@15570
   408
      List.app (perform Remove) succs;
wenzelm@7191
   409
      change_thys (Graph.del_nodes succs)
wenzelm@6666
   410
    end;
wenzelm@6666
   411
wenzelm@6666
   412
wenzelm@7066
   413
(* begin / end theory *)
wenzelm@6211
   414
wenzelm@8805
   415
fun begin_theory present upd name parents paths =
wenzelm@6211
   416
  let
berghofe@15065
   417
    val bparents = map base_of_path parents;
berghofe@15158
   418
    val dir' = opt_path'' (lookup_deps name);
skalberg@15570
   419
    val dir = getOpt (dir',Path.current);
berghofe@15158
   420
    val assert_thy = if upd then quiet_update_thy' true dir else weak_use_thy dir;
wenzelm@7244
   421
    val _ = check_unfinished error name;
skalberg@15570
   422
    val _ = List.app assert_thy parents;
berghofe@15065
   423
    val theory = PureThy.begin_theory name (map get_theory bparents);
wenzelm@7952
   424
    val deps =
wenzelm@7952
   425
      if known_thy name then get_deps name
skalberg@15531
   426
      else (init_deps NONE (map #1 paths));   (*records additional ML files only!*)
skalberg@15570
   427
    val use_paths = List.mapPartial (fn (x, true) => SOME x | _ => NONE) paths;
wenzelm@9451
   428
skalberg@15531
   429
    val _ = change_thys (update_node name bparents (deps, SOME (Theory.copy theory)));
berghofe@15158
   430
    val theory' = theory |> present dir' name bparents paths;
wenzelm@9451
   431
    val _ = put_theory name (Theory.copy theory');
skalberg@15570
   432
    val ((), theory'') = Context.pass_theory theory' (List.app (load_file false)) use_paths;
wenzelm@9451
   433
    val _ = put_theory name (Theory.copy theory'');
wenzelm@8805
   434
  in theory'' end;
wenzelm@7244
   435
wenzelm@6211
   436
fun end_theory theory =
wenzelm@7099
   437
  let
wenzelm@7099
   438
    val theory' = PureThy.end_theory theory;
wenzelm@7099
   439
    val name = PureThy.get_name theory;
wenzelm@7099
   440
  in put_theory name theory'; theory' end;
wenzelm@6211
   441
wenzelm@6211
   442
wenzelm@6666
   443
(* finish all theories *)
wenzelm@6211
   444
skalberg@15531
   445
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (NONE, entry)));
wenzelm@6211
   446
wenzelm@6211
   447
wenzelm@6211
   448
(* register existing theories *)
wenzelm@6211
   449
wenzelm@6211
   450
fun register_theory theory =
wenzelm@6211
   451
  let
wenzelm@6211
   452
    val name = PureThy.get_name theory;
wenzelm@6211
   453
    val parents = Theory.parents_of theory;
wenzelm@6211
   454
    val parent_names = map PureThy.get_name parents;
wenzelm@6211
   455
wenzelm@6211
   456
    fun err txt bads =
wenzelm@6211
   457
      error (loader_msg txt bads ^ "\n" ^ gen_msg "cannot register theory" [name]);
wenzelm@6211
   458
wenzelm@6666
   459
    val nonfinished = filter_out is_finished parent_names;
wenzelm@6211
   460
    fun get_variant (x, y_name) =
skalberg@15531
   461
      if Theory.eq_thy (x, get_theory y_name) then NONE
skalberg@15531
   462
      else SOME y_name;
skalberg@15570
   463
    val variants = List.mapPartial get_variant (parents ~~ parent_names);
wenzelm@6211
   464
wenzelm@6211
   465
    fun register G =
skalberg@15531
   466
      (Graph.new_node (name, (NONE, SOME theory)) G
wenzelm@9327
   467
        handle Graph.DUP _ => err "duplicate theory entry" [])
wenzelm@6211
   468
      |> add_deps name parent_names;
wenzelm@6211
   469
  in
wenzelm@6666
   470
    if not (null nonfinished) then err "non-finished parent theories" nonfinished
wenzelm@6211
   471
    else if not (null variants) then err "different versions of parent theories" variants
wenzelm@7099
   472
    else (change_thys register; perform Update name)
wenzelm@6211
   473
  end;
wenzelm@6211
   474
wenzelm@6211
   475
wenzelm@6211
   476
(*final declarations of this structure*)
wenzelm@6211
   477
val theory = get_theory;
wenzelm@6211
   478
val names = get_names;
wenzelm@6211
   479
wenzelm@6211
   480
end;
wenzelm@6211
   481
wenzelm@5209
   482
structure BasicThyInfo: BASIC_THY_INFO = ThyInfo;
wenzelm@5209
   483
open BasicThyInfo;