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