src/HOL/Sum_Type.thy
author haftmann
Wed Nov 25 11:16:58 2009 +0100 (2009-11-25)
changeset 33961 03f2ab6a4ea6
parent 31080 21ffc770ebc0
child 33962 abf9fa17452a
permissions -rw-r--r--
centralized sum type matter in Sum_Type.thy
nipkow@10213
     1
(*  Title:      HOL/Sum_Type.thy
nipkow@10213
     2
    ID:         $Id$
nipkow@10213
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
nipkow@10213
     4
    Copyright   1992  University of Cambridge
nipkow@10213
     5
*)
nipkow@10213
     6
paulson@15391
     7
header{*The Disjoint Sum of Two Types*}
nipkow@10213
     8
paulson@15391
     9
theory Sum_Type
haftmann@33961
    10
imports Typedef Inductive Fun
paulson@15391
    11
begin
paulson@15391
    12
paulson@15391
    13
text{*The representations of the two injections*}
nipkow@10213
    14
nipkow@10213
    15
constdefs
paulson@15391
    16
  Inl_Rep :: "['a, 'a, 'b, bool] => bool"
nipkow@10213
    17
  "Inl_Rep == (%a. %x y p. x=a & p)"
nipkow@10213
    18
paulson@15391
    19
  Inr_Rep :: "['b, 'a, 'b, bool] => bool"
nipkow@10213
    20
  "Inr_Rep == (%b. %x y p. y=b & ~p)"
nipkow@10213
    21
paulson@15391
    22
nipkow@10213
    23
global
nipkow@10213
    24
nipkow@10213
    25
typedef (Sum)
haftmann@22838
    26
  ('a, 'b) "+"          (infixr "+" 10)
nipkow@10213
    27
    = "{f. (? a. f = Inl_Rep(a::'a)) | (? b. f = Inr_Rep(b::'b))}"
paulson@15391
    28
  by auto
nipkow@10213
    29
nipkow@10213
    30
local
nipkow@10213
    31
paulson@15391
    32
paulson@15391
    33
text{*abstract constants and syntax*}
paulson@15391
    34
paulson@15391
    35
constdefs
paulson@15391
    36
  Inl :: "'a => 'a + 'b"
paulson@15391
    37
   "Inl == (%a. Abs_Sum(Inl_Rep(a)))"
paulson@15391
    38
paulson@15391
    39
  Inr :: "'b => 'a + 'b"
paulson@15391
    40
   "Inr == (%b. Abs_Sum(Inr_Rep(b)))"
paulson@15391
    41
paulson@15391
    42
  Plus :: "['a set, 'b set] => ('a + 'b) set"        (infixr "<+>" 65)
paulson@15391
    43
   "A <+> B == (Inl`A) Un (Inr`B)"
paulson@15391
    44
    --{*disjoint sum for sets; the operator + is overloaded with wrong type!*}
paulson@15391
    45
paulson@15391
    46
  Part :: "['a set, 'b => 'a] => 'a set"
paulson@15391
    47
   "Part A h == A Int {x. ? z. x = h(z)}"
paulson@15391
    48
    --{*for selecting out the components of a mutually recursive definition*}
paulson@15391
    49
paulson@15391
    50
paulson@15391
    51
paulson@15391
    52
(** Inl_Rep and Inr_Rep: Representations of the constructors **)
paulson@15391
    53
paulson@15391
    54
(*This counts as a non-emptiness result for admitting 'a+'b as a type*)
paulson@15391
    55
lemma Inl_RepI: "Inl_Rep(a) : Sum"
paulson@15391
    56
by (auto simp add: Sum_def)
paulson@15391
    57
paulson@15391
    58
lemma Inr_RepI: "Inr_Rep(b) : Sum"
paulson@15391
    59
by (auto simp add: Sum_def)
paulson@15391
    60
paulson@15391
    61
lemma inj_on_Abs_Sum: "inj_on Abs_Sum Sum"
paulson@15391
    62
apply (rule inj_on_inverseI)
paulson@15391
    63
apply (erule Abs_Sum_inverse)
paulson@15391
    64
done
paulson@15391
    65
paulson@15391
    66
subsection{*Freeness Properties for @{term Inl} and  @{term Inr}*}
paulson@15391
    67
paulson@15391
    68
text{*Distinctness*}
paulson@15391
    69
paulson@15391
    70
lemma Inl_Rep_not_Inr_Rep: "Inl_Rep(a) ~= Inr_Rep(b)"
paulson@15391
    71
by (auto simp add: Inl_Rep_def Inr_Rep_def expand_fun_eq)
paulson@15391
    72
paulson@15391
    73
lemma Inl_not_Inr [iff]: "Inl(a) ~= Inr(b)"
paulson@15391
    74
apply (simp add: Inl_def Inr_def)
paulson@15391
    75
apply (rule inj_on_Abs_Sum [THEN inj_on_contraD])
paulson@15391
    76
apply (rule Inl_Rep_not_Inr_Rep)
paulson@15391
    77
apply (rule Inl_RepI)
paulson@15391
    78
apply (rule Inr_RepI)
paulson@15391
    79
done
paulson@15391
    80
paulson@17084
    81
lemmas Inr_not_Inl = Inl_not_Inr [THEN not_sym, standard]
paulson@17084
    82
declare Inr_not_Inl [iff]
paulson@15391
    83
paulson@15391
    84
lemmas Inl_neq_Inr = Inl_not_Inr [THEN notE, standard]
paulson@15391
    85
lemmas Inr_neq_Inl = sym [THEN Inl_neq_Inr, standard]
paulson@15391
    86
paulson@15391
    87
paulson@15391
    88
text{*Injectiveness*}
paulson@15391
    89
paulson@15391
    90
lemma Inl_Rep_inject: "Inl_Rep(a) = Inl_Rep(c) ==> a=c"
paulson@15391
    91
by (auto simp add: Inl_Rep_def expand_fun_eq)
paulson@15391
    92
paulson@15391
    93
lemma Inr_Rep_inject: "Inr_Rep(b) = Inr_Rep(d) ==> b=d"
paulson@15391
    94
by (auto simp add: Inr_Rep_def expand_fun_eq)
paulson@15391
    95
huffman@29025
    96
lemma inj_Inl [simp]: "inj_on Inl A"
paulson@15391
    97
apply (simp add: Inl_def)
paulson@15391
    98
apply (rule inj_onI)
paulson@15391
    99
apply (erule inj_on_Abs_Sum [THEN inj_onD, THEN Inl_Rep_inject])
paulson@15391
   100
apply (rule Inl_RepI)
paulson@15391
   101
apply (rule Inl_RepI)
paulson@15391
   102
done
huffman@29025
   103
paulson@15391
   104
lemmas Inl_inject = inj_Inl [THEN injD, standard]
paulson@15391
   105
huffman@29025
   106
lemma inj_Inr [simp]: "inj_on Inr A"
paulson@15391
   107
apply (simp add: Inr_def)
paulson@15391
   108
apply (rule inj_onI)
paulson@15391
   109
apply (erule inj_on_Abs_Sum [THEN inj_onD, THEN Inr_Rep_inject])
paulson@15391
   110
apply (rule Inr_RepI)
paulson@15391
   111
apply (rule Inr_RepI)
paulson@15391
   112
done
paulson@15391
   113
paulson@15391
   114
lemmas Inr_inject = inj_Inr [THEN injD, standard]
paulson@15391
   115
paulson@15391
   116
lemma Inl_eq [iff]: "(Inl(x)=Inl(y)) = (x=y)"
paulson@15391
   117
by (blast dest!: Inl_inject)
paulson@15391
   118
paulson@15391
   119
lemma Inr_eq [iff]: "(Inr(x)=Inr(y)) = (x=y)"
paulson@15391
   120
by (blast dest!: Inr_inject)
paulson@15391
   121
paulson@15391
   122
paulson@15391
   123
subsection{*The Disjoint Sum of Sets*}
paulson@15391
   124
paulson@15391
   125
(** Introduction rules for the injections **)
nipkow@10213
   126
paulson@15391
   127
lemma InlI [intro!]: "a : A ==> Inl(a) : A <+> B"
paulson@15391
   128
by (simp add: Plus_def)
paulson@15391
   129
paulson@15391
   130
lemma InrI [intro!]: "b : B ==> Inr(b) : A <+> B"
paulson@15391
   131
by (simp add: Plus_def)
paulson@15391
   132
paulson@15391
   133
(** Elimination rules **)
paulson@15391
   134
paulson@15391
   135
lemma PlusE [elim!]: 
paulson@15391
   136
    "[| u: A <+> B;   
paulson@15391
   137
        !!x. [| x:A;  u=Inl(x) |] ==> P;  
paulson@15391
   138
        !!y. [| y:B;  u=Inr(y) |] ==> P  
paulson@15391
   139
     |] ==> P"
paulson@15391
   140
by (auto simp add: Plus_def)
paulson@15391
   141
paulson@15391
   142
paulson@15391
   143
paulson@15391
   144
text{*Exhaustion rule for sums, a degenerate form of induction*}
paulson@15391
   145
lemma sumE: 
paulson@15391
   146
    "[| !!x::'a. s = Inl(x) ==> P;  !!y::'b. s = Inr(y) ==> P  
paulson@15391
   147
     |] ==> P"
paulson@15391
   148
apply (rule Abs_Sum_cases [of s]) 
paulson@15391
   149
apply (auto simp add: Sum_def Inl_def Inr_def)
paulson@15391
   150
done
paulson@15391
   151
paulson@15391
   152
nipkow@17026
   153
lemma UNIV_Plus_UNIV [simp]: "UNIV <+> UNIV = UNIV"
nipkow@17026
   154
apply (rule set_ext)
nipkow@17026
   155
apply(rename_tac s)
nipkow@17026
   156
apply(rule_tac s=s in sumE)
nipkow@17026
   157
apply auto
nipkow@17026
   158
done
nipkow@17026
   159
nipkow@31080
   160
lemma Plus_eq_empty_conv[simp]: "A <+> B = {} \<longleftrightarrow> A = {} \<and> B = {}"
nipkow@31080
   161
by(auto)
nipkow@17026
   162
paulson@15391
   163
subsection{*The @{term Part} Primitive*}
paulson@15391
   164
paulson@15391
   165
lemma Part_eqI [intro]: "[| a : A;  a=h(b) |] ==> a : Part A h"
paulson@15391
   166
by (auto simp add: Part_def)
paulson@15391
   167
paulson@15391
   168
lemmas PartI = Part_eqI [OF _ refl, standard]
paulson@15391
   169
paulson@15391
   170
lemma PartE [elim!]: "[| a : Part A h;  !!z. [| a : A;  a=h(z) |] ==> P |] ==> P"
paulson@15391
   171
by (auto simp add: Part_def)
paulson@15391
   172
paulson@15391
   173
paulson@15391
   174
lemma Part_subset: "Part A h <= A"
paulson@15391
   175
by (auto simp add: Part_def)
paulson@15391
   176
paulson@15391
   177
lemma Part_mono: "A<=B ==> Part A h <= Part B h"
paulson@15391
   178
by blast
paulson@15391
   179
paulson@15391
   180
lemmas basic_monos = basic_monos Part_mono
nipkow@10213
   181
paulson@15391
   182
lemma PartD1: "a : Part A h ==> a : A"
paulson@15391
   183
by (simp add: Part_def)
paulson@15391
   184
paulson@15391
   185
lemma Part_id: "Part A (%x. x) = A"
paulson@15391
   186
by blast
paulson@15391
   187
paulson@15391
   188
lemma Part_Int: "Part (A Int B) h = (Part A h) Int (Part B h)"
paulson@15391
   189
by blast
paulson@15391
   190
paulson@15391
   191
lemma Part_Collect: "Part (A Int {x. P x}) h = (Part A h) Int {x. P x}"
paulson@15391
   192
by blast
paulson@15391
   193
haftmann@33961
   194
subsection {* Representing sums *}
haftmann@33961
   195
haftmann@33961
   196
rep_datatype (sum) Inl Inr
haftmann@33961
   197
proof -
haftmann@33961
   198
  fix P
haftmann@33961
   199
  fix s :: "'a + 'b"
haftmann@33961
   200
  assume x: "\<And>x\<Colon>'a. P (Inl x)" and y: "\<And>y\<Colon>'b. P (Inr y)"
haftmann@33961
   201
  then show "P s" by (auto intro: sumE [of s])
haftmann@33961
   202
qed simp_all
haftmann@33961
   203
haftmann@33961
   204
lemma sum_case_KK[simp]: "sum_case (%x. a) (%x. a) = (%x. a)"
haftmann@33961
   205
  by (rule ext) (simp split: sum.split)
haftmann@33961
   206
haftmann@33961
   207
lemma surjective_sum: "sum_case (%x::'a. f (Inl x)) (%y::'b. f (Inr y)) s = f(s)"
haftmann@33961
   208
  apply (rule_tac s = s in sumE)
haftmann@33961
   209
   apply (erule ssubst)
haftmann@33961
   210
   apply (rule sum.cases(1))
haftmann@33961
   211
  apply (erule ssubst)
haftmann@33961
   212
  apply (rule sum.cases(2))
haftmann@33961
   213
  done
haftmann@33961
   214
haftmann@33961
   215
lemma sum_case_weak_cong: "s = t ==> sum_case f g s = sum_case f g t"
haftmann@33961
   216
  -- {* Prevents simplification of @{text f} and @{text g}: much faster. *}
haftmann@33961
   217
  by simp
haftmann@33961
   218
haftmann@33961
   219
lemma sum_case_inject:
haftmann@33961
   220
  "sum_case f1 f2 = sum_case g1 g2 ==> (f1 = g1 ==> f2 = g2 ==> P) ==> P"
haftmann@33961
   221
proof -
haftmann@33961
   222
  assume a: "sum_case f1 f2 = sum_case g1 g2"
haftmann@33961
   223
  assume r: "f1 = g1 ==> f2 = g2 ==> P"
haftmann@33961
   224
  show P
haftmann@33961
   225
    apply (rule r)
haftmann@33961
   226
     apply (rule ext)
haftmann@33961
   227
     apply (cut_tac x = "Inl x" in a [THEN fun_cong], simp)
haftmann@33961
   228
    apply (rule ext)
haftmann@33961
   229
    apply (cut_tac x = "Inr x" in a [THEN fun_cong], simp)
haftmann@33961
   230
    done
haftmann@33961
   231
qed
haftmann@33961
   232
haftmann@33961
   233
constdefs
haftmann@33961
   234
  Suml :: "('a => 'c) => 'a + 'b => 'c"
haftmann@33961
   235
  "Suml == (%f. sum_case f undefined)"
haftmann@33961
   236
haftmann@33961
   237
  Sumr :: "('b => 'c) => 'a + 'b => 'c"
haftmann@33961
   238
  "Sumr == sum_case undefined"
haftmann@33961
   239
haftmann@33961
   240
lemma [code]:
haftmann@33961
   241
  "Suml f (Inl x) = f x"
haftmann@33961
   242
  by (simp add: Suml_def)
haftmann@33961
   243
haftmann@33961
   244
lemma [code]:
haftmann@33961
   245
  "Sumr f (Inr x) = f x"
haftmann@33961
   246
  by (simp add: Sumr_def)
haftmann@33961
   247
haftmann@33961
   248
lemma Suml_inject: "Suml f = Suml g ==> f = g"
haftmann@33961
   249
  by (unfold Suml_def) (erule sum_case_inject)
haftmann@33961
   250
haftmann@33961
   251
lemma Sumr_inject: "Sumr f = Sumr g ==> f = g"
haftmann@33961
   252
  by (unfold Sumr_def) (erule sum_case_inject)
haftmann@33961
   253
haftmann@33961
   254
primrec Projl :: "'a + 'b => 'a"
haftmann@33961
   255
where Projl_Inl: "Projl (Inl x) = x"
haftmann@33961
   256
haftmann@33961
   257
primrec Projr :: "'a + 'b => 'b"
haftmann@33961
   258
where Projr_Inr: "Projr (Inr x) = x"
haftmann@33961
   259
haftmann@33961
   260
hide (open) const Suml Sumr Projl Projr
haftmann@33961
   261
haftmann@20588
   262
paulson@15391
   263
ML
paulson@15391
   264
{*
paulson@15391
   265
val Inl_RepI = thm "Inl_RepI";
paulson@15391
   266
val Inr_RepI = thm "Inr_RepI";
paulson@15391
   267
val inj_on_Abs_Sum = thm "inj_on_Abs_Sum";
paulson@15391
   268
val Inl_Rep_not_Inr_Rep = thm "Inl_Rep_not_Inr_Rep";
paulson@15391
   269
val Inl_not_Inr = thm "Inl_not_Inr";
paulson@15391
   270
val Inr_not_Inl = thm "Inr_not_Inl";
paulson@15391
   271
val Inl_neq_Inr = thm "Inl_neq_Inr";
paulson@15391
   272
val Inr_neq_Inl = thm "Inr_neq_Inl";
paulson@15391
   273
val Inl_Rep_inject = thm "Inl_Rep_inject";
paulson@15391
   274
val Inr_Rep_inject = thm "Inr_Rep_inject";
paulson@15391
   275
val inj_Inl = thm "inj_Inl";
paulson@15391
   276
val Inl_inject = thm "Inl_inject";
paulson@15391
   277
val inj_Inr = thm "inj_Inr";
paulson@15391
   278
val Inr_inject = thm "Inr_inject";
paulson@15391
   279
val Inl_eq = thm "Inl_eq";
paulson@15391
   280
val Inr_eq = thm "Inr_eq";
paulson@15391
   281
val InlI = thm "InlI";
paulson@15391
   282
val InrI = thm "InrI";
paulson@15391
   283
val PlusE = thm "PlusE";
paulson@15391
   284
val sumE = thm "sumE";
paulson@15391
   285
val Part_eqI = thm "Part_eqI";
paulson@15391
   286
val PartI = thm "PartI";
paulson@15391
   287
val PartE = thm "PartE";
paulson@15391
   288
val Part_subset = thm "Part_subset";
paulson@15391
   289
val Part_mono = thm "Part_mono";
paulson@15391
   290
val PartD1 = thm "PartD1";
paulson@15391
   291
val Part_id = thm "Part_id";
paulson@15391
   292
val Part_Int = thm "Part_Int";
paulson@15391
   293
val Part_Collect = thm "Part_Collect";
paulson@15391
   294
paulson@15391
   295
val basic_monos = thms "basic_monos";
paulson@15391
   296
*}
paulson@15391
   297
nipkow@10213
   298
end