author  haftmann 
Thu, 18 Jun 2020 09:07:30 +0000  
changeset 71952  2efc5b8c7456 
parent 71951  ac6f9738c200 
child 71953  428609096812 
permissions  rwrr 
29628  1 
(* Title: HOL/Word/Word.thy 
46124  2 
Author: Jeremy Dawson and Gerwin Klein, NICTA 
24333  3 
*) 
4 

61799  5 
section \<open>A type of finite bit strings\<close> 
24350  6 

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

8 
imports 
66453
cc19f7ca2ed6
sessionqualified theory imports: isabelle imports U i d '~~/src/Benchmarks' a;
wenzelm
parents:
65363
diff
changeset

9 
"HOLLibrary.Type_Length" 
cc19f7ca2ed6
sessionqualified theory imports: isabelle imports U i d '~~/src/Benchmarks' a;
wenzelm
parents:
65363
diff
changeset

10 
"HOLLibrary.Boolean_Algebra" 
70190  11 
Bits_Int 
70342
e4d626692640
clear separation of types for bits (False / True) and Z2 (0 / 1)
haftmann
parents:
70193
diff
changeset

12 
Bits_Z2 
70192  13 
Bit_Comprehension 
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

14 
Misc_Typedef 
70170  15 
Misc_Arithmetic 
37660  16 
begin 
17 

70173  18 
text \<open>See \<^file>\<open>Word_Examples.thy\<close> for examples.\<close> 
61799  19 

20 
subsection \<open>Type definition\<close> 

37660  21 

71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

22 
quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len0) l\<close> 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

23 
morphisms rep_word word_of_int by (auto intro!: equivpI reflpI sympI transpI) 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

24 

6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

25 
lift_definition uint :: \<open>'a::len0 word \<Rightarrow> int\<close> 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

26 
is \<open>take_bit LENGTH('a)\<close> . 
37660  27 

65268  28 
lemma uint_nonnegative: "0 \<le> uint w" 
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

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

30 

70185  31 
lemma uint_bounded: "uint w < 2 ^ LENGTH('a)" 
65268  32 
for w :: "'a::len0 word" 
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

33 
by transfer (simp add: take_bit_eq_mod) 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

34 

70185  35 
lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w" 
65268  36 
for w :: "'a::len0 word" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

37 
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

38 

71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

39 
lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b" 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

40 
by transfer simp 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

41 

65268  42 
lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b" 
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

43 
using word_uint_eqI by auto 
70185  44 

45 
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len0 word) = k mod 2 ^ LENGTH('a)" 

71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

46 
by transfer (simp add: take_bit_eq_mod) 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

47 

65268  48 
lemma word_of_int_uint: "word_of_int (uint w) = w" 
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

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

50 

65268  51 
lemma split_word_all: "(\<And>x::'a::len0 word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

52 
proof 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

53 
fix x :: "'a word" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

54 
assume "\<And>x. PROP P (word_of_int x)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

55 
then have "PROP P (word_of_int (uint x))" . 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

56 
then show "PROP P x" by (simp add: word_of_int_uint) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

57 
qed 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

58 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

59 

61799  60 
subsection \<open>Type conversions and casting\<close> 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

61 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

62 
definition sint :: "'a::len word \<Rightarrow> int" 
61799  63 
\<comment> \<open>treats the mostsignificantbit as a sign bit\<close> 
70175  64 
where sint_uint: "sint w = sbintrunc (LENGTH('a)  1) (uint w)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

65 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

66 
definition unat :: "'a::len0 word \<Rightarrow> nat" 
65268  67 
where "unat w = nat (uint w)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

68 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

69 
definition uints :: "nat \<Rightarrow> int set" 
67443
3abf6a722518
standardized towards newstyle formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset

70 
\<comment> \<open>the sets of integers representing the words\<close> 
65268  71 
where "uints n = range (bintrunc n)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

72 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

73 
definition sints :: "nat \<Rightarrow> int set" 
65268  74 
where "sints n = range (sbintrunc (n  1))" 
75 

76 
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}" 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

77 
by (simp add: uints_def range_bintrunc) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

78 

65268  79 
lemma sints_num: "sints n = {i.  (2 ^ (n  1)) \<le> i \<and> i < 2 ^ (n  1)}" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

80 
by (simp add: sints_def range_sbintrunc) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

81 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

82 
definition unats :: "nat \<Rightarrow> nat set" 
65268  83 
where "unats n = {i. i < 2 ^ n}" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

84 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

85 
definition norm_sint :: "nat \<Rightarrow> int \<Rightarrow> int" 
65268  86 
where "norm_sint n w = (w + 2 ^ (n  1)) mod 2 ^ n  2 ^ (n  1)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

87 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

88 
definition scast :: "'a::len word \<Rightarrow> 'b::len word" 
67443
3abf6a722518
standardized towards newstyle formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset

89 
\<comment> \<open>cast a word to a different length\<close> 
65268  90 
where "scast w = word_of_int (sint w)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

91 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

92 
definition ucast :: "'a::len0 word \<Rightarrow> 'b::len0 word" 
65268  93 
where "ucast w = word_of_int (uint w)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

94 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

95 
instantiation word :: (len0) size 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

96 
begin 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

97 

70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset

98 
definition word_size: "size (w :: 'a word) = LENGTH('a)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

99 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

100 
instance .. 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

101 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

102 
end 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

103 

65268  104 
lemma word_size_gt_0 [iff]: "0 < size w" 
105 
for w :: "'a::len word" 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

106 
by (simp add: word_size) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

107 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

108 
lemmas lens_gt_0 = word_size_gt_0 len_gt_0 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

109 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

110 
lemma lens_not_0 [iff]: 
71942  111 
\<open>size w \<noteq> 0\<close> for w :: \<open>'a::len word\<close> 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

112 
by auto 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

113 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

114 
definition source_size :: "('a::len0 word \<Rightarrow> 'b) \<Rightarrow> nat" 
67443
3abf6a722518
standardized towards newstyle formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset

115 
\<comment> \<open>whether a cast (or other) function is to a longer or shorter length\<close> 
65268  116 
where [code del]: "source_size c = (let arb = undefined; x = c arb in size arb)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

117 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

118 
definition target_size :: "('a \<Rightarrow> 'b::len0 word) \<Rightarrow> nat" 
65268  119 
where [code del]: "target_size c = size (c undefined)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

120 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

121 
definition is_up :: "('a::len0 word \<Rightarrow> 'b::len0 word) \<Rightarrow> bool" 
65268  122 
where "is_up c \<longleftrightarrow> source_size c \<le> target_size c" 
123 

124 
definition is_down :: "('a::len0 word \<Rightarrow> 'b::len0 word) \<Rightarrow> bool" 

125 
where "is_down c \<longleftrightarrow> target_size c \<le> source_size c" 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

126 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

127 
definition of_bl :: "bool list \<Rightarrow> 'a::len0 word" 
65268  128 
where "of_bl bl = word_of_int (bl_to_bin bl)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

129 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

130 
definition to_bl :: "'a::len0 word \<Rightarrow> bool list" 
70185  131 
where "to_bl w = bin_to_bl (LENGTH('a)) (uint w)" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

132 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

133 
definition word_reverse :: "'a::len0 word \<Rightarrow> 'a word" 
65268  134 
where "word_reverse w = of_bl (rev (to_bl w))" 
135 

136 
definition word_int_case :: "(int \<Rightarrow> 'b) \<Rightarrow> 'a::len0 word \<Rightarrow> 'b" 

137 
where "word_int_case f w = f (uint w)" 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

138 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

139 
translations 
65268  140 
"case x of XCONST of_int y \<Rightarrow> b" \<rightleftharpoons> "CONST word_int_case (\<lambda>y. b) x" 
141 
"case x of (XCONST of_int :: 'a) y \<Rightarrow> b" \<rightharpoonup> "CONST word_int_case (\<lambda>y. b) x" 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

142 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

143 

61799  144 
subsection \<open>Basic code generation setup\<close> 
55817
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

145 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

146 
definition Word :: "int \<Rightarrow> 'a::len0 word" 
65268  147 
where [code_post]: "Word = word_of_int" 
148 

149 
lemma [code abstype]: "Word (uint w) = w" 

55817
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

150 
by (simp add: Word_def word_of_int_uint) 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

151 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

152 
declare uint_word_of_int [code abstract] 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

153 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

154 
instantiation word :: (len0) equal 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

155 
begin 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

156 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

157 
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" 
65268  158 
where "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" 
159 

160 
instance 

161 
by standard (simp add: equal equal_word_def word_uint_eq_iff) 

55817
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

162 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

163 
end 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

164 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

165 
notation fcomp (infixl "\<circ>>" 60) 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

166 
notation scomp (infixl "\<circ>\<rightarrow>" 60) 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

167 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

168 
instantiation word :: ("{len0, typerep}") random 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

169 
begin 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

170 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

171 
definition 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

172 
"random_word i = Random.range i \<circ>\<rightarrow> (\<lambda>k. Pair ( 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

173 
let j = word_of_int (int_of_integer (integer_of_natural k)) :: 'a word 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

174 
in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

175 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

176 
instance .. 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

177 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

178 
end 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

179 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

180 
no_notation fcomp (infixl "\<circ>>" 60) 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

181 
no_notation scomp (infixl "\<circ>\<rightarrow>" 60) 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

182 

0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

183 

61799  184 
subsection \<open>Typedefinition locale instantiations\<close> 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

185 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

186 
lemmas uint_0 = uint_nonnegative (* FIXME duplicate *) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

187 
lemmas uint_lt = uint_bounded (* FIXME duplicate *) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

188 
lemmas uint_mod_same = uint_idem (* FIXME duplicate *) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

189 

65268  190 
lemma td_ext_uint: 
70185  191 
"td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len0))) 
192 
(\<lambda>w::int. w mod 2 ^ LENGTH('a))" 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

193 
apply (unfold td_ext_def') 
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

194 
apply transfer 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

195 
apply (simp add: uints_num take_bit_eq_mod) 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

196 
done 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

197 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

198 
interpretation word_uint: 
65268  199 
td_ext 
200 
"uint::'a::len0 word \<Rightarrow> int" 

201 
word_of_int 

70185  202 
"uints (LENGTH('a::len0))" 
203 
"\<lambda>w. w mod 2 ^ LENGTH('a::len0)" 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

204 
by (fact td_ext_uint) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

205 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

206 
lemmas td_uint = word_uint.td_thm 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

207 
lemmas int_word_uint = word_uint.eq_norm 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

208 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

209 
lemma td_ext_ubin: 
70185  210 
"td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len0))) 
211 
(bintrunc (LENGTH('a)))" 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

212 
by (unfold no_bintr_alt1) (fact td_ext_uint) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

213 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

214 
interpretation word_ubin: 
65268  215 
td_ext 
216 
"uint::'a::len0 word \<Rightarrow> int" 

217 
word_of_int 

70185  218 
"uints (LENGTH('a::len0))" 
219 
"bintrunc (LENGTH('a::len0))" 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

220 
by (fact td_ext_ubin) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

221 

e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

222 

61799  223 
subsection \<open>Arithmetic operations\<close> 
37660  224 

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

225 
lift_definition word_succ :: "'a::len0 word \<Rightarrow> 'a word" is "\<lambda>x. x + 1" 
64593
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

226 
by (auto simp add: bintrunc_mod2p intro: mod_add_cong) 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

227 

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

228 
lift_definition word_pred :: "'a::len0 word \<Rightarrow> 'a word" is "\<lambda>x. x  1" 
64593
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

229 
by (auto simp add: bintrunc_mod2p intro: mod_diff_cong) 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

230 

63950
cdc1e59aa513
syntactic type class for operation mod named after mod;
haftmann
parents:
63762
diff
changeset

231 
instantiation word :: (len0) "{neg_numeral, modulo, comm_monoid_mult, comm_ring}" 
37660  232 
begin 
233 

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

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

235 

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

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

237 

67399  238 
lift_definition plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(+)" 
64593
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

239 
by (auto simp add: bintrunc_mod2p intro: mod_add_cong) 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

240 

67399  241 
lift_definition minus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "()" 
64593
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

242 
by (auto simp add: bintrunc_mod2p intro: mod_diff_cong) 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

243 

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

244 
lift_definition uminus_word :: "'a word \<Rightarrow> 'a word" is uminus 
64593
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

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

246 

69064
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents:
68157
diff
changeset

247 
lift_definition times_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(*)" 
64593
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

248 
by (auto simp add: bintrunc_mod2p intro: mod_mult_cong) 
37660  249 

71950  250 
lift_definition divide_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" 
251 
is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b" 

252 
by simp 

253 

254 
lift_definition modulo_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" 

255 
is "\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b" 

256 
by simp 

37660  257 

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

258 
instance 
61169  259 
by standard (transfer, simp add: algebra_simps)+ 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

260 

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

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

262 

71950  263 
lemma word_div_def [code]: 
264 
"a div b = word_of_int (uint a div uint b)" 

265 
by transfer rule 

266 

267 
lemma word_mod_def [code]: 

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

269 
by transfer rule 

270 

70901  271 
quickcheck_generator word 
272 
constructors: 

273 
"zero_class.zero :: ('a::len) word", 

274 
"numeral :: num \<Rightarrow> ('a::len) word", 

275 
"uminus :: ('a::len) word \<Rightarrow> ('a::len) word" 

276 

71950  277 
context 
278 
includes lifting_syntax 

279 
notes power_transfer [transfer_rule] 

280 
begin 

281 

282 
lemma power_transfer_word [transfer_rule]: 

283 
\<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close> 

284 
by transfer_prover 

285 

286 
end 

287 

288 

71951  289 

61799  290 
text \<open>Legacy theorems:\<close> 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

291 

65268  292 
lemma word_arith_wis [code]: 
293 
shows word_add_def: "a + b = word_of_int (uint a + uint b)" 

294 
and word_sub_wi: "a  b = word_of_int (uint a  uint b)" 

295 
and word_mult_def: "a * b = word_of_int (uint a * uint b)" 

296 
and word_minus_def: " a = word_of_int ( uint a)" 

297 
and word_succ_alt: "word_succ a = word_of_int (uint a + 1)" 

298 
and word_pred_alt: "word_pred a = word_of_int (uint a  1)" 

299 
and word_0_wi: "0 = word_of_int 0" 

300 
and word_1_wi: "1 = word_of_int 1" 

71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

301 
apply (simp_all flip: plus_word.abs_eq minus_word.abs_eq 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

302 
times_word.abs_eq uminus_word.abs_eq 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

303 
zero_word.abs_eq one_word.abs_eq) 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

304 
apply transfer 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

305 
apply simp 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

306 
apply transfer 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

307 
apply simp 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

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

309 

65268  310 
lemma wi_homs: 
311 
shows wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" 

312 
and wi_hom_sub: "word_of_int a  word_of_int b = word_of_int (a  b)" 

313 
and wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" 

314 
and wi_hom_neg: " word_of_int a = word_of_int ( a)" 

315 
and wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" 

316 
and 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

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

318 

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

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

320 

46013  321 
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi 
46009  322 

323 
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

324 

71950  325 
instance word :: (len0) comm_monoid_add .. 
326 

327 
instance word :: (len0) semiring_numeral .. 

328 

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

329 
instance word :: (len) comm_ring_1 
45810  330 
proof 
70185  331 
have *: "0 < LENGTH('a)" by (rule len_gt_0) 
65268  332 
show "(0::'a word) \<noteq> 1" 
333 
by transfer (use * in \<open>auto simp add: gr0_conv_Suc\<close>) 

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

335 

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

336 
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

337 
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

338 

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

339 
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

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

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

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

344 

71950  345 
context 
346 
includes lifting_syntax 

347 
notes 

348 
transfer_rule_of_bool [transfer_rule] 

349 
transfer_rule_numeral [transfer_rule] 

350 
transfer_rule_of_nat [transfer_rule] 

351 
transfer_rule_of_int [transfer_rule] 

352 
begin 

353 

354 
lemma [transfer_rule]: 

355 
"((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) of_bool of_bool" 

356 
by transfer_prover 

357 

358 
lemma [transfer_rule]: 

359 
"((=) ===> (pcr_word :: int \<Rightarrow> 'a::len0 word \<Rightarrow> bool)) numeral numeral" 

360 
by transfer_prover 

361 

362 
lemma [transfer_rule]: 

363 
"((=) ===> pcr_word) int of_nat" 

364 
by transfer_prover 

365 

366 
lemma [transfer_rule]: 

367 
"((=) ===> pcr_word) (\<lambda>k. k) of_int" 

368 
proof  

369 
have "((=) ===> pcr_word) of_int of_int" 

370 
by transfer_prover 

371 
then show ?thesis by (simp add: id_def) 

372 
qed 

373 

374 
end 

375 

376 
lemma word_of_int_eq: 

377 
"word_of_int = of_int" 

378 
by (rule ext) (transfer, rule) 

379 

65268  380 
definition udvd :: "'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool" (infixl "udvd" 50) 
381 
where "a udvd b = (\<exists>n\<ge>0. uint b = n * uint a)" 

37660  382 

71950  383 
context 
384 
includes lifting_syntax 

385 
begin 

386 

387 
lemma [transfer_rule]: 

388 
"(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)" 

389 
proof  

390 
have even_word_unfold: "even k \<longleftrightarrow> (\<exists>l. take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l))" (is "?P \<longleftrightarrow> ?Q") 

391 
for k :: int 

392 
proof 

393 
assume ?P 

394 
then show ?Q 

395 
by auto 

396 
next 

397 
assume ?Q 

398 
then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" .. 

399 
then have "even (take_bit LENGTH('a) k)" 

400 
by simp 

401 
then show ?P 

402 
by simp 

403 
qed 

404 
show ?thesis by (simp only: even_word_unfold [abs_def] dvd_def [where ?'a = "'a word", abs_def]) 

405 
transfer_prover 

406 
qed 

407 

408 
end 

409 

71951  410 
instance word :: (len) semiring_modulo 
411 
proof 

412 
show "a div b * b + a mod b = a" for a b :: "'a word" 

413 
proof transfer 

414 
fix k l :: int 

415 
define r :: int where "r = 2 ^ LENGTH('a)" 

416 
then have r: "take_bit LENGTH('a) k = k mod r" for k 

417 
by (simp add: take_bit_eq_mod) 

418 
have "k mod r = ((k mod r) div (l mod r) * (l mod r) 

419 
+ (k mod r) mod (l mod r)) mod r" 

420 
by (simp add: div_mult_mod_eq) 

421 
also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r 

422 
+ (k mod r) mod (l mod r)) mod r" 

423 
by (simp add: mod_add_left_eq) 

424 
also have "... = (((k mod r) div (l mod r) * l) mod r 

425 
+ (k mod r) mod (l mod r)) mod r" 

426 
by (simp add: mod_mult_right_eq) 

427 
finally have "k mod r = ((k mod r) div (l mod r) * l 

428 
+ (k mod r) mod (l mod r)) mod r" 

429 
by (simp add: mod_simps) 

430 
with r show "take_bit LENGTH('a) (take_bit LENGTH('a) k div take_bit LENGTH('a) l * l 

431 
+ take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k" 

432 
by simp 

433 
qed 

434 
qed 

435 

436 
instance word :: (len) semiring_parity 

437 
proof 

438 
show "\<not> 2 dvd (1::'a word)" 

439 
by transfer simp 

440 
show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0" 

441 
for a :: "'a word" 

442 
by transfer (simp_all add: mod_2_eq_odd take_bit_Suc) 

443 
show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1" 

444 
for a :: "'a word" 

445 
by transfer (simp_all add: mod_2_eq_odd take_bit_Suc) 

446 
qed 

447 

45547  448 

61799  449 
subsection \<open>Ordering\<close> 
45547  450 

451 
instantiation word :: (len0) linorder 

452 
begin 

453 

71950  454 
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" 
455 
is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b" 

456 
by simp 

457 

458 
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" 

459 
is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b" 

460 
by simp 

37660  461 

45547  462 
instance 
71950  463 
by (standard; transfer) auto 
45547  464 

465 
end 

466 

71950  467 
lemma word_le_def [code]: 
468 
"a \<le> b \<longleftrightarrow> uint a \<le> uint b" 

469 
by transfer rule 

470 

471 
lemma word_less_def [code]: 

472 
"a < b \<longleftrightarrow> uint a < uint b" 

473 
by transfer rule 

474 

71951  475 
lemma word_greater_zero_iff: 
476 
\<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len0 word\<close> 

477 
by transfer (simp add: less_le) 

478 

479 
lemma of_nat_word_eq_iff: 

480 
\<open>of_nat m = (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close> 

481 
by transfer (simp add: take_bit_of_nat) 

482 

483 
lemma of_nat_word_less_eq_iff: 

484 
\<open>of_nat m \<le> (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close> 

485 
by transfer (simp add: take_bit_of_nat) 

486 

487 
lemma of_nat_word_less_iff: 

488 
\<open>of_nat m < (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close> 

489 
by transfer (simp add: take_bit_of_nat) 

490 

491 
lemma of_nat_word_eq_0_iff: 

492 
\<open>of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close> 

493 
using of_nat_word_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff) 

494 

495 
lemma of_int_word_eq_iff: 

496 
\<open>of_int k = (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> 

497 
by transfer rule 

498 

499 
lemma of_int_word_less_eq_iff: 

500 
\<open>of_int k \<le> (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close> 

501 
by transfer rule 

502 

503 
lemma of_int_word_less_iff: 

504 
\<open>of_int k < (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close> 

505 
by transfer rule 

506 

507 
lemma of_int_word_eq_0_iff: 

508 
\<open>of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close> 

509 
using of_int_word_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff) 

510 

65268  511 
definition word_sle :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> bool" ("(_/ <=s _)" [50, 51] 50) 
512 
where "a <=s b \<longleftrightarrow> sint a \<le> sint b" 

513 

514 
definition word_sless :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> bool" ("(_/ <s _)" [50, 51] 50) 

515 
where "x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y" 

37660  516 

517 

61799  518 
subsection \<open>Bitwise operations\<close> 
37660  519 

71951  520 
lemma word_bit_induct [case_names zero even odd]: 
521 
\<open>P a\<close> if word_zero: \<open>P 0\<close> 

522 
and word_even: \<open>\<And>a. P a \<Longrightarrow> 0 < a \<Longrightarrow> a < 2 ^ (LENGTH('a)  1) \<Longrightarrow> P (2 * a)\<close> 

523 
and word_odd: \<open>\<And>a. P a \<Longrightarrow> a < 2 ^ (LENGTH('a)  1) \<Longrightarrow> P (1 + 2 * a)\<close> 

524 
for P and a :: \<open>'a::len word\<close> 

525 
proof  

526 
define m :: nat where \<open>m = LENGTH('a)  1\<close> 

527 
then have l: \<open>LENGTH('a) = Suc m\<close> 

528 
by simp 

529 
define n :: nat where \<open>n = unat a\<close> 

530 
then have \<open>n < 2 ^ LENGTH('a)\<close> 

531 
by (unfold unat_def) (transfer, simp add: take_bit_eq_mod) 

532 
then have \<open>n < 2 * 2 ^ m\<close> 

533 
by (simp add: l) 

534 
then have \<open>P (of_nat n)\<close> 

535 
proof (induction n rule: nat_bit_induct) 

536 
case zero 

537 
show ?case 

538 
by simp (rule word_zero) 

539 
next 

540 
case (even n) 

541 
then have \<open>n < 2 ^ m\<close> 

542 
by simp 

543 
with even.IH have \<open>P (of_nat n)\<close> 

544 
by simp 

545 
moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close> 

546 
by (auto simp add: word_greater_zero_iff of_nat_word_eq_0_iff l) 

547 
moreover from \<open>n < 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a)  1)\<close> 

548 
using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>] 

549 
by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l) 

550 
ultimately have \<open>P (2 * of_nat n)\<close> 

551 
by (rule word_even) 

552 
then show ?case 

553 
by simp 

554 
next 

555 
case (odd n) 

556 
then have \<open>Suc n \<le> 2 ^ m\<close> 

557 
by simp 

558 
with odd.IH have \<open>P (of_nat n)\<close> 

559 
by simp 

560 
moreover from \<open>Suc n \<le> 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a)  1)\<close> 

561 
using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>] 

562 
by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l) 

563 
ultimately have \<open>P (1 + 2 * of_nat n)\<close> 

564 
by (rule word_odd) 

565 
then show ?case 

566 
by simp 

567 
qed 

568 
moreover have \<open>of_nat (nat (uint a)) = a\<close> 

569 
by transfer simp 

570 
ultimately show ?thesis 

571 
by (simp add: n_def unat_def) 

572 
qed 

573 

574 
lemma bit_word_half_eq: 

575 
\<open>(of_bool b + a * 2) div 2 = a\<close> 

576 
if \<open>a < 2 ^ (LENGTH('a)  Suc 0)\<close> 

577 
for a :: \<open>'a::len word\<close> 

578 
proof (cases \<open>2 \<le> LENGTH('a::len)\<close>) 

579 
case False 

580 
have \<open>of_bool (odd k) < (1 :: int) \<longleftrightarrow> even k\<close> for k :: int 

581 
by auto 

582 
with False that show ?thesis 

583 
by transfer (simp add: eq_iff) 

584 
next 

585 
case True 

586 
obtain n where length: \<open>LENGTH('a) = Suc n\<close> 

587 
by (cases \<open>LENGTH('a)\<close>) simp_all 

588 
show ?thesis proof (cases b) 

589 
case False 

590 
moreover have \<open>a * 2 div 2 = a\<close> 

591 
using that proof transfer 

592 
fix k :: int 

593 
from length have \<open>k * 2 mod 2 ^ LENGTH('a) = (k mod 2 ^ n) * 2\<close> 

594 
by simp 

595 
moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a)  Suc 0))\<close> 

596 
with \<open>LENGTH('a) = Suc n\<close> 

597 
have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close> 

598 
by (simp add: take_bit_eq_mod divmod_digit_0) 

599 
ultimately have \<open>take_bit LENGTH('a) (k * 2) = take_bit LENGTH('a) k * 2\<close> 

600 
by (simp add: take_bit_eq_mod) 

601 
with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (k * 2) div take_bit LENGTH('a) 2) 

602 
= take_bit LENGTH('a) k\<close> 

603 
by simp 

604 
qed 

605 
ultimately show ?thesis 

606 
by simp 

607 
next 

608 
case True 

609 
moreover have \<open>(1 + a * 2) div 2 = a\<close> 

610 
using that proof transfer 

611 
fix k :: int 

612 
from length have \<open>(1 + k * 2) mod 2 ^ LENGTH('a) = 1 + (k mod 2 ^ n) * 2\<close> 

613 
using pos_zmod_mult_2 [of \<open>2 ^ n\<close> k] by (simp add: ac_simps) 

614 
moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a)  Suc 0))\<close> 

615 
with \<open>LENGTH('a) = Suc n\<close> 

616 
have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close> 

617 
by (simp add: take_bit_eq_mod divmod_digit_0) 

618 
ultimately have \<open>take_bit LENGTH('a) (1 + k * 2) = 1 + take_bit LENGTH('a) k * 2\<close> 

619 
by (simp add: take_bit_eq_mod) 

620 
with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (1 + k * 2) div take_bit LENGTH('a) 2) 

621 
= take_bit LENGTH('a) k\<close> 

622 
by (auto simp add: take_bit_Suc) 

623 
qed 

624 
ultimately show ?thesis 

625 
by simp 

626 
qed 

627 
qed 

628 

629 
lemma even_mult_exp_div_word_iff: 

630 
\<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> ( 

631 
m \<le> n \<and> 

632 
n < LENGTH('a) \<and> odd (a div 2 ^ (n  m)))\<close> for a :: \<open>'a::len word\<close> 

633 
by transfer 

634 
(auto simp flip: drop_bit_eq_div simp add: even_drop_bit_iff_not_bit bit_take_bit_iff, 

635 
simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int) 

636 

637 
instance word :: (len) semiring_bits 

638 
proof 

639 
show \<open>P a\<close> if stable: \<open>\<And>a. a div 2 = a \<Longrightarrow> P a\<close> 

640 
and rec: \<open>\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)\<close> 

641 
for P and a :: \<open>'a word\<close> 

642 
proof (induction a rule: word_bit_induct) 

643 
case zero 

644 
have \<open>0 div 2 = (0::'a word)\<close> 

645 
by transfer simp 

646 
with stable [of 0] show ?case 

647 
by simp 

648 
next 

649 
case (even a) 

650 
with rec [of a False] show ?case 

651 
using bit_word_half_eq [of a False] by (simp add: ac_simps) 

652 
next 

653 
case (odd a) 

654 
with rec [of a True] show ?case 

655 
using bit_word_half_eq [of a True] by (simp add: ac_simps) 

656 
qed 

657 
show \<open>0 div a = 0\<close> 

658 
for a :: \<open>'a word\<close> 

659 
by transfer simp 

660 
show \<open>a div 1 = a\<close> 

661 
for a :: \<open>'a word\<close> 

662 
by transfer simp 

663 
show \<open>a mod b div b = 0\<close> 

664 
for a b :: \<open>'a word\<close> 

665 
apply transfer 

666 
apply (simp add: take_bit_eq_mod) 

667 
apply (subst (3) mod_pos_pos_trivial [of _ \<open>2 ^ LENGTH('a)\<close>]) 

668 
apply simp_all 

669 
apply (metis le_less mod_by_0 pos_mod_conj zero_less_numeral zero_less_power) 

670 
using pos_mod_bound [of \<open>2 ^ LENGTH('a)\<close>] apply simp 

671 
proof  

672 
fix aa :: int and ba :: int 

673 
have f1: "\<And>i n. (i::int) mod 2 ^ n = 0 \<or> 0 < i mod 2 ^ n" 

674 
by (metis le_less take_bit_eq_mod take_bit_nonnegative) 

675 
have "(0::int) < 2 ^ len_of (TYPE('a)::'a itself) \<and> ba mod 2 ^ len_of (TYPE('a)::'a itself) \<noteq> 0 \<or> aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)" 

676 
by (metis (no_types) mod_by_0 unique_euclidean_semiring_numeral_class.pos_mod_bound zero_less_numeral zero_less_power) 

677 
then show "aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)" 

678 
using f1 by (meson le_less less_le_trans unique_euclidean_semiring_numeral_class.pos_mod_bound) 

679 
qed 

680 
show \<open>(1 + a) div 2 = a div 2\<close> 

681 
if \<open>even a\<close> 

682 
for a :: \<open>'a word\<close> 

683 
using that by transfer (auto dest: le_Suc_ex simp add: take_bit_Suc) 

684 
show \<open>(2 :: 'a word) ^ m div 2 ^ n = of_bool ((2 :: 'a word) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m  n)\<close> 

685 
for m n :: nat 

686 
by transfer (simp, simp add: exp_div_exp_eq) 

687 
show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)" 

688 
for a :: "'a word" and m n :: nat 

689 
apply transfer 

690 
apply (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: drop_bit_eq_div) 

691 
apply (simp add: drop_bit_take_bit) 

692 
done 

693 
show "a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n" 

694 
for a :: "'a word" and m n :: nat 

695 
by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps) 

696 
show \<open>a * 2 ^ m mod 2 ^ n = a mod 2 ^ (n  m) * 2 ^ m\<close> 

697 
if \<open>m \<le> n\<close> for a :: "'a word" and m n :: nat 

698 
using that apply transfer 

699 
apply (auto simp flip: take_bit_eq_mod) 

700 
apply (auto simp flip: push_bit_eq_mult simp add: push_bit_take_bit split: split_min_lin) 

701 
done 

702 
show \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close> 

703 
for a :: "'a word" and m n :: nat 

704 
by transfer (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: take_bit_eq_mod drop_bit_eq_div split: split_min_lin) 

705 
show \<open>even ((2 ^ m  1) div (2::'a word) ^ n) \<longleftrightarrow> 2 ^ n = (0::'a word) \<or> m \<le> n\<close> 

706 
for m n :: nat 

707 
by transfer (auto simp add: take_bit_of_mask even_mask_div_iff) 

708 
show \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::'a word) ^ n = 0 \<or> m \<le> n \<and> even (a div 2 ^ (n  m))\<close> 

709 
for a :: \<open>'a word\<close> and m n :: nat 

710 
proof transfer 

711 
show \<open>even (take_bit LENGTH('a) (k * 2 ^ m) div take_bit LENGTH('a) (2 ^ n)) \<longleftrightarrow> 

712 
n < m 

713 
\<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0 

714 
\<or> (m \<le> n \<and> even (take_bit LENGTH('a) k div take_bit LENGTH('a) (2 ^ (n  m))))\<close> 

715 
for m n :: nat and k l :: int 

716 
by (auto simp flip: take_bit_eq_mod drop_bit_eq_div push_bit_eq_mult 

717 
simp add: div_push_bit_of_1_eq_drop_bit drop_bit_take_bit drop_bit_push_bit_int [of n m]) 

718 
qed 

719 
qed 

720 

721 
context 

722 
includes lifting_syntax 

723 
begin 

724 

725 
lemma transfer_rule_bit_word [transfer_rule]: 

726 
\<open>((pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool) ===> (=)) (\<lambda>k n. n < LENGTH('a) \<and> bit k n) bit\<close> 

727 
proof  

728 
let ?t = \<open>\<lambda>a n. odd (take_bit LENGTH('a) a div take_bit LENGTH('a) ((2::int) ^ n))\<close> 

729 
have \<open>((pcr_word :: int \<Rightarrow> 'a word \<Rightarrow> bool) ===> (=)) ?t bit\<close> 

730 
by (unfold bit_def) transfer_prover 

731 
also have \<open>?t = (\<lambda>k n. n < LENGTH('a) \<and> bit k n)\<close> 

732 
by (simp add: fun_eq_iff bit_take_bit_iff flip: bit_def) 

733 
finally show ?thesis . 

734 
qed 

735 

736 
end 

737 

71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

738 
instantiation word :: (len) semiring_bit_shifts 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

739 
begin 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

740 

2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

741 
lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

742 
is push_bit 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

743 
proof  
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

744 
show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

745 
if \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> for k l :: int and n :: nat 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

746 
proof  
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

747 
from that 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

748 
have \<open>take_bit (LENGTH('a)  n) (take_bit LENGTH('a) k) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

749 
= take_bit (LENGTH('a)  n) (take_bit LENGTH('a) l)\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

750 
by simp 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

751 
moreover have \<open>min (LENGTH('a)  n) LENGTH('a) = LENGTH('a)  n\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

752 
by simp 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

753 
ultimately show ?thesis 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

754 
by (simp add: take_bit_push_bit) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

755 
qed 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

756 
qed 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

757 

2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

758 
lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

759 
is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

760 
by (simp add: take_bit_eq_mod) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

761 

2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

762 
instance proof 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

763 
show \<open>push_bit n a = a * 2 ^ n\<close> for n :: nat and a :: "'a word" 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

764 
by transfer (simp add: push_bit_eq_mult) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

765 
show \<open>drop_bit n a = a div 2 ^ n\<close> for n :: nat and a :: "'a word" 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

766 
by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

767 
qed 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

768 

2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

769 
end 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

770 

70191  771 
definition shiftl1 :: "'a::len0 word \<Rightarrow> 'a word" 
772 
where "shiftl1 w = word_of_int (uint w BIT False)" 

773 

71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

774 
lemma shiftl1_eq_mult_2: 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

775 
\<open>shiftl1 = (*) 2\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

776 
apply (simp add: fun_eq_iff shiftl1_def Bit_def) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

777 
apply (simp only: mult_2) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

778 
apply transfer 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

779 
apply (simp only: take_bit_add) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

780 
apply simp 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

781 
done 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

782 

70191  783 
definition shiftr1 :: "'a::len0 word \<Rightarrow> 'a word" 
784 
\<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close> 

785 
where "shiftr1 w = word_of_int (bin_rest (uint w))" 

786 

71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

787 
lemma shiftr1_eq_div_2: 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

788 
\<open>shiftr1 w = w div 2\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

789 
apply (simp add: fun_eq_iff shiftr1_def) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

790 
apply transfer 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

791 
apply (auto simp add: not_le dest: less_2_cases) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

792 
done 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

793 

70191  794 
instantiation word :: (len0) bit_operations 
37660  795 
begin 
796 

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

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

799 

71826  800 
lift_definition bitAND_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is \<open>(AND)\<close> 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

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

802 

71826  803 
lift_definition bitOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is \<open>(OR)\<close> 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

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

805 

71826  806 
lift_definition bitXOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is \<open>(XOR)\<close> 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

807 
by (metis bin_trunc_xor) 
37660  808 

65268  809 
definition word_test_bit_def: "test_bit a = bin_nth (uint a)" 
810 

811 
definition word_set_bit_def: "set_bit a n x = word_of_int (bin_sc n x (uint a))" 

812 

813 
definition word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a)" 

37660  814 

70175  815 
definition "msb a \<longleftrightarrow> bin_sign (sbintrunc (LENGTH('a)  1) (uint a)) =  1" 
816 

65268  817 
definition shiftl_def: "w << n = (shiftl1 ^^ n) w" 
818 

819 
definition shiftr_def: "w >> n = (shiftr1 ^^ n) w" 

37660  820 

821 
instance .. 

822 

823 
end 

824 

71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

825 
lemma test_bit_word_eq: 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

826 
\<open>test_bit w = bit w\<close> for w :: \<open>'a::len word\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

827 
apply (simp add: word_test_bit_def fun_eq_iff) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

828 
apply transfer 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

829 
apply (simp add: bit_take_bit_iff) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

830 
done 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

831 

2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

832 
lemma lsb_word_eq: 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

833 
\<open>lsb = (odd :: 'a word \<Rightarrow> bool)\<close> for w :: \<open>'a::len word\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

834 
apply (simp add: word_lsb_def fun_eq_iff) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

835 
apply transfer 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

836 
apply simp 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

837 
done 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

838 

2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

839 
lemma msb_word_eq: 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

840 
\<open>msb w \<longleftrightarrow> bit w (LENGTH('a)  1)\<close> for w :: \<open>'a::len word\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

841 
apply (simp add: msb_word_def bin_sign_lem) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

842 
apply transfer 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

843 
apply (simp add: bit_take_bit_iff) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

844 
done 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

845 

2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

846 
lemma shiftl_word_eq: 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

847 
\<open>w << n = push_bit n w\<close> for w :: \<open>'a::len word\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

848 
by (induction n) (simp_all add: shiftl_def shiftl1_eq_mult_2 push_bit_double) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

849 

2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

850 
lemma shiftr_word_eq: 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

851 
\<open>w >> n = drop_bit n w\<close> for w :: \<open>'a::len word\<close> 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

852 
by (induction n) (simp_all add: shiftr_def shiftr1_eq_div_2 drop_bit_Suc drop_bit_half) 
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset

853 

70175  854 
lemma word_msb_def: 
855 
"msb a \<longleftrightarrow> bin_sign (sint a) =  1" 

856 
by (simp add: msb_word_def sint_uint) 

857 

65268  858 
lemma [code]: 
859 
shows word_not_def: "NOT (a::'a::len0 word) = word_of_int (NOT (uint a))" 

860 
and word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)" 

861 
and word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)" 

862 
and word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)" 

71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

863 
by (simp_all flip: bitNOT_word.abs_eq bitAND_word.abs_eq 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

864 
bitOR_word.abs_eq bitXOR_word.abs_eq) 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

865 

65268  866 
definition setBit :: "'a::len0 word \<Rightarrow> nat \<Rightarrow> 'a word" 
867 
where "setBit w n = set_bit w n True" 

868 

869 
definition clearBit :: "'a::len0 word \<Rightarrow> nat \<Rightarrow> 'a word" 

870 
where "clearBit w n = set_bit w n False" 

37660  871 

872 

61799  873 
subsection \<open>Shift operations\<close> 
37660  874 

65268  875 
definition sshiftr1 :: "'a::len word \<Rightarrow> 'a word" 
876 
where "sshiftr1 w = word_of_int (bin_rest (sint w))" 

877 

878 
definition bshiftr1 :: "bool \<Rightarrow> 'a::len word \<Rightarrow> 'a word" 

879 
where "bshiftr1 b w = of_bl (b # butlast (to_bl w))" 

880 

881 
definition sshiftr :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word" (infixl ">>>" 55) 

882 
where "w >>> n = (sshiftr1 ^^ n) w" 

883 

884 
definition mask :: "nat \<Rightarrow> 'a::len word" 

885 
where "mask n = (1 << n)  1" 

886 

887 
definition revcast :: "'a::len0 word \<Rightarrow> 'b::len0 word" 

70185  888 
where "revcast w = of_bl (takefill False (LENGTH('b)) (to_bl w))" 
65268  889 

890 
definition slice1 :: "nat \<Rightarrow> 'a::len0 word \<Rightarrow> 'b::len0 word" 

891 
where "slice1 n w = of_bl (takefill False n (to_bl w))" 

892 

893 
definition slice :: "nat \<Rightarrow> 'a::len0 word \<Rightarrow> 'b::len0 word" 

894 
where "slice n w = slice1 (size w  n) w" 

37660  895 

896 

61799  897 
subsection \<open>Rotation\<close> 
37660  898 

65268  899 
definition rotater1 :: "'a list \<Rightarrow> 'a list" 
900 
where "rotater1 ys = 

901 
(case ys of [] \<Rightarrow> []  x # xs \<Rightarrow> last ys # butlast ys)" 

902 

903 
definition rotater :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" 

904 
where "rotater n = rotater1 ^^ n" 

905 

906 
definition word_rotr :: "nat \<Rightarrow> 'a::len0 word \<Rightarrow> 'a::len0 word" 

907 
where "word_rotr n w = of_bl (rotater n (to_bl w))" 

908 

909 
definition word_rotl :: "nat \<Rightarrow> 'a::len0 word \<Rightarrow> 'a::len0 word" 

910 
where "word_rotl n w = of_bl (rotate n (to_bl w))" 

911 

912 
definition word_roti :: "int \<Rightarrow> 'a::len0 word \<Rightarrow> 'a::len0 word" 

913 
where "word_roti i w = 

914 
(if i \<ge> 0 then word_rotr (nat i) w else word_rotl (nat ( i)) w)" 

37660  915 

916 

61799  917 
subsection \<open>Split and cat operations\<close> 
37660  918 

65268  919 
definition word_cat :: "'a::len0 word \<Rightarrow> 'b::len0 word \<Rightarrow> 'c::len0 word" 
70185  920 
where "word_cat a b = word_of_int (bin_cat (uint a) (LENGTH('b)) (uint b))" 
65268  921 

922 
definition word_split :: "'a::len0 word \<Rightarrow> 'b::len0 word \<times> 'c::len0 word" 

923 
where "word_split a = 

70185  924 
(case bin_split (LENGTH('c)) (uint a) of 
65268  925 
(u, v) \<Rightarrow> (word_of_int u, word_of_int v))" 
926 

927 
definition word_rcat :: "'a::len0 word list \<Rightarrow> 'b::len0 word" 

70185  928 
where "word_rcat ws = word_of_int (bin_rcat (LENGTH('a)) (map uint ws))" 
65268  929 

930 
definition word_rsplit :: "'a::len0 word \<Rightarrow> 'b::len word list" 

70185  931 
where "word_rsplit w = map word_of_int (bin_rsplit (LENGTH('b)) (LENGTH('a), uint w))" 
65268  932 

71946  933 
abbreviation (input) max_word :: \<open>'a::len0 word\<close> 
67443
3abf6a722518
standardized towards newstyle formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset

934 
\<comment> \<open>Largest representable machine integer.\<close> 
71946  935 
where "max_word \<equiv>  1" 
37660  936 

937 

61799  938 
subsection \<open>Theorems about typedefs\<close> 
46010  939 

70185  940 
lemma sint_sbintrunc': "sint (word_of_int bin :: 'a word) = sbintrunc (LENGTH('a::len)  1) bin" 
65268  941 
by (auto simp: sint_uint word_ubin.eq_norm sbintrunc_bintrunc_lt) 
942 

70185  943 
lemma uint_sint: "uint w = bintrunc (LENGTH('a)) (sint w)" 
65328  944 
for w :: "'a::len word" 
65268  945 
by (auto simp: sint_uint bintrunc_sbintrunc_le) 
946 

70185  947 
lemma bintr_uint: "LENGTH('a) \<le> n \<Longrightarrow> bintrunc n (uint w) = uint w" 
65268  948 
for w :: "'a::len0 word" 
949 
apply (subst word_ubin.norm_Rep [symmetric]) 

37660  950 
apply (simp only: bintrunc_bintrunc_min word_size) 
54863
82acc20ded73
prefer more canonical names for lemmas on min/max
haftmann
parents:
54854
diff
changeset

951 
apply (simp add: min.absorb2) 
37660  952 
done 
953 

46057  954 
lemma wi_bintr: 
70185  955 
"LENGTH('a::len0) \<le> n \<Longrightarrow> 
46057  956 
word_of_int (bintrunc n w) = (word_of_int w :: 'a word)" 
65268  957 
by (auto simp: word_ubin.norm_eq_iff [symmetric] min.absorb1) 
958 

959 
lemma td_ext_sbin: 

70185  960 
"td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len))) 
961 
(sbintrunc (LENGTH('a)  1))" 

37660  962 
apply (unfold td_ext_def' sint_uint) 
963 
apply (simp add : word_ubin.eq_norm) 

70185  964 
apply (cases "LENGTH('a)") 
37660  965 
apply (auto simp add : sints_def) 
966 
apply (rule sym [THEN trans]) 

65268  967 
apply (rule word_ubin.Abs_norm) 
37660  968 
apply (simp only: bintrunc_sbintrunc) 
969 
apply (drule sym) 

970 
apply simp 

971 
done 

972 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

973 
lemma td_ext_sint: 
70185  974 
"td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len))) 
975 
(\<lambda>w. (w + 2 ^ (LENGTH('a)  1)) mod 2 ^ LENGTH('a)  

976 
2 ^ (LENGTH('a)  1))" 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

977 
using td_ext_sbin [where ?'a = 'a] by (simp add: no_sbintr_alt2) 
37660  978 

67408  979 
text \<open> 
980 
We do \<open>sint\<close> before \<open>sbin\<close>, before \<open>sint\<close> is the user version 

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

982 
we get the name \<open>word_sint.Rep_eqD\<close>, but not \<open>word_sbin.Req_eqD\<close>, 

983 
because the latter is the same thm as the former. 

984 
\<close> 

37660  985 
interpretation word_sint: 
65268  986 
td_ext 
987 
"sint ::'a::len word \<Rightarrow> int" 

988 
word_of_int 

70185  989 
"sints (LENGTH('a::len))" 
990 
"\<lambda>w. (w + 2^(LENGTH('a::len)  1)) mod 2^LENGTH('a::len)  

991 
2 ^ (LENGTH('a::len)  1)" 

37660  992 
by (rule td_ext_sint) 
993 

994 
interpretation word_sbin: 

65268  995 
td_ext 
996 
"sint ::'a::len word \<Rightarrow> int" 

997 
word_of_int 

70185  998 
"sints (LENGTH('a::len))" 
999 
"sbintrunc (LENGTH('a::len)  1)" 

37660  1000 
by (rule td_ext_sbin) 
1001 

45604  1002 
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] 
37660  1003 

1004 
lemmas td_sint = word_sint.td 

1005 

70185  1006 
lemma to_bl_def': "(to_bl :: 'a::len0 word \<Rightarrow> bool list) = bin_to_bl (LENGTH('a)) \<circ> uint" 
44762  1007 
by (auto simp: to_bl_def) 
37660  1008 

65268  1009 
lemmas word_reverse_no_def [simp] = 
1010 
word_reverse_def [of "numeral w"] for w 

37660  1011 

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

1014 

65268  1015 
lemma word_numeral_alt: "numeral b = word_of_int (numeral b)" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1016 
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

1017 

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

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

1019 

65268  1020 
lemma word_neg_numeral_alt: " numeral b = word_of_int ( numeral b)" 
54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

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

1022 

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

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

1024 

45805  1025 
lemma uint_bintrunc [simp]: 
65268  1026 
"uint (numeral bin :: 'a word) = 
70185  1027 
bintrunc (LENGTH('a::len0)) (numeral bin)" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

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

1029 

65268  1030 
lemma uint_bintrunc_neg [simp]: 
70185  1031 
"uint ( numeral bin :: 'a word) = bintrunc (LENGTH('a::len0)) ( numeral bin)" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1032 
by (simp only: word_neg_numeral_alt word_ubin.eq_norm) 
37660  1033 

45805  1034 
lemma sint_sbintrunc [simp]: 
70185  1035 
"sint (numeral bin :: 'a word) = sbintrunc (LENGTH('a::len)  1) (numeral bin)" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

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

1037 

65268  1038 
lemma sint_sbintrunc_neg [simp]: 
70185  1039 
"sint ( numeral bin :: 'a word) = sbintrunc (LENGTH('a::len)  1) ( numeral bin)" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1040 
by (simp only: word_neg_numeral_alt word_sbin.eq_norm) 
37660  1041 

45805  1042 
lemma unat_bintrunc [simp]: 
70185  1043 
"unat (numeral bin :: 'a::len0 word) = nat (bintrunc (LENGTH('a)) (numeral bin))" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

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

1045 

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

1046 
lemma unat_bintrunc_neg [simp]: 
70185  1047 
"unat ( numeral bin :: 'a::len0 word) = nat (bintrunc (LENGTH('a)) ( numeral bin))" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1048 
by (simp only: unat_def uint_bintrunc_neg) 
37660  1049 

65328  1050 
lemma size_0_eq: "size w = 0 \<Longrightarrow> v = w" 
1051 
for v w :: "'a::len0 word" 

37660  1052 
apply (unfold word_size) 
1053 
apply (rule word_uint.Rep_eqD) 

1054 
apply (rule box_equals) 

1055 
defer 

1056 
apply (rule word_ubin.norm_Rep)+ 

1057 
apply simp 

1058 
done 

1059 

65268  1060 
lemma uint_ge_0 [iff]: "0 \<le> uint x" 
1061 
for x :: "'a::len0 word" 

45805  1062 
using word_uint.Rep [of x] by (simp add: uints_num) 
1063 

70185  1064 
lemma uint_lt2p [iff]: "uint x < 2 ^ LENGTH('a)" 
65268  1065 
for x :: "'a::len0 word" 
45805  1066 
using word_uint.Rep [of x] by (simp add: uints_num) 
1067 

71946  1068 
lemma word_exp_length_eq_0 [simp]: 
1069 
\<open>(2 :: 'a::len0 word) ^ LENGTH('a) = 0\<close> 

1070 
by transfer (simp add: bintrunc_mod2p) 

1071 

70185  1072 
lemma sint_ge: " (2 ^ (LENGTH('a)  1)) \<le> sint x" 
65268  1073 
for x :: "'a::len word" 
45805  1074 
using word_sint.Rep [of x] by (simp add: sints_num) 
1075 

70185  1076 
lemma sint_lt: "sint x < 2 ^ (LENGTH('a)  1)" 
65268  1077 
for x :: "'a::len word" 
45805  1078 
using word_sint.Rep [of x] by (simp add: sints_num) 
37660  1079 

65268  1080 
lemma sign_uint_Pls [simp]: "bin_sign (uint x) = 0" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1081 
by (simp add: sign_Pls_ge_0) 
37660  1082 

70185  1083 
lemma uint_m2p_neg: "uint x  2 ^ LENGTH('a) < 0" 
65268  1084 
for x :: "'a::len0 word" 
45805  1085 
by (simp only: diff_less_0_iff_less uint_lt2p) 
1086 

70185  1087 
lemma uint_m2p_not_non_neg: "\<not> 0 \<le> uint x  2 ^ LENGTH('a)" 
65268  1088 
for x :: "'a::len0 word" 
45805  1089 
by (simp only: not_le uint_m2p_neg) 
37660  1090 

70185  1091 
lemma lt2p_lem: "LENGTH('a) \<le> n \<Longrightarrow> uint w < 2 ^ n" 
65268  1092 
for w :: "'a::len0 word" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

1093 
by (metis bintr_uint bintrunc_mod2p int_mod_lem zless2p) 
37660  1094 

45805  1095 
lemma uint_le_0_iff [simp]: "uint x \<le> 0 \<longleftrightarrow> uint x = 0" 
70749
5d06b7bb9d22
More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
paulson <lp15@cam.ac.uk>
parents:
70342
diff
changeset

1096 
by (fact uint_ge_0 [THEN leD, THEN antisym_conv1]) 
37660  1097 

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

1098 
lemma uint_nat: "uint w = int (unat w)" 
65268  1099 
by (auto simp: unat_def) 
1100 

70185  1101 
lemma uint_numeral: "uint (numeral b :: 'a::len0 word) = numeral b mod 2 ^ LENGTH('a)" 
65268  1102 
by (simp only: word_numeral_alt int_word_uint) 
1103 

70185  1104 
lemma uint_neg_numeral: "uint ( numeral b :: 'a::len0 word) =  numeral b mod 2 ^ LENGTH('a)" 
65268  1105 
by (simp only: word_neg_numeral_alt int_word_uint) 
1106 

70185  1107 
lemma unat_numeral: "unat (numeral b :: 'a::len0 word) = numeral b mod 2 ^ LENGTH('a)" 
37660  1108 
apply (unfold unat_def) 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

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

1111 
apply (rule zero_le_numeral) 
37660  1112 
apply (simp_all add: nat_power_eq) 
1113 
done 

1114 

65268  1115 
lemma sint_numeral: 
1116 
"sint (numeral b :: 'a::len word) = 

1117 
(numeral b + 

70185  1118 
2 ^ (LENGTH('a)  1)) mod 2 ^ LENGTH('a)  
1119 
2 ^ (LENGTH('a)  1)" 

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

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

1121 

65268  1122 
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0" 
45958  1123 
unfolding word_0_wi .. 
1124 

65268  1125 
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1" 
45958  1126 
unfolding word_1_wi .. 
1127 

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

1128 
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

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

1130 

65268  1131 
lemma word_of_int_numeral [simp] : "(word_of_int (numeral bin) :: 'a::len0 word) = numeral bin" 
1132 
by (simp only: word_numeral_alt) 

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

1133 

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

1134 
lemma word_of_int_neg_numeral [simp]: 
65268  1135 
"(word_of_int ( numeral bin) :: 'a::len0 word) =  numeral bin" 
1136 
by (simp only: word_numeral_alt wi_hom_syms) 

1137 

1138 
lemma word_int_case_wi: 

70185  1139 
"word_int_case f (word_of_int i :: 'b word) = f (i mod 2 ^ LENGTH('b::len0))" 
65268  1140 
by (simp add: word_int_case_def word_uint.eq_norm) 
1141 

1142 
lemma word_int_split: 

1143 
"P (word_int_case f x) = 

70185  1144 
(\<forall>i. x = (word_of_int i :: 'b::len0 word) \<and> 0 \<le> i \<and> i < 2 ^ LENGTH('b) \<longrightarrow> P (f i))" 
71942  1145 
by (auto simp: word_int_case_def word_uint.eq_norm) 
65268  1146 

1147 
lemma word_int_split_asm: 

1148 
"P (word_int_case f x) = 

70185  1149 
(\<nexists>n. x = (word_of_int n :: 'b::len0 word) \<and> 0 \<le> n \<and> n < 2 ^ LENGTH('b::len0) \<and> \<not> P (f n))" 
71942  1150 
by (auto simp: word_int_case_def word_uint.eq_norm) 
45805  1151 

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

37660  1154 

65268  1155 
lemma uint_range_size: "0 \<le> uint w \<and> uint w < 2 ^ size w" 
37660  1156 
unfolding word_size by (rule uint_range') 
1157 

65268  1158 
lemma sint_range_size: " (2 ^ (size w  Suc 0)) \<le> sint w \<and> sint w < 2 ^ (size w  Suc 0)" 
37660  1159 
unfolding word_size by (rule sint_range') 
1160 

65268  1161 
lemma sint_above_size: "2 ^ (size w  1) \<le> x \<Longrightarrow> sint w < x" 
1162 
for w :: "'a::len word" 

45805  1163 
unfolding word_size by (rule less_le_trans [OF sint_lt]) 
1164 

65268  1165 
lemma sint_below_size: "x \<le>  (2 ^ (size w  1)) \<Longrightarrow> x \<le> sint w" 
1166 
for w :: "'a::len word" 

45805  1167 
unfolding word_size by (rule order_trans [OF _ sint_ge]) 
37660  1168 

55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

1169 

61799  1170 
subsection \<open>Testing bits\<close> 
46010  1171 

65268  1172 
lemma test_bit_eq_iff: "test_bit u = test_bit v \<longleftrightarrow> u = v" 
1173 
for u v :: "'a::len0 word" 

37660  1174 
unfolding word_test_bit_def by (simp add: bin_nth_eq_iff) 
1175 

65268  1176 
lemma test_bit_size [rule_format] : "w !! n \<longrightarrow> n < size w" 
1177 
for w :: "'a::len0 word" 

37660  1178 
apply (unfold word_test_bit_def) 
1179 
apply (subst word_ubin.norm_Rep [symmetric]) 

1180 
apply (simp only: nth_bintr word_size) 

1181 
apply fast 

1182 
done 

1183 

71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1184 
lemma word_eq_iff: "x = y \<longleftrightarrow> (\<forall>n<LENGTH('a). x !! n = y !! n)" (is \<open>?P \<longleftrightarrow> ?Q\<close>) 
65268  1185 
for x y :: "'a::len0 word" 
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1186 
proof 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1187 
assume ?P 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1188 
then show ?Q 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1189 
by simp 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1190 
next 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1191 
assume ?Q 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1192 
then have *: \<open>bit (uint x) n \<longleftrightarrow> bit (uint y) n\<close> if \<open>n < LENGTH('a)\<close> for n 
71949  1193 
using that by (simp add: word_test_bit_def) 
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1194 
show ?P 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1195 
proof (rule word_uint_eqI, rule bit_eqI, rule iffI) 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1196 
fix n 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1197 
assume \<open>bit (uint x) n\<close> 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1198 
then have \<open>n < LENGTH('a)\<close> 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1199 
by (simp add: bit_take_bit_iff uint.rep_eq) 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1200 
with * \<open>bit (uint x) n\<close> 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1201 
show \<open>bit (uint y) n\<close> 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1202 
by simp 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1203 
next 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1204 
fix n 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1205 
assume \<open>bit (uint y) n\<close> 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1206 
then have \<open>n < LENGTH('a)\<close> 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1207 
by (simp add: bit_take_bit_iff uint.rep_eq) 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1208 
with * \<open>bit (uint y) n\<close> 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1209 
show \<open>bit (uint x) n\<close> 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1210 
by simp 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1211 
qed 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

1212 
qed 
46021  1213 

65268  1214 
lemma word_eqI: "(\<And>n. n < size u \<longrightarrow> u !! n = v !! n) \<Longrightarrow> u = v" 
1215 
for u :: "'a::len0 word" 

46021  1216 
by (simp add: word_size word_eq_iff) 
37660  1217 

65268  1218 
lemma word_eqD: "u = v \<Longrightarrow> u !! x = v !! x" 
1219 
for u v :: "'a::len0 word" 

45805  1220 
by simp 
37660  1221 

65268  1222 
lemma test_bit_bin': "w !! n \<longleftrightarrow> n < size w \<and> bin_nth (uint w) n" 
1223 
by (simp add: word_test_bit_def word_size nth_bintr [symmetric]) 

37660  1224 

1225 
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] 

1226 

70185  1227 
lemma bin_nth_uint_imp: "bin_nth (uint w) n \<Longrightarrow> n < LENGTH('a)" 
65268  1228 
for w :: "'a::len0 word" 
37660  1229 
apply (rule nth_bintr [THEN iffD1, THEN conjunct1]) 
1230 
apply (subst word_ubin.norm_Rep) 

1231 
apply assumption 

1232 
done 

1233 

46057  1234 
lemma bin_nth_sint: 
70185  1235 
"LENGTH('a) \<le> n \<Longrightarrow> 
1236 
bin_nth (sint w) n = bin_nth (sint w) (LENGTH('a)  1)" 

65268  1237 
for w :: "'a::len word" 
37660  1238 
apply (subst word_sbin.norm_Rep [symmetric]) 
46057  1239 
apply (auto simp add: nth_sbintr) 
37660  1240 
done 
1241 

67408  1242 
\<comment> \<open>type definitions theorem for in terms of equivalent bool list\<close> 
65268  1243 
lemma td_bl: 
1244 
"type_definition 

1245 
(to_bl :: 'a::len0 word \<Rightarrow> bool list) 

1246 
of_bl 

70185  1247 
{bl. length bl = LENGTH('a)}" 
37660  1248 
apply (unfold type_definition_def of_bl_def to_bl_def) 
1249 
apply (simp add: word_ubin.eq_norm) 

1250 
apply safe 

1251 
apply (drule sym) 

1252 
apply simp 

1253 
done 

1254 

1255 
interpretation word_bl: 

65268  1256 
type_definition 
1257 
"to_bl :: 'a::len0 word \<Rightarrow> bool list" 

1258 
of_bl 

70185  1259 
"{bl. length bl = LENGTH('a::len0)}" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

1260 
by (fact td_bl) 
37660  1261 

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

1262 
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

1263 

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

1264 
lemma word_size_bl: "size w = size (to_bl w)" 
65268  1265 
by (auto simp: word_size) 
1266 

1267 
lemma to_bl_use_of_bl: "to_bl w = bl \<longleftrightarrow> 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

1268 
by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq]) 
37660  1269 

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

65268  1271 
by (simp add: word_reverse_def word_bl.Abs_inverse) 
37660  1272 

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

65268  1274 
by (simp add: word_reverse_def word_bl.Abs_inverse) 
37660  1275 

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

1276 
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

1277 
by (metis word_rev_rev) 
37660  1278 

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

1281 

65268  1282 
lemma length_bl_gt_0 [iff]: "0 < length (to_bl x)" 
1283 
for x :: "'a::len word" 

45805  1284 
unfolding word_bl_Rep' by (rule len_gt_0) 
1285 

65268  1286 
lemma bl_not_Nil [iff]: "to_bl x \<noteq> []" 
1287 
for x :: "'a::len word" 

45805  1288 
by (fact length_bl_gt_0 [unfolded length_greater_0_conv]) 
1289 

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

45805  1292 
by (fact length_bl_gt_0 [THEN gr_implies_not0]) 
37660  1293 

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

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

1297 
apply simp 

1298 
done 

1299 

65268  1300 