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