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