src/HOL/Sum_Type.thy
author haftmann
Tue Sep 19 15:21:42 2006 +0200 (2006-09-19)
changeset 20588 c847c56edf0c
parent 20380 14f9f2a1caa6
child 21046 fe1db2f991a7
permissions -rw-r--r--
added operational equality
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
paulson@15391
    10
imports Product_Type
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)
nipkow@10213
    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
paulson@15391
   122
subsection{*The Disjoint Sum of Sets*}
paulson@15391
   123
paulson@15391
   124
(** Introduction rules for the injections **)
nipkow@10213
   125
paulson@15391
   126
lemma InlI [intro!]: "a : A ==> Inl(a) : A <+> B"
paulson@15391
   127
by (simp add: Plus_def)
paulson@15391
   128
paulson@15391
   129
lemma InrI [intro!]: "b : B ==> Inr(b) : A <+> B"
paulson@15391
   130
by (simp add: Plus_def)
paulson@15391
   131
paulson@15391
   132
(** Elimination rules **)
paulson@15391
   133
paulson@15391
   134
lemma PlusE [elim!]: 
paulson@15391
   135
    "[| u: A <+> B;   
paulson@15391
   136
        !!x. [| x:A;  u=Inl(x) |] ==> P;  
paulson@15391
   137
        !!y. [| y:B;  u=Inr(y) |] ==> P  
paulson@15391
   138
     |] ==> P"
paulson@15391
   139
by (auto simp add: Plus_def)
paulson@15391
   140
paulson@15391
   141
paulson@15391
   142
paulson@15391
   143
text{*Exhaustion rule for sums, a degenerate form of induction*}
paulson@15391
   144
lemma sumE: 
paulson@15391
   145
    "[| !!x::'a. s = Inl(x) ==> P;  !!y::'b. s = Inr(y) ==> P  
paulson@15391
   146
     |] ==> P"
paulson@15391
   147
apply (rule Abs_Sum_cases [of s]) 
paulson@15391
   148
apply (auto simp add: Sum_def Inl_def Inr_def)
paulson@15391
   149
done
paulson@15391
   150
paulson@15391
   151
lemma sum_induct: "[| !!x. P (Inl x); !!x. P (Inr x) |] ==> P x"
paulson@15391
   152
by (rule sumE [of x], auto)
paulson@15391
   153
paulson@15391
   154
nipkow@17026
   155
lemma UNIV_Plus_UNIV [simp]: "UNIV <+> UNIV = UNIV"
nipkow@17026
   156
apply (rule set_ext)
nipkow@17026
   157
apply(rename_tac s)
nipkow@17026
   158
apply(rule_tac s=s in sumE)
nipkow@17026
   159
apply auto
nipkow@17026
   160
done
nipkow@17026
   161
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@20588
   194
haftmann@20588
   195
subsection {* Code generator setup *}
haftmann@20588
   196
haftmann@20588
   197
instance "+" :: (eq, eq) eq ..
haftmann@20588
   198
haftmann@20588
   199
lemma [code func]:
haftmann@20588
   200
  "OperationalEquality.eq (Inl x) (Inl y) = OperationalEquality.eq x y"
haftmann@20588
   201
  unfolding eq_def Inl_eq ..
haftmann@20588
   202
haftmann@20588
   203
lemma [code func]:
haftmann@20588
   204
  "OperationalEquality.eq (Inr x) (Inr y) = OperationalEquality.eq x y"
haftmann@20588
   205
  unfolding eq_def Inr_eq ..
haftmann@20588
   206
haftmann@20588
   207
lemma [code func]:
haftmann@20588
   208
  "OperationalEquality.eq (Inl x) (Inr y) = False"
haftmann@20588
   209
  unfolding eq_def using Inl_not_Inr by auto
haftmann@20588
   210
haftmann@20588
   211
lemma [code func]:
haftmann@20588
   212
  "OperationalEquality.eq (Inr x) (Inl y) = False"
haftmann@20588
   213
  unfolding eq_def using Inr_not_Inl by auto
haftmann@20588
   214
paulson@15391
   215
ML
paulson@15391
   216
{*
paulson@15391
   217
val Inl_RepI = thm "Inl_RepI";
paulson@15391
   218
val Inr_RepI = thm "Inr_RepI";
paulson@15391
   219
val inj_on_Abs_Sum = thm "inj_on_Abs_Sum";
paulson@15391
   220
val Inl_Rep_not_Inr_Rep = thm "Inl_Rep_not_Inr_Rep";
paulson@15391
   221
val Inl_not_Inr = thm "Inl_not_Inr";
paulson@15391
   222
val Inr_not_Inl = thm "Inr_not_Inl";
paulson@15391
   223
val Inl_neq_Inr = thm "Inl_neq_Inr";
paulson@15391
   224
val Inr_neq_Inl = thm "Inr_neq_Inl";
paulson@15391
   225
val Inl_Rep_inject = thm "Inl_Rep_inject";
paulson@15391
   226
val Inr_Rep_inject = thm "Inr_Rep_inject";
paulson@15391
   227
val inj_Inl = thm "inj_Inl";
paulson@15391
   228
val Inl_inject = thm "Inl_inject";
paulson@15391
   229
val inj_Inr = thm "inj_Inr";
paulson@15391
   230
val Inr_inject = thm "Inr_inject";
paulson@15391
   231
val Inl_eq = thm "Inl_eq";
paulson@15391
   232
val Inr_eq = thm "Inr_eq";
paulson@15391
   233
val InlI = thm "InlI";
paulson@15391
   234
val InrI = thm "InrI";
paulson@15391
   235
val PlusE = thm "PlusE";
paulson@15391
   236
val sumE = thm "sumE";
paulson@15391
   237
val sum_induct = thm "sum_induct";
paulson@15391
   238
val Part_eqI = thm "Part_eqI";
paulson@15391
   239
val PartI = thm "PartI";
paulson@15391
   240
val PartE = thm "PartE";
paulson@15391
   241
val Part_subset = thm "Part_subset";
paulson@15391
   242
val Part_mono = thm "Part_mono";
paulson@15391
   243
val PartD1 = thm "PartD1";
paulson@15391
   244
val Part_id = thm "Part_id";
paulson@15391
   245
val Part_Int = thm "Part_Int";
paulson@15391
   246
val Part_Collect = thm "Part_Collect";
paulson@15391
   247
paulson@15391
   248
val basic_monos = thms "basic_monos";
paulson@15391
   249
*}
paulson@15391
   250
nipkow@10213
   251
end