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