src/Pure/pure_thy.ML
author wenzelm
Thu Jul 13 23:14:49 2000 +0200 (2000-07-13)
changeset 9318 4c3fb0786022
parent 9238 ad37b21c0dc6
child 9534 0d14a9e7930c
permissions -rw-r--r--
add_defs(_i): overloaded option;
     1 (*  Title:      Pure/pure_thy.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Theorem database, derived theory operations, and the ProtoPure theory.
     7 *)
     8 
     9 signature BASIC_PURE_THY =
    10 sig
    11   val print_theorems: theory -> unit
    12   val print_theory: theory -> unit
    13   val get_thm: theory -> xstring -> thm
    14   val get_thms: theory -> xstring -> thm list
    15   val get_thmss: theory -> xstring list -> thm list
    16   val thms_of: theory -> (string * thm) list
    17   structure ProtoPure:
    18     sig
    19       val thy: theory
    20       val flexpair_def: thm
    21       val Goal_def: thm
    22     end
    23 end;
    24 
    25 signature PURE_THY =
    26 sig
    27   include BASIC_PURE_THY
    28   val cond_extern_thm_sg: Sign.sg -> string -> xstring
    29   val thms_containing: theory -> string list -> (string * thm) list
    30   val store_thm: (bstring * thm) * theory attribute list -> theory -> theory * thm
    31   val smart_store_thms: (bstring * thm list) -> thm list
    32   val forall_elim_var: int -> thm -> thm
    33   val forall_elim_vars: int -> thm -> thm
    34   val add_thms: ((bstring * thm) * theory attribute list) list -> theory -> theory * thm list
    35   val add_thmss: ((bstring * thm list) * theory attribute list) list -> theory -> theory * thm list list
    36   val have_thmss: theory attribute list -> ((bstring * theory attribute list) *
    37     (thm list * theory attribute list) list) list -> theory -> theory * (string * thm list) list
    38   val add_axioms: ((bstring * string) * theory attribute list) list -> theory -> theory * thm list
    39   val add_axioms_i: ((bstring * term) * theory attribute list) list -> theory -> theory * thm list
    40   val add_axiomss: ((bstring * string list) * theory attribute list) list -> theory -> theory * thm list list
    41   val add_axiomss_i: ((bstring * term list) * theory attribute list) list -> theory -> theory * thm list list
    42   val add_defs: bool -> ((bstring * string) * theory attribute list) list
    43     -> theory -> theory * thm list
    44   val add_defs_i: bool -> ((bstring * term) * theory attribute list) list
    45     -> theory -> theory * thm list
    46   val add_defss: bool -> ((bstring * string list) * theory attribute list) list
    47     -> theory -> theory * thm list list
    48   val add_defss_i: bool -> ((bstring * term list) * theory attribute list) list
    49     -> theory -> theory * thm list list
    50   val get_name: theory -> string
    51   val put_name: string -> theory -> theory
    52   val global_path: theory -> theory
    53   val local_path: theory -> theory
    54   val begin_theory: string -> theory list -> theory
    55   val end_theory: theory -> theory
    56   val checkpoint: theory -> theory
    57   val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
    58   val dummy_patternN: string
    59 end;
    60 
    61 structure PureThy: PURE_THY =
    62 struct
    63 
    64 
    65 (*** theorem database ***)
    66 
    67 (** data kind 'Pure/theorems' **)
    68 
    69 structure TheoremsDataArgs =
    70 struct
    71   val name = "Pure/theorems";
    72 
    73   type T =
    74     {space: NameSpace.T,
    75       thms_tab: thm list Symtab.table,
    76       const_idx: int * (int * thm) list Symtab.table} ref;
    77 
    78   fun mk_empty _ =
    79     ref {space = NameSpace.empty, thms_tab = Symtab.empty, const_idx = (0, Symtab.empty)} : T;
    80 
    81   val empty = mk_empty ();
    82   fun copy (ref x) = ref x;
    83   val prep_ext = mk_empty;
    84   val merge = mk_empty;
    85 
    86   fun pretty sg (ref {space, thms_tab, const_idx = _}) =
    87     let
    88       val prt_thm = Display.pretty_thm o Thm.transfer_sg sg;
    89       fun prt_thms (name, [th]) =
    90             Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
    91         | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
    92 
    93       val thmss = NameSpace.cond_extern_table space thms_tab;
    94     in Pretty.big_list "theorems:" (map prt_thms thmss) end;
    95 
    96   fun print sg data = Pretty.writeln (pretty sg data);
    97 end;
    98 
    99 structure TheoremsData = TheoryDataFun(TheoremsDataArgs);
   100 val get_theorems_sg = TheoremsData.get_sg;
   101 val get_theorems = TheoremsData.get;
   102 
   103 val cond_extern_thm_sg = NameSpace.cond_extern o #space o ! o get_theorems_sg;
   104 
   105 
   106 (* print theory *)
   107 
   108 val print_theorems = TheoremsData.print;
   109 
   110 fun print_theory thy =
   111   Display.pretty_full_theory thy @
   112   [TheoremsDataArgs.pretty (Theory.sign_of thy) (get_theorems thy)]
   113   |> Pretty.chunks |> Pretty.writeln;
   114 
   115 
   116 
   117 (** retrieve theorems **)
   118 
   119 (* get_thms etc. *)
   120 
   121 fun lookup_thms name thy =
   122   let val ref {space, thms_tab, ...} = get_theorems thy
   123   in Symtab.lookup (thms_tab, NameSpace.intern space name) end;
   124 
   125 fun get_thms thy name =
   126   (case get_first (lookup_thms name) (thy :: Theory.ancestors_of thy) of
   127     None => raise THEORY ("Unknown theorem(s) " ^ quote name, [thy])
   128   | Some thms => map (Thm.transfer thy) thms);
   129 
   130 fun get_thm thy name =
   131   (case get_thms thy name of
   132     [thm] => thm
   133   | _ => raise THEORY ("Single theorem expected " ^ quote name, [thy]));
   134 
   135 fun get_thmss thy names = flat (map (get_thms thy) names);
   136 
   137 
   138 (* thms_of *)
   139 
   140 fun attach_name thm = (Thm.name_of_thm thm, thm);
   141 
   142 fun thms_of thy =
   143   let val ref {thms_tab, ...} = get_theorems thy in
   144     map attach_name (flat (map snd (Symtab.dest thms_tab)))
   145   end;
   146 
   147 
   148 
   149 (** theorems indexed by constants **)
   150 
   151 (* make index *)
   152 
   153 fun add_const_idx ((next, table), thm) =
   154   let
   155     val {hyps, prop, ...} = Thm.rep_thm thm;
   156     val consts =
   157       foldr add_term_consts (hyps, add_term_consts (prop, []));
   158 
   159     fun add (tab, c) =
   160       Symtab.update ((c, (next, thm) :: Symtab.lookup_multi (tab, c)), tab);
   161   in (next + 1, foldl add (table, consts)) end;
   162 
   163 fun make_const_idx thm_tab =
   164   Symtab.foldl (fn (x, (_, ths)) => foldl add_const_idx (x, ths)) ((0, Symtab.empty), thm_tab);
   165 
   166 
   167 (* lookup index *)
   168 
   169 (*search locally*)
   170 fun containing [] thy = thms_of thy
   171   | containing consts thy =
   172       let
   173         fun int ([], _) = []
   174           | int (_, []) = []
   175           | int (xxs as ((x as (i:int, _)) :: xs), yys as ((y as (j, _)) :: ys)) =
   176               if i = j then x :: int (xs, ys)
   177               else if i > j then int (xs, yys)
   178               else int (xxs, ys);
   179 
   180         fun ints [xs] = xs
   181           | ints xss = if exists null xss then [] else foldl int (hd xss, tl xss);
   182 
   183         val ref {const_idx = (_, ctab), ...} = get_theorems thy;
   184         val ithmss = map (fn c => Symtab.lookup_multi (ctab, c)) consts;
   185       in map (attach_name o snd) (ints ithmss) end;
   186 
   187 (*search globally*)
   188 fun thms_containing thy consts =
   189   (case filter (is_none o Sign.const_type (Theory.sign_of thy)) consts of
   190     [] => flat (map (containing consts) (thy :: Theory.ancestors_of thy))
   191   | cs => raise THEORY ("thms_containing: undeclared consts " ^ commas_quote cs, [thy]));
   192 
   193 
   194 
   195 (** store theorems **)                    (*DESTRUCTIVE*)
   196 
   197 (* naming *)
   198 
   199 fun gen_names len name =
   200   map (fn i => name ^ "_" ^ string_of_int i) (1 upto len);
   201 
   202 fun name_single name x = [(name, x)];
   203 fun name_multi name xs = gen_names (length xs) name ~~ xs;
   204 
   205 
   206 (* enter_thmx *)
   207 
   208 fun warn_overwrite name = warning ("Replaced old copy of theorems " ^ quote name);
   209 fun warn_same name = warning ("Theorem database already contains a copy of " ^ quote name);
   210 
   211 fun enter_thmx _ app_name ("", thmx) = map #2 (app_name "" thmx)
   212   | enter_thmx sg app_name (bname, thmx) =
   213       let
   214         val name = Sign.full_name sg bname;
   215         val named_thms = map Thm.name_thm (app_name name thmx);
   216 
   217         val r as ref {space, thms_tab, const_idx} = get_theorems_sg sg;
   218 
   219         val overwrite =
   220           (case Symtab.lookup (thms_tab, name) of
   221             None => false
   222           | Some thms' =>
   223               if Library.equal_lists Thm.eq_thm (thms', named_thms) then (warn_same name; false)
   224               else (warn_overwrite name; true));
   225 
   226         val space' = NameSpace.extend (space, [name]);
   227         val thms_tab' = Symtab.update ((name, named_thms), thms_tab);
   228         val const_idx' =
   229           if overwrite then make_const_idx thms_tab'
   230           else foldl add_const_idx (const_idx, named_thms);
   231       in r := {space = space', thms_tab = thms_tab', const_idx = const_idx'}; named_thms end;
   232 
   233 
   234 (* add_thms(s) *)
   235 
   236 fun add_thmx app_name app_att ((bname, thmx), atts) thy =
   237   let
   238     val (thy', thmx') = app_att ((thy, thmx), atts);
   239     val thms'' = enter_thmx (Theory.sign_of thy') app_name (bname, thmx');
   240   in (thy', thms'') end;
   241 
   242 fun add_thms args theory =
   243   (theory, args)
   244   |> foldl_map (fn (thy, arg) => add_thmx name_single Thm.apply_attributes arg thy)
   245   |> apsnd (map hd);
   246 
   247 fun add_thmss args theory =
   248   (theory, args)
   249   |> foldl_map (fn (thy, arg) => add_thmx name_multi Thm.applys_attributes arg thy);
   250 
   251 
   252 (* have_thmss *)
   253 
   254 local
   255   fun have_thss kind_atts (thy, ((bname, more_atts), ths_atts)) =
   256     let
   257       fun app (x, (ths, atts)) = Thm.applys_attributes ((x, ths), atts);
   258       val (thy', thmss') =
   259         foldl_map app (thy, map (fn (ths, atts) => (ths, atts @ more_atts @ kind_atts)) ths_atts);
   260       val thms' = flat thmss';
   261       val thms'' = enter_thmx (Theory.sign_of thy') name_multi (bname, thms');
   262     in (thy', (bname, thms'')) end;
   263 in
   264   fun have_thmss kind_atts args thy = foldl_map (have_thss kind_atts) (thy, args);
   265 end;
   266 
   267 
   268 (* store_thm *)
   269 
   270 fun store_thm th_atts thy =
   271   let val (thy', [th']) = add_thmx name_single Thm.apply_attributes th_atts thy
   272   in (thy', th') end;
   273 
   274 
   275 (* smart_store_thms *)
   276 
   277 fun smart_store_thms (name, []) = error ("Cannot store empty list of theorems: " ^ quote name)
   278   | smart_store_thms (name, [thm]) = enter_thmx (Thm.sign_of_thm thm) name_single (name, thm)
   279   | smart_store_thms (name, thms) =
   280       let
   281         val merge_sg = Sign.merge_refs o apsnd (Sign.self_ref o Thm.sign_of_thm);
   282         val sg_ref = foldl merge_sg (Sign.self_ref (Thm.sign_of_thm (hd thms)), tl thms);
   283       in enter_thmx (Sign.deref sg_ref) name_multi (name, thms) end;
   284 
   285 
   286 (* forall_elim_vars (belongs to drule.ML) *)
   287 
   288 (*Replace outermost quantified variable by Var of given index.
   289     Could clash with Vars already present.*)
   290 fun forall_elim_var i th =
   291     let val {prop,sign,...} = rep_thm th
   292     in case prop of
   293           Const("all",_) $ Abs(a,T,_) =>
   294               forall_elim (cterm_of sign (Var((a,i), T)))  th
   295         | _ => raise THM("forall_elim_var", i, [th])
   296     end;
   297 
   298 (*Repeat forall_elim_var until all outer quantifiers are removed*)
   299 fun forall_elim_vars i th =
   300     forall_elim_vars i (forall_elim_var i th)
   301         handle THM _ => th;
   302 
   303 
   304 (* store axioms as theorems *)
   305 
   306 local
   307   fun get_axs thy named_axs =
   308     map (forall_elim_vars 0 o Thm.get_axiom thy o fst) named_axs;
   309 
   310   fun add_single add (thy, ((name, ax), atts)) =
   311     let
   312       val named_ax = name_single name ax;
   313       val thy' = add named_ax thy;
   314       val thm = hd (get_axs thy' named_ax);
   315     in apsnd hd (add_thms [((name, thm), atts)] thy') end;
   316 
   317   fun add_multi add (thy, ((name, axs), atts)) =
   318     let
   319       val named_axs = name_multi name axs;
   320       val thy' = add named_axs thy;
   321       val thms = get_axs thy' named_axs;
   322     in apsnd hd (add_thmss [((name, thms), atts)] thy') end;
   323 
   324   fun add_singles add args thy = foldl_map (add_single add) (thy, args);
   325   fun add_multis add args thy = foldl_map (add_multi add) (thy, args);
   326 in
   327   val add_axioms    = add_singles Theory.add_axioms;
   328   val add_axioms_i  = add_singles Theory.add_axioms_i;
   329   val add_axiomss   = add_multis Theory.add_axioms;
   330   val add_axiomss_i = add_multis Theory.add_axioms_i;
   331   val add_defs      = add_singles o Theory.add_defs;
   332   val add_defs_i    = add_singles o Theory.add_defs_i;
   333   val add_defss     = add_multis o Theory.add_defs;
   334   val add_defss_i   = add_multis o Theory.add_defs_i;
   335 end;
   336 
   337 
   338 
   339 (*** derived theory operations ***)
   340 
   341 (** theory management **)
   342 
   343 (* data kind 'Pure/theory_management' *)
   344 
   345 structure TheoryManagementDataArgs =
   346 struct
   347   val name = "Pure/theory_management";
   348   type T = {name: string, version: int};
   349 
   350   val empty = {name = "", version = 0};
   351   val copy = I;
   352   val prep_ext  = I;
   353   fun merge _ = empty;
   354   fun print _ _ = ();
   355 end;
   356 
   357 structure TheoryManagementData = TheoryDataFun(TheoryManagementDataArgs);
   358 val get_info = TheoryManagementData.get;
   359 val put_info = TheoryManagementData.put;
   360 
   361 
   362 (* get / put name *)
   363 
   364 val get_name = #name o get_info;
   365 fun put_name name = put_info {name = name, version = 0};
   366 
   367 
   368 (* control prefixing of theory name *)
   369 
   370 val global_path = Theory.root_path;
   371 
   372 fun local_path thy =
   373   thy |> Theory.root_path |> Theory.add_path (get_name thy);
   374 
   375 
   376 (* begin / end theory *)
   377 
   378 fun begin_theory name thys =
   379   Theory.prep_ext_merge thys
   380   |> put_name name
   381   |> local_path;
   382 
   383 fun end_theory thy = Theory.add_name (get_name thy) thy;
   384 
   385 fun checkpoint thy =
   386   if is_draft thy then
   387     let val {name, version} = get_info thy in
   388       thy
   389       |> Theory.add_name (name ^ ":" ^ string_of_int version)
   390       |> put_info {name = name, version = version + 1}
   391     end
   392   else thy;
   393 
   394 
   395 
   396 (** add logical types **)
   397 
   398 fun add_typedecls decls thy =
   399   let
   400     val full = Sign.full_name (Theory.sign_of thy);
   401 
   402     fun type_of (raw_name, vs, mx) =
   403       if null (duplicates vs) then (raw_name, length vs, mx)
   404       else error ("Duplicate parameters in type declaration: " ^ quote raw_name);
   405 
   406     fun arity_of (raw_name, len, mx) =
   407       (full (Syntax.type_name raw_name mx), replicate len logicS, logicS);
   408 
   409     val types = map type_of decls;
   410     val arities = map arity_of types;
   411   in
   412     thy
   413     |> Theory.add_types types
   414     |> Theory.add_arities_i arities
   415   end;
   416 
   417 
   418 
   419 (*** the ProtoPure theory ***)
   420 
   421 val dummy_patternN = "dummy_pattern";
   422 
   423 val proto_pure =
   424   Theory.pre_pure
   425   |> Library.apply [TheoremsData.init, TheoryManagementData.init]
   426   |> put_name "ProtoPure"
   427   |> global_path
   428   |> Theory.add_types
   429    [("fun", 2, NoSyn),
   430     ("prop", 0, NoSyn),
   431     ("itself", 1, NoSyn),
   432     ("dummy", 0, NoSyn)]
   433   |> Theory.add_classes_i [(logicC, [])]
   434   |> Theory.add_defsort_i logicS
   435   |> Theory.add_arities_i
   436    [("fun", [logicS, logicS], logicS),
   437     ("prop", [], logicS),
   438     ("itself", [logicS], logicS)]
   439   |> Theory.add_nonterminals Syntax.pure_nonterms
   440   |> Theory.add_syntax Syntax.pure_syntax
   441   |> Theory.add_modesyntax (Symbol.symbolsN, true) Syntax.pure_sym_syntax
   442   |> Theory.add_modesyntax (Symbol.xsymbolsN, true) Syntax.pure_xsym_syntax
   443   |> Theory.add_trfuns Syntax.pure_trfuns
   444   |> Theory.add_trfunsT Syntax.pure_trfunsT
   445   |> Theory.add_syntax
   446    [("==>", "[prop, prop] => prop", Delimfix "op ==>"),
   447     (dummy_patternN, "aprop", Delimfix "'_")]
   448   |> Theory.add_consts
   449    [("==", "['a::{}, 'a] => prop", InfixrName ("==", 2)),
   450     ("=?=", "['a::{}, 'a] => prop", InfixrName ("=?=", 2)),
   451     ("==>", "[prop, prop] => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
   452     ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
   453     ("Goal", "prop => prop", Mixfix ("GOAL _", [1000], 999)),
   454     ("TYPE", "'a itself", NoSyn),
   455     (dummy_patternN, "'a", Delimfix "'_")]
   456   |> Theory.add_modesyntax ("", false)
   457     [("Goal", "prop => prop", Mixfix ("_", [0], 0))]
   458   |> local_path
   459   |> (#1 oo (add_defs false o map Thm.no_attributes))
   460    [("flexpair_def", "(t =?= u) == (t == u::'a::{})"),
   461     ("Goal_def", "GOAL (PROP A) == PROP A")]
   462   |> (#1 o add_thmss [(("nothing", []), [])])
   463   |> end_theory;
   464 
   465 structure ProtoPure =
   466 struct
   467   val thy = proto_pure;
   468   val flexpair_def = get_axiom thy "flexpair_def";
   469   val Goal_def = get_axiom thy "Goal_def";
   470 end;
   471 
   472 
   473 end;
   474 
   475 
   476 structure BasicPureThy: BASIC_PURE_THY = PureThy;
   477 open BasicPureThy;