src/HOL/Relation.thy
author traytel
Fri Nov 29 08:26:45 2013 +0100 (2013-11-29)
changeset 54611 31afce809794
parent 54555 e8c5e95d338b
child 55083 0a689157e3ce
permissions -rw-r--r--
set_comprehension_pointfree simproc causes to many surprises if enabled by default
     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 "prod_case 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 (prod_case ` 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 (prod_case ` 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 
   628 subsubsection {* Converse *}
   629 
   630 inductive_set converse :: "('a \<times> 'b) set \<Rightarrow> ('b \<times> 'a) set" ("(_^-1)" [1000] 999)
   631   for r :: "('a \<times> 'b) set"
   632 where
   633   "(a, b) \<in> r \<Longrightarrow> (b, a) \<in> r^-1"
   634 
   635 notation (xsymbols)
   636   converse  ("(_\<inverse>)" [1000] 999)
   637 
   638 notation
   639   conversep ("(_^--1)" [1000] 1000)
   640 
   641 notation (xsymbols)
   642   conversep  ("(_\<inverse>\<inverse>)" [1000] 1000)
   643 
   644 lemma converseI [sym]:
   645   "(a, b) \<in> r \<Longrightarrow> (b, a) \<in> r\<inverse>"
   646   by (fact converse.intros)
   647 
   648 lemma conversepI (* CANDIDATE [sym] *):
   649   "r a b \<Longrightarrow> r\<inverse>\<inverse> b a"
   650   by (fact conversep.intros)
   651 
   652 lemma converseD [sym]:
   653   "(a, b) \<in> r\<inverse> \<Longrightarrow> (b, a) \<in> r"
   654   by (erule converse.cases) iprover
   655 
   656 lemma conversepD (* CANDIDATE [sym] *):
   657   "r\<inverse>\<inverse> b a \<Longrightarrow> r a b"
   658   by (fact converseD [to_pred])
   659 
   660 lemma converseE [elim!]:
   661   -- {* More general than @{text converseD}, as it ``splits'' the member of the relation. *}
   662   "yx \<in> r\<inverse> \<Longrightarrow> (\<And>x y. yx = (y, x) \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> P) \<Longrightarrow> P"
   663   by (cases yx) (simp, erule converse.cases, iprover)
   664 
   665 lemmas conversepE [elim!] = conversep.cases
   666 
   667 lemma converse_iff [iff]:
   668   "(a, b) \<in> r\<inverse> \<longleftrightarrow> (b, a) \<in> r"
   669   by (auto intro: converseI)
   670 
   671 lemma conversep_iff [iff]:
   672   "r\<inverse>\<inverse> a b = r b a"
   673   by (fact converse_iff [to_pred])
   674 
   675 lemma converse_converse [simp]:
   676   "(r\<inverse>)\<inverse> = r"
   677   by (simp add: set_eq_iff)
   678 
   679 lemma conversep_conversep [simp]:
   680   "(r\<inverse>\<inverse>)\<inverse>\<inverse> = r"
   681   by (fact converse_converse [to_pred])
   682 
   683 lemma converse_empty[simp]: "{}\<inverse> = {}"
   684 by auto
   685 
   686 lemma converse_UNIV[simp]: "UNIV\<inverse> = UNIV"
   687 by auto
   688 
   689 lemma converse_relcomp: "(r O s)^-1 = s^-1 O r^-1"
   690   by blast
   691 
   692 lemma converse_relcompp: "(r OO s)^--1 = s^--1 OO r^--1"
   693   by (iprover intro: order_antisym conversepI relcomppI
   694     elim: relcomppE dest: conversepD)
   695 
   696 lemma converse_Int: "(r \<inter> s)^-1 = r^-1 \<inter> s^-1"
   697   by blast
   698 
   699 lemma converse_meet: "(r \<sqinter> s)^--1 = r^--1 \<sqinter> s^--1"
   700   by (simp add: inf_fun_def) (iprover intro: conversepI ext dest: conversepD)
   701 
   702 lemma converse_Un: "(r \<union> s)^-1 = r^-1 \<union> s^-1"
   703   by blast
   704 
   705 lemma converse_join: "(r \<squnion> s)^--1 = r^--1 \<squnion> s^--1"
   706   by (simp add: sup_fun_def) (iprover intro: conversepI ext dest: conversepD)
   707 
   708 lemma converse_INTER: "(INTER S r)^-1 = (INT x:S. (r x)^-1)"
   709   by fast
   710 
   711 lemma converse_UNION: "(UNION S r)^-1 = (UN x:S. (r x)^-1)"
   712   by blast
   713 
   714 lemma converse_mono[simp]: "r^-1 \<subseteq> s ^-1 \<longleftrightarrow> r \<subseteq> s"
   715   by auto
   716 
   717 lemma conversep_mono[simp]: "r^--1 \<le> s ^--1 \<longleftrightarrow> r \<le> s"
   718   by (fact converse_mono[to_pred])
   719 
   720 lemma converse_inject[simp]: "r^-1 = s ^-1 \<longleftrightarrow> r = s"
   721   by auto
   722 
   723 lemma conversep_inject[simp]: "r^--1 = s ^--1 \<longleftrightarrow> r = s"
   724   by (fact converse_inject[to_pred])
   725 
   726 lemma converse_subset_swap: "r \<subseteq> s ^-1 = (r ^-1 \<subseteq> s)"
   727   by auto
   728 
   729 lemma conversep_le_swap: "r \<le> s ^--1 = (r ^--1 \<le> s)"
   730   by (fact converse_subset_swap[to_pred])
   731 
   732 lemma converse_Id [simp]: "Id^-1 = Id"
   733   by blast
   734 
   735 lemma converse_Id_on [simp]: "(Id_on A)^-1 = Id_on A"
   736   by blast
   737 
   738 lemma refl_on_converse [simp]: "refl_on A (converse r) = refl_on A r"
   739   by (unfold refl_on_def) auto
   740 
   741 lemma sym_converse [simp]: "sym (converse r) = sym r"
   742   by (unfold sym_def) blast
   743 
   744 lemma antisym_converse [simp]: "antisym (converse r) = antisym r"
   745   by (unfold antisym_def) blast
   746 
   747 lemma trans_converse [simp]: "trans (converse r) = trans r"
   748   by (unfold trans_def) blast
   749 
   750 lemma sym_conv_converse_eq: "sym r = (r^-1 = r)"
   751   by (unfold sym_def) fast
   752 
   753 lemma sym_Un_converse: "sym (r \<union> r^-1)"
   754   by (unfold sym_def) blast
   755 
   756 lemma sym_Int_converse: "sym (r \<inter> r^-1)"
   757   by (unfold sym_def) blast
   758 
   759 lemma total_on_converse [simp]: "total_on A (r^-1) = total_on A r"
   760   by (auto simp: total_on_def)
   761 
   762 lemma finite_converse [iff]: "finite (r^-1) = finite r"  
   763   unfolding converse_def conversep_iff using [[simproc add: finite_Collect]]
   764   by (auto elim: finite_imageD simp: inj_on_def)
   765 
   766 lemma conversep_noteq [simp]: "(op \<noteq>)^--1 = op \<noteq>"
   767   by (auto simp add: fun_eq_iff)
   768 
   769 lemma conversep_eq [simp]: "(op =)^--1 = op ="
   770   by (auto simp add: fun_eq_iff)
   771 
   772 lemma converse_unfold [code]:
   773   "r\<inverse> = {(y, x). (x, y) \<in> r}"
   774   by (simp add: set_eq_iff)
   775 
   776 
   777 subsubsection {* Domain, range and field *}
   778 
   779 inductive_set Domain :: "('a \<times> 'b) set \<Rightarrow> 'a set"
   780   for r :: "('a \<times> 'b) set"
   781 where
   782   DomainI [intro]: "(a, b) \<in> r \<Longrightarrow> a \<in> Domain r"
   783 
   784 abbreviation (input) "DomainP \<equiv> Domainp"
   785 
   786 lemmas DomainPI = Domainp.DomainI
   787 
   788 inductive_cases DomainE [elim!]: "a \<in> Domain r"
   789 inductive_cases DomainpE [elim!]: "Domainp r a"
   790 
   791 inductive_set Range :: "('a \<times> 'b) set \<Rightarrow> 'b set"
   792   for r :: "('a \<times> 'b) set"
   793 where
   794   RangeI [intro]: "(a, b) \<in> r \<Longrightarrow> b \<in> Range r"
   795 
   796 abbreviation (input) "RangeP \<equiv> Rangep"
   797 
   798 lemmas RangePI = Rangep.RangeI
   799 
   800 inductive_cases RangeE [elim!]: "b \<in> Range r"
   801 inductive_cases RangepE [elim!]: "Rangep r b"
   802 
   803 definition Field :: "'a rel \<Rightarrow> 'a set"
   804 where
   805   "Field r = Domain r \<union> Range r"
   806 
   807 lemma Domain_fst [code]:
   808   "Domain r = fst ` r"
   809   by force
   810 
   811 lemma Range_snd [code]:
   812   "Range r = snd ` r"
   813   by force
   814 
   815 lemma fst_eq_Domain: "fst ` R = Domain R"
   816   by force
   817 
   818 lemma snd_eq_Range: "snd ` R = Range R"
   819   by force
   820 
   821 lemma Domain_empty [simp]: "Domain {} = {}"
   822   by auto
   823 
   824 lemma Range_empty [simp]: "Range {} = {}"
   825   by auto
   826 
   827 lemma Field_empty [simp]: "Field {} = {}"
   828   by (simp add: Field_def)
   829 
   830 lemma Domain_empty_iff: "Domain r = {} \<longleftrightarrow> r = {}"
   831   by auto
   832 
   833 lemma Range_empty_iff: "Range r = {} \<longleftrightarrow> r = {}"
   834   by auto
   835 
   836 lemma Domain_insert [simp]: "Domain (insert (a, b) r) = insert a (Domain r)"
   837   by blast
   838 
   839 lemma Range_insert [simp]: "Range (insert (a, b) r) = insert b (Range r)"
   840   by blast
   841 
   842 lemma Field_insert [simp]: "Field (insert (a, b) r) = {a, b} \<union> Field r"
   843   by (auto simp add: Field_def)
   844 
   845 lemma Domain_iff: "a \<in> Domain r \<longleftrightarrow> (\<exists>y. (a, y) \<in> r)"
   846   by blast
   847 
   848 lemma Range_iff: "a \<in> Range r \<longleftrightarrow> (\<exists>y. (y, a) \<in> r)"
   849   by blast
   850 
   851 lemma Domain_Id [simp]: "Domain Id = UNIV"
   852   by blast
   853 
   854 lemma Range_Id [simp]: "Range Id = UNIV"
   855   by blast
   856 
   857 lemma Domain_Id_on [simp]: "Domain (Id_on A) = A"
   858   by blast
   859 
   860 lemma Range_Id_on [simp]: "Range (Id_on A) = A"
   861   by blast
   862 
   863 lemma Domain_Un_eq: "Domain (A \<union> B) = Domain A \<union> Domain B"
   864   by blast
   865 
   866 lemma Range_Un_eq: "Range (A \<union> B) = Range A \<union> Range B"
   867   by blast
   868 
   869 lemma Field_Un [simp]: "Field (r \<union> s) = Field r \<union> Field s"
   870   by (auto simp: Field_def)
   871 
   872 lemma Domain_Int_subset: "Domain (A \<inter> B) \<subseteq> Domain A \<inter> Domain B"
   873   by blast
   874 
   875 lemma Range_Int_subset: "Range (A \<inter> B) \<subseteq> Range A \<inter> Range B"
   876   by blast
   877 
   878 lemma Domain_Diff_subset: "Domain A - Domain B \<subseteq> Domain (A - B)"
   879   by blast
   880 
   881 lemma Range_Diff_subset: "Range A - Range B \<subseteq> Range (A - B)"
   882   by blast
   883 
   884 lemma Domain_Union: "Domain (\<Union>S) = (\<Union>A\<in>S. Domain A)"
   885   by blast
   886 
   887 lemma Range_Union: "Range (\<Union>S) = (\<Union>A\<in>S. Range A)"
   888   by blast
   889 
   890 lemma Field_Union [simp]: "Field (\<Union>R) = \<Union>(Field ` R)"
   891   by (auto simp: Field_def)
   892 
   893 lemma Domain_converse [simp]: "Domain (r\<inverse>) = Range r"
   894   by auto
   895 
   896 lemma Range_converse [simp]: "Range (r\<inverse>) = Domain r"
   897   by blast
   898 
   899 lemma Field_converse [simp]: "Field (r\<inverse>) = Field r"
   900   by (auto simp: Field_def)
   901 
   902 lemma Domain_Collect_split [simp]: "Domain {(x, y). P x y} = {x. EX y. P x y}"
   903   by auto
   904 
   905 lemma Range_Collect_split [simp]: "Range {(x, y). P x y} = {y. EX x. P x y}"
   906   by auto
   907 
   908 lemma finite_Domain: "finite r \<Longrightarrow> finite (Domain r)"
   909   by (induct set: finite) auto
   910 
   911 lemma finite_Range: "finite r \<Longrightarrow> finite (Range r)"
   912   by (induct set: finite) auto
   913 
   914 lemma finite_Field: "finite r \<Longrightarrow> finite (Field r)"
   915   by (simp add: Field_def finite_Domain finite_Range)
   916 
   917 lemma Domain_mono: "r \<subseteq> s \<Longrightarrow> Domain r \<subseteq> Domain s"
   918   by blast
   919 
   920 lemma Range_mono: "r \<subseteq> s \<Longrightarrow> Range r \<subseteq> Range s"
   921   by blast
   922 
   923 lemma mono_Field: "r \<subseteq> s \<Longrightarrow> Field r \<subseteq> Field s"
   924   by (auto simp: Field_def Domain_def Range_def)
   925 
   926 lemma Domain_unfold:
   927   "Domain r = {x. \<exists>y. (x, y) \<in> r}"
   928   by blast
   929 
   930 lemma Domain_dprod [simp]: "Domain (dprod r s) = uprod (Domain r) (Domain s)"
   931   by auto
   932 
   933 lemma Domain_dsum [simp]: "Domain (dsum r s) = usum (Domain r) (Domain s)"
   934   by auto
   935 
   936 
   937 subsubsection {* Image of a set under a relation *}
   938 
   939 definition Image :: "('a \<times> 'b) set \<Rightarrow> 'a set \<Rightarrow> 'b set" (infixr "``" 90)
   940 where
   941   "r `` s = {y. \<exists>x\<in>s. (x, y) \<in> r}"
   942 
   943 lemma Image_iff: "(b : r``A) = (EX x:A. (x, b) : r)"
   944   by (simp add: Image_def)
   945 
   946 lemma Image_singleton: "r``{a} = {b. (a, b) : r}"
   947   by (simp add: Image_def)
   948 
   949 lemma Image_singleton_iff [iff]: "(b : r``{a}) = ((a, b) : r)"
   950   by (rule Image_iff [THEN trans]) simp
   951 
   952 lemma ImageI [intro]: "(a, b) : r ==> a : A ==> b : r``A"
   953   by (unfold Image_def) blast
   954 
   955 lemma ImageE [elim!]:
   956   "b : r `` A ==> (!!x. (x, b) : r ==> x : A ==> P) ==> P"
   957   by (unfold Image_def) (iprover elim!: CollectE bexE)
   958 
   959 lemma rev_ImageI: "a : A ==> (a, b) : r ==> b : r `` A"
   960   -- {* This version's more effective when we already have the required @{text a} *}
   961   by blast
   962 
   963 lemma Image_empty [simp]: "R``{} = {}"
   964   by blast
   965 
   966 lemma Image_Id [simp]: "Id `` A = A"
   967   by blast
   968 
   969 lemma Image_Id_on [simp]: "Id_on A `` B = A \<inter> B"
   970   by blast
   971 
   972 lemma Image_Int_subset: "R `` (A \<inter> B) \<subseteq> R `` A \<inter> R `` B"
   973   by blast
   974 
   975 lemma Image_Int_eq:
   976   "single_valued (converse R) ==> R `` (A \<inter> B) = R `` A \<inter> R `` B"
   977   by (simp add: single_valued_def, blast) 
   978 
   979 lemma Image_Un: "R `` (A \<union> B) = R `` A \<union> R `` B"
   980   by blast
   981 
   982 lemma Un_Image: "(R \<union> S) `` A = R `` A \<union> S `` A"
   983   by blast
   984 
   985 lemma Image_subset: "r \<subseteq> A \<times> B ==> r``C \<subseteq> B"
   986   by (iprover intro!: subsetI elim!: ImageE dest!: subsetD SigmaD2)
   987 
   988 lemma Image_eq_UN: "r``B = (\<Union>y\<in> B. r``{y})"
   989   -- {* NOT suitable for rewriting *}
   990   by blast
   991 
   992 lemma Image_mono: "r' \<subseteq> r ==> A' \<subseteq> A ==> (r' `` A') \<subseteq> (r `` A)"
   993   by blast
   994 
   995 lemma Image_UN: "(r `` (UNION A B)) = (\<Union>x\<in>A. r `` (B x))"
   996   by blast
   997 
   998 lemma UN_Image: "(\<Union>i\<in>I. X i) `` S = (\<Union>i\<in>I. X i `` S)"
   999   by auto
  1000 
  1001 lemma Image_INT_subset: "(r `` INTER A B) \<subseteq> (\<Inter>x\<in>A. r `` (B x))"
  1002   by blast
  1003 
  1004 text{*Converse inclusion requires some assumptions*}
  1005 lemma Image_INT_eq:
  1006      "[|single_valued (r\<inverse>); A\<noteq>{}|] ==> r `` INTER A B = (\<Inter>x\<in>A. r `` B x)"
  1007 apply (rule equalityI)
  1008  apply (rule Image_INT_subset) 
  1009 apply  (simp add: single_valued_def, blast)
  1010 done
  1011 
  1012 lemma Image_subset_eq: "(r``A \<subseteq> B) = (A \<subseteq> - ((r^-1) `` (-B)))"
  1013   by blast
  1014 
  1015 lemma Image_Collect_split [simp]: "{(x, y). P x y} `` A = {y. EX x:A. P x y}"
  1016   by auto
  1017 
  1018 lemma Sigma_Image: "(SIGMA x:A. B x) `` X = (\<Union>x\<in>X \<inter> A. B x)"
  1019   by auto
  1020 
  1021 lemma relcomp_Image: "(X O Y) `` Z = Y `` (X `` Z)"
  1022   by auto
  1023 
  1024 subsubsection {* Inverse image *}
  1025 
  1026 definition inv_image :: "'b rel \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a rel"
  1027 where
  1028   "inv_image r f = {(x, y). (f x, f y) \<in> r}"
  1029 
  1030 definition inv_imagep :: "('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
  1031 where
  1032   "inv_imagep r f = (\<lambda>x y. r (f x) (f y))"
  1033 
  1034 lemma [pred_set_conv]: "inv_imagep (\<lambda>x y. (x, y) \<in> r) f = (\<lambda>x y. (x, y) \<in> inv_image r f)"
  1035   by (simp add: inv_image_def inv_imagep_def)
  1036 
  1037 lemma sym_inv_image: "sym r ==> sym (inv_image r f)"
  1038   by (unfold sym_def inv_image_def) blast
  1039 
  1040 lemma trans_inv_image: "trans r ==> trans (inv_image r f)"
  1041   apply (unfold trans_def inv_image_def)
  1042   apply (simp (no_asm))
  1043   apply blast
  1044   done
  1045 
  1046 lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
  1047   by (auto simp:inv_image_def)
  1048 
  1049 lemma converse_inv_image[simp]: "(inv_image R f)^-1 = inv_image (R^-1) f"
  1050   unfolding inv_image_def converse_unfold by auto
  1051 
  1052 lemma in_inv_imagep [simp]: "inv_imagep r f x y = r (f x) (f y)"
  1053   by (simp add: inv_imagep_def)
  1054 
  1055 
  1056 subsubsection {* Powerset *}
  1057 
  1058 definition Powp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
  1059 where
  1060   "Powp A = (\<lambda>B. \<forall>x \<in> B. A x)"
  1061 
  1062 lemma Powp_Pow_eq [pred_set_conv]: "Powp (\<lambda>x. x \<in> A) = (\<lambda>x. x \<in> Pow A)"
  1063   by (auto simp add: Powp_def fun_eq_iff)
  1064 
  1065 lemmas Powp_mono [mono] = Pow_mono [to_pred]
  1066 
  1067 subsubsection {* Expressing relation operations via @{const Finite_Set.fold} *}
  1068 
  1069 lemma Id_on_fold:
  1070   assumes "finite A"
  1071   shows "Id_on A = Finite_Set.fold (\<lambda>x. Set.insert (Pair x x)) {} A"
  1072 proof -
  1073   interpret comp_fun_commute "\<lambda>x. Set.insert (Pair x x)" by default auto
  1074   show ?thesis using assms unfolding Id_on_def by (induct A) simp_all
  1075 qed
  1076 
  1077 lemma comp_fun_commute_Image_fold:
  1078   "comp_fun_commute (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A)"
  1079 proof -
  1080   interpret comp_fun_idem Set.insert
  1081       by (fact comp_fun_idem_insert)
  1082   show ?thesis 
  1083   by default (auto simp add: fun_eq_iff comp_fun_commute split:prod.split)
  1084 qed
  1085 
  1086 lemma Image_fold:
  1087   assumes "finite R"
  1088   shows "R `` S = Finite_Set.fold (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A) {} R"
  1089 proof -
  1090   interpret comp_fun_commute "(\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A)" 
  1091     by (rule comp_fun_commute_Image_fold)
  1092   have *: "\<And>x F. Set.insert x F `` S = (if fst x \<in> S then Set.insert (snd x) (F `` S) else (F `` S))"
  1093     by (force intro: rev_ImageI)
  1094   show ?thesis using assms by (induct R) (auto simp: *)
  1095 qed
  1096 
  1097 lemma insert_relcomp_union_fold:
  1098   assumes "finite S"
  1099   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"
  1100 proof -
  1101   interpret comp_fun_commute "\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A'"
  1102   proof - 
  1103     interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
  1104     show "comp_fun_commute (\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A')"
  1105     by default (auto simp add: fun_eq_iff split:prod.split)
  1106   qed
  1107   have *: "{x} O S = {(x', z). x' = fst x \<and> (snd x,z) \<in> S}" by (auto simp: relcomp_unfold intro!: exI)
  1108   show ?thesis unfolding *
  1109   using `finite S` by (induct S) (auto split: prod.split)
  1110 qed
  1111 
  1112 lemma insert_relcomp_fold:
  1113   assumes "finite S"
  1114   shows "Set.insert x R O S = 
  1115     Finite_Set.fold (\<lambda>(w,z) A'. if snd x = w then Set.insert (fst x,z) A' else A') (R O S) S"
  1116 proof -
  1117   have "Set.insert x R O S = ({x} O S) \<union> (R O S)" by auto
  1118   then show ?thesis by (auto simp: insert_relcomp_union_fold[OF assms])
  1119 qed
  1120 
  1121 lemma comp_fun_commute_relcomp_fold:
  1122   assumes "finite S"
  1123   shows "comp_fun_commute (\<lambda>(x,y) A. 
  1124     Finite_Set.fold (\<lambda>(w,z) A'. if y = w then Set.insert (x,z) A' else A') A S)"
  1125 proof -
  1126   have *: "\<And>a b A. 
  1127     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"
  1128     by (auto simp: insert_relcomp_union_fold[OF assms] cong: if_cong)
  1129   show ?thesis by default (auto simp: *)
  1130 qed
  1131 
  1132 lemma relcomp_fold:
  1133   assumes "finite R"
  1134   assumes "finite S"
  1135   shows "R O S = Finite_Set.fold 
  1136     (\<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"
  1137   using assms by (induct R)
  1138     (auto simp: comp_fun_commute.fold_insert comp_fun_commute_relcomp_fold insert_relcomp_fold
  1139       cong: if_cong)
  1140 
  1141 
  1142 
  1143 end
  1144