src/Pure/Thy/thy_info.ML
author wenzelm
Wed Sep 30 23:28:54 2009 +0200 (2009-09-30)
changeset 32794 7b100d30eb32
parent 32738 15bb09ca0378
child 32814 81897d30b97f
permissions -rw-r--r--
eliminated redundant bindings;
schedule_tasks/require_thy: eliminated obsolete tasks_len, which was a leftover from theory-only parallelization;
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@32106
   236
structure Management_Data = TheoryDataFun
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
  val copy = I;
wenzelm@32106
   243
  fun extend _ = empty;
wenzelm@32106
   244
  fun merge _ _ = empty;
wenzelm@32106
   245
);
wenzelm@32106
   246
wenzelm@32106
   247
val thy_ord = int_ord o pairself (#2 o Management_Data.get);
wenzelm@32106
   248
wenzelm@32106
   249
wenzelm@32106
   250
(* pending proofs *)
wenzelm@29434
   251
wenzelm@29434
   252
fun join_thy name =
wenzelm@29434
   253
  (case lookup_theory name of
wenzelm@32106
   254
    NONE => ()
wenzelm@29434
   255
  | SOME thy => PureThy.join_proofs thy);
wenzelm@29434
   256
wenzelm@29434
   257
fun cancel_thy name =
wenzelm@29434
   258
  (case lookup_theory name of
wenzelm@29434
   259
    NONE => ()
wenzelm@32106
   260
  | SOME thy =>
wenzelm@32106
   261
      (case #1 (Management_Data.get thy) of
wenzelm@32106
   262
        NONE => ()
wenzelm@32106
   263
      | SOME group => Future.cancel_group group));
wenzelm@29434
   264
wenzelm@29434
   265
wenzelm@29421
   266
(* remove theory *)
wenzelm@29421
   267
wenzelm@29421
   268
fun remove_thy name =
wenzelm@29421
   269
  if is_finished name then error (loader_msg "cannot remove finished theory" [name])
wenzelm@29421
   270
  else
wenzelm@29434
   271
    let
wenzelm@29434
   272
      val succs = thy_graph Graph.all_succs [name];
wenzelm@29434
   273
      val _ = List.app cancel_thy succs;
wenzelm@29434
   274
      val _ = priority (loader_msg "removing" succs);
wenzelm@29434
   275
      val _ = CRITICAL (fn () =>
wenzelm@29434
   276
        (List.app (perform Remove) succs; change_thys (Graph.del_nodes succs)));
wenzelm@29434
   277
    in () end;
wenzelm@29421
   278
wenzelm@29421
   279
val kill_thy = if_known_thy remove_thy;
wenzelm@29421
   280
wenzelm@29421
   281
wenzelm@7941
   282
(* load_file *)
wenzelm@6211
   283
wenzelm@7191
   284
local
wenzelm@7191
   285
wenzelm@32794
   286
fun provide path name info (SOME {update_time, master, text, parents, files}) =
wenzelm@23871
   287
     (if AList.defined (op =) files path then ()
wenzelm@7941
   288
      else warning (loader_msg "undeclared dependency of theory" [name] ^
wenzelm@21858
   289
        " on file: " ^ quote (Path.implode path));
wenzelm@24151
   290
      SOME (make_deps update_time master text parents
wenzelm@24151
   291
        (AList.update (op =) (path, SOME info) files)))
skalberg@15531
   292
  | provide _ _ _ NONE = NONE;
wenzelm@7941
   293
wenzelm@7941
   294
fun run_file path =
wenzelm@26415
   295
  (case Option.map (Context.theory_name o Context.the_theory) (Context.thread_data ()) of
wenzelm@23886
   296
    NONE => (ThyLoad.load_ml Path.current path; ())
wenzelm@23871
   297
  | SOME name =>
wenzelm@23871
   298
      (case lookup_deps name of
wenzelm@23871
   299
        SOME deps =>
wenzelm@23886
   300
          change_deps name (provide path name (ThyLoad.load_ml (master_dir' deps) path))
wenzelm@23886
   301
      | NONE => (ThyLoad.load_ml Path.current path; ())));
wenzelm@6211
   302
wenzelm@7191
   303
in
wenzelm@7191
   304
wenzelm@25013
   305
fun provide_file path name =
wenzelm@25013
   306
  let
wenzelm@26983
   307
    val dir = master_directory name;
wenzelm@25013
   308
    val _ = check_unfinished error name;
wenzelm@25013
   309
  in
wenzelm@26983
   310
    (case ThyLoad.check_file dir path of
wenzelm@25013
   311
      SOME path_info => change_deps name (provide path name path_info)
wenzelm@25013
   312
    | NONE => error ("Could not find file " ^ quote (Path.implode path)))
wenzelm@25013
   313
  end;
wenzelm@25013
   314
wenzelm@24057
   315
fun load_file time path =
wenzelm@7941
   316
  if time then
wenzelm@21858
   317
    let val name = Path.implode path in
wenzelm@7244
   318
      timeit (fn () =>
wenzelm@9778
   319
       (priority ("\n**** Starting file " ^ quote name ^ " ****");
wenzelm@9036
   320
        run_file path;
wenzelm@9778
   321
        priority ("**** Finished file " ^ quote name ^ " ****\n")))
wenzelm@7244
   322
    end
wenzelm@24057
   323
  else run_file path;
wenzelm@7941
   324
wenzelm@26494
   325
fun exec_file time path = ML_Context.exec (fn () => load_file time path);
wenzelm@26494
   326
wenzelm@21858
   327
val use = load_file false o Path.explode;
wenzelm@21858
   328
val time_use = load_file true o Path.explode;
wenzelm@7191
   329
wenzelm@7191
   330
end;
wenzelm@6233
   331
wenzelm@6233
   332
wenzelm@7099
   333
(* load_thy *)
wenzelm@7099
   334
wenzelm@9490
   335
fun required_by _ [] = ""
wenzelm@9490
   336
  | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
wenzelm@7099
   337
wenzelm@27843
   338
fun load_thy time upd_time initiators name =
wenzelm@7099
   339
  let
wenzelm@24080
   340
    val _ = priority ("Loading theory " ^ quote name ^ required_by " " initiators);
wenzelm@32794
   341
    val (pos, text, _) =
wenzelm@24068
   342
      (case get_deps name of
wenzelm@24190
   343
        SOME {master = SOME (master_path, _), text as _ :: _, files, ...} =>
wenzelm@26881
   344
          (Path.position master_path, text, files)
wenzelm@24068
   345
      | _ => error (loader_msg "corrupted dependency information" [name]));
wenzelm@24068
   346
    val _ = touch_thy name;
wenzelm@24151
   347
    val _ = CRITICAL (fn () =>
wenzelm@24151
   348
      change_deps name (Option.map (fn {master, text, parents, files, ...} =>
wenzelm@24151
   349
        make_deps upd_time master text parents files)));
wenzelm@29429
   350
    val after_load = OuterSyntax.load_thy name pos text (time orelse ! Output.timing);
wenzelm@29429
   351
    val _ =
wenzelm@29429
   352
      CRITICAL (fn () =>
wenzelm@29429
   353
       (change_deps name
wenzelm@29429
   354
          (Option.map (fn {update_time, master, parents, files, ...} =>
wenzelm@29429
   355
            make_deps update_time master [] parents files));
wenzelm@29429
   356
        perform Update name));
wenzelm@29429
   357
  in after_load end;
wenzelm@7099
   358
wenzelm@7099
   359
wenzelm@24209
   360
(* scheduling loader tasks *)
wenzelm@24209
   361
wenzelm@29429
   362
datatype task = Task of (unit -> unit -> unit) | Finished | Running;
wenzelm@24209
   363
fun task_finished Finished = true | task_finished _ = false;
wenzelm@24209
   364
wenzelm@29118
   365
local
wenzelm@29118
   366
wenzelm@29118
   367
fun schedule_futures task_graph =
wenzelm@28194
   368
  let
wenzelm@28194
   369
    val tasks = Graph.topological_order task_graph |> map_filter (fn name =>
wenzelm@28194
   370
      (case Graph.get_node task_graph name of Task body => SOME (name, body) | _ => NONE));
wenzelm@28194
   371
wenzelm@32061
   372
    val par_proofs = ! parallel_proofs >= 1;
wenzelm@29437
   373
wenzelm@29429
   374
    fun fork (name, body) tab =
wenzelm@28194
   375
      let
wenzelm@29000
   376
        val deps = Graph.imm_preds task_graph name
wenzelm@29000
   377
          |> map_filter (fn parent =>
wenzelm@29000
   378
            (case Symtab.lookup tab parent of SOME future => SOME (parent, future) | NONE => NONE));
wenzelm@29000
   379
        fun failed (parent, future) = if can Future.join future then NONE else SOME parent;
wenzelm@29000
   380
wenzelm@29437
   381
        val future = Future.fork_deps (map #2 deps) (fn () =>
wenzelm@29000
   382
          (case map_filter failed deps of
wenzelm@29000
   383
            [] => body ()
wenzelm@29000
   384
          | bad => error (loader_msg
wenzelm@29000
   385
              ("failed to load " ^ quote name ^ " (unresolved " ^ commas_quote bad ^ ")") [])));
wenzelm@29437
   386
        val future' =
wenzelm@29437
   387
          if par_proofs then future
wenzelm@29437
   388
          else Future.map (fn after_load => (after_load (); fn () => ())) future;
wenzelm@29437
   389
      in Symtab.update (name, future') tab end;
wenzelm@29000
   390
wenzelm@29429
   391
    val futures = fold fork tasks Symtab.empty;
wenzelm@29000
   392
wenzelm@30819
   393
    val exns = tasks |> maps (fn (name, _) =>
wenzelm@29429
   394
      let
wenzelm@29429
   395
        val after_load = Future.join (the (Symtab.lookup futures name));
wenzelm@32106
   396
        val _ = join_thy name;
wenzelm@29429
   397
        val _ = after_load ();
wenzelm@29429
   398
      in [] end handle exn => (kill_thy name; [exn]));
wenzelm@29429
   399
wenzelm@29434
   400
  in ignore (Exn.release_all (map Exn.Exn (rev exns))) end;
wenzelm@29429
   401
wenzelm@29429
   402
fun schedule_seq tasks =
wenzelm@29429
   403
  Graph.topological_order tasks
wenzelm@30319
   404
  |> List.app (fn name =>
wenzelm@30319
   405
    (case Graph.get_node tasks name of
wenzelm@30319
   406
      Task body =>
wenzelm@30319
   407
        let val after_load = body ()
wenzelm@31542
   408
        in after_load () handle exn => (kill_thy name; reraise exn) end
wenzelm@30319
   409
    | _ => ()));
wenzelm@28194
   410
wenzelm@24209
   411
in
wenzelm@24209
   412
wenzelm@32794
   413
fun schedule_tasks tasks =
wenzelm@29118
   414
  if not (Multithreading.enabled ()) then schedule_seq tasks
wenzelm@29118
   415
  else if Multithreading.self_critical () then
wenzelm@24209
   416
     (warning (loader_msg "no multithreading within critical section" []);
wenzelm@24209
   417
      schedule_seq tasks)
wenzelm@29118
   418
  else schedule_futures tasks;
wenzelm@24209
   419
wenzelm@24209
   420
end;
wenzelm@24209
   421
wenzelm@24209
   422
wenzelm@23967
   423
(* require_thy -- checking database entries wrt. the file-system *)
berghofe@15065
   424
wenzelm@7211
   425
local
wenzelm@6211
   426
wenzelm@23886
   427
fun check_ml master (src_path, info) =
wenzelm@23871
   428
  let val info' =
wenzelm@23871
   429
    (case info of NONE => NONE
wenzelm@23871
   430
    | SOME (_, id) =>
wenzelm@23886
   431
        (case ThyLoad.check_ml (master_dir master) src_path of NONE => NONE
wenzelm@23871
   432
        | SOME (path', id') => if id <> id' then NONE else SOME (path', id')))
wenzelm@23886
   433
  in (src_path, info') end;
wenzelm@6211
   434
wenzelm@24175
   435
fun check_deps dir name =
wenzelm@6211
   436
  (case lookup_deps name of
wenzelm@23893
   437
    SOME NONE => (true, NONE, get_parents name)
wenzelm@23893
   438
  | NONE =>
wenzelm@24190
   439
      let val {master, text, imports = parents, uses = files} = ThyLoad.deps_thy dir name
wenzelm@24068
   440
      in (false, init_deps (SOME master) text parents files, parents) end
wenzelm@32794
   441
  | SOME (SOME {update_time, master, text, parents, files}) =>
wenzelm@24190
   442
      let
wenzelm@24190
   443
        val (thy_path, thy_id) = ThyLoad.check_thy dir name;
wenzelm@24190
   444
        val master' = SOME (thy_path, thy_id);
wenzelm@24190
   445
      in
wenzelm@24190
   446
        if Option.map #2 master <> SOME thy_id then
wenzelm@24175
   447
          let val {text = text', imports = parents', uses = files', ...} =
wenzelm@24190
   448
            ThyLoad.deps_thy dir name;
wenzelm@24175
   449
          in (false, init_deps master' text' parents' files', parents') end
wenzelm@24175
   450
        else
wenzelm@24175
   451
          let
wenzelm@24186
   452
            val files' = map (check_ml master') files;
wenzelm@24307
   453
            val current = update_time >= 0 andalso can get_theory name
wenzelm@24307
   454
              andalso forall (is_some o snd) files';
wenzelm@24175
   455
            val update_time' = if current then update_time else ~1;
wenzelm@25030
   456
            val deps' = SOME (make_deps update_time' master' text parents files');
wenzelm@24175
   457
          in (current, deps', parents) end
wenzelm@24175
   458
      end);
wenzelm@6211
   459
wenzelm@25030
   460
fun read_text (SOME {update_time, master = master as SOME (path, _), text = _, parents, files}) =
wenzelm@25030
   461
  SOME (make_deps update_time master (explode (File.read path)) parents files);
wenzelm@25030
   462
wenzelm@23967
   463
in
wenzelm@23967
   464
wenzelm@24175
   465
fun require_thys time initiators dir strs tasks =
wenzelm@24186
   466
      fold_map (require_thy time initiators dir) strs tasks |>> forall I
wenzelm@24175
   467
and require_thy time initiators dir str tasks =
wenzelm@6211
   468
  let
wenzelm@21858
   469
    val path = Path.expand (Path.explode str);
wenzelm@21858
   470
    val name = Path.implode (Path.base path);
wenzelm@23893
   471
    val dir' = Path.append dir (Path.dir path);
wenzelm@23871
   472
    val _ = member (op =) initiators name andalso error (cycle_msg initiators);
wenzelm@7066
   473
  in
wenzelm@32794
   474
    (case try (Graph.get_node tasks) name of
wenzelm@24209
   475
      SOME task => (task_finished task, tasks)
wenzelm@23967
   476
    | NONE =>
wenzelm@23967
   477
        let
wenzelm@24175
   478
          val (current, deps, parents) = check_deps dir' name
wenzelm@23967
   479
            handle ERROR msg => cat_error msg
wenzelm@23967
   480
              (loader_msg "the error(s) above occurred while examining theory" [name] ^
wenzelm@23967
   481
                required_by "\n" initiators);
wenzelm@23967
   482
          val parent_names = map base_name parents;
wenzelm@23871
   483
wenzelm@32794
   484
          val (parents_current, tasks_graph') =
wenzelm@24175
   485
            require_thys time (name :: initiators)
wenzelm@24175
   486
              (Path.append dir (master_dir' deps)) parents tasks;
wenzelm@6211
   487
wenzelm@23967
   488
          val all_current = current andalso parents_current;
wenzelm@24186
   489
          val _ = if not all_current andalso known_thy name then outdate_thy name else ();
wenzelm@25030
   490
          val entry =
wenzelm@25030
   491
            if all_current then (deps, SOME (get_theory name))
wenzelm@25030
   492
            else (read_text deps, NONE);
wenzelm@25030
   493
          val _ = change_thys (new_deps name parent_names entry);
wenzelm@23967
   494
wenzelm@24151
   495
          val upd_time = serial ();
wenzelm@23974
   496
          val tasks_graph'' = tasks_graph' |> new_deps name parent_names
wenzelm@24209
   497
           (if all_current then Finished
wenzelm@27843
   498
            else Task (fn () => load_thy time upd_time initiators name));
wenzelm@32794
   499
        in (all_current, tasks_graph'') end)
wenzelm@7066
   500
  end;
wenzelm@6484
   501
wenzelm@23967
   502
end;
wenzelm@23967
   503
wenzelm@23967
   504
wenzelm@24209
   505
(* use_thy etc. *)
wenzelm@23967
   506
wenzelm@23967
   507
local
wenzelm@23967
   508
wenzelm@24057
   509
fun gen_use_thy' req dir arg =
wenzelm@32794
   510
  schedule_tasks (snd (req [] dir arg Graph.empty));
wenzelm@6241
   511
wenzelm@23893
   512
fun gen_use_thy req str =
wenzelm@23893
   513
  let val name = base_name str in
wenzelm@23893
   514
    check_unfinished warning name;
wenzelm@25994
   515
    gen_use_thy' req Path.current str
wenzelm@23893
   516
  end;
wenzelm@7244
   517
wenzelm@7211
   518
in
wenzelm@7211
   519
wenzelm@24186
   520
val use_thys_dir = gen_use_thy' (require_thys false);
wenzelm@24186
   521
val use_thys = use_thys_dir Path.current;
wenzelm@24186
   522
val use_thy = gen_use_thy (require_thy false);
wenzelm@24186
   523
val time_use_thy = gen_use_thy (require_thy true);
wenzelm@6211
   524
wenzelm@7211
   525
end;
wenzelm@7211
   526
wenzelm@6211
   527
wenzelm@7066
   528
(* begin / end theory *)
wenzelm@6211
   529
wenzelm@23900
   530
fun begin_theory name parents uses int =
wenzelm@6211
   531
  let
wenzelm@23893
   532
    val parent_names = map base_name parents;
wenzelm@23886
   533
    val dir = master_dir'' (lookup_deps name);
wenzelm@7244
   534
    val _ = check_unfinished error name;
wenzelm@24186
   535
    val _ = if int then use_thys_dir dir parents else ();
wenzelm@17365
   536
wenzelm@24151
   537
    val theory = Theory.begin_theory name (map get_theory parent_names);
wenzelm@23900
   538
wenzelm@7952
   539
    val deps =
wenzelm@7952
   540
      if known_thy name then get_deps name
wenzelm@24068
   541
      else init_deps NONE [] parents (map #1 uses);
wenzelm@23967
   542
    val _ = change_thys (new_deps name parent_names (deps, NONE));
wenzelm@9451
   543
wenzelm@24151
   544
    val update_time = (case deps of NONE => 0 | SOME {update_time, ...} => update_time);
wenzelm@24563
   545
    val update_time = if update_time > 0 then update_time else serial ();
wenzelm@24563
   546
    val theory' = theory
wenzelm@32106
   547
      |> Management_Data.put (Future.worker_group (), update_time)
wenzelm@24563
   548
      |> Present.begin_theory update_time dir uses;
wenzelm@24151
   549
wenzelm@23900
   550
    val uses_now = map_filter (fn (x, true) => SOME x | _ => NONE) uses;
wenzelm@28292
   551
    val theory'' =
wenzelm@28292
   552
      fold (fn x => Context.theory_map (exec_file false x) o Theory.checkpoint) uses_now theory';
wenzelm@24151
   553
  in theory'' end;
wenzelm@7244
   554
wenzelm@6211
   555
fun end_theory theory =
wenzelm@7099
   556
  let
wenzelm@16454
   557
    val name = Context.theory_name theory;
wenzelm@24190
   558
    val _ = check_files name;
wenzelm@16504
   559
    val theory' = Theory.end_theory theory;
wenzelm@23900
   560
    val _ = change_thy name (fn (deps, _) => (deps, SOME theory'));
wenzelm@27579
   561
  in () end;
wenzelm@6211
   562
wenzelm@6211
   563
wenzelm@24080
   564
(* register existing theories *)
wenzelm@6211
   565
wenzelm@24080
   566
fun register_thy name =
wenzelm@24080
   567
  let
wenzelm@24080
   568
    val _ = priority ("Registering theory " ^ quote name);
wenzelm@24563
   569
    val thy = get_theory name;
wenzelm@24096
   570
    val _ = map get_theory (get_parents name);
wenzelm@24096
   571
    val _ = check_unfinished error name;
wenzelm@24080
   572
    val _ = touch_thy name;
wenzelm@24190
   573
    val master = #master (ThyLoad.deps_thy Path.current name);
wenzelm@32106
   574
    val upd_time = #2 (Management_Data.get thy);
wenzelm@24080
   575
  in
wenzelm@24080
   576
    CRITICAL (fn () =>
wenzelm@24190
   577
     (change_deps name (Option.map
wenzelm@24190
   578
       (fn {parents, files, ...} => make_deps upd_time (SOME master) [] parents files));
wenzelm@24080
   579
      perform Update name))
wenzelm@24080
   580
  end;
wenzelm@6211
   581
wenzelm@6211
   582
fun register_theory theory =
wenzelm@6211
   583
  let
wenzelm@16454
   584
    val name = Context.theory_name theory;
wenzelm@6211
   585
    val parents = Theory.parents_of theory;
wenzelm@16454
   586
    val parent_names = map Context.theory_name parents;
wenzelm@6211
   587
wenzelm@6211
   588
    fun err txt bads =
wenzelm@23871
   589
      error (loader_msg txt bads ^ "\ncannot register theory " ^ quote name);
wenzelm@6211
   590
wenzelm@6666
   591
    val nonfinished = filter_out is_finished parent_names;
wenzelm@6211
   592
    fun get_variant (x, y_name) =
skalberg@15531
   593
      if Theory.eq_thy (x, get_theory y_name) then NONE
skalberg@15531
   594
      else SOME y_name;
wenzelm@19482
   595
    val variants = map_filter get_variant (parents ~~ parent_names);
wenzelm@6211
   596
wenzelm@6211
   597
    fun register G =
skalberg@15531
   598
      (Graph.new_node (name, (NONE, SOME theory)) G
wenzelm@9327
   599
        handle Graph.DUP _ => err "duplicate theory entry" [])
wenzelm@6211
   600
      |> add_deps name parent_names;
wenzelm@6211
   601
  in
wenzelm@6666
   602
    if not (null nonfinished) then err "non-finished parent theories" nonfinished
wenzelm@6211
   603
    else if not (null variants) then err "different versions of parent theories" variants
wenzelm@23939
   604
    else CRITICAL (fn () => (change_thys register; perform Update name))
wenzelm@6211
   605
  end;
wenzelm@6211
   606
wenzelm@24080
   607
wenzelm@24080
   608
(* finish all theories *)
wenzelm@24080
   609
wenzelm@28976
   610
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (NONE, entry)));
wenzelm@24080
   611
wenzelm@6211
   612
end;