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