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