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