src/HOL/Sum_Type.thy
author blanchet
Wed Feb 12 08:35:56 2014 +0100 (2014-02-12)
changeset 55393 ce5cebfaedda
parent 53010 ec5e6f69bd65
child 55403 677569668824
permissions -rw-r--r--
se 'wrap_free_constructors' to register 'sum' , 'prod', 'unit', 'bool' with their discriminators/selectors
* * *
cleaner simp/iff sets
nipkow@10213
     1
(*  Title:      HOL/Sum_Type.thy
nipkow@10213
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
nipkow@10213
     3
    Copyright   1992  University of Cambridge
nipkow@10213
     4
*)
nipkow@10213
     5
paulson@15391
     6
header{*The Disjoint Sum of Two Types*}
nipkow@10213
     7
paulson@15391
     8
theory Sum_Type
haftmann@33961
     9
imports Typedef Inductive Fun
paulson@15391
    10
begin
paulson@15391
    11
haftmann@33962
    12
subsection {* Construction of the sum type and its basic abstract operations *}
nipkow@10213
    13
haftmann@33962
    14
definition Inl_Rep :: "'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool \<Rightarrow> bool" where
haftmann@33962
    15
  "Inl_Rep a x y p \<longleftrightarrow> x = a \<and> p"
nipkow@10213
    16
haftmann@33962
    17
definition Inr_Rep :: "'b \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool \<Rightarrow> bool" where
haftmann@33962
    18
  "Inr_Rep b x y p \<longleftrightarrow> y = b \<and> \<not> p"
paulson@15391
    19
wenzelm@45694
    20
definition "sum = {f. (\<exists>a. f = Inl_Rep (a::'a)) \<or> (\<exists>b. f = Inr_Rep (b::'b))}"
wenzelm@45694
    21
wenzelm@49834
    22
typedef ('a, 'b) sum (infixr "+" 10) = "sum :: ('a => 'b => bool => bool) set"
wenzelm@45694
    23
  unfolding sum_def by auto
nipkow@10213
    24
haftmann@37388
    25
lemma Inl_RepI: "Inl_Rep a \<in> sum"
haftmann@37388
    26
  by (auto simp add: sum_def)
paulson@15391
    27
haftmann@37388
    28
lemma Inr_RepI: "Inr_Rep b \<in> sum"
haftmann@37388
    29
  by (auto simp add: sum_def)
paulson@15391
    30
haftmann@37388
    31
lemma inj_on_Abs_sum: "A \<subseteq> sum \<Longrightarrow> inj_on Abs_sum A"
haftmann@37388
    32
  by (rule inj_on_inverseI, rule Abs_sum_inverse) auto
paulson@15391
    33
haftmann@33962
    34
lemma Inl_Rep_inject: "inj_on Inl_Rep A"
haftmann@33962
    35
proof (rule inj_onI)
haftmann@33962
    36
  show "\<And>a c. Inl_Rep a = Inl_Rep c \<Longrightarrow> a = c"
nipkow@39302
    37
    by (auto simp add: Inl_Rep_def fun_eq_iff)
haftmann@33962
    38
qed
paulson@15391
    39
haftmann@33962
    40
lemma Inr_Rep_inject: "inj_on Inr_Rep A"
haftmann@33962
    41
proof (rule inj_onI)
haftmann@33962
    42
  show "\<And>b d. Inr_Rep b = Inr_Rep d \<Longrightarrow> b = d"
nipkow@39302
    43
    by (auto simp add: Inr_Rep_def fun_eq_iff)
haftmann@33962
    44
qed
paulson@15391
    45
haftmann@33962
    46
lemma Inl_Rep_not_Inr_Rep: "Inl_Rep a \<noteq> Inr_Rep b"
nipkow@39302
    47
  by (auto simp add: Inl_Rep_def Inr_Rep_def fun_eq_iff)
paulson@15391
    48
haftmann@33962
    49
definition Inl :: "'a \<Rightarrow> 'a + 'b" where
haftmann@37388
    50
  "Inl = Abs_sum \<circ> Inl_Rep"
paulson@15391
    51
haftmann@33962
    52
definition Inr :: "'b \<Rightarrow> 'a + 'b" where
haftmann@37388
    53
  "Inr = Abs_sum \<circ> Inr_Rep"
paulson@15391
    54
huffman@29025
    55
lemma inj_Inl [simp]: "inj_on Inl A"
haftmann@37388
    56
by (auto simp add: Inl_def intro!: comp_inj_on Inl_Rep_inject inj_on_Abs_sum Inl_RepI)
huffman@29025
    57
haftmann@33962
    58
lemma Inl_inject: "Inl x = Inl y \<Longrightarrow> x = y"
haftmann@33962
    59
using inj_Inl by (rule injD)
paulson@15391
    60
huffman@29025
    61
lemma inj_Inr [simp]: "inj_on Inr A"
haftmann@37388
    62
by (auto simp add: Inr_def intro!: comp_inj_on Inr_Rep_inject inj_on_Abs_sum Inr_RepI)
paulson@15391
    63
haftmann@33962
    64
lemma Inr_inject: "Inr x = Inr y \<Longrightarrow> x = y"
haftmann@33962
    65
using inj_Inr by (rule injD)
paulson@15391
    66
haftmann@33962
    67
lemma Inl_not_Inr: "Inl a \<noteq> Inr b"
haftmann@33962
    68
proof -
haftmann@37388
    69
  from Inl_RepI [of a] Inr_RepI [of b] have "{Inl_Rep a, Inr_Rep b} \<subseteq> sum" by auto
haftmann@37388
    70
  with inj_on_Abs_sum have "inj_on Abs_sum {Inl_Rep a, Inr_Rep b}" .
haftmann@37388
    71
  with Inl_Rep_not_Inr_Rep [of a b] inj_on_contraD have "Abs_sum (Inl_Rep a) \<noteq> Abs_sum (Inr_Rep b)" by auto
haftmann@33962
    72
  then show ?thesis by (simp add: Inl_def Inr_def)
haftmann@33962
    73
qed
paulson@15391
    74
haftmann@33962
    75
lemma Inr_not_Inl: "Inr b \<noteq> Inl a" 
haftmann@33962
    76
  using Inl_not_Inr by (rule not_sym)
paulson@15391
    77
paulson@15391
    78
lemma sumE: 
haftmann@33962
    79
  assumes "\<And>x::'a. s = Inl x \<Longrightarrow> P"
haftmann@33962
    80
    and "\<And>y::'b. s = Inr y \<Longrightarrow> P"
haftmann@33962
    81
  shows P
haftmann@37388
    82
proof (rule Abs_sum_cases [of s])
haftmann@33962
    83
  fix f 
haftmann@37388
    84
  assume "s = Abs_sum f" and "f \<in> sum"
haftmann@37388
    85
  with assms show P by (auto simp add: sum_def Inl_def Inr_def)
haftmann@33962
    86
qed
haftmann@33961
    87
blanchet@55393
    88
wrap_free_constructors [Inl, Inr] sum_case [isl] [[projl], [projr]]
blanchet@55393
    89
by (erule sumE, assumption) (auto dest: Inl_inject Inr_inject simp add: Inl_not_Inr)
blanchet@55393
    90
blanchet@55393
    91
-- {* Avoid name clashes by prefixing the output of @{text rep_datatype} with @{text old}. *}
blanchet@55393
    92
setup {* Sign.mandatory_path "old" *}
blanchet@55393
    93
haftmann@37678
    94
rep_datatype Inl Inr
haftmann@33961
    95
proof -
haftmann@33961
    96
  fix P
haftmann@33961
    97
  fix s :: "'a + 'b"
haftmann@33961
    98
  assume x: "\<And>x\<Colon>'a. P (Inl x)" and y: "\<And>y\<Colon>'b. P (Inr y)"
haftmann@33961
    99
  then show "P s" by (auto intro: sumE [of s])
haftmann@33962
   100
qed (auto dest: Inl_inject Inr_inject simp add: Inl_not_Inr)
haftmann@33962
   101
blanchet@55393
   102
setup {* Sign.parent_path *}
blanchet@55393
   103
blanchet@55393
   104
-- {* But erase the prefix for properties that are not generated by @{text wrap_free_constructors}. *}
blanchet@55393
   105
setup {* Sign.mandatory_path "sum" *}
blanchet@55393
   106
blanchet@55393
   107
declare
blanchet@55393
   108
  old.sum.inject[iff del]
blanchet@55393
   109
  old.sum.distinct(1)[simp del, induct_simp del]
blanchet@55393
   110
  old.sum.cases[simp del]
blanchet@55393
   111
blanchet@55393
   112
lemmas induct = old.sum.induct
blanchet@55393
   113
lemmas inducts = old.sum.inducts
blanchet@55393
   114
lemmas recs = old.sum.recs
blanchet@55393
   115
lemmas cases = sum.case
blanchet@55393
   116
lemmas simps = sum.inject sum.distinct sum.case old.sum.recs
blanchet@55393
   117
blanchet@55393
   118
setup {* Sign.parent_path *}
blanchet@55393
   119
haftmann@40610
   120
primrec sum_map :: "('a \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> 'a + 'b \<Rightarrow> 'c + 'd" where
haftmann@40610
   121
  "sum_map f1 f2 (Inl a) = Inl (f1 a)"
haftmann@40610
   122
| "sum_map f1 f2 (Inr a) = Inr (f2 a)"
haftmann@40610
   123
haftmann@41505
   124
enriched_type sum_map: sum_map proof -
haftmann@41372
   125
  fix f g h i
haftmann@41372
   126
  show "sum_map f g \<circ> sum_map h i = sum_map (f \<circ> h) (g \<circ> i)"
haftmann@41372
   127
  proof
haftmann@41372
   128
    fix s
haftmann@41372
   129
    show "(sum_map f g \<circ> sum_map h i) s = sum_map (f \<circ> h) (g \<circ> i) s"
haftmann@41372
   130
      by (cases s) simp_all
haftmann@41372
   131
  qed
haftmann@40610
   132
next
haftmann@40610
   133
  fix s
haftmann@41372
   134
  show "sum_map id id = id"
haftmann@41372
   135
  proof
haftmann@41372
   136
    fix s
haftmann@41372
   137
    show "sum_map id id s = id s" 
haftmann@41372
   138
      by (cases s) simp_all
haftmann@41372
   139
  qed
haftmann@40610
   140
qed
haftmann@40610
   141
kuncar@53010
   142
lemma split_sum_all: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P (Inl x)) \<and> (\<forall>x. P (Inr x))"
kuncar@53010
   143
  by (auto intro: sum.induct)
kuncar@53010
   144
kuncar@53010
   145
lemma split_sum_ex: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P (Inl x)) \<or> (\<exists>x. P (Inr x))"
kuncar@53010
   146
using split_sum_all[of "\<lambda>x. \<not>P x"] by blast
haftmann@33961
   147
haftmann@33962
   148
subsection {* Projections *}
haftmann@33962
   149
haftmann@33962
   150
lemma sum_case_KK [simp]: "sum_case (\<lambda>x. a) (\<lambda>x. a) = (\<lambda>x. a)"
haftmann@33961
   151
  by (rule ext) (simp split: sum.split)
haftmann@33961
   152
haftmann@33962
   153
lemma surjective_sum: "sum_case (\<lambda>x::'a. f (Inl x)) (\<lambda>y::'b. f (Inr y)) = f"
haftmann@33962
   154
proof
haftmann@33962
   155
  fix s :: "'a + 'b"
haftmann@33962
   156
  show "(case s of Inl (x\<Colon>'a) \<Rightarrow> f (Inl x) | Inr (y\<Colon>'b) \<Rightarrow> f (Inr y)) = f s"
haftmann@33962
   157
    by (cases s) simp_all
haftmann@33962
   158
qed
haftmann@33961
   159
haftmann@33962
   160
lemma sum_case_inject:
haftmann@33962
   161
  assumes a: "sum_case f1 f2 = sum_case g1 g2"
haftmann@33962
   162
  assumes r: "f1 = g1 \<Longrightarrow> f2 = g2 \<Longrightarrow> P"
haftmann@33962
   163
  shows P
haftmann@33962
   164
proof (rule r)
haftmann@33962
   165
  show "f1 = g1" proof
haftmann@33962
   166
    fix x :: 'a
haftmann@33962
   167
    from a have "sum_case f1 f2 (Inl x) = sum_case g1 g2 (Inl x)" by simp
haftmann@33962
   168
    then show "f1 x = g1 x" by simp
haftmann@33962
   169
  qed
haftmann@33962
   170
  show "f2 = g2" proof
haftmann@33962
   171
    fix y :: 'b
haftmann@33962
   172
    from a have "sum_case f1 f2 (Inr y) = sum_case g1 g2 (Inr y)" by simp
haftmann@33962
   173
    then show "f2 y = g2 y" by simp
haftmann@33962
   174
  qed
haftmann@33962
   175
qed
haftmann@33962
   176
haftmann@33962
   177
primrec Suml :: "('a \<Rightarrow> 'c) \<Rightarrow> 'a + 'b \<Rightarrow> 'c" where
haftmann@33962
   178
  "Suml f (Inl x) = f x"
haftmann@33962
   179
haftmann@33962
   180
primrec Sumr :: "('b \<Rightarrow> 'c) \<Rightarrow> 'a + 'b \<Rightarrow> 'c" where
haftmann@33962
   181
  "Sumr f (Inr x) = f x"
haftmann@33962
   182
haftmann@33962
   183
lemma Suml_inject:
haftmann@33962
   184
  assumes "Suml f = Suml g" shows "f = g"
haftmann@33962
   185
proof
haftmann@33962
   186
  fix x :: 'a
haftmann@33962
   187
  let ?s = "Inl x \<Colon> 'a + 'b"
haftmann@33962
   188
  from assms have "Suml f ?s = Suml g ?s" by simp
haftmann@33962
   189
  then show "f x = g x" by simp
haftmann@33961
   190
qed
haftmann@33961
   191
haftmann@33962
   192
lemma Sumr_inject:
haftmann@33962
   193
  assumes "Sumr f = Sumr g" shows "f = g"
haftmann@33962
   194
proof
haftmann@33962
   195
  fix x :: 'b
haftmann@33962
   196
  let ?s = "Inr x \<Colon> 'a + 'b"
haftmann@33962
   197
  from assms have "Sumr f ?s = Sumr g ?s" by simp
haftmann@33962
   198
  then show "f x = g x" by simp
haftmann@33962
   199
qed
haftmann@33961
   200
haftmann@33995
   201
haftmann@33962
   202
subsection {* The Disjoint Sum of Sets *}
haftmann@33961
   203
haftmann@33962
   204
definition Plus :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a + 'b) set" (infixr "<+>" 65) where
haftmann@33962
   205
  "A <+> B = Inl ` A \<union> Inr ` B"
haftmann@33962
   206
nipkow@40271
   207
hide_const (open) Plus --"Valuable identifier"
nipkow@40271
   208
haftmann@33962
   209
lemma InlI [intro!]: "a \<in> A \<Longrightarrow> Inl a \<in> A <+> B"
haftmann@33962
   210
by (simp add: Plus_def)
haftmann@33961
   211
haftmann@33962
   212
lemma InrI [intro!]: "b \<in> B \<Longrightarrow> Inr b \<in> A <+> B"
haftmann@33962
   213
by (simp add: Plus_def)
haftmann@33961
   214
haftmann@33962
   215
text {* Exhaustion rule for sums, a degenerate form of induction *}
haftmann@33962
   216
haftmann@33962
   217
lemma PlusE [elim!]: 
haftmann@33962
   218
  "u \<in> A <+> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> u = Inl x \<Longrightarrow> P) \<Longrightarrow> (\<And>y. y \<in> B \<Longrightarrow> u = Inr y \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@33962
   219
by (auto simp add: Plus_def)
haftmann@33961
   220
haftmann@33962
   221
lemma Plus_eq_empty_conv [simp]: "A <+> B = {} \<longleftrightarrow> A = {} \<and> B = {}"
haftmann@33962
   222
by auto
haftmann@33961
   223
haftmann@33962
   224
lemma UNIV_Plus_UNIV [simp]: "UNIV <+> UNIV = UNIV"
nipkow@39302
   225
proof (rule set_eqI)
haftmann@33962
   226
  fix u :: "'a + 'b"
haftmann@33962
   227
  show "u \<in> UNIV <+> UNIV \<longleftrightarrow> u \<in> UNIV" by (cases u) auto
haftmann@33962
   228
qed
haftmann@33961
   229
haftmann@49948
   230
lemma UNIV_sum:
haftmann@49948
   231
  "UNIV = Inl ` UNIV \<union> Inr ` UNIV"
haftmann@49948
   232
proof -
haftmann@49948
   233
  { fix x :: "'a + 'b"
haftmann@49948
   234
    assume "x \<notin> range Inr"
haftmann@49948
   235
    then have "x \<in> range Inl"
haftmann@49948
   236
    by (cases x) simp_all
haftmann@49948
   237
  } then show ?thesis by auto
haftmann@49948
   238
qed
haftmann@49948
   239
blanchet@55393
   240
hide_const (open) Suml Sumr sum
huffman@45204
   241
nipkow@10213
   242
end