src/Pure/pure_thy.ML
changeset 5005 4486d53a6438
parent 5000 9271b89c7e2c
child 5022 b4bd7e6402fe
equal deleted inserted replaced
5004:cf4e3b487caf 5005:4486d53a6438
    54 
    54 
    55 (*** theorem database ***)
    55 (*** theorem database ***)
    56 
    56 
    57 (** data kind 'Pure/theorems' **)
    57 (** data kind 'Pure/theorems' **)
    58 
    58 
    59 local
    59 structure TheoremsDataArgs =
    60   val theoremsK = Object.kind "Pure/theorems";
    60 struct
    61 
    61   val name = "Pure/theorems";
    62   exception Theorems of
    62 
    63    {space: NameSpace.T,
    63   type T =
    64     thms_tab: tthm list Symtab.table,
    64     {space: NameSpace.T,
    65     const_idx: int * (int * tthm) list Symtab.table} ref;
    65       thms_tab: tthm list Symtab.table,
    66 
    66       const_idx: int * (int * tthm) list Symtab.table} ref;
    67 
    67 
    68   fun mk_empty _ =
    68   fun mk_empty _ =
    69     Theorems (ref {space = NameSpace.empty,
    69     ref {space = NameSpace.empty, thms_tab = Symtab.empty, const_idx = (0, Symtab.empty)} : T;
    70       thms_tab = Symtab.empty, const_idx = (0, Symtab.empty)});
    70 
    71 
    71   val empty = mk_empty ();
    72   fun print sg (Theorems (ref {space, thms_tab, const_idx = _})) =
    72   val prep_ext = mk_empty;
       
    73   val merge = mk_empty;
       
    74 
       
    75   fun print sg (ref {space, thms_tab, const_idx = _}) =
    73     let
    76     let
    74       val prt_thm = Attribute.pretty_tthm o apfst (Thm.transfer_sg sg);
    77       val prt_thm = Attribute.pretty_tthm o apfst (Thm.transfer_sg sg);
    75       fun prt_thms (name, [th]) =
    78       fun prt_thms (name, [th]) =
    76             Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
    79             Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
    77         | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
    80         | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
    81       val thmss = sort_wrt fst (map (apfst extrn) (Symtab.dest thms_tab));
    84       val thmss = sort_wrt fst (map (apfst extrn) (Symtab.dest thms_tab));
    82     in
    85     in
    83       Pretty.writeln (Display.pretty_name_space ("theorem name space", space));
    86       Pretty.writeln (Display.pretty_name_space ("theorem name space", space));
    84       Pretty.writeln (Pretty.big_list "theorems:" (map prt_thms thmss))
    87       Pretty.writeln (Pretty.big_list "theorems:" (map prt_thms thmss))
    85     end;
    88     end;
    86 in
    89 end;
    87   val init_theorems = Theory.init_data theoremsK (mk_empty (), mk_empty, mk_empty, print);
    90 
    88   val print_theorems = Theory.print_data theoremsK;
    91 structure TheoremsData = TheoryDataFun(TheoremsDataArgs);
    89   val get_theorems_sg = Sign.get_data theoremsK (fn Theorems r => r);
    92 val get_theorems_sg = TheoremsData.get_sg;
    90   val get_theorems = get_theorems_sg o Theory.sign_of;
    93 val get_theorems = TheoremsData.get;
    91 end;
       
    92 
    94 
    93 
    95 
    94 (* print theory *)
    96 (* print theory *)
    95 
    97 
       
    98 val print_theorems = TheoremsData.print;
    96 fun print_theory thy =
    99 fun print_theory thy =
    97   (Display.print_theory thy; print_theorems thy);
   100   (Display.print_theory thy; print_theorems thy);
    98 
   101 
    99 
   102 
   100 
   103 
   206 
   209 
   207 fun warn_overwrite name =
   210 fun warn_overwrite name =
   208   warning ("Replaced old copy of theorems " ^ quote name);
   211   warning ("Replaced old copy of theorems " ^ quote name);
   209 
   212 
   210 fun warn_same name =
   213 fun warn_same name =
   211   warning ("Theorem database already contains a copy of " ^ quote name);  
   214   warning ("Theorem database already contains a copy of " ^ quote name);
   212 
   215 
   213 fun enter_tthmx sg app_name (bname, tthmx) =
   216 fun enter_tthmx sg app_name (bname, tthmx) =
   214   let
   217   let
   215     val name = Sign.full_name sg bname;
   218     val name = Sign.full_name sg bname;
   216     fun name_tthm (nm, (thm, tgs)) = (Thm.name_thm (nm, thm), tgs);
   219     fun name_tthm (nm, (thm, tgs)) = (Thm.name_thm (nm, thm), tgs);
   285 
   288 
   286 (*** derived theory operations ***)
   289 (*** derived theory operations ***)
   287 
   290 
   288 (** theory management **)
   291 (** theory management **)
   289 
   292 
   290 (* data kind 'Pure/theory' *)
   293 (* data kind 'Pure/theory_management' *)
   291 
   294 
   292 local
   295 structure TheoryManagementDataArgs =
   293   val theoryK = Object.kind "Pure/theory";
   296 struct
   294   exception Theory of {name: string, generation: int};
   297   val name = "Pure/theory_management";
   295 
   298   type T = {name: string, generation: int};
   296   val empty = Theory {name = "", generation = 0};
   299 
   297   fun prep_ext (x as Theory _) = x;
   300   val empty = {name = "", generation = 0};
       
   301   val prep_ext  = I;
   298   fun merge _ = empty;
   302   fun merge _ = empty;
   299   fun print _ (Theory _) = ();
   303   fun print _ _ = ();
   300 in
   304 end;
   301   val init_theory = Theory.init_data theoryK (empty, prep_ext, merge, print);
   305 
   302   val get_info = Theory.get_data theoryK (fn Theory info => info);
   306 structure TheoryManagementData = TheoryDataFun(TheoryManagementDataArgs);
   303   val put_info = Theory.put_data theoryK Theory;
   307 val get_info = TheoryManagementData.get;
   304 end;
   308 val put_info = TheoryManagementData.put;
   305 
   309 
   306 
   310 
   307 (* get / put name *)
   311 (* get / put name *)
   308 
   312 
   309 val get_name = #name o get_info;
   313 val get_name = #name o get_info;
   339   let
   343   let
   340     val {name, generation} = get_info thy;
   344     val {name, generation} = get_info thy;
   341     val copy_thy =
   345     val copy_thy =
   342       thy
   346       thy
   343       |> Theory.prep_ext
   347       |> Theory.prep_ext
   344       |> Theory.add_name ("#" ^ name ^ ":" ^ string_of_int generation)	(* FIXME !!?? *)
   348       |> Theory.add_name ("#" ^ name ^ ":" ^ string_of_int generation)  (* FIXME !!?? *)
   345       |> put_info {name = name, generation = generation + 1};
   349       |> put_info {name = name, generation = generation + 1};
   346   in
   350   in
   347     (transform_error f thy, false, None) handle exn =>
   351     (transform_error f thy, false, None) handle exn =>
   348       if Sign.is_stale (Theory.sign_of thy) then (copy_thy, true, Some exn)
   352       if Sign.is_stale (Theory.sign_of thy) then (copy_thy, true, Some exn)
   349       else (thy, false, Some exn)
   353       else (thy, false, Some exn)
   376 
   380 
   377 (*** the Pure theories ***)
   381 (*** the Pure theories ***)
   378 
   382 
   379 val proto_pure =
   383 val proto_pure =
   380   Theory.pre_pure
   384   Theory.pre_pure
   381   |> Theory.apply (Attribute.setup @ [init_theorems, init_theory])
   385   |> Theory.apply (Attribute.setup @ [TheoremsData.init, TheoryManagementData.init])
   382   |> put_name "ProtoPure"
   386   |> put_name "ProtoPure"
   383   |> global_path
   387   |> global_path
   384   |> Theory.add_types
   388   |> Theory.add_types
   385    [("fun", 2, NoSyn),
   389    [("fun", 2, NoSyn),
   386     ("prop", 0, NoSyn),
   390     ("prop", 0, NoSyn),