author | haftmann |
Thu, 18 Jun 2020 09:07:30 +0000 | |
changeset 71954 | 13bb3f5cdc5b |
parent 71953 | 428609096812 |
child 71955 | a9f913d17d00 |
permissions | -rw-r--r-- |
29628 | 1 |
(* Title: HOL/Word/Word.thy |
46124 | 2 |
Author: Jeremy Dawson and Gerwin Klein, NICTA |
24333 | 3 |
*) |
4 |
||
61799 | 5 |
section \<open>A type of finite bit strings\<close> |
24350 | 6 |
|
29628 | 7 |
theory Word |
41413
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
41060
diff
changeset
|
8 |
imports |
66453
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
65363
diff
changeset
|
9 |
"HOL-Library.Type_Length" |
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
65363
diff
changeset
|
10 |
"HOL-Library.Boolean_Algebra" |
70190 | 11 |
Bits_Int |
70342
e4d626692640
clear separation of types for bits (False / True) and Z2 (0 / 1)
haftmann
parents:
70193
diff
changeset
|
12 |
Bits_Z2 |
70192 | 13 |
Bit_Comprehension |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
51717
diff
changeset
|
14 |
Misc_Typedef |
70170 | 15 |
Misc_Arithmetic |
37660 | 16 |
begin |
17 |
||
70173 | 18 |
text \<open>See \<^file>\<open>Word_Examples.thy\<close> for examples.\<close> |
61799 | 19 |
|
20 |
subsection \<open>Type definition\<close> |
|
37660 | 21 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
22 |
quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l\<close> |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
23 |
morphisms rep_word word_of_int by (auto intro!: equivpI reflpI sympI transpI) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
24 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
25 |
lift_definition uint :: \<open>'a::len word \<Rightarrow> int\<close> |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
26 |
is \<open>take_bit LENGTH('a)\<close> . |
37660 | 27 |
|
65268 | 28 |
lemma uint_nonnegative: "0 \<le> uint w" |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
29 |
by transfer simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
30 |
|
70185 | 31 |
lemma uint_bounded: "uint w < 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
32 |
for w :: "'a::len word" |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
33 |
by transfer (simp add: take_bit_eq_mod) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
34 |
|
70185 | 35 |
lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
36 |
for w :: "'a::len word" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
37 |
using uint_nonnegative uint_bounded by (rule mod_pos_pos_trivial) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
38 |
|
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
39 |
lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b" |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
40 |
by transfer simp |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
41 |
|
65268 | 42 |
lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b" |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
43 |
using word_uint_eqI by auto |
70185 | 44 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
45 |
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len word) = k mod 2 ^ LENGTH('a)" |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
46 |
by transfer (simp add: take_bit_eq_mod) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
47 |
|
65268 | 48 |
lemma word_of_int_uint: "word_of_int (uint w) = w" |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
49 |
by transfer simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
50 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
51 |
lemma split_word_all: "(\<And>x::'a::len word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
52 |
proof |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
53 |
fix x :: "'a word" |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
54 |
assume "\<And>x. PROP P (word_of_int x)" |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
55 |
then have "PROP P (word_of_int (uint x))" . |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
56 |
then show "PROP P x" by (simp add: word_of_int_uint) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
57 |
qed |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
58 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
59 |
|
61799 | 60 |
subsection \<open>Type conversions and casting\<close> |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
61 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
62 |
definition sint :: "'a::len word \<Rightarrow> int" |
61799 | 63 |
\<comment> \<open>treats the most-significant-bit as a sign bit\<close> |
70175 | 64 |
where sint_uint: "sint w = sbintrunc (LENGTH('a) - 1) (uint w)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
65 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
66 |
definition unat :: "'a::len word \<Rightarrow> nat" |
65268 | 67 |
where "unat w = nat (uint w)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
68 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
69 |
definition uints :: "nat \<Rightarrow> int set" |
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
70 |
\<comment> \<open>the sets of integers representing the words\<close> |
65268 | 71 |
where "uints n = range (bintrunc n)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
72 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
73 |
definition sints :: "nat \<Rightarrow> int set" |
65268 | 74 |
where "sints n = range (sbintrunc (n - 1))" |
75 |
||
76 |
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}" |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
77 |
by (simp add: uints_def range_bintrunc) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
78 |
|
65268 | 79 |
lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
80 |
by (simp add: sints_def range_sbintrunc) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
81 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
82 |
definition unats :: "nat \<Rightarrow> nat set" |
65268 | 83 |
where "unats n = {i. i < 2 ^ n}" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
84 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
85 |
definition norm_sint :: "nat \<Rightarrow> int \<Rightarrow> int" |
65268 | 86 |
where "norm_sint n w = (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
87 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
88 |
definition scast :: "'a::len word \<Rightarrow> 'b::len word" |
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
89 |
\<comment> \<open>cast a word to a different length\<close> |
65268 | 90 |
where "scast w = word_of_int (sint w)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
91 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
92 |
definition ucast :: "'a::len word \<Rightarrow> 'b::len word" |
65268 | 93 |
where "ucast w = word_of_int (uint w)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
94 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
95 |
instantiation word :: (len) size |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
96 |
begin |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
97 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
98 |
definition word_size: "size (w :: 'a word) = LENGTH('a)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
99 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
100 |
instance .. |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
101 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
102 |
end |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
103 |
|
65268 | 104 |
lemma word_size_gt_0 [iff]: "0 < size w" |
105 |
for w :: "'a::len word" |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
106 |
by (simp add: word_size) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
107 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
108 |
lemmas lens_gt_0 = word_size_gt_0 len_gt_0 |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
109 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
110 |
lemma lens_not_0 [iff]: |
71942 | 111 |
\<open>size w \<noteq> 0\<close> for w :: \<open>'a::len word\<close> |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
112 |
by auto |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
113 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
114 |
definition source_size :: "('a::len word \<Rightarrow> 'b) \<Rightarrow> nat" |
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
115 |
\<comment> \<open>whether a cast (or other) function is to a longer or shorter length\<close> |
65268 | 116 |
where [code del]: "source_size c = (let arb = undefined; x = c arb in size arb)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
117 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
118 |
definition target_size :: "('a \<Rightarrow> 'b::len word) \<Rightarrow> nat" |
65268 | 119 |
where [code del]: "target_size c = size (c undefined)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
120 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
121 |
definition is_up :: "('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool" |
65268 | 122 |
where "is_up c \<longleftrightarrow> source_size c \<le> target_size c" |
123 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
124 |
definition is_down :: "('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool" |
65268 | 125 |
where "is_down c \<longleftrightarrow> target_size c \<le> source_size c" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
126 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
127 |
definition of_bl :: "bool list \<Rightarrow> 'a::len word" |
65268 | 128 |
where "of_bl bl = word_of_int (bl_to_bin bl)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
129 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
130 |
definition to_bl :: "'a::len word \<Rightarrow> bool list" |
70185 | 131 |
where "to_bl w = bin_to_bl (LENGTH('a)) (uint w)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
132 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
133 |
definition word_reverse :: "'a::len word \<Rightarrow> 'a word" |
65268 | 134 |
where "word_reverse w = of_bl (rev (to_bl w))" |
135 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
136 |
definition word_int_case :: "(int \<Rightarrow> 'b) \<Rightarrow> 'a::len word \<Rightarrow> 'b" |
65268 | 137 |
where "word_int_case f w = f (uint w)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
138 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
139 |
translations |
65268 | 140 |
"case x of XCONST of_int y \<Rightarrow> b" \<rightleftharpoons> "CONST word_int_case (\<lambda>y. b) x" |
141 |
"case x of (XCONST of_int :: 'a) y \<Rightarrow> b" \<rightharpoonup> "CONST word_int_case (\<lambda>y. b) x" |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
142 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
143 |
|
61799 | 144 |
subsection \<open>Basic code generation setup\<close> |
55817
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
145 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
146 |
definition Word :: "int \<Rightarrow> 'a::len word" |
65268 | 147 |
where [code_post]: "Word = word_of_int" |
148 |
||
149 |
lemma [code abstype]: "Word (uint w) = w" |
|
55817
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
150 |
by (simp add: Word_def word_of_int_uint) |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
151 |
|
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
152 |
declare uint_word_of_int [code abstract] |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
153 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
154 |
instantiation word :: (len) equal |
55817
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
155 |
begin |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
156 |
|
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
157 |
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
65268 | 158 |
where "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" |
159 |
||
160 |
instance |
|
161 |
by standard (simp add: equal equal_word_def word_uint_eq_iff) |
|
55817
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
162 |
|
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
163 |
end |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
164 |
|
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
165 |
notation fcomp (infixl "\<circ>>" 60) |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
166 |
notation scomp (infixl "\<circ>\<rightarrow>" 60) |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
167 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
168 |
instantiation word :: ("{len, typerep}") random |
55817
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
169 |
begin |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
170 |
|
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
171 |
definition |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
172 |
"random_word i = Random.range i \<circ>\<rightarrow> (\<lambda>k. Pair ( |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
173 |
let j = word_of_int (int_of_integer (integer_of_natural k)) :: 'a word |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
174 |
in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))" |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
175 |
|
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
176 |
instance .. |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
177 |
|
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
178 |
end |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
179 |
|
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
180 |
no_notation fcomp (infixl "\<circ>>" 60) |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
181 |
no_notation scomp (infixl "\<circ>\<rightarrow>" 60) |
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
182 |
|
0bc0217387a5
earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents:
55816
diff
changeset
|
183 |
|
61799 | 184 |
subsection \<open>Type-definition locale instantiations\<close> |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
185 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
186 |
lemmas uint_0 = uint_nonnegative (* FIXME duplicate *) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
187 |
lemmas uint_lt = uint_bounded (* FIXME duplicate *) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
188 |
lemmas uint_mod_same = uint_idem (* FIXME duplicate *) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
189 |
|
65268 | 190 |
lemma td_ext_uint: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
191 |
"td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len))) |
70185 | 192 |
(\<lambda>w::int. w mod 2 ^ LENGTH('a))" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
193 |
apply (unfold td_ext_def') |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
194 |
apply transfer |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
195 |
apply (simp add: uints_num take_bit_eq_mod) |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
196 |
done |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
197 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
198 |
interpretation word_uint: |
65268 | 199 |
td_ext |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
200 |
"uint::'a::len word \<Rightarrow> int" |
65268 | 201 |
word_of_int |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
202 |
"uints (LENGTH('a::len))" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
203 |
"\<lambda>w. w mod 2 ^ LENGTH('a::len)" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
204 |
by (fact td_ext_uint) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
205 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
206 |
lemmas td_uint = word_uint.td_thm |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
207 |
lemmas int_word_uint = word_uint.eq_norm |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
208 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
209 |
lemma td_ext_ubin: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
210 |
"td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len))) |
70185 | 211 |
(bintrunc (LENGTH('a)))" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
212 |
by (unfold no_bintr_alt1) (fact td_ext_uint) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
213 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
214 |
interpretation word_ubin: |
65268 | 215 |
td_ext |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
216 |
"uint::'a::len word \<Rightarrow> int" |
65268 | 217 |
word_of_int |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
218 |
"uints (LENGTH('a::len))" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
219 |
"bintrunc (LENGTH('a::len))" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
220 |
by (fact td_ext_ubin) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
221 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
222 |
|
61799 | 223 |
subsection \<open>Arithmetic operations\<close> |
37660 | 224 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
225 |
lift_definition word_succ :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x + 1" |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
226 |
by (auto simp add: bintrunc_mod2p intro: mod_add_cong) |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
227 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
228 |
lift_definition word_pred :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x - 1" |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
229 |
by (auto simp add: bintrunc_mod2p intro: mod_diff_cong) |
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
230 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
231 |
instantiation word :: (len) "{neg_numeral, modulo, comm_monoid_mult, comm_ring}" |
37660 | 232 |
begin |
233 |
||
47387
a0f257197741
remove now-unnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset
|
234 |
lift_definition zero_word :: "'a word" is "0" . |
a0f257197741
remove now-unnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset
|
235 |
|
a0f257197741
remove now-unnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset
|
236 |
lift_definition one_word :: "'a word" is "1" . |
a0f257197741
remove now-unnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset
|
237 |
|
67399 | 238 |
lift_definition plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(+)" |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
239 |
by (auto simp add: bintrunc_mod2p intro: mod_add_cong) |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
240 |
|
67399 | 241 |
lift_definition minus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(-)" |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
242 |
by (auto simp add: bintrunc_mod2p intro: mod_diff_cong) |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
243 |
|
47387
a0f257197741
remove now-unnecessary type annotations from lift_definition commands
huffman
parents:
47377
diff
changeset
|
244 |
lift_definition uminus_word :: "'a word \<Rightarrow> 'a word" is uminus |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
245 |
by (auto simp add: bintrunc_mod2p intro: mod_minus_cong) |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
246 |
|
69064
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents:
68157
diff
changeset
|
247 |
lift_definition times_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(*)" |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
248 |
by (auto simp add: bintrunc_mod2p intro: mod_mult_cong) |
37660 | 249 |
|
71950 | 250 |
lift_definition divide_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
251 |
is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b" |
|
252 |
by simp |
|
253 |
||
254 |
lift_definition modulo_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
255 |
is "\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b" |
|
256 |
by simp |
|
37660 | 257 |
|
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
258 |
instance |
61169 | 259 |
by standard (transfer, simp add: algebra_simps)+ |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
260 |
|
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
261 |
end |
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
262 |
|
71950 | 263 |
lemma word_div_def [code]: |
264 |
"a div b = word_of_int (uint a div uint b)" |
|
265 |
by transfer rule |
|
266 |
||
267 |
lemma word_mod_def [code]: |
|
268 |
"a mod b = word_of_int (uint a mod uint b)" |
|
269 |
by transfer rule |
|
270 |
||
70901 | 271 |
quickcheck_generator word |
272 |
constructors: |
|
273 |
"zero_class.zero :: ('a::len) word", |
|
274 |
"numeral :: num \<Rightarrow> ('a::len) word", |
|
275 |
"uminus :: ('a::len) word \<Rightarrow> ('a::len) word" |
|
276 |
||
71950 | 277 |
context |
278 |
includes lifting_syntax |
|
279 |
notes power_transfer [transfer_rule] |
|
280 |
begin |
|
281 |
||
282 |
lemma power_transfer_word [transfer_rule]: |
|
283 |
\<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close> |
|
284 |
by transfer_prover |
|
285 |
||
286 |
end |
|
287 |
||
288 |
||
71951 | 289 |
|
61799 | 290 |
text \<open>Legacy theorems:\<close> |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
291 |
|
65268 | 292 |
lemma word_arith_wis [code]: |
293 |
shows word_add_def: "a + b = word_of_int (uint a + uint b)" |
|
294 |
and word_sub_wi: "a - b = word_of_int (uint a - uint b)" |
|
295 |
and word_mult_def: "a * b = word_of_int (uint a * uint b)" |
|
296 |
and word_minus_def: "- a = word_of_int (- uint a)" |
|
297 |
and word_succ_alt: "word_succ a = word_of_int (uint a + 1)" |
|
298 |
and word_pred_alt: "word_pred a = word_of_int (uint a - 1)" |
|
299 |
and word_0_wi: "0 = word_of_int 0" |
|
300 |
and word_1_wi: "1 = word_of_int 1" |
|
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
301 |
apply (simp_all flip: plus_word.abs_eq minus_word.abs_eq |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
302 |
times_word.abs_eq uminus_word.abs_eq |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
303 |
zero_word.abs_eq one_word.abs_eq) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
304 |
apply transfer |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
305 |
apply simp |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
306 |
apply transfer |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
307 |
apply simp |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
308 |
done |
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
309 |
|
65268 | 310 |
lemma wi_homs: |
311 |
shows wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" |
|
312 |
and wi_hom_sub: "word_of_int a - word_of_int b = word_of_int (a - b)" |
|
313 |
and wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" |
|
314 |
and wi_hom_neg: "- word_of_int a = word_of_int (- a)" |
|
315 |
and wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" |
|
316 |
and wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a - 1)" |
|
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
317 |
by (transfer, simp)+ |
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
318 |
|
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
319 |
lemmas wi_hom_syms = wi_homs [symmetric] |
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
320 |
|
46013 | 321 |
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi |
46009 | 322 |
|
323 |
lemmas word_of_int_hom_syms = word_of_int_homs [symmetric] |
|
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
324 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
325 |
instance word :: (len) comm_monoid_add .. |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
326 |
|
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
327 |
instance word :: (len) semiring_numeral .. |
71950 | 328 |
|
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
329 |
instance word :: (len) comm_ring_1 |
45810 | 330 |
proof |
70185 | 331 |
have *: "0 < LENGTH('a)" by (rule len_gt_0) |
65268 | 332 |
show "(0::'a word) \<noteq> 1" |
333 |
by transfer (use * in \<open>auto simp add: gr0_conv_Suc\<close>) |
|
45810 | 334 |
qed |
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
335 |
|
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
336 |
lemma word_of_nat: "of_nat n = word_of_int (int n)" |
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
337 |
by (induct n) (auto simp add : word_of_int_hom_syms) |
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
338 |
|
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
339 |
lemma word_of_int: "of_int = word_of_int" |
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
340 |
apply (rule ext) |
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
341 |
apply (case_tac x rule: int_diff_cases) |
46013 | 342 |
apply (simp add: word_of_nat wi_hom_sub) |
45545
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
343 |
done |
26aebb8ac9c1
Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents:
45544
diff
changeset
|
344 |
|
71950 | 345 |
context |
346 |
includes lifting_syntax |
|
347 |
notes |
|
348 |
transfer_rule_of_bool [transfer_rule] |
|
349 |
transfer_rule_numeral [transfer_rule] |
|
350 |
transfer_rule_of_nat [transfer_rule] |
|
351 |
transfer_rule_of_int [transfer_rule] |
|
352 |
begin |
|
353 |
||
354 |
lemma [transfer_rule]: |
|
355 |
"((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) of_bool of_bool" |
|
356 |
by transfer_prover |
|
357 |
||
358 |
lemma [transfer_rule]: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
359 |
"((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) numeral numeral" |
71950 | 360 |
by transfer_prover |
361 |
||
362 |
lemma [transfer_rule]: |
|
363 |
"((=) ===> pcr_word) int of_nat" |
|
364 |
by transfer_prover |
|
365 |
||
366 |
lemma [transfer_rule]: |
|
367 |
"((=) ===> pcr_word) (\<lambda>k. k) of_int" |
|
368 |
proof - |
|
369 |
have "((=) ===> pcr_word) of_int of_int" |
|
370 |
by transfer_prover |
|
371 |
then show ?thesis by (simp add: id_def) |
|
372 |
qed |
|
373 |
||
374 |
end |
|
375 |
||
376 |
lemma word_of_int_eq: |
|
377 |
"word_of_int = of_int" |
|
378 |
by (rule ext) (transfer, rule) |
|
379 |
||
65268 | 380 |
definition udvd :: "'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool" (infixl "udvd" 50) |
381 |
where "a udvd b = (\<exists>n\<ge>0. uint b = n * uint a)" |
|
37660 | 382 |
|
71950 | 383 |
context |
384 |
includes lifting_syntax |
|
385 |
begin |
|
386 |
||
387 |
lemma [transfer_rule]: |
|
388 |
"(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)" |
|
389 |
proof - |
|
390 |
have even_word_unfold: "even k \<longleftrightarrow> (\<exists>l. take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l))" (is "?P \<longleftrightarrow> ?Q") |
|
391 |
for k :: int |
|
392 |
proof |
|
393 |
assume ?P |
|
394 |
then show ?Q |
|
395 |
by auto |
|
396 |
next |
|
397 |
assume ?Q |
|
398 |
then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" .. |
|
399 |
then have "even (take_bit LENGTH('a) k)" |
|
400 |
by simp |
|
401 |
then show ?P |
|
402 |
by simp |
|
403 |
qed |
|
404 |
show ?thesis by (simp only: even_word_unfold [abs_def] dvd_def [where ?'a = "'a word", abs_def]) |
|
405 |
transfer_prover |
|
406 |
qed |
|
407 |
||
408 |
end |
|
409 |
||
71951 | 410 |
instance word :: (len) semiring_modulo |
411 |
proof |
|
412 |
show "a div b * b + a mod b = a" for a b :: "'a word" |
|
413 |
proof transfer |
|
414 |
fix k l :: int |
|
415 |
define r :: int where "r = 2 ^ LENGTH('a)" |
|
416 |
then have r: "take_bit LENGTH('a) k = k mod r" for k |
|
417 |
by (simp add: take_bit_eq_mod) |
|
418 |
have "k mod r = ((k mod r) div (l mod r) * (l mod r) |
|
419 |
+ (k mod r) mod (l mod r)) mod r" |
|
420 |
by (simp add: div_mult_mod_eq) |
|
421 |
also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r |
|
422 |
+ (k mod r) mod (l mod r)) mod r" |
|
423 |
by (simp add: mod_add_left_eq) |
|
424 |
also have "... = (((k mod r) div (l mod r) * l) mod r |
|
425 |
+ (k mod r) mod (l mod r)) mod r" |
|
426 |
by (simp add: mod_mult_right_eq) |
|
427 |
finally have "k mod r = ((k mod r) div (l mod r) * l |
|
428 |
+ (k mod r) mod (l mod r)) mod r" |
|
429 |
by (simp add: mod_simps) |
|
430 |
with r show "take_bit LENGTH('a) (take_bit LENGTH('a) k div take_bit LENGTH('a) l * l |
|
431 |
+ take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k" |
|
432 |
by simp |
|
433 |
qed |
|
434 |
qed |
|
435 |
||
436 |
instance word :: (len) semiring_parity |
|
437 |
proof |
|
438 |
show "\<not> 2 dvd (1::'a word)" |
|
439 |
by transfer simp |
|
440 |
show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0" |
|
441 |
for a :: "'a word" |
|
442 |
by transfer (simp_all add: mod_2_eq_odd take_bit_Suc) |
|
443 |
show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1" |
|
444 |
for a :: "'a word" |
|
445 |
by transfer (simp_all add: mod_2_eq_odd take_bit_Suc) |
|
446 |
qed |
|
447 |
||
71953 | 448 |
lemma exp_eq_zero_iff: |
449 |
\<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close> |
|
450 |
by transfer simp |
|
451 |
||
45547 | 452 |
|
61799 | 453 |
subsection \<open>Ordering\<close> |
45547 | 454 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
455 |
instantiation word :: (len) linorder |
45547 | 456 |
begin |
457 |
||
71950 | 458 |
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
459 |
is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b" |
|
460 |
by simp |
|
461 |
||
462 |
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
|
463 |
is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b" |
|
464 |
by simp |
|
37660 | 465 |
|
45547 | 466 |
instance |
71950 | 467 |
by (standard; transfer) auto |
45547 | 468 |
|
469 |
end |
|
470 |
||
71950 | 471 |
lemma word_le_def [code]: |
472 |
"a \<le> b \<longleftrightarrow> uint a \<le> uint b" |
|
473 |
by transfer rule |
|
474 |
||
475 |
lemma word_less_def [code]: |
|
476 |
"a < b \<longleftrightarrow> uint a < uint b" |
|
477 |
by transfer rule |
|
478 |
||
71951 | 479 |
lemma word_greater_zero_iff: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
480 |
\<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len word\<close> |
71951 | 481 |
by transfer (simp add: less_le) |
482 |
||
483 |
lemma of_nat_word_eq_iff: |
|
484 |
\<open>of_nat m = (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close> |
|
485 |
by transfer (simp add: take_bit_of_nat) |
|
486 |
||
487 |
lemma of_nat_word_less_eq_iff: |
|
488 |
\<open>of_nat m \<le> (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close> |
|
489 |
by transfer (simp add: take_bit_of_nat) |
|
490 |
||
491 |
lemma of_nat_word_less_iff: |
|
492 |
\<open>of_nat m < (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close> |
|
493 |
by transfer (simp add: take_bit_of_nat) |
|
494 |
||
495 |
lemma of_nat_word_eq_0_iff: |
|
496 |
\<open>of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close> |
|
497 |
using of_nat_word_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff) |
|
498 |
||
499 |
lemma of_int_word_eq_iff: |
|
500 |
\<open>of_int k = (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> |
|
501 |
by transfer rule |
|
502 |
||
503 |
lemma of_int_word_less_eq_iff: |
|
504 |
\<open>of_int k \<le> (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close> |
|
505 |
by transfer rule |
|
506 |
||
507 |
lemma of_int_word_less_iff: |
|
508 |
\<open>of_int k < (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close> |
|
509 |
by transfer rule |
|
510 |
||
511 |
lemma of_int_word_eq_0_iff: |
|
512 |
\<open>of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close> |
|
513 |
using of_int_word_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff) |
|
514 |
||
65268 | 515 |
definition word_sle :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> bool" ("(_/ <=s _)" [50, 51] 50) |
516 |
where "a <=s b \<longleftrightarrow> sint a \<le> sint b" |
|
517 |
||
518 |
definition word_sless :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> bool" ("(_/ <s _)" [50, 51] 50) |
|
519 |
where "x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y" |
|
37660 | 520 |
|
521 |
||
61799 | 522 |
subsection \<open>Bit-wise operations\<close> |
37660 | 523 |
|
71951 | 524 |
lemma word_bit_induct [case_names zero even odd]: |
525 |
\<open>P a\<close> if word_zero: \<open>P 0\<close> |
|
526 |
and word_even: \<open>\<And>a. P a \<Longrightarrow> 0 < a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (2 * a)\<close> |
|
527 |
and word_odd: \<open>\<And>a. P a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (1 + 2 * a)\<close> |
|
528 |
for P and a :: \<open>'a::len word\<close> |
|
529 |
proof - |
|
530 |
define m :: nat where \<open>m = LENGTH('a) - 1\<close> |
|
531 |
then have l: \<open>LENGTH('a) = Suc m\<close> |
|
532 |
by simp |
|
533 |
define n :: nat where \<open>n = unat a\<close> |
|
534 |
then have \<open>n < 2 ^ LENGTH('a)\<close> |
|
535 |
by (unfold unat_def) (transfer, simp add: take_bit_eq_mod) |
|
536 |
then have \<open>n < 2 * 2 ^ m\<close> |
|
537 |
by (simp add: l) |
|
538 |
then have \<open>P (of_nat n)\<close> |
|
539 |
proof (induction n rule: nat_bit_induct) |
|
540 |
case zero |
|
541 |
show ?case |
|
542 |
by simp (rule word_zero) |
|
543 |
next |
|
544 |
case (even n) |
|
545 |
then have \<open>n < 2 ^ m\<close> |
|
546 |
by simp |
|
547 |
with even.IH have \<open>P (of_nat n)\<close> |
|
548 |
by simp |
|
549 |
moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close> |
|
550 |
by (auto simp add: word_greater_zero_iff of_nat_word_eq_0_iff l) |
|
551 |
moreover from \<open>n < 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close> |
|
552 |
using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>] |
|
553 |
by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l) |
|
554 |
ultimately have \<open>P (2 * of_nat n)\<close> |
|
555 |
by (rule word_even) |
|
556 |
then show ?case |
|
557 |
by simp |
|
558 |
next |
|
559 |
case (odd n) |
|
560 |
then have \<open>Suc n \<le> 2 ^ m\<close> |
|
561 |
by simp |
|
562 |
with odd.IH have \<open>P (of_nat n)\<close> |
|
563 |
by simp |
|
564 |
moreover from \<open>Suc n \<le> 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close> |
|
565 |
using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>] |
|
566 |
by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l) |
|
567 |
ultimately have \<open>P (1 + 2 * of_nat n)\<close> |
|
568 |
by (rule word_odd) |
|
569 |
then show ?case |
|
570 |
by simp |
|
571 |
qed |
|
572 |
moreover have \<open>of_nat (nat (uint a)) = a\<close> |
|
573 |
by transfer simp |
|
574 |
ultimately show ?thesis |
|
575 |
by (simp add: n_def unat_def) |
|
576 |
qed |
|
577 |
||
578 |
lemma bit_word_half_eq: |
|
579 |
\<open>(of_bool b + a * 2) div 2 = a\<close> |
|
580 |
if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close> |
|
581 |
for a :: \<open>'a::len word\<close> |
|
582 |
proof (cases \<open>2 \<le> LENGTH('a::len)\<close>) |
|
583 |
case False |
|
584 |
have \<open>of_bool (odd k) < (1 :: int) \<longleftrightarrow> even k\<close> for k :: int |
|
585 |
by auto |
|
586 |
with False that show ?thesis |
|
587 |
by transfer (simp add: eq_iff) |
|
588 |
next |
|
589 |
case True |
|
590 |
obtain n where length: \<open>LENGTH('a) = Suc n\<close> |
|
591 |
by (cases \<open>LENGTH('a)\<close>) simp_all |
|
592 |
show ?thesis proof (cases b) |
|
593 |
case False |
|
594 |
moreover have \<open>a * 2 div 2 = a\<close> |
|
595 |
using that proof transfer |
|
596 |
fix k :: int |
|
597 |
from length have \<open>k * 2 mod 2 ^ LENGTH('a) = (k mod 2 ^ n) * 2\<close> |
|
598 |
by simp |
|
599 |
moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close> |
|
600 |
with \<open>LENGTH('a) = Suc n\<close> |
|
601 |
have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close> |
|
602 |
by (simp add: take_bit_eq_mod divmod_digit_0) |
|
603 |
ultimately have \<open>take_bit LENGTH('a) (k * 2) = take_bit LENGTH('a) k * 2\<close> |
|
604 |
by (simp add: take_bit_eq_mod) |
|
605 |
with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (k * 2) div take_bit LENGTH('a) 2) |
|
606 |
= take_bit LENGTH('a) k\<close> |
|
607 |
by simp |
|
608 |
qed |
|
609 |
ultimately show ?thesis |
|
610 |
by simp |
|
611 |
next |
|
612 |
case True |
|
613 |
moreover have \<open>(1 + a * 2) div 2 = a\<close> |
|
614 |
using that proof transfer |
|
615 |
fix k :: int |
|
616 |
from length have \<open>(1 + k * 2) mod 2 ^ LENGTH('a) = 1 + (k mod 2 ^ n) * 2\<close> |
|
617 |
using pos_zmod_mult_2 [of \<open>2 ^ n\<close> k] by (simp add: ac_simps) |
|
618 |
moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close> |
|
619 |
with \<open>LENGTH('a) = Suc n\<close> |
|
620 |
have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close> |
|
621 |
by (simp add: take_bit_eq_mod divmod_digit_0) |
|
622 |
ultimately have \<open>take_bit LENGTH('a) (1 + k * 2) = 1 + take_bit LENGTH('a) k * 2\<close> |
|
623 |
by (simp add: take_bit_eq_mod) |
|
624 |
with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (1 + k * 2) div take_bit LENGTH('a) 2) |
|
625 |
= take_bit LENGTH('a) k\<close> |
|
626 |
by (auto simp add: take_bit_Suc) |
|
627 |
qed |
|
628 |
ultimately show ?thesis |
|
629 |
by simp |
|
630 |
qed |
|
631 |
qed |
|
632 |
||
633 |
lemma even_mult_exp_div_word_iff: |
|
634 |
\<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> ( |
|
635 |
m \<le> n \<and> |
|
636 |
n < LENGTH('a) \<and> odd (a div 2 ^ (n - m)))\<close> for a :: \<open>'a::len word\<close> |
|
637 |
by transfer |
|
638 |
(auto simp flip: drop_bit_eq_div simp add: even_drop_bit_iff_not_bit bit_take_bit_iff, |
|
639 |
simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int) |
|
640 |
||
641 |
instance word :: (len) semiring_bits |
|
642 |
proof |
|
643 |
show \<open>P a\<close> if stable: \<open>\<And>a. a div 2 = a \<Longrightarrow> P a\<close> |
|
644 |
and rec: \<open>\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)\<close> |
|
645 |
for P and a :: \<open>'a word\<close> |
|
646 |
proof (induction a rule: word_bit_induct) |
|
647 |
case zero |
|
648 |
have \<open>0 div 2 = (0::'a word)\<close> |
|
649 |
by transfer simp |
|
650 |
with stable [of 0] show ?case |
|
651 |
by simp |
|
652 |
next |
|
653 |
case (even a) |
|
654 |
with rec [of a False] show ?case |
|
655 |
using bit_word_half_eq [of a False] by (simp add: ac_simps) |
|
656 |
next |
|
657 |
case (odd a) |
|
658 |
with rec [of a True] show ?case |
|
659 |
using bit_word_half_eq [of a True] by (simp add: ac_simps) |
|
660 |
qed |
|
661 |
show \<open>0 div a = 0\<close> |
|
662 |
for a :: \<open>'a word\<close> |
|
663 |
by transfer simp |
|
664 |
show \<open>a div 1 = a\<close> |
|
665 |
for a :: \<open>'a word\<close> |
|
666 |
by transfer simp |
|
667 |
show \<open>a mod b div b = 0\<close> |
|
668 |
for a b :: \<open>'a word\<close> |
|
669 |
apply transfer |
|
670 |
apply (simp add: take_bit_eq_mod) |
|
671 |
apply (subst (3) mod_pos_pos_trivial [of _ \<open>2 ^ LENGTH('a)\<close>]) |
|
672 |
apply simp_all |
|
673 |
apply (metis le_less mod_by_0 pos_mod_conj zero_less_numeral zero_less_power) |
|
674 |
using pos_mod_bound [of \<open>2 ^ LENGTH('a)\<close>] apply simp |
|
675 |
proof - |
|
676 |
fix aa :: int and ba :: int |
|
677 |
have f1: "\<And>i n. (i::int) mod 2 ^ n = 0 \<or> 0 < i mod 2 ^ n" |
|
678 |
by (metis le_less take_bit_eq_mod take_bit_nonnegative) |
|
679 |
have "(0::int) < 2 ^ len_of (TYPE('a)::'a itself) \<and> ba mod 2 ^ len_of (TYPE('a)::'a itself) \<noteq> 0 \<or> aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)" |
|
680 |
by (metis (no_types) mod_by_0 unique_euclidean_semiring_numeral_class.pos_mod_bound zero_less_numeral zero_less_power) |
|
681 |
then show "aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)" |
|
682 |
using f1 by (meson le_less less_le_trans unique_euclidean_semiring_numeral_class.pos_mod_bound) |
|
683 |
qed |
|
684 |
show \<open>(1 + a) div 2 = a div 2\<close> |
|
685 |
if \<open>even a\<close> |
|
686 |
for a :: \<open>'a word\<close> |
|
71953 | 687 |
using that by transfer |
688 |
(auto dest: le_Suc_ex simp add: mod_2_eq_odd take_bit_Suc elim!: evenE) |
|
71951 | 689 |
show \<open>(2 :: 'a word) ^ m div 2 ^ n = of_bool ((2 :: 'a word) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m - n)\<close> |
690 |
for m n :: nat |
|
691 |
by transfer (simp, simp add: exp_div_exp_eq) |
|
692 |
show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)" |
|
693 |
for a :: "'a word" and m n :: nat |
|
694 |
apply transfer |
|
695 |
apply (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: drop_bit_eq_div) |
|
696 |
apply (simp add: drop_bit_take_bit) |
|
697 |
done |
|
698 |
show "a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n" |
|
699 |
for a :: "'a word" and m n :: nat |
|
700 |
by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps) |
|
701 |
show \<open>a * 2 ^ m mod 2 ^ n = a mod 2 ^ (n - m) * 2 ^ m\<close> |
|
702 |
if \<open>m \<le> n\<close> for a :: "'a word" and m n :: nat |
|
703 |
using that apply transfer |
|
704 |
apply (auto simp flip: take_bit_eq_mod) |
|
705 |
apply (auto simp flip: push_bit_eq_mult simp add: push_bit_take_bit split: split_min_lin) |
|
706 |
done |
|
707 |
show \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close> |
|
708 |
for a :: "'a word" and m n :: nat |
|
709 |
by transfer (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: take_bit_eq_mod drop_bit_eq_div split: split_min_lin) |
|
710 |
show \<open>even ((2 ^ m - 1) div (2::'a word) ^ n) \<longleftrightarrow> 2 ^ n = (0::'a word) \<or> m \<le> n\<close> |
|
711 |
for m n :: nat |
|
712 |
by transfer (auto simp add: take_bit_of_mask even_mask_div_iff) |
|
713 |
show \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::'a word) ^ n = 0 \<or> m \<le> n \<and> even (a div 2 ^ (n - m))\<close> |
|
714 |
for a :: \<open>'a word\<close> and m n :: nat |
|
715 |
proof transfer |
|
716 |
show \<open>even (take_bit LENGTH('a) (k * 2 ^ m) div take_bit LENGTH('a) (2 ^ n)) \<longleftrightarrow> |
|
717 |
n < m |
|
718 |
\<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0 |
|
719 |
\<or> (m \<le> n \<and> even (take_bit LENGTH('a) k div take_bit LENGTH('a) (2 ^ (n - m))))\<close> |
|
720 |
for m n :: nat and k l :: int |
|
721 |
by (auto simp flip: take_bit_eq_mod drop_bit_eq_div push_bit_eq_mult |
|
722 |
simp add: div_push_bit_of_1_eq_drop_bit drop_bit_take_bit drop_bit_push_bit_int [of n m]) |
|
723 |
qed |
|
724 |
qed |
|
725 |
||
726 |
context |
|
727 |
includes lifting_syntax |
|
728 |
begin |
|
729 |
||
730 |
lemma transfer_rule_bit_word [transfer_rule]: |
|
731 |
\<open>((pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool) ===> (=)) (\<lambda>k n. n < LENGTH('a) \<and> bit k n) bit\<close> |
|
732 |
proof - |
|
733 |
let ?t = \<open>\<lambda>a n. odd (take_bit LENGTH('a) a div take_bit LENGTH('a) ((2::int) ^ n))\<close> |
|
734 |
have \<open>((pcr_word :: int \<Rightarrow> 'a word \<Rightarrow> bool) ===> (=)) ?t bit\<close> |
|
735 |
by (unfold bit_def) transfer_prover |
|
736 |
also have \<open>?t = (\<lambda>k n. n < LENGTH('a) \<and> bit k n)\<close> |
|
737 |
by (simp add: fun_eq_iff bit_take_bit_iff flip: bit_def) |
|
738 |
finally show ?thesis . |
|
739 |
qed |
|
740 |
||
741 |
end |
|
742 |
||
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
743 |
instantiation word :: (len) semiring_bit_shifts |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
744 |
begin |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
745 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
746 |
lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
747 |
is push_bit |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
748 |
proof - |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
749 |
show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
750 |
if \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> for k l :: int and n :: nat |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
751 |
proof - |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
752 |
from that |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
753 |
have \<open>take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
754 |
= take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
755 |
by simp |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
756 |
moreover have \<open>min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
757 |
by simp |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
758 |
ultimately show ?thesis |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
759 |
by (simp add: take_bit_push_bit) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
760 |
qed |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
761 |
qed |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
762 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
763 |
lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
764 |
is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
765 |
by (simp add: take_bit_eq_mod) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
766 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
767 |
instance proof |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
768 |
show \<open>push_bit n a = a * 2 ^ n\<close> for n :: nat and a :: "'a word" |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
769 |
by transfer (simp add: push_bit_eq_mult) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
770 |
show \<open>drop_bit n a = a div 2 ^ n\<close> for n :: nat and a :: "'a word" |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
771 |
by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
772 |
qed |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
773 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
774 |
end |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
775 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
776 |
definition shiftl1 :: "'a::len word \<Rightarrow> 'a word" |
70191 | 777 |
where "shiftl1 w = word_of_int (uint w BIT False)" |
778 |
||
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
779 |
lemma shiftl1_eq_mult_2: |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
780 |
\<open>shiftl1 = (*) 2\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
781 |
apply (simp add: fun_eq_iff shiftl1_def Bit_def) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
782 |
apply (simp only: mult_2) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
783 |
apply transfer |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
784 |
apply (simp only: take_bit_add) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
785 |
apply simp |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
786 |
done |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
787 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
788 |
definition shiftr1 :: "'a::len word \<Rightarrow> 'a word" |
70191 | 789 |
\<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close> |
790 |
where "shiftr1 w = word_of_int (bin_rest (uint w))" |
|
791 |
||
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
792 |
lemma shiftr1_eq_div_2: |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
793 |
\<open>shiftr1 w = w div 2\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
794 |
apply (simp add: fun_eq_iff shiftr1_def) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
795 |
apply transfer |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
796 |
apply (auto simp add: not_le dest: less_2_cases) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
797 |
done |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
798 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
799 |
instantiation word :: (len) bit_operations |
37660 | 800 |
begin |
801 |
||
71826 | 802 |
lift_definition bitNOT_word :: "'a word \<Rightarrow> 'a word" is NOT |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
803 |
by (metis bin_trunc_not) |
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
804 |
|
71826 | 805 |
lift_definition bitAND_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is \<open>(AND)\<close> |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
806 |
by (metis bin_trunc_and) |
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
807 |
|
71826 | 808 |
lift_definition bitOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is \<open>(OR)\<close> |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
809 |
by (metis bin_trunc_or) |
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
810 |
|
71826 | 811 |
lift_definition bitXOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is \<open>(XOR)\<close> |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
812 |
by (metis bin_trunc_xor) |
37660 | 813 |
|
65268 | 814 |
definition word_test_bit_def: "test_bit a = bin_nth (uint a)" |
815 |
||
816 |
definition word_set_bit_def: "set_bit a n x = word_of_int (bin_sc n x (uint a))" |
|
817 |
||
818 |
definition word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a)" |
|
37660 | 819 |
|
70175 | 820 |
definition "msb a \<longleftrightarrow> bin_sign (sbintrunc (LENGTH('a) - 1) (uint a)) = - 1" |
821 |
||
65268 | 822 |
definition shiftl_def: "w << n = (shiftl1 ^^ n) w" |
823 |
||
824 |
definition shiftr_def: "w >> n = (shiftr1 ^^ n) w" |
|
37660 | 825 |
|
826 |
instance .. |
|
827 |
||
828 |
end |
|
829 |
||
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
830 |
lemma test_bit_word_eq: |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
831 |
\<open>test_bit w = bit w\<close> for w :: \<open>'a::len word\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
832 |
apply (simp add: word_test_bit_def fun_eq_iff) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
833 |
apply transfer |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
834 |
apply (simp add: bit_take_bit_iff) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
835 |
done |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
836 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
837 |
lemma lsb_word_eq: |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
838 |
\<open>lsb = (odd :: 'a word \<Rightarrow> bool)\<close> for w :: \<open>'a::len word\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
839 |
apply (simp add: word_lsb_def fun_eq_iff) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
840 |
apply transfer |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
841 |
apply simp |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
842 |
done |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
843 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
844 |
lemma msb_word_eq: |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
845 |
\<open>msb w \<longleftrightarrow> bit w (LENGTH('a) - 1)\<close> for w :: \<open>'a::len word\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
846 |
apply (simp add: msb_word_def bin_sign_lem) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
847 |
apply transfer |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
848 |
apply (simp add: bit_take_bit_iff) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
849 |
done |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
850 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
851 |
lemma shiftl_word_eq: |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
852 |
\<open>w << n = push_bit n w\<close> for w :: \<open>'a::len word\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
853 |
by (induction n) (simp_all add: shiftl_def shiftl1_eq_mult_2 push_bit_double) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
854 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
855 |
lemma shiftr_word_eq: |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
856 |
\<open>w >> n = drop_bit n w\<close> for w :: \<open>'a::len word\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
857 |
by (induction n) (simp_all add: shiftr_def shiftr1_eq_div_2 drop_bit_Suc drop_bit_half) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
858 |
|
70175 | 859 |
lemma word_msb_def: |
860 |
"msb a \<longleftrightarrow> bin_sign (sint a) = - 1" |
|
861 |
by (simp add: msb_word_def sint_uint) |
|
862 |
||
65268 | 863 |
lemma [code]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
864 |
shows word_not_def: "NOT (a::'a::len word) = word_of_int (NOT (uint a))" |
65268 | 865 |
and word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)" |
866 |
and word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)" |
|
867 |
and word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)" |
|
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
868 |
by (simp_all flip: bitNOT_word.abs_eq bitAND_word.abs_eq |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
869 |
bitOR_word.abs_eq bitXOR_word.abs_eq) |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
870 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
871 |
definition setBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word" |
65268 | 872 |
where "setBit w n = set_bit w n True" |
873 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
874 |
definition clearBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word" |
65268 | 875 |
where "clearBit w n = set_bit w n False" |
37660 | 876 |
|
877 |
||
61799 | 878 |
subsection \<open>Shift operations\<close> |
37660 | 879 |
|
65268 | 880 |
definition sshiftr1 :: "'a::len word \<Rightarrow> 'a word" |
881 |
where "sshiftr1 w = word_of_int (bin_rest (sint w))" |
|
882 |
||
883 |
definition bshiftr1 :: "bool \<Rightarrow> 'a::len word \<Rightarrow> 'a word" |
|
884 |
where "bshiftr1 b w = of_bl (b # butlast (to_bl w))" |
|
885 |
||
886 |
definition sshiftr :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word" (infixl ">>>" 55) |
|
887 |
where "w >>> n = (sshiftr1 ^^ n) w" |
|
888 |
||
889 |
definition mask :: "nat \<Rightarrow> 'a::len word" |
|
890 |
where "mask n = (1 << n) - 1" |
|
891 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
892 |
definition revcast :: "'a::len word \<Rightarrow> 'b::len word" |
70185 | 893 |
where "revcast w = of_bl (takefill False (LENGTH('b)) (to_bl w))" |
65268 | 894 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
895 |
definition slice1 :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word" |
65268 | 896 |
where "slice1 n w = of_bl (takefill False n (to_bl w))" |
897 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
898 |
definition slice :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word" |
65268 | 899 |
where "slice n w = slice1 (size w - n) w" |
37660 | 900 |
|
901 |
||
61799 | 902 |
subsection \<open>Rotation\<close> |
37660 | 903 |
|
65268 | 904 |
definition rotater1 :: "'a list \<Rightarrow> 'a list" |
905 |
where "rotater1 ys = |
|
906 |
(case ys of [] \<Rightarrow> [] | x # xs \<Rightarrow> last ys # butlast ys)" |
|
907 |
||
908 |
definition rotater :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
909 |
where "rotater n = rotater1 ^^ n" |
|
910 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
911 |
definition word_rotr :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word" |
65268 | 912 |
where "word_rotr n w = of_bl (rotater n (to_bl w))" |
913 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
914 |
definition word_rotl :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word" |
65268 | 915 |
where "word_rotl n w = of_bl (rotate n (to_bl w))" |
916 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
917 |
definition word_roti :: "int \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word" |
65268 | 918 |
where "word_roti i w = |
919 |
(if i \<ge> 0 then word_rotr (nat i) w else word_rotl (nat (- i)) w)" |
|
37660 | 920 |
|
921 |
||
61799 | 922 |
subsection \<open>Split and cat operations\<close> |
37660 | 923 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
924 |
definition word_cat :: "'a::len word \<Rightarrow> 'b::len word \<Rightarrow> 'c::len word" |
70185 | 925 |
where "word_cat a b = word_of_int (bin_cat (uint a) (LENGTH('b)) (uint b))" |
65268 | 926 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
927 |
definition word_split :: "'a::len word \<Rightarrow> 'b::len word \<times> 'c::len word" |
65268 | 928 |
where "word_split a = |
70185 | 929 |
(case bin_split (LENGTH('c)) (uint a) of |
65268 | 930 |
(u, v) \<Rightarrow> (word_of_int u, word_of_int v))" |
931 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
932 |
definition word_rcat :: "'a::len word list \<Rightarrow> 'b::len word" |
70185 | 933 |
where "word_rcat ws = word_of_int (bin_rcat (LENGTH('a)) (map uint ws))" |
65268 | 934 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
935 |
definition word_rsplit :: "'a::len word \<Rightarrow> 'b::len word list" |
70185 | 936 |
where "word_rsplit w = map word_of_int (bin_rsplit (LENGTH('b)) (LENGTH('a), uint w))" |
65268 | 937 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
938 |
abbreviation (input) max_word :: \<open>'a::len word\<close> |
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
939 |
\<comment> \<open>Largest representable machine integer.\<close> |
71946 | 940 |
where "max_word \<equiv> - 1" |
37660 | 941 |
|
942 |
||
61799 | 943 |
subsection \<open>Theorems about typedefs\<close> |
46010 | 944 |
|
70185 | 945 |
lemma sint_sbintrunc': "sint (word_of_int bin :: 'a word) = sbintrunc (LENGTH('a::len) - 1) bin" |
65268 | 946 |
by (auto simp: sint_uint word_ubin.eq_norm sbintrunc_bintrunc_lt) |
947 |
||
70185 | 948 |
lemma uint_sint: "uint w = bintrunc (LENGTH('a)) (sint w)" |
65328 | 949 |
for w :: "'a::len word" |
65268 | 950 |
by (auto simp: sint_uint bintrunc_sbintrunc_le) |
951 |
||
70185 | 952 |
lemma bintr_uint: "LENGTH('a) \<le> n \<Longrightarrow> bintrunc n (uint w) = uint w" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
953 |
for w :: "'a::len word" |
65268 | 954 |
apply (subst word_ubin.norm_Rep [symmetric]) |
37660 | 955 |
apply (simp only: bintrunc_bintrunc_min word_size) |
54863
82acc20ded73
prefer more canonical names for lemmas on min/max
haftmann
parents:
54854
diff
changeset
|
956 |
apply (simp add: min.absorb2) |
37660 | 957 |
done |
958 |
||
46057 | 959 |
lemma wi_bintr: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
960 |
"LENGTH('a::len) \<le> n \<Longrightarrow> |
46057 | 961 |
word_of_int (bintrunc n w) = (word_of_int w :: 'a word)" |
65268 | 962 |
by (auto simp: word_ubin.norm_eq_iff [symmetric] min.absorb1) |
963 |
||
964 |
lemma td_ext_sbin: |
|
70185 | 965 |
"td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len))) |
966 |
(sbintrunc (LENGTH('a) - 1))" |
|
37660 | 967 |
apply (unfold td_ext_def' sint_uint) |
968 |
apply (simp add : word_ubin.eq_norm) |
|
70185 | 969 |
apply (cases "LENGTH('a)") |
37660 | 970 |
apply (auto simp add : sints_def) |
971 |
apply (rule sym [THEN trans]) |
|
65268 | 972 |
apply (rule word_ubin.Abs_norm) |
37660 | 973 |
apply (simp only: bintrunc_sbintrunc) |
974 |
apply (drule sym) |
|
975 |
apply simp |
|
976 |
done |
|
977 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
978 |
lemma td_ext_sint: |
70185 | 979 |
"td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len))) |
980 |
(\<lambda>w. (w + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) - |
|
981 |
2 ^ (LENGTH('a) - 1))" |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
982 |
using td_ext_sbin [where ?'a = 'a] by (simp add: no_sbintr_alt2) |
37660 | 983 |
|
67408 | 984 |
text \<open> |
985 |
We do \<open>sint\<close> before \<open>sbin\<close>, before \<open>sint\<close> is the user version |
|
986 |
and interpretations do not produce thm duplicates. I.e. |
|
987 |
we get the name \<open>word_sint.Rep_eqD\<close>, but not \<open>word_sbin.Req_eqD\<close>, |
|
988 |
because the latter is the same thm as the former. |
|
989 |
\<close> |
|
37660 | 990 |
interpretation word_sint: |
65268 | 991 |
td_ext |
992 |
"sint ::'a::len word \<Rightarrow> int" |
|
993 |
word_of_int |
|
70185 | 994 |
"sints (LENGTH('a::len))" |
995 |
"\<lambda>w. (w + 2^(LENGTH('a::len) - 1)) mod 2^LENGTH('a::len) - |
|
996 |
2 ^ (LENGTH('a::len) - 1)" |
|
37660 | 997 |
by (rule td_ext_sint) |
998 |
||
999 |
interpretation word_sbin: |
|
65268 | 1000 |
td_ext |
1001 |
"sint ::'a::len word \<Rightarrow> int" |
|
1002 |
word_of_int |
|
70185 | 1003 |
"sints (LENGTH('a::len))" |
1004 |
"sbintrunc (LENGTH('a::len) - 1)" |
|
37660 | 1005 |
by (rule td_ext_sbin) |
1006 |
||
45604 | 1007 |
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] |
37660 | 1008 |
|
1009 |
lemmas td_sint = word_sint.td |
|
1010 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1011 |
lemma to_bl_def': "(to_bl :: 'a::len word \<Rightarrow> bool list) = bin_to_bl (LENGTH('a)) \<circ> uint" |
44762 | 1012 |
by (auto simp: to_bl_def) |
37660 | 1013 |
|
65268 | 1014 |
lemmas word_reverse_no_def [simp] = |
1015 |
word_reverse_def [of "numeral w"] for w |
|
37660 | 1016 |
|
45805 | 1017 |
lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)" |
1018 |
by (fact uints_def [unfolded no_bintr_alt1]) |
|
1019 |
||
65268 | 1020 |
lemma word_numeral_alt: "numeral b = word_of_int (numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1021 |
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
|
1022 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1023 |
declare word_numeral_alt [symmetric, code_abbrev] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1024 |
|
65268 | 1025 |
lemma word_neg_numeral_alt: "- numeral b = word_of_int (- numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
1026 |
by (simp only: word_numeral_alt wi_hom_neg) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1027 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1028 |
declare word_neg_numeral_alt [symmetric, code_abbrev] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1029 |
|
45805 | 1030 |
lemma uint_bintrunc [simp]: |
65268 | 1031 |
"uint (numeral bin :: 'a word) = |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1032 |
bintrunc (LENGTH('a::len)) (numeral bin)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1033 |
unfolding word_numeral_alt by (rule word_ubin.eq_norm) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1034 |
|
65268 | 1035 |
lemma uint_bintrunc_neg [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1036 |
"uint (- numeral bin :: 'a word) = bintrunc (LENGTH('a::len)) (- numeral bin)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1037 |
by (simp only: word_neg_numeral_alt word_ubin.eq_norm) |
37660 | 1038 |
|
45805 | 1039 |
lemma sint_sbintrunc [simp]: |
70185 | 1040 |
"sint (numeral bin :: 'a word) = sbintrunc (LENGTH('a::len) - 1) (numeral bin)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1041 |
by (simp only: word_numeral_alt word_sbin.eq_norm) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1042 |
|
65268 | 1043 |
lemma sint_sbintrunc_neg [simp]: |
70185 | 1044 |
"sint (- numeral bin :: 'a word) = sbintrunc (LENGTH('a::len) - 1) (- numeral bin)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1045 |
by (simp only: word_neg_numeral_alt word_sbin.eq_norm) |
37660 | 1046 |
|
45805 | 1047 |
lemma unat_bintrunc [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1048 |
"unat (numeral bin :: 'a::len word) = nat (bintrunc (LENGTH('a)) (numeral bin))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1049 |
by (simp only: unat_def uint_bintrunc) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1050 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1051 |
lemma unat_bintrunc_neg [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1052 |
"unat (- numeral bin :: 'a::len word) = nat (bintrunc (LENGTH('a)) (- numeral bin))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1053 |
by (simp only: unat_def uint_bintrunc_neg) |
37660 | 1054 |
|
65328 | 1055 |
lemma size_0_eq: "size w = 0 \<Longrightarrow> v = w" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1056 |
for v w :: "'a::len word" |
37660 | 1057 |
apply (unfold word_size) |
1058 |
apply (rule word_uint.Rep_eqD) |
|
1059 |
apply (rule box_equals) |
|
1060 |
defer |
|
1061 |
apply (rule word_ubin.norm_Rep)+ |
|
1062 |
apply simp |
|
1063 |
done |
|
1064 |
||
65268 | 1065 |
lemma uint_ge_0 [iff]: "0 \<le> uint x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1066 |
for x :: "'a::len word" |
45805 | 1067 |
using word_uint.Rep [of x] by (simp add: uints_num) |
1068 |
||
70185 | 1069 |
lemma uint_lt2p [iff]: "uint x < 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1070 |
for x :: "'a::len word" |
45805 | 1071 |
using word_uint.Rep [of x] by (simp add: uints_num) |
1072 |
||
71946 | 1073 |
lemma word_exp_length_eq_0 [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1074 |
\<open>(2 :: 'a::len word) ^ LENGTH('a) = 0\<close> |
71946 | 1075 |
by transfer (simp add: bintrunc_mod2p) |
1076 |
||
70185 | 1077 |
lemma sint_ge: "- (2 ^ (LENGTH('a) - 1)) \<le> sint x" |
65268 | 1078 |
for x :: "'a::len word" |
45805 | 1079 |
using word_sint.Rep [of x] by (simp add: sints_num) |
1080 |
||
70185 | 1081 |
lemma sint_lt: "sint x < 2 ^ (LENGTH('a) - 1)" |
65268 | 1082 |
for x :: "'a::len word" |
45805 | 1083 |
using word_sint.Rep [of x] by (simp add: sints_num) |
37660 | 1084 |
|
65268 | 1085 |
lemma sign_uint_Pls [simp]: "bin_sign (uint x) = 0" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1086 |
by (simp add: sign_Pls_ge_0) |
37660 | 1087 |
|
70185 | 1088 |
lemma uint_m2p_neg: "uint x - 2 ^ LENGTH('a) < 0" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1089 |
for x :: "'a::len word" |
45805 | 1090 |
by (simp only: diff_less_0_iff_less uint_lt2p) |
1091 |
||
70185 | 1092 |
lemma uint_m2p_not_non_neg: "\<not> 0 \<le> uint x - 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1093 |
for x :: "'a::len word" |
45805 | 1094 |
by (simp only: not_le uint_m2p_neg) |
37660 | 1095 |
|
70185 | 1096 |
lemma lt2p_lem: "LENGTH('a) \<le> n \<Longrightarrow> uint w < 2 ^ n" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1097 |
for w :: "'a::len word" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1098 |
by (metis bintr_uint bintrunc_mod2p int_mod_lem zless2p) |
37660 | 1099 |
|
45805 | 1100 |
lemma uint_le_0_iff [simp]: "uint x \<le> 0 \<longleftrightarrow> uint x = 0" |
70749
5d06b7bb9d22
More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
paulson <lp15@cam.ac.uk>
parents:
70342
diff
changeset
|
1101 |
by (fact uint_ge_0 [THEN leD, THEN antisym_conv1]) |
37660 | 1102 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1103 |
lemma uint_nat: "uint w = int (unat w)" |
65268 | 1104 |
by (auto simp: unat_def) |
1105 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1106 |
lemma uint_numeral: "uint (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)" |
65268 | 1107 |
by (simp only: word_numeral_alt int_word_uint) |
1108 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1109 |
lemma uint_neg_numeral: "uint (- numeral b :: 'a::len word) = - numeral b mod 2 ^ LENGTH('a)" |
65268 | 1110 |
by (simp only: word_neg_numeral_alt int_word_uint) |
1111 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1112 |
lemma unat_numeral: "unat (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)" |
37660 | 1113 |
apply (unfold unat_def) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1114 |
apply (clarsimp simp only: uint_numeral) |
37660 | 1115 |
apply (rule nat_mod_distrib [THEN trans]) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1116 |
apply (rule zero_le_numeral) |
37660 | 1117 |
apply (simp_all add: nat_power_eq) |
1118 |
done |
|
1119 |
||
65268 | 1120 |
lemma sint_numeral: |
1121 |
"sint (numeral b :: 'a::len word) = |
|
1122 |
(numeral b + |
|
70185 | 1123 |
2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) - |
1124 |
2 ^ (LENGTH('a) - 1)" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1125 |
unfolding word_numeral_alt by (rule int_word_sint) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1126 |
|
65268 | 1127 |
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0" |
45958 | 1128 |
unfolding word_0_wi .. |
1129 |
||
65268 | 1130 |
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1" |
45958 | 1131 |
unfolding word_1_wi .. |
1132 |
||
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
1133 |
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
|
1134 |
by (simp add: wi_hom_syms) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
1135 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1136 |
lemma word_of_int_numeral [simp] : "(word_of_int (numeral bin) :: 'a::len word) = numeral bin" |
65268 | 1137 |
by (simp only: word_numeral_alt) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1138 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1139 |
lemma word_of_int_neg_numeral [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1140 |
"(word_of_int (- numeral bin) :: 'a::len word) = - numeral bin" |
65268 | 1141 |
by (simp only: word_numeral_alt wi_hom_syms) |
1142 |
||
1143 |
lemma word_int_case_wi: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1144 |
"word_int_case f (word_of_int i :: 'b word) = f (i mod 2 ^ LENGTH('b::len))" |
65268 | 1145 |
by (simp add: word_int_case_def word_uint.eq_norm) |
1146 |
||
1147 |
lemma word_int_split: |
|
1148 |
"P (word_int_case f x) = |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1149 |
(\<forall>i. x = (word_of_int i :: 'b::len word) \<and> 0 \<le> i \<and> i < 2 ^ LENGTH('b) \<longrightarrow> P (f i))" |
71942 | 1150 |
by (auto simp: word_int_case_def word_uint.eq_norm) |
65268 | 1151 |
|
1152 |
lemma word_int_split_asm: |
|
1153 |
"P (word_int_case f x) = |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1154 |
(\<nexists>n. x = (word_of_int n :: 'b::len word) \<and> 0 \<le> n \<and> n < 2 ^ LENGTH('b::len) \<and> \<not> P (f n))" |
71942 | 1155 |
by (auto simp: word_int_case_def word_uint.eq_norm) |
45805 | 1156 |
|
45604 | 1157 |
lemmas uint_range' = word_uint.Rep [unfolded uints_num mem_Collect_eq] |
1158 |
lemmas sint_range' = word_sint.Rep [unfolded One_nat_def sints_num mem_Collect_eq] |
|
37660 | 1159 |
|
65268 | 1160 |
lemma uint_range_size: "0 \<le> uint w \<and> uint w < 2 ^ size w" |
37660 | 1161 |
unfolding word_size by (rule uint_range') |
1162 |
||
65268 | 1163 |
lemma sint_range_size: "- (2 ^ (size w - Suc 0)) \<le> sint w \<and> sint w < 2 ^ (size w - Suc 0)" |
37660 | 1164 |
unfolding word_size by (rule sint_range') |
1165 |
||
65268 | 1166 |
lemma sint_above_size: "2 ^ (size w - 1) \<le> x \<Longrightarrow> sint w < x" |
1167 |
for w :: "'a::len word" |
|
45805 | 1168 |
unfolding word_size by (rule less_le_trans [OF sint_lt]) |
1169 |
||
65268 | 1170 |
lemma sint_below_size: "x \<le> - (2 ^ (size w - 1)) \<Longrightarrow> x \<le> sint w" |
1171 |
for w :: "'a::len word" |
|
45805 | 1172 |
unfolding word_size by (rule order_trans [OF _ sint_ge]) |
37660 | 1173 |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1174 |
|
61799 | 1175 |
subsection \<open>Testing bits\<close> |
46010 | 1176 |
|
65268 | 1177 |
lemma test_bit_eq_iff: "test_bit u = test_bit v \<longleftrightarrow> u = v" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1178 |
for u v :: "'a::len word" |
37660 | 1179 |
unfolding word_test_bit_def by (simp add: bin_nth_eq_iff) |
1180 |
||
65268 | 1181 |
lemma test_bit_size [rule_format] : "w !! n \<longrightarrow> n < size w" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1182 |
for w :: "'a::len word" |
37660 | 1183 |
apply (unfold word_test_bit_def) |
1184 |
apply (subst word_ubin.norm_Rep [symmetric]) |
|
1185 |
apply (simp only: nth_bintr word_size) |
|
1186 |
apply fast |
|
1187 |
done |
|
1188 |
||
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1189 |
lemma word_eq_iff: "x = y \<longleftrightarrow> (\<forall>n<LENGTH('a). x !! n = y !! n)" (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1190 |
for x y :: "'a::len word" |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1191 |
proof |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1192 |
assume ?P |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1193 |
then show ?Q |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1194 |
by simp |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1195 |
next |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1196 |
assume ?Q |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1197 |
then have *: \<open>bit (uint x) n \<longleftrightarrow> bit (uint y) n\<close> if \<open>n < LENGTH('a)\<close> for n |
71949 | 1198 |
using that by (simp add: word_test_bit_def) |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1199 |
show ?P |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1200 |
proof (rule word_uint_eqI, rule bit_eqI, rule iffI) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1201 |
fix n |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1202 |
assume \<open>bit (uint x) n\<close> |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1203 |
then have \<open>n < LENGTH('a)\<close> |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1204 |
by (simp add: bit_take_bit_iff uint.rep_eq) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1205 |
with * \<open>bit (uint x) n\<close> |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1206 |
show \<open>bit (uint y) n\<close> |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1207 |
by simp |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1208 |
next |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1209 |
fix n |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1210 |
assume \<open>bit (uint y) n\<close> |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1211 |
then have \<open>n < LENGTH('a)\<close> |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1212 |
by (simp add: bit_take_bit_iff uint.rep_eq) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1213 |
with * \<open>bit (uint y) n\<close> |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1214 |
show \<open>bit (uint x) n\<close> |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1215 |
by simp |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1216 |
qed |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
1217 |
qed |
46021 | 1218 |
|
65268 | 1219 |
lemma word_eqI: "(\<And>n. n < size u \<longrightarrow> u !! n = v !! n) \<Longrightarrow> u = v" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1220 |
for u :: "'a::len word" |
46021 | 1221 |
by (simp add: word_size word_eq_iff) |
37660 | 1222 |
|
65268 | 1223 |
lemma word_eqD: "u = v \<Longrightarrow> u !! x = v !! x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1224 |
for u v :: "'a::len word" |
45805 | 1225 |
by simp |
37660 | 1226 |
|
65268 | 1227 |
lemma test_bit_bin': "w !! n \<longleftrightarrow> n < size w \<and> bin_nth (uint w) n" |
1228 |
by (simp add: word_test_bit_def word_size nth_bintr [symmetric]) |
|
37660 | 1229 |
|
1230 |
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] |
|
1231 |
||
70185 | 1232 |
lemma bin_nth_uint_imp: "bin_nth (uint w) n \<Longrightarrow> n < LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1233 |
for w :: "'a::len word" |
37660 | 1234 |
apply (rule nth_bintr [THEN iffD1, THEN conjunct1]) |
1235 |
apply (subst word_ubin.norm_Rep) |
|
1236 |
apply assumption |
|
1237 |
done |
|
1238 |
||
46057 | 1239 |
lemma bin_nth_sint: |
70185 | 1240 |
"LENGTH('a) \<le> n \<Longrightarrow> |
1241 |
bin_nth (sint w) n = bin_nth (sint w) (LENGTH('a) - 1)" |
|
65268 | 1242 |
for w :: "'a::len word" |
37660 | 1243 |
apply (subst word_sbin.norm_Rep [symmetric]) |
46057 | 1244 |
apply (auto simp add: nth_sbintr) |
37660 | 1245 |
done |
1246 |
||
67408 | 1247 |
\<comment> \<open>type definitions theorem for in terms of equivalent bool list\<close> |
65268 | 1248 |
lemma td_bl: |
1249 |
"type_definition |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1250 |
(to_bl :: 'a::len word \<Rightarrow> bool list) |
65268 | 1251 |
of_bl |
70185 | 1252 |
{bl. length bl = LENGTH('a)}" |
37660 | 1253 |
apply (unfold type_definition_def of_bl_def to_bl_def) |
1254 |
apply (simp add: word_ubin.eq_norm) |
|
1255 |
apply safe |
|
1256 |
apply (drule sym) |
|
1257 |
apply simp |
|
1258 |
done |
|
1259 |
||
1260 |
interpretation word_bl: |
|
65268 | 1261 |
type_definition |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1262 |
"to_bl :: 'a::len word \<Rightarrow> bool list" |
65268 | 1263 |
of_bl |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1264 |
"{bl. length bl = LENGTH('a::len)}" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1265 |
by (fact td_bl) |
37660 | 1266 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
1267 |
lemmas word_bl_Rep' = word_bl.Rep [unfolded mem_Collect_eq, iff] |
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
1268 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1269 |
lemma word_size_bl: "size w = size (to_bl w)" |
65268 | 1270 |
by (auto simp: word_size) |
1271 |
||
1272 |
lemma to_bl_use_of_bl: "to_bl w = bl \<longleftrightarrow> w = of_bl bl \<and> length bl = length (to_bl w)" |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
1273 |
by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq]) |
37660 | 1274 |
|
1275 |
lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)" |
|
65268 | 1276 |
by (simp add: word_reverse_def word_bl.Abs_inverse) |
37660 | 1277 |
|
1278 |
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w" |
|
65268 | 1279 |
by (simp add: word_reverse_def word_bl.Abs_inverse) |
37660 | 1280 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1281 |
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
|
1282 |
by (metis word_rev_rev) |
37660 | 1283 |
|
45805 | 1284 |
lemma word_rev_gal': "u = word_reverse w \<Longrightarrow> w = word_reverse u" |
1285 |
by simp |
|
1286 |
||
65268 | 1287 |
lemma length_bl_gt_0 [iff]: "0 < length (to_bl x)" |
1288 |
for x :: "'a::len word" |
|
45805 | 1289 |
unfolding word_bl_Rep' by (rule len_gt_0) |
1290 |
||
65268 | 1291 |
lemma bl_not_Nil [iff]: "to_bl x \<noteq> []" |
1292 |
for x :: "'a::len word" |
|
45805 | 1293 |
by (fact length_bl_gt_0 [unfolded length_greater_0_conv]) |
1294 |
||
65268 | 1295 |
lemma length_bl_neq_0 [iff]: "length (to_bl x) \<noteq> 0" |
1296 |
for x :: "'a::len word" |
|
45805 | 1297 |
by (fact length_bl_gt_0 [THEN gr_implies_not0]) |
37660 | 1298 |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
1299 |
lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = -1)" |
37660 | 1300 |
apply (unfold to_bl_def sint_uint) |
1301 |
apply (rule trans [OF _ bl_sbin_sign]) |
|
1302 |
apply simp |
|
1303 |
done |
|
1304 |
||
65268 | 1305 |
lemma of_bl_drop': |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1306 |
"lend = length bl - LENGTH('a::len) \<Longrightarrow> |
37660 | 1307 |
of_bl (drop lend bl) = (of_bl bl :: 'a word)" |
65268 | 1308 |
by (auto simp: of_bl_def trunc_bl2bin [symmetric]) |
1309 |
||
1310 |
lemma test_bit_of_bl: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1311 |
"(of_bl bl::'a::len word) !! n = (rev bl ! n \<and> n < LENGTH('a) \<and> n < length bl)" |
65328 | 1312 |
by (auto simp add: of_bl_def word_test_bit_def word_size |
1313 |
word_ubin.eq_norm nth_bintr bin_nth_of_bl) |
|
65268 | 1314 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1315 |
lemma no_of_bl: "(numeral bin ::'a::len word) = of_bl (bin_to_bl (LENGTH('a)) (numeral bin))" |
65268 | 1316 |
by (simp add: of_bl_def) |
37660 | 1317 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1318 |
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)" |
65268 | 1319 |
by (auto simp: word_size to_bl_def) |
37660 | 1320 |
|
1321 |
lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w" |
|
65268 | 1322 |
by (simp add: uint_bl word_size) |
1323 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1324 |
lemma to_bl_of_bin: "to_bl (word_of_int bin::'a::len word) = bin_to_bl (LENGTH('a)) bin" |
65268 | 1325 |
by (auto simp: uint_bl word_ubin.eq_norm word_size) |
37660 | 1326 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1327 |
lemma to_bl_numeral [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1328 |
"to_bl (numeral bin::'a::len word) = |
70185 | 1329 |
bin_to_bl (LENGTH('a)) (numeral bin)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1330 |
unfolding word_numeral_alt by (rule to_bl_of_bin) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1331 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1332 |
lemma to_bl_neg_numeral [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1333 |
"to_bl (- numeral bin::'a::len word) = |
70185 | 1334 |
bin_to_bl (LENGTH('a)) (- numeral bin)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1335 |
unfolding word_neg_numeral_alt by (rule to_bl_of_bin) |
37660 | 1336 |
|
1337 |
lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w" |
|
65268 | 1338 |
by (simp add: uint_bl word_size) |
1339 |
||
70185 | 1340 |
lemma uint_bl_bin: "bl_to_bin (bin_to_bl (LENGTH('a)) (uint x)) = uint x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1341 |
for x :: "'a::len word" |
46011 | 1342 |
by (rule trans [OF bin_bl_bin word_ubin.norm_Rep]) |
45604 | 1343 |
|
67408 | 1344 |
\<comment> \<open>naturals\<close> |
37660 | 1345 |
lemma uints_unats: "uints n = int ` unats n" |
1346 |
apply (unfold unats_def uints_num) |
|
1347 |
apply safe |
|
65268 | 1348 |
apply (rule_tac image_eqI) |
1349 |
apply (erule_tac nat_0_le [symmetric]) |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
66808
diff
changeset
|
1350 |
by auto |
37660 | 1351 |
|
1352 |
lemma unats_uints: "unats n = nat ` uints n" |
|
65268 | 1353 |
by (auto simp: uints_unats image_iff) |
1354 |
||
1355 |
lemmas bintr_num = |
|
1356 |
word_ubin.norm_eq_iff [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b |
|
1357 |
lemmas sbintr_num = |
|
1358 |
word_sbin.norm_eq_iff [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b |
|
37660 | 1359 |
|
1360 |
lemma num_of_bintr': |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1361 |
"bintrunc (LENGTH('a::len)) (numeral a) = (numeral b) \<Longrightarrow> |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1362 |
numeral a = (numeral b :: 'a word)" |
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
1363 |
unfolding bintr_num by (erule subst, simp) |
37660 | 1364 |
|
1365 |
lemma num_of_sbintr': |
|
70185 | 1366 |
"sbintrunc (LENGTH('a::len) - 1) (numeral a) = (numeral b) \<Longrightarrow> |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1367 |
numeral a = (numeral b :: 'a word)" |
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
1368 |
unfolding sbintr_num by (erule subst, simp) |
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
1369 |
|
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
1370 |
lemma num_abs_bintr: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1371 |
"(numeral x :: 'a word) = |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1372 |
word_of_int (bintrunc (LENGTH('a::len)) (numeral x))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1373 |
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
|
1374 |
|
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
1375 |
lemma num_abs_sbintr: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1376 |
"(numeral x :: 'a word) = |
70185 | 1377 |
word_of_int (sbintrunc (LENGTH('a::len) - 1) (numeral x))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1378 |
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
|
1379 |
|
67408 | 1380 |
text \<open> |
1381 |
\<open>cast\<close> -- note, no arg for new length, as it's determined by type of result, |
|
1382 |
thus in \<open>cast w = w\<close>, the type means cast to length of \<open>w\<close>! |
|
1383 |
\<close> |
|
37660 | 1384 |
|
1385 |
lemma ucast_id: "ucast w = w" |
|
65268 | 1386 |
by (auto simp: ucast_def) |
37660 | 1387 |
|
1388 |
lemma scast_id: "scast w = w" |
|
65268 | 1389 |
by (auto simp: scast_def) |
37660 | 1390 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1391 |
lemma ucast_bl: "ucast w = of_bl (to_bl w)" |
65268 | 1392 |
by (auto simp: ucast_def of_bl_def uint_bl word_size) |
1393 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1394 |
lemma nth_ucast: "(ucast w::'a::len word) !! n = (w !! n \<and> n < LENGTH('a))" |
65268 | 1395 |
by (simp add: ucast_def test_bit_bin word_ubin.eq_norm nth_bintr word_size) |
1396 |
(fast elim!: bin_nth_uint_imp) |
|
37660 | 1397 |
|
67408 | 1398 |
\<comment> \<open>literal u(s)cast\<close> |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
1399 |
lemma ucast_bintr [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1400 |
"ucast (numeral w :: 'a::len word) = |
70185 | 1401 |
word_of_int (bintrunc (LENGTH('a)) (numeral w))" |
65268 | 1402 |
by (simp add: ucast_def) |
1403 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1404 |
(* TODO: neg_numeral *) |
37660 | 1405 |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
1406 |
lemma scast_sbintr [simp]: |
65268 | 1407 |
"scast (numeral w ::'a::len word) = |
70185 | 1408 |
word_of_int (sbintrunc (LENGTH('a) - Suc 0) (numeral w))" |
65268 | 1409 |
by (simp add: scast_def) |
37660 | 1410 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1411 |
lemma source_size: "source_size (c::'a::len word \<Rightarrow> _) = LENGTH('a)" |
46011 | 1412 |
unfolding source_size_def word_size Let_def .. |
1413 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1414 |
lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len word) = LENGTH('b)" |
46011 | 1415 |
unfolding target_size_def word_size Let_def .. |
1416 |
||
70185 | 1417 |
lemma is_down: "is_down c \<longleftrightarrow> LENGTH('b) \<le> LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1418 |
for c :: "'a::len word \<Rightarrow> 'b::len word" |
65268 | 1419 |
by (simp only: is_down_def source_size target_size) |
1420 |
||
70185 | 1421 |
lemma is_up: "is_up c \<longleftrightarrow> LENGTH('a) \<le> LENGTH('b)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1422 |
for c :: "'a::len word \<Rightarrow> 'b::len word" |
65268 | 1423 |
by (simp only: is_up_def source_size target_size) |
37660 | 1424 |
|
45604 | 1425 |
lemmas is_up_down = trans [OF is_up is_down [symmetric]] |
37660 | 1426 |
|
45811 | 1427 |
lemma down_cast_same [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc = scast" |
37660 | 1428 |
apply (unfold is_down) |
1429 |
apply safe |
|
1430 |
apply (rule ext) |
|
1431 |
apply (unfold ucast_def scast_def uint_sint) |
|
1432 |
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
|
1433 |
apply simp |
|
1434 |
done |
|
1435 |
||
45811 | 1436 |
lemma word_rev_tf: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1437 |
"to_bl (of_bl bl::'a::len word) = |
70185 | 1438 |
rev (takefill False (LENGTH('a)) (rev bl))" |
65268 | 1439 |
by (auto simp: of_bl_def uint_bl bl_bin_bl_rtf word_ubin.eq_norm word_size) |
37660 | 1440 |
|
45811 | 1441 |
lemma word_rep_drop: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1442 |
"to_bl (of_bl bl::'a::len word) = |
70185 | 1443 |
replicate (LENGTH('a) - length bl) False @ |
1444 |
drop (length bl - LENGTH('a)) bl" |
|
45811 | 1445 |
by (simp add: word_rev_tf takefill_alt rev_take) |
37660 | 1446 |
|
65268 | 1447 |
lemma to_bl_ucast: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1448 |
"to_bl (ucast (w::'b::len word) ::'a::len word) = |
70185 | 1449 |
replicate (LENGTH('a) - LENGTH('b)) False @ |
1450 |
drop (LENGTH('b) - LENGTH('a)) (to_bl w)" |
|
37660 | 1451 |
apply (unfold ucast_bl) |
1452 |
apply (rule trans) |
|
1453 |
apply (rule word_rep_drop) |
|
1454 |
apply simp |
|
1455 |
done |
|
1456 |
||
45811 | 1457 |
lemma ucast_up_app [OF refl]: |
65268 | 1458 |
"uc = ucast \<Longrightarrow> source_size uc + n = target_size uc \<Longrightarrow> |
37660 | 1459 |
to_bl (uc w) = replicate n False @ (to_bl w)" |
1460 |
by (auto simp add : source_size target_size to_bl_ucast) |
|
1461 |
||
45811 | 1462 |
lemma ucast_down_drop [OF refl]: |
65268 | 1463 |
"uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> |
37660 | 1464 |
to_bl (uc w) = drop n (to_bl w)" |
1465 |
by (auto simp add : source_size target_size to_bl_ucast) |
|
1466 |
||
45811 | 1467 |
lemma scast_down_drop [OF refl]: |
65268 | 1468 |
"sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> |
37660 | 1469 |
to_bl (sc w) = drop n (to_bl w)" |
1470 |
apply (subgoal_tac "sc = ucast") |
|
1471 |
apply safe |
|
1472 |
apply simp |
|
45811 | 1473 |
apply (erule ucast_down_drop) |
1474 |
apply (rule down_cast_same [symmetric]) |
|
37660 | 1475 |
apply (simp add : source_size target_size is_down) |
1476 |
done |
|
1477 |
||
65268 | 1478 |
lemma sint_up_scast [OF refl]: "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w" |
37660 | 1479 |
apply (unfold is_up) |
1480 |
apply safe |
|
1481 |
apply (simp add: scast_def word_sbin.eq_norm) |
|
1482 |
apply (rule box_equals) |
|
1483 |
prefer 3 |
|
1484 |
apply (rule word_sbin.norm_Rep) |
|
1485 |
apply (rule sbintrunc_sbintrunc_l) |
|
1486 |
defer |
|
1487 |
apply (subst word_sbin.norm_Rep) |
|
1488 |
apply (rule refl) |
|
1489 |
apply simp |
|
1490 |
done |
|
1491 |
||
65268 | 1492 |
lemma uint_up_ucast [OF refl]: "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w" |
37660 | 1493 |
apply (unfold is_up) |
1494 |
apply safe |
|
1495 |
apply (rule bin_eqI) |
|
1496 |
apply (fold word_test_bit_def) |
|
1497 |
apply (auto simp add: nth_ucast) |
|
1498 |
apply (auto simp add: test_bit_bin) |
|
1499 |
done |
|
45811 | 1500 |
|
65268 | 1501 |
lemma ucast_up_ucast [OF refl]: "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> ucast (uc w) = ucast w" |
37660 | 1502 |
apply (simp (no_asm) add: ucast_def) |
1503 |
apply (clarsimp simp add: uint_up_ucast) |
|
1504 |
done |
|
65268 | 1505 |
|
1506 |
lemma scast_up_scast [OF refl]: "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> scast (sc w) = scast w" |
|
37660 | 1507 |
apply (simp (no_asm) add: scast_def) |
1508 |
apply (clarsimp simp add: sint_up_scast) |
|
1509 |
done |
|
65268 | 1510 |
|
1511 |
lemma ucast_of_bl_up [OF refl]: "w = of_bl bl \<Longrightarrow> size bl \<le> size w \<Longrightarrow> ucast w = of_bl bl" |
|
37660 | 1512 |
by (auto simp add : nth_ucast word_size test_bit_of_bl intro!: word_eqI) |
1513 |
||
1514 |
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] |
|
1515 |
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] |
|
1516 |
||
1517 |
lemmas isduu = is_up_down [where c = "ucast", THEN iffD2] |
|
1518 |
lemmas isdus = is_up_down [where c = "scast", THEN iffD2] |
|
1519 |
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] |
|
1520 |
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] |
|
1521 |
||
1522 |
lemma up_ucast_surj: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1523 |
"is_up (ucast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow> |
65268 | 1524 |
surj (ucast :: 'a word \<Rightarrow> 'b word)" |
1525 |
by (rule surjI) (erule ucast_up_ucast_id) |
|
37660 | 1526 |
|
1527 |
lemma up_scast_surj: |
|
65268 | 1528 |
"is_up (scast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow> |
1529 |
surj (scast :: 'a word \<Rightarrow> 'b word)" |
|
1530 |
by (rule surjI) (erule scast_up_scast_id) |
|
37660 | 1531 |
|
1532 |
lemma down_scast_inj: |
|
65268 | 1533 |
"is_down (scast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow> |
1534 |
inj_on (ucast :: 'a word \<Rightarrow> 'b word) A" |
|
37660 | 1535 |
by (rule inj_on_inverseI, erule scast_down_scast_id) |
1536 |
||
1537 |
lemma down_ucast_inj: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1538 |
"is_down (ucast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow> |
65268 | 1539 |
inj_on (ucast :: 'a word \<Rightarrow> 'b word) A" |
1540 |
by (rule inj_on_inverseI) (erule ucast_down_ucast_id) |
|
37660 | 1541 |
|
1542 |
lemma of_bl_append_same: "of_bl (X @ to_bl w) = w" |
|
1543 |
by (rule word_bl.Rep_eqD) (simp add: word_rep_drop) |
|
45811 | 1544 |
|
65268 | 1545 |
lemma ucast_down_wi [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (word_of_int x) = word_of_int x" |
46646 | 1546 |
apply (unfold is_down) |
37660 | 1547 |
apply (clarsimp simp add: ucast_def word_ubin.eq_norm) |
1548 |
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
|
1549 |
apply (erule bintrunc_bintrunc_ge) |
|
1550 |
done |
|
45811 | 1551 |
|
65268 | 1552 |
lemma ucast_down_no [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (numeral bin) = numeral bin" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1553 |
unfolding word_numeral_alt by clarify (rule ucast_down_wi) |
46646 | 1554 |
|
65268 | 1555 |
lemma ucast_down_bl [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl" |
46646 | 1556 |
unfolding of_bl_def by clarify (erule ucast_down_wi) |
37660 | 1557 |
|
1558 |
lemmas slice_def' = slice_def [unfolded word_size] |
|
1559 |
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] |
|
1560 |
||
1561 |
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def |
|
1562 |
||
1563 |
||
61799 | 1564 |
subsection \<open>Word Arithmetic\<close> |
37660 | 1565 |
|
65268 | 1566 |
lemma word_less_alt: "a < b \<longleftrightarrow> uint a < uint b" |
55818 | 1567 |
by (fact word_less_def) |
37660 | 1568 |
|
1569 |
lemma signed_linorder: "class.linorder word_sle word_sless" |
|
65268 | 1570 |
by standard (auto simp: word_sle_def word_sless_def) |
37660 | 1571 |
|
1572 |
interpretation signed: linorder "word_sle" "word_sless" |
|
1573 |
by (rule signed_linorder) |
|
1574 |
||
65268 | 1575 |
lemma udvdI: "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" |
37660 | 1576 |
by (auto simp: udvd_def) |
1577 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1578 |
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
|
1579 |
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
|
1580 |
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
|
1581 |
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
|
1582 |
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
|
1583 |
lemmas word_sle_no [simp] = word_sle_def [of "numeral a" "numeral b"] for a b |
37660 | 1584 |
|
65268 | 1585 |
lemma word_m1_wi: "- 1 = word_of_int (- 1)" |
1586 |
by (simp add: word_neg_numeral_alt [of Num.One]) |
|
37660 | 1587 |
|
46648 | 1588 |
lemma word_0_bl [simp]: "of_bl [] = 0" |
65268 | 1589 |
by (simp add: of_bl_def) |
1590 |
||
1591 |
lemma word_1_bl: "of_bl [True] = 1" |
|
1592 |
by (simp add: of_bl_def bl_to_bin_def) |
|
46648 | 1593 |
|
1594 |
lemma uint_eq_0 [simp]: "uint 0 = 0" |
|
1595 |
unfolding word_0_wi word_ubin.eq_norm by simp |
|
37660 | 1596 |
|
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
1597 |
lemma of_bl_0 [simp]: "of_bl (replicate n False) = 0" |
46648 | 1598 |
by (simp add: of_bl_def bl_to_bin_rep_False) |
37660 | 1599 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1600 |
lemma to_bl_0 [simp]: "to_bl (0::'a::len word) = replicate (LENGTH('a)) False" |
65268 | 1601 |
by (simp add: uint_bl word_size bin_to_bl_zero) |
1602 |
||
1603 |
lemma uint_0_iff: "uint x = 0 \<longleftrightarrow> x = 0" |
|
55818 | 1604 |
by (simp add: word_uint_eq_iff) |
1605 |
||
65268 | 1606 |
lemma unat_0_iff: "unat x = 0 \<longleftrightarrow> x = 0" |
1607 |
by (auto simp: unat_def nat_eq_iff uint_0_iff) |
|
1608 |
||
1609 |
lemma unat_0 [simp]: "unat 0 = 0" |
|
1610 |
by (auto simp: unat_def) |
|
1611 |
||
1612 |
lemma size_0_same': "size w = 0 \<Longrightarrow> w = v" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1613 |
for v w :: "'a::len word" |
37660 | 1614 |
apply (unfold word_size) |
1615 |
apply (rule box_equals) |
|
1616 |
defer |
|
1617 |
apply (rule word_uint.Rep_inverse)+ |
|
1618 |
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
|
1619 |
apply simp |
|
1620 |
done |
|
1621 |
||
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
1622 |
lemmas size_0_same = size_0_same' [unfolded word_size] |
37660 | 1623 |
|
1624 |
lemmas unat_eq_0 = unat_0_iff |
|
1625 |
lemmas unat_eq_zero = unat_0_iff |
|
1626 |
||
65268 | 1627 |
lemma unat_gt_0: "0 < unat x \<longleftrightarrow> x \<noteq> 0" |
1628 |
by (auto simp: unat_0_iff [symmetric]) |
|
37660 | 1629 |
|
45958 | 1630 |
lemma ucast_0 [simp]: "ucast 0 = 0" |
65268 | 1631 |
by (simp add: ucast_def) |
45958 | 1632 |
|
1633 |
lemma sint_0 [simp]: "sint 0 = 0" |
|
65268 | 1634 |
by (simp add: sint_uint) |
45958 | 1635 |
|
1636 |
lemma scast_0 [simp]: "scast 0 = 0" |
|
65268 | 1637 |
by (simp add: scast_def) |
37660 | 1638 |
|
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
58061
diff
changeset
|
1639 |
lemma sint_n1 [simp] : "sint (- 1) = - 1" |
65268 | 1640 |
by (simp only: word_m1_wi word_sbin.eq_norm) simp |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
1641 |
|
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
1642 |
lemma scast_n1 [simp]: "scast (- 1) = - 1" |
65268 | 1643 |
by (simp add: scast_def) |
45958 | 1644 |
|
1645 |
lemma uint_1 [simp]: "uint (1::'a::len word) = 1" |
|
71947 | 1646 |
by (simp only: word_1_wi word_ubin.eq_norm) simp |
45958 | 1647 |
|
1648 |
lemma unat_1 [simp]: "unat (1::'a::len word) = 1" |
|
65268 | 1649 |
by (simp add: unat_def) |
45958 | 1650 |
|
1651 |
lemma ucast_1 [simp]: "ucast (1::'a::len word) = 1" |
|
65268 | 1652 |
by (simp add: ucast_def) |
37660 | 1653 |
|
67408 | 1654 |
\<comment> \<open>now, to get the weaker results analogous to \<open>word_div\<close>/\<open>mod_def\<close>\<close> |
37660 | 1655 |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1656 |
|
61799 | 1657 |
subsection \<open>Transferring goals from words to ints\<close> |
37660 | 1658 |
|
65268 | 1659 |
lemma word_ths: |
1660 |
shows word_succ_p1: "word_succ a = a + 1" |
|
1661 |
and word_pred_m1: "word_pred a = a - 1" |
|
1662 |
and word_pred_succ: "word_pred (word_succ a) = a" |
|
1663 |
and word_succ_pred: "word_succ (word_pred a) = a" |
|
1664 |
and word_mult_succ: "word_succ a * b = b + a * b" |
|
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
1665 |
by (transfer, simp add: algebra_simps)+ |
37660 | 1666 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
1667 |
lemma uint_cong: "x = y \<Longrightarrow> uint x = uint y" |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
1668 |
by simp |
37660 | 1669 |
|
55818 | 1670 |
lemma uint_word_ariths: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1671 |
fixes a b :: "'a::len word" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1672 |
shows "uint (a + b) = (uint a + uint b) mod 2 ^ LENGTH('a::len)" |
70185 | 1673 |
and "uint (a - b) = (uint a - uint b) mod 2 ^ LENGTH('a)" |
1674 |
and "uint (a * b) = uint a * uint b mod 2 ^ LENGTH('a)" |
|
1675 |
and "uint (- a) = - uint a mod 2 ^ LENGTH('a)" |
|
1676 |
and "uint (word_succ a) = (uint a + 1) mod 2 ^ LENGTH('a)" |
|
1677 |
and "uint (word_pred a) = (uint a - 1) mod 2 ^ LENGTH('a)" |
|
1678 |
and "uint (0 :: 'a word) = 0 mod 2 ^ LENGTH('a)" |
|
1679 |
and "uint (1 :: 'a word) = 1 mod 2 ^ LENGTH('a)" |
|
55818 | 1680 |
by (simp_all add: word_arith_wis [THEN trans [OF uint_cong int_word_uint]]) |
1681 |
||
1682 |
lemma uint_word_arith_bintrs: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1683 |
fixes a b :: "'a::len word" |
70185 | 1684 |
shows "uint (a + b) = bintrunc (LENGTH('a)) (uint a + uint b)" |
1685 |
and "uint (a - b) = bintrunc (LENGTH('a)) (uint a - uint b)" |
|
1686 |
and "uint (a * b) = bintrunc (LENGTH('a)) (uint a * uint b)" |
|
1687 |
and "uint (- a) = bintrunc (LENGTH('a)) (- uint a)" |
|
1688 |
and "uint (word_succ a) = bintrunc (LENGTH('a)) (uint a + 1)" |
|
1689 |
and "uint (word_pred a) = bintrunc (LENGTH('a)) (uint a - 1)" |
|
1690 |
and "uint (0 :: 'a word) = bintrunc (LENGTH('a)) 0" |
|
1691 |
and "uint (1 :: 'a word) = bintrunc (LENGTH('a)) 1" |
|
55818 | 1692 |
by (simp_all add: uint_word_ariths bintrunc_mod2p) |
1693 |
||
1694 |
lemma sint_word_ariths: |
|
1695 |
fixes a b :: "'a::len word" |
|
70185 | 1696 |
shows "sint (a + b) = sbintrunc (LENGTH('a) - 1) (sint a + sint b)" |
1697 |
and "sint (a - b) = sbintrunc (LENGTH('a) - 1) (sint a - sint b)" |
|
1698 |
and "sint (a * b) = sbintrunc (LENGTH('a) - 1) (sint a * sint b)" |
|
1699 |
and "sint (- a) = sbintrunc (LENGTH('a) - 1) (- sint a)" |
|
1700 |
and "sint (word_succ a) = sbintrunc (LENGTH('a) - 1) (sint a + 1)" |
|
1701 |
and "sint (word_pred a) = sbintrunc (LENGTH('a) - 1) (sint a - 1)" |
|
1702 |
and "sint (0 :: 'a word) = sbintrunc (LENGTH('a) - 1) 0" |
|
1703 |
and "sint (1 :: 'a word) = sbintrunc (LENGTH('a) - 1) 1" |
|
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
1704 |
apply (simp_all only: word_sbin.inverse_norm [symmetric]) |
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
1705 |
apply (simp_all add: wi_hom_syms) |
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
1706 |
apply transfer apply simp |
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
1707 |
apply transfer apply simp |
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
1708 |
done |
45604 | 1709 |
|
1710 |
lemmas uint_div_alt = word_div_def [THEN trans [OF uint_cong int_word_uint]] |
|
1711 |
lemmas uint_mod_alt = word_mod_def [THEN trans [OF uint_cong int_word_uint]] |
|
37660 | 1712 |
|
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
58061
diff
changeset
|
1713 |
lemma word_pred_0_n1: "word_pred 0 = word_of_int (- 1)" |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
1714 |
unfolding word_pred_m1 by simp |
37660 | 1715 |
|
1716 |
lemma succ_pred_no [simp]: |
|
65268 | 1717 |
"word_succ (numeral w) = numeral w + 1" |
1718 |
"word_pred (numeral w) = numeral w - 1" |
|
1719 |
"word_succ (- numeral w) = - numeral w + 1" |
|
1720 |
"word_pred (- numeral w) = - numeral w - 1" |
|
1721 |
by (simp_all add: word_succ_p1 word_pred_m1) |
|
1722 |
||
1723 |
lemma word_sp_01 [simp]: |
|
1724 |
"word_succ (- 1) = 0 \<and> word_succ 0 = 1 \<and> word_pred 0 = - 1 \<and> word_pred 1 = 0" |
|
1725 |
by (simp_all add: word_succ_p1 word_pred_m1) |
|
37660 | 1726 |
|
67408 | 1727 |
\<comment> \<open>alternative approach to lifting arithmetic equalities\<close> |
65268 | 1728 |
lemma word_of_int_Ex: "\<exists>y. x = word_of_int y" |
37660 | 1729 |
by (rule_tac x="uint x" in exI) simp |
1730 |
||
1731 |
||
61799 | 1732 |
subsection \<open>Order on fixed-length words\<close> |
37660 | 1733 |
|
65328 | 1734 |
lemma word_zero_le [simp]: "0 \<le> y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1735 |
for y :: "'a::len word" |
37660 | 1736 |
unfolding word_le_def by auto |
65268 | 1737 |
|
65328 | 1738 |
lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *) |
1739 |
by (simp only: word_le_def word_pred_0_n1 word_uint.eq_norm m1mod2k) auto |
|
1740 |
||
1741 |
lemma word_n1_ge [simp]: "y \<le> -1" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1742 |
for y :: "'a::len word" |
65328 | 1743 |
by (simp only: word_le_def word_m1_wi word_uint.eq_norm m1mod2k) auto |
37660 | 1744 |
|
65268 | 1745 |
lemmas word_not_simps [simp] = |
37660 | 1746 |
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] |
1747 |
||
65328 | 1748 |
lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1749 |
for y :: "'a::len word" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1750 |
by (simp add: less_le) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1751 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
1752 |
lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y |
37660 | 1753 |
|
65328 | 1754 |
lemma word_sless_alt: "a <s b \<longleftrightarrow> sint a < sint b" |
1755 |
by (auto simp add: word_sle_def word_sless_def less_le) |
|
1756 |
||
1757 |
lemma word_le_nat_alt: "a \<le> b \<longleftrightarrow> unat a \<le> unat b" |
|
37660 | 1758 |
unfolding unat_def word_le_def |
1759 |
by (rule nat_le_eq_zle [symmetric]) simp |
|
1760 |
||
65328 | 1761 |
lemma word_less_nat_alt: "a < b \<longleftrightarrow> unat a < unat b" |
37660 | 1762 |
unfolding unat_def word_less_alt |
1763 |
by (rule nat_less_eq_zless [symmetric]) simp |
|
65268 | 1764 |
|
70900 | 1765 |
lemmas unat_mono = word_less_nat_alt [THEN iffD1] |
1766 |
||
1767 |
instance word :: (len) wellorder |
|
1768 |
proof |
|
1769 |
fix P :: "'a word \<Rightarrow> bool" and a |
|
1770 |
assume *: "(\<And>b. (\<And>a. a < b \<Longrightarrow> P a) \<Longrightarrow> P b)" |
|
1771 |
have "wf (measure unat)" .. |
|
1772 |
moreover have "{(a, b :: ('a::len) word). a < b} \<subseteq> measure unat" |
|
1773 |
by (auto simp add: word_less_nat_alt) |
|
1774 |
ultimately have "wf {(a, b :: ('a::len) word). a < b}" |
|
1775 |
by (rule wf_subset) |
|
1776 |
then show "P a" using * |
|
1777 |
by induction blast |
|
1778 |
qed |
|
1779 |
||
65268 | 1780 |
lemma wi_less: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1781 |
"(word_of_int n < (word_of_int m :: 'a::len word)) = |
70185 | 1782 |
(n mod 2 ^ LENGTH('a) < m mod 2 ^ LENGTH('a))" |
37660 | 1783 |
unfolding word_less_alt by (simp add: word_uint.eq_norm) |
1784 |
||
65268 | 1785 |
lemma wi_le: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1786 |
"(word_of_int n \<le> (word_of_int m :: 'a::len word)) = |
70185 | 1787 |
(n mod 2 ^ LENGTH('a) \<le> m mod 2 ^ LENGTH('a))" |
37660 | 1788 |
unfolding word_le_def by (simp add: word_uint.eq_norm) |
1789 |
||
65328 | 1790 |
lemma udvd_nat_alt: "a udvd b \<longleftrightarrow> (\<exists>n\<ge>0. unat b = n * unat a)" |
37660 | 1791 |
apply (unfold udvd_def) |
1792 |
apply safe |
|
1793 |
apply (simp add: unat_def nat_mult_distrib) |
|
65328 | 1794 |
apply (simp add: uint_nat) |
37660 | 1795 |
apply (rule exI) |
1796 |
apply safe |
|
1797 |
prefer 2 |
|
1798 |
apply (erule notE) |
|
1799 |
apply (rule refl) |
|
1800 |
apply force |
|
1801 |
done |
|
1802 |
||
61941 | 1803 |
lemma udvd_iff_dvd: "x udvd y \<longleftrightarrow> unat x dvd unat y" |
37660 | 1804 |
unfolding dvd_def udvd_nat_alt by force |
1805 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1806 |
lemma unat_minus_one: |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1807 |
assumes "w \<noteq> 0" |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1808 |
shows "unat (w - 1) = unat w - 1" |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1809 |
proof - |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1810 |
have "0 \<le> uint w" by (fact uint_nonnegative) |
65328 | 1811 |
moreover from assms have "0 \<noteq> uint w" |
1812 |
by (simp add: uint_0_iff) |
|
1813 |
ultimately have "1 \<le> uint w" |
|
1814 |
by arith |
|
70185 | 1815 |
from uint_lt2p [of w] have "uint w - 1 < 2 ^ LENGTH('a)" |
65328 | 1816 |
by arith |
70185 | 1817 |
with \<open>1 \<le> uint w\<close> have "(uint w - 1) mod 2 ^ LENGTH('a) = uint w - 1" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1818 |
by (auto intro: mod_pos_pos_trivial) |
70185 | 1819 |
with \<open>1 \<le> uint w\<close> have "nat ((uint w - 1) mod 2 ^ LENGTH('a)) = nat (uint w) - 1" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1820 |
by auto |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1821 |
then show ?thesis |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
1822 |
by (simp only: unat_def int_word_uint word_arith_wis mod_diff_right_eq) |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1823 |
qed |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1824 |
|
65328 | 1825 |
lemma measure_unat: "p \<noteq> 0 \<Longrightarrow> unat (p - 1) < unat p" |
37660 | 1826 |
by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric]) |
65268 | 1827 |
|
45604 | 1828 |
lemmas uint_add_ge0 [simp] = add_nonneg_nonneg [OF uint_ge_0 uint_ge_0] |
1829 |
lemmas uint_mult_ge0 [simp] = mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0] |
|
37660 | 1830 |
|
70185 | 1831 |
lemma uint_sub_lt2p [simp]: "uint x - uint y < 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1832 |
for x :: "'a::len word" and y :: "'b::len word" |
37660 | 1833 |
using uint_ge_0 [of y] uint_lt2p [of x] by arith |
1834 |
||
1835 |
||
61799 | 1836 |
subsection \<open>Conditions for the addition (etc) of two words to overflow\<close> |
37660 | 1837 |
|
65268 | 1838 |
lemma uint_add_lem: |
70185 | 1839 |
"(uint x + uint y < 2 ^ LENGTH('a)) = |
65328 | 1840 |
(uint (x + y) = uint x + uint y)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1841 |
for x y :: "'a::len word" |
37660 | 1842 |
by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem]) |
1843 |
||
65268 | 1844 |
lemma uint_mult_lem: |
70185 | 1845 |
"(uint x * uint y < 2 ^ LENGTH('a)) = |
65328 | 1846 |
(uint (x * y) = uint x * uint y)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1847 |
for x y :: "'a::len word" |
37660 | 1848 |
by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem]) |
1849 |
||
65328 | 1850 |
lemma uint_sub_lem: "uint x \<ge> uint y \<longleftrightarrow> uint (x - y) = uint x - uint y" |
1851 |
by (auto simp: uint_word_ariths intro!: trans [OF _ int_mod_lem]) |
|
1852 |
||
1853 |
lemma uint_add_le: "uint (x + y) \<le> uint x + uint y" |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1854 |
unfolding uint_word_ariths by (metis uint_add_ge0 zmod_le_nonneg_dividend) |
37660 | 1855 |
|
65328 | 1856 |
lemma uint_sub_ge: "uint (x - y) \<ge> uint x - uint y" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1857 |
unfolding uint_word_ariths by (metis int_mod_ge uint_sub_lt2p zless2p) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1858 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1859 |
lemma mod_add_if_z: |
65328 | 1860 |
"x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow> |
1861 |
(x + y) mod z = (if x + y < z then x + y else x + y - z)" |
|
1862 |
for x y z :: int |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1863 |
by (auto intro: int_mod_eq) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1864 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1865 |
lemma uint_plus_if': |
65328 | 1866 |
"uint (a + b) = |
70185 | 1867 |
(if uint a + uint b < 2 ^ LENGTH('a) then uint a + uint b |
1868 |
else uint a + uint b - 2 ^ LENGTH('a))" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1869 |
for a b :: "'a::len word" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1870 |
using mod_add_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1871 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1872 |
lemma mod_sub_if_z: |
65328 | 1873 |
"x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow> |
1874 |
(x - y) mod z = (if y \<le> x then x - y else x - y + z)" |
|
1875 |
for x y z :: int |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1876 |
by (auto intro: int_mod_eq) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1877 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1878 |
lemma uint_sub_if': |
65328 | 1879 |
"uint (a - b) = |
1880 |
(if uint b \<le> uint a then uint a - uint b |
|
70185 | 1881 |
else uint a - uint b + 2 ^ LENGTH('a))" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1882 |
for a b :: "'a::len word" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1883 |
using mod_sub_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1884 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
1885 |
|
61799 | 1886 |
subsection \<open>Definition of \<open>uint_arith\<close>\<close> |
37660 | 1887 |
|
1888 |
lemma word_of_int_inverse: |
|
70185 | 1889 |
"word_of_int r = a \<Longrightarrow> 0 \<le> r \<Longrightarrow> r < 2 ^ LENGTH('a) \<Longrightarrow> uint a = r" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1890 |
for a :: "'a::len word" |
37660 | 1891 |
apply (erule word_uint.Abs_inverse' [rotated]) |
1892 |
apply (simp add: uints_num) |
|
1893 |
done |
|
1894 |
||
1895 |
lemma uint_split: |
|
70185 | 1896 |
"P (uint x) = (\<forall>i. word_of_int i = x \<and> 0 \<le> i \<and> i < 2^LENGTH('a) \<longrightarrow> P i)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1897 |
for x :: "'a::len word" |
37660 | 1898 |
apply (fold word_int_case_def) |
71942 | 1899 |
apply (auto dest!: word_of_int_inverse simp: int_word_uint |
65328 | 1900 |
split: word_int_split) |
37660 | 1901 |
done |
1902 |
||
1903 |
lemma uint_split_asm: |
|
70185 | 1904 |
"P (uint x) = (\<nexists>i. word_of_int i = x \<and> 0 \<le> i \<and> i < 2^LENGTH('a) \<and> \<not> P i)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1905 |
for x :: "'a::len word" |
65268 | 1906 |
by (auto dest!: word_of_int_inverse |
71942 | 1907 |
simp: int_word_uint |
65328 | 1908 |
split: uint_split) |
37660 | 1909 |
|
1910 |
lemmas uint_splits = uint_split uint_split_asm |
|
1911 |
||
65268 | 1912 |
lemmas uint_arith_simps = |
37660 | 1913 |
word_le_def word_less_alt |
65268 | 1914 |
word_uint.Rep_inject [symmetric] |
37660 | 1915 |
uint_sub_if' uint_plus_if' |
1916 |
||
70185 | 1917 |
\<comment> \<open>use this to stop, eg. \<open>2 ^ LENGTH(32)\<close> being simplified\<close> |
65268 | 1918 |
lemma power_False_cong: "False \<Longrightarrow> a ^ b = c ^ d" |
37660 | 1919 |
by auto |
1920 |
||
67408 | 1921 |
\<comment> \<open>\<open>uint_arith_tac\<close>: reduce to arithmetic on int, try to solve by arith\<close> |
61799 | 1922 |
ML \<open> |
65268 | 1923 |
fun uint_arith_simpset ctxt = |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
1924 |
ctxt addsimps @{thms uint_arith_simps} |
37660 | 1925 |
delsimps @{thms word_uint.Rep_inject} |
62390 | 1926 |
|> fold Splitter.add_split @{thms if_split_asm} |
45620
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents:
45604
diff
changeset
|
1927 |
|> fold Simplifier.add_cong @{thms power_False_cong} |
37660 | 1928 |
|
65268 | 1929 |
fun uint_arith_tacs ctxt = |
37660 | 1930 |
let |
1931 |
fun arith_tac' n t = |
|
59657
2441a80fb6c1
eliminated unused arith "verbose" flag -- tools that need options can use the context;
wenzelm
parents:
59498
diff
changeset
|
1932 |
Arith_Data.arith_tac ctxt n t |
37660 | 1933 |
handle Cooper.COOPER _ => Seq.empty; |
65268 | 1934 |
in |
42793 | 1935 |
[ clarify_tac ctxt 1, |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
1936 |
full_simp_tac (uint_arith_simpset ctxt) 1, |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
1937 |
ALLGOALS (full_simp_tac |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
1938 |
(put_simpset HOL_ss ctxt |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
1939 |
|> fold Splitter.add_split @{thms uint_splits} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
1940 |
|> fold Simplifier.add_cong @{thms power_False_cong})), |
65268 | 1941 |
rewrite_goals_tac ctxt @{thms word_size}, |
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59487
diff
changeset
|
1942 |
ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN |
60754 | 1943 |
REPEAT (eresolve_tac ctxt [conjE] n) THEN |
65268 | 1944 |
REPEAT (dresolve_tac ctxt @{thms word_of_int_inverse} n |
1945 |
THEN assume_tac ctxt n |
|
58963
26bf09b95dda
proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents:
58874
diff
changeset
|
1946 |
THEN assume_tac ctxt n)), |
37660 | 1947 |
TRYALL arith_tac' ] |
1948 |
end |
|
1949 |
||
1950 |
fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt)) |
|
61799 | 1951 |
\<close> |
37660 | 1952 |
|
65268 | 1953 |
method_setup uint_arith = |
61799 | 1954 |
\<open>Scan.succeed (SIMPLE_METHOD' o uint_arith_tac)\<close> |
37660 | 1955 |
"solving word arithmetic via integers and arith" |
1956 |
||
1957 |
||
61799 | 1958 |
subsection \<open>More on overflows and monotonicity\<close> |
37660 | 1959 |
|
65328 | 1960 |
lemma no_plus_overflow_uint_size: "x \<le> x + y \<longleftrightarrow> uint x + uint y < 2 ^ size x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1961 |
for x y :: "'a::len word" |
37660 | 1962 |
unfolding word_size by uint_arith |
1963 |
||
1964 |
lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size] |
|
1965 |
||
65328 | 1966 |
lemma no_ulen_sub: "x \<ge> x - y \<longleftrightarrow> uint y \<le> uint x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1967 |
for x y :: "'a::len word" |
37660 | 1968 |
by uint_arith |
1969 |
||
70185 | 1970 |
lemma no_olen_add': "x \<le> y + x \<longleftrightarrow> uint y + uint x < 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1971 |
for x y :: "'a::len word" |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
1972 |
by (simp add: ac_simps no_olen_add) |
37660 | 1973 |
|
45604 | 1974 |
lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric]] |
1975 |
||
1976 |
lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem] |
|
1977 |
lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1] |
|
1978 |
lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem] |
|
37660 | 1979 |
lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def] |
1980 |
lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def] |
|
45604 | 1981 |
lemmas word_sub_le = word_sub_le_iff [THEN iffD2] |
37660 | 1982 |
|
65328 | 1983 |
lemma word_less_sub1: "x \<noteq> 0 \<Longrightarrow> 1 < x \<longleftrightarrow> 0 < x - 1" |
1984 |
for x :: "'a::len word" |
|
37660 | 1985 |
by uint_arith |
1986 |
||
65328 | 1987 |
lemma word_le_sub1: "x \<noteq> 0 \<Longrightarrow> 1 \<le> x \<longleftrightarrow> 0 \<le> x - 1" |
1988 |
for x :: "'a::len word" |
|
37660 | 1989 |
by uint_arith |
1990 |
||
65328 | 1991 |
lemma sub_wrap_lt: "x < x - z \<longleftrightarrow> x < z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1992 |
for x z :: "'a::len word" |
37660 | 1993 |
by uint_arith |
1994 |
||
65328 | 1995 |
lemma sub_wrap: "x \<le> x - z \<longleftrightarrow> z = 0 \<or> x < z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
1996 |
for x z :: "'a::len word" |
37660 | 1997 |
by uint_arith |
1998 |
||
65328 | 1999 |
lemma plus_minus_not_NULL_ab: "x \<le> ab - c \<Longrightarrow> c \<le> ab \<Longrightarrow> c \<noteq> 0 \<Longrightarrow> x + c \<noteq> 0" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2000 |
for x ab c :: "'a::len word" |
37660 | 2001 |
by uint_arith |
2002 |
||
65328 | 2003 |
lemma plus_minus_no_overflow_ab: "x \<le> ab - c \<Longrightarrow> c \<le> ab \<Longrightarrow> x \<le> x + c" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2004 |
for x ab c :: "'a::len word" |
37660 | 2005 |
by uint_arith |
2006 |
||
65328 | 2007 |
lemma le_minus': "a + c \<le> b \<Longrightarrow> a \<le> a + c \<Longrightarrow> c \<le> b - a" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2008 |
for a b c :: "'a::len word" |
37660 | 2009 |
by uint_arith |
2010 |
||
65328 | 2011 |
lemma le_plus': "a \<le> b \<Longrightarrow> c \<le> b - a \<Longrightarrow> a + c \<le> b" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2012 |
for a b c :: "'a::len word" |
37660 | 2013 |
by uint_arith |
2014 |
||
2015 |
lemmas le_plus = le_plus' [rotated] |
|
2016 |
||
46011 | 2017 |
lemmas le_minus = leD [THEN thin_rl, THEN le_minus'] (* FIXME *) |
37660 | 2018 |
|
65328 | 2019 |
lemma word_plus_mono_right: "y \<le> z \<Longrightarrow> x \<le> x + z \<Longrightarrow> x + y \<le> x + z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2020 |
for x y z :: "'a::len word" |
37660 | 2021 |
by uint_arith |
2022 |
||
65328 | 2023 |
lemma word_less_minus_cancel: "y - x < z - x \<Longrightarrow> x \<le> z \<Longrightarrow> y < z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2024 |
for x y z :: "'a::len word" |
37660 | 2025 |
by uint_arith |
2026 |
||
65328 | 2027 |
lemma word_less_minus_mono_left: "y < z \<Longrightarrow> x \<le> y \<Longrightarrow> y - x < z - x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2028 |
for x y z :: "'a::len word" |
37660 | 2029 |
by uint_arith |
2030 |
||
65328 | 2031 |
lemma word_less_minus_mono: "a < c \<Longrightarrow> d < b \<Longrightarrow> a - b < a \<Longrightarrow> c - d < c \<Longrightarrow> a - b < c - d" |
2032 |
for a b c d :: "'a::len word" |
|
37660 | 2033 |
by uint_arith |
2034 |
||
65328 | 2035 |
lemma word_le_minus_cancel: "y - x \<le> z - x \<Longrightarrow> x \<le> z \<Longrightarrow> y \<le> z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2036 |
for x y z :: "'a::len word" |
37660 | 2037 |
by uint_arith |
2038 |
||
65328 | 2039 |
lemma word_le_minus_mono_left: "y \<le> z \<Longrightarrow> x \<le> y \<Longrightarrow> y - x \<le> z - x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2040 |
for x y z :: "'a::len word" |
37660 | 2041 |
by uint_arith |
2042 |
||
65268 | 2043 |
lemma word_le_minus_mono: |
65328 | 2044 |
"a \<le> c \<Longrightarrow> d \<le> b \<Longrightarrow> a - b \<le> a \<Longrightarrow> c - d \<le> c \<Longrightarrow> a - b \<le> c - d" |
2045 |
for a b c d :: "'a::len word" |
|
37660 | 2046 |
by uint_arith |
2047 |
||
65328 | 2048 |
lemma plus_le_left_cancel_wrap: "x + y' < x \<Longrightarrow> x + y < x \<Longrightarrow> x + y' < x + y \<longleftrightarrow> y' < y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2049 |
for x y y' :: "'a::len word" |
37660 | 2050 |
by uint_arith |
2051 |
||
65328 | 2052 |
lemma plus_le_left_cancel_nowrap: "x \<le> x + y' \<Longrightarrow> x \<le> x + y \<Longrightarrow> x + y' < x + y \<longleftrightarrow> y' < y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2053 |
for x y y' :: "'a::len word" |
37660 | 2054 |
by uint_arith |
2055 |
||
65328 | 2056 |
lemma word_plus_mono_right2: "a \<le> a + b \<Longrightarrow> c \<le> b \<Longrightarrow> a \<le> a + c" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2057 |
for a b c :: "'a::len word" |
65328 | 2058 |
by uint_arith |
2059 |
||
2060 |
lemma word_less_add_right: "x < y - z \<Longrightarrow> z \<le> y \<Longrightarrow> x + z < y" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2061 |
for x y z :: "'a::len word" |
37660 | 2062 |
by uint_arith |
2063 |
||
65328 | 2064 |
lemma word_less_sub_right: "x < y + z \<Longrightarrow> y \<le> x \<Longrightarrow> x - y < z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2065 |
for x y z :: "'a::len word" |
37660 | 2066 |
by uint_arith |
2067 |
||
65328 | 2068 |
lemma word_le_plus_either: "x \<le> y \<or> x \<le> z \<Longrightarrow> y \<le> y + z \<Longrightarrow> x \<le> y + z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2069 |
for x y z :: "'a::len word" |
37660 | 2070 |
by uint_arith |
2071 |
||
65328 | 2072 |
lemma word_less_nowrapI: "x < z - k \<Longrightarrow> k \<le> z \<Longrightarrow> 0 < k \<Longrightarrow> x < x + k" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2073 |
for x z k :: "'a::len word" |
37660 | 2074 |
by uint_arith |
2075 |
||
65328 | 2076 |
lemma inc_le: "i < m \<Longrightarrow> i + 1 \<le> m" |
2077 |
for i m :: "'a::len word" |
|
37660 | 2078 |
by uint_arith |
2079 |
||
65328 | 2080 |
lemma inc_i: "1 \<le> i \<Longrightarrow> i < m \<Longrightarrow> 1 \<le> i + 1 \<and> i + 1 \<le> m" |
2081 |
for i m :: "'a::len word" |
|
37660 | 2082 |
by uint_arith |
2083 |
||
2084 |
lemma udvd_incr_lem: |
|
65268 | 2085 |
"up < uq \<Longrightarrow> up = ua + n * uint K \<Longrightarrow> |
65328 | 2086 |
uq = ua + n' * uint K \<Longrightarrow> up + uint K \<le> uq" |
37660 | 2087 |
apply clarsimp |
2088 |
apply (drule less_le_mult) |
|
65328 | 2089 |
apply safe |
37660 | 2090 |
done |
2091 |
||
65268 | 2092 |
lemma udvd_incr': |
2093 |
"p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow> |
|
65328 | 2094 |
uint q = ua + n' * uint K \<Longrightarrow> p + K \<le> q" |
37660 | 2095 |
apply (unfold word_less_alt word_le_def) |
2096 |
apply (drule (2) udvd_incr_lem) |
|
2097 |
apply (erule uint_add_le [THEN order_trans]) |
|
2098 |
done |
|
2099 |
||
65268 | 2100 |
lemma udvd_decr': |
2101 |
"p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow> |
|
65328 | 2102 |
uint q = ua + n' * uint K \<Longrightarrow> p \<le> q - K" |
37660 | 2103 |
apply (unfold word_less_alt word_le_def) |
2104 |
apply (drule (2) udvd_incr_lem) |
|
2105 |
apply (drule le_diff_eq [THEN iffD2]) |
|
2106 |
apply (erule order_trans) |
|
2107 |
apply (rule uint_sub_ge) |
|
2108 |
done |
|
2109 |
||
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2110 |
lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, unfolded add_0_left] |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2111 |
lemmas udvd_incr0 = udvd_incr' [where ua=0, unfolded add_0_left] |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2112 |
lemmas udvd_decr0 = udvd_decr' [where ua=0, unfolded add_0_left] |
37660 | 2113 |
|
65328 | 2114 |
lemma udvd_minus_le': "xy < k \<Longrightarrow> z udvd xy \<Longrightarrow> z udvd k \<Longrightarrow> xy \<le> k - z" |
37660 | 2115 |
apply (unfold udvd_def) |
2116 |
apply clarify |
|
2117 |
apply (erule (2) udvd_decr0) |
|
2118 |
done |
|
2119 |
||
65268 | 2120 |
lemma udvd_incr2_K: |
65328 | 2121 |
"p < a + s \<Longrightarrow> a \<le> a + s \<Longrightarrow> K udvd s \<Longrightarrow> K udvd p - a \<Longrightarrow> a \<le> p \<Longrightarrow> |
2122 |
0 < K \<Longrightarrow> p \<le> p + K \<and> p + K \<le> a + s" |
|
2123 |
supply [[simproc del: linordered_ring_less_cancel_factor]] |
|
37660 | 2124 |
apply (unfold udvd_def) |
2125 |
apply clarify |
|
62390 | 2126 |
apply (simp add: uint_arith_simps split: if_split_asm) |
65268 | 2127 |
prefer 2 |
37660 | 2128 |
apply (insert uint_range' [of s])[1] |
2129 |
apply arith |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
2130 |
apply (drule add.commute [THEN xtr1]) |
37660 | 2131 |
apply (simp add: diff_less_eq [symmetric]) |
2132 |
apply (drule less_le_mult) |
|
2133 |
apply arith |
|
2134 |
apply simp |
|
2135 |
done |
|
2136 |
||
67408 | 2137 |
\<comment> \<open>links with \<open>rbl\<close> operations\<close> |
65328 | 2138 |
lemma word_succ_rbl: "to_bl w = bl \<Longrightarrow> to_bl (word_succ w) = rev (rbl_succ (rev bl))" |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2139 |
apply (unfold word_succ_alt) |
37660 | 2140 |
apply clarify |
2141 |
apply (simp add: to_bl_of_bin) |
|
46654 | 2142 |
apply (simp add: to_bl_def rbl_succ) |
37660 | 2143 |
done |
2144 |
||
65328 | 2145 |
lemma word_pred_rbl: "to_bl w = bl \<Longrightarrow> to_bl (word_pred w) = rev (rbl_pred (rev bl))" |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2146 |
apply (unfold word_pred_alt) |
37660 | 2147 |
apply clarify |
2148 |
apply (simp add: to_bl_of_bin) |
|
46654 | 2149 |
apply (simp add: to_bl_def rbl_pred) |
37660 | 2150 |
done |
2151 |
||
2152 |
lemma word_add_rbl: |
|
65268 | 2153 |
"to_bl v = vbl \<Longrightarrow> to_bl w = wbl \<Longrightarrow> |
65328 | 2154 |
to_bl (v + w) = rev (rbl_add (rev vbl) (rev wbl))" |
37660 | 2155 |
apply (unfold word_add_def) |
2156 |
apply clarify |
|
2157 |
apply (simp add: to_bl_of_bin) |
|
2158 |
apply (simp add: to_bl_def rbl_add) |
|
2159 |
done |
|
2160 |
||
2161 |
lemma word_mult_rbl: |
|
65268 | 2162 |
"to_bl v = vbl \<Longrightarrow> to_bl w = wbl \<Longrightarrow> |
65328 | 2163 |
to_bl (v * w) = rev (rbl_mult (rev vbl) (rev wbl))" |
37660 | 2164 |
apply (unfold word_mult_def) |
2165 |
apply clarify |
|
2166 |
apply (simp add: to_bl_of_bin) |
|
2167 |
apply (simp add: to_bl_def rbl_mult) |
|
2168 |
done |
|
2169 |
||
2170 |
lemma rtb_rbl_ariths: |
|
2171 |
"rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_succ w)) = rbl_succ ys" |
|
2172 |
"rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_pred w)) = rbl_pred ys" |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2173 |
"rev (to_bl v) = ys \<Longrightarrow> rev (to_bl w) = xs \<Longrightarrow> rev (to_bl (v * w)) = rbl_mult ys xs" |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2174 |
"rev (to_bl v) = ys \<Longrightarrow> rev (to_bl w) = xs \<Longrightarrow> rev (to_bl (v + w)) = rbl_add ys xs" |
65328 | 2175 |
by (auto simp: rev_swap [symmetric] word_succ_rbl word_pred_rbl word_mult_rbl word_add_rbl) |
37660 | 2176 |
|
2177 |
||
61799 | 2178 |
subsection \<open>Arithmetic type class instantiations\<close> |
37660 | 2179 |
|
2180 |
lemmas word_le_0_iff [simp] = |
|
70749
5d06b7bb9d22
More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
paulson <lp15@cam.ac.uk>
parents:
70342
diff
changeset
|
2181 |
word_zero_le [THEN leD, THEN antisym_conv1] |
37660 | 2182 |
|
65328 | 2183 |
lemma word_of_int_nat: "0 \<le> x \<Longrightarrow> word_of_int x = of_nat (nat x)" |
2184 |
by (simp add: word_of_int) |
|
37660 | 2185 |
|
67408 | 2186 |
text \<open> |
2187 |
note that \<open>iszero_def\<close> is only for class \<open>comm_semiring_1_cancel\<close>, |
|
2188 |
which requires word length \<open>\<ge> 1\<close>, ie \<open>'a::len word\<close> |
|
2189 |
\<close> |
|
46603 | 2190 |
lemma iszero_word_no [simp]: |
65268 | 2191 |
"iszero (numeral bin :: 'a::len word) = |
70185 | 2192 |
iszero (bintrunc (LENGTH('a)) (numeral bin))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2193 |
using word_ubin.norm_eq_iff [where 'a='a, of "numeral bin" 0] |
46603 | 2194 |
by (simp add: iszero_def [symmetric]) |
65268 | 2195 |
|
61799 | 2196 |
text \<open>Use \<open>iszero\<close> to simplify equalities between word numerals.\<close> |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2197 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2198 |
lemmas word_eq_numeral_iff_iszero [simp] = |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2199 |
eq_numeral_iff_iszero [where 'a="'a::len word"] |
46603 | 2200 |
|
37660 | 2201 |
|
61799 | 2202 |
subsection \<open>Word and nat\<close> |
37660 | 2203 |
|
45811 | 2204 |
lemma td_ext_unat [OF refl]: |
70185 | 2205 |
"n = LENGTH('a::len) \<Longrightarrow> |
65328 | 2206 |
td_ext (unat :: 'a word \<Rightarrow> nat) of_nat (unats n) (\<lambda>i. i mod 2 ^ n)" |
37660 | 2207 |
apply (unfold td_ext_def' unat_def word_of_nat unats_uints) |
2208 |
apply (auto intro!: imageI simp add : word_of_int_hom_syms) |
|
65328 | 2209 |
apply (erule word_uint.Abs_inverse [THEN arg_cong]) |
37660 | 2210 |
apply (simp add: int_word_uint nat_mod_distrib nat_power_eq) |
2211 |
done |
|
2212 |
||
45604 | 2213 |
lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm] |
37660 | 2214 |
|
2215 |
interpretation word_unat: |
|
65328 | 2216 |
td_ext |
2217 |
"unat::'a::len word \<Rightarrow> nat" |
|
2218 |
of_nat |
|
70185 | 2219 |
"unats (LENGTH('a::len))" |
2220 |
"\<lambda>i. i mod 2 ^ LENGTH('a::len)" |
|
37660 | 2221 |
by (rule td_ext_unat) |
2222 |
||
2223 |
lemmas td_unat = word_unat.td_thm |
|
2224 |
||
2225 |
lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq] |
|
2226 |
||
70185 | 2227 |
lemma unat_le: "y \<le> unat z \<Longrightarrow> y \<in> unats (LENGTH('a))" |
65328 | 2228 |
for z :: "'a::len word" |
37660 | 2229 |
apply (unfold unats_def) |
2230 |
apply clarsimp |
|
65268 | 2231 |
apply (rule xtrans, rule unat_lt2p, assumption) |
37660 | 2232 |
done |
2233 |
||
70185 | 2234 |
lemma word_nchotomy: "\<forall>w :: 'a::len word. \<exists>n. w = of_nat n \<and> n < 2 ^ LENGTH('a)" |
37660 | 2235 |
apply (rule allI) |
2236 |
apply (rule word_unat.Abs_cases) |
|
2237 |
apply (unfold unats_def) |
|
2238 |
apply auto |
|
2239 |
done |
|
2240 |
||
70185 | 2241 |
lemma of_nat_eq: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ LENGTH('a))" |
65328 | 2242 |
for w :: "'a::len word" |
68157 | 2243 |
using mod_div_mult_eq [of n "2 ^ LENGTH('a)", symmetric] |
2244 |
by (auto simp add: word_unat.inverse_norm) |
|
37660 | 2245 |
|
65328 | 2246 |
lemma of_nat_eq_size: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ size w)" |
37660 | 2247 |
unfolding word_size by (rule of_nat_eq) |
2248 |
||
70185 | 2249 |
lemma of_nat_0: "of_nat m = (0::'a::len word) \<longleftrightarrow> (\<exists>q. m = q * 2 ^ LENGTH('a))" |
37660 | 2250 |
by (simp add: of_nat_eq) |
2251 |
||
70185 | 2252 |
lemma of_nat_2p [simp]: "of_nat (2 ^ LENGTH('a)) = (0::'a::len word)" |
45805 | 2253 |
by (fact mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]]) |
37660 | 2254 |
|
65328 | 2255 |
lemma of_nat_gt_0: "of_nat k \<noteq> 0 \<Longrightarrow> 0 < k" |
37660 | 2256 |
by (cases k) auto |
2257 |
||
70185 | 2258 |
lemma of_nat_neq_0: "0 < k \<Longrightarrow> k < 2 ^ LENGTH('a::len) \<Longrightarrow> of_nat k \<noteq> (0 :: 'a word)" |
65328 | 2259 |
by (auto simp add : of_nat_0) |
2260 |
||
2261 |
lemma Abs_fnat_hom_add: "of_nat a + of_nat b = of_nat (a + b)" |
|
37660 | 2262 |
by simp |
2263 |
||
65328 | 2264 |
lemma Abs_fnat_hom_mult: "of_nat a * of_nat b = (of_nat (a * b) :: 'a::len word)" |
61649
268d88ec9087
Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents:
61424
diff
changeset
|
2265 |
by (simp add: word_of_nat wi_hom_mult) |
37660 | 2266 |
|
65328 | 2267 |
lemma Abs_fnat_hom_Suc: "word_succ (of_nat a) = of_nat (Suc a)" |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
2268 |
by (simp add: word_of_nat wi_hom_succ ac_simps) |
37660 | 2269 |
|
2270 |
lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0" |
|
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
2271 |
by simp |
37660 | 2272 |
|
2273 |
lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)" |
|
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
2274 |
by simp |
37660 | 2275 |
|
65268 | 2276 |
lemmas Abs_fnat_homs = |
2277 |
Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc |
|
37660 | 2278 |
Abs_fnat_hom_0 Abs_fnat_hom_1 |
2279 |
||
65328 | 2280 |
lemma word_arith_nat_add: "a + b = of_nat (unat a + unat b)" |
2281 |
by simp |
|
2282 |
||
2283 |
lemma word_arith_nat_mult: "a * b = of_nat (unat a * unat b)" |
|
37660 | 2284 |
by simp |
2285 |
||
65328 | 2286 |
lemma word_arith_nat_Suc: "word_succ a = of_nat (Suc (unat a))" |
37660 | 2287 |
by (subst Abs_fnat_hom_Suc [symmetric]) simp |
2288 |
||
65328 | 2289 |
lemma word_arith_nat_div: "a div b = of_nat (unat a div unat b)" |
37660 | 2290 |
by (simp add: word_div_def word_of_nat zdiv_int uint_nat) |
2291 |
||
65328 | 2292 |
lemma word_arith_nat_mod: "a mod b = of_nat (unat a mod unat b)" |
37660 | 2293 |
by (simp add: word_mod_def word_of_nat zmod_int uint_nat) |
2294 |
||
2295 |
lemmas word_arith_nat_defs = |
|
2296 |
word_arith_nat_add word_arith_nat_mult |
|
2297 |
word_arith_nat_Suc Abs_fnat_hom_0 |
|
2298 |
Abs_fnat_hom_1 word_arith_nat_div |
|
65268 | 2299 |
word_arith_nat_mod |
37660 | 2300 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2301 |
lemma unat_cong: "x = y \<Longrightarrow> unat x = unat y" |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2302 |
by simp |
65268 | 2303 |
|
37660 | 2304 |
lemmas unat_word_ariths = word_arith_nat_defs |
45604 | 2305 |
[THEN trans [OF unat_cong unat_of_nat]] |
37660 | 2306 |
|
2307 |
lemmas word_sub_less_iff = word_sub_le_iff |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2308 |
[unfolded linorder_not_less [symmetric] Not_eq_iff] |
37660 | 2309 |
|
65268 | 2310 |
lemma unat_add_lem: |
70185 | 2311 |
"unat x + unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x + y) = unat x + unat y" |
65328 | 2312 |
for x y :: "'a::len word" |
2313 |
by (auto simp: unat_word_ariths intro!: trans [OF _ nat_mod_lem]) |
|
37660 | 2314 |
|
65268 | 2315 |
lemma unat_mult_lem: |
70185 | 2316 |
"unat x * unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x * y) = unat x * unat y" |
65363 | 2317 |
for x y :: "'a::len word" |
65328 | 2318 |
by (auto simp: unat_word_ariths intro!: trans [OF _ nat_mod_lem]) |
2319 |
||
2320 |
lemmas unat_plus_if' = |
|
2321 |
trans [OF unat_word_ariths(1) mod_nat_add, simplified] |
|
2322 |
||
2323 |
lemma le_no_overflow: "x \<le> b \<Longrightarrow> a \<le> a + b \<Longrightarrow> x \<le> a + b" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2324 |
for a b x :: "'a::len word" |
37660 | 2325 |
apply (erule order_trans) |
2326 |
apply (erule olen_add_eqv [THEN iffD1]) |
|
2327 |
done |
|
2328 |
||
65328 | 2329 |
lemmas un_ui_le = |
2330 |
trans [OF word_le_nat_alt [symmetric] word_le_def] |
|
37660 | 2331 |
|
2332 |
lemma unat_sub_if_size: |
|
65328 | 2333 |
"unat (x - y) = |
2334 |
(if unat y \<le> unat x |
|
2335 |
then unat x - unat y |
|
2336 |
else unat x + 2 ^ size x - unat y)" |
|
37660 | 2337 |
apply (unfold word_size) |
2338 |
apply (simp add: un_ui_le) |
|
2339 |
apply (auto simp add: unat_def uint_sub_if') |
|
2340 |
apply (rule nat_diff_distrib) |
|
2341 |
prefer 3 |
|
2342 |
apply (simp add: algebra_simps) |
|
2343 |
apply (rule nat_diff_distrib [THEN trans]) |
|
2344 |
prefer 3 |
|
2345 |
apply (subst nat_add_distrib) |
|
2346 |
prefer 3 |
|
2347 |
apply (simp add: nat_power_eq) |
|
2348 |
apply auto |
|
2349 |
apply uint_arith |
|
2350 |
done |
|
2351 |
||
2352 |
lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size] |
|
2353 |
||
65328 | 2354 |
lemma unat_div: "unat (x div y) = unat x div unat y" |
2355 |
for x y :: " 'a::len word" |
|
37660 | 2356 |
apply (simp add : unat_word_ariths) |
2357 |
apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq']) |
|
2358 |
apply (rule div_le_dividend) |
|
2359 |
done |
|
2360 |
||
65328 | 2361 |
lemma unat_mod: "unat (x mod y) = unat x mod unat y" |
2362 |
for x y :: "'a::len word" |
|
37660 | 2363 |
apply (clarsimp simp add : unat_word_ariths) |
2364 |
apply (cases "unat y") |
|
2365 |
prefer 2 |
|
2366 |
apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq']) |
|
2367 |
apply (rule mod_le_divisor) |
|
2368 |
apply auto |
|
2369 |
done |
|
2370 |
||
65328 | 2371 |
lemma uint_div: "uint (x div y) = uint x div uint y" |
2372 |
for x y :: "'a::len word" |
|
2373 |
by (simp add: uint_nat unat_div zdiv_int) |
|
2374 |
||
2375 |
lemma uint_mod: "uint (x mod y) = uint x mod uint y" |
|
2376 |
for x y :: "'a::len word" |
|
2377 |
by (simp add: uint_nat unat_mod zmod_int) |
|
37660 | 2378 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
2379 |
text \<open>Definition of \<open>unat_arith\<close> tactic\<close> |
37660 | 2380 |
|
70185 | 2381 |
lemma unat_split: "P (unat x) \<longleftrightarrow> (\<forall>n. of_nat n = x \<and> n < 2^LENGTH('a) \<longrightarrow> P n)" |
65328 | 2382 |
for x :: "'a::len word" |
37660 | 2383 |
by (auto simp: unat_of_nat) |
2384 |
||
70185 | 2385 |
lemma unat_split_asm: "P (unat x) \<longleftrightarrow> (\<nexists>n. of_nat n = x \<and> n < 2^LENGTH('a) \<and> \<not> P n)" |
65328 | 2386 |
for x :: "'a::len word" |
37660 | 2387 |
by (auto simp: unat_of_nat) |
2388 |
||
65268 | 2389 |
lemmas of_nat_inverse = |
37660 | 2390 |
word_unat.Abs_inverse' [rotated, unfolded unats_def, simplified] |
2391 |
||
2392 |
lemmas unat_splits = unat_split unat_split_asm |
|
2393 |
||
2394 |
lemmas unat_arith_simps = |
|
2395 |
word_le_nat_alt word_less_nat_alt |
|
2396 |
word_unat.Rep_inject [symmetric] |
|
2397 |
unat_sub_if' unat_plus_if' unat_div unat_mod |
|
2398 |
||
67408 | 2399 |
\<comment> \<open>\<open>unat_arith_tac\<close>: tactic to reduce word arithmetic to \<open>nat\<close>, try to solve via \<open>arith\<close>\<close> |
61799 | 2400 |
ML \<open> |
65268 | 2401 |
fun unat_arith_simpset ctxt = |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2402 |
ctxt addsimps @{thms unat_arith_simps} |
37660 | 2403 |
delsimps @{thms word_unat.Rep_inject} |
62390 | 2404 |
|> fold Splitter.add_split @{thms if_split_asm} |
45620
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents:
45604
diff
changeset
|
2405 |
|> fold Simplifier.add_cong @{thms power_False_cong} |
37660 | 2406 |
|
65268 | 2407 |
fun unat_arith_tacs ctxt = |
37660 | 2408 |
let |
2409 |
fun arith_tac' n t = |
|
59657
2441a80fb6c1
eliminated unused arith "verbose" flag -- tools that need options can use the context;
wenzelm
parents:
59498
diff
changeset
|
2410 |
Arith_Data.arith_tac ctxt n t |
37660 | 2411 |
handle Cooper.COOPER _ => Seq.empty; |
65268 | 2412 |
in |
42793 | 2413 |
[ clarify_tac ctxt 1, |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2414 |
full_simp_tac (unat_arith_simpset ctxt) 1, |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2415 |
ALLGOALS (full_simp_tac |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2416 |
(put_simpset HOL_ss ctxt |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2417 |
|> fold Splitter.add_split @{thms unat_splits} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2418 |
|> fold Simplifier.add_cong @{thms power_False_cong})), |
65268 | 2419 |
rewrite_goals_tac ctxt @{thms word_size}, |
60754 | 2420 |
ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN |
2421 |
REPEAT (eresolve_tac ctxt [conjE] n) THEN |
|
2422 |
REPEAT (dresolve_tac ctxt @{thms of_nat_inverse} n THEN assume_tac ctxt n)), |
|
65268 | 2423 |
TRYALL arith_tac' ] |
37660 | 2424 |
end |
2425 |
||
2426 |
fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt)) |
|
61799 | 2427 |
\<close> |
37660 | 2428 |
|
65268 | 2429 |
method_setup unat_arith = |
61799 | 2430 |
\<open>Scan.succeed (SIMPLE_METHOD' o unat_arith_tac)\<close> |
37660 | 2431 |
"solving word arithmetic via natural numbers and arith" |
2432 |
||
65328 | 2433 |
lemma no_plus_overflow_unat_size: "x \<le> x + y \<longleftrightarrow> unat x + unat y < 2 ^ size x" |
2434 |
for x y :: "'a::len word" |
|
37660 | 2435 |
unfolding word_size by unat_arith |
2436 |
||
65328 | 2437 |
lemmas no_olen_add_nat = |
2438 |
no_plus_overflow_unat_size [unfolded word_size] |
|
2439 |
||
2440 |
lemmas unat_plus_simple = |
|
2441 |
trans [OF no_olen_add_nat unat_add_lem] |
|
2442 |
||
70185 | 2443 |
lemma word_div_mult: "0 < y \<Longrightarrow> unat x * unat y < 2 ^ LENGTH('a) \<Longrightarrow> x * y div y = x" |
65328 | 2444 |
for x y :: "'a::len word" |
37660 | 2445 |
apply unat_arith |
2446 |
apply clarsimp |
|
2447 |
apply (subst unat_mult_lem [THEN iffD1]) |
|
65328 | 2448 |
apply auto |
37660 | 2449 |
done |
2450 |
||
70185 | 2451 |
lemma div_lt': "i \<le> k div x \<Longrightarrow> unat i * unat x < 2 ^ LENGTH('a)" |
65328 | 2452 |
for i k x :: "'a::len word" |
37660 | 2453 |
apply unat_arith |
2454 |
apply clarsimp |
|
2455 |
apply (drule mult_le_mono1) |
|
2456 |
apply (erule order_le_less_trans) |
|
2457 |
apply (rule xtr7 [OF unat_lt2p div_mult_le]) |
|
2458 |
done |
|
2459 |
||
2460 |
lemmas div_lt'' = order_less_imp_le [THEN div_lt'] |
|
2461 |
||
65328 | 2462 |
lemma div_lt_mult: "i < k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x < k" |
2463 |
for i k x :: "'a::len word" |
|
37660 | 2464 |
apply (frule div_lt'' [THEN unat_mult_lem [THEN iffD1]]) |
2465 |
apply (simp add: unat_arith_simps) |
|
2466 |
apply (drule (1) mult_less_mono1) |
|
2467 |
apply (erule order_less_le_trans) |
|
2468 |
apply (rule div_mult_le) |
|
2469 |
done |
|
2470 |
||
65328 | 2471 |
lemma div_le_mult: "i \<le> k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x \<le> k" |
2472 |
for i k x :: "'a::len word" |
|
37660 | 2473 |
apply (frule div_lt' [THEN unat_mult_lem [THEN iffD1]]) |
2474 |
apply (simp add: unat_arith_simps) |
|
2475 |
apply (drule mult_le_mono1) |
|
2476 |
apply (erule order_trans) |
|
2477 |
apply (rule div_mult_le) |
|
2478 |
done |
|
2479 |
||
70185 | 2480 |
lemma div_lt_uint': "i \<le> k div x \<Longrightarrow> uint i * uint x < 2 ^ LENGTH('a)" |
65328 | 2481 |
for i k x :: "'a::len word" |
37660 | 2482 |
apply (unfold uint_nat) |
2483 |
apply (drule div_lt') |
|
65328 | 2484 |
apply (metis of_nat_less_iff of_nat_mult of_nat_numeral of_nat_power) |
2485 |
done |
|
37660 | 2486 |
|
2487 |
lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint'] |
|
2488 |
||
70185 | 2489 |
lemma word_le_exists': "x \<le> y \<Longrightarrow> \<exists>z. y = x + z \<and> uint x + uint z < 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2490 |
for x y z :: "'a::len word" |
37660 | 2491 |
apply (rule exI) |
2492 |
apply (rule conjI) |
|
65328 | 2493 |
apply (rule zadd_diff_inverse) |
37660 | 2494 |
apply uint_arith |
2495 |
done |
|
2496 |
||
2497 |
lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab] |
|
2498 |
||
2499 |
lemmas plus_minus_no_overflow = |
|
2500 |
order_less_imp_le [THEN plus_minus_no_overflow_ab] |
|
65268 | 2501 |
|
37660 | 2502 |
lemmas mcs = word_less_minus_cancel word_less_minus_mono_left |
2503 |
word_le_minus_cancel word_le_minus_mono_left |
|
2504 |
||
45604 | 2505 |
lemmas word_l_diffs = mcs [where y = "w + x", unfolded add_diff_cancel] for w x |
2506 |
lemmas word_diff_ls = mcs [where z = "w + x", unfolded add_diff_cancel] for w x |
|
2507 |
lemmas word_plus_mcs = word_diff_ls [where y = "v + x", unfolded add_diff_cancel] for v x |
|
37660 | 2508 |
|
2509 |
lemmas le_unat_uoi = unat_le [THEN word_unat.Abs_inverse] |
|
2510 |
||
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66453
diff
changeset
|
2511 |
lemmas thd = times_div_less_eq_dividend |
37660 | 2512 |
|
65268 | 2513 |
lemmas uno_simps [THEN le_unat_uoi] = mod_le_divisor div_le_dividend dtle |
37660 | 2514 |
|
65328 | 2515 |
lemma word_mod_div_equality: "(n div b) * b + (n mod b) = n" |
2516 |
for n b :: "'a::len word" |
|
37660 | 2517 |
apply (unfold word_less_nat_alt word_arith_nat_defs) |
2518 |
apply (cut_tac y="unat b" in gt_or_eq_0) |
|
2519 |
apply (erule disjE) |
|
64242
93c6f0da5c70
more standardized theorem names for facts involving the div and mod identity
haftmann
parents:
63950
diff
changeset
|
2520 |
apply (simp only: div_mult_mod_eq uno_simps Word.word_unat.Rep_inverse) |
37660 | 2521 |
apply simp |
2522 |
done |
|
2523 |
||
65328 | 2524 |
lemma word_div_mult_le: "a div b * b \<le> a" |
2525 |
for a b :: "'a::len word" |
|
37660 | 2526 |
apply (unfold word_le_nat_alt word_arith_nat_defs) |
2527 |
apply (cut_tac y="unat b" in gt_or_eq_0) |
|
2528 |
apply (erule disjE) |
|
61649
268d88ec9087
Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents:
61424
diff
changeset
|
2529 |
apply (simp only: div_mult_le uno_simps Word.word_unat.Rep_inverse) |
37660 | 2530 |
apply simp |
2531 |
done |
|
2532 |
||
65328 | 2533 |
lemma word_mod_less_divisor: "0 < n \<Longrightarrow> m mod n < n" |
2534 |
for m n :: "'a::len word" |
|
37660 | 2535 |
apply (simp only: word_less_nat_alt word_arith_nat_defs) |
65328 | 2536 |
apply (auto simp: uno_simps) |
37660 | 2537 |
done |
2538 |
||
65328 | 2539 |
lemma word_of_int_power_hom: "word_of_int a ^ n = (word_of_int (a ^ n) :: 'a::len word)" |
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
2540 |
by (induct n) (simp_all add: wi_hom_mult [symmetric]) |
37660 | 2541 |
|
65328 | 2542 |
lemma word_arith_power_alt: "a ^ n = (word_of_int (uint a ^ n) :: 'a::len word)" |
37660 | 2543 |
by (simp add : word_of_int_power_hom [symmetric]) |
2544 |
||
65268 | 2545 |
lemma of_bl_length_less: |
70185 | 2546 |
"length x = k \<Longrightarrow> k < LENGTH('a) \<Longrightarrow> (of_bl x :: 'a::len word) < 2 ^ k" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2547 |
apply (unfold of_bl_def word_less_alt word_numeral_alt) |
37660 | 2548 |
apply safe |
65268 | 2549 |
apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm |
65328 | 2550 |
del: word_of_int_numeral) |
71942 | 2551 |
apply simp |
37660 | 2552 |
apply (subst mod_pos_pos_trivial) |
2553 |
apply (rule bl_to_bin_ge0) |
|
2554 |
apply (rule order_less_trans) |
|
2555 |
apply (rule bl_to_bin_lt2p) |
|
2556 |
apply simp |
|
46646 | 2557 |
apply (rule bl_to_bin_lt2p) |
37660 | 2558 |
done |
2559 |
||
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
2560 |
lemma unatSuc: "1 + n \<noteq> 0 \<Longrightarrow> unat (1 + n) = Suc (unat n)" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
2561 |
for n :: "'a::len word" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
2562 |
by unat_arith |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
2563 |
|
37660 | 2564 |
|
61799 | 2565 |
subsection \<open>Cardinality, finiteness of set of words\<close> |
37660 | 2566 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2567 |
lemma inj_on_word_of_int: \<open>inj_on (word_of_int :: int \<Rightarrow> 'a word) {0..<2 ^ LENGTH('a::len)}\<close> |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2568 |
by (rule inj_onI) (simp add: word.abs_eq_iff take_bit_eq_mod) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2569 |
|
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2570 |
lemma inj_uint: \<open>inj uint\<close> |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2571 |
by (rule injI) simp |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2572 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2573 |
lemma range_uint: \<open>range (uint :: 'a word \<Rightarrow> int) = {0..<2 ^ LENGTH('a::len)}\<close> |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2574 |
by transfer (auto simp add: bintr_lt2p range_bintrunc) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2575 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2576 |
lemma UNIV_eq: \<open>(UNIV :: 'a word set) = word_of_int ` {0..<2 ^ LENGTH('a::len)}\<close> |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2577 |
proof - |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2578 |
have \<open>uint ` (UNIV :: 'a word set) = uint ` (word_of_int :: int \<Rightarrow> 'a word) ` {0..<2 ^ LENGTH('a::len)}\<close> |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2579 |
by (simp add: range_uint image_image uint.abs_eq take_bit_eq_mod) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2580 |
then show ?thesis |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2581 |
using inj_image_eq_iff [of \<open>uint :: 'a word \<Rightarrow> int\<close> \<open>UNIV :: 'a word set\<close> \<open>word_of_int ` {0..<2 ^ LENGTH('a)} :: 'a word set\<close>, OF inj_uint] |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2582 |
by simp |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2583 |
qed |
45809
2bee94cbae72
finite class instance for word type; remove unused lemmas
huffman
parents:
45808
diff
changeset
|
2584 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2585 |
lemma card_word: "CARD('a word) = 2 ^ LENGTH('a::len)" |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2586 |
by (simp add: UNIV_eq card_image inj_on_word_of_int) |
37660 | 2587 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
2588 |
lemma card_word_size: "CARD('a word) = 2 ^ size x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2589 |
for x :: "'a::len word" |
65328 | 2590 |
unfolding word_size by (rule card_word) |
37660 | 2591 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2592 |
instance word :: (len) finite |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2593 |
by standard (simp add: UNIV_eq) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
2594 |
|
37660 | 2595 |
|
61799 | 2596 |
subsection \<open>Bitwise Operations on Words\<close> |
37660 | 2597 |
|
70190 | 2598 |
lemma word_eq_rbl_eq: "x = y \<longleftrightarrow> rev (to_bl x) = rev (to_bl y)" |
2599 |
by simp |
|
2600 |
||
37660 | 2601 |
lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or |
65268 | 2602 |
|
67408 | 2603 |
\<comment> \<open>following definitions require both arithmetic and bit-wise word operations\<close> |
2604 |
||
2605 |
\<comment> \<open>to get \<open>word_no_log_defs\<close> from \<open>word_log_defs\<close>, using \<open>bin_log_bintrs\<close>\<close> |
|
37660 | 2606 |
lemmas wils1 = bin_log_bintrs [THEN word_ubin.norm_eq_iff [THEN iffD1], |
45604 | 2607 |
folded word_ubin.eq_norm, THEN eq_reflection] |
37660 | 2608 |
|
67408 | 2609 |
\<comment> \<open>the binary operations only\<close> (* BH: why is this needed? *) |
65268 | 2610 |
lemmas word_log_binary_defs = |
37660 | 2611 |
word_and_def word_or_def word_xor_def |
2612 |
||
46011 | 2613 |
lemma word_wi_log_defs: |
71149 | 2614 |
"NOT (word_of_int a) = word_of_int (NOT a)" |
46011 | 2615 |
"word_of_int a AND word_of_int b = word_of_int (a AND b)" |
2616 |
"word_of_int a OR word_of_int b = word_of_int (a OR b)" |
|
2617 |
"word_of_int a XOR word_of_int b = word_of_int (a XOR b)" |
|
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
2618 |
by (transfer, rule refl)+ |
47372 | 2619 |
|
46011 | 2620 |
lemma word_no_log_defs [simp]: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2621 |
"NOT (numeral a) = word_of_int (NOT (numeral a))" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2622 |
"NOT (- numeral a) = word_of_int (NOT (- numeral a))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2623 |
"numeral a AND numeral b = word_of_int (numeral a AND numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2624 |
"numeral a AND - numeral b = word_of_int (numeral a AND - numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2625 |
"- numeral a AND numeral b = word_of_int (- numeral a AND numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2626 |
"- numeral a AND - numeral b = word_of_int (- numeral a AND - numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2627 |
"numeral a OR numeral b = word_of_int (numeral a OR numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2628 |
"numeral a OR - numeral b = word_of_int (numeral a OR - numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2629 |
"- numeral a OR numeral b = word_of_int (- numeral a OR numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2630 |
"- numeral a OR - numeral b = word_of_int (- numeral a OR - numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2631 |
"numeral a XOR numeral b = word_of_int (numeral a XOR numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2632 |
"numeral a XOR - numeral b = word_of_int (numeral a XOR - numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2633 |
"- numeral a XOR numeral b = word_of_int (- numeral a XOR numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2634 |
"- numeral a XOR - numeral b = word_of_int (- numeral a XOR - numeral b)" |
47372 | 2635 |
by (transfer, rule refl)+ |
37660 | 2636 |
|
61799 | 2637 |
text \<open>Special cases for when one of the arguments equals 1.\<close> |
46064
88ef116e0522
add simp rules for bitwise word operations with 1
huffman
parents:
46057
diff
changeset
|
2638 |
|
88ef116e0522
add simp rules for bitwise word operations with 1
huffman
parents:
46057
diff
changeset
|
2639 |
lemma word_bitwise_1_simps [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2640 |
"NOT (1::'a::len word) = -2" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2641 |
"1 AND numeral b = word_of_int (1 AND numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2642 |
"1 AND - numeral b = word_of_int (1 AND - numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2643 |
"numeral a AND 1 = word_of_int (numeral a AND 1)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2644 |
"- numeral a AND 1 = word_of_int (- numeral a AND 1)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2645 |
"1 OR numeral b = word_of_int (1 OR numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2646 |
"1 OR - numeral b = word_of_int (1 OR - numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2647 |
"numeral a OR 1 = word_of_int (numeral a OR 1)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2648 |
"- numeral a OR 1 = word_of_int (- numeral a OR 1)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2649 |
"1 XOR numeral b = word_of_int (1 XOR numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2650 |
"1 XOR - numeral b = word_of_int (1 XOR - numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2651 |
"numeral a XOR 1 = word_of_int (numeral a XOR 1)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2652 |
"- numeral a XOR 1 = word_of_int (- numeral a XOR 1)" |
47372 | 2653 |
by (transfer, simp)+ |
46064
88ef116e0522
add simp rules for bitwise word operations with 1
huffman
parents:
46057
diff
changeset
|
2654 |
|
61799 | 2655 |
text \<open>Special cases for when one of the arguments equals -1.\<close> |
56979 | 2656 |
|
2657 |
lemma word_bitwise_m1_simps [simp]: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2658 |
"NOT (-1::'a::len word) = 0" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2659 |
"(-1::'a::len word) AND x = x" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2660 |
"x AND (-1::'a::len word) = x" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2661 |
"(-1::'a::len word) OR x = -1" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2662 |
"x OR (-1::'a::len word) = -1" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2663 |
" (-1::'a::len word) XOR x = NOT x" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2664 |
"x XOR (-1::'a::len word) = NOT x" |
56979 | 2665 |
by (transfer, simp)+ |
2666 |
||
65328 | 2667 |
lemma uint_or: "uint (x OR y) = uint x OR uint y" |
47372 | 2668 |
by (transfer, simp add: bin_trunc_ao) |
37660 | 2669 |
|
65328 | 2670 |
lemma uint_and: "uint (x AND y) = uint x AND uint y" |
47372 | 2671 |
by (transfer, simp add: bin_trunc_ao) |
2672 |
||
2673 |
lemma test_bit_wi [simp]: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2674 |
"(word_of_int x :: 'a::len word) !! n \<longleftrightarrow> n < LENGTH('a) \<and> bin_nth x n" |
65328 | 2675 |
by (simp add: word_test_bit_def word_ubin.eq_norm nth_bintr) |
47372 | 2676 |
|
2677 |
lemma word_test_bit_transfer [transfer_rule]: |
|
67399 | 2678 |
"(rel_fun pcr_word (rel_fun (=) (=))) |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2679 |
(\<lambda>x n. n < LENGTH('a) \<and> bin_nth x n) (test_bit :: 'a::len word \<Rightarrow> _)" |
55945 | 2680 |
unfolding rel_fun_def word.pcr_cr_eq cr_word_def by simp |
37660 | 2681 |
|
2682 |
lemma word_ops_nth_size: |
|
65328 | 2683 |
"n < size x \<Longrightarrow> |
2684 |
(x OR y) !! n = (x !! n | y !! n) \<and> |
|
2685 |
(x AND y) !! n = (x !! n \<and> y !! n) \<and> |
|
2686 |
(x XOR y) !! n = (x !! n \<noteq> y !! n) \<and> |
|
2687 |
(NOT x) !! n = (\<not> x !! n)" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2688 |
for x :: "'a::len word" |
47372 | 2689 |
unfolding word_size by transfer (simp add: bin_nth_ops) |
37660 | 2690 |
|
2691 |
lemma word_ao_nth: |
|
65328 | 2692 |
"(x OR y) !! n = (x !! n | y !! n) \<and> |
2693 |
(x AND y) !! n = (x !! n \<and> y !! n)" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2694 |
for x :: "'a::len word" |
47372 | 2695 |
by transfer (auto simp add: bin_nth_ops) |
46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46022
diff
changeset
|
2696 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2697 |
lemma test_bit_numeral [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2698 |
"(numeral w :: 'a::len word) !! n \<longleftrightarrow> |
70185 | 2699 |
n < LENGTH('a) \<and> bin_nth (numeral w) n" |
47372 | 2700 |
by transfer (rule refl) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2701 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2702 |
lemma test_bit_neg_numeral [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2703 |
"(- numeral w :: 'a::len word) !! n \<longleftrightarrow> |
70185 | 2704 |
n < LENGTH('a) \<and> bin_nth (- numeral w) n" |
47372 | 2705 |
by transfer (rule refl) |
46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46022
diff
changeset
|
2706 |
|
65328 | 2707 |
lemma test_bit_1 [simp]: "(1 :: 'a::len word) !! n \<longleftrightarrow> n = 0" |
47372 | 2708 |
by transfer auto |
65268 | 2709 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2710 |
lemma nth_0 [simp]: "\<not> (0 :: 'a::len word) !! n" |
47372 | 2711 |
by transfer simp |
46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46022
diff
changeset
|
2712 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2713 |
lemma nth_minus1 [simp]: "(-1 :: 'a::len word) !! n \<longleftrightarrow> n < LENGTH('a)" |
47372 | 2714 |
by transfer simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2715 |
|
67408 | 2716 |
\<comment> \<open>get from commutativity, associativity etc of \<open>int_and\<close> etc to same for \<open>word_and etc\<close>\<close> |
65268 | 2717 |
lemmas bwsimps = |
46013 | 2718 |
wi_hom_add |
37660 | 2719 |
word_wi_log_defs |
2720 |
||
2721 |
lemma word_bw_assocs: |
|
2722 |
"(x AND y) AND z = x AND y AND z" |
|
2723 |
"(x OR y) OR z = x OR y OR z" |
|
2724 |
"(x XOR y) XOR z = x XOR y XOR z" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2725 |
for x :: "'a::len word" |
46022 | 2726 |
by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size]) |
65268 | 2727 |
|
37660 | 2728 |
lemma word_bw_comms: |
2729 |
"x AND y = y AND x" |
|
2730 |
"x OR y = y OR x" |
|
2731 |
"x XOR y = y XOR x" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2732 |
for x :: "'a::len word" |
46022 | 2733 |
by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size]) |
65268 | 2734 |
|
37660 | 2735 |
lemma word_bw_lcs: |
2736 |
"y AND x AND z = x AND y AND z" |
|
2737 |
"y OR x OR z = x OR y OR z" |
|
2738 |
"y XOR x XOR z = x XOR y XOR z" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2739 |
for x :: "'a::len word" |
46022 | 2740 |
by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size]) |
37660 | 2741 |
|
2742 |
lemma word_log_esimps [simp]: |
|
2743 |
"x AND 0 = 0" |
|
2744 |
"x AND -1 = x" |
|
2745 |
"x OR 0 = x" |
|
2746 |
"x OR -1 = -1" |
|
2747 |
"x XOR 0 = x" |
|
2748 |
"x XOR -1 = NOT x" |
|
2749 |
"0 AND x = 0" |
|
2750 |
"-1 AND x = x" |
|
2751 |
"0 OR x = x" |
|
2752 |
"-1 OR x = -1" |
|
2753 |
"0 XOR x = x" |
|
2754 |
"-1 XOR x = NOT x" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2755 |
for x :: "'a::len word" |
46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46022
diff
changeset
|
2756 |
by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size]) |
37660 | 2757 |
|
2758 |
lemma word_not_dist: |
|
2759 |
"NOT (x OR y) = NOT x AND NOT y" |
|
2760 |
"NOT (x AND y) = NOT x OR NOT y" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2761 |
for x :: "'a::len word" |
46022 | 2762 |
by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size]) |
37660 | 2763 |
|
2764 |
lemma word_bw_same: |
|
2765 |
"x AND x = x" |
|
2766 |
"x OR x = x" |
|
2767 |
"x XOR x = 0" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2768 |
for x :: "'a::len word" |
46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46022
diff
changeset
|
2769 |
by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size]) |
37660 | 2770 |
|
2771 |
lemma word_ao_absorbs [simp]: |
|
2772 |
"x AND (y OR x) = x" |
|
2773 |
"x OR y AND x = x" |
|
2774 |
"x AND (x OR y) = x" |
|
2775 |
"y AND x OR x = x" |
|
2776 |
"(y OR x) AND x = x" |
|
2777 |
"x OR x AND y = x" |
|
2778 |
"(x OR y) AND x = x" |
|
2779 |
"x AND y OR x = x" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2780 |
for x :: "'a::len word" |
46022 | 2781 |
by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size]) |
37660 | 2782 |
|
71149 | 2783 |
lemma word_not_not [simp]: "NOT (NOT x) = x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2784 |
for x :: "'a::len word" |
46022 | 2785 |
by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size]) |
37660 | 2786 |
|
65328 | 2787 |
lemma word_ao_dist: "(x OR y) AND z = x AND z OR y AND z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2788 |
for x :: "'a::len word" |
46022 | 2789 |
by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size]) |
37660 | 2790 |
|
65328 | 2791 |
lemma word_oa_dist: "x AND y OR z = (x OR z) AND (y OR z)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2792 |
for x :: "'a::len word" |
65328 | 2793 |
by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size]) |
2794 |
||
2795 |
lemma word_add_not [simp]: "x + NOT x = -1" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2796 |
for x :: "'a::len word" |
47372 | 2797 |
by transfer (simp add: bin_add_not) |
37660 | 2798 |
|
65328 | 2799 |
lemma word_plus_and_or [simp]: "(x AND y) + (x OR y) = x + y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2800 |
for x :: "'a::len word" |
47372 | 2801 |
by transfer (simp add: plus_and_or) |
37660 | 2802 |
|
65328 | 2803 |
lemma leoa: "w = x OR y \<Longrightarrow> y = w AND y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2804 |
for x :: "'a::len word" |
65328 | 2805 |
by auto |
2806 |
||
2807 |
lemma leao: "w' = x' AND y' \<Longrightarrow> x' = x' OR w'" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2808 |
for x' :: "'a::len word" |
65328 | 2809 |
by auto |
2810 |
||
2811 |
lemma word_ao_equiv: "w = w OR w' \<longleftrightarrow> w' = w AND w'" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2812 |
for w w' :: "'a::len word" |
48196 | 2813 |
by (auto intro: leoa leao) |
37660 | 2814 |
|
65328 | 2815 |
lemma le_word_or2: "x \<le> x OR y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2816 |
for x y :: "'a::len word" |
65328 | 2817 |
by (auto simp: word_le_def uint_or intro: le_int_or) |
37660 | 2818 |
|
45604 | 2819 |
lemmas le_word_or1 = xtr3 [OF word_bw_comms (2) le_word_or2] |
2820 |
lemmas word_and_le1 = xtr3 [OF word_ao_absorbs (4) [symmetric] le_word_or2] |
|
2821 |
lemmas word_and_le2 = xtr3 [OF word_ao_absorbs (8) [symmetric] le_word_or2] |
|
37660 | 2822 |
|
65268 | 2823 |
lemma bl_word_not: "to_bl (NOT w) = map Not (to_bl w)" |
45550
73a4f31d41c4
Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents:
45549
diff
changeset
|
2824 |
unfolding to_bl_def word_log_defs bl_not_bin |
73a4f31d41c4
Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents:
45549
diff
changeset
|
2825 |
by (simp add: word_ubin.eq_norm) |
37660 | 2826 |
|
67399 | 2827 |
lemma bl_word_xor: "to_bl (v XOR w) = map2 (\<noteq>) (to_bl v) (to_bl w)" |
37660 | 2828 |
unfolding to_bl_def word_log_defs bl_xor_bin |
45550
73a4f31d41c4
Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents:
45549
diff
changeset
|
2829 |
by (simp add: word_ubin.eq_norm) |
37660 | 2830 |
|
67399 | 2831 |
lemma bl_word_or: "to_bl (v OR w) = map2 (\<or>) (to_bl v) (to_bl w)" |
45550
73a4f31d41c4
Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents:
45549
diff
changeset
|
2832 |
unfolding to_bl_def word_log_defs bl_or_bin |
73a4f31d41c4
Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents:
45549
diff
changeset
|
2833 |
by (simp add: word_ubin.eq_norm) |
37660 | 2834 |
|
67399 | 2835 |
lemma bl_word_and: "to_bl (v AND w) = map2 (\<and>) (to_bl v) (to_bl w)" |
45550
73a4f31d41c4
Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents:
45549
diff
changeset
|
2836 |
unfolding to_bl_def word_log_defs bl_and_bin |
73a4f31d41c4
Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents:
45549
diff
changeset
|
2837 |
by (simp add: word_ubin.eq_norm) |
37660 | 2838 |
|
65363 | 2839 |
lemma word_lsb_alt: "lsb w = test_bit w 0" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2840 |
for w :: "'a::len word" |
37660 | 2841 |
by (auto simp: word_test_bit_def word_lsb_def) |
2842 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2843 |
lemma word_lsb_1_0 [simp]: "lsb (1::'a::len word) \<and> \<not> lsb (0::'b::len word)" |
45550
73a4f31d41c4
Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents:
45549
diff
changeset
|
2844 |
unfolding word_lsb_def uint_eq_0 uint_1 by simp |
37660 | 2845 |
|
65363 | 2846 |
lemma word_lsb_last: "lsb w = last (to_bl w)" |
2847 |
for w :: "'a::len word" |
|
65268 | 2848 |
apply (unfold word_lsb_def uint_bl bin_to_bl_def) |
37660 | 2849 |
apply (rule_tac bin="uint w" in bin_exhaust) |
2850 |
apply (cases "size w") |
|
2851 |
apply auto |
|
2852 |
apply (auto simp add: bin_to_bl_aux_alt) |
|
2853 |
done |
|
2854 |
||
65328 | 2855 |
lemma word_lsb_int: "lsb w \<longleftrightarrow> uint w mod 2 = 1" |
2856 |
by (auto simp: word_lsb_def bin_last_def) |
|
2857 |
||
2858 |
lemma word_msb_sint: "msb w \<longleftrightarrow> sint w < 0" |
|
2859 |
by (simp only: word_msb_def sign_Min_lt_0) |
|
2860 |
||
70185 | 2861 |
lemma msb_word_of_int: "msb (word_of_int x::'a::len word) = bin_nth x (LENGTH('a) - 1)" |
65328 | 2862 |
by (simp add: word_msb_def word_sbin.eq_norm bin_sign_lem) |
46173
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2863 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2864 |
lemma word_msb_numeral [simp]: |
70185 | 2865 |
"msb (numeral w::'a::len word) = bin_nth (numeral w) (LENGTH('a) - 1)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2866 |
unfolding word_numeral_alt by (rule msb_word_of_int) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2867 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2868 |
lemma word_msb_neg_numeral [simp]: |
70185 | 2869 |
"msb (- numeral w::'a::len word) = bin_nth (- numeral w) (LENGTH('a) - 1)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2870 |
unfolding word_neg_numeral_alt by (rule msb_word_of_int) |
46173
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2871 |
|
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2872 |
lemma word_msb_0 [simp]: "\<not> msb (0::'a::len word)" |
65328 | 2873 |
by (simp add: word_msb_def) |
46173
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2874 |
|
70185 | 2875 |
lemma word_msb_1 [simp]: "msb (1::'a::len word) \<longleftrightarrow> LENGTH('a) = 1" |
46173
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2876 |
unfolding word_1_wi msb_word_of_int eq_iff [where 'a=nat] |
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2877 |
by (simp add: Suc_le_eq) |
45811 | 2878 |
|
70185 | 2879 |
lemma word_msb_nth: "msb w = bin_nth (uint w) (LENGTH('a) - 1)" |
65328 | 2880 |
for w :: "'a::len word" |
2881 |
by (simp add: word_msb_def sint_uint bin_sign_lem) |
|
2882 |
||
2883 |
lemma word_msb_alt: "msb w = hd (to_bl w)" |
|
2884 |
for w :: "'a::len word" |
|
37660 | 2885 |
apply (unfold word_msb_nth uint_bl) |
2886 |
apply (subst hd_conv_nth) |
|
65328 | 2887 |
apply (rule length_greater_0_conv [THEN iffD1]) |
37660 | 2888 |
apply simp |
2889 |
apply (simp add : nth_bin_to_bl word_size) |
|
2890 |
done |
|
2891 |
||
65328 | 2892 |
lemma word_set_nth [simp]: "set_bit w n (test_bit w n) = w" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2893 |
for w :: "'a::len word" |
65328 | 2894 |
by (auto simp: word_test_bit_def word_set_bit_def) |
2895 |
||
2896 |
lemma bin_nth_uint': "bin_nth (uint w) n \<longleftrightarrow> rev (bin_to_bl (size w) (uint w)) ! n \<and> n < size w" |
|
37660 | 2897 |
apply (unfold word_size) |
2898 |
apply (safe elim!: bin_nth_uint_imp) |
|
2899 |
apply (frule bin_nth_uint_imp) |
|
2900 |
apply (fast dest!: bin_nth_bl)+ |
|
2901 |
done |
|
2902 |
||
2903 |
lemmas bin_nth_uint = bin_nth_uint' [unfolded word_size] |
|
2904 |
||
65328 | 2905 |
lemma test_bit_bl: "w !! n \<longleftrightarrow> rev (to_bl w) ! n \<and> n < size w" |
2906 |
unfolding to_bl_def word_test_bit_def word_size by (rule bin_nth_uint) |
|
37660 | 2907 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2908 |
lemma to_bl_nth: "n < size w \<Longrightarrow> to_bl w ! n = w !! (size w - Suc n)" |
37660 | 2909 |
apply (unfold test_bit_bl) |
2910 |
apply clarsimp |
|
2911 |
apply (rule trans) |
|
2912 |
apply (rule nth_rev_alt) |
|
2913 |
apply (auto simp add: word_size) |
|
2914 |
done |
|
2915 |
||
65328 | 2916 |
lemma test_bit_set: "(set_bit w n x) !! n \<longleftrightarrow> n < size w \<and> x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2917 |
for w :: "'a::len word" |
65328 | 2918 |
by (auto simp: word_size word_test_bit_def word_set_bit_def word_ubin.eq_norm nth_bintr) |
37660 | 2919 |
|
65268 | 2920 |
lemma test_bit_set_gen: |
65328 | 2921 |
"test_bit (set_bit w n x) m = (if m = n then n < size w \<and> x else test_bit w m)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2922 |
for w :: "'a::len word" |
37660 | 2923 |
apply (unfold word_size word_test_bit_def word_set_bit_def) |
2924 |
apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen) |
|
2925 |
apply (auto elim!: test_bit_size [unfolded word_size] |
|
65328 | 2926 |
simp add: word_test_bit_def [symmetric]) |
37660 | 2927 |
done |
2928 |
||
2929 |
lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs" |
|
65363 | 2930 |
by (auto simp: of_bl_def bl_to_bin_rep_F) |
65268 | 2931 |
|
70185 | 2932 |
lemma msb_nth: "msb w = w !! (LENGTH('a) - 1)" |
65328 | 2933 |
for w :: "'a::len word" |
2934 |
by (simp add: word_msb_nth word_test_bit_def) |
|
37660 | 2935 |
|
45604 | 2936 |
lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN word_ops_nth_size [unfolded word_size]] |
37660 | 2937 |
lemmas msb1 = msb0 [where i = 0] |
2938 |
lemmas word_ops_msb = msb1 [unfolded msb_nth [symmetric, unfolded One_nat_def]] |
|
2939 |
||
45604 | 2940 |
lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size]] |
37660 | 2941 |
lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt] |
2942 |
||
65328 | 2943 |
lemma word_set_set_same [simp]: "set_bit (set_bit w n x) n y = set_bit w n y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2944 |
for w :: "'a::len word" |
37660 | 2945 |
by (rule word_eqI) (simp add : test_bit_set_gen word_size) |
65268 | 2946 |
|
2947 |
lemma word_set_set_diff: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2948 |
fixes w :: "'a::len word" |
65328 | 2949 |
assumes "m \<noteq> n" |
65268 | 2950 |
shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x" |
65328 | 2951 |
by (rule word_eqI) (auto simp: test_bit_set_gen word_size assms) |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
2952 |
|
65268 | 2953 |
lemma nth_sint: |
37660 | 2954 |
fixes w :: "'a::len word" |
70185 | 2955 |
defines "l \<equiv> LENGTH('a)" |
37660 | 2956 |
shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))" |
2957 |
unfolding sint_uint l_def |
|
65328 | 2958 |
by (auto simp: nth_sbintr word_test_bit_def [symmetric]) |
37660 | 2959 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2960 |
lemma word_lsb_numeral [simp]: |
65268 | 2961 |
"lsb (numeral bin :: 'a::len word) \<longleftrightarrow> bin_last (numeral bin)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2962 |
unfolding word_lsb_alt test_bit_numeral by simp |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2963 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2964 |
lemma word_lsb_neg_numeral [simp]: |
65268 | 2965 |
"lsb (- numeral bin :: 'a::len word) \<longleftrightarrow> bin_last (- numeral bin)" |
65328 | 2966 |
by (simp add: word_lsb_alt) |
2967 |
||
2968 |
lemma set_bit_word_of_int: "set_bit (word_of_int x) n b = word_of_int (bin_sc n b x)" |
|
46173
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2969 |
unfolding word_set_bit_def |
65328 | 2970 |
by (rule word_eqI)(simp add: word_size bin_nth_sc_gen word_ubin.eq_norm nth_bintr) |
46173
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2971 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2972 |
lemma word_set_numeral [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2973 |
"set_bit (numeral bin::'a::len word) n b = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54743
diff
changeset
|
2974 |
word_of_int (bin_sc n b (numeral bin))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2975 |
unfolding word_numeral_alt by (rule set_bit_word_of_int) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2976 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2977 |
lemma word_set_neg_numeral [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2978 |
"set_bit (- numeral bin::'a::len word) n b = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54743
diff
changeset
|
2979 |
word_of_int (bin_sc n b (- numeral bin))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2980 |
unfolding word_neg_numeral_alt by (rule set_bit_word_of_int) |
46173
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2981 |
|
65328 | 2982 |
lemma word_set_bit_0 [simp]: "set_bit 0 n b = word_of_int (bin_sc n b 0)" |
46173
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2983 |
unfolding word_0_wi by (rule set_bit_word_of_int) |
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2984 |
|
65328 | 2985 |
lemma word_set_bit_1 [simp]: "set_bit 1 n b = word_of_int (bin_sc n b 1)" |
46173
5cc700033194
add simp rules for set_bit and msb applied to 0 and 1
huffman
parents:
46172
diff
changeset
|
2986 |
unfolding word_1_wi by (rule set_bit_word_of_int) |
37660 | 2987 |
|
65328 | 2988 |
lemma setBit_no [simp]: "setBit (numeral bin) n = word_of_int (bin_sc n True (numeral bin))" |
45805 | 2989 |
by (simp add: setBit_def) |
2990 |
||
2991 |
lemma clearBit_no [simp]: |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54743
diff
changeset
|
2992 |
"clearBit (numeral bin) n = word_of_int (bin_sc n False (numeral bin))" |
45805 | 2993 |
by (simp add: clearBit_def) |
37660 | 2994 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2995 |
lemma to_bl_n1 [simp]: "to_bl (-1::'a::len word) = replicate (LENGTH('a)) True" |
37660 | 2996 |
apply (rule word_bl.Abs_inverse') |
2997 |
apply simp |
|
2998 |
apply (rule word_eqI) |
|
45805 | 2999 |
apply (clarsimp simp add: word_size) |
37660 | 3000 |
apply (auto simp add: word_bl.Abs_inverse test_bit_bl word_size) |
3001 |
done |
|
3002 |
||
45805 | 3003 |
lemma word_msb_n1 [simp]: "msb (-1::'a::len word)" |
41550 | 3004 |
unfolding word_msb_alt to_bl_n1 by simp |
37660 | 3005 |
|
65328 | 3006 |
lemma word_set_nth_iff: "set_bit w n b = w \<longleftrightarrow> w !! n = b \<or> n \<ge> size w" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3007 |
for w :: "'a::len word" |
37660 | 3008 |
apply (rule iffI) |
3009 |
apply (rule disjCI) |
|
3010 |
apply (drule word_eqD) |
|
3011 |
apply (erule sym [THEN trans]) |
|
3012 |
apply (simp add: test_bit_set) |
|
3013 |
apply (erule disjE) |
|
3014 |
apply clarsimp |
|
3015 |
apply (rule word_eqI) |
|
3016 |
apply (clarsimp simp add : test_bit_set_gen) |
|
3017 |
apply (drule test_bit_size) |
|
3018 |
apply force |
|
3019 |
done |
|
3020 |
||
70185 | 3021 |
lemma test_bit_2p: "(word_of_int (2 ^ n)::'a::len word) !! m \<longleftrightarrow> m = n \<and> m < LENGTH('a)" |
65328 | 3022 |
by (auto simp: word_test_bit_def word_ubin.eq_norm nth_bintr nth_2p_bin) |
3023 |
||
70185 | 3024 |
lemma nth_w2p: "((2::'a::len word) ^ n) !! m \<longleftrightarrow> m = n \<and> m < LENGTH('a::len)" |
65328 | 3025 |
by (simp add: test_bit_2p [symmetric] word_of_int [symmetric]) |
3026 |
||
3027 |
lemma uint_2p: "(0::'a::len word) < 2 ^ n \<Longrightarrow> uint (2 ^ n::'a::len word) = 2 ^ n" |
|
37660 | 3028 |
apply (unfold word_arith_power_alt) |
70185 | 3029 |
apply (case_tac "LENGTH('a)") |
37660 | 3030 |
apply clarsimp |
3031 |
apply (case_tac "nat") |
|
3032 |
apply clarsimp |
|
3033 |
apply (case_tac "n") |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3034 |
apply clarsimp |
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3035 |
apply clarsimp |
37660 | 3036 |
apply (drule word_gt_0 [THEN iffD1]) |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3037 |
apply (safe intro!: word_eqI) |
65328 | 3038 |
apply (auto simp add: nth_2p_bin) |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3039 |
apply (erule notE) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3040 |
apply (simp (no_asm_use) add: uint_word_of_int word_size) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3041 |
apply (subst mod_pos_pos_trivial) |
65328 | 3042 |
apply simp |
3043 |
apply (rule power_strict_increasing) |
|
3044 |
apply simp_all |
|
37660 | 3045 |
done |
3046 |
||
65268 | 3047 |
lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a::len word) = 2 ^ n" |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
3048 |
by (induct n) (simp_all add: wi_hom_syms) |
37660 | 3049 |
|
65328 | 3050 |
lemma bang_is_le: "x !! m \<Longrightarrow> 2 ^ m \<le> x" |
3051 |
for x :: "'a::len word" |
|
65268 | 3052 |
apply (rule xtr3) |
65328 | 3053 |
apply (rule_tac [2] y = "x" in le_word_or2) |
37660 | 3054 |
apply (rule word_eqI) |
3055 |
apply (auto simp add: word_ao_nth nth_w2p word_size) |
|
3056 |
done |
|
3057 |
||
65328 | 3058 |
lemma word_clr_le: "w \<ge> set_bit w n False" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3059 |
for w :: "'a::len word" |
37660 | 3060 |
apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm) |
3061 |
apply (rule order_trans) |
|
3062 |
apply (rule bintr_bin_clr_le) |
|
3063 |
apply simp |
|
3064 |
done |
|
3065 |
||
65328 | 3066 |
lemma word_set_ge: "w \<le> set_bit w n True" |
3067 |
for w :: "'a::len word" |
|
37660 | 3068 |
apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm) |
3069 |
apply (rule order_trans [OF _ bintr_bin_set_ge]) |
|
3070 |
apply simp |
|
3071 |
done |
|
3072 |
||
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3073 |
lemma set_bit_beyond: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3074 |
"size x \<le> n \<Longrightarrow> set_bit x n b = x" for x :: "'a :: len word" |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3075 |
by (auto intro: word_eqI simp add: test_bit_set_gen word_size) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3076 |
|
70190 | 3077 |
lemma rbl_word_or: "rev (to_bl (x OR y)) = map2 (\<or>) (rev (to_bl x)) (rev (to_bl y))" |
70193 | 3078 |
by (simp add: zip_rev bl_word_or rev_map) |
70190 | 3079 |
|
3080 |
lemma rbl_word_and: "rev (to_bl (x AND y)) = map2 (\<and>) (rev (to_bl x)) (rev (to_bl y))" |
|
70193 | 3081 |
by (simp add: zip_rev bl_word_and rev_map) |
70190 | 3082 |
|
3083 |
lemma rbl_word_xor: "rev (to_bl (x XOR y)) = map2 (\<noteq>) (rev (to_bl x)) (rev (to_bl y))" |
|
70193 | 3084 |
by (simp add: zip_rev bl_word_xor rev_map) |
70190 | 3085 |
|
3086 |
lemma rbl_word_not: "rev (to_bl (NOT x)) = map Not (rev (to_bl x))" |
|
3087 |
by (simp add: bl_word_not rev_map) |
|
3088 |
||
37660 | 3089 |
|
70192 | 3090 |
subsection \<open>Bit comprehension\<close> |
3091 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3092 |
instantiation word :: (len) bit_comprehension |
70192 | 3093 |
begin |
3094 |
||
3095 |
definition word_set_bits_def: "(BITS n. f n) = of_bl (bl_of_nth LENGTH('a) f)" |
|
3096 |
||
3097 |
instance .. |
|
3098 |
||
3099 |
end |
|
3100 |
||
3101 |
lemmas of_nth_def = word_set_bits_def (* FIXME duplicate *) |
|
3102 |
||
3103 |
lemma td_ext_nth [OF refl refl refl, unfolded word_size]: |
|
3104 |
"n = size w \<Longrightarrow> ofn = set_bits \<Longrightarrow> [w, ofn g] = l \<Longrightarrow> |
|
3105 |
td_ext test_bit ofn {f. \<forall>i. f i \<longrightarrow> i < n} (\<lambda>h i. h i \<and> i < n)" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3106 |
for w :: "'a::len word" |
70192 | 3107 |
apply (unfold word_size td_ext_def') |
3108 |
apply safe |
|
3109 |
apply (rule_tac [3] ext) |
|
3110 |
apply (rule_tac [4] ext) |
|
3111 |
apply (unfold word_size of_nth_def test_bit_bl) |
|
3112 |
apply safe |
|
3113 |
defer |
|
3114 |
apply (clarsimp simp: word_bl.Abs_inverse)+ |
|
3115 |
apply (rule word_bl.Rep_inverse') |
|
3116 |
apply (rule sym [THEN trans]) |
|
3117 |
apply (rule bl_of_nth_nth) |
|
3118 |
apply simp |
|
3119 |
apply (rule bl_of_nth_inj) |
|
3120 |
apply (clarsimp simp add : test_bit_bl word_size) |
|
3121 |
done |
|
3122 |
||
3123 |
interpretation test_bit: |
|
3124 |
td_ext |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3125 |
"(!!) :: 'a::len word \<Rightarrow> nat \<Rightarrow> bool" |
70192 | 3126 |
set_bits |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3127 |
"{f. \<forall>i. f i \<longrightarrow> i < LENGTH('a::len)}" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3128 |
"(\<lambda>h i. h i \<and> i < LENGTH('a::len))" |
70192 | 3129 |
by (rule td_ext_nth) |
3130 |
||
3131 |
lemmas td_nth = test_bit.td_thm |
|
3132 |
||
3133 |
lemma set_bits_K_False [simp]: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3134 |
"set_bits (\<lambda>_. False) = (0 :: 'a :: len word)" |
70192 | 3135 |
by (rule word_eqI) (simp add: test_bit.eq_norm) |
3136 |
||
3137 |
||
61799 | 3138 |
subsection \<open>Shifting, Rotating, and Splitting Words\<close> |
37660 | 3139 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54743
diff
changeset
|
3140 |
lemma shiftl1_wi [simp]: "shiftl1 (word_of_int w) = word_of_int (w BIT False)" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3141 |
unfolding shiftl1_def |
71947 | 3142 |
apply (simp add: word_ubin.norm_eq_iff [symmetric] word_ubin.eq_norm Bit_def) |
3143 |
apply (simp add: mod_mult_right_eq take_bit_eq_mod) |
|
37660 | 3144 |
done |
3145 |
||
65328 | 3146 |
lemma shiftl1_numeral [simp]: "shiftl1 (numeral w) = numeral (Num.Bit0 w)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3147 |
unfolding word_numeral_alt shiftl1_wi by simp |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3148 |
|
65328 | 3149 |
lemma shiftl1_neg_numeral [simp]: "shiftl1 (- numeral w) = - numeral (Num.Bit0 w)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3150 |
unfolding word_neg_numeral_alt shiftl1_wi by simp |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3151 |
|
37660 | 3152 |
lemma shiftl1_0 [simp] : "shiftl1 0 = 0" |
65328 | 3153 |
by (simp add: shiftl1_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
|
3154 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54743
diff
changeset
|
3155 |
lemma shiftl1_def_u: "shiftl1 w = word_of_int (uint w BIT False)" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3156 |
by (simp only: shiftl1_def) (* FIXME: duplicate *) |
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3157 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54743
diff
changeset
|
3158 |
lemma shiftl1_def_s: "shiftl1 w = word_of_int (sint w BIT False)" |
65328 | 3159 |
by (simp add: shiftl1_def Bit_B0 wi_hom_syms) |
37660 | 3160 |
|
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
3161 |
lemma shiftr1_0 [simp]: "shiftr1 0 = 0" |
65328 | 3162 |
by (simp add: shiftr1_def) |
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
3163 |
|
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
3164 |
lemma sshiftr1_0 [simp]: "sshiftr1 0 = 0" |
65328 | 3165 |
by (simp add: sshiftr1_def) |
3166 |
||
3167 |
lemma sshiftr1_n1 [simp]: "sshiftr1 (- 1) = - 1" |
|
3168 |
by (simp add: sshiftr1_def) |
|
3169 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3170 |
lemma shiftl_0 [simp]: "(0::'a::len word) << n = 0" |
65328 | 3171 |
by (induct n) (auto simp: shiftl_def) |
3172 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3173 |
lemma shiftr_0 [simp]: "(0::'a::len word) >> n = 0" |
65328 | 3174 |
by (induct n) (auto simp: shiftr_def) |
3175 |
||
3176 |
lemma sshiftr_0 [simp]: "0 >>> n = 0" |
|
3177 |
by (induct n) (auto simp: sshiftr_def) |
|
3178 |
||
3179 |
lemma sshiftr_n1 [simp]: "-1 >>> n = -1" |
|
3180 |
by (induct n) (auto simp: sshiftr_def) |
|
3181 |
||
3182 |
lemma nth_shiftl1: "shiftl1 w !! n \<longleftrightarrow> n < size w \<and> n > 0 \<and> w !! (n - 1)" |
|
37660 | 3183 |
apply (unfold shiftl1_def word_test_bit_def) |
3184 |
apply (simp add: nth_bintr word_ubin.eq_norm word_size) |
|
3185 |
apply (cases n) |
|
3186 |
apply auto |
|
3187 |
done |
|
3188 |
||
65328 | 3189 |
lemma nth_shiftl': "(w << m) !! n \<longleftrightarrow> n < size w \<and> n >= m \<and> w !! (n - m)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3190 |
for w :: "'a::len word" |
37660 | 3191 |
apply (unfold shiftl_def) |
65328 | 3192 |
apply (induct m arbitrary: n) |
37660 | 3193 |
apply (force elim!: test_bit_size) |
3194 |
apply (clarsimp simp add : nth_shiftl1 word_size) |
|
3195 |
apply arith |
|
3196 |
done |
|
3197 |
||
65268 | 3198 |
lemmas nth_shiftl = nth_shiftl' [unfolded word_size] |
37660 | 3199 |
|
3200 |
lemma nth_shiftr1: "shiftr1 w !! n = w !! Suc n" |
|
71949 | 3201 |
apply (auto simp add: shiftr1_def word_test_bit_def word_ubin.eq_norm bit_take_bit_iff bit_Suc) |
3202 |
apply (metis (no_types, hide_lams) add_Suc_right add_diff_cancel_left' bit_Suc diff_is_0_eq' le_Suc_ex less_imp_le linorder_not_le test_bit_bin word_test_bit_def) |
|
37660 | 3203 |
done |
3204 |
||
65328 | 3205 |
lemma nth_shiftr: "(w >> m) !! n = w !! (n + m)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3206 |
for w :: "'a::len word" |
37660 | 3207 |
apply (unfold shiftr_def) |
65328 | 3208 |
apply (induct "m" arbitrary: n) |
3209 |
apply (auto simp add: nth_shiftr1) |
|
37660 | 3210 |
done |
65268 | 3211 |
|
67408 | 3212 |
text \<open> |
3213 |
see paper page 10, (1), (2), \<open>shiftr1_def\<close> is of the form of (1), |
|
3214 |
where \<open>f\<close> (ie \<open>bin_rest\<close>) takes normal arguments to normal results, |
|
3215 |
thus we get (2) from (1) |
|
3216 |
\<close> |
|
37660 | 3217 |
|
65268 | 3218 |
lemma uint_shiftr1: "uint (shiftr1 w) = bin_rest (uint w)" |
37660 | 3219 |
apply (unfold shiftr1_def word_ubin.eq_norm bin_rest_trunc_i) |
3220 |
apply (subst bintr_uint [symmetric, OF order_refl]) |
|
3221 |
apply (simp only : bintrunc_bintrunc_l) |
|
65268 | 3222 |
apply simp |
37660 | 3223 |
done |
3224 |
||
65328 | 3225 |
lemma nth_sshiftr1: "sshiftr1 w !! n = (if n = size w - 1 then w !! n else w !! Suc n)" |
37660 | 3226 |
apply (unfold sshiftr1_def word_test_bit_def) |
71949 | 3227 |
apply (simp add: nth_bintr word_ubin.eq_norm bit_Suc [symmetric] word_size) |
3228 |
apply (simp add: nth_bintr uint_sint) |
|
37660 | 3229 |
apply (auto simp add: bin_nth_sint) |
3230 |
done |
|
3231 |
||
65268 | 3232 |
lemma nth_sshiftr [rule_format] : |
65328 | 3233 |
"\<forall>n. sshiftr w m !! n = |
3234 |
(n < size w \<and> (if n + m \<ge> size w then w !! (size w - 1) else w !! (n + m)))" |
|
37660 | 3235 |
apply (unfold sshiftr_def) |
65328 | 3236 |
apply (induct_tac m) |
37660 | 3237 |
apply (simp add: test_bit_bl) |
3238 |
apply (clarsimp simp add: nth_sshiftr1 word_size) |
|
3239 |
apply safe |
|
3240 |
apply arith |
|
3241 |
apply arith |
|
3242 |
apply (erule thin_rl) |
|
3243 |
apply (case_tac n) |
|
3244 |
apply safe |
|
3245 |
apply simp |
|
3246 |
apply simp |
|
3247 |
apply (erule thin_rl) |
|
3248 |
apply (case_tac n) |
|
3249 |
apply safe |
|
3250 |
apply simp |
|
3251 |
apply simp |
|
3252 |
apply arith+ |
|
3253 |
done |
|
65268 | 3254 |
|
37660 | 3255 |
lemma shiftr1_div_2: "uint (shiftr1 w) = uint w div 2" |
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
3256 |
apply (unfold shiftr1_def) |
37660 | 3257 |
apply (rule word_uint.Abs_inverse) |
3258 |
apply (simp add: uints_num pos_imp_zdiv_nonneg_iff) |
|
3259 |
apply (rule xtr7) |
|
3260 |
prefer 2 |
|
3261 |
apply (rule zdiv_le_dividend) |
|
3262 |
apply auto |
|
3263 |
done |
|
3264 |
||
3265 |
lemma sshiftr1_div_2: "sint (sshiftr1 w) = sint w div 2" |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
3266 |
apply (unfold sshiftr1_def) |
37660 | 3267 |
apply (simp add: word_sbin.eq_norm) |
3268 |
apply (rule trans) |
|
3269 |
defer |
|
3270 |
apply (subst word_sbin.norm_Rep [symmetric]) |
|
3271 |
apply (rule refl) |
|
3272 |
apply (subst word_sbin.norm_Rep [symmetric]) |
|
3273 |
apply (unfold One_nat_def) |
|
3274 |
apply (rule sbintrunc_rest) |
|
3275 |
done |
|
3276 |
||
3277 |
lemma shiftr_div_2n: "uint (shiftr w n) = uint w div 2 ^ n" |
|
3278 |
apply (unfold shiftr_def) |
|
65328 | 3279 |
apply (induct n) |
37660 | 3280 |
apply simp |
65328 | 3281 |
apply (simp add: shiftr1_div_2 mult.commute zdiv_zmult2_eq [symmetric]) |
37660 | 3282 |
done |
3283 |
||
3284 |
lemma sshiftr_div_2n: "sint (sshiftr w n) = sint w div 2 ^ n" |
|
3285 |
apply (unfold sshiftr_def) |
|
65328 | 3286 |
apply (induct n) |
37660 | 3287 |
apply simp |
65328 | 3288 |
apply (simp add: sshiftr1_div_2 mult.commute zdiv_zmult2_eq [symmetric]) |
37660 | 3289 |
done |
3290 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
3291 |
|
61799 | 3292 |
subsubsection \<open>shift functions in terms of lists of bools\<close> |
37660 | 3293 |
|
65268 | 3294 |
lemmas bshiftr1_numeral [simp] = |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3295 |
bshiftr1_def [where w="numeral w", unfolded to_bl_numeral] for w |
37660 | 3296 |
|
3297 |
lemma bshiftr1_bl: "to_bl (bshiftr1 b w) = b # butlast (to_bl w)" |
|
3298 |
unfolding bshiftr1_def by (rule word_bl.Abs_inverse) simp |
|
3299 |
||
3300 |
lemma shiftl1_of_bl: "shiftl1 (of_bl bl) = of_bl (bl @ [False])" |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3301 |
by (simp add: of_bl_def bl_to_bin_append) |
37660 | 3302 |
|
65363 | 3303 |
lemma shiftl1_bl: "shiftl1 w = of_bl (to_bl w @ [False])" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3304 |
for w :: "'a::len word" |
37660 | 3305 |
proof - |
65328 | 3306 |
have "shiftl1 w = shiftl1 (of_bl (to_bl w))" |
3307 |
by simp |
|
3308 |
also have "\<dots> = of_bl (to_bl w @ [False])" |
|
3309 |
by (rule shiftl1_of_bl) |
|
37660 | 3310 |
finally show ?thesis . |
3311 |
qed |
|
3312 |
||
65328 | 3313 |
lemma bl_shiftl1: "to_bl (shiftl1 w) = tl (to_bl w) @ [False]" |
3314 |
for w :: "'a::len word" |
|
3315 |
by (simp add: shiftl1_bl word_rep_drop drop_Suc drop_Cons') (fast intro!: Suc_leI) |
|
37660 | 3316 |
|
67408 | 3317 |
\<comment> \<open>Generalized version of \<open>bl_shiftl1\<close>. Maybe this one should replace it?\<close> |
65328 | 3318 |
lemma bl_shiftl1': "to_bl (shiftl1 w) = tl (to_bl w @ [False])" |
3319 |
by (simp add: shiftl1_bl word_rep_drop drop_Suc del: drop_append) |
|
45807 | 3320 |
|
37660 | 3321 |
lemma shiftr1_bl: "shiftr1 w = of_bl (butlast (to_bl w))" |
3322 |
apply (unfold shiftr1_def uint_bl of_bl_def) |
|
3323 |
apply (simp add: butlast_rest_bin word_size) |
|
3324 |
apply (simp add: bin_rest_trunc [symmetric, unfolded One_nat_def]) |
|
3325 |
done |
|
3326 |
||
65328 | 3327 |
lemma bl_shiftr1: "to_bl (shiftr1 w) = False # butlast (to_bl w)" |
3328 |
for w :: "'a::len word" |
|
3329 |
by (simp add: shiftr1_bl word_rep_drop len_gt_0 [THEN Suc_leI]) |
|
37660 | 3330 |
|
67408 | 3331 |
\<comment> \<open>Generalized version of \<open>bl_shiftr1\<close>. Maybe this one should replace it?\<close> |
65328 | 3332 |
lemma bl_shiftr1': "to_bl (shiftr1 w) = butlast (False # to_bl w)" |
45807 | 3333 |
apply (rule word_bl.Abs_inverse') |
65328 | 3334 |
apply (simp del: butlast.simps) |
45807 | 3335 |
apply (simp add: shiftr1_bl of_bl_def) |
3336 |
done |
|
3337 |
||
65328 | 3338 |
lemma shiftl1_rev: "shiftl1 w = word_reverse (shiftr1 (word_reverse w))" |
37660 | 3339 |
apply (unfold word_reverse_def) |
3340 |
apply (rule word_bl.Rep_inverse' [symmetric]) |
|
45807 | 3341 |
apply (simp add: bl_shiftl1' bl_shiftr1' word_bl.Abs_inverse) |
37660 | 3342 |
done |
3343 |
||
65328 | 3344 |
lemma shiftl_rev: "shiftl w n = word_reverse (shiftr (word_reverse w) n)" |
3345 |
by (induct n) (auto simp add: shiftl_def shiftr_def shiftl1_rev) |
|
37660 | 3346 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3347 |
lemma rev_shiftl: "word_reverse w << n = word_reverse (w >> n)" |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3348 |
by (simp add: shiftl_rev) |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3349 |
|
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3350 |
lemma shiftr_rev: "w >> n = word_reverse (word_reverse w << n)" |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3351 |
by (simp add: rev_shiftl) |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3352 |
|
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3353 |
lemma rev_shiftr: "word_reverse w >> n = word_reverse (w << n)" |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3354 |
by (simp add: shiftr_rev) |
37660 | 3355 |
|
65328 | 3356 |
lemma bl_sshiftr1: "to_bl (sshiftr1 w) = hd (to_bl w) # butlast (to_bl w)" |
3357 |
for w :: "'a::len word" |
|
37660 | 3358 |
apply (unfold sshiftr1_def uint_bl word_size) |
3359 |
apply (simp add: butlast_rest_bin word_ubin.eq_norm) |
|
3360 |
apply (simp add: sint_uint) |
|
3361 |
apply (rule nth_equalityI) |
|
3362 |
apply clarsimp |
|
3363 |
apply clarsimp |
|
3364 |
apply (case_tac i) |
|
3365 |
apply (simp_all add: hd_conv_nth length_0_conv [symmetric] |
|
71949 | 3366 |
nth_bin_to_bl bit_Suc [symmetric] nth_sbintr) |
37660 | 3367 |
apply force |
3368 |
apply (rule impI) |
|
3369 |
apply (rule_tac f = "bin_nth (uint w)" in arg_cong) |
|
3370 |
apply simp |
|
3371 |
done |
|
3372 |
||
65328 | 3373 |
lemma drop_shiftr: "drop n (to_bl (w >> n)) = take (size w - n) (to_bl w)" |
3374 |
for w :: "'a::len word" |
|
37660 | 3375 |
apply (unfold shiftr_def) |
3376 |
apply (induct n) |
|
3377 |
prefer 2 |
|
3378 |
apply (simp add: drop_Suc bl_shiftr1 butlast_drop [symmetric]) |
|
3379 |
apply (rule butlast_take [THEN trans]) |
|
65328 | 3380 |
apply (auto simp: word_size) |
37660 | 3381 |
done |
3382 |
||
65328 | 3383 |
lemma drop_sshiftr: "drop n (to_bl (w >>> n)) = take (size w - n) (to_bl w)" |
3384 |
for w :: "'a::len word" |
|
37660 | 3385 |
apply (unfold sshiftr_def) |
3386 |
apply (induct n) |
|
3387 |
prefer 2 |
|
3388 |
apply (simp add: drop_Suc bl_sshiftr1 butlast_drop [symmetric]) |
|
3389 |
apply (rule butlast_take [THEN trans]) |
|
65328 | 3390 |
apply (auto simp: word_size) |
37660 | 3391 |
done |
3392 |
||
65328 | 3393 |
lemma take_shiftr: "n \<le> size w \<Longrightarrow> take n (to_bl (w >> n)) = replicate n False" |
37660 | 3394 |
apply (unfold shiftr_def) |
3395 |
apply (induct n) |
|
3396 |
prefer 2 |
|
45807 | 3397 |
apply (simp add: bl_shiftr1' length_0_conv [symmetric] word_size) |
37660 | 3398 |
apply (rule take_butlast [THEN trans]) |
65328 | 3399 |
apply (auto simp: word_size) |
37660 | 3400 |
done |
3401 |
||
3402 |
lemma take_sshiftr' [rule_format] : |
|
65328 | 3403 |
"n \<le> size w \<longrightarrow> hd (to_bl (w >>> n)) = hd (to_bl w) \<and> |
65268 | 3404 |
take n (to_bl (w >>> n)) = replicate n (hd (to_bl w))" |
65328 | 3405 |
for w :: "'a::len word" |
37660 | 3406 |
apply (unfold sshiftr_def) |
3407 |
apply (induct n) |
|
3408 |
prefer 2 |
|
3409 |
apply (simp add: bl_sshiftr1) |
|
3410 |
apply (rule impI) |
|
3411 |
apply (rule take_butlast [THEN trans]) |
|
65328 | 3412 |
apply (auto simp: word_size) |
37660 | 3413 |
done |
3414 |
||
45604 | 3415 |
lemmas hd_sshiftr = take_sshiftr' [THEN conjunct1] |
3416 |
lemmas take_sshiftr = take_sshiftr' [THEN conjunct2] |
|
37660 | 3417 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3418 |
lemma atd_lem: "take n xs = t \<Longrightarrow> drop n xs = d \<Longrightarrow> xs = t @ d" |
37660 | 3419 |
by (auto intro: append_take_drop_id [symmetric]) |
3420 |
||
3421 |
lemmas bl_shiftr = atd_lem [OF take_shiftr drop_shiftr] |
|
3422 |
lemmas bl_sshiftr = atd_lem [OF take_sshiftr drop_sshiftr] |
|
3423 |
||
3424 |
lemma shiftl_of_bl: "of_bl bl << n = of_bl (bl @ replicate n False)" |
|
65328 | 3425 |
by (induct n) (auto simp: shiftl_def shiftl1_of_bl replicate_app_Cons_same) |
3426 |
||
3427 |
lemma shiftl_bl: "w << n = of_bl (to_bl w @ replicate n False)" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3428 |
for w :: "'a::len word" |
37660 | 3429 |
proof - |
65328 | 3430 |
have "w << n = of_bl (to_bl w) << n" |
3431 |
by simp |
|
3432 |
also have "\<dots> = of_bl (to_bl w @ replicate n False)" |
|
3433 |
by (rule shiftl_of_bl) |
|
37660 | 3434 |
finally show ?thesis . |
3435 |
qed |
|
3436 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3437 |
lemmas shiftl_numeral [simp] = shiftl_def [where w="numeral w"] for w |
37660 | 3438 |
|
65328 | 3439 |
lemma bl_shiftl: "to_bl (w << n) = drop n (to_bl w) @ replicate (min (size w) n) False" |
37660 | 3440 |
by (simp add: shiftl_bl word_rep_drop word_size) |
3441 |
||
65328 | 3442 |
lemma shiftl_zero_size: "size x \<le> n \<Longrightarrow> x << n = 0" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3443 |
for x :: "'a::len word" |
37660 | 3444 |
apply (unfold word_size) |
3445 |
apply (rule word_eqI) |
|
3446 |
apply (clarsimp simp add: shiftl_bl word_size test_bit_of_bl nth_append) |
|
3447 |
done |
|
3448 |
||
67408 | 3449 |
\<comment> \<open>note -- the following results use \<open>'a::len word < number_ring\<close>\<close> |
65268 | 3450 |
|
65328 | 3451 |
lemma shiftl1_2t: "shiftl1 w = 2 * w" |
3452 |
for w :: "'a::len word" |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3453 |
by (simp add: shiftl1_def Bit_def wi_hom_mult [symmetric]) |
37660 | 3454 |
|
65328 | 3455 |
lemma shiftl1_p: "shiftl1 w = w + w" |
3456 |
for w :: "'a::len word" |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3457 |
by (simp add: shiftl1_2t) |
37660 | 3458 |
|
65328 | 3459 |
lemma shiftl_t2n: "shiftl w n = 2 ^ n * w" |
3460 |
for w :: "'a::len word" |
|
3461 |
by (induct n) (auto simp: shiftl_def shiftl1_2t) |
|
37660 | 3462 |
|
3463 |
lemma shiftr1_bintr [simp]: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3464 |
"(shiftr1 (numeral w) :: 'a::len word) = |
70185 | 3465 |
word_of_int (bin_rest (bintrunc (LENGTH('a)) (numeral w)))" |
65328 | 3466 |
unfolding shiftr1_def word_numeral_alt by (simp add: word_ubin.eq_norm) |
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
3467 |
|
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
3468 |
lemma sshiftr1_sbintr [simp]: |
65268 | 3469 |
"(sshiftr1 (numeral w) :: 'a::len word) = |
70185 | 3470 |
word_of_int (bin_rest (sbintrunc (LENGTH('a) - 1) (numeral w)))" |
65328 | 3471 |
unfolding sshiftr1_def word_numeral_alt by (simp add: word_sbin.eq_norm) |
37660 | 3472 |
|
46057 | 3473 |
lemma shiftr_no [simp]: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3474 |
(* FIXME: neg_numeral *) |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3475 |
"(numeral w::'a::len word) >> n = word_of_int |
70185 | 3476 |
((bin_rest ^^ n) (bintrunc (LENGTH('a)) (numeral w)))" |
65328 | 3477 |
by (rule word_eqI) (auto simp: nth_shiftr nth_rest_power_bin nth_bintr word_size) |
37660 | 3478 |
|
46057 | 3479 |
lemma sshiftr_no [simp]: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3480 |
(* FIXME: neg_numeral *) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3481 |
"(numeral w::'a::len word) >>> n = word_of_int |
70185 | 3482 |
((bin_rest ^^ n) (sbintrunc (LENGTH('a) - 1) (numeral w)))" |
37660 | 3483 |
apply (rule word_eqI) |
3484 |
apply (auto simp: nth_sshiftr nth_rest_power_bin nth_sbintr word_size) |
|
70185 | 3485 |
apply (subgoal_tac "na + n = LENGTH('a) - Suc 0", simp, simp)+ |
37660 | 3486 |
done |
3487 |
||
45811 | 3488 |
lemma shiftr1_bl_of: |
70185 | 3489 |
"length bl \<le> LENGTH('a) \<Longrightarrow> |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3490 |
shiftr1 (of_bl bl::'a::len word) = of_bl (butlast bl)" |
65328 | 3491 |
by (clarsimp simp: shiftr1_def of_bl_def butlast_rest_bl2bin word_ubin.eq_norm trunc_bl2bin) |
37660 | 3492 |
|
45811 | 3493 |
lemma shiftr_bl_of: |
70185 | 3494 |
"length bl \<le> LENGTH('a) \<Longrightarrow> |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3495 |
(of_bl bl::'a::len word) >> n = of_bl (take (length bl - n) bl)" |
37660 | 3496 |
apply (unfold shiftr_def) |
3497 |
apply (induct n) |
|
3498 |
apply clarsimp |
|
3499 |
apply clarsimp |
|
3500 |
apply (subst shiftr1_bl_of) |
|
3501 |
apply simp |
|
3502 |
apply (simp add: butlast_take) |
|
3503 |
done |
|
3504 |
||
70185 | 3505 |
lemma shiftr_bl: "x >> n \<equiv> of_bl (take (LENGTH('a) - n) (to_bl x))" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3506 |
for x :: "'a::len word" |
45811 | 3507 |
using shiftr_bl_of [where 'a='a, of "to_bl x"] by simp |
3508 |
||
70185 | 3509 |
lemma msb_shift: "msb w \<longleftrightarrow> w >> (LENGTH('a) - 1) \<noteq> 0" |
65328 | 3510 |
for w :: "'a::len word" |
37660 | 3511 |
apply (unfold shiftr_bl word_msb_alt) |
3512 |
apply (simp add: word_size Suc_le_eq take_Suc) |
|
3513 |
apply (cases "hd (to_bl w)") |
|
65328 | 3514 |
apply (auto simp: word_1_bl of_bl_rep_False [where n=1 and bs="[]", simplified]) |
37660 | 3515 |
done |
3516 |
||
65328 | 3517 |
lemma zip_replicate: "n \<ge> length ys \<Longrightarrow> zip (replicate n x) ys = map (\<lambda>y. (x, y)) ys" |
3518 |
apply (induct ys arbitrary: n) |
|
3519 |
apply simp_all |
|
3520 |
apply (case_tac n) |
|
3521 |
apply simp_all |
|
57492
74bf65a1910a
Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
56979
diff
changeset
|
3522 |
done |
74bf65a1910a
Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
56979
diff
changeset
|
3523 |
|
37660 | 3524 |
lemma align_lem_or [rule_format] : |
65328 | 3525 |
"\<forall>x m. length x = n + m \<longrightarrow> length y = n + m \<longrightarrow> |
3526 |
drop m x = replicate n False \<longrightarrow> take m y = replicate m False \<longrightarrow> |
|
67399 | 3527 |
map2 (|) x y = take m x @ drop m y" |
65328 | 3528 |
apply (induct y) |
37660 | 3529 |
apply force |
3530 |
apply clarsimp |
|
65328 | 3531 |
apply (case_tac x) |
3532 |
apply force |
|
3533 |
apply (case_tac m) |
|
3534 |
apply auto |
|
59807 | 3535 |
apply (drule_tac t="length xs" for xs in sym) |
70193 | 3536 |
apply (auto simp: zip_replicate o_def) |
37660 | 3537 |
done |
3538 |
||
3539 |
lemma align_lem_and [rule_format] : |
|
65328 | 3540 |
"\<forall>x m. length x = n + m \<longrightarrow> length y = n + m \<longrightarrow> |
3541 |
drop m x = replicate n False \<longrightarrow> take m y = replicate m False \<longrightarrow> |
|
67399 | 3542 |
map2 (\<and>) x y = replicate (n + m) False" |
65328 | 3543 |
apply (induct y) |
37660 | 3544 |
apply force |
3545 |
apply clarsimp |
|
65328 | 3546 |
apply (case_tac x) |
3547 |
apply force |
|
3548 |
apply (case_tac m) |
|
3549 |
apply auto |
|
59807 | 3550 |
apply (drule_tac t="length xs" for xs in sym) |
70193 | 3551 |
apply (auto simp: zip_replicate o_def map_replicate_const) |
37660 | 3552 |
done |
3553 |
||
45811 | 3554 |
lemma aligned_bl_add_size [OF refl]: |
65328 | 3555 |
"size x - n = m \<Longrightarrow> n \<le> size x \<Longrightarrow> drop m (to_bl x) = replicate n False \<Longrightarrow> |
65268 | 3556 |
take m (to_bl y) = replicate m False \<Longrightarrow> |
37660 | 3557 |
to_bl (x + y) = take m (to_bl x) @ drop m (to_bl y)" |
3558 |
apply (subgoal_tac "x AND y = 0") |
|
3559 |
prefer 2 |
|
3560 |
apply (rule word_bl.Rep_eqD) |
|
45805 | 3561 |
apply (simp add: bl_word_and) |
37660 | 3562 |
apply (rule align_lem_and [THEN trans]) |
3563 |
apply (simp_all add: word_size)[5] |
|
3564 |
apply simp |
|
3565 |
apply (subst word_plus_and_or [symmetric]) |
|
3566 |
apply (simp add : bl_word_or) |
|
3567 |
apply (rule align_lem_or) |
|
3568 |
apply (simp_all add: word_size) |
|
3569 |
done |
|
3570 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
3571 |
|
61799 | 3572 |
subsubsection \<open>Mask\<close> |
37660 | 3573 |
|
71953 | 3574 |
lemma mask_eq: |
3575 |
\<open>mask n = 2 ^ n - 1\<close> |
|
3576 |
by (simp add: mask_def shiftl_word_eq push_bit_eq_mult) |
|
3577 |
||
3578 |
lemma mask_Suc_rec: |
|
3579 |
\<open>mask (Suc n) = 2 * mask n + 1\<close> |
|
3580 |
by (simp add: mask_eq) |
|
3581 |
||
3582 |
context |
|
3583 |
begin |
|
3584 |
||
3585 |
qualified lemma bit_mask_iff [simp]: |
|
3586 |
\<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < m\<close> |
|
3587 |
by (simp add: mask_eq bit_mask_iff exp_eq_zero_iff not_le) |
|
3588 |
||
3589 |
end |
|
3590 |
||
3591 |
lemma nth_mask [simp]: |
|
3592 |
\<open>(mask n :: 'a::len word) !! i \<longleftrightarrow> i < n \<and> i < size (mask n :: 'a word)\<close> |
|
3593 |
by (auto simp add: test_bit_word_eq word_size) |
|
37660 | 3594 |
|
3595 |
lemma mask_bl: "mask n = of_bl (replicate n True)" |
|
3596 |
by (auto simp add : test_bit_of_bl word_size intro: word_eqI) |
|
3597 |
||
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
58061
diff
changeset
|
3598 |
lemma mask_bin: "mask n = word_of_int (bintrunc n (- 1))" |
37660 | 3599 |
by (auto simp add: nth_bintr word_size intro: word_eqI) |
3600 |
||
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3601 |
lemma and_mask_bintr: "w AND mask n = word_of_int (bintrunc n (uint w))" |
37660 | 3602 |
apply (rule word_eqI) |
3603 |
apply (simp add: nth_bintr word_size word_ops_nth_size) |
|
3604 |
apply (auto simp add: test_bit_bin) |
|
3605 |
done |
|
3606 |
||
45811 | 3607 |
lemma and_mask_wi: "word_of_int i AND mask n = word_of_int (bintrunc n i)" |
46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46022
diff
changeset
|
3608 |
by (auto simp add: nth_bintr word_size word_ops_nth_size word_eq_iff) |
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46022
diff
changeset
|
3609 |
|
65328 | 3610 |
lemma and_mask_wi': |
3611 |
"word_of_int i AND mask n = (word_of_int (bintrunc (min LENGTH('a) n) i) :: 'a::len word)" |
|
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
3612 |
by (auto simp add: nth_bintr word_size word_ops_nth_size word_eq_iff) |
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
3613 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3614 |
lemma and_mask_no: "numeral i AND mask n = word_of_int (bintrunc n (numeral i))" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3615 |
unfolding word_numeral_alt by (rule and_mask_wi) |
37660 | 3616 |
|
3617 |
lemma bl_and_mask': |
|
65268 | 3618 |
"to_bl (w AND mask n :: 'a::len word) = |
70185 | 3619 |
replicate (LENGTH('a) - n) False @ |
3620 |
drop (LENGTH('a) - n) (to_bl w)" |
|
37660 | 3621 |
apply (rule nth_equalityI) |
3622 |
apply simp |
|
3623 |
apply (clarsimp simp add: to_bl_nth word_size) |
|
3624 |
apply (simp add: word_size word_ops_nth_size) |
|
3625 |
apply (auto simp add: word_size test_bit_bl nth_append nth_rev) |
|
3626 |
done |
|
3627 |
||
45811 | 3628 |
lemma and_mask_mod_2p: "w AND mask n = word_of_int (uint w mod 2 ^ n)" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3629 |
by (simp only: and_mask_bintr bintrunc_mod2p) |
37660 | 3630 |
|
3631 |
lemma and_mask_lt_2p: "uint (w AND mask n) < 2 ^ n" |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3632 |
apply (simp add: and_mask_bintr word_ubin.eq_norm) |
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3633 |
apply (simp add: bintrunc_mod2p) |
37660 | 3634 |
apply (rule xtr8) |
3635 |
prefer 2 |
|
3636 |
apply (rule pos_mod_bound) |
|
65328 | 3637 |
apply auto |
37660 | 3638 |
done |
3639 |
||
65363 | 3640 |
lemma eq_mod_iff: "0 < n \<Longrightarrow> b = b mod n \<longleftrightarrow> 0 \<le> b \<and> b < n" |
3641 |
for b n :: int |
|
45811 | 3642 |
by (simp add: int_mod_lem eq_sym_conv) |
37660 | 3643 |
|
65363 | 3644 |
lemma mask_eq_iff: "w AND mask n = w \<longleftrightarrow> uint w < 2 ^ n" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3645 |
apply (simp add: and_mask_bintr) |
37660 | 3646 |
apply (simp add: word_ubin.inverse_norm) |
3647 |
apply (simp add: eq_mod_iff bintrunc_mod2p min_def) |
|
3648 |
apply (fast intro!: lt2p_lem) |
|
3649 |
done |
|
3650 |
||
65328 | 3651 |
lemma and_mask_dvd: "2 ^ n dvd uint w \<longleftrightarrow> w AND mask n = 0" |
37660 | 3652 |
apply (simp add: dvd_eq_mod_eq_0 and_mask_mod_2p) |
65328 | 3653 |
apply (simp add: word_uint.norm_eq_iff [symmetric] word_of_int_homs del: word_of_int_0) |
37660 | 3654 |
apply (subst word_uint.norm_Rep [symmetric]) |
3655 |
apply (simp only: bintrunc_bintrunc_min bintrunc_mod2p [symmetric] min_def) |
|
3656 |
apply auto |
|
3657 |
done |
|
3658 |
||
65328 | 3659 |
lemma and_mask_dvd_nat: "2 ^ n dvd unat w \<longleftrightarrow> w AND mask n = 0" |
37660 | 3660 |
apply (unfold unat_def) |
3661 |
apply (rule trans [OF _ and_mask_dvd]) |
|
65268 | 3662 |
apply (unfold dvd_def) |
3663 |
apply auto |
|
65328 | 3664 |
apply (drule uint_ge_0 [THEN nat_int.Abs_inverse' [simplified], symmetric]) |
3665 |
apply simp |
|
3666 |
apply (simp add: nat_mult_distrib nat_power_eq) |
|
37660 | 3667 |
done |
3668 |
||
65328 | 3669 |
lemma word_2p_lem: "n < size w \<Longrightarrow> w < 2 ^ n = (uint w < 2 ^ n)" |
3670 |
for w :: "'a::len word" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3671 |
apply (unfold word_size word_less_alt word_numeral_alt) |
71942 | 3672 |
apply (auto simp add: word_of_int_power_hom word_uint.eq_norm |
65328 | 3673 |
simp del: word_of_int_numeral) |
37660 | 3674 |
done |
3675 |
||
65328 | 3676 |
lemma less_mask_eq: "x < 2 ^ n \<Longrightarrow> x AND mask n = x" |
3677 |
for x :: "'a::len word" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3678 |
apply (unfold word_less_alt word_numeral_alt) |
65328 | 3679 |
apply (clarsimp simp add: and_mask_mod_2p word_of_int_power_hom word_uint.eq_norm |
3680 |
simp del: word_of_int_numeral) |
|
37660 | 3681 |
apply (drule xtr8 [rotated]) |
65328 | 3682 |
apply (rule int_mod_le) |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3683 |
apply simp_all |
37660 | 3684 |
done |
3685 |
||
45604 | 3686 |
lemmas mask_eq_iff_w2p = trans [OF mask_eq_iff word_2p_lem [symmetric]] |
3687 |
||
3688 |
lemmas and_mask_less' = iffD2 [OF word_2p_lem and_mask_lt_2p, simplified word_size] |
|
37660 | 3689 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3690 |
lemma and_mask_less_size: "n < size x \<Longrightarrow> x AND mask n < 2^n" |
37660 | 3691 |
unfolding word_size by (erule and_mask_less') |
3692 |
||
65328 | 3693 |
lemma word_mod_2p_is_mask [OF refl]: "c = 2 ^ n \<Longrightarrow> c > 0 \<Longrightarrow> x mod c = x AND mask n" |
3694 |
for c x :: "'a::len word" |
|
3695 |
by (auto simp: word_mod_def uint_2p and_mask_mod_2p) |
|
37660 | 3696 |
|
3697 |
lemma mask_eqs: |
|
3698 |
"(a AND mask n) + b AND mask n = a + b AND mask n" |
|
3699 |
"a + (b AND mask n) AND mask n = a + b AND mask n" |
|
3700 |
"(a AND mask n) - b AND mask n = a - b AND mask n" |
|
3701 |
"a - (b AND mask n) AND mask n = a - b AND mask n" |
|
3702 |
"a * (b AND mask n) AND mask n = a * b AND mask n" |
|
3703 |
"(b AND mask n) * a AND mask n = b * a AND mask n" |
|
3704 |
"(a AND mask n) + (b AND mask n) AND mask n = a + b AND mask n" |
|
3705 |
"(a AND mask n) - (b AND mask n) AND mask n = a - b AND mask n" |
|
3706 |
"(a AND mask n) * (b AND mask n) AND mask n = a * b AND mask n" |
|
3707 |
"- (a AND mask n) AND mask n = - a AND mask n" |
|
3708 |
"word_succ (a AND mask n) AND mask n = word_succ a AND mask n" |
|
3709 |
"word_pred (a AND mask n) AND mask n = word_pred a AND mask n" |
|
3710 |
using word_of_int_Ex [where x=a] word_of_int_Ex [where x=b] |
|
65328 | 3711 |
by (auto simp: and_mask_wi' word_of_int_homs word.abs_eq_iff bintrunc_mod2p mod_simps) |
3712 |
||
3713 |
lemma mask_power_eq: "(x AND mask n) ^ k AND mask n = x ^ k AND mask n" |
|
37660 | 3714 |
using word_of_int_Ex [where x=x] |
65328 | 3715 |
by (auto simp: and_mask_wi' word_of_int_power_hom word.abs_eq_iff bintrunc_mod2p mod_simps) |
37660 | 3716 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3717 |
lemma mask_full [simp]: "mask LENGTH('a) = (- 1 :: 'a::len word)" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3718 |
by (simp add: mask_def word_size shiftl_zero_size) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3719 |
|
37660 | 3720 |
|
61799 | 3721 |
subsubsection \<open>Revcast\<close> |
37660 | 3722 |
|
3723 |
lemmas revcast_def' = revcast_def [simplified] |
|
3724 |
lemmas revcast_def'' = revcast_def' [simplified word_size] |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3725 |
lemmas revcast_no_def [simp] = revcast_def' [where w="numeral w", unfolded word_size] for w |
37660 | 3726 |
|
65268 | 3727 |
lemma to_bl_revcast: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3728 |
"to_bl (revcast w :: 'a::len word) = takefill False (LENGTH('a)) (to_bl w)" |
37660 | 3729 |
apply (unfold revcast_def' word_size) |
3730 |
apply (rule word_bl.Abs_inverse) |
|
3731 |
apply simp |
|
3732 |
done |
|
3733 |
||
65268 | 3734 |
lemma revcast_rev_ucast [OF refl refl refl]: |
3735 |
"cs = [rc, uc] \<Longrightarrow> rc = revcast (word_reverse w) \<Longrightarrow> uc = ucast w \<Longrightarrow> |
|
37660 | 3736 |
rc = word_reverse uc" |
3737 |
apply (unfold ucast_def revcast_def' Let_def word_reverse_def) |
|
65328 | 3738 |
apply (auto simp: to_bl_of_bin takefill_bintrunc) |
3739 |
apply (simp add: word_bl.Abs_inverse word_size) |
|
37660 | 3740 |
done |
3741 |
||
45811 | 3742 |
lemma revcast_ucast: "revcast w = word_reverse (ucast (word_reverse w))" |
3743 |
using revcast_rev_ucast [of "word_reverse w"] by simp |
|
3744 |
||
3745 |
lemma ucast_revcast: "ucast w = word_reverse (revcast (word_reverse w))" |
|
3746 |
by (fact revcast_rev_ucast [THEN word_rev_gal']) |
|
3747 |
||
3748 |
lemma ucast_rev_revcast: "ucast (word_reverse w) = word_reverse (revcast w)" |
|
3749 |
by (fact revcast_ucast [THEN word_rev_gal']) |
|
37660 | 3750 |
|
3751 |
||
65328 | 3752 |
text "linking revcast and cast via shift" |
37660 | 3753 |
|
3754 |
lemmas wsst_TYs = source_size target_size word_size |
|
3755 |
||
45811 | 3756 |
lemma revcast_down_uu [OF refl]: |
65328 | 3757 |
"rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = ucast (w >> n)" |
3758 |
for w :: "'a::len word" |
|
37660 | 3759 |
apply (simp add: revcast_def') |
3760 |
apply (rule word_bl.Rep_inverse') |
|
3761 |
apply (rule trans, rule ucast_down_drop) |
|
3762 |
prefer 2 |
|
3763 |
apply (rule trans, rule drop_shiftr) |
|
3764 |
apply (auto simp: takefill_alt wsst_TYs) |
|
3765 |
done |
|
3766 |
||
45811 | 3767 |
lemma revcast_down_us [OF refl]: |
65328 | 3768 |
"rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = ucast (w >>> n)" |
3769 |
for w :: "'a::len word" |
|
37660 | 3770 |
apply (simp add: revcast_def') |
3771 |
apply (rule word_bl.Rep_inverse') |
|
3772 |
apply (rule trans, rule ucast_down_drop) |
|
3773 |
prefer 2 |
|
3774 |
apply (rule trans, rule drop_sshiftr) |
|
3775 |
apply (auto simp: takefill_alt wsst_TYs) |
|
3776 |
done |
|
3777 |
||
45811 | 3778 |
lemma revcast_down_su [OF refl]: |
65328 | 3779 |
"rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = scast (w >> n)" |
3780 |
for w :: "'a::len word" |
|
37660 | 3781 |
apply (simp add: revcast_def') |
3782 |
apply (rule word_bl.Rep_inverse') |
|
3783 |
apply (rule trans, rule scast_down_drop) |
|
3784 |
prefer 2 |
|
3785 |
apply (rule trans, rule drop_shiftr) |
|
3786 |
apply (auto simp: takefill_alt wsst_TYs) |
|
3787 |
done |
|
3788 |
||
45811 | 3789 |
lemma revcast_down_ss [OF refl]: |
65328 | 3790 |
"rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = scast (w >>> n)" |
3791 |
for w :: "'a::len word" |
|
37660 | 3792 |
apply (simp add: revcast_def') |
3793 |
apply (rule word_bl.Rep_inverse') |
|
3794 |
apply (rule trans, rule scast_down_drop) |
|
3795 |
prefer 2 |
|
3796 |
apply (rule trans, rule drop_sshiftr) |
|
3797 |
apply (auto simp: takefill_alt wsst_TYs) |
|
3798 |
done |
|
3799 |
||
45811 | 3800 |
(* FIXME: should this also be [OF refl] ? *) |
65268 | 3801 |
lemma cast_down_rev: |
65328 | 3802 |
"uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> uc w = revcast (w << n)" |
3803 |
for w :: "'a::len word" |
|
37660 | 3804 |
apply (unfold shiftl_rev) |
3805 |
apply clarify |
|
3806 |
apply (simp add: revcast_rev_ucast) |
|
3807 |
apply (rule word_rev_gal') |
|
3808 |
apply (rule trans [OF _ revcast_rev_ucast]) |
|
3809 |
apply (rule revcast_down_uu [symmetric]) |
|
3810 |
apply (auto simp add: wsst_TYs) |
|
3811 |
done |
|
3812 |
||
45811 | 3813 |
lemma revcast_up [OF refl]: |
65268 | 3814 |
"rc = revcast \<Longrightarrow> source_size rc + n = target_size rc \<Longrightarrow> |
3815 |
rc w = (ucast w :: 'a::len word) << n" |
|
37660 | 3816 |
apply (simp add: revcast_def') |
3817 |
apply (rule word_bl.Rep_inverse') |
|
3818 |
apply (simp add: takefill_alt) |
|
3819 |
apply (rule bl_shiftl [THEN trans]) |
|
3820 |
apply (subst ucast_up_app) |
|
65328 | 3821 |
apply (auto simp add: wsst_TYs) |
37660 | 3822 |
done |
3823 |
||
65268 | 3824 |
lemmas rc1 = revcast_up [THEN |
37660 | 3825 |
revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]] |
65268 | 3826 |
lemmas rc2 = revcast_down_uu [THEN |
37660 | 3827 |
revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]] |
3828 |
||
3829 |
lemmas ucast_up = |
|
3830 |
rc1 [simplified rev_shiftr [symmetric] revcast_ucast [symmetric]] |
|
65268 | 3831 |
lemmas ucast_down = |
37660 | 3832 |
rc2 [simplified rev_shiftr revcast_ucast [symmetric]] |
3833 |
||
3834 |
||
61799 | 3835 |
subsubsection \<open>Slices\<close> |
37660 | 3836 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3837 |
lemma slice1_no_bin [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3838 |
"slice1 n (numeral w :: 'b word) = of_bl (takefill False n (bin_to_bl (LENGTH('b::len)) (numeral w)))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3839 |
by (simp add: slice1_def) (* TODO: neg_numeral *) |
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3840 |
|
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3841 |
lemma slice_no_bin [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3842 |
"slice n (numeral w :: 'b word) = of_bl (takefill False (LENGTH('b::len) - n) |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3843 |
(bin_to_bl (LENGTH('b::len)) (numeral w)))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3844 |
by (simp add: slice_def word_size) (* TODO: neg_numeral *) |
37660 | 3845 |
|
3846 |
lemma slice1_0 [simp] : "slice1 n 0 = 0" |
|
45805 | 3847 |
unfolding slice1_def by simp |
37660 | 3848 |
|
3849 |
lemma slice_0 [simp] : "slice n 0 = 0" |
|
3850 |
unfolding slice_def by auto |
|
3851 |
||
3852 |
lemma slice_take': "slice n w = of_bl (take (size w - n) (to_bl w))" |
|
3853 |
unfolding slice_def' slice1_def |
|
3854 |
by (simp add : takefill_alt word_size) |
|
3855 |
||
3856 |
lemmas slice_take = slice_take' [unfolded word_size] |
|
3857 |
||
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
3858 |
\<comment> \<open>shiftr to a word of the same size is just slice, |
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
3859 |
slice is just shiftr then ucast\<close> |
45604 | 3860 |
lemmas shiftr_slice = trans [OF shiftr_bl [THEN meta_eq_to_obj_eq] slice_take [symmetric]] |
37660 | 3861 |
|
3862 |
lemma slice_shiftr: "slice n w = ucast (w >> n)" |
|
3863 |
apply (unfold slice_take shiftr_bl) |
|
3864 |
apply (rule ucast_of_bl_up [symmetric]) |
|
3865 |
apply (simp add: word_size) |
|
3866 |
done |
|
3867 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3868 |
lemma nth_slice: "(slice n w :: 'a::len word) !! m = (w !! (m + n) \<and> m < LENGTH('a))" |
65336 | 3869 |
by (simp add: slice_shiftr nth_ucast nth_shiftr) |
37660 | 3870 |
|
65268 | 3871 |
lemma slice1_down_alt': |
3872 |
"sl = slice1 n w \<Longrightarrow> fs = size sl \<Longrightarrow> fs + k = n \<Longrightarrow> |
|
37660 | 3873 |
to_bl sl = takefill False fs (drop k (to_bl w))" |
65336 | 3874 |
by (auto simp: slice1_def word_size of_bl_def uint_bl |
3875 |
word_ubin.eq_norm bl_bin_bl_rep_drop drop_takefill) |
|
37660 | 3876 |
|
65268 | 3877 |
lemma slice1_up_alt': |
3878 |
"sl = slice1 n w \<Longrightarrow> fs = size sl \<Longrightarrow> fs = n + k \<Longrightarrow> |
|
37660 | 3879 |
to_bl sl = takefill False fs (replicate k False @ (to_bl w))" |
3880 |
apply (unfold slice1_def word_size of_bl_def uint_bl) |
|
65336 | 3881 |
apply (clarsimp simp: word_ubin.eq_norm bl_bin_bl_rep_drop takefill_append [symmetric]) |
70185 | 3882 |
apply (rule_tac f = "\<lambda>k. takefill False (LENGTH('a)) |
3883 |
(replicate k False @ bin_to_bl (LENGTH('b)) (uint w))" in arg_cong) |
|
37660 | 3884 |
apply arith |
3885 |
done |
|
65268 | 3886 |
|
37660 | 3887 |
lemmas sd1 = slice1_down_alt' [OF refl refl, unfolded word_size] |
3888 |
lemmas su1 = slice1_up_alt' [OF refl refl, unfolded word_size] |
|
3889 |
lemmas slice1_down_alt = le_add_diff_inverse [THEN sd1] |
|
65268 | 3890 |
lemmas slice1_up_alts = |
3891 |
le_add_diff_inverse [symmetric, THEN su1] |
|
37660 | 3892 |
le_add_diff_inverse2 [symmetric, THEN su1] |
3893 |
||
3894 |
lemma ucast_slice1: "ucast w = slice1 (size w) w" |
|
65336 | 3895 |
by (simp add: slice1_def ucast_bl takefill_same' word_size) |
37660 | 3896 |
|
3897 |
lemma ucast_slice: "ucast w = slice 0 w" |
|
65336 | 3898 |
by (simp add: slice_def ucast_slice1) |
37660 | 3899 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3900 |
lemma slice_id: "slice 0 t = t" |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3901 |
by (simp only: ucast_slice [symmetric] ucast_id) |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3902 |
|
65336 | 3903 |
lemma revcast_slice1 [OF refl]: "rc = revcast w \<Longrightarrow> slice1 (size rc) w = rc" |
3904 |
by (simp add: slice1_def revcast_def' word_size) |
|
37660 | 3905 |
|
65268 | 3906 |
lemma slice1_tf_tf': |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3907 |
"to_bl (slice1 n w :: 'a::len word) = |
70185 | 3908 |
rev (takefill False (LENGTH('a)) (rev (takefill False n (to_bl w))))" |
37660 | 3909 |
unfolding slice1_def by (rule word_rev_tf) |
3910 |
||
45604 | 3911 |
lemmas slice1_tf_tf = slice1_tf_tf' [THEN word_bl.Rep_inverse', symmetric] |
37660 | 3912 |
|
3913 |
lemma rev_slice1: |
|
70185 | 3914 |
"n + k = LENGTH('a) + LENGTH('b) \<Longrightarrow> |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3915 |
slice1 n (word_reverse w :: 'b::len word) = |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3916 |
word_reverse (slice1 k w :: 'a::len word)" |
37660 | 3917 |
apply (unfold word_reverse_def slice1_tf_tf) |
3918 |
apply (rule word_bl.Rep_inverse') |
|
3919 |
apply (rule rev_swap [THEN iffD1]) |
|
3920 |
apply (rule trans [symmetric]) |
|
65336 | 3921 |
apply (rule tf_rev) |
37660 | 3922 |
apply (simp add: word_bl.Abs_inverse) |
3923 |
apply (simp add: word_bl.Abs_inverse) |
|
3924 |
done |
|
3925 |
||
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3926 |
lemma rev_slice: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3927 |
"n + k + LENGTH('a::len) = LENGTH('b::len) \<Longrightarrow> |
65336 | 3928 |
slice n (word_reverse (w::'b word)) = word_reverse (slice k w :: 'a word)" |
37660 | 3929 |
apply (unfold slice_def word_size) |
3930 |
apply (rule rev_slice1) |
|
3931 |
apply arith |
|
3932 |
done |
|
3933 |
||
65268 | 3934 |
lemmas sym_notr = |
37660 | 3935 |
not_iff [THEN iffD2, THEN not_sym, THEN not_iff [THEN iffD1]] |
3936 |
||
61799 | 3937 |
\<comment> \<open>problem posed by TPHOLs referee: |
3938 |
criterion for overflow of addition of signed integers\<close> |
|
37660 | 3939 |
|
3940 |
lemma sofl_test: |
|
65336 | 3941 |
"(sint x + sint y = sint (x + y)) = |
3942 |
((((x + y) XOR x) AND ((x + y) XOR y)) >> (size x - 1) = 0)" |
|
3943 |
for x y :: "'a::len word" |
|
37660 | 3944 |
apply (unfold word_size) |
70185 | 3945 |
apply (cases "LENGTH('a)", simp) |
37660 | 3946 |
apply (subst msb_shift [THEN sym_notr]) |
3947 |
apply (simp add: word_ops_msb) |
|
3948 |
apply (simp add: word_msb_sint) |
|
3949 |
apply safe |
|
3950 |
apply simp_all |
|
3951 |
apply (unfold sint_word_ariths) |
|
3952 |
apply (unfold word_sbin.set_iff_norm [symmetric] sints_num) |
|
3953 |
apply safe |
|
65336 | 3954 |
apply (insert sint_range' [where x=x]) |
3955 |
apply (insert sint_range' [where x=y]) |
|
3956 |
defer |
|
3957 |
apply (simp (no_asm), arith) |
|
37660 | 3958 |
apply (simp (no_asm), arith) |
65336 | 3959 |
defer |
3960 |
defer |
|
37660 | 3961 |
apply (simp (no_asm), arith) |
3962 |
apply (simp (no_asm), arith) |
|
65336 | 3963 |
apply (rule notI [THEN notnotD], |
3964 |
drule leI not_le_imp_less, |
|
3965 |
drule sbintrunc_inc sbintrunc_dec, |
|
3966 |
simp)+ |
|
37660 | 3967 |
done |
3968 |
||
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3969 |
lemma shiftr_zero_size: "size x \<le> n \<Longrightarrow> x >> n = 0" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3970 |
for x :: "'a :: len word" |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3971 |
by (rule word_eqI) (auto simp add: nth_shiftr dest: test_bit_size) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3972 |
|
37660 | 3973 |
|
61799 | 3974 |
subsection \<open>Split and cat\<close> |
37660 | 3975 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3976 |
lemmas word_split_bin' = word_split_def |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3977 |
lemmas word_cat_bin' = word_cat_def |
37660 | 3978 |
|
3979 |
lemma word_rsplit_no: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3980 |
"(word_rsplit (numeral bin :: 'b::len word) :: 'a word list) = |
70185 | 3981 |
map word_of_int (bin_rsplit (LENGTH('a::len)) |
3982 |
(LENGTH('b), bintrunc (LENGTH('b)) (numeral bin)))" |
|
65336 | 3983 |
by (simp add: word_rsplit_def word_ubin.eq_norm) |
37660 | 3984 |
|
3985 |
lemmas word_rsplit_no_cl [simp] = word_rsplit_no |
|
3986 |
[unfolded bin_rsplitl_def bin_rsplit_l [symmetric]] |
|
3987 |
||
3988 |
lemma test_bit_cat: |
|
65336 | 3989 |
"wc = word_cat a b \<Longrightarrow> wc !! n = (n < size wc \<and> |
37660 | 3990 |
(if n < size b then b !! n else a !! (n - size b)))" |
65336 | 3991 |
apply (auto simp: word_cat_bin' test_bit_bin word_ubin.eq_norm nth_bintr bin_nth_cat word_size) |
37660 | 3992 |
apply (erule bin_nth_uint_imp) |
3993 |
done |
|
3994 |
||
3995 |
lemma word_cat_bl: "word_cat a b = of_bl (to_bl a @ to_bl b)" |
|
65336 | 3996 |
by (simp add: of_bl_def to_bl_def word_cat_bin' bl_to_bin_app_cat) |
37660 | 3997 |
|
3998 |
lemma of_bl_append: |
|
65268 | 3999 |
"(of_bl (xs @ ys) :: 'a::len word) = of_bl xs * 2^(length ys) + of_bl ys" |
65336 | 4000 |
apply (simp add: of_bl_def bl_to_bin_app_cat bin_cat_num) |
46009 | 4001 |
apply (simp add: word_of_int_power_hom [symmetric] word_of_int_hom_syms) |
37660 | 4002 |
done |
4003 |
||
65336 | 4004 |
lemma of_bl_False [simp]: "of_bl (False#xs) = of_bl xs" |
4005 |
by (rule word_eqI) (auto simp: test_bit_of_bl nth_append) |
|
4006 |
||
4007 |
lemma of_bl_True [simp]: "(of_bl (True # xs) :: 'a::len word) = 2^length xs + of_bl xs" |
|
4008 |
by (subst of_bl_append [where xs="[True]", simplified]) (simp add: word_1_bl) |
|
4009 |
||
4010 |
lemma of_bl_Cons: "of_bl (x#xs) = of_bool x * 2^length xs + of_bl xs" |
|
45805 | 4011 |
by (cases x) simp_all |
37660 | 4012 |
|
65336 | 4013 |
lemma split_uint_lem: "bin_split n (uint w) = (a, b) \<Longrightarrow> |
70185 | 4014 |
a = bintrunc (LENGTH('a) - n) a \<and> b = bintrunc (LENGTH('a)) b" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4015 |
for w :: "'a::len word" |
37660 | 4016 |
apply (frule word_ubin.norm_Rep [THEN ssubst]) |
4017 |
apply (drule bin_split_trunc1) |
|
4018 |
apply (drule sym [THEN trans]) |
|
65336 | 4019 |
apply assumption |
37660 | 4020 |
apply safe |
4021 |
done |
|
4022 |
||
65268 | 4023 |
lemma word_split_bl': |
4024 |
"std = size c - size b \<Longrightarrow> (word_split c = (a, b)) \<Longrightarrow> |
|
65336 | 4025 |
(a = of_bl (take std (to_bl c)) \<and> b = of_bl (drop std (to_bl c)))" |
37660 | 4026 |
apply (unfold word_split_bin') |
4027 |
apply safe |
|
4028 |
defer |
|
4029 |
apply (clarsimp split: prod.splits) |
|
71947 | 4030 |
apply (metis of_bl_drop' ucast_bl ucast_def word_size word_size_bl) |
57492
74bf65a1910a
Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
56979
diff
changeset
|
4031 |
apply hypsubst_thin |
37660 | 4032 |
apply (drule word_ubin.norm_Rep [THEN ssubst]) |
65336 | 4033 |
apply (simp add: of_bl_def bl2bin_drop word_size |
4034 |
word_ubin.norm_eq_iff [symmetric] min_def del: word_ubin.norm_Rep) |
|
37660 | 4035 |
apply (clarsimp split: prod.splits) |
70185 | 4036 |
apply (cases "LENGTH('a) \<ge> LENGTH('b)") |
71944 | 4037 |
apply (simp_all add: not_le) |
4038 |
defer |
|
4039 |
apply (simp add: drop_bit_eq_div lt2p_lem) |
|
37660 | 4040 |
apply (simp add : of_bl_def to_bl_def) |
71944 | 4041 |
apply (subst bin_to_bl_drop_bit [symmetric]) |
4042 |
apply (subst diff_add) |
|
71947 | 4043 |
apply (simp_all add: take_bit_drop_bit) |
37660 | 4044 |
done |
4045 |
||
65268 | 4046 |
lemma word_split_bl: "std = size c - size b \<Longrightarrow> |
65336 | 4047 |
(a = of_bl (take std (to_bl c)) \<and> b = of_bl (drop std (to_bl c))) \<longleftrightarrow> |
37660 | 4048 |
word_split c = (a, b)" |
4049 |
apply (rule iffI) |
|
4050 |
defer |
|
4051 |
apply (erule (1) word_split_bl') |
|
4052 |
apply (case_tac "word_split c") |
|
65336 | 4053 |
apply (auto simp add: word_size) |
37660 | 4054 |
apply (frule word_split_bl' [rotated]) |
65336 | 4055 |
apply (auto simp add: word_size) |
37660 | 4056 |
done |
4057 |
||
4058 |
lemma word_split_bl_eq: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4059 |
"(word_split c :: ('c::len word \<times> 'd::len word)) = |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4060 |
(of_bl (take (LENGTH('a::len) - LENGTH('d::len)) (to_bl c)), |
70185 | 4061 |
of_bl (drop (LENGTH('a) - LENGTH('d)) (to_bl c)))" |
65336 | 4062 |
for c :: "'a::len word" |
37660 | 4063 |
apply (rule word_split_bl [THEN iffD1]) |
65336 | 4064 |
apply (unfold word_size) |
4065 |
apply (rule refl conjI)+ |
|
37660 | 4066 |
done |
4067 |
||
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
4068 |
\<comment> \<open>keep quantifiers for use in simplification\<close> |
37660 | 4069 |
lemma test_bit_split': |
65336 | 4070 |
"word_split c = (a, b) \<longrightarrow> |
4071 |
(\<forall>n m. |
|
4072 |
b !! n = (n < size b \<and> c !! n) \<and> |
|
4073 |
a !! m = (m < size a \<and> c !! (m + size b)))" |
|
37660 | 4074 |
apply (unfold word_split_bin' test_bit_bin) |
4075 |
apply (clarify) |
|
4076 |
apply (clarsimp simp: word_ubin.eq_norm nth_bintr word_size split: prod.splits) |
|
71949 | 4077 |
apply (auto simp add: bit_take_bit_iff bit_drop_bit_eq ac_simps bin_nth_uint_imp) |
37660 | 4078 |
done |
4079 |
||
4080 |
lemma test_bit_split: |
|
4081 |
"word_split c = (a, b) \<Longrightarrow> |
|
65336 | 4082 |
(\<forall>n::nat. b !! n \<longleftrightarrow> n < size b \<and> c !! n) \<and> |
4083 |
(\<forall>m::nat. a !! m \<longleftrightarrow> m < size a \<and> c !! (m + size b))" |
|
37660 | 4084 |
by (simp add: test_bit_split') |
4085 |
||
65336 | 4086 |
lemma test_bit_split_eq: |
4087 |
"word_split c = (a, b) \<longleftrightarrow> |
|
4088 |
((\<forall>n::nat. b !! n = (n < size b \<and> c !! n)) \<and> |
|
4089 |
(\<forall>m::nat. a !! m = (m < size a \<and> c !! (m + size b))))" |
|
37660 | 4090 |
apply (rule_tac iffI) |
4091 |
apply (rule_tac conjI) |
|
4092 |
apply (erule test_bit_split [THEN conjunct1]) |
|
4093 |
apply (erule test_bit_split [THEN conjunct2]) |
|
4094 |
apply (case_tac "word_split c") |
|
4095 |
apply (frule test_bit_split) |
|
4096 |
apply (erule trans) |
|
65336 | 4097 |
apply (fastforce intro!: word_eqI simp add: word_size) |
37660 | 4098 |
done |
4099 |
||
65268 | 4100 |
\<comment> \<open>this odd result is analogous to \<open>ucast_id\<close>, |
61799 | 4101 |
result to the length given by the result type\<close> |
37660 | 4102 |
|
4103 |
lemma word_cat_id: "word_cat a b = b" |
|
65336 | 4104 |
by (simp add: word_cat_bin' word_ubin.inverse_norm) |
37660 | 4105 |
|
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
4106 |
\<comment> \<open>limited hom result\<close> |
37660 | 4107 |
lemma word_cat_hom: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4108 |
"LENGTH('a::len) \<le> LENGTH('b::len) + LENGTH('c::len) \<Longrightarrow> |
65336 | 4109 |
(word_cat (word_of_int w :: 'b word) (b :: 'c word) :: 'a word) = |
4110 |
word_of_int (bin_cat w (size b) (uint b))" |
|
4111 |
by (auto simp: word_cat_def word_size word_ubin.norm_eq_iff [symmetric] |
|
54863
82acc20ded73
prefer more canonical names for lemmas on min/max
haftmann
parents:
54854
diff
changeset
|
4112 |
word_ubin.eq_norm bintr_cat min.absorb1) |
65336 | 4113 |
|
4114 |
lemma word_cat_split_alt: "size w \<le> size u + size v \<Longrightarrow> word_split w = (u, v) \<Longrightarrow> word_cat u v = w" |
|
37660 | 4115 |
apply (rule word_eqI) |
4116 |
apply (drule test_bit_split) |
|
4117 |
apply (clarsimp simp add : test_bit_cat word_size) |
|
4118 |
apply safe |
|
4119 |
apply arith |
|
4120 |
done |
|
4121 |
||
45604 | 4122 |
lemmas word_cat_split_size = sym [THEN [2] word_cat_split_alt [symmetric]] |
37660 | 4123 |
|
4124 |
||
61799 | 4125 |
subsubsection \<open>Split and slice\<close> |
37660 | 4126 |
|
65336 | 4127 |
lemma split_slices: "word_split w = (u, v) \<Longrightarrow> u = slice (size v) w \<and> v = slice 0 w" |
37660 | 4128 |
apply (drule test_bit_split) |
4129 |
apply (rule conjI) |
|
4130 |
apply (rule word_eqI, clarsimp simp: nth_slice word_size)+ |
|
4131 |
done |
|
4132 |
||
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4133 |
lemma slice_cat1 [OF refl]: |
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4134 |
"wc = word_cat a b \<Longrightarrow> size wc >= size a + size b \<Longrightarrow> slice (size b) wc = a" |
37660 | 4135 |
apply safe |
4136 |
apply (rule word_eqI) |
|
4137 |
apply (simp add: nth_slice test_bit_cat word_size) |
|
4138 |
done |
|
4139 |
||
4140 |
lemmas slice_cat2 = trans [OF slice_id word_cat_id] |
|
4141 |
||
4142 |
lemma cat_slices: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4143 |
"a = slice n c \<Longrightarrow> b = slice 0 c \<Longrightarrow> n = size b \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4144 |
size a + size b >= size c \<Longrightarrow> word_cat a b = c" |
37660 | 4145 |
apply safe |
4146 |
apply (rule word_eqI) |
|
4147 |
apply (simp add: nth_slice test_bit_cat word_size) |
|
4148 |
apply safe |
|
4149 |
apply arith |
|
4150 |
done |
|
4151 |
||
4152 |
lemma word_split_cat_alt: |
|
65336 | 4153 |
"w = word_cat u v \<Longrightarrow> size u + size v \<le> size w \<Longrightarrow> word_split w = (u, v)" |
59807 | 4154 |
apply (case_tac "word_split w") |
37660 | 4155 |
apply (rule trans, assumption) |
4156 |
apply (drule test_bit_split) |
|
4157 |
apply safe |
|
4158 |
apply (rule word_eqI, clarsimp simp: test_bit_cat word_size)+ |
|
4159 |
done |
|
4160 |
||
4161 |
lemmas word_cat_bl_no_bin [simp] = |
|
65336 | 4162 |
word_cat_bl [where a="numeral a" and b="numeral b", unfolded to_bl_numeral] |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4163 |
for a b (* FIXME: negative numerals, 0 and 1 *) |
37660 | 4164 |
|
4165 |
lemmas word_split_bl_no_bin [simp] = |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4166 |
word_split_bl_eq [where c="numeral c", unfolded to_bl_numeral] for c |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4167 |
|
65336 | 4168 |
text \<open> |
4169 |
This odd result arises from the fact that the statement of the |
|
4170 |
result implies that the decoded words are of the same type, |
|
4171 |
and therefore of the same length, as the original word.\<close> |
|
37660 | 4172 |
|
4173 |
lemma word_rsplit_same: "word_rsplit w = [w]" |
|
65336 | 4174 |
by (simp add: word_rsplit_def bin_rsplit_all) |
4175 |
||
4176 |
lemma word_rsplit_empty_iff_size: "word_rsplit w = [] \<longleftrightarrow> size w = 0" |
|
4177 |
by (simp add: word_rsplit_def bin_rsplit_def word_size bin_rsplit_aux_simp_alt Let_def |
|
4178 |
split: prod.split) |
|
37660 | 4179 |
|
4180 |
lemma test_bit_rsplit: |
|
65363 | 4181 |
"sw = word_rsplit w \<Longrightarrow> m < size (hd sw) \<Longrightarrow> |
4182 |
k < length sw \<Longrightarrow> (rev sw ! k) !! m = w !! (k * size (hd sw) + m)" |
|
4183 |
for sw :: "'a::len word list" |
|
37660 | 4184 |
apply (unfold word_rsplit_def word_test_bit_def) |
4185 |
apply (rule trans) |
|
65336 | 4186 |
apply (rule_tac f = "\<lambda>x. bin_nth x m" in arg_cong) |
37660 | 4187 |
apply (rule nth_map [symmetric]) |
4188 |
apply simp |
|
4189 |
apply (rule bin_nth_rsplit) |
|
4190 |
apply simp_all |
|
4191 |
apply (simp add : word_size rev_map) |
|
4192 |
apply (rule trans) |
|
4193 |
defer |
|
4194 |
apply (rule map_ident [THEN fun_cong]) |
|
4195 |
apply (rule refl [THEN map_cong]) |
|
4196 |
apply (simp add : word_ubin.eq_norm) |
|
4197 |
apply (erule bin_rsplit_size_sign [OF len_gt_0 refl]) |
|
4198 |
done |
|
4199 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4200 |
lemma word_rcat_bl: "word_rcat wl = of_bl (concat (map to_bl wl))" |
65336 | 4201 |
by (auto simp: word_rcat_def to_bl_def' of_bl_def bin_rcat_bl) |
4202 |
||
4203 |
lemma size_rcat_lem': "size (concat (map to_bl wl)) = length wl * size (hd wl)" |
|
4204 |
by (induct wl) (auto simp: word_size) |
|
37660 | 4205 |
|
4206 |
lemmas size_rcat_lem = size_rcat_lem' [unfolded word_size] |
|
4207 |
||
45604 | 4208 |
lemmas td_gal_lt_len = len_gt_0 [THEN td_gal_lt] |
37660 | 4209 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4210 |
lemma nth_rcat_lem: |
70185 | 4211 |
"n < length (wl::'a word list) * LENGTH('a::len) \<Longrightarrow> |
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4212 |
rev (concat (map to_bl wl)) ! n = |
70185 | 4213 |
rev (to_bl (rev wl ! (n div LENGTH('a)))) ! (n mod LENGTH('a))" |
65336 | 4214 |
apply (induct wl) |
37660 | 4215 |
apply clarsimp |
4216 |
apply (clarsimp simp add : nth_append size_rcat_lem) |
|
65268 | 4217 |
apply (simp (no_asm_use) only: mult_Suc [symmetric] |
64243 | 4218 |
td_gal_lt_len less_Suc_eq_le minus_div_mult_eq_mod [symmetric]) |
37660 | 4219 |
apply clarsimp |
4220 |
done |
|
4221 |
||
4222 |
lemma test_bit_rcat: |
|
65363 | 4223 |
"sw = size (hd wl) \<Longrightarrow> rc = word_rcat wl \<Longrightarrow> rc !! n = |
65336 | 4224 |
(n < size rc \<and> n div sw < size wl \<and> (rev wl) ! (n div sw) !! (n mod sw))" |
65363 | 4225 |
for wl :: "'a::len word list" |
37660 | 4226 |
apply (unfold word_rcat_bl word_size) |
65336 | 4227 |
apply (clarsimp simp add: test_bit_of_bl size_rcat_lem word_size td_gal_lt_len) |
37660 | 4228 |
apply safe |
65336 | 4229 |
apply (auto simp: test_bit_bl word_size td_gal_lt_len [THEN iffD2, THEN nth_rcat_lem]) |
37660 | 4230 |
done |
4231 |
||
67399 | 4232 |
lemma foldl_eq_foldr: "foldl (+) x xs = foldr (+) (x # xs) 0" |
65336 | 4233 |
for x :: "'a::comm_monoid_add" |
4234 |
by (induct xs arbitrary: x) (auto simp: add.assoc) |
|
37660 | 4235 |
|
4236 |
lemmas test_bit_cong = arg_cong [where f = "test_bit", THEN fun_cong] |
|
4237 |
||
65268 | 4238 |
lemmas test_bit_rsplit_alt = |
4239 |
trans [OF nth_rev_alt [THEN test_bit_cong] |
|
37660 | 4240 |
test_bit_rsplit [OF refl asm_rl diff_Suc_less]] |
4241 |
||
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
4242 |
\<comment> \<open>lazy way of expressing that u and v, and su and sv, have same types\<close> |
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4243 |
lemma word_rsplit_len_indep [OF refl refl refl refl]: |
65268 | 4244 |
"[u,v] = p \<Longrightarrow> [su,sv] = q \<Longrightarrow> word_rsplit u = su \<Longrightarrow> |
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4245 |
word_rsplit v = sv \<Longrightarrow> length su = length sv" |
65336 | 4246 |
by (auto simp: word_rsplit_def bin_rsplit_len_indep) |
37660 | 4247 |
|
65268 | 4248 |
lemma length_word_rsplit_size: |
70185 | 4249 |
"n = LENGTH('a::len) \<Longrightarrow> |
65336 | 4250 |
length (word_rsplit w :: 'a word list) \<le> m \<longleftrightarrow> size w \<le> m * n" |
4251 |
by (auto simp: word_rsplit_def word_size bin_rsplit_len_le) |
|
37660 | 4252 |
|
65268 | 4253 |
lemmas length_word_rsplit_lt_size = |
37660 | 4254 |
length_word_rsplit_size [unfolded Not_eq_iff linorder_not_less [symmetric]] |
4255 |
||
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4256 |
lemma length_word_rsplit_exp_size: |
70185 | 4257 |
"n = LENGTH('a::len) \<Longrightarrow> |
37660 | 4258 |
length (word_rsplit w :: 'a word list) = (size w + n - 1) div n" |
65336 | 4259 |
by (auto simp: word_rsplit_def word_size bin_rsplit_len) |
37660 | 4260 |
|
65268 | 4261 |
lemma length_word_rsplit_even_size: |
70185 | 4262 |
"n = LENGTH('a::len) \<Longrightarrow> size w = m * n \<Longrightarrow> |
37660 | 4263 |
length (word_rsplit w :: 'a word list) = m" |
65336 | 4264 |
by (auto simp: length_word_rsplit_exp_size given_quot_alt) |
37660 | 4265 |
|
4266 |
lemmas length_word_rsplit_exp_size' = refl [THEN length_word_rsplit_exp_size] |
|
4267 |
||
67408 | 4268 |
\<comment> \<open>alternative proof of \<open>word_rcat_rsplit\<close>\<close> |
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66453
diff
changeset
|
4269 |
lemmas tdle = times_div_less_eq_dividend |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
4270 |
lemmas dtle = xtr4 [OF tdle mult.commute] |
37660 | 4271 |
|
4272 |
lemma word_rcat_rsplit: "word_rcat (word_rsplit w) = w" |
|
4273 |
apply (rule word_eqI) |
|
65336 | 4274 |
apply (clarsimp simp: test_bit_rcat word_size) |
37660 | 4275 |
apply (subst refl [THEN test_bit_rsplit]) |
65268 | 4276 |
apply (simp_all add: word_size |
37660 | 4277 |
refl [THEN length_word_rsplit_size [simplified not_less [symmetric], simplified]]) |
4278 |
apply safe |
|
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66453
diff
changeset
|
4279 |
apply (erule xtr7, rule dtle)+ |
37660 | 4280 |
done |
4281 |
||
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4282 |
lemma size_word_rsplit_rcat_size: |
70185 | 4283 |
"word_rcat ws = frcw \<Longrightarrow> size frcw = length ws * LENGTH('a) |
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4284 |
\<Longrightarrow> length (word_rsplit frcw::'a word list) = length ws" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4285 |
for ws :: "'a::len word list" and frcw :: "'b::len word" |
65336 | 4286 |
apply (clarsimp simp: word_size length_word_rsplit_exp_size') |
37660 | 4287 |
apply (fast intro: given_quot_alt) |
4288 |
done |
|
4289 |
||
4290 |
lemma msrevs: |
|
65336 | 4291 |
"0 < n \<Longrightarrow> (k * n + m) div n = m div n + k" |
4292 |
"(k * n + m) mod n = m mod n" |
|
4293 |
for n :: nat |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
4294 |
by (auto simp: add.commute) |
37660 | 4295 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4296 |
lemma word_rsplit_rcat_size [OF refl]: |
65336 | 4297 |
"word_rcat ws = frcw \<Longrightarrow> |
70185 | 4298 |
size frcw = length ws * LENGTH('a) \<Longrightarrow> word_rsplit frcw = ws" |
65336 | 4299 |
for ws :: "'a::len word list" |
37660 | 4300 |
apply (frule size_word_rsplit_rcat_size, assumption) |
4301 |
apply (clarsimp simp add : word_size) |
|
4302 |
apply (rule nth_equalityI, assumption) |
|
4303 |
apply clarsimp |
|
46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46022
diff
changeset
|
4304 |
apply (rule word_eqI [rule_format]) |
37660 | 4305 |
apply (rule trans) |
4306 |
apply (rule test_bit_rsplit_alt) |
|
4307 |
apply (clarsimp simp: word_size)+ |
|
4308 |
apply (rule trans) |
|
65336 | 4309 |
apply (rule test_bit_rcat [OF refl refl]) |
55818 | 4310 |
apply (simp add: word_size) |
37660 | 4311 |
apply (subst nth_rev) |
4312 |
apply arith |
|
41550 | 4313 |
apply (simp add: le0 [THEN [2] xtr7, THEN diff_Suc_less]) |
37660 | 4314 |
apply safe |
41550 | 4315 |
apply (simp add: diff_mult_distrib) |
37660 | 4316 |
apply (rule mpl_lem) |
65336 | 4317 |
apply (cases "size ws") |
4318 |
apply simp_all |
|
37660 | 4319 |
done |
4320 |
||
4321 |
||
61799 | 4322 |
subsection \<open>Rotation\<close> |
37660 | 4323 |
|
4324 |
lemmas rotater_0' [simp] = rotater_def [where n = "0", simplified] |
|
4325 |
||
4326 |
lemmas word_rot_defs = word_roti_def word_rotr_def word_rotl_def |
|
4327 |
||
65336 | 4328 |
lemma rotate_eq_mod: "m mod length xs = n mod length xs \<Longrightarrow> rotate m xs = rotate n xs" |
37660 | 4329 |
apply (rule box_equals) |
4330 |
defer |
|
4331 |
apply (rule rotate_conv_mod [symmetric])+ |
|
4332 |
apply simp |
|
4333 |
done |
|
4334 |
||
65268 | 4335 |
lemmas rotate_eqs = |
37660 | 4336 |
trans [OF rotate0 [THEN fun_cong] id_apply] |
65268 | 4337 |
rotate_rotate [symmetric] |
45604 | 4338 |
rotate_id |
65268 | 4339 |
rotate_conv_mod |
37660 | 4340 |
rotate_eq_mod |
4341 |
||
4342 |
||
61799 | 4343 |
subsubsection \<open>Rotation of list to right\<close> |
37660 | 4344 |
|
4345 |
lemma rotate1_rl': "rotater1 (l @ [a]) = a # l" |
|
65336 | 4346 |
by (cases l) (auto simp: rotater1_def) |
37660 | 4347 |
|
4348 |
lemma rotate1_rl [simp] : "rotater1 (rotate1 l) = l" |
|
4349 |
apply (unfold rotater1_def) |
|
4350 |
apply (cases "l") |
|
65336 | 4351 |
apply (case_tac [2] "list") |
4352 |
apply auto |
|
37660 | 4353 |
done |
4354 |
||
4355 |
lemma rotate1_lr [simp] : "rotate1 (rotater1 l) = l" |
|
65336 | 4356 |
by (cases l) (auto simp: rotater1_def) |
37660 | 4357 |
|
4358 |
lemma rotater1_rev': "rotater1 (rev xs) = rev (rotate1 xs)" |
|
65336 | 4359 |
by (cases "xs") (simp add: rotater1_def, simp add: rotate1_rl') |
65268 | 4360 |
|
37660 | 4361 |
lemma rotater_rev': "rotater n (rev xs) = rev (rotate n xs)" |
65336 | 4362 |
by (induct n) (auto simp: rotater_def intro: rotater1_rev') |
37660 | 4363 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4364 |
lemma rotater_rev: "rotater n ys = rev (rotate n (rev ys))" |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4365 |
using rotater_rev' [where xs = "rev ys"] by simp |
37660 | 4366 |
|
65268 | 4367 |
lemma rotater_drop_take: |
4368 |
"rotater n xs = |
|
65336 | 4369 |
drop (length xs - n mod length xs) xs @ |
4370 |
take (length xs - n mod length xs) xs" |
|
4371 |
by (auto simp: rotater_rev rotate_drop_take rev_take rev_drop) |
|
4372 |
||
4373 |
lemma rotater_Suc [simp]: "rotater (Suc n) xs = rotater1 (rotater n xs)" |
|
37660 | 4374 |
unfolding rotater_def by auto |
4375 |
||
4376 |
lemma rotate_inv_plus [rule_format] : |
|
65336 | 4377 |
"\<forall>k. k = m + n \<longrightarrow> rotater k (rotate n xs) = rotater m xs \<and> |
4378 |
rotate k (rotater n xs) = rotate m xs \<and> |
|
4379 |
rotater n (rotate k xs) = rotate m xs \<and> |
|
37660 | 4380 |
rotate n (rotater k xs) = rotater m xs" |
65336 | 4381 |
by (induct n) (auto simp: rotater_def rotate_def intro: funpow_swap1 [THEN trans]) |
65268 | 4382 |
|
37660 | 4383 |
lemmas rotate_inv_rel = le_add_diff_inverse2 [symmetric, THEN rotate_inv_plus] |
4384 |
||
4385 |
lemmas rotate_inv_eq = order_refl [THEN rotate_inv_rel, simplified] |
|
4386 |
||
45604 | 4387 |
lemmas rotate_lr [simp] = rotate_inv_eq [THEN conjunct1] |
4388 |
lemmas rotate_rl [simp] = rotate_inv_eq [THEN conjunct2, THEN conjunct1] |
|
37660 | 4389 |
|
65336 | 4390 |
lemma rotate_gal: "rotater n xs = ys \<longleftrightarrow> rotate n ys = xs" |
37660 | 4391 |
by auto |
4392 |
||
65336 | 4393 |
lemma rotate_gal': "ys = rotater n xs \<longleftrightarrow> xs = rotate n ys" |
37660 | 4394 |
by auto |
4395 |
||
65336 | 4396 |
lemma length_rotater [simp]: "length (rotater n xs) = length xs" |
37660 | 4397 |
by (simp add : rotater_rev) |
4398 |
||
65336 | 4399 |
lemma restrict_to_left: "x = y \<Longrightarrow> x = z \<longleftrightarrow> y = z" |
4400 |
by simp |
|
38527 | 4401 |
|
65268 | 4402 |
lemmas rrs0 = rotate_eqs [THEN restrict_to_left, |
45604 | 4403 |
simplified rotate_gal [symmetric] rotate_gal' [symmetric]] |
37660 | 4404 |
lemmas rrs1 = rrs0 [THEN refl [THEN rev_iffD1]] |
45604 | 4405 |
lemmas rotater_eqs = rrs1 [simplified length_rotater] |
37660 | 4406 |
lemmas rotater_0 = rotater_eqs (1) |
4407 |
lemmas rotater_add = rotater_eqs (2) |
|
4408 |
||
4409 |
||
61799 | 4410 |
subsubsection \<open>map, map2, commuting with rotate(r)\<close> |
37660 | 4411 |
|
65336 | 4412 |
lemma butlast_map: "xs \<noteq> [] \<Longrightarrow> butlast (map f xs) = map f (butlast xs)" |
37660 | 4413 |
by (induct xs) auto |
4414 |
||
65268 | 4415 |
lemma rotater1_map: "rotater1 (map f xs) = map f (rotater1 xs)" |
65336 | 4416 |
by (cases xs) (auto simp: rotater1_def last_map butlast_map) |
4417 |
||
4418 |
lemma rotater_map: "rotater n (map f xs) = map f (rotater n xs)" |
|
4419 |
by (induct n) (auto simp: rotater_def rotater1_map) |
|
37660 | 4420 |
|
4421 |
lemma but_last_zip [rule_format] : |
|
65336 | 4422 |
"\<forall>ys. length xs = length ys \<longrightarrow> xs \<noteq> [] \<longrightarrow> |
4423 |
last (zip xs ys) = (last xs, last ys) \<and> |
|
65268 | 4424 |
butlast (zip xs ys) = zip (butlast xs) (butlast ys)" |
65336 | 4425 |
apply (induct xs) |
4426 |
apply auto |
|
37660 | 4427 |
apply ((case_tac ys, auto simp: neq_Nil_conv)[1])+ |
4428 |
done |
|
4429 |
||
4430 |
lemma but_last_map2 [rule_format] : |
|
65336 | 4431 |
"\<forall>ys. length xs = length ys \<longrightarrow> xs \<noteq> [] \<longrightarrow> |
4432 |
last (map2 f xs ys) = f (last xs) (last ys) \<and> |
|
65268 | 4433 |
butlast (map2 f xs ys) = map2 f (butlast xs) (butlast ys)" |
65336 | 4434 |
apply (induct xs) |
4435 |
apply auto |
|
37660 | 4436 |
apply ((case_tac ys, auto simp: neq_Nil_conv)[1])+ |
4437 |
done |
|
4438 |
||
4439 |
lemma rotater1_zip: |
|
65268 | 4440 |
"length xs = length ys \<Longrightarrow> |
4441 |
rotater1 (zip xs ys) = zip (rotater1 xs) (rotater1 ys)" |
|
37660 | 4442 |
apply (unfold rotater1_def) |
65336 | 4443 |
apply (cases xs) |
37660 | 4444 |
apply auto |
4445 |
apply ((case_tac ys, auto simp: neq_Nil_conv but_last_zip)[1])+ |
|
4446 |
done |
|
4447 |
||
4448 |
lemma rotater1_map2: |
|
65268 | 4449 |
"length xs = length ys \<Longrightarrow> |
4450 |
rotater1 (map2 f xs ys) = map2 f (rotater1 xs) (rotater1 ys)" |
|
70193 | 4451 |
by (simp add: rotater1_map rotater1_zip) |
37660 | 4452 |
|
65268 | 4453 |
lemmas lrth = |
4454 |
box_equals [OF asm_rl length_rotater [symmetric] |
|
4455 |
length_rotater [symmetric], |
|
37660 | 4456 |
THEN rotater1_map2] |
4457 |
||
65268 | 4458 |
lemma rotater_map2: |
4459 |
"length xs = length ys \<Longrightarrow> |
|
4460 |
rotater n (map2 f xs ys) = map2 f (rotater n xs) (rotater n ys)" |
|
37660 | 4461 |
by (induct n) (auto intro!: lrth) |
4462 |
||
4463 |
lemma rotate1_map2: |
|
65268 | 4464 |
"length xs = length ys \<Longrightarrow> |
4465 |
rotate1 (map2 f xs ys) = map2 f (rotate1 xs) (rotate1 ys)" |
|
70193 | 4466 |
by (cases xs; cases ys) auto |
37660 | 4467 |
|
65268 | 4468 |
lemmas lth = box_equals [OF asm_rl length_rotate [symmetric] |
37660 | 4469 |
length_rotate [symmetric], THEN rotate1_map2] |
4470 |
||
65268 | 4471 |
lemma rotate_map2: |
4472 |
"length xs = length ys \<Longrightarrow> |
|
4473 |
rotate n (map2 f xs ys) = map2 f (rotate n xs) (rotate n ys)" |
|
37660 | 4474 |
by (induct n) (auto intro!: lth) |
4475 |
||
4476 |
||
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
4477 |
\<comment> \<open>corresponding equalities for word rotation\<close> |
37660 | 4478 |
|
65336 | 4479 |
lemma to_bl_rotl: "to_bl (word_rotl n w) = rotate n (to_bl w)" |
37660 | 4480 |
by (simp add: word_bl.Abs_inverse' word_rotl_def) |
4481 |
||
4482 |
lemmas blrs0 = rotate_eqs [THEN to_bl_rotl [THEN trans]] |
|
4483 |
||
4484 |
lemmas word_rotl_eqs = |
|
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
4485 |
blrs0 [simplified word_bl_Rep' word_bl.Rep_inject to_bl_rotl [symmetric]] |
37660 | 4486 |
|
65336 | 4487 |
lemma to_bl_rotr: "to_bl (word_rotr n w) = rotater n (to_bl w)" |
37660 | 4488 |
by (simp add: word_bl.Abs_inverse' word_rotr_def) |
4489 |
||
4490 |
lemmas brrs0 = rotater_eqs [THEN to_bl_rotr [THEN trans]] |
|
4491 |
||
4492 |
lemmas word_rotr_eqs = |
|
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
4493 |
brrs0 [simplified word_bl_Rep' word_bl.Rep_inject to_bl_rotr [symmetric]] |
37660 | 4494 |
|
4495 |
declare word_rotr_eqs (1) [simp] |
|
4496 |
declare word_rotl_eqs (1) [simp] |
|
4497 |
||
65336 | 4498 |
lemma word_rot_rl [simp]: "word_rotl k (word_rotr k v) = v" |
4499 |
and word_rot_lr [simp]: "word_rotr k (word_rotl k v) = v" |
|
37660 | 4500 |
by (auto simp add: to_bl_rotr to_bl_rotl word_bl.Rep_inject [symmetric]) |
4501 |
||
65336 | 4502 |
lemma word_rot_gal: "word_rotr n v = w \<longleftrightarrow> word_rotl n w = v" |
4503 |
and word_rot_gal': "w = word_rotr n v \<longleftrightarrow> v = word_rotl n w" |
|
4504 |
by (auto simp: to_bl_rotr to_bl_rotl word_bl.Rep_inject [symmetric] dest: sym) |
|
4505 |
||
4506 |
lemma word_rotr_rev: "word_rotr n w = word_reverse (word_rotl n (word_reverse w))" |
|
4507 |
by (simp only: word_bl.Rep_inject [symmetric] to_bl_word_rev to_bl_rotr to_bl_rotl rotater_rev) |
|
65268 | 4508 |
|
37660 | 4509 |
lemma word_roti_0 [simp]: "word_roti 0 w = w" |
65336 | 4510 |
by (auto simp: word_rot_defs) |
37660 | 4511 |
|
4512 |
lemmas abl_cong = arg_cong [where f = "of_bl"] |
|
4513 |
||
65336 | 4514 |
lemma word_roti_add: "word_roti (m + n) w = word_roti m (word_roti n w)" |
37660 | 4515 |
proof - |
65336 | 4516 |
have rotater_eq_lem: "\<And>m n xs. m = n \<Longrightarrow> rotater m xs = rotater n xs" |
37660 | 4517 |
by auto |
4518 |
||
65336 | 4519 |
have rotate_eq_lem: "\<And>m n xs. m = n \<Longrightarrow> rotate m xs = rotate n xs" |
37660 | 4520 |
by auto |
4521 |
||
65268 | 4522 |
note rpts [symmetric] = |
37660 | 4523 |
rotate_inv_plus [THEN conjunct1] |
4524 |
rotate_inv_plus [THEN conjunct2, THEN conjunct1] |
|
4525 |
rotate_inv_plus [THEN conjunct2, THEN conjunct2, THEN conjunct1] |
|
4526 |
rotate_inv_plus [THEN conjunct2, THEN conjunct2, THEN conjunct2] |
|
4527 |
||
4528 |
note rrp = trans [symmetric, OF rotate_rotate rotate_eq_lem] |
|
4529 |
note rrrp = trans [symmetric, OF rotater_add [symmetric] rotater_eq_lem] |
|
4530 |
||
4531 |
show ?thesis |
|
65336 | 4532 |
apply (unfold word_rot_defs) |
4533 |
apply (simp only: split: if_split) |
|
4534 |
apply (safe intro!: abl_cong) |
|
4535 |
apply (simp_all only: to_bl_rotl [THEN word_bl.Rep_inverse'] |
|
4536 |
to_bl_rotl |
|
4537 |
to_bl_rotr [THEN word_bl.Rep_inverse'] |
|
4538 |
to_bl_rotr) |
|
4539 |
apply (rule rrp rrrp rpts, |
|
4540 |
simp add: nat_add_distrib [symmetric] |
|
4541 |
nat_diff_distrib [symmetric])+ |
|
4542 |
done |
|
37660 | 4543 |
qed |
65268 | 4544 |
|
67118 | 4545 |
lemma word_roti_conv_mod': |
4546 |
"word_roti n w = word_roti (n mod int (size w)) w" |
|
4547 |
proof (cases "size w = 0") |
|
4548 |
case True |
|
4549 |
then show ?thesis |
|
4550 |
by simp |
|
4551 |
next |
|
4552 |
case False |
|
4553 |
then have [simp]: "l mod int (size w) \<ge> 0" for l |
|
4554 |
by simp |
|
4555 |
then have *: "word_roti (n mod int (size w)) w = word_rotr (nat (n mod int (size w))) w" |
|
4556 |
by (simp add: word_roti_def) |
|
4557 |
show ?thesis |
|
4558 |
proof (cases "n \<ge> 0") |
|
4559 |
case True |
|
4560 |
then show ?thesis |
|
4561 |
apply (auto simp add: not_le *) |
|
4562 |
apply (auto simp add: word_rot_defs) |
|
4563 |
apply (safe intro!: abl_cong) |
|
4564 |
apply (rule rotater_eqs) |
|
4565 |
apply (simp add: word_size nat_mod_distrib) |
|
4566 |
done |
|
4567 |
next |
|
4568 |
case False |
|
4569 |
moreover define k where "k = - n" |
|
4570 |
ultimately have n: "n = - k" |
|
4571 |
by simp_all |
|
4572 |
from False show ?thesis |
|
4573 |
apply (auto simp add: not_le *) |
|
4574 |
apply (auto simp add: word_rot_defs) |
|
4575 |
apply (simp add: n) |
|
4576 |
apply (safe intro!: abl_cong) |
|
4577 |
apply (simp add: rotater_add [symmetric] rotate_gal [symmetric]) |
|
4578 |
apply (rule rotater_eqs) |
|
4579 |
apply (simp add: word_size [symmetric, of w]) |
|
4580 |
apply (rule of_nat_eq_0_iff [THEN iffD1]) |
|
4581 |
apply (auto simp add: nat_add_distrib [symmetric] mod_eq_0_iff_dvd) |
|
4582 |
using dvd_nat_abs_iff [of "size w" "- k mod int (size w) + k"] |
|
4583 |
apply (simp add: algebra_simps) |
|
4584 |
apply (simp add: word_size) |
|
71942 | 4585 |
apply (metis dvd_eq_mod_eq_0 eq_neg_iff_add_eq_0 k_def mod_0 mod_add_right_eq n) |
67118 | 4586 |
done |
4587 |
qed |
|
4588 |
qed |
|
37660 | 4589 |
|
4590 |
lemmas word_roti_conv_mod = word_roti_conv_mod' [unfolded word_size] |
|
4591 |
||
4592 |
||
61799 | 4593 |
subsubsection \<open>"Word rotation commutes with bit-wise operations\<close> |
37660 | 4594 |
|
67408 | 4595 |
\<comment> \<open>using locale to not pollute lemma namespace\<close> |
65268 | 4596 |
locale word_rotate |
37660 | 4597 |
begin |
4598 |
||
4599 |
lemmas word_rot_defs' = to_bl_rotl to_bl_rotr |
|
4600 |
||
4601 |
lemmas blwl_syms [symmetric] = bl_word_not bl_word_and bl_word_or bl_word_xor |
|
4602 |
||
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
4603 |
lemmas lbl_lbl = trans [OF word_bl_Rep' word_bl_Rep' [symmetric]] |
37660 | 4604 |
|
4605 |
lemmas ths_map2 [OF lbl_lbl] = rotate_map2 rotater_map2 |
|
4606 |
||
45604 | 4607 |
lemmas ths_map [where xs = "to_bl v"] = rotate_map rotater_map for v |
37660 | 4608 |
|
4609 |
lemmas th1s [simplified word_rot_defs' [symmetric]] = ths_map2 ths_map |
|
4610 |
||
4611 |
lemma word_rot_logs: |
|
71149 | 4612 |
"word_rotl n (NOT v) = NOT (word_rotl n v)" |
4613 |
"word_rotr n (NOT v) = NOT (word_rotr n v)" |
|
37660 | 4614 |
"word_rotl n (x AND y) = word_rotl n x AND word_rotl n y" |
4615 |
"word_rotr n (x AND y) = word_rotr n x AND word_rotr n y" |
|
4616 |
"word_rotl n (x OR y) = word_rotl n x OR word_rotl n y" |
|
4617 |
"word_rotr n (x OR y) = word_rotr n x OR word_rotr n y" |
|
4618 |
"word_rotl n (x XOR y) = word_rotl n x XOR word_rotl n y" |
|
65268 | 4619 |
"word_rotr n (x XOR y) = word_rotr n x XOR word_rotr n y" |
37660 | 4620 |
by (rule word_bl.Rep_eqD, |
4621 |
rule word_rot_defs' [THEN trans], |
|
4622 |
simp only: blwl_syms [symmetric], |
|
65268 | 4623 |
rule th1s [THEN trans], |
37660 | 4624 |
rule refl)+ |
4625 |
end |
|
4626 |
||
4627 |
lemmas word_rot_logs = word_rotate.word_rot_logs |
|
4628 |
||
4629 |
lemmas bl_word_rotl_dt = trans [OF to_bl_rotl rotate_drop_take, |
|
45604 | 4630 |
simplified word_bl_Rep'] |
37660 | 4631 |
|
4632 |
lemmas bl_word_rotr_dt = trans [OF to_bl_rotr rotater_drop_take, |
|
45604 | 4633 |
simplified word_bl_Rep'] |
37660 | 4634 |
|
65268 | 4635 |
lemma bl_word_roti_dt': |
4636 |
"n = nat ((- i) mod int (size (w :: 'a::len word))) \<Longrightarrow> |
|
37660 | 4637 |
to_bl (word_roti i w) = drop n (to_bl w) @ take n (to_bl w)" |
4638 |
apply (unfold word_roti_def) |
|
4639 |
apply (simp add: bl_word_rotl_dt bl_word_rotr_dt word_size) |
|
4640 |
apply safe |
|
4641 |
apply (simp add: zmod_zminus1_eq_if) |
|
4642 |
apply safe |
|
4643 |
apply (simp add: nat_mult_distrib) |
|
65268 | 4644 |
apply (simp add: nat_diff_distrib [OF pos_mod_sign pos_mod_conj |
37660 | 4645 |
[THEN conjunct2, THEN order_less_imp_le]] |
4646 |
nat_mod_distrib) |
|
4647 |
apply (simp add: nat_mod_distrib) |
|
4648 |
done |
|
4649 |
||
4650 |
lemmas bl_word_roti_dt = bl_word_roti_dt' [unfolded word_size] |
|
4651 |
||
65268 | 4652 |
lemmas word_rotl_dt = bl_word_rotl_dt [THEN word_bl.Rep_inverse' [symmetric]] |
45604 | 4653 |
lemmas word_rotr_dt = bl_word_rotr_dt [THEN word_bl.Rep_inverse' [symmetric]] |
4654 |
lemmas word_roti_dt = bl_word_roti_dt [THEN word_bl.Rep_inverse' [symmetric]] |
|
37660 | 4655 |
|
65336 | 4656 |
lemma word_rotx_0 [simp] : "word_rotr i 0 = 0 \<and> word_rotl i 0 = 0" |
4657 |
by (simp add: word_rotr_dt word_rotl_dt replicate_add [symmetric]) |
|
37660 | 4658 |
|
4659 |
lemma word_roti_0' [simp] : "word_roti n 0 = 0" |
|
65336 | 4660 |
by (auto simp: word_roti_def) |
37660 | 4661 |
|
65268 | 4662 |
lemmas word_rotr_dt_no_bin' [simp] = |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4663 |
word_rotr_dt [where w="numeral w", unfolded to_bl_numeral] for w |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4664 |
(* FIXME: negative numerals, 0 and 1 *) |
37660 | 4665 |
|
65268 | 4666 |
lemmas word_rotl_dt_no_bin' [simp] = |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4667 |
word_rotl_dt [where w="numeral w", unfolded to_bl_numeral] for w |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4668 |
(* FIXME: negative numerals, 0 and 1 *) |
37660 | 4669 |
|
4670 |
declare word_roti_def [simp] |
|
4671 |
||
4672 |
||
61799 | 4673 |
subsection \<open>Maximum machine word\<close> |
37660 | 4674 |
|
4675 |
lemma word_int_cases: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4676 |
fixes x :: "'a::len word" |
70185 | 4677 |
obtains n where "x = word_of_int n" and "0 \<le> n" and "n < 2^LENGTH('a)" |
37660 | 4678 |
by (cases x rule: word_uint.Abs_cases) (simp add: uints_num) |
4679 |
||
4680 |
lemma word_nat_cases [cases type: word]: |
|
65336 | 4681 |
fixes x :: "'a::len word" |
70185 | 4682 |
obtains n where "x = of_nat n" and "n < 2^LENGTH('a)" |
37660 | 4683 |
by (cases x rule: word_unat.Abs_cases) (simp add: unats_def) |
4684 |
||
71946 | 4685 |
lemma max_word_max [intro!]: "n \<le> max_word" |
4686 |
by (fact word_n1_ge) |
|
65268 | 4687 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4688 |
lemma word_of_int_2p_len: "word_of_int (2 ^ LENGTH('a)) = (0::'a::len word)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4689 |
by (subst word_uint.Abs_norm [symmetric]) simp |
37660 | 4690 |
|
70185 | 4691 |
lemma word_pow_0: "(2::'a::len word) ^ LENGTH('a) = 0" |
37660 | 4692 |
proof - |
70185 | 4693 |
have "word_of_int (2 ^ LENGTH('a)) = (0::'a word)" |
37660 | 4694 |
by (rule word_of_int_2p_len) |
65336 | 4695 |
then show ?thesis by (simp add: word_of_int_2p) |
37660 | 4696 |
qed |
4697 |
||
4698 |
lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word" |
|
71946 | 4699 |
by (simp add: eq_neg_iff_add_eq_0) |
4700 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4701 |
lemma max_word_bl: "to_bl (max_word::'a::len word) = replicate LENGTH('a) True" |
71946 | 4702 |
by (fact to_bl_n1) |
4703 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4704 |
lemma max_test_bit: "(max_word::'a::len word) !! n \<longleftrightarrow> n < LENGTH('a)" |
71946 | 4705 |
by (fact nth_minus1) |
4706 |
||
4707 |
lemma word_and_max: "x AND max_word = x" |
|
4708 |
by (fact word_log_esimps) |
|
4709 |
||
4710 |
lemma word_or_max: "x OR max_word = max_word" |
|
4711 |
by (fact word_log_esimps) |
|
37660 | 4712 |
|
65336 | 4713 |
lemma word_ao_dist2: "x AND (y OR z) = x AND y OR x AND z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4714 |
for x y z :: "'a::len word" |
37660 | 4715 |
by (rule word_eqI) (auto simp add: word_ops_nth_size word_size) |
4716 |
||
65336 | 4717 |
lemma word_oa_dist2: "x OR y AND z = (x OR y) AND (x OR z)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4718 |
for x y z :: "'a::len word" |
37660 | 4719 |
by (rule word_eqI) (auto simp add: word_ops_nth_size word_size) |
4720 |
||
65336 | 4721 |
lemma word_and_not [simp]: "x AND NOT x = 0" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4722 |
for x :: "'a::len word" |
37660 | 4723 |
by (rule word_eqI) (auto simp add: word_ops_nth_size word_size) |
4724 |
||
65336 | 4725 |
lemma word_or_not [simp]: "x OR NOT x = max_word" |
37660 | 4726 |
by (rule word_eqI) (auto simp add: word_ops_nth_size word_size) |
4727 |
||
70187
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4728 |
global_interpretation word_bool_alg: boolean_algebra |
71826 | 4729 |
where conj = "(AND)" and disj = "(OR)" and compl = NOT |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4730 |
and zero = 0 and one = \<open>- 1 :: 'a::len word\<close> |
70187
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4731 |
rewrites "word_bool_alg.xor = (XOR)" |
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4732 |
proof - |
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4733 |
interpret boolean_algebra |
71826 | 4734 |
where conj = "(AND)" and disj = "(OR)" and compl = NOT |
71946 | 4735 |
and zero = 0 and one = \<open>- 1 :: 'a word\<close> |
70187
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4736 |
apply standard |
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4737 |
apply (simp_all add: word_bw_assocs word_bw_comms word_bw_lcs) |
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4738 |
apply (fact word_ao_dist2) |
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4739 |
apply (fact word_oa_dist2) |
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4740 |
done |
71946 | 4741 |
show "boolean_algebra (AND) (OR) NOT 0 (- 1 :: 'a word)" .. |
70187
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4742 |
show "xor = (XOR)" |
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4743 |
by (auto simp add: fun_eq_iff word_eq_iff xor_def word_ops_nth_size word_size) |
2082287357e6
avoid separate type class for mere definitional extension
haftmann
parents:
70186
diff
changeset
|
4744 |
qed |
37660 | 4745 |
|
65336 | 4746 |
lemma word_xor_and_or: "x XOR y = x AND NOT y OR NOT x AND y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4747 |
for x y :: "'a::len word" |
37660 | 4748 |
by (rule word_eqI) (auto simp add: word_ops_nth_size word_size) |
4749 |
||
65336 | 4750 |
lemma shiftr_x_0 [iff]: "x >> 0 = x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4751 |
for x :: "'a::len word" |
37660 | 4752 |
by (simp add: shiftr_bl) |
4753 |
||
65336 | 4754 |
lemma shiftl_x_0 [simp]: "x << 0 = x" |
4755 |
for x :: "'a::len word" |
|
37660 | 4756 |
by (simp add: shiftl_t2n) |
4757 |
||
65336 | 4758 |
lemma shiftl_1 [simp]: "(1::'a::len word) << n = 2^n" |
37660 | 4759 |
by (simp add: shiftl_t2n) |
4760 |
||
65336 | 4761 |
lemma uint_lt_0 [simp]: "uint x < 0 = False" |
37660 | 4762 |
by (simp add: linorder_not_less) |
4763 |
||
65336 | 4764 |
lemma shiftr1_1 [simp]: "shiftr1 (1::'a::len word) = 0" |
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
4765 |
unfolding shiftr1_def by simp |
37660 | 4766 |
|
65336 | 4767 |
lemma shiftr_1[simp]: "(1::'a::len word) >> n = (if n = 0 then 1 else 0)" |
37660 | 4768 |
by (induct n) (auto simp: shiftr_def) |
4769 |
||
65336 | 4770 |
lemma word_less_1 [simp]: "x < 1 \<longleftrightarrow> x = 0" |
4771 |
for x :: "'a::len word" |
|
37660 | 4772 |
by (simp add: word_less_nat_alt unat_0_iff) |
4773 |
||
4774 |
lemma to_bl_mask: |
|
65268 | 4775 |
"to_bl (mask n :: 'a::len word) = |
70185 | 4776 |
replicate (LENGTH('a) - n) False @ |
4777 |
replicate (min (LENGTH('a)) n) True" |
|
37660 | 4778 |
by (simp add: mask_bl word_rep_drop min_def) |
4779 |
||
4780 |
lemma map_replicate_True: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4781 |
"n = length xs \<Longrightarrow> |
65336 | 4782 |
map (\<lambda>(x,y). x \<and> y) (zip xs (replicate n True)) = xs" |
37660 | 4783 |
by (induct xs arbitrary: n) auto |
4784 |
||
4785 |
lemma map_replicate_False: |
|
65336 | 4786 |
"n = length xs \<Longrightarrow> map (\<lambda>(x,y). x \<and> y) |
37660 | 4787 |
(zip xs (replicate n False)) = replicate n False" |
4788 |
by (induct xs arbitrary: n) auto |
|
4789 |
||
4790 |
lemma bl_and_mask: |
|
4791 |
fixes w :: "'a::len word" |
|
65336 | 4792 |
and n :: nat |
70185 | 4793 |
defines "n' \<equiv> LENGTH('a) - n" |
65336 | 4794 |
shows "to_bl (w AND mask n) = replicate n' False @ drop n' (to_bl w)" |
65268 | 4795 |
proof - |
37660 | 4796 |
note [simp] = map_replicate_True map_replicate_False |
67399 | 4797 |
have "to_bl (w AND mask n) = map2 (\<and>) (to_bl w) (to_bl (mask n::'a::len word))" |
37660 | 4798 |
by (simp add: bl_word_and) |
65336 | 4799 |
also have "to_bl w = take n' (to_bl w) @ drop n' (to_bl w)" |
4800 |
by simp |
|
67399 | 4801 |
also have "map2 (\<and>) \<dots> (to_bl (mask n::'a::len word)) = |
65336 | 4802 |
replicate n' False @ drop n' (to_bl w)" |
70193 | 4803 |
unfolding to_bl_mask n'_def by (subst zip_append) auto |
65336 | 4804 |
finally show ?thesis . |
37660 | 4805 |
qed |
4806 |
||
4807 |
lemma drop_rev_takefill: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4808 |
"length xs \<le> n \<Longrightarrow> |
37660 | 4809 |
drop (n - length xs) (rev (takefill False n (rev xs))) = xs" |
4810 |
by (simp add: takefill_alt rev_take) |
|
4811 |
||
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4812 |
lemma map_nth_0 [simp]: "map ((!!) (0::'a::len word)) xs = replicate (length xs) False" |
37660 | 4813 |
by (induct xs) auto |
4814 |
||
4815 |
lemma uint_plus_if_size: |
|
65268 | 4816 |
"uint (x + y) = |
65336 | 4817 |
(if uint x + uint y < 2^size x |
4818 |
then uint x + uint y |
|
4819 |
else uint x + uint y - 2^size x)" |
|
4820 |
by (simp add: word_arith_wis int_word_uint mod_add_if_z word_size) |
|
37660 | 4821 |
|
4822 |
lemma unat_plus_if_size: |
|
65363 | 4823 |
"unat (x + y) = |
65336 | 4824 |
(if unat x + unat y < 2^size x |
4825 |
then unat x + unat y |
|
4826 |
else unat x + unat y - 2^size x)" |
|
65363 | 4827 |
for x y :: "'a::len word" |
37660 | 4828 |
apply (subst word_arith_nat_defs) |
4829 |
apply (subst unat_of_nat) |
|
4830 |
apply (simp add: mod_nat_add word_size) |
|
4831 |
done |
|
4832 |
||
65336 | 4833 |
lemma word_neq_0_conv: "w \<noteq> 0 \<longleftrightarrow> 0 < w" |
4834 |
for w :: "'a::len word" |
|
4835 |
by (simp add: word_gt_0) |
|
4836 |
||
4837 |
lemma max_lt: "unat (max a b div c) = unat (max a b) div unat c" |
|
4838 |
for c :: "'a::len word" |
|
55818 | 4839 |
by (fact unat_div) |
37660 | 4840 |
|
4841 |
lemma uint_sub_if_size: |
|
65268 | 4842 |
"uint (x - y) = |
65336 | 4843 |
(if uint y \<le> uint x |
4844 |
then uint x - uint y |
|
4845 |
else uint x - uint y + 2^size x)" |
|
4846 |
by (simp add: word_arith_wis int_word_uint mod_sub_if_z word_size) |
|
4847 |
||
4848 |
lemma unat_sub: "b \<le> a \<Longrightarrow> unat (a - b) = unat a - unat b" |
|
37660 | 4849 |
by (simp add: unat_def uint_sub_if_size word_le_def nat_diff_distrib) |
4850 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4851 |
lemmas word_less_sub1_numberof [simp] = word_less_sub1 [of "numeral w"] for w |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4852 |
lemmas word_le_sub1_numberof [simp] = word_le_sub1 [of "numeral w"] for w |
65268 | 4853 |
|
70185 | 4854 |
lemma word_of_int_minus: "word_of_int (2^LENGTH('a) - i) = (word_of_int (-i)::'a::len word)" |
37660 | 4855 |
proof - |
70185 | 4856 |
have *: "2^LENGTH('a) - i = -i + 2^LENGTH('a)" |
65336 | 4857 |
by simp |
37660 | 4858 |
show ?thesis |
65336 | 4859 |
apply (subst *) |
37660 | 4860 |
apply (subst word_uint.Abs_norm [symmetric], subst mod_add_self2) |
4861 |
apply simp |
|
4862 |
done |
|
4863 |
qed |
|
65268 | 4864 |
|
4865 |
lemmas word_of_int_inj = |
|
37660 | 4866 |
word_uint.Abs_inject [unfolded uints_num, simplified] |
4867 |
||
65336 | 4868 |
lemma word_le_less_eq: "x \<le> y \<longleftrightarrow> x = y \<or> x < y" |
4869 |
for x y :: "'z::len word" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4870 |
by (auto simp add: order_class.le_less) |
37660 | 4871 |
|
4872 |
lemma mod_plus_cong: |
|
65336 | 4873 |
fixes b b' :: int |
4874 |
assumes 1: "b = b'" |
|
4875 |
and 2: "x mod b' = x' mod b'" |
|
4876 |
and 3: "y mod b' = y' mod b'" |
|
4877 |
and 4: "x' + y' = z'" |
|
37660 | 4878 |
shows "(x + y) mod b = z' mod b'" |
4879 |
proof - |
|
4880 |
from 1 2[symmetric] 3[symmetric] have "(x + y) mod b = (x' mod b' + y' mod b') mod b'" |
|
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
4881 |
by (simp add: mod_add_eq) |
37660 | 4882 |
also have "\<dots> = (x' + y') mod b'" |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
4883 |
by (simp add: mod_add_eq) |
65336 | 4884 |
finally show ?thesis |
4885 |
by (simp add: 4) |
|
37660 | 4886 |
qed |
4887 |
||
4888 |
lemma mod_minus_cong: |
|
65336 | 4889 |
fixes b b' :: int |
4890 |
assumes "b = b'" |
|
4891 |
and "x mod b' = x' mod b'" |
|
4892 |
and "y mod b' = y' mod b'" |
|
4893 |
and "x' - y' = z'" |
|
37660 | 4894 |
shows "(x - y) mod b = z' mod b'" |
65336 | 4895 |
using assms [symmetric] by (auto intro: mod_diff_cong) |
4896 |
||
65363 | 4897 |
lemma word_induct_less: "P 0 \<Longrightarrow> (\<And>n. n < m \<Longrightarrow> P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P m" |
65336 | 4898 |
for P :: "'a::len word \<Rightarrow> bool" |
37660 | 4899 |
apply (cases m) |
4900 |
apply atomize |
|
4901 |
apply (erule rev_mp)+ |
|
4902 |
apply (rule_tac x=m in spec) |
|
4903 |
apply (induct_tac n) |
|
4904 |
apply simp |
|
4905 |
apply clarsimp |
|
4906 |
apply (erule impE) |
|
4907 |
apply clarsimp |
|
4908 |
apply (erule_tac x=n in allE) |
|
4909 |
apply (erule impE) |
|
4910 |
apply (simp add: unat_arith_simps) |
|
4911 |
apply (clarsimp simp: unat_of_nat) |
|
4912 |
apply simp |
|
4913 |
apply (erule_tac x="of_nat na" in allE) |
|
4914 |
apply (erule impE) |
|
4915 |
apply (simp add: unat_arith_simps) |
|
4916 |
apply (clarsimp simp: unat_of_nat) |
|
4917 |
apply simp |
|
4918 |
done |
|
65268 | 4919 |
|
65363 | 4920 |
lemma word_induct: "P 0 \<Longrightarrow> (\<And>n. P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P m" |
65336 | 4921 |
for P :: "'a::len word \<Rightarrow> bool" |
4922 |
by (erule word_induct_less) simp |
|
4923 |
||
65363 | 4924 |
lemma word_induct2 [induct type]: "P 0 \<Longrightarrow> (\<And>n. 1 + n \<noteq> 0 \<Longrightarrow> P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P n" |
65336 | 4925 |
for P :: "'b::len word \<Rightarrow> bool" |
4926 |
apply (rule word_induct) |
|
4927 |
apply simp |
|
4928 |
apply (case_tac "1 + n = 0") |
|
4929 |
apply auto |
|
37660 | 4930 |
done |
4931 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
4932 |
|
61799 | 4933 |
subsection \<open>Recursion combinator for words\<close> |
46010 | 4934 |
|
54848 | 4935 |
definition word_rec :: "'a \<Rightarrow> ('b::len word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a" |
65336 | 4936 |
where "word_rec forZero forSuc n = rec_nat forZero (forSuc \<circ> of_nat) (unat n)" |
37660 | 4937 |
|
4938 |
lemma word_rec_0: "word_rec z s 0 = z" |
|
4939 |
by (simp add: word_rec_def) |
|
4940 |
||
65363 | 4941 |
lemma word_rec_Suc: "1 + n \<noteq> 0 \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)" |
4942 |
for n :: "'a::len word" |
|
37660 | 4943 |
apply (simp add: word_rec_def unat_word_ariths) |
4944 |
apply (subst nat_mod_eq') |
|
61649
268d88ec9087
Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents:
61424
diff
changeset
|
4945 |
apply (metis Suc_eq_plus1_left Suc_lessI of_nat_2p unat_1 unat_lt2p word_arith_nat_add) |
37660 | 4946 |
apply simp |
4947 |
done |
|
4948 |
||
65363 | 4949 |
lemma word_rec_Pred: "n \<noteq> 0 \<Longrightarrow> word_rec z s n = s (n - 1) (word_rec z s (n - 1))" |
37660 | 4950 |
apply (rule subst[where t="n" and s="1 + (n - 1)"]) |
4951 |
apply simp |
|
4952 |
apply (subst word_rec_Suc) |
|
4953 |
apply simp |
|
4954 |
apply simp |
|
4955 |
done |
|
4956 |
||
65336 | 4957 |
lemma word_rec_in: "f (word_rec z (\<lambda>_. f) n) = word_rec (f z) (\<lambda>_. f) n" |
37660 | 4958 |
by (induct n) (simp_all add: word_rec_0 word_rec_Suc) |
4959 |
||
67399 | 4960 |
lemma word_rec_in2: "f n (word_rec z f n) = word_rec (f 0 z) (f \<circ> (+) 1) n" |
37660 | 4961 |
by (induct n) (simp_all add: word_rec_0 word_rec_Suc) |
4962 |
||
65268 | 4963 |
lemma word_rec_twice: |
67399 | 4964 |
"m \<le> n \<Longrightarrow> word_rec z f n = word_rec (word_rec z f (n - m)) (f \<circ> (+) (n - m)) m" |
65336 | 4965 |
apply (erule rev_mp) |
4966 |
apply (rule_tac x=z in spec) |
|
4967 |
apply (rule_tac x=f in spec) |
|
4968 |
apply (induct n) |
|
4969 |
apply (simp add: word_rec_0) |
|
4970 |
apply clarsimp |
|
4971 |
apply (rule_tac t="1 + n - m" and s="1 + (n - m)" in subst) |
|
4972 |
apply simp |
|
4973 |
apply (case_tac "1 + (n - m) = 0") |
|
4974 |
apply (simp add: word_rec_0) |
|
4975 |
apply (rule_tac f = "word_rec a b" for a b in arg_cong) |
|
4976 |
apply (rule_tac t="m" and s="m + (1 + (n - m))" in subst) |
|
4977 |
apply simp |
|
4978 |
apply (simp (no_asm_use)) |
|
4979 |
apply (simp add: word_rec_Suc word_rec_in2) |
|
4980 |
apply (erule impE) |
|
4981 |
apply uint_arith |
|
67399 | 4982 |
apply (drule_tac x="x \<circ> (+) 1" in spec) |
65336 | 4983 |
apply (drule_tac x="x 0 xa" in spec) |
37660 | 4984 |
apply simp |
65336 | 4985 |
apply (rule_tac t="\<lambda>a. x (1 + (n - m + a))" and s="\<lambda>a. x (1 + (n - m) + a)" in subst) |
4986 |
apply (clarsimp simp add: fun_eq_iff) |
|
4987 |
apply (rule_tac t="(1 + (n - m + xb))" and s="1 + (n - m) + xb" in subst) |
|
4988 |
apply simp |
|
4989 |
apply (rule refl) |
|
4990 |
apply (rule refl) |
|
4991 |
done |
|
37660 | 4992 |
|
4993 |
lemma word_rec_id: "word_rec z (\<lambda>_. id) n = z" |
|
4994 |
by (induct n) (auto simp add: word_rec_0 word_rec_Suc) |
|
4995 |
||
4996 |
lemma word_rec_id_eq: "\<forall>m < n. f m = id \<Longrightarrow> word_rec z f n = z" |
|
65336 | 4997 |
apply (erule rev_mp) |
4998 |
apply (induct n) |
|
4999 |
apply (auto simp add: word_rec_0 word_rec_Suc) |
|
5000 |
apply (drule spec, erule mp) |
|
5001 |
apply uint_arith |
|
5002 |
apply (drule_tac x=n in spec, erule impE) |
|
5003 |
apply uint_arith |
|
5004 |
apply simp |
|
5005 |
done |
|
37660 | 5006 |
|
65268 | 5007 |
lemma word_rec_max: |
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
58061
diff
changeset
|
5008 |
"\<forall>m\<ge>n. m \<noteq> - 1 \<longrightarrow> f m = id \<Longrightarrow> word_rec z f (- 1) = word_rec z f n" |
65336 | 5009 |
apply (subst word_rec_twice[where n="-1" and m="-1 - n"]) |
5010 |
apply simp |
|
5011 |
apply simp |
|
5012 |
apply (rule word_rec_id_eq) |
|
5013 |
apply clarsimp |
|
5014 |
apply (drule spec, rule mp, erule mp) |
|
5015 |
apply (rule word_plus_mono_right2[OF _ order_less_imp_le]) |
|
5016 |
prefer 2 |
|
5017 |
apply assumption |
|
5018 |
apply simp |
|
5019 |
apply (erule contrapos_pn) |
|
5020 |
apply simp |
|
5021 |
apply (drule arg_cong[where f="\<lambda>x. x - n"]) |
|
5022 |
apply simp |
|
5023 |
done |
|
5024 |
||
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5025 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5026 |
subsection \<open>More\<close> |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5027 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5028 |
lemma test_bit_1' [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
5029 |
"(1 :: 'a :: len word) !! n \<longleftrightarrow> 0 < LENGTH('a) \<and> n = 0" |
71949 | 5030 |
by (cases n) (simp_all only: one_word_def test_bit_wi, simp_all) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5031 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5032 |
lemma mask_0 [simp]: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5033 |
"mask 0 = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5034 |
by (simp add: Word.mask_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5035 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5036 |
lemma shiftl0 [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
5037 |
"x << 0 = (x :: 'a :: len word)" |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5038 |
by (metis shiftl_rev shiftr_x_0 word_rev_gal) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5039 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5040 |
lemma mask_1: "mask 1 = 1" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5041 |
by (simp add: mask_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5042 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5043 |
lemma mask_Suc_0: "mask (Suc 0) = 1" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5044 |
by (simp add: mask_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5045 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5046 |
lemma mask_numeral: "mask (numeral n) = 2 * mask (pred_numeral n) + 1" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5047 |
by (simp add: mask_def neg_numeral_class.sub_def numeral_eq_Suc numeral_pow) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5048 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5049 |
lemma bin_last_bintrunc: "bin_last (bintrunc l n) = (l > 0 \<and> bin_last n)" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5050 |
by (cases l) simp_all |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5051 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5052 |
lemma word_and_1: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5053 |
"n AND 1 = (if n !! 0 then 1 else 0)" for n :: "_ word" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5054 |
by transfer (rule bin_rl_eqI, simp_all add: bin_rest_trunc bin_last_bintrunc) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5055 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5056 |
lemma bintrunc_shiftl: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5057 |
"bintrunc n (m << i) = bintrunc (n - i) m << i" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5058 |
proof (induction i arbitrary: n) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5059 |
case 0 |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5060 |
show ?case |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5061 |
by simp |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5062 |
next |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5063 |
case (Suc i) |
71947 | 5064 |
then show ?case by (cases n) (simp_all add: take_bit_Suc Bit_def) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5065 |
qed |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5066 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5067 |
lemma shiftl_transfer [transfer_rule]: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5068 |
includes lifting_syntax |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5069 |
shows "(pcr_word ===> (=) ===> pcr_word) (<<) (<<)" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5070 |
by (auto intro!: rel_funI word_eqI simp add: word.pcr_cr_eq cr_word_def word_size nth_shiftl) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5071 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5072 |
lemma uint_shiftl: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5073 |
"uint (n << i) = bintrunc (size n) (uint n << i)" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5074 |
unfolding word_size by transfer (simp add: bintrunc_shiftl) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5075 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5076 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
5077 |
subsection \<open>Misc\<close> |
37660 | 5078 |
|
5079 |
declare bin_to_bl_def [simp] |
|
5080 |
||
69605 | 5081 |
ML_file \<open>Tools/word_lib.ML\<close> |
5082 |
ML_file \<open>Tools/smt_word.ML\<close> |
|
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
35049
diff
changeset
|
5083 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
5084 |
hide_const (open) Word |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
5085 |
|
41060
4199fdcfa3c0
moved smt_word.ML into the directory of the Word library
boehmes
parents:
40827
diff
changeset
|
5086 |
end |