src/Pure/Thy/thy_load.ML
author wenzelm
Thu Aug 26 15:48:08 2010 +0200 (2010-08-26 ago)
changeset 38757 2b3e054ae6fc
parent 38133 987680d2e77d
child 40625 2d9222a2239d
permissions -rw-r--r--
renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
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@38133
    25
  val check_file: Path.T list -> 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@37977
    34
  val begin_theory: Path.T -> string -> theory list -> (Path.T * bool) list -> theory
wenzelm@6168
    35
end;
wenzelm@6168
    36
wenzelm@37216
    37
structure Thy_Load: THY_LOAD =
wenzelm@6168
    38
struct
wenzelm@6168
    39
wenzelm@37949
    40
(* manage source files *)
wenzelm@37949
    41
wenzelm@37949
    42
type files =
wenzelm@37949
    43
 {master_dir: Path.T,                                 (*master directory of theory source*)
wenzelm@37949
    44
  required: Path.T list,                              (*source path*)
wenzelm@37949
    45
  provided: (Path.T * (Path.T * File.ident)) list};   (*source path, physical path, identifier*)
wenzelm@37949
    46
wenzelm@37949
    47
fun make_files (master_dir, required, provided): files =
wenzelm@37949
    48
 {master_dir = master_dir, required = required, provided = provided};
wenzelm@37949
    49
wenzelm@37949
    50
structure Files = Theory_Data
wenzelm@37949
    51
(
wenzelm@37949
    52
  type T = files;
wenzelm@37949
    53
  val empty = make_files (Path.current, [], []);
wenzelm@37949
    54
  fun extend _ = empty;
wenzelm@37949
    55
  fun merge _ = empty;
wenzelm@37949
    56
);
wenzelm@37949
    57
wenzelm@37949
    58
fun map_files f =
wenzelm@37949
    59
  Files.map (fn {master_dir, required, provided} =>
wenzelm@37949
    60
    make_files (f (master_dir, required, provided)));
wenzelm@37949
    61
wenzelm@37949
    62
wenzelm@37949
    63
val master_directory = #master_dir o Files.get;
wenzelm@37949
    64
wenzelm@37977
    65
fun master dir = map_files (fn _ => (dir, [], []));
wenzelm@37949
    66
wenzelm@37949
    67
fun require src_path =
wenzelm@37949
    68
  map_files (fn (master_dir, required, provided) =>
wenzelm@37949
    69
    if member (op =) required src_path then
wenzelm@37949
    70
      error ("Duplicate source file dependency: " ^ Path.implode src_path)
wenzelm@37949
    71
    else (master_dir, src_path :: required, provided));
wenzelm@37949
    72
wenzelm@37978
    73
fun provide (src_path, path_id) =
wenzelm@37949
    74
  map_files (fn (master_dir, required, provided) =>
wenzelm@37949
    75
    if AList.defined (op =) provided src_path then
wenzelm@37949
    76
      error ("Duplicate resolution of source file dependency: " ^ Path.implode src_path)
wenzelm@37978
    77
    else (master_dir, required, (src_path, path_id) :: provided));
wenzelm@37949
    78
wenzelm@37949
    79
wenzelm@37977
    80
(* maintain default paths *)
wenzelm@6168
    81
wenzelm@37977
    82
local
wenzelm@37977
    83
  val load_path = Unsynchronized.ref [Path.current];
wenzelm@37977
    84
  val master_path = Unsynchronized.ref Path.current;
wenzelm@37977
    85
in
wenzelm@6168
    86
wenzelm@21858
    87
fun show_path () = map Path.implode (! load_path);
wenzelm@23944
    88
wenzelm@33221
    89
fun del_path s =
wenzelm@33221
    90
  CRITICAL (fn () => Unsynchronized.change load_path (remove (op =) (Path.explode s)));
wenzelm@33221
    91
wenzelm@33221
    92
fun add_path s =
wenzelm@33221
    93
  CRITICAL (fn () => (del_path s; Unsynchronized.change load_path (cons (Path.explode s))));
wenzelm@33221
    94
wenzelm@33221
    95
fun path_add s =
wenzelm@33221
    96
  CRITICAL (fn () =>
wenzelm@32738
    97
    (del_path s; Unsynchronized.change load_path (fn path => path @ [Path.explode s])));
wenzelm@33221
    98
wenzelm@37938
    99
fun reset_path () = CRITICAL (fn () => load_path := [Path.current]);
wenzelm@6168
   100
wenzelm@23944
   101
fun search_path dir path =
wenzelm@23944
   102
  distinct (op =) (dir :: (if Path.is_basic path then (! load_path) else [Path.current]));
wenzelm@23944
   103
wenzelm@37977
   104
fun set_master_path path = master_path := path;
wenzelm@37977
   105
fun get_master_path () = ! master_path;
wenzelm@37977
   106
wenzelm@23944
   107
end;
wenzelm@23944
   108
wenzelm@6168
   109
wenzelm@23872
   110
(* check files *)
wenzelm@6232
   111
wenzelm@38133
   112
fun get_file dirs src_path =
wenzelm@6232
   113
  let
wenzelm@6232
   114
    val path = Path.expand src_path;
wenzelm@23872
   115
    val _ = Path.is_current path andalso error "Bad file specification";
wenzelm@37939
   116
  in
wenzelm@38133
   117
    dirs |> get_first (fn dir =>
wenzelm@38133
   118
      let val full_path = File.full_path (Path.append dir path) in
wenzelm@38133
   119
        (case File.ident full_path of
wenzelm@38133
   120
          NONE => NONE
wenzelm@38133
   121
        | SOME id => SOME (full_path, id))
wenzelm@38133
   122
      end)
wenzelm@37939
   123
  end;
wenzelm@6168
   124
wenzelm@38133
   125
fun check_file dirs file =
wenzelm@38133
   126
  (case get_file dirs file of
wenzelm@38133
   127
    SOME path_id => path_id
wenzelm@38133
   128
  | NONE => error ("Could not find file " ^ quote (Path.implode file) ^
wenzelm@38133
   129
      "\nin " ^ commas_quote (map Path.implode dirs)));
wenzelm@37939
   130
wenzelm@38133
   131
fun check_thy master_dir name =
wenzelm@38133
   132
  let
wenzelm@38133
   133
    val thy_file = Thy_Header.thy_path name;
wenzelm@38133
   134
    val dirs = search_path master_dir thy_file;
wenzelm@38133
   135
  in check_file dirs thy_file end;
wenzelm@6232
   136
wenzelm@6232
   137
wenzelm@23872
   138
(* theory deps *)
wenzelm@7940
   139
wenzelm@38133
   140
fun deps_thy master_dir name =
wenzelm@23872
   141
  let
wenzelm@38133
   142
    val master as (thy_path, _) = check_thy master_dir name;
wenzelm@37978
   143
    val text = File.read thy_path;
wenzelm@37978
   144
    val (name', imports, uses) = Thy_Header.read (Path.position thy_path) text;
wenzelm@37950
   145
    val _ = Thy_Header.consistent_name name name';
wenzelm@24189
   146
    val uses = map (Path.explode o #1) uses;
wenzelm@24065
   147
  in {master = master, text = text, imports = imports, uses = uses} end;
wenzelm@6484
   148
wenzelm@6168
   149
wenzelm@37949
   150
wenzelm@37949
   151
(* loaded files *)
wenzelm@37949
   152
wenzelm@37949
   153
val loaded_files = map (#1 o #2) o #provided o Files.get;
wenzelm@6168
   154
wenzelm@37949
   155
fun check_loaded thy =
wenzelm@37949
   156
  let
wenzelm@37949
   157
    val {required, provided, ...} = Files.get thy;
wenzelm@37949
   158
    val provided_paths = map #1 provided;
wenzelm@37949
   159
    val _ =
wenzelm@37949
   160
      (case subtract (op =) provided_paths required of
wenzelm@37949
   161
        [] => NONE
wenzelm@37949
   162
      | bad => error ("Pending source file dependencies: " ^ commas (map Path.implode (rev bad))));
wenzelm@37949
   163
    val _ =
wenzelm@37949
   164
      (case subtract (op =) required provided_paths of
wenzelm@37949
   165
        [] => NONE
wenzelm@37949
   166
      | bad => error ("Undeclared source file dependencies: " ^ commas (map Path.implode (rev bad))));
wenzelm@37949
   167
  in () end;
wenzelm@37949
   168
wenzelm@37949
   169
fun all_current thy =
wenzelm@37939
   170
  let
wenzelm@37949
   171
    val {master_dir, provided, ...} = Files.get thy;
wenzelm@37978
   172
    fun current (src_path, (_, id)) =
wenzelm@38133
   173
      (case get_file [master_dir] src_path of
wenzelm@37949
   174
        NONE => false
wenzelm@37978
   175
      | SOME (_, id') => id = id');
wenzelm@37949
   176
  in can check_loaded thy andalso forall current provided end;
wenzelm@37949
   177
wenzelm@37952
   178
val _ = Context.>> (Context.map_theory (Theory.at_end (fn thy => (check_loaded thy; NONE))));
wenzelm@37952
   179
wenzelm@37949
   180
wenzelm@37949
   181
(* provide files *)
wenzelm@37949
   182
wenzelm@37949
   183
fun provide_file src_path thy =
wenzelm@38133
   184
  provide (src_path, check_file [master_directory thy] src_path) thy;
wenzelm@37949
   185
wenzelm@37949
   186
fun use_ml src_path =
wenzelm@37949
   187
  if is_none (Context.thread_data ()) then
wenzelm@38133
   188
    ML_Context.eval_file (#1 (check_file [Path.current] src_path))
wenzelm@37949
   189
  else
wenzelm@37949
   190
    let
wenzelm@37949
   191
      val thy = ML_Context.the_global_context ();
wenzelm@38133
   192
      val (path, id) = check_file [master_directory thy] src_path;
wenzelm@37949
   193
wenzelm@37949
   194
      val _ = ML_Context.eval_file path;
wenzelm@37949
   195
      val _ = Context.>> Local_Theory.propagate_ml_env;
wenzelm@37949
   196
wenzelm@37978
   197
      val provide = provide (src_path, (path, id));
wenzelm@38757
   198
      val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
wenzelm@37978
   199
    in () end;
wenzelm@37949
   200
wenzelm@37949
   201
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
wenzelm@6168
   202
wenzelm@37977
   203
wenzelm@37977
   204
(* begin theory *)
wenzelm@37977
   205
wenzelm@37977
   206
fun begin_theory dir name parents uses =
wenzelm@37977
   207
  Theory.begin_theory name parents
wenzelm@37977
   208
  |> master dir
wenzelm@37977
   209
  |> fold (require o fst) uses
wenzelm@37977
   210
  |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
wenzelm@37977
   211
  |> Theory.checkpoint;
wenzelm@37977
   212
wenzelm@6168
   213
end;
wenzelm@6168
   214
wenzelm@37216
   215
structure Basic_Thy_Load: BASIC_THY_LOAD = Thy_Load;
wenzelm@32738
   216
open Basic_Thy_Load;