nipkow@10574
|
1 |
(* Title: HOL/nat_bin.ML
|
nipkow@10574
|
2 |
ID: $Id$
|
nipkow@10574
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory
|
nipkow@10574
|
4 |
Copyright 1999 University of Cambridge
|
nipkow@10574
|
5 |
|
nipkow@10574
|
6 |
Binary arithmetic for the natural numbers
|
nipkow@10574
|
7 |
*)
|
nipkow@10574
|
8 |
|
nipkow@10574
|
9 |
val nat_number_of_def = thm "nat_number_of_def";
|
nipkow@10574
|
10 |
|
nipkow@10574
|
11 |
(** nat (coercion from int to nat) **)
|
nipkow@10574
|
12 |
|
nipkow@10574
|
13 |
Goal "nat (number_of w) = number_of w";
|
nipkow@10574
|
14 |
by (simp_tac (simpset() addsimps [nat_number_of_def]) 1);
|
nipkow@10574
|
15 |
qed "nat_number_of";
|
paulson@11868
|
16 |
Addsimps [nat_number_of, nat_0, nat_1];
|
nipkow@10574
|
17 |
|
wenzelm@11701
|
18 |
Goal "Numeral0 = (0::nat)";
|
paulson@11868
|
19 |
by (simp_tac (simpset() addsimps [nat_number_of_def]) 1);
|
nipkow@10574
|
20 |
qed "numeral_0_eq_0";
|
nipkow@10574
|
21 |
|
wenzelm@11701
|
22 |
Goal "Numeral1 = (1::nat)";
|
paulson@11868
|
23 |
by (simp_tac (simpset() addsimps [nat_1, nat_number_of_def]) 1);
|
nipkow@10574
|
24 |
qed "numeral_1_eq_1";
|
nipkow@10574
|
25 |
|
paulson@11868
|
26 |
Goal "Numeral1 = Suc 0";
|
paulson@11868
|
27 |
by (simp_tac (simpset() addsimps [numeral_1_eq_1]) 1);
|
paulson@11868
|
28 |
qed "numeral_1_eq_Suc_0";
|
paulson@11868
|
29 |
|
paulson@12196
|
30 |
Goalw [nat_number_of_def] "2 = Suc (Suc 0)";
|
paulson@11868
|
31 |
by (rtac nat_2 1);
|
nipkow@10574
|
32 |
qed "numeral_2_eq_2";
|
nipkow@10574
|
33 |
|
nipkow@10574
|
34 |
(** int (coercion from nat to int) **)
|
nipkow@10574
|
35 |
|
nipkow@10574
|
36 |
(*"neg" is used in rewrite rules for binary comparisons*)
|
nipkow@10574
|
37 |
Goal "int (number_of v :: nat) = \
|
paulson@11868
|
38 |
\ (if neg (number_of v) then 0 \
|
nipkow@10574
|
39 |
\ else (number_of v :: int))";
|
nipkow@10574
|
40 |
by (simp_tac
|
nipkow@10574
|
41 |
(simpset_of Int.thy addsimps [neg_nat, nat_number_of_def,
|
nipkow@10574
|
42 |
not_neg_nat, int_0]) 1);
|
nipkow@10574
|
43 |
qed "int_nat_number_of";
|
nipkow@10574
|
44 |
Addsimps [int_nat_number_of];
|
nipkow@10574
|
45 |
|
nipkow@10574
|
46 |
|
nipkow@10574
|
47 |
val nat_bin_arith_setup =
|
paulson@11868
|
48 |
[Fast_Arith.map_data
|
paulson@11868
|
49 |
(fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, simpset} =>
|
paulson@11868
|
50 |
{add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms,
|
paulson@11868
|
51 |
inj_thms = inj_thms,
|
paulson@11868
|
52 |
lessD = lessD,
|
paulson@11868
|
53 |
simpset = simpset addsimps [int_nat_number_of, not_neg_number_of_Pls,
|
paulson@11868
|
54 |
neg_number_of_Min,neg_number_of_BIT]})];
|
nipkow@10574
|
55 |
|
nipkow@10574
|
56 |
(** Successor **)
|
nipkow@10574
|
57 |
|
paulson@11868
|
58 |
Goal "(0::int) <= z ==> Suc (nat z) = nat (1 + z)";
|
nipkow@10574
|
59 |
by (rtac sym 1);
|
paulson@11868
|
60 |
by (asm_simp_tac (simpset() addsimps [nat_eq_iff, int_Suc]) 1);
|
nipkow@10574
|
61 |
qed "Suc_nat_eq_nat_zadd1";
|
nipkow@10574
|
62 |
|
paulson@11868
|
63 |
Goal "Suc (number_of v + n) = \
|
paulson@11868
|
64 |
\ (if neg (number_of v) then 1+n else number_of (bin_succ v) + n)";
|
nipkow@10574
|
65 |
by (simp_tac
|
nipkow@10574
|
66 |
(simpset_of Int.thy addsimps [neg_nat, nat_1, not_neg_eq_ge_0,
|
nipkow@10574
|
67 |
nat_number_of_def, int_Suc,
|
nipkow@10574
|
68 |
Suc_nat_eq_nat_zadd1, number_of_succ]) 1);
|
paulson@11868
|
69 |
qed "Suc_nat_number_of_add";
|
paulson@11868
|
70 |
|
paulson@11868
|
71 |
Goal "Suc (number_of v) = \
|
paulson@11868
|
72 |
\ (if neg (number_of v) then 1 else number_of (bin_succ v))";
|
paulson@11868
|
73 |
by (cut_inst_tac [("n","0")] Suc_nat_number_of_add 1);
|
paulson@11868
|
74 |
by (asm_full_simp_tac (simpset() delcongs [if_weak_cong]) 1);
|
nipkow@10574
|
75 |
qed "Suc_nat_number_of";
|
nipkow@10574
|
76 |
Addsimps [Suc_nat_number_of];
|
nipkow@10574
|
77 |
|
nipkow@10574
|
78 |
(** Addition **)
|
nipkow@10574
|
79 |
|
paulson@11868
|
80 |
Goal "[| (0::int) <= z; 0 <= z' |] ==> nat (z+z') = nat z + nat z'";
|
nipkow@10574
|
81 |
by (rtac (inj_int RS injD) 1);
|
nipkow@10574
|
82 |
by (asm_simp_tac (simpset() addsimps [zadd_int RS sym]) 1);
|
nipkow@10574
|
83 |
qed "nat_add_distrib";
|
nipkow@10574
|
84 |
|
nipkow@10574
|
85 |
(*"neg" is used in rewrite rules for binary comparisons*)
|
nipkow@10574
|
86 |
Goal "(number_of v :: nat) + number_of v' = \
|
nipkow@10574
|
87 |
\ (if neg (number_of v) then number_of v' \
|
nipkow@10574
|
88 |
\ else if neg (number_of v') then number_of v \
|
nipkow@10574
|
89 |
\ else number_of (bin_add v v'))";
|
nipkow@10574
|
90 |
by (simp_tac
|
nipkow@10574
|
91 |
(simpset_of Int.thy addsimps [neg_nat, not_neg_eq_ge_0, nat_number_of_def,
|
nipkow@10574
|
92 |
nat_add_distrib RS sym, number_of_add]) 1);
|
nipkow@10574
|
93 |
qed "add_nat_number_of";
|
nipkow@10574
|
94 |
|
nipkow@10574
|
95 |
Addsimps [add_nat_number_of];
|
nipkow@10574
|
96 |
|
nipkow@10574
|
97 |
|
nipkow@10574
|
98 |
(** Subtraction **)
|
nipkow@10574
|
99 |
|
paulson@11868
|
100 |
Goal "[| (0::int) <= z'; z' <= z |] ==> nat (z-z') = nat z - nat z'";
|
nipkow@10574
|
101 |
by (rtac (inj_int RS injD) 1);
|
nipkow@10574
|
102 |
by (asm_simp_tac (simpset() addsimps [zdiff_int RS sym, nat_le_eq_zle]) 1);
|
nipkow@10574
|
103 |
qed "nat_diff_distrib";
|
nipkow@10574
|
104 |
|
nipkow@10574
|
105 |
|
nipkow@10574
|
106 |
Goal "nat z - nat z' = \
|
nipkow@10574
|
107 |
\ (if neg z' then nat z \
|
nipkow@10574
|
108 |
\ else let d = z-z' in \
|
nipkow@10574
|
109 |
\ if neg d then 0 else nat d)";
|
nipkow@10574
|
110 |
by (simp_tac (simpset() addsimps [Let_def, nat_diff_distrib RS sym,
|
nipkow@10574
|
111 |
neg_eq_less_0, not_neg_eq_ge_0]) 1);
|
nipkow@10574
|
112 |
by (simp_tac (simpset() addsimps [diff_is_0_eq, nat_le_eq_zle]) 1);
|
nipkow@10574
|
113 |
qed "diff_nat_eq_if";
|
nipkow@10574
|
114 |
|
nipkow@10574
|
115 |
Goalw [nat_number_of_def]
|
nipkow@10574
|
116 |
"(number_of v :: nat) - number_of v' = \
|
nipkow@10574
|
117 |
\ (if neg (number_of v') then number_of v \
|
nipkow@10574
|
118 |
\ else let d = number_of (bin_add v (bin_minus v')) in \
|
paulson@11868
|
119 |
\ if neg d then 0 else nat d)";
|
nipkow@10574
|
120 |
by (simp_tac
|
nipkow@10574
|
121 |
(simpset_of Int.thy delcongs [if_weak_cong]
|
nipkow@10574
|
122 |
addsimps [not_neg_eq_ge_0, nat_0,
|
nipkow@10574
|
123 |
diff_nat_eq_if, diff_number_of_eq]) 1);
|
nipkow@10574
|
124 |
qed "diff_nat_number_of";
|
nipkow@10574
|
125 |
|
nipkow@10574
|
126 |
Addsimps [diff_nat_number_of];
|
nipkow@10574
|
127 |
|
nipkow@10574
|
128 |
|
nipkow@10574
|
129 |
(** Multiplication **)
|
nipkow@10574
|
130 |
|
paulson@11868
|
131 |
Goal "(0::int) <= z ==> nat (z*z') = nat z * nat z'";
|
paulson@11868
|
132 |
by (case_tac "0 <= z'" 1);
|
nipkow@10574
|
133 |
by (asm_full_simp_tac (simpset() addsimps [zmult_le_0_iff]) 2);
|
nipkow@10574
|
134 |
by (rtac (inj_int RS injD) 1);
|
nipkow@10574
|
135 |
by (asm_simp_tac (simpset() addsimps [zmult_int RS sym,
|
nipkow@10574
|
136 |
int_0_le_mult_iff]) 1);
|
nipkow@10574
|
137 |
qed "nat_mult_distrib";
|
nipkow@10574
|
138 |
|
paulson@11868
|
139 |
Goal "z <= (0::int) ==> nat(z*z') = nat(-z) * nat(-z')";
|
nipkow@10574
|
140 |
by (rtac trans 1);
|
nipkow@10574
|
141 |
by (rtac nat_mult_distrib 2);
|
nipkow@10574
|
142 |
by Auto_tac;
|
nipkow@10574
|
143 |
qed "nat_mult_distrib_neg";
|
nipkow@10574
|
144 |
|
nipkow@10574
|
145 |
Goal "(number_of v :: nat) * number_of v' = \
|
paulson@11868
|
146 |
\ (if neg (number_of v) then 0 else number_of (bin_mult v v'))";
|
nipkow@10574
|
147 |
by (simp_tac
|
nipkow@10574
|
148 |
(simpset_of Int.thy addsimps [neg_nat, not_neg_eq_ge_0, nat_number_of_def,
|
nipkow@10574
|
149 |
nat_mult_distrib RS sym, number_of_mult,
|
nipkow@10574
|
150 |
nat_0]) 1);
|
nipkow@10574
|
151 |
qed "mult_nat_number_of";
|
nipkow@10574
|
152 |
|
nipkow@10574
|
153 |
Addsimps [mult_nat_number_of];
|
nipkow@10574
|
154 |
|
nipkow@10574
|
155 |
|
nipkow@10574
|
156 |
(** Quotient **)
|
nipkow@10574
|
157 |
|
paulson@11868
|
158 |
Goal "(0::int) <= z ==> nat (z div z') = nat z div nat z'";
|
paulson@11868
|
159 |
by (case_tac "0 <= z'" 1);
|
nipkow@10574
|
160 |
by (auto_tac (claset(),
|
nipkow@10574
|
161 |
simpset() addsimps [div_nonneg_neg_le0, DIVISION_BY_ZERO_DIV]));
|
paulson@11868
|
162 |
by (zdiv_undefined_case_tac "z' = 0" 1);
|
nipkow@10574
|
163 |
by (simp_tac (simpset() addsimps [numeral_0_eq_0, DIVISION_BY_ZERO_DIV]) 1);
|
nipkow@10574
|
164 |
by (auto_tac (claset() addSEs [nonneg_eq_int], simpset()));
|
nipkow@10574
|
165 |
by (rename_tac "m m'" 1);
|
paulson@11868
|
166 |
by (subgoal_tac "0 <= int m div int m'" 1);
|
nipkow@10574
|
167 |
by (asm_full_simp_tac
|
nipkow@10574
|
168 |
(simpset() addsimps [numeral_0_eq_0, pos_imp_zdiv_nonneg_iff]) 2);
|
nipkow@10574
|
169 |
by (rtac (inj_int RS injD) 1);
|
nipkow@10574
|
170 |
by (Asm_simp_tac 1);
|
nipkow@10574
|
171 |
by (res_inst_tac [("r", "int (m mod m')")] quorem_div 1);
|
nipkow@10574
|
172 |
by (Force_tac 2);
|
nipkow@10574
|
173 |
by (asm_full_simp_tac
|
nipkow@10574
|
174 |
(simpset() addsimps [nat_less_iff RS sym, quorem_def,
|
nipkow@10574
|
175 |
numeral_0_eq_0, zadd_int, zmult_int]) 1);
|
nipkow@10574
|
176 |
by (rtac (mod_div_equality RS sym RS trans) 1);
|
nipkow@10574
|
177 |
by (asm_simp_tac (simpset() addsimps add_ac@mult_ac) 1);
|
nipkow@10574
|
178 |
qed "nat_div_distrib";
|
nipkow@10574
|
179 |
|
nipkow@10574
|
180 |
Goal "(number_of v :: nat) div number_of v' = \
|
paulson@11868
|
181 |
\ (if neg (number_of v) then 0 \
|
nipkow@10574
|
182 |
\ else nat (number_of v div number_of v'))";
|
nipkow@10574
|
183 |
by (simp_tac
|
nipkow@10574
|
184 |
(simpset_of Int.thy addsimps [not_neg_eq_ge_0, nat_number_of_def, neg_nat,
|
nipkow@10574
|
185 |
nat_div_distrib RS sym, nat_0]) 1);
|
nipkow@10574
|
186 |
qed "div_nat_number_of";
|
nipkow@10574
|
187 |
|
nipkow@10574
|
188 |
Addsimps [div_nat_number_of];
|
nipkow@10574
|
189 |
|
nipkow@10574
|
190 |
|
nipkow@10574
|
191 |
(** Remainder **)
|
nipkow@10574
|
192 |
|
nipkow@10574
|
193 |
(*Fails if z'<0: the LHS collapses to (nat z) but the RHS doesn't*)
|
paulson@11868
|
194 |
Goal "[| (0::int) <= z; 0 <= z' |] ==> nat (z mod z') = nat z mod nat z'";
|
paulson@11868
|
195 |
by (zdiv_undefined_case_tac "z' = 0" 1);
|
nipkow@10574
|
196 |
by (simp_tac (simpset() addsimps [numeral_0_eq_0, DIVISION_BY_ZERO_MOD]) 1);
|
nipkow@10574
|
197 |
by (auto_tac (claset() addSEs [nonneg_eq_int], simpset()));
|
nipkow@10574
|
198 |
by (rename_tac "m m'" 1);
|
paulson@11868
|
199 |
by (subgoal_tac "0 <= int m mod int m'" 1);
|
nipkow@10574
|
200 |
by (asm_full_simp_tac
|
nipkow@10574
|
201 |
(simpset() addsimps [nat_less_iff, numeral_0_eq_0, pos_mod_sign]) 2);
|
nipkow@10574
|
202 |
by (rtac (inj_int RS injD) 1);
|
nipkow@10574
|
203 |
by (Asm_simp_tac 1);
|
nipkow@10574
|
204 |
by (res_inst_tac [("q", "int (m div m')")] quorem_mod 1);
|
nipkow@10574
|
205 |
by (Force_tac 2);
|
nipkow@10574
|
206 |
by (asm_full_simp_tac
|
nipkow@10574
|
207 |
(simpset() addsimps [nat_less_iff RS sym, quorem_def,
|
nipkow@10574
|
208 |
numeral_0_eq_0, zadd_int, zmult_int]) 1);
|
nipkow@10574
|
209 |
by (rtac (mod_div_equality RS sym RS trans) 1);
|
nipkow@10574
|
210 |
by (asm_simp_tac (simpset() addsimps add_ac@mult_ac) 1);
|
nipkow@10574
|
211 |
qed "nat_mod_distrib";
|
nipkow@10574
|
212 |
|
nipkow@10574
|
213 |
Goal "(number_of v :: nat) mod number_of v' = \
|
paulson@11868
|
214 |
\ (if neg (number_of v) then 0 \
|
nipkow@10574
|
215 |
\ else if neg (number_of v') then number_of v \
|
nipkow@10574
|
216 |
\ else nat (number_of v mod number_of v'))";
|
nipkow@10574
|
217 |
by (simp_tac
|
nipkow@10574
|
218 |
(simpset_of Int.thy addsimps [not_neg_eq_ge_0, nat_number_of_def,
|
nipkow@10574
|
219 |
neg_nat, nat_0, DIVISION_BY_ZERO_MOD,
|
nipkow@10574
|
220 |
nat_mod_distrib RS sym]) 1);
|
nipkow@10574
|
221 |
qed "mod_nat_number_of";
|
nipkow@10574
|
222 |
|
nipkow@10574
|
223 |
Addsimps [mod_nat_number_of];
|
nipkow@10574
|
224 |
|
paulson@11868
|
225 |
structure NatAbstractNumeralsData =
|
paulson@11868
|
226 |
struct
|
paulson@11868
|
227 |
val dest_eq = HOLogic.dest_eq o HOLogic.dest_Trueprop o concl_of
|
paulson@11868
|
228 |
val is_numeral = Bin_Simprocs.is_numeral
|
paulson@11868
|
229 |
val numeral_0_eq_0 = numeral_0_eq_0
|
paulson@11868
|
230 |
val numeral_1_eq_1 = numeral_1_eq_Suc_0
|
paulson@11868
|
231 |
val prove_conv = Bin_Simprocs.prove_conv_nohyps "nat_abstract_numerals"
|
paulson@11868
|
232 |
fun norm_tac simps = ALLGOALS (simp_tac (HOL_ss addsimps simps))
|
paulson@11868
|
233 |
val simplify_meta_eq = Bin_Simprocs.simplify_meta_eq
|
paulson@11868
|
234 |
end
|
paulson@11868
|
235 |
|
paulson@11868
|
236 |
structure NatAbstractNumerals = AbstractNumeralsFun (NatAbstractNumeralsData)
|
paulson@11868
|
237 |
|
paulson@11868
|
238 |
val nat_eval_numerals =
|
paulson@11868
|
239 |
map Bin_Simprocs.prep_simproc
|
paulson@11868
|
240 |
[("nat_div_eval_numerals",
|
paulson@11868
|
241 |
Bin_Simprocs.prep_pats ["(Suc 0) div m"],
|
paulson@11868
|
242 |
NatAbstractNumerals.proc div_nat_number_of),
|
paulson@11868
|
243 |
("nat_mod_eval_numerals",
|
paulson@11868
|
244 |
Bin_Simprocs.prep_pats ["(Suc 0) mod m"],
|
paulson@11868
|
245 |
NatAbstractNumerals.proc mod_nat_number_of)];
|
paulson@11868
|
246 |
|
paulson@11868
|
247 |
Addsimprocs nat_eval_numerals;
|
paulson@11868
|
248 |
|
nipkow@10574
|
249 |
|
nipkow@10574
|
250 |
(*** Comparisons ***)
|
nipkow@10574
|
251 |
|
nipkow@10574
|
252 |
(** Equals (=) **)
|
nipkow@10574
|
253 |
|
paulson@11868
|
254 |
Goal "[| (0::int) <= z; 0 <= z' |] ==> (nat z = nat z') = (z=z')";
|
nipkow@10574
|
255 |
by (auto_tac (claset() addSEs [nonneg_eq_int], simpset()));
|
nipkow@10574
|
256 |
qed "eq_nat_nat_iff";
|
nipkow@10574
|
257 |
|
nipkow@10574
|
258 |
(*"neg" is used in rewrite rules for binary comparisons*)
|
nipkow@10574
|
259 |
Goal "((number_of v :: nat) = number_of v') = \
|
nipkow@10574
|
260 |
\ (if neg (number_of v) then (iszero (number_of v') | neg (number_of v')) \
|
nipkow@10574
|
261 |
\ else if neg (number_of v') then iszero (number_of v) \
|
nipkow@10574
|
262 |
\ else iszero (number_of (bin_add v (bin_minus v'))))";
|
nipkow@10574
|
263 |
by (simp_tac
|
nipkow@10574
|
264 |
(simpset_of Int.thy addsimps [neg_nat, not_neg_eq_ge_0, nat_number_of_def,
|
nipkow@10574
|
265 |
eq_nat_nat_iff, eq_number_of_eq, nat_0]) 1);
|
nipkow@10574
|
266 |
by (simp_tac (simpset_of Int.thy addsimps [nat_eq_iff, nat_eq_iff2,
|
nipkow@10574
|
267 |
iszero_def]) 1);
|
nipkow@10574
|
268 |
by (simp_tac (simpset () addsimps [not_neg_eq_ge_0 RS sym]) 1);
|
nipkow@10574
|
269 |
qed "eq_nat_number_of";
|
nipkow@10574
|
270 |
|
nipkow@10574
|
271 |
Addsimps [eq_nat_number_of];
|
nipkow@10574
|
272 |
|
nipkow@10574
|
273 |
(** Less-than (<) **)
|
nipkow@10574
|
274 |
|
nipkow@10574
|
275 |
(*"neg" is used in rewrite rules for binary comparisons*)
|
nipkow@10574
|
276 |
Goal "((number_of v :: nat) < number_of v') = \
|
nipkow@10574
|
277 |
\ (if neg (number_of v) then neg (number_of (bin_minus v')) \
|
nipkow@10574
|
278 |
\ else neg (number_of (bin_add v (bin_minus v'))))";
|
nipkow@10574
|
279 |
by (simp_tac
|
nipkow@10574
|
280 |
(simpset_of Int.thy addsimps [neg_nat, not_neg_eq_ge_0, nat_number_of_def,
|
nipkow@10574
|
281 |
nat_less_eq_zless, less_number_of_eq_neg,
|
nipkow@10574
|
282 |
nat_0]) 1);
|
paulson@11868
|
283 |
by (simp_tac (simpset_of Int.thy addsimps [neg_eq_less_0, zminus_zless,
|
nipkow@10574
|
284 |
number_of_minus, zless_nat_eq_int_zless]) 1);
|
nipkow@10574
|
285 |
qed "less_nat_number_of";
|
nipkow@10574
|
286 |
|
nipkow@10574
|
287 |
Addsimps [less_nat_number_of];
|
nipkow@10574
|
288 |
|
nipkow@10574
|
289 |
|
nipkow@10574
|
290 |
(** Less-than-or-equals (<=) **)
|
nipkow@10574
|
291 |
|
nipkow@10574
|
292 |
Goal "(number_of x <= (number_of y::nat)) = \
|
nipkow@10574
|
293 |
\ (~ number_of y < (number_of x::nat))";
|
nipkow@10574
|
294 |
by (rtac (linorder_not_less RS sym) 1);
|
nipkow@10574
|
295 |
qed "le_nat_number_of_eq_not_less";
|
nipkow@10574
|
296 |
|
nipkow@10574
|
297 |
Addsimps [le_nat_number_of_eq_not_less];
|
nipkow@10574
|
298 |
|
nipkow@10574
|
299 |
|
paulson@11868
|
300 |
(*Maps #n to n for n = 0, 1, 2*)
|
wenzelm@11018
|
301 |
bind_thms ("numerals", [numeral_0_eq_0, numeral_1_eq_1, numeral_2_eq_2]);
|
wenzelm@11018
|
302 |
val numeral_ss = simpset() addsimps numerals;
|
nipkow@10574
|
303 |
|
nipkow@10574
|
304 |
(** Nat **)
|
nipkow@10574
|
305 |
|
paulson@11868
|
306 |
Goal "0 < n ==> n = Suc(n - 1)";
|
nipkow@10574
|
307 |
by (asm_full_simp_tac numeral_ss 1);
|
nipkow@10574
|
308 |
qed "Suc_pred'";
|
nipkow@10574
|
309 |
|
nipkow@10574
|
310 |
(*Expresses a natural number constant as the Suc of another one.
|
nipkow@10574
|
311 |
NOT suitable for rewriting because n recurs in the condition.*)
|
nipkow@10574
|
312 |
bind_thm ("expand_Suc", inst "n" "number_of ?v" Suc_pred');
|
nipkow@10574
|
313 |
|
nipkow@10574
|
314 |
(** Arith **)
|
nipkow@10574
|
315 |
|
paulson@11868
|
316 |
Goal "Suc n = n + 1";
|
nipkow@10574
|
317 |
by (asm_simp_tac numeral_ss 1);
|
nipkow@10574
|
318 |
qed "Suc_eq_add_numeral_1";
|
nipkow@10574
|
319 |
|
nipkow@10574
|
320 |
(* These two can be useful when m = number_of... *)
|
nipkow@10574
|
321 |
|
paulson@11868
|
322 |
Goal "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))";
|
nipkow@10574
|
323 |
by (case_tac "m" 1);
|
nipkow@10574
|
324 |
by (ALLGOALS (asm_simp_tac numeral_ss));
|
nipkow@10574
|
325 |
qed "add_eq_if";
|
nipkow@10574
|
326 |
|
paulson@11868
|
327 |
Goal "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))";
|
nipkow@10574
|
328 |
by (case_tac "m" 1);
|
nipkow@10574
|
329 |
by (ALLGOALS (asm_simp_tac numeral_ss));
|
nipkow@10574
|
330 |
qed "mult_eq_if";
|
nipkow@10574
|
331 |
|
paulson@11868
|
332 |
Goal "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))";
|
nipkow@10574
|
333 |
by (case_tac "m" 1);
|
nipkow@10574
|
334 |
by (ALLGOALS (asm_simp_tac numeral_ss));
|
nipkow@10574
|
335 |
qed "power_eq_if";
|
nipkow@10574
|
336 |
|
paulson@11868
|
337 |
Goal "[| 0<n; 0<m |] ==> m - n < (m::nat)";
|
nipkow@10574
|
338 |
by (asm_full_simp_tac (numeral_ss addsimps [diff_less]) 1);
|
nipkow@10574
|
339 |
qed "diff_less'";
|
nipkow@10574
|
340 |
|
nipkow@10574
|
341 |
Addsimps [inst "n" "number_of ?v" diff_less'];
|
nipkow@10574
|
342 |
|
nipkow@10574
|
343 |
(** Power **)
|
nipkow@10574
|
344 |
|
wenzelm@11704
|
345 |
Goal "(p::nat) ^ 2 = p*p";
|
nipkow@10574
|
346 |
by (simp_tac numeral_ss 1);
|
nipkow@10574
|
347 |
qed "power_two";
|
nipkow@10574
|
348 |
|
nipkow@10574
|
349 |
|
nipkow@10574
|
350 |
(*** Comparisons involving (0::nat) ***)
|
nipkow@10574
|
351 |
|
nipkow@10574
|
352 |
Goal "(number_of v = (0::nat)) = \
|
nipkow@10574
|
353 |
\ (if neg (number_of v) then True else iszero (number_of v))";
|
paulson@11868
|
354 |
by (simp_tac (simpset() addsimps [numeral_0_eq_0 RS sym, iszero_0]) 1);
|
paulson@11868
|
355 |
qed "eq_number_of_0";
|
nipkow@10574
|
356 |
|
nipkow@10574
|
357 |
Goal "((0::nat) = number_of v) = \
|
nipkow@10574
|
358 |
\ (if neg (number_of v) then True else iszero (number_of v))";
|
nipkow@10574
|
359 |
by (rtac ([eq_sym_conv, eq_number_of_0] MRS trans) 1);
|
nipkow@10574
|
360 |
qed "eq_0_number_of";
|
nipkow@10574
|
361 |
|
nipkow@10574
|
362 |
Goal "((0::nat) < number_of v) = neg (number_of (bin_minus v))";
|
nipkow@10574
|
363 |
by (simp_tac (simpset() addsimps [numeral_0_eq_0 RS sym]) 1);
|
nipkow@10574
|
364 |
qed "less_0_number_of";
|
nipkow@10574
|
365 |
|
nipkow@10574
|
366 |
(*Simplification already handles n<0, n<=0 and 0<=n.*)
|
nipkow@10574
|
367 |
Addsimps [eq_number_of_0, eq_0_number_of, less_0_number_of];
|
nipkow@10574
|
368 |
|
nipkow@10574
|
369 |
Goal "neg (number_of v) ==> number_of v = (0::nat)";
|
paulson@11868
|
370 |
by (asm_simp_tac (simpset() addsimps [numeral_0_eq_0 RS sym, iszero_0]) 1);
|
nipkow@10574
|
371 |
qed "neg_imp_number_of_eq_0";
|
nipkow@10574
|
372 |
|
nipkow@10574
|
373 |
|
nipkow@10574
|
374 |
|
nipkow@10574
|
375 |
(*** Comparisons involving Suc ***)
|
nipkow@10574
|
376 |
|
nipkow@10574
|
377 |
Goal "(number_of v = Suc n) = \
|
nipkow@10574
|
378 |
\ (let pv = number_of (bin_pred v) in \
|
nipkow@10574
|
379 |
\ if neg pv then False else nat pv = n)";
|
nipkow@10574
|
380 |
by (simp_tac
|
nipkow@10574
|
381 |
(simpset_of Int.thy addsimps
|
nipkow@10574
|
382 |
[Let_def, neg_eq_less_0, linorder_not_less, number_of_pred,
|
nipkow@10574
|
383 |
nat_number_of_def, zadd_0] @ zadd_ac) 1);
|
nipkow@10574
|
384 |
by (res_inst_tac [("x", "number_of v")] spec 1);
|
nipkow@10574
|
385 |
by (auto_tac (claset(), simpset() addsimps [nat_eq_iff]));
|
nipkow@10574
|
386 |
qed "eq_number_of_Suc";
|
nipkow@10574
|
387 |
|
nipkow@10574
|
388 |
Goal "(Suc n = number_of v) = \
|
nipkow@10574
|
389 |
\ (let pv = number_of (bin_pred v) in \
|
nipkow@10574
|
390 |
\ if neg pv then False else nat pv = n)";
|
nipkow@10574
|
391 |
by (rtac ([eq_sym_conv, eq_number_of_Suc] MRS trans) 1);
|
nipkow@10574
|
392 |
qed "Suc_eq_number_of";
|
nipkow@10574
|
393 |
|
nipkow@10574
|
394 |
Goal "(number_of v < Suc n) = \
|
nipkow@10574
|
395 |
\ (let pv = number_of (bin_pred v) in \
|
nipkow@10574
|
396 |
\ if neg pv then True else nat pv < n)";
|
nipkow@10574
|
397 |
by (simp_tac
|
nipkow@10574
|
398 |
(simpset_of Int.thy addsimps
|
nipkow@10574
|
399 |
[Let_def, neg_eq_less_0, linorder_not_less, number_of_pred,
|
nipkow@10574
|
400 |
nat_number_of_def, zadd_0] @ zadd_ac) 1);
|
nipkow@10574
|
401 |
by (res_inst_tac [("x", "number_of v")] spec 1);
|
nipkow@10574
|
402 |
by (auto_tac (claset(), simpset() addsimps [nat_less_iff]));
|
nipkow@10574
|
403 |
qed "less_number_of_Suc";
|
nipkow@10574
|
404 |
|
nipkow@10574
|
405 |
Goal "(Suc n < number_of v) = \
|
nipkow@10574
|
406 |
\ (let pv = number_of (bin_pred v) in \
|
nipkow@10574
|
407 |
\ if neg pv then False else n < nat pv)";
|
nipkow@10574
|
408 |
by (simp_tac
|
nipkow@10574
|
409 |
(simpset_of Int.thy addsimps
|
nipkow@10574
|
410 |
[Let_def, neg_eq_less_0, linorder_not_less, number_of_pred,
|
nipkow@10574
|
411 |
nat_number_of_def, zadd_0] @ zadd_ac) 1);
|
nipkow@10574
|
412 |
by (res_inst_tac [("x", "number_of v")] spec 1);
|
nipkow@10574
|
413 |
by (auto_tac (claset(), simpset() addsimps [zless_nat_eq_int_zless]));
|
nipkow@10574
|
414 |
qed "less_Suc_number_of";
|
nipkow@10574
|
415 |
|
nipkow@10574
|
416 |
Goal "(number_of v <= Suc n) = \
|
nipkow@10574
|
417 |
\ (let pv = number_of (bin_pred v) in \
|
nipkow@10574
|
418 |
\ if neg pv then True else nat pv <= n)";
|
nipkow@10574
|
419 |
by (simp_tac
|
nipkow@10574
|
420 |
(simpset () addsimps
|
nipkow@10574
|
421 |
[Let_def, less_Suc_number_of, linorder_not_less RS sym]) 1);
|
nipkow@10574
|
422 |
qed "le_number_of_Suc";
|
nipkow@10574
|
423 |
|
nipkow@10574
|
424 |
Goal "(Suc n <= number_of v) = \
|
nipkow@10574
|
425 |
\ (let pv = number_of (bin_pred v) in \
|
nipkow@10574
|
426 |
\ if neg pv then False else n <= nat pv)";
|
nipkow@10574
|
427 |
by (simp_tac
|
nipkow@10574
|
428 |
(simpset () addsimps
|
nipkow@10574
|
429 |
[Let_def, less_number_of_Suc, linorder_not_less RS sym]) 1);
|
nipkow@10574
|
430 |
qed "le_Suc_number_of";
|
nipkow@10574
|
431 |
|
nipkow@10574
|
432 |
Addsimps [eq_number_of_Suc, Suc_eq_number_of,
|
nipkow@10574
|
433 |
less_number_of_Suc, less_Suc_number_of,
|
nipkow@10574
|
434 |
le_number_of_Suc, le_Suc_number_of];
|
nipkow@10574
|
435 |
|
nipkow@10574
|
436 |
(* Push int(.) inwards: *)
|
paulson@11868
|
437 |
Addsimps [zadd_int RS sym];
|
nipkow@10574
|
438 |
|
nipkow@10574
|
439 |
Goal "(m+m = n+n) = (m = (n::int))";
|
nipkow@10574
|
440 |
by Auto_tac;
|
nipkow@10574
|
441 |
val lemma1 = result();
|
nipkow@10574
|
442 |
|
paulson@11868
|
443 |
Goal "m+m ~= (1::int) + n + n";
|
nipkow@10574
|
444 |
by Auto_tac;
|
wenzelm@11704
|
445 |
by (dres_inst_tac [("f", "%x. x mod 2")] arg_cong 1);
|
nipkow@10574
|
446 |
by (full_simp_tac (simpset() addsimps [zmod_zadd1_eq]) 1);
|
nipkow@10574
|
447 |
val lemma2 = result();
|
nipkow@10574
|
448 |
|
nipkow@10574
|
449 |
Goal "((number_of (v BIT x) ::int) = number_of (w BIT y)) = \
|
nipkow@10574
|
450 |
\ (x=y & (((number_of v) ::int) = number_of w))";
|
nipkow@10574
|
451 |
by (simp_tac (simpset_of Int.thy addsimps
|
nipkow@10574
|
452 |
[number_of_BIT, lemma1, lemma2, eq_commute]) 1);
|
nipkow@10574
|
453 |
qed "eq_number_of_BIT_BIT";
|
nipkow@10574
|
454 |
|
nipkow@10574
|
455 |
Goal "((number_of (v BIT x) ::int) = number_of Pls) = \
|
nipkow@10574
|
456 |
\ (x=False & (((number_of v) ::int) = number_of Pls))";
|
nipkow@10574
|
457 |
by (simp_tac (simpset_of Int.thy addsimps
|
nipkow@10574
|
458 |
[number_of_BIT, number_of_Pls, eq_commute]) 1);
|
nipkow@10574
|
459 |
by (res_inst_tac [("x", "number_of v")] spec 1);
|
nipkow@10574
|
460 |
by Safe_tac;
|
nipkow@10574
|
461 |
by (ALLGOALS Full_simp_tac);
|
wenzelm@11704
|
462 |
by (dres_inst_tac [("f", "%x. x mod 2")] arg_cong 1);
|
nipkow@10574
|
463 |
by (full_simp_tac (simpset() addsimps [zmod_zadd1_eq]) 1);
|
nipkow@10574
|
464 |
qed "eq_number_of_BIT_Pls";
|
nipkow@10574
|
465 |
|
nipkow@10574
|
466 |
Goal "((number_of (v BIT x) ::int) = number_of Min) = \
|
nipkow@10574
|
467 |
\ (x=True & (((number_of v) ::int) = number_of Min))";
|
nipkow@10574
|
468 |
by (simp_tac (simpset_of Int.thy addsimps
|
nipkow@10574
|
469 |
[number_of_BIT, number_of_Min, eq_commute]) 1);
|
nipkow@10574
|
470 |
by (res_inst_tac [("x", "number_of v")] spec 1);
|
nipkow@10574
|
471 |
by Auto_tac;
|
wenzelm@11704
|
472 |
by (dres_inst_tac [("f", "%x. x mod 2")] arg_cong 1);
|
nipkow@10574
|
473 |
by Auto_tac;
|
nipkow@10574
|
474 |
qed "eq_number_of_BIT_Min";
|
nipkow@10574
|
475 |
|
nipkow@10574
|
476 |
Goal "(number_of Pls ::int) ~= number_of Min";
|
nipkow@10574
|
477 |
by Auto_tac;
|
nipkow@10574
|
478 |
qed "eq_number_of_Pls_Min";
|
nipkow@10574
|
479 |
|
nipkow@10574
|
480 |
|
nipkow@10574
|
481 |
(*** Further lemmas about "nat" ***)
|
nipkow@10574
|
482 |
|
nipkow@10574
|
483 |
Goal "nat (abs (w * z)) = nat (abs w) * nat (abs z)";
|
paulson@11868
|
484 |
by (case_tac "z=0 | w=0" 1);
|
nipkow@10574
|
485 |
by Auto_tac;
|
nipkow@10574
|
486 |
by (simp_tac (simpset() addsimps [zabs_def, nat_mult_distrib RS sym,
|
nipkow@10574
|
487 |
nat_mult_distrib_neg RS sym, zmult_less_0_iff]) 1);
|
nipkow@10574
|
488 |
by (arith_tac 1);
|
nipkow@10574
|
489 |
qed "nat_abs_mult_distrib";
|
paulson@10754
|
490 |
|
paulson@10754
|
491 |
(*Distributive laws for literals*)
|
paulson@10754
|
492 |
Addsimps (map (inst "k" "number_of ?v")
|
paulson@10754
|
493 |
[add_mult_distrib, add_mult_distrib2,
|
paulson@10754
|
494 |
diff_mult_distrib, diff_mult_distrib2]);
|
paulson@10754
|
495 |
|
paulson@12613
|
496 |
|
paulson@12613
|
497 |
(*** Literal arithmetic involving powers, type nat ***)
|
paulson@12613
|
498 |
|
paulson@12613
|
499 |
Goal "(0::int) <= z ==> nat (z^n) = nat z ^ n";
|
paulson@12613
|
500 |
by (induct_tac "n" 1);
|
paulson@12613
|
501 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [nat_mult_distrib])));
|
paulson@12613
|
502 |
qed "nat_power_eq";
|
paulson@12613
|
503 |
|
paulson@12613
|
504 |
Goal "(number_of v :: nat) ^ n = \
|
paulson@12613
|
505 |
\ (if neg (number_of v) then 0^n else nat ((number_of v :: int) ^ n))";
|
paulson@12613
|
506 |
by (simp_tac
|
paulson@12613
|
507 |
(simpset_of Int.thy addsimps [neg_nat, not_neg_eq_ge_0, nat_number_of_def,
|
paulson@12613
|
508 |
nat_power_eq]) 1);
|
paulson@12613
|
509 |
qed "power_nat_number_of";
|
paulson@12613
|
510 |
|
paulson@12613
|
511 |
Addsimps [inst "n" "number_of ?w" power_nat_number_of];
|
paulson@12613
|
512 |
|
paulson@12613
|
513 |
|
paulson@12613
|
514 |
|
paulson@12613
|
515 |
(*** Literal arithmetic involving powers, type int ***)
|
paulson@12613
|
516 |
|
paulson@12613
|
517 |
Goal "(z::int) ^ (2*a) = (z^a)^2";
|
paulson@12613
|
518 |
by (simp_tac (simpset() addsimps [zpower_zpower, mult_commute]) 1);
|
paulson@12613
|
519 |
qed "zpower_even";
|
paulson@12613
|
520 |
|
paulson@12613
|
521 |
Goal "(p::int) ^ 2 = p*p";
|
paulson@12613
|
522 |
by (simp_tac numeral_ss 1);
|
paulson@12613
|
523 |
qed "zpower_two";
|
paulson@12613
|
524 |
|
paulson@12613
|
525 |
Goal "(z::int) ^ (2*a + 1) = z * (z^a)^2";
|
paulson@12613
|
526 |
by (simp_tac (simpset() addsimps [zpower_even, zpower_zadd_distrib]) 1);
|
paulson@12613
|
527 |
qed "zpower_odd";
|
paulson@12613
|
528 |
|
paulson@12613
|
529 |
Goal "(z::int) ^ number_of (w BIT False) = \
|
paulson@12613
|
530 |
\ (let w = z ^ (number_of w) in w*w)";
|
paulson@12613
|
531 |
by (simp_tac (simpset_of Int.thy addsimps [nat_number_of_def,
|
paulson@12613
|
532 |
number_of_BIT, Let_def]) 1);
|
paulson@12613
|
533 |
by (res_inst_tac [("x","number_of w")] spec 1 THEN Clarify_tac 1);
|
paulson@12613
|
534 |
by (case_tac "(0::int) <= x" 1);
|
paulson@12613
|
535 |
by (auto_tac (claset(),
|
paulson@12613
|
536 |
simpset() addsimps [nat_mult_distrib, zpower_even, zpower_two]));
|
paulson@12613
|
537 |
qed "zpower_number_of_even";
|
paulson@12613
|
538 |
|
paulson@12613
|
539 |
Goal "(z::int) ^ number_of (w BIT True) = \
|
paulson@12613
|
540 |
\ (if (0::int) <= number_of w \
|
paulson@12613
|
541 |
\ then (let w = z ^ (number_of w) in z*w*w) \
|
paulson@12613
|
542 |
\ else 1)";
|
paulson@12613
|
543 |
by (simp_tac (simpset_of Int.thy addsimps [nat_number_of_def,
|
paulson@12613
|
544 |
number_of_BIT, Let_def]) 1);
|
paulson@12613
|
545 |
by (res_inst_tac [("x","number_of w")] spec 1 THEN Clarify_tac 1);
|
paulson@12613
|
546 |
by (case_tac "(0::int) <= x" 1);
|
paulson@12613
|
547 |
by (auto_tac (claset(),
|
paulson@12613
|
548 |
simpset() addsimps [nat_add_distrib, nat_mult_distrib,
|
paulson@12613
|
549 |
zpower_even, zpower_two]));
|
paulson@12613
|
550 |
qed "zpower_number_of_odd";
|
paulson@12613
|
551 |
|
paulson@12613
|
552 |
Addsimps (map (inst "z" "number_of ?v")
|
paulson@12613
|
553 |
[zpower_number_of_even, zpower_number_of_odd]);
|
paulson@12613
|
554 |
|