src/HOL/simpdata.ML
author haftmann
Mon Oct 16 14:07:21 2006 +0200 (2006-10-16)
changeset 21045 66d6d1b0ddfa
parent 20973 0b8e436ed071
permissions -rw-r--r--
slight cleanup
clasohm@1465
     1
(*  Title:      HOL/simpdata.ML
clasohm@923
     2
    ID:         $Id$
clasohm@1465
     3
    Author:     Tobias Nipkow
clasohm@923
     4
    Copyright   1991  University of Cambridge
clasohm@923
     5
oheimb@5304
     6
Instantiation of the generic simplifier for HOL.
clasohm@923
     7
*)
clasohm@923
     8
haftmann@20973
     9
(** tools setup **)
paulson@4351
    10
wenzelm@9851
    11
structure Quantifier1 = Quantifier1Fun
wenzelm@9851
    12
(struct
paulson@4351
    13
  (*abstract syntax*)
haftmann@20973
    14
  fun dest_eq ((c as Const("op =",_)) $ s $ t) = SOME (c, s, t)
skalberg@15531
    15
    | dest_eq _ = NONE;
haftmann@20973
    16
  fun dest_conj ((c as Const("op &",_)) $ s $ t) = SOME (c, s, t)
skalberg@15531
    17
    | dest_conj _ = NONE;
haftmann@20973
    18
  fun dest_imp ((c as Const("op -->",_)) $ s $ t) = SOME (c, s, t)
skalberg@15531
    19
    | dest_imp _ = NONE;
paulson@4351
    20
  val conj = HOLogic.conj
paulson@4351
    21
  val imp  = HOLogic.imp
paulson@4351
    22
  (*rules*)
haftmann@20944
    23
  val iff_reflection = HOL.eq_reflection
haftmann@20944
    24
  val iffI = HOL.iffI
haftmann@20944
    25
  val iff_trans = HOL.trans
haftmann@20944
    26
  val conjI= HOL.conjI
haftmann@20944
    27
  val conjE= HOL.conjE
haftmann@20944
    28
  val impI = HOL.impI
haftmann@20944
    29
  val mp   = HOL.mp
haftmann@20973
    30
  val uncurry = thm "uncurry"
haftmann@20944
    31
  val exI  = HOL.exI
haftmann@20944
    32
  val exE  = HOL.exE
haftmann@20973
    33
  val iff_allI = thm "iff_allI"
haftmann@20973
    34
  val iff_exI = thm "iff_exI"
haftmann@20973
    35
  val all_comm = thm "all_comm"
haftmann@20973
    36
  val ex_comm = thm "ex_comm"
paulson@4351
    37
end);
paulson@4351
    38
haftmann@20973
    39
structure HOL =
haftmann@20973
    40
struct
haftmann@20973
    41
haftmann@20973
    42
open HOL;
haftmann@20973
    43
haftmann@20973
    44
val Eq_FalseI = thm "Eq_FalseI";
haftmann@20973
    45
val Eq_TrueI = thm "Eq_TrueI";
haftmann@20973
    46
val simp_implies_def = thm "simp_implies_def";
haftmann@20973
    47
val simp_impliesI = thm "simp_impliesI";
haftmann@20973
    48
haftmann@20973
    49
fun mk_meta_eq r = r RS eq_reflection;
haftmann@20973
    50
fun safe_mk_meta_eq r = mk_meta_eq r handle Thm.THM _ => r;
haftmann@20973
    51
haftmann@20973
    52
fun mk_eq thm = case concl_of thm
haftmann@20973
    53
  (*expects Trueprop if not == *)
haftmann@20973
    54
  of Const ("==",_) $ _ $ _ => thm
haftmann@20973
    55
   | _ $ (Const ("op =", _) $ _ $ _) => mk_meta_eq thm
haftmann@20973
    56
   | _ $ (Const ("Not", _) $ _) => thm RS Eq_FalseI
haftmann@20973
    57
   | _ => thm RS Eq_TrueI;
haftmann@20973
    58
haftmann@20973
    59
fun mk_eq_True r =
haftmann@20973
    60
  SOME (r RS meta_eq_to_obj_eq RS Eq_TrueI) handle Thm.THM _ => NONE;
haftmann@20973
    61
haftmann@20973
    62
(* Produce theorems of the form
haftmann@20973
    63
  (P1 =simp=> ... =simp=> Pn => x == y) ==> (P1 =simp=> ... =simp=> Pn => x = y)
haftmann@20973
    64
*)
haftmann@20973
    65
fun lift_meta_eq_to_obj_eq i st =
haftmann@20973
    66
  let
haftmann@20973
    67
    fun count_imp (Const ("HOL.simp_implies", _) $ P $ Q) = 1 + count_imp Q
haftmann@20973
    68
      | count_imp _ = 0;
haftmann@20973
    69
    val j = count_imp (Logic.strip_assums_concl (List.nth (prems_of st, i - 1)))
haftmann@20973
    70
  in if j = 0 then meta_eq_to_obj_eq
haftmann@20973
    71
    else
haftmann@20973
    72
      let
haftmann@20973
    73
        val Ps = map (fn k => Free ("P" ^ string_of_int k, propT)) (1 upto j);
haftmann@20973
    74
        fun mk_simp_implies Q = foldr (fn (R, S) =>
haftmann@20973
    75
          Const ("HOL.simp_implies", propT --> propT --> propT) $ R $ S) Q Ps
haftmann@20973
    76
        val aT = TFree ("'a", HOLogic.typeS);
haftmann@20973
    77
        val x = Free ("x", aT);
haftmann@20973
    78
        val y = Free ("y", aT)
haftmann@20973
    79
      in Goal.prove_global (Thm.theory_of_thm st) []
haftmann@20973
    80
        [mk_simp_implies (Logic.mk_equals (x, y))]
haftmann@20973
    81
        (mk_simp_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (x, y))))
haftmann@20973
    82
        (fn prems => EVERY
haftmann@20973
    83
         [rewrite_goals_tac [simp_implies_def],
haftmann@20973
    84
          REPEAT (ares_tac (meta_eq_to_obj_eq :: map (rewrite_rule [simp_implies_def]) prems) 1)])
haftmann@20973
    85
      end
haftmann@20973
    86
  end;
haftmann@20973
    87
haftmann@20973
    88
(*Congruence rules for = (instead of ==)*)
haftmann@20973
    89
fun mk_meta_cong rl = zero_var_indexes
haftmann@20973
    90
  (let val rl' = Seq.hd (TRYALL (fn i => fn st =>
haftmann@20973
    91
     rtac (lift_meta_eq_to_obj_eq i st) i st) rl)
haftmann@20973
    92
   in mk_meta_eq rl' handle THM _ =>
haftmann@20973
    93
     if can Logic.dest_equals (concl_of rl') then rl'
haftmann@20973
    94
     else error "Conclusion of congruence rules must be =-equality"
haftmann@20973
    95
   end);
haftmann@20973
    96
haftmann@20973
    97
(*
haftmann@20973
    98
val mk_atomize:      (string * thm list) list -> thm -> thm list
haftmann@20973
    99
looks too specific to move it somewhere else
haftmann@20973
   100
*)
haftmann@20973
   101
fun mk_atomize pairs =
haftmann@20973
   102
  let
haftmann@20973
   103
    fun atoms thm = case concl_of thm
haftmann@20973
   104
     of Const("Trueprop", _) $ p => (case head_of p
haftmann@20973
   105
        of Const(a, _) => (case AList.lookup (op =) pairs a
haftmann@20973
   106
           of SOME rls => maps atoms ([thm] RL rls)
haftmann@20973
   107
            | NONE => [thm])
haftmann@20973
   108
         | _ => [thm])
haftmann@20973
   109
      | _ => [thm]
haftmann@20973
   110
  in atoms end;
haftmann@20973
   111
haftmann@20973
   112
fun mksimps pairs =
haftmann@20973
   113
  (map_filter (try mk_eq) o mk_atomize pairs o gen_all);
haftmann@20973
   114
haftmann@20973
   115
fun unsafe_solver_tac prems =
haftmann@20973
   116
  (fn i => REPEAT_DETERM (match_tac [simp_impliesI] i)) THEN'
haftmann@20973
   117
  FIRST'[resolve_tac(reflexive_thm :: TrueI :: refl :: prems), atac, etac FalseE];
haftmann@20973
   118
val unsafe_solver = mk_solver "HOL unsafe" unsafe_solver_tac;
haftmann@20973
   119
haftmann@20973
   120
(*No premature instantiation of variables during simplification*)
haftmann@20973
   121
fun safe_solver_tac prems =
haftmann@20973
   122
  (fn i => REPEAT_DETERM (match_tac [simp_impliesI] i)) THEN'
haftmann@20973
   123
  FIRST'[match_tac(reflexive_thm :: TrueI :: refl :: prems),
haftmann@20973
   124
         eq_assume_tac, ematch_tac [FalseE]];
haftmann@20973
   125
val safe_solver = mk_solver "HOL safe" safe_solver_tac;
haftmann@20973
   126
nipkow@11232
   127
end;
nipkow@11232
   128
haftmann@20973
   129
structure SplitterData =
haftmann@20973
   130
struct
haftmann@20973
   131
  structure Simplifier = Simplifier
haftmann@20973
   132
  val mk_eq           = HOL.mk_eq
haftmann@20973
   133
  val meta_eq_to_iff  = HOL.meta_eq_to_obj_eq
haftmann@20973
   134
  val iffD            = HOL.iffD2
haftmann@20973
   135
  val disjE           = HOL.disjE
haftmann@20973
   136
  val conjE           = HOL.conjE
haftmann@20973
   137
  val exE             = HOL.exE
haftmann@20973
   138
  val contrapos       = HOL.contrapos_nn
haftmann@20973
   139
  val contrapos2      = HOL.contrapos_pp
haftmann@20973
   140
  val notnotD         = HOL.notnotD
haftmann@20973
   141
end;
haftmann@20973
   142
haftmann@20973
   143
structure Splitter = SplitterFun(SplitterData);
haftmann@20973
   144
haftmann@20973
   145
(* integration of simplifier with classical reasoner *)
haftmann@20973
   146
haftmann@20973
   147
structure Clasimp = ClasimpFun
haftmann@20973
   148
 (structure Simplifier = Simplifier and Splitter = Splitter
haftmann@20973
   149
  and Classical  = Classical and Blast = Blast
haftmann@20973
   150
  val iffD1 = HOL.iffD1 val iffD2 = HOL.iffD2 val notE = HOL.notE);
wenzelm@13462
   151
haftmann@20973
   152
structure HOL =
haftmann@20973
   153
struct
haftmann@20973
   154
haftmann@20973
   155
open HOL;
haftmann@20973
   156
haftmann@20973
   157
val mksimps_pairs =
haftmann@20973
   158
  [("op -->", [mp]), ("op &", [thm "conjunct1", thm "conjunct2"]),
haftmann@20973
   159
   ("All", [spec]), ("True", []), ("False", []),
haftmann@20973
   160
   ("HOL.If", [thm "if_bool_eq_conj" RS iffD1])];
paulson@3913
   161
haftmann@20973
   162
val simpset_basic =
haftmann@20973
   163
  Simplifier.theory_context (the_context ()) empty_ss
haftmann@20973
   164
    setsubgoaler asm_simp_tac
haftmann@20973
   165
    setSSolver safe_solver
haftmann@20973
   166
    setSolver unsafe_solver
haftmann@20973
   167
    setmksimps (mksimps mksimps_pairs)
haftmann@20973
   168
    setmkeqTrue mk_eq_True
haftmann@20973
   169
    setmkcong mk_meta_cong;
haftmann@20973
   170
haftmann@20973
   171
fun simplify rews = Simplifier.full_simplify (simpset_basic addsimps rews);
haftmann@20973
   172
haftmann@20973
   173
fun unfold_tac ths =
haftmann@20973
   174
  let val ss0 = Simplifier.clear_ss simpset_basic addsimps ths
haftmann@20973
   175
  in fn ss => ALLGOALS (full_simp_tac (Simplifier.inherit_context ss ss0)) end;
haftmann@20973
   176
haftmann@20973
   177
(** simprocs **)
nipkow@17778
   178
haftmann@20944
   179
(* simproc for proving "(y = x) == False" from premise "~(x = y)" *)
nipkow@17778
   180
nipkow@17778
   181
val use_neq_simproc = ref true;
nipkow@17778
   182
nipkow@17778
   183
local
haftmann@20973
   184
  val thy = the_context ();
haftmann@20973
   185
  val neq_to_EQ_False = thm "not_sym" RS HOL.Eq_FalseI;
haftmann@20944
   186
  fun neq_prover sg ss (eq $ lhs $ rhs) =
haftmann@20944
   187
    let
haftmann@20944
   188
      fun test thm = (case #prop (rep_thm thm) of
nipkow@17778
   189
                    _ $ (Not $ (eq' $ l' $ r')) =>
nipkow@17778
   190
                      Not = HOLogic.Not andalso eq' = eq andalso
nipkow@17778
   191
                      r' aconv lhs andalso l' aconv rhs
nipkow@17778
   192
                  | _ => false)
haftmann@20944
   193
    in if !use_neq_simproc then case find_first test (prems_of_ss ss)
haftmann@20944
   194
     of NONE => NONE
haftmann@20944
   195
      | SOME thm => SOME (thm RS neq_to_EQ_False)
haftmann@20944
   196
     else NONE
haftmann@20944
   197
    end
nipkow@17778
   198
in
nipkow@17778
   199
haftmann@20973
   200
val neq_simproc = Simplifier.simproc thy "neq_simproc" ["x = y"] neq_prover;
nipkow@17778
   201
haftmann@20973
   202
end; (*local*)
nipkow@17778
   203
nipkow@17778
   204
haftmann@21045
   205
(* simproc for Let *)
schirmer@15423
   206
schirmer@15423
   207
val use_let_simproc = ref true;
schirmer@15423
   208
schirmer@15423
   209
local
haftmann@20973
   210
  val thy = the_context ();
haftmann@20944
   211
  val Let_folded = thm "Let_folded";
haftmann@20944
   212
  val Let_unfold = thm "Let_unfold";
haftmann@20973
   213
  val (f_Let_unfold, x_Let_unfold) =
wenzelm@20070
   214
      let val [(_$(f$x)$_)] = prems_of Let_unfold
haftmann@20973
   215
      in (cterm_of thy f, cterm_of thy x) end
haftmann@20973
   216
  val (f_Let_folded, x_Let_folded) =
wenzelm@20070
   217
      let val [(_$(f$x)$_)] = prems_of Let_folded
haftmann@20973
   218
      in (cterm_of thy f, cterm_of thy x) end;
haftmann@20944
   219
  val g_Let_folded =
haftmann@20973
   220
      let val [(_$_$(g$_))] = prems_of Let_folded in cterm_of thy g end;
schirmer@15423
   221
in
haftmann@20944
   222
schirmer@15423
   223
val let_simproc =
haftmann@20973
   224
  Simplifier.simproc thy "let_simp" ["Let x f"]
schirmer@15423
   225
   (fn sg => fn ss => fn t =>
schirmer@20014
   226
     let val ctxt = Simplifier.the_context ss;
haftmann@20973
   227
         val ([t'], ctxt') = Variable.import_terms false [t] ctxt;
schirmer@20014
   228
     in Option.map (hd o Variable.export ctxt' ctxt o single)
wenzelm@20070
   229
      (case t' of (Const ("Let",_)$x$f) => (* x and f are already in normal form *)
skalberg@15531
   230
         if not (!use_let_simproc) then NONE
wenzelm@20070
   231
         else if is_Free x orelse is_Bound x orelse is_Const x
haftmann@20944
   232
         then SOME (thm "Let_def")
schirmer@15423
   233
         else
schirmer@15423
   234
          let
schirmer@15423
   235
             val n = case f of (Abs (x,_,_)) => x | _ => "x";
schirmer@15423
   236
             val cx = cterm_of sg x;
schirmer@15423
   237
             val {T=xT,...} = rep_cterm cx;
schirmer@15423
   238
             val cf = cterm_of sg f;
schirmer@15423
   239
             val fx_g = Simplifier.rewrite ss (Thm.capply cf cx);
schirmer@15423
   240
             val (_$_$g) = prop_of fx_g;
schirmer@15423
   241
             val g' = abstract_over (x,g);
wenzelm@20070
   242
           in (if (g aconv g')
schirmer@15423
   243
               then
schirmer@15423
   244
                  let
schirmer@20014
   245
                    val rl = cterm_instantiate [(f_Let_unfold,cf),(x_Let_unfold,cx)] Let_unfold;
wenzelm@20070
   246
                  in SOME (rl OF [fx_g]) end
wenzelm@18176
   247
               else if Term.betapply (f,x) aconv g then NONE (*avoid identity conversion*)
wenzelm@20070
   248
               else let
schirmer@15423
   249
                     val abs_g'= Abs (n,xT,g');
schirmer@15423
   250
                     val g'x = abs_g'$x;
schirmer@15423
   251
                     val g_g'x = symmetric (beta_conversion false (cterm_of sg g'x));
schirmer@15423
   252
                     val rl = cterm_instantiate
schirmer@20014
   253
                               [(f_Let_folded,cterm_of sg f),(x_Let_folded,cx),
schirmer@15423
   254
                                (g_Let_folded,cterm_of sg abs_g')]
wenzelm@20070
   255
                               Let_folded;
wenzelm@20070
   256
                   in SOME (rl OF [transitive fx_g g_g'x])
schirmer@20014
   257
                   end)
schirmer@15423
   258
           end
schirmer@20014
   259
        | _ => NONE)
wenzelm@20070
   260
     end)
haftmann@20944
   261
haftmann@20973
   262
end; (*local*)
nipkow@1758
   263
haftmann@21045
   264
(* generic refutation procedure *)
wenzelm@9713
   265
haftmann@21045
   266
(* parameters:
nipkow@5975
   267
nipkow@5975
   268
   test: term -> bool
nipkow@5975
   269
   tests if a term is at all relevant to the refutation proof;
nipkow@5975
   270
   if not, then it can be discarded. Can improve performance,
nipkow@5975
   271
   esp. if disjunctions can be discarded (no case distinction needed!).
nipkow@5975
   272
nipkow@5975
   273
   prep_tac: int -> tactic
nipkow@5975
   274
   A preparation tactic to be applied to the goal once all relevant premises
nipkow@5975
   275
   have been moved to the conclusion.
nipkow@5975
   276
nipkow@5975
   277
   ref_tac: int -> tactic
nipkow@5975
   278
   the actual refutation tactic. Should be able to deal with goals
nipkow@5975
   279
   [| A1; ...; An |] ==> False
wenzelm@9876
   280
   where the Ai are atomic, i.e. no top-level &, | or EX
nipkow@5975
   281
*)
nipkow@5975
   282
nipkow@15184
   283
local
nipkow@15184
   284
  val nnf_simpset =
wenzelm@17892
   285
    empty_ss setmkeqTrue mk_eq_True
wenzelm@17892
   286
    setmksimps (mksimps mksimps_pairs)
haftmann@20973
   287
    addsimps [thm "imp_conv_disj", thm "iff_conv_conj_imp", thm "de_Morgan_disj", thm "de_Morgan_conj",
haftmann@20944
   288
      thm "not_all", thm "not_ex", thm "not_not"];
wenzelm@17892
   289
  fun prem_nnf_tac i st =
wenzelm@17892
   290
    full_simp_tac (Simplifier.theory_context (Thm.theory_of_thm st) nnf_simpset) i st;
nipkow@15184
   291
in
nipkow@15184
   292
fun refute_tac test prep_tac ref_tac =
nipkow@15184
   293
  let val refute_prems_tac =
nipkow@12475
   294
        REPEAT_DETERM
haftmann@20973
   295
              (eresolve_tac [conjE, exE] 1 ORELSE
nipkow@5975
   296
               filter_prems_tac test 1 ORELSE
haftmann@20973
   297
               etac disjE 1) THEN
haftmann@20973
   298
        ((etac notE 1 THEN eq_assume_tac 1) ORELSE
nipkow@11200
   299
         ref_tac 1);
nipkow@5975
   300
  in EVERY'[TRY o filter_prems_tac test,
haftmann@20973
   301
            REPEAT_DETERM o etac rev_mp, prep_tac, rtac ccontr, prem_nnf_tac,
nipkow@5975
   302
            SELECT_GOAL (DEPTH_SOLVE refute_prems_tac)]
nipkow@5975
   303
  end;
haftmann@20973
   304
end; (*local*)
haftmann@20973
   305
haftmann@20973
   306
val defALL_regroup =
haftmann@20973
   307
  Simplifier.simproc (the_context ())
haftmann@20973
   308
    "defined ALL" ["ALL x. P x"] Quantifier1.rearrange_all;
haftmann@20973
   309
haftmann@20973
   310
val defEX_regroup =
haftmann@20973
   311
  Simplifier.simproc (the_context ())
haftmann@20973
   312
    "defined EX" ["EX x. P x"] Quantifier1.rearrange_ex;
haftmann@20973
   313
haftmann@20973
   314
haftmann@20973
   315
val simpset_simprocs = simpset_basic
haftmann@20973
   316
  addsimprocs [defALL_regroup, defEX_regroup, neq_simproc, let_simproc]
haftmann@20973
   317
haftmann@20973
   318
end; (*struct*)