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