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