src/Pure/Thy/thy_load.ML
author wenzelm
Sat Jul 24 12:14:53 2010 +0200 (2010-07-24 ago)
changeset 37949 48a874444164
parent 37943 3cbd7fa164b1
child 37950 bc285d91041e
permissions -rw-r--r--
moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
theory loader: reduced warnings -- thy database can be bypassed in certain situations;
Proof/Local_Theory.propagate_ml_env;
ML use function: propagate ML environment as well;
pervasive type generic_theory;
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@37949
    20
  val master_directory: theory -> Path.T
wenzelm@37949
    21
  val init: Path.T -> theory -> theory
wenzelm@37949
    22
  val require: Path.T -> theory -> theory
wenzelm@37949
    23
  val provide: Path.T * (Path.T * File.ident) -> theory -> theory
wenzelm@6205
    24
  val ml_path: string -> Path.T
wenzelm@7901
    25
  val thy_path: string -> Path.T
wenzelm@29418
    26
  val split_thy_path: Path.T -> Path.T * string
wenzelm@37937
    27
  val consistent_name: string -> string -> unit
wenzelm@37939
    28
  val test_file: Path.T -> Path.T -> (Path.T * File.ident) option
wenzelm@37939
    29
  val check_file: Path.T -> Path.T -> Path.T * File.ident
wenzelm@24189
    30
  val check_thy: Path.T -> string -> Path.T * File.ident
wenzelm@24189
    31
  val deps_thy: Path.T -> string ->
wenzelm@37902
    32
   {master: Path.T * File.ident, text: string, imports: string list, uses: Path.T list}
wenzelm@37949
    33
  val loaded_files: theory -> Path.T list
wenzelm@37949
    34
  val check_loaded: theory -> unit
wenzelm@37949
    35
  val all_current: theory -> bool
wenzelm@37949
    36
  val provide_file: Path.T -> theory -> theory
wenzelm@37949
    37
  val use_ml: Path.T -> unit
wenzelm@37949
    38
  val exec_ml: Path.T -> generic_theory -> generic_theory
wenzelm@6168
    39
end;
wenzelm@6168
    40
wenzelm@37216
    41
structure Thy_Load: THY_LOAD =
wenzelm@6168
    42
struct
wenzelm@6168
    43
wenzelm@37949
    44
(* manage source files *)
wenzelm@37949
    45
wenzelm@37949
    46
type files =
wenzelm@37949
    47
 {master_dir: Path.T,                                 (*master directory of theory source*)
wenzelm@37949
    48
  required: Path.T list,                              (*source path*)
wenzelm@37949
    49
  provided: (Path.T * (Path.T * File.ident)) list};   (*source path, physical path, identifier*)
wenzelm@37949
    50
wenzelm@37949
    51
fun make_files (master_dir, required, provided): files =
wenzelm@37949
    52
 {master_dir = master_dir, required = required, provided = provided};
wenzelm@37949
    53
wenzelm@37949
    54
structure Files = Theory_Data
wenzelm@37949
    55
(
wenzelm@37949
    56
  type T = files;
wenzelm@37949
    57
  val empty = make_files (Path.current, [], []);
wenzelm@37949
    58
  fun extend _ = empty;
wenzelm@37949
    59
  fun merge _ = empty;
wenzelm@37949
    60
);
wenzelm@37949
    61
wenzelm@37949
    62
fun map_files f =
wenzelm@37949
    63
  Files.map (fn {master_dir, required, provided} =>
wenzelm@37949
    64
    make_files (f (master_dir, required, provided)));
wenzelm@37949
    65
wenzelm@37949
    66
wenzelm@37949
    67
val master_directory = #master_dir o Files.get;
wenzelm@37949
    68
wenzelm@37949
    69
fun init master_dir = map_files (fn _ => (master_dir, [], []));
wenzelm@37949
    70
wenzelm@37949
    71
fun require src_path =
wenzelm@37949
    72
  map_files (fn (master_dir, required, provided) =>
wenzelm@37949
    73
    if member (op =) required src_path then
wenzelm@37949
    74
      error ("Duplicate source file dependency: " ^ Path.implode src_path)
wenzelm@37949
    75
    else (master_dir, src_path :: required, provided));
wenzelm@37949
    76
wenzelm@37949
    77
fun provide (src_path, path_info) =
wenzelm@37949
    78
  map_files (fn (master_dir, required, provided) =>
wenzelm@37949
    79
    if AList.defined (op =) provided src_path then
wenzelm@37949
    80
      error ("Duplicate resolution of source file dependency: " ^ Path.implode src_path)
wenzelm@37949
    81
    else (master_dir, required, (src_path, path_info) :: provided));
wenzelm@37949
    82
wenzelm@37949
    83
wenzelm@6232
    84
(* maintain load path *)
wenzelm@6168
    85
wenzelm@32738
    86
local val load_path = Unsynchronized.ref [Path.current] in
wenzelm@6168
    87
wenzelm@21858
    88
fun show_path () = map Path.implode (! load_path);
wenzelm@23944
    89
wenzelm@33221
    90
fun del_path s =
wenzelm@33221
    91
  CRITICAL (fn () => Unsynchronized.change load_path (remove (op =) (Path.explode s)));
wenzelm@33221
    92
wenzelm@33221
    93
fun add_path s =
wenzelm@33221
    94
  CRITICAL (fn () => (del_path s; Unsynchronized.change load_path (cons (Path.explode s))));
wenzelm@33221
    95
wenzelm@33221
    96
fun path_add s =
wenzelm@33221
    97
  CRITICAL (fn () =>
wenzelm@32738
    98
    (del_path s; Unsynchronized.change load_path (fn path => path @ [Path.explode s])));
wenzelm@33221
    99
wenzelm@37938
   100
fun reset_path () = CRITICAL (fn () => load_path := [Path.current]);
wenzelm@6168
   101
wenzelm@23944
   102
fun search_path dir path =
wenzelm@23944
   103
  distinct (op =) (dir :: (if Path.is_basic path then (! load_path) else [Path.current]));
wenzelm@23944
   104
wenzelm@23944
   105
end;
wenzelm@23944
   106
wenzelm@6168
   107
wenzelm@6232
   108
(* file formats *)
wenzelm@6168
   109
wenzelm@6232
   110
val ml_path = Path.ext "ML" o Path.basic;
wenzelm@6232
   111
val thy_path = Path.ext "thy" o Path.basic;
wenzelm@6168
   112
wenzelm@29418
   113
fun split_thy_path path =
wenzelm@29418
   114
  (case Path.split_ext path of
wenzelm@29418
   115
    (path', "thy") => (Path.dir path', Path.implode (Path.base path'))
wenzelm@29418
   116
  | _ => error ("Bad theory file specification " ^ Path.implode path));
wenzelm@29418
   117
wenzelm@37937
   118
fun consistent_name name name' =
wenzelm@37937
   119
  if name = name' then ()
wenzelm@37937
   120
  else error ("Filename " ^ quote (Path.implode (thy_path name)) ^
wenzelm@37937
   121
    " does not agree with theory name " ^ quote name');
wenzelm@37937
   122
wenzelm@6168
   123
wenzelm@23872
   124
(* check files *)
wenzelm@6232
   125
wenzelm@37939
   126
local
wenzelm@37939
   127
wenzelm@37939
   128
exception NOT_FOUND of Path.T list * Path.T;
wenzelm@37939
   129
wenzelm@37939
   130
fun try_file dir src_path =
wenzelm@6232
   131
  let
wenzelm@37939
   132
    val prfxs = search_path dir src_path;
wenzelm@6232
   133
    val path = Path.expand src_path;
wenzelm@23872
   134
    val _ = Path.is_current path andalso error "Bad file specification";
wenzelm@37939
   135
    val result =
wenzelm@37939
   136
      prfxs |> get_first (fn prfx =>
wenzelm@37939
   137
        let val full_path = File.full_path (Path.append prfx path)
wenzelm@37939
   138
        in Option.map (pair full_path) (File.ident full_path) end);
wenzelm@37939
   139
  in
wenzelm@37939
   140
    (case result of
wenzelm@37939
   141
      SOME res => res
wenzelm@37939
   142
    | NONE => raise NOT_FOUND (prfxs, path))
wenzelm@37939
   143
  end;
wenzelm@6168
   144
wenzelm@37939
   145
in
wenzelm@6168
   146
wenzelm@37939
   147
fun test_file dir file =
wenzelm@37939
   148
  SOME (try_file dir file) handle NOT_FOUND _ => NONE;
wenzelm@37939
   149
wenzelm@37939
   150
fun check_file dir file =
wenzelm@37939
   151
  try_file dir file handle NOT_FOUND (prfxs, path) =>
wenzelm@37939
   152
    error ("Could not find file " ^ quote (Path.implode path) ^
wenzelm@37943
   153
      "\nin " ^ commas_quote (map Path.implode prfxs));
wenzelm@37939
   154
wenzelm@37939
   155
fun check_thy dir name = check_file dir (thy_path name);
wenzelm@37939
   156
wenzelm@37939
   157
end;
wenzelm@6232
   158
wenzelm@6232
   159
wenzelm@23872
   160
(* theory deps *)
wenzelm@7940
   161
wenzelm@24189
   162
fun deps_thy dir name =
wenzelm@23872
   163
  let
wenzelm@24189
   164
    val master as (path, _) = check_thy dir name;
wenzelm@37902
   165
    val text = File.read path;
wenzelm@23872
   166
    val (name', imports, uses) =
wenzelm@37903
   167
      Thy_Header.read (Path.position path) (Source.of_string_limited 8000 text);
wenzelm@37937
   168
    val _ = consistent_name name name';
wenzelm@24189
   169
    val uses = map (Path.explode o #1) uses;
wenzelm@24065
   170
  in {master = master, text = text, imports = imports, uses = uses} end;
wenzelm@6484
   171
wenzelm@6168
   172
wenzelm@37949
   173
wenzelm@37949
   174
(* loaded files *)
wenzelm@37949
   175
wenzelm@37949
   176
val loaded_files = map (#1 o #2) o #provided o Files.get;
wenzelm@6168
   177
wenzelm@37949
   178
fun check_loaded thy =
wenzelm@37949
   179
  let
wenzelm@37949
   180
    val {required, provided, ...} = Files.get thy;
wenzelm@37949
   181
    val provided_paths = map #1 provided;
wenzelm@37949
   182
    val _ =
wenzelm@37949
   183
      (case subtract (op =) provided_paths required of
wenzelm@37949
   184
        [] => NONE
wenzelm@37949
   185
      | bad => error ("Pending source file dependencies: " ^ commas (map Path.implode (rev bad))));
wenzelm@37949
   186
    val _ =
wenzelm@37949
   187
      (case subtract (op =) required provided_paths of
wenzelm@37949
   188
        [] => NONE
wenzelm@37949
   189
      | bad => error ("Undeclared source file dependencies: " ^ commas (map Path.implode (rev bad))));
wenzelm@37949
   190
  in () end;
wenzelm@37949
   191
wenzelm@37949
   192
fun all_current thy =
wenzelm@37939
   193
  let
wenzelm@37949
   194
    val {master_dir, provided, ...} = Files.get thy;
wenzelm@37949
   195
    fun current (src_path, path_info) =
wenzelm@37949
   196
      (case test_file master_dir src_path of
wenzelm@37949
   197
        NONE => false
wenzelm@37949
   198
      | SOME path_info' => eq_snd (op =) (path_info, path_info'));
wenzelm@37949
   199
  in can check_loaded thy andalso forall current provided end;
wenzelm@37949
   200
wenzelm@37949
   201
wenzelm@37949
   202
(* provide files *)
wenzelm@37949
   203
wenzelm@37949
   204
fun provide_file src_path thy =
wenzelm@37949
   205
  provide (src_path, check_file (master_directory thy) src_path) thy;
wenzelm@37949
   206
wenzelm@37949
   207
fun use_ml src_path =
wenzelm@37949
   208
  if is_none (Context.thread_data ()) then
wenzelm@37949
   209
    ML_Context.eval_file (#1 (check_file Path.current src_path))
wenzelm@37949
   210
  else
wenzelm@37949
   211
    let
wenzelm@37949
   212
      val thy = ML_Context.the_global_context ();
wenzelm@37949
   213
      val (path, info) = check_file (master_directory thy) src_path;
wenzelm@37949
   214
wenzelm@37949
   215
      val _ = ML_Context.eval_file path;
wenzelm@37949
   216
      val _ = Context.>> Local_Theory.propagate_ml_env;
wenzelm@37949
   217
wenzelm@37949
   218
      val provide = provide (src_path, (path, info));
wenzelm@37949
   219
      val _ = Context.>> (Context.mapping provide (Local_Theory.theory provide));
wenzelm@37949
   220
    in () end
wenzelm@37949
   221
wenzelm@37949
   222
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
wenzelm@6168
   223
wenzelm@6168
   224
end;
wenzelm@6168
   225
wenzelm@37216
   226
structure Basic_Thy_Load: BASIC_THY_LOAD = Thy_Load;
wenzelm@32738
   227
open Basic_Thy_Load;