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