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