author  huffman 
Sun, 25 Mar 2012 20:15:39 +0200  
changeset 47108  2a1953f0d20d 
parent 46962  5bdcdb28be83 
child 47168  8395d7d63fc8 
permissions  rwrr 
29628  1 
(* Title: HOL/Word/Word.thy 
46124  2 
Author: Jeremy Dawson and Gerwin Klein, NICTA 
24333  3 
*) 
4 

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

29628  7 
theory Word 
41413
64cd30d6b0b8
explicit file specifications  avoid secondary load path;
wenzelm
parents:
41060
diff
changeset

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

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

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

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

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

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

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

17 

18 
subsection {* Type definition *} 

19 

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

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

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

23 
lemma uint_nonnegative: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

24 
"0 \<le> uint w" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

25 
using word.uint [of w] by simp 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

26 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

27 
lemma uint_bounded: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

28 
fixes w :: "'a::len0 word" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

29 
shows "uint w < 2 ^ len_of TYPE('a)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

30 
using word.uint [of w] by simp 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

31 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

32 
lemma uint_idem: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

33 
fixes w :: "'a::len0 word" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

34 
shows "uint w mod 2 ^ len_of TYPE('a) = uint w" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

35 
using uint_nonnegative uint_bounded by (rule mod_pos_pos_trivial) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

36 

37660  37 
definition word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word" where 
38 
 {* representation of words using unsigned or signed bins, 

39 
only difference in these is the type class *} 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

40 
"word_of_int k = Abs_word (k mod 2 ^ len_of TYPE('a))" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

41 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

42 
lemma uint_word_of_int: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

43 
"uint (word_of_int k :: 'a::len0 word) = k mod 2 ^ len_of TYPE('a)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

44 
by (auto simp add: word_of_int_def intro: Abs_word_inverse) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

45 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

46 
lemma word_of_int_uint: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

47 
"word_of_int (uint w) = w" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

48 
by (simp add: word_of_int_def uint_idem uint_inverse) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

49 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

50 
lemma word_uint_eq_iff: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

51 
"a = b \<longleftrightarrow> uint a = uint b" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

52 
by (simp add: uint_inject) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

53 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

54 
lemma word_uint_eqI: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

55 
"uint a = uint b \<Longrightarrow> a = b" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

56 
by (simp add: word_uint_eq_iff) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

57 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

58 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

59 
subsection {* Basic code generation setup *} 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

60 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

61 
definition Word :: "int \<Rightarrow> 'a::len0 word" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

62 
where 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

63 
[code_post]: "Word = word_of_int" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

64 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

65 
lemma [code abstype]: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

66 
"Word (uint w) = w" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

67 
by (simp add: Word_def word_of_int_uint) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

68 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

69 
declare uint_word_of_int [code abstract] 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

70 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

71 
instantiation word :: (len0) equal 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

72 
begin 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

73 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

74 
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" where 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

75 
"equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

76 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

77 
instance proof 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

78 
qed (simp add: equal equal_word_def word_uint_eq_iff) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

79 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

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

81 

37751  82 
notation fcomp (infixl "\<circ>>" 60) 
83 
notation scomp (infixl "\<circ>\<rightarrow>" 60) 

37660  84 

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

86 
begin 

87 

88 
definition 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

89 
"random_word i = Random.range i \<circ>\<rightarrow> (\<lambda>k. Pair ( 
37660  90 
let j = word_of_int (Code_Numeral.int_of k) :: 'a word 
91 
in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))" 

92 

93 
instance .. 

94 

95 
end 

96 

37751  97 
no_notation fcomp (infixl "\<circ>>" 60) 
98 
no_notation scomp (infixl "\<circ>\<rightarrow>" 60) 

37660  99 

100 

101 
subsection {* Type conversions and casting *} 

102 

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

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

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

106 

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

108 
"unat w = nat (uint w)" 

109 

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

111 
 "the sets of integers representing the words" 

112 
"uints n = range (bintrunc n)" 

113 

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

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

116 

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

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

119 

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

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

122 

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

124 
 "cast a word to a different length" 

125 
"scast w = word_of_int (sint w)" 

126 

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

128 
"ucast w = word_of_int (uint w)" 

129 

130 
instantiation word :: (len0) size 

131 
begin 

132 

133 
definition 

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

135 

136 
instance .. 

137 

138 
end 

139 

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

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

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

143 

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

145 
"target_size c = size (c undefined)" 

146 

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

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

149 

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

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

152 

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

154 
"of_bl bl = word_of_int (bl_to_bin bl)" 

155 

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

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

158 

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

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

161 

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

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

164 

165 
translations 

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

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

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

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

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

170 

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

173 

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

174 
lemmas lens_gt_0 = word_size_gt_0 len_gt_0 
45604  175 
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

176 

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

177 
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

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

179 

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

180 
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

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

182 

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

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

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

185 
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

186 
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

187 

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

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

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

190 
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

191 

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

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

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

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

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

196 
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

197 
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

198 
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

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

200 

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

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

202 
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

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

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

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

206 
by (rule td_ext_uint) 
46013  207 

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

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

209 

46013  210 
lemmas int_word_uint = word_uint.eq_norm 
211 

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

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

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

214 

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

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

216 
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

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

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

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

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

221 

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

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

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

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

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

226 
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

227 
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

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

229 
qed 
37660  230 

231 
subsection "Arithmetic operations" 

232 

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

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

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

235 
where 
46000  236 
"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

237 

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

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

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

240 
where 
46000  241 
"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

242 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

243 
instantiation word :: (len0) "{neg_numeral, Divides.div, comm_monoid_mult, comm_ring}" 
37660  244 
begin 
245 

246 
definition 

247 
word_0_wi: "0 = word_of_int 0" 

248 

249 
definition 

250 
word_1_wi: "1 = word_of_int 1" 

251 

252 
definition 

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

254 

255 
definition 

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

257 

258 
definition 

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

260 

261 
definition 

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

263 

264 
definition 

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

266 

267 
definition 

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

269 

46013  270 
lemmas word_arith_wis = 
271 
word_add_def word_sub_wi word_mult_def word_minus_def 

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

272 
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

273 

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

274 
lemmas arths = 
45604  275 
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

276 

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

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

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

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

281 
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

282 
wi_hom_neg: " word_of_int a = word_of_int ( a)" and 
46000  283 
wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" and 
284 
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

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

286 

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

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

288 

46013  289 
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi 
46009  290 

291 
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

292 

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

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

294 
by default (auto simp: split_word_all word_of_int_homs algebra_simps) 
37660  295 

296 
end 

297 

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

298 
instance word :: (len) comm_ring_1 
45810  299 
proof 
300 
have "0 < len_of TYPE('a)" by (rule len_gt_0) 

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

302 
unfolding word_0_wi word_1_wi 

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

304 
qed 

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

305 

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

306 
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

307 
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

308 

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

309 
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

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

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

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

314 

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

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

45547  318 

319 
subsection "Ordering" 

320 

321 
instantiation word :: (len0) linorder 

322 
begin 

323 

37660  324 
definition 
325 
word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b" 

326 

327 
definition 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

328 
word_less_def: "a < b \<longleftrightarrow> uint a < uint b" 
37660  329 

45547  330 
instance 
331 
by default (auto simp: word_less_def word_le_def) 

332 

333 
end 

334 

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

336 
"a <=s b = (sint a <= sint b)" 
37660  337 

338 
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

339 
"(x <s y) = (x <=s y & x ~= y)" 
37660  340 

341 

342 
subsection "Bitwise operations" 

343 

344 
instantiation word :: (len0) bits 

345 
begin 

346 

347 
definition 

348 
word_and_def: 

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

350 

351 
definition 

352 
word_or_def: 

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

354 

355 
definition 

356 
word_xor_def: 

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

358 

359 
definition 

360 
word_not_def: 

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

362 

363 
definition 

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

365 

366 
definition 

367 
word_set_bit_def: "set_bit a n x = 

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

369 

370 
definition 

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

372 

373 
definition 

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

375 

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

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

378 

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

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

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

382 

383 
definition 

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

385 

386 
definition 

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

388 

389 
instance .. 

390 

391 
end 

392 

393 
instantiation word :: (len) bitss 

394 
begin 

395 

396 
definition 

397 
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

398 
"msb a \<longleftrightarrow> bin_sign (sint a) = 1" 
37660  399 

400 
instance .. 

401 

402 
end 

403 

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

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

405 
"setBit w n = set_bit w n True" 
37660  406 

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

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

408 
"clearBit w n = set_bit w n False" 
37660  409 

410 

411 
subsection "Shift operations" 

412 

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

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

414 
"sshiftr1 w = word_of_int (bin_rest (sint w))" 
37660  415 

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

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

417 
"bshiftr1 b w = of_bl (b # butlast (to_bl w))" 
37660  418 

419 
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

420 
"w >>> n = (sshiftr1 ^^ n) w" 
37660  421 

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

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

423 
"mask n = (1 << n)  1" 
37660  424 

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

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

426 
"revcast w = of_bl (takefill False (len_of TYPE('b)) (to_bl w))" 
37660  427 

428 
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

429 
"slice1 n w = of_bl (takefill False n (to_bl w))" 
37660  430 

431 
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

432 
"slice n w = slice1 (size w  n) w" 
37660  433 

434 

435 
subsection "Rotation" 

436 

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

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

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

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

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

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

442 
"rotater n = rotater1 ^^ n" 
37660  443 

444 
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

445 
"word_rotr n w = of_bl (rotater n (to_bl w))" 
37660  446 

447 
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

448 
"word_rotl n w = of_bl (rotate n (to_bl w))" 
37660  449 

450 
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

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

452 
else word_rotl (nat ( i)) w)" 
37660  453 

454 

455 
subsection "Split and cat operations" 

456 

457 
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

458 
"word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))" 
37660  459 

460 
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

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

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

463 
(u, v) => (word_of_int u, word_of_int v))" 
37660  464 

465 
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

466 
"word_rcat ws = 
37660  467 
word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))" 
468 

469 
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

470 
"word_rsplit w = 
37660  471 
map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))" 
472 

473 
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

474 
"max_word = word_of_int (2 ^ len_of TYPE('a)  1)" 
37660  475 

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

477 
"of_bool False = 0" 

478 
 "of_bool True = 1" 

479 

45805  480 
(* FIXME: only provide one theorem name *) 
37660  481 
lemmas of_nth_def = word_set_bits_def 
482 

46010  483 
subsection {* Theorems about typedefs *} 
484 

37660  485 
lemma sint_sbintrunc': 
486 
"sint (word_of_int bin :: 'a word) = 

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

488 
unfolding sint_uint 

489 
by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt) 

490 

491 
lemma uint_sint: 

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

493 
unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le) 

494 

46057  495 
lemma bintr_uint: 
496 
fixes w :: "'a::len0 word" 

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

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

500 
apply (simp add: min_max.inf_absorb2) 

501 
done 

502 

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

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

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

508 
lemma td_ext_sbin: 

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

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

511 
apply (unfold td_ext_def' sint_uint) 

512 
apply (simp add : word_ubin.eq_norm) 

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

514 
apply (auto simp add : sints_def) 

515 
apply (rule sym [THEN trans]) 

516 
apply (rule word_ubin.Abs_norm) 

517 
apply (simp only: bintrunc_sbintrunc) 

518 
apply (drule sym) 

519 
apply simp 

520 
done 

521 

522 
lemmas td_ext_sint = td_ext_sbin 

523 
[simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]] 

524 

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

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

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

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

529 
interpretation word_sint: 

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

531 
word_of_int 

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

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

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

535 
by (rule td_ext_sint) 

536 

537 
interpretation word_sbin: 

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

539 
word_of_int 

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

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

542 
by (rule td_ext_sbin) 

543 

45604  544 
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] 
37660  545 

546 
lemmas td_sint = word_sint.td 

547 

548 
lemma to_bl_def': 

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

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

44762  551 
by (auto simp: to_bl_def) 
37660  552 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

553 
lemmas word_reverse_no_def [simp] = word_reverse_def [of "numeral w"] for w 
37660  554 

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

557 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

558 
lemma word_numeral_alt: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

559 
"numeral b = word_of_int (numeral b)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

560 
by (induct b, simp_all only: numeral.simps word_of_int_homs) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

561 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

562 
declare word_numeral_alt [symmetric, code_abbrev] 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

563 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

564 
lemma word_neg_numeral_alt: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

565 
"neg_numeral b = word_of_int (neg_numeral b)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

566 
by (simp only: neg_numeral_def word_numeral_alt wi_hom_neg) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

567 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

568 
declare word_neg_numeral_alt [symmetric, code_abbrev] 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

569 

45805  570 
lemma uint_bintrunc [simp]: 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

571 
"uint (numeral bin :: 'a word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

572 
bintrunc (len_of TYPE ('a :: len0)) (numeral bin)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

573 
unfolding word_numeral_alt by (rule word_ubin.eq_norm) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

574 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

575 
lemma uint_bintrunc_neg [simp]: "uint (neg_numeral bin :: 'a word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

576 
bintrunc (len_of TYPE ('a :: len0)) (neg_numeral bin)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

577 
by (simp only: word_neg_numeral_alt word_ubin.eq_norm) 
37660  578 

45805  579 
lemma sint_sbintrunc [simp]: 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

580 
"sint (numeral bin :: 'a word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

581 
sbintrunc (len_of TYPE ('a :: len)  1) (numeral bin)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

582 
by (simp only: word_numeral_alt word_sbin.eq_norm) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

583 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

584 
lemma sint_sbintrunc_neg [simp]: "sint (neg_numeral bin :: 'a word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

585 
sbintrunc (len_of TYPE ('a :: len)  1) (neg_numeral bin)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

586 
by (simp only: word_neg_numeral_alt word_sbin.eq_norm) 
37660  587 

45805  588 
lemma unat_bintrunc [simp]: 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

589 
"unat (numeral bin :: 'a :: len0 word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

590 
nat (bintrunc (len_of TYPE('a)) (numeral bin))" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

591 
by (simp only: unat_def uint_bintrunc) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

592 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

593 
lemma unat_bintrunc_neg [simp]: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

594 
"unat (neg_numeral bin :: 'a :: len0 word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

595 
nat (bintrunc (len_of TYPE('a)) (neg_numeral bin))" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

596 
by (simp only: unat_def uint_bintrunc_neg) 
37660  597 

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

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

601 
apply (rule box_equals) 

602 
defer 

603 
apply (rule word_ubin.norm_Rep)+ 

604 
apply simp 

605 
done 

606 

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

609 

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

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

612 

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

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

615 

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

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

37660  618 

619 
lemma sign_uint_Pls [simp]: 

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

620 
"bin_sign (uint x) = 0" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

621 
by (simp add: sign_Pls_ge_0) 
37660  622 

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

625 

626 
lemma uint_m2p_not_non_neg: 

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

628 
by (simp only: not_le uint_m2p_neg) 

37660  629 

630 
lemma lt2p_lem: 

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

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

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

37660  636 

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

637 
lemma uint_nat: "uint w = int (unat w)" 
37660  638 
unfolding unat_def by auto 
639 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

640 
lemma uint_numeral: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

641 
"uint (numeral b :: 'a :: len0 word) = numeral b mod 2 ^ len_of TYPE('a)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

642 
unfolding word_numeral_alt 
37660  643 
by (simp only: int_word_uint) 
644 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

645 
lemma uint_neg_numeral: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

646 
"uint (neg_numeral b :: 'a :: len0 word) = neg_numeral b mod 2 ^ len_of TYPE('a)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

647 
unfolding word_neg_numeral_alt 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

648 
by (simp only: int_word_uint) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

649 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

650 
lemma unat_numeral: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

651 
"unat (numeral b::'a::len0 word) = numeral b mod 2 ^ len_of TYPE ('a)" 
37660  652 
apply (unfold unat_def) 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

653 
apply (clarsimp simp only: uint_numeral) 
37660  654 
apply (rule nat_mod_distrib [THEN trans]) 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

655 
apply (rule zero_le_numeral) 
37660  656 
apply (simp_all add: nat_power_eq) 
657 
done 

658 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

659 
lemma sint_numeral: "sint (numeral b :: 'a :: len word) = (numeral b + 
37660  660 
2 ^ (len_of TYPE('a)  1)) mod 2 ^ len_of TYPE('a)  
661 
2 ^ (len_of TYPE('a)  1)" 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

662 
unfolding word_numeral_alt by (rule int_word_sint) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

663 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

664 
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0" 
45958  665 
unfolding word_0_wi .. 
666 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

667 
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1" 
45958  668 
unfolding word_1_wi .. 
669 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

670 
lemma word_of_int_numeral [simp] : 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

671 
"(word_of_int (numeral bin) :: 'a :: len0 word) = (numeral bin)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

672 
unfolding word_numeral_alt .. 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

673 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

674 
lemma word_of_int_neg_numeral [simp]: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

675 
"(word_of_int (neg_numeral bin) :: 'a :: len0 word) = (neg_numeral bin)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

676 
unfolding neg_numeral_def word_numeral_alt wi_hom_syms .. 
37660  677 

678 
lemma word_int_case_wi: 

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

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

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

682 

683 
lemma word_int_split: 

684 
"P (word_int_case f x) = 

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

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

687 
unfolding word_int_case_def 

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

689 

690 
lemma word_int_split_asm: 

691 
"P (word_int_case f x) = 

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

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

694 
unfolding word_int_case_def 

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

45805  696 

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

37660  699 

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

701 
unfolding word_size by (rule uint_range') 

702 

703 
lemma sint_range_size: 

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

705 
unfolding word_size by (rule sint_range') 

706 

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

709 

710 
lemma sint_below_size: 

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

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

37660  713 

46010  714 
subsection {* Testing bits *} 
715 

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

718 

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

720 
apply (unfold word_test_bit_def) 

721 
apply (subst word_ubin.norm_Rep [symmetric]) 

722 
apply (simp only: nth_bintr word_size) 

723 
apply fast 

724 
done 

725 

46021  726 
lemma word_eq_iff: 
727 
fixes x y :: "'a::len0 word" 

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

729 
unfolding uint_inject [symmetric] bin_eq_iff word_test_bit_def [symmetric] 

730 
by (metis test_bit_size [unfolded word_size]) 

731 

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

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

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

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

37660  739 

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

741 
unfolding word_test_bit_def word_size 

742 
by (simp add: nth_bintr [symmetric]) 

743 

744 
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] 

745 

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

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

750 
apply assumption 

751 
done 

752 

46057  753 
lemma bin_nth_sint: 
754 
fixes w :: "'a::len word" 

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

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

37660  757 
apply (subst word_sbin.norm_Rep [symmetric]) 
46057  758 
apply (auto simp add: nth_sbintr) 
37660  759 
done 
760 

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

762 
lemma td_bl: 

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

764 
of_bl 

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

766 
apply (unfold type_definition_def of_bl_def to_bl_def) 

767 
apply (simp add: word_ubin.eq_norm) 

768 
apply safe 

769 
apply (drule sym) 

770 
apply simp 

771 
done 

772 

773 
interpretation word_bl: 

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

775 
of_bl 

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

777 
by (rule td_bl) 

778 

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

779 
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

780 

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

781 
lemma word_size_bl: "size w = size (to_bl w)" 
37660  782 
unfolding word_size by auto 
783 

784 
lemma to_bl_use_of_bl: 

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

786 
by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq]) 
37660  787 

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

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

790 

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

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

793 

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

794 
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

795 
by (metis word_rev_rev) 
37660  796 

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

799 

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

801 
unfolding word_bl_Rep' by (rule len_gt_0) 

802 

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

804 
by (fact length_bl_gt_0 [unfolded length_greater_0_conv]) 

805 

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

807 
by (fact length_bl_gt_0 [THEN gr_implies_not0]) 

37660  808 

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

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

812 
apply simp 

813 
done 

814 

815 
lemma of_bl_drop': 

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

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

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

820 
done 

821 

822 
lemma test_bit_of_bl: 

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

824 
apply (unfold of_bl_def word_test_bit_def) 

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

826 
done 

827 

828 
lemma no_of_bl: 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

829 
"(numeral bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) (numeral bin))" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

830 
unfolding of_bl_def by simp 
37660  831 

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

832 
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)" 
37660  833 
unfolding word_size to_bl_def by auto 
834 

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

836 
unfolding uint_bl by (simp add : word_size) 

837 

838 
lemma to_bl_of_bin: 

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

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

841 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

842 
lemma to_bl_numeral [simp]: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

843 
"to_bl (numeral bin::'a::len0 word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

844 
bin_to_bl (len_of TYPE('a)) (numeral bin)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

845 
unfolding word_numeral_alt by (rule to_bl_of_bin) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

846 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

847 
lemma to_bl_neg_numeral [simp]: 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

848 
"to_bl (neg_numeral bin::'a::len0 word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

849 
bin_to_bl (len_of TYPE('a)) (neg_numeral bin)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

850 
unfolding word_neg_numeral_alt by (rule to_bl_of_bin) 
37660  851 

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

853 
unfolding uint_bl by (simp add : word_size) 

46011  854 

855 
lemma uint_bl_bin: 

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

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

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

45604  859 

37660  860 
(* naturals *) 
861 
lemma uints_unats: "uints n = int ` unats n" 

862 
apply (unfold unats_def uints_num) 

863 
apply safe 

864 
apply (rule_tac image_eqI) 

865 
apply (erule_tac nat_0_le [symmetric]) 

866 
apply auto 

867 
apply (erule_tac nat_less_iff [THEN iffD2]) 

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

869 
apply (auto simp add : nat_power_eq int_power) 

870 
done 

871 

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

873 
by (auto simp add : uints_unats image_iff) 

874 

46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset

875 
lemmas bintr_num = word_ubin.norm_eq_iff 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

876 
[of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b 
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset

877 
lemmas sbintr_num = word_sbin.norm_eq_iff 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

878 
[of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b 
37660  879 

880 
lemma num_of_bintr': 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

881 
"bintrunc (len_of TYPE('a :: len0)) (numeral a) = (numeral b) \<Longrightarrow> 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

882 
numeral a = (numeral b :: 'a word)" 
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset

883 
unfolding bintr_num by (erule subst, simp) 
37660  884 

885 
lemma num_of_sbintr': 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

886 
"sbintrunc (len_of TYPE('a :: len)  1) (numeral a) = (numeral b) \<Longrightarrow> 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

887 
numeral a = (numeral b :: 'a word)" 
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset

888 
unfolding sbintr_num by (erule subst, simp) 
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset

889 

5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset

890 
lemma num_abs_bintr: 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

891 
"(numeral x :: 'a word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

892 
word_of_int (bintrunc (len_of TYPE('a::len0)) (numeral x))" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

893 
by (simp only: word_ubin.Abs_norm word_numeral_alt) 
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset

894 

5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset

895 
lemma num_abs_sbintr: 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

896 
"(numeral x :: 'a word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

897 
word_of_int (sbintrunc (len_of TYPE('a::len)  1) (numeral x))" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

898 
by (simp only: word_sbin.Abs_norm word_numeral_alt) 
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset

899 

37660  900 
(** cast  note, no arg for new length, as it's determined by type of result, 
901 
thus in "cast w = w, the type means cast to length of w! **) 

902 

903 
lemma ucast_id: "ucast w = w" 

904 
unfolding ucast_def by auto 

905 

906 
lemma scast_id: "scast w = w" 

907 
unfolding scast_def by auto 

908 

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

909 
lemma ucast_bl: "ucast w = of_bl (to_bl w)" 
37660  910 
unfolding ucast_def of_bl_def uint_bl 
911 
by (auto simp add : word_size) 

912 

913 
lemma nth_ucast: 

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

915 
apply (unfold ucast_def test_bit_bin) 

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

917 
apply (fast elim!: bin_nth_uint_imp) 

918 
done 

919 

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

921 

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

922 
lemma ucast_bintr [simp]: 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

923 
"ucast (numeral w ::'a::len0 word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

924 
word_of_int (bintrunc (len_of TYPE('a)) (numeral w))" 
37660  925 
unfolding ucast_def by simp 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

926 
(* TODO: neg_numeral *) 
37660  927 

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

928 
lemma scast_sbintr [simp]: 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

929 
"scast (numeral w ::'a::len word) = 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

930 
word_of_int (sbintrunc (len_of TYPE('a)  Suc 0) (numeral w))" 
37660  931 
unfolding scast_def by simp 
932 

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

935 

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

937 
unfolding target_size_def word_size Let_def .. 

938 

939 
lemma is_down: 

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

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

942 
unfolding is_down_def source_size target_size .. 

943 

944 
lemma is_up: 

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

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

947 
unfolding is_up_def source_size target_size .. 

37660  948 

45604  949 
lemmas is_up_down = trans [OF is_up is_down [symmetric]] 
37660  950 

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

954 
apply (rule ext) 

955 
apply (unfold ucast_def scast_def uint_sint) 

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

957 
apply simp 

958 
done 

959 

45811  960 
lemma word_rev_tf: 
961 
"to_bl (of_bl bl::'a::len0 word) = 

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

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

965 

45811  966 
lemma word_rep_drop: 
967 
"to_bl (of_bl bl::'a::len0 word) = 

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

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

970 
by (simp add: word_rev_tf takefill_alt rev_take) 

37660  971 

972 
lemma to_bl_ucast: 

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

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

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

976 
apply (unfold ucast_bl) 

977 
apply (rule trans) 

978 
apply (rule word_rep_drop) 

979 
apply simp 

980 
done 

981 

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

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

986 

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

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

991 

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

993 
"sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> 
37660  994 
to_bl (sc w) = drop n (to_bl w)" 
995 
apply (subgoal_tac "sc = ucast") 

996 
apply safe 

997 
apply simp 

45811  998 
apply (erule ucast_down_drop) 
999 
apply (rule down_cast_same [symmetric]) 

37660  1000 
apply (simp add : source_size target_size is_down) 
1001 
done 

1002 

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

1004 
"sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w" 
37660  1005 
apply (unfold is_up) 
1006 
apply safe 

1007 
apply (simp add: scast_def word_sbin.eq_norm) 

1008 
apply (rule box_equals) 

1009 
prefer 3 

1010 
apply (rule word_sbin.norm_Rep) 

1011 
apply (rule sbintrunc_sbintrunc_l) 

1012 
defer 

1013 
apply (subst word_sbin.norm_Rep) 

1014 
apply (rule refl) 

1015 
apply simp 

1016 
done 

1017 

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

1019 
"uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w" 
37660  1020 
apply (unfold is_up) 
1021 
apply safe 

1022 
apply (rule bin_eqI) 

1023 
apply (fold word_test_bit_def) 

1024 
apply (auto simp add: nth_ucast) 

1025 
apply (auto simp add: test_bit_bin) 

1026 
done 

45811  1027 

1028 
lemma ucast_up_ucast [OF refl]: 

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

37660  1030 
apply (simp (no_asm) add: ucast_def) 
1031 
apply (clarsimp simp add: uint_up_ucast) 

1032 
done 

1033 

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

37660  1036 
apply (simp (no_asm) add: scast_def) 
1037 
apply (clarsimp simp add: sint_up_scast) 

1038 
done 

1039 

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

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

1044 
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] 

1045 
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] 

1046 

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

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

1049 
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] 

1050 
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] 

1051 

1052 
lemma up_ucast_surj: 

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

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

1056 

1057 
lemma up_scast_surj: 

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

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

1061 

1062 
lemma down_scast_inj: 

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

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

1066 

1067 
lemma down_ucast_inj: 

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

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

1071 

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

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

45811  1074 

46646  1075 
lemma ucast_down_wi [OF refl]: 
1076 
"uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (word_of_int x) = word_of_int x" 

1077 
apply (unfold is_down) 

37660  1078 
apply (clarsimp simp add: ucast_def word_ubin.eq_norm) 
1079 
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 

1080 
apply (erule bintrunc_bintrunc_ge) 

1081 
done 

45811  1082 

46646  1083 
lemma ucast_down_no [OF refl]: 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1084 
"uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (numeral bin) = numeral bin" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1085 
unfolding word_numeral_alt by clarify (rule ucast_down_wi) 
46646  1086 

45811  1087 
lemma ucast_down_bl [OF refl]: 
1088 
"uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl" 

46646  1089 
unfolding of_bl_def by clarify (erule ucast_down_wi) 
37660  1090 

1091 
lemmas slice_def' = slice_def [unfolded word_size] 

1092 
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] 

1093 

1094 
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def 

1095 

1096 

1097 
subsection {* Word Arithmetic *} 

1098 

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

46012  1100 
unfolding word_less_def word_le_def by (simp add: less_le) 
37660  1101 

1102 
lemma signed_linorder: "class.linorder word_sle word_sless" 

46124  1103 
by default (unfold word_sle_def word_sless_def, auto) 
37660  1104 

1105 
interpretation signed: linorder "word_sle" "word_sless" 

1106 
by (rule signed_linorder) 

1107 

1108 
lemma udvdI: 

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

1109 
"0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" 
37660  1110 
by (auto simp: udvd_def) 
1111 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1112 
lemmas word_div_no [simp] = word_div_def [of "numeral a" "numeral b"] for a b 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1113 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1114 
lemmas word_mod_no [simp] = word_mod_def [of "numeral a" "numeral b"] for a b 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1115 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1116 
lemmas word_less_no [simp] = word_less_def [of "numeral a" "numeral b"] for a b 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1117 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1118 
lemmas word_le_no [simp] = word_le_def [of "numeral a" "numeral b"] for a b 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1119 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1120 
lemmas word_sless_no [simp] = word_sless_def [of "numeral a" "numeral b"] for a b 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1121 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1122 
lemmas word_sle_no [simp] = word_sle_def [of "numeral a" "numeral b"] for a b 
37660  1123 

46020  1124 
lemma word_1_no: "(1::'a::len0 word) = Numeral1" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1125 
by (simp add: word_numeral_alt) 
37660  1126 

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

1127 
lemma word_m1_wi: "1 = word_of_int 1" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1128 
by (rule word_neg_numeral_alt) 
37660  1129 

46648  1130 
lemma word_0_bl [simp]: "of_bl [] = 0" 
1131 
unfolding of_bl_def by simp 

37660  1132 

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

46648  1134 
unfolding of_bl_def by (simp add: bl_to_bin_def) 
1135 

1136 
lemma uint_eq_0 [simp]: "uint 0 = 0" 

1137 
unfolding word_0_wi word_ubin.eq_norm by simp 

37660  1138 

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

1139 
lemma of_bl_0 [simp]: "of_bl (replicate n False) = 0" 
46648  1140 
by (simp add: of_bl_def bl_to_bin_rep_False) 
37660  1141 

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

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

1145 
by (simp add: word_size bin_to_bl_zero) 
37660  1146 

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

1148 
by (auto intro!: word_uint.Rep_eqD) 

1149 

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

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

1152 

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

1154 
unfolding unat_def by auto 

1155 

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

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

1159 
defer 

1160 
apply (rule word_uint.Rep_inverse)+ 

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

1162 
apply simp 

1163 
done 

1164 

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

1165 
lemmas size_0_same = size_0_same' [unfolded word_size] 
37660  1166 

1167 
lemmas unat_eq_0 = unat_0_iff 

1168 
lemmas unat_eq_zero = unat_0_iff 

1169 

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

1171 
by (auto simp: unat_0_iff [symmetric]) 

1172 

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

1174 
unfolding ucast_def by simp 
45958  1175 

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

1177 
unfolding sint_uint by simp 

1178 

1179 
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

1180 
unfolding scast_def by simp 
37660  1181 

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

45958  1183 
unfolding word_m1_wi by (simp add: word_sbin.eq_norm) 
1184 

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

1186 
unfolding scast_def by simp 

1187 

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

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

1190 
by (simp add: word_ubin.eq_norm bintrunc_minus_simps del: word_of_int_1) 
45958  1191 

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

1193 
unfolding unat_def by simp 

1194 

1195 
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

1196 
unfolding ucast_def by simp 
37660  1197 

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

1199 

1200 
lemmas word_arith_alts = 

46000  1201 
word_sub_wi 
1202 
word_arith_wis (* FIXME: duplicate *) 

1203 

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

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

37660  1206 

1207 
subsection "Transferring goals from words to ints" 

1208 

1209 
lemma word_ths: 

1210 
shows 

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

1212 
word_pred_m1: "word_pred a = a  1" and 

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

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

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

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

1217 
rule word_uint.Abs_cases [of a], 

46000  1218 
simp add: add_commute mult_commute 
46009  1219 
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

1220 
del: word_of_int_0 word_of_int_1)+ 
37660  1221 

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

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

1223 
by simp 
37660  1224 

1225 
lemmas uint_word_ariths = 

45604  1226 
word_arith_alts [THEN trans [OF uint_cong int_word_uint]] 
37660  1227 

1228 
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p] 

1229 

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

1231 
lemmas sint_word_ariths = uint_word_arith_bintrs 

1232 
[THEN uint_sint [symmetric, THEN trans], 

1233 
unfolded uint_sint bintr_arith1s bintr_ariths 

45604  1234 
len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep] 
1235 

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

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

37660  1238 

1239 
lemma word_pred_0_n1: "word_pred 0 = word_of_int 1" 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1240 
unfolding word_pred_def uint_eq_0 by simp 
37660  1241 

1242 
lemma succ_pred_no [simp]: 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1243 
"word_succ (numeral w) = numeral w + 1" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1244 
"word_pred (numeral w) = numeral w  1" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1245 
"word_succ (neg_numeral w) = neg_numeral w + 1" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1246 
"word_pred (neg_numeral w) = neg_numeral w  1" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1247 
unfolding word_succ_p1 word_pred_m1 by simp_all 
37660  1248 

1249 
lemma word_sp_01 [simp] : 

1250 
"word_succ 1 = 0 & word_succ 0 = 1 & word_pred 0 = 1 & word_pred 1 = 0" 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1251 
unfolding word_succ_p1 word_pred_m1 by simp_all 
37660  1252 

1253 
(* alternative approach to lifting arithmetic equalities *) 

1254 
lemma word_of_int_Ex: 

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

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

1257 

1258 

1259 
subsection "Order on fixedlength words" 

1260 

1261 
lemma word_zero_le [simp] : 

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

1263 
unfolding word_le_def by auto 

1264 

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

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

1268 

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

1269 
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

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

1271 
by (simp only: word_m1_wi word_uint.eq_norm m1mod2k) auto 
37660  1272 

1273 
lemmas word_not_simps [simp] = 

1274 
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] 

1275 

47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1276 
lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> (y :: 'a :: len0 word)" 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1277 
by (simp add: less_le) 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1278 

2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1279 
lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y 
37660  1280 

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

1281 
lemma word_sless_alt: "(a <s b) = (sint a < sint b)" 
37660  1282 
unfolding word_sle_def word_sless_def 
1283 
by (auto simp add: less_le) 

1284 

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

1286 
unfolding unat_def word_le_def 

1287 
by (rule nat_le_eq_zle [symmetric]) simp 

1288 

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

1290 
unfolding unat_def word_less_alt 

1291 
by (rule nat_less_eq_zless [symmetric]) simp 

1292 

1293 
lemma wi_less: 

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

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

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

1297 

1298 
lemma wi_le: 

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

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

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

1302 

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

1304 
apply (unfold udvd_def) 

1305 
apply safe 

1306 
apply (simp add: unat_def nat_mult_distrib) 

1307 
apply (simp add: uint_nat int_mult) 

1308 
apply (rule exI) 

1309 
apply safe 

1310 
prefer 2 

1311 
apply (erule notE) 

1312 
apply (rule refl) 

1313 
apply force 

1314 
done 

1315 

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

1317 
unfolding dvd_def udvd_nat_alt by force 

1318 

45604  1319 
lemmas unat_mono = word_less_nat_alt [THEN iffD1] 
37660  1320 

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

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

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

1325 
prefer 2 

1326 
apply (drule contrapos_nn) 
