src/HOL/Sum.ML
author oheimb
Tue Jun 02 15:07:00 1998 +0200 (1998-06-02)
changeset 4988 8f4dc836a2ea
parent 4830 bd73675adbed
child 5069 3ea049f7979d
permissions -rw-r--r--
added split_sum_case_asm
     1 (*  Title:      HOL/Sum.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1991  University of Cambridge
     5 
     6 For Sum.thy.  The disjoint sum of two types
     7 *)
     8 
     9 open Sum;
    10 
    11 (** Inl_Rep and Inr_Rep: Representations of the constructors **)
    12 
    13 (*This counts as a non-emptiness result for admitting 'a+'b as a type*)
    14 goalw Sum.thy [Sum_def] "Inl_Rep(a) : Sum";
    15 by (EVERY1 [rtac CollectI, rtac disjI1, rtac exI, rtac refl]);
    16 qed "Inl_RepI";
    17 
    18 goalw Sum.thy [Sum_def] "Inr_Rep(b) : Sum";
    19 by (EVERY1 [rtac CollectI, rtac disjI2, rtac exI, rtac refl]);
    20 qed "Inr_RepI";
    21 
    22 goal Sum.thy "inj_on Abs_Sum Sum";
    23 by (rtac inj_on_inverseI 1);
    24 by (etac Abs_Sum_inverse 1);
    25 qed "inj_on_Abs_Sum";
    26 
    27 (** Distinctness of Inl and Inr **)
    28 
    29 goalw Sum.thy [Inl_Rep_def, Inr_Rep_def] "Inl_Rep(a) ~= Inr_Rep(b)";
    30 by (EVERY1 [rtac notI,
    31             etac (fun_cong RS fun_cong RS fun_cong RS iffE), 
    32             rtac (notE RS ccontr),  etac (mp RS conjunct2), 
    33             REPEAT o (ares_tac [refl,conjI]) ]);
    34 qed "Inl_Rep_not_Inr_Rep";
    35 
    36 goalw Sum.thy [Inl_def,Inr_def] "Inl(a) ~= Inr(b)";
    37 by (rtac (inj_on_Abs_Sum RS inj_on_contraD) 1);
    38 by (rtac Inl_Rep_not_Inr_Rep 1);
    39 by (rtac Inl_RepI 1);
    40 by (rtac Inr_RepI 1);
    41 qed "Inl_not_Inr";
    42 
    43 bind_thm ("Inr_not_Inl", Inl_not_Inr RS not_sym);
    44 
    45 AddIffs [Inl_not_Inr, Inr_not_Inl];
    46 
    47 bind_thm ("Inl_neq_Inr", Inl_not_Inr RS notE);
    48 
    49 val Inr_neq_Inl = sym RS Inl_neq_Inr;
    50 
    51 
    52 (** Injectiveness of Inl and Inr **)
    53 
    54 val [major] = goalw Sum.thy [Inl_Rep_def] "Inl_Rep(a) = Inl_Rep(c) ==> a=c";
    55 by (rtac (major RS fun_cong RS fun_cong RS fun_cong RS iffE) 1);
    56 by (Blast_tac 1);
    57 qed "Inl_Rep_inject";
    58 
    59 val [major] = goalw Sum.thy [Inr_Rep_def] "Inr_Rep(b) = Inr_Rep(d) ==> b=d";
    60 by (rtac (major RS fun_cong RS fun_cong RS fun_cong RS iffE) 1);
    61 by (Blast_tac 1);
    62 qed "Inr_Rep_inject";
    63 
    64 goalw Sum.thy [Inl_def] "inj(Inl)";
    65 by (rtac injI 1);
    66 by (etac (inj_on_Abs_Sum RS inj_onD RS Inl_Rep_inject) 1);
    67 by (rtac Inl_RepI 1);
    68 by (rtac Inl_RepI 1);
    69 qed "inj_Inl";
    70 val Inl_inject = inj_Inl RS injD;
    71 
    72 goalw Sum.thy [Inr_def] "inj(Inr)";
    73 by (rtac injI 1);
    74 by (etac (inj_on_Abs_Sum RS inj_onD RS Inr_Rep_inject) 1);
    75 by (rtac Inr_RepI 1);
    76 by (rtac Inr_RepI 1);
    77 qed "inj_Inr";
    78 val Inr_inject = inj_Inr RS injD;
    79 
    80 goal Sum.thy "(Inl(x)=Inl(y)) = (x=y)";
    81 by (blast_tac (claset() addSDs [Inl_inject]) 1);
    82 qed "Inl_eq";
    83 
    84 goal Sum.thy "(Inr(x)=Inr(y)) = (x=y)";
    85 by (blast_tac (claset() addSDs [Inr_inject]) 1);
    86 qed "Inr_eq";
    87 
    88 AddIffs [Inl_eq, Inr_eq];
    89 
    90 (*** Rules for the disjoint sum of two SETS ***)
    91 
    92 (** Introduction rules for the injections **)
    93 
    94 goalw Sum.thy [sum_def] "!!a A B. a : A ==> Inl(a) : A Plus B";
    95 by (Blast_tac 1);
    96 qed "InlI";
    97 
    98 goalw Sum.thy [sum_def] "!!b A B. b : B ==> Inr(b) : A Plus B";
    99 by (Blast_tac 1);
   100 qed "InrI";
   101 
   102 (** Elimination rules **)
   103 
   104 val major::prems = goalw Sum.thy [sum_def]
   105     "[| u: A Plus B;  \
   106 \       !!x. [| x:A;  u=Inl(x) |] ==> P; \
   107 \       !!y. [| y:B;  u=Inr(y) |] ==> P \
   108 \    |] ==> P";
   109 by (rtac (major RS UnE) 1);
   110 by (REPEAT (rtac refl 1
   111      ORELSE eresolve_tac (prems@[imageE,ssubst]) 1));
   112 qed "PlusE";
   113 
   114 
   115 AddSIs [InlI, InrI]; 
   116 AddSEs [PlusE];
   117 
   118 
   119 (** sum_case -- the selection operator for sums **)
   120 
   121 goalw Sum.thy [sum_case_def] "sum_case f g (Inl x) = f(x)";
   122 by (Blast_tac 1);
   123 qed "sum_case_Inl";
   124 
   125 goalw Sum.thy [sum_case_def] "sum_case f g (Inr x) = g(x)";
   126 by (Blast_tac 1);
   127 qed "sum_case_Inr";
   128 
   129 Addsimps [sum_case_Inl, sum_case_Inr];
   130 
   131 (** Exhaustion rule for sums -- a degenerate form of induction **)
   132 
   133 val prems = goalw Sum.thy [Inl_def,Inr_def]
   134     "[| !!x::'a. s = Inl(x) ==> P;  !!y::'b. s = Inr(y) ==> P \
   135 \    |] ==> P";
   136 by (rtac (rewrite_rule [Sum_def] Rep_Sum RS CollectE) 1);
   137 by (REPEAT (eresolve_tac [disjE,exE] 1
   138      ORELSE EVERY1 [resolve_tac prems, 
   139                     etac subst,
   140                     rtac (Rep_Sum_inverse RS sym)]));
   141 qed "sumE";
   142 
   143 goal Sum.thy "sum_case (%x::'a. f(Inl x)) (%y::'b. f(Inr y)) s = f(s)";
   144 by (EVERY1 [res_inst_tac [("s","s")] sumE, 
   145             etac ssubst, rtac sum_case_Inl,
   146             etac ssubst, rtac sum_case_Inr]);
   147 qed "surjective_sum";
   148 
   149 goal Sum.thy "R(sum_case f g s) = \
   150 \             ((! x. s = Inl(x) --> R(f(x))) & (! y. s = Inr(y) --> R(g(y))))";
   151 by (res_inst_tac [("s","s")] sumE 1);
   152 by Auto_tac;
   153 qed "split_sum_case";
   154 
   155 qed_goal "split_sum_case_asm" Sum.thy "P (sum_case f g s) = \
   156 \ (~((? x. s = Inl x & ~P (f x)) | (? y. s = Inr y & ~P (g y))))"
   157     (K [stac split_sum_case 1,
   158 	Blast_tac 1]);
   159 
   160 (*Prevents simplification of f and g: much faster*)
   161 qed_goal "sum_case_weak_cong" Sum.thy
   162   "s=t ==> sum_case f g s = sum_case f g t"
   163   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   164 
   165 
   166 
   167 (** Rules for the Part primitive **)
   168 
   169 goalw Sum.thy [Part_def]
   170     "!!a b A h. [| a : A;  a=h(b) |] ==> a : Part A h";
   171 by (Blast_tac 1);
   172 qed "Part_eqI";
   173 
   174 val PartI = refl RSN (2,Part_eqI);
   175 
   176 val major::prems = goalw Sum.thy [Part_def]
   177     "[| a : Part A h;  !!z. [| a : A;  a=h(z) |] ==> P  \
   178 \    |] ==> P";
   179 by (rtac (major RS IntE) 1);
   180 by (etac CollectE 1);
   181 by (etac exE 1);
   182 by (REPEAT (ares_tac prems 1));
   183 qed "PartE";
   184 
   185 AddIs  [Part_eqI];
   186 AddSEs [PartE];
   187 
   188 goalw Sum.thy [Part_def] "Part A h <= A";
   189 by (rtac Int_lower1 1);
   190 qed "Part_subset";
   191 
   192 goal Sum.thy "!!A B. A<=B ==> Part A h <= Part B h";
   193 by (Blast_tac 1);
   194 qed "Part_mono";
   195 
   196 val basic_monos = basic_monos @ [Part_mono];
   197 
   198 goalw Sum.thy [Part_def] "!!a. a : Part A h ==> a : A";
   199 by (etac IntD1 1);
   200 qed "PartD1";
   201 
   202 goal Sum.thy "Part A (%x. x) = A";
   203 by (Blast_tac 1);
   204 qed "Part_id";
   205 
   206 goal Sum.thy "Part (A Int B) h = (Part A h) Int (Part B h)";
   207 by (Blast_tac 1);
   208 qed "Part_Int";
   209 
   210 (*For inductive definitions*)
   211 goal Sum.thy "Part (A Int {x. P x}) h = (Part A h) Int {x. P x}";
   212 by (Blast_tac 1);
   213 qed "Part_Collect";