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