src/Pure/Thy/thy_info.ML
author wenzelm
Wed Mar 17 16:32:38 1999 +0100 (1999-03-17)
changeset 6389 da9c26906f3f
parent 6377 e7b051fae849
child 6484 3f098b0ec683
permissions -rw-r--r--
fixed msg;
     1 (*  Title:      Pure/Thy/thy_info.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Theory loader database, including theory and file dependencies.
     6 
     7 TODO:
     8   - remove operation (GC!?);
     9   - update_all operation (!?);
    10   - put_theory:
    11       . allow for undef entry only;
    12       . elim (via theory_ref);
    13   - stronger handling of missing files (!?!?);
    14   - register_theory: do not require final parents (!?) (no?);
    15   - hooks for init, update, finish operations (!?);
    16 *)
    17 
    18 signature BASIC_THY_INFO =
    19 sig
    20   val theory: string -> theory
    21   val theory_of_sign: Sign.sg -> theory
    22   val theory_of_thm: thm -> theory
    23 (*val use: string -> unit*)             (*exported later*)
    24   val time_use: string -> unit
    25   val touch_thy: string -> unit
    26   val use_thy: string -> unit
    27   val update_thy: string -> unit
    28   val time_use_thy: string -> unit
    29 end;
    30 
    31 signature THY_INFO =
    32 sig
    33   include BASIC_THY_INFO
    34   val names: unit -> string list
    35   val get_theory: string -> theory
    36   val put_theory: theory -> unit
    37   val loaded_files: string -> (Path.T * File.info) list
    38   val load_file: bool -> Path.T -> unit
    39   val use_path: Path.T -> unit
    40   val use: string -> unit
    41   val use_thy_only: string -> unit
    42   val begin_theory: string -> string list -> (Path.T * bool) list -> theory
    43   val end_theory: theory -> theory
    44   val finalize_all: unit -> unit
    45   val register_theory: theory -> unit
    46 end;
    47 
    48 structure ThyInfo: THY_INFO =
    49 struct
    50 
    51 
    52 (** thy database **)
    53 
    54 (* messages *)
    55 
    56 fun gen_msg txt [] = txt
    57   | gen_msg txt names = txt ^ " " ^ commas_quote names;
    58 
    59 fun loader_msg txt names = gen_msg ("Theory loader: " ^ txt) names;
    60 
    61 val show_path = space_implode " via " o map quote;
    62 fun cycle_msg name names = loader_msg ("cyclic dependency of " ^ show_path (name :: names)) [];
    63 
    64 
    65 (* derived graph operations *)          (* FIXME more abstract (!?) *)
    66 
    67 fun add_deps name parents G =
    68   foldl (fn (H, parent) => Graph.add_edge_acyclic (parent, name) H) (G, parents)
    69     handle Graph.CYCLES namess => error (cat_lines (map (cycle_msg name) namess));
    70 
    71 fun del_deps name G =           (* FIXME GC (!?!?) *)
    72   foldl (fn (H, parent) => Graph.del_edge (parent, name) H) (G, Graph.imm_preds G name);
    73 
    74 fun update_node name parents entry G =
    75   add_deps name parents
    76     (if can (Graph.get_node G) name then del_deps name G else Graph.new_node (name, entry) G);
    77 
    78 
    79 (* thy database *)
    80 
    81 type deps =
    82   {present: bool, outdated: bool,
    83     master: (Path.T * File.info) list, files: (Path.T * (Path.T * File.info) option) list};
    84 
    85 fun make_deps present outdated master files =
    86   {present = present, outdated = outdated, master = master, files = files}: deps;
    87 
    88 type thy = deps option * theory option;
    89 
    90 local
    91   val database = ref (Graph.empty: thy Graph.T);
    92 in
    93   fun get_thys () = ! database;
    94   fun change_thys f = database := (f (! database));
    95 end;
    96 
    97 
    98 (* access thy graph *)
    99 
   100 fun thy_graph f x = f (get_thys ()) x;
   101 fun get_names () = map #1 (Graph.get_nodes (get_thys ()));
   102 
   103 
   104 (* access thy *)
   105 
   106 fun lookup_thy name = Some (thy_graph Graph.get_node name) handle Graph.UNDEFINED _ => None;
   107 
   108 fun get_thy name =
   109   (case lookup_thy name of
   110     Some thy => thy
   111   | None => error (loader_msg "nothing known about theory" [name]));
   112 
   113 fun change_thy name f = (get_thy name; change_thys (Graph.map_node name f));
   114 
   115 
   116 (* access deps *)
   117 
   118 val lookup_deps = apsome #1 o lookup_thy;
   119 val get_deps = #1 o get_thy;
   120 fun change_deps name f = change_thy name (fn (deps, x) => (f deps, x));
   121 
   122 fun is_final name = is_none (get_deps name);
   123 fun get_files name = (case get_deps name of Some {files, ...} => files | _ => []);
   124 val loaded_files = mapfilter #2 o get_files;
   125 
   126 
   127 (* access theory *)
   128 
   129 fun get_theory name =
   130   (case get_thy name of
   131     (_, Some theory) => theory
   132   | _ => error (loader_msg "undefined theory entry for" [name]));
   133 
   134 val theory_of_sign = get_theory o Sign.name_of;
   135 val theory_of_thm = theory_of_sign o Thm.sign_of_thm;
   136 
   137 fun put_theory theory =
   138   change_thy (PureThy.get_name theory) (fn (deps, _) => (deps, Some theory));
   139 
   140 
   141 
   142 (** thy operations **)
   143 
   144 (* maintain 'outdated' flag *)
   145 
   146 fun is_outdated name =
   147   (case lookup_deps name of
   148     Some (Some {outdated, ...}) => outdated
   149   | _ => false);
   150 
   151 fun outdate_thy name =
   152   if is_final name then ()
   153   else change_deps name (apsome (fn {present, outdated = _, master, files} =>
   154     make_deps present true master files));
   155 
   156 fun touch_thy name =
   157   if is_outdated name then ()
   158   else if is_final name then warning (loader_msg "tried to touch final theory" [name])
   159   else
   160     (case filter_out is_outdated (thy_graph Graph.all_succs [name]) \ name of
   161       [] => outdate_thy name
   162     | names =>
   163        (warning (loader_msg "the following theories become out-of-date:" names);
   164         seq outdate_thy names; outdate_thy name));
   165 
   166 val untouch_deps = apsome (fn {present, outdated = _, master, files}: deps =>
   167   make_deps present false master files);
   168 
   169 
   170 (* load_thy *)
   171 
   172 fun required_by [] = ""
   173   | required_by initiators = " (required by " ^ show_path (rev initiators) ^ ")";
   174 
   175 fun load_thy ml time initiators name parents =
   176   let
   177     val _ = if name mem_string initiators then error (cycle_msg name (rev initiators)) else ();
   178     val _ = writeln ("Loading theory " ^ quote name ^ required_by initiators);
   179 
   180     val _ = seq touch_thy (thy_graph Graph.all_succs [name]);
   181     val master = ThyLoad.load_thy name ml time;
   182 
   183     val files = get_files name;
   184     val missing_files = mapfilter (fn (path, None) => Some (Path.pack path) | _ => None) files;
   185   in
   186     if null missing_files then ()
   187     else warning (loader_msg "unresolved dependencies of theory" [name] ^
   188       "\nfile(s): " ^ commas_quote missing_files);
   189     change_deps name (fn _ => Some (make_deps true false master files))
   190   end;
   191 
   192 
   193 (* load_file *)
   194 
   195 fun run_file path =
   196   let
   197     fun provide name info (deps as Some {present, outdated, master, files}) =
   198           if exists (equal path o #1) files then
   199             Some (make_deps present outdated master (overwrite (files, (path, Some info))))
   200           else (warning (loader_msg "undeclared dependency of theory" [name] ^
   201             " on file: " ^ quote (Path.pack path)); deps)
   202       | provide _ _ None = None;
   203   in
   204     (case apsome PureThy.get_name (Context.get_context ()) of
   205       None => (ThyLoad.load_file path; ())
   206     | Some name =>
   207         if is_some (lookup_thy name) then change_deps name (provide name (ThyLoad.load_file path))
   208         else (ThyLoad.load_file path; ()))
   209   end;
   210 
   211 fun load_file false path = run_file path
   212   | load_file true path =
   213       let val name = Path.pack path in
   214         timeit (fn () =>
   215          (writeln ("\n**** Starting file " ^ quote name ^ " ****");
   216           run_file path;
   217           writeln ("**** Finished file " ^ quote name ^ " ****\n")))
   218       end;
   219 
   220 val use_path = load_file false;
   221 val use = use_path o Path.unpack;
   222 val time_use = load_file true o Path.unpack;
   223 
   224 
   225 (* require_thy *)
   226 
   227 fun init_deps master files = Some (make_deps false false master (map (rpair None) files));
   228 
   229 fun load_deps name ml =
   230   let val (master, (parents, files)) = ThyLoad.deps_thy name ml
   231   in (Some (init_deps master files), parents) end;
   232 
   233 fun file_current (_, None) = false
   234   | file_current (path, info) = info = ThyLoad.check_file path;
   235 
   236 fun current_deps ml strict name =
   237   (case lookup_deps name of
   238     None => (false, load_deps name ml)
   239   | Some deps =>
   240       let val same_deps = (None, thy_graph Graph.imm_preds name) in
   241         (case deps of
   242           None => (true, same_deps)
   243         | Some {present, outdated, master, files} =>
   244             if present andalso not strict then (true, same_deps)
   245             else
   246               let val master' = ThyLoad.check_thy name ml in
   247                 if master <> master' then (false, load_deps name ml)
   248                 else (not outdated andalso forall file_current files, same_deps)
   249               end)
   250       end);
   251 
   252 fun require_thy ml time strict keep_strict initiators name =
   253   let
   254     val require_parent =
   255       require_thy ml time (strict andalso keep_strict) keep_strict (name :: initiators);
   256     val (current, (new_deps, parents)) = current_deps ml strict name handle ERROR =>
   257       error (loader_msg "The error(s) above occurred while examining theory" [name] ^
   258         (if null initiators then "" else "\n" ^ required_by initiators));
   259     val parents_current = map require_parent parents;
   260   in
   261     if current andalso forall I parents_current then true
   262     else
   263       ((case new_deps of
   264         Some deps => change_thys (update_node name parents (untouch_deps deps, None))
   265       | None => ());
   266         load_thy ml time initiators name parents;
   267         false)
   268   end;
   269 
   270 fun gen_use_thy f name = (f name; Context.context (get_theory name));
   271 
   272 val weak_use_thy = gen_use_thy (require_thy true false false false []);
   273 val use_thy      = gen_use_thy (require_thy true false true false []);
   274 val update_thy   = gen_use_thy (require_thy true false true true []);
   275 val time_use_thy = gen_use_thy (require_thy true true true false []);
   276 val use_thy_only = gen_use_thy (require_thy false false true false []);
   277 
   278 
   279 (* begin / end theory *)                (* FIXME tune *) (* FIXME files vs. paths (!?!?) *)
   280 
   281 fun begin_theory name parents paths =
   282   let
   283     val _ = seq weak_use_thy parents;
   284     val theory = PureThy.begin_theory name (map get_theory parents);
   285     val _ = change_thys (update_node name parents (init_deps [] [], Some theory));
   286     val use_paths = mapfilter (fn (x, true) => Some x | _ => None) paths;
   287   in Context.setmp (Some theory) (seq use_path) use_paths; theory end;
   288 
   289 fun end_theory theory =
   290   let val theory' = PureThy.end_theory theory
   291   in put_theory theory'; theory' end;
   292 
   293 
   294 (* finalize entries *)
   295 
   296 fun update_all () = ();         (* FIXME fake *)
   297 
   298 fun finalize_all () =
   299   (update_all (); change_thys (Graph.map_nodes (fn (_, entry) => (None, entry))));
   300 
   301 
   302 (* register existing theories *)
   303 
   304 fun register_theory theory =
   305   let
   306     val name = PureThy.get_name theory;
   307     val parents = Theory.parents_of theory;
   308     val parent_names = map PureThy.get_name parents;
   309 
   310     fun err txt bads =
   311       error (loader_msg txt bads ^ "\n" ^ gen_msg "cannot register theory" [name]);
   312 
   313     val nonfinal = filter_out is_final parent_names;
   314     fun get_variant (x, y_name) =
   315       if Theory.eq_thy (x, get_theory y_name) then None
   316       else Some y_name;
   317     val variants = mapfilter get_variant (parents ~~ parent_names);
   318 
   319     fun register G =
   320       (Graph.new_node (name, (None, Some theory)) G
   321         handle Graph.DUPLICATE _ => err "duplicate theory entry" [])
   322       |> add_deps name parent_names;
   323   in
   324     if not (null nonfinal) then err "non-final parent theories" nonfinal
   325     else if not (null variants) then err "different versions of parent theories" variants
   326     else change_thys register
   327   end;
   328 
   329 
   330 (*final declarations of this structure*)
   331 val theory = get_theory;
   332 val names = get_names;
   333 
   334 end;
   335 
   336 structure BasicThyInfo: BASIC_THY_INFO = ThyInfo;
   337 open BasicThyInfo;