src/HOL/Sum_Type.thy
author haftmann
Wed Sep 26 20:27:55 2007 +0200 (2007-09-26)
changeset 24728 e2b3a1065676
parent 22838 466599ecf610
child 25534 d0b74fdd6067
permissions -rw-r--r--
moved Finite_Set before Datatype
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@22424
    10
imports Typedef 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
paulson@15391
    96
lemma inj_Inl: "inj(Inl)"
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
paulson@15391
   103
lemmas Inl_inject = inj_Inl [THEN injD, standard]
paulson@15391
   104
paulson@15391
   105
lemma inj_Inr: "inj(Inr)"
paulson@15391
   106
apply (simp add: Inr_def)
paulson@15391
   107
apply (rule inj_onI)
paulson@15391
   108
apply (erule inj_on_Abs_Sum [THEN inj_onD, THEN Inr_Rep_inject])
paulson@15391
   109
apply (rule Inr_RepI)
paulson@15391
   110
apply (rule Inr_RepI)
paulson@15391
   111
done
paulson@15391
   112
paulson@15391
   113
lemmas Inr_inject = inj_Inr [THEN injD, standard]
paulson@15391
   114
paulson@15391
   115
lemma Inl_eq [iff]: "(Inl(x)=Inl(y)) = (x=y)"
paulson@15391
   116
by (blast dest!: Inl_inject)
paulson@15391
   117
paulson@15391
   118
lemma Inr_eq [iff]: "(Inr(x)=Inr(y)) = (x=y)"
paulson@15391
   119
by (blast dest!: Inr_inject)
paulson@15391
   120
paulson@15391
   121
krauss@22622
   122
subsection {* Projections *}
krauss@22622
   123
krauss@22622
   124
definition 
krauss@22622
   125
  "sum_case f g x =
krauss@22622
   126
  (if (\<exists>!y. x = Inl y) 
krauss@22622
   127
  then f (THE y. x = Inl y) 
krauss@22622
   128
  else g (THE y. x = Inr y))"
krauss@22622
   129
definition "Projl x = sum_case id arbitrary x"
krauss@22622
   130
definition "Projr x = sum_case arbitrary id x"
krauss@22622
   131
krauss@22622
   132
lemma sum_cases[simp]: 
krauss@22622
   133
  "sum_case f g (Inl x) = f x"
krauss@22622
   134
  "sum_case f g (Inr y) = g y"
krauss@22622
   135
  unfolding sum_case_def
krauss@22622
   136
  by auto
krauss@22622
   137
krauss@22622
   138
lemma Projl_Inl[simp]: "Projl (Inl x) = x"
krauss@22622
   139
  unfolding Projl_def by simp
krauss@22622
   140
krauss@22622
   141
lemma Projr_Inr[simp]: "Projr (Inr x) = x"
krauss@22622
   142
  unfolding Projr_def by simp
krauss@22622
   143
krauss@22622
   144
paulson@15391
   145
subsection{*The Disjoint Sum of Sets*}
paulson@15391
   146
paulson@15391
   147
(** Introduction rules for the injections **)
nipkow@10213
   148
paulson@15391
   149
lemma InlI [intro!]: "a : A ==> Inl(a) : A <+> B"
paulson@15391
   150
by (simp add: Plus_def)
paulson@15391
   151
paulson@15391
   152
lemma InrI [intro!]: "b : B ==> Inr(b) : A <+> B"
paulson@15391
   153
by (simp add: Plus_def)
paulson@15391
   154
paulson@15391
   155
(** Elimination rules **)
paulson@15391
   156
paulson@15391
   157
lemma PlusE [elim!]: 
paulson@15391
   158
    "[| u: A <+> B;   
paulson@15391
   159
        !!x. [| x:A;  u=Inl(x) |] ==> P;  
paulson@15391
   160
        !!y. [| y:B;  u=Inr(y) |] ==> P  
paulson@15391
   161
     |] ==> P"
paulson@15391
   162
by (auto simp add: Plus_def)
paulson@15391
   163
paulson@15391
   164
paulson@15391
   165
paulson@15391
   166
text{*Exhaustion rule for sums, a degenerate form of induction*}
paulson@15391
   167
lemma sumE: 
paulson@15391
   168
    "[| !!x::'a. s = Inl(x) ==> P;  !!y::'b. s = Inr(y) ==> P  
paulson@15391
   169
     |] ==> P"
paulson@15391
   170
apply (rule Abs_Sum_cases [of s]) 
paulson@15391
   171
apply (auto simp add: Sum_def Inl_def Inr_def)
paulson@15391
   172
done
paulson@15391
   173
paulson@15391
   174
lemma sum_induct: "[| !!x. P (Inl x); !!x. P (Inr x) |] ==> P x"
paulson@15391
   175
by (rule sumE [of x], auto)
paulson@15391
   176
paulson@15391
   177
nipkow@17026
   178
lemma UNIV_Plus_UNIV [simp]: "UNIV <+> UNIV = UNIV"
nipkow@17026
   179
apply (rule set_ext)
nipkow@17026
   180
apply(rename_tac s)
nipkow@17026
   181
apply(rule_tac s=s in sumE)
nipkow@17026
   182
apply auto
nipkow@17026
   183
done
nipkow@17026
   184
nipkow@17026
   185
paulson@15391
   186
subsection{*The @{term Part} Primitive*}
paulson@15391
   187
paulson@15391
   188
lemma Part_eqI [intro]: "[| a : A;  a=h(b) |] ==> a : Part A h"
paulson@15391
   189
by (auto simp add: Part_def)
paulson@15391
   190
paulson@15391
   191
lemmas PartI = Part_eqI [OF _ refl, standard]
paulson@15391
   192
paulson@15391
   193
lemma PartE [elim!]: "[| a : Part A h;  !!z. [| a : A;  a=h(z) |] ==> P |] ==> P"
paulson@15391
   194
by (auto simp add: Part_def)
paulson@15391
   195
paulson@15391
   196
paulson@15391
   197
lemma Part_subset: "Part A h <= A"
paulson@15391
   198
by (auto simp add: Part_def)
paulson@15391
   199
paulson@15391
   200
lemma Part_mono: "A<=B ==> Part A h <= Part B h"
paulson@15391
   201
by blast
paulson@15391
   202
paulson@15391
   203
lemmas basic_monos = basic_monos Part_mono
nipkow@10213
   204
paulson@15391
   205
lemma PartD1: "a : Part A h ==> a : A"
paulson@15391
   206
by (simp add: Part_def)
paulson@15391
   207
paulson@15391
   208
lemma Part_id: "Part A (%x. x) = A"
paulson@15391
   209
by blast
paulson@15391
   210
paulson@15391
   211
lemma Part_Int: "Part (A Int B) h = (Part A h) Int (Part B h)"
paulson@15391
   212
by blast
paulson@15391
   213
paulson@15391
   214
lemma Part_Collect: "Part (A Int {x. P x}) h = (Part A h) Int {x. P x}"
paulson@15391
   215
by blast
paulson@15391
   216
haftmann@20588
   217
haftmann@20588
   218
subsection {* Code generator setup *}
haftmann@20588
   219
haftmann@20588
   220
instance "+" :: (eq, eq) eq ..
haftmann@20588
   221
haftmann@20588
   222
lemma [code func]:
haftmann@21454
   223
  "(Inl x \<Colon> 'a\<Colon>eq + 'b\<Colon>eq) = Inl y \<longleftrightarrow> x = y"
haftmann@21454
   224
  unfolding Inl_eq ..
haftmann@20588
   225
haftmann@20588
   226
lemma [code func]:
haftmann@21454
   227
  "(Inr x \<Colon> 'a\<Colon>eq + 'b\<Colon>eq) = Inr y \<longleftrightarrow> x = y"
haftmann@21454
   228
  unfolding Inr_eq ..
haftmann@20588
   229
haftmann@20588
   230
lemma [code func]:
haftmann@21454
   231
  "Inl (x\<Colon>'a\<Colon>eq) = Inr (y\<Colon>'b\<Colon>eq) \<longleftrightarrow> False"
haftmann@21454
   232
  using Inl_not_Inr by auto
haftmann@20588
   233
haftmann@20588
   234
lemma [code func]:
haftmann@21454
   235
  "Inr (x\<Colon>'b\<Colon>eq) = Inl (y\<Colon>'a\<Colon>eq) \<longleftrightarrow> False"
haftmann@21454
   236
  using Inr_not_Inl by auto
haftmann@20588
   237
paulson@15391
   238
ML
paulson@15391
   239
{*
paulson@15391
   240
val Inl_RepI = thm "Inl_RepI";
paulson@15391
   241
val Inr_RepI = thm "Inr_RepI";
paulson@15391
   242
val inj_on_Abs_Sum = thm "inj_on_Abs_Sum";
paulson@15391
   243
val Inl_Rep_not_Inr_Rep = thm "Inl_Rep_not_Inr_Rep";
paulson@15391
   244
val Inl_not_Inr = thm "Inl_not_Inr";
paulson@15391
   245
val Inr_not_Inl = thm "Inr_not_Inl";
paulson@15391
   246
val Inl_neq_Inr = thm "Inl_neq_Inr";
paulson@15391
   247
val Inr_neq_Inl = thm "Inr_neq_Inl";
paulson@15391
   248
val Inl_Rep_inject = thm "Inl_Rep_inject";
paulson@15391
   249
val Inr_Rep_inject = thm "Inr_Rep_inject";
paulson@15391
   250
val inj_Inl = thm "inj_Inl";
paulson@15391
   251
val Inl_inject = thm "Inl_inject";
paulson@15391
   252
val inj_Inr = thm "inj_Inr";
paulson@15391
   253
val Inr_inject = thm "Inr_inject";
paulson@15391
   254
val Inl_eq = thm "Inl_eq";
paulson@15391
   255
val Inr_eq = thm "Inr_eq";
paulson@15391
   256
val InlI = thm "InlI";
paulson@15391
   257
val InrI = thm "InrI";
paulson@15391
   258
val PlusE = thm "PlusE";
paulson@15391
   259
val sumE = thm "sumE";
paulson@15391
   260
val sum_induct = thm "sum_induct";
paulson@15391
   261
val Part_eqI = thm "Part_eqI";
paulson@15391
   262
val PartI = thm "PartI";
paulson@15391
   263
val PartE = thm "PartE";
paulson@15391
   264
val Part_subset = thm "Part_subset";
paulson@15391
   265
val Part_mono = thm "Part_mono";
paulson@15391
   266
val PartD1 = thm "PartD1";
paulson@15391
   267
val Part_id = thm "Part_id";
paulson@15391
   268
val Part_Int = thm "Part_Int";
paulson@15391
   269
val Part_Collect = thm "Part_Collect";
paulson@15391
   270
paulson@15391
   271
val basic_monos = thms "basic_monos";
paulson@15391
   272
*}
paulson@15391
   273
nipkow@10213
   274
end