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