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