src/HOL/Nat.thy
author paulson
Sat Dec 27 21:02:14 2003 +0100 (2003-12-27)
changeset 14331 8dbbb7cf3637
parent 14302 6c24235e8d5d
child 14341 a09441bd4f1e
permissions -rw-r--r--
re-organized numeric lemmas
     1 (*  Title:      HOL/Nat.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow and Lawrence C Paulson
     4 
     5 Type "nat" is a linear order, and a datatype; arithmetic operators + -
     6 and * (for div, mod and dvd, see theory Divides).
     7 *)
     8 
     9 header {* Natural numbers *}
    10 
    11 theory Nat = Wellfounded_Recursion + Ring_and_Field:
    12 
    13 subsection {* Type @{text ind} *}
    14 
    15 typedecl ind
    16 
    17 consts
    18   Zero_Rep      :: ind
    19   Suc_Rep       :: "ind => ind"
    20 
    21 axioms
    22   -- {* the axiom of infinity in 2 parts *}
    23   inj_Suc_Rep:          "inj Suc_Rep"
    24   Suc_Rep_not_Zero_Rep: "Suc_Rep x \<noteq> Zero_Rep"
    25 
    26 
    27 subsection {* Type nat *}
    28 
    29 text {* Type definition *}
    30 
    31 consts
    32   Nat :: "ind set"
    33 
    34 inductive Nat
    35 intros
    36   Zero_RepI: "Zero_Rep : Nat"
    37   Suc_RepI: "i : Nat ==> Suc_Rep i : Nat"
    38 
    39 global
    40 
    41 typedef (open Nat)
    42   nat = Nat by (rule exI, rule Nat.Zero_RepI)
    43 
    44 instance nat :: ord ..
    45 instance nat :: zero ..
    46 instance nat :: one ..
    47 
    48 
    49 text {* Abstract constants and syntax *}
    50 
    51 consts
    52   Suc :: "nat => nat"
    53   pred_nat :: "(nat * nat) set"
    54 
    55 local
    56 
    57 defs
    58   Zero_nat_def: "0 == Abs_Nat Zero_Rep"
    59   Suc_def: "Suc == (%n. Abs_Nat (Suc_Rep (Rep_Nat n)))"
    60   One_nat_def [simp]: "1 == Suc 0"
    61 
    62   -- {* nat operations *}
    63   pred_nat_def: "pred_nat == {(m, n). n = Suc m}"
    64 
    65   less_def: "m < n == (m, n) : trancl pred_nat"
    66 
    67   le_def: "m \<le> (n::nat) == ~ (n < m)"
    68 
    69 
    70 text {* Induction *}
    71 
    72 theorem nat_induct: "P 0 ==> (!!n. P n ==> P (Suc n)) ==> P n"
    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 [THEN Nat.induct])
    76   apply (rules elim: Abs_Nat_inverse [THEN subst])
    77   done
    78 
    79 
    80 text {* Isomorphisms: @{text Abs_Nat} and @{text Rep_Nat} *}
    81 
    82 lemma inj_Rep_Nat: "inj Rep_Nat"
    83   apply (rule inj_on_inverseI)
    84   apply (rule Rep_Nat_inverse)
    85   done
    86 
    87 lemma inj_on_Abs_Nat: "inj_on Abs_Nat Nat"
    88   apply (rule inj_on_inverseI)
    89   apply (erule Abs_Nat_inverse)
    90   done
    91 
    92 text {* Distinctness of constructors *}
    93 
    94 lemma Suc_not_Zero [iff]: "Suc m \<noteq> 0"
    95   apply (unfold Zero_nat_def Suc_def)
    96   apply (rule inj_on_Abs_Nat [THEN inj_on_contraD])
    97   apply (rule Suc_Rep_not_Zero_Rep)
    98   apply (rule Rep_Nat Nat.Suc_RepI Nat.Zero_RepI)+
    99   done
   100 
   101 lemma Zero_not_Suc [iff]: "0 \<noteq> Suc m"
   102   by (rule not_sym, rule Suc_not_Zero not_sym)
   103 
   104 lemma Suc_neq_Zero: "Suc m = 0 ==> R"
   105   by (rule notE, rule Suc_not_Zero)
   106 
   107 lemma Zero_neq_Suc: "0 = Suc m ==> R"
   108   by (rule Suc_neq_Zero, erule sym)
   109 
   110 text {* Injectiveness of @{term Suc} *}
   111 
   112 lemma inj_Suc: "inj Suc"
   113   apply (unfold Suc_def)
   114   apply (rule inj_onI)
   115   apply (drule inj_on_Abs_Nat [THEN inj_onD])
   116   apply (rule Rep_Nat Nat.Suc_RepI)+
   117   apply (drule inj_Suc_Rep [THEN injD])
   118   apply (erule inj_Rep_Nat [THEN injD])
   119   done
   120 
   121 lemma Suc_inject: "Suc x = Suc y ==> x = y"
   122   by (rule inj_Suc [THEN injD])
   123 
   124 lemma Suc_Suc_eq [iff]: "(Suc m = Suc n) = (m = n)"
   125   apply (rule iffI)
   126   apply (erule Suc_inject)
   127   apply (erule arg_cong)
   128   done
   129 
   130 lemma nat_not_singleton: "(\<forall>x. x = (0::nat)) = False"
   131   by auto
   132 
   133 text {* @{typ nat} is a datatype *}
   134 
   135 rep_datatype nat
   136   distinct  Suc_not_Zero Zero_not_Suc
   137   inject    Suc_Suc_eq
   138   induction nat_induct
   139 
   140 lemma n_not_Suc_n: "n \<noteq> Suc n"
   141   by (induct n) simp_all
   142 
   143 lemma Suc_n_not_n: "Suc t \<noteq> t"
   144   by (rule not_sym, rule n_not_Suc_n)
   145 
   146 text {* A special form of induction for reasoning
   147   about @{term "m < n"} and @{term "m - n"} *}
   148 
   149 theorem diff_induct: "(!!x. P x 0) ==> (!!y. P 0 (Suc y)) ==>
   150     (!!x y. P x y ==> P (Suc x) (Suc y)) ==> P m n"
   151   apply (rule_tac x = m in spec)
   152   apply (induct_tac n)
   153   prefer 2
   154   apply (rule allI)
   155   apply (induct_tac x, rules+)
   156   done
   157 
   158 subsection {* Basic properties of "less than" *}
   159 
   160 lemma wf_pred_nat: "wf pred_nat"
   161   apply (unfold wf_def pred_nat_def, clarify)
   162   apply (induct_tac x, blast+)
   163   done
   164 
   165 lemma wf_less: "wf {(x, y::nat). x < y}"
   166   apply (unfold less_def)
   167   apply (rule wf_pred_nat [THEN wf_trancl, THEN wf_subset], blast)
   168   done
   169 
   170 lemma less_eq: "((m, n) : pred_nat^+) = (m < n)"
   171   apply (unfold less_def)
   172   apply (rule refl)
   173   done
   174 
   175 subsubsection {* Introduction properties *}
   176 
   177 lemma less_trans: "i < j ==> j < k ==> i < (k::nat)"
   178   apply (unfold less_def)
   179   apply (rule trans_trancl [THEN transD], assumption+)
   180   done
   181 
   182 lemma lessI [iff]: "n < Suc n"
   183   apply (unfold less_def pred_nat_def)
   184   apply (simp add: r_into_trancl)
   185   done
   186 
   187 lemma less_SucI: "i < j ==> i < Suc j"
   188   apply (rule less_trans, assumption)
   189   apply (rule lessI)
   190   done
   191 
   192 lemma zero_less_Suc [iff]: "0 < Suc n"
   193   apply (induct n)
   194   apply (rule lessI)
   195   apply (erule less_trans)
   196   apply (rule lessI)
   197   done
   198 
   199 subsubsection {* Elimination properties *}
   200 
   201 lemma less_not_sym: "n < m ==> ~ m < (n::nat)"
   202   apply (unfold less_def)
   203   apply (blast intro: wf_pred_nat wf_trancl [THEN wf_asym])
   204   done
   205 
   206 lemma less_asym:
   207   assumes h1: "(n::nat) < m" and h2: "~ P ==> m < n" shows P
   208   apply (rule contrapos_np)
   209   apply (rule less_not_sym)
   210   apply (rule h1)
   211   apply (erule h2)
   212   done
   213 
   214 lemma less_not_refl: "~ n < (n::nat)"
   215   apply (unfold less_def)
   216   apply (rule wf_pred_nat [THEN wf_trancl, THEN wf_not_refl])
   217   done
   218 
   219 lemma less_irrefl [elim!]: "(n::nat) < n ==> R"
   220   by (rule notE, rule less_not_refl)
   221 
   222 lemma less_not_refl2: "n < m ==> m \<noteq> (n::nat)" by blast
   223 
   224 lemma less_not_refl3: "(s::nat) < t ==> s \<noteq> t"
   225   by (rule not_sym, rule less_not_refl2)
   226 
   227 lemma lessE:
   228   assumes major: "i < k"
   229   and p1: "k = Suc i ==> P" and p2: "!!j. i < j ==> k = Suc j ==> P"
   230   shows P
   231   apply (rule major [unfolded less_def pred_nat_def, THEN tranclE], simp_all)
   232   apply (erule p1)
   233   apply (rule p2)
   234   apply (simp add: less_def pred_nat_def, assumption)
   235   done
   236 
   237 lemma not_less0 [iff]: "~ n < (0::nat)"
   238   by (blast elim: lessE)
   239 
   240 lemma less_zeroE: "(n::nat) < 0 ==> R"
   241   by (rule notE, rule not_less0)
   242 
   243 lemma less_SucE: assumes major: "m < Suc n"
   244   and less: "m < n ==> P" and eq: "m = n ==> P" shows P
   245   apply (rule major [THEN lessE])
   246   apply (rule eq, blast)
   247   apply (rule less, blast)
   248   done
   249 
   250 lemma less_Suc_eq: "(m < Suc n) = (m < n | m = n)"
   251   by (blast elim!: less_SucE intro: less_trans)
   252 
   253 lemma less_one [iff]: "(n < (1::nat)) = (n = 0)"
   254   by (simp add: less_Suc_eq)
   255 
   256 lemma less_Suc0 [iff]: "(n < Suc 0) = (n = 0)"
   257   by (simp add: less_Suc_eq)
   258 
   259 lemma Suc_mono: "m < n ==> Suc m < Suc n"
   260   by (induct n) (fast elim: less_trans lessE)+
   261 
   262 text {* "Less than" is a linear ordering *}
   263 lemma less_linear: "m < n | m = n | n < (m::nat)"
   264   apply (induct_tac m)
   265   apply (induct_tac n)
   266   apply (rule refl [THEN disjI1, THEN disjI2])
   267   apply (rule zero_less_Suc [THEN disjI1])
   268   apply (blast intro: Suc_mono less_SucI elim: lessE)
   269   done
   270 
   271 text {* "Less than" is antisymmetric, sort of *}
   272 lemma less_antisym: "\<lbrakk> \<not> n < m; n < Suc m \<rbrakk> \<Longrightarrow> m = n"
   273 apply(simp only:less_Suc_eq)
   274 apply blast
   275 done
   276 
   277 lemma nat_neq_iff: "((m::nat) \<noteq> n) = (m < n | n < m)"
   278   using less_linear by blast
   279 
   280 lemma nat_less_cases: assumes major: "(m::nat) < n ==> P n m"
   281   and eqCase: "m = n ==> P n m" and lessCase: "n<m ==> P n m"
   282   shows "P n m"
   283   apply (rule less_linear [THEN disjE])
   284   apply (erule_tac [2] disjE)
   285   apply (erule lessCase)
   286   apply (erule sym [THEN eqCase])
   287   apply (erule major)
   288   done
   289 
   290 
   291 subsubsection {* Inductive (?) properties *}
   292 
   293 lemma Suc_lessI: "m < n ==> Suc m \<noteq> n ==> Suc m < n"
   294   apply (simp add: nat_neq_iff)
   295   apply (blast elim!: less_irrefl less_SucE elim: less_asym)
   296   done
   297 
   298 lemma Suc_lessD: "Suc m < n ==> m < n"
   299   apply (induct n)
   300   apply (fast intro!: lessI [THEN less_SucI] elim: less_trans lessE)+
   301   done
   302 
   303 lemma Suc_lessE: assumes major: "Suc i < k"
   304   and minor: "!!j. i < j ==> k = Suc j ==> P" shows P
   305   apply (rule major [THEN lessE])
   306   apply (erule lessI [THEN minor])
   307   apply (erule Suc_lessD [THEN minor], assumption)
   308   done
   309 
   310 lemma Suc_less_SucD: "Suc m < Suc n ==> m < n"
   311   by (blast elim: lessE dest: Suc_lessD)
   312 
   313 lemma Suc_less_eq [iff]: "(Suc m < Suc n) = (m < n)"
   314   apply (rule iffI)
   315   apply (erule Suc_less_SucD)
   316   apply (erule Suc_mono)
   317   done
   318 
   319 lemma less_trans_Suc:
   320   assumes le: "i < j" shows "j < k ==> Suc i < k"
   321   apply (induct k, simp_all)
   322   apply (insert le)
   323   apply (simp add: less_Suc_eq)
   324   apply (blast dest: Suc_lessD)
   325   done
   326 
   327 text {* Can be used with @{text less_Suc_eq} to get @{term "n = m | n < m"} *}
   328 lemma not_less_eq: "(~ m < n) = (n < Suc m)"
   329 by (rule_tac m = m and n = n in diff_induct, simp_all)
   330 
   331 text {* Complete induction, aka course-of-values induction *}
   332 lemma nat_less_induct:
   333   assumes prem: "!!n. \<forall>m::nat. m < n --> P m ==> P n" shows "P n"
   334   apply (rule_tac a=n in wf_induct)
   335   apply (rule wf_pred_nat [THEN wf_trancl])
   336   apply (rule prem)
   337   apply (unfold less_def, assumption)
   338   done
   339 
   340 lemmas less_induct = nat_less_induct [rule_format, case_names less]
   341 
   342 subsection {* Properties of "less than or equal" *}
   343 
   344 text {* Was @{text le_eq_less_Suc}, but this orientation is more useful *}
   345 lemma less_Suc_eq_le: "(m < Suc n) = (m \<le> n)"
   346   by (unfold le_def, rule not_less_eq [symmetric])
   347 
   348 lemma le_imp_less_Suc: "m \<le> n ==> m < Suc n"
   349   by (rule less_Suc_eq_le [THEN iffD2])
   350 
   351 lemma le0 [iff]: "(0::nat) \<le> n"
   352   by (unfold le_def, rule not_less0)
   353 
   354 lemma Suc_n_not_le_n: "~ Suc n \<le> n"
   355   by (simp add: le_def)
   356 
   357 lemma le_0_eq [iff]: "((i::nat) \<le> 0) = (i = 0)"
   358   by (induct i) (simp_all add: le_def)
   359 
   360 lemma le_Suc_eq: "(m \<le> Suc n) = (m \<le> n | m = Suc n)"
   361   by (simp del: less_Suc_eq_le add: less_Suc_eq_le [symmetric] less_Suc_eq)
   362 
   363 lemma le_SucE: "m \<le> Suc n ==> (m \<le> n ==> R) ==> (m = Suc n ==> R) ==> R"
   364   by (drule le_Suc_eq [THEN iffD1], rules+)
   365 
   366 lemma leI: "~ n < m ==> m \<le> (n::nat)" by (simp add: le_def)
   367 
   368 lemma leD: "m \<le> n ==> ~ n < (m::nat)"
   369   by (simp add: le_def)
   370 
   371 lemmas leE = leD [elim_format]
   372 
   373 lemma not_less_iff_le: "(~ n < m) = (m \<le> (n::nat))"
   374   by (blast intro: leI elim: leE)
   375 
   376 lemma not_leE: "~ m \<le> n ==> n<(m::nat)"
   377   by (simp add: le_def)
   378 
   379 lemma not_le_iff_less: "(~ n \<le> m) = (m < (n::nat))"
   380   by (simp add: le_def)
   381 
   382 lemma Suc_leI: "m < n ==> Suc(m) \<le> n"
   383   apply (simp add: le_def less_Suc_eq)
   384   apply (blast elim!: less_irrefl less_asym)
   385   done -- {* formerly called lessD *}
   386 
   387 lemma Suc_leD: "Suc(m) \<le> n ==> m \<le> n"
   388   by (simp add: le_def less_Suc_eq)
   389 
   390 text {* Stronger version of @{text Suc_leD} *}
   391 lemma Suc_le_lessD: "Suc m \<le> n ==> m < n"
   392   apply (simp add: le_def less_Suc_eq)
   393   using less_linear
   394   apply blast
   395   done
   396 
   397 lemma Suc_le_eq: "(Suc m \<le> n) = (m < n)"
   398   by (blast intro: Suc_leI Suc_le_lessD)
   399 
   400 lemma le_SucI: "m \<le> n ==> m \<le> Suc n"
   401   by (unfold le_def) (blast dest: Suc_lessD)
   402 
   403 lemma less_imp_le: "m < n ==> m \<le> (n::nat)"
   404   by (unfold le_def) (blast elim: less_asym)
   405 
   406 text {* For instance, @{text "(Suc m < Suc n) = (Suc m \<le> n) = (m < n)"} *}
   407 lemmas le_simps = less_imp_le less_Suc_eq_le Suc_le_eq
   408 
   409 
   410 text {* Equivalence of @{term "m \<le> n"} and @{term "m < n | m = n"} *}
   411 
   412 lemma le_imp_less_or_eq: "m \<le> n ==> m < n | m = (n::nat)"
   413   apply (unfold le_def)
   414   using less_linear
   415   apply (blast elim: less_irrefl less_asym)
   416   done
   417 
   418 lemma less_or_eq_imp_le: "m < n | m = n ==> m \<le> (n::nat)"
   419   apply (unfold le_def)
   420   using less_linear
   421   apply (blast elim!: less_irrefl elim: less_asym)
   422   done
   423 
   424 lemma le_eq_less_or_eq: "(m \<le> (n::nat)) = (m < n | m=n)"
   425   by (rules intro: less_or_eq_imp_le le_imp_less_or_eq)
   426 
   427 text {* Useful with @{text Blast}. *}
   428 lemma eq_imp_le: "(m::nat) = n ==> m \<le> n"
   429   by (rule less_or_eq_imp_le, rule disjI2)
   430 
   431 lemma le_refl: "n \<le> (n::nat)"
   432   by (simp add: le_eq_less_or_eq)
   433 
   434 lemma le_less_trans: "[| i \<le> j; j < k |] ==> i < (k::nat)"
   435   by (blast dest!: le_imp_less_or_eq intro: less_trans)
   436 
   437 lemma less_le_trans: "[| i < j; j \<le> k |] ==> i < (k::nat)"
   438   by (blast dest!: le_imp_less_or_eq intro: less_trans)
   439 
   440 lemma le_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::nat)"
   441   by (blast dest!: le_imp_less_or_eq intro: less_or_eq_imp_le less_trans)
   442 
   443 lemma le_anti_sym: "[| m \<le> n; n \<le> m |] ==> m = (n::nat)"
   444   by (blast dest!: le_imp_less_or_eq elim!: less_irrefl elim: less_asym)
   445 
   446 lemma Suc_le_mono [iff]: "(Suc n \<le> Suc m) = (n \<le> m)"
   447   by (simp add: le_simps)
   448 
   449 text {* Axiom @{text order_less_le} of class @{text order}: *}
   450 lemma nat_less_le: "((m::nat) < n) = (m \<le> n & m \<noteq> n)"
   451   by (simp add: le_def nat_neq_iff) (blast elim!: less_asym)
   452 
   453 lemma le_neq_implies_less: "(m::nat) \<le> n ==> m \<noteq> n ==> m < n"
   454   by (rule iffD2, rule nat_less_le, rule conjI)
   455 
   456 text {* Axiom @{text linorder_linear} of class @{text linorder}: *}
   457 lemma nat_le_linear: "(m::nat) \<le> n | n \<le> m"
   458   apply (simp add: le_eq_less_or_eq)
   459   using less_linear
   460   apply blast
   461   done
   462 
   463 lemma not_less_less_Suc_eq: "~ n < m ==> (n < Suc m) = (n = m)"
   464   by (blast elim!: less_SucE)
   465 
   466 
   467 text {*
   468   Rewrite @{term "n < Suc m"} to @{term "n = m"}
   469   if @{term "~ n < m"} or @{term "m \<le> n"} hold.
   470   Not suitable as default simprules because they often lead to looping
   471 *}
   472 lemma le_less_Suc_eq: "m \<le> n ==> (n < Suc m) = (n = m)"
   473   by (rule not_less_less_Suc_eq, rule leD)
   474 
   475 lemmas not_less_simps = not_less_less_Suc_eq le_less_Suc_eq
   476 
   477 
   478 text {*
   479   Re-orientation of the equations @{text "0 = x"} and @{text "1 = x"}. 
   480   No longer added as simprules (they loop) 
   481   but via @{text reorient_simproc} in Bin
   482 *}
   483 
   484 text {* Polymorphic, not just for @{typ nat} *}
   485 lemma zero_reorient: "(0 = x) = (x = 0)"
   486   by auto
   487 
   488 lemma one_reorient: "(1 = x) = (x = 1)"
   489   by auto
   490 
   491 text {* Type {@typ nat} is a wellfounded linear order *}
   492 
   493 instance nat :: order by (intro_classes,
   494   (assumption | rule le_refl le_trans le_anti_sym nat_less_le)+)
   495 instance nat :: linorder by (intro_classes, rule nat_le_linear)
   496 instance nat :: wellorder by (intro_classes, rule wf_less)
   497 
   498 subsection {* Arithmetic operators *}
   499 
   500 axclass power < type
   501 
   502 consts
   503   power :: "('a::power) => nat => 'a"            (infixr "^" 80)
   504 
   505 
   506 text {* arithmetic operators @{text "+ -"} and @{text "*"} *}
   507 
   508 instance nat :: plus ..
   509 instance nat :: minus ..
   510 instance nat :: times ..
   511 instance nat :: power ..
   512 
   513 text {* size of a datatype value; overloaded *}
   514 consts size :: "'a => nat"
   515 
   516 primrec
   517   add_0:    "0 + n = n"
   518   add_Suc:  "Suc m + n = Suc (m + n)"
   519 
   520 primrec
   521   diff_0:   "m - 0 = m"
   522   diff_Suc: "m - Suc n = (case m - n of 0 => 0 | Suc k => k)"
   523 
   524 primrec
   525   mult_0:   "0 * n = 0"
   526   mult_Suc: "Suc m * n = n + (m * n)"
   527 
   528 text {* These 2 rules ease the use of primitive recursion. NOTE USE OF @{text "=="} *}
   529 lemma def_nat_rec_0: "(!!n. f n == nat_rec c h n) ==> f 0 = c"
   530   by simp
   531 
   532 lemma def_nat_rec_Suc: "(!!n. f n == nat_rec c h n) ==> f (Suc n) = h n (f n)"
   533   by simp
   534 
   535 lemma not0_implies_Suc: "n \<noteq> 0 ==> \<exists>m. n = Suc m"
   536   by (case_tac n) simp_all
   537 
   538 lemma gr_implies_not0: "!!n::nat. m<n ==> n \<noteq> 0"
   539   by (case_tac n) simp_all
   540 
   541 lemma neq0_conv [iff]: "!!n::nat. (n \<noteq> 0) = (0 < n)"
   542   by (case_tac n) simp_all
   543 
   544 text {* This theorem is useful with @{text blast} *}
   545 lemma gr0I: "((n::nat) = 0 ==> False) ==> 0 < n"
   546   by (rule iffD1, rule neq0_conv, rules)
   547 
   548 lemma gr0_conv_Suc: "(0 < n) = (\<exists>m. n = Suc m)"
   549   by (fast intro: not0_implies_Suc)
   550 
   551 lemma not_gr0 [iff]: "!!n::nat. (~ (0 < n)) = (n = 0)"
   552   apply (rule iffI)
   553   apply (rule ccontr, simp_all)
   554   done
   555 
   556 lemma Suc_le_D: "(Suc n \<le> m') ==> (? m. m' = Suc m)"
   557   by (induct m') simp_all
   558 
   559 text {* Useful in certain inductive arguments *}
   560 lemma less_Suc_eq_0_disj: "(m < Suc n) = (m = 0 | (\<exists>j. m = Suc j & j < n))"
   561   by (case_tac m) simp_all
   562 
   563 lemma nat_induct2: "P 0 ==> P (Suc 0) ==> (!!k. P k ==> P (Suc (Suc k))) ==> P n"
   564   apply (rule nat_less_induct)
   565   apply (case_tac n)
   566   apply (case_tac [2] nat)
   567   apply (blast intro: less_trans)+
   568   done
   569 
   570 subsection {* @{text LEAST} theorems for type @{typ nat} by specialization *}
   571 
   572 lemmas LeastI = wellorder_LeastI
   573 lemmas Least_le = wellorder_Least_le
   574 lemmas not_less_Least = wellorder_not_less_Least
   575 
   576 lemma Least_Suc:
   577      "[| P n; ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))"
   578   apply (case_tac "n", auto)
   579   apply (frule LeastI)
   580   apply (drule_tac P = "%x. P (Suc x) " in LeastI)
   581   apply (subgoal_tac " (LEAST x. P x) \<le> Suc (LEAST x. P (Suc x))")
   582   apply (erule_tac [2] Least_le)
   583   apply (case_tac "LEAST x. P x", auto)
   584   apply (drule_tac P = "%x. P (Suc x) " in Least_le)
   585   apply (blast intro: order_antisym)
   586   done
   587 
   588 lemma Least_Suc2:
   589      "[|P n; Q m; ~P 0; !k. P (Suc k) = Q k|] ==> Least P = Suc (Least Q)"
   590   by (erule (1) Least_Suc [THEN ssubst], simp)
   591 
   592 
   593 
   594 subsection {* @{term min} and @{term max} *}
   595 
   596 lemma min_0L [simp]: "min 0 n = (0::nat)"
   597   by (rule min_leastL) simp
   598 
   599 lemma min_0R [simp]: "min n 0 = (0::nat)"
   600   by (rule min_leastR) simp
   601 
   602 lemma min_Suc_Suc [simp]: "min (Suc m) (Suc n) = Suc (min m n)"
   603   by (simp add: min_of_mono)
   604 
   605 lemma max_0L [simp]: "max 0 n = (n::nat)"
   606   by (rule max_leastL) simp
   607 
   608 lemma max_0R [simp]: "max n 0 = (n::nat)"
   609   by (rule max_leastR) simp
   610 
   611 lemma max_Suc_Suc [simp]: "max (Suc m) (Suc n) = Suc(max m n)"
   612   by (simp add: max_of_mono)
   613 
   614 
   615 subsection {* Basic rewrite rules for the arithmetic operators *}
   616 
   617 text {* Difference *}
   618 
   619 lemma diff_0_eq_0 [simp, code]: "0 - n = (0::nat)"
   620   by (induct_tac n) simp_all
   621 
   622 lemma diff_Suc_Suc [simp, code]: "Suc(m) - Suc(n) = m - n"
   623   by (induct_tac n) simp_all
   624 
   625 
   626 text {*
   627   Could be (and is, below) generalized in various ways
   628   However, none of the generalizations are currently in the simpset,
   629   and I dread to think what happens if I put them in
   630 *}
   631 lemma Suc_pred [simp]: "0 < n ==> Suc (n - Suc 0) = n"
   632   by (simp split add: nat.split)
   633 
   634 declare diff_Suc [simp del, code del]
   635 
   636 
   637 subsection {* Addition *}
   638 
   639 lemma add_0_right [simp]: "m + 0 = (m::nat)"
   640   by (induct m) simp_all
   641 
   642 lemma add_Suc_right [simp]: "m + Suc n = Suc (m + n)"
   643   by (induct m) simp_all
   644 
   645 lemma [code]: "Suc m + n = m + Suc n" by simp
   646 
   647 
   648 text {* Associative law for addition *}
   649 lemma nat_add_assoc: "(m + n) + k = m + ((n + k)::nat)"
   650   by (induct m) simp_all
   651 
   652 text {* Commutative law for addition *}
   653 lemma nat_add_commute: "m + n = n + (m::nat)"
   654   by (induct m) simp_all
   655 
   656 lemma nat_add_left_commute: "x + (y + z) = y + ((x + z)::nat)"
   657   apply (rule mk_left_commute [of "op +"])
   658   apply (rule nat_add_assoc)
   659   apply (rule nat_add_commute)
   660   done
   661 
   662 lemma nat_add_left_cancel [simp]: "(k + m = k + n) = (m = (n::nat))"
   663   by (induct k) simp_all
   664 
   665 lemma nat_add_right_cancel [simp]: "(m + k = n + k) = (m=(n::nat))"
   666   by (induct k) simp_all
   667 
   668 lemma nat_add_left_cancel_le [simp]: "(k + m \<le> k + n) = (m\<le>(n::nat))"
   669   by (induct k) simp_all
   670 
   671 lemma nat_add_left_cancel_less [simp]: "(k + m < k + n) = (m<(n::nat))"
   672   by (induct k) simp_all
   673 
   674 text {* Reasoning about @{text "m + 0 = 0"}, etc. *}
   675 
   676 lemma add_is_0 [iff]: "!!m::nat. (m + n = 0) = (m = 0 & n = 0)"
   677   by (case_tac m) simp_all
   678 
   679 lemma add_is_1: "(m+n= Suc 0) = (m= Suc 0 & n=0 | m=0 & n= Suc 0)"
   680   by (case_tac m) simp_all
   681 
   682 lemma one_is_add: "(Suc 0 = m + n) = (m = Suc 0 & n = 0 | m = 0 & n = Suc 0)"
   683   by (rule trans, rule eq_commute, rule add_is_1)
   684 
   685 lemma add_gr_0 [iff]: "!!m::nat. (0 < m + n) = (0 < m | 0 < n)"
   686   by (simp del: neq0_conv add: neq0_conv [symmetric])
   687 
   688 lemma add_eq_self_zero: "!!m::nat. m + n = m ==> n = 0"
   689   apply (drule add_0_right [THEN ssubst])
   690   apply (simp add: nat_add_assoc del: add_0_right)
   691   done
   692 
   693 subsection {* Monotonicity of Addition *}
   694 
   695 text {* strict, in 1st argument *}
   696 lemma add_less_mono1: "i < j ==> i + k < j + (k::nat)"
   697   by (induct k) simp_all
   698 
   699 text {* strict, in both arguments *}
   700 lemma add_less_mono: "[|i < j; k < l|] ==> i + k < j + (l::nat)"
   701   apply (rule add_less_mono1 [THEN less_trans], assumption+)
   702   apply (induct_tac j, simp_all)
   703   done
   704 
   705 text {* Deleted @{text less_natE}; use @{text "less_imp_Suc_add RS exE"} *}
   706 lemma less_imp_Suc_add: "m < n ==> (\<exists>k. n = Suc (m + k))"
   707   apply (induct n)
   708   apply (simp_all add: order_le_less)
   709   apply (blast elim!: less_SucE intro!: add_0_right [symmetric] add_Suc_right [symmetric])
   710   done
   711 
   712 
   713 subsection {* Multiplication *}
   714 
   715 text {* right annihilation in product *}
   716 lemma mult_0_right [simp]: "(m::nat) * 0 = 0"
   717   by (induct m) simp_all
   718 
   719 text {* right successor law for multiplication *}
   720 lemma mult_Suc_right [simp]: "m * Suc n = m + (m * n)"
   721   by (induct m) (simp_all add: nat_add_left_commute)
   722 
   723 text {* Commutative law for multiplication *}
   724 lemma nat_mult_commute: "m * n = n * (m::nat)"
   725   by (induct m) simp_all
   726 
   727 text {* addition distributes over multiplication *}
   728 lemma add_mult_distrib: "(m + n) * k = (m * k) + ((n * k)::nat)"
   729   by (induct m) (simp_all add: nat_add_assoc nat_add_left_commute)
   730 
   731 lemma add_mult_distrib2: "k * (m + n) = (k * m) + ((k * n)::nat)"
   732   by (induct m) (simp_all add: nat_add_assoc)
   733 
   734 text {* Associative law for multiplication *}
   735 lemma nat_mult_assoc: "(m * n) * k = m * ((n * k)::nat)"
   736   by (induct m) (simp_all add: add_mult_distrib)
   737 
   738 lemma mult_is_0 [simp]: "((m::nat) * n = 0) = (m=0 | n=0)"
   739   apply (induct_tac m)
   740   apply (induct_tac [2] n, simp_all)
   741   done
   742 
   743 text {* strict, in 1st argument; proof is by induction on @{text "k > 0"} *}
   744 lemma mult_less_mono2: "(i::nat) < j ==> 0 < k ==> k * i < k * j"
   745   apply (erule_tac m1 = 0 in less_imp_Suc_add [THEN exE], simp)
   746   apply (induct_tac x) 
   747   apply (simp_all add: add_less_mono)
   748   done
   749 
   750 text{*The Naturals Form an Ordered Semiring*}
   751 instance nat :: ordered_semiring
   752 proof
   753   fix i j k :: nat
   754   show "(i + j) + k = i + (j + k)" by (rule nat_add_assoc)
   755   show "i + j = j + i" by (rule nat_add_commute)
   756   show "0 + i = i" by simp
   757   show "(i * j) * k = i * (j * k)" by (rule nat_mult_assoc)
   758   show "i * j = j * i" by (rule nat_mult_commute)
   759   show "1 * i = i" by simp
   760   show "(i + j) * k = i * k + j * k" by (simp add: add_mult_distrib)
   761   show "0 \<noteq> (1::nat)" by simp
   762   show "i \<le> j ==> k + i \<le> k + j" by simp
   763   show "i < j ==> 0 < k ==> k * i < k * j" by (simp add: mult_less_mono2)
   764 qed
   765 
   766 lemma nat_mult_1: "(1::nat) * n = n"
   767   by simp
   768 
   769 lemma nat_mult_1_right: "n * (1::nat) = n"
   770   by simp
   771 
   772 
   773 subsection {* Additional theorems about "less than" *}
   774 
   775 text {* A [clumsy] way of lifting @{text "<"}
   776   monotonicity to @{text "\<le>"} monotonicity *}
   777 lemma less_mono_imp_le_mono:
   778   assumes lt_mono: "!!i j::nat. i < j ==> f i < f j"
   779   and le: "i \<le> j" shows "f i \<le> ((f j)::nat)" using le
   780   apply (simp add: order_le_less)
   781   apply (blast intro!: lt_mono)
   782   done
   783 
   784 text {* non-strict, in 1st argument *}
   785 lemma add_le_mono1: "i \<le> j ==> i + k \<le> j + (k::nat)"
   786   by (rule add_right_mono)
   787 
   788 text {* non-strict, in both arguments *}
   789 lemma add_le_mono: "[| i \<le> j;  k \<le> l |] ==> i + k \<le> j + (l::nat)"
   790   by (rule add_mono)
   791 
   792 lemma le_add2: "n \<le> ((m + n)::nat)"
   793   apply (induct m, simp_all)
   794   apply (erule le_SucI)
   795   done
   796 
   797 lemma le_add1: "n \<le> ((n + m)::nat)"
   798   apply (simp add: add_ac)
   799   apply (rule le_add2)
   800   done
   801 
   802 lemma less_add_Suc1: "i < Suc (i + m)"
   803   by (rule le_less_trans, rule le_add1, rule lessI)
   804 
   805 lemma less_add_Suc2: "i < Suc (m + i)"
   806   by (rule le_less_trans, rule le_add2, rule lessI)
   807 
   808 lemma less_iff_Suc_add: "(m < n) = (\<exists>k. n = Suc (m + k))"
   809   by (rules intro!: less_add_Suc1 less_imp_Suc_add)
   810 
   811 
   812 lemma trans_le_add1: "(i::nat) \<le> j ==> i \<le> j + m"
   813   by (rule le_trans, assumption, rule le_add1)
   814 
   815 lemma trans_le_add2: "(i::nat) \<le> j ==> i \<le> m + j"
   816   by (rule le_trans, assumption, rule le_add2)
   817 
   818 lemma trans_less_add1: "(i::nat) < j ==> i < j + m"
   819   by (rule less_le_trans, assumption, rule le_add1)
   820 
   821 lemma trans_less_add2: "(i::nat) < j ==> i < m + j"
   822   by (rule less_le_trans, assumption, rule le_add2)
   823 
   824 lemma add_lessD1: "i + j < (k::nat) ==> i < k"
   825   apply (induct j, simp_all)
   826   apply (blast dest: Suc_lessD)
   827   done
   828 
   829 lemma not_add_less1 [iff]: "~ (i + j < (i::nat))"
   830   apply (rule notI)
   831   apply (erule add_lessD1 [THEN less_irrefl])
   832   done
   833 
   834 lemma not_add_less2 [iff]: "~ (j + i < (i::nat))"
   835   by (simp add: add_commute not_add_less1)
   836 
   837 lemma add_leD1: "m + k \<le> n ==> m \<le> (n::nat)"
   838   by (induct k) (simp_all add: le_simps)
   839 
   840 lemma add_leD2: "m + k \<le> n ==> k \<le> (n::nat)"
   841   apply (simp add: add_commute)
   842   apply (erule add_leD1)
   843   done
   844 
   845 lemma add_leE: "(m::nat) + k \<le> n ==> (m \<le> n ==> k \<le> n ==> R) ==> R"
   846   by (blast dest: add_leD1 add_leD2)
   847 
   848 text {* needs @{text "!!k"} for @{text add_ac} to work *}
   849 lemma less_add_eq_less: "!!k::nat. k < l ==> m + l = k + n ==> m < n"
   850   by (force simp del: add_Suc_right
   851     simp add: less_iff_Suc_add add_Suc_right [symmetric] add_ac)
   852 
   853 
   854 
   855 subsection {* Difference *}
   856 
   857 lemma diff_self_eq_0 [simp]: "(m::nat) - m = 0"
   858   by (induct m) simp_all
   859 
   860 text {* Addition is the inverse of subtraction:
   861   if @{term "n \<le> m"} then @{term "n + (m - n) = m"}. *}
   862 lemma add_diff_inverse: "~  m < n ==> n + (m - n) = (m::nat)"
   863   by (induct m n rule: diff_induct) simp_all
   864 
   865 lemma le_add_diff_inverse [simp]: "n \<le> m ==> n + (m - n) = (m::nat)"
   866   by (simp add: add_diff_inverse not_less_iff_le)
   867 
   868 lemma le_add_diff_inverse2 [simp]: "n \<le> m ==> (m - n) + n = (m::nat)"
   869   by (simp add: le_add_diff_inverse add_commute)
   870 
   871 
   872 subsection {* More results about difference *}
   873 
   874 lemma Suc_diff_le: "n \<le> m ==> Suc m - n = Suc (m - n)"
   875   by (induct m n rule: diff_induct) simp_all
   876 
   877 lemma diff_less_Suc: "m - n < Suc m"
   878   apply (induct m n rule: diff_induct)
   879   apply (erule_tac [3] less_SucE)
   880   apply (simp_all add: less_Suc_eq)
   881   done
   882 
   883 lemma diff_le_self [simp]: "m - n \<le> (m::nat)"
   884   by (induct m n rule: diff_induct) (simp_all add: le_SucI)
   885 
   886 lemma less_imp_diff_less: "(j::nat) < k ==> j - n < k"
   887   by (rule le_less_trans, rule diff_le_self)
   888 
   889 lemma diff_diff_left: "(i::nat) - j - k = i - (j + k)"
   890   by (induct i j rule: diff_induct) simp_all
   891 
   892 lemma Suc_diff_diff [simp]: "(Suc m - n) - Suc k = m - n - k"
   893   by (simp add: diff_diff_left)
   894 
   895 lemma diff_Suc_less [simp]: "0<n ==> n - Suc i < n"
   896   apply (case_tac "n", safe)
   897   apply (simp add: le_simps)
   898   done
   899 
   900 text {* This and the next few suggested by Florian Kammueller *}
   901 lemma diff_commute: "(i::nat) - j - k = i - k - j"
   902   by (simp add: diff_diff_left add_commute)
   903 
   904 lemma diff_add_assoc: "k \<le> (j::nat) ==> (i + j) - k = i + (j - k)"
   905   by (induct j k rule: diff_induct) simp_all
   906 
   907 lemma diff_add_assoc2: "k \<le> (j::nat) ==> (j + i) - k = (j - k) + i"
   908   by (simp add: add_commute diff_add_assoc)
   909 
   910 lemma diff_add_inverse: "(n + m) - n = (m::nat)"
   911   by (induct n) simp_all
   912 
   913 lemma diff_add_inverse2: "(m + n) - n = (m::nat)"
   914   by (simp add: diff_add_assoc)
   915 
   916 lemma le_imp_diff_is_add: "i \<le> (j::nat) ==> (j - i = k) = (j = k + i)"
   917   apply safe
   918   apply (simp_all add: diff_add_inverse2)
   919   done
   920 
   921 lemma diff_is_0_eq [simp]: "((m::nat) - n = 0) = (m \<le> n)"
   922   by (induct m n rule: diff_induct) simp_all
   923 
   924 lemma diff_is_0_eq' [simp]: "m \<le> n ==> (m::nat) - n = 0"
   925   by (rule iffD2, rule diff_is_0_eq)
   926 
   927 lemma zero_less_diff [simp]: "(0 < n - (m::nat)) = (m < n)"
   928   by (induct m n rule: diff_induct) simp_all
   929 
   930 lemma less_imp_add_positive: "i < j  ==> \<exists>k::nat. 0 < k & i + k = j"
   931   apply (rule_tac x = "j - i" in exI)
   932   apply (simp (no_asm_simp) add: add_diff_inverse less_not_sym)
   933   done
   934 
   935 lemma zero_induct_lemma: "P k ==> (!!n. P (Suc n) ==> P n) ==> P (k - i)"
   936   apply (induct k i rule: diff_induct)
   937   apply (simp_all (no_asm))
   938   apply rules
   939   done
   940 
   941 lemma zero_induct: "P k ==> (!!n. P (Suc n) ==> P n) ==> P 0"
   942   apply (rule diff_self_eq_0 [THEN subst])
   943   apply (rule zero_induct_lemma, rules+)
   944   done
   945 
   946 lemma diff_cancel: "(k + m) - (k + n) = m - (n::nat)"
   947   by (induct k) simp_all
   948 
   949 lemma diff_cancel2: "(m + k) - (n + k) = m - (n::nat)"
   950   by (simp add: diff_cancel add_commute)
   951 
   952 lemma diff_add_0: "n - (n + m) = (0::nat)"
   953   by (induct n) simp_all
   954 
   955 
   956 text {* Difference distributes over multiplication *}
   957 
   958 lemma diff_mult_distrib: "((m::nat) - n) * k = (m * k) - (n * k)"
   959   by (induct m n rule: diff_induct) (simp_all add: diff_cancel)
   960 
   961 lemma diff_mult_distrib2: "k * ((m::nat) - n) = (k * m) - (k * n)"
   962   by (simp add: diff_mult_distrib mult_commute [of k])
   963   -- {* NOT added as rewrites, since sometimes they are used from right-to-left *}
   964 
   965 lemmas nat_distrib =
   966   add_mult_distrib add_mult_distrib2 diff_mult_distrib diff_mult_distrib2
   967 
   968 
   969 subsection {* Monotonicity of Multiplication *}
   970 
   971 lemma mult_le_mono1: "i \<le> (j::nat) ==> i * k \<le> j * k"
   972   by (induct k) (simp_all add: add_le_mono)
   973 
   974 lemma mult_le_mono2: "i \<le> (j::nat) ==> k * i \<le> k * j"
   975   apply (drule mult_le_mono1)
   976   apply (simp add: mult_commute)
   977   done
   978 
   979 text {* @{text "\<le>"} monotonicity, BOTH arguments *}
   980 lemma mult_le_mono: "i \<le> (j::nat) ==> k \<le> l ==> i * k \<le> j * l"
   981   apply (erule mult_le_mono1 [THEN le_trans])
   982   apply (erule mult_le_mono2)
   983   done
   984 
   985 lemma mult_less_mono1: "(i::nat) < j ==> 0 < k ==> i * k < j * k"
   986   by (drule mult_less_mono2) (simp_all add: mult_commute)
   987 
   988 text{*Differs from the standard @{text zero_less_mult_iff} in that
   989       there are no negative numbers.*}
   990 lemma nat_0_less_mult_iff [simp]: "(0 < (m::nat) * n) = (0 < m & 0 < n)"
   991   apply (induct m)
   992   apply (case_tac [2] n, simp_all)
   993   done
   994 
   995 lemma one_le_mult_iff [simp]: "(Suc 0 \<le> m * n) = (1 \<le> m & 1 \<le> n)"
   996   apply (induct m)
   997   apply (case_tac [2] n, simp_all)
   998   done
   999 
  1000 lemma mult_eq_1_iff [simp]: "(m * n = Suc 0) = (m = 1 & n = 1)"
  1001   apply (induct_tac m, simp)
  1002   apply (induct_tac n, simp, fastsimp)
  1003   done
  1004 
  1005 lemma one_eq_mult_iff [simp]: "(Suc 0 = m * n) = (m = 1 & n = 1)"
  1006   apply (rule trans)
  1007   apply (rule_tac [2] mult_eq_1_iff, fastsimp)
  1008   done
  1009 
  1010 lemma mult_less_cancel2: "((m::nat) * k < n * k) = (0 < k & m < n)"
  1011   apply (safe intro!: mult_less_mono1)
  1012   apply (case_tac k, auto)
  1013   apply (simp del: le_0_eq add: linorder_not_le [symmetric])
  1014   apply (blast intro: mult_le_mono1)
  1015   done
  1016 
  1017 lemma mult_less_cancel1 [simp]: "(k * (m::nat) < k * n) = (0 < k & m < n)"
  1018   by (simp add: mult_less_cancel2 mult_commute [of k])
  1019 
  1020 declare mult_less_cancel2 [simp]
  1021 
  1022 lemma mult_le_cancel1 [simp]: "(k * (m::nat) \<le> k * n) = (0 < k --> m \<le> n)"
  1023 by (simp add: linorder_not_less [symmetric], auto)
  1024 
  1025 lemma mult_le_cancel2 [simp]: "((m::nat) * k \<le> n * k) = (0 < k --> m \<le> n)"
  1026 by (simp add: linorder_not_less [symmetric], auto)
  1027 
  1028 lemma mult_cancel2: "(m * k = n * k) = (m = n | (k = (0::nat)))"
  1029   apply (cut_tac less_linear, safe, auto)
  1030   apply (drule mult_less_mono1, assumption, simp)+
  1031   done
  1032 
  1033 lemma mult_cancel1 [simp]: "(k * m = k * n) = (m = n | (k = (0::nat)))"
  1034   by (simp add: mult_cancel2 mult_commute [of k])
  1035 
  1036 declare mult_cancel2 [simp]
  1037 
  1038 lemma Suc_mult_less_cancel1: "(Suc k * m < Suc k * n) = (m < n)"
  1039   by (subst mult_less_cancel1) simp
  1040 
  1041 lemma Suc_mult_le_cancel1: "(Suc k * m \<le> Suc k * n) = (m \<le> n)"
  1042   by (subst mult_le_cancel1) simp
  1043 
  1044 lemma Suc_mult_cancel1: "(Suc k * m = Suc k * n) = (m = n)"
  1045   by (subst mult_cancel1) simp
  1046 
  1047 
  1048 text {* Lemma for @{text gcd} *}
  1049 lemma mult_eq_self_implies_10: "(m::nat) = m * n ==> n = 1 | m = 0"
  1050   apply (drule sym)
  1051   apply (rule disjCI)
  1052   apply (rule nat_less_cases, erule_tac [2] _)
  1053   apply (fastsimp elim!: less_SucE)
  1054   apply (fastsimp dest: mult_less_mono2)
  1055   done
  1056 
  1057 
  1058 end