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