src/HOL/Word/Bits_Int.thy
author wenzelm
Sun, 03 Dec 2017 18:53:49 +0100
changeset 67120 491fd7f0b5df
parent 65363 5eb619751b14
child 67408 4a4c14b24800
permissions -rw-r--r--
misc tuning and modernization;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     1
(*  Title:      HOL/Word/Bits_Int.thy
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     2
    Author:     Jeremy Dawson and Gerwin Klein, NICTA
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     4
Definitions and basic theorems for bit-wise logical operations
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     5
for integers expressed using Pls, Min, BIT,
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     6
and converting them to and from lists of bools.
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     7
*)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     8
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
     9
section \<open>Bitwise Operations on Binary Integers\<close>
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
    10
54854
3324a0078636 prefer "Bits" as theory name for abstract bit operations, similar to "Orderings", "Lattices", "Groups" etc.
haftmann
parents: 54848
diff changeset
    11
theory Bits_Int
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    12
  imports Bits Bit_Representation
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    13
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    14
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
    15
subsection \<open>Logical operations\<close>
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
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    22
definition int_not_def: "bitNOT = (\<lambda>x::int. - x - 1)"
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
    23
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    24
function bitAND_int
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    25
  where "bitAND_int x y =
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    26
    (if x = 0 then 0 else if x = -1 then y
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    27
     else (bin_rest x AND bin_rest y) BIT (bin_last x \<and> bin_last y))"
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
    28
  by pat_completeness simp
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    29
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
    30
termination
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    31
  by (relation "measure (nat \<circ> abs \<circ> fst)", simp_all add: bin_rest_def)
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
    32
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
    33
declare bitAND_int.simps [simp del]
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    34
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    35
definition int_or_def: "bitOR = (\<lambda>x y::int. NOT (NOT x AND NOT y))"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    36
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    37
definition int_xor_def: "bitXOR = (\<lambda>x y::int. (x AND NOT y) OR (NOT x AND y))"
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    38
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    39
instance ..
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    40
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
    41
end
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
    42
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
    43
subsubsection \<open>Basic simplification rules\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
    44
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    45
lemma int_not_BIT [simp]: "NOT (w BIT b) = (NOT w) BIT (\<not> b)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    46
  by (cases b) (simp_all add: int_not_def Bit_def)
46016
c42e43287b5f simplify definition of NOT for type int
huffman
parents: 46001
diff changeset
    47
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    48
lemma int_not_simps [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
    49
  "NOT (0::int) = -1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
    50
  "NOT (1::int) = -2"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
    51
  "NOT (- 1::int) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
    52
  "NOT (numeral w::int) = - numeral (w + Num.One)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
    53
  "NOT (- numeral (Num.Bit0 w)::int) = numeral (Num.BitM w)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
    54
  "NOT (- numeral (Num.Bit1 w)::int) = numeral (Num.Bit0 w)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
    55
  unfolding int_not_def by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    56
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    57
lemma int_not_not [simp]: "NOT (NOT x) = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    58
  for x :: int
46017
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
    59
  unfolding int_not_def by simp
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
    60
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    61
lemma int_and_0 [simp]: "0 AND x = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    62
  for x :: int
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
    63
  by (simp add: bitAND_int.simps)
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
    64
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    65
lemma int_and_m1 [simp]: "-1 AND x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    66
  for x :: int
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
    67
  by (simp add: bitAND_int.simps)
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
    68
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    69
lemma int_and_Bits [simp]: "(x BIT b) AND (y BIT c) = (x AND y) BIT (b \<and> c)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    70
  by (subst bitAND_int.simps) (simp add: Bit_eq_0_iff Bit_eq_m1_iff)
46017
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
    71
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    72
lemma int_or_zero [simp]: "0 OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    73
  for x :: int
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    74
  by (simp add: int_or_def)
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
    75
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    76
lemma int_or_minus1 [simp]: "-1 OR x = -1"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    77
  for x :: int
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    78
  by (simp add: int_or_def)
46017
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
    79
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    80
lemma int_or_Bits [simp]: "(x BIT b) OR (y BIT c) = (x OR y) BIT (b \<or> c)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    81
  by (simp add: int_or_def)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    82
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    83
lemma int_xor_zero [simp]: "0 XOR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    84
  for x :: int
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    85
  by (simp add: int_xor_def)
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
    86
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    87
lemma int_xor_Bits [simp]: "(x BIT b) XOR (y BIT c) = (x XOR y) BIT ((b \<or> c) \<and> \<not> (b \<and> c))"
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
    88
  unfolding int_xor_def by auto
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
    89
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    90
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
    91
subsubsection \<open>Binary destructors\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
    92
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
    93
lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    94
  by (cases x rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
    95
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
    96
lemma bin_last_NOT [simp]: "bin_last (NOT x) \<longleftrightarrow> \<not> bin_last x"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
    97
  by (cases x rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
    98
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
    99
lemma bin_rest_AND [simp]: "bin_rest (x AND y) = bin_rest x AND bin_rest y"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   100
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   101
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   102
lemma bin_last_AND [simp]: "bin_last (x AND y) \<longleftrightarrow> bin_last x \<and> bin_last y"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   103
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   104
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   105
lemma bin_rest_OR [simp]: "bin_rest (x OR y) = bin_rest x OR bin_rest y"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   106
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   107
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   108
lemma bin_last_OR [simp]: "bin_last (x OR y) \<longleftrightarrow> bin_last x \<or> bin_last y"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   109
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   110
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   111
lemma bin_rest_XOR [simp]: "bin_rest (x XOR y) = bin_rest x XOR bin_rest y"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   112
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   113
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   114
lemma bin_last_XOR [simp]:
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   115
  "bin_last (x XOR y) \<longleftrightarrow> (bin_last x \<or> bin_last y) \<and> \<not> (bin_last x \<and> bin_last y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   116
  by (cases x rule: bin_exhaust, cases y rule: bin_exhaust) simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   117
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   118
lemma bin_nth_ops:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   119
  "\<And>x y. bin_nth (x AND y) n \<longleftrightarrow> bin_nth x n \<and> bin_nth y n"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   120
  "\<And>x y. bin_nth (x OR y) n \<longleftrightarrow> bin_nth x n \<or> bin_nth y n"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   121
  "\<And>x y. bin_nth (x XOR y) n \<longleftrightarrow> bin_nth x n \<noteq> bin_nth y n"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   122
  "\<And>x. bin_nth (NOT x) n \<longleftrightarrow> \<not> bin_nth x n"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   123
  by (induct n) auto
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   124
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   125
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   126
subsubsection \<open>Derived properties\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   127
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   128
lemma int_xor_minus1 [simp]: "-1 XOR x = NOT x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   129
  for x :: int
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
   130
  by (auto simp add: bin_eq_iff bin_nth_ops)
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
   131
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   132
lemma int_xor_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   133
  "w XOR 0 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   134
  "w XOR -1 = NOT w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   135
  for w :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   136
  by (auto simp add: bin_eq_iff bin_nth_ops)
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   137
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   138
lemma int_or_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   139
  "w OR 0 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   140
  "w OR -1 = -1"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   141
  for w :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   142
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   143
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   144
lemma int_and_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   145
  "w AND 0 = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   146
  "w AND -1 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   147
  for w :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   148
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   149
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   150
text \<open>Commutativity of the above.\<close>
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   151
lemma bin_ops_comm:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   152
  fixes x y :: int
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   153
  shows int_and_comm: "x AND y = y AND x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   154
    and int_or_comm:  "x OR y = y OR x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   155
    and int_xor_comm: "x XOR y = y XOR x"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   156
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   157
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   158
lemma bin_ops_same [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   159
  "x AND x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   160
  "x OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   161
  "x XOR x = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   162
  for x :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   163
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   164
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   165
lemmas bin_log_esimps =
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   166
  int_and_extra_simps  int_or_extra_simps  int_xor_extra_simps
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   167
  int_and_0 int_and_m1 int_or_zero int_or_minus1 int_xor_zero int_xor_minus1
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   168
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   169
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   170
subsubsection \<open>Basic properties of logical (bit-wise) operations\<close>
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   171
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   172
lemma bbw_ao_absorb: "x AND (y OR x) = x \<and> x OR (y AND x) = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   173
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   174
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   175
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   176
lemma bbw_ao_absorbs_other:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   177
  "x AND (x OR y) = x \<and> (y AND x) OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   178
  "(y OR x) AND x = x \<and> x OR (x AND y) = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   179
  "(x OR y) AND x = x \<and> (x AND y) OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   180
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   181
  by (auto simp add: bin_eq_iff bin_nth_ops)
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   182
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   183
lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   184
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   185
lemma int_xor_not: "(NOT x) XOR y = NOT (x XOR y) \<and> x XOR (NOT y) = NOT (x XOR y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   186
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   187
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   188
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   189
lemma int_and_assoc: "(x AND y) AND z = x AND (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   190
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   191
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   192
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   193
lemma int_or_assoc: "(x OR y) OR z = x OR (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   194
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   195
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   196
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   197
lemma int_xor_assoc: "(x XOR y) XOR z = x XOR (y XOR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   198
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   199
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   200
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   201
lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   202
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   203
(* BH: Why are these declared as simp rules??? *)
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   204
lemma bbw_lcs [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   205
  "y AND (x AND z) = x AND (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   206
  "y OR (x OR z) = x OR (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   207
  "y XOR (x XOR z) = x XOR (y XOR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   208
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   209
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   210
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   211
lemma bbw_not_dist:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   212
  "NOT (x OR y) = (NOT x) AND (NOT y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   213
  "NOT (x AND y) = (NOT x) OR (NOT y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   214
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   215
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   216
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   217
lemma bbw_oa_dist: "(x AND y) OR z = (x OR z) AND (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   218
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   219
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   220
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   221
lemma bbw_ao_dist: "(x OR y) AND z = (x AND z) OR (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   222
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   223
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   224
24367
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
   225
(*
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
   226
Why were these declared simp???
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   227
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
   228
*)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   229
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   230
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   231
subsubsection \<open>Simplification with numerals\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   232
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   233
text \<open>Cases for \<open>0\<close> and \<open>-1\<close> are already covered by other simp rules.\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   234
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   235
lemma bin_rl_eqI: "\<lbrakk>bin_rest x = bin_rest y; bin_last x = bin_last y\<rbrakk> \<Longrightarrow> x = y"
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   236
  by (metis (mono_tags) BIT_eq_iff bin_ex_rl bin_last_BIT bin_rest_BIT)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   237
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   238
lemma bin_rest_neg_numeral_BitM [simp]:
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   239
  "bin_rest (- numeral (Num.BitM w)) = - numeral w"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   240
  by (simp only: BIT_bin_simps [symmetric] bin_rest_BIT)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   241
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   242
lemma bin_last_neg_numeral_BitM [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   243
  "bin_last (- numeral (Num.BitM w))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   244
  by (simp only: BIT_bin_simps [symmetric] bin_last_BIT)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   245
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   246
(* FIXME: The rule sets below are very large (24 rules for each
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   247
  operator). Is there a simpler way to do this? *)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   248
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   249
lemma int_and_numerals [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   250
  "numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   251
  "numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (numeral x AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   252
  "numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   253
  "numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = (numeral x AND numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   254
  "numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (numeral x AND - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   255
  "numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (numeral x AND - numeral (y + Num.One)) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   256
  "numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (numeral x AND - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   257
  "numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = (numeral x AND - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   258
  "- numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (- numeral x AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   259
  "- numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (- numeral x AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   260
  "- numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (- numeral (x + Num.One) AND numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   261
  "- numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = (- numeral (x + Num.One) AND numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   262
  "- numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (- numeral x AND - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   263
  "- numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (- numeral x AND - numeral (y + Num.One)) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   264
  "- numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (- numeral (x + Num.One) AND - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   265
  "- numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = (- numeral (x + Num.One) AND - numeral (y + Num.One)) BIT True"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   266
  "(1::int) AND numeral (Num.Bit0 y) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   267
  "(1::int) AND numeral (Num.Bit1 y) = 1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   268
  "(1::int) AND - numeral (Num.Bit0 y) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   269
  "(1::int) AND - numeral (Num.Bit1 y) = 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   270
  "numeral (Num.Bit0 x) AND (1::int) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   271
  "numeral (Num.Bit1 x) AND (1::int) = 1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   272
  "- numeral (Num.Bit0 x) AND (1::int) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   273
  "- numeral (Num.Bit1 x) AND (1::int) = 1"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   274
  by (rule bin_rl_eqI; simp)+
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   275
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   276
lemma int_or_numerals [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   277
  "numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (numeral x OR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   278
  "numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = (numeral x OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   279
  "numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = (numeral x OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   280
  "numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = (numeral x OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   281
  "numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (numeral x OR - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   282
  "numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = (numeral x OR - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   283
  "numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = (numeral x OR - numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   284
  "numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = (numeral x OR - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   285
  "- numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (- numeral x OR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   286
  "- numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = (- numeral x OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   287
  "- numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = (- numeral (x + Num.One) OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   288
  "- numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = (- numeral (x + Num.One) OR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   289
  "- numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (- numeral x OR - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   290
  "- numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = (- numeral x OR - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   291
  "- numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = (- numeral (x + Num.One) OR - numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   292
  "- numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = (- numeral (x + Num.One) OR - numeral (y + Num.One)) BIT True"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   293
  "(1::int) OR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   294
  "(1::int) OR numeral (Num.Bit1 y) = numeral (Num.Bit1 y)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   295
  "(1::int) OR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   296
  "(1::int) OR - numeral (Num.Bit1 y) = - numeral (Num.Bit1 y)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   297
  "numeral (Num.Bit0 x) OR (1::int) = numeral (Num.Bit1 x)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   298
  "numeral (Num.Bit1 x) OR (1::int) = numeral (Num.Bit1 x)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   299
  "- numeral (Num.Bit0 x) OR (1::int) = - numeral (Num.BitM x)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   300
  "- numeral (Num.Bit1 x) OR (1::int) = - numeral (Num.Bit1 x)"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   301
  by (rule bin_rl_eqI; simp)+
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   302
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   303
lemma int_xor_numerals [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   304
  "numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (numeral x XOR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   305
  "numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = (numeral x XOR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   306
  "numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = (numeral x XOR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   307
  "numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (numeral x XOR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   308
  "numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (numeral x XOR - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   309
  "numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = (numeral x XOR - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   310
  "numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = (numeral x XOR - numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   311
  "numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (numeral x XOR - numeral (y + Num.One)) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   312
  "- numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (- numeral x XOR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   313
  "- numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = (- numeral x XOR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   314
  "- numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = (- numeral (x + Num.One) XOR numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   315
  "- numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (- numeral (x + Num.One) XOR numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   316
  "- numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (- numeral x XOR - numeral y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   317
  "- numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = (- numeral x XOR - numeral (y + Num.One)) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   318
  "- numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = (- numeral (x + Num.One) XOR - numeral y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   319
  "- numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (- numeral (x + Num.One) XOR - numeral (y + Num.One)) BIT False"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   320
  "(1::int) XOR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   321
  "(1::int) XOR numeral (Num.Bit1 y) = numeral (Num.Bit0 y)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   322
  "(1::int) XOR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   323
  "(1::int) XOR - numeral (Num.Bit1 y) = - numeral (Num.Bit0 (y + Num.One))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   324
  "numeral (Num.Bit0 x) XOR (1::int) = numeral (Num.Bit1 x)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   325
  "numeral (Num.Bit1 x) XOR (1::int) = numeral (Num.Bit0 x)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   326
  "- numeral (Num.Bit0 x) XOR (1::int) = - numeral (Num.BitM x)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   327
  "- numeral (Num.Bit1 x) XOR (1::int) = - numeral (Num.Bit0 (x + Num.One))"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   328
  by (rule bin_rl_eqI; simp)+
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   329
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   330
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   331
subsubsection \<open>Interactions with arithmetic\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   332
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   333
lemma plus_and_or [rule_format]: "\<forall>y::int. (x AND y) + (x OR y) = x + y"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   334
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   335
    apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   336
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   337
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   338
  apply (case_tac y rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   339
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   340
  apply (unfold Bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   341
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   342
  apply (erule_tac x = "x" in allE)
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   343
  apply simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   344
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   345
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   346
lemma le_int_or: "bin_sign y = 0 \<Longrightarrow> x \<le> x OR y"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   347
  for x y :: int
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   348
  apply (induct y arbitrary: x rule: bin_induct)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   349
    apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   350
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   351
  apply (case_tac x rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   352
  apply (case_tac b)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   353
   apply (case_tac [!] bit)
46604
9f9e85264e4d make uses of bin_sign respect int/bin distinction
huffman
parents: 46023
diff changeset
   354
     apply (auto simp: le_Bits)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   355
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   356
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   357
lemmas int_and_le =
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
   358
  xtrans(3) [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   359
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   360
text \<open>Interaction between bit-wise and arithmetic: good example of \<open>bin_induction\<close>.\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   361
lemma bin_add_not: "x + NOT x = (-1::int)"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   362
  apply (induct x rule: bin_induct)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   363
    apply clarsimp
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   364
   apply clarsimp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   365
  apply (case_tac bit, auto)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   366
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   367
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   368
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   369
subsubsection \<open>Truncating results of bit-wise operations\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   370
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   371
lemma bin_trunc_ao:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   372
  "bintrunc n x AND bintrunc n y = bintrunc n (x AND y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   373
  "bintrunc n x OR bintrunc n y = bintrunc n (x OR y)"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   374
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   375
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   376
lemma bin_trunc_xor: "bintrunc n (bintrunc n x XOR bintrunc n y) = bintrunc n (x XOR y)"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   377
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   378
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   379
lemma bin_trunc_not: "bintrunc n (NOT (bintrunc n x)) = bintrunc n (NOT x)"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   380
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   381
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   382
text \<open>Want theorems of the form of \<open>bin_trunc_xor\<close>.\<close>
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   383
lemma bintr_bintr_i: "x = bintrunc n y \<Longrightarrow> bintrunc n x = bintrunc n y"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   384
  by auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   385
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   386
lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   387
lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   388
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   389
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   390
subsection \<open>Setting and clearing bits\<close>
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   391
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   392
text \<open>nth bit, set/clear\<close>
54874
c55c5dacd6a1 move instantiation here from AFP/Native_Word
haftmann
parents: 54854
diff changeset
   393
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   394
primrec bin_sc :: "nat \<Rightarrow> bool \<Rightarrow> int \<Rightarrow> int"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   395
  where
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   396
    Z: "bin_sc 0 b w = bin_rest w BIT b"
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   397
  | 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
   398
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   399
lemma bin_nth_sc [simp]: "bin_nth (bin_sc n b w) n \<longleftrightarrow> b"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   400
  by (induct n arbitrary: w) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   401
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   402
lemma bin_sc_sc_same [simp]: "bin_sc n c (bin_sc n b w) = bin_sc n c w"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   403
  by (induct n arbitrary: w) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   404
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   405
lemma bin_sc_sc_diff: "m \<noteq> n \<Longrightarrow> bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   406
  apply (induct n arbitrary: w m)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   407
   apply (case_tac [!] m)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   408
     apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   409
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   410
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   411
lemma bin_nth_sc_gen: "bin_nth (bin_sc n b w) m = (if m = n then b else bin_nth w m)"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   412
  by (induct n arbitrary: w m) (case_tac [!] m, auto)
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   413
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   414
lemma bin_sc_nth [simp]: "bin_sc n (bin_nth w n) w = w"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   415
  by (induct n arbitrary: w) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   416
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   417
lemma bin_sign_sc [simp]: "bin_sign (bin_sc n b w) = bin_sign w"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   418
  by (induct n arbitrary: w) auto
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   419
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   420
lemma bin_sc_bintr [simp]: "bintrunc m (bin_sc n x (bintrunc m (w))) = bintrunc m (bin_sc n x w)"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   421
  apply (induct n arbitrary: w m)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   422
   apply (case_tac [!] w rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   423
   apply (case_tac [!] m, auto)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   424
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   425
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   426
lemma bin_clr_le: "bin_sc n False w \<le> w"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   427
  apply (induct n arbitrary: w)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   428
   apply (case_tac [!] w rule: bin_exhaust)
46605
b2563f7cf844 simplify proofs
huffman
parents: 46604
diff changeset
   429
   apply (auto simp: le_Bits)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   430
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   431
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   432
lemma bin_set_ge: "bin_sc n True w \<ge> w"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   433
  apply (induct n arbitrary: w)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   434
   apply (case_tac [!] w rule: bin_exhaust)
46605
b2563f7cf844 simplify proofs
huffman
parents: 46604
diff changeset
   435
   apply (auto simp: le_Bits)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   436
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   437
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   438
lemma bintr_bin_clr_le: "bintrunc n (bin_sc m False w) \<le> bintrunc n w"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   439
  apply (induct n arbitrary: w m)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   440
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   441
  apply (case_tac w rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   442
  apply (case_tac m)
46605
b2563f7cf844 simplify proofs
huffman
parents: 46604
diff changeset
   443
   apply (auto simp: le_Bits)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   444
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   445
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   446
lemma bintr_bin_set_ge: "bintrunc n (bin_sc m True w) \<ge> bintrunc n w"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   447
  apply (induct n arbitrary: w m)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   448
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   449
  apply (case_tac w rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   450
  apply (case_tac m)
46605
b2563f7cf844 simplify proofs
huffman
parents: 46604
diff changeset
   451
   apply (auto simp: le_Bits)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   452
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   453
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   454
lemma bin_sc_FP [simp]: "bin_sc n False 0 = 0"
46608
37e383cc7831 make uses of constant bin_sc respect int/bin distinction
huffman
parents: 46605
diff changeset
   455
  by (induct n) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   456
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 54874
diff changeset
   457
lemma bin_sc_TM [simp]: "bin_sc n True (- 1) = - 1"
46608
37e383cc7831 make uses of constant bin_sc respect int/bin distinction
huffman
parents: 46605
diff changeset
   458
  by (induct n) auto
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   459
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   460
lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   461
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   462
lemma bin_sc_minus: "0 < n \<Longrightarrow> bin_sc (Suc (n - 1)) b w = bin_sc n b w"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   463
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   464
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   465
lemmas bin_sc_Suc_minus =
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   466
  trans [OF bin_sc_minus [symmetric] bin_sc.Suc]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   467
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   468
lemma bin_sc_numeral [simp]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   469
  "bin_sc (numeral k) b w =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47108
diff changeset
   470
    bin_sc (pred_numeral k) b (bin_rest w) BIT bin_last w"
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47108
diff changeset
   471
  by (simp add: numeral_eq_Suc)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   472
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24418
diff changeset
   473
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   474
subsection \<open>Splitting and concatenation\<close>
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   475
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   476
definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   477
  where "bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0"
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   478
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   479
fun bin_rsplit_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   480
  where "bin_rsplit_aux n m c bs =
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   481
    (if m = 0 \<or> n = 0 then bs
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   482
     else
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   483
      let (a, b) = bin_split n c
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   484
      in bin_rsplit_aux n (m - n) a (b # bs))"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   485
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   486
definition bin_rsplit :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   487
  where "bin_rsplit n w = bin_rsplit_aux n (fst w) (snd w) []"
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   488
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   489
fun bin_rsplitl_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   490
  where "bin_rsplitl_aux n m c bs =
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   491
    (if m = 0 \<or> n = 0 then bs
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   492
     else
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   493
      let (a, b) = bin_split (min m n) c
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   494
      in bin_rsplitl_aux n (m - n) a (b # bs))"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   495
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   496
definition bin_rsplitl :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   497
  where "bin_rsplitl n w = bin_rsplitl_aux n (fst w) (snd w) []"
26558
7fcc10088e72 renamed app2 to map2
haftmann
parents: 26514
diff changeset
   498
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   499
declare bin_rsplit_aux.simps [simp del]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   500
declare bin_rsplitl_aux.simps [simp del]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   501
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   502
lemma bin_sign_cat: "bin_sign (bin_cat x n y) = bin_sign x"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   503
  by (induct n arbitrary: y) auto
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   504
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   505
lemma bin_cat_Suc_Bit: "bin_cat w (Suc n) (v BIT b) = bin_cat w n v BIT b"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   506
  by auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   507
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   508
lemma bin_nth_cat:
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   509
  "bin_nth (bin_cat x k y) n =
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   510
    (if n < k then bin_nth y n else bin_nth x (n - k))"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   511
  apply (induct k arbitrary: n y)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   512
   apply clarsimp
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   513
  apply (case_tac n, auto)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   514
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   515
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   516
lemma bin_nth_split:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   517
  "bin_split n c = (a, b) \<Longrightarrow>
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   518
    (\<forall>k. bin_nth a k = bin_nth c (n + k)) \<and>
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   519
    (\<forall>k. bin_nth b k = (k < n \<and> bin_nth c k))"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   520
  apply (induct n arbitrary: b c)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   521
   apply clarsimp
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
   522
  apply (clarsimp simp: Let_def split: prod.split_asm)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   523
  apply (case_tac k)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   524
  apply auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   525
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   526
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   527
lemma bin_cat_assoc: "bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   528
  by (induct n arbitrary: z) auto
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   529
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   530
lemma bin_cat_assoc_sym: "bin_cat x m (bin_cat y n z) = bin_cat (bin_cat x (m - n) y) (min m n) z"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   531
  apply (induct n arbitrary: z m)
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   532
   apply clarsimp
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   533
  apply (case_tac m, auto)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   534
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   535
45956
ae70b6830f15 add lemmas bin_cat_zero and bin_split_zero
huffman
parents: 45955
diff changeset
   536
lemma bin_cat_zero [simp]: "bin_cat 0 n w = bintrunc n w"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 45956
diff changeset
   537
  by (induct n arbitrary: w) auto
45956
ae70b6830f15 add lemmas bin_cat_zero and bin_split_zero
huffman
parents: 45955
diff changeset
   538
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   539
lemma bintr_cat1: "bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   540
  by (induct n arbitrary: b) auto
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   541
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   542
lemma bintr_cat: "bintrunc m (bin_cat a n b) =
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   543
    bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)"
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   544
  by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr)
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   545
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   546
lemma bintr_cat_same [simp]: "bintrunc n (bin_cat a n b) = bintrunc n b"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   547
  by (auto simp add : bintr_cat)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   548
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   549
lemma cat_bintr [simp]: "bin_cat a n (bintrunc n b) = bin_cat a n b"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   550
  by (induct n arbitrary: b) auto
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   551
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   552
lemma split_bintrunc: "bin_split n c = (a, b) \<Longrightarrow> b = bintrunc n c"
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
   553
  by (induct n arbitrary: b c) (auto simp: Let_def split: prod.split_asm)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   554
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   555
lemma bin_cat_split: "bin_split n w = (u, v) \<Longrightarrow> w = bin_cat u n v"
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
   556
  by (induct n arbitrary: v w) (auto simp: Let_def split: prod.split_asm)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   557
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   558
lemma bin_split_cat: "bin_split n (bin_cat v n w) = (v, bintrunc n w)"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   559
  by (induct n arbitrary: w) auto
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   560
45956
ae70b6830f15 add lemmas bin_cat_zero and bin_split_zero
huffman
parents: 45955
diff changeset
   561
lemma bin_split_zero [simp]: "bin_split n 0 = (0, 0)"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 45956
diff changeset
   562
  by (induct n) auto
45956
ae70b6830f15 add lemmas bin_cat_zero and bin_split_zero
huffman
parents: 45955
diff changeset
   563
46610
0c3a5e28f425 make uses of bin_split respect int/bin distinction
huffman
parents: 46609
diff changeset
   564
lemma bin_split_minus1 [simp]:
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 54874
diff changeset
   565
  "bin_split n (- 1) = (- 1, bintrunc n (- 1))"
46610
0c3a5e28f425 make uses of bin_split respect int/bin distinction
huffman
parents: 46609
diff changeset
   566
  by (induct n) auto
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   567
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   568
lemma bin_split_trunc:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   569
  "bin_split (min m n) c = (a, b) \<Longrightarrow>
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   570
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   571
  apply (induct n arbitrary: m b c, clarsimp)
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
   572
  apply (simp add: bin_rest_trunc Let_def split: prod.split_asm)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   573
  apply (case_tac m)
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
   574
   apply (auto simp: Let_def split: prod.split_asm)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   575
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   576
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   577
lemma bin_split_trunc1:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   578
  "bin_split n c = (a, b) \<Longrightarrow>
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   579
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   580
  apply (induct n arbitrary: m b c, clarsimp)
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
   581
  apply (simp add: bin_rest_trunc Let_def split: prod.split_asm)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   582
  apply (case_tac m)
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
   583
   apply (auto simp: Let_def split: prod.split_asm)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   584
  done
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   585
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   586
lemma bin_cat_num: "bin_cat a n b = a * 2 ^ n + bintrunc n b"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   587
  apply (induct n arbitrary: b)
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   588
   apply clarsimp
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 45956
diff changeset
   589
  apply (simp add: Bit_def)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   590
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   591
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   592
lemma bin_split_num: "bin_split n b = (b div 2 ^ n, b mod 2 ^ n)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   593
  apply (induct n arbitrary: b)
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   594
   apply simp
45529
0e1037d4e049 remove redundant lemmas bin_last_mod and bin_rest_div, use bin_last_def and bin_rest_def instead
huffman
parents: 45475
diff changeset
   595
  apply (simp add: bin_rest_def zdiv_zmult2_eq)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   596
  apply (case_tac b rule: bin_exhaust)
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   597
  apply simp
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   598
  apply (simp add: Bit_def mod_mult_mult1 p1mod22k)
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   599
  done
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
   600
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   601
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   602
subsection \<open>Miscellaneous lemmas\<close>
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   603
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   604
lemma nth_2p_bin: "bin_nth (2 ^ n) m = (m = n)"
45955
fc303e8f5c20 more uses of 'induct arbitrary'
huffman
parents: 45847
diff changeset
   605
  apply (induct n arbitrary: m)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   606
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   607
   apply safe
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   608
   apply (case_tac m)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   609
    apply (auto simp: Bit_B0_2t [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   610
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   611
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   612
(*for use when simplifying with bin_nth_Bit*)
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   613
lemma ex_eq_or: "(\<exists>m. n = Suc m \<and> (m = k \<or> P m)) \<longleftrightarrow> n = Suc k \<or> (\<exists>m. n = Suc m \<and> P m)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   614
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   615
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   616
lemma power_BIT: "2 ^ (Suc n) - 1 = (2 ^ n - 1) BIT True"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   617
  by (induct n) (simp_all add: Bit_B1)
54427
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   618
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   619
lemma mod_BIT: "bin BIT bit mod 2 ^ Suc n = (bin mod 2 ^ n) BIT bit"
54427
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   620
proof -
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 61799
diff changeset
   621
  have "2 * (bin mod 2 ^ n) + 1 = (2 * bin mod 2 ^ Suc n) + 1"
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 61799
diff changeset
   622
    by (simp add: mod_mult_mult1)
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 61799
diff changeset
   623
  also have "\<dots> = ((2 * bin mod 2 ^ Suc n) + 1) mod 2 ^ Suc n"
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 61799
diff changeset
   624
    by (simp add: ac_simps p1mod22k')
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 61799
diff changeset
   625
  also have "\<dots> = (2 * bin + 1) mod 2 ^ Suc n"
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 61799
diff changeset
   626
    by (simp only: mod_simps)
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 61799
diff changeset
   627
  finally show ?thesis
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 61799
diff changeset
   628
    by (auto simp add: Bit_def)
54427
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   629
qed
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   630
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   631
lemma AND_mod: "x AND 2 ^ n - 1 = x mod 2 ^ n"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   632
  for x :: int
54427
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   633
proof (induct x arbitrary: n rule: bin_induct)
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   634
  case 1
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   635
  then show ?case
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   636
    by simp
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   637
next
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   638
  case 2
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   639
  then show ?case
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   640
    by (simp, simp add: m1mod2k)
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   641
next
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   642
  case (3 bin bit)
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   643
  show ?case
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   644
  proof (cases n)
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   645
    case 0
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   646
    then show ?thesis by simp
54427
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   647
  next
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   648
    case (Suc m)
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   649
    with 3 show ?thesis
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   650
      by (simp only: power_BIT mod_BIT int_and_Bits) simp
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   651
  qed
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   652
qed
783861a66a60 separated comparision on bit operations into separate theory
haftmann
parents: 54224
diff changeset
   653
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   654
end
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
   655