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