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