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