author  haftmann 
Tue, 30 Nov 2010 20:52:49 +0100  
changeset 40827  abbc05c20e24 
parent 39910  10097e0a9dbd 
child 41060  4199fdcfa3c0 
permissions  rwrr 
29628  1 
(* Title: HOL/Word/Word.thy 
37660  2 
Author: Jeremy Dawson and Gerwin Klein, NICTA 
24333  3 
*) 
4 

37660  5 
header {* A type of finite bit strings *} 
24350  6 

29628  7 
theory Word 
37660  8 
imports Type_Length Misc_Typedef Boolean_Algebra Bool_List_Representation 
9 
uses ("~~/src/HOL/Tools/SMT/smt_word.ML") 

10 
begin 

11 

12 
text {* see @{text "Examples/WordExamples.thy"} for examples *} 

13 

14 
subsection {* Type definition *} 

15 

16 
typedef (open word) 'a word = "{(0::int) ..< 2^len_of TYPE('a::len0)}" 

17 
morphisms uint Abs_word by auto 

18 

19 
definition word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word" where 

20 
 {* representation of words using unsigned or signed bins, 

21 
only difference in these is the type class *} 

22 
"word_of_int w = Abs_word (bintrunc (len_of TYPE ('a)) w)" 

23 

24 
lemma uint_word_of_int [code]: "uint (word_of_int w \<Colon> 'a\<Colon>len0 word) = w mod 2 ^ len_of TYPE('a)" 

25 
by (auto simp add: word_of_int_def bintrunc_mod2p intro: Abs_word_inverse) 

26 

27 
code_datatype word_of_int 

28 

37751  29 
notation fcomp (infixl "\<circ>>" 60) 
30 
notation scomp (infixl "\<circ>\<rightarrow>" 60) 

37660  31 

32 
instantiation word :: ("{len0, typerep}") random 

33 
begin 

34 

35 
definition 

37751  36 
"random_word i = Random.range (max i (2 ^ len_of TYPE('a))) \<circ>\<rightarrow> (\<lambda>k. Pair ( 
37660  37 
let j = word_of_int (Code_Numeral.int_of k) :: 'a word 
38 
in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))" 

39 

40 
instance .. 

41 

42 
end 

43 

37751  44 
no_notation fcomp (infixl "\<circ>>" 60) 
45 
no_notation scomp (infixl "\<circ>\<rightarrow>" 60) 

37660  46 

47 

48 
subsection {* Type conversions and casting *} 

49 

50 
definition sint :: "'a :: len word => int" where 

51 
 {* treats the mostsignificantbit as a sign bit *} 

52 
sint_uint: "sint w = sbintrunc (len_of TYPE ('a)  1) (uint w)" 

53 

54 
definition unat :: "'a :: len0 word => nat" where 

55 
"unat w = nat (uint w)" 

56 

57 
definition uints :: "nat => int set" where 

58 
 "the sets of integers representing the words" 

59 
"uints n = range (bintrunc n)" 

60 

61 
definition sints :: "nat => int set" where 

62 
"sints n = range (sbintrunc (n  1))" 

63 

64 
definition unats :: "nat => nat set" where 

65 
"unats n = {i. i < 2 ^ n}" 

66 

67 
definition norm_sint :: "nat => int => int" where 

68 
"norm_sint n w = (w + 2 ^ (n  1)) mod 2 ^ n  2 ^ (n  1)" 

69 

70 
definition scast :: "'a :: len word => 'b :: len word" where 

71 
 "cast a word to a different length" 

72 
"scast w = word_of_int (sint w)" 

73 

74 
definition ucast :: "'a :: len0 word => 'b :: len0 word" where 

75 
"ucast w = word_of_int (uint w)" 

76 

77 
instantiation word :: (len0) size 

78 
begin 

79 

80 
definition 

81 
word_size: "size (w :: 'a word) = len_of TYPE('a)" 

82 

83 
instance .. 

84 

85 
end 

86 

87 
definition source_size :: "('a :: len0 word => 'b) => nat" where 

88 
 "whether a cast (or other) function is to a longer or shorter length" 

89 
"source_size c = (let arb = undefined ; x = c arb in size arb)" 

90 

91 
definition target_size :: "('a => 'b :: len0 word) => nat" where 

92 
"target_size c = size (c undefined)" 

93 

94 
definition is_up :: "('a :: len0 word => 'b :: len0 word) => bool" where 

95 
"is_up c \<longleftrightarrow> source_size c <= target_size c" 

96 

97 
definition is_down :: "('a :: len0 word => 'b :: len0 word) => bool" where 

98 
"is_down c \<longleftrightarrow> target_size c <= source_size c" 

99 

100 
definition of_bl :: "bool list => 'a :: len0 word" where 

101 
"of_bl bl = word_of_int (bl_to_bin bl)" 

102 

103 
definition to_bl :: "'a :: len0 word => bool list" where 

104 
"to_bl w = bin_to_bl (len_of TYPE ('a)) (uint w)" 

105 

106 
definition word_reverse :: "'a :: len0 word => 'a word" where 

107 
"word_reverse w = of_bl (rev (to_bl w))" 

108 

109 
definition word_int_case :: "(int => 'b) => ('a :: len0 word) => 'b" where 

110 
"word_int_case f w = f (uint w)" 

111 

112 
syntax 

113 
of_int :: "int => 'a" 

114 
translations 

115 
"case x of CONST of_int y => b" == "CONST word_int_case (%y. b) x" 

116 

117 

118 
subsection "Arithmetic operations" 

119 

120 
instantiation word :: (len0) "{number, uminus, minus, plus, one, zero, times, Divides.div, ord, bit}" 

121 
begin 

122 

123 
definition 

124 
word_0_wi: "0 = word_of_int 0" 

125 

126 
definition 

127 
word_1_wi: "1 = word_of_int 1" 

128 

129 
definition 

130 
word_add_def: "a + b = word_of_int (uint a + uint b)" 

131 

132 
definition 

133 
word_sub_wi: "a  b = word_of_int (uint a  uint b)" 

134 

135 
definition 

136 
word_minus_def: " a = word_of_int ( uint a)" 

137 

138 
definition 

139 
word_mult_def: "a * b = word_of_int (uint a * uint b)" 

140 

141 
definition 

142 
word_div_def: "a div b = word_of_int (uint a div uint b)" 

143 

144 
definition 

145 
word_mod_def: "a mod b = word_of_int (uint a mod uint b)" 

146 

147 
definition 

148 
word_number_of_def: "number_of w = word_of_int w" 

149 

150 
definition 

151 
word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b" 

152 

153 
definition 

154 
word_less_def: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> (y \<Colon> 'a word)" 

155 

156 
definition 

157 
word_and_def: 

158 
"(a::'a word) AND b = word_of_int (uint a AND uint b)" 

159 

160 
definition 

161 
word_or_def: 

162 
"(a::'a word) OR b = word_of_int (uint a OR uint b)" 

163 

164 
definition 

165 
word_xor_def: 

166 
"(a::'a word) XOR b = word_of_int (uint a XOR uint b)" 

167 

168 
definition 

169 
word_not_def: 

170 
"NOT (a::'a word) = word_of_int (NOT (uint a))" 

171 

172 
instance .. 

173 

174 
end 

175 

176 
definition 

177 
word_succ :: "'a :: len0 word => 'a word" 

178 
where 

179 
"word_succ a = word_of_int (Int.succ (uint a))" 

180 

181 
definition 

182 
word_pred :: "'a :: len0 word => 'a word" 

183 
where 

184 
"word_pred a = word_of_int (Int.pred (uint a))" 

185 

186 
definition udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50) where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

187 
"a udvd b = (EX n>=0. uint b = n * uint a)" 
37660  188 

189 
definition word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50) where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

190 
"a <=s b = (sint a <= sint b)" 
37660  191 

192 
definition word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50) where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

193 
"(x <s y) = (x <=s y & x ~= y)" 
37660  194 

195 

196 

197 
subsection "Bitwise operations" 

198 

199 
instantiation word :: (len0) bits 

200 
begin 

201 

202 
definition 

203 
word_test_bit_def: "test_bit a = bin_nth (uint a)" 

204 

205 
definition 

206 
word_set_bit_def: "set_bit a n x = 

207 
word_of_int (bin_sc n (If x 1 0) (uint a))" 

208 

209 
definition 

210 
word_set_bits_def: "(BITS n. f n) = of_bl (bl_of_nth (len_of TYPE ('a)) f)" 

211 

212 
definition 

213 
word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a) = 1" 

214 

215 
definition shiftl1 :: "'a word \<Rightarrow> 'a word" where 

216 
"shiftl1 w = word_of_int (uint w BIT 0)" 

217 

218 
definition shiftr1 :: "'a word \<Rightarrow> 'a word" where 

219 
 "shift right as unsigned or as signed, ie logical or arithmetic" 

220 
"shiftr1 w = word_of_int (bin_rest (uint w))" 

221 

222 
definition 

223 
shiftl_def: "w << n = (shiftl1 ^^ n) w" 

224 

225 
definition 

226 
shiftr_def: "w >> n = (shiftr1 ^^ n) w" 

227 

228 
instance .. 

229 

230 
end 

231 

232 
instantiation word :: (len) bitss 

233 
begin 

234 

235 
definition 

236 
word_msb_def: 

237 
"msb a \<longleftrightarrow> bin_sign (sint a) = Int.Min" 

238 

239 
instance .. 

240 

241 
end 

242 

37667  243 
lemma [code]: 
244 
"msb a \<longleftrightarrow> bin_sign (sint a) = ( 1 :: int)" 

245 
by (simp only: word_msb_def Min_def) 

246 

37660  247 
definition setBit :: "'a :: len0 word => nat => 'a word" where 
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

248 
"setBit w n = set_bit w n True" 
37660  249 

250 
definition clearBit :: "'a :: len0 word => nat => 'a word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

251 
"clearBit w n = set_bit w n False" 
37660  252 

253 

254 
subsection "Shift operations" 

255 

256 
definition sshiftr1 :: "'a :: len word => 'a word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

257 
"sshiftr1 w = word_of_int (bin_rest (sint w))" 
37660  258 

259 
definition bshiftr1 :: "bool => 'a :: len word => 'a word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

260 
"bshiftr1 b w = of_bl (b # butlast (to_bl w))" 
37660  261 

262 
definition sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55) where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

263 
"w >>> n = (sshiftr1 ^^ n) w" 
37660  264 

265 
definition mask :: "nat => 'a::len word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

266 
"mask n = (1 << n)  1" 
37660  267 

268 
definition revcast :: "'a :: len0 word => 'b :: len0 word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

269 
"revcast w = of_bl (takefill False (len_of TYPE('b)) (to_bl w))" 
37660  270 

271 
definition slice1 :: "nat => 'a :: len0 word => 'b :: len0 word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

272 
"slice1 n w = of_bl (takefill False n (to_bl w))" 
37660  273 

274 
definition slice :: "nat => 'a :: len0 word => 'b :: len0 word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

275 
"slice n w = slice1 (size w  n) w" 
37660  276 

277 

278 
subsection "Rotation" 

279 

280 
definition rotater1 :: "'a list => 'a list" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

281 
"rotater1 ys = 
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

282 
(case ys of [] => []  x # xs => last ys # butlast ys)" 
37660  283 

284 
definition rotater :: "nat => 'a list => 'a list" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

285 
"rotater n = rotater1 ^^ n" 
37660  286 

287 
definition word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

288 
"word_rotr n w = of_bl (rotater n (to_bl w))" 
37660  289 

290 
definition word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

291 
"word_rotl n w = of_bl (rotate n (to_bl w))" 
37660  292 

293 
definition word_roti :: "int => 'a :: len0 word => 'a :: len0 word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

294 
"word_roti i w = (if i >= 0 then word_rotr (nat i) w 
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

295 
else word_rotl (nat ( i)) w)" 
37660  296 

297 

298 
subsection "Split and cat operations" 

299 

300 
definition word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

301 
"word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))" 
37660  302 

303 
definition word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

304 
"word_split a = 
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

305 
(case bin_split (len_of TYPE ('c)) (uint a) of 
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

306 
(u, v) => (word_of_int u, word_of_int v))" 
37660  307 

308 
definition word_rcat :: "'a :: len0 word list => 'b :: len0 word" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

309 
"word_rcat ws = 
37660  310 
word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))" 
311 

312 
definition word_rsplit :: "'a :: len0 word => 'b :: len word list" where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

313 
"word_rsplit w = 
37660  314 
map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))" 
315 

316 
definition max_word :: "'a::len word"  "Largest representable machine integer." where 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

317 
"max_word = word_of_int (2 ^ len_of TYPE('a)  1)" 
37660  318 

319 
primrec of_bool :: "bool \<Rightarrow> 'a::len word" where 

320 
"of_bool False = 0" 

321 
 "of_bool True = 1" 

322 

323 

324 
lemmas of_nth_def = word_set_bits_def 

325 

326 
lemmas word_size_gt_0 [iff] = 

327 
xtr1 [OF word_size len_gt_0, standard] 

328 
lemmas lens_gt_0 = word_size_gt_0 len_gt_0 

329 
lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard] 

330 

331 
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}" 

332 
by (simp add: uints_def range_bintrunc) 

333 

334 
lemma sints_num: "sints n = {i.  (2 ^ (n  1)) \<le> i \<and> i < 2 ^ (n  1)}" 

335 
by (simp add: sints_def range_sbintrunc) 

336 

337 
lemmas atLeastLessThan_alt = atLeastLessThan_def [unfolded 

338 
atLeast_def lessThan_def Collect_conj_eq [symmetric]] 

339 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

340 
lemma mod_in_reps: "m > 0 \<Longrightarrow> y mod m : {0::int ..< m}" 
37660  341 
unfolding atLeastLessThan_alt by auto 
342 

343 
lemma 

344 
uint_0:"0 <= uint x" and 

345 
uint_lt: "uint (x::'a::len0 word) < 2 ^ len_of TYPE('a)" 

346 
by (auto simp: uint [simplified]) 

347 

348 
lemma uint_mod_same: 

349 
"uint x mod 2 ^ len_of TYPE('a) = uint (x::'a::len0 word)" 

350 
by (simp add: int_mod_eq uint_lt uint_0) 

351 

352 
lemma td_ext_uint: 

353 
"td_ext (uint :: 'a word => int) word_of_int (uints (len_of TYPE('a::len0))) 

354 
(%w::int. w mod 2 ^ len_of TYPE('a))" 

355 
apply (unfold td_ext_def') 

356 
apply (simp add: uints_num word_of_int_def bintrunc_mod2p) 

357 
apply (simp add: uint_mod_same uint_0 uint_lt 

358 
word.uint_inverse word.Abs_word_inverse int_mod_lem) 

359 
done 

360 

361 
lemmas int_word_uint = td_ext_uint [THEN td_ext.eq_norm, standard] 

362 

363 
interpretation word_uint: 

364 
td_ext "uint::'a::len0 word \<Rightarrow> int" 

365 
word_of_int 

366 
"uints (len_of TYPE('a::len0))" 

367 
"\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)" 

368 
by (rule td_ext_uint) 

369 

370 
lemmas td_uint = word_uint.td_thm 

371 

372 
lemmas td_ext_ubin = td_ext_uint 

373 
[simplified len_gt_0 no_bintr_alt1 [symmetric]] 

374 

375 
interpretation word_ubin: 

376 
td_ext "uint::'a::len0 word \<Rightarrow> int" 

377 
word_of_int 

378 
"uints (len_of TYPE('a::len0))" 

379 
"bintrunc (len_of TYPE('a::len0))" 

380 
by (rule td_ext_ubin) 

381 

382 
lemma sint_sbintrunc': 

383 
"sint (word_of_int bin :: 'a word) = 

384 
(sbintrunc (len_of TYPE ('a :: len)  1) bin)" 

385 
unfolding sint_uint 

386 
by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt) 

387 

388 
lemma uint_sint: 

389 
"uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a :: len word))" 

390 
unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le) 

391 

392 
lemma bintr_uint': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

393 
"n >= size w \<Longrightarrow> bintrunc n (uint w) = uint w" 
37660  394 
apply (unfold word_size) 
395 
apply (subst word_ubin.norm_Rep [symmetric]) 

396 
apply (simp only: bintrunc_bintrunc_min word_size) 

397 
apply (simp add: min_max.inf_absorb2) 

398 
done 

399 

400 
lemma wi_bintr': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

401 
"wb = word_of_int bin \<Longrightarrow> n >= size wb \<Longrightarrow> 
37660  402 
word_of_int (bintrunc n bin) = wb" 
403 
unfolding word_size 

404 
by (clarsimp simp add: word_ubin.norm_eq_iff [symmetric] min_max.inf_absorb1) 

405 

406 
lemmas bintr_uint = bintr_uint' [unfolded word_size] 

407 
lemmas wi_bintr = wi_bintr' [unfolded word_size] 

408 

409 
lemma td_ext_sbin: 

410 
"td_ext (sint :: 'a word => int) word_of_int (sints (len_of TYPE('a::len))) 

411 
(sbintrunc (len_of TYPE('a)  1))" 

412 
apply (unfold td_ext_def' sint_uint) 

413 
apply (simp add : word_ubin.eq_norm) 

414 
apply (cases "len_of TYPE('a)") 

415 
apply (auto simp add : sints_def) 

416 
apply (rule sym [THEN trans]) 

417 
apply (rule word_ubin.Abs_norm) 

418 
apply (simp only: bintrunc_sbintrunc) 

419 
apply (drule sym) 

420 
apply simp 

421 
done 

422 

423 
lemmas td_ext_sint = td_ext_sbin 

424 
[simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]] 

425 

426 
(* We do sint before sbin, before sint is the user version 

427 
and interpretations do not produce thm duplicates. I.e. 

428 
we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD, 

429 
because the latter is the same thm as the former *) 

430 
interpretation word_sint: 

431 
td_ext "sint ::'a::len word => int" 

432 
word_of_int 

433 
"sints (len_of TYPE('a::len))" 

434 
"%w. (w + 2^(len_of TYPE('a::len)  1)) mod 2^len_of TYPE('a::len)  

435 
2 ^ (len_of TYPE('a::len)  1)" 

436 
by (rule td_ext_sint) 

437 

438 
interpretation word_sbin: 

439 
td_ext "sint ::'a::len word => int" 

440 
word_of_int 

441 
"sints (len_of TYPE('a::len))" 

442 
"sbintrunc (len_of TYPE('a::len)  1)" 

443 
by (rule td_ext_sbin) 

444 

445 
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm, standard] 

446 

447 
lemmas td_sint = word_sint.td 

448 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

449 
lemma word_number_of_alt [code_unfold_post]: 
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

450 
"number_of b = word_of_int (number_of b)" 
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

451 
by (simp add: number_of_eq word_number_of_def) 
37660  452 

453 
lemma word_no_wi: "number_of = word_of_int" 

454 
by (auto simp: word_number_of_def intro: ext) 

455 

456 
lemma to_bl_def': 

457 
"(to_bl :: 'a :: len0 word => bool list) = 

458 
bin_to_bl (len_of TYPE('a)) o uint" 

459 
by (auto simp: to_bl_def intro: ext) 

460 

461 
lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of w", standard] 

462 

463 
lemmas uints_mod = uints_def [unfolded no_bintr_alt1] 

464 

465 
lemma uint_bintrunc: "uint (number_of bin :: 'a word) = 

466 
number_of (bintrunc (len_of TYPE ('a :: len0)) bin)" 

467 
unfolding word_number_of_def number_of_eq 

468 
by (auto intro: word_ubin.eq_norm) 

469 

470 
lemma sint_sbintrunc: "sint (number_of bin :: 'a word) = 

471 
number_of (sbintrunc (len_of TYPE ('a :: len)  1) bin)" 

472 
unfolding word_number_of_def number_of_eq 

473 
by (subst word_sbin.eq_norm) simp 

474 

475 
lemma unat_bintrunc: 

476 
"unat (number_of bin :: 'a :: len0 word) = 

477 
number_of (bintrunc (len_of TYPE('a)) bin)" 

478 
unfolding unat_def nat_number_of_def 

479 
by (simp only: uint_bintrunc) 

480 

481 
(* WARNING  these may not always be helpful *) 

482 
declare 

483 
uint_bintrunc [simp] 

484 
sint_sbintrunc [simp] 

485 
unat_bintrunc [simp] 

486 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

487 
lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 \<Longrightarrow> v = w" 
37660  488 
apply (unfold word_size) 
489 
apply (rule word_uint.Rep_eqD) 

490 
apply (rule box_equals) 

491 
defer 

492 
apply (rule word_ubin.norm_Rep)+ 

493 
apply simp 

494 
done 

495 

496 
lemmas uint_lem = word_uint.Rep [unfolded uints_num mem_Collect_eq] 

497 
lemmas sint_lem = word_sint.Rep [unfolded sints_num mem_Collect_eq] 

498 
lemmas uint_ge_0 [iff] = uint_lem [THEN conjunct1, standard] 

499 
lemmas uint_lt2p [iff] = uint_lem [THEN conjunct2, standard] 

500 
lemmas sint_ge = sint_lem [THEN conjunct1, standard] 

501 
lemmas sint_lt = sint_lem [THEN conjunct2, standard] 

502 

503 
lemma sign_uint_Pls [simp]: 

504 
"bin_sign (uint x) = Int.Pls" 

505 
by (simp add: sign_Pls_ge_0 number_of_eq) 

506 

507 
lemmas uint_m2p_neg = iffD2 [OF diff_less_0_iff_less uint_lt2p, standard] 

508 
lemmas uint_m2p_not_non_neg = 

509 
iffD2 [OF linorder_not_le uint_m2p_neg, standard] 

510 

511 
lemma lt2p_lem: 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

512 
"len_of TYPE('a) <= n \<Longrightarrow> uint (w :: 'a :: len0 word) < 2 ^ n" 
37660  513 
by (rule xtr8 [OF _ uint_lt2p]) simp 
514 

515 
lemmas uint_le_0_iff [simp] = 

516 
uint_ge_0 [THEN leD, THEN linorder_antisym_conv1, standard] 

517 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

518 
lemma uint_nat: "uint w = int (unat w)" 
37660  519 
unfolding unat_def by auto 
520 

521 
lemma uint_number_of: 

522 
"uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)" 

523 
unfolding word_number_of_alt 

524 
by (simp only: int_word_uint) 

525 

526 
lemma unat_number_of: 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

527 
"bin_sign b = Int.Pls \<Longrightarrow> 
37660  528 
unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)" 
529 
apply (unfold unat_def) 

530 
apply (clarsimp simp only: uint_number_of) 

531 
apply (rule nat_mod_distrib [THEN trans]) 

532 
apply (erule sign_Pls_ge_0 [THEN iffD1]) 

533 
apply (simp_all add: nat_power_eq) 

534 
done 

535 

536 
lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + 

537 
2 ^ (len_of TYPE('a)  1)) mod 2 ^ len_of TYPE('a)  

538 
2 ^ (len_of TYPE('a)  1)" 

539 
unfolding word_number_of_alt by (rule int_word_sint) 

540 

541 
lemma word_of_int_bin [simp] : 

542 
"(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)" 

543 
unfolding word_number_of_alt by auto 

544 

545 
lemma word_int_case_wi: 

546 
"word_int_case f (word_of_int i :: 'b word) = 

547 
f (i mod 2 ^ len_of TYPE('b::len0))" 

548 
unfolding word_int_case_def by (simp add: word_uint.eq_norm) 

549 

550 
lemma word_int_split: 

551 
"P (word_int_case f x) = 

552 
(ALL i. x = (word_of_int i :: 'b :: len0 word) & 

553 
0 <= i & i < 2 ^ len_of TYPE('b) > P (f i))" 

554 
unfolding word_int_case_def 

555 
by (auto simp: word_uint.eq_norm int_mod_eq') 

556 

557 
lemma word_int_split_asm: 

558 
"P (word_int_case f x) = 

559 
(~ (EX n. x = (word_of_int n :: 'b::len0 word) & 

560 
0 <= n & n < 2 ^ len_of TYPE('b::len0) & ~ P (f n)))" 

561 
unfolding word_int_case_def 

562 
by (auto simp: word_uint.eq_norm int_mod_eq') 

563 

564 
lemmas uint_range' = 

565 
word_uint.Rep [unfolded uints_num mem_Collect_eq, standard] 

566 
lemmas sint_range' = word_sint.Rep [unfolded One_nat_def 

567 
sints_num mem_Collect_eq, standard] 

568 

569 
lemma uint_range_size: "0 <= uint w & uint w < 2 ^ size w" 

570 
unfolding word_size by (rule uint_range') 

571 

572 
lemma sint_range_size: 

573 
" (2 ^ (size w  Suc 0)) <= sint w & sint w < 2 ^ (size w  Suc 0)" 

574 
unfolding word_size by (rule sint_range') 

575 

576 
lemmas sint_above_size = sint_range_size 

577 
[THEN conjunct2, THEN [2] xtr8, folded One_nat_def, standard] 

578 

579 
lemmas sint_below_size = sint_range_size 

580 
[THEN conjunct1, THEN [2] order_trans, folded One_nat_def, standard] 

581 

582 
lemma test_bit_eq_iff: "(test_bit (u::'a::len0 word) = test_bit v) = (u = v)" 

583 
unfolding word_test_bit_def by (simp add: bin_nth_eq_iff) 

584 

585 
lemma test_bit_size [rule_format] : "(w::'a::len0 word) !! n > n < size w" 

586 
apply (unfold word_test_bit_def) 

587 
apply (subst word_ubin.norm_Rep [symmetric]) 

588 
apply (simp only: nth_bintr word_size) 

589 
apply fast 

590 
done 

591 

592 
lemma word_eqI [rule_format] : 

593 
fixes u :: "'a::len0 word" 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

594 
shows "(ALL n. n < size u > u !! n = v !! n) \<Longrightarrow> u = v" 
37660  595 
apply (rule test_bit_eq_iff [THEN iffD1]) 
596 
apply (rule ext) 

597 
apply (erule allE) 

598 
apply (erule impCE) 

599 
prefer 2 

600 
apply assumption 

601 
apply (auto dest!: test_bit_size simp add: word_size) 

602 
done 

603 

604 
lemmas word_eqD = test_bit_eq_iff [THEN iffD2, THEN fun_cong, standard] 

605 

606 
lemma test_bit_bin': "w !! n = (n < size w & bin_nth (uint w) n)" 

607 
unfolding word_test_bit_def word_size 

608 
by (simp add: nth_bintr [symmetric]) 

609 

610 
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] 

611 

612 
lemma bin_nth_uint_imp': "bin_nth (uint w) n > n < size w" 

613 
apply (unfold word_size) 

614 
apply (rule impI) 

615 
apply (rule nth_bintr [THEN iffD1, THEN conjunct1]) 

616 
apply (subst word_ubin.norm_Rep) 

617 
apply assumption 

618 
done 

619 

620 
lemma bin_nth_sint': 

621 
"n >= size w > bin_nth (sint w) n = bin_nth (sint w) (size w  1)" 

622 
apply (rule impI) 

623 
apply (subst word_sbin.norm_Rep [symmetric]) 

624 
apply (simp add : nth_sbintr word_size) 

625 
apply auto 

626 
done 

627 

628 
lemmas bin_nth_uint_imp = bin_nth_uint_imp' [rule_format, unfolded word_size] 

629 
lemmas bin_nth_sint = bin_nth_sint' [rule_format, unfolded word_size] 

630 

631 
(* type definitions theorem for in terms of equivalent bool list *) 

632 
lemma td_bl: 

633 
"type_definition (to_bl :: 'a::len0 word => bool list) 

634 
of_bl 

635 
{bl. length bl = len_of TYPE('a)}" 

636 
apply (unfold type_definition_def of_bl_def to_bl_def) 

637 
apply (simp add: word_ubin.eq_norm) 

638 
apply safe 

639 
apply (drule sym) 

640 
apply simp 

641 
done 

642 

643 
interpretation word_bl: 

644 
type_definition "to_bl :: 'a::len0 word => bool list" 

645 
of_bl 

646 
"{bl. length bl = len_of TYPE('a::len0)}" 

647 
by (rule td_bl) 

648 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

649 
lemma word_size_bl: "size w = size (to_bl w)" 
37660  650 
unfolding word_size by auto 
651 

652 
lemma to_bl_use_of_bl: 

653 
"(to_bl w = bl) = (w = of_bl bl \<and> length bl = length (to_bl w))" 

654 
by (fastsimp elim!: word_bl.Abs_inverse [simplified]) 

655 

656 
lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)" 

657 
unfolding word_reverse_def by (simp add: word_bl.Abs_inverse) 

658 

659 
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w" 

660 
unfolding word_reverse_def by (simp add : word_bl.Abs_inverse) 

661 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

662 
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w" 
37660  663 
by auto 
664 

665 
lemmas word_rev_gal' = sym [THEN word_rev_gal, symmetric, standard] 

666 

667 
lemmas length_bl_gt_0 [iff] = xtr1 [OF word_bl.Rep' len_gt_0, standard] 

668 
lemmas bl_not_Nil [iff] = 

669 
length_bl_gt_0 [THEN length_greater_0_conv [THEN iffD1], standard] 

670 
lemmas length_bl_neq_0 [iff] = length_bl_gt_0 [THEN gr_implies_not0] 

671 

672 
lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = Int.Min)" 

673 
apply (unfold to_bl_def sint_uint) 

674 
apply (rule trans [OF _ bl_sbin_sign]) 

675 
apply simp 

676 
done 

677 

678 
lemma of_bl_drop': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

679 
"lend = length bl  len_of TYPE ('a :: len0) \<Longrightarrow> 
37660  680 
of_bl (drop lend bl) = (of_bl bl :: 'a word)" 
681 
apply (unfold of_bl_def) 

682 
apply (clarsimp simp add : trunc_bl2bin [symmetric]) 

683 
done 

684 

685 
lemmas of_bl_no = of_bl_def [folded word_number_of_def] 

686 

687 
lemma test_bit_of_bl: 

688 
"(of_bl bl::'a::len0 word) !! n = (rev bl ! n \<and> n < len_of TYPE('a) \<and> n < length bl)" 

689 
apply (unfold of_bl_def word_test_bit_def) 

690 
apply (auto simp add: word_size word_ubin.eq_norm nth_bintr bin_nth_of_bl) 

691 
done 

692 

693 
lemma no_of_bl: 

694 
"(number_of bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) bin)" 

695 
unfolding word_size of_bl_no by (simp add : word_number_of_def) 

696 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

697 
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)" 
37660  698 
unfolding word_size to_bl_def by auto 
699 

700 
lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w" 

701 
unfolding uint_bl by (simp add : word_size) 

702 

703 
lemma to_bl_of_bin: 

704 
"to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin" 

705 
unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size) 

706 

707 
lemmas to_bl_no_bin [simp] = to_bl_of_bin [folded word_number_of_def] 

708 

709 
lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w" 

710 
unfolding uint_bl by (simp add : word_size) 

711 

712 
lemmas uint_bl_bin [simp] = trans [OF bin_bl_bin word_ubin.norm_Rep, standard] 

713 

714 
lemmas num_AB_u [simp] = word_uint.Rep_inverse 

715 
[unfolded o_def word_number_of_def [symmetric], standard] 

716 
lemmas num_AB_s [simp] = word_sint.Rep_inverse 

717 
[unfolded o_def word_number_of_def [symmetric], standard] 

718 

719 
(* naturals *) 

720 
lemma uints_unats: "uints n = int ` unats n" 

721 
apply (unfold unats_def uints_num) 

722 
apply safe 

723 
apply (rule_tac image_eqI) 

724 
apply (erule_tac nat_0_le [symmetric]) 

725 
apply auto 

726 
apply (erule_tac nat_less_iff [THEN iffD2]) 

727 
apply (rule_tac [2] zless_nat_eq_int_zless [THEN iffD1]) 

728 
apply (auto simp add : nat_power_eq int_power) 

729 
done 

730 

731 
lemma unats_uints: "unats n = nat ` uints n" 

732 
by (auto simp add : uints_unats image_iff) 

733 

734 
lemmas bintr_num = word_ubin.norm_eq_iff 

735 
[symmetric, folded word_number_of_def, standard] 

736 
lemmas sbintr_num = word_sbin.norm_eq_iff 

737 
[symmetric, folded word_number_of_def, standard] 

738 

739 
lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def, standard] 

740 
lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def, standard]; 

741 

742 
(* don't add these to simpset, since may want bintrunc n w to be simplified; 

743 
may want these in reverse, but loop as simp rules, so use following *) 

744 

745 
lemma num_of_bintr': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

746 
"bintrunc (len_of TYPE('a :: len0)) a = b \<Longrightarrow> 
37660  747 
number_of a = (number_of b :: 'a word)" 
748 
apply safe 

749 
apply (rule_tac num_of_bintr [symmetric]) 

750 
done 

751 

752 
lemma num_of_sbintr': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

753 
"sbintrunc (len_of TYPE('a :: len)  1) a = b \<Longrightarrow> 
37660  754 
number_of a = (number_of b :: 'a word)" 
755 
apply safe 

756 
apply (rule_tac num_of_sbintr [symmetric]) 

757 
done 

758 

759 
lemmas num_abs_bintr = sym [THEN trans, 

760 
OF num_of_bintr word_number_of_def, standard] 

761 
lemmas num_abs_sbintr = sym [THEN trans, 

762 
OF num_of_sbintr word_number_of_def, standard] 

763 

764 
(** cast  note, no arg for new length, as it's determined by type of result, 

765 
thus in "cast w = w, the type means cast to length of w! **) 

766 

767 
lemma ucast_id: "ucast w = w" 

768 
unfolding ucast_def by auto 

769 

770 
lemma scast_id: "scast w = w" 

771 
unfolding scast_def by auto 

772 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

773 
lemma ucast_bl: "ucast w = of_bl (to_bl w)" 
37660  774 
unfolding ucast_def of_bl_def uint_bl 
775 
by (auto simp add : word_size) 

776 

777 
lemma nth_ucast: 

778 
"(ucast w::'a::len0 word) !! n = (w !! n & n < len_of TYPE('a))" 

779 
apply (unfold ucast_def test_bit_bin) 

780 
apply (simp add: word_ubin.eq_norm nth_bintr word_size) 

781 
apply (fast elim!: bin_nth_uint_imp) 

782 
done 

783 

784 
(* for literal u(s)cast *) 

785 

786 
lemma ucast_bintr [simp]: 

787 
"ucast (number_of w ::'a::len0 word) = 

788 
number_of (bintrunc (len_of TYPE('a)) w)" 

789 
unfolding ucast_def by simp 

790 

791 
lemma scast_sbintr [simp]: 

792 
"scast (number_of w ::'a::len word) = 

793 
number_of (sbintrunc (len_of TYPE('a)  Suc 0) w)" 

794 
unfolding scast_def by simp 

795 

796 
lemmas source_size = source_size_def [unfolded Let_def word_size] 

797 
lemmas target_size = target_size_def [unfolded Let_def word_size] 

798 
lemmas is_down = is_down_def [unfolded source_size target_size] 

799 
lemmas is_up = is_up_def [unfolded source_size target_size] 

800 

801 
lemmas is_up_down = trans [OF is_up is_down [symmetric], standard] 

802 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

803 
lemma down_cast_same': "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc = scast" 
37660  804 
apply (unfold is_down) 
805 
apply safe 

806 
apply (rule ext) 

807 
apply (unfold ucast_def scast_def uint_sint) 

808 
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 

809 
apply simp 

810 
done 

811 

812 
lemma word_rev_tf': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

813 
"r = to_bl (of_bl bl) \<Longrightarrow> r = rev (takefill False (length r) (rev bl))" 
37660  814 
unfolding of_bl_def uint_bl 
815 
by (clarsimp simp add: bl_bin_bl_rtf word_ubin.eq_norm word_size) 

816 

817 
lemmas word_rev_tf = refl [THEN word_rev_tf', unfolded word_bl.Rep', standard] 

818 

819 
lemmas word_rep_drop = word_rev_tf [simplified takefill_alt, 

820 
simplified, simplified rev_take, simplified] 

821 

822 
lemma to_bl_ucast: 

823 
"to_bl (ucast (w::'b::len0 word) ::'a::len0 word) = 

824 
replicate (len_of TYPE('a)  len_of TYPE('b)) False @ 

825 
drop (len_of TYPE('b)  len_of TYPE('a)) (to_bl w)" 

826 
apply (unfold ucast_bl) 

827 
apply (rule trans) 

828 
apply (rule word_rep_drop) 

829 
apply simp 

830 
done 

831 

832 
lemma ucast_up_app': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

833 
"uc = ucast \<Longrightarrow> source_size uc + n = target_size uc \<Longrightarrow> 
37660  834 
to_bl (uc w) = replicate n False @ (to_bl w)" 
835 
by (auto simp add : source_size target_size to_bl_ucast) 

836 

837 
lemma ucast_down_drop': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

838 
"uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> 
37660  839 
to_bl (uc w) = drop n (to_bl w)" 
840 
by (auto simp add : source_size target_size to_bl_ucast) 

841 

842 
lemma scast_down_drop': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

843 
"sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> 
37660  844 
to_bl (sc w) = drop n (to_bl w)" 
845 
apply (subgoal_tac "sc = ucast") 

846 
apply safe 

847 
apply simp 

848 
apply (erule refl [THEN ucast_down_drop']) 

849 
apply (rule refl [THEN down_cast_same', symmetric]) 

850 
apply (simp add : source_size target_size is_down) 

851 
done 

852 

853 
lemma sint_up_scast': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

854 
"sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w" 
37660  855 
apply (unfold is_up) 
856 
apply safe 

857 
apply (simp add: scast_def word_sbin.eq_norm) 

858 
apply (rule box_equals) 

859 
prefer 3 

860 
apply (rule word_sbin.norm_Rep) 

861 
apply (rule sbintrunc_sbintrunc_l) 

862 
defer 

863 
apply (subst word_sbin.norm_Rep) 

864 
apply (rule refl) 

865 
apply simp 

866 
done 

867 

868 
lemma uint_up_ucast': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

869 
"uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w" 
37660  870 
apply (unfold is_up) 
871 
apply safe 

872 
apply (rule bin_eqI) 

873 
apply (fold word_test_bit_def) 

874 
apply (auto simp add: nth_ucast) 

875 
apply (auto simp add: test_bit_bin) 

876 
done 

877 

878 
lemmas down_cast_same = refl [THEN down_cast_same'] 

879 
lemmas ucast_up_app = refl [THEN ucast_up_app'] 

880 
lemmas ucast_down_drop = refl [THEN ucast_down_drop'] 

881 
lemmas scast_down_drop = refl [THEN scast_down_drop'] 

882 
lemmas uint_up_ucast = refl [THEN uint_up_ucast'] 

883 
lemmas sint_up_scast = refl [THEN sint_up_scast'] 

884 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

885 
lemma ucast_up_ucast': "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> ucast (uc w) = ucast w" 
37660  886 
apply (simp (no_asm) add: ucast_def) 
887 
apply (clarsimp simp add: uint_up_ucast) 

888 
done 

889 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

890 
lemma scast_up_scast': "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> scast (sc w) = scast w" 
37660  891 
apply (simp (no_asm) add: scast_def) 
892 
apply (clarsimp simp add: sint_up_scast) 

893 
done 

894 

895 
lemma ucast_of_bl_up': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

896 
"w = of_bl bl \<Longrightarrow> size bl <= size w \<Longrightarrow> ucast w = of_bl bl" 
37660  897 
by (auto simp add : nth_ucast word_size test_bit_of_bl intro!: word_eqI) 
898 

899 
lemmas ucast_up_ucast = refl [THEN ucast_up_ucast'] 

900 
lemmas scast_up_scast = refl [THEN scast_up_scast'] 

901 
lemmas ucast_of_bl_up = refl [THEN ucast_of_bl_up'] 

902 

903 
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] 

904 
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] 

905 

906 
lemmas isduu = is_up_down [where c = "ucast", THEN iffD2] 

907 
lemmas isdus = is_up_down [where c = "scast", THEN iffD2] 

908 
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] 

909 
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] 

910 

911 
lemma up_ucast_surj: 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

912 
"is_up (ucast :: 'b::len0 word => 'a::len0 word) \<Longrightarrow> 
37660  913 
surj (ucast :: 'a word => 'b word)" 
914 
by (rule surjI, erule ucast_up_ucast_id) 

915 

916 
lemma up_scast_surj: 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

917 
"is_up (scast :: 'b::len word => 'a::len word) \<Longrightarrow> 
37660  918 
surj (scast :: 'a word => 'b word)" 
919 
by (rule surjI, erule scast_up_scast_id) 

920 

921 
lemma down_scast_inj: 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

922 
"is_down (scast :: 'b::len word => 'a::len word) \<Longrightarrow> 
37660  923 
inj_on (ucast :: 'a word => 'b word) A" 
924 
by (rule inj_on_inverseI, erule scast_down_scast_id) 

925 

926 
lemma down_ucast_inj: 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

927 
"is_down (ucast :: 'b::len0 word => 'a::len0 word) \<Longrightarrow> 
37660  928 
inj_on (ucast :: 'a word => 'b word) A" 
929 
by (rule inj_on_inverseI, erule ucast_down_ucast_id) 

930 

931 
lemma of_bl_append_same: "of_bl (X @ to_bl w) = w" 

932 
by (rule word_bl.Rep_eqD) (simp add: word_rep_drop) 

933 

934 
lemma ucast_down_no': 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

935 
"uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (number_of bin) = number_of bin" 
37660  936 
apply (unfold word_number_of_def is_down) 
937 
apply (clarsimp simp add: ucast_def word_ubin.eq_norm) 

938 
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 

939 
apply (erule bintrunc_bintrunc_ge) 

940 
done 

941 

942 
lemmas ucast_down_no = ucast_down_no' [OF refl] 

943 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

944 
lemma ucast_down_bl': "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl" 
37660  945 
unfolding of_bl_no by clarify (erule ucast_down_no) 
946 

947 
lemmas ucast_down_bl = ucast_down_bl' [OF refl] 

948 

949 
lemmas slice_def' = slice_def [unfolded word_size] 

950 
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] 

951 

952 
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def 

953 
lemmas word_log_bin_defs = word_log_defs 

954 

955 
text {* Executable equality *} 

956 

38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38527
diff
changeset

957 
instantiation word :: (len0) equal 
24333  958 
begin 
959 

38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38527
diff
changeset

960 
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" where 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38527
diff
changeset

961 
"equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" 
37660  962 

963 
instance proof 

38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38527
diff
changeset

964 
qed (simp add: equal equal_word_def) 
37660  965 

966 
end 

967 

968 

969 
subsection {* Word Arithmetic *} 

970 

971 
lemma word_less_alt: "(a < b) = (uint a < uint b)" 

972 
unfolding word_less_def word_le_def 

973 
by (auto simp del: word_uint.Rep_inject 

974 
simp: word_uint.Rep_inject [symmetric]) 

975 

976 
lemma signed_linorder: "class.linorder word_sle word_sless" 

977 
proof 

978 
qed (unfold word_sle_def word_sless_def, auto) 

979 

980 
interpretation signed: linorder "word_sle" "word_sless" 

981 
by (rule signed_linorder) 

982 

983 
lemmas word_arith_wis = 

984 
word_add_def word_mult_def word_minus_def 

985 
word_succ_def word_pred_def word_0_wi word_1_wi 

986 

987 
lemma udvdI: 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

988 
"0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" 
37660  989 
by (auto simp: udvd_def) 
990 

991 
lemmas word_div_no [simp] = 

992 
word_div_def [of "number_of a" "number_of b", standard] 

993 

994 
lemmas word_mod_no [simp] = 

995 
word_mod_def [of "number_of a" "number_of b", standard] 

996 

997 
lemmas word_less_no [simp] = 

998 
word_less_def [of "number_of a" "number_of b", standard] 

999 

1000 
lemmas word_le_no [simp] = 

1001 
word_le_def [of "number_of a" "number_of b", standard] 

1002 

1003 
lemmas word_sless_no [simp] = 

1004 
word_sless_def [of "number_of a" "number_of b", standard] 

1005 

1006 
lemmas word_sle_no [simp] = 

1007 
word_sle_def [of "number_of a" "number_of b", standard] 

1008 

1009 
(* following two are available in class number_ring, 

1010 
but convenient to have them here here; 

1011 
note  the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1 

1012 
are in the default simpset, so to use the automatic simplifications for 

1013 
(eg) sint (number_of bin) on sint 1, must do 

1014 
(simp add: word_1_no del: numeral_1_eq_1) 

1015 
*) 

1016 
lemmas word_0_wi_Pls = word_0_wi [folded Pls_def] 

1017 
lemmas word_0_no = word_0_wi_Pls [folded word_no_wi] 

1018 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1019 
lemma int_one_bin: "(1 :: int) = (Int.Pls BIT 1)" 
37660  1020 
unfolding Pls_def Bit_def by auto 
1021 

1022 
lemma word_1_no: 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1023 
"(1 :: 'a :: len0 word) = number_of (Int.Pls BIT 1)" 
37660  1024 
unfolding word_1_wi word_number_of_def int_one_bin by auto 
1025 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1026 
lemma word_m1_wi: "1 = word_of_int 1" 
37660  1027 
by (rule word_number_of_alt) 
1028 

1029 
lemma word_m1_wi_Min: "1 = word_of_int Int.Min" 

1030 
by (simp add: word_m1_wi number_of_eq) 

1031 

1032 
lemma word_0_bl: "of_bl [] = 0" 

1033 
unfolding word_0_wi of_bl_def by (simp add : Pls_def) 

1034 

1035 
lemma word_1_bl: "of_bl [True] = 1" 

1036 
unfolding word_1_wi of_bl_def 

1037 
by (simp add : bl_to_bin_def Bit_def Pls_def) 

1038 

1039 
lemma uint_eq_0 [simp] : "(uint 0 = 0)" 

1040 
unfolding word_0_wi 

1041 
by (simp add: word_ubin.eq_norm Pls_def [symmetric]) 

1042 

1043 
lemma of_bl_0 [simp] : "of_bl (replicate n False) = 0" 

1044 
by (simp add : word_0_wi of_bl_def bl_to_bin_rep_False Pls_def) 

1045 

1046 
lemma to_bl_0: 

1047 
"to_bl (0::'a::len0 word) = replicate (len_of TYPE('a)) False" 

1048 
unfolding uint_bl 

1049 
by (simp add : word_size bin_to_bl_Pls Pls_def [symmetric]) 

1050 

1051 
lemma uint_0_iff: "(uint x = 0) = (x = 0)" 

1052 
by (auto intro!: word_uint.Rep_eqD) 

1053 

1054 
lemma unat_0_iff: "(unat x = 0) = (x = 0)" 

1055 
unfolding unat_def by (auto simp add : nat_eq_iff uint_0_iff) 

1056 

1057 
lemma unat_0 [simp]: "unat 0 = 0" 

1058 
unfolding unat_def by auto 

1059 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1060 
lemma size_0_same': "size w = 0 \<Longrightarrow> w = (v :: 'a :: len0 word)" 
37660  1061 
apply (unfold word_size) 
1062 
apply (rule box_equals) 

1063 
defer 

1064 
apply (rule word_uint.Rep_inverse)+ 

1065 
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 

1066 
apply simp 

1067 
done 

1068 

1069 
lemmas size_0_same = size_0_same' [folded word_size] 

1070 

1071 
lemmas unat_eq_0 = unat_0_iff 

1072 
lemmas unat_eq_zero = unat_0_iff 

1073 

1074 
lemma unat_gt_0: "(0 < unat x) = (x ~= 0)" 

1075 
by (auto simp: unat_0_iff [symmetric]) 

1076 

1077 
lemma ucast_0 [simp] : "ucast 0 = 0" 

1078 
unfolding ucast_def 

1079 
by simp (simp add: word_0_wi) 

1080 

1081 
lemma sint_0 [simp] : "sint 0 = 0" 

1082 
unfolding sint_uint 

1083 
by (simp add: Pls_def [symmetric]) 

1084 

1085 
lemma scast_0 [simp] : "scast 0 = 0" 

1086 
apply (unfold scast_def) 

1087 
apply simp 

1088 
apply (simp add: word_0_wi) 

1089 
done 

1090 

1091 
lemma sint_n1 [simp] : "sint 1 = 1" 

1092 
apply (unfold word_m1_wi_Min) 

1093 
apply (simp add: word_sbin.eq_norm) 

1094 
apply (unfold Min_def number_of_eq) 

1095 
apply simp 

1096 
done 

1097 

1098 
lemma scast_n1 [simp] : "scast 1 = 1" 

1099 
apply (unfold scast_def sint_n1) 

1100 
apply (unfold word_number_of_alt) 

1101 
apply (rule refl) 

1102 
done 

1103 

1104 
lemma uint_1 [simp] : "uint (1 :: 'a :: len word) = 1" 

1105 
unfolding word_1_wi 

1106 
by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps) 

1107 

1108 
lemma unat_1 [simp] : "unat (1 :: 'a :: len word) = 1" 

1109 
by (unfold unat_def uint_1) auto 

1110 

1111 
lemma ucast_1 [simp] : "ucast (1 :: 'a :: len word) = 1" 

1112 
unfolding ucast_def word_1_wi 

1113 
by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps) 

1114 

1115 
(* abstraction preserves the operations 

1116 
(the definitions tell this for bins in range uint) *) 

1117 

1118 
lemmas arths = 

1119 
bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1], 

1120 
folded word_ubin.eq_norm, standard] 

1121 

1122 
lemma wi_homs: 

1123 
shows 

1124 
wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and 

1125 
wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and 

1126 
wi_hom_neg: " word_of_int a = word_of_int ( a)" and 

1127 
wi_hom_succ: "word_succ (word_of_int a) = word_of_int (Int.succ a)" and 

1128 
wi_hom_pred: "word_pred (word_of_int a) = word_of_int (Int.pred a)" 

1129 
by (auto simp: word_arith_wis arths) 

1130 

1131 
lemmas wi_hom_syms = wi_homs [symmetric] 

1132 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1133 
lemma word_sub_def: "a  b = a +  (b :: 'a :: len0 word)" 
37887  1134 
unfolding word_sub_wi diff_minus 
37660  1135 
by (simp only : word_uint.Rep_inverse wi_hom_syms) 
1136 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1137 
lemmas word_diff_minus = word_sub_def [standard] 
37660  1138 

1139 
lemma word_of_int_sub_hom: 

1140 
"(word_of_int a)  word_of_int b = word_of_int (a  b)" 

37887  1141 
unfolding word_sub_def diff_minus by (simp only : wi_homs) 
37660  1142 

1143 
lemmas new_word_of_int_homs = 

1144 
word_of_int_sub_hom wi_homs word_0_wi word_1_wi 

1145 

1146 
lemmas new_word_of_int_hom_syms = new_word_of_int_homs [symmetric, standard] 

1147 

1148 
lemmas word_of_int_hom_syms = 

1149 
new_word_of_int_hom_syms [unfolded succ_def pred_def] 

1150 

1151 
lemmas word_of_int_homs = 

1152 
new_word_of_int_homs [unfolded succ_def pred_def] 

1153 

1154 
lemmas word_of_int_add_hom = word_of_int_homs (2) 

1155 
lemmas word_of_int_mult_hom = word_of_int_homs (3) 

1156 
lemmas word_of_int_minus_hom = word_of_int_homs (4) 

1157 
lemmas word_of_int_succ_hom = word_of_int_homs (5) 

1158 
lemmas word_of_int_pred_hom = word_of_int_homs (6) 

1159 
lemmas word_of_int_0_hom = word_of_int_homs (7) 

1160 
lemmas word_of_int_1_hom = word_of_int_homs (8) 

1161 

1162 
(* now, to get the weaker results analogous to word_div/mod_def *) 

1163 

1164 
lemmas word_arith_alts = 

1165 
word_sub_wi [unfolded succ_def pred_def, standard] 

1166 
word_arith_wis [unfolded succ_def pred_def, standard] 

1167 

1168 
lemmas word_sub_alt = word_arith_alts (1) 

1169 
lemmas word_add_alt = word_arith_alts (2) 

1170 
lemmas word_mult_alt = word_arith_alts (3) 

1171 
lemmas word_minus_alt = word_arith_alts (4) 

1172 
lemmas word_succ_alt = word_arith_alts (5) 

1173 
lemmas word_pred_alt = word_arith_alts (6) 

1174 
lemmas word_0_alt = word_arith_alts (7) 

1175 
lemmas word_1_alt = word_arith_alts (8) 

1176 

1177 
subsection "Transferring goals from words to ints" 

1178 

1179 
lemma word_ths: 

1180 
shows 

1181 
word_succ_p1: "word_succ a = a + 1" and 

1182 
word_pred_m1: "word_pred a = a  1" and 

1183 
word_pred_succ: "word_pred (word_succ a) = a" and 

1184 
word_succ_pred: "word_succ (word_pred a) = a" and 

1185 
word_mult_succ: "word_succ a * b = b + a * b" 

1186 
by (rule word_uint.Abs_cases [of b], 

1187 
rule word_uint.Abs_cases [of a], 

1188 
simp add: pred_def succ_def add_commute mult_commute 

1189 
ring_distribs new_word_of_int_homs)+ 

1190 

1191 
lemmas uint_cong = arg_cong [where f = uint] 

1192 

1193 
lemmas uint_word_ariths = 

1194 
word_arith_alts [THEN trans [OF uint_cong int_word_uint], standard] 

1195 

1196 
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p] 

1197 

1198 
(* similar expressions for sint (arith operations) *) 

1199 
lemmas sint_word_ariths = uint_word_arith_bintrs 

1200 
[THEN uint_sint [symmetric, THEN trans], 

1201 
unfolded uint_sint bintr_arith1s bintr_ariths 

1202 
len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep, standard] 

1203 

1204 
lemmas uint_div_alt = word_div_def 

1205 
[THEN trans [OF uint_cong int_word_uint], standard] 

1206 
lemmas uint_mod_alt = word_mod_def 

1207 
[THEN trans [OF uint_cong int_word_uint], standard] 

1208 

1209 
lemma word_pred_0_n1: "word_pred 0 = word_of_int 1" 

1210 
unfolding word_pred_def number_of_eq 

1211 
by (simp add : pred_def word_no_wi) 

1212 

1213 
lemma word_pred_0_Min: "word_pred 0 = word_of_int Int.Min" 

1214 
by (simp add: word_pred_0_n1 number_of_eq) 

1215 

1216 
lemma word_m1_Min: " 1 = word_of_int Int.Min" 

1217 
unfolding Min_def by (simp only: word_of_int_hom_syms) 

1218 

1219 
lemma succ_pred_no [simp]: 

1220 
"word_succ (number_of bin) = number_of (Int.succ bin) & 

1221 
word_pred (number_of bin) = number_of (Int.pred bin)" 

1222 
unfolding word_number_of_def by (simp add : new_word_of_int_homs) 

1223 

1224 
lemma word_sp_01 [simp] : 

1225 
"word_succ 1 = 0 & word_succ 0 = 1 & word_pred 0 = 1 & word_pred 1 = 0" 

1226 
by (unfold word_0_no word_1_no) auto 

1227 

1228 
(* alternative approach to lifting arithmetic equalities *) 

1229 
lemma word_of_int_Ex: 

1230 
"\<exists>y. x = word_of_int y" 

1231 
by (rule_tac x="uint x" in exI) simp 

1232 

1233 
lemma word_arith_eqs: 

1234 
fixes a :: "'a::len0 word" 

1235 
fixes b :: "'a::len0 word" 

1236 
shows 

1237 
word_add_0: "0 + a = a" and 

1238 
word_add_0_right: "a + 0 = a" and 

1239 
word_mult_1: "1 * a = a" and 

1240 
word_mult_1_right: "a * 1 = a" and 

1241 
word_add_commute: "a + b = b + a" and 

1242 
word_add_assoc: "a + b + c = a + (b + c)" and 

1243 
word_add_left_commute: "a + (b + c) = b + (a + c)" and 

1244 
word_mult_commute: "a * b = b * a" and 

1245 
word_mult_assoc: "a * b * c = a * (b * c)" and 

1246 
word_mult_left_commute: "a * (b * c) = b * (a * c)" and 

1247 
word_left_distrib: "(a + b) * c = a * c + b * c" and 

1248 
word_right_distrib: "a * (b + c) = a * b + a * c" and 

1249 
word_left_minus: " a + a = 0" and 

1250 
word_diff_0_right: "a  0 = a" and 

1251 
word_diff_self: "a  a = 0" 

1252 
using word_of_int_Ex [of a] 

1253 
word_of_int_Ex [of b] 

1254 
word_of_int_Ex [of c] 

1255 
by (auto simp: word_of_int_hom_syms [symmetric] 

1256 
zadd_0_right add_commute add_assoc add_left_commute 

1257 
mult_commute mult_assoc mult_left_commute 

1258 
left_distrib right_distrib) 

1259 

1260 
lemmas word_add_ac = word_add_commute word_add_assoc word_add_left_commute 

1261 
lemmas word_mult_ac = word_mult_commute word_mult_assoc word_mult_left_commute 

1262 

1263 
lemmas word_plus_ac0 = word_add_0 word_add_0_right word_add_ac 

1264 
lemmas word_times_ac1 = word_mult_1 word_mult_1_right word_mult_ac 

1265 

1266 

1267 
subsection "Order on fixedlength words" 

1268 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1269 
lemma word_order_trans: "x <= y \<Longrightarrow> y <= z \<Longrightarrow> x <= (z :: 'a :: len0 word)" 
37660  1270 
unfolding word_le_def by auto 
1271 

1272 
lemma word_order_refl: "z <= (z :: 'a :: len0 word)" 

1273 
unfolding word_le_def by auto 

1274 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1275 
lemma word_order_antisym: "x <= y \<Longrightarrow> y <= x \<Longrightarrow> x = (y :: 'a :: len0 word)" 
37660  1276 
unfolding word_le_def by (auto intro!: word_uint.Rep_eqD) 
1277 

1278 
lemma word_order_linear: 

1279 
"y <= x  x <= (y :: 'a :: len0 word)" 

1280 
unfolding word_le_def by auto 

1281 

1282 
lemma word_zero_le [simp] : 

1283 
"0 <= (y :: 'a :: len0 word)" 

1284 
unfolding word_le_def by auto 

1285 

1286 
instance word :: (len0) semigroup_add 

1287 
by intro_classes (simp add: word_add_assoc) 

1288 

1289 
instance word :: (len0) linorder 

1290 
by intro_classes (auto simp: word_less_def word_le_def) 

1291 

1292 
instance word :: (len0) ring 

1293 
by intro_classes 

1294 
(auto simp: word_arith_eqs word_diff_minus 

1295 
word_diff_self [unfolded word_diff_minus]) 

1296 

1297 
lemma word_m1_ge [simp] : "word_pred 0 >= y" 

1298 
unfolding word_le_def 

1299 
by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto 

1300 

1301 
lemmas word_n1_ge [simp] = word_m1_ge [simplified word_sp_01] 

1302 

1303 
lemmas word_not_simps [simp] = 

1304 
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] 

1305 

1306 
lemma word_gt_0: "0 < y = (0 ~= (y :: 'a :: len0 word))" 

1307 
unfolding word_less_def by auto 

1308 

1309 
lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of y", standard] 

1310 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1311 
lemma word_sless_alt: "(a <s b) = (sint a < sint b)" 
37660  1312 
unfolding word_sle_def word_sless_def 
1313 
by (auto simp add: less_le) 

1314 

1315 
lemma word_le_nat_alt: "(a <= b) = (unat a <= unat b)" 

1316 
unfolding unat_def word_le_def 

1317 
by (rule nat_le_eq_zle [symmetric]) simp 

1318 

1319 
lemma word_less_nat_alt: "(a < b) = (unat a < unat b)" 

1320 
unfolding unat_def word_less_alt 

1321 
by (rule nat_less_eq_zless [symmetric]) simp 

1322 

1323 
lemma wi_less: 

1324 
"(word_of_int n < (word_of_int m :: 'a :: len0 word)) = 

1325 
(n mod 2 ^ len_of TYPE('a) < m mod 2 ^ len_of TYPE('a))" 

1326 
unfolding word_less_alt by (simp add: word_uint.eq_norm) 

1327 

1328 
lemma wi_le: 

1329 
"(word_of_int n <= (word_of_int m :: 'a :: len0 word)) = 

1330 
(n mod 2 ^ len_of TYPE('a) <= m mod 2 ^ len_of TYPE('a))" 

1331 
unfolding word_le_def by (simp add: word_uint.eq_norm) 

1332 

1333 
lemma udvd_nat_alt: "a udvd b = (EX n>=0. unat b = n * unat a)" 

1334 
apply (unfold udvd_def) 

1335 
apply safe 

1336 
apply (simp add: unat_def nat_mult_distrib) 

1337 
apply (simp add: uint_nat int_mult) 

1338 
apply (rule exI) 

1339 
apply safe 

1340 
prefer 2 

1341 
apply (erule notE) 

1342 
apply (rule refl) 

1343 
apply force 

1344 
done 

1345 

1346 
lemma udvd_iff_dvd: "x udvd y <> unat x dvd unat y" 

1347 
unfolding dvd_def udvd_nat_alt by force 

1348 

1349 
lemmas unat_mono = word_less_nat_alt [THEN iffD1, standard] 

1350 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1351 
lemma word_zero_neq_one: "0 < len_of TYPE ('a :: len0) \<Longrightarrow> (0 :: 'a word) ~= 1"; 
37660  1352 
unfolding word_arith_wis 
1353 
by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc) 

1354 

1355 
lemmas lenw1_zero_neq_one = len_gt_0 [THEN word_zero_neq_one] 

1356 

1357 
lemma no_no [simp] : "number_of (number_of b) = number_of b" 

1358 
by (simp add: number_of_eq) 

1359 

40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

1360 
lemma unat_minus_one: "x ~= 0 \<Longrightarrow> unat (x  1) = unat x  1" 
37660  1361 
apply (unfold unat_def) 
1362 
apply (simp only: int_word_uint word_arith_alts rdmods) 

1363 
apply (subgoal_tac "uint x >= 1") 

1364 
prefer 2 

1365 
apply (drule contrapos_nn) 
