src/Pure/Thy/thy_load.ML
author wenzelm
Tue Feb 11 17:44:29 2014 +0100 (2014-02-11)
changeset 55431 e0f20a44ff9d
parent 54705 0dff3326d12a
child 55614 e2d71b8b0d95
permissions -rw-r--r--
common Command.Chunk for command source and auxiliary files (static Symbol.Index without actual String content);
more informative type Blob, to allow markup reports;
     1 (*  Title:      Pure/Thy/thy_load.ML
     2     Author:     Makarius
     3 
     4 Management of theory sources and auxiliary files.
     5 *)
     6 
     7 signature THY_LOAD =
     8 sig
     9   val master_directory: theory -> Path.T
    10   val imports_of: theory -> (string * Position.T) list
    11   val thy_path: Path.T -> Path.T
    12   val check_thy: Path.T -> string ->
    13    {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
    14     imports: (string * Position.T) list, keywords: Thy_Header.keywords}
    15   val parse_files: string -> (theory -> Token.file list) parser
    16   val provide: Path.T * SHA1.digest -> theory -> theory
    17   val provide_parse_files: string -> (theory -> Token.file list * theory) parser
    18   val load_file: theory -> Path.T -> (Path.T * SHA1.digest) * string
    19   val loaded_files: theory -> Path.T list
    20   val loaded_files_current: theory -> bool
    21   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    22   val load_thy: bool -> (Toplevel.transition -> Time.time option) -> int -> Path.T ->
    23     Thy_Header.header -> Position.T -> string -> theory list -> theory * (unit -> unit) * int
    24 end;
    25 
    26 structure Thy_Load: THY_LOAD =
    27 struct
    28 
    29 (* manage source files *)
    30 
    31 type files =
    32  {master_dir: Path.T,  (*master directory of theory source*)
    33   imports: (string * Position.T) list,  (*source specification of imports*)
    34   provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
    35 
    36 fun make_files (master_dir, imports, provided): files =
    37  {master_dir = master_dir, imports = imports, provided = provided};
    38 
    39 structure Files = Theory_Data
    40 (
    41   type T = files;
    42   val empty = make_files (Path.current, [], []);
    43   fun extend _ = empty;
    44   fun merge _ = empty;
    45 );
    46 
    47 fun map_files f =
    48   Files.map (fn {master_dir, imports, provided} =>
    49     make_files (f (master_dir, imports, provided)));
    50 
    51 
    52 val master_directory = #master_dir o Files.get;
    53 val imports_of = #imports o Files.get;
    54 
    55 fun put_deps master_dir imports = map_files (fn _ => (master_dir, imports, []));
    56 
    57 
    58 (* theory files *)
    59 
    60 val thy_path = Path.ext "thy";
    61 
    62 fun check_file dir file = File.check_file (File.full_path dir file);
    63 
    64 fun check_thy dir thy_name =
    65   let
    66     val path = thy_path (Path.basic thy_name);
    67     val master_file = check_file dir path;
    68     val text = File.read master_file;
    69 
    70     val {name = (name, pos), imports, keywords} =
    71       Thy_Header.read (Path.position master_file) text;
    72     val _ = thy_name <> name andalso
    73       error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name ^ Position.here pos);
    74   in
    75    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
    76     imports = imports, keywords = keywords}
    77   end;
    78 
    79 
    80 (* load files *)
    81 
    82 fun parse_files cmd =
    83   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
    84     (case Token.get_files tok of
    85       [] =>
    86         let
    87           val master_dir = master_directory thy;
    88           val pos = Token.position_of tok;
    89           val src_paths = Keyword.command_files cmd (Path.explode name);
    90         in map (Command.read_file master_dir pos) src_paths end
    91     | files => map Exn.release files));
    92 
    93 fun provide (src_path, id) =
    94   map_files (fn (master_dir, imports, provided) =>
    95     if AList.defined (op =) provided src_path then
    96       error ("Duplicate use of source file: " ^ Path.print src_path)
    97     else (master_dir, imports, (src_path, id) :: provided));
    98 
    99 fun provide_parse_files cmd =
   100   parse_files cmd >> (fn files => fn thy =>
   101     let
   102       val fs = files thy;
   103       val thy' = fold (fn {src_path, text, ...} => provide (src_path, SHA1.digest text)) fs thy;
   104     in (fs, thy') end);
   105 
   106 fun load_file thy src_path =
   107   let
   108     val full_path = check_file (master_directory thy) src_path;
   109     val text = File.read full_path;
   110     val id = SHA1.digest text;
   111   in ((full_path, id), text) end;
   112 
   113 fun loaded_files_current thy =
   114   #provided (Files.get thy) |>
   115     forall (fn (src_path, id) =>
   116       (case try (load_file thy) src_path of
   117         NONE => false
   118       | SOME ((_, id'), _) => id = id'));
   119 
   120 (*Proof General legacy*)
   121 fun loaded_files thy =
   122   let val {master_dir, provided, ...} = Files.get thy
   123   in map (File.full_path master_dir o #1) provided end;
   124 
   125 
   126 (* load theory *)
   127 
   128 fun begin_theory master_dir {name, imports, keywords} parents =
   129   Theory.begin_theory name parents
   130   |> put_deps master_dir imports
   131   |> fold Thy_Header.declare_keyword keywords;
   132 
   133 fun excursion master_dir last_timing init elements =
   134   let
   135     fun prepare_span span =
   136       Thy_Syntax.span_content span
   137       |> Command.read init master_dir []
   138       |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
   139 
   140     fun element_result span_elem (st, _) =
   141       let
   142         val elem = Thy_Syntax.map_element prepare_span span_elem;
   143         val (results, st') = Toplevel.element_result elem st;
   144         val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
   145       in (results, (st', pos')) end;
   146 
   147     val (results, (end_state, end_pos)) =
   148       fold_map element_result elements (Toplevel.toplevel, Position.none);
   149 
   150     val thy = Toplevel.end_theory end_pos end_state;
   151   in (results, thy) end;
   152 
   153 fun load_thy document last_timing update_time master_dir header text_pos text parents =
   154   let
   155     val time = ! Toplevel.timing;
   156 
   157     val {name = (name, _), ...} = header;
   158     val _ = Thy_Header.define_keywords header;
   159 
   160     val lexs = Keyword.get_lexicons ();
   161     val toks = Thy_Syntax.parse_tokens lexs text_pos text;
   162     val spans = Thy_Syntax.parse_spans toks;
   163     val elements = Thy_Syntax.parse_elements spans;
   164 
   165     fun init () =
   166       begin_theory master_dir header parents
   167       |> Present.begin_theory update_time
   168           (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
   169 
   170     val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
   171     val (results, thy) =
   172       cond_timeit time "" (fn () => excursion master_dir last_timing init elements);
   173     val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
   174 
   175     fun present () =
   176       let
   177         val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
   178         val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax ();
   179       in
   180         if exists (Toplevel.is_skipped_proof o #2) res then
   181           warning ("Cannot present theory with skipped proofs: " ^ quote name)
   182         else
   183           let val tex_source =
   184             Thy_Output.present_thy minor Keyword.command_tags
   185               (Outer_Syntax.is_markup outer_syntax) res toks
   186             |> Buffer.content;
   187           in if document then Present.theory_output name tex_source else () end
   188       end;
   189 
   190   in (thy, present, size text) end;
   191 
   192 
   193 (* document antiquotation *)
   194 
   195 fun file_antiq do_check ctxt (name, pos) =
   196   let
   197     val dir = master_directory (Proof_Context.theory_of ctxt);
   198     val path = Path.append dir (Path.explode name);
   199     val _ =
   200       if not do_check orelse File.exists path then ()
   201       else error ("Bad file: " ^ Path.print (Path.expand path) ^ Position.here pos);
   202     val _ = Position.report pos (Markup.path name);
   203   in
   204     space_explode "/" name
   205     |> map Thy_Output.verb_text
   206     |> space_implode (Thy_Output.verb_text "/" ^ "\\discretionary{}{}{}")
   207   end;
   208 
   209 val _ = Theory.setup
   210   (Thy_Output.antiquotation (Binding.name "file") (Scan.lift (Parse.position Parse.path))
   211     (file_antiq true o #context) #>
   212   (Thy_Output.antiquotation (Binding.name "file_unchecked") (Scan.lift (Parse.position Parse.path))
   213     (file_antiq false o #context)));
   214 
   215 end;