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