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