src/HOL/simpdata.ML
changeset 21163 6860f161111c
child 21313 26fc3a45547c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/simpdata.ML	Fri Nov 03 15:28:13 2006 +0100
     1.3 @@ -0,0 +1,318 @@
     1.4 +(*  Title:      HOL/simpdata.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Tobias Nipkow
     1.7 +    Copyright   1991  University of Cambridge
     1.8 +
     1.9 +Instantiation of the generic simplifier for HOL.
    1.10 +*)
    1.11 +
    1.12 +(** tools setup **)
    1.13 +
    1.14 +structure Quantifier1 = Quantifier1Fun
    1.15 +(struct
    1.16 +  (*abstract syntax*)
    1.17 +  fun dest_eq ((c as Const("op =",_)) $ s $ t) = SOME (c, s, t)
    1.18 +    | dest_eq _ = NONE;
    1.19 +  fun dest_conj ((c as Const("op &",_)) $ s $ t) = SOME (c, s, t)
    1.20 +    | dest_conj _ = NONE;
    1.21 +  fun dest_imp ((c as Const("op -->",_)) $ s $ t) = SOME (c, s, t)
    1.22 +    | dest_imp _ = NONE;
    1.23 +  val conj = HOLogic.conj
    1.24 +  val imp  = HOLogic.imp
    1.25 +  (*rules*)
    1.26 +  val iff_reflection = HOL.eq_reflection
    1.27 +  val iffI = HOL.iffI
    1.28 +  val iff_trans = HOL.trans
    1.29 +  val conjI= HOL.conjI
    1.30 +  val conjE= HOL.conjE
    1.31 +  val impI = HOL.impI
    1.32 +  val mp   = HOL.mp
    1.33 +  val uncurry = thm "uncurry"
    1.34 +  val exI  = HOL.exI
    1.35 +  val exE  = HOL.exE
    1.36 +  val iff_allI = thm "iff_allI"
    1.37 +  val iff_exI = thm "iff_exI"
    1.38 +  val all_comm = thm "all_comm"
    1.39 +  val ex_comm = thm "ex_comm"
    1.40 +end);
    1.41 +
    1.42 +structure HOL =
    1.43 +struct
    1.44 +
    1.45 +open HOL;
    1.46 +
    1.47 +val Eq_FalseI = thm "Eq_FalseI";
    1.48 +val Eq_TrueI = thm "Eq_TrueI";
    1.49 +val simp_implies_def = thm "simp_implies_def";
    1.50 +val simp_impliesI = thm "simp_impliesI";
    1.51 +
    1.52 +fun mk_meta_eq r = r RS eq_reflection;
    1.53 +fun safe_mk_meta_eq r = mk_meta_eq r handle Thm.THM _ => r;
    1.54 +
    1.55 +fun mk_eq thm = case concl_of thm
    1.56 +  (*expects Trueprop if not == *)
    1.57 +  of Const ("==",_) $ _ $ _ => thm
    1.58 +   | _ $ (Const ("op =", _) $ _ $ _) => mk_meta_eq thm
    1.59 +   | _ $ (Const ("Not", _) $ _) => thm RS Eq_FalseI
    1.60 +   | _ => thm RS Eq_TrueI;
    1.61 +
    1.62 +fun mk_eq_True r =
    1.63 +  SOME (r RS meta_eq_to_obj_eq RS Eq_TrueI) handle Thm.THM _ => NONE;
    1.64 +
    1.65 +(* Produce theorems of the form
    1.66 +  (P1 =simp=> ... =simp=> Pn => x == y) ==> (P1 =simp=> ... =simp=> Pn => x = y)
    1.67 +*)
    1.68 +fun lift_meta_eq_to_obj_eq i st =
    1.69 +  let
    1.70 +    fun count_imp (Const ("HOL.simp_implies", _) $ P $ Q) = 1 + count_imp Q
    1.71 +      | count_imp _ = 0;
    1.72 +    val j = count_imp (Logic.strip_assums_concl (List.nth (prems_of st, i - 1)))
    1.73 +  in if j = 0 then meta_eq_to_obj_eq
    1.74 +    else
    1.75 +      let
    1.76 +        val Ps = map (fn k => Free ("P" ^ string_of_int k, propT)) (1 upto j);
    1.77 +        fun mk_simp_implies Q = foldr (fn (R, S) =>
    1.78 +          Const ("HOL.simp_implies", propT --> propT --> propT) $ R $ S) Q Ps
    1.79 +        val aT = TFree ("'a", HOLogic.typeS);
    1.80 +        val x = Free ("x", aT);
    1.81 +        val y = Free ("y", aT)
    1.82 +      in Goal.prove_global (Thm.theory_of_thm st) []
    1.83 +        [mk_simp_implies (Logic.mk_equals (x, y))]
    1.84 +        (mk_simp_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (x, y))))
    1.85 +        (fn prems => EVERY
    1.86 +         [rewrite_goals_tac [simp_implies_def],
    1.87 +          REPEAT (ares_tac (meta_eq_to_obj_eq :: map (rewrite_rule [simp_implies_def]) prems) 1)])
    1.88 +      end
    1.89 +  end;
    1.90 +
    1.91 +(*Congruence rules for = (instead of ==)*)
    1.92 +fun mk_meta_cong rl = zero_var_indexes
    1.93 +  (let val rl' = Seq.hd (TRYALL (fn i => fn st =>
    1.94 +     rtac (lift_meta_eq_to_obj_eq i st) i st) rl)
    1.95 +   in mk_meta_eq rl' handle THM _ =>
    1.96 +     if can Logic.dest_equals (concl_of rl') then rl'
    1.97 +     else error "Conclusion of congruence rules must be =-equality"
    1.98 +   end);
    1.99 +
   1.100 +(*
   1.101 +val mk_atomize:      (string * thm list) list -> thm -> thm list
   1.102 +looks too specific to move it somewhere else
   1.103 +*)
   1.104 +fun mk_atomize pairs =
   1.105 +  let
   1.106 +    fun atoms thm = case concl_of thm
   1.107 +     of Const("Trueprop", _) $ p => (case head_of p
   1.108 +        of Const(a, _) => (case AList.lookup (op =) pairs a
   1.109 +           of SOME rls => maps atoms ([thm] RL rls)
   1.110 +            | NONE => [thm])
   1.111 +         | _ => [thm])
   1.112 +      | _ => [thm]
   1.113 +  in atoms end;
   1.114 +
   1.115 +fun mksimps pairs =
   1.116 +  (map_filter (try mk_eq) o mk_atomize pairs o gen_all);
   1.117 +
   1.118 +fun unsafe_solver_tac prems =
   1.119 +  (fn i => REPEAT_DETERM (match_tac [simp_impliesI] i)) THEN'
   1.120 +  FIRST'[resolve_tac(reflexive_thm :: TrueI :: refl :: prems), atac, etac FalseE];
   1.121 +val unsafe_solver = mk_solver "HOL unsafe" unsafe_solver_tac;
   1.122 +
   1.123 +(*No premature instantiation of variables during simplification*)
   1.124 +fun safe_solver_tac prems =
   1.125 +  (fn i => REPEAT_DETERM (match_tac [simp_impliesI] i)) THEN'
   1.126 +  FIRST'[match_tac(reflexive_thm :: TrueI :: refl :: prems),
   1.127 +         eq_assume_tac, ematch_tac [FalseE]];
   1.128 +val safe_solver = mk_solver "HOL safe" safe_solver_tac;
   1.129 +
   1.130 +end;
   1.131 +
   1.132 +structure SplitterData =
   1.133 +struct
   1.134 +  structure Simplifier = Simplifier
   1.135 +  val mk_eq           = HOL.mk_eq
   1.136 +  val meta_eq_to_iff  = HOL.meta_eq_to_obj_eq
   1.137 +  val iffD            = HOL.iffD2
   1.138 +  val disjE           = HOL.disjE
   1.139 +  val conjE           = HOL.conjE
   1.140 +  val exE             = HOL.exE
   1.141 +  val contrapos       = HOL.contrapos_nn
   1.142 +  val contrapos2      = HOL.contrapos_pp
   1.143 +  val notnotD         = HOL.notnotD
   1.144 +end;
   1.145 +
   1.146 +structure Splitter = SplitterFun(SplitterData);
   1.147 +
   1.148 +(* integration of simplifier with classical reasoner *)
   1.149 +
   1.150 +structure Clasimp = ClasimpFun
   1.151 + (structure Simplifier = Simplifier and Splitter = Splitter
   1.152 +  and Classical  = Classical and Blast = Blast
   1.153 +  val iffD1 = HOL.iffD1 val iffD2 = HOL.iffD2 val notE = HOL.notE);
   1.154 +
   1.155 +structure HOL =
   1.156 +struct
   1.157 +
   1.158 +open HOL;
   1.159 +
   1.160 +val mksimps_pairs =
   1.161 +  [("op -->", [mp]), ("op &", [thm "conjunct1", thm "conjunct2"]),
   1.162 +   ("All", [spec]), ("True", []), ("False", []),
   1.163 +   ("HOL.If", [thm "if_bool_eq_conj" RS iffD1])];
   1.164 +
   1.165 +val simpset_basic =
   1.166 +  Simplifier.theory_context (the_context ()) empty_ss
   1.167 +    setsubgoaler asm_simp_tac
   1.168 +    setSSolver safe_solver
   1.169 +    setSolver unsafe_solver
   1.170 +    setmksimps (mksimps mksimps_pairs)
   1.171 +    setmkeqTrue mk_eq_True
   1.172 +    setmkcong mk_meta_cong;
   1.173 +
   1.174 +fun simplify rews = Simplifier.full_simplify (simpset_basic addsimps rews);
   1.175 +
   1.176 +fun unfold_tac ths =
   1.177 +  let val ss0 = Simplifier.clear_ss simpset_basic addsimps ths
   1.178 +  in fn ss => ALLGOALS (full_simp_tac (Simplifier.inherit_context ss ss0)) end;
   1.179 +
   1.180 +(** simprocs **)
   1.181 +
   1.182 +(* simproc for proving "(y = x) == False" from premise "~(x = y)" *)
   1.183 +
   1.184 +val use_neq_simproc = ref true;
   1.185 +
   1.186 +local
   1.187 +  val thy = the_context ();
   1.188 +  val neq_to_EQ_False = thm "not_sym" RS HOL.Eq_FalseI;
   1.189 +  fun neq_prover sg ss (eq $ lhs $ rhs) =
   1.190 +    let
   1.191 +      fun test thm = (case #prop (rep_thm thm) of
   1.192 +                    _ $ (Not $ (eq' $ l' $ r')) =>
   1.193 +                      Not = HOLogic.Not andalso eq' = eq andalso
   1.194 +                      r' aconv lhs andalso l' aconv rhs
   1.195 +                  | _ => false)
   1.196 +    in if !use_neq_simproc then case find_first test (prems_of_ss ss)
   1.197 +     of NONE => NONE
   1.198 +      | SOME thm => SOME (thm RS neq_to_EQ_False)
   1.199 +     else NONE
   1.200 +    end
   1.201 +in
   1.202 +
   1.203 +val neq_simproc = Simplifier.simproc thy "neq_simproc" ["x = y"] neq_prover;
   1.204 +
   1.205 +end; (*local*)
   1.206 +
   1.207 +
   1.208 +(* simproc for Let *)
   1.209 +
   1.210 +val use_let_simproc = ref true;
   1.211 +
   1.212 +local
   1.213 +  val thy = the_context ();
   1.214 +  val Let_folded = thm "Let_folded";
   1.215 +  val Let_unfold = thm "Let_unfold";
   1.216 +  val (f_Let_unfold, x_Let_unfold) =
   1.217 +      let val [(_$(f$x)$_)] = prems_of Let_unfold
   1.218 +      in (cterm_of thy f, cterm_of thy x) end
   1.219 +  val (f_Let_folded, x_Let_folded) =
   1.220 +      let val [(_$(f$x)$_)] = prems_of Let_folded
   1.221 +      in (cterm_of thy f, cterm_of thy x) end;
   1.222 +  val g_Let_folded =
   1.223 +      let val [(_$_$(g$_))] = prems_of Let_folded in cterm_of thy g end;
   1.224 +in
   1.225 +
   1.226 +val let_simproc =
   1.227 +  Simplifier.simproc thy "let_simp" ["Let x f"]
   1.228 +   (fn sg => fn ss => fn t =>
   1.229 +     let val ctxt = Simplifier.the_context ss;
   1.230 +         val ([t'], ctxt') = Variable.import_terms false [t] ctxt;
   1.231 +     in Option.map (hd o Variable.export ctxt' ctxt o single)
   1.232 +      (case t' of (Const ("Let",_)$x$f) => (* x and f are already in normal form *)
   1.233 +         if not (!use_let_simproc) then NONE
   1.234 +         else if is_Free x orelse is_Bound x orelse is_Const x
   1.235 +         then SOME (thm "Let_def")
   1.236 +         else
   1.237 +          let
   1.238 +             val n = case f of (Abs (x,_,_)) => x | _ => "x";
   1.239 +             val cx = cterm_of sg x;
   1.240 +             val {T=xT,...} = rep_cterm cx;
   1.241 +             val cf = cterm_of sg f;
   1.242 +             val fx_g = Simplifier.rewrite ss (Thm.capply cf cx);
   1.243 +             val (_$_$g) = prop_of fx_g;
   1.244 +             val g' = abstract_over (x,g);
   1.245 +           in (if (g aconv g')
   1.246 +               then
   1.247 +                  let
   1.248 +                    val rl = cterm_instantiate [(f_Let_unfold,cf),(x_Let_unfold,cx)] Let_unfold;
   1.249 +                  in SOME (rl OF [fx_g]) end
   1.250 +               else if Term.betapply (f,x) aconv g then NONE (*avoid identity conversion*)
   1.251 +               else let
   1.252 +                     val abs_g'= Abs (n,xT,g');
   1.253 +                     val g'x = abs_g'$x;
   1.254 +                     val g_g'x = symmetric (beta_conversion false (cterm_of sg g'x));
   1.255 +                     val rl = cterm_instantiate
   1.256 +                               [(f_Let_folded,cterm_of sg f),(x_Let_folded,cx),
   1.257 +                                (g_Let_folded,cterm_of sg abs_g')]
   1.258 +                               Let_folded;
   1.259 +                   in SOME (rl OF [transitive fx_g g_g'x])
   1.260 +                   end)
   1.261 +           end
   1.262 +        | _ => NONE)
   1.263 +     end)
   1.264 +
   1.265 +end; (*local*)
   1.266 +
   1.267 +(* generic refutation procedure *)
   1.268 +
   1.269 +(* parameters:
   1.270 +
   1.271 +   test: term -> bool
   1.272 +   tests if a term is at all relevant to the refutation proof;
   1.273 +   if not, then it can be discarded. Can improve performance,
   1.274 +   esp. if disjunctions can be discarded (no case distinction needed!).
   1.275 +
   1.276 +   prep_tac: int -> tactic
   1.277 +   A preparation tactic to be applied to the goal once all relevant premises
   1.278 +   have been moved to the conclusion.
   1.279 +
   1.280 +   ref_tac: int -> tactic
   1.281 +   the actual refutation tactic. Should be able to deal with goals
   1.282 +   [| A1; ...; An |] ==> False
   1.283 +   where the Ai are atomic, i.e. no top-level &, | or EX
   1.284 +*)
   1.285 +
   1.286 +local
   1.287 +  val nnf_simpset =
   1.288 +    empty_ss setmkeqTrue mk_eq_True
   1.289 +    setmksimps (mksimps mksimps_pairs)
   1.290 +    addsimps [thm "imp_conv_disj", thm "iff_conv_conj_imp", thm "de_Morgan_disj", thm "de_Morgan_conj",
   1.291 +      thm "not_all", thm "not_ex", thm "not_not"];
   1.292 +  fun prem_nnf_tac i st =
   1.293 +    full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st;
   1.294 +in
   1.295 +fun refute_tac test prep_tac ref_tac =
   1.296 +  let val refute_prems_tac =
   1.297 +        REPEAT_DETERM
   1.298 +              (eresolve_tac [conjE, exE] 1 ORELSE
   1.299 +               filter_prems_tac test 1 ORELSE
   1.300 +               etac disjE 1) THEN
   1.301 +        ((etac notE 1 THEN eq_assume_tac 1) ORELSE
   1.302 +         ref_tac 1);
   1.303 +  in EVERY'[TRY o filter_prems_tac test,
   1.304 +            REPEAT_DETERM o etac rev_mp, prep_tac, rtac ccontr, prem_nnf_tac,
   1.305 +            SELECT_GOAL (DEPTH_SOLVE refute_prems_tac)]
   1.306 +  end;
   1.307 +end; (*local*)
   1.308 +
   1.309 +val defALL_regroup =
   1.310 +  Simplifier.simproc (the_context ())
   1.311 +    "defined ALL" ["ALL x. P x"] Quantifier1.rearrange_all;
   1.312 +
   1.313 +val defEX_regroup =
   1.314 +  Simplifier.simproc (the_context ())
   1.315 +    "defined EX" ["EX x. P x"] Quantifier1.rearrange_ex;
   1.316 +
   1.317 +
   1.318 +val simpset_simprocs = simpset_basic
   1.319 +  addsimprocs [defALL_regroup, defEX_regroup, neq_simproc, let_simproc]
   1.320 +
   1.321 +end; (*struct*)
   1.322 \ No newline at end of file