src/Pure/Thy/thy_info.ML
changeset 6362 bbbea7fecb93
parent 6329 bbd03b119f36
child 6377 e7b051fae849
equal deleted inserted replaced
6361:3a45ad4a95eb 6362:bbbea7fecb93
     1 (*  Title:      Pure/Thy/thy_info.ML
     1 (*  Title:      Pure/Thy/thy_info.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     4 
     5 Theory loader database: theory and file dependencies, theory values
     5 Theory loader database, including theory and file dependencies.
     6 and user data.
       
     7 
     6 
     8 TODO:
     7 TODO:
     9   - data: ThyInfoFun;
       
    10   - remove operation (GC!?);
     8   - remove operation (GC!?);
    11   - update_all operation (!?);
     9   - update_all operation (!?);
    12   - put_theory:
    10   - put_theory:
    13       . include data;
       
    14       . allow for undef entry only;
    11       . allow for undef entry only;
    15       . elim (via theory_ref);
    12       . elim (via theory_ref);
    16   - stronger handling of missing files (!?!?);
    13   - stronger handling of missing files (!?!?);
    17   - register_theory: do not require final parents (!?) (no?);
    14   - register_theory: do not require final parents (!?) (no?);
    18   - observe verbose flag;
    15   - observe verbose flag;
    47   val end_theory: theory -> theory
    44   val end_theory: theory -> theory
    48   val finalize_all: unit -> unit
    45   val finalize_all: unit -> unit
    49   val register_theory: theory -> unit
    46   val register_theory: theory -> unit
    50 end;
    47 end;
    51 
    48 
    52 signature PRIVATE_THY_INFO =
    49 structure ThyInfo: THY_INFO =
    53 sig
       
    54   include THY_INFO
       
    55 (* FIXME
       
    56   val init_data: Object.kind -> (Object.T * (Object.T -> Object.T) *
       
    57     (Object.T * Object.T -> Object.T) * (Sign.sg -> Object.T -> unit)) -> unit
       
    58   val print_data: Object.kind -> string -> unit
       
    59   val get_data: Object.kind -> (Object.T -> 'a) -> string -> 'a
       
    60   val put_data: Object.kind -> ('a -> Object.T) -> 'a -> unit
       
    61 *)
       
    62 end;
       
    63 
       
    64 structure ThyInfo: PRIVATE_THY_INFO =
       
    65 struct
    50 struct
    66 
    51 
    67 
    52 
    68 (** thy database **)
    53 (** thy database **)
    69 
    54 
   106     master: (Path.T * File.info) list, files: (Path.T * (Path.T * File.info) option) list};
    91     master: (Path.T * File.info) list, files: (Path.T * (Path.T * File.info) option) list};
   107 
    92 
   108 fun make_deps present outdated master files =
    93 fun make_deps present outdated master files =
   109   {present = present, outdated = outdated, master = master, files = files}: deps;
    94   {present = present, outdated = outdated, master = master, files = files}: deps;
   110 
    95 
   111 type thy = deps option * (theory * Object.T Symtab.table) option;
    96 type thy = deps option * theory option;
   112 type kind = Object.kind * (Object.T * (Object.T -> unit));
       
   113 
       
   114 
    97 
   115 local
    98 local
   116   val database = ref (Graph.empty: thy Graph.T, Symtab.empty: kind Symtab.table);
    99   val database = ref (Graph.empty: thy Graph.T);
   117 in
   100 in
   118 
   101   fun get_thys () = ! database;
   119 fun get_thys () = #1 (! database);
   102   fun change_thys f = database := (f (! database));
   120 fun get_kinds () = #2 (! database);
       
   121 fun change_thys f = database := (f (get_thys ()), get_kinds ());
       
   122 fun change_kinds f = database := (get_thys (), f (get_kinds ()));
       
   123 
       
   124 end;
   103 end;
   125 
   104 
   126 
   105 
   127 (* access thy graph *)
   106 (* access thy graph *)
   128 
   107 
   155 
   134 
   156 (* access theory *)
   135 (* access theory *)
   157 
   136 
   158 fun get_theory name =
   137 fun get_theory name =
   159   (case get_thy name of
   138   (case get_thy name of
   160     (_, Some (theory, _)) => theory
   139     (_, Some theory) => theory
   161   | _ => error (loader_msg "undefined theory entry for" [name]));
   140   | _ => error (loader_msg "undefined theory entry for" [name]));
   162 
   141 
   163 val theory_of_sign = get_theory o Sign.name_of;
   142 val theory_of_sign = get_theory o Sign.name_of;
   164 val theory_of_thm = theory_of_sign o Thm.sign_of_thm;
   143 val theory_of_thm = theory_of_sign o Thm.sign_of_thm;
   165 
   144 
   166 fun put_theory theory =
   145 fun put_theory theory =
   167   change_thy (PureThy.get_name theory) (fn (deps, _) => (deps, (Some (theory, Symtab.empty))));
   146   change_thy (PureThy.get_name theory) (fn (deps, _) => (deps, Some theory));
   168 
       
   169 
       
   170 (** thy data **)  (* FIXME *)
       
   171 
   147 
   172 
   148 
   173 
   149 
   174 (** thy operations **)
   150 (** thy operations **)
   175 
   151 
   312 
   288 
   313 fun begin_theory name parents paths =
   289 fun begin_theory name parents paths =
   314   let
   290   let
   315     val _ = seq weak_use_thy parents;
   291     val _ = seq weak_use_thy parents;
   316     val theory = PureThy.begin_theory name (map get_theory parents);
   292     val theory = PureThy.begin_theory name (map get_theory parents);
   317     val _ = change_thys (update_node name parents (init_deps [] [], Some (theory, Symtab.empty)));
   293     val _ = change_thys (update_node name parents (init_deps [] [], Some theory));
   318     val use_paths = mapfilter (fn (x, true) => Some x | _ => None) paths;
   294     val use_paths = mapfilter (fn (x, true) => Some x | _ => None) paths;
   319   in Context.setmp (Some theory) (seq use_path) use_paths; theory end;
   295   in Context.setmp (Some theory) (seq use_path) use_paths; theory end;
   320 
   296 
   321 fun end_theory theory =
   297 fun end_theory theory =
   322   let val theory' = PureThy.end_theory theory
   298   let val theory' = PureThy.end_theory theory
   323   in put_theory theory'; theory' end;
   299   in put_theory theory'; theory' end;
   324 
   300 
   325 
   301 
   326 (* finalize entries *)
   302 (* finalize entries *)
   327 
       
   328 (* FIXME
       
   329 fun finishs names =
       
   330   let
       
   331     fun err txt bads =
       
   332       error (loader_msg txt bads ^ "\n" ^ gen_msg "cannot mark" names ^ " as finished");
       
   333 
       
   334     val all_preds = thy_graph Graph.all_preds names;
       
   335     val noncurrent = filter_out is_current all_preds;
       
   336     val unfinished = filter_out is_finished (all_preds \\ names);
       
   337   in
       
   338     if not (null noncurrent) then err "non-current theories" noncurrent
       
   339     else if not (null unfinished) then err "unfinished ancestor theories" unfinished
       
   340     else seq (fn name => change_thy name (apfst (K Finished)))
       
   341   end;
       
   342 
       
   343 fun finish name = finishs [name];
       
   344 *)
       
   345 
   303 
   346 fun update_all () = ();         (* FIXME fake *)
   304 fun update_all () = ();         (* FIXME fake *)
   347 
   305 
   348 fun finalize_all () =
   306 fun finalize_all () =
   349   (update_all (); change_thys (Graph.map_nodes (fn (_, entry) => (None, entry))));
   307   (update_all (); change_thys (Graph.map_nodes (fn (_, entry) => (None, entry))));
   365       if Theory.eq_thy (x, get_theory y_name) then None
   323       if Theory.eq_thy (x, get_theory y_name) then None
   366       else Some y_name;
   324       else Some y_name;
   367     val variants = mapfilter get_variant (parents ~~ parent_names);
   325     val variants = mapfilter get_variant (parents ~~ parent_names);
   368 
   326 
   369     fun register G =
   327     fun register G =
   370       (Graph.new_node (name, (None, Some (theory, Symtab.empty))) G
   328       (Graph.new_node (name, (None, Some theory)) G
   371         handle Graph.DUPLICATE _ => err "duplicate theory entry" [])
   329         handle Graph.DUPLICATE _ => err "duplicate theory entry" [])
   372       |> add_deps name parent_names;
   330       |> add_deps name parent_names;
   373   in
   331   in
   374     if not (null nonfinal) then err "non-final parent theories" nonfinal
   332     if not (null nonfinal) then err "non-final parent theories" nonfinal
   375     else if not (null variants) then err "different versions of parent theories" variants
   333     else if not (null variants) then err "different versions of parent theories" variants