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