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