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