src/HOL/Quotient.thy
author haftmann
Sun Apr 15 20:51:07 2012 +0200 (2012-04-15)
changeset 47488 be6dd389639d
parent 47436 d8fad618a67a
child 47544 e455cdaac479
permissions -rw-r--r--
centralized enriched_type declaration, thanks to in-situ available Isar commands
     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 {* Respects predicate *}
    38 
    39 definition
    40   Respects :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set"
    41 where
    42   "Respects R = {x. R x x}"
    43 
    44 lemma in_respects:
    45   shows "x \<in> Respects R \<longleftrightarrow> R x x"
    46   unfolding Respects_def by simp
    47 
    48 subsection {* set map (vimage) and set relation *}
    49 
    50 definition "set_rel R xs ys \<equiv> \<forall>x y. R x y \<longrightarrow> x \<in> xs \<longleftrightarrow> y \<in> ys"
    51 
    52 lemma vimage_id:
    53   "vimage id = id"
    54   unfolding vimage_def fun_eq_iff by auto
    55 
    56 lemma set_rel_eq:
    57   "set_rel op = = op ="
    58   by (subst fun_eq_iff, subst fun_eq_iff) (simp add: set_eq_iff set_rel_def)
    59 
    60 lemma set_rel_equivp:
    61   assumes e: "equivp R"
    62   shows "set_rel R xs ys \<longleftrightarrow> xs = ys \<and> (\<forall>x y. x \<in> xs \<longrightarrow> R x y \<longrightarrow> y \<in> xs)"
    63   unfolding set_rel_def
    64   using equivp_reflp[OF e]
    65   by auto (metis, metis equivp_symp[OF e])
    66 
    67 subsection {* Quotient Predicate *}
    68 
    69 definition
    70   "Quotient3 R Abs Rep \<longleftrightarrow>
    71      (\<forall>a. Abs (Rep a) = a) \<and> (\<forall>a. R (Rep a) (Rep a)) \<and>
    72      (\<forall>r s. R r s \<longleftrightarrow> R r r \<and> R s s \<and> Abs r = Abs s)"
    73 
    74 lemma Quotient3I:
    75   assumes "\<And>a. Abs (Rep a) = a"
    76     and "\<And>a. R (Rep a) (Rep a)"
    77     and "\<And>r s. R r s \<longleftrightarrow> R r r \<and> R s s \<and> Abs r = Abs s"
    78   shows "Quotient3 R Abs Rep"
    79   using assms unfolding Quotient3_def by blast
    80 
    81 lemma Quotient3_abs_rep:
    82   assumes a: "Quotient3 R Abs Rep"
    83   shows "Abs (Rep a) = a"
    84   using a
    85   unfolding Quotient3_def
    86   by simp
    87 
    88 lemma Quotient3_rep_reflp:
    89   assumes a: "Quotient3 R Abs Rep"
    90   shows "R (Rep a) (Rep a)"
    91   using a
    92   unfolding Quotient3_def
    93   by blast
    94 
    95 lemma Quotient3_rel:
    96   assumes a: "Quotient3 R Abs Rep"
    97   shows "R r r \<and> R s s \<and> Abs r = Abs s \<longleftrightarrow> R r s" -- {* orientation does not loop on rewriting *}
    98   using a
    99   unfolding Quotient3_def
   100   by blast
   101 
   102 lemma Quotient3_refl1: 
   103   assumes a: "Quotient3 R Abs Rep" 
   104   shows "R r s \<Longrightarrow> R r r"
   105   using a unfolding Quotient3_def 
   106   by fast
   107 
   108 lemma Quotient3_refl2: 
   109   assumes a: "Quotient3 R Abs Rep" 
   110   shows "R r s \<Longrightarrow> R s s"
   111   using a unfolding Quotient3_def 
   112   by fast
   113 
   114 lemma Quotient3_rel_rep:
   115   assumes a: "Quotient3 R Abs Rep"
   116   shows "R (Rep a) (Rep b) \<longleftrightarrow> a = b"
   117   using a
   118   unfolding Quotient3_def
   119   by metis
   120 
   121 lemma Quotient3_rep_abs:
   122   assumes a: "Quotient3 R Abs Rep"
   123   shows "R r r \<Longrightarrow> R (Rep (Abs r)) r"
   124   using a unfolding Quotient3_def
   125   by blast
   126 
   127 lemma Quotient3_rel_abs:
   128   assumes a: "Quotient3 R Abs Rep"
   129   shows "R r s \<Longrightarrow> Abs r = Abs s"
   130   using a unfolding Quotient3_def
   131   by blast
   132 
   133 lemma Quotient3_symp:
   134   assumes a: "Quotient3 R Abs Rep"
   135   shows "symp R"
   136   using a unfolding Quotient3_def using sympI by metis
   137 
   138 lemma Quotient3_transp:
   139   assumes a: "Quotient3 R Abs Rep"
   140   shows "transp R"
   141   using a unfolding Quotient3_def using transpI by (metis (full_types))
   142 
   143 lemma Quotient3_part_equivp:
   144   assumes a: "Quotient3 R Abs Rep"
   145   shows "part_equivp R"
   146 by (metis Quotient3_rep_reflp Quotient3_symp Quotient3_transp a part_equivpI)
   147 
   148 lemma identity_quotient3:
   149   shows "Quotient3 (op =) id id"
   150   unfolding Quotient3_def id_def
   151   by blast
   152 
   153 lemma fun_quotient3:
   154   assumes q1: "Quotient3 R1 abs1 rep1"
   155   and     q2: "Quotient3 R2 abs2 rep2"
   156   shows "Quotient3 (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
   157 proof -
   158   have "\<And>a.(rep1 ---> abs2) ((abs1 ---> rep2) a) = a"
   159     using q1 q2 by (simp add: Quotient3_def fun_eq_iff)
   160   moreover
   161   have "\<And>a.(R1 ===> R2) ((abs1 ---> rep2) a) ((abs1 ---> rep2) a)"
   162     by (rule fun_relI)
   163       (insert q1 q2 Quotient3_rel_abs [of R1 abs1 rep1] Quotient3_rel_rep [of R2 abs2 rep2],
   164         simp (no_asm) add: Quotient3_def, simp)
   165   
   166   moreover
   167   {
   168   fix r s
   169   have "(R1 ===> R2) r s = ((R1 ===> R2) r r \<and> (R1 ===> R2) s s \<and>
   170         (rep1 ---> abs2) r  = (rep1 ---> abs2) s)"
   171   proof -
   172     
   173     have "(R1 ===> R2) r s \<Longrightarrow> (R1 ===> R2) r r" unfolding fun_rel_def
   174       using Quotient3_part_equivp[OF q1] Quotient3_part_equivp[OF q2] 
   175       by (metis (full_types) part_equivp_def)
   176     moreover have "(R1 ===> R2) r s \<Longrightarrow> (R1 ===> R2) s s" unfolding fun_rel_def
   177       using Quotient3_part_equivp[OF q1] Quotient3_part_equivp[OF q2] 
   178       by (metis (full_types) part_equivp_def)
   179     moreover have "(R1 ===> R2) r s \<Longrightarrow> (rep1 ---> abs2) r  = (rep1 ---> abs2) s"
   180       apply(auto simp add: fun_rel_def fun_eq_iff) using q1 q2 unfolding Quotient3_def by metis
   181     moreover have "((R1 ===> R2) r r \<and> (R1 ===> R2) s s \<and>
   182         (rep1 ---> abs2) r  = (rep1 ---> abs2) s) \<Longrightarrow> (R1 ===> R2) r s"
   183       apply(auto simp add: fun_rel_def fun_eq_iff) using q1 q2 unfolding Quotient3_def 
   184     by (metis map_fun_apply)
   185   
   186     ultimately show ?thesis by blast
   187  qed
   188  }
   189  ultimately show ?thesis by (intro Quotient3I) (assumption+)
   190 qed
   191 
   192 lemma abs_o_rep:
   193   assumes a: "Quotient3 R Abs Rep"
   194   shows "Abs o Rep = id"
   195   unfolding fun_eq_iff
   196   by (simp add: Quotient3_abs_rep[OF a])
   197 
   198 lemma equals_rsp:
   199   assumes q: "Quotient3 R Abs Rep"
   200   and     a: "R xa xb" "R ya yb"
   201   shows "R xa ya = R xb yb"
   202   using a Quotient3_symp[OF q] Quotient3_transp[OF q]
   203   by (blast elim: sympE transpE)
   204 
   205 lemma lambda_prs:
   206   assumes q1: "Quotient3 R1 Abs1 Rep1"
   207   and     q2: "Quotient3 R2 Abs2 Rep2"
   208   shows "(Rep1 ---> Abs2) (\<lambda>x. Rep2 (f (Abs1 x))) = (\<lambda>x. f x)"
   209   unfolding fun_eq_iff
   210   using Quotient3_abs_rep[OF q1] Quotient3_abs_rep[OF q2]
   211   by simp
   212 
   213 lemma lambda_prs1:
   214   assumes q1: "Quotient3 R1 Abs1 Rep1"
   215   and     q2: "Quotient3 R2 Abs2 Rep2"
   216   shows "(Rep1 ---> Abs2) (\<lambda>x. (Abs1 ---> Rep2) f x) = (\<lambda>x. f x)"
   217   unfolding fun_eq_iff
   218   using Quotient3_abs_rep[OF q1] Quotient3_abs_rep[OF q2]
   219   by simp
   220 
   221 lemma rep_abs_rsp:
   222   assumes q: "Quotient3 R Abs Rep"
   223   and     a: "R x1 x2"
   224   shows "R x1 (Rep (Abs x2))"
   225   using a Quotient3_rel[OF q] Quotient3_abs_rep[OF q] Quotient3_rep_reflp[OF q]
   226   by metis
   227 
   228 lemma rep_abs_rsp_left:
   229   assumes q: "Quotient3 R Abs Rep"
   230   and     a: "R x1 x2"
   231   shows "R (Rep (Abs x1)) x2"
   232   using a Quotient3_rel[OF q] Quotient3_abs_rep[OF q] Quotient3_rep_reflp[OF q]
   233   by metis
   234 
   235 text{*
   236   In the following theorem R1 can be instantiated with anything,
   237   but we know some of the types of the Rep and Abs functions;
   238   so by solving Quotient assumptions we can get a unique R1 that
   239   will be provable; which is why we need to use @{text apply_rsp} and
   240   not the primed version *}
   241 
   242 lemma apply_rspQ3:
   243   fixes f g::"'a \<Rightarrow> 'c"
   244   assumes q: "Quotient3 R1 Abs1 Rep1"
   245   and     a: "(R1 ===> R2) f g" "R1 x y"
   246   shows "R2 (f x) (g y)"
   247   using a by (auto elim: fun_relE)
   248 
   249 lemma apply_rspQ3'':
   250   assumes "Quotient3 R Abs Rep"
   251   and "(R ===> S) f f"
   252   shows "S (f (Rep x)) (f (Rep x))"
   253 proof -
   254   from assms(1) have "R (Rep x) (Rep x)" by (rule Quotient3_rep_reflp)
   255   then show ?thesis using assms(2) by (auto intro: apply_rsp')
   256 qed
   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: "Quotient3 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 Quotient3_rel[OF q]
   374   by metis
   375 
   376 lemma babs_prs:
   377   assumes q1: "Quotient3 R1 Abs1 Rep1"
   378   and     q2: "Quotient3 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 Quotient3_abs_rep[OF q1] Quotient3_abs_rep[OF q2])
   384   apply (simp add: in_respects Quotient3_rel_rep[OF q1])
   385   done
   386 
   387 lemma babs_simp:
   388   assumes q: "Quotient3 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 Quotient3_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: "Quotient3 R absf repf"
   425   shows "Ball (Respects R) ((absf ---> id) f) = All f"
   426   using a unfolding Quotient3_def Ball_def in_respects id_apply comp_def map_fun_def
   427   by metis
   428 
   429 lemma ex_prs:
   430   assumes a: "Quotient3 R absf repf"
   431   shows "Bex (Respects R) ((absf ---> id) f) = Ex f"
   432   using a unfolding Quotient3_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: "Quotient3 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: "Quotient3 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 Quotient3_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 Quotient3_rel_rep[OF a])
   522 using a unfolding Quotient3_def apply (simp)
   523 apply rule+
   524 using a unfolding Quotient3_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: "Quotient3 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: "Quotient3 R1 Abs1 Rep1"
   569   and     q2: "Quotient3 R2 Abs2 Rep2"
   570   and     q3: "Quotient3 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 Quotient3_abs_rep[OF q1] Quotient3_abs_rep[OF q2] Quotient3_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: "Quotient3 R absf repf"
   583   shows "absf (if a then repf b else repf c) = (if a then b else c)"
   584   using a unfolding Quotient3_def by auto
   585 
   586 lemma if_prs:
   587   assumes q: "Quotient3 R Abs Rep"
   588   shows "(id ---> Rep ---> Rep ---> Abs) If = If"
   589   using Quotient3_abs_rep[OF q]
   590   by (auto simp add: fun_eq_iff)
   591 
   592 lemma if_rsp:
   593   assumes q: "Quotient3 R Abs Rep"
   594   shows "(op = ===> R ===> R ===> R) If If"
   595   by force
   596 
   597 lemma let_prs:
   598   assumes q1: "Quotient3 R1 Abs1 Rep1"
   599   and     q2: "Quotient3 R2 Abs2 Rep2"
   600   shows "(Rep2 ---> (Abs2 ---> Rep1) ---> Abs1) Let = Let"
   601   using Quotient3_abs_rep[OF q1] Quotient3_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: "Quotient3 R Abs Rep"
   614   shows "(Rep ---> Abs) id = id"
   615   by (simp add: fun_eq_iff Quotient3_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 "Quotient3 R abs rep"
   647   unfolding Quotient3_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 {* Quotient composition *}
   675 
   676 lemma OOO_quotient3:
   677   fixes R1 :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   678   fixes Abs1 :: "'a \<Rightarrow> 'b" and Rep1 :: "'b \<Rightarrow> 'a"
   679   fixes Abs2 :: "'b \<Rightarrow> 'c" and Rep2 :: "'c \<Rightarrow> 'b"
   680   fixes R2' :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   681   fixes R2 :: "'b \<Rightarrow> 'b \<Rightarrow> bool"
   682   assumes R1: "Quotient3 R1 Abs1 Rep1"
   683   assumes R2: "Quotient3 R2 Abs2 Rep2"
   684   assumes Abs1: "\<And>x y. R2' x y \<Longrightarrow> R1 x x \<Longrightarrow> R1 y y \<Longrightarrow> R2 (Abs1 x) (Abs1 y)"
   685   assumes Rep1: "\<And>x y. R2 x y \<Longrightarrow> R2' (Rep1 x) (Rep1 y)"
   686   shows "Quotient3 (R1 OO R2' OO R1) (Abs2 \<circ> Abs1) (Rep1 \<circ> Rep2)"
   687 apply (rule Quotient3I)
   688    apply (simp add: o_def Quotient3_abs_rep [OF R2] Quotient3_abs_rep [OF R1])
   689   apply simp
   690   apply (rule_tac b="Rep1 (Rep2 a)" in relcomppI)
   691    apply (rule Quotient3_rep_reflp [OF R1])
   692   apply (rule_tac b="Rep1 (Rep2 a)" in relcomppI [rotated])
   693    apply (rule Quotient3_rep_reflp [OF R1])
   694   apply (rule Rep1)
   695   apply (rule Quotient3_rep_reflp [OF R2])
   696  apply safe
   697     apply (rename_tac x y)
   698     apply (drule Abs1)
   699       apply (erule Quotient3_refl2 [OF R1])
   700      apply (erule Quotient3_refl1 [OF R1])
   701     apply (drule Quotient3_refl1 [OF R2], drule Rep1)
   702     apply (subgoal_tac "R1 r (Rep1 (Abs1 x))")
   703      apply (rule_tac b="Rep1 (Abs1 x)" in relcomppI, assumption)
   704      apply (erule relcomppI)
   705      apply (erule Quotient3_symp [OF R1, THEN sympD])
   706     apply (rule Quotient3_rel[symmetric, OF R1, THEN iffD2])
   707     apply (rule conjI, erule Quotient3_refl1 [OF R1])
   708     apply (rule conjI, rule Quotient3_rep_reflp [OF R1])
   709     apply (subst Quotient3_abs_rep [OF R1])
   710     apply (erule Quotient3_rel_abs [OF R1])
   711    apply (rename_tac x y)
   712    apply (drule Abs1)
   713      apply (erule Quotient3_refl2 [OF R1])
   714     apply (erule Quotient3_refl1 [OF R1])
   715    apply (drule Quotient3_refl2 [OF R2], drule Rep1)
   716    apply (subgoal_tac "R1 s (Rep1 (Abs1 y))")
   717     apply (rule_tac b="Rep1 (Abs1 y)" in relcomppI, assumption)
   718     apply (erule relcomppI)
   719     apply (erule Quotient3_symp [OF R1, THEN sympD])
   720    apply (rule Quotient3_rel[symmetric, OF R1, THEN iffD2])
   721    apply (rule conjI, erule Quotient3_refl2 [OF R1])
   722    apply (rule conjI, rule Quotient3_rep_reflp [OF R1])
   723    apply (subst Quotient3_abs_rep [OF R1])
   724    apply (erule Quotient3_rel_abs [OF R1, THEN sym])
   725   apply simp
   726   apply (rule Quotient3_rel_abs [OF R2])
   727   apply (rule Quotient3_rel_abs [OF R1, THEN ssubst], assumption)
   728   apply (rule Quotient3_rel_abs [OF R1, THEN subst], assumption)
   729   apply (erule Abs1)
   730    apply (erule Quotient3_refl2 [OF R1])
   731   apply (erule Quotient3_refl1 [OF R1])
   732  apply (rename_tac a b c d)
   733  apply simp
   734  apply (rule_tac b="Rep1 (Abs1 r)" in relcomppI)
   735   apply (rule Quotient3_rel[symmetric, OF R1, THEN iffD2])
   736   apply (rule conjI, erule Quotient3_refl1 [OF R1])
   737   apply (simp add: Quotient3_abs_rep [OF R1] Quotient3_rep_reflp [OF R1])
   738  apply (rule_tac b="Rep1 (Abs1 s)" in relcomppI [rotated])
   739   apply (rule Quotient3_rel[symmetric, OF R1, THEN iffD2])
   740   apply (simp add: Quotient3_abs_rep [OF R1] Quotient3_rep_reflp [OF R1])
   741   apply (erule Quotient3_refl2 [OF R1])
   742  apply (rule Rep1)
   743  apply (drule Abs1)
   744    apply (erule Quotient3_refl2 [OF R1])
   745   apply (erule Quotient3_refl1 [OF R1])
   746  apply (drule Abs1)
   747   apply (erule Quotient3_refl2 [OF R1])
   748  apply (erule Quotient3_refl1 [OF R1])
   749  apply (drule Quotient3_rel_abs [OF R1])
   750  apply (drule Quotient3_rel_abs [OF R1])
   751  apply (drule Quotient3_rel_abs [OF R1])
   752  apply (drule Quotient3_rel_abs [OF R1])
   753  apply simp
   754  apply (rule Quotient3_rel[symmetric, OF R2, THEN iffD2])
   755  apply simp
   756 done
   757 
   758 lemma OOO_eq_quotient3:
   759   fixes R1 :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   760   fixes Abs1 :: "'a \<Rightarrow> 'b" and Rep1 :: "'b \<Rightarrow> 'a"
   761   fixes Abs2 :: "'b \<Rightarrow> 'c" and Rep2 :: "'c \<Rightarrow> 'b"
   762   assumes R1: "Quotient3 R1 Abs1 Rep1"
   763   assumes R2: "Quotient3 op= Abs2 Rep2"
   764   shows "Quotient3 (R1 OOO op=) (Abs2 \<circ> Abs1) (Rep1 \<circ> Rep2)"
   765 using assms
   766 by (rule OOO_quotient3) auto
   767 
   768 subsection {* Quotient3 to Quotient *}
   769 
   770 lemma Quotient3_to_Quotient:
   771 assumes "Quotient3 R Abs Rep"
   772 and "T \<equiv> \<lambda>x y. R x x \<and> Abs x = y"
   773 shows "Quotient R Abs Rep T"
   774 using assms unfolding Quotient3_def by (intro QuotientI) blast+
   775 
   776 lemma Quotient3_to_Quotient_equivp:
   777 assumes q: "Quotient3 R Abs Rep"
   778 and T_def: "T \<equiv> \<lambda>x y. Abs x = y"
   779 and eR: "equivp R"
   780 shows "Quotient R Abs Rep T"
   781 proof (intro QuotientI)
   782   fix a
   783   show "Abs (Rep a) = a" using q by(rule Quotient3_abs_rep)
   784 next
   785   fix a
   786   show "R (Rep a) (Rep a)" using q by(rule Quotient3_rep_reflp)
   787 next
   788   fix r s
   789   show "R r s = (R r r \<and> R s s \<and> Abs r = Abs s)" using q by(rule Quotient3_rel[symmetric])
   790 next
   791   show "T = (\<lambda>x y. R x x \<and> Abs x = y)" using T_def equivp_reflp[OF eR] by simp
   792 qed
   793 
   794 subsection {* ML setup *}
   795 
   796 text {* Auxiliary data for the quotient package *}
   797 
   798 use "Tools/Quotient/quotient_info.ML"
   799 setup Quotient_Info.setup
   800 
   801 declare [[mapQ3 "fun" = (fun_rel, fun_quotient3)]]
   802 
   803 lemmas [quot_thm] = fun_quotient3
   804 lemmas [quot_respect] = quot_rel_rsp if_rsp o_rsp let_rsp id_rsp
   805 lemmas [quot_preserve] = if_prs o_prs let_prs id_prs
   806 lemmas [quot_equiv] = identity_equivp
   807 
   808 
   809 text {* Lemmas about simplifying id's. *}
   810 lemmas [id_simps] =
   811   id_def[symmetric]
   812   map_fun_id
   813   id_apply
   814   id_o
   815   o_id
   816   eq_comp_r
   817   set_rel_eq
   818   vimage_id
   819 
   820 text {* Translation functions for the lifting process. *}
   821 use "Tools/Quotient/quotient_term.ML"
   822 
   823 
   824 text {* Definitions of the quotient types. *}
   825 use "Tools/Quotient/quotient_type.ML"
   826 
   827 
   828 text {* Definitions for quotient constants. *}
   829 use "Tools/Quotient/quotient_def.ML"
   830 
   831 
   832 text {*
   833   An auxiliary constant for recording some information
   834   about the lifted theorem in a tactic.
   835 *}
   836 definition
   837   Quot_True :: "'a \<Rightarrow> bool"
   838 where
   839   "Quot_True x \<longleftrightarrow> True"
   840 
   841 lemma
   842   shows QT_all: "Quot_True (All P) \<Longrightarrow> Quot_True P"
   843   and   QT_ex:  "Quot_True (Ex P) \<Longrightarrow> Quot_True P"
   844   and   QT_ex1: "Quot_True (Ex1 P) \<Longrightarrow> Quot_True P"
   845   and   QT_lam: "Quot_True (\<lambda>x. P x) \<Longrightarrow> (\<And>x. Quot_True (P x))"
   846   and   QT_ext: "(\<And>x. Quot_True (a x) \<Longrightarrow> f x = g x) \<Longrightarrow> (Quot_True a \<Longrightarrow> f = g)"
   847   by (simp_all add: Quot_True_def ext)
   848 
   849 lemma QT_imp: "Quot_True a \<equiv> Quot_True b"
   850   by (simp add: Quot_True_def)
   851 
   852 
   853 text {* Tactics for proving the lifted theorems *}
   854 use "Tools/Quotient/quotient_tacs.ML"
   855 
   856 subsection {* Methods / Interface *}
   857 
   858 method_setup lifting =
   859   {* Attrib.thms >> (fn thms => fn ctxt => 
   860        SIMPLE_METHOD' (Quotient_Tacs.lift_tac ctxt [] thms)) *}
   861   {* lift theorems to quotient types *}
   862 
   863 method_setup lifting_setup =
   864   {* Attrib.thm >> (fn thm => fn ctxt => 
   865        SIMPLE_METHOD' (Quotient_Tacs.lift_procedure_tac ctxt [] thm)) *}
   866   {* set up the three goals for the quotient lifting procedure *}
   867 
   868 method_setup descending =
   869   {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.descend_tac ctxt [])) *}
   870   {* decend theorems to the raw level *}
   871 
   872 method_setup descending_setup =
   873   {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.descend_procedure_tac ctxt [])) *}
   874   {* set up the three goals for the decending theorems *}
   875 
   876 method_setup partiality_descending =
   877   {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.partiality_descend_tac ctxt [])) *}
   878   {* decend theorems to the raw level *}
   879 
   880 method_setup partiality_descending_setup =
   881   {* Scan.succeed (fn ctxt => 
   882        SIMPLE_METHOD' (Quotient_Tacs.partiality_descend_procedure_tac ctxt [])) *}
   883   {* set up the three goals for the decending theorems *}
   884 
   885 method_setup regularize =
   886   {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.regularize_tac ctxt)) *}
   887   {* prove the regularization goals from the quotient lifting procedure *}
   888 
   889 method_setup injection =
   890   {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.all_injection_tac ctxt)) *}
   891   {* prove the rep/abs injection goals from the quotient lifting procedure *}
   892 
   893 method_setup cleaning =
   894   {* Scan.succeed (fn ctxt => SIMPLE_METHOD' (Quotient_Tacs.clean_tac ctxt)) *}
   895   {* prove the cleaning goals from the quotient lifting procedure *}
   896 
   897 attribute_setup quot_lifted =
   898   {* Scan.succeed Quotient_Tacs.lifted_attrib *}
   899   {* lift theorems to quotient types *}
   900 
   901 no_notation
   902   rel_conj (infixr "OOO" 75) and
   903   map_fun (infixr "--->" 55) and
   904   fun_rel (infixr "===>" 55)
   905 
   906 end
   907