author  huffman 
Thu, 23 Feb 2012 16:09:16 +0100  
changeset 46618  a8c342aa53d6 
parent 46617  8c5d10d41391 
child 46645  573aff6b9b0a 
permissions  rwrr 
29628  1 
(* Title: HOL/Word/Word.thy 
46124  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 
41413
64cd30d6b0b8
explicit file specifications  avoid secondary load path;
wenzelm
parents:
41060
diff
changeset

8 
imports 
64cd30d6b0b8
explicit file specifications  avoid secondary load path;
wenzelm
parents:
41060
diff
changeset

9 
Type_Length 
64cd30d6b0b8
explicit file specifications  avoid secondary load path;
wenzelm
parents:
41060
diff
changeset

10 
Misc_Typedef 
64cd30d6b0b8
explicit file specifications  avoid secondary load path;
wenzelm
parents:
41060
diff
changeset

11 
"~~/src/HOL/Library/Boolean_Algebra" 
64cd30d6b0b8
explicit file specifications  avoid secondary load path;
wenzelm
parents:
41060
diff
changeset

12 
Bool_List_Representation 
41060
4199fdcfa3c0
moved smt_word.ML into the directory of the Word library
boehmes
parents:
40827
diff
changeset

13 
uses ("~~/src/HOL/Word/Tools/smt_word.ML") 
37660  14 
begin 
15 

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

17 

18 
subsection {* Type definition *} 

19 

45694
4a8743618257
prefer typedef without extra definition and alternative name;
wenzelm
parents:
45620
diff
changeset

20 
typedef (open) 'a word = "{(0::int) ..< 2^len_of TYPE('a::len0)}" 
37660  21 
morphisms uint Abs_word by auto 
22 

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

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

25 
only difference in these is the type class *} 

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

27 

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

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

30 

31 
code_datatype word_of_int 

32 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

33 
subsection {* Random instance *} 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

34 

37751  35 
notation fcomp (infixl "\<circ>>" 60) 
36 
notation scomp (infixl "\<circ>\<rightarrow>" 60) 

37660  37 

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

39 
begin 

40 

41 
definition 

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

45 

46 
instance .. 

47 

48 
end 

49 

37751  50 
no_notation fcomp (infixl "\<circ>>" 60) 
51 
no_notation scomp (infixl "\<circ>\<rightarrow>" 60) 

37660  52 

53 

54 
subsection {* Type conversions and casting *} 

55 

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

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

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

59 

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

61 
"unat w = nat (uint w)" 

62 

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

64 
 "the sets of integers representing the words" 

65 
"uints n = range (bintrunc n)" 

66 

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

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

69 

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

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

72 

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

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

75 

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

77 
 "cast a word to a different length" 

78 
"scast w = word_of_int (sint w)" 

79 

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

81 
"ucast w = word_of_int (uint w)" 

82 

83 
instantiation word :: (len0) size 

84 
begin 

85 

86 
definition 

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

88 

89 
instance .. 

90 

91 
end 

92 

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

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

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

96 

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

98 
"target_size c = size (c undefined)" 

99 

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

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

102 

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

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

105 

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

107 
"of_bl bl = word_of_int (bl_to_bin bl)" 

108 

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

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

111 

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

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

114 

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

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

117 

118 
translations 

46136
a3d4cf5203f5
recovered case syntax for of_int, also with source positions (appears to be unused nonetheless);
wenzelm
parents:
46124
diff
changeset

119 
"case x of XCONST of_int y => b" == "CONST word_int_case (%y. b) x" 
a3d4cf5203f5
recovered case syntax for of_int, also with source positions (appears to be unused nonetheless);
wenzelm
parents:
46124
diff
changeset

120 
"case x of (XCONST of_int :: 'a) y => b" => "CONST word_int_case (%y. b) x" 
37660  121 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

122 
subsection {* Typedefinition locale instantiations *} 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

123 

45805  124 
lemma word_size_gt_0 [iff]: "0 < size (w::'a::len word)" 
125 
by (fact xtr1 [OF word_size len_gt_0]) 

126 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

127 
lemmas lens_gt_0 = word_size_gt_0 len_gt_0 
45604  128 
lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0] 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

129 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

130 
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

131 
by (simp add: uints_def range_bintrunc) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

132 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

133 
lemma sints_num: "sints n = {i.  (2 ^ (n  1)) \<le> i \<and> i < 2 ^ (n  1)}" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

134 
by (simp add: sints_def range_sbintrunc) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

135 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

136 
lemma 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

137 
uint_0:"0 <= uint x" and 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

138 
uint_lt: "uint (x::'a::len0 word) < 2 ^ len_of TYPE('a)" 
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

139 
by (auto simp: uint [unfolded atLeastLessThan_iff]) 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

140 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

141 
lemma uint_mod_same: 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

142 
"uint x mod 2 ^ len_of TYPE('a) = uint (x::'a::len0 word)" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

143 
by (simp add: int_mod_eq uint_lt uint_0) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

144 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

145 
lemma td_ext_uint: 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

146 
"td_ext (uint :: 'a word => int) word_of_int (uints (len_of TYPE('a::len0))) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

147 
(%w::int. w mod 2 ^ len_of TYPE('a))" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

148 
apply (unfold td_ext_def') 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

149 
apply (simp add: uints_num word_of_int_def bintrunc_mod2p) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

150 
apply (simp add: uint_mod_same uint_0 uint_lt 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

151 
word.uint_inverse word.Abs_word_inverse int_mod_lem) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

152 
done 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

153 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

154 
interpretation word_uint: 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

155 
td_ext "uint::'a::len0 word \<Rightarrow> int" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

156 
word_of_int 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

157 
"uints (len_of TYPE('a::len0))" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

158 
"\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

159 
by (rule td_ext_uint) 
46013  160 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

161 
lemmas td_uint = word_uint.td_thm 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

162 

46013  163 
lemmas int_word_uint = word_uint.eq_norm 
164 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

165 
lemmas td_ext_ubin = td_ext_uint 
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

166 
[unfolded len_gt_0 no_bintr_alt1 [symmetric]] 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

167 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

168 
interpretation word_ubin: 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

169 
td_ext "uint::'a::len0 word \<Rightarrow> int" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

170 
word_of_int 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

171 
"uints (len_of TYPE('a::len0))" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

172 
"bintrunc (len_of TYPE('a::len0))" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

173 
by (rule td_ext_ubin) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

174 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

175 
lemma split_word_all: 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

176 
"(\<And>x::'a::len0 word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

177 
proof 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

178 
fix x :: "'a word" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

179 
assume "\<And>x. PROP P (word_of_int x)" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

180 
hence "PROP P (word_of_int (uint x))" . 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

181 
thus "PROP P x" by simp 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

182 
qed 
37660  183 

184 
subsection "Arithmetic operations" 

185 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

186 
definition 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

187 
word_succ :: "'a :: len0 word => 'a word" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

188 
where 
46000  189 
"word_succ a = word_of_int (uint a + 1)" 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

190 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

191 
definition 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

192 
word_pred :: "'a :: len0 word => 'a word" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

193 
where 
46000  194 
"word_pred a = word_of_int (uint a  1)" 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

195 

45547  196 
instantiation word :: (len0) "{number, Divides.div, comm_monoid_mult, comm_ring}" 
37660  197 
begin 
198 

199 
definition 

200 
word_0_wi: "0 = word_of_int 0" 

201 

202 
definition 

203 
word_1_wi: "1 = word_of_int 1" 

204 

205 
definition 

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

207 

208 
definition 

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

210 

211 
definition 

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

213 

214 
definition 

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

216 

217 
definition 

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

219 

220 
definition 

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

222 

223 
definition 

224 
word_number_of_def: "number_of w = word_of_int w" 

225 

46013  226 
lemmas word_arith_wis = 
227 
word_add_def word_sub_wi word_mult_def word_minus_def 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

228 
word_succ_def word_pred_def word_0_wi word_1_wi 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

229 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

230 
lemmas arths = 
45604  231 
bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1], folded word_ubin.eq_norm] 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

232 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

233 
lemma wi_homs: 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

234 
shows 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

235 
wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and 
46013  236 
wi_hom_sub: "word_of_int a  word_of_int b = word_of_int (a  b)" and 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

237 
wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

238 
wi_hom_neg: " word_of_int a = word_of_int ( a)" and 
46000  239 
wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" and 
240 
wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a  1)" 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

241 
by (auto simp: word_arith_wis arths) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

242 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

243 
lemmas wi_hom_syms = wi_homs [symmetric] 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

244 

46013  245 
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi 
46009  246 

247 
lemmas word_of_int_hom_syms = word_of_int_homs [symmetric] 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

248 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

249 
instance 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

250 
by default (auto simp: split_word_all word_of_int_homs algebra_simps) 
37660  251 

252 
end 

253 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

254 
instance word :: (len) comm_ring_1 
45810  255 
proof 
256 
have "0 < len_of TYPE('a)" by (rule len_gt_0) 

257 
then show "(0::'a word) \<noteq> 1" 

258 
unfolding word_0_wi word_1_wi 

259 
by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc) 

260 
qed 

45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

261 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

262 
lemma word_of_nat: "of_nat n = word_of_int (int n)" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

263 
by (induct n) (auto simp add : word_of_int_hom_syms) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

264 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

265 
lemma word_of_int: "of_int = word_of_int" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

266 
apply (rule ext) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

267 
apply (case_tac x rule: int_diff_cases) 
46013  268 
apply (simp add: word_of_nat wi_hom_sub) 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

269 
done 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

270 

26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

271 
instance word :: (len) number_ring 
45810  272 
by (default, simp add: word_number_of_def word_of_int) 
37660  273 

274 
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

275 
"a udvd b = (EX n>=0. uint b = n * uint a)" 
37660  276 

45547  277 

278 
subsection "Ordering" 

279 

280 
instantiation word :: (len0) linorder 

281 
begin 

282 

37660  283 
definition 
284 
word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b" 

285 

286 
definition 

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

288 

45547  289 
instance 
290 
by default (auto simp: word_less_def word_le_def) 

291 

292 
end 

293 

37660  294 
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

295 
"a <=s b = (sint a <= sint b)" 
37660  296 

297 
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

298 
"(x <s y) = (x <=s y & x ~= y)" 
37660  299 

300 

301 
subsection "Bitwise operations" 

302 

303 
instantiation word :: (len0) bits 

304 
begin 

305 

306 
definition 

307 
word_and_def: 

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

309 

310 
definition 

311 
word_or_def: 

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

313 

314 
definition 

315 
word_xor_def: 

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

317 

318 
definition 

319 
word_not_def: 

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

321 

322 
definition 

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

324 

325 
definition 

326 
word_set_bit_def: "set_bit a n x = 

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

328 

329 
definition 

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

331 

332 
definition 

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

334 

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

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

337 

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

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

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

341 

342 
definition 

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

344 

345 
definition 

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

347 

348 
instance .. 

349 

350 
end 

351 

352 
instantiation word :: (len) bitss 

353 
begin 

354 

355 
definition 

356 
word_msb_def: 

46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

357 
"msb a \<longleftrightarrow> bin_sign (sint a) = 1" 
37660  358 

359 
instance .. 

360 

361 
end 

362 

363 
definition setBit :: "'a :: len0 word => nat => 'a word" where 

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

364 
"setBit w n = set_bit w n True" 
37660  365 

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

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

367 
"clearBit w n = set_bit w n False" 
37660  368 

369 

370 
subsection "Shift operations" 

371 

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

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

373 
"sshiftr1 w = word_of_int (bin_rest (sint w))" 
37660  374 

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

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

376 
"bshiftr1 b w = of_bl (b # butlast (to_bl w))" 
37660  377 

378 
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

379 
"w >>> n = (sshiftr1 ^^ n) w" 
37660  380 

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

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

382 
"mask n = (1 << n)  1" 
37660  383 

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

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

385 
"revcast w = of_bl (takefill False (len_of TYPE('b)) (to_bl w))" 
37660  386 

387 
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

388 
"slice1 n w = of_bl (takefill False n (to_bl w))" 
37660  389 

390 
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

391 
"slice n w = slice1 (size w  n) w" 
37660  392 

393 

394 
subsection "Rotation" 

395 

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

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

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

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

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

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

401 
"rotater n = rotater1 ^^ n" 
37660  402 

403 
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

404 
"word_rotr n w = of_bl (rotater n (to_bl w))" 
37660  405 

406 
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

407 
"word_rotl n w = of_bl (rotate n (to_bl w))" 
37660  408 

409 
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

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

411 
else word_rotl (nat ( i)) w)" 
37660  412 

413 

414 
subsection "Split and cat operations" 

415 

416 
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

417 
"word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))" 
37660  418 

419 
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

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

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

422 
(u, v) => (word_of_int u, word_of_int v))" 
37660  423 

424 
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

425 
"word_rcat ws = 
37660  426 
word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))" 
427 

428 
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

429 
"word_rsplit w = 
37660  430 
map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))" 
431 

432 
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

433 
"max_word = word_of_int (2 ^ len_of TYPE('a)  1)" 
37660  434 

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

436 
"of_bool False = 0" 

437 
 "of_bool True = 1" 

438 

45805  439 
(* FIXME: only provide one theorem name *) 
37660  440 
lemmas of_nth_def = word_set_bits_def 
441 

46010  442 
subsection {* Theorems about typedefs *} 
443 

37660  444 
lemma sint_sbintrunc': 
445 
"sint (word_of_int bin :: 'a word) = 

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

447 
unfolding sint_uint 

448 
by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt) 

449 

450 
lemma uint_sint: 

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

452 
unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le) 

453 

46057  454 
lemma bintr_uint: 
455 
fixes w :: "'a::len0 word" 

456 
shows "len_of TYPE('a) \<le> n \<Longrightarrow> bintrunc n (uint w) = uint w" 

37660  457 
apply (subst word_ubin.norm_Rep [symmetric]) 
458 
apply (simp only: bintrunc_bintrunc_min word_size) 

459 
apply (simp add: min_max.inf_absorb2) 

460 
done 

461 

46057  462 
lemma wi_bintr: 
463 
"len_of TYPE('a::len0) \<le> n \<Longrightarrow> 

464 
word_of_int (bintrunc n w) = (word_of_int w :: 'a word)" 

37660  465 
by (clarsimp simp add: word_ubin.norm_eq_iff [symmetric] min_max.inf_absorb1) 
466 

467 
lemma td_ext_sbin: 

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

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

470 
apply (unfold td_ext_def' sint_uint) 

471 
apply (simp add : word_ubin.eq_norm) 

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

473 
apply (auto simp add : sints_def) 

474 
apply (rule sym [THEN trans]) 

475 
apply (rule word_ubin.Abs_norm) 

476 
apply (simp only: bintrunc_sbintrunc) 

477 
apply (drule sym) 

478 
apply simp 

479 
done 

480 

481 
lemmas td_ext_sint = td_ext_sbin 

482 
[simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]] 

483 

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

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

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

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

488 
interpretation word_sint: 

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

490 
word_of_int 

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

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

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

494 
by (rule td_ext_sint) 

495 

496 
interpretation word_sbin: 

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

498 
word_of_int 

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

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

501 
by (rule td_ext_sbin) 

502 

45604  503 
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] 
37660  504 

505 
lemmas td_sint = word_sint.td 

506 

46026
83caa4f4bd56
semiring_numeral_0_eq_0, semiring_numeral_1_eq_1 now [simp], superseeding corresponding simp rules on type nat
haftmann
parents:
46025
diff
changeset

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

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

509 
by (simp add: number_of_eq word_number_of_def) 
37660  510 

46026
83caa4f4bd56
semiring_numeral_0_eq_0, semiring_numeral_1_eq_1 now [simp], superseeding corresponding simp rules on type nat
haftmann
parents:
46025
diff
changeset

511 
declare word_number_of_alt [symmetric, code_abbrev] 
83caa4f4bd56
semiring_numeral_0_eq_0, semiring_numeral_1_eq_1 now [simp], superseeding corresponding simp rules on type nat
haftmann
parents:
46025
diff
changeset

512 

37660  513 
lemma word_no_wi: "number_of = word_of_int" 
44762  514 
by (auto simp: word_number_of_def) 
37660  515 

516 
lemma to_bl_def': 

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

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

44762  519 
by (auto simp: to_bl_def) 
37660  520 

45604  521 
lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of w"] for w 
37660  522 

45805  523 
lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)" 
524 
by (fact uints_def [unfolded no_bintr_alt1]) 

525 

526 
lemma uint_bintrunc [simp]: 

527 
"uint (number_of bin :: 'a word) = 

46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

528 
bintrunc (len_of TYPE ('a :: len0)) (number_of bin)" 
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

529 
unfolding word_number_of_alt by (rule word_ubin.eq_norm) 
37660  530 

45805  531 
lemma sint_sbintrunc [simp]: 
532 
"sint (number_of bin :: 'a word) = 

46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

533 
sbintrunc (len_of TYPE ('a :: len)  1) (number_of bin)" 
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

534 
unfolding word_number_of_alt by (rule word_sbin.eq_norm) 
37660  535 

45805  536 
lemma unat_bintrunc [simp]: 
37660  537 
"unat (number_of bin :: 'a :: len0 word) = 
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

538 
nat (bintrunc (len_of TYPE('a)) (number_of bin))" 
37660  539 
unfolding unat_def nat_number_of_def 
540 
by (simp only: uint_bintrunc) 

541 

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

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

545 
apply (rule box_equals) 

546 
defer 

547 
apply (rule word_ubin.norm_Rep)+ 

548 
apply simp 

549 
done 

550 

45805  551 
lemma uint_ge_0 [iff]: "0 \<le> uint (x::'a::len0 word)" 
552 
using word_uint.Rep [of x] by (simp add: uints_num) 

553 

554 
lemma uint_lt2p [iff]: "uint (x::'a::len0 word) < 2 ^ len_of TYPE('a)" 

555 
using word_uint.Rep [of x] by (simp add: uints_num) 

556 

557 
lemma sint_ge: " (2 ^ (len_of TYPE('a)  1)) \<le> sint (x::'a::len word)" 

558 
using word_sint.Rep [of x] by (simp add: sints_num) 

559 

560 
lemma sint_lt: "sint (x::'a::len word) < 2 ^ (len_of TYPE('a)  1)" 

561 
using word_sint.Rep [of x] by (simp add: sints_num) 

37660  562 

563 
lemma sign_uint_Pls [simp]: 

46604
9f9e85264e4d
make uses of bin_sign respect int/bin distinction
huffman
parents:
46603
diff
changeset

564 
"bin_sign (uint x) = 0" 
37660  565 
by (simp add: sign_Pls_ge_0 number_of_eq) 
566 

45805  567 
lemma uint_m2p_neg: "uint (x::'a::len0 word)  2 ^ len_of TYPE('a) < 0" 
568 
by (simp only: diff_less_0_iff_less uint_lt2p) 

569 

570 
lemma uint_m2p_not_non_neg: 

571 
"\<not> 0 \<le> uint (x::'a::len0 word)  2 ^ len_of TYPE('a)" 

572 
by (simp only: not_le uint_m2p_neg) 

37660  573 

574 
lemma lt2p_lem: 

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

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

45805  578 
lemma uint_le_0_iff [simp]: "uint x \<le> 0 \<longleftrightarrow> uint x = 0" 
579 
by (fact uint_ge_0 [THEN leD, THEN linorder_antisym_conv1]) 

37660  580 

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

581 
lemma uint_nat: "uint w = int (unat w)" 
37660  582 
unfolding unat_def by auto 
583 

584 
lemma uint_number_of: 

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

586 
unfolding word_number_of_alt 

587 
by (simp only: int_word_uint) 

588 

589 
lemma unat_number_of: 

46604
9f9e85264e4d
make uses of bin_sign respect int/bin distinction
huffman
parents:
46603
diff
changeset

590 
"bin_sign (number_of b) = 0 \<Longrightarrow> 
37660  591 
unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)" 
592 
apply (unfold unat_def) 

593 
apply (clarsimp simp only: uint_number_of) 

594 
apply (rule nat_mod_distrib [THEN trans]) 

595 
apply (erule sign_Pls_ge_0 [THEN iffD1]) 

596 
apply (simp_all add: nat_power_eq) 

597 
done 

598 

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

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

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

602 
unfolding word_number_of_alt by (rule int_word_sint) 

603 

45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

604 
lemma word_of_int_0 [simp]: "word_of_int 0 = 0" 
45958  605 
unfolding word_0_wi .. 
606 

45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

607 
lemma word_of_int_1 [simp]: "word_of_int 1 = 1" 
45958  608 
unfolding word_1_wi .. 
609 

37660  610 
lemma word_of_int_bin [simp] : 
611 
"(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)" 

46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

612 
unfolding word_number_of_alt .. 
37660  613 

614 
lemma word_int_case_wi: 

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

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

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

618 

619 
lemma word_int_split: 

620 
"P (word_int_case f x) = 

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

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

623 
unfolding word_int_case_def 

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

625 

626 
lemma word_int_split_asm: 

627 
"P (word_int_case f x) = 

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

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

630 
unfolding word_int_case_def 

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

45805  632 

45604  633 
lemmas uint_range' = word_uint.Rep [unfolded uints_num mem_Collect_eq] 
634 
lemmas sint_range' = word_sint.Rep [unfolded One_nat_def sints_num mem_Collect_eq] 

37660  635 

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

637 
unfolding word_size by (rule uint_range') 

638 

639 
lemma sint_range_size: 

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

641 
unfolding word_size by (rule sint_range') 

642 

45805  643 
lemma sint_above_size: "2 ^ (size (w::'a::len word)  1) \<le> x \<Longrightarrow> sint w < x" 
644 
unfolding word_size by (rule less_le_trans [OF sint_lt]) 

645 

646 
lemma sint_below_size: 

647 
"x \<le>  (2 ^ (size (w::'a::len word)  1)) \<Longrightarrow> x \<le> sint w" 

648 
unfolding word_size by (rule order_trans [OF _ sint_ge]) 

37660  649 

46010  650 
subsection {* Testing bits *} 
651 

37660  652 
lemma test_bit_eq_iff: "(test_bit (u::'a::len0 word) = test_bit v) = (u = v)" 
653 
unfolding word_test_bit_def by (simp add: bin_nth_eq_iff) 

654 

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

656 
apply (unfold word_test_bit_def) 

657 
apply (subst word_ubin.norm_Rep [symmetric]) 

658 
apply (simp only: nth_bintr word_size) 

659 
apply fast 

660 
done 

661 

46021  662 
lemma word_eq_iff: 
663 
fixes x y :: "'a::len0 word" 

664 
shows "x = y \<longleftrightarrow> (\<forall>n<len_of TYPE('a). x !! n = y !! n)" 

665 
unfolding uint_inject [symmetric] bin_eq_iff word_test_bit_def [symmetric] 

666 
by (metis test_bit_size [unfolded word_size]) 

667 

46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46022
diff
changeset

668 
lemma word_eqI [rule_format]: 
37660  669 
fixes u :: "'a::len0 word" 
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

670 
shows "(ALL n. n < size u > u !! n = v !! n) \<Longrightarrow> u = v" 
46021  671 
by (simp add: word_size word_eq_iff) 
37660  672 

45805  673 
lemma word_eqD: "(u::'a::len0 word) = v \<Longrightarrow> u !! x = v !! x" 
674 
by simp 

37660  675 

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

677 
unfolding word_test_bit_def word_size 

678 
by (simp add: nth_bintr [symmetric]) 

679 

680 
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] 

681 

46057  682 
lemma bin_nth_uint_imp: 
683 
"bin_nth (uint (w::'a::len0 word)) n \<Longrightarrow> n < len_of TYPE('a)" 

37660  684 
apply (rule nth_bintr [THEN iffD1, THEN conjunct1]) 
685 
apply (subst word_ubin.norm_Rep) 

686 
apply assumption 

687 
done 

688 

46057  689 
lemma bin_nth_sint: 
690 
fixes w :: "'a::len word" 

691 
shows "len_of TYPE('a) \<le> n \<Longrightarrow> 

692 
bin_nth (sint w) n = bin_nth (sint w) (len_of TYPE('a)  1)" 

37660  693 
apply (subst word_sbin.norm_Rep [symmetric]) 
46057  694 
apply (auto simp add: nth_sbintr) 
37660  695 
done 
696 

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

698 
lemma td_bl: 

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

700 
of_bl 

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

702 
apply (unfold type_definition_def of_bl_def to_bl_def) 

703 
apply (simp add: word_ubin.eq_norm) 

704 
apply safe 

705 
apply (drule sym) 

706 
apply simp 

707 
done 

708 

709 
interpretation word_bl: 

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

711 
of_bl 

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

713 
by (rule td_bl) 

714 

45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

715 
lemmas word_bl_Rep' = word_bl.Rep [unfolded mem_Collect_eq, iff] 
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamicallyscoped [simplified];
wenzelm
parents:
45529
diff
changeset

716 

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

717 
lemma word_size_bl: "size w = size (to_bl w)" 
37660  718 
unfolding word_size by auto 
719 

720 
lemma to_bl_use_of_bl: 

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

45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

722 
by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq]) 
37660  723 

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

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

726 

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

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

729 

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

730 
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w" 
37660  731 
by auto 
732 

45805  733 
lemma word_rev_gal': "u = word_reverse w \<Longrightarrow> w = word_reverse u" 
734 
by simp 

735 

736 
lemma length_bl_gt_0 [iff]: "0 < length (to_bl (x::'a::len word))" 

737 
unfolding word_bl_Rep' by (rule len_gt_0) 

738 

739 
lemma bl_not_Nil [iff]: "to_bl (x::'a::len word) \<noteq> []" 

740 
by (fact length_bl_gt_0 [unfolded length_greater_0_conv]) 

741 

742 
lemma length_bl_neq_0 [iff]: "length (to_bl (x::'a::len word)) \<noteq> 0" 

743 
by (fact length_bl_gt_0 [THEN gr_implies_not0]) 

37660  744 

46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

745 
lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = 1)" 
37660  746 
apply (unfold to_bl_def sint_uint) 
747 
apply (rule trans [OF _ bl_sbin_sign]) 

748 
apply simp 

749 
done 

750 

751 
lemma of_bl_drop': 

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

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

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

756 
done 

757 

45805  758 
lemma of_bl_no: "of_bl bl = number_of (bl_to_bin bl)" 
759 
by (fact of_bl_def [folded word_number_of_def]) 

37660  760 

761 
lemma test_bit_of_bl: 

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

763 
apply (unfold of_bl_def word_test_bit_def) 

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

765 
done 

766 

767 
lemma no_of_bl: 

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

769 
unfolding word_size of_bl_no by (simp add : word_number_of_def) 

770 

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

771 
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)" 
37660  772 
unfolding word_size to_bl_def by auto 
773 

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

775 
unfolding uint_bl by (simp add : word_size) 

776 

777 
lemma to_bl_of_bin: 

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

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

780 

45805  781 
lemma to_bl_no_bin [simp]: 
46618  782 
"to_bl (number_of bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) (number_of bin)" 
783 
unfolding word_number_of_alt by (rule to_bl_of_bin) 

37660  784 

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

786 
unfolding uint_bl by (simp add : word_size) 

46011  787 

788 
lemma uint_bl_bin: 

789 
fixes x :: "'a::len0 word" 

790 
shows "bl_to_bin (bin_to_bl (len_of TYPE('a)) (uint x)) = uint x" 

791 
by (rule trans [OF bin_bl_bin word_ubin.norm_Rep]) 

45604  792 

37660  793 
(* naturals *) 
794 
lemma uints_unats: "uints n = int ` unats n" 

795 
apply (unfold unats_def uints_num) 

796 
apply safe 

797 
apply (rule_tac image_eqI) 

798 
apply (erule_tac nat_0_le [symmetric]) 

799 
apply auto 

800 
apply (erule_tac nat_less_iff [THEN iffD2]) 

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

802 
apply (auto simp add : nat_power_eq int_power) 

803 
done 

804 

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

806 
by (auto simp add : uints_unats image_iff) 

807 

45604  808 
lemmas bintr_num = word_ubin.norm_eq_iff [symmetric, folded word_number_of_def] 
809 
lemmas sbintr_num = word_sbin.norm_eq_iff [symmetric, folded word_number_of_def] 

810 

811 
lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def] 

812 
lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def] 

37660  813 

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

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

816 

817 
lemma num_of_bintr': 

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

818 
"bintrunc (len_of TYPE('a :: len0)) a = b \<Longrightarrow> 
37660  819 
number_of a = (number_of b :: 'a word)" 
820 
apply safe 

821 
apply (rule_tac num_of_bintr [symmetric]) 

822 
done 

823 

824 
lemma num_of_sbintr': 

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

825 
"sbintrunc (len_of TYPE('a :: len)  1) a = b \<Longrightarrow> 
37660  826 
number_of a = (number_of b :: 'a word)" 
827 
apply safe 

828 
apply (rule_tac num_of_sbintr [symmetric]) 

829 
done 

830 

45604  831 
lemmas num_abs_bintr = sym [THEN trans, OF num_of_bintr word_number_of_def] 
832 
lemmas num_abs_sbintr = sym [THEN trans, OF num_of_sbintr word_number_of_def] 

37660  833 

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

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

836 

837 
lemma ucast_id: "ucast w = w" 

838 
unfolding ucast_def by auto 

839 

840 
lemma scast_id: "scast w = w" 

841 
unfolding scast_def by auto 

842 

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

843 
lemma ucast_bl: "ucast w = of_bl (to_bl w)" 
37660  844 
unfolding ucast_def of_bl_def uint_bl 
845 
by (auto simp add : word_size) 

846 

847 
lemma nth_ucast: 

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

849 
apply (unfold ucast_def test_bit_bin) 

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

851 
apply (fast elim!: bin_nth_uint_imp) 

852 
done 

853 

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

855 

46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

856 
lemma ucast_bintr [simp]: 
37660  857 
"ucast (number_of w ::'a::len0 word) = 
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

858 
word_of_int (bintrunc (len_of TYPE('a)) (number_of w))" 
37660  859 
unfolding ucast_def by simp 
860 

46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

861 
lemma scast_sbintr [simp]: 
37660  862 
"scast (number_of w ::'a::len word) = 
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

863 
word_of_int (sbintrunc (len_of TYPE('a)  Suc 0) (number_of w))" 
37660  864 
unfolding scast_def by simp 
865 

46011  866 
lemma source_size: "source_size (c::'a::len0 word \<Rightarrow> _) = len_of TYPE('a)" 
867 
unfolding source_size_def word_size Let_def .. 

868 

869 
lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len0 word) = len_of TYPE('b)" 

870 
unfolding target_size_def word_size Let_def .. 

871 

872 
lemma is_down: 

873 
fixes c :: "'a::len0 word \<Rightarrow> 'b::len0 word" 

874 
shows "is_down c \<longleftrightarrow> len_of TYPE('b) \<le> len_of TYPE('a)" 

875 
unfolding is_down_def source_size target_size .. 

876 

877 
lemma is_up: 

878 
fixes c :: "'a::len0 word \<Rightarrow> 'b::len0 word" 

879 
shows "is_up c \<longleftrightarrow> len_of TYPE('a) \<le> len_of TYPE('b)" 

880 
unfolding is_up_def source_size target_size .. 

37660  881 

45604  882 
lemmas is_up_down = trans [OF is_up is_down [symmetric]] 
37660  883 

45811  884 
lemma down_cast_same [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc = scast" 
37660  885 
apply (unfold is_down) 
886 
apply safe 

887 
apply (rule ext) 

888 
apply (unfold ucast_def scast_def uint_sint) 

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

890 
apply simp 

891 
done 

892 

45811  893 
lemma word_rev_tf: 
894 
"to_bl (of_bl bl::'a::len0 word) = 

895 
rev (takefill False (len_of TYPE('a)) (rev bl))" 

37660  896 
unfolding of_bl_def uint_bl 
897 
by (clarsimp simp add: bl_bin_bl_rtf word_ubin.eq_norm word_size) 

898 

45811  899 
lemma word_rep_drop: 
900 
"to_bl (of_bl bl::'a::len0 word) = 

901 
replicate (len_of TYPE('a)  length bl) False @ 

902 
drop (length bl  len_of TYPE('a)) bl" 

903 
by (simp add: word_rev_tf takefill_alt rev_take) 

37660  904 

905 
lemma to_bl_ucast: 

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

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

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

909 
apply (unfold ucast_bl) 

910 
apply (rule trans) 

911 
apply (rule word_rep_drop) 

912 
apply simp 

913 
done 

914 

45811  915 
lemma ucast_up_app [OF refl]: 
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

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

919 

45811  920 
lemma ucast_down_drop [OF refl]: 
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

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

924 

45811  925 
lemma scast_down_drop [OF refl]: 
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

926 
"sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> 
37660  927 
to_bl (sc w) = drop n (to_bl w)" 
928 
apply (subgoal_tac "sc = ucast") 

929 
apply safe 

930 
apply simp 

45811  931 
apply (erule ucast_down_drop) 
932 
apply (rule down_cast_same [symmetric]) 

37660  933 
apply (simp add : source_size target_size is_down) 
934 
done 

935 

45811  936 
lemma sint_up_scast [OF refl]: 
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

937 
"sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w" 
37660  938 
apply (unfold is_up) 
939 
apply safe 

940 
apply (simp add: scast_def word_sbin.eq_norm) 

941 
apply (rule box_equals) 

942 
prefer 3 

943 
apply (rule word_sbin.norm_Rep) 

944 
apply (rule sbintrunc_sbintrunc_l) 

945 
defer 

946 
apply (subst word_sbin.norm_Rep) 

947 
apply (rule refl) 

948 
apply simp 

949 
done 

950 

45811  951 
lemma uint_up_ucast [OF refl]: 
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

952 
"uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w" 
37660  953 
apply (unfold is_up) 
954 
apply safe 

955 
apply (rule bin_eqI) 

956 
apply (fold word_test_bit_def) 

957 
apply (auto simp add: nth_ucast) 

958 
apply (auto simp add: test_bit_bin) 

959 
done 

45811  960 

961 
lemma ucast_up_ucast [OF refl]: 

962 
"uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> ucast (uc w) = ucast w" 

37660  963 
apply (simp (no_asm) add: ucast_def) 
964 
apply (clarsimp simp add: uint_up_ucast) 

965 
done 

966 

45811  967 
lemma scast_up_scast [OF refl]: 
968 
"sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> scast (sc w) = scast w" 

37660  969 
apply (simp (no_asm) add: scast_def) 
970 
apply (clarsimp simp add: sint_up_scast) 

971 
done 

972 

45811  973 
lemma ucast_of_bl_up [OF refl]: 
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

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

977 
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] 

978 
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] 

979 

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

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

982 
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] 

983 
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] 

984 

985 
lemma up_ucast_surj: 

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

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

989 

990 
lemma up_scast_surj: 

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

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

994 

995 
lemma down_scast_inj: 

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

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

999 

1000 
lemma down_ucast_inj: 

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

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

1004 

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

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

45811  1007 

1008 
lemma ucast_down_no [OF refl]: 

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

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

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

1013 
apply (erule bintrunc_bintrunc_ge) 

1014 
done 

45811  1015 

1016 
lemma ucast_down_bl [OF refl]: 

1017 
"uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl" 

37660  1018 
unfolding of_bl_no by clarify (erule ucast_down_no) 
1019 

1020 
lemmas slice_def' = slice_def [unfolded word_size] 

1021 
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] 

1022 

1023 
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def 

1024 

1025 
text {* Executable equality *} 

1026 

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

1027 
instantiation word :: (len0) equal 
24333  1028 
begin 
1029 

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

1030 
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

1031 
"equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" 
37660  1032 

1033 
instance proof 

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

1034 
qed (simp add: equal equal_word_def) 
37660  1035 

1036 
end 

1037 

1038 

1039 
subsection {* Word Arithmetic *} 

1040 

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

46012  1042 
unfolding word_less_def word_le_def by (simp add: less_le) 
37660  1043 

1044 
lemma signed_linorder: "class.linorder word_sle word_sless" 

46124  1045 
by default (unfold word_sle_def word_sless_def, auto) 
37660  1046 

1047 
interpretation signed: linorder "word_sle" "word_sless" 

1048 
by (rule signed_linorder) 

1049 

1050 
lemma udvdI: 

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

1051 
"0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" 
37660  1052 
by (auto simp: udvd_def) 
1053 

45604  1054 
lemmas word_div_no [simp] = word_div_def [of "number_of a" "number_of b"] for a b 
1055 

1056 
lemmas word_mod_no [simp] = word_mod_def [of "number_of a" "number_of b"] for a b 

1057 

1058 
lemmas word_less_no [simp] = word_less_def [of "number_of a" "number_of b"] for a b 

1059 

1060 
lemmas word_le_no [simp] = word_le_def [of "number_of a" "number_of b"] for a b 

1061 

1062 
lemmas word_sless_no [simp] = word_sless_def [of "number_of a" "number_of b"] for a b 

1063 

1064 
lemmas word_sle_no [simp] = word_sle_def [of "number_of a" "number_of b"] for a b 

37660  1065 

1066 
(* following two are available in class number_ring, 

1067 
but convenient to have them here here; 

1068 
note  the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1 

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

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

1071 
(simp add: word_1_no del: numeral_1_eq_1) 

1072 
*) 

45958  1073 
lemma word_0_wi_Pls: "0 = word_of_int Int.Pls" 
1074 
by (simp only: Pls_def word_0_wi) 

1075 

1076 
lemma word_0_no: "(0::'a::len0 word) = Numeral0" 

45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1077 
by (simp add: word_number_of_alt) 
37660  1078 

46020  1079 
lemma word_1_no: "(1::'a::len0 word) = Numeral1" 
1080 
by (simp add: word_number_of_alt) 

37660  1081 

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

1082 
lemma word_m1_wi: "1 = word_of_int 1" 
37660  1083 
by (rule word_number_of_alt) 
1084 

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

1086 
by (simp add: word_m1_wi number_of_eq) 

1087 

45805  1088 
lemma word_0_bl [simp]: "of_bl [] = 0" 
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1089 
unfolding of_bl_def by (simp add: Pls_def) 
37660  1090 

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

45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1092 
unfolding of_bl_def 
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1093 
by (simp add: bl_to_bin_def Bit_def Pls_def) 
37660  1094 

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

1096 
unfolding word_0_wi 

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

1098 

45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1099 
lemma of_bl_0 [simp]: "of_bl (replicate n False) = 0" 
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1100 
by (simp add: of_bl_def bl_to_bin_rep_False Pls_def) 
37660  1101 

45805  1102 
lemma to_bl_0 [simp]: 
37660  1103 
"to_bl (0::'a::len0 word) = replicate (len_of TYPE('a)) False" 
1104 
unfolding uint_bl 

46617
8c5d10d41391
make bool list functions respect int/bin distinction
huffman
parents:
46604
diff
changeset

1105 
by (simp add: word_size bin_to_bl_zero) 
37660  1106 

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

1108 
by (auto intro!: word_uint.Rep_eqD) 

1109 

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

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

1112 

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

1114 
unfolding unat_def by auto 

1115 

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

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

1119 
defer 

1120 
apply (rule word_uint.Rep_inverse)+ 

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

1122 
apply simp 

1123 
done 

1124 

45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

1125 
lemmas size_0_same = size_0_same' [unfolded word_size] 
37660  1126 

1127 
lemmas unat_eq_0 = unat_0_iff 

1128 
lemmas unat_eq_zero = unat_0_iff 

1129 

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

1131 
by (auto simp: unat_0_iff [symmetric]) 

1132 

45958  1133 
lemma ucast_0 [simp]: "ucast 0 = 0" 
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1134 
unfolding ucast_def by simp 
45958  1135 

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

1137 
unfolding sint_uint by simp 

1138 

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

45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1140 
unfolding scast_def by simp 
37660  1141 

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

45958  1143 
unfolding word_m1_wi by (simp add: word_sbin.eq_norm) 
1144 

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

1146 
unfolding scast_def by simp 

1147 

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

37660  1149 
unfolding word_1_wi 
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1150 
by (simp add: word_ubin.eq_norm bintrunc_minus_simps del: word_of_int_1) 
45958  1151 

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

1153 
unfolding unat_def by simp 

1154 

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

45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1156 
unfolding ucast_def by simp 
37660  1157 

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

1159 

1160 
lemmas word_arith_alts = 

46000  1161 
word_sub_wi 
1162 
word_arith_wis (* FIXME: duplicate *) 

1163 

1164 
lemmas word_succ_alt = word_succ_def (* FIXME: duplicate *) 

1165 
lemmas word_pred_alt = word_pred_def (* FIXME: duplicate *) 

37660  1166 

1167 
subsection "Transferring goals from words to ints" 

1168 

1169 
lemma word_ths: 

1170 
shows 

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

1172 
word_pred_m1: "word_pred a = a  1" and 

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

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

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

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

1177 
rule word_uint.Abs_cases [of a], 

46000  1178 
simp add: add_commute mult_commute 
46009  1179 
ring_distribs word_of_int_homs 
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1180 
del: word_of_int_0 word_of_int_1)+ 
37660  1181 

45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

1182 
lemma uint_cong: "x = y \<Longrightarrow> uint x = uint y" 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

1183 
by simp 
37660  1184 

1185 
lemmas uint_word_ariths = 

45604  1186 
word_arith_alts [THEN trans [OF uint_cong int_word_uint]] 
37660  1187 

1188 
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p] 

1189 

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

1191 
lemmas sint_word_ariths = uint_word_arith_bintrs 

1192 
[THEN uint_sint [symmetric, THEN trans], 

1193 
unfolded uint_sint bintr_arith1s bintr_ariths 

45604  1194 
len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep] 
1195 

1196 
lemmas uint_div_alt = word_div_def [THEN trans [OF uint_cong int_word_uint]] 

1197 
lemmas uint_mod_alt = word_mod_def [THEN trans [OF uint_cong int_word_uint]] 

37660  1198 

1199 
lemma word_pred_0_n1: "word_pred 0 = word_of_int 1" 

45550
73a4f31d41c4
Word.thy: reduce usage of numeralrepresentationdependent thms like number_of_is_id in proofs
huffman
parents:
45549
diff
changeset

1200 
unfolding word_pred_def uint_eq_0 pred_def by simp 
37660  1201 

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

1203 
by (simp add: word_pred_0_n1 number_of_eq) 

1204 

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

1206 
unfolding Min_def by (simp only: word_of_int_hom_syms) 

1207 

1208 
lemma succ_pred_no [simp]: 

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

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

46000  1211 
unfolding word_number_of_def Int.succ_def Int.pred_def 
46009  1212 
by (simp add: word_of_int_homs) 
37660  1213 

1214 
lemma word_sp_01 [simp] : 

1215 
"word_succ 1 = 0 & word_succ 0 = 1 & word_pred 0 = 1 & word_pred 1 = 0" 

46020  1216 
unfolding word_0_no word_1_no by simp 
37660  1217 

1218 
(* alternative approach to lifting arithmetic equalities *) 

1219 
lemma word_of_int_Ex: 

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

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

1222 

1223 

1224 
subsection "Order on fixedlength words" 

1225 

1226 
lemma word_zero_le [simp] : 

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

1228 
unfolding word_le_def by auto 

1229 

45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

1230 
lemma word_m1_ge [simp] : "word_pred 0 >= y" (* FIXME: delete *) 
37660  1231 
unfolding word_le_def 
1232 
by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto 

1233 

45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

1234 
lemma word_n1_ge [simp]: "y \<le> (1::'a::len0 word)" 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

1235 
unfolding word_le_def 
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset

1236 
by (simp only: word_m1_wi word_uint.eq_norm m1mod2k) auto 
37660  1237 

1238 
lemmas word_not_simps [simp] = 

1239 
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] 

1240 

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

1242 
unfolding word_less_def by auto 

1243 

45604  1244 
lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of y"] for y 
37660  1245 

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

1246 
lemma word_sless_alt: "(a <s b) = (sint a < sint b)" 
37660  1247 
unfolding word_sle_def word_sless_def 
1248 
by (auto simp add: less_le) 

1249 

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

1251 
unfolding unat_def word_le_def 

1252 
by (rule nat_le_eq_zle [symmetric]) simp 

1253 

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

1255 
unfolding unat_def word_less_alt 

1256 
by (rule nat_less_eq_zless [symmetric]) simp 

1257 

1258 
lemma wi_less: 

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

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

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

1262 

1263 
lemma wi_le: 

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

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

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

1267 

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

1269 
apply (unfold udvd_def) 

1270 
apply safe 

1271 
apply (simp add: unat_def nat_mult_distrib) 

1272 
apply (simp add: uint_nat int_mult) 

1273 
apply (rule exI) 

1274 
apply safe 

1275 
prefer 2 

1276 
apply (erule notE) 

1277 
apply (rule refl) 

1278 
apply force 

1279 
done 

1280 

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

1282 
unfolding dvd_def udvd_nat_alt by force 

1283 

45604  1284 
lemmas unat_mono = word_less_nat_alt [THEN iffD1] 
37660  1285 

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

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

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

1290 
prefer 2 

1291 
apply (drule contrapos_nn) 

1292 
apply (erule word_uint.Rep_inverse' [symmetric]) 

1293 
apply (insert uint_ge_0 [of x])[1] 

1294 
apply arith 

1295 
apply (rule box_equals) 

1296 
apply (rule nat_diff_distrib) 

1297 
prefer 2 

1298 
apply assumption 

1299 
apply simp 

1300 
apply (subst mod_pos_pos_trivial) 

1301 
apply arith 

1302 
apply (insert uint_lt2p [of x])[1] 

1303 
apply arith 

1304 
apply (rule refl) 

1305 
apply simp 

1306 
done 

1307 

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

1308 
lemma measure_unat: "p ~= 0 \<Longrightarrow> unat (p  1) < unat p" 
37660  1309 
by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric]) 
1310 

45604  1311 
lemmas uint_add_ge0 [simp] = add_nonneg_nonneg [OF uint_ge_0 uint_ge_0] 
1312 
lemmas uint_mult_ge0 [simp] = mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0] 

37660  1313 

1314 
lemma uint_sub_lt2p [simp]: 

1315 
"uint (x :: 'a :: len0 word)  uint (y :: 'b :: len0 word) < 

1316 
2 ^ len_of TYPE('a)" 

1317 
using uint_ge_0 [of y] uint_lt2p [of x] by arith 

1318 

1319 

1320 
subsection "Conditions for the addition (etc) of two words to overflow" 

1321 

1322 
lemma uint_add_lem: 

1323 
"(uint x + uint y < 2 ^ len_of TYPE('a)) = 

1324 
(uint (x + y :: 'a :: len0 word) = uint x + uint y)" 

1325 
by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem]) 

1326 

1327 
lemma uint_mult_lem: 

1328 
"(uint x * uint y < 2 ^ len_of TYPE('a)) = 

1329 
(uint (x * y :: 'a :: len0 word) = uint x * uint y)" 

1330 
by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem]) 

1331 

1332 
lemma uint_sub_lem: 

56e3520b68b2
one unified Word theory
haftm 