src/HOL/Quotient.thy
author Cezary Kaliszyk <kaliszyk@in.tum.de>
Mon Jun 28 09:48:36 2010 +0200 (2010-06-28)
changeset 37564 a47bb386b238
parent 37493 2377d246a631
child 37593 2505feaf2d70
permissions -rw-r--r--
Quotient package reverse lifting
     1 (*  Title:      Quotient.thy
     2     Author:     Cezary Kaliszyk and Christian Urban
     3 *)
     4 
     5 header {* Definition of Quotient Types *}
     6 
     7 theory Quotient
     8 imports Plain Sledgehammer
     9 uses
    10   ("~~/src/HOL/Tools/Quotient/quotient_info.ML")
    11   ("~~/src/HOL/Tools/Quotient/quotient_typ.ML")
    12   ("~~/src/HOL/Tools/Quotient/quotient_def.ML")
    13   ("~~/src/HOL/Tools/Quotient/quotient_term.ML")
    14   ("~~/src/HOL/Tools/Quotient/quotient_tacs.ML")
    15 begin
    16 
    17 
    18 text {*
    19   Basic definition for equivalence relations
    20   that are represented by predicates.
    21 *}
    22 
    23 definition
    24   "equivp E \<equiv> \<forall>x y. E x y = (E x = E y)"
    25 
    26 definition
    27   "reflp E \<equiv> \<forall>x. E x x"
    28 
    29 definition
    30   "symp E \<equiv> \<forall>x y. E x y \<longrightarrow> E y x"
    31 
    32 definition
    33   "transp E \<equiv> \<forall>x y z. E x y \<and> E y z \<longrightarrow> E x z"
    34 
    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
    39 
    40 lemma equivp_reflp:
    41   shows "equivp E \<Longrightarrow> E x x"
    42   by (simp only: equivp_reflp_symp_transp reflp_def)
    43 
    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)
    47 
    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)
    51 
    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)
    56 
    57 lemma identity_equivp:
    58   shows "equivp (op =)"
    59   unfolding equivp_def
    60   by auto
    61 
    62 text {* Partial equivalences *}
    63 
    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)))"
    66 
    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
    73 
    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)
    80 
    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
    90 
    91 text {* Composition of Relations *}
    92 
    93 abbreviation
    94   rel_conj (infixr "OOO" 75)
    95 where
    96   "r1 OOO r2 \<equiv> r1 OO r2 OO r1"
    97 
    98 lemma eq_comp_r:
    99   shows "((op =) OOO R) = R"
   100   by (auto simp add: expand_fun_eq)
   101 
   102 subsection {* Respects predicate *}
   103 
   104 definition
   105   Respects
   106 where
   107   "Respects R x \<equiv> R x x"
   108 
   109 lemma in_respects:
   110   shows "(x \<in> Respects R) = R x x"
   111   unfolding mem_def Respects_def
   112   by simp
   113 
   114 subsection {* Function map and function relation *}
   115 
   116 definition
   117   fun_map (infixr "--->" 55)
   118 where
   119 [simp]: "fun_map f g h x = g (h (f x))"
   120 
   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))"
   125 
   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)
   130 
   131 lemma fun_map_id:
   132   shows "(id ---> id) = id"
   133   by (simp add: expand_fun_eq id_def)
   134 
   135 lemma fun_rel_eq:
   136   shows "((op =) ===> (op =)) = (op =)"
   137   by (simp add: expand_fun_eq)
   138 
   139 lemma fun_rel_id:
   140   assumes a: "\<And>x y. R1 x y \<Longrightarrow> R2 (f x) (g y)"
   141   shows "(R1 ===> R2) f g"
   142   using a by simp
   143 
   144 lemma fun_rel_id_asm:
   145   assumes a: "\<And>x y. R1 x y \<Longrightarrow> (A \<longrightarrow> R2 (f x) (g y))"
   146   shows "A \<longrightarrow> (R1 ===> R2) f g"
   147   using a by auto
   148 
   149 
   150 subsection {* Quotient Predicate *}
   151 
   152 definition
   153   "Quotient E Abs Rep \<equiv>
   154      (\<forall>a. Abs (Rep a) = a) \<and> (\<forall>a. E (Rep a) (Rep a)) \<and>
   155      (\<forall>r s. E r s = (E r r \<and> E s s \<and> (Abs r = Abs s)))"
   156 
   157 lemma Quotient_abs_rep:
   158   assumes a: "Quotient E Abs Rep"
   159   shows "Abs (Rep a) = a"
   160   using a
   161   unfolding Quotient_def
   162   by simp
   163 
   164 lemma Quotient_rep_reflp:
   165   assumes a: "Quotient E Abs Rep"
   166   shows "E (Rep a) (Rep a)"
   167   using a
   168   unfolding Quotient_def
   169   by blast
   170 
   171 lemma Quotient_rel:
   172   assumes a: "Quotient E Abs Rep"
   173   shows " E r s = (E r r \<and> E s s \<and> (Abs r = Abs s))"
   174   using a
   175   unfolding Quotient_def
   176   by blast
   177 
   178 lemma Quotient_rel_rep:
   179   assumes a: "Quotient R Abs Rep"
   180   shows "R (Rep a) (Rep b) = (a = b)"
   181   using a
   182   unfolding Quotient_def
   183   by metis
   184 
   185 lemma Quotient_rep_abs:
   186   assumes a: "Quotient R Abs Rep"
   187   shows "R r r \<Longrightarrow> R (Rep (Abs r)) r"
   188   using a unfolding Quotient_def
   189   by blast
   190 
   191 lemma Quotient_rel_abs:
   192   assumes a: "Quotient E Abs Rep"
   193   shows "E r s \<Longrightarrow> Abs r = Abs s"
   194   using a unfolding Quotient_def
   195   by blast
   196 
   197 lemma Quotient_symp:
   198   assumes a: "Quotient E Abs Rep"
   199   shows "symp E"
   200   using a unfolding Quotient_def symp_def
   201   by metis
   202 
   203 lemma Quotient_transp:
   204   assumes a: "Quotient E Abs Rep"
   205   shows "transp E"
   206   using a unfolding Quotient_def transp_def
   207   by metis
   208 
   209 lemma identity_quotient:
   210   shows "Quotient (op =) id id"
   211   unfolding Quotient_def id_def
   212   by blast
   213 
   214 lemma fun_quotient:
   215   assumes q1: "Quotient R1 abs1 rep1"
   216   and     q2: "Quotient R2 abs2 rep2"
   217   shows "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
   218 proof -
   219   have "\<forall>a. (rep1 ---> abs2) ((abs1 ---> rep2) a) = a"
   220     using q1 q2
   221     unfolding Quotient_def
   222     unfolding expand_fun_eq
   223     by simp
   224   moreover
   225   have "\<forall>a. (R1 ===> R2) ((abs1 ---> rep2) a) ((abs1 ---> rep2) a)"
   226     using q1 q2
   227     unfolding Quotient_def
   228     by (simp (no_asm)) (metis)
   229   moreover
   230   have "\<forall>r s. (R1 ===> R2) r s = ((R1 ===> R2) r r \<and> (R1 ===> R2) s s \<and>
   231         (rep1 ---> abs2) r  = (rep1 ---> abs2) s)"
   232     unfolding expand_fun_eq
   233     apply(auto)
   234     using q1 q2 unfolding Quotient_def
   235     apply(metis)
   236     using q1 q2 unfolding Quotient_def
   237     apply(metis)
   238     using q1 q2 unfolding Quotient_def
   239     apply(metis)
   240     using q1 q2 unfolding Quotient_def
   241     apply(metis)
   242     done
   243   ultimately
   244   show "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
   245     unfolding Quotient_def by blast
   246 qed
   247 
   248 lemma abs_o_rep:
   249   assumes a: "Quotient R Abs Rep"
   250   shows "Abs o Rep = id"
   251   unfolding expand_fun_eq
   252   by (simp add: Quotient_abs_rep[OF a])
   253 
   254 lemma equals_rsp:
   255   assumes q: "Quotient R Abs Rep"
   256   and     a: "R xa xb" "R ya yb"
   257   shows "R xa ya = R xb yb"
   258   using a Quotient_symp[OF q] Quotient_transp[OF q]
   259   unfolding symp_def transp_def
   260   by blast
   261 
   262 lemma lambda_prs:
   263   assumes q1: "Quotient R1 Abs1 Rep1"
   264   and     q2: "Quotient R2 Abs2 Rep2"
   265   shows "(Rep1 ---> Abs2) (\<lambda>x. Rep2 (f (Abs1 x))) = (\<lambda>x. f x)"
   266   unfolding expand_fun_eq
   267   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
   268   by simp
   269 
   270 lemma lambda_prs1:
   271   assumes q1: "Quotient R1 Abs1 Rep1"
   272   and     q2: "Quotient R2 Abs2 Rep2"
   273   shows "(Rep1 ---> Abs2) (\<lambda>x. (Abs1 ---> Rep2) f x) = (\<lambda>x. f x)"
   274   unfolding expand_fun_eq
   275   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
   276   by simp
   277 
   278 lemma rep_abs_rsp:
   279   assumes q: "Quotient R Abs Rep"
   280   and     a: "R x1 x2"
   281   shows "R x1 (Rep (Abs x2))"
   282   using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
   283   by metis
   284 
   285 lemma rep_abs_rsp_left:
   286   assumes q: "Quotient R Abs Rep"
   287   and     a: "R x1 x2"
   288   shows "R (Rep (Abs x1)) x2"
   289   using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
   290   by metis
   291 
   292 text{*
   293   In the following theorem R1 can be instantiated with anything,
   294   but we know some of the types of the Rep and Abs functions;
   295   so by solving Quotient assumptions we can get a unique R1 that
   296   will be provable; which is why we need to use @{text apply_rsp} and
   297   not the primed version *}
   298 
   299 lemma apply_rsp:
   300   fixes f g::"'a \<Rightarrow> 'c"
   301   assumes q: "Quotient R1 Abs1 Rep1"
   302   and     a: "(R1 ===> R2) f g" "R1 x y"
   303   shows "R2 (f x) (g y)"
   304   using a by simp
   305 
   306 lemma apply_rsp':
   307   assumes a: "(R1 ===> R2) f g" "R1 x y"
   308   shows "R2 (f x) (g y)"
   309   using a by simp
   310 
   311 subsection {* lemmas for regularisation of ball and bex *}
   312 
   313 lemma ball_reg_eqv:
   314   fixes P :: "'a \<Rightarrow> bool"
   315   assumes a: "equivp R"
   316   shows "Ball (Respects R) P = (All P)"
   317   using a
   318   unfolding equivp_def
   319   by (auto simp add: in_respects)
   320 
   321 lemma bex_reg_eqv:
   322   fixes P :: "'a \<Rightarrow> bool"
   323   assumes a: "equivp R"
   324   shows "Bex (Respects R) P = (Ex P)"
   325   using a
   326   unfolding equivp_def
   327   by (auto simp add: in_respects)
   328 
   329 lemma ball_reg_right:
   330   assumes a: "\<And>x. R x \<Longrightarrow> P x \<longrightarrow> Q x"
   331   shows "All P \<longrightarrow> Ball R Q"
   332   using a by (metis COMBC_def Collect_def Collect_mem_eq)
   333 
   334 lemma bex_reg_left:
   335   assumes a: "\<And>x. R x \<Longrightarrow> Q x \<longrightarrow> P x"
   336   shows "Bex R Q \<longrightarrow> Ex P"
   337   using a by (metis COMBC_def Collect_def Collect_mem_eq)
   338 
   339 lemma ball_reg_left:
   340   assumes a: "equivp R"
   341   shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ball (Respects R) Q \<longrightarrow> All P"
   342   using a by (metis equivp_reflp in_respects)
   343 
   344 lemma bex_reg_right:
   345   assumes a: "equivp R"
   346   shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ex Q \<longrightarrow> Bex (Respects R) P"
   347   using a by (metis equivp_reflp in_respects)
   348 
   349 lemma ball_reg_eqv_range:
   350   fixes P::"'a \<Rightarrow> bool"
   351   and x::"'a"
   352   assumes a: "equivp R2"
   353   shows   "(Ball (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = All (\<lambda>f. P (f x)))"
   354   apply(rule iffI)
   355   apply(rule allI)
   356   apply(drule_tac x="\<lambda>y. f x" in bspec)
   357   apply(simp add: in_respects)
   358   apply(rule impI)
   359   using a equivp_reflp_symp_transp[of "R2"]
   360   apply(simp add: reflp_def)
   361   apply(simp)
   362   apply(simp)
   363   done
   364 
   365 lemma bex_reg_eqv_range:
   366   assumes a: "equivp R2"
   367   shows   "(Bex (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = Ex (\<lambda>f. P (f x)))"
   368   apply(auto)
   369   apply(rule_tac x="\<lambda>y. f x" in bexI)
   370   apply(simp)
   371   apply(simp add: Respects_def in_respects)
   372   apply(rule impI)
   373   using a equivp_reflp_symp_transp[of "R2"]
   374   apply(simp add: reflp_def)
   375   done
   376 
   377 (* Next four lemmas are unused *)
   378 lemma all_reg:
   379   assumes a: "!x :: 'a. (P x --> Q x)"
   380   and     b: "All P"
   381   shows "All Q"
   382   using a b by (metis)
   383 
   384 lemma ex_reg:
   385   assumes a: "!x :: 'a. (P x --> Q x)"
   386   and     b: "Ex P"
   387   shows "Ex Q"
   388   using a b by metis
   389 
   390 lemma ball_reg:
   391   assumes a: "!x :: 'a. (R x --> P x --> Q x)"
   392   and     b: "Ball R P"
   393   shows "Ball R Q"
   394   using a b by (metis COMBC_def Collect_def Collect_mem_eq)
   395 
   396 lemma bex_reg:
   397   assumes a: "!x :: 'a. (R x --> P x --> Q x)"
   398   and     b: "Bex R P"
   399   shows "Bex R Q"
   400   using a b by (metis COMBC_def Collect_def Collect_mem_eq)
   401 
   402 
   403 lemma ball_all_comm:
   404   assumes "\<And>y. (\<forall>x\<in>P. A x y) \<longrightarrow> (\<forall>x. B x y)"
   405   shows "(\<forall>x\<in>P. \<forall>y. A x y) \<longrightarrow> (\<forall>x. \<forall>y. B x y)"
   406   using assms by auto
   407 
   408 lemma bex_ex_comm:
   409   assumes "(\<exists>y. \<exists>x. A x y) \<longrightarrow> (\<exists>y. \<exists>x\<in>P. B x y)"
   410   shows "(\<exists>x. \<exists>y. A x y) \<longrightarrow> (\<exists>x\<in>P. \<exists>y. B x y)"
   411   using assms by auto
   412 
   413 subsection {* Bounded abstraction *}
   414 
   415 definition
   416   Babs :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
   417 where
   418   "x \<in> p \<Longrightarrow> Babs p m x = m x"
   419 
   420 lemma babs_rsp:
   421   assumes q: "Quotient R1 Abs1 Rep1"
   422   and     a: "(R1 ===> R2) f g"
   423   shows      "(R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)"
   424   apply (auto simp add: Babs_def in_respects)
   425   apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
   426   using a apply (simp add: Babs_def)
   427   apply (simp add: in_respects)
   428   using Quotient_rel[OF q]
   429   by metis
   430 
   431 lemma babs_prs:
   432   assumes q1: "Quotient R1 Abs1 Rep1"
   433   and     q2: "Quotient R2 Abs2 Rep2"
   434   shows "((Rep1 ---> Abs2) (Babs (Respects R1) ((Abs1 ---> Rep2) f))) = f"
   435   apply (rule ext)
   436   apply (simp)
   437   apply (subgoal_tac "Rep1 x \<in> Respects R1")
   438   apply (simp add: Babs_def Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2])
   439   apply (simp add: in_respects Quotient_rel_rep[OF q1])
   440   done
   441 
   442 lemma babs_simp:
   443   assumes q: "Quotient R1 Abs Rep"
   444   shows "((R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)) = ((R1 ===> R2) f g)"
   445   apply(rule iffI)
   446   apply(simp_all only: babs_rsp[OF q])
   447   apply(auto simp add: Babs_def)
   448   apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
   449   apply(metis Babs_def)
   450   apply (simp add: in_respects)
   451   using Quotient_rel[OF q]
   452   by metis
   453 
   454 (* If a user proves that a particular functional relation
   455    is an equivalence this may be useful in regularising *)
   456 lemma babs_reg_eqv:
   457   shows "equivp R \<Longrightarrow> Babs (Respects R) P = P"
   458   by (simp add: expand_fun_eq Babs_def in_respects equivp_reflp)
   459 
   460 
   461 (* 3 lemmas needed for proving repabs_inj *)
   462 lemma ball_rsp:
   463   assumes a: "(R ===> (op =)) f g"
   464   shows "Ball (Respects R) f = Ball (Respects R) g"
   465   using a by (simp add: Ball_def in_respects)
   466 
   467 lemma bex_rsp:
   468   assumes a: "(R ===> (op =)) f g"
   469   shows "(Bex (Respects R) f = Bex (Respects R) g)"
   470   using a by (simp add: Bex_def in_respects)
   471 
   472 lemma bex1_rsp:
   473   assumes a: "(R ===> (op =)) f g"
   474   shows "Ex1 (\<lambda>x. x \<in> Respects R \<and> f x) = Ex1 (\<lambda>x. x \<in> Respects R \<and> g x)"
   475   using a
   476   by (simp add: Ex1_def in_respects) auto
   477 
   478 (* 2 lemmas needed for cleaning of quantifiers *)
   479 lemma all_prs:
   480   assumes a: "Quotient R absf repf"
   481   shows "Ball (Respects R) ((absf ---> id) f) = All f"
   482   using a unfolding Quotient_def Ball_def in_respects fun_map_def id_apply
   483   by metis
   484 
   485 lemma ex_prs:
   486   assumes a: "Quotient R absf repf"
   487   shows "Bex (Respects R) ((absf ---> id) f) = Ex f"
   488   using a unfolding Quotient_def Bex_def in_respects fun_map_def id_apply
   489   by metis
   490 
   491 subsection {* @{text Bex1_rel} quantifier *}
   492 
   493 definition
   494   Bex1_rel :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
   495 where
   496   "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)))"
   497 
   498 lemma bex1_rel_aux:
   499   "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R x\<rbrakk> \<Longrightarrow> Bex1_rel R y"
   500   unfolding Bex1_rel_def
   501   apply (erule conjE)+
   502   apply (erule bexE)
   503   apply rule
   504   apply (rule_tac x="xa" in bexI)
   505   apply metis
   506   apply metis
   507   apply rule+
   508   apply (erule_tac x="xaa" in ballE)
   509   prefer 2
   510   apply (metis)
   511   apply (erule_tac x="ya" in ballE)
   512   prefer 2
   513   apply (metis)
   514   apply (metis in_respects)
   515   done
   516 
   517 lemma bex1_rel_aux2:
   518   "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R y\<rbrakk> \<Longrightarrow> Bex1_rel R x"
   519   unfolding Bex1_rel_def
   520   apply (erule conjE)+
   521   apply (erule bexE)
   522   apply rule
   523   apply (rule_tac x="xa" in bexI)
   524   apply metis
   525   apply metis
   526   apply rule+
   527   apply (erule_tac x="xaa" in ballE)
   528   prefer 2
   529   apply (metis)
   530   apply (erule_tac x="ya" in ballE)
   531   prefer 2
   532   apply (metis)
   533   apply (metis in_respects)
   534   done
   535 
   536 lemma bex1_rel_rsp:
   537   assumes a: "Quotient R absf repf"
   538   shows "((R ===> op =) ===> op =) (Bex1_rel R) (Bex1_rel R)"
   539   apply simp
   540   apply clarify
   541   apply rule
   542   apply (simp_all add: bex1_rel_aux bex1_rel_aux2)
   543   apply (erule bex1_rel_aux2)
   544   apply assumption
   545   done
   546 
   547 
   548 lemma ex1_prs:
   549   assumes a: "Quotient R absf repf"
   550   shows "((absf ---> id) ---> id) (Bex1_rel R) f = Ex1 f"
   551 apply simp
   552 apply (subst Bex1_rel_def)
   553 apply (subst Bex_def)
   554 apply (subst Ex1_def)
   555 apply simp
   556 apply rule
   557  apply (erule conjE)+
   558  apply (erule_tac exE)
   559  apply (erule conjE)
   560  apply (subgoal_tac "\<forall>y. R y y \<longrightarrow> f (absf y) \<longrightarrow> R x y")
   561   apply (rule_tac x="absf x" in exI)
   562   apply (simp)
   563   apply rule+
   564   using a unfolding Quotient_def
   565   apply metis
   566  apply rule+
   567  apply (erule_tac x="x" in ballE)
   568   apply (erule_tac x="y" in ballE)
   569    apply simp
   570   apply (simp add: in_respects)
   571  apply (simp add: in_respects)
   572 apply (erule_tac exE)
   573  apply rule
   574  apply (rule_tac x="repf x" in exI)
   575  apply (simp only: in_respects)
   576   apply rule
   577  apply (metis Quotient_rel_rep[OF a])
   578 using a unfolding Quotient_def apply (simp)
   579 apply rule+
   580 using a unfolding Quotient_def in_respects
   581 apply metis
   582 done
   583 
   584 lemma bex1_bexeq_reg: "(\<exists>!x\<in>Respects R. P x) \<longrightarrow> (Bex1_rel R (\<lambda>x. P x))"
   585   apply (simp add: Ex1_def Bex1_rel_def in_respects)
   586   apply clarify
   587   apply auto
   588   apply (rule bexI)
   589   apply assumption
   590   apply (simp add: in_respects)
   591   apply (simp add: in_respects)
   592   apply auto
   593   done
   594 
   595 subsection {* Various respects and preserve lemmas *}
   596 
   597 lemma quot_rel_rsp:
   598   assumes a: "Quotient R Abs Rep"
   599   shows "(R ===> R ===> op =) R R"
   600   apply(rule fun_rel_id)+
   601   apply(rule equals_rsp[OF a])
   602   apply(assumption)+
   603   done
   604 
   605 lemma o_prs:
   606   assumes q1: "Quotient R1 Abs1 Rep1"
   607   and     q2: "Quotient R2 Abs2 Rep2"
   608   and     q3: "Quotient R3 Abs3 Rep3"
   609   shows "((Abs2 ---> Rep3) ---> (Abs1 ---> Rep2) ---> (Rep1 ---> Abs3)) op \<circ> = op \<circ>"
   610   and   "(id ---> (Abs1 ---> id) ---> Rep1 ---> id) op \<circ> = op \<circ>"
   611   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2] Quotient_abs_rep[OF q3]
   612   unfolding o_def expand_fun_eq by simp_all
   613 
   614 lemma o_rsp:
   615   "((R2 ===> R3) ===> (R1 ===> R2) ===> (R1 ===> R3)) op \<circ> op \<circ>"
   616   "(op = ===> (R1 ===> op =) ===> R1 ===> op =) op \<circ> op \<circ>"
   617   unfolding fun_rel_def o_def expand_fun_eq by auto
   618 
   619 lemma cond_prs:
   620   assumes a: "Quotient R absf repf"
   621   shows "absf (if a then repf b else repf c) = (if a then b else c)"
   622   using a unfolding Quotient_def by auto
   623 
   624 lemma if_prs:
   625   assumes q: "Quotient R Abs Rep"
   626   shows "(id ---> Rep ---> Rep ---> Abs) If = If"
   627   using Quotient_abs_rep[OF q]
   628   by (auto simp add: expand_fun_eq)
   629 
   630 lemma if_rsp:
   631   assumes q: "Quotient R Abs Rep"
   632   shows "(op = ===> R ===> R ===> R) If If"
   633   by auto
   634 
   635 lemma let_prs:
   636   assumes q1: "Quotient R1 Abs1 Rep1"
   637   and     q2: "Quotient R2 Abs2 Rep2"
   638   shows "(Rep2 ---> (Abs2 ---> Rep1) ---> Abs1) Let = Let"
   639   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
   640   by (auto simp add: expand_fun_eq)
   641 
   642 lemma let_rsp:
   643   shows "(R1 ===> (R1 ===> R2) ===> R2) Let Let"
   644   by auto
   645 
   646 locale quot_type =
   647   fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   648   and   Abs :: "('a \<Rightarrow> bool) \<Rightarrow> 'b"
   649   and   Rep :: "'b \<Rightarrow> ('a \<Rightarrow> bool)"
   650   assumes equivp: "part_equivp R"
   651   and     rep_prop: "\<And>y. \<exists>x. R x x \<and> Rep y = R x"
   652   and     rep_inverse: "\<And>x. Abs (Rep x) = x"
   653   and     abs_inverse: "\<And>c. (\<exists>x. ((R x x) \<and> (c = R x))) \<Longrightarrow> (Rep (Abs c)) = c"
   654   and     rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)"
   655 begin
   656 
   657 definition
   658   abs::"'a \<Rightarrow> 'b"
   659 where
   660   "abs x \<equiv> Abs (R x)"
   661 
   662 definition
   663   rep::"'b \<Rightarrow> 'a"
   664 where
   665   "rep a = Eps (Rep a)"
   666 
   667 lemma homeier5:
   668   assumes a: "R r r"
   669   shows "Rep (Abs (R r)) = R r"
   670   apply (subst abs_inverse)
   671   using a by auto
   672 
   673 theorem homeier6:
   674   assumes a: "R r r"
   675   and b: "R s s"
   676   shows "Abs (R r) = Abs (R s) \<longleftrightarrow> R r = R s"
   677   by (metis a b homeier5)
   678 
   679 theorem homeier8:
   680   assumes "R r r"
   681   shows "R (Eps (R r)) = R r"
   682   using assms equivp[simplified part_equivp_def]
   683   apply clarify
   684   by (metis assms exE_some)
   685 
   686 lemma Quotient:
   687   shows "Quotient R abs rep"
   688   unfolding Quotient_def abs_def rep_def
   689   proof (intro conjI allI)
   690     fix a r s
   691     show "Abs (R (Eps (Rep a))) = a"
   692       by (metis equivp exE_some part_equivp_def rep_inverse rep_prop)
   693     show "R r s \<longleftrightarrow> R r r \<and> R s s \<and> (Abs (R r) = Abs (R s))"
   694       by (metis homeier6 equivp[simplified part_equivp_def])
   695     show "R (Eps (Rep a)) (Eps (Rep a))" proof -
   696       obtain x where r: "R x x" and rep: "Rep a = R x" using rep_prop[of a] by auto
   697       have "R (Eps (R x)) x" using homeier8 r by simp
   698       then have "R x (Eps (R x))" using part_equivp_symp[OF equivp] by fast
   699       then have "R (Eps (R x)) (Eps (R x))" using homeier8[OF r] by simp
   700       then show "R (Eps (Rep a)) (Eps (Rep a))" using rep by simp
   701     qed
   702   qed
   703 
   704 end
   705 
   706 
   707 subsection {* ML setup *}
   708 
   709 text {* Auxiliary data for the quotient package *}
   710 
   711 use "~~/src/HOL/Tools/Quotient/quotient_info.ML"
   712 
   713 declare [[map "fun" = (fun_map, fun_rel)]]
   714 
   715 lemmas [quot_thm] = fun_quotient
   716 lemmas [quot_respect] = quot_rel_rsp if_rsp o_rsp let_rsp
   717 lemmas [quot_preserve] = if_prs o_prs let_prs
   718 lemmas [quot_equiv] = identity_equivp
   719 
   720 
   721 text {* Lemmas about simplifying id's. *}
   722 lemmas [id_simps] =
   723   id_def[symmetric]
   724   fun_map_id
   725   id_apply
   726   id_o
   727   o_id
   728   eq_comp_r
   729 
   730 text {* Translation functions for the lifting process. *}
   731 use "~~/src/HOL/Tools/Quotient/quotient_term.ML"
   732 
   733 
   734 text {* Definitions of the quotient types. *}
   735 use "~~/src/HOL/Tools/Quotient/quotient_typ.ML"
   736 
   737 
   738 text {* Definitions for quotient constants. *}
   739 use "~~/src/HOL/Tools/Quotient/quotient_def.ML"
   740 
   741 
   742 text {*
   743   An auxiliary constant for recording some information
   744   about the lifted theorem in a tactic.
   745 *}
   746 definition
   747   "Quot_True (x :: 'a) \<equiv> True"
   748 
   749 lemma
   750   shows QT_all: "Quot_True (All P) \<Longrightarrow> Quot_True P"
   751   and   QT_ex:  "Quot_True (Ex P) \<Longrightarrow> Quot_True P"
   752   and   QT_ex1: "Quot_True (Ex1 P) \<Longrightarrow> Quot_True P"
   753   and   QT_lam: "Quot_True (\<lambda>x. P x) \<Longrightarrow> (\<And>x. Quot_True (P x))"
   754   and   QT_ext: "(\<And>x. Quot_True (a x) \<Longrightarrow> f x = g x) \<Longrightarrow> (Quot_True a \<Longrightarrow> f = g)"
   755   by (simp_all add: Quot_True_def ext)
   756 
   757 lemma QT_imp: "Quot_True a \<equiv> Quot_True b"
   758   by (simp add: Quot_True_def)
   759 
   760 
   761 text {* Tactics for proving the lifted theorems *}
   762 use "~~/src/HOL/Tools/Quotient/quotient_tacs.ML"
   763 
   764 subsection {* Methods / Interface *}
   765 
   766 method_setup lifting =
   767   {* Attrib.thms >> (fn thms => fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.lift_tac ctxt thms))) *}
   768   {* lifts theorems to quotient types *}
   769 
   770 method_setup lifting_setup =
   771   {* Attrib.thms >> (fn thms => fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.procedure_tac ctxt thms))) *}
   772   {* sets up the three goals for the quotient lifting procedure *}
   773 
   774 method_setup regularize =
   775   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.regularize_tac ctxt))) *}
   776   {* proves the regularization goals from the quotient lifting procedure *}
   777 
   778 method_setup injection =
   779   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.all_injection_tac ctxt))) *}
   780   {* proves the rep/abs injection goals from the quotient lifting procedure *}
   781 
   782 method_setup cleaning =
   783   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.clean_tac ctxt))) *}
   784   {* proves the cleaning goals from the quotient lifting procedure *}
   785 
   786 attribute_setup quot_lifted =
   787   {* Scan.succeed Quotient_Tacs.lifted_attrib *}
   788   {* lifts theorems to quotient types *}
   789 
   790 no_notation
   791   rel_conj (infixr "OOO" 75) and
   792   fun_map (infixr "--->" 55) and
   793   fun_rel (infixr "===>" 55)
   794 
   795 end
   796