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