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