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