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