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