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