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