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