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