src/Pure/Thy/thy_info.ML
author wenzelm
Mon May 23 14:56:37 2005 +0200 (2005-05-23)
changeset 16047 b2bf9a5cde37
parent 15801 d2f5ca3c048d
child 16454 af39c6510b86
permissions -rw-r--r--
tuned pretty_sg;
tuned;
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@6211
    12
  val theory_of_sign: Sign.sg -> theory
wenzelm@6211
    13
  val theory_of_thm: thm -> theory
wenzelm@6241
    14
(*val use: string -> unit*)             (*exported later*)
wenzelm@6241
    15
  val time_use: string -> unit
wenzelm@6211
    16
  val touch_thy: string -> unit
wenzelm@6211
    17
  val use_thy: string -> unit
wenzelm@6211
    18
  val update_thy: string -> unit
wenzelm@6666
    19
  val remove_thy: string -> unit
wenzelm@6211
    20
  val time_use_thy: string -> unit
wenzelm@6527
    21
  val use_thy_only: string -> unit
wenzelm@7099
    22
  val update_thy_only: string -> unit
wenzelm@6211
    23
end;
wenzelm@5209
    24
berghofe@3604
    25
signature THY_INFO =
berghofe@3604
    26
sig
wenzelm@5209
    27
  include BASIC_THY_INFO
wenzelm@7099
    28
  datatype action = Update | Outdate | Remove
wenzelm@7099
    29
  val str_of_action: action -> string
wenzelm@7099
    30
  val add_hook: (action -> string -> unit) -> unit
wenzelm@6211
    31
  val names: unit -> string list
wenzelm@7910
    32
  val known_thy: string -> bool
wenzelm@7935
    33
  val check_known_thy: string -> bool
wenzelm@7935
    34
  val if_known_thy: (string -> unit) -> string -> unit
wenzelm@7288
    35
  val lookup_theory: string -> theory option
wenzelm@6211
    36
  val get_theory: string -> theory
berghofe@6654
    37
  val get_preds: string -> string list
wenzelm@7935
    38
  val loaded_files: string -> Path.T list
wenzelm@7910
    39
  val touch_child_thys: string -> unit
wenzelm@7099
    40
  val touch_all_thys: unit -> unit
wenzelm@7941
    41
  val load_file: bool -> Path.T -> unit
wenzelm@6241
    42
  val use: string -> unit
wenzelm@7952
    43
  val quiet_update_thy: bool -> string -> unit
wenzelm@9822
    44
  val pretend_use_thy_only: string -> unit
berghofe@15158
    45
  val begin_theory: (Path.T option -> string -> string list ->
berghofe@15158
    46
      (Path.T * bool) list -> theory -> theory) ->
wenzelm@8805
    47
    bool -> string -> string list -> (Path.T * bool) list -> theory
wenzelm@6211
    48
  val end_theory: theory -> theory
wenzelm@6666
    49
  val finish: unit -> unit
wenzelm@6211
    50
  val register_theory: theory -> unit
paulson@15633
    51
  val pretty_theory: theory -> Pretty.T
paulson@15633
    52
  val pprint_theory: theory -> pprint_args -> unit
berghofe@3604
    53
end;
berghofe@3604
    54
wenzelm@6362
    55
structure ThyInfo: THY_INFO =
wenzelm@6211
    56
struct
berghofe@3604
    57
berghofe@3604
    58
wenzelm@7099
    59
(** theory loader actions and hooks **)
wenzelm@7099
    60
wenzelm@7099
    61
datatype action = Update | Outdate | Remove;
wenzelm@7099
    62
val str_of_action = fn Update => "Update" | Outdate => "Outdate" | Remove => "Remove";
wenzelm@7099
    63
wenzelm@7099
    64
local
wenzelm@7099
    65
  val hooks = ref ([]: (action -> string -> unit) list);
wenzelm@7099
    66
in
wenzelm@7099
    67
  fun add_hook f = hooks := f :: ! hooks;
skalberg@15570
    68
  fun perform action name = List.app (fn f => (try (fn () => f action name) (); ())) (! hooks);
wenzelm@7099
    69
end;
wenzelm@7099
    70
wenzelm@7099
    71
wenzelm@7099
    72
wenzelm@6211
    73
(** thy database **)
wenzelm@6211
    74
wenzelm@6211
    75
(* messages *)
wenzelm@6211
    76
wenzelm@6211
    77
fun gen_msg txt [] = txt
wenzelm@6211
    78
  | gen_msg txt names = txt ^ " " ^ commas_quote names;
wenzelm@6211
    79
wenzelm@6211
    80
fun loader_msg txt names = gen_msg ("Theory loader: " ^ txt) names;
wenzelm@6211
    81
wenzelm@6211
    82
val show_path = space_implode " via " o map quote;
wenzelm@9332
    83
fun cycle_msg names = loader_msg ("cyclic dependency of " ^ show_path names) [];
berghofe@3604
    84
berghofe@3604
    85
wenzelm@6666
    86
(* derived graph operations *)
berghofe@3604
    87
wenzelm@9327
    88
fun add_deps name parents G = Graph.add_deps_acyclic (name, parents) G
wenzelm@9332
    89
  handle Graph.CYCLES namess => error (cat_lines (map cycle_msg namess));
berghofe@3604
    90
wenzelm@7952
    91
fun upd_deps name entry G =
skalberg@15570
    92
  Library.foldl (fn (H, parent) => Graph.del_edge (parent, name) H) (G, Graph.imm_preds G name)
wenzelm@7952
    93
  |> Graph.map_node name (K entry);
wenzelm@3976
    94
wenzelm@6211
    95
fun update_node name parents entry G =
wenzelm@7952
    96
  (if can (Graph.get_node G) name then upd_deps name entry G else Graph.new_node (name, entry) G)
wenzelm@7952
    97
  |> add_deps name parents;
berghofe@3604
    98
berghofe@3604
    99
wenzelm@6211
   100
(* thy database *)
wenzelm@6211
   101
wenzelm@6211
   102
type deps =
wenzelm@6211
   103
  {present: bool, outdated: bool,
wenzelm@7941
   104
    master: ThyLoad.master option, files: (Path.T * (Path.T * File.info) option) list};
wenzelm@6211
   105
wenzelm@6241
   106
fun make_deps present outdated master files =
wenzelm@6241
   107
  {present = present, outdated = outdated, master = master, files = files}: deps;
berghofe@3604
   108
skalberg@15531
   109
fun init_deps master files = SOME (make_deps false true master (map (rpair NONE) files));
wenzelm@7211
   110
wenzelm@7211
   111
wenzelm@6362
   112
type thy = deps option * theory option;
berghofe@3604
   113
wenzelm@6211
   114
local
wenzelm@6362
   115
  val database = ref (Graph.empty: thy Graph.T);
wenzelm@6211
   116
in
wenzelm@6362
   117
  fun get_thys () = ! database;
wenzelm@9137
   118
  val change_thys = Library.change database;
berghofe@3604
   119
end;
wenzelm@5209
   120
wenzelm@5209
   121
wenzelm@6211
   122
(* access thy graph *)
wenzelm@6211
   123
wenzelm@6211
   124
fun thy_graph f x = f (get_thys ()) x;
wenzelm@9417
   125
wenzelm@9417
   126
(*theory names in topological order*)
wenzelm@9417
   127
fun get_names () =
wenzelm@9417
   128
  let val G = get_thys ()
wenzelm@14774
   129
  in Graph.all_succs G (Graph.minimals G) end;
wenzelm@6211
   130
wenzelm@6211
   131
wenzelm@6211
   132
(* access thy *)
wenzelm@6211
   133
wenzelm@7935
   134
fun lookup_thy name =
skalberg@15531
   135
  SOME (thy_graph Graph.get_node name) handle Graph.UNDEF _ => NONE;
wenzelm@7935
   136
wenzelm@16047
   137
val known_thy = is_some o lookup_thy;
wenzelm@7935
   138
fun check_known_thy name = known_thy name orelse (warning ("Unknown theory " ^ quote name); false);
wenzelm@7935
   139
fun if_known_thy f name = if check_known_thy name then f name else ();
wenzelm@6211
   140
wenzelm@6211
   141
fun get_thy name =
wenzelm@6211
   142
  (case lookup_thy name of
skalberg@15531
   143
    SOME thy => thy
skalberg@15531
   144
  | NONE => error (loader_msg "nothing known about theory" [name]));
wenzelm@6211
   145
wenzelm@6211
   146
fun change_thy name f = (get_thy name; change_thys (Graph.map_node name f));
wenzelm@6211
   147
wenzelm@6211
   148
wenzelm@6211
   149
(* access deps *)
wenzelm@6211
   150
skalberg@15570
   151
val lookup_deps = Option.map #1 o lookup_thy;
wenzelm@6211
   152
val get_deps = #1 o get_thy;
wenzelm@6211
   153
fun change_deps name f = change_thy name (fn (deps, x) => (f deps, x));
wenzelm@6211
   154
wenzelm@6666
   155
fun is_finished name = is_none (get_deps name);
skalberg@15531
   156
fun get_files name = (case get_deps name of SOME {files, ...} => files | _ => []);
wenzelm@7191
   157
wenzelm@7191
   158
fun loaded_files name =
wenzelm@7191
   159
  (case get_deps name of
skalberg@15531
   160
    NONE => []
skalberg@15531
   161
  | SOME {master, files, ...} =>
skalberg@15531
   162
      (case master of SOME m => [#1 (ThyLoad.get_thy m)] | NONE => []) @
skalberg@15570
   163
      (List.mapPartial (Option.map #1 o #2) files));
wenzelm@6211
   164
berghofe@6654
   165
fun get_preds name =
wenzelm@9327
   166
  (thy_graph Graph.imm_preds name) handle Graph.UNDEF _ =>
berghofe@6654
   167
    error (loader_msg "nothing known about theory" [name]);
berghofe@6654
   168
wenzelm@6211
   169
wenzelm@16047
   170
(* pretty printing a theory *)
wenzelm@16047
   171
wenzelm@16047
   172
local
paulson@15633
   173
wenzelm@16047
   174
fun relevant_names names =
wenzelm@16047
   175
  let
wenzelm@16047
   176
    val (finished, unfinished) =
wenzelm@16047
   177
      List.filter (equal "#" orf known_thy) names
wenzelm@16047
   178
      |> List.partition (not_equal "#" andf is_finished);
wenzelm@16047
   179
  in (if not (null finished) then [List.last finished] else []) @ unfinished end;
paulson@15633
   180
wenzelm@16047
   181
fun pretty_sg sg =
wenzelm@16047
   182
  Pretty.str_list "{" "}" (relevant_names (Sign.stamp_names_of sg));
wenzelm@16047
   183
wenzelm@16047
   184
in
paulson@15633
   185
paulson@15633
   186
val pretty_theory = pretty_sg o Theory.sign_of;
paulson@15633
   187
val pprint_theory = Pretty.pprint o pretty_theory;
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
skalberg@15531
   201
    (SOME theory) => theory
wenzelm@6211
   202
  | _ => error (loader_msg "undefined theory entry for" [name]));
wenzelm@6211
   203
wenzelm@6211
   204
val theory_of_sign = get_theory o Sign.name_of;
wenzelm@6211
   205
val theory_of_thm = theory_of_sign o Thm.sign_of_thm;
wenzelm@6211
   206
skalberg@15531
   207
fun put_theory name theory = change_thy name (fn (deps, _) => (deps, SOME theory));
wenzelm@6211
   208
wenzelm@6211
   209
wenzelm@6211
   210
wenzelm@6211
   211
(** thy operations **)
wenzelm@6211
   212
wenzelm@6241
   213
(* maintain 'outdated' flag *)
wenzelm@6211
   214
wenzelm@7099
   215
local
wenzelm@7099
   216
wenzelm@6241
   217
fun is_outdated name =
wenzelm@6241
   218
  (case lookup_deps name of
skalberg@15531
   219
    SOME (SOME {outdated, ...}) => outdated
wenzelm@6241
   220
  | _ => false);
wenzelm@6211
   221
wenzelm@6241
   222
fun outdate_thy name =
wenzelm@7099
   223
  if is_finished name orelse is_outdated name then ()
skalberg@15570
   224
  else (change_deps name (Option.map (fn {present, outdated = _, master, files} =>
wenzelm@7099
   225
    make_deps present true master files)); perform Outdate name);
wenzelm@7099
   226
wenzelm@7910
   227
fun check_unfinished name =
skalberg@15531
   228
  if is_finished name then (warning (loader_msg "tried to touch finished theory" [name]); NONE)
skalberg@15531
   229
  else SOME name;
wenzelm@7910
   230
wenzelm@7099
   231
in
wenzelm@6211
   232
wenzelm@7910
   233
fun touch_thys names =
skalberg@15570
   234
  List.app outdate_thy (thy_graph Graph.all_succs (List.mapPartial check_unfinished names));
wenzelm@7910
   235
wenzelm@7910
   236
fun touch_thy name = touch_thys [name];
wenzelm@7910
   237
fun touch_child_thys name = touch_thys (thy_graph Graph.imm_succs name);
wenzelm@6211
   238
skalberg@15570
   239
fun touch_all_thys () = List.app outdate_thy (get_names ());
wenzelm@6211
   240
wenzelm@7099
   241
end;
wenzelm@6211
   242
wenzelm@6211
   243
wenzelm@7244
   244
(* check 'finished' state *)
wenzelm@7244
   245
wenzelm@7244
   246
fun check_unfinished fail name =
wenzelm@7910
   247
  if known_thy name andalso is_finished name then
wenzelm@7288
   248
    fail (loader_msg "cannot update finished theory" [name])
wenzelm@7244
   249
  else ();
wenzelm@7244
   250
wenzelm@7244
   251
wenzelm@7941
   252
(* load_file *)
wenzelm@6211
   253
skalberg@15570
   254
val opt_path = Option.map (Path.dir o fst o ThyLoad.get_thy);
wenzelm@16047
   255
fun opt_path' (d: deps option) = if_none (Option.map (opt_path o #master) d) NONE;
wenzelm@16047
   256
fun opt_path'' d = if_none (Option.map opt_path' d) NONE;
berghofe@15158
   257
wenzelm@7191
   258
local
wenzelm@7191
   259
skalberg@15531
   260
fun provide path name info (deps as SOME {present, outdated, master, files}) =
wenzelm@7941
   261
     (if exists (equal path o #1) files then ()
wenzelm@7941
   262
      else warning (loader_msg "undeclared dependency of theory" [name] ^
wenzelm@7941
   263
        " on file: " ^ quote (Path.pack path));
skalberg@15531
   264
      SOME (make_deps present outdated master (overwrite (files, (path, SOME info)))))
skalberg@15531
   265
  | provide _ _ _ NONE = NONE;
wenzelm@7941
   266
wenzelm@7941
   267
fun run_file path =
skalberg@15570
   268
  (case Option.map PureThy.get_name (Context.get_context ()) of
skalberg@15531
   269
    NONE => (ThyLoad.load_file NONE path; ())
skalberg@15531
   270
  | SOME name => (case lookup_deps name of
skalberg@15531
   271
      SOME deps => change_deps name (provide path name
berghofe@15158
   272
        (ThyLoad.load_file (opt_path' deps) path))
skalberg@15531
   273
    | NONE => (ThyLoad.load_file NONE path; ())));
wenzelm@6211
   274
wenzelm@7191
   275
in
wenzelm@7191
   276
wenzelm@7941
   277
fun load_file time path =
wenzelm@7941
   278
  if time then
wenzelm@7244
   279
    let val name = Path.pack path in
wenzelm@7244
   280
      timeit (fn () =>
wenzelm@9778
   281
       (priority ("\n**** Starting file " ^ quote name ^ " ****");
wenzelm@9036
   282
        run_file path;
wenzelm@9778
   283
        priority ("**** Finished file " ^ quote name ^ " ****\n")))
wenzelm@7244
   284
    end
wenzelm@7941
   285
  else run_file path;
wenzelm@7941
   286
wenzelm@7941
   287
val use = load_file false o Path.unpack;
wenzelm@7941
   288
val time_use = load_file true o Path.unpack;
wenzelm@7191
   289
wenzelm@7191
   290
end;
wenzelm@6233
   291
wenzelm@6233
   292
wenzelm@7099
   293
(* load_thy *)
wenzelm@7099
   294
wenzelm@9490
   295
fun required_by _ [] = ""
wenzelm@9490
   296
  | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
wenzelm@7099
   297
berghofe@15065
   298
fun load_thy really ml time initiators dir name =
wenzelm@7099
   299
  let
wenzelm@9822
   300
    val _ =
wenzelm@9822
   301
      if really then priority ("Loading theory " ^ quote name ^ required_by " " initiators)
wenzelm@9822
   302
      else priority ("Registering theory " ^ quote name);
wenzelm@7099
   303
wenzelm@7099
   304
    val _ = touch_thy name;
wenzelm@9822
   305
    val master =
wenzelm@9822
   306
      if really then ThyLoad.load_thy dir name ml time
wenzelm@9822
   307
      else #1 (ThyLoad.deps_thy dir name ml);
wenzelm@7099
   308
wenzelm@7099
   309
    val files = get_files name;
skalberg@15570
   310
    val missing_files = List.mapPartial (fn (path, NONE) => SOME (Path.pack path) | _ => NONE) files;
wenzelm@7099
   311
  in
wenzelm@7099
   312
    if null missing_files then ()
wenzelm@7099
   313
    else warning (loader_msg "unresolved dependencies of theory" [name] ^
wenzelm@7223
   314
      " on file(s): " ^ commas_quote missing_files);
skalberg@15531
   315
    change_deps name (fn _ => SOME (make_deps true false (SOME master) files));
wenzelm@7099
   316
    perform Update name
wenzelm@7099
   317
  end;
wenzelm@7099
   318
wenzelm@7099
   319
wenzelm@6211
   320
(* require_thy *)
wenzelm@6211
   321
berghofe@15065
   322
fun base_of_path s = Path.pack (Path.base (Path.unpack s));
berghofe@15065
   323
wenzelm@7211
   324
local
wenzelm@6211
   325
wenzelm@7941
   326
fun load_deps ml dir name =
wenzelm@7941
   327
  let val (master, (parents, files)) = ThyLoad.deps_thy dir name ml
skalberg@15531
   328
  in (SOME (init_deps (SOME master) files), parents) end;
wenzelm@6211
   329
skalberg@15531
   330
fun file_current master (_, NONE) = false
berghofe@15158
   331
  | file_current master (path, info) =
berghofe@15158
   332
      (info = ThyLoad.check_file (opt_path master) path);
wenzelm@6211
   333
wenzelm@7941
   334
fun current_deps ml strict dir name =
wenzelm@6211
   335
  (case lookup_deps name of
skalberg@15531
   336
    NONE => (false, load_deps ml dir name)
skalberg@15531
   337
  | SOME deps =>
berghofe@15158
   338
      let
skalberg@15531
   339
        fun get_name s = (case opt_path'' (lookup_deps s) of NONE => s
skalberg@15531
   340
          | SOME p => Path.pack (Path.append p (Path.basic s)));
skalberg@15531
   341
        val same_deps = (NONE, map get_name (thy_graph Graph.imm_preds name))
berghofe@15158
   342
      in
wenzelm@6211
   343
        (case deps of
skalberg@15531
   344
          NONE => (true, same_deps)
skalberg@15531
   345
        | SOME {present, outdated, master, files} =>
wenzelm@6211
   346
            if present andalso not strict then (true, same_deps)
wenzelm@6211
   347
            else
skalberg@15531
   348
              let val master' = SOME (ThyLoad.check_thy dir name ml) in
wenzelm@7941
   349
                if master <> master' then (false, load_deps ml dir name)
berghofe@15158
   350
                else (not outdated andalso forall (file_current master') files, same_deps)
wenzelm@6211
   351
              end)
wenzelm@6211
   352
      end);
wenzelm@6211
   353
wenzelm@9822
   354
fun require_thy really ml time strict keep_strict initiators prfx (visited, str) =
wenzelm@6211
   355
  let
wenzelm@7066
   356
    val path = Path.expand (Path.unpack str);
wenzelm@6484
   357
    val name = Path.pack (Path.base path);
wenzelm@7066
   358
  in
wenzelm@9332
   359
    if name mem_string initiators then error (cycle_msg initiators) else ();
wenzelm@7952
   360
    if known_thy name andalso is_finished name orelse name mem_string visited then
wenzelm@7952
   361
      (visited, (true, name))
wenzelm@7066
   362
    else
wenzelm@7066
   363
      let
wenzelm@7066
   364
        val dir = Path.append prfx (Path.dir path);
wenzelm@9822
   365
        val req_parent = require_thy true true time (strict andalso keep_strict) keep_strict
berghofe@15065
   366
          (name :: initiators);
wenzelm@6484
   367
wenzelm@7941
   368
        val (current, (new_deps, parents)) = current_deps ml strict dir name handle ERROR =>
wenzelm@7099
   369
          error (loader_msg "the error(s) above occurred while examining theory" [name] ^
wenzelm@9490
   370
            (if null initiators then "" else required_by "\n" initiators));
wenzelm@16047
   371
        val dir' = if_none (opt_path'' new_deps) dir;
berghofe@15065
   372
        val (visited', parent_results) = foldl_map (req_parent dir') (name :: visited, parents);
wenzelm@6211
   373
skalberg@15531
   374
        val thy = if not really then SOME (get_theory name) else NONE;
wenzelm@7066
   375
        val result =
wenzelm@7066
   376
          if current andalso forall #1 parent_results then true
wenzelm@7066
   377
          else
wenzelm@7066
   378
            ((case new_deps of
skalberg@15531
   379
              SOME deps => change_thys (update_node name (map base_of_path parents) (deps, thy))
skalberg@15531
   380
            | NONE => ());
berghofe@15065
   381
             load_thy really ml (time orelse ! Output.timing) initiators dir name;
wenzelm@7099
   382
             false);
wenzelm@7066
   383
      in (visited', (result, name)) end
wenzelm@7066
   384
  end;
wenzelm@6484
   385
berghofe@15158
   386
fun gen_use_thy' req prfx s =
berghofe@15158
   387
  let val (_, (_, name)) = req [] prfx ([], s)
wenzelm@7066
   388
  in Context.context (get_theory name) end;
wenzelm@6241
   389
berghofe@15158
   390
fun gen_use_thy req = gen_use_thy' req Path.current;
berghofe@15158
   391
wenzelm@7244
   392
fun warn_finished f name = (check_unfinished warning name; f name);
wenzelm@7244
   393
wenzelm@7211
   394
in
wenzelm@7211
   395
berghofe@15158
   396
val weak_use_thy         = gen_use_thy' (require_thy true true false false false);
berghofe@15158
   397
fun quiet_update_thy' ml = gen_use_thy' (require_thy true ml false true true);
berghofe@15158
   398
fun quiet_update_thy ml  = gen_use_thy (require_thy true ml 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 use_thy_only     = warn_finished (gen_use_thy (require_thy true false false true false));
wenzelm@9822
   403
val update_thy       = warn_finished (gen_use_thy (require_thy true true false true true));
wenzelm@9822
   404
val update_thy_only  = warn_finished (gen_use_thy (require_thy true false false true true));
wenzelm@9822
   405
val pretend_use_thy_only = warn_finished (gen_use_thy (require_thy false false false true false));
wenzelm@6211
   406
wenzelm@7211
   407
end;
wenzelm@7211
   408
wenzelm@6211
   409
wenzelm@6666
   410
(* remove_thy *)
wenzelm@6666
   411
wenzelm@6666
   412
fun remove_thy name =
wenzelm@7910
   413
  if is_finished name then error (loader_msg "cannot remove finished theory" [name])
wenzelm@6666
   414
  else
wenzelm@6666
   415
    let val succs = thy_graph Graph.all_succs [name] in
wenzelm@9778
   416
      priority (loader_msg "removing" succs);
skalberg@15570
   417
      List.app (perform Remove) succs;
wenzelm@7191
   418
      change_thys (Graph.del_nodes succs)
wenzelm@6666
   419
    end;
wenzelm@6666
   420
wenzelm@6666
   421
wenzelm@7066
   422
(* begin / end theory *)
wenzelm@6211
   423
wenzelm@8805
   424
fun begin_theory present upd name parents paths =
wenzelm@6211
   425
  let
berghofe@15065
   426
    val bparents = map base_of_path parents;
berghofe@15158
   427
    val dir' = opt_path'' (lookup_deps name);
wenzelm@16047
   428
    val dir = if_none dir' Path.current;
berghofe@15158
   429
    val assert_thy = if upd then quiet_update_thy' true dir else weak_use_thy dir;
wenzelm@7244
   430
    val _ = check_unfinished error name;
skalberg@15570
   431
    val _ = List.app assert_thy parents;
berghofe@15065
   432
    val theory = PureThy.begin_theory name (map get_theory bparents);
wenzelm@7952
   433
    val deps =
wenzelm@7952
   434
      if known_thy name then get_deps name
skalberg@15531
   435
      else (init_deps NONE (map #1 paths));   (*records additional ML files only!*)
skalberg@15570
   436
    val use_paths = List.mapPartial (fn (x, true) => SOME x | _ => NONE) paths;
wenzelm@9451
   437
skalberg@15531
   438
    val _ = change_thys (update_node name bparents (deps, SOME (Theory.copy theory)));
berghofe@15158
   439
    val theory' = theory |> present dir' name bparents paths;
wenzelm@9451
   440
    val _ = put_theory name (Theory.copy theory');
skalberg@15570
   441
    val ((), theory'') = Context.pass_theory theory' (List.app (load_file false)) use_paths;
wenzelm@9451
   442
    val _ = put_theory name (Theory.copy theory'');
wenzelm@8805
   443
  in theory'' end;
wenzelm@7244
   444
wenzelm@6211
   445
fun end_theory theory =
wenzelm@7099
   446
  let
wenzelm@7099
   447
    val theory' = PureThy.end_theory theory;
wenzelm@7099
   448
    val name = PureThy.get_name theory;
wenzelm@7099
   449
  in put_theory name theory'; theory' end;
wenzelm@6211
   450
wenzelm@6211
   451
wenzelm@6666
   452
(* finish all theories *)
wenzelm@6211
   453
skalberg@15531
   454
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (NONE, entry)));
wenzelm@6211
   455
wenzelm@6211
   456
wenzelm@6211
   457
(* register existing theories *)
wenzelm@6211
   458
wenzelm@6211
   459
fun register_theory theory =
wenzelm@6211
   460
  let
wenzelm@6211
   461
    val name = PureThy.get_name theory;
wenzelm@6211
   462
    val parents = Theory.parents_of theory;
wenzelm@6211
   463
    val parent_names = map PureThy.get_name parents;
wenzelm@6211
   464
wenzelm@6211
   465
    fun err txt bads =
wenzelm@6211
   466
      error (loader_msg txt bads ^ "\n" ^ gen_msg "cannot register theory" [name]);
wenzelm@6211
   467
wenzelm@6666
   468
    val nonfinished = filter_out is_finished parent_names;
wenzelm@6211
   469
    fun get_variant (x, y_name) =
skalberg@15531
   470
      if Theory.eq_thy (x, get_theory y_name) then NONE
skalberg@15531
   471
      else SOME y_name;
skalberg@15570
   472
    val variants = List.mapPartial get_variant (parents ~~ parent_names);
wenzelm@6211
   473
wenzelm@6211
   474
    fun register G =
skalberg@15531
   475
      (Graph.new_node (name, (NONE, SOME theory)) G
wenzelm@9327
   476
        handle Graph.DUP _ => err "duplicate theory entry" [])
wenzelm@6211
   477
      |> add_deps name parent_names;
wenzelm@6211
   478
  in
wenzelm@6666
   479
    if not (null nonfinished) then err "non-finished parent theories" nonfinished
wenzelm@6211
   480
    else if not (null variants) then err "different versions of parent theories" variants
wenzelm@7099
   481
    else (change_thys register; perform Update name)
wenzelm@6211
   482
  end;
wenzelm@6211
   483
wenzelm@15801
   484
val _ = register_theory ProtoPure.thy;
wenzelm@15801
   485
wenzelm@6211
   486
wenzelm@6211
   487
(*final declarations of this structure*)
wenzelm@6211
   488
val theory = get_theory;
wenzelm@6211
   489
val names = get_names;
wenzelm@6211
   490
wenzelm@6211
   491
end;
wenzelm@6211
   492
wenzelm@5209
   493
structure BasicThyInfo: BASIC_THY_INFO = ThyInfo;
wenzelm@5209
   494
open BasicThyInfo;