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