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