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