src/HOL/Nat.thy
author wenzelm
Mon May 23 15:30:13 2016 +0200 (2016-05-23)
changeset 63113 fe31996e3898
parent 63111 caa0c513bbca
child 63145 703edebd1d92
permissions -rw-r--r--
removed odd cases rule (see also 8cb42cd97579);
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
berghofe@13449
   529
wenzelm@60758
   530
subsubsection \<open>Inductive (?) properties\<close>
berghofe@13449
   531
wenzelm@63110
   532
lemma Suc_lessI: "m < n \<Longrightarrow> Suc m \<noteq> n \<Longrightarrow> Suc m < n"
lp15@60562
   533
  unfolding less_eq_Suc_le [of m] le_less by simp
berghofe@13449
   534
haftmann@26072
   535
lemma lessE:
haftmann@26072
   536
  assumes major: "i < k"
wenzelm@63110
   537
    and 1: "k = Suc i \<Longrightarrow> P"
wenzelm@63110
   538
    and 2: "\<And>j. i < j \<Longrightarrow> k = Suc j \<Longrightarrow> P"
haftmann@26072
   539
  shows P
haftmann@26072
   540
proof -
haftmann@26072
   541
  from major have "\<exists>j. i \<le> j \<and> k = Suc j"
haftmann@26072
   542
    unfolding less_eq_Suc_le by (induct k) simp_all
haftmann@26072
   543
  then have "(\<exists>j. i < j \<and> k = Suc j) \<or> k = Suc i"
wenzelm@63110
   544
    by (auto simp add: less_le)
wenzelm@63110
   545
  with 1 2 show P by auto
haftmann@26072
   546
qed
haftmann@26072
   547
wenzelm@63110
   548
lemma less_SucE:
wenzelm@63110
   549
  assumes major: "m < Suc n"
wenzelm@63110
   550
    and less: "m < n \<Longrightarrow> P"
wenzelm@63110
   551
    and eq: "m = n \<Longrightarrow> P"
wenzelm@63110
   552
  shows P
haftmann@26072
   553
  apply (rule major [THEN lessE])
haftmann@26072
   554
  apply (rule eq, blast)
haftmann@26072
   555
  apply (rule less, blast)
berghofe@13449
   556
  done
berghofe@13449
   557
wenzelm@63110
   558
lemma Suc_lessE:
wenzelm@63110
   559
  assumes major: "Suc i < k"
wenzelm@63110
   560
    and minor: "\<And>j. i < j \<Longrightarrow> k = Suc j \<Longrightarrow> P"
wenzelm@63110
   561
  shows P
berghofe@13449
   562
  apply (rule major [THEN lessE])
berghofe@13449
   563
  apply (erule lessI [THEN minor])
paulson@14208
   564
  apply (erule Suc_lessD [THEN minor], assumption)
berghofe@13449
   565
  done
berghofe@13449
   566
wenzelm@63110
   567
lemma Suc_less_SucD: "Suc m < Suc n \<Longrightarrow> m < n"
haftmann@26072
   568
  by simp
berghofe@13449
   569
berghofe@13449
   570
lemma less_trans_Suc:
wenzelm@63110
   571
  assumes le: "i < j"
wenzelm@63110
   572
  shows "j < k \<Longrightarrow> Suc i < k"
paulson@14208
   573
  apply (induct k, simp_all)
wenzelm@63110
   574
  using le
berghofe@13449
   575
  apply (simp add: less_Suc_eq)
berghofe@13449
   576
  apply (blast dest: Suc_lessD)
berghofe@13449
   577
  done
berghofe@13449
   578
wenzelm@63110
   579
text \<open>Can be used with \<open>less_Suc_eq\<close> to get @{prop "n = m \<or> n < m"}\<close>
haftmann@26072
   580
lemma not_less_eq: "\<not> m < n \<longleftrightarrow> n < Suc m"
haftmann@26072
   581
  unfolding not_less less_Suc_eq_le ..
berghofe@13449
   582
haftmann@26072
   583
lemma not_less_eq_eq: "\<not> m \<le> n \<longleftrightarrow> Suc n \<le> m"
haftmann@26072
   584
  unfolding not_le Suc_le_eq ..
wenzelm@21243
   585
wenzelm@60758
   586
text \<open>Properties of "less than or equal"\<close>
berghofe@13449
   587
wenzelm@63110
   588
lemma le_imp_less_Suc: "m \<le> n \<Longrightarrow> m < Suc n"
haftmann@26072
   589
  unfolding less_Suc_eq_le .
berghofe@13449
   590
wenzelm@63110
   591
lemma Suc_n_not_le_n: "\<not> Suc n \<le> n"
haftmann@26072
   592
  unfolding not_le less_Suc_eq_le ..
berghofe@13449
   593
paulson@14267
   594
lemma le_Suc_eq: "(m \<le> Suc n) = (m \<le> n | m = Suc n)"
haftmann@26072
   595
  by (simp add: less_Suc_eq_le [symmetric] less_Suc_eq)
berghofe@13449
   596
wenzelm@63110
   597
lemma le_SucE: "m \<le> Suc n \<Longrightarrow> (m \<le> n \<Longrightarrow> R) \<Longrightarrow> (m = Suc n \<Longrightarrow> R) \<Longrightarrow> R"
haftmann@26072
   598
  by (drule le_Suc_eq [THEN iffD1], iprover+)
berghofe@13449
   599
wenzelm@63110
   600
lemma Suc_leI: "m < n \<Longrightarrow> Suc(m) \<le> n"
haftmann@26072
   601
  unfolding Suc_le_eq .
berghofe@13449
   602
wenzelm@61799
   603
text \<open>Stronger version of \<open>Suc_leD\<close>\<close>
wenzelm@63110
   604
lemma Suc_le_lessD: "Suc m \<le> n \<Longrightarrow> m < n"
haftmann@26072
   605
  unfolding Suc_le_eq .
berghofe@13449
   606
wenzelm@63110
   607
lemma less_imp_le_nat: "m < n \<Longrightarrow> m \<le> n" for m n :: nat
haftmann@26072
   608
  unfolding less_eq_Suc_le by (rule Suc_leD)
berghofe@13449
   609
wenzelm@61799
   610
text \<open>For instance, \<open>(Suc m < Suc n) = (Suc m \<le> n) = (m < n)\<close>\<close>
wenzelm@26315
   611
lemmas le_simps = less_imp_le_nat less_Suc_eq_le Suc_le_eq
berghofe@13449
   612
berghofe@13449
   613
wenzelm@63110
   614
text \<open>Equivalence of \<open>m \<le> n\<close> and \<open>m < n \<or> m = n\<close>\<close>
wenzelm@63110
   615
wenzelm@63110
   616
lemma less_or_eq_imp_le: "m < n \<or> m = n \<Longrightarrow> m \<le> n" for m n :: nat
haftmann@26072
   617
  unfolding le_less .
berghofe@13449
   618
wenzelm@63110
   619
lemma le_eq_less_or_eq: "m \<le> n \<longleftrightarrow> m < n \<or> m = n" for m n :: nat
haftmann@26072
   620
  by (rule le_less)
berghofe@13449
   621
wenzelm@61799
   622
text \<open>Useful with \<open>blast\<close>.\<close>
wenzelm@63110
   623
lemma eq_imp_le: "m = n \<Longrightarrow> m \<le> n" for m n :: nat
haftmann@26072
   624
  by auto
berghofe@13449
   625
wenzelm@63110
   626
lemma le_refl: "n \<le> n" for n :: nat
haftmann@26072
   627
  by simp
berghofe@13449
   628
wenzelm@63110
   629
lemma le_trans: "i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> i \<le> k" for i j k :: nat
haftmann@26072
   630
  by (rule order_trans)
berghofe@13449
   631
wenzelm@63110
   632
lemma le_antisym: "m \<le> n \<Longrightarrow> n \<le> m \<Longrightarrow> m = n" for m n :: nat
haftmann@26072
   633
  by (rule antisym)
berghofe@13449
   634
wenzelm@63110
   635
lemma nat_less_le: "m < n \<longleftrightarrow> m \<le> n \<and> m \<noteq> n" for m n :: nat
haftmann@26072
   636
  by (rule less_le)
berghofe@13449
   637
wenzelm@63110
   638
lemma le_neq_implies_less: "m \<le> n \<Longrightarrow> m \<noteq> n \<Longrightarrow> m < n" for m n :: nat
haftmann@26072
   639
  unfolding less_le ..
berghofe@13449
   640
wenzelm@63110
   641
lemma nat_le_linear: "m \<le> n | n \<le> m" for m n :: nat
haftmann@26072
   642
  by (rule linear)
paulson@14341
   643
wenzelm@22718
   644
lemmas linorder_neqE_nat = linorder_neqE [where 'a = nat]
nipkow@15921
   645
wenzelm@63110
   646
lemma le_less_Suc_eq: "m \<le> n \<Longrightarrow> n < Suc m \<longleftrightarrow> n = m"
haftmann@26072
   647
  unfolding less_Suc_eq_le by auto
berghofe@13449
   648
wenzelm@63110
   649
lemma not_less_less_Suc_eq: "\<not> n < m \<Longrightarrow> n < Suc m \<longleftrightarrow> n = m"
haftmann@26072
   650
  unfolding not_less by (rule le_less_Suc_eq)
berghofe@13449
   651
berghofe@13449
   652
lemmas not_less_simps = not_less_less_Suc_eq le_less_Suc_eq
berghofe@13449
   653
wenzelm@63110
   654
lemma not0_implies_Suc: "n \<noteq> 0 \<Longrightarrow> \<exists>m. n = Suc m"
wenzelm@63110
   655
  by (cases n) simp_all
wenzelm@63110
   656
wenzelm@63110
   657
lemma gr0_implies_Suc: "n > 0 \<Longrightarrow> \<exists>m. n = Suc m"
wenzelm@63110
   658
  by (cases n) simp_all
wenzelm@63110
   659
wenzelm@63110
   660
lemma gr_implies_not0: "m < n \<Longrightarrow> n \<noteq> 0" for m n :: nat
wenzelm@63110
   661
  by (cases n) simp_all
wenzelm@63110
   662
wenzelm@63110
   663
lemma neq0_conv[iff]: "n \<noteq> 0 \<longleftrightarrow> 0 < n" for n :: nat
wenzelm@63110
   664
  by (cases n) simp_all
nipkow@25140
   665
wenzelm@61799
   666
text \<open>This theorem is useful with \<open>blast\<close>\<close>
wenzelm@63110
   667
lemma gr0I: "(n = 0 \<Longrightarrow> False) \<Longrightarrow> 0 < n" for n :: nat
wenzelm@63110
   668
  by (rule neq0_conv[THEN iffD1], iprover)
wenzelm@63110
   669
wenzelm@63110
   670
lemma gr0_conv_Suc: "0 < n \<longleftrightarrow> (\<exists>m. n = Suc m)"
wenzelm@63110
   671
  by (fast intro: not0_implies_Suc)
wenzelm@63110
   672
wenzelm@63110
   673
lemma not_gr0 [iff]: "\<not> 0 < n \<longleftrightarrow> n = 0" for n :: nat
wenzelm@63110
   674
  using neq0_conv by blast
wenzelm@63110
   675
wenzelm@63110
   676
lemma Suc_le_D: "Suc n \<le> m' \<Longrightarrow> \<exists>m. m' = Suc m"
wenzelm@63110
   677
  by (induct m') simp_all
berghofe@13449
   678
wenzelm@60758
   679
text \<open>Useful in certain inductive arguments\<close>
wenzelm@63110
   680
lemma less_Suc_eq_0_disj: "m < Suc n \<longleftrightarrow> m = 0 \<or> (\<exists>j. m = Suc j \<and> j < n)"
wenzelm@63110
   681
  by (cases m) simp_all
berghofe@13449
   682
berghofe@13449
   683
wenzelm@60758
   684
subsubsection \<open>Monotonicity of Addition\<close>
berghofe@13449
   685
wenzelm@63110
   686
lemma Suc_pred [simp]: "n > 0 \<Longrightarrow> Suc (n - Suc 0) = n"
wenzelm@63110
   687
  by (simp add: diff_Suc split: nat.split)
wenzelm@63110
   688
wenzelm@63110
   689
lemma Suc_diff_1 [simp]: "0 < n \<Longrightarrow> Suc (n - 1) = n"
wenzelm@63110
   690
  unfolding One_nat_def by (rule Suc_pred)
wenzelm@63110
   691
wenzelm@63110
   692
lemma nat_add_left_cancel_le [simp]: "k + m \<le> k + n \<longleftrightarrow> m \<le> n" for k m n :: nat
wenzelm@63110
   693
  by (induct k) simp_all
wenzelm@63110
   694
wenzelm@63110
   695
lemma nat_add_left_cancel_less [simp]: "k + m < k + n \<longleftrightarrow> m < n" for k m n :: nat
wenzelm@63110
   696
  by (induct k) simp_all
wenzelm@63110
   697
wenzelm@63110
   698
lemma add_gr_0 [iff]: "m + n > 0 \<longleftrightarrow> m > 0 \<or> n > 0" for m n :: nat
wenzelm@63110
   699
  by (auto dest: gr0_implies_Suc)
berghofe@13449
   700
wenzelm@60758
   701
text \<open>strict, in 1st argument\<close>
wenzelm@63110
   702
lemma add_less_mono1: "i < j \<Longrightarrow> i + k < j + k" for i j k :: nat
wenzelm@63110
   703
  by (induct k) simp_all
paulson@14341
   704
wenzelm@60758
   705
text \<open>strict, in both arguments\<close>
wenzelm@63110
   706
lemma add_less_mono: "i < j \<Longrightarrow> k < l \<Longrightarrow> i + k < j + l" for i j k l :: nat
paulson@14341
   707
  apply (rule add_less_mono1 [THEN less_trans], assumption+)
paulson@15251
   708
  apply (induct j, simp_all)
paulson@14341
   709
  done
paulson@14341
   710
wenzelm@61799
   711
text \<open>Deleted \<open>less_natE\<close>; use \<open>less_imp_Suc_add RS exE\<close>\<close>
wenzelm@63110
   712
lemma less_imp_Suc_add: "m < n \<Longrightarrow> \<exists>k. n = Suc (m + k)"
paulson@14341
   713
  apply (induct n)
paulson@14341
   714
  apply (simp_all add: order_le_less)
wenzelm@22718
   715
  apply (blast elim!: less_SucE
haftmann@35047
   716
               intro!: Nat.add_0_right [symmetric] add_Suc_right [symmetric])
paulson@14341
   717
  done
paulson@14341
   718
wenzelm@63110
   719
lemma le_Suc_ex: "k \<le> l \<Longrightarrow> (\<exists>n. l = k + n)" for k l :: nat
hoelzl@56194
   720
  by (auto simp: less_Suc_eq_le[symmetric] dest: less_imp_Suc_add)
hoelzl@56194
   721
wenzelm@61799
   722
text \<open>strict, in 1st argument; proof is by induction on \<open>k > 0\<close>\<close>
haftmann@62481
   723
lemma mult_less_mono2:
haftmann@62481
   724
  fixes i j :: nat
haftmann@62481
   725
  assumes "i < j" and "0 < k"
haftmann@62481
   726
  shows "k * i < k * j"
wenzelm@63110
   727
  using \<open>0 < k\<close>
wenzelm@63110
   728
proof (induct k)
wenzelm@63110
   729
  case 0
wenzelm@63110
   730
  then show ?case by simp
haftmann@62481
   731
next
wenzelm@63110
   732
  case (Suc k)
wenzelm@63110
   733
  with \<open>i < j\<close> show ?case
haftmann@62481
   734
    by (cases k) (simp_all add: add_less_mono)
haftmann@62481
   735
qed
paulson@14341
   736
wenzelm@60758
   737
text \<open>Addition is the inverse of subtraction:
wenzelm@60758
   738
  if @{term "n \<le> m"} then @{term "n + (m - n) = m"}.\<close>
wenzelm@63110
   739
lemma add_diff_inverse_nat: "\<not> m < n \<Longrightarrow> n + (m - n) = m" for m n :: nat
wenzelm@63110
   740
  by (induct m n rule: diff_induct) simp_all
wenzelm@63110
   741
wenzelm@63110
   742
lemma nat_le_iff_add: "m \<le> n \<longleftrightarrow> (\<exists>k. n = m + k)" for m n :: nat
wenzelm@63110
   743
  using nat_add_left_cancel_le[of m 0] by (auto dest: le_Suc_ex)
hoelzl@62376
   744
hoelzl@62376
   745
text\<open>The naturals form an ordered \<open>semidom\<close> and a \<open>dioid\<close>\<close>
hoelzl@62376
   746
haftmann@35028
   747
instance nat :: linordered_semidom
paulson@14341
   748
proof
wenzelm@63110
   749
  fix m n q :: nat
paulson@14348
   750
  show "0 < (1::nat)" by simp
wenzelm@63110
   751
  show "m \<le> n \<Longrightarrow> q + m \<le> q + n" by simp
wenzelm@63110
   752
  show "m < n \<Longrightarrow> 0 < q \<Longrightarrow> q * m < q * n" by (simp add: mult_less_mono2)
wenzelm@63110
   753
  show "m \<noteq> 0 \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> m * n \<noteq> 0" by simp
wenzelm@63110
   754
  show "n \<le> m \<Longrightarrow> (m - n) + n = m"
lp15@60562
   755
    by (simp add: add_diff_inverse_nat add.commute linorder_not_less)
hoelzl@62376
   756
qed
hoelzl@62376
   757
hoelzl@62376
   758
instance nat :: dioid
wenzelm@63110
   759
  by standard (rule nat_le_iff_add)
hoelzl@62378
   760
declare le0[simp del] -- \<open>This is now @{thm zero_le}\<close>
hoelzl@62378
   761
declare le_0_eq[simp del] -- \<open>This is now @{thm le_zero_eq}\<close>
hoelzl@62378
   762
declare not_less0[simp del] -- \<open>This is now @{thm not_less_zero}\<close>
hoelzl@62378
   763
declare not_gr0[simp del] -- \<open>This is now @{thm not_gr_zero}\<close>
hoelzl@62376
   764
wenzelm@63110
   765
instance nat :: ordered_cancel_comm_monoid_add ..
wenzelm@63110
   766
instance nat :: ordered_cancel_comm_monoid_diff ..
wenzelm@63110
   767
haftmann@44817
   768
wenzelm@60758
   769
subsubsection \<open>@{term min} and @{term max}\<close>
haftmann@44817
   770
haftmann@44817
   771
lemma mono_Suc: "mono Suc"
wenzelm@63110
   772
  by (rule monoI) simp
wenzelm@63110
   773
wenzelm@63110
   774
lemma min_0L [simp]: "min 0 n = 0" for n :: nat
wenzelm@63110
   775
  by (rule min_absorb1) simp
wenzelm@63110
   776
wenzelm@63110
   777
lemma min_0R [simp]: "min n 0 = 0" for n :: nat
wenzelm@63110
   778
  by (rule min_absorb2) simp
haftmann@44817
   779
haftmann@44817
   780
lemma min_Suc_Suc [simp]: "min (Suc m) (Suc n) = Suc (min m n)"
wenzelm@63110
   781
  by (simp add: mono_Suc min_of_mono)
wenzelm@63110
   782
wenzelm@63110
   783
lemma min_Suc1: "min (Suc n) m = (case m of 0 \<Rightarrow> 0 | Suc m' \<Rightarrow> Suc(min n m'))"
wenzelm@63110
   784
  by (simp split: nat.split)
wenzelm@63110
   785
wenzelm@63110
   786
lemma min_Suc2: "min m (Suc n) = (case m of 0 \<Rightarrow> 0 | Suc m' \<Rightarrow> Suc(min m' n))"
wenzelm@63110
   787
  by (simp split: nat.split)
wenzelm@63110
   788
wenzelm@63110
   789
lemma max_0L [simp]: "max 0 n = n" for n :: nat
wenzelm@63110
   790
  by (rule max_absorb2) simp
wenzelm@63110
   791
wenzelm@63110
   792
lemma max_0R [simp]: "max n 0 = n" for n :: nat
wenzelm@63110
   793
  by (rule max_absorb1) simp
wenzelm@63110
   794
wenzelm@63110
   795
lemma max_Suc_Suc [simp]: "max (Suc m) (Suc n) = Suc (max m n)"
wenzelm@63110
   796
  by (simp add: mono_Suc max_of_mono)
wenzelm@63110
   797
wenzelm@63110
   798
lemma max_Suc1: "max (Suc n) m = (case m of 0 \<Rightarrow> Suc n | Suc m' \<Rightarrow> Suc (max n m'))"
wenzelm@63110
   799
  by (simp split: nat.split)
wenzelm@63110
   800
wenzelm@63110
   801
lemma max_Suc2: "max m (Suc n) = (case m of 0 \<Rightarrow> Suc n | Suc m' \<Rightarrow> Suc (max m' n))"
wenzelm@63110
   802
  by (simp split: nat.split)
wenzelm@63110
   803
wenzelm@63110
   804
lemma nat_mult_min_left: "min m n * q = min (m * q) (n * q)" for m n q :: nat
wenzelm@63110
   805
  by (simp add: min_def not_le)
wenzelm@63110
   806
    (auto dest: mult_right_le_imp_le mult_right_less_imp_less le_less_trans)
wenzelm@63110
   807
wenzelm@63110
   808
lemma nat_mult_min_right: "m * min n q = min (m * n) (m * q)" for m n q :: nat
wenzelm@63110
   809
  by (simp add: min_def not_le)
wenzelm@63110
   810
    (auto dest: mult_left_le_imp_le mult_left_less_imp_less le_less_trans)
wenzelm@63110
   811
wenzelm@63110
   812
lemma nat_add_max_left: "max m n + q = max (m + q) (n + q)" for m n q :: nat
haftmann@44817
   813
  by (simp add: max_def)
haftmann@44817
   814
wenzelm@63110
   815
lemma nat_add_max_right: "m + max n q = max (m + n) (m + q)" for m n q :: nat
haftmann@44817
   816
  by (simp add: max_def)
haftmann@44817
   817
wenzelm@63110
   818
lemma nat_mult_max_left: "max m n * q = max (m * q) (n * q)" for m n q :: nat
wenzelm@63110
   819
  by (simp add: max_def not_le)
wenzelm@63110
   820
    (auto dest: mult_right_le_imp_le mult_right_less_imp_less le_less_trans)
wenzelm@63110
   821
wenzelm@63110
   822
lemma nat_mult_max_right: "m * max n q = max (m * n) (m * q)" for m n q :: nat
wenzelm@63110
   823
  by (simp add: max_def not_le)
wenzelm@63110
   824
    (auto dest: mult_left_le_imp_le mult_left_less_imp_less le_less_trans)
paulson@14267
   825
paulson@14267
   826
wenzelm@60758
   827
subsubsection \<open>Additional theorems about @{term "op \<le>"}\<close>
wenzelm@60758
   828
wenzelm@60758
   829
text \<open>Complete induction, aka course-of-values induction\<close>
krauss@26748
   830
wenzelm@63110
   831
instance nat :: wellorder
wenzelm@63110
   832
proof
haftmann@27823
   833
  fix P and n :: nat
wenzelm@63110
   834
  assume step: "(\<And>m. m < n \<Longrightarrow> P m) \<Longrightarrow> P n" for n :: nat
haftmann@27823
   835
  have "\<And>q. q \<le> n \<Longrightarrow> P q"
haftmann@27823
   836
  proof (induct n)
haftmann@27823
   837
    case (0 n)
krauss@26748
   838
    have "P 0" by (rule step) auto
wenzelm@63110
   839
    then show ?case using 0 by auto
krauss@26748
   840
  next
haftmann@27823
   841
    case (Suc m n)
haftmann@27823
   842
    then have "n \<le> m \<or> n = Suc m" by (simp add: le_Suc_eq)
wenzelm@63110
   843
    then show ?case
krauss@26748
   844
    proof
wenzelm@63110
   845
      assume "n \<le> m"
wenzelm@63110
   846
      then show "P n" by (rule Suc(1))
krauss@26748
   847
    next
haftmann@27823
   848
      assume n: "n = Suc m"
wenzelm@63110
   849
      show "P n" by (rule step) (rule Suc(1), simp add: n le_simps)
krauss@26748
   850
    qed
krauss@26748
   851
  qed
haftmann@27823
   852
  then show "P n" by auto
krauss@26748
   853
qed
krauss@26748
   854
nipkow@57015
   855
wenzelm@63110
   856
lemma Least_eq_0[simp]: "P 0 \<Longrightarrow> Least P = 0" for P :: "nat \<Rightarrow> bool"
wenzelm@63110
   857
  by (rule Least_equality[OF _ le0])
wenzelm@63110
   858
wenzelm@63110
   859
lemma Least_Suc: "P n \<Longrightarrow> \<not> P 0 \<Longrightarrow> (LEAST n. P n) = Suc (LEAST m. P (Suc m))"
wenzelm@47988
   860
  apply (cases n, auto)
haftmann@27823
   861
  apply (frule LeastI)
wenzelm@63110
   862
  apply (drule_tac P = "\<lambda>x. P (Suc x) " in LeastI)
haftmann@27823
   863
  apply (subgoal_tac " (LEAST x. P x) \<le> Suc (LEAST x. P (Suc x))")
haftmann@27823
   864
  apply (erule_tac [2] Least_le)
wenzelm@47988
   865
  apply (cases "LEAST x. P x", auto)
wenzelm@63110
   866
  apply (drule_tac P = "\<lambda>x. P (Suc x) " in Least_le)
haftmann@27823
   867
  apply (blast intro: order_antisym)
haftmann@27823
   868
  done
haftmann@27823
   869
wenzelm@63110
   870
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
   871
  apply (erule (1) Least_Suc [THEN ssubst])
haftmann@27823
   872
  apply simp
haftmann@27823
   873
  done
haftmann@27823
   874
wenzelm@63110
   875
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
   876
  apply (cases n)
haftmann@27823
   877
   apply blast
wenzelm@63110
   878
  apply (rule_tac x="LEAST k. P k" in exI)
haftmann@27823
   879
  apply (blast intro: Least_le dest: not_less_Least intro: LeastI_ex)
haftmann@27823
   880
  done
haftmann@27823
   881
wenzelm@63110
   882
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
   883
  unfolding One_nat_def
haftmann@27823
   884
  apply (cases n)
haftmann@27823
   885
   apply blast
haftmann@27823
   886
  apply (frule (1) ex_least_nat_le)
haftmann@27823
   887
  apply (erule exE)
haftmann@27823
   888
  apply (case_tac k)
haftmann@27823
   889
   apply simp
haftmann@27823
   890
  apply (rename_tac k1)
haftmann@27823
   891
  apply (rule_tac x=k1 in exI)
haftmann@27823
   892
  apply (auto simp add: less_eq_Suc_le)
haftmann@27823
   893
  done
haftmann@27823
   894
krauss@26748
   895
lemma nat_less_induct:
wenzelm@63110
   896
  fixes P :: "nat \<Rightarrow> bool"
wenzelm@63110
   897
  assumes "\<And>n. \<forall>m. m < n \<longrightarrow> P m \<Longrightarrow> P n"
wenzelm@63110
   898
  shows "P n"
krauss@26748
   899
  using assms less_induct by blast
krauss@26748
   900
krauss@26748
   901
lemma measure_induct_rule [case_names less]:
krauss@26748
   902
  fixes f :: "'a \<Rightarrow> nat"
krauss@26748
   903
  assumes step: "\<And>x. (\<And>y. f y < f x \<Longrightarrow> P y) \<Longrightarrow> P x"
krauss@26748
   904
  shows "P a"
wenzelm@63110
   905
  by (induct m \<equiv> "f a" arbitrary: a rule: less_induct) (auto intro: step)
krauss@26748
   906
wenzelm@60758
   907
text \<open>old style induction rules:\<close>
krauss@26748
   908
lemma measure_induct:
krauss@26748
   909
  fixes f :: "'a \<Rightarrow> nat"
krauss@26748
   910
  shows "(\<And>x. \<forall>y. f y < f x \<longrightarrow> P y \<Longrightarrow> P x) \<Longrightarrow> P a"
krauss@26748
   911
  by (rule measure_induct_rule [of f P a]) iprover
krauss@26748
   912
krauss@26748
   913
lemma full_nat_induct:
wenzelm@63110
   914
  assumes step: "\<And>n. (\<forall>m. Suc m \<le> n \<longrightarrow> P m) \<Longrightarrow> P n"
krauss@26748
   915
  shows "P n"
krauss@26748
   916
  by (rule less_induct) (auto intro: step simp:le_simps)
paulson@14267
   917
wenzelm@63110
   918
text\<open>An induction rule for establishing binary relations\<close>
wenzelm@62683
   919
lemma less_Suc_induct [consumes 1]:
wenzelm@63110
   920
  assumes less: "i < j"
wenzelm@63110
   921
    and step: "\<And>i. P i (Suc i)"
wenzelm@63110
   922
    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
   923
  shows "P i j"
paulson@19870
   924
proof -
wenzelm@63110
   925
  from less obtain k where j: "j = Suc (i + k)"
wenzelm@63110
   926
    by (auto dest: less_imp_Suc_add)
wenzelm@22718
   927
  have "P i (Suc (i + k))"
paulson@19870
   928
  proof (induct k)
wenzelm@22718
   929
    case 0
wenzelm@22718
   930
    show ?case by (simp add: step)
paulson@19870
   931
  next
paulson@19870
   932
    case (Suc k)
krauss@31714
   933
    have "0 + i < Suc k + i" by (rule add_less_mono1) simp
wenzelm@63110
   934
    then have "i < Suc (i + k)" by (simp add: add.commute)
krauss@31714
   935
    from trans[OF this lessI Suc step]
krauss@31714
   936
    show ?case by simp
paulson@19870
   937
  qed
wenzelm@63110
   938
  then show "P i j" by (simp add: j)
paulson@19870
   939
qed
paulson@19870
   940
wenzelm@63111
   941
text \<open>
wenzelm@63111
   942
  The method of infinite descent, frequently used in number theory.
wenzelm@63111
   943
  Provided by Roelof Oosterhuis.
wenzelm@63111
   944
  \<open>P n\<close> is true for all natural numbers if
wenzelm@63111
   945
  \<^item> case ``0'': given \<open>n = 0\<close> prove \<open>P n\<close>
wenzelm@63111
   946
  \<^item> case ``smaller'': given \<open>n > 0\<close> and \<open>\<not> P n\<close> prove there exists
wenzelm@63111
   947
    a smaller natural number \<open>m\<close> such that \<open>\<not> P m\<close>.
wenzelm@63111
   948
\<close>
wenzelm@63111
   949
wenzelm@63110
   950
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
   951
  \<comment> \<open>compact version without explicit base case\<close>
wenzelm@63110
   952
  by (induct n rule: less_induct) auto
krauss@26748
   953
wenzelm@63111
   954
lemma infinite_descent0 [case_names 0 smaller]:
wenzelm@63110
   955
  fixes P :: "nat \<Rightarrow> bool"
wenzelm@63111
   956
  assumes "P 0"
wenzelm@63111
   957
    and "\<And>n. n > 0 \<Longrightarrow> \<not> P n \<Longrightarrow> \<exists>m. m < n \<and> \<not> P m"
wenzelm@63110
   958
  shows "P n"
wenzelm@63110
   959
  apply (rule infinite_descent)
wenzelm@63110
   960
  using assms
wenzelm@63110
   961
  apply (case_tac "n > 0")
wenzelm@63110
   962
  apply auto
wenzelm@63110
   963
  done
krauss@26748
   964
wenzelm@60758
   965
text \<open>
wenzelm@63111
   966
  Infinite descent using a mapping to \<open>nat\<close>:
wenzelm@63111
   967
  \<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
   968
  \<^item> case ``0'': given \<open>V x = 0\<close> prove \<open>P x\<close>
wenzelm@63111
   969
  \<^item> ``smaller'': given \<open>V x > 0\<close> and \<open>\<not> P x\<close> prove
wenzelm@63111
   970
  there exists a \<open>y \<in> D\<close> such that \<open>V y < V x\<close> and \<open>\<not> P y\<close>.
wenzelm@63111
   971
\<close>
krauss@26748
   972
corollary infinite_descent0_measure [case_names 0 smaller]:
wenzelm@63110
   973
  fixes V :: "'a \<Rightarrow> nat"
wenzelm@63110
   974
  assumes 1: "\<And>x. V x = 0 \<Longrightarrow> P x"
wenzelm@63110
   975
    and 2: "\<And>x. V x > 0 \<Longrightarrow> \<not> P x \<Longrightarrow> \<exists>y. V y < V x \<and> \<not> P y"
krauss@26748
   976
  shows "P x"
krauss@26748
   977
proof -
krauss@26748
   978
  obtain n where "n = V x" by auto
krauss@26748
   979
  moreover have "\<And>x. V x = n \<Longrightarrow> P x"
krauss@26748
   980
  proof (induct n rule: infinite_descent0)
wenzelm@63110
   981
    case 0
wenzelm@63110
   982
    with 1 show "P x" by auto
wenzelm@63110
   983
  next
krauss@26748
   984
    case (smaller n)
wenzelm@63110
   985
    then obtain x where *: "V x = n " and "V x > 0 \<and> \<not> P x" by auto
wenzelm@63110
   986
    with 2 obtain y where "V y < V x \<and> \<not> P y" by auto
wenzelm@63111
   987
    with * obtain m where "m = V y \<and> m < n \<and> \<not> P y" by auto
krauss@26748
   988
    then show ?case by auto
krauss@26748
   989
  qed
krauss@26748
   990
  ultimately show "P x" by auto
krauss@26748
   991
qed
krauss@26748
   992
wenzelm@60758
   993
text\<open>Again, without explicit base case:\<close>
krauss@26748
   994
lemma infinite_descent_measure:
wenzelm@63110
   995
  fixes V :: "'a \<Rightarrow> nat"
wenzelm@63110
   996
  assumes "\<And>x. \<not> P x \<Longrightarrow> \<exists>y. V y < V x \<and> \<not> P y"
wenzelm@63110
   997
  shows "P x"
krauss@26748
   998
proof -
krauss@26748
   999
  from assms obtain n where "n = V x" by auto
wenzelm@63110
  1000
  moreover have "\<And>x. V x = n \<Longrightarrow> P x"
krauss@26748
  1001
  proof (induct n rule: infinite_descent, auto)
wenzelm@63111
  1002
    show "\<exists>m < V x. \<exists>y. V y = m \<and> \<not> P y" if "\<not> P x" for x
wenzelm@63111
  1003
      using assms and that by auto
krauss@26748
  1004
  qed
krauss@26748
  1005
  ultimately show "P x" by auto
krauss@26748
  1006
qed
krauss@26748
  1007
wenzelm@63111
  1008
text \<open>A (clumsy) way of lifting \<open><\<close> monotonicity to \<open>\<le>\<close> monotonicity\<close>
paulson@14267
  1009
lemma less_mono_imp_le_mono:
wenzelm@63110
  1010
  fixes f :: "nat \<Rightarrow> nat"
wenzelm@63110
  1011
    and i j :: nat
wenzelm@63110
  1012
  assumes "\<And>i j::nat. i < j \<Longrightarrow> f i < f j"
wenzelm@63110
  1013
    and "i \<le> j"
wenzelm@63110
  1014
  shows "f i \<le> f j"
wenzelm@63110
  1015
  using assms by (auto simp add: order_le_less)
nipkow@24438
  1016
paulson@14267
  1017
wenzelm@60758
  1018
text \<open>non-strict, in 1st argument\<close>
wenzelm@63110
  1019
lemma add_le_mono1: "i \<le> j \<Longrightarrow> i + k \<le> j + k" for i j k :: nat
wenzelm@63110
  1020
  by (rule add_right_mono)
paulson@14267
  1021
wenzelm@60758
  1022
text \<open>non-strict, in both arguments\<close>
wenzelm@63110
  1023
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
  1024
  by (rule add_mono)
wenzelm@63110
  1025
wenzelm@63110
  1026
lemma le_add2: "n \<le> m + n" for m n :: nat
haftmann@62608
  1027
  by simp
berghofe@13449
  1028
wenzelm@63110
  1029
lemma le_add1: "n \<le> n + m" for m n :: nat
haftmann@62608
  1030
  by simp
berghofe@13449
  1031
berghofe@13449
  1032
lemma less_add_Suc1: "i < Suc (i + m)"
wenzelm@63110
  1033
  by (rule le_less_trans, rule le_add1, rule lessI)
berghofe@13449
  1034
berghofe@13449
  1035
lemma less_add_Suc2: "i < Suc (m + i)"
wenzelm@63110
  1036
  by (rule le_less_trans, rule le_add2, rule lessI)
wenzelm@63110
  1037
wenzelm@63110
  1038
lemma less_iff_Suc_add: "m < n \<longleftrightarrow> (\<exists>k. n = Suc (m + k))"
wenzelm@63110
  1039
  by (iprover intro!: less_add_Suc1 less_imp_Suc_add)
wenzelm@63110
  1040
wenzelm@63110
  1041
lemma trans_le_add1: "i \<le> j \<Longrightarrow> i \<le> j + m" for i j m :: nat
wenzelm@63110
  1042
  by (rule le_trans, assumption, rule le_add1)
wenzelm@63110
  1043
wenzelm@63110
  1044
lemma trans_le_add2: "i \<le> j \<Longrightarrow> i \<le> m + j" for i j m :: nat
wenzelm@63110
  1045
  by (rule le_trans, assumption, rule le_add2)
wenzelm@63110
  1046
wenzelm@63110
  1047
lemma trans_less_add1: "i < j \<Longrightarrow> i < j + m" for i j m :: nat
wenzelm@63110
  1048
  by (rule less_le_trans, assumption, rule le_add1)
wenzelm@63110
  1049
wenzelm@63110
  1050
lemma trans_less_add2: "i < j \<Longrightarrow> i < m + j" for i j m :: nat
wenzelm@63110
  1051
  by (rule less_le_trans, assumption, rule le_add2)
wenzelm@63110
  1052
wenzelm@63110
  1053
lemma add_lessD1: "i + j < k \<Longrightarrow> i < k" for i j k :: nat
wenzelm@63110
  1054
  by (rule le_less_trans [of _ "i+j"]) (simp_all add: le_add1)
wenzelm@63110
  1055
wenzelm@63110
  1056
lemma not_add_less1 [iff]: "\<not> i + j < i" for i j :: nat
wenzelm@63110
  1057
  apply (rule notI)
wenzelm@63110
  1058
  apply (drule add_lessD1)
wenzelm@63110
  1059
  apply (erule less_irrefl [THEN notE])
wenzelm@63110
  1060
  done
wenzelm@63110
  1061
wenzelm@63110
  1062
lemma not_add_less2 [iff]: "\<not> j + i < i" for i j :: nat
wenzelm@63110
  1063
  by (simp add: add.commute)
wenzelm@63110
  1064
wenzelm@63110
  1065
lemma add_leD1: "m + k \<le> n \<Longrightarrow> m \<le> n" for k m n :: nat
wenzelm@63110
  1066
  by (rule order_trans [of _ "m+k"]) (simp_all add: le_add1)
wenzelm@63110
  1067
wenzelm@63110
  1068
lemma add_leD2: "m + k \<le> n \<Longrightarrow> k \<le> n" for k m n :: nat
wenzelm@63110
  1069
  apply (simp add: add.commute)
wenzelm@63110
  1070
  apply (erule add_leD1)
wenzelm@63110
  1071
  done
wenzelm@63110
  1072
wenzelm@63110
  1073
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
  1074
  by (blast dest: add_leD1 add_leD2)
wenzelm@63110
  1075
wenzelm@63110
  1076
text \<open>needs \<open>\<And>k\<close> for \<open>ac_simps\<close> to work\<close>
wenzelm@63110
  1077
lemma less_add_eq_less: "\<And>k::nat. k < l \<Longrightarrow> m + l = k + n \<Longrightarrow> m < n"
wenzelm@63110
  1078
  by (force simp del: add_Suc_right simp add: less_iff_Suc_add add_Suc_right [symmetric] ac_simps)
berghofe@13449
  1079
berghofe@13449
  1080
wenzelm@60758
  1081
subsubsection \<open>More results about difference\<close>
berghofe@13449
  1082
wenzelm@63110
  1083
lemma Suc_diff_le: "n \<le> m \<Longrightarrow> Suc m - n = Suc (m - n)"
wenzelm@63110
  1084
  by (induct m n rule: diff_induct) simp_all
berghofe@13449
  1085
berghofe@13449
  1086
lemma diff_less_Suc: "m - n < Suc m"
nipkow@24438
  1087
apply (induct m n rule: diff_induct)
nipkow@24438
  1088
apply (erule_tac [3] less_SucE)
nipkow@24438
  1089
apply (simp_all add: less_Suc_eq)
nipkow@24438
  1090
done
berghofe@13449
  1091
wenzelm@63110
  1092
lemma diff_le_self [simp]: "m - n \<le> m" for m n :: nat
wenzelm@63110
  1093
  by (induct m n rule: diff_induct) (simp_all add: le_SucI)
wenzelm@63110
  1094
wenzelm@63110
  1095
lemma less_imp_diff_less: "j < k \<Longrightarrow> j - n < k" for j k n :: nat
wenzelm@63110
  1096
  by (rule le_less_trans, rule diff_le_self)
wenzelm@63110
  1097
wenzelm@63110
  1098
lemma diff_Suc_less [simp]: "0 < n \<Longrightarrow> n - Suc i < n"
wenzelm@63110
  1099
  by (cases n) (auto simp add: le_simps)
wenzelm@63110
  1100
wenzelm@63110
  1101
lemma diff_add_assoc: "k \<le> j \<Longrightarrow> (i + j) - k = i + (j - k)" for i j k :: nat
wenzelm@63110
  1102
  by (induct j k rule: diff_induct) simp_all
wenzelm@63110
  1103
wenzelm@63110
  1104
lemma add_diff_assoc [simp]: "k \<le> j \<Longrightarrow> i + (j - k) = i + j - k" for i j k :: nat
haftmann@62481
  1105
  by (fact diff_add_assoc [symmetric])
haftmann@62481
  1106
wenzelm@63110
  1107
lemma diff_add_assoc2: "k \<le> j \<Longrightarrow> (j + i) - k = (j - k) + i" for i j k :: nat
haftmann@62481
  1108
  by (simp add: ac_simps)
haftmann@62481
  1109
wenzelm@63110
  1110
lemma add_diff_assoc2 [simp]: "k \<le> j \<Longrightarrow> j - k + i = j + i - k" for i j k :: nat
haftmann@62481
  1111
  by (fact diff_add_assoc2 [symmetric])
berghofe@13449
  1112
wenzelm@63110
  1113
lemma le_imp_diff_is_add: "i \<le> j \<Longrightarrow> (j - i = k) = (j = k + i)" for i j k :: nat
wenzelm@63110
  1114
  by auto
wenzelm@63110
  1115
wenzelm@63110
  1116
lemma diff_is_0_eq [simp]: "m - n = 0 \<longleftrightarrow> m \<le> n" for m n :: nat
wenzelm@63110
  1117
  by (induct m n rule: diff_induct) simp_all
wenzelm@63110
  1118
wenzelm@63110
  1119
lemma diff_is_0_eq' [simp]: "m \<le> n \<Longrightarrow> m - n = 0" for m n :: nat
wenzelm@63110
  1120
  by (rule iffD2, rule diff_is_0_eq)
wenzelm@63110
  1121
wenzelm@63110
  1122
lemma zero_less_diff [simp]: "0 < n - m \<longleftrightarrow> m < n" for m n :: nat
wenzelm@63110
  1123
  by (induct m n rule: diff_induct) simp_all
berghofe@13449
  1124
wenzelm@22718
  1125
lemma less_imp_add_positive:
wenzelm@22718
  1126
  assumes "i < j"
wenzelm@63110
  1127
  shows "\<exists>k::nat. 0 < k \<and> i + k = j"
wenzelm@22718
  1128
proof
wenzelm@63110
  1129
  from assms show "0 < j - i \<and> i + (j - i) = j"
huffman@23476
  1130
    by (simp add: order_less_imp_le)
wenzelm@22718
  1131
qed
wenzelm@9436
  1132
wenzelm@60758
  1133
text \<open>a nice rewrite for bounded subtraction\<close>
wenzelm@63110
  1134
lemma nat_minus_add_max: "n - m + m = max n m" for m n :: nat
haftmann@26072
  1135
    by (simp add: max_def not_le order_less_imp_le)
berghofe@13449
  1136
wenzelm@63110
  1137
lemma nat_diff_split: "P (a - b) \<longleftrightarrow> (a < b \<longrightarrow> P 0) \<and> (\<forall>d. a = b + d \<longrightarrow> P d)"
wenzelm@63110
  1138
  for a b :: nat
wenzelm@61799
  1139
    \<comment> \<open>elimination of \<open>-\<close> on \<open>nat\<close>\<close>
haftmann@62365
  1140
  by (cases "a < b")
haftmann@62365
  1141
    (auto simp add: not_less le_less dest!: add_eq_self_zero [OF sym])
berghofe@13449
  1142
wenzelm@63110
  1143
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
  1144
  for a b :: nat
wenzelm@61799
  1145
    \<comment> \<open>elimination of \<open>-\<close> on \<open>nat\<close> in assumptions\<close>
haftmann@62365
  1146
  by (auto split: nat_diff_split)
berghofe@13449
  1147
wenzelm@63110
  1148
lemma Suc_pred': "0 < n \<Longrightarrow> n = Suc(n - 1)"
huffman@47255
  1149
  by simp
huffman@47255
  1150
wenzelm@63110
  1151
lemma add_eq_if: "m + n = (if m = 0 then n else Suc ((m - 1) + n))"
huffman@47255
  1152
  unfolding One_nat_def by (cases m) simp_all
huffman@47255
  1153
wenzelm@63110
  1154
lemma mult_eq_if: "m * n = (if m = 0 then 0 else n + ((m - 1) * n))" for m n :: nat
huffman@47255
  1155
  unfolding One_nat_def by (cases m) simp_all
huffman@47255
  1156
wenzelm@63110
  1157
lemma Suc_diff_eq_diff_pred: "0 < n \<Longrightarrow> Suc m - n = m - (n - 1)"
huffman@47255
  1158
  unfolding One_nat_def by (cases n) simp_all
huffman@47255
  1159
huffman@47255
  1160
lemma diff_Suc_eq_diff_pred: "m - Suc n = (m - 1) - n"
huffman@47255
  1161
  unfolding One_nat_def by (cases m) simp_all
huffman@47255
  1162
huffman@47255
  1163
lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
huffman@47255
  1164
  by (fact Let_def)
huffman@47255
  1165
berghofe@13449
  1166
wenzelm@60758
  1167
subsubsection \<open>Monotonicity of multiplication\<close>
berghofe@13449
  1168
wenzelm@63110
  1169
lemma mult_le_mono1: "i \<le> j \<Longrightarrow> i * k \<le> j * k" for i j k :: nat
wenzelm@63110
  1170
  by (simp add: mult_right_mono)
wenzelm@63110
  1171
wenzelm@63110
  1172
lemma mult_le_mono2: "i \<le> j \<Longrightarrow> k * i \<le> k * j" for i j k :: nat
wenzelm@63110
  1173
  by (simp add: mult_left_mono)
berghofe@13449
  1174
wenzelm@61799
  1175
text \<open>\<open>\<le>\<close> monotonicity, BOTH arguments\<close>
wenzelm@63110
  1176
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
  1177
  by (simp add: mult_mono)
wenzelm@63110
  1178
wenzelm@63110
  1179
lemma mult_less_mono1: "i < j \<Longrightarrow> 0 < k \<Longrightarrow> i * k < j * k" for i j k :: nat
wenzelm@63110
  1180
  by (simp add: mult_strict_right_mono)
berghofe@13449
  1181
wenzelm@61799
  1182
text\<open>Differs from the standard \<open>zero_less_mult_iff\<close> in that
wenzelm@60758
  1183
      there are no negative numbers.\<close>
wenzelm@63110
  1184
lemma nat_0_less_mult_iff [simp]: "0 < m * n \<longleftrightarrow> 0 < m \<and> 0 < n" for m n :: nat
berghofe@13449
  1185
  apply (induct m)
wenzelm@22718
  1186
   apply simp
wenzelm@22718
  1187
  apply (case_tac n)
wenzelm@22718
  1188
   apply simp_all
berghofe@13449
  1189
  done
berghofe@13449
  1190
wenzelm@63110
  1191
lemma one_le_mult_iff [simp]: "Suc 0 \<le> m * n \<longleftrightarrow> Suc 0 \<le> m \<and> Suc 0 \<le> n"
berghofe@13449
  1192
  apply (induct m)
wenzelm@22718
  1193
   apply simp
wenzelm@22718
  1194
  apply (case_tac n)
wenzelm@22718
  1195
   apply simp_all
berghofe@13449
  1196
  done
berghofe@13449
  1197
wenzelm@63110
  1198
lemma mult_less_cancel2 [simp]: "m * k < n * k \<longleftrightarrow> 0 < k \<and> m < n" for k m n :: nat
berghofe@13449
  1199
  apply (safe intro!: mult_less_mono1)
wenzelm@47988
  1200
  apply (cases k, auto)
wenzelm@63110
  1201
  apply (simp add: linorder_not_le [symmetric])
berghofe@13449
  1202
  apply (blast intro: mult_le_mono1)
berghofe@13449
  1203
  done
berghofe@13449
  1204
wenzelm@63110
  1205
lemma mult_less_cancel1 [simp]: "k * m < k * n \<longleftrightarrow> 0 < k \<and> m < n" for k m n :: nat
wenzelm@63110
  1206
  by (simp add: mult.commute [of k])
wenzelm@63110
  1207
wenzelm@63110
  1208
lemma mult_le_cancel1 [simp]: "k * m \<le> k * n \<longleftrightarrow> (0 < k \<longrightarrow> m \<le> n)" for k m n :: nat
wenzelm@63110
  1209
  by (simp add: linorder_not_less [symmetric], auto)
wenzelm@63110
  1210
wenzelm@63110
  1211
lemma mult_le_cancel2 [simp]: "m * k \<le> n * k \<longleftrightarrow> (0 < k \<longrightarrow> m \<le> n)" for k m n :: nat
wenzelm@63110
  1212
  by (simp add: linorder_not_less [symmetric], auto)
wenzelm@63110
  1213
wenzelm@63110
  1214
lemma Suc_mult_less_cancel1: "Suc k * m < Suc k * n \<longleftrightarrow> m < n"
wenzelm@63110
  1215
  by (subst mult_less_cancel1) simp
wenzelm@63110
  1216
wenzelm@63110
  1217
lemma Suc_mult_le_cancel1: "Suc k * m \<le> Suc k * n \<longleftrightarrow> m \<le> n"
wenzelm@63110
  1218
  by (subst mult_le_cancel1) simp
wenzelm@63110
  1219
wenzelm@63110
  1220
lemma le_square: "m \<le> m * m" for m :: nat
haftmann@26072
  1221
  by (cases m) (auto intro: le_add1)
haftmann@26072
  1222
wenzelm@63110
  1223
lemma le_cube: "m \<le> m * (m * m)" for m :: nat
haftmann@26072
  1224
  by (cases m) (auto intro: le_add1)
berghofe@13449
  1225
wenzelm@61799
  1226
text \<open>Lemma for \<open>gcd\<close>\<close>
wenzelm@63110
  1227
lemma mult_eq_self_implies_10: "m = m * n \<Longrightarrow> n = 1 \<or> m = 0" for m n :: nat
berghofe@13449
  1228
  apply (drule sym)
berghofe@13449
  1229
  apply (rule disjCI)
wenzelm@63113
  1230
  apply (rule linorder_cases, erule_tac [2] _)
paulson@25157
  1231
   apply (drule_tac [2] mult_less_mono2)
nipkow@25162
  1232
    apply (auto)
berghofe@13449
  1233
  done
wenzelm@9436
  1234
haftmann@51263
  1235
lemma mono_times_nat:
haftmann@51263
  1236
  fixes n :: nat
haftmann@51263
  1237
  assumes "n > 0"
haftmann@51263
  1238
  shows "mono (times n)"
haftmann@51263
  1239
proof
haftmann@51263
  1240
  fix m q :: nat
haftmann@51263
  1241
  assume "m \<le> q"
haftmann@51263
  1242
  with assms show "n * m \<le> n * q" by simp
haftmann@51263
  1243
qed
haftmann@51263
  1244
wenzelm@60758
  1245
text \<open>the lattice order on @{typ nat}\<close>
haftmann@24995
  1246
haftmann@26072
  1247
instantiation nat :: distrib_lattice
haftmann@26072
  1248
begin
haftmann@24995
  1249
wenzelm@63110
  1250
definition "(inf :: nat \<Rightarrow> nat \<Rightarrow> nat) = min"
wenzelm@63110
  1251
wenzelm@63110
  1252
definition "(sup :: nat \<Rightarrow> nat \<Rightarrow> nat) = max"
wenzelm@63110
  1253
wenzelm@63110
  1254
instance
wenzelm@63110
  1255
  by intro_classes
wenzelm@63110
  1256
    (auto simp add: inf_nat_def sup_nat_def max_def not_le min_def
wenzelm@63110
  1257
      intro: order_less_imp_le antisym elim!: order_trans order_less_trans)
haftmann@24995
  1258
haftmann@26072
  1259
end
haftmann@24995
  1260
haftmann@24995
  1261
wenzelm@60758
  1262
subsection \<open>Natural operation of natural numbers on functions\<close>
wenzelm@60758
  1263
wenzelm@60758
  1264
text \<open>
haftmann@30971
  1265
  We use the same logical constant for the power operations on
haftmann@30971
  1266
  functions and relations, in order to share the same syntax.
wenzelm@60758
  1267
\<close>
haftmann@30971
  1268
haftmann@45965
  1269
consts compow :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@30971
  1270
wenzelm@63110
  1271
abbreviation compower :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixr "^^" 80)
wenzelm@63110
  1272
  where "f ^^ n \<equiv> compow n f"
haftmann@30971
  1273
haftmann@30971
  1274
notation (latex output)
haftmann@30971
  1275
  compower ("(_\<^bsup>_\<^esup>)" [1000] 1000)
haftmann@30971
  1276
wenzelm@61799
  1277
text \<open>\<open>f ^^ n = f o ... o f\<close>, the n-fold composition of \<open>f\<close>\<close>
haftmann@30971
  1278
haftmann@30971
  1279
overloading
wenzelm@63110
  1280
  funpow \<equiv> "compow :: nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a)"
haftmann@30971
  1281
begin
haftmann@30954
  1282
blanchet@55575
  1283
primrec funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@44325
  1284
  "funpow 0 f = id"
haftmann@44325
  1285
| "funpow (Suc n) f = f o funpow n f"
haftmann@30954
  1286
haftmann@30971
  1287
end
haftmann@30971
  1288
lp15@62217
  1289
lemma funpow_0 [simp]: "(f ^^ 0) x = x"
lp15@62217
  1290
  by simp
lp15@62217
  1291
wenzelm@63110
  1292
lemma funpow_Suc_right: "f ^^ Suc n = f ^^ n \<circ> f"
haftmann@49723
  1293
proof (induct n)
wenzelm@63110
  1294
  case 0
wenzelm@63110
  1295
  then show ?case by simp
haftmann@49723
  1296
next
haftmann@49723
  1297
  fix n
haftmann@49723
  1298
  assume "f ^^ Suc n = f ^^ n \<circ> f"
haftmann@49723
  1299
  then show "f ^^ Suc (Suc n) = f ^^ Suc n \<circ> f"
haftmann@49723
  1300
    by (simp add: o_assoc)
haftmann@49723
  1301
qed
haftmann@49723
  1302
haftmann@49723
  1303
lemmas funpow_simps_right = funpow.simps(1) funpow_Suc_right
haftmann@49723
  1304
wenzelm@60758
  1305
text \<open>for code generation\<close>
haftmann@30971
  1306
wenzelm@63110
  1307
definition funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
wenzelm@63110
  1308
  where funpow_code_def [code_abbrev]: "funpow = compow"
haftmann@30954
  1309
haftmann@30971
  1310
lemma [code]:
wenzelm@63110
  1311
  "funpow (Suc n) f = f \<circ> funpow n f"
haftmann@30971
  1312
  "funpow 0 f = id"
haftmann@37430
  1313
  by (simp_all add: funpow_code_def)
haftmann@30971
  1314
wenzelm@36176
  1315
hide_const (open) funpow
haftmann@30954
  1316
wenzelm@63110
  1317
lemma funpow_add: "f ^^ (m + n) = f ^^ m \<circ> f ^^ n"
haftmann@30954
  1318
  by (induct m) simp_all
haftmann@30954
  1319
wenzelm@63110
  1320
lemma funpow_mult: "(f ^^ m) ^^ n = f ^^ (m * n)" for f :: "'a \<Rightarrow> 'a"
haftmann@37430
  1321
  by (induct n) (simp_all add: funpow_add)
haftmann@37430
  1322
wenzelm@63110
  1323
lemma funpow_swap1: "f ((f ^^ n) x) = (f ^^ n) (f x)"
haftmann@30954
  1324
proof -
haftmann@30971
  1325
  have "f ((f ^^ n) x) = (f ^^ (n + 1)) x" by simp
haftmann@30971
  1326
  also have "\<dots>  = (f ^^ n o f ^^ 1) x" by (simp only: funpow_add)
haftmann@30971
  1327
  also have "\<dots> = (f ^^ n) (f x)" by simp
haftmann@30954
  1328
  finally show ?thesis .
haftmann@30954
  1329
qed
haftmann@30954
  1330
wenzelm@63110
  1331
lemma comp_funpow: "comp f ^^ n = comp (f ^^ n)" for f :: "'a \<Rightarrow> 'a"
haftmann@38621
  1332
  by (induct n) simp_all
haftmann@30954
  1333
hoelzl@54496
  1334
lemma Suc_funpow[simp]: "Suc ^^ n = (op + n)"
hoelzl@54496
  1335
  by (induct n) simp_all
hoelzl@54496
  1336
hoelzl@54496
  1337
lemma id_funpow[simp]: "id ^^ n = id"
hoelzl@54496
  1338
  by (induct n) simp_all
haftmann@38621
  1339
wenzelm@63110
  1340
lemma funpow_mono: "mono f \<Longrightarrow> A \<le> B \<Longrightarrow> (f ^^ n) A \<le> (f ^^ n) B"
wenzelm@63110
  1341
  for f :: "'a \<Rightarrow> ('a::lattice)"
hoelzl@59000
  1342
  by (induct n arbitrary: A B)
hoelzl@59000
  1343
     (auto simp del: funpow.simps(2) simp add: funpow_Suc_right mono_def)
hoelzl@59000
  1344
wenzelm@63110
  1345
wenzelm@60758
  1346
subsection \<open>Kleene iteration\<close>
nipkow@45833
  1347
haftmann@52729
  1348
lemma Kleene_iter_lpfp:
wenzelm@63110
  1349
  assumes "mono f"
wenzelm@63110
  1350
    and "f p \<le> p"
wenzelm@63110
  1351
  shows "(f^^k) (bot::'a::order_bot) \<le> p"
nipkow@45833
  1352
proof(induction k)
wenzelm@63110
  1353
  case 0
wenzelm@63110
  1354
  show ?case by simp
nipkow@45833
  1355
next
nipkow@45833
  1356
  case Suc
wenzelm@63110
  1357
  from monoD[OF assms(1) Suc] assms(2) show ?case by simp
nipkow@45833
  1358
qed
nipkow@45833
  1359
wenzelm@63110
  1360
lemma lfp_Kleene_iter:
wenzelm@63110
  1361
  assumes "mono f"
wenzelm@63110
  1362
    and "(f^^Suc k) bot = (f^^k) bot"
wenzelm@63110
  1363
  shows "lfp f = (f^^k) bot"
wenzelm@63110
  1364
proof (rule antisym)
nipkow@45833
  1365
  show "lfp f \<le> (f^^k) bot"
wenzelm@63110
  1366
  proof (rule lfp_lowerbound)
wenzelm@63110
  1367
    show "f ((f^^k) bot) \<le> (f^^k) bot"
wenzelm@63110
  1368
      using assms(2) by simp
nipkow@45833
  1369
  qed
nipkow@45833
  1370
  show "(f^^k) bot \<le> lfp f"
nipkow@45833
  1371
    using Kleene_iter_lpfp[OF assms(1)] lfp_unfold[OF assms(1)] by simp
nipkow@45833
  1372
qed
nipkow@45833
  1373
wenzelm@63110
  1374
lemma mono_pow: "mono f \<Longrightarrow> mono (f ^^ n)" for f :: "'a \<Rightarrow> 'a::complete_lattice"
wenzelm@63110
  1375
  by (induct n) (auto simp: mono_def)
hoelzl@60636
  1376
hoelzl@60636
  1377
lemma lfp_funpow:
wenzelm@63110
  1378
  assumes f: "mono f"
wenzelm@63110
  1379
  shows "lfp (f ^^ Suc n) = lfp f"
hoelzl@60636
  1380
proof (rule antisym)
hoelzl@60636
  1381
  show "lfp f \<le> lfp (f ^^ Suc n)"
hoelzl@60636
  1382
  proof (rule lfp_lowerbound)
hoelzl@60636
  1383
    have "f (lfp (f ^^ Suc n)) = lfp (\<lambda>x. f ((f ^^ n) x))"
hoelzl@60636
  1384
      unfolding funpow_Suc_right by (simp add: lfp_rolling f mono_pow comp_def)
hoelzl@60636
  1385
    then show "f (lfp (f ^^ Suc n)) \<le> lfp (f ^^ Suc n)"
hoelzl@60636
  1386
      by (simp add: comp_def)
hoelzl@60636
  1387
  qed
hoelzl@60636
  1388
  have "(f^^n) (lfp f) = lfp f" for n
wenzelm@63110
  1389
    by (induct n) (auto intro: f lfp_unfold[symmetric])
hoelzl@60636
  1390
  then show "lfp (f^^Suc n) \<le> lfp f"
hoelzl@60636
  1391
    by (intro lfp_lowerbound) (simp del: funpow.simps)
hoelzl@60636
  1392
qed
hoelzl@60636
  1393
hoelzl@60636
  1394
lemma gfp_funpow:
wenzelm@63110
  1395
  assumes f: "mono f"
wenzelm@63110
  1396
  shows "gfp (f ^^ Suc n) = gfp f"
hoelzl@60636
  1397
proof (rule antisym)
hoelzl@60636
  1398
  show "gfp f \<ge> gfp (f ^^ Suc n)"
hoelzl@60636
  1399
  proof (rule gfp_upperbound)
hoelzl@60636
  1400
    have "f (gfp (f ^^ Suc n)) = gfp (\<lambda>x. f ((f ^^ n) x))"
hoelzl@60636
  1401
      unfolding funpow_Suc_right by (simp add: gfp_rolling f mono_pow comp_def)
hoelzl@60636
  1402
    then show "f (gfp (f ^^ Suc n)) \<ge> gfp (f ^^ Suc n)"
hoelzl@60636
  1403
      by (simp add: comp_def)
hoelzl@60636
  1404
  qed
hoelzl@60636
  1405
  have "(f^^n) (gfp f) = gfp f" for n
wenzelm@63110
  1406
    by (induct n) (auto intro: f gfp_unfold[symmetric])
hoelzl@60636
  1407
  then show "gfp (f^^Suc n) \<ge> gfp f"
hoelzl@60636
  1408
    by (intro gfp_upperbound) (simp del: funpow.simps)
hoelzl@60636
  1409
qed
nipkow@45833
  1410
wenzelm@63110
  1411
wenzelm@61799
  1412
subsection \<open>Embedding of the naturals into any \<open>semiring_1\<close>: @{term of_nat}\<close>
haftmann@24196
  1413
haftmann@24196
  1414
context semiring_1
haftmann@24196
  1415
begin
haftmann@24196
  1416
wenzelm@63110
  1417
definition of_nat :: "nat \<Rightarrow> 'a"
wenzelm@63110
  1418
  where "of_nat n = (plus 1 ^^ n) 0"
haftmann@38621
  1419
haftmann@38621
  1420
lemma of_nat_simps [simp]:
haftmann@38621
  1421
  shows of_nat_0: "of_nat 0 = 0"
haftmann@38621
  1422
    and of_nat_Suc: "of_nat (Suc m) = 1 + of_nat m"
haftmann@38621
  1423
  by (simp_all add: of_nat_def)
haftmann@25193
  1424
haftmann@25193
  1425
lemma of_nat_1 [simp]: "of_nat 1 = 1"
haftmann@38621
  1426
  by (simp add: of_nat_def)
haftmann@25193
  1427
haftmann@25193
  1428
lemma of_nat_add [simp]: "of_nat (m + n) = of_nat m + of_nat n"
haftmann@57514
  1429
  by (induct m) (simp_all add: ac_simps)
haftmann@25193
  1430
lp15@61649
  1431
lemma of_nat_mult [simp]: "of_nat (m * n) = of_nat m * of_nat n"
haftmann@57514
  1432
  by (induct m) (simp_all add: ac_simps distrib_right)
haftmann@25193
  1433
eberlm@61531
  1434
lemma mult_of_nat_commute: "of_nat x * y = y * of_nat x"
wenzelm@63110
  1435
  by (induct x) (simp_all add: algebra_simps)
eberlm@61531
  1436
blanchet@55575
  1437
primrec of_nat_aux :: "('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@28514
  1438
  "of_nat_aux inc 0 i = i"
wenzelm@61799
  1439
| "of_nat_aux inc (Suc n) i = of_nat_aux inc n (inc i)" \<comment> \<open>tail recursive\<close>
haftmann@25928
  1440
wenzelm@63110
  1441
lemma of_nat_code: "of_nat n = of_nat_aux (\<lambda>i. i + 1) n 0"
haftmann@28514
  1442
proof (induct n)
wenzelm@63110
  1443
  case 0
wenzelm@63110
  1444
  then show ?case by simp
haftmann@28514
  1445
next
haftmann@28514
  1446
  case (Suc n)
haftmann@28514
  1447
  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
  1448
    by (induct n) simp_all
haftmann@28514
  1449
  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
  1450
    by simp
haftmann@57512
  1451
  with Suc show ?case by (simp add: add.commute)
haftmann@28514
  1452
qed
haftmann@30966
  1453
haftmann@24196
  1454
end
haftmann@24196
  1455
bulwahn@45231
  1456
declare of_nat_code [code]
haftmann@30966
  1457
haftmann@62481
  1458
context ring_1
haftmann@62481
  1459
begin
haftmann@62481
  1460
haftmann@62481
  1461
lemma of_nat_diff: "n \<le> m \<Longrightarrow> of_nat (m - n) = of_nat m - of_nat n"
wenzelm@63110
  1462
  by (simp add: algebra_simps of_nat_add [symmetric])
haftmann@62481
  1463
haftmann@62481
  1464
end
haftmann@62481
  1465
wenzelm@63110
  1466
text \<open>Class for unital semirings with characteristic zero.
wenzelm@60758
  1467
 Includes non-ordered rings like the complex numbers.\<close>
haftmann@26072
  1468
haftmann@26072
  1469
class semiring_char_0 = semiring_1 +
haftmann@38621
  1470
  assumes inj_of_nat: "inj of_nat"
haftmann@26072
  1471
begin
haftmann@26072
  1472
haftmann@38621
  1473
lemma of_nat_eq_iff [simp]: "of_nat m = of_nat n \<longleftrightarrow> m = n"
haftmann@38621
  1474
  by (auto intro: inj_of_nat injD)
haftmann@38621
  1475
wenzelm@63110
  1476
text \<open>Special cases where either operand is zero\<close>
haftmann@26072
  1477
blanchet@54147
  1478
lemma of_nat_0_eq_iff [simp]: "0 = of_nat n \<longleftrightarrow> 0 = n"
haftmann@38621
  1479
  by (fact of_nat_eq_iff [of 0 n, unfolded of_nat_0])
haftmann@26072
  1480
blanchet@54147
  1481
lemma of_nat_eq_0_iff [simp]: "of_nat m = 0 \<longleftrightarrow> m = 0"
haftmann@38621
  1482
  by (fact of_nat_eq_iff [of m 0, unfolded of_nat_0])
haftmann@26072
  1483
haftmann@60353
  1484
lemma of_nat_neq_0 [simp]:
haftmann@60353
  1485
  "of_nat (Suc n) \<noteq> 0"
haftmann@60353
  1486
  unfolding of_nat_eq_0_iff by simp
haftmann@60353
  1487
haftmann@60353
  1488
lemma of_nat_0_neq [simp]:
haftmann@60353
  1489
  "0 \<noteq> of_nat (Suc n)"
lp15@60562
  1490
  unfolding of_nat_0_eq_iff by simp
lp15@60562
  1491
haftmann@26072
  1492
end
haftmann@26072
  1493
haftmann@62481
  1494
class ring_char_0 = ring_1 + semiring_char_0
haftmann@62481
  1495
haftmann@35028
  1496
context linordered_semidom
haftmann@25193
  1497
begin
haftmann@25193
  1498
huffman@47489
  1499
lemma of_nat_0_le_iff [simp]: "0 \<le> of_nat n"
huffman@47489
  1500
  by (induct n) simp_all
haftmann@25193
  1501
huffman@47489
  1502
lemma of_nat_less_0_iff [simp]: "\<not> of_nat m < 0"
huffman@47489
  1503
  by (simp add: not_less)
haftmann@25193
  1504
haftmann@25193
  1505
lemma of_nat_less_iff [simp]: "of_nat m < of_nat n \<longleftrightarrow> m < n"
hoelzl@62376
  1506
  by (induct m n rule: diff_induct) (simp_all add: add_pos_nonneg)
haftmann@25193
  1507
haftmann@26072
  1508
lemma of_nat_le_iff [simp]: "of_nat m \<le> of_nat n \<longleftrightarrow> m \<le> n"
haftmann@26072
  1509
  by (simp add: not_less [symmetric] linorder_not_less [symmetric])
haftmann@25193
  1510
huffman@47489
  1511
lemma less_imp_of_nat_less: "m < n \<Longrightarrow> of_nat m < of_nat n"
huffman@47489
  1512
  by simp
huffman@47489
  1513
huffman@47489
  1514
lemma of_nat_less_imp_less: "of_nat m < of_nat n \<Longrightarrow> m < n"
huffman@47489
  1515
  by simp
huffman@47489
  1516
wenzelm@63110
  1517
text \<open>Every \<open>linordered_semidom\<close> has characteristic zero.\<close>
wenzelm@63110
  1518
wenzelm@63110
  1519
subclass semiring_char_0
wenzelm@63110
  1520
  by standard (auto intro!: injI simp add: eq_iff)
wenzelm@63110
  1521
wenzelm@63110
  1522
text \<open>Special cases where either operand is zero\<close>
haftmann@25193
  1523
blanchet@54147
  1524
lemma of_nat_le_0_iff [simp]: "of_nat m \<le> 0 \<longleftrightarrow> m = 0"
haftmann@25193
  1525
  by (rule of_nat_le_iff [of _ 0, simplified])
haftmann@25193
  1526
haftmann@26072
  1527
lemma of_nat_0_less_iff [simp]: "0 < of_nat n \<longleftrightarrow> 0 < n"
haftmann@26072
  1528
  by (rule of_nat_less_iff [of 0, simplified])
haftmann@26072
  1529
haftmann@26072
  1530
end
haftmann@26072
  1531
haftmann@35028
  1532
context linordered_idom
haftmann@26072
  1533
begin
haftmann@26072
  1534
haftmann@26072
  1535
lemma abs_of_nat [simp]: "\<bar>of_nat n\<bar> = of_nat n"
haftmann@26072
  1536
  unfolding abs_if by auto
haftmann@26072
  1537
haftmann@25193
  1538
end
haftmann@25193
  1539
haftmann@25193
  1540
lemma of_nat_id [simp]: "of_nat n = n"
huffman@35216
  1541
  by (induct n) simp_all
haftmann@25193
  1542
haftmann@25193
  1543
lemma of_nat_eq_id [simp]: "of_nat = id"
nipkow@39302
  1544
  by (auto simp add: fun_eq_iff)
haftmann@25193
  1545
haftmann@25193
  1546
wenzelm@60758
  1547
subsection \<open>The set of natural numbers\<close>
haftmann@25193
  1548
haftmann@26072
  1549
context semiring_1
haftmann@25193
  1550
begin
haftmann@25193
  1551
wenzelm@61070
  1552
definition Nats :: "'a set"  ("\<nat>")
wenzelm@61070
  1553
  where "\<nat> = range of_nat"
haftmann@25193
  1554
haftmann@26072
  1555
lemma of_nat_in_Nats [simp]: "of_nat n \<in> \<nat>"
haftmann@26072
  1556
  by (simp add: Nats_def)
haftmann@26072
  1557
haftmann@26072
  1558
lemma Nats_0 [simp]: "0 \<in> \<nat>"
haftmann@26072
  1559
apply (simp add: Nats_def)
haftmann@26072
  1560
apply (rule range_eqI)
haftmann@26072
  1561
apply (rule of_nat_0 [symmetric])
haftmann@26072
  1562
done
haftmann@25193
  1563
haftmann@26072
  1564
lemma Nats_1 [simp]: "1 \<in> \<nat>"
haftmann@26072
  1565
apply (simp add: Nats_def)
haftmann@26072
  1566
apply (rule range_eqI)
haftmann@26072
  1567
apply (rule of_nat_1 [symmetric])
haftmann@26072
  1568
done
haftmann@25193
  1569
haftmann@26072
  1570
lemma Nats_add [simp]: "a \<in> \<nat> \<Longrightarrow> b \<in> \<nat> \<Longrightarrow> a + b \<in> \<nat>"
haftmann@26072
  1571
apply (auto simp add: Nats_def)
haftmann@26072
  1572
apply (rule range_eqI)
haftmann@26072
  1573
apply (rule of_nat_add [symmetric])
haftmann@26072
  1574
done
haftmann@26072
  1575
haftmann@26072
  1576
lemma Nats_mult [simp]: "a \<in> \<nat> \<Longrightarrow> b \<in> \<nat> \<Longrightarrow> a * b \<in> \<nat>"
haftmann@26072
  1577
apply (auto simp add: Nats_def)
haftmann@26072
  1578
apply (rule range_eqI)
haftmann@26072
  1579
apply (rule of_nat_mult [symmetric])
haftmann@26072
  1580
done
haftmann@25193
  1581
huffman@35633
  1582
lemma Nats_cases [cases set: Nats]:
huffman@35633
  1583
  assumes "x \<in> \<nat>"
huffman@35633
  1584
  obtains (of_nat) n where "x = of_nat n"
huffman@35633
  1585
  unfolding Nats_def
huffman@35633
  1586
proof -
wenzelm@60758
  1587
  from \<open>x \<in> \<nat>\<close> have "x \<in> range of_nat" unfolding Nats_def .
huffman@35633
  1588
  then obtain n where "x = of_nat n" ..
huffman@35633
  1589
  then show thesis ..
huffman@35633
  1590
qed
huffman@35633
  1591
huffman@35633
  1592
lemma Nats_induct [case_names of_nat, induct set: Nats]:
huffman@35633
  1593
  "x \<in> \<nat> \<Longrightarrow> (\<And>n. P (of_nat n)) \<Longrightarrow> P x"
huffman@35633
  1594
  by (rule Nats_cases) auto
huffman@35633
  1595
haftmann@25193
  1596
end
haftmann@25193
  1597
haftmann@25193
  1598
wenzelm@60758
  1599
subsection \<open>Further arithmetic facts concerning the natural numbers\<close>
wenzelm@21243
  1600
haftmann@22845
  1601
lemma subst_equals:
wenzelm@63110
  1602
  assumes "t = s" and "u = t"
haftmann@22845
  1603
  shows "u = s"
wenzelm@63110
  1604
  using assms(2,1) by (rule trans)
haftmann@22845
  1605
wenzelm@48891
  1606
ML_file "Tools/nat_arith.ML"
huffman@48559
  1607
huffman@48559
  1608
simproc_setup nateq_cancel_sums
huffman@48559
  1609
  ("(l::nat) + m = n" | "(l::nat) = m + n" | "Suc m = n" | "m = Suc n") =
wenzelm@60758
  1610
  \<open>fn phi => try o Nat_Arith.cancel_eq_conv\<close>
huffman@48559
  1611
huffman@48559
  1612
simproc_setup natless_cancel_sums
huffman@48559
  1613
  ("(l::nat) + m < n" | "(l::nat) < m + n" | "Suc m < n" | "m < Suc n") =
wenzelm@60758
  1614
  \<open>fn phi => try o Nat_Arith.cancel_less_conv\<close>
huffman@48559
  1615
huffman@48559
  1616
simproc_setup natle_cancel_sums
huffman@48559
  1617
  ("(l::nat) + m \<le> n" | "(l::nat) \<le> m + n" | "Suc m \<le> n" | "m \<le> Suc n") =
wenzelm@60758
  1618
  \<open>fn phi => try o Nat_Arith.cancel_le_conv\<close>
huffman@48559
  1619
huffman@48559
  1620
simproc_setup natdiff_cancel_sums
huffman@48559
  1621
  ("(l::nat) + m - n" | "(l::nat) - (m + n)" | "Suc m - n" | "m - Suc n") =
wenzelm@60758
  1622
  \<open>fn phi => try o Nat_Arith.cancel_diff_conv\<close>
wenzelm@24091
  1623
nipkow@27625
  1624
context order
nipkow@27625
  1625
begin
nipkow@27625
  1626
nipkow@27625
  1627
lemma lift_Suc_mono_le:
haftmann@53986
  1628
  assumes mono: "\<And>n. f n \<le> f (Suc n)" and "n \<le> n'"
krauss@27627
  1629
  shows "f n \<le> f n'"
krauss@27627
  1630
proof (cases "n < n'")
krauss@27627
  1631
  case True
haftmann@53986
  1632
  then show ?thesis
wenzelm@62683
  1633
    by (induct n n' rule: less_Suc_induct) (auto intro: mono)
wenzelm@63110
  1634
next
wenzelm@63110
  1635
  case False
wenzelm@63110
  1636
  with \<open>n \<le> n'\<close> show ?thesis by auto
wenzelm@63110
  1637
qed
nipkow@27625
  1638
hoelzl@56020
  1639
lemma lift_Suc_antimono_le:
hoelzl@56020
  1640
  assumes mono: "\<And>n. f n \<ge> f (Suc n)" and "n \<le> n'"
hoelzl@56020
  1641
  shows "f n \<ge> f n'"
hoelzl@56020
  1642
proof (cases "n < n'")
hoelzl@56020
  1643
  case True
hoelzl@56020
  1644
  then show ?thesis
wenzelm@62683
  1645
    by (induct n n' rule: less_Suc_induct) (auto intro: mono)
wenzelm@63110
  1646
next
wenzelm@63110
  1647
  case False
wenzelm@63110
  1648
  with \<open>n \<le> n'\<close> show ?thesis by auto
wenzelm@63110
  1649
qed
hoelzl@56020
  1650
nipkow@27625
  1651
lemma lift_Suc_mono_less:
haftmann@53986
  1652
  assumes mono: "\<And>n. f n < f (Suc n)" and "n < n'"
krauss@27627
  1653
  shows "f n < f n'"
wenzelm@63110
  1654
  using \<open>n < n'\<close> by (induct n n' rule: less_Suc_induct) (auto intro: mono)
wenzelm@63110
  1655
wenzelm@63110
  1656
lemma lift_Suc_mono_less_iff: "(\<And>n. f n < f (Suc n)) \<Longrightarrow> f n < f m \<longleftrightarrow> n < m"
haftmann@53986
  1657
  by (blast intro: less_asym' lift_Suc_mono_less [of f]
haftmann@53986
  1658
    dest: linorder_not_less[THEN iffD1] le_eq_less_or_eq [THEN iffD1])
nipkow@27789
  1659
nipkow@27625
  1660
end
nipkow@27625
  1661
wenzelm@63110
  1662
lemma mono_iff_le_Suc: "mono f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
haftmann@37387
  1663
  unfolding mono_def by (auto intro: lift_Suc_mono_le [of f])
nipkow@27625
  1664
wenzelm@63110
  1665
lemma antimono_iff_le_Suc: "antimono f \<longleftrightarrow> (\<forall>n. f (Suc n) \<le> f n)"
hoelzl@56020
  1666
  unfolding antimono_def by (auto intro: lift_Suc_antimono_le [of f])
hoelzl@56020
  1667
nipkow@27789
  1668
lemma mono_nat_linear_lb:
haftmann@53986
  1669
  fixes f :: "nat \<Rightarrow> nat"
haftmann@53986
  1670
  assumes "\<And>m n. m < n \<Longrightarrow> f m < f n"
haftmann@53986
  1671
  shows "f m + k \<le> f (m + k)"
haftmann@53986
  1672
proof (induct k)
wenzelm@63110
  1673
  case 0
wenzelm@63110
  1674
  then show ?case by simp
haftmann@53986
  1675
next
haftmann@53986
  1676
  case (Suc k)
haftmann@53986
  1677
  then have "Suc (f m + k) \<le> Suc (f (m + k))" by simp
haftmann@53986
  1678
  also from assms [of "m + k" "Suc (m + k)"] have "Suc (f (m + k)) \<le> f (Suc (m + k))"
haftmann@53986
  1679
    by (simp add: Suc_le_eq)
haftmann@53986
  1680
  finally show ?case by simp
haftmann@53986
  1681
qed
nipkow@27789
  1682
nipkow@27789
  1683
wenzelm@63110
  1684
text \<open>Subtraction laws, mostly by Clemens Ballarin\<close>
wenzelm@21243
  1685
haftmann@62481
  1686
lemma diff_less_mono:
haftmann@62481
  1687
  fixes a b c :: nat
haftmann@62481
  1688
  assumes "a < b" and "c \<le> a"
haftmann@62481
  1689
  shows "a - c < b - c"
haftmann@62481
  1690
proof -
haftmann@62481
  1691
  from assms obtain d e where "b = c + (d + e)" and "a = c + e" and "d > 0"
haftmann@62481
  1692
    by (auto dest!: le_Suc_ex less_imp_Suc_add simp add: ac_simps)
haftmann@62481
  1693
  then show ?thesis by simp
haftmann@62481
  1694
qed
haftmann@62481
  1695
wenzelm@63110
  1696
lemma less_diff_conv: "i < j - k \<longleftrightarrow> i + k < j" for i j k :: nat
wenzelm@63110
  1697
  by (cases "k \<le> j") (auto simp add: not_le dest: less_imp_Suc_add le_Suc_ex)
wenzelm@63110
  1698
wenzelm@63110
  1699
lemma less_diff_conv2: "k \<le> j \<Longrightarrow> j - k < i \<longleftrightarrow> j < i + k" for j k i :: nat
haftmann@62481
  1700
  by (auto dest: le_Suc_ex)
haftmann@62481
  1701
wenzelm@63110
  1702
lemma le_diff_conv: "j - k \<le> i \<longleftrightarrow> j \<le> i + k" for j k i :: nat
wenzelm@63110
  1703
  by (cases "k \<le> j") (auto simp add: not_le dest!: less_imp_Suc_add le_Suc_ex)
wenzelm@63110
  1704
wenzelm@63110
  1705
lemma diff_diff_cancel [simp]: "i \<le> n \<Longrightarrow> n - (n - i) = i" for i n :: nat
wenzelm@63110
  1706
  by (auto dest: le_Suc_ex)
wenzelm@63110
  1707
wenzelm@63110
  1708
lemma diff_less [simp]: "0 < n \<Longrightarrow> 0 < m \<Longrightarrow> m - n < m" for i n :: nat
haftmann@62481
  1709
  by (auto dest: less_imp_Suc_add)
wenzelm@21243
  1710
wenzelm@60758
  1711
text \<open>Simplification of relational expressions involving subtraction\<close>
wenzelm@21243
  1712
wenzelm@63110
  1713
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
  1714
  by (auto dest!: le_Suc_ex)
wenzelm@21243
  1715
wenzelm@36176
  1716
hide_fact (open) diff_diff_eq
haftmann@35064
  1717
wenzelm@63110
  1718
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
  1719
  by (auto dest: le_Suc_ex)
haftmann@62481
  1720
wenzelm@63110
  1721
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
  1722
  by (auto dest!: le_Suc_ex)
haftmann@62481
  1723
wenzelm@63110
  1724
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
  1725
  by (auto dest!: le_Suc_ex)
wenzelm@21243
  1726
wenzelm@63110
  1727
lemma le_diff_iff': "a \<le> c \<Longrightarrow> b \<le> c \<Longrightarrow> c - a \<le> c - b \<longleftrightarrow> b \<le> a" for a b c :: nat
eberlm@63099
  1728
  by (force dest: le_Suc_ex)
wenzelm@63110
  1729
wenzelm@63110
  1730
wenzelm@63110
  1731
text \<open>(Anti)Monotonicity of subtraction -- by Stephan Merz\<close>
wenzelm@63110
  1732
wenzelm@63110
  1733
lemma diff_le_mono: "m \<le> n \<Longrightarrow> m - l \<le> n - l" for m n l :: nat
haftmann@62481
  1734
  by (auto dest: less_imp_le less_imp_Suc_add split add: nat_diff_split)
haftmann@62481
  1735
wenzelm@63110
  1736
lemma diff_le_mono2: "m \<le> n \<Longrightarrow> l - n \<le> l - m" for m n l :: nat
haftmann@62481
  1737
  by (auto dest: less_imp_le le_Suc_ex less_imp_Suc_add less_le_trans split add: nat_diff_split)
haftmann@62481
  1738
wenzelm@63110
  1739
lemma diff_less_mono2: "m < n \<Longrightarrow> m < l \<Longrightarrow> l - n < l - m" for m n l :: nat
haftmann@62481
  1740
  by (auto dest: less_imp_Suc_add split add: nat_diff_split)
haftmann@62481
  1741
wenzelm@63110
  1742
lemma diffs0_imp_equal: "m - n = 0 \<Longrightarrow> n - m = 0 \<Longrightarrow> m = n" for m n :: nat
haftmann@62481
  1743
  by (simp split add: nat_diff_split)
haftmann@62481
  1744
wenzelm@63110
  1745
lemma min_diff: "min (m - i) (n - i) = min m n - i" for m n i :: nat
haftmann@62481
  1746
  by (cases m n rule: le_cases)
haftmann@62481
  1747
    (auto simp add: not_le min.absorb1 min.absorb2 min.absorb_iff1 [symmetric] diff_le_mono)
bulwahn@26143
  1748
lp15@60562
  1749
lemma inj_on_diff_nat:
wenzelm@63110
  1750
  fixes k :: nat
wenzelm@63110
  1751
  assumes "\<forall>n \<in> N. k \<le> n"
bulwahn@26143
  1752
  shows "inj_on (\<lambda>n. n - k) N"
bulwahn@26143
  1753
proof (rule inj_onI)
bulwahn@26143
  1754
  fix x y
bulwahn@26143
  1755
  assume a: "x \<in> N" "y \<in> N" "x - k = y - k"
wenzelm@63110
  1756
  with assms have "x - k + k = y - k + k" by auto
wenzelm@63110
  1757
  with a assms show "x = y" by (auto simp add: eq_diff_iff)
bulwahn@26143
  1758
qed
bulwahn@26143
  1759
wenzelm@63110
  1760
text \<open>Rewriting to pull differences out\<close>
wenzelm@63110
  1761
wenzelm@63110
  1762
lemma diff_diff_right [simp]: "k \<le> j \<Longrightarrow> i - (j - k) = i + k - j" for i j k :: nat
haftmann@62481
  1763
  by (fact diff_diff_right)
haftmann@62481
  1764
haftmann@62481
  1765
lemma diff_Suc_diff_eq1 [simp]:
haftmann@62481
  1766
  assumes "k \<le> j"
haftmann@62481
  1767
  shows "i - Suc (j - k) = i + k - Suc j"
haftmann@62481
  1768
proof -
haftmann@62481
  1769
  from assms have *: "Suc (j - k) = Suc j - k"
haftmann@62481
  1770
    by (simp add: Suc_diff_le)
haftmann@62481
  1771
  from assms have "k \<le> Suc j"
haftmann@62481
  1772
    by (rule order_trans) simp
haftmann@62481
  1773
  with diff_diff_right [of k "Suc j" i] * show ?thesis
haftmann@62481
  1774
    by simp
haftmann@62481
  1775
qed
haftmann@62481
  1776
haftmann@62481
  1777
lemma diff_Suc_diff_eq2 [simp]:
haftmann@62481
  1778
  assumes "k \<le> j"
haftmann@62481
  1779
  shows "Suc (j - k) - i = Suc j - (k + i)"
haftmann@62481
  1780
proof -
haftmann@62481
  1781
  from assms obtain n where "j = k + n"
haftmann@62481
  1782
    by (auto dest: le_Suc_ex)
haftmann@62481
  1783
  moreover have "Suc n - i = (k + Suc n) - (k + i)"
haftmann@62481
  1784
    using add_diff_cancel_left [of k "Suc n" i] by simp
haftmann@62481
  1785
  ultimately show ?thesis by simp
haftmann@62481
  1786
qed
haftmann@62481
  1787
haftmann@62481
  1788
lemma Suc_diff_Suc:
haftmann@62481
  1789
  assumes "n < m"
haftmann@62481
  1790
  shows "Suc (m - Suc n) = m - n"
haftmann@62481
  1791
proof -
haftmann@62481
  1792
  from assms obtain q where "m = n + Suc q"
haftmann@62481
  1793
    by (auto dest: less_imp_Suc_add)
wenzelm@63040
  1794
  moreover define r where "r = Suc q"
haftmann@62481
  1795
  ultimately have "Suc (m - Suc n) = r" and "m = n + r"
haftmann@62481
  1796
    by simp_all
haftmann@62481
  1797
  then show ?thesis by simp
haftmann@62481
  1798
qed
haftmann@62481
  1799
wenzelm@63110
  1800
lemma one_less_mult: "Suc 0 < n \<Longrightarrow> Suc 0 < m \<Longrightarrow> Suc 0 < m * n"
haftmann@62481
  1801
  using less_1_mult [of n m] by (simp add: ac_simps)
haftmann@62481
  1802
wenzelm@63110
  1803
lemma n_less_m_mult_n: "0 < n \<Longrightarrow> Suc 0 < m \<Longrightarrow> n < m * n"
haftmann@62481
  1804
  using mult_strict_right_mono [of 1 m n] by simp
haftmann@62481
  1805
wenzelm@63110
  1806
lemma n_less_n_mult_m: "0 < n \<Longrightarrow> Suc 0 < m \<Longrightarrow> n < n * m"
haftmann@62481
  1807
  using mult_strict_left_mono [of 1 m n] by simp
wenzelm@21243
  1808
wenzelm@63110
  1809
wenzelm@60758
  1810
text \<open>Specialized induction principles that work "backwards":\<close>
krauss@23001
  1811
haftmann@62481
  1812
lemma inc_induct [consumes 1, case_names base step]:
hoelzl@54411
  1813
  assumes less: "i \<le> j"
wenzelm@63110
  1814
    and base: "P j"
wenzelm@63110
  1815
    and step: "\<And>n. i \<le> n \<Longrightarrow> n < j \<Longrightarrow> P (Suc n) \<Longrightarrow> P n"
krauss@23001
  1816
  shows "P i"
hoelzl@54411
  1817
  using less step
haftmann@62481
  1818
proof (induct "j - i" arbitrary: i)
krauss@23001
  1819
  case (0 i)
haftmann@62481
  1820
  then have "i = j" by simp
krauss@23001
  1821
  with base show ?case by simp
krauss@23001
  1822
next
hoelzl@54411
  1823
  case (Suc d n)
haftmann@62481
  1824
  from Suc.hyps have "n \<noteq> j" by auto
haftmann@62481
  1825
  with Suc have "n < j" by (simp add: less_le)
haftmann@62481
  1826
  from \<open>Suc d = j - n\<close> have "d + 1 = j - n" by simp
haftmann@62481
  1827
  then have "d + 1 - 1 = j - n - 1" by simp
haftmann@62481
  1828
  then have "d = j - n - 1" by simp
wenzelm@63110
  1829
  then have "d = j - (n + 1)"
haftmann@62481
  1830
    by (simp add: diff_diff_eq)
haftmann@62481
  1831
  then have "d = j - Suc n"
haftmann@62481
  1832
    by simp
haftmann@62481
  1833
  moreover from \<open>n < j\<close> have "Suc n \<le> j"
haftmann@62481
  1834
    by (simp add: Suc_le_eq)
haftmann@62481
  1835
  ultimately have "P (Suc n)"
haftmann@62481
  1836
  proof (rule Suc.hyps)
haftmann@62481
  1837
    fix q
haftmann@62481
  1838
    assume "Suc n \<le> q"
haftmann@62481
  1839
    then have "n \<le> q" by (simp add: Suc_le_eq less_imp_le)
haftmann@62481
  1840
    moreover assume "q < j"
haftmann@62481
  1841
    moreover assume "P (Suc q)"
haftmann@62481
  1842
    ultimately show "P q"
haftmann@62481
  1843
      by (rule Suc.prems)
haftmann@62481
  1844
  qed
haftmann@62481
  1845
  with order_refl \<open>n < j\<close> show "P n"
haftmann@62481
  1846
    by (rule Suc.prems)
krauss@23001
  1847
qed
wenzelm@63110
  1848
haftmann@62481
  1849
lemma strict_inc_induct [consumes 1, case_names base step]:
krauss@23001
  1850
  assumes less: "i < j"
wenzelm@63110
  1851
    and base: "\<And>i. j = Suc i \<Longrightarrow> P i"
wenzelm@63110
  1852
    and step: "\<And>i. i < j \<Longrightarrow> P (Suc i) \<Longrightarrow> P i"
krauss@23001
  1853
  shows "P i"
haftmann@62481
  1854
using less proof (induct "j - i - 1" arbitrary: i)
krauss@23001
  1855
  case (0 i)
haftmann@62481
  1856
  from \<open>i < j\<close> obtain n where "j = i + n" and "n > 0"
haftmann@62481
  1857
    by (auto dest!: less_imp_Suc_add)
haftmann@62481
  1858
  with 0 have "j = Suc i"
haftmann@62481
  1859
    by (auto intro: order_antisym simp add: Suc_le_eq)
krauss@23001
  1860
  with base show ?case by simp
krauss@23001
  1861
next
krauss@23001
  1862
  case (Suc d i)
haftmann@62481
  1863
  from \<open>Suc d = j - i - 1\<close> have *: "Suc d = j - Suc i"
haftmann@62481
  1864
    by (simp add: diff_diff_add)
haftmann@62481
  1865
  then have "Suc d - 1 = j - Suc i - 1"
haftmann@62481
  1866
    by simp
haftmann@62481
  1867
  then have "d = j - Suc i - 1"
haftmann@62481
  1868
    by simp
haftmann@62481
  1869
  moreover from * have "j - Suc i \<noteq> 0"
haftmann@62481
  1870
    by auto
haftmann@62481
  1871
  then have "Suc i < j"
haftmann@62481
  1872
    by (simp add: not_le)
haftmann@62481
  1873
  ultimately have "P (Suc i)"
haftmann@62481
  1874
    by (rule Suc.hyps)
haftmann@62481
  1875
  with \<open>i < j\<close> show "P i" by (rule step)
krauss@23001
  1876
qed
krauss@23001
  1877
wenzelm@63110
  1878
lemma zero_induct_lemma: "P k \<Longrightarrow> (\<And>n. P (Suc n) \<Longrightarrow> P n) \<Longrightarrow> P (k - i)"
krauss@23001
  1879
  using inc_induct[of "k - i" k P, simplified] by blast
krauss@23001
  1880
wenzelm@63110
  1881
lemma zero_induct: "P k \<Longrightarrow> (\<And>n. P (Suc n) \<Longrightarrow> P n) \<Longrightarrow> P 0"
krauss@23001
  1882
  using inc_induct[of 0 k P] by blast
wenzelm@21243
  1883
wenzelm@60758
  1884
text \<open>Further induction rule similar to @{thm inc_induct}\<close>
nipkow@27625
  1885
haftmann@62481
  1886
lemma dec_induct [consumes 1, case_names base step]:
hoelzl@54411
  1887
  "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
  1888
proof (induct j arbitrary: i)
wenzelm@63110
  1889
  case 0
wenzelm@63110
  1890
  then show ?case by simp
haftmann@62481
  1891
next
haftmann@62481
  1892
  case (Suc j)
wenzelm@63110
  1893
  from Suc.prems consider "i \<le> j" | "i = Suc j"
wenzelm@63110
  1894
    by (auto simp add: le_Suc_eq)
wenzelm@63110
  1895
  then show ?case
wenzelm@63110
  1896
  proof cases
wenzelm@63110
  1897
    case 1
haftmann@62481
  1898
    moreover have "j < Suc j" by simp
haftmann@62481
  1899
    moreover have "P j" using \<open>i \<le> j\<close> \<open>P i\<close>
haftmann@62481
  1900
    proof (rule Suc.hyps)
haftmann@62481
  1901
      fix q
haftmann@62481
  1902
      assume "i \<le> q"
haftmann@62481
  1903
      moreover assume "q < j" then have "q < Suc j"
haftmann@62481
  1904
        by (simp add: less_Suc_eq)
haftmann@62481
  1905
      moreover assume "P q"
haftmann@62481
  1906
      ultimately show "P (Suc q)"
haftmann@62481
  1907
        by (rule Suc.prems)
haftmann@62481
  1908
    qed
haftmann@62481
  1909
    ultimately show "P (Suc j)"
haftmann@62481
  1910
      by (rule Suc.prems)
haftmann@62481
  1911
  next
wenzelm@63110
  1912
    case 2
haftmann@62481
  1913
    with \<open>P i\<close> show "P (Suc j)" by simp
haftmann@62481
  1914
  qed
haftmann@62481
  1915
qed
haftmann@62481
  1916
hoelzl@59000
  1917
wenzelm@63110
  1918
subsection \<open>Monotonicity of \<open>funpow\<close>\<close>
hoelzl@59000
  1919
hoelzl@59000
  1920
lemma funpow_increasing:
wenzelm@63110
  1921
  fixes f :: "'a \<Rightarrow> 'a::{lattice,order_top}"
hoelzl@59000
  1922
  shows "m \<le> n \<Longrightarrow> mono f \<Longrightarrow> (f ^^ n) \<top> \<le> (f ^^ m) \<top>"
hoelzl@59000
  1923
  by (induct rule: inc_induct)
hoelzl@59000
  1924
     (auto simp del: funpow.simps(2) simp add: funpow_Suc_right
hoelzl@59000
  1925
           intro: order_trans[OF _ funpow_mono])
hoelzl@59000
  1926
hoelzl@59000
  1927
lemma funpow_decreasing:
wenzelm@63110
  1928
  fixes f :: "'a \<Rightarrow> 'a::{lattice,order_bot}"
hoelzl@59000
  1929
  shows "m \<le> n \<Longrightarrow> mono f \<Longrightarrow> (f ^^ m) \<bottom> \<le> (f ^^ n) \<bottom>"
hoelzl@59000
  1930
  by (induct rule: dec_induct)
hoelzl@59000
  1931
     (auto simp del: funpow.simps(2) simp add: funpow_Suc_right
hoelzl@59000
  1932
           intro: order_trans[OF _ funpow_mono])
hoelzl@59000
  1933
hoelzl@59000
  1934
lemma mono_funpow:
wenzelm@63110
  1935
  fixes Q :: "'a::{lattice,order_bot} \<Rightarrow> 'a"
hoelzl@59000
  1936
  shows "mono Q \<Longrightarrow> mono (\<lambda>i. (Q ^^ i) \<bottom>)"
hoelzl@59000
  1937
  by (auto intro!: funpow_decreasing simp: mono_def)
blanchet@58377
  1938
hoelzl@60175
  1939
lemma antimono_funpow:
wenzelm@63110
  1940
  fixes Q :: "'a::{lattice,order_top} \<Rightarrow> 'a"
hoelzl@60175
  1941
  shows "mono Q \<Longrightarrow> antimono (\<lambda>i. (Q ^^ i) \<top>)"
hoelzl@60175
  1942
  by (auto intro!: funpow_increasing simp: antimono_def)
hoelzl@60175
  1943
wenzelm@63110
  1944
wenzelm@60758
  1945
subsection \<open>The divides relation on @{typ nat}\<close>
haftmann@33274
  1946
wenzelm@63110
  1947
lemma dvd_1_left [iff]: "Suc 0 dvd k"
haftmann@62365
  1948
  by (simp add: dvd_def)
haftmann@62365
  1949
wenzelm@63110
  1950
lemma dvd_1_iff_1 [simp]: "m dvd Suc 0 \<longleftrightarrow> m = Suc 0"
haftmann@62365
  1951
  by (simp add: dvd_def)
haftmann@62365
  1952
wenzelm@63110
  1953
lemma nat_dvd_1_iff_1 [simp]: "m dvd 1 \<longleftrightarrow> m = 1" for m :: nat
haftmann@62365
  1954
  by (simp add: dvd_def)
haftmann@62365
  1955
wenzelm@63110
  1956
lemma dvd_antisym: "m dvd n \<Longrightarrow> n dvd m \<Longrightarrow> m = n" for m n :: nat
wenzelm@63110
  1957
  unfolding dvd_def by (force dest: mult_eq_self_implies_10 simp add: mult.assoc)
wenzelm@63110
  1958
wenzelm@63110
  1959
lemma dvd_diff_nat [simp]: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m - n)" for k m n :: nat
wenzelm@63110
  1960
  unfolding dvd_def by (blast intro: right_diff_distrib' [symmetric])
wenzelm@63110
  1961
wenzelm@63110
  1962
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
  1963
  apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
haftmann@33274
  1964
  apply (blast intro: dvd_add)
haftmann@33274
  1965
  done
haftmann@33274
  1966
wenzelm@63110
  1967
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
  1968
  by (drule_tac m = m in dvd_diff_nat) auto
haftmann@62365
  1969
haftmann@62365
  1970
lemma dvd_mult_cancel:
haftmann@62365
  1971
  fixes m n k :: nat
haftmann@62365
  1972
  assumes "k * m dvd k * n" and "0 < k"
haftmann@62365
  1973
  shows "m dvd n"
haftmann@62365
  1974
proof -
haftmann@62365
  1975
  from assms(1) obtain q where "k * n = (k * m) * q" ..
haftmann@62365
  1976
  then have "k * n = k * (m * q)" by (simp add: ac_simps)
haftmann@62481
  1977
  with \<open>0 < k\<close> have "n = m * q" by (auto simp add: mult_left_cancel)
haftmann@62365
  1978
  then show ?thesis ..
haftmann@62365
  1979
qed
wenzelm@63110
  1980
wenzelm@63110
  1981
lemma dvd_mult_cancel1: "0 < m \<Longrightarrow> m * n dvd m \<longleftrightarrow> n = 1" for m n :: nat
haftmann@33274
  1982
  apply auto
wenzelm@63110
  1983
   apply (subgoal_tac "m * n dvd m * 1")
haftmann@33274
  1984
   apply (drule dvd_mult_cancel, auto)
haftmann@33274
  1985
  done
haftmann@33274
  1986
wenzelm@63110
  1987
lemma dvd_mult_cancel2: "0 < m \<Longrightarrow> n * m dvd m \<longleftrightarrow> n = 1" for m n :: nat
haftmann@62365
  1988
  using dvd_mult_cancel1 [of m n] by (simp add: ac_simps)
haftmann@62365
  1989
wenzelm@63110
  1990
lemma dvd_imp_le: "k dvd n \<Longrightarrow> 0 < n \<Longrightarrow> k \<le> n" for k n :: nat
haftmann@62365
  1991
  by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
haftmann@33274
  1992
wenzelm@63110
  1993
lemma nat_dvd_not_less: "0 < m \<Longrightarrow> m < n \<Longrightarrow> \<not> n dvd m" for m n :: nat
haftmann@62365
  1994
  by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
haftmann@33274
  1995
haftmann@54222
  1996
lemma less_eq_dvd_minus:
haftmann@51173
  1997
  fixes m n :: nat
haftmann@54222
  1998
  assumes "m \<le> n"
haftmann@54222
  1999
  shows "m dvd n \<longleftrightarrow> m dvd n - m"
haftmann@51173
  2000
proof -
haftmann@54222
  2001
  from assms have "n = m + (n - m)" by simp
haftmann@51173
  2002
  then obtain q where "n = m + q" ..
haftmann@58647
  2003
  then show ?thesis by (simp add: add.commute [of m])
haftmann@51173
  2004
qed
haftmann@51173
  2005
wenzelm@63110
  2006
lemma dvd_minus_self: "m dvd n - m \<longleftrightarrow> n < m \<or> m dvd n" for m n :: nat
haftmann@62481
  2007
  by (cases "n < m") (auto elim!: dvdE simp add: not_less le_imp_diff_is_add dest: less_imp_le)
haftmann@51173
  2008
haftmann@51173
  2009
lemma dvd_minus_add:
haftmann@51173
  2010
  fixes m n q r :: nat
haftmann@51173
  2011
  assumes "q \<le> n" "q \<le> r * m"
haftmann@51173
  2012
  shows "m dvd n - q \<longleftrightarrow> m dvd n + (r * m - q)"
haftmann@51173
  2013
proof -
haftmann@51173
  2014
  have "m dvd n - q \<longleftrightarrow> m dvd r * m + (n - q)"
haftmann@58649
  2015
    using dvd_add_times_triv_left_iff [of m r] by simp
wenzelm@53374
  2016
  also from assms have "\<dots> \<longleftrightarrow> m dvd r * m + n - q" by simp
wenzelm@53374
  2017
  also from assms have "\<dots> \<longleftrightarrow> m dvd (r * m - q) + n" by simp
haftmann@57512
  2018
  also have "\<dots> \<longleftrightarrow> m dvd n + (r * m - q)" by (simp add: add.commute)
haftmann@51173
  2019
  finally show ?thesis .
haftmann@51173
  2020
qed
haftmann@51173
  2021
haftmann@33274
  2022
haftmann@62365
  2023
subsection \<open>Aliasses\<close>
haftmann@44817
  2024
wenzelm@63110
  2025
lemma nat_mult_1: "1 * n = n" for n :: nat
haftmann@58647
  2026
  by (fact mult_1_left)
lp15@60562
  2027
wenzelm@63110
  2028
lemma nat_mult_1_right: "n * 1 = n" for n :: nat
haftmann@58647
  2029
  by (fact mult_1_right)
haftmann@58647
  2030
wenzelm@63110
  2031
lemma nat_add_left_cancel: "k + m = k + n \<longleftrightarrow> m = n" for k m n :: nat
haftmann@62365
  2032
  by (fact add_left_cancel)
haftmann@62365
  2033
wenzelm@63110
  2034
lemma nat_add_right_cancel: "m + k = n + k \<longleftrightarrow> m = n" for k m n :: nat
haftmann@62365
  2035
  by (fact add_right_cancel)
haftmann@62365
  2036
wenzelm@63110
  2037
lemma diff_mult_distrib: "(m - n) * k = (m * k) - (n * k)" for k m n :: nat
haftmann@62365
  2038
  by (fact left_diff_distrib')
haftmann@62365
  2039
wenzelm@63110
  2040
lemma diff_mult_distrib2: "k * (m - n) = (k * m) - (k * n)" for k m n :: nat
haftmann@62365
  2041
  by (fact right_diff_distrib')
haftmann@62365
  2042
wenzelm@63110
  2043
lemma le_add_diff: "k \<le> n \<Longrightarrow> m \<le> n + m - k" for k m n :: nat
wenzelm@63110
  2044
  by (fact le_add_diff)  (* FIXME delete *)
wenzelm@63110
  2045
wenzelm@63110
  2046
lemma le_diff_conv2: "k \<le> j \<Longrightarrow> (i \<le> j - k) = (i + k \<le> j)" for i j k :: nat
wenzelm@63110
  2047
  by (fact le_diff_conv2) (* FIXME delete *)
wenzelm@63110
  2048
wenzelm@63110
  2049
lemma diff_self_eq_0 [simp]: "m - m = 0" for m :: nat
haftmann@62365
  2050
  by (fact diff_cancel)
haftmann@62365
  2051
wenzelm@63110
  2052
lemma diff_diff_left [simp]: "i - j - k = i - (j + k)" for i j k :: nat
haftmann@62365
  2053
  by (fact diff_diff_add)
haftmann@62365
  2054
wenzelm@63110
  2055
lemma diff_commute: "i - j - k = i - k - j" for i j k :: nat
haftmann@62365
  2056
  by (fact diff_right_commute)
haftmann@62365
  2057
wenzelm@63110
  2058
lemma diff_add_inverse: "(n + m) - n = m" for m n :: nat
haftmann@62365
  2059
  by (fact add_diff_cancel_left')
haftmann@62365
  2060
wenzelm@63110
  2061
lemma diff_add_inverse2: "(m + n) - n = m" for m n :: nat
haftmann@62365
  2062
  by (fact add_diff_cancel_right')
haftmann@62365
  2063
wenzelm@63110
  2064
lemma diff_cancel: "(k + m) - (k + n) = m - n" for k m n :: nat
haftmann@62365
  2065
  by (fact add_diff_cancel_left)
haftmann@62365
  2066
wenzelm@63110
  2067
lemma diff_cancel2: "(m + k) - (n + k) = m - n" for k m n :: nat
haftmann@62365
  2068
  by (fact add_diff_cancel_right)
haftmann@62365
  2069
wenzelm@63110
  2070
lemma diff_add_0: "n - (n + m) = 0" for m n :: nat
haftmann@62365
  2071
  by (fact diff_add_zero)
haftmann@62365
  2072
wenzelm@63110
  2073
lemma add_mult_distrib2: "k * (m + n) = (k * m) + (k * n)" for k m n :: nat
haftmann@62365
  2074
  by (fact distrib_left)
haftmann@62365
  2075
haftmann@62365
  2076
lemmas nat_distrib =
haftmann@62365
  2077
  add_mult_distrib distrib_left diff_mult_distrib diff_mult_distrib2
haftmann@62365
  2078
haftmann@44817
  2079
wenzelm@60758
  2080
subsection \<open>Size of a datatype value\<close>
haftmann@25193
  2081
haftmann@29608
  2082
class size =
wenzelm@61799
  2083
  fixes size :: "'a \<Rightarrow> nat" \<comment> \<open>see further theory \<open>Wellfounded\<close>\<close>
haftmann@23852
  2084
blanchet@58377
  2085
instantiation nat :: size
blanchet@58377
  2086
begin
blanchet@58377
  2087
wenzelm@63110
  2088
definition size_nat where [simp, code]: "size (n::nat) = n"
blanchet@58377
  2089
blanchet@58377
  2090
instance ..
blanchet@58377
  2091
blanchet@58377
  2092
end
blanchet@58377
  2093
blanchet@58377
  2094
wenzelm@60758
  2095
subsection \<open>Code module namespace\<close>
haftmann@33364
  2096
haftmann@52435
  2097
code_identifier
haftmann@52435
  2098
  code_module Nat \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
haftmann@33364
  2099
huffman@47108
  2100
hide_const (open) of_nat_aux
huffman@47108
  2101
haftmann@25193
  2102
end