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