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