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