src/Pure/pure_thy.ML
changeset 4853 67bcbb03c235
parent 4792 8e3c2dddb9c8
child 4922 03b81b6e1baa
equal deleted inserted replaced
4852:58b5006d36cc 4853:67bcbb03c235
     1 (*  Title:      Pure/pure_thy.ML
     1 (*  Title:      Pure/pure_thy.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     4 
     5 The Pure theories.
     5 The Pure theories.
     6 
       
     7 TODO:
       
     8   - tagged axioms / defs;
       
     9 *)
     6 *)
    10 
     7 
    11 signature BASIC_PURE_THY =
     8 signature BASIC_PURE_THY =
    12 sig
     9 sig
    13   val get_thm: theory -> xstring -> thm
    10   val get_thm: theory -> xstring -> thm
    14   val get_thms: theory -> xstring -> thm list
    11   val get_thms: theory -> xstring -> thm list
    15   val thms_of: theory -> (string * thm) list
    12   val thms_of: theory -> (string * thm) list
    16 end
    13 end;
    17 
    14 
    18 signature PURE_THY =
    15 signature PURE_THY =
    19 sig
    16 sig
    20   include BASIC_PURE_THY
    17   include BASIC_PURE_THY
    21   val get_tthm: theory -> xstring -> tthm
    18   val get_tthm: theory -> xstring -> tthm
    22   val get_tthms: theory -> xstring -> tthm list
    19   val get_tthms: theory -> xstring -> tthm list
    23   val thms_containing: theory -> string list -> (string * thm) list
    20   val thms_containing: theory -> string list -> (string * thm) list
    24   val store_thms: (bstring * thm) list -> theory -> theory
       
    25   val store_thmss: (bstring * thm list) list -> theory -> theory
       
    26   val store_tthms: (bstring * tthm) list -> theory -> theory
       
    27   val store_tthmss: (bstring * tthm list) list -> theory -> theory
       
    28   val smart_store_thm: (bstring * thm) -> thm
    21   val smart_store_thm: (bstring * thm) -> thm
    29   val add_store_axioms: (bstring * string) list -> theory -> theory
    22   val add_tthms: ((bstring * tthm) * theory attribute list) list -> theory -> theory
    30   val add_store_axioms_i: (bstring * term) list -> theory -> theory
    23   val add_tthmss: ((bstring * tthm list) * theory attribute list) list -> theory -> theory
    31   val add_store_defs: (bstring * string) list -> theory -> theory
    24   val add_axioms: ((bstring * string) * theory attribute list) list -> theory -> theory
    32   val add_store_defs_i: (bstring * term) list -> theory -> theory
    25   val add_axioms_i: ((bstring * term) * theory attribute list) list -> theory -> theory
       
    26   val add_axiomss: ((bstring * string list) * theory attribute list) list -> theory -> theory
       
    27   val add_axiomss_i: ((bstring * term list) * theory attribute list) list -> theory -> theory
       
    28   val add_defs: ((bstring * string) * theory attribute list) list -> theory -> theory
       
    29   val add_defs_i: ((bstring * term) * theory attribute list) list -> theory -> theory
       
    30   val add_defss: ((bstring * string list) * theory attribute list) list -> theory -> theory
       
    31   val add_defss_i: ((bstring * term list) * theory attribute list) list -> theory -> theory
    33   val proto_pure: theory
    32   val proto_pure: theory
    34   val pure: theory
    33   val pure: theory
    35   val cpure: theory
    34   val cpure: theory
    36 end;
    35 end;
    37 
    36 
    52 
    51 
    53 
    52 
    54 (* methods *)
    53 (* methods *)
    55 
    54 
    56 local
    55 local
    57 
    56   fun mk_empty _ =
    58 fun mk_empty _ =
    57     Theorems (ref {space = NameSpace.empty,
    59   Theorems (ref {space = NameSpace.empty,
    58       thms_tab = Symtab.empty, const_idx = (0, Symtab.empty)});
    60     thms_tab = Symtab.empty, const_idx = (0, Symtab.empty)});
    59 
    61 
    60   fun print sg (Theorems (ref {space, thms_tab, const_idx = _})) =
    62 fun print sg (Theorems (ref {space, thms_tab, const_idx = _})) =
    61     let
    63   let
    62       val prt_thm = Attribute.pretty_tthm o apfst (Thm.transfer_sg sg);
    64     val prt_thm = Attribute.pretty_tthm o apfst (Thm.transfer_sg sg);
    63       fun prt_thms (name, [th]) =
    65     fun prt_thms (name, [th]) =
    64             Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
    66           Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
    65         | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
    67       | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
    66 
    68 
    67       fun extrn name =
    69     fun extrn name =
    68         if ! long_names then name else NameSpace.extern space name;
    70       if ! long_names then name else NameSpace.extern space name;
    69       val thmss = sort_wrt fst (map (apfst extrn) (Symtab.dest thms_tab));
    71     val thmss = sort_wrt fst (map (apfst extrn) (Symtab.dest thms_tab));
    70     in
    72   in
    71       Pretty.writeln (Display.pretty_name_space ("theorem name space", space));
    73     Pretty.writeln (Display.pretty_name_space ("theorem name space", space));
    72       Pretty.writeln (Pretty.big_list "theorems:" (map prt_thms thmss))
    74     Pretty.writeln (Pretty.big_list "theorems:" (map prt_thms thmss))
    73     end;
    75   end;
       
    76 
       
    77 in
    74 in
    78 
    75   val theorems_setup = Theory.init_data [(theoremsK, (mk_empty (), mk_empty, mk_empty, print))];
    79 val theorems_setup = Theory.init_data [(theoremsK, (mk_empty (), mk_empty, mk_empty, print))];
       
    80 
       
    81 end;
    76 end;
    82 
    77 
    83 
    78 
    84 (* get data record *)
    79 (* get data record *)
    85 
    80 
   182 
   177 
   183 
   178 
   184 
   179 
   185 (** store theorems **)                    (*DESTRUCTIVE*)
   180 (** store theorems **)                    (*DESTRUCTIVE*)
   186 
   181 
       
   182 (* naming *)
       
   183 
       
   184 fun gen_names len name =
       
   185   map (fn i => name ^ "_" ^ string_of_int i) (1 upto len);
       
   186 
       
   187 fun name_single name x = [(name, x)];
       
   188 fun name_multi name xs = gen_names (length xs) name ~~ xs;
       
   189 
       
   190 
       
   191 (* enter_tthmx *)
       
   192 
   187 fun warn_overwrite name =
   193 fun warn_overwrite name =
   188   warning ("Replaced old copy of theorems " ^ quote name);
   194   warning ("Replaced old copy of theorems " ^ quote name);
   189 
   195 
   190 fun warn_same name =
   196 fun warn_same name =
   191   warning ("Theorem database already contains a copy of " ^ quote name);
   197   warning ("Theorem database already contains a copy of " ^ quote name);  
   192 
   198 
   193 fun enter_tthms sg single (raw_name, tthms) =
   199 fun enter_tthmx sg app_name (bname, tthmx) =
   194   let
   200   let
   195     val len = length tthms;
   201     val name = Sign.full_name sg bname;
   196     val name = Sign.full_name sg raw_name;
   202     fun name_tthm (nm, (thm, tgs)) = (Thm.name_thm (nm, thm), tgs);
   197     val names =
   203     val named_tthms = map name_tthm (app_name name tthmx);
   198       if single then replicate len name
       
   199       else map (fn i => name ^ "_" ^ string_of_int i) (0 upto (len - 1));
       
   200     val named_tthms = map2 (fn (x, (th, tg)) => (Thm.name_thm (x, th), tg)) (names, tthms);
       
   201 
   204 
   202     fun eq_tthm ((th1, _), (th2, _)) = Thm.eq_thm (th1, th2);
   205     fun eq_tthm ((th1, _), (th2, _)) = Thm.eq_thm (th1, th2);
   203 
   206 
   204     val r as ref {space, thms_tab, const_idx} = get_theorems_sg sg;
   207     val r as ref {space, thms_tab, const_idx} = get_theorems_sg sg;
   205 
   208 
   206     val overwrite =
   209     val overwrite =
   207       (case Symtab.lookup (thms_tab, name) of
   210       (case Symtab.lookup (thms_tab, name) of
   208         None => false
   211         None => false
   209       | Some tthms' =>
   212       | Some tthms' =>
   210           if length tthms' = len andalso forall2 eq_tthm (tthms', named_tthms) then
   213           if length tthms' = length named_tthms andalso forall2 eq_tthm (tthms', named_tthms) then
   211             (warn_same name; false)
   214             (warn_same name; false)
   212           else (warn_overwrite name; true));
   215           else (warn_overwrite name; true));
   213 
   216 
   214     val space' = NameSpace.extend (space, [name]);
   217     val space' = NameSpace.extend (space, [name]);
   215     val thms_tab' = Symtab.update ((name, named_tthms), thms_tab);
   218     val thms_tab' = Symtab.update ((name, named_tthms), thms_tab);
   219   in
   222   in
   220     r := {space = space', thms_tab = thms_tab', const_idx = const_idx'};
   223     r := {space = space', thms_tab = thms_tab', const_idx = const_idx'};
   221     named_tthms
   224     named_tthms
   222   end;
   225   end;
   223 
   226 
   224 fun do_enter_tthms sg single tthms = (enter_tthms sg single tthms; ());
   227 
   225 
   228 (* add_tthms(s) *)
   226 
   229 
   227 fun store_tthmss tthmss thy =
   230 fun add_tthmx app_name app_att ((bname, tthmx), atts) thy =
   228   (seq (do_enter_tthms (Theory.sign_of thy) false) tthmss; thy);
   231   let val (thy', tthmx') = app_att ((thy, tthmx), atts)
   229 
   232   in enter_tthmx (Theory.sign_of thy') app_name (bname, tthmx'); thy' end;
   230 fun store_tthms tthms thy =
   233 
   231   (seq (do_enter_tthms (Theory.sign_of thy) true) (map (apsnd (fn th => [th])) tthms); thy);
   234 val add_tthms = Theory.apply o map (add_tthmx name_single Attribute.apply);
   232 
   235 val add_tthmss = Theory.apply o map (add_tthmx name_multi Attribute.applys);
   233 fun store_thmss thmss = store_tthmss (map (apsnd (map Attribute.tthm_of)) thmss);
   236 
   234 fun store_thms thms = store_tthms (map (apsnd Attribute.tthm_of) thms);
   237 
       
   238 (* smart_store_thm *)
   235 
   239 
   236 fun smart_store_thm (name, thm) =
   240 fun smart_store_thm (name, thm) =
   237   let val [(thm', _)] = enter_tthms (Thm.sign_of_thm thm) true (name, [Attribute.tthm_of thm])
   241   let val [(thm', _)] = enter_tthmx (Thm.sign_of_thm thm) name_single (name, Attribute.tthm_of thm)
   238   in thm' end;
   242   in thm' end;
   239 
   243 
   240 
   244 
   241 (* store axioms as theorems *)
   245 (* store axioms as theorems *)
   242 
   246 
   243 fun add_store add named_axs thy =
   247 local
   244   let
   248   fun add_ax app_name add ((name, axs), atts) thy =
   245     val thy' = add named_axs thy;
   249     let
   246     val named_thms = map (fn (name, _) => (name, get_axiom thy' name)) named_axs;
   250       val named_axs = app_name name axs;
   247   in store_thms named_thms thy' end;
   251       val thy' = add named_axs thy;
   248 
   252       val tthms = map (Attribute.tthm_of o Thm.get_axiom thy' o fst) named_axs;
   249 val add_store_axioms = add_store Theory.add_axioms;
   253     in add_tthmss [((name, tthms), atts)] thy' end;
   250 val add_store_axioms_i = add_store Theory.add_axioms_i;
   254 
   251 val add_store_defs = add_store Theory.add_defs;
   255   fun add_axs app_name add = Theory.apply o map (add_ax app_name add);
   252 val add_store_defs_i = add_store Theory.add_defs_i;
   256 in
       
   257   val add_axioms    = add_axs name_single Theory.add_axioms;
       
   258   val add_axioms_i  = add_axs name_single Theory.add_axioms_i;
       
   259   val add_axiomss   = add_axs name_multi Theory.add_axioms;
       
   260   val add_axiomss_i = add_axs name_multi Theory.add_axioms_i;
       
   261   val add_defs      = add_axs name_single Theory.add_defs;
       
   262   val add_defs_i    = add_axs name_single Theory.add_defs_i;
       
   263   val add_defss     = add_axs name_multi Theory.add_defs;
       
   264   val add_defss_i   = add_axs name_multi Theory.add_defs_i;
       
   265 end;
   253 
   266 
   254 
   267 
   255 
   268 
   256 (** the Pure theories **)
   269 (** the Pure theories **)
   257 
   270 
   258 val proto_pure =
   271 val proto_pure =
   259   Theory.pre_pure
   272   Theory.pre_pure
   260   |> Theory.setup [Attribute.setup, theorems_setup]
   273   |> Theory.apply [Attribute.setup, theorems_setup]
   261   |> Theory.add_types
   274   |> Theory.add_types
   262    (("fun", 2, NoSyn) ::
   275    (("fun", 2, NoSyn) ::
   263     ("prop", 0, NoSyn) ::
   276     ("prop", 0, NoSyn) ::
   264     ("itself", 1, NoSyn) ::
   277     ("itself", 1, NoSyn) ::
   265     Syntax.pure_types)
   278     Syntax.pure_types)
   281     ("==>", "[prop, prop] => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
   294     ("==>", "[prop, prop] => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
   282     ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
   295     ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
   283     ("Goal", "prop => prop", Mixfix ("GOAL _", [999], 1000)),
   296     ("Goal", "prop => prop", Mixfix ("GOAL _", [999], 1000)),
   284     ("TYPE", "'a itself", NoSyn)]
   297     ("TYPE", "'a itself", NoSyn)]
   285   |> Theory.add_path "ProtoPure"
   298   |> Theory.add_path "ProtoPure"
   286   |> add_store_defs
   299   |> (add_defs o map Attribute.none)
   287    [("flexpair_def", "(t =?= u) == (t == u::'a::{})"),
   300    [("flexpair_def", "(t =?= u) == (t == u::'a::{})"),
   288     ("Goal_def", "GOAL (PROP A) == PROP A")]
   301     ("Goal_def", "GOAL (PROP A) == PROP A")]
   289   |> Theory.add_name "ProtoPure";
   302   |> Theory.add_name "ProtoPure";
   290 
   303 
   291 val pure =
   304 val pure =