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