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