src/HOL/IntDef.thy
changeset 23164 69e55066dbca
child 23276 a70934b63910
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/IntDef.thy	Thu May 31 18:16:52 2007 +0200
     1.3 @@ -0,0 +1,890 @@
     1.4 +(*  Title:      IntDef.thy
     1.5 +    ID:         $Id$
     1.6 +    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   1996  University of Cambridge
     1.8 +
     1.9 +*)
    1.10 +
    1.11 +header{*The Integers as Equivalence Classes over Pairs of Natural Numbers*} 
    1.12 +
    1.13 +theory IntDef
    1.14 +imports Equiv_Relations Nat
    1.15 +begin
    1.16 +
    1.17 +text {* the equivalence relation underlying the integers *}
    1.18 +
    1.19 +definition
    1.20 +  intrel :: "((nat \<times> nat) \<times> (nat \<times> nat)) set"
    1.21 +where
    1.22 +  "intrel = {((x, y), (u, v)) | x y u v. x + v = u +y }"
    1.23 +
    1.24 +typedef (Integ)
    1.25 +  int = "UNIV//intrel"
    1.26 +  by (auto simp add: quotient_def)
    1.27 +
    1.28 +definition
    1.29 +  int :: "nat \<Rightarrow> int"
    1.30 +where
    1.31 +  [code func del]: "int m = Abs_Integ (intrel `` {(m, 0)})"
    1.32 +
    1.33 +instance int :: zero
    1.34 +  Zero_int_def: "0 \<equiv> int 0" ..
    1.35 +
    1.36 +instance int :: one
    1.37 +  One_int_def: "1 \<equiv> int 1" ..
    1.38 +
    1.39 +instance int :: plus
    1.40 +  add_int_def: "z + w \<equiv> Abs_Integ
    1.41 +    (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u, v) \<in> Rep_Integ w.
    1.42 +      intrel `` {(x + u, y + v)})" ..
    1.43 +
    1.44 +instance int :: minus
    1.45 +  minus_int_def:
    1.46 +    "- z \<equiv> Abs_Integ (\<Union>(x, y) \<in> Rep_Integ z. intrel `` {(y, x)})"
    1.47 +  diff_int_def:  "z - w \<equiv> z + (-w)" ..
    1.48 +
    1.49 +instance int :: times
    1.50 +  mult_int_def: "z * w \<equiv>  Abs_Integ
    1.51 +    (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u,v ) \<in> Rep_Integ w.
    1.52 +      intrel `` {(x*u + y*v, x*v + y*u)})" ..
    1.53 +
    1.54 +instance int :: ord
    1.55 +  le_int_def:
    1.56 +   "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"
    1.57 +  less_int_def: "z < w \<equiv> z \<le> w \<and> z \<noteq> w" ..
    1.58 +
    1.59 +lemmas [code func del] = Zero_int_def One_int_def add_int_def
    1.60 +  minus_int_def mult_int_def le_int_def less_int_def
    1.61 +
    1.62 +
    1.63 +subsection{*Construction of the Integers*}
    1.64 +
    1.65 +subsubsection{*Preliminary Lemmas about the Equivalence Relation*}
    1.66 +
    1.67 +lemma intrel_iff [simp]: "(((x,y),(u,v)) \<in> intrel) = (x+v = u+y)"
    1.68 +by (simp add: intrel_def)
    1.69 +
    1.70 +lemma equiv_intrel: "equiv UNIV intrel"
    1.71 +by (simp add: intrel_def equiv_def refl_def sym_def trans_def)
    1.72 +
    1.73 +text{*Reduces equality of equivalence classes to the @{term intrel} relation:
    1.74 +  @{term "(intrel `` {x} = intrel `` {y}) = ((x,y) \<in> intrel)"} *}
    1.75 +lemmas equiv_intrel_iff [simp] = eq_equiv_class_iff [OF equiv_intrel UNIV_I UNIV_I]
    1.76 +
    1.77 +text{*All equivalence classes belong to set of representatives*}
    1.78 +lemma [simp]: "intrel``{(x,y)} \<in> Integ"
    1.79 +by (auto simp add: Integ_def intrel_def quotient_def)
    1.80 +
    1.81 +text{*Reduces equality on abstractions to equality on representatives:
    1.82 +  @{prop "\<lbrakk>x \<in> Integ; y \<in> Integ\<rbrakk> \<Longrightarrow> (Abs_Integ x = Abs_Integ y) = (x=y)"} *}
    1.83 +declare Abs_Integ_inject [simp]  Abs_Integ_inverse [simp]
    1.84 +
    1.85 +text{*Case analysis on the representation of an integer as an equivalence
    1.86 +      class of pairs of naturals.*}
    1.87 +lemma eq_Abs_Integ [case_names Abs_Integ, cases type: int]:
    1.88 +     "(!!x y. z = Abs_Integ(intrel``{(x,y)}) ==> P) ==> P"
    1.89 +apply (rule Abs_Integ_cases [of z]) 
    1.90 +apply (auto simp add: Integ_def quotient_def) 
    1.91 +done
    1.92 +
    1.93 +
    1.94 +subsubsection{*@{term int}: Embedding the Naturals into the Integers*}
    1.95 +
    1.96 +lemma inj_int: "inj int"
    1.97 +by (simp add: inj_on_def int_def)
    1.98 +
    1.99 +lemma int_int_eq [iff]: "(int m = int n) = (m = n)"
   1.100 +by (fast elim!: inj_int [THEN injD])
   1.101 +
   1.102 +
   1.103 +subsubsection{*Integer Unary Negation*}
   1.104 +
   1.105 +lemma minus: "- Abs_Integ(intrel``{(x,y)}) = Abs_Integ(intrel `` {(y,x)})"
   1.106 +proof -
   1.107 +  have "(\<lambda>(x,y). intrel``{(y,x)}) respects intrel"
   1.108 +    by (simp add: congruent_def) 
   1.109 +  thus ?thesis
   1.110 +    by (simp add: minus_int_def UN_equiv_class [OF equiv_intrel])
   1.111 +qed
   1.112 +
   1.113 +lemma zminus_zminus: "- (- z) = (z::int)"
   1.114 +  by (cases z) (simp add: minus)
   1.115 +
   1.116 +lemma zminus_0: "- 0 = (0::int)"
   1.117 +  by (simp add: int_def Zero_int_def minus)
   1.118 +
   1.119 +
   1.120 +subsection{*Integer Addition*}
   1.121 +
   1.122 +lemma add:
   1.123 +     "Abs_Integ (intrel``{(x,y)}) + Abs_Integ (intrel``{(u,v)}) =
   1.124 +      Abs_Integ (intrel``{(x+u, y+v)})"
   1.125 +proof -
   1.126 +  have "(\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). intrel `` {(x+u, y+v)}) w) z) 
   1.127 +        respects2 intrel"
   1.128 +    by (simp add: congruent2_def)
   1.129 +  thus ?thesis
   1.130 +    by (simp add: add_int_def UN_UN_split_split_eq
   1.131 +                  UN_equiv_class2 [OF equiv_intrel equiv_intrel])
   1.132 +qed
   1.133 +
   1.134 +lemma zminus_zadd_distrib: "- (z + w) = (- z) + (- w::int)"
   1.135 +  by (cases z, cases w) (simp add: minus add)
   1.136 +
   1.137 +lemma zadd_commute: "(z::int) + w = w + z"
   1.138 +  by (cases z, cases w) (simp add: add_ac add)
   1.139 +
   1.140 +lemma zadd_assoc: "((z1::int) + z2) + z3 = z1 + (z2 + z3)"
   1.141 +  by (cases z1, cases z2, cases z3) (simp add: add add_assoc)
   1.142 +
   1.143 +(*For AC rewriting*)
   1.144 +lemma zadd_left_commute: "x + (y + z) = y + ((x + z)  ::int)"
   1.145 +  apply (rule mk_left_commute [of "op +"])
   1.146 +  apply (rule zadd_assoc)
   1.147 +  apply (rule zadd_commute)
   1.148 +  done
   1.149 +
   1.150 +lemmas zadd_ac = zadd_assoc zadd_commute zadd_left_commute
   1.151 +
   1.152 +lemmas zmult_ac = OrderedGroup.mult_ac
   1.153 +
   1.154 +lemma zadd_int: "(int m) + (int n) = int (m + n)"
   1.155 +  by (simp add: int_def add)
   1.156 +
   1.157 +lemma zadd_int_left: "(int m) + (int n + z) = int (m + n) + z"
   1.158 +  by (simp add: zadd_int zadd_assoc [symmetric])
   1.159 +
   1.160 +(*also for the instance declaration int :: comm_monoid_add*)
   1.161 +lemma zadd_0: "(0::int) + z = z"
   1.162 +apply (simp add: Zero_int_def int_def)
   1.163 +apply (cases z, simp add: add)
   1.164 +done
   1.165 +
   1.166 +lemma zadd_0_right: "z + (0::int) = z"
   1.167 +by (rule trans [OF zadd_commute zadd_0])
   1.168 +
   1.169 +lemma zadd_zminus_inverse2: "(- z) + z = (0::int)"
   1.170 +by (cases z, simp add: int_def Zero_int_def minus add)
   1.171 +
   1.172 +
   1.173 +subsection{*Integer Multiplication*}
   1.174 +
   1.175 +text{*Congruence property for multiplication*}
   1.176 +lemma mult_congruent2:
   1.177 +     "(%p1 p2. (%(x,y). (%(u,v). intrel``{(x*u + y*v, x*v + y*u)}) p2) p1)
   1.178 +      respects2 intrel"
   1.179 +apply (rule equiv_intrel [THEN congruent2_commuteI])
   1.180 + apply (force simp add: mult_ac, clarify) 
   1.181 +apply (simp add: congruent_def mult_ac)  
   1.182 +apply (rename_tac u v w x y z)
   1.183 +apply (subgoal_tac "u*y + x*y = w*y + v*y  &  u*z + x*z = w*z + v*z")
   1.184 +apply (simp add: mult_ac)
   1.185 +apply (simp add: add_mult_distrib [symmetric])
   1.186 +done
   1.187 +
   1.188 +
   1.189 +lemma mult:
   1.190 +     "Abs_Integ((intrel``{(x,y)})) * Abs_Integ((intrel``{(u,v)})) =
   1.191 +      Abs_Integ(intrel `` {(x*u + y*v, x*v + y*u)})"
   1.192 +by (simp add: mult_int_def UN_UN_split_split_eq mult_congruent2
   1.193 +              UN_equiv_class2 [OF equiv_intrel equiv_intrel])
   1.194 +
   1.195 +lemma zmult_zminus: "(- z) * w = - (z * (w::int))"
   1.196 +by (cases z, cases w, simp add: minus mult add_ac)
   1.197 +
   1.198 +lemma zmult_commute: "(z::int) * w = w * z"
   1.199 +by (cases z, cases w, simp add: mult add_ac mult_ac)
   1.200 +
   1.201 +lemma zmult_assoc: "((z1::int) * z2) * z3 = z1 * (z2 * z3)"
   1.202 +by (cases z1, cases z2, cases z3, simp add: mult add_mult_distrib2 mult_ac)
   1.203 +
   1.204 +lemma zadd_zmult_distrib: "((z1::int) + z2) * w = (z1 * w) + (z2 * w)"
   1.205 +by (cases z1, cases z2, cases w, simp add: add mult add_mult_distrib2 mult_ac)
   1.206 +
   1.207 +lemma zadd_zmult_distrib2: "(w::int) * (z1 + z2) = (w * z1) + (w * z2)"
   1.208 +by (simp add: zmult_commute [of w] zadd_zmult_distrib)
   1.209 +
   1.210 +lemma zdiff_zmult_distrib: "((z1::int) - z2) * w = (z1 * w) - (z2 * w)"
   1.211 +by (simp add: diff_int_def zadd_zmult_distrib zmult_zminus)
   1.212 +
   1.213 +lemma zdiff_zmult_distrib2: "(w::int) * (z1 - z2) = (w * z1) - (w * z2)"
   1.214 +by (simp add: zmult_commute [of w] zdiff_zmult_distrib)
   1.215 +
   1.216 +lemmas int_distrib =
   1.217 +  zadd_zmult_distrib zadd_zmult_distrib2
   1.218 +  zdiff_zmult_distrib zdiff_zmult_distrib2
   1.219 +
   1.220 +lemma int_mult: "int (m * n) = (int m) * (int n)"
   1.221 +by (simp add: int_def mult)
   1.222 +
   1.223 +text{*Compatibility binding*}
   1.224 +lemmas zmult_int = int_mult [symmetric]
   1.225 +
   1.226 +lemma zmult_1: "(1::int) * z = z"
   1.227 +by (cases z, simp add: One_int_def int_def mult)
   1.228 +
   1.229 +lemma zmult_1_right: "z * (1::int) = z"
   1.230 +by (rule trans [OF zmult_commute zmult_1])
   1.231 +
   1.232 +
   1.233 +text{*The integers form a @{text comm_ring_1}*}
   1.234 +instance int :: comm_ring_1
   1.235 +proof
   1.236 +  fix i j k :: int
   1.237 +  show "(i + j) + k = i + (j + k)" by (simp add: zadd_assoc)
   1.238 +  show "i + j = j + i" by (simp add: zadd_commute)
   1.239 +  show "0 + i = i" by (rule zadd_0)
   1.240 +  show "- i + i = 0" by (rule zadd_zminus_inverse2)
   1.241 +  show "i - j = i + (-j)" by (simp add: diff_int_def)
   1.242 +  show "(i * j) * k = i * (j * k)" by (rule zmult_assoc)
   1.243 +  show "i * j = j * i" by (rule zmult_commute)
   1.244 +  show "1 * i = i" by (rule zmult_1) 
   1.245 +  show "(i + j) * k = i * k + j * k" by (simp add: int_distrib)
   1.246 +  show "0 \<noteq> (1::int)"
   1.247 +    by (simp only: Zero_int_def One_int_def One_nat_def int_int_eq)
   1.248 +qed
   1.249 +
   1.250 +
   1.251 +subsection{*The @{text "\<le>"} Ordering*}
   1.252 +
   1.253 +lemma le:
   1.254 +  "(Abs_Integ(intrel``{(x,y)}) \<le> Abs_Integ(intrel``{(u,v)})) = (x+v \<le> u+y)"
   1.255 +by (force simp add: le_int_def)
   1.256 +
   1.257 +lemma zle_refl: "w \<le> (w::int)"
   1.258 +by (cases w, simp add: le)
   1.259 +
   1.260 +lemma zle_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::int)"
   1.261 +by (cases i, cases j, cases k, simp add: le)
   1.262 +
   1.263 +lemma zle_anti_sym: "[| z \<le> w; w \<le> z |] ==> z = (w::int)"
   1.264 +by (cases w, cases z, simp add: le)
   1.265 +
   1.266 +instance int :: order
   1.267 +  by intro_classes
   1.268 +    (assumption |
   1.269 +      rule zle_refl zle_trans zle_anti_sym less_int_def [THEN meta_eq_to_obj_eq])+
   1.270 +
   1.271 +lemma zle_linear: "(z::int) \<le> w \<or> w \<le> z"
   1.272 +by (cases z, cases w) (simp add: le linorder_linear)
   1.273 +
   1.274 +instance int :: linorder
   1.275 +  by intro_classes (rule zle_linear)
   1.276 +
   1.277 +lemmas zless_linear = linorder_less_linear [where 'a = int]
   1.278 +
   1.279 +
   1.280 +lemma int_eq_0_conv [simp]: "(int n = 0) = (n = 0)"
   1.281 +by (simp add: Zero_int_def)
   1.282 +
   1.283 +lemma zless_int [simp]: "(int m < int n) = (m<n)"
   1.284 +by (simp add: le add int_def linorder_not_le [symmetric]) 
   1.285 +
   1.286 +lemma int_less_0_conv [simp]: "~ (int k < 0)"
   1.287 +by (simp add: Zero_int_def)
   1.288 +
   1.289 +lemma zero_less_int_conv [simp]: "(0 < int n) = (0 < n)"
   1.290 +by (simp add: Zero_int_def)
   1.291 +
   1.292 +lemma int_0_less_1: "0 < (1::int)"
   1.293 +by (simp only: Zero_int_def One_int_def One_nat_def zless_int)
   1.294 +
   1.295 +lemma int_0_neq_1 [simp]: "0 \<noteq> (1::int)"
   1.296 +by (simp only: Zero_int_def One_int_def One_nat_def int_int_eq)
   1.297 +
   1.298 +lemma zle_int [simp]: "(int m \<le> int n) = (m\<le>n)"
   1.299 +by (simp add: linorder_not_less [symmetric])
   1.300 +
   1.301 +lemma zero_zle_int [simp]: "(0 \<le> int n)"
   1.302 +by (simp add: Zero_int_def)
   1.303 +
   1.304 +lemma int_le_0_conv [simp]: "(int n \<le> 0) = (n = 0)"
   1.305 +by (simp add: Zero_int_def)
   1.306 +
   1.307 +lemma int_0 [simp]: "int 0 = (0::int)"
   1.308 +by (simp add: Zero_int_def)
   1.309 +
   1.310 +lemma int_1 [simp]: "int 1 = 1"
   1.311 +by (simp add: One_int_def)
   1.312 +
   1.313 +lemma int_Suc0_eq_1: "int (Suc 0) = 1"
   1.314 +by (simp add: One_int_def One_nat_def)
   1.315 +
   1.316 +lemma int_Suc: "int (Suc m) = 1 + (int m)"
   1.317 +by (simp add: One_int_def zadd_int)
   1.318 +
   1.319 +
   1.320 +subsection{*Monotonicity results*}
   1.321 +
   1.322 +lemma zadd_left_mono: "i \<le> j ==> k + i \<le> k + (j::int)"
   1.323 +by (cases i, cases j, cases k, simp add: le add)
   1.324 +
   1.325 +lemma zadd_strict_right_mono: "i < j ==> i + k < j + (k::int)"
   1.326 +apply (cases i, cases j, cases k)
   1.327 +apply (simp add: linorder_not_le [where 'a = int, symmetric]
   1.328 +                 linorder_not_le [where 'a = nat]  le add)
   1.329 +done
   1.330 +
   1.331 +lemma zadd_zless_mono: "[| w'<w; z'\<le>z |] ==> w' + z' < w + (z::int)"
   1.332 +by (rule order_less_le_trans [OF zadd_strict_right_mono zadd_left_mono])
   1.333 +
   1.334 +
   1.335 +subsection{*Strict Monotonicity of Multiplication*}
   1.336 +
   1.337 +text{*strict, in 1st argument; proof is by induction on k>0*}
   1.338 +lemma zmult_zless_mono2_lemma:
   1.339 +     "i<j ==> 0<k ==> int k * i < int k * j"
   1.340 +apply (induct "k", simp)
   1.341 +apply (simp add: int_Suc)
   1.342 +apply (case_tac "k=0")
   1.343 +apply (simp_all add: zadd_zmult_distrib int_Suc0_eq_1 order_le_less)
   1.344 +apply (simp add: zadd_zless_mono int_Suc0_eq_1 order_le_less)
   1.345 +done
   1.346 +
   1.347 +lemma zero_le_imp_eq_int: "0 \<le> k ==> \<exists>n. k = int n"
   1.348 +apply (cases k)
   1.349 +apply (auto simp add: le add int_def Zero_int_def)
   1.350 +apply (rule_tac x="x-y" in exI, simp)
   1.351 +done
   1.352 +
   1.353 +lemma zmult_zless_mono2: "[| i<j;  (0::int) < k |] ==> k*i < k*j"
   1.354 +apply (frule order_less_imp_le [THEN zero_le_imp_eq_int])
   1.355 +apply (auto simp add: zmult_zless_mono2_lemma)
   1.356 +done
   1.357 +
   1.358 +instance int :: minus
   1.359 +  zabs_def: "\<bar>i\<Colon>int\<bar> \<equiv> if i < 0 then - i else i" ..
   1.360 +
   1.361 +instance int :: distrib_lattice
   1.362 +  "inf \<equiv> min"
   1.363 +  "sup \<equiv> max"
   1.364 +  by intro_classes
   1.365 +    (auto simp add: inf_int_def sup_int_def min_max.sup_inf_distrib1)
   1.366 +
   1.367 +text{*The integers form an ordered @{text comm_ring_1}*}
   1.368 +instance int :: ordered_idom
   1.369 +proof
   1.370 +  fix i j k :: int
   1.371 +  show "i \<le> j ==> k + i \<le> k + j" by (rule zadd_left_mono)
   1.372 +  show "i < j ==> 0 < k ==> k * i < k * j" by (rule zmult_zless_mono2)
   1.373 +  show "\<bar>i\<bar> = (if i < 0 then -i else i)" by (simp only: zabs_def)
   1.374 +qed
   1.375 +
   1.376 +lemma zless_imp_add1_zle: "w<z ==> w + (1::int) \<le> z"
   1.377 +apply (cases w, cases z) 
   1.378 +apply (simp add: linorder_not_le [symmetric] le int_def add One_int_def)
   1.379 +done
   1.380 +
   1.381 +subsection{*Magnitide of an Integer, as a Natural Number: @{term nat}*}
   1.382 +
   1.383 +definition
   1.384 +  nat :: "int \<Rightarrow> nat"
   1.385 +where
   1.386 +  [code func del]: "nat z = contents (\<Union>(x, y) \<in> Rep_Integ z. {x-y})"
   1.387 +
   1.388 +lemma nat: "nat (Abs_Integ (intrel``{(x,y)})) = x-y"
   1.389 +proof -
   1.390 +  have "(\<lambda>(x,y). {x-y}) respects intrel"
   1.391 +    by (simp add: congruent_def) arith
   1.392 +  thus ?thesis
   1.393 +    by (simp add: nat_def UN_equiv_class [OF equiv_intrel])
   1.394 +qed
   1.395 +
   1.396 +lemma nat_int [simp]: "nat(int n) = n"
   1.397 +by (simp add: nat int_def) 
   1.398 +
   1.399 +lemma nat_zero [simp]: "nat 0 = 0"
   1.400 +by (simp only: Zero_int_def nat_int)
   1.401 +
   1.402 +lemma int_nat_eq [simp]: "int (nat z) = (if 0 \<le> z then z else 0)"
   1.403 +by (cases z, simp add: nat le int_def Zero_int_def)
   1.404 +
   1.405 +corollary nat_0_le: "0 \<le> z ==> int (nat z) = z"
   1.406 +by simp
   1.407 +
   1.408 +lemma nat_le_0 [simp]: "z \<le> 0 ==> nat z = 0"
   1.409 +by (cases z, simp add: nat le int_def Zero_int_def)
   1.410 +
   1.411 +lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)"
   1.412 +apply (cases w, cases z) 
   1.413 +apply (simp add: nat le linorder_not_le [symmetric] int_def Zero_int_def, arith)
   1.414 +done
   1.415 +
   1.416 +text{*An alternative condition is @{term "0 \<le> w"} *}
   1.417 +corollary nat_mono_iff: "0 < z ==> (nat w < nat z) = (w < z)"
   1.418 +by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
   1.419 +
   1.420 +corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)"
   1.421 +by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
   1.422 +
   1.423 +lemma zless_nat_conj: "(nat w < nat z) = (0 < z & w < z)"
   1.424 +apply (cases w, cases z) 
   1.425 +apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
   1.426 +done
   1.427 +
   1.428 +lemma nonneg_eq_int: "[| 0 \<le> z;  !!m. z = int m ==> P |] ==> P"
   1.429 +by (blast dest: nat_0_le sym)
   1.430 +
   1.431 +lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = int m else m=0)"
   1.432 +by (cases w, simp add: nat le int_def Zero_int_def, arith)
   1.433 +
   1.434 +corollary nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = int m else m=0)"
   1.435 +by (simp only: eq_commute [of m] nat_eq_iff) 
   1.436 +
   1.437 +lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < int m)"
   1.438 +apply (cases w)
   1.439 +apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
   1.440 +done
   1.441 +
   1.442 +lemma int_eq_iff: "(int m = z) = (m = nat z & 0 \<le> z)"
   1.443 +by (auto simp add: nat_eq_iff2)
   1.444 +
   1.445 +lemma zero_less_nat_eq [simp]: "(0 < nat z) = (0 < z)"
   1.446 +by (insert zless_nat_conj [of 0], auto)
   1.447 +
   1.448 +lemma nat_add_distrib:
   1.449 +     "[| (0::int) \<le> z;  0 \<le> z' |] ==> nat (z+z') = nat z + nat z'"
   1.450 +by (cases z, cases z', simp add: nat add le int_def Zero_int_def)
   1.451 +
   1.452 +lemma nat_diff_distrib:
   1.453 +     "[| (0::int) \<le> z';  z' \<le> z |] ==> nat (z-z') = nat z - nat z'"
   1.454 +by (cases z, cases z', 
   1.455 +    simp add: nat add minus diff_minus le int_def Zero_int_def)
   1.456 +
   1.457 +
   1.458 +lemma nat_zminus_int [simp]: "nat (- (int n)) = 0"
   1.459 +by (simp add: int_def minus nat Zero_int_def) 
   1.460 +
   1.461 +lemma zless_nat_eq_int_zless: "(m < nat z) = (int m < z)"
   1.462 +by (cases z, simp add: nat le int_def  linorder_not_le [symmetric], arith)
   1.463 +
   1.464 +
   1.465 +subsection{*Lemmas about the Function @{term int} and Orderings*}
   1.466 +
   1.467 +lemma negative_zless_0: "- (int (Suc n)) < 0"
   1.468 +by (simp add: order_less_le)
   1.469 +
   1.470 +lemma negative_zless [iff]: "- (int (Suc n)) < int m"
   1.471 +by (rule negative_zless_0 [THEN order_less_le_trans], simp)
   1.472 +
   1.473 +lemma negative_zle_0: "- int n \<le> 0"
   1.474 +by (simp add: minus_le_iff)
   1.475 +
   1.476 +lemma negative_zle [iff]: "- int n \<le> int m"
   1.477 +by (rule order_trans [OF negative_zle_0 zero_zle_int])
   1.478 +
   1.479 +lemma not_zle_0_negative [simp]: "~ (0 \<le> - (int (Suc n)))"
   1.480 +by (subst le_minus_iff, simp)
   1.481 +
   1.482 +lemma int_zle_neg: "(int n \<le> - int m) = (n = 0 & m = 0)"
   1.483 +by (simp add: int_def le minus Zero_int_def) 
   1.484 +
   1.485 +lemma not_int_zless_negative [simp]: "~ (int n < - int m)"
   1.486 +by (simp add: linorder_not_less)
   1.487 +
   1.488 +lemma negative_eq_positive [simp]: "(- int n = int m) = (n = 0 & m = 0)"
   1.489 +by (force simp add: order_eq_iff [of "- int n"] int_zle_neg)
   1.490 +
   1.491 +lemma zle_iff_zadd: "(w \<le> z) = (\<exists>n. z = w + int n)"
   1.492 +proof (cases w, cases z, simp add: le add int_def)
   1.493 +  fix a b c d
   1.494 +  assume "w = Abs_Integ (intrel `` {(a,b)})" "z = Abs_Integ (intrel `` {(c,d)})"
   1.495 +  show "(a+d \<le> c+b) = (\<exists>n. c+b = a+n+d)"
   1.496 +  proof
   1.497 +    assume "a + d \<le> c + b" 
   1.498 +    thus "\<exists>n. c + b = a + n + d" 
   1.499 +      by (auto intro!: exI [where x="c+b - (a+d)"])
   1.500 +  next    
   1.501 +    assume "\<exists>n. c + b = a + n + d" 
   1.502 +    then obtain n where "c + b = a + n + d" ..
   1.503 +    thus "a + d \<le> c + b" by arith
   1.504 +  qed
   1.505 +qed
   1.506 +
   1.507 +lemma abs_int_eq [simp]: "abs (int m) = int m"
   1.508 +by (simp add: abs_if)
   1.509 +
   1.510 +text{*This version is proved for all ordered rings, not just integers!
   1.511 +      It is proved here because attribute @{text arith_split} is not available
   1.512 +      in theory @{text Ring_and_Field}.
   1.513 +      But is it really better than just rewriting with @{text abs_if}?*}
   1.514 +lemma abs_split [arith_split]:
   1.515 +     "P(abs(a::'a::ordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))"
   1.516 +by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
   1.517 +
   1.518 +
   1.519 +subsection {* Constants @{term neg} and @{term iszero} *}
   1.520 +
   1.521 +definition
   1.522 +  neg  :: "'a\<Colon>ordered_idom \<Rightarrow> bool"
   1.523 +where
   1.524 +  [code inline]: "neg Z \<longleftrightarrow> Z < 0"
   1.525 +
   1.526 +definition (*for simplifying equalities*)
   1.527 +  iszero :: "'a\<Colon>comm_semiring_1_cancel \<Rightarrow> bool"
   1.528 +where
   1.529 +  "iszero z \<longleftrightarrow> z = 0"
   1.530 +
   1.531 +lemma not_neg_int [simp]: "~ neg(int n)"
   1.532 +by (simp add: neg_def)
   1.533 +
   1.534 +lemma neg_zminus_int [simp]: "neg(- (int (Suc n)))"
   1.535 +by (simp add: neg_def neg_less_0_iff_less)
   1.536 +
   1.537 +lemmas neg_eq_less_0 = neg_def
   1.538 +
   1.539 +lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
   1.540 +by (simp add: neg_def linorder_not_less)
   1.541 +
   1.542 +
   1.543 +subsection{*To simplify inequalities when Numeral1 can get simplified to 1*}
   1.544 +
   1.545 +lemma not_neg_0: "~ neg 0"
   1.546 +by (simp add: One_int_def neg_def)
   1.547 +
   1.548 +lemma not_neg_1: "~ neg 1"
   1.549 +by (simp add: neg_def linorder_not_less zero_le_one)
   1.550 +
   1.551 +lemma iszero_0: "iszero 0"
   1.552 +by (simp add: iszero_def)
   1.553 +
   1.554 +lemma not_iszero_1: "~ iszero 1"
   1.555 +by (simp add: iszero_def eq_commute)
   1.556 +
   1.557 +lemma neg_nat: "neg z ==> nat z = 0"
   1.558 +by (simp add: neg_def order_less_imp_le) 
   1.559 +
   1.560 +lemma not_neg_nat: "~ neg z ==> int (nat z) = z"
   1.561 +by (simp add: linorder_not_less neg_def)
   1.562 +
   1.563 +
   1.564 +subsection{*The Set of Natural Numbers*}
   1.565 +
   1.566 +constdefs
   1.567 +  Nats  :: "'a::semiring_1_cancel set"
   1.568 +  "Nats == range of_nat"
   1.569 +
   1.570 +notation (xsymbols)
   1.571 +  Nats  ("\<nat>")
   1.572 +
   1.573 +lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats"
   1.574 +by (simp add: Nats_def)
   1.575 +
   1.576 +lemma Nats_0 [simp]: "0 \<in> Nats"
   1.577 +apply (simp add: Nats_def)
   1.578 +apply (rule range_eqI)
   1.579 +apply (rule of_nat_0 [symmetric])
   1.580 +done
   1.581 +
   1.582 +lemma Nats_1 [simp]: "1 \<in> Nats"
   1.583 +apply (simp add: Nats_def)
   1.584 +apply (rule range_eqI)
   1.585 +apply (rule of_nat_1 [symmetric])
   1.586 +done
   1.587 +
   1.588 +lemma Nats_add [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a+b \<in> Nats"
   1.589 +apply (auto simp add: Nats_def)
   1.590 +apply (rule range_eqI)
   1.591 +apply (rule of_nat_add [symmetric])
   1.592 +done
   1.593 +
   1.594 +lemma Nats_mult [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a*b \<in> Nats"
   1.595 +apply (auto simp add: Nats_def)
   1.596 +apply (rule range_eqI)
   1.597 +apply (rule of_nat_mult [symmetric])
   1.598 +done
   1.599 +
   1.600 +text{*Agreement with the specific embedding for the integers*}
   1.601 +lemma int_eq_of_nat: "int = (of_nat :: nat => int)"
   1.602 +proof
   1.603 +  fix n
   1.604 +  show "int n = of_nat n"  by (induct n, simp_all add: int_Suc add_ac)
   1.605 +qed
   1.606 +
   1.607 +lemma of_nat_eq_id [simp]: "of_nat = (id :: nat => nat)"
   1.608 +proof
   1.609 +  fix n
   1.610 +  show "of_nat n = id n"  by (induct n, simp_all)
   1.611 +qed
   1.612 +
   1.613 +
   1.614 +subsection{*Embedding of the Integers into any @{text ring_1}:
   1.615 +@{term of_int}*}
   1.616 +
   1.617 +constdefs
   1.618 +   of_int :: "int => 'a::ring_1"
   1.619 +   "of_int z == contents (\<Union>(i,j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
   1.620 +
   1.621 +
   1.622 +lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
   1.623 +proof -
   1.624 +  have "(\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) }) respects intrel"
   1.625 +    by (simp add: congruent_def compare_rls of_nat_add [symmetric]
   1.626 +            del: of_nat_add) 
   1.627 +  thus ?thesis
   1.628 +    by (simp add: of_int_def UN_equiv_class [OF equiv_intrel])
   1.629 +qed
   1.630 +
   1.631 +lemma of_int_0 [simp]: "of_int 0 = 0"
   1.632 +by (simp add: of_int Zero_int_def int_def)
   1.633 +
   1.634 +lemma of_int_1 [simp]: "of_int 1 = 1"
   1.635 +by (simp add: of_int One_int_def int_def)
   1.636 +
   1.637 +lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z"
   1.638 +by (cases w, cases z, simp add: compare_rls of_int add)
   1.639 +
   1.640 +lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)"
   1.641 +by (cases z, simp add: compare_rls of_int minus)
   1.642 +
   1.643 +lemma of_int_diff [simp]: "of_int (w-z) = of_int w - of_int z"
   1.644 +by (simp add: diff_minus)
   1.645 +
   1.646 +lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z"
   1.647 +apply (cases w, cases z)
   1.648 +apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib
   1.649 +                 mult add_ac)
   1.650 +done
   1.651 +
   1.652 +lemma of_int_le_iff [simp]:
   1.653 +     "(of_int w \<le> (of_int z::'a::ordered_idom)) = (w \<le> z)"
   1.654 +apply (cases w)
   1.655 +apply (cases z)
   1.656 +apply (simp add: compare_rls of_int le diff_int_def add minus
   1.657 +                 of_nat_add [symmetric]   del: of_nat_add)
   1.658 +done
   1.659 +
   1.660 +text{*Special cases where either operand is zero*}
   1.661 +lemmas of_int_0_le_iff [simp] = of_int_le_iff [of 0, simplified]
   1.662 +lemmas of_int_le_0_iff [simp] = of_int_le_iff [of _ 0, simplified]
   1.663 +
   1.664 +
   1.665 +lemma of_int_less_iff [simp]:
   1.666 +     "(of_int w < (of_int z::'a::ordered_idom)) = (w < z)"
   1.667 +by (simp add: linorder_not_le [symmetric])
   1.668 +
   1.669 +text{*Special cases where either operand is zero*}
   1.670 +lemmas of_int_0_less_iff [simp] = of_int_less_iff [of 0, simplified]
   1.671 +lemmas of_int_less_0_iff [simp] = of_int_less_iff [of _ 0, simplified]
   1.672 +
   1.673 +text{*Class for unital rings with characteristic zero.
   1.674 + Includes non-ordered rings like the complex numbers.*}
   1.675 +axclass ring_char_0 < ring_1
   1.676 +  of_int_inject: "of_int w = of_int z ==> w = z"
   1.677 +
   1.678 +lemma of_int_eq_iff [simp]:
   1.679 +     "(of_int w = (of_int z::'a::ring_char_0)) = (w = z)"
   1.680 +by (safe elim!: of_int_inject)
   1.681 +
   1.682 +text{*Every @{text ordered_idom} has characteristic zero.*}
   1.683 +instance ordered_idom < ring_char_0
   1.684 +by intro_classes (simp add: order_eq_iff)
   1.685 +
   1.686 +text{*Special cases where either operand is zero*}
   1.687 +lemmas of_int_0_eq_iff [simp] = of_int_eq_iff [of 0, simplified]
   1.688 +lemmas of_int_eq_0_iff [simp] = of_int_eq_iff [of _ 0, simplified]
   1.689 +
   1.690 +lemma of_int_eq_id [simp]: "of_int = (id :: int => int)"
   1.691 +proof
   1.692 +  fix z
   1.693 +  show "of_int z = id z"  
   1.694 +    by (cases z)
   1.695 +      (simp add: of_int add minus int_eq_of_nat [symmetric] int_def diff_minus)
   1.696 +qed
   1.697 +
   1.698 +
   1.699 +subsection{*The Set of Integers*}
   1.700 +
   1.701 +constdefs
   1.702 +  Ints  :: "'a::ring_1 set"
   1.703 +  "Ints == range of_int"
   1.704 +
   1.705 +notation (xsymbols)
   1.706 +  Ints  ("\<int>")
   1.707 +
   1.708 +lemma Ints_0 [simp]: "0 \<in> Ints"
   1.709 +apply (simp add: Ints_def)
   1.710 +apply (rule range_eqI)
   1.711 +apply (rule of_int_0 [symmetric])
   1.712 +done
   1.713 +
   1.714 +lemma Ints_1 [simp]: "1 \<in> Ints"
   1.715 +apply (simp add: Ints_def)
   1.716 +apply (rule range_eqI)
   1.717 +apply (rule of_int_1 [symmetric])
   1.718 +done
   1.719 +
   1.720 +lemma Ints_add [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a+b \<in> Ints"
   1.721 +apply (auto simp add: Ints_def)
   1.722 +apply (rule range_eqI)
   1.723 +apply (rule of_int_add [symmetric])
   1.724 +done
   1.725 +
   1.726 +lemma Ints_minus [simp]: "a \<in> Ints ==> -a \<in> Ints"
   1.727 +apply (auto simp add: Ints_def)
   1.728 +apply (rule range_eqI)
   1.729 +apply (rule of_int_minus [symmetric])
   1.730 +done
   1.731 +
   1.732 +lemma Ints_diff [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a-b \<in> Ints"
   1.733 +apply (auto simp add: Ints_def)
   1.734 +apply (rule range_eqI)
   1.735 +apply (rule of_int_diff [symmetric])
   1.736 +done
   1.737 +
   1.738 +lemma Ints_mult [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a*b \<in> Ints"
   1.739 +apply (auto simp add: Ints_def)
   1.740 +apply (rule range_eqI)
   1.741 +apply (rule of_int_mult [symmetric])
   1.742 +done
   1.743 +
   1.744 +text{*Collapse nested embeddings*}
   1.745 +lemma of_int_of_nat_eq [simp]: "of_int (of_nat n) = of_nat n"
   1.746 +by (induct n, auto)
   1.747 +
   1.748 +lemma of_int_int_eq [simp]: "of_int (int n) = of_nat n"
   1.749 +by (simp add: int_eq_of_nat)
   1.750 +
   1.751 +lemma Ints_cases [cases set: Ints]:
   1.752 +  assumes "q \<in> \<int>"
   1.753 +  obtains (of_int) z where "q = of_int z"
   1.754 +  unfolding Ints_def
   1.755 +proof -
   1.756 +  from `q \<in> \<int>` have "q \<in> range of_int" unfolding Ints_def .
   1.757 +  then obtain z where "q = of_int z" ..
   1.758 +  then show thesis ..
   1.759 +qed
   1.760 +
   1.761 +lemma Ints_induct [case_names of_int, induct set: Ints]:
   1.762 +  "q \<in> \<int> ==> (!!z. P (of_int z)) ==> P q"
   1.763 +  by (rule Ints_cases) auto
   1.764 +
   1.765 +
   1.766 +(* int (Suc n) = 1 + int n *)
   1.767 +
   1.768 +
   1.769 +
   1.770 +subsection{*More Properties of @{term setsum} and  @{term setprod}*}
   1.771 +
   1.772 +text{*By Jeremy Avigad*}
   1.773 +
   1.774 +
   1.775 +lemma of_nat_setsum: "of_nat (setsum f A) = (\<Sum>x\<in>A. of_nat(f x))"
   1.776 +  apply (cases "finite A")
   1.777 +  apply (erule finite_induct, auto)
   1.778 +  done
   1.779 +
   1.780 +lemma of_int_setsum: "of_int (setsum f A) = (\<Sum>x\<in>A. of_int(f x))"
   1.781 +  apply (cases "finite A")
   1.782 +  apply (erule finite_induct, auto)
   1.783 +  done
   1.784 +
   1.785 +lemma int_setsum: "int (setsum f A) = (\<Sum>x\<in>A. int(f x))"
   1.786 +  by (simp add: int_eq_of_nat of_nat_setsum)
   1.787 +
   1.788 +lemma of_nat_setprod: "of_nat (setprod f A) = (\<Prod>x\<in>A. of_nat(f x))"
   1.789 +  apply (cases "finite A")
   1.790 +  apply (erule finite_induct, auto)
   1.791 +  done
   1.792 +
   1.793 +lemma of_int_setprod: "of_int (setprod f A) = (\<Prod>x\<in>A. of_int(f x))"
   1.794 +  apply (cases "finite A")
   1.795 +  apply (erule finite_induct, auto)
   1.796 +  done
   1.797 +
   1.798 +lemma int_setprod: "int (setprod f A) = (\<Prod>x\<in>A. int(f x))"
   1.799 +  by (simp add: int_eq_of_nat of_nat_setprod)
   1.800 +
   1.801 +lemma setprod_nonzero_nat:
   1.802 +    "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::nat)) ==> setprod f A \<noteq> 0"
   1.803 +  by (rule setprod_nonzero, auto)
   1.804 +
   1.805 +lemma setprod_zero_eq_nat:
   1.806 +    "finite A ==> (setprod f A = (0::nat)) = (\<exists>x \<in> A. f x = 0)"
   1.807 +  by (rule setprod_zero_eq, auto)
   1.808 +
   1.809 +lemma setprod_nonzero_int:
   1.810 +    "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::int)) ==> setprod f A \<noteq> 0"
   1.811 +  by (rule setprod_nonzero, auto)
   1.812 +
   1.813 +lemma setprod_zero_eq_int:
   1.814 +    "finite A ==> (setprod f A = (0::int)) = (\<exists>x \<in> A. f x = 0)"
   1.815 +  by (rule setprod_zero_eq, auto)
   1.816 +
   1.817 +
   1.818 +subsection {* Further properties *}
   1.819 +
   1.820 +text{*Now we replace the case analysis rule by a more conventional one:
   1.821 +whether an integer is negative or not.*}
   1.822 +
   1.823 +lemma zless_iff_Suc_zadd:
   1.824 +    "(w < z) = (\<exists>n. z = w + int(Suc n))"
   1.825 +apply (cases z, cases w)
   1.826 +apply (auto simp add: le add int_def linorder_not_le [symmetric]) 
   1.827 +apply (rename_tac a b c d) 
   1.828 +apply (rule_tac x="a+d - Suc(c+b)" in exI) 
   1.829 +apply arith
   1.830 +done
   1.831 +
   1.832 +lemma negD: "x<0 ==> \<exists>n. x = - (int (Suc n))"
   1.833 +apply (cases x)
   1.834 +apply (auto simp add: le minus Zero_int_def int_def order_less_le) 
   1.835 +apply (rule_tac x="y - Suc x" in exI, arith)
   1.836 +done
   1.837 +
   1.838 +theorem int_cases [cases type: int, case_names nonneg neg]:
   1.839 +     "[|!! n. z = int n ==> P;  !! n. z =  - (int (Suc n)) ==> P |] ==> P"
   1.840 +apply (cases "z < 0", blast dest!: negD)
   1.841 +apply (simp add: linorder_not_less)
   1.842 +apply (blast dest: nat_0_le [THEN sym])
   1.843 +done
   1.844 +
   1.845 +theorem int_induct [induct type: int, case_names nonneg neg]:
   1.846 +     "[|!! n. P (int n);  !!n. P (- (int (Suc n))) |] ==> P z"
   1.847 +  by (cases z) auto
   1.848 +
   1.849 +text{*Contributed by Brian Huffman*}
   1.850 +theorem int_diff_cases [case_names diff]:
   1.851 +assumes prem: "!!m n. z = int m - int n ==> P" shows "P"
   1.852 + apply (rule_tac z=z in int_cases)
   1.853 +  apply (rule_tac m=n and n=0 in prem, simp)
   1.854 + apply (rule_tac m=0 and n="Suc n" in prem, simp)
   1.855 +done
   1.856 +
   1.857 +lemma of_nat_nat: "0 \<le> z ==> of_nat (nat z) = of_int z"
   1.858 +apply (cases z)
   1.859 +apply (simp_all add: not_zle_0_negative del: int_Suc)
   1.860 +done
   1.861 +
   1.862 +lemmas zless_le = less_int_def [THEN meta_eq_to_obj_eq]
   1.863 +
   1.864 +lemmas [simp] = int_Suc
   1.865 +
   1.866 +
   1.867 +subsection {* Legacy ML bindings *}
   1.868 +
   1.869 +ML {*
   1.870 +val of_nat_0 = @{thm of_nat_0};
   1.871 +val of_nat_1 = @{thm of_nat_1};
   1.872 +val of_nat_Suc = @{thm of_nat_Suc};
   1.873 +val of_nat_add = @{thm of_nat_add};
   1.874 +val of_nat_mult = @{thm of_nat_mult};
   1.875 +val of_int_0 = @{thm of_int_0};
   1.876 +val of_int_1 = @{thm of_int_1};
   1.877 +val of_int_add = @{thm of_int_add};
   1.878 +val of_int_mult = @{thm of_int_mult};
   1.879 +val int_eq_of_nat = @{thm int_eq_of_nat};
   1.880 +val zle_int = @{thm zle_int};
   1.881 +val int_int_eq = @{thm int_int_eq};
   1.882 +val diff_int_def = @{thm diff_int_def};
   1.883 +val zadd_ac = @{thms zadd_ac};
   1.884 +val zless_int = @{thm zless_int};
   1.885 +val zadd_int = @{thm zadd_int};
   1.886 +val zmult_int = @{thm zmult_int};
   1.887 +val nat_0_le = @{thm nat_0_le};
   1.888 +val int_0 = @{thm int_0};
   1.889 +val int_1 = @{thm int_1};
   1.890 +val abs_split = @{thm abs_split};
   1.891 +*}
   1.892 +
   1.893 +end