src/Pure/Thy/thy_info.ML
author wenzelm
Fri Jul 23 16:50:20 1999 +0200 (1999-07-23)
changeset 7066 febce8eee487
parent 6900 29060d9b60d4
child 7099 8142ccd13963
permissions -rw-r--r--
require_thy: fixed performance leak;
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@6362
     5
Theory loader database, including theory and file dependencies.
wenzelm@3976
     6
*)
berghofe@3604
     7
wenzelm@5209
     8
signature BASIC_THY_INFO =
wenzelm@5209
     9
sig
wenzelm@5209
    10
  val theory: string -> theory
wenzelm@6211
    11
  val theory_of_sign: Sign.sg -> theory
wenzelm@6211
    12
  val theory_of_thm: thm -> theory
wenzelm@6241
    13
(*val use: string -> unit*)             (*exported later*)
wenzelm@6241
    14
  val time_use: string -> unit
wenzelm@6211
    15
  val touch_thy: string -> unit
wenzelm@6211
    16
  val use_thy: string -> 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@6527
    20
  val use_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@6211
    26
  val names: unit -> string list
wenzelm@6211
    27
  val get_theory: string -> theory
wenzelm@6211
    28
  val put_theory: theory -> unit
berghofe@6654
    29
  val get_preds: string -> string list
wenzelm@6241
    30
  val loaded_files: string -> (Path.T * File.info) list
wenzelm@6241
    31
  val load_file: bool -> Path.T -> unit
wenzelm@6329
    32
  val use_path: Path.T -> unit
wenzelm@6241
    33
  val use: string -> unit
wenzelm@6329
    34
  val begin_theory: string -> string list -> (Path.T * bool) list -> theory
wenzelm@6211
    35
  val end_theory: theory -> theory
wenzelm@6666
    36
  val finish: unit -> unit
wenzelm@6211
    37
  val register_theory: theory -> unit
berghofe@3604
    38
end;
berghofe@3604
    39
wenzelm@6362
    40
structure ThyInfo: THY_INFO =
wenzelm@6211
    41
struct
berghofe@3604
    42
berghofe@3604
    43
wenzelm@6211
    44
(** thy database **)
wenzelm@6211
    45
wenzelm@6211
    46
(* messages *)
wenzelm@6211
    47
wenzelm@6211
    48
fun gen_msg txt [] = txt
wenzelm@6211
    49
  | gen_msg txt names = txt ^ " " ^ commas_quote names;
wenzelm@6211
    50
wenzelm@6211
    51
fun loader_msg txt names = gen_msg ("Theory loader: " ^ txt) names;
wenzelm@6211
    52
wenzelm@6211
    53
val show_path = space_implode " via " o map quote;
wenzelm@6211
    54
fun cycle_msg name names = loader_msg ("cyclic dependency of " ^ show_path (name :: names)) [];
berghofe@3604
    55
berghofe@3604
    56
wenzelm@6666
    57
(* derived graph operations *)
berghofe@3604
    58
wenzelm@6211
    59
fun add_deps name parents G =
wenzelm@6211
    60
  foldl (fn (H, parent) => Graph.add_edge_acyclic (parent, name) H) (G, parents)
wenzelm@6211
    61
    handle Graph.CYCLES namess => error (cat_lines (map (cycle_msg name) namess));
berghofe@3604
    62
wenzelm@6666
    63
fun del_deps name G =
wenzelm@6211
    64
  foldl (fn (H, parent) => Graph.del_edge (parent, name) H) (G, Graph.imm_preds G name);
wenzelm@3976
    65
wenzelm@6211
    66
fun update_node name parents entry G =
wenzelm@6211
    67
  add_deps name parents
wenzelm@6211
    68
    (if can (Graph.get_node G) name then del_deps name G else Graph.new_node (name, entry) G);
berghofe@3604
    69
berghofe@3604
    70
wenzelm@6211
    71
(* thy database *)
wenzelm@6211
    72
wenzelm@6211
    73
type deps =
wenzelm@6211
    74
  {present: bool, outdated: bool,
wenzelm@6241
    75
    master: (Path.T * File.info) list, files: (Path.T * (Path.T * File.info) option) list};
wenzelm@6211
    76
wenzelm@6241
    77
fun make_deps present outdated master files =
wenzelm@6241
    78
  {present = present, outdated = outdated, master = master, files = files}: deps;
berghofe@3604
    79
wenzelm@6362
    80
type thy = deps option * theory option;
berghofe@3604
    81
wenzelm@6211
    82
local
wenzelm@6362
    83
  val database = ref (Graph.empty: thy Graph.T);
wenzelm@6211
    84
in
wenzelm@6362
    85
  fun get_thys () = ! database;
wenzelm@6362
    86
  fun change_thys f = database := (f (! database));
berghofe@3604
    87
end;
wenzelm@5209
    88
wenzelm@5209
    89
wenzelm@6211
    90
(* access thy graph *)
wenzelm@6211
    91
wenzelm@6211
    92
fun thy_graph f x = f (get_thys ()) x;
wenzelm@6666
    93
fun get_names () = Graph.keys (get_thys ());
wenzelm@6211
    94
wenzelm@6211
    95
wenzelm@6211
    96
(* access thy *)
wenzelm@6211
    97
wenzelm@6211
    98
fun lookup_thy name = Some (thy_graph Graph.get_node name) handle Graph.UNDEFINED _ => None;
wenzelm@6211
    99
wenzelm@6211
   100
fun get_thy name =
wenzelm@6211
   101
  (case lookup_thy name of
wenzelm@6211
   102
    Some thy => thy
wenzelm@6211
   103
  | None => error (loader_msg "nothing known about theory" [name]));
wenzelm@6211
   104
wenzelm@6211
   105
fun change_thy name f = (get_thy name; change_thys (Graph.map_node name f));
wenzelm@6211
   106
wenzelm@6211
   107
wenzelm@6211
   108
(* access deps *)
wenzelm@6211
   109
wenzelm@6211
   110
val lookup_deps = apsome #1 o lookup_thy;
wenzelm@6211
   111
val get_deps = #1 o get_thy;
wenzelm@6211
   112
fun change_deps name f = change_thy name (fn (deps, x) => (f deps, x));
wenzelm@6211
   113
wenzelm@6666
   114
fun is_finished name = is_none (get_deps name);
wenzelm@6211
   115
fun get_files name = (case get_deps name of Some {files, ...} => files | _ => []);
wenzelm@6241
   116
val loaded_files = mapfilter #2 o get_files;
wenzelm@6211
   117
berghofe@6654
   118
fun get_preds name =
berghofe@6654
   119
  (thy_graph Graph.imm_preds name) handle Graph.UNDEFINED _ =>
berghofe@6654
   120
    error (loader_msg "nothing known about theory" [name]);
berghofe@6654
   121
wenzelm@6211
   122
wenzelm@6211
   123
(* access theory *)
wenzelm@6211
   124
wenzelm@6211
   125
fun get_theory name =
wenzelm@6211
   126
  (case get_thy name of
wenzelm@6362
   127
    (_, Some theory) => theory
wenzelm@6211
   128
  | _ => error (loader_msg "undefined theory entry for" [name]));
wenzelm@6211
   129
wenzelm@6211
   130
val theory_of_sign = get_theory o Sign.name_of;
wenzelm@6211
   131
val theory_of_thm = theory_of_sign o Thm.sign_of_thm;
wenzelm@6211
   132
wenzelm@6211
   133
fun put_theory theory =
wenzelm@6362
   134
  change_thy (PureThy.get_name theory) (fn (deps, _) => (deps, Some theory));
wenzelm@6211
   135
wenzelm@6211
   136
wenzelm@6211
   137
wenzelm@6211
   138
(** thy operations **)
wenzelm@6211
   139
wenzelm@6241
   140
(* maintain 'outdated' flag *)
wenzelm@6211
   141
wenzelm@6241
   142
fun is_outdated name =
wenzelm@6241
   143
  (case lookup_deps name of
wenzelm@6241
   144
    Some (Some {outdated, ...}) => outdated
wenzelm@6241
   145
  | _ => false);
wenzelm@6211
   146
wenzelm@6241
   147
fun outdate_thy name =
wenzelm@6666
   148
  if is_finished name then ()
wenzelm@6241
   149
  else change_deps name (apsome (fn {present, outdated = _, master, files} =>
wenzelm@6241
   150
    make_deps present true master files));
wenzelm@6211
   151
wenzelm@6241
   152
fun touch_thy name =
wenzelm@6241
   153
  if is_outdated name then ()
wenzelm@6666
   154
  else if is_finished name then warning (loader_msg "tried to touch finished theory" [name])
wenzelm@6241
   155
  else
wenzelm@6241
   156
    (case filter_out is_outdated (thy_graph Graph.all_succs [name]) \ name of
wenzelm@6241
   157
      [] => outdate_thy name
wenzelm@6241
   158
    | names =>
wenzelm@6241
   159
       (warning (loader_msg "the following theories become out-of-date:" names);
wenzelm@6241
   160
        seq outdate_thy names; outdate_thy name));
wenzelm@6241
   161
wenzelm@6241
   162
val untouch_deps = apsome (fn {present, outdated = _, master, files}: deps =>
wenzelm@6241
   163
  make_deps present false master files);
wenzelm@6211
   164
wenzelm@6211
   165
wenzelm@6211
   166
(* load_thy *)
wenzelm@6211
   167
wenzelm@6211
   168
fun required_by [] = ""
wenzelm@6211
   169
  | required_by initiators = " (required by " ^ show_path (rev initiators) ^ ")";
wenzelm@6211
   170
wenzelm@6484
   171
fun load_thy ml time initiators dir name parents =
wenzelm@6211
   172
  let
wenzelm@6211
   173
    val _ = if name mem_string initiators then error (cycle_msg name (rev initiators)) else ();
wenzelm@6211
   174
    val _ = writeln ("Loading theory " ^ quote name ^ required_by initiators);
wenzelm@6211
   175
wenzelm@6211
   176
    val _ = seq touch_thy (thy_graph Graph.all_succs [name]);
wenzelm@6484
   177
    val master = ThyLoad.load_thy dir name ml time;
wenzelm@6211
   178
wenzelm@6211
   179
    val files = get_files name;
wenzelm@6211
   180
    val missing_files = mapfilter (fn (path, None) => Some (Path.pack path) | _ => None) files;
wenzelm@6211
   181
  in
wenzelm@6211
   182
    if null missing_files then ()
wenzelm@6263
   183
    else warning (loader_msg "unresolved dependencies of theory" [name] ^
wenzelm@6263
   184
      "\nfile(s): " ^ commas_quote missing_files);
wenzelm@6241
   185
    change_deps name (fn _ => Some (make_deps true false master files))
wenzelm@6211
   186
  end;
wenzelm@6211
   187
wenzelm@6211
   188
wenzelm@6211
   189
(* load_file *)
wenzelm@6211
   190
wenzelm@6241
   191
fun run_file path =
wenzelm@6211
   192
  let
wenzelm@6211
   193
    fun provide name info (deps as Some {present, outdated, master, files}) =
wenzelm@6263
   194
          if exists (equal path o #1) files then
wenzelm@6241
   195
            Some (make_deps present outdated master (overwrite (files, (path, Some info))))
wenzelm@6241
   196
          else (warning (loader_msg "undeclared dependency of theory" [name] ^
wenzelm@6389
   197
            " on file: " ^ quote (Path.pack path)); deps)
wenzelm@6211
   198
      | provide _ _ None = None;
wenzelm@6211
   199
  in
wenzelm@6211
   200
    (case apsome PureThy.get_name (Context.get_context ()) of
wenzelm@6211
   201
      None => (ThyLoad.load_file path; ())
wenzelm@6211
   202
    | Some name =>
wenzelm@6211
   203
        if is_some (lookup_thy name) then change_deps name (provide name (ThyLoad.load_file path))
wenzelm@6211
   204
        else (ThyLoad.load_file path; ()))
wenzelm@6211
   205
  end;
wenzelm@6211
   206
wenzelm@6241
   207
fun load_file false path = run_file path
wenzelm@6241
   208
  | load_file true path =
wenzelm@6241
   209
      let val name = Path.pack path in
wenzelm@6241
   210
        timeit (fn () =>
wenzelm@6241
   211
         (writeln ("\n**** Starting file " ^ quote name ^ " ****");
wenzelm@6241
   212
          run_file path;
wenzelm@6241
   213
          writeln ("**** Finished file " ^ quote name ^ " ****\n")))
wenzelm@6241
   214
      end;
wenzelm@6211
   215
wenzelm@6329
   216
val use_path = load_file false;
wenzelm@6329
   217
val use = use_path o Path.unpack;
wenzelm@6241
   218
val time_use = load_file true o Path.unpack;
wenzelm@6233
   219
wenzelm@6233
   220
wenzelm@6211
   221
(* require_thy *)
wenzelm@6211
   222
wenzelm@6241
   223
fun init_deps master files = Some (make_deps false false master (map (rpair None) files));
wenzelm@6211
   224
wenzelm@6484
   225
fun load_deps dir name ml =
wenzelm@6484
   226
  let val (master, (parents, files)) = ThyLoad.deps_thy dir name ml
wenzelm@6233
   227
  in (Some (init_deps master files), parents) end;
wenzelm@6211
   228
wenzelm@6211
   229
fun file_current (_, None) = false
wenzelm@6211
   230
  | file_current (path, info) = info = ThyLoad.check_file path;
wenzelm@6211
   231
wenzelm@6484
   232
fun current_deps ml strict dir name =
wenzelm@6211
   233
  (case lookup_deps name of
wenzelm@6484
   234
    None => (false, load_deps dir name ml)
wenzelm@6211
   235
  | Some deps =>
wenzelm@6211
   236
      let val same_deps = (None, thy_graph Graph.imm_preds name) in
wenzelm@6211
   237
        (case deps of
wenzelm@6211
   238
          None => (true, same_deps)
wenzelm@6211
   239
        | Some {present, outdated, master, files} =>
wenzelm@6211
   240
            if present andalso not strict then (true, same_deps)
wenzelm@6211
   241
            else
wenzelm@6484
   242
              let val master' = ThyLoad.check_thy dir name ml in
wenzelm@6484
   243
                if master <> master' then (false, load_deps dir name ml)
wenzelm@6211
   244
                else (not outdated andalso forall file_current files, same_deps)
wenzelm@6211
   245
              end)
wenzelm@6211
   246
      end);
wenzelm@6211
   247
wenzelm@7066
   248
fun require_thy ml time strict keep_strict initiators prfx (visited, str) =
wenzelm@6211
   249
  let
wenzelm@7066
   250
    val path = Path.expand (Path.unpack str);
wenzelm@6484
   251
    val name = Path.pack (Path.base path);
wenzelm@7066
   252
  in
wenzelm@7066
   253
    if name mem_string visited then (visited, (true, name))
wenzelm@7066
   254
    else
wenzelm@7066
   255
      let
wenzelm@7066
   256
        val dir = Path.append prfx (Path.dir path);
wenzelm@7066
   257
        val req_parent =
wenzelm@7066
   258
          require_thy ml time (strict andalso keep_strict) keep_strict (name :: initiators) dir;
wenzelm@6484
   259
wenzelm@7066
   260
        val (current, (new_deps, parents)) = current_deps ml strict dir name handle ERROR =>
wenzelm@7066
   261
          error (loader_msg "The error(s) above occurred while examining theory" [name] ^
wenzelm@7066
   262
            (if null initiators then "" else "\n" ^ required_by initiators));
wenzelm@7066
   263
        val (visited', parent_results) = foldl_map req_parent (name :: visited, parents);
wenzelm@6211
   264
wenzelm@7066
   265
        val result =
wenzelm@7066
   266
          if current andalso forall #1 parent_results then true
wenzelm@7066
   267
          else
wenzelm@7066
   268
            ((case new_deps of
wenzelm@7066
   269
              Some deps => change_thys (update_node name parents (untouch_deps deps, None))
wenzelm@7066
   270
            | None => ());
wenzelm@7066
   271
              load_thy ml time initiators dir name parents;
wenzelm@7066
   272
              false);
wenzelm@7066
   273
      in (visited', (result, name)) end
wenzelm@7066
   274
  end;
wenzelm@6484
   275
wenzelm@7066
   276
fun gen_use_thy req s =
wenzelm@7066
   277
  let val (_, (_, name)) = req [] Path.current ([], s)
wenzelm@7066
   278
  in Context.context (get_theory name) end;
wenzelm@6241
   279
wenzelm@7066
   280
val weak_use_thy = gen_use_thy (require_thy true false false false);
wenzelm@7066
   281
val use_thy      = gen_use_thy (require_thy true false true false);
wenzelm@7066
   282
val update_thy   = gen_use_thy (require_thy true false true true);
wenzelm@7066
   283
val time_use_thy = gen_use_thy (require_thy true true true false);
wenzelm@7066
   284
val use_thy_only = gen_use_thy (require_thy false false true false);
wenzelm@6211
   285
wenzelm@6211
   286
wenzelm@6666
   287
(* remove_thy *)
wenzelm@6666
   288
wenzelm@6666
   289
fun remove_thy name =
wenzelm@6666
   290
  if is_finished name then error (loader_msg "cannot remove finished theory" [name])
wenzelm@6666
   291
  else
wenzelm@6666
   292
    let val succs = thy_graph Graph.all_succs [name] in
wenzelm@6666
   293
      writeln (loader_msg "removing" succs);
wenzelm@6666
   294
      change_thys (Graph.del_nodes succs)
wenzelm@6666
   295
    end;
wenzelm@6666
   296
wenzelm@6666
   297
wenzelm@7066
   298
(* begin / end theory *)
wenzelm@6211
   299
wenzelm@6329
   300
fun begin_theory name parents paths =
wenzelm@6211
   301
  let
wenzelm@6900
   302
    val _ =
wenzelm@6900
   303
      if is_some (lookup_thy name) andalso is_finished name then
wenzelm@6900
   304
        error (loader_msg "cannot change finished theory" [name])
wenzelm@6900
   305
      else ();
wenzelm@6484
   306
    val _ = (map Path.basic parents; seq weak_use_thy parents);
wenzelm@6211
   307
    val theory = PureThy.begin_theory name (map get_theory parents);
wenzelm@6362
   308
    val _ = change_thys (update_node name parents (init_deps [] [], Some theory));
wenzelm@6329
   309
    val use_paths = mapfilter (fn (x, true) => Some x | _ => None) paths;
wenzelm@6329
   310
  in Context.setmp (Some theory) (seq use_path) use_paths; theory end;
wenzelm@6211
   311
wenzelm@6211
   312
fun end_theory theory =
wenzelm@6211
   313
  let val theory' = PureThy.end_theory theory
wenzelm@6211
   314
  in put_theory theory'; theory' end;
wenzelm@6211
   315
wenzelm@6211
   316
wenzelm@6666
   317
(* finish all theories *)
wenzelm@6211
   318
wenzelm@6666
   319
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (None, entry)));
wenzelm@6211
   320
wenzelm@6211
   321
wenzelm@6211
   322
(* register existing theories *)
wenzelm@6211
   323
wenzelm@6211
   324
fun register_theory theory =
wenzelm@6211
   325
  let
wenzelm@6211
   326
    val name = PureThy.get_name theory;
wenzelm@6211
   327
    val parents = Theory.parents_of theory;
wenzelm@6211
   328
    val parent_names = map PureThy.get_name parents;
wenzelm@6211
   329
wenzelm@6211
   330
    fun err txt bads =
wenzelm@6211
   331
      error (loader_msg txt bads ^ "\n" ^ gen_msg "cannot register theory" [name]);
wenzelm@6211
   332
wenzelm@6666
   333
    val nonfinished = filter_out is_finished parent_names;
wenzelm@6211
   334
    fun get_variant (x, y_name) =
wenzelm@6211
   335
      if Theory.eq_thy (x, get_theory y_name) then None
wenzelm@6211
   336
      else Some y_name;
wenzelm@6211
   337
    val variants = mapfilter get_variant (parents ~~ parent_names);
wenzelm@6211
   338
wenzelm@6211
   339
    fun register G =
wenzelm@6362
   340
      (Graph.new_node (name, (None, Some theory)) G
wenzelm@6211
   341
        handle Graph.DUPLICATE _ => err "duplicate theory entry" [])
wenzelm@6211
   342
      |> add_deps name parent_names;
wenzelm@6211
   343
  in
wenzelm@6666
   344
    if not (null nonfinished) then err "non-finished parent theories" nonfinished
wenzelm@6211
   345
    else if not (null variants) then err "different versions of parent theories" variants
wenzelm@6211
   346
    else change_thys register
wenzelm@6211
   347
  end;
wenzelm@6211
   348
wenzelm@6211
   349
wenzelm@6211
   350
(*final declarations of this structure*)
wenzelm@6211
   351
val theory = get_theory;
wenzelm@6211
   352
val names = get_names;
wenzelm@6211
   353
wenzelm@6211
   354
end;
wenzelm@6211
   355
wenzelm@5209
   356
structure BasicThyInfo: BASIC_THY_INFO = ThyInfo;
wenzelm@5209
   357
open BasicThyInfo;