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