src/HOL/Nat.thy
author haftmann
Wed Dec 03 15:58:44 2008 +0100 (2008-12-03)
changeset 28952 15a4b2cf8c34
parent 28823 dcbef866c9e2
child 29608 564ea783ace8
child 29667 53103fc8ffa3
permissions -rw-r--r--
made repository layout more coherent with logical distribution structure; stripped some $Id$s
     1 (*  Title:      HOL/Nat.thy
     2     Author:     Tobias Nipkow and Lawrence C Paulson and Markus Wenzel
     3 
     4 Type "nat" is a linear order, and a datatype; arithmetic operators + -
     5 and * (for div, mod and dvd, see theory Divides).
     6 *)
     7 
     8 header {* Natural numbers *}
     9 
    10 theory Nat
    11 imports Inductive Ring_and_Field
    12 uses
    13   "~~/src/Tools/rat.ML"
    14   "~~/src/Provers/Arith/cancel_sums.ML"
    15   ("Tools/arith_data.ML")
    16   "~~/src/Provers/Arith/fast_lin_arith.ML"
    17   ("Tools/lin_arith.ML")
    18 begin
    19 
    20 subsection {* Type @{text ind} *}
    21 
    22 typedecl ind
    23 
    24 axiomatization
    25   Zero_Rep :: ind and
    26   Suc_Rep :: "ind => ind"
    27 where
    28   -- {* the axiom of infinity in 2 parts *}
    29   inj_Suc_Rep:          "inj Suc_Rep" and
    30   Suc_Rep_not_Zero_Rep: "Suc_Rep x \<noteq> Zero_Rep"
    31 
    32 
    33 subsection {* Type nat *}
    34 
    35 text {* Type definition *}
    36 
    37 inductive Nat :: "ind \<Rightarrow> bool"
    38 where
    39     Zero_RepI: "Nat Zero_Rep"
    40   | Suc_RepI: "Nat i \<Longrightarrow> Nat (Suc_Rep i)"
    41 
    42 global
    43 
    44 typedef (open Nat)
    45   nat = Nat
    46   by (rule exI, unfold mem_def, rule Nat.Zero_RepI)
    47 
    48 constdefs
    49   Suc ::   "nat => nat"
    50   Suc_def: "Suc == (%n. Abs_Nat (Suc_Rep (Rep_Nat n)))"
    51 
    52 local
    53 
    54 instantiation nat :: zero
    55 begin
    56 
    57 definition Zero_nat_def [code del]:
    58   "0 = Abs_Nat Zero_Rep"
    59 
    60 instance ..
    61 
    62 end
    63 
    64 lemma Suc_not_Zero: "Suc m \<noteq> 0"
    65   apply (simp add: Zero_nat_def Suc_def Abs_Nat_inject [unfolded mem_def]
    66     Rep_Nat [unfolded mem_def] Suc_RepI Zero_RepI Suc_Rep_not_Zero_Rep [unfolded mem_def])
    67   done
    68 
    69 lemma Zero_not_Suc: "0 \<noteq> Suc m"
    70   by (rule not_sym, rule Suc_not_Zero not_sym)
    71 
    72 rep_datatype "0 \<Colon> nat" Suc
    73   apply (unfold Zero_nat_def Suc_def)
    74      apply (rule Rep_Nat_inverse [THEN subst]) -- {* types force good instantiation *}
    75      apply (erule Rep_Nat [unfolded mem_def, THEN Nat.induct])
    76      apply (iprover elim: Abs_Nat_inverse [unfolded mem_def, THEN subst])
    77     apply (simp_all add: Abs_Nat_inject [unfolded mem_def] Rep_Nat [unfolded mem_def]
    78       Suc_RepI Zero_RepI Suc_Rep_not_Zero_Rep [unfolded mem_def]
    79       Suc_Rep_not_Zero_Rep [unfolded mem_def, symmetric]
    80       inj_Suc_Rep [THEN inj_eq] Rep_Nat_inject)
    81   done
    82 
    83 lemma nat_induct [case_names 0 Suc, induct type: nat]:
    84   -- {* for backward compatibility -- naming of variables differs *}
    85   fixes n
    86   assumes "P 0"
    87     and "\<And>n. P n \<Longrightarrow> P (Suc n)"
    88   shows "P n"
    89   using assms by (rule nat.induct) 
    90 
    91 declare nat.exhaust [case_names 0 Suc, cases type: nat]
    92 
    93 lemmas nat_rec_0 = nat.recs(1)
    94   and nat_rec_Suc = nat.recs(2)
    95 
    96 lemmas nat_case_0 = nat.cases(1)
    97   and nat_case_Suc = nat.cases(2)
    98    
    99 
   100 text {* Injectiveness and distinctness lemmas *}
   101 
   102 lemma inj_Suc[simp]: "inj_on Suc N"
   103   by (simp add: inj_on_def)
   104 
   105 lemma Suc_neq_Zero: "Suc m = 0 \<Longrightarrow> R"
   106 by (rule notE, rule Suc_not_Zero)
   107 
   108 lemma Zero_neq_Suc: "0 = Suc m \<Longrightarrow> R"
   109 by (rule Suc_neq_Zero, erule sym)
   110 
   111 lemma Suc_inject: "Suc x = Suc y \<Longrightarrow> x = y"
   112 by (rule inj_Suc [THEN injD])
   113 
   114 lemma n_not_Suc_n: "n \<noteq> Suc n"
   115 by (induct n) simp_all
   116 
   117 lemma Suc_n_not_n: "Suc n \<noteq> n"
   118 by (rule not_sym, rule n_not_Suc_n)
   119 
   120 text {* A special form of induction for reasoning
   121   about @{term "m < n"} and @{term "m - n"} *}
   122 
   123 lemma diff_induct: "(!!x. P x 0) ==> (!!y. P 0 (Suc y)) ==>
   124     (!!x y. P x y ==> P (Suc x) (Suc y)) ==> P m n"
   125   apply (rule_tac x = m in spec)
   126   apply (induct n)
   127   prefer 2
   128   apply (rule allI)
   129   apply (induct_tac x, iprover+)
   130   done
   131 
   132 
   133 subsection {* Arithmetic operators *}
   134 
   135 instantiation nat :: "{minus, comm_monoid_add}"
   136 begin
   137 
   138 primrec plus_nat
   139 where
   140   add_0:      "0 + n = (n\<Colon>nat)"
   141   | add_Suc:  "Suc m + n = Suc (m + n)"
   142 
   143 lemma add_0_right [simp]: "m + 0 = (m::nat)"
   144   by (induct m) simp_all
   145 
   146 lemma add_Suc_right [simp]: "m + Suc n = Suc (m + n)"
   147   by (induct m) simp_all
   148 
   149 declare add_0 [code]
   150 
   151 lemma add_Suc_shift [code]: "Suc m + n = m + Suc n"
   152   by simp
   153 
   154 primrec minus_nat
   155 where
   156   diff_0:     "m - 0 = (m\<Colon>nat)"
   157   | diff_Suc: "m - Suc n = (case m - n of 0 => 0 | Suc k => k)"
   158 
   159 declare diff_Suc [simp del]
   160 declare diff_0 [code]
   161 
   162 lemma diff_0_eq_0 [simp, code]: "0 - n = (0::nat)"
   163   by (induct n) (simp_all add: diff_Suc)
   164 
   165 lemma diff_Suc_Suc [simp, code]: "Suc m - Suc n = m - n"
   166   by (induct n) (simp_all add: diff_Suc)
   167 
   168 instance proof
   169   fix n m q :: nat
   170   show "(n + m) + q = n + (m + q)" by (induct n) simp_all
   171   show "n + m = m + n" by (induct n) simp_all
   172   show "0 + n = n" by simp
   173 qed
   174 
   175 end
   176 
   177 instantiation nat :: comm_semiring_1_cancel
   178 begin
   179 
   180 definition
   181   One_nat_def [simp]: "1 = Suc 0"
   182 
   183 primrec times_nat
   184 where
   185   mult_0:     "0 * n = (0\<Colon>nat)"
   186   | mult_Suc: "Suc m * n = n + (m * n)"
   187 
   188 lemma mult_0_right [simp]: "(m::nat) * 0 = 0"
   189   by (induct m) simp_all
   190 
   191 lemma mult_Suc_right [simp]: "m * Suc n = m + (m * n)"
   192   by (induct m) (simp_all add: add_left_commute)
   193 
   194 lemma add_mult_distrib: "(m + n) * k = (m * k) + ((n * k)::nat)"
   195   by (induct m) (simp_all add: add_assoc)
   196 
   197 instance proof
   198   fix n m q :: nat
   199   show "0 \<noteq> (1::nat)" by simp
   200   show "1 * n = n" by simp
   201   show "n * m = m * n" by (induct n) simp_all
   202   show "(n * m) * q = n * (m * q)" by (induct n) (simp_all add: add_mult_distrib)
   203   show "(n + m) * q = n * q + m * q" by (rule add_mult_distrib)
   204   assume "n + m = n + q" thus "m = q" by (induct n) simp_all
   205 qed
   206 
   207 end
   208 
   209 subsubsection {* Addition *}
   210 
   211 lemma nat_add_assoc: "(m + n) + k = m + ((n + k)::nat)"
   212   by (rule add_assoc)
   213 
   214 lemma nat_add_commute: "m + n = n + (m::nat)"
   215   by (rule add_commute)
   216 
   217 lemma nat_add_left_commute: "x + (y + z) = y + ((x + z)::nat)"
   218   by (rule add_left_commute)
   219 
   220 lemma nat_add_left_cancel [simp]: "(k + m = k + n) = (m = (n::nat))"
   221   by (rule add_left_cancel)
   222 
   223 lemma nat_add_right_cancel [simp]: "(m + k = n + k) = (m=(n::nat))"
   224   by (rule add_right_cancel)
   225 
   226 text {* Reasoning about @{text "m + 0 = 0"}, etc. *}
   227 
   228 lemma add_is_0 [iff]:
   229   fixes m n :: nat
   230   shows "(m + n = 0) = (m = 0 & n = 0)"
   231   by (cases m) simp_all
   232 
   233 lemma add_is_1:
   234   "(m+n= Suc 0) = (m= Suc 0 & n=0 | m=0 & n= Suc 0)"
   235   by (cases m) simp_all
   236 
   237 lemma one_is_add:
   238   "(Suc 0 = m + n) = (m = Suc 0 & n = 0 | m = 0 & n = Suc 0)"
   239   by (rule trans, rule eq_commute, rule add_is_1)
   240 
   241 lemma add_eq_self_zero:
   242   fixes m n :: nat
   243   shows "m + n = m \<Longrightarrow> n = 0"
   244   by (induct m) simp_all
   245 
   246 lemma inj_on_add_nat[simp]: "inj_on (%n::nat. n+k) N"
   247   apply (induct k)
   248    apply simp
   249   apply(drule comp_inj_on[OF _ inj_Suc])
   250   apply (simp add:o_def)
   251   done
   252 
   253 
   254 subsubsection {* Difference *}
   255 
   256 lemma diff_self_eq_0 [simp]: "(m\<Colon>nat) - m = 0"
   257   by (induct m) simp_all
   258 
   259 lemma diff_diff_left: "(i::nat) - j - k = i - (j + k)"
   260   by (induct i j rule: diff_induct) simp_all
   261 
   262 lemma Suc_diff_diff [simp]: "(Suc m - n) - Suc k = m - n - k"
   263   by (simp add: diff_diff_left)
   264 
   265 lemma diff_commute: "(i::nat) - j - k = i - k - j"
   266   by (simp add: diff_diff_left add_commute)
   267 
   268 lemma diff_add_inverse: "(n + m) - n = (m::nat)"
   269   by (induct n) simp_all
   270 
   271 lemma diff_add_inverse2: "(m + n) - n = (m::nat)"
   272   by (simp add: diff_add_inverse add_commute [of m n])
   273 
   274 lemma diff_cancel: "(k + m) - (k + n) = m - (n::nat)"
   275   by (induct k) simp_all
   276 
   277 lemma diff_cancel2: "(m + k) - (n + k) = m - (n::nat)"
   278   by (simp add: diff_cancel add_commute)
   279 
   280 lemma diff_add_0: "n - (n + m) = (0::nat)"
   281   by (induct n) simp_all
   282 
   283 text {* Difference distributes over multiplication *}
   284 
   285 lemma diff_mult_distrib: "((m::nat) - n) * k = (m * k) - (n * k)"
   286 by (induct m n rule: diff_induct) (simp_all add: diff_cancel)
   287 
   288 lemma diff_mult_distrib2: "k * ((m::nat) - n) = (k * m) - (k * n)"
   289 by (simp add: diff_mult_distrib mult_commute [of k])
   290   -- {* NOT added as rewrites, since sometimes they are used from right-to-left *}
   291 
   292 
   293 subsubsection {* Multiplication *}
   294 
   295 lemma nat_mult_assoc: "(m * n) * k = m * ((n * k)::nat)"
   296   by (rule mult_assoc)
   297 
   298 lemma nat_mult_commute: "m * n = n * (m::nat)"
   299   by (rule mult_commute)
   300 
   301 lemma add_mult_distrib2: "k * (m + n) = (k * m) + ((k * n)::nat)"
   302   by (rule right_distrib)
   303 
   304 lemma mult_is_0 [simp]: "((m::nat) * n = 0) = (m=0 | n=0)"
   305   by (induct m) auto
   306 
   307 lemmas nat_distrib =
   308   add_mult_distrib add_mult_distrib2 diff_mult_distrib diff_mult_distrib2
   309 
   310 lemma mult_eq_1_iff [simp]: "(m * n = Suc 0) = (m = 1 & n = 1)"
   311   apply (induct m)
   312    apply simp
   313   apply (induct n)
   314    apply auto
   315   done
   316 
   317 lemma one_eq_mult_iff [simp,noatp]: "(Suc 0 = m * n) = (m = 1 & n = 1)"
   318   apply (rule trans)
   319   apply (rule_tac [2] mult_eq_1_iff, fastsimp)
   320   done
   321 
   322 lemma mult_cancel1 [simp]: "(k * m = k * n) = (m = n | (k = (0::nat)))"
   323 proof -
   324   have "k \<noteq> 0 \<Longrightarrow> k * m = k * n \<Longrightarrow> m = n"
   325   proof (induct n arbitrary: m)
   326     case 0 then show "m = 0" by simp
   327   next
   328     case (Suc n) then show "m = Suc n"
   329       by (cases m) (simp_all add: eq_commute [of "0"])
   330   qed
   331   then show ?thesis by auto
   332 qed
   333 
   334 lemma mult_cancel2 [simp]: "(m * k = n * k) = (m = n | (k = (0::nat)))"
   335   by (simp add: mult_commute)
   336 
   337 lemma Suc_mult_cancel1: "(Suc k * m = Suc k * n) = (m = n)"
   338   by (subst mult_cancel1) simp
   339 
   340 
   341 subsection {* Orders on @{typ nat} *}
   342 
   343 subsubsection {* Operation definition *}
   344 
   345 instantiation nat :: linorder
   346 begin
   347 
   348 primrec less_eq_nat where
   349   "(0\<Colon>nat) \<le> n \<longleftrightarrow> True"
   350   | "Suc m \<le> n \<longleftrightarrow> (case n of 0 \<Rightarrow> False | Suc n \<Rightarrow> m \<le> n)"
   351 
   352 declare less_eq_nat.simps [simp del]
   353 lemma [code]: "(0\<Colon>nat) \<le> n \<longleftrightarrow> True" by (simp add: less_eq_nat.simps)
   354 lemma le0 [iff]: "0 \<le> (n\<Colon>nat)" by (simp add: less_eq_nat.simps)
   355 
   356 definition less_nat where
   357   less_eq_Suc_le: "n < m \<longleftrightarrow> Suc n \<le> m"
   358 
   359 lemma Suc_le_mono [iff]: "Suc n \<le> Suc m \<longleftrightarrow> n \<le> m"
   360   by (simp add: less_eq_nat.simps(2))
   361 
   362 lemma Suc_le_eq [code]: "Suc m \<le> n \<longleftrightarrow> m < n"
   363   unfolding less_eq_Suc_le ..
   364 
   365 lemma le_0_eq [iff]: "(n\<Colon>nat) \<le> 0 \<longleftrightarrow> n = 0"
   366   by (induct n) (simp_all add: less_eq_nat.simps(2))
   367 
   368 lemma not_less0 [iff]: "\<not> n < (0\<Colon>nat)"
   369   by (simp add: less_eq_Suc_le)
   370 
   371 lemma less_nat_zero_code [code]: "n < (0\<Colon>nat) \<longleftrightarrow> False"
   372   by simp
   373 
   374 lemma Suc_less_eq [iff]: "Suc m < Suc n \<longleftrightarrow> m < n"
   375   by (simp add: less_eq_Suc_le)
   376 
   377 lemma less_Suc_eq_le [code]: "m < Suc n \<longleftrightarrow> m \<le> n"
   378   by (simp add: less_eq_Suc_le)
   379 
   380 lemma le_SucI: "m \<le> n \<Longrightarrow> m \<le> Suc n"
   381   by (induct m arbitrary: n)
   382     (simp_all add: less_eq_nat.simps(2) split: nat.splits)
   383 
   384 lemma Suc_leD: "Suc m \<le> n \<Longrightarrow> m \<le> n"
   385   by (cases n) (auto intro: le_SucI)
   386 
   387 lemma less_SucI: "m < n \<Longrightarrow> m < Suc n"
   388   by (simp add: less_eq_Suc_le) (erule Suc_leD)
   389 
   390 lemma Suc_lessD: "Suc m < n \<Longrightarrow> m < n"
   391   by (simp add: less_eq_Suc_le) (erule Suc_leD)
   392 
   393 instance
   394 proof
   395   fix n m :: nat
   396   show "n < m \<longleftrightarrow> n \<le> m \<and> \<not> m \<le> n" 
   397   proof (induct n arbitrary: m)
   398     case 0 then show ?case by (cases m) (simp_all add: less_eq_Suc_le)
   399   next
   400     case (Suc n) then show ?case by (cases m) (simp_all add: less_eq_Suc_le)
   401   qed
   402 next
   403   fix n :: nat show "n \<le> n" by (induct n) simp_all
   404 next
   405   fix n m :: nat assume "n \<le> m" and "m \<le> n"
   406   then show "n = m"
   407     by (induct n arbitrary: m)
   408       (simp_all add: less_eq_nat.simps(2) split: nat.splits)
   409 next
   410   fix n m q :: nat assume "n \<le> m" and "m \<le> q"
   411   then show "n \<le> q"
   412   proof (induct n arbitrary: m q)
   413     case 0 show ?case by simp
   414   next
   415     case (Suc n) then show ?case
   416       by (simp_all (no_asm_use) add: less_eq_nat.simps(2) split: nat.splits, clarify,
   417         simp_all (no_asm_use) add: less_eq_nat.simps(2) split: nat.splits, clarify,
   418         simp_all (no_asm_use) add: less_eq_nat.simps(2) split: nat.splits)
   419   qed
   420 next
   421   fix n m :: nat show "n \<le> m \<or> m \<le> n"
   422     by (induct n arbitrary: m)
   423       (simp_all add: less_eq_nat.simps(2) split: nat.splits)
   424 qed
   425 
   426 end
   427 
   428 subsubsection {* Introduction properties *}
   429 
   430 lemma lessI [iff]: "n < Suc n"
   431   by (simp add: less_Suc_eq_le)
   432 
   433 lemma zero_less_Suc [iff]: "0 < Suc n"
   434   by (simp add: less_Suc_eq_le)
   435 
   436 
   437 subsubsection {* Elimination properties *}
   438 
   439 lemma less_not_refl: "~ n < (n::nat)"
   440   by (rule order_less_irrefl)
   441 
   442 lemma less_not_refl2: "n < m ==> m \<noteq> (n::nat)"
   443   by (rule not_sym) (rule less_imp_neq) 
   444 
   445 lemma less_not_refl3: "(s::nat) < t ==> s \<noteq> t"
   446   by (rule less_imp_neq)
   447 
   448 lemma less_irrefl_nat: "(n::nat) < n ==> R"
   449   by (rule notE, rule less_not_refl)
   450 
   451 lemma less_zeroE: "(n::nat) < 0 ==> R"
   452   by (rule notE) (rule not_less0)
   453 
   454 lemma less_Suc_eq: "(m < Suc n) = (m < n | m = n)"
   455   unfolding less_Suc_eq_le le_less ..
   456 
   457 lemma less_one [iff, noatp]: "(n < (1::nat)) = (n = 0)"
   458   by (simp add: less_Suc_eq)
   459 
   460 lemma less_Suc0 [iff]: "(n < Suc 0) = (n = 0)"
   461   by (simp add: less_Suc_eq)
   462 
   463 lemma Suc_mono: "m < n ==> Suc m < Suc n"
   464   by simp
   465 
   466 text {* "Less than" is antisymmetric, sort of *}
   467 lemma less_antisym: "\<lbrakk> \<not> n < m; n < Suc m \<rbrakk> \<Longrightarrow> m = n"
   468   unfolding not_less less_Suc_eq_le by (rule antisym)
   469 
   470 lemma nat_neq_iff: "((m::nat) \<noteq> n) = (m < n | n < m)"
   471   by (rule linorder_neq_iff)
   472 
   473 lemma nat_less_cases: assumes major: "(m::nat) < n ==> P n m"
   474   and eqCase: "m = n ==> P n m" and lessCase: "n<m ==> P n m"
   475   shows "P n m"
   476   apply (rule less_linear [THEN disjE])
   477   apply (erule_tac [2] disjE)
   478   apply (erule lessCase)
   479   apply (erule sym [THEN eqCase])
   480   apply (erule major)
   481   done
   482 
   483 
   484 subsubsection {* Inductive (?) properties *}
   485 
   486 lemma Suc_lessI: "m < n ==> Suc m \<noteq> n ==> Suc m < n"
   487   unfolding less_eq_Suc_le [of m] le_less by simp 
   488 
   489 lemma lessE:
   490   assumes major: "i < k"
   491   and p1: "k = Suc i ==> P" and p2: "!!j. i < j ==> k = Suc j ==> P"
   492   shows P
   493 proof -
   494   from major have "\<exists>j. i \<le> j \<and> k = Suc j"
   495     unfolding less_eq_Suc_le by (induct k) simp_all
   496   then have "(\<exists>j. i < j \<and> k = Suc j) \<or> k = Suc i"
   497     by (clarsimp simp add: less_le)
   498   with p1 p2 show P by auto
   499 qed
   500 
   501 lemma less_SucE: assumes major: "m < Suc n"
   502   and less: "m < n ==> P" and eq: "m = n ==> P" shows P
   503   apply (rule major [THEN lessE])
   504   apply (rule eq, blast)
   505   apply (rule less, blast)
   506   done
   507 
   508 lemma Suc_lessE: assumes major: "Suc i < k"
   509   and minor: "!!j. i < j ==> k = Suc j ==> P" shows P
   510   apply (rule major [THEN lessE])
   511   apply (erule lessI [THEN minor])
   512   apply (erule Suc_lessD [THEN minor], assumption)
   513   done
   514 
   515 lemma Suc_less_SucD: "Suc m < Suc n ==> m < n"
   516   by simp
   517 
   518 lemma less_trans_Suc:
   519   assumes le: "i < j" shows "j < k ==> Suc i < k"
   520   apply (induct k, simp_all)
   521   apply (insert le)
   522   apply (simp add: less_Suc_eq)
   523   apply (blast dest: Suc_lessD)
   524   done
   525 
   526 text {* Can be used with @{text less_Suc_eq} to get @{term "n = m | n < m"} *}
   527 lemma not_less_eq: "\<not> m < n \<longleftrightarrow> n < Suc m"
   528   unfolding not_less less_Suc_eq_le ..
   529 
   530 lemma not_less_eq_eq: "\<not> m \<le> n \<longleftrightarrow> Suc n \<le> m"
   531   unfolding not_le Suc_le_eq ..
   532 
   533 text {* Properties of "less than or equal" *}
   534 
   535 lemma le_imp_less_Suc: "m \<le> n ==> m < Suc n"
   536   unfolding less_Suc_eq_le .
   537 
   538 lemma Suc_n_not_le_n: "~ Suc n \<le> n"
   539   unfolding not_le less_Suc_eq_le ..
   540 
   541 lemma le_Suc_eq: "(m \<le> Suc n) = (m \<le> n | m = Suc n)"
   542   by (simp add: less_Suc_eq_le [symmetric] less_Suc_eq)
   543 
   544 lemma le_SucE: "m \<le> Suc n ==> (m \<le> n ==> R) ==> (m = Suc n ==> R) ==> R"
   545   by (drule le_Suc_eq [THEN iffD1], iprover+)
   546 
   547 lemma Suc_leI: "m < n ==> Suc(m) \<le> n"
   548   unfolding Suc_le_eq .
   549 
   550 text {* Stronger version of @{text Suc_leD} *}
   551 lemma Suc_le_lessD: "Suc m \<le> n ==> m < n"
   552   unfolding Suc_le_eq .
   553 
   554 lemma less_imp_le_nat: "m < n ==> m \<le> (n::nat)"
   555   unfolding less_eq_Suc_le by (rule Suc_leD)
   556 
   557 text {* For instance, @{text "(Suc m < Suc n) = (Suc m \<le> n) = (m < n)"} *}
   558 lemmas le_simps = less_imp_le_nat less_Suc_eq_le Suc_le_eq
   559 
   560 
   561 text {* Equivalence of @{term "m \<le> n"} and @{term "m < n | m = n"} *}
   562 
   563 lemma less_or_eq_imp_le: "m < n | m = n ==> m \<le> (n::nat)"
   564   unfolding le_less .
   565 
   566 lemma le_eq_less_or_eq: "(m \<le> (n::nat)) = (m < n | m=n)"
   567   by (rule le_less)
   568 
   569 text {* Useful with @{text blast}. *}
   570 lemma eq_imp_le: "(m::nat) = n ==> m \<le> n"
   571   by auto
   572 
   573 lemma le_refl: "n \<le> (n::nat)"
   574   by simp
   575 
   576 lemma le_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::nat)"
   577   by (rule order_trans)
   578 
   579 lemma le_anti_sym: "[| m \<le> n; n \<le> m |] ==> m = (n::nat)"
   580   by (rule antisym)
   581 
   582 lemma nat_less_le: "((m::nat) < n) = (m \<le> n & m \<noteq> n)"
   583   by (rule less_le)
   584 
   585 lemma le_neq_implies_less: "(m::nat) \<le> n ==> m \<noteq> n ==> m < n"
   586   unfolding less_le ..
   587 
   588 lemma nat_le_linear: "(m::nat) \<le> n | n \<le> m"
   589   by (rule linear)
   590 
   591 lemmas linorder_neqE_nat = linorder_neqE [where 'a = nat]
   592 
   593 lemma le_less_Suc_eq: "m \<le> n ==> (n < Suc m) = (n = m)"
   594   unfolding less_Suc_eq_le by auto
   595 
   596 lemma not_less_less_Suc_eq: "~ n < m ==> (n < Suc m) = (n = m)"
   597   unfolding not_less by (rule le_less_Suc_eq)
   598 
   599 lemmas not_less_simps = not_less_less_Suc_eq le_less_Suc_eq
   600 
   601 text {* These two rules ease the use of primitive recursion.
   602 NOTE USE OF @{text "=="} *}
   603 lemma def_nat_rec_0: "(!!n. f n == nat_rec c h n) ==> f 0 = c"
   604 by simp
   605 
   606 lemma def_nat_rec_Suc: "(!!n. f n == nat_rec c h n) ==> f (Suc n) = h n (f n)"
   607 by simp
   608 
   609 lemma not0_implies_Suc: "n \<noteq> 0 ==> \<exists>m. n = Suc m"
   610 by (cases n) simp_all
   611 
   612 lemma gr0_implies_Suc: "n > 0 ==> \<exists>m. n = Suc m"
   613 by (cases n) simp_all
   614 
   615 lemma gr_implies_not0: fixes n :: nat shows "m<n ==> n \<noteq> 0"
   616 by (cases n) simp_all
   617 
   618 lemma neq0_conv[iff]: fixes n :: nat shows "(n \<noteq> 0) = (0 < n)"
   619 by (cases n) simp_all
   620 
   621 text {* This theorem is useful with @{text blast} *}
   622 lemma gr0I: "((n::nat) = 0 ==> False) ==> 0 < n"
   623 by (rule neq0_conv[THEN iffD1], iprover)
   624 
   625 lemma gr0_conv_Suc: "(0 < n) = (\<exists>m. n = Suc m)"
   626 by (fast intro: not0_implies_Suc)
   627 
   628 lemma not_gr0 [iff,noatp]: "!!n::nat. (~ (0 < n)) = (n = 0)"
   629 using neq0_conv by blast
   630 
   631 lemma Suc_le_D: "(Suc n \<le> m') ==> (? m. m' = Suc m)"
   632 by (induct m') simp_all
   633 
   634 text {* Useful in certain inductive arguments *}
   635 lemma less_Suc_eq_0_disj: "(m < Suc n) = (m = 0 | (\<exists>j. m = Suc j & j < n))"
   636 by (cases m) simp_all
   637 
   638 
   639 subsubsection {* @{term min} and @{term max} *}
   640 
   641 lemma mono_Suc: "mono Suc"
   642 by (rule monoI) simp
   643 
   644 lemma min_0L [simp]: "min 0 n = (0::nat)"
   645 by (rule min_leastL) simp
   646 
   647 lemma min_0R [simp]: "min n 0 = (0::nat)"
   648 by (rule min_leastR) simp
   649 
   650 lemma min_Suc_Suc [simp]: "min (Suc m) (Suc n) = Suc (min m n)"
   651 by (simp add: mono_Suc min_of_mono)
   652 
   653 lemma min_Suc1:
   654    "min (Suc n) m = (case m of 0 => 0 | Suc m' => Suc(min n m'))"
   655 by (simp split: nat.split)
   656 
   657 lemma min_Suc2:
   658    "min m (Suc n) = (case m of 0 => 0 | Suc m' => Suc(min m' n))"
   659 by (simp split: nat.split)
   660 
   661 lemma max_0L [simp]: "max 0 n = (n::nat)"
   662 by (rule max_leastL) simp
   663 
   664 lemma max_0R [simp]: "max n 0 = (n::nat)"
   665 by (rule max_leastR) simp
   666 
   667 lemma max_Suc_Suc [simp]: "max (Suc m) (Suc n) = Suc(max m n)"
   668 by (simp add: mono_Suc max_of_mono)
   669 
   670 lemma max_Suc1:
   671    "max (Suc n) m = (case m of 0 => Suc n | Suc m' => Suc(max n m'))"
   672 by (simp split: nat.split)
   673 
   674 lemma max_Suc2:
   675    "max m (Suc n) = (case m of 0 => Suc n | Suc m' => Suc(max m' n))"
   676 by (simp split: nat.split)
   677 
   678 
   679 subsubsection {* Monotonicity of Addition *}
   680 
   681 lemma Suc_pred [simp]: "n>0 ==> Suc (n - Suc 0) = n"
   682 by (simp add: diff_Suc split: nat.split)
   683 
   684 lemma nat_add_left_cancel_le [simp]: "(k + m \<le> k + n) = (m\<le>(n::nat))"
   685 by (induct k) simp_all
   686 
   687 lemma nat_add_left_cancel_less [simp]: "(k + m < k + n) = (m<(n::nat))"
   688 by (induct k) simp_all
   689 
   690 lemma add_gr_0 [iff]: "!!m::nat. (m + n > 0) = (m>0 | n>0)"
   691 by(auto dest:gr0_implies_Suc)
   692 
   693 text {* strict, in 1st argument *}
   694 lemma add_less_mono1: "i < j ==> i + k < j + (k::nat)"
   695 by (induct k) simp_all
   696 
   697 text {* strict, in both arguments *}
   698 lemma add_less_mono: "[|i < j; k < l|] ==> i + k < j + (l::nat)"
   699   apply (rule add_less_mono1 [THEN less_trans], assumption+)
   700   apply (induct j, simp_all)
   701   done
   702 
   703 text {* Deleted @{text less_natE}; use @{text "less_imp_Suc_add RS exE"} *}
   704 lemma less_imp_Suc_add: "m < n ==> (\<exists>k. n = Suc (m + k))"
   705   apply (induct n)
   706   apply (simp_all add: order_le_less)
   707   apply (blast elim!: less_SucE
   708                intro!: add_0_right [symmetric] add_Suc_right [symmetric])
   709   done
   710 
   711 text {* strict, in 1st argument; proof is by induction on @{text "k > 0"} *}
   712 lemma mult_less_mono2: "(i::nat) < j ==> 0<k ==> k * i < k * j"
   713 apply(auto simp: gr0_conv_Suc)
   714 apply (induct_tac m)
   715 apply (simp_all add: add_less_mono)
   716 done
   717 
   718 text{*The naturals form an ordered @{text comm_semiring_1_cancel}*}
   719 instance nat :: ordered_semidom
   720 proof
   721   fix i j k :: nat
   722   show "0 < (1::nat)" by simp
   723   show "i \<le> j ==> k + i \<le> k + j" by simp
   724   show "i < j ==> 0 < k ==> k * i < k * j" by (simp add: mult_less_mono2)
   725 qed
   726 
   727 lemma nat_mult_1: "(1::nat) * n = n"
   728 by simp
   729 
   730 lemma nat_mult_1_right: "n * (1::nat) = n"
   731 by simp
   732 
   733 
   734 subsubsection {* Additional theorems about @{term "op \<le>"} *}
   735 
   736 text {* Complete induction, aka course-of-values induction *}
   737 
   738 instance nat :: wellorder proof
   739   fix P and n :: nat
   740   assume step: "\<And>n::nat. (\<And>m. m < n \<Longrightarrow> P m) \<Longrightarrow> P n"
   741   have "\<And>q. q \<le> n \<Longrightarrow> P q"
   742   proof (induct n)
   743     case (0 n)
   744     have "P 0" by (rule step) auto
   745     thus ?case using 0 by auto
   746   next
   747     case (Suc m n)
   748     then have "n \<le> m \<or> n = Suc m" by (simp add: le_Suc_eq)
   749     thus ?case
   750     proof
   751       assume "n \<le> m" thus "P n" by (rule Suc(1))
   752     next
   753       assume n: "n = Suc m"
   754       show "P n"
   755         by (rule step) (rule Suc(1), simp add: n le_simps)
   756     qed
   757   qed
   758   then show "P n" by auto
   759 qed
   760 
   761 lemma Least_Suc:
   762      "[| P n; ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))"
   763   apply (case_tac "n", auto)
   764   apply (frule LeastI)
   765   apply (drule_tac P = "%x. P (Suc x) " in LeastI)
   766   apply (subgoal_tac " (LEAST x. P x) \<le> Suc (LEAST x. P (Suc x))")
   767   apply (erule_tac [2] Least_le)
   768   apply (case_tac "LEAST x. P x", auto)
   769   apply (drule_tac P = "%x. P (Suc x) " in Least_le)
   770   apply (blast intro: order_antisym)
   771   done
   772 
   773 lemma Least_Suc2:
   774    "[|P n; Q m; ~P 0; !k. P (Suc k) = Q k|] ==> Least P = Suc (Least Q)"
   775   apply (erule (1) Least_Suc [THEN ssubst])
   776   apply simp
   777   done
   778 
   779 lemma ex_least_nat_le: "\<not>P(0) \<Longrightarrow> P(n::nat) \<Longrightarrow> \<exists>k\<le>n. (\<forall>i<k. \<not>P i) & P(k)"
   780   apply (cases n)
   781    apply blast
   782   apply (rule_tac x="LEAST k. P(k)" in exI)
   783   apply (blast intro: Least_le dest: not_less_Least intro: LeastI_ex)
   784   done
   785 
   786 lemma ex_least_nat_less: "\<not>P(0) \<Longrightarrow> P(n::nat) \<Longrightarrow> \<exists>k<n. (\<forall>i\<le>k. \<not>P i) & P(k+1)"
   787   apply (cases n)
   788    apply blast
   789   apply (frule (1) ex_least_nat_le)
   790   apply (erule exE)
   791   apply (case_tac k)
   792    apply simp
   793   apply (rename_tac k1)
   794   apply (rule_tac x=k1 in exI)
   795   apply (auto simp add: less_eq_Suc_le)
   796   done
   797 
   798 lemma nat_less_induct:
   799   assumes "!!n. \<forall>m::nat. m < n --> P m ==> P n" shows "P n"
   800   using assms less_induct by blast
   801 
   802 lemma measure_induct_rule [case_names less]:
   803   fixes f :: "'a \<Rightarrow> nat"
   804   assumes step: "\<And>x. (\<And>y. f y < f x \<Longrightarrow> P y) \<Longrightarrow> P x"
   805   shows "P a"
   806 by (induct m\<equiv>"f a" arbitrary: a rule: less_induct) (auto intro: step)
   807 
   808 text {* old style induction rules: *}
   809 lemma measure_induct:
   810   fixes f :: "'a \<Rightarrow> nat"
   811   shows "(\<And>x. \<forall>y. f y < f x \<longrightarrow> P y \<Longrightarrow> P x) \<Longrightarrow> P a"
   812   by (rule measure_induct_rule [of f P a]) iprover
   813 
   814 lemma full_nat_induct:
   815   assumes step: "(!!n. (ALL m. Suc m <= n --> P m) ==> P n)"
   816   shows "P n"
   817   by (rule less_induct) (auto intro: step simp:le_simps)
   818 
   819 text{*An induction rule for estabilishing binary relations*}
   820 lemma less_Suc_induct:
   821   assumes less:  "i < j"
   822      and  step:  "!!i. P i (Suc i)"
   823      and  trans: "!!i j k. P i j ==> P j k ==> P i k"
   824   shows "P i j"
   825 proof -
   826   from less obtain k where j: "j = Suc(i+k)" by (auto dest: less_imp_Suc_add)
   827   have "P i (Suc (i + k))"
   828   proof (induct k)
   829     case 0
   830     show ?case by (simp add: step)
   831   next
   832     case (Suc k)
   833     thus ?case by (auto intro: assms)
   834   qed
   835   thus "P i j" by (simp add: j)
   836 qed
   837 
   838 lemma nat_induct2: "[|P 0; P (Suc 0); !!k. P k ==> P (Suc (Suc k))|] ==> P n"
   839   apply (rule nat_less_induct)
   840   apply (case_tac n)
   841   apply (case_tac [2] nat)
   842   apply (blast intro: less_trans)+
   843   done
   844 
   845 text {* The method of infinite descent, frequently used in number theory.
   846 Provided by Roelof Oosterhuis.
   847 $P(n)$ is true for all $n\in\mathbb{N}$ if
   848 \begin{itemize}
   849   \item case ``0'': given $n=0$ prove $P(n)$,
   850   \item case ``smaller'': given $n>0$ and $\neg P(n)$ prove there exists
   851         a smaller integer $m$ such that $\neg P(m)$.
   852 \end{itemize} *}
   853 
   854 text{* A compact version without explicit base case: *}
   855 lemma infinite_descent:
   856   "\<lbrakk> !!n::nat. \<not> P n \<Longrightarrow>  \<exists>m<n. \<not>  P m \<rbrakk> \<Longrightarrow>  P n"
   857 by (induct n rule: less_induct, auto)
   858 
   859 lemma infinite_descent0[case_names 0 smaller]: 
   860   "\<lbrakk> P 0; !!n. n>0 \<Longrightarrow> \<not> P n \<Longrightarrow> (\<exists>m::nat. m < n \<and> \<not>P m) \<rbrakk> \<Longrightarrow> P n"
   861 by (rule infinite_descent) (case_tac "n>0", auto)
   862 
   863 text {*
   864 Infinite descent using a mapping to $\mathbb{N}$:
   865 $P(x)$ is true for all $x\in D$ if there exists a $V: D \to \mathbb{N}$ and
   866 \begin{itemize}
   867 \item case ``0'': given $V(x)=0$ prove $P(x)$,
   868 \item case ``smaller'': given $V(x)>0$ and $\neg P(x)$ prove there exists a $y \in D$ such that $V(y)<V(x)$ and $~\neg P(y)$.
   869 \end{itemize}
   870 NB: the proof also shows how to use the previous lemma. *}
   871 
   872 corollary infinite_descent0_measure [case_names 0 smaller]:
   873   assumes A0: "!!x. V x = (0::nat) \<Longrightarrow> P x"
   874     and   A1: "!!x. V x > 0 \<Longrightarrow> \<not>P x \<Longrightarrow> (\<exists>y. V y < V x \<and> \<not>P y)"
   875   shows "P x"
   876 proof -
   877   obtain n where "n = V x" by auto
   878   moreover have "\<And>x. V x = n \<Longrightarrow> P x"
   879   proof (induct n rule: infinite_descent0)
   880     case 0 -- "i.e. $V(x) = 0$"
   881     with A0 show "P x" by auto
   882   next -- "now $n>0$ and $P(x)$ does not hold for some $x$ with $V(x)=n$"
   883     case (smaller n)
   884     then obtain x where vxn: "V x = n " and "V x > 0 \<and> \<not> P x" by auto
   885     with A1 obtain y where "V y < V x \<and> \<not> P y" by auto
   886     with vxn obtain m where "m = V y \<and> m<n \<and> \<not> P y" by auto
   887     then show ?case by auto
   888   qed
   889   ultimately show "P x" by auto
   890 qed
   891 
   892 text{* Again, without explicit base case: *}
   893 lemma infinite_descent_measure:
   894 assumes "!!x. \<not> P x \<Longrightarrow> \<exists>y. (V::'a\<Rightarrow>nat) y < V x \<and> \<not> P y" shows "P x"
   895 proof -
   896   from assms obtain n where "n = V x" by auto
   897   moreover have "!!x. V x = n \<Longrightarrow> P x"
   898   proof (induct n rule: infinite_descent, auto)
   899     fix x assume "\<not> P x"
   900     with assms show "\<exists>m < V x. \<exists>y. V y = m \<and> \<not> P y" by auto
   901   qed
   902   ultimately show "P x" by auto
   903 qed
   904 
   905 text {* A [clumsy] way of lifting @{text "<"}
   906   monotonicity to @{text "\<le>"} monotonicity *}
   907 lemma less_mono_imp_le_mono:
   908   "\<lbrakk> !!i j::nat. i < j \<Longrightarrow> f i < f j; i \<le> j \<rbrakk> \<Longrightarrow> f i \<le> ((f j)::nat)"
   909 by (simp add: order_le_less) (blast)
   910 
   911 
   912 text {* non-strict, in 1st argument *}
   913 lemma add_le_mono1: "i \<le> j ==> i + k \<le> j + (k::nat)"
   914 by (rule add_right_mono)
   915 
   916 text {* non-strict, in both arguments *}
   917 lemma add_le_mono: "[| i \<le> j;  k \<le> l |] ==> i + k \<le> j + (l::nat)"
   918 by (rule add_mono)
   919 
   920 lemma le_add2: "n \<le> ((m + n)::nat)"
   921 by (insert add_right_mono [of 0 m n], simp)
   922 
   923 lemma le_add1: "n \<le> ((n + m)::nat)"
   924 by (simp add: add_commute, rule le_add2)
   925 
   926 lemma less_add_Suc1: "i < Suc (i + m)"
   927 by (rule le_less_trans, rule le_add1, rule lessI)
   928 
   929 lemma less_add_Suc2: "i < Suc (m + i)"
   930 by (rule le_less_trans, rule le_add2, rule lessI)
   931 
   932 lemma less_iff_Suc_add: "(m < n) = (\<exists>k. n = Suc (m + k))"
   933 by (iprover intro!: less_add_Suc1 less_imp_Suc_add)
   934 
   935 lemma trans_le_add1: "(i::nat) \<le> j ==> i \<le> j + m"
   936 by (rule le_trans, assumption, rule le_add1)
   937 
   938 lemma trans_le_add2: "(i::nat) \<le> j ==> i \<le> m + j"
   939 by (rule le_trans, assumption, rule le_add2)
   940 
   941 lemma trans_less_add1: "(i::nat) < j ==> i < j + m"
   942 by (rule less_le_trans, assumption, rule le_add1)
   943 
   944 lemma trans_less_add2: "(i::nat) < j ==> i < m + j"
   945 by (rule less_le_trans, assumption, rule le_add2)
   946 
   947 lemma add_lessD1: "i + j < (k::nat) ==> i < k"
   948 apply (rule le_less_trans [of _ "i+j"])
   949 apply (simp_all add: le_add1)
   950 done
   951 
   952 lemma not_add_less1 [iff]: "~ (i + j < (i::nat))"
   953 apply (rule notI)
   954 apply (drule add_lessD1)
   955 apply (erule less_irrefl [THEN notE])
   956 done
   957 
   958 lemma not_add_less2 [iff]: "~ (j + i < (i::nat))"
   959 by (simp add: add_commute)
   960 
   961 lemma add_leD1: "m + k \<le> n ==> m \<le> (n::nat)"
   962 apply (rule order_trans [of _ "m+k"])
   963 apply (simp_all add: le_add1)
   964 done
   965 
   966 lemma add_leD2: "m + k \<le> n ==> k \<le> (n::nat)"
   967 apply (simp add: add_commute)
   968 apply (erule add_leD1)
   969 done
   970 
   971 lemma add_leE: "(m::nat) + k \<le> n ==> (m \<le> n ==> k \<le> n ==> R) ==> R"
   972 by (blast dest: add_leD1 add_leD2)
   973 
   974 text {* needs @{text "!!k"} for @{text add_ac} to work *}
   975 lemma less_add_eq_less: "!!k::nat. k < l ==> m + l = k + n ==> m < n"
   976 by (force simp del: add_Suc_right
   977     simp add: less_iff_Suc_add add_Suc_right [symmetric] add_ac)
   978 
   979 
   980 subsubsection {* More results about difference *}
   981 
   982 text {* Addition is the inverse of subtraction:
   983   if @{term "n \<le> m"} then @{term "n + (m - n) = m"}. *}
   984 lemma add_diff_inverse: "~  m < n ==> n + (m - n) = (m::nat)"
   985 by (induct m n rule: diff_induct) simp_all
   986 
   987 lemma le_add_diff_inverse [simp]: "n \<le> m ==> n + (m - n) = (m::nat)"
   988 by (simp add: add_diff_inverse linorder_not_less)
   989 
   990 lemma le_add_diff_inverse2 [simp]: "n \<le> m ==> (m - n) + n = (m::nat)"
   991 by (simp add: add_commute)
   992 
   993 lemma Suc_diff_le: "n \<le> m ==> Suc m - n = Suc (m - n)"
   994 by (induct m n rule: diff_induct) simp_all
   995 
   996 lemma diff_less_Suc: "m - n < Suc m"
   997 apply (induct m n rule: diff_induct)
   998 apply (erule_tac [3] less_SucE)
   999 apply (simp_all add: less_Suc_eq)
  1000 done
  1001 
  1002 lemma diff_le_self [simp]: "m - n \<le> (m::nat)"
  1003 by (induct m n rule: diff_induct) (simp_all add: le_SucI)
  1004 
  1005 lemma le_iff_add: "(m::nat) \<le> n = (\<exists>k. n = m + k)"
  1006   by (auto simp: le_add1 dest!: le_add_diff_inverse sym [of _ n])
  1007 
  1008 lemma less_imp_diff_less: "(j::nat) < k ==> j - n < k"
  1009 by (rule le_less_trans, rule diff_le_self)
  1010 
  1011 lemma diff_Suc_less [simp]: "0<n ==> n - Suc i < n"
  1012 by (cases n) (auto simp add: le_simps)
  1013 
  1014 lemma diff_add_assoc: "k \<le> (j::nat) ==> (i + j) - k = i + (j - k)"
  1015 by (induct j k rule: diff_induct) simp_all
  1016 
  1017 lemma diff_add_assoc2: "k \<le> (j::nat) ==> (j + i) - k = (j - k) + i"
  1018 by (simp add: add_commute diff_add_assoc)
  1019 
  1020 lemma le_imp_diff_is_add: "i \<le> (j::nat) ==> (j - i = k) = (j = k + i)"
  1021 by (auto simp add: diff_add_inverse2)
  1022 
  1023 lemma diff_is_0_eq [simp]: "((m::nat) - n = 0) = (m \<le> n)"
  1024 by (induct m n rule: diff_induct) simp_all
  1025 
  1026 lemma diff_is_0_eq' [simp]: "m \<le> n ==> (m::nat) - n = 0"
  1027 by (rule iffD2, rule diff_is_0_eq)
  1028 
  1029 lemma zero_less_diff [simp]: "(0 < n - (m::nat)) = (m < n)"
  1030 by (induct m n rule: diff_induct) simp_all
  1031 
  1032 lemma less_imp_add_positive:
  1033   assumes "i < j"
  1034   shows "\<exists>k::nat. 0 < k & i + k = j"
  1035 proof
  1036   from assms show "0 < j - i & i + (j - i) = j"
  1037     by (simp add: order_less_imp_le)
  1038 qed
  1039 
  1040 text {* a nice rewrite for bounded subtraction *}
  1041 lemma nat_minus_add_max:
  1042   fixes n m :: nat
  1043   shows "n - m + m = max n m"
  1044     by (simp add: max_def not_le order_less_imp_le)
  1045 
  1046 lemma nat_diff_split:
  1047   "P(a - b::nat) = ((a<b --> P 0) & (ALL d. a = b + d --> P d))"
  1048     -- {* elimination of @{text -} on @{text nat} *}
  1049 by (cases "a < b")
  1050   (auto simp add: diff_is_0_eq [THEN iffD2] diff_add_inverse
  1051     not_less le_less dest!: sym [of a] sym [of b] add_eq_self_zero)
  1052 
  1053 lemma nat_diff_split_asm:
  1054   "P(a - b::nat) = (~ (a < b & ~ P 0 | (EX d. a = b + d & ~ P d)))"
  1055     -- {* elimination of @{text -} on @{text nat} in assumptions *}
  1056 by (auto split: nat_diff_split)
  1057 
  1058 
  1059 subsubsection {* Monotonicity of Multiplication *}
  1060 
  1061 lemma mult_le_mono1: "i \<le> (j::nat) ==> i * k \<le> j * k"
  1062 by (simp add: mult_right_mono)
  1063 
  1064 lemma mult_le_mono2: "i \<le> (j::nat) ==> k * i \<le> k * j"
  1065 by (simp add: mult_left_mono)
  1066 
  1067 text {* @{text "\<le>"} monotonicity, BOTH arguments *}
  1068 lemma mult_le_mono: "i \<le> (j::nat) ==> k \<le> l ==> i * k \<le> j * l"
  1069 by (simp add: mult_mono)
  1070 
  1071 lemma mult_less_mono1: "(i::nat) < j ==> 0 < k ==> i * k < j * k"
  1072 by (simp add: mult_strict_right_mono)
  1073 
  1074 text{*Differs from the standard @{text zero_less_mult_iff} in that
  1075       there are no negative numbers.*}
  1076 lemma nat_0_less_mult_iff [simp]: "(0 < (m::nat) * n) = (0 < m & 0 < n)"
  1077   apply (induct m)
  1078    apply simp
  1079   apply (case_tac n)
  1080    apply simp_all
  1081   done
  1082 
  1083 lemma one_le_mult_iff [simp]: "(Suc 0 \<le> m * n) = (1 \<le> m & 1 \<le> n)"
  1084   apply (induct m)
  1085    apply simp
  1086   apply (case_tac n)
  1087    apply simp_all
  1088   done
  1089 
  1090 lemma mult_less_cancel2 [simp]: "((m::nat) * k < n * k) = (0 < k & m < n)"
  1091   apply (safe intro!: mult_less_mono1)
  1092   apply (case_tac k, auto)
  1093   apply (simp del: le_0_eq add: linorder_not_le [symmetric])
  1094   apply (blast intro: mult_le_mono1)
  1095   done
  1096 
  1097 lemma mult_less_cancel1 [simp]: "(k * (m::nat) < k * n) = (0 < k & m < n)"
  1098 by (simp add: mult_commute [of k])
  1099 
  1100 lemma mult_le_cancel1 [simp]: "(k * (m::nat) \<le> k * n) = (0 < k --> m \<le> n)"
  1101 by (simp add: linorder_not_less [symmetric], auto)
  1102 
  1103 lemma mult_le_cancel2 [simp]: "((m::nat) * k \<le> n * k) = (0 < k --> m \<le> n)"
  1104 by (simp add: linorder_not_less [symmetric], auto)
  1105 
  1106 lemma Suc_mult_less_cancel1: "(Suc k * m < Suc k * n) = (m < n)"
  1107 by (subst mult_less_cancel1) simp
  1108 
  1109 lemma Suc_mult_le_cancel1: "(Suc k * m \<le> Suc k * n) = (m \<le> n)"
  1110 by (subst mult_le_cancel1) simp
  1111 
  1112 lemma le_square: "m \<le> m * (m::nat)"
  1113   by (cases m) (auto intro: le_add1)
  1114 
  1115 lemma le_cube: "(m::nat) \<le> m * (m * m)"
  1116   by (cases m) (auto intro: le_add1)
  1117 
  1118 text {* Lemma for @{text gcd} *}
  1119 lemma mult_eq_self_implies_10: "(m::nat) = m * n ==> n = 1 | m = 0"
  1120   apply (drule sym)
  1121   apply (rule disjCI)
  1122   apply (rule nat_less_cases, erule_tac [2] _)
  1123    apply (drule_tac [2] mult_less_mono2)
  1124     apply (auto)
  1125   done
  1126 
  1127 text {* the lattice order on @{typ nat} *}
  1128 
  1129 instantiation nat :: distrib_lattice
  1130 begin
  1131 
  1132 definition
  1133   "(inf \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat) = min"
  1134 
  1135 definition
  1136   "(sup \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat) = max"
  1137 
  1138 instance by intro_classes
  1139   (auto simp add: inf_nat_def sup_nat_def max_def not_le min_def
  1140     intro: order_less_imp_le antisym elim!: order_trans order_less_trans)
  1141 
  1142 end
  1143 
  1144 
  1145 subsection {* Embedding of the Naturals into any
  1146   @{text semiring_1}: @{term of_nat} *}
  1147 
  1148 context semiring_1
  1149 begin
  1150 
  1151 primrec
  1152   of_nat :: "nat \<Rightarrow> 'a"
  1153 where
  1154   of_nat_0:     "of_nat 0 = 0"
  1155   | of_nat_Suc: "of_nat (Suc m) = 1 + of_nat m"
  1156 
  1157 lemma of_nat_1 [simp]: "of_nat 1 = 1"
  1158   by simp
  1159 
  1160 lemma of_nat_add [simp]: "of_nat (m + n) = of_nat m + of_nat n"
  1161   by (induct m) (simp_all add: add_ac)
  1162 
  1163 lemma of_nat_mult: "of_nat (m * n) = of_nat m * of_nat n"
  1164   by (induct m) (simp_all add: add_ac left_distrib)
  1165 
  1166 primrec of_nat_aux :: "('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a" where
  1167   "of_nat_aux inc 0 i = i"
  1168   | "of_nat_aux inc (Suc n) i = of_nat_aux inc n (inc i)" -- {* tail recursive *}
  1169 
  1170 lemma of_nat_code [code, code unfold, code inline del]:
  1171   "of_nat n = of_nat_aux (\<lambda>i. i + 1) n 0"
  1172 proof (induct n)
  1173   case 0 then show ?case by simp
  1174 next
  1175   case (Suc n)
  1176   have "\<And>i. of_nat_aux (\<lambda>i. i + 1) n (i + 1) = of_nat_aux (\<lambda>i. i + 1) n i + 1"
  1177     by (induct n) simp_all
  1178   from this [of 0] have "of_nat_aux (\<lambda>i. i + 1) n 1 = of_nat_aux (\<lambda>i. i + 1) n 0 + 1"
  1179     by simp
  1180   with Suc show ?case by (simp add: add_commute)
  1181 qed
  1182     
  1183 end
  1184 
  1185 text{*Class for unital semirings with characteristic zero.
  1186  Includes non-ordered rings like the complex numbers.*}
  1187 
  1188 class semiring_char_0 = semiring_1 +
  1189   assumes of_nat_eq_iff [simp]: "of_nat m = of_nat n \<longleftrightarrow> m = n"
  1190 begin
  1191 
  1192 text{*Special cases where either operand is zero*}
  1193 
  1194 lemma of_nat_0_eq_iff [simp, noatp]: "0 = of_nat n \<longleftrightarrow> 0 = n"
  1195   by (rule of_nat_eq_iff [of 0, simplified])
  1196 
  1197 lemma of_nat_eq_0_iff [simp, noatp]: "of_nat m = 0 \<longleftrightarrow> m = 0"
  1198   by (rule of_nat_eq_iff [of _ 0, simplified])
  1199 
  1200 lemma inj_of_nat: "inj of_nat"
  1201   by (simp add: inj_on_def)
  1202 
  1203 end
  1204 
  1205 context ordered_semidom
  1206 begin
  1207 
  1208 lemma zero_le_imp_of_nat: "0 \<le> of_nat m"
  1209   apply (induct m, simp_all)
  1210   apply (erule order_trans)
  1211   apply (rule ord_le_eq_trans [OF _ add_commute])
  1212   apply (rule less_add_one [THEN less_imp_le])
  1213   done
  1214 
  1215 lemma less_imp_of_nat_less: "m < n \<Longrightarrow> of_nat m < of_nat n"
  1216   apply (induct m n rule: diff_induct, simp_all)
  1217   apply (insert add_less_le_mono [OF zero_less_one zero_le_imp_of_nat], force)
  1218   done
  1219 
  1220 lemma of_nat_less_imp_less: "of_nat m < of_nat n \<Longrightarrow> m < n"
  1221   apply (induct m n rule: diff_induct, simp_all)
  1222   apply (insert zero_le_imp_of_nat)
  1223   apply (force simp add: not_less [symmetric])
  1224   done
  1225 
  1226 lemma of_nat_less_iff [simp]: "of_nat m < of_nat n \<longleftrightarrow> m < n"
  1227   by (blast intro: of_nat_less_imp_less less_imp_of_nat_less)
  1228 
  1229 lemma of_nat_le_iff [simp]: "of_nat m \<le> of_nat n \<longleftrightarrow> m \<le> n"
  1230   by (simp add: not_less [symmetric] linorder_not_less [symmetric])
  1231 
  1232 text{*Every @{text ordered_semidom} has characteristic zero.*}
  1233 
  1234 subclass semiring_char_0
  1235   proof qed (simp add: eq_iff order_eq_iff)
  1236 
  1237 text{*Special cases where either operand is zero*}
  1238 
  1239 lemma of_nat_0_le_iff [simp]: "0 \<le> of_nat n"
  1240   by (rule of_nat_le_iff [of 0, simplified])
  1241 
  1242 lemma of_nat_le_0_iff [simp, noatp]: "of_nat m \<le> 0 \<longleftrightarrow> m = 0"
  1243   by (rule of_nat_le_iff [of _ 0, simplified])
  1244 
  1245 lemma of_nat_0_less_iff [simp]: "0 < of_nat n \<longleftrightarrow> 0 < n"
  1246   by (rule of_nat_less_iff [of 0, simplified])
  1247 
  1248 lemma of_nat_less_0_iff [simp]: "\<not> of_nat m < 0"
  1249   by (rule of_nat_less_iff [of _ 0, simplified])
  1250 
  1251 end
  1252 
  1253 context ring_1
  1254 begin
  1255 
  1256 lemma of_nat_diff: "n \<le> m \<Longrightarrow> of_nat (m - n) = of_nat m - of_nat n"
  1257   by (simp add: compare_rls of_nat_add [symmetric])
  1258 
  1259 end
  1260 
  1261 context ordered_idom
  1262 begin
  1263 
  1264 lemma abs_of_nat [simp]: "\<bar>of_nat n\<bar> = of_nat n"
  1265   unfolding abs_if by auto
  1266 
  1267 end
  1268 
  1269 lemma of_nat_id [simp]: "of_nat n = n"
  1270   by (induct n) auto
  1271 
  1272 lemma of_nat_eq_id [simp]: "of_nat = id"
  1273   by (auto simp add: expand_fun_eq)
  1274 
  1275 
  1276 subsection {* The Set of Natural Numbers *}
  1277 
  1278 context semiring_1
  1279 begin
  1280 
  1281 definition
  1282   Nats  :: "'a set" where
  1283   [code del]: "Nats = range of_nat"
  1284 
  1285 notation (xsymbols)
  1286   Nats  ("\<nat>")
  1287 
  1288 lemma of_nat_in_Nats [simp]: "of_nat n \<in> \<nat>"
  1289   by (simp add: Nats_def)
  1290 
  1291 lemma Nats_0 [simp]: "0 \<in> \<nat>"
  1292 apply (simp add: Nats_def)
  1293 apply (rule range_eqI)
  1294 apply (rule of_nat_0 [symmetric])
  1295 done
  1296 
  1297 lemma Nats_1 [simp]: "1 \<in> \<nat>"
  1298 apply (simp add: Nats_def)
  1299 apply (rule range_eqI)
  1300 apply (rule of_nat_1 [symmetric])
  1301 done
  1302 
  1303 lemma Nats_add [simp]: "a \<in> \<nat> \<Longrightarrow> b \<in> \<nat> \<Longrightarrow> a + b \<in> \<nat>"
  1304 apply (auto simp add: Nats_def)
  1305 apply (rule range_eqI)
  1306 apply (rule of_nat_add [symmetric])
  1307 done
  1308 
  1309 lemma Nats_mult [simp]: "a \<in> \<nat> \<Longrightarrow> b \<in> \<nat> \<Longrightarrow> a * b \<in> \<nat>"
  1310 apply (auto simp add: Nats_def)
  1311 apply (rule range_eqI)
  1312 apply (rule of_nat_mult [symmetric])
  1313 done
  1314 
  1315 end
  1316 
  1317 
  1318 subsection {* Further Arithmetic Facts Concerning the Natural Numbers *}
  1319 
  1320 lemma subst_equals:
  1321   assumes 1: "t = s" and 2: "u = t"
  1322   shows "u = s"
  1323   using 2 1 by (rule trans)
  1324 
  1325 use "Tools/arith_data.ML"
  1326 declaration {* K ArithData.setup *}
  1327 
  1328 use "Tools/lin_arith.ML"
  1329 declaration {* K LinArith.setup *}
  1330 
  1331 lemmas [arith_split] = nat_diff_split split_min split_max
  1332 
  1333 
  1334 context order
  1335 begin
  1336 
  1337 lemma lift_Suc_mono_le:
  1338   assumes mono: "!!n. f n \<le> f(Suc n)" and "n\<le>n'"
  1339   shows "f n \<le> f n'"
  1340 proof (cases "n < n'")
  1341   case True
  1342   thus ?thesis
  1343     by (induct n n' rule: less_Suc_induct[consumes 1]) (auto intro: mono)
  1344 qed (insert `n \<le> n'`, auto) -- {*trivial for @{prop "n = n'"} *}
  1345 
  1346 lemma lift_Suc_mono_less:
  1347   assumes mono: "!!n. f n < f(Suc n)" and "n < n'"
  1348   shows "f n < f n'"
  1349 using `n < n'`
  1350 by (induct n n' rule: less_Suc_induct[consumes 1]) (auto intro: mono)
  1351 
  1352 lemma lift_Suc_mono_less_iff:
  1353   "(!!n. f n < f(Suc n)) \<Longrightarrow> f(n) < f(m) \<longleftrightarrow> n<m"
  1354 by(blast intro: less_asym' lift_Suc_mono_less[of f]
  1355          dest: linorder_not_less[THEN iffD1] le_eq_less_or_eq[THEN iffD1])
  1356 
  1357 end
  1358 
  1359 
  1360 lemma mono_nat_linear_lb:
  1361   "(!!m n::nat. m<n \<Longrightarrow> f m < f n) \<Longrightarrow> f(m)+k \<le> f(m+k)"
  1362 apply(induct_tac k)
  1363  apply simp
  1364 apply(erule_tac x="m+n" in meta_allE)
  1365 apply(erule_tac x="m+n+1" in meta_allE)
  1366 apply simp
  1367 done
  1368 
  1369 
  1370 text{*Subtraction laws, mostly by Clemens Ballarin*}
  1371 
  1372 lemma diff_less_mono: "[| a < (b::nat); c \<le> a |] ==> a-c < b-c"
  1373 by arith
  1374 
  1375 lemma less_diff_conv: "(i < j-k) = (i+k < (j::nat))"
  1376 by arith
  1377 
  1378 lemma le_diff_conv: "(j-k \<le> (i::nat)) = (j \<le> i+k)"
  1379 by arith
  1380 
  1381 lemma le_diff_conv2: "k \<le> j ==> (i \<le> j-k) = (i+k \<le> (j::nat))"
  1382 by arith
  1383 
  1384 lemma diff_diff_cancel [simp]: "i \<le> (n::nat) ==> n - (n - i) = i"
  1385 by arith
  1386 
  1387 lemma le_add_diff: "k \<le> (n::nat) ==> m \<le> n + m - k"
  1388 by arith
  1389 
  1390 (*Replaces the previous diff_less and le_diff_less, which had the stronger
  1391   second premise n\<le>m*)
  1392 lemma diff_less[simp]: "!!m::nat. [| 0<n; 0<m |] ==> m - n < m"
  1393 by arith
  1394 
  1395 text {* Simplification of relational expressions involving subtraction *}
  1396 
  1397 lemma diff_diff_eq: "[| k \<le> m;  k \<le> (n::nat) |] ==> ((m-k) - (n-k)) = (m-n)"
  1398 by (simp split add: nat_diff_split)
  1399 
  1400 lemma eq_diff_iff: "[| k \<le> m;  k \<le> (n::nat) |] ==> (m-k = n-k) = (m=n)"
  1401 by (auto split add: nat_diff_split)
  1402 
  1403 lemma less_diff_iff: "[| k \<le> m;  k \<le> (n::nat) |] ==> (m-k < n-k) = (m<n)"
  1404 by (auto split add: nat_diff_split)
  1405 
  1406 lemma le_diff_iff: "[| k \<le> m;  k \<le> (n::nat) |] ==> (m-k \<le> n-k) = (m\<le>n)"
  1407 by (auto split add: nat_diff_split)
  1408 
  1409 text{*(Anti)Monotonicity of subtraction -- by Stephan Merz*}
  1410 
  1411 (* Monotonicity of subtraction in first argument *)
  1412 lemma diff_le_mono: "m \<le> (n::nat) ==> (m-l) \<le> (n-l)"
  1413 by (simp split add: nat_diff_split)
  1414 
  1415 lemma diff_le_mono2: "m \<le> (n::nat) ==> (l-n) \<le> (l-m)"
  1416 by (simp split add: nat_diff_split)
  1417 
  1418 lemma diff_less_mono2: "[| m < (n::nat); m<l |] ==> (l-n) < (l-m)"
  1419 by (simp split add: nat_diff_split)
  1420 
  1421 lemma diffs0_imp_equal: "!!m::nat. [| m-n = 0; n-m = 0 |] ==>  m=n"
  1422 by (simp split add: nat_diff_split)
  1423 
  1424 lemma min_diff: "min (m - (i::nat)) (n - i) = min m n - i"
  1425 unfolding min_def by auto
  1426 
  1427 lemma inj_on_diff_nat: 
  1428   assumes k_le_n: "\<forall>n \<in> N. k \<le> (n::nat)"
  1429   shows "inj_on (\<lambda>n. n - k) N"
  1430 proof (rule inj_onI)
  1431   fix x y
  1432   assume a: "x \<in> N" "y \<in> N" "x - k = y - k"
  1433   with k_le_n have "x - k + k = y - k + k" by auto
  1434   with a k_le_n show "x = y" by auto
  1435 qed
  1436 
  1437 text{*Rewriting to pull differences out*}
  1438 
  1439 lemma diff_diff_right [simp]: "k\<le>j --> i - (j - k) = i + (k::nat) - j"
  1440 by arith
  1441 
  1442 lemma diff_Suc_diff_eq1 [simp]: "k \<le> j ==> m - Suc (j - k) = m + k - Suc j"
  1443 by arith
  1444 
  1445 lemma diff_Suc_diff_eq2 [simp]: "k \<le> j ==> Suc (j - k) - m = Suc j - (k + m)"
  1446 by arith
  1447 
  1448 text{*Lemmas for ex/Factorization*}
  1449 
  1450 lemma one_less_mult: "[| Suc 0 < n; Suc 0 < m |] ==> Suc 0 < m*n"
  1451 by (cases m) auto
  1452 
  1453 lemma n_less_m_mult_n: "[| Suc 0 < n; Suc 0 < m |] ==> n<m*n"
  1454 by (cases m) auto
  1455 
  1456 lemma n_less_n_mult_m: "[| Suc 0 < n; Suc 0 < m |] ==> n<n*m"
  1457 by (cases m) auto
  1458 
  1459 text {* Specialized induction principles that work "backwards": *}
  1460 
  1461 lemma inc_induct[consumes 1, case_names base step]:
  1462   assumes less: "i <= j"
  1463   assumes base: "P j"
  1464   assumes step: "!!i. [| i < j; P (Suc i) |] ==> P i"
  1465   shows "P i"
  1466   using less
  1467 proof (induct d=="j - i" arbitrary: i)
  1468   case (0 i)
  1469   hence "i = j" by simp
  1470   with base show ?case by simp
  1471 next
  1472   case (Suc d i)
  1473   hence "i < j" "P (Suc i)"
  1474     by simp_all
  1475   thus "P i" by (rule step)
  1476 qed
  1477 
  1478 lemma strict_inc_induct[consumes 1, case_names base step]:
  1479   assumes less: "i < j"
  1480   assumes base: "!!i. j = Suc i ==> P i"
  1481   assumes step: "!!i. [| i < j; P (Suc i) |] ==> P i"
  1482   shows "P i"
  1483   using less
  1484 proof (induct d=="j - i - 1" arbitrary: i)
  1485   case (0 i)
  1486   with `i < j` have "j = Suc i" by simp
  1487   with base show ?case by simp
  1488 next
  1489   case (Suc d i)
  1490   hence "i < j" "P (Suc i)"
  1491     by simp_all
  1492   thus "P i" by (rule step)
  1493 qed
  1494 
  1495 lemma zero_induct_lemma: "P k ==> (!!n. P (Suc n) ==> P n) ==> P (k - i)"
  1496   using inc_induct[of "k - i" k P, simplified] by blast
  1497 
  1498 lemma zero_induct: "P k ==> (!!n. P (Suc n) ==> P n) ==> P 0"
  1499   using inc_induct[of 0 k P] by blast
  1500 
  1501 lemma nat_not_singleton: "(\<forall>x. x = (0::nat)) = False"
  1502   by auto
  1503 
  1504 (*The others are
  1505       i - j - k = i - (j + k),
  1506       k \<le> j ==> j - k + i = j + i - k,
  1507       k \<le> j ==> i + (j - k) = i + j - k *)
  1508 lemmas add_diff_assoc = diff_add_assoc [symmetric]
  1509 lemmas add_diff_assoc2 = diff_add_assoc2[symmetric]
  1510 declare diff_diff_left [simp]  add_diff_assoc [simp] add_diff_assoc2[simp]
  1511 
  1512 text{*At present we prove no analogue of @{text not_less_Least} or @{text
  1513 Least_Suc}, since there appears to be no need.*}
  1514 
  1515 
  1516 subsection {* size of a datatype value *}
  1517 
  1518 class size = type +
  1519   fixes size :: "'a \<Rightarrow> nat" -- {* see further theory @{text Wellfounded} *}
  1520 
  1521 end