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