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