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