src/Pure/Thy/thy_info.ML
author wenzelm
Thu Apr 27 15:06:35 2006 +0200 (2006-04-27)
changeset 19482 9f11af8f7ef9
parent 19473 d87a8838afa4
child 19547 17f504343d0f
permissions -rw-r--r--
tuned basic list operators (flat, maps, map_filter);
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
berghofe@6654
    35
  val get_preds: string -> string list
wenzelm@7935
    36
  val loaded_files: string -> Path.T list
wenzelm@7910
    37
  val touch_child_thys: string -> unit
wenzelm@7099
    38
  val touch_all_thys: unit -> unit
wenzelm@7941
    39
  val load_file: bool -> Path.T -> unit
wenzelm@6241
    40
  val use: string -> unit
wenzelm@7952
    41
  val quiet_update_thy: bool -> string -> unit
wenzelm@9822
    42
  val pretend_use_thy_only: string -> unit
berghofe@15158
    43
  val begin_theory: (Path.T option -> string -> string list ->
berghofe@15158
    44
      (Path.T * bool) list -> theory -> theory) ->
wenzelm@17365
    45
    string -> string list -> (Path.T * bool) list -> bool -> theory
wenzelm@6211
    46
  val end_theory: theory -> theory
wenzelm@6666
    47
  val finish: unit -> unit
wenzelm@6211
    48
  val register_theory: theory -> unit
paulson@15633
    49
  val pretty_theory: theory -> Pretty.T
berghofe@3604
    50
end;
berghofe@3604
    51
wenzelm@6362
    52
structure ThyInfo: THY_INFO =
wenzelm@6211
    53
struct
berghofe@3604
    54
berghofe@3604
    55
wenzelm@7099
    56
(** theory loader actions and hooks **)
wenzelm@7099
    57
wenzelm@7099
    58
datatype action = Update | Outdate | Remove;
wenzelm@7099
    59
val str_of_action = fn Update => "Update" | Outdate => "Outdate" | Remove => "Remove";
wenzelm@7099
    60
wenzelm@7099
    61
local
wenzelm@7099
    62
  val hooks = ref ([]: (action -> string -> unit) list);
wenzelm@7099
    63
in
wenzelm@7099
    64
  fun add_hook f = hooks := f :: ! hooks;
skalberg@15570
    65
  fun perform action name = List.app (fn f => (try (fn () => f action name) (); ())) (! hooks);
wenzelm@7099
    66
end;
wenzelm@7099
    67
wenzelm@7099
    68
wenzelm@7099
    69
wenzelm@6211
    70
(** thy database **)
wenzelm@6211
    71
wenzelm@6211
    72
(* messages *)
wenzelm@6211
    73
wenzelm@6211
    74
fun gen_msg txt [] = txt
wenzelm@6211
    75
  | gen_msg txt names = txt ^ " " ^ commas_quote names;
wenzelm@6211
    76
wenzelm@6211
    77
fun loader_msg txt names = gen_msg ("Theory loader: " ^ txt) names;
wenzelm@6211
    78
wenzelm@6211
    79
val show_path = space_implode " via " o map quote;
wenzelm@9332
    80
fun cycle_msg names = loader_msg ("cyclic dependency of " ^ show_path names) [];
berghofe@3604
    81
berghofe@3604
    82
wenzelm@6666
    83
(* derived graph operations *)
berghofe@3604
    84
wenzelm@9327
    85
fun add_deps name parents G = Graph.add_deps_acyclic (name, parents) G
wenzelm@9332
    86
  handle Graph.CYCLES namess => error (cat_lines (map cycle_msg namess));
berghofe@3604
    87
wenzelm@7952
    88
fun upd_deps name entry G =
wenzelm@19473
    89
  fold (fn parent => Graph.del_edge (parent, name)) (Graph.imm_preds G name) G
wenzelm@7952
    90
  |> Graph.map_node name (K entry);
wenzelm@3976
    91
wenzelm@6211
    92
fun update_node name parents entry G =
wenzelm@7952
    93
  (if can (Graph.get_node G) name then upd_deps name entry G else Graph.new_node (name, entry) G)
wenzelm@7952
    94
  |> add_deps name parents;
berghofe@3604
    95
berghofe@3604
    96
wenzelm@6211
    97
(* thy database *)
wenzelm@6211
    98
wenzelm@6211
    99
type deps =
wenzelm@6211
   100
  {present: bool, outdated: bool,
wenzelm@7941
   101
    master: ThyLoad.master option, files: (Path.T * (Path.T * File.info) option) list};
wenzelm@6211
   102
wenzelm@6241
   103
fun make_deps present outdated master files =
wenzelm@6241
   104
  {present = present, outdated = outdated, master = master, files = files}: deps;
berghofe@3604
   105
skalberg@15531
   106
fun init_deps master files = SOME (make_deps false true master (map (rpair NONE) files));
wenzelm@7211
   107
wenzelm@7211
   108
wenzelm@6362
   109
type thy = deps option * theory option;
berghofe@3604
   110
wenzelm@6211
   111
local
wenzelm@6362
   112
  val database = ref (Graph.empty: thy Graph.T);
wenzelm@6211
   113
in
wenzelm@6362
   114
  fun get_thys () = ! database;
wenzelm@9137
   115
  val change_thys = Library.change database;
berghofe@3604
   116
end;
wenzelm@5209
   117
wenzelm@5209
   118
wenzelm@6211
   119
(* access thy graph *)
wenzelm@6211
   120
wenzelm@6211
   121
fun thy_graph f x = f (get_thys ()) x;
wenzelm@9417
   122
wenzelm@9417
   123
(*theory names in topological order*)
wenzelm@9417
   124
fun get_names () =
wenzelm@9417
   125
  let val G = get_thys ()
wenzelm@14774
   126
  in Graph.all_succs G (Graph.minimals G) end;
wenzelm@6211
   127
wenzelm@6211
   128
wenzelm@6211
   129
(* access thy *)
wenzelm@6211
   130
wenzelm@7935
   131
fun lookup_thy name =
skalberg@15531
   132
  SOME (thy_graph Graph.get_node name) handle Graph.UNDEF _ => NONE;
wenzelm@7935
   133
wenzelm@16047
   134
val known_thy = is_some o lookup_thy;
wenzelm@7935
   135
fun check_known_thy name = known_thy name orelse (warning ("Unknown theory " ^ quote name); false);
wenzelm@7935
   136
fun if_known_thy f name = if check_known_thy name then f name else ();
wenzelm@6211
   137
wenzelm@6211
   138
fun get_thy name =
wenzelm@6211
   139
  (case lookup_thy name of
skalberg@15531
   140
    SOME thy => thy
skalberg@15531
   141
  | NONE => error (loader_msg "nothing known about theory" [name]));
wenzelm@6211
   142
wenzelm@6211
   143
fun change_thy name f = (get_thy name; change_thys (Graph.map_node name f));
wenzelm@6211
   144
wenzelm@6211
   145
wenzelm@6211
   146
(* access deps *)
wenzelm@6211
   147
skalberg@15570
   148
val lookup_deps = Option.map #1 o lookup_thy;
wenzelm@6211
   149
val get_deps = #1 o get_thy;
wenzelm@6211
   150
fun change_deps name f = change_thy name (fn (deps, x) => (f deps, x));
wenzelm@6211
   151
wenzelm@6666
   152
fun is_finished name = is_none (get_deps name);
skalberg@15531
   153
fun get_files name = (case get_deps name of SOME {files, ...} => files | _ => []);
wenzelm@7191
   154
wenzelm@7191
   155
fun loaded_files name =
wenzelm@7191
   156
  (case get_deps name of
skalberg@15531
   157
    NONE => []
skalberg@15531
   158
  | SOME {master, files, ...} =>
skalberg@15531
   159
      (case master of SOME m => [#1 (ThyLoad.get_thy m)] | NONE => []) @
wenzelm@19482
   160
      (map_filter (Option.map #1 o #2) files));
wenzelm@6211
   161
berghofe@6654
   162
fun get_preds name =
wenzelm@9327
   163
  (thy_graph Graph.imm_preds name) handle Graph.UNDEF _ =>
berghofe@6654
   164
    error (loader_msg "nothing known about theory" [name]);
berghofe@6654
   165
wenzelm@6211
   166
wenzelm@16047
   167
(* pretty printing a theory *)
wenzelm@16047
   168
wenzelm@16047
   169
local
paulson@15633
   170
wenzelm@16047
   171
fun relevant_names names =
wenzelm@16047
   172
  let
wenzelm@16047
   173
    val (finished, unfinished) =
wenzelm@19305
   174
      List.filter (fn name => name = Context.draftN orelse known_thy name) names
wenzelm@19305
   175
      |> List.partition (fn name => name <> Context.draftN andalso is_finished name);
wenzelm@16047
   176
  in (if not (null finished) then [List.last finished] else []) @ unfinished end;
paulson@15633
   177
wenzelm@16047
   178
in
paulson@15633
   179
wenzelm@16454
   180
fun pretty_theory thy =
wenzelm@16454
   181
  Pretty.str_list "{" "}" (relevant_names (Context.names_of thy));
paulson@15633
   182
wenzelm@16047
   183
end;
wenzelm@16047
   184
paulson@15633
   185
wenzelm@6211
   186
(* access theory *)
wenzelm@6211
   187
wenzelm@7687
   188
fun lookup_theory name =
wenzelm@7687
   189
  (case lookup_thy name of
skalberg@15531
   190
    SOME (_, SOME thy) => SOME thy
skalberg@15531
   191
  | _ => NONE);
wenzelm@7288
   192
wenzelm@6211
   193
fun get_theory name =
wenzelm@7288
   194
  (case lookup_theory name of
skalberg@15531
   195
    (SOME theory) => theory
wenzelm@6211
   196
  | _ => error (loader_msg "undefined theory entry for" [name]));
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 =
wenzelm@19482
   225
  List.app outdate_thy (thy_graph Graph.all_succs (map_filter 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);
wenzelm@19473
   246
fun opt_path' (d: deps option) = the_default NONE (Option.map (opt_path o #master) d);
wenzelm@19473
   247
fun opt_path'' d = the_default NONE (Option.map opt_path' d);
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));
haftmann@17192
   255
      SOME (make_deps present outdated master (AList.update (op =) (path, SOME info) files)))
skalberg@15531
   256
  | provide _ _ _ NONE = NONE;
wenzelm@7941
   257
wenzelm@7941
   258
fun run_file path =
wenzelm@16454
   259
  (case Option.map Context.theory_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@18721
   268
fun load_file time path = Output.toplevel_errors (fn () =>
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@18721
   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;
wenzelm@19482
   301
    val missing_files = map_filter (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@18678
   359
        val (current, (new_deps, parents)) = current_deps ml strict dir name
wenzelm@18678
   360
          handle ERROR msg => cat_error msg
wenzelm@18678
   361
            (loader_msg "the error(s) above occurred while examining theory" [name] ^
wenzelm@18678
   362
              (if null initiators then "" else required_by "\n" initiators));
wenzelm@19473
   363
        val dir' = the_default dir (opt_path'' new_deps);
berghofe@15065
   364
        val (visited', parent_results) = foldl_map (req_parent dir') (name :: visited, parents);
wenzelm@6211
   365
skalberg@15531
   366
        val thy = if not really then SOME (get_theory name) else NONE;
wenzelm@7066
   367
        val result =
wenzelm@17756
   368
          if current andalso forall fst parent_results then true
wenzelm@7066
   369
          else
wenzelm@7066
   370
            ((case new_deps of
skalberg@15531
   371
              SOME deps => change_thys (update_node name (map base_of_path parents) (deps, thy))
skalberg@15531
   372
            | NONE => ());
berghofe@15065
   373
             load_thy really ml (time orelse ! Output.timing) initiators dir name;
wenzelm@7099
   374
             false);
wenzelm@7066
   375
      in (visited', (result, name)) end
wenzelm@7066
   376
  end;
wenzelm@6484
   377
wenzelm@18721
   378
fun gen_use_thy' req prfx s = Output.toplevel_errors (fn () =>
berghofe@15158
   379
  let val (_, (_, name)) = req [] prfx ([], s)
wenzelm@18721
   380
  in Context.context (get_theory name) end) ();
wenzelm@6241
   381
berghofe@15158
   382
fun gen_use_thy req = gen_use_thy' req Path.current;
berghofe@15158
   383
wenzelm@7244
   384
fun warn_finished f name = (check_unfinished warning name; f name);
wenzelm@7244
   385
wenzelm@7211
   386
in
wenzelm@7211
   387
berghofe@15158
   388
val weak_use_thy         = gen_use_thy' (require_thy true true false false false);
berghofe@15158
   389
fun quiet_update_thy' ml = gen_use_thy' (require_thy true ml false true true);
berghofe@15158
   390
fun quiet_update_thy ml  = gen_use_thy (require_thy true ml false true true);
wenzelm@7244
   391
wenzelm@9822
   392
val use_thy          = warn_finished (gen_use_thy (require_thy true true false true false));
wenzelm@9822
   393
val time_use_thy     = warn_finished (gen_use_thy (require_thy true true true true false));
wenzelm@9822
   394
val use_thy_only     = warn_finished (gen_use_thy (require_thy true false false true false));
wenzelm@9822
   395
val update_thy       = warn_finished (gen_use_thy (require_thy true true false true true));
wenzelm@9822
   396
val update_thy_only  = warn_finished (gen_use_thy (require_thy true false false true true));
wenzelm@9822
   397
val pretend_use_thy_only = warn_finished (gen_use_thy (require_thy false false false true false));
wenzelm@6211
   398
wenzelm@7211
   399
end;
wenzelm@7211
   400
wenzelm@6211
   401
wenzelm@6666
   402
(* remove_thy *)
wenzelm@6666
   403
wenzelm@6666
   404
fun remove_thy name =
wenzelm@7910
   405
  if is_finished name then error (loader_msg "cannot remove finished theory" [name])
wenzelm@6666
   406
  else
wenzelm@6666
   407
    let val succs = thy_graph Graph.all_succs [name] in
wenzelm@9778
   408
      priority (loader_msg "removing" succs);
skalberg@15570
   409
      List.app (perform Remove) succs;
wenzelm@7191
   410
      change_thys (Graph.del_nodes succs)
wenzelm@6666
   411
    end;
wenzelm@6666
   412
wenzelm@6666
   413
wenzelm@7066
   414
(* begin / end theory *)
wenzelm@6211
   415
wenzelm@17365
   416
fun check_uses name uses =
wenzelm@17365
   417
  let val illegal = map (fn ext => Path.ext ext (Path.basic name)) ThyLoad.ml_exts in
wenzelm@17365
   418
    (case find_first (member (op =) illegal o Path.base o Path.expand o #1) uses of
wenzelm@17365
   419
      NONE => ()
wenzelm@17365
   420
    | SOME (path, _) => error ("Illegal use of theory ML file: " ^ quote (Path.pack path)))
wenzelm@17365
   421
  end;
wenzelm@17365
   422
wenzelm@17365
   423
fun begin_theory present name parents uses int =
wenzelm@6211
   424
  let
berghofe@15065
   425
    val bparents = map base_of_path parents;
berghofe@15158
   426
    val dir' = opt_path'' (lookup_deps name);
wenzelm@19473
   427
    val dir = the_default Path.current dir';
wenzelm@17365
   428
    val assert_thy = if int then quiet_update_thy' true dir else weak_use_thy dir;
wenzelm@7244
   429
    val _ = check_unfinished error name;
skalberg@15570
   430
    val _ = List.app assert_thy parents;
wenzelm@17365
   431
    val _ = check_uses name uses;
wenzelm@17365
   432
wenzelm@16504
   433
    val theory = Theory.begin_theory name (map get_theory bparents);
wenzelm@7952
   434
    val deps =
wenzelm@7952
   435
      if known_thy name then get_deps name
wenzelm@17365
   436
      else (init_deps NONE (map #1 uses));   (*records additional ML files only!*)
wenzelm@19482
   437
    val uses_now = map_filter (fn (x, true) => SOME x | _ => NONE) uses;
wenzelm@9451
   438
skalberg@15531
   439
    val _ = change_thys (update_node name bparents (deps, SOME (Theory.copy theory)));
wenzelm@17365
   440
    val theory' = theory |> present dir' name bparents uses;
wenzelm@9451
   441
    val _ = put_theory name (Theory.copy theory');
wenzelm@17365
   442
    val ((), theory'') = Context.pass_theory theory' (List.app (load_file false)) uses_now;
wenzelm@9451
   443
    val _ = put_theory name (Theory.copy theory'');
wenzelm@8805
   444
  in theory'' end;
wenzelm@7244
   445
wenzelm@6211
   446
fun end_theory theory =
wenzelm@7099
   447
  let
wenzelm@16454
   448
    val name = Context.theory_name theory;
wenzelm@16504
   449
    val theory' = Theory.end_theory theory;
wenzelm@7099
   450
  in put_theory name theory'; theory' end;
wenzelm@6211
   451
wenzelm@6211
   452
wenzelm@6666
   453
(* finish all theories *)
wenzelm@6211
   454
skalberg@15531
   455
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (NONE, entry)));
wenzelm@6211
   456
wenzelm@6211
   457
wenzelm@6211
   458
(* register existing theories *)
wenzelm@6211
   459
wenzelm@6211
   460
fun register_theory theory =
wenzelm@6211
   461
  let
wenzelm@16454
   462
    val name = Context.theory_name theory;
wenzelm@6211
   463
    val parents = Theory.parents_of theory;
wenzelm@16454
   464
    val parent_names = map Context.theory_name parents;
wenzelm@6211
   465
wenzelm@6211
   466
    fun err txt bads =
wenzelm@6211
   467
      error (loader_msg txt bads ^ "\n" ^ gen_msg "cannot register theory" [name]);
wenzelm@6211
   468
wenzelm@6666
   469
    val nonfinished = filter_out is_finished parent_names;
wenzelm@6211
   470
    fun get_variant (x, y_name) =
skalberg@15531
   471
      if Theory.eq_thy (x, get_theory y_name) then NONE
skalberg@15531
   472
      else SOME y_name;
wenzelm@19482
   473
    val variants = map_filter get_variant (parents ~~ parent_names);
wenzelm@6211
   474
wenzelm@6211
   475
    fun register G =
skalberg@15531
   476
      (Graph.new_node (name, (NONE, SOME theory)) G
wenzelm@9327
   477
        handle Graph.DUP _ => err "duplicate theory entry" [])
wenzelm@6211
   478
      |> add_deps name parent_names;
wenzelm@6211
   479
  in
wenzelm@6666
   480
    if not (null nonfinished) then err "non-finished parent theories" nonfinished
wenzelm@6211
   481
    else if not (null variants) then err "different versions of parent theories" variants
wenzelm@7099
   482
    else (change_thys register; perform Update name)
wenzelm@6211
   483
  end;
wenzelm@6211
   484
wenzelm@15801
   485
val _ = register_theory ProtoPure.thy;
wenzelm@15801
   486
wenzelm@6211
   487
wenzelm@6211
   488
(*final declarations of this structure*)
wenzelm@6211
   489
val theory = get_theory;
wenzelm@6211
   490
val names = get_names;
wenzelm@6211
   491
wenzelm@6211
   492
end;
wenzelm@6211
   493
wenzelm@5209
   494
structure BasicThyInfo: BASIC_THY_INFO = ThyInfo;
wenzelm@5209
   495
open BasicThyInfo;