more fundamental pred-to-set conversions, particularly by means of inductive_set; associated consolidation of some theorem names (c.f. NEWS)
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
11 text {* A preliminary: classical rules for reasoning on predicates *}
13 (* CANDIDATE declare predicate1I [Pure.intro!, intro!] *)
14 declare predicate1D [Pure.dest?, dest?]
15 (* CANDIDATE declare predicate1D [Pure.dest, dest] *)
16 declare predicate2I [Pure.intro!, intro!]
17 declare predicate2D [Pure.dest, dest]
20 declare top1I [intro!]
21 declare top2I [intro!]
22 declare inf1I [intro!]
23 declare inf2I [intro!]
26 declare sup1I1 [intro?]
27 declare sup2I1 [intro?]
28 declare sup1I2 [intro?]
29 declare sup2I2 [intro?]
32 declare sup1CI [intro!]
33 declare sup2CI [intro!]
34 declare INF1_I [intro!]
35 declare INF2_I [intro!]
40 declare SUP1_I [intro]
41 declare SUP2_I [intro]
42 declare SUP1_E [elim!]
43 declare SUP2_E [elim!]
45 subsection {* Fundamental *}
47 subsubsection {* Relations as sets of pairs *}
49 type_synonym 'a rel = "('a * 'a) set"
51 lemma subrelI: -- {* Version of @{thm [source] subsetI} for binary relations *}
52 "(\<And>x y. (x, y) \<in> r \<Longrightarrow> (x, y) \<in> s) \<Longrightarrow> r \<subseteq> s"
55 lemma lfp_induct2: -- {* Version of @{thm [source] lfp_induct} for binary relations *}
56 "(a, b) \<in> lfp f \<Longrightarrow> mono f \<Longrightarrow>
57 (\<And>a b. (a, b) \<in> f (lfp f \<inter> {(x, y). P x y}) \<Longrightarrow> P a b) \<Longrightarrow> P a b"
58 using lfp_induct_set [of "(a, b)" f "prod_case P"] by auto
61 subsubsection {* 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 {* Properties of relations *}
114 subsubsection {* Reflexivity *}
116 definition refl_on :: "'a set \<Rightarrow> 'a rel \<Rightarrow> bool"
118 "refl_on A r \<longleftrightarrow> r \<subseteq> A \<times> A \<and> (\<forall>x\<in>A. (x, x) \<in> r)"
120 abbreviation refl :: "'a rel \<Rightarrow> bool"
121 where -- {* reflexivity over a type *}
122 "refl \<equiv> refl_on UNIV"
124 definition reflp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
126 "reflp r \<longleftrightarrow> refl {(x, y). r x y}"
128 lemma reflp_refl_eq [pred_set_conv]:
129 "reflp (\<lambda>x y. (x, y) \<in> r) \<longleftrightarrow> refl r"
130 by (simp add: refl_on_def reflp_def)
132 lemma refl_onI: "r \<subseteq> A \<times> A ==> (!!x. x : A ==> (x, x) : r) ==> refl_on A r"
133 by (unfold refl_on_def) (iprover intro!: ballI)
135 lemma refl_onD: "refl_on A r ==> a : A ==> (a, a) : r"
136 by (unfold refl_on_def) blast
138 lemma refl_onD1: "refl_on A r ==> (x, y) : r ==> x : A"
139 by (unfold refl_on_def) blast
141 lemma refl_onD2: "refl_on A r ==> (x, y) : r ==> y : A"
142 by (unfold refl_on_def) blast
145 "(\<And>x. r x x) \<Longrightarrow> reflp r"
146 by (auto intro: refl_onI simp add: reflp_def)
151 using assms by (auto dest: refl_onD simp add: reflp_def)
153 lemma refl_on_Int: "refl_on A r ==> refl_on B s ==> refl_on (A \<inter> B) (r \<inter> s)"
154 by (unfold refl_on_def) blast
157 "reflp r \<Longrightarrow> reflp s \<Longrightarrow> reflp (r \<sqinter> s)"
158 by (auto intro: reflpI elim: reflpE)
160 lemma refl_on_Un: "refl_on A r ==> refl_on B s ==> refl_on (A \<union> B) (r \<union> s)"
161 by (unfold refl_on_def) blast
164 "reflp r \<Longrightarrow> reflp s \<Longrightarrow> reflp (r \<squnion> s)"
165 by (auto intro: reflpI elim: reflpE)
168 "ALL x:S. refl_on (A x) (r x) ==> refl_on (INTER S A) (INTER S r)"
169 by (unfold refl_on_def) fast
172 "ALL x:S. refl_on (A x) (r x) \<Longrightarrow> refl_on (UNION S A) (UNION S r)"
173 by (unfold refl_on_def) blast
175 lemma refl_on_empty [simp]: "refl_on {} {}"
176 by (simp add:refl_on_def)
178 lemma refl_on_def' [nitpick_unfold, code]:
179 "refl_on A r \<longleftrightarrow> (\<forall>(x, y) \<in> r. x \<in> A \<and> y \<in> A) \<and> (\<forall>x \<in> A. (x, x) \<in> r)"
180 by (auto intro: refl_onI dest: refl_onD refl_onD1 refl_onD2)
183 subsubsection {* Irreflexivity *}
185 definition irrefl :: "'a rel \<Rightarrow> bool"
187 "irrefl r \<longleftrightarrow> (\<forall>x. (x, x) \<notin> r)"
189 lemma irrefl_distinct [code]:
190 "irrefl r \<longleftrightarrow> (\<forall>(x, y) \<in> r. x \<noteq> y)"
191 by (auto simp add: irrefl_def)
194 subsubsection {* Symmetry *}
196 definition sym :: "'a rel \<Rightarrow> bool"
198 "sym r \<longleftrightarrow> (\<forall>x y. (x, y) \<in> r \<longrightarrow> (y, x) \<in> r)"
200 definition symp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
202 "symp r \<longleftrightarrow> (\<forall>x y. r x y \<longrightarrow> r y x)"
204 lemma symp_sym_eq [pred_set_conv]:
205 "symp (\<lambda>x y. (x, y) \<in> r) \<longleftrightarrow> sym r"
206 by (simp add: sym_def symp_def)
209 "(\<And>a b. (a, b) \<in> r \<Longrightarrow> (b, a) \<in> r) \<Longrightarrow> sym r"
210 by (unfold sym_def) iprover
213 "(\<And>a b. r a b \<Longrightarrow> r b a) \<Longrightarrow> symp r"
214 by (fact symI [to_pred])
217 assumes "sym r" and "(b, a) \<in> r"
218 obtains "(a, b) \<in> r"
219 using assms by (simp add: sym_def)
222 assumes "symp r" and "r b a"
224 using assms by (rule symE [to_pred])
227 assumes "sym r" and "(b, a) \<in> r"
228 shows "(a, b) \<in> r"
229 using assms by (rule symE)
232 assumes "symp r" and "r b a"
234 using assms by (rule symD [to_pred])
237 "sym r \<Longrightarrow> sym s \<Longrightarrow> sym (r \<inter> s)"
238 by (fast intro: symI elim: symE)
241 "symp r \<Longrightarrow> symp s \<Longrightarrow> symp (r \<sqinter> s)"
242 by (fact sym_Int [to_pred])
245 "sym r \<Longrightarrow> sym s \<Longrightarrow> sym (r \<union> s)"
246 by (fast intro: symI elim: symE)
249 "symp r \<Longrightarrow> symp s \<Longrightarrow> symp (r \<squnion> s)"
250 by (fact sym_Un [to_pred])
253 "\<forall>x\<in>S. sym (r x) \<Longrightarrow> sym (INTER S r)"
254 by (fast intro: symI elim: symE)
256 (* FIXME thm sym_INTER [to_pred] *)
259 "\<forall>x\<in>S. sym (r x) \<Longrightarrow> sym (UNION S r)"
260 by (fast intro: symI elim: symE)
262 (* FIXME thm sym_UNION [to_pred] *)
265 subsubsection {* Antisymmetry *}
267 definition antisym :: "'a rel \<Rightarrow> bool"
269 "antisym r \<longleftrightarrow> (\<forall>x y. (x, y) \<in> r \<longrightarrow> (y, x) \<in> r \<longrightarrow> x = y)"
271 abbreviation antisymP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
273 "antisymP r \<equiv> antisym {(x, y). r x y}"
276 "(!!x y. (x, y) : r ==> (y, x) : r ==> x=y) ==> antisym r"
277 by (unfold antisym_def) iprover
279 lemma antisymD: "antisym r ==> (a, b) : r ==> (b, a) : r ==> a = b"
280 by (unfold antisym_def) iprover
282 lemma antisym_subset: "r \<subseteq> s ==> antisym s ==> antisym r"
283 by (unfold antisym_def) blast
285 lemma antisym_empty [simp]: "antisym {}"
286 by (unfold antisym_def) blast
289 subsubsection {* Transitivity *}
291 definition trans :: "'a rel \<Rightarrow> bool"
293 "trans r \<longleftrightarrow> (\<forall>x y z. (x, y) \<in> r \<longrightarrow> (y, z) \<in> r \<longrightarrow> (x, z) \<in> r)"
295 definition transp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
297 "transp r \<longleftrightarrow> (\<forall>x y z. r x y \<longrightarrow> r y z \<longrightarrow> r x z)"
299 lemma transp_trans_eq [pred_set_conv]:
300 "transp (\<lambda>x y. (x, y) \<in> r) \<longleftrightarrow> trans r"
301 by (simp add: trans_def transp_def)
303 abbreviation transP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
304 where -- {* FIXME drop *}
305 "transP r \<equiv> trans {(x, y). r x y}"
308 "(\<And>x y z. (x, y) \<in> r \<Longrightarrow> (y, z) \<in> r \<Longrightarrow> (x, z) \<in> r) \<Longrightarrow> trans r"
309 by (unfold trans_def) iprover
312 "(\<And>x y z. r x y \<Longrightarrow> r y z \<Longrightarrow> r x z) \<Longrightarrow> transp r"
313 by (fact transI [to_pred])
316 assumes "trans r" and "(x, y) \<in> r" and "(y, z) \<in> r"
317 obtains "(x, z) \<in> r"
318 using assms by (unfold trans_def) iprover
321 assumes "transp r" and "r x y" and "r y z"
323 using assms by (rule transE [to_pred])
326 assumes "trans r" and "(x, y) \<in> r" and "(y, z) \<in> r"
327 shows "(x, z) \<in> r"
328 using assms by (rule transE)
331 assumes "transp r" and "r x y" and "r y z"
333 using assms by (rule transD [to_pred])
336 "trans r \<Longrightarrow> trans s \<Longrightarrow> trans (r \<inter> s)"
337 by (fast intro: transI elim: transE)
340 "transp r \<Longrightarrow> transp s \<Longrightarrow> transp (r \<sqinter> s)"
341 by (fact trans_Int [to_pred])
344 "\<forall>x\<in>S. trans (r x) \<Longrightarrow> trans (INTER S r)"
345 by (fast intro: transI elim: transD)
347 (* FIXME thm trans_INTER [to_pred] *)
349 lemma trans_join [code]:
350 "trans r \<longleftrightarrow> (\<forall>(x, y1) \<in> r. \<forall>(y2, z) \<in> r. y1 = y2 \<longrightarrow> (x, z) \<in> r)"
351 by (auto simp add: trans_def)
354 "transp r \<longleftrightarrow> trans {(x, y). r x y}"
355 by (simp add: trans_def transp_def)
358 subsubsection {* Totality *}
360 definition total_on :: "'a set \<Rightarrow> 'a rel \<Rightarrow> bool"
362 "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)"
364 abbreviation "total \<equiv> total_on UNIV"
366 lemma total_on_empty [simp]: "total_on {} r"
367 by (simp add: total_on_def)
370 subsubsection {* Single valued relations *}
372 definition single_valued :: "('a \<times> 'b) set \<Rightarrow> bool"
374 "single_valued r \<longleftrightarrow> (\<forall>x y. (x, y) \<in> r \<longrightarrow> (\<forall>z. (x, z) \<in> r \<longrightarrow> y = z))"
376 abbreviation single_valuedP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
377 "single_valuedP r \<equiv> single_valued {(x, y). r x y}"
379 lemma single_valuedI:
380 "ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z) ==> single_valued r"
381 by (unfold single_valued_def)
383 lemma single_valuedD:
384 "single_valued r ==> (x, y) : r ==> (x, z) : r ==> y = z"
385 by (simp add: single_valued_def)
387 lemma single_valued_subset:
388 "r \<subseteq> s ==> single_valued s ==> single_valued r"
389 by (unfold single_valued_def) blast
392 subsection {* Relation operations *}
394 subsubsection {* The identity relation *}
396 definition Id :: "'a rel"
398 "Id = {p. \<exists>x. p = (x, x)}"
400 lemma IdI [intro]: "(a, a) : Id"
401 by (simp add: Id_def)
403 lemma IdE [elim!]: "p : Id ==> (!!x. p = (x, x) ==> P) ==> P"
404 by (unfold Id_def) (iprover elim: CollectE)
406 lemma pair_in_Id_conv [iff]: "((a, b) : Id) = (a = b)"
407 by (unfold Id_def) blast
409 lemma refl_Id: "refl Id"
410 by (simp add: refl_on_def)
412 lemma antisym_Id: "antisym Id"
413 -- {* A strange result, since @{text Id} is also symmetric. *}
414 by (simp add: antisym_def)
416 lemma sym_Id: "sym Id"
417 by (simp add: sym_def)
419 lemma trans_Id: "trans Id"
420 by (simp add: trans_def)
422 lemma single_valued_Id [simp]: "single_valued Id"
423 by (unfold single_valued_def) blast
425 lemma irrefl_diff_Id [simp]: "irrefl (r - Id)"
426 by (simp add:irrefl_def)
428 lemma trans_diff_Id: "trans r \<Longrightarrow> antisym r \<Longrightarrow> trans (r - Id)"
429 unfolding antisym_def trans_def by blast
431 lemma total_on_diff_Id [simp]: "total_on A (r - Id) = total_on A r"
432 by (simp add: total_on_def)
435 subsubsection {* Diagonal: identity over a set *}
437 definition Id_on :: "'a set \<Rightarrow> 'a rel"
439 "Id_on A = (\<Union>x\<in>A. {(x, x)})"
441 lemma Id_on_empty [simp]: "Id_on {} = {}"
442 by (simp add: Id_on_def)
444 lemma Id_on_eqI: "a = b ==> a : A ==> (a, b) : Id_on A"
445 by (simp add: Id_on_def)
447 lemma Id_onI [intro!,no_atp]: "a : A ==> (a, a) : Id_on A"
448 by (rule Id_on_eqI) (rule refl)
450 lemma Id_onE [elim!]:
451 "c : Id_on A ==> (!!x. x : A ==> c = (x, x) ==> P) ==> P"
452 -- {* The general elimination rule. *}
453 by (unfold Id_on_def) (iprover elim!: UN_E singletonE)
455 lemma Id_on_iff: "((x, y) : Id_on A) = (x = y & x : A)"
458 lemma Id_on_def' [nitpick_unfold]:
459 "Id_on {x. A x} = Collect (\<lambda>(x, y). x = y \<and> A x)"
462 lemma Id_on_subset_Times: "Id_on A \<subseteq> A \<times> A"
465 lemma refl_on_Id_on: "refl_on A (Id_on A)"
466 by (rule refl_onI [OF Id_on_subset_Times Id_onI])
468 lemma antisym_Id_on [simp]: "antisym (Id_on A)"
469 by (unfold antisym_def) blast
471 lemma sym_Id_on [simp]: "sym (Id_on A)"
472 by (rule symI) clarify
474 lemma trans_Id_on [simp]: "trans (Id_on A)"
475 by (fast intro: transI elim: transD)
477 lemma single_valued_Id_on [simp]: "single_valued (Id_on A)"
478 by (unfold single_valued_def) blast
481 subsubsection {* Composition *}
483 inductive_set rel_comp :: "('a \<times> 'b) set \<Rightarrow> ('b \<times> 'c) set \<Rightarrow> ('a \<times> 'c) set" (infixr "O" 75)
484 for r :: "('a \<times> 'b) set" and s :: "('b \<times> 'c) set"
486 rel_compI [intro]: "(a, b) \<in> r \<Longrightarrow> (b, c) \<in> s \<Longrightarrow> (a, c) \<in> r O s"
488 abbreviation pred_comp (infixr "OO" 75) where
489 "pred_comp \<equiv> rel_compp"
491 lemmas pred_compI = rel_compp.intros
494 For historic reasons, the elimination rules are not wholly corresponding.
495 Feel free to consolidate this.
498 inductive_cases rel_compEpair: "(a, c) \<in> r O s"
499 inductive_cases pred_compE [elim!]: "(r OO s) a c"
501 lemma rel_compE [elim!]: "xz \<in> r O s \<Longrightarrow>
502 (\<And>x y z. xz = (x, z) \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> (y, z) \<in> s \<Longrightarrow> P) \<Longrightarrow> P"
503 by (cases xz) (simp, erule rel_compEpair, iprover)
505 lemmas pred_comp_rel_comp_eq = rel_compp_rel_comp_eq
515 lemma rel_comp_empty1 [simp]:
519 (* CANDIDATE lemma pred_comp_bot1 [simp]:
521 by (fact rel_comp_empty1 [to_pred]) *)
523 lemma rel_comp_empty2 [simp]:
527 (* CANDIDATE lemma pred_comp_bot2 [simp]:
529 by (fact rel_comp_empty2 [to_pred]) *)
532 "(R O S) O T = R O (S O T)"
535 lemma pred_comp_assoc:
536 "(r OO s) OO t = r OO (s OO t)"
537 by (fact O_assoc [to_pred])
539 lemma trans_O_subset:
540 "trans r \<Longrightarrow> r O r \<subseteq> r"
541 by (unfold trans_def) blast
543 lemma transp_pred_comp_less_eq:
544 "transp r \<Longrightarrow> r OO r \<le> r "
545 by (fact trans_O_subset [to_pred])
548 "r' \<subseteq> r \<Longrightarrow> s' \<subseteq> s \<Longrightarrow> r' O s' \<subseteq> r O s"
551 lemma pred_comp_mono:
552 "r' \<le> r \<Longrightarrow> s' \<le> s \<Longrightarrow> r' OO s' \<le> r OO s "
553 by (fact rel_comp_mono [to_pred])
555 lemma rel_comp_subset_Sigma:
556 "r \<subseteq> A \<times> B \<Longrightarrow> s \<subseteq> B \<times> C \<Longrightarrow> r O s \<subseteq> A \<times> C"
559 lemma rel_comp_distrib [simp]:
560 "R O (S \<union> T) = (R O S) \<union> (R O T)"
563 lemma pred_comp_distrib (* CANDIDATE [simp] *):
564 "R OO (S \<squnion> T) = R OO S \<squnion> R OO T"
565 by (fact rel_comp_distrib [to_pred])
567 lemma rel_comp_distrib2 [simp]:
568 "(S \<union> T) O R = (S O R) \<union> (T O R)"
571 lemma pred_comp_distrib2 (* CANDIDATE [simp] *):
572 "(S \<squnion> T) OO R = S OO R \<squnion> T OO R"
573 by (fact rel_comp_distrib2 [to_pred])
575 lemma rel_comp_UNION_distrib:
576 "s O UNION I r = (\<Union>i\<in>I. s O r i) "
579 (* FIXME thm rel_comp_UNION_distrib [to_pred] *)
581 lemma rel_comp_UNION_distrib2:
582 "UNION I r O s = (\<Union>i\<in>I. r i O s) "
585 (* FIXME thm rel_comp_UNION_distrib2 [to_pred] *)
587 lemma single_valued_rel_comp:
588 "single_valued r \<Longrightarrow> single_valued s \<Longrightarrow> single_valued (r O s)"
589 by (unfold single_valued_def) blast
591 lemma rel_comp_unfold:
592 "r O s = {(x, z). \<exists>y. (x, y) \<in> r \<and> (y, z) \<in> s}"
593 by (auto simp add: set_eq_iff)
596 subsubsection {* Converse *}
598 inductive_set converse :: "('a \<times> 'b) set \<Rightarrow> ('b \<times> 'a) set" ("(_^-1)" [1000] 999)
599 for r :: "('a \<times> 'b) set"
601 "(a, b) \<in> r \<Longrightarrow> (b, a) \<in> r^-1"
604 converse ("(_\<inverse>)" [1000] 999)
607 conversep ("(_^--1)" [1000] 1000)
610 conversep ("(_\<inverse>\<inverse>)" [1000] 1000)
612 lemma converseI [sym]:
613 "(a, b) \<in> r \<Longrightarrow> (b, a) \<in> r\<inverse>"
614 by (fact converse.intros)
616 lemma conversepI (* CANDIDATE [sym] *):
617 "r a b \<Longrightarrow> r\<inverse>\<inverse> b a"
618 by (fact conversep.intros)
620 lemma converseD [sym]:
621 "(a, b) \<in> r\<inverse> \<Longrightarrow> (b, a) \<in> r"
622 by (erule converse.cases) iprover
624 lemma conversepD (* CANDIDATE [sym] *):
625 "r\<inverse>\<inverse> b a \<Longrightarrow> r a b"
626 by (fact converseD [to_pred])
628 lemma converseE [elim!]:
629 -- {* More general than @{text converseD}, as it ``splits'' the member of the relation. *}
630 "yx \<in> r\<inverse> \<Longrightarrow> (\<And>x y. yx = (y, x) \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> P) \<Longrightarrow> P"
631 by (cases yx) (simp, erule converse.cases, iprover)
633 lemmas conversepE (* CANDIDATE [elim!] *) = conversep.cases
635 lemma converse_iff [iff]:
636 "(a, b) \<in> r\<inverse> \<longleftrightarrow> (b, a) \<in> r"
637 by (auto intro: converseI)
639 lemma conversep_iff [iff]:
640 "r\<inverse>\<inverse> a b = r b a"
641 by (fact converse_iff [to_pred])
643 lemma converse_converse [simp]:
644 "(r\<inverse>)\<inverse> = r"
645 by (simp add: set_eq_iff)
647 lemma conversep_conversep [simp]:
648 "(r\<inverse>\<inverse>)\<inverse>\<inverse> = r"
649 by (fact converse_converse [to_pred])
651 lemma converse_rel_comp: "(r O s)^-1 = s^-1 O r^-1"
654 lemma converse_pred_comp: "(r OO s)^--1 = s^--1 OO r^--1"
655 by (iprover intro: order_antisym conversepI pred_compI
656 elim: pred_compE dest: conversepD)
658 lemma converse_Int: "(r \<inter> s)^-1 = r^-1 \<inter> s^-1"
661 lemma converse_meet: "(r \<sqinter> s)^--1 = r^--1 \<sqinter> s^--1"
662 by (simp add: inf_fun_def) (iprover intro: conversepI ext dest: conversepD)
664 lemma converse_Un: "(r \<union> s)^-1 = r^-1 \<union> s^-1"
667 lemma converse_join: "(r \<squnion> s)^--1 = r^--1 \<squnion> s^--1"
668 by (simp add: sup_fun_def) (iprover intro: conversepI ext dest: conversepD)
670 lemma converse_INTER: "(INTER S r)^-1 = (INT x:S. (r x)^-1)"
673 lemma converse_UNION: "(UNION S r)^-1 = (UN x:S. (r x)^-1)"
676 lemma converse_Id [simp]: "Id^-1 = Id"
679 lemma converse_Id_on [simp]: "(Id_on A)^-1 = Id_on A"
682 lemma refl_on_converse [simp]: "refl_on A (converse r) = refl_on A r"
683 by (unfold refl_on_def) auto
685 lemma sym_converse [simp]: "sym (converse r) = sym r"
686 by (unfold sym_def) blast
688 lemma antisym_converse [simp]: "antisym (converse r) = antisym r"
689 by (unfold antisym_def) blast
691 lemma trans_converse [simp]: "trans (converse r) = trans r"
692 by (unfold trans_def) blast
694 lemma sym_conv_converse_eq: "sym r = (r^-1 = r)"
695 by (unfold sym_def) fast
697 lemma sym_Un_converse: "sym (r \<union> r^-1)"
698 by (unfold sym_def) blast
700 lemma sym_Int_converse: "sym (r \<inter> r^-1)"
701 by (unfold sym_def) blast
703 lemma total_on_converse [simp]: "total_on A (r^-1) = total_on A r"
704 by (auto simp: total_on_def)
706 lemma finite_converse [iff]: "finite (r^-1) = finite r"
707 apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
710 apply (erule finite_imageD [unfolded inj_on_def])
711 apply (simp split add: split_split)
712 apply (erule finite_imageI)
713 apply (simp add: set_eq_iff image_def, auto)
715 prefer 2 apply assumption
719 lemma conversep_noteq [simp]: "(op \<noteq>)^--1 = op \<noteq>"
720 by (auto simp add: fun_eq_iff)
722 lemma conversep_eq [simp]: "(op =)^--1 = op ="
723 by (auto simp add: fun_eq_iff)
725 lemma converse_unfold:
726 "r\<inverse> = {(y, x). (x, y) \<in> r}"
727 by (simp add: set_eq_iff)
730 subsubsection {* Domain, range and field *}
732 definition Domain :: "('a \<times> 'b) set \<Rightarrow> 'a set"
734 "Domain r = {x. \<exists>y. (x, y) \<in> r}"
736 definition Range :: "('a \<times> 'b) set \<Rightarrow> 'b set"
738 "Range r = Domain (r\<inverse>)"
740 definition Field :: "'a rel \<Rightarrow> 'a set"
742 "Field r = Domain r \<union> Range r"
744 declare Domain_def [no_atp]
746 lemma Domain_iff: "(a : Domain r) = (EX y. (a, y) : r)"
747 by (unfold Domain_def) blast
749 lemma DomainI [intro]: "(a, b) : r ==> a : Domain r"
750 by (iprover intro!: iffD2 [OF Domain_iff])
752 lemma DomainE [elim!]:
753 "a : Domain r ==> (!!y. (a, y) : r ==> P) ==> P"
754 by (iprover dest!: iffD1 [OF Domain_iff])
756 lemma Range_iff: "(a : Range r) = (EX y. (y, a) : r)"
757 by (simp add: Domain_def Range_def)
759 lemma RangeI [intro]: "(a, b) : r ==> b : Range r"
760 by (unfold Range_def) (iprover intro!: converseI DomainI)
762 lemma RangeE [elim!]: "b : Range r ==> (!!x. (x, b) : r ==> P) ==> P"
763 by (unfold Range_def) (iprover elim!: DomainE dest!: converseD)
765 inductive DomainP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> bool"
766 for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool"
768 DomainPI [intro]: "r a b \<Longrightarrow> DomainP r a"
770 inductive_cases DomainPE [elim!]: "DomainP r a"
772 lemma DomainP_Domain_eq [pred_set_conv]: "DomainP (\<lambda>x y. (x, y) \<in> r) = (\<lambda>x. x \<in> Domain r)"
773 by (blast intro!: Orderings.order_antisym predicate1I)
775 inductive RangeP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'b \<Rightarrow> bool"
776 for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool"
778 RangePI [intro]: "r a b \<Longrightarrow> RangeP r b"
780 inductive_cases RangePE [elim!]: "RangeP r b"
782 lemma RangeP_Range_eq [pred_set_conv]: "RangeP (\<lambda>x y. (x, y) \<in> r) = (\<lambda>x. x \<in> Range r)"
783 by (auto intro!: Orderings.order_antisym predicate1I)
785 lemma Domain_fst [code]:
787 by (auto simp add: image_def Bex_def)
789 lemma Domain_empty [simp]: "Domain {} = {}"
792 lemma Domain_empty_iff: "Domain r = {} \<longleftrightarrow> r = {}"
795 lemma Domain_insert: "Domain (insert (a, b) r) = insert a (Domain r)"
798 lemma Domain_Id [simp]: "Domain Id = UNIV"
801 lemma Domain_Id_on [simp]: "Domain (Id_on A) = A"
804 lemma Domain_Un_eq: "Domain(A \<union> B) = Domain(A) \<union> Domain(B)"
807 lemma Domain_Int_subset: "Domain(A \<inter> B) \<subseteq> Domain(A) \<inter> Domain(B)"
810 lemma Domain_Diff_subset: "Domain(A) - Domain(B) \<subseteq> Domain(A - B)"
813 lemma Domain_Union: "Domain (Union S) = (\<Union>A\<in>S. Domain A)"
816 lemma Domain_converse [simp]: "Domain (r\<inverse>) = Range r"
819 lemma Domain_mono: "r \<subseteq> s ==> Domain r \<subseteq> Domain s"
822 lemma fst_eq_Domain: "fst ` R = Domain R"
825 lemma Domain_dprod [simp]: "Domain (dprod r s) = uprod (Domain r) (Domain s)"
828 lemma Domain_dsum [simp]: "Domain (dsum r s) = usum (Domain r) (Domain s)"
831 lemma Domain_Collect_split [simp]: "Domain {(x, y). P x y} = {x. EX y. P x y}"
834 lemma finite_Domain: "finite r ==> finite (Domain r)"
835 by (induct set: finite) (auto simp add: Domain_insert)
837 lemma Range_snd [code]:
839 by (auto simp add: image_def Bex_def)
841 lemma Range_empty [simp]: "Range {} = {}"
844 lemma Range_empty_iff: "Range r = {} \<longleftrightarrow> r = {}"
847 lemma Range_insert: "Range (insert (a, b) r) = insert b (Range r)"
850 lemma Range_Id [simp]: "Range Id = UNIV"
853 lemma Range_Id_on [simp]: "Range (Id_on A) = A"
856 lemma Range_Un_eq: "Range(A \<union> B) = Range(A) \<union> Range(B)"
859 lemma Range_Int_subset: "Range(A \<inter> B) \<subseteq> Range(A) \<inter> Range(B)"
862 lemma Range_Diff_subset: "Range(A) - Range(B) \<subseteq> Range(A - B)"
865 lemma Range_Union: "Range (Union S) = (\<Union>A\<in>S. Range A)"
868 lemma Range_converse [simp]: "Range (r\<inverse>) = Domain r"
871 lemma snd_eq_Range: "snd ` R = Range R"
874 lemma Range_Collect_split [simp]: "Range {(x, y). P x y} = {y. EX x. P x y}"
877 lemma finite_Range: "finite r ==> finite (Range r)"
878 by (induct set: finite) (auto simp add: Range_insert)
880 lemma mono_Field: "r \<subseteq> s \<Longrightarrow> Field r \<subseteq> Field s"
881 by (auto simp: Field_def Domain_def Range_def)
883 lemma Field_empty[simp]: "Field {} = {}"
884 by (auto simp: Field_def)
886 lemma Field_insert [simp]: "Field (insert (a, b) r) = {a, b} \<union> Field r"
887 by (auto simp: Field_def)
889 lemma Field_Un [simp]: "Field (r \<union> s) = Field r \<union> Field s"
890 by (auto simp: Field_def)
892 lemma Field_Union [simp]: "Field (\<Union>R) = \<Union>(Field ` R)"
893 by (auto simp: Field_def)
895 lemma Field_converse [simp]: "Field(r^-1) = Field r"
896 by (auto simp: Field_def)
898 lemma finite_Field: "finite r ==> finite (Field r)"
899 -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
900 apply (induct set: finite)
901 apply (auto simp add: Field_def Domain_insert Range_insert)
905 "Domain r = {x. \<exists>y. (x, y) \<in> r}"
909 subsubsection {* Image of a set under a relation *}
911 definition Image :: "('a \<times> 'b) set \<Rightarrow> 'a set \<Rightarrow> 'b set" (infixl "``" 90)
913 "r `` s = {y. \<exists>x\<in>s. (x, y) \<in> r}"
915 declare Image_def [no_atp]
917 lemma Image_iff: "(b : r``A) = (EX x:A. (x, b) : r)"
918 by (simp add: Image_def)
920 lemma Image_singleton: "r``{a} = {b. (a, b) : r}"
921 by (simp add: Image_def)
923 lemma Image_singleton_iff [iff]: "(b : r``{a}) = ((a, b) : r)"
924 by (rule Image_iff [THEN trans]) simp
926 lemma ImageI [intro,no_atp]: "(a, b) : r ==> a : A ==> b : r``A"
927 by (unfold Image_def) blast
929 lemma ImageE [elim!]:
930 "b : r `` A ==> (!!x. (x, b) : r ==> x : A ==> P) ==> P"
931 by (unfold Image_def) (iprover elim!: CollectE bexE)
933 lemma rev_ImageI: "a : A ==> (a, b) : r ==> b : r `` A"
934 -- {* This version's more effective when we already have the required @{text a} *}
937 lemma Image_empty [simp]: "R``{} = {}"
940 lemma Image_Id [simp]: "Id `` A = A"
943 lemma Image_Id_on [simp]: "Id_on A `` B = A \<inter> B"
946 lemma Image_Int_subset: "R `` (A \<inter> B) \<subseteq> R `` A \<inter> R `` B"
950 "single_valued (converse R) ==> R `` (A \<inter> B) = R `` A \<inter> R `` B"
951 by (simp add: single_valued_def, blast)
953 lemma Image_Un: "R `` (A \<union> B) = R `` A \<union> R `` B"
956 lemma Un_Image: "(R \<union> S) `` A = R `` A \<union> S `` A"
959 lemma Image_subset: "r \<subseteq> A \<times> B ==> r``C \<subseteq> B"
960 by (iprover intro!: subsetI elim!: ImageE dest!: subsetD SigmaD2)
962 lemma Image_eq_UN: "r``B = (\<Union>y\<in> B. r``{y})"
963 -- {* NOT suitable for rewriting *}
966 lemma Image_mono: "r' \<subseteq> r ==> A' \<subseteq> A ==> (r' `` A') \<subseteq> (r `` A)"
969 lemma Image_UN: "(r `` (UNION A B)) = (\<Union>x\<in>A. r `` (B x))"
972 lemma Image_INT_subset: "(r `` INTER A B) \<subseteq> (\<Inter>x\<in>A. r `` (B x))"
975 text{*Converse inclusion requires some assumptions*}
977 "[|single_valued (r\<inverse>); A\<noteq>{}|] ==> r `` INTER A B = (\<Inter>x\<in>A. r `` B x)"
978 apply (rule equalityI)
979 apply (rule Image_INT_subset)
980 apply (simp add: single_valued_def, blast)
983 lemma Image_subset_eq: "(r``A \<subseteq> B) = (A \<subseteq> - ((r^-1) `` (-B)))"
986 lemma Image_Collect_split [simp]: "{(x, y). P x y} `` A = {y. EX x:A. P x y}"
990 subsubsection {* Inverse image *}
992 definition inv_image :: "'b rel \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a rel"
994 "inv_image r f = {(x, y). (f x, f y) \<in> r}"
996 definition inv_imagep :: "('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
998 "inv_imagep r f = (\<lambda>x y. r (f x) (f y))"
1000 lemma [pred_set_conv]: "inv_imagep (\<lambda>x y. (x, y) \<in> r) f = (\<lambda>x y. (x, y) \<in> inv_image r f)"
1001 by (simp add: inv_image_def inv_imagep_def)
1003 lemma sym_inv_image: "sym r ==> sym (inv_image r f)"
1004 by (unfold sym_def inv_image_def) blast
1006 lemma trans_inv_image: "trans r ==> trans (inv_image r f)"
1007 apply (unfold trans_def inv_image_def)
1008 apply (simp (no_asm))
1012 lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
1013 by (auto simp:inv_image_def)
1015 lemma converse_inv_image[simp]: "(inv_image R f)^-1 = inv_image (R^-1) f"
1016 unfolding inv_image_def converse_unfold by auto
1018 lemma in_inv_imagep [simp]: "inv_imagep r f x y = r (f x) (f y)"
1019 by (simp add: inv_imagep_def)
1022 subsubsection {* Powerset *}
1024 definition Powp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
1026 "Powp A = (\<lambda>B. \<forall>x \<in> B. A x)"
1028 lemma Powp_Pow_eq [pred_set_conv]: "Powp (\<lambda>x. x \<in> A) = (\<lambda>x. x \<in> Pow A)"
1029 by (auto simp add: Powp_def fun_eq_iff)
1031 lemmas Powp_mono [mono] = Pow_mono [to_pred]