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