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