src/HOL/Quotient.thy
author Christian Urban <urbanc@in.tum.de>
Sat Aug 28 20:24:40 2010 +0800 (2010-08-28)
changeset 38859 053c69cb4a0e
parent 38702 72fd257f4343
child 38861 27c7b620758c
permissions -rw-r--r--
quotient package: lemmas to be lifted and descended can be pre-simplified
     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 Sledgehammer
     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 expand_fun_eq
    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: expand_fun_eq)
   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: expand_fun_eq id_def)
   134 
   135 lemma fun_rel_eq:
   136   shows "((op =) ===> (op =)) = (op =)"
   137   by (simp add: expand_fun_eq)
   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 expand_fun_eq
   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 expand_fun_eq
   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 expand_fun_eq
   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 expand_fun_eq
   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 expand_fun_eq
   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: expand_fun_eq 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 expand_fun_eq 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 expand_fun_eq 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: expand_fun_eq)
   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: expand_fun_eq)
   649 
   650 lemma let_rsp:
   651   shows "(R1 ===> (R1 ===> R2) ===> R2) Let Let"
   652   by auto
   653 
   654 locale quot_type =
   655   fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   656   and   Abs :: "('a \<Rightarrow> bool) \<Rightarrow> 'b"
   657   and   Rep :: "'b \<Rightarrow> ('a \<Rightarrow> bool)"
   658   assumes equivp: "part_equivp R"
   659   and     rep_prop: "\<And>y. \<exists>x. R x x \<and> Rep y = R x"
   660   and     rep_inverse: "\<And>x. Abs (Rep x) = x"
   661   and     abs_inverse: "\<And>c. (\<exists>x. ((R x x) \<and> (c = R x))) \<Longrightarrow> (Rep (Abs c)) = c"
   662   and     rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)"
   663 begin
   664 
   665 definition
   666   abs::"'a \<Rightarrow> 'b"
   667 where
   668   "abs x \<equiv> Abs (R x)"
   669 
   670 definition
   671   rep::"'b \<Rightarrow> 'a"
   672 where
   673   "rep a = Eps (Rep a)"
   674 
   675 lemma homeier5:
   676   assumes a: "R r r"
   677   shows "Rep (Abs (R r)) = R r"
   678   apply (subst abs_inverse)
   679   using a by auto
   680 
   681 theorem homeier6:
   682   assumes a: "R r r"
   683   and b: "R s s"
   684   shows "Abs (R r) = Abs (R s) \<longleftrightarrow> R r = R s"
   685   by (metis a b homeier5)
   686 
   687 theorem homeier8:
   688   assumes "R r r"
   689   shows "R (Eps (R r)) = R r"
   690   using assms equivp[simplified part_equivp_def]
   691   apply clarify
   692   by (metis assms exE_some)
   693 
   694 lemma Quotient:
   695   shows "Quotient R abs rep"
   696   unfolding Quotient_def abs_def rep_def
   697   proof (intro conjI allI)
   698     fix a r s
   699     show "Abs (R (Eps (Rep a))) = a"
   700       by (metis equivp exE_some part_equivp_def rep_inverse rep_prop)
   701     show "R r s \<longleftrightarrow> R r r \<and> R s s \<and> (Abs (R r) = Abs (R s))"
   702       by (metis homeier6 equivp[simplified part_equivp_def])
   703     show "R (Eps (Rep a)) (Eps (Rep a))" proof -
   704       obtain x where r: "R x x" and rep: "Rep a = R x" using rep_prop[of a] by auto
   705       have "R (Eps (R x)) x" using homeier8 r by simp
   706       then have "R x (Eps (R x))" using part_equivp_symp[OF equivp] by fast
   707       then have "R (Eps (R x)) (Eps (R x))" using homeier8[OF r] by simp
   708       then show "R (Eps (Rep a)) (Eps (Rep a))" using rep by simp
   709     qed
   710   qed
   711 
   712 end
   713 
   714 
   715 subsection {* ML setup *}
   716 
   717 text {* Auxiliary data for the quotient package *}
   718 
   719 use "Tools/Quotient/quotient_info.ML"
   720 
   721 declare [[map "fun" = (fun_map, fun_rel)]]
   722 
   723 lemmas [quot_thm] = fun_quotient
   724 lemmas [quot_respect] = quot_rel_rsp if_rsp o_rsp let_rsp
   725 lemmas [quot_preserve] = if_prs o_prs let_prs
   726 lemmas [quot_equiv] = identity_equivp
   727 
   728 
   729 text {* Lemmas about simplifying id's. *}
   730 lemmas [id_simps] =
   731   id_def[symmetric]
   732   fun_map_id
   733   id_apply
   734   id_o
   735   o_id
   736   eq_comp_r
   737 
   738 text {* Translation functions for the lifting process. *}
   739 use "Tools/Quotient/quotient_term.ML"
   740 
   741 
   742 text {* Definitions of the quotient types. *}
   743 use "Tools/Quotient/quotient_typ.ML"
   744 
   745 
   746 text {* Definitions for quotient constants. *}
   747 use "Tools/Quotient/quotient_def.ML"
   748 
   749 
   750 text {*
   751   An auxiliary constant for recording some information
   752   about the lifted theorem in a tactic.
   753 *}
   754 definition
   755   "Quot_True (x :: 'a) \<equiv> True"
   756 
   757 lemma
   758   shows QT_all: "Quot_True (All P) \<Longrightarrow> Quot_True P"
   759   and   QT_ex:  "Quot_True (Ex P) \<Longrightarrow> Quot_True P"
   760   and   QT_ex1: "Quot_True (Ex1 P) \<Longrightarrow> Quot_True P"
   761   and   QT_lam: "Quot_True (\<lambda>x. P x) \<Longrightarrow> (\<And>x. Quot_True (P x))"
   762   and   QT_ext: "(\<And>x. Quot_True (a x) \<Longrightarrow> f x = g x) \<Longrightarrow> (Quot_True a \<Longrightarrow> f = g)"
   763   by (simp_all add: Quot_True_def ext)
   764 
   765 lemma QT_imp: "Quot_True a \<equiv> Quot_True b"
   766   by (simp add: Quot_True_def)
   767 
   768 
   769 text {* Tactics for proving the lifted theorems *}
   770 use "Tools/Quotient/quotient_tacs.ML"
   771 
   772 subsection {* Methods / Interface *}
   773 
   774 method_setup lifting =
   775   {* Attrib.thms >> (fn thms => fn ctxt => 
   776        SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.lift_tac ctxt [] thms))) *}
   777   {* lifts theorems to quotient types *}
   778 
   779 method_setup lifting_setup =
   780   {* Attrib.thm >> (fn thm => fn ctxt => 
   781        SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.lift_procedure_tac ctxt [] thm))) *}
   782   {* sets up the three goals for the quotient lifting procedure *}
   783 
   784 method_setup descending =
   785   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.descend_tac ctxt []))) *}
   786   {* decends theorems to the raw level *}
   787 
   788 method_setup descending_setup =
   789   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.descend_procedure_tac ctxt []))) *}
   790   {* sets up the three goals for the decending theorems *}
   791 
   792 method_setup regularize =
   793   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.regularize_tac ctxt))) *}
   794   {* proves the regularization goals from the quotient lifting procedure *}
   795 
   796 method_setup injection =
   797   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.all_injection_tac ctxt))) *}
   798   {* proves the rep/abs injection goals from the quotient lifting procedure *}
   799 
   800 method_setup cleaning =
   801   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.clean_tac ctxt))) *}
   802   {* proves the cleaning goals from the quotient lifting procedure *}
   803 
   804 attribute_setup quot_lifted =
   805   {* Scan.succeed Quotient_Tacs.lifted_attrib *}
   806   {* lifts theorems to quotient types *}
   807 
   808 no_notation
   809   rel_conj (infixr "OOO" 75) and
   810   fun_map (infixr "--->" 55) and
   811   fun_rel (infixr "===>" 55)
   812 
   813 end
   814