src/Pure/PIDE/resources.ML
author wenzelm
Sat Apr 08 21:09:34 2017 +0200 (2017-04-08 ago)
changeset 65442 1ca6d8a2a00d
parent 65441 9425e4d8bdb6
child 65443 dccbfc715904
permissions -rw-r--r--
clarified;
     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 master_file =
   107       (case known_theory thy_name of
   108         SOME known_path => check_file Path.current known_path
   109       | NONE => check_file dir (thy_path (Path.basic thy_base_name)));
   110     val text = File.read master_file;
   111 
   112     val {name = (name, pos), imports, keywords} =
   113       Thy_Header.read (Path.position master_file) text;
   114     val _ =
   115       thy_base_name <> name andalso
   116         error ("Bad theory name " ^ quote name ^
   117           " for file " ^ Path.print (Path.base master_file) ^ Position.here pos);
   118   in
   119    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
   120     imports = imports, keywords = keywords}
   121   end;
   122 
   123 
   124 (* load files *)
   125 
   126 fun parse_files cmd =
   127   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
   128     (case Token.get_files tok of
   129       [] =>
   130         let
   131           val keywords = Thy_Header.get_keywords thy;
   132           val master_dir = master_directory thy;
   133           val pos = Token.pos_of tok;
   134           val src_paths = Keyword.command_files keywords cmd (Path.explode name);
   135         in map (Command.read_file master_dir pos) src_paths end
   136     | files => map Exn.release files));
   137 
   138 fun provide (src_path, id) =
   139   map_files (fn (master_dir, imports, provided) =>
   140     if AList.defined (op =) provided src_path then
   141       error ("Duplicate use of source file: " ^ Path.print src_path)
   142     else (master_dir, imports, (src_path, id) :: provided));
   143 
   144 fun provide_parse_files cmd =
   145   parse_files cmd >> (fn files => fn thy =>
   146     let
   147       val fs = files thy;
   148       val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
   149     in (fs, thy') end);
   150 
   151 fun load_file thy src_path =
   152   let
   153     val full_path = check_file (master_directory thy) src_path;
   154     val text = File.read full_path;
   155     val id = SHA1.digest text;
   156   in ((full_path, id), text) end;
   157 
   158 fun loaded_files_current thy =
   159   #provided (Files.get thy) |>
   160     forall (fn (src_path, id) =>
   161       (case try (load_file thy) src_path of
   162         NONE => false
   163       | SOME ((_, id'), _) => id = id'));
   164 
   165 
   166 (* load theory *)
   167 
   168 fun begin_theory master_dir {name, imports, keywords} parents =
   169   Theory.begin_theory name parents
   170   |> put_deps master_dir imports
   171   |> Thy_Header.add_keywords keywords;
   172 
   173 fun excursion keywords master_dir last_timing init elements =
   174   let
   175     fun prepare_span st span =
   176       Command_Span.content span
   177       |> Command.read keywords (Command.read_thy st) master_dir init ([], ~1)
   178       |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
   179 
   180     fun element_result span_elem (st, _) =
   181       let
   182         val elem = Thy_Syntax.map_element (prepare_span st) span_elem;
   183         val (results, st') = Toplevel.element_result keywords elem st;
   184         val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
   185       in (results, (st', pos')) end;
   186 
   187     val (results, (end_state, end_pos)) =
   188       fold_map element_result elements (Toplevel.toplevel, Position.none);
   189 
   190     val thy = Toplevel.end_theory end_pos end_state;
   191   in (results, thy) end;
   192 
   193 fun load_thy document symbols last_timing update_time master_dir header text_pos text parents =
   194   let
   195     val (name, _) = #name header;
   196     val keywords =
   197       fold (curry Keyword.merge_keywords o Thy_Header.get_keywords) parents
   198         (Keyword.add_keywords (#keywords header) Keyword.empty_keywords);
   199 
   200     val toks = Token.explode keywords text_pos text;
   201     val spans = Outer_Syntax.parse_spans toks;
   202     val elements = Thy_Syntax.parse_elements keywords spans;
   203 
   204     fun init () =
   205       begin_theory master_dir header parents
   206       |> Present.begin_theory update_time
   207         (fn () => implode (map (HTML.present_span symbols keywords) spans));
   208 
   209     val (results, thy) =
   210       cond_timeit true ("theory " ^ quote name)
   211         (fn () => excursion keywords master_dir last_timing init elements);
   212 
   213     fun present () =
   214       let
   215         val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
   216       in
   217         if exists (Toplevel.is_skipped_proof o #2) res then
   218           warning ("Cannot present theory with skipped proofs: " ^ quote name)
   219         else
   220           let val tex_source = Thy_Output.present_thy thy res toks |> Buffer.content;
   221           in if document then Present.theory_output name tex_source else () end
   222       end;
   223 
   224   in (thy, present, size text) end;
   225 
   226 
   227 (* antiquotations *)
   228 
   229 local
   230 
   231 fun err msg pos = error (msg ^ Position.here pos);
   232 
   233 fun check_path check_file ctxt dir (name, pos) =
   234   let
   235     val _ = Context_Position.report ctxt pos Markup.language_path;
   236 
   237     val path = Path.append dir (Path.explode name) handle ERROR msg => err msg pos;
   238     val _ = Path.expand path handle ERROR msg => err msg pos;
   239     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   240     val _ =
   241       (case check_file of
   242         NONE => path
   243       | SOME check => (check path handle ERROR msg => err msg pos));
   244   in path end;
   245 
   246 fun document_antiq check_file ctxt (name, pos) =
   247   let
   248     val dir = master_directory (Proof_Context.theory_of ctxt);
   249     val _ = check_path check_file ctxt dir (name, pos);
   250   in
   251     space_explode "/" name
   252     |> map Latex.output_ascii
   253     |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
   254     |> enclose "\\isatt{" "}"
   255   end;
   256 
   257 fun ML_antiq check_file ctxt (name, pos) =
   258   let val path = check_path check_file ctxt Path.current (name, pos);
   259   in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
   260 
   261 in
   262 
   263 val _ = Theory.setup
   264  (Thy_Output.antiquotation @{binding path} (Scan.lift (Parse.position Parse.path))
   265     (document_antiq NONE o #context) #>
   266   Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
   267     (document_antiq (SOME File.check_file) o #context) #>
   268   Thy_Output.antiquotation @{binding dir} (Scan.lift (Parse.position Parse.path))
   269     (document_antiq (SOME File.check_dir) o #context) #>
   270   ML_Antiquotation.value @{binding path}
   271     (Args.context -- Scan.lift (Parse.position Parse.path)
   272       >> uncurry (ML_antiq NONE)) #>
   273   ML_Antiquotation.value @{binding file}
   274     (Args.context -- Scan.lift (Parse.position Parse.path)
   275       >> uncurry (ML_antiq (SOME File.check_file))) #>
   276   ML_Antiquotation.value @{binding dir}
   277     (Args.context -- Scan.lift (Parse.position Parse.path)
   278       >> uncurry (ML_antiq (SOME File.check_dir))));
   279 
   280 end;
   281 
   282 end;