src/HOL/Relation.ML
author paulson
Mon Jan 31 16:18:42 2000 +0100 (2000-01-31)
changeset 8174 56d9baa2ddb0
parent 8004 6273f58ea2c1
child 8265 187cada50e19
permissions -rw-r--r--
new theorem rev_ImageI
     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 Addsimps [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 val 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 Times 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 Goal "[| r'<=r; s'<=s |] ==> (r' O s') <= (r O s)";
   115 by (Blast_tac 1);
   116 qed "comp_mono";
   117 
   118 Goal "[| s <= A Times B;  r <= B Times C |] ==> (r O s) <= A Times C";
   119 by (Blast_tac 1);
   120 qed "comp_subset_Sigma";
   121 
   122 (** Natural deduction for refl(r) **)
   123 
   124 val prems = Goalw [refl_def]
   125     "[| r <= A Times A;  !! x. x:A ==> (x,x):r |] ==> refl A r";
   126 by (REPEAT (ares_tac (prems@[ballI,conjI]) 1));
   127 qed "reflI";
   128 
   129 Goalw [refl_def] "[| refl A r; a:A |] ==> (a,a):r";
   130 by (Blast_tac 1);
   131 qed "reflD";
   132 
   133 (** Natural deduction for antisym(r) **)
   134 
   135 val prems = Goalw [antisym_def]
   136     "(!! x y. [| (x,y):r;  (y,x):r |] ==> x=y) ==> antisym(r)";
   137 by (REPEAT (ares_tac (prems@[allI,impI]) 1));
   138 qed "antisymI";
   139 
   140 Goalw [antisym_def] "[| antisym(r);  (a,b):r;  (b,a):r |] ==> a=b";
   141 by (Blast_tac 1);
   142 qed "antisymD";
   143 
   144 (** Natural deduction for trans(r) **)
   145 
   146 val prems = Goalw [trans_def]
   147     "(!! x y z. [| (x,y):r;  (y,z):r |] ==> (x,z):r) ==> trans(r)";
   148 by (REPEAT (ares_tac (prems@[allI,impI]) 1));
   149 qed "transI";
   150 
   151 Goalw [trans_def] "[| trans(r);  (a,b):r;  (b,c):r |] ==> (a,c):r";
   152 by (Blast_tac 1);
   153 qed "transD";
   154 
   155 (** Natural deduction for r^-1 **)
   156 
   157 Goalw [converse_def] "((a,b): r^-1) = ((b,a):r)";
   158 by (Simp_tac 1);
   159 qed "converse_iff";
   160 
   161 AddIffs [converse_iff];
   162 
   163 Goalw [converse_def] "(a,b):r ==> (b,a): r^-1";
   164 by (Simp_tac 1);
   165 qed "converseI";
   166 
   167 Goalw [converse_def] "(a,b) : r^-1 ==> (b,a) : r";
   168 by (Blast_tac 1);
   169 qed "converseD";
   170 
   171 (*More general than converseD, as it "splits" the member of the relation*)
   172 
   173 val [major,minor] = Goalw [converse_def]
   174     "[| yx : r^-1;  \
   175 \       !!x y. [| yx=(y,x);  (x,y):r |] ==> P \
   176 \    |] ==> P";
   177 by (rtac (major RS CollectE) 1);
   178 by (REPEAT (eresolve_tac [splitE, bexE,exE, conjE, minor] 1));
   179 by (assume_tac 1);
   180 qed "converseE";
   181 AddSEs [converseE];
   182 
   183 Goalw [converse_def] "(r^-1)^-1 = r";
   184 by (Blast_tac 1);
   185 qed "converse_converse";
   186 Addsimps [converse_converse];
   187 
   188 Goal "(r O s)^-1 = s^-1 O r^-1";
   189 by (Blast_tac 1);
   190 qed "converse_comp";
   191 
   192 Goal "Id^-1 = Id";
   193 by (Blast_tac 1);
   194 qed "converse_Id";
   195 Addsimps [converse_Id];
   196 
   197 Goal "(diag A) ^-1 = diag A";
   198 by (Blast_tac 1);
   199 qed "converse_diag";
   200 Addsimps [converse_diag];
   201 
   202 Goalw [refl_def] "refl A r ==> refl A (converse r)";
   203 by (Blast_tac 1);
   204 qed "refl_converse";
   205 
   206 Goalw [antisym_def] "antisym (converse r) = antisym r";
   207 by (Blast_tac 1);
   208 qed "antisym_converse";
   209 
   210 Goalw [trans_def] "trans (converse r) = trans r";
   211 by (Blast_tac 1);
   212 qed "trans_converse";
   213 
   214 (** Domain **)
   215 
   216 Goalw [Domain_def] "a: Domain(r) = (EX y. (a,y): r)";
   217 by (Blast_tac 1);
   218 qed "Domain_iff";
   219 
   220 Goal "(a,b): r ==> a: Domain(r)";
   221 by (etac (exI RS (Domain_iff RS iffD2)) 1) ;
   222 qed "DomainI";
   223 
   224 val prems= Goal "[| a : Domain(r);  !!y. (a,y): r ==> P |] ==> P";
   225 by (rtac (Domain_iff RS iffD1 RS exE) 1);
   226 by (REPEAT (ares_tac prems 1)) ;
   227 qed "DomainE";
   228 
   229 AddIs  [DomainI];
   230 AddSEs [DomainE];
   231 
   232 Goal "Domain Id = UNIV";
   233 by (Blast_tac 1);
   234 qed "Domain_Id";
   235 Addsimps [Domain_Id];
   236 
   237 Goal "Domain (diag A) = A";
   238 by Auto_tac;
   239 qed "Domain_diag";
   240 Addsimps [Domain_diag];
   241 
   242 Goal "Domain(A Un B) = Domain(A) Un Domain(B)";
   243 by (Blast_tac 1);
   244 qed "Domain_Un_eq";
   245 
   246 Goal "Domain(A Int B) <= Domain(A) Int Domain(B)";
   247 by (Blast_tac 1);
   248 qed "Domain_Int_subset";
   249 
   250 Goal "Domain(A) - Domain(B) <= Domain(A - B)";
   251 by (Blast_tac 1);
   252 qed "Domain_Diff_subset";
   253 
   254 Goal "Domain (Union S) = (UN A:S. Domain A)";
   255 by (Blast_tac 1);
   256 qed "Domain_Union";
   257 
   258 Goal "r <= s ==> Domain r <= Domain s";
   259 by (Blast_tac 1);
   260 qed "Domain_mono";
   261 
   262 
   263 (** Range **)
   264 
   265 Goalw [Domain_def, Range_def] "a: Range(r) = (EX y. (y,a): r)";
   266 by (Blast_tac 1);
   267 qed "Range_iff";
   268 
   269 Goalw [Range_def] "(a,b): r ==> b : Range(r)";
   270 by (etac (converseI RS DomainI) 1);
   271 qed "RangeI";
   272 
   273 val major::prems = Goalw [Range_def] 
   274     "[| b : Range(r);  !!x. (x,b): r ==> P |] ==> P";
   275 by (rtac (major RS DomainE) 1);
   276 by (resolve_tac prems 1);
   277 by (etac converseD 1) ;
   278 qed "RangeE";
   279 
   280 AddIs  [RangeI];
   281 AddSEs [RangeE];
   282 
   283 Goal "Range Id = UNIV";
   284 by (Blast_tac 1);
   285 qed "Range_Id";
   286 Addsimps [Range_Id];
   287 
   288 Goal "Range (diag A) = A";
   289 by Auto_tac;
   290 qed "Range_diag";
   291 Addsimps [Range_diag];
   292 
   293 Goal "Range(A Un B) = Range(A) Un Range(B)";
   294 by (Blast_tac 1);
   295 qed "Range_Un_eq";
   296 
   297 Goal "Range(A Int B) <= Range(A) Int Range(B)";
   298 by (Blast_tac 1);
   299 qed "Range_Int_subset";
   300 
   301 Goal "Range(A) - Range(B) <= Range(A - B)";
   302 by (Blast_tac 1);
   303 qed "Range_Diff_subset";
   304 
   305 Goal "Range (Union S) = (UN A:S. Range A)";
   306 by (Blast_tac 1);
   307 qed "Range_Union";
   308 
   309 
   310 (*** Image of a set under a relation ***)
   311 
   312 overload_1st_set "Relation.Image";
   313 
   314 Goalw [Image_def] "b : r^^A = (? x:A. (x,b):r)";
   315 by (Blast_tac 1);
   316 qed "Image_iff";
   317 
   318 Goalw [Image_def] "r^^{a} = {b. (a,b):r}";
   319 by (Blast_tac 1);
   320 qed "Image_singleton";
   321 
   322 Goal "(b : r^^{a}) = ((a,b):r)";
   323 by (rtac (Image_iff RS trans) 1);
   324 by (Blast_tac 1);
   325 qed "Image_singleton_iff";
   326 
   327 AddIffs [Image_singleton_iff];
   328 
   329 Goalw [Image_def] "[| (a,b): r;  a:A |] ==> b : r^^A";
   330 by (Blast_tac 1);
   331 qed "ImageI";
   332 
   333 val major::prems = Goalw [Image_def]
   334     "[| b: r^^A;  !!x.[| (x,b): r;  x:A |] ==> P |] ==> P";
   335 by (rtac (major RS CollectE) 1);
   336 by (Clarify_tac 1);
   337 by (rtac (hd prems) 1);
   338 by (REPEAT (etac bexE 1 ORELSE ares_tac prems 1)) ;
   339 qed "ImageE";
   340 
   341 AddIs  [ImageI];
   342 AddSEs [ImageE];
   343 
   344 (*This version's more effective when we already have the required "a"*)
   345 Goal  "[| a:A;  (a,b): r |] ==> b : r^^A";
   346 by (Blast_tac 1);
   347 qed "rev_ImageI";
   348 
   349 
   350 Goal "R^^{} = {}";
   351 by (Blast_tac 1);
   352 qed "Image_empty";
   353 
   354 Addsimps [Image_empty];
   355 
   356 Goal "Id ^^ A = A";
   357 by (Blast_tac 1);
   358 qed "Image_Id";
   359 
   360 Goal "diag A ^^ B = A Int B";
   361 by (Blast_tac 1);
   362 qed "Image_diag";
   363 
   364 Addsimps [Image_Id, Image_diag];
   365 
   366 Goal "R ^^ (A Int B) <= R ^^ A Int R ^^ B";
   367 by (Blast_tac 1);
   368 qed "Image_Int_subset";
   369 
   370 Goal "R ^^ (A Un B) = R ^^ A Un R ^^ B";
   371 by (Blast_tac 1);
   372 qed "Image_Un";
   373 
   374 Goal "r <= A Times B ==> r^^C <= B";
   375 by (rtac subsetI 1);
   376 by (REPEAT (eresolve_tac [asm_rl, ImageE, subsetD RS SigmaD2] 1)) ;
   377 qed "Image_subset";
   378 
   379 (*NOT suitable for rewriting*)
   380 Goal "r^^B = (UN y: B. r^^{y})";
   381 by (Blast_tac 1);
   382 qed "Image_eq_UN";
   383 
   384 Goal "[| r'<=r; A'<=A |] ==> (r' ^^ A') <= (r ^^ A)";
   385 by (Blast_tac 1);
   386 qed "Image_mono";
   387 
   388 Goal "(r ^^ (UNION A B)) = (UN x:A.(r ^^ (B x)))";
   389 by (Blast_tac 1);
   390 qed "Image_UN";
   391 
   392 (*Converse inclusion fails*)
   393 Goal "(r ^^ (INTER A B)) <= (INT x:A.(r ^^ (B x)))";
   394 by (Blast_tac 1);
   395 qed "Image_INT_subset";
   396 
   397 Goal "(r^^A <= B) = (A <= - ((r^-1) ^^ (-B)))";
   398 by (Blast_tac 1);
   399 qed "Image_subset_eq";
   400 
   401 section "Univalent";
   402 
   403 Goalw [Univalent_def]
   404      "!x y. (x,y):r --> (!z. (x,z):r --> y=z) ==> Univalent r";
   405 by (assume_tac 1);
   406 qed "UnivalentI";
   407 
   408 Goalw [Univalent_def]
   409      "[| Univalent r;  (x,y):r;  (x,z):r|] ==> y=z";
   410 by Auto_tac;
   411 qed "UnivalentD";
   412 
   413 
   414 (** Graphs of partial functions **)
   415 
   416 Goal "Domain{(x,y). y = f x & P x} = {x. P x}";
   417 by (Blast_tac 1);
   418 qed "Domain_partial_func";
   419 
   420 Goal "Range{(x,y). y = f x & P x} = f``{x. P x}";
   421 by (Blast_tac 1);
   422 qed "Range_partial_func";
   423 
   424 
   425 (** Composition of function and relation **)
   426 
   427 Goalw [fun_rel_comp_def] "A <= B ==> fun_rel_comp f A <= fun_rel_comp f B";
   428 by (Fast_tac 1);
   429 qed "fun_rel_comp_mono";
   430 
   431 Goalw [fun_rel_comp_def] "! x. ?! y. (f x, y) : R ==> ?! g. g : fun_rel_comp f R";
   432 by (res_inst_tac [("a","%x. @y. (f x, y) : R")] ex1I 1);
   433 by (rtac CollectI 1);
   434 by (rtac allI 1);
   435 by (etac allE 1);
   436 by (rtac (select_eq_Ex RS iffD2) 1);
   437 by (etac ex1_implies_ex 1);
   438 by (rtac ext 1);
   439 by (etac CollectE 1);
   440 by (REPEAT (etac allE 1));
   441 by (rtac (select1_equality RS sym) 1);
   442 by (atac 1);
   443 by (atac 1);
   444 qed "fun_rel_comp_unique";