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