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