src/HOL/Nat.thy
author paulson
Thu Jul 24 18:23:00 2003 +0200 (2003-07-24)
changeset 14131 a4fc8b1af5e7
parent 13596 ee5f79b210c1
child 14193 30e41f63712e
permissions -rw-r--r--
declarations moved from PreList.thy
clasohm@923
     1
(*  Title:      HOL/Nat.thy
clasohm@923
     2
    ID:         $Id$
wenzelm@9436
     3
    Author:     Tobias Nipkow and Lawrence C Paulson
clasohm@923
     4
wenzelm@9436
     5
Type "nat" is a linear order, and a datatype; arithmetic operators + -
wenzelm@9436
     6
and * (for div, mod and dvd, see theory Divides).
clasohm@923
     7
*)
clasohm@923
     8
berghofe@13449
     9
header {* Natural numbers *}
berghofe@13449
    10
berghofe@13449
    11
theory Nat = Wellfounded_Recursion:
berghofe@13449
    12
berghofe@13449
    13
subsection {* Type @{text ind} *}
berghofe@13449
    14
berghofe@13449
    15
typedecl ind
berghofe@13449
    16
berghofe@13449
    17
consts
berghofe@13449
    18
  Zero_Rep      :: ind
berghofe@13449
    19
  Suc_Rep       :: "ind => ind"
berghofe@13449
    20
berghofe@13449
    21
axioms
berghofe@13449
    22
  -- {* the axiom of infinity in 2 parts *}
berghofe@13449
    23
  inj_Suc_Rep:          "inj Suc_Rep"
berghofe@13449
    24
  Suc_Rep_not_Zero_Rep: "Suc_Rep x ~= Zero_Rep"
berghofe@13449
    25
berghofe@13449
    26
berghofe@13449
    27
subsection {* Type nat *}
berghofe@13449
    28
berghofe@13449
    29
text {* Type definition *}
berghofe@13449
    30
berghofe@13449
    31
consts
berghofe@13449
    32
  Nat :: "ind set"
berghofe@13449
    33
berghofe@13449
    34
inductive Nat
berghofe@13449
    35
intros
berghofe@13449
    36
  Zero_RepI: "Zero_Rep : Nat"
berghofe@13449
    37
  Suc_RepI: "i : Nat ==> Suc_Rep i : Nat"
berghofe@13449
    38
berghofe@13449
    39
global
berghofe@13449
    40
berghofe@13449
    41
typedef (open Nat)
berghofe@13449
    42
  nat = "Nat" by (rule exI, rule Nat.Zero_RepI)
berghofe@13449
    43
berghofe@13449
    44
instance nat :: ord ..
berghofe@13449
    45
instance nat :: zero ..
berghofe@13449
    46
instance nat :: one ..
berghofe@13449
    47
berghofe@13449
    48
berghofe@13449
    49
text {* Abstract constants and syntax *}
berghofe@13449
    50
berghofe@13449
    51
consts
berghofe@13449
    52
  Suc :: "nat => nat"
berghofe@13449
    53
  pred_nat :: "(nat * nat) set"
berghofe@13449
    54
berghofe@13449
    55
local
berghofe@13449
    56
berghofe@13449
    57
defs
berghofe@13449
    58
  Zero_nat_def: "0 == Abs_Nat Zero_Rep"
berghofe@13449
    59
  Suc_def: "Suc == (%n. Abs_Nat (Suc_Rep (Rep_Nat n)))"
berghofe@13449
    60
  One_nat_def [simp]: "1 == Suc 0"
berghofe@13449
    61
berghofe@13449
    62
  -- {* nat operations *}
berghofe@13449
    63
  pred_nat_def: "pred_nat == {(m, n). n = Suc m}"
berghofe@13449
    64
berghofe@13449
    65
  less_def: "m < n == (m, n) : trancl pred_nat"
berghofe@13449
    66
berghofe@13449
    67
  le_def: "m <= (n::nat) == ~ (n < m)"
berghofe@13449
    68
berghofe@13449
    69
berghofe@13449
    70
text {* Induction *}
clasohm@923
    71
berghofe@13449
    72
theorem nat_induct: "P 0 ==> (!!n. P n ==> P (Suc n)) ==> P n"
berghofe@13449
    73
  apply (unfold Zero_nat_def Suc_def)
berghofe@13449
    74
  apply (rule Rep_Nat_inverse [THEN subst]) -- {* types force good instantiation *}
berghofe@13449
    75
  apply (erule Rep_Nat [THEN Nat.induct])
berghofe@13449
    76
  apply (rules elim: Abs_Nat_inverse [THEN subst])
berghofe@13449
    77
  done
berghofe@13449
    78
berghofe@13449
    79
berghofe@13449
    80
text {* Isomorphisms: @{text Abs_Nat} and @{text Rep_Nat} *}
berghofe@13449
    81
berghofe@13449
    82
lemma inj_Rep_Nat: "inj Rep_Nat"
paulson@13585
    83
  apply (rule inj_on_inverseI)
berghofe@13449
    84
  apply (rule Rep_Nat_inverse)
berghofe@13449
    85
  done
berghofe@13449
    86
berghofe@13449
    87
lemma inj_on_Abs_Nat: "inj_on Abs_Nat Nat"
berghofe@13449
    88
  apply (rule inj_on_inverseI)
berghofe@13449
    89
  apply (erule Abs_Nat_inverse)
berghofe@13449
    90
  done
berghofe@13449
    91
berghofe@13449
    92
text {* Distinctness of constructors *}
berghofe@13449
    93
berghofe@13449
    94
lemma Suc_not_Zero [iff]: "Suc m ~= 0"
berghofe@13449
    95
  apply (unfold Zero_nat_def Suc_def)
berghofe@13449
    96
  apply (rule inj_on_Abs_Nat [THEN inj_on_contraD])
berghofe@13449
    97
  apply (rule Suc_Rep_not_Zero_Rep)
berghofe@13449
    98
  apply (rule Rep_Nat Nat.Suc_RepI Nat.Zero_RepI)+
berghofe@13449
    99
  done
berghofe@13449
   100
berghofe@13449
   101
lemma Zero_not_Suc [iff]: "0 ~= Suc m"
berghofe@13449
   102
  by (rule not_sym, rule Suc_not_Zero not_sym)
berghofe@13449
   103
berghofe@13449
   104
lemma Suc_neq_Zero: "Suc m = 0 ==> R"
berghofe@13449
   105
  by (rule notE, rule Suc_not_Zero)
berghofe@13449
   106
berghofe@13449
   107
lemma Zero_neq_Suc: "0 = Suc m ==> R"
berghofe@13449
   108
  by (rule Suc_neq_Zero, erule sym)
berghofe@13449
   109
berghofe@13449
   110
text {* Injectiveness of @{term Suc} *}
berghofe@13449
   111
berghofe@13449
   112
lemma inj_Suc: "inj Suc"
berghofe@13449
   113
  apply (unfold Suc_def)
paulson@13585
   114
  apply (rule inj_onI)
berghofe@13449
   115
  apply (drule inj_on_Abs_Nat [THEN inj_onD])
berghofe@13449
   116
  apply (rule Rep_Nat Nat.Suc_RepI)+
berghofe@13449
   117
  apply (drule inj_Suc_Rep [THEN injD])
berghofe@13449
   118
  apply (erule inj_Rep_Nat [THEN injD])
berghofe@13449
   119
  done
berghofe@13449
   120
berghofe@13449
   121
lemma Suc_inject: "Suc x = Suc y ==> x = y"
berghofe@13449
   122
  by (rule inj_Suc [THEN injD])
berghofe@13449
   123
berghofe@13449
   124
lemma Suc_Suc_eq [iff]: "(Suc m = Suc n) = (m = n)"
berghofe@13449
   125
  apply (rule iffI)
berghofe@13449
   126
  apply (erule Suc_inject)
berghofe@13449
   127
  apply (erule arg_cong)
berghofe@13449
   128
  done
berghofe@13449
   129
berghofe@13449
   130
lemma nat_not_singleton: "(ALL x. x = (0::nat)) = False"
berghofe@13449
   131
  by auto
berghofe@13449
   132
berghofe@13449
   133
text {* @{typ nat} is a datatype *}
wenzelm@9436
   134
berghofe@5188
   135
rep_datatype nat
berghofe@13449
   136
  distinct  Suc_not_Zero Zero_not_Suc
berghofe@13449
   137
  inject    Suc_Suc_eq
berghofe@13449
   138
  induction nat_induct
berghofe@13449
   139
berghofe@13449
   140
lemma n_not_Suc_n: "n ~= Suc n"
berghofe@13449
   141
  by (induct n) simp_all
berghofe@13449
   142
berghofe@13449
   143
lemma Suc_n_not_n: "Suc t ~= t"
berghofe@13449
   144
  by (rule not_sym, rule n_not_Suc_n)
berghofe@13449
   145
berghofe@13449
   146
text {* A special form of induction for reasoning
berghofe@13449
   147
  about @{term "m < n"} and @{term "m - n"} *}
berghofe@13449
   148
berghofe@13449
   149
theorem diff_induct: "(!!x. P x 0) ==> (!!y. P 0 (Suc y)) ==>
berghofe@13449
   150
    (!!x y. P x y ==> P (Suc x) (Suc y)) ==> P m n"
berghofe@13449
   151
  apply (rule_tac x = "m" in spec)
berghofe@13449
   152
  apply (induct_tac n)
berghofe@13449
   153
  prefer 2
berghofe@13449
   154
  apply (rule allI)
berghofe@13449
   155
  apply (induct_tac x)
berghofe@13449
   156
  apply rules+
berghofe@13449
   157
  done
berghofe@13449
   158
berghofe@13449
   159
subsection {* Basic properties of "less than" *}
berghofe@13449
   160
berghofe@13449
   161
lemma wf_pred_nat: "wf pred_nat"
berghofe@13449
   162
  apply (unfold wf_def pred_nat_def)
berghofe@13449
   163
  apply clarify
berghofe@13449
   164
  apply (induct_tac x)
berghofe@13449
   165
  apply blast+
berghofe@13449
   166
  done
berghofe@13449
   167
berghofe@13449
   168
lemma wf_less: "wf {(x, y::nat). x < y}"
berghofe@13449
   169
  apply (unfold less_def)
berghofe@13449
   170
  apply (rule wf_pred_nat [THEN wf_trancl, THEN wf_subset])
berghofe@13449
   171
  apply blast
berghofe@13449
   172
  done
berghofe@13449
   173
berghofe@13449
   174
lemma less_eq: "((m, n) : pred_nat^+) = (m < n)"
berghofe@13449
   175
  apply (unfold less_def)
berghofe@13449
   176
  apply (rule refl)
berghofe@13449
   177
  done
berghofe@13449
   178
berghofe@13449
   179
subsubsection {* Introduction properties *}
berghofe@13449
   180
berghofe@13449
   181
lemma less_trans: "i < j ==> j < k ==> i < (k::nat)"
berghofe@13449
   182
  apply (unfold less_def)
berghofe@13449
   183
  apply (rule trans_trancl [THEN transD])
berghofe@13449
   184
  apply assumption+
berghofe@13449
   185
  done
berghofe@13449
   186
berghofe@13449
   187
lemma lessI [iff]: "n < Suc n"
berghofe@13449
   188
  apply (unfold less_def pred_nat_def)
berghofe@13449
   189
  apply (simp add: r_into_trancl)
berghofe@13449
   190
  done
berghofe@13449
   191
berghofe@13449
   192
lemma less_SucI: "i < j ==> i < Suc j"
berghofe@13449
   193
  apply (rule less_trans)
berghofe@13449
   194
  apply assumption
berghofe@13449
   195
  apply (rule lessI)
berghofe@13449
   196
  done
berghofe@13449
   197
berghofe@13449
   198
lemma zero_less_Suc [iff]: "0 < Suc n"
berghofe@13449
   199
  apply (induct n)
berghofe@13449
   200
  apply (rule lessI)
berghofe@13449
   201
  apply (erule less_trans)
berghofe@13449
   202
  apply (rule lessI)
berghofe@13449
   203
  done
berghofe@13449
   204
berghofe@13449
   205
subsubsection {* Elimination properties *}
berghofe@13449
   206
berghofe@13449
   207
lemma less_not_sym: "n < m ==> ~ m < (n::nat)"
berghofe@13449
   208
  apply (unfold less_def)
berghofe@13449
   209
  apply (blast intro: wf_pred_nat wf_trancl [THEN wf_asym])
berghofe@13449
   210
  done
berghofe@13449
   211
berghofe@13449
   212
lemma less_asym:
berghofe@13449
   213
  assumes h1: "(n::nat) < m" and h2: "~ P ==> m < n" shows P
berghofe@13449
   214
  apply (rule contrapos_np)
berghofe@13449
   215
  apply (rule less_not_sym)
berghofe@13449
   216
  apply (rule h1)
berghofe@13449
   217
  apply (erule h2)
berghofe@13449
   218
  done
berghofe@13449
   219
berghofe@13449
   220
lemma less_not_refl: "~ n < (n::nat)"
berghofe@13449
   221
  apply (unfold less_def)
berghofe@13449
   222
  apply (rule wf_pred_nat [THEN wf_trancl, THEN wf_not_refl])
berghofe@13449
   223
  done
berghofe@13449
   224
berghofe@13449
   225
lemma less_irrefl [elim!]: "(n::nat) < n ==> R"
berghofe@13449
   226
  by (rule notE, rule less_not_refl)
berghofe@13449
   227
berghofe@13449
   228
lemma less_not_refl2: "n < m ==> m ~= (n::nat)" by blast
berghofe@13449
   229
berghofe@13449
   230
lemma less_not_refl3: "(s::nat) < t ==> s ~= t"
berghofe@13449
   231
  by (rule not_sym, rule less_not_refl2)
berghofe@13449
   232
berghofe@13449
   233
lemma lessE:
berghofe@13449
   234
  assumes major: "i < k"
berghofe@13449
   235
  and p1: "k = Suc i ==> P" and p2: "!!j. i < j ==> k = Suc j ==> P"
berghofe@13449
   236
  shows P
berghofe@13449
   237
  apply (rule major [unfolded less_def pred_nat_def, THEN tranclE])
berghofe@13449
   238
  apply simp_all
berghofe@13449
   239
  apply (erule p1)
berghofe@13449
   240
  apply (rule p2)
berghofe@13449
   241
  apply (simp add: less_def pred_nat_def)
berghofe@13449
   242
  apply assumption
berghofe@13449
   243
  done
berghofe@13449
   244
berghofe@13449
   245
lemma not_less0 [iff]: "~ n < (0::nat)"
berghofe@13449
   246
  by (blast elim: lessE)
berghofe@13449
   247
berghofe@13449
   248
lemma less_zeroE: "(n::nat) < 0 ==> R"
berghofe@13449
   249
  by (rule notE, rule not_less0)
berghofe@13449
   250
berghofe@13449
   251
lemma less_SucE: assumes major: "m < Suc n"
berghofe@13449
   252
  and less: "m < n ==> P" and eq: "m = n ==> P" shows P
berghofe@13449
   253
  apply (rule major [THEN lessE])
berghofe@13449
   254
  apply (rule eq)
berghofe@13449
   255
  apply blast
berghofe@13449
   256
  apply (rule less)
berghofe@13449
   257
  apply blast
berghofe@13449
   258
  done
berghofe@13449
   259
berghofe@13449
   260
lemma less_Suc_eq: "(m < Suc n) = (m < n | m = n)"
berghofe@13449
   261
  by (blast elim!: less_SucE intro: less_trans)
berghofe@13449
   262
berghofe@13449
   263
lemma less_one [iff]: "(n < (1::nat)) = (n = 0)"
berghofe@13449
   264
  by (simp add: less_Suc_eq)
berghofe@13449
   265
berghofe@13449
   266
lemma less_Suc0 [iff]: "(n < Suc 0) = (n = 0)"
berghofe@13449
   267
  by (simp add: less_Suc_eq)
berghofe@13449
   268
berghofe@13449
   269
lemma Suc_mono: "m < n ==> Suc m < Suc n"
berghofe@13449
   270
  by (induct n) (fast elim: less_trans lessE)+
berghofe@13449
   271
berghofe@13449
   272
text {* "Less than" is a linear ordering *}
berghofe@13449
   273
lemma less_linear: "m < n | m = n | n < (m::nat)"
berghofe@13449
   274
  apply (induct_tac m)
berghofe@13449
   275
  apply (induct_tac n)
berghofe@13449
   276
  apply (rule refl [THEN disjI1, THEN disjI2])
berghofe@13449
   277
  apply (rule zero_less_Suc [THEN disjI1])
berghofe@13449
   278
  apply (blast intro: Suc_mono less_SucI elim: lessE)
berghofe@13449
   279
  done
berghofe@13449
   280
berghofe@13449
   281
lemma nat_neq_iff: "((m::nat) ~= n) = (m < n | n < m)"
berghofe@13449
   282
  using less_linear by blast
berghofe@13449
   283
berghofe@13449
   284
lemma nat_less_cases: assumes major: "(m::nat) < n ==> P n m"
berghofe@13449
   285
  and eqCase: "m = n ==> P n m" and lessCase: "n<m ==> P n m"
berghofe@13449
   286
  shows "P n m"
berghofe@13449
   287
  apply (rule less_linear [THEN disjE])
berghofe@13449
   288
  apply (erule_tac [2] disjE)
berghofe@13449
   289
  apply (erule lessCase)
berghofe@13449
   290
  apply (erule sym [THEN eqCase])
berghofe@13449
   291
  apply (erule major)
berghofe@13449
   292
  done
berghofe@13449
   293
berghofe@13449
   294
berghofe@13449
   295
subsubsection {* Inductive (?) properties *}
berghofe@13449
   296
berghofe@13449
   297
lemma Suc_lessI: "m < n ==> Suc m ~= n ==> Suc m < n"
berghofe@13449
   298
  apply (simp add: nat_neq_iff)
berghofe@13449
   299
  apply (blast elim!: less_irrefl less_SucE elim: less_asym)
berghofe@13449
   300
  done
berghofe@13449
   301
berghofe@13449
   302
lemma Suc_lessD: "Suc m < n ==> m < n"
berghofe@13449
   303
  apply (induct n)
berghofe@13449
   304
  apply (fast intro!: lessI [THEN less_SucI] elim: less_trans lessE)+
berghofe@13449
   305
  done
berghofe@13449
   306
berghofe@13449
   307
lemma Suc_lessE: assumes major: "Suc i < k"
berghofe@13449
   308
  and minor: "!!j. i < j ==> k = Suc j ==> P" shows P
berghofe@13449
   309
  apply (rule major [THEN lessE])
berghofe@13449
   310
  apply (erule lessI [THEN minor])
berghofe@13449
   311
  apply (erule Suc_lessD [THEN minor])
berghofe@13449
   312
  apply assumption
berghofe@13449
   313
  done
berghofe@13449
   314
berghofe@13449
   315
lemma Suc_less_SucD: "Suc m < Suc n ==> m < n"
berghofe@13449
   316
  by (blast elim: lessE dest: Suc_lessD)
wenzelm@4104
   317
berghofe@13449
   318
lemma Suc_less_eq [iff]: "(Suc m < Suc n) = (m < n)"
berghofe@13449
   319
  apply (rule iffI)
berghofe@13449
   320
  apply (erule Suc_less_SucD)
berghofe@13449
   321
  apply (erule Suc_mono)
berghofe@13449
   322
  done
berghofe@13449
   323
berghofe@13449
   324
lemma less_trans_Suc:
berghofe@13449
   325
  assumes le: "i < j" shows "j < k ==> Suc i < k"
berghofe@13449
   326
  apply (induct k)
berghofe@13449
   327
  apply simp_all
berghofe@13449
   328
  apply (insert le)
berghofe@13449
   329
  apply (simp add: less_Suc_eq)
berghofe@13449
   330
  apply (blast dest: Suc_lessD)
berghofe@13449
   331
  done
berghofe@13449
   332
berghofe@13449
   333
text {* Can be used with @{text less_Suc_eq} to get @{term "n = m | n < m"} *}
berghofe@13449
   334
lemma not_less_eq: "(~ m < n) = (n < Suc m)"
berghofe@13449
   335
  apply (rule_tac m = "m" and n = "n" in diff_induct)
berghofe@13449
   336
  apply simp_all
berghofe@13449
   337
  done
berghofe@13449
   338
berghofe@13449
   339
text {* Complete induction, aka course-of-values induction *}
berghofe@13449
   340
lemma nat_less_induct:
berghofe@13449
   341
  assumes prem: "!!n. ALL m::nat. m < n --> P m ==> P n" shows "P n"
berghofe@13449
   342
  apply (rule_tac a=n in wf_induct)
berghofe@13449
   343
  apply (rule wf_pred_nat [THEN wf_trancl])
berghofe@13449
   344
  apply (rule prem)
berghofe@13449
   345
  apply (unfold less_def)
berghofe@13449
   346
  apply assumption
berghofe@13449
   347
  done
berghofe@13449
   348
paulson@14131
   349
lemmas less_induct = nat_less_induct [rule_format, case_names less]
paulson@14131
   350
paulson@14131
   351
subsection {* Properties of "less than or equal" *}
berghofe@13449
   352
berghofe@13449
   353
text {* Was @{text le_eq_less_Suc}, but this orientation is more useful *}
berghofe@13449
   354
lemma less_Suc_eq_le: "(m < Suc n) = (m <= n)"
berghofe@13449
   355
  by (unfold le_def, rule not_less_eq [symmetric])
berghofe@13449
   356
berghofe@13449
   357
lemma le_imp_less_Suc: "m <= n ==> m < Suc n"
berghofe@13449
   358
  by (rule less_Suc_eq_le [THEN iffD2])
berghofe@13449
   359
berghofe@13449
   360
lemma le0 [iff]: "(0::nat) <= n"
berghofe@13449
   361
  by (unfold le_def, rule not_less0)
berghofe@13449
   362
berghofe@13449
   363
lemma Suc_n_not_le_n: "~ Suc n <= n"
berghofe@13449
   364
  by (simp add: le_def)
berghofe@13449
   365
berghofe@13449
   366
lemma le_0_eq [iff]: "((i::nat) <= 0) = (i = 0)"
berghofe@13449
   367
  by (induct i) (simp_all add: le_def)
berghofe@13449
   368
berghofe@13449
   369
lemma le_Suc_eq: "(m <= Suc n) = (m <= n | m = Suc n)"
berghofe@13449
   370
  by (simp del: less_Suc_eq_le add: less_Suc_eq_le [symmetric] less_Suc_eq)
berghofe@13449
   371
berghofe@13449
   372
lemma le_SucE: "m <= Suc n ==> (m <= n ==> R) ==> (m = Suc n ==> R) ==> R"
berghofe@13449
   373
  by (drule le_Suc_eq [THEN iffD1], rules+)
berghofe@13449
   374
berghofe@13449
   375
lemma leI: "~ n < m ==> m <= (n::nat)" by (simp add: le_def)
berghofe@13449
   376
berghofe@13449
   377
lemma leD: "m <= n ==> ~ n < (m::nat)"
berghofe@13449
   378
  by (simp add: le_def)
berghofe@13449
   379
berghofe@13449
   380
lemmas leE = leD [elim_format]
berghofe@13449
   381
berghofe@13449
   382
lemma not_less_iff_le: "(~ n < m) = (m <= (n::nat))"
berghofe@13449
   383
  by (blast intro: leI elim: leE)
berghofe@13449
   384
berghofe@13449
   385
lemma not_leE: "~ m <= n ==> n<(m::nat)"
berghofe@13449
   386
  by (simp add: le_def)
berghofe@13449
   387
berghofe@13449
   388
lemma not_le_iff_less: "(~ n <= m) = (m < (n::nat))"
berghofe@13449
   389
  by (simp add: le_def)
berghofe@13449
   390
berghofe@13449
   391
lemma Suc_leI: "m < n ==> Suc(m) <= n"
berghofe@13449
   392
  apply (simp add: le_def less_Suc_eq)
berghofe@13449
   393
  apply (blast elim!: less_irrefl less_asym)
berghofe@13449
   394
  done -- {* formerly called lessD *}
berghofe@13449
   395
berghofe@13449
   396
lemma Suc_leD: "Suc(m) <= n ==> m <= n"
berghofe@13449
   397
  by (simp add: le_def less_Suc_eq)
berghofe@13449
   398
berghofe@13449
   399
text {* Stronger version of @{text Suc_leD} *}
berghofe@13449
   400
lemma Suc_le_lessD: "Suc m <= n ==> m < n"
berghofe@13449
   401
  apply (simp add: le_def less_Suc_eq)
berghofe@13449
   402
  using less_linear
berghofe@13449
   403
  apply blast
berghofe@13449
   404
  done
berghofe@13449
   405
berghofe@13449
   406
lemma Suc_le_eq: "(Suc m <= n) = (m < n)"
berghofe@13449
   407
  by (blast intro: Suc_leI Suc_le_lessD)
berghofe@13449
   408
berghofe@13449
   409
lemma le_SucI: "m <= n ==> m <= Suc n"
berghofe@13449
   410
  by (unfold le_def) (blast dest: Suc_lessD)
berghofe@13449
   411
berghofe@13449
   412
lemma less_imp_le: "m < n ==> m <= (n::nat)"
berghofe@13449
   413
  by (unfold le_def) (blast elim: less_asym)
berghofe@13449
   414
berghofe@13449
   415
text {* For instance, @{text "(Suc m < Suc n) = (Suc m <= n) = (m < n)"} *}
berghofe@13449
   416
lemmas le_simps = less_imp_le less_Suc_eq_le Suc_le_eq
berghofe@13449
   417
berghofe@13449
   418
berghofe@13449
   419
text {* Equivalence of @{term "m <= n"} and @{term "m < n | m = n"} *}
berghofe@13449
   420
berghofe@13449
   421
lemma le_imp_less_or_eq: "m <= n ==> m < n | m = (n::nat)"
berghofe@13449
   422
  apply (unfold le_def)
berghofe@13449
   423
  using less_linear
berghofe@13449
   424
  apply (blast elim: less_irrefl less_asym)
berghofe@13449
   425
  done
berghofe@13449
   426
berghofe@13449
   427
lemma less_or_eq_imp_le: "m < n | m = n ==> m <= (n::nat)"
berghofe@13449
   428
  apply (unfold le_def)
berghofe@13449
   429
  using less_linear
berghofe@13449
   430
  apply (blast elim!: less_irrefl elim: less_asym)
berghofe@13449
   431
  done
berghofe@13449
   432
berghofe@13449
   433
lemma le_eq_less_or_eq: "(m <= (n::nat)) = (m < n | m=n)"
berghofe@13449
   434
  by (rules intro: less_or_eq_imp_le le_imp_less_or_eq)
berghofe@13449
   435
berghofe@13449
   436
text {* Useful with @{text Blast}. *}
berghofe@13449
   437
lemma eq_imp_le: "(m::nat) = n ==> m <= n"
berghofe@13449
   438
  by (rule less_or_eq_imp_le, rule disjI2)
berghofe@13449
   439
berghofe@13449
   440
lemma le_refl: "n <= (n::nat)"
berghofe@13449
   441
  by (simp add: le_eq_less_or_eq)
berghofe@13449
   442
berghofe@13449
   443
lemma le_less_trans: "[| i <= j; j < k |] ==> i < (k::nat)"
berghofe@13449
   444
  by (blast dest!: le_imp_less_or_eq intro: less_trans)
berghofe@13449
   445
berghofe@13449
   446
lemma less_le_trans: "[| i < j; j <= k |] ==> i < (k::nat)"
berghofe@13449
   447
  by (blast dest!: le_imp_less_or_eq intro: less_trans)
berghofe@13449
   448
berghofe@13449
   449
lemma le_trans: "[| i <= j; j <= k |] ==> i <= (k::nat)"
berghofe@13449
   450
  by (blast dest!: le_imp_less_or_eq intro: less_or_eq_imp_le less_trans)
berghofe@13449
   451
berghofe@13449
   452
lemma le_anti_sym: "[| m <= n; n <= m |] ==> m = (n::nat)"
berghofe@13449
   453
  by (blast dest!: le_imp_less_or_eq elim!: less_irrefl elim: less_asym)
berghofe@13449
   454
berghofe@13449
   455
lemma Suc_le_mono [iff]: "(Suc n <= Suc m) = (n <= m)"
berghofe@13449
   456
  by (simp add: le_simps)
berghofe@13449
   457
berghofe@13449
   458
text {* Axiom @{text order_less_le} of class @{text order}: *}
berghofe@13449
   459
lemma nat_less_le: "((m::nat) < n) = (m <= n & m ~= n)"
berghofe@13449
   460
  by (simp add: le_def nat_neq_iff) (blast elim!: less_asym)
berghofe@13449
   461
berghofe@13449
   462
lemma le_neq_implies_less: "(m::nat) <= n ==> m ~= n ==> m < n"
berghofe@13449
   463
  by (rule iffD2, rule nat_less_le, rule conjI)
berghofe@13449
   464
berghofe@13449
   465
text {* Axiom @{text linorder_linear} of class @{text linorder}: *}
berghofe@13449
   466
lemma nat_le_linear: "(m::nat) <= n | n <= m"
berghofe@13449
   467
  apply (simp add: le_eq_less_or_eq)
berghofe@13449
   468
  using less_linear
berghofe@13449
   469
  apply blast
berghofe@13449
   470
  done
berghofe@13449
   471
berghofe@13449
   472
lemma not_less_less_Suc_eq: "~ n < m ==> (n < Suc m) = (n = m)"
berghofe@13449
   473
  by (blast elim!: less_SucE)
berghofe@13449
   474
berghofe@13449
   475
berghofe@13449
   476
text {*
berghofe@13449
   477
  Rewrite @{term "n < Suc m"} to @{term "n = m"}
berghofe@13449
   478
  if @{term "~ n < m"} or @{term "m <= n"} hold.
berghofe@13449
   479
  Not suitable as default simprules because they often lead to looping
berghofe@13449
   480
*}
berghofe@13449
   481
lemma le_less_Suc_eq: "m <= n ==> (n < Suc m) = (n = m)"
berghofe@13449
   482
  by (rule not_less_less_Suc_eq, rule leD)
berghofe@13449
   483
berghofe@13449
   484
lemmas not_less_simps = not_less_less_Suc_eq le_less_Suc_eq
berghofe@13449
   485
berghofe@13449
   486
berghofe@13449
   487
text {*
berghofe@13449
   488
  Re-orientation of the equations @{text "0 = x"} and @{text "1 = x"}. 
berghofe@13449
   489
  No longer added as simprules (they loop) 
berghofe@13449
   490
  but via @{text reorient_simproc} in Bin
berghofe@13449
   491
*}
berghofe@13449
   492
berghofe@13449
   493
text {* Polymorphic, not just for @{typ nat} *}
berghofe@13449
   494
lemma zero_reorient: "(0 = x) = (x = 0)"
berghofe@13449
   495
  by auto
berghofe@13449
   496
berghofe@13449
   497
lemma one_reorient: "(1 = x) = (x = 1)"
berghofe@13449
   498
  by auto
berghofe@13449
   499
berghofe@13449
   500
text {* Type {@typ nat} is a wellfounded linear order *}
berghofe@13449
   501
berghofe@13449
   502
instance nat :: order by (intro_classes,
berghofe@13449
   503
  (assumption | rule le_refl le_trans le_anti_sym nat_less_le)+)
berghofe@13449
   504
instance nat :: linorder by (intro_classes, rule nat_le_linear)
berghofe@13449
   505
instance nat :: wellorder by (intro_classes, rule wf_less)
berghofe@13449
   506
berghofe@13449
   507
subsection {* Arithmetic operators *}
oheimb@1660
   508
wenzelm@12338
   509
axclass power < type
wenzelm@10435
   510
paulson@3370
   511
consts
berghofe@13449
   512
  power :: "('a::power) => nat => 'a"            (infixr "^" 80)
paulson@3370
   513
wenzelm@9436
   514
berghofe@13449
   515
text {* arithmetic operators @{text "+ -"} and @{text "*"} *}
berghofe@13449
   516
berghofe@13449
   517
instance nat :: plus ..
berghofe@13449
   518
instance nat :: minus ..
berghofe@13449
   519
instance nat :: times ..
berghofe@13449
   520
instance nat :: power ..
wenzelm@9436
   521
berghofe@13449
   522
text {* size of a datatype value; overloaded *}
berghofe@13449
   523
consts size :: "'a => nat"
wenzelm@9436
   524
berghofe@13449
   525
primrec
berghofe@13449
   526
  add_0:    "0 + n = n"
berghofe@13449
   527
  add_Suc:  "Suc m + n = Suc (m + n)"
berghofe@13449
   528
berghofe@13449
   529
primrec
berghofe@13449
   530
  diff_0:   "m - 0 = m"
berghofe@13449
   531
  diff_Suc: "m - Suc n = (case m - n of 0 => 0 | Suc k => k)"
wenzelm@9436
   532
wenzelm@9436
   533
primrec
berghofe@13449
   534
  mult_0:   "0 * n = 0"
berghofe@13449
   535
  mult_Suc: "Suc m * n = n + (m * n)"
berghofe@13449
   536
berghofe@13449
   537
text {* These 2 rules ease the use of primitive recursion. NOTE USE OF @{text "=="} *}
berghofe@13449
   538
lemma def_nat_rec_0: "(!!n. f n == nat_rec c h n) ==> f 0 = c"
berghofe@13449
   539
  by simp
berghofe@13449
   540
berghofe@13449
   541
lemma def_nat_rec_Suc: "(!!n. f n == nat_rec c h n) ==> f (Suc n) = h n (f n)"
berghofe@13449
   542
  by simp
berghofe@13449
   543
berghofe@13449
   544
lemma not0_implies_Suc: "n ~= 0 ==> EX m. n = Suc m"
berghofe@13449
   545
  by (case_tac n) simp_all
berghofe@13449
   546
berghofe@13449
   547
lemma gr_implies_not0: "!!n::nat. m<n ==> n ~= 0"
berghofe@13449
   548
  by (case_tac n) simp_all
berghofe@13449
   549
berghofe@13449
   550
lemma neq0_conv [iff]: "!!n::nat. (n ~= 0) = (0 < n)"
berghofe@13449
   551
  by (case_tac n) simp_all
berghofe@13449
   552
berghofe@13449
   553
text {* This theorem is useful with @{text blast} *}
berghofe@13449
   554
lemma gr0I: "((n::nat) = 0 ==> False) ==> 0 < n"
berghofe@13449
   555
  by (rule iffD1, rule neq0_conv, rules)
berghofe@13449
   556
berghofe@13449
   557
lemma gr0_conv_Suc: "(0 < n) = (EX m. n = Suc m)"
berghofe@13449
   558
  by (fast intro: not0_implies_Suc)
berghofe@13449
   559
berghofe@13449
   560
lemma not_gr0 [iff]: "!!n::nat. (~ (0 < n)) = (n = 0)"
berghofe@13449
   561
  apply (rule iffI)
berghofe@13449
   562
  apply (rule ccontr)
berghofe@13449
   563
  apply simp_all
berghofe@13449
   564
  done
berghofe@13449
   565
berghofe@13449
   566
lemma Suc_le_D: "(Suc n <= m') ==> (? m. m' = Suc m)"
berghofe@13449
   567
  by (induct m') simp_all
berghofe@13449
   568
berghofe@13449
   569
text {* Useful in certain inductive arguments *}
berghofe@13449
   570
lemma less_Suc_eq_0_disj: "(m < Suc n) = (m = 0 | (EX j. m = Suc j & j < n))"
berghofe@13449
   571
  by (case_tac m) simp_all
berghofe@13449
   572
berghofe@13449
   573
lemma nat_induct2: "P 0 ==> P (Suc 0) ==> (!!k. P k ==> P (Suc (Suc k))) ==> P n"
berghofe@13449
   574
  apply (rule nat_less_induct)
berghofe@13449
   575
  apply (case_tac n)
berghofe@13449
   576
  apply (case_tac [2] nat)
berghofe@13449
   577
  apply (blast intro: less_trans)+
berghofe@13449
   578
  done
berghofe@13449
   579
berghofe@13449
   580
subsection {* @{text LEAST} theorems for type @{typ nat} by specialization *}
berghofe@13449
   581
berghofe@13449
   582
lemmas LeastI = wellorder_LeastI
berghofe@13449
   583
lemmas Least_le = wellorder_Least_le
berghofe@13449
   584
lemmas not_less_Least = wellorder_not_less_Least
berghofe@13449
   585
berghofe@13449
   586
lemma Least_Suc: "[| P n; ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))"
berghofe@13449
   587
  apply (case_tac "n")
berghofe@13449
   588
  apply auto
berghofe@13449
   589
  apply (frule LeastI)
berghofe@13449
   590
  apply (drule_tac P = "%x. P (Suc x) " in LeastI)
berghofe@13449
   591
  apply (subgoal_tac " (LEAST x. P x) <= Suc (LEAST x. P (Suc x))")
berghofe@13449
   592
  apply (erule_tac [2] Least_le)
berghofe@13449
   593
  apply (case_tac "LEAST x. P x")
berghofe@13449
   594
  apply auto
berghofe@13449
   595
  apply (drule_tac P = "%x. P (Suc x) " in Least_le)
berghofe@13449
   596
  apply (blast intro: order_antisym)
berghofe@13449
   597
  done
berghofe@13449
   598
berghofe@13449
   599
lemma Least_Suc2: "[|P n; Q m; ~P 0; !k. P (Suc k) = Q k|] ==> Least P = Suc (Least Q)"
berghofe@13449
   600
  apply (erule (1) Least_Suc [THEN ssubst])
berghofe@13449
   601
  apply simp
berghofe@13449
   602
  done
berghofe@13449
   603
berghofe@13449
   604
berghofe@13449
   605
subsection {* @{term min} and @{term max} *}
berghofe@13449
   606
berghofe@13449
   607
lemma min_0L [simp]: "min 0 n = (0::nat)"
berghofe@13449
   608
  by (rule min_leastL) simp
berghofe@13449
   609
berghofe@13449
   610
lemma min_0R [simp]: "min n 0 = (0::nat)"
berghofe@13449
   611
  by (rule min_leastR) simp
berghofe@13449
   612
berghofe@13449
   613
lemma min_Suc_Suc [simp]: "min (Suc m) (Suc n) = Suc (min m n)"
berghofe@13449
   614
  by (simp add: min_of_mono)
berghofe@13449
   615
berghofe@13449
   616
lemma max_0L [simp]: "max 0 n = (n::nat)"
berghofe@13449
   617
  by (rule max_leastL) simp
berghofe@13449
   618
berghofe@13449
   619
lemma max_0R [simp]: "max n 0 = (n::nat)"
berghofe@13449
   620
  by (rule max_leastR) simp
berghofe@13449
   621
berghofe@13449
   622
lemma max_Suc_Suc [simp]: "max (Suc m) (Suc n) = Suc(max m n)"
berghofe@13449
   623
  by (simp add: max_of_mono)
berghofe@13449
   624
berghofe@13449
   625
berghofe@13449
   626
subsection {* Basic rewrite rules for the arithmetic operators *}
berghofe@13449
   627
berghofe@13449
   628
text {* Difference *}
berghofe@13449
   629
berghofe@13449
   630
lemma diff_0_eq_0 [simp]: "0 - n = (0::nat)"
berghofe@13449
   631
  by (induct_tac n) simp_all
berghofe@13449
   632
berghofe@13449
   633
lemma diff_Suc_Suc [simp]: "Suc(m) - Suc(n) = m - n"
berghofe@13449
   634
  by (induct_tac n) simp_all
berghofe@13449
   635
berghofe@13449
   636
berghofe@13449
   637
text {*
berghofe@13449
   638
  Could be (and is, below) generalized in various ways
berghofe@13449
   639
  However, none of the generalizations are currently in the simpset,
berghofe@13449
   640
  and I dread to think what happens if I put them in
berghofe@13449
   641
*}
berghofe@13449
   642
lemma Suc_pred [simp]: "0 < n ==> Suc (n - Suc 0) = n"
berghofe@13449
   643
  by (simp split add: nat.split)
berghofe@13449
   644
berghofe@13449
   645
declare diff_Suc [simp del]
berghofe@13449
   646
berghofe@13449
   647
berghofe@13449
   648
subsection {* Addition *}
berghofe@13449
   649
berghofe@13449
   650
lemma add_0_right [simp]: "m + 0 = (m::nat)"
berghofe@13449
   651
  by (induct m) simp_all
berghofe@13449
   652
berghofe@13449
   653
lemma add_Suc_right [simp]: "m + Suc n = Suc (m + n)"
berghofe@13449
   654
  by (induct m) simp_all
berghofe@13449
   655
berghofe@13449
   656
berghofe@13449
   657
text {* Associative law for addition *}
berghofe@13449
   658
lemma add_assoc: "(m + n) + k = m + ((n + k)::nat)"
berghofe@13449
   659
  by (induct m) simp_all
berghofe@13449
   660
berghofe@13449
   661
text {* Commutative law for addition *}
berghofe@13449
   662
lemma add_commute: "m + n = n + (m::nat)"
berghofe@13449
   663
  by (induct m) simp_all
berghofe@13449
   664
berghofe@13449
   665
lemma add_left_commute: "x + (y + z) = y + ((x + z)::nat)"
berghofe@13449
   666
  apply (rule mk_left_commute [of "op +"])
berghofe@13449
   667
  apply (rule add_assoc)
berghofe@13449
   668
  apply (rule add_commute)
berghofe@13449
   669
  done
berghofe@13449
   670
berghofe@13449
   671
text {* Addition is an AC-operator *}
berghofe@13449
   672
lemmas add_ac = add_assoc add_commute add_left_commute
berghofe@13449
   673
berghofe@13449
   674
lemma add_left_cancel [simp]: "(k + m = k + n) = (m = (n::nat))"
berghofe@13449
   675
  by (induct k) simp_all
berghofe@13449
   676
berghofe@13449
   677
lemma add_right_cancel [simp]: "(m + k = n + k) = (m=(n::nat))"
berghofe@13449
   678
  by (induct k) simp_all
berghofe@13449
   679
berghofe@13449
   680
lemma add_left_cancel_le [simp]: "(k + m <= k + n) = (m<=(n::nat))"
berghofe@13449
   681
  by (induct k) simp_all
berghofe@13449
   682
berghofe@13449
   683
lemma add_left_cancel_less [simp]: "(k + m < k + n) = (m<(n::nat))"
berghofe@13449
   684
  by (induct k) simp_all
berghofe@13449
   685
berghofe@13449
   686
text {* Reasoning about @{text "m + 0 = 0"}, etc. *}
berghofe@13449
   687
berghofe@13449
   688
lemma add_is_0 [iff]: "!!m::nat. (m + n = 0) = (m = 0 & n = 0)"
berghofe@13449
   689
  by (case_tac m) simp_all
berghofe@13449
   690
berghofe@13449
   691
lemma add_is_1: "(m+n= Suc 0) = (m= Suc 0 & n=0 | m=0 & n= Suc 0)"
berghofe@13449
   692
  by (case_tac m) simp_all
berghofe@13449
   693
berghofe@13449
   694
lemma one_is_add: "(Suc 0 = m + n) = (m = Suc 0 & n = 0 | m = 0 & n = Suc 0)"
berghofe@13449
   695
  by (rule trans, rule eq_commute, rule add_is_1)
berghofe@13449
   696
berghofe@13449
   697
lemma add_gr_0 [iff]: "!!m::nat. (0 < m + n) = (0 < m | 0 < n)"
berghofe@13449
   698
  by (simp del: neq0_conv add: neq0_conv [symmetric])
berghofe@13449
   699
berghofe@13449
   700
lemma add_eq_self_zero: "!!m::nat. m + n = m ==> n = 0"
berghofe@13449
   701
  apply (drule add_0_right [THEN ssubst])
berghofe@13449
   702
  apply (simp add: add_assoc del: add_0_right)
berghofe@13449
   703
  done
berghofe@13449
   704
berghofe@13449
   705
subsection {* Additional theorems about "less than" *}
berghofe@13449
   706
berghofe@13449
   707
text {* Deleted @{text less_natE}; instead use @{text "less_imp_Suc_add RS exE"} *}
berghofe@13449
   708
lemma less_imp_Suc_add: "m < n ==> (EX k. n = Suc (m + k))"
berghofe@13449
   709
  apply (induct n)
berghofe@13449
   710
  apply (simp_all add: order_le_less)
berghofe@13449
   711
  apply (blast elim!: less_SucE intro!: add_0_right [symmetric] add_Suc_right [symmetric])
berghofe@13449
   712
  done
berghofe@13449
   713
berghofe@13449
   714
lemma le_add2: "n <= ((m + n)::nat)"
berghofe@13449
   715
  apply (induct m)
berghofe@13449
   716
  apply simp_all
berghofe@13449
   717
  apply (erule le_SucI)
berghofe@13449
   718
  done
berghofe@13449
   719
berghofe@13449
   720
lemma le_add1: "n <= ((n + m)::nat)"
berghofe@13449
   721
  apply (simp add: add_ac)
berghofe@13449
   722
  apply (rule le_add2)
berghofe@13449
   723
  done
berghofe@13449
   724
berghofe@13449
   725
lemma less_add_Suc1: "i < Suc (i + m)"
berghofe@13449
   726
  by (rule le_less_trans, rule le_add1, rule lessI)
berghofe@13449
   727
berghofe@13449
   728
lemma less_add_Suc2: "i < Suc (m + i)"
berghofe@13449
   729
  by (rule le_less_trans, rule le_add2, rule lessI)
berghofe@13449
   730
berghofe@13449
   731
lemma less_iff_Suc_add: "(m < n) = (EX k. n = Suc (m + k))"
berghofe@13449
   732
  by (rules intro!: less_add_Suc1 less_imp_Suc_add)
berghofe@13449
   733
berghofe@13449
   734
berghofe@13449
   735
lemma trans_le_add1: "(i::nat) <= j ==> i <= j + m"
berghofe@13449
   736
  by (rule le_trans, assumption, rule le_add1)
berghofe@13449
   737
berghofe@13449
   738
lemma trans_le_add2: "(i::nat) <= j ==> i <= m + j"
berghofe@13449
   739
  by (rule le_trans, assumption, rule le_add2)
berghofe@13449
   740
berghofe@13449
   741
lemma trans_less_add1: "(i::nat) < j ==> i < j + m"
berghofe@13449
   742
  by (rule less_le_trans, assumption, rule le_add1)
berghofe@13449
   743
berghofe@13449
   744
lemma trans_less_add2: "(i::nat) < j ==> i < m + j"
berghofe@13449
   745
  by (rule less_le_trans, assumption, rule le_add2)
berghofe@13449
   746
berghofe@13449
   747
lemma add_lessD1: "i + j < (k::nat) ==> i < k"
berghofe@13449
   748
  apply (induct j)
berghofe@13449
   749
  apply simp_all
berghofe@13449
   750
  apply (blast dest: Suc_lessD)
berghofe@13449
   751
  done
berghofe@13449
   752
berghofe@13449
   753
lemma not_add_less1 [iff]: "~ (i + j < (i::nat))"
berghofe@13449
   754
  apply (rule notI)
berghofe@13449
   755
  apply (erule add_lessD1 [THEN less_irrefl])
berghofe@13449
   756
  done
berghofe@13449
   757
berghofe@13449
   758
lemma not_add_less2 [iff]: "~ (j + i < (i::nat))"
berghofe@13449
   759
  by (simp add: add_commute not_add_less1)
berghofe@13449
   760
berghofe@13449
   761
lemma add_leD1: "m + k <= n ==> m <= (n::nat)"
berghofe@13449
   762
  by (induct k) (simp_all add: le_simps)
berghofe@13449
   763
berghofe@13449
   764
lemma add_leD2: "m + k <= n ==> k <= (n::nat)"
berghofe@13449
   765
  apply (simp add: add_commute)
berghofe@13449
   766
  apply (erule add_leD1)
berghofe@13449
   767
  done
berghofe@13449
   768
berghofe@13449
   769
lemma add_leE: "(m::nat) + k <= n ==> (m <= n ==> k <= n ==> R) ==> R"
berghofe@13449
   770
  by (blast dest: add_leD1 add_leD2)
berghofe@13449
   771
berghofe@13449
   772
text {* needs @{text "!!k"} for @{text add_ac} to work *}
berghofe@13449
   773
lemma less_add_eq_less: "!!k::nat. k < l ==> m + l = k + n ==> m < n"
berghofe@13449
   774
  by (force simp del: add_Suc_right
berghofe@13449
   775
    simp add: less_iff_Suc_add add_Suc_right [symmetric] add_ac)
berghofe@13449
   776
berghofe@13449
   777
berghofe@13449
   778
subsection {* Monotonicity of Addition *}
berghofe@13449
   779
berghofe@13449
   780
text {* strict, in 1st argument *}
berghofe@13449
   781
lemma add_less_mono1: "i < j ==> i + k < j + (k::nat)"
berghofe@13449
   782
  by (induct k) simp_all
berghofe@13449
   783
berghofe@13449
   784
text {* strict, in both arguments *}
berghofe@13449
   785
lemma add_less_mono: "[|i < j; k < l|] ==> i + k < j + (l::nat)"
berghofe@13449
   786
  apply (rule add_less_mono1 [THEN less_trans])
berghofe@13449
   787
  apply assumption+
berghofe@13449
   788
  apply (induct_tac j)
berghofe@13449
   789
  apply simp_all
berghofe@13449
   790
  done
berghofe@13449
   791
berghofe@13449
   792
text {* A [clumsy] way of lifting @{text "<"}
berghofe@13449
   793
  monotonicity to @{text "<="} monotonicity *}
berghofe@13449
   794
lemma less_mono_imp_le_mono:
berghofe@13449
   795
  assumes lt_mono: "!!i j::nat. i < j ==> f i < f j"
berghofe@13449
   796
  and le: "i <= j" shows "f i <= ((f j)::nat)" using le
berghofe@13449
   797
  apply (simp add: order_le_less)
berghofe@13449
   798
  apply (blast intro!: lt_mono)
berghofe@13449
   799
  done
berghofe@13449
   800
berghofe@13449
   801
text {* non-strict, in 1st argument *}
berghofe@13449
   802
lemma add_le_mono1: "i <= j ==> i + k <= j + (k::nat)"
berghofe@13449
   803
  apply (rule_tac f = "%j. j + k" in less_mono_imp_le_mono)
berghofe@13449
   804
  apply (erule add_less_mono1)
berghofe@13449
   805
  apply assumption
berghofe@13449
   806
  done
wenzelm@9436
   807
berghofe@13449
   808
text {* non-strict, in both arguments *}
berghofe@13449
   809
lemma add_le_mono: "[| i <= j;  k <= l |] ==> i + k <= j + (l::nat)"
berghofe@13449
   810
  apply (erule add_le_mono1 [THEN le_trans])
berghofe@13449
   811
  apply (simp add: add_commute)
berghofe@13449
   812
  done
berghofe@13449
   813
berghofe@13449
   814
berghofe@13449
   815
subsection {* Multiplication *}
berghofe@13449
   816
berghofe@13449
   817
text {* right annihilation in product *}
berghofe@13449
   818
lemma mult_0_right [simp]: "(m::nat) * 0 = 0"
berghofe@13449
   819
  by (induct m) simp_all
berghofe@13449
   820
berghofe@13449
   821
text {* right successor law for multiplication *}
berghofe@13449
   822
lemma mult_Suc_right [simp]: "m * Suc n = m + (m * n)"
berghofe@13449
   823
  by (induct m) (simp_all add: add_ac)
berghofe@13449
   824
berghofe@13449
   825
lemma mult_1: "(1::nat) * n = n" by simp
berghofe@13449
   826
berghofe@13449
   827
lemma mult_1_right: "n * (1::nat) = n" by simp
berghofe@13449
   828
berghofe@13449
   829
text {* Commutative law for multiplication *}
berghofe@13449
   830
lemma mult_commute: "m * n = n * (m::nat)"
berghofe@13449
   831
  by (induct m) simp_all
berghofe@13449
   832
berghofe@13449
   833
text {* addition distributes over multiplication *}
berghofe@13449
   834
lemma add_mult_distrib: "(m + n) * k = (m * k) + ((n * k)::nat)"
berghofe@13449
   835
  by (induct m) (simp_all add: add_ac)
berghofe@13449
   836
berghofe@13449
   837
lemma add_mult_distrib2: "k * (m + n) = (k * m) + ((k * n)::nat)"
berghofe@13449
   838
  by (induct m) (simp_all add: add_ac)
berghofe@13449
   839
berghofe@13449
   840
text {* Associative law for multiplication *}
berghofe@13449
   841
lemma mult_assoc: "(m * n) * k = m * ((n * k)::nat)"
berghofe@13449
   842
  by (induct m) (simp_all add: add_mult_distrib)
berghofe@13449
   843
berghofe@13449
   844
lemma mult_left_commute: "x * (y * z) = y * ((x * z)::nat)"
berghofe@13449
   845
  apply (rule mk_left_commute [of "op *"])
berghofe@13449
   846
  apply (rule mult_assoc)
berghofe@13449
   847
  apply (rule mult_commute)
berghofe@13449
   848
  done
berghofe@13449
   849
berghofe@13449
   850
lemmas mult_ac = mult_assoc mult_commute mult_left_commute
berghofe@13449
   851
berghofe@13449
   852
lemma mult_is_0 [simp]: "((m::nat) * n = 0) = (m=0 | n=0)"
berghofe@13449
   853
  apply (induct_tac m)
berghofe@13449
   854
  apply (induct_tac [2] n)
berghofe@13449
   855
  apply simp_all
berghofe@13449
   856
  done
berghofe@13449
   857
berghofe@13449
   858
berghofe@13449
   859
subsection {* Difference *}
berghofe@13449
   860
berghofe@13449
   861
lemma diff_self_eq_0 [simp]: "(m::nat) - m = 0"
berghofe@13449
   862
  by (induct m) simp_all
berghofe@13449
   863
berghofe@13449
   864
text {* Addition is the inverse of subtraction:
berghofe@13449
   865
  if @{term "n <= m"} then @{term "n + (m - n) = m"}. *}
berghofe@13449
   866
lemma add_diff_inverse: "~  m < n ==> n + (m - n) = (m::nat)"
berghofe@13449
   867
  by (induct m n rule: diff_induct) simp_all
berghofe@13449
   868
berghofe@13449
   869
lemma le_add_diff_inverse [simp]: "n <= m ==> n + (m - n) = (m::nat)"
berghofe@13449
   870
  by (simp add: add_diff_inverse not_less_iff_le)
berghofe@13449
   871
berghofe@13449
   872
lemma le_add_diff_inverse2 [simp]: "n <= m ==> (m - n) + n = (m::nat)"
berghofe@13449
   873
  by (simp add: le_add_diff_inverse add_commute)
berghofe@13449
   874
berghofe@13449
   875
berghofe@13449
   876
subsection {* More results about difference *}
berghofe@13449
   877
berghofe@13449
   878
lemma Suc_diff_le: "n <= m ==> Suc m - n = Suc (m - n)"
berghofe@13449
   879
  by (induct m n rule: diff_induct) simp_all
berghofe@13449
   880
berghofe@13449
   881
lemma diff_less_Suc: "m - n < Suc m"
berghofe@13449
   882
  apply (induct m n rule: diff_induct)
berghofe@13449
   883
  apply (erule_tac [3] less_SucE)
berghofe@13449
   884
  apply (simp_all add: less_Suc_eq)
berghofe@13449
   885
  done
berghofe@13449
   886
berghofe@13449
   887
lemma diff_le_self [simp]: "m - n <= (m::nat)"
berghofe@13449
   888
  by (induct m n rule: diff_induct) (simp_all add: le_SucI)
berghofe@13449
   889
berghofe@13449
   890
lemma less_imp_diff_less: "(j::nat) < k ==> j - n < k"
berghofe@13449
   891
  by (rule le_less_trans, rule diff_le_self)
berghofe@13449
   892
berghofe@13449
   893
lemma diff_diff_left: "(i::nat) - j - k = i - (j + k)"
berghofe@13449
   894
  by (induct i j rule: diff_induct) simp_all
berghofe@13449
   895
berghofe@13449
   896
lemma Suc_diff_diff [simp]: "(Suc m - n) - Suc k = m - n - k"
berghofe@13449
   897
  by (simp add: diff_diff_left)
berghofe@13449
   898
berghofe@13449
   899
lemma diff_Suc_less [simp]: "0<n ==> n - Suc i < n"
berghofe@13449
   900
  apply (case_tac "n")
berghofe@13449
   901
  apply safe
berghofe@13449
   902
  apply (simp add: le_simps)
berghofe@13449
   903
  done
berghofe@13449
   904
berghofe@13449
   905
text {* This and the next few suggested by Florian Kammueller *}
berghofe@13449
   906
lemma diff_commute: "(i::nat) - j - k = i - k - j"
berghofe@13449
   907
  by (simp add: diff_diff_left add_commute)
berghofe@13449
   908
berghofe@13449
   909
lemma diff_add_assoc: "k <= (j::nat) ==> (i + j) - k = i + (j - k)"
berghofe@13449
   910
  by (induct j k rule: diff_induct) simp_all
berghofe@13449
   911
berghofe@13449
   912
lemma diff_add_assoc2: "k <= (j::nat) ==> (j + i) - k = (j - k) + i"
berghofe@13449
   913
  by (simp add: add_commute diff_add_assoc)
berghofe@13449
   914
berghofe@13449
   915
lemma diff_add_inverse: "(n + m) - n = (m::nat)"
berghofe@13449
   916
  by (induct n) simp_all
berghofe@13449
   917
berghofe@13449
   918
lemma diff_add_inverse2: "(m + n) - n = (m::nat)"
berghofe@13449
   919
  by (simp add: diff_add_assoc)
berghofe@13449
   920
berghofe@13449
   921
lemma le_imp_diff_is_add: "i <= (j::nat) ==> (j - i = k) = (j = k + i)"
berghofe@13449
   922
  apply safe
berghofe@13449
   923
  apply (simp_all add: diff_add_inverse2)
berghofe@13449
   924
  done
berghofe@13449
   925
berghofe@13449
   926
lemma diff_is_0_eq [simp]: "((m::nat) - n = 0) = (m <= n)"
berghofe@13449
   927
  by (induct m n rule: diff_induct) simp_all
berghofe@13449
   928
berghofe@13449
   929
lemma diff_is_0_eq' [simp]: "m <= n ==> (m::nat) - n = 0"
berghofe@13449
   930
  by (rule iffD2, rule diff_is_0_eq)
berghofe@13449
   931
berghofe@13449
   932
lemma zero_less_diff [simp]: "(0 < n - (m::nat)) = (m < n)"
berghofe@13449
   933
  by (induct m n rule: diff_induct) simp_all
berghofe@13449
   934
berghofe@13449
   935
lemma less_imp_add_positive: "i < j  ==> EX k::nat. 0 < k & i + k = j"
berghofe@13449
   936
  apply (rule_tac x = "j - i" in exI)
berghofe@13449
   937
  apply (simp (no_asm_simp) add: add_diff_inverse less_not_sym)
berghofe@13449
   938
  done
wenzelm@9436
   939
berghofe@13449
   940
lemma zero_induct_lemma: "P k ==> (!!n. P (Suc n) ==> P n) ==> P (k - i)"
berghofe@13449
   941
  apply (induct k i rule: diff_induct)
berghofe@13449
   942
  apply (simp_all (no_asm))
berghofe@13449
   943
  apply rules
berghofe@13449
   944
  done
berghofe@13449
   945
berghofe@13449
   946
lemma zero_induct: "P k ==> (!!n. P (Suc n) ==> P n) ==> P 0"
berghofe@13449
   947
  apply (rule diff_self_eq_0 [THEN subst])
berghofe@13449
   948
  apply (rule zero_induct_lemma)
berghofe@13449
   949
  apply rules+
berghofe@13449
   950
  done
berghofe@13449
   951
berghofe@13449
   952
lemma diff_cancel: "(k + m) - (k + n) = m - (n::nat)"
berghofe@13449
   953
  by (induct k) simp_all
berghofe@13449
   954
berghofe@13449
   955
lemma diff_cancel2: "(m + k) - (n + k) = m - (n::nat)"
berghofe@13449
   956
  by (simp add: diff_cancel add_commute)
berghofe@13449
   957
berghofe@13449
   958
lemma diff_add_0: "n - (n + m) = (0::nat)"
berghofe@13449
   959
  by (induct n) simp_all
berghofe@13449
   960
berghofe@13449
   961
berghofe@13449
   962
text {* Difference distributes over multiplication *}
berghofe@13449
   963
berghofe@13449
   964
lemma diff_mult_distrib: "((m::nat) - n) * k = (m * k) - (n * k)"
berghofe@13449
   965
  by (induct m n rule: diff_induct) (simp_all add: diff_cancel)
berghofe@13449
   966
berghofe@13449
   967
lemma diff_mult_distrib2: "k * ((m::nat) - n) = (k * m) - (k * n)"
berghofe@13449
   968
  by (simp add: diff_mult_distrib mult_commute [of k])
berghofe@13449
   969
  -- {* NOT added as rewrites, since sometimes they are used from right-to-left *}
berghofe@13449
   970
berghofe@13449
   971
lemmas nat_distrib =
berghofe@13449
   972
  add_mult_distrib add_mult_distrib2 diff_mult_distrib diff_mult_distrib2
berghofe@13449
   973
berghofe@13449
   974
berghofe@13449
   975
subsection {* Monotonicity of Multiplication *}
berghofe@13449
   976
berghofe@13449
   977
lemma mult_le_mono1: "i <= (j::nat) ==> i * k <= j * k"
berghofe@13449
   978
  by (induct k) (simp_all add: add_le_mono)
berghofe@13449
   979
berghofe@13449
   980
lemma mult_le_mono2: "i <= (j::nat) ==> k * i <= k * j"
berghofe@13449
   981
  apply (drule mult_le_mono1)
berghofe@13449
   982
  apply (simp add: mult_commute)
berghofe@13449
   983
  done
berghofe@13449
   984
berghofe@13449
   985
text {* @{text "<="} monotonicity, BOTH arguments *}
berghofe@13449
   986
lemma mult_le_mono: "i <= (j::nat) ==> k <= l ==> i * k <= j * l"
berghofe@13449
   987
  apply (erule mult_le_mono1 [THEN le_trans])
berghofe@13449
   988
  apply (erule mult_le_mono2)
berghofe@13449
   989
  done
berghofe@13449
   990
berghofe@13449
   991
text {* strict, in 1st argument; proof is by induction on @{text "k > 0"} *}
berghofe@13449
   992
lemma mult_less_mono2: "(i::nat) < j ==> 0 < k ==> k * i < k * j"
berghofe@13449
   993
  apply (erule_tac m1 = "0" in less_imp_Suc_add [THEN exE])
berghofe@13449
   994
  apply simp
berghofe@13449
   995
  apply (induct_tac x)
berghofe@13449
   996
  apply (simp_all add: add_less_mono)
berghofe@13449
   997
  done
berghofe@13449
   998
berghofe@13449
   999
lemma mult_less_mono1: "(i::nat) < j ==> 0 < k ==> i * k < j * k"
berghofe@13449
  1000
  by (drule mult_less_mono2) (simp_all add: mult_commute)
berghofe@13449
  1001
berghofe@13449
  1002
lemma zero_less_mult_iff [simp]: "(0 < (m::nat) * n) = (0 < m & 0 < n)"
berghofe@13449
  1003
  apply (induct m)
berghofe@13449
  1004
  apply (case_tac [2] n)
berghofe@13449
  1005
  apply simp_all
berghofe@13449
  1006
  done
berghofe@13449
  1007
berghofe@13449
  1008
lemma one_le_mult_iff [simp]: "(Suc 0 <= m * n) = (1 <= m & 1 <= n)"
berghofe@13449
  1009
  apply (induct m)
berghofe@13449
  1010
  apply (case_tac [2] n)
berghofe@13449
  1011
  apply simp_all
berghofe@13449
  1012
  done
berghofe@13449
  1013
berghofe@13449
  1014
lemma mult_eq_1_iff [simp]: "(m * n = Suc 0) = (m = 1 & n = 1)"
berghofe@13449
  1015
  apply (induct_tac m)
berghofe@13449
  1016
  apply simp
berghofe@13449
  1017
  apply (induct_tac n)
berghofe@13449
  1018
  apply simp
berghofe@13449
  1019
  apply fastsimp
berghofe@13449
  1020
  done
berghofe@13449
  1021
berghofe@13449
  1022
lemma one_eq_mult_iff [simp]: "(Suc 0 = m * n) = (m = 1 & n = 1)"
berghofe@13449
  1023
  apply (rule trans)
berghofe@13449
  1024
  apply (rule_tac [2] mult_eq_1_iff)
berghofe@13449
  1025
  apply fastsimp
berghofe@13449
  1026
  done
berghofe@13449
  1027
berghofe@13449
  1028
lemma mult_less_cancel2: "((m::nat) * k < n * k) = (0 < k & m < n)"
berghofe@13449
  1029
  apply (safe intro!: mult_less_mono1)
berghofe@13449
  1030
  apply (case_tac k)
berghofe@13449
  1031
  apply auto
berghofe@13449
  1032
  apply (simp del: le_0_eq add: linorder_not_le [symmetric])
berghofe@13449
  1033
  apply (blast intro: mult_le_mono1)
berghofe@13449
  1034
  done
berghofe@13449
  1035
berghofe@13449
  1036
lemma mult_less_cancel1 [simp]: "(k * (m::nat) < k * n) = (0 < k & m < n)"
berghofe@13449
  1037
  by (simp add: mult_less_cancel2 mult_commute [of k])
berghofe@13449
  1038
berghofe@13449
  1039
declare mult_less_cancel2 [simp]
berghofe@13449
  1040
berghofe@13449
  1041
lemma mult_le_cancel1 [simp]: "(k * (m::nat) <= k * n) = (0 < k --> m <= n)"
berghofe@13449
  1042
  apply (simp add: linorder_not_less [symmetric])
berghofe@13449
  1043
  apply auto
berghofe@13449
  1044
  done
berghofe@13449
  1045
berghofe@13449
  1046
lemma mult_le_cancel2 [simp]: "((m::nat) * k <= n * k) = (0 < k --> m <= n)"
berghofe@13449
  1047
  apply (simp add: linorder_not_less [symmetric])
berghofe@13449
  1048
  apply auto
berghofe@13449
  1049
  done
berghofe@13449
  1050
berghofe@13449
  1051
lemma mult_cancel2: "(m * k = n * k) = (m = n | (k = (0::nat)))"
berghofe@13449
  1052
  apply (cut_tac less_linear)
berghofe@13449
  1053
  apply safe
berghofe@13449
  1054
  apply auto
berghofe@13449
  1055
  apply (drule mult_less_mono1, assumption, simp)+
berghofe@13449
  1056
  done
berghofe@13449
  1057
berghofe@13449
  1058
lemma mult_cancel1 [simp]: "(k * m = k * n) = (m = n | (k = (0::nat)))"
berghofe@13449
  1059
  by (simp add: mult_cancel2 mult_commute [of k])
berghofe@13449
  1060
berghofe@13449
  1061
declare mult_cancel2 [simp]
berghofe@13449
  1062
berghofe@13449
  1063
lemma Suc_mult_less_cancel1: "(Suc k * m < Suc k * n) = (m < n)"
berghofe@13449
  1064
  by (subst mult_less_cancel1) simp
berghofe@13449
  1065
berghofe@13449
  1066
lemma Suc_mult_le_cancel1: "(Suc k * m <= Suc k * n) = (m <= n)"
berghofe@13449
  1067
  by (subst mult_le_cancel1) simp
berghofe@13449
  1068
berghofe@13449
  1069
lemma Suc_mult_cancel1: "(Suc k * m = Suc k * n) = (m = n)"
berghofe@13449
  1070
  by (subst mult_cancel1) simp
berghofe@13449
  1071
berghofe@13449
  1072
berghofe@13449
  1073
text {* Lemma for @{text gcd} *}
berghofe@13449
  1074
lemma mult_eq_self_implies_10: "(m::nat) = m * n ==> n = 1 | m = 0"
berghofe@13449
  1075
  apply (drule sym)
berghofe@13449
  1076
  apply (rule disjCI)
berghofe@13449
  1077
  apply (rule nat_less_cases, erule_tac [2] _)
berghofe@13449
  1078
  apply (fastsimp elim!: less_SucE)
berghofe@13449
  1079
  apply (fastsimp dest: mult_less_mono2)
berghofe@13449
  1080
  done
wenzelm@9436
  1081
clasohm@923
  1082
end