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