src/HOL/Parity.thy
author haftmann
Sun, 21 Aug 2022 06:18:23 +0000
changeset 75937 02b18f59f903
parent 74592 3c587b7c3d5c
child 76387 8cb141384682
permissions -rw-r--r--
streamlined
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 36840
diff changeset
     1
(*  Title:      HOL/Parity.thy
b460124855b8 tuned headers;
wenzelm
parents: 36840
diff changeset
     2
    Author:     Jeremy Avigad
b460124855b8 tuned headers;
wenzelm
parents: 36840
diff changeset
     3
    Author:     Jacques D. Fleuriot
21256
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
     4
*)
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
     5
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
     6
section \<open>Parity in rings and semirings\<close>
21256
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
     7
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
     8
theory Parity
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
     9
  imports Euclidean_Division
21256
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
    10
begin
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
    11
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61531
diff changeset
    12
subsection \<open>Ring structures with parity and \<open>even\<close>/\<open>odd\<close> predicates\<close>
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
    13
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    14
class semiring_parity = comm_semiring_1 + semiring_modulo +
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    15
  assumes even_iff_mod_2_eq_zero: "2 dvd a \<longleftrightarrow> a mod 2 = 0"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    16
    and odd_iff_mod_2_eq_one: "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    17
    and odd_one [simp]: "\<not> 2 dvd 1"
66839
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    18
begin
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    19
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
    20
abbreviation even :: "'a \<Rightarrow> bool"
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
    21
  where "even a \<equiv> 2 dvd a"
54228
229282d53781 purely algebraic foundation for even/odd
haftmann
parents: 54227
diff changeset
    22
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
    23
abbreviation odd :: "'a \<Rightarrow> bool"
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
    24
  where "odd a \<equiv> \<not> 2 dvd a"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
    25
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    26
lemma parity_cases [case_names even odd]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    27
  assumes "even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    28
  assumes "odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    29
  shows P
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    30
  using assms by (cases "even a")
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    31
    (simp_all add: even_iff_mod_2_eq_zero [symmetric] odd_iff_mod_2_eq_one [symmetric])
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    32
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
    33
lemma odd_of_bool_self [simp]:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
    34
  \<open>odd (of_bool p) \<longleftrightarrow> p\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
    35
  by (cases p) simp_all
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
    36
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    37
lemma not_mod_2_eq_0_eq_1 [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    38
  "a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    39
  by (cases a rule: parity_cases) simp_all
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    40
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    41
lemma not_mod_2_eq_1_eq_0 [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    42
  "a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    43
  by (cases a rule: parity_cases) simp_all
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    44
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    45
lemma evenE [elim?]:
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    46
  assumes "even a"
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    47
  obtains b where "a = 2 * b"
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
    48
  using assms by (rule dvdE)
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    49
58681
a478a0742a8e legacy cleanup
haftmann
parents: 58680
diff changeset
    50
lemma oddE [elim?]:
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
    51
  assumes "odd a"
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
    52
  obtains b where "a = 2 * b + 1"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
    53
proof -
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    54
  have "a = 2 * (a div 2) + a mod 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    55
    by (simp add: mult_div_mod_eq)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    56
  with assms have "a = 2 * (a div 2) + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    57
    by (simp add: odd_iff_mod_2_eq_one)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    58
  then show ?thesis ..
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    59
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    60
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    61
lemma mod_2_eq_odd:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    62
  "a mod 2 = of_bool (odd a)"
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    63
  by (auto elim: oddE simp add: even_iff_mod_2_eq_zero)
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    64
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    65
lemma of_bool_odd_eq_mod_2:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    66
  "of_bool (odd a) = a mod 2"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    67
  by (simp add: mod_2_eq_odd)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    68
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    69
lemma even_mod_2_iff [simp]:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    70
  \<open>even (a mod 2) \<longleftrightarrow> even a\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    71
  by (simp add: mod_2_eq_odd)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    72
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    73
lemma mod2_eq_if:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    74
  "a mod 2 = (if even a then 0 else 1)"
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    75
  by (simp add: mod_2_eq_odd)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    76
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    77
lemma even_zero [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    78
  "even 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    79
  by (fact dvd_0_right)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    80
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    81
lemma odd_even_add:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    82
  "even (a + b)" if "odd a" and "odd b"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    83
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    84
  from that obtain c d where "a = 2 * c + 1" and "b = 2 * d + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    85
    by (blast elim: oddE)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    86
  then have "a + b = 2 * c + 2 * d + (1 + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    87
    by (simp only: ac_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    88
  also have "\<dots> = 2 * (c + d + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    89
    by (simp add: algebra_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    90
  finally show ?thesis ..
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    91
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    92
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    93
lemma even_add [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    94
  "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    95
  by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    96
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    97
lemma odd_add [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    98
  "odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    99
  by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   100
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   101
lemma even_plus_one_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   102
  "even (a + 1) \<longleftrightarrow> odd a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   103
  by (auto simp add: dvd_add_right_iff intro: odd_even_add)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   104
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   105
lemma even_mult_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   106
  "even (a * b) \<longleftrightarrow> even a \<or> even b" (is "?P \<longleftrightarrow> ?Q")
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   107
proof
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   108
  assume ?Q
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   109
  then show ?P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   110
    by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   111
next
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   112
  assume ?P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   113
  show ?Q
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   114
  proof (rule ccontr)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   115
    assume "\<not> (even a \<or> even b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   116
    then have "odd a" and "odd b"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   117
      by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   118
    then obtain r s where "a = 2 * r + 1" and "b = 2 * s + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   119
      by (blast elim: oddE)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   120
    then have "a * b = (2 * r + 1) * (2 * s + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   121
      by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   122
    also have "\<dots> = 2 * (2 * r * s + r + s) + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   123
      by (simp add: algebra_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   124
    finally have "odd (a * b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   125
      by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   126
    with \<open>?P\<close> show False
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   127
      by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   128
  qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   129
qed
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   130
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   131
lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   132
proof -
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   133
  have "even (2 * numeral n)"
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   134
    unfolding even_mult_iff by simp
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   135
  then have "even (numeral n + numeral n)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   136
    unfolding mult_2 .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   137
  then show ?thesis
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   138
    unfolding numeral.simps .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   139
qed
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   140
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   141
lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   142
proof
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   143
  assume "even (numeral (num.Bit1 n))"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   144
  then have "even (numeral n + numeral n + 1)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   145
    unfolding numeral.simps .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   146
  then have "even (2 * numeral n + 1)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   147
    unfolding mult_2 .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   148
  then have "2 dvd numeral n * 2 + 1"
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
   149
    by (simp add: ac_simps)
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   150
  then have "2 dvd 1"
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   151
    using dvd_add_times_triv_left_iff [of 2 "numeral n" 1] by simp
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   152
  then show False by simp
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   153
qed
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   154
71755
318695613bb7 more complete rules on numerals
haftmann
parents: 71535
diff changeset
   155
lemma odd_numeral_BitM [simp]:
318695613bb7 more complete rules on numerals
haftmann
parents: 71535
diff changeset
   156
  \<open>odd (numeral (Num.BitM w))\<close>
318695613bb7 more complete rules on numerals
haftmann
parents: 71535
diff changeset
   157
  by (cases w) simp_all
318695613bb7 more complete rules on numerals
haftmann
parents: 71535
diff changeset
   158
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   159
lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0"
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   160
  by (induct n) auto
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   161
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   162
lemma mask_eq_sum_exp:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   163
  \<open>2 ^ n - 1 = (\<Sum>m\<in>{q. q < n}. 2 ^ m)\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   164
proof -
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   165
  have *: \<open>{q. q < Suc m} = insert m {q. q < m}\<close> for m
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   166
    by auto
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   167
  have \<open>2 ^ n = (\<Sum>m\<in>{q. q < n}. 2 ^ m) + 1\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   168
    by (induction n) (simp_all add: ac_simps mult_2 *)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   169
  then have \<open>2 ^ n - 1 = (\<Sum>m\<in>{q. q < n}. 2 ^ m) + 1 - 1\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   170
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   171
  then show ?thesis
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   172
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   173
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   174
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   175
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   176
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   177
class ring_parity = ring + semiring_parity
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   178
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   179
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   180
subclass comm_ring_1 ..
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   181
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   182
lemma even_minus:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   183
  "even (- a) \<longleftrightarrow> even a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   184
  by (fact dvd_minus_iff)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   185
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   186
lemma even_diff [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   187
  "even (a - b) \<longleftrightarrow> even (a + b)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   188
  using even_add [of a "- b"] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   189
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   190
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   191
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   192
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   193
subsection \<open>Special case: euclidean rings containing the natural numbers\<close>
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   194
71157
8bdf3c36011c tuned theory structure
haftmann
parents: 71138
diff changeset
   195
context unique_euclidean_semiring_with_nat
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   196
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   197
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   198
subclass semiring_parity
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   199
proof
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   200
  show "2 dvd a \<longleftrightarrow> a mod 2 = 0" for a
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   201
    by (fact dvd_eq_mod_eq_0)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   202
  show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1" for a
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   203
  proof
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   204
    assume "a mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   205
    then show "\<not> 2 dvd a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   206
      by auto
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   207
  next
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   208
    assume "\<not> 2 dvd a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   209
    have eucl: "euclidean_size (a mod 2) = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   210
    proof (rule order_antisym)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   211
      show "euclidean_size (a mod 2) \<le> 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   212
        using mod_size_less [of 2 a] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   213
      show "1 \<le> euclidean_size (a mod 2)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   214
        using \<open>\<not> 2 dvd a\<close> by (simp add: Suc_le_eq dvd_eq_mod_eq_0)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   215
    qed 
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   216
    from \<open>\<not> 2 dvd a\<close> have "\<not> of_nat 2 dvd division_segment a * of_nat (euclidean_size a)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   217
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   218
    then have "\<not> of_nat 2 dvd of_nat (euclidean_size a)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   219
      by (auto simp only: dvd_mult_unit_iff' is_unit_division_segment)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   220
    then have "\<not> 2 dvd euclidean_size a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   221
      using of_nat_dvd_iff [of 2] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   222
    then have "euclidean_size a mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   223
      by (simp add: semidom_modulo_class.dvd_eq_mod_eq_0)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   224
    then have "of_nat (euclidean_size a mod 2) = of_nat 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   225
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   226
    then have "of_nat (euclidean_size a) mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   227
      by (simp add: of_nat_mod)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   228
    from \<open>\<not> 2 dvd a\<close> eucl
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   229
    show "a mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   230
      by (auto intro: division_segment_eq_iff simp add: division_segment_mod)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   231
  qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   232
  show "\<not> is_unit 2"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   233
  proof (rule notI)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   234
    assume "is_unit 2"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   235
    then have "of_nat 2 dvd of_nat 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   236
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   237
    then have "is_unit (2::nat)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   238
      by (simp only: of_nat_dvd_iff)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   239
    then show False
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   240
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   241
  qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   242
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   243
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   244
lemma even_succ_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   245
  "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   246
  by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   247
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   248
lemma odd_succ_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   249
  "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   250
  by (auto elim!: oddE simp add: add.assoc)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   251
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   252
lemma even_two_times_div_two:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   253
  "even a \<Longrightarrow> 2 * (a div 2) = a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   254
  by (fact dvd_mult_div_cancel)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   255
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   256
lemma odd_two_times_div_two_succ [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   257
  "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   258
  using mult_div_mod_eq [of 2 a]
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   259
  by (simp add: even_iff_mod_2_eq_zero)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   260
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   261
lemma coprime_left_2_iff_odd [simp]:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   262
  "coprime 2 a \<longleftrightarrow> odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   263
proof
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   264
  assume "odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   265
  show "coprime 2 a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   266
  proof (rule coprimeI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   267
    fix b
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   268
    assume "b dvd 2" "b dvd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   269
    then have "b dvd a mod 2"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   270
      by (auto intro: dvd_mod)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   271
    with \<open>odd a\<close> show "is_unit b"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   272
      by (simp add: mod_2_eq_odd)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   273
  qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   274
next
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   275
  assume "coprime 2 a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   276
  show "odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   277
  proof (rule notI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   278
    assume "even a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   279
    then obtain b where "a = 2 * b" ..
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   280
    with \<open>coprime 2 a\<close> have "coprime 2 (2 * b)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   281
      by simp
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   282
    moreover have "\<not> coprime 2 (2 * b)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   283
      by (rule not_coprimeI [of 2]) simp_all
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   284
    ultimately show False
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   285
      by blast
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   286
  qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   287
qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   288
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   289
lemma coprime_right_2_iff_odd [simp]:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   290
  "coprime a 2 \<longleftrightarrow> odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   291
  using coprime_left_2_iff_odd [of a] by (simp add: ac_simps)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   292
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   293
end
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   294
71157
8bdf3c36011c tuned theory structure
haftmann
parents: 71138
diff changeset
   295
context unique_euclidean_ring_with_nat
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   296
begin
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   297
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   298
subclass ring_parity ..
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   299
67906
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   300
lemma minus_1_mod_2_eq [simp]:
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   301
  "- 1 mod 2 = 1"
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   302
  by (simp add: mod_2_eq_odd)
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   303
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   304
lemma minus_1_div_2_eq [simp]:
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   305
  "- 1 div 2 = - 1"
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   306
proof -
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   307
  from div_mult_mod_eq [of "- 1" 2]
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   308
  have "- 1 div 2 * 2 = - 1 * 2"
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   309
    using add_implies_diff by fastforce
67906
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   310
  then show ?thesis
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   311
    using mult_right_cancel [of 2 "- 1 div 2" "- 1"] by simp
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   312
qed
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   313
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   314
end
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   315
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   316
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69502
diff changeset
   317
subsection \<open>Instance for \<^typ>\<open>nat\<close>\<close>
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   318
70340
7383930fc946 slightly more specialized name for type class
haftmann
parents: 70339
diff changeset
   319
instance nat :: unique_euclidean_semiring_with_nat
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   320
  by standard (simp_all add: dvd_eq_mod_eq_0)
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   321
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   322
lemma even_Suc_Suc_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   323
  "even (Suc (Suc n)) \<longleftrightarrow> even n"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   324
  using dvd_add_triv_right_iff [of 2 n] by simp
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   325
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   326
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   327
  using even_plus_one_iff [of n] by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   328
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   329
lemma even_diff_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   330
  "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   331
proof (cases "n \<le> m")
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   332
  case True
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   333
  then have "m - n + n * 2 = m + n" by (simp add: mult_2_right)
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   334
  moreover have "even (m - n) \<longleftrightarrow> even (m - n + n * 2)" by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   335
  ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:)
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   336
  then show ?thesis by auto
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   337
next
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   338
  case False
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   339
  then show ?thesis by simp
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   340
qed
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   341
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   342
lemma odd_pos:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   343
  "odd n \<Longrightarrow> 0 < n" for n :: nat
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   344
  by (auto elim: oddE)
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   345
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   346
lemma Suc_double_not_eq_double:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   347
  "Suc (2 * m) \<noteq> 2 * n"
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   348
proof
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   349
  assume "Suc (2 * m) = 2 * n"
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   350
  moreover have "odd (Suc (2 * m))" and "even (2 * n)"
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   351
    by simp_all
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   352
  ultimately show False by simp
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   353
qed
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   354
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   355
lemma double_not_eq_Suc_double:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   356
  "2 * m \<noteq> Suc (2 * n)"
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   357
  using Suc_double_not_eq_double [of n m] by simp
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   358
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   359
lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   360
  by (auto elim: oddE)
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   361
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   362
lemma even_Suc_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   363
  "even n \<Longrightarrow> Suc n div 2 = n div 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   364
  using even_succ_div_two [of n] by simp
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   365
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   366
lemma odd_Suc_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   367
  "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   368
  using odd_succ_div_two [of n] by simp
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   369
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   370
lemma odd_two_times_div_two_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   371
  assumes "odd n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   372
  shows "2 * (n div 2) = n - (1 :: nat)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   373
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   374
  from assms have "2 * (n div 2) + 1 = n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   375
    by (rule odd_two_times_div_two_succ)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   376
  then have "Suc (2 * (n div 2)) - 1 = n - 1"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   377
    by simp
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   378
  then show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   379
    by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   380
qed
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   381
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   382
lemma not_mod2_eq_Suc_0_eq_0 [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   383
  "n mod 2 \<noteq> Suc 0 \<longleftrightarrow> n mod 2 = 0"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   384
  using not_mod_2_eq_1_eq_0 [of n] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   385
69502
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   386
lemma odd_card_imp_not_empty:
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   387
  \<open>A \<noteq> {}\<close> if \<open>odd (card A)\<close>
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   388
  using that by auto
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   389
70365
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   390
lemma nat_induct2 [case_names 0 1 step]:
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   391
  assumes "P 0" "P 1" and step: "\<And>n::nat. P n \<Longrightarrow> P (n + 2)"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   392
  shows "P n"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   393
proof (induct n rule: less_induct)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   394
  case (less n)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   395
  show ?case
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   396
  proof (cases "n < Suc (Suc 0)")
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   397
    case True
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   398
    then show ?thesis
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   399
      using assms by (auto simp: less_Suc_eq)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   400
  next
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   401
    case False
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   402
    then obtain k where k: "n = Suc (Suc k)"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   403
      by (force simp: not_less nat_le_iff_add)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   404
    then have "k<n"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   405
      by simp
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   406
    with less assms have "P (k+2)"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   407
      by blast
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   408
    then show ?thesis
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   409
      by (simp add: k)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   410
  qed
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   411
qed
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   412
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   413
lemma mask_eq_sum_exp_nat:
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   414
  \<open>2 ^ n - Suc 0 = (\<Sum>m\<in>{q. q < n}. 2 ^ m)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   415
  using mask_eq_sum_exp [where ?'a = nat] by simp
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   416
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   417
context semiring_parity
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   418
begin
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   419
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74101
diff changeset
   420
lemma even_of_nat_iff [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74101
diff changeset
   421
  "even (of_nat n) \<longleftrightarrow> even n"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74101
diff changeset
   422
  by (induction n) simp_all
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74101
diff changeset
   423
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   424
lemma even_sum_iff:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   425
  \<open>even (sum f A) \<longleftrightarrow> even (card {a\<in>A. odd (f a)})\<close> if \<open>finite A\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   426
using that proof (induction A)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   427
  case empty
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   428
  then show ?case
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   429
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   430
next
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   431
  case (insert a A)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   432
  moreover have \<open>{b \<in> insert a A. odd (f b)} = (if odd (f a) then {a} else {}) \<union> {b \<in> A. odd (f b)}\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   433
    by auto
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   434
  ultimately show ?case
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   435
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   436
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   437
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   438
lemma even_prod_iff:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   439
  \<open>even (prod f A) \<longleftrightarrow> (\<exists>a\<in>A. even (f a))\<close> if \<open>finite A\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   440
  using that by (induction A) simp_all
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   441
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   442
lemma even_mask_iff [simp]:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   443
  \<open>even (2 ^ n - 1) \<longleftrightarrow> n = 0\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   444
proof (cases \<open>n = 0\<close>)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   445
  case True
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   446
  then show ?thesis
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   447
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   448
next
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   449
  case False
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   450
  then have \<open>{a. a = 0 \<and> a < n} = {0}\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   451
    by auto
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   452
  then show ?thesis
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   453
    by (auto simp add: mask_eq_sum_exp even_sum_iff)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   454
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   455
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   456
end
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   457
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   458
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   459
subsection \<open>Parity and powers\<close>
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   460
61531
ab2e862263e7 Rounding function, uniform limits, cotangent, binomial identities
eberlm
parents: 60867
diff changeset
   461
context ring_1
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   462
begin
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   463
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   464
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   465
  by (auto elim: evenE)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   466
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   467
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   468
  by (auto elim: oddE)
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   469
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   470
lemma uminus_power_if:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   471
  "(- a) ^ n = (if even n then a ^ n else - (a ^ n))"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   472
  by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   473
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   474
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   475
  by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   476
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   477
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   478
  by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   479
66582
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   480
lemma neg_one_power_add_eq_neg_one_power_diff: "k \<le> n \<Longrightarrow> (- 1) ^ (n + k) = (- 1) ^ (n - k)"
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   481
  by (cases "even (n + k)") auto
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   482
67371
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   483
lemma minus_one_power_iff: "(- 1) ^ n = (if even n then 1 else - 1)"
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   484
  by (induct n) auto
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   485
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   486
end
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   487
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   488
context linordered_idom
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   489
begin
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   490
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   491
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   492
  by (auto elim: evenE)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   493
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   494
lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   495
  by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   496
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   497
lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   498
  by (auto simp add: zero_le_even_power zero_le_odd_power)
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   499
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   500
lemma zero_less_power_eq: "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   501
proof -
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   502
  have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   503
    unfolding power_eq_0_iff [of a n, symmetric] by blast
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   504
  show ?thesis
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   505
    unfolding less_le zero_le_power_eq by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   506
qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   507
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   508
lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   509
  unfolding not_le [symmetric] zero_le_power_eq by auto
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   510
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   511
lemma power_le_zero_eq: "a ^ n \<le> 0 \<longleftrightarrow> n > 0 \<and> (odd n \<and> a \<le> 0 \<or> even n \<and> a = 0)"
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   512
  unfolding not_less [symmetric] zero_less_power_eq by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   513
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   514
lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   515
  using power_abs [of a n] by (simp add: zero_le_even_power)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   516
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   517
lemma power_mono_even:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   518
  assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   519
  shows "a ^ n \<le> b ^ n"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   520
proof -
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   521
  have "0 \<le> \<bar>a\<bar>" by auto
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   522
  with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n"
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   523
    by (rule power_mono)
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   524
  with \<open>even n\<close> show ?thesis
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   525
    by (simp add: power_even_abs)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   526
qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   527
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   528
lemma power_mono_odd:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   529
  assumes "odd n" and "a \<le> b"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   530
  shows "a ^ n \<le> b ^ n"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   531
proof (cases "b < 0")
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   532
  case True
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   533
  with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   534
  then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   535
  with \<open>odd n\<close> show ?thesis by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   536
next
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   537
  case False
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   538
  then have "0 \<le> b" by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   539
  show ?thesis
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   540
  proof (cases "a < 0")
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   541
    case True
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   542
    then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   543
    then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   544
    moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   545
    ultimately show ?thesis by auto
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   546
  next
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   547
    case False
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   548
    then have "0 \<le> a" by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   549
    with \<open>a \<le> b\<close> show ?thesis
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   550
      using power_mono by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   551
  qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   552
qed
62083
7582b39f51ed add the proof of the central limit theorem
hoelzl
parents: 61799
diff changeset
   553
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   554
text \<open>Simplify, when the exponent is a numeral\<close>
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   555
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   556
lemma zero_le_power_eq_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   557
  "0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   558
  by (fact zero_le_power_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   559
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   560
lemma zero_less_power_eq_numeral [simp]:
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   561
  "0 < a ^ numeral w \<longleftrightarrow>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   562
    numeral w = (0 :: nat) \<or>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   563
    even (numeral w :: nat) \<and> a \<noteq> 0 \<or>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   564
    odd (numeral w :: nat) \<and> 0 < a"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   565
  by (fact zero_less_power_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   566
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   567
lemma power_le_zero_eq_numeral [simp]:
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   568
  "a ^ numeral w \<le> 0 \<longleftrightarrow>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   569
    (0 :: nat) < numeral w \<and>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   570
    (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   571
  by (fact power_le_zero_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   572
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   573
lemma power_less_zero_eq_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   574
  "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   575
  by (fact power_less_zero_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   576
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   577
lemma power_even_abs_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   578
  "even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   579
  by (fact power_even_abs)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   580
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   581
end
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   582
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   583
context unique_euclidean_semiring_with_nat
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   584
begin
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   585
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   586
lemma even_mask_div_iff':
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   587
  \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> m \<le> n\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   588
proof -
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   589
  have \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> even (of_nat ((2 ^ m - Suc 0) div 2 ^ n))\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   590
    by (simp only: of_nat_div) (simp add: of_nat_diff)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   591
  also have \<open>\<dots> \<longleftrightarrow> even ((2 ^ m - Suc 0) div 2 ^ n)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   592
    by simp
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   593
  also have \<open>\<dots> \<longleftrightarrow> m \<le> n\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   594
  proof (cases \<open>m \<le> n\<close>)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   595
    case True
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   596
    then show ?thesis
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   597
      by (simp add: Suc_le_lessD)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   598
  next
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   599
    case False
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   600
    then obtain r where r: \<open>m = n + Suc r\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   601
      using less_imp_Suc_add by fastforce
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   602
    from r have \<open>{q. q < m} \<inter> {q. 2 ^ n dvd (2::nat) ^ q} = {q. n \<le> q \<and> q < m}\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   603
      by (auto simp add: dvd_power_iff_le)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   604
    moreover from r have \<open>{q. q < m} \<inter> {q. \<not> 2 ^ n dvd (2::nat) ^ q} = {q. q < n}\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   605
      by (auto simp add: dvd_power_iff_le)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   606
    moreover from False have \<open>{q. n \<le> q \<and> q < m \<and> q \<le> n} = {n}\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   607
      by auto
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   608
    then have \<open>odd ((\<Sum>a\<in>{q. n \<le> q \<and> q < m}. 2 ^ a div (2::nat) ^ n) + sum ((^) 2) {q. q < n} div 2 ^ n)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   609
      by (simp_all add: euclidean_semiring_cancel_class.power_diff_power_eq semiring_parity_class.even_sum_iff not_less mask_eq_sum_exp_nat [symmetric])
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   610
    ultimately have \<open>odd (sum ((^) (2::nat)) {q. q < m} div 2 ^ n)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   611
      by (subst euclidean_semiring_cancel_class.sum_div_partition) simp_all
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   612
    with False show ?thesis
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   613
      by (simp add: mask_eq_sum_exp_nat)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   614
  qed
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   615
  finally show ?thesis .
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   616
qed
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   617
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   618
end
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   619
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   620
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69502
diff changeset
   621
subsection \<open>Instance for \<^typ>\<open>int\<close>\<close>
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   622
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   623
lemma even_diff_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   624
  "even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   625
  by (fact even_diff)
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   626
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   627
lemma even_abs_add_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   628
  "even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   629
  by simp
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   630
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   631
lemma even_add_abs_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   632
  "even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   633
  by simp
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   634
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   635
lemma even_nat_iff: "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k"
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74101
diff changeset
   636
  by (simp add: even_of_nat_iff [of "nat k", where ?'a = int, symmetric])
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   637
71837
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   638
context
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   639
  assumes "SORT_CONSTRAINT('a::division_ring)"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   640
begin
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   641
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   642
lemma power_int_minus_left:
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   643
  "power_int (-a :: 'a) n = (if even n then power_int a n else -power_int a n)"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   644
  by (auto simp: power_int_def minus_one_power_iff even_nat_iff)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   645
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   646
lemma power_int_minus_left_even [simp]: "even n \<Longrightarrow> power_int (-a :: 'a) n = power_int a n"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   647
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   648
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   649
lemma power_int_minus_left_odd [simp]: "odd n \<Longrightarrow> power_int (-a :: 'a) n = -power_int a n"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   650
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   651
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   652
lemma power_int_minus_left_distrib:
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   653
  "NO_MATCH (-1) x \<Longrightarrow> power_int (-a :: 'a) n = power_int (-1) n * power_int a n"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   654
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   655
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   656
lemma power_int_minus_one_minus: "power_int (-1 :: 'a) (-n) = power_int (-1) n"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   657
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   658
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   659
lemma power_int_minus_one_diff_commute: "power_int (-1 :: 'a) (a - b) = power_int (-1) (b - a)"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   660
  by (subst power_int_minus_one_minus [symmetric]) auto
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   661
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   662
lemma power_int_minus_one_mult_self [simp]:
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   663
  "power_int (-1 :: 'a) m * power_int (-1) m = 1"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   664
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   665
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   666
lemma power_int_minus_one_mult_self' [simp]:
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   667
  "power_int (-1 :: 'a) m * (power_int (-1) m * b) = b"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   668
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   669
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   670
end
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   671
75937
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   672
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   673
subsection \<open>Computing congruences modulo \<open>2 ^ q\<close>\<close>
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   674
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   675
context unique_euclidean_semiring_with_nat_division
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   676
begin
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   677
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   678
lemma cong_exp_iff_simps:
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   679
  "numeral n mod numeral Num.One = 0
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   680
    \<longleftrightarrow> True"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   681
  "numeral (Num.Bit0 n) mod numeral (Num.Bit0 q) = 0
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   682
    \<longleftrightarrow> numeral n mod numeral q = 0"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   683
  "numeral (Num.Bit1 n) mod numeral (Num.Bit0 q) = 0
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   684
    \<longleftrightarrow> False"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   685
  "numeral m mod numeral Num.One = (numeral n mod numeral Num.One)
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   686
    \<longleftrightarrow> True"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   687
  "numeral Num.One mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q))
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   688
    \<longleftrightarrow> True"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   689
  "numeral Num.One mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q))
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   690
    \<longleftrightarrow> False"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   691
  "numeral Num.One mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q))
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   692
    \<longleftrightarrow> (numeral n mod numeral q) = 0"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   693
  "numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q))
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   694
    \<longleftrightarrow> False"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   695
  "numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q))
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   696
    \<longleftrightarrow> numeral m mod numeral q = (numeral n mod numeral q)"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   697
  "numeral (Num.Bit0 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q))
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   698
    \<longleftrightarrow> False"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   699
  "numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral Num.One mod numeral (Num.Bit0 q))
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   700
    \<longleftrightarrow> (numeral m mod numeral q) = 0"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   701
  "numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit0 n) mod numeral (Num.Bit0 q))
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   702
    \<longleftrightarrow> False"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   703
  "numeral (Num.Bit1 m) mod numeral (Num.Bit0 q) = (numeral (Num.Bit1 n) mod numeral (Num.Bit0 q))
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   704
    \<longleftrightarrow> numeral m mod numeral q = (numeral n mod numeral q)"
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   705
  by (auto simp add: case_prod_beta dest: arg_cong [of _ _ even])
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   706
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   707
end
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   708
02b18f59f903 streamlined
haftmann
parents: 74592
diff changeset
   709
71853
30d92e668b52 tuned module name space for generated code
haftmann
parents: 71837
diff changeset
   710
code_identifier
30d92e668b52 tuned module name space for generated code
haftmann
parents: 71837
diff changeset
   711
  code_module Parity \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
30d92e668b52 tuned module name space for generated code
haftmann
parents: 71837
diff changeset
   712
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74101
diff changeset
   713
lemmas even_of_nat = even_of_nat_iff
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74101
diff changeset
   714
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   715
end