src/Pure/Thy/thy_load.ML
author wenzelm
Fri Jan 14 13:58:07 2011 +0100 (2011-01-14 ago)
changeset 41548 bd0bebf93fa6
parent 41414 00b2b6716ed8
child 41886 aa8dce9ab8a9
permissions -rw-r--r--
Thy_Load.begin_theory: maintain source specification of imports;
Thy_Info.register_thy: reconstruct deps using original imports (important for ProofGeneral.inform_file_processed);
wenzelm@6168
     1
(*  Title:      Pure/Thy/thy_load.ML
wenzelm@6168
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@6168
     3
wenzelm@37939
     4
Loading files that contribute to a theory, including global load path
wenzelm@37939
     5
management.
wenzelm@6168
     6
*)
wenzelm@6168
     7
wenzelm@6168
     8
signature THY_LOAD =
wenzelm@6168
     9
sig
wenzelm@40741
    10
  eqtype file_ident
wenzelm@40741
    11
  val pretty_file_ident: file_ident -> Pretty.T
wenzelm@40741
    12
  val file_ident: Path.T -> file_ident option
wenzelm@37977
    13
  val set_master_path: Path.T -> unit
wenzelm@37977
    14
  val get_master_path: unit -> Path.T
wenzelm@37949
    15
  val master_directory: theory -> Path.T
wenzelm@41548
    16
  val imports_of: theory -> string list
wenzelm@40741
    17
  val provide: Path.T * (Path.T * file_ident) -> theory -> theory
wenzelm@41414
    18
  val legacy_show_path: unit -> string list
wenzelm@41414
    19
  val legacy_add_path: string -> unit
wenzelm@41414
    20
  val legacy_path_add: string -> unit
wenzelm@41414
    21
  val legacy_del_path: string -> unit
wenzelm@41414
    22
  val legacy_reset_path: unit -> unit
wenzelm@40741
    23
  val check_file: Path.T list -> Path.T -> Path.T * file_ident
wenzelm@40741
    24
  val check_thy: Path.T -> string -> Path.T * file_ident
wenzelm@24189
    25
  val deps_thy: Path.T -> string ->
wenzelm@40741
    26
   {master: Path.T * file_ident, text: string, imports: string list, uses: Path.T list}
wenzelm@37949
    27
  val loaded_files: theory -> Path.T list
wenzelm@37949
    28
  val all_current: theory -> bool
wenzelm@37949
    29
  val provide_file: Path.T -> theory -> theory
wenzelm@37949
    30
  val use_ml: Path.T -> unit
wenzelm@37949
    31
  val exec_ml: Path.T -> generic_theory -> generic_theory
wenzelm@41548
    32
  val begin_theory: Path.T -> string -> string list -> theory list -> (Path.T * bool) list -> theory
wenzelm@6168
    33
end;
wenzelm@6168
    34
wenzelm@37216
    35
structure Thy_Load: THY_LOAD =
wenzelm@6168
    36
struct
wenzelm@6168
    37
wenzelm@40741
    38
(* file identification *)
wenzelm@40741
    39
wenzelm@40741
    40
local
wenzelm@40742
    41
  val file_ident_cache =
wenzelm@40742
    42
    Synchronized.var "file_ident_cache"
wenzelm@40742
    43
      (Symtab.empty: {time_stamp: string, id: string} Symtab.table);
wenzelm@40741
    44
in
wenzelm@40741
    45
wenzelm@40741
    46
fun check_cache (path, time) =
wenzelm@40742
    47
  (case Symtab.lookup (Synchronized.value file_ident_cache) path of
wenzelm@40741
    48
    NONE => NONE
wenzelm@40741
    49
  | SOME {time_stamp, id} => if time_stamp = time then SOME id else NONE);
wenzelm@40741
    50
wenzelm@40742
    51
fun update_cache (path, (time, id)) =
wenzelm@40742
    52
  Synchronized.change file_ident_cache
wenzelm@40742
    53
    (Symtab.update (path, {time_stamp = time, id = id}));
wenzelm@40741
    54
wenzelm@40741
    55
end;
wenzelm@40741
    56
wenzelm@40741
    57
datatype file_ident = File_Ident of string;
wenzelm@40741
    58
wenzelm@40741
    59
fun pretty_file_ident (File_Ident s) = Pretty.str (quote s);
wenzelm@40741
    60
wenzelm@40741
    61
fun file_ident path =
wenzelm@40741
    62
  let val physical_path = perhaps (try OS.FileSys.fullPath) (File.platform_path path) in
wenzelm@40741
    63
    (case try (Time.toString o OS.FileSys.modTime) physical_path of
wenzelm@40741
    64
      NONE => NONE
wenzelm@40741
    65
    | SOME mod_time => SOME (File_Ident
wenzelm@40741
    66
        (case getenv "ISABELLE_FILE_IDENT" of
wenzelm@40741
    67
          "" => physical_path ^ ": " ^ mod_time
wenzelm@40741
    68
        | cmd =>
wenzelm@40741
    69
            (case check_cache (physical_path, mod_time) of
wenzelm@40741
    70
              SOME id => id
wenzelm@40741
    71
            | NONE =>
wenzelm@40741
    72
                let
wenzelm@40741
    73
                  val (id, rc) =  (*potentially slow*)
wenzelm@40741
    74
                    bash_output
wenzelm@40741
    75
                      ("\"$ISABELLE_HOME/lib/scripts/fileident\" " ^ File.shell_quote physical_path);
wenzelm@40741
    76
                in
wenzelm@40741
    77
                  if id <> "" andalso rc = 0 then (update_cache (physical_path, (mod_time, id)); id)
wenzelm@40741
    78
                  else error ("Failed to identify file " ^ quote (Path.implode path) ^ " by " ^ cmd)
wenzelm@40741
    79
                end))))
wenzelm@40741
    80
  end;
wenzelm@40741
    81
wenzelm@40741
    82
wenzelm@37949
    83
(* manage source files *)
wenzelm@37949
    84
wenzelm@37949
    85
type files =
wenzelm@40741
    86
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@41548
    87
  imports: string list,  (*source specification of imports*)
wenzelm@40741
    88
  required: Path.T list,  (*source path*)
wenzelm@40741
    89
  provided: (Path.T * (Path.T * file_ident)) list};  (*source path, physical path, identifier*)
wenzelm@37949
    90
wenzelm@41548
    91
fun make_files (master_dir, imports, required, provided): files =
wenzelm@41548
    92
 {master_dir = master_dir, imports = imports, required = required, provided = provided};
wenzelm@37949
    93
wenzelm@37949
    94
structure Files = Theory_Data
wenzelm@37949
    95
(
wenzelm@37949
    96
  type T = files;
wenzelm@41548
    97
  val empty = make_files (Path.current, [], [], []);
wenzelm@37949
    98
  fun extend _ = empty;
wenzelm@37949
    99
  fun merge _ = empty;
wenzelm@37949
   100
);
wenzelm@37949
   101
wenzelm@37949
   102
fun map_files f =
wenzelm@41548
   103
  Files.map (fn {master_dir, imports, required, provided} =>
wenzelm@41548
   104
    make_files (f (master_dir, imports, required, provided)));
wenzelm@37949
   105
wenzelm@37949
   106
wenzelm@37949
   107
val master_directory = #master_dir o Files.get;
wenzelm@41548
   108
val imports_of = #imports o Files.get;
wenzelm@37949
   109
wenzelm@41548
   110
fun put_deps dir imports = map_files (fn _ => (dir, imports, [], []));
wenzelm@37949
   111
wenzelm@37949
   112
fun require src_path =
wenzelm@41548
   113
  map_files (fn (master_dir, imports, required, provided) =>
wenzelm@37949
   114
    if member (op =) required src_path then
wenzelm@37949
   115
      error ("Duplicate source file dependency: " ^ Path.implode src_path)
wenzelm@41548
   116
    else (master_dir, imports, src_path :: required, provided));
wenzelm@37949
   117
wenzelm@37978
   118
fun provide (src_path, path_id) =
wenzelm@41548
   119
  map_files (fn (master_dir, imports, required, provided) =>
wenzelm@37949
   120
    if AList.defined (op =) provided src_path then
wenzelm@37949
   121
      error ("Duplicate resolution of source file dependency: " ^ Path.implode src_path)
wenzelm@41548
   122
    else (master_dir, imports, required, (src_path, path_id) :: provided));
wenzelm@37949
   123
wenzelm@37949
   124
wenzelm@37977
   125
(* maintain default paths *)
wenzelm@6168
   126
wenzelm@37977
   127
local
wenzelm@40742
   128
  val load_path = Synchronized.var "load_path" [Path.current];
wenzelm@37977
   129
  val master_path = Unsynchronized.ref Path.current;
wenzelm@37977
   130
in
wenzelm@6168
   131
wenzelm@41414
   132
fun legacy_show_path () = map Path.implode (Synchronized.value load_path);
wenzelm@23944
   133
wenzelm@41414
   134
fun legacy_del_path s = Synchronized.change load_path (remove (op =) (Path.explode s));
wenzelm@33221
   135
wenzelm@41414
   136
fun legacy_add_path s = Synchronized.change load_path (update (op =) (Path.explode s));
wenzelm@33221
   137
wenzelm@41414
   138
fun legacy_path_add s =
wenzelm@40742
   139
  Synchronized.change load_path (fn path =>
wenzelm@40742
   140
    let val p = Path.explode s
wenzelm@40742
   141
    in remove (op =) p path @ [p] end);
wenzelm@33221
   142
wenzelm@41414
   143
fun legacy_reset_path () = Synchronized.change load_path (K [Path.current]);
wenzelm@6168
   144
wenzelm@23944
   145
fun search_path dir path =
wenzelm@40742
   146
  distinct (op =)
wenzelm@40742
   147
    (dir :: (if Path.is_basic path then (Synchronized.value load_path) else [Path.current]));
wenzelm@23944
   148
wenzelm@37977
   149
fun set_master_path path = master_path := path;
wenzelm@37977
   150
fun get_master_path () = ! master_path;
wenzelm@37977
   151
wenzelm@23944
   152
end;
wenzelm@23944
   153
wenzelm@6168
   154
wenzelm@23872
   155
(* check files *)
wenzelm@6232
   156
wenzelm@38133
   157
fun get_file dirs src_path =
wenzelm@6232
   158
  let
wenzelm@6232
   159
    val path = Path.expand src_path;
wenzelm@23872
   160
    val _ = Path.is_current path andalso error "Bad file specification";
wenzelm@37939
   161
  in
wenzelm@38133
   162
    dirs |> get_first (fn dir =>
wenzelm@38133
   163
      let val full_path = File.full_path (Path.append dir path) in
wenzelm@40741
   164
        (case file_ident full_path of
wenzelm@38133
   165
          NONE => NONE
wenzelm@41412
   166
        | SOME id => SOME (dir, (full_path, id)))
wenzelm@38133
   167
      end)
wenzelm@37939
   168
  end;
wenzelm@6168
   169
wenzelm@38133
   170
fun check_file dirs file =
wenzelm@38133
   171
  (case get_file dirs file of
wenzelm@41412
   172
    SOME (_, path_id) =>
wenzelm@41412
   173
     (if is_some (get_file [hd dirs] file) then ()
wenzelm@41412
   174
      else
wenzelm@41412
   175
        legacy_feature ("File " ^ quote (Path.implode file) ^
wenzelm@41412
   176
          " located via secondary search path: " ^
wenzelm@41412
   177
          quote (Path.implode (#1 (the (get_file (tl dirs) file)))));
wenzelm@41412
   178
      path_id)
wenzelm@38133
   179
  | NONE => error ("Could not find file " ^ quote (Path.implode file) ^
wenzelm@38133
   180
      "\nin " ^ commas_quote (map Path.implode dirs)));
wenzelm@37939
   181
wenzelm@38133
   182
fun check_thy master_dir name =
wenzelm@38133
   183
  let
wenzelm@38133
   184
    val thy_file = Thy_Header.thy_path name;
wenzelm@38133
   185
    val dirs = search_path master_dir thy_file;
wenzelm@38133
   186
  in check_file dirs thy_file end;
wenzelm@6232
   187
wenzelm@6232
   188
wenzelm@23872
   189
(* theory deps *)
wenzelm@7940
   190
wenzelm@38133
   191
fun deps_thy master_dir name =
wenzelm@23872
   192
  let
wenzelm@38133
   193
    val master as (thy_path, _) = check_thy master_dir name;
wenzelm@37978
   194
    val text = File.read thy_path;
wenzelm@37978
   195
    val (name', imports, uses) = Thy_Header.read (Path.position thy_path) text;
wenzelm@37950
   196
    val _ = Thy_Header.consistent_name name name';
wenzelm@24189
   197
    val uses = map (Path.explode o #1) uses;
wenzelm@24065
   198
  in {master = master, text = text, imports = imports, uses = uses} end;
wenzelm@6484
   199
wenzelm@6168
   200
wenzelm@37949
   201
wenzelm@37949
   202
(* loaded files *)
wenzelm@37949
   203
wenzelm@37949
   204
val loaded_files = map (#1 o #2) o #provided o Files.get;
wenzelm@6168
   205
wenzelm@37949
   206
fun check_loaded thy =
wenzelm@37949
   207
  let
wenzelm@37949
   208
    val {required, provided, ...} = Files.get thy;
wenzelm@37949
   209
    val provided_paths = map #1 provided;
wenzelm@37949
   210
    val _ =
wenzelm@37949
   211
      (case subtract (op =) provided_paths required of
wenzelm@37949
   212
        [] => NONE
wenzelm@37949
   213
      | bad => error ("Pending source file dependencies: " ^ commas (map Path.implode (rev bad))));
wenzelm@37949
   214
    val _ =
wenzelm@37949
   215
      (case subtract (op =) required provided_paths of
wenzelm@37949
   216
        [] => NONE
wenzelm@37949
   217
      | bad => error ("Undeclared source file dependencies: " ^ commas (map Path.implode (rev bad))));
wenzelm@37949
   218
  in () end;
wenzelm@37949
   219
wenzelm@37949
   220
fun all_current thy =
wenzelm@37939
   221
  let
wenzelm@37949
   222
    val {master_dir, provided, ...} = Files.get thy;
wenzelm@37978
   223
    fun current (src_path, (_, id)) =
wenzelm@38133
   224
      (case get_file [master_dir] src_path of
wenzelm@37949
   225
        NONE => false
wenzelm@41412
   226
      | SOME (_, (_, id')) => id = id');
wenzelm@37949
   227
  in can check_loaded thy andalso forall current provided end;
wenzelm@37949
   228
wenzelm@37952
   229
val _ = Context.>> (Context.map_theory (Theory.at_end (fn thy => (check_loaded thy; NONE))));
wenzelm@37952
   230
wenzelm@37949
   231
wenzelm@37949
   232
(* provide files *)
wenzelm@37949
   233
wenzelm@37949
   234
fun provide_file src_path thy =
wenzelm@38133
   235
  provide (src_path, check_file [master_directory thy] src_path) thy;
wenzelm@37949
   236
wenzelm@37949
   237
fun use_ml src_path =
wenzelm@37949
   238
  if is_none (Context.thread_data ()) then
wenzelm@38133
   239
    ML_Context.eval_file (#1 (check_file [Path.current] src_path))
wenzelm@37949
   240
  else
wenzelm@37949
   241
    let
wenzelm@37949
   242
      val thy = ML_Context.the_global_context ();
wenzelm@38133
   243
      val (path, id) = check_file [master_directory thy] src_path;
wenzelm@37949
   244
wenzelm@37949
   245
      val _ = ML_Context.eval_file path;
wenzelm@37949
   246
      val _ = Context.>> Local_Theory.propagate_ml_env;
wenzelm@37949
   247
wenzelm@37978
   248
      val provide = provide (src_path, (path, id));
wenzelm@38757
   249
      val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
wenzelm@37978
   250
    in () end;
wenzelm@37949
   251
wenzelm@37949
   252
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
wenzelm@6168
   253
wenzelm@37977
   254
wenzelm@37977
   255
(* begin theory *)
wenzelm@37977
   256
wenzelm@41548
   257
fun begin_theory dir name imports parents uses =
wenzelm@37977
   258
  Theory.begin_theory name parents
wenzelm@41548
   259
  |> put_deps dir imports
wenzelm@37977
   260
  |> fold (require o fst) uses
wenzelm@37977
   261
  |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
wenzelm@37977
   262
  |> Theory.checkpoint;
wenzelm@37977
   263
wenzelm@6168
   264
end;
wenzelm@6168
   265