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