src/HOL/Quotient.thy
author kuncar
Fri Mar 23 18:23:47 2012 +0100 (2012-03-23)
changeset 47105 e64ffc96a49f
parent 47096 3ea48c19673e
child 47308 9caab698dbe4
child 47434 b75ce48a93ee
permissions -rw-r--r--
hide invariant constant
wenzelm@41959
     1
(*  Title:      HOL/Quotient.thy
kaliszyk@35222
     2
    Author:     Cezary Kaliszyk and Christian Urban
kaliszyk@35222
     3
*)
kaliszyk@35222
     4
huffman@35294
     5
header {* Definition of Quotient Types *}
huffman@35294
     6
kaliszyk@35222
     7
theory Quotient
haftmann@40466
     8
imports Plain Hilbert_Choice Equiv_Relations
wenzelm@46950
     9
keywords
wenzelm@46950
    10
  "print_quotmaps" "print_quotients" "print_quotconsts" :: diag and
wenzelm@46950
    11
  "quotient_type" :: thy_goal and "/" and
kuncar@47096
    12
  "setup_lifting" :: thy_decl and
kuncar@47091
    13
  "quotient_definition" :: thy_goal
kaliszyk@35222
    14
uses
wenzelm@37986
    15
  ("Tools/Quotient/quotient_info.ML")
wenzelm@45680
    16
  ("Tools/Quotient/quotient_type.ML")
wenzelm@37986
    17
  ("Tools/Quotient/quotient_def.ML")
wenzelm@37986
    18
  ("Tools/Quotient/quotient_term.ML")
wenzelm@37986
    19
  ("Tools/Quotient/quotient_tacs.ML")
kaliszyk@35222
    20
begin
kaliszyk@35222
    21
kaliszyk@35222
    22
text {*
haftmann@45961
    23
  An aside: contravariant functorial structure of sets.
haftmann@45961
    24
*}
haftmann@45961
    25
haftmann@45961
    26
enriched_type vimage
haftmann@45961
    27
  by (simp_all add: fun_eq_iff vimage_compose)
haftmann@45961
    28
haftmann@45961
    29
text {*
kaliszyk@35222
    30
  Basic definition for equivalence relations
kaliszyk@35222
    31
  that are represented by predicates.
kaliszyk@35222
    32
*}
kaliszyk@35222
    33
kaliszyk@35222
    34
text {* Composition of Relations *}
kaliszyk@35222
    35
kaliszyk@35222
    36
abbreviation
haftmann@40818
    37
  rel_conj :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool" (infixr "OOO" 75)
kaliszyk@35222
    38
where
kaliszyk@35222
    39
  "r1 OOO r2 \<equiv> r1 OO r2 OO r1"
kaliszyk@35222
    40
kaliszyk@35222
    41
lemma eq_comp_r:
kaliszyk@35222
    42
  shows "((op =) OOO R) = R"
nipkow@39302
    43
  by (auto simp add: fun_eq_iff)
kaliszyk@35222
    44
huffman@35294
    45
subsection {* Respects predicate *}
kaliszyk@35222
    46
kaliszyk@35222
    47
definition
haftmann@40466
    48
  Respects :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set"
kaliszyk@35222
    49
where
haftmann@44553
    50
  "Respects R = {x. R x x}"
kaliszyk@35222
    51
kaliszyk@35222
    52
lemma in_respects:
haftmann@40466
    53
  shows "x \<in> Respects R \<longleftrightarrow> R x x"
haftmann@44553
    54
  unfolding Respects_def by simp
kaliszyk@35222
    55
huffman@35294
    56
subsection {* Function map and function relation *}
kaliszyk@35222
    57
haftmann@40602
    58
notation map_fun (infixr "--->" 55)
haftmann@40466
    59
haftmann@40602
    60
lemma map_fun_id:
haftmann@40466
    61
  "(id ---> id) = id"
haftmann@40602
    62
  by (simp add: fun_eq_iff)
kaliszyk@35222
    63
kaliszyk@35222
    64
definition
haftmann@40615
    65
  fun_rel :: "('a \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('c \<Rightarrow> 'd) \<Rightarrow> bool" (infixr "===>" 55)
kaliszyk@35222
    66
where
haftmann@40814
    67
  "fun_rel R1 R2 = (\<lambda>f g. \<forall>x y. R1 x y \<longrightarrow> R2 (f x) (g y))"
kaliszyk@35222
    68
kaliszyk@36276
    69
lemma fun_relI [intro]:
haftmann@40814
    70
  assumes "\<And>x y. R1 x y \<Longrightarrow> R2 (f x) (g y)"
haftmann@40814
    71
  shows "(R1 ===> R2) f g"
kaliszyk@36276
    72
  using assms by (simp add: fun_rel_def)
kaliszyk@35222
    73
haftmann@40466
    74
lemma fun_relE:
haftmann@40814
    75
  assumes "(R1 ===> R2) f g" and "R1 x y"
haftmann@40814
    76
  obtains "R2 (f x) (g y)"
haftmann@40466
    77
  using assms by (simp add: fun_rel_def)
kaliszyk@35222
    78
kaliszyk@35222
    79
lemma fun_rel_eq:
kaliszyk@35222
    80
  shows "((op =) ===> (op =)) = (op =)"
haftmann@40466
    81
  by (auto simp add: fun_eq_iff elim: fun_relE)
kaliszyk@35222
    82
kuncar@47091
    83
lemma fun_rel_eq_rel:
kuncar@47091
    84
  shows "((op =) ===> R) = (\<lambda>f g. \<forall>x. R (f x) (g x))"
kuncar@47091
    85
  by (simp add: fun_rel_def)
kuncar@47091
    86
kaliszyk@44413
    87
subsection {* set map (vimage) and set relation *}
kaliszyk@44413
    88
kaliszyk@44413
    89
definition "set_rel R xs ys \<equiv> \<forall>x y. R x y \<longrightarrow> x \<in> xs \<longleftrightarrow> y \<in> ys"
kaliszyk@44413
    90
kaliszyk@44413
    91
lemma vimage_id:
kaliszyk@44413
    92
  "vimage id = id"
kaliszyk@44413
    93
  unfolding vimage_def fun_eq_iff by auto
kaliszyk@44413
    94
kaliszyk@44413
    95
lemma set_rel_eq:
kaliszyk@44413
    96
  "set_rel op = = op ="
kaliszyk@44413
    97
  by (subst fun_eq_iff, subst fun_eq_iff) (simp add: set_eq_iff set_rel_def)
kaliszyk@44413
    98
kaliszyk@44413
    99
lemma set_rel_equivp:
kaliszyk@44413
   100
  assumes e: "equivp R"
kaliszyk@44413
   101
  shows "set_rel R xs ys \<longleftrightarrow> xs = ys \<and> (\<forall>x y. x \<in> xs \<longrightarrow> R x y \<longrightarrow> y \<in> xs)"
kaliszyk@44413
   102
  unfolding set_rel_def
kaliszyk@44413
   103
  using equivp_reflp[OF e]
huffman@44921
   104
  by auto (metis, metis equivp_symp[OF e])
kaliszyk@35222
   105
huffman@35294
   106
subsection {* Quotient Predicate *}
kaliszyk@35222
   107
kaliszyk@35222
   108
definition
haftmann@40814
   109
  "Quotient R Abs Rep \<longleftrightarrow>
haftmann@40814
   110
     (\<forall>a. Abs (Rep a) = a) \<and> (\<forall>a. R (Rep a) (Rep a)) \<and>
haftmann@40818
   111
     (\<forall>r s. R r s \<longleftrightarrow> R r r \<and> R s s \<and> Abs r = Abs s)"
haftmann@40818
   112
haftmann@40818
   113
lemma QuotientI:
haftmann@40818
   114
  assumes "\<And>a. Abs (Rep a) = a"
haftmann@40818
   115
    and "\<And>a. R (Rep a) (Rep a)"
haftmann@40818
   116
    and "\<And>r s. R r s \<longleftrightarrow> R r r \<and> R s s \<and> Abs r = Abs s"
haftmann@40818
   117
  shows "Quotient R Abs Rep"
haftmann@40818
   118
  using assms unfolding Quotient_def by blast
kaliszyk@35222
   119
kaliszyk@35222
   120
lemma Quotient_abs_rep:
haftmann@40814
   121
  assumes a: "Quotient R Abs Rep"
kaliszyk@35222
   122
  shows "Abs (Rep a) = a"
kaliszyk@35222
   123
  using a
kaliszyk@35222
   124
  unfolding Quotient_def
kaliszyk@35222
   125
  by simp
kaliszyk@35222
   126
kaliszyk@35222
   127
lemma Quotient_rep_reflp:
haftmann@40814
   128
  assumes a: "Quotient R Abs Rep"
haftmann@40814
   129
  shows "R (Rep a) (Rep a)"
kaliszyk@35222
   130
  using a
kaliszyk@35222
   131
  unfolding Quotient_def
kaliszyk@35222
   132
  by blast
kaliszyk@35222
   133
kaliszyk@35222
   134
lemma Quotient_rel:
haftmann@40814
   135
  assumes a: "Quotient R Abs Rep"
haftmann@40818
   136
  shows "R r r \<and> R s s \<and> Abs r = Abs s \<longleftrightarrow> R r s" -- {* orientation does not loop on rewriting *}
kaliszyk@35222
   137
  using a
kaliszyk@35222
   138
  unfolding Quotient_def
kaliszyk@35222
   139
  by blast
kaliszyk@35222
   140
kuncar@47096
   141
lemma Quotient_refl1: 
kuncar@47096
   142
  assumes a: "Quotient R Abs Rep" 
kuncar@47096
   143
  shows "R r s \<Longrightarrow> R r r"
kuncar@47096
   144
  using a unfolding Quotient_def 
kuncar@47096
   145
  by fast
kuncar@47096
   146
kuncar@47096
   147
lemma Quotient_refl2: 
kuncar@47096
   148
  assumes a: "Quotient R Abs Rep" 
kuncar@47096
   149
  shows "R r s \<Longrightarrow> R s s"
kuncar@47096
   150
  using a unfolding Quotient_def 
kuncar@47096
   151
  by fast
kuncar@47096
   152
kaliszyk@35222
   153
lemma Quotient_rel_rep:
kaliszyk@35222
   154
  assumes a: "Quotient R Abs Rep"
haftmann@40818
   155
  shows "R (Rep a) (Rep b) \<longleftrightarrow> a = b"
kaliszyk@35222
   156
  using a
kaliszyk@35222
   157
  unfolding Quotient_def
kaliszyk@35222
   158
  by metis
kaliszyk@35222
   159
kaliszyk@35222
   160
lemma Quotient_rep_abs:
kaliszyk@35222
   161
  assumes a: "Quotient R Abs Rep"
kaliszyk@35222
   162
  shows "R r r \<Longrightarrow> R (Rep (Abs r)) r"
kaliszyk@35222
   163
  using a unfolding Quotient_def
kaliszyk@35222
   164
  by blast
kaliszyk@35222
   165
kaliszyk@35222
   166
lemma Quotient_rel_abs:
haftmann@40814
   167
  assumes a: "Quotient R Abs Rep"
haftmann@40814
   168
  shows "R r s \<Longrightarrow> Abs r = Abs s"
kaliszyk@35222
   169
  using a unfolding Quotient_def
kaliszyk@35222
   170
  by blast
kaliszyk@35222
   171
kaliszyk@35222
   172
lemma Quotient_symp:
haftmann@40814
   173
  assumes a: "Quotient R Abs Rep"
haftmann@40814
   174
  shows "symp R"
haftmann@40814
   175
  using a unfolding Quotient_def using sympI by metis
kaliszyk@35222
   176
kaliszyk@35222
   177
lemma Quotient_transp:
haftmann@40814
   178
  assumes a: "Quotient R Abs Rep"
haftmann@40814
   179
  shows "transp R"
haftmann@40814
   180
  using a unfolding Quotient_def using transpI by metis
kaliszyk@35222
   181
kaliszyk@35222
   182
lemma identity_quotient:
kaliszyk@35222
   183
  shows "Quotient (op =) id id"
kaliszyk@35222
   184
  unfolding Quotient_def id_def
kaliszyk@35222
   185
  by blast
kaliszyk@35222
   186
kaliszyk@35222
   187
lemma fun_quotient:
kaliszyk@35222
   188
  assumes q1: "Quotient R1 abs1 rep1"
kaliszyk@35222
   189
  and     q2: "Quotient R2 abs2 rep2"
kaliszyk@35222
   190
  shows "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
kaliszyk@35222
   191
proof -
haftmann@40466
   192
  have "\<And>a. (rep1 ---> abs2) ((abs1 ---> rep2) a) = a"
haftmann@40466
   193
    using q1 q2 by (simp add: Quotient_def fun_eq_iff)
kaliszyk@35222
   194
  moreover
haftmann@40466
   195
  have "\<And>a. (R1 ===> R2) ((abs1 ---> rep2) a) ((abs1 ---> rep2) a)"
haftmann@40466
   196
    by (rule fun_relI)
haftmann@40466
   197
      (insert q1 q2 Quotient_rel_abs [of R1 abs1 rep1] Quotient_rel_rep [of R2 abs2 rep2],
haftmann@40466
   198
        simp (no_asm) add: Quotient_def, simp)
kaliszyk@35222
   199
  moreover
haftmann@40466
   200
  have "\<And>r s. (R1 ===> R2) r s = ((R1 ===> R2) r r \<and> (R1 ===> R2) s s \<and>
kaliszyk@35222
   201
        (rep1 ---> abs2) r  = (rep1 ---> abs2) s)"
haftmann@40466
   202
    apply(auto simp add: fun_rel_def fun_eq_iff)
kaliszyk@35222
   203
    using q1 q2 unfolding Quotient_def
kaliszyk@35222
   204
    apply(metis)
kaliszyk@35222
   205
    using q1 q2 unfolding Quotient_def
kaliszyk@35222
   206
    apply(metis)
kaliszyk@35222
   207
    using q1 q2 unfolding Quotient_def
kaliszyk@35222
   208
    apply(metis)
kaliszyk@35222
   209
    using q1 q2 unfolding Quotient_def
kaliszyk@35222
   210
    apply(metis)
kaliszyk@35222
   211
    done
kaliszyk@35222
   212
  ultimately
kaliszyk@35222
   213
  show "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
kaliszyk@35222
   214
    unfolding Quotient_def by blast
kaliszyk@35222
   215
qed
kaliszyk@35222
   216
kaliszyk@35222
   217
lemma abs_o_rep:
kaliszyk@35222
   218
  assumes a: "Quotient R Abs Rep"
kaliszyk@35222
   219
  shows "Abs o Rep = id"
nipkow@39302
   220
  unfolding fun_eq_iff
kaliszyk@35222
   221
  by (simp add: Quotient_abs_rep[OF a])
kaliszyk@35222
   222
kaliszyk@35222
   223
lemma equals_rsp:
kaliszyk@35222
   224
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   225
  and     a: "R xa xb" "R ya yb"
kaliszyk@35222
   226
  shows "R xa ya = R xb yb"
kaliszyk@35222
   227
  using a Quotient_symp[OF q] Quotient_transp[OF q]
haftmann@40814
   228
  by (blast elim: sympE transpE)
kaliszyk@35222
   229
kaliszyk@35222
   230
lemma lambda_prs:
kaliszyk@35222
   231
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   232
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@35222
   233
  shows "(Rep1 ---> Abs2) (\<lambda>x. Rep2 (f (Abs1 x))) = (\<lambda>x. f x)"
nipkow@39302
   234
  unfolding fun_eq_iff
kaliszyk@35222
   235
  using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
haftmann@40814
   236
  by simp
kaliszyk@35222
   237
kaliszyk@35222
   238
lemma lambda_prs1:
kaliszyk@35222
   239
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   240
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@35222
   241
  shows "(Rep1 ---> Abs2) (\<lambda>x. (Abs1 ---> Rep2) f x) = (\<lambda>x. f x)"
nipkow@39302
   242
  unfolding fun_eq_iff
kaliszyk@35222
   243
  using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
haftmann@40814
   244
  by simp
kaliszyk@35222
   245
kaliszyk@35222
   246
lemma rep_abs_rsp:
kaliszyk@35222
   247
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   248
  and     a: "R x1 x2"
kaliszyk@35222
   249
  shows "R x1 (Rep (Abs x2))"
kaliszyk@35222
   250
  using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
kaliszyk@35222
   251
  by metis
kaliszyk@35222
   252
kaliszyk@35222
   253
lemma rep_abs_rsp_left:
kaliszyk@35222
   254
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   255
  and     a: "R x1 x2"
kaliszyk@35222
   256
  shows "R (Rep (Abs x1)) x2"
kaliszyk@35222
   257
  using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
kaliszyk@35222
   258
  by metis
kaliszyk@35222
   259
kaliszyk@35222
   260
text{*
kaliszyk@35222
   261
  In the following theorem R1 can be instantiated with anything,
kaliszyk@35222
   262
  but we know some of the types of the Rep and Abs functions;
kaliszyk@35222
   263
  so by solving Quotient assumptions we can get a unique R1 that
kaliszyk@35236
   264
  will be provable; which is why we need to use @{text apply_rsp} and
kaliszyk@35222
   265
  not the primed version *}
kaliszyk@35222
   266
kaliszyk@35222
   267
lemma apply_rsp:
kaliszyk@35222
   268
  fixes f g::"'a \<Rightarrow> 'c"
kaliszyk@35222
   269
  assumes q: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   270
  and     a: "(R1 ===> R2) f g" "R1 x y"
kaliszyk@35222
   271
  shows "R2 (f x) (g y)"
haftmann@40466
   272
  using a by (auto elim: fun_relE)
kaliszyk@35222
   273
kaliszyk@35222
   274
lemma apply_rsp':
kaliszyk@35222
   275
  assumes a: "(R1 ===> R2) f g" "R1 x y"
kaliszyk@35222
   276
  shows "R2 (f x) (g y)"
haftmann@40466
   277
  using a by (auto elim: fun_relE)
kaliszyk@35222
   278
kuncar@47096
   279
lemma apply_rsp'':
kuncar@47096
   280
  assumes "Quotient R Abs Rep"
kuncar@47096
   281
  and "(R ===> S) f f"
kuncar@47096
   282
  shows "S (f (Rep x)) (f (Rep x))"
kuncar@47096
   283
proof -
kuncar@47096
   284
  from assms(1) have "R (Rep x) (Rep x)" by (rule Quotient_rep_reflp)
kuncar@47096
   285
  then show ?thesis using assms(2) by (auto intro: apply_rsp')
kuncar@47096
   286
qed
kuncar@47096
   287
huffman@35294
   288
subsection {* lemmas for regularisation of ball and bex *}
kaliszyk@35222
   289
kaliszyk@35222
   290
lemma ball_reg_eqv:
kaliszyk@35222
   291
  fixes P :: "'a \<Rightarrow> bool"
kaliszyk@35222
   292
  assumes a: "equivp R"
kaliszyk@35222
   293
  shows "Ball (Respects R) P = (All P)"
kaliszyk@35222
   294
  using a
kaliszyk@35222
   295
  unfolding equivp_def
kaliszyk@35222
   296
  by (auto simp add: in_respects)
kaliszyk@35222
   297
kaliszyk@35222
   298
lemma bex_reg_eqv:
kaliszyk@35222
   299
  fixes P :: "'a \<Rightarrow> bool"
kaliszyk@35222
   300
  assumes a: "equivp R"
kaliszyk@35222
   301
  shows "Bex (Respects R) P = (Ex P)"
kaliszyk@35222
   302
  using a
kaliszyk@35222
   303
  unfolding equivp_def
kaliszyk@35222
   304
  by (auto simp add: in_respects)
kaliszyk@35222
   305
kaliszyk@35222
   306
lemma ball_reg_right:
haftmann@44553
   307
  assumes a: "\<And>x. x \<in> R \<Longrightarrow> P x \<longrightarrow> Q x"
kaliszyk@35222
   308
  shows "All P \<longrightarrow> Ball R Q"
huffman@44921
   309
  using a by fast
kaliszyk@35222
   310
kaliszyk@35222
   311
lemma bex_reg_left:
haftmann@44553
   312
  assumes a: "\<And>x. x \<in> R \<Longrightarrow> Q x \<longrightarrow> P x"
kaliszyk@35222
   313
  shows "Bex R Q \<longrightarrow> Ex P"
huffman@44921
   314
  using a by fast
kaliszyk@35222
   315
kaliszyk@35222
   316
lemma ball_reg_left:
kaliszyk@35222
   317
  assumes a: "equivp R"
kaliszyk@35222
   318
  shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ball (Respects R) Q \<longrightarrow> All P"
kaliszyk@35222
   319
  using a by (metis equivp_reflp in_respects)
kaliszyk@35222
   320
kaliszyk@35222
   321
lemma bex_reg_right:
kaliszyk@35222
   322
  assumes a: "equivp R"
kaliszyk@35222
   323
  shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ex Q \<longrightarrow> Bex (Respects R) P"
kaliszyk@35222
   324
  using a by (metis equivp_reflp in_respects)
kaliszyk@35222
   325
kaliszyk@35222
   326
lemma ball_reg_eqv_range:
kaliszyk@35222
   327
  fixes P::"'a \<Rightarrow> bool"
kaliszyk@35222
   328
  and x::"'a"
kaliszyk@35222
   329
  assumes a: "equivp R2"
kaliszyk@35222
   330
  shows   "(Ball (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = All (\<lambda>f. P (f x)))"
kaliszyk@35222
   331
  apply(rule iffI)
kaliszyk@35222
   332
  apply(rule allI)
kaliszyk@35222
   333
  apply(drule_tac x="\<lambda>y. f x" in bspec)
haftmann@40466
   334
  apply(simp add: in_respects fun_rel_def)
kaliszyk@35222
   335
  apply(rule impI)
kaliszyk@35222
   336
  using a equivp_reflp_symp_transp[of "R2"]
haftmann@40814
   337
  apply (auto elim: equivpE reflpE)
kaliszyk@35222
   338
  done
kaliszyk@35222
   339
kaliszyk@35222
   340
lemma bex_reg_eqv_range:
kaliszyk@35222
   341
  assumes a: "equivp R2"
kaliszyk@35222
   342
  shows   "(Bex (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = Ex (\<lambda>f. P (f x)))"
kaliszyk@35222
   343
  apply(auto)
kaliszyk@35222
   344
  apply(rule_tac x="\<lambda>y. f x" in bexI)
kaliszyk@35222
   345
  apply(simp)
haftmann@40466
   346
  apply(simp add: Respects_def in_respects fun_rel_def)
kaliszyk@35222
   347
  apply(rule impI)
kaliszyk@35222
   348
  using a equivp_reflp_symp_transp[of "R2"]
haftmann@40814
   349
  apply (auto elim: equivpE reflpE)
kaliszyk@35222
   350
  done
kaliszyk@35222
   351
kaliszyk@35222
   352
(* Next four lemmas are unused *)
kaliszyk@35222
   353
lemma all_reg:
kaliszyk@35222
   354
  assumes a: "!x :: 'a. (P x --> Q x)"
kaliszyk@35222
   355
  and     b: "All P"
kaliszyk@35222
   356
  shows "All Q"
huffman@44921
   357
  using a b by fast
kaliszyk@35222
   358
kaliszyk@35222
   359
lemma ex_reg:
kaliszyk@35222
   360
  assumes a: "!x :: 'a. (P x --> Q x)"
kaliszyk@35222
   361
  and     b: "Ex P"
kaliszyk@35222
   362
  shows "Ex Q"
huffman@44921
   363
  using a b by fast
kaliszyk@35222
   364
kaliszyk@35222
   365
lemma ball_reg:
haftmann@44553
   366
  assumes a: "!x :: 'a. (x \<in> R --> P x --> Q x)"
kaliszyk@35222
   367
  and     b: "Ball R P"
kaliszyk@35222
   368
  shows "Ball R Q"
huffman@44921
   369
  using a b by fast
kaliszyk@35222
   370
kaliszyk@35222
   371
lemma bex_reg:
haftmann@44553
   372
  assumes a: "!x :: 'a. (x \<in> R --> P x --> Q x)"
kaliszyk@35222
   373
  and     b: "Bex R P"
kaliszyk@35222
   374
  shows "Bex R Q"
huffman@44921
   375
  using a b by fast
kaliszyk@35222
   376
kaliszyk@35222
   377
kaliszyk@35222
   378
lemma ball_all_comm:
kaliszyk@35222
   379
  assumes "\<And>y. (\<forall>x\<in>P. A x y) \<longrightarrow> (\<forall>x. B x y)"
kaliszyk@35222
   380
  shows "(\<forall>x\<in>P. \<forall>y. A x y) \<longrightarrow> (\<forall>x. \<forall>y. B x y)"
kaliszyk@35222
   381
  using assms by auto
kaliszyk@35222
   382
kaliszyk@35222
   383
lemma bex_ex_comm:
kaliszyk@35222
   384
  assumes "(\<exists>y. \<exists>x. A x y) \<longrightarrow> (\<exists>y. \<exists>x\<in>P. B x y)"
kaliszyk@35222
   385
  shows "(\<exists>x. \<exists>y. A x y) \<longrightarrow> (\<exists>x\<in>P. \<exists>y. B x y)"
kaliszyk@35222
   386
  using assms by auto
kaliszyk@35222
   387
huffman@35294
   388
subsection {* Bounded abstraction *}
kaliszyk@35222
   389
kaliszyk@35222
   390
definition
haftmann@40466
   391
  Babs :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
kaliszyk@35222
   392
where
kaliszyk@35222
   393
  "x \<in> p \<Longrightarrow> Babs p m x = m x"
kaliszyk@35222
   394
kaliszyk@35222
   395
lemma babs_rsp:
kaliszyk@35222
   396
  assumes q: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   397
  and     a: "(R1 ===> R2) f g"
kaliszyk@35222
   398
  shows      "(R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)"
haftmann@40466
   399
  apply (auto simp add: Babs_def in_respects fun_rel_def)
kaliszyk@35222
   400
  apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
haftmann@40466
   401
  using a apply (simp add: Babs_def fun_rel_def)
haftmann@40466
   402
  apply (simp add: in_respects fun_rel_def)
kaliszyk@35222
   403
  using Quotient_rel[OF q]
kaliszyk@35222
   404
  by metis
kaliszyk@35222
   405
kaliszyk@35222
   406
lemma babs_prs:
kaliszyk@35222
   407
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   408
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@35222
   409
  shows "((Rep1 ---> Abs2) (Babs (Respects R1) ((Abs1 ---> Rep2) f))) = f"
kaliszyk@35222
   410
  apply (rule ext)
haftmann@40466
   411
  apply (simp add:)
kaliszyk@35222
   412
  apply (subgoal_tac "Rep1 x \<in> Respects R1")
kaliszyk@35222
   413
  apply (simp add: Babs_def Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2])
kaliszyk@35222
   414
  apply (simp add: in_respects Quotient_rel_rep[OF q1])
kaliszyk@35222
   415
  done
kaliszyk@35222
   416
kaliszyk@35222
   417
lemma babs_simp:
kaliszyk@35222
   418
  assumes q: "Quotient R1 Abs Rep"
kaliszyk@35222
   419
  shows "((R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)) = ((R1 ===> R2) f g)"
kaliszyk@35222
   420
  apply(rule iffI)
kaliszyk@35222
   421
  apply(simp_all only: babs_rsp[OF q])
haftmann@40466
   422
  apply(auto simp add: Babs_def fun_rel_def)
kaliszyk@35222
   423
  apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
kaliszyk@35222
   424
  apply(metis Babs_def)
kaliszyk@35222
   425
  apply (simp add: in_respects)
kaliszyk@35222
   426
  using Quotient_rel[OF q]
kaliszyk@35222
   427
  by metis
kaliszyk@35222
   428
kaliszyk@35222
   429
(* If a user proves that a particular functional relation
kaliszyk@35222
   430
   is an equivalence this may be useful in regularising *)
kaliszyk@35222
   431
lemma babs_reg_eqv:
kaliszyk@35222
   432
  shows "equivp R \<Longrightarrow> Babs (Respects R) P = P"
nipkow@39302
   433
  by (simp add: fun_eq_iff Babs_def in_respects equivp_reflp)
kaliszyk@35222
   434
kaliszyk@35222
   435
kaliszyk@35222
   436
(* 3 lemmas needed for proving repabs_inj *)
kaliszyk@35222
   437
lemma ball_rsp:
kaliszyk@35222
   438
  assumes a: "(R ===> (op =)) f g"
kaliszyk@35222
   439
  shows "Ball (Respects R) f = Ball (Respects R) g"
haftmann@40466
   440
  using a by (auto simp add: Ball_def in_respects elim: fun_relE)
kaliszyk@35222
   441
kaliszyk@35222
   442
lemma bex_rsp:
kaliszyk@35222
   443
  assumes a: "(R ===> (op =)) f g"
kaliszyk@35222
   444
  shows "(Bex (Respects R) f = Bex (Respects R) g)"
haftmann@40466
   445
  using a by (auto simp add: Bex_def in_respects elim: fun_relE)
kaliszyk@35222
   446
kaliszyk@35222
   447
lemma bex1_rsp:
kaliszyk@35222
   448
  assumes a: "(R ===> (op =)) f g"
kaliszyk@35222
   449
  shows "Ex1 (\<lambda>x. x \<in> Respects R \<and> f x) = Ex1 (\<lambda>x. x \<in> Respects R \<and> g x)"
haftmann@40466
   450
  using a by (auto elim: fun_relE simp add: Ex1_def in_respects) 
kaliszyk@35222
   451
kaliszyk@35222
   452
(* 2 lemmas needed for cleaning of quantifiers *)
kaliszyk@35222
   453
lemma all_prs:
kaliszyk@35222
   454
  assumes a: "Quotient R absf repf"
kaliszyk@35222
   455
  shows "Ball (Respects R) ((absf ---> id) f) = All f"
haftmann@40602
   456
  using a unfolding Quotient_def Ball_def in_respects id_apply comp_def map_fun_def
kaliszyk@35222
   457
  by metis
kaliszyk@35222
   458
kaliszyk@35222
   459
lemma ex_prs:
kaliszyk@35222
   460
  assumes a: "Quotient R absf repf"
kaliszyk@35222
   461
  shows "Bex (Respects R) ((absf ---> id) f) = Ex f"
haftmann@40602
   462
  using a unfolding Quotient_def Bex_def in_respects id_apply comp_def map_fun_def
kaliszyk@35222
   463
  by metis
kaliszyk@35222
   464
huffman@35294
   465
subsection {* @{text Bex1_rel} quantifier *}
kaliszyk@35222
   466
kaliszyk@35222
   467
definition
kaliszyk@35222
   468
  Bex1_rel :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
kaliszyk@35222
   469
where
kaliszyk@35222
   470
  "Bex1_rel R P \<longleftrightarrow> (\<exists>x \<in> Respects R. P x) \<and> (\<forall>x \<in> Respects R. \<forall>y \<in> Respects R. ((P x \<and> P y) \<longrightarrow> (R x y)))"
kaliszyk@35222
   471
kaliszyk@35222
   472
lemma bex1_rel_aux:
kaliszyk@35222
   473
  "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R x\<rbrakk> \<Longrightarrow> Bex1_rel R y"
kaliszyk@35222
   474
  unfolding Bex1_rel_def
kaliszyk@35222
   475
  apply (erule conjE)+
kaliszyk@35222
   476
  apply (erule bexE)
kaliszyk@35222
   477
  apply rule
kaliszyk@35222
   478
  apply (rule_tac x="xa" in bexI)
kaliszyk@35222
   479
  apply metis
kaliszyk@35222
   480
  apply metis
kaliszyk@35222
   481
  apply rule+
kaliszyk@35222
   482
  apply (erule_tac x="xaa" in ballE)
kaliszyk@35222
   483
  prefer 2
kaliszyk@35222
   484
  apply (metis)
kaliszyk@35222
   485
  apply (erule_tac x="ya" in ballE)
kaliszyk@35222
   486
  prefer 2
kaliszyk@35222
   487
  apply (metis)
kaliszyk@35222
   488
  apply (metis in_respects)
kaliszyk@35222
   489
  done
kaliszyk@35222
   490
kaliszyk@35222
   491
lemma bex1_rel_aux2:
kaliszyk@35222
   492
  "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R y\<rbrakk> \<Longrightarrow> Bex1_rel R x"
kaliszyk@35222
   493
  unfolding Bex1_rel_def
kaliszyk@35222
   494
  apply (erule conjE)+
kaliszyk@35222
   495
  apply (erule bexE)
kaliszyk@35222
   496
  apply rule
kaliszyk@35222
   497
  apply (rule_tac x="xa" in bexI)
kaliszyk@35222
   498
  apply metis
kaliszyk@35222
   499
  apply metis
kaliszyk@35222
   500
  apply rule+
kaliszyk@35222
   501
  apply (erule_tac x="xaa" in ballE)
kaliszyk@35222
   502
  prefer 2
kaliszyk@35222
   503
  apply (metis)
kaliszyk@35222
   504
  apply (erule_tac x="ya" in ballE)
kaliszyk@35222
   505
  prefer 2
kaliszyk@35222
   506
  apply (metis)
kaliszyk@35222
   507
  apply (metis in_respects)
kaliszyk@35222
   508
  done
kaliszyk@35222
   509
kaliszyk@35222
   510
lemma bex1_rel_rsp:
kaliszyk@35222
   511
  assumes a: "Quotient R absf repf"
kaliszyk@35222
   512
  shows "((R ===> op =) ===> op =) (Bex1_rel R) (Bex1_rel R)"
haftmann@40466
   513
  apply (simp add: fun_rel_def)
kaliszyk@35222
   514
  apply clarify
kaliszyk@35222
   515
  apply rule
kaliszyk@35222
   516
  apply (simp_all add: bex1_rel_aux bex1_rel_aux2)
kaliszyk@35222
   517
  apply (erule bex1_rel_aux2)
kaliszyk@35222
   518
  apply assumption
kaliszyk@35222
   519
  done
kaliszyk@35222
   520
kaliszyk@35222
   521
kaliszyk@35222
   522
lemma ex1_prs:
kaliszyk@35222
   523
  assumes a: "Quotient R absf repf"
kaliszyk@35222
   524
  shows "((absf ---> id) ---> id) (Bex1_rel R) f = Ex1 f"
haftmann@40466
   525
apply (simp add:)
kaliszyk@35222
   526
apply (subst Bex1_rel_def)
kaliszyk@35222
   527
apply (subst Bex_def)
kaliszyk@35222
   528
apply (subst Ex1_def)
kaliszyk@35222
   529
apply simp
kaliszyk@35222
   530
apply rule
kaliszyk@35222
   531
 apply (erule conjE)+
kaliszyk@35222
   532
 apply (erule_tac exE)
kaliszyk@35222
   533
 apply (erule conjE)
kaliszyk@35222
   534
 apply (subgoal_tac "\<forall>y. R y y \<longrightarrow> f (absf y) \<longrightarrow> R x y")
kaliszyk@35222
   535
  apply (rule_tac x="absf x" in exI)
kaliszyk@35222
   536
  apply (simp)
kaliszyk@35222
   537
  apply rule+
kaliszyk@35222
   538
  using a unfolding Quotient_def
kaliszyk@35222
   539
  apply metis
kaliszyk@35222
   540
 apply rule+
kaliszyk@35222
   541
 apply (erule_tac x="x" in ballE)
kaliszyk@35222
   542
  apply (erule_tac x="y" in ballE)
kaliszyk@35222
   543
   apply simp
kaliszyk@35222
   544
  apply (simp add: in_respects)
kaliszyk@35222
   545
 apply (simp add: in_respects)
kaliszyk@35222
   546
apply (erule_tac exE)
kaliszyk@35222
   547
 apply rule
kaliszyk@35222
   548
 apply (rule_tac x="repf x" in exI)
kaliszyk@35222
   549
 apply (simp only: in_respects)
kaliszyk@35222
   550
  apply rule
kaliszyk@35222
   551
 apply (metis Quotient_rel_rep[OF a])
kaliszyk@35222
   552
using a unfolding Quotient_def apply (simp)
kaliszyk@35222
   553
apply rule+
kaliszyk@35222
   554
using a unfolding Quotient_def in_respects
kaliszyk@35222
   555
apply metis
kaliszyk@35222
   556
done
kaliszyk@35222
   557
kaliszyk@38702
   558
lemma bex1_bexeq_reg:
kaliszyk@38702
   559
  shows "(\<exists>!x\<in>Respects R. P x) \<longrightarrow> (Bex1_rel R (\<lambda>x. P x))"
kaliszyk@35222
   560
  apply (simp add: Ex1_def Bex1_rel_def in_respects)
kaliszyk@35222
   561
  apply clarify
kaliszyk@35222
   562
  apply auto
kaliszyk@35222
   563
  apply (rule bexI)
kaliszyk@35222
   564
  apply assumption
kaliszyk@35222
   565
  apply (simp add: in_respects)
kaliszyk@35222
   566
  apply (simp add: in_respects)
kaliszyk@35222
   567
  apply auto
kaliszyk@35222
   568
  done
kaliszyk@35222
   569
kaliszyk@38702
   570
lemma bex1_bexeq_reg_eqv:
kaliszyk@38702
   571
  assumes a: "equivp R"
kaliszyk@38702
   572
  shows "(\<exists>!x. P x) \<longrightarrow> Bex1_rel R P"
kaliszyk@38702
   573
  using equivp_reflp[OF a]
kaliszyk@38702
   574
  apply (intro impI)
kaliszyk@38702
   575
  apply (elim ex1E)
kaliszyk@38702
   576
  apply (rule mp[OF bex1_bexeq_reg])
kaliszyk@38702
   577
  apply (rule_tac a="x" in ex1I)
kaliszyk@38702
   578
  apply (subst in_respects)
kaliszyk@38702
   579
  apply (rule conjI)
kaliszyk@38702
   580
  apply assumption
kaliszyk@38702
   581
  apply assumption
kaliszyk@38702
   582
  apply clarify
kaliszyk@38702
   583
  apply (erule_tac x="xa" in allE)
kaliszyk@38702
   584
  apply simp
kaliszyk@38702
   585
  done
kaliszyk@38702
   586
huffman@35294
   587
subsection {* Various respects and preserve lemmas *}
kaliszyk@35222
   588
kaliszyk@35222
   589
lemma quot_rel_rsp:
kaliszyk@35222
   590
  assumes a: "Quotient R Abs Rep"
kaliszyk@35222
   591
  shows "(R ===> R ===> op =) R R"
urbanc@38317
   592
  apply(rule fun_relI)+
kaliszyk@35222
   593
  apply(rule equals_rsp[OF a])
kaliszyk@35222
   594
  apply(assumption)+
kaliszyk@35222
   595
  done
kaliszyk@35222
   596
kaliszyk@35222
   597
lemma o_prs:
kaliszyk@35222
   598
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   599
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@35222
   600
  and     q3: "Quotient R3 Abs3 Rep3"
kaliszyk@36215
   601
  shows "((Abs2 ---> Rep3) ---> (Abs1 ---> Rep2) ---> (Rep1 ---> Abs3)) op \<circ> = op \<circ>"
kaliszyk@36215
   602
  and   "(id ---> (Abs1 ---> id) ---> Rep1 ---> id) op \<circ> = op \<circ>"
kaliszyk@35222
   603
  using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2] Quotient_abs_rep[OF q3]
haftmann@40466
   604
  by (simp_all add: fun_eq_iff)
kaliszyk@35222
   605
kaliszyk@35222
   606
lemma o_rsp:
kaliszyk@36215
   607
  "((R2 ===> R3) ===> (R1 ===> R2) ===> (R1 ===> R3)) op \<circ> op \<circ>"
kaliszyk@36215
   608
  "(op = ===> (R1 ===> op =) ===> R1 ===> op =) op \<circ> op \<circ>"
huffman@44921
   609
  by (force elim: fun_relE)+
kaliszyk@35222
   610
kaliszyk@35222
   611
lemma cond_prs:
kaliszyk@35222
   612
  assumes a: "Quotient R absf repf"
kaliszyk@35222
   613
  shows "absf (if a then repf b else repf c) = (if a then b else c)"
kaliszyk@35222
   614
  using a unfolding Quotient_def by auto
kaliszyk@35222
   615
kaliszyk@35222
   616
lemma if_prs:
kaliszyk@35222
   617
  assumes q: "Quotient R Abs Rep"
kaliszyk@36123
   618
  shows "(id ---> Rep ---> Rep ---> Abs) If = If"
kaliszyk@36123
   619
  using Quotient_abs_rep[OF q]
nipkow@39302
   620
  by (auto simp add: fun_eq_iff)
kaliszyk@35222
   621
kaliszyk@35222
   622
lemma if_rsp:
kaliszyk@35222
   623
  assumes q: "Quotient R Abs Rep"
kaliszyk@36123
   624
  shows "(op = ===> R ===> R ===> R) If If"
huffman@44921
   625
  by force
kaliszyk@35222
   626
kaliszyk@35222
   627
lemma let_prs:
kaliszyk@35222
   628
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   629
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@37049
   630
  shows "(Rep2 ---> (Abs2 ---> Rep1) ---> Abs1) Let = Let"
kaliszyk@37049
   631
  using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
nipkow@39302
   632
  by (auto simp add: fun_eq_iff)
kaliszyk@35222
   633
kaliszyk@35222
   634
lemma let_rsp:
kaliszyk@37049
   635
  shows "(R1 ===> (R1 ===> R2) ===> R2) Let Let"
huffman@44921
   636
  by (force elim: fun_relE)
kaliszyk@35222
   637
kaliszyk@39669
   638
lemma id_rsp:
kaliszyk@39669
   639
  shows "(R ===> R) id id"
huffman@44921
   640
  by auto
kaliszyk@39669
   641
kaliszyk@39669
   642
lemma id_prs:
kaliszyk@39669
   643
  assumes a: "Quotient R Abs Rep"
kaliszyk@39669
   644
  shows "(Rep ---> Abs) id = id"
haftmann@40466
   645
  by (simp add: fun_eq_iff Quotient_abs_rep [OF a])
kaliszyk@39669
   646
kaliszyk@39669
   647
kaliszyk@35222
   648
locale quot_type =
kaliszyk@35222
   649
  fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
kaliszyk@44204
   650
  and   Abs :: "'a set \<Rightarrow> 'b"
kaliszyk@44204
   651
  and   Rep :: "'b \<Rightarrow> 'a set"
kaliszyk@37493
   652
  assumes equivp: "part_equivp R"
kaliszyk@44204
   653
  and     rep_prop: "\<And>y. \<exists>x. R x x \<and> Rep y = Collect (R x)"
kaliszyk@35222
   654
  and     rep_inverse: "\<And>x. Abs (Rep x) = x"
kaliszyk@44204
   655
  and     abs_inverse: "\<And>c. (\<exists>x. ((R x x) \<and> (c = Collect (R x)))) \<Longrightarrow> (Rep (Abs c)) = c"
kaliszyk@35222
   656
  and     rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)"
kaliszyk@35222
   657
begin
kaliszyk@35222
   658
kaliszyk@35222
   659
definition
haftmann@40466
   660
  abs :: "'a \<Rightarrow> 'b"
kaliszyk@35222
   661
where
kaliszyk@44204
   662
  "abs x = Abs (Collect (R x))"
kaliszyk@35222
   663
kaliszyk@35222
   664
definition
haftmann@40466
   665
  rep :: "'b \<Rightarrow> 'a"
kaliszyk@35222
   666
where
kaliszyk@44204
   667
  "rep a = (SOME x. x \<in> Rep a)"
kaliszyk@35222
   668
kaliszyk@44204
   669
lemma some_collect:
kaliszyk@37493
   670
  assumes "R r r"
kaliszyk@44204
   671
  shows "R (SOME x. x \<in> Collect (R r)) = R r"
kaliszyk@44204
   672
  apply simp
kaliszyk@44204
   673
  by (metis assms exE_some equivp[simplified part_equivp_def])
kaliszyk@35222
   674
kaliszyk@35222
   675
lemma Quotient:
kaliszyk@35222
   676
  shows "Quotient R abs rep"
kaliszyk@37493
   677
  unfolding Quotient_def abs_def rep_def
kaliszyk@37493
   678
  proof (intro conjI allI)
kaliszyk@37493
   679
    fix a r s
kaliszyk@44204
   680
    show x: "R (SOME x. x \<in> Rep a) (SOME x. x \<in> Rep a)" proof -
kaliszyk@44204
   681
      obtain x where r: "R x x" and rep: "Rep a = Collect (R x)" using rep_prop[of a] by auto
kaliszyk@44204
   682
      have "R (SOME x. x \<in> Rep a) x"  using r rep some_collect by metis
kaliszyk@44204
   683
      then have "R x (SOME x. x \<in> Rep a)" using part_equivp_symp[OF equivp] by fast
kaliszyk@44204
   684
      then show "R (SOME x. x \<in> Rep a) (SOME x. x \<in> Rep a)"
kaliszyk@44204
   685
        using part_equivp_transp[OF equivp] by (metis `R (SOME x. x \<in> Rep a) x`)
kaliszyk@37493
   686
    qed
kaliszyk@44204
   687
    have "Collect (R (SOME x. x \<in> Rep a)) = (Rep a)" by (metis some_collect rep_prop)
kaliszyk@44204
   688
    then show "Abs (Collect (R (SOME x. x \<in> Rep a))) = a" using rep_inverse by auto
kaliszyk@44204
   689
    have "R r r \<Longrightarrow> R s s \<Longrightarrow> Abs (Collect (R r)) = Abs (Collect (R s)) \<longleftrightarrow> R r = R s"
haftmann@44242
   690
    proof -
haftmann@44242
   691
      assume "R r r" and "R s s"
haftmann@44242
   692
      then have "Abs (Collect (R r)) = Abs (Collect (R s)) \<longleftrightarrow> Collect (R r) = Collect (R s)"
haftmann@44242
   693
        by (metis abs_inverse)
haftmann@44242
   694
      also have "Collect (R r) = Collect (R s) \<longleftrightarrow> (\<lambda>A x. x \<in> A) (Collect (R r)) = (\<lambda>A x. x \<in> A) (Collect (R s))"
haftmann@44242
   695
        by rule simp_all
haftmann@44242
   696
      finally show "Abs (Collect (R r)) = Abs (Collect (R s)) \<longleftrightarrow> R r = R s" by simp
haftmann@44242
   697
    qed
kaliszyk@44204
   698
    then show "R r s \<longleftrightarrow> R r r \<and> R s s \<and> (Abs (Collect (R r)) = Abs (Collect (R s)))"
kaliszyk@44204
   699
      using equivp[simplified part_equivp_def] by metis
kaliszyk@44204
   700
    qed
haftmann@44242
   701
kaliszyk@35222
   702
end
kaliszyk@35222
   703
kuncar@47096
   704
subsection {* Quotient composition *}
kuncar@47096
   705
kuncar@47096
   706
lemma OOO_quotient:
kuncar@47096
   707
  fixes R1 :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
kuncar@47096
   708
  fixes Abs1 :: "'a \<Rightarrow> 'b" and Rep1 :: "'b \<Rightarrow> 'a"
kuncar@47096
   709
  fixes Abs2 :: "'b \<Rightarrow> 'c" and Rep2 :: "'c \<Rightarrow> 'b"
kuncar@47096
   710
  fixes R2' :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
kuncar@47096
   711
  fixes R2 :: "'b \<Rightarrow> 'b \<Rightarrow> bool"
kuncar@47096
   712
  assumes R1: "Quotient R1 Abs1 Rep1"
kuncar@47096
   713
  assumes R2: "Quotient R2 Abs2 Rep2"
kuncar@47096
   714
  assumes Abs1: "\<And>x y. R2' x y \<Longrightarrow> R1 x x \<Longrightarrow> R1 y y \<Longrightarrow> R2 (Abs1 x) (Abs1 y)"
kuncar@47096
   715
  assumes Rep1: "\<And>x y. R2 x y \<Longrightarrow> R2' (Rep1 x) (Rep1 y)"
kuncar@47096
   716
  shows "Quotient (R1 OO R2' OO R1) (Abs2 \<circ> Abs1) (Rep1 \<circ> Rep2)"
kuncar@47096
   717
apply (rule QuotientI)
kuncar@47096
   718
   apply (simp add: o_def Quotient_abs_rep [OF R2] Quotient_abs_rep [OF R1])
kuncar@47096
   719
  apply simp
kuncar@47096
   720
  apply (rule_tac b="Rep1 (Rep2 a)" in pred_compI)
kuncar@47096
   721
   apply (rule Quotient_rep_reflp [OF R1])
kuncar@47096
   722
  apply (rule_tac b="Rep1 (Rep2 a)" in pred_compI [rotated])
kuncar@47096
   723
   apply (rule Quotient_rep_reflp [OF R1])
kuncar@47096
   724
  apply (rule Rep1)
kuncar@47096
   725
  apply (rule Quotient_rep_reflp [OF R2])
kuncar@47096
   726
 apply safe
kuncar@47096
   727
    apply (rename_tac x y)
kuncar@47096
   728
    apply (drule Abs1)
kuncar@47096
   729
      apply (erule Quotient_refl2 [OF R1])
kuncar@47096
   730
     apply (erule Quotient_refl1 [OF R1])
kuncar@47096
   731
    apply (drule Quotient_refl1 [OF R2], drule Rep1)
kuncar@47096
   732
    apply (subgoal_tac "R1 r (Rep1 (Abs1 x))")
kuncar@47096
   733
     apply (rule_tac b="Rep1 (Abs1 x)" in pred_compI, assumption)
kuncar@47096
   734
     apply (erule pred_compI)
kuncar@47096
   735
     apply (erule Quotient_symp [OF R1, THEN sympD])
kuncar@47096
   736
    apply (rule Quotient_rel[symmetric, OF R1, THEN iffD2])
kuncar@47096
   737
    apply (rule conjI, erule Quotient_refl1 [OF R1])
kuncar@47096
   738
    apply (rule conjI, rule Quotient_rep_reflp [OF R1])
kuncar@47096
   739
    apply (subst Quotient_abs_rep [OF R1])
kuncar@47096
   740
    apply (erule Quotient_rel_abs [OF R1])
kuncar@47096
   741
   apply (rename_tac x y)
kuncar@47096
   742
   apply (drule Abs1)
kuncar@47096
   743
     apply (erule Quotient_refl2 [OF R1])
kuncar@47096
   744
    apply (erule Quotient_refl1 [OF R1])
kuncar@47096
   745
   apply (drule Quotient_refl2 [OF R2], drule Rep1)
kuncar@47096
   746
   apply (subgoal_tac "R1 s (Rep1 (Abs1 y))")
kuncar@47096
   747
    apply (rule_tac b="Rep1 (Abs1 y)" in pred_compI, assumption)
kuncar@47096
   748
    apply (erule pred_compI)
kuncar@47096
   749
    apply (erule Quotient_symp [OF R1, THEN sympD])
kuncar@47096
   750
   apply (rule Quotient_rel[symmetric, OF R1, THEN iffD2])
kuncar@47096
   751
   apply (rule conjI, erule Quotient_refl2 [OF R1])
kuncar@47096
   752
   apply (rule conjI, rule Quotient_rep_reflp [OF R1])
kuncar@47096
   753
   apply (subst Quotient_abs_rep [OF R1])
kuncar@47096
   754
   apply (erule Quotient_rel_abs [OF R1, THEN sym])
kuncar@47096
   755
  apply simp
kuncar@47096
   756
  apply (rule Quotient_rel_abs [OF R2])
kuncar@47096
   757
  apply (rule Quotient_rel_abs [OF R1, THEN ssubst], assumption)
kuncar@47096
   758
  apply (rule Quotient_rel_abs [OF R1, THEN subst], assumption)
kuncar@47096
   759
  apply (erule Abs1)
kuncar@47096
   760
   apply (erule Quotient_refl2 [OF R1])
kuncar@47096
   761
  apply (erule Quotient_refl1 [OF R1])
kuncar@47096
   762
 apply (rename_tac a b c d)
kuncar@47096
   763
 apply simp
kuncar@47096
   764
 apply (rule_tac b="Rep1 (Abs1 r)" in pred_compI)
kuncar@47096
   765
  apply (rule Quotient_rel[symmetric, OF R1, THEN iffD2])
kuncar@47096
   766
  apply (rule conjI, erule Quotient_refl1 [OF R1])
kuncar@47096
   767
  apply (simp add: Quotient_abs_rep [OF R1] Quotient_rep_reflp [OF R1])
kuncar@47096
   768
 apply (rule_tac b="Rep1 (Abs1 s)" in pred_compI [rotated])
kuncar@47096
   769
  apply (rule Quotient_rel[symmetric, OF R1, THEN iffD2])
kuncar@47096
   770
  apply (simp add: Quotient_abs_rep [OF R1] Quotient_rep_reflp [OF R1])
kuncar@47096
   771
  apply (erule Quotient_refl2 [OF R1])
kuncar@47096
   772
 apply (rule Rep1)
kuncar@47096
   773
 apply (drule Abs1)
kuncar@47096
   774
   apply (erule Quotient_refl2 [OF R1])
kuncar@47096
   775
  apply (erule Quotient_refl1 [OF R1])
kuncar@47096
   776
 apply (drule Abs1)
kuncar@47096
   777
  apply (erule Quotient_refl2 [OF R1])
kuncar@47096
   778
 apply (erule Quotient_refl1 [OF R1])
kuncar@47096
   779
 apply (drule Quotient_rel_abs [OF R1])
kuncar@47096
   780
 apply (drule Quotient_rel_abs [OF R1])
kuncar@47096
   781
 apply (drule Quotient_rel_abs [OF R1])
kuncar@47096
   782
 apply (drule Quotient_rel_abs [OF R1])
kuncar@47096
   783
 apply simp
kuncar@47096
   784
 apply (rule Quotient_rel[symmetric, OF R2, THEN iffD2])
kuncar@47096
   785
 apply simp
kuncar@47096
   786
done
kuncar@47096
   787
kuncar@47096
   788
lemma OOO_eq_quotient:
kuncar@47096
   789
  fixes R1 :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
kuncar@47096
   790
  fixes Abs1 :: "'a \<Rightarrow> 'b" and Rep1 :: "'b \<Rightarrow> 'a"
kuncar@47096
   791
  fixes Abs2 :: "'b \<Rightarrow> 'c" and Rep2 :: "'c \<Rightarrow> 'b"
kuncar@47096
   792
  assumes R1: "Quotient R1 Abs1 Rep1"
kuncar@47096
   793
  assumes R2: "Quotient op= Abs2 Rep2"
kuncar@47096
   794
  shows "Quotient (R1 OOO op=) (Abs2 \<circ> Abs1) (Rep1 \<circ> Rep2)"
kuncar@47096
   795
using assms
kuncar@47096
   796
by (rule OOO_quotient) auto
kuncar@47096
   797
kuncar@47096
   798
subsection {* Invariant *}
kuncar@47096
   799
kuncar@47096
   800
definition invariant :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" 
kuncar@47096
   801
  where "invariant R = (\<lambda>x y. R x \<and> x = y)"
kuncar@47096
   802
kuncar@47096
   803
lemma invariant_to_eq:
kuncar@47096
   804
  assumes "invariant P x y"
kuncar@47096
   805
  shows "x = y"
kuncar@47096
   806
using assms by (simp add: invariant_def)
kuncar@47096
   807
kuncar@47096
   808
lemma fun_rel_eq_invariant:
kuncar@47096
   809
  shows "((invariant R) ===> S) = (\<lambda>f g. \<forall>x. R x \<longrightarrow> S (f x) (g x))"
kuncar@47096
   810
by (auto simp add: invariant_def fun_rel_def)
kuncar@47096
   811
kuncar@47096
   812
lemma invariant_same_args:
kuncar@47096
   813
  shows "invariant P x x \<equiv> P x"
kuncar@47096
   814
using assms by (auto simp add: invariant_def)
kuncar@47096
   815
kuncar@47096
   816
lemma copy_type_to_Quotient:
kuncar@47096
   817
  assumes "type_definition Rep Abs UNIV"
kuncar@47096
   818
  shows "Quotient (op =) Abs Rep"
kuncar@47096
   819
proof -
kuncar@47096
   820
  interpret type_definition Rep Abs UNIV by fact
kuncar@47096
   821
  from Abs_inject Rep_inverse show ?thesis by (auto intro!: QuotientI)
kuncar@47096
   822
qed
kuncar@47096
   823
kuncar@47096
   824
lemma copy_type_to_equivp:
kuncar@47096
   825
  fixes Abs :: "'a \<Rightarrow> 'b"
kuncar@47096
   826
  and Rep :: "'b \<Rightarrow> 'a"
kuncar@47096
   827
  assumes "type_definition Rep Abs (UNIV::'a set)"
kuncar@47096
   828
  shows "equivp (op=::'a\<Rightarrow>'a\<Rightarrow>bool)"
kuncar@47096
   829
by (rule identity_equivp)
kuncar@47096
   830
kuncar@47096
   831
lemma invariant_type_to_Quotient:
kuncar@47096
   832
  assumes "type_definition Rep Abs {x. P x}"
kuncar@47096
   833
  shows "Quotient (invariant P) Abs Rep"
kuncar@47096
   834
proof -
kuncar@47096
   835
  interpret type_definition Rep Abs "{x. P x}" by fact
kuncar@47096
   836
  from Rep Abs_inject Rep_inverse show ?thesis by (auto intro!: QuotientI simp: invariant_def)
kuncar@47096
   837
qed
kuncar@47096
   838
kuncar@47096
   839
lemma invariant_type_to_part_equivp:
kuncar@47096
   840
  assumes "type_definition Rep Abs {x. P x}"
kuncar@47096
   841
  shows "part_equivp (invariant P)"
kuncar@47096
   842
proof (intro part_equivpI)
kuncar@47096
   843
  interpret type_definition Rep Abs "{x. P x}" by fact
kuncar@47096
   844
  show "\<exists>x. invariant P x x" using Rep by (auto simp: invariant_def)
kuncar@47096
   845
next
kuncar@47096
   846
  show "symp (invariant P)" by (auto intro: sympI simp: invariant_def)
kuncar@47096
   847
next
kuncar@47096
   848
  show "transp (invariant P)" by (auto intro: transpI simp: invariant_def)
kuncar@47096
   849
qed
kuncar@47096
   850
huffman@35294
   851
subsection {* ML setup *}
kaliszyk@35222
   852
kaliszyk@35222
   853
text {* Auxiliary data for the quotient package *}
kaliszyk@35222
   854
wenzelm@37986
   855
use "Tools/Quotient/quotient_info.ML"
wenzelm@41452
   856
setup Quotient_Info.setup
kaliszyk@35222
   857
kuncar@47094
   858
declare [[map "fun" = (fun_rel, fun_quotient)]]
kaliszyk@35222
   859
kaliszyk@35222
   860
lemmas [quot_thm] = fun_quotient
haftmann@44553
   861
lemmas [quot_respect] = quot_rel_rsp if_rsp o_rsp let_rsp id_rsp
haftmann@44553
   862
lemmas [quot_preserve] = if_prs o_prs let_prs id_prs
kaliszyk@35222
   863
lemmas [quot_equiv] = identity_equivp
kaliszyk@35222
   864
kaliszyk@35222
   865
kaliszyk@35222
   866
text {* Lemmas about simplifying id's. *}
kaliszyk@35222
   867
lemmas [id_simps] =
kaliszyk@35222
   868
  id_def[symmetric]
haftmann@40602
   869
  map_fun_id
kaliszyk@35222
   870
  id_apply
kaliszyk@35222
   871
  id_o
kaliszyk@35222
   872
  o_id
kaliszyk@35222
   873
  eq_comp_r
kaliszyk@44413
   874
  set_rel_eq
kaliszyk@44413
   875
  vimage_id
kaliszyk@35222
   876
kaliszyk@35222
   877
text {* Translation functions for the lifting process. *}
wenzelm@37986
   878
use "Tools/Quotient/quotient_term.ML"
kaliszyk@35222
   879
kaliszyk@35222
   880
kaliszyk@35222
   881
text {* Definitions of the quotient types. *}
wenzelm@45680
   882
use "Tools/Quotient/quotient_type.ML"
kaliszyk@35222
   883
kaliszyk@35222
   884
kaliszyk@35222
   885
text {* Definitions for quotient constants. *}
wenzelm@37986
   886
use "Tools/Quotient/quotient_def.ML"
kaliszyk@35222
   887
kaliszyk@35222
   888
kaliszyk@35222
   889
text {*
kaliszyk@35222
   890
  An auxiliary constant for recording some information
kaliszyk@35222
   891
  about the lifted theorem in a tactic.
kaliszyk@35222
   892
*}
kaliszyk@35222
   893
definition
haftmann@40466
   894
  Quot_True :: "'a \<Rightarrow> bool"
haftmann@40466
   895
where
haftmann@40466
   896
  "Quot_True x \<longleftrightarrow> True"
kaliszyk@35222
   897
kaliszyk@35222
   898
lemma
kaliszyk@35222
   899
  shows QT_all: "Quot_True (All P) \<Longrightarrow> Quot_True P"
kaliszyk@35222
   900
  and   QT_ex:  "Quot_True (Ex P) \<Longrightarrow> Quot_True P"
kaliszyk@35222
   901
  and   QT_ex1: "Quot_True (Ex1 P) \<Longrightarrow> Quot_True P"
kaliszyk@35222
   902
  and   QT_lam: "Quot_True (\<lambda>x. P x) \<Longrightarrow> (\<And>x. Quot_True (P x))"
kaliszyk@35222
   903
  and   QT_ext: "(\<And>x. Quot_True (a x) \<Longrightarrow> f x = g x) \<Longrightarrow> (Quot_True a \<Longrightarrow> f = g)"
kaliszyk@35222
   904
  by (simp_all add: Quot_True_def ext)
kaliszyk@35222
   905
kaliszyk@35222
   906
lemma QT_imp: "Quot_True a \<equiv> Quot_True b"
kaliszyk@35222
   907
  by (simp add: Quot_True_def)
kaliszyk@35222
   908
kaliszyk@35222
   909
kaliszyk@35222
   910
text {* Tactics for proving the lifted theorems *}
wenzelm@37986
   911
use "Tools/Quotient/quotient_tacs.ML"
kaliszyk@35222
   912
huffman@35294
   913
subsection {* Methods / Interface *}
kaliszyk@35222
   914
kaliszyk@35222
   915
method_setup lifting =
urbanc@37593
   916
  {* Attrib.thms >> (fn thms => fn ctxt => 
wenzelm@46468
   917
       SIMPLE_METHOD' (Quotient_Tacs.lift_tac ctxt [] thms)) *}
wenzelm@42814
   918
  {* lift theorems to quotient types *}
kaliszyk@35222
   919
kaliszyk@35222
   920
method_setup lifting_setup =
urbanc@37593
   921
  {* Attrib.thm >> (fn thm => fn ctxt => 
wenzelm@46468
   922
       SIMPLE_METHOD' (Quotient_Tacs.lift_procedure_tac ctxt [] thm)) *}
wenzelm@42814
   923
  {* set up the three goals for the quotient lifting procedure *}
kaliszyk@35222
   924
urbanc@37593
   925
method_setup descending =
wenzelm@46468
   926
  {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.descend_tac ctxt [])) *}
wenzelm@42814
   927
  {* decend theorems to the raw level *}
urbanc@37593
   928
urbanc@37593
   929
method_setup descending_setup =
wenzelm@46468
   930
  {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.descend_procedure_tac ctxt [])) *}
wenzelm@42814
   931
  {* set up the three goals for the decending theorems *}
urbanc@37593
   932
urbanc@45782
   933
method_setup partiality_descending =
wenzelm@46468
   934
  {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.partiality_descend_tac ctxt [])) *}
urbanc@45782
   935
  {* decend theorems to the raw level *}
urbanc@45782
   936
urbanc@45782
   937
method_setup partiality_descending_setup =
urbanc@45782
   938
  {* Scan.succeed (fn ctxt => 
wenzelm@46468
   939
       SIMPLE_METHOD' (Quotient_Tacs.partiality_descend_procedure_tac ctxt [])) *}
urbanc@45782
   940
  {* set up the three goals for the decending theorems *}
urbanc@45782
   941
kaliszyk@35222
   942
method_setup regularize =
wenzelm@46468
   943
  {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.regularize_tac ctxt)) *}
wenzelm@42814
   944
  {* prove the regularization goals from the quotient lifting procedure *}
kaliszyk@35222
   945
kaliszyk@35222
   946
method_setup injection =
wenzelm@46468
   947
  {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.all_injection_tac ctxt)) *}
wenzelm@42814
   948
  {* prove the rep/abs injection goals from the quotient lifting procedure *}
kaliszyk@35222
   949
kaliszyk@35222
   950
method_setup cleaning =
wenzelm@46468
   951
  {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.clean_tac ctxt)) *}
wenzelm@42814
   952
  {* prove the cleaning goals from the quotient lifting procedure *}
kaliszyk@35222
   953
kaliszyk@35222
   954
attribute_setup quot_lifted =
kaliszyk@35222
   955
  {* Scan.succeed Quotient_Tacs.lifted_attrib *}
wenzelm@42814
   956
  {* lift theorems to quotient types *}
kaliszyk@35222
   957
kaliszyk@35222
   958
no_notation
kaliszyk@35222
   959
  rel_conj (infixr "OOO" 75) and
haftmann@40602
   960
  map_fun (infixr "--->" 55) and
kaliszyk@35222
   961
  fun_rel (infixr "===>" 55)
kaliszyk@35222
   962
kuncar@47105
   963
hide_const (open) invariant
kuncar@47105
   964
kaliszyk@35222
   965
end