src/Pure/PIDE/resources.ML
author wenzelm
Mon Mar 13 21:37:35 2017 +0100 (2017-03-13 ago)
changeset 65214 a2ec0db555c7
parent 65058 3e9f382fb67e
child 65431 4a3e6cda3b94
permissions -rw-r--r--
clarified modules;
     1 (*  Title:      Pure/PIDE/resources.ML
     2     Author:     Makarius
     3 
     4 Resources for theories and auxiliary files.
     5 *)
     6 
     7 signature RESOURCES =
     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 loaded_files_current: theory -> bool
    19   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    20   val load_thy: bool -> HTML.symbols -> (Toplevel.transition -> Time.time) -> int ->
    21     Path.T -> Thy_Header.header -> Position.T -> string -> theory list ->
    22     theory * (unit -> unit) * int
    23 end;
    24 
    25 structure Resources: RESOURCES =
    26 struct
    27 
    28 (* manage source files *)
    29 
    30 type files =
    31  {master_dir: Path.T,  (*master directory of theory source*)
    32   imports: (string * Position.T) list,  (*source specification of imports*)
    33   provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
    34 
    35 fun make_files (master_dir, imports, provided): files =
    36  {master_dir = master_dir, imports = imports, provided = provided};
    37 
    38 structure Files = Theory_Data
    39 (
    40   type T = files;
    41   val empty = make_files (Path.current, [], []);
    42   fun extend _ = empty;
    43   fun merge _ = empty;
    44 );
    45 
    46 fun map_files f =
    47   Files.map (fn {master_dir, imports, provided} =>
    48     make_files (f (master_dir, imports, provided)));
    49 
    50 
    51 val master_directory = #master_dir o Files.get;
    52 val imports_of = #imports o Files.get;
    53 
    54 fun put_deps master_dir imports = map_files (fn _ => (master_dir, imports, []));
    55 
    56 
    57 (* theory files *)
    58 
    59 val thy_path = Path.ext "thy";
    60 
    61 fun check_file dir file = File.check_file (File.full_path dir file);
    62 
    63 fun check_thy dir thy_name =
    64   let
    65     val path = thy_path (Path.basic thy_name);
    66     val master_file = check_file dir path;
    67     val text = File.read master_file;
    68 
    69     val {name = (name, pos), imports, keywords} =
    70       Thy_Header.read (Path.position master_file) text;
    71     val _ = thy_name <> name andalso
    72       error ("Bad theory name " ^ quote name ^
    73         " for file " ^ Path.print path ^ 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 keywords = Thy_Header.get_keywords thy;
    88           val master_dir = master_directory thy;
    89           val pos = Token.pos_of tok;
    90           val src_paths = Keyword.command_files keywords cmd (Path.explode name);
    91         in map (Command.read_file master_dir pos) src_paths end
    92     | files => map Exn.release files));
    93 
    94 fun provide (src_path, id) =
    95   map_files (fn (master_dir, imports, provided) =>
    96     if AList.defined (op =) provided src_path then
    97       error ("Duplicate use of source file: " ^ Path.print src_path)
    98     else (master_dir, imports, (src_path, id) :: provided));
    99 
   100 fun provide_parse_files cmd =
   101   parse_files cmd >> (fn files => fn thy =>
   102     let
   103       val fs = files thy;
   104       val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
   105     in (fs, thy') end);
   106 
   107 fun load_file thy src_path =
   108   let
   109     val full_path = check_file (master_directory thy) src_path;
   110     val text = File.read full_path;
   111     val id = SHA1.digest text;
   112   in ((full_path, id), text) end;
   113 
   114 fun loaded_files_current thy =
   115   #provided (Files.get thy) |>
   116     forall (fn (src_path, id) =>
   117       (case try (load_file thy) src_path of
   118         NONE => false
   119       | SOME ((_, id'), _) => id = id'));
   120 
   121 
   122 (* load theory *)
   123 
   124 fun begin_theory master_dir {name, imports, keywords} parents =
   125   Theory.begin_theory name parents
   126   |> put_deps master_dir imports
   127   |> Thy_Header.add_keywords keywords;
   128 
   129 fun excursion keywords master_dir last_timing init elements =
   130   let
   131     fun prepare_span st span =
   132       Command_Span.content span
   133       |> Command.read keywords (Command.read_thy st) master_dir init ([], ~1)
   134       |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
   135 
   136     fun element_result span_elem (st, _) =
   137       let
   138         val elem = Thy_Syntax.map_element (prepare_span st) span_elem;
   139         val (results, st') = Toplevel.element_result keywords elem st;
   140         val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
   141       in (results, (st', pos')) end;
   142 
   143     val (results, (end_state, end_pos)) =
   144       fold_map element_result elements (Toplevel.toplevel, Position.none);
   145 
   146     val thy = Toplevel.end_theory end_pos end_state;
   147   in (results, thy) end;
   148 
   149 fun load_thy document symbols last_timing update_time master_dir header text_pos text parents =
   150   let
   151     val (name, _) = #name header;
   152     val keywords =
   153       fold (curry Keyword.merge_keywords o Thy_Header.get_keywords) parents
   154         (Keyword.add_keywords (#keywords header) Keyword.empty_keywords);
   155 
   156     val toks = Token.explode keywords text_pos text;
   157     val spans = Outer_Syntax.parse_spans toks;
   158     val elements = Thy_Syntax.parse_elements keywords spans;
   159 
   160     fun init () =
   161       begin_theory master_dir header parents
   162       |> Present.begin_theory update_time
   163         (fn () => implode (map (HTML.present_span symbols keywords) spans));
   164 
   165     val (results, thy) =
   166       cond_timeit true ("theory " ^ quote name)
   167         (fn () => excursion keywords master_dir last_timing init elements);
   168 
   169     fun present () =
   170       let
   171         val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
   172       in
   173         if exists (Toplevel.is_skipped_proof o #2) res then
   174           warning ("Cannot present theory with skipped proofs: " ^ quote name)
   175         else
   176           let val tex_source = Thy_Output.present_thy thy res toks |> Buffer.content;
   177           in if document then Present.theory_output name tex_source else () end
   178       end;
   179 
   180   in (thy, present, size text) end;
   181 
   182 
   183 (* antiquotations *)
   184 
   185 local
   186 
   187 fun err msg pos = error (msg ^ Position.here pos);
   188 
   189 fun check_path check_file ctxt dir (name, pos) =
   190   let
   191     val _ = Context_Position.report ctxt pos Markup.language_path;
   192 
   193     val path = Path.append dir (Path.explode name) handle ERROR msg => err msg pos;
   194     val _ = Path.expand path handle ERROR msg => err msg pos;
   195     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   196     val _ =
   197       (case check_file of
   198         NONE => path
   199       | SOME check => (check path handle ERROR msg => err msg pos));
   200   in path end;
   201 
   202 fun document_antiq check_file ctxt (name, pos) =
   203   let
   204     val dir = master_directory (Proof_Context.theory_of ctxt);
   205     val _ = check_path check_file ctxt dir (name, pos);
   206   in
   207     space_explode "/" name
   208     |> map Latex.output_ascii
   209     |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
   210     |> enclose "\\isatt{" "}"
   211   end;
   212 
   213 fun ML_antiq check_file ctxt (name, pos) =
   214   let val path = check_path check_file ctxt Path.current (name, pos);
   215   in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
   216 
   217 in
   218 
   219 val _ = Theory.setup
   220  (Thy_Output.antiquotation @{binding path} (Scan.lift (Parse.position Parse.path))
   221     (document_antiq NONE o #context) #>
   222   Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
   223     (document_antiq (SOME File.check_file) o #context) #>
   224   Thy_Output.antiquotation @{binding dir} (Scan.lift (Parse.position Parse.path))
   225     (document_antiq (SOME File.check_dir) o #context) #>
   226   ML_Antiquotation.value @{binding path}
   227     (Args.context -- Scan.lift (Parse.position Parse.path)
   228       >> uncurry (ML_antiq NONE)) #>
   229   ML_Antiquotation.value @{binding file}
   230     (Args.context -- Scan.lift (Parse.position Parse.path)
   231       >> uncurry (ML_antiq (SOME File.check_file))) #>
   232   ML_Antiquotation.value @{binding dir}
   233     (Args.context -- Scan.lift (Parse.position Parse.path)
   234       >> uncurry (ML_antiq (SOME File.check_dir))));
   235 
   236 end;
   237 
   238 end;