src/HOL/Nat.thy
author huffman
Sun Mar 07 07:42:46 2010 -0800 (2010-03-07)
changeset 35633 5da59c1ddece
parent 35416 d8d7d1b785af
child 35828 46cfc4b8112e
permissions -rw-r--r--
add lemmas Nats_cases and Nats_induct
     1 (*  Title:      HOL/Nat.thy
     2     Author:     Tobias Nipkow and Lawrence C Paulson and Markus Wenzel
     3 
     4 Type "nat" is a linear order, and a datatype; arithmetic operators + -
     5 and * (for div and mod, see theory Divides).
     6 *)
     7 
     8 header {* Natural numbers *}
     9 
    10 theory Nat
    11 imports Inductive Typedef Fun Fields
    12 uses
    13   "~~/src/Tools/rat.ML"
    14   "~~/src/Provers/Arith/cancel_sums.ML"
    15   "Tools/arith_data.ML"
    16   ("Tools/nat_arith.ML")
    17   "~~/src/Provers/Arith/fast_lin_arith.ML"
    18   ("Tools/lin_arith.ML")
    19 begin
    20 
    21 subsection {* Type @{text ind} *}
    22 
    23 typedecl ind
    24 
    25 axiomatization
    26   Zero_Rep :: ind and
    27   Suc_Rep :: "ind => ind"
    28 where
    29   -- {* the axiom of infinity in 2 parts *}
    30   Suc_Rep_inject:       "Suc_Rep x = Suc_Rep y ==> x = y" and
    31   Suc_Rep_not_Zero_Rep: "Suc_Rep x \<noteq> Zero_Rep"
    32 
    33 subsection {* Type nat *}
    34 
    35 text {* Type definition *}
    36 
    37 inductive Nat :: "ind \<Rightarrow> bool"
    38 where
    39     Zero_RepI: "Nat Zero_Rep"
    40   | Suc_RepI: "Nat i \<Longrightarrow> Nat (Suc_Rep i)"
    41 
    42 global
    43 
    44 typedef (open Nat)
    45   nat = Nat
    46   by (rule exI, unfold mem_def, rule Nat.Zero_RepI)
    47 
    48 definition Suc :: "nat => nat" where
    49   Suc_def: "Suc == (%n. Abs_Nat (Suc_Rep (Rep_Nat n)))"
    50 
    51 local
    52 
    53 instantiation nat :: zero
    54 begin
    55 
    56 definition Zero_nat_def [code del]:
    57   "0 = Abs_Nat Zero_Rep"
    58 
    59 instance ..
    60 
    61 end
    62 
    63 lemma Suc_not_Zero: "Suc m \<noteq> 0"
    64   by (simp add: Zero_nat_def Suc_def Abs_Nat_inject [unfolded mem_def]
    65     Rep_Nat [unfolded mem_def] Suc_RepI Zero_RepI Suc_Rep_not_Zero_Rep [unfolded mem_def])
    66 
    67 lemma Zero_not_Suc: "0 \<noteq> Suc m"
    68   by (rule not_sym, rule Suc_not_Zero not_sym)
    69 
    70 lemma Suc_Rep_inject': "Suc_Rep x = Suc_Rep y \<longleftrightarrow> x = y"
    71   by (rule iffI, rule Suc_Rep_inject) simp_all
    72 
    73 rep_datatype "0 \<Colon> nat" Suc
    74   apply (unfold Zero_nat_def Suc_def)
    75      apply (rule Rep_Nat_inverse [THEN subst]) -- {* types force good instantiation *}
    76      apply (erule Rep_Nat [unfolded mem_def, THEN Nat.induct])
    77      apply (iprover elim: Abs_Nat_inverse [unfolded mem_def, THEN subst])
    78     apply (simp_all add: Abs_Nat_inject [unfolded mem_def] Rep_Nat [unfolded mem_def]
    79       Suc_RepI Zero_RepI Suc_Rep_not_Zero_Rep [unfolded mem_def]
    80       Suc_Rep_not_Zero_Rep [unfolded mem_def, symmetric]
    81       Suc_Rep_inject' Rep_Nat_inject)
    82   done
    83 
    84 lemma nat_induct [case_names 0 Suc, induct type: nat]:
    85   -- {* for backward compatibility -- names of variables differ *}
    86   fixes n
    87   assumes "P 0"
    88     and "\<And>n. P n \<Longrightarrow> P (Suc n)"
    89   shows "P n"
    90   using assms by (rule nat.induct)
    91 
    92 declare nat.exhaust [case_names 0 Suc, cases type: nat]
    93 
    94 lemmas nat_rec_0 = nat.recs(1)
    95   and nat_rec_Suc = nat.recs(2)
    96 
    97 lemmas nat_case_0 = nat.cases(1)
    98   and nat_case_Suc = nat.cases(2)
    99    
   100 
   101 text {* Injectiveness and distinctness lemmas *}
   102 
   103 lemma inj_Suc[simp]: "inj_on Suc N"
   104   by (simp add: inj_on_def)
   105 
   106 lemma Suc_neq_Zero: "Suc m = 0 \<Longrightarrow> R"
   107 by (rule notE, rule Suc_not_Zero)
   108 
   109 lemma Zero_neq_Suc: "0 = Suc m \<Longrightarrow> R"
   110 by (rule Suc_neq_Zero, erule sym)
   111 
   112 lemma Suc_inject: "Suc x = Suc y \<Longrightarrow> x = y"
   113 by (rule inj_Suc [THEN injD])
   114 
   115 lemma n_not_Suc_n: "n \<noteq> Suc n"
   116 by (induct n) simp_all
   117 
   118 lemma Suc_n_not_n: "Suc n \<noteq> n"
   119 by (rule not_sym, rule n_not_Suc_n)
   120 
   121 text {* A special form of induction for reasoning
   122   about @{term "m < n"} and @{term "m - n"} *}
   123 
   124 lemma diff_induct: "(!!x. P x 0) ==> (!!y. P 0 (Suc y)) ==>
   125     (!!x y. P x y ==> P (Suc x) (Suc y)) ==> P m n"
   126   apply (rule_tac x = m in spec)
   127   apply (induct n)
   128   prefer 2
   129   apply (rule allI)
   130   apply (induct_tac x, iprover+)
   131   done
   132 
   133 
   134 subsection {* Arithmetic operators *}
   135 
   136 instantiation nat :: "{minus, comm_monoid_add}"
   137 begin
   138 
   139 primrec plus_nat
   140 where
   141   add_0:      "0 + n = (n\<Colon>nat)"
   142   | add_Suc:  "Suc m + n = Suc (m + n)"
   143 
   144 lemma add_0_right [simp]: "m + 0 = (m::nat)"
   145   by (induct m) simp_all
   146 
   147 lemma add_Suc_right [simp]: "m + Suc n = Suc (m + n)"
   148   by (induct m) simp_all
   149 
   150 declare add_0 [code]
   151 
   152 lemma add_Suc_shift [code]: "Suc m + n = m + Suc n"
   153   by simp
   154 
   155 primrec minus_nat
   156 where
   157   diff_0:     "m - 0 = (m\<Colon>nat)"
   158   | diff_Suc: "m - Suc n = (case m - n of 0 => 0 | Suc k => k)"
   159 
   160 declare diff_Suc [simp del]
   161 declare diff_0 [code]
   162 
   163 lemma diff_0_eq_0 [simp, code]: "0 - n = (0::nat)"
   164   by (induct n) (simp_all add: diff_Suc)
   165 
   166 lemma diff_Suc_Suc [simp, code]: "Suc m - Suc n = m - n"
   167   by (induct n) (simp_all add: diff_Suc)
   168 
   169 instance proof
   170   fix n m q :: nat
   171   show "(n + m) + q = n + (m + q)" by (induct n) simp_all
   172   show "n + m = m + n" by (induct n) simp_all
   173   show "0 + n = n" by simp
   174 qed
   175 
   176 end
   177 
   178 hide (open) fact add_0 add_0_right diff_0
   179 
   180 instantiation nat :: comm_semiring_1_cancel
   181 begin
   182 
   183 definition
   184   One_nat_def [simp]: "1 = Suc 0"
   185 
   186 primrec times_nat
   187 where
   188   mult_0:     "0 * n = (0\<Colon>nat)"
   189   | mult_Suc: "Suc m * n = n + (m * n)"
   190 
   191 lemma mult_0_right [simp]: "(m::nat) * 0 = 0"
   192   by (induct m) simp_all
   193 
   194 lemma mult_Suc_right [simp]: "m * Suc n = m + (m * n)"
   195   by (induct m) (simp_all add: add_left_commute)
   196 
   197 lemma add_mult_distrib: "(m + n) * k = (m * k) + ((n * k)::nat)"
   198   by (induct m) (simp_all add: add_assoc)
   199 
   200 instance proof
   201   fix n m q :: nat
   202   show "0 \<noteq> (1::nat)" unfolding One_nat_def by simp
   203   show "1 * n = n" unfolding One_nat_def by simp
   204   show "n * m = m * n" by (induct n) simp_all
   205   show "(n * m) * q = n * (m * q)" by (induct n) (simp_all add: add_mult_distrib)
   206   show "(n + m) * q = n * q + m * q" by (rule add_mult_distrib)
   207   assume "n + m = n + q" thus "m = q" by (induct n) simp_all
   208 qed
   209 
   210 end
   211 
   212 subsubsection {* Addition *}
   213 
   214 lemma nat_add_assoc: "(m + n) + k = m + ((n + k)::nat)"
   215   by (rule add_assoc)
   216 
   217 lemma nat_add_commute: "m + n = n + (m::nat)"
   218   by (rule add_commute)
   219 
   220 lemma nat_add_left_commute: "x + (y + z) = y + ((x + z)::nat)"
   221   by (rule add_left_commute)
   222 
   223 lemma nat_add_left_cancel [simp]: "(k + m = k + n) = (m = (n::nat))"
   224   by (rule add_left_cancel)
   225 
   226 lemma nat_add_right_cancel [simp]: "(m + k = n + k) = (m=(n::nat))"
   227   by (rule add_right_cancel)
   228 
   229 text {* Reasoning about @{text "m + 0 = 0"}, etc. *}
   230 
   231 lemma add_is_0 [iff]:
   232   fixes m n :: nat
   233   shows "(m + n = 0) = (m = 0 & n = 0)"
   234   by (cases m) simp_all
   235 
   236 lemma add_is_1:
   237   "(m+n= Suc 0) = (m= Suc 0 & n=0 | m=0 & n= Suc 0)"
   238   by (cases m) simp_all
   239 
   240 lemma one_is_add:
   241   "(Suc 0 = m + n) = (m = Suc 0 & n = 0 | m = 0 & n = Suc 0)"
   242   by (rule trans, rule eq_commute, rule add_is_1)
   243 
   244 lemma add_eq_self_zero:
   245   fixes m n :: nat
   246   shows "m + n = m \<Longrightarrow> n = 0"
   247   by (induct m) simp_all
   248 
   249 lemma inj_on_add_nat[simp]: "inj_on (%n::nat. n+k) N"
   250   apply (induct k)
   251    apply simp
   252   apply(drule comp_inj_on[OF _ inj_Suc])
   253   apply (simp add:o_def)
   254   done
   255 
   256 
   257 subsubsection {* Difference *}
   258 
   259 lemma diff_self_eq_0 [simp]: "(m\<Colon>nat) - m = 0"
   260   by (induct m) simp_all
   261 
   262 lemma diff_diff_left: "(i::nat) - j - k = i - (j + k)"
   263   by (induct i j rule: diff_induct) simp_all
   264 
   265 lemma Suc_diff_diff [simp]: "(Suc m - n) - Suc k = m - n - k"
   266   by (simp add: diff_diff_left)
   267 
   268 lemma diff_commute: "(i::nat) - j - k = i - k - j"
   269   by (simp add: diff_diff_left add_commute)
   270 
   271 lemma diff_add_inverse: "(n + m) - n = (m::nat)"
   272   by (induct n) simp_all
   273 
   274 lemma diff_add_inverse2: "(m + n) - n = (m::nat)"
   275   by (simp add: diff_add_inverse add_commute [of m n])
   276 
   277 lemma diff_cancel: "(k + m) - (k + n) = m - (n::nat)"
   278   by (induct k) simp_all
   279 
   280 lemma diff_cancel2: "(m + k) - (n + k) = m - (n::nat)"
   281   by (simp add: diff_cancel add_commute)
   282 
   283 lemma diff_add_0: "n - (n + m) = (0::nat)"
   284   by (induct n) simp_all
   285 
   286 lemma diff_Suc_1 [simp]: "Suc n - 1 = n"
   287   unfolding One_nat_def by simp
   288 
   289 text {* Difference distributes over multiplication *}
   290 
   291 lemma diff_mult_distrib: "((m::nat) - n) * k = (m * k) - (n * k)"
   292 by (induct m n rule: diff_induct) (simp_all add: diff_cancel)
   293 
   294 lemma diff_mult_distrib2: "k * ((m::nat) - n) = (k * m) - (k * n)"
   295 by (simp add: diff_mult_distrib mult_commute [of k])
   296   -- {* NOT added as rewrites, since sometimes they are used from right-to-left *}
   297 
   298 
   299 subsubsection {* Multiplication *}
   300 
   301 lemma nat_mult_assoc: "(m * n) * k = m * ((n * k)::nat)"
   302   by (rule mult_assoc)
   303 
   304 lemma nat_mult_commute: "m * n = n * (m::nat)"
   305   by (rule mult_commute)
   306 
   307 lemma add_mult_distrib2: "k * (m + n) = (k * m) + ((k * n)::nat)"
   308   by (rule right_distrib)
   309 
   310 lemma mult_is_0 [simp]: "((m::nat) * n = 0) = (m=0 | n=0)"
   311   by (induct m) auto
   312 
   313 lemmas nat_distrib =
   314   add_mult_distrib add_mult_distrib2 diff_mult_distrib diff_mult_distrib2
   315 
   316 lemma mult_eq_1_iff [simp]: "(m * n = Suc 0) = (m = Suc 0 & n = Suc 0)"
   317   apply (induct m)
   318    apply simp
   319   apply (induct n)
   320    apply auto
   321   done
   322 
   323 lemma one_eq_mult_iff [simp,noatp]: "(Suc 0 = m * n) = (m = Suc 0 & n = Suc 0)"
   324   apply (rule trans)
   325   apply (rule_tac [2] mult_eq_1_iff, fastsimp)
   326   done
   327 
   328 lemma nat_mult_eq_1_iff [simp]: "m * n = (1::nat) \<longleftrightarrow> m = 1 \<and> n = 1"
   329   unfolding One_nat_def by (rule mult_eq_1_iff)
   330 
   331 lemma nat_1_eq_mult_iff [simp]: "(1::nat) = m * n \<longleftrightarrow> m = 1 \<and> n = 1"
   332   unfolding One_nat_def by (rule one_eq_mult_iff)
   333 
   334 lemma mult_cancel1 [simp]: "(k * m = k * n) = (m = n | (k = (0::nat)))"
   335 proof -
   336   have "k \<noteq> 0 \<Longrightarrow> k * m = k * n \<Longrightarrow> m = n"
   337   proof (induct n arbitrary: m)
   338     case 0 then show "m = 0" by simp
   339   next
   340     case (Suc n) then show "m = Suc n"
   341       by (cases m) (simp_all add: eq_commute [of "0"])
   342   qed
   343   then show ?thesis by auto
   344 qed
   345 
   346 lemma mult_cancel2 [simp]: "(m * k = n * k) = (m = n | (k = (0::nat)))"
   347   by (simp add: mult_commute)
   348 
   349 lemma Suc_mult_cancel1: "(Suc k * m = Suc k * n) = (m = n)"
   350   by (subst mult_cancel1) simp
   351 
   352 
   353 subsection {* Orders on @{typ nat} *}
   354 
   355 subsubsection {* Operation definition *}
   356 
   357 instantiation nat :: linorder
   358 begin
   359 
   360 primrec less_eq_nat where
   361   "(0\<Colon>nat) \<le> n \<longleftrightarrow> True"
   362   | "Suc m \<le> n \<longleftrightarrow> (case n of 0 \<Rightarrow> False | Suc n \<Rightarrow> m \<le> n)"
   363 
   364 declare less_eq_nat.simps [simp del]
   365 lemma [code]: "(0\<Colon>nat) \<le> n \<longleftrightarrow> True" by (simp add: less_eq_nat.simps)
   366 lemma le0 [iff]: "0 \<le> (n\<Colon>nat)" by (simp add: less_eq_nat.simps)
   367 
   368 definition less_nat where
   369   less_eq_Suc_le: "n < m \<longleftrightarrow> Suc n \<le> m"
   370 
   371 lemma Suc_le_mono [iff]: "Suc n \<le> Suc m \<longleftrightarrow> n \<le> m"
   372   by (simp add: less_eq_nat.simps(2))
   373 
   374 lemma Suc_le_eq [code]: "Suc m \<le> n \<longleftrightarrow> m < n"
   375   unfolding less_eq_Suc_le ..
   376 
   377 lemma le_0_eq [iff]: "(n\<Colon>nat) \<le> 0 \<longleftrightarrow> n = 0"
   378   by (induct n) (simp_all add: less_eq_nat.simps(2))
   379 
   380 lemma not_less0 [iff]: "\<not> n < (0\<Colon>nat)"
   381   by (simp add: less_eq_Suc_le)
   382 
   383 lemma less_nat_zero_code [code]: "n < (0\<Colon>nat) \<longleftrightarrow> False"
   384   by simp
   385 
   386 lemma Suc_less_eq [iff]: "Suc m < Suc n \<longleftrightarrow> m < n"
   387   by (simp add: less_eq_Suc_le)
   388 
   389 lemma less_Suc_eq_le [code]: "m < Suc n \<longleftrightarrow> m \<le> n"
   390   by (simp add: less_eq_Suc_le)
   391 
   392 lemma le_SucI: "m \<le> n \<Longrightarrow> m \<le> Suc n"
   393   by (induct m arbitrary: n)
   394     (simp_all add: less_eq_nat.simps(2) split: nat.splits)
   395 
   396 lemma Suc_leD: "Suc m \<le> n \<Longrightarrow> m \<le> n"
   397   by (cases n) (auto intro: le_SucI)
   398 
   399 lemma less_SucI: "m < n \<Longrightarrow> m < Suc n"
   400   by (simp add: less_eq_Suc_le) (erule Suc_leD)
   401 
   402 lemma Suc_lessD: "Suc m < n \<Longrightarrow> m < n"
   403   by (simp add: less_eq_Suc_le) (erule Suc_leD)
   404 
   405 instance
   406 proof
   407   fix n m :: nat
   408   show "n < m \<longleftrightarrow> n \<le> m \<and> \<not> m \<le> n" 
   409   proof (induct n arbitrary: m)
   410     case 0 then show ?case by (cases m) (simp_all add: less_eq_Suc_le)
   411   next
   412     case (Suc n) then show ?case by (cases m) (simp_all add: less_eq_Suc_le)
   413   qed
   414 next
   415   fix n :: nat show "n \<le> n" by (induct n) simp_all
   416 next
   417   fix n m :: nat assume "n \<le> m" and "m \<le> n"
   418   then show "n = m"
   419     by (induct n arbitrary: m)
   420       (simp_all add: less_eq_nat.simps(2) split: nat.splits)
   421 next
   422   fix n m q :: nat assume "n \<le> m" and "m \<le> q"
   423   then show "n \<le> q"
   424   proof (induct n arbitrary: m q)
   425     case 0 show ?case by simp
   426   next
   427     case (Suc n) then show ?case
   428       by (simp_all (no_asm_use) add: less_eq_nat.simps(2) split: nat.splits, clarify,
   429         simp_all (no_asm_use) add: less_eq_nat.simps(2) split: nat.splits, clarify,
   430         simp_all (no_asm_use) add: less_eq_nat.simps(2) split: nat.splits)
   431   qed
   432 next
   433   fix n m :: nat show "n \<le> m \<or> m \<le> n"
   434     by (induct n arbitrary: m)
   435       (simp_all add: less_eq_nat.simps(2) split: nat.splits)
   436 qed
   437 
   438 end
   439 
   440 instantiation nat :: bot
   441 begin
   442 
   443 definition bot_nat :: nat where
   444   "bot_nat = 0"
   445 
   446 instance proof
   447 qed (simp add: bot_nat_def)
   448 
   449 end
   450 
   451 subsubsection {* Introduction properties *}
   452 
   453 lemma lessI [iff]: "n < Suc n"
   454   by (simp add: less_Suc_eq_le)
   455 
   456 lemma zero_less_Suc [iff]: "0 < Suc n"
   457   by (simp add: less_Suc_eq_le)
   458 
   459 
   460 subsubsection {* Elimination properties *}
   461 
   462 lemma less_not_refl: "~ n < (n::nat)"
   463   by (rule order_less_irrefl)
   464 
   465 lemma less_not_refl2: "n < m ==> m \<noteq> (n::nat)"
   466   by (rule not_sym) (rule less_imp_neq) 
   467 
   468 lemma less_not_refl3: "(s::nat) < t ==> s \<noteq> t"
   469   by (rule less_imp_neq)
   470 
   471 lemma less_irrefl_nat: "(n::nat) < n ==> R"
   472   by (rule notE, rule less_not_refl)
   473 
   474 lemma less_zeroE: "(n::nat) < 0 ==> R"
   475   by (rule notE) (rule not_less0)
   476 
   477 lemma less_Suc_eq: "(m < Suc n) = (m < n | m = n)"
   478   unfolding less_Suc_eq_le le_less ..
   479 
   480 lemma less_Suc0 [iff]: "(n < Suc 0) = (n = 0)"
   481   by (simp add: less_Suc_eq)
   482 
   483 lemma less_one [iff, noatp]: "(n < (1::nat)) = (n = 0)"
   484   unfolding One_nat_def by (rule less_Suc0)
   485 
   486 lemma Suc_mono: "m < n ==> Suc m < Suc n"
   487   by simp
   488 
   489 text {* "Less than" is antisymmetric, sort of *}
   490 lemma less_antisym: "\<lbrakk> \<not> n < m; n < Suc m \<rbrakk> \<Longrightarrow> m = n"
   491   unfolding not_less less_Suc_eq_le by (rule antisym)
   492 
   493 lemma nat_neq_iff: "((m::nat) \<noteq> n) = (m < n | n < m)"
   494   by (rule linorder_neq_iff)
   495 
   496 lemma nat_less_cases: assumes major: "(m::nat) < n ==> P n m"
   497   and eqCase: "m = n ==> P n m" and lessCase: "n<m ==> P n m"
   498   shows "P n m"
   499   apply (rule less_linear [THEN disjE])
   500   apply (erule_tac [2] disjE)
   501   apply (erule lessCase)
   502   apply (erule sym [THEN eqCase])
   503   apply (erule major)
   504   done
   505 
   506 
   507 subsubsection {* Inductive (?) properties *}
   508 
   509 lemma Suc_lessI: "m < n ==> Suc m \<noteq> n ==> Suc m < n"
   510   unfolding less_eq_Suc_le [of m] le_less by simp 
   511 
   512 lemma lessE:
   513   assumes major: "i < k"
   514   and p1: "k = Suc i ==> P" and p2: "!!j. i < j ==> k = Suc j ==> P"
   515   shows P
   516 proof -
   517   from major have "\<exists>j. i \<le> j \<and> k = Suc j"
   518     unfolding less_eq_Suc_le by (induct k) simp_all
   519   then have "(\<exists>j. i < j \<and> k = Suc j) \<or> k = Suc i"
   520     by (clarsimp simp add: less_le)
   521   with p1 p2 show P by auto
   522 qed
   523 
   524 lemma less_SucE: assumes major: "m < Suc n"
   525   and less: "m < n ==> P" and eq: "m = n ==> P" shows P
   526   apply (rule major [THEN lessE])
   527   apply (rule eq, blast)
   528   apply (rule less, blast)
   529   done
   530 
   531 lemma Suc_lessE: assumes major: "Suc i < k"
   532   and minor: "!!j. i < j ==> k = Suc j ==> P" shows P
   533   apply (rule major [THEN lessE])
   534   apply (erule lessI [THEN minor])
   535   apply (erule Suc_lessD [THEN minor], assumption)
   536   done
   537 
   538 lemma Suc_less_SucD: "Suc m < Suc n ==> m < n"
   539   by simp
   540 
   541 lemma less_trans_Suc:
   542   assumes le: "i < j" shows "j < k ==> Suc i < k"
   543   apply (induct k, simp_all)
   544   apply (insert le)
   545   apply (simp add: less_Suc_eq)
   546   apply (blast dest: Suc_lessD)
   547   done
   548 
   549 text {* Can be used with @{text less_Suc_eq} to get @{term "n = m | n < m"} *}
   550 lemma not_less_eq: "\<not> m < n \<longleftrightarrow> n < Suc m"
   551   unfolding not_less less_Suc_eq_le ..
   552 
   553 lemma not_less_eq_eq: "\<not> m \<le> n \<longleftrightarrow> Suc n \<le> m"
   554   unfolding not_le Suc_le_eq ..
   555 
   556 text {* Properties of "less than or equal" *}
   557 
   558 lemma le_imp_less_Suc: "m \<le> n ==> m < Suc n"
   559   unfolding less_Suc_eq_le .
   560 
   561 lemma Suc_n_not_le_n: "~ Suc n \<le> n"
   562   unfolding not_le less_Suc_eq_le ..
   563 
   564 lemma le_Suc_eq: "(m \<le> Suc n) = (m \<le> n | m = Suc n)"
   565   by (simp add: less_Suc_eq_le [symmetric] less_Suc_eq)
   566 
   567 lemma le_SucE: "m \<le> Suc n ==> (m \<le> n ==> R) ==> (m = Suc n ==> R) ==> R"
   568   by (drule le_Suc_eq [THEN iffD1], iprover+)
   569 
   570 lemma Suc_leI: "m < n ==> Suc(m) \<le> n"
   571   unfolding Suc_le_eq .
   572 
   573 text {* Stronger version of @{text Suc_leD} *}
   574 lemma Suc_le_lessD: "Suc m \<le> n ==> m < n"
   575   unfolding Suc_le_eq .
   576 
   577 lemma less_imp_le_nat: "m < n ==> m \<le> (n::nat)"
   578   unfolding less_eq_Suc_le by (rule Suc_leD)
   579 
   580 text {* For instance, @{text "(Suc m < Suc n) = (Suc m \<le> n) = (m < n)"} *}
   581 lemmas le_simps = less_imp_le_nat less_Suc_eq_le Suc_le_eq
   582 
   583 
   584 text {* Equivalence of @{term "m \<le> n"} and @{term "m < n | m = n"} *}
   585 
   586 lemma less_or_eq_imp_le: "m < n | m = n ==> m \<le> (n::nat)"
   587   unfolding le_less .
   588 
   589 lemma le_eq_less_or_eq: "(m \<le> (n::nat)) = (m < n | m=n)"
   590   by (rule le_less)
   591 
   592 text {* Useful with @{text blast}. *}
   593 lemma eq_imp_le: "(m::nat) = n ==> m \<le> n"
   594   by auto
   595 
   596 lemma le_refl: "n \<le> (n::nat)"
   597   by simp
   598 
   599 lemma le_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::nat)"
   600   by (rule order_trans)
   601 
   602 lemma le_antisym: "[| m \<le> n; n \<le> m |] ==> m = (n::nat)"
   603   by (rule antisym)
   604 
   605 lemma nat_less_le: "((m::nat) < n) = (m \<le> n & m \<noteq> n)"
   606   by (rule less_le)
   607 
   608 lemma le_neq_implies_less: "(m::nat) \<le> n ==> m \<noteq> n ==> m < n"
   609   unfolding less_le ..
   610 
   611 lemma nat_le_linear: "(m::nat) \<le> n | n \<le> m"
   612   by (rule linear)
   613 
   614 lemmas linorder_neqE_nat = linorder_neqE [where 'a = nat]
   615 
   616 lemma le_less_Suc_eq: "m \<le> n ==> (n < Suc m) = (n = m)"
   617   unfolding less_Suc_eq_le by auto
   618 
   619 lemma not_less_less_Suc_eq: "~ n < m ==> (n < Suc m) = (n = m)"
   620   unfolding not_less by (rule le_less_Suc_eq)
   621 
   622 lemmas not_less_simps = not_less_less_Suc_eq le_less_Suc_eq
   623 
   624 text {* These two rules ease the use of primitive recursion.
   625 NOTE USE OF @{text "=="} *}
   626 lemma def_nat_rec_0: "(!!n. f n == nat_rec c h n) ==> f 0 = c"
   627 by simp
   628 
   629 lemma def_nat_rec_Suc: "(!!n. f n == nat_rec c h n) ==> f (Suc n) = h n (f n)"
   630 by simp
   631 
   632 lemma not0_implies_Suc: "n \<noteq> 0 ==> \<exists>m. n = Suc m"
   633 by (cases n) simp_all
   634 
   635 lemma gr0_implies_Suc: "n > 0 ==> \<exists>m. n = Suc m"
   636 by (cases n) simp_all
   637 
   638 lemma gr_implies_not0: fixes n :: nat shows "m<n ==> n \<noteq> 0"
   639 by (cases n) simp_all
   640 
   641 lemma neq0_conv[iff]: fixes n :: nat shows "(n \<noteq> 0) = (0 < n)"
   642 by (cases n) simp_all
   643 
   644 text {* This theorem is useful with @{text blast} *}
   645 lemma gr0I: "((n::nat) = 0 ==> False) ==> 0 < n"
   646 by (rule neq0_conv[THEN iffD1], iprover)
   647 
   648 lemma gr0_conv_Suc: "(0 < n) = (\<exists>m. n = Suc m)"
   649 by (fast intro: not0_implies_Suc)
   650 
   651 lemma not_gr0 [iff,noatp]: "!!n::nat. (~ (0 < n)) = (n = 0)"
   652 using neq0_conv by blast
   653 
   654 lemma Suc_le_D: "(Suc n \<le> m') ==> (? m. m' = Suc m)"
   655 by (induct m') simp_all
   656 
   657 text {* Useful in certain inductive arguments *}
   658 lemma less_Suc_eq_0_disj: "(m < Suc n) = (m = 0 | (\<exists>j. m = Suc j & j < n))"
   659 by (cases m) simp_all
   660 
   661 
   662 subsubsection {* @{term min} and @{term max} *}
   663 
   664 lemma mono_Suc: "mono Suc"
   665 by (rule monoI) simp
   666 
   667 lemma min_0L [simp]: "min 0 n = (0::nat)"
   668 by (rule min_leastL) simp
   669 
   670 lemma min_0R [simp]: "min n 0 = (0::nat)"
   671 by (rule min_leastR) simp
   672 
   673 lemma min_Suc_Suc [simp]: "min (Suc m) (Suc n) = Suc (min m n)"
   674 by (simp add: mono_Suc min_of_mono)
   675 
   676 lemma min_Suc1:
   677    "min (Suc n) m = (case m of 0 => 0 | Suc m' => Suc(min n m'))"
   678 by (simp split: nat.split)
   679 
   680 lemma min_Suc2:
   681    "min m (Suc n) = (case m of 0 => 0 | Suc m' => Suc(min m' n))"
   682 by (simp split: nat.split)
   683 
   684 lemma max_0L [simp]: "max 0 n = (n::nat)"
   685 by (rule max_leastL) simp
   686 
   687 lemma max_0R [simp]: "max n 0 = (n::nat)"
   688 by (rule max_leastR) simp
   689 
   690 lemma max_Suc_Suc [simp]: "max (Suc m) (Suc n) = Suc(max m n)"
   691 by (simp add: mono_Suc max_of_mono)
   692 
   693 lemma max_Suc1:
   694    "max (Suc n) m = (case m of 0 => Suc n | Suc m' => Suc(max n m'))"
   695 by (simp split: nat.split)
   696 
   697 lemma max_Suc2:
   698    "max m (Suc n) = (case m of 0 => Suc n | Suc m' => Suc(max m' n))"
   699 by (simp split: nat.split)
   700 
   701 
   702 subsubsection {* Monotonicity of Addition *}
   703 
   704 lemma Suc_pred [simp]: "n>0 ==> Suc (n - Suc 0) = n"
   705 by (simp add: diff_Suc split: nat.split)
   706 
   707 lemma Suc_diff_1 [simp]: "0 < n ==> Suc (n - 1) = n"
   708 unfolding One_nat_def by (rule Suc_pred)
   709 
   710 lemma nat_add_left_cancel_le [simp]: "(k + m \<le> k + n) = (m\<le>(n::nat))"
   711 by (induct k) simp_all
   712 
   713 lemma nat_add_left_cancel_less [simp]: "(k + m < k + n) = (m<(n::nat))"
   714 by (induct k) simp_all
   715 
   716 lemma add_gr_0 [iff]: "!!m::nat. (m + n > 0) = (m>0 | n>0)"
   717 by(auto dest:gr0_implies_Suc)
   718 
   719 text {* strict, in 1st argument *}
   720 lemma add_less_mono1: "i < j ==> i + k < j + (k::nat)"
   721 by (induct k) simp_all
   722 
   723 text {* strict, in both arguments *}
   724 lemma add_less_mono: "[|i < j; k < l|] ==> i + k < j + (l::nat)"
   725   apply (rule add_less_mono1 [THEN less_trans], assumption+)
   726   apply (induct j, simp_all)
   727   done
   728 
   729 text {* Deleted @{text less_natE}; use @{text "less_imp_Suc_add RS exE"} *}
   730 lemma less_imp_Suc_add: "m < n ==> (\<exists>k. n = Suc (m + k))"
   731   apply (induct n)
   732   apply (simp_all add: order_le_less)
   733   apply (blast elim!: less_SucE
   734                intro!: Nat.add_0_right [symmetric] add_Suc_right [symmetric])
   735   done
   736 
   737 text {* strict, in 1st argument; proof is by induction on @{text "k > 0"} *}
   738 lemma mult_less_mono2: "(i::nat) < j ==> 0<k ==> k * i < k * j"
   739 apply(auto simp: gr0_conv_Suc)
   740 apply (induct_tac m)
   741 apply (simp_all add: add_less_mono)
   742 done
   743 
   744 text{*The naturals form an ordered @{text comm_semiring_1_cancel}*}
   745 instance nat :: linordered_semidom
   746 proof
   747   fix i j k :: nat
   748   show "0 < (1::nat)" by simp
   749   show "i \<le> j ==> k + i \<le> k + j" by simp
   750   show "i < j ==> 0 < k ==> k * i < k * j" by (simp add: mult_less_mono2)
   751 qed
   752 
   753 instance nat :: no_zero_divisors
   754 proof
   755   fix a::nat and b::nat show "a ~= 0 \<Longrightarrow> b ~= 0 \<Longrightarrow> a * b ~= 0" by auto
   756 qed
   757 
   758 lemma nat_mult_1: "(1::nat) * n = n"
   759 by simp
   760 
   761 lemma nat_mult_1_right: "n * (1::nat) = n"
   762 by simp
   763 
   764 
   765 subsubsection {* Additional theorems about @{term "op \<le>"} *}
   766 
   767 text {* Complete induction, aka course-of-values induction *}
   768 
   769 instance nat :: wellorder proof
   770   fix P and n :: nat
   771   assume step: "\<And>n::nat. (\<And>m. m < n \<Longrightarrow> P m) \<Longrightarrow> P n"
   772   have "\<And>q. q \<le> n \<Longrightarrow> P q"
   773   proof (induct n)
   774     case (0 n)
   775     have "P 0" by (rule step) auto
   776     thus ?case using 0 by auto
   777   next
   778     case (Suc m n)
   779     then have "n \<le> m \<or> n = Suc m" by (simp add: le_Suc_eq)
   780     thus ?case
   781     proof
   782       assume "n \<le> m" thus "P n" by (rule Suc(1))
   783     next
   784       assume n: "n = Suc m"
   785       show "P n"
   786         by (rule step) (rule Suc(1), simp add: n le_simps)
   787     qed
   788   qed
   789   then show "P n" by auto
   790 qed
   791 
   792 lemma Least_Suc:
   793      "[| P n; ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))"
   794   apply (case_tac "n", auto)
   795   apply (frule LeastI)
   796   apply (drule_tac P = "%x. P (Suc x) " in LeastI)
   797   apply (subgoal_tac " (LEAST x. P x) \<le> Suc (LEAST x. P (Suc x))")
   798   apply (erule_tac [2] Least_le)
   799   apply (case_tac "LEAST x. P x", auto)
   800   apply (drule_tac P = "%x. P (Suc x) " in Least_le)
   801   apply (blast intro: order_antisym)
   802   done
   803 
   804 lemma Least_Suc2:
   805    "[|P n; Q m; ~P 0; !k. P (Suc k) = Q k|] ==> Least P = Suc (Least Q)"
   806   apply (erule (1) Least_Suc [THEN ssubst])
   807   apply simp
   808   done
   809 
   810 lemma ex_least_nat_le: "\<not>P(0) \<Longrightarrow> P(n::nat) \<Longrightarrow> \<exists>k\<le>n. (\<forall>i<k. \<not>P i) & P(k)"
   811   apply (cases n)
   812    apply blast
   813   apply (rule_tac x="LEAST k. P(k)" in exI)
   814   apply (blast intro: Least_le dest: not_less_Least intro: LeastI_ex)
   815   done
   816 
   817 lemma ex_least_nat_less: "\<not>P(0) \<Longrightarrow> P(n::nat) \<Longrightarrow> \<exists>k<n. (\<forall>i\<le>k. \<not>P i) & P(k+1)"
   818   unfolding One_nat_def
   819   apply (cases n)
   820    apply blast
   821   apply (frule (1) ex_least_nat_le)
   822   apply (erule exE)
   823   apply (case_tac k)
   824    apply simp
   825   apply (rename_tac k1)
   826   apply (rule_tac x=k1 in exI)
   827   apply (auto simp add: less_eq_Suc_le)
   828   done
   829 
   830 lemma nat_less_induct:
   831   assumes "!!n. \<forall>m::nat. m < n --> P m ==> P n" shows "P n"
   832   using assms less_induct by blast
   833 
   834 lemma measure_induct_rule [case_names less]:
   835   fixes f :: "'a \<Rightarrow> nat"
   836   assumes step: "\<And>x. (\<And>y. f y < f x \<Longrightarrow> P y) \<Longrightarrow> P x"
   837   shows "P a"
   838 by (induct m\<equiv>"f a" arbitrary: a rule: less_induct) (auto intro: step)
   839 
   840 text {* old style induction rules: *}
   841 lemma measure_induct:
   842   fixes f :: "'a \<Rightarrow> nat"
   843   shows "(\<And>x. \<forall>y. f y < f x \<longrightarrow> P y \<Longrightarrow> P x) \<Longrightarrow> P a"
   844   by (rule measure_induct_rule [of f P a]) iprover
   845 
   846 lemma full_nat_induct:
   847   assumes step: "(!!n. (ALL m. Suc m <= n --> P m) ==> P n)"
   848   shows "P n"
   849   by (rule less_induct) (auto intro: step simp:le_simps)
   850 
   851 text{*An induction rule for estabilishing binary relations*}
   852 lemma less_Suc_induct:
   853   assumes less:  "i < j"
   854      and  step:  "!!i. P i (Suc i)"
   855      and  trans: "!!i j k. i < j ==> j < k ==>  P i j ==> P j k ==> P i k"
   856   shows "P i j"
   857 proof -
   858   from less obtain k where j: "j = Suc (i + k)" by (auto dest: less_imp_Suc_add)
   859   have "P i (Suc (i + k))"
   860   proof (induct k)
   861     case 0
   862     show ?case by (simp add: step)
   863   next
   864     case (Suc k)
   865     have "0 + i < Suc k + i" by (rule add_less_mono1) simp
   866     hence "i < Suc (i + k)" by (simp add: add_commute)
   867     from trans[OF this lessI Suc step]
   868     show ?case by simp
   869   qed
   870   thus "P i j" by (simp add: j)
   871 qed
   872 
   873 text {* The method of infinite descent, frequently used in number theory.
   874 Provided by Roelof Oosterhuis.
   875 $P(n)$ is true for all $n\in\mathbb{N}$ if
   876 \begin{itemize}
   877   \item case ``0'': given $n=0$ prove $P(n)$,
   878   \item case ``smaller'': given $n>0$ and $\neg P(n)$ prove there exists
   879         a smaller integer $m$ such that $\neg P(m)$.
   880 \end{itemize} *}
   881 
   882 text{* A compact version without explicit base case: *}
   883 lemma infinite_descent:
   884   "\<lbrakk> !!n::nat. \<not> P n \<Longrightarrow>  \<exists>m<n. \<not>  P m \<rbrakk> \<Longrightarrow>  P n"
   885 by (induct n rule: less_induct, auto)
   886 
   887 lemma infinite_descent0[case_names 0 smaller]: 
   888   "\<lbrakk> P 0; !!n. n>0 \<Longrightarrow> \<not> P n \<Longrightarrow> (\<exists>m::nat. m < n \<and> \<not>P m) \<rbrakk> \<Longrightarrow> P n"
   889 by (rule infinite_descent) (case_tac "n>0", auto)
   890 
   891 text {*
   892 Infinite descent using a mapping to $\mathbb{N}$:
   893 $P(x)$ is true for all $x\in D$ if there exists a $V: D \to \mathbb{N}$ and
   894 \begin{itemize}
   895 \item case ``0'': given $V(x)=0$ prove $P(x)$,
   896 \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)$.
   897 \end{itemize}
   898 NB: the proof also shows how to use the previous lemma. *}
   899 
   900 corollary infinite_descent0_measure [case_names 0 smaller]:
   901   assumes A0: "!!x. V x = (0::nat) \<Longrightarrow> P x"
   902     and   A1: "!!x. V x > 0 \<Longrightarrow> \<not>P x \<Longrightarrow> (\<exists>y. V y < V x \<and> \<not>P y)"
   903   shows "P x"
   904 proof -
   905   obtain n where "n = V x" by auto
   906   moreover have "\<And>x. V x = n \<Longrightarrow> P x"
   907   proof (induct n rule: infinite_descent0)
   908     case 0 -- "i.e. $V(x) = 0$"
   909     with A0 show "P x" by auto
   910   next -- "now $n>0$ and $P(x)$ does not hold for some $x$ with $V(x)=n$"
   911     case (smaller n)
   912     then obtain x where vxn: "V x = n " and "V x > 0 \<and> \<not> P x" by auto
   913     with A1 obtain y where "V y < V x \<and> \<not> P y" by auto
   914     with vxn obtain m where "m = V y \<and> m<n \<and> \<not> P y" by auto
   915     then show ?case by auto
   916   qed
   917   ultimately show "P x" by auto
   918 qed
   919 
   920 text{* Again, without explicit base case: *}
   921 lemma infinite_descent_measure:
   922 assumes "!!x. \<not> P x \<Longrightarrow> \<exists>y. (V::'a\<Rightarrow>nat) y < V x \<and> \<not> P y" shows "P x"
   923 proof -
   924   from assms obtain n where "n = V x" by auto
   925   moreover have "!!x. V x = n \<Longrightarrow> P x"
   926   proof (induct n rule: infinite_descent, auto)
   927     fix x assume "\<not> P x"
   928     with assms show "\<exists>m < V x. \<exists>y. V y = m \<and> \<not> P y" by auto
   929   qed
   930   ultimately show "P x" by auto
   931 qed
   932 
   933 text {* A [clumsy] way of lifting @{text "<"}
   934   monotonicity to @{text "\<le>"} monotonicity *}
   935 lemma less_mono_imp_le_mono:
   936   "\<lbrakk> !!i j::nat. i < j \<Longrightarrow> f i < f j; i \<le> j \<rbrakk> \<Longrightarrow> f i \<le> ((f j)::nat)"
   937 by (simp add: order_le_less) (blast)
   938 
   939 
   940 text {* non-strict, in 1st argument *}
   941 lemma add_le_mono1: "i \<le> j ==> i + k \<le> j + (k::nat)"
   942 by (rule add_right_mono)
   943 
   944 text {* non-strict, in both arguments *}
   945 lemma add_le_mono: "[| i \<le> j;  k \<le> l |] ==> i + k \<le> j + (l::nat)"
   946 by (rule add_mono)
   947 
   948 lemma le_add2: "n \<le> ((m + n)::nat)"
   949 by (insert add_right_mono [of 0 m n], simp)
   950 
   951 lemma le_add1: "n \<le> ((n + m)::nat)"
   952 by (simp add: add_commute, rule le_add2)
   953 
   954 lemma less_add_Suc1: "i < Suc (i + m)"
   955 by (rule le_less_trans, rule le_add1, rule lessI)
   956 
   957 lemma less_add_Suc2: "i < Suc (m + i)"
   958 by (rule le_less_trans, rule le_add2, rule lessI)
   959 
   960 lemma less_iff_Suc_add: "(m < n) = (\<exists>k. n = Suc (m + k))"
   961 by (iprover intro!: less_add_Suc1 less_imp_Suc_add)
   962 
   963 lemma trans_le_add1: "(i::nat) \<le> j ==> i \<le> j + m"
   964 by (rule le_trans, assumption, rule le_add1)
   965 
   966 lemma trans_le_add2: "(i::nat) \<le> j ==> i \<le> m + j"
   967 by (rule le_trans, assumption, rule le_add2)
   968 
   969 lemma trans_less_add1: "(i::nat) < j ==> i < j + m"
   970 by (rule less_le_trans, assumption, rule le_add1)
   971 
   972 lemma trans_less_add2: "(i::nat) < j ==> i < m + j"
   973 by (rule less_le_trans, assumption, rule le_add2)
   974 
   975 lemma add_lessD1: "i + j < (k::nat) ==> i < k"
   976 apply (rule le_less_trans [of _ "i+j"])
   977 apply (simp_all add: le_add1)
   978 done
   979 
   980 lemma not_add_less1 [iff]: "~ (i + j < (i::nat))"
   981 apply (rule notI)
   982 apply (drule add_lessD1)
   983 apply (erule less_irrefl [THEN notE])
   984 done
   985 
   986 lemma not_add_less2 [iff]: "~ (j + i < (i::nat))"
   987 by (simp add: add_commute)
   988 
   989 lemma add_leD1: "m + k \<le> n ==> m \<le> (n::nat)"
   990 apply (rule order_trans [of _ "m+k"])
   991 apply (simp_all add: le_add1)
   992 done
   993 
   994 lemma add_leD2: "m + k \<le> n ==> k \<le> (n::nat)"
   995 apply (simp add: add_commute)
   996 apply (erule add_leD1)
   997 done
   998 
   999 lemma add_leE: "(m::nat) + k \<le> n ==> (m \<le> n ==> k \<le> n ==> R) ==> R"
  1000 by (blast dest: add_leD1 add_leD2)
  1001 
  1002 text {* needs @{text "!!k"} for @{text add_ac} to work *}
  1003 lemma less_add_eq_less: "!!k::nat. k < l ==> m + l = k + n ==> m < n"
  1004 by (force simp del: add_Suc_right
  1005     simp add: less_iff_Suc_add add_Suc_right [symmetric] add_ac)
  1006 
  1007 
  1008 subsubsection {* More results about difference *}
  1009 
  1010 text {* Addition is the inverse of subtraction:
  1011   if @{term "n \<le> m"} then @{term "n + (m - n) = m"}. *}
  1012 lemma add_diff_inverse: "~  m < n ==> n + (m - n) = (m::nat)"
  1013 by (induct m n rule: diff_induct) simp_all
  1014 
  1015 lemma le_add_diff_inverse [simp]: "n \<le> m ==> n + (m - n) = (m::nat)"
  1016 by (simp add: add_diff_inverse linorder_not_less)
  1017 
  1018 lemma le_add_diff_inverse2 [simp]: "n \<le> m ==> (m - n) + n = (m::nat)"
  1019 by (simp add: add_commute)
  1020 
  1021 lemma Suc_diff_le: "n \<le> m ==> Suc m - n = Suc (m - n)"
  1022 by (induct m n rule: diff_induct) simp_all
  1023 
  1024 lemma diff_less_Suc: "m - n < Suc m"
  1025 apply (induct m n rule: diff_induct)
  1026 apply (erule_tac [3] less_SucE)
  1027 apply (simp_all add: less_Suc_eq)
  1028 done
  1029 
  1030 lemma diff_le_self [simp]: "m - n \<le> (m::nat)"
  1031 by (induct m n rule: diff_induct) (simp_all add: le_SucI)
  1032 
  1033 lemma le_iff_add: "(m::nat) \<le> n = (\<exists>k. n = m + k)"
  1034   by (auto simp: le_add1 dest!: le_add_diff_inverse sym [of _ n])
  1035 
  1036 lemma less_imp_diff_less: "(j::nat) < k ==> j - n < k"
  1037 by (rule le_less_trans, rule diff_le_self)
  1038 
  1039 lemma diff_Suc_less [simp]: "0<n ==> n - Suc i < n"
  1040 by (cases n) (auto simp add: le_simps)
  1041 
  1042 lemma diff_add_assoc: "k \<le> (j::nat) ==> (i + j) - k = i + (j - k)"
  1043 by (induct j k rule: diff_induct) simp_all
  1044 
  1045 lemma diff_add_assoc2: "k \<le> (j::nat) ==> (j + i) - k = (j - k) + i"
  1046 by (simp add: add_commute diff_add_assoc)
  1047 
  1048 lemma le_imp_diff_is_add: "i \<le> (j::nat) ==> (j - i = k) = (j = k + i)"
  1049 by (auto simp add: diff_add_inverse2)
  1050 
  1051 lemma diff_is_0_eq [simp]: "((m::nat) - n = 0) = (m \<le> n)"
  1052 by (induct m n rule: diff_induct) simp_all
  1053 
  1054 lemma diff_is_0_eq' [simp]: "m \<le> n ==> (m::nat) - n = 0"
  1055 by (rule iffD2, rule diff_is_0_eq)
  1056 
  1057 lemma zero_less_diff [simp]: "(0 < n - (m::nat)) = (m < n)"
  1058 by (induct m n rule: diff_induct) simp_all
  1059 
  1060 lemma less_imp_add_positive:
  1061   assumes "i < j"
  1062   shows "\<exists>k::nat. 0 < k & i + k = j"
  1063 proof
  1064   from assms show "0 < j - i & i + (j - i) = j"
  1065     by (simp add: order_less_imp_le)
  1066 qed
  1067 
  1068 text {* a nice rewrite for bounded subtraction *}
  1069 lemma nat_minus_add_max:
  1070   fixes n m :: nat
  1071   shows "n - m + m = max n m"
  1072     by (simp add: max_def not_le order_less_imp_le)
  1073 
  1074 lemma nat_diff_split:
  1075   "P(a - b::nat) = ((a<b --> P 0) & (ALL d. a = b + d --> P d))"
  1076     -- {* elimination of @{text -} on @{text nat} *}
  1077 by (cases "a < b")
  1078   (auto simp add: diff_is_0_eq [THEN iffD2] diff_add_inverse
  1079     not_less le_less dest!: sym [of a] sym [of b] add_eq_self_zero)
  1080 
  1081 lemma nat_diff_split_asm:
  1082   "P(a - b::nat) = (~ (a < b & ~ P 0 | (EX d. a = b + d & ~ P d)))"
  1083     -- {* elimination of @{text -} on @{text nat} in assumptions *}
  1084 by (auto split: nat_diff_split)
  1085 
  1086 
  1087 subsubsection {* Monotonicity of Multiplication *}
  1088 
  1089 lemma mult_le_mono1: "i \<le> (j::nat) ==> i * k \<le> j * k"
  1090 by (simp add: mult_right_mono)
  1091 
  1092 lemma mult_le_mono2: "i \<le> (j::nat) ==> k * i \<le> k * j"
  1093 by (simp add: mult_left_mono)
  1094 
  1095 text {* @{text "\<le>"} monotonicity, BOTH arguments *}
  1096 lemma mult_le_mono: "i \<le> (j::nat) ==> k \<le> l ==> i * k \<le> j * l"
  1097 by (simp add: mult_mono)
  1098 
  1099 lemma mult_less_mono1: "(i::nat) < j ==> 0 < k ==> i * k < j * k"
  1100 by (simp add: mult_strict_right_mono)
  1101 
  1102 text{*Differs from the standard @{text zero_less_mult_iff} in that
  1103       there are no negative numbers.*}
  1104 lemma nat_0_less_mult_iff [simp]: "(0 < (m::nat) * n) = (0 < m & 0 < n)"
  1105   apply (induct m)
  1106    apply simp
  1107   apply (case_tac n)
  1108    apply simp_all
  1109   done
  1110 
  1111 lemma one_le_mult_iff [simp]: "(Suc 0 \<le> m * n) = (Suc 0 \<le> m & Suc 0 \<le> n)"
  1112   apply (induct m)
  1113    apply simp
  1114   apply (case_tac n)
  1115    apply simp_all
  1116   done
  1117 
  1118 lemma mult_less_cancel2 [simp]: "((m::nat) * k < n * k) = (0 < k & m < n)"
  1119   apply (safe intro!: mult_less_mono1)
  1120   apply (case_tac k, auto)
  1121   apply (simp del: le_0_eq add: linorder_not_le [symmetric])
  1122   apply (blast intro: mult_le_mono1)
  1123   done
  1124 
  1125 lemma mult_less_cancel1 [simp]: "(k * (m::nat) < k * n) = (0 < k & m < n)"
  1126 by (simp add: mult_commute [of k])
  1127 
  1128 lemma mult_le_cancel1 [simp]: "(k * (m::nat) \<le> k * n) = (0 < k --> m \<le> n)"
  1129 by (simp add: linorder_not_less [symmetric], auto)
  1130 
  1131 lemma mult_le_cancel2 [simp]: "((m::nat) * k \<le> n * k) = (0 < k --> m \<le> n)"
  1132 by (simp add: linorder_not_less [symmetric], auto)
  1133 
  1134 lemma Suc_mult_less_cancel1: "(Suc k * m < Suc k * n) = (m < n)"
  1135 by (subst mult_less_cancel1) simp
  1136 
  1137 lemma Suc_mult_le_cancel1: "(Suc k * m \<le> Suc k * n) = (m \<le> n)"
  1138 by (subst mult_le_cancel1) simp
  1139 
  1140 lemma le_square: "m \<le> m * (m::nat)"
  1141   by (cases m) (auto intro: le_add1)
  1142 
  1143 lemma le_cube: "(m::nat) \<le> m * (m * m)"
  1144   by (cases m) (auto intro: le_add1)
  1145 
  1146 text {* Lemma for @{text gcd} *}
  1147 lemma mult_eq_self_implies_10: "(m::nat) = m * n ==> n = 1 | m = 0"
  1148   apply (drule sym)
  1149   apply (rule disjCI)
  1150   apply (rule nat_less_cases, erule_tac [2] _)
  1151    apply (drule_tac [2] mult_less_mono2)
  1152     apply (auto)
  1153   done
  1154 
  1155 text {* the lattice order on @{typ nat} *}
  1156 
  1157 instantiation nat :: distrib_lattice
  1158 begin
  1159 
  1160 definition
  1161   "(inf \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat) = min"
  1162 
  1163 definition
  1164   "(sup \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat) = max"
  1165 
  1166 instance by intro_classes
  1167   (auto simp add: inf_nat_def sup_nat_def max_def not_le min_def
  1168     intro: order_less_imp_le antisym elim!: order_trans order_less_trans)
  1169 
  1170 end
  1171 
  1172 
  1173 subsection {* Natural operation of natural numbers on functions *}
  1174 
  1175 text {*
  1176   We use the same logical constant for the power operations on
  1177   functions and relations, in order to share the same syntax.
  1178 *}
  1179 
  1180 consts compow :: "nat \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
  1181 
  1182 abbreviation compower :: "('a \<Rightarrow> 'b) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'b" (infixr "^^" 80) where
  1183   "f ^^ n \<equiv> compow n f"
  1184 
  1185 notation (latex output)
  1186   compower ("(_\<^bsup>_\<^esup>)" [1000] 1000)
  1187 
  1188 notation (HTML output)
  1189   compower ("(_\<^bsup>_\<^esup>)" [1000] 1000)
  1190 
  1191 text {* @{text "f ^^ n = f o ... o f"}, the n-fold composition of @{text f} *}
  1192 
  1193 overloading
  1194   funpow == "compow :: nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a)"
  1195 begin
  1196 
  1197 primrec funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
  1198     "funpow 0 f = id"
  1199   | "funpow (Suc n) f = f o funpow n f"
  1200 
  1201 end
  1202 
  1203 text {* for code generation *}
  1204 
  1205 definition funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
  1206   funpow_code_def [code_post]: "funpow = compow"
  1207 
  1208 lemmas [code_unfold] = funpow_code_def [symmetric]
  1209 
  1210 lemma [code]:
  1211   "funpow 0 f = id"
  1212   "funpow (Suc n) f = f o funpow n f"
  1213   unfolding funpow_code_def by simp_all
  1214 
  1215 hide (open) const funpow
  1216 
  1217 lemma funpow_add:
  1218   "f ^^ (m + n) = f ^^ m \<circ> f ^^ n"
  1219   by (induct m) simp_all
  1220 
  1221 lemma funpow_swap1:
  1222   "f ((f ^^ n) x) = (f ^^ n) (f x)"
  1223 proof -
  1224   have "f ((f ^^ n) x) = (f ^^ (n + 1)) x" by simp
  1225   also have "\<dots>  = (f ^^ n o f ^^ 1) x" by (simp only: funpow_add)
  1226   also have "\<dots> = (f ^^ n) (f x)" by simp
  1227   finally show ?thesis .
  1228 qed
  1229 
  1230 
  1231 subsection {* Embedding of the Naturals into any
  1232   @{text semiring_1}: @{term of_nat} *}
  1233 
  1234 context semiring_1
  1235 begin
  1236 
  1237 primrec
  1238   of_nat :: "nat \<Rightarrow> 'a"
  1239 where
  1240   of_nat_0:     "of_nat 0 = 0"
  1241   | of_nat_Suc: "of_nat (Suc m) = 1 + of_nat m"
  1242 
  1243 lemma of_nat_1 [simp]: "of_nat 1 = 1"
  1244   unfolding One_nat_def by simp
  1245 
  1246 lemma of_nat_add [simp]: "of_nat (m + n) = of_nat m + of_nat n"
  1247   by (induct m) (simp_all add: add_ac)
  1248 
  1249 lemma of_nat_mult: "of_nat (m * n) = of_nat m * of_nat n"
  1250   by (induct m) (simp_all add: add_ac left_distrib)
  1251 
  1252 primrec of_nat_aux :: "('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a" where
  1253   "of_nat_aux inc 0 i = i"
  1254   | "of_nat_aux inc (Suc n) i = of_nat_aux inc n (inc i)" -- {* tail recursive *}
  1255 
  1256 lemma of_nat_code:
  1257   "of_nat n = of_nat_aux (\<lambda>i. i + 1) n 0"
  1258 proof (induct n)
  1259   case 0 then show ?case by simp
  1260 next
  1261   case (Suc n)
  1262   have "\<And>i. of_nat_aux (\<lambda>i. i + 1) n (i + 1) = of_nat_aux (\<lambda>i. i + 1) n i + 1"
  1263     by (induct n) simp_all
  1264   from this [of 0] have "of_nat_aux (\<lambda>i. i + 1) n 1 = of_nat_aux (\<lambda>i. i + 1) n 0 + 1"
  1265     by simp
  1266   with Suc show ?case by (simp add: add_commute)
  1267 qed
  1268 
  1269 end
  1270 
  1271 declare of_nat_code [code, code_unfold, code_inline del]
  1272 
  1273 text{*Class for unital semirings with characteristic zero.
  1274  Includes non-ordered rings like the complex numbers.*}
  1275 
  1276 class semiring_char_0 = semiring_1 +
  1277   assumes of_nat_eq_iff [simp]: "of_nat m = of_nat n \<longleftrightarrow> m = n"
  1278 begin
  1279 
  1280 text{*Special cases where either operand is zero*}
  1281 
  1282 lemma of_nat_0_eq_iff [simp, noatp]: "0 = of_nat n \<longleftrightarrow> 0 = n"
  1283   by (rule of_nat_eq_iff [of 0 n, unfolded of_nat_0])
  1284 
  1285 lemma of_nat_eq_0_iff [simp, noatp]: "of_nat m = 0 \<longleftrightarrow> m = 0"
  1286   by (rule of_nat_eq_iff [of m 0, unfolded of_nat_0])
  1287 
  1288 lemma inj_of_nat: "inj of_nat"
  1289   by (simp add: inj_on_def)
  1290 
  1291 end
  1292 
  1293 context linordered_semidom
  1294 begin
  1295 
  1296 lemma zero_le_imp_of_nat: "0 \<le> of_nat m"
  1297   apply (induct m, simp_all)
  1298   apply (erule order_trans)
  1299   apply (rule ord_le_eq_trans [OF _ add_commute])
  1300   apply (rule less_add_one [THEN less_imp_le])
  1301   done
  1302 
  1303 lemma less_imp_of_nat_less: "m < n \<Longrightarrow> of_nat m < of_nat n"
  1304   apply (induct m n rule: diff_induct, simp_all)
  1305   apply (insert add_less_le_mono [OF zero_less_one zero_le_imp_of_nat], force)
  1306   done
  1307 
  1308 lemma of_nat_less_imp_less: "of_nat m < of_nat n \<Longrightarrow> m < n"
  1309   apply (induct m n rule: diff_induct, simp_all)
  1310   apply (insert zero_le_imp_of_nat)
  1311   apply (force simp add: not_less [symmetric])
  1312   done
  1313 
  1314 lemma of_nat_less_iff [simp]: "of_nat m < of_nat n \<longleftrightarrow> m < n"
  1315   by (blast intro: of_nat_less_imp_less less_imp_of_nat_less)
  1316 
  1317 lemma of_nat_le_iff [simp]: "of_nat m \<le> of_nat n \<longleftrightarrow> m \<le> n"
  1318   by (simp add: not_less [symmetric] linorder_not_less [symmetric])
  1319 
  1320 text{*Every @{text linordered_semidom} has characteristic zero.*}
  1321 
  1322 subclass semiring_char_0
  1323   proof qed (simp add: eq_iff order_eq_iff)
  1324 
  1325 text{*Special cases where either operand is zero*}
  1326 
  1327 lemma of_nat_0_le_iff [simp]: "0 \<le> of_nat n"
  1328   by (rule of_nat_le_iff [of 0, simplified])
  1329 
  1330 lemma of_nat_le_0_iff [simp, noatp]: "of_nat m \<le> 0 \<longleftrightarrow> m = 0"
  1331   by (rule of_nat_le_iff [of _ 0, simplified])
  1332 
  1333 lemma of_nat_0_less_iff [simp]: "0 < of_nat n \<longleftrightarrow> 0 < n"
  1334   by (rule of_nat_less_iff [of 0, simplified])
  1335 
  1336 lemma of_nat_less_0_iff [simp]: "\<not> of_nat m < 0"
  1337   by (rule of_nat_less_iff [of _ 0, simplified])
  1338 
  1339 end
  1340 
  1341 context ring_1
  1342 begin
  1343 
  1344 lemma of_nat_diff: "n \<le> m \<Longrightarrow> of_nat (m - n) = of_nat m - of_nat n"
  1345 by (simp add: algebra_simps of_nat_add [symmetric])
  1346 
  1347 end
  1348 
  1349 context linordered_idom
  1350 begin
  1351 
  1352 lemma abs_of_nat [simp]: "\<bar>of_nat n\<bar> = of_nat n"
  1353   unfolding abs_if by auto
  1354 
  1355 end
  1356 
  1357 lemma of_nat_id [simp]: "of_nat n = n"
  1358   by (induct n) simp_all
  1359 
  1360 lemma of_nat_eq_id [simp]: "of_nat = id"
  1361   by (auto simp add: expand_fun_eq)
  1362 
  1363 
  1364 subsection {* The Set of Natural Numbers *}
  1365 
  1366 context semiring_1
  1367 begin
  1368 
  1369 definition
  1370   Nats  :: "'a set" where
  1371   [code del]: "Nats = range of_nat"
  1372 
  1373 notation (xsymbols)
  1374   Nats  ("\<nat>")
  1375 
  1376 lemma of_nat_in_Nats [simp]: "of_nat n \<in> \<nat>"
  1377   by (simp add: Nats_def)
  1378 
  1379 lemma Nats_0 [simp]: "0 \<in> \<nat>"
  1380 apply (simp add: Nats_def)
  1381 apply (rule range_eqI)
  1382 apply (rule of_nat_0 [symmetric])
  1383 done
  1384 
  1385 lemma Nats_1 [simp]: "1 \<in> \<nat>"
  1386 apply (simp add: Nats_def)
  1387 apply (rule range_eqI)
  1388 apply (rule of_nat_1 [symmetric])
  1389 done
  1390 
  1391 lemma Nats_add [simp]: "a \<in> \<nat> \<Longrightarrow> b \<in> \<nat> \<Longrightarrow> a + b \<in> \<nat>"
  1392 apply (auto simp add: Nats_def)
  1393 apply (rule range_eqI)
  1394 apply (rule of_nat_add [symmetric])
  1395 done
  1396 
  1397 lemma Nats_mult [simp]: "a \<in> \<nat> \<Longrightarrow> b \<in> \<nat> \<Longrightarrow> a * b \<in> \<nat>"
  1398 apply (auto simp add: Nats_def)
  1399 apply (rule range_eqI)
  1400 apply (rule of_nat_mult [symmetric])
  1401 done
  1402 
  1403 lemma Nats_cases [cases set: Nats]:
  1404   assumes "x \<in> \<nat>"
  1405   obtains (of_nat) n where "x = of_nat n"
  1406   unfolding Nats_def
  1407 proof -
  1408   from `x \<in> \<nat>` have "x \<in> range of_nat" unfolding Nats_def .
  1409   then obtain n where "x = of_nat n" ..
  1410   then show thesis ..
  1411 qed
  1412 
  1413 lemma Nats_induct [case_names of_nat, induct set: Nats]:
  1414   "x \<in> \<nat> \<Longrightarrow> (\<And>n. P (of_nat n)) \<Longrightarrow> P x"
  1415   by (rule Nats_cases) auto
  1416 
  1417 end
  1418 
  1419 
  1420 subsection {* Further Arithmetic Facts Concerning the Natural Numbers *}
  1421 
  1422 lemma subst_equals:
  1423   assumes 1: "t = s" and 2: "u = t"
  1424   shows "u = s"
  1425   using 2 1 by (rule trans)
  1426 
  1427 setup Arith_Data.setup
  1428 
  1429 use "Tools/nat_arith.ML"
  1430 declaration {* K Nat_Arith.setup *}
  1431 
  1432 use "Tools/lin_arith.ML"
  1433 setup {* Lin_Arith.global_setup *}
  1434 declaration {* K Lin_Arith.setup *}
  1435 
  1436 lemmas [arith_split] = nat_diff_split split_min split_max
  1437 
  1438 context order
  1439 begin
  1440 
  1441 lemma lift_Suc_mono_le:
  1442   assumes mono: "!!n. f n \<le> f(Suc n)" and "n\<le>n'"
  1443   shows "f n \<le> f n'"
  1444 proof (cases "n < n'")
  1445   case True
  1446   thus ?thesis
  1447     by (induct n n' rule: less_Suc_induct[consumes 1]) (auto intro: mono)
  1448 qed (insert `n \<le> n'`, auto) -- {*trivial for @{prop "n = n'"} *}
  1449 
  1450 lemma lift_Suc_mono_less:
  1451   assumes mono: "!!n. f n < f(Suc n)" and "n < n'"
  1452   shows "f n < f n'"
  1453 using `n < n'`
  1454 by (induct n n' rule: less_Suc_induct[consumes 1]) (auto intro: mono)
  1455 
  1456 lemma lift_Suc_mono_less_iff:
  1457   "(!!n. f n < f(Suc n)) \<Longrightarrow> f(n) < f(m) \<longleftrightarrow> n<m"
  1458 by(blast intro: less_asym' lift_Suc_mono_less[of f]
  1459          dest: linorder_not_less[THEN iffD1] le_eq_less_or_eq[THEN iffD1])
  1460 
  1461 end
  1462 
  1463 lemma mono_iff_le_Suc: "mono f = (\<forall>n. f n \<le> f (Suc n))"
  1464 unfolding mono_def
  1465 by (auto intro:lift_Suc_mono_le[of f])
  1466 
  1467 lemma mono_nat_linear_lb:
  1468   "(!!m n::nat. m<n \<Longrightarrow> f m < f n) \<Longrightarrow> f(m)+k \<le> f(m+k)"
  1469 apply(induct_tac k)
  1470  apply simp
  1471 apply(erule_tac x="m+n" in meta_allE)
  1472 apply(erule_tac x="Suc(m+n)" in meta_allE)
  1473 apply simp
  1474 done
  1475 
  1476 
  1477 text{*Subtraction laws, mostly by Clemens Ballarin*}
  1478 
  1479 lemma diff_less_mono: "[| a < (b::nat); c \<le> a |] ==> a-c < b-c"
  1480 by arith
  1481 
  1482 lemma less_diff_conv: "(i < j-k) = (i+k < (j::nat))"
  1483 by arith
  1484 
  1485 lemma le_diff_conv: "(j-k \<le> (i::nat)) = (j \<le> i+k)"
  1486 by arith
  1487 
  1488 lemma le_diff_conv2: "k \<le> j ==> (i \<le> j-k) = (i+k \<le> (j::nat))"
  1489 by arith
  1490 
  1491 lemma diff_diff_cancel [simp]: "i \<le> (n::nat) ==> n - (n - i) = i"
  1492 by arith
  1493 
  1494 lemma le_add_diff: "k \<le> (n::nat) ==> m \<le> n + m - k"
  1495 by arith
  1496 
  1497 (*Replaces the previous diff_less and le_diff_less, which had the stronger
  1498   second premise n\<le>m*)
  1499 lemma diff_less[simp]: "!!m::nat. [| 0<n; 0<m |] ==> m - n < m"
  1500 by arith
  1501 
  1502 text {* Simplification of relational expressions involving subtraction *}
  1503 
  1504 lemma diff_diff_eq: "[| k \<le> m;  k \<le> (n::nat) |] ==> ((m-k) - (n-k)) = (m-n)"
  1505 by (simp split add: nat_diff_split)
  1506 
  1507 hide (open) fact diff_diff_eq
  1508 
  1509 lemma eq_diff_iff: "[| k \<le> m;  k \<le> (n::nat) |] ==> (m-k = n-k) = (m=n)"
  1510 by (auto split add: nat_diff_split)
  1511 
  1512 lemma less_diff_iff: "[| k \<le> m;  k \<le> (n::nat) |] ==> (m-k < n-k) = (m<n)"
  1513 by (auto split add: nat_diff_split)
  1514 
  1515 lemma le_diff_iff: "[| k \<le> m;  k \<le> (n::nat) |] ==> (m-k \<le> n-k) = (m\<le>n)"
  1516 by (auto split add: nat_diff_split)
  1517 
  1518 text{*(Anti)Monotonicity of subtraction -- by Stephan Merz*}
  1519 
  1520 (* Monotonicity of subtraction in first argument *)
  1521 lemma diff_le_mono: "m \<le> (n::nat) ==> (m-l) \<le> (n-l)"
  1522 by (simp split add: nat_diff_split)
  1523 
  1524 lemma diff_le_mono2: "m \<le> (n::nat) ==> (l-n) \<le> (l-m)"
  1525 by (simp split add: nat_diff_split)
  1526 
  1527 lemma diff_less_mono2: "[| m < (n::nat); m<l |] ==> (l-n) < (l-m)"
  1528 by (simp split add: nat_diff_split)
  1529 
  1530 lemma diffs0_imp_equal: "!!m::nat. [| m-n = 0; n-m = 0 |] ==>  m=n"
  1531 by (simp split add: nat_diff_split)
  1532 
  1533 lemma min_diff: "min (m - (i::nat)) (n - i) = min m n - i"
  1534 by auto
  1535 
  1536 lemma inj_on_diff_nat: 
  1537   assumes k_le_n: "\<forall>n \<in> N. k \<le> (n::nat)"
  1538   shows "inj_on (\<lambda>n. n - k) N"
  1539 proof (rule inj_onI)
  1540   fix x y
  1541   assume a: "x \<in> N" "y \<in> N" "x - k = y - k"
  1542   with k_le_n have "x - k + k = y - k + k" by auto
  1543   with a k_le_n show "x = y" by auto
  1544 qed
  1545 
  1546 text{*Rewriting to pull differences out*}
  1547 
  1548 lemma diff_diff_right [simp]: "k\<le>j --> i - (j - k) = i + (k::nat) - j"
  1549 by arith
  1550 
  1551 lemma diff_Suc_diff_eq1 [simp]: "k \<le> j ==> m - Suc (j - k) = m + k - Suc j"
  1552 by arith
  1553 
  1554 lemma diff_Suc_diff_eq2 [simp]: "k \<le> j ==> Suc (j - k) - m = Suc j - (k + m)"
  1555 by arith
  1556 
  1557 text{*Lemmas for ex/Factorization*}
  1558 
  1559 lemma one_less_mult: "[| Suc 0 < n; Suc 0 < m |] ==> Suc 0 < m*n"
  1560 by (cases m) auto
  1561 
  1562 lemma n_less_m_mult_n: "[| Suc 0 < n; Suc 0 < m |] ==> n<m*n"
  1563 by (cases m) auto
  1564 
  1565 lemma n_less_n_mult_m: "[| Suc 0 < n; Suc 0 < m |] ==> n<n*m"
  1566 by (cases m) auto
  1567 
  1568 text {* Specialized induction principles that work "backwards": *}
  1569 
  1570 lemma inc_induct[consumes 1, case_names base step]:
  1571   assumes less: "i <= j"
  1572   assumes base: "P j"
  1573   assumes step: "!!i. [| i < j; P (Suc i) |] ==> P i"
  1574   shows "P i"
  1575   using less
  1576 proof (induct d=="j - i" arbitrary: i)
  1577   case (0 i)
  1578   hence "i = j" by simp
  1579   with base show ?case by simp
  1580 next
  1581   case (Suc d i)
  1582   hence "i < j" "P (Suc i)"
  1583     by simp_all
  1584   thus "P i" by (rule step)
  1585 qed
  1586 
  1587 lemma strict_inc_induct[consumes 1, case_names base step]:
  1588   assumes less: "i < j"
  1589   assumes base: "!!i. j = Suc i ==> P i"
  1590   assumes step: "!!i. [| i < j; P (Suc i) |] ==> P i"
  1591   shows "P i"
  1592   using less
  1593 proof (induct d=="j - i - 1" arbitrary: i)
  1594   case (0 i)
  1595   with `i < j` have "j = Suc i" by simp
  1596   with base show ?case by simp
  1597 next
  1598   case (Suc d i)
  1599   hence "i < j" "P (Suc i)"
  1600     by simp_all
  1601   thus "P i" by (rule step)
  1602 qed
  1603 
  1604 lemma zero_induct_lemma: "P k ==> (!!n. P (Suc n) ==> P n) ==> P (k - i)"
  1605   using inc_induct[of "k - i" k P, simplified] by blast
  1606 
  1607 lemma zero_induct: "P k ==> (!!n. P (Suc n) ==> P n) ==> P 0"
  1608   using inc_induct[of 0 k P] by blast
  1609 
  1610 (*The others are
  1611       i - j - k = i - (j + k),
  1612       k \<le> j ==> j - k + i = j + i - k,
  1613       k \<le> j ==> i + (j - k) = i + j - k *)
  1614 lemmas add_diff_assoc = diff_add_assoc [symmetric]
  1615 lemmas add_diff_assoc2 = diff_add_assoc2[symmetric]
  1616 declare diff_diff_left [simp]  add_diff_assoc [simp] add_diff_assoc2[simp]
  1617 
  1618 text{*At present we prove no analogue of @{text not_less_Least} or @{text
  1619 Least_Suc}, since there appears to be no need.*}
  1620 
  1621 
  1622 subsection {* The divides relation on @{typ nat} *}
  1623 
  1624 lemma dvd_1_left [iff]: "Suc 0 dvd k"
  1625 unfolding dvd_def by simp
  1626 
  1627 lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
  1628 by (simp add: dvd_def)
  1629 
  1630 lemma nat_dvd_1_iff_1 [simp]: "m dvd (1::nat) \<longleftrightarrow> m = 1"
  1631 by (simp add: dvd_def)
  1632 
  1633 lemma dvd_antisym: "[| m dvd n; n dvd m |] ==> m = (n::nat)"
  1634   unfolding dvd_def
  1635   by (force dest: mult_eq_self_implies_10 simp add: mult_assoc)
  1636 
  1637 text {* @{term "op dvd"} is a partial order *}
  1638 
  1639 interpretation dvd: order "op dvd" "\<lambda>n m \<Colon> nat. n dvd m \<and> \<not> m dvd n"
  1640   proof qed (auto intro: dvd_refl dvd_trans dvd_antisym)
  1641 
  1642 lemma dvd_diff_nat[simp]: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)"
  1643 unfolding dvd_def
  1644 by (blast intro: diff_mult_distrib2 [symmetric])
  1645 
  1646 lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)"
  1647   apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
  1648   apply (blast intro: dvd_add)
  1649   done
  1650 
  1651 lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)"
  1652 by (drule_tac m = m in dvd_diff_nat, auto)
  1653 
  1654 lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))"
  1655   apply (rule iffI)
  1656    apply (erule_tac [2] dvd_add)
  1657    apply (rule_tac [2] dvd_refl)
  1658   apply (subgoal_tac "n = (n+k) -k")
  1659    prefer 2 apply simp
  1660   apply (erule ssubst)
  1661   apply (erule dvd_diff_nat)
  1662   apply (rule dvd_refl)
  1663   done
  1664 
  1665 lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n"
  1666   unfolding dvd_def
  1667   apply (erule exE)
  1668   apply (simp add: mult_ac)
  1669   done
  1670 
  1671 lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))"
  1672   apply auto
  1673    apply (subgoal_tac "m*n dvd m*1")
  1674    apply (drule dvd_mult_cancel, auto)
  1675   done
  1676 
  1677 lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))"
  1678   apply (subst mult_commute)
  1679   apply (erule dvd_mult_cancel1)
  1680   done
  1681 
  1682 lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)"
  1683 by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
  1684 
  1685 lemma nat_dvd_not_less:
  1686   fixes m n :: nat
  1687   shows "0 < m \<Longrightarrow> m < n \<Longrightarrow> \<not> n dvd m"
  1688 by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
  1689 
  1690 
  1691 subsection {* size of a datatype value *}
  1692 
  1693 class size =
  1694   fixes size :: "'a \<Rightarrow> nat" -- {* see further theory @{text Wellfounded} *}
  1695 
  1696 
  1697 subsection {* code module namespace *}
  1698 
  1699 code_modulename SML
  1700   Nat Arith
  1701 
  1702 code_modulename OCaml
  1703   Nat Arith
  1704 
  1705 code_modulename Haskell
  1706   Nat Arith
  1707 
  1708 end