src/HOL/Library/Extended.thy
author haftmann
Fri Nov 01 18:51:14 2013 +0100 (2013-11-01)
changeset 54230 b1d955791529
parent 53427 415354b68f0c
child 54489 03ff4d1e6784
permissions -rw-r--r--
more simplification rules on unary and binary minus
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
nipkow@51338
    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
nipkow@51338
    28
"((x::'a extended) < y) = (x \<le> y & \<not> x \<ge> y)"
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
nipkow@51357
    80
instantiation extended :: (one)one
nipkow@51357
    81
begin
nipkow@51357
    82
definition "1 = Fin(1::'a)"
nipkow@51357
    83
instance ..
nipkow@51357
    84
end
nipkow@51357
    85
nipkow@51338
    86
instantiation extended :: (plus)plus
nipkow@51338
    87
begin
nipkow@51338
    88
nipkow@51338
    89
text {* The following definition of of addition is totalized
nipkow@51338
    90
to make it asociative and commutative. Normally the sum of plus and minus infinity is undefined. *}
nipkow@51338
    91
nipkow@51338
    92
fun plus_extended where
nipkow@51338
    93
"Fin x + Fin y = Fin(x+y)" |
nipkow@51338
    94
"Fin x + Pinf  = Pinf" |
nipkow@51338
    95
"Pinf  + Fin x = Pinf" |
nipkow@51338
    96
"Pinf  + Pinf  = Pinf" |
nipkow@51338
    97
"Minf  + Fin y = Minf" |
nipkow@51338
    98
"Fin x + Minf  = Minf" |
nipkow@51338
    99
"Minf  + Minf  = Minf" |
nipkow@51338
   100
"Minf  + Pinf  = Pinf" |
nipkow@51338
   101
"Pinf  + Minf  = Pinf"
nipkow@51338
   102
noschinl@53427
   103
case_of_simps plus_case: plus_extended.simps
noschinl@53427
   104
nipkow@51338
   105
instance ..
nipkow@51338
   106
nipkow@51338
   107
end
nipkow@51338
   108
nipkow@51338
   109
noschinl@53427
   110
nipkow@51338
   111
instance extended :: (ab_semigroup_add)ab_semigroup_add
noschinl@53427
   112
  by intro_classes (simp_all add: ac_simps plus_case split: extended.splits)
nipkow@51338
   113
nipkow@51338
   114
instance extended :: (ordered_ab_semigroup_add)ordered_ab_semigroup_add
noschinl@53427
   115
  by intro_classes (auto simp: add_left_mono plus_case split: extended.splits)
nipkow@51338
   116
nipkow@51357
   117
instance extended :: (comm_monoid_add)comm_monoid_add
nipkow@51338
   118
proof
nipkow@51338
   119
  fix x :: "'a extended" show "0 + x = x" unfolding zero_extended_def by(cases x)auto
nipkow@51338
   120
qed
nipkow@51338
   121
nipkow@51338
   122
instantiation extended :: (uminus)uminus
nipkow@51338
   123
begin
nipkow@51338
   124
nipkow@51338
   125
fun uminus_extended where
nipkow@51338
   126
"- (Fin x) = Fin (- x)" |
nipkow@51338
   127
"- Pinf    = Minf" |
nipkow@51338
   128
"- Minf    = Pinf"
nipkow@51338
   129
nipkow@51338
   130
instance ..
nipkow@51338
   131
nipkow@51338
   132
end
nipkow@51338
   133
nipkow@51338
   134
nipkow@51338
   135
instantiation extended :: (ab_group_add)minus
nipkow@51338
   136
begin
nipkow@51338
   137
definition "x - y = x + -(y::'a extended)"
nipkow@51338
   138
instance ..
nipkow@51338
   139
end
nipkow@51338
   140
nipkow@51338
   141
lemma minus_extended_simps[simp]:
nipkow@51338
   142
  "Fin x - Fin y = Fin(x - y)"
nipkow@51338
   143
  "Fin x - Pinf  = Minf"
nipkow@51338
   144
  "Fin x - Minf  = Pinf"
nipkow@51338
   145
  "Pinf  - Fin y = Pinf"
nipkow@51338
   146
  "Pinf  - Minf  = Pinf"
nipkow@51338
   147
  "Minf  - Fin y = Minf"
nipkow@51338
   148
  "Minf  - Pinf  = Minf"
nipkow@51338
   149
  "Minf  - Minf  = Pinf"
nipkow@51338
   150
  "Pinf  - Pinf  = Pinf"
nipkow@51338
   151
by (simp_all add: minus_extended_def)
nipkow@51338
   152
nipkow@51357
   153
nipkow@51357
   154
text{* Numerals: *}
nipkow@51357
   155
nipkow@51357
   156
instance extended :: ("{ab_semigroup_add,one}")numeral ..
nipkow@51357
   157
nipkow@51357
   158
lemma Fin_numeral: "Fin(numeral w) = numeral w"
nipkow@51357
   159
  apply (induct w rule: num_induct)
nipkow@51357
   160
  apply (simp only: numeral_One one_extended_def)
nipkow@51357
   161
  apply (simp only: numeral_inc one_extended_def plus_extended.simps(1)[symmetric])
nipkow@51357
   162
  done
nipkow@51357
   163
nipkow@51358
   164
lemma Fin_neg_numeral: "Fin(neg_numeral w) = - numeral w"
nipkow@51358
   165
by (simp only: Fin_numeral minus_numeral[symmetric] uminus_extended.simps[symmetric])
nipkow@51358
   166
nipkow@51357
   167
nipkow@51338
   168
instantiation extended :: (lattice)bounded_lattice
nipkow@51338
   169
begin
nipkow@51338
   170
nipkow@51338
   171
definition "bot = Minf"
nipkow@51338
   172
definition "top = Pinf"
nipkow@51338
   173
nipkow@51338
   174
fun inf_extended :: "'a extended \<Rightarrow> 'a extended \<Rightarrow> 'a extended" where
nipkow@51338
   175
"inf_extended (Fin i) (Fin j) = Fin (inf i j)" |
nipkow@51338
   176
"inf_extended a Minf = Minf" |
nipkow@51338
   177
"inf_extended Minf a = Minf" |
nipkow@51338
   178
"inf_extended Pinf a = a" |
nipkow@51338
   179
"inf_extended a Pinf = a"
nipkow@51338
   180
nipkow@51338
   181
fun sup_extended :: "'a extended \<Rightarrow> 'a extended \<Rightarrow> 'a extended" where
nipkow@51338
   182
"sup_extended (Fin i) (Fin j) = Fin (sup i j)" |
nipkow@51338
   183
"sup_extended a Pinf = Pinf" |
nipkow@51338
   184
"sup_extended Pinf a = Pinf" |
nipkow@51338
   185
"sup_extended Minf a = a" |
nipkow@51338
   186
"sup_extended a Minf = a"
nipkow@51338
   187
noschinl@53427
   188
case_of_simps inf_extended_case: inf_extended.simps
noschinl@53427
   189
case_of_simps sup_extended_case: sup_extended.simps
noschinl@53427
   190
nipkow@51338
   191
instance
noschinl@53427
   192
  by (intro_classes) (auto simp: inf_extended_case sup_extended_case less_eq_extended_case
noschinl@53427
   193
    bot_extended_def top_extended_def split: extended.splits)
nipkow@51338
   194
end
nipkow@51338
   195
nipkow@51338
   196
end
nipkow@51338
   197