src/HOL/Relation.thy
changeset 12905 bbbae3f359e6
parent 12487 bbd564190c9b
child 12913 5ac498bffb6b
     1.1 --- a/src/HOL/Relation.thy	Wed Feb 20 00:55:42 2002 +0100
     1.2 +++ b/src/HOL/Relation.thy	Wed Feb 20 15:47:42 2002 +0100
     1.3 @@ -4,13 +4,15 @@
     1.4      Copyright   1996  University of Cambridge
     1.5  *)
     1.6  
     1.7 -Relation = Product_Type +
     1.8 +header {* Relations *}
     1.9 +
    1.10 +theory Relation = Product_Type:
    1.11  
    1.12  constdefs
    1.13    converse :: "('a * 'b) set => ('b * 'a) set"    ("(_^-1)" [1000] 999)
    1.14    "r^-1 == {(y, x). (x, y) : r}"
    1.15  syntax (xsymbols)
    1.16 -  converse :: "('a * 'b) set => ('b * 'a) set"    ("(_\\<inverse>)" [1000] 999)
    1.17 +  converse :: "('a * 'b) set => ('b * 'a) set"    ("(_\<inverse>)" [1000] 999)
    1.18  
    1.19  constdefs
    1.20    rel_comp  :: "[('b * 'c) set, ('a * 'b) set] => ('a * 'c) set"  (infixr "O" 60)
    1.21 @@ -19,10 +21,10 @@
    1.22    Image :: "[('a * 'b) set, 'a set] => 'b set"                (infixl "``" 90)
    1.23      "r `` s == {y. ? x:s. (x,y):r}"
    1.24  
    1.25 -  Id    :: "('a * 'a) set"                            (*the identity relation*)
    1.26 +  Id    :: "('a * 'a) set"  -- {* the identity relation *}
    1.27      "Id == {p. ? x. p = (x,x)}"
    1.28  
    1.29 -  diag  :: "'a set => ('a * 'a) set"          (*diagonal: identity over a set*)
    1.30 +  diag  :: "'a set => ('a * 'a) set"  -- {* diagonal: identity over a set *}
    1.31      "diag(A) == UN x:A. {(x,x)}"
    1.32    
    1.33    Domain :: "('a * 'b) set => 'a set"
    1.34 @@ -34,16 +36,16 @@
    1.35    Field :: "('a * 'a) set => 'a set"
    1.36      "Field r == Domain r Un Range r"
    1.37  
    1.38 -  refl   :: "['a set, ('a * 'a) set] => bool" (*reflexivity over a set*)
    1.39 +  refl   :: "['a set, ('a * 'a) set] => bool"  -- {* reflexivity over a set *}
    1.40      "refl A r == r <= A <*> A & (ALL x: A. (x,x) : r)"
    1.41  
    1.42 -  sym    :: "('a * 'a) set => bool"             (*symmetry predicate*)
    1.43 +  sym    :: "('a * 'a) set => bool"  -- {* symmetry predicate *}
    1.44      "sym(r) == ALL x y. (x,y): r --> (y,x): r"
    1.45  
    1.46 -  antisym:: "('a * 'a) set => bool"          (*antisymmetry predicate*)
    1.47 +  antisym:: "('a * 'a) set => bool"  -- {* antisymmetry predicate *}
    1.48      "antisym(r) == ALL x y. (x,y):r --> (y,x):r --> x=y"
    1.49  
    1.50 -  trans  :: "('a * 'a) set => bool"          (*transitivity predicate*)
    1.51 +  trans  :: "('a * 'a) set => bool"  -- {* transitivity predicate *}
    1.52      "trans(r) == (!x y z. (x,y):r --> (y,z):r --> (x,z):r)"
    1.53  
    1.54    single_valued :: "('a * 'b) set => bool"
    1.55 @@ -56,8 +58,329 @@
    1.56      "inv_image r f == {(x,y). (f(x), f(y)) : r}"
    1.57  
    1.58  syntax
    1.59 -  reflexive :: "('a * 'a) set => bool"       (*reflexivity over a type*)
    1.60 +  reflexive :: "('a * 'a) set => bool"  -- {* reflexivity over a type *}
    1.61  translations
    1.62    "reflexive" == "refl UNIV"
    1.63  
    1.64 +
    1.65 +subsection {* Identity relation *}
    1.66 +
    1.67 +lemma IdI [intro]: "(a, a) : Id"
    1.68 +  by (simp add: Id_def)
    1.69 +
    1.70 +lemma IdE [elim!]: "p : Id ==> (!!x. p = (x, x) ==> P) ==> P"
    1.71 +  by (unfold Id_def) (rules elim: CollectE)
    1.72 +
    1.73 +lemma pair_in_Id_conv [iff]: "((a, b) : Id) = (a = b)"
    1.74 +  by (unfold Id_def) blast
    1.75 +
    1.76 +lemma reflexive_Id: "reflexive Id"
    1.77 +  by (simp add: refl_def)
    1.78 +
    1.79 +lemma antisym_Id: "antisym Id"
    1.80 +  -- {* A strange result, since @{text Id} is also symmetric. *}
    1.81 +  by (simp add: antisym_def)
    1.82 +
    1.83 +lemma trans_Id: "trans Id"
    1.84 +  by (simp add: trans_def)
    1.85 +
    1.86 +
    1.87 +subsection {* Diagonal relation: identity restricted to some set *}
    1.88 +
    1.89 +lemma diag_eqI: "a = b ==> a : A ==> (a, b) : diag A"
    1.90 +  by (simp add: diag_def)
    1.91 +
    1.92 +lemma diagI [intro!]: "a : A ==> (a, a) : diag A"
    1.93 +  by (rule diag_eqI) (rule refl)
    1.94 +
    1.95 +lemma diagE [elim!]:
    1.96 +  "c : diag A ==> (!!x. x : A ==> c = (x, x) ==> P) ==> P"
    1.97 +  -- {* The general elimination rule *}
    1.98 +  by (unfold diag_def) (rules elim!: UN_E singletonE)
    1.99 +
   1.100 +lemma diag_iff: "((x, y) : diag A) = (x = y & x : A)"
   1.101 +  by blast
   1.102 +
   1.103 +lemma diag_subset_Times: "diag A <= A <*> A"
   1.104 +  by blast
   1.105 +
   1.106 +
   1.107 +subsection {* Composition of two relations *}
   1.108 +
   1.109 +lemma rel_compI [intro]: 
   1.110 +  "(a, b) : s ==> (b, c) : r ==> (a, c) : r O s"
   1.111 +  by (unfold rel_comp_def) blast
   1.112 +
   1.113 +lemma rel_compE [elim!]: "xz : r O s ==>   
   1.114 +  (!!x y z. xz = (x, z) ==> (x, y) : s ==> (y, z) : r  ==> P) ==> P"
   1.115 +  by (unfold rel_comp_def) (rules elim!: CollectE splitE exE conjE)
   1.116 +
   1.117 +lemma rel_compEpair:
   1.118 +  "(a, c) : r O s ==> (!!y. (a, y) : s ==> (y, c) : r ==> P) ==> P"
   1.119 +  by (rules elim: rel_compE Pair_inject ssubst)
   1.120 +
   1.121 +lemma R_O_Id [simp]: "R O Id = R"
   1.122 +  by fast
   1.123 +
   1.124 +lemma Id_O_R [simp]: "Id O R = R"
   1.125 +  by fast
   1.126 +
   1.127 +lemma O_assoc: "(R O S) O T = R O (S O T)"
   1.128 +  by blast
   1.129 +
   1.130 +lemma trans_O_subset: "trans r ==> r O r <= r"
   1.131 +  by (unfold trans_def) blast
   1.132 +
   1.133 +lemma rel_comp_mono: "[| r'<=r; s'<=s |] ==> (r' O s') <= (r O s)"
   1.134 +  by blast
   1.135 +
   1.136 +lemma rel_comp_subset_Sigma:
   1.137 +  "[| s <= A <*> B;  r <= B <*> C |] ==> (r O s) <= A <*> C"
   1.138 +  by blast
   1.139 +
   1.140 +subsection {* Natural deduction for refl(r) *}
   1.141 +
   1.142 +lemma reflI: "r <= A <*> A ==> (!!x. x : A ==> (x, x) : r) ==> refl A r"
   1.143 +  by (unfold refl_def) (rules intro!: ballI)
   1.144 +
   1.145 +lemma reflD: "refl A r ==> a : A ==> (a, a) : r"
   1.146 +  by (unfold refl_def) blast
   1.147 +
   1.148 +subsection {* Natural deduction for antisym(r) *}
   1.149 +
   1.150 +lemma antisymI:
   1.151 +  "(!!x y. (x, y) : r ==> (y, x) : r ==> x=y) ==> antisym r"
   1.152 +  by (unfold antisym_def) rules
   1.153 +
   1.154 +lemma antisymD: "antisym r ==> (a, b) : r ==> (b, a) : r ==> a = b"
   1.155 +  by (unfold antisym_def) rules
   1.156 +
   1.157 +subsection {* Natural deduction for trans(r) *}
   1.158 +
   1.159 +lemma transI:
   1.160 +  "(!!x y z. (x, y) : r ==> (y, z) : r ==> (x, z) : r) ==> trans r"
   1.161 +  by (unfold trans_def) rules
   1.162 +
   1.163 +lemma transD: "trans r ==> (a, b) : r ==> (b, c) : r ==> (a, c) : r"
   1.164 +  by (unfold trans_def) rules
   1.165 +
   1.166 +subsection {* Natural deduction for r^-1 *}
   1.167 +
   1.168 +lemma converse_iff [iff]: "((a,b): r^-1) = ((b,a):r)"
   1.169 +  by (simp add: converse_def)
   1.170 +
   1.171 +lemma converseI: "(a,b):r ==> (b,a): r^-1"
   1.172 +  by (simp add: converse_def)
   1.173 +
   1.174 +lemma converseD: "(a,b) : r^-1 ==> (b,a) : r"
   1.175 +  by (simp add: converse_def)
   1.176 +
   1.177 +(*More general than converseD, as it "splits" the member of the relation*)
   1.178 +
   1.179 +lemma converseE [elim!]:
   1.180 +  "yx : r^-1 ==> (!!x y. yx = (y, x) ==> (x, y) : r ==> P) ==> P"
   1.181 +  by (unfold converse_def) (rules elim!: CollectE splitE bexE)
   1.182 +
   1.183 +lemma converse_converse [simp]: "(r^-1)^-1 = r"
   1.184 +  by (unfold converse_def) blast
   1.185 +
   1.186 +lemma converse_rel_comp: "(r O s)^-1 = s^-1 O r^-1"
   1.187 +  by blast
   1.188 +
   1.189 +lemma converse_Id [simp]: "Id^-1 = Id"
   1.190 +  by blast
   1.191 +
   1.192 +lemma converse_diag [simp]: "(diag A) ^-1 = diag A"
   1.193 +  by blast
   1.194 +
   1.195 +lemma refl_converse: "refl A r ==> refl A (converse r)"
   1.196 +  by (unfold refl_def) blast
   1.197 +
   1.198 +lemma antisym_converse: "antisym (converse r) = antisym r"
   1.199 +  by (unfold antisym_def) blast
   1.200 +
   1.201 +lemma trans_converse: "trans (converse r) = trans r"
   1.202 +  by (unfold trans_def) blast
   1.203 +
   1.204 +subsection {* Domain *}
   1.205 +
   1.206 +lemma Domain_iff: "(a : Domain r) = (EX y. (a, y) : r)"
   1.207 +  by (unfold Domain_def) blast
   1.208 +
   1.209 +lemma DomainI [intro]: "(a, b) : r ==> a : Domain r"
   1.210 +  by (rules intro!: iffD2 [OF Domain_iff])
   1.211 +
   1.212 +lemma DomainE [elim!]:
   1.213 +  "a : Domain r ==> (!!y. (a, y) : r ==> P) ==> P"
   1.214 +  by (rules dest!: iffD1 [OF Domain_iff])
   1.215 +
   1.216 +lemma Domain_empty [simp]: "Domain {} = {}"
   1.217 +  by blast
   1.218 +
   1.219 +lemma Domain_insert: "Domain (insert (a, b) r) = insert a (Domain r)"
   1.220 +  by blast
   1.221 +
   1.222 +lemma Domain_Id [simp]: "Domain Id = UNIV"
   1.223 +  by blast
   1.224 +
   1.225 +lemma Domain_diag [simp]: "Domain (diag A) = A"
   1.226 +  by blast
   1.227 +
   1.228 +lemma Domain_Un_eq: "Domain(A Un B) = Domain(A) Un Domain(B)"
   1.229 +  by blast
   1.230 +
   1.231 +lemma Domain_Int_subset: "Domain(A Int B) <= Domain(A) Int Domain(B)"
   1.232 +  by blast
   1.233 +
   1.234 +lemma Domain_Diff_subset: "Domain(A) - Domain(B) <= Domain(A - B)"
   1.235 +  by blast
   1.236 +
   1.237 +lemma Domain_Union: "Domain (Union S) = (UN A:S. Domain A)"
   1.238 +  by blast
   1.239 +
   1.240 +lemma Domain_mono: "r <= s ==> Domain r <= Domain s"
   1.241 +  by blast
   1.242 +
   1.243 +
   1.244 +subsection {* Range *}
   1.245 +
   1.246 +lemma Range_iff: "(a : Range r) = (EX y. (y, a) : r)"
   1.247 +  by (simp add: Domain_def Range_def)
   1.248 +
   1.249 +lemma RangeI [intro]: "(a, b) : r ==> b : Range r"
   1.250 +  by (unfold Range_def) (rules intro!: converseI DomainI)
   1.251 +
   1.252 +lemma RangeE [elim!]: "b : Range r ==> (!!x. (x, b) : r ==> P) ==> P"
   1.253 +  by (unfold Range_def) (rules elim!: DomainE dest!: converseD)
   1.254 +
   1.255 +lemma Range_empty [simp]: "Range {} = {}"
   1.256 +  by blast
   1.257 +
   1.258 +lemma Range_insert: "Range (insert (a, b) r) = insert b (Range r)"
   1.259 +  by blast
   1.260 +
   1.261 +lemma Range_Id [simp]: "Range Id = UNIV"
   1.262 +  by blast
   1.263 +
   1.264 +lemma Range_diag [simp]: "Range (diag A) = A"
   1.265 +  by auto
   1.266 +
   1.267 +lemma Range_Un_eq: "Range(A Un B) = Range(A) Un Range(B)"
   1.268 +  by blast
   1.269 +
   1.270 +lemma Range_Int_subset: "Range(A Int B) <= Range(A) Int Range(B)"
   1.271 +  by blast
   1.272 +
   1.273 +lemma Range_Diff_subset: "Range(A) - Range(B) <= Range(A - B)"
   1.274 +  by blast
   1.275 +
   1.276 +lemma Range_Union: "Range (Union S) = (UN A:S. Range A)"
   1.277 +  by blast
   1.278 +
   1.279 +
   1.280 +subsection {* Image of a set under a relation *}
   1.281 +
   1.282 +ML {* overload_1st_set "Relation.Image" *}
   1.283 +
   1.284 +lemma Image_iff: "(b : r``A) = (EX x:A. (x,b):r)"
   1.285 +  by (simp add: Image_def)
   1.286 +
   1.287 +lemma Image_singleton: "r``{a} = {b. (a,b):r}"
   1.288 +  by (simp add: Image_def)
   1.289 +
   1.290 +lemma Image_singleton_iff [iff]: "(b : r``{a}) = ((a,b):r)"
   1.291 +  by (rule Image_iff [THEN trans]) simp
   1.292 +
   1.293 +lemma ImageI [intro]: "[| (a,b): r;  a:A |] ==> b : r``A"
   1.294 +  by (unfold Image_def) blast
   1.295 +
   1.296 +lemma ImageE [elim!]:
   1.297 +  "b : r `` A ==> (!!x. (x, b) : r ==> x : A ==> P) ==> P"
   1.298 +  by (unfold Image_def) (rules elim!: CollectE bexE)
   1.299 +
   1.300 +lemma rev_ImageI: "a : A ==> (a, b) : r ==> b : r `` A"
   1.301 +  -- {* This version's more effective when we already have the required @{text a} *}
   1.302 +  by blast
   1.303 +
   1.304 +lemma Image_empty [simp]: "R``{} = {}"
   1.305 +  by blast
   1.306 +
   1.307 +lemma Image_Id [simp]: "Id `` A = A"
   1.308 +  by blast
   1.309 +
   1.310 +lemma Image_diag [simp]: "diag A `` B = A Int B"
   1.311 +  by blast
   1.312 +
   1.313 +lemma Image_Int_subset: "R `` (A Int B) <= R `` A Int R `` B"
   1.314 +  by blast
   1.315 +
   1.316 +lemma Image_Un: "R `` (A Un B) = R `` A Un R `` B"
   1.317 +  by blast
   1.318 +
   1.319 +lemma Image_subset: "r <= A <*> B ==> r``C <= B"
   1.320 +  by (rules intro!: subsetI elim!: ImageE dest!: subsetD SigmaD2)
   1.321 +
   1.322 +lemma Image_eq_UN: "r``B = (UN y: B. r``{y})"
   1.323 +  -- {* NOT suitable for rewriting *}
   1.324 +  by blast
   1.325 +
   1.326 +lemma Image_mono: "[| r'<=r; A'<=A |] ==> (r' `` A') <= (r `` A)"
   1.327 +  by blast
   1.328 +
   1.329 +lemma Image_UN: "(r `` (UNION A B)) = (UN x:A.(r `` (B x)))"
   1.330 +  by blast
   1.331 +
   1.332 +lemma Image_INT_subset: "(r `` (INTER A B)) <= (INT x:A.(r `` (B x)))"
   1.333 +  -- {* Converse inclusion fails *}
   1.334 +  by blast
   1.335 +
   1.336 +lemma Image_subset_eq: "(r``A <= B) = (A <= - ((r^-1) `` (-B)))"
   1.337 +  by blast
   1.338 +
   1.339 +subsection "single_valued"
   1.340 +
   1.341 +lemma single_valuedI: 
   1.342 +  "ALL x y. (x,y):r --> (ALL z. (x,z):r --> y=z) ==> single_valued r"
   1.343 +  by (unfold single_valued_def)
   1.344 +
   1.345 +lemma single_valuedD:
   1.346 +  "single_valued r ==> (x, y) : r ==> (x, z) : r ==> y = z"
   1.347 +  by (simp add: single_valued_def)
   1.348 +
   1.349 +
   1.350 +subsection {* Graphs given by @{text Collect} *}
   1.351 +
   1.352 +lemma Domain_Collect_split [simp]: "Domain{(x,y). P x y} = {x. EX y. P x y}"
   1.353 +  by auto
   1.354 +
   1.355 +lemma Range_Collect_split [simp]: "Range{(x,y). P x y} = {y. EX x. P x y}"
   1.356 +  by auto
   1.357 +
   1.358 +lemma Image_Collect_split [simp]: "{(x,y). P x y} `` A = {y. EX x:A. P x y}"
   1.359 +  by auto
   1.360 +
   1.361 +
   1.362 +subsection {* Composition of function and relation *}
   1.363 +
   1.364 +lemma fun_rel_comp_mono: "A <= B ==> fun_rel_comp f A <= fun_rel_comp f B"
   1.365 +  by (unfold fun_rel_comp_def) fast
   1.366 +
   1.367 +lemma fun_rel_comp_unique: 
   1.368 +  "ALL x. EX! y. (f x, y) : R ==> EX! g. g : fun_rel_comp f R"
   1.369 +  apply (unfold fun_rel_comp_def)
   1.370 +  apply (rule_tac a = "%x. THE y. (f x, y) : R" in ex1I)
   1.371 +  apply (fast dest!: theI')
   1.372 +  apply (fast intro: ext the1_equality [symmetric])
   1.373 +  done
   1.374 +
   1.375 +
   1.376 +subsection "inverse image"
   1.377 +
   1.378 +lemma trans_inv_image: 
   1.379 +  "trans r ==> trans (inv_image r f)"
   1.380 +  apply (unfold trans_def inv_image_def)
   1.381 +  apply (simp (no_asm))
   1.382 +  apply blast
   1.383 +  done
   1.384 +
   1.385  end