src/HOL/Datatype.thy
author haftmann
Thu Nov 12 15:49:01 2009 +0100 (2009-11-12)
changeset 33633 9f7280e0c231
parent 30235 58d147683393
child 33959 2afc55e8ed27
permissions -rw-r--r--
explicit code lemmas produce nices code
     1 (*  Title:      HOL/Datatype.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     4 
     5 Could <*> be generalized to a general summation (Sigma)?
     6 *)
     7 
     8 header {* Analogues of the Cartesian Product and Disjoint Sum for Datatypes *}
     9 
    10 theory Datatype
    11 imports Nat Product_Type
    12 begin
    13 
    14 typedef (Node)
    15   ('a,'b) node = "{p. EX f x k. p = (f::nat=>'b+nat, x::'a+nat) & f k = Inr 0}"
    16     --{*it is a subtype of @{text "(nat=>'b+nat) * ('a+nat)"}*}
    17   by auto
    18 
    19 text{*Datatypes will be represented by sets of type @{text node}*}
    20 
    21 types 'a item        = "('a, unit) node set"
    22       ('a, 'b) dtree = "('a, 'b) node set"
    23 
    24 consts
    25   Push      :: "[('b + nat), nat => ('b + nat)] => (nat => ('b + nat))"
    26 
    27   Push_Node :: "[('b + nat), ('a, 'b) node] => ('a, 'b) node"
    28   ndepth    :: "('a, 'b) node => nat"
    29 
    30   Atom      :: "('a + nat) => ('a, 'b) dtree"
    31   Leaf      :: "'a => ('a, 'b) dtree"
    32   Numb      :: "nat => ('a, 'b) dtree"
    33   Scons     :: "[('a, 'b) dtree, ('a, 'b) dtree] => ('a, 'b) dtree"
    34   In0       :: "('a, 'b) dtree => ('a, 'b) dtree"
    35   In1       :: "('a, 'b) dtree => ('a, 'b) dtree"
    36   Lim       :: "('b => ('a, 'b) dtree) => ('a, 'b) dtree"
    37 
    38   ntrunc    :: "[nat, ('a, 'b) dtree] => ('a, 'b) dtree"
    39 
    40   uprod     :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set"
    41   usum      :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set"
    42 
    43   Split     :: "[[('a, 'b) dtree, ('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c"
    44   Case      :: "[[('a, 'b) dtree]=>'c, [('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c"
    45 
    46   dprod     :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set]
    47                 => (('a, 'b) dtree * ('a, 'b) dtree)set"
    48   dsum      :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set]
    49                 => (('a, 'b) dtree * ('a, 'b) dtree)set"
    50 
    51 
    52 defs
    53 
    54   Push_Node_def:  "Push_Node == (%n x. Abs_Node (apfst (Push n) (Rep_Node x)))"
    55 
    56   (*crude "lists" of nats -- needed for the constructions*)
    57   Push_def:   "Push == (%b h. nat_case b h)"
    58 
    59   (** operations on S-expressions -- sets of nodes **)
    60 
    61   (*S-expression constructors*)
    62   Atom_def:   "Atom == (%x. {Abs_Node((%k. Inr 0, x))})"
    63   Scons_def:  "Scons M N == (Push_Node (Inr 1) ` M) Un (Push_Node (Inr (Suc 1)) ` N)"
    64 
    65   (*Leaf nodes, with arbitrary or nat labels*)
    66   Leaf_def:   "Leaf == Atom o Inl"
    67   Numb_def:   "Numb == Atom o Inr"
    68 
    69   (*Injections of the "disjoint sum"*)
    70   In0_def:    "In0(M) == Scons (Numb 0) M"
    71   In1_def:    "In1(M) == Scons (Numb 1) M"
    72 
    73   (*Function spaces*)
    74   Lim_def: "Lim f == Union {z. ? x. z = Push_Node (Inl x) ` (f x)}"
    75 
    76   (*the set of nodes with depth less than k*)
    77   ndepth_def: "ndepth(n) == (%(f,x). LEAST k. f k = Inr 0) (Rep_Node n)"
    78   ntrunc_def: "ntrunc k N == {n. n:N & ndepth(n)<k}"
    79 
    80   (*products and sums for the "universe"*)
    81   uprod_def:  "uprod A B == UN x:A. UN y:B. { Scons x y }"
    82   usum_def:   "usum A B == In0`A Un In1`B"
    83 
    84   (*the corresponding eliminators*)
    85   Split_def:  "Split c M == THE u. EX x y. M = Scons x y & u = c x y"
    86 
    87   Case_def:   "Case c d M == THE u.  (EX x . M = In0(x) & u = c(x))
    88                                   | (EX y . M = In1(y) & u = d(y))"
    89 
    90 
    91   (** equality for the "universe" **)
    92 
    93   dprod_def:  "dprod r s == UN (x,x'):r. UN (y,y'):s. {(Scons x y, Scons x' y')}"
    94 
    95   dsum_def:   "dsum r s == (UN (x,x'):r. {(In0(x),In0(x'))}) Un
    96                           (UN (y,y'):s. {(In1(y),In1(y'))})"
    97 
    98 
    99 
   100 lemma apfst_convE: 
   101     "[| q = apfst f p;  !!x y. [| p = (x,y);  q = (f(x),y) |] ==> R  
   102      |] ==> R"
   103 by (force simp add: apfst_def)
   104 
   105 (** Push -- an injection, analogous to Cons on lists **)
   106 
   107 lemma Push_inject1: "Push i f = Push j g  ==> i=j"
   108 apply (simp add: Push_def expand_fun_eq) 
   109 apply (drule_tac x=0 in spec, simp) 
   110 done
   111 
   112 lemma Push_inject2: "Push i f = Push j g  ==> f=g"
   113 apply (auto simp add: Push_def expand_fun_eq) 
   114 apply (drule_tac x="Suc x" in spec, simp) 
   115 done
   116 
   117 lemma Push_inject:
   118     "[| Push i f =Push j g;  [| i=j;  f=g |] ==> P |] ==> P"
   119 by (blast dest: Push_inject1 Push_inject2) 
   120 
   121 lemma Push_neq_K0: "Push (Inr (Suc k)) f = (%z. Inr 0) ==> P"
   122 by (auto simp add: Push_def expand_fun_eq split: nat.split_asm)
   123 
   124 lemmas Abs_Node_inj = Abs_Node_inject [THEN [2] rev_iffD1, standard]
   125 
   126 
   127 (*** Introduction rules for Node ***)
   128 
   129 lemma Node_K0_I: "(%k. Inr 0, a) : Node"
   130 by (simp add: Node_def)
   131 
   132 lemma Node_Push_I: "p: Node ==> apfst (Push i) p : Node"
   133 apply (simp add: Node_def Push_def) 
   134 apply (fast intro!: apfst_conv nat_case_Suc [THEN trans])
   135 done
   136 
   137 
   138 subsection{*Freeness: Distinctness of Constructors*}
   139 
   140 (** Scons vs Atom **)
   141 
   142 lemma Scons_not_Atom [iff]: "Scons M N \<noteq> Atom(a)"
   143 apply (simp add: Atom_def Scons_def Push_Node_def One_nat_def)
   144 apply (blast intro: Node_K0_I Rep_Node [THEN Node_Push_I] 
   145          dest!: Abs_Node_inj 
   146          elim!: apfst_convE sym [THEN Push_neq_K0])  
   147 done
   148 
   149 lemmas Atom_not_Scons [iff] = Scons_not_Atom [THEN not_sym, standard]
   150 
   151 
   152 (*** Injectiveness ***)
   153 
   154 (** Atomic nodes **)
   155 
   156 lemma inj_Atom: "inj(Atom)"
   157 apply (simp add: Atom_def)
   158 apply (blast intro!: inj_onI Node_K0_I dest!: Abs_Node_inj)
   159 done
   160 lemmas Atom_inject = inj_Atom [THEN injD, standard]
   161 
   162 lemma Atom_Atom_eq [iff]: "(Atom(a)=Atom(b)) = (a=b)"
   163 by (blast dest!: Atom_inject)
   164 
   165 lemma inj_Leaf: "inj(Leaf)"
   166 apply (simp add: Leaf_def o_def)
   167 apply (rule inj_onI)
   168 apply (erule Atom_inject [THEN Inl_inject])
   169 done
   170 
   171 lemmas Leaf_inject [dest!] = inj_Leaf [THEN injD, standard]
   172 
   173 lemma inj_Numb: "inj(Numb)"
   174 apply (simp add: Numb_def o_def)
   175 apply (rule inj_onI)
   176 apply (erule Atom_inject [THEN Inr_inject])
   177 done
   178 
   179 lemmas Numb_inject [dest!] = inj_Numb [THEN injD, standard]
   180 
   181 
   182 (** Injectiveness of Push_Node **)
   183 
   184 lemma Push_Node_inject:
   185     "[| Push_Node i m =Push_Node j n;  [| i=j;  m=n |] ==> P  
   186      |] ==> P"
   187 apply (simp add: Push_Node_def)
   188 apply (erule Abs_Node_inj [THEN apfst_convE])
   189 apply (rule Rep_Node [THEN Node_Push_I])+
   190 apply (erule sym [THEN apfst_convE]) 
   191 apply (blast intro: Rep_Node_inject [THEN iffD1] trans sym elim!: Push_inject)
   192 done
   193 
   194 
   195 (** Injectiveness of Scons **)
   196 
   197 lemma Scons_inject_lemma1: "Scons M N <= Scons M' N' ==> M<=M'"
   198 apply (simp add: Scons_def One_nat_def)
   199 apply (blast dest!: Push_Node_inject)
   200 done
   201 
   202 lemma Scons_inject_lemma2: "Scons M N <= Scons M' N' ==> N<=N'"
   203 apply (simp add: Scons_def One_nat_def)
   204 apply (blast dest!: Push_Node_inject)
   205 done
   206 
   207 lemma Scons_inject1: "Scons M N = Scons M' N' ==> M=M'"
   208 apply (erule equalityE)
   209 apply (iprover intro: equalityI Scons_inject_lemma1)
   210 done
   211 
   212 lemma Scons_inject2: "Scons M N = Scons M' N' ==> N=N'"
   213 apply (erule equalityE)
   214 apply (iprover intro: equalityI Scons_inject_lemma2)
   215 done
   216 
   217 lemma Scons_inject:
   218     "[| Scons M N = Scons M' N';  [| M=M';  N=N' |] ==> P |] ==> P"
   219 by (iprover dest: Scons_inject1 Scons_inject2)
   220 
   221 lemma Scons_Scons_eq [iff]: "(Scons M N = Scons M' N') = (M=M' & N=N')"
   222 by (blast elim!: Scons_inject)
   223 
   224 (*** Distinctness involving Leaf and Numb ***)
   225 
   226 (** Scons vs Leaf **)
   227 
   228 lemma Scons_not_Leaf [iff]: "Scons M N \<noteq> Leaf(a)"
   229 by (simp add: Leaf_def o_def Scons_not_Atom)
   230 
   231 lemmas Leaf_not_Scons  [iff] = Scons_not_Leaf [THEN not_sym, standard]
   232 
   233 (** Scons vs Numb **)
   234 
   235 lemma Scons_not_Numb [iff]: "Scons M N \<noteq> Numb(k)"
   236 by (simp add: Numb_def o_def Scons_not_Atom)
   237 
   238 lemmas Numb_not_Scons [iff] = Scons_not_Numb [THEN not_sym, standard]
   239 
   240 
   241 (** Leaf vs Numb **)
   242 
   243 lemma Leaf_not_Numb [iff]: "Leaf(a) \<noteq> Numb(k)"
   244 by (simp add: Leaf_def Numb_def)
   245 
   246 lemmas Numb_not_Leaf [iff] = Leaf_not_Numb [THEN not_sym, standard]
   247 
   248 
   249 (*** ndepth -- the depth of a node ***)
   250 
   251 lemma ndepth_K0: "ndepth (Abs_Node(%k. Inr 0, x)) = 0"
   252 by (simp add: ndepth_def  Node_K0_I [THEN Abs_Node_inverse] Least_equality)
   253 
   254 lemma ndepth_Push_Node_aux:
   255      "nat_case (Inr (Suc i)) f k = Inr 0 --> Suc(LEAST x. f x = Inr 0) <= k"
   256 apply (induct_tac "k", auto)
   257 apply (erule Least_le)
   258 done
   259 
   260 lemma ndepth_Push_Node: 
   261     "ndepth (Push_Node (Inr (Suc i)) n) = Suc(ndepth(n))"
   262 apply (insert Rep_Node [of n, unfolded Node_def])
   263 apply (auto simp add: ndepth_def Push_Node_def
   264                  Rep_Node [THEN Node_Push_I, THEN Abs_Node_inverse])
   265 apply (rule Least_equality)
   266 apply (auto simp add: Push_def ndepth_Push_Node_aux)
   267 apply (erule LeastI)
   268 done
   269 
   270 
   271 (*** ntrunc applied to the various node sets ***)
   272 
   273 lemma ntrunc_0 [simp]: "ntrunc 0 M = {}"
   274 by (simp add: ntrunc_def)
   275 
   276 lemma ntrunc_Atom [simp]: "ntrunc (Suc k) (Atom a) = Atom(a)"
   277 by (auto simp add: Atom_def ntrunc_def ndepth_K0)
   278 
   279 lemma ntrunc_Leaf [simp]: "ntrunc (Suc k) (Leaf a) = Leaf(a)"
   280 by (simp add: Leaf_def o_def ntrunc_Atom)
   281 
   282 lemma ntrunc_Numb [simp]: "ntrunc (Suc k) (Numb i) = Numb(i)"
   283 by (simp add: Numb_def o_def ntrunc_Atom)
   284 
   285 lemma ntrunc_Scons [simp]: 
   286     "ntrunc (Suc k) (Scons M N) = Scons (ntrunc k M) (ntrunc k N)"
   287 by (auto simp add: Scons_def ntrunc_def One_nat_def ndepth_Push_Node) 
   288 
   289 
   290 
   291 (** Injection nodes **)
   292 
   293 lemma ntrunc_one_In0 [simp]: "ntrunc (Suc 0) (In0 M) = {}"
   294 apply (simp add: In0_def)
   295 apply (simp add: Scons_def)
   296 done
   297 
   298 lemma ntrunc_In0 [simp]: "ntrunc (Suc(Suc k)) (In0 M) = In0 (ntrunc (Suc k) M)"
   299 by (simp add: In0_def)
   300 
   301 lemma ntrunc_one_In1 [simp]: "ntrunc (Suc 0) (In1 M) = {}"
   302 apply (simp add: In1_def)
   303 apply (simp add: Scons_def)
   304 done
   305 
   306 lemma ntrunc_In1 [simp]: "ntrunc (Suc(Suc k)) (In1 M) = In1 (ntrunc (Suc k) M)"
   307 by (simp add: In1_def)
   308 
   309 
   310 subsection{*Set Constructions*}
   311 
   312 
   313 (*** Cartesian Product ***)
   314 
   315 lemma uprodI [intro!]: "[| M:A;  N:B |] ==> Scons M N : uprod A B"
   316 by (simp add: uprod_def)
   317 
   318 (*The general elimination rule*)
   319 lemma uprodE [elim!]:
   320     "[| c : uprod A B;   
   321         !!x y. [| x:A;  y:B;  c = Scons x y |] ==> P  
   322      |] ==> P"
   323 by (auto simp add: uprod_def) 
   324 
   325 
   326 (*Elimination of a pair -- introduces no eigenvariables*)
   327 lemma uprodE2: "[| Scons M N : uprod A B;  [| M:A;  N:B |] ==> P |] ==> P"
   328 by (auto simp add: uprod_def)
   329 
   330 
   331 (*** Disjoint Sum ***)
   332 
   333 lemma usum_In0I [intro]: "M:A ==> In0(M) : usum A B"
   334 by (simp add: usum_def)
   335 
   336 lemma usum_In1I [intro]: "N:B ==> In1(N) : usum A B"
   337 by (simp add: usum_def)
   338 
   339 lemma usumE [elim!]: 
   340     "[| u : usum A B;   
   341         !!x. [| x:A;  u=In0(x) |] ==> P;  
   342         !!y. [| y:B;  u=In1(y) |] ==> P  
   343      |] ==> P"
   344 by (auto simp add: usum_def)
   345 
   346 
   347 (** Injection **)
   348 
   349 lemma In0_not_In1 [iff]: "In0(M) \<noteq> In1(N)"
   350 by (auto simp add: In0_def In1_def One_nat_def)
   351 
   352 lemmas In1_not_In0 [iff] = In0_not_In1 [THEN not_sym, standard]
   353 
   354 lemma In0_inject: "In0(M) = In0(N) ==>  M=N"
   355 by (simp add: In0_def)
   356 
   357 lemma In1_inject: "In1(M) = In1(N) ==>  M=N"
   358 by (simp add: In1_def)
   359 
   360 lemma In0_eq [iff]: "(In0 M = In0 N) = (M=N)"
   361 by (blast dest!: In0_inject)
   362 
   363 lemma In1_eq [iff]: "(In1 M = In1 N) = (M=N)"
   364 by (blast dest!: In1_inject)
   365 
   366 lemma inj_In0: "inj In0"
   367 by (blast intro!: inj_onI)
   368 
   369 lemma inj_In1: "inj In1"
   370 by (blast intro!: inj_onI)
   371 
   372 
   373 (*** Function spaces ***)
   374 
   375 lemma Lim_inject: "Lim f = Lim g ==> f = g"
   376 apply (simp add: Lim_def)
   377 apply (rule ext)
   378 apply (blast elim!: Push_Node_inject)
   379 done
   380 
   381 
   382 (*** proving equality of sets and functions using ntrunc ***)
   383 
   384 lemma ntrunc_subsetI: "ntrunc k M <= M"
   385 by (auto simp add: ntrunc_def)
   386 
   387 lemma ntrunc_subsetD: "(!!k. ntrunc k M <= N) ==> M<=N"
   388 by (auto simp add: ntrunc_def)
   389 
   390 (*A generalized form of the take-lemma*)
   391 lemma ntrunc_equality: "(!!k. ntrunc k M = ntrunc k N) ==> M=N"
   392 apply (rule equalityI)
   393 apply (rule_tac [!] ntrunc_subsetD)
   394 apply (rule_tac [!] ntrunc_subsetI [THEN [2] subset_trans], auto) 
   395 done
   396 
   397 lemma ntrunc_o_equality: 
   398     "[| !!k. (ntrunc(k) o h1) = (ntrunc(k) o h2) |] ==> h1=h2"
   399 apply (rule ntrunc_equality [THEN ext])
   400 apply (simp add: expand_fun_eq) 
   401 done
   402 
   403 
   404 (*** Monotonicity ***)
   405 
   406 lemma uprod_mono: "[| A<=A';  B<=B' |] ==> uprod A B <= uprod A' B'"
   407 by (simp add: uprod_def, blast)
   408 
   409 lemma usum_mono: "[| A<=A';  B<=B' |] ==> usum A B <= usum A' B'"
   410 by (simp add: usum_def, blast)
   411 
   412 lemma Scons_mono: "[| M<=M';  N<=N' |] ==> Scons M N <= Scons M' N'"
   413 by (simp add: Scons_def, blast)
   414 
   415 lemma In0_mono: "M<=N ==> In0(M) <= In0(N)"
   416 by (simp add: In0_def subset_refl Scons_mono)
   417 
   418 lemma In1_mono: "M<=N ==> In1(M) <= In1(N)"
   419 by (simp add: In1_def subset_refl Scons_mono)
   420 
   421 
   422 (*** Split and Case ***)
   423 
   424 lemma Split [simp]: "Split c (Scons M N) = c M N"
   425 by (simp add: Split_def)
   426 
   427 lemma Case_In0 [simp]: "Case c d (In0 M) = c(M)"
   428 by (simp add: Case_def)
   429 
   430 lemma Case_In1 [simp]: "Case c d (In1 N) = d(N)"
   431 by (simp add: Case_def)
   432 
   433 
   434 
   435 (**** UN x. B(x) rules ****)
   436 
   437 lemma ntrunc_UN1: "ntrunc k (UN x. f(x)) = (UN x. ntrunc k (f x))"
   438 by (simp add: ntrunc_def, blast)
   439 
   440 lemma Scons_UN1_x: "Scons (UN x. f x) M = (UN x. Scons (f x) M)"
   441 by (simp add: Scons_def, blast)
   442 
   443 lemma Scons_UN1_y: "Scons M (UN x. f x) = (UN x. Scons M (f x))"
   444 by (simp add: Scons_def, blast)
   445 
   446 lemma In0_UN1: "In0(UN x. f(x)) = (UN x. In0(f(x)))"
   447 by (simp add: In0_def Scons_UN1_y)
   448 
   449 lemma In1_UN1: "In1(UN x. f(x)) = (UN x. In1(f(x)))"
   450 by (simp add: In1_def Scons_UN1_y)
   451 
   452 
   453 (*** Equality for Cartesian Product ***)
   454 
   455 lemma dprodI [intro!]: 
   456     "[| (M,M'):r;  (N,N'):s |] ==> (Scons M N, Scons M' N') : dprod r s"
   457 by (auto simp add: dprod_def)
   458 
   459 (*The general elimination rule*)
   460 lemma dprodE [elim!]: 
   461     "[| c : dprod r s;   
   462         !!x y x' y'. [| (x,x') : r;  (y,y') : s;  
   463                         c = (Scons x y, Scons x' y') |] ==> P  
   464      |] ==> P"
   465 by (auto simp add: dprod_def)
   466 
   467 
   468 (*** Equality for Disjoint Sum ***)
   469 
   470 lemma dsum_In0I [intro]: "(M,M'):r ==> (In0(M), In0(M')) : dsum r s"
   471 by (auto simp add: dsum_def)
   472 
   473 lemma dsum_In1I [intro]: "(N,N'):s ==> (In1(N), In1(N')) : dsum r s"
   474 by (auto simp add: dsum_def)
   475 
   476 lemma dsumE [elim!]: 
   477     "[| w : dsum r s;   
   478         !!x x'. [| (x,x') : r;  w = (In0(x), In0(x')) |] ==> P;  
   479         !!y y'. [| (y,y') : s;  w = (In1(y), In1(y')) |] ==> P  
   480      |] ==> P"
   481 by (auto simp add: dsum_def)
   482 
   483 
   484 (*** Monotonicity ***)
   485 
   486 lemma dprod_mono: "[| r<=r';  s<=s' |] ==> dprod r s <= dprod r' s'"
   487 by blast
   488 
   489 lemma dsum_mono: "[| r<=r';  s<=s' |] ==> dsum r s <= dsum r' s'"
   490 by blast
   491 
   492 
   493 (*** Bounding theorems ***)
   494 
   495 lemma dprod_Sigma: "(dprod (A <*> B) (C <*> D)) <= (uprod A C) <*> (uprod B D)"
   496 by blast
   497 
   498 lemmas dprod_subset_Sigma = subset_trans [OF dprod_mono dprod_Sigma, standard]
   499 
   500 (*Dependent version*)
   501 lemma dprod_subset_Sigma2:
   502      "(dprod (Sigma A B) (Sigma C D)) <= 
   503       Sigma (uprod A C) (Split (%x y. uprod (B x) (D y)))"
   504 by auto
   505 
   506 lemma dsum_Sigma: "(dsum (A <*> B) (C <*> D)) <= (usum A C) <*> (usum B D)"
   507 by blast
   508 
   509 lemmas dsum_subset_Sigma = subset_trans [OF dsum_mono dsum_Sigma, standard]
   510 
   511 
   512 text {* hides popular names *}
   513 hide (open) type node item
   514 hide (open) const Push Node Atom Leaf Numb Lim Split Case
   515 
   516 
   517 section {* Datatypes *}
   518 
   519 subsection {* Representing sums *}
   520 
   521 rep_datatype (sum) Inl Inr
   522 proof -
   523   fix P
   524   fix s :: "'a + 'b"
   525   assume x: "\<And>x\<Colon>'a. P (Inl x)" and y: "\<And>y\<Colon>'b. P (Inr y)"
   526   then show "P s" by (auto intro: sumE [of s])
   527 qed simp_all
   528 
   529 lemma sum_case_KK[simp]: "sum_case (%x. a) (%x. a) = (%x. a)"
   530   by (rule ext) (simp split: sum.split)
   531 
   532 lemma surjective_sum: "sum_case (%x::'a. f (Inl x)) (%y::'b. f (Inr y)) s = f(s)"
   533   apply (rule_tac s = s in sumE)
   534    apply (erule ssubst)
   535    apply (rule sum.cases(1))
   536   apply (erule ssubst)
   537   apply (rule sum.cases(2))
   538   done
   539 
   540 lemma sum_case_weak_cong: "s = t ==> sum_case f g s = sum_case f g t"
   541   -- {* Prevents simplification of @{text f} and @{text g}: much faster. *}
   542   by simp
   543 
   544 lemma sum_case_inject:
   545   "sum_case f1 f2 = sum_case g1 g2 ==> (f1 = g1 ==> f2 = g2 ==> P) ==> P"
   546 proof -
   547   assume a: "sum_case f1 f2 = sum_case g1 g2"
   548   assume r: "f1 = g1 ==> f2 = g2 ==> P"
   549   show P
   550     apply (rule r)
   551      apply (rule ext)
   552      apply (cut_tac x = "Inl x" in a [THEN fun_cong], simp)
   553     apply (rule ext)
   554     apply (cut_tac x = "Inr x" in a [THEN fun_cong], simp)
   555     done
   556 qed
   557 
   558 constdefs
   559   Suml :: "('a => 'c) => 'a + 'b => 'c"
   560   "Suml == (%f. sum_case f undefined)"
   561 
   562   Sumr :: "('b => 'c) => 'a + 'b => 'c"
   563   "Sumr == sum_case undefined"
   564 
   565 lemma [code]:
   566   "Suml f (Inl x) = f x"
   567   by (simp add: Suml_def)
   568 
   569 lemma [code]:
   570   "Sumr f (Inr x) = f x"
   571   by (simp add: Sumr_def)
   572 
   573 lemma Suml_inject: "Suml f = Suml g ==> f = g"
   574   by (unfold Suml_def) (erule sum_case_inject)
   575 
   576 lemma Sumr_inject: "Sumr f = Sumr g ==> f = g"
   577   by (unfold Sumr_def) (erule sum_case_inject)
   578 
   579 primrec Projl :: "'a + 'b => 'a"
   580 where Projl_Inl: "Projl (Inl x) = x"
   581 
   582 primrec Projr :: "'a + 'b => 'b"
   583 where Projr_Inr: "Projr (Inr x) = x"
   584 
   585 hide (open) const Suml Sumr Projl Projr
   586 
   587 end