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