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