src/HOL/Sum_Type.thy
author haftmann
Wed Nov 22 10:20:12 2006 +0100 (2006-11-22)
changeset 21454 a1937c51ed88
parent 21046 fe1db2f991a7
child 22424 8a5412121687
permissions -rw-r--r--
dropped eq const
     1 (*  Title:      HOL/Sum_Type.thy
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1992  University of Cambridge
     5 *)
     6 
     7 header{*The Disjoint Sum of Two Types*}
     8 
     9 theory Sum_Type
    10 imports Product_Type
    11 begin
    12 
    13 text{*The representations of the two injections*}
    14 
    15 constdefs
    16   Inl_Rep :: "['a, 'a, 'b, bool] => bool"
    17   "Inl_Rep == (%a. %x y p. x=a & p)"
    18 
    19   Inr_Rep :: "['b, 'a, 'b, bool] => bool"
    20   "Inr_Rep == (%b. %x y p. y=b & ~p)"
    21 
    22 
    23 global
    24 
    25 typedef (Sum)
    26   ('a, 'b) "+"          (infixr 10)
    27     = "{f. (? a. f = Inl_Rep(a::'a)) | (? b. f = Inr_Rep(b::'b))}"
    28   by auto
    29 
    30 local
    31 
    32 
    33 text{*abstract constants and syntax*}
    34 
    35 constdefs
    36   Inl :: "'a => 'a + 'b"
    37    "Inl == (%a. Abs_Sum(Inl_Rep(a)))"
    38 
    39   Inr :: "'b => 'a + 'b"
    40    "Inr == (%b. Abs_Sum(Inr_Rep(b)))"
    41 
    42   Plus :: "['a set, 'b set] => ('a + 'b) set"        (infixr "<+>" 65)
    43    "A <+> B == (Inl`A) Un (Inr`B)"
    44     --{*disjoint sum for sets; the operator + is overloaded with wrong type!*}
    45 
    46   Part :: "['a set, 'b => 'a] => 'a set"
    47    "Part A h == A Int {x. ? z. x = h(z)}"
    48     --{*for selecting out the components of a mutually recursive definition*}
    49 
    50 
    51 
    52 (** Inl_Rep and Inr_Rep: Representations of the constructors **)
    53 
    54 (*This counts as a non-emptiness result for admitting 'a+'b as a type*)
    55 lemma Inl_RepI: "Inl_Rep(a) : Sum"
    56 by (auto simp add: Sum_def)
    57 
    58 lemma Inr_RepI: "Inr_Rep(b) : Sum"
    59 by (auto simp add: Sum_def)
    60 
    61 lemma inj_on_Abs_Sum: "inj_on Abs_Sum Sum"
    62 apply (rule inj_on_inverseI)
    63 apply (erule Abs_Sum_inverse)
    64 done
    65 
    66 subsection{*Freeness Properties for @{term Inl} and  @{term Inr}*}
    67 
    68 text{*Distinctness*}
    69 
    70 lemma Inl_Rep_not_Inr_Rep: "Inl_Rep(a) ~= Inr_Rep(b)"
    71 by (auto simp add: Inl_Rep_def Inr_Rep_def expand_fun_eq)
    72 
    73 lemma Inl_not_Inr [iff]: "Inl(a) ~= Inr(b)"
    74 apply (simp add: Inl_def Inr_def)
    75 apply (rule inj_on_Abs_Sum [THEN inj_on_contraD])
    76 apply (rule Inl_Rep_not_Inr_Rep)
    77 apply (rule Inl_RepI)
    78 apply (rule Inr_RepI)
    79 done
    80 
    81 lemmas Inr_not_Inl = Inl_not_Inr [THEN not_sym, standard]
    82 declare Inr_not_Inl [iff]
    83 
    84 lemmas Inl_neq_Inr = Inl_not_Inr [THEN notE, standard]
    85 lemmas Inr_neq_Inl = sym [THEN Inl_neq_Inr, standard]
    86 
    87 
    88 text{*Injectiveness*}
    89 
    90 lemma Inl_Rep_inject: "Inl_Rep(a) = Inl_Rep(c) ==> a=c"
    91 by (auto simp add: Inl_Rep_def expand_fun_eq)
    92 
    93 lemma Inr_Rep_inject: "Inr_Rep(b) = Inr_Rep(d) ==> b=d"
    94 by (auto simp add: Inr_Rep_def expand_fun_eq)
    95 
    96 lemma inj_Inl: "inj(Inl)"
    97 apply (simp add: Inl_def)
    98 apply (rule inj_onI)
    99 apply (erule inj_on_Abs_Sum [THEN inj_onD, THEN Inl_Rep_inject])
   100 apply (rule Inl_RepI)
   101 apply (rule Inl_RepI)
   102 done
   103 lemmas Inl_inject = inj_Inl [THEN injD, standard]
   104 
   105 lemma inj_Inr: "inj(Inr)"
   106 apply (simp add: Inr_def)
   107 apply (rule inj_onI)
   108 apply (erule inj_on_Abs_Sum [THEN inj_onD, THEN Inr_Rep_inject])
   109 apply (rule Inr_RepI)
   110 apply (rule Inr_RepI)
   111 done
   112 
   113 lemmas Inr_inject = inj_Inr [THEN injD, standard]
   114 
   115 lemma Inl_eq [iff]: "(Inl(x)=Inl(y)) = (x=y)"
   116 by (blast dest!: Inl_inject)
   117 
   118 lemma Inr_eq [iff]: "(Inr(x)=Inr(y)) = (x=y)"
   119 by (blast dest!: Inr_inject)
   120 
   121 
   122 subsection{*The Disjoint Sum of Sets*}
   123 
   124 (** Introduction rules for the injections **)
   125 
   126 lemma InlI [intro!]: "a : A ==> Inl(a) : A <+> B"
   127 by (simp add: Plus_def)
   128 
   129 lemma InrI [intro!]: "b : B ==> Inr(b) : A <+> B"
   130 by (simp add: Plus_def)
   131 
   132 (** Elimination rules **)
   133 
   134 lemma PlusE [elim!]: 
   135     "[| u: A <+> B;   
   136         !!x. [| x:A;  u=Inl(x) |] ==> P;  
   137         !!y. [| y:B;  u=Inr(y) |] ==> P  
   138      |] ==> P"
   139 by (auto simp add: Plus_def)
   140 
   141 
   142 
   143 text{*Exhaustion rule for sums, a degenerate form of induction*}
   144 lemma sumE: 
   145     "[| !!x::'a. s = Inl(x) ==> P;  !!y::'b. s = Inr(y) ==> P  
   146      |] ==> P"
   147 apply (rule Abs_Sum_cases [of s]) 
   148 apply (auto simp add: Sum_def Inl_def Inr_def)
   149 done
   150 
   151 lemma sum_induct: "[| !!x. P (Inl x); !!x. P (Inr x) |] ==> P x"
   152 by (rule sumE [of x], auto)
   153 
   154 
   155 lemma UNIV_Plus_UNIV [simp]: "UNIV <+> UNIV = UNIV"
   156 apply (rule set_ext)
   157 apply(rename_tac s)
   158 apply(rule_tac s=s in sumE)
   159 apply auto
   160 done
   161 
   162 
   163 subsection{*The @{term Part} Primitive*}
   164 
   165 lemma Part_eqI [intro]: "[| a : A;  a=h(b) |] ==> a : Part A h"
   166 by (auto simp add: Part_def)
   167 
   168 lemmas PartI = Part_eqI [OF _ refl, standard]
   169 
   170 lemma PartE [elim!]: "[| a : Part A h;  !!z. [| a : A;  a=h(z) |] ==> P |] ==> P"
   171 by (auto simp add: Part_def)
   172 
   173 
   174 lemma Part_subset: "Part A h <= A"
   175 by (auto simp add: Part_def)
   176 
   177 lemma Part_mono: "A<=B ==> Part A h <= Part B h"
   178 by blast
   179 
   180 lemmas basic_monos = basic_monos Part_mono
   181 
   182 lemma PartD1: "a : Part A h ==> a : A"
   183 by (simp add: Part_def)
   184 
   185 lemma Part_id: "Part A (%x. x) = A"
   186 by blast
   187 
   188 lemma Part_Int: "Part (A Int B) h = (Part A h) Int (Part B h)"
   189 by blast
   190 
   191 lemma Part_Collect: "Part (A Int {x. P x}) h = (Part A h) Int {x. P x}"
   192 by blast
   193 
   194 
   195 subsection {* Code generator setup *}
   196 
   197 instance "+" :: (eq, eq) eq ..
   198 
   199 lemma [code func]:
   200   "(Inl x \<Colon> 'a\<Colon>eq + 'b\<Colon>eq) = Inl y \<longleftrightarrow> x = y"
   201   unfolding Inl_eq ..
   202 
   203 lemma [code func]:
   204   "(Inr x \<Colon> 'a\<Colon>eq + 'b\<Colon>eq) = Inr y \<longleftrightarrow> x = y"
   205   unfolding Inr_eq ..
   206 
   207 lemma [code func]:
   208   "Inl (x\<Colon>'a\<Colon>eq) = Inr (y\<Colon>'b\<Colon>eq) \<longleftrightarrow> False"
   209   using Inl_not_Inr by auto
   210 
   211 lemma [code func]:
   212   "Inr (x\<Colon>'b\<Colon>eq) = Inl (y\<Colon>'a\<Colon>eq) \<longleftrightarrow> False"
   213   using Inr_not_Inl by auto
   214 
   215 ML
   216 {*
   217 val Inl_RepI = thm "Inl_RepI";
   218 val Inr_RepI = thm "Inr_RepI";
   219 val inj_on_Abs_Sum = thm "inj_on_Abs_Sum";
   220 val Inl_Rep_not_Inr_Rep = thm "Inl_Rep_not_Inr_Rep";
   221 val Inl_not_Inr = thm "Inl_not_Inr";
   222 val Inr_not_Inl = thm "Inr_not_Inl";
   223 val Inl_neq_Inr = thm "Inl_neq_Inr";
   224 val Inr_neq_Inl = thm "Inr_neq_Inl";
   225 val Inl_Rep_inject = thm "Inl_Rep_inject";
   226 val Inr_Rep_inject = thm "Inr_Rep_inject";
   227 val inj_Inl = thm "inj_Inl";
   228 val Inl_inject = thm "Inl_inject";
   229 val inj_Inr = thm "inj_Inr";
   230 val Inr_inject = thm "Inr_inject";
   231 val Inl_eq = thm "Inl_eq";
   232 val Inr_eq = thm "Inr_eq";
   233 val InlI = thm "InlI";
   234 val InrI = thm "InrI";
   235 val PlusE = thm "PlusE";
   236 val sumE = thm "sumE";
   237 val sum_induct = thm "sum_induct";
   238 val Part_eqI = thm "Part_eqI";
   239 val PartI = thm "PartI";
   240 val PartE = thm "PartE";
   241 val Part_subset = thm "Part_subset";
   242 val Part_mono = thm "Part_mono";
   243 val PartD1 = thm "PartD1";
   244 val Part_id = thm "Part_id";
   245 val Part_Int = thm "Part_Int";
   246 val Part_Collect = thm "Part_Collect";
   247 
   248 val basic_monos = thms "basic_monos";
   249 *}
   250 
   251 end