src/HOL/Sum_Type.thy
 author wenzelm Fri Dec 17 17:43:54 2010 +0100 (2010-12-17) changeset 41229 d797baa3d57c parent 40968 a6fcd305f7dc child 41372 551eb49a6e91 permissions -rw-r--r--
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
 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 \ 'a \ 'b \ bool \ bool" where ``` haftmann@33962 ` 15` ``` "Inl_Rep a x y p \ x = a \ p" ``` nipkow@10213 ` 16` haftmann@33962 ` 17` ```definition Inr_Rep :: "'b \ 'a \ 'b \ bool \ bool" where ``` haftmann@33962 ` 18` ``` "Inr_Rep b x y p \ y = b \ \ p" ``` paulson@15391 ` 19` haftmann@37678 ` 20` ```typedef ('a, 'b) sum (infixr "+" 10) = "{f. (\a. f = Inl_Rep (a::'a)) \ (\b. f = Inr_Rep (b::'b))}" ``` paulson@15391 ` 21` ``` by auto ``` nipkow@10213 ` 22` haftmann@37388 ` 23` ```lemma Inl_RepI: "Inl_Rep a \ sum" ``` haftmann@37388 ` 24` ``` by (auto simp add: sum_def) ``` paulson@15391 ` 25` haftmann@37388 ` 26` ```lemma Inr_RepI: "Inr_Rep b \ sum" ``` haftmann@37388 ` 27` ``` by (auto simp add: sum_def) ``` paulson@15391 ` 28` haftmann@37388 ` 29` ```lemma inj_on_Abs_sum: "A \ sum \ inj_on Abs_sum A" ``` haftmann@37388 ` 30` ``` by (rule inj_on_inverseI, rule Abs_sum_inverse) auto ``` paulson@15391 ` 31` haftmann@33962 ` 32` ```lemma Inl_Rep_inject: "inj_on Inl_Rep A" ``` haftmann@33962 ` 33` ```proof (rule inj_onI) ``` haftmann@33962 ` 34` ``` show "\a c. Inl_Rep a = Inl_Rep c \ a = c" ``` nipkow@39302 ` 35` ``` by (auto simp add: Inl_Rep_def fun_eq_iff) ``` haftmann@33962 ` 36` ```qed ``` paulson@15391 ` 37` haftmann@33962 ` 38` ```lemma Inr_Rep_inject: "inj_on Inr_Rep A" ``` haftmann@33962 ` 39` ```proof (rule inj_onI) ``` haftmann@33962 ` 40` ``` show "\b d. Inr_Rep b = Inr_Rep d \ b = d" ``` nipkow@39302 ` 41` ``` by (auto simp add: Inr_Rep_def fun_eq_iff) ``` haftmann@33962 ` 42` ```qed ``` paulson@15391 ` 43` haftmann@33962 ` 44` ```lemma Inl_Rep_not_Inr_Rep: "Inl_Rep a \ Inr_Rep b" ``` nipkow@39302 ` 45` ``` by (auto simp add: Inl_Rep_def Inr_Rep_def fun_eq_iff) ``` paulson@15391 ` 46` haftmann@33962 ` 47` ```definition Inl :: "'a \ 'a + 'b" where ``` haftmann@37388 ` 48` ``` "Inl = Abs_sum \ Inl_Rep" ``` paulson@15391 ` 49` haftmann@33962 ` 50` ```definition Inr :: "'b \ 'a + 'b" where ``` haftmann@37388 ` 51` ``` "Inr = Abs_sum \ Inr_Rep" ``` paulson@15391 ` 52` huffman@29025 ` 53` ```lemma inj_Inl [simp]: "inj_on Inl A" ``` haftmann@37388 ` 54` ```by (auto simp add: Inl_def intro!: comp_inj_on Inl_Rep_inject inj_on_Abs_sum Inl_RepI) ``` huffman@29025 ` 55` haftmann@33962 ` 56` ```lemma Inl_inject: "Inl x = Inl y \ x = y" ``` haftmann@33962 ` 57` ```using inj_Inl by (rule injD) ``` paulson@15391 ` 58` huffman@29025 ` 59` ```lemma inj_Inr [simp]: "inj_on Inr A" ``` haftmann@37388 ` 60` ```by (auto simp add: Inr_def intro!: comp_inj_on Inr_Rep_inject inj_on_Abs_sum Inr_RepI) ``` paulson@15391 ` 61` haftmann@33962 ` 62` ```lemma Inr_inject: "Inr x = Inr y \ x = y" ``` haftmann@33962 ` 63` ```using inj_Inr by (rule injD) ``` paulson@15391 ` 64` haftmann@33962 ` 65` ```lemma Inl_not_Inr: "Inl a \ Inr b" ``` haftmann@33962 ` 66` ```proof - ``` haftmann@37388 ` 67` ``` from Inl_RepI [of a] Inr_RepI [of b] have "{Inl_Rep a, Inr_Rep b} \ sum" by auto ``` haftmann@37388 ` 68` ``` with inj_on_Abs_sum have "inj_on Abs_sum {Inl_Rep a, Inr_Rep b}" . ``` haftmann@37388 ` 69` ``` with Inl_Rep_not_Inr_Rep [of a b] inj_on_contraD have "Abs_sum (Inl_Rep a) \ Abs_sum (Inr_Rep b)" by auto ``` haftmann@33962 ` 70` ``` then show ?thesis by (simp add: Inl_def Inr_def) ``` haftmann@33962 ` 71` ```qed ``` paulson@15391 ` 72` haftmann@33962 ` 73` ```lemma Inr_not_Inl: "Inr b \ Inl a" ``` haftmann@33962 ` 74` ``` using Inl_not_Inr by (rule not_sym) ``` paulson@15391 ` 75` paulson@15391 ` 76` ```lemma sumE: ``` haftmann@33962 ` 77` ``` assumes "\x::'a. s = Inl x \ P" ``` haftmann@33962 ` 78` ``` and "\y::'b. s = Inr y \ P" ``` haftmann@33962 ` 79` ``` shows P ``` haftmann@37388 ` 80` ```proof (rule Abs_sum_cases [of s]) ``` haftmann@33962 ` 81` ``` fix f ``` haftmann@37388 ` 82` ``` assume "s = Abs_sum f" and "f \ sum" ``` haftmann@37388 ` 83` ``` with assms show P by (auto simp add: sum_def Inl_def Inr_def) ``` haftmann@33962 ` 84` ```qed ``` haftmann@33961 ` 85` haftmann@37678 ` 86` ```rep_datatype Inl Inr ``` haftmann@33961 ` 87` ```proof - ``` haftmann@33961 ` 88` ``` fix P ``` haftmann@33961 ` 89` ``` fix s :: "'a + 'b" ``` haftmann@33961 ` 90` ``` assume x: "\x\'a. P (Inl x)" and y: "\y\'b. P (Inr y)" ``` haftmann@33961 ` 91` ``` then show "P s" by (auto intro: sumE [of s]) ``` haftmann@33962 ` 92` ```qed (auto dest: Inl_inject Inr_inject simp add: Inl_not_Inr) ``` haftmann@33962 ` 93` haftmann@40610 ` 94` ```primrec sum_map :: "('a \ 'c) \ ('b \ 'd) \ 'a + 'b \ 'c + 'd" where ``` haftmann@40610 ` 95` ``` "sum_map f1 f2 (Inl a) = Inl (f1 a)" ``` haftmann@40610 ` 96` ```| "sum_map f1 f2 (Inr a) = Inr (f2 a)" ``` haftmann@40610 ` 97` haftmann@40968 ` 98` ```type_lifting sum_map proof - ``` haftmann@40610 ` 99` ``` fix f g h i s ``` haftmann@40610 ` 100` ``` show "sum_map f g (sum_map h i s) = sum_map (\x. f (h x)) (\x. g (i x)) s" ``` haftmann@40610 ` 101` ``` by (cases s) simp_all ``` haftmann@40610 ` 102` ```next ``` haftmann@40610 ` 103` ``` fix s ``` haftmann@40610 ` 104` ``` show "sum_map (\x. x) (\x. x) s = s" ``` haftmann@40610 ` 105` ``` by (cases s) simp_all ``` haftmann@40610 ` 106` ```qed ``` haftmann@40610 ` 107` haftmann@33961 ` 108` haftmann@33962 ` 109` ```subsection {* Projections *} ``` haftmann@33962 ` 110` haftmann@33962 ` 111` ```lemma sum_case_KK [simp]: "sum_case (\x. a) (\x. a) = (\x. a)" ``` haftmann@33961 ` 112` ``` by (rule ext) (simp split: sum.split) ``` haftmann@33961 ` 113` haftmann@33962 ` 114` ```lemma surjective_sum: "sum_case (\x::'a. f (Inl x)) (\y::'b. f (Inr y)) = f" ``` haftmann@33962 ` 115` ```proof ``` haftmann@33962 ` 116` ``` fix s :: "'a + 'b" ``` haftmann@33962 ` 117` ``` show "(case s of Inl (x\'a) \ f (Inl x) | Inr (y\'b) \ f (Inr y)) = f s" ``` haftmann@33962 ` 118` ``` by (cases s) simp_all ``` haftmann@33962 ` 119` ```qed ``` haftmann@33961 ` 120` haftmann@33962 ` 121` ```lemma sum_case_inject: ``` haftmann@33962 ` 122` ``` assumes a: "sum_case f1 f2 = sum_case g1 g2" ``` haftmann@33962 ` 123` ``` assumes r: "f1 = g1 \ f2 = g2 \ P" ``` haftmann@33962 ` 124` ``` shows P ``` haftmann@33962 ` 125` ```proof (rule r) ``` haftmann@33962 ` 126` ``` show "f1 = g1" proof ``` haftmann@33962 ` 127` ``` fix x :: 'a ``` haftmann@33962 ` 128` ``` from a have "sum_case f1 f2 (Inl x) = sum_case g1 g2 (Inl x)" by simp ``` haftmann@33962 ` 129` ``` then show "f1 x = g1 x" by simp ``` haftmann@33962 ` 130` ``` qed ``` haftmann@33962 ` 131` ``` show "f2 = g2" proof ``` haftmann@33962 ` 132` ``` fix y :: 'b ``` haftmann@33962 ` 133` ``` from a have "sum_case f1 f2 (Inr y) = sum_case g1 g2 (Inr y)" by simp ``` haftmann@33962 ` 134` ``` then show "f2 y = g2 y" by simp ``` haftmann@33962 ` 135` ``` qed ``` haftmann@33962 ` 136` ```qed ``` haftmann@33962 ` 137` haftmann@33962 ` 138` ```lemma sum_case_weak_cong: ``` haftmann@33962 ` 139` ``` "s = t \ sum_case f g s = sum_case f g t" ``` haftmann@33961 ` 140` ``` -- {* Prevents simplification of @{text f} and @{text g}: much faster. *} ``` haftmann@33961 ` 141` ``` by simp ``` haftmann@33961 ` 142` haftmann@33962 ` 143` ```primrec Projl :: "'a + 'b \ 'a" where ``` haftmann@33962 ` 144` ``` Projl_Inl: "Projl (Inl x) = x" ``` haftmann@33962 ` 145` haftmann@33962 ` 146` ```primrec Projr :: "'a + 'b \ 'b" where ``` haftmann@33962 ` 147` ``` Projr_Inr: "Projr (Inr x) = x" ``` haftmann@33962 ` 148` haftmann@33962 ` 149` ```primrec Suml :: "('a \ 'c) \ 'a + 'b \ 'c" where ``` haftmann@33962 ` 150` ``` "Suml f (Inl x) = f x" ``` haftmann@33962 ` 151` haftmann@33962 ` 152` ```primrec Sumr :: "('b \ 'c) \ 'a + 'b \ 'c" where ``` haftmann@33962 ` 153` ``` "Sumr f (Inr x) = f x" ``` haftmann@33962 ` 154` haftmann@33962 ` 155` ```lemma Suml_inject: ``` haftmann@33962 ` 156` ``` assumes "Suml f = Suml g" shows "f = g" ``` haftmann@33962 ` 157` ```proof ``` haftmann@33962 ` 158` ``` fix x :: 'a ``` haftmann@33962 ` 159` ``` let ?s = "Inl x \ 'a + 'b" ``` haftmann@33962 ` 160` ``` from assms have "Suml f ?s = Suml g ?s" by simp ``` haftmann@33962 ` 161` ``` then show "f x = g x" by simp ``` haftmann@33961 ` 162` ```qed ``` haftmann@33961 ` 163` haftmann@33962 ` 164` ```lemma Sumr_inject: ``` haftmann@33962 ` 165` ``` assumes "Sumr f = Sumr g" shows "f = g" ``` haftmann@33962 ` 166` ```proof ``` haftmann@33962 ` 167` ``` fix x :: 'b ``` haftmann@33962 ` 168` ``` let ?s = "Inr x \ 'a + 'b" ``` haftmann@33962 ` 169` ``` from assms have "Sumr f ?s = Sumr g ?s" by simp ``` haftmann@33962 ` 170` ``` then show "f x = g x" by simp ``` haftmann@33962 ` 171` ```qed ``` haftmann@33961 ` 172` haftmann@33995 ` 173` haftmann@33962 ` 174` ```subsection {* The Disjoint Sum of Sets *} ``` haftmann@33961 ` 175` haftmann@33962 ` 176` ```definition Plus :: "'a set \ 'b set \ ('a + 'b) set" (infixr "<+>" 65) where ``` haftmann@33962 ` 177` ``` "A <+> B = Inl ` A \ Inr ` B" ``` haftmann@33962 ` 178` nipkow@40271 ` 179` ```hide_const (open) Plus --"Valuable identifier" ``` nipkow@40271 ` 180` haftmann@33962 ` 181` ```lemma InlI [intro!]: "a \ A \ Inl a \ A <+> B" ``` haftmann@33962 ` 182` ```by (simp add: Plus_def) ``` haftmann@33961 ` 183` haftmann@33962 ` 184` ```lemma InrI [intro!]: "b \ B \ Inr b \ A <+> B" ``` haftmann@33962 ` 185` ```by (simp add: Plus_def) ``` haftmann@33961 ` 186` haftmann@33962 ` 187` ```text {* Exhaustion rule for sums, a degenerate form of induction *} ``` haftmann@33962 ` 188` haftmann@33962 ` 189` ```lemma PlusE [elim!]: ``` haftmann@33962 ` 190` ``` "u \ A <+> B \ (\x. x \ A \ u = Inl x \ P) \ (\y. y \ B \ u = Inr y \ P) \ P" ``` haftmann@33962 ` 191` ```by (auto simp add: Plus_def) ``` haftmann@33961 ` 192` haftmann@33962 ` 193` ```lemma Plus_eq_empty_conv [simp]: "A <+> B = {} \ A = {} \ B = {}" ``` haftmann@33962 ` 194` ```by auto ``` haftmann@33961 ` 195` haftmann@33962 ` 196` ```lemma UNIV_Plus_UNIV [simp]: "UNIV <+> UNIV = UNIV" ``` nipkow@39302 ` 197` ```proof (rule set_eqI) ``` haftmann@33962 ` 198` ``` fix u :: "'a + 'b" ``` haftmann@33962 ` 199` ``` show "u \ UNIV <+> UNIV \ u \ UNIV" by (cases u) auto ``` haftmann@33962 ` 200` ```qed ``` haftmann@33961 ` 201` wenzelm@36176 ` 202` ```hide_const (open) Suml Sumr Projl Projr ``` haftmann@33961 ` 203` nipkow@10213 ` 204` ```end ```