author  huffman 
Thu, 17 Nov 2011 14:24:10 +0100  
changeset 45545  26aebb8ac9c1 
parent 45544  c0304794e9e4 
child 45546  6dd3e88de4c2 
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 

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

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 
syntax 

119 
of_int :: "int => 'a" 

120 
translations 

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

122 

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

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

124 

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

125 
lemmas word_size_gt_0 [iff] = 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

126 
xtr1 [OF word_size len_gt_0, standard] 
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 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

128 
lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard] 
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 
lemmas atLeastLessThan_alt = atLeastLessThan_def [unfolded 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

137 
atLeast_def lessThan_def Collect_conj_eq [symmetric]] 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

138 

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

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

140 
unfolding atLeastLessThan_alt by auto 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

141 

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

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

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

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

145 
by (auto simp: uint [simplified]) 
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 uint_mod_same: 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

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

149 
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

150 

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

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

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

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

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

155 
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

156 
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

157 
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

158 
done 
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 
lemmas int_word_uint = td_ext_uint [THEN td_ext.eq_norm, standard] 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

161 

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

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

163 
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

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

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

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

167 
by (rule td_ext_uint) 
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_uint = word_uint.td_thm 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

170 

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

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

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

173 

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

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

175 
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

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

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

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

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

180 

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

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

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

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

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

185 
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

186 
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

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

188 
qed 
37660  189 

190 
subsection "Arithmetic operations" 

191 

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

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

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

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

195 
"word_succ a = word_of_int (Int.succ (uint a))" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

196 

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

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

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

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

200 
"word_pred a = word_of_int (Int.pred (uint a))" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

201 

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

202 
instantiation word :: (len0) "{number, Divides.div, ord, comm_monoid_mult, comm_ring}" 
37660  203 
begin 
204 

205 
definition 

206 
word_0_wi: "0 = word_of_int 0" 

207 

208 
definition 

209 
word_1_wi: "1 = word_of_int 1" 

210 

211 
definition 

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

213 

214 
definition 

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

216 

217 
definition 

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

219 

220 
definition 

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

222 

223 
definition 

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

225 

226 
definition 

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

228 

229 
definition 

230 
word_number_of_def: "number_of w = word_of_int w" 

231 

232 
definition 

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

234 

235 
definition 

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

237 

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

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

239 
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

240 
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

241 

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

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

243 
bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1], 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

244 
folded word_ubin.eq_norm, standard] 
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 
lemma wi_homs: 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

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

248 
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

249 
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

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

251 
wi_hom_succ: "word_succ (word_of_int a) = word_of_int (Int.succ a)" and 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

252 
wi_hom_pred: "word_pred (word_of_int a) = word_of_int (Int.pred a)" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

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

254 

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

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

256 

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

257 
lemma word_sub_def: "a  b = a +  (b :: 'a :: len0 word)" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

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

259 
by (simp only : word_uint.Rep_inverse wi_hom_syms) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

260 

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

261 
lemmas word_diff_minus = word_sub_def [standard] 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

262 

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

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

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

265 
unfolding word_sub_def diff_minus by (simp only : wi_homs) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

266 

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

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

268 
word_of_int_sub_hom wi_homs word_0_wi word_1_wi 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

269 

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

270 
lemmas new_word_of_int_hom_syms = new_word_of_int_homs [symmetric, standard] 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

271 

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

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

273 
new_word_of_int_hom_syms [unfolded succ_def pred_def] 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

274 

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

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

276 
new_word_of_int_homs [unfolded succ_def pred_def] 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

277 

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

278 
lemmas word_of_int_add_hom = word_of_int_homs (2) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

279 
lemmas word_of_int_mult_hom = word_of_int_homs (3) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

280 
lemmas word_of_int_minus_hom = word_of_int_homs (4) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

281 
lemmas word_of_int_succ_hom = word_of_int_homs (5) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

282 
lemmas word_of_int_pred_hom = word_of_int_homs (6) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

283 
lemmas word_of_int_0_hom = word_of_int_homs (7) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

284 
lemmas word_of_int_1_hom = word_of_int_homs (8) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

285 

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

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

287 
by default (auto simp: split_word_all word_of_int_homs algebra_simps) 
37660  288 

289 
end 

290 

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

291 
lemma word_zero_neq_one: "0 < len_of TYPE ('a :: len0) \<Longrightarrow> (0 :: 'a word) ~= 1" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

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

293 
by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

294 

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

295 
lemmas lenw1_zero_neq_one = len_gt_0 [THEN word_zero_neq_one] 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

296 

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

297 
instance word :: (len) comm_ring_1 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

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

299 

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

300 
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

301 
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

302 

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

303 
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

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

305 
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

306 
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

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

308 

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

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

310 
"number_of w = (of_int w :: 'a :: len word)" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

311 
unfolding word_number_of_def word_of_int by auto 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

312 

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

313 
instance word :: (len) number_ring 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

314 
by (intro_classes) (simp add : word_number_of_eq) 
37660  315 

316 
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

317 
"a udvd b = (EX n>=0. uint b = n * uint a)" 
37660  318 

319 
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

320 
"a <=s b = (sint a <= sint b)" 
37660  321 

322 
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

323 
"(x <s y) = (x <=s y & x ~= y)" 
37660  324 

325 

326 
subsection "Bitwise operations" 

327 

328 
instantiation word :: (len0) bits 

329 
begin 

330 

331 
definition 

45544
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

332 
word_and_def: 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

333 
"(a::'a word) AND b = word_of_int (uint a AND uint b)" 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

334 

c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

335 
definition 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

336 
word_or_def: 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

337 
"(a::'a word) OR b = word_of_int (uint a OR uint b)" 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

338 

c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

339 
definition 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

340 
word_xor_def: 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

341 
"(a::'a word) XOR b = word_of_int (uint a XOR uint b)" 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

342 

c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

343 
definition 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

344 
word_not_def: 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

345 
"NOT (a::'a word) = word_of_int (NOT (uint a))" 
c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

346 

c0304794e9e4
move definitions of bitwise operators into appropriate document section
huffman
parents:
45529
diff
changeset

347 
definition 
37660  348 
word_test_bit_def: "test_bit a = bin_nth (uint a)" 
349 

350 
definition 

351 
word_set_bit_def: "set_bit a n x = 

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

353 

354 
definition 

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

356 

357 
definition 

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

359 

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

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

362 

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

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

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

366 

367 
definition 

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

369 

370 
definition 

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

372 

373 
instance .. 

374 

375 
end 

376 

377 
instantiation word :: (len) bitss 

378 
begin 

379 

380 
definition 

381 
word_msb_def: 

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

383 

384 
instance .. 

385 

386 
end 

387 

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

390 
by (simp only: word_msb_def Min_def) 

391 

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

393 
"setBit w n = set_bit w n True" 
37660  394 

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

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

396 
"clearBit w n = set_bit w n False" 
37660  397 

398 

399 
subsection "Shift operations" 

400 

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

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

402 
"sshiftr1 w = word_of_int (bin_rest (sint w))" 
37660  403 

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

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

405 
"bshiftr1 b w = of_bl (b # butlast (to_bl w))" 
37660  406 

407 
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

408 
"w >>> n = (sshiftr1 ^^ n) w" 
37660  409 

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

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

411 
"mask n = (1 << n)  1" 
37660  412 

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

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

414 
"revcast w = of_bl (takefill False (len_of TYPE('b)) (to_bl w))" 
37660  415 

416 
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

417 
"slice1 n w = of_bl (takefill False n (to_bl w))" 
37660  418 

419 
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

420 
"slice n w = slice1 (size w  n) w" 
37660  421 

422 

423 
subsection "Rotation" 

424 

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

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

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

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

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

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

430 
"rotater n = rotater1 ^^ n" 
37660  431 

432 
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

433 
"word_rotr n w = of_bl (rotater n (to_bl w))" 
37660  434 

435 
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

436 
"word_rotl n w = of_bl (rotate n (to_bl w))" 
37660  437 

438 
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

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

440 
else word_rotl (nat ( i)) w)" 
37660  441 

442 

443 
subsection "Split and cat operations" 

444 

445 
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

446 
"word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))" 
37660  447 

448 
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

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

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

451 
(u, v) => (word_of_int u, word_of_int v))" 
37660  452 

453 
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

454 
"word_rcat ws = 
37660  455 
word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))" 
456 

457 
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

458 
"word_rsplit w = 
37660  459 
map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))" 
460 

461 
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

462 
"max_word = word_of_int (2 ^ len_of TYPE('a)  1)" 
37660  463 

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

465 
"of_bool False = 0" 

466 
 "of_bool True = 1" 

467 

468 

469 
lemmas of_nth_def = word_set_bits_def 

470 

471 
lemma sint_sbintrunc': 

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

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

474 
unfolding sint_uint 

475 
by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt) 

476 

477 
lemma uint_sint: 

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

479 
unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le) 

480 

481 
lemma bintr_uint': 

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

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

485 
apply (simp only: bintrunc_bintrunc_min word_size) 

486 
apply (simp add: min_max.inf_absorb2) 

487 
done 

488 

489 
lemma wi_bintr': 

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

490 
"wb = word_of_int bin \<Longrightarrow> n >= size wb \<Longrightarrow> 
37660  491 
word_of_int (bintrunc n bin) = wb" 
492 
unfolding word_size 

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

494 

495 
lemmas bintr_uint = bintr_uint' [unfolded word_size] 

496 
lemmas wi_bintr = wi_bintr' [unfolded word_size] 

497 

498 
lemma td_ext_sbin: 

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

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

501 
apply (unfold td_ext_def' sint_uint) 

502 
apply (simp add : word_ubin.eq_norm) 

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

504 
apply (auto simp add : sints_def) 

505 
apply (rule sym [THEN trans]) 

506 
apply (rule word_ubin.Abs_norm) 

507 
apply (simp only: bintrunc_sbintrunc) 

508 
apply (drule sym) 

509 
apply simp 

510 
done 

511 

512 
lemmas td_ext_sint = td_ext_sbin 

513 
[simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]] 

514 

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

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

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

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

519 
interpretation word_sint: 

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

521 
word_of_int 

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

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

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

525 
by (rule td_ext_sint) 

526 

527 
interpretation word_sbin: 

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

529 
word_of_int 

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

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

532 
by (rule td_ext_sbin) 

533 

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

535 

536 
lemmas td_sint = word_sint.td 

537 

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

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

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

540 
by (simp add: number_of_eq word_number_of_def) 
37660  541 

542 
lemma word_no_wi: "number_of = word_of_int" 

44762  543 
by (auto simp: word_number_of_def) 
37660  544 

545 
lemma to_bl_def': 

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

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

44762  548 
by (auto simp: to_bl_def) 
37660  549 

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

551 

552 
lemmas uints_mod = uints_def [unfolded no_bintr_alt1] 

553 

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

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

556 
unfolding word_number_of_def number_of_eq 

557 
by (auto intro: word_ubin.eq_norm) 

558 

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

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

561 
unfolding word_number_of_def number_of_eq 

562 
by (subst word_sbin.eq_norm) simp 

563 

564 
lemma unat_bintrunc: 

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

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

567 
unfolding unat_def nat_number_of_def 

568 
by (simp only: uint_bintrunc) 

569 

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

571 
declare 

572 
uint_bintrunc [simp] 

573 
sint_sbintrunc [simp] 

574 
unat_bintrunc [simp] 

575 

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

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

579 
apply (rule box_equals) 

580 
defer 

581 
apply (rule word_ubin.norm_Rep)+ 

582 
apply simp 

583 
done 

584 

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

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

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

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

589 
lemmas sint_ge = sint_lem [THEN conjunct1, standard] 

590 
lemmas sint_lt = sint_lem [THEN conjunct2, standard] 

591 

592 
lemma sign_uint_Pls [simp]: 

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

594 
by (simp add: sign_Pls_ge_0 number_of_eq) 

595 

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

597 
lemmas uint_m2p_not_non_neg = 

598 
iffD2 [OF linorder_not_le uint_m2p_neg, standard] 

599 

600 
lemma lt2p_lem: 

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

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

604 
lemmas uint_le_0_iff [simp] = 

605 
uint_ge_0 [THEN leD, THEN linorder_antisym_conv1, standard] 

606 

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

607 
lemma uint_nat: "uint w = int (unat w)" 
37660  608 
unfolding unat_def by auto 
609 

610 
lemma uint_number_of: 

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

612 
unfolding word_number_of_alt 

613 
by (simp only: int_word_uint) 

614 

615 
lemma unat_number_of: 

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

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

619 
apply (clarsimp simp only: uint_number_of) 

620 
apply (rule nat_mod_distrib [THEN trans]) 

621 
apply (erule sign_Pls_ge_0 [THEN iffD1]) 

622 
apply (simp_all add: nat_power_eq) 

623 
done 

624 

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

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

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

628 
unfolding word_number_of_alt by (rule int_word_sint) 

629 

630 
lemma word_of_int_bin [simp] : 

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

632 
unfolding word_number_of_alt by auto 

633 

634 
lemma word_int_case_wi: 

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

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

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

638 

639 
lemma word_int_split: 

640 
"P (word_int_case f x) = 

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

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

643 
unfolding word_int_case_def 

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

645 

646 
lemma word_int_split_asm: 

647 
"P (word_int_case f x) = 

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

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

650 
unfolding word_int_case_def 

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

652 

653 
lemmas uint_range' = 

654 
word_uint.Rep [unfolded uints_num mem_Collect_eq, standard] 

655 
lemmas sint_range' = word_sint.Rep [unfolded One_nat_def 

656 
sints_num mem_Collect_eq, standard] 

657 

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

659 
unfolding word_size by (rule uint_range') 

660 

661 
lemma sint_range_size: 

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

663 
unfolding word_size by (rule sint_range') 

664 

665 
lemmas sint_above_size = sint_range_size 

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

667 

668 
lemmas sint_below_size = sint_range_size 

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

670 

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 

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

694 

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

696 
unfolding word_test_bit_def word_size 

697 
by (simp add: nth_bintr [symmetric]) 

698 

699 
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] 

700 

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

702 
apply (unfold word_size) 

703 
apply (rule impI) 

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

705 
apply (subst word_ubin.norm_Rep) 

706 
apply assumption 

707 
done 

708 

709 
lemma bin_nth_sint': 

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

711 
apply (rule impI) 

712 
apply (subst word_sbin.norm_Rep [symmetric]) 

713 
apply (simp add : nth_sbintr word_size) 

714 
apply auto 

715 
done 

716 

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

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

719 

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

721 
lemma td_bl: 

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

723 
of_bl 

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

725 
apply (unfold type_definition_def of_bl_def to_bl_def) 

726 
apply (simp add: word_ubin.eq_norm) 

727 
apply safe 

728 
apply (drule sym) 

729 
apply simp 

730 
done 

731 

732 
interpretation word_bl: 

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

734 
of_bl 

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

736 
by (rule td_bl) 

737 

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

738 
lemma word_size_bl: "size w = size (to_bl w)" 
37660  739 
unfolding word_size by auto 
740 

741 
lemma to_bl_use_of_bl: 

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

44890
22f665a2e91c
new fastforce replacing fastsimp  less confusing name
nipkow
parents:
44821
diff
changeset

743 
by (fastforce elim!: word_bl.Abs_inverse [simplified]) 
37660  744 

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

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

747 

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

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

750 

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

751 
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w" 
37660  752 
by auto 
753 

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

755 

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

757 
lemmas bl_not_Nil [iff] = 

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

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

760 

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

762 
apply (unfold to_bl_def sint_uint) 

763 
apply (rule trans [OF _ bl_sbin_sign]) 

764 
apply simp 

765 
done 

766 

767 
lemma of_bl_drop': 

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

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

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

772 
done 

773 

774 
lemmas of_bl_no = of_bl_def [folded word_number_of_def] 

775 

776 
lemma test_bit_of_bl: 

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

778 
apply (unfold of_bl_def word_test_bit_def) 

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

780 
done 

781 

782 
lemma no_of_bl: 

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

784 
unfolding word_size of_bl_no by (simp add : word_number_of_def) 

785 

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

786 
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)" 
37660  787 
unfolding word_size to_bl_def by auto 
788 

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

790 
unfolding uint_bl by (simp add : word_size) 

791 

792 
lemma to_bl_of_bin: 

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

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

795 

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

797 

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

799 
unfolding uint_bl by (simp add : word_size) 

800 

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

802 

803 
lemmas num_AB_u [simp] = word_uint.Rep_inverse 

804 
[unfolded o_def word_number_of_def [symmetric], standard] 

805 
lemmas num_AB_s [simp] = word_sint.Rep_inverse 

806 
[unfolded o_def word_number_of_def [symmetric], standard] 

807 

808 
(* naturals *) 

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

810 
apply (unfold unats_def uints_num) 

811 
apply safe 

812 
apply (rule_tac image_eqI) 

813 
apply (erule_tac nat_0_le [symmetric]) 

814 
apply auto 

815 
apply (erule_tac nat_less_iff [THEN iffD2]) 

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

817 
apply (auto simp add : nat_power_eq int_power) 

818 
done 

819 

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

821 
by (auto simp add : uints_unats image_iff) 

822 

823 
lemmas bintr_num = word_ubin.norm_eq_iff 

824 
[symmetric, folded word_number_of_def, standard] 

825 
lemmas sbintr_num = word_sbin.norm_eq_iff 

826 
[symmetric, folded word_number_of_def, standard] 

827 

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

45284
ae78a4ffa81d
use simproc_setup for cancellation simprocs, to get proper name bindings
huffman
parents:
44938
diff
changeset

829 
lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def, standard] 
37660  830 

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

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

833 

834 
lemma num_of_bintr': 

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

835 
"bintrunc (len_of TYPE('a :: len0)) a = b \<Longrightarrow> 
37660  836 
number_of a = (number_of b :: 'a word)" 
837 
apply safe 

838 
apply (rule_tac num_of_bintr [symmetric]) 

839 
done 

840 

841 
lemma num_of_sbintr': 

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

842 
"sbintrunc (len_of TYPE('a :: len)  1) a = b \<Longrightarrow> 
37660  843 
number_of a = (number_of b :: 'a word)" 
844 
apply safe 

845 
apply (rule_tac num_of_sbintr [symmetric]) 

846 
done 

847 

848 
lemmas num_abs_bintr = sym [THEN trans, 

849 
OF num_of_bintr word_number_of_def, standard] 

850 
lemmas num_abs_sbintr = sym [THEN trans, 

851 
OF num_of_sbintr word_number_of_def, standard] 

852 

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

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

855 

856 
lemma ucast_id: "ucast w = w" 

857 
unfolding ucast_def by auto 

858 

859 
lemma scast_id: "scast w = w" 

860 
unfolding scast_def by auto 

861 

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

862 
lemma ucast_bl: "ucast w = of_bl (to_bl w)" 
37660  863 
unfolding ucast_def of_bl_def uint_bl 
864 
by (auto simp add : word_size) 

865 

866 
lemma nth_ucast: 

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

868 
apply (unfold ucast_def test_bit_bin) 

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

870 
apply (fast elim!: bin_nth_uint_imp) 

871 
done 

872 

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

874 

875 
lemma ucast_bintr [simp]: 

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

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

878 
unfolding ucast_def by simp 

879 

880 
lemma scast_sbintr [simp]: 

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

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

883 
unfolding scast_def by simp 

884 

885 
lemmas source_size = source_size_def [unfolded Let_def word_size] 

886 
lemmas target_size = target_size_def [unfolded Let_def word_size] 

887 
lemmas is_down = is_down_def [unfolded source_size target_size] 

888 
lemmas is_up = is_up_def [unfolded source_size target_size] 

889 

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

891 

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

892 
lemma down_cast_same': "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 

901 
lemma word_rev_tf': 

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

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

905 

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

907 

908 
lemmas word_rep_drop = word_rev_tf [simplified takefill_alt, 

909 
simplified, simplified rev_take, simplified] 

910 

911 
lemma to_bl_ucast: 

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

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

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

915 
apply (unfold ucast_bl) 

916 
apply (rule trans) 

917 
apply (rule word_rep_drop) 

918 
apply simp 

919 
done 

920 

921 
lemma ucast_up_app': 

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

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

925 

926 
lemma ucast_down_drop': 

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

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

930 

931 
lemma scast_down_drop': 

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

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

935 
apply safe 

936 
apply simp 

937 
apply (erule refl [THEN ucast_down_drop']) 

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

939 
apply (simp add : source_size target_size is_down) 

940 
done 

941 

942 
lemma sint_up_scast': 

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

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

946 
apply (simp add: scast_def word_sbin.eq_norm) 

947 
apply (rule box_equals) 

948 
prefer 3 

949 
apply (rule word_sbin.norm_Rep) 

950 
apply (rule sbintrunc_sbintrunc_l) 

951 
defer 

952 
apply (subst word_sbin.norm_Rep) 

953 
apply (rule refl) 

954 
apply simp 

955 
done 

956 

957 
lemma uint_up_ucast': 

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

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

961 
apply (rule bin_eqI) 

962 
apply (fold word_test_bit_def) 

963 
apply (auto simp add: nth_ucast) 

964 
apply (auto simp add: test_bit_bin) 

965 
done 

966 

967 
lemmas down_cast_same = refl [THEN down_cast_same'] 

968 
lemmas ucast_up_app = refl [THEN ucast_up_app'] 

969 
lemmas ucast_down_drop = refl [THEN ucast_down_drop'] 

970 
lemmas scast_down_drop = refl [THEN scast_down_drop'] 

971 
lemmas uint_up_ucast = refl [THEN uint_up_ucast'] 

972 
lemmas sint_up_scast = refl [THEN sint_up_scast'] 

973 

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

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

977 
done 

978 

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

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

982 
done 

983 

984 
lemma ucast_of_bl_up': 

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

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

988 
lemmas ucast_up_ucast = refl [THEN ucast_up_ucast'] 

989 
lemmas scast_up_scast = refl [THEN scast_up_scast'] 

990 
lemmas ucast_of_bl_up = refl [THEN ucast_of_bl_up'] 

991 

992 
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] 

993 
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] 

994 

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

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

997 
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] 

998 
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] 

999 

1000 
lemma up_ucast_surj: 

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

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

1004 

1005 
lemma up_scast_surj: 

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

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

1009 

1010 
lemma down_scast_inj: 

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

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

1014 

1015 
lemma down_ucast_inj: 

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

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

1019 

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

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

1022 

1023 
lemma ucast_down_no': 

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

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

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

1028 
apply (erule bintrunc_bintrunc_ge) 

1029 
done 

1030 

1031 
lemmas ucast_down_no = ucast_down_no' [OF refl] 

1032 

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

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

1036 
lemmas ucast_down_bl = ucast_down_bl' [OF refl] 

1037 

1038 
lemmas slice_def' = slice_def [unfolded word_size] 

1039 
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] 

1040 

1041 
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def 

1042 
lemmas word_log_bin_defs = word_log_defs 

1043 

1044 
text {* Executable equality *} 

1045 

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

1046 
instantiation word :: (len0) equal 
24333  1047 
begin 
1048 

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

1049 
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

1050 
"equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" 
37660  1051 

1052 
instance proof 

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

1053 
qed (simp add: equal equal_word_def) 
37660  1054 

1055 
end 

1056 

1057 

1058 
subsection {* Word Arithmetic *} 

1059 

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

1061 
unfolding word_less_def word_le_def 

1062 
by (auto simp del: word_uint.Rep_inject 

1063 
simp: word_uint.Rep_inject [symmetric]) 

1064 

1065 
lemma signed_linorder: "class.linorder word_sle word_sless" 

1066 
proof 

1067 
qed (unfold word_sle_def word_sless_def, auto) 

1068 

1069 
interpretation signed: linorder "word_sle" "word_sless" 

1070 
by (rule signed_linorder) 

1071 

1072 
lemma udvdI: 

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

1073 
"0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" 
37660  1074 
by (auto simp: udvd_def) 
1075 

1076 
lemmas word_div_no [simp] = 

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

1078 

1079 
lemmas word_mod_no [simp] = 

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

1081 

1082 
lemmas word_less_no [simp] = 

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

1084 

1085 
lemmas word_le_no [simp] = 

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

1087 

1088 
lemmas word_sless_no [simp] = 

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

1090 

1091 
lemmas word_sle_no [simp] = 

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

1093 

1094 
(* following two are available in class number_ring, 

1095 
but convenient to have them here here; 

1096 
note  the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1 

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

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

1099 
(simp add: word_1_no del: numeral_1_eq_1) 

1100 
*) 

1101 
lemmas word_0_wi_Pls = word_0_wi [folded Pls_def] 

1102 
lemmas word_0_no = word_0_wi_Pls [folded word_no_wi] 

1103 

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

1104 
lemma int_one_bin: "(1 :: int) = (Int.Pls BIT 1)" 
37660  1105 
unfolding Pls_def Bit_def by auto 
1106 

1107 
lemma word_1_no: 

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

1108 
"(1 :: 'a :: len0 word) = number_of (Int.Pls BIT 1)" 
37660  1109 
unfolding word_1_wi word_number_of_def int_one_bin by auto 
1110 

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

1111 
lemma word_m1_wi: "1 = word_of_int 1" 
37660  1112 
by (rule word_number_of_alt) 
1113 

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

1115 
by (simp add: word_m1_wi number_of_eq) 

1116 

1117 
lemma word_0_bl: "of_bl [] = 0" 

1118 
unfolding word_0_wi of_bl_def by (simp add : Pls_def) 

1119 

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

1121 
unfolding word_1_wi of_bl_def 

1122 
by (simp add : bl_to_bin_def Bit_def Pls_def) 

1123 

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

1125 
unfolding word_0_wi 

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

1127 

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

1129 
by (simp add : word_0_wi of_bl_def bl_to_bin_rep_False Pls_def) 

1130 

1131 
lemma to_bl_0: 

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

1133 
unfolding uint_bl 

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

1135 

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

1137 
by (auto intro!: word_uint.Rep_eqD) 

1138 

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

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

1141 

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

1143 
unfolding unat_def by auto 

1144 

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

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

1148 
defer 

1149 
apply (rule word_uint.Rep_inverse)+ 

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

1151 
apply simp 

1152 
done 

1153 

1154 
lemmas size_0_same = size_0_same' [folded word_size] 

1155 

1156 
lemmas unat_eq_0 = unat_0_iff 

1157 
lemmas unat_eq_zero = unat_0_iff 

1158 

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

1160 
by (auto simp: unat_0_iff [symmetric]) 

1161 

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

1163 
unfolding ucast_def 

1164 
by simp (simp add: word_0_wi) 

1165 

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

1167 
unfolding sint_uint 

1168 
by (simp add: Pls_def [symmetric]) 

1169 

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

1171 
apply (unfold scast_def) 

1172 
apply simp 

1173 
apply (simp add: word_0_wi) 

1174 
done 

1175 

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

1177 
apply (unfold word_m1_wi_Min) 

1178 
apply (simp add: word_sbin.eq_norm) 

1179 
apply (unfold Min_def number_of_eq) 

1180 
apply simp 

1181 
done 

1182 

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

1184 
apply (unfold scast_def sint_n1) 

1185 
apply (unfold word_number_of_alt) 

1186 
apply (rule refl) 

1187 
done 

1188 

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

1190 
unfolding word_1_wi 

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

1192 

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

1194 
by (unfold unat_def uint_1) auto 

1195 

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

1197 
unfolding ucast_def word_1_wi 

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

1199 

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

1201 

1202 
lemmas word_arith_alts = 

1203 
word_sub_wi [unfolded succ_def pred_def, standard] 

1204 
word_arith_wis [unfolded succ_def pred_def, standard] 

1205 

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

1206 
(* FIXME: Lots of duplicate lemmas *) 
37660  1207 
lemmas word_sub_alt = word_arith_alts (1) 
1208 
lemmas word_add_alt = word_arith_alts (2) 

1209 
lemmas word_mult_alt = word_arith_alts (3) 

1210 
lemmas word_minus_alt = word_arith_alts (4) 

1211 
lemmas word_succ_alt = word_arith_alts (5) 

1212 
lemmas word_pred_alt = word_arith_alts (6) 

1213 
lemmas word_0_alt = word_arith_alts (7) 

1214 
lemmas word_1_alt = word_arith_alts (8) 

1215 

1216 
subsection "Transferring goals from words to ints" 

1217 

1218 
lemma word_ths: 

1219 
shows 

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

1221 
word_pred_m1: "word_pred a = a  1" and 

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

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

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

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

1226 
rule word_uint.Abs_cases [of a], 

1227 
simp add: pred_def succ_def add_commute mult_commute 

1228 
ring_distribs new_word_of_int_homs)+ 

1229 

1230 
lemmas uint_cong = arg_cong [where f = uint] 

1231 

1232 
lemmas uint_word_ariths = 

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

1234 

1235 
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p] 

1236 

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

1238 
lemmas sint_word_ariths = uint_word_arith_bintrs 

1239 
[THEN uint_sint [symmetric, THEN trans], 

1240 
unfolded uint_sint bintr_arith1s bintr_ariths 

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

1242 

1243 
lemmas uint_div_alt = word_div_def 

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

1245 
lemmas uint_mod_alt = word_mod_def 

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

1247 

1248 
lemma word_pred_0_n1: "word_pred 0 = word_of_int 1" 

1249 
unfolding word_pred_def number_of_eq 

1250 
by (simp add : pred_def word_no_wi) 

1251 

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

1253 
by (simp add: word_pred_0_n1 number_of_eq) 

1254 

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

1256 
unfolding Min_def by (simp only: word_of_int_hom_syms) 

1257 

1258 
lemma succ_pred_no [simp]: 

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

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

1261 
unfolding word_number_of_def by (simp add : new_word_of_int_homs) 

1262 

1263 
lemma word_sp_01 [simp] : 

1264 
"word_succ 1 = 0 & word_succ 0 = 1 & word_pred 0 = 1 & word_pred 1 = 0" 

1265 
by (unfold word_0_no word_1_no) auto 

1266 

1267 
(* alternative approach to lifting arithmetic equalities *) 

1268 
lemma word_of_int_Ex: 

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

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

1271 

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

1272 
(* FIXME: redundant theorems *) 
37660  1273 
lemma word_arith_eqs: 
1274 
fixes a :: "'a::len0 word" 

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

1276 
shows 

1277 
word_add_0: "0 + a = a" and 

1278 
word_add_0_right: "a + 0 = a" and 

1279 
word_mult_1: "1 * a = a" and 

1280 
word_mult_1_right: "a * 1 = a" and 

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

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

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

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

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

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

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

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

1289 
word_left_minus: " a + a = 0" and 

1290 
word_diff_0_right: "a  0 = a" and 

1291 
word_diff_self: "a  a = 0" 

1292 
using word_of_int_Ex [of a] 

1293 
word_of_int_Ex [of b] 

1294 
word_of_int_Ex [of c] 

1295 
by (auto simp: word_of_int_hom_syms [symmetric] 

44821  1296 
add_0_right add_commute add_assoc add_left_commute 
37660  1297 
mult_commute mult_assoc mult_left_commute 
1298 
left_distrib right_distrib) 

1299 

1300 
lemmas word_add_ac = word_add_commute word_add_assoc word_add_left_commute 

1301 
lemmas word_mult_ac = word_mult_commute word_mult_assoc word_mult_left_commute 

1302 

1303 
lemmas word_plus_ac0 = word_add_0 word_add_0_right word_add_ac 

1304 
lemmas word_times_ac1 = word_mult_1 word_mult_1_right word_mult_ac 

1305 

1306 

1307 
subsection "Order on fixedlength words" 

1308 

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

1309 
lemma word_order_trans: "x <= y \<Longrightarrow> y <= z \<Longrightarrow> x <= (z :: 'a :: len0 word)" 
37660  1310 
unfolding word_le_def by auto 
1311 

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

1313 
unfolding word_le_def by auto 

1314 

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

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

1318 
lemma word_order_linear: 

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

1320 
unfolding word_le_def by auto 

1321 

1322 
lemma word_zero_le [simp] : 

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

1324 
unfolding word_le_def by auto 

1325 

1326 
instance word :: (len0) linorder 

1327 
by intro_classes (auto simp: word_less_def word_le_def) 

1328 

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

1330 
unfolding word_le_def 

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

1332 

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

1334 

1335 
lemmas word_not_simps [simp] = 

1336 
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] 

1337 

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

1339 
unfolding word_less_def by auto 

1340 

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

1342 

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

1343 
lemma word_sless_alt: "(a <s b) = (sint a < sint b)" 
37660  1344 
unfolding word_sle_def word_sless_def 
1345 
by (auto simp add: less_le) 

1346 

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

1348 
unfolding unat_def word_le_def 

1349 
by (rule nat_le_eq_zle [symmetric]) simp 

56e3520b68b2
one unified Word theory
< 