src/Pure/Thy/thy_info.ML
author wenzelm
Sun Jul 22 21:20:55 2007 +0200 (2007-07-22)
changeset 23910 30e1eb0c5b2f
parent 23900 b25b1444a246
child 23939 e543359fe8b6
permissions -rw-r--r--
added simultaneous use_thys;
deps: removed obsolete present'' flag;
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@7099
    60
  fun add_hook f = hooks := f :: ! hooks;
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@9137
   125
  val change_thys = Library.change database;
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@6211
   153
fun change_thy name f = (get_thy name; change_thys (Graph.map_node name f));
wenzelm@6211
   154
wenzelm@6211
   155
wenzelm@6211
   156
(* access deps *)
wenzelm@6211
   157
skalberg@15570
   158
val lookup_deps = Option.map #1 o lookup_thy;
wenzelm@6211
   159
val get_deps = #1 o get_thy;
wenzelm@6211
   160
fun change_deps name f = change_thy name (fn (deps, x) => (f deps, x));
wenzelm@6211
   161
wenzelm@6666
   162
fun is_finished name = is_none (get_deps name);
skalberg@15531
   163
fun get_files name = (case get_deps name of SOME {files, ...} => files | _ => []);
wenzelm@7191
   164
wenzelm@7191
   165
fun loaded_files name =
wenzelm@7191
   166
  (case get_deps name of
skalberg@15531
   167
    NONE => []
skalberg@15531
   168
  | SOME {master, files, ...} =>
wenzelm@23871
   169
      (case master of SOME ((thy_path, _), _) => [thy_path] | NONE => []) @
wenzelm@19482
   170
      (map_filter (Option.map #1 o #2) files));
wenzelm@6211
   171
wenzelm@23871
   172
fun get_parents name =
wenzelm@9327
   173
  (thy_graph Graph.imm_preds name) handle Graph.UNDEF _ =>
berghofe@6654
   174
    error (loader_msg "nothing known about theory" [name]);
berghofe@6654
   175
wenzelm@6211
   176
wenzelm@16047
   177
(* pretty printing a theory *)
wenzelm@16047
   178
wenzelm@16047
   179
local
paulson@15633
   180
wenzelm@16047
   181
fun relevant_names names =
wenzelm@16047
   182
  let
wenzelm@16047
   183
    val (finished, unfinished) =
wenzelm@19305
   184
      List.filter (fn name => name = Context.draftN orelse known_thy name) names
wenzelm@19305
   185
      |> List.partition (fn name => name <> Context.draftN andalso is_finished name);
wenzelm@16047
   186
  in (if not (null finished) then [List.last finished] else []) @ unfinished end;
paulson@15633
   187
wenzelm@16047
   188
in
paulson@15633
   189
wenzelm@16454
   190
fun pretty_theory thy =
wenzelm@16454
   191
  Pretty.str_list "{" "}" (relevant_names (Context.names_of thy));
paulson@15633
   192
wenzelm@16047
   193
end;
wenzelm@16047
   194
paulson@15633
   195
wenzelm@6211
   196
(* access theory *)
wenzelm@6211
   197
wenzelm@7687
   198
fun lookup_theory name =
wenzelm@7687
   199
  (case lookup_thy name of
skalberg@15531
   200
    SOME (_, SOME thy) => SOME thy
skalberg@15531
   201
  | _ => NONE);
wenzelm@7288
   202
wenzelm@6211
   203
fun get_theory name =
wenzelm@7288
   204
  (case lookup_theory name of
wenzelm@23871
   205
    SOME theory => theory
wenzelm@6211
   206
  | _ => error (loader_msg "undefined theory entry for" [name]));
wenzelm@6211
   207
wenzelm@19547
   208
fun the_theory name thy =
wenzelm@19547
   209
  if Context.theory_name thy = name then thy
wenzelm@19547
   210
  else get_theory name;
wenzelm@19547
   211
wenzelm@22137
   212
val _ = ML_Context.value_antiq "theory"
wenzelm@22137
   213
  (Scan.lift Args.name
wenzelm@22137
   214
    >> (fn name => (name ^ "_thy", "ThyInfo.theory " ^ ML_Syntax.print_string name))
wenzelm@22148
   215
  || Scan.succeed ("thy", "ML_Context.the_context ()"));
wenzelm@22137
   216
wenzelm@6211
   217
wenzelm@6211
   218
wenzelm@6211
   219
(** thy operations **)
wenzelm@6211
   220
wenzelm@6241
   221
(* maintain 'outdated' flag *)
wenzelm@6211
   222
wenzelm@7099
   223
local
wenzelm@7099
   224
wenzelm@6241
   225
fun is_outdated name =
wenzelm@6241
   226
  (case lookup_deps name of
skalberg@15531
   227
    SOME (SOME {outdated, ...}) => outdated
wenzelm@6241
   228
  | _ => false);
wenzelm@6211
   229
wenzelm@6241
   230
fun outdate_thy name =
wenzelm@7099
   231
  if is_finished name orelse is_outdated name then ()
wenzelm@23910
   232
  else (change_deps name (Option.map (fn {outdated = _, master, parents, files} =>
wenzelm@23910
   233
    make_deps true master parents files)); perform Outdate name);
wenzelm@7099
   234
wenzelm@7910
   235
fun check_unfinished name =
skalberg@15531
   236
  if is_finished name then (warning (loader_msg "tried to touch finished theory" [name]); NONE)
skalberg@15531
   237
  else SOME name;
wenzelm@7910
   238
wenzelm@7099
   239
in
wenzelm@6211
   240
wenzelm@7910
   241
fun touch_thys names =
wenzelm@19482
   242
  List.app outdate_thy (thy_graph Graph.all_succs (map_filter check_unfinished names));
wenzelm@7910
   243
wenzelm@7910
   244
fun touch_thy name = touch_thys [name];
wenzelm@7910
   245
fun touch_child_thys name = touch_thys (thy_graph Graph.imm_succs name);
wenzelm@6211
   246
skalberg@15570
   247
fun touch_all_thys () = List.app outdate_thy (get_names ());
wenzelm@6211
   248
wenzelm@7099
   249
end;
wenzelm@6211
   250
wenzelm@6211
   251
wenzelm@7244
   252
(* check 'finished' state *)
wenzelm@7244
   253
wenzelm@7244
   254
fun check_unfinished fail name =
wenzelm@7910
   255
  if known_thy name andalso is_finished name then
wenzelm@7288
   256
    fail (loader_msg "cannot update finished theory" [name])
wenzelm@7244
   257
  else ();
wenzelm@7244
   258
wenzelm@7244
   259
wenzelm@7941
   260
(* load_file *)
wenzelm@6211
   261
wenzelm@7191
   262
local
wenzelm@7191
   263
wenzelm@23910
   264
fun provide path name info (deps as SOME {outdated, master, parents, files}) =
wenzelm@23871
   265
     (if AList.defined (op =) files path then ()
wenzelm@7941
   266
      else warning (loader_msg "undeclared dependency of theory" [name] ^
wenzelm@21858
   267
        " on file: " ^ quote (Path.implode path));
wenzelm@23910
   268
      SOME (make_deps outdated master parents (AList.update (op =) (path, SOME info) files)))
skalberg@15531
   269
  | provide _ _ _ NONE = NONE;
wenzelm@7941
   270
wenzelm@7941
   271
fun run_file path =
wenzelm@22095
   272
  (case Option.map (Context.theory_name o Context.the_theory) (ML_Context.get_context ()) of
wenzelm@23886
   273
    NONE => (ThyLoad.load_ml Path.current path; ())
wenzelm@23871
   274
  | SOME name =>
wenzelm@23871
   275
      (case lookup_deps name of
wenzelm@23871
   276
        SOME deps =>
wenzelm@23886
   277
          change_deps name (provide path name (ThyLoad.load_ml (master_dir' deps) path))
wenzelm@23886
   278
      | NONE => (ThyLoad.load_ml Path.current path; ())));
wenzelm@6211
   279
wenzelm@7191
   280
in
wenzelm@7191
   281
wenzelm@18721
   282
fun load_file time path = Output.toplevel_errors (fn () =>
wenzelm@7941
   283
  if time then
wenzelm@21858
   284
    let val name = Path.implode path in
wenzelm@7244
   285
      timeit (fn () =>
wenzelm@9778
   286
       (priority ("\n**** Starting file " ^ quote name ^ " ****");
wenzelm@9036
   287
        run_file path;
wenzelm@9778
   288
        priority ("**** Finished file " ^ quote name ^ " ****\n")))
wenzelm@7244
   289
    end
wenzelm@18721
   290
  else run_file path) ();
wenzelm@7941
   291
wenzelm@21858
   292
val use = load_file false o Path.explode;
wenzelm@21858
   293
val time_use = load_file true o Path.explode;
wenzelm@7191
   294
wenzelm@7191
   295
end;
wenzelm@6233
   296
wenzelm@6233
   297
wenzelm@7099
   298
(* load_thy *)
wenzelm@7099
   299
wenzelm@9490
   300
fun required_by _ [] = ""
wenzelm@9490
   301
  | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
wenzelm@7099
   302
wenzelm@23886
   303
fun load_thy really ml time initiators dir name =
wenzelm@7099
   304
  let
wenzelm@9822
   305
    val _ =
wenzelm@9822
   306
      if really then priority ("Loading theory " ^ quote name ^ required_by " " initiators)
wenzelm@9822
   307
      else priority ("Registering theory " ^ quote name);
wenzelm@7099
   308
wenzelm@7099
   309
    val _ = touch_thy name;
wenzelm@9822
   310
    val master =
wenzelm@23886
   311
      if really then ThyLoad.load_thy dir name ml time
wenzelm@23886
   312
      else #1 (ThyLoad.deps_thy dir name ml);
wenzelm@7099
   313
wenzelm@7099
   314
    val files = get_files name;
wenzelm@21858
   315
    val missing_files = map_filter (fn (path, NONE) => SOME (Path.implode path) | _ => NONE) files;
wenzelm@7099
   316
  in
wenzelm@7099
   317
    if null missing_files then ()
wenzelm@7099
   318
    else warning (loader_msg "unresolved dependencies of theory" [name] ^
wenzelm@7223
   319
      " on file(s): " ^ commas_quote missing_files);
wenzelm@23893
   320
    change_deps name
wenzelm@23910
   321
      (Option.map (fn {parents, ...} => make_deps false (SOME master) parents files));
wenzelm@7099
   322
    perform Update name
wenzelm@7099
   323
  end;
wenzelm@7099
   324
wenzelm@7099
   325
wenzelm@6211
   326
(* require_thy *)
wenzelm@6211
   327
wenzelm@23871
   328
fun base_name s = Path.implode (Path.base (Path.explode s));
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@23886
   340
fun check_deps ml strict 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@23893
   344
      let val (master, (parents, files)) = ThyLoad.deps_thy dir name ml
wenzelm@23893
   345
      in (false, init_deps (SOME master) parents files, parents) end
wenzelm@23910
   346
  | SOME (deps as SOME {outdated, master, parents, files}) =>
wenzelm@23910
   347
      if not strict andalso can get_theory name then (true, deps, parents)
wenzelm@23871
   348
      else
wenzelm@23893
   349
        let val master' = SOME (ThyLoad.check_thy dir name ml) in
wenzelm@23871
   350
          if master_idents master <> master_idents master'
wenzelm@23893
   351
          then
wenzelm@23893
   352
            let val (parents', files') = #2 (ThyLoad.deps_thy dir name ml)
wenzelm@23893
   353
            in (false, init_deps master' parents' files', parents') end
wenzelm@23871
   354
          else
wenzelm@23871
   355
            let
wenzelm@23871
   356
              val checked_files = map (check_ml master') files;
wenzelm@23871
   357
              val current = not outdated andalso forall (is_some o snd) checked_files;
wenzelm@23910
   358
              val deps' = SOME (make_deps (not current) master' parents checked_files);
wenzelm@23893
   359
            in (current, deps', parents) end
wenzelm@23871
   360
        end);
wenzelm@6211
   361
wenzelm@23893
   362
fun require_thys really ml time strict keep_strict initiators dir strs visited =
wenzelm@23893
   363
  fold_map (require_thy really ml time strict keep_strict initiators dir) strs visited
wenzelm@23893
   364
and require_thy really ml time strict keep_strict initiators dir str visited =
wenzelm@6211
   365
  let
wenzelm@21858
   366
    val path = Path.expand (Path.explode str);
wenzelm@21858
   367
    val name = Path.implode (Path.base path);
wenzelm@23893
   368
    val dir' = Path.append dir (Path.dir path);
wenzelm@23871
   369
    val _ = member (op =) initiators name andalso error (cycle_msg initiators);
wenzelm@7066
   370
  in
wenzelm@23871
   371
    if known_thy name andalso is_finished name orelse member (op =) visited name
wenzelm@23893
   372
    then (true, visited)
wenzelm@7066
   373
    else
wenzelm@7066
   374
      let
wenzelm@23893
   375
        val (current, deps, parents) = check_deps ml strict dir' name
wenzelm@18678
   376
          handle ERROR msg => cat_error msg
wenzelm@18678
   377
            (loader_msg "the error(s) above occurred while examining theory" [name] ^
wenzelm@23893
   378
              required_by "\n" initiators);
wenzelm@23871
   379
wenzelm@23893
   380
        val (parents_current, visited') =
wenzelm@23893
   381
          require_thys true true time (strict andalso keep_strict) keep_strict
wenzelm@23893
   382
            (name :: initiators) (Path.append dir (master_dir' deps)) parents (name :: visited);
wenzelm@6211
   383
wenzelm@23893
   384
        val all_current = current andalso forall I parents_current;
wenzelm@23871
   385
        val thy = if all_current orelse not really then SOME (get_theory name) else NONE;
wenzelm@23871
   386
        val _ = change_thys (update_node name (map base_name parents) (deps, thy));
wenzelm@23871
   387
        val _ =
wenzelm@23871
   388
          if all_current then ()
wenzelm@23893
   389
          else load_thy really ml (time orelse ! Output.timing) initiators dir' name;
wenzelm@23893
   390
      in (all_current, visited') end
wenzelm@7066
   391
  end;
wenzelm@6484
   392
wenzelm@23893
   393
fun gen_use_thy' req dir arg =
wenzelm@23893
   394
  Output.toplevel_errors (fn () => (req [] dir arg []; ())) ();
wenzelm@6241
   395
wenzelm@23893
   396
fun gen_use_thy req str =
wenzelm@23893
   397
  let val name = base_name str in
wenzelm@23893
   398
    check_unfinished warning name;
wenzelm@23893
   399
    gen_use_thy' req Path.current str;
wenzelm@23893
   400
    ML_Context.set_context (SOME (Context.Theory (get_theory name)))
wenzelm@23893
   401
  end;
wenzelm@7244
   402
wenzelm@7211
   403
in
wenzelm@7211
   404
wenzelm@23893
   405
val quiet_update_thys    = gen_use_thy' (require_thys true true false true true);
wenzelm@23893
   406
val pretend_use_thy_only = gen_use_thy' (require_thy false false false true false) Path.current;
wenzelm@23910
   407
val use_thys             = gen_use_thy' (require_thys true true false true false) Path.current;
wenzelm@7244
   408
wenzelm@23893
   409
val use_thy              = gen_use_thy (require_thy true true false true false);
wenzelm@23893
   410
val time_use_thy         = gen_use_thy (require_thy true true true true false);
wenzelm@23893
   411
val update_thy           = gen_use_thy (require_thy true true false true true);
wenzelm@6211
   412
wenzelm@7211
   413
end;
wenzelm@7211
   414
wenzelm@6211
   415
wenzelm@6666
   416
(* remove_thy *)
wenzelm@6666
   417
wenzelm@6666
   418
fun remove_thy name =
wenzelm@7910
   419
  if is_finished name then error (loader_msg "cannot remove finished theory" [name])
wenzelm@6666
   420
  else
wenzelm@6666
   421
    let val succs = thy_graph Graph.all_succs [name] in
wenzelm@9778
   422
      priority (loader_msg "removing" succs);
skalberg@15570
   423
      List.app (perform Remove) succs;
wenzelm@7191
   424
      change_thys (Graph.del_nodes succs)
wenzelm@6666
   425
    end;
wenzelm@6666
   426
wenzelm@6666
   427
wenzelm@7066
   428
(* begin / end theory *)
wenzelm@6211
   429
wenzelm@17365
   430
fun check_uses name uses =
wenzelm@23871
   431
  let val illegal = map (fn ext => Path.ext ext (Path.basic name)) ("" :: ThyLoad.ml_exts) in
wenzelm@17365
   432
    (case find_first (member (op =) illegal o Path.base o Path.expand o #1) uses of
wenzelm@17365
   433
      NONE => ()
wenzelm@21858
   434
    | SOME (path, _) => error ("Illegal use of theory ML file: " ^ quote (Path.implode path)))
wenzelm@17365
   435
  end;
wenzelm@17365
   436
wenzelm@23900
   437
fun begin_theory name parents uses int =
wenzelm@6211
   438
  let
wenzelm@23893
   439
    val parent_names = map base_name parents;
wenzelm@23886
   440
    val dir = master_dir'' (lookup_deps name);
wenzelm@7244
   441
    val _ = check_unfinished error name;
wenzelm@23893
   442
    val _ = if int then quiet_update_thys dir parents else ();
wenzelm@23893
   443
    (* FIXME tmp *)
wenzelm@23893
   444
    val _ = ML_Context.set_context (SOME (Context.Theory (get_theory (List.last parent_names))));
wenzelm@17365
   445
    val _ = check_uses name uses;
wenzelm@17365
   446
wenzelm@23900
   447
    val theory =
wenzelm@23900
   448
      Theory.begin_theory name (map get_theory parent_names)
wenzelm@23900
   449
      |> Present.begin_theory dir uses;
wenzelm@23900
   450
wenzelm@7952
   451
    val deps =
wenzelm@7952
   452
      if known_thy name then get_deps name
wenzelm@23893
   453
      else init_deps NONE parents (map #1 uses);
wenzelm@23900
   454
    val _ = change_thys (update_node name parent_names (deps, NONE));
wenzelm@9451
   455
wenzelm@23900
   456
    val uses_now = map_filter (fn (x, true) => SOME x | _ => NONE) uses;
wenzelm@23900
   457
    val ((), theory') =
wenzelm@23900
   458
      ML_Context.pass_context (Context.Theory theory) (List.app (load_file false)) uses_now
wenzelm@22086
   459
      ||> Context.the_theory;
wenzelm@23900
   460
  in theory' end;
wenzelm@7244
   461
wenzelm@6211
   462
fun end_theory theory =
wenzelm@7099
   463
  let
wenzelm@16454
   464
    val name = Context.theory_name theory;
wenzelm@16504
   465
    val theory' = Theory.end_theory theory;
wenzelm@23900
   466
    val _ = change_thy name (fn (deps, _) => (deps, SOME theory'));
wenzelm@23900
   467
  in theory' end;
wenzelm@6211
   468
wenzelm@6211
   469
wenzelm@6666
   470
(* finish all theories *)
wenzelm@6211
   471
skalberg@15531
   472
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (NONE, entry)));
wenzelm@6211
   473
wenzelm@6211
   474
wenzelm@6211
   475
(* register existing theories *)
wenzelm@6211
   476
wenzelm@6211
   477
fun register_theory theory =
wenzelm@6211
   478
  let
wenzelm@16454
   479
    val name = Context.theory_name theory;
wenzelm@6211
   480
    val parents = Theory.parents_of theory;
wenzelm@16454
   481
    val parent_names = map Context.theory_name parents;
wenzelm@6211
   482
wenzelm@6211
   483
    fun err txt bads =
wenzelm@23871
   484
      error (loader_msg txt bads ^ "\ncannot register theory " ^ quote name);
wenzelm@6211
   485
wenzelm@6666
   486
    val nonfinished = filter_out is_finished parent_names;
wenzelm@6211
   487
    fun get_variant (x, y_name) =
skalberg@15531
   488
      if Theory.eq_thy (x, get_theory y_name) then NONE
skalberg@15531
   489
      else SOME y_name;
wenzelm@19482
   490
    val variants = map_filter get_variant (parents ~~ parent_names);
wenzelm@6211
   491
wenzelm@6211
   492
    fun register G =
skalberg@15531
   493
      (Graph.new_node (name, (NONE, SOME theory)) G
wenzelm@9327
   494
        handle Graph.DUP _ => err "duplicate theory entry" [])
wenzelm@6211
   495
      |> add_deps name parent_names;
wenzelm@6211
   496
  in
wenzelm@6666
   497
    if not (null nonfinished) then err "non-finished parent theories" nonfinished
wenzelm@6211
   498
    else if not (null variants) then err "different versions of parent theories" variants
wenzelm@7099
   499
    else (change_thys register; perform Update name)
wenzelm@6211
   500
  end;
wenzelm@6211
   501
wenzelm@15801
   502
val _ = register_theory ProtoPure.thy;
wenzelm@15801
   503
wenzelm@6211
   504
(*final declarations of this structure*)
wenzelm@6211
   505
val theory = get_theory;
wenzelm@6211
   506
val names = get_names;
wenzelm@6211
   507
wenzelm@6211
   508
end;
wenzelm@6211
   509
wenzelm@5209
   510
structure BasicThyInfo: BASIC_THY_INFO = ThyInfo;
wenzelm@5209
   511
open BasicThyInfo;