src/HOL/Relation.thy
author blanchet
Wed Feb 12 08:35:57 2014 +0100 (2014-02-12)
changeset 55414 eab03e9cee8a
parent 55096 916b2ac758f4
child 56085 3d11892ea537
permissions -rw-r--r--
renamed '{prod,sum,bool,unit}_case' to 'case_...'
     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>x. (x, x) \<notin> r)"
   215 
   216 lemma irrefl_distinct [code]:
   217   "irrefl r \<longleftrightarrow> (\<forall>(x, y) \<in> r. x \<noteq> y)"
   218   by (auto simp add: irrefl_def)
   219 
   220 
   221 subsubsection {* Symmetry *}
   222 
   223 definition sym :: "'a rel \<Rightarrow> bool"
   224 where
   225   "sym r \<longleftrightarrow> (\<forall>x y. (x, y) \<in> r \<longrightarrow> (y, x) \<in> r)"
   226 
   227 definition symp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   228 where
   229   "symp r \<longleftrightarrow> (\<forall>x y. r x y \<longrightarrow> r y x)"
   230 
   231 lemma symp_sym_eq [pred_set_conv]:
   232   "symp (\<lambda>x y. (x, y) \<in> r) \<longleftrightarrow> sym r" 
   233   by (simp add: sym_def symp_def)
   234 
   235 lemma symI:
   236   "(\<And>a b. (a, b) \<in> r \<Longrightarrow> (b, a) \<in> r) \<Longrightarrow> sym r"
   237   by (unfold sym_def) iprover
   238 
   239 lemma sympI:
   240   "(\<And>a b. r a b \<Longrightarrow> r b a) \<Longrightarrow> symp r"
   241   by (fact symI [to_pred])
   242 
   243 lemma symE:
   244   assumes "sym r" and "(b, a) \<in> r"
   245   obtains "(a, b) \<in> r"
   246   using assms by (simp add: sym_def)
   247 
   248 lemma sympE:
   249   assumes "symp r" and "r b a"
   250   obtains "r a b"
   251   using assms by (rule symE [to_pred])
   252 
   253 lemma symD:
   254   assumes "sym r" and "(b, a) \<in> r"
   255   shows "(a, b) \<in> r"
   256   using assms by (rule symE)
   257 
   258 lemma sympD:
   259   assumes "symp r" and "r b a"
   260   shows "r a b"
   261   using assms by (rule symD [to_pred])
   262 
   263 lemma sym_Int:
   264   "sym r \<Longrightarrow> sym s \<Longrightarrow> sym (r \<inter> s)"
   265   by (fast intro: symI elim: symE)
   266 
   267 lemma symp_inf:
   268   "symp r \<Longrightarrow> symp s \<Longrightarrow> symp (r \<sqinter> s)"
   269   by (fact sym_Int [to_pred])
   270 
   271 lemma sym_Un:
   272   "sym r \<Longrightarrow> sym s \<Longrightarrow> sym (r \<union> s)"
   273   by (fast intro: symI elim: symE)
   274 
   275 lemma symp_sup:
   276   "symp r \<Longrightarrow> symp s \<Longrightarrow> symp (r \<squnion> s)"
   277   by (fact sym_Un [to_pred])
   278 
   279 lemma sym_INTER:
   280   "\<forall>x\<in>S. sym (r x) \<Longrightarrow> sym (INTER S r)"
   281   by (fast intro: symI elim: symE)
   282 
   283 lemma symp_INF:
   284   "\<forall>x\<in>S. symp (r x) \<Longrightarrow> symp (INFI S r)"
   285   by (fact sym_INTER [to_pred])
   286 
   287 lemma sym_UNION:
   288   "\<forall>x\<in>S. sym (r x) \<Longrightarrow> sym (UNION S r)"
   289   by (fast intro: symI elim: symE)
   290 
   291 lemma symp_SUP:
   292   "\<forall>x\<in>S. symp (r x) \<Longrightarrow> symp (SUPR S r)"
   293   by (fact sym_UNION [to_pred])
   294 
   295 
   296 subsubsection {* Antisymmetry *}
   297 
   298 definition antisym :: "'a rel \<Rightarrow> bool"
   299 where
   300   "antisym r \<longleftrightarrow> (\<forall>x y. (x, y) \<in> r \<longrightarrow> (y, x) \<in> r \<longrightarrow> x = y)"
   301 
   302 abbreviation antisymP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   303 where
   304   "antisymP r \<equiv> antisym {(x, y). r x y}"
   305 
   306 lemma antisymI:
   307   "(!!x y. (x, y) : r ==> (y, x) : r ==> x=y) ==> antisym r"
   308   by (unfold antisym_def) iprover
   309 
   310 lemma antisymD: "antisym r ==> (a, b) : r ==> (b, a) : r ==> a = b"
   311   by (unfold antisym_def) iprover
   312 
   313 lemma antisym_subset: "r \<subseteq> s ==> antisym s ==> antisym r"
   314   by (unfold antisym_def) blast
   315 
   316 lemma antisym_empty [simp]: "antisym {}"
   317   by (unfold antisym_def) blast
   318 
   319 
   320 subsubsection {* Transitivity *}
   321 
   322 definition trans :: "'a rel \<Rightarrow> bool"
   323 where
   324   "trans r \<longleftrightarrow> (\<forall>x y z. (x, y) \<in> r \<longrightarrow> (y, z) \<in> r \<longrightarrow> (x, z) \<in> r)"
   325 
   326 definition transp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   327 where
   328   "transp r \<longleftrightarrow> (\<forall>x y z. r x y \<longrightarrow> r y z \<longrightarrow> r x z)"
   329 
   330 lemma transp_trans_eq [pred_set_conv]:
   331   "transp (\<lambda>x y. (x, y) \<in> r) \<longleftrightarrow> trans r" 
   332   by (simp add: trans_def transp_def)
   333 
   334 abbreviation transP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
   335 where -- {* FIXME drop *}
   336   "transP r \<equiv> trans {(x, y). r x y}"
   337 
   338 lemma transI:
   339   "(\<And>x y z. (x, y) \<in> r \<Longrightarrow> (y, z) \<in> r \<Longrightarrow> (x, z) \<in> r) \<Longrightarrow> trans r"
   340   by (unfold trans_def) iprover
   341 
   342 lemma transpI:
   343   "(\<And>x y z. r x y \<Longrightarrow> r y z \<Longrightarrow> r x z) \<Longrightarrow> transp r"
   344   by (fact transI [to_pred])
   345 
   346 lemma transE:
   347   assumes "trans r" and "(x, y) \<in> r" and "(y, z) \<in> r"
   348   obtains "(x, z) \<in> r"
   349   using assms by (unfold trans_def) iprover
   350 
   351 lemma transpE:
   352   assumes "transp r" and "r x y" and "r y z"
   353   obtains "r x z"
   354   using assms by (rule transE [to_pred])
   355 
   356 lemma transD:
   357   assumes "trans r" and "(x, y) \<in> r" and "(y, z) \<in> r"
   358   shows "(x, z) \<in> r"
   359   using assms by (rule transE)
   360 
   361 lemma transpD:
   362   assumes "transp r" and "r x y" and "r y z"
   363   shows "r x z"
   364   using assms by (rule transD [to_pred])
   365 
   366 lemma trans_Int:
   367   "trans r \<Longrightarrow> trans s \<Longrightarrow> trans (r \<inter> s)"
   368   by (fast intro: transI elim: transE)
   369 
   370 lemma transp_inf:
   371   "transp r \<Longrightarrow> transp s \<Longrightarrow> transp (r \<sqinter> s)"
   372   by (fact trans_Int [to_pred])
   373 
   374 lemma trans_INTER:
   375   "\<forall>x\<in>S. trans (r x) \<Longrightarrow> trans (INTER S r)"
   376   by (fast intro: transI elim: transD)
   377 
   378 (* FIXME thm trans_INTER [to_pred] *)
   379 
   380 lemma trans_join [code]:
   381   "trans r \<longleftrightarrow> (\<forall>(x, y1) \<in> r. \<forall>(y2, z) \<in> r. y1 = y2 \<longrightarrow> (x, z) \<in> r)"
   382   by (auto simp add: trans_def)
   383 
   384 lemma transp_trans:
   385   "transp r \<longleftrightarrow> trans {(x, y). r x y}"
   386   by (simp add: trans_def transp_def)
   387 
   388 
   389 subsubsection {* Totality *}
   390 
   391 definition total_on :: "'a set \<Rightarrow> 'a rel \<Rightarrow> bool"
   392 where
   393   "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)"
   394 
   395 abbreviation "total \<equiv> total_on UNIV"
   396 
   397 lemma total_on_empty [simp]: "total_on {} r"
   398   by (simp add: total_on_def)
   399 
   400 
   401 subsubsection {* Single valued relations *}
   402 
   403 definition single_valued :: "('a \<times> 'b) set \<Rightarrow> bool"
   404 where
   405   "single_valued r \<longleftrightarrow> (\<forall>x y. (x, y) \<in> r \<longrightarrow> (\<forall>z. (x, z) \<in> r \<longrightarrow> y = z))"
   406 
   407 abbreviation single_valuedP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
   408   "single_valuedP r \<equiv> single_valued {(x, y). r x y}"
   409 
   410 lemma single_valuedI:
   411   "ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z) ==> single_valued r"
   412   by (unfold single_valued_def)
   413 
   414 lemma single_valuedD:
   415   "single_valued r ==> (x, y) : r ==> (x, z) : r ==> y = z"
   416   by (simp add: single_valued_def)
   417 
   418 lemma simgle_valued_empty[simp]: "single_valued {}"
   419 by(simp add: single_valued_def)
   420 
   421 lemma single_valued_subset:
   422   "r \<subseteq> s ==> single_valued s ==> single_valued r"
   423   by (unfold single_valued_def) blast
   424 
   425 
   426 subsection {* Relation operations *}
   427 
   428 subsubsection {* The identity relation *}
   429 
   430 definition Id :: "'a rel"
   431 where
   432   [code del]: "Id = {p. \<exists>x. p = (x, x)}"
   433 
   434 lemma IdI [intro]: "(a, a) : Id"
   435   by (simp add: Id_def)
   436 
   437 lemma IdE [elim!]: "p : Id ==> (!!x. p = (x, x) ==> P) ==> P"
   438   by (unfold Id_def) (iprover elim: CollectE)
   439 
   440 lemma pair_in_Id_conv [iff]: "((a, b) : Id) = (a = b)"
   441   by (unfold Id_def) blast
   442 
   443 lemma refl_Id: "refl Id"
   444   by (simp add: refl_on_def)
   445 
   446 lemma antisym_Id: "antisym Id"
   447   -- {* A strange result, since @{text Id} is also symmetric. *}
   448   by (simp add: antisym_def)
   449 
   450 lemma sym_Id: "sym Id"
   451   by (simp add: sym_def)
   452 
   453 lemma trans_Id: "trans Id"
   454   by (simp add: trans_def)
   455 
   456 lemma single_valued_Id [simp]: "single_valued Id"
   457   by (unfold single_valued_def) blast
   458 
   459 lemma irrefl_diff_Id [simp]: "irrefl (r - Id)"
   460   by (simp add:irrefl_def)
   461 
   462 lemma trans_diff_Id: "trans r \<Longrightarrow> antisym r \<Longrightarrow> trans (r - Id)"
   463   unfolding antisym_def trans_def by blast
   464 
   465 lemma total_on_diff_Id [simp]: "total_on A (r - Id) = total_on A r"
   466   by (simp add: total_on_def)
   467 
   468 
   469 subsubsection {* Diagonal: identity over a set *}
   470 
   471 definition Id_on  :: "'a set \<Rightarrow> 'a rel"
   472 where
   473   "Id_on A = (\<Union>x\<in>A. {(x, x)})"
   474 
   475 lemma Id_on_empty [simp]: "Id_on {} = {}"
   476   by (simp add: Id_on_def) 
   477 
   478 lemma Id_on_eqI: "a = b ==> a : A ==> (a, b) : Id_on A"
   479   by (simp add: Id_on_def)
   480 
   481 lemma Id_onI [intro!]: "a : A ==> (a, a) : Id_on A"
   482   by (rule Id_on_eqI) (rule refl)
   483 
   484 lemma Id_onE [elim!]:
   485   "c : Id_on A ==> (!!x. x : A ==> c = (x, x) ==> P) ==> P"
   486   -- {* The general elimination rule. *}
   487   by (unfold Id_on_def) (iprover elim!: UN_E singletonE)
   488 
   489 lemma Id_on_iff: "((x, y) : Id_on A) = (x = y & x : A)"
   490   by blast
   491 
   492 lemma Id_on_def' [nitpick_unfold]:
   493   "Id_on {x. A x} = Collect (\<lambda>(x, y). x = y \<and> A x)"
   494   by auto
   495 
   496 lemma Id_on_subset_Times: "Id_on A \<subseteq> A \<times> A"
   497   by blast
   498 
   499 lemma refl_on_Id_on: "refl_on A (Id_on A)"
   500   by (rule refl_onI [OF Id_on_subset_Times Id_onI])
   501 
   502 lemma antisym_Id_on [simp]: "antisym (Id_on A)"
   503   by (unfold antisym_def) blast
   504 
   505 lemma sym_Id_on [simp]: "sym (Id_on A)"
   506   by (rule symI) clarify
   507 
   508 lemma trans_Id_on [simp]: "trans (Id_on A)"
   509   by (fast intro: transI elim: transD)
   510 
   511 lemma single_valued_Id_on [simp]: "single_valued (Id_on A)"
   512   by (unfold single_valued_def) blast
   513 
   514 
   515 subsubsection {* Composition *}
   516 
   517 inductive_set relcomp  :: "('a \<times> 'b) set \<Rightarrow> ('b \<times> 'c) set \<Rightarrow> ('a \<times> 'c) set" (infixr "O" 75)
   518   for r :: "('a \<times> 'b) set" and s :: "('b \<times> 'c) set"
   519 where
   520   relcompI [intro]: "(a, b) \<in> r \<Longrightarrow> (b, c) \<in> s \<Longrightarrow> (a, c) \<in> r O s"
   521 
   522 notation relcompp (infixr "OO" 75)
   523 
   524 lemmas relcomppI = relcompp.intros
   525 
   526 text {*
   527   For historic reasons, the elimination rules are not wholly corresponding.
   528   Feel free to consolidate this.
   529 *}
   530 
   531 inductive_cases relcompEpair: "(a, c) \<in> r O s"
   532 inductive_cases relcomppE [elim!]: "(r OO s) a c"
   533 
   534 lemma relcompE [elim!]: "xz \<in> r O s \<Longrightarrow>
   535   (\<And>x y z. xz = (x, z) \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> (y, z) \<in> s  \<Longrightarrow> P) \<Longrightarrow> P"
   536   by (cases xz) (simp, erule relcompEpair, iprover)
   537 
   538 lemma R_O_Id [simp]:
   539   "R O Id = R"
   540   by fast
   541 
   542 lemma Id_O_R [simp]:
   543   "Id O R = R"
   544   by fast
   545 
   546 lemma relcomp_empty1 [simp]:
   547   "{} O R = {}"
   548   by blast
   549 
   550 lemma relcompp_bot1 [simp]:
   551   "\<bottom> OO R = \<bottom>"
   552   by (fact relcomp_empty1 [to_pred])
   553 
   554 lemma relcomp_empty2 [simp]:
   555   "R O {} = {}"
   556   by blast
   557 
   558 lemma relcompp_bot2 [simp]:
   559   "R OO \<bottom> = \<bottom>"
   560   by (fact relcomp_empty2 [to_pred])
   561 
   562 lemma O_assoc:
   563   "(R O S) O T = R O (S O T)"
   564   by blast
   565 
   566 
   567 lemma relcompp_assoc:
   568   "(r OO s) OO t = r OO (s OO t)"
   569   by (fact O_assoc [to_pred])
   570 
   571 lemma trans_O_subset:
   572   "trans r \<Longrightarrow> r O r \<subseteq> r"
   573   by (unfold trans_def) blast
   574 
   575 lemma transp_relcompp_less_eq:
   576   "transp r \<Longrightarrow> r OO r \<le> r "
   577   by (fact trans_O_subset [to_pred])
   578 
   579 lemma relcomp_mono:
   580   "r' \<subseteq> r \<Longrightarrow> s' \<subseteq> s \<Longrightarrow> r' O s' \<subseteq> r O s"
   581   by blast
   582 
   583 lemma relcompp_mono:
   584   "r' \<le> r \<Longrightarrow> s' \<le> s \<Longrightarrow> r' OO s' \<le> r OO s "
   585   by (fact relcomp_mono [to_pred])
   586 
   587 lemma relcomp_subset_Sigma:
   588   "r \<subseteq> A \<times> B \<Longrightarrow> s \<subseteq> B \<times> C \<Longrightarrow> r O s \<subseteq> A \<times> C"
   589   by blast
   590 
   591 lemma relcomp_distrib [simp]:
   592   "R O (S \<union> T) = (R O S) \<union> (R O T)" 
   593   by auto
   594 
   595 lemma relcompp_distrib [simp]:
   596   "R OO (S \<squnion> T) = R OO S \<squnion> R OO T"
   597   by (fact relcomp_distrib [to_pred])
   598 
   599 lemma relcomp_distrib2 [simp]:
   600   "(S \<union> T) O R = (S O R) \<union> (T O R)"
   601   by auto
   602 
   603 lemma relcompp_distrib2 [simp]:
   604   "(S \<squnion> T) OO R = S OO R \<squnion> T OO R"
   605   by (fact relcomp_distrib2 [to_pred])
   606 
   607 lemma relcomp_UNION_distrib:
   608   "s O UNION I r = (\<Union>i\<in>I. s O r i) "
   609   by auto
   610 
   611 (* FIXME thm relcomp_UNION_distrib [to_pred] *)
   612 
   613 lemma relcomp_UNION_distrib2:
   614   "UNION I r O s = (\<Union>i\<in>I. r i O s) "
   615   by auto
   616 
   617 (* FIXME thm relcomp_UNION_distrib2 [to_pred] *)
   618 
   619 lemma single_valued_relcomp:
   620   "single_valued r \<Longrightarrow> single_valued s \<Longrightarrow> single_valued (r O s)"
   621   by (unfold single_valued_def) blast
   622 
   623 lemma relcomp_unfold:
   624   "r O s = {(x, z). \<exists>y. (x, y) \<in> r \<and> (y, z) \<in> s}"
   625   by (auto simp add: set_eq_iff)
   626 
   627 lemma eq_OO: "op= OO R = R"
   628 by blast
   629 
   630 
   631 subsubsection {* Converse *}
   632 
   633 inductive_set converse :: "('a \<times> 'b) set \<Rightarrow> ('b \<times> 'a) set" ("(_^-1)" [1000] 999)
   634   for r :: "('a \<times> 'b) set"
   635 where
   636   "(a, b) \<in> r \<Longrightarrow> (b, a) \<in> r^-1"
   637 
   638 notation (xsymbols)
   639   converse  ("(_\<inverse>)" [1000] 999)
   640 
   641 notation
   642   conversep ("(_^--1)" [1000] 1000)
   643 
   644 notation (xsymbols)
   645   conversep  ("(_\<inverse>\<inverse>)" [1000] 1000)
   646 
   647 lemma converseI [sym]:
   648   "(a, b) \<in> r \<Longrightarrow> (b, a) \<in> r\<inverse>"
   649   by (fact converse.intros)
   650 
   651 lemma conversepI (* CANDIDATE [sym] *):
   652   "r a b \<Longrightarrow> r\<inverse>\<inverse> b a"
   653   by (fact conversep.intros)
   654 
   655 lemma converseD [sym]:
   656   "(a, b) \<in> r\<inverse> \<Longrightarrow> (b, a) \<in> r"
   657   by (erule converse.cases) iprover
   658 
   659 lemma conversepD (* CANDIDATE [sym] *):
   660   "r\<inverse>\<inverse> b a \<Longrightarrow> r a b"
   661   by (fact converseD [to_pred])
   662 
   663 lemma converseE [elim!]:
   664   -- {* More general than @{text converseD}, as it ``splits'' the member of the relation. *}
   665   "yx \<in> r\<inverse> \<Longrightarrow> (\<And>x y. yx = (y, x) \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> P) \<Longrightarrow> P"
   666   by (cases yx) (simp, erule converse.cases, iprover)
   667 
   668 lemmas conversepE [elim!] = conversep.cases
   669 
   670 lemma converse_iff [iff]:
   671   "(a, b) \<in> r\<inverse> \<longleftrightarrow> (b, a) \<in> r"
   672   by (auto intro: converseI)
   673 
   674 lemma conversep_iff [iff]:
   675   "r\<inverse>\<inverse> a b = r b a"
   676   by (fact converse_iff [to_pred])
   677 
   678 lemma converse_converse [simp]:
   679   "(r\<inverse>)\<inverse> = r"
   680   by (simp add: set_eq_iff)
   681 
   682 lemma conversep_conversep [simp]:
   683   "(r\<inverse>\<inverse>)\<inverse>\<inverse> = r"
   684   by (fact converse_converse [to_pred])
   685 
   686 lemma converse_empty[simp]: "{}\<inverse> = {}"
   687 by auto
   688 
   689 lemma converse_UNIV[simp]: "UNIV\<inverse> = UNIV"
   690 by auto
   691 
   692 lemma converse_relcomp: "(r O s)^-1 = s^-1 O r^-1"
   693   by blast
   694 
   695 lemma converse_relcompp: "(r OO s)^--1 = s^--1 OO r^--1"
   696   by (iprover intro: order_antisym conversepI relcomppI
   697     elim: relcomppE dest: conversepD)
   698 
   699 lemma converse_Int: "(r \<inter> s)^-1 = r^-1 \<inter> s^-1"
   700   by blast
   701 
   702 lemma converse_meet: "(r \<sqinter> s)^--1 = r^--1 \<sqinter> s^--1"
   703   by (simp add: inf_fun_def) (iprover intro: conversepI ext dest: conversepD)
   704 
   705 lemma converse_Un: "(r \<union> s)^-1 = r^-1 \<union> s^-1"
   706   by blast
   707 
   708 lemma converse_join: "(r \<squnion> s)^--1 = r^--1 \<squnion> s^--1"
   709   by (simp add: sup_fun_def) (iprover intro: conversepI ext dest: conversepD)
   710 
   711 lemma converse_INTER: "(INTER S r)^-1 = (INT x:S. (r x)^-1)"
   712   by fast
   713 
   714 lemma converse_UNION: "(UNION S r)^-1 = (UN x:S. (r x)^-1)"
   715   by blast
   716 
   717 lemma converse_mono[simp]: "r^-1 \<subseteq> s ^-1 \<longleftrightarrow> r \<subseteq> s"
   718   by auto
   719 
   720 lemma conversep_mono[simp]: "r^--1 \<le> s ^--1 \<longleftrightarrow> r \<le> s"
   721   by (fact converse_mono[to_pred])
   722 
   723 lemma converse_inject[simp]: "r^-1 = s ^-1 \<longleftrightarrow> r = s"
   724   by auto
   725 
   726 lemma conversep_inject[simp]: "r^--1 = s ^--1 \<longleftrightarrow> r = s"
   727   by (fact converse_inject[to_pred])
   728 
   729 lemma converse_subset_swap: "r \<subseteq> s ^-1 = (r ^-1 \<subseteq> s)"
   730   by auto
   731 
   732 lemma conversep_le_swap: "r \<le> s ^--1 = (r ^--1 \<le> s)"
   733   by (fact converse_subset_swap[to_pred])
   734 
   735 lemma converse_Id [simp]: "Id^-1 = Id"
   736   by blast
   737 
   738 lemma converse_Id_on [simp]: "(Id_on A)^-1 = Id_on A"
   739   by blast
   740 
   741 lemma refl_on_converse [simp]: "refl_on A (converse r) = refl_on A r"
   742   by (unfold refl_on_def) auto
   743 
   744 lemma sym_converse [simp]: "sym (converse r) = sym r"
   745   by (unfold sym_def) blast
   746 
   747 lemma antisym_converse [simp]: "antisym (converse r) = antisym r"
   748   by (unfold antisym_def) blast
   749 
   750 lemma trans_converse [simp]: "trans (converse r) = trans r"
   751   by (unfold trans_def) blast
   752 
   753 lemma sym_conv_converse_eq: "sym r = (r^-1 = r)"
   754   by (unfold sym_def) fast
   755 
   756 lemma sym_Un_converse: "sym (r \<union> r^-1)"
   757   by (unfold sym_def) blast
   758 
   759 lemma sym_Int_converse: "sym (r \<inter> r^-1)"
   760   by (unfold sym_def) blast
   761 
   762 lemma total_on_converse [simp]: "total_on A (r^-1) = total_on A r"
   763   by (auto simp: total_on_def)
   764 
   765 lemma finite_converse [iff]: "finite (r^-1) = finite r"  
   766   unfolding converse_def conversep_iff using [[simproc add: finite_Collect]]
   767   by (auto elim: finite_imageD simp: inj_on_def)
   768 
   769 lemma conversep_noteq [simp]: "(op \<noteq>)^--1 = op \<noteq>"
   770   by (auto simp add: fun_eq_iff)
   771 
   772 lemma conversep_eq [simp]: "(op =)^--1 = op ="
   773   by (auto simp add: fun_eq_iff)
   774 
   775 lemma converse_unfold [code]:
   776   "r\<inverse> = {(y, x). (x, y) \<in> r}"
   777   by (simp add: set_eq_iff)
   778 
   779 
   780 subsubsection {* Domain, range and field *}
   781 
   782 inductive_set Domain :: "('a \<times> 'b) set \<Rightarrow> 'a set"
   783   for r :: "('a \<times> 'b) set"
   784 where
   785   DomainI [intro]: "(a, b) \<in> r \<Longrightarrow> a \<in> Domain r"
   786 
   787 abbreviation (input) "DomainP \<equiv> Domainp"
   788 
   789 lemmas DomainPI = Domainp.DomainI
   790 
   791 inductive_cases DomainE [elim!]: "a \<in> Domain r"
   792 inductive_cases DomainpE [elim!]: "Domainp r a"
   793 
   794 inductive_set Range :: "('a \<times> 'b) set \<Rightarrow> 'b set"
   795   for r :: "('a \<times> 'b) set"
   796 where
   797   RangeI [intro]: "(a, b) \<in> r \<Longrightarrow> b \<in> Range r"
   798 
   799 abbreviation (input) "RangeP \<equiv> Rangep"
   800 
   801 lemmas RangePI = Rangep.RangeI
   802 
   803 inductive_cases RangeE [elim!]: "b \<in> Range r"
   804 inductive_cases RangepE [elim!]: "Rangep r b"
   805 
   806 definition Field :: "'a rel \<Rightarrow> 'a set"
   807 where
   808   "Field r = Domain r \<union> Range r"
   809 
   810 lemma Domain_fst [code]:
   811   "Domain r = fst ` r"
   812   by force
   813 
   814 lemma Range_snd [code]:
   815   "Range r = snd ` r"
   816   by force
   817 
   818 lemma fst_eq_Domain: "fst ` R = Domain R"
   819   by force
   820 
   821 lemma snd_eq_Range: "snd ` R = Range R"
   822   by force
   823 
   824 lemma Domain_empty [simp]: "Domain {} = {}"
   825   by auto
   826 
   827 lemma Range_empty [simp]: "Range {} = {}"
   828   by auto
   829 
   830 lemma Field_empty [simp]: "Field {} = {}"
   831   by (simp add: Field_def)
   832 
   833 lemma Domain_empty_iff: "Domain r = {} \<longleftrightarrow> r = {}"
   834   by auto
   835 
   836 lemma Range_empty_iff: "Range r = {} \<longleftrightarrow> r = {}"
   837   by auto
   838 
   839 lemma Domain_insert [simp]: "Domain (insert (a, b) r) = insert a (Domain r)"
   840   by blast
   841 
   842 lemma Range_insert [simp]: "Range (insert (a, b) r) = insert b (Range r)"
   843   by blast
   844 
   845 lemma Field_insert [simp]: "Field (insert (a, b) r) = {a, b} \<union> Field r"
   846   by (auto simp add: Field_def)
   847 
   848 lemma Domain_iff: "a \<in> Domain r \<longleftrightarrow> (\<exists>y. (a, y) \<in> r)"
   849   by blast
   850 
   851 lemma Range_iff: "a \<in> Range r \<longleftrightarrow> (\<exists>y. (y, a) \<in> r)"
   852   by blast
   853 
   854 lemma Domain_Id [simp]: "Domain Id = UNIV"
   855   by blast
   856 
   857 lemma Range_Id [simp]: "Range Id = UNIV"
   858   by blast
   859 
   860 lemma Domain_Id_on [simp]: "Domain (Id_on A) = A"
   861   by blast
   862 
   863 lemma Range_Id_on [simp]: "Range (Id_on A) = A"
   864   by blast
   865 
   866 lemma Domain_Un_eq: "Domain (A \<union> B) = Domain A \<union> Domain B"
   867   by blast
   868 
   869 lemma Range_Un_eq: "Range (A \<union> B) = Range A \<union> Range B"
   870   by blast
   871 
   872 lemma Field_Un [simp]: "Field (r \<union> s) = Field r \<union> Field s"
   873   by (auto simp: Field_def)
   874 
   875 lemma Domain_Int_subset: "Domain (A \<inter> B) \<subseteq> Domain A \<inter> Domain B"
   876   by blast
   877 
   878 lemma Range_Int_subset: "Range (A \<inter> B) \<subseteq> Range A \<inter> Range B"
   879   by blast
   880 
   881 lemma Domain_Diff_subset: "Domain A - Domain B \<subseteq> Domain (A - B)"
   882   by blast
   883 
   884 lemma Range_Diff_subset: "Range A - Range B \<subseteq> Range (A - B)"
   885   by blast
   886 
   887 lemma Domain_Union: "Domain (\<Union>S) = (\<Union>A\<in>S. Domain A)"
   888   by blast
   889 
   890 lemma Range_Union: "Range (\<Union>S) = (\<Union>A\<in>S. Range A)"
   891   by blast
   892 
   893 lemma Field_Union [simp]: "Field (\<Union>R) = \<Union>(Field ` R)"
   894   by (auto simp: Field_def)
   895 
   896 lemma Domain_converse [simp]: "Domain (r\<inverse>) = Range r"
   897   by auto
   898 
   899 lemma Range_converse [simp]: "Range (r\<inverse>) = Domain r"
   900   by blast
   901 
   902 lemma Field_converse [simp]: "Field (r\<inverse>) = Field r"
   903   by (auto simp: Field_def)
   904 
   905 lemma Domain_Collect_split [simp]: "Domain {(x, y). P x y} = {x. EX y. P x y}"
   906   by auto
   907 
   908 lemma Range_Collect_split [simp]: "Range {(x, y). P x y} = {y. EX x. P x y}"
   909   by auto
   910 
   911 lemma finite_Domain: "finite r \<Longrightarrow> finite (Domain r)"
   912   by (induct set: finite) auto
   913 
   914 lemma finite_Range: "finite r \<Longrightarrow> finite (Range r)"
   915   by (induct set: finite) auto
   916 
   917 lemma finite_Field: "finite r \<Longrightarrow> finite (Field r)"
   918   by (simp add: Field_def finite_Domain finite_Range)
   919 
   920 lemma Domain_mono: "r \<subseteq> s \<Longrightarrow> Domain r \<subseteq> Domain s"
   921   by blast
   922 
   923 lemma Range_mono: "r \<subseteq> s \<Longrightarrow> Range r \<subseteq> Range s"
   924   by blast
   925 
   926 lemma mono_Field: "r \<subseteq> s \<Longrightarrow> Field r \<subseteq> Field s"
   927   by (auto simp: Field_def Domain_def Range_def)
   928 
   929 lemma Domain_unfold:
   930   "Domain r = {x. \<exists>y. (x, y) \<in> r}"
   931   by blast
   932 
   933 
   934 subsubsection {* Image of a set under a relation *}
   935 
   936 definition Image :: "('a \<times> 'b) set \<Rightarrow> 'a set \<Rightarrow> 'b set" (infixr "``" 90)
   937 where
   938   "r `` s = {y. \<exists>x\<in>s. (x, y) \<in> r}"
   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]: "(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 UN_Image: "(\<Union>i\<in>I. X i) `` S = (\<Union>i\<in>I. X i `` S)"
   996   by auto
   997 
   998 lemma Image_INT_subset: "(r `` INTER A B) \<subseteq> (\<Inter>x\<in>A. r `` (B x))"
   999   by blast
  1000 
  1001 text{*Converse inclusion requires some assumptions*}
  1002 lemma Image_INT_eq:
  1003      "[|single_valued (r\<inverse>); A\<noteq>{}|] ==> r `` INTER A B = (\<Inter>x\<in>A. r `` B x)"
  1004 apply (rule equalityI)
  1005  apply (rule Image_INT_subset) 
  1006 apply  (simp add: single_valued_def, blast)
  1007 done
  1008 
  1009 lemma Image_subset_eq: "(r``A \<subseteq> B) = (A \<subseteq> - ((r^-1) `` (-B)))"
  1010   by blast
  1011 
  1012 lemma Image_Collect_split [simp]: "{(x, y). P x y} `` A = {y. EX x:A. P x y}"
  1013   by auto
  1014 
  1015 lemma Sigma_Image: "(SIGMA x:A. B x) `` X = (\<Union>x\<in>X \<inter> A. B x)"
  1016   by auto
  1017 
  1018 lemma relcomp_Image: "(X O Y) `` Z = Y `` (X `` Z)"
  1019   by auto
  1020 
  1021 subsubsection {* Inverse image *}
  1022 
  1023 definition inv_image :: "'b rel \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a rel"
  1024 where
  1025   "inv_image r f = {(x, y). (f x, f y) \<in> r}"
  1026 
  1027 definition inv_imagep :: "('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
  1028 where
  1029   "inv_imagep r f = (\<lambda>x y. r (f x) (f y))"
  1030 
  1031 lemma [pred_set_conv]: "inv_imagep (\<lambda>x y. (x, y) \<in> r) f = (\<lambda>x y. (x, y) \<in> inv_image r f)"
  1032   by (simp add: inv_image_def inv_imagep_def)
  1033 
  1034 lemma sym_inv_image: "sym r ==> sym (inv_image r f)"
  1035   by (unfold sym_def inv_image_def) blast
  1036 
  1037 lemma trans_inv_image: "trans r ==> trans (inv_image r f)"
  1038   apply (unfold trans_def inv_image_def)
  1039   apply (simp (no_asm))
  1040   apply blast
  1041   done
  1042 
  1043 lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
  1044   by (auto simp:inv_image_def)
  1045 
  1046 lemma converse_inv_image[simp]: "(inv_image R f)^-1 = inv_image (R^-1) f"
  1047   unfolding inv_image_def converse_unfold by auto
  1048 
  1049 lemma in_inv_imagep [simp]: "inv_imagep r f x y = r (f x) (f y)"
  1050   by (simp add: inv_imagep_def)
  1051 
  1052 
  1053 subsubsection {* Powerset *}
  1054 
  1055 definition Powp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
  1056 where
  1057   "Powp A = (\<lambda>B. \<forall>x \<in> B. A x)"
  1058 
  1059 lemma Powp_Pow_eq [pred_set_conv]: "Powp (\<lambda>x. x \<in> A) = (\<lambda>x. x \<in> Pow A)"
  1060   by (auto simp add: Powp_def fun_eq_iff)
  1061 
  1062 lemmas Powp_mono [mono] = Pow_mono [to_pred]
  1063 
  1064 subsubsection {* Expressing relation operations via @{const Finite_Set.fold} *}
  1065 
  1066 lemma Id_on_fold:
  1067   assumes "finite A"
  1068   shows "Id_on A = Finite_Set.fold (\<lambda>x. Set.insert (Pair x x)) {} A"
  1069 proof -
  1070   interpret comp_fun_commute "\<lambda>x. Set.insert (Pair x x)" by default auto
  1071   show ?thesis using assms unfolding Id_on_def by (induct A) simp_all
  1072 qed
  1073 
  1074 lemma comp_fun_commute_Image_fold:
  1075   "comp_fun_commute (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A)"
  1076 proof -
  1077   interpret comp_fun_idem Set.insert
  1078       by (fact comp_fun_idem_insert)
  1079   show ?thesis 
  1080   by default (auto simp add: fun_eq_iff comp_fun_commute split:prod.split)
  1081 qed
  1082 
  1083 lemma Image_fold:
  1084   assumes "finite R"
  1085   shows "R `` S = Finite_Set.fold (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A) {} R"
  1086 proof -
  1087   interpret comp_fun_commute "(\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A)" 
  1088     by (rule comp_fun_commute_Image_fold)
  1089   have *: "\<And>x F. Set.insert x F `` S = (if fst x \<in> S then Set.insert (snd x) (F `` S) else (F `` S))"
  1090     by (force intro: rev_ImageI)
  1091   show ?thesis using assms by (induct R) (auto simp: *)
  1092 qed
  1093 
  1094 lemma insert_relcomp_union_fold:
  1095   assumes "finite S"
  1096   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"
  1097 proof -
  1098   interpret comp_fun_commute "\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A'"
  1099   proof - 
  1100     interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
  1101     show "comp_fun_commute (\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A')"
  1102     by default (auto simp add: fun_eq_iff split:prod.split)
  1103   qed
  1104   have *: "{x} O S = {(x', z). x' = fst x \<and> (snd x,z) \<in> S}" by (auto simp: relcomp_unfold intro!: exI)
  1105   show ?thesis unfolding *
  1106   using `finite S` by (induct S) (auto split: prod.split)
  1107 qed
  1108 
  1109 lemma insert_relcomp_fold:
  1110   assumes "finite S"
  1111   shows "Set.insert x R O S = 
  1112     Finite_Set.fold (\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A') (R O S) S"
  1113 proof -
  1114   have "Set.insert x R O S = ({x} O S) \<union> (R O S)" by auto
  1115   then show ?thesis by (auto simp: insert_relcomp_union_fold[OF assms])
  1116 qed
  1117 
  1118 lemma comp_fun_commute_relcomp_fold:
  1119   assumes "finite S"
  1120   shows "comp_fun_commute (\<lambda>(x,y) A. 
  1121     Finite_Set.fold (\<lambda>(w,z) A'. if y = w then Set.insert (x,z) A' else A') A S)"
  1122 proof -
  1123   have *: "\<And>a b A. 
  1124     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"
  1125     by (auto simp: insert_relcomp_union_fold[OF assms] cong: if_cong)
  1126   show ?thesis by default (auto simp: *)
  1127 qed
  1128 
  1129 lemma relcomp_fold:
  1130   assumes "finite R"
  1131   assumes "finite S"
  1132   shows "R O S = Finite_Set.fold 
  1133     (\<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"
  1134   using assms by (induct R)
  1135     (auto simp: comp_fun_commute.fold_insert comp_fun_commute_relcomp_fold insert_relcomp_fold
  1136       cong: if_cong)
  1137 
  1138 
  1139 
  1140 end
  1141