src/HOL/Relation.ML
author paulson
Mon Jul 19 15:24:35 1999 +0200 (1999-07-19)
changeset 7031 972b5f62f476
parent 7014 11ee650edcd2
child 7083 9663eb2bce05
permissions -rw-r--r--
getting rid of qed_goal
     1 (*  Title:      Relation.ML
     2     ID:         $Id$
     3     Authors:    Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1996  University of Cambridge
     5 *)
     6 
     7 open Relation;
     8 
     9 (** Identity relation **)
    10 
    11 Goalw [Id_def] "(a,a) : Id";  
    12 by (Blast_tac 1);
    13 qed "IdI";
    14 
    15 val major::prems = Goalw [Id_def]
    16     "[| p: Id;  !!x.[| p = (x,x) |] ==> P  \
    17 \    |] ==>  P";  
    18 by (rtac (major RS CollectE) 1);
    19 by (etac exE 1);
    20 by (eresolve_tac prems 1);
    21 qed "IdE";
    22 
    23 Goalw [Id_def] "(a,b):Id = (a=b)";
    24 by (Blast_tac 1);
    25 qed "pair_in_Id_conv";
    26 Addsimps [pair_in_Id_conv];
    27 
    28 Goalw [refl_def] "reflexive Id";
    29 by Auto_tac;
    30 qed "reflexive_Id";
    31 
    32 (*A strange result, since Id is also symmetric.*)
    33 Goalw [antisym_def] "antisym Id";
    34 by Auto_tac;
    35 qed "antisym_Id";
    36 
    37 Goalw [trans_def] "trans Id";
    38 by Auto_tac;
    39 qed "trans_Id";
    40 
    41 
    42 (** Diagonal relation: indentity restricted to some set **)
    43 
    44 (*** Equality : the diagonal relation ***)
    45 
    46 Goalw [diag_def] "[| a=b;  a:A |] ==> (a,b) : diag(A)";
    47 by (Blast_tac 1);
    48 qed "diag_eqI";
    49 
    50 val diagI = refl RS diag_eqI |> standard;
    51 
    52 (*The general elimination rule*)
    53 val major::prems = Goalw [diag_def]
    54     "[| c : diag(A);  \
    55 \       !!x y. [| x:A;  c = (x,x) |] ==> P \
    56 \    |] ==> P";
    57 by (rtac (major RS UN_E) 1);
    58 by (REPEAT (eresolve_tac [asm_rl,singletonE] 1 ORELSE resolve_tac prems 1));
    59 qed "diagE";
    60 
    61 AddSIs [diagI];
    62 AddSEs [diagE];
    63 
    64 Goal "((x,y) : diag A) = (x=y & x : A)";
    65 by (Blast_tac 1);
    66 qed "diag_iff";
    67 
    68 Goal "diag(A) <= A Times A";
    69 by (Blast_tac 1);
    70 qed "diag_subset_Times";
    71 
    72 
    73 
    74 (** Composition of two relations **)
    75 
    76 Goalw [comp_def]
    77     "[| (a,b):s; (b,c):r |] ==> (a,c) : r O s";
    78 by (Blast_tac 1);
    79 qed "compI";
    80 
    81 (*proof requires higher-level assumptions or a delaying of hyp_subst_tac*)
    82 val prems = Goalw [comp_def]
    83     "[| xz : r O s;  \
    84 \       !!x y z. [| xz = (x,z);  (x,y):s;  (y,z):r |] ==> P \
    85 \    |] ==> P";
    86 by (cut_facts_tac prems 1);
    87 by (REPEAT (eresolve_tac [CollectE, splitE, exE, conjE] 1 
    88      ORELSE ares_tac prems 1));
    89 qed "compE";
    90 
    91 val prems = Goal
    92     "[| (a,c) : r O s;  \
    93 \       !!y. [| (a,y):s;  (y,c):r |] ==> P \
    94 \    |] ==> P";
    95 by (rtac compE 1);
    96 by (REPEAT (ares_tac prems 1 ORELSE eresolve_tac [Pair_inject,ssubst] 1));
    97 qed "compEpair";
    98 
    99 AddIs [compI, IdI];
   100 AddSEs [compE, IdE];
   101 
   102 Goal "R O Id = R";
   103 by (Fast_tac 1);
   104 qed "R_O_Id";
   105 
   106 Goal "Id O R = R";
   107 by (Fast_tac 1);
   108 qed "Id_O_R";
   109 
   110 Addsimps [R_O_Id,Id_O_R];
   111 
   112 Goal "(R O S) O T = R O (S O T)";
   113 by (Blast_tac 1);
   114 qed "O_assoc";
   115 
   116 Goal "[| r'<=r; s'<=s |] ==> (r' O s') <= (r O s)";
   117 by (Blast_tac 1);
   118 qed "comp_mono";
   119 
   120 Goal "[| s <= A Times B;  r <= B Times C |] ==> (r O s) <= A Times C";
   121 by (Blast_tac 1);
   122 qed "comp_subset_Sigma";
   123 
   124 (** Natural deduction for refl(r) **)
   125 
   126 val prems = Goalw [refl_def]
   127     "[| r <= A Times A;  !! x. x:A ==> (x,x):r |] ==> refl A r";
   128 by (REPEAT (ares_tac (prems@[ballI,conjI]) 1));
   129 qed "reflI";
   130 
   131 Goalw [refl_def] "[| refl A r; a:A |] ==> (a,a):r";
   132 by (Blast_tac 1);
   133 qed "reflD";
   134 
   135 (** Natural deduction for antisym(r) **)
   136 
   137 val prems = Goalw [antisym_def]
   138     "(!! x y. [| (x,y):r;  (y,x):r |] ==> x=y) ==> antisym(r)";
   139 by (REPEAT (ares_tac (prems@[allI,impI]) 1));
   140 qed "antisymI";
   141 
   142 Goalw [antisym_def] "[| antisym(r);  (a,b):r;  (b,a):r |] ==> a=b";
   143 by (Blast_tac 1);
   144 qed "antisymD";
   145 
   146 (** Natural deduction for trans(r) **)
   147 
   148 val prems = Goalw [trans_def]
   149     "(!! x y z. [| (x,y):r;  (y,z):r |] ==> (x,z):r) ==> trans(r)";
   150 by (REPEAT (ares_tac (prems@[allI,impI]) 1));
   151 qed "transI";
   152 
   153 Goalw [trans_def] "[| trans(r);  (a,b):r;  (b,c):r |] ==> (a,c):r";
   154 by (Blast_tac 1);
   155 qed "transD";
   156 
   157 (** Natural deduction for r^-1 **)
   158 
   159 Goalw [converse_def] "((a,b): r^-1) = ((b,a):r)";
   160 by (Simp_tac 1);
   161 qed "converse_iff";
   162 
   163 AddIffs [converse_iff];
   164 
   165 Goalw [converse_def] "(a,b):r ==> (b,a): r^-1";
   166 by (Simp_tac 1);
   167 qed "converseI";
   168 
   169 Goalw [converse_def] "(a,b) : r^-1 ==> (b,a) : r";
   170 by (Blast_tac 1);
   171 qed "converseD";
   172 
   173 (*More general than converseD, as it "splits" the member of the relation*)
   174 
   175 val [major,minor] = Goalw [converse_def]
   176     "[| yx : r^-1;  \
   177 \       !!x y. [| yx=(y,x);  (x,y):r |] ==> P \
   178 \    |] ==> P";
   179 by (rtac (major RS CollectE) 1);
   180 by (REPEAT (eresolve_tac [splitE, bexE,exE, conjE, minor] 1));
   181 by (assume_tac 1);
   182 qed "converseE";
   183 AddSEs [converseE];
   184 
   185 Goalw [converse_def] "(r^-1)^-1 = r";
   186 by (Blast_tac 1);
   187 qed "converse_converse";
   188 Addsimps [converse_converse];
   189 
   190 Goal "(r O s)^-1 = s^-1 O r^-1";
   191 by (Blast_tac 1);
   192 qed "converse_comp";
   193 
   194 Goal "Id^-1 = Id";
   195 by (Blast_tac 1);
   196 qed "converse_Id";
   197 Addsimps [converse_Id];
   198 
   199 Goal "(diag A) ^-1 = diag A";
   200 by (Blast_tac 1);
   201 qed "converse_diag";
   202 Addsimps [converse_diag];
   203 
   204 (** Domain **)
   205 
   206 Goalw [Domain_def] "a: Domain(r) = (EX y. (a,y): r)";
   207 by (Blast_tac 1);
   208 qed "Domain_iff";
   209 
   210 Goal "(a,b): r ==> a: Domain(r)";
   211 by (etac (exI RS (Domain_iff RS iffD2)) 1) ;
   212 qed "DomainI";
   213 
   214 val prems= Goal "[| a : Domain(r);  !!y. (a,y): r ==> P |] ==> P";
   215 by (rtac (Domain_iff RS iffD1 RS exE) 1);
   216 by (REPEAT (ares_tac prems 1)) ;
   217 qed "DomainE";
   218 
   219 AddIs  [DomainI];
   220 AddSEs [DomainE];
   221 
   222 Goal "Domain Id = UNIV";
   223 by (Blast_tac 1);
   224 qed "Domain_Id";
   225 Addsimps [Domain_Id];
   226 
   227 Goal "Domain (diag A) = A";
   228 by Auto_tac;
   229 qed "Domain_diag";
   230 Addsimps [Domain_diag];
   231 
   232 Goal "Domain(A Un B) = Domain(A) Un Domain(B)";
   233 by (Blast_tac 1);
   234 qed "Domain_Un_eq";
   235 
   236 Goal "Domain(A Int B) <= Domain(A) Int Domain(B)";
   237 by (Blast_tac 1);
   238 qed "Domain_Int_subset";
   239 
   240 Goal "Domain(A) - Domain(B) <= Domain(A - B)";
   241 by (Blast_tac 1);
   242 qed "Domain_Diff_subset";
   243 
   244 Goal "Domain (Union S) = (UN A:S. Domain A)";
   245 by (Blast_tac 1);
   246 qed "Domain_Union";
   247 
   248 
   249 (** Range **)
   250 
   251 Goalw [Domain_def, Range_def] "a: Range(r) = (EX y. (y,a): r)";
   252 by (Blast_tac 1);
   253 qed "Range_iff";
   254 
   255 Goalw [Range_def] "(a,b): r ==> b : Range(r)";
   256 by (etac (converseI RS DomainI) 1);
   257 qed "RangeI";
   258 
   259 val major::prems = Goalw [Range_def] 
   260     "[| b : Range(r);  !!x. (x,b): r ==> P |] ==> P";
   261 by (rtac (major RS DomainE) 1);
   262 by (resolve_tac prems 1);
   263 by (etac converseD 1) ;
   264 qed "RangeE";
   265 
   266 AddIs  [RangeI];
   267 AddSEs [RangeE];
   268 
   269 Goal "Range Id = UNIV";
   270 by (Blast_tac 1);
   271 qed "Range_Id";
   272 Addsimps [Range_Id];
   273 
   274 Goal "Range (diag A) = A";
   275 by Auto_tac;
   276 qed "Range_diag";
   277 Addsimps [Range_diag];
   278 
   279 Goal "Range(A Un B) = Range(A) Un Range(B)";
   280 by (Blast_tac 1);
   281 qed "Range_Un_eq";
   282 
   283 Goal "Range(A Int B) <= Range(A) Int Range(B)";
   284 by (Blast_tac 1);
   285 qed "Range_Int_subset";
   286 
   287 Goal "Range(A) - Range(B) <= Range(A - B)";
   288 by (Blast_tac 1);
   289 qed "Range_Diff_subset";
   290 
   291 Goal "Range (Union S) = (UN A:S. Range A)";
   292 by (Blast_tac 1);
   293 qed "Range_Union";
   294 
   295 
   296 (*** Image of a set under a relation ***)
   297 
   298 overload_1st_set "Relation.op ^^";
   299 
   300 Goalw [Image_def] "b : r^^A = (? x:A. (x,b):r)";
   301 by (Blast_tac 1);
   302 qed "Image_iff";
   303 
   304 Goalw [Image_def] "r^^{a} = {b. (a,b):r}";
   305 by (Blast_tac 1);
   306 qed "Image_singleton";
   307 
   308 Goal "(b : r^^{a}) = ((a,b):r)";
   309 by (rtac (Image_iff RS trans) 1);
   310 by (Blast_tac 1);
   311 qed "Image_singleton_iff";
   312 
   313 AddIffs [Image_singleton_iff];
   314 
   315 Goalw [Image_def] "[| (a,b): r;  a:A |] ==> b : r^^A";
   316 by (Blast_tac 1);
   317 qed "ImageI";
   318 
   319 val major::prems = Goalw [Image_def]
   320     "[| b: r^^A;  !!x.[| (x,b): r;  x:A |] ==> P |] ==> P";
   321 by (rtac (major RS CollectE) 1);
   322 by (Clarify_tac 1);
   323 by (rtac (hd prems) 1);
   324 by (REPEAT (etac bexE 1 ORELSE ares_tac prems 1)) ;
   325 qed "ImageE";
   326 
   327 AddIs  [ImageI];
   328 AddSEs [ImageE];
   329 
   330 
   331 Goal "R^^{} = {}";
   332 by (Blast_tac 1);
   333 qed "Image_empty";
   334 
   335 Addsimps [Image_empty];
   336 
   337 Goal "Id ^^ A = A";
   338 by (Blast_tac 1);
   339 qed "Image_Id";
   340 
   341 Goal "diag A ^^ B = A Int B";
   342 by (Blast_tac 1);
   343 qed "Image_diag";
   344 
   345 Addsimps [Image_Id, Image_diag];
   346 
   347 Goal "R ^^ (A Int B) <= R ^^ A Int R ^^ B";
   348 by (Blast_tac 1);
   349 qed "Image_Int_subset";
   350 
   351 Goal "R ^^ (A Un B) = R ^^ A Un R ^^ B";
   352 by (Blast_tac 1);
   353 qed "Image_Un";
   354 
   355 Goal "r <= A Times B ==> r^^C <= B";
   356 by (rtac subsetI 1);
   357 by (REPEAT (eresolve_tac [asm_rl, ImageE, subsetD RS SigmaD2] 1)) ;
   358 qed "Image_subset";
   359 
   360 (*NOT suitable for rewriting*)
   361 Goal "r^^B = (UN y: B. r^^{y})";
   362 by (Blast_tac 1);
   363 qed "Image_eq_UN";
   364 
   365 
   366 section "Univalent";
   367 
   368 Goalw [Univalent_def]
   369      "!x y. (x,y):r --> (!z. (x,z):r --> y=z) ==> Univalent r";
   370 by (assume_tac 1);
   371 qed "UnivalentI";
   372 
   373 Goalw [Univalent_def]
   374      "[| Univalent r;  (x,y):r;  (x,z):r|] ==> y=z";
   375 by Auto_tac;
   376 qed "UnivalentD";
   377 
   378 
   379 (** Graphs of partial functions **)
   380 
   381 Goal "Domain{(x,y). y = f x & P x} = {x. P x}";
   382 by (Blast_tac 1);
   383 qed "Domain_partial_func";
   384 
   385 Goal "Range{(x,y). y = f x & P x} = f``{x. P x}";
   386 by (Blast_tac 1);
   387 qed "Range_partial_func";
   388 
   389 
   390 (** Composition of function and relation **)
   391 
   392 Goalw [fun_rel_comp_def] "A <= B ==> fun_rel_comp f A <= fun_rel_comp f B";
   393 by (Fast_tac 1);
   394 qed "fun_rel_comp_mono";
   395 
   396 Goalw [fun_rel_comp_def] "! x. ?! y. (f x, y) : R ==> ?! g. g : fun_rel_comp f R";
   397 by (res_inst_tac [("a","%x. @y. (f x, y) : R")] ex1I 1);
   398 by (rtac CollectI 1);
   399 by (rtac allI 1);
   400 by (etac allE 1);
   401 by (rtac (select_eq_Ex RS iffD2) 1);
   402 by (etac ex1_implies_ex 1);
   403 by (rtac ext 1);
   404 by (etac CollectE 1);
   405 by (REPEAT (etac allE 1));
   406 by (rtac (select1_equality RS sym) 1);
   407 by (atac 1);
   408 by (atac 1);
   409 qed "fun_rel_comp_unique";