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