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