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