src/HOL/Relation.thy
author haftmann
Sat Apr 12 11:27:36 2014 +0200 (2014-04-12)
changeset 56545 8f1e7596deb7
parent 56218 1c3f1f2431f9
child 56742 678a52e676b6
permissions -rw-r--r--
more operations and lemmas
     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 Finite_Set
     9 begin
    10 
    11 text {* A preliminary: classical rules for reasoning on predicates *}
    12 
    13 declare predicate1I [Pure.intro!, intro!]
    14 declare predicate1D [Pure.dest, dest]
    15 declare predicate2I [Pure.intro!, intro!]
    16 declare predicate2D [Pure.dest, dest]
    17 declare bot1E [elim!] 
    18 declare bot2E [elim!]
    19 declare top1I [intro!]
    20 declare top2I [intro!]
    21 declare inf1I [intro!]
    22 declare inf2I [intro!]
    23 declare inf1E [elim!]
    24 declare inf2E [elim!]
    25 declare sup1I1 [intro?]
    26 declare sup2I1 [intro?]
    27 declare sup1I2 [intro?]
    28 declare sup2I2 [intro?]
    29 declare sup1E [elim!]
    30 declare sup2E [elim!]
    31 declare sup1CI [intro!]
    32 declare sup2CI [intro!]
    33 declare INF1_I [intro!]
    34 declare INF2_I [intro!]
    35 declare INF1_D [elim]
    36 declare INF2_D [elim]
    37 declare INF1_E [elim]
    38 declare INF2_E [elim]
    39 declare SUP1_I [intro]
    40 declare SUP2_I [intro]
    41 declare SUP1_E [elim!]
    42 declare SUP2_E [elim!]
    43 
    44 subsection {* Fundamental *}
    45 
    46 subsubsection {* Relations as sets of pairs *}
    47 
    48 type_synonym 'a rel = "('a * 'a) set"
    49 
    50 lemma subrelI: -- {* Version of @{thm [source] subsetI} for binary relations *}
    51   "(\<And>x y. (x, y) \<in> r \<Longrightarrow> (x, y) \<in> s) \<Longrightarrow> r \<subseteq> s"
    52   by auto
    53 
    54 lemma lfp_induct2: -- {* Version of @{thm [source] lfp_induct} for binary relations *}
    55   "(a, b) \<in> lfp f \<Longrightarrow> mono f \<Longrightarrow>
    56     (\<And>a b. (a, b) \<in> f (lfp f \<inter> {(x, y). P x y}) \<Longrightarrow> P a b) \<Longrightarrow> P a b"
    57   using lfp_induct_set [of "(a, b)" f "case_prod P"] by auto
    58 
    59 
    60 subsubsection {* Conversions between set and predicate relations *}
    61 
    62 lemma pred_equals_eq [pred_set_conv]: "(\<lambda>x. x \<in> R) = (\<lambda>x. x \<in> S) \<longleftrightarrow> R = S"
    63   by (simp add: set_eq_iff fun_eq_iff)
    64 
    65 lemma pred_equals_eq2 [pred_set_conv]: "(\<lambda>x y. (x, y) \<in> R) = (\<lambda>x y. (x, y) \<in> S) \<longleftrightarrow> R = S"
    66   by (simp add: set_eq_iff fun_eq_iff)
    67 
    68 lemma pred_subset_eq [pred_set_conv]: "(\<lambda>x. x \<in> R) \<le> (\<lambda>x. x \<in> S) \<longleftrightarrow> R \<subseteq> S"
    69   by (simp add: subset_iff le_fun_def)
    70 
    71 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"
    72   by (simp add: subset_iff le_fun_def)
    73 
    74 lemma bot_empty_eq [pred_set_conv]: "\<bottom> = (\<lambda>x. x \<in> {})"
    75   by (auto simp add: fun_eq_iff)
    76 
    77 lemma bot_empty_eq2 [pred_set_conv]: "\<bottom> = (\<lambda>x y. (x, y) \<in> {})"
    78   by (auto simp add: fun_eq_iff)
    79 
    80 lemma top_empty_eq [pred_set_conv]: "\<top> = (\<lambda>x. x \<in> UNIV)"
    81   by (auto simp add: fun_eq_iff)
    82 
    83 lemma top_empty_eq2 [pred_set_conv]: "\<top> = (\<lambda>x y. (x, y) \<in> UNIV)"
    84   by (auto simp add: fun_eq_iff)
    85 
    86 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)"
    87   by (simp add: inf_fun_def)
    88 
    89 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)"
    90   by (simp add: inf_fun_def)
    91 
    92 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)"
    93   by (simp add: sup_fun_def)
    94 
    95 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)"
    96   by (simp add: sup_fun_def)
    97 
    98 lemma INF_INT_eq [pred_set_conv]: "(\<Sqinter>i\<in>S. (\<lambda>x. x \<in> r i)) = (\<lambda>x. x \<in> (\<Inter>i\<in>S. r i))"
    99   by (simp add: fun_eq_iff)
   100 
   101 lemma INF_INT_eq2 [pred_set_conv]: "(\<Sqinter>i\<in>S. (\<lambda>x y. (x, y) \<in> r i)) = (\<lambda>x y. (x, y) \<in> (\<Inter>i\<in>S. r i))"
   102   by (simp add: fun_eq_iff)
   103 
   104 lemma SUP_UN_eq [pred_set_conv]: "(\<Squnion>i\<in>S. (\<lambda>x. x \<in> r i)) = (\<lambda>x. x \<in> (\<Union>i\<in>S. r i))"
   105   by (simp add: fun_eq_iff)
   106 
   107 lemma SUP_UN_eq2 [pred_set_conv]: "(\<Squnion>i\<in>S. (\<lambda>x y. (x, y) \<in> r i)) = (\<lambda>x y. (x, y) \<in> (\<Union>i\<in>S. r i))"
   108   by (simp add: fun_eq_iff)
   109 
   110 lemma Inf_INT_eq [pred_set_conv]: "\<Sqinter>S = (\<lambda>x. x \<in> INTER S Collect)"
   111   by (simp add: fun_eq_iff)
   112 
   113 lemma INF_Int_eq [pred_set_conv]: "(\<Sqinter>i\<in>S. (\<lambda>x. x \<in> i)) = (\<lambda>x. x \<in> \<Inter>S)"
   114   by (simp add: fun_eq_iff)
   115 
   116 lemma Inf_INT_eq2 [pred_set_conv]: "\<Sqinter>S = (\<lambda>x y. (x, y) \<in> INTER (case_prod ` S) Collect)"
   117   by (simp add: fun_eq_iff)
   118 
   119 lemma INF_Int_eq2 [pred_set_conv]: "(\<Sqinter>i\<in>S. (\<lambda>x y. (x, y) \<in> i)) = (\<lambda>x y. (x, y) \<in> \<Inter>S)"
   120   by (simp add: fun_eq_iff)
   121 
   122 lemma Sup_SUP_eq [pred_set_conv]: "\<Squnion>S = (\<lambda>x. x \<in> UNION S Collect)"
   123   by (simp add: fun_eq_iff)
   124 
   125 lemma SUP_Sup_eq [pred_set_conv]: "(\<Squnion>i\<in>S. (\<lambda>x. x \<in> i)) = (\<lambda>x. x \<in> \<Union>S)"
   126   by (simp add: fun_eq_iff)
   127 
   128 lemma Sup_SUP_eq2 [pred_set_conv]: "\<Squnion>S = (\<lambda>x y. (x, y) \<in> UNION (case_prod ` S) Collect)"
   129   by (simp add: fun_eq_iff)
   130 
   131 lemma SUP_Sup_eq2 [pred_set_conv]: "(\<Squnion>i\<in>S. (\<lambda>x y. (x, y) \<in> i)) = (\<lambda>x y. (x, y) \<in> \<Union>S)"
   132   by (simp add: fun_eq_iff)
   133 
   134 subsection {* Properties of relations *}
   135 
   136 subsubsection {* Reflexivity *}
   137 
   138 definition refl_on :: "'a set \<Rightarrow> 'a rel \<Rightarrow> bool"
   139 where
   140   "refl_on A r \<longleftrightarrow> r \<subseteq> A \<times> A \<and> (\<forall>x\<in>A. (x, x) \<in> r)"
   141 
   142 abbreviation refl :: "'a rel \<Rightarrow> bool"
   143 where -- {* reflexivity over a type *}
   144   "refl \<equiv> refl_on UNIV"
   145 
   146 definition reflp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   147 where
   148   "reflp r \<longleftrightarrow> (\<forall>x. r x x)"
   149 
   150 lemma reflp_refl_eq [pred_set_conv]:
   151   "reflp (\<lambda>x y. (x, y) \<in> r) \<longleftrightarrow> refl r" 
   152   by (simp add: refl_on_def reflp_def)
   153 
   154 lemma refl_onI: "r \<subseteq> A \<times> A ==> (!!x. x : A ==> (x, x) : r) ==> refl_on A r"
   155   by (unfold refl_on_def) (iprover intro!: ballI)
   156 
   157 lemma refl_onD: "refl_on A r ==> a : A ==> (a, a) : r"
   158   by (unfold refl_on_def) blast
   159 
   160 lemma refl_onD1: "refl_on A r ==> (x, y) : r ==> x : A"
   161   by (unfold refl_on_def) blast
   162 
   163 lemma refl_onD2: "refl_on A r ==> (x, y) : r ==> y : A"
   164   by (unfold refl_on_def) blast
   165 
   166 lemma reflpI:
   167   "(\<And>x. r x x) \<Longrightarrow> reflp r"
   168   by (auto intro: refl_onI simp add: reflp_def)
   169 
   170 lemma reflpE:
   171   assumes "reflp r"
   172   obtains "r x x"
   173   using assms by (auto dest: refl_onD simp add: reflp_def)
   174 
   175 lemma reflpD:
   176   assumes "reflp r"
   177   shows "r x x"
   178   using assms by (auto elim: reflpE)
   179 
   180 lemma refl_on_Int: "refl_on A r ==> refl_on B s ==> refl_on (A \<inter> B) (r \<inter> s)"
   181   by (unfold refl_on_def) blast
   182 
   183 lemma reflp_inf:
   184   "reflp r \<Longrightarrow> reflp s \<Longrightarrow> reflp (r \<sqinter> s)"
   185   by (auto intro: reflpI elim: reflpE)
   186 
   187 lemma refl_on_Un: "refl_on A r ==> refl_on B s ==> refl_on (A \<union> B) (r \<union> s)"
   188   by (unfold refl_on_def) blast
   189 
   190 lemma reflp_sup:
   191   "reflp r \<Longrightarrow> reflp s \<Longrightarrow> reflp (r \<squnion> s)"
   192   by (auto intro: reflpI elim: reflpE)
   193 
   194 lemma refl_on_INTER:
   195   "ALL x:S. refl_on (A x) (r x) ==> refl_on (INTER S A) (INTER S r)"
   196   by (unfold refl_on_def) fast
   197 
   198 lemma refl_on_UNION:
   199   "ALL x:S. refl_on (A x) (r x) \<Longrightarrow> refl_on (UNION S A) (UNION S r)"
   200   by (unfold refl_on_def) blast
   201 
   202 lemma refl_on_empty [simp]: "refl_on {} {}"
   203   by (simp add:refl_on_def)
   204 
   205 lemma refl_on_def' [nitpick_unfold, code]:
   206   "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)"
   207   by (auto intro: refl_onI dest: refl_onD refl_onD1 refl_onD2)
   208 
   209 
   210 subsubsection {* Irreflexivity *}
   211 
   212 definition irrefl :: "'a rel \<Rightarrow> bool"
   213 where
   214   "irrefl r \<longleftrightarrow> (\<forall>a. (a, a) \<notin> r)"
   215 
   216 definition irreflp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   217 where
   218   "irreflp R \<longleftrightarrow> (\<forall>a. \<not> R a a)"
   219 
   220 lemma irreflp_irrefl_eq [pred_set_conv]:
   221   "irreflp (\<lambda>a b. (a, b) \<in> R) \<longleftrightarrow> irrefl R" 
   222   by (simp add: irrefl_def irreflp_def)
   223 
   224 lemma irreflI:
   225   "(\<And>a. (a, a) \<notin> R) \<Longrightarrow> irrefl R"
   226   by (simp add: irrefl_def)
   227 
   228 lemma irreflpI:
   229   "(\<And>a. \<not> R a a) \<Longrightarrow> irreflp R"
   230   by (fact irreflI [to_pred])
   231 
   232 lemma irrefl_distinct [code]:
   233   "irrefl r \<longleftrightarrow> (\<forall>(a, b) \<in> r. a \<noteq> b)"
   234   by (auto simp add: irrefl_def)
   235 
   236 
   237 subsubsection {* Asymmetry *}
   238 
   239 inductive asym :: "'a rel \<Rightarrow> bool"
   240 where
   241   asymI: "irrefl R \<Longrightarrow> (\<And>a b. (a, b) \<in> R \<Longrightarrow> (b, a) \<notin> R) \<Longrightarrow> asym R"
   242 
   243 inductive asymp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   244 where
   245   asympI: "irreflp R \<Longrightarrow> (\<And>a b. R a b \<Longrightarrow> \<not> R b a) \<Longrightarrow> asymp R"
   246 
   247 lemma asymp_asym_eq [pred_set_conv]:
   248   "asymp (\<lambda>a b. (a, b) \<in> R) \<longleftrightarrow> asym R" 
   249   by (auto intro!: asymI asympI elim: asym.cases asymp.cases simp add: irreflp_irrefl_eq)
   250 
   251 
   252 subsubsection {* Symmetry *}
   253 
   254 definition sym :: "'a rel \<Rightarrow> bool"
   255 where
   256   "sym r \<longleftrightarrow> (\<forall>x y. (x, y) \<in> r \<longrightarrow> (y, x) \<in> r)"
   257 
   258 definition symp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   259 where
   260   "symp r \<longleftrightarrow> (\<forall>x y. r x y \<longrightarrow> r y x)"
   261 
   262 lemma symp_sym_eq [pred_set_conv]:
   263   "symp (\<lambda>x y. (x, y) \<in> r) \<longleftrightarrow> sym r" 
   264   by (simp add: sym_def symp_def)
   265 
   266 lemma symI:
   267   "(\<And>a b. (a, b) \<in> r \<Longrightarrow> (b, a) \<in> r) \<Longrightarrow> sym r"
   268   by (unfold sym_def) iprover
   269 
   270 lemma sympI:
   271   "(\<And>a b. r a b \<Longrightarrow> r b a) \<Longrightarrow> symp r"
   272   by (fact symI [to_pred])
   273 
   274 lemma symE:
   275   assumes "sym r" and "(b, a) \<in> r"
   276   obtains "(a, b) \<in> r"
   277   using assms by (simp add: sym_def)
   278 
   279 lemma sympE:
   280   assumes "symp r" and "r b a"
   281   obtains "r a b"
   282   using assms by (rule symE [to_pred])
   283 
   284 lemma symD:
   285   assumes "sym r" and "(b, a) \<in> r"
   286   shows "(a, b) \<in> r"
   287   using assms by (rule symE)
   288 
   289 lemma sympD:
   290   assumes "symp r" and "r b a"
   291   shows "r a b"
   292   using assms by (rule symD [to_pred])
   293 
   294 lemma sym_Int:
   295   "sym r \<Longrightarrow> sym s \<Longrightarrow> sym (r \<inter> s)"
   296   by (fast intro: symI elim: symE)
   297 
   298 lemma symp_inf:
   299   "symp r \<Longrightarrow> symp s \<Longrightarrow> symp (r \<sqinter> s)"
   300   by (fact sym_Int [to_pred])
   301 
   302 lemma sym_Un:
   303   "sym r \<Longrightarrow> sym s \<Longrightarrow> sym (r \<union> s)"
   304   by (fast intro: symI elim: symE)
   305 
   306 lemma symp_sup:
   307   "symp r \<Longrightarrow> symp s \<Longrightarrow> symp (r \<squnion> s)"
   308   by (fact sym_Un [to_pred])
   309 
   310 lemma sym_INTER:
   311   "\<forall>x\<in>S. sym (r x) \<Longrightarrow> sym (INTER S r)"
   312   by (fast intro: symI elim: symE)
   313 
   314 lemma symp_INF:
   315   "\<forall>x\<in>S. symp (r x) \<Longrightarrow> symp (INFIMUM S r)"
   316   by (fact sym_INTER [to_pred])
   317 
   318 lemma sym_UNION:
   319   "\<forall>x\<in>S. sym (r x) \<Longrightarrow> sym (UNION S r)"
   320   by (fast intro: symI elim: symE)
   321 
   322 lemma symp_SUP:
   323   "\<forall>x\<in>S. symp (r x) \<Longrightarrow> symp (SUPREMUM S r)"
   324   by (fact sym_UNION [to_pred])
   325 
   326 
   327 subsubsection {* Antisymmetry *}
   328 
   329 definition antisym :: "'a rel \<Rightarrow> bool"
   330 where
   331   "antisym r \<longleftrightarrow> (\<forall>x y. (x, y) \<in> r \<longrightarrow> (y, x) \<in> r \<longrightarrow> x = y)"
   332 
   333 abbreviation antisymP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   334 where
   335   "antisymP r \<equiv> antisym {(x, y). r x y}"
   336 
   337 lemma antisymI:
   338   "(!!x y. (x, y) : r ==> (y, x) : r ==> x=y) ==> antisym r"
   339   by (unfold antisym_def) iprover
   340 
   341 lemma antisymD: "antisym r ==> (a, b) : r ==> (b, a) : r ==> a = b"
   342   by (unfold antisym_def) iprover
   343 
   344 lemma antisym_subset: "r \<subseteq> s ==> antisym s ==> antisym r"
   345   by (unfold antisym_def) blast
   346 
   347 lemma antisym_empty [simp]: "antisym {}"
   348   by (unfold antisym_def) blast
   349 
   350 
   351 subsubsection {* Transitivity *}
   352 
   353 definition trans :: "'a rel \<Rightarrow> bool"
   354 where
   355   "trans r \<longleftrightarrow> (\<forall>x y z. (x, y) \<in> r \<longrightarrow> (y, z) \<in> r \<longrightarrow> (x, z) \<in> r)"
   356 
   357 definition transp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   358 where
   359   "transp r \<longleftrightarrow> (\<forall>x y z. r x y \<longrightarrow> r y z \<longrightarrow> r x z)"
   360 
   361 lemma transp_trans_eq [pred_set_conv]:
   362   "transp (\<lambda>x y. (x, y) \<in> r) \<longleftrightarrow> trans r" 
   363   by (simp add: trans_def transp_def)
   364 
   365 abbreviation transP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   366 where -- {* FIXME drop *}
   367   "transP r \<equiv> trans {(x, y). r x y}"
   368 
   369 lemma transI:
   370   "(\<And>x y z. (x, y) \<in> r \<Longrightarrow> (y, z) \<in> r \<Longrightarrow> (x, z) \<in> r) \<Longrightarrow> trans r"
   371   by (unfold trans_def) iprover
   372 
   373 lemma transpI:
   374   "(\<And>x y z. r x y \<Longrightarrow> r y z \<Longrightarrow> r x z) \<Longrightarrow> transp r"
   375   by (fact transI [to_pred])
   376 
   377 lemma transE:
   378   assumes "trans r" and "(x, y) \<in> r" and "(y, z) \<in> r"
   379   obtains "(x, z) \<in> r"
   380   using assms by (unfold trans_def) iprover
   381 
   382 lemma transpE:
   383   assumes "transp r" and "r x y" and "r y z"
   384   obtains "r x z"
   385   using assms by (rule transE [to_pred])
   386 
   387 lemma transD:
   388   assumes "trans r" and "(x, y) \<in> r" and "(y, z) \<in> r"
   389   shows "(x, z) \<in> r"
   390   using assms by (rule transE)
   391 
   392 lemma transpD:
   393   assumes "transp r" and "r x y" and "r y z"
   394   shows "r x z"
   395   using assms by (rule transD [to_pred])
   396 
   397 lemma trans_Int:
   398   "trans r \<Longrightarrow> trans s \<Longrightarrow> trans (r \<inter> s)"
   399   by (fast intro: transI elim: transE)
   400 
   401 lemma transp_inf:
   402   "transp r \<Longrightarrow> transp s \<Longrightarrow> transp (r \<sqinter> s)"
   403   by (fact trans_Int [to_pred])
   404 
   405 lemma trans_INTER:
   406   "\<forall>x\<in>S. trans (r x) \<Longrightarrow> trans (INTER S r)"
   407   by (fast intro: transI elim: transD)
   408 
   409 (* FIXME thm trans_INTER [to_pred] *)
   410 
   411 lemma trans_join [code]:
   412   "trans r \<longleftrightarrow> (\<forall>(x, y1) \<in> r. \<forall>(y2, z) \<in> r. y1 = y2 \<longrightarrow> (x, z) \<in> r)"
   413   by (auto simp add: trans_def)
   414 
   415 lemma transp_trans:
   416   "transp r \<longleftrightarrow> trans {(x, y). r x y}"
   417   by (simp add: trans_def transp_def)
   418 
   419 
   420 subsubsection {* Totality *}
   421 
   422 definition total_on :: "'a set \<Rightarrow> 'a rel \<Rightarrow> bool"
   423 where
   424   "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)"
   425 
   426 abbreviation "total \<equiv> total_on UNIV"
   427 
   428 lemma total_on_empty [simp]: "total_on {} r"
   429   by (simp add: total_on_def)
   430 
   431 
   432 subsubsection {* Single valued relations *}
   433 
   434 definition single_valued :: "('a \<times> 'b) set \<Rightarrow> bool"
   435 where
   436   "single_valued r \<longleftrightarrow> (\<forall>x y. (x, y) \<in> r \<longrightarrow> (\<forall>z. (x, z) \<in> r \<longrightarrow> y = z))"
   437 
   438 abbreviation single_valuedP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
   439   "single_valuedP r \<equiv> single_valued {(x, y). r x y}"
   440 
   441 lemma single_valuedI:
   442   "ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z) ==> single_valued r"
   443   by (unfold single_valued_def)
   444 
   445 lemma single_valuedD:
   446   "single_valued r ==> (x, y) : r ==> (x, z) : r ==> y = z"
   447   by (simp add: single_valued_def)
   448 
   449 lemma simgle_valued_empty[simp]: "single_valued {}"
   450 by(simp add: single_valued_def)
   451 
   452 lemma single_valued_subset:
   453   "r \<subseteq> s ==> single_valued s ==> single_valued r"
   454   by (unfold single_valued_def) blast
   455 
   456 
   457 subsection {* Relation operations *}
   458 
   459 subsubsection {* The identity relation *}
   460 
   461 definition Id :: "'a rel"
   462 where
   463   [code del]: "Id = {p. \<exists>x. p = (x, x)}"
   464 
   465 lemma IdI [intro]: "(a, a) : Id"
   466   by (simp add: Id_def)
   467 
   468 lemma IdE [elim!]: "p : Id ==> (!!x. p = (x, x) ==> P) ==> P"
   469   by (unfold Id_def) (iprover elim: CollectE)
   470 
   471 lemma pair_in_Id_conv [iff]: "((a, b) : Id) = (a = b)"
   472   by (unfold Id_def) blast
   473 
   474 lemma refl_Id: "refl Id"
   475   by (simp add: refl_on_def)
   476 
   477 lemma antisym_Id: "antisym Id"
   478   -- {* A strange result, since @{text Id} is also symmetric. *}
   479   by (simp add: antisym_def)
   480 
   481 lemma sym_Id: "sym Id"
   482   by (simp add: sym_def)
   483 
   484 lemma trans_Id: "trans Id"
   485   by (simp add: trans_def)
   486 
   487 lemma single_valued_Id [simp]: "single_valued Id"
   488   by (unfold single_valued_def) blast
   489 
   490 lemma irrefl_diff_Id [simp]: "irrefl (r - Id)"
   491   by (simp add:irrefl_def)
   492 
   493 lemma trans_diff_Id: "trans r \<Longrightarrow> antisym r \<Longrightarrow> trans (r - Id)"
   494   unfolding antisym_def trans_def by blast
   495 
   496 lemma total_on_diff_Id [simp]: "total_on A (r - Id) = total_on A r"
   497   by (simp add: total_on_def)
   498 
   499 
   500 subsubsection {* Diagonal: identity over a set *}
   501 
   502 definition Id_on  :: "'a set \<Rightarrow> 'a rel"
   503 where
   504   "Id_on A = (\<Union>x\<in>A. {(x, x)})"
   505 
   506 lemma Id_on_empty [simp]: "Id_on {} = {}"
   507   by (simp add: Id_on_def) 
   508 
   509 lemma Id_on_eqI: "a = b ==> a : A ==> (a, b) : Id_on A"
   510   by (simp add: Id_on_def)
   511 
   512 lemma Id_onI [intro!]: "a : A ==> (a, a) : Id_on A"
   513   by (rule Id_on_eqI) (rule refl)
   514 
   515 lemma Id_onE [elim!]:
   516   "c : Id_on A ==> (!!x. x : A ==> c = (x, x) ==> P) ==> P"
   517   -- {* The general elimination rule. *}
   518   by (unfold Id_on_def) (iprover elim!: UN_E singletonE)
   519 
   520 lemma Id_on_iff: "((x, y) : Id_on A) = (x = y & x : A)"
   521   by blast
   522 
   523 lemma Id_on_def' [nitpick_unfold]:
   524   "Id_on {x. A x} = Collect (\<lambda>(x, y). x = y \<and> A x)"
   525   by auto
   526 
   527 lemma Id_on_subset_Times: "Id_on A \<subseteq> A \<times> A"
   528   by blast
   529 
   530 lemma refl_on_Id_on: "refl_on A (Id_on A)"
   531   by (rule refl_onI [OF Id_on_subset_Times Id_onI])
   532 
   533 lemma antisym_Id_on [simp]: "antisym (Id_on A)"
   534   by (unfold antisym_def) blast
   535 
   536 lemma sym_Id_on [simp]: "sym (Id_on A)"
   537   by (rule symI) clarify
   538 
   539 lemma trans_Id_on [simp]: "trans (Id_on A)"
   540   by (fast intro: transI elim: transD)
   541 
   542 lemma single_valued_Id_on [simp]: "single_valued (Id_on A)"
   543   by (unfold single_valued_def) blast
   544 
   545 
   546 subsubsection {* Composition *}
   547 
   548 inductive_set relcomp  :: "('a \<times> 'b) set \<Rightarrow> ('b \<times> 'c) set \<Rightarrow> ('a \<times> 'c) set" (infixr "O" 75)
   549   for r :: "('a \<times> 'b) set" and s :: "('b \<times> 'c) set"
   550 where
   551   relcompI [intro]: "(a, b) \<in> r \<Longrightarrow> (b, c) \<in> s \<Longrightarrow> (a, c) \<in> r O s"
   552 
   553 notation relcompp (infixr "OO" 75)
   554 
   555 lemmas relcomppI = relcompp.intros
   556 
   557 text {*
   558   For historic reasons, the elimination rules are not wholly corresponding.
   559   Feel free to consolidate this.
   560 *}
   561 
   562 inductive_cases relcompEpair: "(a, c) \<in> r O s"
   563 inductive_cases relcomppE [elim!]: "(r OO s) a c"
   564 
   565 lemma relcompE [elim!]: "xz \<in> r O s \<Longrightarrow>
   566   (\<And>x y z. xz = (x, z) \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> (y, z) \<in> s  \<Longrightarrow> P) \<Longrightarrow> P"
   567   by (cases xz) (simp, erule relcompEpair, iprover)
   568 
   569 lemma R_O_Id [simp]:
   570   "R O Id = R"
   571   by fast
   572 
   573 lemma Id_O_R [simp]:
   574   "Id O R = R"
   575   by fast
   576 
   577 lemma relcomp_empty1 [simp]:
   578   "{} O R = {}"
   579   by blast
   580 
   581 lemma relcompp_bot1 [simp]:
   582   "\<bottom> OO R = \<bottom>"
   583   by (fact relcomp_empty1 [to_pred])
   584 
   585 lemma relcomp_empty2 [simp]:
   586   "R O {} = {}"
   587   by blast
   588 
   589 lemma relcompp_bot2 [simp]:
   590   "R OO \<bottom> = \<bottom>"
   591   by (fact relcomp_empty2 [to_pred])
   592 
   593 lemma O_assoc:
   594   "(R O S) O T = R O (S O T)"
   595   by blast
   596 
   597 
   598 lemma relcompp_assoc:
   599   "(r OO s) OO t = r OO (s OO t)"
   600   by (fact O_assoc [to_pred])
   601 
   602 lemma trans_O_subset:
   603   "trans r \<Longrightarrow> r O r \<subseteq> r"
   604   by (unfold trans_def) blast
   605 
   606 lemma transp_relcompp_less_eq:
   607   "transp r \<Longrightarrow> r OO r \<le> r "
   608   by (fact trans_O_subset [to_pred])
   609 
   610 lemma relcomp_mono:
   611   "r' \<subseteq> r \<Longrightarrow> s' \<subseteq> s \<Longrightarrow> r' O s' \<subseteq> r O s"
   612   by blast
   613 
   614 lemma relcompp_mono:
   615   "r' \<le> r \<Longrightarrow> s' \<le> s \<Longrightarrow> r' OO s' \<le> r OO s "
   616   by (fact relcomp_mono [to_pred])
   617 
   618 lemma relcomp_subset_Sigma:
   619   "r \<subseteq> A \<times> B \<Longrightarrow> s \<subseteq> B \<times> C \<Longrightarrow> r O s \<subseteq> A \<times> C"
   620   by blast
   621 
   622 lemma relcomp_distrib [simp]:
   623   "R O (S \<union> T) = (R O S) \<union> (R O T)" 
   624   by auto
   625 
   626 lemma relcompp_distrib [simp]:
   627   "R OO (S \<squnion> T) = R OO S \<squnion> R OO T"
   628   by (fact relcomp_distrib [to_pred])
   629 
   630 lemma relcomp_distrib2 [simp]:
   631   "(S \<union> T) O R = (S O R) \<union> (T O R)"
   632   by auto
   633 
   634 lemma relcompp_distrib2 [simp]:
   635   "(S \<squnion> T) OO R = S OO R \<squnion> T OO R"
   636   by (fact relcomp_distrib2 [to_pred])
   637 
   638 lemma relcomp_UNION_distrib:
   639   "s O UNION I r = (\<Union>i\<in>I. s O r i) "
   640   by auto
   641 
   642 (* FIXME thm relcomp_UNION_distrib [to_pred] *)
   643 
   644 lemma relcomp_UNION_distrib2:
   645   "UNION I r O s = (\<Union>i\<in>I. r i O s) "
   646   by auto
   647 
   648 (* FIXME thm relcomp_UNION_distrib2 [to_pred] *)
   649 
   650 lemma single_valued_relcomp:
   651   "single_valued r \<Longrightarrow> single_valued s \<Longrightarrow> single_valued (r O s)"
   652   by (unfold single_valued_def) blast
   653 
   654 lemma relcomp_unfold:
   655   "r O s = {(x, z). \<exists>y. (x, y) \<in> r \<and> (y, z) \<in> s}"
   656   by (auto simp add: set_eq_iff)
   657 
   658 lemma eq_OO: "op= OO R = R"
   659 by blast
   660 
   661 
   662 subsubsection {* Converse *}
   663 
   664 inductive_set converse :: "('a \<times> 'b) set \<Rightarrow> ('b \<times> 'a) set" ("(_^-1)" [1000] 999)
   665   for r :: "('a \<times> 'b) set"
   666 where
   667   "(a, b) \<in> r \<Longrightarrow> (b, a) \<in> r^-1"
   668 
   669 notation (xsymbols)
   670   converse  ("(_\<inverse>)" [1000] 999)
   671 
   672 notation
   673   conversep ("(_^--1)" [1000] 1000)
   674 
   675 notation (xsymbols)
   676   conversep  ("(_\<inverse>\<inverse>)" [1000] 1000)
   677 
   678 lemma converseI [sym]:
   679   "(a, b) \<in> r \<Longrightarrow> (b, a) \<in> r\<inverse>"
   680   by (fact converse.intros)
   681 
   682 lemma conversepI (* CANDIDATE [sym] *):
   683   "r a b \<Longrightarrow> r\<inverse>\<inverse> b a"
   684   by (fact conversep.intros)
   685 
   686 lemma converseD [sym]:
   687   "(a, b) \<in> r\<inverse> \<Longrightarrow> (b, a) \<in> r"
   688   by (erule converse.cases) iprover
   689 
   690 lemma conversepD (* CANDIDATE [sym] *):
   691   "r\<inverse>\<inverse> b a \<Longrightarrow> r a b"
   692   by (fact converseD [to_pred])
   693 
   694 lemma converseE [elim!]:
   695   -- {* More general than @{text converseD}, as it ``splits'' the member of the relation. *}
   696   "yx \<in> r\<inverse> \<Longrightarrow> (\<And>x y. yx = (y, x) \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> P) \<Longrightarrow> P"
   697   by (cases yx) (simp, erule converse.cases, iprover)
   698 
   699 lemmas conversepE [elim!] = conversep.cases
   700 
   701 lemma converse_iff [iff]:
   702   "(a, b) \<in> r\<inverse> \<longleftrightarrow> (b, a) \<in> r"
   703   by (auto intro: converseI)
   704 
   705 lemma conversep_iff [iff]:
   706   "r\<inverse>\<inverse> a b = r b a"
   707   by (fact converse_iff [to_pred])
   708 
   709 lemma converse_converse [simp]:
   710   "(r\<inverse>)\<inverse> = r"
   711   by (simp add: set_eq_iff)
   712 
   713 lemma conversep_conversep [simp]:
   714   "(r\<inverse>\<inverse>)\<inverse>\<inverse> = r"
   715   by (fact converse_converse [to_pred])
   716 
   717 lemma converse_empty[simp]: "{}\<inverse> = {}"
   718 by auto
   719 
   720 lemma converse_UNIV[simp]: "UNIV\<inverse> = UNIV"
   721 by auto
   722 
   723 lemma converse_relcomp: "(r O s)^-1 = s^-1 O r^-1"
   724   by blast
   725 
   726 lemma converse_relcompp: "(r OO s)^--1 = s^--1 OO r^--1"
   727   by (iprover intro: order_antisym conversepI relcomppI
   728     elim: relcomppE dest: conversepD)
   729 
   730 lemma converse_Int: "(r \<inter> s)^-1 = r^-1 \<inter> s^-1"
   731   by blast
   732 
   733 lemma converse_meet: "(r \<sqinter> s)^--1 = r^--1 \<sqinter> s^--1"
   734   by (simp add: inf_fun_def) (iprover intro: conversepI ext dest: conversepD)
   735 
   736 lemma converse_Un: "(r \<union> s)^-1 = r^-1 \<union> s^-1"
   737   by blast
   738 
   739 lemma converse_join: "(r \<squnion> s)^--1 = r^--1 \<squnion> s^--1"
   740   by (simp add: sup_fun_def) (iprover intro: conversepI ext dest: conversepD)
   741 
   742 lemma converse_INTER: "(INTER S r)^-1 = (INT x:S. (r x)^-1)"
   743   by fast
   744 
   745 lemma converse_UNION: "(UNION S r)^-1 = (UN x:S. (r x)^-1)"
   746   by blast
   747 
   748 lemma converse_mono[simp]: "r^-1 \<subseteq> s ^-1 \<longleftrightarrow> r \<subseteq> s"
   749   by auto
   750 
   751 lemma conversep_mono[simp]: "r^--1 \<le> s ^--1 \<longleftrightarrow> r \<le> s"
   752   by (fact converse_mono[to_pred])
   753 
   754 lemma converse_inject[simp]: "r^-1 = s ^-1 \<longleftrightarrow> r = s"
   755   by auto
   756 
   757 lemma conversep_inject[simp]: "r^--1 = s ^--1 \<longleftrightarrow> r = s"
   758   by (fact converse_inject[to_pred])
   759 
   760 lemma converse_subset_swap: "r \<subseteq> s ^-1 = (r ^-1 \<subseteq> s)"
   761   by auto
   762 
   763 lemma conversep_le_swap: "r \<le> s ^--1 = (r ^--1 \<le> s)"
   764   by (fact converse_subset_swap[to_pred])
   765 
   766 lemma converse_Id [simp]: "Id^-1 = Id"
   767   by blast
   768 
   769 lemma converse_Id_on [simp]: "(Id_on A)^-1 = Id_on A"
   770   by blast
   771 
   772 lemma refl_on_converse [simp]: "refl_on A (converse r) = refl_on A r"
   773   by (unfold refl_on_def) auto
   774 
   775 lemma sym_converse [simp]: "sym (converse r) = sym r"
   776   by (unfold sym_def) blast
   777 
   778 lemma antisym_converse [simp]: "antisym (converse r) = antisym r"
   779   by (unfold antisym_def) blast
   780 
   781 lemma trans_converse [simp]: "trans (converse r) = trans r"
   782   by (unfold trans_def) blast
   783 
   784 lemma sym_conv_converse_eq: "sym r = (r^-1 = r)"
   785   by (unfold sym_def) fast
   786 
   787 lemma sym_Un_converse: "sym (r \<union> r^-1)"
   788   by (unfold sym_def) blast
   789 
   790 lemma sym_Int_converse: "sym (r \<inter> r^-1)"
   791   by (unfold sym_def) blast
   792 
   793 lemma total_on_converse [simp]: "total_on A (r^-1) = total_on A r"
   794   by (auto simp: total_on_def)
   795 
   796 lemma finite_converse [iff]: "finite (r^-1) = finite r"  
   797   unfolding converse_def conversep_iff using [[simproc add: finite_Collect]]
   798   by (auto elim: finite_imageD simp: inj_on_def)
   799 
   800 lemma conversep_noteq [simp]: "(op \<noteq>)^--1 = op \<noteq>"
   801   by (auto simp add: fun_eq_iff)
   802 
   803 lemma conversep_eq [simp]: "(op =)^--1 = op ="
   804   by (auto simp add: fun_eq_iff)
   805 
   806 lemma converse_unfold [code]:
   807   "r\<inverse> = {(y, x). (x, y) \<in> r}"
   808   by (simp add: set_eq_iff)
   809 
   810 
   811 subsubsection {* Domain, range and field *}
   812 
   813 inductive_set Domain :: "('a \<times> 'b) set \<Rightarrow> 'a set"
   814   for r :: "('a \<times> 'b) set"
   815 where
   816   DomainI [intro]: "(a, b) \<in> r \<Longrightarrow> a \<in> Domain r"
   817 
   818 abbreviation (input) "DomainP \<equiv> Domainp"
   819 
   820 lemmas DomainPI = Domainp.DomainI
   821 
   822 inductive_cases DomainE [elim!]: "a \<in> Domain r"
   823 inductive_cases DomainpE [elim!]: "Domainp r a"
   824 
   825 inductive_set Range :: "('a \<times> 'b) set \<Rightarrow> 'b set"
   826   for r :: "('a \<times> 'b) set"
   827 where
   828   RangeI [intro]: "(a, b) \<in> r \<Longrightarrow> b \<in> Range r"
   829 
   830 abbreviation (input) "RangeP \<equiv> Rangep"
   831 
   832 lemmas RangePI = Rangep.RangeI
   833 
   834 inductive_cases RangeE [elim!]: "b \<in> Range r"
   835 inductive_cases RangepE [elim!]: "Rangep r b"
   836 
   837 definition Field :: "'a rel \<Rightarrow> 'a set"
   838 where
   839   "Field r = Domain r \<union> Range r"
   840 
   841 lemma Domain_fst [code]:
   842   "Domain r = fst ` r"
   843   by force
   844 
   845 lemma Range_snd [code]:
   846   "Range r = snd ` r"
   847   by force
   848 
   849 lemma fst_eq_Domain: "fst ` R = Domain R"
   850   by force
   851 
   852 lemma snd_eq_Range: "snd ` R = Range R"
   853   by force
   854 
   855 lemma Domain_empty [simp]: "Domain {} = {}"
   856   by auto
   857 
   858 lemma Range_empty [simp]: "Range {} = {}"
   859   by auto
   860 
   861 lemma Field_empty [simp]: "Field {} = {}"
   862   by (simp add: Field_def)
   863 
   864 lemma Domain_empty_iff: "Domain r = {} \<longleftrightarrow> r = {}"
   865   by auto
   866 
   867 lemma Range_empty_iff: "Range r = {} \<longleftrightarrow> r = {}"
   868   by auto
   869 
   870 lemma Domain_insert [simp]: "Domain (insert (a, b) r) = insert a (Domain r)"
   871   by blast
   872 
   873 lemma Range_insert [simp]: "Range (insert (a, b) r) = insert b (Range r)"
   874   by blast
   875 
   876 lemma Field_insert [simp]: "Field (insert (a, b) r) = {a, b} \<union> Field r"
   877   by (auto simp add: Field_def)
   878 
   879 lemma Domain_iff: "a \<in> Domain r \<longleftrightarrow> (\<exists>y. (a, y) \<in> r)"
   880   by blast
   881 
   882 lemma Range_iff: "a \<in> Range r \<longleftrightarrow> (\<exists>y. (y, a) \<in> r)"
   883   by blast
   884 
   885 lemma Domain_Id [simp]: "Domain Id = UNIV"
   886   by blast
   887 
   888 lemma Range_Id [simp]: "Range Id = UNIV"
   889   by blast
   890 
   891 lemma Domain_Id_on [simp]: "Domain (Id_on A) = A"
   892   by blast
   893 
   894 lemma Range_Id_on [simp]: "Range (Id_on A) = A"
   895   by blast
   896 
   897 lemma Domain_Un_eq: "Domain (A \<union> B) = Domain A \<union> Domain B"
   898   by blast
   899 
   900 lemma Range_Un_eq: "Range (A \<union> B) = Range A \<union> Range B"
   901   by blast
   902 
   903 lemma Field_Un [simp]: "Field (r \<union> s) = Field r \<union> Field s"
   904   by (auto simp: Field_def)
   905 
   906 lemma Domain_Int_subset: "Domain (A \<inter> B) \<subseteq> Domain A \<inter> Domain B"
   907   by blast
   908 
   909 lemma Range_Int_subset: "Range (A \<inter> B) \<subseteq> Range A \<inter> Range B"
   910   by blast
   911 
   912 lemma Domain_Diff_subset: "Domain A - Domain B \<subseteq> Domain (A - B)"
   913   by blast
   914 
   915 lemma Range_Diff_subset: "Range A - Range B \<subseteq> Range (A - B)"
   916   by blast
   917 
   918 lemma Domain_Union: "Domain (\<Union>S) = (\<Union>A\<in>S. Domain A)"
   919   by blast
   920 
   921 lemma Range_Union: "Range (\<Union>S) = (\<Union>A\<in>S. Range A)"
   922   by blast
   923 
   924 lemma Field_Union [simp]: "Field (\<Union>R) = \<Union>(Field ` R)"
   925   by (auto simp: Field_def)
   926 
   927 lemma Domain_converse [simp]: "Domain (r\<inverse>) = Range r"
   928   by auto
   929 
   930 lemma Range_converse [simp]: "Range (r\<inverse>) = Domain r"
   931   by blast
   932 
   933 lemma Field_converse [simp]: "Field (r\<inverse>) = Field r"
   934   by (auto simp: Field_def)
   935 
   936 lemma Domain_Collect_split [simp]: "Domain {(x, y). P x y} = {x. EX y. P x y}"
   937   by auto
   938 
   939 lemma Range_Collect_split [simp]: "Range {(x, y). P x y} = {y. EX x. P x y}"
   940   by auto
   941 
   942 lemma finite_Domain: "finite r \<Longrightarrow> finite (Domain r)"
   943   by (induct set: finite) auto
   944 
   945 lemma finite_Range: "finite r \<Longrightarrow> finite (Range r)"
   946   by (induct set: finite) auto
   947 
   948 lemma finite_Field: "finite r \<Longrightarrow> finite (Field r)"
   949   by (simp add: Field_def finite_Domain finite_Range)
   950 
   951 lemma Domain_mono: "r \<subseteq> s \<Longrightarrow> Domain r \<subseteq> Domain s"
   952   by blast
   953 
   954 lemma Range_mono: "r \<subseteq> s \<Longrightarrow> Range r \<subseteq> Range s"
   955   by blast
   956 
   957 lemma mono_Field: "r \<subseteq> s \<Longrightarrow> Field r \<subseteq> Field s"
   958   by (auto simp: Field_def Domain_def Range_def)
   959 
   960 lemma Domain_unfold:
   961   "Domain r = {x. \<exists>y. (x, y) \<in> r}"
   962   by blast
   963 
   964 
   965 subsubsection {* Image of a set under a relation *}
   966 
   967 definition Image :: "('a \<times> 'b) set \<Rightarrow> 'a set \<Rightarrow> 'b set" (infixr "``" 90)
   968 where
   969   "r `` s = {y. \<exists>x\<in>s. (x, y) \<in> r}"
   970 
   971 lemma Image_iff: "(b : r``A) = (EX x:A. (x, b) : r)"
   972   by (simp add: Image_def)
   973 
   974 lemma Image_singleton: "r``{a} = {b. (a, b) : r}"
   975   by (simp add: Image_def)
   976 
   977 lemma Image_singleton_iff [iff]: "(b : r``{a}) = ((a, b) : r)"
   978   by (rule Image_iff [THEN trans]) simp
   979 
   980 lemma ImageI [intro]: "(a, b) : r ==> a : A ==> b : r``A"
   981   by (unfold Image_def) blast
   982 
   983 lemma ImageE [elim!]:
   984   "b : r `` A ==> (!!x. (x, b) : r ==> x : A ==> P) ==> P"
   985   by (unfold Image_def) (iprover elim!: CollectE bexE)
   986 
   987 lemma rev_ImageI: "a : A ==> (a, b) : r ==> b : r `` A"
   988   -- {* This version's more effective when we already have the required @{text a} *}
   989   by blast
   990 
   991 lemma Image_empty [simp]: "R``{} = {}"
   992   by blast
   993 
   994 lemma Image_Id [simp]: "Id `` A = A"
   995   by blast
   996 
   997 lemma Image_Id_on [simp]: "Id_on A `` B = A \<inter> B"
   998   by blast
   999 
  1000 lemma Image_Int_subset: "R `` (A \<inter> B) \<subseteq> R `` A \<inter> R `` B"
  1001   by blast
  1002 
  1003 lemma Image_Int_eq:
  1004   "single_valued (converse R) ==> R `` (A \<inter> B) = R `` A \<inter> R `` B"
  1005   by (simp add: single_valued_def, blast) 
  1006 
  1007 lemma Image_Un: "R `` (A \<union> B) = R `` A \<union> R `` B"
  1008   by blast
  1009 
  1010 lemma Un_Image: "(R \<union> S) `` A = R `` A \<union> S `` A"
  1011   by blast
  1012 
  1013 lemma Image_subset: "r \<subseteq> A \<times> B ==> r``C \<subseteq> B"
  1014   by (iprover intro!: subsetI elim!: ImageE dest!: subsetD SigmaD2)
  1015 
  1016 lemma Image_eq_UN: "r``B = (\<Union>y\<in> B. r``{y})"
  1017   -- {* NOT suitable for rewriting *}
  1018   by blast
  1019 
  1020 lemma Image_mono: "r' \<subseteq> r ==> A' \<subseteq> A ==> (r' `` A') \<subseteq> (r `` A)"
  1021   by blast
  1022 
  1023 lemma Image_UN: "(r `` (UNION A B)) = (\<Union>x\<in>A. r `` (B x))"
  1024   by blast
  1025 
  1026 lemma UN_Image: "(\<Union>i\<in>I. X i) `` S = (\<Union>i\<in>I. X i `` S)"
  1027   by auto
  1028 
  1029 lemma Image_INT_subset: "(r `` INTER A B) \<subseteq> (\<Inter>x\<in>A. r `` (B x))"
  1030   by blast
  1031 
  1032 text{*Converse inclusion requires some assumptions*}
  1033 lemma Image_INT_eq:
  1034      "[|single_valued (r\<inverse>); A\<noteq>{}|] ==> r `` INTER A B = (\<Inter>x\<in>A. r `` B x)"
  1035 apply (rule equalityI)
  1036  apply (rule Image_INT_subset) 
  1037 apply  (simp add: single_valued_def, blast)
  1038 done
  1039 
  1040 lemma Image_subset_eq: "(r``A \<subseteq> B) = (A \<subseteq> - ((r^-1) `` (-B)))"
  1041   by blast
  1042 
  1043 lemma Image_Collect_split [simp]: "{(x, y). P x y} `` A = {y. EX x:A. P x y}"
  1044   by auto
  1045 
  1046 lemma Sigma_Image: "(SIGMA x:A. B x) `` X = (\<Union>x\<in>X \<inter> A. B x)"
  1047   by auto
  1048 
  1049 lemma relcomp_Image: "(X O Y) `` Z = Y `` (X `` Z)"
  1050   by auto
  1051 
  1052 subsubsection {* Inverse image *}
  1053 
  1054 definition inv_image :: "'b rel \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a rel"
  1055 where
  1056   "inv_image r f = {(x, y). (f x, f y) \<in> r}"
  1057 
  1058 definition inv_imagep :: "('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
  1059 where
  1060   "inv_imagep r f = (\<lambda>x y. r (f x) (f y))"
  1061 
  1062 lemma [pred_set_conv]: "inv_imagep (\<lambda>x y. (x, y) \<in> r) f = (\<lambda>x y. (x, y) \<in> inv_image r f)"
  1063   by (simp add: inv_image_def inv_imagep_def)
  1064 
  1065 lemma sym_inv_image: "sym r ==> sym (inv_image r f)"
  1066   by (unfold sym_def inv_image_def) blast
  1067 
  1068 lemma trans_inv_image: "trans r ==> trans (inv_image r f)"
  1069   apply (unfold trans_def inv_image_def)
  1070   apply (simp (no_asm))
  1071   apply blast
  1072   done
  1073 
  1074 lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
  1075   by (auto simp:inv_image_def)
  1076 
  1077 lemma converse_inv_image[simp]: "(inv_image R f)^-1 = inv_image (R^-1) f"
  1078   unfolding inv_image_def converse_unfold by auto
  1079 
  1080 lemma in_inv_imagep [simp]: "inv_imagep r f x y = r (f x) (f y)"
  1081   by (simp add: inv_imagep_def)
  1082 
  1083 
  1084 subsubsection {* Powerset *}
  1085 
  1086 definition Powp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
  1087 where
  1088   "Powp A = (\<lambda>B. \<forall>x \<in> B. A x)"
  1089 
  1090 lemma Powp_Pow_eq [pred_set_conv]: "Powp (\<lambda>x. x \<in> A) = (\<lambda>x. x \<in> Pow A)"
  1091   by (auto simp add: Powp_def fun_eq_iff)
  1092 
  1093 lemmas Powp_mono [mono] = Pow_mono [to_pred]
  1094 
  1095 subsubsection {* Expressing relation operations via @{const Finite_Set.fold} *}
  1096 
  1097 lemma Id_on_fold:
  1098   assumes "finite A"
  1099   shows "Id_on A = Finite_Set.fold (\<lambda>x. Set.insert (Pair x x)) {} A"
  1100 proof -
  1101   interpret comp_fun_commute "\<lambda>x. Set.insert (Pair x x)" by default auto
  1102   show ?thesis using assms unfolding Id_on_def by (induct A) simp_all
  1103 qed
  1104 
  1105 lemma comp_fun_commute_Image_fold:
  1106   "comp_fun_commute (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A)"
  1107 proof -
  1108   interpret comp_fun_idem Set.insert
  1109       by (fact comp_fun_idem_insert)
  1110   show ?thesis 
  1111   by default (auto simp add: fun_eq_iff comp_fun_commute split:prod.split)
  1112 qed
  1113 
  1114 lemma Image_fold:
  1115   assumes "finite R"
  1116   shows "R `` S = Finite_Set.fold (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A) {} R"
  1117 proof -
  1118   interpret comp_fun_commute "(\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A)" 
  1119     by (rule comp_fun_commute_Image_fold)
  1120   have *: "\<And>x F. Set.insert x F `` S = (if fst x \<in> S then Set.insert (snd x) (F `` S) else (F `` S))"
  1121     by (force intro: rev_ImageI)
  1122   show ?thesis using assms by (induct R) (auto simp: *)
  1123 qed
  1124 
  1125 lemma insert_relcomp_union_fold:
  1126   assumes "finite S"
  1127   shows "{x} O S \<union> X = Finite_Set.fold (\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A') X S"
  1128 proof -
  1129   interpret comp_fun_commute "\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A'"
  1130   proof - 
  1131     interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
  1132     show "comp_fun_commute (\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A')"
  1133     by default (auto simp add: fun_eq_iff split:prod.split)
  1134   qed
  1135   have *: "{x} O S = {(x', z). x' = fst x \<and> (snd x,z) \<in> S}" by (auto simp: relcomp_unfold intro!: exI)
  1136   show ?thesis unfolding *
  1137   using `finite S` by (induct S) (auto split: prod.split)
  1138 qed
  1139 
  1140 lemma insert_relcomp_fold:
  1141   assumes "finite S"
  1142   shows "Set.insert x R O S = 
  1143     Finite_Set.fold (\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A') (R O S) S"
  1144 proof -
  1145   have "Set.insert x R O S = ({x} O S) \<union> (R O S)" by auto
  1146   then show ?thesis by (auto simp: insert_relcomp_union_fold[OF assms])
  1147 qed
  1148 
  1149 lemma comp_fun_commute_relcomp_fold:
  1150   assumes "finite S"
  1151   shows "comp_fun_commute (\<lambda>(x,y) A. 
  1152     Finite_Set.fold (\<lambda>(w,z) A'. if y = w then Set.insert (x,z) A' else A') A S)"
  1153 proof -
  1154   have *: "\<And>a b A. 
  1155     Finite_Set.fold (\<lambda>(w, z) A'. if b = w then Set.insert (a, z) A' else A') A S = {(a,b)} O S \<union> A"
  1156     by (auto simp: insert_relcomp_union_fold[OF assms] cong: if_cong)
  1157   show ?thesis by default (auto simp: *)
  1158 qed
  1159 
  1160 lemma relcomp_fold:
  1161   assumes "finite R"
  1162   assumes "finite S"
  1163   shows "R O S = Finite_Set.fold 
  1164     (\<lambda>(x,y) A. Finite_Set.fold (\<lambda>(w,z) A'. if y = w then Set.insert (x,z) A' else A') A S) {} R"
  1165   using assms by (induct R)
  1166     (auto simp: comp_fun_commute.fold_insert comp_fun_commute_relcomp_fold insert_relcomp_fold
  1167       cong: if_cong)
  1168 
  1169 end