src/Pure/Thy/thy_load.ML
author wenzelm
Sun Mar 20 13:49:21 2011 +0100 (2011-03-20 ago)
changeset 42002 ac7097bd8611
parent 41955 703ea96b13c6
child 42003 6e45dc518ebb
permissions -rw-r--r--
tuned;
wenzelm@6168
     1
(*  Title:      Pure/Thy/thy_load.ML
wenzelm@42002
     2
    Author:     Makarius
wenzelm@6168
     3
wenzelm@42002
     4
Loading files that contribute to a theory.  Global master path.
wenzelm@6168
     5
*)
wenzelm@6168
     6
wenzelm@6168
     7
signature THY_LOAD =
wenzelm@6168
     8
sig
wenzelm@37949
     9
  val master_directory: theory -> Path.T
wenzelm@41548
    10
  val imports_of: theory -> string list
wenzelm@41955
    11
  val provide: Path.T * (Path.T * SHA1.digest) -> theory -> theory
wenzelm@41955
    12
  val check_file: Path.T -> Path.T -> Path.T * SHA1.digest
wenzelm@41955
    13
  val check_thy: Path.T -> string -> Path.T * SHA1.digest
wenzelm@24189
    14
  val deps_thy: Path.T -> string ->
wenzelm@41955
    15
   {master: Path.T * SHA1.digest, text: string, imports: string list, uses: Path.T list}
wenzelm@37949
    16
  val loaded_files: theory -> Path.T list
wenzelm@37949
    17
  val all_current: theory -> bool
wenzelm@37949
    18
  val provide_file: Path.T -> theory -> theory
wenzelm@37949
    19
  val use_ml: Path.T -> unit
wenzelm@37949
    20
  val exec_ml: Path.T -> generic_theory -> generic_theory
wenzelm@41548
    21
  val begin_theory: Path.T -> string -> string list -> theory list -> (Path.T * bool) list -> theory
wenzelm@42002
    22
  val set_master_path: Path.T -> unit
wenzelm@42002
    23
  val get_master_path: unit -> Path.T
wenzelm@6168
    24
end;
wenzelm@6168
    25
wenzelm@37216
    26
structure Thy_Load: THY_LOAD =
wenzelm@6168
    27
struct
wenzelm@6168
    28
wenzelm@37949
    29
(* manage source files *)
wenzelm@37949
    30
wenzelm@37949
    31
type files =
wenzelm@40741
    32
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@41548
    33
  imports: string list,  (*source specification of imports*)
wenzelm@40741
    34
  required: Path.T list,  (*source path*)
wenzelm@41955
    35
  provided: (Path.T * (Path.T * SHA1.digest)) list};  (*source path, physical path, digest*)
wenzelm@37949
    36
wenzelm@41548
    37
fun make_files (master_dir, imports, required, provided): files =
wenzelm@41548
    38
 {master_dir = master_dir, imports = imports, required = required, provided = provided};
wenzelm@37949
    39
wenzelm@37949
    40
structure Files = Theory_Data
wenzelm@37949
    41
(
wenzelm@37949
    42
  type T = files;
wenzelm@41548
    43
  val empty = make_files (Path.current, [], [], []);
wenzelm@37949
    44
  fun extend _ = empty;
wenzelm@37949
    45
  fun merge _ = empty;
wenzelm@37949
    46
);
wenzelm@37949
    47
wenzelm@37949
    48
fun map_files f =
wenzelm@41548
    49
  Files.map (fn {master_dir, imports, required, provided} =>
wenzelm@41548
    50
    make_files (f (master_dir, imports, required, provided)));
wenzelm@37949
    51
wenzelm@37949
    52
wenzelm@37949
    53
val master_directory = #master_dir o Files.get;
wenzelm@41548
    54
val imports_of = #imports o Files.get;
wenzelm@37949
    55
wenzelm@41548
    56
fun put_deps dir imports = map_files (fn _ => (dir, imports, [], []));
wenzelm@37949
    57
wenzelm@37949
    58
fun require src_path =
wenzelm@41548
    59
  map_files (fn (master_dir, imports, required, provided) =>
wenzelm@37949
    60
    if member (op =) required src_path then
wenzelm@41944
    61
      error ("Duplicate source file dependency: " ^ Path.print src_path)
wenzelm@41548
    62
    else (master_dir, imports, src_path :: required, provided));
wenzelm@37949
    63
wenzelm@37978
    64
fun provide (src_path, path_id) =
wenzelm@41548
    65
  map_files (fn (master_dir, imports, required, provided) =>
wenzelm@37949
    66
    if AList.defined (op =) provided src_path then
wenzelm@41944
    67
      error ("Duplicate resolution of source file dependency: " ^ Path.print src_path)
wenzelm@41548
    68
    else (master_dir, imports, required, (src_path, path_id) :: provided));
wenzelm@37949
    69
wenzelm@37949
    70
wenzelm@23872
    71
(* check files *)
wenzelm@6232
    72
wenzelm@41886
    73
fun get_file dir src_path =
wenzelm@6232
    74
  let
wenzelm@6232
    75
    val path = Path.expand src_path;
wenzelm@23872
    76
    val _ = Path.is_current path andalso error "Bad file specification";
wenzelm@41886
    77
    val full_path = File.full_path (Path.append dir path);
wenzelm@37939
    78
  in
wenzelm@41955
    79
    if File.exists full_path
wenzelm@41955
    80
    then SOME (full_path, SHA1.digest (File.read full_path))
wenzelm@41955
    81
    else NONE
wenzelm@37939
    82
  end;
wenzelm@6168
    83
wenzelm@41886
    84
fun check_file dir file =
wenzelm@41886
    85
  (case get_file dir file of
wenzelm@41886
    86
    SOME path_id => path_id
wenzelm@41944
    87
  | NONE => error ("Could not find file " ^ Path.print file ^ "\nin " ^ Path.print dir));
wenzelm@37939
    88
wenzelm@41886
    89
fun check_thy dir name =
wenzelm@41886
    90
  check_file dir (Thy_Header.thy_path name);
wenzelm@6232
    91
wenzelm@6232
    92
wenzelm@23872
    93
(* theory deps *)
wenzelm@7940
    94
wenzelm@38133
    95
fun deps_thy master_dir name =
wenzelm@23872
    96
  let
wenzelm@38133
    97
    val master as (thy_path, _) = check_thy master_dir name;
wenzelm@37978
    98
    val text = File.read thy_path;
wenzelm@37978
    99
    val (name', imports, uses) = Thy_Header.read (Path.position thy_path) text;
wenzelm@37950
   100
    val _ = Thy_Header.consistent_name name name';
wenzelm@24189
   101
    val uses = map (Path.explode o #1) uses;
wenzelm@24065
   102
  in {master = master, text = text, imports = imports, uses = uses} end;
wenzelm@6484
   103
wenzelm@6168
   104
wenzelm@37949
   105
wenzelm@37949
   106
(* loaded files *)
wenzelm@37949
   107
wenzelm@37949
   108
val loaded_files = map (#1 o #2) o #provided o Files.get;
wenzelm@6168
   109
wenzelm@37949
   110
fun check_loaded thy =
wenzelm@37949
   111
  let
wenzelm@37949
   112
    val {required, provided, ...} = Files.get thy;
wenzelm@37949
   113
    val provided_paths = map #1 provided;
wenzelm@37949
   114
    val _ =
wenzelm@37949
   115
      (case subtract (op =) provided_paths required of
wenzelm@37949
   116
        [] => NONE
wenzelm@41944
   117
      | bad => error ("Pending source file dependencies: " ^ commas (map Path.print (rev bad))));
wenzelm@37949
   118
    val _ =
wenzelm@37949
   119
      (case subtract (op =) required provided_paths of
wenzelm@37949
   120
        [] => NONE
wenzelm@41944
   121
      | bad => error ("Undeclared source file dependencies: " ^ commas (map Path.print (rev bad))));
wenzelm@37949
   122
  in () end;
wenzelm@37949
   123
wenzelm@37949
   124
fun all_current thy =
wenzelm@37939
   125
  let
wenzelm@37949
   126
    val {master_dir, provided, ...} = Files.get thy;
wenzelm@37978
   127
    fun current (src_path, (_, id)) =
wenzelm@41886
   128
      (case get_file master_dir src_path of
wenzelm@37949
   129
        NONE => false
wenzelm@41886
   130
      | SOME (_, id') => id = id');
wenzelm@37949
   131
  in can check_loaded thy andalso forall current provided end;
wenzelm@37949
   132
wenzelm@37952
   133
val _ = Context.>> (Context.map_theory (Theory.at_end (fn thy => (check_loaded thy; NONE))));
wenzelm@37952
   134
wenzelm@37949
   135
wenzelm@37949
   136
(* provide files *)
wenzelm@37949
   137
wenzelm@37949
   138
fun provide_file src_path thy =
wenzelm@41886
   139
  provide (src_path, check_file (master_directory thy) src_path) thy;
wenzelm@37949
   140
wenzelm@37949
   141
fun use_ml src_path =
wenzelm@37949
   142
  if is_none (Context.thread_data ()) then
wenzelm@41886
   143
    ML_Context.eval_file (#1 (check_file Path.current src_path))
wenzelm@37949
   144
  else
wenzelm@37949
   145
    let
wenzelm@37949
   146
      val thy = ML_Context.the_global_context ();
wenzelm@41886
   147
      val (path, id) = check_file (master_directory thy) src_path;
wenzelm@37949
   148
wenzelm@37949
   149
      val _ = ML_Context.eval_file path;
wenzelm@37949
   150
      val _ = Context.>> Local_Theory.propagate_ml_env;
wenzelm@37949
   151
wenzelm@37978
   152
      val provide = provide (src_path, (path, id));
wenzelm@38757
   153
      val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
wenzelm@37978
   154
    in () end;
wenzelm@37949
   155
wenzelm@37949
   156
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
wenzelm@6168
   157
wenzelm@37977
   158
wenzelm@37977
   159
(* begin theory *)
wenzelm@37977
   160
wenzelm@41548
   161
fun begin_theory dir name imports parents uses =
wenzelm@37977
   162
  Theory.begin_theory name parents
wenzelm@41548
   163
  |> put_deps dir imports
wenzelm@37977
   164
  |> fold (require o fst) uses
wenzelm@37977
   165
  |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
wenzelm@37977
   166
  |> Theory.checkpoint;
wenzelm@37977
   167
wenzelm@42002
   168
wenzelm@42002
   169
(* global master path *)
wenzelm@42002
   170
wenzelm@42002
   171
local
wenzelm@42002
   172
  val master_path = Unsynchronized.ref Path.current;
wenzelm@42002
   173
in
wenzelm@42002
   174
wenzelm@42002
   175
fun set_master_path path = master_path := path;
wenzelm@42002
   176
fun get_master_path () = ! master_path;
wenzelm@42002
   177
wenzelm@6168
   178
end;
wenzelm@6168
   179
wenzelm@42002
   180
end;