src/HOL/simpdata.ML
author schirmer
Wed Jul 05 11:32:38 2006 +0200 (2006-07-05)
changeset 20014 729a45534001
parent 19472 896eb8056e97
child 20046 9c8909fc5865
permissions -rw-r--r--
fixed let-simproc
----------------------------------------------------------------------
     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 for HOL.
     7 *)
     8 
     9 (* legacy ML bindings *)
    10 
    11 val Eq_FalseI = thm "Eq_FalseI";
    12 val Eq_TrueI = thm "Eq_TrueI";
    13 val all_conj_distrib = thm "all_conj_distrib";
    14 val all_simps = thms "all_simps";
    15 val cases_simp = thm "cases_simp";
    16 val conj_assoc = thm "conj_assoc";
    17 val conj_comms = thms "conj_comms";
    18 val conj_commute = thm "conj_commute";
    19 val conj_cong = thm "conj_cong";
    20 val conj_disj_distribL = thm "conj_disj_distribL";
    21 val conj_disj_distribR = thm "conj_disj_distribR";
    22 val conj_left_commute = thm "conj_left_commute";
    23 val de_Morgan_conj = thm "de_Morgan_conj";
    24 val de_Morgan_disj = thm "de_Morgan_disj";
    25 val disj_assoc = thm "disj_assoc";
    26 val disj_comms = thms "disj_comms";
    27 val disj_commute = thm "disj_commute";
    28 val disj_cong = thm "disj_cong";
    29 val disj_conj_distribL = thm "disj_conj_distribL";
    30 val disj_conj_distribR = thm "disj_conj_distribR";
    31 val disj_left_commute = thm "disj_left_commute";
    32 val disj_not1 = thm "disj_not1";
    33 val disj_not2 = thm "disj_not2";
    34 val eq_ac = thms "eq_ac";
    35 val eq_assoc = thm "eq_assoc";
    36 val eq_commute = thm "eq_commute";
    37 val eq_left_commute = thm "eq_left_commute";
    38 val eq_sym_conv = thm "eq_sym_conv";
    39 val eta_contract_eq = thm "eta_contract_eq";
    40 val ex_disj_distrib = thm "ex_disj_distrib";
    41 val ex_simps = thms "ex_simps";
    42 val if_False = thm "if_False";
    43 val if_P = thm "if_P";
    44 val if_True = thm "if_True";
    45 val if_bool_eq_conj = thm "if_bool_eq_conj";
    46 val if_bool_eq_disj = thm "if_bool_eq_disj";
    47 val if_cancel = thm "if_cancel";
    48 val if_def2 = thm "if_def2";
    49 val if_eq_cancel = thm "if_eq_cancel";
    50 val if_not_P = thm "if_not_P";
    51 val if_splits = thms "if_splits";
    52 val iff_conv_conj_imp = thm "iff_conv_conj_imp";
    53 val imp_all = thm "imp_all";
    54 val imp_cong = thm "imp_cong";
    55 val imp_conjL = thm "imp_conjL";
    56 val imp_conjR = thm "imp_conjR";
    57 val imp_conv_disj = thm "imp_conv_disj";
    58 val imp_disj1 = thm "imp_disj1";
    59 val imp_disj2 = thm "imp_disj2";
    60 val imp_disjL = thm "imp_disjL";
    61 val imp_disj_not1 = thm "imp_disj_not1";
    62 val imp_disj_not2 = thm "imp_disj_not2";
    63 val imp_ex = thm "imp_ex";
    64 val meta_eq_to_obj_eq = thm "meta_eq_to_obj_eq";
    65 val neq_commute = thm "neq_commute";
    66 val not_all = thm "not_all";
    67 val not_ex = thm "not_ex";
    68 val not_iff = thm "not_iff";
    69 val not_imp = thm "not_imp";
    70 val not_not = thm "not_not";
    71 val rev_conj_cong = thm "rev_conj_cong";
    72 val simp_impliesE = thm "simp_impliesI";
    73 val simp_impliesI = thm "simp_impliesI";
    74 val simp_implies_cong = thm "simp_implies_cong";
    75 val simp_implies_def = thm "simp_implies_def";
    76 val simp_thms = thms "simp_thms";
    77 val split_if = thm "split_if";
    78 val split_if_asm = thm "split_if_asm";
    79 val atomize_not = thm"atomize_not";
    80 
    81 local
    82 val uncurry = prove_goal (the_context()) "P --> Q --> R ==> P & Q --> R"
    83               (fn prems => [cut_facts_tac prems 1, Blast_tac 1]);
    84 
    85 val iff_allI = allI RS
    86     prove_goal (the_context()) "!x. P x = Q x ==> (!x. P x) = (!x. Q x)"
    87                (fn prems => [cut_facts_tac prems 1, Blast_tac 1])
    88 val iff_exI = allI RS
    89     prove_goal (the_context()) "!x. P x = Q x ==> (? x. P x) = (? x. Q x)"
    90                (fn prems => [cut_facts_tac prems 1, Blast_tac 1])
    91 
    92 val all_comm = prove_goal (the_context()) "(!x y. P x y) = (!y x. P x y)"
    93                (fn _ => [Blast_tac 1])
    94 val ex_comm = prove_goal (the_context()) "(? x y. P x y) = (? y x. P x y)"
    95                (fn _ => [Blast_tac 1])
    96 in
    97 
    98 (*** make simplification procedures for quantifier elimination ***)
    99 
   100 structure Quantifier1 = Quantifier1Fun
   101 (struct
   102   (*abstract syntax*)
   103   fun dest_eq((c as Const("op =",_)) $ s $ t) = SOME(c,s,t)
   104     | dest_eq _ = NONE;
   105   fun dest_conj((c as Const("op &",_)) $ s $ t) = SOME(c,s,t)
   106     | dest_conj _ = NONE;
   107   fun dest_imp((c as Const("op -->",_)) $ s $ t) = SOME(c,s,t)
   108     | dest_imp _ = NONE;
   109   val conj = HOLogic.conj
   110   val imp  = HOLogic.imp
   111   (*rules*)
   112   val iff_reflection = eq_reflection
   113   val iffI = iffI
   114   val iff_trans = trans
   115   val conjI= conjI
   116   val conjE= conjE
   117   val impI = impI
   118   val mp   = mp
   119   val uncurry = uncurry
   120   val exI  = exI
   121   val exE  = exE
   122   val iff_allI = iff_allI
   123   val iff_exI = iff_exI
   124   val all_comm = all_comm
   125   val ex_comm = ex_comm
   126 end);
   127 
   128 end;
   129 
   130 val defEX_regroup =
   131   Simplifier.simproc (Theory.sign_of (the_context ()))
   132     "defined EX" ["EX x. P x"] Quantifier1.rearrange_ex;
   133 
   134 val defALL_regroup =
   135   Simplifier.simproc (Theory.sign_of (the_context ()))
   136     "defined ALL" ["ALL x. P x"] Quantifier1.rearrange_all;
   137 
   138 
   139 (*** simproc for proving "(y = x) == False" from prmise "~(x = y)" ***)
   140 
   141 val use_neq_simproc = ref true;
   142 
   143 local
   144 
   145 val neq_to_EQ_False = thm "not_sym" RS Eq_FalseI;
   146 
   147 fun neq_prover sg ss (eq $ lhs $ rhs) =
   148 let
   149   fun test thm = (case #prop(rep_thm thm) of
   150                     _ $ (Not $ (eq' $ l' $ r')) =>
   151                       Not = HOLogic.Not andalso eq' = eq andalso
   152                       r' aconv lhs andalso l' aconv rhs
   153                   | _ => false)
   154 in
   155   if !use_neq_simproc then
   156     case Library.find_first test (prems_of_ss ss) of NONE => NONE
   157     | SOME thm => SOME (thm RS neq_to_EQ_False)
   158   else NONE
   159 end
   160 
   161 in
   162 
   163 val neq_simproc =
   164   Simplifier.simproc (the_context ()) "neq_simproc" ["x = y"] neq_prover;
   165 
   166 end;
   167 
   168 
   169 
   170 
   171 (*** Simproc for Let ***)
   172 
   173 val use_let_simproc = ref true;
   174 
   175 local
   176 val Let_folded = thm "Let_folded";
   177 val Let_unfold = thm "Let_unfold";
   178 
   179 val (f_Let_unfold,x_Let_unfold) = 
   180       let val [(_$(f$x)$_)] = prems_of Let_unfold 
   181       in (cterm_of (sign_of (the_context ())) f,cterm_of (sign_of (the_context ())) x) end
   182 val (f_Let_folded,x_Let_folded) = 
   183       let val [(_$(f$x)$_)] = prems_of Let_folded 
   184       in (cterm_of (sign_of (the_context ())) f, cterm_of (sign_of (the_context ())) x) end;
   185 val g_Let_folded = 
   186       let val [(_$_$(g$_))] = prems_of Let_folded in cterm_of (sign_of (the_context ())) g end;
   187 in
   188 val let_simproc =
   189   Simplifier.simproc (Theory.sign_of (the_context ())) "let_simp" ["Let x f"] 
   190    (fn sg => fn ss => fn t =>
   191 (* FIXME: very slow (replace t by t' in case below)
   192      let val ctxt = Simplifier.the_context ss;
   193          val ([t'],ctxt') = Variable.import_terms false [t] ctxt;
   194      in Option.map (hd o Variable.export ctxt' ctxt o single)
   195 *)
   196       (case t of (Const ("Let",_)$x$f) => (* x and f are already in normal form *)
   197          if not (!use_let_simproc) then NONE
   198          else if is_Free x orelse is_Bound x orelse is_Const x 
   199          then SOME Let_def  
   200          else
   201           let
   202              val n = case f of (Abs (x,_,_)) => x | _ => "x";
   203              val cx = cterm_of sg x;
   204              val {T=xT,...} = rep_cterm cx;
   205              val cf = cterm_of sg f;
   206              val fx_g = Simplifier.rewrite ss (Thm.capply cf cx);
   207              val (_$_$g) = prop_of fx_g;
   208              val g' = abstract_over (x,g);
   209            in (if (g aconv g') 
   210                then
   211                   let
   212                     val rl = cterm_instantiate [(f_Let_unfold,cf),(x_Let_unfold,cx)] Let_unfold;
   213                   in SOME (rl OF [fx_g]) end 
   214                else if Term.betapply (f,x) aconv g then NONE (*avoid identity conversion*)
   215                else let 
   216                      val abs_g'= Abs (n,xT,g');
   217                      val g'x = abs_g'$x;
   218                      val g_g'x = symmetric (beta_conversion false (cterm_of sg g'x));
   219                      val rl = cterm_instantiate
   220                                [(f_Let_folded,cterm_of sg f),(x_Let_folded,cx),
   221                                 (g_Let_folded,cterm_of sg abs_g')]
   222                                Let_folded; 
   223                    in SOME (rl OF [transitive fx_g g_g'x]) 
   224                    end)
   225            end
   226         | _ => NONE)
   227  (*  FIXME: continue
   228   end*)
   229  )
   230 end
   231 
   232 (*** Case splitting ***)
   233 
   234 (*Make meta-equalities.  The operator below is Trueprop*)
   235 
   236 fun mk_meta_eq r = r RS eq_reflection;
   237 fun safe_mk_meta_eq r = mk_meta_eq r handle Thm.THM _ => r;
   238 
   239 fun mk_eq th = case concl_of th of
   240         Const("==",_)$_$_       => th
   241     |   _$(Const("op =",_)$_$_) => mk_meta_eq th
   242     |   _$(Const("Not",_)$_)    => th RS Eq_FalseI
   243     |   _                       => th RS Eq_TrueI;
   244 (* Expects Trueprop(.) if not == *)
   245 
   246 fun mk_eq_True r =
   247   SOME (r RS meta_eq_to_obj_eq RS Eq_TrueI) handle Thm.THM _ => NONE;
   248 
   249 (* Produce theorems of the form
   250   (P1 =simp=> ... =simp=> Pn => x == y) ==> (P1 =simp=> ... =simp=> Pn => x = y)
   251 *)
   252 fun lift_meta_eq_to_obj_eq i st =
   253   let
   254     val {sign, ...} = rep_thm st;
   255     fun count_imp (Const ("HOL.simp_implies", _) $ P $ Q) = 1 + count_imp Q
   256       | count_imp _ = 0;
   257     val j = count_imp (Logic.strip_assums_concl (List.nth (prems_of st, i - 1)))
   258   in if j = 0 then meta_eq_to_obj_eq
   259     else
   260       let
   261         val Ps = map (fn k => Free ("P" ^ string_of_int k, propT)) (1 upto j);
   262         fun mk_simp_implies Q = foldr (fn (R, S) =>
   263           Const ("HOL.simp_implies", propT --> propT --> propT) $ R $ S) Q Ps
   264         val aT = TFree ("'a", HOLogic.typeS);
   265         val x = Free ("x", aT);
   266         val y = Free ("y", aT)
   267       in standard (Goal.prove (Thm.theory_of_thm st) []
   268         [mk_simp_implies (Logic.mk_equals (x, y))]
   269         (mk_simp_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (x, y))))
   270         (fn prems => EVERY
   271          [rewrite_goals_tac [simp_implies_def],
   272           REPEAT (ares_tac (meta_eq_to_obj_eq :: map (rewrite_rule [simp_implies_def]) prems) 1)]))
   273       end
   274   end;
   275 
   276 (*Congruence rules for = (instead of ==)*)
   277 fun mk_meta_cong rl = zero_var_indexes
   278   (let val rl' = Seq.hd (TRYALL (fn i => fn st =>
   279      rtac (lift_meta_eq_to_obj_eq i st) i st) rl)
   280    in mk_meta_eq rl' handle THM _ =>
   281      if Logic.is_equals (concl_of rl') then rl'
   282      else error "Conclusion of congruence rules must be =-equality"
   283    end);
   284 
   285 (* Elimination of True from asumptions: *)
   286 
   287 local fun rd s = read_cterm (sign_of (the_context())) (s, propT);
   288 in val True_implies_equals = standard' (equal_intr
   289   (implies_intr_hyps (implies_elim (assume (rd "True ==> PROP P")) TrueI))
   290   (implies_intr_hyps (implies_intr (rd "True") (assume (rd "PROP P")))));
   291 end;
   292 
   293 
   294 structure SplitterData =
   295   struct
   296   structure Simplifier = Simplifier
   297   val mk_eq          = mk_eq
   298   val meta_eq_to_iff = meta_eq_to_obj_eq
   299   val iffD           = iffD2
   300   val disjE          = disjE
   301   val conjE          = conjE
   302   val exE            = exE
   303   val contrapos      = contrapos_nn
   304   val contrapos2     = contrapos_pp
   305   val notnotD        = notnotD
   306   end;
   307 
   308 structure Splitter = SplitterFun(SplitterData);
   309 
   310 val split_tac        = Splitter.split_tac;
   311 val split_inside_tac = Splitter.split_inside_tac;
   312 val split_asm_tac    = Splitter.split_asm_tac;
   313 val op addsplits     = Splitter.addsplits;
   314 val op delsplits     = Splitter.delsplits;
   315 val Addsplits        = Splitter.Addsplits;
   316 val Delsplits        = Splitter.Delsplits;
   317 
   318 val mksimps_pairs =
   319   [("op -->", [mp]), ("op &", [conjunct1,conjunct2]),
   320    ("All", [spec]), ("True", []), ("False", []),
   321    ("HOL.If", [if_bool_eq_conj RS iffD1])];
   322 
   323 (*
   324 val mk_atomize:      (string * thm list) list -> thm -> thm list
   325 looks too specific to move it somewhere else
   326 *)
   327 fun mk_atomize pairs =
   328   let fun atoms th =
   329         (case concl_of th of
   330            Const("Trueprop",_) $ p =>
   331              (case head_of p of
   332                 Const(a,_) =>
   333                   (case AList.lookup (op =) pairs a of
   334                      SOME(rls) => List.concat (map atoms ([th] RL rls))
   335                    | NONE => [th])
   336               | _ => [th])
   337          | _ => [th])
   338   in atoms end;
   339 
   340 fun mksimps pairs =
   341   (List.mapPartial (try mk_eq) o mk_atomize pairs o gen_all);
   342 
   343 fun unsafe_solver_tac prems =
   344   (fn i => REPEAT_DETERM (match_tac [simp_impliesI] i)) THEN'
   345   FIRST'[resolve_tac(reflexive_thm::TrueI::refl::prems), atac, etac FalseE];
   346 val unsafe_solver = mk_solver "HOL unsafe" unsafe_solver_tac;
   347 
   348 (*No premature instantiation of variables during simplification*)
   349 fun safe_solver_tac prems =
   350   (fn i => REPEAT_DETERM (match_tac [simp_impliesI] i)) THEN'
   351   FIRST'[match_tac(reflexive_thm::TrueI::refl::prems),
   352          eq_assume_tac, ematch_tac [FalseE]];
   353 val safe_solver = mk_solver "HOL safe" safe_solver_tac;
   354 
   355 val HOL_basic_ss =
   356   Simplifier.theory_context (the_context ()) empty_ss
   357     setsubgoaler asm_simp_tac
   358     setSSolver safe_solver
   359     setSolver unsafe_solver
   360     setmksimps (mksimps mksimps_pairs)
   361     setmkeqTrue mk_eq_True
   362     setmkcong mk_meta_cong;
   363 
   364 fun unfold_tac ths =
   365   let val ss0 = Simplifier.clear_ss HOL_basic_ss addsimps ths
   366   in fn ss => ALLGOALS (full_simp_tac (Simplifier.inherit_context ss ss0)) end;
   367 
   368 (*In general it seems wrong to add distributive laws by default: they
   369   might cause exponential blow-up.  But imp_disjL has been in for a while
   370   and cannot be removed without affecting existing proofs.  Moreover,
   371   rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
   372   grounds that it allows simplification of R in the two cases.*)
   373 
   374 val HOL_ss =
   375     HOL_basic_ss addsimps
   376      ([triv_forall_equality, (* prunes params *)
   377        True_implies_equals, (* prune asms `True' *)
   378        if_True, if_False, if_cancel, if_eq_cancel,
   379        imp_disjL, conj_assoc, disj_assoc,
   380        de_Morgan_conj, de_Morgan_disj, imp_disj1, imp_disj2, not_imp,
   381        disj_not1, not_all, not_ex, cases_simp,
   382        thm "the_eq_trivial", the_sym_eq_trivial]
   383      @ ex_simps @ all_simps @ simp_thms)
   384      addsimprocs [defALL_regroup,defEX_regroup,neq_simproc,let_simproc]
   385      addcongs [imp_cong, simp_implies_cong]
   386      addsplits [split_if];
   387 
   388 fun hol_simplify rews = Simplifier.full_simplify (HOL_basic_ss addsimps rews);
   389 
   390 
   391 (*Simplifies x assuming c and y assuming ~c*)
   392 val prems = Goalw [if_def]
   393   "[| b=c; c ==> x=u; ~c ==> y=v |] ==> \
   394 \  (if b then x else y) = (if c then u else v)";
   395 by (asm_simp_tac (HOL_ss addsimps prems) 1);
   396 qed "if_cong";
   397 
   398 (*Prevents simplification of x and y: faster and allows the execution
   399   of functional programs. NOW THE DEFAULT.*)
   400 Goal "b=c ==> (if b then x else y) = (if c then x else y)";
   401 by (etac arg_cong 1);
   402 qed "if_weak_cong";
   403 
   404 (*Prevents simplification of t: much faster*)
   405 Goal "a = b ==> (let x=a in t(x)) = (let x=b in t(x))";
   406 by (etac arg_cong 1);
   407 qed "let_weak_cong";
   408 
   409 (*To tidy up the result of a simproc.  Only the RHS will be simplified.*)
   410 Goal "u = u' ==> (t==u) == (t==u')";
   411 by (asm_simp_tac HOL_ss 1);
   412 qed "eq_cong2";
   413 
   414 Goal "f(if c then x else y) = (if c then f x else f y)";
   415 by (simp_tac (HOL_ss setloop (split_tac [split_if])) 1);
   416 qed "if_distrib";
   417 
   418 (*For expand_case_tac*)
   419 val prems = Goal "[| P ==> Q(True); ~P ==> Q(False) |] ==> Q(P)";
   420 by (case_tac "P" 1);
   421 by (ALLGOALS (asm_simp_tac (HOL_ss addsimps prems)));
   422 qed "expand_case";
   423 
   424 (*This lemma restricts the effect of the rewrite rule u=v to the left-hand
   425   side of an equality.  Used in {Integ,Real}/simproc.ML*)
   426 Goal "x=y ==> (x=z) = (y=z)";
   427 by (asm_simp_tac HOL_ss 1);
   428 qed "restrict_to_left";
   429 
   430 (* default simpset *)
   431 val simpsetup =
   432   (fn thy => (change_simpset_of thy (fn _ => HOL_ss addcongs [if_weak_cong]); thy));
   433 
   434 
   435 (*** integration of simplifier with classical reasoner ***)
   436 
   437 structure Clasimp = ClasimpFun
   438  (structure Simplifier = Simplifier and Splitter = Splitter
   439   and Classical  = Classical and Blast = Blast
   440   val iffD1 = iffD1 val iffD2 = iffD2 val notE = notE);
   441 open Clasimp;
   442 
   443 val HOL_css = (HOL_cs, HOL_ss);
   444 
   445 
   446 
   447 (*** A general refutation procedure ***)
   448 
   449 (* Parameters:
   450 
   451    test: term -> bool
   452    tests if a term is at all relevant to the refutation proof;
   453    if not, then it can be discarded. Can improve performance,
   454    esp. if disjunctions can be discarded (no case distinction needed!).
   455 
   456    prep_tac: int -> tactic
   457    A preparation tactic to be applied to the goal once all relevant premises
   458    have been moved to the conclusion.
   459 
   460    ref_tac: int -> tactic
   461    the actual refutation tactic. Should be able to deal with goals
   462    [| A1; ...; An |] ==> False
   463    where the Ai are atomic, i.e. no top-level &, | or EX
   464 *)
   465 
   466 local
   467   val nnf_simpset =
   468     empty_ss setmkeqTrue mk_eq_True
   469     setmksimps (mksimps mksimps_pairs)
   470     addsimps [imp_conv_disj,iff_conv_conj_imp,de_Morgan_disj,de_Morgan_conj,
   471       not_all,not_ex,not_not];
   472   fun prem_nnf_tac i st =
   473     full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st;
   474 in
   475 fun refute_tac test prep_tac ref_tac =
   476   let val refute_prems_tac =
   477         REPEAT_DETERM
   478               (eresolve_tac [conjE, exE] 1 ORELSE
   479                filter_prems_tac test 1 ORELSE
   480                etac disjE 1) THEN
   481         ((etac notE 1 THEN eq_assume_tac 1) ORELSE
   482          ref_tac 1);
   483   in EVERY'[TRY o filter_prems_tac test,
   484             REPEAT_DETERM o etac rev_mp, prep_tac, rtac ccontr, prem_nnf_tac,
   485             SELECT_GOAL (DEPTH_SOLVE refute_prems_tac)]
   486   end;
   487 end;