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