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