| author | haftmann | 
| Wed, 02 Apr 2008 15:58:37 +0200 | |
| changeset 26515 | 4a2063a8c2d2 | 
| parent 26494 | 6816ca8b48ef | 
| child 26614 | 1f09a22a1027 | 
| permissions | -rw-r--r-- | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 1 | (* Title: Pure/Thy/thy_info.ML | 
| 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 2 | ID: $Id$ | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 3 | Author: Markus Wenzel, TU Muenchen | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 4 | |
| 15801 | 5 | Main part of theory loader database, including handling of theory and | 
| 6 | file dependencies. | |
| 3976 | 7 | *) | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 8 | |
| 5209 | 9 | signature BASIC_THY_INFO = | 
| 10 | sig | |
| 11 | val theory: string -> theory | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 12 | val touch_thy: string -> unit | 
| 6666 | 13 | val remove_thy: string -> unit | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 14 | end; | 
| 5209 | 15 | |
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 16 | signature THY_INFO = | 
| 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 17 | sig | 
| 5209 | 18 | include BASIC_THY_INFO | 
| 7099 | 19 | datatype action = Update | Outdate | Remove | 
| 20 | val str_of_action: action -> string | |
| 21 | val add_hook: (action -> string -> unit) -> unit | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 22 | val names: unit -> string list | 
| 7910 | 23 | val known_thy: string -> bool | 
| 7935 | 24 | val check_known_thy: string -> bool | 
| 25 | val if_known_thy: (string -> unit) -> string -> unit | |
| 7288 | 26 | val lookup_theory: string -> theory option | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 27 | val get_theory: string -> theory | 
| 19547 | 28 | val the_theory: string -> theory -> theory | 
| 24563 | 29 | val is_finished: string -> bool | 
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 30 | val loaded_files: string -> Path.T list | 
| 23871 | 31 | val get_parents: string -> string list | 
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 32 | val pretty_theory: theory -> Pretty.T | 
| 7910 | 33 | val touch_child_thys: string -> unit | 
| 25013 | 34 | val provide_file: Path.T -> string -> unit | 
| 7941 | 35 | val load_file: bool -> Path.T -> unit | 
| 26494 | 36 | val exec_file: bool -> Path.T -> Context.generic -> Context.generic | 
| 6241 | 37 | val use: string -> unit | 
| 24057 
f42665561801
removed obsolete Output.ML_errors/toplevel_errors;
 wenzelm parents: 
23982diff
changeset | 38 | val time_use: string -> unit | 
| 
f42665561801
removed obsolete Output.ML_errors/toplevel_errors;
 wenzelm parents: 
23982diff
changeset | 39 | val use_thys: string list -> unit | 
| 
f42665561801
removed obsolete Output.ML_errors/toplevel_errors;
 wenzelm parents: 
23982diff
changeset | 40 | val use_thy: string -> unit | 
| 
f42665561801
removed obsolete Output.ML_errors/toplevel_errors;
 wenzelm parents: 
23982diff
changeset | 41 | val time_use_thy: string -> unit | 
| 24563 | 42 | val thy_ord: theory * theory -> order | 
| 23900 
b25b1444a246
begin_theory: simplified interface, keep thy info empty until end_theory;
 wenzelm parents: 
23893diff
changeset | 43 | val begin_theory: string -> string list -> (Path.T * bool) list -> bool -> theory | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 44 | val end_theory: theory -> theory | 
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 45 | val register_thy: string -> unit | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 46 | val register_theory: theory -> unit | 
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 47 | val finish: unit -> unit | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 48 | end; | 
| 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 49 | |
| 6362 | 50 | structure ThyInfo: THY_INFO = | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 51 | struct | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 52 | |
| 7099 | 53 | (** theory loader actions and hooks **) | 
| 54 | ||
| 55 | datatype action = Update | Outdate | Remove; | |
| 56 | val str_of_action = fn Update => "Update" | Outdate => "Outdate" | Remove => "Remove"; | |
| 57 | ||
| 58 | local | |
| 59 | val hooks = ref ([]: (action -> string -> unit) list); | |
| 60 | in | |
| 23939 | 61 | fun add_hook f = CRITICAL (fn () => change hooks (cons f)); | 
| 15570 | 62 | fun perform action name = List.app (fn f => (try (fn () => f action name) (); ())) (! hooks); | 
| 7099 | 63 | end; | 
| 64 | ||
| 65 | ||
| 66 | ||
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 67 | (** thy database **) | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 68 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 69 | (* messages *) | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 70 | |
| 23871 | 71 | fun loader_msg txt [] = "Theory loader: " ^ txt | 
| 72 | | loader_msg txt names = "Theory loader: " ^ txt ^ " " ^ commas_quote names; | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 73 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 74 | val show_path = space_implode " via " o map quote; | 
| 9332 | 75 | fun cycle_msg names = loader_msg ("cyclic dependency of " ^ show_path names) [];
 | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 76 | |
| 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 77 | |
| 6666 | 78 | (* derived graph operations *) | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 79 | |
| 9327 | 80 | fun add_deps name parents G = Graph.add_deps_acyclic (name, parents) G | 
| 9332 | 81 | handle Graph.CYCLES namess => error (cat_lines (map cycle_msg namess)); | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 82 | |
| 7952 | 83 | fun upd_deps name entry G = | 
| 19473 | 84 | fold (fn parent => Graph.del_edge (parent, name)) (Graph.imm_preds G name) G | 
| 7952 | 85 | |> Graph.map_node name (K entry); | 
| 3976 | 86 | |
| 23967 | 87 | fun new_deps name parents entry G = | 
| 7952 | 88 | (if can (Graph.get_node G) name then upd_deps name entry G else Graph.new_node (name, entry) G) | 
| 89 | |> add_deps name parents; | |
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 90 | |
| 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 91 | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 92 | (* thy database *) | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 93 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 94 | type deps = | 
| 24190 | 95 |   {update_time: int,                      (*symbolic time of update; negative value means outdated*)
 | 
| 96 | master: (Path.T * File.ident) option, (*master dependencies for thy file*) | |
| 97 | text: string list, (*source text for thy*) | |
| 98 | parents: string list, (*source specification of parents (partially qualified)*) | |
| 99 | (*auxiliary files: source path, physical path + identifier*) | |
| 100 | files: (Path.T * (Path.T * File.ident) option) list}; | |
| 23886 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 101 | |
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 102 | fun make_deps update_time master text parents files : deps = | 
| 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 103 |   {update_time = update_time, master = master, text = text, parents = parents, files = files};
 | 
| 23886 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 104 | |
| 24068 | 105 | fun init_deps master text parents files = | 
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 106 | SOME (make_deps ~1 master text parents (map (rpair NONE) files)); | 
| 23871 | 107 | |
| 24190 | 108 | fun master_dir NONE = Path.current | 
| 109 | | master_dir (SOME (path, _)) = Path.dir path; | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 110 | |
| 23886 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 111 | fun master_dir' (d: deps option) = the_default Path.current (Option.map (master_dir o #master) d); | 
| 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 112 | fun master_dir'' d = the_default Path.current (Option.map master_dir' d); | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 113 | |
| 23967 | 114 | fun base_name s = Path.implode (Path.base (Path.explode s)); | 
| 115 | ||
| 7211 | 116 | |
| 6362 | 117 | type thy = deps option * theory option; | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 118 | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 119 | local | 
| 6362 | 120 | val database = ref (Graph.empty: thy Graph.T); | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 121 | in | 
| 6362 | 122 | fun get_thys () = ! database; | 
| 23939 | 123 | fun change_thys f = CRITICAL (fn () => Library.change database f); | 
| 3604 
6bf9f09f3d61
Moved functions for theory information storage / retrieval
 berghofe parents: diff
changeset | 124 | end; | 
| 5209 | 125 | |
| 126 | ||
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 127 | (* access thy graph *) | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 128 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 129 | fun thy_graph f x = f (get_thys ()) x; | 
| 9417 | 130 | |
| 23967 | 131 | fun get_names () = Graph.topological_order (get_thys ()); | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 132 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 133 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 134 | (* access thy *) | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 135 | |
| 7935 | 136 | fun lookup_thy name = | 
| 15531 | 137 | SOME (thy_graph Graph.get_node name) handle Graph.UNDEF _ => NONE; | 
| 7935 | 138 | |
| 16047 | 139 | val known_thy = is_some o lookup_thy; | 
| 7935 | 140 | fun check_known_thy name = known_thy name orelse (warning ("Unknown theory " ^ quote name); false);
 | 
| 141 | fun if_known_thy f name = if check_known_thy name then f name else (); | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 142 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 143 | fun get_thy name = | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 144 | (case lookup_thy name of | 
| 15531 | 145 | SOME thy => thy | 
| 146 | | NONE => error (loader_msg "nothing known about theory" [name])); | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 147 | |
| 23939 | 148 | fun change_thy name f = CRITICAL (fn () => | 
| 149 | (get_thy name; change_thys (Graph.map_node name f))); | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 150 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 151 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 152 | (* access deps *) | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 153 | |
| 15570 | 154 | val lookup_deps = Option.map #1 o lookup_thy; | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 155 | val get_deps = #1 o get_thy; | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 156 | fun change_deps name f = change_thy name (fn (deps, x) => (f deps, x)); | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 157 | |
| 6666 | 158 | fun is_finished name = is_none (get_deps name); | 
| 7191 | 159 | |
| 160 | fun loaded_files name = | |
| 161 | (case get_deps name of | |
| 15531 | 162 | NONE => [] | 
| 163 |   | SOME {master, files, ...} =>
 | |
| 24190 | 164 | (case master of SOME (thy_path, _) => [thy_path] | NONE => []) @ | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19473diff
changeset | 165 | (map_filter (Option.map #1 o #2) files)); | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 166 | |
| 23871 | 167 | fun get_parents name = | 
| 23967 | 168 | thy_graph Graph.imm_preds name handle Graph.UNDEF _ => | 
| 6654 | 169 | error (loader_msg "nothing known about theory" [name]); | 
| 170 | ||
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 171 | |
| 24563 | 172 | (* pretty_theory *) | 
| 16047 | 173 | |
| 174 | local | |
| 15633 | 175 | |
| 16047 | 176 | fun relevant_names names = | 
| 177 | let | |
| 178 | val (finished, unfinished) = | |
| 19305 | 179 | List.filter (fn name => name = Context.draftN orelse known_thy name) names | 
| 180 | |> List.partition (fn name => name <> Context.draftN andalso is_finished name); | |
| 16047 | 181 | in (if not (null finished) then [List.last finished] else []) @ unfinished end; | 
| 15633 | 182 | |
| 16047 | 183 | in | 
| 15633 | 184 | |
| 16454 | 185 | fun pretty_theory thy = | 
| 186 |   Pretty.str_list "{" "}" (relevant_names (Context.names_of thy));
 | |
| 15633 | 187 | |
| 16047 | 188 | end; | 
| 189 | ||
| 15633 | 190 | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 191 | (* access theory *) | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 192 | |
| 7687 | 193 | fun lookup_theory name = | 
| 194 | (case lookup_thy name of | |
| 15531 | 195 | SOME (_, SOME thy) => SOME thy | 
| 196 | | _ => NONE); | |
| 7288 | 197 | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 198 | fun get_theory name = | 
| 7288 | 199 | (case lookup_theory name of | 
| 23871 | 200 | SOME theory => theory | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 201 | | _ => error (loader_msg "undefined theory entry for" [name])); | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 202 | |
| 19547 | 203 | fun the_theory name thy = | 
| 204 | if Context.theory_name thy = name then thy | |
| 205 | else get_theory name; | |
| 206 | ||
| 22137 | 207 | val _ = ML_Context.value_antiq "theory" | 
| 208 | (Scan.lift Args.name | |
| 209 | >> (fn name => (name ^ "_thy", "ThyInfo.theory " ^ ML_Syntax.print_string name)) | |
| 26425 
6561665c5cb1
renamed ML_Context.the_context to ML_Context.the_global_context;
 wenzelm parents: 
26415diff
changeset | 210 |   || Scan.succeed ("thy", "ML_Context.the_global_context ()"));
 | 
| 22137 | 211 | |
| 24696 | 212 | val _ = ML_Context.value_antiq "theory_ref" | 
| 213 | (Scan.lift Args.name | |
| 214 | >> (fn name => (name ^ "_thy", | |
| 215 | "Theory.check_thy (ThyInfo.theory " ^ ML_Syntax.print_string name ^ ")")) | |
| 26425 
6561665c5cb1
renamed ML_Context.the_context to ML_Context.the_global_context;
 wenzelm parents: 
26415diff
changeset | 216 |   || Scan.succeed ("thy", "Theory.check_thy (ML_Context.the_global_context ())"));
 | 
| 24696 | 217 | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 218 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 219 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 220 | (** thy operations **) | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 221 | |
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 222 | (* check state *) | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 223 | |
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 224 | fun check_unfinished fail name = | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 225 | if known_thy name andalso is_finished name then | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 226 | fail (loader_msg "cannot update finished theory" [name]) | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 227 | else (); | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 228 | |
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 229 | fun check_files name = | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 230 | let | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 231 |     val files = (case get_deps name of SOME {files, ...} => files | NONE => []);
 | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 232 | val missing_files = map_filter (fn (path, NONE) => SOME (Path.implode path) | _ => NONE) files; | 
| 24190 | 233 | val _ = null missing_files orelse | 
| 234 | error (loader_msg "unresolved dependencies of theory" [name] ^ | |
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 235 | " on file(s): " ^ commas_quote missing_files); | 
| 24190 | 236 | in () end; | 
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 237 | |
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 238 | |
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 239 | (* maintain update_time *) | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 240 | |
| 7099 | 241 | local | 
| 242 | ||
| 6241 | 243 | fun is_outdated name = | 
| 244 | (case lookup_deps name of | |
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 245 |     SOME (SOME {update_time, ...}) => update_time < 0
 | 
| 6241 | 246 | | _ => false); | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 247 | |
| 25013 | 248 | fun unfinished name = | 
| 24186 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 249 | if is_finished name then (warning (loader_msg "tried to touch finished theory" [name]); NONE) | 
| 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 250 | else SOME name; | 
| 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 251 | |
| 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 252 | in | 
| 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 253 | |
| 6241 | 254 | fun outdate_thy name = | 
| 7099 | 255 | if is_finished name orelse is_outdated name then () | 
| 23939 | 256 | else CRITICAL (fn () => | 
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 257 |    (change_deps name (Option.map (fn {master, text, parents, files, ...} =>
 | 
| 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 258 | make_deps ~1 master text parents files)); perform Outdate name)); | 
| 7099 | 259 | |
| 7910 | 260 | fun touch_thys names = | 
| 25013 | 261 | List.app outdate_thy (thy_graph Graph.all_succs (map_filter unfinished names)); | 
| 7910 | 262 | |
| 263 | fun touch_thy name = touch_thys [name]; | |
| 264 | fun touch_child_thys name = touch_thys (thy_graph Graph.imm_succs name); | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 265 | |
| 7099 | 266 | end; | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 267 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 268 | |
| 7941 | 269 | (* load_file *) | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 270 | |
| 7191 | 271 | local | 
| 272 | ||
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 273 | fun provide path name info (deps as SOME {update_time, master, text, parents, files}) =
 | 
| 23871 | 274 | (if AList.defined (op =) files path then () | 
| 7941 | 275 | else warning (loader_msg "undeclared dependency of theory" [name] ^ | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
20664diff
changeset | 276 | " on file: " ^ quote (Path.implode path)); | 
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 277 | SOME (make_deps update_time master text parents | 
| 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 278 | (AList.update (op =) (path, SOME info) files))) | 
| 15531 | 279 | | provide _ _ _ NONE = NONE; | 
| 7941 | 280 | |
| 281 | fun run_file path = | |
| 26415 | 282 | (case Option.map (Context.theory_name o Context.the_theory) (Context.thread_data ()) of | 
| 23886 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 283 | NONE => (ThyLoad.load_ml Path.current path; ()) | 
| 23871 | 284 | | SOME name => | 
| 285 | (case lookup_deps name of | |
| 286 | SOME deps => | |
| 23886 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 287 | change_deps name (provide path name (ThyLoad.load_ml (master_dir' deps) path)) | 
| 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 288 | | NONE => (ThyLoad.load_ml Path.current path; ()))); | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 289 | |
| 7191 | 290 | in | 
| 291 | ||
| 25013 | 292 | fun provide_file path name = | 
| 293 | let | |
| 294 | val deps = get_deps name; | |
| 295 | val _ = check_unfinished error name; | |
| 296 | in | |
| 297 | (case ThyLoad.check_file (master_dir' deps) path of | |
| 298 | SOME path_info => change_deps name (provide path name path_info) | |
| 299 |     | NONE => error ("Could not find file " ^ quote (Path.implode path)))
 | |
| 300 | end; | |
| 301 | ||
| 24057 
f42665561801
removed obsolete Output.ML_errors/toplevel_errors;
 wenzelm parents: 
23982diff
changeset | 302 | fun load_file time path = | 
| 7941 | 303 | if time then | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
20664diff
changeset | 304 | let val name = Path.implode path in | 
| 7244 | 305 | timeit (fn () => | 
| 9778 | 306 |        (priority ("\n**** Starting file " ^ quote name ^ " ****");
 | 
| 9036 | 307 | run_file path; | 
| 9778 | 308 |         priority ("**** Finished file " ^ quote name ^ " ****\n")))
 | 
| 7244 | 309 | end | 
| 24057 
f42665561801
removed obsolete Output.ML_errors/toplevel_errors;
 wenzelm parents: 
23982diff
changeset | 310 | else run_file path; | 
| 7941 | 311 | |
| 26494 | 312 | fun exec_file time path = ML_Context.exec (fn () => load_file time path); | 
| 313 | ||
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
20664diff
changeset | 314 | val use = load_file false o Path.explode; | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
20664diff
changeset | 315 | val time_use = load_file true o Path.explode; | 
| 7191 | 316 | |
| 317 | end; | |
| 6233 | 318 | |
| 319 | ||
| 7099 | 320 | (* load_thy *) | 
| 321 | ||
| 9490 | 322 | fun required_by _ [] = "" | 
| 323 | | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")"; | |
| 7099 | 324 | |
| 24190 | 325 | fun load_thy time upd_time initiators dir name = | 
| 7099 | 326 | let | 
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 327 |     val _ = priority ("Loading theory " ^ quote name ^ required_by " " initiators);
 | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 328 | val (pos, text, files) = | 
| 24068 | 329 | (case get_deps name of | 
| 24190 | 330 |         SOME {master = SOME (master_path, _), text as _ :: _, files, ...} =>
 | 
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 331 | (Position.path master_path, text, files) | 
| 24068 | 332 | | _ => error (loader_msg "corrupted dependency information" [name])); | 
| 333 | val _ = touch_thy name; | |
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 334 | val _ = CRITICAL (fn () => | 
| 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 335 |       change_deps name (Option.map (fn {master, text, parents, files, ...} =>
 | 
| 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 336 | make_deps upd_time master text parents files))); | 
| 24190 | 337 | val _ = ThyLoad.load_thy dir name pos text (time orelse ! Output.timing); | 
| 7099 | 338 | in | 
| 23939 | 339 | CRITICAL (fn () => | 
| 340 | (change_deps name | |
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 341 |         (Option.map (fn {update_time, master, parents, files, ...} =>
 | 
| 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 342 | make_deps update_time master [] parents files)); | 
| 23939 | 343 | perform Update name)) | 
| 7099 | 344 | end; | 
| 345 | ||
| 346 | ||
| 24209 | 347 | (* scheduling loader tasks *) | 
| 348 | ||
| 349 | datatype task = Task of (unit -> unit) | Finished | Running; | |
| 350 | fun task_finished Finished = true | task_finished _ = false; | |
| 351 | ||
| 352 | local | |
| 353 | ||
| 24230 | 354 | fun max_task (name, (Task body, m)) NONE = SOME (name: string, (body, m)) | 
| 24229 
4b5306c36bd9
schedule_tasks: alphabetical order for equivalent tasks;
 wenzelm parents: 
24209diff
changeset | 355 | | max_task (name, (Task body, m)) (task' as SOME (name', (_, m'))) = | 
| 
4b5306c36bd9
schedule_tasks: alphabetical order for equivalent tasks;
 wenzelm parents: 
24209diff
changeset | 356 | if m > m' orelse m = m' andalso name < name' then SOME (name, (body, m)) else task' | 
| 24209 | 357 | | max_task _ task' = task'; | 
| 358 | ||
| 359 | fun next_task G = | |
| 360 | let | |
| 361 | val tasks = Graph.minimals G |> map (fn name => | |
| 362 | (name, (Graph.get_node G name, length (Graph.imm_succs G name)))); | |
| 363 | val finished = filter (task_finished o fst o snd) tasks; | |
| 364 | in | |
| 365 | if not (null finished) then next_task (Graph.del_nodes (map fst finished) G) | |
| 366 | else if null tasks then (Multithreading.Terminate, G) | |
| 367 | else | |
| 368 | (case fold max_task tasks NONE of | |
| 369 | NONE => (Multithreading.Wait, G) | |
| 370 | | SOME (name, (body, _)) => | |
| 25736 | 371 |          (Multithreading.Task {body = PrintMode.closure body,
 | 
| 372 | cont = Graph.del_nodes [name], fail = K Graph.empty}, | |
| 24209 | 373 | Graph.map_node name (K Running) G)) | 
| 374 | end; | |
| 375 | ||
| 376 | fun schedule_seq tasks = | |
| 377 | Graph.topological_order tasks | |
| 378 | |> List.app (fn name => (case Graph.get_node tasks name of Task body => body () | _ => ())); | |
| 379 | ||
| 380 | in | |
| 381 | ||
| 382 | fun schedule_tasks tasks n = | |
| 25775 
90525e67ede7
added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
 wenzelm parents: 
25736diff
changeset | 383 | let val m = Multithreading.max_threads_value () in | 
| 24209 | 384 | if m <= 1 orelse n <= 1 then schedule_seq tasks | 
| 385 | else if Multithreading.self_critical () then | |
| 386 | (warning (loader_msg "no multithreading within critical section" []); | |
| 387 | schedule_seq tasks) | |
| 388 | else | |
| 389 | (case Multithreading.schedule (Int.min (m, n)) next_task tasks of | |
| 390 | [] => () | |
| 391 | | exns => raise Exn.EXCEPTIONS (exns, "")) | |
| 392 | end; | |
| 393 | ||
| 394 | end; | |
| 395 | ||
| 396 | ||
| 23967 | 397 | (* require_thy -- checking database entries wrt. the file-system *) | 
| 15065 | 398 | |
| 7211 | 399 | local | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 400 | |
| 23886 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 401 | fun check_ml master (src_path, info) = | 
| 23871 | 402 | let val info' = | 
| 403 | (case info of NONE => NONE | |
| 404 | | SOME (_, id) => | |
| 23886 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 405 | (case ThyLoad.check_ml (master_dir master) src_path of NONE => NONE | 
| 23871 | 406 | | SOME (path', id') => if id <> id' then NONE else SOME (path', id'))) | 
| 23886 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 407 | in (src_path, info') end; | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 408 | |
| 24175 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 409 | fun check_deps dir name = | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 410 | (case lookup_deps name of | 
| 23893 
8babfcaaf129
deps: maintain source specification of parents (prevents repeated ThyLoad.deps_thy);
 wenzelm parents: 
23886diff
changeset | 411 | SOME NONE => (true, NONE, get_parents name) | 
| 
8babfcaaf129
deps: maintain source specification of parents (prevents repeated ThyLoad.deps_thy);
 wenzelm parents: 
23886diff
changeset | 412 | | NONE => | 
| 24190 | 413 |       let val {master, text, imports = parents, uses = files} = ThyLoad.deps_thy dir name
 | 
| 24068 | 414 | in (false, init_deps (SOME master) text parents files, parents) end | 
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 415 |   | SOME (deps as SOME {update_time, master, text, parents, files}) =>
 | 
| 24190 | 416 | let | 
| 417 | val (thy_path, thy_id) = ThyLoad.check_thy dir name; | |
| 418 | val master' = SOME (thy_path, thy_id); | |
| 419 | in | |
| 420 | if Option.map #2 master <> SOME thy_id then | |
| 24175 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 421 |           let val {text = text', imports = parents', uses = files', ...} =
 | 
| 24190 | 422 | ThyLoad.deps_thy dir name; | 
| 24175 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 423 | in (false, init_deps master' text' parents' files', parents') end | 
| 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 424 | else | 
| 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 425 | let | 
| 24186 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 426 | val files' = map (check_ml master') files; | 
| 24307 
434c9fbc1787
check_deps: ensure that theory is actually present, not just update_time > 1;
 wenzelm parents: 
24230diff
changeset | 427 | val current = update_time >= 0 andalso can get_theory name | 
| 
434c9fbc1787
check_deps: ensure that theory is actually present, not just update_time > 1;
 wenzelm parents: 
24230diff
changeset | 428 | andalso forall (is_some o snd) files'; | 
| 24175 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 429 | val update_time' = if current then update_time else ~1; | 
| 25030 | 430 | val deps' = SOME (make_deps update_time' master' text parents files'); | 
| 24175 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 431 | in (current, deps', parents) end | 
| 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 432 | end); | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 433 | |
| 25030 | 434 | fun read_text (SOME {update_time, master = master as SOME (path, _), text = _, parents, files}) =
 | 
| 435 | SOME (make_deps update_time master (explode (File.read path)) parents files); | |
| 436 | ||
| 23967 | 437 | in | 
| 438 | ||
| 24175 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 439 | fun require_thys time initiators dir strs tasks = | 
| 24186 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 440 | fold_map (require_thy time initiators dir) strs tasks |>> forall I | 
| 24175 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 441 | and require_thy time initiators dir str tasks = | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 442 | let | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
20664diff
changeset | 443 | val path = Path.expand (Path.explode str); | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
20664diff
changeset | 444 | val name = Path.implode (Path.base path); | 
| 23893 
8babfcaaf129
deps: maintain source specification of parents (prevents repeated ThyLoad.deps_thy);
 wenzelm parents: 
23886diff
changeset | 445 | val dir' = Path.append dir (Path.dir path); | 
| 23871 | 446 | val _ = member (op =) initiators name andalso error (cycle_msg initiators); | 
| 7066 | 447 | in | 
| 23974 | 448 | (case try (Graph.get_node (fst tasks)) name of | 
| 24209 | 449 | SOME task => (task_finished task, tasks) | 
| 23967 | 450 | | NONE => | 
| 451 | let | |
| 24175 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 452 | val (current, deps, parents) = check_deps dir' name | 
| 23967 | 453 | handle ERROR msg => cat_error msg | 
| 454 | (loader_msg "the error(s) above occurred while examining theory" [name] ^ | |
| 455 | required_by "\n" initiators); | |
| 456 | val parent_names = map base_name parents; | |
| 23871 | 457 | |
| 23974 | 458 | val (parents_current, (tasks_graph', tasks_len')) = | 
| 24175 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 459 | require_thys time (name :: initiators) | 
| 
38a15a3a1aad
theory loader: removed obsolete update_thy (coincides with use_thy);
 wenzelm parents: 
24151diff
changeset | 460 | (Path.append dir (master_dir' deps)) parents tasks; | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 461 | |
| 23967 | 462 | val all_current = current andalso parents_current; | 
| 24186 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 463 | val _ = if not all_current andalso known_thy name then outdate_thy name else (); | 
| 25030 | 464 | val entry = | 
| 465 | if all_current then (deps, SOME (get_theory name)) | |
| 466 | else (read_text deps, NONE); | |
| 467 | val _ = change_thys (new_deps name parent_names entry); | |
| 23967 | 468 | |
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 469 | val upd_time = serial (); | 
| 23974 | 470 | val tasks_graph'' = tasks_graph' |> new_deps name parent_names | 
| 24209 | 471 | (if all_current then Finished | 
| 472 | else Task (fn () => load_thy time upd_time initiators dir' name)); | |
| 23974 | 473 | val tasks_len'' = if all_current then tasks_len' else tasks_len' + 1; | 
| 474 | in (all_current, (tasks_graph'', tasks_len'')) end) | |
| 7066 | 475 | end; | 
| 6484 
3f098b0ec683
use_thy etc.: may specify path prefix, which is temporarily used as load path;
 wenzelm parents: 
6389diff
changeset | 476 | |
| 23967 | 477 | end; | 
| 478 | ||
| 479 | ||
| 24209 | 480 | (* use_thy etc. *) | 
| 23967 | 481 | |
| 482 | local | |
| 483 | ||
| 24057 
f42665561801
removed obsolete Output.ML_errors/toplevel_errors;
 wenzelm parents: 
23982diff
changeset | 484 | fun gen_use_thy' req dir arg = | 
| 24209 | 485 | let val (_, (tasks, n)) = req [] dir arg (Graph.empty, 0) | 
| 486 | in schedule_tasks tasks n end; | |
| 6241 | 487 | |
| 23893 
8babfcaaf129
deps: maintain source specification of parents (prevents repeated ThyLoad.deps_thy);
 wenzelm parents: 
23886diff
changeset | 488 | fun gen_use_thy req str = | 
| 
8babfcaaf129
deps: maintain source specification of parents (prevents repeated ThyLoad.deps_thy);
 wenzelm parents: 
23886diff
changeset | 489 | let val name = base_name str in | 
| 
8babfcaaf129
deps: maintain source specification of parents (prevents repeated ThyLoad.deps_thy);
 wenzelm parents: 
23886diff
changeset | 490 | check_unfinished warning name; | 
| 25994 | 491 | gen_use_thy' req Path.current str | 
| 23893 
8babfcaaf129
deps: maintain source specification of parents (prevents repeated ThyLoad.deps_thy);
 wenzelm parents: 
23886diff
changeset | 492 | end; | 
| 7244 | 493 | |
| 7211 | 494 | in | 
| 495 | ||
| 24186 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 496 | val use_thys_dir = gen_use_thy' (require_thys false); | 
| 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 497 | val use_thys = use_thys_dir Path.current; | 
| 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 498 | val use_thy = gen_use_thy (require_thy false); | 
| 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 499 | val time_use_thy = gen_use_thy (require_thy true); | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 500 | |
| 7211 | 501 | end; | 
| 502 | ||
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 503 | |
| 6666 | 504 | (* remove_thy *) | 
| 505 | ||
| 506 | fun remove_thy name = | |
| 7910 | 507 | if is_finished name then error (loader_msg "cannot remove finished theory" [name]) | 
| 6666 | 508 | else | 
| 509 | let val succs = thy_graph Graph.all_succs [name] in | |
| 9778 | 510 | priority (loader_msg "removing" succs); | 
| 23939 | 511 | CRITICAL (fn () => (List.app (perform Remove) succs; change_thys (Graph.del_nodes succs))) | 
| 6666 | 512 | end; | 
| 513 | ||
| 514 | ||
| 24563 | 515 | (* update_time *) | 
| 516 | ||
| 517 | structure UpdateTime = TheoryDataFun | |
| 518 | ( | |
| 519 | type T = int; | |
| 520 | val empty = 0; | |
| 521 | val copy = I; | |
| 522 | fun extend _ = empty; | |
| 523 | fun merge _ _ = empty; | |
| 524 | ); | |
| 525 | ||
| 526 | val thy_ord = int_ord o pairself UpdateTime.get; | |
| 527 | ||
| 528 | ||
| 7066 | 529 | (* begin / end theory *) | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 530 | |
| 23900 
b25b1444a246
begin_theory: simplified interface, keep thy info empty until end_theory;
 wenzelm parents: 
23893diff
changeset | 531 | fun begin_theory name parents uses int = | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 532 | let | 
| 23893 
8babfcaaf129
deps: maintain source specification of parents (prevents repeated ThyLoad.deps_thy);
 wenzelm parents: 
23886diff
changeset | 533 | val parent_names = map base_name parents; | 
| 23886 
f40fba467384
simplified ThyLoad interfaces: only one additional directory;
 wenzelm parents: 
23871diff
changeset | 534 | val dir = master_dir'' (lookup_deps name); | 
| 7244 | 535 | val _ = check_unfinished error name; | 
| 24186 
d7f267b806c9
check_deps: really do reload the master text if required;
 wenzelm parents: 
24175diff
changeset | 536 | val _ = if int then use_thys_dir dir parents else (); | 
| 17365 | 537 | |
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 538 | val theory = Theory.begin_theory name (map get_theory parent_names); | 
| 23900 
b25b1444a246
begin_theory: simplified interface, keep thy info empty until end_theory;
 wenzelm parents: 
23893diff
changeset | 539 | |
| 7952 | 540 | val deps = | 
| 541 | if known_thy name then get_deps name | |
| 24068 | 542 | else init_deps NONE [] parents (map #1 uses); | 
| 23967 | 543 | val _ = change_thys (new_deps name parent_names (deps, NONE)); | 
| 9451 | 544 | |
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 545 |     val update_time = (case deps of NONE => 0 | SOME {update_time, ...} => update_time);
 | 
| 24563 | 546 | val update_time = if update_time > 0 then update_time else serial (); | 
| 547 | val theory' = theory | |
| 548 | |> UpdateTime.put update_time | |
| 549 | |> Present.begin_theory update_time dir uses; | |
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 550 | |
| 23900 
b25b1444a246
begin_theory: simplified interface, keep thy info empty until end_theory;
 wenzelm parents: 
23893diff
changeset | 551 | val uses_now = map_filter (fn (x, true) => SOME x | _ => NONE) uses; | 
| 26494 | 552 | val theory'' = fold (Context.theory_map o exec_file false) uses_now theory'; | 
| 24151 
255f76dcc16b
replaced outdated flag by update_time (multithreading-safe presentation order);
 wenzelm parents: 
24133diff
changeset | 553 | in theory'' end; | 
| 7244 | 554 | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 555 | fun end_theory theory = | 
| 7099 | 556 | let | 
| 16454 | 557 | val name = Context.theory_name theory; | 
| 24190 | 558 | val _ = check_files name; | 
| 16504 | 559 | val theory' = Theory.end_theory theory; | 
| 23900 
b25b1444a246
begin_theory: simplified interface, keep thy info empty until end_theory;
 wenzelm parents: 
23893diff
changeset | 560 | val _ = change_thy name (fn (deps, _) => (deps, SOME theory')); | 
| 
b25b1444a246
begin_theory: simplified interface, keep thy info empty until end_theory;
 wenzelm parents: 
23893diff
changeset | 561 | in theory' end; | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 562 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 563 | |
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 564 | (* register existing theories *) | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 565 | |
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 566 | fun register_thy name = | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 567 | let | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 568 |     val _ = priority ("Registering theory " ^ quote name);
 | 
| 24563 | 569 | val thy = get_theory name; | 
| 24096 | 570 | val _ = map get_theory (get_parents name); | 
| 571 | val _ = check_unfinished error name; | |
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 572 | val _ = touch_thy name; | 
| 24190 | 573 | val master = #master (ThyLoad.deps_thy Path.current name); | 
| 24563 | 574 | val upd_time = UpdateTime.get thy; | 
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 575 | in | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 576 | CRITICAL (fn () => | 
| 24190 | 577 | (change_deps name (Option.map | 
| 578 |        (fn {parents, files, ...} => make_deps upd_time (SOME master) [] parents files));
 | |
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 579 | perform Update name)) | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 580 | end; | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 581 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 582 | fun register_theory theory = | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 583 | let | 
| 16454 | 584 | val name = Context.theory_name theory; | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 585 | val parents = Theory.parents_of theory; | 
| 16454 | 586 | val parent_names = map Context.theory_name parents; | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 587 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 588 | fun err txt bads = | 
| 23871 | 589 | error (loader_msg txt bads ^ "\ncannot register theory " ^ quote name); | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 590 | |
| 6666 | 591 | val nonfinished = filter_out is_finished parent_names; | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 592 | fun get_variant (x, y_name) = | 
| 15531 | 593 | if Theory.eq_thy (x, get_theory y_name) then NONE | 
| 594 | else SOME y_name; | |
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19473diff
changeset | 595 | val variants = map_filter get_variant (parents ~~ parent_names); | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 596 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 597 | fun register G = | 
| 15531 | 598 | (Graph.new_node (name, (NONE, SOME theory)) G | 
| 9327 | 599 | handle Graph.DUP _ => err "duplicate theory entry" []) | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 600 | |> add_deps name parent_names; | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 601 | in | 
| 6666 | 602 | if not (null nonfinished) then err "non-finished parent theories" nonfinished | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 603 | else if not (null variants) then err "different versions of parent theories" variants | 
| 23939 | 604 | else CRITICAL (fn () => (change_thys register; perform Update name)) | 
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 605 | end; | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 606 | |
| 24080 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 607 | |
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 608 | (* finish all theories *) | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 609 | |
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 610 | fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (NONE, entry))); | 
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 611 | |
| 
8c67d869531b
added register_thy (replaces pretend_use_thy_only and really flag);
 wenzelm parents: 
24071diff
changeset | 612 | |
| 6211 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 613 | (*final declarations of this structure*) | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 614 | val theory = get_theory; | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 615 | val names = get_names; | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 616 | |
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 617 | end; | 
| 
43d0efafa025
Theory loader database: theory and file dependencies, theory values
 wenzelm parents: 
5211diff
changeset | 618 | |
| 5209 | 619 | structure BasicThyInfo: BASIC_THY_INFO = ThyInfo; | 
| 620 | open BasicThyInfo; |