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