src/HOL/Relation.thy
author haftmann
Sun Feb 26 21:26:28 2012 +0100 (2012-02-26)
changeset 46694 0988b22e2626
parent 46692 1f8b766224f6
child 46696 28a01ea3523a
permissions -rw-r--r--
tuned structure
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
wenzelm@21404
   116
definition
haftmann@46694
   117
  refl_on :: "'a set \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> bool" where
haftmann@45137
   118
  "refl_on A r \<longleftrightarrow> r \<subseteq> A \<times> A & (ALL x: A. (x,x) : r)"
paulson@6806
   119
nipkow@26297
   120
abbreviation
nipkow@30198
   121
  refl :: "('a * 'a) set => bool" where -- {* reflexivity over a type *}
haftmann@45137
   122
  "refl \<equiv> refl_on UNIV"
nipkow@26297
   123
haftmann@46694
   124
definition reflp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
haftmann@46694
   125
  "reflp r \<longleftrightarrow> refl {(x, y). r x y}"
haftmann@46694
   126
haftmann@46692
   127
lemma refl_onI: "r \<subseteq> A \<times> A ==> (!!x. x : A ==> (x, x) : r) ==> refl_on A r"
haftmann@46692
   128
by (unfold refl_on_def) (iprover intro!: ballI)
haftmann@46692
   129
haftmann@46692
   130
lemma refl_onD: "refl_on A r ==> a : A ==> (a, a) : r"
haftmann@46692
   131
by (unfold refl_on_def) blast
haftmann@46692
   132
haftmann@46692
   133
lemma refl_onD1: "refl_on A r ==> (x, y) : r ==> x : A"
haftmann@46692
   134
by (unfold refl_on_def) blast
haftmann@46692
   135
haftmann@46692
   136
lemma refl_onD2: "refl_on A r ==> (x, y) : r ==> y : A"
haftmann@46692
   137
by (unfold refl_on_def) blast
haftmann@46692
   138
haftmann@46694
   139
lemma reflpI:
haftmann@46694
   140
  "(\<And>x. r x x) \<Longrightarrow> reflp r"
haftmann@46694
   141
  by (auto intro: refl_onI simp add: reflp_def)
haftmann@46694
   142
haftmann@46694
   143
lemma reflpE:
haftmann@46694
   144
  assumes "reflp r"
haftmann@46694
   145
  obtains "r x x"
haftmann@46694
   146
  using assms by (auto dest: refl_onD simp add: reflp_def)
haftmann@46694
   147
haftmann@46692
   148
lemma refl_on_Int: "refl_on A r ==> refl_on B s ==> refl_on (A \<inter> B) (r \<inter> s)"
haftmann@46692
   149
by (unfold refl_on_def) blast
haftmann@46692
   150
haftmann@46692
   151
lemma refl_on_Un: "refl_on A r ==> refl_on B s ==> refl_on (A \<union> B) (r \<union> s)"
haftmann@46692
   152
by (unfold refl_on_def) blast
haftmann@46692
   153
haftmann@46692
   154
lemma refl_on_INTER:
haftmann@46692
   155
  "ALL x:S. refl_on (A x) (r x) ==> refl_on (INTER S A) (INTER S r)"
haftmann@46692
   156
by (unfold refl_on_def) fast
haftmann@46692
   157
haftmann@46692
   158
lemma refl_on_UNION:
haftmann@46692
   159
  "ALL x:S. refl_on (A x) (r x) \<Longrightarrow> refl_on (UNION S A) (UNION S r)"
haftmann@46692
   160
by (unfold refl_on_def) blast
haftmann@46692
   161
haftmann@46692
   162
lemma refl_on_empty[simp]: "refl_on {} {}"
haftmann@46692
   163
by(simp add:refl_on_def)
haftmann@46692
   164
haftmann@46692
   165
lemma refl_on_def' [nitpick_unfold, code]:
haftmann@46692
   166
  "refl_on A r = ((\<forall>(x, y) \<in> r. x : A \<and> y : A) \<and> (\<forall>x \<in> A. (x, x) : r))"
haftmann@46692
   167
by (auto intro: refl_onI dest: refl_onD refl_onD1 refl_onD2)
haftmann@46692
   168
haftmann@46692
   169
haftmann@46694
   170
subsubsection {* Irreflexivity *}
paulson@6806
   171
wenzelm@21404
   172
definition
haftmann@46694
   173
  irrefl :: "('a * 'a) set => bool" where
haftmann@46694
   174
  "irrefl r \<longleftrightarrow> (\<forall>x. (x,x) \<notin> r)"
haftmann@46692
   175
haftmann@46694
   176
lemma irrefl_distinct [code]:
haftmann@46694
   177
  "irrefl r \<longleftrightarrow> (\<forall>(x, y) \<in> r. x \<noteq> y)"
haftmann@46694
   178
  by (auto simp add: irrefl_def)
haftmann@46692
   179
haftmann@46692
   180
haftmann@46692
   181
subsubsection {* Symmetry *}
haftmann@46692
   182
haftmann@46692
   183
definition
haftmann@46694
   184
  sym :: "('a * 'a) set => bool" where
haftmann@46692
   185
  "sym r \<longleftrightarrow> (ALL x y. (x,y): r --> (y,x): r)"
haftmann@46692
   186
haftmann@46692
   187
lemma symI: "(!!a b. (a, b) : r ==> (b, a) : r) ==> sym r"
haftmann@46692
   188
by (unfold sym_def) iprover
haftmann@46692
   189
haftmann@46692
   190
lemma symD: "sym r ==> (a, b) : r ==> (b, a) : r"
haftmann@46692
   191
by (unfold sym_def, blast)
haftmann@46692
   192
haftmann@46694
   193
definition symp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
haftmann@46694
   194
  "symp r \<longleftrightarrow> sym {(x, y). r x y}"
haftmann@46694
   195
haftmann@46694
   196
lemma sympI:
haftmann@46694
   197
  "(\<And>x y. r x y \<Longrightarrow> r y x) \<Longrightarrow> symp r"
haftmann@46694
   198
  by (auto intro: symI simp add: symp_def)
haftmann@46694
   199
haftmann@46694
   200
lemma sympE:
haftmann@46694
   201
  assumes "symp r" and "r x y"
haftmann@46694
   202
  obtains "r y x"
haftmann@46694
   203
  using assms by (auto dest: symD simp add: symp_def)
haftmann@46694
   204
haftmann@46692
   205
lemma sym_Int: "sym r ==> sym s ==> sym (r \<inter> s)"
haftmann@46692
   206
by (fast intro: symI dest: symD)
haftmann@46692
   207
haftmann@46692
   208
lemma sym_Un: "sym r ==> sym s ==> sym (r \<union> s)"
haftmann@46692
   209
by (fast intro: symI dest: symD)
haftmann@46692
   210
haftmann@46692
   211
lemma sym_INTER: "ALL x:S. sym (r x) ==> sym (INTER S r)"
haftmann@46692
   212
by (fast intro: symI dest: symD)
haftmann@46692
   213
haftmann@46692
   214
lemma sym_UNION: "ALL x:S. sym (r x) ==> sym (UNION S r)"
haftmann@46692
   215
by (fast intro: symI dest: symD)
haftmann@46692
   216
haftmann@46692
   217
haftmann@46694
   218
subsubsection {* Antisymmetry *}
haftmann@46694
   219
haftmann@46694
   220
definition
haftmann@46694
   221
  antisym :: "('a * 'a) set => bool" where
haftmann@46694
   222
  "antisym r \<longleftrightarrow> (ALL x y. (x,y):r --> (y,x):r --> x=y)"
haftmann@46694
   223
haftmann@46694
   224
lemma antisymI:
haftmann@46694
   225
  "(!!x y. (x, y) : r ==> (y, x) : r ==> x=y) ==> antisym r"
haftmann@46694
   226
by (unfold antisym_def) iprover
haftmann@46694
   227
haftmann@46694
   228
lemma antisymD: "antisym r ==> (a, b) : r ==> (b, a) : r ==> a = b"
haftmann@46694
   229
by (unfold antisym_def) iprover
haftmann@46694
   230
haftmann@46694
   231
abbreviation antisymP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
haftmann@46694
   232
  "antisymP r \<equiv> antisym {(x, y). r x y}"
haftmann@46694
   233
haftmann@46694
   234
lemma antisym_subset: "r \<subseteq> s ==> antisym s ==> antisym r"
haftmann@46694
   235
by (unfold antisym_def) blast
haftmann@46694
   236
haftmann@46694
   237
lemma antisym_empty [simp]: "antisym {}"
haftmann@46694
   238
by (unfold antisym_def) blast
haftmann@46694
   239
haftmann@46694
   240
haftmann@46692
   241
subsubsection {* Transitivity *}
haftmann@46692
   242
wenzelm@21404
   243
definition
haftmann@46694
   244
  trans :: "('a * 'a) set => bool" where
haftmann@45137
   245
  "trans r \<longleftrightarrow> (ALL x y z. (x,y):r --> (y,z):r --> (x,z):r)"
paulson@5978
   246
haftmann@46692
   247
lemma transI:
haftmann@46692
   248
  "(!!x y z. (x, y) : r ==> (y, z) : r ==> (x, z) : r) ==> trans r"
haftmann@46692
   249
by (unfold trans_def) iprover
haftmann@46692
   250
haftmann@46692
   251
lemma transD: "trans r ==> (a, b) : r ==> (b, c) : r ==> (a, c) : r"
haftmann@46692
   252
by (unfold trans_def) iprover
haftmann@46692
   253
haftmann@46694
   254
abbreviation transP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
haftmann@46694
   255
  "transP r \<equiv> trans {(x, y). r x y}"
haftmann@46694
   256
haftmann@46694
   257
definition transp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
haftmann@46694
   258
  "transp r \<longleftrightarrow> trans {(x, y). r x y}"
haftmann@46694
   259
haftmann@46694
   260
lemma transpI:
haftmann@46694
   261
  "(\<And>x y z. r x y \<Longrightarrow> r y z \<Longrightarrow> r x z) \<Longrightarrow> transp r"
haftmann@46694
   262
  by (auto intro: transI simp add: transp_def)
haftmann@46694
   263
  
haftmann@46694
   264
lemma transpE:
haftmann@46694
   265
  assumes "transp r" and "r x y" and "r y z"
haftmann@46694
   266
  obtains "r x z"
haftmann@46694
   267
  using assms by (auto dest: transD simp add: transp_def)
haftmann@46694
   268
haftmann@46692
   269
lemma trans_Int: "trans r ==> trans s ==> trans (r \<inter> s)"
haftmann@46692
   270
by (fast intro: transI elim: transD)
haftmann@46692
   271
haftmann@46692
   272
lemma trans_INTER: "ALL x:S. trans (r x) ==> trans (INTER S r)"
haftmann@46692
   273
by (fast intro: transI elim: transD)
haftmann@46692
   274
haftmann@46694
   275
lemma trans_join [code]:
haftmann@46694
   276
  "trans r \<longleftrightarrow> (\<forall>(x, y1) \<in> r. \<forall>(y2, z) \<in> r. y1 = y2 \<longrightarrow> (x, z) \<in> r)"
haftmann@46694
   277
  by (auto simp add: trans_def)
haftmann@46692
   278
haftmann@46692
   279
haftmann@46692
   280
subsubsection {* Totality *}
haftmann@46692
   281
nipkow@29859
   282
definition
haftmann@45137
   283
  total_on :: "'a set => ('a * 'a) set => bool" where
haftmann@45137
   284
  "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
   285
nipkow@29859
   286
abbreviation "total \<equiv> total_on UNIV"
nipkow@29859
   287
haftmann@46692
   288
lemma total_on_empty[simp]: "total_on {} r"
haftmann@46692
   289
by(simp add:total_on_def)
haftmann@46692
   290
haftmann@46692
   291
haftmann@46692
   292
subsubsection {* Single valued relations *}
haftmann@46692
   293
nipkow@29859
   294
definition
wenzelm@21404
   295
  single_valued :: "('a * 'b) set => bool" where
haftmann@45137
   296
  "single_valued r \<longleftrightarrow> (ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z))"
berghofe@7014
   297
haftmann@46692
   298
lemma single_valuedI:
haftmann@46692
   299
  "ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z) ==> single_valued r"
haftmann@46692
   300
by (unfold single_valued_def)
haftmann@46692
   301
haftmann@46692
   302
lemma single_valuedD:
haftmann@46692
   303
  "single_valued r ==> (x, y) : r ==> (x, z) : r ==> y = z"
haftmann@46692
   304
by (simp add: single_valued_def)
haftmann@46692
   305
haftmann@46694
   306
abbreviation single_valuedP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
haftmann@46694
   307
  "single_valuedP r \<equiv> single_valued {(x, y). r x y}"
haftmann@46694
   308
haftmann@46692
   309
lemma single_valued_subset:
haftmann@46692
   310
  "r \<subseteq> s ==> single_valued s ==> single_valued r"
haftmann@46692
   311
by (unfold single_valued_def) blast
oheimb@11136
   312
berghofe@12905
   313
haftmann@46694
   314
subsection {* Relation operations *}
haftmann@46694
   315
haftmann@46664
   316
subsubsection {* The identity relation *}
berghofe@12905
   317
haftmann@46692
   318
definition
haftmann@46694
   319
  Id :: "('a * 'a) set" where
haftmann@46692
   320
  "Id = {p. EX x. p = (x,x)}"
haftmann@46692
   321
berghofe@12905
   322
lemma IdI [intro]: "(a, a) : Id"
nipkow@26271
   323
by (simp add: Id_def)
berghofe@12905
   324
berghofe@12905
   325
lemma IdE [elim!]: "p : Id ==> (!!x. p = (x, x) ==> P) ==> P"
nipkow@26271
   326
by (unfold Id_def) (iprover elim: CollectE)
berghofe@12905
   327
berghofe@12905
   328
lemma pair_in_Id_conv [iff]: "((a, b) : Id) = (a = b)"
nipkow@26271
   329
by (unfold Id_def) blast
berghofe@12905
   330
nipkow@30198
   331
lemma refl_Id: "refl Id"
nipkow@30198
   332
by (simp add: refl_on_def)
berghofe@12905
   333
berghofe@12905
   334
lemma antisym_Id: "antisym Id"
berghofe@12905
   335
  -- {* A strange result, since @{text Id} is also symmetric. *}
nipkow@26271
   336
by (simp add: antisym_def)
berghofe@12905
   337
huffman@19228
   338
lemma sym_Id: "sym Id"
nipkow@26271
   339
by (simp add: sym_def)
huffman@19228
   340
berghofe@12905
   341
lemma trans_Id: "trans Id"
nipkow@26271
   342
by (simp add: trans_def)
berghofe@12905
   343
haftmann@46692
   344
lemma single_valued_Id [simp]: "single_valued Id"
haftmann@46692
   345
  by (unfold single_valued_def) blast
haftmann@46692
   346
haftmann@46692
   347
lemma irrefl_diff_Id [simp]: "irrefl (r - Id)"
haftmann@46692
   348
  by (simp add:irrefl_def)
haftmann@46692
   349
haftmann@46692
   350
lemma trans_diff_Id: "trans r \<Longrightarrow> antisym r \<Longrightarrow> trans (r - Id)"
haftmann@46692
   351
  unfolding antisym_def trans_def by blast
haftmann@46692
   352
haftmann@46692
   353
lemma total_on_diff_Id [simp]: "total_on A (r - Id) = total_on A r"
haftmann@46692
   354
  by (simp add: total_on_def)
haftmann@46692
   355
berghofe@12905
   356
haftmann@46664
   357
subsubsection {* Diagonal: identity over a set *}
berghofe@12905
   358
haftmann@46692
   359
definition
haftmann@46694
   360
  Id_on  :: "'a set => ('a * 'a) set" where
haftmann@46692
   361
  "Id_on A = (\<Union>x\<in>A. {(x,x)})"
haftmann@46692
   362
nipkow@30198
   363
lemma Id_on_empty [simp]: "Id_on {} = {}"
nipkow@30198
   364
by (simp add: Id_on_def) 
paulson@13812
   365
nipkow@30198
   366
lemma Id_on_eqI: "a = b ==> a : A ==> (a, b) : Id_on A"
nipkow@30198
   367
by (simp add: Id_on_def)
berghofe@12905
   368
blanchet@35828
   369
lemma Id_onI [intro!,no_atp]: "a : A ==> (a, a) : Id_on A"
nipkow@30198
   370
by (rule Id_on_eqI) (rule refl)
berghofe@12905
   371
nipkow@30198
   372
lemma Id_onE [elim!]:
nipkow@30198
   373
  "c : Id_on A ==> (!!x. x : A ==> c = (x, x) ==> P) ==> P"
wenzelm@12913
   374
  -- {* The general elimination rule. *}
nipkow@30198
   375
by (unfold Id_on_def) (iprover elim!: UN_E singletonE)
berghofe@12905
   376
nipkow@30198
   377
lemma Id_on_iff: "((x, y) : Id_on A) = (x = y & x : A)"
nipkow@26271
   378
by blast
berghofe@12905
   379
haftmann@45967
   380
lemma Id_on_def' [nitpick_unfold]:
haftmann@44278
   381
  "Id_on {x. A x} = Collect (\<lambda>(x, y). x = y \<and> A x)"
haftmann@44278
   382
by auto
bulwahn@40923
   383
nipkow@30198
   384
lemma Id_on_subset_Times: "Id_on A \<subseteq> A \<times> A"
nipkow@26271
   385
by blast
berghofe@12905
   386
haftmann@46692
   387
lemma refl_on_Id_on: "refl_on A (Id_on A)"
haftmann@46692
   388
by (rule refl_onI [OF Id_on_subset_Times Id_onI])
haftmann@46692
   389
haftmann@46692
   390
lemma antisym_Id_on [simp]: "antisym (Id_on A)"
haftmann@46692
   391
by (unfold antisym_def) blast
haftmann@46692
   392
haftmann@46692
   393
lemma sym_Id_on [simp]: "sym (Id_on A)"
haftmann@46692
   394
by (rule symI) clarify
haftmann@46692
   395
haftmann@46692
   396
lemma trans_Id_on [simp]: "trans (Id_on A)"
haftmann@46692
   397
by (fast intro: transI elim: transD)
haftmann@46692
   398
haftmann@46692
   399
lemma single_valued_Id_on [simp]: "single_valued (Id_on A)"
haftmann@46692
   400
  by (unfold single_valued_def) blast
haftmann@46692
   401
berghofe@12905
   402
haftmann@46694
   403
subsubsection {* Composition *}
berghofe@12905
   404
haftmann@46694
   405
definition rel_comp  :: "('a \<times> 'b) set \<Rightarrow> ('b \<times> 'c) set \<Rightarrow> ('a * 'c) set" (infixr "O" 75)
haftmann@46694
   406
where
haftmann@46694
   407
  "r O s = {(x, z). \<exists>y. (x, y) \<in> r \<and> (y, z) \<in> s}"
haftmann@46692
   408
wenzelm@12913
   409
lemma rel_compI [intro]:
krauss@32235
   410
  "(a, b) : r ==> (b, c) : s ==> (a, c) : r O s"
nipkow@26271
   411
by (unfold rel_comp_def) blast
berghofe@12905
   412
wenzelm@12913
   413
lemma rel_compE [elim!]: "xz : r O s ==>
krauss@32235
   414
  (!!x y z. xz = (x, z) ==> (x, y) : r ==> (y, z) : s  ==> P) ==> P"
nipkow@26271
   415
by (unfold rel_comp_def) (iprover elim!: CollectE splitE exE conjE)
berghofe@12905
   416
haftmann@46694
   417
inductive pred_comp :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'c \<Rightarrow> bool" (infixr "OO" 75)
haftmann@46694
   418
for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" and s :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
haftmann@46694
   419
where
haftmann@46694
   420
  pred_compI [intro]: "r a b \<Longrightarrow> s b c \<Longrightarrow> (r OO s) a c"
haftmann@46694
   421
haftmann@46694
   422
inductive_cases pred_compE [elim!]: "(r OO s) a c"
haftmann@46694
   423
haftmann@46694
   424
lemma pred_comp_rel_comp_eq [pred_set_conv]:
haftmann@46694
   425
  "((\<lambda>x y. (x, y) \<in> r) OO (\<lambda>x y. (x, y) \<in> s)) = (\<lambda>x y. (x, y) \<in> r O s)"
haftmann@46694
   426
  by (auto simp add: fun_eq_iff)
haftmann@46694
   427
berghofe@12905
   428
lemma rel_compEpair:
krauss@32235
   429
  "(a, c) : r O s ==> (!!y. (a, y) : r ==> (y, c) : s ==> P) ==> P"
nipkow@26271
   430
by (iprover elim: rel_compE Pair_inject ssubst)
berghofe@12905
   431
berghofe@12905
   432
lemma R_O_Id [simp]: "R O Id = R"
nipkow@26271
   433
by fast
berghofe@12905
   434
berghofe@12905
   435
lemma Id_O_R [simp]: "Id O R = R"
nipkow@26271
   436
by fast
berghofe@12905
   437
krauss@23185
   438
lemma rel_comp_empty1[simp]: "{} O R = {}"
nipkow@26271
   439
by blast
krauss@23185
   440
krauss@23185
   441
lemma rel_comp_empty2[simp]: "R O {} = {}"
nipkow@26271
   442
by blast
krauss@23185
   443
berghofe@12905
   444
lemma O_assoc: "(R O S) O T = R O (S O T)"
nipkow@26271
   445
by blast
berghofe@12905
   446
wenzelm@12913
   447
lemma trans_O_subset: "trans r ==> r O r \<subseteq> r"
nipkow@26271
   448
by (unfold trans_def) blast
berghofe@12905
   449
wenzelm@12913
   450
lemma rel_comp_mono: "r' \<subseteq> r ==> s' \<subseteq> s ==> (r' O s') \<subseteq> (r O s)"
nipkow@26271
   451
by blast
berghofe@12905
   452
berghofe@12905
   453
lemma rel_comp_subset_Sigma:
krauss@32235
   454
    "r \<subseteq> A \<times> B ==> s \<subseteq> B \<times> C ==> (r O s) \<subseteq> A \<times> C"
nipkow@26271
   455
by blast
berghofe@12905
   456
krauss@28008
   457
lemma rel_comp_distrib[simp]: "R O (S \<union> T) = (R O S) \<union> (R O T)" 
krauss@28008
   458
by auto
krauss@28008
   459
krauss@28008
   460
lemma rel_comp_distrib2[simp]: "(S \<union> T) O R = (S O R) \<union> (T O R)"
krauss@28008
   461
by auto
krauss@28008
   462
krauss@36772
   463
lemma rel_comp_UNION_distrib: "s O UNION I r = UNION I (%i. s O r i)"
krauss@36772
   464
by auto
krauss@36772
   465
krauss@36772
   466
lemma rel_comp_UNION_distrib2: "UNION I r O s = UNION I (%i. r i O s)"
krauss@36772
   467
by auto
krauss@36772
   468
haftmann@46692
   469
lemma single_valued_rel_comp:
haftmann@46692
   470
  "single_valued r ==> single_valued s ==> single_valued (r O s)"
haftmann@46692
   471
by (unfold single_valued_def) blast
berghofe@12905
   472
haftmann@46664
   473
haftmann@46664
   474
subsubsection {* Converse *}
wenzelm@12913
   475
haftmann@46692
   476
definition
haftmann@46692
   477
  converse :: "('a * 'b) set => ('b * 'a) set"
haftmann@46692
   478
    ("(_^-1)" [1000] 999) where
haftmann@46692
   479
  "r^-1 = {(y, x). (x, y) : r}"
haftmann@46692
   480
haftmann@46692
   481
notation (xsymbols)
haftmann@46692
   482
  converse  ("(_\<inverse>)" [1000] 999)
haftmann@46692
   483
haftmann@46694
   484
lemma converseI [sym]: "(a, b) : r ==> (b, a) : r^-1"
haftmann@46694
   485
  by (simp add: converse_def)
berghofe@12905
   486
haftmann@46694
   487
lemma converseD [sym]: "(a,b) : r^-1 ==> (b, a) : r"
haftmann@46694
   488
  by (simp add: converse_def)
berghofe@12905
   489
berghofe@12905
   490
lemma converseE [elim!]:
berghofe@12905
   491
  "yx : r^-1 ==> (!!x y. yx = (y, x) ==> (x, y) : r ==> P) ==> P"
wenzelm@12913
   492
    -- {* More general than @{text converseD}, as it ``splits'' the member of the relation. *}
haftmann@46694
   493
  by (unfold converse_def) (iprover elim!: CollectE splitE bexE)
haftmann@46694
   494
haftmann@46694
   495
lemma converse_iff [iff]: "((a,b): r^-1) = ((b,a) : r)"
haftmann@46694
   496
  by (simp add: converse_def)
haftmann@46694
   497
haftmann@46694
   498
inductive conversep :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> bool" ("(_^--1)" [1000] 1000)
haftmann@46694
   499
  for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where
haftmann@46694
   500
  conversepI: "r a b \<Longrightarrow> r^--1 b a"
haftmann@46694
   501
haftmann@46694
   502
notation (xsymbols)
haftmann@46694
   503
  conversep  ("(_\<inverse>\<inverse>)" [1000] 1000)
haftmann@46694
   504
haftmann@46694
   505
lemma conversepD:
haftmann@46694
   506
  assumes ab: "r^--1 a b"
haftmann@46694
   507
  shows "r b a" using ab
haftmann@46694
   508
  by cases simp
haftmann@46694
   509
haftmann@46694
   510
lemma conversep_iff [iff]: "r^--1 a b = r b a"
haftmann@46694
   511
  by (iprover intro: conversepI dest: conversepD)
haftmann@46694
   512
haftmann@46694
   513
lemma conversep_converse_eq [pred_set_conv]:
haftmann@46694
   514
  "(\<lambda>x y. (x, y) \<in> r)^--1 = (\<lambda>x y. (x, y) \<in> r^-1)"
haftmann@46694
   515
  apply (auto simp add: fun_eq_iff)
haftmann@46694
   516
  oops
haftmann@46694
   517
haftmann@46694
   518
lemma conversep_conversep [simp]: "(r^--1)^--1 = r"
haftmann@46694
   519
  by (iprover intro: order_antisym conversepI dest: conversepD)
haftmann@46694
   520
haftmann@46694
   521
lemma converse_pred_comp: "(r OO s)^--1 = s^--1 OO r^--1"
haftmann@46694
   522
  by (iprover intro: order_antisym conversepI pred_compI
haftmann@46694
   523
    elim: pred_compE dest: conversepD)
haftmann@46694
   524
haftmann@46694
   525
lemma converse_meet: "(r \<sqinter> s)^--1 = r^--1 \<sqinter> s^--1"
haftmann@46694
   526
  by (simp add: inf_fun_def) (iprover intro: conversepI ext dest: conversepD)
haftmann@46694
   527
haftmann@46694
   528
lemma converse_join: "(r \<squnion> s)^--1 = r^--1 \<squnion> s^--1"
haftmann@46694
   529
  by (simp add: sup_fun_def) (iprover intro: conversepI ext dest: conversepD)
haftmann@46694
   530
haftmann@46694
   531
lemma conversep_noteq [simp]: "(op \<noteq>)^--1 = op \<noteq>"
haftmann@46694
   532
  by (auto simp add: fun_eq_iff)
haftmann@46694
   533
haftmann@46694
   534
lemma conversep_eq [simp]: "(op =)^--1 = op ="
haftmann@46694
   535
  by (auto simp add: fun_eq_iff)
berghofe@12905
   536
berghofe@12905
   537
lemma converse_converse [simp]: "(r^-1)^-1 = r"
nipkow@26271
   538
by (unfold converse_def) blast
berghofe@12905
   539
berghofe@12905
   540
lemma converse_rel_comp: "(r O s)^-1 = s^-1 O r^-1"
nipkow@26271
   541
by blast
berghofe@12905
   542
huffman@19228
   543
lemma converse_Int: "(r \<inter> s)^-1 = r^-1 \<inter> s^-1"
nipkow@26271
   544
by blast
huffman@19228
   545
huffman@19228
   546
lemma converse_Un: "(r \<union> s)^-1 = r^-1 \<union> s^-1"
nipkow@26271
   547
by blast
huffman@19228
   548
huffman@19228
   549
lemma converse_INTER: "(INTER S r)^-1 = (INT x:S. (r x)^-1)"
nipkow@26271
   550
by fast
huffman@19228
   551
huffman@19228
   552
lemma converse_UNION: "(UNION S r)^-1 = (UN x:S. (r x)^-1)"
nipkow@26271
   553
by blast
huffman@19228
   554
berghofe@12905
   555
lemma converse_Id [simp]: "Id^-1 = Id"
nipkow@26271
   556
by blast
berghofe@12905
   557
nipkow@30198
   558
lemma converse_Id_on [simp]: "(Id_on A)^-1 = Id_on A"
nipkow@26271
   559
by blast
berghofe@12905
   560
nipkow@30198
   561
lemma refl_on_converse [simp]: "refl_on A (converse r) = refl_on A r"
nipkow@30198
   562
by (unfold refl_on_def) auto
berghofe@12905
   563
huffman@19228
   564
lemma sym_converse [simp]: "sym (converse r) = sym r"
nipkow@26271
   565
by (unfold sym_def) blast
huffman@19228
   566
huffman@19228
   567
lemma antisym_converse [simp]: "antisym (converse r) = antisym r"
nipkow@26271
   568
by (unfold antisym_def) blast
berghofe@12905
   569
huffman@19228
   570
lemma trans_converse [simp]: "trans (converse r) = trans r"
nipkow@26271
   571
by (unfold trans_def) blast
berghofe@12905
   572
huffman@19228
   573
lemma sym_conv_converse_eq: "sym r = (r^-1 = r)"
nipkow@26271
   574
by (unfold sym_def) fast
huffman@19228
   575
huffman@19228
   576
lemma sym_Un_converse: "sym (r \<union> r^-1)"
nipkow@26271
   577
by (unfold sym_def) blast
huffman@19228
   578
huffman@19228
   579
lemma sym_Int_converse: "sym (r \<inter> r^-1)"
nipkow@26271
   580
by (unfold sym_def) blast
huffman@19228
   581
nipkow@29859
   582
lemma total_on_converse[simp]: "total_on A (r^-1) = total_on A r"
nipkow@29859
   583
by (auto simp: total_on_def)
nipkow@29859
   584
haftmann@46692
   585
lemma finite_converse [iff]: "finite (r^-1) = finite r"
haftmann@46692
   586
  apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
haftmann@46692
   587
   apply simp
haftmann@46692
   588
   apply (rule iffI)
haftmann@46692
   589
    apply (erule finite_imageD [unfolded inj_on_def])
haftmann@46692
   590
    apply (simp split add: split_split)
haftmann@46692
   591
   apply (erule finite_imageI)
haftmann@46692
   592
  apply (simp add: converse_def image_def, auto)
haftmann@46692
   593
  apply (rule bexI)
haftmann@46692
   594
   prefer 2 apply assumption
haftmann@46692
   595
  apply simp
haftmann@46692
   596
  done
wenzelm@12913
   597
haftmann@46692
   598
haftmann@46692
   599
subsubsection {* Domain, range and field *}
haftmann@46692
   600
haftmann@46692
   601
definition
haftmann@46692
   602
  Domain :: "('a * 'b) set => 'a set" where
haftmann@46692
   603
  "Domain r = {x. EX y. (x,y):r}"
haftmann@46692
   604
haftmann@46692
   605
definition
haftmann@46692
   606
  Range  :: "('a * 'b) set => 'b set" where
haftmann@46692
   607
  "Range r = Domain(r^-1)"
haftmann@46692
   608
haftmann@46692
   609
definition
haftmann@46692
   610
  Field :: "('a * 'a) set => 'a set" where
haftmann@46692
   611
  "Field r = Domain r \<union> Range r"
berghofe@12905
   612
blanchet@35828
   613
declare Domain_def [no_atp]
paulson@24286
   614
berghofe@12905
   615
lemma Domain_iff: "(a : Domain r) = (EX y. (a, y) : r)"
nipkow@26271
   616
by (unfold Domain_def) blast
berghofe@12905
   617
berghofe@12905
   618
lemma DomainI [intro]: "(a, b) : r ==> a : Domain r"
nipkow@26271
   619
by (iprover intro!: iffD2 [OF Domain_iff])
berghofe@12905
   620
berghofe@12905
   621
lemma DomainE [elim!]:
berghofe@12905
   622
  "a : Domain r ==> (!!y. (a, y) : r ==> P) ==> P"
nipkow@26271
   623
by (iprover dest!: iffD1 [OF Domain_iff])
berghofe@12905
   624
berghofe@12905
   625
lemma Range_iff: "(a : Range r) = (EX y. (y, a) : r)"
nipkow@26271
   626
by (simp add: Domain_def Range_def)
berghofe@12905
   627
berghofe@12905
   628
lemma RangeI [intro]: "(a, b) : r ==> b : Range r"
nipkow@26271
   629
by (unfold Range_def) (iprover intro!: converseI DomainI)
berghofe@12905
   630
berghofe@12905
   631
lemma RangeE [elim!]: "b : Range r ==> (!!x. (x, b) : r ==> P) ==> P"
nipkow@26271
   632
by (unfold Range_def) (iprover elim!: DomainE dest!: converseD)
berghofe@12905
   633
haftmann@46694
   634
inductive DomainP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@46694
   635
  for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where
haftmann@46694
   636
  DomainPI [intro]: "r a b \<Longrightarrow> DomainP r a"
haftmann@46694
   637
haftmann@46694
   638
inductive_cases DomainPE [elim!]: "DomainP r a"
haftmann@46694
   639
haftmann@46694
   640
lemma DomainP_Domain_eq [pred_set_conv]: "DomainP (\<lambda>x y. (x, y) \<in> r) = (\<lambda>x. x \<in> Domain r)"
haftmann@46694
   641
  by (blast intro!: Orderings.order_antisym predicate1I)
haftmann@46694
   642
haftmann@46694
   643
inductive RangeP :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'b \<Rightarrow> bool"
haftmann@46694
   644
  for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where
haftmann@46694
   645
  RangePI [intro]: "r a b \<Longrightarrow> RangeP r b"
haftmann@46694
   646
haftmann@46694
   647
inductive_cases RangePE [elim!]: "RangeP r b"
haftmann@46694
   648
haftmann@46694
   649
lemma RangeP_Range_eq [pred_set_conv]: "RangeP (\<lambda>x y. (x, y) \<in> r) = (\<lambda>x. x \<in> Range r)"
haftmann@46694
   650
  by (auto intro!: Orderings.order_antisym predicate1I)
haftmann@46694
   651
haftmann@46694
   652
lemma Domain_fst [code]:
haftmann@46694
   653
  "Domain r = fst ` r"
haftmann@46694
   654
  by (auto simp add: image_def Bex_def)
haftmann@46694
   655
haftmann@46694
   656
lemma Domain_empty [simp]: "Domain {} = {}"
haftmann@46694
   657
  by blast
haftmann@46694
   658
haftmann@46694
   659
lemma Domain_empty_iff: "Domain r = {} \<longleftrightarrow> r = {}"
haftmann@46694
   660
  by auto
haftmann@46694
   661
haftmann@46694
   662
lemma Domain_insert: "Domain (insert (a, b) r) = insert a (Domain r)"
haftmann@46694
   663
  by blast
haftmann@46694
   664
haftmann@46694
   665
lemma Domain_Id [simp]: "Domain Id = UNIV"
haftmann@46694
   666
  by blast
haftmann@46694
   667
haftmann@46694
   668
lemma Domain_Id_on [simp]: "Domain (Id_on A) = A"
haftmann@46694
   669
  by blast
haftmann@46694
   670
haftmann@46694
   671
lemma Domain_Un_eq: "Domain(A \<union> B) = Domain(A) \<union> Domain(B)"
haftmann@46694
   672
  by blast
haftmann@46694
   673
haftmann@46694
   674
lemma Domain_Int_subset: "Domain(A \<inter> B) \<subseteq> Domain(A) \<inter> Domain(B)"
haftmann@46694
   675
  by blast
haftmann@46694
   676
haftmann@46694
   677
lemma Domain_Diff_subset: "Domain(A) - Domain(B) \<subseteq> Domain(A - B)"
haftmann@46694
   678
  by blast
haftmann@46694
   679
haftmann@46694
   680
lemma Domain_Union: "Domain (Union S) = (\<Union>A\<in>S. Domain A)"
haftmann@46694
   681
  by blast
haftmann@46694
   682
haftmann@46694
   683
lemma Domain_converse[simp]: "Domain(r^-1) = Range r"
haftmann@46694
   684
  by(auto simp: Range_def)
haftmann@46694
   685
haftmann@46694
   686
lemma Domain_mono: "r \<subseteq> s ==> Domain r \<subseteq> Domain s"
haftmann@46694
   687
  by blast
haftmann@46694
   688
haftmann@46694
   689
lemma fst_eq_Domain: "fst ` R = Domain R"
haftmann@46694
   690
  by force
haftmann@46694
   691
haftmann@46694
   692
lemma Domain_dprod [simp]: "Domain (dprod r s) = uprod (Domain r) (Domain s)"
haftmann@46694
   693
  by auto
haftmann@46694
   694
haftmann@46694
   695
lemma Domain_dsum [simp]: "Domain (dsum r s) = usum (Domain r) (Domain s)"
haftmann@46694
   696
  by auto
haftmann@46694
   697
haftmann@46694
   698
lemma Domain_Collect_split [simp]: "Domain {(x, y). P x y} = {x. EX y. P x y}"
haftmann@46694
   699
  by auto
haftmann@46694
   700
haftmann@46694
   701
lemma finite_Domain: "finite r ==> finite (Domain r)"
haftmann@46694
   702
  by (induct set: finite) (auto simp add: Domain_insert)
haftmann@46694
   703
haftmann@46127
   704
lemma Range_snd [code]:
haftmann@45012
   705
  "Range r = snd ` r"
haftmann@45012
   706
  by (auto simp add: image_def Bex_def)
haftmann@45012
   707
berghofe@12905
   708
lemma Range_empty [simp]: "Range {} = {}"
haftmann@46694
   709
  by blast
berghofe@12905
   710
paulson@32876
   711
lemma Range_empty_iff: "Range r = {} \<longleftrightarrow> r = {}"
paulson@32876
   712
  by auto
paulson@32876
   713
berghofe@12905
   714
lemma Range_insert: "Range (insert (a, b) r) = insert b (Range r)"
haftmann@46694
   715
  by blast
berghofe@12905
   716
berghofe@12905
   717
lemma Range_Id [simp]: "Range Id = UNIV"
haftmann@46694
   718
  by blast
berghofe@12905
   719
nipkow@30198
   720
lemma Range_Id_on [simp]: "Range (Id_on A) = A"
haftmann@46694
   721
  by auto
berghofe@12905
   722
paulson@13830
   723
lemma Range_Un_eq: "Range(A \<union> B) = Range(A) \<union> Range(B)"
haftmann@46694
   724
  by blast
berghofe@12905
   725
paulson@13830
   726
lemma Range_Int_subset: "Range(A \<inter> B) \<subseteq> Range(A) \<inter> Range(B)"
haftmann@46694
   727
  by blast
berghofe@12905
   728
wenzelm@12913
   729
lemma Range_Diff_subset: "Range(A) - Range(B) \<subseteq> Range(A - B)"
haftmann@46694
   730
  by blast
berghofe@12905
   731
paulson@13830
   732
lemma Range_Union: "Range (Union S) = (\<Union>A\<in>S. Range A)"
haftmann@46694
   733
  by blast
nipkow@26271
   734
haftmann@46694
   735
lemma Range_converse [simp]: "Range(r^-1) = Domain r"
haftmann@46694
   736
  by blast
berghofe@12905
   737
krauss@36729
   738
lemma snd_eq_Range: "snd ` R = Range R"
huffman@44921
   739
  by force
nipkow@26271
   740
haftmann@46692
   741
lemma Range_Collect_split [simp]: "Range {(x, y). P x y} = {y. EX x. P x y}"
haftmann@46694
   742
  by auto
nipkow@26271
   743
haftmann@46692
   744
lemma finite_Range: "finite r ==> finite (Range r)"
haftmann@46692
   745
  by (induct set: finite) (auto simp add: Range_insert)
nipkow@26271
   746
nipkow@26271
   747
lemma mono_Field: "r \<subseteq> s \<Longrightarrow> Field r \<subseteq> Field s"
haftmann@46694
   748
  by (auto simp: Field_def Domain_def Range_def)
nipkow@26271
   749
nipkow@26271
   750
lemma Field_empty[simp]: "Field {} = {}"
haftmann@46694
   751
  by (auto simp: Field_def)
nipkow@26271
   752
haftmann@46694
   753
lemma Field_insert [simp]: "Field (insert (a, b) r) = {a, b} \<union> Field r"
haftmann@46694
   754
  by (auto simp: Field_def)
nipkow@26271
   755
haftmann@46694
   756
lemma Field_Un [simp]: "Field (r \<union> s) = Field r \<union> Field s"
haftmann@46694
   757
  by (auto simp: Field_def)
nipkow@26271
   758
haftmann@46694
   759
lemma Field_Union [simp]: "Field (\<Union>R) = \<Union>(Field ` R)"
haftmann@46694
   760
  by (auto simp: Field_def)
nipkow@26271
   761
haftmann@46694
   762
lemma Field_converse [simp]: "Field(r^-1) = Field r"
haftmann@46694
   763
  by (auto simp: Field_def)
paulson@22172
   764
haftmann@46692
   765
lemma finite_Field: "finite r ==> finite (Field r)"
haftmann@46692
   766
  -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
haftmann@46692
   767
  apply (induct set: finite)
haftmann@46692
   768
   apply (auto simp add: Field_def Domain_insert Range_insert)
haftmann@46692
   769
  done
haftmann@46692
   770
berghofe@12905
   771
haftmann@46664
   772
subsubsection {* Image of a set under a relation *}
berghofe@12905
   773
haftmann@46692
   774
definition
haftmann@46692
   775
  Image :: "[('a * 'b) set, 'a set] => 'b set"
haftmann@46692
   776
    (infixl "``" 90) where
haftmann@46692
   777
  "r `` s = {y. EX x:s. (x,y):r}"
haftmann@46692
   778
blanchet@35828
   779
declare Image_def [no_atp]
paulson@24286
   780
wenzelm@12913
   781
lemma Image_iff: "(b : r``A) = (EX x:A. (x, b) : r)"
nipkow@26271
   782
by (simp add: Image_def)
berghofe@12905
   783
wenzelm@12913
   784
lemma Image_singleton: "r``{a} = {b. (a, b) : r}"
nipkow@26271
   785
by (simp add: Image_def)
berghofe@12905
   786
wenzelm@12913
   787
lemma Image_singleton_iff [iff]: "(b : r``{a}) = ((a, b) : r)"
nipkow@26271
   788
by (rule Image_iff [THEN trans]) simp
berghofe@12905
   789
blanchet@35828
   790
lemma ImageI [intro,no_atp]: "(a, b) : r ==> a : A ==> b : r``A"
nipkow@26271
   791
by (unfold Image_def) blast
berghofe@12905
   792
berghofe@12905
   793
lemma ImageE [elim!]:
wenzelm@12913
   794
    "b : r `` A ==> (!!x. (x, b) : r ==> x : A ==> P) ==> P"
nipkow@26271
   795
by (unfold Image_def) (iprover elim!: CollectE bexE)
berghofe@12905
   796
berghofe@12905
   797
lemma rev_ImageI: "a : A ==> (a, b) : r ==> b : r `` A"
berghofe@12905
   798
  -- {* This version's more effective when we already have the required @{text a} *}
nipkow@26271
   799
by blast
berghofe@12905
   800
berghofe@12905
   801
lemma Image_empty [simp]: "R``{} = {}"
nipkow@26271
   802
by blast
berghofe@12905
   803
berghofe@12905
   804
lemma Image_Id [simp]: "Id `` A = A"
nipkow@26271
   805
by blast
berghofe@12905
   806
nipkow@30198
   807
lemma Image_Id_on [simp]: "Id_on A `` B = A \<inter> B"
nipkow@26271
   808
by blast
paulson@13830
   809
paulson@13830
   810
lemma Image_Int_subset: "R `` (A \<inter> B) \<subseteq> R `` A \<inter> R `` B"
nipkow@26271
   811
by blast
berghofe@12905
   812
paulson@13830
   813
lemma Image_Int_eq:
paulson@13830
   814
     "single_valued (converse R) ==> R `` (A \<inter> B) = R `` A \<inter> R `` B"
nipkow@26271
   815
by (simp add: single_valued_def, blast) 
berghofe@12905
   816
paulson@13830
   817
lemma Image_Un: "R `` (A \<union> B) = R `` A \<union> R `` B"
nipkow@26271
   818
by blast
berghofe@12905
   819
paulson@13812
   820
lemma Un_Image: "(R \<union> S) `` A = R `` A \<union> S `` A"
nipkow@26271
   821
by blast
paulson@13812
   822
wenzelm@12913
   823
lemma Image_subset: "r \<subseteq> A \<times> B ==> r``C \<subseteq> B"
nipkow@26271
   824
by (iprover intro!: subsetI elim!: ImageE dest!: subsetD SigmaD2)
berghofe@12905
   825
paulson@13830
   826
lemma Image_eq_UN: "r``B = (\<Union>y\<in> B. r``{y})"
berghofe@12905
   827
  -- {* NOT suitable for rewriting *}
nipkow@26271
   828
by blast
berghofe@12905
   829
wenzelm@12913
   830
lemma Image_mono: "r' \<subseteq> r ==> A' \<subseteq> A ==> (r' `` A') \<subseteq> (r `` A)"
nipkow@26271
   831
by blast
berghofe@12905
   832
paulson@13830
   833
lemma Image_UN: "(r `` (UNION A B)) = (\<Union>x\<in>A. r `` (B x))"
nipkow@26271
   834
by blast
paulson@13830
   835
paulson@13830
   836
lemma Image_INT_subset: "(r `` INTER A B) \<subseteq> (\<Inter>x\<in>A. r `` (B x))"
nipkow@26271
   837
by blast
berghofe@12905
   838
paulson@13830
   839
text{*Converse inclusion requires some assumptions*}
paulson@13830
   840
lemma Image_INT_eq:
paulson@13830
   841
     "[|single_valued (r\<inverse>); A\<noteq>{}|] ==> r `` INTER A B = (\<Inter>x\<in>A. r `` B x)"
paulson@13830
   842
apply (rule equalityI)
paulson@13830
   843
 apply (rule Image_INT_subset) 
paulson@13830
   844
apply  (simp add: single_valued_def, blast)
paulson@13830
   845
done
berghofe@12905
   846
wenzelm@12913
   847
lemma Image_subset_eq: "(r``A \<subseteq> B) = (A \<subseteq> - ((r^-1) `` (-B)))"
nipkow@26271
   848
by blast
berghofe@12905
   849
haftmann@46692
   850
lemma Image_Collect_split [simp]: "{(x, y). P x y} `` A = {y. EX x:A. P x y}"
nipkow@26271
   851
by auto
berghofe@12905
   852
berghofe@12905
   853
haftmann@46664
   854
subsubsection {* Inverse image *}
berghofe@12905
   855
haftmann@46692
   856
definition
haftmann@46692
   857
  inv_image :: "('b * 'b) set => ('a => 'b) => ('a * 'a) set" where
haftmann@46692
   858
  "inv_image r f = {(x, y). (f x, f y) : r}"
haftmann@46692
   859
haftmann@46694
   860
definition inv_imagep :: "('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" where
haftmann@46694
   861
  "inv_imagep r f = (\<lambda>x y. r (f x) (f y))"
haftmann@46694
   862
haftmann@46694
   863
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
   864
  by (simp add: inv_image_def inv_imagep_def)
haftmann@46694
   865
huffman@19228
   866
lemma sym_inv_image: "sym r ==> sym (inv_image r f)"
nipkow@26271
   867
by (unfold sym_def inv_image_def) blast
huffman@19228
   868
wenzelm@12913
   869
lemma trans_inv_image: "trans r ==> trans (inv_image r f)"
berghofe@12905
   870
  apply (unfold trans_def inv_image_def)
berghofe@12905
   871
  apply (simp (no_asm))
berghofe@12905
   872
  apply blast
berghofe@12905
   873
  done
berghofe@12905
   874
krauss@32463
   875
lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
krauss@32463
   876
  by (auto simp:inv_image_def)
krauss@32463
   877
krauss@33218
   878
lemma converse_inv_image[simp]: "(inv_image R f)^-1 = inv_image (R^-1) f"
krauss@33218
   879
unfolding inv_image_def converse_def by auto
krauss@33218
   880
haftmann@46664
   881
lemma in_inv_imagep [simp]: "inv_imagep r f x y = r (f x) (f y)"
haftmann@46664
   882
  by (simp add: inv_imagep_def)
haftmann@46664
   883
haftmann@46664
   884
haftmann@46664
   885
subsubsection {* Powerset *}
haftmann@46664
   886
haftmann@46664
   887
definition Powp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where
haftmann@46664
   888
  "Powp A = (\<lambda>B. \<forall>x \<in> B. A x)"
haftmann@46664
   889
haftmann@46664
   890
lemma Powp_Pow_eq [pred_set_conv]: "Powp (\<lambda>x. x \<in> A) = (\<lambda>x. x \<in> Pow A)"
haftmann@46664
   891
  by (auto simp add: Powp_def fun_eq_iff)
haftmann@46664
   892
haftmann@46664
   893
lemmas Powp_mono [mono] = Pow_mono [to_pred]
haftmann@46664
   894
nipkow@1128
   895
end
haftmann@46689
   896