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