src/HOL/ex/PER.thy
author haftmann
Tue Feb 23 10:11:12 2010 +0100 (2010-02-23)
changeset 35315 fbdc860d87a3
parent 28616 ac1da69fbc5a
child 45694 4a8743618257
permissions -rw-r--r--
dropped axclass
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
haftmann@35315
   154
typedef 'a quot = "{{x. a \<sim> x}| a::'a::partial_equiv. True}"
wenzelm@10352
   155
  by blast
wenzelm@10352
   156
wenzelm@10352
   157
lemma quotI [intro]: "{x. a \<sim> x} \<in> quot"
wenzelm@20811
   158
  unfolding quot_def by blast
wenzelm@10352
   159
wenzelm@10352
   160
lemma quotE [elim]: "R \<in> quot ==> (!!a. R = {x. a \<sim> x} ==> C) ==> C"
wenzelm@20811
   161
  unfolding quot_def by blast
wenzelm@10352
   162
wenzelm@10352
   163
text {*
wenzelm@12338
   164
  \medskip Abstracted equivalence classes are the canonical
wenzelm@12338
   165
  representation of elements of a quotient type.
wenzelm@10352
   166
*}
wenzelm@10352
   167
wenzelm@19736
   168
definition
wenzelm@21404
   169
  eqv_class :: "('a::partial_equiv) => 'a quot"    ("\<lfloor>_\<rfloor>") where
wenzelm@19736
   170
  "\<lfloor>a\<rfloor> = Abs_quot {x. a \<sim> x}"
wenzelm@10352
   171
wenzelm@10352
   172
theorem quot_rep: "\<exists>a. A = \<lfloor>a\<rfloor>"
wenzelm@10352
   173
proof (cases A)
wenzelm@10352
   174
  fix R assume R: "A = Abs_quot R"
wenzelm@20811
   175
  assume "R \<in> quot" then have "\<exists>a. R = {x. a \<sim> x}" by blast
wenzelm@10352
   176
  with R have "\<exists>a. A = Abs_quot {x. a \<sim> x}" by blast
wenzelm@20811
   177
  then show ?thesis by (unfold eqv_class_def)
wenzelm@10352
   178
qed
wenzelm@10352
   179
wenzelm@20811
   180
lemma quot_cases [cases type: quot]:
wenzelm@20811
   181
  obtains (rep) a where "A = \<lfloor>a\<rfloor>"
wenzelm@20811
   182
  using quot_rep by blast
wenzelm@10352
   183
wenzelm@10352
   184
wenzelm@10352
   185
subsection {* Equality on quotients *}
wenzelm@10352
   186
wenzelm@10352
   187
text {*
wenzelm@12338
   188
  Equality of canonical quotient elements corresponds to the original
wenzelm@12338
   189
  relation as follows.
wenzelm@10352
   190
*}
wenzelm@10352
   191
wenzelm@10352
   192
theorem eqv_class_eqI [intro]: "a \<sim> b ==> \<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10352
   193
proof -
wenzelm@10352
   194
  assume ab: "a \<sim> b"
wenzelm@10352
   195
  have "{x. a \<sim> x} = {x. b \<sim> x}"
wenzelm@10352
   196
  proof (rule Collect_cong)
wenzelm@10352
   197
    fix x show "(a \<sim> x) = (b \<sim> x)"
wenzelm@10352
   198
    proof
wenzelm@10352
   199
      from ab have "b \<sim> a" ..
wenzelm@10352
   200
      also assume "a \<sim> x"
wenzelm@10352
   201
      finally show "b \<sim> x" .
wenzelm@10352
   202
    next
wenzelm@10352
   203
      note ab
wenzelm@10352
   204
      also assume "b \<sim> x"
wenzelm@10352
   205
      finally show "a \<sim> x" .
wenzelm@10352
   206
    qed
wenzelm@10352
   207
  qed
wenzelm@20811
   208
  then show ?thesis by (simp only: eqv_class_def)
wenzelm@10352
   209
qed
wenzelm@10352
   210
wenzelm@10352
   211
theorem eqv_class_eqD' [dest?]: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor> ==> a \<in> domain ==> a \<sim> b"
wenzelm@10352
   212
proof (unfold eqv_class_def)
wenzelm@10352
   213
  assume "Abs_quot {x. a \<sim> x} = Abs_quot {x. b \<sim> x}"
wenzelm@20811
   214
  then have "{x. a \<sim> x} = {x. b \<sim> x}" by (simp only: Abs_quot_inject quotI)
wenzelm@20811
   215
  moreover assume "a \<in> domain" then have "a \<sim> a" ..
wenzelm@10352
   216
  ultimately have "a \<in> {x. b \<sim> x}" by blast
wenzelm@20811
   217
  then have "b \<sim> a" by blast
wenzelm@20811
   218
  then show "a \<sim> b" ..
wenzelm@10352
   219
qed
wenzelm@10352
   220
wenzelm@10352
   221
theorem eqv_class_eqD [dest?]: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor> ==> a \<sim> (b::'a::equiv)"
wenzelm@10352
   222
proof (rule eqv_class_eqD')
wenzelm@10352
   223
  show "a \<in> domain" ..
wenzelm@10352
   224
qed
wenzelm@10352
   225
wenzelm@10352
   226
lemma eqv_class_eq' [simp]: "a \<in> domain ==> (\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>) = (a \<sim> b)"
wenzelm@28616
   227
  using eqv_class_eqI eqv_class_eqD' by (blast del: eqv_refl)
wenzelm@10352
   228
wenzelm@10352
   229
lemma eqv_class_eq [simp]: "(\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>) = (a \<sim> (b::'a::equiv))"
wenzelm@20811
   230
  using eqv_class_eqI eqv_class_eqD by blast
wenzelm@10352
   231
wenzelm@10352
   232
wenzelm@10352
   233
subsection {* Picking representing elements *}
wenzelm@10352
   234
wenzelm@19736
   235
definition
wenzelm@21404
   236
  pick :: "'a::partial_equiv quot => 'a" where
wenzelm@19736
   237
  "pick A = (SOME a. A = \<lfloor>a\<rfloor>)"
wenzelm@10352
   238
wenzelm@10352
   239
theorem pick_eqv' [intro?, simp]: "a \<in> domain ==> pick \<lfloor>a\<rfloor> \<sim> a"
wenzelm@10352
   240
proof (unfold pick_def)
wenzelm@10352
   241
  assume a: "a \<in> domain"
wenzelm@10352
   242
  show "(SOME x. \<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>) \<sim> a"
wenzelm@10352
   243
  proof (rule someI2)
wenzelm@10352
   244
    show "\<lfloor>a\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10352
   245
    fix x assume "\<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>"
wenzelm@23373
   246
    from this and a have "a \<sim> x" ..
wenzelm@20811
   247
    then show "x \<sim> a" ..
wenzelm@10352
   248
  qed
wenzelm@10352
   249
qed
wenzelm@10352
   250
wenzelm@10352
   251
theorem pick_eqv [intro, simp]: "pick \<lfloor>a\<rfloor> \<sim> (a::'a::equiv)"
wenzelm@10352
   252
proof (rule pick_eqv')
wenzelm@10352
   253
  show "a \<in> domain" ..
wenzelm@10352
   254
qed
wenzelm@10352
   255
wenzelm@10352
   256
theorem pick_inverse: "\<lfloor>pick A\<rfloor> = (A::'a::equiv quot)"
wenzelm@10352
   257
proof (cases A)
wenzelm@10352
   258
  fix a assume a: "A = \<lfloor>a\<rfloor>"
wenzelm@20811
   259
  then have "pick A \<sim> a" by simp
wenzelm@20811
   260
  then have "\<lfloor>pick A\<rfloor> = \<lfloor>a\<rfloor>" by simp
wenzelm@10352
   261
  with a show ?thesis by simp
wenzelm@10352
   262
qed
wenzelm@10352
   263
wenzelm@10352
   264
end