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