src/HOL/Library/Extended.thy
author wenzelm
Wed Jun 17 11:03:05 2015 +0200 (2015-06-17)
changeset 60500 903bb1495239
parent 60343 063698416239
child 65366 10ca63a18e56
permissions -rw-r--r--
isabelle update_cartouches;
nipkow@51338
     1
(*  Author:     Tobias Nipkow, TU M√ľnchen
nipkow@51338
     2
nipkow@51338
     3
A theory of types extended with a greatest and a least element.
nipkow@51338
     4
Oriented towards numeric types, hence "\<infinity>" and "-\<infinity>".
nipkow@51338
     5
*)
nipkow@51338
     6
nipkow@51338
     7
theory Extended
noschinl@53427
     8
imports
noschinl@53427
     9
  Main
noschinl@53427
    10
  "~~/src/HOL/Library/Simps_Case_Conv"
nipkow@51338
    11
begin
nipkow@51338
    12
blanchet@58310
    13
datatype 'a extended = Fin 'a | Pinf ("\<infinity>") | Minf ("-\<infinity>")
nipkow@51338
    14
nipkow@51357
    15
nipkow@51338
    16
instantiation extended :: (order)order
nipkow@51338
    17
begin
nipkow@51338
    18
nipkow@51338
    19
fun less_eq_extended :: "'a extended \<Rightarrow> 'a extended \<Rightarrow> bool" where
nipkow@51338
    20
"Fin x \<le> Fin y = (x \<le> y)" |
nipkow@51338
    21
"_     \<le> Pinf  = True" |
nipkow@51338
    22
"Minf  \<le> _     = True" |
nipkow@51338
    23
"(_::'a extended) \<le> _     = False"
nipkow@51338
    24
noschinl@53427
    25
case_of_simps less_eq_extended_case: less_eq_extended.simps
nipkow@51338
    26
nipkow@51338
    27
definition less_extended :: "'a extended \<Rightarrow> 'a extended \<Rightarrow> bool" where
haftmann@60343
    28
"((x::'a extended) < y) = (x \<le> y & \<not> y \<le> x)"
nipkow@51338
    29
nipkow@51338
    30
instance
noschinl@53427
    31
  by intro_classes (auto simp: less_extended_def less_eq_extended_case split: extended.splits)
nipkow@51338
    32
nipkow@51338
    33
end
nipkow@51338
    34
nipkow@51338
    35
instance extended :: (linorder)linorder
noschinl@53427
    36
  by intro_classes (auto simp: less_eq_extended_case split:extended.splits)
nipkow@51338
    37
nipkow@51338
    38
lemma Minf_le[simp]: "Minf \<le> y"
nipkow@51338
    39
by(cases y) auto
nipkow@51338
    40
lemma le_Pinf[simp]: "x \<le> Pinf"
nipkow@51338
    41
by(cases x) auto
nipkow@51338
    42
lemma le_Minf[simp]: "x \<le> Minf \<longleftrightarrow> x = Minf"
nipkow@51338
    43
by(cases x) auto
nipkow@51338
    44
lemma Pinf_le[simp]: "Pinf \<le> x \<longleftrightarrow> x = Pinf"
nipkow@51338
    45
by(cases x) auto
nipkow@51338
    46
nipkow@51338
    47
lemma less_extended_simps[simp]:
nipkow@51338
    48
  "Fin x < Fin y = (x < y)"
nipkow@51338
    49
  "Fin x < Pinf  = True"
nipkow@51338
    50
  "Fin x < Minf  = False"
nipkow@51338
    51
  "Pinf < h      = False"
nipkow@51338
    52
  "Minf < Fin x  = True"
nipkow@51338
    53
  "Minf < Pinf   = True"
nipkow@51338
    54
  "l    < Minf   = False"
nipkow@51338
    55
by (auto simp add: less_extended_def)
nipkow@51338
    56
nipkow@51338
    57
lemma min_extended_simps[simp]:
nipkow@51338
    58
  "min (Fin x) (Fin y) = Fin(min x y)"
nipkow@51338
    59
  "min xx      Pinf    = xx"
nipkow@51338
    60
  "min xx      Minf    = Minf"
nipkow@51338
    61
  "min Pinf    yy      = yy"
nipkow@51338
    62
  "min Minf    yy      = Minf"
nipkow@51338
    63
by (auto simp add: min_def)
nipkow@51338
    64
nipkow@51338
    65
lemma max_extended_simps[simp]:
nipkow@51338
    66
  "max (Fin x) (Fin y) = Fin(max x y)"
nipkow@51338
    67
  "max xx      Pinf    = Pinf"
nipkow@51338
    68
  "max xx      Minf    = xx"
nipkow@51338
    69
  "max Pinf    yy      = Pinf"
nipkow@51338
    70
  "max Minf    yy      = yy"
nipkow@51338
    71
by (auto simp add: max_def)
nipkow@51338
    72
nipkow@51338
    73
nipkow@51357
    74
instantiation extended :: (zero)zero
nipkow@51357
    75
begin
nipkow@51357
    76
definition "0 = Fin(0::'a)"
nipkow@51357
    77
instance ..
nipkow@51357
    78
end
nipkow@51357
    79
hoelzl@55124
    80
declare zero_extended_def[symmetric, code_post]
hoelzl@55124
    81
nipkow@51357
    82
instantiation extended :: (one)one
nipkow@51357
    83
begin
nipkow@51357
    84
definition "1 = Fin(1::'a)"
nipkow@51357
    85
instance ..
nipkow@51357
    86
end
nipkow@51357
    87
hoelzl@55124
    88
declare one_extended_def[symmetric, code_post]
hoelzl@55124
    89
nipkow@51338
    90
instantiation extended :: (plus)plus
nipkow@51338
    91
begin
nipkow@51338
    92
wenzelm@60500
    93
text \<open>The following definition of of addition is totalized
wenzelm@60500
    94
to make it asociative and commutative. Normally the sum of plus and minus infinity is undefined.\<close>
nipkow@51338
    95
nipkow@51338
    96
fun plus_extended where
nipkow@51338
    97
"Fin x + Fin y = Fin(x+y)" |
nipkow@51338
    98
"Fin x + Pinf  = Pinf" |
nipkow@51338
    99
"Pinf  + Fin x = Pinf" |
nipkow@51338
   100
"Pinf  + Pinf  = Pinf" |
nipkow@51338
   101
"Minf  + Fin y = Minf" |
nipkow@51338
   102
"Fin x + Minf  = Minf" |
nipkow@51338
   103
"Minf  + Minf  = Minf" |
nipkow@51338
   104
"Minf  + Pinf  = Pinf" |
nipkow@51338
   105
"Pinf  + Minf  = Pinf"
nipkow@51338
   106
noschinl@53427
   107
case_of_simps plus_case: plus_extended.simps
noschinl@53427
   108
nipkow@51338
   109
instance ..
nipkow@51338
   110
nipkow@51338
   111
end
nipkow@51338
   112
nipkow@51338
   113
noschinl@53427
   114
nipkow@51338
   115
instance extended :: (ab_semigroup_add)ab_semigroup_add
noschinl@53427
   116
  by intro_classes (simp_all add: ac_simps plus_case split: extended.splits)
nipkow@51338
   117
nipkow@51338
   118
instance extended :: (ordered_ab_semigroup_add)ordered_ab_semigroup_add
noschinl@53427
   119
  by intro_classes (auto simp: add_left_mono plus_case split: extended.splits)
nipkow@51338
   120
nipkow@51357
   121
instance extended :: (comm_monoid_add)comm_monoid_add
nipkow@51338
   122
proof
nipkow@51338
   123
  fix x :: "'a extended" show "0 + x = x" unfolding zero_extended_def by(cases x)auto
nipkow@51338
   124
qed
nipkow@51338
   125
nipkow@51338
   126
instantiation extended :: (uminus)uminus
nipkow@51338
   127
begin
nipkow@51338
   128
nipkow@51338
   129
fun uminus_extended where
nipkow@51338
   130
"- (Fin x) = Fin (- x)" |
nipkow@51338
   131
"- Pinf    = Minf" |
nipkow@51338
   132
"- Minf    = Pinf"
nipkow@51338
   133
nipkow@51338
   134
instance ..
nipkow@51338
   135
nipkow@51338
   136
end
nipkow@51338
   137
nipkow@51338
   138
nipkow@51338
   139
instantiation extended :: (ab_group_add)minus
nipkow@51338
   140
begin
nipkow@51338
   141
definition "x - y = x + -(y::'a extended)"
nipkow@51338
   142
instance ..
nipkow@51338
   143
end
nipkow@51338
   144
nipkow@51338
   145
lemma minus_extended_simps[simp]:
nipkow@51338
   146
  "Fin x - Fin y = Fin(x - y)"
nipkow@51338
   147
  "Fin x - Pinf  = Minf"
nipkow@51338
   148
  "Fin x - Minf  = Pinf"
nipkow@51338
   149
  "Pinf  - Fin y = Pinf"
nipkow@51338
   150
  "Pinf  - Minf  = Pinf"
nipkow@51338
   151
  "Minf  - Fin y = Minf"
nipkow@51338
   152
  "Minf  - Pinf  = Minf"
nipkow@51338
   153
  "Minf  - Minf  = Pinf"
nipkow@51338
   154
  "Pinf  - Pinf  = Pinf"
nipkow@51338
   155
by (simp_all add: minus_extended_def)
nipkow@51338
   156
nipkow@51357
   157
wenzelm@60500
   158
text\<open>Numerals:\<close>
nipkow@51357
   159
nipkow@51357
   160
instance extended :: ("{ab_semigroup_add,one}")numeral ..
nipkow@51357
   161
hoelzl@55124
   162
lemma Fin_numeral[code_post]: "Fin(numeral w) = numeral w"
nipkow@51357
   163
  apply (induct w rule: num_induct)
nipkow@51357
   164
  apply (simp only: numeral_One one_extended_def)
nipkow@51357
   165
  apply (simp only: numeral_inc one_extended_def plus_extended.simps(1)[symmetric])
nipkow@51357
   166
  done
nipkow@51357
   167
hoelzl@55124
   168
lemma Fin_neg_numeral[code_post]: "Fin (- numeral w) = - numeral w"
haftmann@54489
   169
by (simp only: Fin_numeral uminus_extended.simps[symmetric])
nipkow@51358
   170
nipkow@51357
   171
nipkow@51338
   172
instantiation extended :: (lattice)bounded_lattice
nipkow@51338
   173
begin
nipkow@51338
   174
nipkow@51338
   175
definition "bot = Minf"
nipkow@51338
   176
definition "top = Pinf"
nipkow@51338
   177
nipkow@51338
   178
fun inf_extended :: "'a extended \<Rightarrow> 'a extended \<Rightarrow> 'a extended" where
nipkow@51338
   179
"inf_extended (Fin i) (Fin j) = Fin (inf i j)" |
nipkow@51338
   180
"inf_extended a Minf = Minf" |
nipkow@51338
   181
"inf_extended Minf a = Minf" |
nipkow@51338
   182
"inf_extended Pinf a = a" |
nipkow@51338
   183
"inf_extended a Pinf = a"
nipkow@51338
   184
nipkow@51338
   185
fun sup_extended :: "'a extended \<Rightarrow> 'a extended \<Rightarrow> 'a extended" where
nipkow@51338
   186
"sup_extended (Fin i) (Fin j) = Fin (sup i j)" |
nipkow@51338
   187
"sup_extended a Pinf = Pinf" |
nipkow@51338
   188
"sup_extended Pinf a = Pinf" |
nipkow@51338
   189
"sup_extended Minf a = a" |
nipkow@51338
   190
"sup_extended a Minf = a"
nipkow@51338
   191
noschinl@53427
   192
case_of_simps inf_extended_case: inf_extended.simps
noschinl@53427
   193
case_of_simps sup_extended_case: sup_extended.simps
noschinl@53427
   194
nipkow@51338
   195
instance
noschinl@53427
   196
  by (intro_classes) (auto simp: inf_extended_case sup_extended_case less_eq_extended_case
noschinl@53427
   197
    bot_extended_def top_extended_def split: extended.splits)
nipkow@51338
   198
end
nipkow@51338
   199
nipkow@51338
   200
end
nipkow@51338
   201