src/Pure/Thy/thy_load.ML
author wenzelm
Wed Aug 22 21:02:02 2012 +0200 (2012-08-22 ago)
changeset 48886 9604c6563226
parent 48881 46e053eda5dd
child 48887 c49eca45cbb0
permissions -rw-r--r--
discontinued separate list of required files -- maintain only provided files as they occur at runtime;
tuned signature;
     1 (*  Title:      Pure/Thy/thy_load.ML
     2     Author:     Makarius
     3 
     4 Loading files that contribute to a theory.  Global master path.
     5 *)
     6 
     7 signature THY_LOAD =
     8 sig
     9   val master_directory: theory -> Path.T
    10   val imports_of: theory -> string list
    11   val provide: Path.T * (Path.T * SHA1.digest) -> theory -> theory
    12   val check_file: Path.T -> Path.T -> Path.T
    13   val thy_path: Path.T -> Path.T
    14   val check_thy: Thy_Header.keywords -> Path.T -> string ->
    15    {master: Path.T * SHA1.digest, text: string, imports: string list,
    16     uses: (Path.T * bool) list, keywords: Thy_Header.keywords}
    17   val load_file: theory -> Path.T -> (Path.T * SHA1.digest) * string
    18   val use_file: Path.T -> theory -> string * theory
    19   val loaded_files: theory -> Path.T list
    20   val load_current: theory -> bool
    21   val use_ml: Path.T -> unit
    22   val exec_ml: Path.T -> generic_theory -> generic_theory
    23   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    24   val load_thy: int -> Path.T -> Thy_Header.header -> Position.T -> string ->
    25     theory list -> theory * unit future
    26   val resolve_files: Path.T -> Thy_Syntax.span -> Thy_Syntax.span
    27   val parse_files: string -> (theory -> Token.files) parser
    28   val set_master_path: Path.T -> unit
    29   val get_master_path: unit -> Path.T
    30 end;
    31 
    32 structure Thy_Load: THY_LOAD =
    33 struct
    34 
    35 (* manage source files *)
    36 
    37 type files =
    38  {master_dir: Path.T,  (*master directory of theory source*)
    39   imports: string list,  (*source specification of imports*)
    40   provided: (Path.T * (Path.T * SHA1.digest)) list};  (*source path, physical path, digest*)
    41 
    42 fun make_files (master_dir, imports, provided): files =
    43  {master_dir = master_dir, imports = imports, provided = provided};
    44 
    45 structure Files = Theory_Data
    46 (
    47   type T = files;
    48   val empty = make_files (Path.current, [], []);
    49   fun extend _ = empty;
    50   fun merge _ = empty;
    51 );
    52 
    53 fun map_files f =
    54   Files.map (fn {master_dir, imports, provided} =>
    55     make_files (f (master_dir, imports, provided)));
    56 
    57 
    58 val master_directory = #master_dir o Files.get;
    59 val imports_of = #imports o Files.get;
    60 
    61 fun put_deps dir imports = map_files (fn _ => (dir, imports, []));
    62 
    63 fun provide (src_path, path_id) =
    64   map_files (fn (master_dir, imports, provided) =>
    65     if AList.defined (op =) provided src_path then
    66       error ("Duplicate use of source file: " ^ Path.print src_path)
    67     else (master_dir, imports, (src_path, path_id) :: provided));
    68 
    69 
    70 (* inlined files *)
    71 
    72 fun check_file dir file = File.check_file (File.full_path dir file);
    73 
    74 local
    75 
    76 fun clean ((i1, t1) :: (i2, t2) :: toks) =
    77       if Token.keyword_with (fn s => s = "%" orelse s = "--") t1 then clean toks
    78       else (i1, t1) :: clean ((i2, t2) :: toks)
    79   | clean toks = toks;
    80 
    81 fun clean_tokens toks =
    82   ((0 upto length toks - 1) ~~ toks)
    83   |> filter (fn (_, tok) => Token.is_proper tok)
    84   |> clean;
    85 
    86 fun find_file toks =
    87   rev (clean_tokens toks) |> get_first (fn (i, tok) =>
    88     if Token.is_name tok then
    89       SOME (i, Path.explode (Token.content_of tok))
    90         handle ERROR msg => error (msg ^ Token.pos_of tok)
    91     else NONE);
    92 
    93 fun command_files path exts =
    94   if null exts then [path]
    95   else map (fn ext => Path.ext ext path) exts;
    96 
    97 in
    98 
    99 fun find_files syntax text =
   100   let val thy_load_commands = Keyword.thy_load_commands syntax in
   101     if exists (fn (cmd, _) => String.isSubstring cmd text) thy_load_commands then
   102       Thy_Syntax.parse_tokens (Keyword.lexicons_of syntax) Position.none text
   103       |> Thy_Syntax.parse_spans
   104       |> maps
   105         (fn Thy_Syntax.Span (Thy_Syntax.Command (cmd, _), toks) =>
   106               (case AList.lookup (op =) thy_load_commands cmd of
   107                 SOME exts =>
   108                   (case find_file toks of
   109                     SOME (_, path) => command_files path exts
   110                   | NONE => [])
   111               | NONE => [])
   112           | _ => [])
   113     else []
   114   end;
   115 
   116 fun read_files cmd dir path =
   117   let
   118     val files =
   119       command_files path (Keyword.command_files cmd)
   120       |> map (check_file dir #> (fn p => (File.read p, Path.position p)));
   121   in (dir, files) end;
   122 
   123 fun parse_files cmd =
   124   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
   125     (case Token.get_files tok of
   126       SOME files => files
   127     | NONE =>
   128         let
   129           val path = Path.explode name;
   130           val _ = warning ("Dynamic loading of files: " ^ Path.print path);
   131         in read_files cmd (master_directory thy) path end));
   132 
   133 fun resolve_files dir span =
   134   (case span of
   135     Thy_Syntax.Span (Thy_Syntax.Command (cmd, pos), toks) =>
   136       if Keyword.is_theory_load cmd then
   137         (case find_file toks of
   138           NONE => error ("Bad file argument of command " ^ quote cmd ^ Position.str_of pos)
   139         | SOME (i, path) =>
   140             let
   141               val toks' = toks |> map_index (fn (j, tok) =>
   142                 if i = j then Token.put_files (read_files cmd dir path) tok
   143                 else tok);
   144             in Thy_Syntax.Span (Thy_Syntax.Command (cmd, pos), toks') end)
   145       else span
   146   | span => span);
   147 
   148 end;
   149 
   150 
   151 (* check files *)
   152 
   153 val thy_path = Path.ext "thy";
   154 
   155 fun check_thy base_keywords dir thy_name =
   156   let
   157     val path = thy_path (Path.basic thy_name);
   158     val master_file = check_file dir path;
   159     val text = File.read master_file;
   160 
   161     val {name, imports, uses, keywords} = Thy_Header.read (Path.position master_file) text;
   162     val _ = thy_name <> name andalso
   163       error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name);
   164 
   165     val syntax =
   166       fold (Keyword.define_keywords o apsnd (Option.map Keyword.spec))
   167         (keywords @ base_keywords) (Keyword.get_keywords ());
   168     val more_uses = map (rpair false) (find_files syntax text);
   169   in
   170    {master = (master_file, SHA1.digest text), text = text,
   171     imports = imports, uses = uses @ more_uses, keywords = keywords}
   172   end;
   173 
   174 
   175 (* load files *)
   176 
   177 fun load_file thy src_path =
   178   let
   179     val full_path = check_file (master_directory thy) src_path;
   180     val text = File.read full_path;
   181     val id = SHA1.digest text;
   182   in ((full_path, id), text) end;
   183 
   184 fun use_file src_path thy =
   185   let
   186     val (path_id, text) = load_file thy src_path;
   187     val thy' = provide (src_path, path_id) thy;
   188   in (text, thy') end;
   189 
   190 val loaded_files = map (#1 o #2) o #provided o Files.get;
   191 
   192 fun load_current thy =
   193   #provided (Files.get thy) |>
   194     forall (fn (src_path, (_, id)) =>
   195       (case try (load_file thy) src_path of
   196         NONE => false
   197       | SOME ((_, id'), _) => id = id'));
   198 
   199 
   200 (* provide files *)
   201 
   202 fun eval_file path text = ML_Context.eval_text true (Path.position path) text;
   203 
   204 fun use_ml src_path =
   205   if is_none (Context.thread_data ()) then
   206     let val path = check_file Path.current src_path
   207     in eval_file path (File.read path) end
   208   else
   209     let
   210       val thy = ML_Context.the_global_context ();
   211 
   212       val ((path, id), text) = load_file thy src_path;
   213       val _ = eval_file path text;
   214       val _ = Context.>> Local_Theory.propagate_ml_env;
   215 
   216       val provide = provide (src_path, (path, id));
   217       val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
   218     in () end;
   219 
   220 fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
   221 
   222 
   223 (* load_thy *)
   224 
   225 fun begin_theory dir {name, imports, keywords, uses} parents =
   226   Theory.begin_theory name parents
   227   |> put_deps dir imports
   228   |> fold Thy_Header.declare_keyword keywords
   229   |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
   230   |> Theory.checkpoint;
   231 
   232 fun excursion init elements =
   233   let
   234     val immediate = not (Goal.future_enabled ());
   235 
   236     fun proof_result (tr, trs) (st, _) =
   237       let
   238         val (result, st') = Toplevel.proof_result immediate (tr, trs) st;
   239         val pos' = Toplevel.pos_of (List.last (tr :: trs));
   240       in (result, (st', pos')) end;
   241 
   242     fun element_result elem x =
   243       fold_map proof_result
   244         (Outer_Syntax.read_element (#2 (Outer_Syntax.get_syntax ())) init elem) x;
   245 
   246     val (results, (end_state, end_pos)) =
   247       fold_map element_result elements (Toplevel.toplevel, Position.none);
   248 
   249     val thy = Toplevel.end_theory end_pos end_state;
   250   in (flat results, thy) end;
   251 
   252 fun load_thy update_time dir header pos text parents =
   253   let
   254     val time = ! Toplevel.timing;
   255 
   256     val {name, uses, ...} = header;
   257     val _ = Thy_Header.define_keywords header;
   258     val _ = Present.init_theory name;
   259     fun init () =
   260       begin_theory dir header parents
   261       |> Present.begin_theory update_time dir uses;
   262 
   263     val lexs = Keyword.get_lexicons ();
   264 
   265     val toks = Thy_Syntax.parse_tokens lexs pos text;
   266     val spans = map (resolve_files dir) (Thy_Syntax.parse_spans toks);
   267     val elements = Thy_Syntax.parse_elements spans;
   268 
   269     val _ = Present.theory_source name
   270       (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
   271 
   272     val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
   273     val (results, thy) = cond_timeit time "" (fn () => excursion init elements);
   274     val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
   275 
   276     val present =
   277       singleton (Future.cond_forks {name = "Outer_Syntax.present:" ^ name, group = NONE,
   278         deps = map Future.task_of results, pri = 0, interrupts = true})
   279       (fn () =>
   280         let val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax () in
   281           Thy_Output.present_thy minor Keyword.command_tags
   282             (Outer_Syntax.is_markup outer_syntax)
   283             (maps Future.join results) toks
   284           |> Buffer.content
   285           |> Present.theory_output name
   286         end);
   287 
   288   in (thy, present) end;
   289 
   290 
   291 (* global master path *)
   292 
   293 local
   294   val master_path = Unsynchronized.ref Path.current;
   295 in
   296 
   297 fun set_master_path path = master_path := path;
   298 fun get_master_path () = ! master_path;
   299 
   300 end;
   301 
   302 end;