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