src/HOL/Word/BinOperations.thy
author hoelzl
Thu, 12 Nov 2009 17:21:43 +0100
changeset 33638 548a34929e98
parent 30943 eb3dbbe971f6
child 37654 8e33b9d04a82
permissions -rw-r--r--
Renamed upd_snd_conv to apsnd_conv to be consistent with apfst_conv; Added apsnd_apfst_commute
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     1
(* 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     2
  Author: Jeremy Dawson and Gerwin Klein, NICTA
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
  definition and basic theorems for bit-wise logical operations 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     5
  for integers expressed using Pls, Min, BIT,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     6
  and converting them to and from lists of bools
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     7
*) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     8
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
     9
header {* Bitwise Operations on Binary Integers *}
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
    10
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
    11
theory BinOperations
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
    12
imports BinGeneral BitSyntax
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    13
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    14
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
    15
subsection {* Logical operations *}
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
    16
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
    17
text "bit-wise logical operations on the int type"
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
    18
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    19
instantiation int :: bit
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    20
begin
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    21
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    22
definition
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    23
  int_not_def [code del]: "bitNOT = bin_rec Int.Min Int.Pls 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
    24
    (\<lambda>w b s. s BIT (NOT b))"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    25
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    26
definition
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    27
  int_and_def [code del]: "bitAND = bin_rec (\<lambda>x. Int.Pls) (\<lambda>y. y) 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
    28
    (\<lambda>w b s y. s (bin_rest y) BIT (b AND bin_last y))"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    29
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    30
definition
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    31
  int_or_def [code del]: "bitOR = bin_rec (\<lambda>x. x) (\<lambda>y. Int.Min) 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
    32
    (\<lambda>w b s y. s (bin_rest y) BIT (b OR bin_last y))"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    33
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    34
definition
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    35
  int_xor_def [code del]: "bitXOR = bin_rec (\<lambda>x. x) bitNOT 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
    36
    (\<lambda>w b s y. s (bin_rest y) BIT (b XOR bin_last y))"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    37
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    38
instance ..
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    39
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    40
end
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
    41
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    42
lemma int_not_simps [simp]:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
    43
  "NOT Int.Pls = Int.Min"
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
    44
  "NOT Int.Min = Int.Pls"
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    45
  "NOT (Int.Bit0 w) = Int.Bit1 (NOT w)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    46
  "NOT (Int.Bit1 w) = Int.Bit0 (NOT w)"
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
    47
  "NOT (w BIT b) = (NOT w) BIT (NOT b)"
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    48
  unfolding int_not_def by (simp_all add: bin_rec_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    49
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    50
declare int_not_simps(1-4) [code]
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
    51
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    52
lemma int_xor_Pls [simp, code]: 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
    53
  "Int.Pls XOR x = x"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
    54
  unfolding int_xor_def by (simp add: bin_rec_PM)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    55
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    56
lemma int_xor_Min [simp, code]: 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
    57
  "Int.Min XOR x = NOT x"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
    58
  unfolding int_xor_def by (simp add: bin_rec_PM)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    59
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    60
lemma int_xor_Bits [simp]: 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
    61
  "(x BIT b) XOR (y BIT c) = (x XOR y) BIT (b XOR c)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    62
  apply (unfold int_xor_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    63
  apply (rule bin_rec_simps (1) [THEN fun_cong, THEN trans])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    64
    apply (rule ext, simp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    65
   prefer 2
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    66
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    67
  apply (rule ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    68
  apply (simp add: int_not_simps [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    69
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    70
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    71
lemma int_xor_Bits2 [simp, code]: 
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    72
  "(Int.Bit0 x) XOR (Int.Bit0 y) = Int.Bit0 (x XOR y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    73
  "(Int.Bit0 x) XOR (Int.Bit1 y) = Int.Bit1 (x XOR y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    74
  "(Int.Bit1 x) XOR (Int.Bit0 y) = Int.Bit1 (x XOR y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    75
  "(Int.Bit1 x) XOR (Int.Bit1 y) = Int.Bit0 (x XOR y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    76
  unfolding BIT_simps [symmetric] int_xor_Bits by simp_all
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    77
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    78
lemma int_xor_x_simps':
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
    79
  "w XOR (Int.Pls BIT bit.B0) = w"
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
    80
  "w XOR (Int.Min BIT bit.B1) = NOT w"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    81
  apply (induct w rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    82
       apply simp_all[4]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    83
   apply (unfold int_xor_Bits)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    84
   apply clarsimp+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    85
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    86
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    87
lemma int_xor_extra_simps [simp, code]:
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    88
  "w XOR Int.Pls = w"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    89
  "w XOR Int.Min = NOT w"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    90
  using int_xor_x_simps' by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    91
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    92
lemma int_or_Pls [simp, code]: 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
    93
  "Int.Pls OR x = x"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
    94
  by (unfold int_or_def) (simp add: bin_rec_PM)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    95
  
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
    96
lemma int_or_Min [simp, code]:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
    97
  "Int.Min OR x = Int.Min"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
    98
  by (unfold int_or_def) (simp add: bin_rec_PM)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    99
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   100
lemma int_or_Bits [simp]: 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   101
  "(x BIT b) OR (y BIT c) = (x OR y) BIT (b OR c)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   102
  unfolding int_or_def by (simp add: bin_rec_simps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   103
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
   104
lemma int_or_Bits2 [simp, code]: 
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   105
  "(Int.Bit0 x) OR (Int.Bit0 y) = Int.Bit0 (x OR y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   106
  "(Int.Bit0 x) OR (Int.Bit1 y) = Int.Bit1 (x OR y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   107
  "(Int.Bit1 x) OR (Int.Bit0 y) = Int.Bit1 (x OR y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   108
  "(Int.Bit1 x) OR (Int.Bit1 y) = Int.Bit1 (x OR y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   109
  unfolding BIT_simps [symmetric] int_or_Bits by simp_all
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   110
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   111
lemma int_or_x_simps': 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   112
  "w OR (Int.Pls BIT bit.B0) = w"
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   113
  "w OR (Int.Min BIT bit.B1) = Int.Min"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   114
  apply (induct w rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   115
       apply simp_all[4]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   116
   apply (unfold int_or_Bits)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   117
   apply clarsimp+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   118
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   119
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
   120
lemma int_or_extra_simps [simp, code]:
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   121
  "w OR Int.Pls = w"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   122
  "w OR Int.Min = Int.Min"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   123
  using int_or_x_simps' by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   124
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
   125
lemma int_and_Pls [simp, code]:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   126
  "Int.Pls AND x = Int.Pls"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   127
  unfolding int_and_def by (simp add: bin_rec_PM)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   128
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
   129
lemma int_and_Min [simp, code]:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   130
  "Int.Min AND x = x"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   131
  unfolding int_and_def by (simp add: bin_rec_PM)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   132
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   133
lemma int_and_Bits [simp]: 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   134
  "(x BIT b) AND (y BIT c) = (x AND y) BIT (b AND c)" 
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   135
  unfolding int_and_def by (simp add: bin_rec_simps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   136
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
   137
lemma int_and_Bits2 [simp, code]: 
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   138
  "(Int.Bit0 x) AND (Int.Bit0 y) = Int.Bit0 (x AND y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   139
  "(Int.Bit0 x) AND (Int.Bit1 y) = Int.Bit0 (x AND y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   140
  "(Int.Bit1 x) AND (Int.Bit0 y) = Int.Bit0 (x AND y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   141
  "(Int.Bit1 x) AND (Int.Bit1 y) = Int.Bit1 (x AND y)"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   142
  unfolding BIT_simps [symmetric] int_and_Bits by simp_all
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   143
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   144
lemma int_and_x_simps': 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   145
  "w AND (Int.Pls BIT bit.B0) = Int.Pls"
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   146
  "w AND (Int.Min BIT bit.B1) = w"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   147
  apply (induct w rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   148
       apply simp_all[4]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   149
   apply (unfold int_and_Bits)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   150
   apply clarsimp+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   151
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   152
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28042
diff changeset
   153
lemma int_and_extra_simps [simp, code]:
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   154
  "w AND Int.Pls = Int.Pls"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   155
  "w AND Int.Min = w"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   156
  using int_and_x_simps' by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   157
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   158
(* commutativity of the above *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   159
lemma bin_ops_comm:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   160
  shows
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   161
  int_and_comm: "!!y::int. x AND y = y AND x" and
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   162
  int_or_comm:  "!!y::int. x OR y = y OR x" and
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   163
  int_xor_comm: "!!y::int. x XOR y = y XOR x"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   164
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   165
          apply simp_all[6]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   166
    apply (case_tac y rule: bin_exhaust, simp add: bit_ops_comm)+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   167
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   168
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   169
lemma bin_ops_same [simp]:
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   170
  "(x::int) AND x = x" 
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   171
  "(x::int) OR x = x" 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   172
  "(x::int) XOR x = Int.Pls"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   173
  by (induct x rule: bin_induct) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   174
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   175
lemma int_not_not [simp]: "NOT (NOT (x::int)) = x"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   176
  by (induct x rule: bin_induct) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   177
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   178
lemmas bin_log_esimps = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   179
  int_and_extra_simps  int_or_extra_simps  int_xor_extra_simps
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   180
  int_and_Pls int_and_Min  int_or_Pls int_or_Min  int_xor_Pls int_xor_Min
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   181
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   182
(* basic properties of logical (bit-wise) operations *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   183
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   184
lemma bbw_ao_absorb: 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   185
  "!!y::int. x AND (y OR x) = x & x OR (y AND x) = x"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   186
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   187
    apply auto 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   188
   apply (case_tac [!] y rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   189
   apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   190
   apply (case_tac [!] bit)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   191
     apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   192
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   193
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   194
lemma bbw_ao_absorbs_other:
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   195
  "x AND (x OR y) = x \<and> (y AND x) OR x = (x::int)"
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   196
  "(y OR x) AND x = x \<and> x OR (x AND y) = (x::int)"
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   197
  "(x OR y) AND x = x \<and> (x AND y) OR x = (x::int)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   198
  apply (auto simp: bbw_ao_absorb int_or_comm)  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   199
      apply (subst int_or_comm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   200
    apply (simp add: bbw_ao_absorb)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   201
   apply (subst int_and_comm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   202
   apply (subst int_or_comm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   203
   apply (simp add: bbw_ao_absorb)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   204
  apply (subst int_and_comm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   205
  apply (simp add: bbw_ao_absorb)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   206
  done
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   207
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   208
lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   209
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   210
lemma int_xor_not:
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   211
  "!!y::int. (NOT x) XOR y = NOT (x XOR y) & 
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   212
        x XOR (NOT y) = NOT (x XOR y)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   213
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   214
    apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   215
   apply (case_tac y rule: bin_exhaust, auto, 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   216
          case_tac b, auto)+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   217
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   218
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   219
lemma bbw_assocs': 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   220
  "!!y z::int. (x AND y) AND z = x AND (y AND z) & 
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   221
          (x OR y) OR z = x OR (y OR z) & 
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   222
          (x XOR y) XOR z = x XOR (y XOR z)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   223
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   224
    apply (auto simp: int_xor_not)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   225
    apply (case_tac [!] y rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   226
    apply (case_tac [!] z rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   227
    apply (case_tac [!] bit)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   228
       apply (case_tac [!] b)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   229
             apply (auto simp del: BIT_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   230
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   231
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   232
lemma int_and_assoc:
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   233
  "(x AND y) AND (z::int) = x AND (y AND z)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   234
  by (simp add: bbw_assocs')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   235
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   236
lemma int_or_assoc:
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   237
  "(x OR y) OR (z::int) = x OR (y OR z)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   238
  by (simp add: bbw_assocs')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   239
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   240
lemma int_xor_assoc:
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   241
  "(x XOR y) XOR (z::int) = x XOR (y XOR z)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   242
  by (simp add: bbw_assocs')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   243
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   244
lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   245
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   246
lemma bbw_lcs [simp]: 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   247
  "(y::int) AND (x AND z) = x AND (y AND z)"
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   248
  "(y::int) OR (x OR z) = x OR (y OR z)"
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   249
  "(y::int) XOR (x XOR z) = x XOR (y XOR z)" 
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   250
  apply (auto simp: bbw_assocs [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   251
  apply (auto simp: bin_ops_comm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   252
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   253
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   254
lemma bbw_not_dist: 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   255
  "!!y::int. NOT (x OR y) = (NOT x) AND (NOT y)" 
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   256
  "!!y::int. NOT (x AND y) = (NOT x) OR (NOT y)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   257
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   258
       apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   259
   apply (case_tac [!] y rule: bin_exhaust)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   260
   apply (case_tac [!] bit, auto simp del: BIT_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   261
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   262
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   263
lemma bbw_oa_dist: 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   264
  "!!y z::int. (x AND y) OR z = 
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   265
          (x OR z) AND (y OR z)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   266
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   267
    apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   268
  apply (case_tac y rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   269
  apply (case_tac z rule: bin_exhaust)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   270
  apply (case_tac ba, auto simp del: BIT_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   271
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   272
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   273
lemma bbw_ao_dist: 
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   274
  "!!y z::int. (x OR y) AND z = 
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   275
          (x AND z) OR (y AND z)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   276
   apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   277
    apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   278
  apply (case_tac y rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   279
  apply (case_tac z rule: bin_exhaust)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   280
  apply (case_tac ba, auto simp del: BIT_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   281
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   282
24367
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
   283
(*
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
   284
Why were these declared simp???
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   285
declare bin_ops_comm [simp] bbw_assocs [simp] 
24367
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
   286
*)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   287
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   288
lemma plus_and_or [rule_format]:
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   289
  "ALL y::int. (x AND y) + (x OR y) = x + y"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   290
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   291
    apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   292
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   293
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   294
  apply (case_tac y rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   295
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   296
  apply (unfold Bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   297
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   298
  apply (erule_tac x = "x" in allE)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   299
  apply (simp split: bit.split)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   300
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   301
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   302
lemma le_int_or:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   303
  "!!x.  bin_sign y = Int.Pls ==> x <= x OR y"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   304
  apply (induct y rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   305
    apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   306
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   307
  apply (case_tac x rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   308
  apply (case_tac b)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   309
   apply (case_tac [!] bit)
26514
eff55c0a6d34 tuned towards code generation
haftmann
parents: 26086
diff changeset
   310
     apply (auto simp: less_eq_int_code)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   311
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   312
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   313
lemmas int_and_le =
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   314
  xtr3 [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or] ;
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   315
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   316
lemma bin_nth_ops:
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   317
  "!!x y. bin_nth (x AND y) n = (bin_nth x n & bin_nth y n)" 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   318
  "!!x y. bin_nth (x OR y) n = (bin_nth x n | bin_nth y n)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   319
  "!!x y. bin_nth (x XOR y) n = (bin_nth x n ~= bin_nth y n)" 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   320
  "!!x. bin_nth (NOT x) n = (~ bin_nth x n)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   321
  apply (induct n)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   322
         apply safe
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   323
                         apply (case_tac [!] x rule: bin_exhaust)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   324
                         apply (simp_all del: BIT_simps)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   325
                      apply (case_tac [!] y rule: bin_exhaust)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   326
                      apply (simp_all del: BIT_simps)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   327
        apply (auto dest: not_B1_is_B0 intro: B1_ass_B0)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   328
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   329
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   330
(* interaction between bit-wise and arithmetic *)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   331
(* good example of bin_induction *)
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   332
lemma bin_add_not: "x + NOT x = Int.Min"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   333
  apply (induct x rule: bin_induct)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   334
    apply clarsimp
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   335
   apply clarsimp
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   336
  apply (case_tac bit, auto)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   337
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   338
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   339
(* truncating results of bit-wise operations *)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   340
lemma bin_trunc_ao: 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   341
  "!!x y. (bintrunc n x) AND (bintrunc n y) = bintrunc n (x AND y)" 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   342
  "!!x y. (bintrunc n x) OR (bintrunc n y) = bintrunc n (x OR y)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   343
  apply (induct n)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   344
      apply auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   345
      apply (case_tac [!] x rule: bin_exhaust)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   346
      apply (case_tac [!] y rule: bin_exhaust)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   347
      apply auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   348
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   349
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   350
lemma bin_trunc_xor: 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   351
  "!!x y. bintrunc n (bintrunc n x XOR bintrunc n y) = 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   352
          bintrunc n (x XOR y)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   353
  apply (induct n)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   354
   apply auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   355
   apply (case_tac [!] x rule: bin_exhaust)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   356
   apply (case_tac [!] y rule: bin_exhaust)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   357
   apply auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   358
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   359
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   360
lemma bin_trunc_not: 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   361
  "!!x. bintrunc n (NOT (bintrunc n x)) = bintrunc n (NOT x)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   362
  apply (induct n)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   363
   apply auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   364
   apply (case_tac [!] x rule: bin_exhaust)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   365
   apply auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   366
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   367
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   368
(* want theorems of the form of bin_trunc_xor *)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   369
lemma bintr_bintr_i:
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   370
  "x = bintrunc n y ==> bintrunc n x = bintrunc n y"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   371
  by auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   372
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   373
lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   374
lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   375
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   376
subsection {* Setting and clearing bits *}
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   377
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   378
primrec
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   379
  bin_sc :: "nat => bit => int => int"
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   380
where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   381
  Z: "bin_sc 0 b w = bin_rest w BIT b"
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   382
  | Suc: "bin_sc (Suc n) b w = bin_sc n b (bin_rest w) BIT bin_last w"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   383
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   384
(** nth bit, set/clear **)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   385
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   386
lemma bin_nth_sc [simp]: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   387
  "!!w. bin_nth (bin_sc n b w) n = (b = bit.B1)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   388
  by (induct n)  auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   389
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   390
lemma bin_sc_sc_same [simp]: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   391
  "!!w. bin_sc n c (bin_sc n b w) = bin_sc n c w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   392
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   393
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   394
lemma bin_sc_sc_diff:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   395
  "!!w m. m ~= n ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   396
    bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   397
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   398
   apply (case_tac [!] m)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   399
     apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   400
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   401
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   402
lemma bin_nth_sc_gen: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   403
  "!!w m. bin_nth (bin_sc n b w) m = (if m = n then b = bit.B1 else bin_nth w m)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   404
  by (induct n) (case_tac [!] m, auto)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   405
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   406
lemma bin_sc_nth [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   407
  "!!w. (bin_sc n (If (bin_nth w n) bit.B1 bit.B0) w) = w"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   408
  by (induct n) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   409
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   410
lemma bin_sign_sc [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   411
  "!!w. bin_sign (bin_sc n b w) = bin_sign w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   412
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   413
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   414
lemma bin_sc_bintr [simp]: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   415
  "!!w m. bintrunc m (bin_sc n x (bintrunc m (w))) = bintrunc m (bin_sc n x w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   416
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   417
   apply (case_tac [!] w rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   418
   apply (case_tac [!] m, auto)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   419
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   420
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   421
lemma bin_clr_le:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   422
  "!!w. bin_sc n bit.B0 w <= w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   423
  apply (induct n) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   424
   apply (case_tac [!] w rule: bin_exhaust)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   425
   apply (auto simp del: BIT_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   426
   apply (unfold Bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   427
   apply (simp_all split: bit.split)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   428
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   429
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   430
lemma bin_set_ge:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   431
  "!!w. bin_sc n bit.B1 w >= w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   432
  apply (induct n) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   433
   apply (case_tac [!] w rule: bin_exhaust)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   434
   apply (auto simp del: BIT_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   435
   apply (unfold Bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   436
   apply (simp_all split: bit.split)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   437
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   438
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   439
lemma bintr_bin_clr_le:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   440
  "!!w m. bintrunc n (bin_sc m bit.B0 w) <= bintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   441
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   442
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   443
  apply (case_tac w rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   444
  apply (case_tac m)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   445
   apply (auto simp del: BIT_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   446
   apply (unfold Bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   447
   apply (simp_all split: bit.split)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   448
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   449
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   450
lemma bintr_bin_set_ge:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   451
  "!!w m. bintrunc n (bin_sc m bit.B1 w) >= bintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   452
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   453
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   454
  apply (case_tac w rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   455
  apply (case_tac m)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   456
   apply (auto simp del: BIT_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   457
   apply (unfold Bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   458
   apply (simp_all split: bit.split)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   459
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   460
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   461
lemma bin_sc_FP [simp]: "bin_sc n bit.B0 Int.Pls = Int.Pls"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   462
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   463
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   464
lemma bin_sc_TM [simp]: "bin_sc n bit.B1 Int.Min = Int.Min"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   465
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   466
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   467
lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   468
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   469
lemma bin_sc_minus:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   470
  "0 < n ==> bin_sc (Suc (n - 1)) b w = bin_sc n b w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   471
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   472
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   473
lemmas bin_sc_Suc_minus = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   474
  trans [OF bin_sc_minus [symmetric] bin_sc.Suc, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   475
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   476
lemmas bin_sc_Suc_pred [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   477
  bin_sc_Suc_minus [of "number_of bin", simplified nobm1, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   478
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   479
subsection {* Operations on lists of booleans *}
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   480
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   481
primrec bl_to_bin_aux :: "bool list \<Rightarrow> int \<Rightarrow> int" where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   482
  Nil: "bl_to_bin_aux [] w = w"
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   483
  | Cons: "bl_to_bin_aux (b # bs) w = 
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   484
      bl_to_bin_aux bs (w BIT (if b then bit.B1 else bit.B0))"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   485
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   486
definition bl_to_bin :: "bool list \<Rightarrow> int" where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   487
  bl_to_bin_def : "bl_to_bin bs = bl_to_bin_aux bs Int.Pls"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   488
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   489
primrec bin_to_bl_aux :: "nat \<Rightarrow> int \<Rightarrow> bool list \<Rightarrow> bool list" where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   490
  Z: "bin_to_bl_aux 0 w bl = bl"
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   491
  | Suc: "bin_to_bl_aux (Suc n) w bl =
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   492
      bin_to_bl_aux n (bin_rest w) ((bin_last w = bit.B1) # bl)"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   493
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   494
definition bin_to_bl :: "nat \<Rightarrow> int \<Rightarrow> bool list" where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   495
  bin_to_bl_def : "bin_to_bl n w = bin_to_bl_aux n w []"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   496
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   497
primrec bl_of_nth :: "nat \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool list" where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   498
  Suc: "bl_of_nth (Suc n) f = f n # bl_of_nth n f"
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   499
  | Z: "bl_of_nth 0 f = []"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   500
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   501
primrec takefill :: "'a \<Rightarrow> nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   502
  Z: "takefill fill 0 xs = []"
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   503
  | Suc: "takefill fill (Suc n) xs = (
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   504
      case xs of [] => fill # takefill fill n xs
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   505
        | y # ys => y # takefill fill n ys)"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   506
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   507
definition map2 :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'c list" where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   508
  "map2 f as bs = map (split f) (zip as bs)"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   509
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   510
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   511
subsection {* Splitting and concatenation *}
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   512
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   513
definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int" where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   514
  "bin_rcat n = foldl (%u v. bin_cat u n v) Int.Pls"
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   515
28042
1471f2974eb1 more function -> fun
krauss
parents: 26558
diff changeset
   516
fun bin_rsplit_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" where
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   517
  "bin_rsplit_aux n m c bs =
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   518
    (if m = 0 | n = 0 then bs else
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   519
      let (a, b) = bin_split n c 
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   520
      in bin_rsplit_aux n (m - n) a (b # bs))"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   521
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   522
definition bin_rsplit :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list" where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   523
  "bin_rsplit n w = bin_rsplit_aux n (fst w) (snd w) []"
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   524
28042
1471f2974eb1 more function -> fun
krauss
parents: 26558
diff changeset
   525
fun bin_rsplitl_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" where
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   526
  "bin_rsplitl_aux n m c bs =
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   527
    (if m = 0 | n = 0 then bs else
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   528
      let (a, b) = bin_split (min m n) c 
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   529
      in bin_rsplitl_aux n (m - n) a (b # bs))"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   530
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   531
definition bin_rsplitl :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list" where
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   532
  "bin_rsplitl n w = bin_rsplitl_aux n (fst w) (snd w) []"
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   533
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   534
declare bin_rsplit_aux.simps [simp del]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   535
declare bin_rsplitl_aux.simps [simp del]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   536
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   537
lemma bin_sign_cat: 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   538
  "!!y. bin_sign (bin_cat x n y) = bin_sign x"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   539
  by (induct n) auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   540
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   541
lemma bin_cat_Suc_Bit:
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   542
  "bin_cat w (Suc n) (v BIT b) = bin_cat w n v BIT b"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   543
  by auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   544
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   545
lemma bin_nth_cat: 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   546
  "!!n y. bin_nth (bin_cat x k y) n = 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   547
    (if n < k then bin_nth y n else bin_nth x (n - k))"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   548
  apply (induct k)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   549
   apply clarsimp
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   550
  apply (case_tac n, auto)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   551
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   552
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   553
lemma bin_nth_split:
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   554
  "!!b c. bin_split n c = (a, b) ==> 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   555
    (ALL k. bin_nth a k = bin_nth c (n + k)) & 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   556
    (ALL k. bin_nth b k = (k < n & bin_nth c k))"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   557
  apply (induct n)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   558
   apply clarsimp
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   559
  apply (clarsimp simp: Let_def split: ls_splits)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   560
  apply (case_tac k)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   561
  apply auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   562
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   563
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   564
lemma bin_cat_assoc: 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   565
  "!!z. bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)" 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   566
  by (induct n) auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   567
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   568
lemma bin_cat_assoc_sym: "!!z m. 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   569
  bin_cat x m (bin_cat y n z) = bin_cat (bin_cat x (m - n) y) (min m n) z"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   570
  apply (induct n, clarsimp)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   571
  apply (case_tac m, auto)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   572
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   573
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   574
lemma bin_cat_Pls [simp]: 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   575
  "!!w. bin_cat Int.Pls n w = bintrunc n w"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   576
  by (induct n) auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   577
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   578
lemma bintr_cat1: 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   579
  "!!b. bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   580
  by (induct n) auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   581
    
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   582
lemma bintr_cat: "bintrunc m (bin_cat a n b) = 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   583
    bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   584
  by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   585
    
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   586
lemma bintr_cat_same [simp]: 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   587
  "bintrunc n (bin_cat a n b) = bintrunc n b"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   588
  by (auto simp add : bintr_cat)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   589
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   590
lemma cat_bintr [simp]: 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   591
  "!!b. bin_cat a n (bintrunc n b) = bin_cat a n b"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   592
  by (induct n) auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   593
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   594
lemma split_bintrunc: 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   595
  "!!b c. bin_split n c = (a, b) ==> b = bintrunc n c"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   596
  by (induct n) (auto simp: Let_def split: ls_splits)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   597
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   598
lemma bin_cat_split:
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   599
  "!!v w. bin_split n w = (u, v) ==> w = bin_cat u n v"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   600
  by (induct n) (auto simp: Let_def split: ls_splits)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   601
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   602
lemma bin_split_cat:
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   603
  "!!w. bin_split n (bin_cat v n w) = (v, bintrunc n w)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   604
  by (induct n) auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   605
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   606
lemma bin_split_Pls [simp]:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   607
  "bin_split n Int.Pls = (Int.Pls, Int.Pls)"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   608
  by (induct n) (auto simp: Let_def split: ls_splits)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   609
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   610
lemma bin_split_Min [simp]:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25762
diff changeset
   611
  "bin_split n Int.Min = (Int.Min, bintrunc n Int.Min)"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   612
  by (induct n) (auto simp: Let_def split: ls_splits)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   613
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   614
lemma bin_split_trunc:
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   615
  "!!m b c. bin_split (min m n) c = (a, b) ==> 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   616
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   617
  apply (induct n, clarsimp)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   618
  apply (simp add: bin_rest_trunc Let_def split: ls_splits)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   619
  apply (case_tac m)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   620
   apply (auto simp: Let_def split: ls_splits)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   621
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   622
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   623
lemma bin_split_trunc1:
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   624
  "!!m b c. bin_split n c = (a, b) ==> 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   625
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   626
  apply (induct n, clarsimp)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   627
  apply (simp add: bin_rest_trunc Let_def split: ls_splits)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   628
  apply (case_tac m)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   629
   apply (auto simp: Let_def split: ls_splits)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   630
  done
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   631
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   632
lemma bin_cat_num:
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   633
  "!!b. bin_cat a n b = a * 2 ^ n + bintrunc n b"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   634
  apply (induct n, clarsimp)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   635
  apply (simp add: Bit_def cong: number_of_False_cong)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   636
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   637
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   638
lemma bin_split_num:
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   639
  "!!b. bin_split n b = (b div 2 ^ n, b mod 2 ^ n)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   640
  apply (induct n, clarsimp)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   641
  apply (simp add: bin_rest_div zdiv_zmult2_eq)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   642
  apply (case_tac b rule: bin_exhaust)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   643
  apply simp
30943
eb3dbbe971f6 zmod_zmult_zmult1 now subsumed by mod_mult_mult1
haftmann
parents: 29631
diff changeset
   644
  apply (simp add: Bit_def mod_mult_mult1 p1mod22k
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   645
              split: bit.split 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   646
              cong: number_of_False_cong)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   647
  done 
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   648
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   649
subsection {* Miscellaneous lemmas *}
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   650
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   651
lemma nth_2p_bin: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   652
  "!!m. bin_nth (2 ^ n) m = (m = n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   653
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   654
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   655
   apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   656
     apply (case_tac m) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   657
      apply (auto simp: trans [OF numeral_1_eq_1 [symmetric] number_of_eq])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   658
   apply (case_tac m) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   659
    apply (auto simp: Bit_B0_2t [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   660
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   661
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   662
(* for use when simplifying with bin_nth_Bit *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   663
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   664
lemma ex_eq_or:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   665
  "(EX m. n = Suc m & (m = k | P m)) = (n = Suc k | (EX m. n = Suc m & P m))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   666
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   667
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   668
end
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   669