src/Pure/Thy/thy_load.ML
author wenzelm
Thu Mar 15 00:10:45 2012 +0100 (2012-03-15 ago)
changeset 46938 cda018294515
parent 46811 03a2dc9e0624
child 46958 0ec8f04e753a
permissions -rw-r--r--
some support for outer syntax keyword declarations within theory header;
more uniform Thy_Header.header as argument for begin_theory etc.;
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@46737
    13
  val thy_path: Path.T -> Path.T
wenzelm@42003
    14
  val check_thy: Path.T -> string ->
wenzelm@42003
    15
    {master: Path.T * SHA1.digest, text: string, imports: string list, uses: (Path.T * bool) list}
wenzelm@43702
    16
  val load_file: theory -> Path.T -> (Path.T * SHA1.digest) * string
wenzelm@43702
    17
  val use_file: Path.T -> theory -> string * theory
wenzelm@37949
    18
  val loaded_files: theory -> Path.T list
wenzelm@37949
    19
  val all_current: theory -> bool
wenzelm@37949
    20
  val use_ml: Path.T -> unit
wenzelm@37949
    21
  val exec_ml: Path.T -> generic_theory -> generic_theory
wenzelm@46938
    22
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
wenzelm@46938
    23
  val load_thy: int -> Path.T -> Thy_Header.header -> Position.T -> string ->
wenzelm@46938
    24
    theory list -> theory * unit future
wenzelm@42002
    25
  val set_master_path: Path.T -> unit
wenzelm@42002
    26
  val get_master_path: unit -> Path.T
wenzelm@6168
    27
end;
wenzelm@6168
    28
wenzelm@37216
    29
structure Thy_Load: THY_LOAD =
wenzelm@6168
    30
struct
wenzelm@6168
    31
wenzelm@37949
    32
(* manage source files *)
wenzelm@37949
    33
wenzelm@37949
    34
type files =
wenzelm@40741
    35
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@41548
    36
  imports: string list,  (*source specification of imports*)
wenzelm@40741
    37
  required: Path.T list,  (*source path*)
wenzelm@41955
    38
  provided: (Path.T * (Path.T * SHA1.digest)) list};  (*source path, physical path, digest*)
wenzelm@37949
    39
wenzelm@41548
    40
fun make_files (master_dir, imports, required, provided): files =
wenzelm@41548
    41
 {master_dir = master_dir, imports = imports, required = required, provided = provided};
wenzelm@37949
    42
wenzelm@37949
    43
structure Files = Theory_Data
wenzelm@37949
    44
(
wenzelm@37949
    45
  type T = files;
wenzelm@41548
    46
  val empty = make_files (Path.current, [], [], []);
wenzelm@37949
    47
  fun extend _ = empty;
wenzelm@37949
    48
  fun merge _ = empty;
wenzelm@37949
    49
);
wenzelm@37949
    50
wenzelm@37949
    51
fun map_files f =
wenzelm@41548
    52
  Files.map (fn {master_dir, imports, required, provided} =>
wenzelm@41548
    53
    make_files (f (master_dir, imports, required, provided)));
wenzelm@37949
    54
wenzelm@37949
    55
wenzelm@37949
    56
val master_directory = #master_dir o Files.get;
wenzelm@41548
    57
val imports_of = #imports o Files.get;
wenzelm@37949
    58
wenzelm@41548
    59
fun put_deps dir imports = map_files (fn _ => (dir, imports, [], []));
wenzelm@37949
    60
wenzelm@37949
    61
fun require src_path =
wenzelm@41548
    62
  map_files (fn (master_dir, imports, required, provided) =>
wenzelm@37949
    63
    if member (op =) required src_path then
wenzelm@41944
    64
      error ("Duplicate source file dependency: " ^ Path.print src_path)
wenzelm@41548
    65
    else (master_dir, imports, src_path :: required, provided));
wenzelm@37949
    66
wenzelm@37978
    67
fun provide (src_path, path_id) =
wenzelm@41548
    68
  map_files (fn (master_dir, imports, required, provided) =>
wenzelm@37949
    69
    if AList.defined (op =) provided src_path then
wenzelm@41944
    70
      error ("Duplicate resolution of source file dependency: " ^ Path.print src_path)
wenzelm@41548
    71
    else (master_dir, imports, required, (src_path, path_id) :: provided));
wenzelm@37949
    72
wenzelm@37949
    73
wenzelm@23872
    74
(* check files *)
wenzelm@6232
    75
wenzelm@42003
    76
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@42003
    77
wenzelm@46737
    78
val thy_path = Path.ext "thy";
wenzelm@46737
    79
wenzelm@41886
    80
fun check_thy dir name =
wenzelm@23872
    81
  let
wenzelm@46737
    82
    val path = thy_path (Path.basic name);
wenzelm@46737
    83
    val master_file = check_file dir path;
wenzelm@42003
    84
    val text = File.read master_file;
wenzelm@42003
    85
    val (name', imports, uses) =
wenzelm@42003
    86
      if name = Context.PureN then (Context.PureN, [], [])
wenzelm@46938
    87
      else
wenzelm@46938
    88
        let val {name, imports, uses, ...} = Thy_Header.read (Path.position master_file) text
wenzelm@46938
    89
        in (name, imports, uses) end;
wenzelm@46737
    90
    val _ = name <> name' andalso
wenzelm@46737
    91
      error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name');
wenzelm@42003
    92
  in {master = (master_file, SHA1.digest text), text = text, imports = imports, uses = uses} end;
wenzelm@6168
    93
wenzelm@37949
    94
wenzelm@43702
    95
(* load files *)
wenzelm@43702
    96
wenzelm@43702
    97
fun load_file thy src_path =
wenzelm@43702
    98
  let
wenzelm@43702
    99
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   100
    val text = File.read full_path;
wenzelm@43702
   101
    val id = SHA1.digest text;
wenzelm@43702
   102
  in ((full_path, id), text) end;
wenzelm@43702
   103
wenzelm@43702
   104
fun use_file src_path thy =
wenzelm@43702
   105
  let
wenzelm@43702
   106
    val (path_id, text) = load_file thy src_path;
wenzelm@43702
   107
    val thy' = provide (src_path, path_id) thy;
wenzelm@43702
   108
  in (text, thy') end;
wenzelm@37949
   109
wenzelm@37949
   110
val loaded_files = map (#1 o #2) o #provided o Files.get;
wenzelm@6168
   111
wenzelm@37949
   112
fun check_loaded thy =
wenzelm@37949
   113
  let
wenzelm@37949
   114
    val {required, provided, ...} = Files.get thy;
wenzelm@37949
   115
    val provided_paths = map #1 provided;
wenzelm@37949
   116
    val _ =
wenzelm@37949
   117
      (case subtract (op =) provided_paths required of
wenzelm@37949
   118
        [] => NONE
wenzelm@41944
   119
      | bad => error ("Pending source file dependencies: " ^ commas (map Path.print (rev bad))));
wenzelm@37949
   120
    val _ =
wenzelm@37949
   121
      (case subtract (op =) required provided_paths of
wenzelm@37949
   122
        [] => NONE
wenzelm@41944
   123
      | bad => error ("Undeclared source file dependencies: " ^ commas (map Path.print (rev bad))));
wenzelm@37949
   124
  in () end;
wenzelm@37949
   125
wenzelm@37949
   126
fun all_current thy =
wenzelm@37939
   127
  let
wenzelm@43702
   128
    val {provided, ...} = Files.get thy;
wenzelm@37978
   129
    fun current (src_path, (_, id)) =
wenzelm@43702
   130
      (case try (load_file thy) src_path of
wenzelm@37949
   131
        NONE => false
wenzelm@43702
   132
      | SOME ((_, id'), _) => id = id');
wenzelm@37949
   133
  in can check_loaded thy andalso forall current provided end;
wenzelm@37949
   134
wenzelm@37952
   135
val _ = Context.>> (Context.map_theory (Theory.at_end (fn thy => (check_loaded thy; NONE))));
wenzelm@37952
   136
wenzelm@37949
   137
wenzelm@37949
   138
(* provide files *)
wenzelm@37949
   139
wenzelm@43700
   140
fun eval_file path text = ML_Context.eval_text true (Path.position path) text;
wenzelm@37949
   141
wenzelm@37949
   142
fun use_ml src_path =
wenzelm@37949
   143
  if is_none (Context.thread_data ()) then
wenzelm@43700
   144
    let val path = check_file Path.current src_path
wenzelm@43700
   145
    in eval_file path (File.read path) end
wenzelm@37949
   146
  else
wenzelm@37949
   147
    let
wenzelm@37949
   148
      val thy = ML_Context.the_global_context ();
wenzelm@37949
   149
wenzelm@43702
   150
      val ((path, id), text) = load_file thy src_path;
wenzelm@43700
   151
      val _ = eval_file path text;
wenzelm@37949
   152
      val _ = Context.>> Local_Theory.propagate_ml_env;
wenzelm@37949
   153
wenzelm@37978
   154
      val provide = provide (src_path, (path, id));
wenzelm@38757
   155
      val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
wenzelm@37978
   156
    in () end;
wenzelm@37949
   157
wenzelm@37949
   158
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
wenzelm@6168
   159
wenzelm@37977
   160
wenzelm@43712
   161
(* load_thy *)
wenzelm@37977
   162
wenzelm@46938
   163
fun begin_theory dir {name, imports, keywords, uses} parents =
wenzelm@37977
   164
  Theory.begin_theory name parents
wenzelm@41548
   165
  |> put_deps dir imports
wenzelm@46938
   166
  |> fold Thy_Header.declare_keyword keywords
wenzelm@37977
   167
  |> fold (require o fst) uses
wenzelm@37977
   168
  |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
wenzelm@37977
   169
  |> Theory.checkpoint;
wenzelm@37977
   170
wenzelm@46938
   171
fun load_thy update_time dir header pos text parents =
wenzelm@43712
   172
  let
wenzelm@43712
   173
    val (lexs, outer_syntax) = Outer_Syntax.get_syntax ();
wenzelm@43712
   174
    val time = ! Toplevel.timing;
wenzelm@43712
   175
wenzelm@46938
   176
    val {name, imports, uses, ...} = header;
wenzelm@43712
   177
    val _ = Present.init_theory name;
wenzelm@44186
   178
    fun init () =
wenzelm@46938
   179
      begin_theory dir header parents
wenzelm@43712
   180
      |> Present.begin_theory update_time dir uses;
wenzelm@43712
   181
wenzelm@46811
   182
    val toks = Thy_Syntax.parse_tokens lexs pos text;
wenzelm@46811
   183
    val spans = Thy_Syntax.parse_spans toks;
wenzelm@43712
   184
    val elements =
wenzelm@46811
   185
      maps (Outer_Syntax.read_element outer_syntax init) (Thy_Syntax.parse_elements spans);
wenzelm@43712
   186
wenzelm@43712
   187
    val _ = Present.theory_source name
wenzelm@43712
   188
      (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
wenzelm@43712
   189
wenzelm@43712
   190
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@43712
   191
    val (results, thy) = cond_timeit time "" (fn () => Toplevel.excursion elements);
wenzelm@43712
   192
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@43712
   193
wenzelm@43712
   194
    val present =
wenzelm@43712
   195
      singleton (Future.cond_forks {name = "Outer_Syntax.present:" ^ name, group = NONE,
wenzelm@44113
   196
        deps = map Future.task_of results, pri = 0, interrupts = true})
wenzelm@43712
   197
      (fn () =>
wenzelm@43712
   198
        Thy_Output.present_thy (#1 lexs) Keyword.command_tags
wenzelm@43712
   199
          (Outer_Syntax.is_markup outer_syntax)
wenzelm@43712
   200
          (maps Future.join results) toks
wenzelm@43712
   201
        |> Buffer.content
wenzelm@43712
   202
        |> Present.theory_output name);
wenzelm@43712
   203
wenzelm@43712
   204
  in (thy, present) end;
wenzelm@43712
   205
wenzelm@42002
   206
wenzelm@42002
   207
(* global master path *)
wenzelm@42002
   208
wenzelm@42002
   209
local
wenzelm@42002
   210
  val master_path = Unsynchronized.ref Path.current;
wenzelm@42002
   211
in
wenzelm@42002
   212
wenzelm@42002
   213
fun set_master_path path = master_path := path;
wenzelm@42002
   214
fun get_master_path () = ! master_path;
wenzelm@42002
   215
wenzelm@6168
   216
end;
wenzelm@6168
   217
wenzelm@42002
   218
end;