src/Pure/Thy/thy_info.ML
author wenzelm
Mon Jul 23 19:45:46 2007 +0200 (2007-07-23)
changeset 23939 e543359fe8b6
parent 23910 30e1eb0c5b2f
child 23967 92130b24e87f
permissions -rw-r--r--
marked some CRITICAL sections;
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@7099
    52
(** theory loader actions and hooks **)
wenzelm@7099
    53
wenzelm@7099
    54
datatype action = Update | Outdate | Remove;
wenzelm@7099
    55
val str_of_action = fn Update => "Update" | Outdate => "Outdate" | Remove => "Remove";
wenzelm@7099
    56
wenzelm@7099
    57
local
wenzelm@7099
    58
  val hooks = ref ([]: (action -> string -> unit) list);
wenzelm@7099
    59
in
wenzelm@23939
    60
  fun add_hook f = CRITICAL (fn () => change hooks (cons f));
skalberg@15570
    61
  fun perform action name = List.app (fn f => (try (fn () => f action name) (); ())) (! hooks);
wenzelm@7099
    62
end;
wenzelm@7099
    63
wenzelm@7099
    64
wenzelm@7099
    65
wenzelm@6211
    66
(** thy database **)
wenzelm@6211
    67
wenzelm@6211
    68
(* messages *)
wenzelm@6211
    69
wenzelm@23871
    70
fun loader_msg txt [] = "Theory loader: " ^ txt
wenzelm@23871
    71
  | loader_msg txt names = "Theory loader: " ^ txt ^ " " ^ commas_quote names;
wenzelm@6211
    72
wenzelm@6211
    73
val show_path = space_implode " via " o map quote;
wenzelm@9332
    74
fun cycle_msg names = loader_msg ("cyclic dependency of " ^ show_path names) [];
berghofe@3604
    75
berghofe@3604
    76
wenzelm@6666
    77
(* derived graph operations *)
berghofe@3604
    78
wenzelm@9327
    79
fun add_deps name parents G = Graph.add_deps_acyclic (name, parents) G
wenzelm@9332
    80
  handle Graph.CYCLES namess => error (cat_lines (map cycle_msg namess));
berghofe@3604
    81
wenzelm@7952
    82
fun upd_deps name entry G =
wenzelm@19473
    83
  fold (fn parent => Graph.del_edge (parent, name)) (Graph.imm_preds G name) G
wenzelm@7952
    84
  |> Graph.map_node name (K entry);
wenzelm@3976
    85
wenzelm@6211
    86
fun update_node name parents entry G =
wenzelm@7952
    87
  (if can (Graph.get_node G) name then upd_deps name entry G else Graph.new_node (name, entry) G)
wenzelm@7952
    88
  |> add_deps name parents;
berghofe@3604
    89
berghofe@3604
    90
wenzelm@6211
    91
(* thy database *)
wenzelm@6211
    92
wenzelm@23871
    93
type master = (Path.T * File.ident) * (Path.T * File.ident) option;
wenzelm@23871
    94
wenzelm@6211
    95
type deps =
wenzelm@23910
    96
  {outdated: bool,              (*entry considered outdated*)
wenzelm@23886
    97
    master: master option,      (*master dependencies for thy + attached ML file*)
wenzelm@23893
    98
    parents: string list,       (*source specification of parents (partially qualified)*)
wenzelm@23886
    99
    files:                      (*auxiliary files: source path, physical path + identifier*)
wenzelm@23886
   100
      (Path.T * (Path.T * File.ident) option) list};
wenzelm@23886
   101
wenzelm@23910
   102
fun make_deps outdated master parents files : deps =
wenzelm@23910
   103
  {outdated = outdated, master = master, parents = parents, files = files};
wenzelm@23886
   104
wenzelm@23893
   105
fun init_deps master parents files =
wenzelm@23910
   106
  SOME (make_deps true master parents (map (rpair NONE) files));
wenzelm@23871
   107
wenzelm@23871
   108
fun master_idents (NONE: master option) = []
wenzelm@23871
   109
  | master_idents (SOME ((_, thy_id), NONE)) = [thy_id]
wenzelm@23871
   110
  | master_idents (SOME ((_, thy_id), SOME (_, ml_id))) = [thy_id, ml_id];
wenzelm@23871
   111
wenzelm@23886
   112
fun master_dir (NONE: master option) = Path.current
wenzelm@23886
   113
  | master_dir (SOME ((path, _), _)) = Path.dir path;
wenzelm@6211
   114
wenzelm@23886
   115
fun master_dir' (d: deps option) = the_default Path.current (Option.map (master_dir o #master) d);
wenzelm@23886
   116
fun master_dir'' d = the_default Path.current (Option.map master_dir' d);
berghofe@3604
   117
wenzelm@7211
   118
wenzelm@6362
   119
type thy = deps option * theory option;
berghofe@3604
   120
wenzelm@6211
   121
local
wenzelm@6362
   122
  val database = ref (Graph.empty: thy Graph.T);
wenzelm@6211
   123
in
wenzelm@6362
   124
  fun get_thys () = ! database;
wenzelm@23939
   125
  fun change_thys f = CRITICAL (fn () => Library.change database f);
berghofe@3604
   126
end;
wenzelm@5209
   127
wenzelm@5209
   128
wenzelm@6211
   129
(* access thy graph *)
wenzelm@6211
   130
wenzelm@6211
   131
fun thy_graph f x = f (get_thys ()) x;
wenzelm@9417
   132
wenzelm@9417
   133
(*theory names in topological order*)
wenzelm@9417
   134
fun get_names () =
wenzelm@9417
   135
  let val G = get_thys ()
wenzelm@14774
   136
  in Graph.all_succs G (Graph.minimals G) end;
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@9327
   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@23886
   313
      if really then ThyLoad.load_thy dir name ml time
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@6211
   329
(* require_thy *)
wenzelm@6211
   330
wenzelm@23871
   331
fun base_name s = Path.implode (Path.base (Path.explode s));
berghofe@15065
   332
wenzelm@7211
   333
local
wenzelm@6211
   334
wenzelm@23886
   335
fun check_ml master (src_path, info) =
wenzelm@23871
   336
  let val info' =
wenzelm@23871
   337
    (case info of NONE => NONE
wenzelm@23871
   338
    | SOME (_, id) =>
wenzelm@23886
   339
        (case ThyLoad.check_ml (master_dir master) src_path of NONE => NONE
wenzelm@23871
   340
        | SOME (path', id') => if id <> id' then NONE else SOME (path', id')))
wenzelm@23886
   341
  in (src_path, info') end;
wenzelm@6211
   342
wenzelm@23886
   343
fun check_deps ml strict dir name =
wenzelm@6211
   344
  (case lookup_deps name of
wenzelm@23893
   345
    SOME NONE => (true, NONE, get_parents name)
wenzelm@23893
   346
  | NONE =>
wenzelm@23893
   347
      let val (master, (parents, files)) = ThyLoad.deps_thy dir name ml
wenzelm@23893
   348
      in (false, init_deps (SOME master) parents files, parents) end
wenzelm@23910
   349
  | SOME (deps as SOME {outdated, master, parents, files}) =>
wenzelm@23910
   350
      if not strict andalso can get_theory name then (true, deps, parents)
wenzelm@23871
   351
      else
wenzelm@23893
   352
        let val master' = SOME (ThyLoad.check_thy dir name ml) in
wenzelm@23871
   353
          if master_idents master <> master_idents master'
wenzelm@23893
   354
          then
wenzelm@23893
   355
            let val (parents', files') = #2 (ThyLoad.deps_thy dir name ml)
wenzelm@23893
   356
            in (false, init_deps master' parents' files', parents') end
wenzelm@23871
   357
          else
wenzelm@23871
   358
            let
wenzelm@23871
   359
              val checked_files = map (check_ml master') files;
wenzelm@23871
   360
              val current = not outdated andalso forall (is_some o snd) checked_files;
wenzelm@23910
   361
              val deps' = SOME (make_deps (not current) master' parents checked_files);
wenzelm@23893
   362
            in (current, deps', parents) end
wenzelm@23871
   363
        end);
wenzelm@6211
   364
wenzelm@23893
   365
fun require_thys really ml time strict keep_strict initiators dir strs visited =
wenzelm@23893
   366
  fold_map (require_thy really ml time strict keep_strict initiators dir) strs visited
wenzelm@23893
   367
and require_thy really ml time strict keep_strict initiators dir str visited =
wenzelm@6211
   368
  let
wenzelm@21858
   369
    val path = Path.expand (Path.explode str);
wenzelm@21858
   370
    val name = Path.implode (Path.base path);
wenzelm@23893
   371
    val dir' = Path.append dir (Path.dir path);
wenzelm@23871
   372
    val _ = member (op =) initiators name andalso error (cycle_msg initiators);
wenzelm@7066
   373
  in
wenzelm@23871
   374
    if known_thy name andalso is_finished name orelse member (op =) visited name
wenzelm@23893
   375
    then (true, visited)
wenzelm@7066
   376
    else
wenzelm@7066
   377
      let
wenzelm@23893
   378
        val (current, deps, parents) = check_deps ml strict dir' name
wenzelm@18678
   379
          handle ERROR msg => cat_error msg
wenzelm@18678
   380
            (loader_msg "the error(s) above occurred while examining theory" [name] ^
wenzelm@23893
   381
              required_by "\n" initiators);
wenzelm@23871
   382
wenzelm@23893
   383
        val (parents_current, visited') =
wenzelm@23893
   384
          require_thys true true time (strict andalso keep_strict) keep_strict
wenzelm@23893
   385
            (name :: initiators) (Path.append dir (master_dir' deps)) parents (name :: visited);
wenzelm@6211
   386
wenzelm@23893
   387
        val all_current = current andalso forall I parents_current;
wenzelm@23871
   388
        val thy = if all_current orelse not really then SOME (get_theory name) else NONE;
wenzelm@23871
   389
        val _ = change_thys (update_node name (map base_name parents) (deps, thy));
wenzelm@23871
   390
        val _ =
wenzelm@23871
   391
          if all_current then ()
wenzelm@23893
   392
          else load_thy really ml (time orelse ! Output.timing) initiators dir' name;
wenzelm@23893
   393
      in (all_current, visited') end
wenzelm@7066
   394
  end;
wenzelm@6484
   395
wenzelm@23893
   396
fun gen_use_thy' req dir arg =
wenzelm@23893
   397
  Output.toplevel_errors (fn () => (req [] dir arg []; ())) ();
wenzelm@6241
   398
wenzelm@23893
   399
fun gen_use_thy req str =
wenzelm@23893
   400
  let val name = base_name str in
wenzelm@23893
   401
    check_unfinished warning name;
wenzelm@23893
   402
    gen_use_thy' req Path.current str;
wenzelm@23893
   403
    ML_Context.set_context (SOME (Context.Theory (get_theory name)))
wenzelm@23893
   404
  end;
wenzelm@7244
   405
wenzelm@7211
   406
in
wenzelm@7211
   407
wenzelm@23893
   408
val quiet_update_thys    = gen_use_thy' (require_thys true true false true true);
wenzelm@23893
   409
val pretend_use_thy_only = gen_use_thy' (require_thy false false false true false) Path.current;
wenzelm@23910
   410
val use_thys             = gen_use_thy' (require_thys true true false true false) Path.current;
wenzelm@7244
   411
wenzelm@23893
   412
val use_thy              = gen_use_thy (require_thy true true false true false);
wenzelm@23893
   413
val time_use_thy         = gen_use_thy (require_thy true true true true false);
wenzelm@23893
   414
val update_thy           = gen_use_thy (require_thy true true false true true);
wenzelm@6211
   415
wenzelm@7211
   416
end;
wenzelm@7211
   417
wenzelm@6211
   418
wenzelm@6666
   419
(* remove_thy *)
wenzelm@6666
   420
wenzelm@6666
   421
fun remove_thy name =
wenzelm@7910
   422
  if is_finished name then error (loader_msg "cannot remove finished theory" [name])
wenzelm@6666
   423
  else
wenzelm@6666
   424
    let val succs = thy_graph Graph.all_succs [name] in
wenzelm@9778
   425
      priority (loader_msg "removing" succs);
wenzelm@23939
   426
      CRITICAL (fn () => (List.app (perform Remove) succs; change_thys (Graph.del_nodes succs)))
wenzelm@6666
   427
    end;
wenzelm@6666
   428
wenzelm@6666
   429
wenzelm@7066
   430
(* begin / end theory *)
wenzelm@6211
   431
wenzelm@17365
   432
fun check_uses name uses =
wenzelm@23871
   433
  let val illegal = map (fn ext => Path.ext ext (Path.basic name)) ("" :: ThyLoad.ml_exts) in
wenzelm@17365
   434
    (case find_first (member (op =) illegal o Path.base o Path.expand o #1) uses of
wenzelm@17365
   435
      NONE => ()
wenzelm@21858
   436
    | SOME (path, _) => error ("Illegal use of theory ML file: " ^ quote (Path.implode path)))
wenzelm@17365
   437
  end;
wenzelm@17365
   438
wenzelm@23900
   439
fun begin_theory name parents uses int =
wenzelm@6211
   440
  let
wenzelm@23893
   441
    val parent_names = map base_name parents;
wenzelm@23886
   442
    val dir = master_dir'' (lookup_deps name);
wenzelm@7244
   443
    val _ = check_unfinished error name;
wenzelm@23893
   444
    val _ = if int then quiet_update_thys dir parents else ();
wenzelm@23893
   445
    (* FIXME tmp *)
wenzelm@23893
   446
    val _ = ML_Context.set_context (SOME (Context.Theory (get_theory (List.last parent_names))));
wenzelm@17365
   447
    val _ = check_uses name uses;
wenzelm@17365
   448
wenzelm@23900
   449
    val theory =
wenzelm@23900
   450
      Theory.begin_theory name (map get_theory parent_names)
wenzelm@23900
   451
      |> Present.begin_theory dir uses;
wenzelm@23900
   452
wenzelm@7952
   453
    val deps =
wenzelm@7952
   454
      if known_thy name then get_deps name
wenzelm@23893
   455
      else init_deps NONE parents (map #1 uses);
wenzelm@23900
   456
    val _ = change_thys (update_node name parent_names (deps, NONE));
wenzelm@9451
   457
wenzelm@23900
   458
    val uses_now = map_filter (fn (x, true) => SOME x | _ => NONE) uses;
wenzelm@23900
   459
    val ((), theory') =
wenzelm@23900
   460
      ML_Context.pass_context (Context.Theory theory) (List.app (load_file false)) uses_now
wenzelm@22086
   461
      ||> Context.the_theory;
wenzelm@23900
   462
  in theory' end;
wenzelm@7244
   463
wenzelm@6211
   464
fun end_theory theory =
wenzelm@7099
   465
  let
wenzelm@16454
   466
    val name = Context.theory_name theory;
wenzelm@16504
   467
    val theory' = Theory.end_theory theory;
wenzelm@23900
   468
    val _ = change_thy name (fn (deps, _) => (deps, SOME theory'));
wenzelm@23900
   469
  in theory' end;
wenzelm@6211
   470
wenzelm@6211
   471
wenzelm@6666
   472
(* finish all theories *)
wenzelm@6211
   473
skalberg@15531
   474
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (NONE, entry)));
wenzelm@6211
   475
wenzelm@6211
   476
wenzelm@6211
   477
(* register existing theories *)
wenzelm@6211
   478
wenzelm@6211
   479
fun register_theory theory =
wenzelm@6211
   480
  let
wenzelm@16454
   481
    val name = Context.theory_name theory;
wenzelm@6211
   482
    val parents = Theory.parents_of theory;
wenzelm@16454
   483
    val parent_names = map Context.theory_name parents;
wenzelm@6211
   484
wenzelm@6211
   485
    fun err txt bads =
wenzelm@23871
   486
      error (loader_msg txt bads ^ "\ncannot register theory " ^ quote name);
wenzelm@6211
   487
wenzelm@6666
   488
    val nonfinished = filter_out is_finished parent_names;
wenzelm@6211
   489
    fun get_variant (x, y_name) =
skalberg@15531
   490
      if Theory.eq_thy (x, get_theory y_name) then NONE
skalberg@15531
   491
      else SOME y_name;
wenzelm@19482
   492
    val variants = map_filter get_variant (parents ~~ parent_names);
wenzelm@6211
   493
wenzelm@6211
   494
    fun register G =
skalberg@15531
   495
      (Graph.new_node (name, (NONE, SOME theory)) G
wenzelm@9327
   496
        handle Graph.DUP _ => err "duplicate theory entry" [])
wenzelm@6211
   497
      |> add_deps name parent_names;
wenzelm@6211
   498
  in
wenzelm@6666
   499
    if not (null nonfinished) then err "non-finished parent theories" nonfinished
wenzelm@6211
   500
    else if not (null variants) then err "different versions of parent theories" variants
wenzelm@23939
   501
    else CRITICAL (fn () => (change_thys register; perform Update name))
wenzelm@6211
   502
  end;
wenzelm@6211
   503
wenzelm@15801
   504
val _ = register_theory ProtoPure.thy;
wenzelm@15801
   505
wenzelm@6211
   506
(*final declarations of this structure*)
wenzelm@6211
   507
val theory = get_theory;
wenzelm@6211
   508
val names = get_names;
wenzelm@6211
   509
wenzelm@6211
   510
end;
wenzelm@6211
   511
wenzelm@5209
   512
structure BasicThyInfo: BASIC_THY_INFO = ThyInfo;
wenzelm@5209
   513
open BasicThyInfo;