author | haftmann |
Tue, 16 Apr 2019 19:50:05 +0000 | |
changeset 70170 | 56727602d0a5 |
parent 70169 | 8bb835f10a39 |
permissions | -rw-r--r-- |
65363 | 1 |
(* Title: HOL/Word/Bit_Representation.thy |
2 |
Author: Jeremy Dawson, NICTA |
|
3 |
*) |
|
24333 | 4 |
|
70169 | 5 |
section \<open>Integers as implicit bit strings\<close> |
24350 | 6 |
|
37658 | 7 |
theory Bit_Representation |
70170 | 8 |
imports Main |
24333 | 9 |
begin |
10 |
||
70170 | 11 |
lemma int_mod_lem: "0 < n \<Longrightarrow> 0 \<le> b \<and> b < n \<longleftrightarrow> b mod n = b" |
12 |
for b n :: int |
|
13 |
apply safe |
|
14 |
apply (erule (1) mod_pos_pos_trivial) |
|
15 |
apply (erule_tac [!] subst) |
|
16 |
apply auto |
|
17 |
done |
|
18 |
||
19 |
lemma int_mod_ge: "a < n \<Longrightarrow> 0 < n \<Longrightarrow> a \<le> a mod n" |
|
20 |
for a n :: int |
|
21 |
by (metis dual_order.trans le_cases mod_pos_pos_trivial pos_mod_conj) |
|
22 |
||
23 |
lemma int_mod_ge': "b < 0 \<Longrightarrow> 0 < n \<Longrightarrow> b + n \<le> b mod n" |
|
24 |
for b n :: int |
|
25 |
by (metis add_less_same_cancel2 int_mod_ge mod_add_self2) |
|
26 |
||
27 |
lemma int_mod_le': "0 \<le> b - n \<Longrightarrow> b mod n \<le> b - n" |
|
28 |
for b n :: int |
|
29 |
by (metis minus_mod_self2 zmod_le_nonneg_dividend) |
|
30 |
||
31 |
lemma emep1: "even n \<Longrightarrow> even d \<Longrightarrow> 0 \<le> d \<Longrightarrow> (n + 1) mod d = (n mod d) + 1" |
|
32 |
for n d :: int |
|
33 |
by (auto simp add: pos_zmod_mult_2 add.commute dvd_def) |
|
34 |
||
35 |
lemma m1mod2k: "- 1 mod 2 ^ n = (2 ^ n - 1 :: int)" |
|
36 |
by (rule zmod_minus1) simp |
|
37 |
||
38 |
||
61799 | 39 |
subsection \<open>Constructors and destructors for binary integers\<close> |
26557 | 40 |
|
65363 | 41 |
definition Bit :: "int \<Rightarrow> bool \<Rightarrow> int" (infixl "BIT" 90) |
42 |
where "k BIT b = (if b then 1 else 0) + k + k" |
|
26557 | 43 |
|
65363 | 44 |
lemma Bit_B0: "k BIT False = k + k" |
45 |
by (simp add: Bit_def) |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
46 |
|
65363 | 47 |
lemma Bit_B1: "k BIT True = k + k + 1" |
48 |
by (simp add: Bit_def) |
|
49 |
||
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
50 |
lemma Bit_B0_2t: "k BIT False = 2 * k" |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
51 |
by (rule trans, rule Bit_B0) simp |
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
52 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
53 |
lemma Bit_B1_2t: "k BIT True = 2 * k + 1" |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
54 |
by (rule trans, rule Bit_B1) simp |
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
55 |
|
70169 | 56 |
lemma power_BIT: "2 ^ Suc n - 1 = (2 ^ n - 1) BIT True" |
57 |
by (simp add: Bit_B1) |
|
58 |
||
54848 | 59 |
definition bin_last :: "int \<Rightarrow> bool" |
65363 | 60 |
where "bin_last w \<longleftrightarrow> w mod 2 = 1" |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
61 |
|
65363 | 62 |
lemma bin_last_odd: "bin_last = odd" |
58645 | 63 |
by (rule ext) (simp add: bin_last_def even_iff_mod_2_eq_zero) |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
64 |
|
54848 | 65 |
definition bin_rest :: "int \<Rightarrow> int" |
65363 | 66 |
where "bin_rest w = w div 2" |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
67 |
|
65363 | 68 |
lemma bin_rl_simp [simp]: "bin_rest w BIT bin_last w = w" |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
69 |
unfolding bin_rest_def bin_last_def Bit_def |
65363 | 70 |
by (cases "w mod 2 = 0") (use div_mult_mod_eq [of w 2] in simp_all) |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
71 |
|
46600 | 72 |
lemma bin_rest_BIT [simp]: "bin_rest (x BIT b) = x" |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
73 |
unfolding bin_rest_def Bit_def |
65363 | 74 |
by (cases b) simp_all |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
75 |
|
46600 | 76 |
lemma bin_last_BIT [simp]: "bin_last (x BIT b) = b" |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
77 |
unfolding bin_last_def Bit_def |
54873 | 78 |
by (cases b) simp_all |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
79 |
|
45848 | 80 |
lemma BIT_eq_iff [iff]: "u BIT b = v BIT c \<longleftrightarrow> u = v \<and> b = c" |
65363 | 81 |
by (auto simp: Bit_def) arith+ |
45843
c58ce659ce2a
reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents:
45604
diff
changeset
|
82 |
|
45849 | 83 |
lemma BIT_bin_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
84 |
"numeral k BIT False = numeral (Num.Bit0 k)" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
85 |
"numeral k BIT True = numeral (Num.Bit1 k)" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
86 |
"(- numeral k) BIT False = - numeral (Num.Bit0 k)" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
87 |
"(- numeral k) BIT True = - numeral (Num.BitM k)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
88 |
unfolding numeral.simps numeral_BitM |
65363 | 89 |
by (simp_all add: Bit_def del: arith_simps add_numeral_special diff_numeral_special) |
45849 | 90 |
|
91 |
lemma BIT_special_simps [simp]: |
|
65363 | 92 |
shows "0 BIT False = 0" |
93 |
and "0 BIT True = 1" |
|
94 |
and "1 BIT False = 2" |
|
95 |
and "1 BIT True = 3" |
|
96 |
and "(- 1) BIT False = - 2" |
|
97 |
and "(- 1) BIT True = - 1" |
|
98 |
by (simp_all add: Bit_def) |
|
45849 | 99 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
100 |
lemma Bit_eq_0_iff: "w BIT b = 0 \<longleftrightarrow> w = 0 \<and> \<not> b" |
65363 | 101 |
by (auto simp: Bit_def) arith |
53438 | 102 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
103 |
lemma Bit_eq_m1_iff: "w BIT b = -1 \<longleftrightarrow> w = -1 \<and> b" |
65363 | 104 |
by (auto simp: Bit_def) arith |
53438 | 105 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
106 |
lemma BitM_inc: "Num.BitM (Num.inc w) = Num.Bit1 w" |
65363 | 107 |
by (induct w) simp_all |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
108 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
109 |
lemma expand_BIT: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
110 |
"numeral (Num.Bit0 w) = numeral w BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
111 |
"numeral (Num.Bit1 w) = numeral w BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
112 |
"- numeral (Num.Bit0 w) = (- numeral w) BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
113 |
"- numeral (Num.Bit1 w) = (- numeral (w + Num.One)) BIT True" |
65363 | 114 |
by (simp_all add: add_One BitM_inc) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
115 |
|
45851
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
116 |
lemma bin_last_numeral_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
117 |
"\<not> bin_last 0" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
118 |
"bin_last 1" |
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
54873
diff
changeset
|
119 |
"bin_last (- 1)" |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
120 |
"bin_last Numeral1" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
121 |
"\<not> bin_last (numeral (Num.Bit0 w))" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
122 |
"bin_last (numeral (Num.Bit1 w))" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
123 |
"\<not> bin_last (- numeral (Num.Bit0 w))" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
124 |
"bin_last (- numeral (Num.Bit1 w))" |
60867 | 125 |
by (simp_all add: bin_last_def zmod_zminus1_eq_if) (auto simp add: divmod_def) |
45851
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
126 |
|
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
127 |
lemma bin_rest_numeral_simps [simp]: |
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
128 |
"bin_rest 0 = 0" |
19f7ac6cf3cc
add simp rules for bin_rest and bin_last applied to numerals
huffman
parents:
45850
diff
changeset
|
129 |
"bin_rest 1 = 0" |
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
54873
diff
changeset
|
130 |
"bin_rest (- 1) = - 1" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
131 |
"bin_rest Numeral1 = 0" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
132 |
"bin_rest (numeral (Num.Bit0 w)) = numeral w" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
133 |
"bin_rest (numeral (Num.Bit1 w)) = numeral w" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
134 |
"bin_rest (- numeral (Num.Bit0 w)) = - numeral w" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
135 |
"bin_rest (- numeral (Num.Bit1 w)) = - numeral (w + Num.One)" |
60867 | 136 |
by (simp_all add: bin_rest_def zdiv_zminus1_eq_if) (auto simp add: divmod_def) |
26557 | 137 |
|
65363 | 138 |
lemma less_Bits: "v BIT b < w BIT c \<longleftrightarrow> v < w \<or> v \<le> w \<and> \<not> b \<and> c" |
139 |
by (auto simp: Bit_def) |
|
24333 | 140 |
|
65363 | 141 |
lemma le_Bits: "v BIT b \<le> w BIT c \<longleftrightarrow> v < w \<or> v \<le> w \<and> (\<not> b \<or> c)" |
142 |
by (auto simp: Bit_def) |
|
26557 | 143 |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
144 |
lemma pred_BIT_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
145 |
"x BIT False - 1 = (x - 1) BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
146 |
"x BIT True - 1 = x BIT False" |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
147 |
by (simp_all add: Bit_B0_2t Bit_B1_2t) |
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
148 |
|
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
149 |
lemma succ_BIT_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
150 |
"x BIT False + 1 = x BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
151 |
"x BIT True + 1 = (x + 1) BIT False" |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
152 |
by (simp_all add: Bit_B0_2t Bit_B1_2t) |
26557 | 153 |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
154 |
lemma add_BIT_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
155 |
"x BIT False + y BIT False = (x + y) BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
156 |
"x BIT False + y BIT True = (x + y) BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
157 |
"x BIT True + y BIT False = (x + y) BIT True" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
158 |
"x BIT True + y BIT True = (x + y + 1) BIT False" |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
159 |
by (simp_all add: Bit_B0_2t Bit_B1_2t) |
26557 | 160 |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
161 |
lemma mult_BIT_simps [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
162 |
"x BIT False * y = (x * y) BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
163 |
"x * y BIT False = (x * y) BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
164 |
"x BIT True * y = (x * y) BIT False + y" |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
165 |
by (simp_all add: Bit_B0_2t Bit_B1_2t algebra_simps) |
26557 | 166 |
|
65363 | 167 |
lemma B_mod_2': "X = 2 \<Longrightarrow> (w BIT True) mod X = 1 \<and> (w BIT False) mod X = 0" |
168 |
by (simp add: Bit_B0 Bit_B1) |
|
24333 | 169 |
|
65363 | 170 |
lemma bin_ex_rl: "\<exists>w b. w BIT b = bin" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
171 |
by (metis bin_rl_simp) |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
172 |
|
67142 | 173 |
lemma bin_exhaust: "(\<And>x b. bin = x BIT b \<Longrightarrow> Q) \<Longrightarrow> Q" |
174 |
by (metis bin_ex_rl) |
|
24333 | 175 |
|
67160 | 176 |
primrec bin_nth :: "int \<Rightarrow> nat \<Rightarrow> bool" |
65363 | 177 |
where |
178 |
Z: "bin_nth w 0 \<longleftrightarrow> bin_last w" |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
179 |
| Suc: "bin_nth w (Suc n) \<longleftrightarrow> bin_nth (bin_rest w) n" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
180 |
|
67160 | 181 |
lemma bin_nth_eq_mod: |
182 |
"bin_nth w n \<longleftrightarrow> odd (w div 2 ^ n)" |
|
183 |
by (induction n arbitrary: w) (simp_all add: bin_last_def bin_rest_def odd_iff_mod_2_eq_one zdiv_zmult2_eq) |
|
184 |
||
65363 | 185 |
lemma bin_abs_lem: "bin = (w BIT b) \<Longrightarrow> bin \<noteq> -1 \<longrightarrow> bin \<noteq> 0 \<longrightarrow> nat \<bar>w\<bar> < nat \<bar>bin\<bar>" |
46598 | 186 |
apply clarsimp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
187 |
apply (unfold Bit_def) |
26557 | 188 |
apply (cases b) |
189 |
apply (clarsimp, arith) |
|
190 |
apply (clarsimp, arith) |
|
191 |
done |
|
192 |
||
193 |
lemma bin_induct: |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
194 |
assumes PPls: "P 0" |
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
54873
diff
changeset
|
195 |
and PMin: "P (- 1)" |
65363 | 196 |
and PBit: "\<And>bin bit. P bin \<Longrightarrow> P (bin BIT bit)" |
26557 | 197 |
shows "P bin" |
65363 | 198 |
apply (rule_tac P=P and a=bin and f1="nat \<circ> abs" in wf_measure [THEN wf_induct]) |
26557 | 199 |
apply (simp add: measure_def inv_image_def) |
200 |
apply (case_tac x rule: bin_exhaust) |
|
201 |
apply (frule bin_abs_lem) |
|
202 |
apply (auto simp add : PPls PMin PBit) |
|
203 |
done |
|
204 |
||
24333 | 205 |
lemma Bit_div2 [simp]: "(w BIT b) div 2 = w" |
46600 | 206 |
unfolding bin_rest_def [symmetric] by (rule bin_rest_BIT) |
24333 | 207 |
|
65363 | 208 |
lemma bin_nth_eq_iff: "bin_nth x = bin_nth y \<longleftrightarrow> x = y" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
209 |
proof - |
65363 | 210 |
have bin_nth_lem [rule_format]: "\<forall>y. bin_nth x = bin_nth y \<longrightarrow> x = y" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
211 |
apply (induct x rule: bin_induct) |
46008
c296c75f4cf4
reverted some changes for set->predicate transition, according to "hg log -u berghofe -r Isabelle2007:Isabelle2008";
wenzelm
parents:
46001
diff
changeset
|
212 |
apply safe |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
213 |
apply (erule rev_mp) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
214 |
apply (induct_tac y rule: bin_induct) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
215 |
apply safe |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
216 |
apply (drule_tac x=0 in fun_cong, force) |
65363 | 217 |
apply (erule notE, rule ext, drule_tac x="Suc x" in fun_cong, force) |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
218 |
apply (drule_tac x=0 in fun_cong, force) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
219 |
apply (erule rev_mp) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
220 |
apply (induct_tac y rule: bin_induct) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
221 |
apply safe |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
222 |
apply (drule_tac x=0 in fun_cong, force) |
65363 | 223 |
apply (erule notE, rule ext, drule_tac x="Suc x" in fun_cong, force) |
224 |
apply (metis Bit_eq_m1_iff Z bin_last_BIT) |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
225 |
apply (case_tac y rule: bin_exhaust) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
226 |
apply clarify |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
227 |
apply (erule allE) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
228 |
apply (erule impE) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
229 |
prefer 2 |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
230 |
apply (erule conjI) |
24333 | 231 |
apply (drule_tac x=0 in fun_cong, force) |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
232 |
apply (rule ext) |
59807 | 233 |
apply (drule_tac x="Suc x" for x in fun_cong, force) |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
234 |
done |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
235 |
show ?thesis |
65363 | 236 |
by (auto elim: bin_nth_lem) |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
237 |
qed |
24333 | 238 |
|
45604 | 239 |
lemmas bin_eqI = ext [THEN bin_nth_eq_iff [THEN iffD1]] |
24333 | 240 |
|
65363 | 241 |
lemma bin_eq_iff: "x = y \<longleftrightarrow> (\<forall>n. bin_nth x n = bin_nth y n)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
242 |
using bin_nth_eq_iff by auto |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
243 |
|
45853 | 244 |
lemma bin_nth_zero [simp]: "\<not> bin_nth 0 n" |
245 |
by (induct n) auto |
|
246 |
||
46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46001
diff
changeset
|
247 |
lemma bin_nth_1 [simp]: "bin_nth 1 n \<longleftrightarrow> n = 0" |
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46001
diff
changeset
|
248 |
by (cases n) simp_all |
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46001
diff
changeset
|
249 |
|
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
54873
diff
changeset
|
250 |
lemma bin_nth_minus1 [simp]: "bin_nth (- 1) n" |
45952 | 251 |
by (induct n) auto |
252 |
||
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
253 |
lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 \<longleftrightarrow> b" |
24333 | 254 |
by auto |
255 |
||
256 |
lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n" |
|
257 |
by auto |
|
258 |
||
65363 | 259 |
lemma bin_nth_minus [simp]: "0 < n \<Longrightarrow> bin_nth (w BIT b) n = bin_nth w (n - 1)" |
24333 | 260 |
by (cases n) auto |
261 |
||
65363 | 262 |
lemma bin_nth_numeral: "bin_rest x = y \<Longrightarrow> bin_nth x (numeral n) = bin_nth y (pred_numeral n)" |
47219
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47170
diff
changeset
|
263 |
by (simp add: numeral_eq_Suc) |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
264 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
265 |
lemmas bin_nth_numeral_simps [simp] = |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
266 |
bin_nth_numeral [OF bin_rest_numeral_simps(2)] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
267 |
bin_nth_numeral [OF bin_rest_numeral_simps(5)] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
268 |
bin_nth_numeral [OF bin_rest_numeral_simps(6)] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
269 |
bin_nth_numeral [OF bin_rest_numeral_simps(7)] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
270 |
bin_nth_numeral [OF bin_rest_numeral_simps(8)] |
24333 | 271 |
|
65363 | 272 |
lemmas bin_nth_simps = |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
273 |
bin_nth.Z bin_nth.Suc bin_nth_zero bin_nth_minus1 |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
274 |
bin_nth_numeral_simps |
24333 | 275 |
|
70169 | 276 |
lemma nth_2p_bin: "bin_nth (2 ^ n) m = (m = n)" \<comment> \<open>for use when simplifying with \<open>bin_nth_Bit\<close>\<close> |
277 |
apply (induct n arbitrary: m) |
|
278 |
apply clarsimp |
|
279 |
apply safe |
|
280 |
apply (case_tac m) |
|
281 |
apply (auto simp: Bit_B0_2t [symmetric]) |
|
282 |
done |
|
283 |
||
26557 | 284 |
|
61799 | 285 |
subsection \<open>Truncating binary integers\<close> |
26557 | 286 |
|
54848 | 287 |
definition bin_sign :: "int \<Rightarrow> int" |
65363 | 288 |
where "bin_sign k = (if k \<ge> 0 then 0 else - 1)" |
26557 | 289 |
|
290 |
lemma bin_sign_simps [simp]: |
|
45850 | 291 |
"bin_sign 0 = 0" |
46604
9f9e85264e4d
make uses of bin_sign respect int/bin distinction
huffman
parents:
46601
diff
changeset
|
292 |
"bin_sign 1 = 0" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
293 |
"bin_sign (- 1) = - 1" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
294 |
"bin_sign (numeral k) = 0" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
295 |
"bin_sign (- numeral k) = -1" |
26557 | 296 |
"bin_sign (w BIT b) = bin_sign w" |
65363 | 297 |
by (simp_all add: bin_sign_def Bit_def) |
26557 | 298 |
|
65363 | 299 |
lemma bin_sign_rest [simp]: "bin_sign (bin_rest w) = bin_sign w" |
26557 | 300 |
by (cases w rule: bin_exhaust) auto |
24364 | 301 |
|
65363 | 302 |
primrec bintrunc :: "nat \<Rightarrow> int \<Rightarrow> int" |
303 |
where |
|
304 |
Z : "bintrunc 0 bin = 0" |
|
305 |
| Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)" |
|
24364 | 306 |
|
65363 | 307 |
primrec sbintrunc :: "nat \<Rightarrow> int \<Rightarrow> int" |
308 |
where |
|
309 |
Z : "sbintrunc 0 bin = (if bin_last bin then -1 else 0)" |
|
310 |
| Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)" |
|
37667 | 311 |
|
65363 | 312 |
lemma bintrunc_mod2p: "bintrunc n w = w mod 2 ^ n" |
313 |
by (induct n arbitrary: w) (auto simp add: bin_last_def bin_rest_def Bit_def zmod_zmult2_eq) |
|
24333 | 314 |
|
67160 | 315 |
lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n" |
316 |
proof (induction n arbitrary: w) |
|
317 |
case 0 |
|
318 |
then show ?case |
|
319 |
by (auto simp add: bin_last_odd odd_iff_mod_2_eq_one) |
|
320 |
next |
|
321 |
case (Suc n) |
|
322 |
moreover have "((bin_rest w + 2 ^ n) mod (2 * 2 ^ n) - 2 ^ n) BIT bin_last w = |
|
323 |
(w + 2 * 2 ^ n) mod (4 * 2 ^ n) - 2 * 2 ^ n" |
|
324 |
proof (cases w rule: parity_cases) |
|
325 |
case even |
|
326 |
then show ?thesis |
|
327 |
by (simp add: bin_last_odd bin_rest_def Bit_B0_2t mult_mod_right) |
|
328 |
next |
|
329 |
case odd |
|
330 |
then have "2 * (w div 2) = w - 1" |
|
331 |
using minus_mod_eq_mult_div [of w 2] by simp |
|
332 |
moreover have "(2 * 2 ^ n + w - 1) mod (2 * 2 * 2 ^ n) + 1 = (2 * 2 ^ n + w) mod (2 * 2 * 2 ^ n)" |
|
333 |
using odd emep1 [of "2 * 2 ^ n + w - 1" "2 * 2 * 2 ^ n"] by simp |
|
334 |
ultimately show ?thesis |
|
335 |
using odd by (simp add: bin_last_odd bin_rest_def Bit_B1_2t mult_mod_right) (simp add: algebra_simps) |
|
336 |
qed |
|
337 |
ultimately show ?case |
|
338 |
by simp |
|
339 |
qed |
|
24333 | 340 |
|
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64246
diff
changeset
|
341 |
|
24465 | 342 |
subsection "Simplifications for (s)bintrunc" |
343 |
||
67160 | 344 |
lemma sign_bintr: "bin_sign (bintrunc n w) = 0" |
345 |
by (simp add: bintrunc_mod2p bin_sign_def) |
|
346 |
||
45852 | 347 |
lemma bintrunc_n_0 [simp]: "bintrunc n 0 = 0" |
67160 | 348 |
by (simp add: bintrunc_mod2p) |
45852 | 349 |
|
45855 | 350 |
lemma sbintrunc_n_0 [simp]: "sbintrunc n 0 = 0" |
67160 | 351 |
by (simp add: sbintrunc_mod2p) |
45855 | 352 |
|
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
54873
diff
changeset
|
353 |
lemma sbintrunc_n_minus1 [simp]: "sbintrunc n (- 1) = -1" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
354 |
by (induct n) auto |
45856 | 355 |
|
45852 | 356 |
lemma bintrunc_Suc_numeral: |
357 |
"bintrunc (Suc n) 1 = 1" |
|
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
54873
diff
changeset
|
358 |
"bintrunc (Suc n) (- 1) = bintrunc n (- 1) BIT True" |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
359 |
"bintrunc (Suc n) (numeral (Num.Bit0 w)) = bintrunc n (numeral w) BIT False" |
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
360 |
"bintrunc (Suc n) (numeral (Num.Bit1 w)) = bintrunc n (numeral w) BIT True" |
65363 | 361 |
"bintrunc (Suc n) (- numeral (Num.Bit0 w)) = bintrunc n (- numeral w) BIT False" |
362 |
"bintrunc (Suc n) (- numeral (Num.Bit1 w)) = bintrunc n (- numeral (w + Num.One)) BIT True" |
|
45852 | 363 |
by simp_all |
364 |
||
45856 | 365 |
lemma sbintrunc_0_numeral [simp]: |
366 |
"sbintrunc 0 1 = -1" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
367 |
"sbintrunc 0 (numeral (Num.Bit0 w)) = 0" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
368 |
"sbintrunc 0 (numeral (Num.Bit1 w)) = -1" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
369 |
"sbintrunc 0 (- numeral (Num.Bit0 w)) = 0" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
370 |
"sbintrunc 0 (- numeral (Num.Bit1 w)) = -1" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
371 |
by simp_all |
45856 | 372 |
|
45855 | 373 |
lemma sbintrunc_Suc_numeral: |
374 |
"sbintrunc (Suc n) 1 = 1" |
|
65363 | 375 |
"sbintrunc (Suc n) (numeral (Num.Bit0 w)) = sbintrunc n (numeral w) BIT False" |
376 |
"sbintrunc (Suc n) (numeral (Num.Bit1 w)) = sbintrunc n (numeral w) BIT True" |
|
377 |
"sbintrunc (Suc n) (- numeral (Num.Bit0 w)) = sbintrunc n (- numeral w) BIT False" |
|
378 |
"sbintrunc (Suc n) (- numeral (Num.Bit1 w)) = sbintrunc n (- numeral (w + Num.One)) BIT True" |
|
45855 | 379 |
by simp_all |
380 |
||
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
381 |
lemma bin_sign_lem: "(bin_sign (sbintrunc n bin) = -1) = bin_nth bin n" |
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
382 |
apply (induct n arbitrary: bin) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
383 |
apply (case_tac bin rule: bin_exhaust, case_tac b, auto) |
24333 | 384 |
done |
385 |
||
65363 | 386 |
lemma nth_bintr: "bin_nth (bintrunc m w) n \<longleftrightarrow> n < m \<and> bin_nth w n" |
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
387 |
apply (induct n arbitrary: w m) |
24333 | 388 |
apply (case_tac m, auto)[1] |
389 |
apply (case_tac m, auto)[1] |
|
390 |
done |
|
391 |
||
65363 | 392 |
lemma nth_sbintr: "bin_nth (sbintrunc m w) n = (if n < m then bin_nth w n else bin_nth w m)" |
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
393 |
apply (induct n arbitrary: w m) |
65363 | 394 |
apply (case_tac m) |
395 |
apply simp_all |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
396 |
apply (case_tac m) |
65363 | 397 |
apply simp_all |
24333 | 398 |
done |
399 |
||
65363 | 400 |
lemma bin_nth_Bit: "bin_nth (w BIT b) n \<longleftrightarrow> n = 0 \<and> b \<or> (\<exists>m. n = Suc m \<and> bin_nth w m)" |
24333 | 401 |
by (cases n) auto |
402 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
403 |
lemma bin_nth_Bit0: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
404 |
"bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow> |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
405 |
(\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)" |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
406 |
using bin_nth_Bit [where w="numeral w" and b="False"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
407 |
|
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
408 |
lemma bin_nth_Bit1: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
409 |
"bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow> |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
410 |
n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)" |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
411 |
using bin_nth_Bit [where w="numeral w" and b="True"] by simp |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
412 |
|
65363 | 413 |
lemma bintrunc_bintrunc_l: "n \<le> m \<Longrightarrow> bintrunc m (bintrunc n w) = bintrunc n w" |
414 |
by (rule bin_eqI) (auto simp: nth_bintr) |
|
24333 | 415 |
|
65363 | 416 |
lemma sbintrunc_sbintrunc_l: "n \<le> m \<Longrightarrow> sbintrunc m (sbintrunc n w) = sbintrunc n w" |
32439 | 417 |
by (rule bin_eqI) (auto simp: nth_sbintr) |
24333 | 418 |
|
65363 | 419 |
lemma bintrunc_bintrunc_ge: "n \<le> m \<Longrightarrow> bintrunc n (bintrunc m w) = bintrunc n w" |
24333 | 420 |
by (rule bin_eqI) (auto simp: nth_bintr) |
421 |
||
65363 | 422 |
lemma bintrunc_bintrunc_min [simp]: "bintrunc m (bintrunc n w) = bintrunc (min m n) w" |
423 |
by (rule bin_eqI) (auto simp: nth_bintr) |
|
24333 | 424 |
|
65363 | 425 |
lemma sbintrunc_sbintrunc_min [simp]: "sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w" |
426 |
by (rule bin_eqI) (auto simp: nth_sbintr min.absorb1 min.absorb2) |
|
24333 | 427 |
|
65363 | 428 |
lemmas bintrunc_Pls = |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
429 |
bintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
24333 | 430 |
|
65363 | 431 |
lemmas bintrunc_Min [simp] = |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
432 |
bintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
24333 | 433 |
|
65363 | 434 |
lemmas bintrunc_BIT [simp] = |
46600 | 435 |
bintrunc.Suc [where bin="w BIT b", simplified bin_last_BIT bin_rest_BIT] for w b |
24333 | 436 |
|
437 |
lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT |
|
45852 | 438 |
bintrunc_Suc_numeral |
24333 | 439 |
|
65363 | 440 |
lemmas sbintrunc_Suc_Pls = |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
441 |
sbintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
24333 | 442 |
|
65363 | 443 |
lemmas sbintrunc_Suc_Min = |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
444 |
sbintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
24333 | 445 |
|
65363 | 446 |
lemmas sbintrunc_Suc_BIT [simp] = |
46600 | 447 |
sbintrunc.Suc [where bin="w BIT b", simplified bin_last_BIT bin_rest_BIT] for w b |
24333 | 448 |
|
449 |
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min sbintrunc_Suc_BIT |
|
45855 | 450 |
sbintrunc_Suc_numeral |
24333 | 451 |
|
65363 | 452 |
lemmas sbintrunc_Pls = |
453 |
sbintrunc.Z [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
|
24333 | 454 |
|
65363 | 455 |
lemmas sbintrunc_Min = |
456 |
sbintrunc.Z [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
|
24333 | 457 |
|
65363 | 458 |
lemmas sbintrunc_0_BIT_B0 [simp] = |
459 |
sbintrunc.Z [where bin="w BIT False", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
|
460 |
for w |
|
24333 | 461 |
|
65363 | 462 |
lemmas sbintrunc_0_BIT_B1 [simp] = |
463 |
sbintrunc.Z [where bin="w BIT True", simplified bin_last_BIT bin_rest_numeral_simps] |
|
464 |
for w |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
465 |
|
24333 | 466 |
lemmas sbintrunc_0_simps = |
467 |
sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1 |
|
468 |
||
469 |
lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs |
|
470 |
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs |
|
471 |
||
65363 | 472 |
lemma bintrunc_minus: "0 < n \<Longrightarrow> bintrunc (Suc (n - 1)) w = bintrunc n w" |
473 |
by auto |
|
474 |
||
475 |
lemma sbintrunc_minus: "0 < n \<Longrightarrow> sbintrunc (Suc (n - 1)) w = sbintrunc n w" |
|
24333 | 476 |
by auto |
477 |
||
65363 | 478 |
lemmas bintrunc_minus_simps = |
45604 | 479 |
bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans]] |
65363 | 480 |
lemmas sbintrunc_minus_simps = |
45604 | 481 |
sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans]] |
24333 | 482 |
|
65363 | 483 |
lemmas thobini1 = arg_cong [where f = "\<lambda>w. w BIT b"] for b |
24333 | 484 |
|
485 |
lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1] |
|
486 |
lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1] |
|
487 |
||
45604 | 488 |
lemmas bmsts = bintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans]] |
24333 | 489 |
lemmas bintrunc_Pls_minus_I = bmsts(1) |
490 |
lemmas bintrunc_Min_minus_I = bmsts(2) |
|
491 |
lemmas bintrunc_BIT_minus_I = bmsts(3) |
|
492 |
||
65363 | 493 |
lemma bintrunc_Suc_lem: "bintrunc (Suc n) x = y \<Longrightarrow> m = Suc n \<Longrightarrow> bintrunc m x = y" |
24333 | 494 |
by auto |
495 |
||
65363 | 496 |
lemmas bintrunc_Suc_Ialts = |
45604 | 497 |
bintrunc_Min_I [THEN bintrunc_Suc_lem] |
498 |
bintrunc_BIT_I [THEN bintrunc_Suc_lem] |
|
24333 | 499 |
|
500 |
lemmas sbintrunc_BIT_I = trans [OF sbintrunc_Suc_BIT thobini1] |
|
501 |
||
65363 | 502 |
lemmas sbintrunc_Suc_Is = |
45604 | 503 |
sbintrunc_Sucs(1-3) [THEN thobini1 [THEN [2] trans]] |
24333 | 504 |
|
65363 | 505 |
lemmas sbintrunc_Suc_minus_Is = |
45604 | 506 |
sbintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans]] |
24333 | 507 |
|
65363 | 508 |
lemma sbintrunc_Suc_lem: "sbintrunc (Suc n) x = y \<Longrightarrow> m = Suc n \<Longrightarrow> sbintrunc m x = y" |
24333 | 509 |
by auto |
510 |
||
65363 | 511 |
lemmas sbintrunc_Suc_Ialts = |
45604 | 512 |
sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem] |
24333 | 513 |
|
65363 | 514 |
lemma sbintrunc_bintrunc_lt: "m > n \<Longrightarrow> sbintrunc n (bintrunc m w) = sbintrunc n w" |
24333 | 515 |
by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr) |
516 |
||
65363 | 517 |
lemma bintrunc_sbintrunc_le: "m \<le> Suc n \<Longrightarrow> bintrunc m (sbintrunc n w) = bintrunc m w" |
24333 | 518 |
apply (rule bin_eqI) |
519 |
apply (auto simp: nth_sbintr nth_bintr) |
|
520 |
apply (subgoal_tac "x=n", safe, arith+)[1] |
|
521 |
apply (subgoal_tac "x=n", safe, arith+)[1] |
|
522 |
done |
|
523 |
||
524 |
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le] |
|
525 |
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt] |
|
526 |
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l] |
|
65363 | 527 |
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] |
24333 | 528 |
|
65363 | 529 |
lemma bintrunc_sbintrunc' [simp]: "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w" |
24333 | 530 |
by (cases n) (auto simp del: bintrunc.Suc) |
531 |
||
65363 | 532 |
lemma sbintrunc_bintrunc' [simp]: "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w" |
24333 | 533 |
by (cases n) (auto simp del: bintrunc.Suc) |
534 |
||
65363 | 535 |
lemma bin_sbin_eq_iff: "bintrunc (Suc n) x = bintrunc (Suc n) y \<longleftrightarrow> sbintrunc n x = sbintrunc n y" |
24333 | 536 |
apply (rule iffI) |
537 |
apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc]) |
|
538 |
apply simp |
|
539 |
apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc]) |
|
540 |
apply simp |
|
541 |
done |
|
542 |
||
543 |
lemma bin_sbin_eq_iff': |
|
65363 | 544 |
"0 < n \<Longrightarrow> bintrunc n x = bintrunc n y \<longleftrightarrow> sbintrunc (n - 1) x = sbintrunc (n - 1) y" |
24333 | 545 |
by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc) |
546 |
||
547 |
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def] |
|
548 |
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def] |
|
549 |
||
550 |
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l] |
|
551 |
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l] |
|
552 |
||
553 |
(* although bintrunc_minus_simps, if added to default simpset, |
|
554 |
tends to get applied where it's not wanted in developing the theories, |
|
555 |
we get a version for when the word length is given literally *) |
|
556 |
||
65363 | 557 |
lemmas nat_non0_gr = |
45604 | 558 |
trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl] |
24333 | 559 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
560 |
lemma bintrunc_numeral: |
65363 | 561 |
"bintrunc (numeral k) x = bintrunc (pred_numeral k) (bin_rest x) BIT bin_last x" |
47219
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47170
diff
changeset
|
562 |
by (simp add: numeral_eq_Suc) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
563 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
564 |
lemma sbintrunc_numeral: |
65363 | 565 |
"sbintrunc (numeral k) x = sbintrunc (pred_numeral k) (bin_rest x) BIT bin_last x" |
47219
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47170
diff
changeset
|
566 |
by (simp add: numeral_eq_Suc) |
24333 | 567 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
568 |
lemma bintrunc_numeral_simps [simp]: |
65363 | 569 |
"bintrunc (numeral k) (numeral (Num.Bit0 w)) = bintrunc (pred_numeral k) (numeral w) BIT False" |
570 |
"bintrunc (numeral k) (numeral (Num.Bit1 w)) = bintrunc (pred_numeral k) (numeral w) BIT True" |
|
571 |
"bintrunc (numeral k) (- numeral (Num.Bit0 w)) = bintrunc (pred_numeral k) (- numeral w) BIT False" |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
572 |
"bintrunc (numeral k) (- numeral (Num.Bit1 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
573 |
bintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT True" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
574 |
"bintrunc (numeral k) 1 = 1" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
575 |
by (simp_all add: bintrunc_numeral) |
24333 | 576 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
577 |
lemma sbintrunc_numeral_simps [simp]: |
65363 | 578 |
"sbintrunc (numeral k) (numeral (Num.Bit0 w)) = sbintrunc (pred_numeral k) (numeral w) BIT False" |
579 |
"sbintrunc (numeral k) (numeral (Num.Bit1 w)) = sbintrunc (pred_numeral k) (numeral w) BIT True" |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
580 |
"sbintrunc (numeral k) (- numeral (Num.Bit0 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
581 |
sbintrunc (pred_numeral k) (- numeral w) BIT False" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
582 |
"sbintrunc (numeral k) (- numeral (Num.Bit1 w)) = |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
583 |
sbintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT True" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
584 |
"sbintrunc (numeral k) 1 = 1" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
585 |
by (simp_all add: sbintrunc_numeral) |
24333 | 586 |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
587 |
lemma no_bintr_alt1: "bintrunc n = (\<lambda>w. w mod 2 ^ n :: int)" |
24333 | 588 |
by (rule ext) (rule bintrunc_mod2p) |
589 |
||
65363 | 590 |
lemma range_bintrunc: "range (bintrunc n) = {i. 0 \<le> i \<and> i < 2 ^ n}" |
24333 | 591 |
apply (unfold no_bintr_alt1) |
592 |
apply (auto simp add: image_iff) |
|
593 |
apply (rule exI) |
|
70170 | 594 |
apply (rule sym) |
595 |
using int_mod_lem [symmetric, of "2 ^ n"] |
|
596 |
apply auto |
|
24333 | 597 |
done |
598 |
||
65363 | 599 |
lemma no_sbintr_alt2: "sbintrunc n = (\<lambda>w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)" |
24333 | 600 |
by (rule ext) (simp add : sbintrunc_mod2p) |
601 |
||
65363 | 602 |
lemma range_sbintrunc: "range (sbintrunc n) = {i. - (2 ^ n) \<le> i \<and> i < 2 ^ n}" |
24333 | 603 |
apply (unfold no_sbintr_alt2) |
604 |
apply (auto simp add: image_iff eq_diff_eq) |
|
70170 | 605 |
|
24333 | 606 |
apply (rule exI) |
607 |
apply (auto intro: int_mod_lem [THEN iffD1, symmetric]) |
|
608 |
done |
|
609 |
||
65363 | 610 |
lemma sb_inc_lem: "a + 2^k < 0 \<Longrightarrow> a + 2^k + 2^(Suc k) \<le> (a + 2^k) mod 2^(Suc k)" |
611 |
for a :: int |
|
70170 | 612 |
using int_mod_ge' [where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"] |
613 |
by simp |
|
24333 | 614 |
|
65363 | 615 |
lemma sb_inc_lem': "a < - (2^k) \<Longrightarrow> a + 2^k + 2^(Suc k) \<le> (a + 2^k) mod 2^(Suc k)" |
616 |
for a :: int |
|
35048 | 617 |
by (rule sb_inc_lem) simp |
24333 | 618 |
|
65363 | 619 |
lemma sbintrunc_inc: "x < - (2^n) \<Longrightarrow> x + 2^(Suc n) \<le> sbintrunc n x" |
24333 | 620 |
unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp |
621 |
||
65363 | 622 |
lemma sb_dec_lem: "0 \<le> - (2 ^ k) + a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a" |
623 |
for a :: int |
|
54230
b1d955791529
more simplification rules on unary and binary minus
haftmann
parents:
53438
diff
changeset
|
624 |
using int_mod_le'[where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"] by simp |
24333 | 625 |
|
65363 | 626 |
lemma sb_dec_lem': "2 ^ k \<le> a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a" |
627 |
for a :: int |
|
54230
b1d955791529
more simplification rules on unary and binary minus
haftmann
parents:
53438
diff
changeset
|
628 |
by (rule sb_dec_lem) simp |
24333 | 629 |
|
65363 | 630 |
lemma sbintrunc_dec: "x \<ge> (2 ^ n) \<Longrightarrow> x - 2 ^ (Suc n) >= sbintrunc n x" |
24333 | 631 |
unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp |
632 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
633 |
lemma bintr_ge0: "0 \<le> bintrunc n w" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
634 |
by (simp add: bintrunc_mod2p) |
24333 | 635 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
636 |
lemma bintr_lt2p: "bintrunc n w < 2 ^ n" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
637 |
by (simp add: bintrunc_mod2p) |
24333 | 638 |
|
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
54873
diff
changeset
|
639 |
lemma bintr_Min: "bintrunc n (- 1) = 2 ^ n - 1" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
640 |
by (simp add: bintrunc_mod2p m1mod2k) |
24333 | 641 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
642 |
lemma sbintr_ge: "- (2 ^ n) \<le> sbintrunc n w" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
643 |
by (simp add: sbintrunc_mod2p) |
24333 | 644 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
645 |
lemma sbintr_lt: "sbintrunc n w < 2 ^ n" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
646 |
by (simp add: sbintrunc_mod2p) |
24333 | 647 |
|
65363 | 648 |
lemma sign_Pls_ge_0: "bin_sign bin = 0 \<longleftrightarrow> bin \<ge> 0" |
649 |
for bin :: int |
|
650 |
by (simp add: bin_sign_def) |
|
24333 | 651 |
|
65363 | 652 |
lemma sign_Min_lt_0: "bin_sign bin = -1 \<longleftrightarrow> bin < 0" |
653 |
for bin :: int |
|
654 |
by (simp add: bin_sign_def) |
|
24333 | 655 |
|
65363 | 656 |
lemma bin_rest_trunc: "bin_rest (bintrunc n bin) = bintrunc (n - 1) (bin_rest bin)" |
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
657 |
by (induct n arbitrary: bin) auto |
24333 | 658 |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
659 |
lemma bin_rest_power_trunc: |
65363 | 660 |
"(bin_rest ^^ k) (bintrunc n bin) = bintrunc (n - k) ((bin_rest ^^ k) bin)" |
24333 | 661 |
by (induct k) (auto simp: bin_rest_trunc) |
662 |
||
65363 | 663 |
lemma bin_rest_trunc_i: "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)" |
24333 | 664 |
by auto |
665 |
||
65363 | 666 |
lemma bin_rest_strunc: "bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)" |
45954
f67d3bb5f09c
use 'induct arbitrary' instead of universal quantifiers
huffman
parents:
45953
diff
changeset
|
667 |
by (induct n arbitrary: bin) auto |
24333 | 668 |
|
65363 | 669 |
lemma bintrunc_rest [simp]: "bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)" |
670 |
apply (induct n arbitrary: bin) |
|
671 |
apply simp |
|
24333 | 672 |
apply (case_tac bin rule: bin_exhaust) |
673 |
apply (auto simp: bintrunc_bintrunc_l) |
|
674 |
done |
|
675 |
||
65363 | 676 |
lemma sbintrunc_rest [simp]: "sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)" |
677 |
apply (induct n arbitrary: bin) |
|
678 |
apply simp |
|
24333 | 679 |
apply (case_tac bin rule: bin_exhaust) |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
680 |
apply (auto simp: bintrunc_bintrunc_l split: bool.splits) |
24333 | 681 |
done |
682 |
||
65363 | 683 |
lemma bintrunc_rest': "bintrunc n \<circ> bin_rest \<circ> bintrunc n = bin_rest \<circ> bintrunc n" |
24333 | 684 |
by (rule ext) auto |
685 |
||
65363 | 686 |
lemma sbintrunc_rest': "sbintrunc n \<circ> bin_rest \<circ> sbintrunc n = bin_rest \<circ> sbintrunc n" |
24333 | 687 |
by (rule ext) auto |
688 |
||
65363 | 689 |
lemma rco_lem: "f \<circ> g \<circ> f = g \<circ> f \<Longrightarrow> f \<circ> (g \<circ> f) ^^ n = g ^^ n \<circ> f" |
24333 | 690 |
apply (rule ext) |
691 |
apply (induct_tac n) |
|
692 |
apply (simp_all (no_asm)) |
|
693 |
apply (drule fun_cong) |
|
694 |
apply (unfold o_def) |
|
695 |
apply (erule trans) |
|
696 |
apply simp |
|
697 |
done |
|
698 |
||
65363 | 699 |
lemmas rco_bintr = bintrunc_rest' |
24333 | 700 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
65363 | 701 |
lemmas rco_sbintr = sbintrunc_rest' |
24333 | 702 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
703 |
||
65363 | 704 |
|
61799 | 705 |
subsection \<open>Splitting and concatenation\<close> |
24364 | 706 |
|
65363 | 707 |
primrec bin_split :: "nat \<Rightarrow> int \<Rightarrow> int \<times> int" |
708 |
where |
|
709 |
Z: "bin_split 0 w = (w, 0)" |
|
710 |
| Suc: "bin_split (Suc n) w = |
|
711 |
(let (w1, w2) = bin_split n (bin_rest w) |
|
712 |
in (w1, w2 BIT bin_last w))" |
|
24364 | 713 |
|
37667 | 714 |
lemma [code]: |
715 |
"bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w) in (w1, w2 BIT bin_last w))" |
|
716 |
"bin_split 0 w = (w, 0)" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46607
diff
changeset
|
717 |
by simp_all |
37667 | 718 |
|
65363 | 719 |
primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int" |
720 |
where |
|
721 |
Z: "bin_cat w 0 v = w" |
|
26557 | 722 |
| Suc: "bin_cat w (Suc n) v = bin_cat w n (bin_rest v) BIT bin_last v" |
24364 | 723 |
|
70169 | 724 |
lemma bin_sign_cat: "bin_sign (bin_cat x n y) = bin_sign x" |
725 |
by (induct n arbitrary: y) auto |
|
726 |
||
727 |
lemma bin_cat_Suc_Bit: "bin_cat w (Suc n) (v BIT b) = bin_cat w n v BIT b" |
|
728 |
by auto |
|
729 |
||
730 |
lemma bin_cat_assoc: "bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)" |
|
731 |
by (induct n arbitrary: z) auto |
|
732 |
||
733 |
lemma bin_cat_assoc_sym: "bin_cat x m (bin_cat y n z) = bin_cat (bin_cat x (m - n) y) (min m n) z" |
|
734 |
apply (induct n arbitrary: z m) |
|
735 |
apply clarsimp |
|
736 |
apply (case_tac m, auto) |
|
737 |
done |
|
738 |
||
739 |
definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int" |
|
740 |
where "bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0" |
|
741 |
||
742 |
fun bin_rsplit_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" |
|
743 |
where "bin_rsplit_aux n m c bs = |
|
744 |
(if m = 0 \<or> n = 0 then bs |
|
745 |
else |
|
746 |
let (a, b) = bin_split n c |
|
747 |
in bin_rsplit_aux n (m - n) a (b # bs))" |
|
748 |
||
749 |
definition bin_rsplit :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list" |
|
750 |
where "bin_rsplit n w = bin_rsplit_aux n (fst w) (snd w) []" |
|
751 |
||
752 |
fun bin_rsplitl_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" |
|
753 |
where "bin_rsplitl_aux n m c bs = |
|
754 |
(if m = 0 \<or> n = 0 then bs |
|
755 |
else |
|
756 |
let (a, b) = bin_split (min m n) c |
|
757 |
in bin_rsplitl_aux n (m - n) a (b # bs))" |
|
758 |
||
759 |
definition bin_rsplitl :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list" |
|
760 |
where "bin_rsplitl n w = bin_rsplitl_aux n (fst w) (snd w) []" |
|
761 |
||
762 |
declare bin_rsplit_aux.simps [simp del] |
|
763 |
declare bin_rsplitl_aux.simps [simp del] |
|
764 |
||
765 |
lemma bin_nth_cat: |
|
766 |
"bin_nth (bin_cat x k y) n = |
|
767 |
(if n < k then bin_nth y n else bin_nth x (n - k))" |
|
768 |
apply (induct k arbitrary: n y) |
|
769 |
apply clarsimp |
|
770 |
apply (case_tac n, auto) |
|
771 |
done |
|
772 |
||
773 |
lemma bin_nth_split: |
|
774 |
"bin_split n c = (a, b) \<Longrightarrow> |
|
775 |
(\<forall>k. bin_nth a k = bin_nth c (n + k)) \<and> |
|
776 |
(\<forall>k. bin_nth b k = (k < n \<and> bin_nth c k))" |
|
777 |
apply (induct n arbitrary: b c) |
|
778 |
apply clarsimp |
|
779 |
apply (clarsimp simp: Let_def split: prod.split_asm) |
|
780 |
apply (case_tac k) |
|
781 |
apply auto |
|
782 |
done |
|
783 |
||
784 |
lemma bin_cat_zero [simp]: "bin_cat 0 n w = bintrunc n w" |
|
785 |
by (induct n arbitrary: w) auto |
|
786 |
||
787 |
lemma bintr_cat1: "bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b" |
|
788 |
by (induct n arbitrary: b) auto |
|
789 |
||
790 |
lemma bintr_cat: "bintrunc m (bin_cat a n b) = |
|
791 |
bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)" |
|
792 |
by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr) |
|
793 |
||
794 |
lemma bintr_cat_same [simp]: "bintrunc n (bin_cat a n b) = bintrunc n b" |
|
795 |
by (auto simp add : bintr_cat) |
|
796 |
||
797 |
lemma cat_bintr [simp]: "bin_cat a n (bintrunc n b) = bin_cat a n b" |
|
798 |
by (induct n arbitrary: b) auto |
|
799 |
||
800 |
lemma split_bintrunc: "bin_split n c = (a, b) \<Longrightarrow> b = bintrunc n c" |
|
801 |
by (induct n arbitrary: b c) (auto simp: Let_def split: prod.split_asm) |
|
802 |
||
803 |
lemma bin_cat_split: "bin_split n w = (u, v) \<Longrightarrow> w = bin_cat u n v" |
|
804 |
by (induct n arbitrary: v w) (auto simp: Let_def split: prod.split_asm) |
|
805 |
||
806 |
lemma bin_split_cat: "bin_split n (bin_cat v n w) = (v, bintrunc n w)" |
|
807 |
by (induct n arbitrary: w) auto |
|
808 |
||
809 |
lemma bin_split_zero [simp]: "bin_split n 0 = (0, 0)" |
|
810 |
by (induct n) auto |
|
811 |
||
812 |
lemma bin_split_minus1 [simp]: |
|
813 |
"bin_split n (- 1) = (- 1, bintrunc n (- 1))" |
|
814 |
by (induct n) auto |
|
815 |
||
816 |
lemma bin_split_trunc: |
|
817 |
"bin_split (min m n) c = (a, b) \<Longrightarrow> |
|
818 |
bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)" |
|
819 |
apply (induct n arbitrary: m b c, clarsimp) |
|
820 |
apply (simp add: bin_rest_trunc Let_def split: prod.split_asm) |
|
821 |
apply (case_tac m) |
|
822 |
apply (auto simp: Let_def split: prod.split_asm) |
|
823 |
done |
|
824 |
||
825 |
lemma bin_split_trunc1: |
|
826 |
"bin_split n c = (a, b) \<Longrightarrow> |
|
827 |
bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)" |
|
828 |
apply (induct n arbitrary: m b c, clarsimp) |
|
829 |
apply (simp add: bin_rest_trunc Let_def split: prod.split_asm) |
|
830 |
apply (case_tac m) |
|
831 |
apply (auto simp: Let_def split: prod.split_asm) |
|
832 |
done |
|
833 |
||
834 |
lemma bin_cat_num: "bin_cat a n b = a * 2 ^ n + bintrunc n b" |
|
835 |
apply (induct n arbitrary: b) |
|
836 |
apply clarsimp |
|
837 |
apply (simp add: Bit_def) |
|
838 |
done |
|
839 |
||
840 |
lemma bin_split_num: "bin_split n b = (b div 2 ^ n, b mod 2 ^ n)" |
|
841 |
apply (induct n arbitrary: b) |
|
842 |
apply simp |
|
843 |
apply (simp add: bin_rest_def zdiv_zmult2_eq) |
|
844 |
apply (case_tac b rule: bin_exhaust) |
|
845 |
apply simp |
|
70170 | 846 |
apply (simp add: Bit_def mod_mult_mult1 pos_zmod_mult_2 add.commute) |
70169 | 847 |
done |
848 |
||
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
849 |
end |