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