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