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