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