src/HOL/Integ/Parity.thy
author nipkow
Mon Aug 16 14:22:27 2004 +0200 (2004-08-16)
changeset 15131 c69542757a4d
parent 15003 6145dd7538d7
child 15140 322485b816ac
permissions -rw-r--r--
New theory header syntax.
paulson@14430
     1
(*  Title:      Parity.thy
paulson@14450
     2
    ID:         $Id$
paulson@14430
     3
    Author:     Jeremy Avigad
paulson@14430
     4
*)
paulson@14430
     5
paulson@14430
     6
header {* Parity: Even and Odd for ints and nats*}
paulson@14430
     7
nipkow@15131
     8
theory Parity
nipkow@15131
     9
import Divides IntDiv NatSimprocs
nipkow@15131
    10
begin
paulson@14430
    11
paulson@14430
    12
axclass even_odd < type
paulson@14430
    13
paulson@14430
    14
instance int :: even_odd ..
paulson@14430
    15
instance nat :: even_odd ..
paulson@14430
    16
paulson@14430
    17
consts
paulson@14430
    18
  even :: "'a::even_odd => bool"
paulson@14430
    19
paulson@14430
    20
syntax 
paulson@14430
    21
  odd :: "'a::even_odd => bool"
paulson@14430
    22
paulson@14430
    23
translations 
paulson@14430
    24
  "odd x" == "~even x" 
paulson@14430
    25
paulson@14430
    26
defs (overloaded)
paulson@14430
    27
  even_def: "even (x::int) == x mod 2 = 0"
paulson@14430
    28
  even_nat_def: "even (x::nat) == even (int x)"
paulson@14430
    29
paulson@14430
    30
paulson@14430
    31
subsection {* Casting a nat power to an integer *}
paulson@14430
    32
paulson@14430
    33
lemma zpow_int: "int (x^y) = (int x)^y"
paulson@14430
    34
  apply (induct_tac y)
paulson@14430
    35
  apply (simp, simp add: zmult_int [THEN sym])
paulson@14430
    36
  done
paulson@14430
    37
paulson@14430
    38
subsection {* Even and odd are mutually exclusive *}
paulson@14430
    39
paulson@14430
    40
lemma int_pos_lt_two_imp_zero_or_one: 
paulson@14430
    41
    "0 <= x ==> (x::int) < 2 ==> x = 0 | x = 1"
paulson@14430
    42
  by auto
paulson@14430
    43
paulson@14430
    44
lemma neq_one_mod_two [simp]: "((x::int) mod 2 ~= 0) = (x mod 2 = 1)"
paulson@14430
    45
  apply (subgoal_tac "x mod 2 = 0 | x mod 2 = 1", force)
paulson@14430
    46
  apply (rule int_pos_lt_two_imp_zero_or_one, auto)
paulson@14430
    47
  done
paulson@14430
    48
paulson@14430
    49
subsection {* Behavior under integer arithmetic operations *}
paulson@14430
    50
paulson@14430
    51
lemma even_times_anything: "even (x::int) ==> even (x * y)"
paulson@14430
    52
  by (simp add: even_def zmod_zmult1_eq')
paulson@14430
    53
paulson@14430
    54
lemma anything_times_even: "even (y::int) ==> even (x * y)"
paulson@14430
    55
  by (simp add: even_def zmod_zmult1_eq)
paulson@14430
    56
paulson@14430
    57
lemma odd_times_odd: "odd (x::int) ==> odd y ==> odd (x * y)"
paulson@14430
    58
  by (simp add: even_def zmod_zmult1_eq)
paulson@14430
    59
paulson@14430
    60
lemma even_product: "even((x::int) * y) = (even x | even y)"
paulson@14430
    61
  apply (auto simp add: even_times_anything anything_times_even) 
paulson@14430
    62
  apply (rule ccontr)
paulson@14430
    63
  apply (auto simp add: odd_times_odd)
paulson@14430
    64
  done
paulson@14430
    65
paulson@14430
    66
lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
paulson@14430
    67
  by (simp add: even_def zmod_zadd1_eq)
paulson@14430
    68
paulson@14430
    69
lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
paulson@14430
    70
  by (simp add: even_def zmod_zadd1_eq)
paulson@14430
    71
paulson@14430
    72
lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
paulson@14430
    73
  by (simp add: even_def zmod_zadd1_eq)
paulson@14430
    74
paulson@14430
    75
lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)"
paulson@14430
    76
  by (simp add: even_def zmod_zadd1_eq)
paulson@14430
    77
paulson@14430
    78
lemma even_sum: "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
paulson@14430
    79
  apply (auto intro: even_plus_even odd_plus_odd)
paulson@14430
    80
  apply (rule ccontr, simp add: even_plus_odd)
paulson@14430
    81
  apply (rule ccontr, simp add: odd_plus_even)
paulson@14430
    82
  done
paulson@14430
    83
paulson@14430
    84
lemma even_neg: "even (-(x::int)) = even x"
paulson@14430
    85
  by (auto simp add: even_def zmod_zminus1_eq_if)
paulson@14430
    86
paulson@14430
    87
lemma even_difference: 
paulson@14430
    88
  "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))"
paulson@14430
    89
  by (simp only: diff_minus even_sum even_neg)
paulson@14430
    90
paulson@14430
    91
lemma even_pow_gt_zero [rule_format]: 
paulson@14430
    92
    "even (x::int) ==> 0 < n --> even (x^n)"
paulson@14430
    93
  apply (induct_tac n)
paulson@14430
    94
  apply (auto simp add: even_product)
paulson@14430
    95
  done
paulson@14430
    96
paulson@14430
    97
lemma odd_pow: "odd x ==> odd((x::int)^n)"
paulson@14430
    98
  apply (induct_tac n)
paulson@14430
    99
  apply (simp add: even_def)
paulson@14430
   100
  apply (simp add: even_product)
paulson@14430
   101
  done
paulson@14430
   102
paulson@14430
   103
lemma even_power: "even ((x::int)^n) = (even x & 0 < n)"
paulson@14430
   104
  apply (auto simp add: even_pow_gt_zero) 
paulson@14430
   105
  apply (erule contrapos_pp, erule odd_pow)
paulson@14430
   106
  apply (erule contrapos_pp, simp add: even_def)
paulson@14430
   107
  done
paulson@14430
   108
paulson@14430
   109
lemma even_zero: "even (0::int)"
paulson@14430
   110
  by (simp add: even_def)
paulson@14430
   111
paulson@14430
   112
lemma odd_one: "odd (1::int)"
paulson@14430
   113
  by (simp add: even_def)
paulson@14430
   114
paulson@14430
   115
lemmas even_odd_simps [simp] = even_def[of "number_of v",standard] even_zero 
paulson@14430
   116
  odd_one even_product even_sum even_neg even_difference even_power
paulson@14430
   117
paulson@14430
   118
paulson@14430
   119
subsection {* Equivalent definitions *}
paulson@14430
   120
paulson@14430
   121
lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x" 
paulson@14430
   122
  by (auto simp add: even_def)
paulson@14430
   123
paulson@14430
   124
lemma two_times_odd_div_two_plus_one: "odd (x::int) ==> 
paulson@14430
   125
    2 * (x div 2) + 1 = x"
paulson@14430
   126
  apply (insert zmod_zdiv_equality [of x 2, THEN sym])
paulson@14430
   127
  by (simp add: even_def)
paulson@14430
   128
paulson@14430
   129
lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)"
paulson@14430
   130
  apply auto
paulson@14430
   131
  apply (rule exI)
paulson@14430
   132
  by (erule two_times_even_div_two [THEN sym])
paulson@14430
   133
paulson@14430
   134
lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)"
paulson@14430
   135
  apply auto
paulson@14430
   136
  apply (rule exI)
paulson@14430
   137
  by (erule two_times_odd_div_two_plus_one [THEN sym])
paulson@14430
   138
paulson@14430
   139
paulson@14430
   140
subsection {* even and odd for nats *}
paulson@14430
   141
paulson@14430
   142
lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
paulson@14430
   143
  by (simp add: even_nat_def)
paulson@14430
   144
paulson@14430
   145
lemma even_nat_product: "even((x::nat) * y) = (even x | even y)"
paulson@14430
   146
  by (simp add: even_nat_def zmult_int [THEN sym])
paulson@14430
   147
paulson@14430
   148
lemma even_nat_sum: "even ((x::nat) + y) = 
paulson@14430
   149
    ((even x & even y) | (odd x & odd y))"
paulson@14430
   150
  by (unfold even_nat_def, simp)
paulson@14430
   151
paulson@14430
   152
lemma even_nat_difference: 
paulson@14430
   153
    "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
paulson@14430
   154
  apply (auto simp add: even_nat_def zdiff_int [THEN sym])
paulson@14430
   155
  apply (case_tac "x < y", auto simp add: zdiff_int [THEN sym])
paulson@14430
   156
  apply (case_tac "x < y", auto simp add: zdiff_int [THEN sym])
paulson@14430
   157
  done
paulson@14430
   158
paulson@14436
   159
lemma even_nat_Suc: "even (Suc x) = odd x"
paulson@14430
   160
  by (simp add: even_nat_def)
paulson@14430
   161
paulson@14436
   162
text{*Compatibility, in case Avigad uses this*}
paulson@14436
   163
lemmas even_nat_suc = even_nat_Suc
paulson@14436
   164
paulson@14430
   165
lemma even_nat_power: "even ((x::nat)^y) = (even x & 0 < y)"
paulson@14430
   166
  by (simp add: even_nat_def zpow_int)
paulson@14430
   167
paulson@14430
   168
lemma even_nat_zero: "even (0::nat)"
paulson@14430
   169
  by (simp add: even_nat_def)
paulson@14430
   170
paulson@14430
   171
lemmas even_odd_nat_simps [simp] = even_nat_def[of "number_of v",standard] 
paulson@14436
   172
  even_nat_zero even_nat_Suc even_nat_product even_nat_sum even_nat_power
paulson@14430
   173
paulson@14430
   174
paulson@14430
   175
subsection {* Equivalent definitions *}
paulson@14430
   176
paulson@14430
   177
lemma nat_lt_two_imp_zero_or_one: "(x::nat) < Suc (Suc 0) ==> 
paulson@14430
   178
    x = 0 | x = Suc 0"
paulson@14430
   179
  by auto
paulson@14430
   180
paulson@14430
   181
lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
paulson@14430
   182
  apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
paulson@14430
   183
  apply (drule subst, assumption)
paulson@14430
   184
  apply (subgoal_tac "x mod Suc (Suc 0) = 0 | x mod Suc (Suc 0) = Suc 0")
paulson@14430
   185
  apply force
paulson@14430
   186
  apply (subgoal_tac "0 < Suc (Suc 0)")
paulson@14430
   187
  apply (frule mod_less_divisor [of "Suc (Suc 0)" x])
paulson@14430
   188
  apply (erule nat_lt_two_imp_zero_or_one, auto)
paulson@14430
   189
  done
paulson@14430
   190
paulson@14430
   191
lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
paulson@14430
   192
  apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
paulson@14430
   193
  apply (drule subst, assumption)
paulson@14430
   194
  apply (subgoal_tac "x mod Suc (Suc 0) = 0 | x mod Suc (Suc 0) = Suc 0")
paulson@14430
   195
  apply force 
paulson@14430
   196
  apply (subgoal_tac "0 < Suc (Suc 0)")
paulson@14430
   197
  apply (frule mod_less_divisor [of "Suc (Suc 0)" x])
paulson@14430
   198
  apply (erule nat_lt_two_imp_zero_or_one, auto)
paulson@14430
   199
  done
paulson@14430
   200
paulson@14430
   201
lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)" 
paulson@14430
   202
  apply (rule iffI)
paulson@14430
   203
  apply (erule even_nat_mod_two_eq_zero)
paulson@14430
   204
  apply (insert odd_nat_mod_two_eq_one [of x], auto)
paulson@14430
   205
  done
paulson@14430
   206
paulson@14430
   207
lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
paulson@14430
   208
  apply (auto simp add: even_nat_equiv_def)
paulson@14430
   209
  apply (subgoal_tac "x mod (Suc (Suc 0)) < Suc (Suc 0)")
paulson@14430
   210
  apply (frule nat_lt_two_imp_zero_or_one, auto)
paulson@14430
   211
  done
paulson@14430
   212
paulson@14430
   213
lemma even_nat_div_two_times_two: "even (x::nat) ==> 
paulson@14430
   214
    Suc (Suc 0) * (x div Suc (Suc 0)) = x"
paulson@14430
   215
  apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
paulson@14430
   216
  apply (drule even_nat_mod_two_eq_zero, simp)
paulson@14430
   217
  done
paulson@14430
   218
paulson@14430
   219
lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==> 
paulson@14430
   220
    Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x"  
paulson@14430
   221
  apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
paulson@14430
   222
  apply (drule odd_nat_mod_two_eq_one, simp)
paulson@14430
   223
  done
paulson@14430
   224
paulson@14430
   225
lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
paulson@14430
   226
  apply (rule iffI, rule exI)
paulson@14430
   227
  apply (erule even_nat_div_two_times_two [THEN sym], auto)
paulson@14430
   228
  done
paulson@14430
   229
paulson@14430
   230
lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
paulson@14430
   231
  apply (rule iffI, rule exI)
paulson@14430
   232
  apply (erule odd_nat_div_two_times_two_plus_one [THEN sym], auto)
paulson@14430
   233
  done
paulson@14430
   234
paulson@14430
   235
subsection {* Powers of negative one *}
paulson@14430
   236
paulson@14430
   237
lemma neg_one_even_odd_power:
paulson@15003
   238
     "(even x --> (-1::'a::{number_ring,recpower})^x = 1) & 
paulson@14430
   239
      (odd x --> (-1::'a)^x = -1)"
paulson@14430
   240
  apply (induct_tac x)
paulson@14430
   241
  apply (simp, simp add: power_Suc)
paulson@14430
   242
  done
paulson@14430
   243
paulson@14436
   244
lemma neg_one_even_power [simp]:
paulson@15003
   245
     "even x ==> (-1::'a::{number_ring,recpower})^x = 1"
paulson@14430
   246
  by (rule neg_one_even_odd_power [THEN conjunct1, THEN mp], assumption)
paulson@14430
   247
paulson@14436
   248
lemma neg_one_odd_power [simp]:
paulson@15003
   249
     "odd x ==> (-1::'a::{number_ring,recpower})^x = -1"
paulson@14430
   250
  by (rule neg_one_even_odd_power [THEN conjunct2, THEN mp], assumption)
paulson@14430
   251
paulson@14443
   252
lemma neg_power_if:
paulson@15003
   253
     "(-x::'a::{comm_ring_1,recpower}) ^ n = 
paulson@14443
   254
      (if even n then (x ^ n) else -(x ^ n))"
paulson@14443
   255
  by (induct n, simp_all split: split_if_asm add: power_Suc) 
paulson@14443
   256
paulson@14430
   257
paulson@14450
   258
subsection {* An Equivalence for @{term "0 \<le> a^n"} *}
paulson@14450
   259
paulson@14450
   260
lemma even_power_le_0_imp_0:
paulson@15003
   261
     "a ^ (2*k) \<le> (0::'a::{ordered_idom,recpower}) ==> a=0"
paulson@14450
   262
apply (induct k) 
paulson@14450
   263
apply (auto simp add: zero_le_mult_iff mult_le_0_iff power_Suc)  
paulson@14450
   264
done
paulson@14450
   265
paulson@14450
   266
lemma zero_le_power_iff:
paulson@15003
   267
     "(0 \<le> a^n) = (0 \<le> (a::'a::{ordered_idom,recpower}) | even n)"
paulson@14450
   268
      (is "?P n")
paulson@14450
   269
proof cases
paulson@14450
   270
  assume even: "even n"
paulson@14473
   271
  then obtain k where "n = 2*k"
paulson@14450
   272
    by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
paulson@14450
   273
  thus ?thesis by (simp add: zero_le_even_power even) 
paulson@14450
   274
next
paulson@14450
   275
  assume odd: "odd n"
paulson@14473
   276
  then obtain k where "n = Suc(2*k)"
paulson@14450
   277
    by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
paulson@14450
   278
  thus ?thesis
paulson@14450
   279
    by (auto simp add: power_Suc zero_le_mult_iff zero_le_even_power 
paulson@14450
   280
             dest!: even_power_le_0_imp_0) 
paulson@14450
   281
qed 
paulson@14450
   282
paulson@14430
   283
subsection {* Miscellaneous *}
paulson@14430
   284
paulson@14430
   285
lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"
paulson@14430
   286
  apply (subst zdiv_zadd1_eq)
paulson@14430
   287
  apply (simp add: even_def)
paulson@14430
   288
  done
paulson@14430
   289
paulson@14430
   290
lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1"
paulson@14430
   291
  apply (subst zdiv_zadd1_eq)
paulson@14430
   292
  apply (simp add: even_def)
paulson@14430
   293
  done
paulson@14430
   294
paulson@14430
   295
lemma div_Suc: "Suc a div c = a div c + Suc 0 div c + 
paulson@14430
   296
    (a mod c + Suc 0 mod c) div c"
paulson@14430
   297
  apply (subgoal_tac "Suc a = a + Suc 0")
paulson@14430
   298
  apply (erule ssubst)
paulson@14430
   299
  apply (rule div_add1_eq, simp)
paulson@14430
   300
  done
paulson@14430
   301
paulson@14430
   302
lemma even_nat_plus_one_div_two: "even (x::nat) ==> 
paulson@14430
   303
   (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)"
paulson@14430
   304
  apply (subst div_Suc)
paulson@14430
   305
  apply (simp add: even_nat_equiv_def)
paulson@14430
   306
  done
paulson@14430
   307
paulson@14430
   308
lemma odd_nat_plus_one_div_two: "odd (x::nat) ==> 
paulson@14430
   309
    (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))"
paulson@14430
   310
  apply (subst div_Suc)
paulson@14430
   311
  apply (simp add: odd_nat_equiv_def)
paulson@14430
   312
  done
paulson@14430
   313
paulson@14430
   314
end