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