src/HOL/Nat.thy
 author wenzelm Thu Oct 04 20:29:42 2007 +0200 (2007-10-04) changeset 24850 0cfd722ab579 parent 24729 f5015dd2431b child 24995 c26e0166e568 permissions -rw-r--r--
Name.uu, Name.aT;
     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   ("Tools/function_package/size.ML")

    20 begin

    21

    22 subsection {* Type @{text ind} *}

    23

    24 typedecl ind

    25

    26 axiomatization

    27   Zero_Rep :: ind and

    28   Suc_Rep :: "ind => ind"

    29 where

    30   -- {* the axiom of infinity in 2 parts *}

    31   inj_Suc_Rep:          "inj Suc_Rep" and

    32   Suc_Rep_not_Zero_Rep: "Suc_Rep x \<noteq> Zero_Rep"

    33

    34

    35 subsection {* Type nat *}

    36

    37 text {* Type definition *}

    38

    39 inductive_set Nat :: "ind set"

    40 where

    41     Zero_RepI: "Zero_Rep : Nat"

    42   | Suc_RepI: "i : Nat ==> Suc_Rep i : Nat"

    43

    44 global

    45

    46 typedef (open Nat)

    47   nat = Nat

    48 proof

    49   show "Zero_Rep : Nat" by (rule Nat.Zero_RepI)

    50 qed

    51

    52 text {* Abstract constants and syntax *}

    53

    54 consts

    55   Suc :: "nat => nat"

    56

    57 local

    58

    59 defs

    60   Suc_def:      "Suc == (%n. Abs_Nat (Suc_Rep (Rep_Nat n)))"

    61

    62 definition

    63   pred_nat :: "(nat * nat) set" where

    64   "pred_nat = {(m, n). n = Suc m}"

    65

    66 instance nat :: "{ord, zero, one}"

    67   Zero_nat_def: "0 == Abs_Nat Zero_Rep"

    68   One_nat_def [simp]: "1 == Suc 0"

    69   less_def: "m < n == (m, n) : pred_nat^+"

    70   le_def:   "m \<le> (n::nat) == ~ (n < m)" ..

    71

    72 lemmas [code func del] = Zero_nat_def less_def le_def

    73

    74 text {* Induction *}

    75

    76 theorem nat_induct: "P 0 ==> (!!n. P n ==> P (Suc n)) ==> P n"

    77   apply (unfold Zero_nat_def Suc_def)

    78   apply (rule Rep_Nat_inverse [THEN subst]) -- {* types force good instantiation *}

    79   apply (erule Rep_Nat [THEN Nat.induct])

    80   apply (iprover elim: Abs_Nat_inverse [THEN subst])

    81   done

    82

    83 text {* Distinctness of constructors *}

    84

    85 lemma Suc_not_Zero [iff]: "Suc m \<noteq> 0"

    86   by (simp add: Zero_nat_def Suc_def Abs_Nat_inject Rep_Nat Suc_RepI Zero_RepI

    87                 Suc_Rep_not_Zero_Rep)

    88

    89 lemma Zero_not_Suc [iff]: "0 \<noteq> Suc m"

    90   by (rule not_sym, rule Suc_not_Zero not_sym)

    91

    92 lemma Suc_neq_Zero: "Suc m = 0 ==> R"

    93   by (rule notE, rule Suc_not_Zero)

    94

    95 lemma Zero_neq_Suc: "0 = Suc m ==> R"

    96   by (rule Suc_neq_Zero, erule sym)

    97

    98 text {* Injectiveness of @{term Suc} *}

    99

   100 lemma inj_Suc[simp]: "inj_on Suc N"

   101   by (simp add: Suc_def inj_on_def Abs_Nat_inject Rep_Nat Suc_RepI

   102                 inj_Suc_Rep [THEN inj_eq] Rep_Nat_inject)

   103

   104 lemma Suc_inject: "Suc x = Suc y ==> x = y"

   105   by (rule inj_Suc [THEN injD])

   106

   107 lemma Suc_Suc_eq [iff]: "(Suc m = Suc n) = (m = n)"

   108   by (rule inj_Suc [THEN inj_eq])

   109

   110 lemma nat_not_singleton: "(\<forall>x. x = (0::nat)) = False"

   111   by auto

   112

   113 text {* size of a datatype value *}

   114

   115 use "Tools/function_package/size.ML"

   116

   117 class size = type +

   118   fixes size :: "'a \<Rightarrow> nat"

   119

   120 setup Size.setup

   121

   122 rep_datatype nat

   123   distinct  Suc_not_Zero Zero_not_Suc

   124   inject    Suc_Suc_eq

   125   induction nat_induct

   126

   127 declare nat.induct [case_names 0 Suc, induct type: nat]

   128 declare nat.exhaust [case_names 0 Suc, cases type: nat]

   129

   130 lemmas nat_rec_0 = nat.recs(1)

   131   and nat_rec_Suc = nat.recs(2)

   132

   133 lemmas nat_case_0 = nat.cases(1)

   134   and nat_case_Suc = nat.cases(2)

   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,noatp]: "(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,noatp]: "!!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 text {* The method of infinite descent, frequently used in number theory.

   802 Provided by Roelof Oosterhuis.

   803 $P(n)$ is true for all $n\in\mathbb{N}$ if

   804 \begin{itemize}

   805   \item case 0'': given $n=0$ prove $P(n)$,

   806   \item case smaller'': given $n>0$ and $\neg P(n)$ prove there exists

   807         a smaller integer $m$ such that $\neg P(m)$.

   808 \end{itemize} *}

   809

   810 lemma infinite_descent0[case_names 0 smaller]:

   811   "\<lbrakk> P 0; !!n. n>0 \<Longrightarrow> \<not> P n \<Longrightarrow> (\<exists>m::nat. m < n \<and> \<not>P m) \<rbrakk> \<Longrightarrow> P n"

   812 by (induct n rule: less_induct, case_tac "n>0", auto)

   813

   814 text{* A compact version without explicit base case: *}

   815 lemma infinite_descent:

   816   "\<lbrakk> !!n::nat. \<not> P n \<Longrightarrow>  \<exists>m<n. \<not>  P m \<rbrakk> \<Longrightarrow>  P n"

   817 by (induct n rule: less_induct, auto)

   818

   819

   820 text {* Infinite descent using a mapping to $\mathbb{N}$:

   821 $P(x)$ is true for all $x\in D$ if there exists a $V: D \to \mathbb{N}$ and

   822 \begin{itemize}

   823 \item case 0'': given $V(x)=0$ prove $P(x)$,

   824 \item case smaller'': given $V(x)>0$ and $\neg P(x)$ prove there exists a $y \in D$ such that $V(y)<V(x)$ and $~\neg P(y)$.

   825 \end{itemize}

   826 NB: the proof also shows how to use the previous lemma. *}

   827 corollary infinite_descent0_measure[case_names 0 smaller]:

   828 assumes 0: "!!x. V x = (0::nat) \<Longrightarrow> P x"

   829 and     1: "!!x. V x > 0 \<Longrightarrow> \<not>P x \<Longrightarrow> (\<exists>y. V y < V x \<and> \<not>P y)"

   830 shows "P x"

   831 proof -

   832   obtain n where "n = V x" by auto

   833   moreover have "!!x. V x = n \<Longrightarrow> P x"

   834   proof (induct n rule: infinite_descent0)

   835     case 0 -- "i.e. $V(x) = 0$"

   836     with 0 show "P x" by auto

   837   next -- "now $n>0$ and $P(x)$ does not hold for some $x$ with $V(x)=n$"

   838     case (smaller n)

   839     then obtain x where vxn: "V x = n " and "V x > 0 \<and> \<not> P x" by auto

   840     with 1 obtain y where "V y < V x \<and> \<not> P y" by auto

   841     with vxn obtain m where "m = V y \<and> m<n \<and> \<not> P y" by auto

   842     thus ?case by auto

   843   qed

   844   ultimately show "P x" by auto

   845 qed

   846

   847 text{* Again, without explicit base case: *}

   848 lemma infinite_descent_measure:

   849 assumes "!!x. \<not> P x \<Longrightarrow> \<exists>y. (V::'a\<Rightarrow>nat) y < V x \<and> \<not> P y" shows "P x"

   850 proof -

   851   from assms obtain n where "n = V x" by auto

   852   moreover have "!!x. V x = n \<Longrightarrow> P x"

   853   proof (induct n rule: infinite_descent, auto)

   854     fix x assume "\<not> P x"

   855     with assms show "\<exists>m < V x. \<exists>y. V y = m \<and> \<not> P y" by auto

   856   qed

   857   ultimately show "P x" by auto

   858 qed

   859

   860

   861

   862 text {* A [clumsy] way of lifting @{text "<"}

   863   monotonicity to @{text "\<le>"} monotonicity *}

   864 lemma less_mono_imp_le_mono:

   865   "\<lbrakk> !!i j::nat. i < j \<Longrightarrow> f i < f j; i \<le> j \<rbrakk> \<Longrightarrow> f i \<le> ((f j)::nat)"

   866 by (simp add: order_le_less) (blast)

   867

   868

   869 text {* non-strict, in 1st argument *}

   870 lemma add_le_mono1: "i \<le> j ==> i + k \<le> j + (k::nat)"

   871 by (rule add_right_mono)

   872

   873 text {* non-strict, in both arguments *}

   874 lemma add_le_mono: "[| i \<le> j;  k \<le> l |] ==> i + k \<le> j + (l::nat)"

   875 by (rule add_mono)

   876

   877 lemma le_add2: "n \<le> ((m + n)::nat)"

   878 by (insert add_right_mono [of 0 m n], simp)

   879

   880 lemma le_add1: "n \<le> ((n + m)::nat)"

   881 by (simp add: add_commute, rule le_add2)

   882

   883 lemma less_add_Suc1: "i < Suc (i + m)"

   884 by (rule le_less_trans, rule le_add1, rule lessI)

   885

   886 lemma less_add_Suc2: "i < Suc (m + i)"

   887 by (rule le_less_trans, rule le_add2, rule lessI)

   888

   889 lemma less_iff_Suc_add: "(m < n) = (\<exists>k. n = Suc (m + k))"

   890 by (iprover intro!: less_add_Suc1 less_imp_Suc_add)

   891

   892 lemma trans_le_add1: "(i::nat) \<le> j ==> i \<le> j + m"

   893 by (rule le_trans, assumption, rule le_add1)

   894

   895 lemma trans_le_add2: "(i::nat) \<le> j ==> i \<le> m + j"

   896 by (rule le_trans, assumption, rule le_add2)

   897

   898 lemma trans_less_add1: "(i::nat) < j ==> i < j + m"

   899 by (rule less_le_trans, assumption, rule le_add1)

   900

   901 lemma trans_less_add2: "(i::nat) < j ==> i < m + j"

   902 by (rule less_le_trans, assumption, rule le_add2)

   903

   904 lemma add_lessD1: "i + j < (k::nat) ==> i < k"

   905 apply (rule le_less_trans [of _ "i+j"])

   906 apply (simp_all add: le_add1)

   907 done

   908

   909 lemma not_add_less1 [iff]: "~ (i + j < (i::nat))"

   910 apply (rule notI)

   911 apply (erule add_lessD1 [THEN less_irrefl])

   912 done

   913

   914 lemma not_add_less2 [iff]: "~ (j + i < (i::nat))"

   915 by (simp add: add_commute not_add_less1)

   916

   917 lemma add_leD1: "m + k \<le> n ==> m \<le> (n::nat)"

   918 apply (rule order_trans [of _ "m+k"])

   919 apply (simp_all add: le_add1)

   920 done

   921

   922 lemma add_leD2: "m + k \<le> n ==> k \<le> (n::nat)"

   923 apply (simp add: add_commute)

   924 apply (erule add_leD1)

   925 done

   926

   927 lemma add_leE: "(m::nat) + k \<le> n ==> (m \<le> n ==> k \<le> n ==> R) ==> R"

   928 by (blast dest: add_leD1 add_leD2)

   929

   930 text {* needs @{text "!!k"} for @{text add_ac} to work *}

   931 lemma less_add_eq_less: "!!k::nat. k < l ==> m + l = k + n ==> m < n"

   932 by (force simp del: add_Suc_right

   933     simp add: less_iff_Suc_add add_Suc_right [symmetric] add_ac)

   934

   935

   936 subsection {* Difference *}

   937

   938 lemma diff_self_eq_0 [simp]: "(m::nat) - m = 0"

   939 by (induct m) simp_all

   940

   941 text {* Addition is the inverse of subtraction:

   942   if @{term "n \<le> m"} then @{term "n + (m - n) = m"}. *}

   943 lemma add_diff_inverse: "~  m < n ==> n + (m - n) = (m::nat)"

   944 by (induct m n rule: diff_induct) simp_all

   945

   946 lemma le_add_diff_inverse [simp]: "n \<le> m ==> n + (m - n) = (m::nat)"

   947 by (simp add: add_diff_inverse linorder_not_less)

   948

   949 lemma le_add_diff_inverse2 [simp]: "n \<le> m ==> (m - n) + n = (m::nat)"

   950 by (simp add: le_add_diff_inverse add_commute)

   951

   952

   953 subsection {* More results about difference *}

   954

   955 lemma Suc_diff_le: "n \<le> m ==> Suc m - n = Suc (m - n)"

   956 by (induct m n rule: diff_induct) simp_all

   957

   958 lemma diff_less_Suc: "m - n < Suc m"

   959 apply (induct m n rule: diff_induct)

   960 apply (erule_tac [3] less_SucE)

   961 apply (simp_all add: less_Suc_eq)

   962 done

   963

   964 lemma diff_le_self [simp]: "m - n \<le> (m::nat)"

   965 by (induct m n rule: diff_induct) (simp_all add: le_SucI)

   966

   967 lemma less_imp_diff_less: "(j::nat) < k ==> j - n < k"

   968 by (rule le_less_trans, rule diff_le_self)

   969

   970 lemma diff_diff_left: "(i::nat) - j - k = i - (j + k)"

   971 by (induct i j rule: diff_induct) simp_all

   972

   973 lemma Suc_diff_diff [simp]: "(Suc m - n) - Suc k = m - n - k"

   974 by (simp add: diff_diff_left)

   975

   976 lemma diff_Suc_less [simp]: "0<n ==> n - Suc i < n"

   977 by (cases n) (auto simp add: le_simps)

   978

   979 text {* This and the next few suggested by Florian Kammueller *}

   980 lemma diff_commute: "(i::nat) - j - k = i - k - j"

   981 by (simp add: diff_diff_left add_commute)

   982

   983 lemma diff_add_assoc: "k \<le> (j::nat) ==> (i + j) - k = i + (j - k)"

   984 by (induct j k rule: diff_induct) simp_all

   985

   986 lemma diff_add_assoc2: "k \<le> (j::nat) ==> (j + i) - k = (j - k) + i"

   987 by (simp add: add_commute diff_add_assoc)

   988

   989 lemma diff_add_inverse: "(n + m) - n = (m::nat)"

   990 by (induct n) simp_all

   991

   992 lemma diff_add_inverse2: "(m + n) - n = (m::nat)"

   993 by (simp add: diff_add_assoc)

   994

   995 lemma le_imp_diff_is_add: "i \<le> (j::nat) ==> (j - i = k) = (j = k + i)"

   996 by (auto simp add: diff_add_inverse2)

   997

   998 lemma diff_is_0_eq [simp]: "((m::nat) - n = 0) = (m \<le> n)"

   999 by (induct m n rule: diff_induct) simp_all

  1000

  1001 lemma diff_is_0_eq' [simp]: "m \<le> n ==> (m::nat) - n = 0"

  1002 by (rule iffD2, rule diff_is_0_eq)

  1003

  1004 lemma zero_less_diff [simp]: "(0 < n - (m::nat)) = (m < n)"

  1005 by (induct m n rule: diff_induct) simp_all

  1006

  1007 lemma less_imp_add_positive:

  1008   assumes "i < j"

  1009   shows "\<exists>k::nat. 0 < k & i + k = j"

  1010 proof

  1011   from assms show "0 < j - i & i + (j - i) = j"

  1012     by (simp add: order_less_imp_le)

  1013 qed

  1014

  1015 lemma diff_cancel: "(k + m) - (k + n) = m - (n::nat)"

  1016 by (induct k) simp_all

  1017

  1018 lemma diff_cancel2: "(m + k) - (n + k) = m - (n::nat)"

  1019 by (simp add: diff_cancel add_commute)

  1020

  1021 lemma diff_add_0: "n - (n + m) = (0::nat)"

  1022 by (induct n) simp_all

  1023

  1024

  1025 text {* Difference distributes over multiplication *}

  1026

  1027 lemma diff_mult_distrib: "((m::nat) - n) * k = (m * k) - (n * k)"

  1028 by (induct m n rule: diff_induct) (simp_all add: diff_cancel)

  1029

  1030 lemma diff_mult_distrib2: "k * ((m::nat) - n) = (k * m) - (k * n)"

  1031 by (simp add: diff_mult_distrib mult_commute [of k])

  1032   -- {* NOT added as rewrites, since sometimes they are used from right-to-left *}

  1033

  1034 lemmas nat_distrib =

  1035   add_mult_distrib add_mult_distrib2 diff_mult_distrib diff_mult_distrib2

  1036

  1037

  1038 subsection {* Monotonicity of Multiplication *}

  1039

  1040 lemma mult_le_mono1: "i \<le> (j::nat) ==> i * k \<le> j * k"

  1041 by (simp add: mult_right_mono)

  1042

  1043 lemma mult_le_mono2: "i \<le> (j::nat) ==> k * i \<le> k * j"

  1044 by (simp add: mult_left_mono)

  1045

  1046 text {* @{text "\<le>"} monotonicity, BOTH arguments *}

  1047 lemma mult_le_mono: "i \<le> (j::nat) ==> k \<le> l ==> i * k \<le> j * l"

  1048 by (simp add: mult_mono)

  1049

  1050 lemma mult_less_mono1: "(i::nat) < j ==> 0 < k ==> i * k < j * k"

  1051 by (simp add: mult_strict_right_mono)

  1052

  1053 text{*Differs from the standard @{text zero_less_mult_iff} in that

  1054       there are no negative numbers.*}

  1055 lemma nat_0_less_mult_iff [simp]: "(0 < (m::nat) * n) = (0 < m & 0 < n)"

  1056   apply (induct m)

  1057    apply simp

  1058   apply (case_tac n)

  1059    apply simp_all

  1060   done

  1061

  1062 lemma one_le_mult_iff [simp]: "(Suc 0 \<le> m * n) = (1 \<le> m & 1 \<le> n)"

  1063   apply (induct m)

  1064    apply simp

  1065   apply (case_tac n)

  1066    apply simp_all

  1067   done

  1068

  1069 lemma mult_eq_1_iff [simp]: "(m * n = Suc 0) = (m = 1 & n = 1)"

  1070   apply (induct m)

  1071    apply simp

  1072   apply (induct n)

  1073    apply auto

  1074   done

  1075

  1076 lemma one_eq_mult_iff [simp,noatp]: "(Suc 0 = m * n) = (m = 1 & n = 1)"

  1077   apply (rule trans)

  1078   apply (rule_tac [2] mult_eq_1_iff, fastsimp)

  1079   done

  1080

  1081 lemma mult_less_cancel2 [simp]: "((m::nat) * k < n * k) = (0 < k & m < n)"

  1082   apply (safe intro!: mult_less_mono1)

  1083   apply (case_tac k, auto)

  1084   apply (simp del: le_0_eq add: linorder_not_le [symmetric])

  1085   apply (blast intro: mult_le_mono1)

  1086   done

  1087

  1088 lemma mult_less_cancel1 [simp]: "(k * (m::nat) < k * n) = (0 < k & m < n)"

  1089 by (simp add: mult_commute [of k])

  1090

  1091 lemma mult_le_cancel1 [simp]: "(k * (m::nat) \<le> k * n) = (0 < k --> m \<le> n)"

  1092 by (simp add: linorder_not_less [symmetric], auto)

  1093

  1094 lemma mult_le_cancel2 [simp]: "((m::nat) * k \<le> n * k) = (0 < k --> m \<le> n)"

  1095 by (simp add: linorder_not_less [symmetric], auto)

  1096

  1097 lemma mult_cancel2 [simp]: "(m * k = n * k) = (m = n | (k = (0::nat)))"

  1098   apply (cut_tac less_linear, safe, auto)

  1099   apply (drule mult_less_mono1, assumption, simp)+

  1100   done

  1101

  1102 lemma mult_cancel1 [simp]: "(k * m = k * n) = (m = n | (k = (0::nat)))"

  1103 by (simp add: mult_commute [of k])

  1104

  1105 lemma Suc_mult_less_cancel1: "(Suc k * m < Suc k * n) = (m < n)"

  1106 by (subst mult_less_cancel1) simp

  1107

  1108 lemma Suc_mult_le_cancel1: "(Suc k * m \<le> Suc k * n) = (m \<le> n)"

  1109 by (subst mult_le_cancel1) simp

  1110

  1111 lemma Suc_mult_cancel1: "(Suc k * m = Suc k * n) = (m = n)"

  1112 by (subst mult_cancel1) simp

  1113

  1114 text {* Lemma for @{text gcd} *}

  1115 lemma mult_eq_self_implies_10: "(m::nat) = m * n ==> n = 1 | m = 0"

  1116   apply (drule sym)

  1117   apply (rule disjCI)

  1118   apply (rule nat_less_cases, erule_tac [2] _)

  1119   apply (fastsimp elim!: less_SucE)

  1120   apply (fastsimp dest: mult_less_mono2)

  1121   done

  1122

  1123

  1124 subsection {* Code generator setup *}

  1125

  1126 lemma one_is_Suc_zero [code inline]: "1 = Suc 0"

  1127 by simp

  1128

  1129 instance nat :: eq ..

  1130

  1131 lemma [code func]:

  1132     "(0\<Colon>nat) = 0 \<longleftrightarrow> True"

  1133     "Suc n = Suc m \<longleftrightarrow> n = m"

  1134     "Suc n = 0 \<longleftrightarrow> False"

  1135     "0 = Suc m \<longleftrightarrow> False"

  1136 by auto

  1137

  1138 lemma [code func]:

  1139     "(0\<Colon>nat) \<le> m \<longleftrightarrow> True"

  1140     "Suc (n\<Colon>nat) \<le> m \<longleftrightarrow> n < m"

  1141     "(n\<Colon>nat) < 0 \<longleftrightarrow> False"

  1142     "(n\<Colon>nat) < Suc m \<longleftrightarrow> n \<le> m"

  1143   using Suc_le_eq less_Suc_eq_le by simp_all

  1144

  1145

  1146 subsection{*Embedding of the Naturals into any

  1147   @{text semiring_1}: @{term of_nat}*}

  1148

  1149 context semiring_1

  1150 begin

  1151

  1152 definition

  1153   of_nat_def: "of_nat = nat_rec \<^loc>0 (\<lambda>_. (op \<^loc>+) \<^loc>1)"

  1154

  1155 end

  1156

  1157 subsection {* Further Arithmetic Facts Concerning the Natural Numbers *}

  1158

  1159 lemma subst_equals:

  1160   assumes 1: "t = s" and 2: "u = t"

  1161   shows "u = s"

  1162   using 2 1 by (rule trans)

  1163

  1164

  1165 use "arith_data.ML"

  1166 declaration {* K arith_data_setup *}

  1167

  1168 use "Tools/lin_arith.ML"

  1169 declaration {* K LinArith.setup *}

  1170

  1171

  1172 text{*The following proofs may rely on the arithmetic proof procedures.*}

  1173

  1174 lemma le_iff_add: "(m::nat) \<le> n = (\<exists>k. n = m + k)"

  1175 by (auto simp: le_eq_less_or_eq dest: less_imp_Suc_add)

  1176

  1177 lemma pred_nat_trancl_eq_le: "((m, n) : pred_nat^*) = (m \<le> n)"

  1178 by (simp add: less_eq reflcl_trancl [symmetric] del: reflcl_trancl, arith)

  1179

  1180 lemma nat_diff_split:

  1181   "P(a - b::nat) = ((a<b --> P 0) & (ALL d. a = b + d --> P d))"

  1182     -- {* elimination of @{text -} on @{text nat} *}

  1183 by (cases "a<b" rule: case_split) (auto simp add: diff_is_0_eq [THEN iffD2])

  1184

  1185 lemma nat_diff_split_asm:

  1186     "P(a - b::nat) = (~ (a < b & ~ P 0 | (EX d. a = b + d & ~ P d)))"

  1187     -- {* elimination of @{text -} on @{text nat} in assumptions *}

  1188 by (simp split: nat_diff_split)

  1189

  1190 lemmas [arith_split] = nat_diff_split split_min split_max

  1191

  1192

  1193 lemma le_square: "m \<le> m * (m::nat)"

  1194 by (induct m) auto

  1195

  1196 lemma le_cube: "(m::nat) \<le> m * (m * m)"

  1197 by (induct m) auto

  1198

  1199

  1200 text{*Subtraction laws, mostly by Clemens Ballarin*}

  1201

  1202 lemma diff_less_mono: "[| a < (b::nat); c \<le> a |] ==> a-c < b-c"

  1203 by arith

  1204

  1205 lemma less_diff_conv: "(i < j-k) = (i+k < (j::nat))"

  1206 by arith

  1207

  1208 lemma le_diff_conv: "(j-k \<le> (i::nat)) = (j \<le> i+k)"

  1209 by arith

  1210

  1211 lemma le_diff_conv2: "k \<le> j ==> (i \<le> j-k) = (i+k \<le> (j::nat))"

  1212 by arith

  1213

  1214 lemma diff_diff_cancel [simp]: "i \<le> (n::nat) ==> n - (n - i) = i"

  1215 by arith

  1216

  1217 lemma le_add_diff: "k \<le> (n::nat) ==> m \<le> n + m - k"

  1218 by arith

  1219

  1220 (*Replaces the previous diff_less and le_diff_less, which had the stronger

  1221   second premise n\<le>m*)

  1222 lemma diff_less[simp]: "!!m::nat. [| 0<n; 0<m |] ==> m - n < m"

  1223 by arith

  1224

  1225

  1226 (** Simplification of relational expressions involving subtraction **)

  1227

  1228 lemma diff_diff_eq: "[| k \<le> m;  k \<le> (n::nat) |] ==> ((m-k) - (n-k)) = (m-n)"

  1229 by (simp split add: nat_diff_split)

  1230

  1231 lemma eq_diff_iff: "[| k \<le> m;  k \<le> (n::nat) |] ==> (m-k = n-k) = (m=n)"

  1232 by (auto split add: nat_diff_split)

  1233

  1234 lemma less_diff_iff: "[| k \<le> m;  k \<le> (n::nat) |] ==> (m-k < n-k) = (m<n)"

  1235 by (auto split add: nat_diff_split)

  1236

  1237 lemma le_diff_iff: "[| k \<le> m;  k \<le> (n::nat) |] ==> (m-k \<le> n-k) = (m\<le>n)"

  1238 by (auto split add: nat_diff_split)

  1239

  1240

  1241 text{*(Anti)Monotonicity of subtraction -- by Stephan Merz*}

  1242

  1243 (* Monotonicity of subtraction in first argument *)

  1244 lemma diff_le_mono: "m \<le> (n::nat) ==> (m-l) \<le> (n-l)"

  1245 by (simp split add: nat_diff_split)

  1246

  1247 lemma diff_le_mono2: "m \<le> (n::nat) ==> (l-n) \<le> (l-m)"

  1248 by (simp split add: nat_diff_split)

  1249

  1250 lemma diff_less_mono2: "[| m < (n::nat); m<l |] ==> (l-n) < (l-m)"

  1251 by (simp split add: nat_diff_split)

  1252

  1253 lemma diffs0_imp_equal: "!!m::nat. [| m-n = 0; n-m = 0 |] ==>  m=n"

  1254 by (simp split add: nat_diff_split)

  1255

  1256 text{*Lemmas for ex/Factorization*}

  1257

  1258 lemma one_less_mult: "[| Suc 0 < n; Suc 0 < m |] ==> Suc 0 < m*n"

  1259 by (cases m) auto

  1260

  1261 lemma n_less_m_mult_n: "[| Suc 0 < n; Suc 0 < m |] ==> n<m*n"

  1262 by (cases m) auto

  1263

  1264 lemma n_less_n_mult_m: "[| Suc 0 < n; Suc 0 < m |] ==> n<n*m"

  1265 by (cases m) auto

  1266

  1267 text {* Specialized induction principles that work "backwards": *}

  1268

  1269 lemma inc_induct[consumes 1, case_names base step]:

  1270   assumes less: "i <= j"

  1271   assumes base: "P j"

  1272   assumes step: "!!i. [| i < j; P (Suc i) |] ==> P i"

  1273   shows "P i"

  1274   using less

  1275 proof (induct d=="j - i" arbitrary: i)

  1276   case (0 i)

  1277   hence "i = j" by simp

  1278   with base show ?case by simp

  1279 next

  1280   case (Suc d i)

  1281   hence "i < j" "P (Suc i)"

  1282     by simp_all

  1283   thus "P i" by (rule step)

  1284 qed

  1285

  1286 lemma strict_inc_induct[consumes 1, case_names base step]:

  1287   assumes less: "i < j"

  1288   assumes base: "!!i. j = Suc i ==> P i"

  1289   assumes step: "!!i. [| i < j; P (Suc i) |] ==> P i"

  1290   shows "P i"

  1291   using less

  1292 proof (induct d=="j - i - 1" arbitrary: i)

  1293   case (0 i)

  1294   with i < j have "j = Suc i" by simp

  1295   with base show ?case by simp

  1296 next

  1297   case (Suc d i)

  1298   hence "i < j" "P (Suc i)"

  1299     by simp_all

  1300   thus "P i" by (rule step)

  1301 qed

  1302

  1303 lemma zero_induct_lemma: "P k ==> (!!n. P (Suc n) ==> P n) ==> P (k - i)"

  1304   using inc_induct[of "k - i" k P, simplified] by blast

  1305

  1306 lemma zero_induct: "P k ==> (!!n. P (Suc n) ==> P n) ==> P 0"

  1307   using inc_induct[of 0 k P] by blast

  1308

  1309 text{*Rewriting to pull differences out*}

  1310

  1311 lemma diff_diff_right [simp]: "k\<le>j --> i - (j - k) = i + (k::nat) - j"

  1312 by arith

  1313

  1314 lemma diff_Suc_diff_eq1 [simp]: "k \<le> j ==> m - Suc (j - k) = m + k - Suc j"

  1315 by arith

  1316

  1317 lemma diff_Suc_diff_eq2 [simp]: "k \<le> j ==> Suc (j - k) - m = Suc j - (k + m)"

  1318 by arith

  1319

  1320 (*The others are

  1321       i - j - k = i - (j + k),

  1322       k \<le> j ==> j - k + i = j + i - k,

  1323       k \<le> j ==> i + (j - k) = i + j - k *)

  1324 lemmas add_diff_assoc = diff_add_assoc [symmetric]

  1325 lemmas add_diff_assoc2 = diff_add_assoc2[symmetric]

  1326 declare diff_diff_left [simp]  add_diff_assoc [simp]  add_diff_assoc2[simp]

  1327

  1328 text{*At present we prove no analogue of @{text not_less_Least} or @{text

  1329 Least_Suc}, since there appears to be no need.*}

  1330

  1331

  1332 subsection{*Embedding of the Naturals into any

  1333   @{text semiring_1}: @{term of_nat}*}

  1334

  1335 context semiring_1

  1336 begin

  1337

  1338 lemma of_nat_simps [simp, code]:

  1339   shows of_nat_0:   "of_nat 0 = \<^loc>0"

  1340     and of_nat_Suc: "of_nat (Suc m) = \<^loc>1 \<^loc>+ of_nat m"

  1341   unfolding of_nat_def by simp_all

  1342

  1343 end

  1344

  1345 lemma of_nat_id [simp]: "(of_nat n \<Colon> nat) = n"

  1346 by (induct n) auto

  1347

  1348 lemma of_nat_1 [simp]: "of_nat 1 = 1"

  1349 by simp

  1350

  1351 lemma of_nat_add [simp]: "of_nat (m + n) = of_nat m + of_nat n"

  1352 by (induct m) (simp_all add: add_ac)

  1353

  1354 lemma of_nat_mult: "of_nat (m*n) = of_nat m * of_nat n"

  1355 by (induct m) (simp_all add: add_ac left_distrib)

  1356

  1357 lemma zero_le_imp_of_nat: "0 \<le> (of_nat m::'a::ordered_semidom)"

  1358   apply (induct m, simp_all)

  1359   apply (erule order_trans)

  1360   apply (rule ord_le_eq_trans [OF _ add_commute])

  1361   apply (rule less_add_one [THEN order_less_imp_le])

  1362   done

  1363

  1364 lemma less_imp_of_nat_less:

  1365     "m < n ==> of_nat m < (of_nat n::'a::ordered_semidom)"

  1366   apply (induct m n rule: diff_induct, simp_all)

  1367   apply (insert add_less_le_mono [OF zero_less_one zero_le_imp_of_nat], force)

  1368   done

  1369

  1370 lemma of_nat_less_imp_less:

  1371     "of_nat m < (of_nat n::'a::ordered_semidom) ==> m < n"

  1372   apply (induct m n rule: diff_induct, simp_all)

  1373   apply (insert zero_le_imp_of_nat)

  1374   apply (force simp add: linorder_not_less [symmetric])

  1375   done

  1376

  1377 lemma of_nat_less_iff [simp]:

  1378     "(of_nat m < (of_nat n::'a::ordered_semidom)) = (m<n)"

  1379 by (blast intro: of_nat_less_imp_less less_imp_of_nat_less)

  1380

  1381 text{*Special cases where either operand is zero*}

  1382

  1383 lemma of_nat_0_less_iff [simp]: "((0::'a::ordered_semidom) < of_nat n) = (0 < n)"

  1384 by (rule of_nat_less_iff [of 0, simplified])

  1385

  1386 lemma of_nat_less_0_iff [simp]: "\<not> of_nat m < (0::'a::ordered_semidom)"

  1387 by (rule of_nat_less_iff [of _ 0, simplified])

  1388

  1389 lemma of_nat_le_iff [simp]:

  1390     "(of_nat m \<le> (of_nat n::'a::ordered_semidom)) = (m \<le> n)"

  1391 by (simp add: linorder_not_less [symmetric])

  1392

  1393 text{*Special cases where either operand is zero*}

  1394 lemma of_nat_0_le_iff [simp]: "(0::'a::ordered_semidom) \<le> of_nat n"

  1395 by (rule of_nat_le_iff [of 0, simplified])

  1396 lemma of_nat_le_0_iff [simp,noatp]: "(of_nat m \<le> (0::'a::ordered_semidom)) = (m = 0)"

  1397 by (rule of_nat_le_iff [of _ 0, simplified])

  1398

  1399 text{*Class for unital semirings with characteristic zero.

  1400  Includes non-ordered rings like the complex numbers.*}

  1401

  1402 class semiring_char_0 = semiring_1 +

  1403   assumes of_nat_eq_iff [simp]:

  1404     "(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)"

  1405

  1406 text{*Every @{text ordered_semidom} has characteristic zero.*}

  1407 instance ordered_semidom < semiring_char_0

  1408 by intro_classes (simp add: order_eq_iff)

  1409

  1410 text{*Special cases where either operand is zero*}

  1411 lemma of_nat_0_eq_iff [simp,noatp]: "((0::'a::semiring_char_0) = of_nat n) = (0 = n)"

  1412 by (rule of_nat_eq_iff [of 0, simplified])

  1413 lemma of_nat_eq_0_iff [simp,noatp]: "(of_nat m = (0::'a::semiring_char_0)) = (m = 0)"

  1414 by (rule of_nat_eq_iff [of _ 0, simplified])

  1415

  1416 lemma inj_of_nat: "inj (of_nat :: nat \<Rightarrow> 'a::semiring_char_0)"

  1417 by (simp add: inj_on_def)

  1418

  1419 lemma of_nat_diff:

  1420     "n \<le> m ==> of_nat (m - n) = of_nat m - (of_nat n :: 'a::ring_1)"

  1421 by (simp del: of_nat_add

  1422     add: compare_rls of_nat_add [symmetric] split add: nat_diff_split)

  1423

  1424 lemma abs_of_nat [simp]: "\<bar>of_nat n::'a::ordered_idom\<bar> = of_nat n"

  1425 by (rule of_nat_0_le_iff [THEN abs_of_nonneg])

  1426

  1427

  1428 subsection {*The Set of Natural Numbers*}

  1429

  1430 definition

  1431   Nats  :: "'a::semiring_1 set"

  1432 where

  1433   "Nats = range of_nat"

  1434

  1435 notation (xsymbols)

  1436   Nats  ("\<nat>")

  1437

  1438 lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats"

  1439 by (simp add: Nats_def)

  1440

  1441 lemma Nats_0 [simp]: "0 \<in> Nats"

  1442 apply (simp add: Nats_def)

  1443 apply (rule range_eqI)

  1444 apply (rule of_nat_0 [symmetric])

  1445 done

  1446

  1447 lemma Nats_1 [simp]: "1 \<in> Nats"

  1448 apply (simp add: Nats_def)

  1449 apply (rule range_eqI)

  1450 apply (rule of_nat_1 [symmetric])

  1451 done

  1452

  1453 lemma Nats_add [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a+b \<in> Nats"

  1454 apply (auto simp add: Nats_def)

  1455 apply (rule range_eqI)

  1456 apply (rule of_nat_add [symmetric])

  1457 done

  1458

  1459 lemma Nats_mult [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a*b \<in> Nats"

  1460 apply (auto simp add: Nats_def)

  1461 apply (rule range_eqI)

  1462 apply (rule of_nat_mult [symmetric])

  1463 done

  1464

  1465 lemma of_nat_eq_id [simp]: "of_nat = (id :: nat => nat)"

  1466 by (auto simp add: expand_fun_eq)

  1467

  1468

  1469 instance nat :: distrib_lattice

  1470   "inf \<equiv> min"

  1471   "sup \<equiv> max"

  1472 by intro_classes (auto simp add: inf_nat_def sup_nat_def)

  1473

  1474

  1475 subsection {* Size function declarations *}

  1476

  1477 lemma nat_size [simp, code func]: "size (n\<Colon>nat) = n"

  1478   by (induct n) simp_all

  1479

  1480 lemma size_bool [code func]:

  1481   "size (b\<Colon>bool) = 0" by (cases b) auto

  1482

  1483 declare "*.size" [noatp]

  1484

  1485

  1486 subsection {* legacy bindings *}

  1487

  1488 ML

  1489 {*

  1490 val pred_nat_trancl_eq_le = thm "pred_nat_trancl_eq_le";

  1491 val nat_diff_split = thm "nat_diff_split";

  1492 val nat_diff_split_asm = thm "nat_diff_split_asm";

  1493 val le_square = thm "le_square";

  1494 val le_cube = thm "le_cube";

  1495 val diff_less_mono = thm "diff_less_mono";

  1496 val less_diff_conv = thm "less_diff_conv";

  1497 val le_diff_conv = thm "le_diff_conv";

  1498 val le_diff_conv2 = thm "le_diff_conv2";

  1499 val diff_diff_cancel = thm "diff_diff_cancel";

  1500 val le_add_diff = thm "le_add_diff";

  1501 val diff_less = thm "diff_less";

  1502 val diff_diff_eq = thm "diff_diff_eq";

  1503 val eq_diff_iff = thm "eq_diff_iff";

  1504 val less_diff_iff = thm "less_diff_iff";

  1505 val le_diff_iff = thm "le_diff_iff";

  1506 val diff_le_mono = thm "diff_le_mono";

  1507 val diff_le_mono2 = thm "diff_le_mono2";

  1508 val diff_less_mono2 = thm "diff_less_mono2";

  1509 val diffs0_imp_equal = thm "diffs0_imp_equal";

  1510 val one_less_mult = thm "one_less_mult";

  1511 val n_less_m_mult_n = thm "n_less_m_mult_n";

  1512 val n_less_n_mult_m = thm "n_less_n_mult_m";

  1513 val diff_diff_right = thm "diff_diff_right";

  1514 val diff_Suc_diff_eq1 = thm "diff_Suc_diff_eq1";

  1515 val diff_Suc_diff_eq2 = thm "diff_Suc_diff_eq2";

  1516 *}

  1517

  1518 end