src/Pure/Thy/thy_load.ML
author wenzelm
Thu Aug 23 12:33:42 2012 +0200 (2012-08-23 ago)
changeset 48898 9fc880720663
parent 48897 873fdafc5b09
child 48905 04576657cf94
permissions -rw-r--r--
simplified Thy_Load.check_thy (again) -- no need to pass keywords nor find files in body text;
wenzelm@6168
     1
(*  Title:      Pure/Thy/thy_load.ML
wenzelm@42002
     2
    Author:     Makarius
wenzelm@6168
     3
wenzelm@48888
     4
Loading files that contribute to a theory.  Global master path for TTY loop.
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@48896
    11
  val provide: Path.T * SHA1.digest -> theory -> theory
wenzelm@46737
    12
  val thy_path: Path.T -> Path.T
wenzelm@48898
    13
  val check_thy: Path.T -> string ->
wenzelm@48874
    14
   {master: Path.T * SHA1.digest, text: string, imports: string list,
wenzelm@48876
    15
    uses: (Path.T * bool) list, keywords: Thy_Header.keywords}
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@48886
    19
  val load_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@48867
    25
  val parse_files: string -> (theory -> Token.files) parser
wenzelm@42002
    26
  val set_master_path: Path.T -> unit
wenzelm@42002
    27
  val get_master_path: unit -> Path.T
wenzelm@6168
    28
end;
wenzelm@6168
    29
wenzelm@37216
    30
structure Thy_Load: THY_LOAD =
wenzelm@6168
    31
struct
wenzelm@6168
    32
wenzelm@37949
    33
(* manage source files *)
wenzelm@37949
    34
wenzelm@37949
    35
type files =
wenzelm@40741
    36
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@41548
    37
  imports: string list,  (*source specification of imports*)
wenzelm@48896
    38
  provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
wenzelm@37949
    39
wenzelm@48886
    40
fun make_files (master_dir, imports, provided): files =
wenzelm@48886
    41
 {master_dir = master_dir, imports = imports, provided = provided};
wenzelm@37949
    42
wenzelm@37949
    43
structure Files = Theory_Data
wenzelm@37949
    44
(
wenzelm@37949
    45
  type T = files;
wenzelm@48886
    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@48886
    52
  Files.map (fn {master_dir, imports, provided} =>
wenzelm@48886
    53
    make_files (f (master_dir, imports, 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@48888
    59
fun put_deps master_dir imports = map_files (fn _ => (master_dir, imports, []));
wenzelm@37949
    60
wenzelm@48896
    61
fun provide (src_path, id) =
wenzelm@48886
    62
  map_files (fn (master_dir, imports, provided) =>
wenzelm@37949
    63
    if AList.defined (op =) provided src_path then
wenzelm@48886
    64
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48896
    65
    else (master_dir, imports, (src_path, id) :: provided));
wenzelm@37949
    66
wenzelm@37949
    67
wenzelm@48869
    68
(* inlined files *)
wenzelm@48869
    69
wenzelm@48878
    70
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@48878
    71
wenzelm@48898
    72
fun read_files cmd dir path =
wenzelm@48898
    73
  let
wenzelm@48898
    74
    val paths =
wenzelm@48898
    75
      (case Keyword.command_files cmd of
wenzelm@48898
    76
        [] => [path]
wenzelm@48898
    77
      | exts => map (fn ext => Path.ext ext path) exts);
wenzelm@48898
    78
    val files = paths |> map (check_file dir #> (fn p => (File.read p, Path.position p)));
wenzelm@48898
    79
  in (dir, files) end;
wenzelm@48898
    80
wenzelm@48898
    81
fun parse_files cmd =
wenzelm@48898
    82
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
wenzelm@48898
    83
    (case Token.get_files tok of
wenzelm@48898
    84
      SOME files => files
wenzelm@48898
    85
    | NONE => read_files cmd (master_directory thy) (Path.explode name)));
wenzelm@48898
    86
wenzelm@48874
    87
local
wenzelm@48874
    88
wenzelm@48874
    89
fun clean ((i1, t1) :: (i2, t2) :: toks) =
wenzelm@48874
    90
      if Token.keyword_with (fn s => s = "%" orelse s = "--") t1 then clean toks
wenzelm@48874
    91
      else (i1, t1) :: clean ((i2, t2) :: toks)
wenzelm@48874
    92
  | clean toks = toks;
wenzelm@48874
    93
wenzelm@48874
    94
fun clean_tokens toks =
wenzelm@48874
    95
  ((0 upto length toks - 1) ~~ toks)
wenzelm@48874
    96
  |> filter (fn (_, tok) => Token.is_proper tok)
wenzelm@48874
    97
  |> clean;
wenzelm@48874
    98
wenzelm@48874
    99
fun find_file toks =
wenzelm@48874
   100
  rev (clean_tokens toks) |> get_first (fn (i, tok) =>
wenzelm@48881
   101
    if Token.is_name tok then
wenzelm@48886
   102
      SOME (i, Path.explode (Token.content_of tok))
wenzelm@48886
   103
        handle ERROR msg => error (msg ^ Token.pos_of tok)
wenzelm@48874
   104
    else NONE);
wenzelm@48874
   105
wenzelm@48874
   106
in
wenzelm@48874
   107
wenzelm@48888
   108
fun resolve_files master_dir span =
wenzelm@48874
   109
  (case span of
wenzelm@48878
   110
    Thy_Syntax.Span (Thy_Syntax.Command (cmd, pos), toks) =>
wenzelm@48869
   111
      if Keyword.is_theory_load cmd then
wenzelm@48880
   112
        (case find_file toks of
wenzelm@48880
   113
          NONE => error ("Bad file argument of command " ^ quote cmd ^ Position.str_of pos)
wenzelm@48874
   114
        | SOME (i, path) =>
wenzelm@48869
   115
            let
wenzelm@48869
   116
              val toks' = toks |> map_index (fn (j, tok) =>
wenzelm@48888
   117
                if i = j then Token.put_files (read_files cmd master_dir path) tok
wenzelm@48869
   118
                else tok);
wenzelm@48878
   119
            in Thy_Syntax.Span (Thy_Syntax.Command (cmd, pos), toks') end)
wenzelm@48869
   120
      else span
wenzelm@48874
   121
  | span => span);
wenzelm@48869
   122
wenzelm@48869
   123
end;
wenzelm@48869
   124
wenzelm@48869
   125
wenzelm@23872
   126
(* check files *)
wenzelm@6232
   127
wenzelm@46737
   128
val thy_path = Path.ext "thy";
wenzelm@46737
   129
wenzelm@48898
   130
fun check_thy dir thy_name =
wenzelm@23872
   131
  let
wenzelm@48876
   132
    val path = thy_path (Path.basic thy_name);
wenzelm@46737
   133
    val master_file = check_file dir path;
wenzelm@42003
   134
    val text = File.read master_file;
wenzelm@48876
   135
wenzelm@48876
   136
    val {name, imports, uses, keywords} = Thy_Header.read (Path.position master_file) text;
wenzelm@48876
   137
    val _ = thy_name <> name andalso
wenzelm@48876
   138
      error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name);
wenzelm@48874
   139
  in
wenzelm@48874
   140
   {master = (master_file, SHA1.digest text), text = text,
wenzelm@48898
   141
    imports = imports, uses = uses, keywords = keywords}
wenzelm@48874
   142
  end;
wenzelm@6168
   143
wenzelm@37949
   144
wenzelm@43702
   145
(* load files *)
wenzelm@43702
   146
wenzelm@43702
   147
fun load_file thy src_path =
wenzelm@43702
   148
  let
wenzelm@43702
   149
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   150
    val text = File.read full_path;
wenzelm@43702
   151
    val id = SHA1.digest text;
wenzelm@43702
   152
  in ((full_path, id), text) end;
wenzelm@43702
   153
wenzelm@43702
   154
fun use_file src_path thy =
wenzelm@43702
   155
  let
wenzelm@48896
   156
    val ((_, id), text) = load_file thy src_path;
wenzelm@48896
   157
    val thy' = provide (src_path, id) thy;
wenzelm@43702
   158
  in (text, thy') end;
wenzelm@37949
   159
wenzelm@48896
   160
fun loaded_files thy =
wenzelm@48896
   161
  let val {master_dir, provided, ...} = Files.get thy
wenzelm@48896
   162
  in map (File.full_path master_dir o #1) provided end;
wenzelm@6168
   163
wenzelm@48886
   164
fun load_current thy =
wenzelm@48886
   165
  #provided (Files.get thy) |>
wenzelm@48896
   166
    forall (fn (src_path, id) =>
wenzelm@43702
   167
      (case try (load_file thy) src_path of
wenzelm@37949
   168
        NONE => false
wenzelm@48886
   169
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   170
wenzelm@37949
   171
wenzelm@37949
   172
(* provide files *)
wenzelm@37949
   173
wenzelm@43700
   174
fun eval_file path text = ML_Context.eval_text true (Path.position path) text;
wenzelm@37949
   175
wenzelm@37949
   176
fun use_ml src_path =
wenzelm@37949
   177
  if is_none (Context.thread_data ()) then
wenzelm@43700
   178
    let val path = check_file Path.current src_path
wenzelm@43700
   179
    in eval_file path (File.read path) end
wenzelm@37949
   180
  else
wenzelm@37949
   181
    let
wenzelm@37949
   182
      val thy = ML_Context.the_global_context ();
wenzelm@37949
   183
wenzelm@43702
   184
      val ((path, id), text) = load_file thy src_path;
wenzelm@43700
   185
      val _ = eval_file path text;
wenzelm@37949
   186
      val _ = Context.>> Local_Theory.propagate_ml_env;
wenzelm@37949
   187
wenzelm@48896
   188
      val provide = provide (src_path, id);
wenzelm@38757
   189
      val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
wenzelm@37978
   190
    in () end;
wenzelm@37949
   191
wenzelm@37949
   192
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
wenzelm@6168
   193
wenzelm@37977
   194
wenzelm@43712
   195
(* load_thy *)
wenzelm@37977
   196
wenzelm@48888
   197
fun begin_theory master_dir {name, imports, keywords, uses} parents =
wenzelm@37977
   198
  Theory.begin_theory name parents
wenzelm@48888
   199
  |> put_deps master_dir imports
wenzelm@46938
   200
  |> fold Thy_Header.declare_keyword keywords
wenzelm@37977
   201
  |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
wenzelm@37977
   202
  |> Theory.checkpoint;
wenzelm@37977
   203
wenzelm@46959
   204
fun excursion init elements =
wenzelm@46959
   205
  let
wenzelm@46959
   206
    val immediate = not (Goal.future_enabled ());
wenzelm@46959
   207
wenzelm@46959
   208
    fun proof_result (tr, trs) (st, _) =
wenzelm@46959
   209
      let
wenzelm@46959
   210
        val (result, st') = Toplevel.proof_result immediate (tr, trs) st;
wenzelm@46959
   211
        val pos' = Toplevel.pos_of (List.last (tr :: trs));
wenzelm@46959
   212
      in (result, (st', pos')) end;
wenzelm@46959
   213
wenzelm@46959
   214
    fun element_result elem x =
wenzelm@46959
   215
      fold_map proof_result
wenzelm@46959
   216
        (Outer_Syntax.read_element (#2 (Outer_Syntax.get_syntax ())) init elem) x;
wenzelm@46959
   217
wenzelm@46959
   218
    val (results, (end_state, end_pos)) =
wenzelm@46959
   219
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@46959
   220
wenzelm@46959
   221
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@46959
   222
  in (flat results, thy) end;
wenzelm@46959
   223
wenzelm@48888
   224
fun load_thy update_time master_dir header pos text parents =
wenzelm@43712
   225
  let
wenzelm@43712
   226
    val time = ! Toplevel.timing;
wenzelm@43712
   227
wenzelm@48867
   228
    val {name, uses, ...} = header;
wenzelm@46958
   229
    val _ = Thy_Header.define_keywords header;
wenzelm@43712
   230
    val _ = Present.init_theory name;
wenzelm@44186
   231
    fun init () =
wenzelm@48888
   232
      begin_theory master_dir header parents
wenzelm@48888
   233
      |> Present.begin_theory update_time master_dir uses;
wenzelm@43712
   234
wenzelm@46959
   235
    val lexs = Keyword.get_lexicons ();
wenzelm@46958
   236
wenzelm@46811
   237
    val toks = Thy_Syntax.parse_tokens lexs pos text;
wenzelm@48888
   238
    val spans = map (resolve_files master_dir) (Thy_Syntax.parse_spans toks);
wenzelm@46959
   239
    val elements = Thy_Syntax.parse_elements spans;
wenzelm@43712
   240
wenzelm@43712
   241
    val _ = Present.theory_source name
wenzelm@43712
   242
      (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
wenzelm@43712
   243
wenzelm@43712
   244
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@46959
   245
    val (results, thy) = cond_timeit time "" (fn () => excursion init elements);
wenzelm@43712
   246
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@43712
   247
wenzelm@43712
   248
    val present =
wenzelm@43712
   249
      singleton (Future.cond_forks {name = "Outer_Syntax.present:" ^ name, group = NONE,
wenzelm@44113
   250
        deps = map Future.task_of results, pri = 0, interrupts = true})
wenzelm@43712
   251
      (fn () =>
wenzelm@46959
   252
        let val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax () in
wenzelm@46959
   253
          Thy_Output.present_thy minor Keyword.command_tags
wenzelm@46959
   254
            (Outer_Syntax.is_markup outer_syntax)
wenzelm@46959
   255
            (maps Future.join results) toks
wenzelm@46959
   256
          |> Buffer.content
wenzelm@46959
   257
          |> Present.theory_output name
wenzelm@46959
   258
        end);
wenzelm@43712
   259
wenzelm@43712
   260
  in (thy, present) end;
wenzelm@43712
   261
wenzelm@42002
   262
wenzelm@42002
   263
(* global master path *)
wenzelm@42002
   264
wenzelm@42002
   265
local
wenzelm@42002
   266
  val master_path = Unsynchronized.ref Path.current;
wenzelm@42002
   267
in
wenzelm@42002
   268
wenzelm@42002
   269
fun set_master_path path = master_path := path;
wenzelm@42002
   270
fun get_master_path () = ! master_path;
wenzelm@42002
   271
wenzelm@6168
   272
end;
wenzelm@6168
   273
wenzelm@42002
   274
end;