src/HOL/Nat.thy
author nipkow
Wed Aug 09 12:01:16 2017 +0200 (23 months ago)
changeset 66386 962c12353c67
parent 66295 1ec601d9c829
child 66810 cc2b490f9dc4
permissions -rw-r--r--
added lemmas
     1 (*  Title:      HOL/Nat.thy
     2     Author:     Tobias Nipkow
     3     Author:     Lawrence C Paulson
     4     Author:     Markus Wenzel
     5 *)
     6 
     7 section \<open>Natural numbers\<close>
     8 
     9 theory Nat
    10 imports Inductive Typedef Fun Rings
    11 begin
    12 
    13 named_theorems arith "arith facts -- only ground formulas"
    14 ML_file "Tools/arith_data.ML"
    15 
    16 
    17 subsection \<open>Type \<open>ind\<close>\<close>
    18 
    19 typedecl ind
    20 
    21 axiomatization Zero_Rep :: ind and Suc_Rep :: "ind \<Rightarrow> ind"
    22   \<comment> \<open>The axiom of infinity in 2 parts:\<close>
    23   where Suc_Rep_inject: "Suc_Rep x = Suc_Rep y \<Longrightarrow> x = y"
    24     and Suc_Rep_not_Zero_Rep: "Suc_Rep x \<noteq> Zero_Rep"
    25 
    26 
    27 subsection \<open>Type nat\<close>
    28 
    29 text \<open>Type definition\<close>
    30 
    31 inductive Nat :: "ind \<Rightarrow> bool"
    32   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: "Nat (Rep_Nat n)"
    41   using Rep_Nat by simp
    42 
    43 lemma Nat_Abs_Nat_inverse: "Nat n \<Longrightarrow> Rep_Nat (Abs_Nat n) = n"
    44   using Abs_Nat_inverse by simp
    45 
    46 lemma Nat_Abs_Nat_inject: "Nat n \<Longrightarrow> Nat m \<Longrightarrow> Abs_Nat n = Abs_Nat m \<longleftrightarrow> n = m"
    47   using Abs_Nat_inject by simp
    48 
    49 instantiation nat :: zero
    50 begin
    51 
    52 definition Zero_nat_def: "0 = Abs_Nat Zero_Rep"
    53 
    54 instance ..
    55 
    56 end
    57 
    58 definition Suc :: "nat \<Rightarrow> nat"
    59   where "Suc n = Abs_Nat (Suc_Rep (Rep_Nat n))"
    60 
    61 lemma Suc_not_Zero: "Suc m \<noteq> 0"
    62   by (simp add: Zero_nat_def Suc_def Suc_RepI Zero_RepI
    63       Nat_Abs_Nat_inject Suc_Rep_not_Zero_Rep Nat_Rep_Nat)
    64 
    65 lemma Zero_not_Suc: "0 \<noteq> Suc m"
    66   by (rule not_sym) (rule Suc_not_Zero)
    67 
    68 lemma Suc_Rep_inject': "Suc_Rep x = Suc_Rep y \<longleftrightarrow> x = y"
    69   by (rule iffI, rule Suc_Rep_inject) simp_all
    70 
    71 lemma nat_induct0:
    72   assumes "P 0"
    73     and "\<And>n. P n \<Longrightarrow> P (Suc n)"
    74   shows "P n"
    75   using assms
    76   apply (unfold Zero_nat_def Suc_def)
    77   apply (rule Rep_Nat_inverse [THEN subst]) \<comment> \<open>types force good instantiation\<close>
    78   apply (erule Nat_Rep_Nat [THEN Nat.induct])
    79   apply (iprover elim: Nat_Abs_Nat_inverse [THEN subst])
    80   done
    81 
    82 free_constructors case_nat for "0 :: nat" | Suc pred
    83   where "pred (0 :: nat) = (0 :: nat)"
    84     apply atomize_elim
    85     apply (rename_tac n, induct_tac n rule: nat_induct0, auto)
    86    apply (simp add: Suc_def Nat_Abs_Nat_inject Nat_Rep_Nat Suc_RepI Suc_Rep_inject' Rep_Nat_inject)
    87   apply (simp only: Suc_not_Zero)
    88   done
    89 
    90 \<comment> \<open>Avoid name clashes by prefixing the output of \<open>old_rep_datatype\<close> with \<open>old\<close>.\<close>
    91 setup \<open>Sign.mandatory_path "old"\<close>
    92 
    93 old_rep_datatype "0 :: nat" Suc
    94     apply (erule nat_induct0)
    95     apply assumption
    96    apply (rule nat.inject)
    97   apply (rule nat.distinct(1))
    98   done
    99 
   100 setup \<open>Sign.parent_path\<close>
   101 
   102 \<comment> \<open>But erase the prefix for properties that are not generated by \<open>free_constructors\<close>.\<close>
   103 setup \<open>Sign.mandatory_path "nat"\<close>
   104 
   105 declare old.nat.inject[iff del]
   106   and old.nat.distinct(1)[simp del, induct_simp del]
   107 
   108 lemmas induct = old.nat.induct
   109 lemmas inducts = old.nat.inducts
   110 lemmas rec = old.nat.rec
   111 lemmas simps = nat.inject nat.distinct nat.case nat.rec
   112 
   113 setup \<open>Sign.parent_path\<close>
   114 
   115 abbreviation rec_nat :: "'a \<Rightarrow> (nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a"
   116   where "rec_nat \<equiv> old.rec_nat"
   117 
   118 declare nat.sel[code del]
   119 
   120 hide_const (open) Nat.pred \<comment> \<open>hide everything related to the selector\<close>
   121 hide_fact
   122   nat.case_eq_if
   123   nat.collapse
   124   nat.expand
   125   nat.sel
   126   nat.exhaust_sel
   127   nat.split_sel
   128   nat.split_sel_asm
   129 
   130 lemma nat_exhaust [case_names 0 Suc, cases type: nat]:
   131   "(y = 0 \<Longrightarrow> P) \<Longrightarrow> (\<And>nat. y = Suc nat \<Longrightarrow> P) \<Longrightarrow> P"
   132   \<comment> \<open>for backward compatibility -- names of variables differ\<close>
   133   by (rule old.nat.exhaust)
   134 
   135 lemma nat_induct [case_names 0 Suc, induct type: nat]:
   136   fixes n
   137   assumes "P 0" and "\<And>n. P n \<Longrightarrow> P (Suc n)"
   138   shows "P n"
   139   \<comment> \<open>for backward compatibility -- names of variables differ\<close>
   140   using assms by (rule nat.induct)
   141 
   142 hide_fact
   143   nat_exhaust
   144   nat_induct0
   145 
   146 ML \<open>
   147 val nat_basic_lfp_sugar =
   148   let
   149     val ctr_sugar = the (Ctr_Sugar.ctr_sugar_of_global @{theory} @{type_name nat});
   150     val recx = Logic.varify_types_global @{term rec_nat};
   151     val C = body_type (fastype_of recx);
   152   in
   153     {T = HOLogic.natT, fp_res_index = 0, C = C, fun_arg_Tsss = [[], [[HOLogic.natT, C]]],
   154      ctr_sugar = ctr_sugar, recx = recx, rec_thms = @{thms nat.rec}}
   155   end;
   156 \<close>
   157 
   158 setup \<open>
   159 let
   160   fun basic_lfp_sugars_of _ [@{typ nat}] _ _ ctxt =
   161       ([], [0], [nat_basic_lfp_sugar], [], [], [], TrueI (*dummy*), [], false, ctxt)
   162     | basic_lfp_sugars_of bs arg_Ts callers callssss ctxt =
   163       BNF_LFP_Rec_Sugar.default_basic_lfp_sugars_of bs arg_Ts callers callssss ctxt;
   164 in
   165   BNF_LFP_Rec_Sugar.register_lfp_rec_extension
   166     {nested_simps = [], special_endgame_tac = K (K (K (K no_tac))), is_new_datatype = K (K true),
   167      basic_lfp_sugars_of = basic_lfp_sugars_of, rewrite_nested_rec_call = NONE}
   168 end
   169 \<close>
   170 
   171 text \<open>Injectiveness and distinctness lemmas\<close>
   172 
   173 lemma (in semidom_divide) inj_times:
   174   "inj (times a)" if "a \<noteq> 0"
   175 proof (rule injI)
   176   fix b c
   177   assume "a * b = a * c"
   178   then have "a * b div a = a * c div a"
   179     by (simp only:)
   180   with that show "b = c"
   181     by simp
   182 qed
   183 
   184 lemma (in cancel_ab_semigroup_add) inj_plus:
   185   "inj (plus a)"
   186 proof (rule injI)
   187   fix b c
   188   assume "a + b = a + c"
   189   then have "a + b - a = a + c - a"
   190     by (simp only:)
   191   then show "b = c"
   192     by simp
   193 qed
   194 
   195 lemma inj_Suc[simp]: "inj_on Suc N"
   196   by (simp add: inj_on_def)
   197 
   198 lemma Suc_neq_Zero: "Suc m = 0 \<Longrightarrow> R"
   199   by (rule notE) (rule Suc_not_Zero)
   200 
   201 lemma Zero_neq_Suc: "0 = Suc m \<Longrightarrow> R"
   202   by (rule Suc_neq_Zero) (erule sym)
   203 
   204 lemma Suc_inject: "Suc x = Suc y \<Longrightarrow> x = y"
   205   by (rule inj_Suc [THEN injD])
   206 
   207 lemma n_not_Suc_n: "n \<noteq> Suc n"
   208   by (induct n) simp_all
   209 
   210 lemma Suc_n_not_n: "Suc n \<noteq> n"
   211   by (rule not_sym) (rule n_not_Suc_n)
   212 
   213 text \<open>A special form of induction for reasoning about @{term "m < n"} and @{term "m - n"}.\<close>
   214 lemma diff_induct:
   215   assumes "\<And>x. P x 0"
   216     and "\<And>y. P 0 (Suc y)"
   217     and "\<And>x y. P x y \<Longrightarrow> P (Suc x) (Suc y)"
   218   shows "P m n"
   219 proof (induct n arbitrary: m)
   220   case 0
   221   show ?case by (rule assms(1))
   222 next
   223   case (Suc n)
   224   show ?case
   225   proof (induct m)
   226     case 0
   227     show ?case by (rule assms(2))
   228   next
   229     case (Suc m)
   230     from \<open>P m n\<close> show ?case by (rule assms(3))
   231   qed
   232 qed
   233 
   234 
   235 subsection \<open>Arithmetic operators\<close>
   236 
   237 instantiation nat :: comm_monoid_diff
   238 begin
   239 
   240 primrec plus_nat
   241   where
   242     add_0: "0 + n = (n::nat)"
   243   | add_Suc: "Suc m + n = Suc (m + n)"
   244 
   245 lemma add_0_right [simp]: "m + 0 = m"
   246   for m :: nat
   247   by (induct m) simp_all
   248 
   249 lemma add_Suc_right [simp]: "m + Suc n = Suc (m + n)"
   250   by (induct m) simp_all
   251 
   252 declare add_0 [code]
   253 
   254 lemma add_Suc_shift [code]: "Suc m + n = m + Suc n"
   255   by simp
   256 
   257 primrec minus_nat
   258   where
   259     diff_0 [code]: "m - 0 = (m::nat)"
   260   | diff_Suc: "m - Suc n = (case m - n of 0 \<Rightarrow> 0 | Suc k \<Rightarrow> k)"
   261 
   262 declare diff_Suc [simp del]
   263 
   264 lemma diff_0_eq_0 [simp, code]: "0 - n = 0"
   265   for n :: nat
   266   by (induct n) (simp_all add: diff_Suc)
   267 
   268 lemma diff_Suc_Suc [simp, code]: "Suc m - Suc n = m - n"
   269   by (induct n) (simp_all add: diff_Suc)
   270 
   271 instance
   272 proof
   273   fix n m q :: nat
   274   show "(n + m) + q = n + (m + q)" by (induct n) simp_all
   275   show "n + m = m + n" by (induct n) simp_all
   276   show "m + n - m = n" by (induct m) simp_all
   277   show "n - m - q = n - (m + q)" by (induct q) (simp_all add: diff_Suc)
   278   show "0 + n = n" by simp
   279   show "0 - n = 0" by simp
   280 qed
   281 
   282 end
   283 
   284 hide_fact (open) add_0 add_0_right diff_0
   285 
   286 instantiation nat :: comm_semiring_1_cancel
   287 begin
   288 
   289 definition One_nat_def [simp]: "1 = Suc 0"
   290 
   291 primrec times_nat
   292   where
   293     mult_0: "0 * n = (0::nat)"
   294   | mult_Suc: "Suc m * n = n + (m * n)"
   295 
   296 lemma mult_0_right [simp]: "m * 0 = 0"
   297   for m :: nat
   298   by (induct m) simp_all
   299 
   300 lemma mult_Suc_right [simp]: "m * Suc n = m + (m * n)"
   301   by (induct m) (simp_all add: add.left_commute)
   302 
   303 lemma add_mult_distrib: "(m + n) * k = (m * k) + (n * k)"
   304   for m n k :: nat
   305   by (induct m) (simp_all add: add.assoc)
   306 
   307 instance
   308 proof
   309   fix k n m q :: nat
   310   show "0 \<noteq> (1::nat)"
   311     by simp
   312   show "1 * n = n"
   313     by simp
   314   show "n * m = m * n"
   315     by (induct n) simp_all
   316   show "(n * m) * q = n * (m * q)"
   317     by (induct n) (simp_all add: add_mult_distrib)
   318   show "(n + m) * q = n * q + m * q"
   319     by (rule add_mult_distrib)
   320   show "k * (m - n) = (k * m) - (k * n)"
   321     by (induct m n rule: diff_induct) simp_all
   322 qed
   323 
   324 end
   325 
   326 
   327 subsubsection \<open>Addition\<close>
   328 
   329 text \<open>Reasoning about \<open>m + 0 = 0\<close>, etc.\<close>
   330 
   331 lemma add_is_0 [iff]: "m + n = 0 \<longleftrightarrow> m = 0 \<and> n = 0"
   332   for m n :: nat
   333   by (cases m) simp_all
   334 
   335 lemma add_is_1: "m + n = Suc 0 \<longleftrightarrow> m = Suc 0 \<and> n = 0 | m = 0 \<and> n = Suc 0"
   336   by (cases m) simp_all
   337 
   338 lemma one_is_add: "Suc 0 = m + n \<longleftrightarrow> m = Suc 0 \<and> n = 0 | m = 0 \<and> n = Suc 0"
   339   by (rule trans, rule eq_commute, rule add_is_1)
   340 
   341 lemma add_eq_self_zero: "m + n = m \<Longrightarrow> n = 0"
   342   for m n :: nat
   343   by (induct m) simp_all
   344 
   345 lemma inj_on_add_nat [simp]: "inj_on (\<lambda>n. n + k) N"
   346   for k :: nat
   347 proof (induct k)
   348   case 0
   349   then show ?case by simp
   350 next
   351   case (Suc k)
   352   show ?case
   353     using comp_inj_on [OF Suc inj_Suc] by (simp add: o_def)
   354 qed
   355 
   356 lemma Suc_eq_plus1: "Suc n = n + 1"
   357   by simp
   358 
   359 lemma Suc_eq_plus1_left: "Suc n = 1 + n"
   360   by simp
   361 
   362 
   363 subsubsection \<open>Difference\<close>
   364 
   365 lemma Suc_diff_diff [simp]: "(Suc m - n) - Suc k = m - n - k"
   366   by (simp add: diff_diff_add)
   367 
   368 lemma diff_Suc_1 [simp]: "Suc n - 1 = n"
   369   by simp
   370 
   371 
   372 subsubsection \<open>Multiplication\<close>
   373 
   374 lemma mult_is_0 [simp]: "m * n = 0 \<longleftrightarrow> m = 0 \<or> n = 0" for m n :: nat
   375   by (induct m) auto
   376 
   377 lemma mult_eq_1_iff [simp]: "m * n = Suc 0 \<longleftrightarrow> m = Suc 0 \<and> n = Suc 0"
   378 proof (induct m)
   379   case 0
   380   then show ?case by simp
   381 next
   382   case (Suc m)
   383   then show ?case by (induct n) auto
   384 qed
   385 
   386 lemma one_eq_mult_iff [simp]: "Suc 0 = m * n \<longleftrightarrow> m = Suc 0 \<and> n = Suc 0"
   387   apply (rule trans)
   388    apply (rule_tac [2] mult_eq_1_iff)
   389   apply fastforce
   390   done
   391 
   392 lemma nat_mult_eq_1_iff [simp]: "m * n = 1 \<longleftrightarrow> m = 1 \<and> n = 1"
   393   for m n :: nat
   394   unfolding One_nat_def by (rule mult_eq_1_iff)
   395 
   396 lemma nat_1_eq_mult_iff [simp]: "1 = m * n \<longleftrightarrow> m = 1 \<and> n = 1"
   397   for m n :: nat
   398   unfolding One_nat_def by (rule one_eq_mult_iff)
   399 
   400 lemma mult_cancel1 [simp]: "k * m = k * n \<longleftrightarrow> m = n \<or> k = 0"
   401   for k m n :: nat
   402 proof -
   403   have "k \<noteq> 0 \<Longrightarrow> k * m = k * n \<Longrightarrow> m = n"
   404   proof (induct n arbitrary: m)
   405     case 0
   406     then show "m = 0" by simp
   407   next
   408     case (Suc n)
   409     then show "m = Suc n"
   410       by (cases m) (simp_all add: eq_commute [of 0])
   411   qed
   412   then show ?thesis by auto
   413 qed
   414 
   415 lemma mult_cancel2 [simp]: "m * k = n * k \<longleftrightarrow> m = n \<or> k = 0"
   416   for k m n :: nat
   417   by (simp add: mult.commute)
   418 
   419 lemma Suc_mult_cancel1: "Suc k * m = Suc k * n \<longleftrightarrow> m = n"
   420   by (subst mult_cancel1) simp
   421 
   422 
   423 subsection \<open>Orders on @{typ nat}\<close>
   424 
   425 subsubsection \<open>Operation definition\<close>
   426 
   427 instantiation nat :: linorder
   428 begin
   429 
   430 primrec less_eq_nat
   431   where
   432     "(0::nat) \<le> n \<longleftrightarrow> True"
   433   | "Suc m \<le> n \<longleftrightarrow> (case n of 0 \<Rightarrow> False | Suc n \<Rightarrow> m \<le> n)"
   434 
   435 declare less_eq_nat.simps [simp del]
   436 
   437 lemma le0 [iff]: "0 \<le> n" for
   438   n :: nat
   439   by (simp add: less_eq_nat.simps)
   440 
   441 lemma [code]: "0 \<le> n \<longleftrightarrow> True"
   442   for n :: nat
   443   by simp
   444 
   445 definition less_nat
   446   where less_eq_Suc_le: "n < m \<longleftrightarrow> Suc n \<le> m"
   447 
   448 lemma Suc_le_mono [iff]: "Suc n \<le> Suc m \<longleftrightarrow> n \<le> m"
   449   by (simp add: less_eq_nat.simps(2))
   450 
   451 lemma Suc_le_eq [code]: "Suc m \<le> n \<longleftrightarrow> m < n"
   452   unfolding less_eq_Suc_le ..
   453 
   454 lemma le_0_eq [iff]: "n \<le> 0 \<longleftrightarrow> n = 0"
   455   for n :: nat
   456   by (induct n) (simp_all add: less_eq_nat.simps(2))
   457 
   458 lemma not_less0 [iff]: "\<not> n < 0"
   459   for n :: nat
   460   by (simp add: less_eq_Suc_le)
   461 
   462 lemma less_nat_zero_code [code]: "n < 0 \<longleftrightarrow> False"
   463   for n :: nat
   464   by simp
   465 
   466 lemma Suc_less_eq [iff]: "Suc m < Suc n \<longleftrightarrow> m < n"
   467   by (simp add: less_eq_Suc_le)
   468 
   469 lemma less_Suc_eq_le [code]: "m < Suc n \<longleftrightarrow> m \<le> n"
   470   by (simp add: less_eq_Suc_le)
   471 
   472 lemma Suc_less_eq2: "Suc n < m \<longleftrightarrow> (\<exists>m'. m = Suc m' \<and> n < m')"
   473   by (cases m) auto
   474 
   475 lemma le_SucI: "m \<le> n \<Longrightarrow> m \<le> Suc n"
   476   by (induct m arbitrary: n) (simp_all add: less_eq_nat.simps(2) split: nat.splits)
   477 
   478 lemma Suc_leD: "Suc m \<le> n \<Longrightarrow> m \<le> n"
   479   by (cases n) (auto intro: le_SucI)
   480 
   481 lemma less_SucI: "m < n \<Longrightarrow> m < Suc n"
   482   by (simp add: less_eq_Suc_le) (erule Suc_leD)
   483 
   484 lemma Suc_lessD: "Suc m < n \<Longrightarrow> m < n"
   485   by (simp add: less_eq_Suc_le) (erule Suc_leD)
   486 
   487 instance
   488 proof
   489   fix n m q :: nat
   490   show "n < m \<longleftrightarrow> n \<le> m \<and> \<not> m \<le> n"
   491   proof (induct n arbitrary: m)
   492     case 0
   493     then show ?case
   494       by (cases m) (simp_all add: less_eq_Suc_le)
   495   next
   496     case (Suc n)
   497     then show ?case
   498       by (cases m) (simp_all add: less_eq_Suc_le)
   499   qed
   500   show "n \<le> n"
   501     by (induct n) simp_all
   502   then show "n = m" if "n \<le> m" and "m \<le> n"
   503     using that by (induct n arbitrary: m)
   504       (simp_all add: less_eq_nat.simps(2) split: nat.splits)
   505   show "n \<le> q" if "n \<le> m" and "m \<le> q"
   506     using that
   507   proof (induct n arbitrary: m q)
   508     case 0
   509     show ?case by simp
   510   next
   511     case (Suc n)
   512     then show ?case
   513       by (simp_all (no_asm_use) add: less_eq_nat.simps(2) split: nat.splits, clarify,
   514         simp_all (no_asm_use) add: less_eq_nat.simps(2) split: nat.splits, clarify,
   515         simp_all (no_asm_use) add: less_eq_nat.simps(2) split: nat.splits)
   516   qed
   517   show "n \<le> m \<or> m \<le> n"
   518     by (induct n arbitrary: m)
   519       (simp_all add: less_eq_nat.simps(2) split: nat.splits)
   520 qed
   521 
   522 end
   523 
   524 instantiation nat :: order_bot
   525 begin
   526 
   527 definition bot_nat :: nat
   528   where "bot_nat = 0"
   529 
   530 instance
   531   by standard (simp add: bot_nat_def)
   532 
   533 end
   534 
   535 instance nat :: no_top
   536   by standard (auto intro: less_Suc_eq_le [THEN iffD2])
   537 
   538 
   539 subsubsection \<open>Introduction properties\<close>
   540 
   541 lemma lessI [iff]: "n < Suc n"
   542   by (simp add: less_Suc_eq_le)
   543 
   544 lemma zero_less_Suc [iff]: "0 < Suc n"
   545   by (simp add: less_Suc_eq_le)
   546 
   547 
   548 subsubsection \<open>Elimination properties\<close>
   549 
   550 lemma less_not_refl: "\<not> n < n"
   551   for n :: nat
   552   by (rule order_less_irrefl)
   553 
   554 lemma less_not_refl2: "n < m \<Longrightarrow> m \<noteq> n"
   555   for m n :: nat
   556   by (rule not_sym) (rule less_imp_neq)
   557 
   558 lemma less_not_refl3: "s < t \<Longrightarrow> s \<noteq> t"
   559   for s t :: nat
   560   by (rule less_imp_neq)
   561 
   562 lemma less_irrefl_nat: "n < n \<Longrightarrow> R"
   563   for n :: nat
   564   by (rule notE, rule less_not_refl)
   565 
   566 lemma less_zeroE: "n < 0 \<Longrightarrow> R"
   567   for n :: nat
   568   by (rule notE) (rule not_less0)
   569 
   570 lemma less_Suc_eq: "m < Suc n \<longleftrightarrow> m < n \<or> m = n"
   571   unfolding less_Suc_eq_le le_less ..
   572 
   573 lemma less_Suc0 [iff]: "(n < Suc 0) = (n = 0)"
   574   by (simp add: less_Suc_eq)
   575 
   576 lemma less_one [iff]: "n < 1 \<longleftrightarrow> n = 0"
   577   for n :: nat
   578   unfolding One_nat_def by (rule less_Suc0)
   579 
   580 lemma Suc_mono: "m < n \<Longrightarrow> Suc m < Suc n"
   581   by simp
   582 
   583 text \<open>"Less than" is antisymmetric, sort of.\<close>
   584 lemma less_antisym: "\<not> n < m \<Longrightarrow> n < Suc m \<Longrightarrow> m = n"
   585   unfolding not_less less_Suc_eq_le by (rule antisym)
   586 
   587 lemma nat_neq_iff: "m \<noteq> n \<longleftrightarrow> m < n \<or> n < m"
   588   for m n :: nat
   589   by (rule linorder_neq_iff)
   590 
   591 
   592 subsubsection \<open>Inductive (?) properties\<close>
   593 
   594 lemma Suc_lessI: "m < n \<Longrightarrow> Suc m \<noteq> n \<Longrightarrow> Suc m < n"
   595   unfolding less_eq_Suc_le [of m] le_less by simp
   596 
   597 lemma lessE:
   598   assumes major: "i < k"
   599     and 1: "k = Suc i \<Longrightarrow> P"
   600     and 2: "\<And>j. i < j \<Longrightarrow> k = Suc j \<Longrightarrow> P"
   601   shows P
   602 proof -
   603   from major have "\<exists>j. i \<le> j \<and> k = Suc j"
   604     unfolding less_eq_Suc_le by (induct k) simp_all
   605   then have "(\<exists>j. i < j \<and> k = Suc j) \<or> k = Suc i"
   606     by (auto simp add: less_le)
   607   with 1 2 show P by auto
   608 qed
   609 
   610 lemma less_SucE:
   611   assumes major: "m < Suc n"
   612     and less: "m < n \<Longrightarrow> P"
   613     and eq: "m = n \<Longrightarrow> P"
   614   shows P
   615   apply (rule major [THEN lessE])
   616    apply (rule eq)
   617    apply blast
   618   apply (rule less)
   619   apply blast
   620   done
   621 
   622 lemma Suc_lessE:
   623   assumes major: "Suc i < k"
   624     and minor: "\<And>j. i < j \<Longrightarrow> k = Suc j \<Longrightarrow> P"
   625   shows P
   626   apply (rule major [THEN lessE])
   627    apply (erule lessI [THEN minor])
   628   apply (erule Suc_lessD [THEN minor])
   629   apply assumption
   630   done
   631 
   632 lemma Suc_less_SucD: "Suc m < Suc n \<Longrightarrow> m < n"
   633   by simp
   634 
   635 lemma less_trans_Suc:
   636   assumes le: "i < j"
   637   shows "j < k \<Longrightarrow> Suc i < k"
   638 proof (induct k)
   639   case 0
   640   then show ?case by simp
   641 next
   642   case (Suc k)
   643   with le show ?case
   644     by simp (auto simp add: less_Suc_eq dest: Suc_lessD)
   645 qed
   646 
   647 text \<open>Can be used with \<open>less_Suc_eq\<close> to get @{prop "n = m \<or> n < m"}.\<close>
   648 lemma not_less_eq: "\<not> m < n \<longleftrightarrow> n < Suc m"
   649   by (simp only: not_less less_Suc_eq_le)
   650 
   651 lemma not_less_eq_eq: "\<not> m \<le> n \<longleftrightarrow> Suc n \<le> m"
   652   by (simp only: not_le Suc_le_eq)
   653 
   654 text \<open>Properties of "less than or equal".\<close>
   655 
   656 lemma le_imp_less_Suc: "m \<le> n \<Longrightarrow> m < Suc n"
   657   by (simp only: less_Suc_eq_le)
   658 
   659 lemma Suc_n_not_le_n: "\<not> Suc n \<le> n"
   660   by (simp add: not_le less_Suc_eq_le)
   661 
   662 lemma le_Suc_eq: "m \<le> Suc n \<longleftrightarrow> m \<le> n \<or> m = Suc n"
   663   by (simp add: less_Suc_eq_le [symmetric] less_Suc_eq)
   664 
   665 lemma le_SucE: "m \<le> Suc n \<Longrightarrow> (m \<le> n \<Longrightarrow> R) \<Longrightarrow> (m = Suc n \<Longrightarrow> R) \<Longrightarrow> R"
   666   by (drule le_Suc_eq [THEN iffD1], iprover+)
   667 
   668 lemma Suc_leI: "m < n \<Longrightarrow> Suc m \<le> n"
   669   by (simp only: Suc_le_eq)
   670 
   671 text \<open>Stronger version of \<open>Suc_leD\<close>.\<close>
   672 lemma Suc_le_lessD: "Suc m \<le> n \<Longrightarrow> m < n"
   673   by (simp only: Suc_le_eq)
   674 
   675 lemma less_imp_le_nat: "m < n \<Longrightarrow> m \<le> n" for m n :: nat
   676   unfolding less_eq_Suc_le by (rule Suc_leD)
   677 
   678 text \<open>For instance, \<open>(Suc m < Suc n) = (Suc m \<le> n) = (m < n)\<close>\<close>
   679 lemmas le_simps = less_imp_le_nat less_Suc_eq_le Suc_le_eq
   680 
   681 
   682 text \<open>Equivalence of \<open>m \<le> n\<close> and \<open>m < n \<or> m = n\<close>\<close>
   683 
   684 lemma less_or_eq_imp_le: "m < n \<or> m = n \<Longrightarrow> m \<le> n"
   685   for m n :: nat
   686   unfolding le_less .
   687 
   688 lemma le_eq_less_or_eq: "m \<le> n \<longleftrightarrow> m < n \<or> m = n"
   689   for m n :: nat
   690   by (rule le_less)
   691 
   692 text \<open>Useful with \<open>blast\<close>.\<close>
   693 lemma eq_imp_le: "m = n \<Longrightarrow> m \<le> n"
   694   for m n :: nat
   695   by auto
   696 
   697 lemma le_refl: "n \<le> n"
   698   for n :: nat
   699   by simp
   700 
   701 lemma le_trans: "i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> i \<le> k"
   702   for i j k :: nat
   703   by (rule order_trans)
   704 
   705 lemma le_antisym: "m \<le> n \<Longrightarrow> n \<le> m \<Longrightarrow> m = n"
   706   for m n :: nat
   707   by (rule antisym)
   708 
   709 lemma nat_less_le: "m < n \<longleftrightarrow> m \<le> n \<and> m \<noteq> n"
   710   for m n :: nat
   711   by (rule less_le)
   712 
   713 lemma le_neq_implies_less: "m \<le> n \<Longrightarrow> m \<noteq> n \<Longrightarrow> m < n"
   714   for m n :: nat
   715   unfolding less_le ..
   716 
   717 lemma nat_le_linear: "m \<le> n | n \<le> m"
   718   for m n :: nat
   719   by (rule linear)
   720 
   721 lemmas linorder_neqE_nat = linorder_neqE [where 'a = nat]
   722 
   723 lemma le_less_Suc_eq: "m \<le> n \<Longrightarrow> n < Suc m \<longleftrightarrow> n = m"
   724   unfolding less_Suc_eq_le by auto
   725 
   726 lemma not_less_less_Suc_eq: "\<not> n < m \<Longrightarrow> n < Suc m \<longleftrightarrow> n = m"
   727   unfolding not_less by (rule le_less_Suc_eq)
   728 
   729 lemmas not_less_simps = not_less_less_Suc_eq le_less_Suc_eq
   730 
   731 lemma not0_implies_Suc: "n \<noteq> 0 \<Longrightarrow> \<exists>m. n = Suc m"
   732   by (cases n) simp_all
   733 
   734 lemma gr0_implies_Suc: "n > 0 \<Longrightarrow> \<exists>m. n = Suc m"
   735   by (cases n) simp_all
   736 
   737 lemma gr_implies_not0: "m < n \<Longrightarrow> n \<noteq> 0"
   738   for m n :: nat
   739   by (cases n) simp_all
   740 
   741 lemma neq0_conv[iff]: "n \<noteq> 0 \<longleftrightarrow> 0 < n"
   742   for n :: nat
   743   by (cases n) simp_all
   744 
   745 text \<open>This theorem is useful with \<open>blast\<close>\<close>
   746 lemma gr0I: "(n = 0 \<Longrightarrow> False) \<Longrightarrow> 0 < n"
   747   for n :: nat
   748   by (rule neq0_conv[THEN iffD1]) iprover
   749 
   750 lemma gr0_conv_Suc: "0 < n \<longleftrightarrow> (\<exists>m. n = Suc m)"
   751   by (fast intro: not0_implies_Suc)
   752 
   753 lemma not_gr0 [iff]: "\<not> 0 < n \<longleftrightarrow> n = 0"
   754   for n :: nat
   755   using neq0_conv by blast
   756 
   757 lemma Suc_le_D: "Suc n \<le> m' \<Longrightarrow> \<exists>m. m' = Suc m"
   758   by (induct m') simp_all
   759 
   760 text \<open>Useful in certain inductive arguments\<close>
   761 lemma less_Suc_eq_0_disj: "m < Suc n \<longleftrightarrow> m = 0 \<or> (\<exists>j. m = Suc j \<and> j < n)"
   762   by (cases m) simp_all
   763 
   764 lemma All_less_Suc: "(\<forall>i < Suc n. P i) = (P n \<and> (\<forall>i < n. P i))"
   765 by (auto simp: less_Suc_eq)
   766 
   767 lemma All_less_Suc2: "(\<forall>i < Suc n. P i) = (P 0 \<and> (\<forall>i < n. P(Suc i)))"
   768 by (auto simp: less_Suc_eq_0_disj)
   769 
   770 lemma Ex_less_Suc: "(\<exists>i < Suc n. P i) = (P n \<or> (\<exists>i < n. P i))"
   771 by (auto simp: less_Suc_eq)
   772 
   773 lemma Ex_less_Suc2: "(\<exists>i < Suc n. P i) = (P 0 \<or> (\<exists>i < n. P(Suc i)))"
   774 by (auto simp: less_Suc_eq_0_disj)
   775 
   776 
   777 subsubsection \<open>Monotonicity of Addition\<close>
   778 
   779 lemma Suc_pred [simp]: "n > 0 \<Longrightarrow> Suc (n - Suc 0) = n"
   780   by (simp add: diff_Suc split: nat.split)
   781 
   782 lemma Suc_diff_1 [simp]: "0 < n \<Longrightarrow> Suc (n - 1) = n"
   783   unfolding One_nat_def by (rule Suc_pred)
   784 
   785 lemma nat_add_left_cancel_le [simp]: "k + m \<le> k + n \<longleftrightarrow> m \<le> n"
   786   for k m n :: nat
   787   by (induct k) simp_all
   788 
   789 lemma nat_add_left_cancel_less [simp]: "k + m < k + n \<longleftrightarrow> m < n"
   790   for k m n :: nat
   791   by (induct k) simp_all
   792 
   793 lemma add_gr_0 [iff]: "m + n > 0 \<longleftrightarrow> m > 0 \<or> n > 0"
   794   for m n :: nat
   795   by (auto dest: gr0_implies_Suc)
   796 
   797 text \<open>strict, in 1st argument\<close>
   798 lemma add_less_mono1: "i < j \<Longrightarrow> i + k < j + k"
   799   for i j k :: nat
   800   by (induct k) simp_all
   801 
   802 text \<open>strict, in both arguments\<close>
   803 lemma add_less_mono: "i < j \<Longrightarrow> k < l \<Longrightarrow> i + k < j + l"
   804   for i j k l :: nat
   805   apply (rule add_less_mono1 [THEN less_trans], assumption+)
   806   apply (induct j)
   807    apply simp_all
   808   done
   809 
   810 text \<open>Deleted \<open>less_natE\<close>; use \<open>less_imp_Suc_add RS exE\<close>\<close>
   811 lemma less_imp_Suc_add: "m < n \<Longrightarrow> \<exists>k. n = Suc (m + k)"
   812 proof (induct n)
   813   case 0
   814   then show ?case by simp
   815 next
   816   case Suc
   817   then show ?case
   818     by (simp add: order_le_less)
   819       (blast elim!: less_SucE intro!: Nat.add_0_right [symmetric] add_Suc_right [symmetric])
   820 qed
   821 
   822 lemma le_Suc_ex: "k \<le> l \<Longrightarrow> (\<exists>n. l = k + n)"
   823   for k l :: nat
   824   by (auto simp: less_Suc_eq_le[symmetric] dest: less_imp_Suc_add)
   825 
   826 text \<open>strict, in 1st argument; proof is by induction on \<open>k > 0\<close>\<close>
   827 lemma mult_less_mono2:
   828   fixes i j :: nat
   829   assumes "i < j" and "0 < k"
   830   shows "k * i < k * j"
   831   using \<open>0 < k\<close>
   832 proof (induct k)
   833   case 0
   834   then show ?case by simp
   835 next
   836   case (Suc k)
   837   with \<open>i < j\<close> show ?case
   838     by (cases k) (simp_all add: add_less_mono)
   839 qed
   840 
   841 text \<open>Addition is the inverse of subtraction:
   842   if @{term "n \<le> m"} then @{term "n + (m - n) = m"}.\<close>
   843 lemma add_diff_inverse_nat: "\<not> m < n \<Longrightarrow> n + (m - n) = m"
   844   for m n :: nat
   845   by (induct m n rule: diff_induct) simp_all
   846 
   847 lemma nat_le_iff_add: "m \<le> n \<longleftrightarrow> (\<exists>k. n = m + k)"
   848   for m n :: nat
   849   using nat_add_left_cancel_le[of m 0] by (auto dest: le_Suc_ex)
   850 
   851 text \<open>The naturals form an ordered \<open>semidom\<close> and a \<open>dioid\<close>.\<close>
   852 
   853 instance nat :: linordered_semidom
   854 proof
   855   fix m n q :: nat
   856   show "0 < (1::nat)"
   857     by simp
   858   show "m \<le> n \<Longrightarrow> q + m \<le> q + n"
   859     by simp
   860   show "m < n \<Longrightarrow> 0 < q \<Longrightarrow> q * m < q * n"
   861     by (simp add: mult_less_mono2)
   862   show "m \<noteq> 0 \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> m * n \<noteq> 0"
   863     by simp
   864   show "n \<le> m \<Longrightarrow> (m - n) + n = m"
   865     by (simp add: add_diff_inverse_nat add.commute linorder_not_less)
   866 qed
   867 
   868 instance nat :: dioid
   869   by standard (rule nat_le_iff_add)
   870 
   871 declare le0[simp del] \<comment> \<open>This is now @{thm zero_le}\<close>
   872 declare le_0_eq[simp del] \<comment> \<open>This is now @{thm le_zero_eq}\<close>
   873 declare not_less0[simp del] \<comment> \<open>This is now @{thm not_less_zero}\<close>
   874 declare not_gr0[simp del] \<comment> \<open>This is now @{thm not_gr_zero}\<close>
   875 
   876 instance nat :: ordered_cancel_comm_monoid_add ..
   877 instance nat :: ordered_cancel_comm_monoid_diff ..
   878 
   879 
   880 subsubsection \<open>@{term min} and @{term max}\<close>
   881 
   882 lemma mono_Suc: "mono Suc"
   883   by (rule monoI) simp
   884 
   885 lemma min_0L [simp]: "min 0 n = 0"
   886   for n :: nat
   887   by (rule min_absorb1) simp
   888 
   889 lemma min_0R [simp]: "min n 0 = 0"
   890   for n :: nat
   891   by (rule min_absorb2) simp
   892 
   893 lemma min_Suc_Suc [simp]: "min (Suc m) (Suc n) = Suc (min m n)"
   894   by (simp add: mono_Suc min_of_mono)
   895 
   896 lemma min_Suc1: "min (Suc n) m = (case m of 0 \<Rightarrow> 0 | Suc m' \<Rightarrow> Suc(min n m'))"
   897   by (simp split: nat.split)
   898 
   899 lemma min_Suc2: "min m (Suc n) = (case m of 0 \<Rightarrow> 0 | Suc m' \<Rightarrow> Suc(min m' n))"
   900   by (simp split: nat.split)
   901 
   902 lemma max_0L [simp]: "max 0 n = n"
   903   for n :: nat
   904   by (rule max_absorb2) simp
   905 
   906 lemma max_0R [simp]: "max n 0 = n"
   907   for n :: nat
   908   by (rule max_absorb1) simp
   909 
   910 lemma max_Suc_Suc [simp]: "max (Suc m) (Suc n) = Suc (max m n)"
   911   by (simp add: mono_Suc max_of_mono)
   912 
   913 lemma max_Suc1: "max (Suc n) m = (case m of 0 \<Rightarrow> Suc n | Suc m' \<Rightarrow> Suc (max n m'))"
   914   by (simp split: nat.split)
   915 
   916 lemma max_Suc2: "max m (Suc n) = (case m of 0 \<Rightarrow> Suc n | Suc m' \<Rightarrow> Suc (max m' n))"
   917   by (simp split: nat.split)
   918 
   919 lemma nat_mult_min_left: "min m n * q = min (m * q) (n * q)"
   920   for m n q :: nat
   921   by (simp add: min_def not_le)
   922     (auto dest: mult_right_le_imp_le mult_right_less_imp_less le_less_trans)
   923 
   924 lemma nat_mult_min_right: "m * min n q = min (m * n) (m * q)"
   925   for m n q :: nat
   926   by (simp add: min_def not_le)
   927     (auto dest: mult_left_le_imp_le mult_left_less_imp_less le_less_trans)
   928 
   929 lemma nat_add_max_left: "max m n + q = max (m + q) (n + q)"
   930   for m n q :: nat
   931   by (simp add: max_def)
   932 
   933 lemma nat_add_max_right: "m + max n q = max (m + n) (m + q)"
   934   for m n q :: nat
   935   by (simp add: max_def)
   936 
   937 lemma nat_mult_max_left: "max m n * q = max (m * q) (n * q)"
   938   for m n q :: nat
   939   by (simp add: max_def not_le)
   940     (auto dest: mult_right_le_imp_le mult_right_less_imp_less le_less_trans)
   941 
   942 lemma nat_mult_max_right: "m * max n q = max (m * n) (m * q)"
   943   for m n q :: nat
   944   by (simp add: max_def not_le)
   945     (auto dest: mult_left_le_imp_le mult_left_less_imp_less le_less_trans)
   946 
   947 
   948 subsubsection \<open>Additional theorems about @{term "op \<le>"}\<close>
   949 
   950 text \<open>Complete induction, aka course-of-values induction\<close>
   951 
   952 instance nat :: wellorder
   953 proof
   954   fix P and n :: nat
   955   assume step: "(\<And>m. m < n \<Longrightarrow> P m) \<Longrightarrow> P n" for n :: nat
   956   have "\<And>q. q \<le> n \<Longrightarrow> P q"
   957   proof (induct n)
   958     case (0 n)
   959     have "P 0" by (rule step) auto
   960     with 0 show ?case by auto
   961   next
   962     case (Suc m n)
   963     then have "n \<le> m \<or> n = Suc m"
   964       by (simp add: le_Suc_eq)
   965     then show ?case
   966     proof
   967       assume "n \<le> m"
   968       then show "P n" by (rule Suc(1))
   969     next
   970       assume n: "n = Suc m"
   971       show "P n" by (rule step) (rule Suc(1), simp add: n le_simps)
   972     qed
   973   qed
   974   then show "P n" by auto
   975 qed
   976 
   977 
   978 lemma Least_eq_0[simp]: "P 0 \<Longrightarrow> Least P = 0"
   979   for P :: "nat \<Rightarrow> bool"
   980   by (rule Least_equality[OF _ le0])
   981 
   982 lemma Least_Suc: "P n \<Longrightarrow> \<not> P 0 \<Longrightarrow> (LEAST n. P n) = Suc (LEAST m. P (Suc m))"
   983   apply (cases n)
   984    apply auto
   985   apply (frule LeastI)
   986   apply (drule_tac P = "\<lambda>x. P (Suc x)" in LeastI)
   987   apply (subgoal_tac " (LEAST x. P x) \<le> Suc (LEAST x. P (Suc x))")
   988    apply (erule_tac [2] Least_le)
   989   apply (cases "LEAST x. P x")
   990    apply auto
   991   apply (drule_tac P = "\<lambda>x. P (Suc x)" in Least_le)
   992   apply (blast intro: order_antisym)
   993   done
   994 
   995 lemma Least_Suc2: "P n \<Longrightarrow> Q m \<Longrightarrow> \<not> P 0 \<Longrightarrow> \<forall>k. P (Suc k) = Q k \<Longrightarrow> Least P = Suc (Least Q)"
   996   by (erule (1) Least_Suc [THEN ssubst]) simp
   997 
   998 lemma ex_least_nat_le: "\<not> P 0 \<Longrightarrow> P n \<Longrightarrow> \<exists>k\<le>n. (\<forall>i<k. \<not> P i) \<and> P k"
   999   for P :: "nat \<Rightarrow> bool"
  1000   apply (cases n)
  1001    apply blast
  1002   apply (rule_tac x="LEAST k. P k" in exI)
  1003   apply (blast intro: Least_le dest: not_less_Least intro: LeastI_ex)
  1004   done
  1005 
  1006 lemma ex_least_nat_less: "\<not> P 0 \<Longrightarrow> P n \<Longrightarrow> \<exists>k<n. (\<forall>i\<le>k. \<not> P i) \<and> P (k + 1)"
  1007   for P :: "nat \<Rightarrow> bool"
  1008   apply (cases n)
  1009    apply blast
  1010   apply (frule (1) ex_least_nat_le)
  1011   apply (erule exE)
  1012   apply (case_tac k)
  1013    apply simp
  1014   apply (rename_tac k1)
  1015   apply (rule_tac x=k1 in exI)
  1016   apply (auto simp add: less_eq_Suc_le)
  1017   done
  1018 
  1019 lemma nat_less_induct:
  1020   fixes P :: "nat \<Rightarrow> bool"
  1021   assumes "\<And>n. \<forall>m. m < n \<longrightarrow> P m \<Longrightarrow> P n"
  1022   shows "P n"
  1023   using assms less_induct by blast
  1024 
  1025 lemma measure_induct_rule [case_names less]:
  1026   fixes f :: "'a \<Rightarrow> 'b::wellorder"
  1027   assumes step: "\<And>x. (\<And>y. f y < f x \<Longrightarrow> P y) \<Longrightarrow> P x"
  1028   shows "P a"
  1029   by (induct m \<equiv> "f a" arbitrary: a rule: less_induct) (auto intro: step)
  1030 
  1031 text \<open>old style induction rules:\<close>
  1032 lemma measure_induct:
  1033   fixes f :: "'a \<Rightarrow> 'b::wellorder"
  1034   shows "(\<And>x. \<forall>y. f y < f x \<longrightarrow> P y \<Longrightarrow> P x) \<Longrightarrow> P a"
  1035   by (rule measure_induct_rule [of f P a]) iprover
  1036 
  1037 lemma full_nat_induct:
  1038   assumes step: "\<And>n. (\<forall>m. Suc m \<le> n \<longrightarrow> P m) \<Longrightarrow> P n"
  1039   shows "P n"
  1040   by (rule less_induct) (auto intro: step simp:le_simps)
  1041 
  1042 text\<open>An induction rule for establishing binary relations\<close>
  1043 lemma less_Suc_induct [consumes 1]:
  1044   assumes less: "i < j"
  1045     and step: "\<And>i. P i (Suc i)"
  1046     and trans: "\<And>i j k. i < j \<Longrightarrow> j < k \<Longrightarrow> P i j \<Longrightarrow> P j k \<Longrightarrow> P i k"
  1047   shows "P i j"
  1048 proof -
  1049   from less obtain k where j: "j = Suc (i + k)"
  1050     by (auto dest: less_imp_Suc_add)
  1051   have "P i (Suc (i + k))"
  1052   proof (induct k)
  1053     case 0
  1054     show ?case by (simp add: step)
  1055   next
  1056     case (Suc k)
  1057     have "0 + i < Suc k + i" by (rule add_less_mono1) simp
  1058     then have "i < Suc (i + k)" by (simp add: add.commute)
  1059     from trans[OF this lessI Suc step]
  1060     show ?case by simp
  1061   qed
  1062   then show "P i j" by (simp add: j)
  1063 qed
  1064 
  1065 text \<open>
  1066   The method of infinite descent, frequently used in number theory.
  1067   Provided by Roelof Oosterhuis.
  1068   \<open>P n\<close> is true for all natural numbers if
  1069   \<^item> case ``0'': given \<open>n = 0\<close> prove \<open>P n\<close>
  1070   \<^item> case ``smaller'': given \<open>n > 0\<close> and \<open>\<not> P n\<close> prove there exists
  1071     a smaller natural number \<open>m\<close> such that \<open>\<not> P m\<close>.
  1072 \<close>
  1073 
  1074 lemma infinite_descent: "(\<And>n. \<not> P n \<Longrightarrow> \<exists>m<n. \<not> P m) \<Longrightarrow> P n" for P :: "nat \<Rightarrow> bool"
  1075   \<comment> \<open>compact version without explicit base case\<close>
  1076   by (induct n rule: less_induct) auto
  1077 
  1078 lemma infinite_descent0 [case_names 0 smaller]:
  1079   fixes P :: "nat \<Rightarrow> bool"
  1080   assumes "P 0"
  1081     and "\<And>n. n > 0 \<Longrightarrow> \<not> P n \<Longrightarrow> \<exists>m. m < n \<and> \<not> P m"
  1082   shows "P n"
  1083   apply (rule infinite_descent)
  1084   using assms
  1085   apply (case_tac "n > 0")
  1086    apply auto
  1087   done
  1088 
  1089 text \<open>
  1090   Infinite descent using a mapping to \<open>nat\<close>:
  1091   \<open>P x\<close> is true for all \<open>x \<in> D\<close> if there exists a \<open>V \<in> D \<Rightarrow> nat\<close> and
  1092   \<^item> case ``0'': given \<open>V x = 0\<close> prove \<open>P x\<close>
  1093   \<^item> ``smaller'': given \<open>V x > 0\<close> and \<open>\<not> P x\<close> prove
  1094   there exists a \<open>y \<in> D\<close> such that \<open>V y < V x\<close> and \<open>\<not> P y\<close>.
  1095 \<close>
  1096 corollary infinite_descent0_measure [case_names 0 smaller]:
  1097   fixes V :: "'a \<Rightarrow> nat"
  1098   assumes 1: "\<And>x. V x = 0 \<Longrightarrow> P x"
  1099     and 2: "\<And>x. V x > 0 \<Longrightarrow> \<not> P x \<Longrightarrow> \<exists>y. V y < V x \<and> \<not> P y"
  1100   shows "P x"
  1101 proof -
  1102   obtain n where "n = V x" by auto
  1103   moreover have "\<And>x. V x = n \<Longrightarrow> P x"
  1104   proof (induct n rule: infinite_descent0)
  1105     case 0
  1106     with 1 show "P x" by auto
  1107   next
  1108     case (smaller n)
  1109     then obtain x where *: "V x = n " and "V x > 0 \<and> \<not> P x" by auto
  1110     with 2 obtain y where "V y < V x \<and> \<not> P y" by auto
  1111     with * obtain m where "m = V y \<and> m < n \<and> \<not> P y" by auto
  1112     then show ?case by auto
  1113   qed
  1114   ultimately show "P x" by auto
  1115 qed
  1116 
  1117 text \<open>Again, without explicit base case:\<close>
  1118 lemma infinite_descent_measure:
  1119   fixes V :: "'a \<Rightarrow> nat"
  1120   assumes "\<And>x. \<not> P x \<Longrightarrow> \<exists>y. V y < V x \<and> \<not> P y"
  1121   shows "P x"
  1122 proof -
  1123   from assms obtain n where "n = V x" by auto
  1124   moreover have "\<And>x. V x = n \<Longrightarrow> P x"
  1125   proof (induct n rule: infinite_descent, auto)
  1126     show "\<exists>m < V x. \<exists>y. V y = m \<and> \<not> P y" if "\<not> P x" for x
  1127       using assms and that by auto
  1128   qed
  1129   ultimately show "P x" by auto
  1130 qed
  1131 
  1132 text \<open>A (clumsy) way of lifting \<open><\<close> monotonicity to \<open>\<le>\<close> monotonicity\<close>
  1133 lemma less_mono_imp_le_mono:
  1134   fixes f :: "nat \<Rightarrow> nat"
  1135     and i j :: nat
  1136   assumes "\<And>i j::nat. i < j \<Longrightarrow> f i < f j"
  1137     and "i \<le> j"
  1138   shows "f i \<le> f j"
  1139   using assms by (auto simp add: order_le_less)
  1140 
  1141 
  1142 text \<open>non-strict, in 1st argument\<close>
  1143 lemma add_le_mono1: "i \<le> j \<Longrightarrow> i + k \<le> j + k"
  1144   for i j k :: nat
  1145   by (rule add_right_mono)
  1146 
  1147 text \<open>non-strict, in both arguments\<close>
  1148 lemma add_le_mono: "i \<le> j \<Longrightarrow> k \<le> l \<Longrightarrow> i + k \<le> j + l"
  1149   for i j k l :: nat
  1150   by (rule add_mono)
  1151 
  1152 lemma le_add2: "n \<le> m + n"
  1153   for m n :: nat
  1154   by simp
  1155 
  1156 lemma le_add1: "n \<le> n + m"
  1157   for m n :: nat
  1158   by simp
  1159 
  1160 lemma less_add_Suc1: "i < Suc (i + m)"
  1161   by (rule le_less_trans, rule le_add1, rule lessI)
  1162 
  1163 lemma less_add_Suc2: "i < Suc (m + i)"
  1164   by (rule le_less_trans, rule le_add2, rule lessI)
  1165 
  1166 lemma less_iff_Suc_add: "m < n \<longleftrightarrow> (\<exists>k. n = Suc (m + k))"
  1167   by (iprover intro!: less_add_Suc1 less_imp_Suc_add)
  1168 
  1169 lemma trans_le_add1: "i \<le> j \<Longrightarrow> i \<le> j + m"
  1170   for i j m :: nat
  1171   by (rule le_trans, assumption, rule le_add1)
  1172 
  1173 lemma trans_le_add2: "i \<le> j \<Longrightarrow> i \<le> m + j"
  1174   for i j m :: nat
  1175   by (rule le_trans, assumption, rule le_add2)
  1176 
  1177 lemma trans_less_add1: "i < j \<Longrightarrow> i < j + m"
  1178   for i j m :: nat
  1179   by (rule less_le_trans, assumption, rule le_add1)
  1180 
  1181 lemma trans_less_add2: "i < j \<Longrightarrow> i < m + j"
  1182   for i j m :: nat
  1183   by (rule less_le_trans, assumption, rule le_add2)
  1184 
  1185 lemma add_lessD1: "i + j < k \<Longrightarrow> i < k"
  1186   for i j k :: nat
  1187   by (rule le_less_trans [of _ "i+j"]) (simp_all add: le_add1)
  1188 
  1189 lemma not_add_less1 [iff]: "\<not> i + j < i"
  1190   for i j :: nat
  1191   apply (rule notI)
  1192   apply (drule add_lessD1)
  1193   apply (erule less_irrefl [THEN notE])
  1194   done
  1195 
  1196 lemma not_add_less2 [iff]: "\<not> j + i < i"
  1197   for i j :: nat
  1198   by (simp add: add.commute)
  1199 
  1200 lemma add_leD1: "m + k \<le> n \<Longrightarrow> m \<le> n"
  1201   for k m n :: nat
  1202   by (rule order_trans [of _ "m + k"]) (simp_all add: le_add1)
  1203 
  1204 lemma add_leD2: "m + k \<le> n \<Longrightarrow> k \<le> n"
  1205   for k m n :: nat
  1206   apply (simp add: add.commute)
  1207   apply (erule add_leD1)
  1208   done
  1209 
  1210 lemma add_leE: "m + k \<le> n \<Longrightarrow> (m \<le> n \<Longrightarrow> k \<le> n \<Longrightarrow> R) \<Longrightarrow> R"
  1211   for k m n :: nat
  1212   by (blast dest: add_leD1 add_leD2)
  1213 
  1214 text \<open>needs \<open>\<And>k\<close> for \<open>ac_simps\<close> to work\<close>
  1215 lemma less_add_eq_less: "\<And>k. k < l \<Longrightarrow> m + l = k + n \<Longrightarrow> m < n"
  1216   for l m n :: nat
  1217   by (force simp del: add_Suc_right simp add: less_iff_Suc_add add_Suc_right [symmetric] ac_simps)
  1218 
  1219 
  1220 subsubsection \<open>More results about difference\<close>
  1221 
  1222 lemma Suc_diff_le: "n \<le> m \<Longrightarrow> Suc m - n = Suc (m - n)"
  1223   by (induct m n rule: diff_induct) simp_all
  1224 
  1225 lemma diff_less_Suc: "m - n < Suc m"
  1226   apply (induct m n rule: diff_induct)
  1227     apply (erule_tac [3] less_SucE)
  1228      apply (simp_all add: less_Suc_eq)
  1229   done
  1230 
  1231 lemma diff_le_self [simp]: "m - n \<le> m"
  1232   for m n :: nat
  1233   by (induct m n rule: diff_induct) (simp_all add: le_SucI)
  1234 
  1235 lemma less_imp_diff_less: "j < k \<Longrightarrow> j - n < k"
  1236   for j k n :: nat
  1237   by (rule le_less_trans, rule diff_le_self)
  1238 
  1239 lemma diff_Suc_less [simp]: "0 < n \<Longrightarrow> n - Suc i < n"
  1240   by (cases n) (auto simp add: le_simps)
  1241 
  1242 lemma diff_add_assoc: "k \<le> j \<Longrightarrow> (i + j) - k = i + (j - k)"
  1243   for i j k :: nat
  1244   by (induct j k rule: diff_induct) simp_all
  1245 
  1246 lemma add_diff_assoc [simp]: "k \<le> j \<Longrightarrow> i + (j - k) = i + j - k"
  1247   for i j k :: nat
  1248   by (fact diff_add_assoc [symmetric])
  1249 
  1250 lemma diff_add_assoc2: "k \<le> j \<Longrightarrow> (j + i) - k = (j - k) + i"
  1251   for i j k :: nat
  1252   by (simp add: ac_simps)
  1253 
  1254 lemma add_diff_assoc2 [simp]: "k \<le> j \<Longrightarrow> j - k + i = j + i - k"
  1255   for i j k :: nat
  1256   by (fact diff_add_assoc2 [symmetric])
  1257 
  1258 lemma le_imp_diff_is_add: "i \<le> j \<Longrightarrow> (j - i = k) = (j = k + i)"
  1259   for i j k :: nat
  1260   by auto
  1261 
  1262 lemma diff_is_0_eq [simp]: "m - n = 0 \<longleftrightarrow> m \<le> n"
  1263   for m n :: nat
  1264   by (induct m n rule: diff_induct) simp_all
  1265 
  1266 lemma diff_is_0_eq' [simp]: "m \<le> n \<Longrightarrow> m - n = 0"
  1267   for m n :: nat
  1268   by (rule iffD2, rule diff_is_0_eq)
  1269 
  1270 lemma zero_less_diff [simp]: "0 < n - m \<longleftrightarrow> m < n"
  1271   for m n :: nat
  1272   by (induct m n rule: diff_induct) simp_all
  1273 
  1274 lemma less_imp_add_positive:
  1275   assumes "i < j"
  1276   shows "\<exists>k::nat. 0 < k \<and> i + k = j"
  1277 proof
  1278   from assms show "0 < j - i \<and> i + (j - i) = j"
  1279     by (simp add: order_less_imp_le)
  1280 qed
  1281 
  1282 text \<open>a nice rewrite for bounded subtraction\<close>
  1283 lemma nat_minus_add_max: "n - m + m = max n m"
  1284   for m n :: nat
  1285   by (simp add: max_def not_le order_less_imp_le)
  1286 
  1287 lemma nat_diff_split: "P (a - b) \<longleftrightarrow> (a < b \<longrightarrow> P 0) \<and> (\<forall>d. a = b + d \<longrightarrow> P d)"
  1288   for a b :: nat
  1289   \<comment> \<open>elimination of \<open>-\<close> on \<open>nat\<close>\<close>
  1290   by (cases "a < b") (auto simp add: not_less le_less dest!: add_eq_self_zero [OF sym])
  1291 
  1292 lemma nat_diff_split_asm: "P (a - b) \<longleftrightarrow> \<not> (a < b \<and> \<not> P 0 \<or> (\<exists>d. a = b + d \<and> \<not> P d))"
  1293   for a b :: nat
  1294   \<comment> \<open>elimination of \<open>-\<close> on \<open>nat\<close> in assumptions\<close>
  1295   by (auto split: nat_diff_split)
  1296 
  1297 lemma Suc_pred': "0 < n \<Longrightarrow> n = Suc(n - 1)"
  1298   by simp
  1299 
  1300 lemma add_eq_if: "m + n = (if m = 0 then n else Suc ((m - 1) + n))"
  1301   unfolding One_nat_def by (cases m) simp_all
  1302 
  1303 lemma mult_eq_if: "m * n = (if m = 0 then 0 else n + ((m - 1) * n))"
  1304   for m n :: nat
  1305   by (cases m) simp_all
  1306 
  1307 lemma Suc_diff_eq_diff_pred: "0 < n \<Longrightarrow> Suc m - n = m - (n - 1)"
  1308   by (cases n) simp_all
  1309 
  1310 lemma diff_Suc_eq_diff_pred: "m - Suc n = (m - 1) - n"
  1311   by (cases m) simp_all
  1312 
  1313 lemma Let_Suc [simp]: "Let (Suc n) f \<equiv> f (Suc n)"
  1314   by (fact Let_def)
  1315 
  1316 
  1317 subsubsection \<open>Monotonicity of multiplication\<close>
  1318 
  1319 lemma mult_le_mono1: "i \<le> j \<Longrightarrow> i * k \<le> j * k"
  1320   for i j k :: nat
  1321   by (simp add: mult_right_mono)
  1322 
  1323 lemma mult_le_mono2: "i \<le> j \<Longrightarrow> k * i \<le> k * j"
  1324   for i j k :: nat
  1325   by (simp add: mult_left_mono)
  1326 
  1327 text \<open>\<open>\<le>\<close> monotonicity, BOTH arguments\<close>
  1328 lemma mult_le_mono: "i \<le> j \<Longrightarrow> k \<le> l \<Longrightarrow> i * k \<le> j * l"
  1329   for i j k l :: nat
  1330   by (simp add: mult_mono)
  1331 
  1332 lemma mult_less_mono1: "i < j \<Longrightarrow> 0 < k \<Longrightarrow> i * k < j * k"
  1333   for i j k :: nat
  1334   by (simp add: mult_strict_right_mono)
  1335 
  1336 text \<open>Differs from the standard \<open>zero_less_mult_iff\<close> in that there are no negative numbers.\<close>
  1337 lemma nat_0_less_mult_iff [simp]: "0 < m * n \<longleftrightarrow> 0 < m \<and> 0 < n"
  1338   for m n :: nat
  1339 proof (induct m)
  1340   case 0
  1341   then show ?case by simp
  1342 next
  1343   case (Suc m)
  1344   then show ?case by (cases n) simp_all
  1345 qed
  1346 
  1347 lemma one_le_mult_iff [simp]: "Suc 0 \<le> m * n \<longleftrightarrow> Suc 0 \<le> m \<and> Suc 0 \<le> n"
  1348 proof (induct m)
  1349   case 0
  1350   then show ?case by simp
  1351 next
  1352   case (Suc m)
  1353   then show ?case by (cases n) simp_all
  1354 qed
  1355 
  1356 lemma mult_less_cancel2 [simp]: "m * k < n * k \<longleftrightarrow> 0 < k \<and> m < n"
  1357   for k m n :: nat
  1358   apply (safe intro!: mult_less_mono1)
  1359    apply (cases k)
  1360     apply auto
  1361   apply (simp add: linorder_not_le [symmetric])
  1362   apply (blast intro: mult_le_mono1)
  1363   done
  1364 
  1365 lemma mult_less_cancel1 [simp]: "k * m < k * n \<longleftrightarrow> 0 < k \<and> m < n"
  1366   for k m n :: nat
  1367   by (simp add: mult.commute [of k])
  1368 
  1369 lemma mult_le_cancel1 [simp]: "k * m \<le> k * n \<longleftrightarrow> (0 < k \<longrightarrow> m \<le> n)"
  1370   for k m n :: nat
  1371   by (simp add: linorder_not_less [symmetric], auto)
  1372 
  1373 lemma mult_le_cancel2 [simp]: "m * k \<le> n * k \<longleftrightarrow> (0 < k \<longrightarrow> m \<le> n)"
  1374   for k m n :: nat
  1375   by (simp add: linorder_not_less [symmetric], auto)
  1376 
  1377 lemma Suc_mult_less_cancel1: "Suc k * m < Suc k * n \<longleftrightarrow> m < n"
  1378   by (subst mult_less_cancel1) simp
  1379 
  1380 lemma Suc_mult_le_cancel1: "Suc k * m \<le> Suc k * n \<longleftrightarrow> m \<le> n"
  1381   by (subst mult_le_cancel1) simp
  1382 
  1383 lemma le_square: "m \<le> m * m"
  1384   for m :: nat
  1385   by (cases m) (auto intro: le_add1)
  1386 
  1387 lemma le_cube: "m \<le> m * (m * m)"
  1388   for m :: nat
  1389   by (cases m) (auto intro: le_add1)
  1390 
  1391 text \<open>Lemma for \<open>gcd\<close>\<close>
  1392 lemma mult_eq_self_implies_10: "m = m * n \<Longrightarrow> n = 1 \<or> m = 0"
  1393   for m n :: nat
  1394   apply (drule sym)
  1395   apply (rule disjCI)
  1396   apply (rule linorder_cases)
  1397     defer
  1398     apply assumption
  1399    apply (drule mult_less_mono2)
  1400     apply auto
  1401   done
  1402 
  1403 lemma mono_times_nat:
  1404   fixes n :: nat
  1405   assumes "n > 0"
  1406   shows "mono (times n)"
  1407 proof
  1408   fix m q :: nat
  1409   assume "m \<le> q"
  1410   with assms show "n * m \<le> n * q" by simp
  1411 qed
  1412 
  1413 text \<open>The lattice order on @{typ nat}.\<close>
  1414 
  1415 instantiation nat :: distrib_lattice
  1416 begin
  1417 
  1418 definition "(inf :: nat \<Rightarrow> nat \<Rightarrow> nat) = min"
  1419 
  1420 definition "(sup :: nat \<Rightarrow> nat \<Rightarrow> nat) = max"
  1421 
  1422 instance
  1423   by intro_classes
  1424     (auto simp add: inf_nat_def sup_nat_def max_def not_le min_def
  1425       intro: order_less_imp_le antisym elim!: order_trans order_less_trans)
  1426 
  1427 end
  1428 
  1429 
  1430 subsection \<open>Natural operation of natural numbers on functions\<close>
  1431 
  1432 text \<open>
  1433   We use the same logical constant for the power operations on
  1434   functions and relations, in order to share the same syntax.
  1435 \<close>
  1436 
  1437 consts compow :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"
  1438 
  1439 abbreviation compower :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixr "^^" 80)
  1440   where "f ^^ n \<equiv> compow n f"
  1441 
  1442 notation (latex output)
  1443   compower ("(_\<^bsup>_\<^esup>)" [1000] 1000)
  1444 
  1445 text \<open>\<open>f ^^ n = f \<circ> \<dots> \<circ> f\<close>, the \<open>n\<close>-fold composition of \<open>f\<close>\<close>
  1446 
  1447 overloading
  1448   funpow \<equiv> "compow :: nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a)"
  1449 begin
  1450 
  1451 primrec funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
  1452   where
  1453     "funpow 0 f = id"
  1454   | "funpow (Suc n) f = f \<circ> funpow n f"
  1455 
  1456 end
  1457 
  1458 lemma funpow_0 [simp]: "(f ^^ 0) x = x"
  1459   by simp
  1460 
  1461 lemma funpow_Suc_right: "f ^^ Suc n = f ^^ n \<circ> f"
  1462 proof (induct n)
  1463   case 0
  1464   then show ?case by simp
  1465 next
  1466   fix n
  1467   assume "f ^^ Suc n = f ^^ n \<circ> f"
  1468   then show "f ^^ Suc (Suc n) = f ^^ Suc n \<circ> f"
  1469     by (simp add: o_assoc)
  1470 qed
  1471 
  1472 lemmas funpow_simps_right = funpow.simps(1) funpow_Suc_right
  1473 
  1474 text \<open>For code generation.\<close>
  1475 
  1476 definition funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
  1477   where funpow_code_def [code_abbrev]: "funpow = compow"
  1478 
  1479 lemma [code]:
  1480   "funpow (Suc n) f = f \<circ> funpow n f"
  1481   "funpow 0 f = id"
  1482   by (simp_all add: funpow_code_def)
  1483 
  1484 hide_const (open) funpow
  1485 
  1486 lemma funpow_add: "f ^^ (m + n) = f ^^ m \<circ> f ^^ n"
  1487   by (induct m) simp_all
  1488 
  1489 lemma funpow_mult: "(f ^^ m) ^^ n = f ^^ (m * n)"
  1490   for f :: "'a \<Rightarrow> 'a"
  1491   by (induct n) (simp_all add: funpow_add)
  1492 
  1493 lemma funpow_swap1: "f ((f ^^ n) x) = (f ^^ n) (f x)"
  1494 proof -
  1495   have "f ((f ^^ n) x) = (f ^^ (n + 1)) x" by simp
  1496   also have "\<dots>  = (f ^^ n \<circ> f ^^ 1) x" by (simp only: funpow_add)
  1497   also have "\<dots> = (f ^^ n) (f x)" by simp
  1498   finally show ?thesis .
  1499 qed
  1500 
  1501 lemma comp_funpow: "comp f ^^ n = comp (f ^^ n)"
  1502   for f :: "'a \<Rightarrow> 'a"
  1503   by (induct n) simp_all
  1504 
  1505 lemma Suc_funpow[simp]: "Suc ^^ n = (op + n)"
  1506   by (induct n) simp_all
  1507 
  1508 lemma id_funpow[simp]: "id ^^ n = id"
  1509   by (induct n) simp_all
  1510 
  1511 lemma funpow_mono: "mono f \<Longrightarrow> A \<le> B \<Longrightarrow> (f ^^ n) A \<le> (f ^^ n) B"
  1512   for f :: "'a \<Rightarrow> ('a::order)"
  1513   by (induct n arbitrary: A B)
  1514      (auto simp del: funpow.simps(2) simp add: funpow_Suc_right mono_def)
  1515 
  1516 lemma funpow_mono2:
  1517   assumes "mono f"
  1518     and "i \<le> j"
  1519     and "x \<le> y"
  1520     and "x \<le> f x"
  1521   shows "(f ^^ i) x \<le> (f ^^ j) y"
  1522   using assms(2,3)
  1523 proof (induct j arbitrary: y)
  1524   case 0
  1525   then show ?case by simp
  1526 next
  1527   case (Suc j)
  1528   show ?case
  1529   proof(cases "i = Suc j")
  1530     case True
  1531     with assms(1) Suc show ?thesis
  1532       by (simp del: funpow.simps add: funpow_simps_right monoD funpow_mono)
  1533   next
  1534     case False
  1535     with assms(1,4) Suc show ?thesis
  1536       by (simp del: funpow.simps add: funpow_simps_right le_eq_less_or_eq less_Suc_eq_le)
  1537         (simp add: Suc.hyps monoD order_subst1)
  1538   qed
  1539 qed
  1540 
  1541 
  1542 subsection \<open>Kleene iteration\<close>
  1543 
  1544 lemma Kleene_iter_lpfp:
  1545   fixes f :: "'a::order_bot \<Rightarrow> 'a"
  1546   assumes "mono f"
  1547     and "f p \<le> p"
  1548   shows "(f ^^ k) bot \<le> p"
  1549 proof (induct k)
  1550   case 0
  1551   show ?case by simp
  1552 next
  1553   case Suc
  1554   show ?case
  1555     using monoD[OF assms(1) Suc] assms(2) by simp
  1556 qed
  1557 
  1558 lemma lfp_Kleene_iter:
  1559   assumes "mono f"
  1560     and "(f ^^ Suc k) bot = (f ^^ k) bot"
  1561   shows "lfp f = (f ^^ k) bot"
  1562 proof (rule antisym)
  1563   show "lfp f \<le> (f ^^ k) bot"
  1564   proof (rule lfp_lowerbound)
  1565     show "f ((f ^^ k) bot) \<le> (f ^^ k) bot"
  1566       using assms(2) by simp
  1567   qed
  1568   show "(f ^^ k) bot \<le> lfp f"
  1569     using Kleene_iter_lpfp[OF assms(1)] lfp_unfold[OF assms(1)] by simp
  1570 qed
  1571 
  1572 lemma mono_pow: "mono f \<Longrightarrow> mono (f ^^ n)"
  1573   for f :: "'a \<Rightarrow> 'a::complete_lattice"
  1574   by (induct n) (auto simp: mono_def)
  1575 
  1576 lemma lfp_funpow:
  1577   assumes f: "mono f"
  1578   shows "lfp (f ^^ Suc n) = lfp f"
  1579 proof (rule antisym)
  1580   show "lfp f \<le> lfp (f ^^ Suc n)"
  1581   proof (rule lfp_lowerbound)
  1582     have "f (lfp (f ^^ Suc n)) = lfp (\<lambda>x. f ((f ^^ n) x))"
  1583       unfolding funpow_Suc_right by (simp add: lfp_rolling f mono_pow comp_def)
  1584     then show "f (lfp (f ^^ Suc n)) \<le> lfp (f ^^ Suc n)"
  1585       by (simp add: comp_def)
  1586   qed
  1587   have "(f ^^ n) (lfp f) = lfp f" for n
  1588     by (induct n) (auto intro: f lfp_fixpoint)
  1589   then show "lfp (f ^^ Suc n) \<le> lfp f"
  1590     by (intro lfp_lowerbound) (simp del: funpow.simps)
  1591 qed
  1592 
  1593 lemma gfp_funpow:
  1594   assumes f: "mono f"
  1595   shows "gfp (f ^^ Suc n) = gfp f"
  1596 proof (rule antisym)
  1597   show "gfp f \<ge> gfp (f ^^ Suc n)"
  1598   proof (rule gfp_upperbound)
  1599     have "f (gfp (f ^^ Suc n)) = gfp (\<lambda>x. f ((f ^^ n) x))"
  1600       unfolding funpow_Suc_right by (simp add: gfp_rolling f mono_pow comp_def)
  1601     then show "f (gfp (f ^^ Suc n)) \<ge> gfp (f ^^ Suc n)"
  1602       by (simp add: comp_def)
  1603   qed
  1604   have "(f ^^ n) (gfp f) = gfp f" for n
  1605     by (induct n) (auto intro: f gfp_fixpoint)
  1606   then show "gfp (f ^^ Suc n) \<ge> gfp f"
  1607     by (intro gfp_upperbound) (simp del: funpow.simps)
  1608 qed
  1609 
  1610 lemma Kleene_iter_gpfp:
  1611   fixes f :: "'a::order_top \<Rightarrow> 'a"
  1612   assumes "mono f"
  1613     and "p \<le> f p"
  1614   shows "p \<le> (f ^^ k) top"
  1615 proof (induct k)
  1616   case 0
  1617   show ?case by simp
  1618 next
  1619   case Suc
  1620   show ?case
  1621     using monoD[OF assms(1) Suc] assms(2) by simp
  1622 qed
  1623 
  1624 lemma gfp_Kleene_iter:
  1625   assumes "mono f"
  1626     and "(f ^^ Suc k) top = (f ^^ k) top"
  1627   shows "gfp f = (f ^^ k) top"
  1628     (is "?lhs = ?rhs")
  1629 proof (rule antisym)
  1630   have "?rhs \<le> f ?rhs"
  1631     using assms(2) by simp
  1632   then show "?rhs \<le> ?lhs"
  1633     by (rule gfp_upperbound)
  1634   show "?lhs \<le> ?rhs"
  1635     using Kleene_iter_gpfp[OF assms(1)] gfp_unfold[OF assms(1)] by simp
  1636 qed
  1637 
  1638 
  1639 subsection \<open>Embedding of the naturals into any \<open>semiring_1\<close>: @{term of_nat}\<close>
  1640 
  1641 context semiring_1
  1642 begin
  1643 
  1644 definition of_nat :: "nat \<Rightarrow> 'a"
  1645   where "of_nat n = (plus 1 ^^ n) 0"
  1646 
  1647 lemma of_nat_simps [simp]:
  1648   shows of_nat_0: "of_nat 0 = 0"
  1649     and of_nat_Suc: "of_nat (Suc m) = 1 + of_nat m"
  1650   by (simp_all add: of_nat_def)
  1651 
  1652 lemma of_nat_1 [simp]: "of_nat 1 = 1"
  1653   by (simp add: of_nat_def)
  1654 
  1655 lemma of_nat_add [simp]: "of_nat (m + n) = of_nat m + of_nat n"
  1656   by (induct m) (simp_all add: ac_simps)
  1657 
  1658 lemma of_nat_mult [simp]: "of_nat (m * n) = of_nat m * of_nat n"
  1659   by (induct m) (simp_all add: ac_simps distrib_right)
  1660 
  1661 lemma mult_of_nat_commute: "of_nat x * y = y * of_nat x"
  1662   by (induct x) (simp_all add: algebra_simps)
  1663 
  1664 primrec of_nat_aux :: "('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a"
  1665   where
  1666     "of_nat_aux inc 0 i = i"
  1667   | "of_nat_aux inc (Suc n) i = of_nat_aux inc n (inc i)" \<comment> \<open>tail recursive\<close>
  1668 
  1669 lemma of_nat_code: "of_nat n = of_nat_aux (\<lambda>i. i + 1) n 0"
  1670 proof (induct n)
  1671   case 0
  1672   then show ?case by simp
  1673 next
  1674   case (Suc n)
  1675   have "\<And>i. of_nat_aux (\<lambda>i. i + 1) n (i + 1) = of_nat_aux (\<lambda>i. i + 1) n i + 1"
  1676     by (induct n) simp_all
  1677   from this [of 0] have "of_nat_aux (\<lambda>i. i + 1) n 1 = of_nat_aux (\<lambda>i. i + 1) n 0 + 1"
  1678     by simp
  1679   with Suc show ?case
  1680     by (simp add: add.commute)
  1681 qed
  1682 
  1683 end
  1684 
  1685 declare of_nat_code [code]
  1686 
  1687 context ring_1
  1688 begin
  1689 
  1690 lemma of_nat_diff: "n \<le> m \<Longrightarrow> of_nat (m - n) = of_nat m - of_nat n"
  1691   by (simp add: algebra_simps of_nat_add [symmetric])
  1692 
  1693 end
  1694 
  1695 text \<open>Class for unital semirings with characteristic zero.
  1696  Includes non-ordered rings like the complex numbers.\<close>
  1697 
  1698 class semiring_char_0 = semiring_1 +
  1699   assumes inj_of_nat: "inj of_nat"
  1700 begin
  1701 
  1702 lemma of_nat_eq_iff [simp]: "of_nat m = of_nat n \<longleftrightarrow> m = n"
  1703   by (auto intro: inj_of_nat injD)
  1704 
  1705 text \<open>Special cases where either operand is zero\<close>
  1706 
  1707 lemma of_nat_0_eq_iff [simp]: "0 = of_nat n \<longleftrightarrow> 0 = n"
  1708   by (fact of_nat_eq_iff [of 0 n, unfolded of_nat_0])
  1709 
  1710 lemma of_nat_eq_0_iff [simp]: "of_nat m = 0 \<longleftrightarrow> m = 0"
  1711   by (fact of_nat_eq_iff [of m 0, unfolded of_nat_0])
  1712 
  1713 lemma of_nat_1_eq_iff [simp]: "1 = of_nat n \<longleftrightarrow> n=1"
  1714   using of_nat_eq_iff by fastforce
  1715 
  1716 lemma of_nat_eq_1_iff [simp]: "of_nat n = 1 \<longleftrightarrow> n=1"
  1717   using of_nat_eq_iff by fastforce
  1718 
  1719 lemma of_nat_neq_0 [simp]: "of_nat (Suc n) \<noteq> 0"
  1720   unfolding of_nat_eq_0_iff by simp
  1721 
  1722 lemma of_nat_0_neq [simp]: "0 \<noteq> of_nat (Suc n)"
  1723   unfolding of_nat_0_eq_iff by simp
  1724 
  1725 end
  1726 
  1727 class ring_char_0 = ring_1 + semiring_char_0
  1728 
  1729 context linordered_semidom
  1730 begin
  1731 
  1732 lemma of_nat_0_le_iff [simp]: "0 \<le> of_nat n"
  1733   by (induct n) simp_all
  1734 
  1735 lemma of_nat_less_0_iff [simp]: "\<not> of_nat m < 0"
  1736   by (simp add: not_less)
  1737 
  1738 lemma of_nat_less_iff [simp]: "of_nat m < of_nat n \<longleftrightarrow> m < n"
  1739   by (induct m n rule: diff_induct) (simp_all add: add_pos_nonneg)
  1740 
  1741 lemma of_nat_le_iff [simp]: "of_nat m \<le> of_nat n \<longleftrightarrow> m \<le> n"
  1742   by (simp add: not_less [symmetric] linorder_not_less [symmetric])
  1743 
  1744 lemma less_imp_of_nat_less: "m < n \<Longrightarrow> of_nat m < of_nat n"
  1745   by simp
  1746 
  1747 lemma of_nat_less_imp_less: "of_nat m < of_nat n \<Longrightarrow> m < n"
  1748   by simp
  1749 
  1750 text \<open>Every \<open>linordered_semidom\<close> has characteristic zero.\<close>
  1751 
  1752 subclass semiring_char_0
  1753   by standard (auto intro!: injI simp add: eq_iff)
  1754 
  1755 text \<open>Special cases where either operand is zero\<close>
  1756 
  1757 lemma of_nat_le_0_iff [simp]: "of_nat m \<le> 0 \<longleftrightarrow> m = 0"
  1758   by (rule of_nat_le_iff [of _ 0, simplified])
  1759 
  1760 lemma of_nat_0_less_iff [simp]: "0 < of_nat n \<longleftrightarrow> 0 < n"
  1761   by (rule of_nat_less_iff [of 0, simplified])
  1762 
  1763 end
  1764 
  1765 context linordered_idom
  1766 begin
  1767 
  1768 lemma abs_of_nat [simp]: "\<bar>of_nat n\<bar> = of_nat n"
  1769   unfolding abs_if by auto
  1770 
  1771 end
  1772 
  1773 lemma of_nat_id [simp]: "of_nat n = n"
  1774   by (induct n) simp_all
  1775 
  1776 lemma of_nat_eq_id [simp]: "of_nat = id"
  1777   by (auto simp add: fun_eq_iff)
  1778 
  1779 
  1780 subsection \<open>The set of natural numbers\<close>
  1781 
  1782 context semiring_1
  1783 begin
  1784 
  1785 definition Nats :: "'a set"  ("\<nat>")
  1786   where "\<nat> = range of_nat"
  1787 
  1788 lemma of_nat_in_Nats [simp]: "of_nat n \<in> \<nat>"
  1789   by (simp add: Nats_def)
  1790 
  1791 lemma Nats_0 [simp]: "0 \<in> \<nat>"
  1792   apply (simp add: Nats_def)
  1793   apply (rule range_eqI)
  1794   apply (rule of_nat_0 [symmetric])
  1795   done
  1796 
  1797 lemma Nats_1 [simp]: "1 \<in> \<nat>"
  1798   apply (simp add: Nats_def)
  1799   apply (rule range_eqI)
  1800   apply (rule of_nat_1 [symmetric])
  1801   done
  1802 
  1803 lemma Nats_add [simp]: "a \<in> \<nat> \<Longrightarrow> b \<in> \<nat> \<Longrightarrow> a + b \<in> \<nat>"
  1804   apply (auto simp add: Nats_def)
  1805   apply (rule range_eqI)
  1806   apply (rule of_nat_add [symmetric])
  1807   done
  1808 
  1809 lemma Nats_mult [simp]: "a \<in> \<nat> \<Longrightarrow> b \<in> \<nat> \<Longrightarrow> a * b \<in> \<nat>"
  1810   apply (auto simp add: Nats_def)
  1811   apply (rule range_eqI)
  1812   apply (rule of_nat_mult [symmetric])
  1813   done
  1814 
  1815 lemma Nats_cases [cases set: Nats]:
  1816   assumes "x \<in> \<nat>"
  1817   obtains (of_nat) n where "x = of_nat n"
  1818   unfolding Nats_def
  1819 proof -
  1820   from \<open>x \<in> \<nat>\<close> have "x \<in> range of_nat" unfolding Nats_def .
  1821   then obtain n where "x = of_nat n" ..
  1822   then show thesis ..
  1823 qed
  1824 
  1825 lemma Nats_induct [case_names of_nat, induct set: Nats]: "x \<in> \<nat> \<Longrightarrow> (\<And>n. P (of_nat n)) \<Longrightarrow> P x"
  1826   by (rule Nats_cases) auto
  1827 
  1828 end
  1829 
  1830 
  1831 subsection \<open>Further arithmetic facts concerning the natural numbers\<close>
  1832 
  1833 lemma subst_equals:
  1834   assumes "t = s" and "u = t"
  1835   shows "u = s"
  1836   using assms(2,1) by (rule trans)
  1837 
  1838 ML_file "Tools/nat_arith.ML"
  1839 
  1840 simproc_setup nateq_cancel_sums
  1841   ("(l::nat) + m = n" | "(l::nat) = m + n" | "Suc m = n" | "m = Suc n") =
  1842   \<open>fn phi => try o Nat_Arith.cancel_eq_conv\<close>
  1843 
  1844 simproc_setup natless_cancel_sums
  1845   ("(l::nat) + m < n" | "(l::nat) < m + n" | "Suc m < n" | "m < Suc n") =
  1846   \<open>fn phi => try o Nat_Arith.cancel_less_conv\<close>
  1847 
  1848 simproc_setup natle_cancel_sums
  1849   ("(l::nat) + m \<le> n" | "(l::nat) \<le> m + n" | "Suc m \<le> n" | "m \<le> Suc n") =
  1850   \<open>fn phi => try o Nat_Arith.cancel_le_conv\<close>
  1851 
  1852 simproc_setup natdiff_cancel_sums
  1853   ("(l::nat) + m - n" | "(l::nat) - (m + n)" | "Suc m - n" | "m - Suc n") =
  1854   \<open>fn phi => try o Nat_Arith.cancel_diff_conv\<close>
  1855 
  1856 context order
  1857 begin
  1858 
  1859 lemma lift_Suc_mono_le:
  1860   assumes mono: "\<And>n. f n \<le> f (Suc n)"
  1861     and "n \<le> n'"
  1862   shows "f n \<le> f n'"
  1863 proof (cases "n < n'")
  1864   case True
  1865   then show ?thesis
  1866     by (induct n n' rule: less_Suc_induct) (auto intro: mono)
  1867 next
  1868   case False
  1869   with \<open>n \<le> n'\<close> show ?thesis by auto
  1870 qed
  1871 
  1872 lemma lift_Suc_antimono_le:
  1873   assumes mono: "\<And>n. f n \<ge> f (Suc n)"
  1874     and "n \<le> n'"
  1875   shows "f n \<ge> f n'"
  1876 proof (cases "n < n'")
  1877   case True
  1878   then show ?thesis
  1879     by (induct n n' rule: less_Suc_induct) (auto intro: mono)
  1880 next
  1881   case False
  1882   with \<open>n \<le> n'\<close> show ?thesis by auto
  1883 qed
  1884 
  1885 lemma lift_Suc_mono_less:
  1886   assumes mono: "\<And>n. f n < f (Suc n)"
  1887     and "n < n'"
  1888   shows "f n < f n'"
  1889   using \<open>n < n'\<close> by (induct n n' rule: less_Suc_induct) (auto intro: mono)
  1890 
  1891 lemma lift_Suc_mono_less_iff: "(\<And>n. f n < f (Suc n)) \<Longrightarrow> f n < f m \<longleftrightarrow> n < m"
  1892   by (blast intro: less_asym' lift_Suc_mono_less [of f]
  1893     dest: linorder_not_less[THEN iffD1] le_eq_less_or_eq [THEN iffD1])
  1894 
  1895 end
  1896 
  1897 lemma mono_iff_le_Suc: "mono f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
  1898   unfolding mono_def by (auto intro: lift_Suc_mono_le [of f])
  1899 
  1900 lemma antimono_iff_le_Suc: "antimono f \<longleftrightarrow> (\<forall>n. f (Suc n) \<le> f n)"
  1901   unfolding antimono_def by (auto intro: lift_Suc_antimono_le [of f])
  1902 
  1903 lemma mono_nat_linear_lb:
  1904   fixes f :: "nat \<Rightarrow> nat"
  1905   assumes "\<And>m n. m < n \<Longrightarrow> f m < f n"
  1906   shows "f m + k \<le> f (m + k)"
  1907 proof (induct k)
  1908   case 0
  1909   then show ?case by simp
  1910 next
  1911   case (Suc k)
  1912   then have "Suc (f m + k) \<le> Suc (f (m + k))" by simp
  1913   also from assms [of "m + k" "Suc (m + k)"] have "Suc (f (m + k)) \<le> f (Suc (m + k))"
  1914     by (simp add: Suc_le_eq)
  1915   finally show ?case by simp
  1916 qed
  1917 
  1918 
  1919 text \<open>Subtraction laws, mostly by Clemens Ballarin\<close>
  1920 
  1921 lemma diff_less_mono:
  1922   fixes a b c :: nat
  1923   assumes "a < b" and "c \<le> a"
  1924   shows "a - c < b - c"
  1925 proof -
  1926   from assms obtain d e where "b = c + (d + e)" and "a = c + e" and "d > 0"
  1927     by (auto dest!: le_Suc_ex less_imp_Suc_add simp add: ac_simps)
  1928   then show ?thesis by simp
  1929 qed
  1930 
  1931 lemma less_diff_conv: "i < j - k \<longleftrightarrow> i + k < j"
  1932   for i j k :: nat
  1933   by (cases "k \<le> j") (auto simp add: not_le dest: less_imp_Suc_add le_Suc_ex)
  1934 
  1935 lemma less_diff_conv2: "k \<le> j \<Longrightarrow> j - k < i \<longleftrightarrow> j < i + k"
  1936   for j k i :: nat
  1937   by (auto dest: le_Suc_ex)
  1938 
  1939 lemma le_diff_conv: "j - k \<le> i \<longleftrightarrow> j \<le> i + k"
  1940   for j k i :: nat
  1941   by (cases "k \<le> j") (auto simp add: not_le dest!: less_imp_Suc_add le_Suc_ex)
  1942 
  1943 lemma diff_diff_cancel [simp]: "i \<le> n \<Longrightarrow> n - (n - i) = i"
  1944   for i n :: nat
  1945   by (auto dest: le_Suc_ex)
  1946 
  1947 lemma diff_less [simp]: "0 < n \<Longrightarrow> 0 < m \<Longrightarrow> m - n < m"
  1948   for i n :: nat
  1949   by (auto dest: less_imp_Suc_add)
  1950 
  1951 text \<open>Simplification of relational expressions involving subtraction\<close>
  1952 
  1953 lemma diff_diff_eq: "k \<le> m \<Longrightarrow> k \<le> n \<Longrightarrow> m - k - (n - k) = m - n"
  1954   for m n k :: nat
  1955   by (auto dest!: le_Suc_ex)
  1956 
  1957 hide_fact (open) diff_diff_eq
  1958 
  1959 lemma eq_diff_iff: "k \<le> m \<Longrightarrow> k \<le> n \<Longrightarrow> m - k = n - k \<longleftrightarrow> m = n"
  1960   for m n k :: nat
  1961   by (auto dest: le_Suc_ex)
  1962 
  1963 lemma less_diff_iff: "k \<le> m \<Longrightarrow> k \<le> n \<Longrightarrow> m - k < n - k \<longleftrightarrow> m < n"
  1964   for m n k :: nat
  1965   by (auto dest!: le_Suc_ex)
  1966 
  1967 lemma le_diff_iff: "k \<le> m \<Longrightarrow> k \<le> n \<Longrightarrow> m - k \<le> n - k \<longleftrightarrow> m \<le> n"
  1968   for m n k :: nat
  1969   by (auto dest!: le_Suc_ex)
  1970 
  1971 lemma le_diff_iff': "a \<le> c \<Longrightarrow> b \<le> c \<Longrightarrow> c - a \<le> c - b \<longleftrightarrow> b \<le> a"
  1972   for a b c :: nat
  1973   by (force dest: le_Suc_ex)
  1974 
  1975 
  1976 text \<open>(Anti)Monotonicity of subtraction -- by Stephan Merz\<close>
  1977 
  1978 lemma diff_le_mono: "m \<le> n \<Longrightarrow> m - l \<le> n - l"
  1979   for m n l :: nat
  1980   by (auto dest: less_imp_le less_imp_Suc_add split: nat_diff_split)
  1981 
  1982 lemma diff_le_mono2: "m \<le> n \<Longrightarrow> l - n \<le> l - m"
  1983   for m n l :: nat
  1984   by (auto dest: less_imp_le le_Suc_ex less_imp_Suc_add less_le_trans split: nat_diff_split)
  1985 
  1986 lemma diff_less_mono2: "m < n \<Longrightarrow> m < l \<Longrightarrow> l - n < l - m"
  1987   for m n l :: nat
  1988   by (auto dest: less_imp_Suc_add split: nat_diff_split)
  1989 
  1990 lemma diffs0_imp_equal: "m - n = 0 \<Longrightarrow> n - m = 0 \<Longrightarrow> m = n"
  1991   for m n :: nat
  1992   by (simp split: nat_diff_split)
  1993 
  1994 lemma min_diff: "min (m - i) (n - i) = min m n - i"
  1995   for m n i :: nat
  1996   by (cases m n rule: le_cases)
  1997     (auto simp add: not_le min.absorb1 min.absorb2 min.absorb_iff1 [symmetric] diff_le_mono)
  1998 
  1999 lemma inj_on_diff_nat:
  2000   fixes k :: nat
  2001   assumes "\<forall>n \<in> N. k \<le> n"
  2002   shows "inj_on (\<lambda>n. n - k) N"
  2003 proof (rule inj_onI)
  2004   fix x y
  2005   assume a: "x \<in> N" "y \<in> N" "x - k = y - k"
  2006   with assms have "x - k + k = y - k + k" by auto
  2007   with a assms show "x = y" by (auto simp add: eq_diff_iff)
  2008 qed
  2009 
  2010 text \<open>Rewriting to pull differences out\<close>
  2011 
  2012 lemma diff_diff_right [simp]: "k \<le> j \<Longrightarrow> i - (j - k) = i + k - j"
  2013   for i j k :: nat
  2014   by (fact diff_diff_right)
  2015 
  2016 lemma diff_Suc_diff_eq1 [simp]:
  2017   assumes "k \<le> j"
  2018   shows "i - Suc (j - k) = i + k - Suc j"
  2019 proof -
  2020   from assms have *: "Suc (j - k) = Suc j - k"
  2021     by (simp add: Suc_diff_le)
  2022   from assms have "k \<le> Suc j"
  2023     by (rule order_trans) simp
  2024   with diff_diff_right [of k "Suc j" i] * show ?thesis
  2025     by simp
  2026 qed
  2027 
  2028 lemma diff_Suc_diff_eq2 [simp]:
  2029   assumes "k \<le> j"
  2030   shows "Suc (j - k) - i = Suc j - (k + i)"
  2031 proof -
  2032   from assms obtain n where "j = k + n"
  2033     by (auto dest: le_Suc_ex)
  2034   moreover have "Suc n - i = (k + Suc n) - (k + i)"
  2035     using add_diff_cancel_left [of k "Suc n" i] by simp
  2036   ultimately show ?thesis by simp
  2037 qed
  2038 
  2039 lemma Suc_diff_Suc:
  2040   assumes "n < m"
  2041   shows "Suc (m - Suc n) = m - n"
  2042 proof -
  2043   from assms obtain q where "m = n + Suc q"
  2044     by (auto dest: less_imp_Suc_add)
  2045   moreover define r where "r = Suc q"
  2046   ultimately have "Suc (m - Suc n) = r" and "m = n + r"
  2047     by simp_all
  2048   then show ?thesis by simp
  2049 qed
  2050 
  2051 lemma one_less_mult: "Suc 0 < n \<Longrightarrow> Suc 0 < m \<Longrightarrow> Suc 0 < m * n"
  2052   using less_1_mult [of n m] by (simp add: ac_simps)
  2053 
  2054 lemma n_less_m_mult_n: "0 < n \<Longrightarrow> Suc 0 < m \<Longrightarrow> n < m * n"
  2055   using mult_strict_right_mono [of 1 m n] by simp
  2056 
  2057 lemma n_less_n_mult_m: "0 < n \<Longrightarrow> Suc 0 < m \<Longrightarrow> n < n * m"
  2058   using mult_strict_left_mono [of 1 m n] by simp
  2059 
  2060 
  2061 text \<open>Specialized induction principles that work "backwards":\<close>
  2062 
  2063 lemma inc_induct [consumes 1, case_names base step]:
  2064   assumes less: "i \<le> j"
  2065     and base: "P j"
  2066     and step: "\<And>n. i \<le> n \<Longrightarrow> n < j \<Longrightarrow> P (Suc n) \<Longrightarrow> P n"
  2067   shows "P i"
  2068   using less step
  2069 proof (induct "j - i" arbitrary: i)
  2070   case (0 i)
  2071   then have "i = j" by simp
  2072   with base show ?case by simp
  2073 next
  2074   case (Suc d n)
  2075   from Suc.hyps have "n \<noteq> j" by auto
  2076   with Suc have "n < j" by (simp add: less_le)
  2077   from \<open>Suc d = j - n\<close> have "d + 1 = j - n" by simp
  2078   then have "d + 1 - 1 = j - n - 1" by simp
  2079   then have "d = j - n - 1" by simp
  2080   then have "d = j - (n + 1)" by (simp add: diff_diff_eq)
  2081   then have "d = j - Suc n" by simp
  2082   moreover from \<open>n < j\<close> have "Suc n \<le> j" by (simp add: Suc_le_eq)
  2083   ultimately have "P (Suc n)"
  2084   proof (rule Suc.hyps)
  2085     fix q
  2086     assume "Suc n \<le> q"
  2087     then have "n \<le> q" by (simp add: Suc_le_eq less_imp_le)
  2088     moreover assume "q < j"
  2089     moreover assume "P (Suc q)"
  2090     ultimately show "P q" by (rule Suc.prems)
  2091   qed
  2092   with order_refl \<open>n < j\<close> show "P n" by (rule Suc.prems)
  2093 qed
  2094 
  2095 lemma strict_inc_induct [consumes 1, case_names base step]:
  2096   assumes less: "i < j"
  2097     and base: "\<And>i. j = Suc i \<Longrightarrow> P i"
  2098     and step: "\<And>i. i < j \<Longrightarrow> P (Suc i) \<Longrightarrow> P i"
  2099   shows "P i"
  2100 using less proof (induct "j - i - 1" arbitrary: i)
  2101   case (0 i)
  2102   from \<open>i < j\<close> obtain n where "j = i + n" and "n > 0"
  2103     by (auto dest!: less_imp_Suc_add)
  2104   with 0 have "j = Suc i"
  2105     by (auto intro: order_antisym simp add: Suc_le_eq)
  2106   with base show ?case by simp
  2107 next
  2108   case (Suc d i)
  2109   from \<open>Suc d = j - i - 1\<close> have *: "Suc d = j - Suc i"
  2110     by (simp add: diff_diff_add)
  2111   then have "Suc d - 1 = j - Suc i - 1" by simp
  2112   then have "d = j - Suc i - 1" by simp
  2113   moreover from * have "j - Suc i \<noteq> 0" by auto
  2114   then have "Suc i < j" by (simp add: not_le)
  2115   ultimately have "P (Suc i)" by (rule Suc.hyps)
  2116   with \<open>i < j\<close> show "P i" by (rule step)
  2117 qed
  2118 
  2119 lemma zero_induct_lemma: "P k \<Longrightarrow> (\<And>n. P (Suc n) \<Longrightarrow> P n) \<Longrightarrow> P (k - i)"
  2120   using inc_induct[of "k - i" k P, simplified] by blast
  2121 
  2122 lemma zero_induct: "P k \<Longrightarrow> (\<And>n. P (Suc n) \<Longrightarrow> P n) \<Longrightarrow> P 0"
  2123   using inc_induct[of 0 k P] by blast
  2124 
  2125 text \<open>Further induction rule similar to @{thm inc_induct}.\<close>
  2126 
  2127 lemma dec_induct [consumes 1, case_names base step]:
  2128   "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"
  2129 proof (induct j arbitrary: i)
  2130   case 0
  2131   then show ?case by simp
  2132 next
  2133   case (Suc j)
  2134   from Suc.prems consider "i \<le> j" | "i = Suc j"
  2135     by (auto simp add: le_Suc_eq)
  2136   then show ?case
  2137   proof cases
  2138     case 1
  2139     moreover have "j < Suc j" by simp
  2140     moreover have "P j" using \<open>i \<le> j\<close> \<open>P i\<close>
  2141     proof (rule Suc.hyps)
  2142       fix q
  2143       assume "i \<le> q"
  2144       moreover assume "q < j" then have "q < Suc j"
  2145         by (simp add: less_Suc_eq)
  2146       moreover assume "P q"
  2147       ultimately show "P (Suc q)" by (rule Suc.prems)
  2148     qed
  2149     ultimately show "P (Suc j)" by (rule Suc.prems)
  2150   next
  2151     case 2
  2152     with \<open>P i\<close> show "P (Suc j)" by simp
  2153   qed
  2154 qed
  2155 
  2156 lemma transitive_stepwise_le:
  2157   assumes "m \<le> n" "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" and "\<And>n. R n (Suc n)"
  2158   shows "R m n"
  2159 using \<open>m \<le> n\<close>  
  2160   by (induction rule: dec_induct) (use assms in blast)+
  2161 
  2162 
  2163 subsubsection \<open>Greatest operator\<close>
  2164 
  2165 lemma ex_has_greatest_nat:
  2166   "P (k::nat) \<Longrightarrow> \<forall>y. P y \<longrightarrow> y \<le> b \<Longrightarrow> \<exists>x. P x \<and> (\<forall>y. P y \<longrightarrow> y \<le> x)"
  2167 proof (induction "b-k" arbitrary: b k rule: less_induct)
  2168   case less
  2169   show ?case
  2170   proof cases
  2171     assume "\<exists>n>k. P n"
  2172     then obtain n where "n>k" "P n" by blast
  2173     have "n \<le> b" using \<open>P n\<close> less.prems(2) by auto
  2174     hence "b-n < b-k"
  2175       by(rule diff_less_mono2[OF \<open>k<n\<close> less_le_trans[OF \<open>k<n\<close>]])
  2176     from less.hyps[OF this \<open>P n\<close> less.prems(2)]
  2177     show ?thesis .
  2178   next
  2179     assume "\<not> (\<exists>n>k. P n)"
  2180     hence "\<forall>y. P y \<longrightarrow> y \<le> k" by (auto simp: not_less)
  2181     thus ?thesis using less.prems(1) by auto
  2182   qed
  2183 qed
  2184 
  2185 lemma GreatestI_nat:
  2186   "\<lbrakk> P(k::nat); \<forall>y. P y \<longrightarrow> y \<le> b \<rbrakk> \<Longrightarrow> P (Greatest P)"
  2187 apply(drule (1) ex_has_greatest_nat)
  2188 using GreatestI2_order by auto
  2189 
  2190 lemma Greatest_le_nat:
  2191   "\<lbrakk> P(k::nat);  \<forall>y. P y \<longrightarrow> y \<le> b \<rbrakk> \<Longrightarrow> k \<le> (Greatest P)"
  2192 apply(frule (1) ex_has_greatest_nat)
  2193 using GreatestI2_order[where P=P and Q=\<open>\<lambda>x. k \<le> x\<close>] by auto
  2194 
  2195 lemma GreatestI_ex_nat:
  2196   "\<lbrakk> \<exists>k::nat. P k;  \<forall>y. P y \<longrightarrow> y \<le> b \<rbrakk> \<Longrightarrow> P (Greatest P)"
  2197 apply (erule exE)
  2198 apply (erule (1) GreatestI_nat)
  2199 done
  2200 
  2201 
  2202 subsection \<open>Monotonicity of \<open>funpow\<close>\<close>
  2203 
  2204 lemma funpow_increasing: "m \<le> n \<Longrightarrow> mono f \<Longrightarrow> (f ^^ n) \<top> \<le> (f ^^ m) \<top>"
  2205   for f :: "'a::{lattice,order_top} \<Rightarrow> 'a"
  2206   by (induct rule: inc_induct)
  2207     (auto simp del: funpow.simps(2) simp add: funpow_Suc_right
  2208       intro: order_trans[OF _ funpow_mono])
  2209 
  2210 lemma funpow_decreasing: "m \<le> n \<Longrightarrow> mono f \<Longrightarrow> (f ^^ m) \<bottom> \<le> (f ^^ n) \<bottom>"
  2211   for f :: "'a::{lattice,order_bot} \<Rightarrow> 'a"
  2212   by (induct rule: dec_induct)
  2213     (auto simp del: funpow.simps(2) simp add: funpow_Suc_right
  2214       intro: order_trans[OF _ funpow_mono])
  2215 
  2216 lemma mono_funpow: "mono Q \<Longrightarrow> mono (\<lambda>i. (Q ^^ i) \<bottom>)"
  2217   for Q :: "'a::{lattice,order_bot} \<Rightarrow> 'a"
  2218   by (auto intro!: funpow_decreasing simp: mono_def)
  2219 
  2220 lemma antimono_funpow: "mono Q \<Longrightarrow> antimono (\<lambda>i. (Q ^^ i) \<top>)"
  2221   for Q :: "'a::{lattice,order_top} \<Rightarrow> 'a"
  2222   by (auto intro!: funpow_increasing simp: antimono_def)
  2223 
  2224 
  2225 subsection \<open>The divides relation on @{typ nat}\<close>
  2226 
  2227 lemma dvd_1_left [iff]: "Suc 0 dvd k"
  2228   by (simp add: dvd_def)
  2229 
  2230 lemma dvd_1_iff_1 [simp]: "m dvd Suc 0 \<longleftrightarrow> m = Suc 0"
  2231   by (simp add: dvd_def)
  2232 
  2233 lemma nat_dvd_1_iff_1 [simp]: "m dvd 1 \<longleftrightarrow> m = 1"
  2234   for m :: nat
  2235   by (simp add: dvd_def)
  2236 
  2237 lemma dvd_antisym: "m dvd n \<Longrightarrow> n dvd m \<Longrightarrow> m = n"
  2238   for m n :: nat
  2239   unfolding dvd_def by (force dest: mult_eq_self_implies_10 simp add: mult.assoc)
  2240 
  2241 lemma dvd_diff_nat [simp]: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m - n)"
  2242   for k m n :: nat
  2243   unfolding dvd_def by (blast intro: right_diff_distrib' [symmetric])
  2244 
  2245 lemma dvd_diffD: "k dvd m - n \<Longrightarrow> k dvd n \<Longrightarrow> n \<le> m \<Longrightarrow> k dvd m"
  2246   for k m n :: nat
  2247   apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
  2248   apply (blast intro: dvd_add)
  2249   done
  2250 
  2251 lemma dvd_diffD1: "k dvd m - n \<Longrightarrow> k dvd m \<Longrightarrow> n \<le> m \<Longrightarrow> k dvd n"
  2252   for k m n :: nat
  2253   by (drule_tac m = m in dvd_diff_nat) auto
  2254 
  2255 lemma dvd_mult_cancel:
  2256   fixes m n k :: nat
  2257   assumes "k * m dvd k * n" and "0 < k"
  2258   shows "m dvd n"
  2259 proof -
  2260   from assms(1) obtain q where "k * n = (k * m) * q" ..
  2261   then have "k * n = k * (m * q)" by (simp add: ac_simps)
  2262   with \<open>0 < k\<close> have "n = m * q" by (auto simp add: mult_left_cancel)
  2263   then show ?thesis ..
  2264 qed
  2265 
  2266 lemma dvd_mult_cancel1: "0 < m \<Longrightarrow> m * n dvd m \<longleftrightarrow> n = 1"
  2267   for m n :: nat
  2268   apply auto
  2269   apply (subgoal_tac "m * n dvd m * 1")
  2270    apply (drule dvd_mult_cancel)
  2271     apply auto
  2272   done
  2273 
  2274 lemma dvd_mult_cancel2: "0 < m \<Longrightarrow> n * m dvd m \<longleftrightarrow> n = 1"
  2275   for m n :: nat
  2276   using dvd_mult_cancel1 [of m n] by (simp add: ac_simps)
  2277 
  2278 lemma dvd_imp_le: "k dvd n \<Longrightarrow> 0 < n \<Longrightarrow> k \<le> n"
  2279   for k n :: nat
  2280   by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
  2281 
  2282 lemma nat_dvd_not_less: "0 < m \<Longrightarrow> m < n \<Longrightarrow> \<not> n dvd m"
  2283   for m n :: nat
  2284   by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
  2285 
  2286 lemma less_eq_dvd_minus:
  2287   fixes m n :: nat
  2288   assumes "m \<le> n"
  2289   shows "m dvd n \<longleftrightarrow> m dvd n - m"
  2290 proof -
  2291   from assms have "n = m + (n - m)" by simp
  2292   then obtain q where "n = m + q" ..
  2293   then show ?thesis by (simp add: add.commute [of m])
  2294 qed
  2295 
  2296 lemma dvd_minus_self: "m dvd n - m \<longleftrightarrow> n < m \<or> m dvd n"
  2297   for m n :: nat
  2298   by (cases "n < m") (auto elim!: dvdE simp add: not_less le_imp_diff_is_add dest: less_imp_le)
  2299 
  2300 lemma dvd_minus_add:
  2301   fixes m n q r :: nat
  2302   assumes "q \<le> n" "q \<le> r * m"
  2303   shows "m dvd n - q \<longleftrightarrow> m dvd n + (r * m - q)"
  2304 proof -
  2305   have "m dvd n - q \<longleftrightarrow> m dvd r * m + (n - q)"
  2306     using dvd_add_times_triv_left_iff [of m r] by simp
  2307   also from assms have "\<dots> \<longleftrightarrow> m dvd r * m + n - q" by simp
  2308   also from assms have "\<dots> \<longleftrightarrow> m dvd (r * m - q) + n" by simp
  2309   also have "\<dots> \<longleftrightarrow> m dvd n + (r * m - q)" by (simp add: add.commute)
  2310   finally show ?thesis .
  2311 qed
  2312 
  2313 
  2314 subsection \<open>Aliasses\<close>
  2315 
  2316 lemma nat_mult_1: "1 * n = n"
  2317   for n :: nat
  2318   by (fact mult_1_left)
  2319 
  2320 lemma nat_mult_1_right: "n * 1 = n"
  2321   for n :: nat
  2322   by (fact mult_1_right)
  2323 
  2324 lemma nat_add_left_cancel: "k + m = k + n \<longleftrightarrow> m = n"
  2325   for k m n :: nat
  2326   by (fact add_left_cancel)
  2327 
  2328 lemma nat_add_right_cancel: "m + k = n + k \<longleftrightarrow> m = n"
  2329   for k m n :: nat
  2330   by (fact add_right_cancel)
  2331 
  2332 lemma diff_mult_distrib: "(m - n) * k = (m * k) - (n * k)"
  2333   for k m n :: nat
  2334   by (fact left_diff_distrib')
  2335 
  2336 lemma diff_mult_distrib2: "k * (m - n) = (k * m) - (k * n)"
  2337   for k m n :: nat
  2338   by (fact right_diff_distrib')
  2339 
  2340 lemma le_add_diff: "k \<le> n \<Longrightarrow> m \<le> n + m - k"
  2341   for k m n :: nat
  2342   by (fact le_add_diff)  (* FIXME delete *)
  2343 
  2344 lemma le_diff_conv2: "k \<le> j \<Longrightarrow> (i \<le> j - k) = (i + k \<le> j)"
  2345   for i j k :: nat
  2346   by (fact le_diff_conv2) (* FIXME delete *)
  2347 
  2348 lemma diff_self_eq_0 [simp]: "m - m = 0"
  2349   for m :: nat
  2350   by (fact diff_cancel)
  2351 
  2352 lemma diff_diff_left [simp]: "i - j - k = i - (j + k)"
  2353   for i j k :: nat
  2354   by (fact diff_diff_add)
  2355 
  2356 lemma diff_commute: "i - j - k = i - k - j"
  2357   for i j k :: nat
  2358   by (fact diff_right_commute)
  2359 
  2360 lemma diff_add_inverse: "(n + m) - n = m"
  2361   for m n :: nat
  2362   by (fact add_diff_cancel_left')
  2363 
  2364 lemma diff_add_inverse2: "(m + n) - n = m"
  2365   for m n :: nat
  2366   by (fact add_diff_cancel_right')
  2367 
  2368 lemma diff_cancel: "(k + m) - (k + n) = m - n"
  2369   for k m n :: nat
  2370   by (fact add_diff_cancel_left)
  2371 
  2372 lemma diff_cancel2: "(m + k) - (n + k) = m - n"
  2373   for k m n :: nat
  2374   by (fact add_diff_cancel_right)
  2375 
  2376 lemma diff_add_0: "n - (n + m) = 0"
  2377   for m n :: nat
  2378   by (fact diff_add_zero)
  2379 
  2380 lemma add_mult_distrib2: "k * (m + n) = (k * m) + (k * n)"
  2381   for k m n :: nat
  2382   by (fact distrib_left)
  2383 
  2384 lemmas nat_distrib =
  2385   add_mult_distrib distrib_left diff_mult_distrib diff_mult_distrib2
  2386 
  2387 
  2388 subsection \<open>Size of a datatype value\<close>
  2389 
  2390 class size =
  2391   fixes size :: "'a \<Rightarrow> nat" \<comment> \<open>see further theory \<open>Wellfounded\<close>\<close>
  2392 
  2393 instantiation nat :: size
  2394 begin
  2395 
  2396 definition size_nat where [simp, code]: "size (n::nat) = n"
  2397 
  2398 instance ..
  2399 
  2400 end
  2401 
  2402 
  2403 subsection \<open>Code module namespace\<close>
  2404 
  2405 code_identifier
  2406   code_module Nat \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
  2407 
  2408 hide_const (open) of_nat_aux
  2409 
  2410 end