author | wenzelm |
Tue, 03 Mar 2009 14:07:43 +0100 | |
changeset 30211 | 556d1810cdad |
parent 30081 | 46b9c8ae3897 |
child 30242 | aea5d7fa7ef5 |
permissions | -rw-r--r-- |
23164 | 1 |
(* Title: HOL/NatBin.thy |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1999 University of Cambridge |
|
5 |
*) |
|
6 |
||
7 |
header {* Binary arithmetic for the natural numbers *} |
|
8 |
||
9 |
theory NatBin |
|
10 |
imports IntDiv |
|
11 |
begin |
|
12 |
||
13 |
text {* |
|
14 |
Arithmetic for naturals is reduced to that for the non-negative integers. |
|
15 |
*} |
|
16 |
||
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25481
diff
changeset
|
17 |
instantiation nat :: number |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25481
diff
changeset
|
18 |
begin |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25481
diff
changeset
|
19 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25481
diff
changeset
|
20 |
definition |
28562 | 21 |
nat_number_of_def [code inline, code del]: "number_of v = nat (number_of v)" |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25481
diff
changeset
|
22 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25481
diff
changeset
|
23 |
instance .. |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25481
diff
changeset
|
24 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25481
diff
changeset
|
25 |
end |
23164 | 26 |
|
25965 | 27 |
lemma [code post]: |
28 |
"nat (number_of v) = number_of v" |
|
29 |
unfolding nat_number_of_def .. |
|
30 |
||
23164 | 31 |
abbreviation (xsymbols) |
29401
94fd5dd918f5
rename abbreviation square -> power2, to match theorem names
huffman
parents:
29045
diff
changeset
|
32 |
power2 :: "'a::power => 'a" ("(_\<twosuperior>)" [1000] 999) where |
23164 | 33 |
"x\<twosuperior> == x^2" |
34 |
||
35 |
notation (latex output) |
|
29401
94fd5dd918f5
rename abbreviation square -> power2, to match theorem names
huffman
parents:
29045
diff
changeset
|
36 |
power2 ("(_\<twosuperior>)" [1000] 999) |
23164 | 37 |
|
38 |
notation (HTML output) |
|
29401
94fd5dd918f5
rename abbreviation square -> power2, to match theorem names
huffman
parents:
29045
diff
changeset
|
39 |
power2 ("(_\<twosuperior>)" [1000] 999) |
23164 | 40 |
|
41 |
||
29040
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
42 |
subsection {* Predicate for negative binary numbers *} |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
43 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
44 |
definition |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
45 |
neg :: "int \<Rightarrow> bool" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
46 |
where |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
47 |
"neg Z \<longleftrightarrow> Z < 0" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
48 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
49 |
lemma not_neg_int [simp]: "~ neg (of_nat n)" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
50 |
by (simp add: neg_def) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
51 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
52 |
lemma neg_zminus_int [simp]: "neg (- (of_nat (Suc n)))" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
53 |
by (simp add: neg_def neg_less_0_iff_less del: of_nat_Suc) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
54 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
55 |
lemmas neg_eq_less_0 = neg_def |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
56 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
57 |
lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
58 |
by (simp add: neg_def linorder_not_less) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
59 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
60 |
text{*To simplify inequalities when Numeral1 can get simplified to 1*} |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
61 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
62 |
lemma not_neg_0: "~ neg 0" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
63 |
by (simp add: One_int_def neg_def) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
64 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
65 |
lemma not_neg_1: "~ neg 1" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
66 |
by (simp add: neg_def linorder_not_less zero_le_one) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
67 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
68 |
lemma neg_nat: "neg z ==> nat z = 0" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
69 |
by (simp add: neg_def order_less_imp_le) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
70 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
71 |
lemma not_neg_nat: "~ neg z ==> of_nat (nat z) = z" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
72 |
by (simp add: linorder_not_less neg_def) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
73 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
74 |
text {* |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
75 |
If @{term Numeral0} is rewritten to 0 then this rule can't be applied: |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
76 |
@{term Numeral0} IS @{term "number_of Pls"} |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
77 |
*} |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
78 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
79 |
lemma not_neg_number_of_Pls: "~ neg (number_of Int.Pls)" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
80 |
by (simp add: neg_def) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
81 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
82 |
lemma neg_number_of_Min: "neg (number_of Int.Min)" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
83 |
by (simp add: neg_def) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
84 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
85 |
lemma neg_number_of_Bit0: |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
86 |
"neg (number_of (Int.Bit0 w)) = neg (number_of w)" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
87 |
by (simp add: neg_def) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
88 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
89 |
lemma neg_number_of_Bit1: |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
90 |
"neg (number_of (Int.Bit1 w)) = neg (number_of w)" |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
91 |
by (simp add: neg_def) |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
92 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
93 |
lemmas neg_simps [simp] = |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
94 |
not_neg_0 not_neg_1 |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
95 |
not_neg_number_of_Pls neg_number_of_Min |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
96 |
neg_number_of_Bit0 neg_number_of_Bit1 |
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
97 |
|
286c669d3a7a
move all neg-related lemmas to NatBin; make type of neg specific to int
huffman
parents:
29039
diff
changeset
|
98 |
|
23164 | 99 |
subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*} |
100 |
||
101 |
declare nat_0 [simp] nat_1 [simp] |
|
102 |
||
103 |
lemma nat_number_of [simp]: "nat (number_of w) = number_of w" |
|
104 |
by (simp add: nat_number_of_def) |
|
105 |
||
106 |
lemma nat_numeral_0_eq_0 [simp]: "Numeral0 = (0::nat)" |
|
107 |
by (simp add: nat_number_of_def) |
|
108 |
||
109 |
lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)" |
|
110 |
by (simp add: nat_1 nat_number_of_def) |
|
111 |
||
112 |
lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0" |
|
113 |
by (simp add: nat_numeral_1_eq_1) |
|
114 |
||
115 |
lemma numeral_2_eq_2: "2 = Suc (Suc 0)" |
|
116 |
apply (unfold nat_number_of_def) |
|
117 |
apply (rule nat_2) |
|
118 |
done |
|
119 |
||
120 |
||
121 |
subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*} |
|
122 |
||
123 |
lemma int_nat_number_of [simp]: |
|
23365 | 124 |
"int (number_of v) = |
23307
2fe3345035c7
modify proofs to avoid referring to int::nat=>int
huffman
parents:
23294
diff
changeset
|
125 |
(if neg (number_of v :: int) then 0 |
2fe3345035c7
modify proofs to avoid referring to int::nat=>int
huffman
parents:
23294
diff
changeset
|
126 |
else (number_of v :: int))" |
28984 | 127 |
unfolding nat_number_of_def number_of_is_id neg_def |
128 |
by simp |
|
23307
2fe3345035c7
modify proofs to avoid referring to int::nat=>int
huffman
parents:
23294
diff
changeset
|
129 |
|
23164 | 130 |
|
131 |
subsubsection{*Successor *} |
|
132 |
||
133 |
lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)" |
|
134 |
apply (rule sym) |
|
135 |
apply (simp add: nat_eq_iff int_Suc) |
|
136 |
done |
|
137 |
||
138 |
lemma Suc_nat_number_of_add: |
|
139 |
"Suc (number_of v + n) = |
|
28984 | 140 |
(if neg (number_of v :: int) then 1+n else number_of (Int.succ v) + n)" |
141 |
unfolding nat_number_of_def number_of_is_id neg_def numeral_simps |
|
142 |
by (simp add: Suc_nat_eq_nat_zadd1 add_ac) |
|
23164 | 143 |
|
144 |
lemma Suc_nat_number_of [simp]: |
|
145 |
"Suc (number_of v) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
146 |
(if neg (number_of v :: int) then 1 else number_of (Int.succ v))" |
23164 | 147 |
apply (cut_tac n = 0 in Suc_nat_number_of_add) |
148 |
apply (simp cong del: if_weak_cong) |
|
149 |
done |
|
150 |
||
151 |
||
152 |
subsubsection{*Addition *} |
|
153 |
||
154 |
lemma add_nat_number_of [simp]: |
|
155 |
"(number_of v :: nat) + number_of v' = |
|
29012 | 156 |
(if v < Int.Pls then number_of v' |
157 |
else if v' < Int.Pls then number_of v |
|
23164 | 158 |
else number_of (v + v'))" |
29012 | 159 |
unfolding nat_number_of_def number_of_is_id numeral_simps |
28984 | 160 |
by (simp add: nat_add_distrib) |
23164 | 161 |
|
30081 | 162 |
lemma nat_number_of_add_1 [simp]: |
163 |
"number_of v + (1::nat) = |
|
164 |
(if v < Int.Pls then 1 else number_of (Int.succ v))" |
|
165 |
unfolding nat_number_of_def number_of_is_id numeral_simps |
|
166 |
by (simp add: nat_add_distrib) |
|
167 |
||
168 |
lemma nat_1_add_number_of [simp]: |
|
169 |
"(1::nat) + number_of v = |
|
170 |
(if v < Int.Pls then 1 else number_of (Int.succ v))" |
|
171 |
unfolding nat_number_of_def number_of_is_id numeral_simps |
|
172 |
by (simp add: nat_add_distrib) |
|
173 |
||
174 |
lemma nat_1_add_1 [simp]: "1 + 1 = (2::nat)" |
|
175 |
by (rule int_int_eq [THEN iffD1]) simp |
|
176 |
||
23164 | 177 |
|
178 |
subsubsection{*Subtraction *} |
|
179 |
||
180 |
lemma diff_nat_eq_if: |
|
181 |
"nat z - nat z' = |
|
182 |
(if neg z' then nat z |
|
183 |
else let d = z-z' in |
|
184 |
if neg d then 0 else nat d)" |
|
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
26342
diff
changeset
|
185 |
by (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0) |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
26342
diff
changeset
|
186 |
|
23164 | 187 |
|
188 |
lemma diff_nat_number_of [simp]: |
|
189 |
"(number_of v :: nat) - number_of v' = |
|
29012 | 190 |
(if v' < Int.Pls then number_of v |
23164 | 191 |
else let d = number_of (v + uminus v') in |
192 |
if neg d then 0 else nat d)" |
|
29012 | 193 |
unfolding nat_number_of_def number_of_is_id numeral_simps neg_def |
194 |
by auto |
|
23164 | 195 |
|
30081 | 196 |
lemma nat_number_of_diff_1 [simp]: |
197 |
"number_of v - (1::nat) = |
|
198 |
(if v \<le> Int.Pls then 0 else number_of (Int.pred v))" |
|
199 |
unfolding nat_number_of_def number_of_is_id numeral_simps |
|
200 |
by auto |
|
201 |
||
23164 | 202 |
|
203 |
subsubsection{*Multiplication *} |
|
204 |
||
205 |
lemma mult_nat_number_of [simp]: |
|
206 |
"(number_of v :: nat) * number_of v' = |
|
29012 | 207 |
(if v < Int.Pls then 0 else number_of (v * v'))" |
208 |
unfolding nat_number_of_def number_of_is_id numeral_simps |
|
28984 | 209 |
by (simp add: nat_mult_distrib) |
23164 | 210 |
|
211 |
||
212 |
subsubsection{*Quotient *} |
|
213 |
||
214 |
lemma div_nat_number_of [simp]: |
|
215 |
"(number_of v :: nat) div number_of v' = |
|
216 |
(if neg (number_of v :: int) then 0 |
|
217 |
else nat (number_of v div number_of v'))" |
|
28984 | 218 |
unfolding nat_number_of_def number_of_is_id neg_def |
219 |
by (simp add: nat_div_distrib) |
|
23164 | 220 |
|
221 |
lemma one_div_nat_number_of [simp]: |
|
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
26342
diff
changeset
|
222 |
"Suc 0 div number_of v' = nat (1 div number_of v')" |
23164 | 223 |
by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) |
224 |
||
225 |
||
226 |
subsubsection{*Remainder *} |
|
227 |
||
228 |
lemma mod_nat_number_of [simp]: |
|
229 |
"(number_of v :: nat) mod number_of v' = |
|
230 |
(if neg (number_of v :: int) then 0 |
|
231 |
else if neg (number_of v' :: int) then number_of v |
|
232 |
else nat (number_of v mod number_of v'))" |
|
28984 | 233 |
unfolding nat_number_of_def number_of_is_id neg_def |
234 |
by (simp add: nat_mod_distrib) |
|
23164 | 235 |
|
236 |
lemma one_mod_nat_number_of [simp]: |
|
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
26342
diff
changeset
|
237 |
"Suc 0 mod number_of v' = |
23164 | 238 |
(if neg (number_of v' :: int) then Suc 0 |
239 |
else nat (1 mod number_of v'))" |
|
240 |
by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) |
|
241 |
||
242 |
||
243 |
subsubsection{* Divisibility *} |
|
244 |
||
245 |
lemmas dvd_eq_mod_eq_0_number_of = |
|
246 |
dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard] |
|
247 |
||
248 |
declare dvd_eq_mod_eq_0_number_of [simp] |
|
249 |
||
250 |
ML |
|
251 |
{* |
|
252 |
val nat_number_of_def = thm"nat_number_of_def"; |
|
253 |
||
254 |
val nat_number_of = thm"nat_number_of"; |
|
255 |
val nat_numeral_0_eq_0 = thm"nat_numeral_0_eq_0"; |
|
256 |
val nat_numeral_1_eq_1 = thm"nat_numeral_1_eq_1"; |
|
257 |
val numeral_1_eq_Suc_0 = thm"numeral_1_eq_Suc_0"; |
|
258 |
val numeral_2_eq_2 = thm"numeral_2_eq_2"; |
|
259 |
val nat_div_distrib = thm"nat_div_distrib"; |
|
260 |
val nat_mod_distrib = thm"nat_mod_distrib"; |
|
261 |
val int_nat_number_of = thm"int_nat_number_of"; |
|
262 |
val Suc_nat_eq_nat_zadd1 = thm"Suc_nat_eq_nat_zadd1"; |
|
263 |
val Suc_nat_number_of_add = thm"Suc_nat_number_of_add"; |
|
264 |
val Suc_nat_number_of = thm"Suc_nat_number_of"; |
|
265 |
val add_nat_number_of = thm"add_nat_number_of"; |
|
266 |
val diff_nat_eq_if = thm"diff_nat_eq_if"; |
|
267 |
val diff_nat_number_of = thm"diff_nat_number_of"; |
|
268 |
val mult_nat_number_of = thm"mult_nat_number_of"; |
|
269 |
val div_nat_number_of = thm"div_nat_number_of"; |
|
270 |
val mod_nat_number_of = thm"mod_nat_number_of"; |
|
271 |
*} |
|
272 |
||
273 |
||
274 |
subsection{*Comparisons*} |
|
275 |
||
276 |
subsubsection{*Equals (=) *} |
|
277 |
||
278 |
lemma eq_nat_nat_iff: |
|
279 |
"[| (0::int) <= z; 0 <= z' |] ==> (nat z = nat z') = (z=z')" |
|
280 |
by (auto elim!: nonneg_eq_int) |
|
281 |
||
282 |
lemma eq_nat_number_of [simp]: |
|
283 |
"((number_of v :: nat) = number_of v') = |
|
28969 | 284 |
(if neg (number_of v :: int) then (number_of v' :: int) \<le> 0 |
285 |
else if neg (number_of v' :: int) then (number_of v :: int) = 0 |
|
286 |
else v = v')" |
|
287 |
unfolding nat_number_of_def number_of_is_id neg_def |
|
288 |
by auto |
|
23164 | 289 |
|
290 |
||
291 |
subsubsection{*Less-than (<) *} |
|
292 |
||
293 |
lemma less_nat_number_of [simp]: |
|
29011 | 294 |
"(number_of v :: nat) < number_of v' \<longleftrightarrow> |
295 |
(if v < v' then Int.Pls < v' else False)" |
|
296 |
unfolding nat_number_of_def number_of_is_id numeral_simps |
|
28961 | 297 |
by auto |
23164 | 298 |
|
299 |
||
29010 | 300 |
subsubsection{*Less-than-or-equal *} |
301 |
||
302 |
lemma le_nat_number_of [simp]: |
|
303 |
"(number_of v :: nat) \<le> number_of v' \<longleftrightarrow> |
|
304 |
(if v \<le> v' then True else v \<le> Int.Pls)" |
|
305 |
unfolding nat_number_of_def number_of_is_id numeral_simps |
|
306 |
by auto |
|
307 |
||
23164 | 308 |
(*Maps #n to n for n = 0, 1, 2*) |
309 |
lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2 |
|
310 |
||
311 |
||
312 |
subsection{*Powers with Numeric Exponents*} |
|
313 |
||
314 |
text{*We cannot refer to the number @{term 2} in @{text Ring_and_Field.thy}. |
|
315 |
We cannot prove general results about the numeral @{term "-1"}, so we have to |
|
316 |
use @{term "- 1"} instead.*} |
|
317 |
||
23277 | 318 |
lemma power2_eq_square: "(a::'a::recpower)\<twosuperior> = a * a" |
23164 | 319 |
by (simp add: numeral_2_eq_2 Power.power_Suc) |
320 |
||
23277 | 321 |
lemma zero_power2 [simp]: "(0::'a::{semiring_1,recpower})\<twosuperior> = 0" |
23164 | 322 |
by (simp add: power2_eq_square) |
323 |
||
23277 | 324 |
lemma one_power2 [simp]: "(1::'a::{semiring_1,recpower})\<twosuperior> = 1" |
23164 | 325 |
by (simp add: power2_eq_square) |
326 |
||
327 |
lemma power3_eq_cube: "(x::'a::recpower) ^ 3 = x * x * x" |
|
328 |
apply (subgoal_tac "3 = Suc (Suc (Suc 0))") |
|
329 |
apply (erule ssubst) |
|
330 |
apply (simp add: power_Suc mult_ac) |
|
331 |
apply (unfold nat_number_of_def) |
|
332 |
apply (subst nat_eq_iff) |
|
333 |
apply simp |
|
334 |
done |
|
335 |
||
336 |
text{*Squares of literal numerals will be evaluated.*} |
|
337 |
lemmas power2_eq_square_number_of = |
|
338 |
power2_eq_square [of "number_of w", standard] |
|
339 |
declare power2_eq_square_number_of [simp] |
|
340 |
||
341 |
||
342 |
lemma zero_le_power2[simp]: "0 \<le> (a\<twosuperior>::'a::{ordered_idom,recpower})" |
|
343 |
by (simp add: power2_eq_square) |
|
344 |
||
345 |
lemma zero_less_power2[simp]: |
|
346 |
"(0 < a\<twosuperior>) = (a \<noteq> (0::'a::{ordered_idom,recpower}))" |
|
347 |
by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff) |
|
348 |
||
349 |
lemma power2_less_0[simp]: |
|
350 |
fixes a :: "'a::{ordered_idom,recpower}" |
|
351 |
shows "~ (a\<twosuperior> < 0)" |
|
352 |
by (force simp add: power2_eq_square mult_less_0_iff) |
|
353 |
||
354 |
lemma zero_eq_power2[simp]: |
|
355 |
"(a\<twosuperior> = 0) = (a = (0::'a::{ordered_idom,recpower}))" |
|
356 |
by (force simp add: power2_eq_square mult_eq_0_iff) |
|
357 |
||
358 |
lemma abs_power2[simp]: |
|
359 |
"abs(a\<twosuperior>) = (a\<twosuperior>::'a::{ordered_idom,recpower})" |
|
360 |
by (simp add: power2_eq_square abs_mult abs_mult_self) |
|
361 |
||
362 |
lemma power2_abs[simp]: |
|
363 |
"(abs a)\<twosuperior> = (a\<twosuperior>::'a::{ordered_idom,recpower})" |
|
364 |
by (simp add: power2_eq_square abs_mult_self) |
|
365 |
||
366 |
lemma power2_minus[simp]: |
|
367 |
"(- a)\<twosuperior> = (a\<twosuperior>::'a::{comm_ring_1,recpower})" |
|
368 |
by (simp add: power2_eq_square) |
|
369 |
||
370 |
lemma power2_le_imp_le: |
|
371 |
fixes x y :: "'a::{ordered_semidom,recpower}" |
|
372 |
shows "\<lbrakk>x\<twosuperior> \<le> y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x \<le> y" |
|
373 |
unfolding numeral_2_eq_2 by (rule power_le_imp_le_base) |
|
374 |
||
375 |
lemma power2_less_imp_less: |
|
376 |
fixes x y :: "'a::{ordered_semidom,recpower}" |
|
377 |
shows "\<lbrakk>x\<twosuperior> < y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x < y" |
|
378 |
by (rule power_less_imp_less_base) |
|
379 |
||
380 |
lemma power2_eq_imp_eq: |
|
381 |
fixes x y :: "'a::{ordered_semidom,recpower}" |
|
382 |
shows "\<lbrakk>x\<twosuperior> = y\<twosuperior>; 0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> x = y" |
|
383 |
unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base, simp) |
|
384 |
||
385 |
lemma power_minus1_even[simp]: "(- 1) ^ (2*n) = (1::'a::{comm_ring_1,recpower})" |
|
29958 | 386 |
proof (induct n) |
387 |
case 0 show ?case by simp |
|
388 |
next |
|
389 |
case (Suc n) then show ?case by (simp add: power_Suc power_add) |
|
390 |
qed |
|
391 |
||
392 |
lemma power_minus1_odd: "(- 1) ^ Suc(2*n) = -(1::'a::{comm_ring_1,recpower})" |
|
393 |
by (simp add: power_Suc) |
|
23164 | 394 |
|
395 |
lemma power_even_eq: "(a::'a::recpower) ^ (2*n) = (a^n)^2" |
|
396 |
by (subst mult_commute) (simp add: power_mult) |
|
397 |
||
398 |
lemma power_odd_eq: "(a::int) ^ Suc(2*n) = a * (a^n)^2" |
|
399 |
by (simp add: power_even_eq) |
|
400 |
||
401 |
lemma power_minus_even [simp]: |
|
402 |
"(-a) ^ (2*n) = (a::'a::{comm_ring_1,recpower}) ^ (2*n)" |
|
403 |
by (simp add: power_minus1_even power_minus [of a]) |
|
404 |
||
405 |
lemma zero_le_even_power'[simp]: |
|
406 |
"0 \<le> (a::'a::{ordered_idom,recpower}) ^ (2*n)" |
|
407 |
proof (induct "n") |
|
408 |
case 0 |
|
409 |
show ?case by (simp add: zero_le_one) |
|
410 |
next |
|
411 |
case (Suc n) |
|
412 |
have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" |
|
413 |
by (simp add: mult_ac power_add power2_eq_square) |
|
414 |
thus ?case |
|
415 |
by (simp add: prems zero_le_mult_iff) |
|
416 |
qed |
|
417 |
||
418 |
lemma odd_power_less_zero: |
|
419 |
"(a::'a::{ordered_idom,recpower}) < 0 ==> a ^ Suc(2*n) < 0" |
|
420 |
proof (induct "n") |
|
421 |
case 0 |
|
23389 | 422 |
then show ?case by (simp add: Power.power_Suc) |
23164 | 423 |
next |
424 |
case (Suc n) |
|
23389 | 425 |
have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)" |
426 |
by (simp add: mult_ac power_add power2_eq_square Power.power_Suc) |
|
427 |
thus ?case |
|
428 |
by (simp add: prems mult_less_0_iff mult_neg_neg) |
|
23164 | 429 |
qed |
430 |
||
431 |
lemma odd_0_le_power_imp_0_le: |
|
432 |
"0 \<le> a ^ Suc(2*n) ==> 0 \<le> (a::'a::{ordered_idom,recpower})" |
|
433 |
apply (insert odd_power_less_zero [of a n]) |
|
434 |
apply (force simp add: linorder_not_less [symmetric]) |
|
435 |
done |
|
436 |
||
437 |
text{*Simprules for comparisons where common factors can be cancelled.*} |
|
438 |
lemmas zero_compare_simps = |
|
439 |
add_strict_increasing add_strict_increasing2 add_increasing |
|
440 |
zero_le_mult_iff zero_le_divide_iff |
|
441 |
zero_less_mult_iff zero_less_divide_iff |
|
442 |
mult_le_0_iff divide_le_0_iff |
|
443 |
mult_less_0_iff divide_less_0_iff |
|
444 |
zero_le_power2 power2_less_0 |
|
445 |
||
446 |
subsubsection{*Nat *} |
|
447 |
||
448 |
lemma Suc_pred': "0 < n ==> n = Suc(n - 1)" |
|
449 |
by (simp add: numerals) |
|
450 |
||
451 |
(*Expresses a natural number constant as the Suc of another one. |
|
452 |
NOT suitable for rewriting because n recurs in the condition.*) |
|
453 |
lemmas expand_Suc = Suc_pred' [of "number_of v", standard] |
|
454 |
||
455 |
subsubsection{*Arith *} |
|
456 |
||
457 |
lemma Suc_eq_add_numeral_1: "Suc n = n + 1" |
|
458 |
by (simp add: numerals) |
|
459 |
||
460 |
lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n" |
|
461 |
by (simp add: numerals) |
|
462 |
||
463 |
(* These two can be useful when m = number_of... *) |
|
464 |
||
465 |
lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))" |
|
30079
293b896b9c25
make proofs work whether or not One_nat_def is a simp rule; replace 1 with Suc 0 in the rhs of some simp rules
huffman
parents:
29958
diff
changeset
|
466 |
unfolding One_nat_def by (cases m) simp_all |
23164 | 467 |
|
468 |
lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))" |
|
30079
293b896b9c25
make proofs work whether or not One_nat_def is a simp rule; replace 1 with Suc 0 in the rhs of some simp rules
huffman
parents:
29958
diff
changeset
|
469 |
unfolding One_nat_def by (cases m) simp_all |
23164 | 470 |
|
471 |
lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))" |
|
30079
293b896b9c25
make proofs work whether or not One_nat_def is a simp rule; replace 1 with Suc 0 in the rhs of some simp rules
huffman
parents:
29958
diff
changeset
|
472 |
unfolding One_nat_def by (cases m) simp_all |
23164 | 473 |
|
474 |
||
475 |
subsection{*Comparisons involving (0::nat) *} |
|
476 |
||
477 |
text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*} |
|
478 |
||
479 |
lemma eq_number_of_0 [simp]: |
|
29012 | 480 |
"number_of v = (0::nat) \<longleftrightarrow> v \<le> Int.Pls" |
481 |
unfolding nat_number_of_def number_of_is_id numeral_simps |
|
482 |
by auto |
|
23164 | 483 |
|
484 |
lemma eq_0_number_of [simp]: |
|
29012 | 485 |
"(0::nat) = number_of v \<longleftrightarrow> v \<le> Int.Pls" |
23164 | 486 |
by (rule trans [OF eq_sym_conv eq_number_of_0]) |
487 |
||
488 |
lemma less_0_number_of [simp]: |
|
29012 | 489 |
"(0::nat) < number_of v \<longleftrightarrow> Int.Pls < v" |
490 |
unfolding nat_number_of_def number_of_is_id numeral_simps |
|
491 |
by simp |
|
23164 | 492 |
|
493 |
lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)" |
|
28969 | 494 |
by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric]) |
23164 | 495 |
|
496 |
||
497 |
||
498 |
subsection{*Comparisons involving @{term Suc} *} |
|
499 |
||
500 |
lemma eq_number_of_Suc [simp]: |
|
501 |
"(number_of v = Suc n) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
502 |
(let pv = number_of (Int.pred v) in |
23164 | 503 |
if neg pv then False else nat pv = n)" |
504 |
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less |
|
505 |
number_of_pred nat_number_of_def |
|
506 |
split add: split_if) |
|
507 |
apply (rule_tac x = "number_of v" in spec) |
|
508 |
apply (auto simp add: nat_eq_iff) |
|
509 |
done |
|
510 |
||
511 |
lemma Suc_eq_number_of [simp]: |
|
512 |
"(Suc n = number_of v) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
513 |
(let pv = number_of (Int.pred v) in |
23164 | 514 |
if neg pv then False else nat pv = n)" |
515 |
by (rule trans [OF eq_sym_conv eq_number_of_Suc]) |
|
516 |
||
517 |
lemma less_number_of_Suc [simp]: |
|
518 |
"(number_of v < Suc n) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
519 |
(let pv = number_of (Int.pred v) in |
23164 | 520 |
if neg pv then True else nat pv < n)" |
521 |
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less |
|
522 |
number_of_pred nat_number_of_def |
|
523 |
split add: split_if) |
|
524 |
apply (rule_tac x = "number_of v" in spec) |
|
525 |
apply (auto simp add: nat_less_iff) |
|
526 |
done |
|
527 |
||
528 |
lemma less_Suc_number_of [simp]: |
|
529 |
"(Suc n < number_of v) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
530 |
(let pv = number_of (Int.pred v) in |
23164 | 531 |
if neg pv then False else n < nat pv)" |
532 |
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less |
|
533 |
number_of_pred nat_number_of_def |
|
534 |
split add: split_if) |
|
535 |
apply (rule_tac x = "number_of v" in spec) |
|
536 |
apply (auto simp add: zless_nat_eq_int_zless) |
|
537 |
done |
|
538 |
||
539 |
lemma le_number_of_Suc [simp]: |
|
540 |
"(number_of v <= Suc n) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
541 |
(let pv = number_of (Int.pred v) in |
23164 | 542 |
if neg pv then True else nat pv <= n)" |
543 |
by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric]) |
|
544 |
||
545 |
lemma le_Suc_number_of [simp]: |
|
546 |
"(Suc n <= number_of v) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
547 |
(let pv = number_of (Int.pred v) in |
23164 | 548 |
if neg pv then False else n <= nat pv)" |
549 |
by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric]) |
|
550 |
||
551 |
||
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
552 |
lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Int.Min" |
23164 | 553 |
by auto |
554 |
||
555 |
||
556 |
||
557 |
subsection{*Max and Min Combined with @{term Suc} *} |
|
558 |
||
559 |
lemma max_number_of_Suc [simp]: |
|
560 |
"max (Suc n) (number_of v) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
561 |
(let pv = number_of (Int.pred v) in |
23164 | 562 |
if neg pv then Suc n else Suc(max n (nat pv)))" |
563 |
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def |
|
564 |
split add: split_if nat.split) |
|
565 |
apply (rule_tac x = "number_of v" in spec) |
|
566 |
apply auto |
|
567 |
done |
|
568 |
||
569 |
lemma max_Suc_number_of [simp]: |
|
570 |
"max (number_of v) (Suc n) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
571 |
(let pv = number_of (Int.pred v) in |
23164 | 572 |
if neg pv then Suc n else Suc(max (nat pv) n))" |
573 |
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def |
|
574 |
split add: split_if nat.split) |
|
575 |
apply (rule_tac x = "number_of v" in spec) |
|
576 |
apply auto |
|
577 |
done |
|
578 |
||
579 |
lemma min_number_of_Suc [simp]: |
|
580 |
"min (Suc n) (number_of v) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
581 |
(let pv = number_of (Int.pred v) in |
23164 | 582 |
if neg pv then 0 else Suc(min n (nat pv)))" |
583 |
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def |
|
584 |
split add: split_if nat.split) |
|
585 |
apply (rule_tac x = "number_of v" in spec) |
|
586 |
apply auto |
|
587 |
done |
|
588 |
||
589 |
lemma min_Suc_number_of [simp]: |
|
590 |
"min (number_of v) (Suc n) = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
591 |
(let pv = number_of (Int.pred v) in |
23164 | 592 |
if neg pv then 0 else Suc(min (nat pv) n))" |
593 |
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def |
|
594 |
split add: split_if nat.split) |
|
595 |
apply (rule_tac x = "number_of v" in spec) |
|
596 |
apply auto |
|
597 |
done |
|
598 |
||
599 |
subsection{*Literal arithmetic involving powers*} |
|
600 |
||
601 |
lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n" |
|
602 |
apply (induct "n") |
|
603 |
apply (simp_all (no_asm_simp) add: nat_mult_distrib) |
|
604 |
done |
|
605 |
||
606 |
lemma power_nat_number_of: |
|
607 |
"(number_of v :: nat) ^ n = |
|
608 |
(if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))" |
|
609 |
by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq |
|
610 |
split add: split_if cong: imp_cong) |
|
611 |
||
612 |
||
613 |
lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard] |
|
614 |
declare power_nat_number_of_number_of [simp] |
|
615 |
||
616 |
||
617 |
||
23294 | 618 |
text{*For arbitrary rings*} |
23164 | 619 |
|
23294 | 620 |
lemma power_number_of_even: |
621 |
fixes z :: "'a::{number_ring,recpower}" |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25965
diff
changeset
|
622 |
shows "z ^ number_of (Int.Bit0 w) = (let w = z ^ (number_of w) in w * w)" |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25965
diff
changeset
|
623 |
unfolding Let_def nat_number_of_def number_of_Bit0 |
23164 | 624 |
apply (rule_tac x = "number_of w" in spec, clarify) |
625 |
apply (case_tac " (0::int) <= x") |
|
626 |
apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square) |
|
627 |
done |
|
628 |
||
23294 | 629 |
lemma power_number_of_odd: |
630 |
fixes z :: "'a::{number_ring,recpower}" |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25965
diff
changeset
|
631 |
shows "z ^ number_of (Int.Bit1 w) = (if (0::int) <= number_of w |
23164 | 632 |
then (let w = z ^ (number_of w) in z * w * w) else 1)" |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25965
diff
changeset
|
633 |
unfolding Let_def nat_number_of_def number_of_Bit1 |
23164 | 634 |
apply (rule_tac x = "number_of w" in spec, auto) |
635 |
apply (simp only: nat_add_distrib nat_mult_distrib) |
|
636 |
apply simp |
|
23294 | 637 |
apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat power_Suc) |
23164 | 638 |
done |
639 |
||
23294 | 640 |
lemmas zpower_number_of_even = power_number_of_even [where 'a=int] |
641 |
lemmas zpower_number_of_odd = power_number_of_odd [where 'a=int] |
|
23164 | 642 |
|
23294 | 643 |
lemmas power_number_of_even_number_of [simp] = |
644 |
power_number_of_even [of "number_of v", standard] |
|
23164 | 645 |
|
23294 | 646 |
lemmas power_number_of_odd_number_of [simp] = |
647 |
power_number_of_odd [of "number_of v", standard] |
|
23164 | 648 |
|
649 |
||
650 |
||
651 |
ML |
|
652 |
{* |
|
26342 | 653 |
val numeral_ss = @{simpset} addsimps @{thms numerals}; |
23164 | 654 |
|
655 |
val nat_bin_arith_setup = |
|
24093 | 656 |
LinArith.map_data |
23164 | 657 |
(fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} => |
658 |
{add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, |
|
659 |
inj_thms = inj_thms, |
|
660 |
lessD = lessD, neqE = neqE, |
|
29039 | 661 |
simpset = simpset addsimps @{thms neg_simps} @ |
662 |
[@{thm Suc_nat_number_of}, @{thm int_nat_number_of}]}) |
|
23164 | 663 |
*} |
664 |
||
24075 | 665 |
declaration {* K nat_bin_arith_setup *} |
23164 | 666 |
|
667 |
(* Enable arith to deal with div/mod k where k is a numeral: *) |
|
668 |
declare split_div[of _ _ "number_of k", standard, arith_split] |
|
669 |
declare split_mod[of _ _ "number_of k", standard, arith_split] |
|
670 |
||
671 |
lemma nat_number_of_Pls: "Numeral0 = (0::nat)" |
|
672 |
by (simp add: number_of_Pls nat_number_of_def) |
|
673 |
||
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25571
diff
changeset
|
674 |
lemma nat_number_of_Min: "number_of Int.Min = (0::nat)" |
23164 | 675 |
apply (simp only: number_of_Min nat_number_of_def nat_zminus_int) |
676 |
done |
|
677 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25965
diff
changeset
|
678 |
lemma nat_number_of_Bit0: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25965
diff
changeset
|
679 |
"number_of (Int.Bit0 w) = (let n::nat = number_of w in n + n)" |
28969 | 680 |
unfolding nat_number_of_def number_of_is_id numeral_simps Let_def |
681 |
by auto |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25965
diff
changeset
|
682 |
|
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25965
diff
changeset
|
683 |
lemma nat_number_of_Bit1: |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25965
diff
changeset
|
684 |
"number_of (Int.Bit1 w) = |
23164 | 685 |
(if neg (number_of w :: int) then 0 |
686 |
else let n = number_of w in Suc (n + n))" |
|
28969 | 687 |
unfolding nat_number_of_def number_of_is_id numeral_simps neg_def Let_def |
28968 | 688 |
by auto |
23164 | 689 |
|
690 |
lemmas nat_number = |
|
691 |
nat_number_of_Pls nat_number_of_Min |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25965
diff
changeset
|
692 |
nat_number_of_Bit0 nat_number_of_Bit1 |
23164 | 693 |
|
694 |
lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)" |
|
695 |
by (simp add: Let_def) |
|
696 |
||
697 |
lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring,recpower})" |
|
23294 | 698 |
by (simp add: power_mult power_Suc); |
23164 | 699 |
|
700 |
lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring,recpower})" |
|
701 |
by (simp add: power_mult power_Suc); |
|
702 |
||
703 |
||
704 |
subsection{*Literal arithmetic and @{term of_nat}*} |
|
705 |
||
706 |
lemma of_nat_double: |
|
707 |
"0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)" |
|
708 |
by (simp only: mult_2 nat_add_distrib of_nat_add) |
|
709 |
||
710 |
lemma nat_numeral_m1_eq_0: "-1 = (0::nat)" |
|
711 |
by (simp only: nat_number_of_def) |
|
712 |
||
713 |
lemma of_nat_number_of_lemma: |
|
714 |
"of_nat (number_of v :: nat) = |
|
715 |
(if 0 \<le> (number_of v :: int) |
|
716 |
then (number_of v :: 'a :: number_ring) |
|
717 |
else 0)" |
|
718 |
by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat); |
|
719 |
||
720 |
lemma of_nat_number_of_eq [simp]: |
|
721 |
"of_nat (number_of v :: nat) = |
|
722 |
(if neg (number_of v :: int) then 0 |
|
723 |
else (number_of v :: 'a :: number_ring))" |
|
724 |
by (simp only: of_nat_number_of_lemma neg_def, simp) |
|
725 |
||
726 |
||
727 |
subsection {*Lemmas for the Combination and Cancellation Simprocs*} |
|
728 |
||
729 |
lemma nat_number_of_add_left: |
|
730 |
"number_of v + (number_of v' + (k::nat)) = |
|
731 |
(if neg (number_of v :: int) then number_of v' + k |
|
732 |
else if neg (number_of v' :: int) then number_of v + k |
|
733 |
else number_of (v + v') + k)" |
|
28968 | 734 |
unfolding nat_number_of_def number_of_is_id neg_def |
735 |
by auto |
|
23164 | 736 |
|
737 |
lemma nat_number_of_mult_left: |
|
738 |
"number_of v * (number_of v' * (k::nat)) = |
|
29012 | 739 |
(if v < Int.Pls then 0 |
23164 | 740 |
else number_of (v * v') * k)" |
741 |
by simp |
|
742 |
||
743 |
||
744 |
subsubsection{*For @{text combine_numerals}*} |
|
745 |
||
746 |
lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)" |
|
747 |
by (simp add: add_mult_distrib) |
|
748 |
||
749 |
||
750 |
subsubsection{*For @{text cancel_numerals}*} |
|
751 |
||
752 |
lemma nat_diff_add_eq1: |
|
753 |
"j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)" |
|
754 |
by (simp split add: nat_diff_split add: add_mult_distrib) |
|
755 |
||
756 |
lemma nat_diff_add_eq2: |
|
757 |
"i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))" |
|
758 |
by (simp split add: nat_diff_split add: add_mult_distrib) |
|
759 |
||
760 |
lemma nat_eq_add_iff1: |
|
761 |
"j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)" |
|
762 |
by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
763 |
||
764 |
lemma nat_eq_add_iff2: |
|
765 |
"i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)" |
|
766 |
by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
767 |
||
768 |
lemma nat_less_add_iff1: |
|
769 |
"j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)" |
|
770 |
by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
771 |
||
772 |
lemma nat_less_add_iff2: |
|
773 |
"i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)" |
|
774 |
by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
775 |
||
776 |
lemma nat_le_add_iff1: |
|
777 |
"j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)" |
|
778 |
by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
779 |
||
780 |
lemma nat_le_add_iff2: |
|
781 |
"i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)" |
|
782 |
by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
783 |
||
784 |
||
785 |
subsubsection{*For @{text cancel_numeral_factors} *} |
|
786 |
||
787 |
lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)" |
|
788 |
by auto |
|
789 |
||
790 |
lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)" |
|
791 |
by auto |
|
792 |
||
793 |
lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)" |
|
794 |
by auto |
|
795 |
||
796 |
lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)" |
|
797 |
by auto |
|
798 |
||
23969 | 799 |
lemma nat_mult_dvd_cancel_disj[simp]: |
800 |
"(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))" |
|
801 |
by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric]) |
|
802 |
||
803 |
lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)" |
|
804 |
by(auto) |
|
805 |
||
23164 | 806 |
|
807 |
subsubsection{*For @{text cancel_factor} *} |
|
808 |
||
809 |
lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)" |
|
810 |
by auto |
|
811 |
||
812 |
lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)" |
|
813 |
by auto |
|
814 |
||
815 |
lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)" |
|
816 |
by auto |
|
817 |
||
23969 | 818 |
lemma nat_mult_div_cancel_disj[simp]: |
23164 | 819 |
"(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)" |
820 |
by (simp add: nat_mult_div_cancel1) |
|
821 |
||
822 |
end |