src/HOL/HOL.thy
author nipkow
Wed Apr 22 12:11:48 2015 +0200 (2015-04-22)
changeset 60143 2cd31c81e0e7
parent 59990 a81dc82ecba3
child 60147 6d7b7a037e8d
permissions -rw-r--r--
added simp rules for ==>
clasohm@923
     1
(*  Title:      HOL/HOL.thy
wenzelm@11750
     2
    Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
wenzelm@11750
     3
*)
clasohm@923
     4
wenzelm@58889
     5
section {* The basis of Higher-Order Logic *}
clasohm@923
     6
nipkow@15131
     7
theory HOL
haftmann@30929
     8
imports Pure "~~/src/Tools/Code_Generator"
wenzelm@46950
     9
keywords
wenzelm@52432
    10
  "try" "solve_direct" "quickcheck" "print_coercions" "print_claset"
wenzelm@52432
    11
    "print_induct_rules" :: diag and
haftmann@47657
    12
  "quickcheck_params" :: thy_decl
nipkow@15131
    13
begin
wenzelm@2260
    14
wenzelm@48891
    15
ML_file "~~/src/Tools/misc_legacy.ML"
wenzelm@48891
    16
ML_file "~~/src/Tools/try.ML"
wenzelm@48891
    17
ML_file "~~/src/Tools/quickcheck.ML"
wenzelm@48891
    18
ML_file "~~/src/Tools/solve_direct.ML"
wenzelm@48891
    19
ML_file "~~/src/Tools/IsaPlanner/zipper.ML"
wenzelm@48891
    20
ML_file "~~/src/Tools/IsaPlanner/isand.ML"
wenzelm@48891
    21
ML_file "~~/src/Tools/IsaPlanner/rw_inst.ML"
wenzelm@48891
    22
ML_file "~~/src/Provers/hypsubst.ML"
wenzelm@48891
    23
ML_file "~~/src/Provers/splitter.ML"
wenzelm@48891
    24
ML_file "~~/src/Provers/classical.ML"
wenzelm@48891
    25
ML_file "~~/src/Provers/blast.ML"
wenzelm@48891
    26
ML_file "~~/src/Provers/clasimp.ML"
wenzelm@48891
    27
ML_file "~~/src/Tools/eqsubst.ML"
wenzelm@48891
    28
ML_file "~~/src/Provers/quantifier1.ML"
wenzelm@48891
    29
ML_file "~~/src/Tools/atomize_elim.ML"
wenzelm@48891
    30
ML_file "~~/src/Tools/cong_tac.ML"
wenzelm@58826
    31
ML_file "~~/src/Tools/intuitionistic.ML" setup \<open>Intuitionistic.method_setup @{binding iprover}\<close>
wenzelm@48891
    32
ML_file "~~/src/Tools/project_rule.ML"
wenzelm@48891
    33
ML_file "~~/src/Tools/subtyping.ML"
wenzelm@48891
    34
ML_file "~~/src/Tools/case_product.ML"
wenzelm@48891
    35
wenzelm@30165
    36
wenzelm@58659
    37
ML \<open>Plugin_Name.declare_setup @{binding extraction}\<close>
wenzelm@58659
    38
wenzelm@58659
    39
ML \<open>
wenzelm@58659
    40
  Plugin_Name.declare_setup @{binding quickcheck_random};
wenzelm@58659
    41
  Plugin_Name.declare_setup @{binding quickcheck_exhaustive};
wenzelm@58659
    42
  Plugin_Name.declare_setup @{binding quickcheck_bounded_forall};
wenzelm@58659
    43
  Plugin_Name.declare_setup @{binding quickcheck_full_exhaustive};
wenzelm@58659
    44
  Plugin_Name.declare_setup @{binding quickcheck_narrowing};
wenzelm@58659
    45
\<close>
wenzelm@58659
    46
ML \<open>
wenzelm@58659
    47
  Plugin_Name.define_setup @{binding quickcheck}
wenzelm@58659
    48
   [@{plugin quickcheck_exhaustive},
wenzelm@58659
    49
    @{plugin quickcheck_random},
wenzelm@58659
    50
    @{plugin quickcheck_bounded_forall},
wenzelm@58659
    51
    @{plugin quickcheck_full_exhaustive},
wenzelm@58659
    52
    @{plugin quickcheck_narrowing}]
wenzelm@58659
    53
\<close>
wenzelm@58659
    54
wenzelm@58659
    55
wenzelm@11750
    56
subsection {* Primitive logic *}
wenzelm@11750
    57
wenzelm@11750
    58
subsubsection {* Core syntax *}
wenzelm@2260
    59
wenzelm@56941
    60
setup {* Axclass.class_axiomatization (@{binding type}, []) *}
wenzelm@36452
    61
default_sort type
wenzelm@35625
    62
setup {* Object_Logic.add_base_sort @{sort type} *}
haftmann@25460
    63
wenzelm@55383
    64
axiomatization where fun_arity: "OFCLASS('a \<Rightarrow> 'b, type_class)"
wenzelm@55383
    65
instance "fun" :: (type, type) type by (rule fun_arity)
wenzelm@55383
    66
wenzelm@55383
    67
axiomatization where itself_arity: "OFCLASS('a itself, type_class)"
wenzelm@55383
    68
instance itself :: (type) type by (rule itself_arity)
haftmann@25460
    69
wenzelm@7357
    70
typedecl bool
clasohm@923
    71
wenzelm@11750
    72
judgment
wenzelm@11750
    73
  Trueprop      :: "bool => prop"                   ("(_)" 5)
clasohm@923
    74
wenzelm@46973
    75
axiomatization
wenzelm@46973
    76
  implies       :: "[bool, bool] => bool"           (infixr "-->" 25)  and
wenzelm@46973
    77
  eq            :: "['a, 'a] => bool"               (infixl "=" 50)  and
wenzelm@46973
    78
  The           :: "('a => bool) => 'a"
wenzelm@46973
    79
wenzelm@11750
    80
consts
wenzelm@7357
    81
  True          :: bool
wenzelm@7357
    82
  False         :: bool
haftmann@38547
    83
  Not           :: "bool => bool"                   ("~ _" [40] 40)
haftmann@38795
    84
haftmann@38795
    85
  conj          :: "[bool, bool] => bool"           (infixr "&" 35)
haftmann@38795
    86
  disj          :: "[bool, bool] => bool"           (infixr "|" 30)
haftmann@38555
    87
wenzelm@7357
    88
  All           :: "('a => bool) => bool"           (binder "ALL " 10)
wenzelm@7357
    89
  Ex            :: "('a => bool) => bool"           (binder "EX " 10)
wenzelm@7357
    90
  Ex1           :: "('a => bool) => bool"           (binder "EX! " 10)
clasohm@923
    91
wenzelm@19656
    92
wenzelm@11750
    93
subsubsection {* Additional concrete syntax *}
wenzelm@2260
    94
wenzelm@21210
    95
notation (output)
haftmann@38864
    96
  eq  (infix "=" 50)
wenzelm@19656
    97
wenzelm@19656
    98
abbreviation
wenzelm@21404
    99
  not_equal :: "['a, 'a] => bool"  (infixl "~=" 50) where
wenzelm@19656
   100
  "x ~= y == ~ (x = y)"
wenzelm@19656
   101
wenzelm@21210
   102
notation (output)
wenzelm@19656
   103
  not_equal  (infix "~=" 50)
wenzelm@19656
   104
wenzelm@21210
   105
notation (xsymbols)
wenzelm@21404
   106
  Not  ("\<not> _" [40] 40) and
haftmann@38864
   107
  conj  (infixr "\<and>" 35) and
haftmann@38864
   108
  disj  (infixr "\<or>" 30) and
haftmann@38864
   109
  implies  (infixr "\<longrightarrow>" 25) and
nipkow@50360
   110
  not_equal  (infixl "\<noteq>" 50)
nipkow@50360
   111
nipkow@50360
   112
notation (xsymbols output)
wenzelm@19656
   113
  not_equal  (infix "\<noteq>" 50)
wenzelm@19656
   114
wenzelm@21210
   115
notation (HTML output)
wenzelm@21404
   116
  Not  ("\<not> _" [40] 40) and
haftmann@38864
   117
  conj  (infixr "\<and>" 35) and
haftmann@38864
   118
  disj  (infixr "\<or>" 30) and
wenzelm@19656
   119
  not_equal  (infix "\<noteq>" 50)
wenzelm@19656
   120
wenzelm@19656
   121
abbreviation (iff)
wenzelm@21404
   122
  iff :: "[bool, bool] => bool"  (infixr "<->" 25) where
wenzelm@19656
   123
  "A <-> B == A = B"
wenzelm@19656
   124
wenzelm@21210
   125
notation (xsymbols)
wenzelm@19656
   126
  iff  (infixr "\<longleftrightarrow>" 25)
wenzelm@19656
   127
wenzelm@46973
   128
syntax "_The" :: "[pttrn, bool] => 'a"  ("(3THE _./ _)" [0, 10] 10)
wenzelm@46973
   129
translations "THE x. P" == "CONST The (%x. P)"
wenzelm@46125
   130
print_translation {*
wenzelm@52143
   131
  [(@{const_syntax The}, fn _ => fn [Abs abs] =>
wenzelm@46125
   132
      let val (x, t) = Syntax_Trans.atomic_abs_tr' abs
wenzelm@46125
   133
      in Syntax.const @{syntax_const "_The"} $ x $ t end)]
wenzelm@46125
   134
*}  -- {* To avoid eta-contraction of body *}
clasohm@923
   135
wenzelm@46125
   136
nonterminal letbinds and letbind
clasohm@923
   137
syntax
wenzelm@7357
   138
  "_bind"       :: "[pttrn, 'a] => letbind"              ("(2_ =/ _)" 10)
wenzelm@7357
   139
  ""            :: "letbind => letbinds"                 ("_")
wenzelm@7357
   140
  "_binds"      :: "[letbind, letbinds] => letbinds"     ("_;/ _")
huffman@36363
   141
  "_Let"        :: "[letbinds, 'a] => 'a"                ("(let (_)/ in (_))" [0, 10] 10)
clasohm@923
   142
wenzelm@46125
   143
nonterminal case_syn and cases_syn
wenzelm@46125
   144
syntax
wenzelm@46125
   145
  "_case_syntax" :: "['a, cases_syn] => 'b"  ("(case _ of/ _)" 10)
wenzelm@46125
   146
  "_case1" :: "['a, 'b] => case_syn"  ("(2_ =>/ _)" 10)
wenzelm@46125
   147
  "" :: "case_syn => cases_syn"  ("_")
wenzelm@46125
   148
  "_case2" :: "[case_syn, cases_syn] => cases_syn"  ("_/ | _")
wenzelm@42057
   149
syntax (xsymbols)
wenzelm@46125
   150
  "_case1" :: "['a, 'b] => case_syn"  ("(2_ \<Rightarrow>/ _)" 10)
nipkow@13763
   151
wenzelm@21524
   152
notation (xsymbols)
wenzelm@21524
   153
  All  (binder "\<forall>" 10) and
wenzelm@21524
   154
  Ex  (binder "\<exists>" 10) and
wenzelm@21524
   155
  Ex1  (binder "\<exists>!" 10)
wenzelm@2372
   156
wenzelm@21524
   157
notation (HTML output)
wenzelm@21524
   158
  All  (binder "\<forall>" 10) and
wenzelm@21524
   159
  Ex  (binder "\<exists>" 10) and
wenzelm@21524
   160
  Ex1  (binder "\<exists>!" 10)
wenzelm@6340
   161
wenzelm@21524
   162
notation (HOL)
wenzelm@21524
   163
  All  (binder "! " 10) and
wenzelm@21524
   164
  Ex  (binder "? " 10) and
wenzelm@21524
   165
  Ex1  (binder "?! " 10)
wenzelm@7238
   166
wenzelm@7238
   167
wenzelm@11750
   168
subsubsection {* Axioms and basic definitions *}
wenzelm@2260
   169
wenzelm@46973
   170
axiomatization where
wenzelm@46973
   171
  refl: "t = (t::'a)" and
wenzelm@46973
   172
  subst: "s = t \<Longrightarrow> P s \<Longrightarrow> P t" and
wenzelm@46973
   173
  ext: "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
paulson@15380
   174
    -- {*Extensionality is built into the meta-logic, and this rule expresses
paulson@15380
   175
         a related property.  It is an eta-expanded version of the traditional
wenzelm@46973
   176
         rule, and similar to the ABS rule of HOL*} and
paulson@6289
   177
wenzelm@11432
   178
  the_eq_trivial: "(THE x. x = a) = (a::'a)"
clasohm@923
   179
wenzelm@46973
   180
axiomatization where
wenzelm@46973
   181
  impI: "(P ==> Q) ==> P-->Q" and
wenzelm@46973
   182
  mp: "[| P-->Q;  P |] ==> Q" and
paulson@15380
   183
wenzelm@46973
   184
  iff: "(P-->Q) --> (Q-->P) --> (P=Q)" and
wenzelm@46973
   185
  True_or_False: "(P=True) | (P=False)"
paulson@15380
   186
clasohm@923
   187
defs
wenzelm@7357
   188
  True_def:     "True      == ((%x::bool. x) = (%x. x))"
wenzelm@7357
   189
  All_def:      "All(P)    == (P = (%x. True))"
paulson@11451
   190
  Ex_def:       "Ex(P)     == !Q. (!x. P x --> Q) --> Q"
wenzelm@7357
   191
  False_def:    "False     == (!P. P)"
wenzelm@7357
   192
  not_def:      "~ P       == P-->False"
wenzelm@7357
   193
  and_def:      "P & Q     == !R. (P-->Q-->R) --> R"
wenzelm@7357
   194
  or_def:       "P | Q     == !R. (P-->R) --> (Q-->R) --> R"
wenzelm@7357
   195
  Ex1_def:      "Ex1(P)    == ? x. P(x) & (! y. P(y) --> y=x)"
clasohm@923
   196
wenzelm@46973
   197
definition If :: "bool \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a" ("(if (_)/ then (_)/ else (_))" [0, 0, 10] 10)
wenzelm@46973
   198
  where "If P x y \<equiv> (THE z::'a. (P=True --> z=x) & (P=False --> z=y))"
clasohm@923
   199
wenzelm@46973
   200
definition Let :: "'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b"
wenzelm@46973
   201
  where "Let s f \<equiv> f s"
haftmann@38525
   202
haftmann@38525
   203
translations
haftmann@38525
   204
  "_Let (_binds b bs) e"  == "_Let b (_Let bs e)"
haftmann@38525
   205
  "let x = a in e"        == "CONST Let a (%x. e)"
haftmann@38525
   206
wenzelm@46973
   207
axiomatization undefined :: 'a
haftmann@22481
   208
wenzelm@46973
   209
class default = fixes default :: 'a
wenzelm@4868
   210
wenzelm@11750
   211
haftmann@20944
   212
subsection {* Fundamental rules *}
haftmann@20944
   213
haftmann@20973
   214
subsubsection {* Equality *}
haftmann@20944
   215
wenzelm@18457
   216
lemma sym: "s = t ==> t = s"
wenzelm@18457
   217
  by (erule subst) (rule refl)
paulson@15411
   218
wenzelm@18457
   219
lemma ssubst: "t = s ==> P s ==> P t"
wenzelm@18457
   220
  by (drule sym) (erule subst)
paulson@15411
   221
paulson@15411
   222
lemma trans: "[| r=s; s=t |] ==> r=t"
wenzelm@18457
   223
  by (erule subst)
paulson@15411
   224
wenzelm@40715
   225
lemma trans_sym [Pure.elim?]: "r = s ==> t = s ==> r = t"
wenzelm@40715
   226
  by (rule trans [OF _ sym])
wenzelm@40715
   227
wenzelm@58826
   228
lemma meta_eq_to_obj_eq:
haftmann@20944
   229
  assumes meq: "A == B"
haftmann@20944
   230
  shows "A = B"
haftmann@20944
   231
  by (unfold meq) (rule refl)
paulson@15411
   232
wenzelm@21502
   233
text {* Useful with @{text erule} for proving equalities from known equalities. *}
haftmann@20944
   234
     (* a = b
paulson@15411
   235
        |   |
paulson@15411
   236
        c = d   *)
paulson@15411
   237
lemma box_equals: "[| a=b;  a=c;  b=d |] ==> c=d"
paulson@15411
   238
apply (rule trans)
paulson@15411
   239
apply (rule trans)
paulson@15411
   240
apply (rule sym)
paulson@15411
   241
apply assumption+
paulson@15411
   242
done
paulson@15411
   243
nipkow@15524
   244
text {* For calculational reasoning: *}
nipkow@15524
   245
nipkow@15524
   246
lemma forw_subst: "a = b ==> P b ==> P a"
nipkow@15524
   247
  by (rule ssubst)
nipkow@15524
   248
nipkow@15524
   249
lemma back_subst: "P a ==> a = b ==> P b"
nipkow@15524
   250
  by (rule subst)
nipkow@15524
   251
paulson@15411
   252
wenzelm@32733
   253
subsubsection {* Congruence rules for application *}
paulson@15411
   254
wenzelm@32733
   255
text {* Similar to @{text AP_THM} in Gordon's HOL. *}
paulson@15411
   256
lemma fun_cong: "(f::'a=>'b) = g ==> f(x)=g(x)"
paulson@15411
   257
apply (erule subst)
paulson@15411
   258
apply (rule refl)
paulson@15411
   259
done
paulson@15411
   260
wenzelm@32733
   261
text {* Similar to @{text AP_TERM} in Gordon's HOL and FOL's @{text subst_context}. *}
paulson@15411
   262
lemma arg_cong: "x=y ==> f(x)=f(y)"
paulson@15411
   263
apply (erule subst)
paulson@15411
   264
apply (rule refl)
paulson@15411
   265
done
paulson@15411
   266
paulson@15655
   267
lemma arg_cong2: "\<lbrakk> a = b; c = d \<rbrakk> \<Longrightarrow> f a c = f b d"
paulson@15655
   268
apply (erule ssubst)+
paulson@15655
   269
apply (rule refl)
paulson@15655
   270
done
paulson@15655
   271
wenzelm@32733
   272
lemma cong: "[| f = g; (x::'a) = y |] ==> f x = g y"
paulson@15411
   273
apply (erule subst)+
paulson@15411
   274
apply (rule refl)
paulson@15411
   275
done
paulson@15411
   276
wenzelm@58956
   277
ML {* fun cong_tac ctxt = Cong_Tac.cong_tac ctxt @{thm cong} *}
paulson@15411
   278
wenzelm@32733
   279
wenzelm@32733
   280
subsubsection {* Equality of booleans -- iff *}
paulson@15411
   281
wenzelm@21504
   282
lemma iffI: assumes "P ==> Q" and "Q ==> P" shows "P=Q"
wenzelm@21504
   283
  by (iprover intro: iff [THEN mp, THEN mp] impI assms)
paulson@15411
   284
paulson@15411
   285
lemma iffD2: "[| P=Q; Q |] ==> P"
wenzelm@18457
   286
  by (erule ssubst)
paulson@15411
   287
paulson@15411
   288
lemma rev_iffD2: "[| Q; P=Q |] ==> P"
wenzelm@18457
   289
  by (erule iffD2)
paulson@15411
   290
wenzelm@21504
   291
lemma iffD1: "Q = P \<Longrightarrow> Q \<Longrightarrow> P"
wenzelm@21504
   292
  by (drule sym) (rule iffD2)
wenzelm@21504
   293
wenzelm@21504
   294
lemma rev_iffD1: "Q \<Longrightarrow> Q = P \<Longrightarrow> P"
wenzelm@21504
   295
  by (drule sym) (rule rev_iffD2)
paulson@15411
   296
paulson@15411
   297
lemma iffE:
paulson@15411
   298
  assumes major: "P=Q"
wenzelm@21504
   299
    and minor: "[| P --> Q; Q --> P |] ==> R"
wenzelm@18457
   300
  shows R
wenzelm@18457
   301
  by (iprover intro: minor impI major [THEN iffD2] major [THEN iffD1])
paulson@15411
   302
paulson@15411
   303
haftmann@20944
   304
subsubsection {*True*}
paulson@15411
   305
paulson@15411
   306
lemma TrueI: "True"
wenzelm@21504
   307
  unfolding True_def by (rule refl)
paulson@15411
   308
wenzelm@21504
   309
lemma eqTrueI: "P ==> P = True"
wenzelm@18457
   310
  by (iprover intro: iffI TrueI)
paulson@15411
   311
wenzelm@21504
   312
lemma eqTrueE: "P = True ==> P"
wenzelm@21504
   313
  by (erule iffD2) (rule TrueI)
paulson@15411
   314
paulson@15411
   315
haftmann@20944
   316
subsubsection {*Universal quantifier*}
paulson@15411
   317
wenzelm@21504
   318
lemma allI: assumes "!!x::'a. P(x)" shows "ALL x. P(x)"
wenzelm@21504
   319
  unfolding All_def by (iprover intro: ext eqTrueI assms)
paulson@15411
   320
paulson@15411
   321
lemma spec: "ALL x::'a. P(x) ==> P(x)"
paulson@15411
   322
apply (unfold All_def)
paulson@15411
   323
apply (rule eqTrueE)
paulson@15411
   324
apply (erule fun_cong)
paulson@15411
   325
done
paulson@15411
   326
paulson@15411
   327
lemma allE:
paulson@15411
   328
  assumes major: "ALL x. P(x)"
wenzelm@21504
   329
    and minor: "P(x) ==> R"
wenzelm@21504
   330
  shows R
wenzelm@21504
   331
  by (iprover intro: minor major [THEN spec])
paulson@15411
   332
paulson@15411
   333
lemma all_dupE:
paulson@15411
   334
  assumes major: "ALL x. P(x)"
wenzelm@21504
   335
    and minor: "[| P(x); ALL x. P(x) |] ==> R"
wenzelm@21504
   336
  shows R
wenzelm@21504
   337
  by (iprover intro: minor major major [THEN spec])
paulson@15411
   338
paulson@15411
   339
wenzelm@21504
   340
subsubsection {* False *}
wenzelm@21504
   341
wenzelm@21504
   342
text {*
wenzelm@21504
   343
  Depends upon @{text spec}; it is impossible to do propositional
wenzelm@21504
   344
  logic before quantifiers!
wenzelm@21504
   345
*}
paulson@15411
   346
paulson@15411
   347
lemma FalseE: "False ==> P"
wenzelm@21504
   348
  apply (unfold False_def)
wenzelm@21504
   349
  apply (erule spec)
wenzelm@21504
   350
  done
paulson@15411
   351
wenzelm@21504
   352
lemma False_neq_True: "False = True ==> P"
wenzelm@21504
   353
  by (erule eqTrueE [THEN FalseE])
paulson@15411
   354
paulson@15411
   355
wenzelm@21504
   356
subsubsection {* Negation *}
paulson@15411
   357
paulson@15411
   358
lemma notI:
wenzelm@21504
   359
  assumes "P ==> False"
paulson@15411
   360
  shows "~P"
wenzelm@21504
   361
  apply (unfold not_def)
wenzelm@21504
   362
  apply (iprover intro: impI assms)
wenzelm@21504
   363
  done
paulson@15411
   364
paulson@15411
   365
lemma False_not_True: "False ~= True"
wenzelm@21504
   366
  apply (rule notI)
wenzelm@21504
   367
  apply (erule False_neq_True)
wenzelm@21504
   368
  done
paulson@15411
   369
paulson@15411
   370
lemma True_not_False: "True ~= False"
wenzelm@21504
   371
  apply (rule notI)
wenzelm@21504
   372
  apply (drule sym)
wenzelm@21504
   373
  apply (erule False_neq_True)
wenzelm@21504
   374
  done
paulson@15411
   375
paulson@15411
   376
lemma notE: "[| ~P;  P |] ==> R"
wenzelm@21504
   377
  apply (unfold not_def)
wenzelm@21504
   378
  apply (erule mp [THEN FalseE])
wenzelm@21504
   379
  apply assumption
wenzelm@21504
   380
  done
paulson@15411
   381
wenzelm@21504
   382
lemma notI2: "(P \<Longrightarrow> \<not> Pa) \<Longrightarrow> (P \<Longrightarrow> Pa) \<Longrightarrow> \<not> P"
wenzelm@21504
   383
  by (erule notE [THEN notI]) (erule meta_mp)
paulson@15411
   384
paulson@15411
   385
haftmann@20944
   386
subsubsection {*Implication*}
paulson@15411
   387
paulson@15411
   388
lemma impE:
paulson@15411
   389
  assumes "P-->Q" "P" "Q ==> R"
paulson@15411
   390
  shows "R"
wenzelm@23553
   391
by (iprover intro: assms mp)
paulson@15411
   392
paulson@15411
   393
(* Reduces Q to P-->Q, allowing substitution in P. *)
paulson@15411
   394
lemma rev_mp: "[| P;  P --> Q |] ==> Q"
nipkow@17589
   395
by (iprover intro: mp)
paulson@15411
   396
paulson@15411
   397
lemma contrapos_nn:
paulson@15411
   398
  assumes major: "~Q"
paulson@15411
   399
      and minor: "P==>Q"
paulson@15411
   400
  shows "~P"
nipkow@17589
   401
by (iprover intro: notI minor major [THEN notE])
paulson@15411
   402
paulson@15411
   403
(*not used at all, but we already have the other 3 combinations *)
paulson@15411
   404
lemma contrapos_pn:
paulson@15411
   405
  assumes major: "Q"
paulson@15411
   406
      and minor: "P ==> ~Q"
paulson@15411
   407
  shows "~P"
nipkow@17589
   408
by (iprover intro: notI minor major notE)
paulson@15411
   409
paulson@15411
   410
lemma not_sym: "t ~= s ==> s ~= t"
haftmann@21250
   411
  by (erule contrapos_nn) (erule sym)
haftmann@21250
   412
haftmann@21250
   413
lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
haftmann@21250
   414
  by (erule subst, erule ssubst, assumption)
paulson@15411
   415
paulson@15411
   416
haftmann@20944
   417
subsubsection {*Existential quantifier*}
paulson@15411
   418
paulson@15411
   419
lemma exI: "P x ==> EX x::'a. P x"
paulson@15411
   420
apply (unfold Ex_def)
nipkow@17589
   421
apply (iprover intro: allI allE impI mp)
paulson@15411
   422
done
paulson@15411
   423
paulson@15411
   424
lemma exE:
paulson@15411
   425
  assumes major: "EX x::'a. P(x)"
paulson@15411
   426
      and minor: "!!x. P(x) ==> Q"
paulson@15411
   427
  shows "Q"
paulson@15411
   428
apply (rule major [unfolded Ex_def, THEN spec, THEN mp])
nipkow@17589
   429
apply (iprover intro: impI [THEN allI] minor)
paulson@15411
   430
done
paulson@15411
   431
paulson@15411
   432
haftmann@20944
   433
subsubsection {*Conjunction*}
paulson@15411
   434
paulson@15411
   435
lemma conjI: "[| P; Q |] ==> P&Q"
paulson@15411
   436
apply (unfold and_def)
nipkow@17589
   437
apply (iprover intro: impI [THEN allI] mp)
paulson@15411
   438
done
paulson@15411
   439
paulson@15411
   440
lemma conjunct1: "[| P & Q |] ==> P"
paulson@15411
   441
apply (unfold and_def)
nipkow@17589
   442
apply (iprover intro: impI dest: spec mp)
paulson@15411
   443
done
paulson@15411
   444
paulson@15411
   445
lemma conjunct2: "[| P & Q |] ==> Q"
paulson@15411
   446
apply (unfold and_def)
nipkow@17589
   447
apply (iprover intro: impI dest: spec mp)
paulson@15411
   448
done
paulson@15411
   449
paulson@15411
   450
lemma conjE:
paulson@15411
   451
  assumes major: "P&Q"
paulson@15411
   452
      and minor: "[| P; Q |] ==> R"
paulson@15411
   453
  shows "R"
paulson@15411
   454
apply (rule minor)
paulson@15411
   455
apply (rule major [THEN conjunct1])
paulson@15411
   456
apply (rule major [THEN conjunct2])
paulson@15411
   457
done
paulson@15411
   458
paulson@15411
   459
lemma context_conjI:
wenzelm@23553
   460
  assumes "P" "P ==> Q" shows "P & Q"
wenzelm@23553
   461
by (iprover intro: conjI assms)
paulson@15411
   462
paulson@15411
   463
haftmann@20944
   464
subsubsection {*Disjunction*}
paulson@15411
   465
paulson@15411
   466
lemma disjI1: "P ==> P|Q"
paulson@15411
   467
apply (unfold or_def)
nipkow@17589
   468
apply (iprover intro: allI impI mp)
paulson@15411
   469
done
paulson@15411
   470
paulson@15411
   471
lemma disjI2: "Q ==> P|Q"
paulson@15411
   472
apply (unfold or_def)
nipkow@17589
   473
apply (iprover intro: allI impI mp)
paulson@15411
   474
done
paulson@15411
   475
paulson@15411
   476
lemma disjE:
paulson@15411
   477
  assumes major: "P|Q"
paulson@15411
   478
      and minorP: "P ==> R"
paulson@15411
   479
      and minorQ: "Q ==> R"
paulson@15411
   480
  shows "R"
nipkow@17589
   481
by (iprover intro: minorP minorQ impI
paulson@15411
   482
                 major [unfolded or_def, THEN spec, THEN mp, THEN mp])
paulson@15411
   483
paulson@15411
   484
haftmann@20944
   485
subsubsection {*Classical logic*}
paulson@15411
   486
paulson@15411
   487
lemma classical:
paulson@15411
   488
  assumes prem: "~P ==> P"
paulson@15411
   489
  shows "P"
paulson@15411
   490
apply (rule True_or_False [THEN disjE, THEN eqTrueE])
paulson@15411
   491
apply assumption
paulson@15411
   492
apply (rule notI [THEN prem, THEN eqTrueI])
paulson@15411
   493
apply (erule subst)
paulson@15411
   494
apply assumption
paulson@15411
   495
done
paulson@15411
   496
wenzelm@45607
   497
lemmas ccontr = FalseE [THEN classical]
paulson@15411
   498
paulson@15411
   499
(*notE with premises exchanged; it discharges ~R so that it can be used to
paulson@15411
   500
  make elimination rules*)
paulson@15411
   501
lemma rev_notE:
paulson@15411
   502
  assumes premp: "P"
paulson@15411
   503
      and premnot: "~R ==> ~P"
paulson@15411
   504
  shows "R"
paulson@15411
   505
apply (rule ccontr)
paulson@15411
   506
apply (erule notE [OF premnot premp])
paulson@15411
   507
done
paulson@15411
   508
paulson@15411
   509
(*Double negation law*)
paulson@15411
   510
lemma notnotD: "~~P ==> P"
paulson@15411
   511
apply (rule classical)
paulson@15411
   512
apply (erule notE)
paulson@15411
   513
apply assumption
paulson@15411
   514
done
paulson@15411
   515
paulson@15411
   516
lemma contrapos_pp:
paulson@15411
   517
  assumes p1: "Q"
paulson@15411
   518
      and p2: "~P ==> ~Q"
paulson@15411
   519
  shows "P"
nipkow@17589
   520
by (iprover intro: classical p1 p2 notE)
paulson@15411
   521
paulson@15411
   522
haftmann@20944
   523
subsubsection {*Unique existence*}
paulson@15411
   524
paulson@15411
   525
lemma ex1I:
wenzelm@23553
   526
  assumes "P a" "!!x. P(x) ==> x=a"
paulson@15411
   527
  shows "EX! x. P(x)"
wenzelm@23553
   528
by (unfold Ex1_def, iprover intro: assms exI conjI allI impI)
paulson@15411
   529
paulson@15411
   530
text{*Sometimes easier to use: the premises have no shared variables.  Safe!*}
paulson@15411
   531
lemma ex_ex1I:
paulson@15411
   532
  assumes ex_prem: "EX x. P(x)"
paulson@15411
   533
      and eq: "!!x y. [| P(x); P(y) |] ==> x=y"
paulson@15411
   534
  shows "EX! x. P(x)"
nipkow@17589
   535
by (iprover intro: ex_prem [THEN exE] ex1I eq)
paulson@15411
   536
paulson@15411
   537
lemma ex1E:
paulson@15411
   538
  assumes major: "EX! x. P(x)"
paulson@15411
   539
      and minor: "!!x. [| P(x);  ALL y. P(y) --> y=x |] ==> R"
paulson@15411
   540
  shows "R"
paulson@15411
   541
apply (rule major [unfolded Ex1_def, THEN exE])
paulson@15411
   542
apply (erule conjE)
nipkow@17589
   543
apply (iprover intro: minor)
paulson@15411
   544
done
paulson@15411
   545
paulson@15411
   546
lemma ex1_implies_ex: "EX! x. P x ==> EX x. P x"
paulson@15411
   547
apply (erule ex1E)
paulson@15411
   548
apply (rule exI)
paulson@15411
   549
apply assumption
paulson@15411
   550
done
paulson@15411
   551
paulson@15411
   552
haftmann@20944
   553
subsubsection {*Classical intro rules for disjunction and existential quantifiers*}
paulson@15411
   554
paulson@15411
   555
lemma disjCI:
paulson@15411
   556
  assumes "~Q ==> P" shows "P|Q"
paulson@15411
   557
apply (rule classical)
wenzelm@23553
   558
apply (iprover intro: assms disjI1 disjI2 notI elim: notE)
paulson@15411
   559
done
paulson@15411
   560
paulson@15411
   561
lemma excluded_middle: "~P | P"
nipkow@17589
   562
by (iprover intro: disjCI)
paulson@15411
   563
haftmann@20944
   564
text {*
haftmann@20944
   565
  case distinction as a natural deduction rule.
haftmann@20944
   566
  Note that @{term "~P"} is the second case, not the first
haftmann@20944
   567
*}
wenzelm@27126
   568
lemma case_split [case_names True False]:
paulson@15411
   569
  assumes prem1: "P ==> Q"
paulson@15411
   570
      and prem2: "~P ==> Q"
paulson@15411
   571
  shows "Q"
paulson@15411
   572
apply (rule excluded_middle [THEN disjE])
paulson@15411
   573
apply (erule prem2)
paulson@15411
   574
apply (erule prem1)
paulson@15411
   575
done
wenzelm@27126
   576
paulson@15411
   577
(*Classical implies (-->) elimination. *)
paulson@15411
   578
lemma impCE:
paulson@15411
   579
  assumes major: "P-->Q"
paulson@15411
   580
      and minor: "~P ==> R" "Q ==> R"
paulson@15411
   581
  shows "R"
paulson@15411
   582
apply (rule excluded_middle [of P, THEN disjE])
nipkow@17589
   583
apply (iprover intro: minor major [THEN mp])+
paulson@15411
   584
done
paulson@15411
   585
paulson@15411
   586
(*This version of --> elimination works on Q before P.  It works best for
paulson@15411
   587
  those cases in which P holds "almost everywhere".  Can't install as
paulson@15411
   588
  default: would break old proofs.*)
paulson@15411
   589
lemma impCE':
paulson@15411
   590
  assumes major: "P-->Q"
paulson@15411
   591
      and minor: "Q ==> R" "~P ==> R"
paulson@15411
   592
  shows "R"
paulson@15411
   593
apply (rule excluded_middle [of P, THEN disjE])
nipkow@17589
   594
apply (iprover intro: minor major [THEN mp])+
paulson@15411
   595
done
paulson@15411
   596
paulson@15411
   597
(*Classical <-> elimination. *)
paulson@15411
   598
lemma iffCE:
paulson@15411
   599
  assumes major: "P=Q"
paulson@15411
   600
      and minor: "[| P; Q |] ==> R"  "[| ~P; ~Q |] ==> R"
paulson@15411
   601
  shows "R"
paulson@15411
   602
apply (rule major [THEN iffE])
nipkow@17589
   603
apply (iprover intro: minor elim: impCE notE)
paulson@15411
   604
done
paulson@15411
   605
paulson@15411
   606
lemma exCI:
paulson@15411
   607
  assumes "ALL x. ~P(x) ==> P(a)"
paulson@15411
   608
  shows "EX x. P(x)"
paulson@15411
   609
apply (rule ccontr)
wenzelm@23553
   610
apply (iprover intro: assms exI allI notI notE [of "\<exists>x. P x"])
paulson@15411
   611
done
paulson@15411
   612
paulson@15411
   613
wenzelm@12386
   614
subsubsection {* Intuitionistic Reasoning *}
wenzelm@12386
   615
wenzelm@12386
   616
lemma impE':
wenzelm@12937
   617
  assumes 1: "P --> Q"
wenzelm@12937
   618
    and 2: "Q ==> R"
wenzelm@12937
   619
    and 3: "P --> Q ==> P"
wenzelm@12937
   620
  shows R
wenzelm@12386
   621
proof -
wenzelm@12386
   622
  from 3 and 1 have P .
wenzelm@12386
   623
  with 1 have Q by (rule impE)
wenzelm@12386
   624
  with 2 show R .
wenzelm@12386
   625
qed
wenzelm@12386
   626
wenzelm@12386
   627
lemma allE':
wenzelm@12937
   628
  assumes 1: "ALL x. P x"
wenzelm@12937
   629
    and 2: "P x ==> ALL x. P x ==> Q"
wenzelm@12937
   630
  shows Q
wenzelm@12386
   631
proof -
wenzelm@12386
   632
  from 1 have "P x" by (rule spec)
wenzelm@12386
   633
  from this and 1 show Q by (rule 2)
wenzelm@12386
   634
qed
wenzelm@12386
   635
wenzelm@12937
   636
lemma notE':
wenzelm@12937
   637
  assumes 1: "~ P"
wenzelm@12937
   638
    and 2: "~ P ==> P"
wenzelm@12937
   639
  shows R
wenzelm@12386
   640
proof -
wenzelm@12386
   641
  from 2 and 1 have P .
wenzelm@12386
   642
  with 1 show R by (rule notE)
wenzelm@12386
   643
qed
wenzelm@12386
   644
dixon@22444
   645
lemma TrueE: "True ==> P ==> P" .
dixon@22444
   646
lemma notFalseE: "~ False ==> P ==> P" .
dixon@22444
   647
dixon@22467
   648
lemmas [Pure.elim!] = disjE iffE FalseE conjE exE TrueE notFalseE
wenzelm@15801
   649
  and [Pure.intro!] = iffI conjI impI TrueI notI allI refl
wenzelm@15801
   650
  and [Pure.elim 2] = allE notE' impE'
wenzelm@15801
   651
  and [Pure.intro] = exI disjI2 disjI1
wenzelm@12386
   652
wenzelm@12386
   653
lemmas [trans] = trans
wenzelm@12386
   654
  and [sym] = sym not_sym
wenzelm@15801
   655
  and [Pure.elim?] = iffD1 iffD2 impE
wenzelm@11750
   656
wenzelm@11438
   657
wenzelm@11750
   658
subsubsection {* Atomizing meta-level connectives *}
wenzelm@11750
   659
haftmann@28513
   660
axiomatization where
haftmann@28513
   661
  eq_reflection: "x = y \<Longrightarrow> x \<equiv> y" (*admissible axiom*)
haftmann@28513
   662
wenzelm@11750
   663
lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
wenzelm@12003
   664
proof
wenzelm@9488
   665
  assume "!!x. P x"
wenzelm@23389
   666
  then show "ALL x. P x" ..
wenzelm@9488
   667
next
wenzelm@9488
   668
  assume "ALL x. P x"
wenzelm@23553
   669
  then show "!!x. P x" by (rule allE)
wenzelm@9488
   670
qed
wenzelm@9488
   671
wenzelm@11750
   672
lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
wenzelm@12003
   673
proof
wenzelm@9488
   674
  assume r: "A ==> B"
wenzelm@10383
   675
  show "A --> B" by (rule impI) (rule r)
wenzelm@9488
   676
next
wenzelm@9488
   677
  assume "A --> B" and A
wenzelm@23553
   678
  then show B by (rule mp)
wenzelm@9488
   679
qed
wenzelm@9488
   680
paulson@14749
   681
lemma atomize_not: "(A ==> False) == Trueprop (~A)"
paulson@14749
   682
proof
paulson@14749
   683
  assume r: "A ==> False"
paulson@14749
   684
  show "~A" by (rule notI) (rule r)
paulson@14749
   685
next
paulson@14749
   686
  assume "~A" and A
wenzelm@23553
   687
  then show False by (rule notE)
paulson@14749
   688
qed
paulson@14749
   689
haftmann@39566
   690
lemma atomize_eq [atomize, code]: "(x == y) == Trueprop (x = y)"
wenzelm@12003
   691
proof
wenzelm@10432
   692
  assume "x == y"
wenzelm@23553
   693
  show "x = y" by (unfold `x == y`) (rule refl)
wenzelm@10432
   694
next
wenzelm@10432
   695
  assume "x = y"
wenzelm@23553
   696
  then show "x == y" by (rule eq_reflection)
wenzelm@10432
   697
qed
wenzelm@10432
   698
wenzelm@28856
   699
lemma atomize_conj [atomize]: "(A &&& B) == Trueprop (A & B)"
wenzelm@12003
   700
proof
wenzelm@28856
   701
  assume conj: "A &&& B"
wenzelm@19121
   702
  show "A & B"
wenzelm@19121
   703
  proof (rule conjI)
wenzelm@19121
   704
    from conj show A by (rule conjunctionD1)
wenzelm@19121
   705
    from conj show B by (rule conjunctionD2)
wenzelm@19121
   706
  qed
wenzelm@11953
   707
next
wenzelm@19121
   708
  assume conj: "A & B"
wenzelm@28856
   709
  show "A &&& B"
wenzelm@19121
   710
  proof -
wenzelm@19121
   711
    from conj show A ..
wenzelm@19121
   712
    from conj show B ..
wenzelm@11953
   713
  qed
wenzelm@11953
   714
qed
wenzelm@11953
   715
wenzelm@12386
   716
lemmas [symmetric, rulify] = atomize_all atomize_imp
wenzelm@18832
   717
  and [symmetric, defn] = atomize_all atomize_imp atomize_eq
wenzelm@12386
   718
wenzelm@11750
   719
krauss@26580
   720
subsubsection {* Atomizing elimination rules *}
krauss@26580
   721
krauss@26580
   722
lemma atomize_exL[atomize_elim]: "(!!x. P x ==> Q) == ((EX x. P x) ==> Q)"
krauss@26580
   723
  by rule iprover+
krauss@26580
   724
krauss@26580
   725
lemma atomize_conjL[atomize_elim]: "(A ==> B ==> C) == (A & B ==> C)"
krauss@26580
   726
  by rule iprover+
krauss@26580
   727
krauss@26580
   728
lemma atomize_disjL[atomize_elim]: "((A ==> C) ==> (B ==> C) ==> C) == ((A | B ==> C) ==> C)"
krauss@26580
   729
  by rule iprover+
krauss@26580
   730
krauss@26580
   731
lemma atomize_elimL[atomize_elim]: "(!!B. (A ==> B) ==> B) == Trueprop A" ..
krauss@26580
   732
krauss@26580
   733
haftmann@20944
   734
subsection {* Package setup *}
haftmann@20944
   735
wenzelm@51314
   736
ML_file "Tools/hologic.ML"
wenzelm@51314
   737
wenzelm@51314
   738
blanchet@35828
   739
subsubsection {* Sledgehammer setup *}
blanchet@35828
   740
blanchet@35828
   741
text {*
blanchet@35828
   742
Theorems blacklisted to Sledgehammer. These theorems typically produce clauses
blanchet@35828
   743
that are prolific (match too many equality or membership literals) and relate to
blanchet@35828
   744
seldom-used facts. Some duplicate other rules.
blanchet@35828
   745
*}
blanchet@35828
   746
wenzelm@57963
   747
named_theorems no_atp "theorems that should be filtered out by Sledgehammer"
blanchet@35828
   748
blanchet@35828
   749
wenzelm@11750
   750
subsubsection {* Classical Reasoner setup *}
wenzelm@9529
   751
wenzelm@26411
   752
lemma imp_elim: "P --> Q ==> (~ R ==> P) ==> (Q ==> R) ==> R"
wenzelm@26411
   753
  by (rule classical) iprover
wenzelm@26411
   754
wenzelm@26411
   755
lemma swap: "~ P ==> (~ R ==> P) ==> R"
wenzelm@26411
   756
  by (rule classical) iprover
wenzelm@26411
   757
haftmann@20944
   758
lemma thin_refl:
haftmann@20944
   759
  "\<And>X. \<lbrakk> x=x; PROP W \<rbrakk> \<Longrightarrow> PROP W" .
haftmann@20944
   760
haftmann@21151
   761
ML {*
wenzelm@42799
   762
structure Hypsubst = Hypsubst
wenzelm@42799
   763
(
wenzelm@21218
   764
  val dest_eq = HOLogic.dest_eq
haftmann@21151
   765
  val dest_Trueprop = HOLogic.dest_Trueprop
haftmann@21151
   766
  val dest_imp = HOLogic.dest_imp
wenzelm@26411
   767
  val eq_reflection = @{thm eq_reflection}
wenzelm@26411
   768
  val rev_eq_reflection = @{thm meta_eq_to_obj_eq}
wenzelm@26411
   769
  val imp_intr = @{thm impI}
wenzelm@26411
   770
  val rev_mp = @{thm rev_mp}
wenzelm@26411
   771
  val subst = @{thm subst}
wenzelm@26411
   772
  val sym = @{thm sym}
wenzelm@22129
   773
  val thin_refl = @{thm thin_refl};
wenzelm@42799
   774
);
wenzelm@21671
   775
open Hypsubst;
haftmann@21151
   776
wenzelm@42799
   777
structure Classical = Classical
wenzelm@42799
   778
(
wenzelm@26411
   779
  val imp_elim = @{thm imp_elim}
wenzelm@26411
   780
  val not_elim = @{thm notE}
wenzelm@26411
   781
  val swap = @{thm swap}
wenzelm@26411
   782
  val classical = @{thm classical}
haftmann@21151
   783
  val sizef = Drule.size_of_thm
haftmann@21151
   784
  val hyp_subst_tacs = [Hypsubst.hyp_subst_tac]
wenzelm@42799
   785
);
haftmann@21151
   786
wenzelm@58826
   787
structure Basic_Classical: BASIC_CLASSICAL = Classical;
wenzelm@33308
   788
open Basic_Classical;
wenzelm@43560
   789
*}
wenzelm@22129
   790
haftmann@21009
   791
setup {*
wenzelm@35389
   792
  (*prevent substitution on bool*)
wenzelm@58826
   793
  let
wenzelm@58826
   794
    fun non_bool_eq (@{const_name HOL.eq}, Type (_, [T, _])) = T <> @{typ bool}
wenzelm@58826
   795
      | non_bool_eq _ = false;
wenzelm@58826
   796
    fun hyp_subst_tac' ctxt =
wenzelm@58826
   797
      SUBGOAL (fn (goal, i) =>
wenzelm@58826
   798
        if Term.exists_Const non_bool_eq goal
wenzelm@58826
   799
        then Hypsubst.hyp_subst_tac ctxt i
wenzelm@58826
   800
        else no_tac);
wenzelm@58826
   801
  in
wenzelm@58826
   802
    Context_Rules.addSWrapper (fn ctxt => fn tac => hyp_subst_tac' ctxt ORELSE' tac)
wenzelm@58826
   803
  end
haftmann@21009
   804
*}
haftmann@21009
   805
haftmann@21009
   806
declare iffI [intro!]
haftmann@21009
   807
  and notI [intro!]
haftmann@21009
   808
  and impI [intro!]
haftmann@21009
   809
  and disjCI [intro!]
haftmann@21009
   810
  and conjI [intro!]
haftmann@21009
   811
  and TrueI [intro!]
haftmann@21009
   812
  and refl [intro!]
haftmann@21009
   813
haftmann@21009
   814
declare iffCE [elim!]
haftmann@21009
   815
  and FalseE [elim!]
haftmann@21009
   816
  and impCE [elim!]
haftmann@21009
   817
  and disjE [elim!]
haftmann@21009
   818
  and conjE [elim!]
haftmann@21009
   819
haftmann@21009
   820
declare ex_ex1I [intro!]
haftmann@21009
   821
  and allI [intro!]
haftmann@21009
   822
  and exI [intro]
haftmann@21009
   823
haftmann@21009
   824
declare exE [elim!]
haftmann@21009
   825
  allE [elim]
haftmann@21009
   826
wenzelm@51687
   827
ML {* val HOL_cs = claset_of @{context} *}
mengj@19162
   828
wenzelm@20223
   829
lemma contrapos_np: "~ Q ==> (~ P ==> Q) ==> P"
wenzelm@20223
   830
  apply (erule swap)
wenzelm@20223
   831
  apply (erule (1) meta_mp)
wenzelm@20223
   832
  done
wenzelm@10383
   833
wenzelm@18689
   834
declare ex_ex1I [rule del, intro! 2]
wenzelm@18689
   835
  and ex1I [intro]
wenzelm@18689
   836
paulson@41865
   837
declare ext [intro]
paulson@41865
   838
wenzelm@12386
   839
lemmas [intro?] = ext
wenzelm@12386
   840
  and [elim?] = ex1_implies_ex
wenzelm@11977
   841
haftmann@20944
   842
(*Better then ex1E for classical reasoner: needs no quantifier duplication!*)
haftmann@20973
   843
lemma alt_ex1E [elim!]:
haftmann@20944
   844
  assumes major: "\<exists>!x. P x"
haftmann@20944
   845
      and prem: "\<And>x. \<lbrakk> P x; \<forall>y y'. P y \<and> P y' \<longrightarrow> y = y' \<rbrakk> \<Longrightarrow> R"
haftmann@20944
   846
  shows R
haftmann@20944
   847
apply (rule ex1E [OF major])
haftmann@20944
   848
apply (rule prem)
wenzelm@59499
   849
apply assumption
wenzelm@59499
   850
apply (rule allI)+
wenzelm@59970
   851
apply (tactic {* eresolve_tac @{context} [Classical.dup_elim @{context} @{thm allE}] 1 *})
wenzelm@22129
   852
apply iprover
wenzelm@22129
   853
done
haftmann@20944
   854
haftmann@21151
   855
ML {*
wenzelm@42477
   856
  structure Blast = Blast
wenzelm@42477
   857
  (
wenzelm@42477
   858
    structure Classical = Classical
wenzelm@42802
   859
    val Trueprop_const = dest_Const @{const Trueprop}
wenzelm@42477
   860
    val equality_name = @{const_name HOL.eq}
wenzelm@42477
   861
    val not_name = @{const_name Not}
wenzelm@42477
   862
    val notE = @{thm notE}
wenzelm@42477
   863
    val ccontr = @{thm ccontr}
wenzelm@42477
   864
    val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
wenzelm@42477
   865
  );
wenzelm@42477
   866
  val blast_tac = Blast.blast_tac;
haftmann@20944
   867
*}
haftmann@20944
   868
haftmann@20944
   869
lp15@59504
   870
subsubsection {*THE: definite description operator*}
lp15@59504
   871
lp15@59504
   872
lemma the_equality [intro]:
lp15@59504
   873
  assumes "P a"
lp15@59504
   874
      and "!!x. P x ==> x=a"
lp15@59504
   875
  shows "(THE x. P x) = a"
lp15@59504
   876
  by (blast intro: assms trans [OF arg_cong [where f=The] the_eq_trivial])
lp15@59504
   877
lp15@59504
   878
lemma theI:
lp15@59504
   879
  assumes "P a" and "!!x. P x ==> x=a"
lp15@59504
   880
  shows "P (THE x. P x)"
lp15@59504
   881
by (iprover intro: assms the_equality [THEN ssubst])
lp15@59504
   882
lp15@59504
   883
lemma theI': "EX! x. P x ==> P (THE x. P x)"
lp15@59504
   884
  by (blast intro: theI)
lp15@59504
   885
lp15@59504
   886
(*Easier to apply than theI: only one occurrence of P*)
lp15@59504
   887
lemma theI2:
lp15@59504
   888
  assumes "P a" "!!x. P x ==> x=a" "!!x. P x ==> Q x"
lp15@59504
   889
  shows "Q (THE x. P x)"
lp15@59504
   890
by (iprover intro: assms theI)
lp15@59504
   891
lp15@59504
   892
lemma the1I2: assumes "EX! x. P x" "\<And>x. P x \<Longrightarrow> Q x" shows "Q (THE x. P x)"
lp15@59504
   893
by(iprover intro:assms(2) theI2[where P=P and Q=Q] ex1E[OF assms(1)]
lp15@59504
   894
           elim:allE impE)
lp15@59504
   895
lp15@59504
   896
lemma the1_equality [elim?]: "[| EX!x. P x; P a |] ==> (THE x. P x) = a"
lp15@59504
   897
  by blast
lp15@59504
   898
lp15@59504
   899
lemma the_sym_eq_trivial: "(THE y. x=y) = x"
lp15@59504
   900
  by blast
lp15@59504
   901
lp15@59504
   902
haftmann@20944
   903
subsubsection {* Simplifier *}
wenzelm@12281
   904
wenzelm@12281
   905
lemma eta_contract_eq: "(%s. f s) = f" ..
wenzelm@12281
   906
wenzelm@12281
   907
lemma simp_thms:
wenzelm@12937
   908
  shows not_not: "(~ ~ P) = P"
nipkow@15354
   909
  and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
wenzelm@12937
   910
  and
berghofe@12436
   911
    "(P ~= Q) = (P = (~Q))"
berghofe@12436
   912
    "(P | ~P) = True"    "(~P | P) = True"
wenzelm@12281
   913
    "(x = x) = True"
haftmann@32068
   914
  and not_True_eq_False [code]: "(\<not> True) = False"
haftmann@32068
   915
  and not_False_eq_True [code]: "(\<not> False) = True"
haftmann@20944
   916
  and
berghofe@12436
   917
    "(~P) ~= P"  "P ~= (~P)"
haftmann@20944
   918
    "(True=P) = P"
haftmann@20944
   919
  and eq_True: "(P = True) = P"
haftmann@20944
   920
  and "(False=P) = (~P)"
haftmann@20944
   921
  and eq_False: "(P = False) = (\<not> P)"
haftmann@20944
   922
  and
wenzelm@12281
   923
    "(True --> P) = P"  "(False --> P) = True"
wenzelm@12281
   924
    "(P --> True) = True"  "(P --> P) = True"
wenzelm@12281
   925
    "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
wenzelm@12281
   926
    "(P & True) = P"  "(True & P) = P"
wenzelm@12281
   927
    "(P & False) = False"  "(False & P) = False"
wenzelm@12281
   928
    "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
wenzelm@12281
   929
    "(P & ~P) = False"    "(~P & P) = False"
wenzelm@12281
   930
    "(P | True) = True"  "(True | P) = True"
wenzelm@12281
   931
    "(P | False) = P"  "(False | P) = P"
berghofe@12436
   932
    "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
wenzelm@12281
   933
    "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
nipkow@31166
   934
  and
wenzelm@12281
   935
    "!!P. (EX x. x=t & P(x)) = P(t)"
wenzelm@12281
   936
    "!!P. (EX x. t=x & P(x)) = P(t)"
wenzelm@12281
   937
    "!!P. (ALL x. x=t --> P(x)) = P(t)"
wenzelm@12937
   938
    "!!P. (ALL x. t=x --> P(x)) = P(t)"
nipkow@17589
   939
  by (blast, blast, blast, blast, blast, iprover+)
wenzelm@13421
   940
paulson@14201
   941
lemma disj_absorb: "(A | A) = A"
paulson@14201
   942
  by blast
paulson@14201
   943
paulson@14201
   944
lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
paulson@14201
   945
  by blast
paulson@14201
   946
paulson@14201
   947
lemma conj_absorb: "(A & A) = A"
paulson@14201
   948
  by blast
paulson@14201
   949
paulson@14201
   950
lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
paulson@14201
   951
  by blast
paulson@14201
   952
wenzelm@12281
   953
lemma eq_ac:
haftmann@57512
   954
  shows eq_commute: "a = b \<longleftrightarrow> b = a"
haftmann@57512
   955
    and iff_left_commute: "(P \<longleftrightarrow> (Q \<longleftrightarrow> R)) \<longleftrightarrow> (Q \<longleftrightarrow> (P \<longleftrightarrow> R))"
haftmann@57512
   956
    and iff_assoc: "((P \<longleftrightarrow> Q) \<longleftrightarrow> R) \<longleftrightarrow> (P \<longleftrightarrow> (Q \<longleftrightarrow> R))" by (iprover, blast+)
haftmann@57512
   957
lemma neq_commute: "a \<noteq> b \<longleftrightarrow> b \<noteq> a" by iprover
wenzelm@12281
   958
wenzelm@12281
   959
lemma conj_comms:
wenzelm@12937
   960
  shows conj_commute: "(P&Q) = (Q&P)"
nipkow@17589
   961
    and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
nipkow@17589
   962
lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
wenzelm@12281
   963
paulson@19174
   964
lemmas conj_ac = conj_commute conj_left_commute conj_assoc
paulson@19174
   965
wenzelm@12281
   966
lemma disj_comms:
wenzelm@12937
   967
  shows disj_commute: "(P|Q) = (Q|P)"
nipkow@17589
   968
    and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
nipkow@17589
   969
lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
wenzelm@12281
   970
paulson@19174
   971
lemmas disj_ac = disj_commute disj_left_commute disj_assoc
paulson@19174
   972
nipkow@17589
   973
lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
nipkow@17589
   974
lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
wenzelm@12281
   975
nipkow@17589
   976
lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
nipkow@17589
   977
lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
wenzelm@12281
   978
nipkow@17589
   979
lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
nipkow@17589
   980
lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by iprover
nipkow@17589
   981
lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
wenzelm@12281
   982
wenzelm@12281
   983
text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
wenzelm@12281
   984
lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
wenzelm@12281
   985
lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
wenzelm@12281
   986
wenzelm@12281
   987
lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
wenzelm@12281
   988
lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
wenzelm@12281
   989
haftmann@21151
   990
lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
haftmann@21151
   991
  by iprover
haftmann@21151
   992
nipkow@17589
   993
lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
wenzelm@12281
   994
lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
wenzelm@12281
   995
lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
wenzelm@12281
   996
lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
wenzelm@12281
   997
lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
wenzelm@12281
   998
lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
wenzelm@12281
   999
  by blast
wenzelm@12281
  1000
lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
wenzelm@12281
  1001
nipkow@17589
  1002
lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
wenzelm@12281
  1003
wenzelm@12281
  1004
wenzelm@12281
  1005
lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
wenzelm@12281
  1006
  -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
wenzelm@12281
  1007
  -- {* cases boil down to the same thing. *}
wenzelm@12281
  1008
  by blast
wenzelm@12281
  1009
wenzelm@12281
  1010
lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
wenzelm@12281
  1011
lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
nipkow@17589
  1012
lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
nipkow@17589
  1013
lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
chaieb@23403
  1014
lemma all_not_ex: "(ALL x. P x) = (~ (EX x. ~ P x ))" by blast
wenzelm@12281
  1015
blanchet@35828
  1016
declare All_def [no_atp]
paulson@24286
  1017
nipkow@17589
  1018
lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
nipkow@17589
  1019
lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
wenzelm@12281
  1020
wenzelm@12281
  1021
text {*
wenzelm@12281
  1022
  \medskip The @{text "&"} congruence rule: not included by default!
wenzelm@12281
  1023
  May slow rewrite proofs down by as much as 50\% *}
wenzelm@12281
  1024
wenzelm@12281
  1025
lemma conj_cong:
wenzelm@12281
  1026
    "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
nipkow@17589
  1027
  by iprover
wenzelm@12281
  1028
wenzelm@12281
  1029
lemma rev_conj_cong:
wenzelm@12281
  1030
    "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
nipkow@17589
  1031
  by iprover
wenzelm@12281
  1032
wenzelm@12281
  1033
text {* The @{text "|"} congruence rule: not included by default! *}
wenzelm@12281
  1034
wenzelm@12281
  1035
lemma disj_cong:
wenzelm@12281
  1036
    "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
wenzelm@12281
  1037
  by blast
wenzelm@12281
  1038
wenzelm@12281
  1039
wenzelm@12281
  1040
text {* \medskip if-then-else rules *}
wenzelm@12281
  1041
haftmann@32068
  1042
lemma if_True [code]: "(if True then x else y) = x"
haftmann@38525
  1043
  by (unfold If_def) blast
wenzelm@12281
  1044
haftmann@32068
  1045
lemma if_False [code]: "(if False then x else y) = y"
haftmann@38525
  1046
  by (unfold If_def) blast
wenzelm@12281
  1047
wenzelm@12281
  1048
lemma if_P: "P ==> (if P then x else y) = x"
haftmann@38525
  1049
  by (unfold If_def) blast
wenzelm@12281
  1050
wenzelm@12281
  1051
lemma if_not_P: "~P ==> (if P then x else y) = y"
haftmann@38525
  1052
  by (unfold If_def) blast
wenzelm@12281
  1053
wenzelm@12281
  1054
lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
wenzelm@12281
  1055
  apply (rule case_split [of Q])
paulson@15481
  1056
   apply (simplesubst if_P)
paulson@15481
  1057
    prefer 3 apply (simplesubst if_not_P, blast+)
wenzelm@12281
  1058
  done
wenzelm@12281
  1059
wenzelm@12281
  1060
lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
paulson@15481
  1061
by (simplesubst split_if, blast)
wenzelm@12281
  1062
blanchet@35828
  1063
lemmas if_splits [no_atp] = split_if split_if_asm
wenzelm@12281
  1064
wenzelm@12281
  1065
lemma if_cancel: "(if c then x else x) = x"
paulson@15481
  1066
by (simplesubst split_if, blast)
wenzelm@12281
  1067
wenzelm@12281
  1068
lemma if_eq_cancel: "(if x = y then y else x) = x"
paulson@15481
  1069
by (simplesubst split_if, blast)
wenzelm@12281
  1070
blanchet@41792
  1071
lemma if_bool_eq_conj:
blanchet@41792
  1072
"(if P then Q else R) = ((P-->Q) & (~P-->R))"
wenzelm@19796
  1073
  -- {* This form is useful for expanding @{text "if"}s on the RIGHT of the @{text "==>"} symbol. *}
wenzelm@12281
  1074
  by (rule split_if)
wenzelm@12281
  1075
wenzelm@12281
  1076
lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
wenzelm@19796
  1077
  -- {* And this form is useful for expanding @{text "if"}s on the LEFT. *}
lp15@59504
  1078
  by (simplesubst split_if) blast
wenzelm@12281
  1079
nipkow@17589
  1080
lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
nipkow@17589
  1081
lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
wenzelm@12281
  1082
schirmer@15423
  1083
text {* \medskip let rules for simproc *}
schirmer@15423
  1084
schirmer@15423
  1085
lemma Let_folded: "f x \<equiv> g x \<Longrightarrow>  Let x f \<equiv> Let x g"
schirmer@15423
  1086
  by (unfold Let_def)
schirmer@15423
  1087
schirmer@15423
  1088
lemma Let_unfold: "f x \<equiv> g \<Longrightarrow>  Let x f \<equiv> g"
schirmer@15423
  1089
  by (unfold Let_def)
schirmer@15423
  1090
berghofe@16633
  1091
text {*
ballarin@16999
  1092
  The following copy of the implication operator is useful for
ballarin@16999
  1093
  fine-tuning congruence rules.  It instructs the simplifier to simplify
ballarin@16999
  1094
  its premise.
berghofe@16633
  1095
*}
berghofe@16633
  1096
haftmann@35416
  1097
definition simp_implies :: "[prop, prop] => prop"  (infixr "=simp=>" 1) where
haftmann@37767
  1098
  "simp_implies \<equiv> op ==>"
berghofe@16633
  1099
wenzelm@18457
  1100
lemma simp_impliesI:
berghofe@16633
  1101
  assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
berghofe@16633
  1102
  shows "PROP P =simp=> PROP Q"
berghofe@16633
  1103
  apply (unfold simp_implies_def)
berghofe@16633
  1104
  apply (rule PQ)
berghofe@16633
  1105
  apply assumption
berghofe@16633
  1106
  done
berghofe@16633
  1107
berghofe@16633
  1108
lemma simp_impliesE:
wenzelm@25388
  1109
  assumes PQ: "PROP P =simp=> PROP Q"
berghofe@16633
  1110
  and P: "PROP P"
berghofe@16633
  1111
  and QR: "PROP Q \<Longrightarrow> PROP R"
berghofe@16633
  1112
  shows "PROP R"
berghofe@16633
  1113
  apply (rule QR)
berghofe@16633
  1114
  apply (rule PQ [unfolded simp_implies_def])
berghofe@16633
  1115
  apply (rule P)
berghofe@16633
  1116
  done
berghofe@16633
  1117
berghofe@16633
  1118
lemma simp_implies_cong:
berghofe@16633
  1119
  assumes PP' :"PROP P == PROP P'"
berghofe@16633
  1120
  and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
berghofe@16633
  1121
  shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
berghofe@16633
  1122
proof (unfold simp_implies_def, rule equal_intr_rule)
berghofe@16633
  1123
  assume PQ: "PROP P \<Longrightarrow> PROP Q"
berghofe@16633
  1124
  and P': "PROP P'"
berghofe@16633
  1125
  from PP' [symmetric] and P' have "PROP P"
berghofe@16633
  1126
    by (rule equal_elim_rule1)
wenzelm@23553
  1127
  then have "PROP Q" by (rule PQ)
berghofe@16633
  1128
  with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
berghofe@16633
  1129
next
berghofe@16633
  1130
  assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
berghofe@16633
  1131
  and P: "PROP P"
berghofe@16633
  1132
  from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
wenzelm@23553
  1133
  then have "PROP Q'" by (rule P'Q')
berghofe@16633
  1134
  with P'QQ' [OF P', symmetric] show "PROP Q"
berghofe@16633
  1135
    by (rule equal_elim_rule1)
berghofe@16633
  1136
qed
berghofe@16633
  1137
haftmann@20944
  1138
lemma uncurry:
haftmann@20944
  1139
  assumes "P \<longrightarrow> Q \<longrightarrow> R"
haftmann@20944
  1140
  shows "P \<and> Q \<longrightarrow> R"
wenzelm@23553
  1141
  using assms by blast
haftmann@20944
  1142
haftmann@20944
  1143
lemma iff_allI:
haftmann@20944
  1144
  assumes "\<And>x. P x = Q x"
haftmann@20944
  1145
  shows "(\<forall>x. P x) = (\<forall>x. Q x)"
wenzelm@23553
  1146
  using assms by blast
haftmann@20944
  1147
haftmann@20944
  1148
lemma iff_exI:
haftmann@20944
  1149
  assumes "\<And>x. P x = Q x"
haftmann@20944
  1150
  shows "(\<exists>x. P x) = (\<exists>x. Q x)"
wenzelm@23553
  1151
  using assms by blast
haftmann@20944
  1152
haftmann@20944
  1153
lemma all_comm:
haftmann@20944
  1154
  "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
haftmann@20944
  1155
  by blast
haftmann@20944
  1156
haftmann@20944
  1157
lemma ex_comm:
haftmann@20944
  1158
  "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
haftmann@20944
  1159
  by blast
haftmann@20944
  1160
wenzelm@48891
  1161
ML_file "Tools/simpdata.ML"
wenzelm@21671
  1162
ML {* open Simpdata *}
wenzelm@42455
  1163
wenzelm@58826
  1164
setup {*
wenzelm@58826
  1165
  map_theory_simpset (put_simpset HOL_basic_ss) #>
wenzelm@58826
  1166
  Simplifier.method_setup Splitter.split_modifiers
wenzelm@58826
  1167
*}
wenzelm@42455
  1168
wenzelm@42459
  1169
simproc_setup defined_Ex ("EX x. P x") = {* fn _ => Quantifier1.rearrange_ex *}
wenzelm@42459
  1170
simproc_setup defined_All ("ALL x. P x") = {* fn _ => Quantifier1.rearrange_all *}
wenzelm@21671
  1171
wenzelm@24035
  1172
text {* Simproc for proving @{text "(y = x) == False"} from premise @{text "~(x = y)"}: *}
wenzelm@24035
  1173
wenzelm@24035
  1174
simproc_setup neq ("x = y") = {* fn _ =>
wenzelm@24035
  1175
let
wenzelm@24035
  1176
  val neq_to_EQ_False = @{thm not_sym} RS @{thm Eq_FalseI};
wenzelm@24035
  1177
  fun is_neq eq lhs rhs thm =
wenzelm@24035
  1178
    (case Thm.prop_of thm of
wenzelm@24035
  1179
      _ $ (Not $ (eq' $ l' $ r')) =>
wenzelm@24035
  1180
        Not = HOLogic.Not andalso eq' = eq andalso
wenzelm@24035
  1181
        r' aconv lhs andalso l' aconv rhs
wenzelm@24035
  1182
    | _ => false);
wenzelm@24035
  1183
  fun proc ss ct =
wenzelm@24035
  1184
    (case Thm.term_of ct of
wenzelm@24035
  1185
      eq $ lhs $ rhs =>
wenzelm@43597
  1186
        (case find_first (is_neq eq lhs rhs) (Simplifier.prems_of ss) of
wenzelm@24035
  1187
          SOME thm => SOME (thm RS neq_to_EQ_False)
wenzelm@24035
  1188
        | NONE => NONE)
wenzelm@24035
  1189
     | _ => NONE);
wenzelm@24035
  1190
in proc end;
wenzelm@24035
  1191
*}
wenzelm@24035
  1192
wenzelm@24035
  1193
simproc_setup let_simp ("Let x f") = {*
wenzelm@24035
  1194
let
wenzelm@24035
  1195
  val (f_Let_unfold, x_Let_unfold) =
wenzelm@59582
  1196
    let val [(_ $ (f $ x) $ _)] = Thm.prems_of @{thm Let_unfold}
wenzelm@59628
  1197
    in apply2 (Thm.cterm_of @{context}) (f, x) end
wenzelm@24035
  1198
  val (f_Let_folded, x_Let_folded) =
wenzelm@59582
  1199
    let val [(_ $ (f $ x) $ _)] = Thm.prems_of @{thm Let_folded}
wenzelm@59628
  1200
    in apply2 (Thm.cterm_of @{context}) (f, x) end;
wenzelm@24035
  1201
  val g_Let_folded =
wenzelm@59582
  1202
    let val [(_ $ _ $ (g $ _))] = Thm.prems_of @{thm Let_folded}
wenzelm@59628
  1203
    in Thm.cterm_of @{context} g end;
haftmann@28741
  1204
  fun count_loose (Bound i) k = if i >= k then 1 else 0
haftmann@28741
  1205
    | count_loose (s $ t) k = count_loose s k + count_loose t k
haftmann@28741
  1206
    | count_loose (Abs (_, _, t)) k = count_loose  t (k + 1)
haftmann@28741
  1207
    | count_loose _ _ = 0;
haftmann@28741
  1208
  fun is_trivial_let (Const (@{const_name Let}, _) $ x $ t) =
wenzelm@59628
  1209
    (case t of
wenzelm@59628
  1210
      Abs (_, _, t') => count_loose t' 0 <= 1
wenzelm@59628
  1211
    | _ => true);
wenzelm@59628
  1212
in
wenzelm@59628
  1213
  fn _ => fn ctxt => fn ct =>
wenzelm@59628
  1214
    if is_trivial_let (Thm.term_of ct)
wenzelm@59628
  1215
    then SOME @{thm Let_def} (*no or one ocurrence of bound variable*)
wenzelm@59628
  1216
    else
wenzelm@59628
  1217
      let (*Norbert Schirmer's case*)
wenzelm@59628
  1218
        val t = Thm.term_of ct;
wenzelm@59628
  1219
        val ([t'], ctxt') = Variable.import_terms false [t] ctxt;
wenzelm@59628
  1220
      in
wenzelm@59628
  1221
        Option.map (hd o Variable.export ctxt' ctxt o single)
wenzelm@59628
  1222
          (case t' of Const (@{const_name Let},_) $ x $ f => (* x and f are already in normal form *)
wenzelm@59628
  1223
            if is_Free x orelse is_Bound x orelse is_Const x
wenzelm@59628
  1224
            then SOME @{thm Let_def}
wenzelm@59628
  1225
            else
wenzelm@59628
  1226
              let
wenzelm@59628
  1227
                val n = case f of (Abs (x, _, _)) => x | _ => "x";
wenzelm@59628
  1228
                val cx = Thm.cterm_of ctxt x;
wenzelm@59628
  1229
                val xT = Thm.typ_of_cterm cx;
wenzelm@59628
  1230
                val cf = Thm.cterm_of ctxt f;
wenzelm@59628
  1231
                val fx_g = Simplifier.rewrite ctxt (Thm.apply cf cx);
wenzelm@59628
  1232
                val (_ $ _ $ g) = Thm.prop_of fx_g;
wenzelm@59628
  1233
                val g' = abstract_over (x, g);
wenzelm@59628
  1234
                val abs_g'= Abs (n, xT, g');
wenzelm@59628
  1235
              in
wenzelm@59628
  1236
                if g aconv g' then
wenzelm@59628
  1237
                  let
wenzelm@59628
  1238
                    val rl =
wenzelm@59628
  1239
                      cterm_instantiate [(f_Let_unfold, cf), (x_Let_unfold, cx)] @{thm Let_unfold};
wenzelm@59628
  1240
                  in SOME (rl OF [fx_g]) end
wenzelm@59628
  1241
                else if (Envir.beta_eta_contract f) aconv (Envir.beta_eta_contract abs_g')
wenzelm@59628
  1242
                then NONE (*avoid identity conversion*)
wenzelm@59628
  1243
                else
wenzelm@59628
  1244
                  let
wenzelm@59628
  1245
                    val g'x = abs_g' $ x;
wenzelm@59628
  1246
                    val g_g'x = Thm.symmetric (Thm.beta_conversion false (Thm.cterm_of ctxt g'x));
wenzelm@59628
  1247
                    val rl =
wenzelm@59628
  1248
                      @{thm Let_folded} |> cterm_instantiate
wenzelm@59628
  1249
                        [(f_Let_folded, Thm.cterm_of ctxt f),
wenzelm@59628
  1250
                         (x_Let_folded, cx),
wenzelm@59628
  1251
                         (g_Let_folded, Thm.cterm_of ctxt abs_g')];
wenzelm@59628
  1252
                  in SOME (rl OF [Thm.transitive fx_g g_g'x]) end
wenzelm@59628
  1253
              end
wenzelm@59628
  1254
          | _ => NONE)
wenzelm@59628
  1255
      end
haftmann@28741
  1256
end *}
wenzelm@24035
  1257
haftmann@21151
  1258
lemma True_implies_equals: "(True \<Longrightarrow> PROP P) \<equiv> PROP P"
haftmann@21151
  1259
proof
wenzelm@23389
  1260
  assume "True \<Longrightarrow> PROP P"
wenzelm@23389
  1261
  from this [OF TrueI] show "PROP P" .
haftmann@21151
  1262
next
haftmann@21151
  1263
  assume "PROP P"
wenzelm@23389
  1264
  then show "PROP P" .
haftmann@21151
  1265
qed
haftmann@21151
  1266
nipkow@59864
  1267
lemma implies_True_equals: "(PROP P \<Longrightarrow> True) \<equiv> Trueprop True"
nipkow@59864
  1268
by default (intro TrueI)
nipkow@59864
  1269
nipkow@59864
  1270
lemma False_implies_equals: "(False \<Longrightarrow> P) \<equiv> Trueprop True"
nipkow@59864
  1271
by default simp_all
nipkow@59864
  1272
haftmann@21151
  1273
lemma ex_simps:
haftmann@21151
  1274
  "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
haftmann@21151
  1275
  "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
haftmann@21151
  1276
  "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
haftmann@21151
  1277
  "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
haftmann@21151
  1278
  "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
haftmann@21151
  1279
  "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
haftmann@21151
  1280
  -- {* Miniscoping: pushing in existential quantifiers. *}
haftmann@21151
  1281
  by (iprover | blast)+
haftmann@21151
  1282
haftmann@21151
  1283
lemma all_simps:
haftmann@21151
  1284
  "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
haftmann@21151
  1285
  "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
haftmann@21151
  1286
  "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
haftmann@21151
  1287
  "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
haftmann@21151
  1288
  "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
haftmann@21151
  1289
  "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
haftmann@21151
  1290
  -- {* Miniscoping: pushing in universal quantifiers. *}
haftmann@21151
  1291
  by (iprover | blast)+
paulson@15481
  1292
wenzelm@21671
  1293
lemmas [simp] =
wenzelm@21671
  1294
  triv_forall_equality (*prunes params*)
nipkow@60143
  1295
  True_implies_equals implies_True_equals (*prune True in asms*)
nipkow@60143
  1296
  False_implies_equals (*prune False in asms*)
wenzelm@21671
  1297
  if_True
wenzelm@21671
  1298
  if_False
wenzelm@21671
  1299
  if_cancel
wenzelm@21671
  1300
  if_eq_cancel
wenzelm@21671
  1301
  imp_disjL
haftmann@20973
  1302
  (*In general it seems wrong to add distributive laws by default: they
haftmann@20973
  1303
    might cause exponential blow-up.  But imp_disjL has been in for a while
haftmann@20973
  1304
    and cannot be removed without affecting existing proofs.  Moreover,
haftmann@20973
  1305
    rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
haftmann@20973
  1306
    grounds that it allows simplification of R in the two cases.*)
wenzelm@21671
  1307
  conj_assoc
wenzelm@21671
  1308
  disj_assoc
wenzelm@21671
  1309
  de_Morgan_conj
wenzelm@21671
  1310
  de_Morgan_disj
wenzelm@21671
  1311
  imp_disj1
wenzelm@21671
  1312
  imp_disj2
wenzelm@21671
  1313
  not_imp
wenzelm@21671
  1314
  disj_not1
wenzelm@21671
  1315
  not_all
wenzelm@21671
  1316
  not_ex
wenzelm@21671
  1317
  cases_simp
wenzelm@21671
  1318
  the_eq_trivial
wenzelm@21671
  1319
  the_sym_eq_trivial
wenzelm@21671
  1320
  ex_simps
wenzelm@21671
  1321
  all_simps
wenzelm@21671
  1322
  simp_thms
wenzelm@21671
  1323
wenzelm@21671
  1324
lemmas [cong] = imp_cong simp_implies_cong
wenzelm@21671
  1325
lemmas [split] = split_if
haftmann@20973
  1326
wenzelm@51717
  1327
ML {* val HOL_ss = simpset_of @{context} *}
haftmann@20973
  1328
haftmann@20944
  1329
text {* Simplifies x assuming c and y assuming ~c *}
haftmann@20944
  1330
lemma if_cong:
haftmann@20944
  1331
  assumes "b = c"
haftmann@20944
  1332
      and "c \<Longrightarrow> x = u"
haftmann@20944
  1333
      and "\<not> c \<Longrightarrow> y = v"
haftmann@20944
  1334
  shows "(if b then x else y) = (if c then u else v)"
haftmann@38525
  1335
  using assms by simp
haftmann@20944
  1336
haftmann@20944
  1337
text {* Prevents simplification of x and y:
haftmann@20944
  1338
  faster and allows the execution of functional programs. *}
haftmann@20944
  1339
lemma if_weak_cong [cong]:
haftmann@20944
  1340
  assumes "b = c"
haftmann@20944
  1341
  shows "(if b then x else y) = (if c then x else y)"
wenzelm@23553
  1342
  using assms by (rule arg_cong)
haftmann@20944
  1343
haftmann@20944
  1344
text {* Prevents simplification of t: much faster *}
haftmann@20944
  1345
lemma let_weak_cong:
haftmann@20944
  1346
  assumes "a = b"
haftmann@20944
  1347
  shows "(let x = a in t x) = (let x = b in t x)"
wenzelm@23553
  1348
  using assms by (rule arg_cong)
haftmann@20944
  1349
haftmann@20944
  1350
text {* To tidy up the result of a simproc.  Only the RHS will be simplified. *}
haftmann@20944
  1351
lemma eq_cong2:
haftmann@20944
  1352
  assumes "u = u'"
haftmann@20944
  1353
  shows "(t \<equiv> u) \<equiv> (t \<equiv> u')"
wenzelm@23553
  1354
  using assms by simp
haftmann@20944
  1355
haftmann@20944
  1356
lemma if_distrib:
haftmann@20944
  1357
  "f (if c then x else y) = (if c then f x else f y)"
haftmann@20944
  1358
  by simp
haftmann@20944
  1359
haftmann@44277
  1360
text{*As a simplification rule, it replaces all function equalities by
haftmann@44277
  1361
  first-order equalities.*}
haftmann@44277
  1362
lemma fun_eq_iff: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
haftmann@44277
  1363
  by auto
haftmann@44277
  1364
wenzelm@17459
  1365
haftmann@20944
  1366
subsubsection {* Generic cases and induction *}
wenzelm@17459
  1367
haftmann@20944
  1368
text {* Rule projections: *}
haftmann@20944
  1369
ML {*
wenzelm@32172
  1370
structure Project_Rule = Project_Rule
wenzelm@25388
  1371
(
wenzelm@27126
  1372
  val conjunct1 = @{thm conjunct1}
wenzelm@27126
  1373
  val conjunct2 = @{thm conjunct2}
wenzelm@27126
  1374
  val mp = @{thm mp}
wenzelm@59929
  1375
);
wenzelm@17459
  1376
*}
wenzelm@17459
  1377
wenzelm@59940
  1378
context
wenzelm@59940
  1379
begin
wenzelm@59940
  1380
wenzelm@59990
  1381
qualified definition "induct_forall P \<equiv> \<forall>x. P x"
wenzelm@59990
  1382
qualified definition "induct_implies A B \<equiv> A \<longrightarrow> B"
wenzelm@59990
  1383
qualified definition "induct_equal x y \<equiv> x = y"
wenzelm@59990
  1384
qualified definition "induct_conj A B \<equiv> A \<and> B"
wenzelm@59990
  1385
qualified definition "induct_true \<equiv> True"
wenzelm@59990
  1386
qualified definition "induct_false \<equiv> False"
haftmann@35416
  1387
wenzelm@59929
  1388
lemma induct_forall_eq: "(\<And>x. P x) \<equiv> Trueprop (induct_forall (\<lambda>x. P x))"
wenzelm@18457
  1389
  by (unfold atomize_all induct_forall_def)
wenzelm@11824
  1390
wenzelm@59929
  1391
lemma induct_implies_eq: "(A \<Longrightarrow> B) \<equiv> Trueprop (induct_implies A B)"
wenzelm@18457
  1392
  by (unfold atomize_imp induct_implies_def)
wenzelm@11824
  1393
wenzelm@59929
  1394
lemma induct_equal_eq: "(x \<equiv> y) \<equiv> Trueprop (induct_equal x y)"
wenzelm@18457
  1395
  by (unfold atomize_eq induct_equal_def)
wenzelm@18457
  1396
wenzelm@59929
  1397
lemma induct_conj_eq: "(A &&& B) \<equiv> Trueprop (induct_conj A B)"
wenzelm@18457
  1398
  by (unfold atomize_conj induct_conj_def)
wenzelm@18457
  1399
berghofe@34908
  1400
lemmas induct_atomize' = induct_forall_eq induct_implies_eq induct_conj_eq
berghofe@34908
  1401
lemmas induct_atomize = induct_atomize' induct_equal_eq
wenzelm@45607
  1402
lemmas induct_rulify' [symmetric] = induct_atomize'
wenzelm@45607
  1403
lemmas induct_rulify [symmetric] = induct_atomize
wenzelm@18457
  1404
lemmas induct_rulify_fallback =
wenzelm@18457
  1405
  induct_forall_def induct_implies_def induct_equal_def induct_conj_def
berghofe@34908
  1406
  induct_true_def induct_false_def
wenzelm@18457
  1407
wenzelm@11989
  1408
lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
wenzelm@11989
  1409
    induct_conj (induct_forall A) (induct_forall B)"
nipkow@17589
  1410
  by (unfold induct_forall_def induct_conj_def) iprover
wenzelm@11824
  1411
wenzelm@11989
  1412
lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
wenzelm@11989
  1413
    induct_conj (induct_implies C A) (induct_implies C B)"
nipkow@17589
  1414
  by (unfold induct_implies_def induct_conj_def) iprover
wenzelm@11989
  1415
wenzelm@59929
  1416
lemma induct_conj_curry: "(induct_conj A B \<Longrightarrow> PROP C) \<equiv> (A \<Longrightarrow> B \<Longrightarrow> PROP C)"
berghofe@13598
  1417
proof
wenzelm@59929
  1418
  assume r: "induct_conj A B \<Longrightarrow> PROP C"
wenzelm@59929
  1419
  assume ab: A B
wenzelm@59929
  1420
  show "PROP C" by (rule r) (simp add: induct_conj_def ab)
berghofe@13598
  1421
next
wenzelm@59929
  1422
  assume r: "A \<Longrightarrow> B \<Longrightarrow> PROP C"
wenzelm@59929
  1423
  assume ab: "induct_conj A B"
wenzelm@59929
  1424
  show "PROP C" by (rule r) (simp_all add: ab [unfolded induct_conj_def])
berghofe@13598
  1425
qed
wenzelm@11824
  1426
wenzelm@11989
  1427
lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
wenzelm@11824
  1428
berghofe@34908
  1429
lemma induct_trueI: "induct_true"
berghofe@34908
  1430
  by (simp add: induct_true_def)
wenzelm@11824
  1431
wenzelm@11824
  1432
text {* Method setup. *}
wenzelm@11824
  1433
wenzelm@58826
  1434
ML_file "~~/src/Tools/induct.ML"
wenzelm@11824
  1435
ML {*
wenzelm@32171
  1436
structure Induct = Induct
wenzelm@27126
  1437
(
wenzelm@27126
  1438
  val cases_default = @{thm case_split}
wenzelm@27126
  1439
  val atomize = @{thms induct_atomize}
berghofe@34908
  1440
  val rulify = @{thms induct_rulify'}
wenzelm@27126
  1441
  val rulify_fallback = @{thms induct_rulify_fallback}
berghofe@34988
  1442
  val equal_def = @{thm induct_equal_def}
berghofe@34908
  1443
  fun dest_def (Const (@{const_name induct_equal}, _) $ t $ u) = SOME (t, u)
berghofe@34908
  1444
    | dest_def _ = NONE
wenzelm@58957
  1445
  fun trivial_tac ctxt = match_tac ctxt @{thms induct_trueI}
wenzelm@27126
  1446
)
wenzelm@11824
  1447
*}
wenzelm@11824
  1448
wenzelm@48891
  1449
ML_file "~~/src/Tools/induction.ML"
nipkow@45014
  1450
wenzelm@59940
  1451
declaration {*
wenzelm@59940
  1452
  fn _ => Induct.map_simpset (fn ss => ss
berghofe@34908
  1453
    addsimprocs
wenzelm@38715
  1454
      [Simplifier.simproc_global @{theory} "swap_induct_false"
berghofe@34908
  1455
         ["induct_false ==> PROP P ==> PROP Q"]
wenzelm@51717
  1456
         (fn _ =>
berghofe@34908
  1457
            (fn _ $ (P as _ $ @{const induct_false}) $ (_ $ Q $ _) =>
berghofe@34908
  1458
                  if P <> Q then SOME Drule.swap_prems_eq else NONE
berghofe@34908
  1459
              | _ => NONE)),
wenzelm@38715
  1460
       Simplifier.simproc_global @{theory} "induct_equal_conj_curry"
berghofe@34908
  1461
         ["induct_conj P Q ==> PROP R"]
wenzelm@51717
  1462
         (fn _ =>
berghofe@34908
  1463
            (fn _ $ (_ $ P) $ _ =>
berghofe@34908
  1464
                let
berghofe@34908
  1465
                  fun is_conj (@{const induct_conj} $ P $ Q) =
berghofe@34908
  1466
                        is_conj P andalso is_conj Q
berghofe@34908
  1467
                    | is_conj (Const (@{const_name induct_equal}, _) $ _ $ _) = true
berghofe@34908
  1468
                    | is_conj @{const induct_true} = true
berghofe@34908
  1469
                    | is_conj @{const induct_false} = true
berghofe@34908
  1470
                    | is_conj _ = false
berghofe@34908
  1471
                in if is_conj P then SOME @{thm induct_conj_curry} else NONE end
wenzelm@45625
  1472
              | _ => NONE))]
wenzelm@54742
  1473
    |> Simplifier.set_mksimps (fn ctxt =>
wenzelm@54742
  1474
        Simpdata.mksimps Simpdata.mksimps_pairs ctxt #>
wenzelm@59940
  1475
        map (rewrite_rule ctxt (map Thm.symmetric @{thms induct_rulify_fallback}))))
berghofe@34908
  1476
*}
berghofe@34908
  1477
berghofe@34908
  1478
text {* Pre-simplification of induction and cases rules *}
berghofe@34908
  1479
wenzelm@59929
  1480
lemma [induct_simp]: "(\<And>x. induct_equal x t \<Longrightarrow> PROP P x) \<equiv> PROP P t"
berghofe@34908
  1481
  unfolding induct_equal_def
berghofe@34908
  1482
proof
wenzelm@59929
  1483
  assume r: "\<And>x. x = t \<Longrightarrow> PROP P x"
wenzelm@59929
  1484
  show "PROP P t" by (rule r [OF refl])
berghofe@34908
  1485
next
wenzelm@59929
  1486
  fix x
wenzelm@59929
  1487
  assume "PROP P t" "x = t"
berghofe@34908
  1488
  then show "PROP P x" by simp
berghofe@34908
  1489
qed
berghofe@34908
  1490
wenzelm@59929
  1491
lemma [induct_simp]: "(\<And>x. induct_equal t x \<Longrightarrow> PROP P x) \<equiv> PROP P t"
berghofe@34908
  1492
  unfolding induct_equal_def
berghofe@34908
  1493
proof
wenzelm@59929
  1494
  assume r: "\<And>x. t = x \<Longrightarrow> PROP P x"
wenzelm@59929
  1495
  show "PROP P t" by (rule r [OF refl])
berghofe@34908
  1496
next
wenzelm@59929
  1497
  fix x
wenzelm@59929
  1498
  assume "PROP P t" "t = x"
berghofe@34908
  1499
  then show "PROP P x" by simp
berghofe@34908
  1500
qed
berghofe@34908
  1501
wenzelm@59929
  1502
lemma [induct_simp]: "(induct_false \<Longrightarrow> P) \<equiv> Trueprop induct_true"
berghofe@34908
  1503
  unfolding induct_false_def induct_true_def
berghofe@34908
  1504
  by (iprover intro: equal_intr_rule)
berghofe@34908
  1505
wenzelm@59929
  1506
lemma [induct_simp]: "(induct_true \<Longrightarrow> PROP P) \<equiv> PROP P"
berghofe@34908
  1507
  unfolding induct_true_def
berghofe@34908
  1508
proof
wenzelm@59929
  1509
  assume "True \<Longrightarrow> PROP P"
wenzelm@59929
  1510
  then show "PROP P" using TrueI .
berghofe@34908
  1511
next
berghofe@34908
  1512
  assume "PROP P"
berghofe@34908
  1513
  then show "PROP P" .
berghofe@34908
  1514
qed
berghofe@34908
  1515
wenzelm@59929
  1516
lemma [induct_simp]: "(PROP P \<Longrightarrow> induct_true) \<equiv> Trueprop induct_true"
berghofe@34908
  1517
  unfolding induct_true_def
berghofe@34908
  1518
  by (iprover intro: equal_intr_rule)
berghofe@34908
  1519
wenzelm@59929
  1520
lemma [induct_simp]: "(\<And>x. induct_true) \<equiv> Trueprop induct_true"
berghofe@34908
  1521
  unfolding induct_true_def
berghofe@34908
  1522
  by (iprover intro: equal_intr_rule)
berghofe@34908
  1523
wenzelm@59929
  1524
lemma [induct_simp]: "induct_implies induct_true P \<equiv> P"
berghofe@34908
  1525
  by (simp add: induct_implies_def induct_true_def)
berghofe@34908
  1526
wenzelm@59929
  1527
lemma [induct_simp]: "x = x \<longleftrightarrow> True"
berghofe@34908
  1528
  by (rule simp_thms)
berghofe@34908
  1529
wenzelm@59940
  1530
end
wenzelm@18457
  1531
wenzelm@48891
  1532
ML_file "~~/src/Tools/induct_tacs.ML"
wenzelm@27126
  1533
haftmann@20944
  1534
berghofe@28325
  1535
subsubsection {* Coherent logic *}
berghofe@28325
  1536
wenzelm@55632
  1537
ML_file "~~/src/Tools/coherent.ML"
berghofe@28325
  1538
ML {*
wenzelm@32734
  1539
structure Coherent = Coherent
berghofe@28325
  1540
(
wenzelm@55632
  1541
  val atomize_elimL = @{thm atomize_elimL};
wenzelm@55632
  1542
  val atomize_exL = @{thm atomize_exL};
wenzelm@55632
  1543
  val atomize_conjL = @{thm atomize_conjL};
wenzelm@55632
  1544
  val atomize_disjL = @{thm atomize_disjL};
wenzelm@55632
  1545
  val operator_names = [@{const_name HOL.disj}, @{const_name HOL.conj}, @{const_name Ex}];
berghofe@28325
  1546
);
berghofe@28325
  1547
*}
berghofe@28325
  1548
berghofe@28325
  1549
huffman@31024
  1550
subsubsection {* Reorienting equalities *}
huffman@31024
  1551
huffman@31024
  1552
ML {*
huffman@31024
  1553
signature REORIENT_PROC =
huffman@31024
  1554
sig
huffman@31024
  1555
  val add : (term -> bool) -> theory -> theory
wenzelm@51717
  1556
  val proc : morphism -> Proof.context -> cterm -> thm option
huffman@31024
  1557
end;
huffman@31024
  1558
wenzelm@33523
  1559
structure Reorient_Proc : REORIENT_PROC =
huffman@31024
  1560
struct
wenzelm@33523
  1561
  structure Data = Theory_Data
huffman@31024
  1562
  (
wenzelm@33523
  1563
    type T = ((term -> bool) * stamp) list;
wenzelm@33523
  1564
    val empty = [];
huffman@31024
  1565
    val extend = I;
wenzelm@33523
  1566
    fun merge data : T = Library.merge (eq_snd op =) data;
wenzelm@33523
  1567
  );
wenzelm@33523
  1568
  fun add m = Data.map (cons (m, stamp ()));
wenzelm@33523
  1569
  fun matches thy t = exists (fn (m, _) => m t) (Data.get thy);
huffman@31024
  1570
huffman@31024
  1571
  val meta_reorient = @{thm eq_commute [THEN eq_reflection]};
wenzelm@51717
  1572
  fun proc phi ctxt ct =
huffman@31024
  1573
    let
wenzelm@42361
  1574
      val thy = Proof_Context.theory_of ctxt;
huffman@31024
  1575
    in
huffman@31024
  1576
      case Thm.term_of ct of
wenzelm@33523
  1577
        (_ $ t $ u) => if matches thy u then NONE else SOME meta_reorient
huffman@31024
  1578
      | _ => NONE
huffman@31024
  1579
    end;
huffman@31024
  1580
end;
huffman@31024
  1581
*}
huffman@31024
  1582
huffman@31024
  1583
haftmann@20944
  1584
subsection {* Other simple lemmas and lemma duplicates *}
haftmann@20944
  1585
haftmann@20944
  1586
lemma ex1_eq [iff]: "EX! x. x = t" "EX! x. t = x"
haftmann@20944
  1587
  by blast+
haftmann@20944
  1588
haftmann@20944
  1589
lemma choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
haftmann@20944
  1590
  apply (rule iffI)
haftmann@20944
  1591
  apply (rule_tac a = "%x. THE y. P x y" in ex1I)
haftmann@20944
  1592
  apply (fast dest!: theI')
huffman@44921
  1593
  apply (fast intro: the1_equality [symmetric])
haftmann@20944
  1594
  apply (erule ex1E)
haftmann@20944
  1595
  apply (rule allI)
haftmann@20944
  1596
  apply (rule ex1I)
haftmann@20944
  1597
  apply (erule spec)
haftmann@20944
  1598
  apply (erule_tac x = "%z. if z = x then y else f z" in allE)
haftmann@20944
  1599
  apply (erule impE)
haftmann@20944
  1600
  apply (rule allI)
wenzelm@27126
  1601
  apply (case_tac "xa = x")
haftmann@20944
  1602
  apply (drule_tac [3] x = x in fun_cong, simp_all)
haftmann@20944
  1603
  done
haftmann@20944
  1604
haftmann@22218
  1605
lemmas eq_sym_conv = eq_commute
haftmann@22218
  1606
chaieb@23037
  1607
lemma nnf_simps:
wenzelm@58826
  1608
  "(\<not>(P \<and> Q)) = (\<not> P \<or> \<not> Q)" "(\<not> (P \<or> Q)) = (\<not> P \<and> \<not>Q)" "(P \<longrightarrow> Q) = (\<not>P \<or> Q)"
wenzelm@58826
  1609
  "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))"
chaieb@23037
  1610
  "(\<not> \<not>(P)) = P"
chaieb@23037
  1611
by blast+
chaieb@23037
  1612
wenzelm@21671
  1613
subsection {* Basic ML bindings *}
wenzelm@21671
  1614
wenzelm@21671
  1615
ML {*
wenzelm@22129
  1616
val FalseE = @{thm FalseE}
wenzelm@22129
  1617
val Let_def = @{thm Let_def}
wenzelm@22129
  1618
val TrueI = @{thm TrueI}
wenzelm@22129
  1619
val allE = @{thm allE}
wenzelm@22129
  1620
val allI = @{thm allI}
wenzelm@22129
  1621
val all_dupE = @{thm all_dupE}
wenzelm@22129
  1622
val arg_cong = @{thm arg_cong}
wenzelm@22129
  1623
val box_equals = @{thm box_equals}
wenzelm@22129
  1624
val ccontr = @{thm ccontr}
wenzelm@22129
  1625
val classical = @{thm classical}
wenzelm@22129
  1626
val conjE = @{thm conjE}
wenzelm@22129
  1627
val conjI = @{thm conjI}
wenzelm@22129
  1628
val conjunct1 = @{thm conjunct1}
wenzelm@22129
  1629
val conjunct2 = @{thm conjunct2}
wenzelm@22129
  1630
val disjCI = @{thm disjCI}
wenzelm@22129
  1631
val disjE = @{thm disjE}
wenzelm@22129
  1632
val disjI1 = @{thm disjI1}
wenzelm@22129
  1633
val disjI2 = @{thm disjI2}
wenzelm@22129
  1634
val eq_reflection = @{thm eq_reflection}
wenzelm@22129
  1635
val ex1E = @{thm ex1E}
wenzelm@22129
  1636
val ex1I = @{thm ex1I}
wenzelm@22129
  1637
val ex1_implies_ex = @{thm ex1_implies_ex}
wenzelm@22129
  1638
val exE = @{thm exE}
wenzelm@22129
  1639
val exI = @{thm exI}
wenzelm@22129
  1640
val excluded_middle = @{thm excluded_middle}
wenzelm@22129
  1641
val ext = @{thm ext}
wenzelm@22129
  1642
val fun_cong = @{thm fun_cong}
wenzelm@22129
  1643
val iffD1 = @{thm iffD1}
wenzelm@22129
  1644
val iffD2 = @{thm iffD2}
wenzelm@22129
  1645
val iffI = @{thm iffI}
wenzelm@22129
  1646
val impE = @{thm impE}
wenzelm@22129
  1647
val impI = @{thm impI}
wenzelm@22129
  1648
val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
wenzelm@22129
  1649
val mp = @{thm mp}
wenzelm@22129
  1650
val notE = @{thm notE}
wenzelm@22129
  1651
val notI = @{thm notI}
wenzelm@22129
  1652
val not_all = @{thm not_all}
wenzelm@22129
  1653
val not_ex = @{thm not_ex}
wenzelm@22129
  1654
val not_iff = @{thm not_iff}
wenzelm@22129
  1655
val not_not = @{thm not_not}
wenzelm@22129
  1656
val not_sym = @{thm not_sym}
wenzelm@22129
  1657
val refl = @{thm refl}
wenzelm@22129
  1658
val rev_mp = @{thm rev_mp}
wenzelm@22129
  1659
val spec = @{thm spec}
wenzelm@22129
  1660
val ssubst = @{thm ssubst}
wenzelm@22129
  1661
val subst = @{thm subst}
wenzelm@22129
  1662
val sym = @{thm sym}
wenzelm@22129
  1663
val trans = @{thm trans}
wenzelm@21671
  1664
*}
wenzelm@21671
  1665
wenzelm@55239
  1666
ML_file "Tools/cnf.ML"
wenzelm@55239
  1667
wenzelm@21671
  1668
hoelzl@58775
  1669
section {* @{text NO_MATCH} simproc *}
hoelzl@58775
  1670
hoelzl@58775
  1671
text {*
hoelzl@58775
  1672
 The simplification procedure can be used to avoid simplification of terms of a certain form
hoelzl@58775
  1673
*}
hoelzl@58775
  1674
hoelzl@59779
  1675
definition NO_MATCH :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where "NO_MATCH pat val \<equiv> True"
hoelzl@58830
  1676
hoelzl@59779
  1677
lemma NO_MATCH_cong[cong]: "NO_MATCH pat val = NO_MATCH pat val" by (rule refl)
hoelzl@58775
  1678
hoelzl@58830
  1679
declare [[coercion_args NO_MATCH - -]]
hoelzl@58830
  1680
hoelzl@59779
  1681
simproc_setup NO_MATCH ("NO_MATCH pat val") = {* fn _ => fn ctxt => fn ct =>
hoelzl@58775
  1682
  let
hoelzl@58775
  1683
    val thy = Proof_Context.theory_of ctxt
hoelzl@58775
  1684
    val dest_binop = Term.dest_comb #> apfst (Term.dest_comb #> snd)
hoelzl@58775
  1685
    val m = Pattern.matches thy (dest_binop (Thm.term_of ct))
hoelzl@58775
  1686
  in if m then NONE else SOME @{thm NO_MATCH_def} end
hoelzl@58775
  1687
*}
hoelzl@58775
  1688
hoelzl@58775
  1689
text {*
hoelzl@59779
  1690
  This setup ensures that a rewrite rule of the form @{term "NO_MATCH pat val \<Longrightarrow> t"}
hoelzl@58775
  1691
  is only applied, if the pattern @{term pat} does not match the value @{term val}.
hoelzl@58775
  1692
*}
hoelzl@58775
  1693
hoelzl@58775
  1694
haftmann@30929
  1695
subsection {* Code generator setup *}
haftmann@30929
  1696
haftmann@31151
  1697
subsubsection {* Generic code generator preprocessor setup *}
haftmann@31151
  1698
haftmann@53146
  1699
lemma conj_left_cong:
haftmann@53146
  1700
  "P \<longleftrightarrow> Q \<Longrightarrow> P \<and> R \<longleftrightarrow> Q \<and> R"
haftmann@53146
  1701
  by (fact arg_cong)
haftmann@53146
  1702
haftmann@53146
  1703
lemma disj_left_cong:
haftmann@53146
  1704
  "P \<longleftrightarrow> Q \<Longrightarrow> P \<or> R \<longleftrightarrow> Q \<or> R"
haftmann@53146
  1705
  by (fact arg_cong)
haftmann@53146
  1706
haftmann@31151
  1707
setup {*
wenzelm@58826
  1708
  Code_Preproc.map_pre (put_simpset HOL_basic_ss) #>
wenzelm@58826
  1709
  Code_Preproc.map_post (put_simpset HOL_basic_ss) #>
wenzelm@58826
  1710
  Code_Simp.map_ss (put_simpset HOL_basic_ss #>
wenzelm@58826
  1711
  Simplifier.add_cong @{thm conj_left_cong} #>
wenzelm@58826
  1712
  Simplifier.add_cong @{thm disj_left_cong})
haftmann@31151
  1713
*}
haftmann@31151
  1714
haftmann@53146
  1715
haftmann@30929
  1716
subsubsection {* Equality *}
haftmann@24844
  1717
haftmann@38857
  1718
class equal =
haftmann@38857
  1719
  fixes equal :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@38857
  1720
  assumes equal_eq: "equal x y \<longleftrightarrow> x = y"
haftmann@26513
  1721
begin
haftmann@26513
  1722
bulwahn@45231
  1723
lemma equal: "equal = (op =)"
haftmann@38857
  1724
  by (rule ext equal_eq)+
haftmann@28346
  1725
haftmann@38857
  1726
lemma equal_refl: "equal x x \<longleftrightarrow> True"
haftmann@38857
  1727
  unfolding equal by rule+
haftmann@28346
  1728
haftmann@38857
  1729
lemma eq_equal: "(op =) \<equiv> equal"
haftmann@38857
  1730
  by (rule eq_reflection) (rule ext, rule ext, rule sym, rule equal_eq)
haftmann@30929
  1731
haftmann@26513
  1732
end
haftmann@26513
  1733
haftmann@38857
  1734
declare eq_equal [symmetric, code_post]
haftmann@38857
  1735
declare eq_equal [code]
haftmann@30966
  1736
haftmann@31151
  1737
setup {*
wenzelm@51717
  1738
  Code_Preproc.map_pre (fn ctxt =>
wenzelm@51717
  1739
    ctxt addsimprocs [Simplifier.simproc_global_i @{theory} "equal" [@{term HOL.eq}]
wenzelm@51717
  1740
      (fn _ => fn Const (_, Type ("fun", [Type _, _])) => SOME @{thm eq_equal} | _ => NONE)])
haftmann@31151
  1741
*}
haftmann@31151
  1742
haftmann@30966
  1743
haftmann@30929
  1744
subsubsection {* Generic code generator foundation *}
haftmann@30929
  1745
haftmann@39421
  1746
text {* Datatype @{typ bool} *}
haftmann@30929
  1747
haftmann@30929
  1748
code_datatype True False
haftmann@30929
  1749
haftmann@30929
  1750
lemma [code]:
haftmann@33185
  1751
  shows "False \<and> P \<longleftrightarrow> False"
haftmann@33185
  1752
    and "True \<and> P \<longleftrightarrow> P"
haftmann@33185
  1753
    and "P \<and> False \<longleftrightarrow> False"
haftmann@33185
  1754
    and "P \<and> True \<longleftrightarrow> P" by simp_all
haftmann@30929
  1755
haftmann@30929
  1756
lemma [code]:
haftmann@33185
  1757
  shows "False \<or> P \<longleftrightarrow> P"
haftmann@33185
  1758
    and "True \<or> P \<longleftrightarrow> True"
haftmann@33185
  1759
    and "P \<or> False \<longleftrightarrow> P"
haftmann@33185
  1760
    and "P \<or> True \<longleftrightarrow> True" by simp_all
haftmann@30929
  1761
haftmann@33185
  1762
lemma [code]:
haftmann@33185
  1763
  shows "(False \<longrightarrow> P) \<longleftrightarrow> True"
haftmann@33185
  1764
    and "(True \<longrightarrow> P) \<longleftrightarrow> P"
haftmann@33185
  1765
    and "(P \<longrightarrow> False) \<longleftrightarrow> \<not> P"
haftmann@33185
  1766
    and "(P \<longrightarrow> True) \<longleftrightarrow> True" by simp_all
haftmann@30929
  1767
haftmann@39421
  1768
text {* More about @{typ prop} *}
haftmann@39421
  1769
haftmann@39421
  1770
lemma [code nbe]:
wenzelm@58826
  1771
  shows "(True \<Longrightarrow> PROP Q) \<equiv> PROP Q"
haftmann@39421
  1772
    and "(PROP Q \<Longrightarrow> True) \<equiv> Trueprop True"
haftmann@39421
  1773
    and "(P \<Longrightarrow> R) \<equiv> Trueprop (P \<longrightarrow> R)" by (auto intro!: equal_intr_rule)
haftmann@39421
  1774
haftmann@39421
  1775
lemma Trueprop_code [code]:
haftmann@39421
  1776
  "Trueprop True \<equiv> Code_Generator.holds"
haftmann@39421
  1777
  by (auto intro!: equal_intr_rule holds)
haftmann@39421
  1778
haftmann@39421
  1779
declare Trueprop_code [symmetric, code_post]
haftmann@39421
  1780
haftmann@39421
  1781
text {* Equality *}
haftmann@39421
  1782
haftmann@39421
  1783
declare simp_thms(6) [code nbe]
haftmann@39421
  1784
haftmann@38857
  1785
instantiation itself :: (type) equal
haftmann@31132
  1786
begin
haftmann@31132
  1787
haftmann@38857
  1788
definition equal_itself :: "'a itself \<Rightarrow> 'a itself \<Rightarrow> bool" where
haftmann@38857
  1789
  "equal_itself x y \<longleftrightarrow> x = y"
haftmann@31132
  1790
haftmann@31132
  1791
instance proof
haftmann@38857
  1792
qed (fact equal_itself_def)
haftmann@31132
  1793
haftmann@31132
  1794
end
haftmann@31132
  1795
haftmann@38857
  1796
lemma equal_itself_code [code]:
haftmann@38857
  1797
  "equal TYPE('a) TYPE('a) \<longleftrightarrow> True"
haftmann@38857
  1798
  by (simp add: equal)
haftmann@31132
  1799
wenzelm@58826
  1800
setup {* Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a\<Colon>type \<Rightarrow> 'a \<Rightarrow> bool"}) *}
haftmann@31956
  1801
haftmann@38857
  1802
lemma equal_alias_cert: "OFCLASS('a, equal_class) \<equiv> ((op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool) \<equiv> equal)" (is "?ofclass \<equiv> ?equal")
haftmann@31956
  1803
proof
haftmann@31956
  1804
  assume "PROP ?ofclass"
haftmann@38857
  1805
  show "PROP ?equal"
wenzelm@59498
  1806
    by (tactic {* ALLGOALS (resolve_tac @{context} [Thm.unconstrainT @{thm eq_equal}]) *})
haftmann@31956
  1807
      (fact `PROP ?ofclass`)
haftmann@31956
  1808
next
haftmann@38857
  1809
  assume "PROP ?equal"
haftmann@31956
  1810
  show "PROP ?ofclass" proof
haftmann@38857
  1811
  qed (simp add: `PROP ?equal`)
haftmann@31956
  1812
qed
haftmann@31956
  1813
wenzelm@58826
  1814
setup {* Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a\<Colon>equal \<Rightarrow> 'a \<Rightarrow> bool"}) *}
wenzelm@58826
  1815
wenzelm@58826
  1816
setup {* Nbe.add_const_alias @{thm equal_alias_cert} *}
haftmann@30929
  1817
haftmann@30929
  1818
text {* Cases *}
haftmann@30929
  1819
haftmann@30929
  1820
lemma Let_case_cert:
haftmann@30929
  1821
  assumes "CASE \<equiv> (\<lambda>x. Let x f)"
haftmann@30929
  1822
  shows "CASE x \<equiv> f x"
haftmann@30929
  1823
  using assms by simp_all
haftmann@30929
  1824
haftmann@30929
  1825
setup {*
wenzelm@58826
  1826
  Code.add_case @{thm Let_case_cert} #>
wenzelm@58826
  1827
  Code.add_undefined @{const_name undefined}
haftmann@30929
  1828
*}
haftmann@30929
  1829
haftmann@54890
  1830
declare [[code abort: undefined]]
haftmann@30929
  1831
haftmann@38972
  1832
haftmann@30929
  1833
subsubsection {* Generic code generator target languages *}
haftmann@30929
  1834
haftmann@38972
  1835
text {* type @{typ bool} *}
haftmann@30929
  1836
haftmann@52435
  1837
code_printing
haftmann@52435
  1838
  type_constructor bool \<rightharpoonup>
haftmann@52435
  1839
    (SML) "bool" and (OCaml) "bool" and (Haskell) "Bool" and (Scala) "Boolean"
haftmann@52435
  1840
| constant True \<rightharpoonup>
haftmann@52435
  1841
    (SML) "true" and (OCaml) "true" and (Haskell) "True" and (Scala) "true"
haftmann@52435
  1842
| constant False \<rightharpoonup>
wenzelm@58826
  1843
    (SML) "false" and (OCaml) "false" and (Haskell) "False" and (Scala) "false"
haftmann@34294
  1844
haftmann@30929
  1845
code_reserved SML
haftmann@52435
  1846
  bool true false
haftmann@30929
  1847
haftmann@30929
  1848
code_reserved OCaml
haftmann@52435
  1849
  bool
haftmann@30929
  1850
haftmann@34294
  1851
code_reserved Scala
haftmann@34294
  1852
  Boolean
haftmann@34294
  1853
haftmann@52435
  1854
code_printing
haftmann@52435
  1855
  constant Not \<rightharpoonup>
haftmann@52435
  1856
    (SML) "not" and (OCaml) "not" and (Haskell) "not" and (Scala) "'! _"
haftmann@52435
  1857
| constant HOL.conj \<rightharpoonup>
haftmann@52435
  1858
    (SML) infixl 1 "andalso" and (OCaml) infixl 3 "&&" and (Haskell) infixr 3 "&&" and (Scala) infixl 3 "&&"
haftmann@52435
  1859
| constant HOL.disj \<rightharpoonup>
haftmann@52435
  1860
    (SML) infixl 0 "orelse" and (OCaml) infixl 2 "||" and (Haskell) infixl 2 "||" and (Scala) infixl 1 "||"
haftmann@52435
  1861
| constant HOL.implies \<rightharpoonup>
haftmann@52435
  1862
    (SML) "!(if (_)/ then (_)/ else true)"
haftmann@52435
  1863
    and (OCaml) "!(if (_)/ then (_)/ else true)"
haftmann@52435
  1864
    and (Haskell) "!(if (_)/ then (_)/ else True)"
haftmann@52435
  1865
    and (Scala) "!(if ((_))/ (_)/ else true)"
haftmann@52435
  1866
| constant If \<rightharpoonup>
haftmann@52435
  1867
    (SML) "!(if (_)/ then (_)/ else (_))"
haftmann@52435
  1868
    and (OCaml) "!(if (_)/ then (_)/ else (_))"
haftmann@52435
  1869
    and (Haskell) "!(if (_)/ then (_)/ else (_))"
haftmann@52435
  1870
    and (Scala) "!(if ((_))/ (_)/ else (_))"
haftmann@52435
  1871
haftmann@52435
  1872
code_reserved SML
haftmann@52435
  1873
  not
haftmann@52435
  1874
haftmann@52435
  1875
code_reserved OCaml
haftmann@52435
  1876
  not
haftmann@52435
  1877
haftmann@52435
  1878
code_identifier
haftmann@52435
  1879
  code_module Pure \<rightharpoonup>
haftmann@52435
  1880
    (SML) HOL and (OCaml) HOL and (Haskell) HOL and (Scala) HOL
haftmann@39026
  1881
haftmann@30929
  1882
text {* using built-in Haskell equality *}
haftmann@30929
  1883
haftmann@52435
  1884
code_printing
haftmann@52435
  1885
  type_class equal \<rightharpoonup> (Haskell) "Eq"
haftmann@52435
  1886
| constant HOL.equal \<rightharpoonup> (Haskell) infix 4 "=="
haftmann@52435
  1887
| constant HOL.eq \<rightharpoonup> (Haskell) infix 4 "=="
haftmann@30929
  1888
haftmann@30929
  1889
text {* undefined *}
haftmann@30929
  1890
haftmann@52435
  1891
code_printing
haftmann@52435
  1892
  constant undefined \<rightharpoonup>
haftmann@52435
  1893
    (SML) "!(raise/ Fail/ \"undefined\")"
haftmann@52435
  1894
    and (OCaml) "failwith/ \"undefined\""
haftmann@52435
  1895
    and (Haskell) "error/ \"undefined\""
haftmann@52435
  1896
    and (Scala) "!sys.error(\"undefined\")"
haftmann@52435
  1897
haftmann@30929
  1898
haftmann@30929
  1899
subsubsection {* Evaluation and normalization by evaluation *}
haftmann@30929
  1900
haftmann@55757
  1901
method_setup eval = {*
wenzelm@58826
  1902
  let
wenzelm@58826
  1903
    fun eval_tac ctxt =
wenzelm@58826
  1904
      let val conv = Code_Runtime.dynamic_holds_conv ctxt
wenzelm@58839
  1905
      in
wenzelm@58839
  1906
        CONVERSION (Conv.params_conv ~1 (K (Conv.concl_conv ~1 conv)) ctxt) THEN'
wenzelm@59498
  1907
        resolve_tac ctxt [TrueI]
wenzelm@58839
  1908
      end
wenzelm@58826
  1909
  in
wenzelm@58826
  1910
    Scan.succeed (SIMPLE_METHOD' o eval_tac)
wenzelm@58826
  1911
  end
haftmann@55757
  1912
*} "solve goal by evaluation"
haftmann@30929
  1913
haftmann@30929
  1914
method_setup normalization = {*
wenzelm@46190
  1915
  Scan.succeed (fn ctxt =>
wenzelm@46190
  1916
    SIMPLE_METHOD'
wenzelm@46190
  1917
      (CHANGED_PROP o
haftmann@55757
  1918
        (CONVERSION (Nbe.dynamic_conv ctxt)
wenzelm@59498
  1919
          THEN_ALL_NEW (TRY o resolve_tac ctxt [TrueI]))))
haftmann@30929
  1920
*} "solve goal by normalization"
haftmann@30929
  1921
wenzelm@31902
  1922
haftmann@33084
  1923
subsection {* Counterexample Search Units *}
haftmann@33084
  1924
haftmann@30929
  1925
subsubsection {* Quickcheck *}
haftmann@30929
  1926
haftmann@33084
  1927
quickcheck_params [size = 5, iterations = 50]
haftmann@33084
  1928
haftmann@30929
  1929
haftmann@33084
  1930
subsubsection {* Nitpick setup *}
blanchet@30309
  1931
wenzelm@59028
  1932
named_theorems nitpick_unfold "alternative definitions of constants as needed by Nitpick"
wenzelm@59028
  1933
  and nitpick_simp "equational specification of constants as needed by Nitpick"
wenzelm@59028
  1934
  and nitpick_psimp "partial equational specification of constants as needed by Nitpick"
wenzelm@59028
  1935
  and nitpick_choice_spec "choice specification of constants as needed by Nitpick"
wenzelm@30980
  1936
blanchet@41792
  1937
declare if_bool_eq_conj [nitpick_unfold, no_atp]
blanchet@41792
  1938
        if_bool_eq_disj [no_atp]
blanchet@41792
  1939
blanchet@29863
  1940
haftmann@33084
  1941
subsection {* Preprocessing for the predicate compiler *}
haftmann@33084
  1942
wenzelm@59028
  1943
named_theorems code_pred_def "alternative definitions of constants for the Predicate Compiler"
wenzelm@59028
  1944
  and code_pred_inline "inlining definitions for the Predicate Compiler"
wenzelm@59028
  1945
  and code_pred_simp "simplification rules for the optimisations in the Predicate Compiler"
haftmann@33084
  1946
haftmann@33084
  1947
haftmann@22839
  1948
subsection {* Legacy tactics and ML bindings *}
wenzelm@21671
  1949
wenzelm@21671
  1950
ML {*
wenzelm@58826
  1951
  (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
wenzelm@58826
  1952
  local
wenzelm@58826
  1953
    fun wrong_prem (Const (@{const_name All}, _) $ Abs (_, _, t)) = wrong_prem t
wenzelm@58826
  1954
      | wrong_prem (Bound _) = true
wenzelm@58826
  1955
      | wrong_prem _ = false;
wenzelm@58826
  1956
    val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
wenzelm@58826
  1957
  in
wenzelm@58826
  1958
    fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp]);
wenzelm@59498
  1959
    fun smp_tac ctxt j = EVERY' [dresolve_tac ctxt (smp j), assume_tac ctxt];
wenzelm@58826
  1960
  end;
haftmann@22839
  1961
wenzelm@58826
  1962
  local
wenzelm@58826
  1963
    val nnf_ss =
wenzelm@58826
  1964
      simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms simp_thms nnf_simps});
wenzelm@58826
  1965
  in
wenzelm@58826
  1966
    fun nnf_conv ctxt = Simplifier.rewrite (put_simpset nnf_ss ctxt);
wenzelm@58826
  1967
  end
wenzelm@21671
  1968
*}
wenzelm@21671
  1969
haftmann@38866
  1970
hide_const (open) eq equal
haftmann@38866
  1971
kleing@14357
  1972
end