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