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