src/HOL/ex/Bit_Lists.thy
author haftmann
Sat, 19 Oct 2019 20:41:09 +0200
changeset 70912 8c2bef3df488
parent 70353 7aa64296b9b0
child 70926 b4564de51fa7
permissions -rw-r--r--
refined proof of concept for bit operations
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
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
     7
  imports Main
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
     8
begin
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
     9
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    10
subsection \<open>Bit representations\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    11
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    12
subsubsection \<open>Mere syntactic bit representation\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    13
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    14
class bit_representation =
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    15
  fixes bits_of :: "'a \<Rightarrow> bool list"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    16
    and of_bits :: "bool list \<Rightarrow> 'a"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    17
  assumes of_bits_of [simp]: "of_bits (bits_of a) = a"
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
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    20
subsubsection \<open>Algebraic bit representation\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    21
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
    22
context comm_semiring_1
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
    23
begin
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    24
 
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    25
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
    26
  where "radix_value f b [] = 0"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    27
  | "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
    28
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    29
abbreviation (input) unsigned_of_bits :: "bool list \<Rightarrow> 'a"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    30
  where "unsigned_of_bits \<equiv> radix_value of_bool 2"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    31
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    32
lemma unsigned_of_bits_replicate_False [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    33
  "unsigned_of_bits (replicate n False) = 0"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    34
  by (induction n) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    35
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    36
end
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
context unique_euclidean_semiring_with_nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    39
begin
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
    40
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    41
lemma unsigned_of_bits_append [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    42
  "unsigned_of_bits (bs @ cs) = unsigned_of_bits bs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    43
    + push_bit (length bs) (unsigned_of_bits cs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    44
  by (induction bs) (simp_all add: push_bit_double,
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    45
    simp_all add: algebra_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    46
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    47
lemma unsigned_of_bits_take [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    48
  "unsigned_of_bits (take n bs) = take_bit n (unsigned_of_bits bs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    49
proof (induction bs arbitrary: n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    50
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    51
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    52
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    53
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    54
  case (Cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    55
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    56
    by (cases n) (simp_all add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    57
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    58
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    59
lemma unsigned_of_bits_drop [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    60
  "unsigned_of_bits (drop n bs) = drop_bit n (unsigned_of_bits bs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    61
proof (induction bs arbitrary: n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    62
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    63
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    64
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    65
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    66
  case (Cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    67
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    68
    by (cases n) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    69
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    70
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    71
end
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    72
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    73
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    74
subsubsection \<open>Instances\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    75
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    76
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
    77
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    78
instantiation nat :: bit_representation
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    79
begin
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    80
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    81
fun bits_of_nat :: "nat \<Rightarrow> bool list"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    82
  where "bits_of (n::nat) =
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    83
    (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
    84
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    85
lemma bits_of_nat_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    86
  "bits_of (0::nat) = []"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    87
  "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
    88
  by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    89
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    90
declare bits_of_nat.simps [simp del]
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    91
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    92
definition of_bits_nat :: "bool list \<Rightarrow> nat"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    93
  where [simp]: "of_bits_nat = unsigned_of_bits"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    94
  \<comment> \<open>remove simp\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    95
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    96
instance proof
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    97
  show "of_bits (bits_of n) = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    98
    by (induction n rule: nat_bit_induct) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
    99
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   100
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   101
end
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   102
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   103
lemma bits_of_Suc_0 [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   104
  "bits_of (Suc 0) = [True]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   105
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   106
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   107
lemma bits_of_1_nat [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   108
  "bits_of (1 :: nat) = [True]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   109
  by simp
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
lemma bits_of_nat_numeral_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   112
  "bits_of (numeral Num.One :: nat) = [True]" (is ?One)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   113
  "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
   114
  "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
   115
proof -
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   116
  show ?One
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   117
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   118
  define m :: nat where "m = numeral n"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   119
  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
   120
    by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   121
  from \<open>m > 0\<close> show ?Bit0 ?Bit1
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   122
    by (simp_all add: *)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   123
qed
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
lemma unsigned_of_bits_of_nat [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   126
  "unsigned_of_bits (bits_of n) = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   127
  using of_bits_of [of n] by simp
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
instantiation int :: bit_representation
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   130
begin
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   131
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   132
fun bits_of_int :: "int \<Rightarrow> bool list"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   133
  where "bits_of_int k = odd k #
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   134
    (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
   135
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   136
lemma bits_of_int_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   137
  "bits_of (0 :: int) = [False]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   138
  "bits_of (- 1 :: int) = [True]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   139
  "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
   140
  by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   141
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   142
lemma bits_of_not_Nil [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   143
  "bits_of k \<noteq> []" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   144
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   145
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   146
declare bits_of_int.simps [simp del]
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
definition of_bits_int :: "bool list \<Rightarrow> int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   149
  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
   150
    else unsigned_of_bits bs - 2 ^ length bs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   151
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   152
lemma of_bits_int_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   153
  "of_bits [] = (0 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   154
  "of_bits [False] = (0 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   155
  "of_bits [True] = (- 1 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   156
  "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
   157
  "of_bits (False # bs) = 2 * (of_bits bs :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   158
  "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
   159
  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
   160
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   161
instance proof
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   162
  show "of_bits (bits_of k) = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   163
    by (induction k rule: int_bit_induct) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   164
qed
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
lemma bits_of_1_int [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   167
  "bits_of (1 :: int) = [True, False]"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   168
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   169
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   170
lemma bits_of_int_numeral_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   171
  "bits_of (numeral Num.One :: int) = [True, False]" (is ?One)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   172
  "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
   173
  "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
   174
  "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
   175
  "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
   176
proof -
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   177
  show ?One
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   178
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   179
  define k :: int where "k = numeral n"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   180
  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
   181
    "numeral (Num.inc n) = k + 1"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   182
    by (simp_all add: add_One)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   183
  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
   184
    by simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   185
  with \<open>k > 0\<close> show ?Bit0 ?Bit1 ?nBit0 ?nBit1
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   186
    by (simp_all add: *)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   187
qed
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_append [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   190
  "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
   191
    if "bs \<noteq> []" "\<not> last bs"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   192
using that proof (induction bs rule: list_nonempty_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   193
  case (single b)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   194
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   195
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   196
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   197
  case (cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   198
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   199
    by (cases b) (simp_all add: push_bit_double)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   200
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   201
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   202
lemma of_bits_replicate_False [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   203
  "of_bits (replicate n False) = (0 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   204
  by (auto simp add: of_bits_int_def)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   205
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   206
lemma of_bits_drop [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   207
  "of_bits (drop n bs) = drop_bit n (of_bits bs :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   208
    if "n < length bs"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   209
using that proof (induction bs arbitrary: n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   210
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   211
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   212
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   213
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   214
  case (Cons b bs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   215
  show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   216
  proof (cases n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   217
    case 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   218
    then show ?thesis
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   219
      by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   220
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   221
    case (Suc n)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   222
    with Cons.prems have "bs \<noteq> []"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   223
      by auto
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   224
    with Suc Cons.IH [of n] Cons.prems show ?thesis
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   225
      by (cases b) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   226
  qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   227
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   228
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   229
end
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   230
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   231
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   232
subsection \<open>Syntactic bit operations\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   233
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   234
class bit_operations = bit_representation +
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   235
  fixes not :: "'a \<Rightarrow> 'a"  ("NOT _" [70] 71)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   236
    and "and" :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixr "AND" 64)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   237
    and or :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixr "OR"  59)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   238
    and xor :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixr "XOR" 59)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   239
    and shift_left :: "'a \<Rightarrow> nat \<Rightarrow> 'a"  (infixl "<<" 55)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   240
    and shift_right :: "'a \<Rightarrow> nat \<Rightarrow> 'a"  (infixl ">>" 55)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   241
  assumes not_eq: "not = of_bits \<circ> map Not \<circ> bits_of"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   242
    and and_eq: "length bs = length cs \<Longrightarrow>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   243
      of_bits bs AND of_bits cs = of_bits (map2 (\<and>) bs cs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   244
    and semilattice_and: "semilattice (AND)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   245
    and or_eq: "length bs = length cs \<Longrightarrow>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   246
      of_bits bs OR of_bits cs = of_bits (map2 (\<or>) bs cs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   247
    and semilattice_or: "semilattice (OR)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   248
    and xor_eq: "length bs = length cs \<Longrightarrow>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   249
      of_bits bs XOR of_bits cs = of_bits (map2 (\<noteq>) bs cs)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   250
    and abel_semigroup_xor: "abel_semigroup (XOR)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   251
    and shift_right_eq: "a << n = of_bits (replicate n False @ bits_of a)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   252
    and shift_left_eq: "n < length (bits_of a) \<Longrightarrow> a >> n = of_bits (drop n (bits_of a))"
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   253
begin
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   254
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   255
text \<open>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   256
  We want the bitwise operations to bind slightly weaker
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   257
  than \<open>+\<close> and \<open>-\<close>, but \<open>~~\<close> to
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   258
  bind slightly stronger than \<open>*\<close>.
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   259
\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   260
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   261
sublocale "and": semilattice "(AND)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   262
  by (fact semilattice_and)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   263
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   264
sublocale or: semilattice "(OR)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   265
  by (fact semilattice_or)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   266
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   267
sublocale xor: abel_semigroup "(XOR)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   268
  by (fact abel_semigroup_xor)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   269
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   270
end
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   271
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   272
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   273
subsubsection \<open>Instance \<^typ>\<open>nat\<close>\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   274
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   275
locale zip_nat = single: abel_semigroup f
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   276
    for f :: "bool \<Rightarrow> bool \<Rightarrow> bool"  (infixl "\<^bold>*" 70) +
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   277
  assumes end_of_bits: "\<not> False \<^bold>* False"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   278
begin
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   279
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   280
lemma False_P_imp:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   281
  "False \<^bold>* True \<and> P" if "False \<^bold>* P"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   282
  using that end_of_bits by (cases P) simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   283
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   284
function F :: "nat \<Rightarrow> nat \<Rightarrow> nat"  (infixl "\<^bold>\<times>" 70)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   285
  where "m \<^bold>\<times> n = (if m = 0 \<and> n = 0 then 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   286
    else of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\<times> (n div 2) * 2)"
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   287
  by auto
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   288
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   289
termination
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   290
  by (relation "measure (case_prod (+))") auto
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   291
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   292
lemma zero_left_eq:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   293
  "0 \<^bold>\<times> n = of_bool (False \<^bold>* True) * n"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   294
  by (induction n rule: nat_bit_induct) (simp_all add: end_of_bits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   295
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   296
lemma zero_right_eq:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   297
  "m \<^bold>\<times> 0 = of_bool (True \<^bold>* False) * m"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   298
  by (induction m rule: nat_bit_induct) (simp_all add: end_of_bits)
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   299
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   300
lemma simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   301
  "0 \<^bold>\<times> 0 = 0"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   302
  "0 \<^bold>\<times> n = of_bool (False \<^bold>* True) * n"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   303
  "m \<^bold>\<times> 0 = of_bool (True \<^bold>* False) * m"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   304
  "m > 0 \<Longrightarrow> n > 0 \<Longrightarrow> m \<^bold>\<times> n = of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\<times> (n div 2) * 2"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   305
  by (simp_all only: zero_left_eq zero_right_eq) simp
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   306
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   307
lemma rec:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   308
  "m \<^bold>\<times> n = of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\<times> (n div 2) * 2"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   309
  by (cases "m = 0 \<and> n = 0") (auto simp add: end_of_bits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   310
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   311
declare F.simps [simp del]
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   312
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   313
sublocale abel_semigroup F
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   314
proof
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   315
  show "m \<^bold>\<times> n \<^bold>\<times> q = m \<^bold>\<times> (n \<^bold>\<times> q)" for m n q :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   316
  proof (induction m arbitrary: n q rule: nat_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   317
    case zero
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   318
    show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   319
      by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   320
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   321
    case (even m)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   322
    with rec [of "2 * m"] rec [of _ q] show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   323
      by (cases "even n") (auto dest: False_P_imp)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   324
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   325
    case (odd m)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   326
    with rec [of "Suc (2 * m)"] rec [of _ q] show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   327
      by (cases "even n"; cases "even q")
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   328
        (auto dest: False_P_imp simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   329
  qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   330
  show "m \<^bold>\<times> n = n \<^bold>\<times> m" for m n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   331
  proof (induction m arbitrary: n rule: nat_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   332
    case zero
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   333
    show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   334
      by (simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   335
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   336
    case (even m)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   337
    with rec [of "2 * m" n] rec [of n "2 * m"] show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   338
      by (simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   339
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   340
    case (odd m)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   341
    with rec [of "Suc (2 * m)" n] rec [of n "Suc (2 * m)"] show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   342
      by (simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   343
  qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   344
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   345
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   346
lemma self [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   347
  "n \<^bold>\<times> n = of_bool (True \<^bold>* True) * n"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   348
  by (induction n rule: nat_bit_induct) (simp_all add: end_of_bits)
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   349
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   350
lemma even_iff [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   351
  "even (m \<^bold>\<times> n) \<longleftrightarrow> \<not> (odd m \<^bold>* odd n)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   352
proof (induction m arbitrary: n rule: nat_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   353
  case zero
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   354
  show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   355
    by (cases "even n") (simp_all add: end_of_bits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   356
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   357
  case (even m)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   358
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   359
    by (simp add: rec [of "2 * m"])
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   360
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   361
  case (odd m)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   362
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   363
    by (simp add: rec [of "Suc (2 * m)"])
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   364
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   365
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   366
lemma of_bits:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   367
  "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
   368
    if "length bs = length cs" for bs cs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   369
using that proof (induction bs cs rule: list_induct2)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   370
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   371
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   372
    by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   373
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   374
  case (Cons b bs c cs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   375
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   376
    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
   377
      (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
   378
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   379
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   380
end
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   381
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   382
instantiation nat :: bit_operations
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   383
begin
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   384
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   385
definition not_nat :: "nat \<Rightarrow> nat"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   386
  where "not_nat = of_bits \<circ> map Not \<circ> bits_of"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   387
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   388
global_interpretation and_nat: zip_nat "(\<and>)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   389
  defines and_nat = and_nat.F
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   390
  by standard auto
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   391
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   392
global_interpretation and_nat: semilattice "(AND) :: nat \<Rightarrow> nat \<Rightarrow> nat"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   393
proof (rule semilattice.intro, fact and_nat.abel_semigroup_axioms, standard)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   394
  show "n AND n = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   395
    by (simp add: and_nat.self)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   396
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   397
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   398
declare and_nat.simps [simp] \<comment> \<open>inconsistent declaration handling by
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   399
  \<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   400
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   401
lemma zero_nat_and_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   402
  "0 AND n = 0" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   403
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   404
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   405
lemma and_zero_nat_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   406
  "n AND 0 = 0" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   407
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   408
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   409
global_interpretation or_nat: zip_nat "(\<or>)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   410
  defines or_nat = or_nat.F
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   411
  by standard auto
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   412
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   413
global_interpretation or_nat: semilattice "(OR) :: nat \<Rightarrow> nat \<Rightarrow> nat"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   414
proof (rule semilattice.intro, fact or_nat.abel_semigroup_axioms, standard)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   415
  show "n OR n = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   416
    by (simp add: or_nat.self)
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   417
qed
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   418
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   419
declare or_nat.simps [simp] \<comment> \<open>inconsistent declaration handling by
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   420
  \<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   421
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   422
lemma zero_nat_or_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   423
  "0 OR n = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   424
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   425
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   426
lemma or_zero_nat_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   427
  "n OR 0 = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   428
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   429
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   430
global_interpretation xor_nat: zip_nat "(\<noteq>)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   431
  defines xor_nat = xor_nat.F
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   432
  by standard auto
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   433
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   434
declare xor_nat.simps [simp] \<comment> \<open>inconsistent declaration handling by
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   435
  \<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   436
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   437
lemma zero_nat_xor_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   438
  "0 XOR n = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   439
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   440
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   441
lemma xor_zero_nat_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   442
  "n XOR 0 = n" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   443
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   444
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   445
definition shift_left_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   446
  where [simp]: "m << n = push_bit n m" for m :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   447
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   448
definition shift_right_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   449
  where [simp]: "m >> n = drop_bit n m" for m :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   450
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   451
instance proof
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   452
  show "semilattice ((AND) :: nat \<Rightarrow> _)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   453
    by (fact and_nat.semilattice_axioms)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   454
  show "semilattice ((OR):: nat \<Rightarrow> _)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   455
    by (fact or_nat.semilattice_axioms)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   456
  show "abel_semigroup ((XOR):: nat \<Rightarrow> _)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   457
    by (fact xor_nat.abel_semigroup_axioms)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   458
  show "(not :: nat \<Rightarrow> _) = of_bits \<circ> map Not \<circ> bits_of"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   459
    by (fact not_nat_def)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   460
  show "of_bits bs AND of_bits cs = (of_bits (map2 (\<and>) bs cs) :: nat)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   461
    if "length bs = length cs" for bs cs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   462
    using that by (fact and_nat.of_bits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   463
  show "of_bits bs OR of_bits cs = (of_bits (map2 (\<or>) bs cs) :: nat)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   464
    if "length bs = length cs" for bs cs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   465
    using that by (fact or_nat.of_bits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   466
  show "of_bits bs XOR of_bits cs = (of_bits (map2 (\<noteq>) bs cs) :: nat)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   467
    if "length bs = length cs" for bs cs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   468
    using that by (fact xor_nat.of_bits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   469
  show "m << n = of_bits (replicate n False @ bits_of m)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   470
    for m n :: nat
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   471
    by simp
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   472
  show "m >> n = of_bits (drop n (bits_of m))"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   473
    for m n :: nat
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   474
    by simp
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   475
qed
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   476
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   477
end
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   478
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   479
global_interpretation or_nat: semilattice_neutr "(OR)" "0 :: nat"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   480
  by standard simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   481
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   482
global_interpretation xor_nat: comm_monoid "(XOR)" "0 :: nat"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   483
  by standard simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   484
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   485
lemma not_nat_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   486
  "NOT 0 = (0::nat)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   487
  "n > 0 \<Longrightarrow> NOT n = of_bool (even n) + 2 * NOT (n div 2)" for n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   488
  by (simp_all add: not_eq)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   489
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   490
lemma not_1_nat [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   491
  "NOT 1 = (0::nat)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   492
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   493
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   494
lemma not_Suc_0 [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   495
  "NOT (Suc 0) = (0::nat)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   496
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   497
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   498
lemma Suc_0_and_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   499
  "Suc 0 AND n = n mod 2"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   500
  by (cases n) auto
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   501
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   502
lemma and_Suc_0_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   503
  "n AND Suc 0 = n mod 2"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   504
  using Suc_0_and_eq [of n] by (simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   505
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   506
lemma Suc_0_or_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   507
  "Suc 0 OR n = n + of_bool (even n)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   508
  by (cases n) (simp_all add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   509
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   510
lemma or_Suc_0_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   511
  "n OR Suc 0 = n + of_bool (even n)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   512
  using Suc_0_or_eq [of n] by (simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   513
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   514
lemma Suc_0_xor_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   515
  "Suc 0 XOR n = n + of_bool (even n) - of_bool (odd n)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   516
  by (cases n) (simp_all add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   517
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   518
lemma xor_Suc_0_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   519
  "n XOR Suc 0 = n + of_bool (even n) - of_bool (odd n)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   520
  using Suc_0_xor_eq [of n] by (simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   521
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   522
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   523
subsubsection \<open>Instance \<^typ>\<open>int\<close>\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   524
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   525
abbreviation (input) complement :: "int \<Rightarrow> int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   526
  where "complement k \<equiv> - k - 1"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   527
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   528
lemma complement_half:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   529
  "complement (k * 2) div 2 = complement k"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   530
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   531
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   532
locale zip_int = single: abel_semigroup f
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   533
  for f :: "bool \<Rightarrow> bool \<Rightarrow> bool"  (infixl "\<^bold>*" 70)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   534
begin
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   535
 
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   536
lemma False_False_imp_True_True:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   537
  "True \<^bold>* True" if "False \<^bold>* False"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   538
proof (rule ccontr)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   539
  assume "\<not> True \<^bold>* True"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   540
  with that show False
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   541
    using single.assoc [of False True True]
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   542
    by (cases "False \<^bold>* True") simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   543
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   544
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   545
function F :: "int \<Rightarrow> int \<Rightarrow> int"  (infixl "\<^bold>\<times>" 70)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   546
  where "k \<^bold>\<times> l = (if k \<in> {0, - 1} \<and> l \<in> {0, - 1}
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   547
    then - of_bool (odd k \<^bold>* odd l)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   548
    else of_bool (odd k \<^bold>* odd l) + (k div 2) \<^bold>\<times> (l div 2) * 2)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   549
  by auto
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   550
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   551
termination
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   552
  by (relation "measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))") auto
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   553
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   554
lemma zero_left_eq:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   555
  "0 \<^bold>\<times> l = (case (False \<^bold>* False, False \<^bold>* True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   556
    of (False, False) \<Rightarrow> 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   557
     | (False, True) \<Rightarrow> l
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   558
     | (True, False) \<Rightarrow> complement l
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   559
     | (True, True) \<Rightarrow> - 1)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   560
  by (induction l rule: int_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   561
   (simp_all split: bool.split) 
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   562
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   563
lemma minus_left_eq:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   564
  "- 1 \<^bold>\<times> l = (case (True \<^bold>* False, True \<^bold>* True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   565
    of (False, False) \<Rightarrow> 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   566
     | (False, True) \<Rightarrow> l
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   567
     | (True, False) \<Rightarrow> complement l
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   568
     | (True, True) \<Rightarrow> - 1)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   569
  by (induction l rule: int_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   570
   (simp_all split: bool.split) 
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   571
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   572
lemma zero_right_eq:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   573
  "k \<^bold>\<times> 0 = (case (False \<^bold>* False, False \<^bold>* True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   574
    of (False, False) \<Rightarrow> 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   575
     | (False, True) \<Rightarrow> k
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   576
     | (True, False) \<Rightarrow> complement k
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   577
     | (True, True) \<Rightarrow> - 1)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   578
  by (induction k rule: int_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   579
    (simp_all add: ac_simps split: bool.split)
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   580
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   581
lemma minus_right_eq:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   582
  "k \<^bold>\<times> - 1 = (case (True \<^bold>* False, True \<^bold>* True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   583
    of (False, False) \<Rightarrow> 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   584
     | (False, True) \<Rightarrow> k
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   585
     | (True, False) \<Rightarrow> complement k
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   586
     | (True, True) \<Rightarrow> - 1)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   587
  by (induction k rule: int_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   588
    (simp_all add: ac_simps split: bool.split)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   589
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   590
lemma simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   591
  "0 \<^bold>\<times> 0 = - of_bool (False \<^bold>* False)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   592
  "- 1 \<^bold>\<times> 0 = - of_bool (True \<^bold>* False)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   593
  "0 \<^bold>\<times> - 1 = - of_bool (False \<^bold>* True)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   594
  "- 1 \<^bold>\<times> - 1 = - of_bool (True \<^bold>* True)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   595
  "0 \<^bold>\<times> l = (case (False \<^bold>* False, False \<^bold>* True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   596
    of (False, False) \<Rightarrow> 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   597
     | (False, True) \<Rightarrow> l
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   598
     | (True, False) \<Rightarrow> complement l
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   599
     | (True, True) \<Rightarrow> - 1)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   600
  "- 1 \<^bold>\<times> l = (case (True \<^bold>* False, True \<^bold>* True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   601
    of (False, False) \<Rightarrow> 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   602
     | (False, True) \<Rightarrow> l
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   603
     | (True, False) \<Rightarrow> complement l
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   604
     | (True, True) \<Rightarrow> - 1)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   605
  "k \<^bold>\<times> 0 = (case (False \<^bold>* False, False \<^bold>* True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   606
    of (False, False) \<Rightarrow> 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   607
     | (False, True) \<Rightarrow> k
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   608
     | (True, False) \<Rightarrow> complement k
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   609
     | (True, True) \<Rightarrow> - 1)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   610
  "k \<^bold>\<times> - 1 = (case (True \<^bold>* False, True \<^bold>* True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   611
    of (False, False) \<Rightarrow> 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   612
     | (False, True) \<Rightarrow> k
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   613
     | (True, False) \<Rightarrow> complement k
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   614
     | (True, True) \<Rightarrow> - 1)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   615
  "k \<noteq> 0 \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> l \<noteq> 0 \<Longrightarrow> l \<noteq> - 1
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   616
    \<Longrightarrow> k \<^bold>\<times> l = of_bool (odd k \<^bold>* odd l) + (k div 2) \<^bold>\<times> (l div 2) * 2"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   617
  by simp_all[4] (simp_all only: zero_left_eq minus_left_eq zero_right_eq minus_right_eq, simp)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   618
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   619
declare F.simps [simp del]
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   620
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   621
lemma rec:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   622
  "k \<^bold>\<times> l = of_bool (odd k \<^bold>* odd l) + (k div 2) \<^bold>\<times> (l div 2) * 2"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   623
  by (cases "k \<in> {0, - 1} \<and> l \<in> {0, - 1}") (auto simp add: ac_simps F.simps [of k l] split: bool.split)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   624
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   625
sublocale abel_semigroup F
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   626
proof
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   627
  show "k \<^bold>\<times> l \<^bold>\<times> r = k \<^bold>\<times> (l \<^bold>\<times> r)" for k l r :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   628
  proof (induction k arbitrary: l r rule: int_bit_induct)
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   629
    case zero
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   630
    have "complement l \<^bold>\<times> r = complement (l \<^bold>\<times> r)" if "False \<^bold>* False" "\<not> False \<^bold>* True"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   631
    proof (induction l arbitrary: r rule: int_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   632
      case zero
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   633
      from that show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   634
        by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   635
    next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   636
      case minus
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   637
      from that show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   638
        by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   639
    next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   640
      case (even l)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   641
      with that rec [of _ r] show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   642
        by (cases "even r")
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   643
          (auto simp add: complement_half ac_simps False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   644
    next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   645
      case (odd l)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   646
      moreover have "- l - 1 = - 1 - l"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   647
        by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   648
      ultimately show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   649
        using that rec [of _ r]
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   650
        by (cases "even r")
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   651
          (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   652
    qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   653
    then show ?case
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   654
      by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   655
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   656
    case minus
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   657
    have "complement l \<^bold>\<times> r = complement (l \<^bold>\<times> r)" if "\<not> True \<^bold>* True" "False \<^bold>* True"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   658
    proof (induction l arbitrary: r rule: int_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   659
      case zero
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   660
      from that show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   661
        by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   662
    next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   663
      case minus
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   664
      from that show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   665
        by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   666
    next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   667
      case (even l)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   668
      with that rec [of _ r] show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   669
        by (cases "even r")
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   670
          (auto simp add: complement_half ac_simps False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   671
    next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   672
      case (odd l)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   673
      moreover have "- l - 1 = - 1 - l"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   674
        by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   675
      ultimately show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   676
        using that rec [of _ r]
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   677
        by (cases "even r")
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   678
          (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   679
    qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   680
    then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   681
      by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   682
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   683
    case (even k)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   684
    with rec [of "k * 2"] rec [of _ r] show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   685
      by (cases "even r"; cases "even l") (auto simp add: ac_simps False_False_imp_True_True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   686
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   687
    case (odd k)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   688
    with rec [of "1 + k * 2"] rec [of _ r] show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   689
      by (cases "even r"; cases "even l") (auto simp add: ac_simps False_False_imp_True_True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   690
  qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   691
  show "k \<^bold>\<times> l = l \<^bold>\<times> k" for k l :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   692
  proof (induction k arbitrary: l rule: int_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   693
    case zero
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   694
    show ?case
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   695
      by simp
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   696
  next
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   697
    case minus
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   698
    show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   699
      by simp
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   700
  next
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   701
    case (even k)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   702
    with rec [of "k * 2" l] rec [of l "k * 2"] show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   703
      by (simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   704
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   705
    case (odd k)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   706
    with rec [of "k * 2 + 1" l] rec [of l "k * 2 + 1"] show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   707
      by (simp add: ac_simps)
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   708
  qed
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   709
qed
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   710
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   711
lemma self [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   712
  "k \<^bold>\<times> k = (case (False \<^bold>* False, True \<^bold>* True)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   713
    of (False, False) \<Rightarrow> 0
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   714
     | (False, True) \<Rightarrow> k
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   715
     | (True, True) \<Rightarrow> - 1)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   716
  by (induction k rule: int_bit_induct) (auto simp add: False_False_imp_True_True split: bool.split)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   717
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   718
lemma even_iff [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   719
  "even (k \<^bold>\<times> l) \<longleftrightarrow> \<not> (odd k \<^bold>* odd l)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   720
proof (induction k arbitrary: l rule: int_bit_induct)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   721
  case zero
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   722
  show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   723
    by (cases "even l") (simp_all split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   724
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   725
  case minus
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   726
  show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   727
    by (cases "even l") (simp_all split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   728
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   729
  case (even k)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   730
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   731
    by (simp add: rec [of "k * 2"])
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   732
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   733
  case (odd k)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   734
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   735
    by (simp add: rec [of "1 + k * 2"])
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   736
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   737
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   738
lemma of_bits:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   739
  "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
   740
    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
   741
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
   742
  case Nil
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   743
  then show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   744
    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
   745
next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   746
  case (Cons b bs c cs)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   747
  show ?case
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   748
  proof (cases "bs = []")
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   749
    case True
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   750
    with Cons show ?thesis
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   751
      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
   752
        (auto simp add: ac_simps split: bool.splits)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   753
  next
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   754
    case False
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   755
    with Cons.hyps have "cs \<noteq> []"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   756
      by auto
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   757
    with \<open>bs \<noteq> []\<close> have "map2 (\<^bold>*) bs cs \<noteq> []"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   758
      by (simp add: zip_eq_Nil_iff)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   759
    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
   760
      by (cases b; cases c)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   761
        (auto simp add: \<open>\<not> False \<^bold>* False\<close> ac_simps
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   762
          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
   763
          rec [of "1 + of_bits bs * 2"])
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   764
  qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   765
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   766
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   767
end
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   768
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   769
instantiation int :: bit_operations
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   770
begin
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   771
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   772
definition not_int :: "int \<Rightarrow> int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   773
  where "not_int = complement"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   774
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   775
global_interpretation and_int: zip_int "(\<and>)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   776
  defines and_int = and_int.F
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   777
  by standard
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   778
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   779
declare and_int.simps [simp] \<comment> \<open>inconsistent declaration handling by
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   780
  \<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   781
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   782
global_interpretation and_int: semilattice "(AND) :: int \<Rightarrow> int \<Rightarrow> int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   783
proof (rule semilattice.intro, fact and_int.abel_semigroup_axioms, standard)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   784
  show "k AND k = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   785
    by (simp add: and_int.self)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   786
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   787
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   788
lemma zero_int_and_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   789
  "0 AND k = 0" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   790
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   791
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   792
lemma and_zero_int_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   793
  "k AND 0 = 0" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   794
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   795
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   796
lemma minus_int_and_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   797
  "- 1 AND k = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   798
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   799
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   800
lemma and_minus_int_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   801
  "k AND - 1 = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   802
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   803
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   804
global_interpretation or_int: zip_int "(\<or>)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   805
  defines or_int = or_int.F
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   806
  by standard
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   807
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   808
declare or_int.simps [simp] \<comment> \<open>inconsistent declaration handling by
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   809
  \<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   810
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   811
global_interpretation or_int: semilattice "(OR) :: int \<Rightarrow> int \<Rightarrow> int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   812
proof (rule semilattice.intro, fact or_int.abel_semigroup_axioms, standard)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   813
  show "k OR k = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   814
    by (simp add: or_int.self)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   815
qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   816
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   817
lemma zero_int_or_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   818
  "0 OR k = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   819
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   820
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   821
lemma and_zero_or_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   822
  "k OR 0 = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   823
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   824
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   825
lemma minus_int_or_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   826
  "- 1 OR k = - 1" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   827
  by simp
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   828
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   829
lemma or_minus_int_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   830
  "k OR - 1 = - 1" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   831
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   832
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   833
global_interpretation xor_int: zip_int "(\<noteq>)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   834
  defines xor_int = xor_int.F
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   835
  by standard
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   836
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   837
declare xor_int.simps [simp] \<comment> \<open>inconsistent declaration handling by
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   838
  \<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   839
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   840
lemma zero_int_xor_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   841
  "0 XOR k = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   842
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   843
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   844
lemma and_zero_xor_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   845
  "k XOR 0 = k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   846
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   847
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   848
lemma minus_int_xor_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   849
  "- 1 XOR k = complement k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   850
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   851
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   852
lemma xor_minus_int_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   853
  "k XOR - 1 = complement k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   854
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   855
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   856
definition shift_left_int :: "int \<Rightarrow> nat \<Rightarrow> int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   857
  where [simp]: "k << n = push_bit n k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   858
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   859
definition shift_right_int :: "int \<Rightarrow> nat \<Rightarrow> int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   860
  where [simp]: "k >> n = drop_bit n k" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   861
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   862
instance proof
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   863
  show "semilattice ((AND) :: int \<Rightarrow> _)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   864
    by (fact and_int.semilattice_axioms)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   865
  show "semilattice ((OR) :: int \<Rightarrow> _)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   866
    by (fact or_int.semilattice_axioms)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   867
  show "abel_semigroup ((XOR) :: int \<Rightarrow> _)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   868
    by (fact xor_int.abel_semigroup_axioms)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   869
  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
   870
  proof (rule sym, rule ext)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   871
    fix k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   872
    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
   873
      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
   874
  qed
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   875
  show "of_bits bs AND of_bits cs = (of_bits (map2 (\<and>) bs cs) :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   876
    if "length bs = length cs" for bs cs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   877
    using that by (rule and_int.of_bits) simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   878
  show "of_bits bs OR of_bits cs = (of_bits (map2 (\<or>) bs cs) :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   879
    if "length bs = length cs" for bs cs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   880
    using that by (rule or_int.of_bits) simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   881
  show "of_bits bs XOR of_bits cs = (of_bits (map2 (\<noteq>) bs cs) :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   882
    if "length bs = length cs" for bs cs
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   883
    using that by (rule xor_int.of_bits) simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   884
  show "k << n = of_bits (replicate n False @ bits_of k)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   885
    for k :: int and n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   886
    by (cases "n = 0") simp_all
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   887
  show "k >> n = of_bits (drop n (bits_of k))"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   888
    if "n < length (bits_of k)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   889
    for k :: int and n :: nat
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   890
    using that by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   891
qed
67909
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   892
f55b07f4d1ee proof of concept for algebraically founded bit lists
haftmann
parents:
diff changeset
   893
end
70912
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   894
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   895
global_interpretation and_int: semilattice_neutr "(AND)" "- 1 :: int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   896
  by standard simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   897
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   898
global_interpretation or_int: semilattice_neutr "(OR)" "0 :: int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   899
  by standard simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   900
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   901
global_interpretation xor_int: comm_monoid "(XOR)" "0 :: int"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   902
  by standard simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   903
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   904
lemma not_int_simps [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   905
  "NOT 0 = (- 1 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   906
  "NOT - 1 = (0 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   907
  "k \<noteq> 0 \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> NOT k = of_bool (even k) + 2 * NOT (k div 2)" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   908
  by (auto simp add: not_int_def elim: oddE)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   909
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   910
lemma not_one_int [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   911
  "NOT 1 = (- 2 :: int)"
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   912
  by simp
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   913
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   914
lemma one_and_int_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   915
  "1 AND k = k mod 2" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   916
  using and_int.rec [of 1] by (simp add: mod2_eq_if)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   917
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   918
lemma and_one_int_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   919
  "k AND 1 = k mod 2" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   920
  using one_and_int_eq [of 1] by (simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   921
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   922
lemma one_or_int_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   923
  "1 OR k = k + of_bool (even k)" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   924
  using or_int.rec [of 1] by (auto elim: oddE)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   925
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   926
lemma or_one_int_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   927
  "k OR 1 = k + of_bool (even k)" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   928
  using one_or_int_eq [of k] by (simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   929
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   930
lemma one_xor_int_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   931
  "1 XOR k = k + of_bool (even k) - of_bool (odd k)" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   932
  using xor_int.rec [of 1] by (auto elim: oddE)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   933
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   934
lemma xor_one_int_eq [simp]:
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   935
  "k XOR 1 = k + of_bool (even k) - of_bool (odd k)" for k :: int
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   936
  using one_xor_int_eq [of k] by (simp add: ac_simps)
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   937
8c2bef3df488 refined proof of concept for bit operations
haftmann
parents: 70353
diff changeset
   938
end