# Theory Word_Type

theory Word_Type
imports Type_Length
```(*  Author:  Florian Haftmann, TUM
*)

section ‹Proof of concept for algebraically founded bit word types›

theory Word_Type
imports
Main
"HOL-Library.Type_Length"
begin

subsection ‹Truncating bit representations of numeric types›

class semiring_bits = semiring_div_parity +
assumes semiring_bits: "(1 + 2 * a) mod of_nat (2 * n) = 1 + 2 * (a mod of_nat n)"
begin

definition bitrunc :: "nat ⇒ 'a ⇒ 'a"
where bitrunc_eq_mod: "bitrunc n a = a mod of_nat (2 ^ n)"

lemma bitrunc_bitrunc [simp]:
"bitrunc n (bitrunc n a) = bitrunc n a"

lemma bitrunc_0 [simp]:
"bitrunc 0 a = 0"

lemma bitrunc_Suc [simp]:
"bitrunc (Suc n) a = bitrunc n (a div 2) * 2 + a mod 2"
proof -
define b and c
where "b = a div 2" and "c = a mod 2"
then have a: "a = b * 2 + c"
and "c = 0 ∨ c = 1"
from ‹c = 0 ∨ c = 1›
have "bitrunc (Suc n) (b * 2 + c) = bitrunc n b * 2 + c"
proof
assume "c = 0"
moreover have "(2 * b) mod (2 * 2 ^ n) = 2 * (b mod 2 ^ n)"
ultimately show ?thesis
next
assume "c = 1"
with semiring_bits [of b "2 ^ n"] show ?thesis
qed
with a show ?thesis
qed

lemma bitrunc_of_0 [simp]:
"bitrunc n 0 = 0"

lemma bitrunc_plus:
"bitrunc n (bitrunc n a + bitrunc n b) = bitrunc n (a + b)"

lemma bitrunc_of_1_eq_0_iff [simp]:
"bitrunc n 1 = 0 ⟷ n = 0"
by (induct n) simp_all

end

instance nat :: semiring_bits
by standard (simp add: mod_Suc Suc_double_not_eq_double)

instance int :: semiring_bits

lemma bitrunc_uminus:
fixes k :: int
shows "bitrunc n (- (bitrunc n k)) = bitrunc n (- k)"

lemma bitrunc_minus:
fixes k l :: int
shows "bitrunc n (bitrunc n k - bitrunc n l) = bitrunc n (k - l)"

lemma bitrunc_nonnegative [simp]:
fixes k :: int
shows "bitrunc n k ≥ 0"

definition signed_bitrunc :: "nat ⇒ int ⇒ int"
where signed_bitrunc_eq_bitrunc:
"signed_bitrunc n k = bitrunc (Suc n) (k + 2 ^ n) - 2 ^ n"

lemma signed_bitrunc_eq_bitrunc':
assumes "n > 0"
shows "signed_bitrunc (n - Suc 0) k = bitrunc n (k + 2 ^ (n - 1)) - 2 ^ (n - 1)"
using assms by (simp add: signed_bitrunc_eq_bitrunc)

lemma signed_bitrunc_0 [simp]:
"signed_bitrunc 0 k = - (k mod 2)"
proof (cases "even k")
case True
then have "odd (k + 1)"
by simp
then have "(k + 1) mod 2 = 1"
with True show ?thesis
next
case False
then show ?thesis
qed

lemma signed_bitrunc_Suc [simp]:
"signed_bitrunc (Suc n) k = signed_bitrunc n (k div 2) * 2 + k mod 2"
using zero_not_eq_two by (simp add: signed_bitrunc_eq_bitrunc algebra_simps)

lemma signed_bitrunc_of_0 [simp]:
"signed_bitrunc n 0 = 0"

lemma signed_bitrunc_of_minus_1 [simp]:
"signed_bitrunc n (- 1) = - 1"
by (induct n) simp_all

lemma signed_bitrunc_eq_iff_bitrunc_eq:
assumes "n > 0"
shows "signed_bitrunc (n - Suc 0) k = signed_bitrunc (n - Suc 0) l ⟷ bitrunc n k = bitrunc n l" (is "?P ⟷ ?Q")
proof -
from assms obtain m where m: "n = Suc m"
by (cases n) auto
show ?thesis
proof
assume ?Q
have "bitrunc (Suc m) (k + 2 ^ m) =
bitrunc (Suc m) (bitrunc (Suc m) k + bitrunc (Suc m) (2 ^ m))"
by (simp only: bitrunc_plus)
also have "… =
bitrunc (Suc m) (bitrunc (Suc m) l + bitrunc (Suc m) (2 ^ m))"
by (simp only: ‹?Q› m [symmetric])
also have "… = bitrunc (Suc m) (l + 2 ^ m)"
by (simp only: bitrunc_plus)
finally show ?P
by (simp only: signed_bitrunc_eq_bitrunc m) simp
next
assume ?P
with assms have "(k + 2 ^ (n - Suc 0)) mod 2 ^ n = (l + 2 ^ (n - Suc 0)) mod 2 ^ n"
then have "(i + (k + 2 ^ (n - Suc 0))) mod 2 ^ n = (i + (l + 2 ^ (n - Suc 0))) mod 2 ^ n" for i
then have "k mod 2 ^ n = l mod 2 ^ n"
then show ?Q
qed
qed

subsection ‹Bit strings as quotient type›

subsubsection ‹Basic properties›

quotient_type (overloaded) 'a word = int / "λk l. bitrunc LENGTH('a) k = bitrunc LENGTH('a::len0) l"
by (auto intro!: equivpI reflpI sympI transpI)

instantiation word :: (len0) "{semiring_numeral, comm_semiring_0, comm_ring}"
begin

lift_definition zero_word :: "'a word"
is 0
.

lift_definition one_word :: "'a word"
is 1
.

lift_definition plus_word :: "'a word ⇒ 'a word ⇒ 'a word"
is plus
by (subst bitrunc_plus [symmetric]) (simp add: bitrunc_plus)

lift_definition uminus_word :: "'a word ⇒ 'a word"
is uminus
by (subst bitrunc_uminus [symmetric]) (simp add: bitrunc_uminus)

lift_definition minus_word :: "'a word ⇒ 'a word ⇒ 'a word"
is minus
by (subst bitrunc_minus [symmetric]) (simp add: bitrunc_minus)

lift_definition times_word :: "'a word ⇒ 'a word ⇒ 'a word"
is times
by (auto simp add: bitrunc_eq_mod intro: mod_mult_cong)

instance
by standard (transfer; simp add: algebra_simps)+

end

instance word :: (len) comm_ring_1
by standard (transfer; simp)+

subsubsection ‹Conversions›

lemma [transfer_rule]:
"rel_fun HOL.eq pcr_word int of_nat"
proof -
note transfer_rule_of_nat [transfer_rule]
show ?thesis by transfer_prover
qed

lemma [transfer_rule]:
"rel_fun HOL.eq pcr_word (λk. k) of_int"
proof -
note transfer_rule_of_int [transfer_rule]
have "rel_fun HOL.eq pcr_word (of_int :: int ⇒ int) (of_int :: int ⇒ 'a word)"
by transfer_prover
then show ?thesis by (simp add: id_def)
qed

context semiring_1
begin

lift_definition unsigned :: "'b::len0 word ⇒ 'a"
is "of_nat ∘ nat ∘ bitrunc LENGTH('b)"
by simp

lemma unsigned_0 [simp]:
"unsigned 0 = 0"
by transfer simp

end

context semiring_char_0
begin

lemma word_eq_iff_unsigned:
"a = b ⟷ unsigned a = unsigned b"
by safe (transfer; simp add: eq_nat_nat_iff)

end

context ring_1
begin

lift_definition signed :: "'b::len word ⇒ 'a"
is "of_int ∘ signed_bitrunc (LENGTH('b) - 1)"

lemma signed_0 [simp]:
"signed 0 = 0"
by transfer simp

end

lemma unsigned_of_nat [simp]:
"unsigned (of_nat n :: 'a word) = bitrunc LENGTH('a::len) n"
by transfer (simp add: nat_eq_iff bitrunc_eq_mod zmod_int)

lemma of_nat_unsigned [simp]:
"of_nat (unsigned a) = a"
by transfer simp

lemma of_int_unsigned [simp]:
"of_int (unsigned a) = a"
by transfer simp

context ring_char_0
begin

lemma word_eq_iff_signed:
"a = b ⟷ signed a = signed b"
by safe (transfer; auto simp add: signed_bitrunc_eq_iff_bitrunc_eq)

end

lemma signed_of_int [simp]:
"signed (of_int k :: 'a word) = signed_bitrunc (LENGTH('a::len) - 1) k"
by transfer simp

lemma of_int_signed [simp]:
"of_int (signed a) = a"
by transfer (simp add: signed_bitrunc_eq_bitrunc bitrunc_eq_mod mod_simps)

subsubsection ‹Properties›

subsubsection ‹Division›

instantiation word :: (len0) modulo
begin

lift_definition divide_word :: "'a word ⇒ 'a word ⇒ 'a word"
is "λa b. bitrunc LENGTH('a) a div bitrunc LENGTH('a) b"
by simp

lift_definition modulo_word :: "'a word ⇒ 'a word ⇒ 'a word"
is "λa b. bitrunc LENGTH('a) a mod bitrunc LENGTH('a) b"
by simp

instance ..

end

subsubsection ‹Orderings›

instantiation word :: (len0) linorder
begin

lift_definition less_eq_word :: "'a word ⇒ 'a word ⇒ bool"
is "λa b. bitrunc LENGTH('a) a ≤ bitrunc LENGTH('a) b"
by simp

lift_definition less_word :: "'a word ⇒ 'a word ⇒ bool"
is "λa b. bitrunc LENGTH('a) a < bitrunc LENGTH('a) b"
by simp

instance
by standard (transfer; auto)+

end

context linordered_semidom
begin

lemma word_less_eq_iff_unsigned:
"a ≤ b ⟷ unsigned a ≤ unsigned b"
by (transfer fixing: less_eq) (simp add: nat_le_eq_zle)

lemma word_less_iff_unsigned:
"a < b ⟷ unsigned a < unsigned b"
by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF bitrunc_nonnegative])

end

end
```