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