author  wenzelm 
Sat, 14 Dec 2013 17:28:05 +0100  
changeset 54742  7a86358a3c0b 
parent 54489  03ff4d1e6784 
child 54743  b9ae4a2f615b 
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 
"~~/src/HOL/Library/Boolean_Algebra" 
54224
9fda41a04c32
separated bit operations on type bit from generic syntactic bit operations
haftmann
parents:
54221
diff
changeset

11 
Bit_Bit 
41413
64cd30d6b0b8
explicit file specifications  avoid secondary load path;
wenzelm
parents:
41060
diff
changeset

12 
Bool_List_Representation 
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
51717
diff
changeset

13 
Misc_Typedef 
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
51717
diff
changeset

14 
Word_Miscellaneous 
37660  15 
begin 
16 

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

18 

19 
subsection {* Type definition *} 

20 

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

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

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

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

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

27 

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

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

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

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

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

32 

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

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

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

35 
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

36 
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

37 

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

40 
only difference in these is the type class *} 

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

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

42 

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

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

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

45 
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

46 

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

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

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

49 
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

50 

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

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

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

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

54 

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

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

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

57 
by (simp add: word_uint_eq_iff) 
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 

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

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

61 

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

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

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

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

65 

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

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

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

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

69 

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

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

71 

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

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

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

74 

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

75 
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

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

77 

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

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

79 
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

80 

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

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

82 

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

37660  85 

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

87 
begin 

88 

89 
definition 

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

90 
"random_word i = Random.range i \<circ>\<rightarrow> (\<lambda>k. Pair ( 
51143
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
49834
diff
changeset

91 
let j = word_of_int (int_of_integer (integer_of_natural k)) :: 'a word 
37660  92 
in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))" 
93 

94 
instance .. 

95 

96 
end 

97 

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

37660  100 

101 

102 
subsection {* Type conversions and casting *} 

103 

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

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

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

107 

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

109 
"unat w = nat (uint w)" 

110 

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

112 
 "the sets of integers representing the words" 

113 
"uints n = range (bintrunc n)" 

114 

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

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

117 

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

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

120 

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

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

123 

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

125 
 "cast a word to a different length" 

126 
"scast w = word_of_int (sint w)" 

127 

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

129 
"ucast w = word_of_int (uint w)" 

130 

131 
instantiation word :: (len0) size 

132 
begin 

133 

134 
definition 

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

136 

137 
instance .. 

138 

139 
end 

140 

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

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

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

144 

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

146 
"target_size c = size (c undefined)" 

147 

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

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

150 

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

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

153 

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

155 
"of_bl bl = word_of_int (bl_to_bin bl)" 

156 

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

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

159 

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

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

162 

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

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

165 

166 
translations 

46136
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 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

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

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

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

171 

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

174 

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

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

177 

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

178 
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

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

180 

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

181 
lemma 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

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

183 

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

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

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

186 
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

187 
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

188 

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

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

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

191 
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

192 

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

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

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

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

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

197 
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

198 
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

199 
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

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

201 

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

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

203 
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

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

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

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

207 
by (rule td_ext_uint) 
46013  208 

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

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

210 

46013  211 
lemmas int_word_uint = word_uint.eq_norm 
212 

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

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

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

215 

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

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

217 
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

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

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

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

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

222 

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

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

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

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

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

227 
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

228 
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

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

230 
qed 
37660  231 

47372  232 
subsection {* Correspondence relation for theorem transfer *} 
233 

234 
definition cr_word :: "int \<Rightarrow> 'a::len0 word \<Rightarrow> bool" 

235 
where "cr_word \<equiv> (\<lambda>x y. word_of_int x = y)" 

236 

47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

237 
lemma Quotient_word: 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

238 
"Quotient (\<lambda>x y. bintrunc (len_of TYPE('a)) x = bintrunc (len_of TYPE('a)) y) 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

239 
word_of_int uint (cr_word :: _ \<Rightarrow> 'a::len0 word \<Rightarrow> bool)" 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

240 
unfolding Quotient_alt_def cr_word_def 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

241 
by (simp add: word_ubin.norm_eq_iff) 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

242 

47377
360d7ed4cc0f
use standard quotient lemmas to generate transfer rules
huffman
parents:
47374
diff
changeset

243 
lemma reflp_word: 
360d7ed4cc0f
use standard quotient lemmas to generate transfer rules
huffman
parents:
47374
diff
changeset

244 
"reflp (\<lambda>x y. bintrunc (len_of TYPE('a::len0)) x = bintrunc (len_of TYPE('a)) y)" 
360d7ed4cc0f
use standard quotient lemmas to generate transfer rules
huffman
parents:
47374
diff
changeset

245 
by (simp add: reflp_def) 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

246 

47941
22e001795641
don't generate code in Word because it breaks the current code setup
kuncar
parents:
47611
diff
changeset

247 
setup_lifting(no_code) Quotient_word reflp_word 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

248 

47521
69f95ac85c3d
tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents:
47387
diff
changeset

249 
text {* TODO: The next lemma could be generated automatically. *} 
47372  250 

251 
lemma uint_transfer [transfer_rule]: 

51375
d9e62d9c98de
patch Isabelle ditribution to conform to changes regarding the parametricity
kuncar
parents:
51286
diff
changeset

252 
"(fun_rel pcr_word op =) (bintrunc (len_of TYPE('a))) 
47372  253 
(uint :: 'a::len0 word \<Rightarrow> int)" 
51375
d9e62d9c98de
patch Isabelle ditribution to conform to changes regarding the parametricity
kuncar
parents:
51286
diff
changeset

254 
unfolding fun_rel_def word.pcr_cr_eq cr_word_def by (simp add: word_ubin.eq_norm) 
47372  255 

37660  256 
subsection "Arithmetic operations" 
257 

47387
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

258 
lift_definition word_succ :: "'a::len0 word \<Rightarrow> 'a word" is "\<lambda>x. x + 1" 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

259 
by (metis bintr_ariths(6)) 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

260 

47387
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

261 
lift_definition word_pred :: "'a::len0 word \<Rightarrow> 'a word" is "\<lambda>x. x  1" 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

262 
by (metis bintr_ariths(7)) 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

263 

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

264 
instantiation word :: (len0) "{neg_numeral, Divides.div, comm_monoid_mult, comm_ring}" 
37660  265 
begin 
266 

47387
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

267 
lift_definition zero_word :: "'a word" is "0" . 
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

268 

a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

269 
lift_definition one_word :: "'a word" is "1" . 
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

270 

a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

271 
lift_definition plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "op +" 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

272 
by (metis bintr_ariths(2)) 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

273 

47387
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

274 
lift_definition minus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "op " 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

275 
by (metis bintr_ariths(3)) 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

276 

47387
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

277 
lift_definition uminus_word :: "'a word \<Rightarrow> 'a word" is uminus 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

278 
by (metis bintr_ariths(5)) 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

279 

47387
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

280 
lift_definition times_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "op *" 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

281 
by (metis bintr_ariths(4)) 
37660  282 

283 
definition 

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

285 

286 
definition 

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

288 

47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

289 
instance 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

290 
by default (transfer, simp add: algebra_simps)+ 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

291 

9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

292 
end 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

293 

9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

294 
text {* Legacy theorems: *} 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

295 

47611  296 
lemma word_arith_wis [code]: shows 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

297 
word_add_def: "a + b = word_of_int (uint a + uint b)" and 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

298 
word_sub_wi: "a  b = word_of_int (uint a  uint b)" and 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

299 
word_mult_def: "a * b = word_of_int (uint a * uint b)" and 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

300 
word_minus_def: " a = word_of_int ( uint a)" and 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

301 
word_succ_alt: "word_succ a = word_of_int (uint a + 1)" and 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

302 
word_pred_alt: "word_pred a = word_of_int (uint a  1)" and 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

303 
word_0_wi: "0 = word_of_int 0" and 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

304 
word_1_wi: "1 = word_of_int 1" 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

305 
unfolding plus_word_def minus_word_def times_word_def uminus_word_def 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

306 
unfolding word_succ_def word_pred_def zero_word_def one_word_def 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

307 
by simp_all 
45545
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 
lemmas arths = 
45604  310 
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

311 

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

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

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

314 
wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and 
46013  315 
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

316 
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

317 
wi_hom_neg: " word_of_int a = word_of_int ( a)" and 
46000  318 
wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" and 
319 
wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a  1)" 

47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

320 
by (transfer, simp)+ 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

321 

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

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

323 

46013  324 
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi 
46009  325 

326 
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

327 

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

328 
instance word :: (len) comm_ring_1 
45810  329 
proof 
330 
have "0 < len_of TYPE('a)" by (rule len_gt_0) 

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

47372  332 
by  (transfer, auto simp add: gr0_conv_Suc) 
45810  333 
qed 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

334 

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

335 
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

336 
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

337 

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

338 
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

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

340 
apply (case_tac x rule: int_diff_cases) 
46013  341 
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

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

343 

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

345 
"a udvd b = (EX n>=0. uint b = n * uint a)" 
37660  346 

45547  347 

348 
subsection "Ordering" 

349 

350 
instantiation word :: (len0) linorder 

351 
begin 

352 

37660  353 
definition 
354 
word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b" 

355 

356 
definition 

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

357 
word_less_def: "a < b \<longleftrightarrow> uint a < uint b" 
37660  358 

45547  359 
instance 
360 
by default (auto simp: word_less_def word_le_def) 

361 

362 
end 

363 

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

365 
"a <=s b = (sint a <= sint b)" 
37660  366 

367 
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

368 
"(x <s y) = (x <=s y & x ~= y)" 
37660  369 

370 

371 
subsection "Bitwise operations" 

372 

373 
instantiation word :: (len0) bits 

374 
begin 

375 

47387
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

376 
lift_definition bitNOT_word :: "'a word \<Rightarrow> 'a word" is bitNOT 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

377 
by (metis bin_trunc_not) 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

378 

47387
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

379 
lift_definition bitAND_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is bitAND 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

380 
by (metis bin_trunc_and) 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

381 

47387
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

382 
lift_definition bitOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is bitOR 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

383 
by (metis bin_trunc_or) 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

384 

47387
a0f257197741
remove nowunnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset

385 
lift_definition bitXOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is bitXOR 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

386 
by (metis bin_trunc_xor) 
37660  387 

388 
definition 

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

390 

391 
definition 

392 
word_set_bit_def: "set_bit a n x = 

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

394 

395 
definition 

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

397 

398 
definition 

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

400 

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

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

403 

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

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

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

407 

408 
definition 

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

410 

411 
definition 

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

413 

414 
instance .. 

415 

416 
end 

417 

47611  418 
lemma [code]: shows 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

419 
word_not_def: "NOT (a::'a::len0 word) = word_of_int (NOT (uint a))" and 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

420 
word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)" and 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

421 
word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)" and 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

422 
word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)" 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

423 
unfolding bitNOT_word_def bitAND_word_def bitOR_word_def bitXOR_word_def 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

424 
by simp_all 
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

425 

37660  426 
instantiation word :: (len) bitss 
427 
begin 

428 

429 
definition 

430 
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

431 
"msb a \<longleftrightarrow> bin_sign (sint a) = 1" 
37660  432 

433 
instance .. 

434 

435 
end 

436 

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

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

438 
"setBit w n = set_bit w n True" 
37660  439 

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

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

441 
"clearBit w n = set_bit w n False" 
37660  442 

443 

444 
subsection "Shift operations" 

445 

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

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

447 
"sshiftr1 w = word_of_int (bin_rest (sint w))" 
37660  448 

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

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

450 
"bshiftr1 b w = of_bl (b # butlast (to_bl w))" 
37660  451 

452 
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

453 
"w >>> n = (sshiftr1 ^^ n) w" 
37660  454 

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

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

456 
"mask n = (1 << n)  1" 
37660  457 

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

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

459 
"revcast w = of_bl (takefill False (len_of TYPE('b)) (to_bl w))" 
37660  460 

461 
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

462 
"slice1 n w = of_bl (takefill False n (to_bl w))" 
37660  463 

464 
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

465 
"slice n w = slice1 (size w  n) w" 
37660  466 

467 

468 
subsection "Rotation" 

469 

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

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

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

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

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

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

475 
"rotater n = rotater1 ^^ n" 
37660  476 

477 
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

478 
"word_rotr n w = of_bl (rotater n (to_bl w))" 
37660  479 

480 
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

481 
"word_rotl n w = of_bl (rotate n (to_bl w))" 
37660  482 

483 
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

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

485 
else word_rotl (nat ( i)) w)" 
37660  486 

487 

488 
subsection "Split and cat operations" 

489 

490 
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

491 
"word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))" 
37660  492 

493 
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

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

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

496 
(u, v) => (word_of_int u, word_of_int v))" 
37660  497 

498 
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

499 
"word_rcat ws = 
37660  500 
word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))" 
501 

502 
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

503 
"word_rsplit w = 
37660  504 
map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))" 
505 

506 
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

507 
"max_word = word_of_int (2 ^ len_of TYPE('a)  1)" 
37660  508 

45805  509 
(* FIXME: only provide one theorem name *) 
37660  510 
lemmas of_nth_def = word_set_bits_def 
511 

46010  512 
subsection {* Theorems about typedefs *} 
513 

37660  514 
lemma sint_sbintrunc': 
515 
"sint (word_of_int bin :: 'a word) = 

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

517 
unfolding sint_uint 

518 
by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt) 

519 

520 
lemma uint_sint: 

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

522 
unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le) 

523 

46057  524 
lemma bintr_uint: 
525 
fixes w :: "'a::len0 word" 

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

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

529 
apply (simp add: min_max.inf_absorb2) 

530 
done 

531 

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

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

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

537 
lemma td_ext_sbin: 

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

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

540 
apply (unfold td_ext_def' sint_uint) 

541 
apply (simp add : word_ubin.eq_norm) 

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

543 
apply (auto simp add : sints_def) 

544 
apply (rule sym [THEN trans]) 

545 
apply (rule word_ubin.Abs_norm) 

546 
apply (simp only: bintrunc_sbintrunc) 

547 
apply (drule sym) 

548 
apply simp 

549 
done 

550 

551 
lemmas td_ext_sint = td_ext_sbin 

552 
[simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]] 

553 

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

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

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

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

558 
interpretation word_sint: 

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

560 
word_of_int 

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

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

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

564 
by (rule td_ext_sint) 

565 

566 
interpretation word_sbin: 

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

568 
word_of_int 

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

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

571 
by (rule td_ext_sbin) 

572 

45604  573 
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] 
37660  574 

575 
lemmas td_sint = word_sint.td 

576 

577 
lemma to_bl_def': 

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

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

44762  580 
by (auto simp: to_bl_def) 
37660  581 

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

582 
lemmas word_reverse_no_def [simp] = word_reverse_def [of "numeral w"] for w 
37660  583 

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

586 

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

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

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

589 
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

590 

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

591 
declare word_numeral_alt [symmetric, code_abbrev] 
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 word_neg_numeral_alt: 
54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

594 
" numeral b = word_of_int ( numeral b)" 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

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

596 

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

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

598 

47372  599 
lemma word_numeral_transfer [transfer_rule]: 
51375
d9e62d9c98de
patch Isabelle ditribution to conform to changes regarding the parametricity
kuncar
parents:
51286
diff
changeset

600 
"(fun_rel op = pcr_word) numeral numeral" 
54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

601 
"(fun_rel op = pcr_word) ( numeral) ( numeral)" 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

602 
apply (simp_all add: fun_rel_def word.pcr_cr_eq cr_word_def) 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

603 
using word_numeral_alt [symmetric] word_neg_numeral_alt [symmetric] by blast+ 
47372  604 

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

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

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

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

609 

54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

610 
lemma uint_bintrunc_neg [simp]: "uint ( numeral bin :: 'a word) = 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

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

612 
by (simp only: word_neg_numeral_alt word_ubin.eq_norm) 
37660  613 

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

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

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

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

618 

54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

619 
lemma sint_sbintrunc_neg [simp]: "sint ( numeral bin :: 'a word) = 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

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

621 
by (simp only: word_neg_numeral_alt word_sbin.eq_norm) 
37660  622 

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

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

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

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

627 

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

628 
lemma unat_bintrunc_neg [simp]: 
54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

629 
"unat ( numeral bin :: 'a :: len0 word) = 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

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

631 
by (simp only: unat_def uint_bintrunc_neg) 
37660  632 

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

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

636 
apply (rule box_equals) 

637 
defer 

638 
apply (rule word_ubin.norm_Rep)+ 

639 
apply simp 

640 
done 

641 

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

644 

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

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

647 

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

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

650 

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

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

37660  653 

654 
lemma sign_uint_Pls [simp]: 

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

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

656 
by (simp add: sign_Pls_ge_0) 
37660  657 

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

660 

661 
lemma uint_m2p_not_non_neg: 

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

663 
by (simp only: not_le uint_m2p_neg) 

37660  664 

665 
lemma lt2p_lem: 

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

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

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

37660  671 

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

672 
lemma uint_nat: "uint w = int (unat w)" 
37660  673 
unfolding unat_def by auto 
674 

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

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

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

677 
unfolding word_numeral_alt 
37660  678 
by (simp only: int_word_uint) 
679 

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

680 
lemma uint_neg_numeral: 
54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

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

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

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

684 

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

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

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

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

690 
apply (rule zero_le_numeral) 
37660  691 
apply (simp_all add: nat_power_eq) 
692 
done 

693 

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

694 
lemma sint_numeral: "sint (numeral b :: 'a :: len word) = (numeral b + 
37660  695 
2 ^ (len_of TYPE('a)  1)) mod 2 ^ len_of TYPE('a)  
696 
2 ^ (len_of TYPE('a)  1)" 

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

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

698 

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

699 
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0" 
45958  700 
unfolding word_0_wi .. 
701 

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

702 
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1" 
45958  703 
unfolding word_1_wi .. 
704 

54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

705 
lemma word_of_int_neg_1 [simp]: "word_of_int ( 1) =  1" 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

706 
by (simp add: wi_hom_syms) 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

707 

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

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

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

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

711 

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

712 
lemma word_of_int_neg_numeral [simp]: 
54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

713 
"(word_of_int ( numeral bin) :: 'a :: len0 word) = ( numeral bin)" 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

714 
unfolding word_numeral_alt wi_hom_syms .. 
37660  715 

716 
lemma word_int_case_wi: 

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

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

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

720 

721 
lemma word_int_split: 

722 
"P (word_int_case f x) = 

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

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

725 
unfolding word_int_case_def 

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

727 

728 
lemma word_int_split_asm: 

729 
"P (word_int_case f x) = 

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

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

732 
unfolding word_int_case_def 

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

45805  734 

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

37660  737 

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

739 
unfolding word_size by (rule uint_range') 

740 

741 
lemma sint_range_size: 

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

743 
unfolding word_size by (rule sint_range') 

744 

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

747 

748 
lemma sint_below_size: 

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

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

37660  751 

46010  752 
subsection {* Testing bits *} 
753 

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

756 

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

758 
apply (unfold word_test_bit_def) 

759 
apply (subst word_ubin.norm_Rep [symmetric]) 

760 
apply (simp only: nth_bintr word_size) 

761 
apply fast 

762 
done 

763 

46021  764 
lemma word_eq_iff: 
765 
fixes x y :: "'a::len0 word" 

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

767 
unfolding uint_inject [symmetric] bin_eq_iff word_test_bit_def [symmetric] 

768 
by (metis test_bit_size [unfolded word_size]) 

769 

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

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

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

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

37660  777 

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

779 
unfolding word_test_bit_def word_size 

780 
by (simp add: nth_bintr [symmetric]) 

781 

782 
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] 

783 

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

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

788 
apply assumption 

789 
done 

790 

46057  791 
lemma bin_nth_sint: 
792 
fixes w :: "'a::len word" 

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

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

37660  795 
apply (subst word_sbin.norm_Rep [symmetric]) 
46057  796 
apply (auto simp add: nth_sbintr) 
37660  797 
done 
798 

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

800 
lemma td_bl: 

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

802 
of_bl 

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

804 
apply (unfold type_definition_def of_bl_def to_bl_def) 

805 
apply (simp add: word_ubin.eq_norm) 

806 
apply safe 

807 
apply (drule sym) 

808 
apply simp 

809 
done 

810 

811 
interpretation word_bl: 

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

813 
of_bl 

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

815 
by (rule td_bl) 

816 

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

817 
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

818 

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

819 
lemma word_size_bl: "size w = size (to_bl w)" 
37660  820 
unfolding word_size by auto 
821 

822 
lemma to_bl_use_of_bl: 

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

824 
by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq]) 
37660  825 

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

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

828 

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

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

831 

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

832 
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

833 
by (metis word_rev_rev) 
37660  834 

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

837 

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

839 
unfolding word_bl_Rep' by (rule len_gt_0) 

840 

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

842 
by (fact length_bl_gt_0 [unfolded length_greater_0_conv]) 

843 

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

845 
by (fact length_bl_gt_0 [THEN gr_implies_not0]) 

37660  846 

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

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

850 
apply simp 

851 
done 

852 

853 
lemma of_bl_drop': 

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

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

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

858 
done 

859 

860 
lemma test_bit_of_bl: 

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

862 
apply (unfold of_bl_def word_test_bit_def) 

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

864 
done 

865 

866 
lemma no_of_bl: 

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

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

868 
unfolding of_bl_def by simp 
37660  869 

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

870 
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)" 
37660  871 
unfolding word_size to_bl_def by auto 
872 

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

874 
unfolding uint_bl by (simp add : word_size) 

875 

876 
lemma to_bl_of_bin: 

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

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

879 

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

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

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

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

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

884 

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

885 
lemma to_bl_neg_numeral [simp]: 
54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

886 
"to_bl ( numeral bin::'a::len0 word) = 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

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

888 
unfolding word_neg_numeral_alt by (rule to_bl_of_bin) 
37660  889 

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

891 
unfolding uint_bl by (simp add : word_size) 

46011  892 

893 
lemma uint_bl_bin: 

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

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

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

45604  897 

37660  898 
(* naturals *) 
899 
lemma uints_unats: "uints n = int ` unats n" 

900 
apply (unfold unats_def uints_num) 

901 
apply safe 

902 
apply (rule_tac image_eqI) 

903 
apply (erule_tac nat_0_le [symmetric]) 

904 
apply auto 

905 
apply (erule_tac nat_less_iff [THEN iffD2]) 

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

907 
apply (auto simp add : nat_power_eq int_power) 

908 
done 

909 

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

911 
by (auto simp add : uints_unats image_iff) 

912 

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

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

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

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

916 
[of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b 
37660  917 

918 
lemma num_of_bintr': 

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

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

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

921 
unfolding bintr_num by (erule subst, simp) 
37660  922 

923 
lemma num_of_sbintr': 

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

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

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

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

927 

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

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

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

930 
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

931 
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

932 

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

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

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

935 
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

936 
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

937 

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

940 

941 
lemma ucast_id: "ucast w = w" 

942 
unfolding ucast_def by auto 

943 

944 
lemma scast_id: "scast w = w" 

945 
unfolding scast_def by auto 

946 

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

947 
lemma ucast_bl: "ucast w = of_bl (to_bl w)" 
37660  948 
unfolding ucast_def of_bl_def uint_bl 
949 
by (auto simp add : word_size) 

950 

951 
lemma nth_ucast: 

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

953 
apply (unfold ucast_def test_bit_bin) 

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

955 
apply (fast elim!: bin_nth_uint_imp) 

956 
done 

957 

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

959 

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

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

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

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

964 
(* TODO: neg_numeral *) 
37660  965 

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

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

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

968 
word_of_int (sbintrunc (len_of TYPE('a)  Suc 0) (numeral w))" 
37660  969 
unfolding scast_def by simp 
970 

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

973 

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

975 
unfolding target_size_def word_size Let_def .. 

976 

977 
lemma is_down: 

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

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

980 
unfolding is_down_def source_size target_size .. 

981 

982 
lemma is_up: 

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

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

985 
unfolding is_up_def source_size target_size .. 

37660  986 

45604  987 
lemmas is_up_down = trans [OF is_up is_down [symmetric]] 
37660  988 

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

992 
apply (rule ext) 

993 
apply (unfold ucast_def scast_def uint_sint) 

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

995 
apply simp 

996 
done 

997 

45811  998 
lemma word_rev_tf: 
999 
"to_bl (of_bl bl::'a::len0 word) = 

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

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

1003 

45811  1004 
lemma word_rep_drop: 
1005 
"to_bl (of_bl bl::'a::len0 word) = 

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

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

1008 
by (simp add: word_rev_tf takefill_alt rev_take) 

37660  1009 

1010 
lemma to_bl_ucast: 

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

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

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

1014 
apply (unfold ucast_bl) 

1015 
apply (rule trans) 

1016 
apply (rule word_rep_drop) 

1017 
apply simp 

1018 
done 

1019 

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

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

1024 

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

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

1029 

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

1031 
"sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> 
37660  1032 
to_bl (sc w) = drop n (to_bl w)" 
1033 
apply (subgoal_tac "sc = ucast") 

1034 
apply safe 

1035 
apply simp 

45811  1036 
apply (erule ucast_down_drop) 
1037 
apply (rule down_cast_same [symmetric]) 

37660  1038 
apply (simp add : source_size target_size is_down) 
1039 
done 

1040 

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

1042 
"sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w" 
37660  1043 
apply (unfold is_up) 
1044 
apply safe 

1045 
apply (simp add: scast_def word_sbin.eq_norm) 

1046 
apply (rule box_equals) 

1047 
prefer 3 

1048 
apply (rule word_sbin.norm_Rep) 

1049 
apply (rule sbintrunc_sbintrunc_l) 

1050 
defer 

1051 
apply (subst word_sbin.norm_Rep) 

1052 
apply (rule refl) 

1053 
apply simp 

1054 
done 

1055 

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

1057 
"uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w" 
37660  1058 
apply (unfold is_up) 
1059 
apply safe 

1060 
apply (rule bin_eqI) 

1061 
apply (fold word_test_bit_def) 

1062 
apply (auto simp add: nth_ucast) 

1063 
apply (auto simp add: test_bit_bin) 

1064 
done 

45811  1065 

1066 
lemma ucast_up_ucast [OF refl]: 

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

37660  1068 
apply (simp (no_asm) add: ucast_def) 
1069 
apply (clarsimp simp add: uint_up_ucast) 

1070 
done 

1071 

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

37660  1074 
apply (simp (no_asm) add: scast_def) 
1075 
apply (clarsimp simp add: sint_up_scast) 

1076 
done 

1077 

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

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

1082 
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] 

1083 
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] 

1084 

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

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

1087 
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] 

1088 
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] 

1089 

1090 
lemma up_ucast_surj: 

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

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

1094 

1095 
lemma up_scast_surj: 

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

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

1099 

1100 
lemma down_scast_inj: 

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

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

1104 

1105 
lemma down_ucast_inj: 

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

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

1109 

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

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

45811  1112 

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

1115 
apply (unfold is_down) 

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

1118 
apply (erule bintrunc_bintrunc_ge) 

1119 
done 

45811  1120 

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

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

1123 
unfolding word_numeral_alt by clarify (rule ucast_down_wi) 
46646  1124 

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

46646  1127 
unfolding of_bl_def by clarify (erule ucast_down_wi) 
37660  1128 

1129 
lemmas slice_def' = slice_def [unfolded word_size] 

1130 
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] 

1131 

1132 
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def 

1133 

1134 

1135 
subsection {* Word Arithmetic *} 

1136 

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

46012  1138 
unfolding word_less_def word_le_def by (simp add: less_le) 
37660  1139 

1140 
lemma signed_linorder: "class.linorder word_sle word_sless" 

46124  1141 
by default (unfold word_sle_def word_sless_def, auto) 
37660  1142 

1143 
interpretation signed: linorder "word_sle" "word_sless" 

1144 
by (rule signed_linorder) 

1145 

1146 
lemma udvdI: 

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

1147 
"0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" 
37660  1148 
by (auto simp: udvd_def) 
1149 

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

1150 
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

1151 

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

1152 
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

1153 

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

1154 
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

1155 

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

1156 
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

1157 

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

1158 
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

1159 

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

1160 
lemmas word_sle_no [simp] = word_sle_def [of "numeral a" "numeral b"] for a b 
37660  1161 

54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

1162 
lemma word_m1_wi: " 1 = word_of_int ( 1)" 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

1163 
using word_neg_numeral_alt [of Num.One] by simp 
37660  1164 

46648  1165 
lemma word_0_bl [simp]: "of_bl [] = 0" 
1166 
unfolding of_bl_def by simp 

37660  1167 

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

46648  1169 
unfolding of_bl_def by (simp add: bl_to_bin_def) 
1170 

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

1172 
unfolding word_0_wi word_ubin.eq_norm by simp 

37660  1173 

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

1174 
lemma of_bl_0 [simp]: "of_bl (replicate n False) = 0" 
46648  1175 
by (simp add: of_bl_def bl_to_bin_rep_False) 
37660  1176 

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

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

1180 
by (simp add: word_size bin_to_bl_zero) 
37660  1181 

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

1183 
by (auto intro!: word_uint.Rep_eqD) 

1184 

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

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

1187 

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

1189 
unfolding unat_def by auto 

1190 

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

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

1194 
defer 

1195 
apply (rule word_uint.Rep_inverse)+ 

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

1197 
apply simp 

1198 
done 

1199 

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

1200 
lemmas size_0_same = size_0_same' [unfolded word_size] 
37660  1201 

1202 
lemmas unat_eq_0 = unat_0_iff 

1203 
lemmas unat_eq_zero = unat_0_iff 

1204 

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

1206 
by (auto simp: unat_0_iff [symmetric]) 

1207 

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

1209 
unfolding ucast_def by simp 
45958  1210 

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

1212 
unfolding sint_uint by simp 

1213 

1214 
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

1215 
unfolding scast_def by simp 
37660  1216 

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

54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

1218 
unfolding word_m1_wi word_sbin.eq_norm by simp 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

1219 

03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

1220 
lemma scast_n1 [simp]: "scast ( 1) =  1" 
45958  1221 
unfolding scast_def by simp 
1222 

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

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

1225 
by (simp add: word_ubin.eq_norm bintrunc_minus_simps del: word_of_int_1) 
45958  1226 

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

1228 
unfolding unat_def by simp 

1229 

1230 
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

1231 
unfolding ucast_def by simp 
37660  1232 

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

1234 

1235 
lemmas word_arith_alts = 

46000  1236 
word_sub_wi 
1237 
word_arith_wis (* FIXME: duplicate *) 

1238 

37660  1239 
subsection "Transferring goals from words to ints" 
1240 

1241 
lemma word_ths: 

1242 
shows 

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

1244 
word_pred_m1: "word_pred a = a  1" and 

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

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

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

47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

1248 
by (transfer, simp add: algebra_simps)+ 
37660  1249 

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

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

1251 
by simp 
37660  1252 

1253 
lemmas uint_word_ariths = 

45604  1254 
word_arith_alts [THEN trans [OF uint_cong int_word_uint]] 
37660  1255 

1256 
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p] 

1257 

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

1259 
lemmas sint_word_ariths = uint_word_arith_bintrs 

1260 
[THEN uint_sint [symmetric, THEN trans], 

1261 
unfolded uint_sint bintr_arith1s bintr_ariths 

45604  1262 
len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep] 
1263 

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

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

37660  1266 

1267 
lemma word_pred_0_n1: "word_pred 0 = word_of_int 1" 

47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

1268 
unfolding word_pred_m1 by simp 
37660  1269 

1270 
lemma succ_pred_no [simp]: 

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

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

1272 
"word_pred (numeral w) = numeral w  1" 
54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

1273 
"word_succ ( numeral w) =  numeral w + 1" 
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

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

1275 
unfolding word_succ_p1 word_pred_m1 by simp_all 
37660  1276 

1277 
lemma word_sp_01 [simp] : 

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

1279 
unfolding word_succ_p1 word_pred_m1 by simp_all 
37660  1280 

1281 
(* alternative approach to lifting arithmetic equalities *) 

1282 
lemma word_of_int_Ex: 

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

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

1285 

1286 

1287 
subsection "Order on fixedlength words" 

1288 

1289 
lemma word_zero_le [simp] : 

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

1291 
unfolding word_le_def by auto 

1292 

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

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

1296 

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

1297 
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

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

1299 
by (simp only: word_m1_wi word_uint.eq_norm m1mod2k) auto 
37660  1300 

1301 
lemmas word_not_simps [simp] = 

1302 
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] 

1303 

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

1304 
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

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

1306 

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

1307 
lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y 
37660  1308 

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

1309 
lemma word_sless_alt: "(a <s b) = (sint a < sint b)" 
37660  1310 
unfolding word_sle_def word_sless_def 
1311 
by (auto simp add: less_le) 

1312 

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

1314 
unfolding unat_def word_le_def 

1315 
by (rule nat_le_eq_zle [symmetric]) simp 

1316 

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