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