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