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