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