1 (* Title: HOL/Relation.thy
2 Author: Lawrence C Paulson, Cambridge University Computer Laboratory; Stefan Berghofer, TU Muenchen
5 header {* Relations – as sets of pairs, and binary predicates *}
8 imports Datatype Finite_Set
14 inf (infixl "\<sqinter>" 70) and
15 sup (infixl "\<squnion>" 65) and
16 Inf ("\<Sqinter>_" [900] 900) and
17 Sup ("\<Squnion>_" [900] 900)
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)
26 subsection {* Classical rules for reasoning on predicates *}
28 (* CANDIDATE declare predicate1I [Pure.intro!, intro!] *)
29 declare predicate1D [Pure.dest?, dest?]
30 (* CANDIDATE declare predicate1D [Pure.dest, dest] *)
31 declare predicate2I [Pure.intro!, intro!]
32 declare predicate2D [Pure.dest, dest]
35 declare top1I [intro!]
36 declare top2I [intro!]
37 declare inf1I [intro!]
38 declare inf2I [intro!]
41 declare sup1I1 [intro?]
42 declare sup2I1 [intro?]
43 declare sup1I2 [intro?]
44 declare sup2I2 [intro?]
47 declare sup1CI [intro!]
48 declare sup2CI [intro!]
49 declare INF1_I [intro!]
50 declare INF2_I [intro!]
55 declare SUP1_I [intro]
56 declare SUP2_I [intro]
57 declare SUP1_E [elim!]
58 declare SUP2_E [elim!]
61 subsection {* Conversions between set and predicate relations *}
63 lemma pred_equals_eq [pred_set_conv]: "((\<lambda>x. x \<in> R) = (\<lambda>x. x \<in> S)) \<longleftrightarrow> (R = S)"
64 by (simp add: set_eq_iff fun_eq_iff)
66 lemma pred_equals_eq2 [pred_set_conv]: "((\<lambda>x y. (x, y) \<in> R) = (\<lambda>x y. (x, y) \<in> S)) \<longleftrightarrow> (R = S)"
67 by (simp add: set_eq_iff fun_eq_iff)
69 lemma pred_subset_eq [pred_set_conv]: "((\<lambda>x. x \<in> R) \<le> (\<lambda>x. x \<in> S)) \<longleftrightarrow> (R \<subseteq> S)"
70 by (simp add: subset_iff le_fun_def)
72 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)"
73 by (simp add: subset_iff le_fun_def)
75 lemma bot_empty_eq (* CANDIDATE [pred_set_conv] *): "\<bottom> = (\<lambda>x. x \<in> {})"
76 by (auto simp add: fun_eq_iff)
78 lemma bot_empty_eq2 (* CANDIDATE [pred_set_conv] *): "\<bottom> = (\<lambda>x y. (x, y) \<in> {})"
79 by (auto simp add: fun_eq_iff)
81 (* CANDIDATE lemma top_empty_eq [pred_set_conv]: "\<top> = (\<lambda>x. x \<in> UNIV)"
82 by (auto simp add: fun_eq_iff) *)
84 (* CANDIDATE lemma top_empty_eq2 [pred_set_conv]: "\<top> = (\<lambda>x y. (x, y) \<in> UNIV)"
85 by (auto simp add: fun_eq_iff) *)
87 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)"
88 by (simp add: inf_fun_def)
90 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)"
91 by (simp add: inf_fun_def)
93 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)"
94 by (simp add: sup_fun_def)
96 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)"
97 by (simp add: sup_fun_def)
99 lemma INF_INT_eq (* CANDIDATE [pred_set_conv] *): "(\<Sqinter>i. (\<lambda>x. x \<in> r i)) = (\<lambda>x. x \<in> (\<Inter>i. r i))"
100 by (simp add: INF_apply fun_eq_iff)
102 lemma INF_INT_eq2 (* CANDIDATE [pred_set_conv] *): "(\<Sqinter>i. (\<lambda>x y. (x, y) \<in> r i)) = (\<lambda>x y. (x, y) \<in> (\<Inter>i. r i))"
103 by (simp add: INF_apply fun_eq_iff)
105 lemma SUP_UN_eq [pred_set_conv]: "(\<Squnion>i. (\<lambda>x. x \<in> r i)) = (\<lambda>x. x \<in> (\<Union>i. r i))"
106 by (simp add: SUP_apply fun_eq_iff)
108 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))"
109 by (simp add: SUP_apply fun_eq_iff)
112 subsection {* Relations as sets of pairs *}
114 type_synonym 'a rel = "('a * 'a) set"
117 converse :: "('a * 'b) set => ('b * 'a) set"
118 ("(_^-1)" [1000] 999) where
119 "r^-1 = {(y, x). (x, y) : r}"
122 converse ("(_\<inverse>)" [1000] 999)
125 rel_comp :: "[('a * 'b) set, ('b * 'c) set] => ('a * 'c) set"
126 (infixr "O" 75) where
127 "r O s = {(x,z). EX y. (x, y) : r & (y, z) : s}"
130 Image :: "[('a * 'b) set, 'a set] => 'b set"
131 (infixl "``" 90) where
132 "r `` s = {y. EX x:s. (x,y):r}"
135 Id :: "('a * 'a) set" where -- {* the identity relation *}
136 "Id = {p. EX x. p = (x,x)}"
139 Id_on :: "'a set => ('a * 'a) set" where -- {* diagonal: identity over a set *}
140 "Id_on A = (\<Union>x\<in>A. {(x,x)})"
143 Domain :: "('a * 'b) set => 'a set" where
144 "Domain r = {x. EX y. (x,y):r}"
147 Range :: "('a * 'b) set => 'b set" where
148 "Range r = Domain(r^-1)"
151 Field :: "('a * 'a) set => 'a set" where
152 "Field r = Domain r \<union> Range r"
155 refl_on :: "['a set, ('a * 'a) set] => bool" where -- {* reflexivity over a set *}
156 "refl_on A r \<longleftrightarrow> r \<subseteq> A \<times> A & (ALL x: A. (x,x) : r)"
159 refl :: "('a * 'a) set => bool" where -- {* reflexivity over a type *}
160 "refl \<equiv> refl_on UNIV"
163 sym :: "('a * 'a) set => bool" where -- {* symmetry predicate *}
164 "sym r \<longleftrightarrow> (ALL x y. (x,y): r --> (y,x): r)"
167 antisym :: "('a * 'a) set => bool" where -- {* antisymmetry predicate *}
168 "antisym r \<longleftrightarrow> (ALL x y. (x,y):r --> (y,x):r --> x=y)"
171 trans :: "('a * 'a) set => bool" where -- {* transitivity predicate *}
172 "trans r \<longleftrightarrow> (ALL x y z. (x,y):r --> (y,z):r --> (x,z):r)"
175 irrefl :: "('a * 'a) set => bool" where
176 "irrefl r \<longleftrightarrow> (\<forall>x. (x,x) \<notin> r)"
179 total_on :: "'a set => ('a * 'a) set => bool" where
180 "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)"
182 abbreviation "total \<equiv> total_on UNIV"
185 single_valued :: "('a * 'b) set => bool" where
186 "single_valued r \<longleftrightarrow> (ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z))"
189 inv_image :: "('b * 'b) set => ('a => 'b) => ('a * 'a) set" where
190 "inv_image r f = {(x, y). (f x, f y) : r}"
193 subsubsection {* The identity relation *}
195 lemma IdI [intro]: "(a, a) : Id"
196 by (simp add: Id_def)
198 lemma IdE [elim!]: "p : Id ==> (!!x. p = (x, x) ==> P) ==> P"
199 by (unfold Id_def) (iprover elim: CollectE)
201 lemma pair_in_Id_conv [iff]: "((a, b) : Id) = (a = b)"
202 by (unfold Id_def) blast
204 lemma refl_Id: "refl Id"
205 by (simp add: refl_on_def)
207 lemma antisym_Id: "antisym Id"
208 -- {* A strange result, since @{text Id} is also symmetric. *}
209 by (simp add: antisym_def)
211 lemma sym_Id: "sym Id"
212 by (simp add: sym_def)
214 lemma trans_Id: "trans Id"
215 by (simp add: trans_def)
218 subsubsection {* Diagonal: identity over a set *}
220 lemma Id_on_empty [simp]: "Id_on {} = {}"
221 by (simp add: Id_on_def)
223 lemma Id_on_eqI: "a = b ==> a : A ==> (a, b) : Id_on A"
224 by (simp add: Id_on_def)
226 lemma Id_onI [intro!,no_atp]: "a : A ==> (a, a) : Id_on A"
227 by (rule Id_on_eqI) (rule refl)
229 lemma Id_onE [elim!]:
230 "c : Id_on A ==> (!!x. x : A ==> c = (x, x) ==> P) ==> P"
231 -- {* The general elimination rule. *}
232 by (unfold Id_on_def) (iprover elim!: UN_E singletonE)
234 lemma Id_on_iff: "((x, y) : Id_on A) = (x = y & x : A)"
237 lemma Id_on_def' [nitpick_unfold]:
238 "Id_on {x. A x} = Collect (\<lambda>(x, y). x = y \<and> A x)"
241 lemma Id_on_subset_Times: "Id_on A \<subseteq> A \<times> A"
245 subsubsection {* Composition of two relations *}
247 lemma rel_compI [intro]:
248 "(a, b) : r ==> (b, c) : s ==> (a, c) : r O s"
249 by (unfold rel_comp_def) blast
251 lemma rel_compE [elim!]: "xz : r O s ==>
252 (!!x y z. xz = (x, z) ==> (x, y) : r ==> (y, z) : s ==> P) ==> P"
253 by (unfold rel_comp_def) (iprover elim!: CollectE splitE exE conjE)
256 "(a, c) : r O s ==> (!!y. (a, y) : r ==> (y, c) : s ==> P) ==> P"
257 by (iprover elim: rel_compE Pair_inject ssubst)
259 lemma R_O_Id [simp]: "R O Id = R"
262 lemma Id_O_R [simp]: "Id O R = R"
265 lemma rel_comp_empty1[simp]: "{} O R = {}"
268 lemma rel_comp_empty2[simp]: "R O {} = {}"
271 lemma O_assoc: "(R O S) O T = R O (S O T)"
274 lemma trans_O_subset: "trans r ==> r O r \<subseteq> r"
275 by (unfold trans_def) blast
277 lemma rel_comp_mono: "r' \<subseteq> r ==> s' \<subseteq> s ==> (r' O s') \<subseteq> (r O s)"
280 lemma rel_comp_subset_Sigma:
281 "r \<subseteq> A \<times> B ==> s \<subseteq> B \<times> C ==> (r O s) \<subseteq> A \<times> C"
284 lemma rel_comp_distrib[simp]: "R O (S \<union> T) = (R O S) \<union> (R O T)"
287 lemma rel_comp_distrib2[simp]: "(S \<union> T) O R = (S O R) \<union> (T O R)"
290 lemma rel_comp_UNION_distrib: "s O UNION I r = UNION I (%i. s O r i)"
293 lemma rel_comp_UNION_distrib2: "UNION I r O s = UNION I (%i. r i O s)"
297 subsubsection {* Reflexivity *}
299 lemma refl_onI: "r \<subseteq> A \<times> A ==> (!!x. x : A ==> (x, x) : r) ==> refl_on A r"
300 by (unfold refl_on_def) (iprover intro!: ballI)
302 lemma refl_onD: "refl_on A r ==> a : A ==> (a, a) : r"
303 by (unfold refl_on_def) blast
305 lemma refl_onD1: "refl_on A r ==> (x, y) : r ==> x : A"
306 by (unfold refl_on_def) blast
308 lemma refl_onD2: "refl_on A r ==> (x, y) : r ==> y : A"
309 by (unfold refl_on_def) blast
311 lemma refl_on_Int: "refl_on A r ==> refl_on B s ==> refl_on (A \<inter> B) (r \<inter> s)"
312 by (unfold refl_on_def) blast
314 lemma refl_on_Un: "refl_on A r ==> refl_on B s ==> refl_on (A \<union> B) (r \<union> s)"
315 by (unfold refl_on_def) blast
318 "ALL x:S. refl_on (A x) (r x) ==> refl_on (INTER S A) (INTER S r)"
319 by (unfold refl_on_def) fast
322 "ALL x:S. refl_on (A x) (r x) \<Longrightarrow> refl_on (UNION S A) (UNION S r)"
323 by (unfold refl_on_def) blast
325 lemma refl_on_empty[simp]: "refl_on {} {}"
326 by(simp add:refl_on_def)
328 lemma refl_on_Id_on: "refl_on A (Id_on A)"
329 by (rule refl_onI [OF Id_on_subset_Times Id_onI])
331 lemma refl_on_def' [nitpick_unfold, code]:
332 "refl_on A r = ((\<forall>(x, y) \<in> r. x : A \<and> y : A) \<and> (\<forall>x \<in> A. (x, x) : r))"
333 by (auto intro: refl_onI dest: refl_onD refl_onD1 refl_onD2)
336 subsubsection {* Antisymmetry *}
339 "(!!x y. (x, y) : r ==> (y, x) : r ==> x=y) ==> antisym r"
340 by (unfold antisym_def) iprover
342 lemma antisymD: "antisym r ==> (a, b) : r ==> (b, a) : r ==> a = b"
343 by (unfold antisym_def) iprover
345 lemma antisym_subset: "r \<subseteq> s ==> antisym s ==> antisym r"
346 by (unfold antisym_def) blast
348 lemma antisym_empty [simp]: "antisym {}"
349 by (unfold antisym_def) blast
351 lemma antisym_Id_on [simp]: "antisym (Id_on A)"
352 by (unfold antisym_def) blast
355 subsubsection {* Symmetry *}
357 lemma symI: "(!!a b. (a, b) : r ==> (b, a) : r) ==> sym r"
358 by (unfold sym_def) iprover
360 lemma symD: "sym r ==> (a, b) : r ==> (b, a) : r"
361 by (unfold sym_def, blast)
363 lemma sym_Int: "sym r ==> sym s ==> sym (r \<inter> s)"
364 by (fast intro: symI dest: symD)
366 lemma sym_Un: "sym r ==> sym s ==> sym (r \<union> s)"
367 by (fast intro: symI dest: symD)
369 lemma sym_INTER: "ALL x:S. sym (r x) ==> sym (INTER S r)"
370 by (fast intro: symI dest: symD)
372 lemma sym_UNION: "ALL x:S. sym (r x) ==> sym (UNION S r)"
373 by (fast intro: symI dest: symD)
375 lemma sym_Id_on [simp]: "sym (Id_on A)"
376 by (rule symI) clarify
379 subsubsection {* Transitivity *}
381 lemma trans_join [code]:
382 "trans r \<longleftrightarrow> (\<forall>(x, y1) \<in> r. \<forall>(y2, z) \<in> r. y1 = y2 \<longrightarrow> (x, z) \<in> r)"
383 by (auto simp add: trans_def)
386 "(!!x y z. (x, y) : r ==> (y, z) : r ==> (x, z) : r) ==> trans r"
387 by (unfold trans_def) iprover
389 lemma transD: "trans r ==> (a, b) : r ==> (b, c) : r ==> (a, c) : r"
390 by (unfold trans_def) iprover
392 lemma trans_Int: "trans r ==> trans s ==> trans (r \<inter> s)"
393 by (fast intro: transI elim: transD)
395 lemma trans_INTER: "ALL x:S. trans (r x) ==> trans (INTER S r)"
396 by (fast intro: transI elim: transD)
398 lemma trans_Id_on [simp]: "trans (Id_on A)"
399 by (fast intro: transI elim: transD)
401 lemma trans_diff_Id: " trans r \<Longrightarrow> antisym r \<Longrightarrow> trans (r-Id)"
402 unfolding antisym_def trans_def by blast
405 subsubsection {* Irreflexivity *}
407 lemma irrefl_distinct [code]:
408 "irrefl r \<longleftrightarrow> (\<forall>(x, y) \<in> r. x \<noteq> y)"
409 by (auto simp add: irrefl_def)
411 lemma irrefl_diff_Id[simp]: "irrefl(r-Id)"
412 by(simp add:irrefl_def)
415 subsubsection {* Totality *}
417 lemma total_on_empty[simp]: "total_on {} r"
418 by(simp add:total_on_def)
420 lemma total_on_diff_Id[simp]: "total_on A (r-Id) = total_on A r"
421 by(simp add: total_on_def)
424 subsubsection {* Converse *}
426 lemma converse_iff [iff]: "((a,b): r^-1) = ((b,a) : r)"
427 by (simp add: converse_def)
429 lemma converseI[sym]: "(a, b) : r ==> (b, a) : r^-1"
430 by (simp add: converse_def)
432 lemma converseD[sym]: "(a,b) : r^-1 ==> (b, a) : r"
433 by (simp add: converse_def)
435 lemma converseE [elim!]:
436 "yx : r^-1 ==> (!!x y. yx = (y, x) ==> (x, y) : r ==> P) ==> P"
437 -- {* More general than @{text converseD}, as it ``splits'' the member of the relation. *}
438 by (unfold converse_def) (iprover elim!: CollectE splitE bexE)
440 lemma converse_converse [simp]: "(r^-1)^-1 = r"
441 by (unfold converse_def) blast
443 lemma converse_rel_comp: "(r O s)^-1 = s^-1 O r^-1"
446 lemma converse_Int: "(r \<inter> s)^-1 = r^-1 \<inter> s^-1"
449 lemma converse_Un: "(r \<union> s)^-1 = r^-1 \<union> s^-1"
452 lemma converse_INTER: "(INTER S r)^-1 = (INT x:S. (r x)^-1)"
455 lemma converse_UNION: "(UNION S r)^-1 = (UN x:S. (r x)^-1)"
458 lemma converse_Id [simp]: "Id^-1 = Id"
461 lemma converse_Id_on [simp]: "(Id_on A)^-1 = Id_on A"
464 lemma refl_on_converse [simp]: "refl_on A (converse r) = refl_on A r"
465 by (unfold refl_on_def) auto
467 lemma sym_converse [simp]: "sym (converse r) = sym r"
468 by (unfold sym_def) blast
470 lemma antisym_converse [simp]: "antisym (converse r) = antisym r"
471 by (unfold antisym_def) blast
473 lemma trans_converse [simp]: "trans (converse r) = trans r"
474 by (unfold trans_def) blast
476 lemma sym_conv_converse_eq: "sym r = (r^-1 = r)"
477 by (unfold sym_def) fast
479 lemma sym_Un_converse: "sym (r \<union> r^-1)"
480 by (unfold sym_def) blast
482 lemma sym_Int_converse: "sym (r \<inter> r^-1)"
483 by (unfold sym_def) blast
485 lemma total_on_converse[simp]: "total_on A (r^-1) = total_on A r"
486 by (auto simp: total_on_def)
489 subsubsection {* Domain *}
491 declare Domain_def [no_atp]
493 lemma Domain_iff: "(a : Domain r) = (EX y. (a, y) : r)"
494 by (unfold Domain_def) blast
496 lemma DomainI [intro]: "(a, b) : r ==> a : Domain r"
497 by (iprover intro!: iffD2 [OF Domain_iff])
499 lemma DomainE [elim!]:
500 "a : Domain r ==> (!!y. (a, y) : r ==> P) ==> P"
501 by (iprover dest!: iffD1 [OF Domain_iff])
503 lemma Domain_fst [code]:
505 by (auto simp add: image_def Bex_def)
507 lemma Domain_empty [simp]: "Domain {} = {}"
510 lemma Domain_empty_iff: "Domain r = {} \<longleftrightarrow> r = {}"
513 lemma Domain_insert: "Domain (insert (a, b) r) = insert a (Domain r)"
516 lemma Domain_Id [simp]: "Domain Id = UNIV"
519 lemma Domain_Id_on [simp]: "Domain (Id_on A) = A"
522 lemma Domain_Un_eq: "Domain(A \<union> B) = Domain(A) \<union> Domain(B)"
525 lemma Domain_Int_subset: "Domain(A \<inter> B) \<subseteq> Domain(A) \<inter> Domain(B)"
528 lemma Domain_Diff_subset: "Domain(A) - Domain(B) \<subseteq> Domain(A - B)"
531 lemma Domain_Union: "Domain (Union S) = (\<Union>A\<in>S. Domain A)"
534 lemma Domain_converse[simp]: "Domain(r^-1) = Range r"
535 by(auto simp:Range_def)
537 lemma Domain_mono: "r \<subseteq> s ==> Domain r \<subseteq> Domain s"
540 lemma fst_eq_Domain: "fst ` R = Domain R"
543 lemma Domain_dprod [simp]: "Domain (dprod r s) = uprod (Domain r) (Domain s)"
546 lemma Domain_dsum [simp]: "Domain (dsum r s) = usum (Domain r) (Domain s)"
550 subsubsection {* Range *}
552 lemma Range_iff: "(a : Range r) = (EX y. (y, a) : r)"
553 by (simp add: Domain_def Range_def)
555 lemma RangeI [intro]: "(a, b) : r ==> b : Range r"
556 by (unfold Range_def) (iprover intro!: converseI DomainI)
558 lemma RangeE [elim!]: "b : Range r ==> (!!x. (x, b) : r ==> P) ==> P"
559 by (unfold Range_def) (iprover elim!: DomainE dest!: converseD)
561 lemma Range_snd [code]:
563 by (auto simp add: image_def Bex_def)
565 lemma Range_empty [simp]: "Range {} = {}"
568 lemma Range_empty_iff: "Range r = {} \<longleftrightarrow> r = {}"
571 lemma Range_insert: "Range (insert (a, b) r) = insert b (Range r)"
574 lemma Range_Id [simp]: "Range Id = UNIV"
577 lemma Range_Id_on [simp]: "Range (Id_on A) = A"
580 lemma Range_Un_eq: "Range(A \<union> B) = Range(A) \<union> Range(B)"
583 lemma Range_Int_subset: "Range(A \<inter> B) \<subseteq> Range(A) \<inter> Range(B)"
586 lemma Range_Diff_subset: "Range(A) - Range(B) \<subseteq> Range(A - B)"
589 lemma Range_Union: "Range (Union S) = (\<Union>A\<in>S. Range A)"
592 lemma Range_converse[simp]: "Range(r^-1) = Domain r"
595 lemma snd_eq_Range: "snd ` R = Range R"
599 subsubsection {* Field *}
601 lemma mono_Field: "r \<subseteq> s \<Longrightarrow> Field r \<subseteq> Field s"
602 by(auto simp:Field_def Domain_def Range_def)
604 lemma Field_empty[simp]: "Field {} = {}"
605 by(auto simp:Field_def)
607 lemma Field_insert[simp]: "Field (insert (a,b) r) = {a,b} \<union> Field r"
608 by(auto simp:Field_def)
610 lemma Field_Un[simp]: "Field (r \<union> s) = Field r \<union> Field s"
611 by(auto simp:Field_def)
613 lemma Field_Union[simp]: "Field (\<Union>R) = \<Union>(Field ` R)"
614 by(auto simp:Field_def)
616 lemma Field_converse[simp]: "Field(r^-1) = Field r"
617 by(auto simp:Field_def)
620 subsubsection {* Image of a set under a relation *}
622 declare Image_def [no_atp]
624 lemma Image_iff: "(b : r``A) = (EX x:A. (x, b) : r)"
625 by (simp add: Image_def)
627 lemma Image_singleton: "r``{a} = {b. (a, b) : r}"
628 by (simp add: Image_def)
630 lemma Image_singleton_iff [iff]: "(b : r``{a}) = ((a, b) : r)"
631 by (rule Image_iff [THEN trans]) simp
633 lemma ImageI [intro,no_atp]: "(a, b) : r ==> a : A ==> b : r``A"
634 by (unfold Image_def) blast
636 lemma ImageE [elim!]:
637 "b : r `` A ==> (!!x. (x, b) : r ==> x : A ==> P) ==> P"
638 by (unfold Image_def) (iprover elim!: CollectE bexE)
640 lemma rev_ImageI: "a : A ==> (a, b) : r ==> b : r `` A"
641 -- {* This version's more effective when we already have the required @{text a} *}
644 lemma Image_empty [simp]: "R``{} = {}"
647 lemma Image_Id [simp]: "Id `` A = A"
650 lemma Image_Id_on [simp]: "Id_on A `` B = A \<inter> B"
653 lemma Image_Int_subset: "R `` (A \<inter> B) \<subseteq> R `` A \<inter> R `` B"
657 "single_valued (converse R) ==> R `` (A \<inter> B) = R `` A \<inter> R `` B"
658 by (simp add: single_valued_def, blast)
660 lemma Image_Un: "R `` (A \<union> B) = R `` A \<union> R `` B"
663 lemma Un_Image: "(R \<union> S) `` A = R `` A \<union> S `` A"
666 lemma Image_subset: "r \<subseteq> A \<times> B ==> r``C \<subseteq> B"
667 by (iprover intro!: subsetI elim!: ImageE dest!: subsetD SigmaD2)
669 lemma Image_eq_UN: "r``B = (\<Union>y\<in> B. r``{y})"
670 -- {* NOT suitable for rewriting *}
673 lemma Image_mono: "r' \<subseteq> r ==> A' \<subseteq> A ==> (r' `` A') \<subseteq> (r `` A)"
676 lemma Image_UN: "(r `` (UNION A B)) = (\<Union>x\<in>A. r `` (B x))"
679 lemma Image_INT_subset: "(r `` INTER A B) \<subseteq> (\<Inter>x\<in>A. r `` (B x))"
682 text{*Converse inclusion requires some assumptions*}
684 "[|single_valued (r\<inverse>); A\<noteq>{}|] ==> r `` INTER A B = (\<Inter>x\<in>A. r `` B x)"
685 apply (rule equalityI)
686 apply (rule Image_INT_subset)
687 apply (simp add: single_valued_def, blast)
690 lemma Image_subset_eq: "(r``A \<subseteq> B) = (A \<subseteq> - ((r^-1) `` (-B)))"
694 subsubsection {* Single valued relations *}
696 lemma single_valuedI:
697 "ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z) ==> single_valued r"
698 by (unfold single_valued_def)
700 lemma single_valuedD:
701 "single_valued r ==> (x, y) : r ==> (x, z) : r ==> y = z"
702 by (simp add: single_valued_def)
704 lemma single_valued_rel_comp:
705 "single_valued r ==> single_valued s ==> single_valued (r O s)"
706 by (unfold single_valued_def) blast
708 lemma single_valued_subset:
709 "r \<subseteq> s ==> single_valued s ==> single_valued r"
710 by (unfold single_valued_def) blast
712 lemma single_valued_Id [simp]: "single_valued Id"
713 by (unfold single_valued_def) blast
715 lemma single_valued_Id_on [simp]: "single_valued (Id_on A)"
716 by (unfold single_valued_def) blast
719 subsubsection {* Graphs given by @{text Collect} *}
721 lemma Domain_Collect_split [simp]: "Domain{(x,y). P x y} = {x. EX y. P x y}"
724 lemma Range_Collect_split [simp]: "Range{(x,y). P x y} = {y. EX x. P x y}"
727 lemma Image_Collect_split [simp]: "{(x,y). P x y} `` A = {y. EX x:A. P x y}"
731 subsubsection {* Inverse image *}
733 lemma sym_inv_image: "sym r ==> sym (inv_image r f)"
734 by (unfold sym_def inv_image_def) blast
736 lemma trans_inv_image: "trans r ==> trans (inv_image r f)"
737 apply (unfold trans_def inv_image_def)
738 apply (simp (no_asm))
742 lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
743 by (auto simp:inv_image_def)
745 lemma converse_inv_image[simp]: "(inv_image R f)^-1 = inv_image (R^-1) f"
746 unfolding inv_image_def converse_def by auto
749 subsubsection {* Finiteness *}
751 lemma finite_converse [iff]: "finite (r^-1) = finite r"
752 apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
755 apply (erule finite_imageD [unfolded inj_on_def])
756 apply (simp split add: split_split)
757 apply (erule finite_imageI)
758 apply (simp add: converse_def image_def, auto)
760 prefer 2 apply assumption
764 lemma finite_Domain: "finite r ==> finite (Domain r)"
765 by (induct set: finite) (auto simp add: Domain_insert)
767 lemma finite_Range: "finite r ==> finite (Range r)"
768 by (induct set: finite) (auto simp add: Range_insert)
770 lemma finite_Field: "finite r ==> finite (Field r)"
771 -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
772 apply (induct set: finite)
773 apply (auto simp add: Field_def Domain_insert Range_insert)
777 subsubsection {* Miscellaneous *}
779 text {* Version of @{thm[source] lfp_induct} for binary relations *}
782 lfp_induct_set [of "(a, b)", split_format (complete)]
784 text {* Version of @{thm[source] subsetI} for binary relations *}
786 lemma subrelI: "(\<And>x y. (x, y) \<in> r \<Longrightarrow> (x, y) \<in> s) \<Longrightarrow> r \<subseteq> s"
790 subsection {* Relations as binary predicates *}
792 subsubsection {* Composition *}
794 inductive pred_comp :: "['a \<Rightarrow> 'b \<Rightarrow> bool, 'b \<Rightarrow> 'c \<Rightarrow> bool] \<Rightarrow> 'a \<Rightarrow> 'c \<Rightarrow> bool" (infixr "OO" 75)
795 for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" and s :: "'b \<Rightarrow> 'c \<Rightarrow> bool" where
796 pred_compI [intro]: "r a b \<Longrightarrow> s b c \<Longrightarrow> (r OO s) a c"
798 inductive_cases pred_compE [elim!]: "(r OO s) a c"
800 lemma pred_comp_rel_comp_eq [pred_set_conv]:
801 "((\<lambda>x y. (x, y) \<in> r) OO (\<lambda>x y. (x, y) \<in> s)) = (\<lambda>x y. (x, y) \<in> r O s)"
802 by (auto simp add: fun_eq_iff)
805 subsubsection {* Converse *}
807 inductive conversep :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> bool" ("(_^--1)" [1000] 1000)
808 for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where
809 conversepI: "r a b \<Longrightarrow> r^--1 b a"
812 conversep ("(_\<inverse>\<inverse>)" [1000] 1000)
815 assumes ab: "r^--1 a b"
816 shows "r b a" using ab
819 lemma conversep_iff [iff]: "r^--1 a b = r b a"
820 by (iprover intro: conversepI dest: conversepD)
822 lemma conversep_converse_eq [pred_set_conv]:
823 "(\<lambda>x y. (x, y) \<in> r)^--1 = (\<lambda>x y. (x, y) \<in> r^-1)"
824 by (auto simp add: fun_eq_iff)
826 lemma conversep_conversep [simp]: "(r^--1)^--1 = r"
827 by (iprover intro: order_antisym conversepI dest: conversepD)
829 lemma converse_pred_comp: "(r OO s)^--1 = s^--1 OO r^--1"
830 by (iprover intro: order_antisym conversepI pred_compI
831 elim: pred_compE dest: conversepD)
833 lemma converse_meet: "(r \<sqinter> s)^--1 = r^--1 \<sqinter> s^--1"
834 by (simp add: inf_fun_def) (iprover intro: conversepI ext dest: conversepD)
836 lemma converse_join: "(r \<squnion> s)^--1 = r^--1 \<squnion> s^--1"
837 by (simp add: sup_fun_def) (iprover intro: conversepI ext dest: conversepD)
839 lemma conversep_noteq [simp]: "(op \<noteq>)^--1 = op \<noteq>"
840 by (auto simp add: fun_eq_iff)
842 lemma conversep_eq [simp]: "(op =)^--1 = op ="
843 by (auto simp add: fun_eq_iff)
846 subsubsection {* Domain *}
848 inductive DomainP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> bool"
849 for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where
850 DomainPI [intro]: "r a b \<Longrightarrow> DomainP r a"
852 inductive_cases DomainPE [elim!]: "DomainP r a"
854 lemma DomainP_Domain_eq [pred_set_conv]: "DomainP (\<lambda>x y. (x, y) \<in> r) = (\<lambda>x. x \<in> Domain r)"
855 by (blast intro!: Orderings.order_antisym predicate1I)
858 subsubsection {* Range *}
860 inductive RangeP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'b \<Rightarrow> bool"
861 for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where
862 RangePI [intro]: "r a b \<Longrightarrow> RangeP r b"
864 inductive_cases RangePE [elim!]: "RangeP r b"
866 lemma RangeP_Range_eq [pred_set_conv]: "RangeP (\<lambda>x y. (x, y) \<in> r) = (\<lambda>x. x \<in> Range r)"
867 by (blast intro!: Orderings.order_antisym predicate1I)
870 subsubsection {* Inverse image *}
872 definition inv_imagep :: "('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" where
873 "inv_imagep r f = (\<lambda>x y. r (f x) (f y))"
875 lemma [pred_set_conv]: "inv_imagep (\<lambda>x y. (x, y) \<in> r) f = (\<lambda>x y. (x, y) \<in> inv_image r f)"
876 by (simp add: inv_image_def inv_imagep_def)
878 lemma in_inv_imagep [simp]: "inv_imagep r f x y = r (f x) (f y)"
879 by (simp add: inv_imagep_def)
882 subsubsection {* Powerset *}
884 definition Powp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where
885 "Powp A = (\<lambda>B. \<forall>x \<in> B. A x)"
887 lemma Powp_Pow_eq [pred_set_conv]: "Powp (\<lambda>x. x \<in> A) = (\<lambda>x. x \<in> Pow A)"
888 by (auto simp add: Powp_def fun_eq_iff)
890 lemmas Powp_mono [mono] = Pow_mono [to_pred]
893 subsubsection {* Properties of predicate relations *}
895 abbreviation antisymP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
896 "antisymP r \<equiv> antisym {(x, y). r x y}"
898 abbreviation transP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
899 "transP r \<equiv> trans {(x, y). r x y}"
901 abbreviation single_valuedP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
902 "single_valuedP r \<equiv> single_valued {(x, y). r x y}"
904 (*FIXME inconsistencies: abbreviations vs. definitions, suffix `P` vs. suffix `p`*)
906 definition reflp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
907 "reflp r \<longleftrightarrow> refl {(x, y). r x y}"
909 definition symp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
910 "symp r \<longleftrightarrow> sym {(x, y). r x y}"
912 definition transp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
913 "transp r \<longleftrightarrow> trans {(x, y). r x y}"
916 "(\<And>x. r x x) \<Longrightarrow> reflp r"
917 by (auto intro: refl_onI simp add: reflp_def)
922 using assms by (auto dest: refl_onD simp add: reflp_def)
925 "(\<And>x y. r x y \<Longrightarrow> r y x) \<Longrightarrow> symp r"
926 by (auto intro: symI simp add: symp_def)
929 assumes "symp r" and "r x y"
931 using assms by (auto dest: symD simp add: symp_def)
934 "(\<And>x y z. r x y \<Longrightarrow> r y z \<Longrightarrow> r x z) \<Longrightarrow> transp r"
935 by (auto intro: transI simp add: transp_def)
938 assumes "transp r" and "r x y" and "r y z"
940 using assms by (auto dest: transD simp add: transp_def)
943 bot ("\<bottom>") and
945 inf (infixl "\<sqinter>" 70) and
946 sup (infixl "\<squnion>" 65) and
947 Inf ("\<Sqinter>_" [900] 900) and
948 Sup ("\<Squnion>_" [900] 900)
951 "_INF1" :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sqinter>_./ _)" [0, 10] 10)
952 "_INF" :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
953 "_SUP1" :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Squnion>_./ _)" [0, 10] 10)
954 "_SUP" :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)