src/HOL/ex/PER.thy
author hoelzl
Thu Jan 31 11:31:27 2013 +0100 (2013-01-31)
changeset 50999 3de230ed0547
parent 49834 b27bbb021df1
child 58889 5b7a9633cfa8
permissions -rw-r--r--
introduce order topology
wenzelm@10352
     1
(*  Title:      HOL/ex/PER.thy
wenzelm@10352
     2
    Author:     Oscar Slotosch and Markus Wenzel, TU Muenchen
wenzelm@10352
     3
*)
wenzelm@10352
     4
wenzelm@10352
     5
header {* Partial equivalence relations *}
wenzelm@10352
     6
haftmann@16417
     7
theory PER imports Main begin
wenzelm@10352
     8
wenzelm@10352
     9
text {*
wenzelm@12338
    10
  Higher-order quotients are defined over partial equivalence
wenzelm@12338
    11
  relations (PERs) instead of total ones.  We provide axiomatic type
wenzelm@12338
    12
  classes @{text "equiv < partial_equiv"} and a type constructor
wenzelm@12338
    13
  @{text "'a quot"} with basic operations.  This development is based
wenzelm@12338
    14
  on:
wenzelm@10352
    15
wenzelm@12338
    16
  Oscar Slotosch: \emph{Higher Order Quotients and their
wenzelm@12338
    17
  Implementation in Isabelle HOL.}  Elsa L. Gunter and Amy Felty,
wenzelm@12338
    18
  editors, Theorem Proving in Higher Order Logics: TPHOLs '97,
wenzelm@12338
    19
  Springer LNCS 1275, 1997.
wenzelm@10352
    20
*}
wenzelm@10352
    21
wenzelm@10352
    22
wenzelm@10352
    23
subsection {* Partial equivalence *}
wenzelm@10352
    24
wenzelm@10352
    25
text {*
wenzelm@12338
    26
  Type class @{text partial_equiv} models partial equivalence
wenzelm@12338
    27
  relations (PERs) using the polymorphic @{text "\<sim> :: 'a => 'a =>
wenzelm@12338
    28
  bool"} relation, which is required to be symmetric and transitive,
wenzelm@12338
    29
  but not necessarily reflexive.
wenzelm@10352
    30
*}
wenzelm@10352
    31
haftmann@35315
    32
class partial_equiv =
haftmann@35315
    33
  fixes eqv :: "'a => 'a => bool"    (infixl "\<sim>" 50)
haftmann@35315
    34
  assumes partial_equiv_sym [elim?]: "x \<sim> y ==> y \<sim> x"
haftmann@35315
    35
  assumes partial_equiv_trans [trans]: "x \<sim> y ==> y \<sim> z ==> x \<sim> z"
wenzelm@10352
    36
wenzelm@10352
    37
text {*
wenzelm@12338
    38
  \medskip The domain of a partial equivalence relation is the set of
wenzelm@12338
    39
  reflexive elements.  Due to symmetry and transitivity this
wenzelm@12338
    40
  characterizes exactly those elements that are connected with
wenzelm@12338
    41
  \emph{any} other one.
wenzelm@10352
    42
*}
wenzelm@10352
    43
wenzelm@19736
    44
definition
wenzelm@21404
    45
  "domain" :: "'a::partial_equiv set" where
wenzelm@19736
    46
  "domain = {x. x \<sim> x}"
wenzelm@10352
    47
wenzelm@10352
    48
lemma domainI [intro]: "x \<sim> x ==> x \<in> domain"
wenzelm@20811
    49
  unfolding domain_def by blast
wenzelm@10352
    50
wenzelm@10352
    51
lemma domainD [dest]: "x \<in> domain ==> x \<sim> x"
wenzelm@20811
    52
  unfolding domain_def by blast
wenzelm@10352
    53
wenzelm@10352
    54
theorem domainI' [elim?]: "x \<sim> y ==> x \<in> domain"
wenzelm@10352
    55
proof
wenzelm@10352
    56
  assume xy: "x \<sim> y"
wenzelm@10352
    57
  also from xy have "y \<sim> x" ..
wenzelm@10352
    58
  finally show "x \<sim> x" .
wenzelm@10352
    59
qed
wenzelm@10352
    60
wenzelm@10352
    61
wenzelm@10352
    62
subsection {* Equivalence on function spaces *}
wenzelm@10352
    63
wenzelm@10352
    64
text {*
wenzelm@12338
    65
  The @{text \<sim>} relation is lifted to function spaces.  It is
wenzelm@12338
    66
  important to note that this is \emph{not} the direct product, but a
wenzelm@12338
    67
  structural one corresponding to the congruence property.
wenzelm@10352
    68
*}
wenzelm@10352
    69
haftmann@35315
    70
instantiation "fun" :: (partial_equiv, partial_equiv) partial_equiv
haftmann@35315
    71
begin
haftmann@35315
    72
haftmann@35315
    73
definition
wenzelm@10352
    74
  eqv_fun_def: "f \<sim> g == \<forall>x \<in> domain. \<forall>y \<in> domain. x \<sim> y --> f x \<sim> g y"
wenzelm@10352
    75
wenzelm@10352
    76
lemma partial_equiv_funI [intro?]:
wenzelm@10352
    77
    "(!!x y. x \<in> domain ==> y \<in> domain ==> x \<sim> y ==> f x \<sim> g y) ==> f \<sim> g"
wenzelm@20811
    78
  unfolding eqv_fun_def by blast
wenzelm@10352
    79
wenzelm@10352
    80
lemma partial_equiv_funD [dest?]:
wenzelm@10352
    81
    "f \<sim> g ==> x \<in> domain ==> y \<in> domain ==> x \<sim> y ==> f x \<sim> g y"
wenzelm@20811
    82
  unfolding eqv_fun_def by blast
wenzelm@10352
    83
wenzelm@10352
    84
text {*
wenzelm@12338
    85
  The class of partial equivalence relations is closed under function
wenzelm@12338
    86
  spaces (in \emph{both} argument positions).
wenzelm@10352
    87
*}
wenzelm@10352
    88
haftmann@35315
    89
instance proof
wenzelm@10352
    90
  fix f g h :: "'a::partial_equiv => 'b::partial_equiv"
wenzelm@10352
    91
  assume fg: "f \<sim> g"
wenzelm@10352
    92
  show "g \<sim> f"
wenzelm@10352
    93
  proof
wenzelm@10352
    94
    fix x y :: 'a
wenzelm@10352
    95
    assume x: "x \<in> domain" and y: "y \<in> domain"
wenzelm@20811
    96
    assume "x \<sim> y" then have "y \<sim> x" ..
wenzelm@10352
    97
    with fg y x have "f y \<sim> g x" ..
wenzelm@20811
    98
    then show "g x \<sim> f y" ..
wenzelm@10352
    99
  qed
wenzelm@10352
   100
  assume gh: "g \<sim> h"
wenzelm@10352
   101
  show "f \<sim> h"
wenzelm@10352
   102
  proof
wenzelm@10352
   103
    fix x y :: 'a
wenzelm@10352
   104
    assume x: "x \<in> domain" and y: "y \<in> domain" and "x \<sim> y"
wenzelm@10352
   105
    with fg have "f x \<sim> g y" ..
wenzelm@10352
   106
    also from y have "y \<sim> y" ..
wenzelm@10352
   107
    with gh y y have "g y \<sim> h y" ..
wenzelm@10352
   108
    finally show "f x \<sim> h y" .
wenzelm@10352
   109
  qed
wenzelm@10352
   110
qed
wenzelm@10352
   111
haftmann@35315
   112
end
haftmann@35315
   113
wenzelm@10352
   114
wenzelm@10352
   115
subsection {* Total equivalence *}
wenzelm@10352
   116
wenzelm@10352
   117
text {*
wenzelm@12338
   118
  The class of total equivalence relations on top of PERs.  It
wenzelm@12338
   119
  coincides with the standard notion of equivalence, i.e.\ @{text "\<sim>
wenzelm@12338
   120
  :: 'a => 'a => bool"} is required to be reflexive, transitive and
wenzelm@12338
   121
  symmetric.
wenzelm@10352
   122
*}
wenzelm@10352
   123
haftmann@35315
   124
class equiv =
haftmann@35315
   125
  assumes eqv_refl [intro]: "x \<sim> x"
wenzelm@10352
   126
wenzelm@10352
   127
text {*
wenzelm@12338
   128
  On total equivalences all elements are reflexive, and congruence
wenzelm@12338
   129
  holds unconditionally.
wenzelm@10352
   130
*}
wenzelm@10352
   131
wenzelm@10352
   132
theorem equiv_domain [intro]: "(x::'a::equiv) \<in> domain"
wenzelm@10352
   133
proof
wenzelm@10352
   134
  show "x \<sim> x" ..
wenzelm@10352
   135
qed
wenzelm@10352
   136
wenzelm@10352
   137
theorem equiv_cong [dest?]: "f \<sim> g ==> x \<sim> y ==> f x \<sim> g (y::'a::equiv)"
wenzelm@10352
   138
proof -
wenzelm@10352
   139
  assume "f \<sim> g"
wenzelm@10352
   140
  moreover have "x \<in> domain" ..
wenzelm@10352
   141
  moreover have "y \<in> domain" ..
wenzelm@10352
   142
  moreover assume "x \<sim> y"
wenzelm@10352
   143
  ultimately show ?thesis ..
wenzelm@10352
   144
qed
wenzelm@10352
   145
wenzelm@10352
   146
wenzelm@10352
   147
subsection {* Quotient types *}
wenzelm@10352
   148
wenzelm@10352
   149
text {*
wenzelm@12338
   150
  The quotient type @{text "'a quot"} consists of all
wenzelm@12338
   151
  \emph{equivalence classes} over elements of the base type @{typ 'a}.
wenzelm@10352
   152
*}
wenzelm@10352
   153
wenzelm@45694
   154
definition "quot = {{x. a \<sim> x}| a::'a::partial_equiv. True}"
wenzelm@45694
   155
wenzelm@49834
   156
typedef 'a quot = "quot :: 'a::partial_equiv set set"
wenzelm@45694
   157
  unfolding quot_def by blast
wenzelm@10352
   158
wenzelm@10352
   159
lemma quotI [intro]: "{x. a \<sim> x} \<in> quot"
wenzelm@20811
   160
  unfolding quot_def by blast
wenzelm@10352
   161
wenzelm@10352
   162
lemma quotE [elim]: "R \<in> quot ==> (!!a. R = {x. a \<sim> x} ==> C) ==> C"
wenzelm@20811
   163
  unfolding quot_def by blast
wenzelm@10352
   164
wenzelm@10352
   165
text {*
wenzelm@12338
   166
  \medskip Abstracted equivalence classes are the canonical
wenzelm@12338
   167
  representation of elements of a quotient type.
wenzelm@10352
   168
*}
wenzelm@10352
   169
wenzelm@19736
   170
definition
wenzelm@21404
   171
  eqv_class :: "('a::partial_equiv) => 'a quot"    ("\<lfloor>_\<rfloor>") where
wenzelm@19736
   172
  "\<lfloor>a\<rfloor> = Abs_quot {x. a \<sim> x}"
wenzelm@10352
   173
wenzelm@10352
   174
theorem quot_rep: "\<exists>a. A = \<lfloor>a\<rfloor>"
wenzelm@10352
   175
proof (cases A)
wenzelm@10352
   176
  fix R assume R: "A = Abs_quot R"
wenzelm@20811
   177
  assume "R \<in> quot" then have "\<exists>a. R = {x. a \<sim> x}" by blast
wenzelm@10352
   178
  with R have "\<exists>a. A = Abs_quot {x. a \<sim> x}" by blast
wenzelm@20811
   179
  then show ?thesis by (unfold eqv_class_def)
wenzelm@10352
   180
qed
wenzelm@10352
   181
wenzelm@20811
   182
lemma quot_cases [cases type: quot]:
wenzelm@20811
   183
  obtains (rep) a where "A = \<lfloor>a\<rfloor>"
wenzelm@20811
   184
  using quot_rep by blast
wenzelm@10352
   185
wenzelm@10352
   186
wenzelm@10352
   187
subsection {* Equality on quotients *}
wenzelm@10352
   188
wenzelm@10352
   189
text {*
wenzelm@12338
   190
  Equality of canonical quotient elements corresponds to the original
wenzelm@12338
   191
  relation as follows.
wenzelm@10352
   192
*}
wenzelm@10352
   193
wenzelm@10352
   194
theorem eqv_class_eqI [intro]: "a \<sim> b ==> \<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10352
   195
proof -
wenzelm@10352
   196
  assume ab: "a \<sim> b"
wenzelm@10352
   197
  have "{x. a \<sim> x} = {x. b \<sim> x}"
wenzelm@10352
   198
  proof (rule Collect_cong)
wenzelm@10352
   199
    fix x show "(a \<sim> x) = (b \<sim> x)"
wenzelm@10352
   200
    proof
wenzelm@10352
   201
      from ab have "b \<sim> a" ..
wenzelm@10352
   202
      also assume "a \<sim> x"
wenzelm@10352
   203
      finally show "b \<sim> x" .
wenzelm@10352
   204
    next
wenzelm@10352
   205
      note ab
wenzelm@10352
   206
      also assume "b \<sim> x"
wenzelm@10352
   207
      finally show "a \<sim> x" .
wenzelm@10352
   208
    qed
wenzelm@10352
   209
  qed
wenzelm@20811
   210
  then show ?thesis by (simp only: eqv_class_def)
wenzelm@10352
   211
qed
wenzelm@10352
   212
wenzelm@10352
   213
theorem eqv_class_eqD' [dest?]: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor> ==> a \<in> domain ==> a \<sim> b"
wenzelm@10352
   214
proof (unfold eqv_class_def)
wenzelm@10352
   215
  assume "Abs_quot {x. a \<sim> x} = Abs_quot {x. b \<sim> x}"
wenzelm@20811
   216
  then have "{x. a \<sim> x} = {x. b \<sim> x}" by (simp only: Abs_quot_inject quotI)
wenzelm@20811
   217
  moreover assume "a \<in> domain" then have "a \<sim> a" ..
wenzelm@10352
   218
  ultimately have "a \<in> {x. b \<sim> x}" by blast
wenzelm@20811
   219
  then have "b \<sim> a" by blast
wenzelm@20811
   220
  then show "a \<sim> b" ..
wenzelm@10352
   221
qed
wenzelm@10352
   222
wenzelm@10352
   223
theorem eqv_class_eqD [dest?]: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor> ==> a \<sim> (b::'a::equiv)"
wenzelm@10352
   224
proof (rule eqv_class_eqD')
wenzelm@10352
   225
  show "a \<in> domain" ..
wenzelm@10352
   226
qed
wenzelm@10352
   227
wenzelm@10352
   228
lemma eqv_class_eq' [simp]: "a \<in> domain ==> (\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>) = (a \<sim> b)"
wenzelm@28616
   229
  using eqv_class_eqI eqv_class_eqD' by (blast del: eqv_refl)
wenzelm@10352
   230
wenzelm@10352
   231
lemma eqv_class_eq [simp]: "(\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>) = (a \<sim> (b::'a::equiv))"
wenzelm@20811
   232
  using eqv_class_eqI eqv_class_eqD by blast
wenzelm@10352
   233
wenzelm@10352
   234
wenzelm@10352
   235
subsection {* Picking representing elements *}
wenzelm@10352
   236
wenzelm@19736
   237
definition
wenzelm@21404
   238
  pick :: "'a::partial_equiv quot => 'a" where
wenzelm@19736
   239
  "pick A = (SOME a. A = \<lfloor>a\<rfloor>)"
wenzelm@10352
   240
wenzelm@10352
   241
theorem pick_eqv' [intro?, simp]: "a \<in> domain ==> pick \<lfloor>a\<rfloor> \<sim> a"
wenzelm@10352
   242
proof (unfold pick_def)
wenzelm@10352
   243
  assume a: "a \<in> domain"
wenzelm@10352
   244
  show "(SOME x. \<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>) \<sim> a"
wenzelm@10352
   245
  proof (rule someI2)
wenzelm@10352
   246
    show "\<lfloor>a\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10352
   247
    fix x assume "\<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>"
wenzelm@23373
   248
    from this and a have "a \<sim> x" ..
wenzelm@20811
   249
    then show "x \<sim> a" ..
wenzelm@10352
   250
  qed
wenzelm@10352
   251
qed
wenzelm@10352
   252
wenzelm@10352
   253
theorem pick_eqv [intro, simp]: "pick \<lfloor>a\<rfloor> \<sim> (a::'a::equiv)"
wenzelm@10352
   254
proof (rule pick_eqv')
wenzelm@10352
   255
  show "a \<in> domain" ..
wenzelm@10352
   256
qed
wenzelm@10352
   257
wenzelm@10352
   258
theorem pick_inverse: "\<lfloor>pick A\<rfloor> = (A::'a::equiv quot)"
wenzelm@10352
   259
proof (cases A)
wenzelm@10352
   260
  fix a assume a: "A = \<lfloor>a\<rfloor>"
wenzelm@20811
   261
  then have "pick A \<sim> a" by simp
wenzelm@20811
   262
  then have "\<lfloor>pick A\<rfloor> = \<lfloor>a\<rfloor>" by simp
wenzelm@10352
   263
  with a show ?thesis by simp
wenzelm@10352
   264
qed
wenzelm@10352
   265
wenzelm@10352
   266
end