# HG changeset patch # User haftmann # Date 1580933820 0 # Node ID 572ab9e64e182bd49bdf1444d4c3c848bd915fd0 # Parent 1d8e914e04d67b933648c666dac00354618dcbcd simplified logical constructions diff -r 1d8e914e04d6 -r 572ab9e64e18 src/HOL/Library/More_List.thy --- a/src/HOL/Library/More_List.thy Wed Feb 05 20:16:59 2020 +0000 +++ b/src/HOL/Library/More_List.thy Wed Feb 05 20:17:00 2020 +0000 @@ -378,5 +378,18 @@ by simp qed +lemma nth_default_map2: + \nth_default d (map2 f xs ys) n = f (nth_default d1 xs n) (nth_default d2 ys n)\ + if \length xs = length ys\ and \f d1 d2 = d\ for bs cs +using that proof (induction xs ys arbitrary: n rule: list_induct2) + case Nil + then show ?case + by simp +next + case (Cons x xs y ys) + then show ?case + by (cases n) simp_all +qed + end diff -r 1d8e914e04d6 -r 572ab9e64e18 src/HOL/ex/Bit_Lists.thy --- a/src/HOL/ex/Bit_Lists.thy Wed Feb 05 20:16:59 2020 +0000 +++ b/src/HOL/ex/Bit_Lists.thy Wed Feb 05 20:17:00 2020 +0000 @@ -5,9 +5,19 @@ theory Bit_Lists imports - Word + Word "HOL-Library.More_List" begin +lemma hd_zip: + \hd (zip xs ys) = (hd xs, hd ys)\ if \xs \ []\ and \ys \ []\ + using that by (cases xs; cases ys) simp_all + +lemma last_zip: + \last (zip xs ys) = (last xs, last ys)\ if \xs \ []\ and \ys \ []\ + and \length xs = length ys\ + using that by (cases xs rule: rev_cases; cases ys rule: rev_cases) simp_all + + subsection \Fragments of algebraic bit representations\ context comm_semiring_1 @@ -36,7 +46,7 @@ simp_all add: algebra_simps) lemma unsigned_of_bits_take [simp]: - "unsigned_of_bits (take n bs) = Parity.take_bit n (unsigned_of_bits bs)" + "unsigned_of_bits (take n bs) = take_bit n (unsigned_of_bits bs)" proof (induction bs arbitrary: n) case Nil then show ?case @@ -59,6 +69,18 @@ by (cases n) simp_all qed +lemma bit_unsigned_of_bits_iff: + \bit (unsigned_of_bits bs) n \ nth_default False bs n\ +proof (induction bs arbitrary: n) + case Nil + then show ?case + by simp +next + case (Cons b bs) + then show ?case + by (cases n) simp_all +qed + primrec n_bits_of :: "nat \ 'a \ bool list" where "n_bits_of 0 a = []" @@ -125,6 +147,10 @@ end +lemma bit_of_bits_nat_iff: + \bit (of_bits bs :: nat) n \ nth_default False bs n\ + by (simp add: bit_unsigned_of_bits_iff) + lemma bits_of_Suc_0 [simp]: "bits_of (Suc 0) = [True]" by simp @@ -211,6 +237,18 @@ by (simp_all add: *) qed +lemma bit_of_bits_int_iff: + \bit (of_bits bs :: int) n \ nth_default (bs \ [] \ last bs) bs n\ +proof (induction bs arbitrary: n) + case Nil + then show ?case + by simp +next + case (Cons b bs) + then show ?case + by (cases n; cases b; cases bs) simp_all +qed + lemma of_bits_append [simp]: "of_bits (bs @ cs) = of_bits bs + push_bit (length bs) (of_bits cs :: int)" if "bs \ []" "\ last bs" @@ -291,24 +329,14 @@ class ring_bit_representation = ring_bit_operations + semiring_bit_representation + assumes not_eq: "not = of_bits \ map Not \ bits_of" - context zip_nat begin lemma of_bits: "of_bits bs \<^bold>\ of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: nat)" - if "length bs = length cs" for bs cs -using that proof (induction bs cs rule: list_induct2) - case Nil - then show ?case - by simp -next - case (Cons b bs c cs) - then show ?case - by (cases "of_bits bs = (0::nat) \ of_bits cs = (0::nat)") - (auto simp add: ac_simps end_of_bits rec [of "Suc 0"]) -qed - + if "length bs = length cs" for bs cs + by (simp add: Parity.bit_eq_iff bit_unsigned_of_bits_iff bit_eq_iff) + (simp add: that end_of_bits nth_default_map2 [of _ _ _ False False]) end instance nat :: semiring_bit_representation @@ -321,32 +349,22 @@ begin lemma of_bits: - "of_bits bs \<^bold>\ of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: int)" - if "length bs = length cs" and "\ False \<^bold>* False" for bs cs -using \length bs = length cs\ proof (induction bs cs rule: list_induct2) - case Nil - then show ?case - using \\ False \<^bold>* False\ by (auto simp add: False_False_imp_True_True split: bool.splits) + \of_bits bs \<^bold>\ of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: int)\ + if \length bs = length cs\ and \\ False \<^bold>* False\ for bs cs +proof (cases \bs = []\) + case True + moreover have \cs = []\ + using True that by simp + ultimately show ?thesis + by (simp add: Parity.bit_eq_iff bit_eq_iff that) next - case (Cons b bs c cs) - show ?case - proof (cases "bs = []") - case True - with Cons show ?thesis - using \\ False \<^bold>* False\ by (cases b; cases c) - (auto simp add: ac_simps split: bool.splits) - next - case False - with Cons.hyps have "cs \ []" - by auto - with \bs \ []\ have "map2 (\<^bold>*) bs cs \ []" - by (simp add: zip_eq_Nil_iff) - from \bs \ []\ \cs \ []\ \map2 (\<^bold>*) bs cs \ []\ Cons show ?thesis - by (cases b; cases c) - (auto simp add: \\ False \<^bold>* False\ ac_simps - rec [of "of_bits bs * 2"] rec [of "of_bits cs * 2"] - rec [of "1 + of_bits bs * 2"]) - qed + case False + moreover have \cs \ []\ + using False that by auto + ultimately show ?thesis + apply (simp add: Parity.bit_eq_iff bit_of_bits_int_iff bit_eq_iff zip_eq_Nil_iff last_map last_zip that) + apply (simp add: that nth_default_map2 [of _ _ _ \last bs\ \last cs\]) + done qed end diff -r 1d8e914e04d6 -r 572ab9e64e18 src/HOL/ex/Bit_Operations.thy --- a/src/HOL/ex/Bit_Operations.thy Wed Feb 05 20:16:59 2020 +0000 +++ b/src/HOL/ex/Bit_Operations.thy Wed Feb 05 20:17:00 2020 +0000 @@ -248,176 +248,92 @@ subsubsection \Instance \<^typ>\nat\\ locale zip_nat = single: abel_semigroup f - for f :: "bool \ bool \ bool" (infixl "\<^bold>*" 70) + - assumes end_of_bits: "\ False \<^bold>* False" + for f :: "bool \ bool \ bool" (infixl \\<^bold>*\ 70) + + assumes end_of_bits: \\ False \<^bold>* False\ begin -lemma False_P_imp: - "False \<^bold>* True \ P" if "False \<^bold>* P" - using that end_of_bits by (cases P) simp_all - -function F :: "nat \ nat \ nat" (infixl "\<^bold>\" 70) - where "m \<^bold>\ n = (if m = 0 \ n = 0 then 0 - else of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\ (n div 2) * 2)" +function F :: \nat \ nat \ nat\ (infixl \\<^bold>\\ 70) + where \m \<^bold>\ n = (if m = 0 \ n = 0 then 0 + else of_bool (odd m \<^bold>* odd n) + 2 * ((m div 2) \<^bold>\ (n div 2)))\ by auto termination by (relation "measure (case_prod (+))") auto -lemma zero_left_eq: - "0 \<^bold>\ n = of_bool (False \<^bold>* True) * n" - by (induction n rule: nat_bit_induct) (simp_all add: end_of_bits) - -lemma zero_right_eq: - "m \<^bold>\ 0 = of_bool (True \<^bold>* False) * m" - by (induction m rule: nat_bit_induct) (simp_all add: end_of_bits) - -lemma simps [simp]: - "0 \<^bold>\ 0 = 0" - "0 \<^bold>\ n = of_bool (False \<^bold>* True) * n" - "m \<^bold>\ 0 = of_bool (True \<^bold>* False) * m" - "m > 0 \ n > 0 \ m \<^bold>\ n = of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\ (n div 2) * 2" - by (simp_all only: zero_left_eq zero_right_eq) simp +declare F.simps [simp del] lemma rec: "m \<^bold>\ n = of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\ (n div 2) * 2" - by (cases "m = 0 \ n = 0") (auto simp add: end_of_bits) +proof (cases \m = 0 \ n = 0\) + case True + then have \m \<^bold>\ n = 0\ + using True by (simp add: F.simps [of 0 0]) + moreover have \(m div 2) \<^bold>\ (n div 2) = m \<^bold>\ n\ + using True by simp + ultimately show ?thesis + using True by (simp add: end_of_bits) +next + case False + then show ?thesis + by (auto simp add: ac_simps F.simps [of m n]) +qed -declare F.simps [simp del] +lemma bit_eq_iff: + \bit (m \<^bold>\ n) q \ bit m q \<^bold>* bit n q\ +proof (induction q arbitrary: m n) + case 0 + then show ?case + by (simp add: rec [of m n]) +next + case (Suc n) + then show ?case + by (simp add: rec [of m n]) +qed sublocale abel_semigroup F -proof - show "m \<^bold>\ n \<^bold>\ q = m \<^bold>\ (n \<^bold>\ q)" for m n q :: nat - proof (induction m arbitrary: n q rule: nat_bit_induct) - case zero - show ?case - by simp - next - case (even m) - with rec [of "2 * m"] rec [of _ q] show ?case - by (cases "even n") (auto simp add: ac_simps dest: False_P_imp) - next - case (odd m) - with rec [of "Suc (2 * m)"] rec [of _ q] show ?case - by (cases "even n"; cases "even q") - (auto dest: False_P_imp simp add: ac_simps) - qed - show "m \<^bold>\ n = n \<^bold>\ m" for m n :: nat - proof (induction m arbitrary: n rule: nat_bit_induct) - case zero - show ?case - by (simp add: ac_simps) - next - case (even m) - with rec [of "2 * m" n] rec [of n "2 * m"] show ?case - by (simp add: ac_simps) - next - case (odd m) - with rec [of "Suc (2 * m)" n] rec [of n "Suc (2 * m)"] show ?case - by (simp add: ac_simps) - qed -qed - -lemma self [simp]: - "n \<^bold>\ n = of_bool (True \<^bold>* True) * n" - by (induction n rule: nat_bit_induct) (simp_all add: end_of_bits) - -lemma even_iff [simp]: - "even (m \<^bold>\ n) \ \ (odd m \<^bold>* odd n)" -proof (induction m arbitrary: n rule: nat_bit_induct) - case zero - show ?case - by (cases "even n") (simp_all add: end_of_bits) -next - case (even m) - then show ?case - by (simp add: rec [of "2 * m"]) -next - case (odd m) - then show ?case - by (simp add: rec [of "Suc (2 * m)"]) -qed + by standard (simp_all add: Parity.bit_eq_iff bit_eq_iff ac_simps) end instantiation nat :: semiring_bit_operations begin -global_interpretation and_nat: zip_nat "(\)" +global_interpretation and_nat: zip_nat \(\)\ defines and_nat = and_nat.F by standard auto -global_interpretation and_nat: semilattice "(AND) :: nat \ nat \ nat" +global_interpretation and_nat: semilattice \(AND) :: nat \ nat \ nat\ proof (rule semilattice.intro, fact and_nat.abel_semigroup_axioms, standard) - show "n AND n = n" for n :: nat - by (simp add: and_nat.self) + show \n AND n = n\ for n :: nat + by (simp add: bit_eq_iff and_nat.bit_eq_iff) qed -declare and_nat.simps [simp] \ \inconsistent declaration handling by - \global_interpretation\ in \instantiation\\ - -global_interpretation or_nat: zip_nat "(\)" +global_interpretation or_nat: zip_nat \(\)\ defines or_nat = or_nat.F by standard auto -global_interpretation or_nat: semilattice "(OR) :: nat \ nat \ nat" +global_interpretation or_nat: semilattice \(OR) :: nat \ nat \ nat\ proof (rule semilattice.intro, fact or_nat.abel_semigroup_axioms, standard) - show "n OR n = n" for n :: nat - by (simp add: or_nat.self) + show \n OR n = n\ for n :: nat + by (simp add: bit_eq_iff or_nat.bit_eq_iff) qed -declare or_nat.simps [simp] \ \inconsistent declaration handling by - \global_interpretation\ in \instantiation\\ - -global_interpretation xor_nat: zip_nat "(\)" +global_interpretation xor_nat: zip_nat \(\)\ defines xor_nat = xor_nat.F by standard auto -declare xor_nat.simps [simp] \ \inconsistent declaration handling by - \global_interpretation\ in \instantiation\\ - instance proof fix m n q :: nat show \bit (m AND n) q \ bit m q \ bit n q\ - proof (rule sym, induction q arbitrary: m n) - case 0 - then show ?case - by (simp add: and_nat.even_iff) - next - case (Suc q) - with and_nat.rec [of m n] show ?case - by simp - qed + by (fact and_nat.bit_eq_iff) show \bit (m OR n) q \ bit m q \ bit n q\ - proof (rule sym, induction q arbitrary: m n) - case 0 - then show ?case - by (simp add: or_nat.even_iff) - next - case (Suc q) - with or_nat.rec [of m n] show ?case - by simp - qed + by (fact or_nat.bit_eq_iff) show \bit (m XOR n) q \ bit m q \ bit n q\ - proof (rule sym, induction q arbitrary: m n) - case 0 - then show ?case - by (simp add: xor_nat.even_iff) - next - case (Suc q) - with xor_nat.rec [of m n] show ?case - by simp - qed + by (fact xor_nat.bit_eq_iff) qed end -global_interpretation or_nat: semilattice_neutr "(OR)" "0 :: nat" - by standard simp - -global_interpretation xor_nat: comm_monoid "(XOR)" "0 :: nat" - by standard simp - lemma Suc_0_and_eq [simp]: \Suc 0 AND n = of_bool (odd n)\ using one_and_eq [of n] by simp @@ -445,314 +361,112 @@ subsubsection \Instance \<^typ>\int\\ -abbreviation (input) complement :: "int \ int" - where "complement k \ - k - 1" - -lemma complement_half: - "complement (k * 2) div 2 = complement k" - by simp - -lemma complement_div_2: - "complement (k div 2) = complement k div 2" - by linarith - locale zip_int = single: abel_semigroup f - for f :: "bool \ bool \ bool" (infixl "\<^bold>*" 70) + for f :: \bool \ bool \ bool\ (infixl \\<^bold>*\ 70) begin - -lemma False_False_imp_True_True: - "True \<^bold>* True" if "False \<^bold>* False" -proof (rule ccontr) - assume "\ True \<^bold>* True" - with that show False - using single.assoc [of False True True] - by (cases "False \<^bold>* True") simp_all -qed -function F :: "int \ int \ int" (infixl "\<^bold>\" 70) - where "k \<^bold>\ l = (if k \ {0, - 1} \ l \ {0, - 1} +function F :: \int \ int \ int\ (infixl \\<^bold>\\ 70) + where \k \<^bold>\ l = (if k \ {0, - 1} \ l \ {0, - 1} then - of_bool (odd k \<^bold>* odd l) - else of_bool (odd k \<^bold>* odd l) + (k div 2) \<^bold>\ (l div 2) * 2)" + else of_bool (odd k \<^bold>* odd l) + 2 * ((k div 2) \<^bold>\ (l div 2)))\ by auto termination by (relation "measure (\(k, l). nat (\k\ + \l\))") auto -lemma zero_left_eq: - "0 \<^bold>\ l = (case (False \<^bold>* False, False \<^bold>* True) - of (False, False) \ 0 - | (False, True) \ l - | (True, False) \ complement l - | (True, True) \ - 1)" - by (induction l rule: int_bit_induct) - (simp_all split: bool.split) - -lemma minus_left_eq: - "- 1 \<^bold>\ l = (case (True \<^bold>* False, True \<^bold>* True) - of (False, False) \ 0 - | (False, True) \ l - | (True, False) \ complement l - | (True, True) \ - 1)" - by (induction l rule: int_bit_induct) - (simp_all split: bool.split) - -lemma zero_right_eq: - "k \<^bold>\ 0 = (case (False \<^bold>* False, False \<^bold>* True) - of (False, False) \ 0 - | (False, True) \ k - | (True, False) \ complement k - | (True, True) \ - 1)" - by (induction k rule: int_bit_induct) - (simp_all add: ac_simps split: bool.split) - -lemma minus_right_eq: - "k \<^bold>\ - 1 = (case (True \<^bold>* False, True \<^bold>* True) - of (False, False) \ 0 - | (False, True) \ k - | (True, False) \ complement k - | (True, True) \ - 1)" - by (induction k rule: int_bit_induct) - (simp_all add: ac_simps split: bool.split) - -lemma simps [simp]: - "0 \<^bold>\ 0 = - of_bool (False \<^bold>* False)" - "- 1 \<^bold>\ 0 = - of_bool (True \<^bold>* False)" - "0 \<^bold>\ - 1 = - of_bool (False \<^bold>* True)" - "- 1 \<^bold>\ - 1 = - of_bool (True \<^bold>* True)" - "0 \<^bold>\ l = (case (False \<^bold>* False, False \<^bold>* True) - of (False, False) \ 0 - | (False, True) \ l - | (True, False) \ complement l - | (True, True) \ - 1)" - "- 1 \<^bold>\ l = (case (True \<^bold>* False, True \<^bold>* True) - of (False, False) \ 0 - | (False, True) \ l - | (True, False) \ complement l - | (True, True) \ - 1)" - "k \<^bold>\ 0 = (case (False \<^bold>* False, False \<^bold>* True) - of (False, False) \ 0 - | (False, True) \ k - | (True, False) \ complement k - | (True, True) \ - 1)" - "k \<^bold>\ - 1 = (case (True \<^bold>* False, True \<^bold>* True) - of (False, False) \ 0 - | (False, True) \ k - | (True, False) \ complement k - | (True, True) \ - 1)" - "k \ 0 \ k \ - 1 \ l \ 0 \ l \ - 1 - \ k \<^bold>\ l = of_bool (odd k \<^bold>* odd l) + (k div 2) \<^bold>\ (l div 2) * 2" - by simp_all[4] (simp_all only: zero_left_eq minus_left_eq zero_right_eq minus_right_eq, simp) - declare F.simps [simp del] lemma rec: - "k \<^bold>\ l = of_bool (odd k \<^bold>* odd l) + (k div 2) \<^bold>\ (l div 2) * 2" - by (cases "k \ {0, - 1} \ l \ {0, - 1}") (auto simp add: ac_simps F.simps [of k l] split: bool.split) + \k \<^bold>\ l = of_bool (odd k \<^bold>* odd l) + 2 * ((k div 2) \<^bold>\ (l div 2))\ +proof (cases \k \ {0, - 1} \ l \ {0, - 1}\) + case True + then have \(k div 2) \<^bold>\ (l div 2) = k \<^bold>\ l\ + by auto + moreover have \of_bool (odd k \<^bold>* odd l) = - (k \<^bold>\ l)\ + using True by (simp add: F.simps [of k l]) + ultimately show ?thesis by simp +next + case False + then show ?thesis + by (auto simp add: ac_simps F.simps [of k l]) +qed + +lemma bit_eq_iff: + \bit (k \<^bold>\ l) n \ bit k n \<^bold>* bit l n\ +proof (induction n arbitrary: k l) + case 0 + then show ?case + by (simp add: rec [of k l]) +next + case (Suc n) + then show ?case + by (simp add: rec [of k l]) +qed sublocale abel_semigroup F -proof - show "k \<^bold>\ l \<^bold>\ r = k \<^bold>\ (l \<^bold>\ r)" for k l r :: int - proof (induction k arbitrary: l r rule: int_bit_induct) - case zero - have "complement l \<^bold>\ r = complement (l \<^bold>\ r)" if "False \<^bold>* False" "\ False \<^bold>* True" - proof (induction l arbitrary: r rule: int_bit_induct) - case zero - from that show ?case - by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits) - next - case minus - from that show ?case - by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits) - next - case (even l) - with that rec [of _ r] show ?case - by (cases "even r") - (auto simp add: complement_half ac_simps False_False_imp_True_True split: bool.splits) - next - case (odd l) - moreover have "- l - 1 = - 1 - l" - by simp - ultimately show ?case - using that rec [of _ r] - by (cases "even r") - (auto simp add: ac_simps False_False_imp_True_True split: bool.splits) - qed - then show ?case - by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits) - next - case minus - have "complement l \<^bold>\ r = complement (l \<^bold>\ r)" if "\ True \<^bold>* True" "False \<^bold>* True" - proof (induction l arbitrary: r rule: int_bit_induct) - case zero - from that show ?case - by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits) - next - case minus - from that show ?case - by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits) - next - case (even l) - with that rec [of _ r] show ?case - by (cases "even r") - (auto simp add: complement_half ac_simps False_False_imp_True_True split: bool.splits) - next - case (odd l) - moreover have "- l - 1 = - 1 - l" - by simp - ultimately show ?case - using that rec [of _ r] - by (cases "even r") - (auto simp add: ac_simps False_False_imp_True_True split: bool.splits) - qed - then show ?case - by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits) - next - case (even k) - with rec [of "k * 2"] rec [of _ r] show ?case - by (cases "even r"; cases "even l") (auto simp add: ac_simps False_False_imp_True_True) - next - case (odd k) - with rec [of "1 + k * 2"] rec [of _ r] show ?case - by (cases "even r"; cases "even l") (auto simp add: ac_simps False_False_imp_True_True) - qed - show "k \<^bold>\ l = l \<^bold>\ k" for k l :: int - proof (induction k arbitrary: l rule: int_bit_induct) - case zero - show ?case - by simp - next - case minus - show ?case - by simp - next - case (even k) - with rec [of "k * 2" l] rec [of l "k * 2"] show ?case - by (simp add: ac_simps) - next - case (odd k) - with rec [of "k * 2 + 1" l] rec [of l "k * 2 + 1"] show ?case - by (simp add: ac_simps) - qed -qed - -lemma self [simp]: - "k \<^bold>\ k = (case (False \<^bold>* False, True \<^bold>* True) - of (False, False) \ 0 - | (False, True) \ k - | (True, True) \ - 1)" - by (induction k rule: int_bit_induct) (auto simp add: False_False_imp_True_True split: bool.split) - -lemma even_iff [simp]: - "even (k \<^bold>\ l) \ \ (odd k \<^bold>* odd l)" -proof (induction k arbitrary: l rule: int_bit_induct) - case zero - show ?case - by (cases "even l") (simp_all split: bool.splits) -next - case minus - show ?case - by (cases "even l") (simp_all split: bool.splits) -next - case (even k) - then show ?case - by (simp add: rec [of "k * 2"]) -next - case (odd k) - then show ?case - by (simp add: rec [of "1 + k * 2"]) -qed + by standard (simp_all add: Parity.bit_eq_iff bit_eq_iff ac_simps) end instantiation int :: ring_bit_operations begin -definition not_int :: "int \ int" - where "not_int = complement" - global_interpretation and_int: zip_int "(\)" defines and_int = and_int.F by standard -declare and_int.simps [simp] \ \inconsistent declaration handling by - \global_interpretation\ in \instantiation\\ - global_interpretation and_int: semilattice "(AND) :: int \ int \ int" proof (rule semilattice.intro, fact and_int.abel_semigroup_axioms, standard) show "k AND k = k" for k :: int - by (simp add: and_int.self) + by (simp add: bit_eq_iff and_int.bit_eq_iff) qed global_interpretation or_int: zip_int "(\)" defines or_int = or_int.F by standard -declare or_int.simps [simp] \ \inconsistent declaration handling by - \global_interpretation\ in \instantiation\\ - global_interpretation or_int: semilattice "(OR) :: int \ int \ int" proof (rule semilattice.intro, fact or_int.abel_semigroup_axioms, standard) show "k OR k = k" for k :: int - by (simp add: or_int.self) + by (simp add: bit_eq_iff or_int.bit_eq_iff) qed global_interpretation xor_int: zip_int "(\)" defines xor_int = xor_int.F by standard -declare xor_int.simps [simp] \ \inconsistent declaration handling by - \global_interpretation\ in \instantiation\\ +definition not_int :: \int \ int\ + where \not_int k = - k - 1\ + +lemma not_int_rec: + "NOT k = of_bool (even k) + 2 * NOT (k div 2)" for k :: int + by (auto simp add: not_int_def elim: oddE) + +lemma even_not_iff_int: + \even (NOT k) \ odd k\ for k :: int + by (simp add: not_int_def) + +lemma not_int_div_2: + \NOT k div 2 = NOT (k div 2)\ for k :: int + by (simp add: not_int_def) lemma bit_not_iff_int: \bit (NOT k) n \ \ bit k n\ for k :: int - by (induction n arbitrary: k) - (simp_all add: not_int_def flip: complement_div_2) + by (induction n arbitrary: k) (simp_all add: not_int_div_2 even_not_iff_int) instance proof fix k l :: int and n :: nat show \- k = NOT (k - 1)\ by (simp add: not_int_def) show \bit (k AND l) n \ bit k n \ bit l n\ - proof (rule sym, induction n arbitrary: k l) - case 0 - then show ?case - by (simp add: and_int.even_iff) - next - case (Suc n) - with and_int.rec [of k l] show ?case - by simp - qed + by (fact and_int.bit_eq_iff) show \bit (k OR l) n \ bit k n \ bit l n\ - proof (rule sym, induction n arbitrary: k l) - case 0 - then show ?case - by (simp add: or_int.even_iff) - next - case (Suc n) - with or_int.rec [of k l] show ?case - by simp - qed + by (fact or_int.bit_eq_iff) show \bit (k XOR l) n \ bit k n \ bit l n\ - proof (rule sym, induction n arbitrary: k l) - case 0 - then show ?case - by (simp add: xor_int.even_iff) - next - case (Suc n) - with xor_int.rec [of k l] show ?case - by simp - qed -qed (simp_all add: minus_1_div_exp_eq_int bit_not_iff_int) + by (fact xor_int.bit_eq_iff) +qed (simp_all add: bit_not_iff_int) end -lemma not_int_div_2: - "NOT k div 2 = NOT (k div 2)" for k :: int - by (simp add: complement_div_2 not_int_def) - -lemma not_int_rec [simp]: - "k \ 0 \ k \ - 1 \ NOT k = of_bool (even k) + 2 * NOT (k div 2)" for k :: int - by (auto simp add: not_int_def elim: oddE) - end