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