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