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