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