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