author  haftmann 
Sat, 01 Mar 2014 17:08:39 +0100  
changeset 55818  d8b2f50705d0 
parent 55817  0bc0217387a5 
child 55833  6fe16c8a6474 
permissions  rwrr 
29628  1 
(* Title: HOL/Word/Word.thy 
46124  2 
Author: Jeremy Dawson and Gerwin Klein, NICTA 
24333  3 
*) 
4 

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

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

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

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

10 
"~~/src/HOL/Library/Boolean_Algebra" 
54854
3324a0078636
prefer "Bits" as theory name for abstract bit operations, similar to "Orderings", "Lattices", "Groups" etc.
haftmann
parents:
54849
diff
changeset

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

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

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

14 
Word_Miscellaneous 
37660  15 
begin 
16 

54743  17 
text {* See @{file "Examples/WordExamples.thy"} for examples. *} 
37660  18 

19 
subsection {* Type definition *} 

20 

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

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

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

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

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

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

27 

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

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

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

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

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

32 

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

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

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

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

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

37 

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

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

39 
"a = b \<longleftrightarrow> uint a = uint b" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

41 

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

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

43 
"uint a = uint b \<Longrightarrow> a = b" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

45 

54848  46 
definition word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word" 
47 
where 

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

48 
 {* representation of words using unsigned or signed bins, 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

49 
only difference in these is the type class *} 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

51 

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

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

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

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

55 

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

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

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

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

59 

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

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

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

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

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

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

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

66 
find_theorems word_of_int uint 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

67 
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

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

69 

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

70 

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

71 
subsection {* Type conversions and casting *} 
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 sint :: "'a::len word \<Rightarrow> int" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

75 
 {* treats the mostsignificantbit as a sign bit *} 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

76 
sint_uint: "sint w = sbintrunc (len_of TYPE ('a)  1) (uint w)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

77 

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

78 
definition unat :: "'a::len0 word \<Rightarrow> nat" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

80 
"unat w = nat (uint w)" 
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 uints :: "nat \<Rightarrow> int set" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

84 
 "the sets of integers representing the words" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

85 
"uints n = range (bintrunc n)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

86 

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

87 
definition sints :: "nat \<Rightarrow> int set" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

89 
"sints n = range (sbintrunc (n  1))" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

90 

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

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

92 
"uints n = {i. 0 \<le> i \<and> i < 2 ^ n}" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

94 

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

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

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

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

98 

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

99 
definition unats :: "nat \<Rightarrow> nat set" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

101 
"unats n = {i. i < 2 ^ n}" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

102 

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

103 
definition norm_sint :: "nat \<Rightarrow> int \<Rightarrow> int" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

105 
"norm_sint n w = (w + 2 ^ (n  1)) mod 2 ^ n  2 ^ (n  1)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

106 

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

107 
definition scast :: "'a::len word \<Rightarrow> 'b::len word" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

109 
 "cast a word to a different length" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

110 
"scast w = word_of_int (sint w)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

111 

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

112 
definition ucast :: "'a::len0 word \<Rightarrow> 'b::len0 word" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

114 
"ucast w = word_of_int (uint w)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

115 

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

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

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

118 

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

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

120 
word_size: "size (w :: 'a word) = len_of TYPE('a)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

121 

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

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

123 

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

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

125 

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

126 
lemma word_size_gt_0 [iff]: 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

127 
"0 < size (w::'a::len word)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

129 

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

130 
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

131 

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

132 
lemma lens_not_0 [iff]: 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

133 
shows "size (w::'a::len word) \<noteq> 0" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

134 
and "len_of TYPE('a::len) \<noteq> 0" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

136 

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

137 
definition source_size :: "('a::len0 word \<Rightarrow> 'b) \<Rightarrow> nat" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

139 
 "whether a cast (or other) function is to a longer or shorter length" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

140 
"source_size c = (let arb = undefined ; x = c arb in size arb)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

141 

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

142 
definition target_size :: "('a \<Rightarrow> 'b::len0 word) \<Rightarrow> nat" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

144 
"target_size c = size (c undefined)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

145 

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

146 
definition is_up :: "('a::len0 word \<Rightarrow> 'b::len0 word) \<Rightarrow> bool" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

148 
"is_up c \<longleftrightarrow> source_size c \<le> target_size c" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

149 

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

150 
definition is_down :: "('a :: len0 word \<Rightarrow> 'b :: len0 word) \<Rightarrow> bool" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

152 
"is_down c \<longleftrightarrow> target_size c \<le> source_size c" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

153 

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

154 
definition of_bl :: "bool list \<Rightarrow> 'a::len0 word" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

156 
"of_bl bl = word_of_int (bl_to_bin bl)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

157 

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

158 
definition to_bl :: "'a::len0 word \<Rightarrow> bool list" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

160 
"to_bl w = bin_to_bl (len_of TYPE ('a)) (uint w)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

161 

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

162 
definition word_reverse :: "'a::len0 word \<Rightarrow> 'a word" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

164 
"word_reverse w = of_bl (rev (to_bl w))" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

165 

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

166 
definition word_int_case :: "(int \<Rightarrow> 'b) \<Rightarrow> 'a::len0 word \<Rightarrow> 'b" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

168 
"word_int_case f w = f (uint w)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

169 

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

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

171 
"case x of XCONST of_int y => b" == "CONST word_int_case (%y. b) x" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

172 
"case x of (XCONST of_int :: 'a) y => b" => "CONST word_int_case (%y. b) x" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

173 

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

174 

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

175 
subsection {* Correspondence relation for theorem transfer *} 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

176 

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

177 
definition cr_word :: "int \<Rightarrow> 'a::len0 word \<Rightarrow> bool" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

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

179 
"cr_word = (\<lambda>x y. word_of_int x = y)" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

180 

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

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

182 
"Quotient (\<lambda>x y. bintrunc (len_of TYPE('a)) x = bintrunc (len_of TYPE('a)) y) 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

183 
word_of_int uint (cr_word :: _ \<Rightarrow> 'a::len0 word \<Rightarrow> bool)" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

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

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

186 

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

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

188 
"reflp (\<lambda>x y. bintrunc (len_of TYPE('a::len0)) x = bintrunc (len_of TYPE('a)) y)" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

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

190 

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

191 
setup_lifting (no_code) Quotient_word reflp_word 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

192 

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

193 
text {* TODO: The next lemma could be generated automatically. *} 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

194 

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

195 
lemma uint_transfer [transfer_rule]: 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

196 
"(fun_rel pcr_word op =) (bintrunc (len_of TYPE('a))) 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

197 
(uint :: 'a::len0 word \<Rightarrow> int)" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

198 
unfolding fun_rel_def word.pcr_cr_eq cr_word_def 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

199 
by (simp add: no_bintr_alt1 uint_word_of_int) 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

200 

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

201 

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

202 
subsection {* Basic code generation setup *} 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

203 

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

204 
definition Word :: "int \<Rightarrow> 'a::len0 word" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

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

206 
[code_post]: "Word = word_of_int" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

207 

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

208 
lemma [code abstype]: 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

209 
"Word (uint w) = w" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

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

211 

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

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

213 

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

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

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

216 

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

217 
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

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

219 
"equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

220 

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

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

222 
qed (simp add: equal equal_word_def word_uint_eq_iff) 
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset

223 

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

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

225 

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

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

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

228 

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

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

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

231 

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

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

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

234 
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

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

236 

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

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

238 

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

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

240 

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

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

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

243 

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

244 

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

245 
subsection {* Typedefinition locale instantiations *} 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

246 

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

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

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

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

250 

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

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

252 
"td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (len_of TYPE('a::len0))) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

253 
(\<lambda>w::int. w mod 2 ^ len_of TYPE('a))" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

254 
apply (unfold td_ext_def') 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

255 
apply (simp add: uints_num word_of_int_def bintrunc_mod2p) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

256 
apply (simp add: uint_mod_same uint_0 uint_lt 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

257 
word.uint_inverse word.Abs_word_inverse int_mod_lem) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

259 

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

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

261 
td_ext "uint::'a::len0 word \<Rightarrow> int" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

263 
"uints (len_of TYPE('a::len0))" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

264 
"\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

266 

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

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

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

269 

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

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

271 
"td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (len_of TYPE('a::len0))) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

272 
(bintrunc (len_of TYPE('a)))" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

274 

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

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

276 
td_ext "uint::'a::len0 word \<Rightarrow> int" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

278 
"uints (len_of TYPE('a::len0))" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

279 
"bintrunc (len_of TYPE('a::len0))" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

281 

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

282 

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

283 
subsection {* Arithmetic operations *} 
37660  284 

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

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

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

287 

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

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

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

290 

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

291 
instantiation word :: (len0) "{neg_numeral, Divides.div, comm_monoid_mult, comm_ring}" 
37660  292 
begin 
293 

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

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

295 

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

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

297 

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

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

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

300 

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

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

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

303 

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

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

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

306 

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

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

308 
by (metis bintr_ariths(4)) 
37660  309 

310 
definition 

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

312 

313 
definition 

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

315 

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

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

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

318 

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

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

320 

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

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

322 

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

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

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

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

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

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

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

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

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

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

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

334 
by simp_all 
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 
lemmas arths = 
45604  337 
bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1], folded word_ubin.eq_norm] 
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

338 

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

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

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

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

343 
wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and 
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset

344 
wi_hom_neg: " word_of_int a = word_of_int ( a)" and 
46000  345 
wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" and 
346 
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

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

348 

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

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

350 

46013  351 
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi 
46009  352 

353 
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

354 

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

355 
instance word :: (len) comm_ring_1 
45810  356 
proof 
357 
have "0 < len_of TYPE('a)" by (rule len_gt_0) 

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

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

361 

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

362 
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

363 
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

364 

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

365 
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

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

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

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

370 

54848  371 
definition udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50) 
372 
where 

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

373 
"a udvd b = (EX n>=0. uint b = n * uint a)" 
37660  374 

45547  375 

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

376 
subsection {* Ordering *} 
45547  377 

378 
instantiation word :: (len0) linorder 

379 
begin 

380 

37660  381 
definition 
382 
word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b" 

383 

384 
definition 

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

385 
word_less_def: "a < b \<longleftrightarrow> uint a < uint b" 
37660  386 

45547  387 
instance 
388 
by default (auto simp: word_less_def word_le_def) 

389 

390 
end 

391 

54848  392 
definition word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50) 
393 
where 

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

394 
"a <=s b = (sint a <= sint b)" 
37660  395 

54848  396 
definition word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50) 
397 
where 

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

398 
"(x <s y) = (x <=s y & x ~= y)" 
37660  399 

400 

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

401 
subsection {* Bitwise operations *} 
37660  402 

403 
instantiation word :: (len0) bits 

404 
begin 

405 

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

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

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

408 

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

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

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

411 

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

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

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

414 

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

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

416 
by (metis bin_trunc_xor) 
37660  417 

418 
definition 

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

420 

421 
definition 

422 
word_set_bit_def: "set_bit a n x = 

54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54743
diff
changeset

423 
word_of_int (bin_sc n x (uint a))" 
37660  424 

425 
definition 

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

427 

428 
definition 

54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54743
diff
changeset

429 
word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a)" 
37660  430 

54848  431 
definition shiftl1 :: "'a word \<Rightarrow> 'a word" 
432 
where 

54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54743
diff
changeset

433 
"shiftl1 w = word_of_int (uint w BIT False)" 
37660  434 

54848  435 
definition shiftr1 :: "'a word \<Rightarrow> 'a word" 
436 
where 

37660  437 
 "shift right as unsigned or as signed, ie logical or arithmetic" 
438 
"shiftr1 w = word_of_int (bin_rest (uint w))" 

439 

440 
definition 

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

442 

443 
definition 

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

445 

446 
instance .. 

447 

448 
end 

449 

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

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

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

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

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

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

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

457 

37660  458 
instantiation word :: (len) bitss 
459 
begin 

460 

461 
definition 

462 
word_msb_def: 

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

463 
"msb a \<longleftrightarrow> bin_sign (sint a) = 1" 
37660  464 

465 
instance .. 

466 

467 
end 

468 

54848  469 
definition setBit :: "'a :: len0 word => nat => 'a word" 
470 
where 

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

471 
"setBit w n = set_bit w n True" 
37660  472 

54848  473 
definition clearBit :: "'a :: len0 word => nat => 'a word" 
474 
where 

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

475 
"clearBit w n = set_bit w n False" 
37660  476 

477 

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

478 
subsection {* Shift operations *} 
37660  479 

54848  480 
definition sshiftr1 :: "'a :: len word => 'a word" 
481 
where 

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

482 
"sshiftr1 w = word_of_int (bin_rest (sint w))" 
37660  483 

54848  484 
definition bshiftr1 :: "bool => 'a :: len word => 'a word" 
485 
where 

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

486 
"bshiftr1 b w = of_bl (b # butlast (to_bl w))" 
37660  487 

54848  488 
definition sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55) 
489 
where 

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

490 
"w >>> n = (sshiftr1 ^^ n) w" 
37660  491 

54848  492 
definition mask :: "nat => 'a::len word" 
493 
where 

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

494 
"mask n = (1 << n)  1" 
37660  495 

54848  496 
definition revcast :: "'a :: len0 word => 'b :: len0 word" 
497 
where 

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

498 
"revcast w = of_bl (takefill False (len_of TYPE('b)) (to_bl w))" 
37660  499 

54848  500 
definition slice1 :: "nat => 'a :: len0 word => 'b :: len0 word" 
501 
where 

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

502 
"slice1 n w = of_bl (takefill False n (to_bl w))" 
37660  503 

54848  504 
definition slice :: "nat => 'a :: len0 word => 'b :: len0 word" 
505 
where 

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

506 
"slice n w = slice1 (size w  n) w" 
37660  507 

508 

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

509 
subsection {* Rotation *} 
37660  510 

54848  511 
definition rotater1 :: "'a list => 'a list" 
512 
where 

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

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

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

54848  516 
definition rotater :: "nat => 'a list => 'a list" 
517 
where 

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

518 
"rotater n = rotater1 ^^ n" 
37660  519 

54848  520 
definition word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word" 
521 
where 

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

522 
"word_rotr n w = of_bl (rotater n (to_bl w))" 
37660  523 

54848  524 
definition word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word" 
525 
where 

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

526 
"word_rotl n w = of_bl (rotate n (to_bl w))" 
37660  527 

54848  528 
definition word_roti :: "int => 'a :: len0 word => 'a :: len0 word" 
529 
where 

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

530 
"word_roti i w = (if i >= 0 then word_rotr (nat i) w 
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset

531 
else word_rotl (nat ( i)) w)" 
37660  532 

533 

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

534 
subsection {* Split and cat operations *} 
37660  535 

54848  536 
definition word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word" 
537 
where 

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

538 
"word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))" 
37660  539 

54848  540 
definition word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)" 
541 
where 

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

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

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

544 
(u, v) => (word_of_int u, word_of_int v))" 
37660  545 

54848  546 
definition word_rcat :: "'a :: len0 word list => 'b :: len0 word" 
547 
where 

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

548 
"word_rcat ws = 
37660  549 
word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))" 
550 

54848  551 
definition word_rsplit :: "'a :: len0 word => 'b :: len word list" 
552 
where 

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

553 
"word_rsplit w = 
37660  554 
map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))" 
555 

54848  556 
definition max_word :: "'a::len word"  "Largest representable machine integer." 
557 
where 

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

558 
"max_word = word_of_int (2 ^ len_of TYPE('a)  1)" 
37660  559 

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

560 
lemmas of_nth_def = word_set_bits_def (* FIXME duplicate *) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

561 

37660  562 

46010  563 
subsection {* Theorems about typedefs *} 
564 

37660  565 
lemma sint_sbintrunc': 
566 
"sint (word_of_int bin :: 'a word) = 

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

568 
unfolding sint_uint 

569 
by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt) 

570 

571 
lemma uint_sint: 

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

573 
unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le) 

574 

46057  575 
lemma bintr_uint: 
576 
fixes w :: "'a::len0 word" 

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

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

54863
82acc20ded73
prefer more canonical names for lemmas on min/max
haftmann
parents:
54854
diff
changeset

580 
apply (simp add: min.absorb2) 
37660  581 
done 
582 

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

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

54863
82acc20ded73
prefer more canonical names for lemmas on min/max
haftmann
parents:
54854
diff
changeset

586 
by (clarsimp simp add: word_ubin.norm_eq_iff [symmetric] min.absorb1) 
37660  587 

588 
lemma td_ext_sbin: 

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

589 
"td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (len_of TYPE('a::len))) 
37660  590 
(sbintrunc (len_of TYPE('a)  1))" 
591 
apply (unfold td_ext_def' sint_uint) 

592 
apply (simp add : word_ubin.eq_norm) 

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

594 
apply (auto simp add : sints_def) 

595 
apply (rule sym [THEN trans]) 

596 
apply (rule word_ubin.Abs_norm) 

597 
apply (simp only: bintrunc_sbintrunc) 

598 
apply (drule sym) 

599 
apply simp 

600 
done 

601 

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

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

603 
"td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (len_of TYPE('a::len))) 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

604 
(\<lambda>w. (w + 2 ^ (len_of TYPE('a)  1)) mod 2 ^ len_of TYPE('a)  
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

605 
2 ^ (len_of TYPE('a)  1))" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

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

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

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

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

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

612 
interpretation word_sint: 

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

614 
word_of_int 

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

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

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

618 
by (rule td_ext_sint) 

619 

620 
interpretation word_sbin: 

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

622 
word_of_int 

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

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

625 
by (rule td_ext_sbin) 

626 

45604  627 
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] 
37660  628 

629 
lemmas td_sint = word_sint.td 

630 

631 
lemma to_bl_def': 

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

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

44762  634 
by (auto simp: to_bl_def) 
37660  635 

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

636 
lemmas word_reverse_no_def [simp] = word_reverse_def [of "numeral w"] for w 
37660  637 

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

640 

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

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

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

643 
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

644 

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

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

646 

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

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

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

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

650 

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

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

652 

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

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

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

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

657 
using word_numeral_alt [symmetric] word_neg_numeral_alt [symmetric] by blast+ 
47372  658 

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

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

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

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

663 

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

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

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

666 
by (simp only: word_neg_numeral_alt word_ubin.eq_norm) 
37660  667 

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

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

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

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

672 

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

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

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

675 
by (simp only: word_neg_numeral_alt word_sbin.eq_norm) 
37660  676 

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

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

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

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

681 

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

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

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

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

685 
by (simp only: unat_def uint_bintrunc_neg) 
37660  686 

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

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

690 
apply (rule box_equals) 

691 
defer 

692 
apply (rule word_ubin.norm_Rep)+ 

693 
apply simp 

694 
done 

695 

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

698 

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

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

701 

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

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

704 

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

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

37660  707 

708 
lemma sign_uint_Pls [simp]: 

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

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

710 
by (simp add: sign_Pls_ge_0) 
37660  711 

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

714 

715 
lemma uint_m2p_not_non_neg: 

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

717 
by (simp only: not_le uint_m2p_neg) 

37660  718 

719 
lemma lt2p_lem: 

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

720 
"len_of TYPE('a) \<le> n \<Longrightarrow> uint (w :: 'a::len0 word) < 2 ^ n" 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

721 
by (metis bintr_uint bintrunc_mod2p int_mod_lem zless2p) 
37660  722 

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

37660  725 

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

726 
lemma uint_nat: "uint w = int (unat w)" 
37660  727 
unfolding unat_def by auto 
728 

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

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

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

731 
unfolding word_numeral_alt 
37660  732 
by (simp only: int_word_uint) 
733 

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

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

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

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

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

738 

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

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

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

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

744 
apply (rule zero_le_numeral) 
37660  745 
apply (simp_all add: nat_power_eq) 
746 
done 

747 

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

748 
lemma sint_numeral: "sint (numeral b :: 'a :: len word) = (numeral b + 
37660  749 
2 ^ (len_of TYPE('a)  1)) mod 2 ^ len_of TYPE('a)  
750 
2 ^ (len_of TYPE('a)  1)" 

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

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

752 

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

753 
lemma word_of_int_0 [simp, code_post]: 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

754 
"word_of_int 0 = 0" 
45958  755 
unfolding word_0_wi .. 
756 

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

757 
lemma word_of_int_1 [simp, code_post]: 
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset

758 
"word_of_int 1 = 1" 
45958  759 
unfolding word_1_wi .. 
760 

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

761 
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

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

763 

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

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

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

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

767 

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

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

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

770 
unfolding word_numeral_alt wi_hom_syms .. 
37660  771 

772 
lemma word_int_case_wi: 

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

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

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

776 

777 
lemma word_int_split: 

778 
"P (word_int_case f x) = 

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

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

781 
unfolding word_int_case_def 

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

782 
by (auto simp: word_uint.eq_norm mod_pos_pos_trivial) 
37660  783 

784 
lemma word_int_split_asm: 

785 
"P (word_int_case f x) = 

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

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

788 
unfolding word_int_case_def 

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

789 
by (auto simp: word_uint.eq_norm mod_pos_pos_trivial) 
45805  790 

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

37660  793 

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

795 
unfolding word_size by (rule uint_range') 

796 

797 
lemma sint_range_size: 

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

799 
unfolding word_size by (rule sint_range') 

800 

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

803 

804 
lemma sint_below_size: 

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

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

37660  807 

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

808 

46010  809 
subsection {* Testing bits *} 
810 

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

813 

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

815 
apply (unfold word_test_bit_def) 

816 
apply (subst word_ubin.norm_Rep [symmetric]) 

817 
apply (simp only: nth_bintr word_size) 

818 
apply fast 

819 
done 

820 

46021  821 
lemma word_eq_iff: 
822 
fixes x y :: "'a::len0 word" 

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

824 
unfolding uint_inject [symmetric] bin_eq_iff word_test_bit_def [symmetric] 

825 
by (metis test_bit_size [unfolded word_size]) 

826 

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

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

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

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

37660  834 

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

836 
unfolding word_test_bit_def word_size 

837 
by (simp add: nth_bintr [symmetric]) 

838 

839 
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] 

840 

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

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

845 
apply assumption 

846 
done 

847 

46057  848 
lemma bin_nth_sint: 
849 
fixes w :: "'a::len word" 

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

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

37660  852 
apply (subst word_sbin.norm_Rep [symmetric]) 
46057  853 
apply (auto simp add: nth_sbintr) 
37660  854 
done 
855 

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

857 
lemma td_bl: 

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

859 
of_bl 

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

861 
apply (unfold type_definition_def of_bl_def to_bl_def) 

862 
apply (simp add: word_ubin.eq_norm) 

863 
apply safe 

864 
apply (drule sym) 

865 
apply simp 

866 
done 

867 

868 
interpretation word_bl: 

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

870 
of_bl 

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

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

872 
by (fact td_bl) 
37660  873 

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

874 
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

875 

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

876 
lemma word_size_bl: "size w = size (to_bl w)" 
37660  877 
unfolding word_size by auto 
878 

879 
lemma to_bl_use_of_bl: 

880 
"(to_bl w = bl) = (w = of_bl bl \<and> length bl = length (to_bl w))" 

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

881 
by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq]) 
37660  882 

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

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

885 

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

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

888 

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

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

890 
by (metis word_rev_rev) 
37660  891 

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

894 

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

896 
unfolding word_bl_Rep' by (rule len_gt_0) 

897 

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

899 
by (fact length_bl_gt_0 [unfolded length_greater_0_conv]) 

900 

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

902 
by (fact length_bl_gt_0 [THEN gr_implies_not0]) 

37660  903 

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

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

907 
apply simp 

908 
done 

909 

910 
lemma of_bl_drop': 

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

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

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

915 
done 

916 

917 
lemma test_bit_of_bl: 

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

919 
apply (unfold of_bl_def word_test_bit_def) 

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

921 
done 

922 

923 
lemma no_of_bl: 

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

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

925 
unfolding of_bl_def by simp 
37660  926 

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

927 
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)" 
37660  928 
unfolding word_size to_bl_def by auto 
929 

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

931 
unfolding uint_bl by (simp add : word_size) 

932 

933 
lemma to_bl_of_bin: 

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

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

936 

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

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

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

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

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

941 

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

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

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

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

945 
unfolding word_neg_numeral_alt by (rule to_bl_of_bin) 
37660  946 

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

948 
unfolding uint_bl by (simp add : word_size) 

46011  949 

950 
lemma uint_bl_bin: 

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

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

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

45604  954 

37660  955 
(* naturals *) 
956 
lemma uints_unats: "uints n = int ` unats n" 

957 
apply (unfold unats_def uints_num) 

958 
apply safe 

959 
apply (rule_tac image_eqI) 

960 
apply (erule_tac nat_0_le [symmetric]) 

961 
apply auto 

962 
apply (erule_tac nat_less_iff [THEN iffD2]) 

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

964 
apply (auto simp add : nat_power_eq int_power) 

965 
done 

966 

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

968 
by (auto simp add : uints_unats image_iff) 

969 

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

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

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

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

973 
[of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b 
37660  974 

975 
lemma num_of_bintr': 

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

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

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

978 
unfolding bintr_num by (erule subst, simp) 
37660  979 

980 
lemma num_of_sbintr': 

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

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

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

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

984 

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

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

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

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

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

989 

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

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

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

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

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

994 

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

997 

998 
lemma ucast_id: "ucast w = w" 

999 
unfolding ucast_def by auto 

1000 

1001 
lemma scast_id: "scast w = w" 

1002 
unfolding scast_def by auto 

1003 

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

1004 
lemma ucast_bl: "ucast w = of_bl (to_bl w)" 
37660  1005 
unfolding ucast_def of_bl_def uint_bl 
1006 
by (auto simp add : word_size) 

1007 

1008 
lemma nth_ucast: 

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

1010 
apply (unfold ucast_def test_bit_bin) 

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

1012 
apply (fast elim!: bin_nth_uint_imp) 

1013 
done 

1014 

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

1016 

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

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

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

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

1021 
(* TODO: neg_numeral *) 
37660  1022 

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

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

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

1025 
word_of_int (sbintrunc (len_of TYPE('a)  Suc 0) (numeral w))" 
37660  1026 
unfolding scast_def by simp 
1027 

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

1030 

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

1032 
unfolding target_size_def word_size Let_def .. 

1033 

1034 
lemma is_down: 

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

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

1037 
unfolding is_down_def source_size target_size .. 

1038 

1039 
lemma is_up: 

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

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

1042 
unfolding is_up_def source_size target_size .. 

37660  1043 

45604  1044 
lemmas is_up_down = trans [OF is_up is_down [symmetric]] 
37660  1045 

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

1049 
apply (rule ext) 

1050 
apply (unfold ucast_def scast_def uint_sint) 

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

1052 
apply simp 

1053 
done 

1054 

45811  1055 
lemma word_rev_tf: 
1056 
"to_bl (of_bl bl::'a::len0 word) = 

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

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

1060 

45811  1061 
lemma word_rep_drop: 
1062 
"to_bl (of_bl bl::'a::len0 word) = 

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

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

1065 
by (simp add: word_rev_tf takefill_alt rev_take) 

37660  1066 

1067 
lemma to_bl_ucast: 

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

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

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

1071 
apply (unfold ucast_bl) 

1072 
apply (rule trans) 

1073 
apply (rule word_rep_drop) 

1074 
apply simp 

1075 
done 

1076 

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

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

1081 

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

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

1086 

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

1088 
"sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> 
37660  1089 
to_bl (sc w) = drop n (to_bl w)" 
1090 
apply (subgoal_tac "sc = ucast") 

1091 
apply safe 

1092 
apply simp 

45811  1093 
apply (erule ucast_down_drop) 
1094 
apply (rule down_cast_same [symmetric]) 

37660  1095 
apply (simp add : source_size target_size is_down) 
1096 
done 

1097 

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

1099 
"sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w" 
37660  1100 
apply (unfold is_up) 
1101 
apply safe 

1102 
apply (simp add: scast_def word_sbin.eq_norm) 

1103 
apply (rule box_equals) 

1104 
prefer 3 

1105 
apply (rule word_sbin.norm_Rep) 

1106 
apply (rule sbintrunc_sbintrunc_l) 

1107 
defer 

1108 
apply (subst word_sbin.norm_Rep) 

1109 
apply (rule refl) 

1110 
apply simp 

1111 
done 

1112 

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

1114 
"uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w" 
37660  1115 
apply (unfold is_up) 
1116 
apply safe 

1117 
apply (rule bin_eqI) 

1118 
apply (fold word_test_bit_def) 

1119 
apply (auto simp add: nth_ucast) 

1120 
apply (auto simp add: test_bit_bin) 

1121 
done 

45811  1122 

1123 
lemma ucast_up_ucast [OF refl]: 

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

37660  1125 
apply (simp (no_asm) add: ucast_def) 
1126 
apply (clarsimp simp add: uint_up_ucast) 

1127 
done 

1128 

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

37660  1131 
apply (simp (no_asm) add: scast_def) 
1132 
apply (clarsimp simp add: sint_up_scast) 

1133 
done 

1134 

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

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

1139 
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] 

1140 
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] 

1141 

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

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

1144 
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] 

1145 
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] 

1146 

1147 
lemma up_ucast_surj: 

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

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

1151 

1152 
lemma up_scast_surj: 

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

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

1156 

1157 
lemma down_scast_inj: 

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

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

1161 

1162 
lemma down_ucast_inj: 

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

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

1166 

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

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

45811  1169 

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

1172 
apply (unfold is_down) 

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

1175 
apply (erule bintrunc_bintrunc_ge) 

1176 
done 

45811  1177 

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

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

1180 
unfolding word_numeral_alt by clarify (rule ucast_down_wi) 
46646  1181 

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

46646  1184 
unfolding of_bl_def by clarify (erule ucast_down_wi) 
37660  1185 

1186 
lemmas slice_def' = slice_def [unfolded word_size] 

1187 
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] 

1188 

1189 
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def 

1190 

1191 

1192 
subsection {* Word Arithmetic *} 

1193 

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

55818  1195 
by (fact word_less_def) 
37660  1196 

1197 
lemma signed_linorder: "class.linorder word_sle word_sless" 

46124  1198 
by default (unfold word_sle_def word_sless_def, auto) 
37660  1199 

1200 
interpretation signed: linorder "word_sle" "word_sless" 

1201 
by (rule signed_linorder) 

1202 

1203 
lemma udvdI: 

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

1204 
"0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" 
37660  1205 
by (auto simp: udvd_def) 
1206 

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

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

1208 

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

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

1210 

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

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

1212 

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

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

1214 

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

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

1216 

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

1217 
lemmas word_sle_no [simp] = word_sle_def [of "numeral a" "numeral b"] for a b 
37660  1218 

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

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

1220 
using word_neg_numeral_alt [of Num.One] by simp 
37660  1221 

46648  1222 
lemma word_0_bl [simp]: "of_bl [] = 0" 
1223 
unfolding of_bl_def by simp 

37660  1224 

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

46648  1226 
unfolding of_bl_def by (simp add: bl_to_bin_def) 
1227 

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

1229 
unfolding word_0_wi word_ubin.eq_norm by simp 

37660  1230 

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

1231 
lemma of_bl_0 [simp]: "of_bl (replicate n False) = 0" 
46648  1232 
by (simp add: of_bl_def bl_to_bin_rep_False) 
37660  1233 

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

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

1237 
by (simp add: word_size bin_to_bl_zero) 
37660  1238 

55818  1239 
lemma uint_0_iff: 
1240 
"uint x = 0 \<longleftrightarrow> x = 0" 

1241 
by (simp add: word_uint_eq_iff) 

1242 

1243 
lemma unat_0_iff: 

1244 
"unat x = 0 \<longleftrightarrow> x = 0" 

37660  1245 
unfolding unat_def by (auto simp add : nat_eq_iff uint_0_iff) 
1246 

55818  1247 
lemma unat_0 [simp]: 
1248 
"unat 0 = 0" 

37660  1249 
unfolding unat_def by auto 
1250 

55818  1251 
lemma size_0_same': 
1252 
"size w = 0 \<Longrightarrow> w = (v :: 'a :: len0 word)" 

37660  1253 
apply (unfold word_size) 
1254 
apply (rule box_equals) 

1255 
defer 

1256 
apply (rule word_uint.Rep_inverse)+ 

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

1258 
apply simp 

1259 
done 

1260 

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

1261 
lemmas size_0_same = size_0_same' [unfolded word_size] 
37660  1262 

1263 
lemmas unat_eq_0 = unat_0_iff 

1264 
lemmas unat_eq_zero = unat_0_iff 

1265 

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

1267 
by (auto simp: unat_0_iff [symmetric]) 

1268 

45958  1269 
lemma ucast_0 [simp]: "ucast 0 = 0" 
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset

1270 
unfolding ucast_def by simp 
45958  1271 

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

1273 
unfolding sint_uint by simp 

1274 

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

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

1276 
unfolding scast_def by simp 
37660  1277 

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

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

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

1280 

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

1281 
lemma scast_n1 [simp]: "scast ( 1) =  1" 
45958  1282 
unfolding scast_def by simp 
1283 

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

55818  1285 
by (simp only: word_1_wi word_ubin.eq_norm) (simp add: bintrunc_minus_simps(4)) 
45958  1286 

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

1288 
unfolding unat_def by simp 

1289 

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

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

1291 
unfolding ucast_def by simp 
37660  1292 

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

1294 

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

1295 

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

1296 
subsection {* Transferring goals from words to ints *} 
37660  1297 

1298 
lemma word_ths: 

1299 
shows 

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

1301 
word_pred_m1: "word_pred a = a  1" and 

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

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

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

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

1305 
by (transfer, simp add: algebra_simps)+ 
37660  1306 

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

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

1308 
by simp 
37660  1309 

55818  1310 
lemma uint_word_ariths: 
1311 
fixes a b :: "'a::len0 word" 
