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