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