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