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