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