author  haftmann 
Fri, 04 Apr 2008 13:40:24 +0200  
changeset 26557  9e7f95903b24 
parent 26514  eff55c0a6d34 
child 26827  a62f8db42f4a 
permissions  rwrr 
24333  1 
(* 
2 
ID: $Id$ 

3 
Author: Jeremy Dawson, NICTA 

4 

5 
contains basic definition to do with integers 

6 
expressed using Pls, Min, BIT and important resulting theorems, 

7 
in particular, bin_rec and related work 

8 
*) 

9 

24350  10 
header {* Basic Definitions for Binary Integers *} 
11 

26557  12 
theory BinGeneral 
13 
imports Num_Lemmas 

24333  14 
begin 
15 

26557  16 
subsection {* Further properties of numerals *} 
17 

18 
datatype bit = B0  B1 

19 

20 
definition 

21 
Bit :: "int \<Rightarrow> bit \<Rightarrow> int" (infixl "BIT" 90) where 

22 
"k BIT b = (case b of B0 \<Rightarrow> 0  B1 \<Rightarrow> 1) + k + k" 

23 

24 
lemma BIT_B0_eq_Bit0 [simp]: "w BIT B0 = Int.Bit0 w" 

25 
unfolding Bit_def Bit0_def by simp 

26 

27 
lemma BIT_B1_eq_Bit1 [simp]: "w BIT B1 = Int.Bit1 w" 

28 
unfolding Bit_def Bit1_def by simp 

29 

30 
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

31 

26557  32 
hide (open) const B0 B1 
33 

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" 
52 
apply (unfold Bit_def) 

53 
apply (simp (no_asm_use) split: bit.split_asm) 

54 
apply simp_all 

55 
apply (drule_tac f=even in arg_cong, clarsimp)+ 

56 
done 

57 

58 
lemmas BIT_eqE [elim!] = BIT_eq [THEN conjE, standard] 

59 

60 
lemma BIT_eq_iff [simp]: 

61 
"(u BIT b = v BIT c) = (u = v \<and> b = c)" 

62 
by (rule iffI) auto 

63 

64 
lemmas BIT_eqI [intro!] = conjI [THEN BIT_eq_iff [THEN iffD2]] 

65 

66 
lemma less_Bits: 

67 
"(v BIT b < w BIT c) = (v < w  v <= w & b = bit.B0 & c = bit.B1)" 

68 
unfolding Bit_def by (auto split: bit.split) 

24333  69 

26557  70 
lemma le_Bits: 
71 
"(v BIT b <= w BIT c) = (v < w  v <= w & (b ~= bit.B1  c ~= bit.B0))" 

72 
unfolding Bit_def by (auto split: bit.split) 

73 

74 
lemma no_no [simp]: "number_of (number_of i) = i" 

75 
unfolding number_of_eq by simp 

76 

77 
lemma Bit_B0: 

78 
"k BIT bit.B0 = k + k" 

79 
by (unfold Bit_def) simp 

80 

81 
lemma Bit_B1: 

82 
"k BIT bit.B1 = k + k + 1" 

83 
by (unfold Bit_def) simp 

84 

85 
lemma Bit_B0_2t: "k BIT bit.B0 = 2 * k" 

86 
by (rule trans, rule Bit_B0) simp 

87 

88 
lemma Bit_B1_2t: "k BIT bit.B1 = 2 * k + 1" 

89 
by (rule trans, rule Bit_B1) simp 

90 

91 
lemma B_mod_2': 

92 
"X = 2 ==> (w BIT bit.B1) mod X = 1 & (w BIT bit.B0) mod X = 0" 

93 
apply (simp (no_asm) only: Bit_B0 Bit_B1) 

94 
apply (simp add: z1pmod2) 

24465  95 
done 
24333  96 

26557  97 
lemma B1_mod_2 [simp]: "(Int.Bit1 w) mod 2 = 1" 
98 
unfolding numeral_simps number_of_is_id by (simp add: z1pmod2) 

24333  99 

26557  100 
lemma B0_mod_2 [simp]: "(Int.Bit0 w) mod 2 = 0" 
101 
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

102 

26557  103 
lemma neB1E [elim!]: 
104 
assumes ne: "y \<noteq> bit.B1" 

105 
assumes y: "y = bit.B0 \<Longrightarrow> P" 

106 
shows "P" 

107 
apply (rule y) 

108 
apply (cases y rule: bit.exhaust, simp) 

109 
apply (simp add: ne) 

110 
done 

26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

111 

26557  112 
lemma bin_ex_rl: "EX w b. w BIT b = bin" 
113 
apply (unfold Bit_def) 

114 
apply (cases "even bin") 

115 
apply (clarsimp simp: even_equiv_def) 

116 
apply (auto simp: odd_equiv_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

117 
done 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

118 

26557  119 
lemma bin_exhaust: 
120 
assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q" 

121 
shows "Q" 

122 
apply (insert bin_ex_rl [of bin]) 

123 
apply (erule exE)+ 

124 
apply (rule Q) 

125 
apply force 

26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

126 
done 
24333  127 

128 

24465  129 
subsection {* Destructors for binary integers *} 
24364  130 

26557  131 
definition bin_rl :: "int \<Rightarrow> int \<times> bit" where 
132 
[code func del]: "bin_rl w = (THE (r, l). w = r BIT l)" 

133 

134 
lemma bin_rl_char: "(bin_rl w = (r, l)) = (r BIT l = w)" 

135 
apply (unfold bin_rl_def) 

136 
apply safe 

137 
apply (cases w rule: bin_exhaust) 

138 
apply auto 

139 
done 

140 

26514  141 
definition 
142 
bin_rest_def [code func del]: "bin_rest w = fst (bin_rl w)" 

143 

144 
definition 

145 
bin_last_def [code func del] : "bin_last w = snd (bin_rl w)" 

24465  146 

26514  147 
primrec bin_nth where 
26557  148 
Z: "bin_nth w 0 = (bin_last w = bit.B1)" 
149 
 Suc: "bin_nth w (Suc n) = bin_nth (bin_rest w) n" 

24364  150 

24465  151 
lemma bin_rl: "bin_rl w = (bin_rest w, bin_last w)" 
152 
unfolding bin_rest_def bin_last_def by auto 

153 

26557  154 
lemma bin_rl_simps [simp]: 
155 
"bin_rl Int.Pls = (Int.Pls, bit.B0)" 

156 
"bin_rl Int.Min = (Int.Min, bit.B1)" 

157 
"bin_rl (Int.Bit0 r) = (r, bit.B0)" 

158 
"bin_rl (Int.Bit1 r) = (r, bit.B1)" 

159 
"bin_rl (r BIT b) = (r, b)" 

160 
unfolding bin_rl_char by simp_all 

161 

162 
declare bin_rl_simps(14) [code func] 

163 

24465  164 
lemmas bin_rl_simp [simp] = iffD1 [OF bin_rl_char bin_rl] 
24364  165 

26557  166 
lemma bin_abs_lem: 
167 
"bin = (w BIT b) ==> ~ bin = Int.Min > ~ bin = Int.Pls > 

168 
nat (abs w) < nat (abs bin)" 

169 
apply (clarsimp simp add: bin_rl_char) 

170 
apply (unfold Pls_def Min_def Bit_def) 

171 
apply (cases b) 

172 
apply (clarsimp, arith) 

173 
apply (clarsimp, arith) 

174 
done 

175 

176 
lemma bin_induct: 

177 
assumes PPls: "P Int.Pls" 

178 
and PMin: "P Int.Min" 

179 
and PBit: "!!bin bit. P bin ==> P (bin BIT bit)" 

180 
shows "P bin" 

181 
apply (rule_tac P=P and a=bin and f1="nat o abs" 

182 
in wf_measure [THEN wf_induct]) 

183 
apply (simp add: measure_def inv_image_def) 

184 
apply (case_tac x rule: bin_exhaust) 

185 
apply (frule bin_abs_lem) 

186 
apply (auto simp add : PPls PMin PBit) 

187 
done 

188 

189 
lemma numeral_induct: 

190 
assumes Pls: "P Int.Pls" 

191 
assumes Min: "P Int.Min" 

192 
assumes Bit0: "\<And>w. \<lbrakk>P w; w \<noteq> Int.Pls\<rbrakk> \<Longrightarrow> P (Int.Bit0 w)" 

193 
assumes Bit1: "\<And>w. \<lbrakk>P w; w \<noteq> Int.Min\<rbrakk> \<Longrightarrow> P (Int.Bit1 w)" 

194 
shows "P x" 

195 
apply (induct x rule: bin_induct) 

196 
apply (rule Pls) 

197 
apply (rule Min) 

198 
apply (case_tac bit) 

199 
apply (case_tac "bin = Int.Pls") 

200 
apply simp 

201 
apply (simp add: Bit0) 

202 
apply (case_tac "bin = Int.Min") 

203 
apply simp 

204 
apply (simp add: Bit1) 

205 
done 

206 

24465  207 
lemma bin_rest_simps [simp]: 
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

208 
"bin_rest Int.Pls = Int.Pls" 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

209 
"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

210 
"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

211 
"bin_rest (Int.Bit1 w) = w" 
26514  212 
"bin_rest (w BIT b) = w" 
24465  213 
unfolding bin_rest_def by auto 
214 

26514  215 
declare bin_rest_simps(14) [code func] 
216 

24465  217 
lemma bin_last_simps [simp]: 
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

218 
"bin_last Int.Pls = bit.B0" 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

219 
"bin_last Int.Min = bit.B1" 
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

220 
"bin_last (Int.Bit0 w) = bit.B0" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

221 
"bin_last (Int.Bit1 w) = bit.B1" 
26514  222 
"bin_last (w BIT b) = b" 
24465  223 
unfolding bin_last_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

224 

26514  225 
declare bin_last_simps(14) [code func] 
226 

24333  227 
lemma bin_r_l_extras [simp]: 
228 
"bin_last 0 = bit.B0" 

229 
"bin_last ( 1) = bit.B1" 

230 
"bin_last 1 = bit.B1" 

231 
"bin_last 1 = bit.B1" 

232 
"bin_rest 1 = 0" 

233 
"bin_rest 0 = 0" 

234 
"bin_rest ( 1) =  1" 

235 
"bin_rest 1 = 1" 

236 
apply (unfold number_of_Min) 

237 
apply (unfold Pls_def [symmetric] Min_def [symmetric]) 

238 
apply (unfold numeral_1_eq_1 [symmetric]) 

239 
apply (auto simp: number_of_eq) 

240 
done 

241 

242 
lemma bin_last_mod: 

243 
"bin_last w = (if w mod 2 = 0 then bit.B0 else bit.B1)" 

24465  244 
apply (case_tac w rule: bin_exhaust) 
245 
apply (case_tac b) 

246 
apply auto 

24333  247 
done 
248 

249 
lemma bin_rest_div: 

250 
"bin_rest w = w div 2" 

24465  251 
apply (case_tac w rule: bin_exhaust) 
252 
apply (rule trans) 

253 
apply clarsimp 

254 
apply (rule refl) 

255 
apply (drule trans) 

256 
apply (rule Bit_def) 

257 
apply (simp add: z1pdiv2 split: bit.split) 

24333  258 
done 
259 

260 
lemma Bit_div2 [simp]: "(w BIT b) div 2 = w" 

261 
unfolding bin_rest_div [symmetric] by auto 

262 

26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

263 
lemma Bit0_div2 [simp]: "(Int.Bit0 w) div 2 = w" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

264 
using Bit_div2 [where b=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

265 

3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

266 
lemma Bit1_div2 [simp]: "(Int.Bit1 w) div 2 = w" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

267 
using Bit_div2 [where b=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

268 

24333  269 
lemma bin_nth_lem [rule_format]: 
270 
"ALL y. bin_nth x = bin_nth y > x = y" 

271 
apply (induct x rule: bin_induct) 

272 
apply safe 

273 
apply (erule rev_mp) 

274 
apply (induct_tac y rule: bin_induct) 

275 
apply safe 

276 
apply (drule_tac x=0 in fun_cong, force) 

277 
apply (erule notE, rule ext, 

278 
drule_tac x="Suc x" in fun_cong, force) 

279 
apply (drule_tac x=0 in fun_cong, force) 

280 
apply (erule rev_mp) 

281 
apply (induct_tac y rule: bin_induct) 

282 
apply safe 

283 
apply (drule_tac x=0 in fun_cong, force) 

284 
apply (erule notE, rule ext, 

285 
drule_tac x="Suc x" in fun_cong, force) 

286 
apply (drule_tac x=0 in fun_cong, force) 

287 
apply (case_tac y rule: bin_exhaust) 

288 
apply clarify 

289 
apply (erule allE) 

290 
apply (erule impE) 

291 
prefer 2 

292 
apply (erule BIT_eqI) 

293 
apply (drule_tac x=0 in fun_cong, force) 

294 
apply (rule ext) 

295 
apply (drule_tac x="Suc ?x" in fun_cong, force) 

296 
done 

297 

298 
lemma bin_nth_eq_iff: "(bin_nth x = bin_nth y) = (x = y)" 

299 
by (auto elim: bin_nth_lem) 

300 

301 
lemmas bin_eqI = ext [THEN bin_nth_eq_iff [THEN iffD1], standard] 

302 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

303 
lemma bin_nth_Pls [simp]: "~ bin_nth Int.Pls n" 
24333  304 
by (induct n) auto 
305 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

306 
lemma bin_nth_Min [simp]: "bin_nth Int.Min n" 
24333  307 
by (induct n) auto 
308 

309 
lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 = (b = bit.B1)" 

310 
by auto 

311 

312 
lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n" 

313 
by auto 

314 

315 
lemma bin_nth_minus [simp]: "0 < n ==> bin_nth (w BIT b) n = bin_nth w (n  1)" 

316 
by (cases n) auto 

317 

26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

318 
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

319 
"0 < n ==> bin_nth (Int.Bit0 w) n = bin_nth w (n  1)" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

320 
using bin_nth_minus [where b=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

321 

3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

322 
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

323 
"0 < n ==> bin_nth (Int.Bit1 w) n = bin_nth w (n  1)" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

324 
using bin_nth_minus [where b=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

325 

24333  326 
lemmas bin_nth_0 = bin_nth.simps(1) 
327 
lemmas bin_nth_Suc = bin_nth.simps(2) 

328 

329 
lemmas bin_nth_simps = 

330 
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

331 
bin_nth_minus_Bit0 bin_nth_minus_Bit1 
24333  332 

26557  333 

334 
subsection {* Recursion combinator for binary integers *} 

335 

336 
lemma brlem: "(bin = Int.Min) = ( bin + Int.pred 0 = 0)" 

337 
unfolding Min_def pred_def by arith 

338 

339 
function 

340 
bin_rec :: "'a \<Rightarrow> 'a \<Rightarrow> (int \<Rightarrow> bit \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> int \<Rightarrow> 'a" 

341 
where 

342 
"bin_rec f1 f2 f3 bin = (if bin = Int.Pls then f1 

343 
else if bin = Int.Min then f2 

344 
else case bin_rl bin of (w, b) => f3 w b (bin_rec f1 f2 f3 w))" 

345 
by pat_completeness auto 

346 

347 
termination 

348 
apply (relation "measure (nat o abs o snd o snd o snd)") 

349 
apply simp 

350 
apply (simp add: Pls_def brlem) 

351 
apply (clarsimp simp: bin_rl_char pred_def) 

352 
apply (frule thin_rl [THEN refl [THEN bin_abs_lem [rule_format]]]) 

353 
apply (unfold Pls_def Min_def number_of_eq) 

354 
prefer 2 

355 
apply (erule asm_rl) 

356 
apply auto 

357 
done 

358 

359 
declare bin_rec.simps [simp del] 

360 

361 
lemma bin_rec_PM: 

362 
"f = bin_rec f1 f2 f3 ==> f Int.Pls = f1 & f Int.Min = f2" 

363 
by (auto simp add: bin_rec.simps) 

364 

365 
lemma bin_rec_Pls: "bin_rec f1 f2 f3 Int.Pls = f1" 

366 
by (simp add: bin_rec.simps) 

367 

368 
lemma bin_rec_Min: "bin_rec f1 f2 f3 Int.Min = f2" 

369 
by (simp add: bin_rec.simps) 

370 

371 
lemma bin_rec_Bit0: 

372 
"f3 Int.Pls bit.B0 f1 = f1 \<Longrightarrow> 

373 
bin_rec f1 f2 f3 (Int.Bit0 w) = f3 w bit.B0 (bin_rec f1 f2 f3 w)" 

374 
apply (simp add: bin_rec_Pls bin_rec.simps [of _ _ _ "Int.Bit0 w"]) 

375 
unfolding Pls_def Min_def Bit0_def 

376 
apply auto 

377 
apply presburger 

378 
apply (simp add: bin_rec.simps) 

379 
done 

380 

381 
lemma bin_rec_Bit1: 

382 
"f3 Int.Min bit.B1 f2 = f2 \<Longrightarrow> 

383 
bin_rec f1 f2 f3 (Int.Bit1 w) = f3 w bit.B1 (bin_rec f1 f2 f3 w)" 

384 
apply (simp add: bin_rec.simps [of _ _ _ "Int.Bit1 w"]) 

385 
unfolding Pls_def Min_def Bit1_def 

386 
apply auto 

387 
apply (cases w) 

388 
apply auto 

389 
apply (simp add: bin_rec.simps) 

390 
unfolding Min_def Pls_def number_of_is_id apply auto 

391 
unfolding Bit0_def apply presburger 

392 
done 

393 

394 
lemma bin_rec_Bit: 

395 
"f = bin_rec f1 f2 f3 ==> f3 Int.Pls bit.B0 f1 = f1 ==> 

396 
f3 Int.Min bit.B1 f2 = f2 ==> f (w BIT b) = f3 w b (f w)" 

397 
by (cases b, simp add: bin_rec_Bit0, simp add: bin_rec_Bit1) 

398 

399 
lemmas bin_rec_simps = refl [THEN bin_rec_Bit] bin_rec_Pls bin_rec_Min 

400 
bin_rec_Bit0 bin_rec_Bit1 

401 

402 

403 
subsection {* Truncating binary integers *} 

404 

405 
definition 

406 
bin_sign_def [code func del] : "bin_sign = bin_rec Int.Pls Int.Min (%w b s. s)" 

407 

408 
lemma bin_sign_simps [simp]: 

409 
"bin_sign Int.Pls = Int.Pls" 

410 
"bin_sign Int.Min = Int.Min" 

411 
"bin_sign (Int.Bit0 w) = bin_sign w" 

412 
"bin_sign (Int.Bit1 w) = bin_sign w" 

413 
"bin_sign (w BIT b) = bin_sign w" 

414 
unfolding bin_sign_def by (auto simp: bin_rec_simps) 

415 

416 
declare bin_sign_simps(14) [code func] 

417 

24364  418 
lemma bin_sign_rest [simp]: 
419 
"bin_sign (bin_rest w) = (bin_sign w)" 

26557  420 
by (cases w rule: bin_exhaust) auto 
24364  421 

422 
consts 

423 
bintrunc :: "nat => int => int" 

424 
primrec 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

425 
Z : "bintrunc 0 bin = Int.Pls" 
24364  426 
Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)" 
427 

428 
consts 

429 
sbintrunc :: "nat => int => int" 

430 
primrec 

431 
Z : "sbintrunc 0 bin = 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

432 
(case bin_last bin of bit.B1 => Int.Min  bit.B0 => Int.Pls)" 
24364  433 
Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)" 
434 

24333  435 
lemma sign_bintr: 
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

436 
"!!w. bin_sign (bintrunc n w) = Int.Pls" 
24333  437 
by (induct n) auto 
438 

439 
lemma bintrunc_mod2p: 

440 
"!!w. bintrunc n w = (w mod 2 ^ n :: int)" 

441 
apply (induct n, clarsimp) 

442 
apply (simp add: bin_last_mod bin_rest_div Bit_def zmod_zmult2_eq 

443 
cong: number_of_False_cong) 

444 
done 

445 

446 
lemma sbintrunc_mod2p: 

447 
"!!w. sbintrunc n w = ((w + 2 ^ n) mod 2 ^ (Suc n)  2 ^ n :: int)" 

448 
apply (induct n) 

449 
apply clarsimp 

450 
apply (subst zmod_zadd_left_eq) 

451 
apply (simp add: bin_last_mod) 

452 
apply (simp add: number_of_eq) 

453 
apply clarsimp 

454 
apply (simp add: bin_last_mod bin_rest_div Bit_def 

455 
cong: number_of_False_cong) 

456 
apply (clarsimp simp: zmod_zmult_zmult1 [symmetric] 

457 
zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2 [THEN sym]]]) 

458 
apply (rule trans [symmetric, OF _ emep1]) 

459 
apply auto 

460 
apply (auto simp: even_def) 

461 
done 

462 

24465  463 
subsection "Simplifications for (s)bintrunc" 
464 

465 
lemma bit_bool: 

466 
"(b = (b' = bit.B1)) = (b' = (if b then bit.B1 else bit.B0))" 

467 
by (cases b') auto 

468 

469 
lemmas bit_bool1 [simp] = refl [THEN bit_bool [THEN iffD1], symmetric] 

24333  470 

471 
lemma bin_sign_lem: 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

472 
"!!bin. (bin_sign (sbintrunc n bin) = Int.Min) = bin_nth bin n" 
24333  473 
apply (induct n) 
474 
apply (case_tac bin rule: bin_exhaust, case_tac b, auto)+ 

475 
done 

476 

477 
lemma nth_bintr: 

478 
"!!w m. bin_nth (bintrunc m w) n = (n < m & bin_nth w n)" 

479 
apply (induct n) 

480 
apply (case_tac m, auto)[1] 

481 
apply (case_tac m, auto)[1] 

482 
done 

483 

484 
lemma nth_sbintr: 

485 
"!!w m. bin_nth (sbintrunc m w) n = 

486 
(if n < m then bin_nth w n else bin_nth w m)" 

487 
apply (induct n) 

488 
apply (case_tac m, simp_all split: bit.splits)[1] 

489 
apply (case_tac m, simp_all split: bit.splits)[1] 

490 
done 

491 

492 
lemma bin_nth_Bit: 

493 
"bin_nth (w BIT b) n = (n = 0 & b = bit.B1  (EX m. n = Suc m & bin_nth w m))" 

494 
by (cases n) auto 

495 

26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

496 
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

497 
"bin_nth (Int.Bit0 w) n = (EX m. n = Suc m & bin_nth w m)" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

498 
using bin_nth_Bit [where b=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

499 

3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

500 
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

501 
"bin_nth (Int.Bit1 w) n = (n = 0  (EX m. n = Suc m & bin_nth w m))" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

502 
using bin_nth_Bit [where b=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

503 

24333  504 
lemma bintrunc_bintrunc_l: 
505 
"n <= m ==> (bintrunc m (bintrunc n w) = bintrunc n w)" 

506 
by (rule bin_eqI) (auto simp add : nth_bintr) 

507 

508 
lemma sbintrunc_sbintrunc_l: 

509 
"n <= m ==> (sbintrunc m (sbintrunc n w) = sbintrunc n w)" 

510 
by (rule bin_eqI) (auto simp: nth_sbintr min_def) 

511 

512 
lemma bintrunc_bintrunc_ge: 

513 
"n <= m ==> (bintrunc n (bintrunc m w) = bintrunc n w)" 

514 
by (rule bin_eqI) (auto simp: nth_bintr) 

515 

516 
lemma bintrunc_bintrunc_min [simp]: 

517 
"bintrunc m (bintrunc n w) = bintrunc (min m n) w" 

518 
apply (unfold min_def) 

519 
apply (rule bin_eqI) 

520 
apply (auto simp: nth_bintr) 

521 
done 

522 

523 
lemma sbintrunc_sbintrunc_min [simp]: 

524 
"sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w" 

525 
apply (unfold min_def) 

526 
apply (rule bin_eqI) 

527 
apply (auto simp: nth_sbintr) 

528 
done 

529 

530 
lemmas bintrunc_Pls = 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

531 
bintrunc.Suc [where bin="Int.Pls", simplified bin_last_simps bin_rest_simps, standard] 
24333  532 

533 
lemmas bintrunc_Min [simp] = 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

534 
bintrunc.Suc [where bin="Int.Min", simplified bin_last_simps bin_rest_simps, standard] 
24333  535 

536 
lemmas bintrunc_BIT [simp] = 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

537 
bintrunc.Suc [where bin="w BIT b", simplified bin_last_simps bin_rest_simps, standard] 
24333  538 

26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

539 
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

540 
"bintrunc (Suc n) (Int.Bit0 w) = Int.Bit0 (bintrunc n w)" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

541 
using bintrunc_BIT [where b=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

542 

3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

543 
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

544 
"bintrunc (Suc n) (Int.Bit1 w) = Int.Bit1 (bintrunc n w)" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

545 
using bintrunc_BIT [where b=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

546 

24333  547 
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

548 
bintrunc_Bit0 bintrunc_Bit1 
24333  549 

550 
lemmas sbintrunc_Suc_Pls = 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

551 
sbintrunc.Suc [where bin="Int.Pls", simplified bin_last_simps bin_rest_simps, standard] 
24333  552 

553 
lemmas sbintrunc_Suc_Min = 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

554 
sbintrunc.Suc [where bin="Int.Min", simplified bin_last_simps bin_rest_simps, standard] 
24333  555 

556 
lemmas sbintrunc_Suc_BIT [simp] = 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

557 
sbintrunc.Suc [where bin="w BIT b", simplified bin_last_simps bin_rest_simps, standard] 
24333  558 

26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

559 
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

560 
"sbintrunc (Suc n) (Int.Bit0 w) = Int.Bit0 (sbintrunc n w)" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

561 
using sbintrunc_Suc_BIT [where b=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

562 

3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

563 
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

564 
"sbintrunc (Suc n) (Int.Bit1 w) = Int.Bit1 (sbintrunc n w)" 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

565 
using sbintrunc_Suc_BIT [where b=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

566 

24333  567 
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

568 
sbintrunc_Suc_Bit0 sbintrunc_Suc_Bit1 
24333  569 

570 
lemmas sbintrunc_Pls = 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

571 
sbintrunc.Z [where bin="Int.Pls", 
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

572 
simplified bin_last_simps bin_rest_simps bit.simps, standard] 
24333  573 

574 
lemmas sbintrunc_Min = 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

575 
sbintrunc.Z [where bin="Int.Min", 
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

576 
simplified bin_last_simps bin_rest_simps bit.simps, standard] 
24333  577 

578 
lemmas sbintrunc_0_BIT_B0 [simp] = 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

579 
sbintrunc.Z [where bin="w BIT bit.B0", 
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

580 
simplified bin_last_simps bin_rest_simps bit.simps, standard] 
24333  581 

582 
lemmas sbintrunc_0_BIT_B1 [simp] = 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

583 
sbintrunc.Z [where bin="w BIT bit.B1", 
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

584 
simplified bin_last_simps bin_rest_simps bit.simps, standard] 
24333  585 

26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

586 
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

587 
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

588 

3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

589 
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

590 
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

591 

24333  592 
lemmas sbintrunc_0_simps = 
593 
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

594 
sbintrunc_0_Bit0 sbintrunc_0_Bit1 
24333  595 

596 
lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs 

597 
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs 

598 

599 
lemma bintrunc_minus: 

600 
"0 < n ==> bintrunc (Suc (n  1)) w = bintrunc n w" 

601 
by auto 

602 

603 
lemma sbintrunc_minus: 

604 
"0 < n ==> sbintrunc (Suc (n  1)) w = sbintrunc n w" 

605 
by auto 

606 

607 
lemmas bintrunc_minus_simps = 

608 
bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans], standard] 

609 
lemmas sbintrunc_minus_simps = 

610 
sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans], standard] 

611 

612 
lemma bintrunc_n_Pls [simp]: 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

613 
"bintrunc n Int.Pls = Int.Pls" 
24333  614 
by (induct n) auto 
615 

616 
lemma sbintrunc_n_PM [simp]: 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

617 
"sbintrunc n Int.Pls = Int.Pls" 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

618 
"sbintrunc n Int.Min = Int.Min" 
24333  619 
by (induct n) auto 
620 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

621 
lemmas thobini1 = arg_cong [where f = "%w. w BIT b", standard] 
24333  622 

623 
lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1] 

624 
lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1] 

625 

26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset

626 
lemmas bmsts = bintrunc_minus_simps(13) [THEN thobini1 [THEN [2] trans], standard] 
24333  627 
lemmas bintrunc_Pls_minus_I = bmsts(1) 
628 
lemmas bintrunc_Min_minus_I = bmsts(2) 

629 
lemmas bintrunc_BIT_minus_I = bmsts(3) 

630 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

631 
lemma bintrunc_0_Min: "bintrunc 0 Int.Min = Int.Pls" 
24333  632 
by auto 
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

633 
lemma bintrunc_0_BIT: "bintrunc 0 (w BIT b) = Int.Pls" 
24333  634 
by auto 
635 

636 
lemma bintrunc_Suc_lem: 

637 
"bintrunc (Suc n) x = y ==> m = Suc n ==> bintrunc m x = y" 

638 
by auto 

639 

640 
lemmas bintrunc_Suc_Ialts = 

26294  641 
bintrunc_Min_I [THEN bintrunc_Suc_lem, standard] 
642 
bintrunc_BIT_I [THEN bintrunc_Suc_lem, standard] 

24333  643 

644 
lemmas sbintrunc_BIT_I = trans [OF sbintrunc_Suc_BIT thobini1] 

645 

646 
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

647 
sbintrunc_Sucs(13) [THEN thobini1 [THEN [2] trans], standard] 
24333  648 

649 
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

650 
sbintrunc_minus_simps(13) [THEN thobini1 [THEN [2] trans], standard] 
24333  651 

652 
lemma sbintrunc_Suc_lem: 

653 
"sbintrunc (Suc n) x = y ==> m = Suc n ==> sbintrunc m x = y" 

654 
by auto 

655 

656 
lemmas sbintrunc_Suc_Ialts = 

657 
sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem, standard] 

658 

659 
lemma sbintrunc_bintrunc_lt: 

660 
"m > n ==> sbintrunc n (bintrunc m w) = sbintrunc n w" 

661 
by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr) 

662 

663 
lemma bintrunc_sbintrunc_le: 

664 
"m <= Suc n ==> bintrunc m (sbintrunc n w) = bintrunc m w" 

665 
apply (rule bin_eqI) 

666 
apply (auto simp: nth_sbintr nth_bintr) 

667 
apply (subgoal_tac "x=n", safe, arith+)[1] 

668 
apply (subgoal_tac "x=n", safe, arith+)[1] 

669 
done 

670 

671 
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le] 

672 
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt] 

673 
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l] 

674 
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] 

675 

676 
lemma bintrunc_sbintrunc' [simp]: 

677 
"0 < n \<Longrightarrow> bintrunc n (sbintrunc (n  1) w) = bintrunc n w" 

678 
by (cases n) (auto simp del: bintrunc.Suc) 

679 

680 
lemma sbintrunc_bintrunc' [simp]: 

681 
"0 < n \<Longrightarrow> sbintrunc (n  1) (bintrunc n w) = sbintrunc (n  1) w" 

682 
by (cases n) (auto simp del: bintrunc.Suc) 

683 

684 
lemma bin_sbin_eq_iff: 

685 
"bintrunc (Suc n) x = bintrunc (Suc n) y <> 

686 
sbintrunc n x = sbintrunc n y" 

687 
apply (rule iffI) 

688 
apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc]) 

689 
apply simp 

690 
apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc]) 

691 
apply simp 

692 
done 

693 

694 
lemma bin_sbin_eq_iff': 

695 
"0 < n \<Longrightarrow> bintrunc n x = bintrunc n y <> 

696 
sbintrunc (n  1) x = sbintrunc (n  1) y" 

697 
by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc) 

698 

699 
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def] 

700 
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def] 

701 

702 
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l] 

703 
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l] 

704 

705 
(* although bintrunc_minus_simps, if added to default simpset, 

706 
tends to get applied where it's not wanted in developing the theories, 

707 
we get a version for when the word length is given literally *) 

708 

709 
lemmas nat_non0_gr = 

25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
24465
diff
changeset

710 
trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl, standard] 
24333  711 

712 
lemmas bintrunc_pred_simps [simp] = 

713 
bintrunc_minus_simps [of "number_of bin", simplified nobm1, standard] 

714 

715 
lemmas sbintrunc_pred_simps [simp] = 

716 
sbintrunc_minus_simps [of "number_of bin", simplified nobm1, standard] 

717 

718 
lemma no_bintr_alt: 

719 
"number_of (bintrunc n w) = w mod 2 ^ n" 

720 
by (simp add: number_of_eq bintrunc_mod2p) 

721 

722 
lemma no_bintr_alt1: "bintrunc n = (%w. w mod 2 ^ n :: int)" 

723 
by (rule ext) (rule bintrunc_mod2p) 

724 

725 
lemma range_bintrunc: "range (bintrunc n) = {i. 0 <= i & i < 2 ^ n}" 

726 
apply (unfold no_bintr_alt1) 

727 
apply (auto simp add: image_iff) 

728 
apply (rule exI) 

729 
apply (auto intro: int_mod_lem [THEN iffD1, symmetric]) 

730 
done 

731 

732 
lemma no_bintr: 

733 
"number_of (bintrunc n w) = (number_of w mod 2 ^ n :: int)" 

734 
by (simp add : bintrunc_mod2p number_of_eq) 

735 

736 
lemma no_sbintr_alt2: 

737 
"sbintrunc n = (%w. (w + 2 ^ n) mod 2 ^ Suc n  2 ^ n :: int)" 

738 
by (rule ext) (simp add : sbintrunc_mod2p) 

739 

740 
lemma no_sbintr: 

741 
"number_of (sbintrunc n w) = 

742 
((number_of w + 2 ^ n) mod 2 ^ Suc n  2 ^ n :: int)" 

743 
by (simp add : no_sbintr_alt2 number_of_eq) 

744 

745 
lemma range_sbintrunc: 

746 
"range (sbintrunc n) = {i.  (2 ^ n) <= i & i < 2 ^ n}" 

747 
apply (unfold no_sbintr_alt2) 

748 
apply (auto simp add: image_iff eq_diff_eq) 

749 
apply (rule exI) 

750 
apply (auto intro: int_mod_lem [THEN iffD1, symmetric]) 

751 
done 

752 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

753 
lemma sb_inc_lem: 
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

754 
"(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

755 
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

756 
apply (rule TrueI) 
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

757 
done 
24333  758 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

759 
lemma sb_inc_lem': 
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

760 
"(a::int) <  (2^k) \<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

761 
by (rule iffD1 [OF less_diff_eq, THEN sb_inc_lem, simplified OrderedGroup.diff_0]) 
24333  762 

763 
lemma sbintrunc_inc: 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

764 
"x <  (2^n) ==> x + 2^(Suc n) <= sbintrunc n x" 
24333  765 
unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp 
766 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

767 
lemma sb_dec_lem: 
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

768 
"(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

769 
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

770 
simplified zless2p, OF _ TrueI, simplified]) 
24333  771 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

772 
lemma sb_dec_lem': 
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

773 
"(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

774 
by (rule iffD1 [OF diff_le_eq', THEN sb_dec_lem, simplified]) 
24333  775 

776 
lemma sbintrunc_dec: 

777 
"x >= (2 ^ n) ==> x  2 ^ (Suc n) >= sbintrunc n x" 

778 
unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp 

779 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

780 
lemmas zmod_uminus' = zmod_uminus [where b="c", standard] 
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

781 
lemmas zpower_zmod' = zpower_zmod [where m="c" and y="k", standard] 
24333  782 

783 
lemmas brdmod1s' [symmetric] = 

784 
zmod_zadd_left_eq zmod_zadd_right_eq 

785 
zmod_zsub_left_eq zmod_zsub_right_eq 

786 
zmod_zmult1_eq zmod_zmult1_eq_rev 

787 

788 
lemmas brdmods' [symmetric] = 

789 
zpower_zmod' [symmetric] 

790 
trans [OF zmod_zadd_left_eq zmod_zadd_right_eq] 

791 
trans [OF zmod_zsub_left_eq zmod_zsub_right_eq] 

792 
trans [OF zmod_zmult1_eq zmod_zmult1_eq_rev] 

793 
zmod_uminus' [symmetric] 

794 
zmod_zadd_left_eq [where b = "1"] 

795 
zmod_zsub_left_eq [where b = "1"] 

796 

797 
lemmas bintr_arith1s = 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

798 
brdmod1s' [where c="2^n", folded pred_def succ_def bintrunc_mod2p, standard] 
24333  799 
lemmas bintr_ariths = 
25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

800 
brdmods' [where c="2^n", folded pred_def succ_def bintrunc_mod2p, standard] 
24333  801 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

802 
lemmas m2pths = pos_mod_sign pos_mod_bound [OF zless2p, standard] 
24364  803 

24333  804 
lemma bintr_ge0: "(0 :: int) <= number_of (bintrunc n w)" 
805 
by (simp add : no_bintr m2pths) 

806 

807 
lemma bintr_lt2p: "number_of (bintrunc n w) < (2 ^ n :: int)" 

808 
by (simp add : no_bintr m2pths) 

809 

810 
lemma bintr_Min: 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

811 
"number_of (bintrunc n Int.Min) = (2 ^ n :: int)  1" 
24333  812 
by (simp add : no_bintr m1mod2k) 
813 

814 
lemma sbintr_ge: "( (2 ^ n) :: int) <= number_of (sbintrunc n w)" 

815 
by (simp add : no_sbintr m2pths) 

816 

817 
lemma sbintr_lt: "number_of (sbintrunc n w) < (2 ^ n :: int)" 

818 
by (simp add : no_sbintr m2pths) 

819 

820 
lemma bintrunc_Suc: 

821 
"bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT bin_last bin" 

822 
by (case_tac bin rule: bin_exhaust) auto 

823 

824 
lemma sign_Pls_ge_0: 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

825 
"(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

826 
by (induct bin rule: numeral_induct) auto 
24333  827 

828 
lemma sign_Min_lt_0: 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

829 
"(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

830 
by (induct bin rule: numeral_induct) auto 
24333  831 

832 
lemmas sign_Min_neg = trans [OF sign_Min_lt_0 neg_def [symmetric]] 

833 

834 
lemma bin_rest_trunc: 

835 
"!!bin. (bin_rest (bintrunc n bin)) = bintrunc (n  1) (bin_rest bin)" 

836 
by (induct n) auto 

837 

838 
lemma bin_rest_power_trunc [rule_format] : 

839 
"(bin_rest ^ k) (bintrunc n bin) = 

840 
bintrunc (n  k) ((bin_rest ^ k) bin)" 

841 
by (induct k) (auto simp: bin_rest_trunc) 

842 

843 
lemma bin_rest_trunc_i: 

844 
"bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)" 

845 
by auto 

846 

847 
lemma bin_rest_strunc: 

848 
"!!bin. bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)" 

849 
by (induct n) auto 

850 

851 
lemma bintrunc_rest [simp]: 

852 
"!!bin. bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)" 

853 
apply (induct n, simp) 

854 
apply (case_tac bin rule: bin_exhaust) 

855 
apply (auto simp: bintrunc_bintrunc_l) 

856 
done 

857 

858 
lemma sbintrunc_rest [simp]: 

859 
"!!bin. sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)" 

860 
apply (induct n, simp) 

861 
apply (case_tac bin rule: bin_exhaust) 

862 
apply (auto simp: bintrunc_bintrunc_l split: bit.splits) 

863 
done 

864 

865 
lemma bintrunc_rest': 

866 
"bintrunc n o bin_rest o bintrunc n = bin_rest o bintrunc n" 

867 
by (rule ext) auto 

868 

869 
lemma sbintrunc_rest' : 

870 
"sbintrunc n o bin_rest o sbintrunc n = bin_rest o sbintrunc n" 

871 
by (rule ext) auto 

872 

873 
lemma rco_lem: 

874 
"f o g o f = g o f ==> f o (g o f) ^ n = g ^ n o f" 

875 
apply (rule ext) 

876 
apply (induct_tac n) 

877 
apply (simp_all (no_asm)) 

878 
apply (drule fun_cong) 

879 
apply (unfold o_def) 

880 
apply (erule trans) 

881 
apply simp 

882 
done 

883 

884 
lemma rco_alt: "(f o g) ^ n o f = f o (g o f) ^ n" 

885 
apply (rule ext) 

886 
apply (induct n) 

887 
apply (simp_all add: o_def) 

888 
done 

889 

890 
lemmas rco_bintr = bintrunc_rest' 

891 
[THEN rco_lem [THEN fun_cong], unfolded o_def] 

892 
lemmas rco_sbintr = sbintrunc_rest' 

893 
[THEN rco_lem [THEN fun_cong], unfolded o_def] 

894 

24364  895 
subsection {* Splitting and concatenation *} 
896 

26557  897 
primrec bin_split :: "nat \<Rightarrow> int \<Rightarrow> int \<times> int" where 
898 
Z: "bin_split 0 w = (w, Int.Pls)" 

899 
 Suc: "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w) 

900 
in (w1, w2 BIT bin_last w))" 

24364  901 

26557  902 
primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int" where 
903 
Z: "bin_cat w 0 v = w" 

904 
 Suc: "bin_cat w (Suc n) v = bin_cat w n (bin_rest v) BIT bin_last v" 

24364  905 

906 
subsection {* Miscellaneous lemmas *} 

907 

908 
lemmas funpow_minus_simp = 

909 
trans [OF gen_minus [where f = "power f"] funpow_Suc, standard] 

910 

911 
lemmas funpow_pred_simp [simp] = 

912 
funpow_minus_simp [of "number_of bin", simplified nobm1, standard] 

913 

914 
lemmas replicate_minus_simp = 

915 
trans [OF gen_minus [where f = "%n. replicate n x"] replicate.replicate_Suc, 

916 
standard] 

917 

918 
lemmas replicate_pred_simp [simp] = 

919 
replicate_minus_simp [of "number_of bin", simplified nobm1, standard] 

920 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

921 
lemmas power_Suc_no [simp] = power_Suc [of "number_of a", standard] 
24364  922 

923 
lemmas power_minus_simp = 

924 
trans [OF gen_minus [where f = "power f"] power_Suc, standard] 

925 

926 
lemmas power_pred_simp = 

927 
power_minus_simp [of "number_of bin", simplified nobm1, standard] 

25349
0d46bea01741
eliminated illegal schematic variables in where/of;
wenzelm
parents:
25134
diff
changeset

928 
lemmas power_pred_simp_no [simp] = power_pred_simp [where f= "number_of f", standard] 
24364  929 

930 
lemma list_exhaust_size_gt0: 

931 
assumes y: "\<And>a list. y = a # list \<Longrightarrow> P" 

932 
shows "0 < length y \<Longrightarrow> P" 

933 
apply (cases y, simp) 

934 
apply (rule y) 

935 
apply fastsimp 

936 
done 

937 

938 
lemma list_exhaust_size_eq0: 

939 
assumes y: "y = [] \<Longrightarrow> P" 

940 
shows "length y = 0 \<Longrightarrow> P" 

941 
apply (cases y) 

942 
apply (rule y, simp) 

943 
apply simp 

944 
done 

945 

946 
lemma size_Cons_lem_eq: 

947 
"y = xa # list ==> size y = Suc k ==> size list = k" 

948 
by auto 

949 

950 
lemma size_Cons_lem_eq_bin: 

25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25349
diff
changeset

951 
"y = xa # list ==> size y = number_of (Int.succ k) ==> 
24364  952 
size list = number_of k" 
953 
by (auto simp: pred_def succ_def split add : split_if_asm) 

954 

24333  955 
lemmas ls_splits = 
956 
prod.split split_split prod.split_asm split_split_asm split_if_asm 

957 

958 
lemma not_B1_is_B0: "y \<noteq> bit.B1 \<Longrightarrow> y = bit.B0" 

959 
by (cases y) auto 

960 

961 
lemma B1_ass_B0: 

962 
assumes y: "y = bit.B0 \<Longrightarrow> y = bit.B1" 

963 
shows "y = bit.B1" 

964 
apply (rule classical) 

965 
apply (drule not_B1_is_B0) 

966 
apply (erule y) 

967 
done 

968 

969 
 "simplifications for specific word lengths" 

970 
lemmas n2s_ths [THEN eq_reflection] = add_2_eq_Suc add_2_eq_Suc' 

971 

972 
lemmas s2n_ths = n2s_ths [symmetric] 

973 

974 
end 