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