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