src/Pure/Thy/thy_load.ML
author wenzelm
Sat Nov 16 18:08:57 2013 +0100 (2013-11-16 ago)
changeset 54451 459cf6ee254e
parent 54450 7815563f50dc
child 54454 044faa8a8080
permissions -rw-r--r--
tuned signature;
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@48927
    10
  val imports_of: theory -> (string * Position.T) list
wenzelm@46737
    11
  val thy_path: Path.T -> Path.T
wenzelm@54451
    12
  val read_files: Path.T -> string -> Path.T * Position.T -> Token.file list
wenzelm@48906
    13
  val parse_files: string -> (theory -> Token.file list) parser
wenzelm@48898
    14
  val check_thy: Path.T -> string ->
wenzelm@48928
    15
   {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
wenzelm@51293
    16
    imports: (string * Position.T) list, keywords: Thy_Header.keywords}
wenzelm@48906
    17
  val provide: Path.T * SHA1.digest -> theory -> theory
wenzelm@48906
    18
  val provide_parse_files: string -> (theory -> Token.file list * theory) parser
wenzelm@43702
    19
  val load_file: theory -> Path.T -> (Path.T * SHA1.digest) * string
wenzelm@37949
    20
  val loaded_files: theory -> Path.T list
wenzelm@54446
    21
  val loaded_files_current: theory -> bool
wenzelm@46938
    22
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
wenzelm@51423
    23
  val load_thy: (Toplevel.transition -> Time.time option) -> int -> Path.T -> Thy_Header.header ->
wenzelm@51331
    24
    Position.T -> string -> theory list -> theory * (unit -> unit) * int
wenzelm@6168
    25
end;
wenzelm@6168
    26
wenzelm@37216
    27
structure Thy_Load: THY_LOAD =
wenzelm@6168
    28
struct
wenzelm@6168
    29
wenzelm@37949
    30
(* manage source files *)
wenzelm@37949
    31
wenzelm@37949
    32
type files =
wenzelm@40741
    33
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@48927
    34
  imports: (string * Position.T) list,  (*source specification of imports*)
wenzelm@48896
    35
  provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
wenzelm@37949
    36
wenzelm@48886
    37
fun make_files (master_dir, imports, provided): files =
wenzelm@48886
    38
 {master_dir = master_dir, imports = imports, provided = provided};
wenzelm@37949
    39
wenzelm@37949
    40
structure Files = Theory_Data
wenzelm@37949
    41
(
wenzelm@37949
    42
  type T = files;
wenzelm@48886
    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@48886
    49
  Files.map (fn {master_dir, imports, provided} =>
wenzelm@48886
    50
    make_files (f (master_dir, imports, 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@48888
    56
fun put_deps master_dir imports = map_files (fn _ => (master_dir, imports, []));
wenzelm@37949
    57
wenzelm@37949
    58
wenzelm@54451
    59
(* auxiliary files *)
wenzelm@48869
    60
wenzelm@48878
    61
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@48878
    62
wenzelm@54451
    63
fun read_files dir cmd (path, pos) =
wenzelm@48898
    64
  let
wenzelm@48919
    65
    fun make_file file =
wenzelm@48919
    66
      let
wenzelm@50201
    67
        val _ = Position.report pos (Markup.path (Path.implode file));
wenzelm@48919
    68
        val full_path = check_file dir file;
wenzelm@48919
    69
      in {src_path = file, text = File.read full_path, pos = Path.position full_path} end;
wenzelm@48898
    70
    val paths =
wenzelm@48898
    71
      (case Keyword.command_files cmd of
wenzelm@48898
    72
        [] => [path]
wenzelm@48898
    73
      | exts => map (fn ext => Path.ext ext path) exts);
wenzelm@48919
    74
  in map make_file paths end;
wenzelm@48898
    75
wenzelm@48898
    76
fun parse_files cmd =
wenzelm@48898
    77
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
wenzelm@48898
    78
    (case Token.get_files tok of
wenzelm@48898
    79
      SOME files => files
wenzelm@54451
    80
    | NONE => read_files (master_directory thy) cmd (Path.explode name, Token.position_of tok)));
wenzelm@48869
    81
wenzelm@48869
    82
wenzelm@54451
    83
(* theory files *)
wenzelm@6232
    84
wenzelm@46737
    85
val thy_path = Path.ext "thy";
wenzelm@46737
    86
wenzelm@48898
    87
fun check_thy dir thy_name =
wenzelm@23872
    88
  let
wenzelm@48876
    89
    val path = thy_path (Path.basic thy_name);
wenzelm@46737
    90
    val master_file = check_file dir path;
wenzelm@42003
    91
    val text = File.read master_file;
wenzelm@48876
    92
wenzelm@51294
    93
    val {name = (name, pos), imports, keywords} =
wenzelm@48927
    94
      Thy_Header.read (Path.position master_file) text;
wenzelm@48876
    95
    val _ = thy_name <> name andalso
wenzelm@48992
    96
      error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name ^ Position.here pos);
wenzelm@48874
    97
  in
wenzelm@48928
    98
   {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
wenzelm@51293
    99
    imports = imports, keywords = keywords}
wenzelm@48874
   100
  end;
wenzelm@6168
   101
wenzelm@37949
   102
wenzelm@43702
   103
(* load files *)
wenzelm@43702
   104
wenzelm@48906
   105
fun provide (src_path, id) =
wenzelm@48906
   106
  map_files (fn (master_dir, imports, provided) =>
wenzelm@48906
   107
    if AList.defined (op =) provided src_path then
wenzelm@48906
   108
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48906
   109
    else (master_dir, imports, (src_path, id) :: provided));
wenzelm@48906
   110
wenzelm@48906
   111
fun provide_parse_files cmd =
wenzelm@48906
   112
  parse_files cmd >> (fn files => fn thy =>
wenzelm@48906
   113
    let
wenzelm@48906
   114
      val fs = files thy;
wenzelm@48906
   115
      val thy' = fold (fn {src_path, text, ...} => provide (src_path, SHA1.digest text)) fs thy;
wenzelm@48906
   116
    in (fs, thy') end);
wenzelm@48906
   117
wenzelm@43702
   118
fun load_file thy src_path =
wenzelm@43702
   119
  let
wenzelm@43702
   120
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   121
    val text = File.read full_path;
wenzelm@43702
   122
    val id = SHA1.digest text;
wenzelm@43702
   123
  in ((full_path, id), text) end;
wenzelm@43702
   124
wenzelm@54446
   125
(*Proof General legacy*)
wenzelm@48896
   126
fun loaded_files thy =
wenzelm@48896
   127
  let val {master_dir, provided, ...} = Files.get thy
wenzelm@48896
   128
  in map (File.full_path master_dir o #1) provided end;
wenzelm@6168
   129
wenzelm@54446
   130
fun loaded_files_current thy =
wenzelm@48886
   131
  #provided (Files.get thy) |>
wenzelm@48896
   132
    forall (fn (src_path, id) =>
wenzelm@43702
   133
      (case try (load_file thy) src_path of
wenzelm@37949
   134
        NONE => false
wenzelm@48886
   135
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   136
wenzelm@37949
   137
wenzelm@43712
   138
(* load_thy *)
wenzelm@37977
   139
wenzelm@51294
   140
fun begin_theory master_dir {name, imports, keywords} parents =
wenzelm@37977
   141
  Theory.begin_theory name parents
wenzelm@48888
   142
  |> put_deps master_dir imports
wenzelm@52788
   143
  |> fold Thy_Header.declare_keyword keywords;
wenzelm@37977
   144
wenzelm@51217
   145
fun excursion last_timing init elements =
wenzelm@46959
   146
  let
wenzelm@51268
   147
    fun prepare_span span =
wenzelm@51268
   148
      Thy_Syntax.span_content span
wenzelm@52534
   149
      |> Command.read init
wenzelm@51268
   150
      |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
wenzelm@51217
   151
wenzelm@51268
   152
    fun element_result span_elem (st, _) =
wenzelm@46959
   153
      let
wenzelm@51273
   154
        val elem = Thy_Syntax.map_element prepare_span span_elem;
wenzelm@51273
   155
        val (results, st') = Toplevel.element_result elem st;
wenzelm@51273
   156
        val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
wenzelm@51268
   157
      in (results, (st', pos')) end;
wenzelm@46959
   158
wenzelm@46959
   159
    val (results, (end_state, end_pos)) =
wenzelm@46959
   160
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@46959
   161
wenzelm@46959
   162
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@51273
   163
  in (results, thy) end;
wenzelm@46959
   164
wenzelm@51217
   165
fun load_thy last_timing update_time master_dir header text_pos text parents =
wenzelm@43712
   166
  let
wenzelm@43712
   167
    val time = ! Toplevel.timing;
wenzelm@43712
   168
wenzelm@51294
   169
    val {name = (name, _), ...} = header;
wenzelm@46958
   170
    val _ = Thy_Header.define_keywords header;
wenzelm@43712
   171
    val _ = Present.init_theory name;
wenzelm@44186
   172
    fun init () =
wenzelm@48888
   173
      begin_theory master_dir header parents
wenzelm@51293
   174
      |> Present.begin_theory update_time master_dir;
wenzelm@43712
   175
wenzelm@46959
   176
    val lexs = Keyword.get_lexicons ();
wenzelm@46958
   177
wenzelm@48927
   178
    val toks = Thy_Syntax.parse_tokens lexs text_pos text;
wenzelm@54451
   179
    val spans = map (Thy_Syntax.resolve_files (read_files master_dir)) (Thy_Syntax.parse_spans toks);
wenzelm@46959
   180
    val elements = Thy_Syntax.parse_elements spans;
wenzelm@43712
   181
wenzelm@43712
   182
    val _ = Present.theory_source name
wenzelm@43712
   183
      (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
wenzelm@43712
   184
wenzelm@43712
   185
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@51217
   186
    val (results, thy) = cond_timeit time "" (fn () => excursion last_timing init elements);
wenzelm@43712
   187
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@43712
   188
wenzelm@51331
   189
    fun present () =
wenzelm@51331
   190
      let
wenzelm@51332
   191
        val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
wenzelm@51331
   192
        val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax ();
wenzelm@51331
   193
      in
wenzelm@51556
   194
        if exists (Toplevel.is_skipped_proof o #2) res then
wenzelm@51556
   195
          warning ("Cannot present theory with skipped proofs: " ^ quote name)
wenzelm@51556
   196
        else
wenzelm@51556
   197
          Thy_Output.present_thy minor Keyword.command_tags
wenzelm@51556
   198
            (Outer_Syntax.is_markup outer_syntax) res toks
wenzelm@51556
   199
          |> Buffer.content
wenzelm@51556
   200
          |> Present.theory_output name
wenzelm@51331
   201
      end;
wenzelm@43712
   202
wenzelm@51331
   203
  in (thy, present, size text) end;
wenzelm@43712
   204
wenzelm@42002
   205
wenzelm@49244
   206
(* document antiquotation *)
wenzelm@49244
   207
wenzelm@53171
   208
val _ = Theory.setup
wenzelm@53171
   209
  (Thy_Output.antiquotation (Binding.name "file") (Scan.lift (Parse.position Parse.path))
wenzelm@49244
   210
    (fn {context = ctxt, ...} => fn (name, pos) =>
wenzelm@49244
   211
      let
wenzelm@49244
   212
        val dir = master_directory (Proof_Context.theory_of ctxt);
wenzelm@49244
   213
        val path = Path.append dir (Path.explode name);
wenzelm@49244
   214
        val _ =
wenzelm@49244
   215
          if File.exists path then ()
wenzelm@49244
   216
          else error ("Bad file: " ^ Path.print (Path.expand path) ^ Position.here pos);
wenzelm@50201
   217
        val _ = Position.report pos (Markup.path name);
wenzelm@52752
   218
      in
wenzelm@52752
   219
        space_explode "/" name
wenzelm@52752
   220
        |> map Thy_Output.verb_text
wenzelm@52752
   221
        |> space_implode (Thy_Output.verb_text "/" ^ "\\discretionary{}{}{}")
wenzelm@53171
   222
      end));
wenzelm@49244
   223
wenzelm@6168
   224
end;