src/Pure/Thy/thy_load.ML
author wenzelm
Thu Mar 03 18:10:28 2011 +0100 (2011-03-03 ago)
changeset 41886 aa8dce9ab8a9
parent 41548 bd0bebf93fa6
child 41944 b97091ae583a
permissions -rw-r--r--
discontinued legacy load path;
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@41886
    18
  val check_file: Path.T -> Path.T -> Path.T * file_ident
wenzelm@40741
    19
  val check_thy: Path.T -> string -> Path.T * file_ident
wenzelm@24189
    20
  val deps_thy: Path.T -> string ->
wenzelm@40741
    21
   {master: Path.T * file_ident, text: string, imports: string list, uses: Path.T list}
wenzelm@37949
    22
  val loaded_files: theory -> Path.T list
wenzelm@37949
    23
  val all_current: theory -> bool
wenzelm@37949
    24
  val provide_file: Path.T -> theory -> theory
wenzelm@37949
    25
  val use_ml: Path.T -> unit
wenzelm@37949
    26
  val exec_ml: Path.T -> generic_theory -> generic_theory
wenzelm@41548
    27
  val begin_theory: Path.T -> string -> string list -> theory list -> (Path.T * bool) list -> theory
wenzelm@6168
    28
end;
wenzelm@6168
    29
wenzelm@37216
    30
structure Thy_Load: THY_LOAD =
wenzelm@6168
    31
struct
wenzelm@6168
    32
wenzelm@40741
    33
(* file identification *)
wenzelm@40741
    34
wenzelm@40741
    35
local
wenzelm@40742
    36
  val file_ident_cache =
wenzelm@40742
    37
    Synchronized.var "file_ident_cache"
wenzelm@40742
    38
      (Symtab.empty: {time_stamp: string, id: string} Symtab.table);
wenzelm@40741
    39
in
wenzelm@40741
    40
wenzelm@40741
    41
fun check_cache (path, time) =
wenzelm@40742
    42
  (case Symtab.lookup (Synchronized.value file_ident_cache) path of
wenzelm@40741
    43
    NONE => NONE
wenzelm@40741
    44
  | SOME {time_stamp, id} => if time_stamp = time then SOME id else NONE);
wenzelm@40741
    45
wenzelm@40742
    46
fun update_cache (path, (time, id)) =
wenzelm@40742
    47
  Synchronized.change file_ident_cache
wenzelm@40742
    48
    (Symtab.update (path, {time_stamp = time, id = id}));
wenzelm@40741
    49
wenzelm@40741
    50
end;
wenzelm@40741
    51
wenzelm@40741
    52
datatype file_ident = File_Ident of string;
wenzelm@40741
    53
wenzelm@40741
    54
fun pretty_file_ident (File_Ident s) = Pretty.str (quote s);
wenzelm@40741
    55
wenzelm@40741
    56
fun file_ident path =
wenzelm@40741
    57
  let val physical_path = perhaps (try OS.FileSys.fullPath) (File.platform_path path) in
wenzelm@40741
    58
    (case try (Time.toString o OS.FileSys.modTime) physical_path of
wenzelm@40741
    59
      NONE => NONE
wenzelm@40741
    60
    | SOME mod_time => SOME (File_Ident
wenzelm@40741
    61
        (case getenv "ISABELLE_FILE_IDENT" of
wenzelm@40741
    62
          "" => physical_path ^ ": " ^ mod_time
wenzelm@40741
    63
        | cmd =>
wenzelm@40741
    64
            (case check_cache (physical_path, mod_time) of
wenzelm@40741
    65
              SOME id => id
wenzelm@40741
    66
            | NONE =>
wenzelm@40741
    67
                let
wenzelm@40741
    68
                  val (id, rc) =  (*potentially slow*)
wenzelm@40741
    69
                    bash_output
wenzelm@40741
    70
                      ("\"$ISABELLE_HOME/lib/scripts/fileident\" " ^ File.shell_quote physical_path);
wenzelm@40741
    71
                in
wenzelm@40741
    72
                  if id <> "" andalso rc = 0 then (update_cache (physical_path, (mod_time, id)); id)
wenzelm@40741
    73
                  else error ("Failed to identify file " ^ quote (Path.implode path) ^ " by " ^ cmd)
wenzelm@40741
    74
                end))))
wenzelm@40741
    75
  end;
wenzelm@40741
    76
wenzelm@40741
    77
wenzelm@37949
    78
(* manage source files *)
wenzelm@37949
    79
wenzelm@37949
    80
type files =
wenzelm@40741
    81
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@41548
    82
  imports: string list,  (*source specification of imports*)
wenzelm@40741
    83
  required: Path.T list,  (*source path*)
wenzelm@40741
    84
  provided: (Path.T * (Path.T * file_ident)) list};  (*source path, physical path, identifier*)
wenzelm@37949
    85
wenzelm@41548
    86
fun make_files (master_dir, imports, required, provided): files =
wenzelm@41548
    87
 {master_dir = master_dir, imports = imports, required = required, provided = provided};
wenzelm@37949
    88
wenzelm@37949
    89
structure Files = Theory_Data
wenzelm@37949
    90
(
wenzelm@37949
    91
  type T = files;
wenzelm@41548
    92
  val empty = make_files (Path.current, [], [], []);
wenzelm@37949
    93
  fun extend _ = empty;
wenzelm@37949
    94
  fun merge _ = empty;
wenzelm@37949
    95
);
wenzelm@37949
    96
wenzelm@37949
    97
fun map_files f =
wenzelm@41548
    98
  Files.map (fn {master_dir, imports, required, provided} =>
wenzelm@41548
    99
    make_files (f (master_dir, imports, required, provided)));
wenzelm@37949
   100
wenzelm@37949
   101
wenzelm@37949
   102
val master_directory = #master_dir o Files.get;
wenzelm@41548
   103
val imports_of = #imports o Files.get;
wenzelm@37949
   104
wenzelm@41548
   105
fun put_deps dir imports = map_files (fn _ => (dir, imports, [], []));
wenzelm@37949
   106
wenzelm@37949
   107
fun require src_path =
wenzelm@41548
   108
  map_files (fn (master_dir, imports, required, provided) =>
wenzelm@37949
   109
    if member (op =) required src_path then
wenzelm@37949
   110
      error ("Duplicate source file dependency: " ^ Path.implode src_path)
wenzelm@41548
   111
    else (master_dir, imports, src_path :: required, provided));
wenzelm@37949
   112
wenzelm@37978
   113
fun provide (src_path, path_id) =
wenzelm@41548
   114
  map_files (fn (master_dir, imports, required, provided) =>
wenzelm@37949
   115
    if AList.defined (op =) provided src_path then
wenzelm@37949
   116
      error ("Duplicate resolution of source file dependency: " ^ Path.implode src_path)
wenzelm@41548
   117
    else (master_dir, imports, required, (src_path, path_id) :: provided));
wenzelm@37949
   118
wenzelm@37949
   119
wenzelm@41886
   120
(* stateful master path *)
wenzelm@6168
   121
wenzelm@37977
   122
local
wenzelm@37977
   123
  val master_path = Unsynchronized.ref Path.current;
wenzelm@37977
   124
in
wenzelm@6168
   125
wenzelm@37977
   126
fun set_master_path path = master_path := path;
wenzelm@37977
   127
fun get_master_path () = ! master_path;
wenzelm@37977
   128
wenzelm@23944
   129
end;
wenzelm@23944
   130
wenzelm@6168
   131
wenzelm@23872
   132
(* check files *)
wenzelm@6232
   133
wenzelm@41886
   134
fun get_file dir src_path =
wenzelm@6232
   135
  let
wenzelm@6232
   136
    val path = Path.expand src_path;
wenzelm@23872
   137
    val _ = Path.is_current path andalso error "Bad file specification";
wenzelm@41886
   138
    val full_path = File.full_path (Path.append dir path);
wenzelm@37939
   139
  in
wenzelm@41886
   140
    (case file_ident full_path of
wenzelm@41886
   141
      NONE => NONE
wenzelm@41886
   142
    | SOME id => SOME (full_path, id))
wenzelm@37939
   143
  end;
wenzelm@6168
   144
wenzelm@41886
   145
fun check_file dir file =
wenzelm@41886
   146
  (case get_file dir file of
wenzelm@41886
   147
    SOME path_id => path_id
wenzelm@38133
   148
  | NONE => error ("Could not find file " ^ quote (Path.implode file) ^
wenzelm@41886
   149
      "\nin " ^ quote (Path.implode dir)));
wenzelm@37939
   150
wenzelm@41886
   151
fun check_thy dir name =
wenzelm@41886
   152
  check_file dir (Thy_Header.thy_path name);
wenzelm@6232
   153
wenzelm@6232
   154
wenzelm@23872
   155
(* theory deps *)
wenzelm@7940
   156
wenzelm@38133
   157
fun deps_thy master_dir name =
wenzelm@23872
   158
  let
wenzelm@38133
   159
    val master as (thy_path, _) = check_thy master_dir name;
wenzelm@37978
   160
    val text = File.read thy_path;
wenzelm@37978
   161
    val (name', imports, uses) = Thy_Header.read (Path.position thy_path) text;
wenzelm@37950
   162
    val _ = Thy_Header.consistent_name name name';
wenzelm@24189
   163
    val uses = map (Path.explode o #1) uses;
wenzelm@24065
   164
  in {master = master, text = text, imports = imports, uses = uses} end;
wenzelm@6484
   165
wenzelm@6168
   166
wenzelm@37949
   167
wenzelm@37949
   168
(* loaded files *)
wenzelm@37949
   169
wenzelm@37949
   170
val loaded_files = map (#1 o #2) o #provided o Files.get;
wenzelm@6168
   171
wenzelm@37949
   172
fun check_loaded thy =
wenzelm@37949
   173
  let
wenzelm@37949
   174
    val {required, provided, ...} = Files.get thy;
wenzelm@37949
   175
    val provided_paths = map #1 provided;
wenzelm@37949
   176
    val _ =
wenzelm@37949
   177
      (case subtract (op =) provided_paths required of
wenzelm@37949
   178
        [] => NONE
wenzelm@37949
   179
      | bad => error ("Pending source file dependencies: " ^ commas (map Path.implode (rev bad))));
wenzelm@37949
   180
    val _ =
wenzelm@37949
   181
      (case subtract (op =) required provided_paths of
wenzelm@37949
   182
        [] => NONE
wenzelm@37949
   183
      | bad => error ("Undeclared source file dependencies: " ^ commas (map Path.implode (rev bad))));
wenzelm@37949
   184
  in () end;
wenzelm@37949
   185
wenzelm@37949
   186
fun all_current thy =
wenzelm@37939
   187
  let
wenzelm@37949
   188
    val {master_dir, provided, ...} = Files.get thy;
wenzelm@37978
   189
    fun current (src_path, (_, id)) =
wenzelm@41886
   190
      (case get_file master_dir src_path of
wenzelm@37949
   191
        NONE => false
wenzelm@41886
   192
      | SOME (_, id') => id = id');
wenzelm@37949
   193
  in can check_loaded thy andalso forall current provided end;
wenzelm@37949
   194
wenzelm@37952
   195
val _ = Context.>> (Context.map_theory (Theory.at_end (fn thy => (check_loaded thy; NONE))));
wenzelm@37952
   196
wenzelm@37949
   197
wenzelm@37949
   198
(* provide files *)
wenzelm@37949
   199
wenzelm@37949
   200
fun provide_file src_path thy =
wenzelm@41886
   201
  provide (src_path, check_file (master_directory thy) src_path) thy;
wenzelm@37949
   202
wenzelm@37949
   203
fun use_ml src_path =
wenzelm@37949
   204
  if is_none (Context.thread_data ()) then
wenzelm@41886
   205
    ML_Context.eval_file (#1 (check_file Path.current src_path))
wenzelm@37949
   206
  else
wenzelm@37949
   207
    let
wenzelm@37949
   208
      val thy = ML_Context.the_global_context ();
wenzelm@41886
   209
      val (path, id) = check_file (master_directory thy) src_path;
wenzelm@37949
   210
wenzelm@37949
   211
      val _ = ML_Context.eval_file path;
wenzelm@37949
   212
      val _ = Context.>> Local_Theory.propagate_ml_env;
wenzelm@37949
   213
wenzelm@37978
   214
      val provide = provide (src_path, (path, id));
wenzelm@38757
   215
      val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
wenzelm@37978
   216
    in () end;
wenzelm@37949
   217
wenzelm@37949
   218
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
wenzelm@6168
   219
wenzelm@37977
   220
wenzelm@37977
   221
(* begin theory *)
wenzelm@37977
   222
wenzelm@41548
   223
fun begin_theory dir name imports parents uses =
wenzelm@37977
   224
  Theory.begin_theory name parents
wenzelm@41548
   225
  |> put_deps dir imports
wenzelm@37977
   226
  |> fold (require o fst) uses
wenzelm@37977
   227
  |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
wenzelm@37977
   228
  |> Theory.checkpoint;
wenzelm@37977
   229
wenzelm@6168
   230
end;
wenzelm@6168
   231