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