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