author  haftmann 
Thu, 18 Jun 2020 09:07:29 +0000  
changeset 71948  6ede899d26d3 
parent 71947  476b9e6904d9 
child 71949  5b8b1183c641 
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 

65328  250 
definition word_div_def: "a div b = word_of_int (uint a div uint b)" 
251 

252 
definition word_mod_def: "a mod b = word_of_int (uint a mod uint b)" 

37660  253 

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

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

256 

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

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

258 

70901  259 
quickcheck_generator word 
260 
constructors: 

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

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

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

264 

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

266 

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

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

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

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

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

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

274 
and word_0_wi: "0 = word_of_int 0" 

275 
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

276 
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

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

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

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

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

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

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

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

284 

65268  285 
lemma wi_homs: 
286 
shows wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" 

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

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

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

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

291 
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

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

293 

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

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

295 

46013  296 
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi 
46009  297 

298 
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

299 

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

300 
instance word :: (len) comm_ring_1 
45810  301 
proof 
70185  302 
have *: "0 < LENGTH('a)" by (rule len_gt_0) 
65268  303 
show "(0::'a word) \<noteq> 1" 
304 
by transfer (use * in \<open>auto simp add: gr0_conv_Suc\<close>) 

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

306 

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

307 
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

308 
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

309 

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

310 
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

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

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

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

315 

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

37660  318 

45547  319 

61799  320 
subsection \<open>Ordering\<close> 
45547  321 

322 
instantiation word :: (len0) linorder 

323 
begin 

324 

65268  325 
definition word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b" 
326 

327 
definition word_less_def: "a < b \<longleftrightarrow> uint a < uint b" 

37660  328 

45547  329 
instance 
61169  330 
by standard (auto simp: word_less_def word_le_def) 
45547  331 

332 
end 

333 

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

336 

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

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

37660  339 

340 

61799  341 
subsection \<open>Bitwise operations\<close> 
37660  342 

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

345 

346 
definition shiftr1 :: "'a::len0 word \<Rightarrow> 'a word" 

347 
\<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close> 

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

349 

350 
instantiation word :: (len0) bit_operations 

37660  351 
begin 
352 

71826  353 
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

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

355 

71826  356 
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

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

358 

71826  359 
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

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

361 

71826  362 
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

363 
by (metis bin_trunc_xor) 
37660  364 

65268  365 
definition word_test_bit_def: "test_bit a = bin_nth (uint a)" 
366 

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

368 

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

37660  370 

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

65268  373 
definition shiftl_def: "w << n = (shiftl1 ^^ n) w" 
374 

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

37660  376 

377 
instance .. 

378 

379 
end 

380 

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

383 
by (simp add: msb_word_def sint_uint) 

384 

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

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

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

389 
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

390 
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

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

392 

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

395 

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

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

37660  398 

399 

61799  400 
subsection \<open>Shift operations\<close> 
37660  401 

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

404 

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

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

407 

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

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

410 

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

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

413 

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

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

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

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

419 

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

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

37660  422 

423 

61799  424 
subsection \<open>Rotation\<close> 
37660  425 

65268  426 
definition rotater1 :: "'a list \<Rightarrow> 'a list" 
427 
where "rotater1 ys = 

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

429 

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

431 
where "rotater n = rotater1 ^^ n" 

432 

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

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

435 

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

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

438 

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

440 
where "word_roti i w = 

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

37660  442 

443 

61799  444 
subsection \<open>Split and cat operations\<close> 
37660  445 

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

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

450 
where "word_split a = 

70185  451 
(case bin_split (LENGTH('c)) (uint a) of 
65268  452 
(u, v) \<Rightarrow> (word_of_int u, word_of_int v))" 
453 

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

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

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

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

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

461 
\<comment> \<open>Largest representable machine integer.\<close> 
71946  462 
where "max_word \<equiv>  1" 
37660  463 

464 

61799  465 
subsection \<open>Theorems about typedefs\<close> 
46010  466 

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

70185  470 
lemma uint_sint: "uint w = bintrunc (LENGTH('a)) (sint w)" 
65328  471 
for w :: "'a::len word" 
65268  472 
by (auto simp: sint_uint bintrunc_sbintrunc_le) 
473 

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

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

478 
apply (simp add: min.absorb2) 
37660  479 
done 
480 

46057  481 
lemma wi_bintr: 
70185  482 
"LENGTH('a::len0) \<le> n \<Longrightarrow> 
46057  483 
word_of_int (bintrunc n w) = (word_of_int w :: 'a word)" 
65268  484 
by (auto simp: word_ubin.norm_eq_iff [symmetric] min.absorb1) 
485 

486 
lemma td_ext_sbin: 

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

37660  489 
apply (unfold td_ext_def' sint_uint) 
490 
apply (simp add : word_ubin.eq_norm) 

70185  491 
apply (cases "LENGTH('a)") 
37660  492 
apply (auto simp add : sints_def) 
493 
apply (rule sym [THEN trans]) 

65268  494 
apply (rule word_ubin.Abs_norm) 
37660  495 
apply (simp only: bintrunc_sbintrunc) 
496 
apply (drule sym) 

497 
apply simp 

498 
done 

499 

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

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

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

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

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

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

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

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

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

511 
\<close> 

37660  512 
interpretation word_sint: 
65268  513 
td_ext 
514 
"sint ::'a::len word \<Rightarrow> int" 

515 
word_of_int 

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

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

37660  519 
by (rule td_ext_sint) 
520 

521 
interpretation word_sbin: 

65268  522 
td_ext 
523 
"sint ::'a::len word \<Rightarrow> int" 

524 
word_of_int 

70185  525 
"sints (LENGTH('a::len))" 
526 
"sbintrunc (LENGTH('a::len)  1)" 

37660  527 
by (rule td_ext_sbin) 
528 

45604  529 
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] 
37660  530 

531 
lemmas td_sint = word_sint.td 

532 

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

65268  536 
lemmas word_reverse_no_def [simp] = 
537 
word_reverse_def [of "numeral w"] for w 

37660  538 

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

541 

65268  542 
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

543 
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

544 

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

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

546 

65268  547 
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

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

549 

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

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

551 

47372  552 
lemma word_numeral_transfer [transfer_rule]: 
67399  553 
"(rel_fun (=) pcr_word) numeral numeral" 
554 
"(rel_fun (=) pcr_word) ( numeral) ( numeral)" 

55945  555 
apply (simp_all add: rel_fun_def word.pcr_cr_eq cr_word_def) 
65268  556 
using word_numeral_alt [symmetric] word_neg_numeral_alt [symmetric] by auto 
47372  557 

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

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

562 

65268  563 
lemma uint_bintrunc_neg [simp]: 
70185  564 
"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

565 
by (simp only: word_neg_numeral_alt word_ubin.eq_norm) 
37660  566 

45805  567 
lemma sint_sbintrunc [simp]: 
70185  568 
"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

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

570 

65268  571 
lemma sint_sbintrunc_neg [simp]: 
70185  572 
"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

573 
by (simp only: word_neg_numeral_alt word_sbin.eq_norm) 
37660  574 

45805  575 
lemma unat_bintrunc [simp]: 
70185  576 
"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

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

578 

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

579 
lemma unat_bintrunc_neg [simp]: 
70185  580 
"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

581 
by (simp only: unat_def uint_bintrunc_neg) 
37660  582 

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

37660  585 
apply (unfold word_size) 
586 
apply (rule word_uint.Rep_eqD) 

587 
apply (rule box_equals) 

588 
defer 

589 
apply (rule word_ubin.norm_Rep)+ 

590 
apply simp 

591 
done 

592 

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

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

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

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

603 
by transfer (simp add: bintrunc_mod2p) 

604 

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

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

65268  613 
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

614 
by (simp add: sign_Pls_ge_0) 
37660  615 

70185  616 
lemma uint_m2p_neg: "uint x  2 ^ LENGTH('a) < 0" 
65268  617 
for x :: "'a::len0 word" 
45805  618 
by (simp only: diff_less_0_iff_less uint_lt2p) 
619 

70185  620 
lemma uint_m2p_not_non_neg: "\<not> 0 \<le> uint x  2 ^ LENGTH('a)" 
65268  621 
for x :: "'a::len0 word" 
45805  622 
by (simp only: not_le uint_m2p_neg) 
37660  623 

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

626 
by (metis bintr_uint bintrunc_mod2p int_mod_lem zless2p) 
37660  627 

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

629 
by (fact uint_ge_0 [THEN leD, THEN antisym_conv1]) 
37660  630 

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

631 
lemma uint_nat: "uint w = int (unat w)" 
65268  632 
by (auto simp: unat_def) 
633 

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

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

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

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

644 
apply (rule zero_le_numeral) 
37660  645 
apply (simp_all add: nat_power_eq) 
646 
done 

647 

65268  648 
lemma sint_numeral: 
649 
"sint (numeral b :: 'a::len word) = 

650 
(numeral b + 

70185  651 
2 ^ (LENGTH('a)  1)) mod 2 ^ LENGTH('a)  
652 
2 ^ (LENGTH('a)  1)" 

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

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

654 

65268  655 
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0" 
45958  656 
unfolding word_0_wi .. 
657 

65268  658 
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1" 
45958  659 
unfolding word_1_wi .. 
660 

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

661 
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

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

663 

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

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

666 

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

667 
lemma word_of_int_neg_numeral [simp]: 
65268  668 
"(word_of_int ( numeral bin) :: 'a::len0 word) =  numeral bin" 
669 
by (simp only: word_numeral_alt wi_hom_syms) 

670 

671 
lemma word_int_case_wi: 

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

675 
lemma word_int_split: 

676 
"P (word_int_case f x) = 

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

680 
lemma word_int_split_asm: 

681 
"P (word_int_case f x) = 

70185  682 
(\<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  683 
by (auto simp: word_int_case_def word_uint.eq_norm) 
45805  684 

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

37660  687 

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

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

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

45805  696 
unfolding word_size by (rule less_le_trans [OF sint_lt]) 
697 

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

45805  700 
unfolding word_size by (rule order_trans [OF _ sint_ge]) 
37660  701 

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

702 

61799  703 
subsection \<open>Testing bits\<close> 
46010  704 

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

37660  707 
unfolding word_test_bit_def by (simp add: bin_nth_eq_iff) 
708 

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

37660  711 
apply (unfold word_test_bit_def) 
712 
apply (subst word_ubin.norm_Rep [symmetric]) 

713 
apply (simp only: nth_bintr word_size) 

714 
apply fast 

715 
done 

716 

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

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

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

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

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

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

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

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

725 
then have *: \<open>bit (uint x) n \<longleftrightarrow> bit (uint y) n\<close> if \<open>n < LENGTH('a)\<close> for n 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

726 
using that by (simp add: word_test_bit_def bin_nth_iff) 
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

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

728 
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

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

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

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

732 
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

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

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

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

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

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

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

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

740 
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

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

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

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

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

745 
qed 
46021  746 

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

46021  749 
by (simp add: word_size word_eq_iff) 
37660  750 

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

45805  753 
by simp 
37660  754 

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

37660  757 

758 
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] 

759 

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

764 
apply assumption 

765 
done 

766 

46057  767 
lemma bin_nth_sint: 
70185  768 
"LENGTH('a) \<le> n \<Longrightarrow> 
769 
bin_nth (sint w) n = bin_nth (sint w) (LENGTH('a)  1)" 

65268  770 
for w :: "'a::len word" 
37660  771 
apply (subst word_sbin.norm_Rep [symmetric]) 
46057  772 
apply (auto simp add: nth_sbintr) 
37660  773 
done 
774 

67408  775 
\<comment> \<open>type definitions theorem for in terms of equivalent bool list\<close> 
65268  776 
lemma td_bl: 
777 
"type_definition 

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

779 
of_bl 

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

783 
apply safe 

784 
apply (drule sym) 

785 
apply simp 

786 
done 

787 

788 
interpretation word_bl: 

65268  789 
type_definition 
790 
"to_bl :: 'a::len0 word \<Rightarrow> bool list" 

791 
of_bl 

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

793 
by (fact td_bl) 
37660  794 

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

795 
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

796 

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

797 
lemma word_size_bl: "size w = size (to_bl w)" 
65268  798 
by (auto simp: word_size) 
799 

800 
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

801 
by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq]) 
37660  802 

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

65268  804 
by (simp add: word_reverse_def word_bl.Abs_inverse) 
37660  805 

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

65268  807 
by (simp add: word_reverse_def word_bl.Abs_inverse) 
37660  808 

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

809 
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

810 
by (metis word_rev_rev) 
37660  811 

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

814 

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

45805  817 
unfolding word_bl_Rep' by (rule len_gt_0) 
818 

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

45805  821 
by (fact length_bl_gt_0 [unfolded length_greater_0_conv]) 
822 

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

45805  825 
by (fact length_bl_gt_0 [THEN gr_implies_not0]) 
37660  826 

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

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

830 
apply simp 

831 
done 

832 

65268  833 
lemma of_bl_drop': 
70185  834 
"lend = length bl  LENGTH('a::len0) \<Longrightarrow> 
37660  835 
of_bl (drop lend bl) = (of_bl bl :: 'a word)" 
65268  836 
by (auto simp: of_bl_def trunc_bl2bin [symmetric]) 
837 

838 
lemma test_bit_of_bl: 

70185  839 
"(of_bl bl::'a::len0 word) !! n = (rev bl ! n \<and> n < LENGTH('a) \<and> n < length bl)" 
65328  840 
by (auto simp add: of_bl_def word_test_bit_def word_size 
841 
word_ubin.eq_norm nth_bintr bin_nth_of_bl) 

65268  842 

70185  843 
lemma no_of_bl: "(numeral bin ::'a::len0 word) = of_bl (bin_to_bl (LENGTH('a)) (numeral bin))" 
65268  844 
by (simp add: of_bl_def) 
37660  845 

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

846 
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)" 
65268  847 
by (auto simp: word_size to_bl_def) 
37660  848 

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

65268  850 
by (simp add: uint_bl word_size) 
851 

70185  852 
lemma to_bl_of_bin: "to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (LENGTH('a)) bin" 
65268  853 
by (auto simp: uint_bl word_ubin.eq_norm word_size) 
37660  854 

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

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

856 
"to_bl (numeral bin::'a::len0 word) = 
70185  857 
bin_to_bl (LENGTH('a)) (numeral bin)" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

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

859 

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

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

861 
"to_bl ( numeral bin::'a::len0 word) = 
70185  862 
bin_to_bl (LENGTH('a)) ( numeral bin)" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

863 
unfolding word_neg_numeral_alt by (rule to_bl_of_bin) 
37660  864 

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

65268  866 
by (simp add: uint_bl word_size) 
867 

70185  868 
lemma uint_bl_bin: "bl_to_bin (bin_to_bl (LENGTH('a)) (uint x)) = uint x" 
65268  869 
for x :: "'a::len0 word" 
46011  870 
by (rule trans [OF bin_bl_bin word_ubin.norm_Rep]) 
45604  871 

67408  872 
\<comment> \<open>naturals\<close> 
37660  873 
lemma uints_unats: "uints n = int ` unats n" 
874 
apply (unfold unats_def uints_num) 

875 
apply safe 

65268  876 
apply (rule_tac image_eqI) 
877 
apply (erule_tac nat_0_le [symmetric]) 

66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
66808
diff
changeset

878 
by auto 
37660  879 

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

65268  881 
by (auto simp: uints_unats image_iff) 
882 

883 
lemmas bintr_num = 

884 
word_ubin.norm_eq_iff [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b 

885 
lemmas sbintr_num = 

886 
word_sbin.norm_eq_iff [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b 

37660  887 

888 
lemma num_of_bintr': 

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

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

891 
unfolding bintr_num by (erule subst, simp) 
37660  892 

893 
lemma num_of_sbintr': 

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

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

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

897 

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

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

899 
"(numeral x :: 'a word) = 
70185  900 
word_of_int (bintrunc (LENGTH('a::len0)) (numeral x))" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

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

902 

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

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

904 
"(numeral x :: 'a word) = 
70185  905 
word_of_int (sbintrunc (LENGTH('a::len)  1) (numeral x))" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

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

907 

67408  908 
text \<open> 
909 
\<open>cast\<close>  note, no arg for new length, as it's determined by type of result, 

910 
thus in \<open>cast w = w\<close>, the type means cast to length of \<open>w\<close>! 

911 
\<close> 

37660  912 

913 
lemma ucast_id: "ucast w = w" 

65268  914 
by (auto simp: ucast_def) 
37660  915 

916 
lemma scast_id: "scast w = w" 

65268  917 
by (auto simp: scast_def) 
37660  918 

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

919 
lemma ucast_bl: "ucast w = of_bl (to_bl w)" 
65268  920 
by (auto simp: ucast_def of_bl_def uint_bl word_size) 
921 

70185  922 
lemma nth_ucast: "(ucast w::'a::len0 word) !! n = (w !! n \<and> n < LENGTH('a))" 
65268  923 
by (simp add: ucast_def test_bit_bin word_ubin.eq_norm nth_bintr word_size) 
924 
(fast elim!: bin_nth_uint_imp) 

37660  925 

67408  926 
\<comment> \<open>literal u(s)cast\<close> 
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset

927 
lemma ucast_bintr [simp]: 
65328  928 
"ucast (numeral w :: 'a::len0 word) = 
70185  929 
word_of_int (bintrunc (LENGTH('a)) (numeral w))" 
65268  930 
by (simp add: ucast_def) 
931 

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

932 
(* TODO: neg_numeral *) 
37660  933 

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

934 
lemma scast_sbintr [simp]: 
65268  935 
"scast (numeral w ::'a::len word) = 
70185  936 
word_of_int (sbintrunc (LENGTH('a)  Suc 0) (numeral w))" 
65268  937 
by (simp add: scast_def) 
37660  938 

70185  939 
lemma source_size: "source_size (c::'a::len0 word \<Rightarrow> _) = LENGTH('a)" 
46011  940 
unfolding source_size_def word_size Let_def .. 
941 

70185  942 
lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len0 word) = LENGTH('b)" 
46011  943 
unfolding target_size_def word_size Let_def .. 
944 

70185  945 
lemma is_down: "is_down c \<longleftrightarrow> LENGTH('b) \<le> LENGTH('a)" 
65268  946 
for c :: "'a::len0 word \<Rightarrow> 'b::len0 word" 
947 
by (simp only: is_down_def source_size target_size) 

948 

70185  949 
lemma is_up: "is_up c \<longleftrightarrow> LENGTH('a) \<le> LENGTH('b)" 
65268  950 
for c :: "'a::len0 word \<Rightarrow> 'b::len0 word" 
951 
by (simp only: is_up_def source_size target_size) 

37660  952 

45604  953 
lemmas is_up_down = trans [OF is_up is_down [symmetric]] 
37660  954 

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

958 
apply (rule ext) 

959 
apply (unfold ucast_def scast_def uint_sint) 

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

961 
apply simp 

962 
done 

963 

45811  964 
lemma word_rev_tf: 
965 
"to_bl (of_bl bl::'a::len0 word) = 

70185  966 
rev (takefill False (LENGTH('a)) (rev bl))" 
65268  967 
by (auto simp: of_bl_def uint_bl bl_bin_bl_rtf word_ubin.eq_norm word_size) 
37660  968 

45811  969 
lemma word_rep_drop: 
970 
"to_bl (of_bl bl::'a::len0 word) = 

70185  971 
replicate (LENGTH('a)  length bl) False @ 
972 
drop (length bl  LENGTH('a)) bl" 

45811  973 
by (simp add: word_rev_tf takefill_alt rev_take) 
37660  974 

65268  975 
lemma to_bl_ucast: 
976 
"to_bl (ucast (w::'b::len0 word) ::'a::len0 word) = 

70185  977 
replicate (LENGTH('a)  LENGTH('b)) False @ 
978 
drop (LENGTH('b)  LENGTH('a)) (to_bl w)" 

37660  979 
apply (unfold ucast_bl) 
980 
apply (rule trans) 

981 
apply (rule word_rep_drop) 

982 
apply simp 

983 
done 

984 

45811  985 
lemma ucast_up_app [OF refl]: 
65268  986 
"uc = ucast \<Longrightarrow> source_size uc + n = target_size uc \<Longrightarrow> 
37660  987 
to_bl (uc w) = replicate n False @ (to_bl w)" 
988 
by (auto simp add : source_size target_size to_bl_ucast) 

989 

45811  990 
lemma ucast_down_drop [OF refl]: 
65268  991 
"uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> 
37660  992 
to_bl (uc w) = drop n (to_bl w)" 
993 
by (auto simp add : source_size target_size to_bl_ucast) 

994 

45811  995 
lemma scast_down_drop [OF refl]: 
65268  996 
"sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> 
37660  997 
to_bl (sc w) = drop n (to_bl w)" 
998 
apply (subgoal_tac "sc = ucast") 

999 
apply safe 

1000 
apply simp 

45811  1001 
apply (erule ucast_down_drop) 
1002 
apply (rule down_cast_same [symmetric]) 

37660  1003 
apply (simp add : source_size target_size is_down) 
1004 
done 

1005 

65268  1006 
lemma sint_up_scast [OF refl]: "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w" 
37660  1007 
apply (unfold is_up) 
1008 
apply safe 

1009 
apply (simp add: scast_def word_sbin.eq_norm) 

1010 
apply (rule box_equals) 

1011 
prefer 3 

1012 
apply (rule word_sbin.norm_Rep) 

1013 
apply (rule sbintrunc_sbintrunc_l) 

1014 
defer 

1015 
apply (subst word_sbin.norm_Rep) 

1016 
apply (rule refl) 

1017 
apply simp 

1018 
done 

1019 

65268  1020 
lemma uint_up_ucast [OF refl]: "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w" 
37660  1021 
apply (unfold is_up) 
1022 
apply safe 

1023 
apply (rule bin_eqI) 

1024 
apply (fold word_test_bit_def) 

1025 
apply (auto simp add: nth_ucast) 

1026 
apply (auto simp add: test_bit_bin) 

1027 
done 

45811  1028 

65268  1029 
lemma ucast_up_ucast [OF refl]: "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> ucast (uc w) = ucast w" 
37660  1030 
apply (simp (no_asm) add: ucast_def) 
1031 
apply (clarsimp simp add: uint_up_ucast) 

1032 
done 

65268  1033 

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

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

1037 
done 

65268  1038 

1039 
lemma ucast_of_bl_up [OF refl]: "w = of_bl bl \<Longrightarrow> size bl \<le> size w \<Longrightarrow> ucast w = of_bl bl" 

37660  1040 
by (auto simp add : nth_ucast word_size test_bit_of_bl intro!: word_eqI) 
1041 

1042 
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] 

1043 
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] 

1044 

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

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

1047 
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] 

1048 
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] 

1049 

1050 
lemma up_ucast_surj: 

65268  1051 
"is_up (ucast :: 'b::len0 word \<Rightarrow> 'a::len0 word) \<Longrightarrow> 
1052 
surj (ucast :: 'a word \<Rightarrow> 'b word)" 

1053 
by (rule surjI) (erule ucast_up_ucast_id) 

37660  1054 

1055 
lemma up_scast_surj: 

65268  1056 
"is_up (scast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow> 
1057 
surj (scast :: 'a word \<Rightarrow> 'b word)" 

1058 
by (rule surjI) (erule scast_up_scast_id) 

37660  1059 

1060 
lemma down_scast_inj: 

65268  1061 
"is_down (scast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow> 
1062 
inj_on (ucast :: 'a word \<Rightarrow> 'b word) A" 

37660  1063 
by (rule inj_on_inverseI, erule scast_down_scast_id) 
1064 

1065 
lemma down_ucast_inj: 

65268  1066 
"is_down (ucast :: 'b::len0 word \<Rightarrow> 'a::len0 word) \<Longrightarrow> 
1067 
inj_on (ucast :: 'a word \<Rightarrow> 'b word) A" 

1068 
by (rule inj_on_inverseI) (erule ucast_down_ucast_id) 

37660  1069 

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

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

45811  1072 

65268  1073 
lemma ucast_down_wi [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (word_of_int x) = word_of_int x" 
46646  1074 
apply (unfold is_down) 
37660  1075 
apply (clarsimp simp add: ucast_def word_ubin.eq_norm) 
1076 
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 

1077 
apply (erule bintrunc_bintrunc_ge) 

1078 
done 

45811  1079 

65268  1080 
lemma ucast_down_no [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (numeral bin) = numeral bin" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1081 
unfolding word_numeral_alt by clarify (rule ucast_down_wi) 
46646  1082 

65268  1083 
lemma ucast_down_bl [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl" 
46646  1084 
unfolding of_bl_def by clarify (erule ucast_down_wi) 
37660  1085 

1086 
lemmas slice_def' = slice_def [unfolded word_size] 

1087 
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] 

1088 

1089 
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def 

1090 

1091 

61799  1092 
subsection \<open>Word Arithmetic\<close> 
37660  1093 

65268  1094 
lemma word_less_alt: "a < b \<longleftrightarrow> uint a < uint b" 
55818  1095 
by (fact word_less_def) 
37660  1096 

1097 
lemma signed_linorder: "class.linorder word_sle word_sless" 

65268  1098 
by standard (auto simp: word_sle_def word_sless_def) 
37660  1099 

1100 
interpretation signed: linorder "word_sle" "word_sless" 

1101 
by (rule signed_linorder) 

1102 

65268  1103 
lemma udvdI: "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" 
37660  1104 
by (auto simp: udvd_def) 
1105 

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

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

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

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

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

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

1111 
lemmas word_sle_no [simp] = word_sle_def [of "numeral a" "numeral b"] for a b 
37660  1112 

65268  1113 
lemma word_m1_wi: " 1 = word_of_int ( 1)" 
1114 
by (simp add: word_neg_numeral_alt [of Num.One]) 

37660  1115 

46648  1116 
lemma word_0_bl [simp]: "of_bl [] = 0" 
65268  1117 
by (simp add: of_bl_def) 
1118 

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

1120 
by (simp add: of_bl_def bl_to_bin_def) 

46648  1121 

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

1123 
unfolding word_0_wi word_ubin.eq_norm by simp 

37660  1124 

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

1125 
lemma of_bl_0 [simp]: "of_bl (replicate n False) = 0" 
46648  1126 
by (simp add: of_bl_def bl_to_bin_rep_False) 
37660  1127 

70185  1128 
lemma to_bl_0 [simp]: "to_bl (0::'a::len0 word) = replicate (LENGTH('a)) False" 
65268  1129 
by (simp add: uint_bl word_size bin_to_bl_zero) 
1130 

1131 
lemma uint_0_iff: "uint x = 0 \<longleftrightarrow> x = 0" 

55818  1132 
by (simp add: word_uint_eq_iff) 
1133 

65268  1134 
lemma unat_0_iff: "unat x = 0 \<longleftrightarrow> x = 0" 
1135 
by (auto simp: unat_def nat_eq_iff uint_0_iff) 

1136 

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

1138 
by (auto simp: unat_def) 

1139 

1140 
lemma size_0_same': "size w = 0 \<Longrightarrow> w = v" 

1141 
for v w :: "'a::len0 word" 

37660  1142 
apply (unfold word_size) 
1143 
apply (rule box_equals) 

1144 
defer 

1145 
apply (rule word_uint.Rep_inverse)+ 

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

1147 
apply simp 

1148 
done 

1149 

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

1150 
lemmas size_0_same = size_0_same' [unfolded word_size] 
37660  1151 

1152 
lemmas unat_eq_0 = unat_0_iff 

1153 
lemmas unat_eq_zero = unat_0_iff 

1154 

65268  1155 
lemma unat_gt_0: "0 < unat x \<longleftrightarrow> x \<noteq> 0" 
1156 
by (auto simp: unat_0_iff [symmetric]) 

37660  1157 

45958  1158 
lemma ucast_0 [simp]: "ucast 0 = 0" 
65268  1159 
by (simp add: ucast_def) 
45958  1160 

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

65268  1162 
by (simp add: sint_uint) 
45958  1163 

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

65268  1165 
by (simp add: scast_def) 
37660  1166 

58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
58061
diff
changeset

1167 
lemma sint_n1 [simp] : "sint ( 1) =  1" 
65268  1168 
by (simp only: word_m1_wi word_sbin.eq_norm) simp 
54489
03ff4d1e6784
eliminiated neg_numeral in favour of  (numeral _)
haftmann
parents:
54225
diff
changeset

1169 

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

1170 
lemma scast_n1 [simp]: "scast ( 1) =  1" 
65268  1171 
by (simp add: scast_def) 
45958  1172 

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

71947  1174 
by (simp only: word_1_wi word_ubin.eq_norm) simp 
45958  1175 

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

65268  1177 
by (simp add: unat_def) 
45958  1178 

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

65268  1180 
by (simp add: ucast_def) 
37660  1181 

67408  1182 
\<comment> \<open>now, to get the weaker results analogous to \<open>word_div\<close>/\<open>mod_def\<close>\<close> 
37660  1183 

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

1184 

61799  1185 
subsection \<open>Transferring goals from words to ints\<close> 
37660  1186 

65268  1187 
lemma word_ths: 
1188 
shows word_succ_p1: "word_succ a = a + 1" 

1189 
and word_pred_m1: "word_pred a = a  1" 

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

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

1192 
and word_mult_succ: "word_succ a * b = b + a * b" 

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

1193 
by (transfer, simp add: algebra_simps)+ 
37660  1194 

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

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

1196 
by simp 
37660  1197 

55818  1198 
lemma uint_word_ariths: 
1199 
fixes a b :: "'a::len0 word" 

70185  1200 
shows "uint (a + b) = (uint a + uint b) mod 2 ^ LENGTH('a::len0)" 
1201 
and "uint (a  b) = (uint a  uint b) mod 2 ^ LENGTH('a)" 

1202 
and "uint (a * b) = uint a * uint b mod 2 ^ LENGTH('a)" 

1203 
and "uint ( a) =  uint a mod 2 ^ LENGTH('a)" 

1204 
and "uint (word_succ a) = (uint a + 1) mod 2 ^ LENGTH('a)" 

1205 
and "uint (word_pred a) = (uint a  1) mod 2 ^ LENGTH('a)" 

1206 
and "uint (0 :: 'a word) = 0 mod 2 ^ LENGTH('a)" 

1207 
and "uint (1 :: 'a word) = 1 mod 2 ^ LENGTH('a)" 

55818  1208 
by (simp_all add: word_arith_wis [THEN trans [OF uint_cong int_word_uint]]) 
1209 

1210 
lemma uint_word_arith_bintrs: 

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

70185  1212 
shows "uint (a + b) = bintrunc (LENGTH('a)) (uint a + uint b)" 
1213 
and "uint (a  b) = bintrunc (LENGTH('a)) (uint a  uint b)" 

1214 
and "uint (a * b) = bintrunc (LENGTH('a)) (uint a * uint b)" 

1215 
and "uint ( a) = bintrunc (LENGTH('a)) ( uint a)" 

1216 
and "uint (word_succ a) = bintrunc (LENGTH('a)) (uint a + 1)" 

1217 
and "uint (word_pred a) = bintrunc (LENGTH('a)) (uint a  1)" 

1218 
and "uint (0 :: 'a word) = bintrunc (LENGTH('a)) 0" 

1219 
and "uint (1 :: 'a word) = bintrunc (LENGTH('a)) 1" 

55818  1220 
by (simp_all add: uint_word_ariths bintrunc_mod2p) 
1221 

1222 
lemma sint_word_ariths: 

1223 
fixes a b :: "'a::len word" 

70185  1224 
shows "sint (a + b) = sbintrunc (LENGTH('a)  1) (sint a + sint b)" 
1225 
and "sint (a  b) = sbintrunc (LENGTH('a)  1) (sint a  sint b)" 

1226 
and "sint (a * b) = sbintrunc (LENGTH('a)  1) (sint a * sint b)" 

1227 
and "sint ( a) = sbintrunc (LENGTH('a)  1) ( sint a)" 

1228 
and "sint (word_succ a) = sbintrunc (LENGTH('a)  1) (sint a + 1)" 

1229 
and "sint (word_pred a) = sbintrunc (LENGTH('a)  1) (sint a  1)" 

1230 
and "sint (0 :: 'a word) = sbintrunc (LENGTH('a)  1) 0" 

1231 
and "sint (1 :: 'a word) = sbintrunc (LENGTH('a)  1) 1" 

64593
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

1232 
apply (simp_all only: word_sbin.inverse_norm [symmetric]) 
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

1233 
apply (simp_all add: wi_hom_syms) 
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

1234 
apply transfer apply simp 
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

1235 
apply transfer apply simp 
50c715579715
reoriented congruence rules in nonexplosive direction
haftmann
parents:
64243
diff
changeset

1236 
done 
45604  1237 

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

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

37660  1240 

58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
58061
diff
changeset

1241 
lemma word_pred_0_n1: "word_pred 0 = word_of_int ( 1)" 
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset

1242 
unfolding word_pred_m1 by simp 
37660  1243 

1244 
lemma succ_pred_no [simp]: 

65268  1245 
"word_succ (numeral w) = numeral w + 1" 
1246 
"word_pred (numeral w) = numeral w  1" 

1247 
"word_succ ( numeral w) =  numeral w + 1" 

1248 
"word_pred ( numeral w) =  numeral w  1" 

1249 
by (simp_all add: word_succ_p1 word_pred_m1) 

1250 

1251 
lemma word_sp_01 [simp]: 

1252 
"word_succ ( 1) = 0 \<and> word_succ 0 = 1 \<and> word_pred 0 =  1 \<and> word_pred 1 = 0" 

1253 
by (simp_all add: word_succ_p1 word_pred_m1) 

37660  1254 

67408  1255 
\<comment> \<open>alternative approach to lifting arithmetic equalities\<close> 
65268  1256 
lemma word_of_int_Ex: "\<exists>y. x = word_of_int y" 
37660  1257 
by (rule_tac x="uint x" in exI) simp 
1258 

1259 

61799  1260 
subsection \<open>Order on fixedlength words\<close> 
37660  1261 

65328  1262 
lemma word_zero_le [simp]: "0 \<le> y" 
1263 
for y :: "'a::len0 word" 

37660  1264 
unfolding word_le_def by auto 
65268  1265 

65328  1266 
lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *) 
1267 
by (simp only: word_le_def word_pred_0_n1 word_uint.eq_norm m1mod2k) auto 

1268 

1269 
lemma word_n1_ge [simp]: "y \<le> 1" 

1270 
for y :: "'a::len0 word" 

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

37660  1272 

65268  1273 
lemmas word_not_simps [simp] = 
37660  1274 
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] 
1275 

65328  1276 
lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y" 
1277 
for y :: "'a::len0 word" 

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

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

1279 

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

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

65328  1282 
lemma word_sless_alt: "a <s b \<longleftrightarrow> sint a < sint b" 
1283 
by (auto simp add: word_sle_def word_sless_def less_le) 

1284 

1285 
lemma word_le_nat_alt: "a \<le> b \<longleftrightarrow> unat a \<le> unat b" 

37660 