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