src/HOL/Relation.ML
author wenzelm
Wed Oct 03 20:54:16 2001 +0200 (2001-10-03)
changeset 11655 923e4d0d36d5
parent 11451 8abfb4f7bd02
child 12487 bbd564190c9b
permissions -rw-r--r--
tuned parentheses in relational expressions;
     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 {} = {}";
   237 by (Blast_tac 1); 
   238 qed "Domain_empty";
   239 Addsimps [Domain_empty];
   240 
   241 Goal "Domain (insert (a, b) r) = insert a (Domain r)";
   242 by (Blast_tac 1); 
   243 qed "Domain_insert";
   244 
   245 Goal "Domain Id = UNIV";
   246 by (Blast_tac 1);
   247 qed "Domain_Id";
   248 Addsimps [Domain_Id];
   249 
   250 Goal "Domain (diag A) = A";
   251 by Auto_tac;
   252 qed "Domain_diag";
   253 Addsimps [Domain_diag];
   254 
   255 Goal "Domain(A Un B) = Domain(A) Un Domain(B)";
   256 by (Blast_tac 1);
   257 qed "Domain_Un_eq";
   258 
   259 Goal "Domain(A Int B) <= Domain(A) Int Domain(B)";
   260 by (Blast_tac 1);
   261 qed "Domain_Int_subset";
   262 
   263 Goal "Domain(A) - Domain(B) <= Domain(A - B)";
   264 by (Blast_tac 1);
   265 qed "Domain_Diff_subset";
   266 
   267 Goal "Domain (Union S) = (UN A:S. Domain A)";
   268 by (Blast_tac 1);
   269 qed "Domain_Union";
   270 
   271 Goal "r <= s ==> Domain r <= Domain s";
   272 by (Blast_tac 1);
   273 qed "Domain_mono";
   274 
   275 
   276 (** Range **)
   277 
   278 Goalw [Domain_def, Range_def] "(a: Range(r)) = (EX y. (y,a): r)";
   279 by (Blast_tac 1);
   280 qed "Range_iff";
   281 
   282 Goalw [Range_def] "(a,b): r ==> b : Range(r)";
   283 by (etac (converseI RS DomainI) 1);
   284 qed "RangeI";
   285 
   286 val major::prems = Goalw [Range_def] 
   287     "[| b : Range(r);  !!x. (x,b): r ==> P |] ==> P";
   288 by (rtac (major RS DomainE) 1);
   289 by (resolve_tac prems 1);
   290 by (etac converseD 1) ;
   291 qed "RangeE";
   292 
   293 AddIs  [RangeI];
   294 AddSEs [RangeE];
   295 
   296 Goal "Range {} = {}";
   297 by (Blast_tac 1); 
   298 qed "Range_empty";
   299 Addsimps [Range_empty];
   300 
   301 Goal "Range (insert (a, b) r) = insert b (Range r)";
   302 by (Blast_tac 1); 
   303 qed "Range_insert";
   304 
   305 Goal "Range Id = UNIV";
   306 by (Blast_tac 1);
   307 qed "Range_Id";
   308 Addsimps [Range_Id];
   309 
   310 Goal "Range (diag A) = A";
   311 by Auto_tac;
   312 qed "Range_diag";
   313 Addsimps [Range_diag];
   314 
   315 Goal "Range(A Un B) = Range(A) Un Range(B)";
   316 by (Blast_tac 1);
   317 qed "Range_Un_eq";
   318 
   319 Goal "Range(A Int B) <= Range(A) Int Range(B)";
   320 by (Blast_tac 1);
   321 qed "Range_Int_subset";
   322 
   323 Goal "Range(A) - Range(B) <= Range(A - B)";
   324 by (Blast_tac 1);
   325 qed "Range_Diff_subset";
   326 
   327 Goal "Range (Union S) = (UN A:S. Range A)";
   328 by (Blast_tac 1);
   329 qed "Range_Union";
   330 
   331 
   332 (*** Image of a set under a relation ***)
   333 
   334 overload_1st_set "Relation.Image";
   335 
   336 Goalw [Image_def] "(b : r``A) = (EX x:A. (x,b):r)";
   337 by (Blast_tac 1);
   338 qed "Image_iff";
   339 
   340 Goalw [Image_def] "r``{a} = {b. (a,b):r}";
   341 by (Blast_tac 1);
   342 qed "Image_singleton";
   343 
   344 Goal "(b : r``{a}) = ((a,b):r)";
   345 by (rtac (Image_iff RS trans) 1);
   346 by (Blast_tac 1);
   347 qed "Image_singleton_iff";
   348 
   349 AddIffs [Image_singleton_iff];
   350 
   351 Goalw [Image_def] "[| (a,b): r;  a:A |] ==> b : r``A";
   352 by (Blast_tac 1);
   353 qed "ImageI";
   354 
   355 val major::prems = Goalw [Image_def]
   356     "[| b: r``A;  !!x.[| (x,b): r;  x:A |] ==> P |] ==> P";
   357 by (rtac (major RS CollectE) 1);
   358 by (Clarify_tac 1);
   359 by (rtac (hd prems) 1);
   360 by (REPEAT (etac bexE 1 ORELSE ares_tac prems 1)) ;
   361 qed "ImageE";
   362 
   363 AddIs  [ImageI];
   364 AddSEs [ImageE];
   365 
   366 (*This version's more effective when we already have the required "a"*)
   367 Goal  "[| a:A;  (a,b): r |] ==> b : r``A";
   368 by (Blast_tac 1);
   369 qed "rev_ImageI";
   370 
   371 Goal "R``{} = {}";
   372 by (Blast_tac 1);
   373 qed "Image_empty";
   374 
   375 Addsimps [Image_empty];
   376 
   377 Goal "Id `` A = A";
   378 by (Blast_tac 1);
   379 qed "Image_Id";
   380 
   381 Goal "diag A `` B = A Int B";
   382 by (Blast_tac 1);
   383 qed "Image_diag";
   384 
   385 Addsimps [Image_Id, Image_diag];
   386 
   387 Goal "R `` (A Int B) <= R `` A Int R `` B";
   388 by (Blast_tac 1);
   389 qed "Image_Int_subset";
   390 
   391 Goal "R `` (A Un B) = R `` A Un R `` B";
   392 by (Blast_tac 1);
   393 qed "Image_Un";
   394 
   395 Goal "r <= A <*> B ==> r``C <= B";
   396 by (rtac subsetI 1);
   397 by (REPEAT (eresolve_tac [asm_rl, ImageE, subsetD RS SigmaD2] 1)) ;
   398 qed "Image_subset";
   399 
   400 (*NOT suitable for rewriting*)
   401 Goal "r``B = (UN y: B. r``{y})";
   402 by (Blast_tac 1);
   403 qed "Image_eq_UN";
   404 
   405 Goal "[| r'<=r; A'<=A |] ==> (r' `` A') <= (r `` A)";
   406 by (Blast_tac 1);
   407 qed "Image_mono";
   408 
   409 Goal "(r `` (UNION A B)) = (UN x:A.(r `` (B x)))";
   410 by (Blast_tac 1);
   411 qed "Image_UN";
   412 
   413 (*Converse inclusion fails*)
   414 Goal "(r `` (INTER A B)) <= (INT x:A.(r `` (B x)))";
   415 by (Blast_tac 1);
   416 qed "Image_INT_subset";
   417 
   418 Goal "(r``A <= B) = (A <= - ((r^-1) `` (-B)))";
   419 by (Blast_tac 1);
   420 qed "Image_subset_eq";
   421 
   422 section "single_valued";
   423 
   424 Goalw [single_valued_def]
   425      "ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z) ==> single_valued r";
   426 by (assume_tac 1);
   427 qed "single_valuedI";
   428 
   429 Goalw [single_valued_def]
   430      "[| single_valued r;  (x,y):r;  (x,z):r|] ==> y=z";
   431 by Auto_tac;
   432 qed "single_valuedD";
   433 
   434 
   435 (** Graphs given by Collect **)
   436 
   437 Goal "Domain{(x,y). P x y} = {x. EX y. P x y}";
   438 by Auto_tac; 
   439 qed "Domain_Collect_split";
   440 
   441 Goal "Range{(x,y). P x y} = {y. EX x. P x y}";
   442 by Auto_tac; 
   443 qed "Range_Collect_split";
   444 
   445 Goal "{(x,y). P x y} `` A = {y. EX x:A. P x y}";
   446 by Auto_tac; 
   447 qed "Image_Collect_split";
   448 
   449 Addsimps [Domain_Collect_split, Range_Collect_split, Image_Collect_split];
   450 
   451 (** Composition of function and relation **)
   452 
   453 Goalw [fun_rel_comp_def] "A <= B ==> fun_rel_comp f A <= fun_rel_comp f B";
   454 by (Fast_tac 1);
   455 qed "fun_rel_comp_mono";
   456 
   457 Goalw [fun_rel_comp_def]
   458      "ALL x. EX! y. (f x, y) : R ==> EX! g. g : fun_rel_comp f R";
   459 by (res_inst_tac [("a","%x. THE y. (f x, y) : R")] ex1I 1);
   460 by (fast_tac (claset() addSDs [theI']) 1); 
   461 by (fast_tac (claset() addIs [ext, the1_equality RS sym]) 1);
   462 qed "fun_rel_comp_unique";
   463 
   464 
   465 section "inverse image";
   466 
   467 Goalw [trans_def,inv_image_def]
   468     "trans r ==> trans (inv_image r f)";
   469 by (Simp_tac 1);
   470 by (Blast_tac 1);
   471 qed "trans_inv_image";
   472