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