src/HOL/simpdata.ML
author paulson
Mon Oct 07 10:28:44 1996 +0200 (1996-10-07)
changeset 2056 93c093620c28
parent 2054 bf3891343aa5
child 2082 8659e3063a30
permissions -rw-r--r--
Removed commands made redundant by new one-point rules
     1 (*  Title:      HOL/simpdata.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   1991  University of Cambridge
     5 
     6 Instantiation of the generic simplifier
     7 *)
     8 
     9 section "Simplifier";
    10 
    11 open Simplifier;
    12 
    13 (*** Integration of simplifier with classical reasoner ***)
    14 
    15 (*Add a simpset to a classical set!*)
    16 infix 4 addss;
    17 fun cs addss ss = cs addbefore asm_full_simp_tac ss 1;
    18 
    19 fun Addss ss = (claset := !claset addbefore asm_full_simp_tac ss 1);
    20 
    21 (*Designed to be idempotent, except if best_tac instantiates variables
    22   in some of the subgoals*)
    23 fun auto_tac (cs,ss) = 
    24     ALLGOALS (asm_full_simp_tac ss) THEN
    25     REPEAT (safe_tac cs THEN ALLGOALS (asm_full_simp_tac ss)) THEN
    26     REPEAT (FIRSTGOAL (best_tac (cs addss ss))) THEN
    27     prune_params_tac;
    28 
    29 fun Auto_tac() = auto_tac (!claset, !simpset);
    30 
    31 fun auto() = by (Auto_tac());
    32 
    33 
    34 (*** Addition of rules to simpsets and clasets simultaneously ***)
    35 
    36 (*Takes UNCONDITIONAL theorems of the form A<->B to 
    37         the Safe Intr     rule B==>A and 
    38         the Safe Destruct rule A==>B.
    39   Also ~A goes to the Safe Elim rule A ==> ?R
    40   Failing other cases, A is added as a Safe Intr rule*)
    41 local
    42   val iff_const = HOLogic.eq_const HOLogic.boolT;
    43 
    44   fun addIff th = 
    45       (case HOLogic.dest_Trueprop (#prop(rep_thm th)) of
    46                 (Const("not",_) $ A) =>
    47                     AddSEs [zero_var_indexes (th RS notE)]
    48               | (con $ _ $ _) =>
    49                     if con=iff_const
    50                     then (AddSIs [zero_var_indexes (th RS iffD2)];  
    51                           AddSDs [zero_var_indexes (th RS iffD1)])
    52                     else  AddSIs [th]
    53               | _ => AddSIs [th];
    54        Addsimps [th])
    55       handle _ => error ("AddIffs: theorem must be unconditional\n" ^ 
    56                          string_of_thm th)
    57 
    58   fun delIff th = 
    59       (case HOLogic.dest_Trueprop (#prop(rep_thm th)) of
    60                 (Const("not",_) $ A) =>
    61                     Delrules [zero_var_indexes (th RS notE)]
    62               | (con $ _ $ _) =>
    63                     if con=iff_const
    64                     then Delrules [zero_var_indexes (th RS iffD2),
    65                                    zero_var_indexes (th RS iffD1)]
    66                     else Delrules [th]
    67               | _ => Delrules [th];
    68        Delsimps [th])
    69       handle _ => warning("DelIffs: ignoring conditional theorem\n" ^ 
    70                           string_of_thm th)
    71 in
    72 val AddIffs = seq addIff
    73 val DelIffs = seq delIff
    74 end;
    75 
    76 
    77 local
    78 
    79   fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
    80 
    81   val P_imp_P_iff_True = prover "P --> (P = True)" RS mp;
    82   val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
    83 
    84   val not_P_imp_P_iff_F = prover "~P --> (P = False)" RS mp;
    85   val not_P_imp_P_eq_False = not_P_imp_P_iff_F RS eq_reflection;
    86 
    87   fun atomize pairs =
    88     let fun atoms th =
    89           (case concl_of th of
    90              Const("Trueprop",_) $ p =>
    91                (case head_of p of
    92                   Const(a,_) =>
    93                     (case assoc(pairs,a) of
    94                        Some(rls) => flat (map atoms ([th] RL rls))
    95                      | None => [th])
    96                 | _ => [th])
    97            | _ => [th])
    98     in atoms end;
    99 
   100   fun mk_meta_eq r = case concl_of r of
   101           Const("==",_)$_$_ => r
   102       |   _$(Const("op =",_)$_$_) => r RS eq_reflection
   103       |   _$(Const("not",_)$_) => r RS not_P_imp_P_eq_False
   104       |   _ => r RS P_imp_P_eq_True;
   105   (* last 2 lines requires all formulae to be of the from Trueprop(.) *)
   106 
   107   fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th;
   108 
   109   val simp_thms = map prover
   110    [ "(x=x) = True",
   111      "(~True) = False", "(~False) = True", "(~ ~ P) = P",
   112      "(~P) ~= P", "P ~= (~P)", "(P ~= Q) = (P = (~Q))",
   113      "(True=P) = P", "(P=True) = P",
   114      "(True --> P) = P", "(False --> P) = True", 
   115      "(P --> True) = True", "(P --> P) = True",
   116      "(P --> False) = (~P)", "(P --> ~P) = (~P)",
   117      "(P & True) = P", "(True & P) = P", 
   118      "(P & False) = False", "(False & P) = False", "(P & P) = P",
   119      "(P | True) = True", "(True | P) = True", 
   120      "(P | False) = P", "(False | P) = P", "(P | P) = P",
   121      "((~P) = (~Q)) = (P=Q)",
   122      "(!x.P) = P", "(? x.P) = P", "? x. x=t", 
   123      "(? x. x=t & P(x)) = P(t)", "(? x. t=x & P(x)) = P(t)", 
   124      "(! x. x=t --> P(x)) = P(t)", "(! x. t=x --> P(x)) = P(t)" ];
   125 
   126 in
   127 
   128 val meta_eq_to_obj_eq = prove_goal HOL.thy "x==y ==> x=y"
   129   (fn [prem] => [rewtac prem, rtac refl 1]);
   130 
   131 val eq_sym_conv = prover "(x=y) = (y=x)";
   132 
   133 val conj_assoc = prover "((P&Q)&R) = (P&(Q&R))";
   134 
   135 val disj_assoc = prover "((P|Q)|R) = (P|(Q|R))";
   136 
   137 val imp_disj   = prover "(P|Q --> R) = ((P-->R)&(Q-->R))";
   138 
   139 (*Avoids duplication of subgoals after expand_if, when the true and false 
   140   cases boil down to the same thing.*) 
   141 val cases_simp = prover "((P --> Q) & (~P --> Q)) = Q";
   142 
   143 val if_True = prove_goalw HOL.thy [if_def] "(if True then x else y) = x"
   144  (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
   145 
   146 val if_False = prove_goalw HOL.thy [if_def] "(if False then x else y) = y"
   147  (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
   148 
   149 val if_P = prove_goal HOL.thy "P ==> (if P then x else y) = x"
   150  (fn [prem] => [ stac (prem RS eqTrueI) 1, rtac if_True 1 ]);
   151 
   152 val if_not_P = prove_goal HOL.thy "~P ==> (if P then x else y) = y"
   153  (fn [prem] => [ stac (prem RS not_P_imp_P_iff_F) 1, rtac if_False 1 ]);
   154 
   155 val expand_if = prove_goal HOL.thy
   156     "P(if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
   157  (fn _=> [ (res_inst_tac [("Q","Q")] (excluded_middle RS disjE) 1),
   158          stac if_P 2,
   159          stac if_not_P 1,
   160          REPEAT(fast_tac HOL_cs 1) ]);
   161 
   162 val if_bool_eq = prove_goal HOL.thy
   163                    "(if P then Q else R) = ((P-->Q) & (~P-->R))"
   164                    (fn _ => [rtac expand_if 1]);
   165 
   166 (*Add congruence rules for = (instead of ==) *)
   167 infix 4 addcongs;
   168 fun ss addcongs congs = ss addeqcongs (congs RL [eq_reflection]);
   169 
   170 fun Addcongs congs = (simpset := !simpset addcongs congs);
   171 
   172 val mksimps_pairs =
   173   [("op -->", [mp]), ("op &", [conjunct1,conjunct2]),
   174    ("All", [spec]), ("True", []), ("False", []),
   175    ("If", [if_bool_eq RS iffD1])];
   176 
   177 fun mksimps pairs = map mk_meta_eq o atomize pairs o gen_all;
   178 
   179 val imp_cong = impI RSN
   180     (2, prove_goal HOL.thy "(P=P')--> (P'--> (Q=Q'))--> ((P-->Q) = (P'-->Q'))"
   181         (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
   182 
   183 val o_apply = prove_goalw HOL.thy [o_def] "(f o g)(x) = f(g(x))"
   184  (fn _ => [rtac refl 1]);
   185 
   186 (*Miniscoping: pushing in existential quantifiers*)
   187 val ex_simps = map prover 
   188                 ["(EX x. P x & Q)   = ((EX x.P x) & Q)",
   189                  "(EX x. P & Q x)   = (P & (EX x.Q x))",
   190                  "(EX x. P x | Q)   = ((EX x.P x) | Q)",
   191                  "(EX x. P | Q x)   = (P | (EX x.Q x))",
   192                  "(EX x. P x --> Q) = ((ALL x.P x) --> Q)",
   193                  "(EX x. P --> Q x) = (P --> (EX x.Q x))"];
   194 
   195 (*Miniscoping: pushing in universal quantifiers*)
   196 val all_simps = map prover
   197                 ["(ALL x. P x & Q)   = ((ALL x.P x) & Q)",
   198                  "(ALL x. P & Q x)   = (P & (ALL x.Q x))",
   199                  "(ALL x. P x | Q)   = ((ALL x.P x) | Q)",
   200                  "(ALL x. P | Q x)   = (P | (ALL x.Q x))",
   201                  "(ALL x. P x --> Q) = ((EX x.P x) --> Q)",
   202                  "(ALL x. P --> Q x) = (P --> (ALL x.Q x))"];
   203 
   204 val HOL_ss = empty_ss
   205       setmksimps (mksimps mksimps_pairs)
   206       setsolver (fn prems => resolve_tac (TrueI::refl::prems) ORELSE' atac
   207                              ORELSE' etac FalseE)
   208       setsubgoaler asm_simp_tac
   209       addsimps ([if_True, if_False, o_apply, imp_disj, conj_assoc, disj_assoc,
   210                  cases_simp]
   211         @ ex_simps @ all_simps @ simp_thms)
   212       addcongs [imp_cong];
   213 
   214 
   215 (*In general it seems wrong to add distributive laws by default: they
   216   might cause exponential blow-up.  But imp_disj has been in for a while
   217   and cannot be removed without affecting existing proofs.  Moreover, 
   218   rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
   219   grounds that it allows simplification of R in the two cases.*)
   220 
   221 
   222 local val mktac = mk_case_split_tac (meta_eq_to_obj_eq RS iffD2)
   223 in
   224 fun split_tac splits = mktac (map mk_meta_eq splits)
   225 end;
   226 
   227 local val mktac = mk_case_split_inside_tac (meta_eq_to_obj_eq RS iffD2)
   228 in
   229 fun split_inside_tac splits = mktac (map mk_meta_eq splits)
   230 end;
   231 
   232 
   233 (* elimination of existential quantifiers in assumptions *)
   234 
   235 val ex_all_equiv =
   236   let val lemma1 = prove_goal HOL.thy
   237         "(? x. P(x) ==> PROP Q) ==> (!!x. P(x) ==> PROP Q)"
   238         (fn prems => [resolve_tac prems 1, etac exI 1]);
   239       val lemma2 = prove_goalw HOL.thy [Ex_def]
   240         "(!!x. P(x) ==> PROP Q) ==> (? x. P(x) ==> PROP Q)"
   241         (fn prems => [REPEAT(resolve_tac prems 1)])
   242   in equal_intr lemma1 lemma2 end;
   243 
   244 (* '&' congruence rule: not included by default!
   245    May slow rewrite proofs down by as much as 50% *)
   246 
   247 val conj_cong = 
   248   let val th = prove_goal HOL.thy 
   249                 "(P=P')--> (P'--> (Q=Q'))--> ((P&Q) = (P'&Q'))"
   250                 (fn _=> [fast_tac HOL_cs 1])
   251   in  standard (impI RSN (2, th RS mp RS mp))  end;
   252 
   253 val rev_conj_cong =
   254   let val th = prove_goal HOL.thy 
   255                 "(Q=Q')--> (Q'--> (P=P'))--> ((P&Q) = (P'&Q'))"
   256                 (fn _=> [fast_tac HOL_cs 1])
   257   in  standard (impI RSN (2, th RS mp RS mp))  end;
   258 
   259 (* '|' congruence rule: not included by default! *)
   260 
   261 val disj_cong = 
   262   let val th = prove_goal HOL.thy 
   263                 "(P=P')--> (~P'--> (Q=Q'))--> ((P|Q) = (P'|Q'))"
   264                 (fn _=> [fast_tac HOL_cs 1])
   265   in  standard (impI RSN (2, th RS mp RS mp))  end;
   266 
   267 (** 'if' congruence rules: neither included by default! *)
   268 
   269 (*Simplifies x assuming c and y assuming ~c*)
   270 val if_cong = prove_goal HOL.thy
   271   "[| b=c; c ==> x=u; ~c ==> y=v |] ==>\
   272 \  (if b then x else y) = (if c then u else v)"
   273   (fn rew::prems =>
   274    [stac rew 1, stac expand_if 1, stac expand_if 1,
   275     fast_tac (HOL_cs addDs prems) 1]);
   276 
   277 (*Prevents simplification of x and y: much faster*)
   278 val if_weak_cong = prove_goal HOL.thy
   279   "b=c ==> (if b then x else y) = (if c then x else y)"
   280   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   281 
   282 (*Prevents simplification of t: much faster*)
   283 val let_weak_cong = prove_goal HOL.thy
   284   "a = b ==> (let x=a in t(x)) = (let x=b in t(x))"
   285   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   286 
   287 end;
   288 
   289 fun prove nm thm  = qed_goal nm HOL.thy thm (fn _ => [fast_tac HOL_cs 1]);
   290 
   291 prove "conj_commute" "(P&Q) = (Q&P)";
   292 prove "conj_left_commute" "(P&(Q&R)) = (Q&(P&R))";
   293 val conj_comms = [conj_commute, conj_left_commute];
   294 
   295 prove "disj_commute" "(P|Q) = (Q|P)";
   296 prove "disj_left_commute" "(P|(Q|R)) = (Q|(P|R))";
   297 val disj_comms = [disj_commute, disj_left_commute];
   298 
   299 prove "conj_disj_distribL" "(P&(Q|R)) = (P&Q | P&R)";
   300 prove "conj_disj_distribR" "((P|Q)&R) = (P&R | Q&R)";
   301 
   302 prove "disj_conj_distribL" "(P|(Q&R)) = ((P|Q) & (P|R))";
   303 prove "disj_conj_distribR" "((P&Q)|R) = ((P|R) & (Q|R))";
   304 
   305 prove "imp_conj_distrib" "(P --> (Q&R)) = ((P-->Q) & (P-->R))";
   306 prove "imp_conj"         "((P&Q)-->R)   = (P --> (Q --> R))";
   307 
   308 prove "de_Morgan_disj" "(~(P | Q)) = (~P & ~Q)";
   309 prove "de_Morgan_conj" "(~(P & Q)) = (~P | ~Q)";
   310 prove "not_iff" "(P~=Q) = (P = (~Q))";
   311 
   312 prove "not_all" "(~ (! x.P(x))) = (? x.~P(x))";
   313 prove "imp_all" "((! x. P x) --> Q) = (? x. P x --> Q)";
   314 prove "not_ex"  "(~ (? x.P(x))) = (! x.~P(x))";
   315 prove "imp_ex" "((? x. P x) --> Q) = (! x. P x --> Q)";
   316 
   317 prove "ex_disj_distrib" "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))";
   318 prove "all_conj_distrib" "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))";
   319 
   320 
   321 qed_goal "if_cancel" HOL.thy "(if c then x else x) = x"
   322   (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
   323 
   324 qed_goal "if_distrib" HOL.thy
   325   "f(if c then x else y) = (if c then f x else f y)" 
   326   (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
   327 
   328 qed_goalw "o_assoc" HOL.thy [o_def] "f o (g o h) = (f o g o h)"
   329   (fn _=>[rtac ext 1, rtac refl 1]);
   330 
   331 
   332 
   333 
   334 (*** Install simpsets and datatypes in theory structure ***)
   335 
   336 simpset := HOL_ss;
   337 
   338 exception SS_DATA of simpset;
   339 
   340 let fun merge [] = SS_DATA empty_ss
   341       | merge ss = let val ss = map (fn SS_DATA x => x) ss;
   342                    in SS_DATA (foldl merge_ss (hd ss, tl ss)) end;
   343 
   344     fun put (SS_DATA ss) = simpset := ss;
   345 
   346     fun get () = SS_DATA (!simpset);
   347 in add_thydata "HOL"
   348      ("simpset", ThyMethods {merge = merge, put = put, get = get})
   349 end;
   350 
   351 type dtype_info = {case_const:term, case_rewrites:thm list,
   352                    constructors:term list, nchotomy:thm, case_cong:thm};
   353 
   354 exception DT_DATA of (string * dtype_info) list;
   355 val datatypes = ref [] : (string * dtype_info) list ref;
   356 
   357 let fun merge [] = DT_DATA []
   358       | merge ds =
   359           let val ds = map (fn DT_DATA x => x) ds;
   360           in DT_DATA (foldl (gen_union eq_fst) (hd ds, tl ds)) end;
   361 
   362     fun put (DT_DATA ds) = datatypes := ds;
   363 
   364     fun get () = DT_DATA (!datatypes);
   365 in add_thydata "HOL"
   366      ("datatypes", ThyMethods {merge = merge, put = put, get = get})
   367 end;
   368 
   369 
   370 add_thy_reader_file "thy_data.ML";