src/HOL/Quotient.thy
 author haftmann Thu Nov 18 17:01:15 2010 +0100 (2010-11-18) changeset 40602 91e583511113 parent 40466 c6587375088e child 40615 ab551d108feb permissions -rw-r--r--
map_fun combinator in theory Fun
1 (*  Title:      Quotient.thy
2     Author:     Cezary Kaliszyk and Christian Urban
3 *)
5 header {* Definition of Quotient Types *}
7 theory Quotient
8 imports Plain Hilbert_Choice Equiv_Relations
9 uses
10   ("Tools/Quotient/quotient_info.ML")
11   ("Tools/Quotient/quotient_typ.ML")
12   ("Tools/Quotient/quotient_def.ML")
13   ("Tools/Quotient/quotient_term.ML")
14   ("Tools/Quotient/quotient_tacs.ML")
15 begin
18 text {*
19   Basic definition for equivalence relations
20   that are represented by predicates.
21 *}
23 definition
24   "reflp E \<longleftrightarrow> (\<forall>x. E x x)"
26 lemma refl_reflp:
27   "refl A \<longleftrightarrow> reflp (\<lambda>x y. (x, y) \<in> A)"
28   by (simp add: refl_on_def reflp_def)
30 definition
31   "symp E \<longleftrightarrow> (\<forall>x y. E x y \<longrightarrow> E y x)"
33 lemma sym_symp:
34   "sym A \<longleftrightarrow> symp (\<lambda>x y. (x, y) \<in> A)"
35   by (simp add: sym_def symp_def)
37 definition
38   "transp E \<longleftrightarrow> (\<forall>x y z. E x y \<and> E y z \<longrightarrow> E x z)"
40 lemma trans_transp:
41   "trans A \<longleftrightarrow> transp (\<lambda>x y. (x, y) \<in> A)"
42   by (auto simp add: trans_def transp_def)
44 definition
45   "equivp E \<longleftrightarrow> (\<forall>x y. E x y = (E x = E y))"
47 lemma equivp_reflp_symp_transp:
48   shows "equivp E = (reflp E \<and> symp E \<and> transp E)"
49   unfolding equivp_def reflp_def symp_def transp_def fun_eq_iff
50   by blast
52 lemma equiv_equivp:
53   "equiv UNIV A \<longleftrightarrow> equivp (\<lambda>x y. (x, y) \<in> A)"
54   by (simp add: equiv_def equivp_reflp_symp_transp refl_reflp sym_symp trans_transp)
56 lemma equivp_reflp:
57   shows "equivp E \<Longrightarrow> E x x"
58   by (simp only: equivp_reflp_symp_transp reflp_def)
60 lemma equivp_symp:
61   shows "equivp E \<Longrightarrow> E x y \<Longrightarrow> E y x"
64 lemma equivp_transp:
65   shows "equivp E \<Longrightarrow> E x y \<Longrightarrow> E y z \<Longrightarrow> E x z"
68 lemma equivpI:
69   assumes "reflp R" "symp R" "transp R"
70   shows "equivp R"
71   using assms by (simp add: equivp_reflp_symp_transp)
73 lemma identity_equivp:
74   shows "equivp (op =)"
75   unfolding equivp_def
76   by auto
78 text {* Partial equivalences *}
80 definition
81   "part_equivp E \<longleftrightarrow> (\<exists>x. E x x) \<and> (\<forall>x y. E x y = (E x x \<and> E y y \<and> (E x = E y)))"
83 lemma equivp_implies_part_equivp:
84   assumes a: "equivp E"
85   shows "part_equivp E"
86   using a
87   unfolding equivp_def part_equivp_def
88   by auto
90 lemma part_equivp_symp:
91   assumes e: "part_equivp R"
92   and a: "R x y"
93   shows "R y x"
94   using e[simplified part_equivp_def] a
95   by (metis)
97 lemma part_equivp_typedef:
98   shows "part_equivp R \<Longrightarrow> \<exists>d. d \<in> (\<lambda>c. \<exists>x. R x x \<and> c = R x)"
99   unfolding part_equivp_def mem_def
100   apply clarify
101   apply (intro exI)
102   apply (rule conjI)
103   apply assumption
104   apply (rule refl)
105   done
107 lemma part_equivp_refl_symp_transp:
108   shows "part_equivp E \<longleftrightarrow> ((\<exists>x. E x x) \<and> symp E \<and> transp E)"
109 proof
110   assume "part_equivp E"
111   then show "(\<exists>x. E x x) \<and> symp E \<and> transp E"
112   unfolding part_equivp_def symp_def transp_def
113   by metis
114 next
115   assume a: "(\<exists>x. E x x) \<and> symp E \<and> transp E"
116   then have b: "(\<forall>x y. E x y \<longrightarrow> E y x)" and c: "(\<forall>x y z. E x y \<and> E y z \<longrightarrow> E x z)"
117     unfolding symp_def transp_def by (metis, metis)
118   have "(\<forall>x y. E x y = (E x x \<and> E y y \<and> E x = E y))"
119   proof (intro allI iffI conjI)
120     fix x y
121     assume d: "E x y"
122     then show "E x x" using b c by metis
123     show "E y y" using b c d by metis
124     show "E x = E y" unfolding fun_eq_iff using b c d by metis
125   next
126     fix x y
127     assume "E x x \<and> E y y \<and> E x = E y"
128     then show "E x y" using b c by metis
129   qed
130   then show "part_equivp E" unfolding part_equivp_def using a by metis
131 qed
133 lemma part_equivpI:
134   assumes "\<exists>x. R x x" "symp R" "transp R"
135   shows "part_equivp R"
136   using assms by (simp add: part_equivp_refl_symp_transp)
138 text {* Composition of Relations *}
140 abbreviation
141   rel_conj (infixr "OOO" 75)
142 where
143   "r1 OOO r2 \<equiv> r1 OO r2 OO r1"
145 lemma eq_comp_r:
146   shows "((op =) OOO R) = R"
147   by (auto simp add: fun_eq_iff)
149 subsection {* Respects predicate *}
151 definition
152   Respects :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set"
153 where
154   "Respects R x = R x x"
156 lemma in_respects:
157   shows "x \<in> Respects R \<longleftrightarrow> R x x"
158   unfolding mem_def Respects_def
159   by simp
161 subsection {* Function map and function relation *}
163 notation map_fun (infixr "--->" 55)
165 lemma map_fun_id:
166   "(id ---> id) = id"
169 definition
170   fun_rel :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> bool" (infixr "===>" 55)
171 where
172   "fun_rel E1 E2 = (\<lambda>f g. \<forall>x y. E1 x y \<longrightarrow> E2 (f x) (g y))"
174 lemma fun_relI [intro]:
175   assumes "\<And>x y. E1 x y \<Longrightarrow> E2 (f x) (g y)"
176   shows "(E1 ===> E2) f g"
177   using assms by (simp add: fun_rel_def)
179 lemma fun_relE:
180   assumes "(E1 ===> E2) f g" and "E1 x y"
181   obtains "E2 (f x) (g y)"
182   using assms by (simp add: fun_rel_def)
184 lemma fun_rel_eq:
185   shows "((op =) ===> (op =)) = (op =)"
186   by (auto simp add: fun_eq_iff elim: fun_relE)
189 subsection {* Quotient Predicate *}
191 definition
192   "Quotient E Abs Rep \<longleftrightarrow>
193      (\<forall>a. Abs (Rep a) = a) \<and> (\<forall>a. E (Rep a) (Rep a)) \<and>
194      (\<forall>r s. E r s = (E r r \<and> E s s \<and> (Abs r = Abs s)))"
196 lemma Quotient_abs_rep:
197   assumes a: "Quotient E Abs Rep"
198   shows "Abs (Rep a) = a"
199   using a
200   unfolding Quotient_def
201   by simp
203 lemma Quotient_rep_reflp:
204   assumes a: "Quotient E Abs Rep"
205   shows "E (Rep a) (Rep a)"
206   using a
207   unfolding Quotient_def
208   by blast
210 lemma Quotient_rel:
211   assumes a: "Quotient E Abs Rep"
212   shows " E r s = (E r r \<and> E s s \<and> (Abs r = Abs s))"
213   using a
214   unfolding Quotient_def
215   by blast
217 lemma Quotient_rel_rep:
218   assumes a: "Quotient R Abs Rep"
219   shows "R (Rep a) (Rep b) = (a = b)"
220   using a
221   unfolding Quotient_def
222   by metis
224 lemma Quotient_rep_abs:
225   assumes a: "Quotient R Abs Rep"
226   shows "R r r \<Longrightarrow> R (Rep (Abs r)) r"
227   using a unfolding Quotient_def
228   by blast
230 lemma Quotient_rel_abs:
231   assumes a: "Quotient E Abs Rep"
232   shows "E r s \<Longrightarrow> Abs r = Abs s"
233   using a unfolding Quotient_def
234   by blast
236 lemma Quotient_symp:
237   assumes a: "Quotient E Abs Rep"
238   shows "symp E"
239   using a unfolding Quotient_def symp_def
240   by metis
242 lemma Quotient_transp:
243   assumes a: "Quotient E Abs Rep"
244   shows "transp E"
245   using a unfolding Quotient_def transp_def
246   by metis
248 lemma identity_quotient:
249   shows "Quotient (op =) id id"
250   unfolding Quotient_def id_def
251   by blast
253 lemma fun_quotient:
254   assumes q1: "Quotient R1 abs1 rep1"
255   and     q2: "Quotient R2 abs2 rep2"
256   shows "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
257 proof -
258   have "\<And>a. (rep1 ---> abs2) ((abs1 ---> rep2) a) = a"
259     using q1 q2 by (simp add: Quotient_def fun_eq_iff)
260   moreover
261   have "\<And>a. (R1 ===> R2) ((abs1 ---> rep2) a) ((abs1 ---> rep2) a)"
262     by (rule fun_relI)
263       (insert q1 q2 Quotient_rel_abs [of R1 abs1 rep1] Quotient_rel_rep [of R2 abs2 rep2],
264         simp (no_asm) add: Quotient_def, simp)
265   moreover
266   have "\<And>r s. (R1 ===> R2) r s = ((R1 ===> R2) r r \<and> (R1 ===> R2) s s \<and>
267         (rep1 ---> abs2) r  = (rep1 ---> abs2) s)"
268     apply(auto simp add: fun_rel_def fun_eq_iff)
269     using q1 q2 unfolding Quotient_def
270     apply(metis)
271     using q1 q2 unfolding Quotient_def
272     apply(metis)
273     using q1 q2 unfolding Quotient_def
274     apply(metis)
275     using q1 q2 unfolding Quotient_def
276     apply(metis)
277     done
278   ultimately
279   show "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
280     unfolding Quotient_def by blast
281 qed
283 lemma abs_o_rep:
284   assumes a: "Quotient R Abs Rep"
285   shows "Abs o Rep = id"
286   unfolding fun_eq_iff
287   by (simp add: Quotient_abs_rep[OF a])
289 lemma equals_rsp:
290   assumes q: "Quotient R Abs Rep"
291   and     a: "R xa xb" "R ya yb"
292   shows "R xa ya = R xb yb"
293   using a Quotient_symp[OF q] Quotient_transp[OF q]
294   unfolding symp_def transp_def
295   by blast
297 lemma lambda_prs:
298   assumes q1: "Quotient R1 Abs1 Rep1"
299   and     q2: "Quotient R2 Abs2 Rep2"
300   shows "(Rep1 ---> Abs2) (\<lambda>x. Rep2 (f (Abs1 x))) = (\<lambda>x. f x)"
301   unfolding fun_eq_iff
302   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
305 lemma lambda_prs1:
306   assumes q1: "Quotient R1 Abs1 Rep1"
307   and     q2: "Quotient R2 Abs2 Rep2"
308   shows "(Rep1 ---> Abs2) (\<lambda>x. (Abs1 ---> Rep2) f x) = (\<lambda>x. f x)"
309   unfolding fun_eq_iff
310   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
313 lemma rep_abs_rsp:
314   assumes q: "Quotient R Abs Rep"
315   and     a: "R x1 x2"
316   shows "R x1 (Rep (Abs x2))"
317   using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
318   by metis
320 lemma rep_abs_rsp_left:
321   assumes q: "Quotient R Abs Rep"
322   and     a: "R x1 x2"
323   shows "R (Rep (Abs x1)) x2"
324   using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
325   by metis
327 text{*
328   In the following theorem R1 can be instantiated with anything,
329   but we know some of the types of the Rep and Abs functions;
330   so by solving Quotient assumptions we can get a unique R1 that
331   will be provable; which is why we need to use @{text apply_rsp} and
332   not the primed version *}
334 lemma apply_rsp:
335   fixes f g::"'a \<Rightarrow> 'c"
336   assumes q: "Quotient R1 Abs1 Rep1"
337   and     a: "(R1 ===> R2) f g" "R1 x y"
338   shows "R2 (f x) (g y)"
339   using a by (auto elim: fun_relE)
341 lemma apply_rsp':
342   assumes a: "(R1 ===> R2) f g" "R1 x y"
343   shows "R2 (f x) (g y)"
344   using a by (auto elim: fun_relE)
346 subsection {* lemmas for regularisation of ball and bex *}
348 lemma ball_reg_eqv:
349   fixes P :: "'a \<Rightarrow> bool"
350   assumes a: "equivp R"
351   shows "Ball (Respects R) P = (All P)"
352   using a
353   unfolding equivp_def
354   by (auto simp add: in_respects)
356 lemma bex_reg_eqv:
357   fixes P :: "'a \<Rightarrow> bool"
358   assumes a: "equivp R"
359   shows "Bex (Respects R) P = (Ex P)"
360   using a
361   unfolding equivp_def
362   by (auto simp add: in_respects)
364 lemma ball_reg_right:
365   assumes a: "\<And>x. R x \<Longrightarrow> P x \<longrightarrow> Q x"
366   shows "All P \<longrightarrow> Ball R Q"
367   using a by (metis Collect_def Collect_mem_eq)
369 lemma bex_reg_left:
370   assumes a: "\<And>x. R x \<Longrightarrow> Q x \<longrightarrow> P x"
371   shows "Bex R Q \<longrightarrow> Ex P"
372   using a by (metis Collect_def Collect_mem_eq)
374 lemma ball_reg_left:
375   assumes a: "equivp R"
376   shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ball (Respects R) Q \<longrightarrow> All P"
377   using a by (metis equivp_reflp in_respects)
379 lemma bex_reg_right:
380   assumes a: "equivp R"
381   shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ex Q \<longrightarrow> Bex (Respects R) P"
382   using a by (metis equivp_reflp in_respects)
384 lemma ball_reg_eqv_range:
385   fixes P::"'a \<Rightarrow> bool"
386   and x::"'a"
387   assumes a: "equivp R2"
388   shows   "(Ball (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = All (\<lambda>f. P (f x)))"
389   apply(rule iffI)
390   apply(rule allI)
391   apply(drule_tac x="\<lambda>y. f x" in bspec)
393   apply(rule impI)
394   using a equivp_reflp_symp_transp[of "R2"]
396   apply(simp)
397   apply(simp)
398   done
400 lemma bex_reg_eqv_range:
401   assumes a: "equivp R2"
402   shows   "(Bex (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = Ex (\<lambda>f. P (f x)))"
403   apply(auto)
404   apply(rule_tac x="\<lambda>y. f x" in bexI)
405   apply(simp)
406   apply(simp add: Respects_def in_respects fun_rel_def)
407   apply(rule impI)
408   using a equivp_reflp_symp_transp[of "R2"]
410   done
412 (* Next four lemmas are unused *)
413 lemma all_reg:
414   assumes a: "!x :: 'a. (P x --> Q x)"
415   and     b: "All P"
416   shows "All Q"
417   using a b by (metis)
419 lemma ex_reg:
420   assumes a: "!x :: 'a. (P x --> Q x)"
421   and     b: "Ex P"
422   shows "Ex Q"
423   using a b by metis
425 lemma ball_reg:
426   assumes a: "!x :: 'a. (R x --> P x --> Q x)"
427   and     b: "Ball R P"
428   shows "Ball R Q"
429   using a b by (metis Collect_def Collect_mem_eq)
431 lemma bex_reg:
432   assumes a: "!x :: 'a. (R x --> P x --> Q x)"
433   and     b: "Bex R P"
434   shows "Bex R Q"
435   using a b by (metis Collect_def Collect_mem_eq)
438 lemma ball_all_comm:
439   assumes "\<And>y. (\<forall>x\<in>P. A x y) \<longrightarrow> (\<forall>x. B x y)"
440   shows "(\<forall>x\<in>P. \<forall>y. A x y) \<longrightarrow> (\<forall>x. \<forall>y. B x y)"
441   using assms by auto
443 lemma bex_ex_comm:
444   assumes "(\<exists>y. \<exists>x. A x y) \<longrightarrow> (\<exists>y. \<exists>x\<in>P. B x y)"
445   shows "(\<exists>x. \<exists>y. A x y) \<longrightarrow> (\<exists>x\<in>P. \<exists>y. B x y)"
446   using assms by auto
448 subsection {* Bounded abstraction *}
450 definition
451   Babs :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
452 where
453   "x \<in> p \<Longrightarrow> Babs p m x = m x"
455 lemma babs_rsp:
456   assumes q: "Quotient R1 Abs1 Rep1"
457   and     a: "(R1 ===> R2) f g"
458   shows      "(R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)"
459   apply (auto simp add: Babs_def in_respects fun_rel_def)
460   apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
461   using a apply (simp add: Babs_def fun_rel_def)
462   apply (simp add: in_respects fun_rel_def)
463   using Quotient_rel[OF q]
464   by metis
466 lemma babs_prs:
467   assumes q1: "Quotient R1 Abs1 Rep1"
468   and     q2: "Quotient R2 Abs2 Rep2"
469   shows "((Rep1 ---> Abs2) (Babs (Respects R1) ((Abs1 ---> Rep2) f))) = f"
470   apply (rule ext)
472   apply (subgoal_tac "Rep1 x \<in> Respects R1")
473   apply (simp add: Babs_def Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2])
474   apply (simp add: in_respects Quotient_rel_rep[OF q1])
475   done
477 lemma babs_simp:
478   assumes q: "Quotient R1 Abs Rep"
479   shows "((R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)) = ((R1 ===> R2) f g)"
480   apply(rule iffI)
481   apply(simp_all only: babs_rsp[OF q])
482   apply(auto simp add: Babs_def fun_rel_def)
483   apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
484   apply(metis Babs_def)
486   using Quotient_rel[OF q]
487   by metis
489 (* If a user proves that a particular functional relation
490    is an equivalence this may be useful in regularising *)
491 lemma babs_reg_eqv:
492   shows "equivp R \<Longrightarrow> Babs (Respects R) P = P"
493   by (simp add: fun_eq_iff Babs_def in_respects equivp_reflp)
496 (* 3 lemmas needed for proving repabs_inj *)
497 lemma ball_rsp:
498   assumes a: "(R ===> (op =)) f g"
499   shows "Ball (Respects R) f = Ball (Respects R) g"
500   using a by (auto simp add: Ball_def in_respects elim: fun_relE)
502 lemma bex_rsp:
503   assumes a: "(R ===> (op =)) f g"
504   shows "(Bex (Respects R) f = Bex (Respects R) g)"
505   using a by (auto simp add: Bex_def in_respects elim: fun_relE)
507 lemma bex1_rsp:
508   assumes a: "(R ===> (op =)) f g"
509   shows "Ex1 (\<lambda>x. x \<in> Respects R \<and> f x) = Ex1 (\<lambda>x. x \<in> Respects R \<and> g x)"
510   using a by (auto elim: fun_relE simp add: Ex1_def in_respects)
512 (* 2 lemmas needed for cleaning of quantifiers *)
513 lemma all_prs:
514   assumes a: "Quotient R absf repf"
515   shows "Ball (Respects R) ((absf ---> id) f) = All f"
516   using a unfolding Quotient_def Ball_def in_respects id_apply comp_def map_fun_def
517   by metis
519 lemma ex_prs:
520   assumes a: "Quotient R absf repf"
521   shows "Bex (Respects R) ((absf ---> id) f) = Ex f"
522   using a unfolding Quotient_def Bex_def in_respects id_apply comp_def map_fun_def
523   by metis
525 subsection {* @{text Bex1_rel} quantifier *}
527 definition
528   Bex1_rel :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
529 where
530   "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)))"
532 lemma bex1_rel_aux:
533   "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R x\<rbrakk> \<Longrightarrow> Bex1_rel R y"
534   unfolding Bex1_rel_def
535   apply (erule conjE)+
536   apply (erule bexE)
537   apply rule
538   apply (rule_tac x="xa" in bexI)
539   apply metis
540   apply metis
541   apply rule+
542   apply (erule_tac x="xaa" in ballE)
543   prefer 2
544   apply (metis)
545   apply (erule_tac x="ya" in ballE)
546   prefer 2
547   apply (metis)
548   apply (metis in_respects)
549   done
551 lemma bex1_rel_aux2:
552   "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R y\<rbrakk> \<Longrightarrow> Bex1_rel R x"
553   unfolding Bex1_rel_def
554   apply (erule conjE)+
555   apply (erule bexE)
556   apply rule
557   apply (rule_tac x="xa" in bexI)
558   apply metis
559   apply metis
560   apply rule+
561   apply (erule_tac x="xaa" in ballE)
562   prefer 2
563   apply (metis)
564   apply (erule_tac x="ya" in ballE)
565   prefer 2
566   apply (metis)
567   apply (metis in_respects)
568   done
570 lemma bex1_rel_rsp:
571   assumes a: "Quotient R absf repf"
572   shows "((R ===> op =) ===> op =) (Bex1_rel R) (Bex1_rel R)"
574   apply clarify
575   apply rule
576   apply (simp_all add: bex1_rel_aux bex1_rel_aux2)
577   apply (erule bex1_rel_aux2)
578   apply assumption
579   done
582 lemma ex1_prs:
583   assumes a: "Quotient R absf repf"
584   shows "((absf ---> id) ---> id) (Bex1_rel R) f = Ex1 f"
586 apply (subst Bex1_rel_def)
587 apply (subst Bex_def)
588 apply (subst Ex1_def)
589 apply simp
590 apply rule
591  apply (erule conjE)+
592  apply (erule_tac exE)
593  apply (erule conjE)
594  apply (subgoal_tac "\<forall>y. R y y \<longrightarrow> f (absf y) \<longrightarrow> R x y")
595   apply (rule_tac x="absf x" in exI)
596   apply (simp)
597   apply rule+
598   using a unfolding Quotient_def
599   apply metis
600  apply rule+
601  apply (erule_tac x="x" in ballE)
602   apply (erule_tac x="y" in ballE)
603    apply simp
606 apply (erule_tac exE)
607  apply rule
608  apply (rule_tac x="repf x" in exI)
609  apply (simp only: in_respects)
610   apply rule
611  apply (metis Quotient_rel_rep[OF a])
612 using a unfolding Quotient_def apply (simp)
613 apply rule+
614 using a unfolding Quotient_def in_respects
615 apply metis
616 done
618 lemma bex1_bexeq_reg:
619   shows "(\<exists>!x\<in>Respects R. P x) \<longrightarrow> (Bex1_rel R (\<lambda>x. P x))"
620   apply (simp add: Ex1_def Bex1_rel_def in_respects)
621   apply clarify
622   apply auto
623   apply (rule bexI)
624   apply assumption
627   apply auto
628   done
630 lemma bex1_bexeq_reg_eqv:
631   assumes a: "equivp R"
632   shows "(\<exists>!x. P x) \<longrightarrow> Bex1_rel R P"
633   using equivp_reflp[OF a]
634   apply (intro impI)
635   apply (elim ex1E)
636   apply (rule mp[OF bex1_bexeq_reg])
637   apply (rule_tac a="x" in ex1I)
638   apply (subst in_respects)
639   apply (rule conjI)
640   apply assumption
641   apply assumption
642   apply clarify
643   apply (erule_tac x="xa" in allE)
644   apply simp
645   done
647 subsection {* Various respects and preserve lemmas *}
649 lemma quot_rel_rsp:
650   assumes a: "Quotient R Abs Rep"
651   shows "(R ===> R ===> op =) R R"
652   apply(rule fun_relI)+
653   apply(rule equals_rsp[OF a])
654   apply(assumption)+
655   done
657 lemma o_prs:
658   assumes q1: "Quotient R1 Abs1 Rep1"
659   and     q2: "Quotient R2 Abs2 Rep2"
660   and     q3: "Quotient R3 Abs3 Rep3"
661   shows "((Abs2 ---> Rep3) ---> (Abs1 ---> Rep2) ---> (Rep1 ---> Abs3)) op \<circ> = op \<circ>"
662   and   "(id ---> (Abs1 ---> id) ---> Rep1 ---> id) op \<circ> = op \<circ>"
663   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2] Quotient_abs_rep[OF q3]
666 lemma o_rsp:
667   "((R2 ===> R3) ===> (R1 ===> R2) ===> (R1 ===> R3)) op \<circ> op \<circ>"
668   "(op = ===> (R1 ===> op =) ===> R1 ===> op =) op \<circ> op \<circ>"
669   by (auto intro!: fun_relI elim: fun_relE)
671 lemma cond_prs:
672   assumes a: "Quotient R absf repf"
673   shows "absf (if a then repf b else repf c) = (if a then b else c)"
674   using a unfolding Quotient_def by auto
676 lemma if_prs:
677   assumes q: "Quotient R Abs Rep"
678   shows "(id ---> Rep ---> Rep ---> Abs) If = If"
679   using Quotient_abs_rep[OF q]
680   by (auto simp add: fun_eq_iff)
682 lemma if_rsp:
683   assumes q: "Quotient R Abs Rep"
684   shows "(op = ===> R ===> R ===> R) If If"
685   by (auto intro!: fun_relI)
687 lemma let_prs:
688   assumes q1: "Quotient R1 Abs1 Rep1"
689   and     q2: "Quotient R2 Abs2 Rep2"
690   shows "(Rep2 ---> (Abs2 ---> Rep1) ---> Abs1) Let = Let"
691   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
692   by (auto simp add: fun_eq_iff)
694 lemma let_rsp:
695   shows "(R1 ===> (R1 ===> R2) ===> R2) Let Let"
696   by (auto intro!: fun_relI elim: fun_relE)
698 lemma mem_rsp:
699   shows "(R1 ===> (R1 ===> R2) ===> R2) op \<in> op \<in>"
700   by (auto intro!: fun_relI elim: fun_relE simp add: mem_def)
702 lemma mem_prs:
703   assumes a1: "Quotient R1 Abs1 Rep1"
704   and     a2: "Quotient R2 Abs2 Rep2"
705   shows "(Rep1 ---> (Abs1 ---> Rep2) ---> Abs2) op \<in> = op \<in>"
706   by (simp add: fun_eq_iff mem_def Quotient_abs_rep[OF a1] Quotient_abs_rep[OF a2])
708 lemma id_rsp:
709   shows "(R ===> R) id id"
710   by (auto intro: fun_relI)
712 lemma id_prs:
713   assumes a: "Quotient R Abs Rep"
714   shows "(Rep ---> Abs) id = id"
715   by (simp add: fun_eq_iff Quotient_abs_rep [OF a])
718 locale quot_type =
719   fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
720   and   Abs :: "('a \<Rightarrow> bool) \<Rightarrow> 'b"
721   and   Rep :: "'b \<Rightarrow> ('a \<Rightarrow> bool)"
722   assumes equivp: "part_equivp R"
723   and     rep_prop: "\<And>y. \<exists>x. R x x \<and> Rep y = R x"
724   and     rep_inverse: "\<And>x. Abs (Rep x) = x"
725   and     abs_inverse: "\<And>c. (\<exists>x. ((R x x) \<and> (c = R x))) \<Longrightarrow> (Rep (Abs c)) = c"
726   and     rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)"
727 begin
729 definition
730   abs :: "'a \<Rightarrow> 'b"
731 where
732   "abs x = Abs (R x)"
734 definition
735   rep :: "'b \<Rightarrow> 'a"
736 where
737   "rep a = Eps (Rep a)"
739 lemma homeier5:
740   assumes a: "R r r"
741   shows "Rep (Abs (R r)) = R r"
742   apply (subst abs_inverse)
743   using a by auto
745 theorem homeier6:
746   assumes a: "R r r"
747   and b: "R s s"
748   shows "Abs (R r) = Abs (R s) \<longleftrightarrow> R r = R s"
749   by (metis a b homeier5)
751 theorem homeier8:
752   assumes "R r r"
753   shows "R (Eps (R r)) = R r"
754   using assms equivp[simplified part_equivp_def]
755   apply clarify
756   by (metis assms exE_some)
758 lemma Quotient:
759   shows "Quotient R abs rep"
760   unfolding Quotient_def abs_def rep_def
761   proof (intro conjI allI)
762     fix a r s
763     show "Abs (R (Eps (Rep a))) = a"
764       by (metis equivp exE_some part_equivp_def rep_inverse rep_prop)
765     show "R r s \<longleftrightarrow> R r r \<and> R s s \<and> (Abs (R r) = Abs (R s))"
766       by (metis homeier6 equivp[simplified part_equivp_def])
767     show "R (Eps (Rep a)) (Eps (Rep a))" proof -
768       obtain x where r: "R x x" and rep: "Rep a = R x" using rep_prop[of a] by auto
769       have "R (Eps (R x)) x" using homeier8 r by simp
770       then have "R x (Eps (R x))" using part_equivp_symp[OF equivp] by fast
771       then have "R (Eps (R x)) (Eps (R x))" using homeier8[OF r] by simp
772       then show "R (Eps (Rep a)) (Eps (Rep a))" using rep by simp
773     qed
774   qed
776 end
779 subsection {* ML setup *}
781 text {* Auxiliary data for the quotient package *}
783 use "Tools/Quotient/quotient_info.ML"
785 declare [[map "fun" = (map_fun, fun_rel)]]
787 lemmas [quot_thm] = fun_quotient
788 lemmas [quot_respect] = quot_rel_rsp if_rsp o_rsp let_rsp mem_rsp id_rsp
789 lemmas [quot_preserve] = if_prs o_prs let_prs mem_prs id_prs
790 lemmas [quot_equiv] = identity_equivp
793 text {* Lemmas about simplifying id's. *}
794 lemmas [id_simps] =
795   id_def[symmetric]
796   map_fun_id
797   id_apply
798   id_o
799   o_id
800   eq_comp_r
802 text {* Translation functions for the lifting process. *}
803 use "Tools/Quotient/quotient_term.ML"
806 text {* Definitions of the quotient types. *}
807 use "Tools/Quotient/quotient_typ.ML"
810 text {* Definitions for quotient constants. *}
811 use "Tools/Quotient/quotient_def.ML"
814 text {*
815   An auxiliary constant for recording some information
816   about the lifted theorem in a tactic.
817 *}
818 definition
819   Quot_True :: "'a \<Rightarrow> bool"
820 where
821   "Quot_True x \<longleftrightarrow> True"
823 lemma
824   shows QT_all: "Quot_True (All P) \<Longrightarrow> Quot_True P"
825   and   QT_ex:  "Quot_True (Ex P) \<Longrightarrow> Quot_True P"
826   and   QT_ex1: "Quot_True (Ex1 P) \<Longrightarrow> Quot_True P"
827   and   QT_lam: "Quot_True (\<lambda>x. P x) \<Longrightarrow> (\<And>x. Quot_True (P x))"
828   and   QT_ext: "(\<And>x. Quot_True (a x) \<Longrightarrow> f x = g x) \<Longrightarrow> (Quot_True a \<Longrightarrow> f = g)"
829   by (simp_all add: Quot_True_def ext)
831 lemma QT_imp: "Quot_True a \<equiv> Quot_True b"
835 text {* Tactics for proving the lifted theorems *}
836 use "Tools/Quotient/quotient_tacs.ML"
838 subsection {* Methods / Interface *}
840 method_setup lifting =
841   {* Attrib.thms >> (fn thms => fn ctxt =>
842        SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.lift_tac ctxt [] thms))) *}
843   {* lifts theorems to quotient types *}
845 method_setup lifting_setup =
846   {* Attrib.thm >> (fn thm => fn ctxt =>
847        SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.lift_procedure_tac ctxt [] thm))) *}
848   {* sets up the three goals for the quotient lifting procedure *}
850 method_setup descending =
851   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.descend_tac ctxt []))) *}
852   {* decends theorems to the raw level *}
854 method_setup descending_setup =
855   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.descend_procedure_tac ctxt []))) *}
856   {* sets up the three goals for the decending theorems *}
858 method_setup regularize =
859   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.regularize_tac ctxt))) *}
860   {* proves the regularization goals from the quotient lifting procedure *}
862 method_setup injection =
863   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.all_injection_tac ctxt))) *}
864   {* proves the rep/abs injection goals from the quotient lifting procedure *}
866 method_setup cleaning =
867   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.clean_tac ctxt))) *}
868   {* proves the cleaning goals from the quotient lifting procedure *}
870 attribute_setup quot_lifted =
871   {* Scan.succeed Quotient_Tacs.lifted_attrib *}
872   {* lifts theorems to quotient types *}
874 no_notation
875   rel_conj (infixr "OOO" 75) and
876   map_fun (infixr "--->" 55) and
877   fun_rel (infixr "===>" 55)
879 end