| author | blanchet | 
| Thu, 28 May 2015 10:18:46 +0200 | |
| changeset 60306 | 6b7c64ab8bd2 | 
| parent 59996 | 4dca48557921 | 
| child 60758 | d8d85a8172b5 | 
| permissions | -rw-r--r-- | 
| 47108 | 1  | 
(* Title: HOL/Num.thy  | 
2  | 
Author: Florian Haftmann  | 
|
3  | 
Author: Brian Huffman  | 
|
4  | 
*)  | 
|
5  | 
||
| 58889 | 6  | 
section {* Binary Numerals *}
 | 
| 47108 | 7  | 
|
8  | 
theory Num  | 
|
| 58154 | 9  | 
imports BNF_Least_Fixpoint  | 
| 47108 | 10  | 
begin  | 
11  | 
||
12  | 
subsection {* The @{text num} type *}
 | 
|
13  | 
||
| 58310 | 14  | 
datatype num = One | Bit0 num | Bit1 num  | 
| 47108 | 15  | 
|
16  | 
text {* Increment function for type @{typ num} *}
 | 
|
17  | 
||
18  | 
primrec inc :: "num \<Rightarrow> num" where  | 
|
19  | 
"inc One = Bit0 One" |  | 
|
20  | 
"inc (Bit0 x) = Bit1 x" |  | 
|
21  | 
"inc (Bit1 x) = Bit0 (inc x)"  | 
|
22  | 
||
23  | 
text {* Converting between type @{typ num} and type @{typ nat} *}
 | 
|
24  | 
||
25  | 
primrec nat_of_num :: "num \<Rightarrow> nat" where  | 
|
26  | 
"nat_of_num One = Suc 0" |  | 
|
27  | 
"nat_of_num (Bit0 x) = nat_of_num x + nat_of_num x" |  | 
|
28  | 
"nat_of_num (Bit1 x) = Suc (nat_of_num x + nat_of_num x)"  | 
|
29  | 
||
30  | 
primrec num_of_nat :: "nat \<Rightarrow> num" where  | 
|
31  | 
"num_of_nat 0 = One" |  | 
|
32  | 
"num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"  | 
|
33  | 
||
34  | 
lemma nat_of_num_pos: "0 < nat_of_num x"  | 
|
35  | 
by (induct x) simp_all  | 
|
36  | 
||
37  | 
lemma nat_of_num_neq_0: " nat_of_num x \<noteq> 0"  | 
|
38  | 
by (induct x) simp_all  | 
|
39  | 
||
40  | 
lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"  | 
|
41  | 
by (induct x) simp_all  | 
|
42  | 
||
43  | 
lemma num_of_nat_double:  | 
|
44  | 
"0 < n \<Longrightarrow> num_of_nat (n + n) = Bit0 (num_of_nat n)"  | 
|
45  | 
by (induct n) simp_all  | 
|
46  | 
||
47  | 
text {*
 | 
|
48  | 
  Type @{typ num} is isomorphic to the strictly positive
 | 
|
49  | 
natural numbers.  | 
|
50  | 
*}  | 
|
51  | 
||
52  | 
lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"  | 
|
53  | 
by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)  | 
|
54  | 
||
55  | 
lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n"  | 
|
56  | 
by (induct n) (simp_all add: nat_of_num_inc)  | 
|
57  | 
||
58  | 
lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y"  | 
|
59  | 
apply safe  | 
|
60  | 
apply (drule arg_cong [where f=num_of_nat])  | 
|
61  | 
apply (simp add: nat_of_num_inverse)  | 
|
62  | 
done  | 
|
63  | 
||
64  | 
lemma num_induct [case_names One inc]:  | 
|
65  | 
fixes P :: "num \<Rightarrow> bool"  | 
|
66  | 
assumes One: "P One"  | 
|
67  | 
and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"  | 
|
68  | 
shows "P x"  | 
|
69  | 
proof -  | 
|
70  | 
obtain n where n: "Suc n = nat_of_num x"  | 
|
71  | 
by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)  | 
|
72  | 
have "P (num_of_nat (Suc n))"  | 
|
73  | 
proof (induct n)  | 
|
74  | 
case 0 show ?case using One by simp  | 
|
75  | 
next  | 
|
76  | 
case (Suc n)  | 
|
77  | 
then have "P (inc (num_of_nat (Suc n)))" by (rule inc)  | 
|
78  | 
then show "P (num_of_nat (Suc (Suc n)))" by simp  | 
|
79  | 
qed  | 
|
80  | 
with n show "P x"  | 
|
81  | 
by (simp add: nat_of_num_inverse)  | 
|
82  | 
qed  | 
|
83  | 
||
84  | 
text {*
 | 
|
85  | 
  From now on, there are two possible models for @{typ num}:
 | 
|
86  | 
  as positive naturals (rule @{text "num_induct"})
 | 
|
87  | 
  and as digit representation (rules @{text "num.induct"}, @{text "num.cases"}).
 | 
|
88  | 
*}  | 
|
89  | 
||
90  | 
||
91  | 
subsection {* Numeral operations *}
 | 
|
92  | 
||
93  | 
instantiation num :: "{plus,times,linorder}"
 | 
|
94  | 
begin  | 
|
95  | 
||
96  | 
definition [code del]:  | 
|
97  | 
"m + n = num_of_nat (nat_of_num m + nat_of_num n)"  | 
|
98  | 
||
99  | 
definition [code del]:  | 
|
100  | 
"m * n = num_of_nat (nat_of_num m * nat_of_num n)"  | 
|
101  | 
||
102  | 
definition [code del]:  | 
|
103  | 
"m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"  | 
|
104  | 
||
105  | 
definition [code del]:  | 
|
106  | 
"m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"  | 
|
107  | 
||
108  | 
instance  | 
|
109  | 
by (default, auto simp add: less_num_def less_eq_num_def num_eq_iff)  | 
|
110  | 
||
111  | 
end  | 
|
112  | 
||
113  | 
lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"  | 
|
114  | 
unfolding plus_num_def  | 
|
115  | 
by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)  | 
|
116  | 
||
117  | 
lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"  | 
|
118  | 
unfolding times_num_def  | 
|
119  | 
by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)  | 
|
120  | 
||
121  | 
lemma add_num_simps [simp, code]:  | 
|
122  | 
"One + One = Bit0 One"  | 
|
123  | 
"One + Bit0 n = Bit1 n"  | 
|
124  | 
"One + Bit1 n = Bit0 (n + One)"  | 
|
125  | 
"Bit0 m + One = Bit1 m"  | 
|
126  | 
"Bit0 m + Bit0 n = Bit0 (m + n)"  | 
|
127  | 
"Bit0 m + Bit1 n = Bit1 (m + n)"  | 
|
128  | 
"Bit1 m + One = Bit0 (m + One)"  | 
|
129  | 
"Bit1 m + Bit0 n = Bit1 (m + n)"  | 
|
130  | 
"Bit1 m + Bit1 n = Bit0 (m + n + One)"  | 
|
131  | 
by (simp_all add: num_eq_iff nat_of_num_add)  | 
|
132  | 
||
133  | 
lemma mult_num_simps [simp, code]:  | 
|
134  | 
"m * One = m"  | 
|
135  | 
"One * n = n"  | 
|
136  | 
"Bit0 m * Bit0 n = Bit0 (Bit0 (m * n))"  | 
|
137  | 
"Bit0 m * Bit1 n = Bit0 (m * Bit1 n)"  | 
|
138  | 
"Bit1 m * Bit0 n = Bit0 (Bit1 m * n)"  | 
|
139  | 
"Bit1 m * Bit1 n = Bit1 (m + n + Bit0 (m * n))"  | 
|
140  | 
by (simp_all add: num_eq_iff nat_of_num_add  | 
|
| 
49962
 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 
webertj 
parents: 
49690 
diff
changeset
 | 
141  | 
nat_of_num_mult distrib_right distrib_left)  | 
| 47108 | 142  | 
|
143  | 
lemma eq_num_simps:  | 
|
144  | 
"One = One \<longleftrightarrow> True"  | 
|
145  | 
"One = Bit0 n \<longleftrightarrow> False"  | 
|
146  | 
"One = Bit1 n \<longleftrightarrow> False"  | 
|
147  | 
"Bit0 m = One \<longleftrightarrow> False"  | 
|
148  | 
"Bit1 m = One \<longleftrightarrow> False"  | 
|
149  | 
"Bit0 m = Bit0 n \<longleftrightarrow> m = n"  | 
|
150  | 
"Bit0 m = Bit1 n \<longleftrightarrow> False"  | 
|
151  | 
"Bit1 m = Bit0 n \<longleftrightarrow> False"  | 
|
152  | 
"Bit1 m = Bit1 n \<longleftrightarrow> m = n"  | 
|
153  | 
by simp_all  | 
|
154  | 
||
155  | 
lemma le_num_simps [simp, code]:  | 
|
156  | 
"One \<le> n \<longleftrightarrow> True"  | 
|
157  | 
"Bit0 m \<le> One \<longleftrightarrow> False"  | 
|
158  | 
"Bit1 m \<le> One \<longleftrightarrow> False"  | 
|
159  | 
"Bit0 m \<le> Bit0 n \<longleftrightarrow> m \<le> n"  | 
|
160  | 
"Bit0 m \<le> Bit1 n \<longleftrightarrow> m \<le> n"  | 
|
161  | 
"Bit1 m \<le> Bit1 n \<longleftrightarrow> m \<le> n"  | 
|
162  | 
"Bit1 m \<le> Bit0 n \<longleftrightarrow> m < n"  | 
|
163  | 
using nat_of_num_pos [of n] nat_of_num_pos [of m]  | 
|
164  | 
by (auto simp add: less_eq_num_def less_num_def)  | 
|
165  | 
||
166  | 
lemma less_num_simps [simp, code]:  | 
|
167  | 
"m < One \<longleftrightarrow> False"  | 
|
168  | 
"One < Bit0 n \<longleftrightarrow> True"  | 
|
169  | 
"One < Bit1 n \<longleftrightarrow> True"  | 
|
170  | 
"Bit0 m < Bit0 n \<longleftrightarrow> m < n"  | 
|
171  | 
"Bit0 m < Bit1 n \<longleftrightarrow> m \<le> n"  | 
|
172  | 
"Bit1 m < Bit1 n \<longleftrightarrow> m < n"  | 
|
173  | 
"Bit1 m < Bit0 n \<longleftrightarrow> m < n"  | 
|
174  | 
using nat_of_num_pos [of n] nat_of_num_pos [of m]  | 
|
175  | 
by (auto simp add: less_eq_num_def less_num_def)  | 
|
176  | 
||
177  | 
text {* Rules using @{text One} and @{text inc} as constructors *}
 | 
|
178  | 
||
179  | 
lemma add_One: "x + One = inc x"  | 
|
180  | 
by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)  | 
|
181  | 
||
182  | 
lemma add_One_commute: "One + n = n + One"  | 
|
183  | 
by (induct n) simp_all  | 
|
184  | 
||
185  | 
lemma add_inc: "x + inc y = inc (x + y)"  | 
|
186  | 
by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)  | 
|
187  | 
||
188  | 
lemma mult_inc: "x * inc y = x * y + x"  | 
|
189  | 
by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)  | 
|
190  | 
||
191  | 
text {* The @{const num_of_nat} conversion *}
 | 
|
192  | 
||
193  | 
lemma num_of_nat_One:  | 
|
| 47300 | 194  | 
"n \<le> 1 \<Longrightarrow> num_of_nat n = One"  | 
| 47108 | 195  | 
by (cases n) simp_all  | 
196  | 
||
197  | 
lemma num_of_nat_plus_distrib:  | 
|
198  | 
"0 < m \<Longrightarrow> 0 < n \<Longrightarrow> num_of_nat (m + n) = num_of_nat m + num_of_nat n"  | 
|
199  | 
by (induct n) (auto simp add: add_One add_One_commute add_inc)  | 
|
200  | 
||
201  | 
text {* A double-and-decrement function *}
 | 
|
202  | 
||
203  | 
primrec BitM :: "num \<Rightarrow> num" where  | 
|
204  | 
"BitM One = One" |  | 
|
205  | 
"BitM (Bit0 n) = Bit1 (BitM n)" |  | 
|
206  | 
"BitM (Bit1 n) = Bit1 (Bit0 n)"  | 
|
207  | 
||
208  | 
lemma BitM_plus_one: "BitM n + One = Bit0 n"  | 
|
209  | 
by (induct n) simp_all  | 
|
210  | 
||
211  | 
lemma one_plus_BitM: "One + BitM n = Bit0 n"  | 
|
212  | 
unfolding add_One_commute BitM_plus_one ..  | 
|
213  | 
||
214  | 
text {* Squaring and exponentiation *}
 | 
|
215  | 
||
216  | 
primrec sqr :: "num \<Rightarrow> num" where  | 
|
217  | 
"sqr One = One" |  | 
|
218  | 
"sqr (Bit0 n) = Bit0 (Bit0 (sqr n))" |  | 
|
219  | 
"sqr (Bit1 n) = Bit1 (Bit0 (sqr n + n))"  | 
|
220  | 
||
221  | 
primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where  | 
|
222  | 
"pow x One = x" |  | 
|
223  | 
"pow x (Bit0 y) = sqr (pow x y)" |  | 
|
| 47191 | 224  | 
"pow x (Bit1 y) = sqr (pow x y) * x"  | 
| 47108 | 225  | 
|
226  | 
lemma nat_of_num_sqr: "nat_of_num (sqr x) = nat_of_num x * nat_of_num x"  | 
|
227  | 
by (induct x, simp_all add: algebra_simps nat_of_num_add)  | 
|
228  | 
||
229  | 
lemma sqr_conv_mult: "sqr x = x * x"  | 
|
230  | 
by (simp add: num_eq_iff nat_of_num_sqr nat_of_num_mult)  | 
|
231  | 
||
232  | 
||
| 47211 | 233  | 
subsection {* Binary numerals *}
 | 
| 47108 | 234  | 
|
235  | 
text {*
 | 
|
| 47211 | 236  | 
We embed binary representations into a generic algebraic  | 
| 47108 | 237  | 
  structure using @{text numeral}.
 | 
238  | 
*}  | 
|
239  | 
||
240  | 
class numeral = one + semigroup_add  | 
|
241  | 
begin  | 
|
242  | 
||
243  | 
primrec numeral :: "num \<Rightarrow> 'a" where  | 
|
244  | 
numeral_One: "numeral One = 1" |  | 
|
245  | 
numeral_Bit0: "numeral (Bit0 n) = numeral n + numeral n" |  | 
|
246  | 
numeral_Bit1: "numeral (Bit1 n) = numeral n + numeral n + 1"  | 
|
247  | 
||
| 50817 | 248  | 
lemma numeral_code [code]:  | 
249  | 
"numeral One = 1"  | 
|
250  | 
"numeral (Bit0 n) = (let m = numeral n in m + m)"  | 
|
251  | 
"numeral (Bit1 n) = (let m = numeral n in m + m + 1)"  | 
|
252  | 
by (simp_all add: Let_def)  | 
|
253  | 
||
| 47108 | 254  | 
lemma one_plus_numeral_commute: "1 + numeral x = numeral x + 1"  | 
255  | 
apply (induct x)  | 
|
256  | 
apply simp  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
257  | 
apply (simp add: add.assoc [symmetric], simp add: add.assoc)  | 
| 
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
258  | 
apply (simp add: add.assoc [symmetric], simp add: add.assoc)  | 
| 47108 | 259  | 
done  | 
260  | 
||
261  | 
lemma numeral_inc: "numeral (inc x) = numeral x + 1"  | 
|
262  | 
proof (induct x)  | 
|
263  | 
case (Bit1 x)  | 
|
264  | 
have "numeral x + (1 + numeral x) + 1 = numeral x + (numeral x + 1) + 1"  | 
|
265  | 
by (simp only: one_plus_numeral_commute)  | 
|
266  | 
with Bit1 show ?case  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
267  | 
by (simp add: add.assoc)  | 
| 47108 | 268  | 
qed simp_all  | 
269  | 
||
270  | 
declare numeral.simps [simp del]  | 
|
271  | 
||
272  | 
abbreviation "Numeral1 \<equiv> numeral One"  | 
|
273  | 
||
274  | 
declare numeral_One [code_post]  | 
|
275  | 
||
276  | 
end  | 
|
277  | 
||
278  | 
text {* Numeral syntax. *}
 | 
|
279  | 
||
280  | 
syntax  | 
|
281  | 
  "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
 | 
|
282  | 
||
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
283  | 
ML_file "Tools/numeral.ML"  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
284  | 
|
| 47108 | 285  | 
parse_translation {*
 | 
| 52143 | 286  | 
let  | 
287  | 
    fun numeral_tr [(c as Const (@{syntax_const "_constrain"}, _)) $ t $ u] =
 | 
|
288  | 
c $ numeral_tr [t] $ u  | 
|
289  | 
| numeral_tr [Const (num, _)] =  | 
|
| 58421 | 290  | 
(Numeral.mk_number_syntax o #value o Lexicon.read_num) num  | 
| 52143 | 291  | 
      | numeral_tr ts = raise TERM ("numeral_tr", ts);
 | 
| 
55974
 
c835a9379026
more official const syntax: avoid educated guessing by Syntax_Phases.decode_term;
 
wenzelm 
parents: 
55534 
diff
changeset
 | 
292  | 
  in [(@{syntax_const "_Numeral"}, K numeral_tr)] end
 | 
| 47108 | 293  | 
*}  | 
294  | 
||
| 52143 | 295  | 
typed_print_translation {*
 | 
296  | 
let  | 
|
297  | 
    fun dest_num (Const (@{const_syntax Bit0}, _) $ n) = 2 * dest_num n
 | 
|
298  | 
      | dest_num (Const (@{const_syntax Bit1}, _) $ n) = 2 * dest_num n + 1
 | 
|
299  | 
      | dest_num (Const (@{const_syntax One}, _)) = 1;
 | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
300  | 
fun num_tr' ctxt T [n] =  | 
| 52143 | 301  | 
let  | 
302  | 
val k = dest_num n;  | 
|
| 52187 | 303  | 
val t' =  | 
304  | 
          Syntax.const @{syntax_const "_Numeral"} $
 | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
305  | 
Syntax.free (string_of_int k);  | 
| 52143 | 306  | 
in  | 
307  | 
(case T of  | 
|
308  | 
          Type (@{type_name fun}, [_, T']) =>
 | 
|
| 
52210
 
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
 
wenzelm 
parents: 
52187 
diff
changeset
 | 
309  | 
if Printer.type_emphasis ctxt T' then  | 
| 
 
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
 
wenzelm 
parents: 
52187 
diff
changeset
 | 
310  | 
              Syntax.const @{syntax_const "_constrain"} $ t' $
 | 
| 
 
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
 
wenzelm 
parents: 
52187 
diff
changeset
 | 
311  | 
Syntax_Phases.term_of_typ ctxt T'  | 
| 
 
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
 
wenzelm 
parents: 
52187 
diff
changeset
 | 
312  | 
else t'  | 
| 52187 | 313  | 
| _ => if T = dummyT then t' else raise Match)  | 
| 52143 | 314  | 
end;  | 
315  | 
in  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
316  | 
   [(@{const_syntax numeral}, num_tr')]
 | 
| 52143 | 317  | 
end  | 
| 47108 | 318  | 
*}  | 
319  | 
||
| 47228 | 320  | 
|
| 47108 | 321  | 
subsection {* Class-specific numeral rules *}
 | 
322  | 
||
323  | 
text {*
 | 
|
324  | 
  @{const numeral} is a morphism.
 | 
|
325  | 
*}  | 
|
326  | 
||
327  | 
subsubsection {* Structures with addition: class @{text numeral} *}
 | 
|
328  | 
||
329  | 
context numeral  | 
|
330  | 
begin  | 
|
331  | 
||
332  | 
lemma numeral_add: "numeral (m + n) = numeral m + numeral n"  | 
|
333  | 
by (induct n rule: num_induct)  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
334  | 
(simp_all only: numeral_One add_One add_inc numeral_inc add.assoc)  | 
| 47108 | 335  | 
|
336  | 
lemma numeral_plus_numeral: "numeral m + numeral n = numeral (m + n)"  | 
|
337  | 
by (rule numeral_add [symmetric])  | 
|
338  | 
||
339  | 
lemma numeral_plus_one: "numeral n + 1 = numeral (n + One)"  | 
|
340  | 
using numeral_add [of n One] by (simp add: numeral_One)  | 
|
341  | 
||
342  | 
lemma one_plus_numeral: "1 + numeral n = numeral (One + n)"  | 
|
343  | 
using numeral_add [of One n] by (simp add: numeral_One)  | 
|
344  | 
||
345  | 
lemma one_add_one: "1 + 1 = 2"  | 
|
346  | 
using numeral_add [of One One] by (simp add: numeral_One)  | 
|
347  | 
||
348  | 
lemmas add_numeral_special =  | 
|
349  | 
numeral_plus_one one_plus_numeral one_add_one  | 
|
350  | 
||
351  | 
end  | 
|
352  | 
||
353  | 
subsubsection {*
 | 
|
354  | 
  Structures with negation: class @{text neg_numeral}
 | 
|
355  | 
*}  | 
|
356  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
357  | 
class neg_numeral = numeral + group_add  | 
| 47108 | 358  | 
begin  | 
359  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
360  | 
lemma uminus_numeral_One:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
361  | 
"- Numeral1 = - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
362  | 
by (simp add: numeral_One)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
363  | 
|
| 47108 | 364  | 
text {* Numerals form an abelian subgroup. *}
 | 
365  | 
||
366  | 
inductive is_num :: "'a \<Rightarrow> bool" where  | 
|
367  | 
"is_num 1" |  | 
|
368  | 
"is_num x \<Longrightarrow> is_num (- x)" |  | 
|
369  | 
"\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> is_num (x + y)"  | 
|
370  | 
||
371  | 
lemma is_num_numeral: "is_num (numeral k)"  | 
|
372  | 
by (induct k, simp_all add: numeral.simps is_num.intros)  | 
|
373  | 
||
374  | 
lemma is_num_add_commute:  | 
|
375  | 
"\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + y = y + x"  | 
|
376  | 
apply (induct x rule: is_num.induct)  | 
|
377  | 
apply (induct y rule: is_num.induct)  | 
|
378  | 
apply simp  | 
|
379  | 
apply (rule_tac a=x in add_left_imp_eq)  | 
|
380  | 
apply (rule_tac a=x in add_right_imp_eq)  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
381  | 
apply (simp add: add.assoc)  | 
| 
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
382  | 
apply (simp add: add.assoc [symmetric], simp add: add.assoc)  | 
| 47108 | 383  | 
apply (rule_tac a=x in add_left_imp_eq)  | 
384  | 
apply (rule_tac a=x in add_right_imp_eq)  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
385  | 
apply (simp add: add.assoc)  | 
| 
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
386  | 
apply (simp add: add.assoc, simp add: add.assoc [symmetric])  | 
| 47108 | 387  | 
done  | 
388  | 
||
389  | 
lemma is_num_add_left_commute:  | 
|
390  | 
"\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + (y + z) = y + (x + z)"  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
391  | 
by (simp only: add.assoc [symmetric] is_num_add_commute)  | 
| 47108 | 392  | 
|
393  | 
lemmas is_num_normalize =  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
394  | 
add.assoc is_num_add_commute is_num_add_left_commute  | 
| 47108 | 395  | 
is_num.intros is_num_numeral  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53064 
diff
changeset
 | 
396  | 
minus_add  | 
| 47108 | 397  | 
|
398  | 
definition dbl :: "'a \<Rightarrow> 'a" where "dbl x = x + x"  | 
|
399  | 
definition dbl_inc :: "'a \<Rightarrow> 'a" where "dbl_inc x = x + x + 1"  | 
|
400  | 
definition dbl_dec :: "'a \<Rightarrow> 'a" where "dbl_dec x = x + x - 1"  | 
|
401  | 
||
402  | 
definition sub :: "num \<Rightarrow> num \<Rightarrow> 'a" where  | 
|
403  | 
"sub k l = numeral k - numeral l"  | 
|
404  | 
||
405  | 
lemma numeral_BitM: "numeral (BitM n) = numeral (Bit0 n) - 1"  | 
|
406  | 
by (simp only: BitM_plus_one [symmetric] numeral_add numeral_One eq_diff_eq)  | 
|
407  | 
||
408  | 
lemma dbl_simps [simp]:  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
409  | 
"dbl (- numeral k) = - dbl (numeral k)"  | 
| 47108 | 410  | 
"dbl 0 = 0"  | 
411  | 
"dbl 1 = 2"  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
412  | 
"dbl (- 1) = - 2"  | 
| 47108 | 413  | 
"dbl (numeral k) = numeral (Bit0 k)"  | 
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
414  | 
by (simp_all add: dbl_def numeral.simps minus_add)  | 
| 47108 | 415  | 
|
416  | 
lemma dbl_inc_simps [simp]:  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
417  | 
"dbl_inc (- numeral k) = - dbl_dec (numeral k)"  | 
| 47108 | 418  | 
"dbl_inc 0 = 1"  | 
419  | 
"dbl_inc 1 = 3"  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
420  | 
"dbl_inc (- 1) = - 1"  | 
| 47108 | 421  | 
"dbl_inc (numeral k) = numeral (Bit1 k)"  | 
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
422  | 
by (simp_all add: dbl_inc_def dbl_dec_def numeral.simps numeral_BitM is_num_normalize algebra_simps del: add_uminus_conv_diff)  | 
| 47108 | 423  | 
|
424  | 
lemma dbl_dec_simps [simp]:  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
425  | 
"dbl_dec (- numeral k) = - dbl_inc (numeral k)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
426  | 
"dbl_dec 0 = - 1"  | 
| 47108 | 427  | 
"dbl_dec 1 = 1"  | 
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
428  | 
"dbl_dec (- 1) = - 3"  | 
| 47108 | 429  | 
"dbl_dec (numeral k) = numeral (BitM k)"  | 
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
430  | 
by (simp_all add: dbl_dec_def dbl_inc_def numeral.simps numeral_BitM is_num_normalize)  | 
| 47108 | 431  | 
|
432  | 
lemma sub_num_simps [simp]:  | 
|
433  | 
"sub One One = 0"  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
434  | 
"sub One (Bit0 l) = - numeral (BitM l)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
435  | 
"sub One (Bit1 l) = - numeral (Bit0 l)"  | 
| 47108 | 436  | 
"sub (Bit0 k) One = numeral (BitM k)"  | 
437  | 
"sub (Bit1 k) One = numeral (Bit0 k)"  | 
|
438  | 
"sub (Bit0 k) (Bit0 l) = dbl (sub k l)"  | 
|
439  | 
"sub (Bit0 k) (Bit1 l) = dbl_dec (sub k l)"  | 
|
440  | 
"sub (Bit1 k) (Bit0 l) = dbl_inc (sub k l)"  | 
|
441  | 
"sub (Bit1 k) (Bit1 l) = dbl (sub k l)"  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
442  | 
by (simp_all add: dbl_def dbl_dec_def dbl_inc_def sub_def numeral.simps  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53064 
diff
changeset
 | 
443  | 
numeral_BitM is_num_normalize del: add_uminus_conv_diff add: diff_conv_add_uminus)  | 
| 47108 | 444  | 
|
445  | 
lemma add_neg_numeral_simps:  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
446  | 
"numeral m + - numeral n = sub m n"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
447  | 
"- numeral m + numeral n = sub n m"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
448  | 
"- numeral m + - numeral n = - (numeral m + numeral n)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
449  | 
by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53064 
diff
changeset
 | 
450  | 
del: add_uminus_conv_diff add: diff_conv_add_uminus)  | 
| 47108 | 451  | 
|
452  | 
lemma add_neg_numeral_special:  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
453  | 
"1 + - numeral m = sub One m"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
454  | 
"- numeral m + 1 = sub One m"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
455  | 
"numeral m + - 1 = sub m One"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
456  | 
"- 1 + numeral n = sub n One"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
457  | 
"- 1 + - numeral n = - numeral (inc n)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
458  | 
"- numeral m + - 1 = - numeral (inc m)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
459  | 
"1 + - 1 = 0"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
460  | 
"- 1 + 1 = 0"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
461  | 
"- 1 + - 1 = - 2"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
462  | 
by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize right_minus numeral_inc  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
463  | 
del: add_uminus_conv_diff add: diff_conv_add_uminus)  | 
| 47108 | 464  | 
|
465  | 
lemma diff_numeral_simps:  | 
|
466  | 
"numeral m - numeral n = sub m n"  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
467  | 
"numeral m - - numeral n = numeral (m + n)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
468  | 
"- numeral m - numeral n = - numeral (m + n)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
469  | 
"- numeral m - - numeral n = sub n m"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
470  | 
by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53064 
diff
changeset
 | 
471  | 
del: add_uminus_conv_diff add: diff_conv_add_uminus)  | 
| 47108 | 472  | 
|
473  | 
lemma diff_numeral_special:  | 
|
474  | 
"1 - numeral n = sub One n"  | 
|
475  | 
"numeral m - 1 = sub m One"  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
476  | 
"1 - - numeral n = numeral (One + n)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
477  | 
"- numeral m - 1 = - numeral (m + One)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
478  | 
"- 1 - numeral n = - numeral (inc n)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
479  | 
"numeral m - - 1 = numeral (inc m)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
480  | 
"- 1 - - numeral n = sub n One"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
481  | 
"- numeral m - - 1 = sub One m"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
482  | 
"1 - 1 = 0"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
483  | 
"- 1 - 1 = - 2"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
484  | 
"1 - - 1 = 2"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
485  | 
"- 1 - - 1 = 0"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
486  | 
by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize numeral_inc  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
487  | 
del: add_uminus_conv_diff add: diff_conv_add_uminus)  | 
| 47108 | 488  | 
|
489  | 
end  | 
|
490  | 
||
491  | 
subsubsection {*
 | 
|
492  | 
  Structures with multiplication: class @{text semiring_numeral}
 | 
|
493  | 
*}  | 
|
494  | 
||
495  | 
class semiring_numeral = semiring + monoid_mult  | 
|
496  | 
begin  | 
|
497  | 
||
498  | 
subclass numeral ..  | 
|
499  | 
||
500  | 
lemma numeral_mult: "numeral (m * n) = numeral m * numeral n"  | 
|
501  | 
apply (induct n rule: num_induct)  | 
|
502  | 
apply (simp add: numeral_One)  | 
|
| 
49962
 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 
webertj 
parents: 
49690 
diff
changeset
 | 
503  | 
apply (simp add: mult_inc numeral_inc numeral_add distrib_left)  | 
| 47108 | 504  | 
done  | 
505  | 
||
506  | 
lemma numeral_times_numeral: "numeral m * numeral n = numeral (m * n)"  | 
|
507  | 
by (rule numeral_mult [symmetric])  | 
|
508  | 
||
| 53064 | 509  | 
lemma mult_2: "2 * z = z + z"  | 
510  | 
unfolding one_add_one [symmetric] distrib_right by simp  | 
|
511  | 
||
512  | 
lemma mult_2_right: "z * 2 = z + z"  | 
|
513  | 
unfolding one_add_one [symmetric] distrib_left by simp  | 
|
514  | 
||
| 47108 | 515  | 
end  | 
516  | 
||
517  | 
subsubsection {*
 | 
|
518  | 
  Structures with a zero: class @{text semiring_1}
 | 
|
519  | 
*}  | 
|
520  | 
||
521  | 
context semiring_1  | 
|
522  | 
begin  | 
|
523  | 
||
524  | 
subclass semiring_numeral ..  | 
|
525  | 
||
526  | 
lemma of_nat_numeral [simp]: "of_nat (numeral n) = numeral n"  | 
|
527  | 
by (induct n,  | 
|
528  | 
simp_all only: numeral.simps numeral_class.numeral.simps of_nat_add of_nat_1)  | 
|
529  | 
||
530  | 
end  | 
|
531  | 
||
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
50817 
diff
changeset
 | 
532  | 
lemma nat_of_num_numeral [code_abbrev]:  | 
| 
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
50817 
diff
changeset
 | 
533  | 
"nat_of_num = numeral"  | 
| 47108 | 534  | 
proof  | 
535  | 
fix n  | 
|
536  | 
have "numeral n = nat_of_num n"  | 
|
537  | 
by (induct n) (simp_all add: numeral.simps)  | 
|
538  | 
then show "nat_of_num n = numeral n" by simp  | 
|
539  | 
qed  | 
|
540  | 
||
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
50817 
diff
changeset
 | 
541  | 
lemma nat_of_num_code [code]:  | 
| 
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
50817 
diff
changeset
 | 
542  | 
"nat_of_num One = 1"  | 
| 
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
50817 
diff
changeset
 | 
543  | 
"nat_of_num (Bit0 n) = (let m = nat_of_num n in m + m)"  | 
| 
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
50817 
diff
changeset
 | 
544  | 
"nat_of_num (Bit1 n) = (let m = nat_of_num n in Suc (m + m))"  | 
| 
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
50817 
diff
changeset
 | 
545  | 
by (simp_all add: Let_def)  | 
| 
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
50817 
diff
changeset
 | 
546  | 
|
| 47108 | 547  | 
subsubsection {*
 | 
548  | 
  Equality: class @{text semiring_char_0}
 | 
|
549  | 
*}  | 
|
550  | 
||
551  | 
context semiring_char_0  | 
|
552  | 
begin  | 
|
553  | 
||
554  | 
lemma numeral_eq_iff: "numeral m = numeral n \<longleftrightarrow> m = n"  | 
|
555  | 
unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]  | 
|
556  | 
of_nat_eq_iff num_eq_iff ..  | 
|
557  | 
||
558  | 
lemma numeral_eq_one_iff: "numeral n = 1 \<longleftrightarrow> n = One"  | 
|
559  | 
by (rule numeral_eq_iff [of n One, unfolded numeral_One])  | 
|
560  | 
||
561  | 
lemma one_eq_numeral_iff: "1 = numeral n \<longleftrightarrow> One = n"  | 
|
562  | 
by (rule numeral_eq_iff [of One n, unfolded numeral_One])  | 
|
563  | 
||
564  | 
lemma numeral_neq_zero: "numeral n \<noteq> 0"  | 
|
565  | 
unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]  | 
|
566  | 
by (simp add: nat_of_num_pos)  | 
|
567  | 
||
568  | 
lemma zero_neq_numeral: "0 \<noteq> numeral n"  | 
|
569  | 
unfolding eq_commute [of 0] by (rule numeral_neq_zero)  | 
|
570  | 
||
571  | 
lemmas eq_numeral_simps [simp] =  | 
|
572  | 
numeral_eq_iff  | 
|
573  | 
numeral_eq_one_iff  | 
|
574  | 
one_eq_numeral_iff  | 
|
575  | 
numeral_neq_zero  | 
|
576  | 
zero_neq_numeral  | 
|
577  | 
||
578  | 
end  | 
|
579  | 
||
580  | 
subsubsection {*
 | 
|
581  | 
  Comparisons: class @{text linordered_semidom}
 | 
|
582  | 
*}  | 
|
583  | 
||
584  | 
text {*  Could be perhaps more general than here. *}
 | 
|
585  | 
||
586  | 
context linordered_semidom  | 
|
587  | 
begin  | 
|
588  | 
||
589  | 
lemma numeral_le_iff: "numeral m \<le> numeral n \<longleftrightarrow> m \<le> n"  | 
|
590  | 
proof -  | 
|
591  | 
have "of_nat (numeral m) \<le> of_nat (numeral n) \<longleftrightarrow> m \<le> n"  | 
|
592  | 
unfolding less_eq_num_def nat_of_num_numeral of_nat_le_iff ..  | 
|
593  | 
then show ?thesis by simp  | 
|
594  | 
qed  | 
|
595  | 
||
596  | 
lemma one_le_numeral: "1 \<le> numeral n"  | 
|
597  | 
using numeral_le_iff [of One n] by (simp add: numeral_One)  | 
|
598  | 
||
599  | 
lemma numeral_le_one_iff: "numeral n \<le> 1 \<longleftrightarrow> n \<le> One"  | 
|
600  | 
using numeral_le_iff [of n One] by (simp add: numeral_One)  | 
|
601  | 
||
602  | 
lemma numeral_less_iff: "numeral m < numeral n \<longleftrightarrow> m < n"  | 
|
603  | 
proof -  | 
|
604  | 
have "of_nat (numeral m) < of_nat (numeral n) \<longleftrightarrow> m < n"  | 
|
605  | 
unfolding less_num_def nat_of_num_numeral of_nat_less_iff ..  | 
|
606  | 
then show ?thesis by simp  | 
|
607  | 
qed  | 
|
608  | 
||
609  | 
lemma not_numeral_less_one: "\<not> numeral n < 1"  | 
|
610  | 
using numeral_less_iff [of n One] by (simp add: numeral_One)  | 
|
611  | 
||
612  | 
lemma one_less_numeral_iff: "1 < numeral n \<longleftrightarrow> One < n"  | 
|
613  | 
using numeral_less_iff [of One n] by (simp add: numeral_One)  | 
|
614  | 
||
615  | 
lemma zero_le_numeral: "0 \<le> numeral n"  | 
|
616  | 
by (induct n) (simp_all add: numeral.simps)  | 
|
617  | 
||
618  | 
lemma zero_less_numeral: "0 < numeral n"  | 
|
619  | 
by (induct n) (simp_all add: numeral.simps add_pos_pos)  | 
|
620  | 
||
621  | 
lemma not_numeral_le_zero: "\<not> numeral n \<le> 0"  | 
|
622  | 
by (simp add: not_le zero_less_numeral)  | 
|
623  | 
||
624  | 
lemma not_numeral_less_zero: "\<not> numeral n < 0"  | 
|
625  | 
by (simp add: not_less zero_le_numeral)  | 
|
626  | 
||
627  | 
lemmas le_numeral_extra =  | 
|
628  | 
zero_le_one not_one_le_zero  | 
|
629  | 
order_refl [of 0] order_refl [of 1]  | 
|
630  | 
||
631  | 
lemmas less_numeral_extra =  | 
|
632  | 
zero_less_one not_one_less_zero  | 
|
633  | 
less_irrefl [of 0] less_irrefl [of 1]  | 
|
634  | 
||
635  | 
lemmas le_numeral_simps [simp] =  | 
|
636  | 
numeral_le_iff  | 
|
637  | 
one_le_numeral  | 
|
638  | 
numeral_le_one_iff  | 
|
639  | 
zero_le_numeral  | 
|
640  | 
not_numeral_le_zero  | 
|
641  | 
||
642  | 
lemmas less_numeral_simps [simp] =  | 
|
643  | 
numeral_less_iff  | 
|
644  | 
one_less_numeral_iff  | 
|
645  | 
not_numeral_less_one  | 
|
646  | 
zero_less_numeral  | 
|
647  | 
not_numeral_less_zero  | 
|
648  | 
||
649  | 
end  | 
|
650  | 
||
651  | 
subsubsection {*
 | 
|
652  | 
  Multiplication and negation: class @{text ring_1}
 | 
|
653  | 
*}  | 
|
654  | 
||
655  | 
context ring_1  | 
|
656  | 
begin  | 
|
657  | 
||
658  | 
subclass neg_numeral ..  | 
|
659  | 
||
660  | 
lemma mult_neg_numeral_simps:  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
661  | 
"- numeral m * - numeral n = numeral (m * n)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
662  | 
"- numeral m * numeral n = - numeral (m * n)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
663  | 
"numeral m * - numeral n = - numeral (m * n)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
664  | 
unfolding mult_minus_left mult_minus_right  | 
| 47108 | 665  | 
by (simp_all only: minus_minus numeral_mult)  | 
666  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
667  | 
lemma mult_minus1 [simp]: "- 1 * z = - z"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
668  | 
unfolding numeral.simps mult_minus_left by simp  | 
| 47108 | 669  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
670  | 
lemma mult_minus1_right [simp]: "z * - 1 = - z"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
671  | 
unfolding numeral.simps mult_minus_right by simp  | 
| 47108 | 672  | 
|
673  | 
end  | 
|
674  | 
||
675  | 
subsubsection {*
 | 
|
676  | 
  Equality using @{text iszero} for rings with non-zero characteristic
 | 
|
677  | 
*}  | 
|
678  | 
||
679  | 
context ring_1  | 
|
680  | 
begin  | 
|
681  | 
||
682  | 
definition iszero :: "'a \<Rightarrow> bool"  | 
|
683  | 
where "iszero z \<longleftrightarrow> z = 0"  | 
|
684  | 
||
685  | 
lemma iszero_0 [simp]: "iszero 0"  | 
|
686  | 
by (simp add: iszero_def)  | 
|
687  | 
||
688  | 
lemma not_iszero_1 [simp]: "\<not> iszero 1"  | 
|
689  | 
by (simp add: iszero_def)  | 
|
690  | 
||
691  | 
lemma not_iszero_Numeral1: "\<not> iszero Numeral1"  | 
|
692  | 
by (simp add: numeral_One)  | 
|
693  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
694  | 
lemma not_iszero_neg_1 [simp]: "\<not> iszero (- 1)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
695  | 
by (simp add: iszero_def)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
696  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
697  | 
lemma not_iszero_neg_Numeral1: "\<not> iszero (- Numeral1)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
698  | 
by (simp add: numeral_One)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
699  | 
|
| 47108 | 700  | 
lemma iszero_neg_numeral [simp]:  | 
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
701  | 
"iszero (- numeral w) \<longleftrightarrow> iszero (numeral w)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
702  | 
unfolding iszero_def  | 
| 47108 | 703  | 
by (rule neg_equal_0_iff_equal)  | 
704  | 
||
705  | 
lemma eq_iff_iszero_diff: "x = y \<longleftrightarrow> iszero (x - y)"  | 
|
706  | 
unfolding iszero_def by (rule eq_iff_diff_eq_0)  | 
|
707  | 
||
708  | 
text {* The @{text "eq_numeral_iff_iszero"} lemmas are not declared
 | 
|
709  | 
@{text "[simp]"} by default, because for rings of characteristic zero,
 | 
|
710  | 
better simp rules are possible. For a type like integers mod @{text
 | 
|
711  | 
"n"}, type-instantiated versions of these rules should be added to the  | 
|
712  | 
simplifier, along with a type-specific rule for deciding propositions  | 
|
713  | 
of the form @{text "iszero (numeral w)"}.
 | 
|
714  | 
||
715  | 
bh: Maybe it would not be so bad to just declare these as simp  | 
|
716  | 
rules anyway? I should test whether these rules take precedence over  | 
|
717  | 
the @{text "ring_char_0"} rules in the simplifier.
 | 
|
718  | 
*}  | 
|
719  | 
||
720  | 
lemma eq_numeral_iff_iszero:  | 
|
721  | 
"numeral x = numeral y \<longleftrightarrow> iszero (sub x y)"  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
722  | 
"numeral x = - numeral y \<longleftrightarrow> iszero (numeral (x + y))"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
723  | 
"- numeral x = numeral y \<longleftrightarrow> iszero (numeral (x + y))"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
724  | 
"- numeral x = - numeral y \<longleftrightarrow> iszero (sub y x)"  | 
| 47108 | 725  | 
"numeral x = 1 \<longleftrightarrow> iszero (sub x One)"  | 
726  | 
"1 = numeral y \<longleftrightarrow> iszero (sub One y)"  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
727  | 
"- numeral x = 1 \<longleftrightarrow> iszero (numeral (x + One))"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
728  | 
"1 = - numeral y \<longleftrightarrow> iszero (numeral (One + y))"  | 
| 47108 | 729  | 
"numeral x = 0 \<longleftrightarrow> iszero (numeral x)"  | 
730  | 
"0 = numeral y \<longleftrightarrow> iszero (numeral y)"  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
731  | 
"- numeral x = 0 \<longleftrightarrow> iszero (numeral x)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
732  | 
"0 = - numeral y \<longleftrightarrow> iszero (numeral y)"  | 
| 47108 | 733  | 
unfolding eq_iff_iszero_diff diff_numeral_simps diff_numeral_special  | 
734  | 
by simp_all  | 
|
735  | 
||
736  | 
end  | 
|
737  | 
||
738  | 
subsubsection {*
 | 
|
739  | 
  Equality and negation: class @{text ring_char_0}
 | 
|
740  | 
*}  | 
|
741  | 
||
742  | 
class ring_char_0 = ring_1 + semiring_char_0  | 
|
743  | 
begin  | 
|
744  | 
||
745  | 
lemma not_iszero_numeral [simp]: "\<not> iszero (numeral w)"  | 
|
746  | 
by (simp add: iszero_def)  | 
|
747  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
748  | 
lemma neg_numeral_eq_iff: "- numeral m = - numeral n \<longleftrightarrow> m = n"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
749  | 
by simp  | 
| 47108 | 750  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
751  | 
lemma numeral_neq_neg_numeral: "numeral m \<noteq> - numeral n"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
752  | 
unfolding eq_neg_iff_add_eq_0  | 
| 47108 | 753  | 
by (simp add: numeral_plus_numeral)  | 
754  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
755  | 
lemma neg_numeral_neq_numeral: "- numeral m \<noteq> numeral n"  | 
| 47108 | 756  | 
by (rule numeral_neq_neg_numeral [symmetric])  | 
757  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
758  | 
lemma zero_neq_neg_numeral: "0 \<noteq> - numeral n"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
759  | 
unfolding neg_0_equal_iff_equal by simp  | 
| 47108 | 760  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
761  | 
lemma neg_numeral_neq_zero: "- numeral n \<noteq> 0"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
762  | 
unfolding neg_equal_0_iff_equal by simp  | 
| 47108 | 763  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
764  | 
lemma one_neq_neg_numeral: "1 \<noteq> - numeral n"  | 
| 47108 | 765  | 
using numeral_neq_neg_numeral [of One n] by (simp add: numeral_One)  | 
766  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
767  | 
lemma neg_numeral_neq_one: "- numeral n \<noteq> 1"  | 
| 47108 | 768  | 
using neg_numeral_neq_numeral [of n One] by (simp add: numeral_One)  | 
769  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
770  | 
lemma neg_one_neq_numeral:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
771  | 
"- 1 \<noteq> numeral n"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
772  | 
using neg_numeral_neq_numeral [of One n] by (simp add: numeral_One)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
773  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
774  | 
lemma numeral_neq_neg_one:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
775  | 
"numeral n \<noteq> - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
776  | 
using numeral_neq_neg_numeral [of n One] by (simp add: numeral_One)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
777  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
778  | 
lemma neg_one_eq_numeral_iff:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
779  | 
"- 1 = - numeral n \<longleftrightarrow> n = One"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
780  | 
using neg_numeral_eq_iff [of One n] by (auto simp add: numeral_One)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
781  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
782  | 
lemma numeral_eq_neg_one_iff:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
783  | 
"- numeral n = - 1 \<longleftrightarrow> n = One"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
784  | 
using neg_numeral_eq_iff [of n One] by (auto simp add: numeral_One)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
785  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
786  | 
lemma neg_one_neq_zero:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
787  | 
"- 1 \<noteq> 0"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
788  | 
by simp  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
789  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
790  | 
lemma zero_neq_neg_one:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
791  | 
"0 \<noteq> - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
792  | 
by simp  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
793  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
794  | 
lemma neg_one_neq_one:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
795  | 
"- 1 \<noteq> 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
796  | 
using neg_numeral_neq_numeral [of One One] by (simp only: numeral_One not_False_eq_True)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
797  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
798  | 
lemma one_neq_neg_one:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
799  | 
"1 \<noteq> - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
800  | 
using numeral_neq_neg_numeral [of One One] by (simp only: numeral_One not_False_eq_True)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
801  | 
|
| 47108 | 802  | 
lemmas eq_neg_numeral_simps [simp] =  | 
803  | 
neg_numeral_eq_iff  | 
|
804  | 
numeral_neq_neg_numeral neg_numeral_neq_numeral  | 
|
805  | 
one_neq_neg_numeral neg_numeral_neq_one  | 
|
806  | 
zero_neq_neg_numeral neg_numeral_neq_zero  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
807  | 
neg_one_neq_numeral numeral_neq_neg_one  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
808  | 
neg_one_eq_numeral_iff numeral_eq_neg_one_iff  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
809  | 
neg_one_neq_zero zero_neq_neg_one  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
810  | 
neg_one_neq_one one_neq_neg_one  | 
| 47108 | 811  | 
|
812  | 
end  | 
|
813  | 
||
814  | 
subsubsection {*
 | 
|
815  | 
  Structures with negation and order: class @{text linordered_idom}
 | 
|
816  | 
*}  | 
|
817  | 
||
818  | 
context linordered_idom  | 
|
819  | 
begin  | 
|
820  | 
||
821  | 
subclass ring_char_0 ..  | 
|
822  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
823  | 
lemma neg_numeral_le_iff: "- numeral m \<le> - numeral n \<longleftrightarrow> n \<le> m"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
824  | 
by (simp only: neg_le_iff_le numeral_le_iff)  | 
| 47108 | 825  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
826  | 
lemma neg_numeral_less_iff: "- numeral m < - numeral n \<longleftrightarrow> n < m"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
827  | 
by (simp only: neg_less_iff_less numeral_less_iff)  | 
| 47108 | 828  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
829  | 
lemma neg_numeral_less_zero: "- numeral n < 0"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
830  | 
by (simp only: neg_less_0_iff_less zero_less_numeral)  | 
| 47108 | 831  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
832  | 
lemma neg_numeral_le_zero: "- numeral n \<le> 0"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
833  | 
by (simp only: neg_le_0_iff_le zero_le_numeral)  | 
| 47108 | 834  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
835  | 
lemma not_zero_less_neg_numeral: "\<not> 0 < - numeral n"  | 
| 47108 | 836  | 
by (simp only: not_less neg_numeral_le_zero)  | 
837  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
838  | 
lemma not_zero_le_neg_numeral: "\<not> 0 \<le> - numeral n"  | 
| 47108 | 839  | 
by (simp only: not_le neg_numeral_less_zero)  | 
840  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
841  | 
lemma neg_numeral_less_numeral: "- numeral m < numeral n"  | 
| 47108 | 842  | 
using neg_numeral_less_zero zero_less_numeral by (rule less_trans)  | 
843  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
844  | 
lemma neg_numeral_le_numeral: "- numeral m \<le> numeral n"  | 
| 47108 | 845  | 
by (simp only: less_imp_le neg_numeral_less_numeral)  | 
846  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
847  | 
lemma not_numeral_less_neg_numeral: "\<not> numeral m < - numeral n"  | 
| 47108 | 848  | 
by (simp only: not_less neg_numeral_le_numeral)  | 
849  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
850  | 
lemma not_numeral_le_neg_numeral: "\<not> numeral m \<le> - numeral n"  | 
| 47108 | 851  | 
by (simp only: not_le neg_numeral_less_numeral)  | 
852  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
853  | 
lemma neg_numeral_less_one: "- numeral m < 1"  | 
| 47108 | 854  | 
by (rule neg_numeral_less_numeral [of m One, unfolded numeral_One])  | 
855  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
856  | 
lemma neg_numeral_le_one: "- numeral m \<le> 1"  | 
| 47108 | 857  | 
by (rule neg_numeral_le_numeral [of m One, unfolded numeral_One])  | 
858  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
859  | 
lemma not_one_less_neg_numeral: "\<not> 1 < - numeral m"  | 
| 47108 | 860  | 
by (simp only: not_less neg_numeral_le_one)  | 
861  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
862  | 
lemma not_one_le_neg_numeral: "\<not> 1 \<le> - numeral m"  | 
| 47108 | 863  | 
by (simp only: not_le neg_numeral_less_one)  | 
864  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
865  | 
lemma not_numeral_less_neg_one: "\<not> numeral m < - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
866  | 
using not_numeral_less_neg_numeral [of m One] by (simp add: numeral_One)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
867  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
868  | 
lemma not_numeral_le_neg_one: "\<not> numeral m \<le> - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
869  | 
using not_numeral_le_neg_numeral [of m One] by (simp add: numeral_One)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
870  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
871  | 
lemma neg_one_less_numeral: "- 1 < numeral m"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
872  | 
using neg_numeral_less_numeral [of One m] by (simp add: numeral_One)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
873  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
874  | 
lemma neg_one_le_numeral: "- 1 \<le> numeral m"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
875  | 
using neg_numeral_le_numeral [of One m] by (simp add: numeral_One)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
876  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
877  | 
lemma neg_numeral_less_neg_one_iff: "- numeral m < - 1 \<longleftrightarrow> m \<noteq> One"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
878  | 
by (cases m) simp_all  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
879  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
880  | 
lemma neg_numeral_le_neg_one: "- numeral m \<le> - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
881  | 
by simp  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
882  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
883  | 
lemma not_neg_one_less_neg_numeral: "\<not> - 1 < - numeral m"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
884  | 
by simp  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
885  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
886  | 
lemma not_neg_one_le_neg_numeral_iff: "\<not> - 1 \<le> - numeral m \<longleftrightarrow> m \<noteq> One"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
887  | 
by (cases m) simp_all  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
888  | 
|
| 47108 | 889  | 
lemma sub_non_negative:  | 
890  | 
"sub n m \<ge> 0 \<longleftrightarrow> n \<ge> m"  | 
|
891  | 
by (simp only: sub_def le_diff_eq) simp  | 
|
892  | 
||
893  | 
lemma sub_positive:  | 
|
894  | 
"sub n m > 0 \<longleftrightarrow> n > m"  | 
|
895  | 
by (simp only: sub_def less_diff_eq) simp  | 
|
896  | 
||
897  | 
lemma sub_non_positive:  | 
|
898  | 
"sub n m \<le> 0 \<longleftrightarrow> n \<le> m"  | 
|
899  | 
by (simp only: sub_def diff_le_eq) simp  | 
|
900  | 
||
901  | 
lemma sub_negative:  | 
|
902  | 
"sub n m < 0 \<longleftrightarrow> n < m"  | 
|
903  | 
by (simp only: sub_def diff_less_eq) simp  | 
|
904  | 
||
905  | 
lemmas le_neg_numeral_simps [simp] =  | 
|
906  | 
neg_numeral_le_iff  | 
|
907  | 
neg_numeral_le_numeral not_numeral_le_neg_numeral  | 
|
908  | 
neg_numeral_le_zero not_zero_le_neg_numeral  | 
|
909  | 
neg_numeral_le_one not_one_le_neg_numeral  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
910  | 
neg_one_le_numeral not_numeral_le_neg_one  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
911  | 
neg_numeral_le_neg_one not_neg_one_le_neg_numeral_iff  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
912  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
913  | 
lemma le_minus_one_simps [simp]:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
914  | 
"- 1 \<le> 0"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
915  | 
"- 1 \<le> 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
916  | 
"\<not> 0 \<le> - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
917  | 
"\<not> 1 \<le> - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
918  | 
by simp_all  | 
| 47108 | 919  | 
|
920  | 
lemmas less_neg_numeral_simps [simp] =  | 
|
921  | 
neg_numeral_less_iff  | 
|
922  | 
neg_numeral_less_numeral not_numeral_less_neg_numeral  | 
|
923  | 
neg_numeral_less_zero not_zero_less_neg_numeral  | 
|
924  | 
neg_numeral_less_one not_one_less_neg_numeral  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
925  | 
neg_one_less_numeral not_numeral_less_neg_one  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
926  | 
neg_numeral_less_neg_one_iff not_neg_one_less_neg_numeral  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
927  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
928  | 
lemma less_minus_one_simps [simp]:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
929  | 
"- 1 < 0"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
930  | 
"- 1 < 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
931  | 
"\<not> 0 < - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
932  | 
"\<not> 1 < - 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
933  | 
by (simp_all add: less_le)  | 
| 47108 | 934  | 
|
935  | 
lemma abs_numeral [simp]: "abs (numeral n) = numeral n"  | 
|
936  | 
by simp  | 
|
937  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
938  | 
lemma abs_neg_numeral [simp]: "abs (- numeral n) = numeral n"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
939  | 
by (simp only: abs_minus_cancel abs_numeral)  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
940  | 
|
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
941  | 
lemma abs_neg_one [simp]:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
942  | 
"abs (- 1) = 1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
943  | 
by simp  | 
| 47108 | 944  | 
|
945  | 
end  | 
|
946  | 
||
947  | 
subsubsection {*
 | 
|
948  | 
Natural numbers  | 
|
949  | 
*}  | 
|
950  | 
||
| 47299 | 951  | 
lemma Suc_1 [simp]: "Suc 1 = 2"  | 
952  | 
unfolding Suc_eq_plus1 by (rule one_add_one)  | 
|
953  | 
||
| 47108 | 954  | 
lemma Suc_numeral [simp]: "Suc (numeral n) = numeral (n + One)"  | 
| 47299 | 955  | 
unfolding Suc_eq_plus1 by (rule numeral_plus_one)  | 
| 47108 | 956  | 
|
| 
47209
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
957  | 
definition pred_numeral :: "num \<Rightarrow> nat"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
958  | 
where [code del]: "pred_numeral k = numeral k - 1"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
959  | 
|
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
960  | 
lemma numeral_eq_Suc: "numeral k = Suc (pred_numeral k)"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
961  | 
unfolding pred_numeral_def by simp  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
962  | 
|
| 
47220
 
52426c62b5d0
replace lemmas eval_nat_numeral with a simpler reformulation
 
huffman 
parents: 
47218 
diff
changeset
 | 
963  | 
lemma eval_nat_numeral:  | 
| 47108 | 964  | 
"numeral One = Suc 0"  | 
965  | 
"numeral (Bit0 n) = Suc (numeral (BitM n))"  | 
|
966  | 
"numeral (Bit1 n) = Suc (numeral (Bit0 n))"  | 
|
967  | 
by (simp_all add: numeral.simps BitM_plus_one)  | 
|
968  | 
||
| 
47209
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
969  | 
lemma pred_numeral_simps [simp]:  | 
| 47300 | 970  | 
"pred_numeral One = 0"  | 
971  | 
"pred_numeral (Bit0 k) = numeral (BitM k)"  | 
|
972  | 
"pred_numeral (Bit1 k) = numeral (Bit0 k)"  | 
|
| 
47220
 
52426c62b5d0
replace lemmas eval_nat_numeral with a simpler reformulation
 
huffman 
parents: 
47218 
diff
changeset
 | 
973  | 
unfolding pred_numeral_def eval_nat_numeral  | 
| 
47209
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
974  | 
by (simp_all only: diff_Suc_Suc diff_0)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
975  | 
|
| 
47192
 
0c0501cb6da6
move many lemmas from Nat_Numeral.thy to Power.thy or Num.thy
 
huffman 
parents: 
47191 
diff
changeset
 | 
976  | 
lemma numeral_2_eq_2: "2 = Suc (Suc 0)"  | 
| 
47220
 
52426c62b5d0
replace lemmas eval_nat_numeral with a simpler reformulation
 
huffman 
parents: 
47218 
diff
changeset
 | 
977  | 
by (simp add: eval_nat_numeral)  | 
| 
47192
 
0c0501cb6da6
move many lemmas from Nat_Numeral.thy to Power.thy or Num.thy
 
huffman 
parents: 
47191 
diff
changeset
 | 
978  | 
|
| 
 
0c0501cb6da6
move many lemmas from Nat_Numeral.thy to Power.thy or Num.thy
 
huffman 
parents: 
47191 
diff
changeset
 | 
979  | 
lemma numeral_3_eq_3: "3 = Suc (Suc (Suc 0))"  | 
| 
47220
 
52426c62b5d0
replace lemmas eval_nat_numeral with a simpler reformulation
 
huffman 
parents: 
47218 
diff
changeset
 | 
980  | 
by (simp add: eval_nat_numeral)  | 
| 
47192
 
0c0501cb6da6
move many lemmas from Nat_Numeral.thy to Power.thy or Num.thy
 
huffman 
parents: 
47191 
diff
changeset
 | 
981  | 
|
| 
47207
 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 
huffman 
parents: 
47192 
diff
changeset
 | 
982  | 
lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"  | 
| 
 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 
huffman 
parents: 
47192 
diff
changeset
 | 
983  | 
by (simp only: numeral_One One_nat_def)  | 
| 
 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 
huffman 
parents: 
47192 
diff
changeset
 | 
984  | 
|
| 
 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 
huffman 
parents: 
47192 
diff
changeset
 | 
985  | 
lemma Suc_nat_number_of_add:  | 
| 47300 | 986  | 
"Suc (numeral v + n) = numeral (v + One) + n"  | 
| 
47207
 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 
huffman 
parents: 
47192 
diff
changeset
 | 
987  | 
by simp  | 
| 
 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 
huffman 
parents: 
47192 
diff
changeset
 | 
988  | 
|
| 
 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 
huffman 
parents: 
47192 
diff
changeset
 | 
989  | 
(*Maps #n to n for n = 1, 2*)  | 
| 
 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 
huffman 
parents: 
47192 
diff
changeset
 | 
990  | 
lemmas numerals = numeral_One [where 'a=nat] numeral_2_eq_2  | 
| 
 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 
huffman 
parents: 
47192 
diff
changeset
 | 
991  | 
|
| 
47209
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
992  | 
text {* Comparisons involving @{term Suc}. *}
 | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
993  | 
|
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
994  | 
lemma eq_numeral_Suc [simp]: "numeral k = Suc n \<longleftrightarrow> pred_numeral k = n"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
995  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
996  | 
|
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
997  | 
lemma Suc_eq_numeral [simp]: "Suc n = numeral k \<longleftrightarrow> n = pred_numeral k"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
998  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
999  | 
|
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1000  | 
lemma less_numeral_Suc [simp]: "numeral k < Suc n \<longleftrightarrow> pred_numeral k < n"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1001  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1002  | 
|
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1003  | 
lemma less_Suc_numeral [simp]: "Suc n < numeral k \<longleftrightarrow> n < pred_numeral k"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1004  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1005  | 
|
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1006  | 
lemma le_numeral_Suc [simp]: "numeral k \<le> Suc n \<longleftrightarrow> pred_numeral k \<le> n"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1007  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1008  | 
|
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1009  | 
lemma le_Suc_numeral [simp]: "Suc n \<le> numeral k \<longleftrightarrow> n \<le> pred_numeral k"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1010  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1011  | 
|
| 
47218
 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 
huffman 
parents: 
47216 
diff
changeset
 | 
1012  | 
lemma diff_Suc_numeral [simp]: "Suc n - numeral k = n - pred_numeral k"  | 
| 
 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 
huffman 
parents: 
47216 
diff
changeset
 | 
1013  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 
huffman 
parents: 
47216 
diff
changeset
 | 
1014  | 
|
| 
 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 
huffman 
parents: 
47216 
diff
changeset
 | 
1015  | 
lemma diff_numeral_Suc [simp]: "numeral k - Suc n = pred_numeral k - n"  | 
| 
 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 
huffman 
parents: 
47216 
diff
changeset
 | 
1016  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 
huffman 
parents: 
47216 
diff
changeset
 | 
1017  | 
|
| 
47209
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1018  | 
lemma max_Suc_numeral [simp]:  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1019  | 
"max (Suc n) (numeral k) = Suc (max n (pred_numeral k))"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1020  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1021  | 
|
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1022  | 
lemma max_numeral_Suc [simp]:  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1023  | 
"max (numeral k) (Suc n) = Suc (max (pred_numeral k) n)"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1024  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1025  | 
|
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1026  | 
lemma min_Suc_numeral [simp]:  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1027  | 
"min (Suc n) (numeral k) = Suc (min n (pred_numeral k))"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1028  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1029  | 
|
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1030  | 
lemma min_numeral_Suc [simp]:  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1031  | 
"min (numeral k) (Suc n) = Suc (min (pred_numeral k) n)"  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1032  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 
huffman 
parents: 
47207 
diff
changeset
 | 
1033  | 
|
| 55415 | 1034  | 
text {* For @{term case_nat} and @{term rec_nat}. *}
 | 
| 
47216
 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 
huffman 
parents: 
47211 
diff
changeset
 | 
1035  | 
|
| 55415 | 1036  | 
lemma case_nat_numeral [simp]:  | 
1037  | 
"case_nat a f (numeral v) = (let pv = pred_numeral v in f pv)"  | 
|
| 
47216
 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 
huffman 
parents: 
47211 
diff
changeset
 | 
1038  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 
huffman 
parents: 
47211 
diff
changeset
 | 
1039  | 
|
| 55415 | 1040  | 
lemma case_nat_add_eq_if [simp]:  | 
1041  | 
"case_nat a f ((numeral v) + n) = (let pv = pred_numeral v in f (pv + n))"  | 
|
| 
47216
 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 
huffman 
parents: 
47211 
diff
changeset
 | 
1042  | 
by (simp add: numeral_eq_Suc)  | 
| 
 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 
huffman 
parents: 
47211 
diff
changeset
 | 
1043  | 
|
| 55415 | 1044  | 
lemma rec_nat_numeral [simp]:  | 
1045  | 
"rec_nat a f (numeral v) =  | 
|
1046  | 
(let pv = pred_numeral v in f pv (rec_nat a f pv))"  | 
|
| 
47216
 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 
huffman 
parents: 
47211 
diff
changeset
 | 
1047  | 
by (simp add: numeral_eq_Suc Let_def)  | 
| 
 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 
huffman 
parents: 
47211 
diff
changeset
 | 
1048  | 
|
| 55415 | 1049  | 
lemma rec_nat_add_eq_if [simp]:  | 
1050  | 
"rec_nat a f (numeral v + n) =  | 
|
1051  | 
(let pv = pred_numeral v in f (pv + n) (rec_nat a f (pv + n)))"  | 
|
| 
47216
 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 
huffman 
parents: 
47211 
diff
changeset
 | 
1052  | 
by (simp add: numeral_eq_Suc Let_def)  | 
| 
 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 
huffman 
parents: 
47211 
diff
changeset
 | 
1053  | 
|
| 
47255
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1054  | 
text {* Case analysis on @{term "n < 2"} *}
 | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1055  | 
|
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1056  | 
lemma less_2_cases: "n < 2 \<Longrightarrow> n = 0 \<or> n = Suc 0"  | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1057  | 
by (auto simp add: numeral_2_eq_2)  | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1058  | 
|
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1059  | 
text {* Removal of Small Numerals: 0, 1 and (in additive positions) 2 *}
 | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1060  | 
text {* bh: Are these rules really a good idea? *}
 | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1061  | 
|
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1062  | 
lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"  | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1063  | 
by simp  | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1064  | 
|
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1065  | 
lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)"  | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1066  | 
by simp  | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1067  | 
|
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1068  | 
text {* Can be used to eliminate long strings of Sucs, but not by default. *}
 | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1069  | 
|
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1070  | 
lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"  | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1071  | 
by simp  | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1072  | 
|
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1073  | 
lemmas nat_1_add_1 = one_add_one [where 'a=nat] (* legacy *)  | 
| 
 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 
huffman 
parents: 
47228 
diff
changeset
 | 
1074  | 
|
| 47108 | 1075  | 
|
| 
58512
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1076  | 
subsection {* Particular lemmas concerning @{term 2} *}
 | 
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1077  | 
|
| 
59867
 
58043346ca64
given up separate type classes demanding `inverse 0 = 0`
 
haftmann 
parents: 
59621 
diff
changeset
 | 
1078  | 
context linordered_field  | 
| 
58512
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1079  | 
begin  | 
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1080  | 
|
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1081  | 
lemma half_gt_zero_iff:  | 
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1082  | 
"0 < a / 2 \<longleftrightarrow> 0 < a" (is "?P \<longleftrightarrow> ?Q")  | 
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1083  | 
by (auto simp add: field_simps)  | 
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1084  | 
|
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1085  | 
lemma half_gt_zero [simp]:  | 
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1086  | 
"0 < a \<Longrightarrow> 0 < a / 2"  | 
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1087  | 
by (simp add: half_gt_zero_iff)  | 
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1088  | 
|
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1089  | 
end  | 
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1090  | 
|
| 
 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 
haftmann 
parents: 
58421 
diff
changeset
 | 
1091  | 
|
| 47108 | 1092  | 
subsection {* Numeral equations as default simplification rules *}
 | 
1093  | 
||
1094  | 
declare (in numeral) numeral_One [simp]  | 
|
1095  | 
declare (in numeral) numeral_plus_numeral [simp]  | 
|
1096  | 
declare (in numeral) add_numeral_special [simp]  | 
|
1097  | 
declare (in neg_numeral) add_neg_numeral_simps [simp]  | 
|
1098  | 
declare (in neg_numeral) add_neg_numeral_special [simp]  | 
|
1099  | 
declare (in neg_numeral) diff_numeral_simps [simp]  | 
|
1100  | 
declare (in neg_numeral) diff_numeral_special [simp]  | 
|
1101  | 
declare (in semiring_numeral) numeral_times_numeral [simp]  | 
|
1102  | 
declare (in ring_1) mult_neg_numeral_simps [simp]  | 
|
1103  | 
||
1104  | 
subsection {* Setting up simprocs *}
 | 
|
1105  | 
||
1106  | 
lemma mult_numeral_1: "Numeral1 * a = (a::'a::semiring_numeral)"  | 
|
1107  | 
by simp  | 
|
1108  | 
||
1109  | 
lemma mult_numeral_1_right: "a * Numeral1 = (a::'a::semiring_numeral)"  | 
|
1110  | 
by simp  | 
|
1111  | 
||
1112  | 
lemma divide_numeral_1: "a / Numeral1 = (a::'a::field)"  | 
|
1113  | 
by simp  | 
|
1114  | 
||
1115  | 
lemma inverse_numeral_1:  | 
|
1116  | 
"inverse Numeral1 = (Numeral1::'a::division_ring)"  | 
|
1117  | 
by simp  | 
|
1118  | 
||
| 47211 | 1119  | 
text{*Theorem lists for the cancellation simprocs. The use of a binary
 | 
| 47108 | 1120  | 
numeral for 1 reduces the number of special cases.*}  | 
1121  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1122  | 
lemma mult_1s:  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1123  | 
fixes a :: "'a::semiring_numeral"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1124  | 
and b :: "'b::ring_1"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1125  | 
shows "Numeral1 * a = a"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1126  | 
"a * Numeral1 = a"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1127  | 
"- Numeral1 * b = - b"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1128  | 
"b * - Numeral1 = - b"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1129  | 
by simp_all  | 
| 47108 | 1130  | 
|
| 47226 | 1131  | 
setup {*
 | 
1132  | 
Reorient_Proc.add  | 
|
1133  | 
    (fn Const (@{const_name numeral}, _) $ _ => true
 | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1134  | 
    | Const (@{const_name uminus}, _) $ (Const (@{const_name numeral}, _) $ _) => true
 | 
| 47226 | 1135  | 
| _ => false)  | 
1136  | 
*}  | 
|
1137  | 
||
1138  | 
simproc_setup reorient_numeral  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1139  | 
  ("numeral w = x" | "- numeral w = y") = Reorient_Proc.proc
 | 
| 47226 | 1140  | 
|
| 47108 | 1141  | 
|
1142  | 
subsubsection {* Simplification of arithmetic operations on integer constants. *}
 | 
|
1143  | 
||
1144  | 
lemmas arith_special = (* already declared simp above *)  | 
|
1145  | 
add_numeral_special add_neg_numeral_special  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1146  | 
diff_numeral_special  | 
| 47108 | 1147  | 
|
1148  | 
(* rules already in simpset *)  | 
|
1149  | 
lemmas arith_extra_simps =  | 
|
1150  | 
numeral_plus_numeral add_neg_numeral_simps add_0_left add_0_right  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1151  | 
minus_zero  | 
| 47108 | 1152  | 
diff_numeral_simps diff_0 diff_0_right  | 
1153  | 
numeral_times_numeral mult_neg_numeral_simps  | 
|
1154  | 
mult_zero_left mult_zero_right  | 
|
1155  | 
abs_numeral abs_neg_numeral  | 
|
1156  | 
||
1157  | 
text {*
 | 
|
1158  | 
For making a minimal simpset, one must include these default simprules.  | 
|
1159  | 
  Also include @{text simp_thms}.
 | 
|
1160  | 
*}  | 
|
1161  | 
||
1162  | 
lemmas arith_simps =  | 
|
1163  | 
add_num_simps mult_num_simps sub_num_simps  | 
|
1164  | 
BitM.simps dbl_simps dbl_inc_simps dbl_dec_simps  | 
|
1165  | 
abs_zero abs_one arith_extra_simps  | 
|
1166  | 
||
| 54249 | 1167  | 
lemmas more_arith_simps =  | 
1168  | 
neg_le_iff_le  | 
|
1169  | 
minus_zero left_minus right_minus  | 
|
1170  | 
mult_1_left mult_1_right  | 
|
1171  | 
mult_minus_left mult_minus_right  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
1172  | 
minus_add_distrib minus_minus mult.assoc  | 
| 54249 | 1173  | 
|
1174  | 
lemmas of_nat_simps =  | 
|
1175  | 
of_nat_0 of_nat_1 of_nat_Suc of_nat_add of_nat_mult  | 
|
1176  | 
||
| 47108 | 1177  | 
text {* Simplification of relational operations *}
 | 
1178  | 
||
1179  | 
lemmas eq_numeral_extra =  | 
|
1180  | 
zero_neq_one one_neq_zero  | 
|
1181  | 
||
1182  | 
lemmas rel_simps =  | 
|
1183  | 
le_num_simps less_num_simps eq_num_simps  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1184  | 
le_numeral_simps le_neg_numeral_simps le_minus_one_simps le_numeral_extra  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1185  | 
less_numeral_simps less_neg_numeral_simps less_minus_one_simps less_numeral_extra  | 
| 47108 | 1186  | 
eq_numeral_simps eq_neg_numeral_simps eq_numeral_extra  | 
1187  | 
||
| 54249 | 1188  | 
lemma Let_numeral [simp]: "Let (numeral v) f = f (numeral v)"  | 
1189  | 
  -- {* Unfold all @{text let}s involving constants *}
 | 
|
1190  | 
unfolding Let_def ..  | 
|
1191  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1192  | 
lemma Let_neg_numeral [simp]: "Let (- numeral v) f = f (- numeral v)"  | 
| 54249 | 1193  | 
  -- {* Unfold all @{text let}s involving constants *}
 | 
1194  | 
unfolding Let_def ..  | 
|
1195  | 
||
1196  | 
declaration {*
 | 
|
1197  | 
let  | 
|
| 59996 | 1198  | 
fun number_of ctxt T n =  | 
1199  | 
    if not (Sign.of_sort (Proof_Context.theory_of ctxt) (T, @{sort numeral}))
 | 
|
| 54249 | 1200  | 
    then raise CTERM ("number_of", [])
 | 
| 59996 | 1201  | 
else Numeral.mk_cnumber (Thm.ctyp_of ctxt T) n;  | 
| 54249 | 1202  | 
in  | 
1203  | 
K (  | 
|
1204  | 
    Lin_Arith.add_simps (@{thms arith_simps} @ @{thms more_arith_simps}
 | 
|
1205  | 
      @ @{thms rel_simps}
 | 
|
1206  | 
      @ @{thms pred_numeral_simps}
 | 
|
1207  | 
      @ @{thms arith_special numeral_One}
 | 
|
1208  | 
      @ @{thms of_nat_simps})
 | 
|
1209  | 
    #> Lin_Arith.add_simps [@{thm Suc_numeral},
 | 
|
1210  | 
      @{thm Let_numeral}, @{thm Let_neg_numeral}, @{thm Let_0}, @{thm Let_1},
 | 
|
1211  | 
      @{thm le_Suc_numeral}, @{thm le_numeral_Suc},
 | 
|
1212  | 
      @{thm less_Suc_numeral}, @{thm less_numeral_Suc},
 | 
|
1213  | 
      @{thm Suc_eq_numeral}, @{thm eq_numeral_Suc},
 | 
|
1214  | 
      @{thm mult_Suc}, @{thm mult_Suc_right},
 | 
|
1215  | 
      @{thm of_nat_numeral}]
 | 
|
1216  | 
#> Lin_Arith.set_number_of number_of)  | 
|
1217  | 
end  | 
|
1218  | 
*}  | 
|
1219  | 
||
| 47108 | 1220  | 
|
1221  | 
subsubsection {* Simplification of arithmetic when nested to the right. *}
 | 
|
1222  | 
||
1223  | 
lemma add_numeral_left [simp]:  | 
|
1224  | 
"numeral v + (numeral w + z) = (numeral(v + w) + z)"  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
1225  | 
by (simp_all add: add.assoc [symmetric])  | 
| 47108 | 1226  | 
|
1227  | 
lemma add_neg_numeral_left [simp]:  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1228  | 
"numeral v + (- numeral w + y) = (sub v w + y)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1229  | 
"- numeral v + (numeral w + y) = (sub w v + y)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1230  | 
"- numeral v + (- numeral w + y) = (- numeral(v + w) + y)"  | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
1231  | 
by (simp_all add: add.assoc [symmetric])  | 
| 47108 | 1232  | 
|
1233  | 
lemma mult_numeral_left [simp]:  | 
|
1234  | 
"numeral v * (numeral w * z) = (numeral(v * w) * z :: 'a::semiring_numeral)"  | 
|
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1235  | 
"- numeral v * (numeral w * y) = (- numeral(v * w) * y :: 'b::ring_1)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1236  | 
"numeral v * (- numeral w * y) = (- numeral(v * w) * y :: 'b::ring_1)"  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54249 
diff
changeset
 | 
1237  | 
"- numeral v * (- numeral w * y) = (numeral(v * w) * y :: 'b::ring_1)"  | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
55974 
diff
changeset
 | 
1238  | 
by (simp_all add: mult.assoc [symmetric])  | 
| 47108 | 1239  | 
|
1240  | 
hide_const (open) One Bit0 Bit1 BitM inc pow sqr sub dbl dbl_inc dbl_dec  | 
|
1241  | 
||
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
50817 
diff
changeset
 | 
1242  | 
|
| 47108 | 1243  | 
subsection {* code module namespace *}
 | 
1244  | 
||
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52210 
diff
changeset
 | 
1245  | 
code_identifier  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52210 
diff
changeset
 | 
1246  | 
code_module Num \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith  | 
| 47108 | 1247  | 
|
1248  | 
end  |