author | wenzelm |
Sat, 15 Mar 2014 11:28:07 +0100 | |
changeset 56159 | 39f7b7690de6 |
parent 54873 | c92a0e6ba828 |
child 58410 | 6d46ad54a2ab |
permissions | -rw-r--r-- |
24333 | 1 |
(* |
2 |
Author: Jeremy Dawson, NICTA |
|
3 |
*) |
|
4 |
||
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
|
5 |
header {* Integers as implict bit strings *} |
24350 | 6 |
|
37658 | 7 |
theory Bit_Representation |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
8 |
imports Misc_Numeric |
24333 | 9 |
begin |
10 |
||
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
|
11 |
subsection {* Constructors and destructors for binary integers *} |
26557 | 12 |
|
54848 | 13 |
definition Bit :: "int \<Rightarrow> bool \<Rightarrow> int" (infixl "BIT" 90) |
14 |
where |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
15 |
"k BIT b = (if b then 1 else 0) + k + k" |
26557 | 16 |
|
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
|
17 |
lemma Bit_B0: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
18 |
"k BIT False = k + k" |
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
|
19 |
by (unfold Bit_def) simp |
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
|
20 |
|
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
|
21 |
lemma Bit_B1: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
22 |
"k BIT True = k + k + 1" |
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
|
23 |
by (unfold Bit_def) simp |
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
|
24 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
25 |
lemma Bit_B0_2t: "k BIT False = 2 * k" |
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
|
26 |
by (rule trans, rule Bit_B0) simp |
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
|
27 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
28 |
lemma Bit_B1_2t: "k BIT True = 2 * k + 1" |
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
|
29 |
by (rule trans, rule Bit_B1) simp |
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
|
30 |
|
54848 | 31 |
definition bin_last :: "int \<Rightarrow> bool" |
32 |
where |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
33 |
"bin_last w \<longleftrightarrow> w mod 2 = 1" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
34 |
|
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
35 |
lemma bin_last_odd: |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
36 |
"bin_last = odd" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
37 |
by (rule ext) (simp add: bin_last_def even_def) |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
38 |
|
54848 | 39 |
definition bin_rest :: "int \<Rightarrow> int" |
40 |
where |
|
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
41 |
"bin_rest w = w div 2" |
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
42 |
|
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
43 |
lemma bin_rl_simp [simp]: |
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
44 |
"bin_rest w BIT bin_last w = w" |
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
45 |
unfolding bin_rest_def bin_last_def Bit_def |
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
46 |
using mod_div_equality [of w 2] |
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
47 |
by (cases "w mod 2 = 0", simp_all) |
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
48 |
|
46600 | 49 |
lemma bin_rest_BIT [simp]: "bin_rest (x BIT b) = x" |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
50 |
unfolding bin_rest_def Bit_def |
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
51 |
by (cases b, simp_all) |
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
52 |
|
46600 | 53 |
lemma bin_last_BIT [simp]: "bin_last (x BIT b) = b" |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
54 |
unfolding bin_last_def Bit_def |
54873 | 55 |
by (cases b) simp_all |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
56 |
|
45848 | 57 |
lemma BIT_eq_iff [iff]: "u BIT b = v BIT c \<longleftrightarrow> u = v \<and> b = c" |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
58 |
apply (auto simp add: Bit_def) |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
59 |
apply arith |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
60 |
apply arith |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
61 |
done |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
62 |
|
45849 | 63 |
lemma BIT_bin_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
64 |
"numeral k BIT False = numeral (Num.Bit0 k)" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
65 |
"numeral k BIT True = numeral (Num.Bit1 k)" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
66 |
"(- numeral k) BIT False = - numeral (Num.Bit0 k)" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
67 |
"(- numeral k) BIT True = - numeral (Num.BitM k)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
68 |
unfolding numeral.simps numeral_BitM |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
69 |
unfolding Bit_def |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
70 |
by (simp_all del: arith_simps add_numeral_special diff_numeral_special) |
45849 | 71 |
|
72 |
lemma BIT_special_simps [simp]: |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
73 |
shows "0 BIT False = 0" and "0 BIT True = 1" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
74 |
and "1 BIT False = 2" and "1 BIT True = 3" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
75 |
and "(- 1) BIT False = - 2" and "(- 1) BIT True = - 1" |
45849 | 76 |
unfolding Bit_def by simp_all |
77 |
||
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
78 |
lemma Bit_eq_0_iff: "w BIT b = 0 \<longleftrightarrow> w = 0 \<and> \<not> b" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
79 |
apply (auto simp add: Bit_def) |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
80 |
apply arith |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
81 |
done |
53438 | 82 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
83 |
lemma Bit_eq_m1_iff: "w BIT b = -1 \<longleftrightarrow> w = -1 \<and> b" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
84 |
apply (auto simp add: Bit_def) |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
85 |
apply arith |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
86 |
done |
53438 | 87 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
88 |
lemma BitM_inc: "Num.BitM (Num.inc w) = Num.Bit1 w" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
89 |
by (induct w, simp_all) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
90 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
91 |
lemma expand_BIT: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
92 |
"numeral (Num.Bit0 w) = numeral w BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
93 |
"numeral (Num.Bit1 w) = numeral w BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
94 |
"- numeral (Num.Bit0 w) = (- numeral w) BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
95 |
"- numeral (Num.Bit1 w) = (- numeral (w + Num.One)) BIT True" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
96 |
unfolding add_One by (simp_all add: BitM_inc) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
97 |
|
45851
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
98 |
lemma bin_last_numeral_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
99 |
"\<not> bin_last 0" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
100 |
"bin_last 1" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
101 |
"bin_last -1" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
102 |
"bin_last Numeral1" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
103 |
"\<not> bin_last (numeral (Num.Bit0 w))" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
104 |
"bin_last (numeral (Num.Bit1 w))" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
105 |
"\<not> bin_last (- numeral (Num.Bit0 w))" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
106 |
"bin_last (- numeral (Num.Bit1 w))" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
107 |
unfolding expand_BIT bin_last_BIT by (simp_all add: bin_last_def zmod_zminus1_eq_if) |
45851
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
108 |
|
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
109 |
lemma bin_rest_numeral_simps [simp]: |
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
110 |
"bin_rest 0 = 0" |
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
111 |
"bin_rest 1 = 0" |
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
112 |
"bin_rest -1 = -1" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
113 |
"bin_rest Numeral1 = 0" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
114 |
"bin_rest (numeral (Num.Bit0 w)) = numeral w" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
115 |
"bin_rest (numeral (Num.Bit1 w)) = numeral w" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
116 |
"bin_rest (- numeral (Num.Bit0 w)) = - numeral w" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
117 |
"bin_rest (- numeral (Num.Bit1 w)) = - numeral (w + Num.One)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
118 |
unfolding expand_BIT bin_rest_BIT by (simp_all add: bin_rest_def zdiv_zminus1_eq_if) |
26557 | 119 |
|
120 |
lemma less_Bits: |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
121 |
"v BIT b < w BIT c \<longleftrightarrow> v < w \<or> v \<le> w \<and> \<not> b \<and> c" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
122 |
unfolding Bit_def by auto |
24333 | 123 |
|
26557 | 124 |
lemma le_Bits: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
125 |
"v BIT b \<le> w BIT c \<longleftrightarrow> v < w \<or> v \<le> w \<and> (\<not> b \<or> c)" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
126 |
unfolding Bit_def by auto |
26557 | 127 |
|
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
|
128 |
lemma pred_BIT_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
129 |
"x BIT False - 1 = (x - 1) BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
130 |
"x BIT True - 1 = x BIT False" |
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
|
131 |
by (simp_all add: Bit_B0_2t Bit_B1_2t) |
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
|
132 |
|
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
|
133 |
lemma succ_BIT_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
134 |
"x BIT False + 1 = x BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
135 |
"x BIT True + 1 = (x + 1) BIT False" |
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
|
136 |
by (simp_all add: Bit_B0_2t Bit_B1_2t) |
26557 | 137 |
|
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
|
138 |
lemma add_BIT_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
139 |
"x BIT False + y BIT False = (x + y) BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
140 |
"x BIT False + y BIT True = (x + y) BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
141 |
"x BIT True + y BIT False = (x + y) BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
142 |
"x BIT True + y BIT True = (x + y + 1) BIT False" |
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
|
143 |
by (simp_all add: Bit_B0_2t Bit_B1_2t) |
26557 | 144 |
|
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
|
145 |
lemma mult_BIT_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
146 |
"x BIT False * y = (x * y) BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
147 |
"x * y BIT False = (x * y) BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
148 |
"x BIT True * y = (x * y) BIT False + y" |
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
|
149 |
by (simp_all add: Bit_B0_2t Bit_B1_2t algebra_simps) |
26557 | 150 |
|
151 |
lemma B_mod_2': |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
152 |
"X = 2 ==> (w BIT True) mod X = 1 & (w BIT False) mod X = 0" |
26557 | 153 |
apply (simp (no_asm) only: Bit_B0 Bit_B1) |
54873 | 154 |
apply simp |
24465 | 155 |
done |
24333 | 156 |
|
26557 | 157 |
lemma bin_ex_rl: "EX w b. w BIT b = bin" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
158 |
by (metis bin_rl_simp) |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
159 |
|
26557 | 160 |
lemma bin_exhaust: |
161 |
assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q" |
|
162 |
shows "Q" |
|
163 |
apply (insert bin_ex_rl [of bin]) |
|
164 |
apply (erule exE)+ |
|
165 |
apply (rule Q) |
|
166 |
apply force |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
167 |
done |
24333 | 168 |
|
26514 | 169 |
primrec bin_nth where |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
170 |
Z: "bin_nth w 0 \<longleftrightarrow> bin_last w" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
171 |
| Suc: "bin_nth w (Suc n) \<longleftrightarrow> bin_nth (bin_rest w) n" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
172 |
|
26557 | 173 |
lemma bin_abs_lem: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
174 |
"bin = (w BIT b) ==> bin ~= -1 --> bin ~= 0 --> |
26557 | 175 |
nat (abs w) < nat (abs bin)" |
46598 | 176 |
apply clarsimp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
177 |
apply (unfold Bit_def) |
26557 | 178 |
apply (cases b) |
179 |
apply (clarsimp, arith) |
|
180 |
apply (clarsimp, arith) |
|
181 |
done |
|
182 |
||
183 |
lemma bin_induct: |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
184 |
assumes PPls: "P 0" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
185 |
and PMin: "P -1" |
26557 | 186 |
and PBit: "!!bin bit. P bin ==> P (bin BIT bit)" |
187 |
shows "P bin" |
|
188 |
apply (rule_tac P=P and a=bin and f1="nat o abs" |
|
189 |
in wf_measure [THEN wf_induct]) |
|
190 |
apply (simp add: measure_def inv_image_def) |
|
191 |
apply (case_tac x rule: bin_exhaust) |
|
192 |
apply (frule bin_abs_lem) |
|
193 |
apply (auto simp add : PPls PMin PBit) |
|
194 |
done |
|
195 |
||
24333 | 196 |
lemma Bit_div2 [simp]: "(w BIT b) div 2 = w" |
46600 | 197 |
unfolding bin_rest_def [symmetric] by (rule bin_rest_BIT) |
24333 | 198 |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
199 |
lemma bin_nth_eq_iff: |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
200 |
"bin_nth x = bin_nth y \<longleftrightarrow> x = y" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
201 |
proof - |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
202 |
have bin_nth_lem [rule_format]: "ALL y. bin_nth x = bin_nth y --> x = y" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
203 |
apply (induct x rule: bin_induct) |
46008
c296c75f4cf4
reverted some changes for set->predicate transition, according to "hg log -u berghofe -r Isabelle2007:Isabelle2008";
wenzelm
parents:
46001
diff
changeset
|
204 |
apply safe |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
205 |
apply (erule rev_mp) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
206 |
apply (induct_tac y rule: bin_induct) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
207 |
apply safe |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
208 |
apply (drule_tac x=0 in fun_cong, force) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
209 |
apply (erule notE, rule ext, |
24333 | 210 |
drule_tac x="Suc x" in fun_cong, force) |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
211 |
apply (drule_tac x=0 in fun_cong, force) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
212 |
apply (erule rev_mp) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
213 |
apply (induct_tac y rule: bin_induct) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
214 |
apply safe |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
215 |
apply (drule_tac x=0 in fun_cong, force) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
216 |
apply (erule notE, rule ext, |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
217 |
drule_tac x="Suc x" in fun_cong, force) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
218 |
apply (metis Bit_eq_m1_iff Z bin_last_BIT) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
219 |
apply (case_tac y rule: bin_exhaust) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
220 |
apply clarify |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
221 |
apply (erule allE) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
222 |
apply (erule impE) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
223 |
prefer 2 |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
224 |
apply (erule conjI) |
24333 | 225 |
apply (drule_tac x=0 in fun_cong, force) |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
226 |
apply (rule ext) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
227 |
apply (drule_tac x="Suc ?x" in fun_cong, force) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
228 |
done |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
229 |
show ?thesis |
24333 | 230 |
by (auto elim: bin_nth_lem) |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
231 |
qed |
24333 | 232 |
|
45604 | 233 |
lemmas bin_eqI = ext [THEN bin_nth_eq_iff [THEN iffD1]] |
24333 | 234 |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
235 |
lemma bin_eq_iff: |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
236 |
"x = y \<longleftrightarrow> (\<forall>n. bin_nth x n = bin_nth y n)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
237 |
using bin_nth_eq_iff by auto |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
238 |
|
45853 | 239 |
lemma bin_nth_zero [simp]: "\<not> bin_nth 0 n" |
240 |
by (induct n) auto |
|
241 |
||
46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46001
diff
changeset
|
242 |
lemma bin_nth_1 [simp]: "bin_nth 1 n \<longleftrightarrow> n = 0" |
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46001
diff
changeset
|
243 |
by (cases n) simp_all |
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46001
diff
changeset
|
244 |
|
45952 | 245 |
lemma bin_nth_minus1 [simp]: "bin_nth -1 n" |
246 |
by (induct n) auto |
|
247 |
||
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
248 |
lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 \<longleftrightarrow> b" |
24333 | 249 |
by auto |
250 |
||
251 |
lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n" |
|
252 |
by auto |
|
253 |
||
254 |
lemma bin_nth_minus [simp]: "0 < n ==> bin_nth (w BIT b) n = bin_nth w (n - 1)" |
|
255 |
by (cases n) auto |
|
256 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
257 |
lemma bin_nth_numeral: |
47219
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47170
diff
changeset
|
258 |
"bin_rest x = y \<Longrightarrow> bin_nth x (numeral n) = bin_nth y (pred_numeral n)" |
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47170
diff
changeset
|
259 |
by (simp add: numeral_eq_Suc) |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
260 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
261 |
lemmas bin_nth_numeral_simps [simp] = |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
262 |
bin_nth_numeral [OF bin_rest_numeral_simps(2)] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
263 |
bin_nth_numeral [OF bin_rest_numeral_simps(5)] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
264 |
bin_nth_numeral [OF bin_rest_numeral_simps(6)] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
265 |
bin_nth_numeral [OF bin_rest_numeral_simps(7)] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
266 |
bin_nth_numeral [OF bin_rest_numeral_simps(8)] |
24333 | 267 |
|
268 |
lemmas bin_nth_simps = |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
269 |
bin_nth.Z bin_nth.Suc bin_nth_zero bin_nth_minus1 |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
270 |
bin_nth_numeral_simps |
24333 | 271 |
|
26557 | 272 |
|
273 |
subsection {* Truncating binary integers *} |
|
274 |
||
54848 | 275 |
definition bin_sign :: "int \<Rightarrow> int" |
276 |
where |
|
37667 | 277 |
bin_sign_def: "bin_sign k = (if k \<ge> 0 then 0 else - 1)" |
26557 | 278 |
|
279 |
lemma bin_sign_simps [simp]: |
|
45850 | 280 |
"bin_sign 0 = 0" |
46604
9f9e85264e4d
make uses of bin_sign respect int/bin distinction
huffman
parents:
46601
diff
changeset
|
281 |
"bin_sign 1 = 0" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
282 |
"bin_sign (- 1) = - 1" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
283 |
"bin_sign (numeral k) = 0" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
284 |
"bin_sign (- numeral k) = -1" |
26557 | 285 |
"bin_sign (w BIT b) = bin_sign w" |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
286 |
unfolding bin_sign_def Bit_def |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
287 |
by simp_all |
26557 | 288 |
|
24364 | 289 |
lemma bin_sign_rest [simp]: |
37667 | 290 |
"bin_sign (bin_rest w) = bin_sign w" |
26557 | 291 |
by (cases w rule: bin_exhaust) auto |
24364 | 292 |
|
37667 | 293 |
primrec bintrunc :: "nat \<Rightarrow> int \<Rightarrow> int" where |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
294 |
Z : "bintrunc 0 bin = 0" |
37667 | 295 |
| Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)" |
24364 | 296 |
|
37667 | 297 |
primrec sbintrunc :: "nat => int => int" where |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
298 |
Z : "sbintrunc 0 bin = (if bin_last bin then -1 else 0)" |
37667 | 299 |
| Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)" |
300 |
||
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
301 |
lemma sign_bintr: "bin_sign (bintrunc n w) = 0" |
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
302 |
by (induct n arbitrary: w) auto |
24333 | 303 |
|
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
304 |
lemma bintrunc_mod2p: "bintrunc n w = (w mod 2 ^ n)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
305 |
apply (induct n arbitrary: w, clarsimp) |
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
306 |
apply (simp add: bin_last_def bin_rest_def Bit_def zmod_zmult2_eq) |
24333 | 307 |
done |
308 |
||
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
309 |
lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ (Suc n) - 2 ^ n" |
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
310 |
apply (induct n arbitrary: w) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
311 |
apply simp |
30034 | 312 |
apply (subst mod_add_left_eq) |
45529
0e1037d4e049
remove redundant lemmas bin_last_mod and bin_rest_div, use bin_last_def and bin_rest_def instead
huffman
parents:
44939
diff
changeset
|
313 |
apply (simp add: bin_last_def) |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
314 |
apply arith |
54873 | 315 |
apply (simp add: bin_last_def bin_rest_def Bit_def) |
30940
663af91c0720
zmod_zmult_zmult1 now subsumed by mod_mult_mult1
haftmann
parents:
30034
diff
changeset
|
316 |
apply (clarsimp simp: mod_mult_mult1 [symmetric] |
24333 | 317 |
zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2 [THEN sym]]]) |
318 |
apply (rule trans [symmetric, OF _ emep1]) |
|
319 |
apply auto |
|
320 |
apply (auto simp: even_def) |
|
321 |
done |
|
322 |
||
24465 | 323 |
subsection "Simplifications for (s)bintrunc" |
324 |
||
45852 | 325 |
lemma bintrunc_n_0 [simp]: "bintrunc n 0 = 0" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
326 |
by (induct n) auto |
45852 | 327 |
|
45855 | 328 |
lemma sbintrunc_n_0 [simp]: "sbintrunc n 0 = 0" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
329 |
by (induct n) auto |
45855 | 330 |
|
45856 | 331 |
lemma sbintrunc_n_minus1 [simp]: "sbintrunc n -1 = -1" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
332 |
by (induct n) auto |
45856 | 333 |
|
45852 | 334 |
lemma bintrunc_Suc_numeral: |
335 |
"bintrunc (Suc n) 1 = 1" |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
336 |
"bintrunc (Suc n) -1 = bintrunc n -1 BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
337 |
"bintrunc (Suc n) (numeral (Num.Bit0 w)) = bintrunc n (numeral w) BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
338 |
"bintrunc (Suc n) (numeral (Num.Bit1 w)) = bintrunc n (numeral w) BIT True" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
339 |
"bintrunc (Suc n) (- numeral (Num.Bit0 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
340 |
bintrunc n (- numeral w) BIT False" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
341 |
"bintrunc (Suc n) (- numeral (Num.Bit1 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
342 |
bintrunc n (- numeral (w + Num.One)) BIT True" |
45852 | 343 |
by simp_all |
344 |
||
45856 | 345 |
lemma sbintrunc_0_numeral [simp]: |
346 |
"sbintrunc 0 1 = -1" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
347 |
"sbintrunc 0 (numeral (Num.Bit0 w)) = 0" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
348 |
"sbintrunc 0 (numeral (Num.Bit1 w)) = -1" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
349 |
"sbintrunc 0 (- numeral (Num.Bit0 w)) = 0" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
350 |
"sbintrunc 0 (- numeral (Num.Bit1 w)) = -1" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
351 |
by simp_all |
45856 | 352 |
|
45855 | 353 |
lemma sbintrunc_Suc_numeral: |
354 |
"sbintrunc (Suc n) 1 = 1" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
355 |
"sbintrunc (Suc n) (numeral (Num.Bit0 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
356 |
sbintrunc n (numeral w) BIT False" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
357 |
"sbintrunc (Suc n) (numeral (Num.Bit1 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
358 |
sbintrunc n (numeral w) BIT True" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
359 |
"sbintrunc (Suc n) (- numeral (Num.Bit0 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
360 |
sbintrunc n (- numeral w) BIT False" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
361 |
"sbintrunc (Suc n) (- numeral (Num.Bit1 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
362 |
sbintrunc n (- numeral (w + Num.One)) BIT True" |
45855 | 363 |
by simp_all |
364 |
||
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
365 |
lemma bin_sign_lem: "(bin_sign (sbintrunc n bin) = -1) = bin_nth bin n" |
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
366 |
apply (induct n arbitrary: bin) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
367 |
apply (case_tac bin rule: bin_exhaust, case_tac b, auto) |
24333 | 368 |
done |
369 |
||
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
370 |
lemma nth_bintr: "bin_nth (bintrunc m w) n = (n < m & bin_nth w n)" |
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
371 |
apply (induct n arbitrary: w m) |
24333 | 372 |
apply (case_tac m, auto)[1] |
373 |
apply (case_tac m, auto)[1] |
|
374 |
done |
|
375 |
||
376 |
lemma nth_sbintr: |
|
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
377 |
"bin_nth (sbintrunc m w) n = |
24333 | 378 |
(if n < m then bin_nth w n else bin_nth w m)" |
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
379 |
apply (induct n arbitrary: w m) |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
380 |
apply (case_tac m) |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
381 |
apply simp_all |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
382 |
apply (case_tac m) |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
383 |
apply simp_all |
24333 | 384 |
done |
385 |
||
386 |
lemma bin_nth_Bit: |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
387 |
"bin_nth (w BIT b) n = (n = 0 & b | (EX m. n = Suc m & bin_nth w m))" |
24333 | 388 |
by (cases n) auto |
389 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
390 |
lemma bin_nth_Bit0: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
391 |
"bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow> |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
392 |
(\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)" |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
393 |
using bin_nth_Bit [where w="numeral w" and b="False"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
394 |
|
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
395 |
lemma bin_nth_Bit1: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
396 |
"bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow> |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
397 |
n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)" |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
398 |
using bin_nth_Bit [where w="numeral w" and b="True"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
399 |
|
24333 | 400 |
lemma bintrunc_bintrunc_l: |
401 |
"n <= m ==> (bintrunc m (bintrunc n w) = bintrunc n w)" |
|
402 |
by (rule bin_eqI) (auto simp add : nth_bintr) |
|
403 |
||
404 |
lemma sbintrunc_sbintrunc_l: |
|
405 |
"n <= m ==> (sbintrunc m (sbintrunc n w) = sbintrunc n w)" |
|
32439 | 406 |
by (rule bin_eqI) (auto simp: nth_sbintr) |
24333 | 407 |
|
408 |
lemma bintrunc_bintrunc_ge: |
|
409 |
"n <= m ==> (bintrunc n (bintrunc m w) = bintrunc n w)" |
|
410 |
by (rule bin_eqI) (auto simp: nth_bintr) |
|
411 |
||
412 |
lemma bintrunc_bintrunc_min [simp]: |
|
413 |
"bintrunc m (bintrunc n w) = bintrunc (min m n) w" |
|
414 |
apply (rule bin_eqI) |
|
415 |
apply (auto simp: nth_bintr) |
|
416 |
done |
|
417 |
||
418 |
lemma sbintrunc_sbintrunc_min [simp]: |
|
419 |
"sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w" |
|
420 |
apply (rule bin_eqI) |
|
54863
82acc20ded73
prefer more canonical names for lemmas on min/max
haftmann
parents:
54848
diff
changeset
|
421 |
apply (auto simp: nth_sbintr min.absorb1 min.absorb2) |
24333 | 422 |
done |
423 |
||
424 |
lemmas bintrunc_Pls = |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
425 |
bintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
24333 | 426 |
|
427 |
lemmas bintrunc_Min [simp] = |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
428 |
bintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
24333 | 429 |
|
430 |
lemmas bintrunc_BIT [simp] = |
|
46600 | 431 |
bintrunc.Suc [where bin="w BIT b", simplified bin_last_BIT bin_rest_BIT] for w b |
24333 | 432 |
|
433 |
lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT |
|
45852 | 434 |
bintrunc_Suc_numeral |
24333 | 435 |
|
436 |
lemmas sbintrunc_Suc_Pls = |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
437 |
sbintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
24333 | 438 |
|
439 |
lemmas sbintrunc_Suc_Min = |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
440 |
sbintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
24333 | 441 |
|
442 |
lemmas sbintrunc_Suc_BIT [simp] = |
|
46600 | 443 |
sbintrunc.Suc [where bin="w BIT b", simplified bin_last_BIT bin_rest_BIT] for w b |
24333 | 444 |
|
445 |
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min sbintrunc_Suc_BIT |
|
45855 | 446 |
sbintrunc_Suc_numeral |
24333 | 447 |
|
448 |
lemmas sbintrunc_Pls = |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
449 |
sbintrunc.Z [where bin="0", |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
450 |
simplified bin_last_numeral_simps bin_rest_numeral_simps] |
24333 | 451 |
|
452 |
lemmas sbintrunc_Min = |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
453 |
sbintrunc.Z [where bin="-1", |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
454 |
simplified bin_last_numeral_simps bin_rest_numeral_simps] |
24333 | 455 |
|
456 |
lemmas sbintrunc_0_BIT_B0 [simp] = |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
457 |
sbintrunc.Z [where bin="w BIT False", |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
458 |
simplified bin_last_numeral_simps bin_rest_numeral_simps] for w |
24333 | 459 |
|
460 |
lemmas sbintrunc_0_BIT_B1 [simp] = |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
461 |
sbintrunc.Z [where bin="w BIT True", |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
462 |
simplified bin_last_BIT bin_rest_numeral_simps] for w |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
463 |
|
24333 | 464 |
lemmas sbintrunc_0_simps = |
465 |
sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1 |
|
466 |
||
467 |
lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs |
|
468 |
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs |
|
469 |
||
470 |
lemma bintrunc_minus: |
|
471 |
"0 < n ==> bintrunc (Suc (n - 1)) w = bintrunc n w" |
|
472 |
by auto |
|
473 |
||
474 |
lemma sbintrunc_minus: |
|
475 |
"0 < n ==> sbintrunc (Suc (n - 1)) w = sbintrunc n w" |
|
476 |
by auto |
|
477 |
||
478 |
lemmas bintrunc_minus_simps = |
|
45604 | 479 |
bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans]] |
24333 | 480 |
lemmas sbintrunc_minus_simps = |
45604 | 481 |
sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans]] |
24333 | 482 |
|
45604 | 483 |
lemmas thobini1 = arg_cong [where f = "%w. w BIT b"] for b |
24333 | 484 |
|
485 |
lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1] |
|
486 |
lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1] |
|
487 |
||
45604 | 488 |
lemmas bmsts = bintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans]] |
24333 | 489 |
lemmas bintrunc_Pls_minus_I = bmsts(1) |
490 |
lemmas bintrunc_Min_minus_I = bmsts(2) |
|
491 |
lemmas bintrunc_BIT_minus_I = bmsts(3) |
|
492 |
||
493 |
lemma bintrunc_Suc_lem: |
|
494 |
"bintrunc (Suc n) x = y ==> m = Suc n ==> bintrunc m x = y" |
|
495 |
by auto |
|
496 |
||
497 |
lemmas bintrunc_Suc_Ialts = |
|
45604 | 498 |
bintrunc_Min_I [THEN bintrunc_Suc_lem] |
499 |
bintrunc_BIT_I [THEN bintrunc_Suc_lem] |
|
24333 | 500 |
|
501 |
lemmas sbintrunc_BIT_I = trans [OF sbintrunc_Suc_BIT thobini1] |
|
502 |
||
503 |
lemmas sbintrunc_Suc_Is = |
|
45604 | 504 |
sbintrunc_Sucs(1-3) [THEN thobini1 [THEN [2] trans]] |
24333 | 505 |
|
506 |
lemmas sbintrunc_Suc_minus_Is = |
|
45604 | 507 |
sbintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans]] |
24333 | 508 |
|
509 |
lemma sbintrunc_Suc_lem: |
|
510 |
"sbintrunc (Suc n) x = y ==> m = Suc n ==> sbintrunc m x = y" |
|
511 |
by auto |
|
512 |
||
513 |
lemmas sbintrunc_Suc_Ialts = |
|
45604 | 514 |
sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem] |
24333 | 515 |
|
516 |
lemma sbintrunc_bintrunc_lt: |
|
517 |
"m > n ==> sbintrunc n (bintrunc m w) = sbintrunc n w" |
|
518 |
by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr) |
|
519 |
||
520 |
lemma bintrunc_sbintrunc_le: |
|
521 |
"m <= Suc n ==> bintrunc m (sbintrunc n w) = bintrunc m w" |
|
522 |
apply (rule bin_eqI) |
|
523 |
apply (auto simp: nth_sbintr nth_bintr) |
|
524 |
apply (subgoal_tac "x=n", safe, arith+)[1] |
|
525 |
apply (subgoal_tac "x=n", safe, arith+)[1] |
|
526 |
done |
|
527 |
||
528 |
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le] |
|
529 |
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt] |
|
530 |
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l] |
|
531 |
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] |
|
532 |
||
533 |
lemma bintrunc_sbintrunc' [simp]: |
|
534 |
"0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w" |
|
535 |
by (cases n) (auto simp del: bintrunc.Suc) |
|
536 |
||
537 |
lemma sbintrunc_bintrunc' [simp]: |
|
538 |
"0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w" |
|
539 |
by (cases n) (auto simp del: bintrunc.Suc) |
|
540 |
||
541 |
lemma bin_sbin_eq_iff: |
|
542 |
"bintrunc (Suc n) x = bintrunc (Suc n) y <-> |
|
543 |
sbintrunc n x = sbintrunc n y" |
|
544 |
apply (rule iffI) |
|
545 |
apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc]) |
|
546 |
apply simp |
|
547 |
apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc]) |
|
548 |
apply simp |
|
549 |
done |
|
550 |
||
551 |
lemma bin_sbin_eq_iff': |
|
552 |
"0 < n \<Longrightarrow> bintrunc n x = bintrunc n y <-> |
|
553 |
sbintrunc (n - 1) x = sbintrunc (n - 1) y" |
|
554 |
by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc) |
|
555 |
||
556 |
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def] |
|
557 |
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def] |
|
558 |
||
559 |
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l] |
|
560 |
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l] |
|
561 |
||
562 |
(* although bintrunc_minus_simps, if added to default simpset, |
|
563 |
tends to get applied where it's not wanted in developing the theories, |
|
564 |
we get a version for when the word length is given literally *) |
|
565 |
||
566 |
lemmas nat_non0_gr = |
|
45604 | 567 |
trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl] |
24333 | 568 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
569 |
lemma bintrunc_numeral: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
570 |
"bintrunc (numeral k) x = |
47219
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47170
diff
changeset
|
571 |
bintrunc (pred_numeral k) (bin_rest x) BIT bin_last x" |
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47170
diff
changeset
|
572 |
by (simp add: numeral_eq_Suc) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
573 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
574 |
lemma sbintrunc_numeral: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
575 |
"sbintrunc (numeral k) x = |
47219
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47170
diff
changeset
|
576 |
sbintrunc (pred_numeral k) (bin_rest x) BIT bin_last x" |
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47170
diff
changeset
|
577 |
by (simp add: numeral_eq_Suc) |
24333 | 578 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
579 |
lemma bintrunc_numeral_simps [simp]: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
580 |
"bintrunc (numeral k) (numeral (Num.Bit0 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
581 |
bintrunc (pred_numeral k) (numeral w) BIT False" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
582 |
"bintrunc (numeral k) (numeral (Num.Bit1 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
583 |
bintrunc (pred_numeral k) (numeral w) BIT True" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
584 |
"bintrunc (numeral k) (- numeral (Num.Bit0 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
585 |
bintrunc (pred_numeral k) (- numeral w) BIT False" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
586 |
"bintrunc (numeral k) (- numeral (Num.Bit1 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
587 |
bintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT True" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
588 |
"bintrunc (numeral k) 1 = 1" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
589 |
by (simp_all add: bintrunc_numeral) |
24333 | 590 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
591 |
lemma sbintrunc_numeral_simps [simp]: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
592 |
"sbintrunc (numeral k) (numeral (Num.Bit0 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
593 |
sbintrunc (pred_numeral k) (numeral w) BIT False" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
594 |
"sbintrunc (numeral k) (numeral (Num.Bit1 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
595 |
sbintrunc (pred_numeral k) (numeral w) BIT True" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
596 |
"sbintrunc (numeral k) (- numeral (Num.Bit0 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
597 |
sbintrunc (pred_numeral k) (- numeral w) BIT False" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
598 |
"sbintrunc (numeral k) (- numeral (Num.Bit1 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
599 |
sbintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT True" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
600 |
"sbintrunc (numeral k) 1 = 1" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
601 |
by (simp_all add: sbintrunc_numeral) |
24333 | 602 |
|
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
|
603 |
lemma no_bintr_alt1: "bintrunc n = (\<lambda>w. w mod 2 ^ n :: int)" |
24333 | 604 |
by (rule ext) (rule bintrunc_mod2p) |
605 |
||
606 |
lemma range_bintrunc: "range (bintrunc n) = {i. 0 <= i & i < 2 ^ n}" |
|
607 |
apply (unfold no_bintr_alt1) |
|
608 |
apply (auto simp add: image_iff) |
|
609 |
apply (rule exI) |
|
610 |
apply (auto intro: int_mod_lem [THEN iffD1, symmetric]) |
|
611 |
done |
|
612 |
||
613 |
lemma no_sbintr_alt2: |
|
614 |
"sbintrunc n = (%w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)" |
|
615 |
by (rule ext) (simp add : sbintrunc_mod2p) |
|
616 |
||
617 |
lemma range_sbintrunc: |
|
618 |
"range (sbintrunc n) = {i. - (2 ^ n) <= i & i < 2 ^ n}" |
|
619 |
apply (unfold no_sbintr_alt2) |
|
620 |
apply (auto simp add: image_iff eq_diff_eq) |
|
621 |
apply (rule exI) |
|
622 |
apply (auto intro: int_mod_lem [THEN iffD1, symmetric]) |
|
623 |
done |
|
624 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
625 |
lemma sb_inc_lem: |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
626 |
"(a::int) + 2^k < 0 \<Longrightarrow> a + 2^k + 2^(Suc k) <= (a + 2^k) mod 2^(Suc k)" |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
627 |
apply (erule int_mod_ge' [where n = "2 ^ (Suc k)" and b = "a + 2 ^ k", simplified zless2p]) |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
628 |
apply (rule TrueI) |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
629 |
done |
24333 | 630 |
|
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
631 |
lemma sb_inc_lem': |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
632 |
"(a::int) < - (2^k) \<Longrightarrow> a + 2^k + 2^(Suc k) <= (a + 2^k) mod 2^(Suc k)" |
35048 | 633 |
by (rule sb_inc_lem) simp |
24333 | 634 |
|
635 |
lemma sbintrunc_inc: |
|
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
636 |
"x < - (2^n) ==> x + 2^(Suc n) <= sbintrunc n x" |
24333 | 637 |
unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp |
638 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
639 |
lemma sb_dec_lem: |
54230
b1d955791529
more simplification rules on unary and binary minus
haftmann
parents:
53438
diff
changeset
|
640 |
"(0::int) \<le> - (2 ^ k) + a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a" |
b1d955791529
more simplification rules on unary and binary minus
haftmann
parents:
53438
diff
changeset
|
641 |
using int_mod_le'[where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"] by simp |
24333 | 642 |
|
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
643 |
lemma sb_dec_lem': |
54230
b1d955791529
more simplification rules on unary and binary minus
haftmann
parents:
53438
diff
changeset
|
644 |
"(2::int) ^ k \<le> a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a" |
b1d955791529
more simplification rules on unary and binary minus
haftmann
parents:
53438
diff
changeset
|
645 |
by (rule sb_dec_lem) simp |
24333 | 646 |
|
647 |
lemma sbintrunc_dec: |
|
648 |
"x >= (2 ^ n) ==> x - 2 ^ (Suc n) >= sbintrunc n x" |
|
649 |
unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp |
|
650 |
||
47168 | 651 |
lemmas zmod_uminus' = zminus_zmod [where m=c] for c |
47164 | 652 |
lemmas zpower_zmod' = power_mod [where b=c and n=k] for c k |
24333 | 653 |
|
47168 | 654 |
lemmas brdmod1s' [symmetric] = |
655 |
mod_add_left_eq mod_add_right_eq |
|
656 |
mod_diff_left_eq mod_diff_right_eq |
|
657 |
mod_mult_left_eq mod_mult_right_eq |
|
24333 | 658 |
|
659 |
lemmas brdmods' [symmetric] = |
|
660 |
zpower_zmod' [symmetric] |
|
30034 | 661 |
trans [OF mod_add_left_eq mod_add_right_eq] |
47168 | 662 |
trans [OF mod_diff_left_eq mod_diff_right_eq] |
663 |
trans [OF mod_mult_right_eq mod_mult_left_eq] |
|
24333 | 664 |
zmod_uminus' [symmetric] |
30034 | 665 |
mod_add_left_eq [where b = "1::int"] |
47168 | 666 |
mod_diff_left_eq [where b = "1::int"] |
24333 | 667 |
|
668 |
lemmas bintr_arith1s = |
|
46000 | 669 |
brdmod1s' [where c="2^n::int", folded bintrunc_mod2p] for n |
24333 | 670 |
lemmas bintr_ariths = |
46000 | 671 |
brdmods' [where c="2^n::int", folded bintrunc_mod2p] for n |
24333 | 672 |
|
45604 | 673 |
lemmas m2pths = pos_mod_sign pos_mod_bound [OF zless2p] |
24364 | 674 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
675 |
lemma bintr_ge0: "0 \<le> bintrunc n w" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
676 |
by (simp add: bintrunc_mod2p) |
24333 | 677 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
678 |
lemma bintr_lt2p: "bintrunc n w < 2 ^ n" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
679 |
by (simp add: bintrunc_mod2p) |
24333 | 680 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
681 |
lemma bintr_Min: "bintrunc n -1 = 2 ^ n - 1" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
682 |
by (simp add: bintrunc_mod2p m1mod2k) |
24333 | 683 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
684 |
lemma sbintr_ge: "- (2 ^ n) \<le> sbintrunc n w" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
685 |
by (simp add: sbintrunc_mod2p) |
24333 | 686 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
687 |
lemma sbintr_lt: "sbintrunc n w < 2 ^ n" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
688 |
by (simp add: sbintrunc_mod2p) |
24333 | 689 |
|
690 |
lemma sign_Pls_ge_0: |
|
46604
9f9e85264e4d
make uses of bin_sign respect int/bin distinction
huffman
parents:
46601
diff
changeset
|
691 |
"(bin_sign bin = 0) = (bin >= (0 :: int))" |
9f9e85264e4d
make uses of bin_sign respect int/bin distinction
huffman
parents:
46601
diff
changeset
|
692 |
unfolding bin_sign_def by simp |
24333 | 693 |
|
694 |
lemma sign_Min_lt_0: |
|
46604
9f9e85264e4d
make uses of bin_sign respect int/bin distinction
huffman
parents:
46601
diff
changeset
|
695 |
"(bin_sign bin = -1) = (bin < (0 :: int))" |
9f9e85264e4d
make uses of bin_sign respect int/bin distinction
huffman
parents:
46601
diff
changeset
|
696 |
unfolding bin_sign_def by simp |
24333 | 697 |
|
698 |
lemma bin_rest_trunc: |
|
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
699 |
"(bin_rest (bintrunc n bin)) = bintrunc (n - 1) (bin_rest bin)" |
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
700 |
by (induct n arbitrary: bin) auto |
24333 | 701 |
|
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
|
702 |
lemma bin_rest_power_trunc: |
30971 | 703 |
"(bin_rest ^^ k) (bintrunc n bin) = |
704 |
bintrunc (n - k) ((bin_rest ^^ k) bin)" |
|
24333 | 705 |
by (induct k) (auto simp: bin_rest_trunc) |
706 |
||
707 |
lemma bin_rest_trunc_i: |
|
708 |
"bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)" |
|
709 |
by auto |
|
710 |
||
711 |
lemma bin_rest_strunc: |
|
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
712 |
"bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)" |
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
713 |
by (induct n arbitrary: bin) auto |
24333 | 714 |
|
715 |
lemma bintrunc_rest [simp]: |
|
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
716 |
"bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)" |
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
717 |
apply (induct n arbitrary: bin, simp) |
24333 | 718 |
apply (case_tac bin rule: bin_exhaust) |
719 |
apply (auto simp: bintrunc_bintrunc_l) |
|
720 |
done |
|
721 |
||
722 |
lemma sbintrunc_rest [simp]: |
|
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
723 |
"sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)" |
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
724 |
apply (induct n arbitrary: bin, simp) |
24333 | 725 |
apply (case_tac bin rule: bin_exhaust) |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
726 |
apply (auto simp: bintrunc_bintrunc_l split: bool.splits) |
24333 | 727 |
done |
728 |
||
729 |
lemma bintrunc_rest': |
|
730 |
"bintrunc n o bin_rest o bintrunc n = bin_rest o bintrunc n" |
|
731 |
by (rule ext) auto |
|
732 |
||
733 |
lemma sbintrunc_rest' : |
|
734 |
"sbintrunc n o bin_rest o sbintrunc n = bin_rest o sbintrunc n" |
|
735 |
by (rule ext) auto |
|
736 |
||
737 |
lemma rco_lem: |
|
30971 | 738 |
"f o g o f = g o f ==> f o (g o f) ^^ n = g ^^ n o f" |
24333 | 739 |
apply (rule ext) |
740 |
apply (induct_tac n) |
|
741 |
apply (simp_all (no_asm)) |
|
742 |
apply (drule fun_cong) |
|
743 |
apply (unfold o_def) |
|
744 |
apply (erule trans) |
|
745 |
apply simp |
|
746 |
done |
|
747 |
||
748 |
lemmas rco_bintr = bintrunc_rest' |
|
749 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
|
750 |
lemmas rco_sbintr = sbintrunc_rest' |
|
751 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
|
752 |
||
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
|
753 |
|
24364 | 754 |
subsection {* Splitting and concatenation *} |
755 |
||
26557 | 756 |
primrec bin_split :: "nat \<Rightarrow> int \<Rightarrow> int \<times> int" where |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
757 |
Z: "bin_split 0 w = (w, 0)" |
26557 | 758 |
| Suc: "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w) |
759 |
in (w1, w2 BIT bin_last w))" |
|
24364 | 760 |
|
37667 | 761 |
lemma [code]: |
762 |
"bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w) in (w1, w2 BIT bin_last w))" |
|
763 |
"bin_split 0 w = (w, 0)" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
764 |
by simp_all |
37667 | 765 |
|
26557 | 766 |
primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int" where |
767 |
Z: "bin_cat w 0 v = w" |
|
768 |
| Suc: "bin_cat w (Suc n) v = bin_cat w n (bin_rest v) BIT bin_last v" |
|
24364 | 769 |
|
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
|
770 |
end |
24364 | 771 |