author  haftmann 
Thu, 18 Jun 2020 09:07:30 +0000  
changeset 71955  a9f913d17d00 
parent 71954  13bb3f5cdc5b 
child 71957  3e162c63371a 
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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

22 
quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l\<close> 
71948
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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

25 
lift_definition uint :: \<open>'a::len word \<Rightarrow> int\<close> 
71948
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)" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

32 
for w :: "'a::len 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" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

36 
for w :: "'a::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

45 
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

51 
lemma split_word_all: "(\<And>x::'a::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

66 
definition unat :: "'a::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

92 
definition ucast :: "'a::len word \<Rightarrow> 'b::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

95 
instantiation word :: (len) size 
55816
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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

114 
definition source_size :: "('a::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

118 
definition target_size :: "('a \<Rightarrow> 'b::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

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

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

124 
definition is_down :: "('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool" 
65268  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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

127 
definition of_bl :: "bool list \<Rightarrow> 'a::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

130 
definition to_bl :: "'a::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

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

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

136 
definition word_int_case :: "(int \<Rightarrow> 'b) \<Rightarrow> 'a::len word \<Rightarrow> 'b" 
65268  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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

146 
definition Word :: "int \<Rightarrow> 'a::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

154 
instantiation word :: (len) equal 
55817
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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

168 
instantiation word :: ("{len, typerep}") random 
55817
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: 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

191 
"td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len))) 
70185  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 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

200 
"uint::'a::len word \<Rightarrow> int" 
65268  201 
word_of_int 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

202 
"uints (LENGTH('a::len))" 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

203 
"\<lambda>w. w mod 2 ^ LENGTH('a::len)" 
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: 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

210 
"td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len))) 
70185  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 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

216 
"uint::'a::len word \<Rightarrow> int" 
65268  217 
word_of_int 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

218 
"uints (LENGTH('a::len))" 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

219 
"bintrunc (LENGTH('a::len))" 
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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

225 
lift_definition word_succ :: "'a::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

228 
lift_definition word_pred :: "'a::len 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 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

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

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

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

235 

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

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

237 

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

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

240 

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

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

243 

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

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

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

246 

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

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

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

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

252 
by simp 

253 

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

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

256 
by simp 

37660  257 

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

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

260 

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

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

262 

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

265 
by transfer rule 

266 

267 
lemma word_mod_def [code]: 

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

269 
by transfer rule 

270 

70901  271 
quickcheck_generator word 
272 
constructors: 

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

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

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

276 

71950  277 
context 
278 
includes lifting_syntax 

279 
notes power_transfer [transfer_rule] 

280 
begin 

281 

282 
lemma power_transfer_word [transfer_rule]: 

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

284 
by transfer_prover 

285 

286 
end 

287 

288 

71951  289 

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

291 

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

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

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

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

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

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

299 
and word_0_wi: "0 = word_of_int 0" 

300 
and word_1_wi: "1 = word_of_int 1" 

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

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

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

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

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

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

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

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

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

309 

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

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

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

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

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

316 
and wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a  1)" 

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

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

318 

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

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

320 

46013  321 
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi 
46009  322 

323 
lemmas word_of_int_hom_syms = word_of_int_homs [symmetric] 

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

324 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

325 
instance word :: (len) comm_monoid_add .. 
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

326 

13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

327 
instance word :: (len) semiring_numeral .. 
71950  328 

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

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

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

335 

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

336 
lemma word_of_nat: "of_nat n = word_of_int (int n)" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

337 
by (induct n) (auto simp add : word_of_int_hom_syms) 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

338 

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

339 
lemma word_of_int: "of_int = word_of_int" 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

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

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

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

344 

71950  345 
context 
346 
includes lifting_syntax 

347 
notes 

348 
transfer_rule_of_bool [transfer_rule] 

349 
transfer_rule_numeral [transfer_rule] 

350 
transfer_rule_of_nat [transfer_rule] 

351 
transfer_rule_of_int [transfer_rule] 

352 
begin 

353 

354 
lemma [transfer_rule]: 

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

356 
by transfer_prover 

357 

358 
lemma [transfer_rule]: 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

359 
"((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) numeral numeral" 
71950  360 
by transfer_prover 
361 

362 
lemma [transfer_rule]: 

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

364 
by transfer_prover 

365 

366 
lemma [transfer_rule]: 

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

368 
proof  

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

370 
by transfer_prover 

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

372 
qed 

373 

374 
end 

375 

376 
lemma word_of_int_eq: 

377 
"word_of_int = of_int" 

378 
by (rule ext) (transfer, rule) 

379 

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

37660  382 

71950  383 
context 
384 
includes lifting_syntax 

385 
begin 

386 

387 
lemma [transfer_rule]: 

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

389 
proof  

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

391 
for k :: int 

392 
proof 

393 
assume ?P 

394 
then show ?Q 

395 
by auto 

396 
next 

397 
assume ?Q 

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

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

400 
by simp 

401 
then show ?P 

402 
by simp 

403 
qed 

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

405 
transfer_prover 

406 
qed 

407 

408 
end 

409 

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

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

413 
proof transfer 

414 
fix k l :: int 

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

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

417 
by (simp add: take_bit_eq_mod) 

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

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

420 
by (simp add: div_mult_mod_eq) 

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

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

423 
by (simp add: mod_add_left_eq) 

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

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

426 
by (simp add: mod_mult_right_eq) 

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

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

429 
by (simp add: mod_simps) 

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

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

432 
by simp 

433 
qed 

434 
qed 

435 

436 
instance word :: (len) semiring_parity 

437 
proof 

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

439 
by transfer simp 

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

441 
for a :: "'a word" 

442 
by transfer (simp_all add: mod_2_eq_odd take_bit_Suc) 

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

444 
for a :: "'a word" 

445 
by transfer (simp_all add: mod_2_eq_odd take_bit_Suc) 

446 
qed 

447 

71953  448 
lemma exp_eq_zero_iff: 
449 
\<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close> 

450 
by transfer simp 

451 

45547  452 

61799  453 
subsection \<open>Ordering\<close> 
45547  454 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

455 
instantiation word :: (len) linorder 
45547  456 
begin 
457 

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

460 
by simp 

461 

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

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

464 
by simp 

37660  465 

45547  466 
instance 
71950  467 
by (standard; transfer) auto 
45547  468 

469 
end 

470 

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

473 
by transfer rule 

474 

475 
lemma word_less_def [code]: 

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

477 
by transfer rule 

478 

71951  479 
lemma word_greater_zero_iff: 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

480 
\<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len word\<close> 
71951  481 
by transfer (simp add: less_le) 
482 

483 
lemma of_nat_word_eq_iff: 

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

485 
by transfer (simp add: take_bit_of_nat) 

486 

487 
lemma of_nat_word_less_eq_iff: 

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

489 
by transfer (simp add: take_bit_of_nat) 

490 

491 
lemma of_nat_word_less_iff: 

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

493 
by transfer (simp add: take_bit_of_nat) 

494 

495 
lemma of_nat_word_eq_0_iff: 

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

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

498 

499 
lemma of_int_word_eq_iff: 

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

501 
by transfer rule 

502 

503 
lemma of_int_word_less_eq_iff: 

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

505 
by transfer rule 

506 

507 
lemma of_int_word_less_iff: 

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

509 
by transfer rule 

510 

511 
lemma of_int_word_eq_0_iff: 

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

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

514 

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

517 

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

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

37660  520 

521 

61799  522 
subsection \<open>Bitwise operations\<close> 
37660  523 

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

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

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

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

529 
proof  

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

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

532 
by simp 

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

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

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

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

537 
by (simp add: l) 

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

539 
proof (induction n rule: nat_bit_induct) 

540 
case zero 

541 
show ?case 

542 
by simp (rule word_zero) 

543 
next 

544 
case (even n) 

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

546 
by simp 

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

548 
by simp 

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

550 
by (auto simp add: word_greater_zero_iff of_nat_word_eq_0_iff l) 

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

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

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

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

555 
by (rule word_even) 

556 
then show ?case 

557 
by simp 

558 
next 

559 
case (odd n) 

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

561 
by simp 

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

563 
by simp 

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

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

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

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

568 
by (rule word_odd) 

569 
then show ?case 

570 
by simp 

571 
qed 

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

573 
by transfer simp 

574 
ultimately show ?thesis 

575 
by (simp add: n_def unat_def) 

576 
qed 

577 

578 
lemma bit_word_half_eq: 

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

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

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

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

583 
case False 

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

585 
by auto 

586 
with False that show ?thesis 

587 
by transfer (simp add: eq_iff) 

588 
next 

589 
case True 

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

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

592 
show ?thesis proof (cases b) 

593 
case False 

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

595 
using that proof transfer 

596 
fix k :: int 

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

598 
by simp 

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

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

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

602 
by (simp add: take_bit_eq_mod divmod_digit_0) 

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

604 
by (simp add: take_bit_eq_mod) 

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

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

607 
by simp 

608 
qed 

609 
ultimately show ?thesis 

610 
by simp 

611 
next 

612 
case True 

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

614 
using that proof transfer 

615 
fix k :: int 

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

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

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

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

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

621 
by (simp add: take_bit_eq_mod divmod_digit_0) 

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

623 
by (simp add: take_bit_eq_mod) 

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

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

626 
by (auto simp add: take_bit_Suc) 

627 
qed 

628 
ultimately show ?thesis 

629 
by simp 

630 
qed 

631 
qed 

632 

633 
lemma even_mult_exp_div_word_iff: 

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

635 
m \<le> n \<and> 

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

637 
by transfer 

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

639 
simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int) 

640 

641 
instance word :: (len) semiring_bits 

642 
proof 

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

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

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

646 
proof (induction a rule: word_bit_induct) 

647 
case zero 

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

649 
by transfer simp 

650 
with stable [of 0] show ?case 

651 
by simp 

652 
next 

653 
case (even a) 

654 
with rec [of a False] show ?case 

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

656 
next 

657 
case (odd a) 

658 
with rec [of a True] show ?case 

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

660 
qed 

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

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

663 
by transfer simp 

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

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

666 
by transfer simp 

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

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

669 
apply transfer 

670 
apply (simp add: take_bit_eq_mod) 

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

672 
apply simp_all 

673 
apply (metis le_less mod_by_0 pos_mod_conj zero_less_numeral zero_less_power) 

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

675 
proof  

676 
fix aa :: int and ba :: int 

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

678 
by (metis le_less take_bit_eq_mod take_bit_nonnegative) 

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

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

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

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

683 
qed 

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

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

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

71953  687 
using that by transfer 
688 
(auto dest: le_Suc_ex simp add: mod_2_eq_odd take_bit_Suc elim!: evenE) 

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

691 
by transfer (simp, simp add: exp_div_exp_eq) 

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

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

694 
apply transfer 

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

696 
apply (simp add: drop_bit_take_bit) 

697 
done 

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

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

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

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

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

703 
using that apply transfer 

704 
apply (auto simp flip: take_bit_eq_mod) 

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

706 
done 

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

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

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

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

711 
for m n :: nat 

712 
by transfer (auto simp add: take_bit_of_mask even_mask_div_iff) 

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

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

715 
proof transfer 

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

717 
n < m 

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

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

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

721 
by (auto simp flip: take_bit_eq_mod drop_bit_eq_div push_bit_eq_mult 

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

723 
qed 

724 
qed 

725 

726 
context 

727 
includes lifting_syntax 

728 
begin 

729 

730 
lemma transfer_rule_bit_word [transfer_rule]: 

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

732 
proof  

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

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

735 
by (unfold bit_def) transfer_prover 

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

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

738 
finally show ?thesis . 

739 
qed 

740 

741 
end 

742 

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

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

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

745 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

762 

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

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

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

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

766 

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

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

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

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

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

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

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

773 

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

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

775 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

776 
definition shiftl1 :: "'a::len word \<Rightarrow> 'a word" 
70191  777 
where "shiftl1 w = word_of_int (uint w BIT False)" 
778 

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

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

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

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

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

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

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

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

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

787 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

788 
definition shiftr1 :: "'a::len word \<Rightarrow> 'a word" 
70191  789 
\<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close> 
790 
where "shiftr1 w = word_of_int (bin_rest (uint w))" 

791 

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

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

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

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

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

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

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

798 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

799 
instantiation word :: (len) bit_operations 
37660  800 
begin 
801 

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

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

804 

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

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

807 

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

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

810 

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

812 
by (metis bin_trunc_xor) 
37660  813 

65268  814 
definition word_test_bit_def: "test_bit a = bin_nth (uint a)" 
815 

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

817 

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

37660  819 

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

65268  822 
definition shiftl_def: "w << n = (shiftl1 ^^ n) w" 
823 

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

37660  825 

826 
instance .. 

827 

828 
end 

829 

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

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

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

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

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

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

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

836 

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

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

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

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

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

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

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

843 

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

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

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

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

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

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

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

850 

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

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

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

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

854 

71955  855 
lemma [code]: 
856 
\<open>push_bit n w = w << n\<close> for w :: \<open>'a::len word\<close> 

857 
by (simp add: shiftl_word_eq) 

858 

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

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

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

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

862 

71955  863 
lemma [code]: 
864 
\<open>drop_bit n w = w >> n\<close> for w :: \<open>'a::len word\<close> 

865 
by (simp add: shiftr_word_eq) 

866 

867 
lemma [code_abbrev]: 

868 
\<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close> 

869 
by (fact push_bit_of_1) 

870 

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

873 
by (simp add: msb_word_def sint_uint) 

874 

65268  875 
lemma [code]: 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

876 
shows word_not_def: "NOT (a::'a::len word) = word_of_int (NOT (uint a))" 
65268  877 
and word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)" 
878 
and word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)" 

879 
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

880 
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

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

882 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

883 
definition setBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word" 
65268  884 
where "setBit w n = set_bit w n True" 
885 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

886 
definition clearBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word" 
65268  887 
where "clearBit w n = set_bit w n False" 
37660  888 

889 

61799  890 
subsection \<open>Shift operations\<close> 
37660  891 

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

894 

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

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

897 

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

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

900 

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

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

903 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

904 
definition revcast :: "'a::len word \<Rightarrow> 'b::len word" 
70185  905 
where "revcast w = of_bl (takefill False (LENGTH('b)) (to_bl w))" 
65268  906 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

907 
definition slice1 :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word" 
65268  908 
where "slice1 n w = of_bl (takefill False n (to_bl w))" 
909 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

910 
definition slice :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word" 
65268  911 
where "slice n w = slice1 (size w  n) w" 
37660  912 

913 

61799  914 
subsection \<open>Rotation\<close> 
37660  915 

65268  916 
definition rotater1 :: "'a list \<Rightarrow> 'a list" 
917 
where "rotater1 ys = 

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

919 

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

921 
where "rotater n = rotater1 ^^ n" 

922 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

923 
definition word_rotr :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word" 
65268  924 
where "word_rotr n w = of_bl (rotater n (to_bl w))" 
925 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

926 
definition word_rotl :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word" 
65268  927 
where "word_rotl n w = of_bl (rotate n (to_bl w))" 
928 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

929 
definition word_roti :: "int \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word" 
65268  930 
where "word_roti i w = 
931 
(if i \<ge> 0 then word_rotr (nat i) w else word_rotl (nat ( i)) w)" 

37660  932 

933 

61799  934 
subsection \<open>Split and cat operations\<close> 
37660  935 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

936 
definition word_cat :: "'a::len word \<Rightarrow> 'b::len word \<Rightarrow> 'c::len word" 
70185  937 
where "word_cat a b = word_of_int (bin_cat (uint a) (LENGTH('b)) (uint b))" 
65268  938 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

939 
definition word_split :: "'a::len word \<Rightarrow> 'b::len word \<times> 'c::len word" 
65268  940 
where "word_split a = 
70185  941 
(case bin_split (LENGTH('c)) (uint a) of 
65268  942 
(u, v) \<Rightarrow> (word_of_int u, word_of_int v))" 
943 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

944 
definition word_rcat :: "'a::len word list \<Rightarrow> 'b::len word" 
70185  945 
where "word_rcat ws = word_of_int (bin_rcat (LENGTH('a)) (map uint ws))" 
65268  946 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

947 
definition word_rsplit :: "'a::len word \<Rightarrow> 'b::len word list" 
70185  948 
where "word_rsplit w = map word_of_int (bin_rsplit (LENGTH('b)) (LENGTH('a), uint w))" 
65268  949 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

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

951 
\<comment> \<open>Largest representable machine integer.\<close> 
71946  952 
where "max_word \<equiv>  1" 
37660  953 

954 

61799  955 
subsection \<open>Theorems about typedefs\<close> 
46010  956 

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

70185  960 
lemma uint_sint: "uint w = bintrunc (LENGTH('a)) (sint w)" 
65328  961 
for w :: "'a::len word" 
65268  962 
by (auto simp: sint_uint bintrunc_sbintrunc_le) 
963 

70185  964 
lemma bintr_uint: "LENGTH('a) \<le> n \<Longrightarrow> bintrunc n (uint w) = uint w" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

965 
for w :: "'a::len word" 
65268  966 
apply (subst word_ubin.norm_Rep [symmetric]) 
37660  967 
apply (simp only: bintrunc_bintrunc_min word_size) 
54863
82acc20ded73
prefer more canonical names for lemmas on min/max
haftmann
parents:
54854
diff
changeset

968 
apply (simp add: min.absorb2) 
37660  969 
done 
970 

46057  971 
lemma wi_bintr: 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

972 
"LENGTH('a::len) \<le> n \<Longrightarrow> 
46057  973 
word_of_int (bintrunc n w) = (word_of_int w :: 'a word)" 
65268  974 
by (auto simp: word_ubin.norm_eq_iff [symmetric] min.absorb1) 
975 

976 
lemma td_ext_sbin: 

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

37660  979 
apply (unfold td_ext_def' sint_uint) 
980 
apply (simp add : word_ubin.eq_norm) 

70185  981 
apply (cases "LENGTH('a)") 
37660  982 
apply (auto simp add : sints_def) 
983 
apply (rule sym [THEN trans]) 

65268  984 
apply (rule word_ubin.Abs_norm) 
37660  985 
apply (simp only: bintrunc_sbintrunc) 
986 
apply (drule sym) 

987 
apply simp 

988 
done 

989 

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

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

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

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

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

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

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

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

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

1001 
\<close> 

37660  1002 
interpretation word_sint: 
65268  1003 
td_ext 
1004 
"sint ::'a::len word \<Rightarrow> int" 

1005 
word_of_int 

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

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

37660  1009 
by (rule td_ext_sint) 
1010 

1011 
interpretation word_sbin: 

65268  1012 
td_ext 
1013 
"sint ::'a::len word \<Rightarrow> int" 

1014 
word_of_int 

70185  1015 
"sints (LENGTH('a::len))" 
1016 
"sbintrunc (LENGTH('a::len)  1)" 

37660  1017 
by (rule td_ext_sbin) 
1018 

45604  1019 
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] 
37660  1020 

1021 
lemmas td_sint = word_sint.td 

1022 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1023 
lemma to_bl_def': "(to_bl :: 'a::len word \<Rightarrow> bool list) = bin_to_bl (LENGTH('a)) \<circ> uint" 
44762  1024 
by (auto simp: to_bl_def) 
37660  1025 

65268  1026 
lemmas word_reverse_no_def [simp] = 
1027 
word_reverse_def [of "numeral w"] for w 

37660  1028 

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

1031 

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

1033 
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

1034 

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

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

1036 

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

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

1039 

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

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

1041 

45805  1042 
lemma uint_bintrunc [simp]: 
65268  1043 
"uint (numeral bin :: 'a word) = 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1044 
bintrunc (LENGTH('a::len)) (numeral bin)" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

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

1046 

65268  1047 
lemma uint_bintrunc_neg [simp]: 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

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

1049 
by (simp only: word_neg_numeral_alt word_ubin.eq_norm) 
37660  1050 

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

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

1054 

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

1057 
by (simp only: word_neg_numeral_alt word_sbin.eq_norm) 
37660  1058 

45805  1059 
lemma unat_bintrunc [simp]: 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1060 
"unat (numeral bin :: 'a::len word) = nat (bintrunc (LENGTH('a)) (numeral bin))" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

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

1062 

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

1063 
lemma unat_bintrunc_neg [simp]: 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1064 
"unat ( numeral bin :: 'a::len word) = nat (bintrunc (LENGTH('a)) ( numeral bin))" 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1065 
by (simp only: unat_def uint_bintrunc_neg) 
37660  1066 

65328  1067 
lemma size_0_eq: "size w = 0 \<Longrightarrow> v = w" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1068 
for v w :: "'a::len word" 
37660  1069 
apply (unfold word_size) 
1070 
apply (rule word_uint.Rep_eqD) 

1071 
apply (rule box_equals) 

1072 
defer 

1073 
apply (rule word_ubin.norm_Rep)+ 

1074 
apply simp 

1075 
done 

1076 

65268  1077 
lemma uint_ge_0 [iff]: "0 \<le> uint x" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1078 
for x :: "'a::len word" 
45805  1079 
using word_uint.Rep [of x] by (simp add: uints_num) 
1080 

70185  1081 
lemma uint_lt2p [iff]: "uint x < 2 ^ LENGTH('a)" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1082 
for x :: "'a::len word" 
45805  1083 
using word_uint.Rep [of x] by (simp add: uints_num) 
1084 

71946  1085 
lemma word_exp_length_eq_0 [simp]: 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1086 
\<open>(2 :: 'a::len word) ^ LENGTH('a) = 0\<close> 
71946  1087 
by transfer (simp add: bintrunc_mod2p) 
1088 

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

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

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

1098 
by (simp add: sign_Pls_ge_0) 
37660  1099 

70185  1100 
lemma uint_m2p_neg: "uint x  2 ^ LENGTH('a) < 0" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1101 
for x :: "'a::len word" 
45805  1102 
by (simp only: diff_less_0_iff_less uint_lt2p) 
1103 

70185  1104 
lemma uint_m2p_not_non_neg: "\<not> 0 \<le> uint x  2 ^ LENGTH('a)" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1105 
for x :: "'a::len word" 
45805  1106 
by (simp only: not_le uint_m2p_neg) 
37660  1107 

70185  1108 
lemma lt2p_lem: "LENGTH('a) \<le> n \<Longrightarrow> uint w < 2 ^ n" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1109 
for w :: "'a::len word" 
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

1110 
by (metis bintr_uint bintrunc_mod2p int_mod_lem zless2p) 
37660  1111 

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

1113 
by (fact uint_ge_0 [THEN leD, THEN antisym_conv1]) 
37660  1114 

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

1115 
lemma uint_nat: "uint w = int (unat w)" 
65268  1116 
by (auto simp: unat_def) 
1117 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1118 
lemma uint_numeral: "uint (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)" 
65268  1119 
by (simp only: word_numeral_alt int_word_uint) 
1120 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1121 
lemma uint_neg_numeral: "uint ( numeral b :: 'a::len word) =  numeral b mod 2 ^ LENGTH('a)" 
65268  1122 
by (simp only: word_neg_numeral_alt int_word_uint) 
1123 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

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

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

1128 
apply (rule zero_le_numeral) 
37660  1129 
apply (simp_all add: nat_power_eq) 
1130 
done 

1131 

65268  1132 
lemma sint_numeral: 
1133 
"sint (numeral b :: 'a::len word) = 

1134 
(numeral b + 

70185  1135 
2 ^ (LENGTH('a)  1)) mod 2 ^ LENGTH('a)  
1136 
2 ^ (LENGTH('a)  1)" 

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

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

1138 

65268  1139 
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0" 
45958  1140 
unfolding word_0_wi .. 
1141 

65268  1142 
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1" 
45958  1143 
unfolding word_1_wi .. 
1144 

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

1145 
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

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

1147 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1148 
lemma word_of_int_numeral [simp] : "(word_of_int (numeral bin) :: 'a::len word) = numeral bin" 
65268  1149 
by (simp only: word_numeral_alt) 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset

1150 

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

1151 
lemma word_of_int_neg_numeral [simp]: 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1152 
"(word_of_int ( numeral bin) :: 'a::len word) =  numeral bin" 
65268  1153 
by (simp only: word_numeral_alt wi_hom_syms) 
1154 

1155 
lemma word_int_case_wi: 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1156 
"word_int_case f (word_of_int i :: 'b word) = f (i mod 2 ^ LENGTH('b::len))" 
65268  1157 
by (simp add: word_int_case_def word_uint.eq_norm) 
1158 

1159 
lemma word_int_split: 

1160 
"P (word_int_case f x) = 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1161 
(\<forall>i. x = (word_of_int i :: 'b::len word) \<and> 0 \<le> i \<and> i < 2 ^ LENGTH('b) \<longrightarrow> P (f i))" 
71942  1162 
by (auto simp: word_int_case_def word_uint.eq_norm) 
65268  1163 

1164 
lemma word_int_split_asm: 

1165 
"P (word_int_case f x) = 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1166 
(\<nexists>n. x = (word_of_int n :: 'b::len word) \<and> 0 \<le> n \<and> n < 2 ^ LENGTH('b::len) \<and> \<not> P (f n))" 
71942  1167 
by (auto simp: word_int_case_def word_uint.eq_norm) 
45805  1168 

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

37660  1171 

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

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

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

45805  1180 
unfolding word_size by (rule less_le_trans [OF sint_lt]) 
1181 

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

45805  1184 
unfolding word_size by (rule order_trans [OF _ sint_ge]) 
37660  1185 

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

1186 

61799  1187 
subsection \<open>Testing bits\<close> 
46010  1188 

65268  1189 
lemma test_bit_eq_iff: "test_bit u = test_bit v \<longleftrightarrow> u = v" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1190 
for u v :: "'a::len word" 
37660  1191 
unfolding word_test_bit_def by (simp add: bin_nth_eq_iff) 
1192 

65268  1193 
lemma test_bit_size [rule_format] : "w !! n \<longrightarrow> n < size w" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1194 
for w :: "'a::len word" 
37660  1195 
apply (unfold word_test_bit_def) 
1196 
apply (subst word_ubin.norm_Rep [symmetric]) 

1197 
apply (simp only: nth_bintr word_size) 

1198 
apply fast 

1199 
done 

1200 

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

1201 
lemma word_eq_iff: "x = y \<longleftrightarrow> (\<forall>n<LENGTH('a). x !! n = y !! n)" (is \<open>?P \<longleftrightarrow> ?Q\<close>) 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1202 
for x y :: "'a::len word" 
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset

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

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

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

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

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

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

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

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

1212 
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

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

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

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

1216 
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

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

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

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

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

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

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

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

1224 
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

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

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

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

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

1229 
qed 
46021  1230 

65268  1231 
lemma word_eqI: "(\<And>n. n < size u \<longrightarrow> u !! n = v !! n) \<Longrightarrow> u = v" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1232 
for u :: "'a::len word" 
46021  1233 
by (simp add: word_size word_eq_iff) 
37660  1234 

65268  1235 
lemma word_eqD: "u = v \<Longrightarrow> u !! x = v !! x" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1236 
for u v :: "'a::len word" 
45805  1237 
by simp 
37660  1238 

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

37660  1241 

1242 
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] 

1243 

70185  1244 
lemma bin_nth_uint_imp: "bin_nth (uint w) n \<Longrightarrow> n < LENGTH('a)" 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1245 
for w :: "'a::len word" 
37660  1246 
apply (rule nth_bintr [THEN iffD1, THEN conjunct1]) 
1247 
apply (subst word_ubin.norm_Rep) 

1248 
apply assumption 

1249 
done 

1250 

46057  1251 
lemma bin_nth_sint: 
70185  1252 
"LENGTH('a) \<le> n \<Longrightarrow> 
1253 
bin_nth (sint w) n = bin_nth (sint w) (LENGTH('a)  1)" 

65268  1254 
for w :: "'a::len word" 
37660  1255 
apply (subst word_sbin.norm_Rep [symmetric]) 
46057  1256 
apply (auto simp add: nth_sbintr) 
37660  1257 
done 
1258 

67408  1259 
\<comment> \<open>type definitions theorem for in terms of equivalent bool list\<close> 
65268  1260 
lemma td_bl: 
1261 
"type_definition 

71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1262 
(to_bl :: 'a::len word \<Rightarrow> bool list) 
65268  1263 
of_bl 
70185  1264 
{bl. length bl = LENGTH('a)}" 
37660  1265 
apply (unfold type_definition_def of_bl_def to_bl_def) 
1266 
apply (simp add: word_ubin.eq_norm) 

1267 
apply safe 

1268 
apply (drule sym) 

1269 
apply simp 

1270 
done 

1271 

1272 
interpretation word_bl: 

65268  1273 
type_definition 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

1274 
"to_bl :: 'a::len word \<Rightarrow> bool list" 
65268  1275 
of_bl 
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset

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

1277 
by (fact td_bl) 
37660  1278 

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

1279 
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

1280 

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

1281 
lemma word_size_bl: "size w = size (to_bl w)" 
65268  1282 
by (auto simp: word_size) 
1283 

1284 
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

1285 
by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq]) 
37660  1286 
