src/HOL/Library/Extended_Nat.thy
author huffman
Wed Mar 06 10:44:43 2013 -0800 (2013-03-06)
changeset 51366 abdcf1a7cabf
parent 51301 6822aa82aafa
child 51717 9e7d1c139569
permissions -rw-r--r--
avoid using Arith_Data.dest_sum in extended-nat simprocs (it treats 'x - y' as 'x + - y', which is not valid for enat)
hoelzl@43919
     1
(*  Title:      HOL/Library/Extended_Nat.thy
haftmann@27110
     2
    Author:     David von Oheimb, TU Muenchen;  Florian Haftmann, TU Muenchen
nipkow@41853
     3
    Contributions: David Trachtenherz, TU Muenchen
oheimb@11351
     4
*)
oheimb@11351
     5
hoelzl@43919
     6
header {* Extended natural numbers (i.e. with infinity) *}
oheimb@11351
     7
hoelzl@43919
     8
theory Extended_Nat
wenzelm@51301
     9
imports Main
nipkow@15131
    10
begin
oheimb@11351
    11
hoelzl@43921
    12
class infinity =
hoelzl@43921
    13
  fixes infinity :: "'a"
hoelzl@43921
    14
hoelzl@43921
    15
notation (xsymbols)
hoelzl@43921
    16
  infinity  ("\<infinity>")
hoelzl@43921
    17
hoelzl@43921
    18
notation (HTML output)
hoelzl@43921
    19
  infinity  ("\<infinity>")
hoelzl@43921
    20
haftmann@27110
    21
subsection {* Type definition *}
oheimb@11351
    22
oheimb@11351
    23
text {*
wenzelm@11355
    24
  We extend the standard natural numbers by a special value indicating
haftmann@27110
    25
  infinity.
oheimb@11351
    26
*}
oheimb@11351
    27
wenzelm@49834
    28
typedef enat = "UNIV :: nat option set" ..
hoelzl@43921
    29
 
hoelzl@43924
    30
definition enat :: "nat \<Rightarrow> enat" where
hoelzl@43924
    31
  "enat n = Abs_enat (Some n)"
hoelzl@43921
    32
 
hoelzl@43921
    33
instantiation enat :: infinity
hoelzl@43921
    34
begin
hoelzl@43921
    35
  definition "\<infinity> = Abs_enat None"
hoelzl@43921
    36
  instance proof qed
hoelzl@43921
    37
end
hoelzl@43921
    38
 
hoelzl@43924
    39
rep_datatype enat "\<infinity> :: enat"
hoelzl@43921
    40
proof -
hoelzl@43924
    41
  fix P i assume "\<And>j. P (enat j)" "P \<infinity>"
hoelzl@43921
    42
  then show "P i"
hoelzl@43921
    43
  proof induct
hoelzl@43921
    44
    case (Abs_enat y) then show ?case
hoelzl@43921
    45
      by (cases y rule: option.exhaust)
hoelzl@43924
    46
         (auto simp: enat_def infinity_enat_def)
hoelzl@43921
    47
  qed
hoelzl@43924
    48
qed (auto simp add: enat_def infinity_enat_def Abs_enat_inject)
wenzelm@19736
    49
hoelzl@43924
    50
declare [[coercion "enat::nat\<Rightarrow>enat"]]
wenzelm@19736
    51
noschinl@45934
    52
lemmas enat2_cases = enat.exhaust[case_product enat.exhaust]
noschinl@45934
    53
lemmas enat3_cases = enat.exhaust[case_product enat.exhaust enat.exhaust]
noschinl@45934
    54
huffman@44019
    55
lemma not_infinity_eq [iff]: "(x \<noteq> \<infinity>) = (EX i. x = enat i)"
huffman@44019
    56
  by (cases x) auto
nipkow@31084
    57
hoelzl@43924
    58
lemma not_enat_eq [iff]: "(ALL y. x ~= enat y) = (x = \<infinity>)"
huffman@44019
    59
  by (cases x) auto
nipkow@31077
    60
hoelzl@43924
    61
primrec the_enat :: "enat \<Rightarrow> nat"
huffman@44019
    62
  where "the_enat (enat n) = n"
nipkow@41855
    63
huffman@47108
    64
haftmann@27110
    65
subsection {* Constructors and numbers *}
haftmann@27110
    66
huffman@47108
    67
instantiation enat :: "{zero, one}"
haftmann@25594
    68
begin
haftmann@25594
    69
haftmann@25594
    70
definition
hoelzl@43924
    71
  "0 = enat 0"
haftmann@25594
    72
haftmann@25594
    73
definition
huffman@47108
    74
  "1 = enat 1"
oheimb@11351
    75
haftmann@25594
    76
instance ..
haftmann@25594
    77
haftmann@25594
    78
end
haftmann@25594
    79
huffman@44019
    80
definition eSuc :: "enat \<Rightarrow> enat" where
huffman@44019
    81
  "eSuc i = (case i of enat n \<Rightarrow> enat (Suc n) | \<infinity> \<Rightarrow> \<infinity>)"
oheimb@11351
    82
huffman@47108
    83
lemma enat_0 [code_post]: "enat 0 = 0"
hoelzl@43919
    84
  by (simp add: zero_enat_def)
haftmann@27110
    85
huffman@47108
    86
lemma enat_1 [code_post]: "enat 1 = 1"
hoelzl@43919
    87
  by (simp add: one_enat_def)
haftmann@27110
    88
huffman@44019
    89
lemma one_eSuc: "1 = eSuc 0"
huffman@44019
    90
  by (simp add: zero_enat_def one_enat_def eSuc_def)
oheimb@11351
    91
huffman@44019
    92
lemma infinity_ne_i0 [simp]: "(\<infinity>::enat) \<noteq> 0"
hoelzl@43919
    93
  by (simp add: zero_enat_def)
oheimb@11351
    94
huffman@44019
    95
lemma i0_ne_infinity [simp]: "0 \<noteq> (\<infinity>::enat)"
hoelzl@43919
    96
  by (simp add: zero_enat_def)
haftmann@27110
    97
hoelzl@43919
    98
lemma zero_one_enat_neq [simp]:
hoelzl@43919
    99
  "\<not> 0 = (1\<Colon>enat)"
hoelzl@43919
   100
  "\<not> 1 = (0\<Colon>enat)"
hoelzl@43919
   101
  unfolding zero_enat_def one_enat_def by simp_all
oheimb@11351
   102
huffman@44019
   103
lemma infinity_ne_i1 [simp]: "(\<infinity>::enat) \<noteq> 1"
hoelzl@43919
   104
  by (simp add: one_enat_def)
haftmann@27110
   105
huffman@44019
   106
lemma i1_ne_infinity [simp]: "1 \<noteq> (\<infinity>::enat)"
hoelzl@43919
   107
  by (simp add: one_enat_def)
haftmann@27110
   108
huffman@44019
   109
lemma eSuc_enat: "eSuc (enat n) = enat (Suc n)"
huffman@44019
   110
  by (simp add: eSuc_def)
haftmann@27110
   111
huffman@44019
   112
lemma eSuc_infinity [simp]: "eSuc \<infinity> = \<infinity>"
huffman@44019
   113
  by (simp add: eSuc_def)
oheimb@11351
   114
huffman@44019
   115
lemma eSuc_ne_0 [simp]: "eSuc n \<noteq> 0"
huffman@44019
   116
  by (simp add: eSuc_def zero_enat_def split: enat.splits)
haftmann@27110
   117
huffman@44019
   118
lemma zero_ne_eSuc [simp]: "0 \<noteq> eSuc n"
huffman@44019
   119
  by (rule eSuc_ne_0 [symmetric])
oheimb@11351
   120
huffman@44019
   121
lemma eSuc_inject [simp]: "eSuc m = eSuc n \<longleftrightarrow> m = n"
huffman@44019
   122
  by (simp add: eSuc_def split: enat.splits)
haftmann@27110
   123
haftmann@27110
   124
subsection {* Addition *}
haftmann@27110
   125
hoelzl@43919
   126
instantiation enat :: comm_monoid_add
haftmann@27110
   127
begin
haftmann@27110
   128
blanchet@38167
   129
definition [nitpick_simp]:
hoelzl@43924
   130
  "m + n = (case m of \<infinity> \<Rightarrow> \<infinity> | enat m \<Rightarrow> (case n of \<infinity> \<Rightarrow> \<infinity> | enat n \<Rightarrow> enat (m + n)))"
oheimb@11351
   131
hoelzl@43919
   132
lemma plus_enat_simps [simp, code]:
hoelzl@43921
   133
  fixes q :: enat
hoelzl@43924
   134
  shows "enat m + enat n = enat (m + n)"
hoelzl@43921
   135
    and "\<infinity> + q = \<infinity>"
hoelzl@43921
   136
    and "q + \<infinity> = \<infinity>"
hoelzl@43919
   137
  by (simp_all add: plus_enat_def split: enat.splits)
haftmann@27110
   138
haftmann@27110
   139
instance proof
hoelzl@43919
   140
  fix n m q :: enat
haftmann@27110
   141
  show "n + m + q = n + (m + q)"
noschinl@45934
   142
    by (cases n m q rule: enat3_cases) auto
haftmann@27110
   143
  show "n + m = m + n"
noschinl@45934
   144
    by (cases n m rule: enat2_cases) auto
haftmann@27110
   145
  show "0 + n = n"
hoelzl@43919
   146
    by (cases n) (simp_all add: zero_enat_def)
huffman@26089
   147
qed
huffman@26089
   148
haftmann@27110
   149
end
oheimb@11351
   150
huffman@44019
   151
lemma eSuc_plus_1:
huffman@44019
   152
  "eSuc n = n + 1"
huffman@44019
   153
  by (cases n) (simp_all add: eSuc_enat one_enat_def)
haftmann@27110
   154
  
huffman@44019
   155
lemma plus_1_eSuc:
huffman@44019
   156
  "1 + q = eSuc q"
huffman@44019
   157
  "q + 1 = eSuc q"
huffman@44019
   158
  by (simp_all add: eSuc_plus_1 add_ac)
nipkow@41853
   159
huffman@44019
   160
lemma iadd_Suc: "eSuc m + n = eSuc (m + n)"
huffman@44019
   161
  by (simp_all add: eSuc_plus_1 add_ac)
oheimb@11351
   162
huffman@44019
   163
lemma iadd_Suc_right: "m + eSuc n = eSuc (m + n)"
huffman@44019
   164
  by (simp only: add_commute[of m] iadd_Suc)
nipkow@41853
   165
hoelzl@43919
   166
lemma iadd_is_0: "(m + n = (0::enat)) = (m = 0 \<and> n = 0)"
huffman@44019
   167
  by (cases m, cases n, simp_all add: zero_enat_def)
oheimb@11351
   168
huffman@29014
   169
subsection {* Multiplication *}
huffman@29014
   170
hoelzl@43919
   171
instantiation enat :: comm_semiring_1
huffman@29014
   172
begin
huffman@29014
   173
hoelzl@43919
   174
definition times_enat_def [nitpick_simp]:
hoelzl@43924
   175
  "m * n = (case m of \<infinity> \<Rightarrow> if n = 0 then 0 else \<infinity> | enat m \<Rightarrow>
hoelzl@43924
   176
    (case n of \<infinity> \<Rightarrow> if m = 0 then 0 else \<infinity> | enat n \<Rightarrow> enat (m * n)))"
huffman@29014
   177
hoelzl@43919
   178
lemma times_enat_simps [simp, code]:
hoelzl@43924
   179
  "enat m * enat n = enat (m * n)"
hoelzl@43921
   180
  "\<infinity> * \<infinity> = (\<infinity>::enat)"
hoelzl@43924
   181
  "\<infinity> * enat n = (if n = 0 then 0 else \<infinity>)"
hoelzl@43924
   182
  "enat m * \<infinity> = (if m = 0 then 0 else \<infinity>)"
hoelzl@43919
   183
  unfolding times_enat_def zero_enat_def
hoelzl@43919
   184
  by (simp_all split: enat.split)
huffman@29014
   185
huffman@29014
   186
instance proof
hoelzl@43919
   187
  fix a b c :: enat
huffman@29014
   188
  show "(a * b) * c = a * (b * c)"
hoelzl@43919
   189
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   190
    by (simp split: enat.split)
huffman@29014
   191
  show "a * b = b * a"
hoelzl@43919
   192
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   193
    by (simp split: enat.split)
huffman@29014
   194
  show "1 * a = a"
hoelzl@43919
   195
    unfolding times_enat_def zero_enat_def one_enat_def
hoelzl@43919
   196
    by (simp split: enat.split)
huffman@29014
   197
  show "(a + b) * c = a * c + b * c"
hoelzl@43919
   198
    unfolding times_enat_def zero_enat_def
webertj@49962
   199
    by (simp split: enat.split add: distrib_right)
huffman@29014
   200
  show "0 * a = 0"
hoelzl@43919
   201
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   202
    by (simp split: enat.split)
huffman@29014
   203
  show "a * 0 = 0"
hoelzl@43919
   204
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   205
    by (simp split: enat.split)
hoelzl@43919
   206
  show "(0::enat) \<noteq> 1"
hoelzl@43919
   207
    unfolding zero_enat_def one_enat_def
huffman@29014
   208
    by simp
huffman@29014
   209
qed
huffman@29014
   210
huffman@29014
   211
end
huffman@29014
   212
huffman@44019
   213
lemma mult_eSuc: "eSuc m * n = n + m * n"
huffman@44019
   214
  unfolding eSuc_plus_1 by (simp add: algebra_simps)
huffman@29014
   215
huffman@44019
   216
lemma mult_eSuc_right: "m * eSuc n = m + m * n"
huffman@44019
   217
  unfolding eSuc_plus_1 by (simp add: algebra_simps)
huffman@29014
   218
hoelzl@43924
   219
lemma of_nat_eq_enat: "of_nat n = enat n"
huffman@29023
   220
  apply (induct n)
hoelzl@43924
   221
  apply (simp add: enat_0)
huffman@44019
   222
  apply (simp add: plus_1_eSuc eSuc_enat)
huffman@29023
   223
  done
huffman@29023
   224
hoelzl@43919
   225
instance enat :: semiring_char_0 proof
hoelzl@43924
   226
  have "inj enat" by (rule injI) simp
hoelzl@43924
   227
  then show "inj (\<lambda>n. of_nat n :: enat)" by (simp add: of_nat_eq_enat)
haftmann@38621
   228
qed
huffman@29023
   229
huffman@44019
   230
lemma imult_is_0 [simp]: "((m::enat) * n = 0) = (m = 0 \<or> n = 0)"
huffman@44019
   231
  by (auto simp add: times_enat_def zero_enat_def split: enat.split)
nipkow@41853
   232
huffman@44019
   233
lemma imult_is_infinity: "((a::enat) * b = \<infinity>) = (a = \<infinity> \<and> b \<noteq> 0 \<or> b = \<infinity> \<and> a \<noteq> 0)"
huffman@44019
   234
  by (auto simp add: times_enat_def zero_enat_def split: enat.split)
nipkow@41853
   235
nipkow@41853
   236
huffman@47108
   237
subsection {* Numerals *}
huffman@47108
   238
huffman@47108
   239
lemma numeral_eq_enat:
huffman@47108
   240
  "numeral k = enat (numeral k)"
huffman@47108
   241
  using of_nat_eq_enat [of "numeral k"] by simp
huffman@47108
   242
huffman@47108
   243
lemma enat_numeral [code_abbrev]:
huffman@47108
   244
  "enat (numeral k) = numeral k"
huffman@47108
   245
  using numeral_eq_enat ..
huffman@47108
   246
huffman@47108
   247
lemma infinity_ne_numeral [simp]: "(\<infinity>::enat) \<noteq> numeral k"
huffman@47108
   248
  by (simp add: numeral_eq_enat)
huffman@47108
   249
huffman@47108
   250
lemma numeral_ne_infinity [simp]: "numeral k \<noteq> (\<infinity>::enat)"
huffman@47108
   251
  by (simp add: numeral_eq_enat)
huffman@47108
   252
huffman@47108
   253
lemma eSuc_numeral [simp]: "eSuc (numeral k) = numeral (k + Num.One)"
huffman@47108
   254
  by (simp only: eSuc_plus_1 numeral_plus_one)
huffman@47108
   255
nipkow@41853
   256
subsection {* Subtraction *}
nipkow@41853
   257
hoelzl@43919
   258
instantiation enat :: minus
nipkow@41853
   259
begin
nipkow@41853
   260
hoelzl@43919
   261
definition diff_enat_def:
hoelzl@43924
   262
"a - b = (case a of (enat x) \<Rightarrow> (case b of (enat y) \<Rightarrow> enat (x - y) | \<infinity> \<Rightarrow> 0)
nipkow@41853
   263
          | \<infinity> \<Rightarrow> \<infinity>)"
nipkow@41853
   264
nipkow@41853
   265
instance ..
nipkow@41853
   266
nipkow@41853
   267
end
nipkow@41853
   268
huffman@47108
   269
lemma idiff_enat_enat [simp, code]: "enat a - enat b = enat (a - b)"
huffman@44019
   270
  by (simp add: diff_enat_def)
nipkow@41853
   271
huffman@47108
   272
lemma idiff_infinity [simp, code]: "\<infinity> - n = (\<infinity>::enat)"
huffman@44019
   273
  by (simp add: diff_enat_def)
nipkow@41853
   274
huffman@47108
   275
lemma idiff_infinity_right [simp, code]: "enat a - \<infinity> = 0"
huffman@44019
   276
  by (simp add: diff_enat_def)
nipkow@41853
   277
huffman@44019
   278
lemma idiff_0 [simp]: "(0::enat) - n = 0"
huffman@44019
   279
  by (cases n, simp_all add: zero_enat_def)
nipkow@41853
   280
huffman@44019
   281
lemmas idiff_enat_0 [simp] = idiff_0 [unfolded zero_enat_def]
nipkow@41853
   282
huffman@44019
   283
lemma idiff_0_right [simp]: "(n::enat) - 0 = n"
huffman@44019
   284
  by (cases n) (simp_all add: zero_enat_def)
nipkow@41853
   285
huffman@44019
   286
lemmas idiff_enat_0_right [simp] = idiff_0_right [unfolded zero_enat_def]
nipkow@41853
   287
huffman@44019
   288
lemma idiff_self [simp]: "n \<noteq> \<infinity> \<Longrightarrow> (n::enat) - n = 0"
huffman@44019
   289
  by (auto simp: zero_enat_def)
nipkow@41853
   290
huffman@44019
   291
lemma eSuc_minus_eSuc [simp]: "eSuc n - eSuc m = n - m"
huffman@44019
   292
  by (simp add: eSuc_def split: enat.split)
nipkow@41855
   293
huffman@44019
   294
lemma eSuc_minus_1 [simp]: "eSuc n - 1 = n"
huffman@44019
   295
  by (simp add: one_enat_def eSuc_enat[symmetric] zero_enat_def[symmetric])
nipkow@41855
   296
hoelzl@43924
   297
(*lemmas idiff_self_eq_0_enat = idiff_self_eq_0[unfolded zero_enat_def]*)
nipkow@41853
   298
haftmann@27110
   299
subsection {* Ordering *}
haftmann@27110
   300
hoelzl@43919
   301
instantiation enat :: linordered_ab_semigroup_add
haftmann@27110
   302
begin
oheimb@11351
   303
blanchet@38167
   304
definition [nitpick_simp]:
hoelzl@43924
   305
  "m \<le> n = (case n of enat n1 \<Rightarrow> (case m of enat m1 \<Rightarrow> m1 \<le> n1 | \<infinity> \<Rightarrow> False)
haftmann@27110
   306
    | \<infinity> \<Rightarrow> True)"
oheimb@11351
   307
blanchet@38167
   308
definition [nitpick_simp]:
hoelzl@43924
   309
  "m < n = (case m of enat m1 \<Rightarrow> (case n of enat n1 \<Rightarrow> m1 < n1 | \<infinity> \<Rightarrow> True)
haftmann@27110
   310
    | \<infinity> \<Rightarrow> False)"
oheimb@11351
   311
hoelzl@43919
   312
lemma enat_ord_simps [simp]:
hoelzl@43924
   313
  "enat m \<le> enat n \<longleftrightarrow> m \<le> n"
hoelzl@43924
   314
  "enat m < enat n \<longleftrightarrow> m < n"
hoelzl@43921
   315
  "q \<le> (\<infinity>::enat)"
hoelzl@43921
   316
  "q < (\<infinity>::enat) \<longleftrightarrow> q \<noteq> \<infinity>"
hoelzl@43921
   317
  "(\<infinity>::enat) \<le> q \<longleftrightarrow> q = \<infinity>"
hoelzl@43921
   318
  "(\<infinity>::enat) < q \<longleftrightarrow> False"
hoelzl@43919
   319
  by (simp_all add: less_eq_enat_def less_enat_def split: enat.splits)
oheimb@11351
   320
huffman@47108
   321
lemma numeral_le_enat_iff[simp]:
huffman@47108
   322
  shows "numeral m \<le> enat n \<longleftrightarrow> numeral m \<le> n"
huffman@47108
   323
by (auto simp: numeral_eq_enat)
noschinl@45934
   324
huffman@47108
   325
lemma numeral_less_enat_iff[simp]:
huffman@47108
   326
  shows "numeral m < enat n \<longleftrightarrow> numeral m < n"
huffman@47108
   327
by (auto simp: numeral_eq_enat)
noschinl@45934
   328
hoelzl@43919
   329
lemma enat_ord_code [code]:
hoelzl@43924
   330
  "enat m \<le> enat n \<longleftrightarrow> m \<le> n"
hoelzl@43924
   331
  "enat m < enat n \<longleftrightarrow> m < n"
hoelzl@43921
   332
  "q \<le> (\<infinity>::enat) \<longleftrightarrow> True"
hoelzl@43924
   333
  "enat m < \<infinity> \<longleftrightarrow> True"
hoelzl@43924
   334
  "\<infinity> \<le> enat n \<longleftrightarrow> False"
hoelzl@43921
   335
  "(\<infinity>::enat) < q \<longleftrightarrow> False"
haftmann@27110
   336
  by simp_all
oheimb@11351
   337
haftmann@27110
   338
instance by default
hoelzl@43919
   339
  (auto simp add: less_eq_enat_def less_enat_def plus_enat_def split: enat.splits)
oheimb@11351
   340
haftmann@27110
   341
end
haftmann@27110
   342
hoelzl@43919
   343
instance enat :: ordered_comm_semiring
huffman@29014
   344
proof
hoelzl@43919
   345
  fix a b c :: enat
huffman@29014
   346
  assume "a \<le> b" and "0 \<le> c"
huffman@29014
   347
  thus "c * a \<le> c * b"
hoelzl@43919
   348
    unfolding times_enat_def less_eq_enat_def zero_enat_def
hoelzl@43919
   349
    by (simp split: enat.splits)
huffman@29014
   350
qed
huffman@29014
   351
huffman@47108
   352
(* BH: These equations are already proven generally for any type in
huffman@47108
   353
class linordered_semidom. However, enat is not in that class because
huffman@47108
   354
it does not have the cancellation property. Would it be worthwhile to
huffman@47108
   355
a generalize linordered_semidom to a new class that includes enat? *)
huffman@47108
   356
hoelzl@43919
   357
lemma enat_ord_number [simp]:
huffman@47108
   358
  "(numeral m \<Colon> enat) \<le> numeral n \<longleftrightarrow> (numeral m \<Colon> nat) \<le> numeral n"
huffman@47108
   359
  "(numeral m \<Colon> enat) < numeral n \<longleftrightarrow> (numeral m \<Colon> nat) < numeral n"
huffman@47108
   360
  by (simp_all add: numeral_eq_enat)
oheimb@11351
   361
hoelzl@43919
   362
lemma i0_lb [simp]: "(0\<Colon>enat) \<le> n"
hoelzl@43919
   363
  by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
oheimb@11351
   364
hoelzl@43919
   365
lemma ile0_eq [simp]: "n \<le> (0\<Colon>enat) \<longleftrightarrow> n = 0"
hoelzl@43919
   366
  by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
oheimb@11351
   367
huffman@44019
   368
lemma infinity_ileE [elim!]: "\<infinity> \<le> enat m \<Longrightarrow> R"
huffman@44019
   369
  by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
huffman@44019
   370
huffman@44019
   371
lemma infinity_ilessE [elim!]: "\<infinity> < enat m \<Longrightarrow> R"
haftmann@27110
   372
  by simp
oheimb@11351
   373
hoelzl@43919
   374
lemma not_iless0 [simp]: "\<not> n < (0\<Colon>enat)"
hoelzl@43919
   375
  by (simp add: zero_enat_def less_enat_def split: enat.splits)
haftmann@27110
   376
hoelzl@43919
   377
lemma i0_less [simp]: "(0\<Colon>enat) < n \<longleftrightarrow> n \<noteq> 0"
huffman@44019
   378
  by (simp add: zero_enat_def less_enat_def split: enat.splits)
oheimb@11351
   379
huffman@44019
   380
lemma eSuc_ile_mono [simp]: "eSuc n \<le> eSuc m \<longleftrightarrow> n \<le> m"
huffman@44019
   381
  by (simp add: eSuc_def less_eq_enat_def split: enat.splits)
haftmann@27110
   382
 
huffman@44019
   383
lemma eSuc_mono [simp]: "eSuc n < eSuc m \<longleftrightarrow> n < m"
huffman@44019
   384
  by (simp add: eSuc_def less_enat_def split: enat.splits)
oheimb@11351
   385
huffman@44019
   386
lemma ile_eSuc [simp]: "n \<le> eSuc n"
huffman@44019
   387
  by (simp add: eSuc_def less_eq_enat_def split: enat.splits)
oheimb@11351
   388
huffman@44019
   389
lemma not_eSuc_ilei0 [simp]: "\<not> eSuc n \<le> 0"
huffman@44019
   390
  by (simp add: zero_enat_def eSuc_def less_eq_enat_def split: enat.splits)
haftmann@27110
   391
huffman@44019
   392
lemma i0_iless_eSuc [simp]: "0 < eSuc n"
huffman@44019
   393
  by (simp add: zero_enat_def eSuc_def less_enat_def split: enat.splits)
haftmann@27110
   394
huffman@44019
   395
lemma iless_eSuc0[simp]: "(n < eSuc 0) = (n = 0)"
huffman@44019
   396
  by (simp add: zero_enat_def eSuc_def less_enat_def split: enat.split)
nipkow@41853
   397
huffman@44019
   398
lemma ileI1: "m < n \<Longrightarrow> eSuc m \<le> n"
huffman@44019
   399
  by (simp add: eSuc_def less_eq_enat_def less_enat_def split: enat.splits)
haftmann@27110
   400
hoelzl@43924
   401
lemma Suc_ile_eq: "enat (Suc m) \<le> n \<longleftrightarrow> enat m < n"
haftmann@27110
   402
  by (cases n) auto
haftmann@27110
   403
huffman@44019
   404
lemma iless_Suc_eq [simp]: "enat m < eSuc n \<longleftrightarrow> enat m \<le> n"
huffman@44019
   405
  by (auto simp add: eSuc_def less_enat_def split: enat.splits)
oheimb@11351
   406
huffman@44019
   407
lemma imult_infinity: "(0::enat) < n \<Longrightarrow> \<infinity> * n = \<infinity>"
huffman@44019
   408
  by (simp add: zero_enat_def less_enat_def split: enat.splits)
nipkow@41853
   409
huffman@44019
   410
lemma imult_infinity_right: "(0::enat) < n \<Longrightarrow> n * \<infinity> = \<infinity>"
huffman@44019
   411
  by (simp add: zero_enat_def less_enat_def split: enat.splits)
nipkow@41853
   412
hoelzl@43919
   413
lemma enat_0_less_mult_iff: "(0 < (m::enat) * n) = (0 < m \<and> 0 < n)"
huffman@44019
   414
  by (simp only: i0_less imult_is_0, simp)
nipkow@41853
   415
huffman@44019
   416
lemma mono_eSuc: "mono eSuc"
huffman@44019
   417
  by (simp add: mono_def)
nipkow@41853
   418
nipkow@41853
   419
hoelzl@43919
   420
lemma min_enat_simps [simp]:
hoelzl@43924
   421
  "min (enat m) (enat n) = enat (min m n)"
haftmann@27110
   422
  "min q 0 = 0"
haftmann@27110
   423
  "min 0 q = 0"
hoelzl@43921
   424
  "min q (\<infinity>::enat) = q"
hoelzl@43921
   425
  "min (\<infinity>::enat) q = q"
haftmann@27110
   426
  by (auto simp add: min_def)
oheimb@11351
   427
hoelzl@43919
   428
lemma max_enat_simps [simp]:
hoelzl@43924
   429
  "max (enat m) (enat n) = enat (max m n)"
haftmann@27110
   430
  "max q 0 = q"
haftmann@27110
   431
  "max 0 q = q"
hoelzl@43921
   432
  "max q \<infinity> = (\<infinity>::enat)"
hoelzl@43921
   433
  "max \<infinity> q = (\<infinity>::enat)"
haftmann@27110
   434
  by (simp_all add: max_def)
haftmann@27110
   435
hoelzl@43924
   436
lemma enat_ile: "n \<le> enat m \<Longrightarrow> \<exists>k. n = enat k"
haftmann@27110
   437
  by (cases n) simp_all
haftmann@27110
   438
hoelzl@43924
   439
lemma enat_iless: "n < enat m \<Longrightarrow> \<exists>k. n = enat k"
haftmann@27110
   440
  by (cases n) simp_all
oheimb@11351
   441
hoelzl@43924
   442
lemma chain_incr: "\<forall>i. \<exists>j. Y i < Y j ==> \<exists>j. enat k < Y j"
nipkow@25134
   443
apply (induct_tac k)
hoelzl@43924
   444
 apply (simp (no_asm) only: enat_0)
haftmann@27110
   445
 apply (fast intro: le_less_trans [OF i0_lb])
nipkow@25134
   446
apply (erule exE)
nipkow@25134
   447
apply (drule spec)
nipkow@25134
   448
apply (erule exE)
nipkow@25134
   449
apply (drule ileI1)
huffman@44019
   450
apply (rule eSuc_enat [THEN subst])
nipkow@25134
   451
apply (rule exI)
haftmann@27110
   452
apply (erule (1) le_less_trans)
nipkow@25134
   453
done
oheimb@11351
   454
hoelzl@43919
   455
instantiation enat :: "{bot, top}"
haftmann@29337
   456
begin
haftmann@29337
   457
hoelzl@43919
   458
definition bot_enat :: enat where
hoelzl@43919
   459
  "bot_enat = 0"
haftmann@29337
   460
hoelzl@43919
   461
definition top_enat :: enat where
hoelzl@43919
   462
  "top_enat = \<infinity>"
haftmann@29337
   463
haftmann@29337
   464
instance proof
hoelzl@43919
   465
qed (simp_all add: bot_enat_def top_enat_def)
haftmann@29337
   466
haftmann@29337
   467
end
haftmann@29337
   468
hoelzl@43924
   469
lemma finite_enat_bounded:
hoelzl@43924
   470
  assumes le_fin: "\<And>y. y \<in> A \<Longrightarrow> y \<le> enat n"
noschinl@42993
   471
  shows "finite A"
noschinl@42993
   472
proof (rule finite_subset)
hoelzl@43924
   473
  show "finite (enat ` {..n})" by blast
noschinl@42993
   474
nipkow@44890
   475
  have "A \<subseteq> {..enat n}" using le_fin by fastforce
hoelzl@43924
   476
  also have "\<dots> \<subseteq> enat ` {..n}"
noschinl@42993
   477
    by (rule subsetI) (case_tac x, auto)
hoelzl@43924
   478
  finally show "A \<subseteq> enat ` {..n}" .
noschinl@42993
   479
qed
noschinl@42993
   480
huffman@26089
   481
huffman@45775
   482
subsection {* Cancellation simprocs *}
huffman@45775
   483
huffman@45775
   484
lemma enat_add_left_cancel: "a + b = a + c \<longleftrightarrow> a = (\<infinity>::enat) \<or> b = c"
huffman@45775
   485
  unfolding plus_enat_def by (simp split: enat.split)
huffman@45775
   486
huffman@45775
   487
lemma enat_add_left_cancel_le: "a + b \<le> a + c \<longleftrightarrow> a = (\<infinity>::enat) \<or> b \<le> c"
huffman@45775
   488
  unfolding plus_enat_def by (simp split: enat.split)
huffman@45775
   489
huffman@45775
   490
lemma enat_add_left_cancel_less: "a + b < a + c \<longleftrightarrow> a \<noteq> (\<infinity>::enat) \<and> b < c"
huffman@45775
   491
  unfolding plus_enat_def by (simp split: enat.split)
huffman@45775
   492
huffman@45775
   493
ML {*
huffman@45775
   494
structure Cancel_Enat_Common =
huffman@45775
   495
struct
huffman@45775
   496
  (* copied from src/HOL/Tools/nat_numeral_simprocs.ML *)
huffman@45775
   497
  fun find_first_t _    _ []         = raise TERM("find_first_t", [])
huffman@45775
   498
    | find_first_t past u (t::terms) =
huffman@45775
   499
          if u aconv t then (rev past @ terms)
huffman@45775
   500
          else find_first_t (t::past) u terms
huffman@45775
   501
huffman@51366
   502
  fun dest_summing (Const (@{const_name Groups.plus}, _) $ t $ u, ts) =
huffman@51366
   503
        dest_summing (t, dest_summing (u, ts))
huffman@51366
   504
    | dest_summing (t, ts) = t :: ts
huffman@51366
   505
huffman@45775
   506
  val mk_sum = Arith_Data.long_mk_sum
huffman@51366
   507
  fun dest_sum t = dest_summing (t, [])
huffman@45775
   508
  val find_first = find_first_t []
huffman@45775
   509
  val trans_tac = Numeral_Simprocs.trans_tac
huffman@45775
   510
  val norm_ss = HOL_basic_ss addsimps
huffman@47108
   511
    @{thms add_ac add_0_left add_0_right}
huffman@45775
   512
  fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss))
huffman@45775
   513
  fun simplify_meta_eq ss cancel_th th =
huffman@47108
   514
    Arith_Data.simplify_meta_eq [] ss
huffman@45775
   515
      ([th, cancel_th] MRS trans)
huffman@45775
   516
  fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b))
huffman@45775
   517
end
huffman@45775
   518
huffman@45775
   519
structure Eq_Enat_Cancel = ExtractCommonTermFun
huffman@45775
   520
(open Cancel_Enat_Common
huffman@45775
   521
  val mk_bal = HOLogic.mk_eq
huffman@45775
   522
  val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} @{typ enat}
huffman@45775
   523
  fun simp_conv _ _ = SOME @{thm enat_add_left_cancel}
huffman@45775
   524
)
huffman@45775
   525
huffman@45775
   526
structure Le_Enat_Cancel = ExtractCommonTermFun
huffman@45775
   527
(open Cancel_Enat_Common
huffman@45775
   528
  val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less_eq}
huffman@45775
   529
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} @{typ enat}
huffman@45775
   530
  fun simp_conv _ _ = SOME @{thm enat_add_left_cancel_le}
huffman@45775
   531
)
huffman@45775
   532
huffman@45775
   533
structure Less_Enat_Cancel = ExtractCommonTermFun
huffman@45775
   534
(open Cancel_Enat_Common
huffman@45775
   535
  val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less}
huffman@45775
   536
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} @{typ enat}
huffman@45775
   537
  fun simp_conv _ _ = SOME @{thm enat_add_left_cancel_less}
huffman@45775
   538
)
huffman@45775
   539
*}
huffman@45775
   540
huffman@45775
   541
simproc_setup enat_eq_cancel
huffman@45775
   542
  ("(l::enat) + m = n" | "(l::enat) = m + n") =
huffman@45775
   543
  {* fn phi => fn ss => fn ct => Eq_Enat_Cancel.proc ss (term_of ct) *}
huffman@45775
   544
huffman@45775
   545
simproc_setup enat_le_cancel
huffman@45775
   546
  ("(l::enat) + m \<le> n" | "(l::enat) \<le> m + n") =
huffman@45775
   547
  {* fn phi => fn ss => fn ct => Le_Enat_Cancel.proc ss (term_of ct) *}
huffman@45775
   548
huffman@45775
   549
simproc_setup enat_less_cancel
huffman@45775
   550
  ("(l::enat) + m < n" | "(l::enat) < m + n") =
huffman@45775
   551
  {* fn phi => fn ss => fn ct => Less_Enat_Cancel.proc ss (term_of ct) *}
huffman@45775
   552
huffman@45775
   553
text {* TODO: add regression tests for these simprocs *}
huffman@45775
   554
huffman@45775
   555
text {* TODO: add simprocs for combining and cancelling numerals *}
huffman@45775
   556
huffman@45775
   557
haftmann@27110
   558
subsection {* Well-ordering *}
huffman@26089
   559
hoelzl@43924
   560
lemma less_enatE:
hoelzl@43924
   561
  "[| n < enat m; !!k. n = enat k ==> k < m ==> P |] ==> P"
huffman@26089
   562
by (induct n) auto
huffman@26089
   563
huffman@44019
   564
lemma less_infinityE:
hoelzl@43924
   565
  "[| n < \<infinity>; !!k. n = enat k ==> P |] ==> P"
huffman@26089
   566
by (induct n) auto
huffman@26089
   567
hoelzl@43919
   568
lemma enat_less_induct:
hoelzl@43919
   569
  assumes prem: "!!n. \<forall>m::enat. m < n --> P m ==> P n" shows "P n"
huffman@26089
   570
proof -
hoelzl@43924
   571
  have P_enat: "!!k. P (enat k)"
huffman@26089
   572
    apply (rule nat_less_induct)
huffman@26089
   573
    apply (rule prem, clarify)
hoelzl@43924
   574
    apply (erule less_enatE, simp)
huffman@26089
   575
    done
huffman@26089
   576
  show ?thesis
huffman@26089
   577
  proof (induct n)
huffman@26089
   578
    fix nat
hoelzl@43924
   579
    show "P (enat nat)" by (rule P_enat)
huffman@26089
   580
  next
hoelzl@43921
   581
    show "P \<infinity>"
huffman@26089
   582
      apply (rule prem, clarify)
huffman@44019
   583
      apply (erule less_infinityE)
hoelzl@43924
   584
      apply (simp add: P_enat)
huffman@26089
   585
      done
huffman@26089
   586
  qed
huffman@26089
   587
qed
huffman@26089
   588
hoelzl@43919
   589
instance enat :: wellorder
huffman@26089
   590
proof
haftmann@27823
   591
  fix P and n
hoelzl@43919
   592
  assume hyp: "(\<And>n\<Colon>enat. (\<And>m\<Colon>enat. m < n \<Longrightarrow> P m) \<Longrightarrow> P n)"
hoelzl@43919
   593
  show "P n" by (blast intro: enat_less_induct hyp)
huffman@26089
   594
qed
huffman@26089
   595
noschinl@42993
   596
subsection {* Complete Lattice *}
noschinl@42993
   597
hoelzl@43919
   598
instantiation enat :: complete_lattice
noschinl@42993
   599
begin
noschinl@42993
   600
hoelzl@43919
   601
definition inf_enat :: "enat \<Rightarrow> enat \<Rightarrow> enat" where
hoelzl@43919
   602
  "inf_enat \<equiv> min"
noschinl@42993
   603
hoelzl@43919
   604
definition sup_enat :: "enat \<Rightarrow> enat \<Rightarrow> enat" where
hoelzl@43919
   605
  "sup_enat \<equiv> max"
noschinl@42993
   606
hoelzl@43919
   607
definition Inf_enat :: "enat set \<Rightarrow> enat" where
hoelzl@43919
   608
  "Inf_enat A \<equiv> if A = {} then \<infinity> else (LEAST x. x \<in> A)"
noschinl@42993
   609
hoelzl@43919
   610
definition Sup_enat :: "enat set \<Rightarrow> enat" where
hoelzl@43919
   611
  "Sup_enat A \<equiv> if A = {} then 0
noschinl@42993
   612
    else if finite A then Max A
noschinl@42993
   613
                     else \<infinity>"
noschinl@42993
   614
instance proof
hoelzl@43919
   615
  fix x :: "enat" and A :: "enat set"
noschinl@42993
   616
  { assume "x \<in> A" then show "Inf A \<le> x"
hoelzl@43919
   617
      unfolding Inf_enat_def by (auto intro: Least_le) }
noschinl@42993
   618
  { assume "\<And>y. y \<in> A \<Longrightarrow> x \<le> y" then show "x \<le> Inf A"
hoelzl@43919
   619
      unfolding Inf_enat_def
noschinl@42993
   620
      by (cases "A = {}") (auto intro: LeastI2_ex) }
noschinl@42993
   621
  { assume "x \<in> A" then show "x \<le> Sup A"
hoelzl@43919
   622
      unfolding Sup_enat_def by (cases "finite A") auto }
noschinl@42993
   623
  { assume "\<And>y. y \<in> A \<Longrightarrow> y \<le> x" then show "Sup A \<le> x"
hoelzl@43924
   624
      unfolding Sup_enat_def using finite_enat_bounded by auto }
hoelzl@43919
   625
qed (simp_all add: inf_enat_def sup_enat_def)
noschinl@42993
   626
end
noschinl@42993
   627
hoelzl@43978
   628
instance enat :: complete_linorder ..
haftmann@27110
   629
haftmann@27110
   630
subsection {* Traditional theorem names *}
haftmann@27110
   631
huffman@47108
   632
lemmas enat_defs = zero_enat_def one_enat_def eSuc_def
hoelzl@43919
   633
  plus_enat_def less_eq_enat_def less_enat_def
haftmann@27110
   634
oheimb@11351
   635
end