src/Pure/Thy/thy_info.ML
author wenzelm
Wed Jan 02 16:32:53 2008 +0100 (2008-01-02 ago)
changeset 25775 90525e67ede7
parent 25736 68834086f910
child 25994 d35484265f46
permissions -rw-r--r--
added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
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@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@6666
   157
fun is_finished name = is_none (get_deps name);
wenzelm@7191
   158
wenzelm@7191
   159
fun loaded_files name =
wenzelm@7191
   160
  (case get_deps name of
skalberg@15531
   161
    NONE => []
skalberg@15531
   162
  | SOME {master, files, ...} =>
wenzelm@24190
   163
      (case master of SOME (thy_path, _) => [thy_path] | NONE => []) @
wenzelm@19482
   164
      (map_filter (Option.map #1 o #2) files));
wenzelm@6211
   165
wenzelm@23871
   166
fun get_parents name =
wenzelm@23967
   167
  thy_graph Graph.imm_preds name handle Graph.UNDEF _ =>
berghofe@6654
   168
    error (loader_msg "nothing known about theory" [name]);
berghofe@6654
   169
wenzelm@6211
   170
wenzelm@24563
   171
(* pretty_theory *)
wenzelm@16047
   172
wenzelm@16047
   173
local
paulson@15633
   174
wenzelm@16047
   175
fun relevant_names names =
wenzelm@16047
   176
  let
wenzelm@16047
   177
    val (finished, unfinished) =
wenzelm@19305
   178
      List.filter (fn name => name = Context.draftN orelse known_thy name) names
wenzelm@19305
   179
      |> List.partition (fn name => name <> Context.draftN andalso is_finished name);
wenzelm@16047
   180
  in (if not (null finished) then [List.last finished] else []) @ unfinished end;
paulson@15633
   181
wenzelm@16047
   182
in
paulson@15633
   183
wenzelm@16454
   184
fun pretty_theory thy =
wenzelm@16454
   185
  Pretty.str_list "{" "}" (relevant_names (Context.names_of thy));
paulson@15633
   186
wenzelm@16047
   187
end;
wenzelm@16047
   188
paulson@15633
   189
wenzelm@6211
   190
(* access theory *)
wenzelm@6211
   191
wenzelm@7687
   192
fun lookup_theory name =
wenzelm@7687
   193
  (case lookup_thy name of
skalberg@15531
   194
    SOME (_, SOME thy) => SOME thy
skalberg@15531
   195
  | _ => NONE);
wenzelm@7288
   196
wenzelm@6211
   197
fun get_theory name =
wenzelm@7288
   198
  (case lookup_theory name of
wenzelm@23871
   199
    SOME theory => theory
wenzelm@6211
   200
  | _ => error (loader_msg "undefined theory entry for" [name]));
wenzelm@6211
   201
wenzelm@19547
   202
fun the_theory name thy =
wenzelm@19547
   203
  if Context.theory_name thy = name then thy
wenzelm@19547
   204
  else get_theory name;
wenzelm@19547
   205
wenzelm@22137
   206
val _ = ML_Context.value_antiq "theory"
wenzelm@22137
   207
  (Scan.lift Args.name
wenzelm@22137
   208
    >> (fn name => (name ^ "_thy", "ThyInfo.theory " ^ ML_Syntax.print_string name))
wenzelm@22148
   209
  || Scan.succeed ("thy", "ML_Context.the_context ()"));
wenzelm@22137
   210
wenzelm@24696
   211
val _ = ML_Context.value_antiq "theory_ref"
wenzelm@24696
   212
  (Scan.lift Args.name
wenzelm@24696
   213
    >> (fn name => (name ^ "_thy",
wenzelm@24696
   214
        "Theory.check_thy (ThyInfo.theory " ^ ML_Syntax.print_string name ^ ")"))
wenzelm@24696
   215
  || Scan.succeed ("thy", "Theory.check_thy (ML_Context.the_context ())"));
wenzelm@24696
   216
wenzelm@6211
   217
wenzelm@6211
   218
wenzelm@6211
   219
(** thy operations **)
wenzelm@6211
   220
wenzelm@24080
   221
(* check state *)
wenzelm@24080
   222
wenzelm@24080
   223
fun check_unfinished fail name =
wenzelm@24080
   224
  if known_thy name andalso is_finished name then
wenzelm@24080
   225
    fail (loader_msg "cannot update finished theory" [name])
wenzelm@24080
   226
  else ();
wenzelm@24080
   227
wenzelm@24080
   228
fun check_files name =
wenzelm@24080
   229
  let
wenzelm@24080
   230
    val files = (case get_deps name of SOME {files, ...} => files | NONE => []);
wenzelm@24080
   231
    val missing_files = map_filter (fn (path, NONE) => SOME (Path.implode path) | _ => NONE) files;
wenzelm@24190
   232
    val _ = null missing_files orelse
wenzelm@24190
   233
      error (loader_msg "unresolved dependencies of theory" [name] ^
wenzelm@24080
   234
        " on file(s): " ^ commas_quote missing_files);
wenzelm@24190
   235
  in () end;
wenzelm@24080
   236
wenzelm@24080
   237
wenzelm@24151
   238
(* maintain update_time *)
wenzelm@6211
   239
wenzelm@7099
   240
local
wenzelm@7099
   241
wenzelm@6241
   242
fun is_outdated name =
wenzelm@6241
   243
  (case lookup_deps name of
wenzelm@24151
   244
    SOME (SOME {update_time, ...}) => update_time < 0
wenzelm@6241
   245
  | _ => false);
wenzelm@6211
   246
wenzelm@25013
   247
fun unfinished name =
wenzelm@24186
   248
  if is_finished name then (warning (loader_msg "tried to touch finished theory" [name]); NONE)
wenzelm@24186
   249
  else SOME name;
wenzelm@24186
   250
wenzelm@24186
   251
in
wenzelm@24186
   252
wenzelm@6241
   253
fun outdate_thy name =
wenzelm@7099
   254
  if is_finished name orelse is_outdated name then ()
wenzelm@23939
   255
  else CRITICAL (fn () =>
wenzelm@24151
   256
   (change_deps name (Option.map (fn {master, text, parents, files, ...} =>
wenzelm@24151
   257
    make_deps ~1 master text parents files)); perform Outdate name));
wenzelm@7099
   258
wenzelm@7910
   259
fun touch_thys names =
wenzelm@25013
   260
  List.app outdate_thy (thy_graph Graph.all_succs (map_filter unfinished names));
wenzelm@7910
   261
wenzelm@7910
   262
fun touch_thy name = touch_thys [name];
wenzelm@7910
   263
fun touch_child_thys name = touch_thys (thy_graph Graph.imm_succs name);
wenzelm@6211
   264
wenzelm@7099
   265
end;
wenzelm@6211
   266
wenzelm@6211
   267
wenzelm@7941
   268
(* load_file *)
wenzelm@6211
   269
wenzelm@7191
   270
local
wenzelm@7191
   271
wenzelm@24151
   272
fun provide path name info (deps as SOME {update_time, master, text, parents, files}) =
wenzelm@23871
   273
     (if AList.defined (op =) files path then ()
wenzelm@7941
   274
      else warning (loader_msg "undeclared dependency of theory" [name] ^
wenzelm@21858
   275
        " on file: " ^ quote (Path.implode path));
wenzelm@24151
   276
      SOME (make_deps update_time master text parents
wenzelm@24151
   277
        (AList.update (op =) (path, SOME info) files)))
skalberg@15531
   278
  | provide _ _ _ NONE = NONE;
wenzelm@7941
   279
wenzelm@7941
   280
fun run_file path =
wenzelm@22095
   281
  (case Option.map (Context.theory_name o Context.the_theory) (ML_Context.get_context ()) of
wenzelm@23886
   282
    NONE => (ThyLoad.load_ml Path.current path; ())
wenzelm@23871
   283
  | SOME name =>
wenzelm@23871
   284
      (case lookup_deps name of
wenzelm@23871
   285
        SOME deps =>
wenzelm@23886
   286
          change_deps name (provide path name (ThyLoad.load_ml (master_dir' deps) path))
wenzelm@23886
   287
      | NONE => (ThyLoad.load_ml Path.current path; ())));
wenzelm@6211
   288
wenzelm@7191
   289
in
wenzelm@7191
   290
wenzelm@25013
   291
fun provide_file path name =
wenzelm@25013
   292
  let
wenzelm@25013
   293
    val deps = get_deps name;
wenzelm@25013
   294
    val _ = check_unfinished error name;
wenzelm@25013
   295
  in
wenzelm@25013
   296
    (case ThyLoad.check_file (master_dir' deps) path of
wenzelm@25013
   297
      SOME path_info => change_deps name (provide path name path_info)
wenzelm@25013
   298
    | NONE => error ("Could not find file " ^ quote (Path.implode path)))
wenzelm@25013
   299
  end;
wenzelm@25013
   300
wenzelm@24057
   301
fun load_file time path =
wenzelm@7941
   302
  if time then
wenzelm@21858
   303
    let val name = Path.implode path in
wenzelm@7244
   304
      timeit (fn () =>
wenzelm@9778
   305
       (priority ("\n**** Starting file " ^ quote name ^ " ****");
wenzelm@9036
   306
        run_file path;
wenzelm@9778
   307
        priority ("**** Finished file " ^ quote name ^ " ****\n")))
wenzelm@7244
   308
    end
wenzelm@24057
   309
  else run_file path;
wenzelm@7941
   310
wenzelm@21858
   311
val use = load_file false o Path.explode;
wenzelm@21858
   312
val time_use = load_file true o Path.explode;
wenzelm@7191
   313
wenzelm@7191
   314
end;
wenzelm@6233
   315
wenzelm@6233
   316
wenzelm@7099
   317
(* load_thy *)
wenzelm@7099
   318
wenzelm@9490
   319
fun required_by _ [] = ""
wenzelm@9490
   320
  | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
wenzelm@7099
   321
wenzelm@24190
   322
fun load_thy time upd_time initiators dir name =
wenzelm@7099
   323
  let
wenzelm@24080
   324
    val _ = priority ("Loading theory " ^ quote name ^ required_by " " initiators);
wenzelm@24080
   325
    val (pos, text, files) =
wenzelm@24068
   326
      (case get_deps name of
wenzelm@24190
   327
        SOME {master = SOME (master_path, _), text as _ :: _, files, ...} =>
wenzelm@24080
   328
          (Position.path master_path, text, files)
wenzelm@24068
   329
      | _ => error (loader_msg "corrupted dependency information" [name]));
wenzelm@24068
   330
    val _ = touch_thy name;
wenzelm@24151
   331
    val _ = CRITICAL (fn () =>
wenzelm@24151
   332
      change_deps name (Option.map (fn {master, text, parents, files, ...} =>
wenzelm@24151
   333
        make_deps upd_time master text parents files)));
wenzelm@24190
   334
    val _ = ThyLoad.load_thy dir name pos text (time orelse ! Output.timing);
wenzelm@7099
   335
  in
wenzelm@23939
   336
    CRITICAL (fn () =>
wenzelm@23939
   337
     (change_deps name
wenzelm@24151
   338
        (Option.map (fn {update_time, master, parents, files, ...} =>
wenzelm@24151
   339
          make_deps update_time master [] parents files));
wenzelm@23939
   340
      perform Update name))
wenzelm@7099
   341
  end;
wenzelm@7099
   342
wenzelm@7099
   343
wenzelm@24209
   344
(* scheduling loader tasks *)
wenzelm@24209
   345
wenzelm@24209
   346
datatype task = Task of (unit -> unit) | Finished | Running;
wenzelm@24209
   347
fun task_finished Finished = true | task_finished _ = false;
wenzelm@24209
   348
wenzelm@24209
   349
local
wenzelm@24209
   350
wenzelm@24230
   351
fun max_task (name, (Task body, m)) NONE = SOME (name: string, (body, m))
wenzelm@24229
   352
  | max_task (name, (Task body, m)) (task' as SOME (name', (_, m'))) =
wenzelm@24229
   353
      if m > m' orelse m = m' andalso name < name' then SOME (name, (body, m)) else task'
wenzelm@24209
   354
  | max_task _ task' = task';
wenzelm@24209
   355
wenzelm@24209
   356
fun next_task G =
wenzelm@24209
   357
  let
wenzelm@24209
   358
    val tasks = Graph.minimals G |> map (fn name =>
wenzelm@24209
   359
      (name, (Graph.get_node G name, length (Graph.imm_succs G name))));
wenzelm@24209
   360
    val finished = filter (task_finished o fst o snd) tasks;
wenzelm@24209
   361
  in
wenzelm@24209
   362
    if not (null finished) then next_task (Graph.del_nodes (map fst finished) G)
wenzelm@24209
   363
    else if null tasks then (Multithreading.Terminate, G)
wenzelm@24209
   364
    else
wenzelm@24209
   365
      (case fold max_task tasks NONE of
wenzelm@24209
   366
        NONE => (Multithreading.Wait, G)
wenzelm@24209
   367
      | SOME (name, (body, _)) =>
wenzelm@25736
   368
         (Multithreading.Task {body = PrintMode.closure body,
wenzelm@25736
   369
           cont = Graph.del_nodes [name], fail = K Graph.empty},
wenzelm@24209
   370
          Graph.map_node name (K Running) G))
wenzelm@24209
   371
  end;
wenzelm@24209
   372
wenzelm@24209
   373
fun schedule_seq tasks =
wenzelm@24209
   374
  Graph.topological_order tasks
wenzelm@24209
   375
  |> List.app (fn name => (case Graph.get_node tasks name of Task body => body () | _ => ()));
wenzelm@24209
   376
wenzelm@24209
   377
in
wenzelm@24209
   378
wenzelm@24209
   379
fun schedule_tasks tasks n =
wenzelm@25775
   380
  let val m = Multithreading.max_threads_value () in
wenzelm@24209
   381
    if m <= 1 orelse n <= 1 then schedule_seq tasks
wenzelm@24209
   382
    else if Multithreading.self_critical () then
wenzelm@24209
   383
     (warning (loader_msg "no multithreading within critical section" []);
wenzelm@24209
   384
      schedule_seq tasks)
wenzelm@24209
   385
    else
wenzelm@24209
   386
      (case Multithreading.schedule (Int.min (m, n)) next_task tasks of
wenzelm@24209
   387
        [] => ()
wenzelm@24209
   388
      | exns => raise Exn.EXCEPTIONS (exns, ""))
wenzelm@24209
   389
  end;
wenzelm@24209
   390
wenzelm@24209
   391
end;
wenzelm@24209
   392
wenzelm@24209
   393
wenzelm@23967
   394
(* require_thy -- checking database entries wrt. the file-system *)
berghofe@15065
   395
wenzelm@7211
   396
local
wenzelm@6211
   397
wenzelm@23886
   398
fun check_ml master (src_path, info) =
wenzelm@23871
   399
  let val info' =
wenzelm@23871
   400
    (case info of NONE => NONE
wenzelm@23871
   401
    | SOME (_, id) =>
wenzelm@23886
   402
        (case ThyLoad.check_ml (master_dir master) src_path of NONE => NONE
wenzelm@23871
   403
        | SOME (path', id') => if id <> id' then NONE else SOME (path', id')))
wenzelm@23886
   404
  in (src_path, info') end;
wenzelm@6211
   405
wenzelm@24175
   406
fun check_deps dir name =
wenzelm@6211
   407
  (case lookup_deps name of
wenzelm@23893
   408
    SOME NONE => (true, NONE, get_parents name)
wenzelm@23893
   409
  | NONE =>
wenzelm@24190
   410
      let val {master, text, imports = parents, uses = files} = ThyLoad.deps_thy dir name
wenzelm@24068
   411
      in (false, init_deps (SOME master) text parents files, parents) end
wenzelm@24151
   412
  | SOME (deps as SOME {update_time, master, text, parents, files}) =>
wenzelm@24190
   413
      let
wenzelm@24190
   414
        val (thy_path, thy_id) = ThyLoad.check_thy dir name;
wenzelm@24190
   415
        val master' = SOME (thy_path, thy_id);
wenzelm@24190
   416
      in
wenzelm@24190
   417
        if Option.map #2 master <> SOME thy_id then
wenzelm@24175
   418
          let val {text = text', imports = parents', uses = files', ...} =
wenzelm@24190
   419
            ThyLoad.deps_thy dir name;
wenzelm@24175
   420
          in (false, init_deps master' text' parents' files', parents') end
wenzelm@24175
   421
        else
wenzelm@24175
   422
          let
wenzelm@24186
   423
            val files' = map (check_ml master') files;
wenzelm@24307
   424
            val current = update_time >= 0 andalso can get_theory name
wenzelm@24307
   425
              andalso forall (is_some o snd) files';
wenzelm@24175
   426
            val update_time' = if current then update_time else ~1;
wenzelm@25030
   427
            val deps' = SOME (make_deps update_time' master' text parents files');
wenzelm@24175
   428
          in (current, deps', parents) end
wenzelm@24175
   429
      end);
wenzelm@6211
   430
wenzelm@25030
   431
fun read_text (SOME {update_time, master = master as SOME (path, _), text = _, parents, files}) =
wenzelm@25030
   432
  SOME (make_deps update_time master (explode (File.read path)) parents files);
wenzelm@25030
   433
wenzelm@23967
   434
in
wenzelm@23967
   435
wenzelm@24175
   436
fun require_thys time initiators dir strs tasks =
wenzelm@24186
   437
      fold_map (require_thy time initiators dir) strs tasks |>> forall I
wenzelm@24175
   438
and require_thy time initiators dir str tasks =
wenzelm@6211
   439
  let
wenzelm@21858
   440
    val path = Path.expand (Path.explode str);
wenzelm@21858
   441
    val name = Path.implode (Path.base path);
wenzelm@23893
   442
    val dir' = Path.append dir (Path.dir path);
wenzelm@23871
   443
    val _ = member (op =) initiators name andalso error (cycle_msg initiators);
wenzelm@7066
   444
  in
wenzelm@23974
   445
    (case try (Graph.get_node (fst tasks)) name of
wenzelm@24209
   446
      SOME task => (task_finished task, tasks)
wenzelm@23967
   447
    | NONE =>
wenzelm@23967
   448
        let
wenzelm@24175
   449
          val (current, deps, parents) = check_deps dir' name
wenzelm@23967
   450
            handle ERROR msg => cat_error msg
wenzelm@23967
   451
              (loader_msg "the error(s) above occurred while examining theory" [name] ^
wenzelm@23967
   452
                required_by "\n" initiators);
wenzelm@23967
   453
          val parent_names = map base_name parents;
wenzelm@23871
   454
wenzelm@23974
   455
          val (parents_current, (tasks_graph', tasks_len')) =
wenzelm@24175
   456
            require_thys time (name :: initiators)
wenzelm@24175
   457
              (Path.append dir (master_dir' deps)) parents tasks;
wenzelm@6211
   458
wenzelm@23967
   459
          val all_current = current andalso parents_current;
wenzelm@24186
   460
          val _ = if not all_current andalso known_thy name then outdate_thy name else ();
wenzelm@25030
   461
          val entry =
wenzelm@25030
   462
            if all_current then (deps, SOME (get_theory name))
wenzelm@25030
   463
            else (read_text deps, NONE);
wenzelm@25030
   464
          val _ = change_thys (new_deps name parent_names entry);
wenzelm@23967
   465
wenzelm@24151
   466
          val upd_time = serial ();
wenzelm@23974
   467
          val tasks_graph'' = tasks_graph' |> new_deps name parent_names
wenzelm@24209
   468
           (if all_current then Finished
wenzelm@24209
   469
            else Task (fn () => load_thy time upd_time initiators dir' name));
wenzelm@23974
   470
          val tasks_len'' = if all_current then tasks_len' else tasks_len' + 1;
wenzelm@23974
   471
        in (all_current, (tasks_graph'', tasks_len'')) end)
wenzelm@7066
   472
  end;
wenzelm@6484
   473
wenzelm@23967
   474
end;
wenzelm@23967
   475
wenzelm@23967
   476
wenzelm@24209
   477
(* use_thy etc. *)
wenzelm@23967
   478
wenzelm@23967
   479
local
wenzelm@23967
   480
wenzelm@24057
   481
fun gen_use_thy' req dir arg =
wenzelm@24209
   482
  let val (_, (tasks, n)) = req [] dir arg (Graph.empty, 0)
wenzelm@24209
   483
  in schedule_tasks tasks n end;
wenzelm@6241
   484
wenzelm@23893
   485
fun gen_use_thy req str =
wenzelm@23893
   486
  let val name = base_name str in
wenzelm@23893
   487
    check_unfinished warning name;
wenzelm@23893
   488
    gen_use_thy' req Path.current str;
wenzelm@24071
   489
    CRITICAL (fn () => ML_Context.set_context (SOME (Context.Theory (get_theory name))))
wenzelm@23893
   490
  end;
wenzelm@7244
   491
wenzelm@7211
   492
in
wenzelm@7211
   493
wenzelm@24186
   494
val use_thys_dir = gen_use_thy' (require_thys false);
wenzelm@24186
   495
val use_thys = use_thys_dir Path.current;
wenzelm@24186
   496
val use_thy = gen_use_thy (require_thy false);
wenzelm@24186
   497
val time_use_thy = gen_use_thy (require_thy true);
wenzelm@6211
   498
wenzelm@7211
   499
end;
wenzelm@7211
   500
wenzelm@6211
   501
wenzelm@6666
   502
(* remove_thy *)
wenzelm@6666
   503
wenzelm@6666
   504
fun remove_thy name =
wenzelm@7910
   505
  if is_finished name then error (loader_msg "cannot remove finished theory" [name])
wenzelm@6666
   506
  else
wenzelm@6666
   507
    let val succs = thy_graph Graph.all_succs [name] in
wenzelm@9778
   508
      priority (loader_msg "removing" succs);
wenzelm@23939
   509
      CRITICAL (fn () => (List.app (perform Remove) succs; change_thys (Graph.del_nodes succs)))
wenzelm@6666
   510
    end;
wenzelm@6666
   511
wenzelm@6666
   512
wenzelm@24563
   513
(* update_time *)
wenzelm@24563
   514
wenzelm@24563
   515
structure UpdateTime = TheoryDataFun
wenzelm@24563
   516
(
wenzelm@24563
   517
  type T = int;
wenzelm@24563
   518
  val empty = 0;
wenzelm@24563
   519
  val copy = I;
wenzelm@24563
   520
  fun extend _ = empty;
wenzelm@24563
   521
  fun merge _ _ = empty;
wenzelm@24563
   522
);
wenzelm@24563
   523
wenzelm@24563
   524
val thy_ord = int_ord o pairself UpdateTime.get;
wenzelm@24563
   525
wenzelm@24563
   526
wenzelm@7066
   527
(* begin / end theory *)
wenzelm@6211
   528
wenzelm@23900
   529
fun begin_theory name parents uses int =
wenzelm@6211
   530
  let
wenzelm@23893
   531
    val parent_names = map base_name parents;
wenzelm@23886
   532
    val dir = master_dir'' (lookup_deps name);
wenzelm@7244
   533
    val _ = check_unfinished error name;
wenzelm@24186
   534
    val _ = if int then use_thys_dir dir parents else ();
wenzelm@23893
   535
    (* FIXME tmp *)
wenzelm@24071
   536
    val _ = CRITICAL (fn () =>
wenzelm@24071
   537
      ML_Context.set_context (SOME (Context.Theory (get_theory (List.last parent_names)))));
wenzelm@17365
   538
wenzelm@24151
   539
    val theory = Theory.begin_theory name (map get_theory parent_names);
wenzelm@23900
   540
wenzelm@7952
   541
    val deps =
wenzelm@7952
   542
      if known_thy name then get_deps name
wenzelm@24068
   543
      else init_deps NONE [] parents (map #1 uses);
wenzelm@23967
   544
    val _ = change_thys (new_deps name parent_names (deps, NONE));
wenzelm@9451
   545
wenzelm@24151
   546
    val update_time = (case deps of NONE => 0 | SOME {update_time, ...} => update_time);
wenzelm@24563
   547
    val update_time = if update_time > 0 then update_time else serial ();
wenzelm@24563
   548
    val theory' = theory
wenzelm@24563
   549
      |> UpdateTime.put update_time
wenzelm@24563
   550
      |> Present.begin_theory update_time dir uses;
wenzelm@24151
   551
wenzelm@23900
   552
    val uses_now = map_filter (fn (x, true) => SOME x | _ => NONE) uses;
wenzelm@24151
   553
    val ((), theory'') =
wenzelm@24151
   554
      ML_Context.pass_context (Context.Theory theory') (List.app (load_file false)) uses_now
wenzelm@22086
   555
      ||> Context.the_theory;
wenzelm@24151
   556
  in theory'' end;
wenzelm@7244
   557
wenzelm@6211
   558
fun end_theory theory =
wenzelm@7099
   559
  let
wenzelm@16454
   560
    val name = Context.theory_name theory;
wenzelm@24190
   561
    val _ = check_files name;
wenzelm@16504
   562
    val theory' = Theory.end_theory theory;
wenzelm@23900
   563
    val _ = change_thy name (fn (deps, _) => (deps, SOME theory'));
wenzelm@23900
   564
  in theory' end;
wenzelm@6211
   565
wenzelm@6211
   566
wenzelm@24080
   567
(* register existing theories *)
wenzelm@6211
   568
wenzelm@24080
   569
fun register_thy name =
wenzelm@24080
   570
  let
wenzelm@24080
   571
    val _ = priority ("Registering theory " ^ quote name);
wenzelm@24563
   572
    val thy = get_theory name;
wenzelm@24096
   573
    val _ = map get_theory (get_parents name);
wenzelm@24096
   574
    val _ = check_unfinished error name;
wenzelm@24080
   575
    val _ = touch_thy name;
wenzelm@24190
   576
    val master = #master (ThyLoad.deps_thy Path.current name);
wenzelm@24563
   577
    val upd_time = UpdateTime.get thy;
wenzelm@24080
   578
  in
wenzelm@24080
   579
    CRITICAL (fn () =>
wenzelm@24190
   580
     (change_deps name (Option.map
wenzelm@24190
   581
       (fn {parents, files, ...} => make_deps upd_time (SOME master) [] parents files));
wenzelm@24080
   582
      perform Update name))
wenzelm@24080
   583
  end;
wenzelm@6211
   584
wenzelm@6211
   585
fun register_theory theory =
wenzelm@6211
   586
  let
wenzelm@16454
   587
    val name = Context.theory_name theory;
wenzelm@6211
   588
    val parents = Theory.parents_of theory;
wenzelm@16454
   589
    val parent_names = map Context.theory_name parents;
wenzelm@6211
   590
wenzelm@6211
   591
    fun err txt bads =
wenzelm@23871
   592
      error (loader_msg txt bads ^ "\ncannot register theory " ^ quote name);
wenzelm@6211
   593
wenzelm@6666
   594
    val nonfinished = filter_out is_finished parent_names;
wenzelm@6211
   595
    fun get_variant (x, y_name) =
skalberg@15531
   596
      if Theory.eq_thy (x, get_theory y_name) then NONE
skalberg@15531
   597
      else SOME y_name;
wenzelm@19482
   598
    val variants = map_filter get_variant (parents ~~ parent_names);
wenzelm@6211
   599
wenzelm@6211
   600
    fun register G =
skalberg@15531
   601
      (Graph.new_node (name, (NONE, SOME theory)) G
wenzelm@9327
   602
        handle Graph.DUP _ => err "duplicate theory entry" [])
wenzelm@6211
   603
      |> add_deps name parent_names;
wenzelm@6211
   604
  in
wenzelm@6666
   605
    if not (null nonfinished) then err "non-finished parent theories" nonfinished
wenzelm@6211
   606
    else if not (null variants) then err "different versions of parent theories" variants
wenzelm@23939
   607
    else CRITICAL (fn () => (change_thys register; perform Update name))
wenzelm@6211
   608
  end;
wenzelm@6211
   609
wenzelm@15801
   610
val _ = register_theory ProtoPure.thy;
wenzelm@15801
   611
wenzelm@24080
   612
wenzelm@24080
   613
(* finish all theories *)
wenzelm@24080
   614
wenzelm@24080
   615
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (NONE, entry)));
wenzelm@24080
   616
wenzelm@24080
   617
wenzelm@6211
   618
(*final declarations of this structure*)
wenzelm@6211
   619
val theory = get_theory;
wenzelm@6211
   620
val names = get_names;
wenzelm@6211
   621
wenzelm@6211
   622
end;
wenzelm@6211
   623
wenzelm@5209
   624
structure BasicThyInfo: BASIC_THY_INFO = ThyInfo;
wenzelm@5209
   625
open BasicThyInfo;