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