src/HOL/Relation.thy
author paulson
Sat Feb 08 16:05:33 2003 +0100 (2003-02-08)
changeset 13812 91713a1915ee
parent 13639 8ee6ea6627e1
child 13830 7f8c1b533e8b
permissions -rw-r--r--
converting HOL/UNITY to use unconditional fairness
wenzelm@10358
     1
(*  Title:      HOL/Relation.thy
nipkow@1128
     2
    ID:         $Id$
paulson@1983
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@1983
     4
    Copyright   1996  University of Cambridge
nipkow@1128
     5
*)
nipkow@1128
     6
berghofe@12905
     7
header {* Relations *}
berghofe@12905
     8
berghofe@12905
     9
theory Relation = Product_Type:
paulson@5978
    10
wenzelm@12913
    11
subsection {* Definitions *}
wenzelm@12913
    12
paulson@5978
    13
constdefs
wenzelm@10358
    14
  converse :: "('a * 'b) set => ('b * 'a) set"    ("(_^-1)" [1000] 999)
wenzelm@10358
    15
  "r^-1 == {(y, x). (x, y) : r}"
wenzelm@10358
    16
syntax (xsymbols)
berghofe@12905
    17
  converse :: "('a * 'b) set => ('b * 'a) set"    ("(_\<inverse>)" [1000] 999)
paulson@7912
    18
wenzelm@10358
    19
constdefs
nipkow@12487
    20
  rel_comp  :: "[('b * 'c) set, ('a * 'b) set] => ('a * 'c) set"  (infixr "O" 60)
wenzelm@12913
    21
  "r O s == {(x,z). EX y. (x, y) : s & (y, z) : r}"
wenzelm@12913
    22
oheimb@11136
    23
  Image :: "[('a * 'b) set, 'a set] => 'b set"                (infixl "``" 90)
wenzelm@12913
    24
  "r `` s == {y. EX x:s. (x,y):r}"
paulson@7912
    25
berghofe@12905
    26
  Id    :: "('a * 'a) set"  -- {* the identity relation *}
wenzelm@12913
    27
  "Id == {p. EX x. p = (x,x)}"
paulson@7912
    28
berghofe@12905
    29
  diag  :: "'a set => ('a * 'a) set"  -- {* diagonal: identity over a set *}
wenzelm@12913
    30
  "diag A == UN x:A. {(x,x)}"
wenzelm@12913
    31
oheimb@11136
    32
  Domain :: "('a * 'b) set => 'a set"
wenzelm@12913
    33
  "Domain r == {x. EX y. (x,y):r}"
paulson@5978
    34
oheimb@11136
    35
  Range  :: "('a * 'b) set => 'b set"
wenzelm@12913
    36
  "Range r == Domain(r^-1)"
paulson@5978
    37
oheimb@11136
    38
  Field :: "('a * 'a) set => 'a set"
wenzelm@12913
    39
  "Field r == Domain r Un Range r"
paulson@10786
    40
berghofe@12905
    41
  refl   :: "['a set, ('a * 'a) set] => bool"  -- {* reflexivity over a set *}
wenzelm@12913
    42
  "refl A r == r \<subseteq> A \<times> A & (ALL x: A. (x,x) : r)"
paulson@6806
    43
berghofe@12905
    44
  sym    :: "('a * 'a) set => bool"  -- {* symmetry predicate *}
wenzelm@12913
    45
  "sym r == ALL x y. (x,y): r --> (y,x): r"
paulson@6806
    46
berghofe@12905
    47
  antisym:: "('a * 'a) set => bool"  -- {* antisymmetry predicate *}
wenzelm@12913
    48
  "antisym r == ALL x y. (x,y):r --> (y,x):r --> x=y"
paulson@6806
    49
berghofe@12905
    50
  trans  :: "('a * 'a) set => bool"  -- {* transitivity predicate *}
wenzelm@12913
    51
  "trans r == (ALL x y z. (x,y):r --> (y,z):r --> (x,z):r)"
paulson@5978
    52
oheimb@11136
    53
  single_valued :: "('a * 'b) set => bool"
wenzelm@12913
    54
  "single_valued r == ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z)"
berghofe@7014
    55
oheimb@11136
    56
  inv_image :: "('b * 'b) set => ('a => 'b) => ('a * 'a) set"
wenzelm@12913
    57
  "inv_image r f == {(x, y). (f x, f y) : r}"
oheimb@11136
    58
paulson@6806
    59
syntax
berghofe@12905
    60
  reflexive :: "('a * 'a) set => bool"  -- {* reflexivity over a type *}
paulson@6806
    61
translations
paulson@6806
    62
  "reflexive" == "refl UNIV"
paulson@6806
    63
berghofe@12905
    64
wenzelm@12913
    65
subsection {* The identity relation *}
berghofe@12905
    66
berghofe@12905
    67
lemma IdI [intro]: "(a, a) : Id"
berghofe@12905
    68
  by (simp add: Id_def)
berghofe@12905
    69
berghofe@12905
    70
lemma IdE [elim!]: "p : Id ==> (!!x. p = (x, x) ==> P) ==> P"
berghofe@12905
    71
  by (unfold Id_def) (rules elim: CollectE)
berghofe@12905
    72
berghofe@12905
    73
lemma pair_in_Id_conv [iff]: "((a, b) : Id) = (a = b)"
berghofe@12905
    74
  by (unfold Id_def) blast
berghofe@12905
    75
berghofe@12905
    76
lemma reflexive_Id: "reflexive Id"
berghofe@12905
    77
  by (simp add: refl_def)
berghofe@12905
    78
berghofe@12905
    79
lemma antisym_Id: "antisym Id"
berghofe@12905
    80
  -- {* A strange result, since @{text Id} is also symmetric. *}
berghofe@12905
    81
  by (simp add: antisym_def)
berghofe@12905
    82
berghofe@12905
    83
lemma trans_Id: "trans Id"
berghofe@12905
    84
  by (simp add: trans_def)
berghofe@12905
    85
berghofe@12905
    86
wenzelm@12913
    87
subsection {* Diagonal: identity over a set *}
berghofe@12905
    88
paulson@13812
    89
lemma diag_empty [simp]: "diag {} = {}"
paulson@13812
    90
  by (simp add: diag_def) 
paulson@13812
    91
berghofe@12905
    92
lemma diag_eqI: "a = b ==> a : A ==> (a, b) : diag A"
berghofe@12905
    93
  by (simp add: diag_def)
berghofe@12905
    94
berghofe@12905
    95
lemma diagI [intro!]: "a : A ==> (a, a) : diag A"
berghofe@12905
    96
  by (rule diag_eqI) (rule refl)
berghofe@12905
    97
berghofe@12905
    98
lemma diagE [elim!]:
berghofe@12905
    99
  "c : diag A ==> (!!x. x : A ==> c = (x, x) ==> P) ==> P"
wenzelm@12913
   100
  -- {* The general elimination rule. *}
berghofe@12905
   101
  by (unfold diag_def) (rules elim!: UN_E singletonE)
berghofe@12905
   102
berghofe@12905
   103
lemma diag_iff: "((x, y) : diag A) = (x = y & x : A)"
berghofe@12905
   104
  by blast
berghofe@12905
   105
wenzelm@12913
   106
lemma diag_subset_Times: "diag A \<subseteq> A \<times> A"
berghofe@12905
   107
  by blast
berghofe@12905
   108
berghofe@12905
   109
berghofe@12905
   110
subsection {* Composition of two relations *}
berghofe@12905
   111
wenzelm@12913
   112
lemma rel_compI [intro]:
berghofe@12905
   113
  "(a, b) : s ==> (b, c) : r ==> (a, c) : r O s"
berghofe@12905
   114
  by (unfold rel_comp_def) blast
berghofe@12905
   115
wenzelm@12913
   116
lemma rel_compE [elim!]: "xz : r O s ==>
berghofe@12905
   117
  (!!x y z. xz = (x, z) ==> (x, y) : s ==> (y, z) : r  ==> P) ==> P"
berghofe@12905
   118
  by (unfold rel_comp_def) (rules elim!: CollectE splitE exE conjE)
berghofe@12905
   119
berghofe@12905
   120
lemma rel_compEpair:
berghofe@12905
   121
  "(a, c) : r O s ==> (!!y. (a, y) : s ==> (y, c) : r ==> P) ==> P"
berghofe@12905
   122
  by (rules elim: rel_compE Pair_inject ssubst)
berghofe@12905
   123
berghofe@12905
   124
lemma R_O_Id [simp]: "R O Id = R"
berghofe@12905
   125
  by fast
berghofe@12905
   126
berghofe@12905
   127
lemma Id_O_R [simp]: "Id O R = R"
berghofe@12905
   128
  by fast
berghofe@12905
   129
berghofe@12905
   130
lemma O_assoc: "(R O S) O T = R O (S O T)"
berghofe@12905
   131
  by blast
berghofe@12905
   132
wenzelm@12913
   133
lemma trans_O_subset: "trans r ==> r O r \<subseteq> r"
berghofe@12905
   134
  by (unfold trans_def) blast
berghofe@12905
   135
wenzelm@12913
   136
lemma rel_comp_mono: "r' \<subseteq> r ==> s' \<subseteq> s ==> (r' O s') \<subseteq> (r O s)"
berghofe@12905
   137
  by blast
berghofe@12905
   138
berghofe@12905
   139
lemma rel_comp_subset_Sigma:
wenzelm@12913
   140
    "s \<subseteq> A \<times> B ==> r \<subseteq> B \<times> C ==> (r O s) \<subseteq> A \<times> C"
berghofe@12905
   141
  by blast
berghofe@12905
   142
wenzelm@12913
   143
wenzelm@12913
   144
subsection {* Reflexivity *}
wenzelm@12913
   145
wenzelm@12913
   146
lemma reflI: "r \<subseteq> A \<times> A ==> (!!x. x : A ==> (x, x) : r) ==> refl A r"
berghofe@12905
   147
  by (unfold refl_def) (rules intro!: ballI)
berghofe@12905
   148
berghofe@12905
   149
lemma reflD: "refl A r ==> a : A ==> (a, a) : r"
berghofe@12905
   150
  by (unfold refl_def) blast
berghofe@12905
   151
wenzelm@12913
   152
wenzelm@12913
   153
subsection {* Antisymmetry *}
berghofe@12905
   154
berghofe@12905
   155
lemma antisymI:
berghofe@12905
   156
  "(!!x y. (x, y) : r ==> (y, x) : r ==> x=y) ==> antisym r"
berghofe@12905
   157
  by (unfold antisym_def) rules
berghofe@12905
   158
berghofe@12905
   159
lemma antisymD: "antisym r ==> (a, b) : r ==> (b, a) : r ==> a = b"
berghofe@12905
   160
  by (unfold antisym_def) rules
berghofe@12905
   161
wenzelm@12913
   162
wenzelm@12913
   163
subsection {* Transitivity *}
berghofe@12905
   164
berghofe@12905
   165
lemma transI:
berghofe@12905
   166
  "(!!x y z. (x, y) : r ==> (y, z) : r ==> (x, z) : r) ==> trans r"
berghofe@12905
   167
  by (unfold trans_def) rules
berghofe@12905
   168
berghofe@12905
   169
lemma transD: "trans r ==> (a, b) : r ==> (b, c) : r ==> (a, c) : r"
berghofe@12905
   170
  by (unfold trans_def) rules
berghofe@12905
   171
berghofe@12905
   172
wenzelm@12913
   173
subsection {* Converse *}
wenzelm@12913
   174
wenzelm@12913
   175
lemma converse_iff [iff]: "((a,b): r^-1) = ((b,a) : r)"
berghofe@12905
   176
  by (simp add: converse_def)
berghofe@12905
   177
nipkow@13343
   178
lemma converseI[sym]: "(a, b) : r ==> (b, a) : r^-1"
berghofe@12905
   179
  by (simp add: converse_def)
berghofe@12905
   180
nipkow@13343
   181
lemma converseD[sym]: "(a,b) : r^-1 ==> (b, a) : r"
berghofe@12905
   182
  by (simp add: converse_def)
berghofe@12905
   183
berghofe@12905
   184
lemma converseE [elim!]:
berghofe@12905
   185
  "yx : r^-1 ==> (!!x y. yx = (y, x) ==> (x, y) : r ==> P) ==> P"
wenzelm@12913
   186
    -- {* More general than @{text converseD}, as it ``splits'' the member of the relation. *}
berghofe@12905
   187
  by (unfold converse_def) (rules elim!: CollectE splitE bexE)
berghofe@12905
   188
berghofe@12905
   189
lemma converse_converse [simp]: "(r^-1)^-1 = r"
berghofe@12905
   190
  by (unfold converse_def) blast
berghofe@12905
   191
berghofe@12905
   192
lemma converse_rel_comp: "(r O s)^-1 = s^-1 O r^-1"
berghofe@12905
   193
  by blast
berghofe@12905
   194
berghofe@12905
   195
lemma converse_Id [simp]: "Id^-1 = Id"
berghofe@12905
   196
  by blast
berghofe@12905
   197
wenzelm@12913
   198
lemma converse_diag [simp]: "(diag A)^-1 = diag A"
berghofe@12905
   199
  by blast
berghofe@12905
   200
berghofe@12905
   201
lemma refl_converse: "refl A r ==> refl A (converse r)"
berghofe@12905
   202
  by (unfold refl_def) blast
berghofe@12905
   203
berghofe@12905
   204
lemma antisym_converse: "antisym (converse r) = antisym r"
berghofe@12905
   205
  by (unfold antisym_def) blast
berghofe@12905
   206
berghofe@12905
   207
lemma trans_converse: "trans (converse r) = trans r"
berghofe@12905
   208
  by (unfold trans_def) blast
berghofe@12905
   209
wenzelm@12913
   210
berghofe@12905
   211
subsection {* Domain *}
berghofe@12905
   212
berghofe@12905
   213
lemma Domain_iff: "(a : Domain r) = (EX y. (a, y) : r)"
berghofe@12905
   214
  by (unfold Domain_def) blast
berghofe@12905
   215
berghofe@12905
   216
lemma DomainI [intro]: "(a, b) : r ==> a : Domain r"
berghofe@12905
   217
  by (rules intro!: iffD2 [OF Domain_iff])
berghofe@12905
   218
berghofe@12905
   219
lemma DomainE [elim!]:
berghofe@12905
   220
  "a : Domain r ==> (!!y. (a, y) : r ==> P) ==> P"
berghofe@12905
   221
  by (rules dest!: iffD1 [OF Domain_iff])
berghofe@12905
   222
berghofe@12905
   223
lemma Domain_empty [simp]: "Domain {} = {}"
berghofe@12905
   224
  by blast
berghofe@12905
   225
berghofe@12905
   226
lemma Domain_insert: "Domain (insert (a, b) r) = insert a (Domain r)"
berghofe@12905
   227
  by blast
berghofe@12905
   228
berghofe@12905
   229
lemma Domain_Id [simp]: "Domain Id = UNIV"
berghofe@12905
   230
  by blast
berghofe@12905
   231
berghofe@12905
   232
lemma Domain_diag [simp]: "Domain (diag A) = A"
berghofe@12905
   233
  by blast
berghofe@12905
   234
berghofe@12905
   235
lemma Domain_Un_eq: "Domain(A Un B) = Domain(A) Un Domain(B)"
berghofe@12905
   236
  by blast
berghofe@12905
   237
wenzelm@12913
   238
lemma Domain_Int_subset: "Domain(A Int B) \<subseteq> Domain(A) Int Domain(B)"
berghofe@12905
   239
  by blast
berghofe@12905
   240
wenzelm@12913
   241
lemma Domain_Diff_subset: "Domain(A) - Domain(B) \<subseteq> Domain(A - B)"
berghofe@12905
   242
  by blast
berghofe@12905
   243
berghofe@12905
   244
lemma Domain_Union: "Domain (Union S) = (UN A:S. Domain A)"
berghofe@12905
   245
  by blast
berghofe@12905
   246
wenzelm@12913
   247
lemma Domain_mono: "r \<subseteq> s ==> Domain r \<subseteq> Domain s"
berghofe@12905
   248
  by blast
berghofe@12905
   249
berghofe@12905
   250
berghofe@12905
   251
subsection {* Range *}
berghofe@12905
   252
berghofe@12905
   253
lemma Range_iff: "(a : Range r) = (EX y. (y, a) : r)"
berghofe@12905
   254
  by (simp add: Domain_def Range_def)
berghofe@12905
   255
berghofe@12905
   256
lemma RangeI [intro]: "(a, b) : r ==> b : Range r"
berghofe@12905
   257
  by (unfold Range_def) (rules intro!: converseI DomainI)
berghofe@12905
   258
berghofe@12905
   259
lemma RangeE [elim!]: "b : Range r ==> (!!x. (x, b) : r ==> P) ==> P"
berghofe@12905
   260
  by (unfold Range_def) (rules elim!: DomainE dest!: converseD)
berghofe@12905
   261
berghofe@12905
   262
lemma Range_empty [simp]: "Range {} = {}"
berghofe@12905
   263
  by blast
berghofe@12905
   264
berghofe@12905
   265
lemma Range_insert: "Range (insert (a, b) r) = insert b (Range r)"
berghofe@12905
   266
  by blast
berghofe@12905
   267
berghofe@12905
   268
lemma Range_Id [simp]: "Range Id = UNIV"
berghofe@12905
   269
  by blast
berghofe@12905
   270
berghofe@12905
   271
lemma Range_diag [simp]: "Range (diag A) = A"
berghofe@12905
   272
  by auto
berghofe@12905
   273
berghofe@12905
   274
lemma Range_Un_eq: "Range(A Un B) = Range(A) Un Range(B)"
berghofe@12905
   275
  by blast
berghofe@12905
   276
wenzelm@12913
   277
lemma Range_Int_subset: "Range(A Int B) \<subseteq> Range(A) Int Range(B)"
berghofe@12905
   278
  by blast
berghofe@12905
   279
wenzelm@12913
   280
lemma Range_Diff_subset: "Range(A) - Range(B) \<subseteq> Range(A - B)"
berghofe@12905
   281
  by blast
berghofe@12905
   282
berghofe@12905
   283
lemma Range_Union: "Range (Union S) = (UN A:S. Range A)"
berghofe@12905
   284
  by blast
berghofe@12905
   285
berghofe@12905
   286
berghofe@12905
   287
subsection {* Image of a set under a relation *}
berghofe@12905
   288
wenzelm@12913
   289
lemma Image_iff: "(b : r``A) = (EX x:A. (x, b) : r)"
berghofe@12905
   290
  by (simp add: Image_def)
berghofe@12905
   291
wenzelm@12913
   292
lemma Image_singleton: "r``{a} = {b. (a, b) : r}"
berghofe@12905
   293
  by (simp add: Image_def)
berghofe@12905
   294
wenzelm@12913
   295
lemma Image_singleton_iff [iff]: "(b : r``{a}) = ((a, b) : r)"
berghofe@12905
   296
  by (rule Image_iff [THEN trans]) simp
berghofe@12905
   297
wenzelm@12913
   298
lemma ImageI [intro]: "(a, b) : r ==> a : A ==> b : r``A"
berghofe@12905
   299
  by (unfold Image_def) blast
berghofe@12905
   300
berghofe@12905
   301
lemma ImageE [elim!]:
wenzelm@12913
   302
    "b : r `` A ==> (!!x. (x, b) : r ==> x : A ==> P) ==> P"
berghofe@12905
   303
  by (unfold Image_def) (rules elim!: CollectE bexE)
berghofe@12905
   304
berghofe@12905
   305
lemma rev_ImageI: "a : A ==> (a, b) : r ==> b : r `` A"
berghofe@12905
   306
  -- {* This version's more effective when we already have the required @{text a} *}
berghofe@12905
   307
  by blast
berghofe@12905
   308
berghofe@12905
   309
lemma Image_empty [simp]: "R``{} = {}"
berghofe@12905
   310
  by blast
berghofe@12905
   311
berghofe@12905
   312
lemma Image_Id [simp]: "Id `` A = A"
berghofe@12905
   313
  by blast
berghofe@12905
   314
berghofe@12905
   315
lemma Image_diag [simp]: "diag A `` B = A Int B"
berghofe@12905
   316
  by blast
berghofe@12905
   317
wenzelm@12913
   318
lemma Image_Int_subset: "R `` (A Int B) \<subseteq> R `` A Int R `` B"
berghofe@12905
   319
  by blast
berghofe@12905
   320
berghofe@12905
   321
lemma Image_Un: "R `` (A Un B) = R `` A Un R `` B"
berghofe@12905
   322
  by blast
berghofe@12905
   323
paulson@13812
   324
lemma Un_Image: "(R \<union> S) `` A = R `` A \<union> S `` A"
paulson@13812
   325
  by blast
paulson@13812
   326
wenzelm@12913
   327
lemma Image_subset: "r \<subseteq> A \<times> B ==> r``C \<subseteq> B"
berghofe@12905
   328
  by (rules intro!: subsetI elim!: ImageE dest!: subsetD SigmaD2)
berghofe@12905
   329
berghofe@12905
   330
lemma Image_eq_UN: "r``B = (UN y: B. r``{y})"
berghofe@12905
   331
  -- {* NOT suitable for rewriting *}
berghofe@12905
   332
  by blast
berghofe@12905
   333
wenzelm@12913
   334
lemma Image_mono: "r' \<subseteq> r ==> A' \<subseteq> A ==> (r' `` A') \<subseteq> (r `` A)"
berghofe@12905
   335
  by blast
berghofe@12905
   336
berghofe@12905
   337
lemma Image_UN: "(r `` (UNION A B)) = (UN x:A.(r `` (B x)))"
berghofe@12905
   338
  by blast
berghofe@12905
   339
wenzelm@12913
   340
lemma Image_INT_subset: "(r `` (INTER A B)) \<subseteq> (INT x:A.(r `` (B x)))"
berghofe@12905
   341
  -- {* Converse inclusion fails *}
berghofe@12905
   342
  by blast
berghofe@12905
   343
wenzelm@12913
   344
lemma Image_subset_eq: "(r``A \<subseteq> B) = (A \<subseteq> - ((r^-1) `` (-B)))"
berghofe@12905
   345
  by blast
berghofe@12905
   346
berghofe@12905
   347
wenzelm@12913
   348
subsection {* Single valued relations *}
wenzelm@12913
   349
wenzelm@12913
   350
lemma single_valuedI:
berghofe@12905
   351
  "ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z) ==> single_valued r"
berghofe@12905
   352
  by (unfold single_valued_def)
berghofe@12905
   353
berghofe@12905
   354
lemma single_valuedD:
berghofe@12905
   355
  "single_valued r ==> (x, y) : r ==> (x, z) : r ==> y = z"
berghofe@12905
   356
  by (simp add: single_valued_def)
berghofe@12905
   357
berghofe@12905
   358
berghofe@12905
   359
subsection {* Graphs given by @{text Collect} *}
berghofe@12905
   360
berghofe@12905
   361
lemma Domain_Collect_split [simp]: "Domain{(x,y). P x y} = {x. EX y. P x y}"
berghofe@12905
   362
  by auto
berghofe@12905
   363
berghofe@12905
   364
lemma Range_Collect_split [simp]: "Range{(x,y). P x y} = {y. EX x. P x y}"
berghofe@12905
   365
  by auto
berghofe@12905
   366
berghofe@12905
   367
lemma Image_Collect_split [simp]: "{(x,y). P x y} `` A = {y. EX x:A. P x y}"
berghofe@12905
   368
  by auto
berghofe@12905
   369
berghofe@12905
   370
wenzelm@12913
   371
subsection {* Inverse image *}
berghofe@12905
   372
wenzelm@12913
   373
lemma trans_inv_image: "trans r ==> trans (inv_image r f)"
berghofe@12905
   374
  apply (unfold trans_def inv_image_def)
berghofe@12905
   375
  apply (simp (no_asm))
berghofe@12905
   376
  apply blast
berghofe@12905
   377
  done
berghofe@12905
   378
nipkow@1128
   379
end