author  wenzelm 
Fri, 06 Jan 2012 16:42:15 +0100  
changeset 46136  a3d4cf5203f5 
parent 46124  3ee75fe01986 
child 46172  c06e868dc339 
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]: 

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

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: 

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

590 
"bin_sign b = Int.Pls \<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]: 
782 
"to_bl (number_of bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin" 

783 
by (fact to_bl_of_bin [folded word_number_of_def]) 

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 

45805  793 
(* FIXME: the next two lemmas should be unnecessary, because the lhs 
794 
terms should never occur in practice *) 

795 
lemma num_AB_u [simp]: "number_of (uint x) = x" 

796 
unfolding word_number_of_def by (rule word_uint.Rep_inverse) 

797 

798 
lemma num_AB_s [simp]: "number_of (sint x) = x" 

799 
unfolding word_number_of_def by (rule word_sint.Rep_inverse) 

37660  800 

801 
(* naturals *) 

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

803 
apply (unfold unats_def uints_num) 

804 
apply safe 

805 
apply (rule_tac image_eqI) 

806 
apply (erule_tac nat_0_le [symmetric]) 

807 
apply auto 

808 
apply (erule_tac nat_less_iff [THEN iffD2]) 

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

810 
apply (auto simp add : nat_power_eq int_power) 

811 
done 

812 

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

814 
by (auto simp add : uints_unats image_iff) 

815 

45604  816 
lemmas bintr_num = word_ubin.norm_eq_iff [symmetric, folded word_number_of_def] 
817 
lemmas sbintr_num = word_sbin.norm_eq_iff [symmetric, folded word_number_of_def] 

818 

819 
lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def] 

820 
lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def] 

37660  821 

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

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

824 

825 
lemma num_of_bintr': 

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

826 
"bintrunc (len_of TYPE('a :: len0)) a = b \<Longrightarrow> 
37660  827 
number_of a = (number_of b :: 'a word)" 
828 
apply safe 

829 
apply (rule_tac num_of_bintr [symmetric]) 

830 
done 

831 

832 
lemma num_of_sbintr': 

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

833 
"sbintrunc (len_of TYPE('a :: len)  1) a = b \<Longrightarrow> 
37660  834 
number_of a = (number_of b :: 'a word)" 
835 
apply safe 

836 
apply (rule_tac num_of_sbintr [symmetric]) 

837 
done 

838 

45604  839 
lemmas num_abs_bintr = sym [THEN trans, OF num_of_bintr word_number_of_def] 
840 
lemmas num_abs_sbintr = sym [THEN trans, OF num_of_sbintr word_number_of_def] 

37660  841 

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

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

844 

845 
lemma ucast_id: "ucast w = w" 

846 
unfolding ucast_def by auto 

847 

848 
lemma scast_id: "scast w = w" 

849 
unfolding scast_def by auto 

850 

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

851 
lemma ucast_bl: "ucast w = of_bl (to_bl w)" 
37660  852 
unfolding ucast_def of_bl_def uint_bl 
853 
by (auto simp add : word_size) 

854 

855 
lemma nth_ucast: 

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

857 
apply (unfold ucast_def test_bit_bin) 

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

859 
apply (fast elim!: bin_nth_uint_imp) 

860 
done 

861 

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

863 

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

864 
lemma ucast_bintr [simp]: 
37660  865 
"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

866 
word_of_int (bintrunc (len_of TYPE('a)) (number_of w))" 
37660  867 
unfolding ucast_def by simp 
868 

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

869 
lemma scast_sbintr [simp]: 
37660  870 
"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

871 
word_of_int (sbintrunc (len_of TYPE('a)  Suc 0) (number_of w))" 
37660  872 
unfolding scast_def by simp 
873 

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

876 

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

878 
unfolding target_size_def word_size Let_def .. 

879 

880 
lemma is_down: 

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

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

883 
unfolding is_down_def source_size target_size .. 

884 

885 
lemma is_up: 

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

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

888 
unfolding is_up_def source_size target_size .. 

37660  889 

45604  890 
lemmas is_up_down = trans [OF is_up is_down [symmetric]] 
37660  891 

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

895 
apply (rule ext) 

896 
apply (unfold ucast_def scast_def uint_sint) 

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

898 
apply simp 

899 
done 

900 

45811  901 
lemma word_rev_tf: 
902 
"to_bl (of_bl bl::'a::len0 word) = 

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

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

906 

45811  907 
lemma word_rep_drop: 
908 
"to_bl (of_bl bl::'a::len0 word) = 

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

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

911 
by (simp add: word_rev_tf takefill_alt rev_take) 

37660  912 

913 
lemma to_bl_ucast: 

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

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

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

917 
apply (unfold ucast_bl) 

918 
apply (rule trans) 

919 
apply (rule word_rep_drop) 

920 
apply simp 

921 
done 

922 

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

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

927 

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

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

932 

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

934 
"sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> 
37660  935 
to_bl (sc w) = drop n (to_bl w)" 
936 
apply (subgoal_tac "sc = ucast") 

937 
apply safe 

938 
apply simp 

45811  939 
apply (erule ucast_down_drop) 
940 
apply (rule down_cast_same [symmetric]) 

37660  941 
apply (simp add : source_size target_size is_down) 
942 
done 

943 

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

945 
"sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w" 
37660  946 
apply (unfold is_up) 
947 
apply safe 

948 
apply (simp add: scast_def word_sbin.eq_norm) 

949 
apply (rule box_equals) 

950 
prefer 3 

951 
apply (rule word_sbin.norm_Rep) 

952 
apply (rule sbintrunc_sbintrunc_l) 

953 
defer 

954 
apply (subst word_sbin.norm_Rep) 

955 
apply (rule refl) 

956 
apply simp 

957 
done 

958 

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

960 
"uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w" 
37660  961 
apply (unfold is_up) 
962 
apply safe 

963 
apply (rule bin_eqI) 

964 
apply (fold word_test_bit_def) 

965 
apply (auto simp add: nth_ucast) 

966 
apply (auto simp add: test_bit_bin) 

967 
done 

45811  968 

969 
lemma ucast_up_ucast [OF refl]: 

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

37660  971 
apply (simp (no_asm) add: ucast_def) 
972 
apply (clarsimp simp add: uint_up_ucast) 

973 
done 

974 

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

37660  977 
apply (simp (no_asm) add: scast_def) 
978 
apply (clarsimp simp add: sint_up_scast) 

979 
done 

980 

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

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

985 
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] 

986 
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] 

987 

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

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

990 
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] 

991 
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] 

992 

993 
lemma up_ucast_surj: 

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

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

997 

998 
lemma up_scast_surj: 

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

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

1002 

1003 
lemma down_scast_inj: 

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

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

1007 

1008 
lemma down_ucast_inj: 

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

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

1012 

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

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

45811  1015 

1016 
lemma ucast_down_no [OF refl]: 

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

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

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

1021 
apply (erule bintrunc_bintrunc_ge) 

1022 
done 

45811  1023 

1024 
lemma ucast_down_bl [OF refl]: 

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

37660  1026 
unfolding of_bl_no by clarify (erule ucast_down_no) 
1027 

1028 
lemmas slice_def' = slice_def [unfolded word_size] 

1029 
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] 

1030 

1031 
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def 

1032 

1033 
text {* Executable equality *} 

1034 

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

1035 
instantiation word :: (len0) equal 
24333  1036 
begin 
1037 

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

1038 
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

1039 
"equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" 
37660  1040 

1041 
instance proof 

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

1042 
qed (simp add: equal equal_word_def) 
37660  1043 

1044 
end 

1045 

1046 

1047 
subsection {* Word Arithmetic *} 

1048 

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

46012  1050 
unfolding word_less_def word_le_def by (simp add: less_le) 
37660  1051 

1052 
lemma signed_linorder: "class.linorder word_sle word_sless" 

46124  1053 
by default (unfold word_sle_def word_sless_def, auto) 
37660  1054 

1055 
interpretation signed: linorder "word_sle" "word_sless" 

1056 
by (rule signed_linorder) 

1057 

1058 
lemma udvdI: 

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

1059 
"0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" 
37660  1060 
by (auto simp: udvd_def) 
1061 

45604  1062 
lemmas word_div_no [simp] = word_div_def [of "number_of a" "number_of b"] for a b 
1063 

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

1065 

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

1067 

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

1069 

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

1071 

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

37660  1073 

1074 
(* following two are available in class number_ring, 

1075 
but convenient to have them here here; 

1076 
note  the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1 

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

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

1079 
(simp add: word_1_no del: numeral_1_eq_1) 

1080 
*) 

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

1083 

1084 
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

1085 
by (simp add: word_number_of_alt) 
37660  1086 

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

37660  1089 

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

1090 
lemma word_m1_wi: "1 = word_of_int 1" 
37660  1091 
by (rule word_number_of_alt) 
1092 

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

1094 
by (simp add: word_m1_wi number_of_eq) 

1095 

45805  1096 
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

1097 
unfolding of_bl_def by (simp add: Pls_def) 
37660  1098 

1099 
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

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

1101 
by (simp add: bl_to_bin_def Bit_def Pls_def) 
37660  1102 

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

1104 
unfolding word_0_wi 

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

1106 

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

1107 
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

1108 
by (simp add: of_bl_def bl_to_bin_rep_False Pls_def) 
37660  1109 

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

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

1114 

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

1116 
by (auto intro!: word_uint.Rep_eqD) 

1117 

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

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

1120 

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

1122 
unfolding unat_def by auto 

1123 

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

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

1127 
defer 

1128 
apply (rule word_uint.Rep_inverse)+ 

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

1130 
apply simp 

1131 
done 

1132 

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

1133 
lemmas size_0_same = size_0_same' [unfolded word_size] 
37660  1134 

1135 
lemmas unat_eq_0 = unat_0_iff 

1136 
lemmas unat_eq_zero = unat_0_iff 

1137 

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

1139 
by (auto simp: unat_0_iff [symmetric]) 

1140 

45958  1141 
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

1142 
unfolding ucast_def by simp 
45958  1143 

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

1145 
unfolding sint_uint by simp 

1146 

1147 
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

1148 
unfolding scast_def by simp 
37660  1149 

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

45958  1151 
unfolding word_m1_wi by (simp add: word_sbin.eq_norm) 
1152 

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

1154 
unfolding scast_def by simp 

1155 

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

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

1158 
by (simp add: word_ubin.eq_norm bintrunc_minus_simps del: word_of_int_1) 
45958  1159 

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

1161 
unfolding unat_def by simp 

1162 

1163 
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

1164 
unfolding ucast_def by simp 
37660  1165 

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

1167 

1168 
lemmas word_arith_alts = 

46000  1169 
word_sub_wi 
1170 
word_arith_wis (* FIXME: duplicate *) 

1171 

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

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

37660  1174 

1175 
subsection "Transferring goals from words to ints" 

1176 

1177 
lemma word_ths: 

1178 
shows 

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

1180 
word_pred_m1: "word_pred a = a  1" and 

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

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

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

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

1185 
rule word_uint.Abs_cases [of a], 

46000  1186 
simp add: add_commute mult_commute 
46009  1187 
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

1188 
del: word_of_int_0 word_of_int_1)+ 
37660  1189 

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

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

1191 
by simp 
37660  1192 

1193 
lemmas uint_word_ariths = 

45604  1194 
word_arith_alts [THEN trans [OF uint_cong int_word_uint]] 
37660  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 

45604  1202 
len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep] 
1203 

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

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

37660  1206 

1207 
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

1208 
unfolding word_pred_def uint_eq_0 pred_def by simp 
37660  1209 

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

1211 
by (simp add: word_pred_0_n1 number_of_eq) 

1212 

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

1214 
unfolding Min_def by (simp only: word_of_int_hom_syms) 

1215 

1216 
lemma succ_pred_no [simp]: 

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

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

46000  1219 
unfolding word_number_of_def Int.succ_def Int.pred_def 
46009  1220 
by (simp add: word_of_int_homs) 
37660  1221 

1222 
lemma word_sp_01 [simp] : 

1223 
"word_succ 1 = 0 & word_succ 0 = 1 & word_pred 0 = 1 & word_pred 1 = 0" 

46020  1224 
unfolding word_0_no word_1_no by simp 
37660  1225 

1226 
(* alternative approach to lifting arithmetic equalities *) 

1227 
lemma word_of_int_Ex: 

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

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

1230 

1231 

1232 
subsection "Order on fixedlength words" 

1233 

1234 
lemma word_zero_le [simp] : 

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

1236 
unfolding word_le_def by auto 

1237 

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

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

1241 

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

1242 
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

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

1244 
by (simp only: word_m1_wi word_uint.eq_norm m1mod2k) auto 
37660  1245 

1246 
lemmas word_not_simps [simp] = 

1247 
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] 

1248 

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

1250 
unfolding word_less_def by auto 

1251 

45604  1252 
lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of y"] for y 
37660  1253 

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

1254 
lemma word_sless_alt: "(a <s b) = (sint a < sint b)" 
37660  1255 
unfolding word_sle_def word_sless_def 
1256 
by (auto simp add: less_le) 

1257 

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

1259 
unfolding unat_def word_le_def 

1260 
by (rule nat_le_eq_zle [symmetric]) simp 

1261 

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

1263 
unfolding unat_def word_less_alt 

1264 
by (rule nat_less_eq_zless [symmetric]) simp 

1265 

1266 
lemma wi_less: 

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

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

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

1270 

1271 
lemma wi_le: 

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

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

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

1275 

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

1277 
apply (unfold udvd_def) 

1278 
apply safe 

1279 
apply (simp add: unat_def nat_mult_distrib) 

1280 
apply (simp add: uint_nat int_mult) 

1281 
apply (rule exI) 

1282 
apply safe 

1283 
prefer 2 

1284 
apply (erule notE) 

1285 
apply (rule refl) 

1286 
apply force 

1287 
done 

1288 

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

1290 
unfolding dvd_def udvd_nat_alt by force 

1291 

45604  1292 
lemmas unat_mono = word_less_nat_alt [THEN iffD1] 
37660  1293 

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

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

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

1298 
prefer 2 

1299 
apply (drule contrapos_nn) 

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

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

1302 
apply arith 

1303 
apply (rule box_equals) 

1304 
apply (rule nat_diff_distrib) 

1305 
prefer 2 

1306 
apply assumption 

1307 
apply simp 

1308 
apply (subst mod_pos_pos_trivial) 

1309 
apply arith 

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

1311 
apply arith 

1312 
apply (rule refl) 

1313 
apply simp 

1314 
done 

1315 

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

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

45604  1319 
lemmas uint_add_ge0 [simp] = add_nonneg_nonneg [OF uint_ge_0 uint_ge_0] 
1320 
lemmas uint_mult_ge0 [simp] = mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0] 

37660  1321 

1322 
lemma uint_sub_lt2p [simp]: 

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

1324 
2 ^ len_of TYPE('a)" 

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

1326 

1327 

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

1329 

1330 
lemma uint_add_lem: 

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

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

1333 
by (unfold uint_word_ariths) (auto intro!: trans [OF _ 