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