author | huffman |
Thu, 17 Nov 2011 12:29:48 +0100 | |
changeset 45543 | 827bf668c822 |
parent 45529 | 0e1037d4e049 |
child 45604 | 29cf40fe8daf |
permissions | -rw-r--r-- |
24333 | 1 |
(* |
2 |
Author: Jeremy Dawson, NICTA |
|
3 |
||
44939
5930d35c976d
removed unused legacy lemma names, some comment cleanup.
kleing
parents:
44890
diff
changeset
|
4 |
Basic definitions to do with integers, expressed using Pls, Min, BIT. |
24333 | 5 |
*) |
6 |
||
24350 | 7 |
header {* Basic Definitions for Binary Integers *} |
8 |
||
37658 | 9 |
theory Bit_Representation |
41413
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
37667
diff
changeset
|
10 |
imports Misc_Numeric "~~/src/HOL/Library/Bit" |
24333 | 11 |
begin |
12 |
||
26557 | 13 |
subsection {* Further properties of numerals *} |
14 |
||
37667 | 15 |
definition bitval :: "bit \<Rightarrow> 'a\<Colon>zero_neq_one" where |
16 |
"bitval = bit_case 0 1" |
|
17 |
||
18 |
lemma bitval_simps [simp]: |
|
19 |
"bitval 0 = 0" |
|
20 |
"bitval 1 = 1" |
|
21 |
by (simp_all add: bitval_def) |
|
22 |
||
37654 | 23 |
definition Bit :: "int \<Rightarrow> bit \<Rightarrow> int" (infixl "BIT" 90) where |
37667 | 24 |
"k BIT b = bitval b + k + k" |
26557 | 25 |
|
37654 | 26 |
lemma BIT_B0_eq_Bit0 [simp]: "w BIT 0 = Int.Bit0 w" |
26557 | 27 |
unfolding Bit_def Bit0_def by simp |
28 |
||
37654 | 29 |
lemma BIT_B1_eq_Bit1 [simp]: "w BIT 1 = Int.Bit1 w" |
26557 | 30 |
unfolding Bit_def Bit1_def by simp |
31 |
||
32 |
lemmas BIT_simps = BIT_B0_eq_Bit0 BIT_B1_eq_Bit1 |
|
24384
0002537695df
move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents:
24383
diff
changeset
|
33 |
|
26557 | 34 |
lemma Min_ne_Pls [iff]: |
35 |
"Int.Min ~= Int.Pls" |
|
36 |
unfolding Min_def Pls_def by auto |
|
37 |
||
38 |
lemmas Pls_ne_Min [iff] = Min_ne_Pls [symmetric] |
|
39 |
||
40 |
lemmas PlsMin_defs [intro!] = |
|
41 |
Pls_def Min_def Pls_def [symmetric] Min_def [symmetric] |
|
42 |
||
43 |
lemmas PlsMin_simps [simp] = PlsMin_defs [THEN Eq_TrueI] |
|
44 |
||
45 |
lemma number_of_False_cong: |
|
46 |
"False \<Longrightarrow> number_of x = number_of y" |
|
47 |
by (rule FalseE) |
|
48 |
||
49 |
(** ways in which type Bin resembles a datatype **) |
|
24350 | 50 |
|
26557 | 51 |
lemma BIT_eq: "u BIT b = v BIT c ==> u = v & b = c" |
37667 | 52 |
apply (cases b) apply (simp_all) |
53 |
apply (cases c) apply (simp_all) |
|
54 |
apply (cases c) apply (simp_all) |
|
26557 | 55 |
done |
56 |
||
57 |
lemmas BIT_eqE [elim!] = BIT_eq [THEN conjE, standard] |
|
58 |
||
59 |
lemma BIT_eq_iff [simp]: |
|
60 |
"(u BIT b = v BIT c) = (u = v \<and> b = c)" |
|
61 |
by (rule iffI) auto |
|
62 |
||
63 |
lemmas BIT_eqI [intro!] = conjI [THEN BIT_eq_iff [THEN iffD2]] |
|
64 |
||
65 |
lemma less_Bits: |
|
37654 | 66 |
"(v BIT b < w BIT c) = (v < w | v <= w & b = (0::bit) & c = (1::bit))" |
37667 | 67 |
unfolding Bit_def by (auto simp add: bitval_def split: bit.split) |
24333 | 68 |
|
26557 | 69 |
lemma le_Bits: |
37654 | 70 |
"(v BIT b <= w BIT c) = (v < w | v <= w & (b ~= (1::bit) | c ~= (0::bit)))" |
37667 | 71 |
unfolding Bit_def by (auto simp add: bitval_def split: bit.split) |
26557 | 72 |
|
73 |
lemma no_no [simp]: "number_of (number_of i) = i" |
|
74 |
unfolding number_of_eq by simp |
|
75 |
||
76 |
lemma Bit_B0: |
|
37654 | 77 |
"k BIT (0::bit) = k + k" |
26557 | 78 |
by (unfold Bit_def) simp |
79 |
||
80 |
lemma Bit_B1: |
|
37654 | 81 |
"k BIT (1::bit) = k + k + 1" |
26557 | 82 |
by (unfold Bit_def) simp |
83 |
||
37654 | 84 |
lemma Bit_B0_2t: "k BIT (0::bit) = 2 * k" |
26557 | 85 |
by (rule trans, rule Bit_B0) simp |
86 |
||
37654 | 87 |
lemma Bit_B1_2t: "k BIT (1::bit) = 2 * k + 1" |
26557 | 88 |
by (rule trans, rule Bit_B1) simp |
89 |
||
90 |
lemma B_mod_2': |
|
37654 | 91 |
"X = 2 ==> (w BIT (1::bit)) mod X = 1 & (w BIT (0::bit)) mod X = 0" |
26557 | 92 |
apply (simp (no_asm) only: Bit_B0 Bit_B1) |
93 |
apply (simp add: z1pmod2) |
|
24465 | 94 |
done |
24333 | 95 |
|
26557 | 96 |
lemma B1_mod_2 [simp]: "(Int.Bit1 w) mod 2 = 1" |
97 |
unfolding numeral_simps number_of_is_id by (simp add: z1pmod2) |
|
24333 | 98 |
|
26557 | 99 |
lemma B0_mod_2 [simp]: "(Int.Bit0 w) mod 2 = 0" |
100 |
unfolding numeral_simps number_of_is_id by simp |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
101 |
|
26557 | 102 |
lemma neB1E [elim!]: |
37654 | 103 |
assumes ne: "y \<noteq> (1::bit)" |
104 |
assumes y: "y = (0::bit) \<Longrightarrow> P" |
|
26557 | 105 |
shows "P" |
106 |
apply (rule y) |
|
107 |
apply (cases y rule: bit.exhaust, simp) |
|
108 |
apply (simp add: ne) |
|
109 |
done |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
110 |
|
26557 | 111 |
lemma bin_ex_rl: "EX w b. w BIT b = bin" |
112 |
apply (unfold Bit_def) |
|
113 |
apply (cases "even bin") |
|
114 |
apply (clarsimp simp: even_equiv_def) |
|
37667 | 115 |
apply (auto simp: odd_equiv_def bitval_def split: bit.split) |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
116 |
done |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
117 |
|
26557 | 118 |
lemma bin_exhaust: |
119 |
assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q" |
|
120 |
shows "Q" |
|
121 |
apply (insert bin_ex_rl [of bin]) |
|
122 |
apply (erule exE)+ |
|
123 |
apply (rule Q) |
|
124 |
apply force |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
125 |
done |
24333 | 126 |
|
127 |
||
24465 | 128 |
subsection {* Destructors for binary integers *} |
24364 | 129 |
|
37546 | 130 |
definition bin_last :: "int \<Rightarrow> bit" where |
37654 | 131 |
"bin_last w = (if w mod 2 = 0 then (0::bit) else (1::bit))" |
37546 | 132 |
|
133 |
definition bin_rest :: "int \<Rightarrow> int" where |
|
134 |
"bin_rest w = w div 2" |
|
26557 | 135 |
|
37546 | 136 |
definition bin_rl :: "int \<Rightarrow> int \<times> bit" where |
137 |
"bin_rl w = (bin_rest w, bin_last w)" |
|
138 |
||
139 |
lemma bin_rl_char: "bin_rl w = (r, l) \<longleftrightarrow> r BIT l = w" |
|
140 |
apply (cases l) |
|
141 |
apply (auto simp add: bin_rl_def bin_last_def bin_rest_def) |
|
142 |
unfolding Pls_def Min_def Bit0_def Bit1_def number_of_is_id |
|
143 |
apply arith+ |
|
26557 | 144 |
done |
145 |
||
26514 | 146 |
primrec bin_nth where |
37654 | 147 |
Z: "bin_nth w 0 = (bin_last w = (1::bit))" |
26557 | 148 |
| Suc: "bin_nth w (Suc n) = bin_nth (bin_rest w) n" |
24364 | 149 |
|
26557 | 150 |
lemma bin_rl_simps [simp]: |
37654 | 151 |
"bin_rl Int.Pls = (Int.Pls, (0::bit))" |
152 |
"bin_rl Int.Min = (Int.Min, (1::bit))" |
|
153 |
"bin_rl (Int.Bit0 r) = (r, (0::bit))" |
|
154 |
"bin_rl (Int.Bit1 r) = (r, (1::bit))" |
|
26557 | 155 |
"bin_rl (r BIT b) = (r, b)" |
156 |
unfolding bin_rl_char by simp_all |
|
157 |
||
37546 | 158 |
lemma bin_rl_simp [simp]: |
159 |
"bin_rest w BIT bin_last w = w" |
|
160 |
by (simp add: iffD1 [OF bin_rl_char bin_rl_def]) |
|
24364 | 161 |
|
26557 | 162 |
lemma bin_abs_lem: |
163 |
"bin = (w BIT b) ==> ~ bin = Int.Min --> ~ bin = Int.Pls --> |
|
164 |
nat (abs w) < nat (abs bin)" |
|
165 |
apply (clarsimp simp add: bin_rl_char) |
|
166 |
apply (unfold Pls_def Min_def Bit_def) |
|
167 |
apply (cases b) |
|
168 |
apply (clarsimp, arith) |
|
169 |
apply (clarsimp, arith) |
|
170 |
done |
|
171 |
||
172 |
lemma bin_induct: |
|
173 |
assumes PPls: "P Int.Pls" |
|
174 |
and PMin: "P Int.Min" |
|
175 |
and PBit: "!!bin bit. P bin ==> P (bin BIT bit)" |
|
176 |
shows "P bin" |
|
177 |
apply (rule_tac P=P and a=bin and f1="nat o abs" |
|
178 |
in wf_measure [THEN wf_induct]) |
|
179 |
apply (simp add: measure_def inv_image_def) |
|
180 |
apply (case_tac x rule: bin_exhaust) |
|
181 |
apply (frule bin_abs_lem) |
|
182 |
apply (auto simp add : PPls PMin PBit) |
|
183 |
done |
|
184 |
||
185 |
lemma numeral_induct: |
|
186 |
assumes Pls: "P Int.Pls" |
|
187 |
assumes Min: "P Int.Min" |
|
188 |
assumes Bit0: "\<And>w. \<lbrakk>P w; w \<noteq> Int.Pls\<rbrakk> \<Longrightarrow> P (Int.Bit0 w)" |
|
189 |
assumes Bit1: "\<And>w. \<lbrakk>P w; w \<noteq> Int.Min\<rbrakk> \<Longrightarrow> P (Int.Bit1 w)" |
|
190 |
shows "P x" |
|
191 |
apply (induct x rule: bin_induct) |
|
192 |
apply (rule Pls) |
|
193 |
apply (rule Min) |
|
194 |
apply (case_tac bit) |
|
195 |
apply (case_tac "bin = Int.Pls") |
|
196 |
apply simp |
|
197 |
apply (simp add: Bit0) |
|
198 |
apply (case_tac "bin = Int.Min") |
|
199 |
apply simp |
|
200 |
apply (simp add: Bit1) |
|
201 |
done |
|
202 |
||
24465 | 203 |
lemma bin_rest_simps [simp]: |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
204 |
"bin_rest Int.Pls = Int.Pls" |
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
205 |
"bin_rest Int.Min = Int.Min" |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
206 |
"bin_rest (Int.Bit0 w) = w" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
207 |
"bin_rest (Int.Bit1 w) = w" |
26514 | 208 |
"bin_rest (w BIT b) = w" |
37546 | 209 |
using bin_rl_simps bin_rl_def by auto |
24465 | 210 |
|
211 |
lemma bin_last_simps [simp]: |
|
37654 | 212 |
"bin_last Int.Pls = (0::bit)" |
213 |
"bin_last Int.Min = (1::bit)" |
|
214 |
"bin_last (Int.Bit0 w) = (0::bit)" |
|
215 |
"bin_last (Int.Bit1 w) = (1::bit)" |
|
26514 | 216 |
"bin_last (w BIT b) = b" |
37546 | 217 |
using bin_rl_simps bin_rl_def by auto |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
218 |
|
24333 | 219 |
lemma bin_r_l_extras [simp]: |
37654 | 220 |
"bin_last 0 = (0::bit)" |
221 |
"bin_last (- 1) = (1::bit)" |
|
222 |
"bin_last -1 = (1::bit)" |
|
223 |
"bin_last 1 = (1::bit)" |
|
24333 | 224 |
"bin_rest 1 = 0" |
225 |
"bin_rest 0 = 0" |
|
226 |
"bin_rest (- 1) = - 1" |
|
227 |
"bin_rest -1 = -1" |
|
37654 | 228 |
by (simp_all add: bin_last_def bin_rest_def) |
24333 | 229 |
|
230 |
lemma Bit_div2 [simp]: "(w BIT b) div 2 = w" |
|
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
|
231 |
unfolding bin_rest_def [symmetric] by auto |
24333 | 232 |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
233 |
lemma Bit0_div2 [simp]: "(Int.Bit0 w) div 2 = w" |
37654 | 234 |
using Bit_div2 [where b="(0::bit)"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
235 |
|
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
236 |
lemma Bit1_div2 [simp]: "(Int.Bit1 w) div 2 = w" |
37654 | 237 |
using Bit_div2 [where b="(1::bit)"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
238 |
|
24333 | 239 |
lemma bin_nth_lem [rule_format]: |
240 |
"ALL y. bin_nth x = bin_nth y --> x = y" |
|
241 |
apply (induct x rule: bin_induct) |
|
242 |
apply safe |
|
243 |
apply (erule rev_mp) |
|
244 |
apply (induct_tac y rule: bin_induct) |
|
26827
a62f8db42f4a
Deleted subset_antisym in a few proofs, because it is
berghofe
parents:
26557
diff
changeset
|
245 |
apply (safe del: subset_antisym) |
24333 | 246 |
apply (drule_tac x=0 in fun_cong, force) |
247 |
apply (erule notE, rule ext, |
|
248 |
drule_tac x="Suc x" in fun_cong, force) |
|
249 |
apply (drule_tac x=0 in fun_cong, force) |
|
250 |
apply (erule rev_mp) |
|
251 |
apply (induct_tac y rule: bin_induct) |
|
26827
a62f8db42f4a
Deleted subset_antisym in a few proofs, because it is
berghofe
parents:
26557
diff
changeset
|
252 |
apply (safe del: subset_antisym) |
24333 | 253 |
apply (drule_tac x=0 in fun_cong, force) |
254 |
apply (erule notE, rule ext, |
|
255 |
drule_tac x="Suc x" in fun_cong, force) |
|
256 |
apply (drule_tac x=0 in fun_cong, force) |
|
257 |
apply (case_tac y rule: bin_exhaust) |
|
258 |
apply clarify |
|
259 |
apply (erule allE) |
|
260 |
apply (erule impE) |
|
261 |
prefer 2 |
|
262 |
apply (erule BIT_eqI) |
|
263 |
apply (drule_tac x=0 in fun_cong, force) |
|
264 |
apply (rule ext) |
|
265 |
apply (drule_tac x="Suc ?x" in fun_cong, force) |
|
266 |
done |
|
267 |
||
268 |
lemma bin_nth_eq_iff: "(bin_nth x = bin_nth y) = (x = y)" |
|
269 |
by (auto elim: bin_nth_lem) |
|
270 |
||
271 |
lemmas bin_eqI = ext [THEN bin_nth_eq_iff [THEN iffD1], standard] |
|
272 |
||
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
273 |
lemma bin_eq_iff: "x = y \<longleftrightarrow> (\<forall>n. bin_nth x n = bin_nth y n)" |
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
274 |
by (auto intro!: bin_nth_lem del: equalityI) |
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
275 |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
276 |
lemma bin_nth_Pls [simp]: "~ bin_nth Int.Pls n" |
24333 | 277 |
by (induct n) auto |
278 |
||
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
279 |
lemma bin_nth_Min [simp]: "bin_nth Int.Min n" |
24333 | 280 |
by (induct n) auto |
281 |
||
37654 | 282 |
lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 = (b = (1::bit))" |
24333 | 283 |
by auto |
284 |
||
285 |
lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n" |
|
286 |
by auto |
|
287 |
||
288 |
lemma bin_nth_minus [simp]: "0 < n ==> bin_nth (w BIT b) n = bin_nth w (n - 1)" |
|
289 |
by (cases n) auto |
|
290 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
291 |
lemma bin_nth_minus_Bit0 [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
292 |
"0 < n ==> bin_nth (Int.Bit0 w) n = bin_nth w (n - 1)" |
37654 | 293 |
using bin_nth_minus [where b="(0::bit)"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
294 |
|
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
295 |
lemma bin_nth_minus_Bit1 [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
296 |
"0 < n ==> bin_nth (Int.Bit1 w) n = bin_nth w (n - 1)" |
37654 | 297 |
using bin_nth_minus [where b="(1::bit)"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
298 |
|
24333 | 299 |
lemmas bin_nth_0 = bin_nth.simps(1) |
300 |
lemmas bin_nth_Suc = bin_nth.simps(2) |
|
301 |
||
302 |
lemmas bin_nth_simps = |
|
303 |
bin_nth_0 bin_nth_Suc bin_nth_Pls bin_nth_Min bin_nth_minus |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
304 |
bin_nth_minus_Bit0 bin_nth_minus_Bit1 |
24333 | 305 |
|
26557 | 306 |
|
307 |
subsection {* Truncating binary integers *} |
|
308 |
||
309 |
definition |
|
37667 | 310 |
bin_sign_def: "bin_sign k = (if k \<ge> 0 then 0 else - 1)" |
26557 | 311 |
|
312 |
lemma bin_sign_simps [simp]: |
|
313 |
"bin_sign Int.Pls = Int.Pls" |
|
314 |
"bin_sign Int.Min = Int.Min" |
|
315 |
"bin_sign (Int.Bit0 w) = bin_sign w" |
|
316 |
"bin_sign (Int.Bit1 w) = bin_sign w" |
|
317 |
"bin_sign (w BIT b) = bin_sign w" |
|
37667 | 318 |
by (unfold bin_sign_def numeral_simps Bit_def bitval_def) (simp_all split: bit.split) |
26557 | 319 |
|
24364 | 320 |
lemma bin_sign_rest [simp]: |
37667 | 321 |
"bin_sign (bin_rest w) = bin_sign w" |
26557 | 322 |
by (cases w rule: bin_exhaust) auto |
24364 | 323 |
|
37667 | 324 |
primrec bintrunc :: "nat \<Rightarrow> int \<Rightarrow> int" where |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
325 |
Z : "bintrunc 0 bin = Int.Pls" |
37667 | 326 |
| Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)" |
24364 | 327 |
|
37667 | 328 |
primrec sbintrunc :: "nat => int => int" where |
24364 | 329 |
Z : "sbintrunc 0 bin = |
37654 | 330 |
(case bin_last bin of (1::bit) => Int.Min | (0::bit) => Int.Pls)" |
37667 | 331 |
| Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)" |
332 |
||
333 |
lemma [code]: |
|
334 |
"sbintrunc 0 bin = |
|
335 |
(case bin_last bin of (1::bit) => - 1 | (0::bit) => 0)" |
|
336 |
"sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)" |
|
337 |
apply simp_all apply (cases "bin_last bin") apply simp apply (unfold Min_def number_of_is_id) apply simp done |
|
24364 | 338 |
|
24333 | 339 |
lemma sign_bintr: |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
340 |
"!!w. bin_sign (bintrunc n w) = Int.Pls" |
24333 | 341 |
by (induct n) auto |
342 |
||
343 |
lemma bintrunc_mod2p: |
|
344 |
"!!w. bintrunc n w = (w mod 2 ^ n :: int)" |
|
345 |
apply (induct n, clarsimp) |
|
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
|
346 |
apply (simp add: bin_last_def bin_rest_def Bit_def zmod_zmult2_eq |
24333 | 347 |
cong: number_of_False_cong) |
348 |
done |
|
349 |
||
350 |
lemma sbintrunc_mod2p: |
|
351 |
"!!w. sbintrunc n w = ((w + 2 ^ n) mod 2 ^ (Suc n) - 2 ^ n :: int)" |
|
352 |
apply (induct n) |
|
353 |
apply clarsimp |
|
30034 | 354 |
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
|
355 |
apply (simp add: bin_last_def) |
24333 | 356 |
apply (simp add: number_of_eq) |
357 |
apply clarsimp |
|
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
|
358 |
apply (simp add: bin_last_def bin_rest_def Bit_def |
24333 | 359 |
cong: number_of_False_cong) |
30940
663af91c0720
zmod_zmult_zmult1 now subsumed by mod_mult_mult1
haftmann
parents:
30034
diff
changeset
|
360 |
apply (clarsimp simp: mod_mult_mult1 [symmetric] |
24333 | 361 |
zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2 [THEN sym]]]) |
362 |
apply (rule trans [symmetric, OF _ emep1]) |
|
363 |
apply auto |
|
364 |
apply (auto simp: even_def) |
|
365 |
done |
|
366 |
||
24465 | 367 |
subsection "Simplifications for (s)bintrunc" |
368 |
||
369 |
lemma bit_bool: |
|
37654 | 370 |
"(b = (b' = (1::bit))) = (b' = (if b then (1::bit) else (0::bit)))" |
24465 | 371 |
by (cases b') auto |
372 |
||
373 |
lemmas bit_bool1 [simp] = refl [THEN bit_bool [THEN iffD1], symmetric] |
|
24333 | 374 |
|
375 |
lemma bin_sign_lem: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
376 |
"!!bin. (bin_sign (sbintrunc n bin) = Int.Min) = bin_nth bin n" |
24333 | 377 |
apply (induct n) |
378 |
apply (case_tac bin rule: bin_exhaust, case_tac b, auto)+ |
|
379 |
done |
|
380 |
||
381 |
lemma nth_bintr: |
|
382 |
"!!w m. bin_nth (bintrunc m w) n = (n < m & bin_nth w n)" |
|
383 |
apply (induct n) |
|
384 |
apply (case_tac m, auto)[1] |
|
385 |
apply (case_tac m, auto)[1] |
|
386 |
done |
|
387 |
||
388 |
lemma nth_sbintr: |
|
389 |
"!!w m. bin_nth (sbintrunc m w) n = |
|
390 |
(if n < m then bin_nth w n else bin_nth w m)" |
|
391 |
apply (induct n) |
|
392 |
apply (case_tac m, simp_all split: bit.splits)[1] |
|
393 |
apply (case_tac m, simp_all split: bit.splits)[1] |
|
394 |
done |
|
395 |
||
396 |
lemma bin_nth_Bit: |
|
37654 | 397 |
"bin_nth (w BIT b) n = (n = 0 & b = (1::bit) | (EX m. n = Suc m & bin_nth w m))" |
24333 | 398 |
by (cases n) auto |
399 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
400 |
lemma bin_nth_Bit0: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
401 |
"bin_nth (Int.Bit0 w) n = (EX m. n = Suc m & bin_nth w m)" |
37654 | 402 |
using bin_nth_Bit [where b="(0::bit)"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
403 |
|
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
404 |
lemma bin_nth_Bit1: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
405 |
"bin_nth (Int.Bit1 w) n = (n = 0 | (EX m. n = Suc m & bin_nth w m))" |
37654 | 406 |
using bin_nth_Bit [where b="(1::bit)"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
407 |
|
24333 | 408 |
lemma bintrunc_bintrunc_l: |
409 |
"n <= m ==> (bintrunc m (bintrunc n w) = bintrunc n w)" |
|
410 |
by (rule bin_eqI) (auto simp add : nth_bintr) |
|
411 |
||
412 |
lemma sbintrunc_sbintrunc_l: |
|
413 |
"n <= m ==> (sbintrunc m (sbintrunc n w) = sbintrunc n w)" |
|
32439 | 414 |
by (rule bin_eqI) (auto simp: nth_sbintr) |
24333 | 415 |
|
416 |
lemma bintrunc_bintrunc_ge: |
|
417 |
"n <= m ==> (bintrunc n (bintrunc m w) = bintrunc n w)" |
|
418 |
by (rule bin_eqI) (auto simp: nth_bintr) |
|
419 |
||
420 |
lemma bintrunc_bintrunc_min [simp]: |
|
421 |
"bintrunc m (bintrunc n w) = bintrunc (min m n) w" |
|
422 |
apply (rule bin_eqI) |
|
423 |
apply (auto simp: nth_bintr) |
|
424 |
done |
|
425 |
||
426 |
lemma sbintrunc_sbintrunc_min [simp]: |
|
427 |
"sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w" |
|
428 |
apply (rule bin_eqI) |
|
32642
026e7c6a6d08
be more cautious wrt. simp rules: inf_absorb1, inf_absorb2, sup_absorb1, sup_absorb2 are no simp rules by default any longer
haftmann
parents:
32439
diff
changeset
|
429 |
apply (auto simp: nth_sbintr min_max.inf_absorb1 min_max.inf_absorb2) |
24333 | 430 |
done |
431 |
||
432 |
lemmas bintrunc_Pls = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
433 |
bintrunc.Suc [where bin="Int.Pls", simplified bin_last_simps bin_rest_simps, standard] |
24333 | 434 |
|
435 |
lemmas bintrunc_Min [simp] = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
436 |
bintrunc.Suc [where bin="Int.Min", simplified bin_last_simps bin_rest_simps, standard] |
24333 | 437 |
|
438 |
lemmas bintrunc_BIT [simp] = |
|
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
439 |
bintrunc.Suc [where bin="w BIT b", simplified bin_last_simps bin_rest_simps, standard] |
24333 | 440 |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
441 |
lemma bintrunc_Bit0 [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
442 |
"bintrunc (Suc n) (Int.Bit0 w) = Int.Bit0 (bintrunc n w)" |
37654 | 443 |
using bintrunc_BIT [where b="(0::bit)"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
444 |
|
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
445 |
lemma bintrunc_Bit1 [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
446 |
"bintrunc (Suc n) (Int.Bit1 w) = Int.Bit1 (bintrunc n w)" |
37654 | 447 |
using bintrunc_BIT [where b="(1::bit)"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
448 |
|
24333 | 449 |
lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
450 |
bintrunc_Bit0 bintrunc_Bit1 |
24333 | 451 |
|
452 |
lemmas sbintrunc_Suc_Pls = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
453 |
sbintrunc.Suc [where bin="Int.Pls", simplified bin_last_simps bin_rest_simps, standard] |
24333 | 454 |
|
455 |
lemmas sbintrunc_Suc_Min = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
456 |
sbintrunc.Suc [where bin="Int.Min", simplified bin_last_simps bin_rest_simps, standard] |
24333 | 457 |
|
458 |
lemmas sbintrunc_Suc_BIT [simp] = |
|
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
459 |
sbintrunc.Suc [where bin="w BIT b", simplified bin_last_simps bin_rest_simps, standard] |
24333 | 460 |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
461 |
lemma sbintrunc_Suc_Bit0 [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
462 |
"sbintrunc (Suc n) (Int.Bit0 w) = Int.Bit0 (sbintrunc n w)" |
37654 | 463 |
using sbintrunc_Suc_BIT [where b="(0::bit)"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
464 |
|
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
465 |
lemma sbintrunc_Suc_Bit1 [simp]: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
466 |
"sbintrunc (Suc n) (Int.Bit1 w) = Int.Bit1 (sbintrunc n w)" |
37654 | 467 |
using sbintrunc_Suc_BIT [where b="(1::bit)"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
468 |
|
24333 | 469 |
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min sbintrunc_Suc_BIT |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
470 |
sbintrunc_Suc_Bit0 sbintrunc_Suc_Bit1 |
24333 | 471 |
|
472 |
lemmas sbintrunc_Pls = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
473 |
sbintrunc.Z [where bin="Int.Pls", |
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
474 |
simplified bin_last_simps bin_rest_simps bit.simps, standard] |
24333 | 475 |
|
476 |
lemmas sbintrunc_Min = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
477 |
sbintrunc.Z [where bin="Int.Min", |
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
478 |
simplified bin_last_simps bin_rest_simps bit.simps, standard] |
24333 | 479 |
|
480 |
lemmas sbintrunc_0_BIT_B0 [simp] = |
|
37654 | 481 |
sbintrunc.Z [where bin="w BIT (0::bit)", |
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
482 |
simplified bin_last_simps bin_rest_simps bit.simps, standard] |
24333 | 483 |
|
484 |
lemmas sbintrunc_0_BIT_B1 [simp] = |
|
37654 | 485 |
sbintrunc.Z [where bin="w BIT (1::bit)", |
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
486 |
simplified bin_last_simps bin_rest_simps bit.simps, standard] |
24333 | 487 |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
488 |
lemma sbintrunc_0_Bit0 [simp]: "sbintrunc 0 (Int.Bit0 w) = Int.Pls" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
489 |
using sbintrunc_0_BIT_B0 by simp |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
490 |
|
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
491 |
lemma sbintrunc_0_Bit1 [simp]: "sbintrunc 0 (Int.Bit1 w) = Int.Min" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
492 |
using sbintrunc_0_BIT_B1 by simp |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
493 |
|
24333 | 494 |
lemmas sbintrunc_0_simps = |
495 |
sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1 |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
496 |
sbintrunc_0_Bit0 sbintrunc_0_Bit1 |
24333 | 497 |
|
498 |
lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs |
|
499 |
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs |
|
500 |
||
501 |
lemma bintrunc_minus: |
|
502 |
"0 < n ==> bintrunc (Suc (n - 1)) w = bintrunc n w" |
|
503 |
by auto |
|
504 |
||
505 |
lemma sbintrunc_minus: |
|
506 |
"0 < n ==> sbintrunc (Suc (n - 1)) w = sbintrunc n w" |
|
507 |
by auto |
|
508 |
||
509 |
lemmas bintrunc_minus_simps = |
|
510 |
bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans], standard] |
|
511 |
lemmas sbintrunc_minus_simps = |
|
512 |
sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans], standard] |
|
513 |
||
514 |
lemma bintrunc_n_Pls [simp]: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
515 |
"bintrunc n Int.Pls = Int.Pls" |
24333 | 516 |
by (induct n) auto |
517 |
||
518 |
lemma sbintrunc_n_PM [simp]: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
519 |
"sbintrunc n Int.Pls = Int.Pls" |
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
520 |
"sbintrunc n Int.Min = Int.Min" |
24333 | 521 |
by (induct n) auto |
522 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
523 |
lemmas thobini1 = arg_cong [where f = "%w. w BIT b", standard] |
24333 | 524 |
|
525 |
lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1] |
|
526 |
lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1] |
|
527 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
528 |
lemmas bmsts = bintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans], standard] |
24333 | 529 |
lemmas bintrunc_Pls_minus_I = bmsts(1) |
530 |
lemmas bintrunc_Min_minus_I = bmsts(2) |
|
531 |
lemmas bintrunc_BIT_minus_I = bmsts(3) |
|
532 |
||
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
533 |
lemma bintrunc_0_Min: "bintrunc 0 Int.Min = Int.Pls" |
24333 | 534 |
by auto |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
535 |
lemma bintrunc_0_BIT: "bintrunc 0 (w BIT b) = Int.Pls" |
24333 | 536 |
by auto |
537 |
||
538 |
lemma bintrunc_Suc_lem: |
|
539 |
"bintrunc (Suc n) x = y ==> m = Suc n ==> bintrunc m x = y" |
|
540 |
by auto |
|
541 |
||
542 |
lemmas bintrunc_Suc_Ialts = |
|
26294 | 543 |
bintrunc_Min_I [THEN bintrunc_Suc_lem, standard] |
544 |
bintrunc_BIT_I [THEN bintrunc_Suc_lem, standard] |
|
24333 | 545 |
|
546 |
lemmas sbintrunc_BIT_I = trans [OF sbintrunc_Suc_BIT thobini1] |
|
547 |
||
548 |
lemmas sbintrunc_Suc_Is = |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
549 |
sbintrunc_Sucs(1-3) [THEN thobini1 [THEN [2] trans], standard] |
24333 | 550 |
|
551 |
lemmas sbintrunc_Suc_minus_Is = |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
552 |
sbintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans], standard] |
24333 | 553 |
|
554 |
lemma sbintrunc_Suc_lem: |
|
555 |
"sbintrunc (Suc n) x = y ==> m = Suc n ==> sbintrunc m x = y" |
|
556 |
by auto |
|
557 |
||
558 |
lemmas sbintrunc_Suc_Ialts = |
|
559 |
sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem, standard] |
|
560 |
||
561 |
lemma sbintrunc_bintrunc_lt: |
|
562 |
"m > n ==> sbintrunc n (bintrunc m w) = sbintrunc n w" |
|
563 |
by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr) |
|
564 |
||
565 |
lemma bintrunc_sbintrunc_le: |
|
566 |
"m <= Suc n ==> bintrunc m (sbintrunc n w) = bintrunc m w" |
|
567 |
apply (rule bin_eqI) |
|
568 |
apply (auto simp: nth_sbintr nth_bintr) |
|
569 |
apply (subgoal_tac "x=n", safe, arith+)[1] |
|
570 |
apply (subgoal_tac "x=n", safe, arith+)[1] |
|
571 |
done |
|
572 |
||
573 |
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le] |
|
574 |
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt] |
|
575 |
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l] |
|
576 |
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] |
|
577 |
||
578 |
lemma bintrunc_sbintrunc' [simp]: |
|
579 |
"0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w" |
|
580 |
by (cases n) (auto simp del: bintrunc.Suc) |
|
581 |
||
582 |
lemma sbintrunc_bintrunc' [simp]: |
|
583 |
"0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w" |
|
584 |
by (cases n) (auto simp del: bintrunc.Suc) |
|
585 |
||
586 |
lemma bin_sbin_eq_iff: |
|
587 |
"bintrunc (Suc n) x = bintrunc (Suc n) y <-> |
|
588 |
sbintrunc n x = sbintrunc n y" |
|
589 |
apply (rule iffI) |
|
590 |
apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc]) |
|
591 |
apply simp |
|
592 |
apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc]) |
|
593 |
apply simp |
|
594 |
done |
|
595 |
||
596 |
lemma bin_sbin_eq_iff': |
|
597 |
"0 < n \<Longrightarrow> bintrunc n x = bintrunc n y <-> |
|
598 |
sbintrunc (n - 1) x = sbintrunc (n - 1) y" |
|
599 |
by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc) |
|
600 |
||
601 |
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def] |
|
602 |
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def] |
|
603 |
||
604 |
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l] |
|
605 |
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l] |
|
606 |
||
607 |
(* although bintrunc_minus_simps, if added to default simpset, |
|
608 |
tends to get applied where it's not wanted in developing the theories, |
|
609 |
we get a version for when the word length is given literally *) |
|
610 |
||
611 |
lemmas nat_non0_gr = |
|
25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
24465
diff
changeset
|
612 |
trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl, standard] |
24333 | 613 |
|
614 |
lemmas bintrunc_pred_simps [simp] = |
|
615 |
bintrunc_minus_simps [of "number_of bin", simplified nobm1, standard] |
|
616 |
||
617 |
lemmas sbintrunc_pred_simps [simp] = |
|
618 |
sbintrunc_minus_simps [of "number_of bin", simplified nobm1, standard] |
|
619 |
||
620 |
lemma no_bintr_alt: |
|
621 |
"number_of (bintrunc n w) = w mod 2 ^ n" |
|
622 |
by (simp add: number_of_eq bintrunc_mod2p) |
|
623 |
||
624 |
lemma no_bintr_alt1: "bintrunc n = (%w. w mod 2 ^ n :: int)" |
|
625 |
by (rule ext) (rule bintrunc_mod2p) |
|
626 |
||
627 |
lemma range_bintrunc: "range (bintrunc n) = {i. 0 <= i & i < 2 ^ n}" |
|
628 |
apply (unfold no_bintr_alt1) |
|
629 |
apply (auto simp add: image_iff) |
|
630 |
apply (rule exI) |
|
631 |
apply (auto intro: int_mod_lem [THEN iffD1, symmetric]) |
|
632 |
done |
|
633 |
||
634 |
lemma no_bintr: |
|
635 |
"number_of (bintrunc n w) = (number_of w mod 2 ^ n :: int)" |
|
636 |
by (simp add : bintrunc_mod2p number_of_eq) |
|
637 |
||
638 |
lemma no_sbintr_alt2: |
|
639 |
"sbintrunc n = (%w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)" |
|
640 |
by (rule ext) (simp add : sbintrunc_mod2p) |
|
641 |
||
642 |
lemma no_sbintr: |
|
643 |
"number_of (sbintrunc n w) = |
|
644 |
((number_of w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)" |
|
645 |
by (simp add : no_sbintr_alt2 number_of_eq) |
|
646 |
||
647 |
lemma range_sbintrunc: |
|
648 |
"range (sbintrunc n) = {i. - (2 ^ n) <= i & i < 2 ^ n}" |
|
649 |
apply (unfold no_sbintr_alt2) |
|
650 |
apply (auto simp add: image_iff eq_diff_eq) |
|
651 |
apply (rule exI) |
|
652 |
apply (auto intro: int_mod_lem [THEN iffD1, symmetric]) |
|
653 |
done |
|
654 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
655 |
lemma sb_inc_lem: |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
656 |
"(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
|
657 |
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
|
658 |
apply (rule TrueI) |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
659 |
done |
24333 | 660 |
|
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
661 |
lemma sb_inc_lem': |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
662 |
"(a::int) < - (2^k) \<Longrightarrow> a + 2^k + 2^(Suc k) <= (a + 2^k) mod 2^(Suc k)" |
35048 | 663 |
by (rule sb_inc_lem) simp |
24333 | 664 |
|
665 |
lemma sbintrunc_inc: |
|
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
666 |
"x < - (2^n) ==> x + 2^(Suc n) <= sbintrunc n x" |
24333 | 667 |
unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp |
668 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
669 |
lemma sb_dec_lem: |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
670 |
"(0::int) <= - (2^k) + a ==> (a + 2^k) mod (2 * 2 ^ k) <= - (2 ^ k) + a" |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
671 |
by (rule int_mod_le' [where n = "2 ^ (Suc k)" and b = "a + 2 ^ k", |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
672 |
simplified zless2p, OF _ TrueI, simplified]) |
24333 | 673 |
|
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
674 |
lemma sb_dec_lem': |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
675 |
"(2::int) ^ k <= a ==> (a + 2 ^ k) mod (2 * 2 ^ k) <= - (2 ^ k) + a" |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
676 |
by (rule iffD1 [OF diff_le_eq', THEN sb_dec_lem, simplified]) |
24333 | 677 |
|
678 |
lemma sbintrunc_dec: |
|
679 |
"x >= (2 ^ n) ==> x - 2 ^ (Suc n) >= sbintrunc n x" |
|
680 |
unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp |
|
681 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
682 |
lemmas zmod_uminus' = zmod_uminus [where b="c", standard] |
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
683 |
lemmas zpower_zmod' = zpower_zmod [where m="c" and y="k", standard] |
24333 | 684 |
|
685 |
lemmas brdmod1s' [symmetric] = |
|
30034 | 686 |
mod_add_left_eq mod_add_right_eq |
24333 | 687 |
zmod_zsub_left_eq zmod_zsub_right_eq |
688 |
zmod_zmult1_eq zmod_zmult1_eq_rev |
|
689 |
||
690 |
lemmas brdmods' [symmetric] = |
|
691 |
zpower_zmod' [symmetric] |
|
30034 | 692 |
trans [OF mod_add_left_eq mod_add_right_eq] |
24333 | 693 |
trans [OF zmod_zsub_left_eq zmod_zsub_right_eq] |
694 |
trans [OF zmod_zmult1_eq zmod_zmult1_eq_rev] |
|
695 |
zmod_uminus' [symmetric] |
|
30034 | 696 |
mod_add_left_eq [where b = "1::int"] |
24333 | 697 |
zmod_zsub_left_eq [where b = "1"] |
698 |
||
699 |
lemmas bintr_arith1s = |
|
30034 | 700 |
brdmod1s' [where c="2^n::int", folded pred_def succ_def bintrunc_mod2p, standard] |
24333 | 701 |
lemmas bintr_ariths = |
30034 | 702 |
brdmods' [where c="2^n::int", folded pred_def succ_def bintrunc_mod2p, standard] |
24333 | 703 |
|
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
704 |
lemmas m2pths = pos_mod_sign pos_mod_bound [OF zless2p, standard] |
24364 | 705 |
|
24333 | 706 |
lemma bintr_ge0: "(0 :: int) <= number_of (bintrunc n w)" |
707 |
by (simp add : no_bintr m2pths) |
|
708 |
||
709 |
lemma bintr_lt2p: "number_of (bintrunc n w) < (2 ^ n :: int)" |
|
710 |
by (simp add : no_bintr m2pths) |
|
711 |
||
712 |
lemma bintr_Min: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
713 |
"number_of (bintrunc n Int.Min) = (2 ^ n :: int) - 1" |
24333 | 714 |
by (simp add : no_bintr m1mod2k) |
715 |
||
716 |
lemma sbintr_ge: "(- (2 ^ n) :: int) <= number_of (sbintrunc n w)" |
|
717 |
by (simp add : no_sbintr m2pths) |
|
718 |
||
719 |
lemma sbintr_lt: "number_of (sbintrunc n w) < (2 ^ n :: int)" |
|
720 |
by (simp add : no_sbintr m2pths) |
|
721 |
||
722 |
lemma bintrunc_Suc: |
|
723 |
"bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT bin_last bin" |
|
724 |
by (case_tac bin rule: bin_exhaust) auto |
|
725 |
||
726 |
lemma sign_Pls_ge_0: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
727 |
"(bin_sign bin = Int.Pls) = (number_of bin >= (0 :: int))" |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
728 |
by (induct bin rule: numeral_induct) auto |
24333 | 729 |
|
730 |
lemma sign_Min_lt_0: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
731 |
"(bin_sign bin = Int.Min) = (number_of bin < (0 :: int))" |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
732 |
by (induct bin rule: numeral_induct) auto |
24333 | 733 |
|
734 |
lemmas sign_Min_neg = trans [OF sign_Min_lt_0 neg_def [symmetric]] |
|
735 |
||
736 |
lemma bin_rest_trunc: |
|
737 |
"!!bin. (bin_rest (bintrunc n bin)) = bintrunc (n - 1) (bin_rest bin)" |
|
738 |
by (induct n) auto |
|
739 |
||
740 |
lemma bin_rest_power_trunc [rule_format] : |
|
30971 | 741 |
"(bin_rest ^^ k) (bintrunc n bin) = |
742 |
bintrunc (n - k) ((bin_rest ^^ k) bin)" |
|
24333 | 743 |
by (induct k) (auto simp: bin_rest_trunc) |
744 |
||
745 |
lemma bin_rest_trunc_i: |
|
746 |
"bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)" |
|
747 |
by auto |
|
748 |
||
749 |
lemma bin_rest_strunc: |
|
750 |
"!!bin. bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)" |
|
751 |
by (induct n) auto |
|
752 |
||
753 |
lemma bintrunc_rest [simp]: |
|
754 |
"!!bin. bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)" |
|
755 |
apply (induct n, simp) |
|
756 |
apply (case_tac bin rule: bin_exhaust) |
|
757 |
apply (auto simp: bintrunc_bintrunc_l) |
|
758 |
done |
|
759 |
||
760 |
lemma sbintrunc_rest [simp]: |
|
761 |
"!!bin. sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)" |
|
762 |
apply (induct n, simp) |
|
763 |
apply (case_tac bin rule: bin_exhaust) |
|
764 |
apply (auto simp: bintrunc_bintrunc_l split: bit.splits) |
|
765 |
done |
|
766 |
||
767 |
lemma bintrunc_rest': |
|
768 |
"bintrunc n o bin_rest o bintrunc n = bin_rest o bintrunc n" |
|
769 |
by (rule ext) auto |
|
770 |
||
771 |
lemma sbintrunc_rest' : |
|
772 |
"sbintrunc n o bin_rest o sbintrunc n = bin_rest o sbintrunc n" |
|
773 |
by (rule ext) auto |
|
774 |
||
775 |
lemma rco_lem: |
|
30971 | 776 |
"f o g o f = g o f ==> f o (g o f) ^^ n = g ^^ n o f" |
24333 | 777 |
apply (rule ext) |
778 |
apply (induct_tac n) |
|
779 |
apply (simp_all (no_asm)) |
|
780 |
apply (drule fun_cong) |
|
781 |
apply (unfold o_def) |
|
782 |
apply (erule trans) |
|
783 |
apply simp |
|
784 |
done |
|
785 |
||
30971 | 786 |
lemma rco_alt: "(f o g) ^^ n o f = f o (g o f) ^^ n" |
24333 | 787 |
apply (rule ext) |
788 |
apply (induct n) |
|
789 |
apply (simp_all add: o_def) |
|
790 |
done |
|
791 |
||
792 |
lemmas rco_bintr = bintrunc_rest' |
|
793 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
|
794 |
lemmas rco_sbintr = sbintrunc_rest' |
|
795 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
|
796 |
||
24364 | 797 |
subsection {* Splitting and concatenation *} |
798 |
||
26557 | 799 |
primrec bin_split :: "nat \<Rightarrow> int \<Rightarrow> int \<times> int" where |
800 |
Z: "bin_split 0 w = (w, Int.Pls)" |
|
801 |
| Suc: "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w) |
|
802 |
in (w1, w2 BIT bin_last w))" |
|
24364 | 803 |
|
37667 | 804 |
lemma [code]: |
805 |
"bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w) in (w1, w2 BIT bin_last w))" |
|
806 |
"bin_split 0 w = (w, 0)" |
|
807 |
by (simp_all add: Pls_def) |
|
808 |
||
26557 | 809 |
primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int" where |
810 |
Z: "bin_cat w 0 v = w" |
|
811 |
| Suc: "bin_cat w (Suc n) v = bin_cat w n (bin_rest v) BIT bin_last v" |
|
24364 | 812 |
|
813 |
subsection {* Miscellaneous lemmas *} |
|
814 |
||
30952
7ab2716dd93b
power operation on functions with syntax o^; power operation on relations with syntax ^^
haftmann
parents:
30940
diff
changeset
|
815 |
lemma funpow_minus_simp: |
30971 | 816 |
"0 < n \<Longrightarrow> f ^^ n = f \<circ> f ^^ (n - 1)" |
30952
7ab2716dd93b
power operation on functions with syntax o^; power operation on relations with syntax ^^
haftmann
parents:
30940
diff
changeset
|
817 |
by (cases n) simp_all |
24364 | 818 |
|
819 |
lemmas funpow_pred_simp [simp] = |
|
820 |
funpow_minus_simp [of "number_of bin", simplified nobm1, standard] |
|
821 |
||
822 |
lemmas replicate_minus_simp = |
|
823 |
trans [OF gen_minus [where f = "%n. replicate n x"] replicate.replicate_Suc, |
|
824 |
standard] |
|
825 |
||
826 |
lemmas replicate_pred_simp [simp] = |
|
827 |
replicate_minus_simp [of "number_of bin", simplified nobm1, standard] |
|
828 |
||
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
829 |
lemmas power_Suc_no [simp] = power_Suc [of "number_of a", standard] |
24364 | 830 |
|
831 |
lemmas power_minus_simp = |
|
832 |
trans [OF gen_minus [where f = "power f"] power_Suc, standard] |
|
833 |
||
834 |
lemmas power_pred_simp = |
|
835 |
power_minus_simp [of "number_of bin", simplified nobm1, standard] |
|
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset
|
836 |
lemmas power_pred_simp_no [simp] = power_pred_simp [where f= "number_of f", standard] |
24364 | 837 |
|
838 |
lemma list_exhaust_size_gt0: |
|
839 |
assumes y: "\<And>a list. y = a # list \<Longrightarrow> P" |
|
840 |
shows "0 < length y \<Longrightarrow> P" |
|
841 |
apply (cases y, simp) |
|
842 |
apply (rule y) |
|
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
41413
diff
changeset
|
843 |
apply fastforce |
24364 | 844 |
done |
845 |
||
846 |
lemma list_exhaust_size_eq0: |
|
847 |
assumes y: "y = [] \<Longrightarrow> P" |
|
848 |
shows "length y = 0 \<Longrightarrow> P" |
|
849 |
apply (cases y) |
|
850 |
apply (rule y, simp) |
|
851 |
apply simp |
|
852 |
done |
|
853 |
||
854 |
lemma size_Cons_lem_eq: |
|
855 |
"y = xa # list ==> size y = Suc k ==> size list = k" |
|
856 |
by auto |
|
857 |
||
858 |
lemma size_Cons_lem_eq_bin: |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset
|
859 |
"y = xa # list ==> size y = number_of (Int.succ k) ==> |
24364 | 860 |
size list = number_of k" |
861 |
by (auto simp: pred_def succ_def split add : split_if_asm) |
|
862 |
||
44939
5930d35c976d
removed unused legacy lemma names, some comment cleanup.
kleing
parents:
44890
diff
changeset
|
863 |
lemmas ls_splits = prod.split prod.split_asm split_if_asm |
24333 | 864 |
|
37654 | 865 |
lemma not_B1_is_B0: "y \<noteq> (1::bit) \<Longrightarrow> y = (0::bit)" |
24333 | 866 |
by (cases y) auto |
867 |
||
868 |
lemma B1_ass_B0: |
|
37654 | 869 |
assumes y: "y = (0::bit) \<Longrightarrow> y = (1::bit)" |
870 |
shows "y = (1::bit)" |
|
24333 | 871 |
apply (rule classical) |
872 |
apply (drule not_B1_is_B0) |
|
873 |
apply (erule y) |
|
874 |
done |
|
875 |
||
876 |
-- "simplifications for specific word lengths" |
|
877 |
lemmas n2s_ths [THEN eq_reflection] = add_2_eq_Suc add_2_eq_Suc' |
|
878 |
||
879 |
lemmas s2n_ths = n2s_ths [symmetric] |
|
880 |
||
881 |
end |