src/HOL/ex/Bit_Lists.thy
author wenzelm
Mon, 02 Dec 2019 16:15:27 +0100
changeset 71216 e64c249d3d98
parent 71095 038727567817
child 71420 572ab9e64e18
permissions -rw-r--r--
proper dynamic position of application context, e.g. relevant for 'global_interpretation';
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
     1
(*  Author:  Florian Haftmann, TUM
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
71095
038727567817 tuned order between theories
haftmann
parents: 71094
diff changeset
     8
    Word
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
primrec radix_value :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'a"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    17
  where "radix_value f b [] = 0"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    18
  | "radix_value f b (a # as) = f a + radix_value f b as * b"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    19
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    20
abbreviation (input) unsigned_of_bits :: "bool list \<Rightarrow> 'a"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    21
  where "unsigned_of_bits \<equiv> radix_value of_bool 2"
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
lemma unsigned_of_bits_replicate_False [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    24
  "unsigned_of_bits (replicate n False) = 0"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    25
  by (induction n) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    26
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    27
end
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    28
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71042
diff changeset
    29
context unique_euclidean_semiring_with_bit_shifts
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    30
begin
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
    31
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    32
lemma unsigned_of_bits_append [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    33
  "unsigned_of_bits (bs @ cs) = unsigned_of_bits bs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    34
    + push_bit (length bs) (unsigned_of_bits cs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    35
  by (induction bs) (simp_all add: push_bit_double,
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    36
    simp_all add: algebra_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    37
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    38
lemma unsigned_of_bits_take [simp]:
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    39
  "unsigned_of_bits (take n bs) = Parity.take_bit n (unsigned_of_bits bs)"
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    40
proof (induction bs arbitrary: n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    41
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    42
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    43
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    44
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    45
  case (Cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    46
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    47
    by (cases n) (simp_all add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    48
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    49
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    50
lemma unsigned_of_bits_drop [simp]:
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    51
  "unsigned_of_bits (drop n bs) = Parity.drop_bit n (unsigned_of_bits bs)"
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    52
proof (induction bs arbitrary: n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    53
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    54
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    55
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    56
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    57
  case (Cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    58
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    59
    by (cases n) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    60
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    61
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    62
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
    63
  where
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    64
    "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
    65
  | "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
    66
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    67
lemma n_bits_of_eq_iff:
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    68
  "n_bits_of n a = n_bits_of n b \<longleftrightarrow> Parity.take_bit n a = Parity.take_bit n b"
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    69
  apply (induction n arbitrary: a b)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    70
   apply (auto elim!: evenE oddE)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    71
   apply (metis dvd_triv_right even_plus_one_iff)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    72
  apply (metis dvd_triv_right even_plus_one_iff)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    73
  done
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 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
    76
  "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
    77
proof -
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    78
  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
    79
  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
    80
    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
    81
  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
    82
    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
    83
  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
    84
    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
    85
qed
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    86
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    87
lemma unsigned_of_bits_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
    88
  "unsigned_of_bits (n_bits_of n a) = Parity.take_bit 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
    89
  by (induction n arbitrary: a) (simp_all add: ac_simps)
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    90
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    91
end
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    92
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    93
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    94
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
    95
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
    96
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
    97
  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
    98
    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
    99
  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
   100
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   101
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
   102
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   103
instantiation nat :: bit_representation
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   104
begin
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   105
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   106
fun bits_of_nat :: "nat \<Rightarrow> bool list"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   107
  where "bits_of (n::nat) =
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   108
    (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
   109
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   110
lemma bits_of_nat_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   111
  "bits_of (0::nat) = []"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   112
  "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
   113
  by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   114
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   115
declare bits_of_nat.simps [simp del]
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   116
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   117
definition of_bits_nat :: "bool list \<Rightarrow> nat"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   118
  where [simp]: "of_bits_nat = unsigned_of_bits"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   119
  \<comment> \<open>remove simp\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   120
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   121
instance proof
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   122
  show "of_bits (bits_of n) = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   123
    by (induction n rule: nat_bit_induct) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   124
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   125
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   126
end
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   127
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   128
lemma bits_of_Suc_0 [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   129
  "bits_of (Suc 0) = [True]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   130
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   131
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   132
lemma bits_of_1_nat [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   133
  "bits_of (1 :: nat) = [True]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   134
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   135
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   136
lemma bits_of_nat_numeral_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   137
  "bits_of (numeral Num.One :: nat) = [True]" (is ?One)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   138
  "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
   139
  "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
   140
proof -
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   141
  show ?One
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
  define m :: nat where "m = numeral n"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   144
  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
   145
    by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   146
  from \<open>m > 0\<close> show ?Bit0 ?Bit1
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   147
    by (simp_all add: *)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   148
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   149
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   150
lemma unsigned_of_bits_of_nat [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   151
  "unsigned_of_bits (bits_of n) = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   152
  using of_bits_of [of n] by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   153
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   154
instantiation int :: bit_representation
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   155
begin
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   156
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   157
fun bits_of_int :: "int \<Rightarrow> bool list"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   158
  where "bits_of_int k = odd k #
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   159
    (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
   160
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   161
lemma bits_of_int_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   162
  "bits_of (0 :: int) = [False]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   163
  "bits_of (- 1 :: int) = [True]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   164
  "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
   165
  by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   166
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   167
lemma bits_of_not_Nil [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   168
  "bits_of k \<noteq> []" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   169
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   170
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   171
declare bits_of_int.simps [simp del]
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
definition of_bits_int :: "bool list \<Rightarrow> int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   174
  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
   175
    else unsigned_of_bits bs - 2 ^ length bs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   176
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   177
lemma of_bits_int_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   178
  "of_bits [] = (0 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   179
  "of_bits [False] = (0 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   180
  "of_bits [True] = (- 1 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   181
  "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
   182
  "of_bits (False # bs) = 2 * (of_bits bs :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   183
  "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
   184
  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
   185
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   186
instance proof
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   187
  show "of_bits (bits_of k) = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   188
    by (induction k rule: int_bit_induct) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   189
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   190
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   191
lemma bits_of_1_int [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   192
  "bits_of (1 :: int) = [True, False]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   193
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   194
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   195
lemma bits_of_int_numeral_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   196
  "bits_of (numeral Num.One :: int) = [True, False]" (is ?One)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   197
  "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
   198
  "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
   199
  "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
   200
  "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
   201
proof -
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   202
  show ?One
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   203
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   204
  define k :: int where "k = numeral n"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   205
  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
   206
    "numeral (Num.inc n) = k + 1"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   207
    by (simp_all add: add_One)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   208
  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
   209
    by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   210
  with \<open>k > 0\<close> show ?Bit0 ?Bit1 ?nBit0 ?nBit1
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   211
    by (simp_all add: *)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   212
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   213
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   214
lemma of_bits_append [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   215
  "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
   216
    if "bs \<noteq> []" "\<not> last bs"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   217
using that proof (induction bs rule: list_nonempty_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   218
  case (single b)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   219
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   220
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   221
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   222
  case (cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   223
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   224
    by (cases b) (simp_all add: push_bit_double)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   225
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   226
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   227
lemma of_bits_replicate_False [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   228
  "of_bits (replicate n False) = (0 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   229
  by (auto simp add: of_bits_int_def)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   230
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   231
lemma of_bits_drop [simp]:
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   232
  "of_bits (drop n bs) = Parity.drop_bit n (of_bits bs :: int)"
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   233
    if "n < length bs"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   234
using that proof (induction bs arbitrary: n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   235
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   236
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   237
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   238
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   239
  case (Cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   240
  show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   241
  proof (cases n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   242
    case 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   243
    then show ?thesis
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 (Suc n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   247
    with Cons.prems have "bs \<noteq> []"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   248
      by auto
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   249
    with Suc Cons.IH [of n] Cons.prems show ?thesis
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   250
      by (cases b) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   251
  qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   252
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   253
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   254
end
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   255
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   256
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
   257
  "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
   258
  by (simp add: of_bits_int_def)
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   259
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   260
instantiation word :: (len) bit_representation
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   261
begin
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   262
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   263
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
   264
  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
   265
  by (simp add: n_bits_of_eq_iff)
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   266
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   267
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
   268
  is unsigned_of_bits .
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   269
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   270
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
   271
  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
   272
  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
   273
    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
   274
qed
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   275
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   276
end
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   277
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   278
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   279
subsection \<open>Bit representations with bit operations\<close>
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   280
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   281
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
   282
  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
   283
      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
   284
    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
   285
      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
   286
    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
   287
      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
   288
    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
   289
    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
   290
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   291
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
   292
  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
   293
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   294
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   295
context zip_nat
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   296
begin
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   297
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   298
lemma of_bits:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   299
  "of_bits bs \<^bold>\<times> of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: nat)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   300
    if "length bs = length cs" for bs cs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   301
using that proof (induction bs cs rule: list_induct2)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   302
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   303
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   304
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   305
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   306
  case (Cons b bs c cs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   307
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   308
    by (cases "of_bits bs = (0::nat) \<or> of_bits cs = (0::nat)")
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   309
      (auto simp add: ac_simps end_of_bits rec [of "Suc 0"])
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   310
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   311
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   312
end
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   313
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   314
instance nat :: 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
   315
  apply standard
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   316
      apply (simp_all only: and_nat.of_bits or_nat.of_bits xor_nat.of_bits)
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71042
diff changeset
   317
   apply simp_all
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
  done
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
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
context zip_int
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   321
begin
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   322
 
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   323
lemma of_bits:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   324
  "of_bits bs \<^bold>\<times> of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   325
    if "length bs = length cs" and "\<not> False \<^bold>* False" for bs cs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   326
using \<open>length bs = length cs\<close> proof (induction bs cs rule: list_induct2)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   327
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   328
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   329
    using \<open>\<not> False \<^bold>* False\<close> by (auto simp add: False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   330
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   331
  case (Cons b bs c cs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   332
  show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   333
  proof (cases "bs = []")
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   334
    case True
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   335
    with Cons show ?thesis
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   336
      using \<open>\<not> False \<^bold>* False\<close> by (cases b; cases c)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   337
        (auto simp add: ac_simps split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   338
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   339
    case False
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   340
    with Cons.hyps have "cs \<noteq> []"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   341
      by auto
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   342
    with \<open>bs \<noteq> []\<close> have "map2 (\<^bold>*) bs cs \<noteq> []"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   343
      by (simp add: zip_eq_Nil_iff)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   344
    from \<open>bs \<noteq> []\<close> \<open>cs \<noteq> []\<close> \<open>map2 (\<^bold>*) bs cs \<noteq> []\<close> Cons show ?thesis
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   345
      by (cases b; cases c)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   346
        (auto simp add: \<open>\<not> False \<^bold>* False\<close> ac_simps
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   347
          rec [of "of_bits bs * 2"] rec [of "of_bits cs * 2"]
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   348
          rec [of "1 + of_bits bs * 2"])
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   349
  qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   350
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   351
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   352
end
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   353
71042
400e9512f1d3 proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents: 70926
diff changeset
   354
instance int :: ring_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
   355
proof
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   356
  show "(not :: int \<Rightarrow> _) = of_bits \<circ> map Not \<circ> bits_of"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   357
  proof (rule sym, rule ext)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   358
    fix k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   359
    show "(of_bits \<circ> map Not \<circ> bits_of) k = NOT k"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   360
      by (induction k rule: int_bit_induct) (simp_all add: not_int_def)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   361
  qed
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71042
diff changeset
   362
  show "push_bit n k = of_bits (replicate n False @ bits_of k)"
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   363
    for k :: int and n :: nat
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 71042
diff changeset
   364
    by (cases "n = 0") simp_all
a197532693a5 bit shifts as class operations
haftmann
parents: 71042
diff changeset
   365
qed (simp_all add: and_int.of_bits or_int.of_bits xor_int.of_bits)
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   366
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   367
end