author  huffman 
Wed, 28 Dec 2011 10:48:39 +0100  
changeset 46012  8a070c62b548 
parent 46011  96a5f44c22da 
child 46013  d2f179d26133 
permissions  rwrr 
29628  1 
(* Title: HOL/Word/Word.thy 
37660  2 
Author: Jeremy Dawson and Gerwin Klein, NICTA 
24333  3 
*) 
4 

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

29628  7 
theory Word 
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 

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

120 

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

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

122 

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

125 

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

126 
lemmas lens_gt_0 = word_size_gt_0 len_gt_0 
45604  127 
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

128 

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

129 
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

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

131 

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

132 
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

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

134 

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

135 
lemma mod_in_reps: "m > 0 \<Longrightarrow> y mod m : {0::int ..< m}" 
45805  136 
by auto 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

137 

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

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

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

140 
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

141 
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

142 

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

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

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

145 
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

146 

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

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

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

149 
(%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

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

151 
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

152 
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

153 
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

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

155 

45805  156 
lemma int_word_uint: 
157 
"uint (word_of_int x::'a::len0 word) = x mod 2 ^ len_of TYPE('a)" 

158 
by (fact td_ext_uint [THEN td_ext.eq_norm]) 

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

159 

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

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

161 
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

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

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

164 
"\<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

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

166 

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

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

168 

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

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

170 
[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

171 

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

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

173 
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

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

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

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

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

178 

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

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

180 
"(\<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

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

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

183 
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

184 
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

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

186 
qed 
37660  187 

188 
subsection "Arithmetic operations" 

189 

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

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

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

192 
where 
46000  193 
"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

194 

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

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

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

197 
where 
46000  198 
"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

199 

45547  200 
instantiation word :: (len0) "{number, Divides.div, comm_monoid_mult, comm_ring}" 
37660  201 
begin 
202 

203 
definition 

204 
word_0_wi: "0 = word_of_int 0" 

205 

206 
definition 

207 
word_1_wi: "1 = word_of_int 1" 

208 

209 
definition 

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

211 

212 
definition 

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

214 

215 
definition 

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

217 

218 
definition 

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

220 

221 
definition 

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

223 

224 
definition 

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

226 

227 
definition 

228 
word_number_of_def: "number_of w = word_of_int w" 

229 

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

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

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

232 
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

233 

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

234 
lemmas arths = 
45604  235 
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

236 

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

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

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

239 
wi_hom_add: "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

240 
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

241 
wi_hom_neg: " word_of_int a = word_of_int ( a)" and 
46000  242 
wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" and 
243 
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

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

245 

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

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

247 

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

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

249 
"(word_of_int a)  word_of_int b = word_of_int (a  b)" 
45805  250 
by (simp add: word_sub_wi arths) 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

251 

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

252 
lemmas word_of_int_homs = 
46009  253 
word_of_int_sub_hom wi_homs word_0_wi word_1_wi 
254 

255 
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

256 

45805  257 
(* FIXME: provide only one copy of these theorems! *) 
258 
lemmas word_of_int_add_hom = wi_hom_add 

259 
lemmas word_of_int_mult_hom = wi_hom_mult 

260 
lemmas word_of_int_minus_hom = wi_hom_neg 

46000  261 
lemmas word_of_int_succ_hom = wi_hom_succ 
262 
lemmas word_of_int_pred_hom = wi_hom_pred 

45805  263 
lemmas word_of_int_0_hom = word_0_wi 
264 
lemmas word_of_int_1_hom = word_1_wi 

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

265 

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

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

267 
by default (auto simp: split_word_all word_of_int_homs algebra_simps) 
37660  268 

269 
end 

270 

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

271 
instance word :: (len) comm_ring_1 
45810  272 
proof 
273 
have "0 < len_of TYPE('a)" by (rule len_gt_0) 

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

275 
unfolding word_0_wi word_1_wi 

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

277 
qed 

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

278 

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

279 
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

280 
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

281 

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

282 
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

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

284 
apply (case_tac x rule: int_diff_cases) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

285 
apply (simp add: word_of_nat word_of_int_sub_hom) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

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

287 

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

288 
instance word :: (len) number_ring 
45810  289 
by (default, simp add: word_number_of_def word_of_int) 
37660  290 

291 
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

292 
"a udvd b = (EX n>=0. uint b = n * uint a)" 
37660  293 

45547  294 

295 
subsection "Ordering" 

296 

297 
instantiation word :: (len0) linorder 

298 
begin 

299 

37660  300 
definition 
301 
word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b" 

302 

303 
definition 

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

305 

45547  306 
instance 
307 
by default (auto simp: word_less_def word_le_def) 

308 

309 
end 

310 

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

312 
"a <=s b = (sint a <= sint b)" 
37660  313 

314 
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

315 
"(x <s y) = (x <=s y & x ~= y)" 
37660  316 

317 

318 
subsection "Bitwise operations" 

319 

320 
instantiation word :: (len0) bits 

321 
begin 

322 

323 
definition 

324 
word_and_def: 

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

326 

327 
definition 

328 
word_or_def: 

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

330 

331 
definition 

332 
word_xor_def: 

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

334 

335 
definition 

336 
word_not_def: 

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

338 

339 
definition 

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

341 

342 
definition 

343 
word_set_bit_def: "set_bit a n x = 

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

345 

346 
definition 

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

348 

349 
definition 

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

351 

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

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

354 

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

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

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

358 

359 
definition 

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

361 

362 
definition 

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

364 

365 
instance .. 

366 

367 
end 

368 

369 
instantiation word :: (len) bitss 

370 
begin 

371 

372 
definition 

373 
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

374 
"msb a \<longleftrightarrow> bin_sign (sint a) = 1" 
37660  375 

376 
instance .. 

377 

378 
end 

379 

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

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

381 
"setBit w n = set_bit w n True" 
37660  382 

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

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

384 
"clearBit w n = set_bit w n False" 
37660  385 

386 

387 
subsection "Shift operations" 

388 

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

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

390 
"sshiftr1 w = word_of_int (bin_rest (sint w))" 
37660  391 

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

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

393 
"bshiftr1 b w = of_bl (b # butlast (to_bl w))" 
37660  394 

395 
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

396 
"w >>> n = (sshiftr1 ^^ n) w" 
37660  397 

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

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

399 
"mask n = (1 << n)  1" 
37660  400 

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

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

402 
"revcast w = of_bl (takefill False (len_of TYPE('b)) (to_bl w))" 
37660  403 

404 
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

405 
"slice1 n w = of_bl (takefill False n (to_bl w))" 
37660  406 

407 
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

408 
"slice n w = slice1 (size w  n) w" 
37660  409 

410 

411 
subsection "Rotation" 

412 

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

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

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

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

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

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

418 
"rotater n = rotater1 ^^ n" 
37660  419 

420 
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

421 
"word_rotr n w = of_bl (rotater n (to_bl w))" 
37660  422 

423 
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

424 
"word_rotl n w = of_bl (rotate n (to_bl w))" 
37660  425 

426 
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

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

428 
else word_rotl (nat ( i)) w)" 
37660  429 

430 

431 
subsection "Split and cat operations" 

432 

433 
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

434 
"word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))" 
37660  435 

436 
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

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

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

439 
(u, v) => (word_of_int u, word_of_int v))" 
37660  440 

441 
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

442 
"word_rcat ws = 
37660  443 
word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))" 
444 

445 
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

446 
"word_rsplit w = 
37660  447 
map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))" 
448 

449 
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

450 
"max_word = word_of_int (2 ^ len_of TYPE('a)  1)" 
37660  451 

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

453 
"of_bool False = 0" 

454 
 "of_bool True = 1" 

455 

45805  456 
(* FIXME: only provide one theorem name *) 
37660  457 
lemmas of_nth_def = word_set_bits_def 
458 

46010  459 
subsection {* Theorems about typedefs *} 
460 

37660  461 
lemma sint_sbintrunc': 
462 
"sint (word_of_int bin :: 'a word) = 

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

464 
unfolding sint_uint 

465 
by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt) 

466 

467 
lemma uint_sint: 

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

469 
unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le) 

470 

471 
lemma bintr_uint': 

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

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

475 
apply (simp only: bintrunc_bintrunc_min word_size) 

476 
apply (simp add: min_max.inf_absorb2) 

477 
done 

478 

479 
lemma wi_bintr': 

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

480 
"wb = word_of_int bin \<Longrightarrow> n >= size wb \<Longrightarrow> 
37660  481 
word_of_int (bintrunc n bin) = wb" 
482 
unfolding word_size 

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

484 

485 
lemmas bintr_uint = bintr_uint' [unfolded word_size] 

486 
lemmas wi_bintr = wi_bintr' [unfolded word_size] 

487 

488 
lemma td_ext_sbin: 

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

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

491 
apply (unfold td_ext_def' sint_uint) 

492 
apply (simp add : word_ubin.eq_norm) 

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

494 
apply (auto simp add : sints_def) 

495 
apply (rule sym [THEN trans]) 

496 
apply (rule word_ubin.Abs_norm) 

497 
apply (simp only: bintrunc_sbintrunc) 

498 
apply (drule sym) 

499 
apply simp 

500 
done 

501 

502 
lemmas td_ext_sint = td_ext_sbin 

503 
[simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]] 

504 

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

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

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

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

509 
interpretation word_sint: 

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

511 
word_of_int 

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

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

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

515 
by (rule td_ext_sint) 

516 

517 
interpretation word_sbin: 

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

519 
word_of_int 

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

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

522 
by (rule td_ext_sbin) 

523 

45604  524 
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] 
37660  525 

526 
lemmas td_sint = word_sint.td 

527 

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

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

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

530 
by (simp add: number_of_eq word_number_of_def) 
37660  531 

532 
lemma word_no_wi: "number_of = word_of_int" 

44762  533 
by (auto simp: word_number_of_def) 
37660  534 

535 
lemma to_bl_def': 

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

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

44762  538 
by (auto simp: to_bl_def) 
37660  539 

45604  540 
lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of w"] for w 
37660  541 

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

544 

545 
lemma uint_bintrunc [simp]: 

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

547 
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

548 
unfolding word_number_of_alt by (rule word_ubin.eq_norm) 
37660  549 

45805  550 
lemma sint_sbintrunc [simp]: 
551 
"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

552 
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

553 
unfolding word_number_of_alt by (rule word_sbin.eq_norm) 
37660  554 

45805  555 
lemma unat_bintrunc [simp]: 
37660  556 
"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

557 
nat (bintrunc (len_of TYPE('a)) (number_of bin))" 
37660  558 
unfolding unat_def nat_number_of_def 
559 
by (simp only: uint_bintrunc) 

560 

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

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

564 
apply (rule box_equals) 

565 
defer 

566 
apply (rule word_ubin.norm_Rep)+ 

567 
apply simp 

568 
done 

569 

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

572 

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

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

575 

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

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

578 

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

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

37660  581 

582 
lemma sign_uint_Pls [simp]: 

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

584 
by (simp add: sign_Pls_ge_0 number_of_eq) 

585 

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

588 

589 
lemma uint_m2p_not_non_neg: 

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

591 
by (simp only: not_le uint_m2p_neg) 

37660  592 

593 
lemma lt2p_lem: 

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

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

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

37660  599 

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

600 
lemma uint_nat: "uint w = int (unat w)" 
37660  601 
unfolding unat_def by auto 
602 

603 
lemma uint_number_of: 

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

605 
unfolding word_number_of_alt 

606 
by (simp only: int_word_uint) 

607 

608 
lemma unat_number_of: 

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

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

612 
apply (clarsimp simp only: uint_number_of) 

613 
apply (rule nat_mod_distrib [THEN trans]) 

614 
apply (erule sign_Pls_ge_0 [THEN iffD1]) 

615 
apply (simp_all add: nat_power_eq) 

616 
done 

617 

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

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

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

621 
unfolding word_number_of_alt by (rule int_word_sint) 

622 

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

623 
lemma word_of_int_0 [simp]: "word_of_int 0 = 0" 
45958  624 
unfolding word_0_wi .. 
625 

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

626 
lemma word_of_int_1 [simp]: "word_of_int 1 = 1" 
45958  627 
unfolding word_1_wi .. 
628 

37660  629 
lemma word_of_int_bin [simp] : 
630 
"(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

631 
unfolding word_number_of_alt .. 
37660  632 

633 
lemma word_int_case_wi: 

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

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

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

637 

638 
lemma word_int_split: 

639 
"P (word_int_case f x) = 

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

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

642 
unfolding word_int_case_def 

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

644 

645 
lemma word_int_split_asm: 

646 
"P (word_int_case f x) = 

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

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

649 
unfolding word_int_case_def 

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

45805  651 

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

37660  654 

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

656 
unfolding word_size by (rule uint_range') 

657 

658 
lemma sint_range_size: 

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

660 
unfolding word_size by (rule sint_range') 

661 

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

664 

665 
lemma sint_below_size: 

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

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

37660  668 

46010  669 
subsection {* Testing bits *} 
670 

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

673 

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

675 
apply (unfold word_test_bit_def) 

676 
apply (subst word_ubin.norm_Rep [symmetric]) 

677 
apply (simp only: nth_bintr word_size) 

678 
apply fast 

679 
done 

680 

681 
lemma word_eqI [rule_format] : 

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

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

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

686 
apply (erule allE) 

687 
apply (erule impCE) 

688 
prefer 2 

689 
apply assumption 

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

691 
done 

692 

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

37660  695 

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

697 
unfolding word_test_bit_def word_size 

698 
by (simp add: nth_bintr [symmetric]) 

699 

700 
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] 

701 

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

703 
apply (unfold word_size) 

704 
apply (rule impI) 

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

706 
apply (subst word_ubin.norm_Rep) 

707 
apply assumption 

708 
done 

709 

710 
lemma bin_nth_sint': 

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

712 
apply (rule impI) 

713 
apply (subst word_sbin.norm_Rep [symmetric]) 

714 
apply (simp add : nth_sbintr word_size) 

715 
apply auto 

716 
done 

717 

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

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

720 

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

722 
lemma td_bl: 

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

724 
of_bl 

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

726 
apply (unfold type_definition_def of_bl_def to_bl_def) 

727 
apply (simp add: word_ubin.eq_norm) 

728 
apply safe 

729 
apply (drule sym) 

730 
apply simp 

731 
done 

732 

733 
interpretation word_bl: 

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

735 
of_bl 

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

737 
by (rule td_bl) 

738 

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

739 
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

740 

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

741 
lemma word_size_bl: "size w = size (to_bl w)" 
37660  742 
unfolding word_size by auto 
743 

744 
lemma to_bl_use_of_bl: 

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

746 
by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq]) 
37660  747 

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

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

750 

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

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

753 

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

754 
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w" 
37660  755 
by auto 
756 

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

759 

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

761 
unfolding word_bl_Rep' by (rule len_gt_0) 

762 

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

764 
by (fact length_bl_gt_0 [unfolded length_greater_0_conv]) 

765 

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

767 
by (fact length_bl_gt_0 [THEN gr_implies_not0]) 

37660  768 

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

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

772 
apply simp 

773 
done 

774 

775 
lemma of_bl_drop': 

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

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

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

780 
done 

781 

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

37660  784 

785 
lemma test_bit_of_bl: 

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

787 
apply (unfold of_bl_def word_test_bit_def) 

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

789 
done 

790 

791 
lemma no_of_bl: 

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

793 
unfolding word_size of_bl_no by (simp add : word_number_of_def) 

794 

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

795 
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)" 
37660  796 
unfolding word_size to_bl_def by auto 
797 

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

799 
unfolding uint_bl by (simp add : word_size) 

800 

801 
lemma to_bl_of_bin: 

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

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

804 

45805  805 
lemma to_bl_no_bin [simp]: 
806 
"to_bl (number_of bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin" 

807 
by (fact to_bl_of_bin [folded word_number_of_def]) 

37660  808 

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

810 
unfolding uint_bl by (simp add : word_size) 

46011  811 

812 
lemma uint_bl_bin: 

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

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

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

45604  816 

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

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

820 
unfolding word_number_of_def by (rule word_uint.Rep_inverse) 

821 

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

823 
unfolding word_number_of_def by (rule word_sint.Rep_inverse) 

37660  824 

825 
(* naturals *) 

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

827 
apply (unfold unats_def uints_num) 

828 
apply safe 

829 
apply (rule_tac image_eqI) 

830 
apply (erule_tac nat_0_le [symmetric]) 

831 
apply auto 

832 
apply (erule_tac nat_less_iff [THEN iffD2]) 

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

834 
apply (auto simp add : nat_power_eq int_power) 

835 
done 

836 

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

838 
by (auto simp add : uints_unats image_iff) 

839 

45604  840 
lemmas bintr_num = word_ubin.norm_eq_iff [symmetric, folded word_number_of_def] 
841 
lemmas sbintr_num = word_sbin.norm_eq_iff [symmetric, folded word_number_of_def] 

842 

843 
lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def] 

844 
lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def] 

37660  845 

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

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

848 

849 
lemma num_of_bintr': 

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

850 
"bintrunc (len_of TYPE('a :: len0)) a = b \<Longrightarrow> 
37660  851 
number_of a = (number_of b :: 'a word)" 
852 
apply safe 

853 
apply (rule_tac num_of_bintr [symmetric]) 

854 
done 

855 

856 
lemma num_of_sbintr': 

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

857 
"sbintrunc (len_of TYPE('a :: len)  1) a = b \<Longrightarrow> 
37660  858 
number_of a = (number_of b :: 'a word)" 
859 
apply safe 

860 
apply (rule_tac num_of_sbintr [symmetric]) 

861 
done 

862 

45604  863 
lemmas num_abs_bintr = sym [THEN trans, OF num_of_bintr word_number_of_def] 
864 
lemmas num_abs_sbintr = sym [THEN trans, OF num_of_sbintr word_number_of_def] 

37660  865 

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

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

868 

869 
lemma ucast_id: "ucast w = w" 

870 
unfolding ucast_def by auto 

871 

872 
lemma scast_id: "scast w = w" 

873 
unfolding scast_def by auto 

874 

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

875 
lemma ucast_bl: "ucast w = of_bl (to_bl w)" 
37660  876 
unfolding ucast_def of_bl_def uint_bl 
877 
by (auto simp add : word_size) 

878 

879 
lemma nth_ucast: 

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

881 
apply (unfold ucast_def test_bit_bin) 

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

883 
apply (fast elim!: bin_nth_uint_imp) 

884 
done 

885 

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

887 

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

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

890 
word_of_int (bintrunc (len_of TYPE('a)) (number_of w))" 
37660  891 
unfolding ucast_def by simp 
892 

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

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

895 
word_of_int (sbintrunc (len_of TYPE('a)  Suc 0) (number_of w))" 
37660  896 
unfolding scast_def by simp 
897 

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

900 

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

902 
unfolding target_size_def word_size Let_def .. 

903 

904 
lemma is_down: 

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

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

907 
unfolding is_down_def source_size target_size .. 

908 

909 
lemma is_up: 

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

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

912 
unfolding is_up_def source_size target_size .. 

37660  913 

45604  914 
lemmas is_up_down = trans [OF is_up is_down [symmetric]] 
37660  915 

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

919 
apply (rule ext) 

920 
apply (unfold ucast_def scast_def uint_sint) 

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

922 
apply simp 

923 
done 

924 

45811  925 
lemma word_rev_tf: 
926 
"to_bl (of_bl bl::'a::len0 word) = 

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

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

930 

45811  931 
lemma word_rep_drop: 
932 
"to_bl (of_bl bl::'a::len0 word) = 

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

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

935 
by (simp add: word_rev_tf takefill_alt rev_take) 

37660  936 

937 
lemma to_bl_ucast: 

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

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

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

941 
apply (unfold ucast_bl) 

942 
apply (rule trans) 

943 
apply (rule word_rep_drop) 

944 
apply simp 

945 
done 

946 

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

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

951 

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

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

956 

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

958 
"sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> 
37660  959 
to_bl (sc w) = drop n (to_bl w)" 
960 
apply (subgoal_tac "sc = ucast") 

961 
apply safe 

962 
apply simp 

45811  963 
apply (erule ucast_down_drop) 
964 
apply (rule down_cast_same [symmetric]) 

37660  965 
apply (simp add : source_size target_size is_down) 
966 
done 

967 

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

969 
"sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w" 
37660  970 
apply (unfold is_up) 
971 
apply safe 

972 
apply (simp add: scast_def word_sbin.eq_norm) 

973 
apply (rule box_equals) 

974 
prefer 3 

975 
apply (rule word_sbin.norm_Rep) 

976 
apply (rule sbintrunc_sbintrunc_l) 

977 
defer 

978 
apply (subst word_sbin.norm_Rep) 

979 
apply (rule refl) 

980 
apply simp 

981 
done 

982 

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

984 
"uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w" 
37660  985 
apply (unfold is_up) 
986 
apply safe 

987 
apply (rule bin_eqI) 

988 
apply (fold word_test_bit_def) 

989 
apply (auto simp add: nth_ucast) 

990 
apply (auto simp add: test_bit_bin) 

991 
done 

45811  992 

993 
lemma ucast_up_ucast [OF refl]: 

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

37660  995 
apply (simp (no_asm) add: ucast_def) 
996 
apply (clarsimp simp add: uint_up_ucast) 

997 
done 

998 

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

37660  1001 
apply (simp (no_asm) add: scast_def) 
1002 
apply (clarsimp simp add: sint_up_scast) 

1003 
done 

1004 

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

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

1009 
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] 

1010 
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] 

1011 

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

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

1014 
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] 

1015 
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] 

1016 

1017 
lemma up_ucast_surj: 

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

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

1021 

1022 
lemma up_scast_surj: 

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

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

1026 

1027 
lemma down_scast_inj: 

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

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

1031 

1032 
lemma down_ucast_inj: 

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

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

1036 

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

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

45811  1039 

1040 
lemma ucast_down_no [OF refl]: 

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

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

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

1045 
apply (erule bintrunc_bintrunc_ge) 

1046 
done 

45811  1047 

1048 
lemma ucast_down_bl [OF refl]: 

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

37660  1050 
unfolding of_bl_no by clarify (erule ucast_down_no) 
1051 

1052 
lemmas slice_def' = slice_def [unfolded word_size] 

1053 
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] 

1054 

1055 
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def 

46011  1056 
lemmas word_log_bin_defs = word_log_defs (* FIXME: duplicate *) 
37660  1057 

1058 
text {* Executable equality *} 

1059 

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

1060 
instantiation word :: (len0) equal 
24333  1061 
begin 
1062 

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

1063 
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

1064 
"equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" 
37660  1065 

1066 
instance proof 

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

1067 
qed (simp add: equal equal_word_def) 
37660  1068 

1069 
end 

1070 

1071 

1072 
subsection {* Word Arithmetic *} 

1073 

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

46012  1075 
unfolding word_less_def word_le_def by (simp add: less_le) 
37660  1076 

1077 
lemma signed_linorder: "class.linorder word_sle word_sless" 

1078 
proof 

1079 
qed (unfold word_sle_def word_sless_def, auto) 

1080 

1081 
interpretation signed: linorder "word_sle" "word_sless" 

1082 
by (rule signed_linorder) 

1083 

1084 
lemma udvdI: 

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

1085 
"0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" 
37660  1086 
by (auto simp: udvd_def) 
1087 

45604  1088 
lemmas word_div_no [simp] = word_div_def [of "number_of a" "number_of b"] for a b 
1089 

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

1091 

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

1093 

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

1095 

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

1097 

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

37660  1099 

1100 
(* following two are available in class number_ring, 

1101 
but convenient to have them here here; 

1102 
note  the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1 

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

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

1105 
(simp add: word_1_no del: numeral_1_eq_1) 

1106 
*) 

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

1109 

1110 
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

1111 
by (simp add: word_number_of_alt) 
37660  1112 

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

1113 
lemma int_one_bin: "(1 :: int) = (Int.Pls BIT 1)" 
37660  1114 
unfolding Pls_def Bit_def by auto 
1115 

1116 
lemma word_1_no: 

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

1117 
"(1 :: 'a :: len0 word) = number_of (Int.Pls BIT 1)" 
37660  1118 
unfolding word_1_wi word_number_of_def int_one_bin by auto 
1119 

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

1120 
lemma word_m1_wi: "1 = word_of_int 1" 
37660  1121 
by (rule word_number_of_alt) 
1122 

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

1124 
by (simp add: word_m1_wi number_of_eq) 

1125 

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

1127 
unfolding of_bl_def by (simp add: Pls_def) 
37660  1128 

1129 
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

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

1131 
by (simp add: bl_to_bin_def Bit_def Pls_def) 
37660  1132 

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

1134 
unfolding word_0_wi 

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

1136 

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

1137 
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

1138 
by (simp add: of_bl_def bl_to_bin_rep_False Pls_def) 
37660  1139 

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

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

1144 

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

1146 
by (auto intro!: word_uint.Rep_eqD) 

1147 

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

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

1150 

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

1152 
unfolding unat_def by auto 

1153 

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

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

1157 
defer 

1158 
apply (rule word_uint.Rep_inverse)+ 

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

1160 
apply simp 

1161 
done 

1162 

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

1163 
lemmas size_0_same = size_0_same' [unfolded word_size] 
37660  1164 

1165 
lemmas unat_eq_0 = unat_0_iff 

1166 
lemmas unat_eq_zero = unat_0_iff 

1167 

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

1169 
by (auto simp: unat_0_iff [symmetric]) 

1170 

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

1172 
unfolding ucast_def by simp 
45958  1173 

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

1175 
unfolding sint_uint by simp 

1176 

1177 
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

1178 
unfolding scast_def by simp 
37660  1179 

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

45958  1181 
unfolding word_m1_wi by (simp add: word_sbin.eq_norm) 
1182 

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

1184 
unfolding scast_def by simp 

1185 

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

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

1188 
by (simp add: word_ubin.eq_norm bintrunc_minus_simps del: word_of_int_1) 
45958  1189 

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

1191 
unfolding unat_def by simp 

1192 

1193 
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

1194 
unfolding ucast_def by simp 
37660  1195 

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

1197 

1198 
lemmas word_arith_alts = 

46000  1199 
word_sub_wi 
1200 
word_arith_wis (* FIXME: duplicate *) 

1201 

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

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

37660  1204 

1205 
subsection "Transferring goals from words to ints" 

1206 

1207 
lemma word_ths: 

1208 
shows 

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

1210 
word_pred_m1: "word_pred a = a  1" and 

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

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

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

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

1215 
rule word_uint.Abs_cases [of a], 

46000  1216 
simp add: add_commute mult_commute 
46009  1217 
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

1218 
del: word_of_int_0 word_of_int_1)+ 
37660  1219 

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

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

1221 
by simp 
37660  1222 

1223 
lemmas uint_word_ariths = 

45604  1224 
word_arith_alts [THEN trans [OF uint_cong int_word_uint]] 
37660  1225 

1226 
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p] 

1227 

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

1229 
lemmas sint_word_ariths = uint_word_arith_bintrs 

1230 
[THEN uint_sint [symmetric, THEN trans], 

1231 
unfolded uint_sint bintr_arith1s bintr_ariths 

45604  1232 
len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep] 
1233 

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

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

37660  1236 

1237 
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

1238 
unfolding word_pred_def uint_eq_0 pred_def by simp 
37660  1239 

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

1241 
by (simp add: word_pred_0_n1 number_of_eq) 

1242 

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

1244 
unfolding Min_def by (simp only: word_of_int_hom_syms) 

1245 

1246 
lemma succ_pred_no [simp]: 

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

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

46000  1249 
unfolding word_number_of_def Int.succ_def Int.pred_def 
46009  1250 
by (simp add: word_of_int_homs) 
37660  1251 

1252 
lemma word_sp_01 [simp] : 

1253 
"word_succ 1 = 0 & word_succ 0 = 1 & word_pred 0 = 1 & word_pred 1 = 0" 

45847  1254 
by (unfold word_0_no word_1_no) (auto simp: BIT_simps) 
37660  1255 

1256 
(* alternative approach to lifting arithmetic equalities *) 

1257 
lemma word_of_int_Ex: 

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

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

1260 

1261 

1262 
subsection "Order on fixedlength words" 

1263 

1264 
lemma word_zero_le [simp] : 

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

1266 
unfolding word_le_def by auto 

1267 

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

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

1271 

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

1272 
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

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

1274 
by (simp only: word_m1_wi word_uint.eq_norm m1mod2k) auto 
37660  1275 

1276 
lemmas word_not_simps [simp] = 

1277 
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] 

1278 

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

1280 
unfolding word_less_def by auto 

1281 

45604  1282 
lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of y"] for y 
37660  1283 

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

1284 
lemma word_sless_alt: "(a <s b) = (sint a < sint b)" 
37660  1285 
unfolding word_sle_def word_sless_def 
1286 
by (auto simp add: less_le) 

1287 

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

1289 
unfolding unat_def word_le_def 

1290 
by (rule nat_le_eq_zle [symmetric]) simp 

1291 

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

1293 
unfolding unat_def word_less_alt 

1294 
by (rule nat_less_eq_zless [symmetric]) simp 

1295 

1296 
lemma wi_less: 

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

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

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

1300 

1301 
lemma wi_le: 

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

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

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

1305 

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

1307 
apply (unfold udvd_def) 

1308 
apply safe 

1309 
apply (simp add: unat_def nat_mult_distrib) 

1310 
apply (simp add: uint_nat int_mult) 

1311 
apply (rule exI) 

1312 
apply safe 

1313 
prefer 2 

1314 
apply (erule notE) 

1315 
apply (rule refl) 

1316 
apply force 

1317 
done 

1318 

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

1320 
unfolding dvd_def udvd_nat_alt by force 

1321 

45604  1322 
lemmas unat_mono = word_less_nat_alt [THEN iffD1] 
37660  1323 

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

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

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

1328 
prefer 2 

1329 
apply (drule contrapos_nn) 

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

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

1332 
apply arith 

1333 
apply (rule box_equals) 

1334 
apply (rule nat_diff_distrib) 

1335 
prefer 2 
