src/HOL/Relation.thy
author nipkow
Wed Aug 18 11:09:40 2004 +0200 (2004-08-18)
changeset 15140 322485b816ac
parent 15131 c69542757a4d
child 15177 e7616269fdca
permissions -rw-r--r--
import -> imports
     1 (*  Title:      HOL/Relation.thy
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1996  University of Cambridge
     5 *)
     6 
     7 header {* Relations *}
     8 
     9 theory Relation
    10 imports Product_Type
    11 begin
    12 
    13 subsection {* Definitions *}
    14 
    15 constdefs
    16   converse :: "('a * 'b) set => ('b * 'a) set"    ("(_^-1)" [1000] 999)
    17   "r^-1 == {(y, x). (x, y) : r}"
    18 syntax (xsymbols)
    19   converse :: "('a * 'b) set => ('b * 'a) set"    ("(_\<inverse>)" [1000] 999)
    20 
    21 constdefs
    22   rel_comp  :: "[('b * 'c) set, ('a * 'b) set] => ('a * 'c) set"  (infixr "O" 60)
    23   "r O s == {(x,z). EX y. (x, y) : s & (y, z) : r}"
    24 
    25   Image :: "[('a * 'b) set, 'a set] => 'b set"                (infixl "``" 90)
    26   "r `` s == {y. EX x:s. (x,y):r}"
    27 
    28   Id    :: "('a * 'a) set"  -- {* the identity relation *}
    29   "Id == {p. EX x. p = (x,x)}"
    30 
    31   diag  :: "'a set => ('a * 'a) set"  -- {* diagonal: identity over a set *}
    32   "diag A == \<Union>x\<in>A. {(x,x)}"
    33 
    34   Domain :: "('a * 'b) set => 'a set"
    35   "Domain r == {x. EX y. (x,y):r}"
    36 
    37   Range  :: "('a * 'b) set => 'b set"
    38   "Range r == Domain(r^-1)"
    39 
    40   Field :: "('a * 'a) set => 'a set"
    41   "Field r == Domain r \<union> Range r"
    42 
    43   refl   :: "['a set, ('a * 'a) set] => bool"  -- {* reflexivity over a set *}
    44   "refl A r == r \<subseteq> A \<times> A & (ALL x: A. (x,x) : r)"
    45 
    46   sym    :: "('a * 'a) set => bool"  -- {* symmetry predicate *}
    47   "sym r == ALL x y. (x,y): r --> (y,x): r"
    48 
    49   antisym:: "('a * 'a) set => bool"  -- {* antisymmetry predicate *}
    50   "antisym r == ALL x y. (x,y):r --> (y,x):r --> x=y"
    51 
    52   trans  :: "('a * 'a) set => bool"  -- {* transitivity predicate *}
    53   "trans r == (ALL x y z. (x,y):r --> (y,z):r --> (x,z):r)"
    54 
    55   single_valued :: "('a * 'b) set => bool"
    56   "single_valued r == ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z)"
    57 
    58   inv_image :: "('b * 'b) set => ('a => 'b) => ('a * 'a) set"
    59   "inv_image r f == {(x, y). (f x, f y) : r}"
    60 
    61 syntax
    62   reflexive :: "('a * 'a) set => bool"  -- {* reflexivity over a type *}
    63 translations
    64   "reflexive" == "refl UNIV"
    65 
    66 
    67 subsection {* The identity relation *}
    68 
    69 lemma IdI [intro]: "(a, a) : Id"
    70   by (simp add: Id_def)
    71 
    72 lemma IdE [elim!]: "p : Id ==> (!!x. p = (x, x) ==> P) ==> P"
    73   by (unfold Id_def) (rules elim: CollectE)
    74 
    75 lemma pair_in_Id_conv [iff]: "((a, b) : Id) = (a = b)"
    76   by (unfold Id_def) blast
    77 
    78 lemma reflexive_Id: "reflexive Id"
    79   by (simp add: refl_def)
    80 
    81 lemma antisym_Id: "antisym Id"
    82   -- {* A strange result, since @{text Id} is also symmetric. *}
    83   by (simp add: antisym_def)
    84 
    85 lemma trans_Id: "trans Id"
    86   by (simp add: trans_def)
    87 
    88 
    89 subsection {* Diagonal: identity over a set *}
    90 
    91 lemma diag_empty [simp]: "diag {} = {}"
    92   by (simp add: diag_def) 
    93 
    94 lemma diag_eqI: "a = b ==> a : A ==> (a, b) : diag A"
    95   by (simp add: diag_def)
    96 
    97 lemma diagI [intro!]: "a : A ==> (a, a) : diag A"
    98   by (rule diag_eqI) (rule refl)
    99 
   100 lemma diagE [elim!]:
   101   "c : diag A ==> (!!x. x : A ==> c = (x, x) ==> P) ==> P"
   102   -- {* The general elimination rule. *}
   103   by (unfold diag_def) (rules elim!: UN_E singletonE)
   104 
   105 lemma diag_iff: "((x, y) : diag A) = (x = y & x : A)"
   106   by blast
   107 
   108 lemma diag_subset_Times: "diag A \<subseteq> A \<times> A"
   109   by blast
   110 
   111 
   112 subsection {* Composition of two relations *}
   113 
   114 lemma rel_compI [intro]:
   115   "(a, b) : s ==> (b, c) : r ==> (a, c) : r O s"
   116   by (unfold rel_comp_def) blast
   117 
   118 lemma rel_compE [elim!]: "xz : r O s ==>
   119   (!!x y z. xz = (x, z) ==> (x, y) : s ==> (y, z) : r  ==> P) ==> P"
   120   by (unfold rel_comp_def) (rules elim!: CollectE splitE exE conjE)
   121 
   122 lemma rel_compEpair:
   123   "(a, c) : r O s ==> (!!y. (a, y) : s ==> (y, c) : r ==> P) ==> P"
   124   by (rules elim: rel_compE Pair_inject ssubst)
   125 
   126 lemma R_O_Id [simp]: "R O Id = R"
   127   by fast
   128 
   129 lemma Id_O_R [simp]: "Id O R = R"
   130   by fast
   131 
   132 lemma O_assoc: "(R O S) O T = R O (S O T)"
   133   by blast
   134 
   135 lemma trans_O_subset: "trans r ==> r O r \<subseteq> r"
   136   by (unfold trans_def) blast
   137 
   138 lemma rel_comp_mono: "r' \<subseteq> r ==> s' \<subseteq> s ==> (r' O s') \<subseteq> (r O s)"
   139   by blast
   140 
   141 lemma rel_comp_subset_Sigma:
   142     "s \<subseteq> A \<times> B ==> r \<subseteq> B \<times> C ==> (r O s) \<subseteq> A \<times> C"
   143   by blast
   144 
   145 
   146 subsection {* Reflexivity *}
   147 
   148 lemma reflI: "r \<subseteq> A \<times> A ==> (!!x. x : A ==> (x, x) : r) ==> refl A r"
   149   by (unfold refl_def) (rules intro!: ballI)
   150 
   151 lemma reflD: "refl A r ==> a : A ==> (a, a) : r"
   152   by (unfold refl_def) blast
   153 
   154 
   155 subsection {* Antisymmetry *}
   156 
   157 lemma antisymI:
   158   "(!!x y. (x, y) : r ==> (y, x) : r ==> x=y) ==> antisym r"
   159   by (unfold antisym_def) rules
   160 
   161 lemma antisymD: "antisym r ==> (a, b) : r ==> (b, a) : r ==> a = b"
   162   by (unfold antisym_def) rules
   163 
   164 
   165 subsection {* Transitivity *}
   166 
   167 lemma transI:
   168   "(!!x y z. (x, y) : r ==> (y, z) : r ==> (x, z) : r) ==> trans r"
   169   by (unfold trans_def) rules
   170 
   171 lemma transD: "trans r ==> (a, b) : r ==> (b, c) : r ==> (a, c) : r"
   172   by (unfold trans_def) rules
   173 
   174 
   175 subsection {* Converse *}
   176 
   177 lemma converse_iff [iff]: "((a,b): r^-1) = ((b,a) : r)"
   178   by (simp add: converse_def)
   179 
   180 lemma converseI[sym]: "(a, b) : r ==> (b, a) : r^-1"
   181   by (simp add: converse_def)
   182 
   183 lemma converseD[sym]: "(a,b) : r^-1 ==> (b, a) : r"
   184   by (simp add: converse_def)
   185 
   186 lemma converseE [elim!]:
   187   "yx : r^-1 ==> (!!x y. yx = (y, x) ==> (x, y) : r ==> P) ==> P"
   188     -- {* More general than @{text converseD}, as it ``splits'' the member of the relation. *}
   189   by (unfold converse_def) (rules elim!: CollectE splitE bexE)
   190 
   191 lemma converse_converse [simp]: "(r^-1)^-1 = r"
   192   by (unfold converse_def) blast
   193 
   194 lemma converse_rel_comp: "(r O s)^-1 = s^-1 O r^-1"
   195   by blast
   196 
   197 lemma converse_Id [simp]: "Id^-1 = Id"
   198   by blast
   199 
   200 lemma converse_diag [simp]: "(diag A)^-1 = diag A"
   201   by blast
   202 
   203 lemma refl_converse: "refl A r ==> refl A (converse r)"
   204   by (unfold refl_def) blast
   205 
   206 lemma antisym_converse: "antisym (converse r) = antisym r"
   207   by (unfold antisym_def) blast
   208 
   209 lemma trans_converse: "trans (converse r) = trans r"
   210   by (unfold trans_def) blast
   211 
   212 
   213 subsection {* Domain *}
   214 
   215 lemma Domain_iff: "(a : Domain r) = (EX y. (a, y) : r)"
   216   by (unfold Domain_def) blast
   217 
   218 lemma DomainI [intro]: "(a, b) : r ==> a : Domain r"
   219   by (rules intro!: iffD2 [OF Domain_iff])
   220 
   221 lemma DomainE [elim!]:
   222   "a : Domain r ==> (!!y. (a, y) : r ==> P) ==> P"
   223   by (rules dest!: iffD1 [OF Domain_iff])
   224 
   225 lemma Domain_empty [simp]: "Domain {} = {}"
   226   by blast
   227 
   228 lemma Domain_insert: "Domain (insert (a, b) r) = insert a (Domain r)"
   229   by blast
   230 
   231 lemma Domain_Id [simp]: "Domain Id = UNIV"
   232   by blast
   233 
   234 lemma Domain_diag [simp]: "Domain (diag A) = A"
   235   by blast
   236 
   237 lemma Domain_Un_eq: "Domain(A \<union> B) = Domain(A) \<union> Domain(B)"
   238   by blast
   239 
   240 lemma Domain_Int_subset: "Domain(A \<inter> B) \<subseteq> Domain(A) \<inter> Domain(B)"
   241   by blast
   242 
   243 lemma Domain_Diff_subset: "Domain(A) - Domain(B) \<subseteq> Domain(A - B)"
   244   by blast
   245 
   246 lemma Domain_Union: "Domain (Union S) = (\<Union>A\<in>S. Domain A)"
   247   by blast
   248 
   249 lemma Domain_mono: "r \<subseteq> s ==> Domain r \<subseteq> Domain s"
   250   by blast
   251 
   252 
   253 subsection {* Range *}
   254 
   255 lemma Range_iff: "(a : Range r) = (EX y. (y, a) : r)"
   256   by (simp add: Domain_def Range_def)
   257 
   258 lemma RangeI [intro]: "(a, b) : r ==> b : Range r"
   259   by (unfold Range_def) (rules intro!: converseI DomainI)
   260 
   261 lemma RangeE [elim!]: "b : Range r ==> (!!x. (x, b) : r ==> P) ==> P"
   262   by (unfold Range_def) (rules elim!: DomainE dest!: converseD)
   263 
   264 lemma Range_empty [simp]: "Range {} = {}"
   265   by blast
   266 
   267 lemma Range_insert: "Range (insert (a, b) r) = insert b (Range r)"
   268   by blast
   269 
   270 lemma Range_Id [simp]: "Range Id = UNIV"
   271   by blast
   272 
   273 lemma Range_diag [simp]: "Range (diag A) = A"
   274   by auto
   275 
   276 lemma Range_Un_eq: "Range(A \<union> B) = Range(A) \<union> Range(B)"
   277   by blast
   278 
   279 lemma Range_Int_subset: "Range(A \<inter> B) \<subseteq> Range(A) \<inter> Range(B)"
   280   by blast
   281 
   282 lemma Range_Diff_subset: "Range(A) - Range(B) \<subseteq> Range(A - B)"
   283   by blast
   284 
   285 lemma Range_Union: "Range (Union S) = (\<Union>A\<in>S. Range A)"
   286   by blast
   287 
   288 
   289 subsection {* Image of a set under a relation *}
   290 
   291 lemma Image_iff: "(b : r``A) = (EX x:A. (x, b) : r)"
   292   by (simp add: Image_def)
   293 
   294 lemma Image_singleton: "r``{a} = {b. (a, b) : r}"
   295   by (simp add: Image_def)
   296 
   297 lemma Image_singleton_iff [iff]: "(b : r``{a}) = ((a, b) : r)"
   298   by (rule Image_iff [THEN trans]) simp
   299 
   300 lemma ImageI [intro]: "(a, b) : r ==> a : A ==> b : r``A"
   301   by (unfold Image_def) blast
   302 
   303 lemma ImageE [elim!]:
   304     "b : r `` A ==> (!!x. (x, b) : r ==> x : A ==> P) ==> P"
   305   by (unfold Image_def) (rules elim!: CollectE bexE)
   306 
   307 lemma rev_ImageI: "a : A ==> (a, b) : r ==> b : r `` A"
   308   -- {* This version's more effective when we already have the required @{text a} *}
   309   by blast
   310 
   311 lemma Image_empty [simp]: "R``{} = {}"
   312   by blast
   313 
   314 lemma Image_Id [simp]: "Id `` A = A"
   315   by blast
   316 
   317 lemma Image_diag [simp]: "diag A `` B = A \<inter> B"
   318   by blast
   319 
   320 lemma Image_Int_subset: "R `` (A \<inter> B) \<subseteq> R `` A \<inter> R `` B"
   321   by blast
   322 
   323 lemma Image_Int_eq:
   324      "single_valued (converse R) ==> R `` (A \<inter> B) = R `` A \<inter> R `` B"
   325   by (simp add: single_valued_def, blast) 
   326 
   327 lemma Image_Un: "R `` (A \<union> B) = R `` A \<union> R `` B"
   328   by blast
   329 
   330 lemma Un_Image: "(R \<union> S) `` A = R `` A \<union> S `` A"
   331   by blast
   332 
   333 lemma Image_subset: "r \<subseteq> A \<times> B ==> r``C \<subseteq> B"
   334   by (rules intro!: subsetI elim!: ImageE dest!: subsetD SigmaD2)
   335 
   336 lemma Image_eq_UN: "r``B = (\<Union>y\<in> B. r``{y})"
   337   -- {* NOT suitable for rewriting *}
   338   by blast
   339 
   340 lemma Image_mono: "r' \<subseteq> r ==> A' \<subseteq> A ==> (r' `` A') \<subseteq> (r `` A)"
   341   by blast
   342 
   343 lemma Image_UN: "(r `` (UNION A B)) = (\<Union>x\<in>A. r `` (B x))"
   344   by blast
   345 
   346 lemma Image_INT_subset: "(r `` INTER A B) \<subseteq> (\<Inter>x\<in>A. r `` (B x))"
   347   by blast
   348 
   349 text{*Converse inclusion requires some assumptions*}
   350 lemma Image_INT_eq:
   351      "[|single_valued (r\<inverse>); A\<noteq>{}|] ==> r `` INTER A B = (\<Inter>x\<in>A. r `` B x)"
   352 apply (rule equalityI)
   353  apply (rule Image_INT_subset) 
   354 apply  (simp add: single_valued_def, blast)
   355 done
   356 
   357 lemma Image_subset_eq: "(r``A \<subseteq> B) = (A \<subseteq> - ((r^-1) `` (-B)))"
   358   by blast
   359 
   360 
   361 subsection {* Single valued relations *}
   362 
   363 lemma single_valuedI:
   364   "ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z) ==> single_valued r"
   365   by (unfold single_valued_def)
   366 
   367 lemma single_valuedD:
   368   "single_valued r ==> (x, y) : r ==> (x, z) : r ==> y = z"
   369   by (simp add: single_valued_def)
   370 
   371 
   372 subsection {* Graphs given by @{text Collect} *}
   373 
   374 lemma Domain_Collect_split [simp]: "Domain{(x,y). P x y} = {x. EX y. P x y}"
   375   by auto
   376 
   377 lemma Range_Collect_split [simp]: "Range{(x,y). P x y} = {y. EX x. P x y}"
   378   by auto
   379 
   380 lemma Image_Collect_split [simp]: "{(x,y). P x y} `` A = {y. EX x:A. P x y}"
   381   by auto
   382 
   383 
   384 subsection {* Inverse image *}
   385 
   386 lemma trans_inv_image: "trans r ==> trans (inv_image r f)"
   387   apply (unfold trans_def inv_image_def)
   388   apply (simp (no_asm))
   389   apply blast
   390   done
   391 
   392 end