src/HOL/Quotient.thy
 author Cezary Kaliszyk Mon Aug 30 15:44:33 2010 +0900 (2010-08-30) changeset 38861 27c7b620758c parent 38859 053c69cb4a0e child 39198 f967a16dfcdd permissions -rw-r--r--
Quotient Package: added respectfulness and preservation lemmas for mem.
1 (*  Title:      Quotient.thy
2     Author:     Cezary Kaliszyk and Christian Urban
3 *)
5 header {* Definition of Quotient Types *}
7 theory Quotient
8 imports Plain Sledgehammer
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   "equivp E \<equiv> \<forall>x y. E x y = (E x = E y)"
26 definition
27   "reflp E \<equiv> \<forall>x. E x x"
29 definition
30   "symp E \<equiv> \<forall>x y. E x y \<longrightarrow> E y x"
32 definition
33   "transp E \<equiv> \<forall>x y z. E x y \<and> E y z \<longrightarrow> E x z"
35 lemma equivp_reflp_symp_transp:
36   shows "equivp E = (reflp E \<and> symp E \<and> transp E)"
37   unfolding equivp_def reflp_def symp_def transp_def expand_fun_eq
38   by blast
40 lemma equivp_reflp:
41   shows "equivp E \<Longrightarrow> E x x"
42   by (simp only: equivp_reflp_symp_transp reflp_def)
44 lemma equivp_symp:
45   shows "equivp E \<Longrightarrow> E x y \<Longrightarrow> E y x"
46   by (metis equivp_reflp_symp_transp symp_def)
48 lemma equivp_transp:
49   shows "equivp E \<Longrightarrow> E x y \<Longrightarrow> E y z \<Longrightarrow> E x z"
50   by (metis equivp_reflp_symp_transp transp_def)
52 lemma equivpI:
53   assumes "reflp R" "symp R" "transp R"
54   shows "equivp R"
55   using assms by (simp add: equivp_reflp_symp_transp)
57 lemma identity_equivp:
58   shows "equivp (op =)"
59   unfolding equivp_def
60   by auto
62 text {* Partial equivalences *}
64 definition
65   "part_equivp E \<equiv> (\<exists>x. E x x) \<and> (\<forall>x y. E x y = (E x x \<and> E y y \<and> (E x = E y)))"
67 lemma equivp_implies_part_equivp:
68   assumes a: "equivp E"
69   shows "part_equivp E"
70   using a
71   unfolding equivp_def part_equivp_def
72   by auto
74 lemma part_equivp_symp:
75   assumes e: "part_equivp R"
76   and a: "R x y"
77   shows "R y x"
78   using e[simplified part_equivp_def] a
79   by (metis)
81 lemma part_equivp_typedef:
82   shows "part_equivp R \<Longrightarrow> \<exists>d. d \<in> (\<lambda>c. \<exists>x. R x x \<and> c = R x)"
83   unfolding part_equivp_def mem_def
84   apply clarify
85   apply (intro exI)
86   apply (rule conjI)
87   apply assumption
88   apply (rule refl)
89   done
91 text {* Composition of Relations *}
93 abbreviation
94   rel_conj (infixr "OOO" 75)
95 where
96   "r1 OOO r2 \<equiv> r1 OO r2 OO r1"
98 lemma eq_comp_r:
99   shows "((op =) OOO R) = R"
100   by (auto simp add: expand_fun_eq)
102 subsection {* Respects predicate *}
104 definition
105   Respects
106 where
107   "Respects R x \<equiv> R x x"
109 lemma in_respects:
110   shows "(x \<in> Respects R) = R x x"
111   unfolding mem_def Respects_def
112   by simp
114 subsection {* Function map and function relation *}
116 definition
117   fun_map (infixr "--->" 55)
118 where
119 [simp]: "fun_map f g h x = g (h (f x))"
121 definition
122   fun_rel (infixr "===>" 55)
123 where
124 [simp]: "fun_rel E1 E2 f g = (\<forall>x y. E1 x y \<longrightarrow> E2 (f x) (g y))"
126 lemma fun_relI [intro]:
127   assumes "\<And>a b. P a b \<Longrightarrow> Q (x a) (y b)"
128   shows "(P ===> Q) x y"
129   using assms by (simp add: fun_rel_def)
131 lemma fun_map_id:
132   shows "(id ---> id) = id"
133   by (simp add: expand_fun_eq id_def)
135 lemma fun_rel_eq:
136   shows "((op =) ===> (op =)) = (op =)"
140 subsection {* Quotient Predicate *}
142 definition
143   "Quotient E Abs Rep \<equiv>
144      (\<forall>a. Abs (Rep a) = a) \<and> (\<forall>a. E (Rep a) (Rep a)) \<and>
145      (\<forall>r s. E r s = (E r r \<and> E s s \<and> (Abs r = Abs s)))"
147 lemma Quotient_abs_rep:
148   assumes a: "Quotient E Abs Rep"
149   shows "Abs (Rep a) = a"
150   using a
151   unfolding Quotient_def
152   by simp
154 lemma Quotient_rep_reflp:
155   assumes a: "Quotient E Abs Rep"
156   shows "E (Rep a) (Rep a)"
157   using a
158   unfolding Quotient_def
159   by blast
161 lemma Quotient_rel:
162   assumes a: "Quotient E Abs Rep"
163   shows " E r s = (E r r \<and> E s s \<and> (Abs r = Abs s))"
164   using a
165   unfolding Quotient_def
166   by blast
168 lemma Quotient_rel_rep:
169   assumes a: "Quotient R Abs Rep"
170   shows "R (Rep a) (Rep b) = (a = b)"
171   using a
172   unfolding Quotient_def
173   by metis
175 lemma Quotient_rep_abs:
176   assumes a: "Quotient R Abs Rep"
177   shows "R r r \<Longrightarrow> R (Rep (Abs r)) r"
178   using a unfolding Quotient_def
179   by blast
181 lemma Quotient_rel_abs:
182   assumes a: "Quotient E Abs Rep"
183   shows "E r s \<Longrightarrow> Abs r = Abs s"
184   using a unfolding Quotient_def
185   by blast
187 lemma Quotient_symp:
188   assumes a: "Quotient E Abs Rep"
189   shows "symp E"
190   using a unfolding Quotient_def symp_def
191   by metis
193 lemma Quotient_transp:
194   assumes a: "Quotient E Abs Rep"
195   shows "transp E"
196   using a unfolding Quotient_def transp_def
197   by metis
199 lemma identity_quotient:
200   shows "Quotient (op =) id id"
201   unfolding Quotient_def id_def
202   by blast
204 lemma fun_quotient:
205   assumes q1: "Quotient R1 abs1 rep1"
206   and     q2: "Quotient R2 abs2 rep2"
207   shows "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
208 proof -
209   have "\<forall>a. (rep1 ---> abs2) ((abs1 ---> rep2) a) = a"
210     using q1 q2
211     unfolding Quotient_def
212     unfolding expand_fun_eq
213     by simp
214   moreover
215   have "\<forall>a. (R1 ===> R2) ((abs1 ---> rep2) a) ((abs1 ---> rep2) a)"
216     using q1 q2
217     unfolding Quotient_def
218     by (simp (no_asm)) (metis)
219   moreover
220   have "\<forall>r s. (R1 ===> R2) r s = ((R1 ===> R2) r r \<and> (R1 ===> R2) s s \<and>
221         (rep1 ---> abs2) r  = (rep1 ---> abs2) s)"
222     unfolding expand_fun_eq
223     apply(auto)
224     using q1 q2 unfolding Quotient_def
225     apply(metis)
226     using q1 q2 unfolding Quotient_def
227     apply(metis)
228     using q1 q2 unfolding Quotient_def
229     apply(metis)
230     using q1 q2 unfolding Quotient_def
231     apply(metis)
232     done
233   ultimately
234   show "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
235     unfolding Quotient_def by blast
236 qed
238 lemma abs_o_rep:
239   assumes a: "Quotient R Abs Rep"
240   shows "Abs o Rep = id"
241   unfolding expand_fun_eq
242   by (simp add: Quotient_abs_rep[OF a])
244 lemma equals_rsp:
245   assumes q: "Quotient R Abs Rep"
246   and     a: "R xa xb" "R ya yb"
247   shows "R xa ya = R xb yb"
248   using a Quotient_symp[OF q] Quotient_transp[OF q]
249   unfolding symp_def transp_def
250   by blast
252 lemma lambda_prs:
253   assumes q1: "Quotient R1 Abs1 Rep1"
254   and     q2: "Quotient R2 Abs2 Rep2"
255   shows "(Rep1 ---> Abs2) (\<lambda>x. Rep2 (f (Abs1 x))) = (\<lambda>x. f x)"
256   unfolding expand_fun_eq
257   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
258   by simp
260 lemma lambda_prs1:
261   assumes q1: "Quotient R1 Abs1 Rep1"
262   and     q2: "Quotient R2 Abs2 Rep2"
263   shows "(Rep1 ---> Abs2) (\<lambda>x. (Abs1 ---> Rep2) f x) = (\<lambda>x. f x)"
264   unfolding expand_fun_eq
265   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
266   by simp
268 lemma rep_abs_rsp:
269   assumes q: "Quotient R Abs Rep"
270   and     a: "R x1 x2"
271   shows "R x1 (Rep (Abs x2))"
272   using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
273   by metis
275 lemma rep_abs_rsp_left:
276   assumes q: "Quotient R Abs Rep"
277   and     a: "R x1 x2"
278   shows "R (Rep (Abs x1)) x2"
279   using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
280   by metis
282 text{*
283   In the following theorem R1 can be instantiated with anything,
284   but we know some of the types of the Rep and Abs functions;
285   so by solving Quotient assumptions we can get a unique R1 that
286   will be provable; which is why we need to use @{text apply_rsp} and
287   not the primed version *}
289 lemma apply_rsp:
290   fixes f g::"'a \<Rightarrow> 'c"
291   assumes q: "Quotient R1 Abs1 Rep1"
292   and     a: "(R1 ===> R2) f g" "R1 x y"
293   shows "R2 (f x) (g y)"
294   using a by simp
296 lemma apply_rsp':
297   assumes a: "(R1 ===> R2) f g" "R1 x y"
298   shows "R2 (f x) (g y)"
299   using a by simp
301 subsection {* lemmas for regularisation of ball and bex *}
303 lemma ball_reg_eqv:
304   fixes P :: "'a \<Rightarrow> bool"
305   assumes a: "equivp R"
306   shows "Ball (Respects R) P = (All P)"
307   using a
308   unfolding equivp_def
309   by (auto simp add: in_respects)
311 lemma bex_reg_eqv:
312   fixes P :: "'a \<Rightarrow> bool"
313   assumes a: "equivp R"
314   shows "Bex (Respects R) P = (Ex P)"
315   using a
316   unfolding equivp_def
317   by (auto simp add: in_respects)
319 lemma ball_reg_right:
320   assumes a: "\<And>x. R x \<Longrightarrow> P x \<longrightarrow> Q x"
321   shows "All P \<longrightarrow> Ball R Q"
322   using a by (metis COMBC_def Collect_def Collect_mem_eq)
324 lemma bex_reg_left:
325   assumes a: "\<And>x. R x \<Longrightarrow> Q x \<longrightarrow> P x"
326   shows "Bex R Q \<longrightarrow> Ex P"
327   using a by (metis COMBC_def Collect_def Collect_mem_eq)
329 lemma ball_reg_left:
330   assumes a: "equivp R"
331   shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ball (Respects R) Q \<longrightarrow> All P"
332   using a by (metis equivp_reflp in_respects)
334 lemma bex_reg_right:
335   assumes a: "equivp R"
336   shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ex Q \<longrightarrow> Bex (Respects R) P"
337   using a by (metis equivp_reflp in_respects)
339 lemma ball_reg_eqv_range:
340   fixes P::"'a \<Rightarrow> bool"
341   and x::"'a"
342   assumes a: "equivp R2"
343   shows   "(Ball (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = All (\<lambda>f. P (f x)))"
344   apply(rule iffI)
345   apply(rule allI)
346   apply(drule_tac x="\<lambda>y. f x" in bspec)
348   apply(rule impI)
349   using a equivp_reflp_symp_transp[of "R2"]
351   apply(simp)
352   apply(simp)
353   done
355 lemma bex_reg_eqv_range:
356   assumes a: "equivp R2"
357   shows   "(Bex (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = Ex (\<lambda>f. P (f x)))"
358   apply(auto)
359   apply(rule_tac x="\<lambda>y. f x" in bexI)
360   apply(simp)
362   apply(rule impI)
363   using a equivp_reflp_symp_transp[of "R2"]
365   done
367 (* Next four lemmas are unused *)
368 lemma all_reg:
369   assumes a: "!x :: 'a. (P x --> Q x)"
370   and     b: "All P"
371   shows "All Q"
372   using a b by (metis)
374 lemma ex_reg:
375   assumes a: "!x :: 'a. (P x --> Q x)"
376   and     b: "Ex P"
377   shows "Ex Q"
378   using a b by metis
380 lemma ball_reg:
381   assumes a: "!x :: 'a. (R x --> P x --> Q x)"
382   and     b: "Ball R P"
383   shows "Ball R Q"
384   using a b by (metis COMBC_def Collect_def Collect_mem_eq)
386 lemma bex_reg:
387   assumes a: "!x :: 'a. (R x --> P x --> Q x)"
388   and     b: "Bex R P"
389   shows "Bex R Q"
390   using a b by (metis COMBC_def Collect_def Collect_mem_eq)
393 lemma ball_all_comm:
394   assumes "\<And>y. (\<forall>x\<in>P. A x y) \<longrightarrow> (\<forall>x. B x y)"
395   shows "(\<forall>x\<in>P. \<forall>y. A x y) \<longrightarrow> (\<forall>x. \<forall>y. B x y)"
396   using assms by auto
398 lemma bex_ex_comm:
399   assumes "(\<exists>y. \<exists>x. A x y) \<longrightarrow> (\<exists>y. \<exists>x\<in>P. B x y)"
400   shows "(\<exists>x. \<exists>y. A x y) \<longrightarrow> (\<exists>x\<in>P. \<exists>y. B x y)"
401   using assms by auto
403 subsection {* Bounded abstraction *}
405 definition
406   Babs :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
407 where
408   "x \<in> p \<Longrightarrow> Babs p m x = m x"
410 lemma babs_rsp:
411   assumes q: "Quotient R1 Abs1 Rep1"
412   and     a: "(R1 ===> R2) f g"
413   shows      "(R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)"
414   apply (auto simp add: Babs_def in_respects)
415   apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
416   using a apply (simp add: Babs_def)
418   using Quotient_rel[OF q]
419   by metis
421 lemma babs_prs:
422   assumes q1: "Quotient R1 Abs1 Rep1"
423   and     q2: "Quotient R2 Abs2 Rep2"
424   shows "((Rep1 ---> Abs2) (Babs (Respects R1) ((Abs1 ---> Rep2) f))) = f"
425   apply (rule ext)
426   apply (simp)
427   apply (subgoal_tac "Rep1 x \<in> Respects R1")
428   apply (simp add: Babs_def Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2])
429   apply (simp add: in_respects Quotient_rel_rep[OF q1])
430   done
432 lemma babs_simp:
433   assumes q: "Quotient R1 Abs Rep"
434   shows "((R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)) = ((R1 ===> R2) f g)"
435   apply(rule iffI)
436   apply(simp_all only: babs_rsp[OF q])
438   apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
439   apply(metis Babs_def)
441   using Quotient_rel[OF q]
442   by metis
444 (* If a user proves that a particular functional relation
445    is an equivalence this may be useful in regularising *)
446 lemma babs_reg_eqv:
447   shows "equivp R \<Longrightarrow> Babs (Respects R) P = P"
448   by (simp add: expand_fun_eq Babs_def in_respects equivp_reflp)
451 (* 3 lemmas needed for proving repabs_inj *)
452 lemma ball_rsp:
453   assumes a: "(R ===> (op =)) f g"
454   shows "Ball (Respects R) f = Ball (Respects R) g"
455   using a by (simp add: Ball_def in_respects)
457 lemma bex_rsp:
458   assumes a: "(R ===> (op =)) f g"
459   shows "(Bex (Respects R) f = Bex (Respects R) g)"
460   using a by (simp add: Bex_def in_respects)
462 lemma bex1_rsp:
463   assumes a: "(R ===> (op =)) f g"
464   shows "Ex1 (\<lambda>x. x \<in> Respects R \<and> f x) = Ex1 (\<lambda>x. x \<in> Respects R \<and> g x)"
465   using a
466   by (simp add: Ex1_def in_respects) auto
468 (* 2 lemmas needed for cleaning of quantifiers *)
469 lemma all_prs:
470   assumes a: "Quotient R absf repf"
471   shows "Ball (Respects R) ((absf ---> id) f) = All f"
472   using a unfolding Quotient_def Ball_def in_respects fun_map_def id_apply
473   by metis
475 lemma ex_prs:
476   assumes a: "Quotient R absf repf"
477   shows "Bex (Respects R) ((absf ---> id) f) = Ex f"
478   using a unfolding Quotient_def Bex_def in_respects fun_map_def id_apply
479   by metis
481 subsection {* @{text Bex1_rel} quantifier *}
483 definition
484   Bex1_rel :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
485 where
486   "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)))"
488 lemma bex1_rel_aux:
489   "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R x\<rbrakk> \<Longrightarrow> Bex1_rel R y"
490   unfolding Bex1_rel_def
491   apply (erule conjE)+
492   apply (erule bexE)
493   apply rule
494   apply (rule_tac x="xa" in bexI)
495   apply metis
496   apply metis
497   apply rule+
498   apply (erule_tac x="xaa" in ballE)
499   prefer 2
500   apply (metis)
501   apply (erule_tac x="ya" in ballE)
502   prefer 2
503   apply (metis)
504   apply (metis in_respects)
505   done
507 lemma bex1_rel_aux2:
508   "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R y\<rbrakk> \<Longrightarrow> Bex1_rel R x"
509   unfolding Bex1_rel_def
510   apply (erule conjE)+
511   apply (erule bexE)
512   apply rule
513   apply (rule_tac x="xa" in bexI)
514   apply metis
515   apply metis
516   apply rule+
517   apply (erule_tac x="xaa" in ballE)
518   prefer 2
519   apply (metis)
520   apply (erule_tac x="ya" in ballE)
521   prefer 2
522   apply (metis)
523   apply (metis in_respects)
524   done
526 lemma bex1_rel_rsp:
527   assumes a: "Quotient R absf repf"
528   shows "((R ===> op =) ===> op =) (Bex1_rel R) (Bex1_rel R)"
529   apply simp
530   apply clarify
531   apply rule
532   apply (simp_all add: bex1_rel_aux bex1_rel_aux2)
533   apply (erule bex1_rel_aux2)
534   apply assumption
535   done
538 lemma ex1_prs:
539   assumes a: "Quotient R absf repf"
540   shows "((absf ---> id) ---> id) (Bex1_rel R) f = Ex1 f"
541 apply simp
542 apply (subst Bex1_rel_def)
543 apply (subst Bex_def)
544 apply (subst Ex1_def)
545 apply simp
546 apply rule
547  apply (erule conjE)+
548  apply (erule_tac exE)
549  apply (erule conjE)
550  apply (subgoal_tac "\<forall>y. R y y \<longrightarrow> f (absf y) \<longrightarrow> R x y")
551   apply (rule_tac x="absf x" in exI)
552   apply (simp)
553   apply rule+
554   using a unfolding Quotient_def
555   apply metis
556  apply rule+
557  apply (erule_tac x="x" in ballE)
558   apply (erule_tac x="y" in ballE)
559    apply simp
562 apply (erule_tac exE)
563  apply rule
564  apply (rule_tac x="repf x" in exI)
565  apply (simp only: in_respects)
566   apply rule
567  apply (metis Quotient_rel_rep[OF a])
568 using a unfolding Quotient_def apply (simp)
569 apply rule+
570 using a unfolding Quotient_def in_respects
571 apply metis
572 done
574 lemma bex1_bexeq_reg:
575   shows "(\<exists>!x\<in>Respects R. P x) \<longrightarrow> (Bex1_rel R (\<lambda>x. P x))"
576   apply (simp add: Ex1_def Bex1_rel_def in_respects)
577   apply clarify
578   apply auto
579   apply (rule bexI)
580   apply assumption
583   apply auto
584   done
586 lemma bex1_bexeq_reg_eqv:
587   assumes a: "equivp R"
588   shows "(\<exists>!x. P x) \<longrightarrow> Bex1_rel R P"
589   using equivp_reflp[OF a]
590   apply (intro impI)
591   apply (elim ex1E)
592   apply (rule mp[OF bex1_bexeq_reg])
593   apply (rule_tac a="x" in ex1I)
594   apply (subst in_respects)
595   apply (rule conjI)
596   apply assumption
597   apply assumption
598   apply clarify
599   apply (erule_tac x="xa" in allE)
600   apply simp
601   done
603 subsection {* Various respects and preserve lemmas *}
605 lemma quot_rel_rsp:
606   assumes a: "Quotient R Abs Rep"
607   shows "(R ===> R ===> op =) R R"
608   apply(rule fun_relI)+
609   apply(rule equals_rsp[OF a])
610   apply(assumption)+
611   done
613 lemma o_prs:
614   assumes q1: "Quotient R1 Abs1 Rep1"
615   and     q2: "Quotient R2 Abs2 Rep2"
616   and     q3: "Quotient R3 Abs3 Rep3"
617   shows "((Abs2 ---> Rep3) ---> (Abs1 ---> Rep2) ---> (Rep1 ---> Abs3)) op \<circ> = op \<circ>"
618   and   "(id ---> (Abs1 ---> id) ---> Rep1 ---> id) op \<circ> = op \<circ>"
619   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2] Quotient_abs_rep[OF q3]
620   unfolding o_def expand_fun_eq by simp_all
622 lemma o_rsp:
623   "((R2 ===> R3) ===> (R1 ===> R2) ===> (R1 ===> R3)) op \<circ> op \<circ>"
624   "(op = ===> (R1 ===> op =) ===> R1 ===> op =) op \<circ> op \<circ>"
625   unfolding fun_rel_def o_def expand_fun_eq by auto
627 lemma cond_prs:
628   assumes a: "Quotient R absf repf"
629   shows "absf (if a then repf b else repf c) = (if a then b else c)"
630   using a unfolding Quotient_def by auto
632 lemma if_prs:
633   assumes q: "Quotient R Abs Rep"
634   shows "(id ---> Rep ---> Rep ---> Abs) If = If"
635   using Quotient_abs_rep[OF q]
636   by (auto simp add: expand_fun_eq)
638 lemma if_rsp:
639   assumes q: "Quotient R Abs Rep"
640   shows "(op = ===> R ===> R ===> R) If If"
641   by auto
643 lemma let_prs:
644   assumes q1: "Quotient R1 Abs1 Rep1"
645   and     q2: "Quotient R2 Abs2 Rep2"
646   shows "(Rep2 ---> (Abs2 ---> Rep1) ---> Abs1) Let = Let"
647   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
648   by (auto simp add: expand_fun_eq)
650 lemma let_rsp:
651   shows "(R1 ===> (R1 ===> R2) ===> R2) Let Let"
652   by auto
654 lemma mem_rsp:
655   shows "(R1 ===> (R1 ===> R2) ===> R2) op \<in> op \<in>"
658 lemma mem_prs:
659   assumes a1: "Quotient R1 Abs1 Rep1"
660   and     a2: "Quotient R2 Abs2 Rep2"
661   shows "(Rep1 ---> (Abs1 ---> Rep2) ---> Abs2) op \<in> = op \<in>"
662   by (simp add: expand_fun_eq mem_def Quotient_abs_rep[OF a1] Quotient_abs_rep[OF a2])
664 locale quot_type =
665   fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
666   and   Abs :: "('a \<Rightarrow> bool) \<Rightarrow> 'b"
667   and   Rep :: "'b \<Rightarrow> ('a \<Rightarrow> bool)"
668   assumes equivp: "part_equivp R"
669   and     rep_prop: "\<And>y. \<exists>x. R x x \<and> Rep y = R x"
670   and     rep_inverse: "\<And>x. Abs (Rep x) = x"
671   and     abs_inverse: "\<And>c. (\<exists>x. ((R x x) \<and> (c = R x))) \<Longrightarrow> (Rep (Abs c)) = c"
672   and     rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)"
673 begin
675 definition
676   abs::"'a \<Rightarrow> 'b"
677 where
678   "abs x \<equiv> Abs (R x)"
680 definition
681   rep::"'b \<Rightarrow> 'a"
682 where
683   "rep a = Eps (Rep a)"
685 lemma homeier5:
686   assumes a: "R r r"
687   shows "Rep (Abs (R r)) = R r"
688   apply (subst abs_inverse)
689   using a by auto
691 theorem homeier6:
692   assumes a: "R r r"
693   and b: "R s s"
694   shows "Abs (R r) = Abs (R s) \<longleftrightarrow> R r = R s"
695   by (metis a b homeier5)
697 theorem homeier8:
698   assumes "R r r"
699   shows "R (Eps (R r)) = R r"
700   using assms equivp[simplified part_equivp_def]
701   apply clarify
702   by (metis assms exE_some)
704 lemma Quotient:
705   shows "Quotient R abs rep"
706   unfolding Quotient_def abs_def rep_def
707   proof (intro conjI allI)
708     fix a r s
709     show "Abs (R (Eps (Rep a))) = a"
710       by (metis equivp exE_some part_equivp_def rep_inverse rep_prop)
711     show "R r s \<longleftrightarrow> R r r \<and> R s s \<and> (Abs (R r) = Abs (R s))"
712       by (metis homeier6 equivp[simplified part_equivp_def])
713     show "R (Eps (Rep a)) (Eps (Rep a))" proof -
714       obtain x where r: "R x x" and rep: "Rep a = R x" using rep_prop[of a] by auto
715       have "R (Eps (R x)) x" using homeier8 r by simp
716       then have "R x (Eps (R x))" using part_equivp_symp[OF equivp] by fast
717       then have "R (Eps (R x)) (Eps (R x))" using homeier8[OF r] by simp
718       then show "R (Eps (Rep a)) (Eps (Rep a))" using rep by simp
719     qed
720   qed
722 end
725 subsection {* ML setup *}
727 text {* Auxiliary data for the quotient package *}
729 use "Tools/Quotient/quotient_info.ML"
731 declare [[map "fun" = (fun_map, fun_rel)]]
733 lemmas [quot_thm] = fun_quotient
734 lemmas [quot_respect] = quot_rel_rsp if_rsp o_rsp let_rsp mem_rsp
735 lemmas [quot_preserve] = if_prs o_prs let_prs mem_prs
736 lemmas [quot_equiv] = identity_equivp
739 text {* Lemmas about simplifying id's. *}
740 lemmas [id_simps] =
741   id_def[symmetric]
742   fun_map_id
743   id_apply
744   id_o
745   o_id
746   eq_comp_r
748 text {* Translation functions for the lifting process. *}
749 use "Tools/Quotient/quotient_term.ML"
752 text {* Definitions of the quotient types. *}
753 use "Tools/Quotient/quotient_typ.ML"
756 text {* Definitions for quotient constants. *}
757 use "Tools/Quotient/quotient_def.ML"
760 text {*
761   An auxiliary constant for recording some information
762   about the lifted theorem in a tactic.
763 *}
764 definition
765   "Quot_True (x :: 'a) \<equiv> True"
767 lemma
768   shows QT_all: "Quot_True (All P) \<Longrightarrow> Quot_True P"
769   and   QT_ex:  "Quot_True (Ex P) \<Longrightarrow> Quot_True P"
770   and   QT_ex1: "Quot_True (Ex1 P) \<Longrightarrow> Quot_True P"
771   and   QT_lam: "Quot_True (\<lambda>x. P x) \<Longrightarrow> (\<And>x. Quot_True (P x))"
772   and   QT_ext: "(\<And>x. Quot_True (a x) \<Longrightarrow> f x = g x) \<Longrightarrow> (Quot_True a \<Longrightarrow> f = g)"
773   by (simp_all add: Quot_True_def ext)
775 lemma QT_imp: "Quot_True a \<equiv> Quot_True b"
779 text {* Tactics for proving the lifted theorems *}
780 use "Tools/Quotient/quotient_tacs.ML"
782 subsection {* Methods / Interface *}
784 method_setup lifting =
785   {* Attrib.thms >> (fn thms => fn ctxt =>
786        SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.lift_tac ctxt [] thms))) *}
787   {* lifts theorems to quotient types *}
789 method_setup lifting_setup =
790   {* Attrib.thm >> (fn thm => fn ctxt =>
791        SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.lift_procedure_tac ctxt [] thm))) *}
792   {* sets up the three goals for the quotient lifting procedure *}
794 method_setup descending =
795   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.descend_tac ctxt []))) *}
796   {* decends theorems to the raw level *}
798 method_setup descending_setup =
799   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.descend_procedure_tac ctxt []))) *}
800   {* sets up the three goals for the decending theorems *}
802 method_setup regularize =
803   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.regularize_tac ctxt))) *}
804   {* proves the regularization goals from the quotient lifting procedure *}
806 method_setup injection =
807   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.all_injection_tac ctxt))) *}
808   {* proves the rep/abs injection goals from the quotient lifting procedure *}
810 method_setup cleaning =
811   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.clean_tac ctxt))) *}
812   {* proves the cleaning goals from the quotient lifting procedure *}
814 attribute_setup quot_lifted =
815   {* Scan.succeed Quotient_Tacs.lifted_attrib *}
816   {* lifts theorems to quotient types *}
818 no_notation
819   rel_conj (infixr "OOO" 75) and
820   fun_map (infixr "--->" 55) and
821   fun_rel (infixr "===>" 55)
823 end