src/HOL/Relation.ML
author berghofe
Fri Jul 16 12:09:48 1999 +0200 (1999-07-16)
changeset 7014 11ee650edcd2
parent 7007 b46ccfee8e59
child 7031 972b5f62f476
permissions -rw-r--r--
Added some definitions and theorems needed for the
construction of datatypes involving function types.
     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 qed_goalw "converseE" thy [converse_def]
   175     "[| yx : r^-1;  \
   176 \       !!x y. [| yx=(y,x);  (x,y):r |] ==> P \
   177 \    |] ==> P"
   178  (fn [major,minor]=>
   179   [ (rtac (major RS CollectE) 1),
   180     (REPEAT (eresolve_tac [splitE, bexE,exE, conjE, minor] 1)),
   181     (assume_tac 1) ]);
   182 
   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 qed_goalw "RangeI" thy [Range_def] "!!a b r.(a,b): r ==> b : Range(r)"
   256  (fn _ => [ (etac (converseI RS DomainI) 1) ]);
   257 
   258 qed_goalw "RangeE" thy [Range_def]
   259     "[| b : Range(r);  !!x. (x,b): r ==> P |] ==> P"
   260  (fn major::prems=>
   261   [ (rtac (major RS DomainE) 1),
   262     (resolve_tac prems 1),
   263     (etac converseD 1) ]);
   264 
   265 AddIs  [RangeI];
   266 AddSEs [RangeE];
   267 
   268 Goal "Range Id = UNIV";
   269 by (Blast_tac 1);
   270 qed "Range_Id";
   271 Addsimps [Range_Id];
   272 
   273 Goal "Range (diag A) = A";
   274 by Auto_tac;
   275 qed "Range_diag";
   276 Addsimps [Range_diag];
   277 
   278 Goal "Range(A Un B) = Range(A) Un Range(B)";
   279 by (Blast_tac 1);
   280 qed "Range_Un_eq";
   281 
   282 Goal "Range(A Int B) <= Range(A) Int Range(B)";
   283 by (Blast_tac 1);
   284 qed "Range_Int_subset";
   285 
   286 Goal "Range(A) - Range(B) <= Range(A - B)";
   287 by (Blast_tac 1);
   288 qed "Range_Diff_subset";
   289 
   290 Goal "Range (Union S) = (UN A:S. Range A)";
   291 by (Blast_tac 1);
   292 qed "Range_Union";
   293 
   294 
   295 (*** Image of a set under a relation ***)
   296 
   297 overload_1st_set "Relation.op ^^";
   298 
   299 qed_goalw "Image_iff" thy [Image_def]
   300     "b : r^^A = (? x:A. (x,b):r)"
   301  (fn _ => [(Blast_tac 1)]);
   302 
   303 qed_goalw "Image_singleton" thy [Image_def]
   304     "r^^{a} = {b. (a,b):r}"
   305  (fn _ => [(Blast_tac 1)]);
   306 
   307 Goal
   308     "(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 qed_goalw "ImageE" thy [Image_def]
   320     "[| b: r^^A;  !!x.[| (x,b): r;  x:A |] ==> P |] ==> P"
   321  (fn major::prems=>
   322   [ (rtac (major RS CollectE) 1),
   323     (Clarify_tac 1),
   324     (rtac (hd prems) 1),
   325     (REPEAT (etac bexE 1 ORELSE ares_tac prems 1)) ]);
   326 
   327 AddIs  [ImageI];
   328 AddSEs [ImageE];
   329 
   330 
   331 Goal
   332     "R^^{} = {}";
   333 by (Blast_tac 1);
   334 qed "Image_empty";
   335 
   336 Addsimps [Image_empty];
   337 
   338 Goal "Id ^^ A = A";
   339 by (Blast_tac 1);
   340 qed "Image_Id";
   341 
   342 Goal "diag A ^^ B = A Int B";
   343 by (Blast_tac 1);
   344 qed "Image_diag";
   345 
   346 Addsimps [Image_Id, Image_diag];
   347 
   348 Goal "R ^^ (A Int B) <= R ^^ A Int R ^^ B";
   349 by (Blast_tac 1);
   350 qed "Image_Int_subset";
   351 
   352 Goal "R ^^ (A Un B) = R ^^ A Un R ^^ B";
   353 by (Blast_tac 1);
   354 qed "Image_Un";
   355 
   356 Goal "r <= A Times B ==> r^^C <= B";
   357 by (rtac subsetI 1);
   358 by (REPEAT (eresolve_tac [asm_rl, ImageE, subsetD RS SigmaD2] 1)) ;
   359 qed "Image_subset";
   360 
   361 (*NOT suitable for rewriting*)
   362 Goal "r^^B = (UN y: B. r^^{y})";
   363 by (Blast_tac 1);
   364 qed "Image_eq_UN";
   365 
   366 
   367 section "Univalent";
   368 
   369 qed_goalw "UnivalentI" Relation.thy [Univalent_def] 
   370    "!!r. !x y. (x,y):r --> (!z. (x,z):r --> y=z) ==> Univalent r" (K [atac 1]);
   371 
   372 qed_goalw "UnivalentD" Relation.thy [Univalent_def] 
   373 	"!!r. [| Univalent r; (x,y):r; (x,z):r|] ==> y=z" (K [Auto_tac]);
   374 
   375 
   376 (** Graphs of partial functions **)
   377 
   378 Goal "Domain{(x,y). y = f x & P x} = {x. P x}";
   379 by (Blast_tac 1);
   380 qed "Domain_partial_func";
   381 
   382 Goal "Range{(x,y). y = f x & P x} = f``{x. P x}";
   383 by (Blast_tac 1);
   384 qed "Range_partial_func";
   385 
   386 
   387 (** Composition of function and relation **)
   388 
   389 Goalw [fun_rel_comp_def] "A <= B ==> fun_rel_comp f A <= fun_rel_comp f B";
   390 by (Fast_tac 1);
   391 qed "fun_rel_comp_mono";
   392 
   393 Goalw [fun_rel_comp_def] "! x. ?! y. (f x, y) : R ==> ?! g. g : fun_rel_comp f R";
   394 by (res_inst_tac [("a","%x. @y. (f x, y) : R")] ex1I 1);
   395 by (rtac CollectI 1);
   396 by (rtac allI 1);
   397 by (etac allE 1);
   398 by (rtac (select_eq_Ex RS iffD2) 1);
   399 by (etac ex1_implies_ex 1);
   400 by (rtac ext 1);
   401 by (etac CollectE 1);
   402 by (REPEAT (etac allE 1));
   403 by (rtac (select1_equality RS sym) 1);
   404 by (atac 1);
   405 by (atac 1);
   406 qed "fun_rel_comp_unique";