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