src/Pure/PIDE/resources.ML
author wenzelm
Tue Oct 21 09:50:22 2014 +0200 (2014-10-21 ago)
changeset 58741 6e7b009e6d94
parent 58716 23a380cc45f4
child 58849 ef7700ecce83
permissions -rw-r--r--
clarified verbatim line breaks, e.g. relevant for Implementation mldecls;
     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: theory -> Path.T list
    19   val loaded_files_current: theory -> bool
    20   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    21   val load_thy: bool -> (Toplevel.transition -> Time.time option) -> int -> Path.T ->
    22     Thy_Header.header -> Position.T -> string -> theory list -> 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 file name " ^ Path.print path ^ " for theory " ^ quote name ^ Position.here pos);
    73   in
    74    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
    75     imports = imports, keywords = keywords}
    76   end;
    77 
    78 
    79 (* load files *)
    80 
    81 fun parse_files cmd =
    82   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
    83     (case Token.get_files tok of
    84       [] =>
    85         let
    86           val master_dir = master_directory thy;
    87           val pos = Token.pos_of tok;
    88           val src_paths = Keyword.command_files cmd (Path.explode name);
    89         in map (Command.read_file master_dir pos) src_paths end
    90     | files => map Exn.release files));
    91 
    92 fun provide (src_path, id) =
    93   map_files (fn (master_dir, imports, provided) =>
    94     if AList.defined (op =) provided src_path then
    95       error ("Duplicate use of source file: " ^ Path.print src_path)
    96     else (master_dir, imports, (src_path, id) :: provided));
    97 
    98 fun provide_parse_files cmd =
    99   parse_files cmd >> (fn files => fn thy =>
   100     let
   101       val fs = files thy;
   102       val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
   103     in (fs, thy') end);
   104 
   105 fun load_file thy src_path =
   106   let
   107     val full_path = check_file (master_directory thy) src_path;
   108     val text = File.read full_path;
   109     val id = SHA1.digest text;
   110   in ((full_path, id), text) end;
   111 
   112 fun loaded_files_current thy =
   113   #provided (Files.get thy) |>
   114     forall (fn (src_path, id) =>
   115       (case try (load_file thy) src_path of
   116         NONE => false
   117       | SOME ((_, id'), _) => id = id'));
   118 
   119 (*Proof General legacy*)
   120 fun loaded_files thy =
   121   let val {master_dir, provided, ...} = Files.get thy
   122   in map (File.full_path master_dir o #1) provided end;
   123 
   124 
   125 (* load theory *)
   126 
   127 fun begin_theory master_dir {name, imports, keywords} parents =
   128   Theory.begin_theory name parents
   129   |> put_deps master_dir imports
   130   |> fold Thy_Header.declare_keyword keywords;
   131 
   132 fun excursion master_dir last_timing init elements =
   133   let
   134     fun prepare_span span =
   135       Command_Span.content span
   136       |> Command.read init master_dir []
   137       |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
   138 
   139     fun element_result span_elem (st, _) =
   140       let
   141         val elem = Thy_Syntax.map_element prepare_span span_elem;
   142         val (results, st') = Toplevel.element_result elem st;
   143         val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
   144       in (results, (st', pos')) end;
   145 
   146     val (results, (end_state, end_pos)) =
   147       fold_map element_result elements (Toplevel.toplevel, Position.none);
   148 
   149     val thy = Toplevel.end_theory end_pos end_state;
   150   in (results, thy) end;
   151 
   152 fun load_thy document last_timing update_time master_dir header text_pos text parents =
   153   let
   154     val time = ! Toplevel.timing;
   155 
   156     val {name = (name, _), ...} = header;
   157     val _ = Thy_Header.define_keywords header;
   158 
   159     val lexs = Keyword.get_lexicons ();
   160     val toks = Outer_Syntax.scan lexs text_pos text;
   161     val spans = Outer_Syntax.parse_spans toks;
   162     val elements = Thy_Syntax.parse_elements spans;
   163 
   164     fun init () =
   165       begin_theory master_dir header parents
   166       |> Present.begin_theory update_time
   167           (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
   168 
   169     val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
   170     val (results, thy) =
   171       cond_timeit time "" (fn () => excursion master_dir last_timing init elements);
   172     val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
   173 
   174     fun present () =
   175       let
   176         val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
   177         val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax ();
   178       in
   179         if exists (Toplevel.is_skipped_proof o #2) res then
   180           warning ("Cannot present theory with skipped proofs: " ^ quote name)
   181         else
   182           let val tex_source =
   183             Thy_Output.present_thy minor Keyword.command_tags
   184               (Outer_Syntax.is_markup outer_syntax) res toks
   185             |> Buffer.content;
   186           in if document then Present.theory_output name tex_source else () end
   187       end;
   188 
   189   in (thy, present, size text) end;
   190 
   191 
   192 (* antiquotations *)
   193 
   194 local
   195 
   196 fun check_path strict ctxt dir (name, pos) =
   197   let
   198     val _ = Context_Position.report ctxt pos Markup.language_path;
   199 
   200     val path = Path.append dir (Path.explode name)
   201       handle ERROR msg => error (msg ^ Position.here pos);
   202 
   203     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   204     val _ =
   205       if can Path.expand path andalso File.exists path then ()
   206       else
   207         let
   208           val path' = perhaps (try Path.expand) path;
   209           val msg = "Bad file: " ^ Path.print path' ^ Position.here pos;
   210         in
   211           if strict then error msg
   212           else if Context_Position.is_visible ctxt then
   213             Output.report
   214               [Markup.markup (Markup.bad |> Markup.properties (Position.properties_of pos)) msg]
   215           else ()
   216         end;
   217   in path end;
   218 
   219 fun file_antiq strict ctxt (name, pos) =
   220   let
   221     val dir = master_directory (Proof_Context.theory_of ctxt);
   222     val _ = check_path strict ctxt dir (name, pos);
   223   in
   224     space_explode "/" name
   225     |> map Latex.output_ascii
   226     |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
   227     |> enclose "\\isatt{" "}"
   228   end;
   229 
   230 in
   231 
   232 val _ = Theory.setup
   233  (Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
   234     (file_antiq true o #context) #>
   235   Thy_Output.antiquotation @{binding file_unchecked} (Scan.lift (Parse.position Parse.path))
   236     (file_antiq false o #context) #>
   237   ML_Antiquotation.value @{binding path}
   238     (Args.context -- Scan.lift (Parse.position Parse.path) >> (fn (ctxt, arg) =>
   239       let val path = check_path true ctxt Path.current arg
   240       in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end)));
   241 
   242 end;
   243 
   244 end;