src/HOL/Int.thy
author haftmann
Mon Oct 09 19:10:47 2017 +0200 (21 months ago)
changeset 66836 4eb431c3f974
parent 66816 212a3334e7da
child 66886 960509bfd47e
permissions -rw-r--r--
tuned imports
     1 (*  Title:      HOL/Int.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Author:     Tobias Nipkow, Florian Haftmann, TU Muenchen
     4 *)
     5 
     6 section \<open>The Integers as Equivalence Classes over Pairs of Natural Numbers\<close>
     7 
     8 theory Int
     9   imports Equiv_Relations Power Quotient Fun_Def
    10 begin
    11 
    12 subsection \<open>Definition of integers as a quotient type\<close>
    13 
    14 definition intrel :: "(nat \<times> nat) \<Rightarrow> (nat \<times> nat) \<Rightarrow> bool"
    15   where "intrel = (\<lambda>(x, y) (u, v). x + v = u + y)"
    16 
    17 lemma intrel_iff [simp]: "intrel (x, y) (u, v) \<longleftrightarrow> x + v = u + y"
    18   by (simp add: intrel_def)
    19 
    20 quotient_type int = "nat \<times> nat" / "intrel"
    21   morphisms Rep_Integ Abs_Integ
    22 proof (rule equivpI)
    23   show "reflp intrel" by (auto simp: reflp_def)
    24   show "symp intrel" by (auto simp: symp_def)
    25   show "transp intrel" by (auto simp: transp_def)
    26 qed
    27 
    28 lemma eq_Abs_Integ [case_names Abs_Integ, cases type: int]:
    29   "(\<And>x y. z = Abs_Integ (x, y) \<Longrightarrow> P) \<Longrightarrow> P"
    30   by (induct z) auto
    31 
    32 
    33 subsection \<open>Integers form a commutative ring\<close>
    34 
    35 instantiation int :: comm_ring_1
    36 begin
    37 
    38 lift_definition zero_int :: "int" is "(0, 0)" .
    39 
    40 lift_definition one_int :: "int" is "(1, 0)" .
    41 
    42 lift_definition plus_int :: "int \<Rightarrow> int \<Rightarrow> int"
    43   is "\<lambda>(x, y) (u, v). (x + u, y + v)"
    44   by clarsimp
    45 
    46 lift_definition uminus_int :: "int \<Rightarrow> int"
    47   is "\<lambda>(x, y). (y, x)"
    48   by clarsimp
    49 
    50 lift_definition minus_int :: "int \<Rightarrow> int \<Rightarrow> int"
    51   is "\<lambda>(x, y) (u, v). (x + v, y + u)"
    52   by clarsimp
    53 
    54 lift_definition times_int :: "int \<Rightarrow> int \<Rightarrow> int"
    55   is "\<lambda>(x, y) (u, v). (x*u + y*v, x*v + y*u)"
    56 proof (clarsimp)
    57   fix s t u v w x y z :: nat
    58   assume "s + v = u + t" and "w + z = y + x"
    59   then have "(s + v) * w + (u + t) * x + u * (w + z) + v * (y + x) =
    60     (u + t) * w + (s + v) * x + u * (y + x) + v * (w + z)"
    61     by simp
    62   then show "(s * w + t * x) + (u * z + v * y) = (u * y + v * z) + (s * x + t * w)"
    63     by (simp add: algebra_simps)
    64 qed
    65 
    66 instance
    67   by standard (transfer; clarsimp simp: algebra_simps)+
    68 
    69 end
    70 
    71 abbreviation int :: "nat \<Rightarrow> int"
    72   where "int \<equiv> of_nat"
    73 
    74 lemma int_def: "int n = Abs_Integ (n, 0)"
    75   by (induct n) (simp add: zero_int.abs_eq, simp add: one_int.abs_eq plus_int.abs_eq)
    76 
    77 lemma int_transfer [transfer_rule]: "(rel_fun (op =) pcr_int) (\<lambda>n. (n, 0)) int"
    78   by (simp add: rel_fun_def int.pcr_cr_eq cr_int_def int_def)
    79 
    80 lemma int_diff_cases: obtains (diff) m n where "z = int m - int n"
    81   by transfer clarsimp
    82 
    83 
    84 subsection \<open>Integers are totally ordered\<close>
    85 
    86 instantiation int :: linorder
    87 begin
    88 
    89 lift_definition less_eq_int :: "int \<Rightarrow> int \<Rightarrow> bool"
    90   is "\<lambda>(x, y) (u, v). x + v \<le> u + y"
    91   by auto
    92 
    93 lift_definition less_int :: "int \<Rightarrow> int \<Rightarrow> bool"
    94   is "\<lambda>(x, y) (u, v). x + v < u + y"
    95   by auto
    96 
    97 instance
    98   by standard (transfer, force)+
    99 
   100 end
   101 
   102 instantiation int :: distrib_lattice
   103 begin
   104 
   105 definition "(inf :: int \<Rightarrow> int \<Rightarrow> int) = min"
   106 
   107 definition "(sup :: int \<Rightarrow> int \<Rightarrow> int) = max"
   108 
   109 instance
   110   by standard (auto simp add: inf_int_def sup_int_def max_min_distrib2)
   111 
   112 end
   113 
   114 
   115 subsection \<open>Ordering properties of arithmetic operations\<close>
   116 
   117 instance int :: ordered_cancel_ab_semigroup_add
   118 proof
   119   fix i j k :: int
   120   show "i \<le> j \<Longrightarrow> k + i \<le> k + j"
   121     by transfer clarsimp
   122 qed
   123 
   124 text \<open>Strict Monotonicity of Multiplication.\<close>
   125 
   126 text \<open>Strict, in 1st argument; proof is by induction on \<open>k > 0\<close>.\<close>
   127 lemma zmult_zless_mono2_lemma: "i < j \<Longrightarrow> 0 < k \<Longrightarrow> int k * i < int k * j"
   128   for i j :: int
   129 proof (induct k)
   130   case 0
   131   then show ?case by simp
   132 next
   133   case (Suc k)
   134   then show ?case
   135     by (cases "k = 0") (simp_all add: distrib_right add_strict_mono)
   136 qed
   137 
   138 lemma zero_le_imp_eq_int: "0 \<le> k \<Longrightarrow> \<exists>n. k = int n"
   139   for k :: int
   140   apply transfer
   141   apply clarsimp
   142   apply (rule_tac x="a - b" in exI)
   143   apply simp
   144   done
   145 
   146 lemma zero_less_imp_eq_int: "0 < k \<Longrightarrow> \<exists>n>0. k = int n"
   147   for k :: int
   148   apply transfer
   149   apply clarsimp
   150   apply (rule_tac x="a - b" in exI)
   151   apply simp
   152   done
   153 
   154 lemma zmult_zless_mono2: "i < j \<Longrightarrow> 0 < k \<Longrightarrow> k * i < k * j"
   155   for i j k :: int
   156   by (drule zero_less_imp_eq_int) (auto simp add: zmult_zless_mono2_lemma)
   157 
   158 
   159 text \<open>The integers form an ordered integral domain.\<close>
   160 
   161 instantiation int :: linordered_idom
   162 begin
   163 
   164 definition zabs_def: "\<bar>i::int\<bar> = (if i < 0 then - i else i)"
   165 
   166 definition zsgn_def: "sgn (i::int) = (if i = 0 then 0 else if 0 < i then 1 else - 1)"
   167 
   168 instance
   169 proof
   170   fix i j k :: int
   171   show "i < j \<Longrightarrow> 0 < k \<Longrightarrow> k * i < k * j"
   172     by (rule zmult_zless_mono2)
   173   show "\<bar>i\<bar> = (if i < 0 then -i else i)"
   174     by (simp only: zabs_def)
   175   show "sgn (i::int) = (if i=0 then 0 else if 0<i then 1 else - 1)"
   176     by (simp only: zsgn_def)
   177 qed
   178 
   179 end
   180 
   181 lemma zless_imp_add1_zle: "w < z \<Longrightarrow> w + 1 \<le> z"
   182   for w z :: int
   183   by transfer clarsimp
   184 
   185 lemma zless_iff_Suc_zadd: "w < z \<longleftrightarrow> (\<exists>n. z = w + int (Suc n))"
   186   for w z :: int
   187   apply transfer
   188   apply auto
   189   apply (rename_tac a b c d)
   190   apply (rule_tac x="c+b - Suc(a+d)" in exI)
   191   apply arith
   192   done
   193 
   194 lemma zabs_less_one_iff [simp]: "\<bar>z\<bar> < 1 \<longleftrightarrow> z = 0" (is "?lhs \<longleftrightarrow> ?rhs")
   195   for z :: int
   196 proof
   197   assume ?rhs
   198   then show ?lhs by simp
   199 next
   200   assume ?lhs
   201   with zless_imp_add1_zle [of "\<bar>z\<bar>" 1] have "\<bar>z\<bar> + 1 \<le> 1" by simp
   202   then have "\<bar>z\<bar> \<le> 0" by simp
   203   then show ?rhs by simp
   204 qed
   205 
   206 lemmas int_distrib =
   207   distrib_right [of z1 z2 w]
   208   distrib_left [of w z1 z2]
   209   left_diff_distrib [of z1 z2 w]
   210   right_diff_distrib [of w z1 z2]
   211   for z1 z2 w :: int
   212 
   213 
   214 subsection \<open>Embedding of the Integers into any \<open>ring_1\<close>: \<open>of_int\<close>\<close>
   215 
   216 context ring_1
   217 begin
   218 
   219 lift_definition of_int :: "int \<Rightarrow> 'a"
   220   is "\<lambda>(i, j). of_nat i - of_nat j"
   221   by (clarsimp simp add: diff_eq_eq eq_diff_eq diff_add_eq
   222       of_nat_add [symmetric] simp del: of_nat_add)
   223 
   224 lemma of_int_0 [simp]: "of_int 0 = 0"
   225   by transfer simp
   226 
   227 lemma of_int_1 [simp]: "of_int 1 = 1"
   228   by transfer simp
   229 
   230 lemma of_int_add [simp]: "of_int (w + z) = of_int w + of_int z"
   231   by transfer (clarsimp simp add: algebra_simps)
   232 
   233 lemma of_int_minus [simp]: "of_int (- z) = - (of_int z)"
   234   by (transfer fixing: uminus) clarsimp
   235 
   236 lemma of_int_diff [simp]: "of_int (w - z) = of_int w - of_int z"
   237   using of_int_add [of w "- z"] by simp
   238 
   239 lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z"
   240   by (transfer fixing: times) (clarsimp simp add: algebra_simps)
   241 
   242 lemma mult_of_int_commute: "of_int x * y = y * of_int x"
   243   by (transfer fixing: times) (auto simp: algebra_simps mult_of_nat_commute)
   244 
   245 text \<open>Collapse nested embeddings.\<close>
   246 lemma of_int_of_nat_eq [simp]: "of_int (int n) = of_nat n"
   247   by (induct n) auto
   248 
   249 lemma of_int_numeral [simp, code_post]: "of_int (numeral k) = numeral k"
   250   by (simp add: of_nat_numeral [symmetric] of_int_of_nat_eq [symmetric])
   251 
   252 lemma of_int_neg_numeral [code_post]: "of_int (- numeral k) = - numeral k"
   253   by simp
   254 
   255 lemma of_int_power [simp]: "of_int (z ^ n) = of_int z ^ n"
   256   by (induct n) simp_all
   257 
   258 lemma of_int_of_bool [simp]:
   259   "of_int (of_bool P) = of_bool P"
   260   by auto
   261 
   262 end
   263 
   264 context ring_char_0
   265 begin
   266 
   267 lemma of_int_eq_iff [simp]: "of_int w = of_int z \<longleftrightarrow> w = z"
   268   by transfer (clarsimp simp add: algebra_simps of_nat_add [symmetric] simp del: of_nat_add)
   269 
   270 text \<open>Special cases where either operand is zero.\<close>
   271 lemma of_int_eq_0_iff [simp]: "of_int z = 0 \<longleftrightarrow> z = 0"
   272   using of_int_eq_iff [of z 0] by simp
   273 
   274 lemma of_int_0_eq_iff [simp]: "0 = of_int z \<longleftrightarrow> z = 0"
   275   using of_int_eq_iff [of 0 z] by simp
   276 
   277 lemma of_int_eq_1_iff [iff]: "of_int z = 1 \<longleftrightarrow> z = 1"
   278   using of_int_eq_iff [of z 1] by simp
   279 
   280 end
   281 
   282 context linordered_idom
   283 begin
   284 
   285 text \<open>Every \<open>linordered_idom\<close> has characteristic zero.\<close>
   286 subclass ring_char_0 ..
   287 
   288 lemma of_int_le_iff [simp]: "of_int w \<le> of_int z \<longleftrightarrow> w \<le> z"
   289   by (transfer fixing: less_eq)
   290     (clarsimp simp add: algebra_simps of_nat_add [symmetric] simp del: of_nat_add)
   291 
   292 lemma of_int_less_iff [simp]: "of_int w < of_int z \<longleftrightarrow> w < z"
   293   by (simp add: less_le order_less_le)
   294 
   295 lemma of_int_0_le_iff [simp]: "0 \<le> of_int z \<longleftrightarrow> 0 \<le> z"
   296   using of_int_le_iff [of 0 z] by simp
   297 
   298 lemma of_int_le_0_iff [simp]: "of_int z \<le> 0 \<longleftrightarrow> z \<le> 0"
   299   using of_int_le_iff [of z 0] by simp
   300 
   301 lemma of_int_0_less_iff [simp]: "0 < of_int z \<longleftrightarrow> 0 < z"
   302   using of_int_less_iff [of 0 z] by simp
   303 
   304 lemma of_int_less_0_iff [simp]: "of_int z < 0 \<longleftrightarrow> z < 0"
   305   using of_int_less_iff [of z 0] by simp
   306 
   307 lemma of_int_1_le_iff [simp]: "1 \<le> of_int z \<longleftrightarrow> 1 \<le> z"
   308   using of_int_le_iff [of 1 z] by simp
   309 
   310 lemma of_int_le_1_iff [simp]: "of_int z \<le> 1 \<longleftrightarrow> z \<le> 1"
   311   using of_int_le_iff [of z 1] by simp
   312 
   313 lemma of_int_1_less_iff [simp]: "1 < of_int z \<longleftrightarrow> 1 < z"
   314   using of_int_less_iff [of 1 z] by simp
   315 
   316 lemma of_int_less_1_iff [simp]: "of_int z < 1 \<longleftrightarrow> z < 1"
   317   using of_int_less_iff [of z 1] by simp
   318 
   319 lemma of_int_pos: "z > 0 \<Longrightarrow> of_int z > 0"
   320   by simp
   321 
   322 lemma of_int_nonneg: "z \<ge> 0 \<Longrightarrow> of_int z \<ge> 0"
   323   by simp
   324 
   325 lemma of_int_abs [simp]: "of_int \<bar>x\<bar> = \<bar>of_int x\<bar>"
   326   by (auto simp add: abs_if)
   327 
   328 lemma of_int_lessD:
   329   assumes "\<bar>of_int n\<bar> < x"
   330   shows "n = 0 \<or> x > 1"
   331 proof (cases "n = 0")
   332   case True
   333   then show ?thesis by simp
   334 next
   335   case False
   336   then have "\<bar>n\<bar> \<noteq> 0" by simp
   337   then have "\<bar>n\<bar> > 0" by simp
   338   then have "\<bar>n\<bar> \<ge> 1"
   339     using zless_imp_add1_zle [of 0 "\<bar>n\<bar>"] by simp
   340   then have "\<bar>of_int n\<bar> \<ge> 1"
   341     unfolding of_int_1_le_iff [of "\<bar>n\<bar>", symmetric] by simp
   342   then have "1 < x" using assms by (rule le_less_trans)
   343   then show ?thesis ..
   344 qed
   345 
   346 lemma of_int_leD:
   347   assumes "\<bar>of_int n\<bar> \<le> x"
   348   shows "n = 0 \<or> 1 \<le> x"
   349 proof (cases "n = 0")
   350   case True
   351   then show ?thesis by simp
   352 next
   353   case False
   354   then have "\<bar>n\<bar> \<noteq> 0" by simp
   355   then have "\<bar>n\<bar> > 0" by simp
   356   then have "\<bar>n\<bar> \<ge> 1"
   357     using zless_imp_add1_zle [of 0 "\<bar>n\<bar>"] by simp
   358   then have "\<bar>of_int n\<bar> \<ge> 1"
   359     unfolding of_int_1_le_iff [of "\<bar>n\<bar>", symmetric] by simp
   360   then have "1 \<le> x" using assms by (rule order_trans)
   361   then show ?thesis ..
   362 qed
   363 
   364 end
   365 
   366 text \<open>Comparisons involving @{term of_int}.\<close>
   367 
   368 lemma of_int_eq_numeral_iff [iff]: "of_int z = (numeral n :: 'a::ring_char_0) \<longleftrightarrow> z = numeral n"
   369   using of_int_eq_iff by fastforce
   370 
   371 lemma of_int_le_numeral_iff [simp]:
   372   "of_int z \<le> (numeral n :: 'a::linordered_idom) \<longleftrightarrow> z \<le> numeral n"
   373   using of_int_le_iff [of z "numeral n"] by simp
   374 
   375 lemma of_int_numeral_le_iff [simp]:
   376   "(numeral n :: 'a::linordered_idom) \<le> of_int z \<longleftrightarrow> numeral n \<le> z"
   377   using of_int_le_iff [of "numeral n"] by simp
   378 
   379 lemma of_int_less_numeral_iff [simp]:
   380   "of_int z < (numeral n :: 'a::linordered_idom) \<longleftrightarrow> z < numeral n"
   381   using of_int_less_iff [of z "numeral n"] by simp
   382 
   383 lemma of_int_numeral_less_iff [simp]:
   384   "(numeral n :: 'a::linordered_idom) < of_int z \<longleftrightarrow> numeral n < z"
   385   using of_int_less_iff [of "numeral n" z] by simp
   386 
   387 lemma of_nat_less_of_int_iff: "(of_nat n::'a::linordered_idom) < of_int x \<longleftrightarrow> int n < x"
   388   by (metis of_int_of_nat_eq of_int_less_iff)
   389 
   390 lemma of_int_eq_id [simp]: "of_int = id"
   391 proof
   392   show "of_int z = id z" for z
   393     by (cases z rule: int_diff_cases) simp
   394 qed
   395 
   396 instance int :: no_top
   397   apply standard
   398   apply (rule_tac x="x + 1" in exI)
   399   apply simp
   400   done
   401 
   402 instance int :: no_bot
   403   apply standard
   404   apply (rule_tac x="x - 1" in exI)
   405   apply simp
   406   done
   407 
   408 
   409 subsection \<open>Magnitude of an Integer, as a Natural Number: \<open>nat\<close>\<close>
   410 
   411 lift_definition nat :: "int \<Rightarrow> nat" is "\<lambda>(x, y). x - y"
   412   by auto
   413 
   414 lemma nat_int [simp]: "nat (int n) = n"
   415   by transfer simp
   416 
   417 lemma int_nat_eq [simp]: "int (nat z) = (if 0 \<le> z then z else 0)"
   418   by transfer clarsimp
   419 
   420 lemma nat_0_le: "0 \<le> z \<Longrightarrow> int (nat z) = z"
   421   by simp
   422 
   423 lemma nat_le_0 [simp]: "z \<le> 0 \<Longrightarrow> nat z = 0"
   424   by transfer clarsimp
   425 
   426 lemma nat_le_eq_zle: "0 < w \<or> 0 \<le> z \<Longrightarrow> nat w \<le> nat z \<longleftrightarrow> w \<le> z"
   427   by transfer (clarsimp, arith)
   428 
   429 text \<open>An alternative condition is @{term "0 \<le> w"}.\<close>
   430 lemma nat_mono_iff: "0 < z \<Longrightarrow> nat w < nat z \<longleftrightarrow> w < z"
   431   by (simp add: nat_le_eq_zle linorder_not_le [symmetric])
   432 
   433 lemma nat_less_eq_zless: "0 \<le> w \<Longrightarrow> nat w < nat z \<longleftrightarrow> w < z"
   434   by (simp add: nat_le_eq_zle linorder_not_le [symmetric])
   435 
   436 lemma zless_nat_conj [simp]: "nat w < nat z \<longleftrightarrow> 0 < z \<and> w < z"
   437   by transfer (clarsimp, arith)
   438 
   439 lemma nonneg_int_cases:
   440   assumes "0 \<le> k"
   441   obtains n where "k = int n"
   442 proof -
   443   from assms have "k = int (nat k)"
   444     by simp
   445   then show thesis
   446     by (rule that)
   447 qed
   448 
   449 lemma pos_int_cases:
   450   assumes "0 < k"
   451   obtains n where "k = int n" and "n > 0"
   452 proof -
   453   from assms have "0 \<le> k"
   454     by simp
   455   then obtain n where "k = int n"
   456     by (rule nonneg_int_cases)
   457   moreover have "n > 0"
   458     using \<open>k = int n\<close> assms by simp
   459   ultimately show thesis
   460     by (rule that)
   461 qed
   462 
   463 lemma nonpos_int_cases:
   464   assumes "k \<le> 0"
   465   obtains n where "k = - int n"
   466 proof -
   467   from assms have "- k \<ge> 0"
   468     by simp
   469   then obtain n where "- k = int n"
   470     by (rule nonneg_int_cases)
   471   then have "k = - int n"
   472     by simp
   473   then show thesis
   474     by (rule that)
   475 qed
   476 
   477 lemma neg_int_cases:
   478   assumes "k < 0"
   479   obtains n where "k = - int n" and "n > 0"
   480 proof -
   481   from assms have "- k > 0"
   482     by simp
   483   then obtain n where "- k = int n" and "- k > 0"
   484     by (blast elim: pos_int_cases)
   485   then have "k = - int n" and "n > 0"
   486     by simp_all
   487   then show thesis
   488     by (rule that)
   489 qed
   490 
   491 lemma nat_eq_iff: "nat w = m \<longleftrightarrow> (if 0 \<le> w then w = int m else m = 0)"
   492   by transfer (clarsimp simp add: le_imp_diff_is_add)
   493 
   494 lemma nat_eq_iff2: "m = nat w \<longleftrightarrow> (if 0 \<le> w then w = int m else m = 0)"
   495   using nat_eq_iff [of w m] by auto
   496 
   497 lemma nat_0 [simp]: "nat 0 = 0"
   498   by (simp add: nat_eq_iff)
   499 
   500 lemma nat_1 [simp]: "nat 1 = Suc 0"
   501   by (simp add: nat_eq_iff)
   502 
   503 lemma nat_numeral [simp]: "nat (numeral k) = numeral k"
   504   by (simp add: nat_eq_iff)
   505 
   506 lemma nat_neg_numeral [simp]: "nat (- numeral k) = 0"
   507   by simp
   508 
   509 lemma nat_2: "nat 2 = Suc (Suc 0)"
   510   by simp
   511 
   512 lemma nat_less_iff: "0 \<le> w \<Longrightarrow> nat w < m \<longleftrightarrow> w < of_nat m"
   513   by transfer (clarsimp, arith)
   514 
   515 lemma nat_le_iff: "nat x \<le> n \<longleftrightarrow> x \<le> int n"
   516   by transfer (clarsimp simp add: le_diff_conv)
   517 
   518 lemma nat_mono: "x \<le> y \<Longrightarrow> nat x \<le> nat y"
   519   by transfer auto
   520 
   521 lemma nat_0_iff[simp]: "nat i = 0 \<longleftrightarrow> i \<le> 0"
   522   for i :: int
   523   by transfer clarsimp
   524 
   525 lemma int_eq_iff: "of_nat m = z \<longleftrightarrow> m = nat z \<and> 0 \<le> z"
   526   by (auto simp add: nat_eq_iff2)
   527 
   528 lemma zero_less_nat_eq [simp]: "0 < nat z \<longleftrightarrow> 0 < z"
   529   using zless_nat_conj [of 0] by auto
   530 
   531 lemma nat_add_distrib: "0 \<le> z \<Longrightarrow> 0 \<le> z' \<Longrightarrow> nat (z + z') = nat z + nat z'"
   532   by transfer clarsimp
   533 
   534 lemma nat_diff_distrib': "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> nat (x - y) = nat x - nat y"
   535   by transfer clarsimp
   536 
   537 lemma nat_diff_distrib: "0 \<le> z' \<Longrightarrow> z' \<le> z \<Longrightarrow> nat (z - z') = nat z - nat z'"
   538   by (rule nat_diff_distrib') auto
   539 
   540 lemma nat_zminus_int [simp]: "nat (- int n) = 0"
   541   by transfer simp
   542 
   543 lemma le_nat_iff: "k \<ge> 0 \<Longrightarrow> n \<le> nat k \<longleftrightarrow> int n \<le> k"
   544   by transfer auto
   545 
   546 lemma zless_nat_eq_int_zless: "m < nat z \<longleftrightarrow> int m < z"
   547   by transfer (clarsimp simp add: less_diff_conv)
   548 
   549 lemma (in ring_1) of_nat_nat [simp]: "0 \<le> z \<Longrightarrow> of_nat (nat z) = of_int z"
   550   by transfer (clarsimp simp add: of_nat_diff)
   551 
   552 lemma diff_nat_numeral [simp]: "(numeral v :: nat) - numeral v' = nat (numeral v - numeral v')"
   553   by (simp only: nat_diff_distrib' zero_le_numeral nat_numeral)
   554 
   555 lemma nat_of_bool [simp]:
   556   "nat (of_bool P) = of_bool P"
   557   by auto
   558 
   559 lemma split_nat [arith_split]: "P (nat i) \<longleftrightarrow> ((\<forall>n. i = int n \<longrightarrow> P n) \<and> (i < 0 \<longrightarrow> P 0))"
   560   (is "?P = (?L \<and> ?R)")
   561   for i :: int
   562 proof (cases "i < 0")
   563   case True
   564   then show ?thesis
   565     by auto
   566 next
   567   case False
   568   have "?P = ?L"
   569   proof
   570     assume ?P
   571     then show ?L using False by auto
   572   next
   573     assume ?L
   574     moreover from False have "int (nat i) = i"
   575       by (simp add: not_less)
   576     ultimately show ?P
   577       by simp
   578   qed
   579   with False show ?thesis by simp
   580 qed
   581 
   582 lemma all_nat: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x\<ge>0. P (nat x))"
   583   by (auto split: split_nat)
   584 
   585 lemma ex_nat: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. 0 \<le> x \<and> P (nat x))"
   586 proof
   587   assume "\<exists>x. P x"
   588   then obtain x where "P x" ..
   589   then have "int x \<ge> 0 \<and> P (nat (int x))" by simp
   590   then show "\<exists>x\<ge>0. P (nat x)" ..
   591 next
   592   assume "\<exists>x\<ge>0. P (nat x)"
   593   then show "\<exists>x. P x" by auto
   594 qed
   595 
   596 
   597 text \<open>For termination proofs:\<close>
   598 lemma measure_function_int[measure_function]: "is_measure (nat \<circ> abs)" ..
   599 
   600 
   601 subsection \<open>Lemmas about the Function @{term of_nat} and Orderings\<close>
   602 
   603 lemma negative_zless_0: "- (int (Suc n)) < (0 :: int)"
   604   by (simp add: order_less_le del: of_nat_Suc)
   605 
   606 lemma negative_zless [iff]: "- (int (Suc n)) < int m"
   607   by (rule negative_zless_0 [THEN order_less_le_trans], simp)
   608 
   609 lemma negative_zle_0: "- int n \<le> 0"
   610   by (simp add: minus_le_iff)
   611 
   612 lemma negative_zle [iff]: "- int n \<le> int m"
   613   by (rule order_trans [OF negative_zle_0 of_nat_0_le_iff])
   614 
   615 lemma not_zle_0_negative [simp]: "\<not> 0 \<le> - int (Suc n)"
   616   by (subst le_minus_iff) (simp del: of_nat_Suc)
   617 
   618 lemma int_zle_neg: "int n \<le> - int m \<longleftrightarrow> n = 0 \<and> m = 0"
   619   by transfer simp
   620 
   621 lemma not_int_zless_negative [simp]: "\<not> int n < - int m"
   622   by (simp add: linorder_not_less)
   623 
   624 lemma negative_eq_positive [simp]: "- int n = of_nat m \<longleftrightarrow> n = 0 \<and> m = 0"
   625   by (force simp add: order_eq_iff [of "- of_nat n"] int_zle_neg)
   626 
   627 lemma zle_iff_zadd: "w \<le> z \<longleftrightarrow> (\<exists>n. z = w + int n)"
   628   (is "?lhs \<longleftrightarrow> ?rhs")
   629 proof
   630   assume ?rhs
   631   then show ?lhs by auto
   632 next
   633   assume ?lhs
   634   then have "0 \<le> z - w" by simp
   635   then obtain n where "z - w = int n"
   636     using zero_le_imp_eq_int [of "z - w"] by blast
   637   then have "z = w + int n" by simp
   638   then show ?rhs ..
   639 qed
   640 
   641 lemma zadd_int_left: "int m + (int n + z) = int (m + n) + z"
   642   by simp
   643 
   644 text \<open>
   645   This version is proved for all ordered rings, not just integers!
   646   It is proved here because attribute \<open>arith_split\<close> is not available
   647   in theory \<open>Rings\<close>.
   648   But is it really better than just rewriting with \<open>abs_if\<close>?
   649 \<close>
   650 lemma abs_split [arith_split, no_atp]: "P \<bar>a\<bar> \<longleftrightarrow> (0 \<le> a \<longrightarrow> P a) \<and> (a < 0 \<longrightarrow> P (- a))"
   651   for a :: "'a::linordered_idom"
   652   by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
   653 
   654 lemma negD: "x < 0 \<Longrightarrow> \<exists>n. x = - (int (Suc n))"
   655   apply transfer
   656   apply clarsimp
   657   apply (rule_tac x="b - Suc a" in exI)
   658   apply arith
   659   done
   660 
   661 
   662 subsection \<open>Cases and induction\<close>
   663 
   664 text \<open>
   665   Now we replace the case analysis rule by a more conventional one:
   666   whether an integer is negative or not.
   667 \<close>
   668 
   669 text \<open>This version is symmetric in the two subgoals.\<close>
   670 lemma int_cases2 [case_names nonneg nonpos, cases type: int]:
   671   "(\<And>n. z = int n \<Longrightarrow> P) \<Longrightarrow> (\<And>n. z = - (int n) \<Longrightarrow> P) \<Longrightarrow> P"
   672   by (cases "z < 0") (auto simp add: linorder_not_less dest!: negD nat_0_le [THEN sym])
   673 
   674 text \<open>This is the default, with a negative case.\<close>
   675 lemma int_cases [case_names nonneg neg, cases type: int]:
   676   "(\<And>n. z = int n \<Longrightarrow> P) \<Longrightarrow> (\<And>n. z = - (int (Suc n)) \<Longrightarrow> P) \<Longrightarrow> P"
   677   apply (cases "z < 0")
   678    apply (blast dest!: negD)
   679   apply (simp add: linorder_not_less del: of_nat_Suc)
   680   apply auto
   681   apply (blast dest: nat_0_le [THEN sym])
   682   done
   683 
   684 lemma int_cases3 [case_names zero pos neg]:
   685   fixes k :: int
   686   assumes "k = 0 \<Longrightarrow> P" and "\<And>n. k = int n \<Longrightarrow> n > 0 \<Longrightarrow> P"
   687     and "\<And>n. k = - int n \<Longrightarrow> n > 0 \<Longrightarrow> P"
   688   shows "P"
   689 proof (cases k "0::int" rule: linorder_cases)
   690   case equal
   691   with assms(1) show P by simp
   692 next
   693   case greater
   694   then have *: "nat k > 0" by simp
   695   moreover from * have "k = int (nat k)" by auto
   696   ultimately show P using assms(2) by blast
   697 next
   698   case less
   699   then have *: "nat (- k) > 0" by simp
   700   moreover from * have "k = - int (nat (- k))" by auto
   701   ultimately show P using assms(3) by blast
   702 qed
   703 
   704 lemma int_of_nat_induct [case_names nonneg neg, induct type: int]:
   705   "(\<And>n. P (int n)) \<Longrightarrow> (\<And>n. P (- (int (Suc n)))) \<Longrightarrow> P z"
   706   by (cases z) auto
   707 
   708 lemma Let_numeral [simp]: "Let (numeral v) f = f (numeral v)"
   709   \<comment> \<open>Unfold all \<open>let\<close>s involving constants\<close>
   710   by (fact Let_numeral) \<comment> \<open>FIXME drop\<close>
   711 
   712 lemma Let_neg_numeral [simp]: "Let (- numeral v) f = f (- numeral v)"
   713   \<comment> \<open>Unfold all \<open>let\<close>s involving constants\<close>
   714   by (fact Let_neg_numeral) \<comment> \<open>FIXME drop\<close>
   715 
   716 lemma sgn_mult_dvd_iff [simp]:
   717   "sgn r * l dvd k \<longleftrightarrow> l dvd k \<and> (r = 0 \<longrightarrow> k = 0)" for k l r :: int
   718   by (cases r rule: int_cases3) auto
   719 
   720 lemma mult_sgn_dvd_iff [simp]:
   721   "l * sgn r dvd k \<longleftrightarrow> l dvd k \<and> (r = 0 \<longrightarrow> k = 0)" for k l r :: int
   722   using sgn_mult_dvd_iff [of r l k] by (simp add: ac_simps)
   723 
   724 lemma dvd_sgn_mult_iff [simp]:
   725   "l dvd sgn r * k \<longleftrightarrow> l dvd k \<or> r = 0" for k l r :: int
   726   by (cases r rule: int_cases3) simp_all
   727 
   728 lemma dvd_mult_sgn_iff [simp]:
   729   "l dvd k * sgn r \<longleftrightarrow> l dvd k \<or> r = 0" for k l r :: int
   730   using dvd_sgn_mult_iff [of l r k] by (simp add: ac_simps)
   731 
   732 lemma int_sgnE:
   733   fixes k :: int
   734   obtains n and l where "k = sgn l * int n"
   735 proof -
   736   have "k = sgn k * int (nat \<bar>k\<bar>)"
   737     by (simp add: sgn_mult_abs)
   738   then show ?thesis ..
   739 qed
   740 
   741 text \<open>Unfold \<open>min\<close> and \<open>max\<close> on numerals.\<close>
   742 
   743 lemmas max_number_of [simp] =
   744   max_def [of "numeral u" "numeral v"]
   745   max_def [of "numeral u" "- numeral v"]
   746   max_def [of "- numeral u" "numeral v"]
   747   max_def [of "- numeral u" "- numeral v"] for u v
   748 
   749 lemmas min_number_of [simp] =
   750   min_def [of "numeral u" "numeral v"]
   751   min_def [of "numeral u" "- numeral v"]
   752   min_def [of "- numeral u" "numeral v"]
   753   min_def [of "- numeral u" "- numeral v"] for u v
   754 
   755 
   756 subsubsection \<open>Binary comparisons\<close>
   757 
   758 text \<open>Preliminaries\<close>
   759 
   760 lemma le_imp_0_less:
   761   fixes z :: int
   762   assumes le: "0 \<le> z"
   763   shows "0 < 1 + z"
   764 proof -
   765   have "0 \<le> z" by fact
   766   also have "\<dots> < z + 1" by (rule less_add_one)
   767   also have "\<dots> = 1 + z" by (simp add: ac_simps)
   768   finally show "0 < 1 + z" .
   769 qed
   770 
   771 lemma odd_less_0_iff: "1 + z + z < 0 \<longleftrightarrow> z < 0"
   772   for z :: int
   773 proof (cases z)
   774   case (nonneg n)
   775   then show ?thesis
   776     by (simp add: linorder_not_less add.assoc add_increasing le_imp_0_less [THEN order_less_imp_le])
   777 next
   778   case (neg n)
   779   then show ?thesis
   780     by (simp del: of_nat_Suc of_nat_add of_nat_1
   781         add: algebra_simps of_nat_1 [where 'a=int, symmetric] of_nat_add [symmetric])
   782 qed
   783 
   784 
   785 subsubsection \<open>Comparisons, for Ordered Rings\<close>
   786 
   787 lemmas double_eq_0_iff = double_zero
   788 
   789 lemma odd_nonzero: "1 + z + z \<noteq> 0"
   790   for z :: int
   791 proof (cases z)
   792   case (nonneg n)
   793   have le: "0 \<le> z + z"
   794     by (simp add: nonneg add_increasing)
   795   then show ?thesis
   796     using  le_imp_0_less [OF le] by (auto simp: add.assoc)
   797 next
   798   case (neg n)
   799   show ?thesis
   800   proof
   801     assume eq: "1 + z + z = 0"
   802     have "0 < 1 + (int n + int n)"
   803       by (simp add: le_imp_0_less add_increasing)
   804     also have "\<dots> = - (1 + z + z)"
   805       by (simp add: neg add.assoc [symmetric])
   806     also have "\<dots> = 0" by (simp add: eq)
   807     finally have "0<0" ..
   808     then show False by blast
   809   qed
   810 qed
   811 
   812 
   813 subsection \<open>The Set of Integers\<close>
   814 
   815 context ring_1
   816 begin
   817 
   818 definition Ints :: "'a set"  ("\<int>")
   819   where "\<int> = range of_int"
   820 
   821 lemma Ints_of_int [simp]: "of_int z \<in> \<int>"
   822   by (simp add: Ints_def)
   823 
   824 lemma Ints_of_nat [simp]: "of_nat n \<in> \<int>"
   825   using Ints_of_int [of "of_nat n"] by simp
   826 
   827 lemma Ints_0 [simp]: "0 \<in> \<int>"
   828   using Ints_of_int [of "0"] by simp
   829 
   830 lemma Ints_1 [simp]: "1 \<in> \<int>"
   831   using Ints_of_int [of "1"] by simp
   832 
   833 lemma Ints_numeral [simp]: "numeral n \<in> \<int>"
   834   by (subst of_nat_numeral [symmetric], rule Ints_of_nat)
   835 
   836 lemma Ints_add [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a + b \<in> \<int>"
   837   apply (auto simp add: Ints_def)
   838   apply (rule range_eqI)
   839   apply (rule of_int_add [symmetric])
   840   done
   841 
   842 lemma Ints_minus [simp]: "a \<in> \<int> \<Longrightarrow> -a \<in> \<int>"
   843   apply (auto simp add: Ints_def)
   844   apply (rule range_eqI)
   845   apply (rule of_int_minus [symmetric])
   846   done
   847 
   848 lemma Ints_diff [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a - b \<in> \<int>"
   849   apply (auto simp add: Ints_def)
   850   apply (rule range_eqI)
   851   apply (rule of_int_diff [symmetric])
   852   done
   853 
   854 lemma Ints_mult [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a * b \<in> \<int>"
   855   apply (auto simp add: Ints_def)
   856   apply (rule range_eqI)
   857   apply (rule of_int_mult [symmetric])
   858   done
   859 
   860 lemma Ints_power [simp]: "a \<in> \<int> \<Longrightarrow> a ^ n \<in> \<int>"
   861   by (induct n) simp_all
   862 
   863 lemma Ints_cases [cases set: Ints]:
   864   assumes "q \<in> \<int>"
   865   obtains (of_int) z where "q = of_int z"
   866   unfolding Ints_def
   867 proof -
   868   from \<open>q \<in> \<int>\<close> have "q \<in> range of_int" unfolding Ints_def .
   869   then obtain z where "q = of_int z" ..
   870   then show thesis ..
   871 qed
   872 
   873 lemma Ints_induct [case_names of_int, induct set: Ints]:
   874   "q \<in> \<int> \<Longrightarrow> (\<And>z. P (of_int z)) \<Longrightarrow> P q"
   875   by (rule Ints_cases) auto
   876 
   877 lemma Nats_subset_Ints: "\<nat> \<subseteq> \<int>"
   878   unfolding Nats_def Ints_def
   879   by (rule subsetI, elim imageE, hypsubst, subst of_int_of_nat_eq[symmetric], rule imageI) simp_all
   880 
   881 lemma Nats_altdef1: "\<nat> = {of_int n |n. n \<ge> 0}"
   882 proof (intro subsetI equalityI)
   883   fix x :: 'a
   884   assume "x \<in> {of_int n |n. n \<ge> 0}"
   885   then obtain n where "x = of_int n" "n \<ge> 0"
   886     by (auto elim!: Ints_cases)
   887   then have "x = of_nat (nat n)"
   888     by (subst of_nat_nat) simp_all
   889   then show "x \<in> \<nat>"
   890     by simp
   891 next
   892   fix x :: 'a
   893   assume "x \<in> \<nat>"
   894   then obtain n where "x = of_nat n"
   895     by (auto elim!: Nats_cases)
   896   then have "x = of_int (int n)" by simp
   897   also have "int n \<ge> 0" by simp
   898   then have "of_int (int n) \<in> {of_int n |n. n \<ge> 0}" by blast
   899   finally show "x \<in> {of_int n |n. n \<ge> 0}" .
   900 qed
   901 
   902 end
   903 
   904 lemma (in linordered_idom) Ints_abs [simp]:
   905   shows "a \<in> \<int> \<Longrightarrow> abs a \<in> \<int>"
   906   by (auto simp: abs_if)
   907 
   908 lemma (in linordered_idom) Nats_altdef2: "\<nat> = {n \<in> \<int>. n \<ge> 0}"
   909 proof (intro subsetI equalityI)
   910   fix x :: 'a
   911   assume "x \<in> {n \<in> \<int>. n \<ge> 0}"
   912   then obtain n where "x = of_int n" "n \<ge> 0"
   913     by (auto elim!: Ints_cases)
   914   then have "x = of_nat (nat n)"
   915     by (subst of_nat_nat) simp_all
   916   then show "x \<in> \<nat>"
   917     by simp
   918 qed (auto elim!: Nats_cases)
   919 
   920 lemma (in idom_divide) of_int_divide_in_Ints: 
   921   "of_int a div of_int b \<in> \<int>" if "b dvd a"
   922 proof -
   923   from that obtain c where "a = b * c" ..
   924   then show ?thesis
   925     by (cases "of_int b = 0") simp_all
   926 qed
   927 
   928 text \<open>The premise involving @{term Ints} prevents @{term "a = 1/2"}.\<close>
   929 
   930 lemma Ints_double_eq_0_iff:
   931   fixes a :: "'a::ring_char_0"
   932   assumes in_Ints: "a \<in> \<int>"
   933   shows "a + a = 0 \<longleftrightarrow> a = 0"
   934     (is "?lhs \<longleftrightarrow> ?rhs")
   935 proof -
   936   from in_Ints have "a \<in> range of_int"
   937     unfolding Ints_def [symmetric] .
   938   then obtain z where a: "a = of_int z" ..
   939   show ?thesis
   940   proof
   941     assume ?rhs
   942     then show ?lhs by simp
   943   next
   944     assume ?lhs
   945     with a have "of_int (z + z) = (of_int 0 :: 'a)" by simp
   946     then have "z + z = 0" by (simp only: of_int_eq_iff)
   947     then have "z = 0" by (simp only: double_eq_0_iff)
   948     with a show ?rhs by simp
   949   qed
   950 qed
   951 
   952 lemma Ints_odd_nonzero:
   953   fixes a :: "'a::ring_char_0"
   954   assumes in_Ints: "a \<in> \<int>"
   955   shows "1 + a + a \<noteq> 0"
   956 proof -
   957   from in_Ints have "a \<in> range of_int"
   958     unfolding Ints_def [symmetric] .
   959   then obtain z where a: "a = of_int z" ..
   960   show ?thesis
   961   proof
   962     assume "1 + a + a = 0"
   963     with a have "of_int (1 + z + z) = (of_int 0 :: 'a)" by simp
   964     then have "1 + z + z = 0" by (simp only: of_int_eq_iff)
   965     with odd_nonzero show False by blast
   966   qed
   967 qed
   968 
   969 lemma Nats_numeral [simp]: "numeral w \<in> \<nat>"
   970   using of_nat_in_Nats [of "numeral w"] by simp
   971 
   972 lemma Ints_odd_less_0:
   973   fixes a :: "'a::linordered_idom"
   974   assumes in_Ints: "a \<in> \<int>"
   975   shows "1 + a + a < 0 \<longleftrightarrow> a < 0"
   976 proof -
   977   from in_Ints have "a \<in> range of_int"
   978     unfolding Ints_def [symmetric] .
   979   then obtain z where a: "a = of_int z" ..
   980   with a have "1 + a + a < 0 \<longleftrightarrow> of_int (1 + z + z) < (of_int 0 :: 'a)"
   981     by simp
   982   also have "\<dots> \<longleftrightarrow> z < 0"
   983     by (simp only: of_int_less_iff odd_less_0_iff)
   984   also have "\<dots> \<longleftrightarrow> a < 0"
   985     by (simp add: a)
   986   finally show ?thesis .
   987 qed
   988 
   989 
   990 subsection \<open>@{term sum} and @{term prod}\<close>
   991 
   992 lemma of_nat_sum [simp]: "of_nat (sum f A) = (\<Sum>x\<in>A. of_nat(f x))"
   993   by (induct A rule: infinite_finite_induct) auto
   994 
   995 lemma of_int_sum [simp]: "of_int (sum f A) = (\<Sum>x\<in>A. of_int(f x))"
   996   by (induct A rule: infinite_finite_induct) auto
   997 
   998 lemma of_nat_prod [simp]: "of_nat (prod f A) = (\<Prod>x\<in>A. of_nat(f x))"
   999   by (induct A rule: infinite_finite_induct) auto
  1000 
  1001 lemma of_int_prod [simp]: "of_int (prod f A) = (\<Prod>x\<in>A. of_int(f x))"
  1002   by (induct A rule: infinite_finite_induct) auto
  1003 
  1004 
  1005 text \<open>Legacy theorems\<close>
  1006 
  1007 lemmas int_sum = of_nat_sum [where 'a=int]
  1008 lemmas int_prod = of_nat_prod [where 'a=int]
  1009 lemmas zle_int = of_nat_le_iff [where 'a=int]
  1010 lemmas int_int_eq = of_nat_eq_iff [where 'a=int]
  1011 lemmas nonneg_eq_int = nonneg_int_cases
  1012 
  1013 
  1014 subsection \<open>Setting up simplification procedures\<close>
  1015 
  1016 lemmas of_int_simps =
  1017   of_int_0 of_int_1 of_int_add of_int_mult
  1018 
  1019 ML_file "Tools/int_arith.ML"
  1020 declaration \<open>K Int_Arith.setup\<close>
  1021 
  1022 simproc_setup fast_arith
  1023   ("(m::'a::linordered_idom) < n" |
  1024     "(m::'a::linordered_idom) \<le> n" |
  1025     "(m::'a::linordered_idom) = n") =
  1026   \<open>K Lin_Arith.simproc\<close>
  1027 
  1028 
  1029 subsection\<open>More Inequality Reasoning\<close>
  1030 
  1031 lemma zless_add1_eq: "w < z + 1 \<longleftrightarrow> w < z \<or> w = z"
  1032   for w z :: int
  1033   by arith
  1034 
  1035 lemma add1_zle_eq: "w + 1 \<le> z \<longleftrightarrow> w < z"
  1036   for w z :: int
  1037   by arith
  1038 
  1039 lemma zle_diff1_eq [simp]: "w \<le> z - 1 \<longleftrightarrow> w < z"
  1040   for w z :: int
  1041   by arith
  1042 
  1043 lemma zle_add1_eq_le [simp]: "w < z + 1 \<longleftrightarrow> w \<le> z"
  1044   for w z :: int
  1045   by arith
  1046 
  1047 lemma int_one_le_iff_zero_less: "1 \<le> z \<longleftrightarrow> 0 < z"
  1048   for z :: int
  1049   by arith
  1050 
  1051 lemma Ints_nonzero_abs_ge1:
  1052   fixes x:: "'a :: linordered_idom"
  1053     assumes "x \<in> Ints" "x \<noteq> 0"
  1054     shows "1 \<le> abs x"
  1055 proof (rule Ints_cases [OF \<open>x \<in> Ints\<close>])
  1056   fix z::int
  1057   assume "x = of_int z"
  1058     with \<open>x \<noteq> 0\<close> 
  1059   show "1 \<le> \<bar>x\<bar>"
  1060     apply (auto simp add: abs_if)
  1061     by (metis diff_0 of_int_1 of_int_le_iff of_int_minus zle_diff1_eq)
  1062 qed
  1063   
  1064 lemma Ints_nonzero_abs_less1:
  1065   fixes x:: "'a :: linordered_idom"
  1066   shows "\<lbrakk>x \<in> Ints; abs x < 1\<rbrakk> \<Longrightarrow> x = 0"
  1067     using Ints_nonzero_abs_ge1 [of x] by auto
  1068     
  1069 
  1070 subsection \<open>The functions @{term nat} and @{term int}\<close>
  1071 
  1072 text \<open>Simplify the term @{term "w + - z"}.\<close>
  1073 
  1074 lemma one_less_nat_eq [simp]: "Suc 0 < nat z \<longleftrightarrow> 1 < z"
  1075   using zless_nat_conj [of 1 z] by auto
  1076 
  1077 text \<open>
  1078   This simplifies expressions of the form @{term "int n = z"} where
  1079   \<open>z\<close> is an integer literal.
  1080 \<close>
  1081 lemmas int_eq_iff_numeral [simp] = int_eq_iff [of _ "numeral v"] for v
  1082 
  1083 lemma nat_abs_int_diff: "nat \<bar>int a - int b\<bar> = (if a \<le> b then b - a else a - b)"
  1084   by auto
  1085 
  1086 lemma nat_int_add: "nat (int a + int b) = a + b"
  1087   by auto
  1088 
  1089 context ring_1
  1090 begin
  1091 
  1092 lemma of_int_of_nat [nitpick_simp]:
  1093   "of_int k = (if k < 0 then - of_nat (nat (- k)) else of_nat (nat k))"
  1094 proof (cases "k < 0")
  1095   case True
  1096   then have "0 \<le> - k" by simp
  1097   then have "of_nat (nat (- k)) = of_int (- k)" by (rule of_nat_nat)
  1098   with True show ?thesis by simp
  1099 next
  1100   case False
  1101   then show ?thesis by (simp add: not_less)
  1102 qed
  1103 
  1104 end
  1105 
  1106 lemma transfer_rule_of_int:
  1107   fixes R :: "'a::ring_1 \<Rightarrow> 'b::ring_1 \<Rightarrow> bool"
  1108   assumes [transfer_rule]: "R 0 0" "R 1 1"
  1109     "rel_fun R (rel_fun R R) plus plus"
  1110     "rel_fun R R uminus uminus"
  1111   shows "rel_fun HOL.eq R of_int of_int"
  1112 proof -
  1113   note transfer_rule_of_nat [transfer_rule]
  1114   have [transfer_rule]: "rel_fun HOL.eq R of_nat of_nat"
  1115     by transfer_prover
  1116   show ?thesis
  1117     by (unfold of_int_of_nat [abs_def]) transfer_prover
  1118 qed
  1119 
  1120 lemma nat_mult_distrib:
  1121   fixes z z' :: int
  1122   assumes "0 \<le> z"
  1123   shows "nat (z * z') = nat z * nat z'"
  1124 proof (cases "0 \<le> z'")
  1125   case False
  1126   with assms have "z * z' \<le> 0"
  1127     by (simp add: not_le mult_le_0_iff)
  1128   then have "nat (z * z') = 0" by simp
  1129   moreover from False have "nat z' = 0" by simp
  1130   ultimately show ?thesis by simp
  1131 next
  1132   case True
  1133   with assms have ge_0: "z * z' \<ge> 0" by (simp add: zero_le_mult_iff)
  1134   show ?thesis
  1135     by (rule injD [of "of_nat :: nat \<Rightarrow> int", OF inj_of_nat])
  1136       (simp only: of_nat_mult of_nat_nat [OF True]
  1137          of_nat_nat [OF assms] of_nat_nat [OF ge_0], simp)
  1138 qed
  1139 
  1140 lemma nat_mult_distrib_neg: "z \<le> 0 \<Longrightarrow> nat (z * z') = nat (- z) * nat (- z')"
  1141   for z z' :: int
  1142   apply (rule trans)
  1143    apply (rule_tac [2] nat_mult_distrib)
  1144    apply auto
  1145   done
  1146 
  1147 lemma nat_abs_mult_distrib: "nat \<bar>w * z\<bar> = nat \<bar>w\<bar> * nat \<bar>z\<bar>"
  1148   by (cases "z = 0 \<or> w = 0")
  1149     (auto simp add: abs_if nat_mult_distrib [symmetric]
  1150       nat_mult_distrib_neg [symmetric] mult_less_0_iff)
  1151 
  1152 lemma int_in_range_abs [simp]: "int n \<in> range abs"
  1153 proof (rule range_eqI)
  1154   show "int n = \<bar>int n\<bar>" by simp
  1155 qed
  1156 
  1157 lemma range_abs_Nats [simp]: "range abs = (\<nat> :: int set)"
  1158 proof -
  1159   have "\<bar>k\<bar> \<in> \<nat>" for k :: int
  1160     by (cases k) simp_all
  1161   moreover have "k \<in> range abs" if "k \<in> \<nat>" for k :: int
  1162     using that by induct simp
  1163   ultimately show ?thesis by blast
  1164 qed
  1165 
  1166 lemma Suc_nat_eq_nat_zadd1: "0 \<le> z \<Longrightarrow> Suc (nat z) = nat (1 + z)"
  1167   for z :: int
  1168   by (rule sym) (simp add: nat_eq_iff)
  1169 
  1170 lemma diff_nat_eq_if:
  1171   "nat z - nat z' =
  1172     (if z' < 0 then nat z
  1173      else
  1174       let d = z - z'
  1175       in if d < 0 then 0 else nat d)"
  1176   by (simp add: Let_def nat_diff_distrib [symmetric])
  1177 
  1178 lemma nat_numeral_diff_1 [simp]: "numeral v - (1::nat) = nat (numeral v - 1)"
  1179   using diff_nat_numeral [of v Num.One] by simp
  1180 
  1181 
  1182 subsection \<open>Induction principles for int\<close>
  1183 
  1184 text \<open>Well-founded segments of the integers.\<close>
  1185 
  1186 definition int_ge_less_than :: "int \<Rightarrow> (int \<times> int) set"
  1187   where "int_ge_less_than d = {(z', z). d \<le> z' \<and> z' < z}"
  1188 
  1189 lemma wf_int_ge_less_than: "wf (int_ge_less_than d)"
  1190 proof -
  1191   have "int_ge_less_than d \<subseteq> measure (\<lambda>z. nat (z - d))"
  1192     by (auto simp add: int_ge_less_than_def)
  1193   then show ?thesis
  1194     by (rule wf_subset [OF wf_measure])
  1195 qed
  1196 
  1197 text \<open>
  1198   This variant looks odd, but is typical of the relations suggested
  1199   by RankFinder.\<close>
  1200 
  1201 definition int_ge_less_than2 :: "int \<Rightarrow> (int \<times> int) set"
  1202   where "int_ge_less_than2 d = {(z',z). d \<le> z \<and> z' < z}"
  1203 
  1204 lemma wf_int_ge_less_than2: "wf (int_ge_less_than2 d)"
  1205 proof -
  1206   have "int_ge_less_than2 d \<subseteq> measure (\<lambda>z. nat (1 + z - d))"
  1207     by (auto simp add: int_ge_less_than2_def)
  1208   then show ?thesis
  1209     by (rule wf_subset [OF wf_measure])
  1210 qed
  1211 
  1212 (* `set:int': dummy construction *)
  1213 theorem int_ge_induct [case_names base step, induct set: int]:
  1214   fixes i :: int
  1215   assumes ge: "k \<le> i"
  1216     and base: "P k"
  1217     and step: "\<And>i. k \<le> i \<Longrightarrow> P i \<Longrightarrow> P (i + 1)"
  1218   shows "P i"
  1219 proof -
  1220   have "\<And>i::int. n = nat (i - k) \<Longrightarrow> k \<le> i \<Longrightarrow> P i" for n
  1221   proof (induct n)
  1222     case 0
  1223     then have "i = k" by arith
  1224     with base show "P i" by simp
  1225   next
  1226     case (Suc n)
  1227     then have "n = nat ((i - 1) - k)" by arith
  1228     moreover have k: "k \<le> i - 1" using Suc.prems by arith
  1229     ultimately have "P (i - 1)" by (rule Suc.hyps)
  1230     from step [OF k this] show ?case by simp
  1231   qed
  1232   with ge show ?thesis by fast
  1233 qed
  1234 
  1235 (* `set:int': dummy construction *)
  1236 theorem int_gr_induct [case_names base step, induct set: int]:
  1237   fixes i k :: int
  1238   assumes gr: "k < i"
  1239     and base: "P (k + 1)"
  1240     and step: "\<And>i. k < i \<Longrightarrow> P i \<Longrightarrow> P (i + 1)"
  1241   shows "P i"
  1242   apply (rule int_ge_induct[of "k + 1"])
  1243   using gr apply arith
  1244    apply (rule base)
  1245   apply (rule step)
  1246    apply simp_all
  1247   done
  1248 
  1249 theorem int_le_induct [consumes 1, case_names base step]:
  1250   fixes i k :: int
  1251   assumes le: "i \<le> k"
  1252     and base: "P k"
  1253     and step: "\<And>i. i \<le> k \<Longrightarrow> P i \<Longrightarrow> P (i - 1)"
  1254   shows "P i"
  1255 proof -
  1256   have "\<And>i::int. n = nat(k-i) \<Longrightarrow> i \<le> k \<Longrightarrow> P i" for n
  1257   proof (induct n)
  1258     case 0
  1259     then have "i = k" by arith
  1260     with base show "P i" by simp
  1261   next
  1262     case (Suc n)
  1263     then have "n = nat (k - (i + 1))" by arith
  1264     moreover have k: "i + 1 \<le> k" using Suc.prems by arith
  1265     ultimately have "P (i + 1)" by (rule Suc.hyps)
  1266     from step[OF k this] show ?case by simp
  1267   qed
  1268   with le show ?thesis by fast
  1269 qed
  1270 
  1271 theorem int_less_induct [consumes 1, case_names base step]:
  1272   fixes i k :: int
  1273   assumes less: "i < k"
  1274     and base: "P (k - 1)"
  1275     and step: "\<And>i. i < k \<Longrightarrow> P i \<Longrightarrow> P (i - 1)"
  1276   shows "P i"
  1277   apply (rule int_le_induct[of _ "k - 1"])
  1278   using less apply arith
  1279    apply (rule base)
  1280   apply (rule step)
  1281    apply simp_all
  1282   done
  1283 
  1284 theorem int_induct [case_names base step1 step2]:
  1285   fixes k :: int
  1286   assumes base: "P k"
  1287     and step1: "\<And>i. k \<le> i \<Longrightarrow> P i \<Longrightarrow> P (i + 1)"
  1288     and step2: "\<And>i. k \<ge> i \<Longrightarrow> P i \<Longrightarrow> P (i - 1)"
  1289   shows "P i"
  1290 proof -
  1291   have "i \<le> k \<or> i \<ge> k" by arith
  1292   then show ?thesis
  1293   proof
  1294     assume "i \<ge> k"
  1295     then show ?thesis
  1296       using base by (rule int_ge_induct) (fact step1)
  1297   next
  1298     assume "i \<le> k"
  1299     then show ?thesis
  1300       using base by (rule int_le_induct) (fact step2)
  1301   qed
  1302 qed
  1303 
  1304 
  1305 subsection \<open>Intermediate value theorems\<close>
  1306 
  1307 lemma int_val_lemma: "(\<forall>i<n. \<bar>f (i + 1) - f i\<bar> \<le> 1) \<longrightarrow> f 0 \<le> k \<longrightarrow> k \<le> f n \<longrightarrow> (\<exists>i \<le> n. f i = k)"
  1308   for n :: nat and k :: int
  1309   unfolding One_nat_def
  1310   apply (induct n)
  1311    apply simp
  1312   apply (intro strip)
  1313   apply (erule impE)
  1314    apply simp
  1315   apply (erule_tac x = n in allE)
  1316   apply simp
  1317   apply (case_tac "k = f (Suc n)")
  1318    apply force
  1319   apply (erule impE)
  1320    apply (simp add: abs_if split: if_split_asm)
  1321   apply (blast intro: le_SucI)
  1322   done
  1323 
  1324 lemmas nat0_intermed_int_val = int_val_lemma [rule_format (no_asm)]
  1325 
  1326 lemma nat_intermed_int_val:
  1327   "\<forall>i. m \<le> i \<and> i < n \<longrightarrow> \<bar>f (i + 1) - f i\<bar> \<le> 1 \<Longrightarrow> m < n \<Longrightarrow>
  1328     f m \<le> k \<Longrightarrow> k \<le> f n \<Longrightarrow> \<exists>i. m \<le> i \<and> i \<le> n \<and> f i = k"
  1329     for f :: "nat \<Rightarrow> int" and k :: int
  1330   apply (cut_tac n = "n-m" and f = "\<lambda>i. f (i + m)" and k = k in int_val_lemma)
  1331   unfolding One_nat_def
  1332   apply simp
  1333   apply (erule exE)
  1334   apply (rule_tac x = "i+m" in exI)
  1335   apply arith
  1336   done
  1337 
  1338 
  1339 subsection \<open>Products and 1, by T. M. Rasmussen\<close>
  1340 
  1341 lemma abs_zmult_eq_1:
  1342   fixes m n :: int
  1343   assumes mn: "\<bar>m * n\<bar> = 1"
  1344   shows "\<bar>m\<bar> = 1"
  1345 proof -
  1346   from mn have 0: "m \<noteq> 0" "n \<noteq> 0" by auto
  1347   have "\<not> 2 \<le> \<bar>m\<bar>"
  1348   proof
  1349     assume "2 \<le> \<bar>m\<bar>"
  1350     then have "2 * \<bar>n\<bar> \<le> \<bar>m\<bar> * \<bar>n\<bar>" by (simp add: mult_mono 0)
  1351     also have "\<dots> = \<bar>m * n\<bar>" by (simp add: abs_mult)
  1352     also from mn have "\<dots> = 1" by simp
  1353     finally have "2 * \<bar>n\<bar> \<le> 1" .
  1354     with 0 show "False" by arith
  1355   qed
  1356   with 0 show ?thesis by auto
  1357 qed
  1358 
  1359 lemma pos_zmult_eq_1_iff_lemma: "m * n = 1 \<Longrightarrow> m = 1 \<or> m = - 1"
  1360   for m n :: int
  1361   using abs_zmult_eq_1 [of m n] by arith
  1362 
  1363 lemma pos_zmult_eq_1_iff:
  1364   fixes m n :: int
  1365   assumes "0 < m"
  1366   shows "m * n = 1 \<longleftrightarrow> m = 1 \<and> n = 1"
  1367 proof -
  1368   from assms have "m * n = 1 \<Longrightarrow> m = 1"
  1369     by (auto dest: pos_zmult_eq_1_iff_lemma)
  1370   then show ?thesis
  1371     by (auto dest: pos_zmult_eq_1_iff_lemma)
  1372 qed
  1373 
  1374 lemma zmult_eq_1_iff: "m * n = 1 \<longleftrightarrow> (m = 1 \<and> n = 1) \<or> (m = - 1 \<and> n = - 1)"
  1375   for m n :: int
  1376   apply (rule iffI)
  1377    apply (frule pos_zmult_eq_1_iff_lemma)
  1378    apply (simp add: mult.commute [of m])
  1379    apply (frule pos_zmult_eq_1_iff_lemma)
  1380    apply auto
  1381   done
  1382 
  1383 lemma infinite_UNIV_int: "\<not> finite (UNIV::int set)"
  1384 proof
  1385   assume "finite (UNIV::int set)"
  1386   moreover have "inj (\<lambda>i::int. 2 * i)"
  1387     by (rule injI) simp
  1388   ultimately have "surj (\<lambda>i::int. 2 * i)"
  1389     by (rule finite_UNIV_inj_surj)
  1390   then obtain i :: int where "1 = 2 * i" by (rule surjE)
  1391   then show False by (simp add: pos_zmult_eq_1_iff)
  1392 qed
  1393 
  1394 
  1395 subsection \<open>Further theorems on numerals\<close>
  1396 
  1397 subsubsection \<open>Special Simplification for Constants\<close>
  1398 
  1399 text \<open>These distributive laws move literals inside sums and differences.\<close>
  1400 
  1401 lemmas distrib_right_numeral [simp] = distrib_right [of _ _ "numeral v"] for v
  1402 lemmas distrib_left_numeral [simp] = distrib_left [of "numeral v"] for v
  1403 lemmas left_diff_distrib_numeral [simp] = left_diff_distrib [of _ _ "numeral v"] for v
  1404 lemmas right_diff_distrib_numeral [simp] = right_diff_distrib [of "numeral v"] for v
  1405 
  1406 text \<open>These are actually for fields, like real: but where else to put them?\<close>
  1407 
  1408 lemmas zero_less_divide_iff_numeral [simp, no_atp] = zero_less_divide_iff [of "numeral w"] for w
  1409 lemmas divide_less_0_iff_numeral [simp, no_atp] = divide_less_0_iff [of "numeral w"] for w
  1410 lemmas zero_le_divide_iff_numeral [simp, no_atp] = zero_le_divide_iff [of "numeral w"] for w
  1411 lemmas divide_le_0_iff_numeral [simp, no_atp] = divide_le_0_iff [of "numeral w"] for w
  1412 
  1413 
  1414 text \<open>Replaces \<open>inverse #nn\<close> by \<open>1/#nn\<close>.  It looks
  1415   strange, but then other simprocs simplify the quotient.\<close>
  1416 
  1417 lemmas inverse_eq_divide_numeral [simp] =
  1418   inverse_eq_divide [of "numeral w"] for w
  1419 
  1420 lemmas inverse_eq_divide_neg_numeral [simp] =
  1421   inverse_eq_divide [of "- numeral w"] for w
  1422 
  1423 text \<open>These laws simplify inequalities, moving unary minus from a term
  1424   into the literal.\<close>
  1425 
  1426 lemmas equation_minus_iff_numeral [no_atp] =
  1427   equation_minus_iff [of "numeral v"] for v
  1428 
  1429 lemmas minus_equation_iff_numeral [no_atp] =
  1430   minus_equation_iff [of _ "numeral v"] for v
  1431 
  1432 lemmas le_minus_iff_numeral [no_atp] =
  1433   le_minus_iff [of "numeral v"] for v
  1434 
  1435 lemmas minus_le_iff_numeral [no_atp] =
  1436   minus_le_iff [of _ "numeral v"] for v
  1437 
  1438 lemmas less_minus_iff_numeral [no_atp] =
  1439   less_minus_iff [of "numeral v"] for v
  1440 
  1441 lemmas minus_less_iff_numeral [no_atp] =
  1442   minus_less_iff [of _ "numeral v"] for v
  1443 
  1444 (* FIXME maybe simproc *)
  1445 
  1446 
  1447 text \<open>Cancellation of constant factors in comparisons (\<open><\<close> and \<open>\<le>\<close>)\<close>
  1448 
  1449 lemmas mult_less_cancel_left_numeral [simp, no_atp] = mult_less_cancel_left [of "numeral v"] for v
  1450 lemmas mult_less_cancel_right_numeral [simp, no_atp] = mult_less_cancel_right [of _ "numeral v"] for v
  1451 lemmas mult_le_cancel_left_numeral [simp, no_atp] = mult_le_cancel_left [of "numeral v"] for v
  1452 lemmas mult_le_cancel_right_numeral [simp, no_atp] = mult_le_cancel_right [of _ "numeral v"] for v
  1453 
  1454 
  1455 text \<open>Multiplying out constant divisors in comparisons (\<open><\<close>, \<open>\<le>\<close> and \<open>=\<close>)\<close>
  1456 
  1457 named_theorems divide_const_simps "simplification rules to simplify comparisons involving constant divisors"
  1458 
  1459 lemmas le_divide_eq_numeral1 [simp,divide_const_simps] =
  1460   pos_le_divide_eq [of "numeral w", OF zero_less_numeral]
  1461   neg_le_divide_eq [of "- numeral w", OF neg_numeral_less_zero] for w
  1462 
  1463 lemmas divide_le_eq_numeral1 [simp,divide_const_simps] =
  1464   pos_divide_le_eq [of "numeral w", OF zero_less_numeral]
  1465   neg_divide_le_eq [of "- numeral w", OF neg_numeral_less_zero] for w
  1466 
  1467 lemmas less_divide_eq_numeral1 [simp,divide_const_simps] =
  1468   pos_less_divide_eq [of "numeral w", OF zero_less_numeral]
  1469   neg_less_divide_eq [of "- numeral w", OF neg_numeral_less_zero] for w
  1470 
  1471 lemmas divide_less_eq_numeral1 [simp,divide_const_simps] =
  1472   pos_divide_less_eq [of "numeral w", OF zero_less_numeral]
  1473   neg_divide_less_eq [of "- numeral w", OF neg_numeral_less_zero] for w
  1474 
  1475 lemmas eq_divide_eq_numeral1 [simp,divide_const_simps] =
  1476   eq_divide_eq [of _ _ "numeral w"]
  1477   eq_divide_eq [of _ _ "- numeral w"] for w
  1478 
  1479 lemmas divide_eq_eq_numeral1 [simp,divide_const_simps] =
  1480   divide_eq_eq [of _ "numeral w"]
  1481   divide_eq_eq [of _ "- numeral w"] for w
  1482 
  1483 
  1484 subsubsection \<open>Optional Simplification Rules Involving Constants\<close>
  1485 
  1486 text \<open>Simplify quotients that are compared with a literal constant.\<close>
  1487 
  1488 lemmas le_divide_eq_numeral [divide_const_simps] =
  1489   le_divide_eq [of "numeral w"]
  1490   le_divide_eq [of "- numeral w"] for w
  1491 
  1492 lemmas divide_le_eq_numeral [divide_const_simps] =
  1493   divide_le_eq [of _ _ "numeral w"]
  1494   divide_le_eq [of _ _ "- numeral w"] for w
  1495 
  1496 lemmas less_divide_eq_numeral [divide_const_simps] =
  1497   less_divide_eq [of "numeral w"]
  1498   less_divide_eq [of "- numeral w"] for w
  1499 
  1500 lemmas divide_less_eq_numeral [divide_const_simps] =
  1501   divide_less_eq [of _ _ "numeral w"]
  1502   divide_less_eq [of _ _ "- numeral w"] for w
  1503 
  1504 lemmas eq_divide_eq_numeral [divide_const_simps] =
  1505   eq_divide_eq [of "numeral w"]
  1506   eq_divide_eq [of "- numeral w"] for w
  1507 
  1508 lemmas divide_eq_eq_numeral [divide_const_simps] =
  1509   divide_eq_eq [of _ _ "numeral w"]
  1510   divide_eq_eq [of _ _ "- numeral w"] for w
  1511 
  1512 
  1513 text \<open>Not good as automatic simprules because they cause case splits.\<close>
  1514 lemmas [divide_const_simps] =
  1515   le_divide_eq_1 divide_le_eq_1 less_divide_eq_1 divide_less_eq_1
  1516 
  1517 
  1518 subsection \<open>The divides relation\<close>
  1519 
  1520 lemma zdvd_antisym_nonneg: "0 \<le> m \<Longrightarrow> 0 \<le> n \<Longrightarrow> m dvd n \<Longrightarrow> n dvd m \<Longrightarrow> m = n"
  1521   for m n :: int
  1522   by (auto simp add: dvd_def mult.assoc zero_le_mult_iff zmult_eq_1_iff)
  1523 
  1524 lemma zdvd_antisym_abs:
  1525   fixes a b :: int
  1526   assumes "a dvd b" and "b dvd a"
  1527   shows "\<bar>a\<bar> = \<bar>b\<bar>"
  1528 proof (cases "a = 0")
  1529   case True
  1530   with assms show ?thesis by simp
  1531 next
  1532   case False
  1533   from \<open>a dvd b\<close> obtain k where k: "b = a * k"
  1534     unfolding dvd_def by blast
  1535   from \<open>b dvd a\<close> obtain k' where k': "a = b * k'"
  1536     unfolding dvd_def by blast
  1537   from k k' have "a = a * k * k'" by simp
  1538   with mult_cancel_left1[where c="a" and b="k*k'"] have kk': "k * k' = 1"
  1539     using \<open>a \<noteq> 0\<close> by (simp add: mult.assoc)
  1540   then have "k = 1 \<and> k' = 1 \<or> k = -1 \<and> k' = -1"
  1541     by (simp add: zmult_eq_1_iff)
  1542   with k k' show ?thesis by auto
  1543 qed
  1544 
  1545 lemma zdvd_zdiffD: "k dvd m - n \<Longrightarrow> k dvd n \<Longrightarrow> k dvd m"
  1546   for k m n :: int
  1547   using dvd_add_right_iff [of k "- n" m] by simp
  1548 
  1549 lemma zdvd_reduce: "k dvd n + k * m \<longleftrightarrow> k dvd n"
  1550   for k m n :: int
  1551   using dvd_add_times_triv_right_iff [of k n m] by (simp add: ac_simps)
  1552 
  1553 lemma dvd_imp_le_int:
  1554   fixes d i :: int
  1555   assumes "i \<noteq> 0" and "d dvd i"
  1556   shows "\<bar>d\<bar> \<le> \<bar>i\<bar>"
  1557 proof -
  1558   from \<open>d dvd i\<close> obtain k where "i = d * k" ..
  1559   with \<open>i \<noteq> 0\<close> have "k \<noteq> 0" by auto
  1560   then have "1 \<le> \<bar>k\<bar>" and "0 \<le> \<bar>d\<bar>" by auto
  1561   then have "\<bar>d\<bar> * 1 \<le> \<bar>d\<bar> * \<bar>k\<bar>" by (rule mult_left_mono)
  1562   with \<open>i = d * k\<close> show ?thesis by (simp add: abs_mult)
  1563 qed
  1564 
  1565 lemma zdvd_not_zless:
  1566   fixes m n :: int
  1567   assumes "0 < m" and "m < n"
  1568   shows "\<not> n dvd m"
  1569 proof
  1570   from assms have "0 < n" by auto
  1571   assume "n dvd m" then obtain k where k: "m = n * k" ..
  1572   with \<open>0 < m\<close> have "0 < n * k" by auto
  1573   with \<open>0 < n\<close> have "0 < k" by (simp add: zero_less_mult_iff)
  1574   with k \<open>0 < n\<close> \<open>m < n\<close> have "n * k < n * 1" by simp
  1575   with \<open>0 < n\<close> \<open>0 < k\<close> show False unfolding mult_less_cancel_left by auto
  1576 qed
  1577 
  1578 lemma zdvd_mult_cancel:
  1579   fixes k m n :: int
  1580   assumes d: "k * m dvd k * n"
  1581     and "k \<noteq> 0"
  1582   shows "m dvd n"
  1583 proof -
  1584   from d obtain h where h: "k * n = k * m * h"
  1585     unfolding dvd_def by blast
  1586   have "n = m * h"
  1587   proof (rule ccontr)
  1588     assume "\<not> ?thesis"
  1589     with \<open>k \<noteq> 0\<close> have "k * n \<noteq> k * (m * h)" by simp
  1590     with h show False
  1591       by (simp add: mult.assoc)
  1592   qed
  1593   then show ?thesis by simp
  1594 qed
  1595 
  1596 theorem zdvd_int: "x dvd y \<longleftrightarrow> int x dvd int y"
  1597 proof -
  1598   have "x dvd y" if "int y = int x * k" for k
  1599   proof (cases k)
  1600     case (nonneg n)
  1601     with that have "y = x * n"
  1602       by (simp del: of_nat_mult add: of_nat_mult [symmetric])
  1603     then show ?thesis ..
  1604   next
  1605     case (neg n)
  1606     with that have "int y = int x * (- int (Suc n))"
  1607       by simp
  1608     also have "\<dots> = - (int x * int (Suc n))"
  1609       by (simp only: mult_minus_right)
  1610     also have "\<dots> = - int (x * Suc n)"
  1611       by (simp only: of_nat_mult [symmetric])
  1612     finally have "- int (x * Suc n) = int y" ..
  1613     then show ?thesis
  1614       by (simp only: negative_eq_positive) auto
  1615   qed
  1616   then show ?thesis
  1617     by (auto elim!: dvdE simp only: dvd_triv_left of_nat_mult)
  1618 qed
  1619 
  1620 lemma zdvd1_eq[simp]: "x dvd 1 \<longleftrightarrow> \<bar>x\<bar> = 1"
  1621   (is "?lhs \<longleftrightarrow> ?rhs")
  1622   for x :: int
  1623 proof
  1624   assume ?lhs
  1625   then have "int (nat \<bar>x\<bar>) dvd int (nat 1)" by simp
  1626   then have "nat \<bar>x\<bar> dvd 1" by (simp add: zdvd_int)
  1627   then have "nat \<bar>x\<bar> = 1" by simp
  1628   then show ?rhs by (cases "x < 0") auto
  1629 next
  1630   assume ?rhs
  1631   then have "x = 1 \<or> x = - 1" by auto
  1632   then show ?lhs by (auto intro: dvdI)
  1633 qed
  1634 
  1635 lemma zdvd_mult_cancel1:
  1636   fixes m :: int
  1637   assumes mp: "m \<noteq> 0"
  1638   shows "m * n dvd m \<longleftrightarrow> \<bar>n\<bar> = 1"
  1639     (is "?lhs \<longleftrightarrow> ?rhs")
  1640 proof
  1641   assume ?rhs
  1642   then show ?lhs
  1643     by (cases "n > 0") (auto simp add: minus_equation_iff)
  1644 next
  1645   assume ?lhs
  1646   then have "m * n dvd m * 1" by simp
  1647   from zdvd_mult_cancel[OF this mp] show ?rhs
  1648     by (simp only: zdvd1_eq)
  1649 qed
  1650 
  1651 lemma int_dvd_iff: "int m dvd z \<longleftrightarrow> m dvd nat \<bar>z\<bar>"
  1652   by (cases "z \<ge> 0") (simp_all add: zdvd_int)
  1653 
  1654 lemma dvd_int_iff: "z dvd int m \<longleftrightarrow> nat \<bar>z\<bar> dvd m"
  1655   by (cases "z \<ge> 0") (simp_all add: zdvd_int)
  1656 
  1657 lemma dvd_int_unfold_dvd_nat: "k dvd l \<longleftrightarrow> nat \<bar>k\<bar> dvd nat \<bar>l\<bar>"
  1658   by (simp add: dvd_int_iff [symmetric])
  1659 
  1660 lemma nat_dvd_iff: "nat z dvd m \<longleftrightarrow> (if 0 \<le> z then z dvd int m else m = 0)"
  1661   by (auto simp add: dvd_int_iff)
  1662 
  1663 lemma eq_nat_nat_iff: "0 \<le> z \<Longrightarrow> 0 \<le> z' \<Longrightarrow> nat z = nat z' \<longleftrightarrow> z = z'"
  1664   by (auto elim!: nonneg_eq_int)
  1665 
  1666 lemma nat_power_eq: "0 \<le> z \<Longrightarrow> nat (z ^ n) = nat z ^ n"
  1667   by (induct n) (simp_all add: nat_mult_distrib)
  1668 
  1669 lemma zdvd_imp_le: "z dvd n \<Longrightarrow> 0 < n \<Longrightarrow> z \<le> n"
  1670   for n z :: int
  1671   apply (cases n)
  1672    apply (auto simp add: dvd_int_iff)
  1673   apply (cases z)
  1674    apply (auto simp add: dvd_imp_le)
  1675   done
  1676 
  1677 lemma zdvd_period:
  1678   fixes a d :: int
  1679   assumes "a dvd d"
  1680   shows "a dvd (x + t) \<longleftrightarrow> a dvd ((x + c * d) + t)"
  1681     (is "?lhs \<longleftrightarrow> ?rhs")
  1682 proof -
  1683   from assms have "a dvd (x + t) \<longleftrightarrow> a dvd ((x + t) + c * d)"
  1684     by (simp add: dvd_add_left_iff)
  1685   then show ?thesis
  1686     by (simp add: ac_simps)
  1687 qed
  1688 
  1689 
  1690 subsection \<open>Finiteness of intervals\<close>
  1691 
  1692 lemma finite_interval_int1 [iff]: "finite {i :: int. a \<le> i \<and> i \<le> b}"
  1693 proof (cases "a \<le> b")
  1694   case True
  1695   then show ?thesis
  1696   proof (induct b rule: int_ge_induct)
  1697     case base
  1698     have "{i. a \<le> i \<and> i \<le> a} = {a}" by auto
  1699     then show ?case by simp
  1700   next
  1701     case (step b)
  1702     then have "{i. a \<le> i \<and> i \<le> b + 1} = {i. a \<le> i \<and> i \<le> b} \<union> {b + 1}" by auto
  1703     with step show ?case by simp
  1704   qed
  1705 next
  1706   case False
  1707   then show ?thesis
  1708     by (metis (lifting, no_types) Collect_empty_eq finite.emptyI order_trans)
  1709 qed
  1710 
  1711 lemma finite_interval_int2 [iff]: "finite {i :: int. a \<le> i \<and> i < b}"
  1712   by (rule rev_finite_subset[OF finite_interval_int1[of "a" "b"]]) auto
  1713 
  1714 lemma finite_interval_int3 [iff]: "finite {i :: int. a < i \<and> i \<le> b}"
  1715   by (rule rev_finite_subset[OF finite_interval_int1[of "a" "b"]]) auto
  1716 
  1717 lemma finite_interval_int4 [iff]: "finite {i :: int. a < i \<and> i < b}"
  1718   by (rule rev_finite_subset[OF finite_interval_int1[of "a" "b"]]) auto
  1719 
  1720 
  1721 subsection \<open>Configuration of the code generator\<close>
  1722 
  1723 text \<open>Constructors\<close>
  1724 
  1725 definition Pos :: "num \<Rightarrow> int"
  1726   where [simp, code_abbrev]: "Pos = numeral"
  1727 
  1728 definition Neg :: "num \<Rightarrow> int"
  1729   where [simp, code_abbrev]: "Neg n = - (Pos n)"
  1730 
  1731 code_datatype "0::int" Pos Neg
  1732 
  1733 
  1734 text \<open>Auxiliary operations.\<close>
  1735 
  1736 definition dup :: "int \<Rightarrow> int"
  1737   where [simp]: "dup k = k + k"
  1738 
  1739 lemma dup_code [code]:
  1740   "dup 0 = 0"
  1741   "dup (Pos n) = Pos (Num.Bit0 n)"
  1742   "dup (Neg n) = Neg (Num.Bit0 n)"
  1743   by (simp_all add: numeral_Bit0)
  1744 
  1745 definition sub :: "num \<Rightarrow> num \<Rightarrow> int"
  1746   where [simp]: "sub m n = numeral m - numeral n"
  1747 
  1748 lemma sub_code [code]:
  1749   "sub Num.One Num.One = 0"
  1750   "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
  1751   "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
  1752   "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
  1753   "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
  1754   "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
  1755   "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
  1756   "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
  1757   "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
  1758   by (simp_all only: sub_def dup_def numeral.simps Pos_def Neg_def numeral_BitM)
  1759 
  1760 text \<open>Implementations.\<close>
  1761 
  1762 lemma one_int_code [code]: "1 = Pos Num.One"
  1763   by simp
  1764 
  1765 lemma plus_int_code [code]:
  1766   "k + 0 = k"
  1767   "0 + l = l"
  1768   "Pos m + Pos n = Pos (m + n)"
  1769   "Pos m + Neg n = sub m n"
  1770   "Neg m + Pos n = sub n m"
  1771   "Neg m + Neg n = Neg (m + n)"
  1772   for k l :: int
  1773   by simp_all
  1774 
  1775 lemma uminus_int_code [code]:
  1776   "uminus 0 = (0::int)"
  1777   "uminus (Pos m) = Neg m"
  1778   "uminus (Neg m) = Pos m"
  1779   by simp_all
  1780 
  1781 lemma minus_int_code [code]:
  1782   "k - 0 = k"
  1783   "0 - l = uminus l"
  1784   "Pos m - Pos n = sub m n"
  1785   "Pos m - Neg n = Pos (m + n)"
  1786   "Neg m - Pos n = Neg (m + n)"
  1787   "Neg m - Neg n = sub n m"
  1788   for k l :: int
  1789   by simp_all
  1790 
  1791 lemma times_int_code [code]:
  1792   "k * 0 = 0"
  1793   "0 * l = 0"
  1794   "Pos m * Pos n = Pos (m * n)"
  1795   "Pos m * Neg n = Neg (m * n)"
  1796   "Neg m * Pos n = Neg (m * n)"
  1797   "Neg m * Neg n = Pos (m * n)"
  1798   for k l :: int
  1799   by simp_all
  1800 
  1801 instantiation int :: equal
  1802 begin
  1803 
  1804 definition "HOL.equal k l \<longleftrightarrow> k = (l::int)"
  1805 
  1806 instance
  1807   by standard (rule equal_int_def)
  1808 
  1809 end
  1810 
  1811 lemma equal_int_code [code]:
  1812   "HOL.equal 0 (0::int) \<longleftrightarrow> True"
  1813   "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
  1814   "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
  1815   "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
  1816   "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
  1817   "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
  1818   "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
  1819   "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
  1820   "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
  1821   by (auto simp add: equal)
  1822 
  1823 lemma equal_int_refl [code nbe]: "HOL.equal k k \<longleftrightarrow> True"
  1824   for k :: int
  1825   by (fact equal_refl)
  1826 
  1827 lemma less_eq_int_code [code]:
  1828   "0 \<le> (0::int) \<longleftrightarrow> True"
  1829   "0 \<le> Pos l \<longleftrightarrow> True"
  1830   "0 \<le> Neg l \<longleftrightarrow> False"
  1831   "Pos k \<le> 0 \<longleftrightarrow> False"
  1832   "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
  1833   "Pos k \<le> Neg l \<longleftrightarrow> False"
  1834   "Neg k \<le> 0 \<longleftrightarrow> True"
  1835   "Neg k \<le> Pos l \<longleftrightarrow> True"
  1836   "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
  1837   by simp_all
  1838 
  1839 lemma less_int_code [code]:
  1840   "0 < (0::int) \<longleftrightarrow> False"
  1841   "0 < Pos l \<longleftrightarrow> True"
  1842   "0 < Neg l \<longleftrightarrow> False"
  1843   "Pos k < 0 \<longleftrightarrow> False"
  1844   "Pos k < Pos l \<longleftrightarrow> k < l"
  1845   "Pos k < Neg l \<longleftrightarrow> False"
  1846   "Neg k < 0 \<longleftrightarrow> True"
  1847   "Neg k < Pos l \<longleftrightarrow> True"
  1848   "Neg k < Neg l \<longleftrightarrow> l < k"
  1849   by simp_all
  1850 
  1851 lemma nat_code [code]:
  1852   "nat (Int.Neg k) = 0"
  1853   "nat 0 = 0"
  1854   "nat (Int.Pos k) = nat_of_num k"
  1855   by (simp_all add: nat_of_num_numeral)
  1856 
  1857 lemma (in ring_1) of_int_code [code]:
  1858   "of_int (Int.Neg k) = - numeral k"
  1859   "of_int 0 = 0"
  1860   "of_int (Int.Pos k) = numeral k"
  1861   by simp_all
  1862 
  1863 
  1864 text \<open>Serializer setup.\<close>
  1865 
  1866 code_identifier
  1867   code_module Int \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
  1868 
  1869 quickcheck_params [default_type = int]
  1870 
  1871 hide_const (open) Pos Neg sub dup
  1872 
  1873 
  1874 text \<open>De-register \<open>int\<close> as a quotient type:\<close>
  1875 
  1876 lifting_update int.lifting
  1877 lifting_forget int.lifting
  1878 
  1879 end