src/HOL/ex/Bit_Lists.thy
author haftmann
Thu, 17 Sep 2020 09:57:31 +0000
changeset 72262 a282abb07642
parent 72198 7ffa26f05c72
child 72515 c7038c397ae3
permissions -rw-r--r--
integrated generic conversions into word corpse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71443
diff changeset
     1
 (*  Author:  Florian Haftmann, TUM
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
     2
*)
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
     3
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
     4
section \<open>Proof(s) of concept for algebraically founded lists of bits\<close>
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
     5
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
     6
theory Bit_Lists
70926
b4564de51fa7 bit operations form an boolean algebra
haftmann
parents: 70912
diff changeset
     7
  imports
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72198
diff changeset
     8
    "HOL-Word.Word" "HOL-Library.More_List"
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
     9
begin
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
    10
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    11
subsection \<open>Fragments of algebraic bit representations\<close>
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    12
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
    13
context comm_semiring_1
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
    14
begin
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    15
 
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    16
abbreviation (input) unsigned_of_bits :: "bool list \<Rightarrow> 'a"
72024
9b4135e8bade a generic horner sum operation
haftmann
parents: 71823
diff changeset
    17
  where "unsigned_of_bits \<equiv> horner_sum of_bool 2"
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    18
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    19
lemma unsigned_of_bits_replicate_False [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    20
  "unsigned_of_bits (replicate n False) = 0"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    21
  by (induction n) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    22
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    23
end
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    24
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71042
diff changeset
    25
context unique_euclidean_semiring_with_bit_shifts
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    26
begin
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
    27
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    28
lemma unsigned_of_bits_append [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    29
  "unsigned_of_bits (bs @ cs) = unsigned_of_bits bs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    30
    + push_bit (length bs) (unsigned_of_bits cs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    31
  by (induction bs) (simp_all add: push_bit_double,
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    32
    simp_all add: algebra_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    33
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    34
lemma unsigned_of_bits_take [simp]:
71420
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    35
  "unsigned_of_bits (take n bs) = take_bit n (unsigned_of_bits bs)"
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    36
proof (induction bs arbitrary: n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    37
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    38
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    39
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    40
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    41
  case (Cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    42
  then show ?case
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71443
diff changeset
    43
    by (cases n) (simp_all add: ac_simps take_bit_Suc)
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    44
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    45
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    46
lemma unsigned_of_bits_drop [simp]:
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71443
diff changeset
    47
  "unsigned_of_bits (drop n bs) = drop_bit n (unsigned_of_bits bs)"
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    48
proof (induction bs arbitrary: n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    49
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    50
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    51
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    52
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    53
  case (Cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    54
  then show ?case
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71443
diff changeset
    55
    by (cases n) (simp_all add: drop_bit_Suc)
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    56
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    57
71420
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    58
lemma bit_unsigned_of_bits_iff:
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    59
  \<open>bit (unsigned_of_bits bs) n \<longleftrightarrow> nth_default False bs n\<close>
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    60
proof (induction bs arbitrary: n)
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    61
  case Nil
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    62
  then show ?case
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    63
    by simp
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    64
next
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    65
  case (Cons b bs)
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    66
  then show ?case
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71443
diff changeset
    67
    by (cases n) (simp_all add: bit_Suc)
71420
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    68
qed
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
    69
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    70
primrec n_bits_of :: "nat \<Rightarrow> 'a \<Rightarrow> bool list"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    71
  where
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    72
    "n_bits_of 0 a = []"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    73
  | "n_bits_of (Suc n) a = odd a # n_bits_of n (a div 2)"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    74
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    75
lemma n_bits_of_eq_iff:
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71443
diff changeset
    76
  "n_bits_of n a = n_bits_of n b \<longleftrightarrow> take_bit n a = take_bit n b"
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    77
  apply (induction n arbitrary: a b)
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71804
diff changeset
    78
   apply (auto elim!: evenE oddE simp add: take_bit_Suc mod_2_eq_odd)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71804
diff changeset
    79
    apply (metis dvd_triv_right even_plus_one_iff odd_iff_mod_2_eq_one)
214b48a1937b explicit mask operation for bits
haftmann
parents: 71804
diff changeset
    80
   apply (metis dvd_triv_right even_plus_one_iff odd_iff_mod_2_eq_one)
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    81
  done
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    82
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    83
lemma take_n_bits_of [simp]:
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    84
  "take m (n_bits_of n a) = n_bits_of (min m n) a"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    85
proof -
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    86
  define q and v and w where "q = min m n" and "v = m - q" and "w = n - q"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    87
  then have "v = 0 \<or> w = 0"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    88
    by auto
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    89
  then have "take (q + v) (n_bits_of (q + w) a) = n_bits_of q a"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    90
    by (induction q arbitrary: a) auto
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    91
  with q_def v_def w_def show ?thesis
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    92
    by simp
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    93
qed
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    94
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    95
lemma unsigned_of_bits_n_bits_of [simp]:
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71443
diff changeset
    96
  "unsigned_of_bits (n_bits_of n a) = take_bit n a"
71823
214b48a1937b explicit mask operation for bits
haftmann
parents: 71804
diff changeset
    97
  by (induction n arbitrary: a) (simp_all add: ac_simps take_bit_Suc mod_2_eq_odd)
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    98
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    99
end
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   100
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   101
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   102
subsection \<open>Syntactic bit representation\<close>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   103
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   104
class bit_representation =
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   105
  fixes bits_of :: "'a \<Rightarrow> bool list"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   106
    and of_bits :: "bool list \<Rightarrow> 'a"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   107
  assumes of_bits_of [simp]: "of_bits (bits_of a) = a"
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   108
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   109
text \<open>Unclear whether a \<^typ>\<open>bool\<close> instantiation is needed or not\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   110
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   111
instantiation nat :: bit_representation
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   112
begin
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   113
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   114
fun bits_of_nat :: "nat \<Rightarrow> bool list"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   115
  where "bits_of (n::nat) =
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   116
    (if n = 0 then [] else odd n # bits_of (n div 2))"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   117
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   118
lemma bits_of_nat_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   119
  "bits_of (0::nat) = []"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   120
  "n > 0 \<Longrightarrow> bits_of n = odd n # bits_of (n div 2)" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   121
  by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   122
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   123
declare bits_of_nat.simps [simp del]
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   124
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   125
definition of_bits_nat :: "bool list \<Rightarrow> nat"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   126
  where [simp]: "of_bits_nat = unsigned_of_bits"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   127
  \<comment> \<open>remove simp\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   128
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   129
instance proof
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   130
  show "of_bits (bits_of n) = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   131
    by (induction n rule: nat_bit_induct) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   132
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   133
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   134
end
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   135
71420
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   136
lemma bit_of_bits_nat_iff:
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   137
  \<open>bit (of_bits bs :: nat) n \<longleftrightarrow> nth_default False bs n\<close>
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   138
  by (simp add: bit_unsigned_of_bits_iff)
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   139
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   140
lemma bits_of_Suc_0 [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   141
  "bits_of (Suc 0) = [True]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   142
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   143
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   144
lemma bits_of_1_nat [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   145
  "bits_of (1 :: nat) = [True]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   146
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   147
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   148
lemma bits_of_nat_numeral_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   149
  "bits_of (numeral Num.One :: nat) = [True]" (is ?One)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   150
  "bits_of (numeral (Num.Bit0 n) :: nat) = False # bits_of (numeral n :: nat)" (is ?Bit0)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   151
  "bits_of (numeral (Num.Bit1 n) :: nat) = True # bits_of (numeral n :: nat)" (is ?Bit1)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   152
proof -
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   153
  show ?One
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   154
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   155
  define m :: nat where "m = numeral n"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   156
  then have "m > 0" and *: "numeral n = m" "numeral (Num.Bit0 n) = 2 * m" "numeral (Num.Bit1 n) = Suc (2 * m)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   157
    by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   158
  from \<open>m > 0\<close> show ?Bit0 ?Bit1
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   159
    by (simp_all add: *)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   160
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   161
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   162
lemma unsigned_of_bits_of_nat [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   163
  "unsigned_of_bits (bits_of n) = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   164
  using of_bits_of [of n] by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   165
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   166
instantiation int :: bit_representation
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   167
begin
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   168
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   169
fun bits_of_int :: "int \<Rightarrow> bool list"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   170
  where "bits_of_int k = odd k #
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   171
    (if k = 0 \<or> k = - 1 then [] else bits_of_int (k div 2))"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   172
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   173
lemma bits_of_int_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   174
  "bits_of (0 :: int) = [False]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   175
  "bits_of (- 1 :: int) = [True]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   176
  "k \<noteq> 0 \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> bits_of k = odd k # bits_of (k div 2)" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   177
  by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   178
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   179
lemma bits_of_not_Nil [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   180
  "bits_of k \<noteq> []" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   181
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   182
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   183
declare bits_of_int.simps [simp del]
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   184
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   185
definition of_bits_int :: "bool list \<Rightarrow> int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   186
  where "of_bits_int bs = (if bs = [] \<or> \<not> last bs then unsigned_of_bits bs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   187
    else unsigned_of_bits bs - 2 ^ length bs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   188
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   189
lemma of_bits_int_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   190
  "of_bits [] = (0 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   191
  "of_bits [False] = (0 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   192
  "of_bits [True] = (- 1 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   193
  "of_bits (bs @ [b]) = (unsigned_of_bits bs :: int) - (2 ^ length bs) * of_bool b"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   194
  "of_bits (False # bs) = 2 * (of_bits bs :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   195
  "bs \<noteq> [] \<Longrightarrow> of_bits (True # bs) = 1 + 2 * (of_bits bs :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   196
  by (simp_all add: of_bits_int_def push_bit_of_1)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   197
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   198
instance proof
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   199
  show "of_bits (bits_of k) = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   200
    by (induction k rule: int_bit_induct) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   201
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   202
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   203
lemma bits_of_1_int [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   204
  "bits_of (1 :: int) = [True, False]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   205
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   206
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   207
lemma bits_of_int_numeral_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   208
  "bits_of (numeral Num.One :: int) = [True, False]" (is ?One)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   209
  "bits_of (numeral (Num.Bit0 n) :: int) = False # bits_of (numeral n :: int)" (is ?Bit0)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   210
  "bits_of (numeral (Num.Bit1 n) :: int) = True # bits_of (numeral n :: int)" (is ?Bit1)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   211
  "bits_of (- numeral (Num.Bit0 n) :: int) = False # bits_of (- numeral n :: int)" (is ?nBit0)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   212
  "bits_of (- numeral (Num.Bit1 n) :: int) = True # bits_of (- numeral (Num.inc n) :: int)" (is ?nBit1)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   213
proof -
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   214
  show ?One
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   215
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   216
  define k :: int where "k = numeral n"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   217
  then have "k > 0" and *: "numeral n = k" "numeral (Num.Bit0 n) = 2 * k" "numeral (Num.Bit1 n) = 2 * k + 1"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   218
    "numeral (Num.inc n) = k + 1"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   219
    by (simp_all add: add_One)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   220
  have "- (2 * k) div 2 = - k" "(- (2 * k) - 1) div 2 = - k - 1"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   221
    by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   222
  with \<open>k > 0\<close> show ?Bit0 ?Bit1 ?nBit0 ?nBit1
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   223
    by (simp_all add: *)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   224
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   225
71420
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   226
lemma bit_of_bits_int_iff:
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   227
  \<open>bit (of_bits bs :: int) n \<longleftrightarrow> nth_default (bs \<noteq> [] \<and> last bs) bs n\<close>
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   228
proof (induction bs arbitrary: n)
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   229
  case Nil
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   230
  then show ?case
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   231
    by simp
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   232
next
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   233
  case (Cons b bs)
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   234
  then show ?case
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71443
diff changeset
   235
    by (cases n; cases b; cases bs) (simp_all add: bit_Suc)
71420
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   236
qed
572ab9e64e18 simplified logical constructions
haftmann
parents: 71095
diff changeset
   237
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   238
lemma of_bits_append [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   239
  "of_bits (bs @ cs) = of_bits bs + push_bit (length bs) (of_bits cs :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   240
    if "bs \<noteq> []" "\<not> last bs"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   241
using that proof (induction bs rule: list_nonempty_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   242
  case (single b)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   243
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   244
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   245
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   246
  case (cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   247
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   248
    by (cases b) (simp_all add: push_bit_double)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   249
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   250
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   251
lemma of_bits_replicate_False [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   252
  "of_bits (replicate n False) = (0 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   253
  by (auto simp add: of_bits_int_def)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   254
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   255
lemma of_bits_drop [simp]:
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71443
diff changeset
   256
  "of_bits (drop n bs) = drop_bit n (of_bits bs :: int)"
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   257
    if "n < length bs"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   258
using that proof (induction bs arbitrary: n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   259
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   260
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   261
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   262
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   263
  case (Cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   264
  show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   265
  proof (cases n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   266
    case 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   267
    then show ?thesis
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   268
      by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   269
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   270
    case (Suc n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   271
    with Cons.prems have "bs \<noteq> []"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   272
      by auto
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   273
    with Suc Cons.IH [of n] Cons.prems show ?thesis
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71443
diff changeset
   274
      by (cases b) (simp_all add: drop_bit_Suc)
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   275
  qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   276
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   277
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   278
end
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   279
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   280
lemma unsigned_of_bits_eq_of_bits:
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   281
  "unsigned_of_bits bs = (of_bits (bs @ [False]) :: int)"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   282
  by (simp add: of_bits_int_def)
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   283
71443
ff6394cfc05c canonical approach towards lifting
haftmann
parents: 71423
diff changeset
   284
unbundle word.lifting
ff6394cfc05c canonical approach towards lifting
haftmann
parents: 71423
diff changeset
   285
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   286
instantiation word :: (len) bit_representation
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   287
begin
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   288
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   289
lift_definition bits_of_word :: "'a word \<Rightarrow> bool list"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   290
  is "n_bits_of LENGTH('a)"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   291
  by (simp add: n_bits_of_eq_iff)
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   292
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   293
lift_definition of_bits_word :: "bool list \<Rightarrow> 'a word"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   294
  is unsigned_of_bits .
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   295
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   296
instance proof
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   297
  fix a :: "'a word"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   298
  show "of_bits (bits_of a) = a"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   299
    by transfer simp
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   300
qed
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   301
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   302
end
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   303
71443
ff6394cfc05c canonical approach towards lifting
haftmann
parents: 71423
diff changeset
   304
lifting_update word.lifting
ff6394cfc05c canonical approach towards lifting
haftmann
parents: 71423
diff changeset
   305
lifting_forget word.lifting
ff6394cfc05c canonical approach towards lifting
haftmann
parents: 71423
diff changeset
   306
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   307
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   308
subsection \<open>Bit representations with bit operations\<close>
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   309
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   310
class semiring_bit_representation = semiring_bit_operations + bit_representation +
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   311
  assumes and_eq: "length bs = length cs \<Longrightarrow>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   312
      of_bits bs AND of_bits cs = of_bits (map2 (\<and>) bs cs)"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   313
    and or_eq: "length bs = length cs \<Longrightarrow>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   314
      of_bits bs OR of_bits cs = of_bits (map2 (\<or>) bs cs)"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   315
    and xor_eq: "length bs = length cs \<Longrightarrow>
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   316
      of_bits bs XOR of_bits cs = of_bits (map2 (\<noteq>) bs cs)"
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71042
diff changeset
   317
    and push_bit_eq: "push_bit n a = of_bits (replicate n False @ bits_of a)"
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   318
    and drop_bit_eq: "n < length (bits_of a) \<Longrightarrow> drop_bit n a = of_bits (drop n (bits_of a))"
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   319
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   320
class ring_bit_representation = ring_bit_operations + semiring_bit_representation +
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   321
  assumes not_eq: "not = of_bits \<circ> map Not \<circ> bits_of"
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   322
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   323
instance nat :: semiring_bit_representation
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   324
  by standard (simp_all add: bit_eq_iff bit_unsigned_of_bits_iff nth_default_map2 [of _ _ _ False False]
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   325
    bit_and_iff bit_or_iff bit_xor_iff)
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   326
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   327
instance int :: ring_bit_representation
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   328
proof
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   329
  {
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   330
    fix bs cs :: \<open>bool list\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   331
    assume \<open>length bs = length cs\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   332
    then have \<open>cs = [] \<longleftrightarrow> bs = []\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   333
      by auto
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   334
    with \<open>length bs = length cs\<close> have \<open>zip bs cs \<noteq> [] \<and> last (map2 (\<and>) bs cs) \<longleftrightarrow> (bs \<noteq> [] \<and> last bs) \<and> (cs \<noteq> [] \<and> last cs)\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   335
      and \<open>zip bs cs \<noteq> [] \<and> last (map2 (\<or>) bs cs) \<longleftrightarrow> (bs \<noteq> [] \<and> last bs) \<or> (cs \<noteq> [] \<and> last cs)\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   336
      and \<open>zip bs cs \<noteq> [] \<and> last (map2 (\<noteq>) bs cs) \<longleftrightarrow> ((bs \<noteq> [] \<and> last bs) \<noteq> (cs \<noteq> [] \<and> last cs))\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   337
      by (auto simp add: last_map last_zip zip_eq_Nil_iff prod_eq_iff)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   338
    then show \<open>of_bits bs AND of_bits cs = (of_bits (map2 (\<and>) bs cs) :: int)\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   339
      and \<open>of_bits bs OR of_bits cs = (of_bits (map2 (\<or>) bs cs) :: int)\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   340
      and \<open>of_bits bs XOR of_bits cs = (of_bits (map2 (\<noteq>) bs cs) :: int)\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   341
      by (simp_all add: fun_eq_iff bit_eq_iff bit_and_iff bit_or_iff bit_xor_iff bit_not_iff bit_of_bits_int_iff \<open>length bs = length cs\<close> nth_default_map2 [of bs cs _ \<open>bs \<noteq> [] \<and> last bs\<close> \<open>cs \<noteq> [] \<and> last cs\<close>])
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   342
  }
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   343
  show \<open>push_bit n k = of_bits (replicate n False @ bits_of k)\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   344
    for k :: int and n :: nat
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   345
    by (cases "n = 0") simp_all
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   346
  show \<open>drop_bit n k = of_bits (drop n (bits_of k))\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   347
    if \<open>n < length (bits_of k)\<close> for k :: int and n :: nat
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   348
    using that by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   349
  show \<open>(not :: int \<Rightarrow> _) = of_bits \<circ> map Not \<circ> bits_of\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   350
  proof (rule sym, rule ext)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   351
    fix k :: int
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   352
    show \<open>(of_bits \<circ> map Not \<circ> bits_of) k = NOT k\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   353
      by (induction k rule: int_bit_induct) (simp_all add: not_int_def)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71535
diff changeset
   354
  qed
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   355
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   356
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   357
end