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