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