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