src/HOL/Parity.thy
changeset 79117 7476818dfd5d
parent 78937 5e6b195eee83
child 79118 486a32079c60
equal deleted inserted replaced
79116:b90bf6636260 79117:7476818dfd5d
    38 instance int :: ring_parity
    38 instance int :: ring_parity
    39   by standard (auto simp add: dvd_eq_mod_eq_0)
    39   by standard (auto simp add: dvd_eq_mod_eq_0)
    40 
    40 
    41 context semiring_parity
    41 context semiring_parity
    42 begin
    42 begin
    43 
       
    44 lemma parity_cases [case_names even odd]:
       
    45   assumes "even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P"
       
    46   assumes "odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P"
       
    47   shows P
       
    48   using assms by (cases "even a")
       
    49     (simp_all add: even_iff_mod_2_eq_zero [symmetric] odd_iff_mod_2_eq_one [symmetric])
       
    50 
       
    51 lemma odd_of_bool_self [simp]:
       
    52   \<open>odd (of_bool p) \<longleftrightarrow> p\<close>
       
    53   by (cases p) simp_all
       
    54 
       
    55 lemma not_mod_2_eq_0_eq_1 [simp]:
       
    56   "a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1"
       
    57   by (cases a rule: parity_cases) simp_all
       
    58 
       
    59 lemma not_mod_2_eq_1_eq_0 [simp]:
       
    60   "a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"
       
    61   by (cases a rule: parity_cases) simp_all
       
    62 
    43 
    63 lemma evenE [elim?]:
    44 lemma evenE [elim?]:
    64   assumes "even a"
    45   assumes "even a"
    65   obtains b where "a = 2 * b"
    46   obtains b where "a = 2 * b"
    66   using assms by (rule dvdE)
    47   using assms by (rule dvdE)
    75     by (simp add: odd_iff_mod_2_eq_one)
    56     by (simp add: odd_iff_mod_2_eq_one)
    76   then show ?thesis ..
    57   then show ?thesis ..
    77 qed
    58 qed
    78 
    59 
    79 lemma mod_2_eq_odd:
    60 lemma mod_2_eq_odd:
    80   "a mod 2 = of_bool (odd a)"
    61   \<open>a mod 2 = of_bool (odd a)\<close>
    81   by (auto elim: oddE simp add: even_iff_mod_2_eq_zero)
    62   by (simp_all flip: odd_iff_mod_2_eq_one even_iff_mod_2_eq_zero)
    82 
    63 
    83 lemma of_bool_odd_eq_mod_2:
    64 lemma of_bool_odd_eq_mod_2:
    84   "of_bool (odd a) = a mod 2"
    65   \<open>of_bool (odd a) = a mod 2\<close>
       
    66   by (simp add: mod_2_eq_odd)
       
    67 
       
    68 lemma odd_of_bool_self [simp]:
       
    69   \<open>odd (of_bool p) \<longleftrightarrow> p\<close>
       
    70   by (cases p) simp_all
       
    71 
       
    72 lemma not_mod_2_eq_0_eq_1 [simp]:
       
    73   \<open>a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1\<close>
       
    74   by (simp add: mod_2_eq_odd)
       
    75 
       
    76 lemma not_mod_2_eq_1_eq_0 [simp]:
       
    77   \<open>a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0\<close>
    85   by (simp add: mod_2_eq_odd)
    78   by (simp add: mod_2_eq_odd)
    86 
    79 
    87 lemma even_mod_2_iff [simp]:
    80 lemma even_mod_2_iff [simp]:
    88   \<open>even (a mod 2) \<longleftrightarrow> even a\<close>
    81   \<open>even (a mod 2) \<longleftrightarrow> even a\<close>
    89   by (simp add: mod_2_eq_odd)
    82   by (simp add: mod_2_eq_odd)
    90 
    83 
    91 lemma mod2_eq_if:
    84 lemma mod2_eq_if:
    92   "a mod 2 = (if even a then 0 else 1)"
    85   "a mod 2 = (if even a then 0 else 1)"
    93   by (simp add: mod_2_eq_odd)
    86   by (simp add: mod_2_eq_odd)
    94 
    87 
       
    88 lemma zero_mod_two_eq_zero [simp]:
       
    89   \<open>0 mod 2 = 0\<close>
       
    90   by (simp add: mod_2_eq_odd)
       
    91 
       
    92 lemma one_mod_two_eq_one [simp]:
       
    93   \<open>1 mod 2 = 1\<close>
       
    94   by (simp add: mod_2_eq_odd)
       
    95 
       
    96 lemma parity_cases [case_names even odd]:
       
    97   assumes \<open>even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P\<close>
       
    98   assumes \<open>odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P\<close>
       
    99   shows P
       
   100   using assms by (auto simp add: mod_2_eq_odd)
       
   101 
    95 lemma even_zero [simp]:
   102 lemma even_zero [simp]:
    96   "even 0"
   103   \<open>even 0\<close>
    97   by (fact dvd_0_right)
   104   by (fact dvd_0_right)
    98 
   105 
    99 lemma odd_even_add:
   106 lemma odd_even_add:
   100   "even (a + b)" if "odd a" and "odd b"
   107   "even (a + b)" if "odd a" and "odd b"
   101 proof -
   108 proof -
   615 
   622 
   616 lemma one_div_two_eq_zero [simp]:
   623 lemma one_div_two_eq_zero [simp]:
   617   "1 div 2 = 0"
   624   "1 div 2 = 0"
   618 proof -
   625 proof -
   619   from of_nat_div [symmetric] have "of_nat 1 div of_nat 2 = of_nat 0"
   626   from of_nat_div [symmetric] have "of_nat 1 div of_nat 2 = of_nat 0"
   620     by (simp only:) simp
       
   621   then show ?thesis
       
   622     by simp
       
   623 qed
       
   624 
       
   625 lemma one_mod_two_eq_one [simp]:
       
   626   "1 mod 2 = 1"
       
   627 proof -
       
   628   from of_nat_mod [symmetric] have "of_nat 1 mod of_nat 2 = of_nat 1"
       
   629     by (simp only:) simp
   627     by (simp only:) simp
   630   then show ?thesis
   628   then show ?thesis
   631     by simp
   629     by simp
   632 qed
   630 qed
   633 
   631