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