src/HOL/IntDef.thy
 author haftmann Tue Jul 10 17:30:43 2007 +0200 (2007-07-10) changeset 23705 315c638d5856 parent 23477 f4b83f03cac9 child 23852 3736cdf9398b permissions -rw-r--r--
moved finite lemmas to Finite_Set.thy
```     1 (*  Title:      IntDef.thy
```
```     2     ID:         \$Id\$
```
```     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
```
```     4     Copyright   1996  University of Cambridge
```
```     5
```
```     6 *)
```
```     7
```
```     8 header{*The Integers as Equivalence Classes over Pairs of Natural Numbers*}
```
```     9
```
```    10 theory IntDef
```
```    11 imports Equiv_Relations Nat
```
```    12 begin
```
```    13
```
```    14
```
```    15 text {* the equivalence relation underlying the integers *}
```
```    16
```
```    17 definition
```
```    18   intrel :: "((nat \<times> nat) \<times> (nat \<times> nat)) set"
```
```    19 where
```
```    20   "intrel = {((x, y), (u, v)) | x y u v. x + v = u +y }"
```
```    21
```
```    22 typedef (Integ)
```
```    23   int = "UNIV//intrel"
```
```    24   by (auto simp add: quotient_def)
```
```    25
```
```    26 instance int :: zero
```
```    27   Zero_int_def: "0 \<equiv> Abs_Integ (intrel `` {(0, 0)})" ..
```
```    28
```
```    29 instance int :: one
```
```    30   One_int_def: "1 \<equiv> Abs_Integ (intrel `` {(1, 0)})" ..
```
```    31
```
```    32 instance int :: plus
```
```    33   add_int_def: "z + w \<equiv> Abs_Integ
```
```    34     (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u, v) \<in> Rep_Integ w.
```
```    35       intrel `` {(x + u, y + v)})" ..
```
```    36
```
```    37 instance int :: minus
```
```    38   minus_int_def:
```
```    39     "- z \<equiv> Abs_Integ (\<Union>(x, y) \<in> Rep_Integ z. intrel `` {(y, x)})"
```
```    40   diff_int_def:  "z - w \<equiv> z + (-w)" ..
```
```    41
```
```    42 instance int :: times
```
```    43   mult_int_def: "z * w \<equiv>  Abs_Integ
```
```    44     (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u,v ) \<in> Rep_Integ w.
```
```    45       intrel `` {(x*u + y*v, x*v + y*u)})" ..
```
```    46
```
```    47 instance int :: ord
```
```    48   le_int_def:
```
```    49    "z \<le> w \<equiv> \<exists>x y u v. x+v \<le> u+y \<and> (x, y) \<in> Rep_Integ z \<and> (u, v) \<in> Rep_Integ w"
```
```    50   less_int_def: "z < w \<equiv> z \<le> w \<and> z \<noteq> w" ..
```
```    51
```
```    52 lemmas [code func del] = Zero_int_def One_int_def add_int_def
```
```    53   minus_int_def mult_int_def le_int_def less_int_def
```
```    54
```
```    55
```
```    56 subsection{*Construction of the Integers*}
```
```    57
```
```    58 lemma intrel_iff [simp]: "(((x,y),(u,v)) \<in> intrel) = (x+v = u+y)"
```
```    59 by (simp add: intrel_def)
```
```    60
```
```    61 lemma equiv_intrel: "equiv UNIV intrel"
```
```    62 by (simp add: intrel_def equiv_def refl_def sym_def trans_def)
```
```    63
```
```    64 text{*Reduces equality of equivalence classes to the @{term intrel} relation:
```
```    65   @{term "(intrel `` {x} = intrel `` {y}) = ((x,y) \<in> intrel)"} *}
```
```    66 lemmas equiv_intrel_iff [simp] = eq_equiv_class_iff [OF equiv_intrel UNIV_I UNIV_I]
```
```    67
```
```    68 text{*All equivalence classes belong to set of representatives*}
```
```    69 lemma [simp]: "intrel``{(x,y)} \<in> Integ"
```
```    70 by (auto simp add: Integ_def intrel_def quotient_def)
```
```    71
```
```    72 text{*Reduces equality on abstractions to equality on representatives:
```
```    73   @{prop "\<lbrakk>x \<in> Integ; y \<in> Integ\<rbrakk> \<Longrightarrow> (Abs_Integ x = Abs_Integ y) = (x=y)"} *}
```
```    74 declare Abs_Integ_inject [simp]  Abs_Integ_inverse [simp]
```
```    75
```
```    76 text{*Case analysis on the representation of an integer as an equivalence
```
```    77       class of pairs of naturals.*}
```
```    78 lemma eq_Abs_Integ [case_names Abs_Integ, cases type: int]:
```
```    79      "(!!x y. z = Abs_Integ(intrel``{(x,y)}) ==> P) ==> P"
```
```    80 apply (rule Abs_Integ_cases [of z])
```
```    81 apply (auto simp add: Integ_def quotient_def)
```
```    82 done
```
```    83
```
```    84
```
```    85 subsection{*Arithmetic Operations*}
```
```    86
```
```    87 lemma minus: "- Abs_Integ(intrel``{(x,y)}) = Abs_Integ(intrel `` {(y,x)})"
```
```    88 proof -
```
```    89   have "(\<lambda>(x,y). intrel``{(y,x)}) respects intrel"
```
```    90     by (simp add: congruent_def)
```
```    91   thus ?thesis
```
```    92     by (simp add: minus_int_def UN_equiv_class [OF equiv_intrel])
```
```    93 qed
```
```    94
```
```    95 lemma add:
```
```    96      "Abs_Integ (intrel``{(x,y)}) + Abs_Integ (intrel``{(u,v)}) =
```
```    97       Abs_Integ (intrel``{(x+u, y+v)})"
```
```    98 proof -
```
```    99   have "(\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). intrel `` {(x+u, y+v)}) w) z)
```
```   100         respects2 intrel"
```
```   101     by (simp add: congruent2_def)
```
```   102   thus ?thesis
```
```   103     by (simp add: add_int_def UN_UN_split_split_eq
```
```   104                   UN_equiv_class2 [OF equiv_intrel equiv_intrel])
```
```   105 qed
```
```   106
```
```   107 text{*Congruence property for multiplication*}
```
```   108 lemma mult_congruent2:
```
```   109      "(%p1 p2. (%(x,y). (%(u,v). intrel``{(x*u + y*v, x*v + y*u)}) p2) p1)
```
```   110       respects2 intrel"
```
```   111 apply (rule equiv_intrel [THEN congruent2_commuteI])
```
```   112  apply (force simp add: mult_ac, clarify)
```
```   113 apply (simp add: congruent_def mult_ac)
```
```   114 apply (rename_tac u v w x y z)
```
```   115 apply (subgoal_tac "u*y + x*y = w*y + v*y  &  u*z + x*z = w*z + v*z")
```
```   116 apply (simp add: mult_ac)
```
```   117 apply (simp add: add_mult_distrib [symmetric])
```
```   118 done
```
```   119
```
```   120 lemma mult:
```
```   121      "Abs_Integ((intrel``{(x,y)})) * Abs_Integ((intrel``{(u,v)})) =
```
```   122       Abs_Integ(intrel `` {(x*u + y*v, x*v + y*u)})"
```
```   123 by (simp add: mult_int_def UN_UN_split_split_eq mult_congruent2
```
```   124               UN_equiv_class2 [OF equiv_intrel equiv_intrel])
```
```   125
```
```   126 text{*The integers form a @{text comm_ring_1}*}
```
```   127 instance int :: comm_ring_1
```
```   128 proof
```
```   129   fix i j k :: int
```
```   130   show "(i + j) + k = i + (j + k)"
```
```   131     by (cases i, cases j, cases k) (simp add: add add_assoc)
```
```   132   show "i + j = j + i"
```
```   133     by (cases i, cases j) (simp add: add_ac add)
```
```   134   show "0 + i = i"
```
```   135     by (cases i) (simp add: Zero_int_def add)
```
```   136   show "- i + i = 0"
```
```   137     by (cases i) (simp add: Zero_int_def minus add)
```
```   138   show "i - j = i + - j"
```
```   139     by (simp add: diff_int_def)
```
```   140   show "(i * j) * k = i * (j * k)"
```
```   141     by (cases i, cases j, cases k) (simp add: mult ring_simps)
```
```   142   show "i * j = j * i"
```
```   143     by (cases i, cases j) (simp add: mult ring_simps)
```
```   144   show "1 * i = i"
```
```   145     by (cases i) (simp add: One_int_def mult)
```
```   146   show "(i + j) * k = i * k + j * k"
```
```   147     by (cases i, cases j, cases k) (simp add: add mult ring_simps)
```
```   148   show "0 \<noteq> (1::int)"
```
```   149     by (simp add: Zero_int_def One_int_def)
```
```   150 qed
```
```   151
```
```   152 abbreviation
```
```   153   int :: "nat \<Rightarrow> int"
```
```   154 where
```
```   155   "int \<equiv> of_nat"
```
```   156
```
```   157 lemma int_def: "int m = Abs_Integ (intrel `` {(m, 0)})"
```
```   158 by (induct m, simp_all add: Zero_int_def One_int_def add)
```
```   159
```
```   160
```
```   161 subsection{*The @{text "\<le>"} Ordering*}
```
```   162
```
```   163 lemma le:
```
```   164   "(Abs_Integ(intrel``{(x,y)}) \<le> Abs_Integ(intrel``{(u,v)})) = (x+v \<le> u+y)"
```
```   165 by (force simp add: le_int_def)
```
```   166
```
```   167 lemma less:
```
```   168   "(Abs_Integ(intrel``{(x,y)}) < Abs_Integ(intrel``{(u,v)})) = (x+v < u+y)"
```
```   169 by (simp add: less_int_def le order_less_le)
```
```   170
```
```   171 instance int :: linorder
```
```   172 proof
```
```   173   fix i j k :: int
```
```   174   show "(i < j) = (i \<le> j \<and> i \<noteq> j)"
```
```   175     by (simp add: less_int_def)
```
```   176   show "i \<le> i"
```
```   177     by (cases i) (simp add: le)
```
```   178   show "i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> i \<le> k"
```
```   179     by (cases i, cases j, cases k) (simp add: le)
```
```   180   show "i \<le> j \<Longrightarrow> j \<le> i \<Longrightarrow> i = j"
```
```   181     by (cases i, cases j) (simp add: le)
```
```   182   show "i \<le> j \<or> j \<le> i"
```
```   183     by (cases i, cases j) (simp add: le linorder_linear)
```
```   184 qed
```
```   185
```
```   186 instance int :: pordered_cancel_ab_semigroup_add
```
```   187 proof
```
```   188   fix i j k :: int
```
```   189   show "i \<le> j \<Longrightarrow> k + i \<le> k + j"
```
```   190     by (cases i, cases j, cases k) (simp add: le add)
```
```   191 qed
```
```   192
```
```   193 text{*Strict Monotonicity of Multiplication*}
```
```   194
```
```   195 text{*strict, in 1st argument; proof is by induction on k>0*}
```
```   196 lemma zmult_zless_mono2_lemma:
```
```   197      "(i::int)<j ==> 0<k ==> int k * i < int k * j"
```
```   198 apply (induct "k", simp)
```
```   199 apply (simp add: left_distrib)
```
```   200 apply (case_tac "k=0")
```
```   201 apply (simp_all add: add_strict_mono)
```
```   202 done
```
```   203
```
```   204 lemma zero_le_imp_eq_int: "(0::int) \<le> k ==> \<exists>n. k = int n"
```
```   205 apply (cases k)
```
```   206 apply (auto simp add: le add int_def Zero_int_def)
```
```   207 apply (rule_tac x="x-y" in exI, simp)
```
```   208 done
```
```   209
```
```   210 lemma zero_less_imp_eq_int: "(0::int) < k ==> \<exists>n>0. k = int n"
```
```   211 apply (cases k)
```
```   212 apply (simp add: less int_def Zero_int_def)
```
```   213 apply (rule_tac x="x-y" in exI, simp)
```
```   214 done
```
```   215
```
```   216 lemma zmult_zless_mono2: "[| i<j;  (0::int) < k |] ==> k*i < k*j"
```
```   217 apply (drule zero_less_imp_eq_int)
```
```   218 apply (auto simp add: zmult_zless_mono2_lemma)
```
```   219 done
```
```   220
```
```   221 instance int :: minus
```
```   222   zabs_def: "\<bar>i\<Colon>int\<bar> \<equiv> if i < 0 then - i else i" ..
```
```   223
```
```   224 instance int :: distrib_lattice
```
```   225   "inf \<equiv> min"
```
```   226   "sup \<equiv> max"
```
```   227   by intro_classes
```
```   228     (auto simp add: inf_int_def sup_int_def min_max.sup_inf_distrib1)
```
```   229
```
```   230 text{*The integers form an ordered integral domain*}
```
```   231 instance int :: ordered_idom
```
```   232 proof
```
```   233   fix i j k :: int
```
```   234   show "i < j \<Longrightarrow> 0 < k \<Longrightarrow> k * i < k * j"
```
```   235     by (rule zmult_zless_mono2)
```
```   236   show "\<bar>i\<bar> = (if i < 0 then -i else i)"
```
```   237     by (simp only: zabs_def)
```
```   238 qed
```
```   239
```
```   240 lemma zless_imp_add1_zle: "w<z ==> w + (1::int) \<le> z"
```
```   241 apply (cases w, cases z)
```
```   242 apply (simp add: less le add One_int_def)
```
```   243 done
```
```   244
```
```   245
```
```   246 subsection{*Magnitude of an Integer, as a Natural Number: @{term nat}*}
```
```   247
```
```   248 definition
```
```   249   nat :: "int \<Rightarrow> nat"
```
```   250 where
```
```   251   [code func del]: "nat z = contents (\<Union>(x, y) \<in> Rep_Integ z. {x-y})"
```
```   252
```
```   253 lemma nat: "nat (Abs_Integ (intrel``{(x,y)})) = x-y"
```
```   254 proof -
```
```   255   have "(\<lambda>(x,y). {x-y}) respects intrel"
```
```   256     by (simp add: congruent_def) arith
```
```   257   thus ?thesis
```
```   258     by (simp add: nat_def UN_equiv_class [OF equiv_intrel])
```
```   259 qed
```
```   260
```
```   261 lemma nat_int [simp]: "nat (int n) = n"
```
```   262 by (simp add: nat int_def)
```
```   263
```
```   264 lemma nat_zero [simp]: "nat 0 = 0"
```
```   265 by (simp add: Zero_int_def nat)
```
```   266
```
```   267 lemma int_nat_eq [simp]: "int (nat z) = (if 0 \<le> z then z else 0)"
```
```   268 by (cases z, simp add: nat le int_def Zero_int_def)
```
```   269
```
```   270 corollary nat_0_le: "0 \<le> z ==> int (nat z) = z"
```
```   271 by simp
```
```   272
```
```   273 lemma nat_le_0 [simp]: "z \<le> 0 ==> nat z = 0"
```
```   274 by (cases z, simp add: nat le Zero_int_def)
```
```   275
```
```   276 lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)"
```
```   277 apply (cases w, cases z)
```
```   278 apply (simp add: nat le linorder_not_le [symmetric] Zero_int_def, arith)
```
```   279 done
```
```   280
```
```   281 text{*An alternative condition is @{term "0 \<le> w"} *}
```
```   282 corollary nat_mono_iff: "0 < z ==> (nat w < nat z) = (w < z)"
```
```   283 by (simp add: nat_le_eq_zle linorder_not_le [symmetric])
```
```   284
```
```   285 corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)"
```
```   286 by (simp add: nat_le_eq_zle linorder_not_le [symmetric])
```
```   287
```
```   288 lemma zless_nat_conj [simp]: "(nat w < nat z) = (0 < z & w < z)"
```
```   289 apply (cases w, cases z)
```
```   290 apply (simp add: nat le Zero_int_def linorder_not_le [symmetric], arith)
```
```   291 done
```
```   292
```
```   293 lemma nonneg_eq_int: "[| 0 \<le> z;  !!m. z = int m ==> P |] ==> P"
```
```   294 by (blast dest: nat_0_le sym)
```
```   295
```
```   296 lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = int m else m=0)"
```
```   297 by (cases w, simp add: nat le int_def Zero_int_def, arith)
```
```   298
```
```   299 corollary nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = int m else m=0)"
```
```   300 by (simp only: eq_commute [of m] nat_eq_iff)
```
```   301
```
```   302 lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < int m)"
```
```   303 apply (cases w)
```
```   304 apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
```
```   305 done
```
```   306
```
```   307 lemma int_eq_iff: "(int m = z) = (m = nat z & 0 \<le> z)"
```
```   308 by (auto simp add: nat_eq_iff2)
```
```   309
```
```   310 lemma zero_less_nat_eq [simp]: "(0 < nat z) = (0 < z)"
```
```   311 by (insert zless_nat_conj [of 0], auto)
```
```   312
```
```   313 lemma nat_add_distrib:
```
```   314      "[| (0::int) \<le> z;  0 \<le> z' |] ==> nat (z+z') = nat z + nat z'"
```
```   315 by (cases z, cases z', simp add: nat add le Zero_int_def)
```
```   316
```
```   317 lemma nat_diff_distrib:
```
```   318      "[| (0::int) \<le> z';  z' \<le> z |] ==> nat (z-z') = nat z - nat z'"
```
```   319 by (cases z, cases z',
```
```   320     simp add: nat add minus diff_minus le Zero_int_def)
```
```   321
```
```   322 lemma nat_zminus_int [simp]: "nat (- (int n)) = 0"
```
```   323 by (simp add: int_def minus nat Zero_int_def)
```
```   324
```
```   325 lemma zless_nat_eq_int_zless: "(m < nat z) = (int m < z)"
```
```   326 by (cases z, simp add: nat less int_def, arith)
```
```   327
```
```   328
```
```   329 subsection{*Lemmas about the Function @{term int} and Orderings*}
```
```   330
```
```   331 lemma negative_zless_0: "- (int (Suc n)) < 0"
```
```   332 by (simp add: order_less_le del: of_nat_Suc)
```
```   333
```
```   334 lemma negative_zless [iff]: "- (int (Suc n)) < int m"
```
```   335 by (rule negative_zless_0 [THEN order_less_le_trans], simp)
```
```   336
```
```   337 lemma negative_zle_0: "- int n \<le> 0"
```
```   338 by (simp add: minus_le_iff)
```
```   339
```
```   340 lemma negative_zle [iff]: "- int n \<le> int m"
```
```   341 by (rule order_trans [OF negative_zle_0 of_nat_0_le_iff])
```
```   342
```
```   343 lemma not_zle_0_negative [simp]: "~ (0 \<le> - (int (Suc n)))"
```
```   344 by (subst le_minus_iff, simp del: of_nat_Suc)
```
```   345
```
```   346 lemma int_zle_neg: "(int n \<le> - int m) = (n = 0 & m = 0)"
```
```   347 by (simp add: int_def le minus Zero_int_def)
```
```   348
```
```   349 lemma not_int_zless_negative [simp]: "~ (int n < - int m)"
```
```   350 by (simp add: linorder_not_less)
```
```   351
```
```   352 lemma negative_eq_positive [simp]: "(- int n = int m) = (n = 0 & m = 0)"
```
```   353 by (force simp add: order_eq_iff [of "- int n"] int_zle_neg)
```
```   354
```
```   355 lemma zle_iff_zadd: "(w \<le> z) = (\<exists>n. z = w + int n)"
```
```   356 proof -
```
```   357   have "(w \<le> z) = (0 \<le> z - w)"
```
```   358     by (simp only: le_diff_eq add_0_left)
```
```   359   also have "\<dots> = (\<exists>n. z - w = int n)"
```
```   360     by (auto elim: zero_le_imp_eq_int)
```
```   361   also have "\<dots> = (\<exists>n. z = w + int n)"
```
```   362     by (simp only: group_simps)
```
```   363   finally show ?thesis .
```
```   364 qed
```
```   365
```
```   366 lemma zadd_int_left: "(int m) + (int n + z) = int (m + n) + z"
```
```   367 by simp
```
```   368
```
```   369 lemma int_Suc0_eq_1: "int (Suc 0) = 1"
```
```   370 by simp
```
```   371
```
```   372 lemma abs_of_nat [simp]: "\<bar>of_nat n::'a::ordered_idom\<bar> = of_nat n"
```
```   373 by (rule  of_nat_0_le_iff [THEN abs_of_nonneg]) (* belongs in Nat.thy *)
```
```   374
```
```   375 text{*This version is proved for all ordered rings, not just integers!
```
```   376       It is proved here because attribute @{text arith_split} is not available
```
```   377       in theory @{text Ring_and_Field}.
```
```   378       But is it really better than just rewriting with @{text abs_if}?*}
```
```   379 lemma abs_split [arith_split]:
```
```   380      "P(abs(a::'a::ordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))"
```
```   381 by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
```
```   382
```
```   383
```
```   384 subsection {* Constants @{term neg} and @{term iszero} *}
```
```   385
```
```   386 definition
```
```   387   neg  :: "'a\<Colon>ordered_idom \<Rightarrow> bool"
```
```   388 where
```
```   389   [code inline]: "neg Z \<longleftrightarrow> Z < 0"
```
```   390
```
```   391 definition (*for simplifying equalities*)
```
```   392   iszero :: "'a\<Colon>semiring_1 \<Rightarrow> bool"
```
```   393 where
```
```   394   "iszero z \<longleftrightarrow> z = 0"
```
```   395
```
```   396 lemma not_neg_int [simp]: "~ neg (int n)"
```
```   397 by (simp add: neg_def)
```
```   398
```
```   399 lemma neg_zminus_int [simp]: "neg (- (int (Suc n)))"
```
```   400 by (simp add: neg_def neg_less_0_iff_less del: of_nat_Suc)
```
```   401
```
```   402 lemmas neg_eq_less_0 = neg_def
```
```   403
```
```   404 lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
```
```   405 by (simp add: neg_def linorder_not_less)
```
```   406
```
```   407
```
```   408 text{*To simplify inequalities when Numeral1 can get simplified to 1*}
```
```   409
```
```   410 lemma not_neg_0: "~ neg 0"
```
```   411 by (simp add: One_int_def neg_def)
```
```   412
```
```   413 lemma not_neg_1: "~ neg 1"
```
```   414 by (simp add: neg_def linorder_not_less zero_le_one)
```
```   415
```
```   416 lemma iszero_0: "iszero 0"
```
```   417 by (simp add: iszero_def)
```
```   418
```
```   419 lemma not_iszero_1: "~ iszero 1"
```
```   420 by (simp add: iszero_def eq_commute)
```
```   421
```
```   422 lemma neg_nat: "neg z ==> nat z = 0"
```
```   423 by (simp add: neg_def order_less_imp_le)
```
```   424
```
```   425 lemma not_neg_nat: "~ neg z ==> int (nat z) = z"
```
```   426 by (simp add: linorder_not_less neg_def)
```
```   427
```
```   428
```
```   429 subsection{*The Set of Natural Numbers*}
```
```   430
```
```   431 constdefs
```
```   432   Nats  :: "'a::semiring_1 set"
```
```   433   "Nats == range of_nat"
```
```   434
```
```   435 notation (xsymbols)
```
```   436   Nats  ("\<nat>")
```
```   437
```
```   438 lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats"
```
```   439 by (simp add: Nats_def)
```
```   440
```
```   441 lemma Nats_0 [simp]: "0 \<in> Nats"
```
```   442 apply (simp add: Nats_def)
```
```   443 apply (rule range_eqI)
```
```   444 apply (rule of_nat_0 [symmetric])
```
```   445 done
```
```   446
```
```   447 lemma Nats_1 [simp]: "1 \<in> Nats"
```
```   448 apply (simp add: Nats_def)
```
```   449 apply (rule range_eqI)
```
```   450 apply (rule of_nat_1 [symmetric])
```
```   451 done
```
```   452
```
```   453 lemma Nats_add [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a+b \<in> Nats"
```
```   454 apply (auto simp add: Nats_def)
```
```   455 apply (rule range_eqI)
```
```   456 apply (rule of_nat_add [symmetric])
```
```   457 done
```
```   458
```
```   459 lemma Nats_mult [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a*b \<in> Nats"
```
```   460 apply (auto simp add: Nats_def)
```
```   461 apply (rule range_eqI)
```
```   462 apply (rule of_nat_mult [symmetric])
```
```   463 done
```
```   464
```
```   465 lemma of_nat_eq_id [simp]: "of_nat = (id :: nat => nat)"
```
```   466 proof
```
```   467   fix n
```
```   468   show "of_nat n = id n"  by (induct n, simp_all)
```
```   469 qed (* belongs in Nat.thy *)
```
```   470
```
```   471
```
```   472 subsection{*Embedding of the Integers into any @{text ring_1}:
```
```   473 @{term of_int}*}
```
```   474
```
```   475 constdefs
```
```   476    of_int :: "int => 'a::ring_1"
```
```   477    "of_int z == contents (\<Union>(i,j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
```
```   478
```
```   479
```
```   480 lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
```
```   481 proof -
```
```   482   have "(\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) }) respects intrel"
```
```   483     by (simp add: congruent_def compare_rls of_nat_add [symmetric]
```
```   484             del: of_nat_add)
```
```   485   thus ?thesis
```
```   486     by (simp add: of_int_def UN_equiv_class [OF equiv_intrel])
```
```   487 qed
```
```   488
```
```   489 lemma of_int_0 [simp]: "of_int 0 = 0"
```
```   490 by (simp add: of_int Zero_int_def)
```
```   491
```
```   492 lemma of_int_1 [simp]: "of_int 1 = 1"
```
```   493 by (simp add: of_int One_int_def)
```
```   494
```
```   495 lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z"
```
```   496 by (cases w, cases z, simp add: compare_rls of_int add)
```
```   497
```
```   498 lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)"
```
```   499 by (cases z, simp add: compare_rls of_int minus)
```
```   500
```
```   501 lemma of_int_diff [simp]: "of_int (w-z) = of_int w - of_int z"
```
```   502 by (simp add: diff_minus)
```
```   503
```
```   504 lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z"
```
```   505 apply (cases w, cases z)
```
```   506 apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib
```
```   507                  mult add_ac of_nat_mult)
```
```   508 done
```
```   509
```
```   510 lemma of_int_le_iff [simp]:
```
```   511      "(of_int w \<le> (of_int z::'a::ordered_idom)) = (w \<le> z)"
```
```   512 apply (cases w)
```
```   513 apply (cases z)
```
```   514 apply (simp add: compare_rls of_int le diff_int_def add minus
```
```   515                  of_nat_add [symmetric]   del: of_nat_add)
```
```   516 done
```
```   517
```
```   518 text{*Special cases where either operand is zero*}
```
```   519 lemmas of_int_0_le_iff [simp] = of_int_le_iff [of 0, simplified]
```
```   520 lemmas of_int_le_0_iff [simp] = of_int_le_iff [of _ 0, simplified]
```
```   521
```
```   522
```
```   523 lemma of_int_less_iff [simp]:
```
```   524      "(of_int w < (of_int z::'a::ordered_idom)) = (w < z)"
```
```   525 by (simp add: linorder_not_le [symmetric])
```
```   526
```
```   527 text{*Special cases where either operand is zero*}
```
```   528 lemmas of_int_0_less_iff [simp] = of_int_less_iff [of 0, simplified]
```
```   529 lemmas of_int_less_0_iff [simp] = of_int_less_iff [of _ 0, simplified]
```
```   530
```
```   531 text{*Class for unital rings with characteristic zero.
```
```   532  Includes non-ordered rings like the complex numbers.*}
```
```   533 axclass ring_char_0 < ring_1, semiring_char_0
```
```   534
```
```   535 lemma of_int_eq_iff [simp]:
```
```   536      "(of_int w = (of_int z::'a::ring_char_0)) = (w = z)"
```
```   537 apply (cases w, cases z, simp add: of_int)
```
```   538 apply (simp only: diff_eq_eq diff_add_eq eq_diff_eq)
```
```   539 apply (simp only: of_nat_add [symmetric] of_nat_eq_iff)
```
```   540 done
```
```   541
```
```   542 text{*Every @{text ordered_idom} has characteristic zero.*}
```
```   543 instance ordered_idom < ring_char_0 ..
```
```   544
```
```   545 text{*Special cases where either operand is zero*}
```
```   546 lemmas of_int_0_eq_iff [simp] = of_int_eq_iff [of 0, simplified]
```
```   547 lemmas of_int_eq_0_iff [simp] = of_int_eq_iff [of _ 0, simplified]
```
```   548
```
```   549 lemma of_int_eq_id [simp]: "of_int = (id :: int => int)"
```
```   550 proof
```
```   551   fix z
```
```   552   show "of_int z = id z"
```
```   553     by (cases z)
```
```   554       (simp add: of_int add minus int_def diff_minus)
```
```   555 qed
```
```   556
```
```   557 lemma of_nat_nat: "0 \<le> z ==> of_nat (nat z) = of_int z"
```
```   558 by (cases z rule: eq_Abs_Integ)
```
```   559    (simp add: nat le of_int Zero_int_def of_nat_diff)
```
```   560
```
```   561
```
```   562 subsection{*The Set of Integers*}
```
```   563
```
```   564 constdefs
```
```   565   Ints  :: "'a::ring_1 set"
```
```   566   "Ints == range of_int"
```
```   567
```
```   568 notation (xsymbols)
```
```   569   Ints  ("\<int>")
```
```   570
```
```   571 lemma Ints_0 [simp]: "0 \<in> Ints"
```
```   572 apply (simp add: Ints_def)
```
```   573 apply (rule range_eqI)
```
```   574 apply (rule of_int_0 [symmetric])
```
```   575 done
```
```   576
```
```   577 lemma Ints_1 [simp]: "1 \<in> Ints"
```
```   578 apply (simp add: Ints_def)
```
```   579 apply (rule range_eqI)
```
```   580 apply (rule of_int_1 [symmetric])
```
```   581 done
```
```   582
```
```   583 lemma Ints_add [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a+b \<in> Ints"
```
```   584 apply (auto simp add: Ints_def)
```
```   585 apply (rule range_eqI)
```
```   586 apply (rule of_int_add [symmetric])
```
```   587 done
```
```   588
```
```   589 lemma Ints_minus [simp]: "a \<in> Ints ==> -a \<in> Ints"
```
```   590 apply (auto simp add: Ints_def)
```
```   591 apply (rule range_eqI)
```
```   592 apply (rule of_int_minus [symmetric])
```
```   593 done
```
```   594
```
```   595 lemma Ints_diff [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a-b \<in> Ints"
```
```   596 apply (auto simp add: Ints_def)
```
```   597 apply (rule range_eqI)
```
```   598 apply (rule of_int_diff [symmetric])
```
```   599 done
```
```   600
```
```   601 lemma Ints_mult [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a*b \<in> Ints"
```
```   602 apply (auto simp add: Ints_def)
```
```   603 apply (rule range_eqI)
```
```   604 apply (rule of_int_mult [symmetric])
```
```   605 done
```
```   606
```
```   607 text{*Collapse nested embeddings*}
```
```   608 lemma of_int_of_nat_eq [simp]: "of_int (of_nat n) = of_nat n"
```
```   609 by (induct n, auto)
```
```   610
```
```   611 lemma Ints_cases [cases set: Ints]:
```
```   612   assumes "q \<in> \<int>"
```
```   613   obtains (of_int) z where "q = of_int z"
```
```   614   unfolding Ints_def
```
```   615 proof -
```
```   616   from `q \<in> \<int>` have "q \<in> range of_int" unfolding Ints_def .
```
```   617   then obtain z where "q = of_int z" ..
```
```   618   then show thesis ..
```
```   619 qed
```
```   620
```
```   621 lemma Ints_induct [case_names of_int, induct set: Ints]:
```
```   622   "q \<in> \<int> ==> (!!z. P (of_int z)) ==> P q"
```
```   623   by (rule Ints_cases) auto
```
```   624
```
```   625
```
```   626 subsection {* Further properties *}
```
```   627
```
```   628 text{*Now we replace the case analysis rule by a more conventional one:
```
```   629 whether an integer is negative or not.*}
```
```   630
```
```   631 lemma zless_iff_Suc_zadd:
```
```   632     "(w < z) = (\<exists>n. z = w + int (Suc n))"
```
```   633 apply (cases z, cases w)
```
```   634 apply (auto simp add: less add int_def)
```
```   635 apply (rename_tac a b c d)
```
```   636 apply (rule_tac x="a+d - Suc(c+b)" in exI)
```
```   637 apply arith
```
```   638 done
```
```   639
```
```   640 lemma negD: "x<0 ==> \<exists>n. x = - (int (Suc n))"
```
```   641 apply (cases x)
```
```   642 apply (auto simp add: le minus Zero_int_def int_def order_less_le)
```
```   643 apply (rule_tac x="y - Suc x" in exI, arith)
```
```   644 done
```
```   645
```
```   646 theorem int_cases [cases type: int, case_names nonneg neg]:
```
```   647      "[|!! n. z = int n ==> P;  !! n. z =  - (int (Suc n)) ==> P |] ==> P"
```
```   648 apply (cases "z < 0", blast dest!: negD)
```
```   649 apply (simp add: linorder_not_less del: of_nat_Suc)
```
```   650 apply (blast dest: nat_0_le [THEN sym])
```
```   651 done
```
```   652
```
```   653 theorem int_induct [induct type: int, case_names nonneg neg]:
```
```   654      "[|!! n. P (int n);  !!n. P (- (int (Suc n))) |] ==> P z"
```
```   655   by (cases z rule: int_cases) auto
```
```   656
```
```   657 text{*Contributed by Brian Huffman*}
```
```   658 theorem int_diff_cases [case_names diff]:
```
```   659 assumes prem: "!!m n. z = int m - int n ==> P" shows "P"
```
```   660 apply (cases z rule: eq_Abs_Integ)
```
```   661 apply (rule_tac m=x and n=y in prem)
```
```   662 apply (simp add: int_def diff_def minus add)
```
```   663 done
```
```   664
```
```   665
```
```   666 subsection {* Legacy theorems *}
```
```   667
```
```   668 lemmas zminus_zminus = minus_minus [of "?z::int"]
```
```   669 lemmas zminus_0 = minus_zero [where 'a=int]
```
```   670 lemmas zminus_zadd_distrib = minus_add_distrib [of "?z::int" "?w"]
```
```   671 lemmas zadd_commute = add_commute [of "?z::int" "?w"]
```
```   672 lemmas zadd_assoc = add_assoc [of "?z1.0::int" "?z2.0" "?z3.0"]
```
```   673 lemmas zadd_left_commute = add_left_commute [of "?x::int" "?y" "?z"]
```
```   674 lemmas zadd_ac = zadd_assoc zadd_commute zadd_left_commute
```
```   675 lemmas zmult_ac = OrderedGroup.mult_ac
```
```   676 lemmas zadd_0 = OrderedGroup.add_0_left [of "?z::int"]
```
```   677 lemmas zadd_0_right = OrderedGroup.add_0_left [of "?z::int"]
```
```   678 lemmas zadd_zminus_inverse2 = left_minus [of "?z::int"]
```
```   679 lemmas zmult_zminus = mult_minus_left [of "?z::int" "?w"]
```
```   680 lemmas zmult_commute = mult_commute [of "?z::int" "?w"]
```
```   681 lemmas zmult_assoc = mult_assoc [of "?z1.0::int" "?z2.0" "?z3.0"]
```
```   682 lemmas zadd_zmult_distrib = left_distrib [of "?z1.0::int" "?z2.0" "?w"]
```
```   683 lemmas zadd_zmult_distrib2 = right_distrib [of "?w::int" "?z1.0" "?z2.0"]
```
```   684 lemmas zdiff_zmult_distrib = left_diff_distrib [of "?z1.0::int" "?z2.0" "?w"]
```
```   685 lemmas zdiff_zmult_distrib2 = right_diff_distrib [of "?w::int" "?z1.0" "?z2.0"]
```
```   686
```
```   687 lemmas int_distrib =
```
```   688   zadd_zmult_distrib zadd_zmult_distrib2
```
```   689   zdiff_zmult_distrib zdiff_zmult_distrib2
```
```   690
```
```   691 lemmas zmult_1 = mult_1_left [of "?z::int"]
```
```   692 lemmas zmult_1_right = mult_1_right [of "?z::int"]
```
```   693
```
```   694 lemmas zle_refl = order_refl [of "?w::int"]
```
```   695 lemmas zle_trans = order_trans [where 'a=int and x="?i" and y="?j" and z="?k"]
```
```   696 lemmas zle_anti_sym = order_antisym [of "?z::int" "?w"]
```
```   697 lemmas zle_linear = linorder_linear [of "?z::int" "?w"]
```
```   698 lemmas zless_linear = linorder_less_linear [where 'a = int]
```
```   699
```
```   700 lemmas zadd_left_mono = add_left_mono [of "?i::int" "?j" "?k"]
```
```   701 lemmas zadd_strict_right_mono = add_strict_right_mono [of "?i::int" "?j" "?k"]
```
```   702 lemmas zadd_zless_mono = add_less_le_mono [of "?w'::int" "?w" "?z'" "?z"]
```
```   703
```
```   704 lemmas int_0_less_1 = zero_less_one [where 'a=int]
```
```   705 lemmas int_0_neq_1 = zero_neq_one [where 'a=int]
```
```   706
```
```   707 lemmas inj_int = inj_of_nat [where 'a=int]
```
```   708 lemmas int_int_eq = of_nat_eq_iff [where 'a=int]
```
```   709 lemmas zadd_int = of_nat_add [where 'a=int, symmetric]
```
```   710 lemmas int_mult = of_nat_mult [where 'a=int]
```
```   711 lemmas zmult_int = of_nat_mult [where 'a=int, symmetric]
```
```   712 lemmas int_eq_0_conv = of_nat_eq_0_iff [where 'a=int and m="?n"]
```
```   713 lemmas zless_int = of_nat_less_iff [where 'a=int]
```
```   714 lemmas int_less_0_conv = of_nat_less_0_iff [where 'a=int and m="?k"]
```
```   715 lemmas zero_less_int_conv = of_nat_0_less_iff [where 'a=int]
```
```   716 lemmas zle_int = of_nat_le_iff [where 'a=int]
```
```   717 lemmas zero_zle_int = of_nat_0_le_iff [where 'a=int]
```
```   718 lemmas int_le_0_conv = of_nat_le_0_iff [where 'a=int and m="?n"]
```
```   719 lemmas int_0 = of_nat_0 [where ?'a_1.0=int]
```
```   720 lemmas int_1 = of_nat_1 [where 'a=int]
```
```   721 lemmas int_Suc = of_nat_Suc [where ?'a_1.0=int]
```
```   722 lemmas abs_int_eq = abs_of_nat [where 'a=int and n="?m"]
```
```   723 lemmas of_int_int_eq = of_int_of_nat_eq [where 'a=int]
```
```   724 lemmas zdiff_int = of_nat_diff [where 'a=int, symmetric]
```
```   725 lemmas zless_le = less_int_def [THEN meta_eq_to_obj_eq]
```
```   726 lemmas int_eq_of_nat = TrueI
```
```   727
```
```   728 abbreviation
```
```   729   int_of_nat :: "nat \<Rightarrow> int"
```
```   730 where
```
```   731   "int_of_nat \<equiv> of_nat"
```
```   732
```
```   733 end
```