src/Pure/Thy/thy_info.ML
author wenzelm
Tue Jan 05 00:04:29 2010 +0100 (2010-01-05)
changeset 34260 2524c1bbd087
parent 33522 737589bb9bb8
child 36953 2af1ad9aa1a3
permissions -rw-r--r--
kill failed theories in reverse order -- avoids cascaded warnings;
berghofe@3604
     1
(*  Title:      Pure/Thy/thy_info.ML
wenzelm@6211
     2
    Author:     Markus Wenzel, TU Muenchen
berghofe@3604
     3
wenzelm@15801
     4
Main part of theory loader database, including handling of theory and
wenzelm@15801
     5
file dependencies.
wenzelm@3976
     6
*)
berghofe@3604
     7
berghofe@3604
     8
signature THY_INFO =
berghofe@3604
     9
sig
wenzelm@7099
    10
  datatype action = Update | Outdate | Remove
wenzelm@7099
    11
  val str_of_action: action -> string
wenzelm@7099
    12
  val add_hook: (action -> string -> unit) -> unit
wenzelm@26614
    13
  val get_names: unit -> string list
wenzelm@7910
    14
  val known_thy: string -> bool
wenzelm@7935
    15
  val check_known_thy: string -> bool
wenzelm@7935
    16
  val if_known_thy: (string -> unit) -> string -> unit
wenzelm@7288
    17
  val lookup_theory: string -> theory option
wenzelm@6211
    18
  val get_theory: string -> theory
wenzelm@19547
    19
  val the_theory: string -> theory -> theory
wenzelm@24563
    20
  val is_finished: string -> bool
wenzelm@26983
    21
  val master_directory: string -> Path.T
wenzelm@24080
    22
  val loaded_files: string -> Path.T list
wenzelm@23871
    23
  val get_parents: string -> string list
wenzelm@27495
    24
  val touch_thy: string -> unit
wenzelm@7910
    25
  val touch_child_thys: string -> unit
wenzelm@32106
    26
  val thy_ord: theory * theory -> order
wenzelm@29421
    27
  val remove_thy: string -> unit
wenzelm@29421
    28
  val kill_thy: string -> unit
wenzelm@25013
    29
  val provide_file: Path.T -> string -> unit
wenzelm@7941
    30
  val load_file: bool -> Path.T -> unit
wenzelm@26494
    31
  val exec_file: bool -> Path.T -> Context.generic -> Context.generic
wenzelm@6241
    32
  val use: string -> unit
wenzelm@24057
    33
  val time_use: string -> unit
wenzelm@24057
    34
  val use_thys: string list -> unit
wenzelm@24057
    35
  val use_thy: string -> unit
wenzelm@24057
    36
  val time_use_thy: string -> unit
wenzelm@23900
    37
  val begin_theory: string -> string list -> (Path.T * bool) list -> bool -> theory
wenzelm@27579
    38
  val end_theory: theory -> unit
wenzelm@24080
    39
  val register_thy: string -> unit
wenzelm@6211
    40
  val register_theory: theory -> unit
wenzelm@24080
    41
  val finish: unit -> unit
berghofe@3604
    42
end;
berghofe@3604
    43
wenzelm@6362
    44
structure ThyInfo: THY_INFO =
wenzelm@6211
    45
struct
berghofe@3604
    46
wenzelm@7099
    47
(** theory loader actions and hooks **)
wenzelm@7099
    48
wenzelm@7099
    49
datatype action = Update | Outdate | Remove;
wenzelm@7099
    50
val str_of_action = fn Update => "Update" | Outdate => "Outdate" | Remove => "Remove";
wenzelm@7099
    51
wenzelm@7099
    52
local
wenzelm@32738
    53
  val hooks = Unsynchronized.ref ([]: (action -> string -> unit) list);
wenzelm@7099
    54
in
wenzelm@32738
    55
  fun add_hook f = CRITICAL (fn () => Unsynchronized.change hooks (cons f));
skalberg@15570
    56
  fun perform action name = List.app (fn f => (try (fn () => f action name) (); ())) (! hooks);
wenzelm@7099
    57
end;
wenzelm@7099
    58
wenzelm@7099
    59
wenzelm@7099
    60
wenzelm@6211
    61
(** thy database **)
wenzelm@6211
    62
wenzelm@6211
    63
(* messages *)
wenzelm@6211
    64
wenzelm@23871
    65
fun loader_msg txt [] = "Theory loader: " ^ txt
wenzelm@23871
    66
  | loader_msg txt names = "Theory loader: " ^ txt ^ " " ^ commas_quote names;
wenzelm@6211
    67
wenzelm@6211
    68
val show_path = space_implode " via " o map quote;
wenzelm@9332
    69
fun cycle_msg names = loader_msg ("cyclic dependency of " ^ show_path names) [];
berghofe@3604
    70
berghofe@3604
    71
wenzelm@6666
    72
(* derived graph operations *)
berghofe@3604
    73
wenzelm@9327
    74
fun add_deps name parents G = Graph.add_deps_acyclic (name, parents) G
wenzelm@9332
    75
  handle Graph.CYCLES namess => error (cat_lines (map cycle_msg namess));
berghofe@3604
    76
wenzelm@7952
    77
fun upd_deps name entry G =
wenzelm@19473
    78
  fold (fn parent => Graph.del_edge (parent, name)) (Graph.imm_preds G name) G
wenzelm@7952
    79
  |> Graph.map_node name (K entry);
wenzelm@3976
    80
wenzelm@23967
    81
fun new_deps name parents entry G =
wenzelm@7952
    82
  (if can (Graph.get_node G) name then upd_deps name entry G else Graph.new_node (name, entry) G)
wenzelm@7952
    83
  |> add_deps name parents;
berghofe@3604
    84
berghofe@3604
    85
wenzelm@6211
    86
(* thy database *)
wenzelm@6211
    87
wenzelm@6211
    88
type deps =
wenzelm@24190
    89
  {update_time: int,                      (*symbolic time of update; negative value means outdated*)
wenzelm@24190
    90
    master: (Path.T * File.ident) option, (*master dependencies for thy file*)
wenzelm@24190
    91
    text: string list,                    (*source text for thy*)
wenzelm@24190
    92
    parents: string list,                 (*source specification of parents (partially qualified)*)
wenzelm@24190
    93
      (*auxiliary files: source path, physical path + identifier*)
wenzelm@24190
    94
    files: (Path.T * (Path.T * File.ident) option) list};
wenzelm@23886
    95
wenzelm@24151
    96
fun make_deps update_time master text parents files : deps =
wenzelm@24151
    97
  {update_time = update_time, master = master, text = text, parents = parents, files = files};
wenzelm@23886
    98
wenzelm@24068
    99
fun init_deps master text parents files =
wenzelm@24151
   100
  SOME (make_deps ~1 master text parents (map (rpair NONE) files));
wenzelm@23871
   101
wenzelm@24190
   102
fun master_dir NONE = Path.current
wenzelm@24190
   103
  | master_dir (SOME (path, _)) = Path.dir path;
wenzelm@6211
   104
wenzelm@23886
   105
fun master_dir' (d: deps option) = the_default Path.current (Option.map (master_dir o #master) d);
wenzelm@23886
   106
fun master_dir'' d = the_default Path.current (Option.map master_dir' d);
berghofe@3604
   107
wenzelm@23967
   108
fun base_name s = Path.implode (Path.base (Path.explode s));
wenzelm@23967
   109
wenzelm@7211
   110
wenzelm@6362
   111
type thy = deps option * theory option;
berghofe@3604
   112
wenzelm@6211
   113
local
wenzelm@32738
   114
  val database = Unsynchronized.ref (Graph.empty: thy Graph.T);
wenzelm@6211
   115
in
wenzelm@6362
   116
  fun get_thys () = ! database;
wenzelm@32738
   117
  fun change_thys f = CRITICAL (fn () => Unsynchronized.change database f);
berghofe@3604
   118
end;
wenzelm@5209
   119
wenzelm@5209
   120
wenzelm@6211
   121
(* access thy graph *)
wenzelm@6211
   122
wenzelm@6211
   123
fun thy_graph f x = f (get_thys ()) x;
wenzelm@9417
   124
wenzelm@23967
   125
fun get_names () = Graph.topological_order (get_thys ());
wenzelm@6211
   126
wenzelm@6211
   127
wenzelm@6211
   128
(* access thy *)
wenzelm@6211
   129
wenzelm@7935
   130
fun lookup_thy name =
skalberg@15531
   131
  SOME (thy_graph Graph.get_node name) handle Graph.UNDEF _ => NONE;
wenzelm@7935
   132
wenzelm@16047
   133
val known_thy = is_some o lookup_thy;
wenzelm@7935
   134
fun check_known_thy name = known_thy name orelse (warning ("Unknown theory " ^ quote name); false);
wenzelm@7935
   135
fun if_known_thy f name = if check_known_thy name then f name else ();
wenzelm@6211
   136
wenzelm@6211
   137
fun get_thy name =
wenzelm@6211
   138
  (case lookup_thy name of
skalberg@15531
   139
    SOME thy => thy
skalberg@15531
   140
  | NONE => error (loader_msg "nothing known about theory" [name]));
wenzelm@6211
   141
wenzelm@23939
   142
fun change_thy name f = CRITICAL (fn () =>
wenzelm@23939
   143
  (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@26983
   152
val is_finished = is_none o get_deps;
wenzelm@26983
   153
val master_directory = master_dir' o get_deps;
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, ...} =>
wenzelm@24190
   159
      (case master of SOME (thy_path, _) => [thy_path] | NONE => []) @
wenzelm@19482
   160
      (map_filter (Option.map #1 o #2) files));
wenzelm@6211
   161
wenzelm@23871
   162
fun get_parents name =
wenzelm@23967
   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@6211
   167
(* access theory *)
wenzelm@6211
   168
wenzelm@7687
   169
fun lookup_theory name =
wenzelm@7687
   170
  (case lookup_thy name of
skalberg@15531
   171
    SOME (_, SOME thy) => SOME thy
skalberg@15531
   172
  | _ => NONE);
wenzelm@7288
   173
wenzelm@6211
   174
fun get_theory name =
wenzelm@7288
   175
  (case lookup_theory name of
wenzelm@23871
   176
    SOME theory => theory
wenzelm@6211
   177
  | _ => error (loader_msg "undefined theory entry for" [name]));
wenzelm@6211
   178
wenzelm@19547
   179
fun the_theory name thy =
wenzelm@19547
   180
  if Context.theory_name thy = name then thy
wenzelm@19547
   181
  else get_theory name;
wenzelm@19547
   182
wenzelm@6211
   183
wenzelm@6211
   184
wenzelm@6211
   185
(** thy operations **)
wenzelm@6211
   186
wenzelm@24080
   187
(* check state *)
wenzelm@24080
   188
wenzelm@24080
   189
fun check_unfinished fail name =
wenzelm@24080
   190
  if known_thy name andalso is_finished name then
wenzelm@24080
   191
    fail (loader_msg "cannot update finished theory" [name])
wenzelm@24080
   192
  else ();
wenzelm@24080
   193
wenzelm@24080
   194
fun check_files name =
wenzelm@24080
   195
  let
wenzelm@24080
   196
    val files = (case get_deps name of SOME {files, ...} => files | NONE => []);
wenzelm@24080
   197
    val missing_files = map_filter (fn (path, NONE) => SOME (Path.implode path) | _ => NONE) files;
wenzelm@24190
   198
    val _ = null missing_files orelse
wenzelm@24190
   199
      error (loader_msg "unresolved dependencies of theory" [name] ^
wenzelm@24080
   200
        " on file(s): " ^ commas_quote missing_files);
wenzelm@24190
   201
  in () end;
wenzelm@24080
   202
wenzelm@24080
   203
wenzelm@24151
   204
(* maintain update_time *)
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
wenzelm@24151
   210
    SOME (SOME {update_time, ...}) => update_time < 0
wenzelm@6241
   211
  | _ => false);
wenzelm@6211
   212
wenzelm@25013
   213
fun unfinished name =
wenzelm@24186
   214
  if is_finished name then (warning (loader_msg "tried to touch finished theory" [name]); NONE)
wenzelm@24186
   215
  else SOME name;
wenzelm@24186
   216
wenzelm@24186
   217
in
wenzelm@24186
   218
wenzelm@6241
   219
fun outdate_thy name =
wenzelm@7099
   220
  if is_finished name orelse is_outdated name then ()
wenzelm@23939
   221
  else CRITICAL (fn () =>
wenzelm@24151
   222
   (change_deps name (Option.map (fn {master, text, parents, files, ...} =>
wenzelm@24151
   223
    make_deps ~1 master text parents files)); perform Outdate name));
wenzelm@7099
   224
wenzelm@7910
   225
fun touch_thys names =
wenzelm@25013
   226
  List.app outdate_thy (thy_graph Graph.all_succs (map_filter unfinished names));
wenzelm@7910
   227
wenzelm@7910
   228
fun touch_thy name = touch_thys [name];
wenzelm@7910
   229
fun touch_child_thys name = touch_thys (thy_graph Graph.imm_succs name);
wenzelm@6211
   230
wenzelm@7099
   231
end;
wenzelm@6211
   232
wenzelm@6211
   233
wenzelm@32106
   234
(* management data *)
wenzelm@32106
   235
wenzelm@33522
   236
structure Management_Data = Theory_Data
wenzelm@32106
   237
(
wenzelm@32106
   238
  type T =
wenzelm@32106
   239
    Task_Queue.group option *   (*worker thread group*)
wenzelm@32106
   240
    int;                        (*abstract update time*)
wenzelm@32106
   241
  val empty = (NONE, 0);
wenzelm@32106
   242
  fun extend _ = empty;
wenzelm@33522
   243
  fun merge _ = empty;
wenzelm@32106
   244
);
wenzelm@32106
   245
wenzelm@32106
   246
val thy_ord = int_ord o pairself (#2 o Management_Data.get);
wenzelm@32106
   247
wenzelm@32106
   248
wenzelm@32106
   249
(* pending proofs *)
wenzelm@29434
   250
wenzelm@29434
   251
fun join_thy name =
wenzelm@29434
   252
  (case lookup_theory name of
wenzelm@32106
   253
    NONE => ()
wenzelm@29434
   254
  | SOME thy => PureThy.join_proofs thy);
wenzelm@29434
   255
wenzelm@29434
   256
fun cancel_thy name =
wenzelm@29434
   257
  (case lookup_theory name of
wenzelm@29434
   258
    NONE => ()
wenzelm@32106
   259
  | SOME thy =>
wenzelm@32106
   260
      (case #1 (Management_Data.get thy) of
wenzelm@32106
   261
        NONE => ()
wenzelm@32106
   262
      | SOME group => Future.cancel_group group));
wenzelm@29434
   263
wenzelm@29434
   264
wenzelm@29421
   265
(* remove theory *)
wenzelm@29421
   266
wenzelm@29421
   267
fun remove_thy name =
wenzelm@29421
   268
  if is_finished name then error (loader_msg "cannot remove finished theory" [name])
wenzelm@29421
   269
  else
wenzelm@29434
   270
    let
wenzelm@29434
   271
      val succs = thy_graph Graph.all_succs [name];
wenzelm@29434
   272
      val _ = List.app cancel_thy succs;
wenzelm@29434
   273
      val _ = priority (loader_msg "removing" succs);
wenzelm@29434
   274
      val _ = CRITICAL (fn () =>
wenzelm@29434
   275
        (List.app (perform Remove) succs; change_thys (Graph.del_nodes succs)));
wenzelm@29434
   276
    in () end;
wenzelm@29421
   277
wenzelm@29421
   278
val kill_thy = if_known_thy remove_thy;
wenzelm@29421
   279
wenzelm@29421
   280
wenzelm@7941
   281
(* load_file *)
wenzelm@6211
   282
wenzelm@7191
   283
local
wenzelm@7191
   284
wenzelm@32794
   285
fun provide path name info (SOME {update_time, master, text, parents, files}) =
wenzelm@23871
   286
     (if AList.defined (op =) files path then ()
wenzelm@7941
   287
      else warning (loader_msg "undeclared dependency of theory" [name] ^
wenzelm@21858
   288
        " on file: " ^ quote (Path.implode path));
wenzelm@24151
   289
      SOME (make_deps update_time master text parents
wenzelm@24151
   290
        (AList.update (op =) (path, SOME info) files)))
skalberg@15531
   291
  | provide _ _ _ NONE = NONE;
wenzelm@7941
   292
wenzelm@7941
   293
fun run_file path =
wenzelm@26415
   294
  (case Option.map (Context.theory_name o Context.the_theory) (Context.thread_data ()) of
wenzelm@23886
   295
    NONE => (ThyLoad.load_ml Path.current path; ())
wenzelm@23871
   296
  | SOME name =>
wenzelm@23871
   297
      (case lookup_deps name of
wenzelm@23871
   298
        SOME deps =>
wenzelm@23886
   299
          change_deps name (provide path name (ThyLoad.load_ml (master_dir' deps) path))
wenzelm@23886
   300
      | NONE => (ThyLoad.load_ml Path.current path; ())));
wenzelm@6211
   301
wenzelm@7191
   302
in
wenzelm@7191
   303
wenzelm@25013
   304
fun provide_file path name =
wenzelm@25013
   305
  let
wenzelm@26983
   306
    val dir = master_directory name;
wenzelm@25013
   307
    val _ = check_unfinished error name;
wenzelm@25013
   308
  in
wenzelm@26983
   309
    (case ThyLoad.check_file dir path of
wenzelm@25013
   310
      SOME path_info => change_deps name (provide path name path_info)
wenzelm@25013
   311
    | NONE => error ("Could not find file " ^ quote (Path.implode path)))
wenzelm@25013
   312
  end;
wenzelm@25013
   313
wenzelm@24057
   314
fun load_file time path =
wenzelm@7941
   315
  if time then
wenzelm@21858
   316
    let val name = Path.implode path in
wenzelm@7244
   317
      timeit (fn () =>
wenzelm@9778
   318
       (priority ("\n**** Starting file " ^ quote name ^ " ****");
wenzelm@9036
   319
        run_file path;
wenzelm@9778
   320
        priority ("**** Finished file " ^ quote name ^ " ****\n")))
wenzelm@7244
   321
    end
wenzelm@24057
   322
  else run_file path;
wenzelm@7941
   323
wenzelm@26494
   324
fun exec_file time path = ML_Context.exec (fn () => load_file time path);
wenzelm@26494
   325
wenzelm@21858
   326
val use = load_file false o Path.explode;
wenzelm@21858
   327
val time_use = load_file true o Path.explode;
wenzelm@7191
   328
wenzelm@7191
   329
end;
wenzelm@6233
   330
wenzelm@6233
   331
wenzelm@7099
   332
(* load_thy *)
wenzelm@7099
   333
wenzelm@9490
   334
fun required_by _ [] = ""
wenzelm@9490
   335
  | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
wenzelm@7099
   336
wenzelm@27843
   337
fun load_thy time upd_time initiators name =
wenzelm@7099
   338
  let
wenzelm@24080
   339
    val _ = priority ("Loading theory " ^ quote name ^ required_by " " initiators);
wenzelm@32794
   340
    val (pos, text, _) =
wenzelm@24068
   341
      (case get_deps name of
wenzelm@24190
   342
        SOME {master = SOME (master_path, _), text as _ :: _, files, ...} =>
wenzelm@26881
   343
          (Path.position master_path, text, files)
wenzelm@24068
   344
      | _ => error (loader_msg "corrupted dependency information" [name]));
wenzelm@24068
   345
    val _ = touch_thy name;
wenzelm@24151
   346
    val _ = CRITICAL (fn () =>
wenzelm@24151
   347
      change_deps name (Option.map (fn {master, text, parents, files, ...} =>
wenzelm@24151
   348
        make_deps upd_time master text parents files)));
wenzelm@29429
   349
    val after_load = OuterSyntax.load_thy name pos text (time orelse ! Output.timing);
wenzelm@29429
   350
    val _ =
wenzelm@29429
   351
      CRITICAL (fn () =>
wenzelm@29429
   352
       (change_deps name
wenzelm@29429
   353
          (Option.map (fn {update_time, master, parents, files, ...} =>
wenzelm@29429
   354
            make_deps update_time master [] parents files));
wenzelm@29429
   355
        perform Update name));
wenzelm@29429
   356
  in after_load end;
wenzelm@7099
   357
wenzelm@7099
   358
wenzelm@24209
   359
(* scheduling loader tasks *)
wenzelm@24209
   360
wenzelm@29429
   361
datatype task = Task of (unit -> unit -> unit) | Finished | Running;
wenzelm@24209
   362
fun task_finished Finished = true | task_finished _ = false;
wenzelm@24209
   363
wenzelm@29118
   364
local
wenzelm@29118
   365
wenzelm@32814
   366
fun schedule_futures task_graph = uninterruptible (fn _ => fn () =>
wenzelm@28194
   367
  let
wenzelm@28194
   368
    val tasks = Graph.topological_order task_graph |> map_filter (fn name =>
wenzelm@28194
   369
      (case Graph.get_node task_graph name of Task body => SOME (name, body) | _ => NONE));
wenzelm@28194
   370
wenzelm@32061
   371
    val par_proofs = ! parallel_proofs >= 1;
wenzelm@29437
   372
wenzelm@29429
   373
    fun fork (name, body) tab =
wenzelm@28194
   374
      let
wenzelm@29000
   375
        val deps = Graph.imm_preds task_graph name
wenzelm@29000
   376
          |> map_filter (fn parent =>
wenzelm@29000
   377
            (case Symtab.lookup tab parent of SOME future => SOME (parent, future) | NONE => NONE));
wenzelm@29000
   378
        fun failed (parent, future) = if can Future.join future then NONE else SOME parent;
wenzelm@29000
   379
wenzelm@29437
   380
        val future = Future.fork_deps (map #2 deps) (fn () =>
wenzelm@29000
   381
          (case map_filter failed deps of
wenzelm@29000
   382
            [] => body ()
wenzelm@29000
   383
          | bad => error (loader_msg
wenzelm@29000
   384
              ("failed to load " ^ quote name ^ " (unresolved " ^ commas_quote bad ^ ")") [])));
wenzelm@29437
   385
        val future' =
wenzelm@29437
   386
          if par_proofs then future
wenzelm@29437
   387
          else Future.map (fn after_load => (after_load (); fn () => ())) future;
wenzelm@29437
   388
      in Symtab.update (name, future') tab end;
wenzelm@29000
   389
wenzelm@29429
   390
    val futures = fold fork tasks Symtab.empty;
wenzelm@29000
   391
wenzelm@34260
   392
    val failed = tasks |> maps (fn (name, _) =>
wenzelm@29429
   393
      let
wenzelm@29429
   394
        val after_load = Future.join (the (Symtab.lookup futures name));
wenzelm@32106
   395
        val _ = join_thy name;
wenzelm@29429
   396
        val _ = after_load ();
wenzelm@34260
   397
      in [] end handle exn => [(name, exn)]) |> rev;
wenzelm@29429
   398
wenzelm@34260
   399
    val _ = List.app (kill_thy o #1) failed;
wenzelm@34260
   400
    val _ = Exn.release_all (map (Exn.Exn o #2) failed);
wenzelm@34260
   401
  in () end) ();
wenzelm@29429
   402
wenzelm@29429
   403
fun schedule_seq tasks =
wenzelm@29429
   404
  Graph.topological_order tasks
wenzelm@30319
   405
  |> List.app (fn name =>
wenzelm@30319
   406
    (case Graph.get_node tasks name of
wenzelm@30319
   407
      Task body =>
wenzelm@30319
   408
        let val after_load = body ()
wenzelm@31542
   409
        in after_load () handle exn => (kill_thy name; reraise exn) end
wenzelm@30319
   410
    | _ => ()));
wenzelm@28194
   411
wenzelm@24209
   412
in
wenzelm@24209
   413
wenzelm@32794
   414
fun schedule_tasks tasks =
wenzelm@29118
   415
  if not (Multithreading.enabled ()) then schedule_seq tasks
wenzelm@29118
   416
  else if Multithreading.self_critical () then
wenzelm@24209
   417
     (warning (loader_msg "no multithreading within critical section" []);
wenzelm@24209
   418
      schedule_seq tasks)
wenzelm@29118
   419
  else schedule_futures tasks;
wenzelm@24209
   420
wenzelm@24209
   421
end;
wenzelm@24209
   422
wenzelm@24209
   423
wenzelm@23967
   424
(* require_thy -- checking database entries wrt. the file-system *)
berghofe@15065
   425
wenzelm@7211
   426
local
wenzelm@6211
   427
wenzelm@23886
   428
fun check_ml master (src_path, info) =
wenzelm@23871
   429
  let val info' =
wenzelm@23871
   430
    (case info of NONE => NONE
wenzelm@23871
   431
    | SOME (_, id) =>
wenzelm@23886
   432
        (case ThyLoad.check_ml (master_dir master) src_path of NONE => NONE
wenzelm@23871
   433
        | SOME (path', id') => if id <> id' then NONE else SOME (path', id')))
wenzelm@23886
   434
  in (src_path, info') end;
wenzelm@6211
   435
wenzelm@24175
   436
fun check_deps dir name =
wenzelm@6211
   437
  (case lookup_deps name of
wenzelm@23893
   438
    SOME NONE => (true, NONE, get_parents name)
wenzelm@23893
   439
  | NONE =>
wenzelm@24190
   440
      let val {master, text, imports = parents, uses = files} = ThyLoad.deps_thy dir name
wenzelm@24068
   441
      in (false, init_deps (SOME master) text parents files, parents) end
wenzelm@32794
   442
  | SOME (SOME {update_time, master, text, parents, files}) =>
wenzelm@24190
   443
      let
wenzelm@24190
   444
        val (thy_path, thy_id) = ThyLoad.check_thy dir name;
wenzelm@24190
   445
        val master' = SOME (thy_path, thy_id);
wenzelm@24190
   446
      in
wenzelm@24190
   447
        if Option.map #2 master <> SOME thy_id then
wenzelm@24175
   448
          let val {text = text', imports = parents', uses = files', ...} =
wenzelm@24190
   449
            ThyLoad.deps_thy dir name;
wenzelm@24175
   450
          in (false, init_deps master' text' parents' files', parents') end
wenzelm@24175
   451
        else
wenzelm@24175
   452
          let
wenzelm@24186
   453
            val files' = map (check_ml master') files;
wenzelm@24307
   454
            val current = update_time >= 0 andalso can get_theory name
wenzelm@24307
   455
              andalso forall (is_some o snd) files';
wenzelm@24175
   456
            val update_time' = if current then update_time else ~1;
wenzelm@25030
   457
            val deps' = SOME (make_deps update_time' master' text parents files');
wenzelm@24175
   458
          in (current, deps', parents) end
wenzelm@24175
   459
      end);
wenzelm@6211
   460
wenzelm@25030
   461
fun read_text (SOME {update_time, master = master as SOME (path, _), text = _, parents, files}) =
wenzelm@25030
   462
  SOME (make_deps update_time master (explode (File.read path)) parents files);
wenzelm@25030
   463
wenzelm@23967
   464
in
wenzelm@23967
   465
wenzelm@24175
   466
fun require_thys time initiators dir strs tasks =
wenzelm@24186
   467
      fold_map (require_thy time initiators dir) strs tasks |>> forall I
wenzelm@24175
   468
and require_thy time initiators dir str tasks =
wenzelm@6211
   469
  let
wenzelm@21858
   470
    val path = Path.expand (Path.explode str);
wenzelm@21858
   471
    val name = Path.implode (Path.base path);
wenzelm@23893
   472
    val dir' = Path.append dir (Path.dir path);
wenzelm@23871
   473
    val _ = member (op =) initiators name andalso error (cycle_msg initiators);
wenzelm@7066
   474
  in
wenzelm@32794
   475
    (case try (Graph.get_node tasks) name of
wenzelm@24209
   476
      SOME task => (task_finished task, tasks)
wenzelm@23967
   477
    | NONE =>
wenzelm@23967
   478
        let
wenzelm@24175
   479
          val (current, deps, parents) = check_deps dir' name
wenzelm@23967
   480
            handle ERROR msg => cat_error msg
wenzelm@23967
   481
              (loader_msg "the error(s) above occurred while examining theory" [name] ^
wenzelm@23967
   482
                required_by "\n" initiators);
wenzelm@23967
   483
          val parent_names = map base_name parents;
wenzelm@23871
   484
wenzelm@32794
   485
          val (parents_current, tasks_graph') =
wenzelm@24175
   486
            require_thys time (name :: initiators)
wenzelm@24175
   487
              (Path.append dir (master_dir' deps)) parents tasks;
wenzelm@6211
   488
wenzelm@23967
   489
          val all_current = current andalso parents_current;
wenzelm@24186
   490
          val _ = if not all_current andalso known_thy name then outdate_thy name else ();
wenzelm@25030
   491
          val entry =
wenzelm@25030
   492
            if all_current then (deps, SOME (get_theory name))
wenzelm@25030
   493
            else (read_text deps, NONE);
wenzelm@25030
   494
          val _ = change_thys (new_deps name parent_names entry);
wenzelm@23967
   495
wenzelm@24151
   496
          val upd_time = serial ();
wenzelm@23974
   497
          val tasks_graph'' = tasks_graph' |> new_deps name parent_names
wenzelm@24209
   498
           (if all_current then Finished
wenzelm@27843
   499
            else Task (fn () => load_thy time upd_time initiators name));
wenzelm@32794
   500
        in (all_current, tasks_graph'') end)
wenzelm@7066
   501
  end;
wenzelm@6484
   502
wenzelm@23967
   503
end;
wenzelm@23967
   504
wenzelm@23967
   505
wenzelm@24209
   506
(* use_thy etc. *)
wenzelm@23967
   507
wenzelm@23967
   508
local
wenzelm@23967
   509
wenzelm@24057
   510
fun gen_use_thy' req dir arg =
wenzelm@32794
   511
  schedule_tasks (snd (req [] dir arg Graph.empty));
wenzelm@6241
   512
wenzelm@23893
   513
fun gen_use_thy req str =
wenzelm@23893
   514
  let val name = base_name str in
wenzelm@23893
   515
    check_unfinished warning name;
wenzelm@25994
   516
    gen_use_thy' req Path.current str
wenzelm@23893
   517
  end;
wenzelm@7244
   518
wenzelm@7211
   519
in
wenzelm@7211
   520
wenzelm@24186
   521
val use_thys_dir = gen_use_thy' (require_thys false);
wenzelm@24186
   522
val use_thys = use_thys_dir Path.current;
wenzelm@24186
   523
val use_thy = gen_use_thy (require_thy false);
wenzelm@24186
   524
val time_use_thy = gen_use_thy (require_thy true);
wenzelm@6211
   525
wenzelm@7211
   526
end;
wenzelm@7211
   527
wenzelm@6211
   528
wenzelm@7066
   529
(* begin / end theory *)
wenzelm@6211
   530
wenzelm@23900
   531
fun begin_theory name parents uses int =
wenzelm@6211
   532
  let
wenzelm@23893
   533
    val parent_names = map base_name parents;
wenzelm@23886
   534
    val dir = master_dir'' (lookup_deps name);
wenzelm@7244
   535
    val _ = check_unfinished error name;
wenzelm@24186
   536
    val _ = if int then use_thys_dir dir parents else ();
wenzelm@17365
   537
wenzelm@24151
   538
    val theory = Theory.begin_theory name (map get_theory parent_names);
wenzelm@23900
   539
wenzelm@7952
   540
    val deps =
wenzelm@7952
   541
      if known_thy name then get_deps name
wenzelm@24068
   542
      else init_deps NONE [] parents (map #1 uses);
wenzelm@23967
   543
    val _ = change_thys (new_deps name parent_names (deps, NONE));
wenzelm@9451
   544
wenzelm@24151
   545
    val update_time = (case deps of NONE => 0 | SOME {update_time, ...} => update_time);
wenzelm@24563
   546
    val update_time = if update_time > 0 then update_time else serial ();
wenzelm@24563
   547
    val theory' = theory
wenzelm@32106
   548
      |> Management_Data.put (Future.worker_group (), update_time)
wenzelm@24563
   549
      |> Present.begin_theory update_time dir uses;
wenzelm@24151
   550
wenzelm@23900
   551
    val uses_now = map_filter (fn (x, true) => SOME x | _ => NONE) uses;
wenzelm@28292
   552
    val theory'' =
wenzelm@28292
   553
      fold (fn x => Context.theory_map (exec_file false x) o Theory.checkpoint) uses_now theory';
wenzelm@24151
   554
  in theory'' end;
wenzelm@7244
   555
wenzelm@6211
   556
fun end_theory theory =
wenzelm@7099
   557
  let
wenzelm@16454
   558
    val name = Context.theory_name theory;
wenzelm@24190
   559
    val _ = check_files name;
wenzelm@16504
   560
    val theory' = Theory.end_theory theory;
wenzelm@23900
   561
    val _ = change_thy name (fn (deps, _) => (deps, SOME theory'));
wenzelm@27579
   562
  in () end;
wenzelm@6211
   563
wenzelm@6211
   564
wenzelm@24080
   565
(* register existing theories *)
wenzelm@6211
   566
wenzelm@24080
   567
fun register_thy name =
wenzelm@24080
   568
  let
wenzelm@24080
   569
    val _ = priority ("Registering theory " ^ quote name);
wenzelm@24563
   570
    val thy = get_theory name;
wenzelm@24096
   571
    val _ = map get_theory (get_parents name);
wenzelm@24096
   572
    val _ = check_unfinished error name;
wenzelm@24080
   573
    val _ = touch_thy name;
wenzelm@24190
   574
    val master = #master (ThyLoad.deps_thy Path.current name);
wenzelm@32106
   575
    val upd_time = #2 (Management_Data.get thy);
wenzelm@24080
   576
  in
wenzelm@24080
   577
    CRITICAL (fn () =>
wenzelm@24190
   578
     (change_deps name (Option.map
wenzelm@24190
   579
       (fn {parents, files, ...} => make_deps upd_time (SOME master) [] parents files));
wenzelm@24080
   580
      perform Update name))
wenzelm@24080
   581
  end;
wenzelm@6211
   582
wenzelm@6211
   583
fun register_theory theory =
wenzelm@6211
   584
  let
wenzelm@16454
   585
    val name = Context.theory_name theory;
wenzelm@6211
   586
    val parents = Theory.parents_of theory;
wenzelm@16454
   587
    val parent_names = map Context.theory_name parents;
wenzelm@6211
   588
wenzelm@6211
   589
    fun err txt bads =
wenzelm@23871
   590
      error (loader_msg txt bads ^ "\ncannot register theory " ^ quote name);
wenzelm@6211
   591
wenzelm@6666
   592
    val nonfinished = filter_out is_finished parent_names;
wenzelm@6211
   593
    fun get_variant (x, y_name) =
skalberg@15531
   594
      if Theory.eq_thy (x, get_theory y_name) then NONE
skalberg@15531
   595
      else SOME y_name;
wenzelm@19482
   596
    val variants = map_filter get_variant (parents ~~ parent_names);
wenzelm@6211
   597
wenzelm@6211
   598
    fun register G =
skalberg@15531
   599
      (Graph.new_node (name, (NONE, SOME theory)) G
wenzelm@9327
   600
        handle Graph.DUP _ => err "duplicate theory entry" [])
wenzelm@6211
   601
      |> add_deps name parent_names;
wenzelm@6211
   602
  in
wenzelm@6666
   603
    if not (null nonfinished) then err "non-finished parent theories" nonfinished
wenzelm@6211
   604
    else if not (null variants) then err "different versions of parent theories" variants
wenzelm@23939
   605
    else CRITICAL (fn () => (change_thys register; perform Update name))
wenzelm@6211
   606
  end;
wenzelm@6211
   607
wenzelm@24080
   608
wenzelm@24080
   609
(* finish all theories *)
wenzelm@24080
   610
wenzelm@28976
   611
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (NONE, entry)));
wenzelm@24080
   612
wenzelm@6211
   613
end;