src/HOL/Relation.thy
author haftmann
Thu Feb 23 21:25:59 2012 +0100 (2012-02-23)
changeset 46635 cde737f9c911
parent 46372 6fa9cdb8b850
child 46637 0bd7c16a4200
permissions -rw-r--r--
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
     1 (*  Title:      HOL/Relation.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory; Stefan Berghofer, TU Muenchen
     3 *)
     4 
     5 header {* Relations – as sets of pairs, and binary predicates *}
     6 
     7 theory Relation
     8 imports Datatype Finite_Set
     9 begin
    10 
    11 notation
    12   bot ("\<bottom>") and
    13   top ("\<top>") and
    14   inf (infixl "\<sqinter>" 70) and
    15   sup (infixl "\<squnion>" 65) and
    16   Inf ("\<Sqinter>_" [900] 900) and
    17   Sup ("\<Squnion>_" [900] 900)
    18 
    19 syntax (xsymbols)
    20   "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
    21   "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
    22   "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
    23   "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
    24 
    25 
    26 subsection {* Classical rules for reasoning on predicates *}
    27 
    28 declare predicate1D [Pure.dest?, dest?]
    29 declare predicate2I [Pure.intro!, intro!]
    30 declare predicate2D [Pure.dest, dest]
    31 declare bot1E [elim!]
    32 declare bot2E [elim!]
    33 declare top1I [intro!]
    34 declare top2I [intro!]
    35 declare inf1I [intro!]
    36 declare inf2I [intro!]
    37 declare inf1E [elim!]
    38 declare inf2E [elim!]
    39 declare sup1I1 [intro?]
    40 declare sup2I1 [intro?]
    41 declare sup1I2 [intro?]
    42 declare sup2I2 [intro?]
    43 declare sup1E [elim!]
    44 declare sup2E [elim!]
    45 declare sup1CI [intro!]
    46 declare sup2CI [intro!]
    47 declare INF1_I [intro!]
    48 declare INF2_I [intro!]
    49 declare INF1_D [elim]
    50 declare INF2_D [elim]
    51 declare INF1_E [elim]
    52 declare INF2_E [elim]
    53 declare SUP1_I [intro]
    54 declare SUP2_I [intro]
    55 declare SUP1_E [elim!]
    56 declare SUP2_E [elim!]
    57 
    58 
    59 subsection {* Conversions between set and predicate relations *}
    60 
    61 lemma pred_equals_eq [pred_set_conv]: "((\<lambda>x. x \<in> R) = (\<lambda>x. x \<in> S)) \<longleftrightarrow> (R = S)"
    62   by (simp add: set_eq_iff fun_eq_iff)
    63 
    64 lemma pred_equals_eq2 [pred_set_conv]: "((\<lambda>x y. (x, y) \<in> R) = (\<lambda>x y. (x, y) \<in> S)) \<longleftrightarrow> (R = S)"
    65   by (simp add: set_eq_iff fun_eq_iff)
    66 
    67 lemma pred_subset_eq [pred_set_conv]: "((\<lambda>x. x \<in> R) \<le> (\<lambda>x. x \<in> S)) \<longleftrightarrow> (R \<subseteq> S)"
    68   by (simp add: subset_iff le_fun_def)
    69 
    70 lemma pred_subset_eq2 [pred_set_conv]: "((\<lambda>x y. (x, y) \<in> R) \<le> (\<lambda>x y. (x, y) \<in> S)) \<longleftrightarrow> (R \<subseteq> S)"
    71   by (simp add: subset_iff le_fun_def)
    72 
    73 lemma bot_empty_eq: "\<bottom> = (\<lambda>x. x \<in> {})"
    74   by (auto simp add: fun_eq_iff)
    75 
    76 lemma bot_empty_eq2: "\<bottom> = (\<lambda>x y. (x, y) \<in> {})"
    77   by (auto simp add: fun_eq_iff)
    78 
    79 lemma inf_Int_eq [pred_set_conv]: "(\<lambda>x. x \<in> R) \<sqinter> (\<lambda>x. x \<in> S) = (\<lambda>x. x \<in> R \<inter> S)"
    80   by (simp add: inf_fun_def)
    81 
    82 lemma inf_Int_eq2 [pred_set_conv]: "(\<lambda>x y. (x, y) \<in> R) \<sqinter> (\<lambda>x y. (x, y) \<in> S) = (\<lambda>x y. (x, y) \<in> R \<inter> S)"
    83   by (simp add: inf_fun_def)
    84 
    85 lemma sup_Un_eq [pred_set_conv]: "(\<lambda>x. x \<in> R) \<squnion> (\<lambda>x. x \<in> S) = (\<lambda>x. x \<in> R \<union> S)"
    86   by (simp add: sup_fun_def)
    87 
    88 lemma sup_Un_eq2 [pred_set_conv]: "(\<lambda>x y. (x, y) \<in> R) \<squnion> (\<lambda>x y. (x, y) \<in> S) = (\<lambda>x y. (x, y) \<in> R \<union> S)"
    89   by (simp add: sup_fun_def)
    90 
    91 lemma INF_INT_eq: "(\<Sqinter>i. (\<lambda>x. x \<in> r i)) = (\<lambda>x. x \<in> (\<Inter>i. r i))"
    92   by (simp add: INF_apply fun_eq_iff)
    93 
    94 lemma INF_INT_eq2: "(\<Sqinter>i. (\<lambda>x y. (x, y) \<in> r i)) = (\<lambda>x y. (x, y) \<in> (\<Inter>i. r i))"
    95   by (simp add: INF_apply fun_eq_iff)
    96 
    97 lemma SUP_UN_eq [pred_set_conv]: "(\<Squnion>i. (\<lambda>x. x \<in> r i)) = (\<lambda>x. x \<in> (\<Union>i. r i))"
    98   by (simp add: SUP_apply fun_eq_iff)
    99 
   100 lemma SUP_UN_eq2 [pred_set_conv]: "(\<Squnion>i. (\<lambda>x y. (x, y) \<in> r i)) = (\<lambda>x y. (x, y) \<in> (\<Union>i. r i))"
   101   by (simp add: SUP_apply fun_eq_iff)
   102 
   103 
   104 subsection {* Relations as sets of pairs *}
   105 
   106 type_synonym 'a rel = "('a * 'a) set"
   107 
   108 definition
   109   converse :: "('a * 'b) set => ('b * 'a) set"
   110     ("(_^-1)" [1000] 999) where
   111   "r^-1 = {(y, x). (x, y) : r}"
   112 
   113 notation (xsymbols)
   114   converse  ("(_\<inverse>)" [1000] 999)
   115 
   116 definition
   117   rel_comp  :: "[('a * 'b) set, ('b * 'c) set] => ('a * 'c) set"
   118     (infixr "O" 75) where
   119   "r O s = {(x,z). EX y. (x, y) : r & (y, z) : s}"
   120 
   121 definition
   122   Image :: "[('a * 'b) set, 'a set] => 'b set"
   123     (infixl "``" 90) where
   124   "r `` s = {y. EX x:s. (x,y):r}"
   125 
   126 definition
   127   Id :: "('a * 'a) set" where -- {* the identity relation *}
   128   "Id = {p. EX x. p = (x,x)}"
   129 
   130 definition
   131   Id_on  :: "'a set => ('a * 'a) set" where -- {* diagonal: identity over a set *}
   132   "Id_on A = (\<Union>x\<in>A. {(x,x)})"
   133 
   134 definition
   135   Domain :: "('a * 'b) set => 'a set" where
   136   "Domain r = {x. EX y. (x,y):r}"
   137 
   138 definition
   139   Range  :: "('a * 'b) set => 'b set" where
   140   "Range r = Domain(r^-1)"
   141 
   142 definition
   143   Field :: "('a * 'a) set => 'a set" where
   144   "Field r = Domain r \<union> Range r"
   145 
   146 definition
   147   refl_on :: "['a set, ('a * 'a) set] => bool" where -- {* reflexivity over a set *}
   148   "refl_on A r \<longleftrightarrow> r \<subseteq> A \<times> A & (ALL x: A. (x,x) : r)"
   149 
   150 abbreviation
   151   refl :: "('a * 'a) set => bool" where -- {* reflexivity over a type *}
   152   "refl \<equiv> refl_on UNIV"
   153 
   154 definition
   155   sym :: "('a * 'a) set => bool" where -- {* symmetry predicate *}
   156   "sym r \<longleftrightarrow> (ALL x y. (x,y): r --> (y,x): r)"
   157 
   158 definition
   159   antisym :: "('a * 'a) set => bool" where -- {* antisymmetry predicate *}
   160   "antisym r \<longleftrightarrow> (ALL x y. (x,y):r --> (y,x):r --> x=y)"
   161 
   162 definition
   163   trans :: "('a * 'a) set => bool" where -- {* transitivity predicate *}
   164   "trans r \<longleftrightarrow> (ALL x y z. (x,y):r --> (y,z):r --> (x,z):r)"
   165 
   166 definition
   167   irrefl :: "('a * 'a) set => bool" where
   168   "irrefl r \<longleftrightarrow> (\<forall>x. (x,x) \<notin> r)"
   169 
   170 definition
   171   total_on :: "'a set => ('a * 'a) set => bool" where
   172   "total_on A r \<longleftrightarrow> (\<forall>x\<in>A.\<forall>y\<in>A. x\<noteq>y \<longrightarrow> (x,y)\<in>r \<or> (y,x)\<in>r)"
   173 
   174 abbreviation "total \<equiv> total_on UNIV"
   175 
   176 definition
   177   single_valued :: "('a * 'b) set => bool" where
   178   "single_valued r \<longleftrightarrow> (ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z))"
   179 
   180 definition
   181   inv_image :: "('b * 'b) set => ('a => 'b) => ('a * 'a) set" where
   182   "inv_image r f = {(x, y). (f x, f y) : r}"
   183 
   184 
   185 subsubsection {* The identity relation *}
   186 
   187 lemma IdI [intro]: "(a, a) : Id"
   188 by (simp add: Id_def)
   189 
   190 lemma IdE [elim!]: "p : Id ==> (!!x. p = (x, x) ==> P) ==> P"
   191 by (unfold Id_def) (iprover elim: CollectE)
   192 
   193 lemma pair_in_Id_conv [iff]: "((a, b) : Id) = (a = b)"
   194 by (unfold Id_def) blast
   195 
   196 lemma refl_Id: "refl Id"
   197 by (simp add: refl_on_def)
   198 
   199 lemma antisym_Id: "antisym Id"
   200   -- {* A strange result, since @{text Id} is also symmetric. *}
   201 by (simp add: antisym_def)
   202 
   203 lemma sym_Id: "sym Id"
   204 by (simp add: sym_def)
   205 
   206 lemma trans_Id: "trans Id"
   207 by (simp add: trans_def)
   208 
   209 
   210 subsubsection {* Diagonal: identity over a set *}
   211 
   212 lemma Id_on_empty [simp]: "Id_on {} = {}"
   213 by (simp add: Id_on_def) 
   214 
   215 lemma Id_on_eqI: "a = b ==> a : A ==> (a, b) : Id_on A"
   216 by (simp add: Id_on_def)
   217 
   218 lemma Id_onI [intro!,no_atp]: "a : A ==> (a, a) : Id_on A"
   219 by (rule Id_on_eqI) (rule refl)
   220 
   221 lemma Id_onE [elim!]:
   222   "c : Id_on A ==> (!!x. x : A ==> c = (x, x) ==> P) ==> P"
   223   -- {* The general elimination rule. *}
   224 by (unfold Id_on_def) (iprover elim!: UN_E singletonE)
   225 
   226 lemma Id_on_iff: "((x, y) : Id_on A) = (x = y & x : A)"
   227 by blast
   228 
   229 lemma Id_on_def' [nitpick_unfold]:
   230   "Id_on {x. A x} = Collect (\<lambda>(x, y). x = y \<and> A x)"
   231 by auto
   232 
   233 lemma Id_on_subset_Times: "Id_on A \<subseteq> A \<times> A"
   234 by blast
   235 
   236 
   237 subsubsection {* Composition of two relations *}
   238 
   239 lemma rel_compI [intro]:
   240   "(a, b) : r ==> (b, c) : s ==> (a, c) : r O s"
   241 by (unfold rel_comp_def) blast
   242 
   243 lemma rel_compE [elim!]: "xz : r O s ==>
   244   (!!x y z. xz = (x, z) ==> (x, y) : r ==> (y, z) : s  ==> P) ==> P"
   245 by (unfold rel_comp_def) (iprover elim!: CollectE splitE exE conjE)
   246 
   247 lemma rel_compEpair:
   248   "(a, c) : r O s ==> (!!y. (a, y) : r ==> (y, c) : s ==> P) ==> P"
   249 by (iprover elim: rel_compE Pair_inject ssubst)
   250 
   251 lemma R_O_Id [simp]: "R O Id = R"
   252 by fast
   253 
   254 lemma Id_O_R [simp]: "Id O R = R"
   255 by fast
   256 
   257 lemma rel_comp_empty1[simp]: "{} O R = {}"
   258 by blast
   259 
   260 lemma rel_comp_empty2[simp]: "R O {} = {}"
   261 by blast
   262 
   263 lemma O_assoc: "(R O S) O T = R O (S O T)"
   264 by blast
   265 
   266 lemma trans_O_subset: "trans r ==> r O r \<subseteq> r"
   267 by (unfold trans_def) blast
   268 
   269 lemma rel_comp_mono: "r' \<subseteq> r ==> s' \<subseteq> s ==> (r' O s') \<subseteq> (r O s)"
   270 by blast
   271 
   272 lemma rel_comp_subset_Sigma:
   273     "r \<subseteq> A \<times> B ==> s \<subseteq> B \<times> C ==> (r O s) \<subseteq> A \<times> C"
   274 by blast
   275 
   276 lemma rel_comp_distrib[simp]: "R O (S \<union> T) = (R O S) \<union> (R O T)" 
   277 by auto
   278 
   279 lemma rel_comp_distrib2[simp]: "(S \<union> T) O R = (S O R) \<union> (T O R)"
   280 by auto
   281 
   282 lemma rel_comp_UNION_distrib: "s O UNION I r = UNION I (%i. s O r i)"
   283 by auto
   284 
   285 lemma rel_comp_UNION_distrib2: "UNION I r O s = UNION I (%i. r i O s)"
   286 by auto
   287 
   288 
   289 subsubsection {* Reflexivity *}
   290 
   291 lemma refl_onI: "r \<subseteq> A \<times> A ==> (!!x. x : A ==> (x, x) : r) ==> refl_on A r"
   292 by (unfold refl_on_def) (iprover intro!: ballI)
   293 
   294 lemma refl_onD: "refl_on A r ==> a : A ==> (a, a) : r"
   295 by (unfold refl_on_def) blast
   296 
   297 lemma refl_onD1: "refl_on A r ==> (x, y) : r ==> x : A"
   298 by (unfold refl_on_def) blast
   299 
   300 lemma refl_onD2: "refl_on A r ==> (x, y) : r ==> y : A"
   301 by (unfold refl_on_def) blast
   302 
   303 lemma refl_on_Int: "refl_on A r ==> refl_on B s ==> refl_on (A \<inter> B) (r \<inter> s)"
   304 by (unfold refl_on_def) blast
   305 
   306 lemma refl_on_Un: "refl_on A r ==> refl_on B s ==> refl_on (A \<union> B) (r \<union> s)"
   307 by (unfold refl_on_def) blast
   308 
   309 lemma refl_on_INTER:
   310   "ALL x:S. refl_on (A x) (r x) ==> refl_on (INTER S A) (INTER S r)"
   311 by (unfold refl_on_def) fast
   312 
   313 lemma refl_on_UNION:
   314   "ALL x:S. refl_on (A x) (r x) \<Longrightarrow> refl_on (UNION S A) (UNION S r)"
   315 by (unfold refl_on_def) blast
   316 
   317 lemma refl_on_empty[simp]: "refl_on {} {}"
   318 by(simp add:refl_on_def)
   319 
   320 lemma refl_on_Id_on: "refl_on A (Id_on A)"
   321 by (rule refl_onI [OF Id_on_subset_Times Id_onI])
   322 
   323 lemma refl_on_def' [nitpick_unfold, code]:
   324   "refl_on A r = ((\<forall>(x, y) \<in> r. x : A \<and> y : A) \<and> (\<forall>x \<in> A. (x, x) : r))"
   325 by (auto intro: refl_onI dest: refl_onD refl_onD1 refl_onD2)
   326 
   327 
   328 subsubsection {* Antisymmetry *}
   329 
   330 lemma antisymI:
   331   "(!!x y. (x, y) : r ==> (y, x) : r ==> x=y) ==> antisym r"
   332 by (unfold antisym_def) iprover
   333 
   334 lemma antisymD: "antisym r ==> (a, b) : r ==> (b, a) : r ==> a = b"
   335 by (unfold antisym_def) iprover
   336 
   337 lemma antisym_subset: "r \<subseteq> s ==> antisym s ==> antisym r"
   338 by (unfold antisym_def) blast
   339 
   340 lemma antisym_empty [simp]: "antisym {}"
   341 by (unfold antisym_def) blast
   342 
   343 lemma antisym_Id_on [simp]: "antisym (Id_on A)"
   344 by (unfold antisym_def) blast
   345 
   346 
   347 subsubsection {* Symmetry *}
   348 
   349 lemma symI: "(!!a b. (a, b) : r ==> (b, a) : r) ==> sym r"
   350 by (unfold sym_def) iprover
   351 
   352 lemma symD: "sym r ==> (a, b) : r ==> (b, a) : r"
   353 by (unfold sym_def, blast)
   354 
   355 lemma sym_Int: "sym r ==> sym s ==> sym (r \<inter> s)"
   356 by (fast intro: symI dest: symD)
   357 
   358 lemma sym_Un: "sym r ==> sym s ==> sym (r \<union> s)"
   359 by (fast intro: symI dest: symD)
   360 
   361 lemma sym_INTER: "ALL x:S. sym (r x) ==> sym (INTER S r)"
   362 by (fast intro: symI dest: symD)
   363 
   364 lemma sym_UNION: "ALL x:S. sym (r x) ==> sym (UNION S r)"
   365 by (fast intro: symI dest: symD)
   366 
   367 lemma sym_Id_on [simp]: "sym (Id_on A)"
   368 by (rule symI) clarify
   369 
   370 
   371 subsubsection {* Transitivity *}
   372 
   373 lemma trans_join [code]:
   374   "trans r \<longleftrightarrow> (\<forall>(x, y1) \<in> r. \<forall>(y2, z) \<in> r. y1 = y2 \<longrightarrow> (x, z) \<in> r)"
   375   by (auto simp add: trans_def)
   376 
   377 lemma transI:
   378   "(!!x y z. (x, y) : r ==> (y, z) : r ==> (x, z) : r) ==> trans r"
   379 by (unfold trans_def) iprover
   380 
   381 lemma transD: "trans r ==> (a, b) : r ==> (b, c) : r ==> (a, c) : r"
   382 by (unfold trans_def) iprover
   383 
   384 lemma trans_Int: "trans r ==> trans s ==> trans (r \<inter> s)"
   385 by (fast intro: transI elim: transD)
   386 
   387 lemma trans_INTER: "ALL x:S. trans (r x) ==> trans (INTER S r)"
   388 by (fast intro: transI elim: transD)
   389 
   390 lemma trans_Id_on [simp]: "trans (Id_on A)"
   391 by (fast intro: transI elim: transD)
   392 
   393 lemma trans_diff_Id: " trans r \<Longrightarrow> antisym r \<Longrightarrow> trans (r-Id)"
   394 unfolding antisym_def trans_def by blast
   395 
   396 
   397 subsubsection {* Irreflexivity *}
   398 
   399 lemma irrefl_distinct [code]:
   400   "irrefl r \<longleftrightarrow> (\<forall>(x, y) \<in> r. x \<noteq> y)"
   401   by (auto simp add: irrefl_def)
   402 
   403 lemma irrefl_diff_Id[simp]: "irrefl(r-Id)"
   404 by(simp add:irrefl_def)
   405 
   406 
   407 subsubsection {* Totality *}
   408 
   409 lemma total_on_empty[simp]: "total_on {} r"
   410 by(simp add:total_on_def)
   411 
   412 lemma total_on_diff_Id[simp]: "total_on A (r-Id) = total_on A r"
   413 by(simp add: total_on_def)
   414 
   415 
   416 subsubsection {* Converse *}
   417 
   418 lemma converse_iff [iff]: "((a,b): r^-1) = ((b,a) : r)"
   419 by (simp add: converse_def)
   420 
   421 lemma converseI[sym]: "(a, b) : r ==> (b, a) : r^-1"
   422 by (simp add: converse_def)
   423 
   424 lemma converseD[sym]: "(a,b) : r^-1 ==> (b, a) : r"
   425 by (simp add: converse_def)
   426 
   427 lemma converseE [elim!]:
   428   "yx : r^-1 ==> (!!x y. yx = (y, x) ==> (x, y) : r ==> P) ==> P"
   429     -- {* More general than @{text converseD}, as it ``splits'' the member of the relation. *}
   430 by (unfold converse_def) (iprover elim!: CollectE splitE bexE)
   431 
   432 lemma converse_converse [simp]: "(r^-1)^-1 = r"
   433 by (unfold converse_def) blast
   434 
   435 lemma converse_rel_comp: "(r O s)^-1 = s^-1 O r^-1"
   436 by blast
   437 
   438 lemma converse_Int: "(r \<inter> s)^-1 = r^-1 \<inter> s^-1"
   439 by blast
   440 
   441 lemma converse_Un: "(r \<union> s)^-1 = r^-1 \<union> s^-1"
   442 by blast
   443 
   444 lemma converse_INTER: "(INTER S r)^-1 = (INT x:S. (r x)^-1)"
   445 by fast
   446 
   447 lemma converse_UNION: "(UNION S r)^-1 = (UN x:S. (r x)^-1)"
   448 by blast
   449 
   450 lemma converse_Id [simp]: "Id^-1 = Id"
   451 by blast
   452 
   453 lemma converse_Id_on [simp]: "(Id_on A)^-1 = Id_on A"
   454 by blast
   455 
   456 lemma refl_on_converse [simp]: "refl_on A (converse r) = refl_on A r"
   457 by (unfold refl_on_def) auto
   458 
   459 lemma sym_converse [simp]: "sym (converse r) = sym r"
   460 by (unfold sym_def) blast
   461 
   462 lemma antisym_converse [simp]: "antisym (converse r) = antisym r"
   463 by (unfold antisym_def) blast
   464 
   465 lemma trans_converse [simp]: "trans (converse r) = trans r"
   466 by (unfold trans_def) blast
   467 
   468 lemma sym_conv_converse_eq: "sym r = (r^-1 = r)"
   469 by (unfold sym_def) fast
   470 
   471 lemma sym_Un_converse: "sym (r \<union> r^-1)"
   472 by (unfold sym_def) blast
   473 
   474 lemma sym_Int_converse: "sym (r \<inter> r^-1)"
   475 by (unfold sym_def) blast
   476 
   477 lemma total_on_converse[simp]: "total_on A (r^-1) = total_on A r"
   478 by (auto simp: total_on_def)
   479 
   480 
   481 subsubsection {* Domain *}
   482 
   483 declare Domain_def [no_atp]
   484 
   485 lemma Domain_iff: "(a : Domain r) = (EX y. (a, y) : r)"
   486 by (unfold Domain_def) blast
   487 
   488 lemma DomainI [intro]: "(a, b) : r ==> a : Domain r"
   489 by (iprover intro!: iffD2 [OF Domain_iff])
   490 
   491 lemma DomainE [elim!]:
   492   "a : Domain r ==> (!!y. (a, y) : r ==> P) ==> P"
   493 by (iprover dest!: iffD1 [OF Domain_iff])
   494 
   495 lemma Domain_fst [code]:
   496   "Domain r = fst ` r"
   497   by (auto simp add: image_def Bex_def)
   498 
   499 lemma Domain_empty [simp]: "Domain {} = {}"
   500 by blast
   501 
   502 lemma Domain_empty_iff: "Domain r = {} \<longleftrightarrow> r = {}"
   503   by auto
   504 
   505 lemma Domain_insert: "Domain (insert (a, b) r) = insert a (Domain r)"
   506 by blast
   507 
   508 lemma Domain_Id [simp]: "Domain Id = UNIV"
   509 by blast
   510 
   511 lemma Domain_Id_on [simp]: "Domain (Id_on A) = A"
   512 by blast
   513 
   514 lemma Domain_Un_eq: "Domain(A \<union> B) = Domain(A) \<union> Domain(B)"
   515 by blast
   516 
   517 lemma Domain_Int_subset: "Domain(A \<inter> B) \<subseteq> Domain(A) \<inter> Domain(B)"
   518 by blast
   519 
   520 lemma Domain_Diff_subset: "Domain(A) - Domain(B) \<subseteq> Domain(A - B)"
   521 by blast
   522 
   523 lemma Domain_Union: "Domain (Union S) = (\<Union>A\<in>S. Domain A)"
   524 by blast
   525 
   526 lemma Domain_converse[simp]: "Domain(r^-1) = Range r"
   527 by(auto simp:Range_def)
   528 
   529 lemma Domain_mono: "r \<subseteq> s ==> Domain r \<subseteq> Domain s"
   530 by blast
   531 
   532 lemma fst_eq_Domain: "fst ` R = Domain R"
   533   by force
   534 
   535 lemma Domain_dprod [simp]: "Domain (dprod r s) = uprod (Domain r) (Domain s)"
   536 by auto
   537 
   538 lemma Domain_dsum [simp]: "Domain (dsum r s) = usum (Domain r) (Domain s)"
   539 by auto
   540 
   541 
   542 subsubsection {* Range *}
   543 
   544 lemma Range_iff: "(a : Range r) = (EX y. (y, a) : r)"
   545 by (simp add: Domain_def Range_def)
   546 
   547 lemma RangeI [intro]: "(a, b) : r ==> b : Range r"
   548 by (unfold Range_def) (iprover intro!: converseI DomainI)
   549 
   550 lemma RangeE [elim!]: "b : Range r ==> (!!x. (x, b) : r ==> P) ==> P"
   551 by (unfold Range_def) (iprover elim!: DomainE dest!: converseD)
   552 
   553 lemma Range_snd [code]:
   554   "Range r = snd ` r"
   555   by (auto simp add: image_def Bex_def)
   556 
   557 lemma Range_empty [simp]: "Range {} = {}"
   558 by blast
   559 
   560 lemma Range_empty_iff: "Range r = {} \<longleftrightarrow> r = {}"
   561   by auto
   562 
   563 lemma Range_insert: "Range (insert (a, b) r) = insert b (Range r)"
   564 by blast
   565 
   566 lemma Range_Id [simp]: "Range Id = UNIV"
   567 by blast
   568 
   569 lemma Range_Id_on [simp]: "Range (Id_on A) = A"
   570 by auto
   571 
   572 lemma Range_Un_eq: "Range(A \<union> B) = Range(A) \<union> Range(B)"
   573 by blast
   574 
   575 lemma Range_Int_subset: "Range(A \<inter> B) \<subseteq> Range(A) \<inter> Range(B)"
   576 by blast
   577 
   578 lemma Range_Diff_subset: "Range(A) - Range(B) \<subseteq> Range(A - B)"
   579 by blast
   580 
   581 lemma Range_Union: "Range (Union S) = (\<Union>A\<in>S. Range A)"
   582 by blast
   583 
   584 lemma Range_converse[simp]: "Range(r^-1) = Domain r"
   585 by blast
   586 
   587 lemma snd_eq_Range: "snd ` R = Range R"
   588   by force
   589 
   590 
   591 subsubsection {* Field *}
   592 
   593 lemma mono_Field: "r \<subseteq> s \<Longrightarrow> Field r \<subseteq> Field s"
   594 by(auto simp:Field_def Domain_def Range_def)
   595 
   596 lemma Field_empty[simp]: "Field {} = {}"
   597 by(auto simp:Field_def)
   598 
   599 lemma Field_insert[simp]: "Field (insert (a,b) r) = {a,b} \<union> Field r"
   600 by(auto simp:Field_def)
   601 
   602 lemma Field_Un[simp]: "Field (r \<union> s) = Field r \<union> Field s"
   603 by(auto simp:Field_def)
   604 
   605 lemma Field_Union[simp]: "Field (\<Union>R) = \<Union>(Field ` R)"
   606 by(auto simp:Field_def)
   607 
   608 lemma Field_converse[simp]: "Field(r^-1) = Field r"
   609 by(auto simp:Field_def)
   610 
   611 
   612 subsubsection {* Image of a set under a relation *}
   613 
   614 declare Image_def [no_atp]
   615 
   616 lemma Image_iff: "(b : r``A) = (EX x:A. (x, b) : r)"
   617 by (simp add: Image_def)
   618 
   619 lemma Image_singleton: "r``{a} = {b. (a, b) : r}"
   620 by (simp add: Image_def)
   621 
   622 lemma Image_singleton_iff [iff]: "(b : r``{a}) = ((a, b) : r)"
   623 by (rule Image_iff [THEN trans]) simp
   624 
   625 lemma ImageI [intro,no_atp]: "(a, b) : r ==> a : A ==> b : r``A"
   626 by (unfold Image_def) blast
   627 
   628 lemma ImageE [elim!]:
   629     "b : r `` A ==> (!!x. (x, b) : r ==> x : A ==> P) ==> P"
   630 by (unfold Image_def) (iprover elim!: CollectE bexE)
   631 
   632 lemma rev_ImageI: "a : A ==> (a, b) : r ==> b : r `` A"
   633   -- {* This version's more effective when we already have the required @{text a} *}
   634 by blast
   635 
   636 lemma Image_empty [simp]: "R``{} = {}"
   637 by blast
   638 
   639 lemma Image_Id [simp]: "Id `` A = A"
   640 by blast
   641 
   642 lemma Image_Id_on [simp]: "Id_on A `` B = A \<inter> B"
   643 by blast
   644 
   645 lemma Image_Int_subset: "R `` (A \<inter> B) \<subseteq> R `` A \<inter> R `` B"
   646 by blast
   647 
   648 lemma Image_Int_eq:
   649      "single_valued (converse R) ==> R `` (A \<inter> B) = R `` A \<inter> R `` B"
   650 by (simp add: single_valued_def, blast) 
   651 
   652 lemma Image_Un: "R `` (A \<union> B) = R `` A \<union> R `` B"
   653 by blast
   654 
   655 lemma Un_Image: "(R \<union> S) `` A = R `` A \<union> S `` A"
   656 by blast
   657 
   658 lemma Image_subset: "r \<subseteq> A \<times> B ==> r``C \<subseteq> B"
   659 by (iprover intro!: subsetI elim!: ImageE dest!: subsetD SigmaD2)
   660 
   661 lemma Image_eq_UN: "r``B = (\<Union>y\<in> B. r``{y})"
   662   -- {* NOT suitable for rewriting *}
   663 by blast
   664 
   665 lemma Image_mono: "r' \<subseteq> r ==> A' \<subseteq> A ==> (r' `` A') \<subseteq> (r `` A)"
   666 by blast
   667 
   668 lemma Image_UN: "(r `` (UNION A B)) = (\<Union>x\<in>A. r `` (B x))"
   669 by blast
   670 
   671 lemma Image_INT_subset: "(r `` INTER A B) \<subseteq> (\<Inter>x\<in>A. r `` (B x))"
   672 by blast
   673 
   674 text{*Converse inclusion requires some assumptions*}
   675 lemma Image_INT_eq:
   676      "[|single_valued (r\<inverse>); A\<noteq>{}|] ==> r `` INTER A B = (\<Inter>x\<in>A. r `` B x)"
   677 apply (rule equalityI)
   678  apply (rule Image_INT_subset) 
   679 apply  (simp add: single_valued_def, blast)
   680 done
   681 
   682 lemma Image_subset_eq: "(r``A \<subseteq> B) = (A \<subseteq> - ((r^-1) `` (-B)))"
   683 by blast
   684 
   685 
   686 subsubsection {* Single valued relations *}
   687 
   688 lemma single_valuedI:
   689   "ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z) ==> single_valued r"
   690 by (unfold single_valued_def)
   691 
   692 lemma single_valuedD:
   693   "single_valued r ==> (x, y) : r ==> (x, z) : r ==> y = z"
   694 by (simp add: single_valued_def)
   695 
   696 lemma single_valued_rel_comp:
   697   "single_valued r ==> single_valued s ==> single_valued (r O s)"
   698 by (unfold single_valued_def) blast
   699 
   700 lemma single_valued_subset:
   701   "r \<subseteq> s ==> single_valued s ==> single_valued r"
   702 by (unfold single_valued_def) blast
   703 
   704 lemma single_valued_Id [simp]: "single_valued Id"
   705 by (unfold single_valued_def) blast
   706 
   707 lemma single_valued_Id_on [simp]: "single_valued (Id_on A)"
   708 by (unfold single_valued_def) blast
   709 
   710 
   711 subsubsection {* Graphs given by @{text Collect} *}
   712 
   713 lemma Domain_Collect_split [simp]: "Domain{(x,y). P x y} = {x. EX y. P x y}"
   714 by auto
   715 
   716 lemma Range_Collect_split [simp]: "Range{(x,y). P x y} = {y. EX x. P x y}"
   717 by auto
   718 
   719 lemma Image_Collect_split [simp]: "{(x,y). P x y} `` A = {y. EX x:A. P x y}"
   720 by auto
   721 
   722 
   723 subsubsection {* Inverse image *}
   724 
   725 lemma sym_inv_image: "sym r ==> sym (inv_image r f)"
   726 by (unfold sym_def inv_image_def) blast
   727 
   728 lemma trans_inv_image: "trans r ==> trans (inv_image r f)"
   729   apply (unfold trans_def inv_image_def)
   730   apply (simp (no_asm))
   731   apply blast
   732   done
   733 
   734 lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
   735   by (auto simp:inv_image_def)
   736 
   737 lemma converse_inv_image[simp]: "(inv_image R f)^-1 = inv_image (R^-1) f"
   738 unfolding inv_image_def converse_def by auto
   739 
   740 
   741 subsubsection {* Finiteness *}
   742 
   743 lemma finite_converse [iff]: "finite (r^-1) = finite r"
   744   apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
   745    apply simp
   746    apply (rule iffI)
   747     apply (erule finite_imageD [unfolded inj_on_def])
   748     apply (simp split add: split_split)
   749    apply (erule finite_imageI)
   750   apply (simp add: converse_def image_def, auto)
   751   apply (rule bexI)
   752    prefer 2 apply assumption
   753   apply simp
   754   done
   755 
   756 lemma finite_Domain: "finite r ==> finite (Domain r)"
   757   by (induct set: finite) (auto simp add: Domain_insert)
   758 
   759 lemma finite_Range: "finite r ==> finite (Range r)"
   760   by (induct set: finite) (auto simp add: Range_insert)
   761 
   762 lemma finite_Field: "finite r ==> finite (Field r)"
   763   -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
   764   apply (induct set: finite)
   765    apply (auto simp add: Field_def Domain_insert Range_insert)
   766   done
   767 
   768 
   769 subsubsection {* Miscellaneous *}
   770 
   771 text {* Version of @{thm[source] lfp_induct} for binary relations *}
   772 
   773 lemmas lfp_induct2 = 
   774   lfp_induct_set [of "(a, b)", split_format (complete)]
   775 
   776 text {* Version of @{thm[source] subsetI} for binary relations *}
   777 
   778 lemma subrelI: "(\<And>x y. (x, y) \<in> r \<Longrightarrow> (x, y) \<in> s) \<Longrightarrow> r \<subseteq> s"
   779 by auto
   780 
   781 
   782 subsection {* Relations as binary predicates *}
   783 
   784 subsubsection {* Composition *}
   785 
   786 inductive pred_comp  :: "['a \<Rightarrow> 'b \<Rightarrow> bool, 'b \<Rightarrow> 'c \<Rightarrow> bool] \<Rightarrow> 'a \<Rightarrow> 'c \<Rightarrow> bool" (infixr "OO" 75)
   787   for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" and s :: "'b \<Rightarrow> 'c \<Rightarrow> bool" where
   788   pred_compI [intro]: "r a b \<Longrightarrow> s b c \<Longrightarrow> (r OO s) a c"
   789 
   790 inductive_cases pred_compE [elim!]: "(r OO s) a c"
   791 
   792 lemma pred_comp_rel_comp_eq [pred_set_conv]:
   793   "((\<lambda>x y. (x, y) \<in> r) OO (\<lambda>x y. (x, y) \<in> s)) = (\<lambda>x y. (x, y) \<in> r O s)"
   794   by (auto simp add: fun_eq_iff)
   795 
   796 
   797 subsubsection {* Converse *}
   798 
   799 inductive conversep :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> bool" ("(_^--1)" [1000] 1000)
   800   for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where
   801   conversepI: "r a b \<Longrightarrow> r^--1 b a"
   802 
   803 notation (xsymbols)
   804   conversep  ("(_\<inverse>\<inverse>)" [1000] 1000)
   805 
   806 lemma conversepD:
   807   assumes ab: "r^--1 a b"
   808   shows "r b a" using ab
   809   by cases simp
   810 
   811 lemma conversep_iff [iff]: "r^--1 a b = r b a"
   812   by (iprover intro: conversepI dest: conversepD)
   813 
   814 lemma conversep_converse_eq [pred_set_conv]:
   815   "(\<lambda>x y. (x, y) \<in> r)^--1 = (\<lambda>x y. (x, y) \<in> r^-1)"
   816   by (auto simp add: fun_eq_iff)
   817 
   818 lemma conversep_conversep [simp]: "(r^--1)^--1 = r"
   819   by (iprover intro: order_antisym conversepI dest: conversepD)
   820 
   821 lemma converse_pred_comp: "(r OO s)^--1 = s^--1 OO r^--1"
   822   by (iprover intro: order_antisym conversepI pred_compI
   823     elim: pred_compE dest: conversepD)
   824 
   825 lemma converse_meet: "(r \<sqinter> s)^--1 = r^--1 \<sqinter> s^--1"
   826   by (simp add: inf_fun_def) (iprover intro: conversepI ext dest: conversepD)
   827 
   828 lemma converse_join: "(r \<squnion> s)^--1 = r^--1 \<squnion> s^--1"
   829   by (simp add: sup_fun_def) (iprover intro: conversepI ext dest: conversepD)
   830 
   831 lemma conversep_noteq [simp]: "(op \<noteq>)^--1 = op \<noteq>"
   832   by (auto simp add: fun_eq_iff)
   833 
   834 lemma conversep_eq [simp]: "(op =)^--1 = op ="
   835   by (auto simp add: fun_eq_iff)
   836 
   837 
   838 subsubsection {* Domain *}
   839 
   840 inductive DomainP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> bool"
   841   for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where
   842   DomainPI [intro]: "r a b \<Longrightarrow> DomainP r a"
   843 
   844 inductive_cases DomainPE [elim!]: "DomainP r a"
   845 
   846 lemma DomainP_Domain_eq [pred_set_conv]: "DomainP (\<lambda>x y. (x, y) \<in> r) = (\<lambda>x. x \<in> Domain r)"
   847   by (blast intro!: Orderings.order_antisym predicate1I)
   848 
   849 
   850 subsubsection {* Range *}
   851 
   852 inductive RangeP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'b \<Rightarrow> bool"
   853   for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where
   854   RangePI [intro]: "r a b \<Longrightarrow> RangeP r b"
   855 
   856 inductive_cases RangePE [elim!]: "RangeP r b"
   857 
   858 lemma RangeP_Range_eq [pred_set_conv]: "RangeP (\<lambda>x y. (x, y) \<in> r) = (\<lambda>x. x \<in> Range r)"
   859   by (blast intro!: Orderings.order_antisym predicate1I)
   860 
   861 
   862 subsubsection {* Inverse image *}
   863 
   864 definition inv_imagep :: "('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" where
   865   "inv_imagep r f = (\<lambda>x y. r (f x) (f y))"
   866 
   867 lemma [pred_set_conv]: "inv_imagep (\<lambda>x y. (x, y) \<in> r) f = (\<lambda>x y. (x, y) \<in> inv_image r f)"
   868   by (simp add: inv_image_def inv_imagep_def)
   869 
   870 lemma in_inv_imagep [simp]: "inv_imagep r f x y = r (f x) (f y)"
   871   by (simp add: inv_imagep_def)
   872 
   873 
   874 subsubsection {* Powerset *}
   875 
   876 definition Powp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where
   877   "Powp A = (\<lambda>B. \<forall>x \<in> B. A x)"
   878 
   879 lemma Powp_Pow_eq [pred_set_conv]: "Powp (\<lambda>x. x \<in> A) = (\<lambda>x. x \<in> Pow A)"
   880   by (auto simp add: Powp_def fun_eq_iff)
   881 
   882 lemmas Powp_mono [mono] = Pow_mono [to_pred]
   883 
   884 
   885 subsubsection {* Properties of predicate relations *}
   886 
   887 abbreviation antisymP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
   888   "antisymP r \<equiv> antisym {(x, y). r x y}"
   889 
   890 abbreviation transP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
   891   "transP r \<equiv> trans {(x, y). r x y}"
   892 
   893 abbreviation single_valuedP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
   894   "single_valuedP r \<equiv> single_valued {(x, y). r x y}"
   895 
   896 (*FIXME inconsistencies: abbreviations vs. definitions, suffix `P` vs. suffix `p`*)
   897 
   898 definition reflp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
   899   "reflp r \<longleftrightarrow> refl {(x, y). r x y}"
   900 
   901 definition symp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
   902   "symp r \<longleftrightarrow> sym {(x, y). r x y}"
   903 
   904 definition transp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
   905   "transp r \<longleftrightarrow> trans {(x, y). r x y}"
   906 
   907 lemma reflpI:
   908   "(\<And>x. r x x) \<Longrightarrow> reflp r"
   909   by (auto intro: refl_onI simp add: reflp_def)
   910 
   911 lemma reflpE:
   912   assumes "reflp r"
   913   obtains "r x x"
   914   using assms by (auto dest: refl_onD simp add: reflp_def)
   915 
   916 lemma sympI:
   917   "(\<And>x y. r x y \<Longrightarrow> r y x) \<Longrightarrow> symp r"
   918   by (auto intro: symI simp add: symp_def)
   919 
   920 lemma sympE:
   921   assumes "symp r" and "r x y"
   922   obtains "r y x"
   923   using assms by (auto dest: symD simp add: symp_def)
   924 
   925 lemma transpI:
   926   "(\<And>x y z. r x y \<Longrightarrow> r y z \<Longrightarrow> r x z) \<Longrightarrow> transp r"
   927   by (auto intro: transI simp add: transp_def)
   928   
   929 lemma transpE:
   930   assumes "transp r" and "r x y" and "r y z"
   931   obtains "r x z"
   932   using assms by (auto dest: transD simp add: transp_def)
   933 
   934 end