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