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