src/HOL/Int.thy
author haftmann
Mon, 26 Apr 2010 15:37:50 +0200
changeset 36409 d323e7773aa8
parent 36348 39be26d1bc28
child 36419 f3f389fc7974
permissions -rw-r--r--
use new classes (linordered_)field_inverse_zero
     1 (*  Title:      Int.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3                 Tobias Nipkow, Florian Haftmann, TU Muenchen
     4     Copyright   1994  University of Cambridge
     5 
     6 *)
     7 
     8 header {* The Integers as Equivalence Classes over Pairs of Natural Numbers *} 
     9 
    10 theory Int
    11 imports Equiv_Relations Nat Wellfounded
    12 uses
    13   ("Tools/numeral.ML")
    14   ("Tools/numeral_syntax.ML")
    15   ("Tools/int_arith.ML")
    16 begin
    17 
    18 subsection {* The equivalence relation underlying the integers *}
    19 
    20 definition intrel :: "((nat \<times> nat) \<times> (nat \<times> nat)) set" where
    21   [code del]: "intrel = {((x, y), (u, v)) | x y u v. x + v = u +y }"
    22 
    23 typedef (Integ)
    24   int = "UNIV//intrel"
    25   by (auto simp add: quotient_def)
    26 
    27 instantiation int :: "{zero, one, plus, minus, uminus, times, ord, abs, sgn}"
    28 begin
    29 
    30 definition
    31   Zero_int_def [code del]: "0 = Abs_Integ (intrel `` {(0, 0)})"
    32 
    33 definition
    34   One_int_def [code del]: "1 = Abs_Integ (intrel `` {(1, 0)})"
    35 
    36 definition
    37   add_int_def [code del]: "z + w = 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 definition
    42   minus_int_def [code del]:
    43     "- z = Abs_Integ (\<Union>(x, y) \<in> Rep_Integ z. intrel `` {(y, x)})"
    44 
    45 definition
    46   diff_int_def [code del]:  "z - w = z + (-w \<Colon> int)"
    47 
    48 definition
    49   mult_int_def [code del]: "z * w = Abs_Integ
    50     (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u,v ) \<in> Rep_Integ w.
    51       intrel `` {(x*u + y*v, x*v + y*u)})"
    52 
    53 definition
    54   le_int_def [code del]:
    55    "z \<le> w \<longleftrightarrow> (\<exists>x y u v. x+v \<le> u+y \<and> (x, y) \<in> Rep_Integ z \<and> (u, v) \<in> Rep_Integ w)"
    56 
    57 definition
    58   less_int_def [code del]: "(z\<Colon>int) < w \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
    59 
    60 definition
    61   zabs_def: "\<bar>i\<Colon>int\<bar> = (if i < 0 then - i else i)"
    62 
    63 definition
    64   zsgn_def: "sgn (i\<Colon>int) = (if i=0 then 0 else if 0<i then 1 else - 1)"
    65 
    66 instance ..
    67 
    68 end
    69 
    70 
    71 subsection{*Construction of the Integers*}
    72 
    73 lemma intrel_iff [simp]: "(((x,y),(u,v)) \<in> intrel) = (x+v = u+y)"
    74 by (simp add: intrel_def)
    75 
    76 lemma equiv_intrel: "equiv UNIV intrel"
    77 by (simp add: intrel_def equiv_def refl_on_def sym_def trans_def)
    78 
    79 text{*Reduces equality of equivalence classes to the @{term intrel} relation:
    80   @{term "(intrel `` {x} = intrel `` {y}) = ((x,y) \<in> intrel)"} *}
    81 lemmas equiv_intrel_iff [simp] = eq_equiv_class_iff [OF equiv_intrel UNIV_I UNIV_I]
    82 
    83 text{*All equivalence classes belong to set of representatives*}
    84 lemma [simp]: "intrel``{(x,y)} \<in> Integ"
    85 by (auto simp add: Integ_def intrel_def quotient_def)
    86 
    87 text{*Reduces equality on abstractions to equality on representatives:
    88   @{prop "\<lbrakk>x \<in> Integ; y \<in> Integ\<rbrakk> \<Longrightarrow> (Abs_Integ x = Abs_Integ y) = (x=y)"} *}
    89 declare Abs_Integ_inject [simp,no_atp]  Abs_Integ_inverse [simp,no_atp]
    90 
    91 text{*Case analysis on the representation of an integer as an equivalence
    92       class of pairs of naturals.*}
    93 lemma eq_Abs_Integ [case_names Abs_Integ, cases type: int]:
    94      "(!!x y. z = Abs_Integ(intrel``{(x,y)}) ==> P) ==> P"
    95 apply (rule Abs_Integ_cases [of z]) 
    96 apply (auto simp add: Integ_def quotient_def) 
    97 done
    98 
    99 
   100 subsection {* Arithmetic Operations *}
   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 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 text{*Congruence property for multiplication*}
   123 lemma mult_congruent2:
   124      "(%p1 p2. (%(x,y). (%(u,v). intrel``{(x*u + y*v, x*v + y*u)}) p2) p1)
   125       respects2 intrel"
   126 apply (rule equiv_intrel [THEN congruent2_commuteI])
   127  apply (force simp add: mult_ac, clarify) 
   128 apply (simp add: congruent_def mult_ac)  
   129 apply (rename_tac u v w x y z)
   130 apply (subgoal_tac "u*y + x*y = w*y + v*y  &  u*z + x*z = w*z + v*z")
   131 apply (simp add: mult_ac)
   132 apply (simp add: add_mult_distrib [symmetric])
   133 done
   134 
   135 lemma mult:
   136      "Abs_Integ((intrel``{(x,y)})) * Abs_Integ((intrel``{(u,v)})) =
   137       Abs_Integ(intrel `` {(x*u + y*v, x*v + y*u)})"
   138 by (simp add: mult_int_def UN_UN_split_split_eq mult_congruent2
   139               UN_equiv_class2 [OF equiv_intrel equiv_intrel])
   140 
   141 text{*The integers form a @{text comm_ring_1}*}
   142 instance int :: comm_ring_1
   143 proof
   144   fix i j k :: int
   145   show "(i + j) + k = i + (j + k)"
   146     by (cases i, cases j, cases k) (simp add: add add_assoc)
   147   show "i + j = j + i" 
   148     by (cases i, cases j) (simp add: add_ac add)
   149   show "0 + i = i"
   150     by (cases i) (simp add: Zero_int_def add)
   151   show "- i + i = 0"
   152     by (cases i) (simp add: Zero_int_def minus add)
   153   show "i - j = i + - j"
   154     by (simp add: diff_int_def)
   155   show "(i * j) * k = i * (j * k)"
   156     by (cases i, cases j, cases k) (simp add: mult algebra_simps)
   157   show "i * j = j * i"
   158     by (cases i, cases j) (simp add: mult algebra_simps)
   159   show "1 * i = i"
   160     by (cases i) (simp add: One_int_def mult)
   161   show "(i + j) * k = i * k + j * k"
   162     by (cases i, cases j, cases k) (simp add: add mult algebra_simps)
   163   show "0 \<noteq> (1::int)"
   164     by (simp add: Zero_int_def One_int_def)
   165 qed
   166 
   167 lemma int_def: "of_nat m = Abs_Integ (intrel `` {(m, 0)})"
   168 by (induct m, simp_all add: Zero_int_def One_int_def add)
   169 
   170 
   171 subsection {* The @{text "\<le>"} Ordering *}
   172 
   173 lemma le:
   174   "(Abs_Integ(intrel``{(x,y)}) \<le> Abs_Integ(intrel``{(u,v)})) = (x+v \<le> u+y)"
   175 by (force simp add: le_int_def)
   176 
   177 lemma less:
   178   "(Abs_Integ(intrel``{(x,y)}) < Abs_Integ(intrel``{(u,v)})) = (x+v < u+y)"
   179 by (simp add: less_int_def le order_less_le)
   180 
   181 instance int :: linorder
   182 proof
   183   fix i j k :: int
   184   show antisym: "i \<le> j \<Longrightarrow> j \<le> i \<Longrightarrow> i = j"
   185     by (cases i, cases j) (simp add: le)
   186   show "(i < j) = (i \<le> j \<and> \<not> j \<le> i)"
   187     by (auto simp add: less_int_def dest: antisym) 
   188   show "i \<le> i"
   189     by (cases i) (simp add: le)
   190   show "i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> i \<le> k"
   191     by (cases i, cases j, cases k) (simp add: le)
   192   show "i \<le> j \<or> j \<le> i"
   193     by (cases i, cases j) (simp add: le linorder_linear)
   194 qed
   195 
   196 instantiation int :: distrib_lattice
   197 begin
   198 
   199 definition
   200   "(inf \<Colon> int \<Rightarrow> int \<Rightarrow> int) = min"
   201 
   202 definition
   203   "(sup \<Colon> int \<Rightarrow> int \<Rightarrow> int) = max"
   204 
   205 instance
   206   by intro_classes
   207     (auto simp add: inf_int_def sup_int_def min_max.sup_inf_distrib1)
   208 
   209 end
   210 
   211 instance int :: ordered_cancel_ab_semigroup_add
   212 proof
   213   fix i j k :: int
   214   show "i \<le> j \<Longrightarrow> k + i \<le> k + j"
   215     by (cases i, cases j, cases k) (simp add: le add)
   216 qed
   217 
   218 
   219 text{*Strict Monotonicity of Multiplication*}
   220 
   221 text{*strict, in 1st argument; proof is by induction on k>0*}
   222 lemma zmult_zless_mono2_lemma:
   223      "(i::int)<j ==> 0<k ==> of_nat k * i < of_nat k * j"
   224 apply (induct "k", simp)
   225 apply (simp add: left_distrib)
   226 apply (case_tac "k=0")
   227 apply (simp_all add: add_strict_mono)
   228 done
   229 
   230 lemma zero_le_imp_eq_int: "(0::int) \<le> k ==> \<exists>n. k = of_nat n"
   231 apply (cases k)
   232 apply (auto simp add: le add int_def Zero_int_def)
   233 apply (rule_tac x="x-y" in exI, simp)
   234 done
   235 
   236 lemma zero_less_imp_eq_int: "(0::int) < k ==> \<exists>n>0. k = of_nat n"
   237 apply (cases k)
   238 apply (simp add: less int_def Zero_int_def)
   239 apply (rule_tac x="x-y" in exI, simp)
   240 done
   241 
   242 lemma zmult_zless_mono2: "[| i<j;  (0::int) < k |] ==> k*i < k*j"
   243 apply (drule zero_less_imp_eq_int)
   244 apply (auto simp add: zmult_zless_mono2_lemma)
   245 done
   246 
   247 text{*The integers form an ordered integral domain*}
   248 instance int :: linordered_idom
   249 proof
   250   fix i j k :: int
   251   show "i < j \<Longrightarrow> 0 < k \<Longrightarrow> k * i < k * j"
   252     by (rule zmult_zless_mono2)
   253   show "\<bar>i\<bar> = (if i < 0 then -i else i)"
   254     by (simp only: zabs_def)
   255   show "sgn (i\<Colon>int) = (if i=0 then 0 else if 0<i then 1 else - 1)"
   256     by (simp only: zsgn_def)
   257 qed
   258 
   259 lemma zless_imp_add1_zle: "w < z \<Longrightarrow> w + (1\<Colon>int) \<le> z"
   260 apply (cases w, cases z) 
   261 apply (simp add: less le add One_int_def)
   262 done
   263 
   264 lemma zless_iff_Suc_zadd:
   265   "(w \<Colon> int) < z \<longleftrightarrow> (\<exists>n. z = w + of_nat (Suc n))"
   266 apply (cases z, cases w)
   267 apply (auto simp add: less add int_def)
   268 apply (rename_tac a b c d) 
   269 apply (rule_tac x="a+d - Suc(c+b)" in exI) 
   270 apply arith
   271 done
   272 
   273 lemmas int_distrib =
   274   left_distrib [of "z1::int" "z2" "w", standard]
   275   right_distrib [of "w::int" "z1" "z2", standard]
   276   left_diff_distrib [of "z1::int" "z2" "w", standard]
   277   right_diff_distrib [of "w::int" "z1" "z2", standard]
   278 
   279 
   280 subsection {* Embedding of the Integers into any @{text ring_1}: @{text of_int}*}
   281 
   282 context ring_1
   283 begin
   284 
   285 definition of_int :: "int \<Rightarrow> 'a" where
   286   [code del]: "of_int z = contents (\<Union>(i, j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
   287 
   288 lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
   289 proof -
   290   have "(\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) }) respects intrel"
   291     by (simp add: congruent_def algebra_simps of_nat_add [symmetric]
   292             del: of_nat_add) 
   293   thus ?thesis
   294     by (simp add: of_int_def UN_equiv_class [OF equiv_intrel])
   295 qed
   296 
   297 lemma of_int_0 [simp]: "of_int 0 = 0"
   298 by (simp add: of_int Zero_int_def)
   299 
   300 lemma of_int_1 [simp]: "of_int 1 = 1"
   301 by (simp add: of_int One_int_def)
   302 
   303 lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z"
   304 by (cases w, cases z, simp add: algebra_simps of_int add)
   305 
   306 lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)"
   307 by (cases z, simp add: algebra_simps of_int minus)
   308 
   309 lemma of_int_diff [simp]: "of_int (w - z) = of_int w - of_int z"
   310 by (simp add: diff_minus Groups.diff_minus)
   311 
   312 lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z"
   313 apply (cases w, cases z)
   314 apply (simp add: algebra_simps of_int mult of_nat_mult)
   315 done
   316 
   317 text{*Collapse nested embeddings*}
   318 lemma of_int_of_nat_eq [simp]: "of_int (of_nat n) = of_nat n"
   319 by (induct n) auto
   320 
   321 lemma of_int_power:
   322   "of_int (z ^ n) = of_int z ^ n"
   323   by (induct n) simp_all
   324 
   325 end
   326 
   327 context linordered_idom
   328 begin
   329 
   330 lemma of_int_le_iff [simp]:
   331   "of_int w \<le> of_int z \<longleftrightarrow> w \<le> z"
   332 by (cases w, cases z, simp add: of_int le minus algebra_simps of_nat_add [symmetric] del: of_nat_add)
   333 
   334 text{*Special cases where either operand is zero*}
   335 lemmas of_int_0_le_iff [simp] = of_int_le_iff [of 0, simplified]
   336 lemmas of_int_le_0_iff [simp] = of_int_le_iff [of _ 0, simplified]
   337 
   338 lemma of_int_less_iff [simp]:
   339   "of_int w < of_int z \<longleftrightarrow> w < z"
   340   by (simp add: not_le [symmetric] linorder_not_le [symmetric])
   341 
   342 text{*Special cases where either operand is zero*}
   343 lemmas of_int_0_less_iff [simp] = of_int_less_iff [of 0, simplified]
   344 lemmas of_int_less_0_iff [simp] = of_int_less_iff [of _ 0, simplified]
   345 
   346 end
   347 
   348 text{*Class for unital rings with characteristic zero.
   349  Includes non-ordered rings like the complex numbers.*}
   350 class ring_char_0 = ring_1 + semiring_char_0
   351 begin
   352 
   353 lemma of_int_eq_iff [simp]:
   354    "of_int w = of_int z \<longleftrightarrow> w = z"
   355 apply (cases w, cases z, simp add: of_int)
   356 apply (simp only: diff_eq_eq diff_add_eq eq_diff_eq)
   357 apply (simp only: of_nat_add [symmetric] of_nat_eq_iff)
   358 done
   359 
   360 text{*Special cases where either operand is zero*}
   361 lemmas of_int_0_eq_iff [simp] = of_int_eq_iff [of 0, simplified]
   362 lemmas of_int_eq_0_iff [simp] = of_int_eq_iff [of _ 0, simplified]
   363 
   364 end
   365 
   366 text{*Every @{text linordered_idom} has characteristic zero.*}
   367 subclass (in linordered_idom) ring_char_0 by intro_locales
   368 
   369 lemma of_int_eq_id [simp]: "of_int = id"
   370 proof
   371   fix z show "of_int z = id z"
   372     by (cases z) (simp add: of_int add minus int_def diff_minus)
   373 qed
   374 
   375 
   376 subsection {* Magnitude of an Integer, as a Natural Number: @{text nat} *}
   377 
   378 definition
   379   nat :: "int \<Rightarrow> nat"
   380 where
   381   [code del]: "nat z = contents (\<Union>(x, y) \<in> Rep_Integ z. {x-y})"
   382 
   383 lemma nat: "nat (Abs_Integ (intrel``{(x,y)})) = x-y"
   384 proof -
   385   have "(\<lambda>(x,y). {x-y}) respects intrel"
   386     by (simp add: congruent_def) arith
   387   thus ?thesis
   388     by (simp add: nat_def UN_equiv_class [OF equiv_intrel])
   389 qed
   390 
   391 lemma nat_int [simp]: "nat (of_nat n) = n"
   392 by (simp add: nat int_def)
   393 
   394 (* FIXME: duplicates nat_0 *)
   395 lemma nat_zero [simp]: "nat 0 = 0"
   396 by (simp add: Zero_int_def nat)
   397 
   398 lemma int_nat_eq [simp]: "of_nat (nat z) = (if 0 \<le> z then z else 0)"
   399 by (cases z, simp add: nat le int_def Zero_int_def)
   400 
   401 corollary nat_0_le: "0 \<le> z ==> of_nat (nat z) = z"
   402 by simp
   403 
   404 lemma nat_le_0 [simp]: "z \<le> 0 ==> nat z = 0"
   405 by (cases z, simp add: nat le Zero_int_def)
   406 
   407 lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)"
   408 apply (cases w, cases z) 
   409 apply (simp add: nat le linorder_not_le [symmetric] Zero_int_def, arith)
   410 done
   411 
   412 text{*An alternative condition is @{term "0 \<le> w"} *}
   413 corollary nat_mono_iff: "0 < z ==> (nat w < nat z) = (w < z)"
   414 by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
   415 
   416 corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)"
   417 by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
   418 
   419 lemma zless_nat_conj [simp]: "(nat w < nat z) = (0 < z & w < z)"
   420 apply (cases w, cases z) 
   421 apply (simp add: nat le Zero_int_def linorder_not_le [symmetric], arith)
   422 done
   423 
   424 lemma nonneg_eq_int:
   425   fixes z :: int
   426   assumes "0 \<le> z" and "\<And>m. z = of_nat m \<Longrightarrow> P"
   427   shows P
   428   using assms by (blast dest: nat_0_le sym)
   429 
   430 lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = of_nat m else m=0)"
   431 by (cases w, simp add: nat le int_def Zero_int_def, arith)
   432 
   433 corollary nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = of_nat m else m=0)"
   434 by (simp only: eq_commute [of m] nat_eq_iff)
   435 
   436 lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < of_nat m)"
   437 apply (cases w)
   438 apply (simp add: nat le int_def Zero_int_def linorder_not_le[symmetric], arith)
   439 done
   440 
   441 lemma nat_0_iff[simp]: "nat(i::int) = 0 \<longleftrightarrow> i\<le>0"
   442 by(simp add: nat_eq_iff) arith
   443 
   444 lemma int_eq_iff: "(of_nat m = z) = (m = nat z & 0 \<le> z)"
   445 by (auto simp add: nat_eq_iff2)
   446 
   447 lemma zero_less_nat_eq [simp]: "(0 < nat z) = (0 < z)"
   448 by (insert zless_nat_conj [of 0], auto)
   449 
   450 lemma nat_add_distrib:
   451      "[| (0::int) \<le> z;  0 \<le> z' |] ==> nat (z+z') = nat z + nat z'"
   452 by (cases z, cases z', simp add: nat add le Zero_int_def)
   453 
   454 lemma nat_diff_distrib:
   455      "[| (0::int) \<le> z';  z' \<le> z |] ==> nat (z-z') = nat z - nat z'"
   456 by (cases z, cases z', 
   457     simp add: nat add minus diff_minus le Zero_int_def)
   458 
   459 lemma nat_zminus_int [simp]: "nat (- (of_nat n)) = 0"
   460 by (simp add: int_def minus nat Zero_int_def) 
   461 
   462 lemma zless_nat_eq_int_zless: "(m < nat z) = (of_nat m < z)"
   463 by (cases z, simp add: nat less int_def, arith)
   464 
   465 context ring_1
   466 begin
   467 
   468 lemma of_nat_nat: "0 \<le> z \<Longrightarrow> of_nat (nat z) = of_int z"
   469   by (cases z rule: eq_Abs_Integ)
   470    (simp add: nat le of_int Zero_int_def of_nat_diff)
   471 
   472 end
   473 
   474 text {* For termination proofs: *}
   475 lemma measure_function_int[measure_function]: "is_measure (nat o abs)" ..
   476 
   477 
   478 subsection{*Lemmas about the Function @{term of_nat} and Orderings*}
   479 
   480 lemma negative_zless_0: "- (of_nat (Suc n)) < (0 \<Colon> int)"
   481 by (simp add: order_less_le del: of_nat_Suc)
   482 
   483 lemma negative_zless [iff]: "- (of_nat (Suc n)) < (of_nat m \<Colon> int)"
   484 by (rule negative_zless_0 [THEN order_less_le_trans], simp)
   485 
   486 lemma negative_zle_0: "- of_nat n \<le> (0 \<Colon> int)"
   487 by (simp add: minus_le_iff)
   488 
   489 lemma negative_zle [iff]: "- of_nat n \<le> (of_nat m \<Colon> int)"
   490 by (rule order_trans [OF negative_zle_0 of_nat_0_le_iff])
   491 
   492 lemma not_zle_0_negative [simp]: "~ (0 \<le> - (of_nat (Suc n) \<Colon> int))"
   493 by (subst le_minus_iff, simp del: of_nat_Suc)
   494 
   495 lemma int_zle_neg: "((of_nat n \<Colon> int) \<le> - of_nat m) = (n = 0 & m = 0)"
   496 by (simp add: int_def le minus Zero_int_def)
   497 
   498 lemma not_int_zless_negative [simp]: "~ ((of_nat n \<Colon> int) < - of_nat m)"
   499 by (simp add: linorder_not_less)
   500 
   501 lemma negative_eq_positive [simp]: "((- of_nat n \<Colon> int) = of_nat m) = (n = 0 & m = 0)"
   502 by (force simp add: order_eq_iff [of "- of_nat n"] int_zle_neg)
   503 
   504 lemma zle_iff_zadd: "(w\<Colon>int) \<le> z \<longleftrightarrow> (\<exists>n. z = w + of_nat n)"
   505 proof -
   506   have "(w \<le> z) = (0 \<le> z - w)"
   507     by (simp only: le_diff_eq add_0_left)
   508   also have "\<dots> = (\<exists>n. z - w = of_nat n)"
   509     by (auto elim: zero_le_imp_eq_int)
   510   also have "\<dots> = (\<exists>n. z = w + of_nat n)"
   511     by (simp only: algebra_simps)
   512   finally show ?thesis .
   513 qed
   514 
   515 lemma zadd_int_left: "of_nat m + (of_nat n + z) = of_nat (m + n) + (z\<Colon>int)"
   516 by simp
   517 
   518 lemma int_Suc0_eq_1: "of_nat (Suc 0) = (1\<Colon>int)"
   519 by simp
   520 
   521 text{*This version is proved for all ordered rings, not just integers!
   522       It is proved here because attribute @{text arith_split} is not available
   523       in theory @{text Rings}.
   524       But is it really better than just rewriting with @{text abs_if}?*}
   525 lemma abs_split [arith_split,no_atp]:
   526      "P(abs(a::'a::linordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))"
   527 by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
   528 
   529 lemma negD: "(x \<Colon> int) < 0 \<Longrightarrow> \<exists>n. x = - (of_nat (Suc n))"
   530 apply (cases x)
   531 apply (auto simp add: le minus Zero_int_def int_def order_less_le)
   532 apply (rule_tac x="y - Suc x" in exI, arith)
   533 done
   534 
   535 
   536 subsection {* Cases and induction *}
   537 
   538 text{*Now we replace the case analysis rule by a more conventional one:
   539 whether an integer is negative or not.*}
   540 
   541 theorem int_cases [cases type: int, case_names nonneg neg]:
   542   "[|!! n. (z \<Colon> int) = of_nat n ==> P;  !! n. z =  - (of_nat (Suc n)) ==> P |] ==> P"
   543 apply (cases "z < 0", blast dest!: negD)
   544 apply (simp add: linorder_not_less del: of_nat_Suc)
   545 apply auto
   546 apply (blast dest: nat_0_le [THEN sym])
   547 done
   548 
   549 theorem int_induct [induct type: int, case_names nonneg neg]:
   550      "[|!! n. P (of_nat n \<Colon> int);  !!n. P (- (of_nat (Suc n))) |] ==> P z"
   551   by (cases z rule: int_cases) auto
   552 
   553 text{*Contributed by Brian Huffman*}
   554 theorem int_diff_cases:
   555   obtains (diff) m n where "(z\<Colon>int) = of_nat m - of_nat n"
   556 apply (cases z rule: eq_Abs_Integ)
   557 apply (rule_tac m=x and n=y in diff)
   558 apply (simp add: int_def diff_def minus add)
   559 done
   560 
   561 
   562 subsection {* Binary representation *}
   563 
   564 text {*
   565   This formalization defines binary arithmetic in terms of the integers
   566   rather than using a datatype. This avoids multiple representations (leading
   567   zeroes, etc.)  See @{text "ZF/Tools/twos-compl.ML"}, function @{text
   568   int_of_binary}, for the numerical interpretation.
   569 
   570   The representation expects that @{text "(m mod 2)"} is 0 or 1,
   571   even if m is negative;
   572   For instance, @{text "-5 div 2 = -3"} and @{text "-5 mod 2 = 1"}; thus
   573   @{text "-5 = (-3)*2 + 1"}.
   574   
   575   This two's complement binary representation derives from the paper 
   576   "An Efficient Representation of Arithmetic for Term Rewriting" by
   577   Dave Cohen and Phil Watson, Rewriting Techniques and Applications,
   578   Springer LNCS 488 (240-251), 1991.
   579 *}
   580 
   581 subsubsection {* The constructors @{term Bit0}, @{term Bit1}, @{term Pls} and @{term Min} *}
   582 
   583 definition
   584   Pls :: int where
   585   [code del]: "Pls = 0"
   586 
   587 definition
   588   Min :: int where
   589   [code del]: "Min = - 1"
   590 
   591 definition
   592   Bit0 :: "int \<Rightarrow> int" where
   593   [code del]: "Bit0 k = k + k"
   594 
   595 definition
   596   Bit1 :: "int \<Rightarrow> int" where
   597   [code del]: "Bit1 k = 1 + k + k"
   598 
   599 class number = -- {* for numeric types: nat, int, real, \dots *}
   600   fixes number_of :: "int \<Rightarrow> 'a"
   601 
   602 use "Tools/numeral.ML"
   603 
   604 syntax
   605   "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
   606 
   607 use "Tools/numeral_syntax.ML"
   608 setup Numeral_Syntax.setup
   609 
   610 abbreviation
   611   "Numeral0 \<equiv> number_of Pls"
   612 
   613 abbreviation
   614   "Numeral1 \<equiv> number_of (Bit1 Pls)"
   615 
   616 lemma Let_number_of [simp]: "Let (number_of v) f = f (number_of v)"
   617   -- {* Unfold all @{text let}s involving constants *}
   618   unfolding Let_def ..
   619 
   620 definition
   621   succ :: "int \<Rightarrow> int" where
   622   [code del]: "succ k = k + 1"
   623 
   624 definition
   625   pred :: "int \<Rightarrow> int" where
   626   [code del]: "pred k = k - 1"
   627 
   628 lemmas
   629   max_number_of [simp] = max_def
   630     [of "number_of u" "number_of v", standard]
   631 and
   632   min_number_of [simp] = min_def 
   633     [of "number_of u" "number_of v", standard]
   634   -- {* unfolding @{text minx} and @{text max} on numerals *}
   635 
   636 lemmas numeral_simps = 
   637   succ_def pred_def Pls_def Min_def Bit0_def Bit1_def
   638 
   639 text {* Removal of leading zeroes *}
   640 
   641 lemma Bit0_Pls [simp, code_post]:
   642   "Bit0 Pls = Pls"
   643   unfolding numeral_simps by simp
   644 
   645 lemma Bit1_Min [simp, code_post]:
   646   "Bit1 Min = Min"
   647   unfolding numeral_simps by simp
   648 
   649 lemmas normalize_bin_simps =
   650   Bit0_Pls Bit1_Min
   651 
   652 
   653 subsubsection {* Successor and predecessor functions *}
   654 
   655 text {* Successor *}
   656 
   657 lemma succ_Pls:
   658   "succ Pls = Bit1 Pls"
   659   unfolding numeral_simps by simp
   660 
   661 lemma succ_Min:
   662   "succ Min = Pls"
   663   unfolding numeral_simps by simp
   664 
   665 lemma succ_Bit0:
   666   "succ (Bit0 k) = Bit1 k"
   667   unfolding numeral_simps by simp
   668 
   669 lemma succ_Bit1:
   670   "succ (Bit1 k) = Bit0 (succ k)"
   671   unfolding numeral_simps by simp
   672 
   673 lemmas succ_bin_simps [simp] =
   674   succ_Pls succ_Min succ_Bit0 succ_Bit1
   675 
   676 text {* Predecessor *}
   677 
   678 lemma pred_Pls:
   679   "pred Pls = Min"
   680   unfolding numeral_simps by simp
   681 
   682 lemma pred_Min:
   683   "pred Min = Bit0 Min"
   684   unfolding numeral_simps by simp
   685 
   686 lemma pred_Bit0:
   687   "pred (Bit0 k) = Bit1 (pred k)"
   688   unfolding numeral_simps by simp 
   689 
   690 lemma pred_Bit1:
   691   "pred (Bit1 k) = Bit0 k"
   692   unfolding numeral_simps by simp
   693 
   694 lemmas pred_bin_simps [simp] =
   695   pred_Pls pred_Min pred_Bit0 pred_Bit1
   696 
   697 
   698 subsubsection {* Binary arithmetic *}
   699 
   700 text {* Addition *}
   701 
   702 lemma add_Pls:
   703   "Pls + k = k"
   704   unfolding numeral_simps by simp
   705 
   706 lemma add_Min:
   707   "Min + k = pred k"
   708   unfolding numeral_simps by simp
   709 
   710 lemma add_Bit0_Bit0:
   711   "(Bit0 k) + (Bit0 l) = Bit0 (k + l)"
   712   unfolding numeral_simps by simp
   713 
   714 lemma add_Bit0_Bit1:
   715   "(Bit0 k) + (Bit1 l) = Bit1 (k + l)"
   716   unfolding numeral_simps by simp
   717 
   718 lemma add_Bit1_Bit0:
   719   "(Bit1 k) + (Bit0 l) = Bit1 (k + l)"
   720   unfolding numeral_simps by simp
   721 
   722 lemma add_Bit1_Bit1:
   723   "(Bit1 k) + (Bit1 l) = Bit0 (k + succ l)"
   724   unfolding numeral_simps by simp
   725 
   726 lemma add_Pls_right:
   727   "k + Pls = k"
   728   unfolding numeral_simps by simp
   729 
   730 lemma add_Min_right:
   731   "k + Min = pred k"
   732   unfolding numeral_simps by simp
   733 
   734 lemmas add_bin_simps [simp] =
   735   add_Pls add_Min add_Pls_right add_Min_right
   736   add_Bit0_Bit0 add_Bit0_Bit1 add_Bit1_Bit0 add_Bit1_Bit1
   737 
   738 text {* Negation *}
   739 
   740 lemma minus_Pls:
   741   "- Pls = Pls"
   742   unfolding numeral_simps by simp
   743 
   744 lemma minus_Min:
   745   "- Min = Bit1 Pls"
   746   unfolding numeral_simps by simp
   747 
   748 lemma minus_Bit0:
   749   "- (Bit0 k) = Bit0 (- k)"
   750   unfolding numeral_simps by simp
   751 
   752 lemma minus_Bit1:
   753   "- (Bit1 k) = Bit1 (pred (- k))"
   754   unfolding numeral_simps by simp
   755 
   756 lemmas minus_bin_simps [simp] =
   757   minus_Pls minus_Min minus_Bit0 minus_Bit1
   758 
   759 text {* Subtraction *}
   760 
   761 lemma diff_bin_simps [simp]:
   762   "k - Pls = k"
   763   "k - Min = succ k"
   764   "Pls - (Bit0 l) = Bit0 (Pls - l)"
   765   "Pls - (Bit1 l) = Bit1 (Min - l)"
   766   "Min - (Bit0 l) = Bit1 (Min - l)"
   767   "Min - (Bit1 l) = Bit0 (Min - l)"
   768   "(Bit0 k) - (Bit0 l) = Bit0 (k - l)"
   769   "(Bit0 k) - (Bit1 l) = Bit1 (pred k - l)"
   770   "(Bit1 k) - (Bit0 l) = Bit1 (k - l)"
   771   "(Bit1 k) - (Bit1 l) = Bit0 (k - l)"
   772   unfolding numeral_simps by simp_all
   773 
   774 text {* Multiplication *}
   775 
   776 lemma mult_Pls:
   777   "Pls * w = Pls"
   778   unfolding numeral_simps by simp
   779 
   780 lemma mult_Min:
   781   "Min * k = - k"
   782   unfolding numeral_simps by simp
   783 
   784 lemma mult_Bit0:
   785   "(Bit0 k) * l = Bit0 (k * l)"
   786   unfolding numeral_simps int_distrib by simp
   787 
   788 lemma mult_Bit1:
   789   "(Bit1 k) * l = (Bit0 (k * l)) + l"
   790   unfolding numeral_simps int_distrib by simp
   791 
   792 lemmas mult_bin_simps [simp] =
   793   mult_Pls mult_Min mult_Bit0 mult_Bit1
   794 
   795 
   796 subsubsection {* Binary comparisons *}
   797 
   798 text {* Preliminaries *}
   799 
   800 lemma even_less_0_iff:
   801   "a + a < 0 \<longleftrightarrow> a < (0::'a::linordered_idom)"
   802 proof -
   803   have "a + a < 0 \<longleftrightarrow> (1+1)*a < 0" by (simp add: left_distrib)
   804   also have "(1+1)*a < 0 \<longleftrightarrow> a < 0"
   805     by (simp add: mult_less_0_iff zero_less_two 
   806                   order_less_not_sym [OF zero_less_two])
   807   finally show ?thesis .
   808 qed
   809 
   810 lemma le_imp_0_less: 
   811   assumes le: "0 \<le> z"
   812   shows "(0::int) < 1 + z"
   813 proof -
   814   have "0 \<le> z" by fact
   815   also have "... < z + 1" by (rule less_add_one) 
   816   also have "... = 1 + z" by (simp add: add_ac)
   817   finally show "0 < 1 + z" .
   818 qed
   819 
   820 lemma odd_less_0_iff:
   821   "(1 + z + z < 0) = (z < (0::int))"
   822 proof (cases z rule: int_cases)
   823   case (nonneg n)
   824   thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing
   825                              le_imp_0_less [THEN order_less_imp_le])  
   826 next
   827   case (neg n)
   828   thus ?thesis by (simp del: of_nat_Suc of_nat_add of_nat_1
   829     add: algebra_simps of_nat_1 [where 'a=int, symmetric] of_nat_add [symmetric])
   830 qed
   831 
   832 lemma bin_less_0_simps:
   833   "Pls < 0 \<longleftrightarrow> False"
   834   "Min < 0 \<longleftrightarrow> True"
   835   "Bit0 w < 0 \<longleftrightarrow> w < 0"
   836   "Bit1 w < 0 \<longleftrightarrow> w < 0"
   837   unfolding numeral_simps
   838   by (simp_all add: even_less_0_iff odd_less_0_iff)
   839 
   840 lemma less_bin_lemma: "k < l \<longleftrightarrow> k - l < (0::int)"
   841   by simp
   842 
   843 lemma le_iff_pred_less: "k \<le> l \<longleftrightarrow> pred k < l"
   844   unfolding numeral_simps
   845   proof
   846     have "k - 1 < k" by simp
   847     also assume "k \<le> l"
   848     finally show "k - 1 < l" .
   849   next
   850     assume "k - 1 < l"
   851     hence "(k - 1) + 1 \<le> l" by (rule zless_imp_add1_zle)
   852     thus "k \<le> l" by simp
   853   qed
   854 
   855 lemma succ_pred: "succ (pred x) = x"
   856   unfolding numeral_simps by simp
   857 
   858 text {* Less-than *}
   859 
   860 lemma less_bin_simps [simp]:
   861   "Pls < Pls \<longleftrightarrow> False"
   862   "Pls < Min \<longleftrightarrow> False"
   863   "Pls < Bit0 k \<longleftrightarrow> Pls < k"
   864   "Pls < Bit1 k \<longleftrightarrow> Pls \<le> k"
   865   "Min < Pls \<longleftrightarrow> True"
   866   "Min < Min \<longleftrightarrow> False"
   867   "Min < Bit0 k \<longleftrightarrow> Min < k"
   868   "Min < Bit1 k \<longleftrightarrow> Min < k"
   869   "Bit0 k < Pls \<longleftrightarrow> k < Pls"
   870   "Bit0 k < Min \<longleftrightarrow> k \<le> Min"
   871   "Bit1 k < Pls \<longleftrightarrow> k < Pls"
   872   "Bit1 k < Min \<longleftrightarrow> k < Min"
   873   "Bit0 k < Bit0 l \<longleftrightarrow> k < l"
   874   "Bit0 k < Bit1 l \<longleftrightarrow> k \<le> l"
   875   "Bit1 k < Bit0 l \<longleftrightarrow> k < l"
   876   "Bit1 k < Bit1 l \<longleftrightarrow> k < l"
   877   unfolding le_iff_pred_less
   878     less_bin_lemma [of Pls]
   879     less_bin_lemma [of Min]
   880     less_bin_lemma [of "k"]
   881     less_bin_lemma [of "Bit0 k"]
   882     less_bin_lemma [of "Bit1 k"]
   883     less_bin_lemma [of "pred Pls"]
   884     less_bin_lemma [of "pred k"]
   885   by (simp_all add: bin_less_0_simps succ_pred)
   886 
   887 text {* Less-than-or-equal *}
   888 
   889 lemma le_bin_simps [simp]:
   890   "Pls \<le> Pls \<longleftrightarrow> True"
   891   "Pls \<le> Min \<longleftrightarrow> False"
   892   "Pls \<le> Bit0 k \<longleftrightarrow> Pls \<le> k"
   893   "Pls \<le> Bit1 k \<longleftrightarrow> Pls \<le> k"
   894   "Min \<le> Pls \<longleftrightarrow> True"
   895   "Min \<le> Min \<longleftrightarrow> True"
   896   "Min \<le> Bit0 k \<longleftrightarrow> Min < k"
   897   "Min \<le> Bit1 k \<longleftrightarrow> Min \<le> k"
   898   "Bit0 k \<le> Pls \<longleftrightarrow> k \<le> Pls"
   899   "Bit0 k \<le> Min \<longleftrightarrow> k \<le> Min"
   900   "Bit1 k \<le> Pls \<longleftrightarrow> k < Pls"
   901   "Bit1 k \<le> Min \<longleftrightarrow> k \<le> Min"
   902   "Bit0 k \<le> Bit0 l \<longleftrightarrow> k \<le> l"
   903   "Bit0 k \<le> Bit1 l \<longleftrightarrow> k \<le> l"
   904   "Bit1 k \<le> Bit0 l \<longleftrightarrow> k < l"
   905   "Bit1 k \<le> Bit1 l \<longleftrightarrow> k \<le> l"
   906   unfolding not_less [symmetric]
   907   by (simp_all add: not_le)
   908 
   909 text {* Equality *}
   910 
   911 lemma eq_bin_simps [simp]:
   912   "Pls = Pls \<longleftrightarrow> True"
   913   "Pls = Min \<longleftrightarrow> False"
   914   "Pls = Bit0 l \<longleftrightarrow> Pls = l"
   915   "Pls = Bit1 l \<longleftrightarrow> False"
   916   "Min = Pls \<longleftrightarrow> False"
   917   "Min = Min \<longleftrightarrow> True"
   918   "Min = Bit0 l \<longleftrightarrow> False"
   919   "Min = Bit1 l \<longleftrightarrow> Min = l"
   920   "Bit0 k = Pls \<longleftrightarrow> k = Pls"
   921   "Bit0 k = Min \<longleftrightarrow> False"
   922   "Bit1 k = Pls \<longleftrightarrow> False"
   923   "Bit1 k = Min \<longleftrightarrow> k = Min"
   924   "Bit0 k = Bit0 l \<longleftrightarrow> k = l"
   925   "Bit0 k = Bit1 l \<longleftrightarrow> False"
   926   "Bit1 k = Bit0 l \<longleftrightarrow> False"
   927   "Bit1 k = Bit1 l \<longleftrightarrow> k = l"
   928   unfolding order_eq_iff [where 'a=int]
   929   by (simp_all add: not_less)
   930 
   931 
   932 subsection {* Converting Numerals to Rings: @{term number_of} *}
   933 
   934 class number_ring = number + comm_ring_1 +
   935   assumes number_of_eq: "number_of k = of_int k"
   936 
   937 text {* self-embedding of the integers *}
   938 
   939 instantiation int :: number_ring
   940 begin
   941 
   942 definition int_number_of_def [code del]:
   943   "number_of w = (of_int w \<Colon> int)"
   944 
   945 instance proof
   946 qed (simp only: int_number_of_def)
   947 
   948 end
   949 
   950 lemma number_of_is_id:
   951   "number_of (k::int) = k"
   952   unfolding int_number_of_def by simp
   953 
   954 lemma number_of_succ:
   955   "number_of (succ k) = (1 + number_of k ::'a::number_ring)"
   956   unfolding number_of_eq numeral_simps by simp
   957 
   958 lemma number_of_pred:
   959   "number_of (pred w) = (- 1 + number_of w ::'a::number_ring)"
   960   unfolding number_of_eq numeral_simps by simp
   961 
   962 lemma number_of_minus:
   963   "number_of (uminus w) = (- (number_of w)::'a::number_ring)"
   964   unfolding number_of_eq by (rule of_int_minus)
   965 
   966 lemma number_of_add:
   967   "number_of (v + w) = (number_of v + number_of w::'a::number_ring)"
   968   unfolding number_of_eq by (rule of_int_add)
   969 
   970 lemma number_of_diff:
   971   "number_of (v - w) = (number_of v - number_of w::'a::number_ring)"
   972   unfolding number_of_eq by (rule of_int_diff)
   973 
   974 lemma number_of_mult:
   975   "number_of (v * w) = (number_of v * number_of w::'a::number_ring)"
   976   unfolding number_of_eq by (rule of_int_mult)
   977 
   978 text {*
   979   The correctness of shifting.
   980   But it doesn't seem to give a measurable speed-up.
   981 *}
   982 
   983 lemma double_number_of_Bit0:
   984   "(1 + 1) * number_of w = (number_of (Bit0 w) ::'a::number_ring)"
   985   unfolding number_of_eq numeral_simps left_distrib by simp
   986 
   987 text {*
   988   Converting numerals 0 and 1 to their abstract versions.
   989 *}
   990 
   991 lemma numeral_0_eq_0 [simp, code_post]:
   992   "Numeral0 = (0::'a::number_ring)"
   993   unfolding number_of_eq numeral_simps by simp
   994 
   995 lemma numeral_1_eq_1 [simp, code_post]:
   996   "Numeral1 = (1::'a::number_ring)"
   997   unfolding number_of_eq numeral_simps by simp
   998 
   999 text {*
  1000   Special-case simplification for small constants.
  1001 *}
  1002 
  1003 text{*
  1004   Unary minus for the abstract constant 1. Cannot be inserted
  1005   as a simprule until later: it is @{text number_of_Min} re-oriented!
  1006 *}
  1007 
  1008 lemma numeral_m1_eq_minus_1:
  1009   "(-1::'a::number_ring) = - 1"
  1010   unfolding number_of_eq numeral_simps by simp
  1011 
  1012 lemma mult_minus1 [simp]:
  1013   "-1 * z = -(z::'a::number_ring)"
  1014   unfolding number_of_eq numeral_simps by simp
  1015 
  1016 lemma mult_minus1_right [simp]:
  1017   "z * -1 = -(z::'a::number_ring)"
  1018   unfolding number_of_eq numeral_simps by simp
  1019 
  1020 (*Negation of a coefficient*)
  1021 lemma minus_number_of_mult [simp]:
  1022    "- (number_of w) * z = number_of (uminus w) * (z::'a::number_ring)"
  1023    unfolding number_of_eq by simp
  1024 
  1025 text {* Subtraction *}
  1026 
  1027 lemma diff_number_of_eq:
  1028   "number_of v - number_of w =
  1029     (number_of (v + uminus w)::'a::number_ring)"
  1030   unfolding number_of_eq by simp
  1031 
  1032 lemma number_of_Pls:
  1033   "number_of Pls = (0::'a::number_ring)"
  1034   unfolding number_of_eq numeral_simps by simp
  1035 
  1036 lemma number_of_Min:
  1037   "number_of Min = (- 1::'a::number_ring)"
  1038   unfolding number_of_eq numeral_simps by simp
  1039 
  1040 lemma number_of_Bit0:
  1041   "number_of (Bit0 w) = (0::'a::number_ring) + (number_of w) + (number_of w)"
  1042   unfolding number_of_eq numeral_simps by simp
  1043 
  1044 lemma number_of_Bit1:
  1045   "number_of (Bit1 w) = (1::'a::number_ring) + (number_of w) + (number_of w)"
  1046   unfolding number_of_eq numeral_simps by simp
  1047 
  1048 
  1049 subsubsection {* Equality of Binary Numbers *}
  1050 
  1051 text {* First version by Norbert Voelker *}
  1052 
  1053 definition (*for simplifying equalities*)
  1054   iszero :: "'a\<Colon>semiring_1 \<Rightarrow> bool"
  1055 where
  1056   "iszero z \<longleftrightarrow> z = 0"
  1057 
  1058 lemma iszero_0: "iszero 0"
  1059 by (simp add: iszero_def)
  1060 
  1061 lemma not_iszero_1: "~ iszero 1"
  1062 by (simp add: iszero_def eq_commute)
  1063 
  1064 lemma eq_number_of_eq [simp]:
  1065   "((number_of x::'a::number_ring) = number_of y) =
  1066    iszero (number_of (x + uminus y) :: 'a)"
  1067 unfolding iszero_def number_of_add number_of_minus
  1068 by (simp add: algebra_simps)
  1069 
  1070 lemma iszero_number_of_Pls:
  1071   "iszero ((number_of Pls)::'a::number_ring)"
  1072 unfolding iszero_def numeral_0_eq_0 ..
  1073 
  1074 lemma nonzero_number_of_Min:
  1075   "~ iszero ((number_of Min)::'a::number_ring)"
  1076 unfolding iszero_def numeral_m1_eq_minus_1 by simp
  1077 
  1078 
  1079 subsubsection {* Comparisons, for Ordered Rings *}
  1080 
  1081 lemmas double_eq_0_iff = double_zero
  1082 
  1083 lemma odd_nonzero:
  1084   "1 + z + z \<noteq> (0::int)"
  1085 proof (cases z rule: int_cases)
  1086   case (nonneg n)
  1087   have le: "0 \<le> z+z" by (simp add: nonneg add_increasing) 
  1088   thus ?thesis using  le_imp_0_less [OF le]
  1089     by (auto simp add: add_assoc) 
  1090 next
  1091   case (neg n)
  1092   show ?thesis
  1093   proof
  1094     assume eq: "1 + z + z = 0"
  1095     have "(0::int) < 1 + (of_nat n + of_nat n)"
  1096       by (simp add: le_imp_0_less add_increasing) 
  1097     also have "... = - (1 + z + z)" 
  1098       by (simp add: neg add_assoc [symmetric]) 
  1099     also have "... = 0" by (simp add: eq) 
  1100     finally have "0<0" ..
  1101     thus False by blast
  1102   qed
  1103 qed
  1104 
  1105 lemma iszero_number_of_Bit0:
  1106   "iszero (number_of (Bit0 w)::'a) = 
  1107    iszero (number_of w::'a::{ring_char_0,number_ring})"
  1108 proof -
  1109   have "(of_int w + of_int w = (0::'a)) \<Longrightarrow> (w = 0)"
  1110   proof -
  1111     assume eq: "of_int w + of_int w = (0::'a)"
  1112     then have "of_int (w + w) = (of_int 0 :: 'a)" by simp
  1113     then have "w + w = 0" by (simp only: of_int_eq_iff)
  1114     then show "w = 0" by (simp only: double_eq_0_iff)
  1115   qed
  1116   thus ?thesis
  1117     by (auto simp add: iszero_def number_of_eq numeral_simps)
  1118 qed
  1119 
  1120 lemma iszero_number_of_Bit1:
  1121   "~ iszero (number_of (Bit1 w)::'a::{ring_char_0,number_ring})"
  1122 proof -
  1123   have "1 + of_int w + of_int w \<noteq> (0::'a)"
  1124   proof
  1125     assume eq: "1 + of_int w + of_int w = (0::'a)"
  1126     hence "of_int (1 + w + w) = (of_int 0 :: 'a)" by simp 
  1127     hence "1 + w + w = 0" by (simp only: of_int_eq_iff)
  1128     with odd_nonzero show False by blast
  1129   qed
  1130   thus ?thesis
  1131     by (auto simp add: iszero_def number_of_eq numeral_simps)
  1132 qed
  1133 
  1134 lemmas iszero_simps [simp] =
  1135   iszero_0 not_iszero_1
  1136   iszero_number_of_Pls nonzero_number_of_Min
  1137   iszero_number_of_Bit0 iszero_number_of_Bit1
  1138 (* iszero_number_of_Pls would never normally be used
  1139    because its lhs simplifies to "iszero 0" *)
  1140 
  1141 subsubsection {* The Less-Than Relation *}
  1142 
  1143 lemma double_less_0_iff:
  1144   "(a + a < 0) = (a < (0::'a::linordered_idom))"
  1145 proof -
  1146   have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)
  1147   also have "... = (a < 0)"
  1148     by (simp add: mult_less_0_iff zero_less_two 
  1149                   order_less_not_sym [OF zero_less_two]) 
  1150   finally show ?thesis .
  1151 qed
  1152 
  1153 lemma odd_less_0:
  1154   "(1 + z + z < 0) = (z < (0::int))"
  1155 proof (cases z rule: int_cases)
  1156   case (nonneg n)
  1157   thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing
  1158                              le_imp_0_less [THEN order_less_imp_le])  
  1159 next
  1160   case (neg n)
  1161   thus ?thesis by (simp del: of_nat_Suc of_nat_add of_nat_1
  1162     add: algebra_simps of_nat_1 [where 'a=int, symmetric] of_nat_add [symmetric])
  1163 qed
  1164 
  1165 text {* Less-Than or Equals *}
  1166 
  1167 text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
  1168 
  1169 lemmas le_number_of_eq_not_less =
  1170   linorder_not_less [of "number_of w" "number_of v", symmetric, 
  1171   standard]
  1172 
  1173 
  1174 text {* Absolute value (@{term abs}) *}
  1175 
  1176 lemma abs_number_of:
  1177   "abs(number_of x::'a::{linordered_idom,number_ring}) =
  1178    (if number_of x < (0::'a) then -number_of x else number_of x)"
  1179   by (simp add: abs_if)
  1180 
  1181 
  1182 text {* Re-orientation of the equation nnn=x *}
  1183 
  1184 lemma number_of_reorient:
  1185   "(number_of w = x) = (x = number_of w)"
  1186   by auto
  1187 
  1188 
  1189 subsubsection {* Simplification of arithmetic operations on integer constants. *}
  1190 
  1191 lemmas arith_extra_simps [standard, simp] =
  1192   number_of_add [symmetric]
  1193   number_of_minus [symmetric]
  1194   numeral_m1_eq_minus_1 [symmetric]
  1195   number_of_mult [symmetric]
  1196   diff_number_of_eq abs_number_of 
  1197 
  1198 text {*
  1199   For making a minimal simpset, one must include these default simprules.
  1200   Also include @{text simp_thms}.
  1201 *}
  1202 
  1203 lemmas arith_simps = 
  1204   normalize_bin_simps pred_bin_simps succ_bin_simps
  1205   add_bin_simps minus_bin_simps mult_bin_simps
  1206   abs_zero abs_one arith_extra_simps
  1207 
  1208 text {* Simplification of relational operations *}
  1209 
  1210 lemma less_number_of [simp]:
  1211   "(number_of x::'a::{linordered_idom,number_ring}) < number_of y \<longleftrightarrow> x < y"
  1212   unfolding number_of_eq by (rule of_int_less_iff)
  1213 
  1214 lemma le_number_of [simp]:
  1215   "(number_of x::'a::{linordered_idom,number_ring}) \<le> number_of y \<longleftrightarrow> x \<le> y"
  1216   unfolding number_of_eq by (rule of_int_le_iff)
  1217 
  1218 lemma eq_number_of [simp]:
  1219   "(number_of x::'a::{ring_char_0,number_ring}) = number_of y \<longleftrightarrow> x = y"
  1220   unfolding number_of_eq by (rule of_int_eq_iff)
  1221 
  1222 lemmas rel_simps =
  1223   less_number_of less_bin_simps
  1224   le_number_of le_bin_simps
  1225   eq_number_of_eq eq_bin_simps
  1226   iszero_simps
  1227 
  1228 
  1229 subsubsection {* Simplification of arithmetic when nested to the right. *}
  1230 
  1231 lemma add_number_of_left [simp]:
  1232   "number_of v + (number_of w + z) =
  1233    (number_of(v + w) + z::'a::number_ring)"
  1234   by (simp add: add_assoc [symmetric])
  1235 
  1236 lemma mult_number_of_left [simp]:
  1237   "number_of v * (number_of w * z) =
  1238    (number_of(v * w) * z::'a::number_ring)"
  1239   by (simp add: mult_assoc [symmetric])
  1240 
  1241 lemma add_number_of_diff1:
  1242   "number_of v + (number_of w - c) = 
  1243   number_of(v + w) - (c::'a::number_ring)"
  1244   by (simp add: diff_minus)
  1245 
  1246 lemma add_number_of_diff2 [simp]:
  1247   "number_of v + (c - number_of w) =
  1248    number_of (v + uminus w) + (c::'a::number_ring)"
  1249 by (simp add: algebra_simps diff_number_of_eq [symmetric])
  1250 
  1251 
  1252 
  1253 
  1254 subsection {* The Set of Integers *}
  1255 
  1256 context ring_1
  1257 begin
  1258 
  1259 definition Ints  :: "'a set" where
  1260   [code del]: "Ints = range of_int"
  1261 
  1262 notation (xsymbols)
  1263   Ints  ("\<int>")
  1264 
  1265 lemma Ints_of_int [simp]: "of_int z \<in> \<int>"
  1266   by (simp add: Ints_def)
  1267 
  1268 lemma Ints_of_nat [simp]: "of_nat n \<in> \<int>"
  1269 apply (simp add: Ints_def)
  1270 apply (rule range_eqI)
  1271 apply (rule of_int_of_nat_eq [symmetric])
  1272 done
  1273 
  1274 lemma Ints_0 [simp]: "0 \<in> \<int>"
  1275 apply (simp add: Ints_def)
  1276 apply (rule range_eqI)
  1277 apply (rule of_int_0 [symmetric])
  1278 done
  1279 
  1280 lemma Ints_1 [simp]: "1 \<in> \<int>"
  1281 apply (simp add: Ints_def)
  1282 apply (rule range_eqI)
  1283 apply (rule of_int_1 [symmetric])
  1284 done
  1285 
  1286 lemma Ints_add [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a + b \<in> \<int>"
  1287 apply (auto simp add: Ints_def)
  1288 apply (rule range_eqI)
  1289 apply (rule of_int_add [symmetric])
  1290 done
  1291 
  1292 lemma Ints_minus [simp]: "a \<in> \<int> \<Longrightarrow> -a \<in> \<int>"
  1293 apply (auto simp add: Ints_def)
  1294 apply (rule range_eqI)
  1295 apply (rule of_int_minus [symmetric])
  1296 done
  1297 
  1298 lemma Ints_diff [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a - b \<in> \<int>"
  1299 apply (auto simp add: Ints_def)
  1300 apply (rule range_eqI)
  1301 apply (rule of_int_diff [symmetric])
  1302 done
  1303 
  1304 lemma Ints_mult [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a * b \<in> \<int>"
  1305 apply (auto simp add: Ints_def)
  1306 apply (rule range_eqI)
  1307 apply (rule of_int_mult [symmetric])
  1308 done
  1309 
  1310 lemma Ints_power [simp]: "a \<in> \<int> \<Longrightarrow> a ^ n \<in> \<int>"
  1311 by (induct n) simp_all
  1312 
  1313 lemma Ints_cases [cases set: Ints]:
  1314   assumes "q \<in> \<int>"
  1315   obtains (of_int) z where "q = of_int z"
  1316   unfolding Ints_def
  1317 proof -
  1318   from `q \<in> \<int>` have "q \<in> range of_int" unfolding Ints_def .
  1319   then obtain z where "q = of_int z" ..
  1320   then show thesis ..
  1321 qed
  1322 
  1323 lemma Ints_induct [case_names of_int, induct set: Ints]:
  1324   "q \<in> \<int> \<Longrightarrow> (\<And>z. P (of_int z)) \<Longrightarrow> P q"
  1325   by (rule Ints_cases) auto
  1326 
  1327 end
  1328 
  1329 text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
  1330 
  1331 lemma Ints_double_eq_0_iff:
  1332   assumes in_Ints: "a \<in> Ints"
  1333   shows "(a + a = 0) = (a = (0::'a::ring_char_0))"
  1334 proof -
  1335   from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
  1336   then obtain z where a: "a = of_int z" ..
  1337   show ?thesis
  1338   proof
  1339     assume "a = 0"
  1340     thus "a + a = 0" by simp
  1341   next
  1342     assume eq: "a + a = 0"
  1343     hence "of_int (z + z) = (of_int 0 :: 'a)" by (simp add: a)
  1344     hence "z + z = 0" by (simp only: of_int_eq_iff)
  1345     hence "z = 0" by (simp only: double_eq_0_iff)
  1346     thus "a = 0" by (simp add: a)
  1347   qed
  1348 qed
  1349 
  1350 lemma Ints_odd_nonzero:
  1351   assumes in_Ints: "a \<in> Ints"
  1352   shows "1 + a + a \<noteq> (0::'a::ring_char_0)"
  1353 proof -
  1354   from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
  1355   then obtain z where a: "a = of_int z" ..
  1356   show ?thesis
  1357   proof
  1358     assume eq: "1 + a + a = 0"
  1359     hence "of_int (1 + z + z) = (of_int 0 :: 'a)" by (simp add: a)
  1360     hence "1 + z + z = 0" by (simp only: of_int_eq_iff)
  1361     with odd_nonzero show False by blast
  1362   qed
  1363 qed 
  1364 
  1365 lemma Ints_number_of [simp]:
  1366   "(number_of w :: 'a::number_ring) \<in> Ints"
  1367   unfolding number_of_eq Ints_def by simp
  1368 
  1369 lemma Nats_number_of [simp]:
  1370   "Int.Pls \<le> w \<Longrightarrow> (number_of w :: 'a::number_ring) \<in> Nats"
  1371 unfolding Int.Pls_def number_of_eq
  1372 by (simp only: of_nat_nat [symmetric] of_nat_in_Nats)
  1373 
  1374 lemma Ints_odd_less_0: 
  1375   assumes in_Ints: "a \<in> Ints"
  1376   shows "(1 + a + a < 0) = (a < (0::'a::linordered_idom))"
  1377 proof -
  1378   from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
  1379   then obtain z where a: "a = of_int z" ..
  1380   hence "((1::'a) + a + a < 0) = (of_int (1 + z + z) < (of_int 0 :: 'a))"
  1381     by (simp add: a)
  1382   also have "... = (z < 0)" by (simp only: of_int_less_iff odd_less_0)
  1383   also have "... = (a < 0)" by (simp add: a)
  1384   finally show ?thesis .
  1385 qed
  1386 
  1387 
  1388 subsection {* @{term setsum} and @{term setprod} *}
  1389 
  1390 lemma of_nat_setsum: "of_nat (setsum f A) = (\<Sum>x\<in>A. of_nat(f x))"
  1391   apply (cases "finite A")
  1392   apply (erule finite_induct, auto)
  1393   done
  1394 
  1395 lemma of_int_setsum: "of_int (setsum f A) = (\<Sum>x\<in>A. of_int(f x))"
  1396   apply (cases "finite A")
  1397   apply (erule finite_induct, auto)
  1398   done
  1399 
  1400 lemma of_nat_setprod: "of_nat (setprod f A) = (\<Prod>x\<in>A. of_nat(f x))"
  1401   apply (cases "finite A")
  1402   apply (erule finite_induct, auto simp add: of_nat_mult)
  1403   done
  1404 
  1405 lemma of_int_setprod: "of_int (setprod f A) = (\<Prod>x\<in>A. of_int(f x))"
  1406   apply (cases "finite A")
  1407   apply (erule finite_induct, auto)
  1408   done
  1409 
  1410 lemmas int_setsum = of_nat_setsum [where 'a=int]
  1411 lemmas int_setprod = of_nat_setprod [where 'a=int]
  1412 
  1413 
  1414 subsection{*Inequality Reasoning for the Arithmetic Simproc*}
  1415 
  1416 lemma add_numeral_0: "Numeral0 + a = (a::'a::number_ring)"
  1417 by simp 
  1418 
  1419 lemma add_numeral_0_right: "a + Numeral0 = (a::'a::number_ring)"
  1420 by simp
  1421 
  1422 lemma mult_numeral_1: "Numeral1 * a = (a::'a::number_ring)"
  1423 by simp 
  1424 
  1425 lemma mult_numeral_1_right: "a * Numeral1 = (a::'a::number_ring)"
  1426 by simp
  1427 
  1428 lemma divide_numeral_1: "a / Numeral1 = (a::'a::{number_ring,field})"
  1429 by simp
  1430 
  1431 lemma inverse_numeral_1:
  1432   "inverse Numeral1 = (Numeral1::'a::{number_ring,field})"
  1433 by simp
  1434 
  1435 text{*Theorem lists for the cancellation simprocs. The use of binary numerals
  1436 for 0 and 1 reduces the number of special cases.*}
  1437 
  1438 lemmas add_0s = add_numeral_0 add_numeral_0_right
  1439 lemmas mult_1s = mult_numeral_1 mult_numeral_1_right 
  1440                  mult_minus1 mult_minus1_right
  1441 
  1442 
  1443 subsection{*Special Arithmetic Rules for Abstract 0 and 1*}
  1444 
  1445 text{*Arithmetic computations are defined for binary literals, which leaves 0
  1446 and 1 as special cases. Addition already has rules for 0, but not 1.
  1447 Multiplication and unary minus already have rules for both 0 and 1.*}
  1448 
  1449 
  1450 lemma binop_eq: "[|f x y = g x y; x = x'; y = y'|] ==> f x' y' = g x' y'"
  1451 by simp
  1452 
  1453 
  1454 lemmas add_number_of_eq = number_of_add [symmetric]
  1455 
  1456 text{*Allow 1 on either or both sides*}
  1457 lemma one_add_one_is_two: "1 + 1 = (2::'a::number_ring)"
  1458 by (simp del: numeral_1_eq_1 add: numeral_1_eq_1 [symmetric])
  1459 
  1460 lemmas add_special =
  1461     one_add_one_is_two
  1462     binop_eq [of "op +", OF add_number_of_eq numeral_1_eq_1 refl, standard]
  1463     binop_eq [of "op +", OF add_number_of_eq refl numeral_1_eq_1, standard]
  1464 
  1465 text{*Allow 1 on either or both sides (1-1 already simplifies to 0)*}
  1466 lemmas diff_special =
  1467     binop_eq [of "op -", OF diff_number_of_eq numeral_1_eq_1 refl, standard]
  1468     binop_eq [of "op -", OF diff_number_of_eq refl numeral_1_eq_1, standard]
  1469 
  1470 text{*Allow 0 or 1 on either side with a binary numeral on the other*}
  1471 lemmas eq_special =
  1472     binop_eq [of "op =", OF eq_number_of_eq numeral_0_eq_0 refl, standard]
  1473     binop_eq [of "op =", OF eq_number_of_eq numeral_1_eq_1 refl, standard]
  1474     binop_eq [of "op =", OF eq_number_of_eq refl numeral_0_eq_0, standard]
  1475     binop_eq [of "op =", OF eq_number_of_eq refl numeral_1_eq_1, standard]
  1476 
  1477 text{*Allow 0 or 1 on either side with a binary numeral on the other*}
  1478 lemmas less_special =
  1479   binop_eq [of "op <", OF less_number_of numeral_0_eq_0 refl, standard]
  1480   binop_eq [of "op <", OF less_number_of numeral_1_eq_1 refl, standard]
  1481   binop_eq [of "op <", OF less_number_of refl numeral_0_eq_0, standard]
  1482   binop_eq [of "op <", OF less_number_of refl numeral_1_eq_1, standard]
  1483 
  1484 text{*Allow 0 or 1 on either side with a binary numeral on the other*}
  1485 lemmas le_special =
  1486     binop_eq [of "op \<le>", OF le_number_of numeral_0_eq_0 refl, standard]
  1487     binop_eq [of "op \<le>", OF le_number_of numeral_1_eq_1 refl, standard]
  1488     binop_eq [of "op \<le>", OF le_number_of refl numeral_0_eq_0, standard]
  1489     binop_eq [of "op \<le>", OF le_number_of refl numeral_1_eq_1, standard]
  1490 
  1491 lemmas arith_special[simp] = 
  1492        add_special diff_special eq_special less_special le_special
  1493 
  1494 
  1495 text {* Legacy theorems *}
  1496 
  1497 lemmas zle_int = of_nat_le_iff [where 'a=int]
  1498 lemmas int_int_eq = of_nat_eq_iff [where 'a=int]
  1499 
  1500 subsection {* Setting up simplification procedures *}
  1501 
  1502 lemmas int_arith_rules =
  1503   neg_le_iff_le numeral_0_eq_0 numeral_1_eq_1
  1504   minus_zero diff_minus left_minus right_minus
  1505   mult_zero_left mult_zero_right mult_Bit1 mult_1_left mult_1_right
  1506   mult_minus_left mult_minus_right
  1507   minus_add_distrib minus_minus mult_assoc
  1508   of_nat_0 of_nat_1 of_nat_Suc of_nat_add of_nat_mult
  1509   of_int_0 of_int_1 of_int_add of_int_mult
  1510 
  1511 use "Tools/int_arith.ML"
  1512 setup {* Int_Arith.global_setup *}
  1513 declaration {* K Int_Arith.setup *}
  1514 
  1515 setup {*
  1516   Reorient_Proc.add
  1517     (fn Const (@{const_name number_of}, _) $ _ => true | _ => false)
  1518 *}
  1519 
  1520 simproc_setup reorient_numeral ("number_of w = x") = Reorient_Proc.proc
  1521 
  1522 
  1523 subsection{*Lemmas About Small Numerals*}
  1524 
  1525 lemma of_int_m1 [simp]: "of_int -1 = (-1 :: 'a :: number_ring)"
  1526 proof -
  1527   have "(of_int -1 :: 'a) = of_int (- 1)" by simp
  1528   also have "... = - of_int 1" by (simp only: of_int_minus)
  1529   also have "... = -1" by simp
  1530   finally show ?thesis .
  1531 qed
  1532 
  1533 lemma abs_minus_one [simp]: "abs (-1) = (1::'a::{linordered_idom,number_ring})"
  1534 by (simp add: abs_if)
  1535 
  1536 lemma abs_power_minus_one [simp]:
  1537   "abs(-1 ^ n) = (1::'a::{linordered_idom,number_ring})"
  1538 by (simp add: power_abs)
  1539 
  1540 lemma of_int_number_of_eq [simp]:
  1541      "of_int (number_of v) = (number_of v :: 'a :: number_ring)"
  1542 by (simp add: number_of_eq) 
  1543 
  1544 text{*Lemmas for specialist use, NOT as default simprules*}
  1545 lemma mult_2: "2 * z = (z+z::'a::number_ring)"
  1546 unfolding one_add_one_is_two [symmetric] left_distrib by simp
  1547 
  1548 lemma mult_2_right: "z * 2 = (z+z::'a::number_ring)"
  1549 by (subst mult_commute, rule mult_2)
  1550 
  1551 
  1552 subsection{*More Inequality Reasoning*}
  1553 
  1554 lemma zless_add1_eq: "(w < z + (1::int)) = (w<z | w=z)"
  1555 by arith
  1556 
  1557 lemma add1_zle_eq: "(w + (1::int) \<le> z) = (w<z)"
  1558 by arith
  1559 
  1560 lemma zle_diff1_eq [simp]: "(w \<le> z - (1::int)) = (w<z)"
  1561 by arith
  1562 
  1563 lemma zle_add1_eq_le [simp]: "(w < z + (1::int)) = (w\<le>z)"
  1564 by arith
  1565 
  1566 lemma int_one_le_iff_zero_less: "((1::int) \<le> z) = (0 < z)"
  1567 by arith
  1568 
  1569 
  1570 subsection{*The functions @{term nat} and @{term int}*}
  1571 
  1572 text{*Simplify the terms @{term "int 0"}, @{term "int(Suc 0)"} and
  1573   @{term "w + - z"}*}
  1574 declare Zero_int_def [symmetric, simp]
  1575 declare One_int_def [symmetric, simp]
  1576 
  1577 lemmas diff_int_def_symmetric = diff_int_def [symmetric, simp]
  1578 
  1579 (* FIXME: duplicates nat_zero *)
  1580 lemma nat_0: "nat 0 = 0"
  1581 by (simp add: nat_eq_iff)
  1582 
  1583 lemma nat_1: "nat 1 = Suc 0"
  1584 by (subst nat_eq_iff, simp)
  1585 
  1586 lemma nat_2: "nat 2 = Suc (Suc 0)"
  1587 by (subst nat_eq_iff, simp)
  1588 
  1589 lemma one_less_nat_eq [simp]: "(Suc 0 < nat z) = (1 < z)"
  1590 apply (insert zless_nat_conj [of 1 z])
  1591 apply (auto simp add: nat_1)
  1592 done
  1593 
  1594 text{*This simplifies expressions of the form @{term "int n = z"} where
  1595       z is an integer literal.*}
  1596 lemmas int_eq_iff_number_of [simp] = int_eq_iff [of _ "number_of v", standard]
  1597 
  1598 lemma split_nat [arith_split]:
  1599   "P(nat(i::int)) = ((\<forall>n. i = of_nat n \<longrightarrow> P n) & (i < 0 \<longrightarrow> P 0))"
  1600   (is "?P = (?L & ?R)")
  1601 proof (cases "i < 0")
  1602   case True thus ?thesis by auto
  1603 next
  1604   case False
  1605   have "?P = ?L"
  1606   proof
  1607     assume ?P thus ?L using False by clarsimp
  1608   next
  1609     assume ?L thus ?P using False by simp
  1610   qed
  1611   with False show ?thesis by simp
  1612 qed
  1613 
  1614 context ring_1
  1615 begin
  1616 
  1617 lemma of_int_of_nat [nitpick_simp]:
  1618   "of_int k = (if k < 0 then - of_nat (nat (- k)) else of_nat (nat k))"
  1619 proof (cases "k < 0")
  1620   case True then have "0 \<le> - k" by simp
  1621   then have "of_nat (nat (- k)) = of_int (- k)" by (rule of_nat_nat)
  1622   with True show ?thesis by simp
  1623 next
  1624   case False then show ?thesis by (simp add: not_less of_nat_nat)
  1625 qed
  1626 
  1627 end
  1628 
  1629 lemma nat_mult_distrib:
  1630   fixes z z' :: int
  1631   assumes "0 \<le> z"
  1632   shows "nat (z * z') = nat z * nat z'"
  1633 proof (cases "0 \<le> z'")
  1634   case False with assms have "z * z' \<le> 0"
  1635     by (simp add: not_le mult_le_0_iff)
  1636   then have "nat (z * z') = 0" by simp
  1637   moreover from False have "nat z' = 0" by simp
  1638   ultimately show ?thesis by simp
  1639 next
  1640   case True with assms have ge_0: "z * z' \<ge> 0" by (simp add: zero_le_mult_iff)
  1641   show ?thesis
  1642     by (rule injD [of "of_nat :: nat \<Rightarrow> int", OF inj_of_nat])
  1643       (simp only: of_nat_mult of_nat_nat [OF True]
  1644          of_nat_nat [OF assms] of_nat_nat [OF ge_0], simp)
  1645 qed
  1646 
  1647 lemma nat_mult_distrib_neg: "z \<le> (0::int) ==> nat(z*z') = nat(-z) * nat(-z')"
  1648 apply (rule trans)
  1649 apply (rule_tac [2] nat_mult_distrib, auto)
  1650 done
  1651 
  1652 lemma nat_abs_mult_distrib: "nat (abs (w * z)) = nat (abs w) * nat (abs z)"
  1653 apply (cases "z=0 | w=0")
  1654 apply (auto simp add: abs_if nat_mult_distrib [symmetric] 
  1655                       nat_mult_distrib_neg [symmetric] mult_less_0_iff)
  1656 done
  1657 
  1658 
  1659 subsection "Induction principles for int"
  1660 
  1661 text{*Well-founded segments of the integers*}
  1662 
  1663 definition
  1664   int_ge_less_than  ::  "int => (int * int) set"
  1665 where
  1666   "int_ge_less_than d = {(z',z). d \<le> z' & z' < z}"
  1667 
  1668 theorem wf_int_ge_less_than: "wf (int_ge_less_than d)"
  1669 proof -
  1670   have "int_ge_less_than d \<subseteq> measure (%z. nat (z-d))"
  1671     by (auto simp add: int_ge_less_than_def)
  1672   thus ?thesis 
  1673     by (rule wf_subset [OF wf_measure]) 
  1674 qed
  1675 
  1676 text{*This variant looks odd, but is typical of the relations suggested
  1677 by RankFinder.*}
  1678 
  1679 definition
  1680   int_ge_less_than2 ::  "int => (int * int) set"
  1681 where
  1682   "int_ge_less_than2 d = {(z',z). d \<le> z & z' < z}"
  1683 
  1684 theorem wf_int_ge_less_than2: "wf (int_ge_less_than2 d)"
  1685 proof -
  1686   have "int_ge_less_than2 d \<subseteq> measure (%z. nat (1+z-d))" 
  1687     by (auto simp add: int_ge_less_than2_def)
  1688   thus ?thesis 
  1689     by (rule wf_subset [OF wf_measure]) 
  1690 qed
  1691 
  1692 abbreviation
  1693   int :: "nat \<Rightarrow> int"
  1694 where
  1695   "int \<equiv> of_nat"
  1696 
  1697 (* `set:int': dummy construction *)
  1698 theorem int_ge_induct [case_names base step, induct set: int]:
  1699   fixes i :: int
  1700   assumes ge: "k \<le> i" and
  1701     base: "P k" and
  1702     step: "\<And>i. k \<le> i \<Longrightarrow> P i \<Longrightarrow> P (i + 1)"
  1703   shows "P i"
  1704 proof -
  1705   { fix n have "\<And>i::int. n = nat(i-k) \<Longrightarrow> k \<le> i \<Longrightarrow> P i"
  1706     proof (induct n)
  1707       case 0
  1708       hence "i = k" by arith
  1709       thus "P i" using base by simp
  1710     next
  1711       case (Suc n)
  1712       then have "n = nat((i - 1) - k)" by arith
  1713       moreover
  1714       have ki1: "k \<le> i - 1" using Suc.prems by arith
  1715       ultimately
  1716       have "P(i - 1)" by(rule Suc.hyps)
  1717       from step[OF ki1 this] show ?case by simp
  1718     qed
  1719   }
  1720   with ge show ?thesis by fast
  1721 qed
  1722 
  1723 (* `set:int': dummy construction *)
  1724 theorem int_gr_induct [case_names base step, induct set: int]:
  1725   assumes gr: "k < (i::int)" and
  1726         base: "P(k+1)" and
  1727         step: "\<And>i. \<lbrakk>k < i; P i\<rbrakk> \<Longrightarrow> P(i+1)"
  1728   shows "P i"
  1729 apply(rule int_ge_induct[of "k + 1"])
  1730   using gr apply arith
  1731  apply(rule base)
  1732 apply (rule step, simp+)
  1733 done
  1734 
  1735 theorem int_le_induct[consumes 1,case_names base step]:
  1736   assumes le: "i \<le> (k::int)" and
  1737         base: "P(k)" and
  1738         step: "\<And>i. \<lbrakk>i \<le> k; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
  1739   shows "P i"
  1740 proof -
  1741   { fix n have "\<And>i::int. n = nat(k-i) \<Longrightarrow> i \<le> k \<Longrightarrow> P i"
  1742     proof (induct n)
  1743       case 0
  1744       hence "i = k" by arith
  1745       thus "P i" using base by simp
  1746     next
  1747       case (Suc n)
  1748       hence "n = nat(k - (i+1))" by arith
  1749       moreover
  1750       have ki1: "i + 1 \<le> k" using Suc.prems by arith
  1751       ultimately
  1752       have "P(i+1)" by(rule Suc.hyps)
  1753       from step[OF ki1 this] show ?case by simp
  1754     qed
  1755   }
  1756   with le show ?thesis by fast
  1757 qed
  1758 
  1759 theorem int_less_induct [consumes 1,case_names base step]:
  1760   assumes less: "(i::int) < k" and
  1761         base: "P(k - 1)" and
  1762         step: "\<And>i. \<lbrakk>i < k; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
  1763   shows "P i"
  1764 apply(rule int_le_induct[of _ "k - 1"])
  1765   using less apply arith
  1766  apply(rule base)
  1767 apply (rule step, simp+)
  1768 done
  1769 
  1770 subsection{*Intermediate value theorems*}
  1771 
  1772 lemma int_val_lemma:
  1773      "(\<forall>i<n::nat. abs(f(i+1) - f i) \<le> 1) -->  
  1774       f 0 \<le> k --> k \<le> f n --> (\<exists>i \<le> n. f i = (k::int))"
  1775 unfolding One_nat_def
  1776 apply (induct n, simp)
  1777 apply (intro strip)
  1778 apply (erule impE, simp)
  1779 apply (erule_tac x = n in allE, simp)
  1780 apply (case_tac "k = f (Suc n)")
  1781 apply force
  1782 apply (erule impE)
  1783  apply (simp add: abs_if split add: split_if_asm)
  1784 apply (blast intro: le_SucI)
  1785 done
  1786 
  1787 lemmas nat0_intermed_int_val = int_val_lemma [rule_format (no_asm)]
  1788 
  1789 lemma nat_intermed_int_val:
  1790      "[| \<forall>i. m \<le> i & i < n --> abs(f(i + 1::nat) - f i) \<le> 1; m < n;  
  1791          f m \<le> k; k \<le> f n |] ==> ? i. m \<le> i & i \<le> n & f i = (k::int)"
  1792 apply (cut_tac n = "n-m" and f = "%i. f (i+m) " and k = k 
  1793        in int_val_lemma)
  1794 unfolding One_nat_def
  1795 apply simp
  1796 apply (erule exE)
  1797 apply (rule_tac x = "i+m" in exI, arith)
  1798 done
  1799 
  1800 
  1801 subsection{*Products and 1, by T. M. Rasmussen*}
  1802 
  1803 lemma zabs_less_one_iff [simp]: "(\<bar>z\<bar> < 1) = (z = (0::int))"
  1804 by arith
  1805 
  1806 lemma abs_zmult_eq_1:
  1807   assumes mn: "\<bar>m * n\<bar> = 1"
  1808   shows "\<bar>m\<bar> = (1::int)"
  1809 proof -
  1810   have 0: "m \<noteq> 0 & n \<noteq> 0" using mn
  1811     by auto
  1812   have "~ (2 \<le> \<bar>m\<bar>)"
  1813   proof
  1814     assume "2 \<le> \<bar>m\<bar>"
  1815     hence "2*\<bar>n\<bar> \<le> \<bar>m\<bar>*\<bar>n\<bar>"
  1816       by (simp add: mult_mono 0) 
  1817     also have "... = \<bar>m*n\<bar>" 
  1818       by (simp add: abs_mult)
  1819     also have "... = 1"
  1820       by (simp add: mn)
  1821     finally have "2*\<bar>n\<bar> \<le> 1" .
  1822     thus "False" using 0
  1823       by auto
  1824   qed
  1825   thus ?thesis using 0
  1826     by auto
  1827 qed
  1828 
  1829 lemma pos_zmult_eq_1_iff_lemma: "(m * n = 1) ==> m = (1::int) | m = -1"
  1830 by (insert abs_zmult_eq_1 [of m n], arith)
  1831 
  1832 lemma pos_zmult_eq_1_iff:
  1833   assumes "0 < (m::int)" shows "(m * n = 1) = (m = 1 & n = 1)"
  1834 proof -
  1835   from assms have "m * n = 1 ==> m = 1" by (auto dest: pos_zmult_eq_1_iff_lemma)
  1836   thus ?thesis by (auto dest: pos_zmult_eq_1_iff_lemma)
  1837 qed
  1838 
  1839 lemma zmult_eq_1_iff: "(m*n = (1::int)) = ((m = 1 & n = 1) | (m = -1 & n = -1))"
  1840 apply (rule iffI) 
  1841  apply (frule pos_zmult_eq_1_iff_lemma)
  1842  apply (simp add: mult_commute [of m]) 
  1843  apply (frule pos_zmult_eq_1_iff_lemma, auto) 
  1844 done
  1845 
  1846 lemma infinite_UNIV_int: "\<not> finite (UNIV::int set)"
  1847 proof
  1848   assume "finite (UNIV::int set)"
  1849   moreover have "inj (\<lambda>i\<Colon>int. 2 * i)"
  1850     by (rule injI) simp
  1851   ultimately have "surj (\<lambda>i\<Colon>int. 2 * i)"
  1852     by (rule finite_UNIV_inj_surj)
  1853   then obtain i :: int where "1 = 2 * i" by (rule surjE)
  1854   then show False by (simp add: pos_zmult_eq_1_iff)
  1855 qed
  1856 
  1857 
  1858 subsection {* Further theorems on numerals *}
  1859 
  1860 subsubsection{*Special Simplification for Constants*}
  1861 
  1862 text{*These distributive laws move literals inside sums and differences.*}
  1863 
  1864 lemmas left_distrib_number_of [simp] =
  1865   left_distrib [of _ _ "number_of v", standard]
  1866 
  1867 lemmas right_distrib_number_of [simp] =
  1868   right_distrib [of "number_of v", standard]
  1869 
  1870 lemmas left_diff_distrib_number_of [simp] =
  1871   left_diff_distrib [of _ _ "number_of v", standard]
  1872 
  1873 lemmas right_diff_distrib_number_of [simp] =
  1874   right_diff_distrib [of "number_of v", standard]
  1875 
  1876 text{*These are actually for fields, like real: but where else to put them?*}
  1877 
  1878 lemmas zero_less_divide_iff_number_of [simp, no_atp] =
  1879   zero_less_divide_iff [of "number_of w", standard]
  1880 
  1881 lemmas divide_less_0_iff_number_of [simp, no_atp] =
  1882   divide_less_0_iff [of "number_of w", standard]
  1883 
  1884 lemmas zero_le_divide_iff_number_of [simp, no_atp] =
  1885   zero_le_divide_iff [of "number_of w", standard]
  1886 
  1887 lemmas divide_le_0_iff_number_of [simp, no_atp] =
  1888   divide_le_0_iff [of "number_of w", standard]
  1889 
  1890 
  1891 text {*Replaces @{text "inverse #nn"} by @{text "1/#nn"}.  It looks
  1892   strange, but then other simprocs simplify the quotient.*}
  1893 
  1894 lemmas inverse_eq_divide_number_of [simp] =
  1895   inverse_eq_divide [of "number_of w", standard]
  1896 
  1897 text {*These laws simplify inequalities, moving unary minus from a term
  1898 into the literal.*}
  1899 
  1900 lemmas less_minus_iff_number_of [simp, no_atp] =
  1901   less_minus_iff [of "number_of v", standard]
  1902 
  1903 lemmas le_minus_iff_number_of [simp, no_atp] =
  1904   le_minus_iff [of "number_of v", standard]
  1905 
  1906 lemmas equation_minus_iff_number_of [simp, no_atp] =
  1907   equation_minus_iff [of "number_of v", standard]
  1908 
  1909 lemmas minus_less_iff_number_of [simp, no_atp] =
  1910   minus_less_iff [of _ "number_of v", standard]
  1911 
  1912 lemmas minus_le_iff_number_of [simp, no_atp] =
  1913   minus_le_iff [of _ "number_of v", standard]
  1914 
  1915 lemmas minus_equation_iff_number_of [simp, no_atp] =
  1916   minus_equation_iff [of _ "number_of v", standard]
  1917 
  1918 
  1919 text{*To Simplify Inequalities Where One Side is the Constant 1*}
  1920 
  1921 lemma less_minus_iff_1 [simp,no_atp]:
  1922   fixes b::"'b::{linordered_idom,number_ring}"
  1923   shows "(1 < - b) = (b < -1)"
  1924 by auto
  1925 
  1926 lemma le_minus_iff_1 [simp,no_atp]:
  1927   fixes b::"'b::{linordered_idom,number_ring}"
  1928   shows "(1 \<le> - b) = (b \<le> -1)"
  1929 by auto
  1930 
  1931 lemma equation_minus_iff_1 [simp,no_atp]:
  1932   fixes b::"'b::number_ring"
  1933   shows "(1 = - b) = (b = -1)"
  1934 by (subst equation_minus_iff, auto)
  1935 
  1936 lemma minus_less_iff_1 [simp,no_atp]:
  1937   fixes a::"'b::{linordered_idom,number_ring}"
  1938   shows "(- a < 1) = (-1 < a)"
  1939 by auto
  1940 
  1941 lemma minus_le_iff_1 [simp,no_atp]:
  1942   fixes a::"'b::{linordered_idom,number_ring}"
  1943   shows "(- a \<le> 1) = (-1 \<le> a)"
  1944 by auto
  1945 
  1946 lemma minus_equation_iff_1 [simp,no_atp]:
  1947   fixes a::"'b::number_ring"
  1948   shows "(- a = 1) = (a = -1)"
  1949 by (subst minus_equation_iff, auto)
  1950 
  1951 
  1952 text {*Cancellation of constant factors in comparisons (@{text "<"} and @{text "\<le>"}) *}
  1953 
  1954 lemmas mult_less_cancel_left_number_of [simp, no_atp] =
  1955   mult_less_cancel_left [of "number_of v", standard]
  1956 
  1957 lemmas mult_less_cancel_right_number_of [simp, no_atp] =
  1958   mult_less_cancel_right [of _ "number_of v", standard]
  1959 
  1960 lemmas mult_le_cancel_left_number_of [simp, no_atp] =
  1961   mult_le_cancel_left [of "number_of v", standard]
  1962 
  1963 lemmas mult_le_cancel_right_number_of [simp, no_atp] =
  1964   mult_le_cancel_right [of _ "number_of v", standard]
  1965 
  1966 
  1967 text {*Multiplying out constant divisors in comparisons (@{text "<"}, @{text "\<le>"} and @{text "="}) *}
  1968 
  1969 lemmas le_divide_eq_number_of1 [simp] = le_divide_eq [of _ _ "number_of w", standard]
  1970 lemmas divide_le_eq_number_of1 [simp] = divide_le_eq [of _ "number_of w", standard]
  1971 lemmas less_divide_eq_number_of1 [simp] = less_divide_eq [of _ _ "number_of w", standard]
  1972 lemmas divide_less_eq_number_of1 [simp] = divide_less_eq [of _ "number_of w", standard]
  1973 lemmas eq_divide_eq_number_of1 [simp] = eq_divide_eq [of _ _ "number_of w", standard]
  1974 lemmas divide_eq_eq_number_of1 [simp] = divide_eq_eq [of _ "number_of w", standard]
  1975 
  1976 
  1977 subsubsection{*Optional Simplification Rules Involving Constants*}
  1978 
  1979 text{*Simplify quotients that are compared with a literal constant.*}
  1980 
  1981 lemmas le_divide_eq_number_of = le_divide_eq [of "number_of w", standard]
  1982 lemmas divide_le_eq_number_of = divide_le_eq [of _ _ "number_of w", standard]
  1983 lemmas less_divide_eq_number_of = less_divide_eq [of "number_of w", standard]
  1984 lemmas divide_less_eq_number_of = divide_less_eq [of _ _ "number_of w", standard]
  1985 lemmas eq_divide_eq_number_of = eq_divide_eq [of "number_of w", standard]
  1986 lemmas divide_eq_eq_number_of = divide_eq_eq [of _ _ "number_of w", standard]
  1987 
  1988 
  1989 text{*Not good as automatic simprules because they cause case splits.*}
  1990 lemmas divide_const_simps =
  1991   le_divide_eq_number_of divide_le_eq_number_of less_divide_eq_number_of
  1992   divide_less_eq_number_of eq_divide_eq_number_of divide_eq_eq_number_of
  1993   le_divide_eq_1 divide_le_eq_1 less_divide_eq_1 divide_less_eq_1
  1994 
  1995 text{*Division By @{text "-1"}*}
  1996 
  1997 lemma divide_minus1 [simp]:
  1998      "x/-1 = -(x::'a::{field_inverse_zero, number_ring})"
  1999 by simp
  2000 
  2001 lemma minus1_divide [simp]:
  2002      "-1 / (x::'a::{field_inverse_zero, number_ring}) = - (1/x)"
  2003 by (simp add: divide_inverse)
  2004 
  2005 lemma half_gt_zero_iff:
  2006      "(0 < r/2) = (0 < (r::'a::{linordered_field_inverse_zero,number_ring}))"
  2007 by auto
  2008 
  2009 lemmas half_gt_zero [simp] = half_gt_zero_iff [THEN iffD2, standard]
  2010 
  2011 
  2012 subsection {* The divides relation *}
  2013 
  2014 lemma zdvd_antisym_nonneg:
  2015     "0 <= m ==> 0 <= n ==> m dvd n ==> n dvd m ==> m = (n::int)"
  2016   apply (simp add: dvd_def, auto)
  2017   apply (auto simp add: mult_assoc zero_le_mult_iff zmult_eq_1_iff)
  2018   done
  2019 
  2020 lemma zdvd_antisym_abs: assumes "(a::int) dvd b" and "b dvd a" 
  2021   shows "\<bar>a\<bar> = \<bar>b\<bar>"
  2022 proof cases
  2023   assume "a = 0" with assms show ?thesis by simp
  2024 next
  2025   assume "a \<noteq> 0"
  2026   from `a dvd b` obtain k where k:"b = a*k" unfolding dvd_def by blast 
  2027   from `b dvd a` obtain k' where k':"a = b*k'" unfolding dvd_def by blast 
  2028   from k k' have "a = a*k*k'" by simp
  2029   with mult_cancel_left1[where c="a" and b="k*k'"]
  2030   have kk':"k*k' = 1" using `a\<noteq>0` by (simp add: mult_assoc)
  2031   hence "k = 1 \<and> k' = 1 \<or> k = -1 \<and> k' = -1" by (simp add: zmult_eq_1_iff)
  2032   thus ?thesis using k k' by auto
  2033 qed
  2034 
  2035 lemma zdvd_zdiffD: "k dvd m - n ==> k dvd n ==> k dvd (m::int)"
  2036   apply (subgoal_tac "m = n + (m - n)")
  2037    apply (erule ssubst)
  2038    apply (blast intro: dvd_add, simp)
  2039   done
  2040 
  2041 lemma zdvd_reduce: "(k dvd n + k * m) = (k dvd (n::int))"
  2042 apply (rule iffI)
  2043  apply (erule_tac [2] dvd_add)
  2044  apply (subgoal_tac "n = (n + k * m) - k * m")
  2045   apply (erule ssubst)
  2046   apply (erule dvd_diff)
  2047   apply(simp_all)
  2048 done
  2049 
  2050 lemma dvd_imp_le_int:
  2051   fixes d i :: int
  2052   assumes "i \<noteq> 0" and "d dvd i"
  2053   shows "\<bar>d\<bar> \<le> \<bar>i\<bar>"
  2054 proof -
  2055   from `d dvd i` obtain k where "i = d * k" ..
  2056   with `i \<noteq> 0` have "k \<noteq> 0" by auto
  2057   then have "1 \<le> \<bar>k\<bar>" and "0 \<le> \<bar>d\<bar>" by auto
  2058   then have "\<bar>d\<bar> * 1 \<le> \<bar>d\<bar> * \<bar>k\<bar>" by (rule mult_left_mono)
  2059   with `i = d * k` show ?thesis by (simp add: abs_mult)
  2060 qed
  2061 
  2062 lemma zdvd_not_zless:
  2063   fixes m n :: int
  2064   assumes "0 < m" and "m < n"
  2065   shows "\<not> n dvd m"
  2066 proof
  2067   from assms have "0 < n" by auto
  2068   assume "n dvd m" then obtain k where k: "m = n * k" ..
  2069   with `0 < m` have "0 < n * k" by auto
  2070   with `0 < n` have "0 < k" by (simp add: zero_less_mult_iff)
  2071   with k `0 < n` `m < n` have "n * k < n * 1" by simp
  2072   with `0 < n` `0 < k` show False unfolding mult_less_cancel_left by auto
  2073 qed
  2074 
  2075 lemma zdvd_mult_cancel: assumes d:"k * m dvd k * n" and kz:"k \<noteq> (0::int)"
  2076   shows "m dvd n"
  2077 proof-
  2078   from d obtain h where h: "k*n = k*m * h" unfolding dvd_def by blast
  2079   {assume "n \<noteq> m*h" hence "k* n \<noteq> k* (m*h)" using kz by simp
  2080     with h have False by (simp add: mult_assoc)}
  2081   hence "n = m * h" by blast
  2082   thus ?thesis by simp
  2083 qed
  2084 
  2085 theorem zdvd_int: "(x dvd y) = (int x dvd int y)"
  2086 proof -
  2087   have "\<And>k. int y = int x * k \<Longrightarrow> x dvd y"
  2088   proof -
  2089     fix k
  2090     assume A: "int y = int x * k"
  2091     then show "x dvd y" proof (cases k)
  2092       case (1 n) with A have "y = x * n" by (simp add: of_nat_mult [symmetric])
  2093       then show ?thesis ..
  2094     next
  2095       case (2 n) with A have "int y = int x * (- int (Suc n))" by simp
  2096       also have "\<dots> = - (int x * int (Suc n))" by (simp only: mult_minus_right)
  2097       also have "\<dots> = - int (x * Suc n)" by (simp only: of_nat_mult [symmetric])
  2098       finally have "- int (x * Suc n) = int y" ..
  2099       then show ?thesis by (simp only: negative_eq_positive) auto
  2100     qed
  2101   qed
  2102   then show ?thesis by (auto elim!: dvdE simp only: dvd_triv_left of_nat_mult)
  2103 qed
  2104 
  2105 lemma zdvd1_eq[simp]: "(x::int) dvd 1 = ( \<bar>x\<bar> = 1)"
  2106 proof
  2107   assume d: "x dvd 1" hence "int (nat \<bar>x\<bar>) dvd int (nat 1)" by simp
  2108   hence "nat \<bar>x\<bar> dvd 1" by (simp add: zdvd_int)
  2109   hence "nat \<bar>x\<bar> = 1"  by simp
  2110   thus "\<bar>x\<bar> = 1" by (cases "x < 0", auto)
  2111 next
  2112   assume "\<bar>x\<bar>=1"
  2113   then have "x = 1 \<or> x = -1" by auto
  2114   then show "x dvd 1" by (auto intro: dvdI)
  2115 qed
  2116 
  2117 lemma zdvd_mult_cancel1: 
  2118   assumes mp:"m \<noteq>(0::int)" shows "(m * n dvd m) = (\<bar>n\<bar> = 1)"
  2119 proof
  2120   assume n1: "\<bar>n\<bar> = 1" thus "m * n dvd m" 
  2121     by (cases "n >0", auto simp add: minus_equation_iff)
  2122 next
  2123   assume H: "m * n dvd m" hence H2: "m * n dvd m * 1" by simp
  2124   from zdvd_mult_cancel[OF H2 mp] show "\<bar>n\<bar> = 1" by (simp only: zdvd1_eq)
  2125 qed
  2126 
  2127 lemma int_dvd_iff: "(int m dvd z) = (m dvd nat (abs z))"
  2128   unfolding zdvd_int by (cases "z \<ge> 0") simp_all
  2129 
  2130 lemma dvd_int_iff: "(z dvd int m) = (nat (abs z) dvd m)"
  2131   unfolding zdvd_int by (cases "z \<ge> 0") simp_all
  2132 
  2133 lemma nat_dvd_iff: "(nat z dvd m) = (if 0 \<le> z then (z dvd int m) else m = 0)"
  2134   by (auto simp add: dvd_int_iff)
  2135 
  2136 lemma eq_nat_nat_iff:
  2137   "0 \<le> z \<Longrightarrow> 0 \<le> z' \<Longrightarrow> nat z = nat z' \<longleftrightarrow> z = z'"
  2138   by (auto elim!: nonneg_eq_int)
  2139 
  2140 lemma nat_power_eq:
  2141   "0 \<le> z \<Longrightarrow> nat (z ^ n) = nat z ^ n"
  2142   by (induct n) (simp_all add: nat_mult_distrib)
  2143 
  2144 lemma zdvd_imp_le: "[| z dvd n; 0 < n |] ==> z \<le> (n::int)"
  2145   apply (rule_tac z=n in int_cases)
  2146   apply (auto simp add: dvd_int_iff)
  2147   apply (rule_tac z=z in int_cases)
  2148   apply (auto simp add: dvd_imp_le)
  2149   done
  2150 
  2151 
  2152 subsection {* Configuration of the code generator *}
  2153 
  2154 code_datatype Pls Min Bit0 Bit1 "number_of \<Colon> int \<Rightarrow> int"
  2155 
  2156 lemmas pred_succ_numeral_code [code] =
  2157   pred_bin_simps succ_bin_simps
  2158 
  2159 lemmas plus_numeral_code [code] =
  2160   add_bin_simps
  2161   arith_extra_simps(1) [where 'a = int]
  2162 
  2163 lemmas minus_numeral_code [code] =
  2164   minus_bin_simps
  2165   arith_extra_simps(2) [where 'a = int]
  2166   arith_extra_simps(5) [where 'a = int]
  2167 
  2168 lemmas times_numeral_code [code] =
  2169   mult_bin_simps
  2170   arith_extra_simps(4) [where 'a = int]
  2171 
  2172 instantiation int :: eq
  2173 begin
  2174 
  2175 definition [code del]: "eq_class.eq k l \<longleftrightarrow> k - l = (0\<Colon>int)"
  2176 
  2177 instance by default (simp add: eq_int_def)
  2178 
  2179 end
  2180 
  2181 lemma eq_number_of_int_code [code]:
  2182   "eq_class.eq (number_of k \<Colon> int) (number_of l) \<longleftrightarrow> eq_class.eq k l"
  2183   unfolding eq_int_def number_of_is_id ..
  2184 
  2185 lemma eq_int_code [code]:
  2186   "eq_class.eq Int.Pls Int.Pls \<longleftrightarrow> True"
  2187   "eq_class.eq Int.Pls Int.Min \<longleftrightarrow> False"
  2188   "eq_class.eq Int.Pls (Int.Bit0 k2) \<longleftrightarrow> eq_class.eq Int.Pls k2"
  2189   "eq_class.eq Int.Pls (Int.Bit1 k2) \<longleftrightarrow> False"
  2190   "eq_class.eq Int.Min Int.Pls \<longleftrightarrow> False"
  2191   "eq_class.eq Int.Min Int.Min \<longleftrightarrow> True"
  2192   "eq_class.eq Int.Min (Int.Bit0 k2) \<longleftrightarrow> False"
  2193   "eq_class.eq Int.Min (Int.Bit1 k2) \<longleftrightarrow> eq_class.eq Int.Min k2"
  2194   "eq_class.eq (Int.Bit0 k1) Int.Pls \<longleftrightarrow> eq_class.eq k1 Int.Pls"
  2195   "eq_class.eq (Int.Bit1 k1) Int.Pls \<longleftrightarrow> False"
  2196   "eq_class.eq (Int.Bit0 k1) Int.Min \<longleftrightarrow> False"
  2197   "eq_class.eq (Int.Bit1 k1) Int.Min \<longleftrightarrow> eq_class.eq k1 Int.Min"
  2198   "eq_class.eq (Int.Bit0 k1) (Int.Bit0 k2) \<longleftrightarrow> eq_class.eq k1 k2"
  2199   "eq_class.eq (Int.Bit0 k1) (Int.Bit1 k2) \<longleftrightarrow> False"
  2200   "eq_class.eq (Int.Bit1 k1) (Int.Bit0 k2) \<longleftrightarrow> False"
  2201   "eq_class.eq (Int.Bit1 k1) (Int.Bit1 k2) \<longleftrightarrow> eq_class.eq k1 k2"
  2202   unfolding eq_equals by simp_all
  2203 
  2204 lemma eq_int_refl [code nbe]:
  2205   "eq_class.eq (k::int) k \<longleftrightarrow> True"
  2206   by (rule HOL.eq_refl)
  2207 
  2208 lemma less_eq_number_of_int_code [code]:
  2209   "(number_of k \<Colon> int) \<le> number_of l \<longleftrightarrow> k \<le> l"
  2210   unfolding number_of_is_id ..
  2211 
  2212 lemma less_eq_int_code [code]:
  2213   "Int.Pls \<le> Int.Pls \<longleftrightarrow> True"
  2214   "Int.Pls \<le> Int.Min \<longleftrightarrow> False"
  2215   "Int.Pls \<le> Int.Bit0 k \<longleftrightarrow> Int.Pls \<le> k"
  2216   "Int.Pls \<le> Int.Bit1 k \<longleftrightarrow> Int.Pls \<le> k"
  2217   "Int.Min \<le> Int.Pls \<longleftrightarrow> True"
  2218   "Int.Min \<le> Int.Min \<longleftrightarrow> True"
  2219   "Int.Min \<le> Int.Bit0 k \<longleftrightarrow> Int.Min < k"
  2220   "Int.Min \<le> Int.Bit1 k \<longleftrightarrow> Int.Min \<le> k"
  2221   "Int.Bit0 k \<le> Int.Pls \<longleftrightarrow> k \<le> Int.Pls"
  2222   "Int.Bit1 k \<le> Int.Pls \<longleftrightarrow> k < Int.Pls"
  2223   "Int.Bit0 k \<le> Int.Min \<longleftrightarrow> k \<le> Int.Min"
  2224   "Int.Bit1 k \<le> Int.Min \<longleftrightarrow> k \<le> Int.Min"
  2225   "Int.Bit0 k1 \<le> Int.Bit0 k2 \<longleftrightarrow> k1 \<le> k2"
  2226   "Int.Bit0 k1 \<le> Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
  2227   "Int.Bit1 k1 \<le> Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
  2228   "Int.Bit1 k1 \<le> Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
  2229   by simp_all
  2230 
  2231 lemma less_number_of_int_code [code]:
  2232   "(number_of k \<Colon> int) < number_of l \<longleftrightarrow> k < l"
  2233   unfolding number_of_is_id ..
  2234 
  2235 lemma less_int_code [code]:
  2236   "Int.Pls < Int.Pls \<longleftrightarrow> False"
  2237   "Int.Pls < Int.Min \<longleftrightarrow> False"
  2238   "Int.Pls < Int.Bit0 k \<longleftrightarrow> Int.Pls < k"
  2239   "Int.Pls < Int.Bit1 k \<longleftrightarrow> Int.Pls \<le> k"
  2240   "Int.Min < Int.Pls \<longleftrightarrow> True"
  2241   "Int.Min < Int.Min \<longleftrightarrow> False"
  2242   "Int.Min < Int.Bit0 k \<longleftrightarrow> Int.Min < k"
  2243   "Int.Min < Int.Bit1 k \<longleftrightarrow> Int.Min < k"
  2244   "Int.Bit0 k < Int.Pls \<longleftrightarrow> k < Int.Pls"
  2245   "Int.Bit1 k < Int.Pls \<longleftrightarrow> k < Int.Pls"
  2246   "Int.Bit0 k < Int.Min \<longleftrightarrow> k \<le> Int.Min"
  2247   "Int.Bit1 k < Int.Min \<longleftrightarrow> k < Int.Min"
  2248   "Int.Bit0 k1 < Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
  2249   "Int.Bit0 k1 < Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
  2250   "Int.Bit1 k1 < Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
  2251   "Int.Bit1 k1 < Int.Bit1 k2 \<longleftrightarrow> k1 < k2"
  2252   by simp_all
  2253 
  2254 definition
  2255   nat_aux :: "int \<Rightarrow> nat \<Rightarrow> nat" where
  2256   "nat_aux i n = nat i + n"
  2257 
  2258 lemma [code]:
  2259   "nat_aux i n = (if i \<le> 0 then n else nat_aux (i - 1) (Suc n))"  -- {* tail recursive *}
  2260   by (auto simp add: nat_aux_def nat_eq_iff linorder_not_le order_less_imp_le
  2261     dest: zless_imp_add1_zle)
  2262 
  2263 lemma [code]: "nat i = nat_aux i 0"
  2264   by (simp add: nat_aux_def)
  2265 
  2266 hide_const (open) nat_aux
  2267 
  2268 lemma zero_is_num_zero [code, code_unfold_post]:
  2269   "(0\<Colon>int) = Numeral0" 
  2270   by simp
  2271 
  2272 lemma one_is_num_one [code, code_unfold_post]:
  2273   "(1\<Colon>int) = Numeral1" 
  2274   by simp
  2275 
  2276 code_modulename SML
  2277   Int Arith
  2278 
  2279 code_modulename OCaml
  2280   Int Arith
  2281 
  2282 code_modulename Haskell
  2283   Int Arith
  2284 
  2285 types_code
  2286   "int" ("int")
  2287 attach (term_of) {*
  2288 val term_of_int = HOLogic.mk_number HOLogic.intT;
  2289 *}
  2290 attach (test) {*
  2291 fun gen_int i =
  2292   let val j = one_of [~1, 1] * random_range 0 i
  2293   in (j, fn () => term_of_int j) end;
  2294 *}
  2295 
  2296 setup {*
  2297 let
  2298 
  2299 fun strip_number_of (@{term "Int.number_of :: int => int"} $ t) = t
  2300   | strip_number_of t = t;
  2301 
  2302 fun numeral_codegen thy defs dep module b t gr =
  2303   let val i = HOLogic.dest_numeral (strip_number_of t)
  2304   in
  2305     SOME (Codegen.str (string_of_int i),
  2306       snd (Codegen.invoke_tycodegen thy defs dep module false HOLogic.intT gr))
  2307   end handle TERM _ => NONE;
  2308 
  2309 in
  2310 
  2311 Codegen.add_codegen "numeral_codegen" numeral_codegen
  2312 
  2313 end
  2314 *}
  2315 
  2316 consts_code
  2317   "number_of :: int \<Rightarrow> int"    ("(_)")
  2318   "0 :: int"                   ("0")
  2319   "1 :: int"                   ("1")
  2320   "uminus :: int => int"       ("~")
  2321   "op + :: int => int => int"  ("(_ +/ _)")
  2322   "op * :: int => int => int"  ("(_ */ _)")
  2323   "op \<le> :: int => int => bool" ("(_ <=/ _)")
  2324   "op < :: int => int => bool" ("(_ </ _)")
  2325 
  2326 quickcheck_params [default_type = int]
  2327 
  2328 hide_const (open) Pls Min Bit0 Bit1 succ pred
  2329 
  2330 
  2331 subsection {* Legacy theorems *}
  2332 
  2333 lemmas zminus_zminus = minus_minus [of "z::int", standard]
  2334 lemmas zminus_0 = minus_zero [where 'a=int]
  2335 lemmas zminus_zadd_distrib = minus_add_distrib [of "z::int" "w", standard]
  2336 lemmas zadd_commute = add_commute [of "z::int" "w", standard]
  2337 lemmas zadd_assoc = add_assoc [of "z1::int" "z2" "z3", standard]
  2338 lemmas zadd_left_commute = add_left_commute [of "x::int" "y" "z", standard]
  2339 lemmas zadd_ac = zadd_assoc zadd_commute zadd_left_commute
  2340 lemmas zmult_ac = mult_ac
  2341 lemmas zadd_0 = add_0_left [of "z::int", standard]
  2342 lemmas zadd_0_right = add_0_right [of "z::int", standard]
  2343 lemmas zadd_zminus_inverse2 = left_minus [of "z::int", standard]
  2344 lemmas zmult_zminus = mult_minus_left [of "z::int" "w", standard]
  2345 lemmas zmult_commute = mult_commute [of "z::int" "w", standard]
  2346 lemmas zmult_assoc = mult_assoc [of "z1::int" "z2" "z3", standard]
  2347 lemmas zadd_zmult_distrib = left_distrib [of "z1::int" "z2" "w", standard]
  2348 lemmas zadd_zmult_distrib2 = right_distrib [of "w::int" "z1" "z2", standard]
  2349 lemmas zdiff_zmult_distrib = left_diff_distrib [of "z1::int" "z2" "w", standard]
  2350 lemmas zdiff_zmult_distrib2 = right_diff_distrib [of "w::int" "z1" "z2", standard]
  2351 
  2352 lemmas zmult_1 = mult_1_left [of "z::int", standard]
  2353 lemmas zmult_1_right = mult_1_right [of "z::int", standard]
  2354 
  2355 lemmas zle_refl = order_refl [of "w::int", standard]
  2356 lemmas zle_trans = order_trans [where 'a=int and x="i" and y="j" and z="k", standard]
  2357 lemmas zle_antisym = order_antisym [of "z::int" "w", standard]
  2358 lemmas zle_linear = linorder_linear [of "z::int" "w", standard]
  2359 lemmas zless_linear = linorder_less_linear [where 'a = int]
  2360 
  2361 lemmas zadd_left_mono = add_left_mono [of "i::int" "j" "k", standard]
  2362 lemmas zadd_strict_right_mono = add_strict_right_mono [of "i::int" "j" "k", standard]
  2363 lemmas zadd_zless_mono = add_less_le_mono [of "w'::int" "w" "z'" "z", standard]
  2364 
  2365 lemmas int_0_less_1 = zero_less_one [where 'a=int]
  2366 lemmas int_0_neq_1 = zero_neq_one [where 'a=int]
  2367 
  2368 lemmas inj_int = inj_of_nat [where 'a=int]
  2369 lemmas zadd_int = of_nat_add [where 'a=int, symmetric]
  2370 lemmas int_mult = of_nat_mult [where 'a=int]
  2371 lemmas zmult_int = of_nat_mult [where 'a=int, symmetric]
  2372 lemmas int_eq_0_conv = of_nat_eq_0_iff [where 'a=int and m="n", standard]
  2373 lemmas zless_int = of_nat_less_iff [where 'a=int]
  2374 lemmas int_less_0_conv = of_nat_less_0_iff [where 'a=int and m="k", standard]
  2375 lemmas zero_less_int_conv = of_nat_0_less_iff [where 'a=int]
  2376 lemmas zero_zle_int = of_nat_0_le_iff [where 'a=int]
  2377 lemmas int_le_0_conv = of_nat_le_0_iff [where 'a=int and m="n", standard]
  2378 lemmas int_0 = of_nat_0 [where 'a=int]
  2379 lemmas int_1 = of_nat_1 [where 'a=int]
  2380 lemmas int_Suc = of_nat_Suc [where 'a=int]
  2381 lemmas abs_int_eq = abs_of_nat [where 'a=int and n="m", standard]
  2382 lemmas of_int_int_eq = of_int_of_nat_eq [where 'a=int]
  2383 lemmas zdiff_int = of_nat_diff [where 'a=int, symmetric]
  2384 lemmas zless_le = less_int_def
  2385 lemmas int_eq_of_nat = TrueI
  2386 
  2387 lemma zpower_zadd_distrib:
  2388   "x ^ (y + z) = ((x ^ y) * (x ^ z)::int)"
  2389   by (rule power_add)
  2390 
  2391 lemma zero_less_zpower_abs_iff:
  2392   "(0 < abs x ^ n) \<longleftrightarrow> (x \<noteq> (0::int) | n = 0)"
  2393   by (rule zero_less_power_abs_iff)
  2394 
  2395 lemma zero_le_zpower_abs: "(0::int) \<le> abs x ^ n"
  2396   by (rule zero_le_power_abs)
  2397 
  2398 lemma zpower_zpower:
  2399   "(x ^ y) ^ z = (x ^ (y * z)::int)"
  2400   by (rule power_mult [symmetric])
  2401 
  2402 lemma int_power:
  2403   "int (m ^ n) = int m ^ n"
  2404   by (rule of_nat_power)
  2405 
  2406 lemmas zpower_int = int_power [symmetric]
  2407 
  2408 end