
diff -r bf86b2002c96 -r d6cf9a5b9be9 src/HOL/Word/Bit_Representation.thy
--- a/src/HOL/Word/Bit_Representation.thy	Mon Dec 23 09:21:38 2013 +0100
+++ b/src/HOL/Word/Bit_Representation.thy	Mon Dec 23 14:24:20 2013 +0100
@@ -5,38 +5,34 @@
 header {* Integers as implict bit strings *}
 
 theory Bit_Representation
-imports "~~/src/HOL/Library/Bit" Misc_Numeric
+imports Misc_Numeric
 begin
 
 subsection {* Constructors and destructors for binary integers *}
 
-definition bitval :: "bit \<Rightarrow> 'a\<Colon>zero_neq_one" where
-  "bitval = bit_case 0 1"
-
-lemma bitval_simps [simp]:
-  "bitval 0 = 0"
-  "bitval 1 = 1"
-  by (simp_all add: bitval_def)
-
-definition Bit :: "int \<Rightarrow> bit \<Rightarrow> int" (infixl "BIT" 90) where
-  "k BIT b = bitval b + k + k"
+definition Bit :: "int \<Rightarrow> bool \<Rightarrow> int" (infixl "BIT" 90) where
+  "k BIT b = (if b then 1 else 0) + k + k"
 
 lemma Bit_B0:
-  "k BIT (0::bit) = k + k"
+  "k BIT False = k + k"
    by (unfold Bit_def) simp
 
 lemma Bit_B1:
-  "k BIT (1::bit) = k + k + 1"
+  "k BIT True = k + k + 1"
    by (unfold Bit_def) simp
   
-lemma Bit_B0_2t: "k BIT (0::bit) = 2 * k"
+lemma Bit_B0_2t: "k BIT False = 2 * k"
   by (rule trans, rule Bit_B0) simp
 
-lemma Bit_B1_2t: "k BIT (1::bit) = 2 * k + 1"
+lemma Bit_B1_2t: "k BIT True = 2 * k + 1"
   by (rule trans, rule Bit_B1) simp
 
-definition bin_last :: "int \<Rightarrow> bit" where
-  "bin_last w = (if w mod 2 = 0 then (0::bit) else (1::bit))"
+definition bin_last :: "int \<Rightarrow> bool" where
+  "bin_last w \<longleftrightarrow> w mod 2 = 1"
+
+lemma bin_last_odd:
+  "bin_last = odd"
+  by (rule ext) (simp add: bin_last_def even_def)
 
 definition bin_rest :: "int \<Rightarrow> int" where
   "bin_rest w = w div 2"
@@ -56,48 +52,55 @@
   by (cases b, simp_all add: z1pmod2)
 
 lemma BIT_eq_iff [iff]: "u BIT b = v BIT c \<longleftrightarrow> u = v \<and> b = c"
-  by (metis bin_rest_BIT bin_last_BIT)
+  apply (auto simp add: Bit_def)
+  apply arith
+  apply arith
+  done
 
 lemma BIT_bin_simps [simp]:
-  "numeral k BIT 0 = numeral (Num.Bit0 k)"
-  "numeral k BIT 1 = numeral (Num.Bit1 k)"
-  "(- numeral k) BIT 0 = - numeral (Num.Bit0 k)"
-  "(- numeral k) BIT 1 = - numeral (Num.BitM k)"
+  "numeral k BIT False = numeral (Num.Bit0 k)"
+  "numeral k BIT True = numeral (Num.Bit1 k)"
+  "(- numeral k) BIT False = - numeral (Num.Bit0 k)"
+  "(- numeral k) BIT True = - numeral (Num.BitM k)"
   unfolding numeral.simps numeral_BitM
-  unfolding Bit_def bitval_simps
+  unfolding Bit_def
   by (simp_all del: arith_simps add_numeral_special diff_numeral_special)
 
 lemma BIT_special_simps [simp]:
-  shows "0 BIT 0 = 0" and "0 BIT 1 = 1"
-  and "1 BIT 0 = 2" and "1 BIT 1 = 3"
-  and "(- 1) BIT 0 = - 2" and "(- 1) BIT 1 = - 1"
+  shows "0 BIT False = 0" and "0 BIT True = 1"
+  and "1 BIT False = 2" and "1 BIT True = 3"
+  and "(- 1) BIT False = - 2" and "(- 1) BIT True = - 1"
   unfolding Bit_def by simp_all
 
-lemma Bit_eq_0_iff: "w BIT b = 0 \<longleftrightarrow> w = 0 \<and> b = 0"
-  by (subst BIT_eq_iff [symmetric], simp)
+lemma Bit_eq_0_iff: "w BIT b = 0 \<longleftrightarrow> w = 0 \<and> \<not> b"
+  apply (auto simp add: Bit_def)
+  apply arith
+  done
 
-lemma Bit_eq_m1_iff: "w BIT b = - 1 \<longleftrightarrow> w = - 1 \<and> b = 1"
-  by (cases b) (auto simp add: Bit_def, arith)
+lemma Bit_eq_m1_iff: "w BIT b = -1 \<longleftrightarrow> w = -1 \<and> b"
+  apply (auto simp add: Bit_def)
+  apply arith
+  done
 
 lemma BitM_inc: "Num.BitM (Num.inc w) = Num.Bit1 w"
   by (induct w, simp_all)
 
 lemma expand_BIT:
-  "numeral (Num.Bit0 w) = numeral w BIT 0"
-  "numeral (Num.Bit1 w) = numeral w BIT 1"
-  "- numeral (Num.Bit0 w) = - numeral w BIT 0"
-  "- numeral (Num.Bit1 w) = (- numeral (w + Num.One)) BIT 1"
+  "numeral (Num.Bit0 w) = numeral w BIT False"
+  "numeral (Num.Bit1 w) = numeral w BIT True"
+  "- numeral (Num.Bit0 w) = (- numeral w) BIT False"
+  "- numeral (Num.Bit1 w) = (- numeral (w + Num.One)) BIT True"
   unfolding add_One by (simp_all add: BitM_inc)
 
 lemma bin_last_numeral_simps [simp]:
-  "bin_last 0 = 0"
-  "bin_last 1 = 1"
-  "bin_last -1 = 1"
-  "bin_last Numeral1 = 1"
-  "bin_last (numeral (Num.Bit0 w)) = 0"
-  "bin_last (numeral (Num.Bit1 w)) = 1"
-  "bin_last (- numeral (Num.Bit0 w)) = 0"
-  "bin_last (- numeral (Num.Bit1 w)) = 1"
+  "\<not> bin_last 0"
+  "bin_last 1"
+  "bin_last -1"
+  "bin_last Numeral1"
+  "\<not> bin_last (numeral (Num.Bit0 w))"
+  "bin_last (numeral (Num.Bit1 w))"
+  "\<not> bin_last (- numeral (Num.Bit0 w))"
+  "bin_last (- numeral (Num.Bit1 w))"
   unfolding expand_BIT bin_last_BIT by (simp_all add: bin_last_def zmod_zminus1_eq_if)
 
 lemma bin_rest_numeral_simps [simp]:
@@ -112,51 +115,42 @@
   unfolding expand_BIT bin_rest_BIT by (simp_all add: bin_rest_def zdiv_zminus1_eq_if)
 
 lemma less_Bits: 
-  "(v BIT b < w BIT c) = (v < w | v <= w & b = (0::bit) & c = (1::bit))"
-  unfolding Bit_def by (auto simp add: bitval_def split: bit.split)
+  "v BIT b < w BIT c \<longleftrightarrow> v < w \<or> v \<le> w \<and> \<not> b \<and> c"
+  unfolding Bit_def by auto
 
 lemma le_Bits: 
-  "(v BIT b <= w BIT c) = (v < w | v <= w & (b ~= (1::bit) | c ~= (0::bit)))" 
-  unfolding Bit_def by (auto simp add: bitval_def split: bit.split)
+  "v BIT b \<le> w BIT c \<longleftrightarrow> v < w \<or> v \<le> w \<and> (\<not> b \<or> c)" 
+  unfolding Bit_def by auto
 
 lemma pred_BIT_simps [simp]:
-  "x BIT 0 - 1 = (x - 1) BIT 1"
-  "x BIT 1 - 1 = x BIT 0"
+  "x BIT False - 1 = (x - 1) BIT True"
+  "x BIT True - 1 = x BIT False"
   by (simp_all add: Bit_B0_2t Bit_B1_2t)
 
 lemma succ_BIT_simps [simp]:
-  "x BIT 0 + 1 = x BIT 1"
-  "x BIT 1 + 1 = (x + 1) BIT 0"
+  "x BIT False + 1 = x BIT True"
+  "x BIT True + 1 = (x + 1) BIT False"
   by (simp_all add: Bit_B0_2t Bit_B1_2t)
 
 lemma add_BIT_simps [simp]:
-  "x BIT 0 + y BIT 0 = (x + y) BIT 0"
-  "x BIT 0 + y BIT 1 = (x + y) BIT 1"
-  "x BIT 1 + y BIT 0 = (x + y) BIT 1"
-  "x BIT 1 + y BIT 1 = (x + y + 1) BIT 0"
+  "x BIT False + y BIT False = (x + y) BIT False"
+  "x BIT False + y BIT True = (x + y) BIT True"
+  "x BIT True + y BIT False = (x + y) BIT True"
+  "x BIT True + y BIT True = (x + y + 1) BIT False"
   by (simp_all add: Bit_B0_2t Bit_B1_2t)
 
 lemma mult_BIT_simps [simp]:
-  "x BIT 0 * y = (x * y) BIT 0"
-  "x * y BIT 0 = (x * y) BIT 0"
-  "x BIT 1 * y = (x * y) BIT 0 + y"
+  "x BIT False * y = (x * y) BIT False"
+  "x * y BIT False = (x * y) BIT False"
+  "x BIT True * y = (x * y) BIT False + y"
   by (simp_all add: Bit_B0_2t Bit_B1_2t algebra_simps)
 
 lemma B_mod_2': 
-  "X = 2 ==> (w BIT (1::bit)) mod X = 1 & (w BIT (0::bit)) mod X = 0"
+  "X = 2 ==> (w BIT True) mod X = 1 & (w BIT False) mod X = 0"
   apply (simp (no_asm) only: Bit_B0 Bit_B1)
   apply (simp add: z1pmod2)
   done
 
-lemma neB1E [elim!]:
-  assumes ne: "y \<noteq> (1::bit)"
-  assumes y: "y = (0::bit) \<Longrightarrow> P"
-  shows "P"
-  apply (rule y)
-  apply (cases y rule: bit.exhaust, simp)
-  apply (simp add: ne)
-  done
-
 lemma bin_ex_rl: "EX w b. w BIT b = bin"
   by (metis bin_rl_simp)
 
@@ -170,8 +164,10 @@
   done
 
 primrec bin_nth where
-  Z: "bin_nth w 0 = (bin_last w = (1::bit))"
-  | Suc: "bin_nth w (Suc n) = bin_nth (bin_rest w) n"
+  Z: "bin_nth w 0 \<longleftrightarrow> bin_last w"
+  | Suc: "bin_nth w (Suc n) \<longleftrightarrow> bin_nth (bin_rest w) n"
+
+find_theorems "bin_rest _ = _"
 
 lemma bin_abs_lem:
   "bin = (w BIT b) ==> bin ~= -1 --> bin ~= 0 -->
@@ -248,7 +244,7 @@
 lemma bin_nth_minus1 [simp]: "bin_nth -1 n"
   by (induct n) auto
 
-lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 = (b = (1::bit))"
+lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 \<longleftrightarrow> b"
   by auto
 
 lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n"
@@ -285,8 +281,8 @@
   "bin_sign (numeral k) = 0"
   "bin_sign (- numeral k) = -1"
   "bin_sign (w BIT b) = bin_sign w"
-  unfolding bin_sign_def Bit_def bitval_def
-  by (simp_all split: bit.split)
+  unfolding bin_sign_def Bit_def
+  by simp_all
 
 lemma bin_sign_rest [simp]: 
   "bin_sign (bin_rest w) = bin_sign w"
@@ -297,7 +293,7 @@
 | Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)"
 
 primrec sbintrunc :: "nat => int => int" where
-  Z : "sbintrunc 0 bin = (case bin_last bin of (1::bit) \<Rightarrow> -1 | (0::bit) \<Rightarrow> 0)"
+  Z : "sbintrunc 0 bin = (if bin_last bin then -1 else 0)"
 | Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)"
 
 lemma sign_bintr: "bin_sign (bintrunc n w) = 0"
@@ -313,7 +309,8 @@
    apply simp
    apply (subst mod_add_left_eq)
    apply (simp add: bin_last_def)
-  apply (simp add: bin_last_def bin_rest_def Bit_def)
+   apply arith
+  apply (simp add: bin_last_def bin_rest_def Bit_def mod_2_neq_1_eq_eq_0)
   apply (clarsimp simp: mod_mult_mult1 [symmetric] 
          zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2 [THEN sym]]])
   apply (rule trans [symmetric, OF _ emep1])
@@ -334,13 +331,13 @@
 
 lemma bintrunc_Suc_numeral:
   "bintrunc (Suc n) 1 = 1"
-  "bintrunc (Suc n) -1 = bintrunc n -1 BIT 1"
-  "bintrunc (Suc n) (numeral (Num.Bit0 w)) = bintrunc n (numeral w) BIT 0"
-  "bintrunc (Suc n) (numeral (Num.Bit1 w)) = bintrunc n (numeral w) BIT 1"
+  "bintrunc (Suc n) -1 = bintrunc n -1 BIT True"
+  "bintrunc (Suc n) (numeral (Num.Bit0 w)) = bintrunc n (numeral w) BIT False"
+  "bintrunc (Suc n) (numeral (Num.Bit1 w)) = bintrunc n (numeral w) BIT True"
   "bintrunc (Suc n) (- numeral (Num.Bit0 w)) =
-    bintrunc n (- numeral w) BIT 0"
+    bintrunc n (- numeral w) BIT False"
   "bintrunc (Suc n) (- numeral (Num.Bit1 w)) =
-    bintrunc n (- numeral (w + Num.One)) BIT 1"
+    bintrunc n (- numeral (w + Num.One)) BIT True"
   by simp_all
 
 lemma sbintrunc_0_numeral [simp]:
@@ -354,21 +351,15 @@
 lemma sbintrunc_Suc_numeral:
   "sbintrunc (Suc n) 1 = 1"
   "sbintrunc (Suc n) (numeral (Num.Bit0 w)) =
-    sbintrunc n (numeral w) BIT 0"
+    sbintrunc n (numeral w) BIT False"
   "sbintrunc (Suc n) (numeral (Num.Bit1 w)) =
-    sbintrunc n (numeral w) BIT 1"
+    sbintrunc n (numeral w) BIT True"
   "sbintrunc (Suc n) (- numeral (Num.Bit0 w)) =
-    sbintrunc n (- numeral w) BIT 0"
+    sbintrunc n (- numeral w) BIT False"
   "sbintrunc (Suc n) (- numeral (Num.Bit1 w)) =
-    sbintrunc n (- numeral (w + Num.One)) BIT 1"
+    sbintrunc n (- numeral (w + Num.One)) BIT True"
   by simp_all
 
-lemma bit_bool:
-  "(b = (b' = (1::bit))) = (b' = (if b then (1::bit) else (0::bit)))"
-  by (cases b') auto
-
-lemmas bit_bool1 [simp] = refl [THEN bit_bool [THEN iffD1], symmetric]
-
 lemma bin_sign_lem: "(bin_sign (sbintrunc n bin) = -1) = bin_nth bin n"
   apply (induct n arbitrary: bin)
   apply (case_tac bin rule: bin_exhaust, case_tac b, auto)
@@ -384,23 +375,25 @@
   "bin_nth (sbintrunc m w) n = 
           (if n < m then bin_nth w n else bin_nth w m)"
   apply (induct n arbitrary: w m)
-   apply (case_tac m, simp_all split: bit.splits)[1]
-  apply (case_tac m, simp_all split: bit.splits)[1]
+  apply (case_tac m)
+  apply simp_all
+  apply (case_tac m)
+  apply simp_all
   done
 
 lemma bin_nth_Bit:
-  "bin_nth (w BIT b) n = (n = 0 & b = (1::bit) | (EX m. n = Suc m & bin_nth w m))"
+  "bin_nth (w BIT b) n = (n = 0 & b | (EX m. n = Suc m & bin_nth w m))"
   by (cases n) auto
 
 lemma bin_nth_Bit0:
   "bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow>
     (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
-  using bin_nth_Bit [where w="numeral w" and b="(0::bit)"] by simp
+  using bin_nth_Bit [where w="numeral w" and b="False"] by simp
 
 lemma bin_nth_Bit1:
   "bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow>
     n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
-  using bin_nth_Bit [where w="numeral w" and b="(1::bit)"] by simp
+  using bin_nth_Bit [where w="numeral w" and b="True"] by simp
 
 lemma bintrunc_bintrunc_l:
   "n <= m ==> (bintrunc m (bintrunc n w) = bintrunc n w)"
@@ -452,19 +445,19 @@
 
 lemmas sbintrunc_Pls = 
   sbintrunc.Z [where bin="0", 
-               simplified bin_last_numeral_simps bin_rest_numeral_simps bit.simps]
+               simplified bin_last_numeral_simps bin_rest_numeral_simps]
 
 lemmas sbintrunc_Min = 
   sbintrunc.Z [where bin="-1",
-               simplified bin_last_numeral_simps bin_rest_numeral_simps bit.simps]
+               simplified bin_last_numeral_simps bin_rest_numeral_simps]
 
 lemmas sbintrunc_0_BIT_B0 [simp] = 
-  sbintrunc.Z [where bin="w BIT (0::bit)", 
-               simplified bin_last_numeral_simps bin_rest_numeral_simps bit.simps] for w
+  sbintrunc.Z [where bin="w BIT False", 
+               simplified bin_last_numeral_simps bin_rest_numeral_simps] for w
 
 lemmas sbintrunc_0_BIT_B1 [simp] = 
-  sbintrunc.Z [where bin="w BIT (1::bit)", 
-               simplified bin_last_BIT bin_rest_numeral_simps bit.simps] for w
+  sbintrunc.Z [where bin="w BIT True", 
+               simplified bin_last_BIT bin_rest_numeral_simps] for w
 
 lemmas sbintrunc_0_simps =
   sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1
@@ -583,25 +576,25 @@
 
 lemma bintrunc_numeral_simps [simp]:
   "bintrunc (numeral k) (numeral (Num.Bit0 w)) =
-    bintrunc (pred_numeral k) (numeral w) BIT 0"
+    bintrunc (pred_numeral k) (numeral w) BIT False"
   "bintrunc (numeral k) (numeral (Num.Bit1 w)) =
-    bintrunc (pred_numeral k) (numeral w) BIT 1"
+    bintrunc (pred_numeral k) (numeral w) BIT True"
   "bintrunc (numeral k) (- numeral (Num.Bit0 w)) =
-    bintrunc (pred_numeral k) (- numeral w) BIT 0"
+    bintrunc (pred_numeral k) (- numeral w) BIT False"
   "bintrunc (numeral k) (- numeral (Num.Bit1 w)) =
-    bintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT 1"
+    bintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT True"
   "bintrunc (numeral k) 1 = 1"
   by (simp_all add: bintrunc_numeral)
 
 lemma sbintrunc_numeral_simps [simp]:
   "sbintrunc (numeral k) (numeral (Num.Bit0 w)) =
-    sbintrunc (pred_numeral k) (numeral w) BIT 0"
+    sbintrunc (pred_numeral k) (numeral w) BIT False"
   "sbintrunc (numeral k) (numeral (Num.Bit1 w)) =
-    sbintrunc (pred_numeral k) (numeral w) BIT 1"
+    sbintrunc (pred_numeral k) (numeral w) BIT True"
   "sbintrunc (numeral k) (- numeral (Num.Bit0 w)) =
-    sbintrunc (pred_numeral k) (- numeral w) BIT 0"
+    sbintrunc (pred_numeral k) (- numeral w) BIT False"
   "sbintrunc (numeral k) (- numeral (Num.Bit1 w)) =
-    sbintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT 1"
+    sbintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT True"
   "sbintrunc (numeral k) 1 = 1"
   by (simp_all add: sbintrunc_numeral)
 
@@ -728,7 +721,7 @@
   "sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)"
   apply (induct n arbitrary: bin, simp)
   apply (case_tac bin rule: bin_exhaust)
-  apply (auto simp: bintrunc_bintrunc_l split: bit.splits)
+  apply (auto simp: bintrunc_bintrunc_l split: bool.splits)
   done
 
 lemma bintrunc_rest':




