src/HOL/Nat.thy
author blanchet
Wed Jan 11 16:43:31 2017 +0100 (2017-01-11)
changeset 64876 65a247444100
parent 64849 766db3539859
child 65583 8d53b3bebab4
permissions -rw-r--r--
generalized types in 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 = [], is_new_datatype = K (K true), basic_lfp_sugars_of = basic_lfp_sugars_of,
   167      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 subsubsection \<open>Monotonicity of Addition\<close>
   768 
   769 lemma Suc_pred [simp]: "n > 0 \<Longrightarrow> Suc (n - Suc 0) = n"
   770   by (simp add: diff_Suc split: nat.split)
   771 
   772 lemma Suc_diff_1 [simp]: "0 < n \<Longrightarrow> Suc (n - 1) = n"
   773   unfolding One_nat_def by (rule Suc_pred)
   774 
   775 lemma nat_add_left_cancel_le [simp]: "k + m \<le> k + n \<longleftrightarrow> m \<le> n"
   776   for k m n :: nat
   777   by (induct k) simp_all
   778 
   779 lemma nat_add_left_cancel_less [simp]: "k + m < k + n \<longleftrightarrow> m < n"
   780   for k m n :: nat
   781   by (induct k) simp_all
   782 
   783 lemma add_gr_0 [iff]: "m + n > 0 \<longleftrightarrow> m > 0 \<or> n > 0"
   784   for m n :: nat
   785   by (auto dest: gr0_implies_Suc)
   786 
   787 text \<open>strict, in 1st argument\<close>
   788 lemma add_less_mono1: "i < j \<Longrightarrow> i + k < j + k"
   789   for i j k :: nat
   790   by (induct k) simp_all
   791 
   792 text \<open>strict, in both arguments\<close>
   793 lemma add_less_mono: "i < j \<Longrightarrow> k < l \<Longrightarrow> i + k < j + l"
   794   for i j k l :: nat
   795   apply (rule add_less_mono1 [THEN less_trans], assumption+)
   796   apply (induct j)
   797    apply simp_all
   798   done
   799 
   800 text \<open>Deleted \<open>less_natE\<close>; use \<open>less_imp_Suc_add RS exE\<close>\<close>
   801 lemma less_imp_Suc_add: "m < n \<Longrightarrow> \<exists>k. n = Suc (m + k)"
   802 proof (induct n)
   803   case 0
   804   then show ?case by simp
   805 next
   806   case Suc
   807   then show ?case
   808     by (simp add: order_le_less)
   809       (blast elim!: less_SucE intro!: Nat.add_0_right [symmetric] add_Suc_right [symmetric])
   810 qed
   811 
   812 lemma le_Suc_ex: "k \<le> l \<Longrightarrow> (\<exists>n. l = k + n)"
   813   for k l :: nat
   814   by (auto simp: less_Suc_eq_le[symmetric] dest: less_imp_Suc_add)
   815 
   816 text \<open>strict, in 1st argument; proof is by induction on \<open>k > 0\<close>\<close>
   817 lemma mult_less_mono2:
   818   fixes i j :: nat
   819   assumes "i < j" and "0 < k"
   820   shows "k * i < k * j"
   821   using \<open>0 < k\<close>
   822 proof (induct k)
   823   case 0
   824   then show ?case by simp
   825 next
   826   case (Suc k)
   827   with \<open>i < j\<close> show ?case
   828     by (cases k) (simp_all add: add_less_mono)
   829 qed
   830 
   831 text \<open>Addition is the inverse of subtraction:
   832   if @{term "n \<le> m"} then @{term "n + (m - n) = m"}.\<close>
   833 lemma add_diff_inverse_nat: "\<not> m < n \<Longrightarrow> n + (m - n) = m"
   834   for m n :: nat
   835   by (induct m n rule: diff_induct) simp_all
   836 
   837 lemma nat_le_iff_add: "m \<le> n \<longleftrightarrow> (\<exists>k. n = m + k)"
   838   for m n :: nat
   839   using nat_add_left_cancel_le[of m 0] by (auto dest: le_Suc_ex)
   840 
   841 text \<open>The naturals form an ordered \<open>semidom\<close> and a \<open>dioid\<close>.\<close>
   842 
   843 instance nat :: linordered_semidom
   844 proof
   845   fix m n q :: nat
   846   show "0 < (1::nat)"
   847     by simp
   848   show "m \<le> n \<Longrightarrow> q + m \<le> q + n"
   849     by simp
   850   show "m < n \<Longrightarrow> 0 < q \<Longrightarrow> q * m < q * n"
   851     by (simp add: mult_less_mono2)
   852   show "m \<noteq> 0 \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> m * n \<noteq> 0"
   853     by simp
   854   show "n \<le> m \<Longrightarrow> (m - n) + n = m"
   855     by (simp add: add_diff_inverse_nat add.commute linorder_not_less)
   856 qed
   857 
   858 instance nat :: dioid
   859   by standard (rule nat_le_iff_add)
   860 
   861 declare le0[simp del] \<comment> \<open>This is now @{thm zero_le}\<close>
   862 declare le_0_eq[simp del] \<comment> \<open>This is now @{thm le_zero_eq}\<close>
   863 declare not_less0[simp del] \<comment> \<open>This is now @{thm not_less_zero}\<close>
   864 declare not_gr0[simp del] \<comment> \<open>This is now @{thm not_gr_zero}\<close>
   865 
   866 instance nat :: ordered_cancel_comm_monoid_add ..
   867 instance nat :: ordered_cancel_comm_monoid_diff ..
   868 
   869 
   870 subsubsection \<open>@{term min} and @{term max}\<close>
   871 
   872 lemma mono_Suc: "mono Suc"
   873   by (rule monoI) simp
   874 
   875 lemma min_0L [simp]: "min 0 n = 0"
   876   for n :: nat
   877   by (rule min_absorb1) simp
   878 
   879 lemma min_0R [simp]: "min n 0 = 0"
   880   for n :: nat
   881   by (rule min_absorb2) simp
   882 
   883 lemma min_Suc_Suc [simp]: "min (Suc m) (Suc n) = Suc (min m n)"
   884   by (simp add: mono_Suc min_of_mono)
   885 
   886 lemma min_Suc1: "min (Suc n) m = (case m of 0 \<Rightarrow> 0 | Suc m' \<Rightarrow> Suc(min n m'))"
   887   by (simp split: nat.split)
   888 
   889 lemma min_Suc2: "min m (Suc n) = (case m of 0 \<Rightarrow> 0 | Suc m' \<Rightarrow> Suc(min m' n))"
   890   by (simp split: nat.split)
   891 
   892 lemma max_0L [simp]: "max 0 n = n"
   893   for n :: nat
   894   by (rule max_absorb2) simp
   895 
   896 lemma max_0R [simp]: "max n 0 = n"
   897   for n :: nat
   898   by (rule max_absorb1) simp
   899 
   900 lemma max_Suc_Suc [simp]: "max (Suc m) (Suc n) = Suc (max m n)"
   901   by (simp add: mono_Suc max_of_mono)
   902 
   903 lemma max_Suc1: "max (Suc n) m = (case m of 0 \<Rightarrow> Suc n | Suc m' \<Rightarrow> Suc (max n m'))"
   904   by (simp split: nat.split)
   905 
   906 lemma max_Suc2: "max m (Suc n) = (case m of 0 \<Rightarrow> Suc n | Suc m' \<Rightarrow> Suc (max m' n))"
   907   by (simp split: nat.split)
   908 
   909 lemma nat_mult_min_left: "min m n * q = min (m * q) (n * q)"
   910   for m n q :: nat
   911   by (simp add: min_def not_le)
   912     (auto dest: mult_right_le_imp_le mult_right_less_imp_less le_less_trans)
   913 
   914 lemma nat_mult_min_right: "m * min n q = min (m * n) (m * q)"
   915   for m n q :: nat
   916   by (simp add: min_def not_le)
   917     (auto dest: mult_left_le_imp_le mult_left_less_imp_less le_less_trans)
   918 
   919 lemma nat_add_max_left: "max m n + q = max (m + q) (n + q)"
   920   for m n q :: nat
   921   by (simp add: max_def)
   922 
   923 lemma nat_add_max_right: "m + max n q = max (m + n) (m + q)"
   924   for m n q :: nat
   925   by (simp add: max_def)
   926 
   927 lemma nat_mult_max_left: "max m n * q = max (m * q) (n * q)"
   928   for m n q :: nat
   929   by (simp add: max_def not_le)
   930     (auto dest: mult_right_le_imp_le mult_right_less_imp_less le_less_trans)
   931 
   932 lemma nat_mult_max_right: "m * max n q = max (m * n) (m * q)"
   933   for m n q :: nat
   934   by (simp add: max_def not_le)
   935     (auto dest: mult_left_le_imp_le mult_left_less_imp_less le_less_trans)
   936 
   937 
   938 subsubsection \<open>Additional theorems about @{term "op \<le>"}\<close>
   939 
   940 text \<open>Complete induction, aka course-of-values induction\<close>
   941 
   942 instance nat :: wellorder
   943 proof
   944   fix P and n :: nat
   945   assume step: "(\<And>m. m < n \<Longrightarrow> P m) \<Longrightarrow> P n" for n :: nat
   946   have "\<And>q. q \<le> n \<Longrightarrow> P q"
   947   proof (induct n)
   948     case (0 n)
   949     have "P 0" by (rule step) auto
   950     with 0 show ?case by auto
   951   next
   952     case (Suc m n)
   953     then have "n \<le> m \<or> n = Suc m"
   954       by (simp add: le_Suc_eq)
   955     then show ?case
   956     proof
   957       assume "n \<le> m"
   958       then show "P n" by (rule Suc(1))
   959     next
   960       assume n: "n = Suc m"
   961       show "P n" by (rule step) (rule Suc(1), simp add: n le_simps)
   962     qed
   963   qed
   964   then show "P n" by auto
   965 qed
   966 
   967 
   968 lemma Least_eq_0[simp]: "P 0 \<Longrightarrow> Least P = 0"
   969   for P :: "nat \<Rightarrow> bool"
   970   by (rule Least_equality[OF _ le0])
   971 
   972 lemma Least_Suc: "P n \<Longrightarrow> \<not> P 0 \<Longrightarrow> (LEAST n. P n) = Suc (LEAST m. P (Suc m))"
   973   apply (cases n)
   974    apply auto
   975   apply (frule LeastI)
   976   apply (drule_tac P = "\<lambda>x. P (Suc x)" in LeastI)
   977   apply (subgoal_tac " (LEAST x. P x) \<le> Suc (LEAST x. P (Suc x))")
   978    apply (erule_tac [2] Least_le)
   979   apply (cases "LEAST x. P x")
   980    apply auto
   981   apply (drule_tac P = "\<lambda>x. P (Suc x)" in Least_le)
   982   apply (blast intro: order_antisym)
   983   done
   984 
   985 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)"
   986   by (erule (1) Least_Suc [THEN ssubst]) simp
   987 
   988 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"
   989   for P :: "nat \<Rightarrow> bool"
   990   apply (cases n)
   991    apply blast
   992   apply (rule_tac x="LEAST k. P k" in exI)
   993   apply (blast intro: Least_le dest: not_less_Least intro: LeastI_ex)
   994   done
   995 
   996 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)"
   997   for P :: "nat \<Rightarrow> bool"
   998   apply (cases n)
   999    apply blast
  1000   apply (frule (1) ex_least_nat_le)
  1001   apply (erule exE)
  1002   apply (case_tac k)
  1003    apply simp
  1004   apply (rename_tac k1)
  1005   apply (rule_tac x=k1 in exI)
  1006   apply (auto simp add: less_eq_Suc_le)
  1007   done
  1008 
  1009 lemma nat_less_induct:
  1010   fixes P :: "nat \<Rightarrow> bool"
  1011   assumes "\<And>n. \<forall>m. m < n \<longrightarrow> P m \<Longrightarrow> P n"
  1012   shows "P n"
  1013   using assms less_induct by blast
  1014 
  1015 lemma measure_induct_rule [case_names less]:
  1016   fixes f :: "'a \<Rightarrow> 'b::wellorder"
  1017   assumes step: "\<And>x. (\<And>y. f y < f x \<Longrightarrow> P y) \<Longrightarrow> P x"
  1018   shows "P a"
  1019   by (induct m \<equiv> "f a" arbitrary: a rule: less_induct) (auto intro: step)
  1020 
  1021 text \<open>old style induction rules:\<close>
  1022 lemma measure_induct:
  1023   fixes f :: "'a \<Rightarrow> 'b::wellorder"
  1024   shows "(\<And>x. \<forall>y. f y < f x \<longrightarrow> P y \<Longrightarrow> P x) \<Longrightarrow> P a"
  1025   by (rule measure_induct_rule [of f P a]) iprover
  1026 
  1027 lemma full_nat_induct:
  1028   assumes step: "\<And>n. (\<forall>m. Suc m \<le> n \<longrightarrow> P m) \<Longrightarrow> P n"
  1029   shows "P n"
  1030   by (rule less_induct) (auto intro: step simp:le_simps)
  1031 
  1032 text\<open>An induction rule for establishing binary relations\<close>
  1033 lemma less_Suc_induct [consumes 1]:
  1034   assumes less: "i < j"
  1035     and step: "\<And>i. P i (Suc i)"
  1036     and trans: "\<And>i j k. i < j \<Longrightarrow> j < k \<Longrightarrow> P i j \<Longrightarrow> P j k \<Longrightarrow> P i k"
  1037   shows "P i j"
  1038 proof -
  1039   from less obtain k where j: "j = Suc (i + k)"
  1040     by (auto dest: less_imp_Suc_add)
  1041   have "P i (Suc (i + k))"
  1042   proof (induct k)
  1043     case 0
  1044     show ?case by (simp add: step)
  1045   next
  1046     case (Suc k)
  1047     have "0 + i < Suc k + i" by (rule add_less_mono1) simp
  1048     then have "i < Suc (i + k)" by (simp add: add.commute)
  1049     from trans[OF this lessI Suc step]
  1050     show ?case by simp
  1051   qed
  1052   then show "P i j" by (simp add: j)
  1053 qed
  1054 
  1055 text \<open>
  1056   The method of infinite descent, frequently used in number theory.
  1057   Provided by Roelof Oosterhuis.
  1058   \<open>P n\<close> is true for all natural numbers if
  1059   \<^item> case ``0'': given \<open>n = 0\<close> prove \<open>P n\<close>
  1060   \<^item> case ``smaller'': given \<open>n > 0\<close> and \<open>\<not> P n\<close> prove there exists
  1061     a smaller natural number \<open>m\<close> such that \<open>\<not> P m\<close>.
  1062 \<close>
  1063 
  1064 lemma infinite_descent: "(\<And>n. \<not> P n \<Longrightarrow> \<exists>m<n. \<not> P m) \<Longrightarrow> P n" for P :: "nat \<Rightarrow> bool"
  1065   \<comment> \<open>compact version without explicit base case\<close>
  1066   by (induct n rule: less_induct) auto
  1067 
  1068 lemma infinite_descent0 [case_names 0 smaller]:
  1069   fixes P :: "nat \<Rightarrow> bool"
  1070   assumes "P 0"
  1071     and "\<And>n. n > 0 \<Longrightarrow> \<not> P n \<Longrightarrow> \<exists>m. m < n \<and> \<not> P m"
  1072   shows "P n"
  1073   apply (rule infinite_descent)
  1074   using assms
  1075   apply (case_tac "n > 0")
  1076    apply auto
  1077   done
  1078 
  1079 text \<open>
  1080   Infinite descent using a mapping to \<open>nat\<close>:
  1081   \<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
  1082   \<^item> case ``0'': given \<open>V x = 0\<close> prove \<open>P x\<close>
  1083   \<^item> ``smaller'': given \<open>V x > 0\<close> and \<open>\<not> P x\<close> prove
  1084   there exists a \<open>y \<in> D\<close> such that \<open>V y < V x\<close> and \<open>\<not> P y\<close>.
  1085 \<close>
  1086 corollary infinite_descent0_measure [case_names 0 smaller]:
  1087   fixes V :: "'a \<Rightarrow> nat"
  1088   assumes 1: "\<And>x. V x = 0 \<Longrightarrow> P x"
  1089     and 2: "\<And>x. V x > 0 \<Longrightarrow> \<not> P x \<Longrightarrow> \<exists>y. V y < V x \<and> \<not> P y"
  1090   shows "P x"
  1091 proof -
  1092   obtain n where "n = V x" by auto
  1093   moreover have "\<And>x. V x = n \<Longrightarrow> P x"
  1094   proof (induct n rule: infinite_descent0)
  1095     case 0
  1096     with 1 show "P x" by auto
  1097   next
  1098     case (smaller n)
  1099     then obtain x where *: "V x = n " and "V x > 0 \<and> \<not> P x" by auto
  1100     with 2 obtain y where "V y < V x \<and> \<not> P y" by auto
  1101     with * obtain m where "m = V y \<and> m < n \<and> \<not> P y" by auto
  1102     then show ?case by auto
  1103   qed
  1104   ultimately show "P x" by auto
  1105 qed
  1106 
  1107 text \<open>Again, without explicit base case:\<close>
  1108 lemma infinite_descent_measure:
  1109   fixes V :: "'a \<Rightarrow> nat"
  1110   assumes "\<And>x. \<not> P x \<Longrightarrow> \<exists>y. V y < V x \<and> \<not> P y"
  1111   shows "P x"
  1112 proof -
  1113   from assms obtain n where "n = V x" by auto
  1114   moreover have "\<And>x. V x = n \<Longrightarrow> P x"
  1115   proof (induct n rule: infinite_descent, auto)
  1116     show "\<exists>m < V x. \<exists>y. V y = m \<and> \<not> P y" if "\<not> P x" for x
  1117       using assms and that by auto
  1118   qed
  1119   ultimately show "P x" by auto
  1120 qed
  1121 
  1122 text \<open>A (clumsy) way of lifting \<open><\<close> monotonicity to \<open>\<le>\<close> monotonicity\<close>
  1123 lemma less_mono_imp_le_mono:
  1124   fixes f :: "nat \<Rightarrow> nat"
  1125     and i j :: nat
  1126   assumes "\<And>i j::nat. i < j \<Longrightarrow> f i < f j"
  1127     and "i \<le> j"
  1128   shows "f i \<le> f j"
  1129   using assms by (auto simp add: order_le_less)
  1130 
  1131 
  1132 text \<open>non-strict, in 1st argument\<close>
  1133 lemma add_le_mono1: "i \<le> j \<Longrightarrow> i + k \<le> j + k"
  1134   for i j k :: nat
  1135   by (rule add_right_mono)
  1136 
  1137 text \<open>non-strict, in both arguments\<close>
  1138 lemma add_le_mono: "i \<le> j \<Longrightarrow> k \<le> l \<Longrightarrow> i + k \<le> j + l"
  1139   for i j k l :: nat
  1140   by (rule add_mono)
  1141 
  1142 lemma le_add2: "n \<le> m + n"
  1143   for m n :: nat
  1144   by simp
  1145 
  1146 lemma le_add1: "n \<le> n + m"
  1147   for m n :: nat
  1148   by simp
  1149 
  1150 lemma less_add_Suc1: "i < Suc (i + m)"
  1151   by (rule le_less_trans, rule le_add1, rule lessI)
  1152 
  1153 lemma less_add_Suc2: "i < Suc (m + i)"
  1154   by (rule le_less_trans, rule le_add2, rule lessI)
  1155 
  1156 lemma less_iff_Suc_add: "m < n \<longleftrightarrow> (\<exists>k. n = Suc (m + k))"
  1157   by (iprover intro!: less_add_Suc1 less_imp_Suc_add)
  1158 
  1159 lemma trans_le_add1: "i \<le> j \<Longrightarrow> i \<le> j + m"
  1160   for i j m :: nat
  1161   by (rule le_trans, assumption, rule le_add1)
  1162 
  1163 lemma trans_le_add2: "i \<le> j \<Longrightarrow> i \<le> m + j"
  1164   for i j m :: nat
  1165   by (rule le_trans, assumption, rule le_add2)
  1166 
  1167 lemma trans_less_add1: "i < j \<Longrightarrow> i < j + m"
  1168   for i j m :: nat
  1169   by (rule less_le_trans, assumption, rule le_add1)
  1170 
  1171 lemma trans_less_add2: "i < j \<Longrightarrow> i < m + j"
  1172   for i j m :: nat
  1173   by (rule less_le_trans, assumption, rule le_add2)
  1174 
  1175 lemma add_lessD1: "i + j < k \<Longrightarrow> i < k"
  1176   for i j k :: nat
  1177   by (rule le_less_trans [of _ "i+j"]) (simp_all add: le_add1)
  1178 
  1179 lemma not_add_less1 [iff]: "\<not> i + j < i"
  1180   for i j :: nat
  1181   apply (rule notI)
  1182   apply (drule add_lessD1)
  1183   apply (erule less_irrefl [THEN notE])
  1184   done
  1185 
  1186 lemma not_add_less2 [iff]: "\<not> j + i < i"
  1187   for i j :: nat
  1188   by (simp add: add.commute)
  1189 
  1190 lemma add_leD1: "m + k \<le> n \<Longrightarrow> m \<le> n"
  1191   for k m n :: nat
  1192   by (rule order_trans [of _ "m + k"]) (simp_all add: le_add1)
  1193 
  1194 lemma add_leD2: "m + k \<le> n \<Longrightarrow> k \<le> n"
  1195   for k m n :: nat
  1196   apply (simp add: add.commute)
  1197   apply (erule add_leD1)
  1198   done
  1199 
  1200 lemma add_leE: "m + k \<le> n \<Longrightarrow> (m \<le> n \<Longrightarrow> k \<le> n \<Longrightarrow> R) \<Longrightarrow> R"
  1201   for k m n :: nat
  1202   by (blast dest: add_leD1 add_leD2)
  1203 
  1204 text \<open>needs \<open>\<And>k\<close> for \<open>ac_simps\<close> to work\<close>
  1205 lemma less_add_eq_less: "\<And>k. k < l \<Longrightarrow> m + l = k + n \<Longrightarrow> m < n"
  1206   for l m n :: nat
  1207   by (force simp del: add_Suc_right simp add: less_iff_Suc_add add_Suc_right [symmetric] ac_simps)
  1208 
  1209 
  1210 subsubsection \<open>More results about difference\<close>
  1211 
  1212 lemma Suc_diff_le: "n \<le> m \<Longrightarrow> Suc m - n = Suc (m - n)"
  1213   by (induct m n rule: diff_induct) simp_all
  1214 
  1215 lemma diff_less_Suc: "m - n < Suc m"
  1216   apply (induct m n rule: diff_induct)
  1217     apply (erule_tac [3] less_SucE)
  1218      apply (simp_all add: less_Suc_eq)
  1219   done
  1220 
  1221 lemma diff_le_self [simp]: "m - n \<le> m"
  1222   for m n :: nat
  1223   by (induct m n rule: diff_induct) (simp_all add: le_SucI)
  1224 
  1225 lemma less_imp_diff_less: "j < k \<Longrightarrow> j - n < k"
  1226   for j k n :: nat
  1227   by (rule le_less_trans, rule diff_le_self)
  1228 
  1229 lemma diff_Suc_less [simp]: "0 < n \<Longrightarrow> n - Suc i < n"
  1230   by (cases n) (auto simp add: le_simps)
  1231 
  1232 lemma diff_add_assoc: "k \<le> j \<Longrightarrow> (i + j) - k = i + (j - k)"
  1233   for i j k :: nat
  1234   by (induct j k rule: diff_induct) simp_all
  1235 
  1236 lemma add_diff_assoc [simp]: "k \<le> j \<Longrightarrow> i + (j - k) = i + j - k"
  1237   for i j k :: nat
  1238   by (fact diff_add_assoc [symmetric])
  1239 
  1240 lemma diff_add_assoc2: "k \<le> j \<Longrightarrow> (j + i) - k = (j - k) + i"
  1241   for i j k :: nat
  1242   by (simp add: ac_simps)
  1243 
  1244 lemma add_diff_assoc2 [simp]: "k \<le> j \<Longrightarrow> j - k + i = j + i - k"
  1245   for i j k :: nat
  1246   by (fact diff_add_assoc2 [symmetric])
  1247 
  1248 lemma le_imp_diff_is_add: "i \<le> j \<Longrightarrow> (j - i = k) = (j = k + i)"
  1249   for i j k :: nat
  1250   by auto
  1251 
  1252 lemma diff_is_0_eq [simp]: "m - n = 0 \<longleftrightarrow> m \<le> n"
  1253   for m n :: nat
  1254   by (induct m n rule: diff_induct) simp_all
  1255 
  1256 lemma diff_is_0_eq' [simp]: "m \<le> n \<Longrightarrow> m - n = 0"
  1257   for m n :: nat
  1258   by (rule iffD2, rule diff_is_0_eq)
  1259 
  1260 lemma zero_less_diff [simp]: "0 < n - m \<longleftrightarrow> m < n"
  1261   for m n :: nat
  1262   by (induct m n rule: diff_induct) simp_all
  1263 
  1264 lemma less_imp_add_positive:
  1265   assumes "i < j"
  1266   shows "\<exists>k::nat. 0 < k \<and> i + k = j"
  1267 proof
  1268   from assms show "0 < j - i \<and> i + (j - i) = j"
  1269     by (simp add: order_less_imp_le)
  1270 qed
  1271 
  1272 text \<open>a nice rewrite for bounded subtraction\<close>
  1273 lemma nat_minus_add_max: "n - m + m = max n m"
  1274   for m n :: nat
  1275   by (simp add: max_def not_le order_less_imp_le)
  1276 
  1277 lemma nat_diff_split: "P (a - b) \<longleftrightarrow> (a < b \<longrightarrow> P 0) \<and> (\<forall>d. a = b + d \<longrightarrow> P d)"
  1278   for a b :: nat
  1279   \<comment> \<open>elimination of \<open>-\<close> on \<open>nat\<close>\<close>
  1280   by (cases "a < b") (auto simp add: not_less le_less dest!: add_eq_self_zero [OF sym])
  1281 
  1282 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))"
  1283   for a b :: nat
  1284   \<comment> \<open>elimination of \<open>-\<close> on \<open>nat\<close> in assumptions\<close>
  1285   by (auto split: nat_diff_split)
  1286 
  1287 lemma Suc_pred': "0 < n \<Longrightarrow> n = Suc(n - 1)"
  1288   by simp
  1289 
  1290 lemma add_eq_if: "m + n = (if m = 0 then n else Suc ((m - 1) + n))"
  1291   unfolding One_nat_def by (cases m) simp_all
  1292 
  1293 lemma mult_eq_if: "m * n = (if m = 0 then 0 else n + ((m - 1) * n))"
  1294   for m n :: nat
  1295   by (cases m) simp_all
  1296 
  1297 lemma Suc_diff_eq_diff_pred: "0 < n \<Longrightarrow> Suc m - n = m - (n - 1)"
  1298   by (cases n) simp_all
  1299 
  1300 lemma diff_Suc_eq_diff_pred: "m - Suc n = (m - 1) - n"
  1301   by (cases m) simp_all
  1302 
  1303 lemma Let_Suc [simp]: "Let (Suc n) f \<equiv> f (Suc n)"
  1304   by (fact Let_def)
  1305 
  1306 
  1307 subsubsection \<open>Monotonicity of multiplication\<close>
  1308 
  1309 lemma mult_le_mono1: "i \<le> j \<Longrightarrow> i * k \<le> j * k"
  1310   for i j k :: nat
  1311   by (simp add: mult_right_mono)
  1312 
  1313 lemma mult_le_mono2: "i \<le> j \<Longrightarrow> k * i \<le> k * j"
  1314   for i j k :: nat
  1315   by (simp add: mult_left_mono)
  1316 
  1317 text \<open>\<open>\<le>\<close> monotonicity, BOTH arguments\<close>
  1318 lemma mult_le_mono: "i \<le> j \<Longrightarrow> k \<le> l \<Longrightarrow> i * k \<le> j * l"
  1319   for i j k l :: nat
  1320   by (simp add: mult_mono)
  1321 
  1322 lemma mult_less_mono1: "i < j \<Longrightarrow> 0 < k \<Longrightarrow> i * k < j * k"
  1323   for i j k :: nat
  1324   by (simp add: mult_strict_right_mono)
  1325 
  1326 text \<open>Differs from the standard \<open>zero_less_mult_iff\<close> in that there are no negative numbers.\<close>
  1327 lemma nat_0_less_mult_iff [simp]: "0 < m * n \<longleftrightarrow> 0 < m \<and> 0 < n"
  1328   for m n :: nat
  1329 proof (induct m)
  1330   case 0
  1331   then show ?case by simp
  1332 next
  1333   case (Suc m)
  1334   then show ?case by (cases n) simp_all
  1335 qed
  1336 
  1337 lemma one_le_mult_iff [simp]: "Suc 0 \<le> m * n \<longleftrightarrow> Suc 0 \<le> m \<and> Suc 0 \<le> n"
  1338 proof (induct m)
  1339   case 0
  1340   then show ?case by simp
  1341 next
  1342   case (Suc m)
  1343   then show ?case by (cases n) simp_all
  1344 qed
  1345 
  1346 lemma mult_less_cancel2 [simp]: "m * k < n * k \<longleftrightarrow> 0 < k \<and> m < n"
  1347   for k m n :: nat
  1348   apply (safe intro!: mult_less_mono1)
  1349    apply (cases k)
  1350     apply auto
  1351   apply (simp add: linorder_not_le [symmetric])
  1352   apply (blast intro: mult_le_mono1)
  1353   done
  1354 
  1355 lemma mult_less_cancel1 [simp]: "k * m < k * n \<longleftrightarrow> 0 < k \<and> m < n"
  1356   for k m n :: nat
  1357   by (simp add: mult.commute [of k])
  1358 
  1359 lemma mult_le_cancel1 [simp]: "k * m \<le> k * n \<longleftrightarrow> (0 < k \<longrightarrow> m \<le> n)"
  1360   for k m n :: nat
  1361   by (simp add: linorder_not_less [symmetric], auto)
  1362 
  1363 lemma mult_le_cancel2 [simp]: "m * k \<le> n * k \<longleftrightarrow> (0 < k \<longrightarrow> m \<le> n)"
  1364   for k m n :: nat
  1365   by (simp add: linorder_not_less [symmetric], auto)
  1366 
  1367 lemma Suc_mult_less_cancel1: "Suc k * m < Suc k * n \<longleftrightarrow> m < n"
  1368   by (subst mult_less_cancel1) simp
  1369 
  1370 lemma Suc_mult_le_cancel1: "Suc k * m \<le> Suc k * n \<longleftrightarrow> m \<le> n"
  1371   by (subst mult_le_cancel1) simp
  1372 
  1373 lemma le_square: "m \<le> m * m"
  1374   for m :: nat
  1375   by (cases m) (auto intro: le_add1)
  1376 
  1377 lemma le_cube: "m \<le> m * (m * m)"
  1378   for m :: nat
  1379   by (cases m) (auto intro: le_add1)
  1380 
  1381 text \<open>Lemma for \<open>gcd\<close>\<close>
  1382 lemma mult_eq_self_implies_10: "m = m * n \<Longrightarrow> n = 1 \<or> m = 0"
  1383   for m n :: nat
  1384   apply (drule sym)
  1385   apply (rule disjCI)
  1386   apply (rule linorder_cases)
  1387     defer
  1388     apply assumption
  1389    apply (drule mult_less_mono2)
  1390     apply auto
  1391   done
  1392 
  1393 lemma mono_times_nat:
  1394   fixes n :: nat
  1395   assumes "n > 0"
  1396   shows "mono (times n)"
  1397 proof
  1398   fix m q :: nat
  1399   assume "m \<le> q"
  1400   with assms show "n * m \<le> n * q" by simp
  1401 qed
  1402 
  1403 text \<open>The lattice order on @{typ nat}.\<close>
  1404 
  1405 instantiation nat :: distrib_lattice
  1406 begin
  1407 
  1408 definition "(inf :: nat \<Rightarrow> nat \<Rightarrow> nat) = min"
  1409 
  1410 definition "(sup :: nat \<Rightarrow> nat \<Rightarrow> nat) = max"
  1411 
  1412 instance
  1413   by intro_classes
  1414     (auto simp add: inf_nat_def sup_nat_def max_def not_le min_def
  1415       intro: order_less_imp_le antisym elim!: order_trans order_less_trans)
  1416 
  1417 end
  1418 
  1419 
  1420 subsection \<open>Natural operation of natural numbers on functions\<close>
  1421 
  1422 text \<open>
  1423   We use the same logical constant for the power operations on
  1424   functions and relations, in order to share the same syntax.
  1425 \<close>
  1426 
  1427 consts compow :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"
  1428 
  1429 abbreviation compower :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixr "^^" 80)
  1430   where "f ^^ n \<equiv> compow n f"
  1431 
  1432 notation (latex output)
  1433   compower ("(_\<^bsup>_\<^esup>)" [1000] 1000)
  1434 
  1435 text \<open>\<open>f ^^ n = f \<circ> \<dots> \<circ> f\<close>, the \<open>n\<close>-fold composition of \<open>f\<close>\<close>
  1436 
  1437 overloading
  1438   funpow \<equiv> "compow :: nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a)"
  1439 begin
  1440 
  1441 primrec funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
  1442   where
  1443     "funpow 0 f = id"
  1444   | "funpow (Suc n) f = f \<circ> funpow n f"
  1445 
  1446 end
  1447 
  1448 lemma funpow_0 [simp]: "(f ^^ 0) x = x"
  1449   by simp
  1450 
  1451 lemma funpow_Suc_right: "f ^^ Suc n = f ^^ n \<circ> f"
  1452 proof (induct n)
  1453   case 0
  1454   then show ?case by simp
  1455 next
  1456   fix n
  1457   assume "f ^^ Suc n = f ^^ n \<circ> f"
  1458   then show "f ^^ Suc (Suc n) = f ^^ Suc n \<circ> f"
  1459     by (simp add: o_assoc)
  1460 qed
  1461 
  1462 lemmas funpow_simps_right = funpow.simps(1) funpow_Suc_right
  1463 
  1464 text \<open>For code generation.\<close>
  1465 
  1466 definition funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
  1467   where funpow_code_def [code_abbrev]: "funpow = compow"
  1468 
  1469 lemma [code]:
  1470   "funpow (Suc n) f = f \<circ> funpow n f"
  1471   "funpow 0 f = id"
  1472   by (simp_all add: funpow_code_def)
  1473 
  1474 hide_const (open) funpow
  1475 
  1476 lemma funpow_add: "f ^^ (m + n) = f ^^ m \<circ> f ^^ n"
  1477   by (induct m) simp_all
  1478 
  1479 lemma funpow_mult: "(f ^^ m) ^^ n = f ^^ (m * n)"
  1480   for f :: "'a \<Rightarrow> 'a"
  1481   by (induct n) (simp_all add: funpow_add)
  1482 
  1483 lemma funpow_swap1: "f ((f ^^ n) x) = (f ^^ n) (f x)"
  1484 proof -
  1485   have "f ((f ^^ n) x) = (f ^^ (n + 1)) x" by simp
  1486   also have "\<dots>  = (f ^^ n \<circ> f ^^ 1) x" by (simp only: funpow_add)
  1487   also have "\<dots> = (f ^^ n) (f x)" by simp
  1488   finally show ?thesis .
  1489 qed
  1490 
  1491 lemma comp_funpow: "comp f ^^ n = comp (f ^^ n)"
  1492   for f :: "'a \<Rightarrow> 'a"
  1493   by (induct n) simp_all
  1494 
  1495 lemma Suc_funpow[simp]: "Suc ^^ n = (op + n)"
  1496   by (induct n) simp_all
  1497 
  1498 lemma id_funpow[simp]: "id ^^ n = id"
  1499   by (induct n) simp_all
  1500 
  1501 lemma funpow_mono: "mono f \<Longrightarrow> A \<le> B \<Longrightarrow> (f ^^ n) A \<le> (f ^^ n) B"
  1502   for f :: "'a \<Rightarrow> ('a::order)"
  1503   by (induct n arbitrary: A B)
  1504      (auto simp del: funpow.simps(2) simp add: funpow_Suc_right mono_def)
  1505 
  1506 lemma funpow_mono2:
  1507   assumes "mono f"
  1508     and "i \<le> j"
  1509     and "x \<le> y"
  1510     and "x \<le> f x"
  1511   shows "(f ^^ i) x \<le> (f ^^ j) y"
  1512   using assms(2,3)
  1513 proof (induct j arbitrary: y)
  1514   case 0
  1515   then show ?case by simp
  1516 next
  1517   case (Suc j)
  1518   show ?case
  1519   proof(cases "i = Suc j")
  1520     case True
  1521     with assms(1) Suc show ?thesis
  1522       by (simp del: funpow.simps add: funpow_simps_right monoD funpow_mono)
  1523   next
  1524     case False
  1525     with assms(1,4) Suc show ?thesis
  1526       by (simp del: funpow.simps add: funpow_simps_right le_eq_less_or_eq less_Suc_eq_le)
  1527         (simp add: Suc.hyps monoD order_subst1)
  1528   qed
  1529 qed
  1530 
  1531 
  1532 subsection \<open>Kleene iteration\<close>
  1533 
  1534 lemma Kleene_iter_lpfp:
  1535   fixes f :: "'a::order_bot \<Rightarrow> 'a"
  1536   assumes "mono f"
  1537     and "f p \<le> p"
  1538   shows "(f ^^ k) bot \<le> p"
  1539 proof (induct k)
  1540   case 0
  1541   show ?case by simp
  1542 next
  1543   case Suc
  1544   show ?case
  1545     using monoD[OF assms(1) Suc] assms(2) by simp
  1546 qed
  1547 
  1548 lemma lfp_Kleene_iter:
  1549   assumes "mono f"
  1550     and "(f ^^ Suc k) bot = (f ^^ k) bot"
  1551   shows "lfp f = (f ^^ k) bot"
  1552 proof (rule antisym)
  1553   show "lfp f \<le> (f ^^ k) bot"
  1554   proof (rule lfp_lowerbound)
  1555     show "f ((f ^^ k) bot) \<le> (f ^^ k) bot"
  1556       using assms(2) by simp
  1557   qed
  1558   show "(f ^^ k) bot \<le> lfp f"
  1559     using Kleene_iter_lpfp[OF assms(1)] lfp_unfold[OF assms(1)] by simp
  1560 qed
  1561 
  1562 lemma mono_pow: "mono f \<Longrightarrow> mono (f ^^ n)"
  1563   for f :: "'a \<Rightarrow> 'a::complete_lattice"
  1564   by (induct n) (auto simp: mono_def)
  1565 
  1566 lemma lfp_funpow:
  1567   assumes f: "mono f"
  1568   shows "lfp (f ^^ Suc n) = lfp f"
  1569 proof (rule antisym)
  1570   show "lfp f \<le> lfp (f ^^ Suc n)"
  1571   proof (rule lfp_lowerbound)
  1572     have "f (lfp (f ^^ Suc n)) = lfp (\<lambda>x. f ((f ^^ n) x))"
  1573       unfolding funpow_Suc_right by (simp add: lfp_rolling f mono_pow comp_def)
  1574     then show "f (lfp (f ^^ Suc n)) \<le> lfp (f ^^ Suc n)"
  1575       by (simp add: comp_def)
  1576   qed
  1577   have "(f ^^ n) (lfp f) = lfp f" for n
  1578     by (induct n) (auto intro: f lfp_fixpoint)
  1579   then show "lfp (f ^^ Suc n) \<le> lfp f"
  1580     by (intro lfp_lowerbound) (simp del: funpow.simps)
  1581 qed
  1582 
  1583 lemma gfp_funpow:
  1584   assumes f: "mono f"
  1585   shows "gfp (f ^^ Suc n) = gfp f"
  1586 proof (rule antisym)
  1587   show "gfp f \<ge> gfp (f ^^ Suc n)"
  1588   proof (rule gfp_upperbound)
  1589     have "f (gfp (f ^^ Suc n)) = gfp (\<lambda>x. f ((f ^^ n) x))"
  1590       unfolding funpow_Suc_right by (simp add: gfp_rolling f mono_pow comp_def)
  1591     then show "f (gfp (f ^^ Suc n)) \<ge> gfp (f ^^ Suc n)"
  1592       by (simp add: comp_def)
  1593   qed
  1594   have "(f ^^ n) (gfp f) = gfp f" for n
  1595     by (induct n) (auto intro: f gfp_fixpoint)
  1596   then show "gfp (f ^^ Suc n) \<ge> gfp f"
  1597     by (intro gfp_upperbound) (simp del: funpow.simps)
  1598 qed
  1599 
  1600 lemma Kleene_iter_gpfp:
  1601   fixes f :: "'a::order_top \<Rightarrow> 'a"
  1602   assumes "mono f"
  1603     and "p \<le> f p"
  1604   shows "p \<le> (f ^^ k) top"
  1605 proof (induct k)
  1606   case 0
  1607   show ?case by simp
  1608 next
  1609   case Suc
  1610   show ?case
  1611     using monoD[OF assms(1) Suc] assms(2) by simp
  1612 qed
  1613 
  1614 lemma gfp_Kleene_iter:
  1615   assumes "mono f"
  1616     and "(f ^^ Suc k) top = (f ^^ k) top"
  1617   shows "gfp f = (f ^^ k) top"
  1618     (is "?lhs = ?rhs")
  1619 proof (rule antisym)
  1620   have "?rhs \<le> f ?rhs"
  1621     using assms(2) by simp
  1622   then show "?rhs \<le> ?lhs"
  1623     by (rule gfp_upperbound)
  1624   show "?lhs \<le> ?rhs"
  1625     using Kleene_iter_gpfp[OF assms(1)] gfp_unfold[OF assms(1)] by simp
  1626 qed
  1627 
  1628 
  1629 subsection \<open>Embedding of the naturals into any \<open>semiring_1\<close>: @{term of_nat}\<close>
  1630 
  1631 context semiring_1
  1632 begin
  1633 
  1634 definition of_nat :: "nat \<Rightarrow> 'a"
  1635   where "of_nat n = (plus 1 ^^ n) 0"
  1636 
  1637 lemma of_nat_simps [simp]:
  1638   shows of_nat_0: "of_nat 0 = 0"
  1639     and of_nat_Suc: "of_nat (Suc m) = 1 + of_nat m"
  1640   by (simp_all add: of_nat_def)
  1641 
  1642 lemma of_nat_1 [simp]: "of_nat 1 = 1"
  1643   by (simp add: of_nat_def)
  1644 
  1645 lemma of_nat_add [simp]: "of_nat (m + n) = of_nat m + of_nat n"
  1646   by (induct m) (simp_all add: ac_simps)
  1647 
  1648 lemma of_nat_mult [simp]: "of_nat (m * n) = of_nat m * of_nat n"
  1649   by (induct m) (simp_all add: ac_simps distrib_right)
  1650 
  1651 lemma mult_of_nat_commute: "of_nat x * y = y * of_nat x"
  1652   by (induct x) (simp_all add: algebra_simps)
  1653 
  1654 primrec of_nat_aux :: "('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a"
  1655   where
  1656     "of_nat_aux inc 0 i = i"
  1657   | "of_nat_aux inc (Suc n) i = of_nat_aux inc n (inc i)" \<comment> \<open>tail recursive\<close>
  1658 
  1659 lemma of_nat_code: "of_nat n = of_nat_aux (\<lambda>i. i + 1) n 0"
  1660 proof (induct n)
  1661   case 0
  1662   then show ?case by simp
  1663 next
  1664   case (Suc n)
  1665   have "\<And>i. of_nat_aux (\<lambda>i. i + 1) n (i + 1) = of_nat_aux (\<lambda>i. i + 1) n i + 1"
  1666     by (induct n) simp_all
  1667   from this [of 0] have "of_nat_aux (\<lambda>i. i + 1) n 1 = of_nat_aux (\<lambda>i. i + 1) n 0 + 1"
  1668     by simp
  1669   with Suc show ?case
  1670     by (simp add: add.commute)
  1671 qed
  1672 
  1673 end
  1674 
  1675 declare of_nat_code [code]
  1676 
  1677 context ring_1
  1678 begin
  1679 
  1680 lemma of_nat_diff: "n \<le> m \<Longrightarrow> of_nat (m - n) = of_nat m - of_nat n"
  1681   by (simp add: algebra_simps of_nat_add [symmetric])
  1682 
  1683 end
  1684 
  1685 text \<open>Class for unital semirings with characteristic zero.
  1686  Includes non-ordered rings like the complex numbers.\<close>
  1687 
  1688 class semiring_char_0 = semiring_1 +
  1689   assumes inj_of_nat: "inj of_nat"
  1690 begin
  1691 
  1692 lemma of_nat_eq_iff [simp]: "of_nat m = of_nat n \<longleftrightarrow> m = n"
  1693   by (auto intro: inj_of_nat injD)
  1694 
  1695 text \<open>Special cases where either operand is zero\<close>
  1696 
  1697 lemma of_nat_0_eq_iff [simp]: "0 = of_nat n \<longleftrightarrow> 0 = n"
  1698   by (fact of_nat_eq_iff [of 0 n, unfolded of_nat_0])
  1699 
  1700 lemma of_nat_eq_0_iff [simp]: "of_nat m = 0 \<longleftrightarrow> m = 0"
  1701   by (fact of_nat_eq_iff [of m 0, unfolded of_nat_0])
  1702 
  1703 lemma of_nat_neq_0 [simp]: "of_nat (Suc n) \<noteq> 0"
  1704   unfolding of_nat_eq_0_iff by simp
  1705 
  1706 lemma of_nat_0_neq [simp]: "0 \<noteq> of_nat (Suc n)"
  1707   unfolding of_nat_0_eq_iff by simp
  1708 
  1709 end
  1710 
  1711 class ring_char_0 = ring_1 + semiring_char_0
  1712 
  1713 context linordered_semidom
  1714 begin
  1715 
  1716 lemma of_nat_0_le_iff [simp]: "0 \<le> of_nat n"
  1717   by (induct n) simp_all
  1718 
  1719 lemma of_nat_less_0_iff [simp]: "\<not> of_nat m < 0"
  1720   by (simp add: not_less)
  1721 
  1722 lemma of_nat_less_iff [simp]: "of_nat m < of_nat n \<longleftrightarrow> m < n"
  1723   by (induct m n rule: diff_induct) (simp_all add: add_pos_nonneg)
  1724 
  1725 lemma of_nat_le_iff [simp]: "of_nat m \<le> of_nat n \<longleftrightarrow> m \<le> n"
  1726   by (simp add: not_less [symmetric] linorder_not_less [symmetric])
  1727 
  1728 lemma less_imp_of_nat_less: "m < n \<Longrightarrow> of_nat m < of_nat n"
  1729   by simp
  1730 
  1731 lemma of_nat_less_imp_less: "of_nat m < of_nat n \<Longrightarrow> m < n"
  1732   by simp
  1733 
  1734 text \<open>Every \<open>linordered_semidom\<close> has characteristic zero.\<close>
  1735 
  1736 subclass semiring_char_0
  1737   by standard (auto intro!: injI simp add: eq_iff)
  1738 
  1739 text \<open>Special cases where either operand is zero\<close>
  1740 
  1741 lemma of_nat_le_0_iff [simp]: "of_nat m \<le> 0 \<longleftrightarrow> m = 0"
  1742   by (rule of_nat_le_iff [of _ 0, simplified])
  1743 
  1744 lemma of_nat_0_less_iff [simp]: "0 < of_nat n \<longleftrightarrow> 0 < n"
  1745   by (rule of_nat_less_iff [of 0, simplified])
  1746 
  1747 end
  1748 
  1749 context linordered_idom
  1750 begin
  1751 
  1752 lemma abs_of_nat [simp]: "\<bar>of_nat n\<bar> = of_nat n"
  1753   unfolding abs_if by auto
  1754 
  1755 end
  1756 
  1757 lemma of_nat_id [simp]: "of_nat n = n"
  1758   by (induct n) simp_all
  1759 
  1760 lemma of_nat_eq_id [simp]: "of_nat = id"
  1761   by (auto simp add: fun_eq_iff)
  1762 
  1763 
  1764 subsection \<open>The set of natural numbers\<close>
  1765 
  1766 context semiring_1
  1767 begin
  1768 
  1769 definition Nats :: "'a set"  ("\<nat>")
  1770   where "\<nat> = range of_nat"
  1771 
  1772 lemma of_nat_in_Nats [simp]: "of_nat n \<in> \<nat>"
  1773   by (simp add: Nats_def)
  1774 
  1775 lemma Nats_0 [simp]: "0 \<in> \<nat>"
  1776   apply (simp add: Nats_def)
  1777   apply (rule range_eqI)
  1778   apply (rule of_nat_0 [symmetric])
  1779   done
  1780 
  1781 lemma Nats_1 [simp]: "1 \<in> \<nat>"
  1782   apply (simp add: Nats_def)
  1783   apply (rule range_eqI)
  1784   apply (rule of_nat_1 [symmetric])
  1785   done
  1786 
  1787 lemma Nats_add [simp]: "a \<in> \<nat> \<Longrightarrow> b \<in> \<nat> \<Longrightarrow> a + b \<in> \<nat>"
  1788   apply (auto simp add: Nats_def)
  1789   apply (rule range_eqI)
  1790   apply (rule of_nat_add [symmetric])
  1791   done
  1792 
  1793 lemma Nats_mult [simp]: "a \<in> \<nat> \<Longrightarrow> b \<in> \<nat> \<Longrightarrow> a * b \<in> \<nat>"
  1794   apply (auto simp add: Nats_def)
  1795   apply (rule range_eqI)
  1796   apply (rule of_nat_mult [symmetric])
  1797   done
  1798 
  1799 lemma Nats_cases [cases set: Nats]:
  1800   assumes "x \<in> \<nat>"
  1801   obtains (of_nat) n where "x = of_nat n"
  1802   unfolding Nats_def
  1803 proof -
  1804   from \<open>x \<in> \<nat>\<close> have "x \<in> range of_nat" unfolding Nats_def .
  1805   then obtain n where "x = of_nat n" ..
  1806   then show thesis ..
  1807 qed
  1808 
  1809 lemma Nats_induct [case_names of_nat, induct set: Nats]: "x \<in> \<nat> \<Longrightarrow> (\<And>n. P (of_nat n)) \<Longrightarrow> P x"
  1810   by (rule Nats_cases) auto
  1811 
  1812 end
  1813 
  1814 
  1815 subsection \<open>Further arithmetic facts concerning the natural numbers\<close>
  1816 
  1817 lemma subst_equals:
  1818   assumes "t = s" and "u = t"
  1819   shows "u = s"
  1820   using assms(2,1) by (rule trans)
  1821 
  1822 ML_file "Tools/nat_arith.ML"
  1823 
  1824 simproc_setup nateq_cancel_sums
  1825   ("(l::nat) + m = n" | "(l::nat) = m + n" | "Suc m = n" | "m = Suc n") =
  1826   \<open>fn phi => try o Nat_Arith.cancel_eq_conv\<close>
  1827 
  1828 simproc_setup natless_cancel_sums
  1829   ("(l::nat) + m < n" | "(l::nat) < m + n" | "Suc m < n" | "m < Suc n") =
  1830   \<open>fn phi => try o Nat_Arith.cancel_less_conv\<close>
  1831 
  1832 simproc_setup natle_cancel_sums
  1833   ("(l::nat) + m \<le> n" | "(l::nat) \<le> m + n" | "Suc m \<le> n" | "m \<le> Suc n") =
  1834   \<open>fn phi => try o Nat_Arith.cancel_le_conv\<close>
  1835 
  1836 simproc_setup natdiff_cancel_sums
  1837   ("(l::nat) + m - n" | "(l::nat) - (m + n)" | "Suc m - n" | "m - Suc n") =
  1838   \<open>fn phi => try o Nat_Arith.cancel_diff_conv\<close>
  1839 
  1840 context order
  1841 begin
  1842 
  1843 lemma lift_Suc_mono_le:
  1844   assumes mono: "\<And>n. f n \<le> f (Suc n)"
  1845     and "n \<le> n'"
  1846   shows "f n \<le> f n'"
  1847 proof (cases "n < n'")
  1848   case True
  1849   then show ?thesis
  1850     by (induct n n' rule: less_Suc_induct) (auto intro: mono)
  1851 next
  1852   case False
  1853   with \<open>n \<le> n'\<close> show ?thesis by auto
  1854 qed
  1855 
  1856 lemma lift_Suc_antimono_le:
  1857   assumes mono: "\<And>n. f n \<ge> f (Suc n)"
  1858     and "n \<le> n'"
  1859   shows "f n \<ge> f n'"
  1860 proof (cases "n < n'")
  1861   case True
  1862   then show ?thesis
  1863     by (induct n n' rule: less_Suc_induct) (auto intro: mono)
  1864 next
  1865   case False
  1866   with \<open>n \<le> n'\<close> show ?thesis by auto
  1867 qed
  1868 
  1869 lemma lift_Suc_mono_less:
  1870   assumes mono: "\<And>n. f n < f (Suc n)"
  1871     and "n < n'"
  1872   shows "f n < f n'"
  1873   using \<open>n < n'\<close> by (induct n n' rule: less_Suc_induct) (auto intro: mono)
  1874 
  1875 lemma lift_Suc_mono_less_iff: "(\<And>n. f n < f (Suc n)) \<Longrightarrow> f n < f m \<longleftrightarrow> n < m"
  1876   by (blast intro: less_asym' lift_Suc_mono_less [of f]
  1877     dest: linorder_not_less[THEN iffD1] le_eq_less_or_eq [THEN iffD1])
  1878 
  1879 end
  1880 
  1881 lemma mono_iff_le_Suc: "mono f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
  1882   unfolding mono_def by (auto intro: lift_Suc_mono_le [of f])
  1883 
  1884 lemma antimono_iff_le_Suc: "antimono f \<longleftrightarrow> (\<forall>n. f (Suc n) \<le> f n)"
  1885   unfolding antimono_def by (auto intro: lift_Suc_antimono_le [of f])
  1886 
  1887 lemma mono_nat_linear_lb:
  1888   fixes f :: "nat \<Rightarrow> nat"
  1889   assumes "\<And>m n. m < n \<Longrightarrow> f m < f n"
  1890   shows "f m + k \<le> f (m + k)"
  1891 proof (induct k)
  1892   case 0
  1893   then show ?case by simp
  1894 next
  1895   case (Suc k)
  1896   then have "Suc (f m + k) \<le> Suc (f (m + k))" by simp
  1897   also from assms [of "m + k" "Suc (m + k)"] have "Suc (f (m + k)) \<le> f (Suc (m + k))"
  1898     by (simp add: Suc_le_eq)
  1899   finally show ?case by simp
  1900 qed
  1901 
  1902 
  1903 text \<open>Subtraction laws, mostly by Clemens Ballarin\<close>
  1904 
  1905 lemma diff_less_mono:
  1906   fixes a b c :: nat
  1907   assumes "a < b" and "c \<le> a"
  1908   shows "a - c < b - c"
  1909 proof -
  1910   from assms obtain d e where "b = c + (d + e)" and "a = c + e" and "d > 0"
  1911     by (auto dest!: le_Suc_ex less_imp_Suc_add simp add: ac_simps)
  1912   then show ?thesis by simp
  1913 qed
  1914 
  1915 lemma less_diff_conv: "i < j - k \<longleftrightarrow> i + k < j"
  1916   for i j k :: nat
  1917   by (cases "k \<le> j") (auto simp add: not_le dest: less_imp_Suc_add le_Suc_ex)
  1918 
  1919 lemma less_diff_conv2: "k \<le> j \<Longrightarrow> j - k < i \<longleftrightarrow> j < i + k"
  1920   for j k i :: nat
  1921   by (auto dest: le_Suc_ex)
  1922 
  1923 lemma le_diff_conv: "j - k \<le> i \<longleftrightarrow> j \<le> i + k"
  1924   for j k i :: nat
  1925   by (cases "k \<le> j") (auto simp add: not_le dest!: less_imp_Suc_add le_Suc_ex)
  1926 
  1927 lemma diff_diff_cancel [simp]: "i \<le> n \<Longrightarrow> n - (n - i) = i"
  1928   for i n :: nat
  1929   by (auto dest: le_Suc_ex)
  1930 
  1931 lemma diff_less [simp]: "0 < n \<Longrightarrow> 0 < m \<Longrightarrow> m - n < m"
  1932   for i n :: nat
  1933   by (auto dest: less_imp_Suc_add)
  1934 
  1935 text \<open>Simplification of relational expressions involving subtraction\<close>
  1936 
  1937 lemma diff_diff_eq: "k \<le> m \<Longrightarrow> k \<le> n \<Longrightarrow> m - k - (n - k) = m - n"
  1938   for m n k :: nat
  1939   by (auto dest!: le_Suc_ex)
  1940 
  1941 hide_fact (open) diff_diff_eq
  1942 
  1943 lemma eq_diff_iff: "k \<le> m \<Longrightarrow> k \<le> n \<Longrightarrow> m - k = n - k \<longleftrightarrow> m = n"
  1944   for m n k :: nat
  1945   by (auto dest: le_Suc_ex)
  1946 
  1947 lemma less_diff_iff: "k \<le> m \<Longrightarrow> k \<le> n \<Longrightarrow> m - k < n - k \<longleftrightarrow> m < n"
  1948   for m n k :: nat
  1949   by (auto dest!: le_Suc_ex)
  1950 
  1951 lemma le_diff_iff: "k \<le> m \<Longrightarrow> k \<le> n \<Longrightarrow> m - k \<le> n - k \<longleftrightarrow> m \<le> n"
  1952   for m n k :: nat
  1953   by (auto dest!: le_Suc_ex)
  1954 
  1955 lemma le_diff_iff': "a \<le> c \<Longrightarrow> b \<le> c \<Longrightarrow> c - a \<le> c - b \<longleftrightarrow> b \<le> a"
  1956   for a b c :: nat
  1957   by (force dest: le_Suc_ex)
  1958 
  1959 
  1960 text \<open>(Anti)Monotonicity of subtraction -- by Stephan Merz\<close>
  1961 
  1962 lemma diff_le_mono: "m \<le> n \<Longrightarrow> m - l \<le> n - l"
  1963   for m n l :: nat
  1964   by (auto dest: less_imp_le less_imp_Suc_add split: nat_diff_split)
  1965 
  1966 lemma diff_le_mono2: "m \<le> n \<Longrightarrow> l - n \<le> l - m"
  1967   for m n l :: nat
  1968   by (auto dest: less_imp_le le_Suc_ex less_imp_Suc_add less_le_trans split: nat_diff_split)
  1969 
  1970 lemma diff_less_mono2: "m < n \<Longrightarrow> m < l \<Longrightarrow> l - n < l - m"
  1971   for m n l :: nat
  1972   by (auto dest: less_imp_Suc_add split: nat_diff_split)
  1973 
  1974 lemma diffs0_imp_equal: "m - n = 0 \<Longrightarrow> n - m = 0 \<Longrightarrow> m = n"
  1975   for m n :: nat
  1976   by (simp split: nat_diff_split)
  1977 
  1978 lemma min_diff: "min (m - i) (n - i) = min m n - i"
  1979   for m n i :: nat
  1980   by (cases m n rule: le_cases)
  1981     (auto simp add: not_le min.absorb1 min.absorb2 min.absorb_iff1 [symmetric] diff_le_mono)
  1982 
  1983 lemma inj_on_diff_nat:
  1984   fixes k :: nat
  1985   assumes "\<forall>n \<in> N. k \<le> n"
  1986   shows "inj_on (\<lambda>n. n - k) N"
  1987 proof (rule inj_onI)
  1988   fix x y
  1989   assume a: "x \<in> N" "y \<in> N" "x - k = y - k"
  1990   with assms have "x - k + k = y - k + k" by auto
  1991   with a assms show "x = y" by (auto simp add: eq_diff_iff)
  1992 qed
  1993 
  1994 text \<open>Rewriting to pull differences out\<close>
  1995 
  1996 lemma diff_diff_right [simp]: "k \<le> j \<Longrightarrow> i - (j - k) = i + k - j"
  1997   for i j k :: nat
  1998   by (fact diff_diff_right)
  1999 
  2000 lemma diff_Suc_diff_eq1 [simp]:
  2001   assumes "k \<le> j"
  2002   shows "i - Suc (j - k) = i + k - Suc j"
  2003 proof -
  2004   from assms have *: "Suc (j - k) = Suc j - k"
  2005     by (simp add: Suc_diff_le)
  2006   from assms have "k \<le> Suc j"
  2007     by (rule order_trans) simp
  2008   with diff_diff_right [of k "Suc j" i] * show ?thesis
  2009     by simp
  2010 qed
  2011 
  2012 lemma diff_Suc_diff_eq2 [simp]:
  2013   assumes "k \<le> j"
  2014   shows "Suc (j - k) - i = Suc j - (k + i)"
  2015 proof -
  2016   from assms obtain n where "j = k + n"
  2017     by (auto dest: le_Suc_ex)
  2018   moreover have "Suc n - i = (k + Suc n) - (k + i)"
  2019     using add_diff_cancel_left [of k "Suc n" i] by simp
  2020   ultimately show ?thesis by simp
  2021 qed
  2022 
  2023 lemma Suc_diff_Suc:
  2024   assumes "n < m"
  2025   shows "Suc (m - Suc n) = m - n"
  2026 proof -
  2027   from assms obtain q where "m = n + Suc q"
  2028     by (auto dest: less_imp_Suc_add)
  2029   moreover define r where "r = Suc q"
  2030   ultimately have "Suc (m - Suc n) = r" and "m = n + r"
  2031     by simp_all
  2032   then show ?thesis by simp
  2033 qed
  2034 
  2035 lemma one_less_mult: "Suc 0 < n \<Longrightarrow> Suc 0 < m \<Longrightarrow> Suc 0 < m * n"
  2036   using less_1_mult [of n m] by (simp add: ac_simps)
  2037 
  2038 lemma n_less_m_mult_n: "0 < n \<Longrightarrow> Suc 0 < m \<Longrightarrow> n < m * n"
  2039   using mult_strict_right_mono [of 1 m n] by simp
  2040 
  2041 lemma n_less_n_mult_m: "0 < n \<Longrightarrow> Suc 0 < m \<Longrightarrow> n < n * m"
  2042   using mult_strict_left_mono [of 1 m n] by simp
  2043 
  2044 
  2045 text \<open>Specialized induction principles that work "backwards":\<close>
  2046 
  2047 lemma inc_induct [consumes 1, case_names base step]:
  2048   assumes less: "i \<le> j"
  2049     and base: "P j"
  2050     and step: "\<And>n. i \<le> n \<Longrightarrow> n < j \<Longrightarrow> P (Suc n) \<Longrightarrow> P n"
  2051   shows "P i"
  2052   using less step
  2053 proof (induct "j - i" arbitrary: i)
  2054   case (0 i)
  2055   then have "i = j" by simp
  2056   with base show ?case by simp
  2057 next
  2058   case (Suc d n)
  2059   from Suc.hyps have "n \<noteq> j" by auto
  2060   with Suc have "n < j" by (simp add: less_le)
  2061   from \<open>Suc d = j - n\<close> have "d + 1 = j - n" by simp
  2062   then have "d + 1 - 1 = j - n - 1" by simp
  2063   then have "d = j - n - 1" by simp
  2064   then have "d = j - (n + 1)" by (simp add: diff_diff_eq)
  2065   then have "d = j - Suc n" by simp
  2066   moreover from \<open>n < j\<close> have "Suc n \<le> j" by (simp add: Suc_le_eq)
  2067   ultimately have "P (Suc n)"
  2068   proof (rule Suc.hyps)
  2069     fix q
  2070     assume "Suc n \<le> q"
  2071     then have "n \<le> q" by (simp add: Suc_le_eq less_imp_le)
  2072     moreover assume "q < j"
  2073     moreover assume "P (Suc q)"
  2074     ultimately show "P q" by (rule Suc.prems)
  2075   qed
  2076   with order_refl \<open>n < j\<close> show "P n" by (rule Suc.prems)
  2077 qed
  2078 
  2079 lemma strict_inc_induct [consumes 1, case_names base step]:
  2080   assumes less: "i < j"
  2081     and base: "\<And>i. j = Suc i \<Longrightarrow> P i"
  2082     and step: "\<And>i. i < j \<Longrightarrow> P (Suc i) \<Longrightarrow> P i"
  2083   shows "P i"
  2084 using less proof (induct "j - i - 1" arbitrary: i)
  2085   case (0 i)
  2086   from \<open>i < j\<close> obtain n where "j = i + n" and "n > 0"
  2087     by (auto dest!: less_imp_Suc_add)
  2088   with 0 have "j = Suc i"
  2089     by (auto intro: order_antisym simp add: Suc_le_eq)
  2090   with base show ?case by simp
  2091 next
  2092   case (Suc d i)
  2093   from \<open>Suc d = j - i - 1\<close> have *: "Suc d = j - Suc i"
  2094     by (simp add: diff_diff_add)
  2095   then have "Suc d - 1 = j - Suc i - 1" by simp
  2096   then have "d = j - Suc i - 1" by simp
  2097   moreover from * have "j - Suc i \<noteq> 0" by auto
  2098   then have "Suc i < j" by (simp add: not_le)
  2099   ultimately have "P (Suc i)" by (rule Suc.hyps)
  2100   with \<open>i < j\<close> show "P i" by (rule step)
  2101 qed
  2102 
  2103 lemma zero_induct_lemma: "P k \<Longrightarrow> (\<And>n. P (Suc n) \<Longrightarrow> P n) \<Longrightarrow> P (k - i)"
  2104   using inc_induct[of "k - i" k P, simplified] by blast
  2105 
  2106 lemma zero_induct: "P k \<Longrightarrow> (\<And>n. P (Suc n) \<Longrightarrow> P n) \<Longrightarrow> P 0"
  2107   using inc_induct[of 0 k P] by blast
  2108 
  2109 text \<open>Further induction rule similar to @{thm inc_induct}.\<close>
  2110 
  2111 lemma dec_induct [consumes 1, case_names base step]:
  2112   "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"
  2113 proof (induct j arbitrary: i)
  2114   case 0
  2115   then show ?case by simp
  2116 next
  2117   case (Suc j)
  2118   from Suc.prems consider "i \<le> j" | "i = Suc j"
  2119     by (auto simp add: le_Suc_eq)
  2120   then show ?case
  2121   proof cases
  2122     case 1
  2123     moreover have "j < Suc j" by simp
  2124     moreover have "P j" using \<open>i \<le> j\<close> \<open>P i\<close>
  2125     proof (rule Suc.hyps)
  2126       fix q
  2127       assume "i \<le> q"
  2128       moreover assume "q < j" then have "q < Suc j"
  2129         by (simp add: less_Suc_eq)
  2130       moreover assume "P q"
  2131       ultimately show "P (Suc q)" by (rule Suc.prems)
  2132     qed
  2133     ultimately show "P (Suc j)" by (rule Suc.prems)
  2134   next
  2135     case 2
  2136     with \<open>P i\<close> show "P (Suc j)" by simp
  2137   qed
  2138 qed
  2139 
  2140 
  2141 subsection \<open>Monotonicity of \<open>funpow\<close>\<close>
  2142 
  2143 lemma funpow_increasing: "m \<le> n \<Longrightarrow> mono f \<Longrightarrow> (f ^^ n) \<top> \<le> (f ^^ m) \<top>"
  2144   for f :: "'a::{lattice,order_top} \<Rightarrow> 'a"
  2145   by (induct rule: inc_induct)
  2146     (auto simp del: funpow.simps(2) simp add: funpow_Suc_right
  2147       intro: order_trans[OF _ funpow_mono])
  2148 
  2149 lemma funpow_decreasing: "m \<le> n \<Longrightarrow> mono f \<Longrightarrow> (f ^^ m) \<bottom> \<le> (f ^^ n) \<bottom>"
  2150   for f :: "'a::{lattice,order_bot} \<Rightarrow> 'a"
  2151   by (induct rule: dec_induct)
  2152     (auto simp del: funpow.simps(2) simp add: funpow_Suc_right
  2153       intro: order_trans[OF _ funpow_mono])
  2154 
  2155 lemma mono_funpow: "mono Q \<Longrightarrow> mono (\<lambda>i. (Q ^^ i) \<bottom>)"
  2156   for Q :: "'a::{lattice,order_bot} \<Rightarrow> 'a"
  2157   by (auto intro!: funpow_decreasing simp: mono_def)
  2158 
  2159 lemma antimono_funpow: "mono Q \<Longrightarrow> antimono (\<lambda>i. (Q ^^ i) \<top>)"
  2160   for Q :: "'a::{lattice,order_top} \<Rightarrow> 'a"
  2161   by (auto intro!: funpow_increasing simp: antimono_def)
  2162 
  2163 
  2164 subsection \<open>The divides relation on @{typ nat}\<close>
  2165 
  2166 lemma dvd_1_left [iff]: "Suc 0 dvd k"
  2167   by (simp add: dvd_def)
  2168 
  2169 lemma dvd_1_iff_1 [simp]: "m dvd Suc 0 \<longleftrightarrow> m = Suc 0"
  2170   by (simp add: dvd_def)
  2171 
  2172 lemma nat_dvd_1_iff_1 [simp]: "m dvd 1 \<longleftrightarrow> m = 1"
  2173   for m :: nat
  2174   by (simp add: dvd_def)
  2175 
  2176 lemma dvd_antisym: "m dvd n \<Longrightarrow> n dvd m \<Longrightarrow> m = n"
  2177   for m n :: nat
  2178   unfolding dvd_def by (force dest: mult_eq_self_implies_10 simp add: mult.assoc)
  2179 
  2180 lemma dvd_diff_nat [simp]: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m - n)"
  2181   for k m n :: nat
  2182   unfolding dvd_def by (blast intro: right_diff_distrib' [symmetric])
  2183 
  2184 lemma dvd_diffD: "k dvd m - n \<Longrightarrow> k dvd n \<Longrightarrow> n \<le> m \<Longrightarrow> k dvd m"
  2185   for k m n :: nat
  2186   apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
  2187   apply (blast intro: dvd_add)
  2188   done
  2189 
  2190 lemma dvd_diffD1: "k dvd m - n \<Longrightarrow> k dvd m \<Longrightarrow> n \<le> m \<Longrightarrow> k dvd n"
  2191   for k m n :: nat
  2192   by (drule_tac m = m in dvd_diff_nat) auto
  2193 
  2194 lemma dvd_mult_cancel:
  2195   fixes m n k :: nat
  2196   assumes "k * m dvd k * n" and "0 < k"
  2197   shows "m dvd n"
  2198 proof -
  2199   from assms(1) obtain q where "k * n = (k * m) * q" ..
  2200   then have "k * n = k * (m * q)" by (simp add: ac_simps)
  2201   with \<open>0 < k\<close> have "n = m * q" by (auto simp add: mult_left_cancel)
  2202   then show ?thesis ..
  2203 qed
  2204 
  2205 lemma dvd_mult_cancel1: "0 < m \<Longrightarrow> m * n dvd m \<longleftrightarrow> n = 1"
  2206   for m n :: nat
  2207   apply auto
  2208   apply (subgoal_tac "m * n dvd m * 1")
  2209    apply (drule dvd_mult_cancel)
  2210     apply auto
  2211   done
  2212 
  2213 lemma dvd_mult_cancel2: "0 < m \<Longrightarrow> n * m dvd m \<longleftrightarrow> n = 1"
  2214   for m n :: nat
  2215   using dvd_mult_cancel1 [of m n] by (simp add: ac_simps)
  2216 
  2217 lemma dvd_imp_le: "k dvd n \<Longrightarrow> 0 < n \<Longrightarrow> k \<le> n"
  2218   for k n :: nat
  2219   by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
  2220 
  2221 lemma nat_dvd_not_less: "0 < m \<Longrightarrow> m < n \<Longrightarrow> \<not> n dvd m"
  2222   for m n :: nat
  2223   by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
  2224 
  2225 lemma less_eq_dvd_minus:
  2226   fixes m n :: nat
  2227   assumes "m \<le> n"
  2228   shows "m dvd n \<longleftrightarrow> m dvd n - m"
  2229 proof -
  2230   from assms have "n = m + (n - m)" by simp
  2231   then obtain q where "n = m + q" ..
  2232   then show ?thesis by (simp add: add.commute [of m])
  2233 qed
  2234 
  2235 lemma dvd_minus_self: "m dvd n - m \<longleftrightarrow> n < m \<or> m dvd n"
  2236   for m n :: nat
  2237   by (cases "n < m") (auto elim!: dvdE simp add: not_less le_imp_diff_is_add dest: less_imp_le)
  2238 
  2239 lemma dvd_minus_add:
  2240   fixes m n q r :: nat
  2241   assumes "q \<le> n" "q \<le> r * m"
  2242   shows "m dvd n - q \<longleftrightarrow> m dvd n + (r * m - q)"
  2243 proof -
  2244   have "m dvd n - q \<longleftrightarrow> m dvd r * m + (n - q)"
  2245     using dvd_add_times_triv_left_iff [of m r] by simp
  2246   also from assms have "\<dots> \<longleftrightarrow> m dvd r * m + n - q" by simp
  2247   also from assms have "\<dots> \<longleftrightarrow> m dvd (r * m - q) + n" by simp
  2248   also have "\<dots> \<longleftrightarrow> m dvd n + (r * m - q)" by (simp add: add.commute)
  2249   finally show ?thesis .
  2250 qed
  2251 
  2252 
  2253 subsection \<open>Aliasses\<close>
  2254 
  2255 lemma nat_mult_1: "1 * n = n"
  2256   for n :: nat
  2257   by (fact mult_1_left)
  2258 
  2259 lemma nat_mult_1_right: "n * 1 = n"
  2260   for n :: nat
  2261   by (fact mult_1_right)
  2262 
  2263 lemma nat_add_left_cancel: "k + m = k + n \<longleftrightarrow> m = n"
  2264   for k m n :: nat
  2265   by (fact add_left_cancel)
  2266 
  2267 lemma nat_add_right_cancel: "m + k = n + k \<longleftrightarrow> m = n"
  2268   for k m n :: nat
  2269   by (fact add_right_cancel)
  2270 
  2271 lemma diff_mult_distrib: "(m - n) * k = (m * k) - (n * k)"
  2272   for k m n :: nat
  2273   by (fact left_diff_distrib')
  2274 
  2275 lemma diff_mult_distrib2: "k * (m - n) = (k * m) - (k * n)"
  2276   for k m n :: nat
  2277   by (fact right_diff_distrib')
  2278 
  2279 lemma le_add_diff: "k \<le> n \<Longrightarrow> m \<le> n + m - k"
  2280   for k m n :: nat
  2281   by (fact le_add_diff)  (* FIXME delete *)
  2282 
  2283 lemma le_diff_conv2: "k \<le> j \<Longrightarrow> (i \<le> j - k) = (i + k \<le> j)"
  2284   for i j k :: nat
  2285   by (fact le_diff_conv2) (* FIXME delete *)
  2286 
  2287 lemma diff_self_eq_0 [simp]: "m - m = 0"
  2288   for m :: nat
  2289   by (fact diff_cancel)
  2290 
  2291 lemma diff_diff_left [simp]: "i - j - k = i - (j + k)"
  2292   for i j k :: nat
  2293   by (fact diff_diff_add)
  2294 
  2295 lemma diff_commute: "i - j - k = i - k - j"
  2296   for i j k :: nat
  2297   by (fact diff_right_commute)
  2298 
  2299 lemma diff_add_inverse: "(n + m) - n = m"
  2300   for m n :: nat
  2301   by (fact add_diff_cancel_left')
  2302 
  2303 lemma diff_add_inverse2: "(m + n) - n = m"
  2304   for m n :: nat
  2305   by (fact add_diff_cancel_right')
  2306 
  2307 lemma diff_cancel: "(k + m) - (k + n) = m - n"
  2308   for k m n :: nat
  2309   by (fact add_diff_cancel_left)
  2310 
  2311 lemma diff_cancel2: "(m + k) - (n + k) = m - n"
  2312   for k m n :: nat
  2313   by (fact add_diff_cancel_right)
  2314 
  2315 lemma diff_add_0: "n - (n + m) = 0"
  2316   for m n :: nat
  2317   by (fact diff_add_zero)
  2318 
  2319 lemma add_mult_distrib2: "k * (m + n) = (k * m) + (k * n)"
  2320   for k m n :: nat
  2321   by (fact distrib_left)
  2322 
  2323 lemmas nat_distrib =
  2324   add_mult_distrib distrib_left diff_mult_distrib diff_mult_distrib2
  2325 
  2326 
  2327 subsection \<open>Size of a datatype value\<close>
  2328 
  2329 class size =
  2330   fixes size :: "'a \<Rightarrow> nat" \<comment> \<open>see further theory \<open>Wellfounded\<close>\<close>
  2331 
  2332 instantiation nat :: size
  2333 begin
  2334 
  2335 definition size_nat where [simp, code]: "size (n::nat) = n"
  2336 
  2337 instance ..
  2338 
  2339 end
  2340 
  2341 
  2342 subsection \<open>Code module namespace\<close>
  2343 
  2344 code_identifier
  2345   code_module Nat \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
  2346 
  2347 hide_const (open) of_nat_aux
  2348 
  2349 end