src/Pure/PIDE/resources.ML
author wenzelm
Fri Apr 21 14:09:03 2017 +0200 (2017-04-21 ago)
changeset 65532 febfd9f78bd4
parent 65505 741fad555d82
child 66711 80fa1401cf76
permissions -rw-r--r--
eliminated default_qualifier: just a constant;
     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 default_qualifier: string
    10   val init_session_base:
    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 global_theory: string -> string option
    16   val loaded_theory: string -> string option
    17   val known_theory: string -> Path.T option
    18   val master_directory: theory -> Path.T
    19   val imports_of: theory -> (string * Position.T) list
    20   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    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 end;
    33 
    34 structure Resources: RESOURCES =
    35 struct
    36 
    37 (* session base *)
    38 
    39 val default_qualifier = "Draft";
    40 
    41 val empty_session_base =
    42   {global_theories = Symtab.empty: string Symtab.table,
    43    loaded_theories = Symtab.empty: string Symtab.table,
    44    known_theories = Symtab.empty: Path.T Symtab.table};
    45 
    46 val global_session_base =
    47   Synchronized.var "Sessions.base" empty_session_base;
    48 
    49 fun init_session_base {global_theories, loaded_theories, known_theories} =
    50   Synchronized.change global_session_base
    51     (fn _ =>
    52       {global_theories = Symtab.make global_theories,
    53        loaded_theories = Symtab.make loaded_theories,
    54        known_theories = Symtab.make (map (apsnd Path.explode) known_theories)});
    55 
    56 fun finish_session_base () =
    57   Synchronized.change global_session_base
    58     (fn {global_theories, loaded_theories, ...} =>
    59       {global_theories = global_theories,
    60        loaded_theories = loaded_theories,
    61        known_theories = #known_theories empty_session_base});
    62 
    63 fun get_session_base f = f (Synchronized.value global_session_base);
    64 
    65 fun global_theory a = Symtab.lookup (get_session_base #global_theories) a;
    66 fun loaded_theory a = Symtab.lookup (get_session_base #loaded_theories) a;
    67 fun known_theory a = Symtab.lookup (get_session_base #known_theories) a;
    68 
    69 
    70 (* manage source files *)
    71 
    72 type files =
    73  {master_dir: Path.T,  (*master directory of theory source*)
    74   imports: (string * Position.T) list,  (*source specification of imports*)
    75   provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
    76 
    77 fun make_files (master_dir, imports, provided): files =
    78  {master_dir = master_dir, imports = imports, provided = provided};
    79 
    80 structure Files = Theory_Data
    81 (
    82   type T = files;
    83   val empty = make_files (Path.current, [], []);
    84   fun extend _ = empty;
    85   fun merge _ = empty;
    86 );
    87 
    88 fun map_files f =
    89   Files.map (fn {master_dir, imports, provided} =>
    90     make_files (f (master_dir, imports, provided)));
    91 
    92 
    93 val master_directory = #master_dir o Files.get;
    94 val imports_of = #imports o Files.get;
    95 
    96 fun begin_theory master_dir {name, imports, keywords} parents =
    97   Theory.begin_theory name parents
    98   |> map_files (fn _ => (master_dir, imports, []))
    99   |> Thy_Header.add_keywords keywords;
   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       let val theory =
   116         if Long_Name.is_qualified theory0 orelse is_some (global_theory theory0)
   117         then theory0
   118         else Long_Name.qualify qualifier theory0
   119       in (false, theory) end);
   120 
   121 fun import_name qualifier dir s =
   122   (case theory_name qualifier (Thy_Header.import_name s) of
   123     (true, theory) =>
   124       {node_name = Path.basic theory, master_dir = Path.current, theory_name = theory}
   125   | (false, theory) =>
   126       let val node_name =
   127         (case known_theory theory of
   128           SOME node_name => node_name
   129         | NONE => File.full_path dir (thy_path (Path.expand (Path.explode s))))
   130       in {node_name = node_name, master_dir = Path.dir node_name, theory_name = theory} end);
   131 
   132 fun check_file dir file = File.check_file (File.full_path dir file);
   133 
   134 fun check_thy dir thy_name =
   135   let
   136     val thy_base_name = Long_Name.base_name thy_name;
   137     val master_file =
   138       (case known_theory thy_name of
   139         SOME known_path => check_file Path.current known_path
   140       | NONE => check_file dir (thy_path (Path.basic thy_base_name)));
   141     val text = File.read master_file;
   142 
   143     val {name = (name, pos), imports, keywords} =
   144       Thy_Header.read (Path.position master_file) text;
   145     val _ =
   146       thy_base_name <> name andalso
   147         error ("Bad theory name " ^ quote name ^
   148           " for file " ^ Path.print (Path.base master_file) ^ Position.here pos);
   149   in
   150    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
   151     imports = imports, keywords = keywords}
   152   end;
   153 
   154 
   155 (* load files *)
   156 
   157 fun parse_files cmd =
   158   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
   159     (case Token.get_files tok of
   160       [] =>
   161         let
   162           val keywords = Thy_Header.get_keywords thy;
   163           val master_dir = master_directory thy;
   164           val pos = Token.pos_of tok;
   165           val src_paths = Keyword.command_files keywords cmd (Path.explode name);
   166         in map (Command.read_file master_dir pos) src_paths end
   167     | files => map Exn.release files));
   168 
   169 fun provide (src_path, id) =
   170   map_files (fn (master_dir, imports, provided) =>
   171     if AList.defined (op =) provided src_path then
   172       error ("Duplicate use of source file: " ^ Path.print src_path)
   173     else (master_dir, imports, (src_path, id) :: provided));
   174 
   175 fun provide_parse_files cmd =
   176   parse_files cmd >> (fn files => fn thy =>
   177     let
   178       val fs = files thy;
   179       val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
   180     in (fs, thy') end);
   181 
   182 fun load_file thy src_path =
   183   let
   184     val full_path = check_file (master_directory thy) src_path;
   185     val text = File.read full_path;
   186     val id = SHA1.digest text;
   187   in ((full_path, id), text) end;
   188 
   189 fun loaded_files_current thy =
   190   #provided (Files.get thy) |>
   191     forall (fn (src_path, id) =>
   192       (case try (load_file thy) src_path of
   193         NONE => false
   194       | SOME ((_, id'), _) => id = id'));
   195 
   196 
   197 (* antiquotations *)
   198 
   199 local
   200 
   201 fun err msg pos = error (msg ^ Position.here pos);
   202 
   203 fun check_path check_file ctxt dir (name, pos) =
   204   let
   205     val _ = Context_Position.report ctxt pos Markup.language_path;
   206 
   207     val path = Path.append dir (Path.explode name) handle ERROR msg => err msg pos;
   208     val _ = Path.expand path handle ERROR msg => err msg pos;
   209     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   210     val _ =
   211       (case check_file of
   212         NONE => path
   213       | SOME check => (check path handle ERROR msg => err msg pos));
   214   in path end;
   215 
   216 fun document_antiq check_file ctxt (name, pos) =
   217   let
   218     val dir = master_directory (Proof_Context.theory_of ctxt);
   219     val _ = check_path check_file ctxt dir (name, pos);
   220   in
   221     space_explode "/" name
   222     |> map Latex.output_ascii
   223     |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
   224     |> enclose "\\isatt{" "}"
   225   end;
   226 
   227 fun ML_antiq check_file ctxt (name, pos) =
   228   let val path = check_path check_file ctxt Path.current (name, pos);
   229   in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
   230 
   231 in
   232 
   233 val _ = Theory.setup
   234  (Thy_Output.antiquotation @{binding path} (Scan.lift (Parse.position Parse.path))
   235     (document_antiq NONE o #context) #>
   236   Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
   237     (document_antiq (SOME File.check_file) o #context) #>
   238   Thy_Output.antiquotation @{binding dir} (Scan.lift (Parse.position Parse.path))
   239     (document_antiq (SOME File.check_dir) o #context) #>
   240   ML_Antiquotation.value @{binding path}
   241     (Args.context -- Scan.lift (Parse.position Parse.path)
   242       >> uncurry (ML_antiq NONE)) #>
   243   ML_Antiquotation.value @{binding file}
   244     (Args.context -- Scan.lift (Parse.position Parse.path)
   245       >> uncurry (ML_antiq (SOME File.check_file))) #>
   246   ML_Antiquotation.value @{binding dir}
   247     (Args.context -- Scan.lift (Parse.position Parse.path)
   248       >> uncurry (ML_antiq (SOME File.check_dir))));
   249 
   250 end;
   251 
   252 end;