src/Pure/Thy/thy_info.ML
changeset 72638 2a7fc87495e0
parent 72624 35524fade6a4
child 72651 52cb065aa916
equal deleted inserted replaced
72637:fd68c9c1b90b 72638:2a7fc87495e0
    16   val get_names: unit -> string list
    16   val get_names: unit -> string list
    17   val lookup_theory: string -> theory option
    17   val lookup_theory: string -> theory option
    18   val get_theory: string -> theory
    18   val get_theory: string -> theory
    19   val master_directory: string -> Path.T
    19   val master_directory: string -> Path.T
    20   val remove_thy: string -> unit
    20   val remove_thy: string -> unit
    21   type context = {options: Options.T, last_timing: Toplevel.transition -> Time.time}
    21   val use_theories: Options.T -> string -> (string * Position.T) list -> unit
    22   val use_theories: context -> string -> (string * Position.T) list -> unit
       
    23   val use_thy: string -> unit
    22   val use_thy: string -> unit
    24   val script_thy: Position.T -> string -> theory -> theory
    23   val script_thy: Position.T -> string -> theory -> theory
    25   val register_thy: theory -> unit
    24   val register_thy: theory -> unit
    26   val finish: unit -> unit
    25   val finish: unit -> unit
    27 end;
    26 end;
   176     val thys' = remove name thys;
   175     val thys' = remove name thys;
   177     val _ = map (get thys') parents;
   176     val _ = map (get thys') parents;
   178   in new_entry name parents (SOME deps, SOME theory) thys' end;
   177   in new_entry name parents (SOME deps, SOME theory) thys' end;
   179 
   178 
   180 fun update_thy deps theory = change_thys (update deps theory);
   179 fun update_thy deps theory = change_thys (update deps theory);
   181 
       
   182 
       
   183 (* context *)
       
   184 
       
   185 type context = {options: Options.T, last_timing: Toplevel.transition -> Time.time};
       
   186 
       
   187 fun default_context (): context =
       
   188   {options = Options.default (), last_timing = K Time.zeroTime};
       
   189 
   180 
   190 
   181 
   191 (* scheduling loader tasks *)
   182 (* scheduling loader tasks *)
   192 
   183 
   193 datatype result =
   184 datatype result =
   270   in () end);
   261   in () end);
   271 
   262 
   272 
   263 
   273 (* eval theory *)
   264 (* eval theory *)
   274 
   265 
   275 fun excursion keywords master_dir last_timing init elements =
   266 fun excursion keywords master_dir init elements =
   276   let
   267   let
   277     fun prepare_span st span =
   268     fun prepare_span st span =
   278       Command_Span.content span
   269       Command_Span.content span
   279       |> Command.read keywords (Command.read_thy st) master_dir init ([], ~1)
   270       |> Command.read keywords (Command.read_thy st) master_dir init ([], ~1)
   280       |> (fn tr => Toplevel.timing (last_timing tr) tr);
   271       |> (fn tr => Toplevel.timing (Resources.last_timing tr) tr);
   281 
   272 
   282     fun element_result span_elem (st, _) =
   273     fun element_result span_elem (st, _) =
   283       let
   274       let
   284         val elem = Thy_Element.map_element (prepare_span st) span_elem;
   275         val elem = Thy_Element.map_element (prepare_span st) span_elem;
   285         val (results, st') = Toplevel.element_result keywords elem st;
   276         val (results, st') = Toplevel.element_result keywords elem st;
   290       fold_map element_result elements (Toplevel.init_toplevel (), Position.none);
   281       fold_map element_result elements (Toplevel.init_toplevel (), Position.none);
   291 
   282 
   292     val thy = Toplevel.end_theory end_pos end_state;
   283     val thy = Toplevel.end_theory end_pos end_state;
   293   in (results, thy) end;
   284   in (results, thy) end;
   294 
   285 
   295 fun eval_thy (context: context) update_time master_dir header text_pos text parents =
   286 fun eval_thy options update_time master_dir header text_pos text parents =
   296   let
   287   let
   297     val {options, last_timing} = context;
       
   298     val (name, _) = #name header;
   288     val (name, _) = #name header;
   299     val keywords =
   289     val keywords =
   300       fold (curry Keyword.merge_keywords o Thy_Header.get_keywords) parents
   290       fold (curry Keyword.merge_keywords o Thy_Header.get_keywords) parents
   301         (Keyword.add_keywords (#keywords header) Keyword.empty_keywords);
   291         (Keyword.add_keywords (#keywords header) Keyword.empty_keywords);
   302 
   292 
   304     val elements = Thy_Element.parse_elements keywords spans;
   294     val elements = Thy_Element.parse_elements keywords spans;
   305 
   295 
   306     fun init () = Resources.begin_theory master_dir header parents;
   296     fun init () = Resources.begin_theory master_dir header parents;
   307     val (results, thy) =
   297     val (results, thy) =
   308       cond_timeit true ("theory " ^ quote name)
   298       cond_timeit true ("theory " ^ quote name)
   309         (fn () => excursion keywords master_dir last_timing init elements);
   299         (fn () => excursion keywords master_dir init elements);
   310 
   300 
   311     fun present () =
   301     fun present () =
   312       let
   302       let
   313         val segments = (spans ~~ maps Toplevel.join_results results)
   303         val segments = (spans ~~ maps Toplevel.join_results results)
   314           |> map (fn (span, (tr, st')) => {span = span, command = tr, state = st'});
   304           |> map (fn (span, (tr, st')) => {span = span, command = tr, state = st'});
   323 local
   313 local
   324 
   314 
   325 fun required_by _ [] = ""
   315 fun required_by _ [] = ""
   326   | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
   316   | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
   327 
   317 
   328 fun load_thy context initiators update_time deps text (name, pos) keywords parents =
   318 fun load_thy options initiators update_time deps text (name, pos) keywords parents =
   329   let
   319   let
   330     val _ = remove_thy name;
   320     val _ = remove_thy name;
   331     val _ = writeln ("Loading theory " ^ quote name ^ required_by " " initiators);
   321     val _ = writeln ("Loading theory " ^ quote name ^ required_by " " initiators);
   332     val _ = Output.try_protocol_message (Markup.loading_theory name) [];
   322     val _ = Output.try_protocol_message (Markup.loading_theory name) [];
   333 
   323 
   346 
   336 
   347     val timing_start = Timing.start ();
   337     val timing_start = Timing.start ();
   348 
   338 
   349     val text_pos = Position.put_id (Document_ID.print exec_id) (Path.position thy_path);
   339     val text_pos = Position.put_id (Document_ID.print exec_id) (Path.position thy_path);
   350     val (theory, present, weight) =
   340     val (theory, present, weight) =
   351       eval_thy context update_time dir header text_pos text
   341       eval_thy options update_time dir header text_pos text
   352         (if name = Context.PureN then [Context.the_global_context ()] else parents);
   342         (if name = Context.PureN then [Context.the_global_context ()] else parents);
   353 
   343 
   354     val timing_result = Timing.result timing_start;
   344     val timing_result = Timing.result timing_start;
   355     val timing_props = [Markup.theory_timing, (Markup.nameN, name)];
   345     val timing_props = [Markup.theory_timing, (Markup.nameN, name)];
   356     val _  = Output.try_protocol_message (timing_props @ Markup.timing_properties timing_result) []
   346     val _  = Output.try_protocol_message (timing_props @ Markup.timing_properties timing_result) []
   378             | SOME theory => Resources.loaded_files_current theory);
   368             | SOME theory => Resources.loaded_files_current theory);
   379       in (current, deps', theory_pos', imports', keywords') end);
   369       in (current, deps', theory_pos', imports', keywords') end);
   380 
   370 
   381 in
   371 in
   382 
   372 
   383 fun require_thys context initiators qualifier dir strs tasks =
   373 fun require_thys options initiators qualifier dir strs tasks =
   384       fold_map (require_thy context initiators qualifier dir) strs tasks |>> forall I
   374       fold_map (require_thy options initiators qualifier dir) strs tasks |>> forall I
   385 and require_thy context initiators qualifier dir (s, require_pos) tasks =
   375 and require_thy options initiators qualifier dir (s, require_pos) tasks =
   386   let
   376   let
   387     val {master_dir, theory_name, ...} = Resources.import_name qualifier dir s;
   377     val {master_dir, theory_name, ...} = Resources.import_name qualifier dir s;
   388   in
   378   in
   389     (case try (String_Graph.get_node tasks) theory_name of
   379     (case try (String_Graph.get_node tasks) theory_name of
   390       SOME task => (task_finished task, tasks)
   380       SOME task => (task_finished task, tasks)
   401           val qualifier' = Resources.theory_qualifier theory_name;
   391           val qualifier' = Resources.theory_qualifier theory_name;
   402           val dir' = dir + master_dir_deps (Option.map #1 deps);
   392           val dir' = dir + master_dir_deps (Option.map #1 deps);
   403 
   393 
   404           val parents = map (#theory_name o Resources.import_name qualifier' dir' o #1) imports;
   394           val parents = map (#theory_name o Resources.import_name qualifier' dir' o #1) imports;
   405           val (parents_current, tasks') =
   395           val (parents_current, tasks') =
   406             require_thys context (theory_name :: initiators) qualifier' dir' imports tasks;
   396             require_thys options (theory_name :: initiators) qualifier' dir' imports tasks;
   407 
   397 
   408           val all_current = current andalso parents_current;
   398           val all_current = current andalso parents_current;
   409           val task =
   399           val task =
   410             if all_current then Finished (get_theory theory_name)
   400             if all_current then Finished (get_theory theory_name)
   411             else
   401             else
   413                 NONE => raise Fail "Malformed deps"
   403                 NONE => raise Fail "Malformed deps"
   414               | SOME (dep, text) =>
   404               | SOME (dep, text) =>
   415                   let
   405                   let
   416                     val update_time = serial ();
   406                     val update_time = serial ();
   417                     val load =
   407                     val load =
   418                       load_thy context initiators update_time
   408                       load_thy options initiators update_time
   419                         dep text (theory_name, theory_pos) keywords;
   409                         dep text (theory_name, theory_pos) keywords;
   420                   in Task (parents, load) end);
   410                   in Task (parents, load) end);
   421 
   411 
   422           val tasks'' = new_entry theory_name parents task tasks';
   412           val tasks'' = new_entry theory_name parents task tasks';
   423         in (all_current, tasks'') end)
   413         in (all_current, tasks'') end)
   426 end;
   416 end;
   427 
   417 
   428 
   418 
   429 (* use theories *)
   419 (* use theories *)
   430 
   420 
   431 fun use_theories context qualifier imports =
   421 fun use_theories options qualifier imports =
   432   let val (_, tasks) = require_thys context [] qualifier Path.current imports String_Graph.empty
   422   let val (_, tasks) = require_thys options [] qualifier Path.current imports String_Graph.empty
   433   in if Multithreading.max_threads () > 1 then schedule_futures tasks else schedule_seq tasks end;
   423   in if Multithreading.max_threads () > 1 then schedule_futures tasks else schedule_seq tasks end;
   434 
   424 
   435 fun use_thy name =
   425 fun use_thy name =
   436   use_theories (default_context ()) Resources.default_qualifier [(name, Position.none)];
   426   use_theories (Options.default ()) Resources.default_qualifier [(name, Position.none)];
   437 
   427 
   438 
   428 
   439 (* toplevel scripting -- without maintaining database *)
   429 (* toplevel scripting -- without maintaining database *)
   440 
   430 
   441 fun script_thy pos txt thy =
   431 fun script_thy pos txt thy =