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