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