src/Pure/Thy/thy_info.ML
author wenzelm
Mon Aug 16 16:44:24 1999 +0200 (1999-08-16)
changeset 7211 8e4aa9044599
parent 7191 fcce2387ad6d
child 7223 b0198ca65867
permissions -rw-r--r--
fixed thy_only;
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
*)
berghofe@3604
     7
wenzelm@5209
     8
signature BASIC_THY_INFO =
wenzelm@5209
     9
sig
wenzelm@5209
    10
  val theory: string -> theory
wenzelm@6211
    11
  val theory_of_sign: Sign.sg -> theory
wenzelm@6211
    12
  val theory_of_thm: thm -> theory
wenzelm@6241
    13
(*val use: string -> unit*)             (*exported later*)
wenzelm@6241
    14
  val time_use: string -> unit
wenzelm@6211
    15
  val touch_thy: string -> unit
wenzelm@6211
    16
  val use_thy: string -> unit
wenzelm@6211
    17
  val update_thy: string -> unit
wenzelm@6666
    18
  val remove_thy: string -> unit
wenzelm@6211
    19
  val time_use_thy: string -> unit
wenzelm@6527
    20
  val use_thy_only: string -> unit
wenzelm@7099
    21
  val update_thy_only: string -> unit
wenzelm@6211
    22
end;
wenzelm@5209
    23
berghofe@3604
    24
signature THY_INFO =
berghofe@3604
    25
sig
wenzelm@5209
    26
  include BASIC_THY_INFO
wenzelm@7099
    27
  datatype action = Update | Outdate | Remove
wenzelm@7099
    28
  val str_of_action: action -> string
wenzelm@7099
    29
  val add_hook: (action -> string -> unit) -> unit
wenzelm@6211
    30
  val names: unit -> string list
wenzelm@6211
    31
  val get_theory: string -> theory
berghofe@6654
    32
  val get_preds: string -> string list
wenzelm@6241
    33
  val loaded_files: string -> (Path.T * File.info) list
wenzelm@7099
    34
  val touch_all_thys: unit -> unit
wenzelm@6241
    35
  val load_file: bool -> Path.T -> unit
wenzelm@6329
    36
  val use_path: Path.T -> unit
wenzelm@6241
    37
  val use: string -> unit
wenzelm@7191
    38
  val pretend_use: string -> unit
wenzelm@6329
    39
  val begin_theory: string -> string list -> (Path.T * bool) list -> theory
wenzelm@6211
    40
  val end_theory: theory -> theory
wenzelm@6666
    41
  val finish: unit -> unit
wenzelm@6211
    42
  val register_theory: theory -> unit
berghofe@3604
    43
end;
berghofe@3604
    44
wenzelm@6362
    45
structure ThyInfo: THY_INFO =
wenzelm@6211
    46
struct
berghofe@3604
    47
berghofe@3604
    48
wenzelm@7099
    49
(** theory loader actions and hooks **)
wenzelm@7099
    50
wenzelm@7099
    51
datatype action = Update | Outdate | Remove;
wenzelm@7099
    52
val str_of_action = fn Update => "Update" | Outdate => "Outdate" | Remove => "Remove";
wenzelm@7099
    53
wenzelm@7099
    54
local
wenzelm@7099
    55
  val hooks = ref ([]: (action -> string -> unit) list);
wenzelm@7099
    56
in
wenzelm@7099
    57
  fun add_hook f = hooks := f :: ! hooks;
wenzelm@7191
    58
  fun perform action name = seq (fn f => (try (fn () => f action name) (); ())) (! hooks);
wenzelm@7099
    59
end;
wenzelm@7099
    60
wenzelm@7099
    61
wenzelm@7099
    62
wenzelm@6211
    63
(** thy database **)
wenzelm@6211
    64
wenzelm@6211
    65
(* messages *)
wenzelm@6211
    66
wenzelm@6211
    67
fun gen_msg txt [] = txt
wenzelm@6211
    68
  | gen_msg txt names = txt ^ " " ^ commas_quote names;
wenzelm@6211
    69
wenzelm@6211
    70
fun loader_msg txt names = gen_msg ("Theory loader: " ^ txt) names;
wenzelm@6211
    71
wenzelm@6211
    72
val show_path = space_implode " via " o map quote;
wenzelm@6211
    73
fun cycle_msg name names = loader_msg ("cyclic dependency of " ^ show_path (name :: names)) [];
berghofe@3604
    74
berghofe@3604
    75
wenzelm@6666
    76
(* derived graph operations *)
berghofe@3604
    77
wenzelm@6211
    78
fun add_deps name parents G =
wenzelm@6211
    79
  foldl (fn (H, parent) => Graph.add_edge_acyclic (parent, name) H) (G, parents)
wenzelm@6211
    80
    handle Graph.CYCLES namess => error (cat_lines (map (cycle_msg name) namess));
berghofe@3604
    81
wenzelm@6666
    82
fun del_deps name G =
wenzelm@6211
    83
  foldl (fn (H, parent) => Graph.del_edge (parent, name) H) (G, Graph.imm_preds G name);
wenzelm@3976
    84
wenzelm@6211
    85
fun update_node name parents entry G =
wenzelm@6211
    86
  add_deps name parents
wenzelm@6211
    87
    (if can (Graph.get_node G) name then del_deps name G else Graph.new_node (name, entry) G);
berghofe@3604
    88
berghofe@3604
    89
wenzelm@6211
    90
(* thy database *)
wenzelm@6211
    91
wenzelm@6211
    92
type deps =
wenzelm@6211
    93
  {present: bool, outdated: bool,
wenzelm@7191
    94
    master: ThyLoad.master option, files: (Path.T * (Path.T * File.info) option) list};
wenzelm@6211
    95
wenzelm@6241
    96
fun make_deps present outdated master files =
wenzelm@6241
    97
  {present = present, outdated = outdated, master = master, files = files}: deps;
berghofe@3604
    98
wenzelm@7211
    99
fun init_deps master files = Some (make_deps false true master (map (rpair None) files));
wenzelm@7211
   100
wenzelm@7211
   101
wenzelm@6362
   102
type thy = deps option * theory option;
berghofe@3604
   103
wenzelm@6211
   104
local
wenzelm@6362
   105
  val database = ref (Graph.empty: thy Graph.T);
wenzelm@6211
   106
in
wenzelm@6362
   107
  fun get_thys () = ! database;
wenzelm@6362
   108
  fun change_thys f = database := (f (! database));
berghofe@3604
   109
end;
wenzelm@5209
   110
wenzelm@5209
   111
wenzelm@6211
   112
(* access thy graph *)
wenzelm@6211
   113
wenzelm@6211
   114
fun thy_graph f x = f (get_thys ()) x;
wenzelm@6666
   115
fun get_names () = Graph.keys (get_thys ());
wenzelm@6211
   116
wenzelm@6211
   117
wenzelm@6211
   118
(* access thy *)
wenzelm@6211
   119
wenzelm@6211
   120
fun lookup_thy name = Some (thy_graph Graph.get_node name) handle Graph.UNDEFINED _ => None;
wenzelm@6211
   121
wenzelm@6211
   122
fun get_thy name =
wenzelm@6211
   123
  (case lookup_thy name of
wenzelm@6211
   124
    Some thy => thy
wenzelm@6211
   125
  | None => error (loader_msg "nothing known about theory" [name]));
wenzelm@6211
   126
wenzelm@6211
   127
fun change_thy name f = (get_thy name; change_thys (Graph.map_node name f));
wenzelm@6211
   128
wenzelm@6211
   129
wenzelm@6211
   130
(* access deps *)
wenzelm@6211
   131
wenzelm@6211
   132
val lookup_deps = apsome #1 o lookup_thy;
wenzelm@6211
   133
val get_deps = #1 o get_thy;
wenzelm@6211
   134
fun change_deps name f = change_thy name (fn (deps, x) => (f deps, x));
wenzelm@6211
   135
wenzelm@6666
   136
fun is_finished name = is_none (get_deps name);
wenzelm@6211
   137
fun get_files name = (case get_deps name of Some {files, ...} => files | _ => []);
wenzelm@7191
   138
wenzelm@7191
   139
fun loaded_files name =
wenzelm@7191
   140
  (case get_deps name of
wenzelm@7191
   141
    Some {master = Some master, files, ...} => ThyLoad.get_thy master :: mapfilter #2 files
wenzelm@7191
   142
  | Some {files, ...} => mapfilter #2 files
wenzelm@7191
   143
  | None => []);
wenzelm@6211
   144
berghofe@6654
   145
fun get_preds name =
berghofe@6654
   146
  (thy_graph Graph.imm_preds name) handle Graph.UNDEFINED _ =>
berghofe@6654
   147
    error (loader_msg "nothing known about theory" [name]);
berghofe@6654
   148
wenzelm@6211
   149
wenzelm@6211
   150
(* access theory *)
wenzelm@6211
   151
wenzelm@6211
   152
fun get_theory name =
wenzelm@6211
   153
  (case get_thy name of
wenzelm@6362
   154
    (_, Some theory) => theory
wenzelm@6211
   155
  | _ => error (loader_msg "undefined theory entry for" [name]));
wenzelm@6211
   156
wenzelm@6211
   157
val theory_of_sign = get_theory o Sign.name_of;
wenzelm@6211
   158
val theory_of_thm = theory_of_sign o Thm.sign_of_thm;
wenzelm@6211
   159
wenzelm@7099
   160
fun put_theory name theory = change_thy name (fn (deps, _) => (deps, Some theory));
wenzelm@6211
   161
wenzelm@6211
   162
wenzelm@6211
   163
wenzelm@6211
   164
(** thy operations **)
wenzelm@6211
   165
wenzelm@6241
   166
(* maintain 'outdated' flag *)
wenzelm@6211
   167
wenzelm@7099
   168
local
wenzelm@7099
   169
wenzelm@6241
   170
fun is_outdated name =
wenzelm@6241
   171
  (case lookup_deps name of
wenzelm@6241
   172
    Some (Some {outdated, ...}) => outdated
wenzelm@6241
   173
  | _ => false);
wenzelm@6211
   174
wenzelm@6241
   175
fun outdate_thy name =
wenzelm@7099
   176
  if is_finished name orelse is_outdated name then ()
wenzelm@7099
   177
  else (change_deps name (apsome (fn {present, outdated = _, master, files} =>
wenzelm@7099
   178
    make_deps present true master files)); perform Outdate name);
wenzelm@7099
   179
wenzelm@7099
   180
in
wenzelm@6211
   181
wenzelm@6241
   182
fun touch_thy name =
wenzelm@7099
   183
  if is_finished name then warning (loader_msg "tried to touch finished theory" [name])
wenzelm@7099
   184
  else seq outdate_thy (thy_graph Graph.all_succs [name]);
wenzelm@6211
   185
wenzelm@7099
   186
fun touch_all_thys () = seq outdate_thy (get_names ());
wenzelm@6211
   187
wenzelm@7099
   188
end;
wenzelm@6211
   189
wenzelm@6211
   190
wenzelm@6211
   191
(* load_file *)
wenzelm@6211
   192
wenzelm@7191
   193
local
wenzelm@7191
   194
wenzelm@7191
   195
fun may_run_file really path =
wenzelm@6211
   196
  let
wenzelm@7191
   197
    val load = ThyLoad.may_load_file really;
wenzelm@6211
   198
    fun provide name info (deps as Some {present, outdated, master, files}) =
wenzelm@6263
   199
          if exists (equal path o #1) files then
wenzelm@6241
   200
            Some (make_deps present outdated master (overwrite (files, (path, Some info))))
wenzelm@6241
   201
          else (warning (loader_msg "undeclared dependency of theory" [name] ^
wenzelm@6389
   202
            " on file: " ^ quote (Path.pack path)); deps)
wenzelm@6211
   203
      | provide _ _ None = None;
wenzelm@6211
   204
  in
wenzelm@6211
   205
    (case apsome PureThy.get_name (Context.get_context ()) of
wenzelm@7191
   206
      None => (load path; ())
wenzelm@6211
   207
    | Some name =>
wenzelm@7191
   208
        if is_some (lookup_thy name) then change_deps name (provide name (load path))
wenzelm@7191
   209
        else (load path; ()))
wenzelm@6211
   210
  end;
wenzelm@6211
   211
wenzelm@7191
   212
val run_file = may_run_file true;
wenzelm@7191
   213
wenzelm@7191
   214
in
wenzelm@7191
   215
wenzelm@6241
   216
fun load_file false path = run_file path
wenzelm@6241
   217
  | load_file true path =
wenzelm@6241
   218
      let val name = Path.pack path in
wenzelm@6241
   219
        timeit (fn () =>
wenzelm@6241
   220
         (writeln ("\n**** Starting file " ^ quote name ^ " ****");
wenzelm@6241
   221
          run_file path;
wenzelm@6241
   222
          writeln ("**** Finished file " ^ quote name ^ " ****\n")))
wenzelm@6241
   223
      end;
wenzelm@6211
   224
wenzelm@6329
   225
val use_path = load_file false;
wenzelm@6329
   226
val use = use_path o Path.unpack;
wenzelm@6241
   227
val time_use = load_file true o Path.unpack;
wenzelm@7191
   228
val pretend_use = may_run_file false o Path.unpack;
wenzelm@7191
   229
wenzelm@7191
   230
end;
wenzelm@6233
   231
wenzelm@6233
   232
wenzelm@7099
   233
(* load_thy *)
wenzelm@7099
   234
wenzelm@7099
   235
fun required_by [] = ""
wenzelm@7099
   236
  | required_by initiators = " (required by " ^ show_path (rev initiators) ^ ")";
wenzelm@7099
   237
wenzelm@7099
   238
fun load_thy ml time initiators dir name parents =
wenzelm@7099
   239
  let
wenzelm@7099
   240
    val _ = if name mem_string initiators then error (cycle_msg name (rev initiators)) else ();
wenzelm@7099
   241
    val _ = writeln ("Loading theory " ^ quote name ^ required_by initiators);
wenzelm@7099
   242
wenzelm@7099
   243
    val _ = touch_thy name;
wenzelm@7099
   244
    val master = ThyLoad.load_thy dir name ml time;
wenzelm@7099
   245
wenzelm@7099
   246
    val files = get_files name;
wenzelm@7099
   247
    val missing_files = mapfilter (fn (path, None) => Some (Path.pack path) | _ => None) files;
wenzelm@7099
   248
  in
wenzelm@7099
   249
    if null missing_files then ()
wenzelm@7099
   250
    else warning (loader_msg "unresolved dependencies of theory" [name] ^
wenzelm@7191
   251
      "\non file(s): " ^ commas_quote missing_files);
wenzelm@7191
   252
    change_deps name (fn _ => Some (make_deps true false (Some master) files));
wenzelm@7099
   253
    perform Update name
wenzelm@7099
   254
  end;
wenzelm@7099
   255
wenzelm@7099
   256
wenzelm@6211
   257
(* require_thy *)
wenzelm@6211
   258
wenzelm@7211
   259
local
wenzelm@6211
   260
wenzelm@7191
   261
fun load_deps dir name =
wenzelm@7191
   262
  let val (master, (parents, files)) = ThyLoad.deps_thy dir name
wenzelm@7191
   263
  in (Some (init_deps (Some 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@7191
   268
fun current_deps strict dir name =
wenzelm@6211
   269
  (case lookup_deps name of
wenzelm@7191
   270
    None => (false, load_deps dir name)
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@7191
   278
              let val master' = Some (ThyLoad.check_thy dir name) in
wenzelm@7191
   279
                if master <> master' then (false, load_deps dir name)
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@7066
   284
fun require_thy ml time strict keep_strict initiators prfx (visited, str) =
wenzelm@6211
   285
  let
wenzelm@7066
   286
    val path = Path.expand (Path.unpack str);
wenzelm@6484
   287
    val name = Path.pack (Path.base path);
wenzelm@7066
   288
  in
wenzelm@7066
   289
    if name mem_string visited then (visited, (true, name))
wenzelm@7066
   290
    else
wenzelm@7066
   291
      let
wenzelm@7066
   292
        val dir = Path.append prfx (Path.dir path);
wenzelm@7066
   293
        val req_parent =
wenzelm@7211
   294
          require_thy true time (strict andalso keep_strict) keep_strict (name :: initiators) dir;
wenzelm@6484
   295
wenzelm@7191
   296
        val (current, (new_deps, parents)) = current_deps strict dir name handle ERROR =>
wenzelm@7099
   297
          error (loader_msg "the error(s) above occurred while examining theory" [name] ^
wenzelm@7066
   298
            (if null initiators then "" else "\n" ^ required_by initiators));
wenzelm@7066
   299
        val (visited', parent_results) = foldl_map req_parent (name :: visited, parents);
wenzelm@6211
   300
wenzelm@7066
   301
        val result =
wenzelm@7066
   302
          if current andalso forall #1 parent_results then true
wenzelm@7066
   303
          else
wenzelm@7066
   304
            ((case new_deps of
wenzelm@7099
   305
              Some deps => change_thys (update_node name parents (deps, None))
wenzelm@7066
   306
            | None => ());
wenzelm@7099
   307
             load_thy ml time initiators dir name parents;
wenzelm@7099
   308
             false);
wenzelm@7066
   309
      in (visited', (result, name)) end
wenzelm@7066
   310
  end;
wenzelm@6484
   311
wenzelm@7066
   312
fun gen_use_thy req s =
wenzelm@7066
   313
  let val (_, (_, name)) = req [] Path.current ([], s)
wenzelm@7066
   314
  in Context.context (get_theory name) end;
wenzelm@6241
   315
wenzelm@7211
   316
in
wenzelm@7211
   317
wenzelm@7099
   318
val weak_use_thy    = gen_use_thy (require_thy true false false false);
wenzelm@7099
   319
val use_thy         = gen_use_thy (require_thy true false true false);
wenzelm@7099
   320
val time_use_thy    = gen_use_thy (require_thy true true true false);
wenzelm@7099
   321
val use_thy_only    = gen_use_thy (require_thy false false true false);
wenzelm@7099
   322
val update_thy      = gen_use_thy (require_thy true false true true);
wenzelm@7099
   323
val update_thy_only = gen_use_thy (require_thy false false true true);
wenzelm@6211
   324
wenzelm@7211
   325
end;
wenzelm@7211
   326
wenzelm@6211
   327
wenzelm@6666
   328
(* remove_thy *)
wenzelm@6666
   329
wenzelm@6666
   330
fun remove_thy name =
wenzelm@6666
   331
  if is_finished name then error (loader_msg "cannot remove finished theory" [name])
wenzelm@6666
   332
  else
wenzelm@6666
   333
    let val succs = thy_graph Graph.all_succs [name] in
wenzelm@6666
   334
      writeln (loader_msg "removing" succs);
wenzelm@7191
   335
      seq (perform Remove) succs;
wenzelm@7191
   336
      change_thys (Graph.del_nodes succs)
wenzelm@6666
   337
    end;
wenzelm@6666
   338
wenzelm@6666
   339
wenzelm@7066
   340
(* begin / end theory *)
wenzelm@6211
   341
wenzelm@6329
   342
fun begin_theory name parents paths =
wenzelm@6211
   343
  let
wenzelm@6900
   344
    val _ =
wenzelm@6900
   345
      if is_some (lookup_thy name) andalso is_finished name then
wenzelm@6900
   346
        error (loader_msg "cannot change finished theory" [name])
wenzelm@6900
   347
      else ();
wenzelm@6484
   348
    val _ = (map Path.basic parents; seq weak_use_thy parents);
wenzelm@6211
   349
    val theory = PureThy.begin_theory name (map get_theory parents);
wenzelm@7191
   350
    val _ = change_thys (update_node name parents (init_deps None [], Some theory));
wenzelm@6329
   351
    val use_paths = mapfilter (fn (x, true) => Some x | _ => None) paths;
wenzelm@6329
   352
  in Context.setmp (Some theory) (seq use_path) use_paths; theory end;
wenzelm@6211
   353
wenzelm@6211
   354
fun end_theory theory =
wenzelm@7099
   355
  let
wenzelm@7099
   356
    val theory' = PureThy.end_theory theory;
wenzelm@7099
   357
    val name = PureThy.get_name theory;
wenzelm@7099
   358
  in put_theory name theory'; theory' end;
wenzelm@6211
   359
wenzelm@6211
   360
wenzelm@6666
   361
(* finish all theories *)
wenzelm@6211
   362
wenzelm@6666
   363
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (None, entry)));
wenzelm@6211
   364
wenzelm@6211
   365
wenzelm@6211
   366
(* register existing theories *)
wenzelm@6211
   367
wenzelm@6211
   368
fun register_theory theory =
wenzelm@6211
   369
  let
wenzelm@6211
   370
    val name = PureThy.get_name theory;
wenzelm@6211
   371
    val parents = Theory.parents_of theory;
wenzelm@6211
   372
    val parent_names = map PureThy.get_name parents;
wenzelm@6211
   373
wenzelm@6211
   374
    fun err txt bads =
wenzelm@6211
   375
      error (loader_msg txt bads ^ "\n" ^ gen_msg "cannot register theory" [name]);
wenzelm@6211
   376
wenzelm@6666
   377
    val nonfinished = filter_out is_finished parent_names;
wenzelm@6211
   378
    fun get_variant (x, y_name) =
wenzelm@6211
   379
      if Theory.eq_thy (x, get_theory y_name) then None
wenzelm@6211
   380
      else Some y_name;
wenzelm@6211
   381
    val variants = mapfilter get_variant (parents ~~ parent_names);
wenzelm@6211
   382
wenzelm@6211
   383
    fun register G =
wenzelm@6362
   384
      (Graph.new_node (name, (None, Some theory)) G
wenzelm@6211
   385
        handle Graph.DUPLICATE _ => err "duplicate theory entry" [])
wenzelm@6211
   386
      |> add_deps name parent_names;
wenzelm@6211
   387
  in
wenzelm@6666
   388
    if not (null nonfinished) then err "non-finished parent theories" nonfinished
wenzelm@6211
   389
    else if not (null variants) then err "different versions of parent theories" variants
wenzelm@7099
   390
    else (change_thys register; perform Update name)
wenzelm@6211
   391
  end;
wenzelm@6211
   392
wenzelm@6211
   393
wenzelm@6211
   394
(*final declarations of this structure*)
wenzelm@6211
   395
val theory = get_theory;
wenzelm@6211
   396
val names = get_names;
wenzelm@6211
   397
wenzelm@6211
   398
end;
wenzelm@6211
   399
wenzelm@5209
   400
structure BasicThyInfo: BASIC_THY_INFO = ThyInfo;
wenzelm@5209
   401
open BasicThyInfo;