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