src/HOL/Quotient.thy
author Cezary Kaliszyk <kaliszyk@in.tum.de>
Thu Apr 22 11:55:19 2010 +0200 (2010-04-22)
changeset 36276 92011cc923f5
parent 36215 88ff48884d26
child 37049 ca1c293e521e
permissions -rw-r--r--
fun_rel introduction and list_rel elimination for quotient package
     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 lemma fun_relI [intro]:
   110   assumes "\<And>a b. P a b \<Longrightarrow> Q (x a) (y b)"
   111   shows "(P ===> Q) x y"
   112   using assms by (simp add: fun_rel_def)
   113 
   114 lemma fun_map_id:
   115   shows "(id ---> id) = id"
   116   by (simp add: expand_fun_eq id_def)
   117 
   118 lemma fun_rel_eq:
   119   shows "((op =) ===> (op =)) = (op =)"
   120   by (simp add: expand_fun_eq)
   121 
   122 lemma fun_rel_id:
   123   assumes a: "\<And>x y. R1 x y \<Longrightarrow> R2 (f x) (g y)"
   124   shows "(R1 ===> R2) f g"
   125   using a by simp
   126 
   127 lemma fun_rel_id_asm:
   128   assumes a: "\<And>x y. R1 x y \<Longrightarrow> (A \<longrightarrow> R2 (f x) (g y))"
   129   shows "A \<longrightarrow> (R1 ===> R2) f g"
   130   using a by auto
   131 
   132 
   133 subsection {* Quotient Predicate *}
   134 
   135 definition
   136   "Quotient E Abs Rep \<equiv>
   137      (\<forall>a. Abs (Rep a) = a) \<and> (\<forall>a. E (Rep a) (Rep a)) \<and>
   138      (\<forall>r s. E r s = (E r r \<and> E s s \<and> (Abs r = Abs s)))"
   139 
   140 lemma Quotient_abs_rep:
   141   assumes a: "Quotient E Abs Rep"
   142   shows "Abs (Rep a) = a"
   143   using a
   144   unfolding Quotient_def
   145   by simp
   146 
   147 lemma Quotient_rep_reflp:
   148   assumes a: "Quotient E Abs Rep"
   149   shows "E (Rep a) (Rep a)"
   150   using a
   151   unfolding Quotient_def
   152   by blast
   153 
   154 lemma Quotient_rel:
   155   assumes a: "Quotient E Abs Rep"
   156   shows " E r s = (E r r \<and> E s s \<and> (Abs r = Abs s))"
   157   using a
   158   unfolding Quotient_def
   159   by blast
   160 
   161 lemma Quotient_rel_rep:
   162   assumes a: "Quotient R Abs Rep"
   163   shows "R (Rep a) (Rep b) = (a = b)"
   164   using a
   165   unfolding Quotient_def
   166   by metis
   167 
   168 lemma Quotient_rep_abs:
   169   assumes a: "Quotient R Abs Rep"
   170   shows "R r r \<Longrightarrow> R (Rep (Abs r)) r"
   171   using a unfolding Quotient_def
   172   by blast
   173 
   174 lemma Quotient_rel_abs:
   175   assumes a: "Quotient E Abs Rep"
   176   shows "E r s \<Longrightarrow> Abs r = Abs s"
   177   using a unfolding Quotient_def
   178   by blast
   179 
   180 lemma Quotient_symp:
   181   assumes a: "Quotient E Abs Rep"
   182   shows "symp E"
   183   using a unfolding Quotient_def symp_def
   184   by metis
   185 
   186 lemma Quotient_transp:
   187   assumes a: "Quotient E Abs Rep"
   188   shows "transp E"
   189   using a unfolding Quotient_def transp_def
   190   by metis
   191 
   192 lemma identity_quotient:
   193   shows "Quotient (op =) id id"
   194   unfolding Quotient_def id_def
   195   by blast
   196 
   197 lemma fun_quotient:
   198   assumes q1: "Quotient R1 abs1 rep1"
   199   and     q2: "Quotient R2 abs2 rep2"
   200   shows "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
   201 proof -
   202   have "\<forall>a. (rep1 ---> abs2) ((abs1 ---> rep2) a) = a"
   203     using q1 q2
   204     unfolding Quotient_def
   205     unfolding expand_fun_eq
   206     by simp
   207   moreover
   208   have "\<forall>a. (R1 ===> R2) ((abs1 ---> rep2) a) ((abs1 ---> rep2) a)"
   209     using q1 q2
   210     unfolding Quotient_def
   211     by (simp (no_asm)) (metis)
   212   moreover
   213   have "\<forall>r s. (R1 ===> R2) r s = ((R1 ===> R2) r r \<and> (R1 ===> R2) s s \<and>
   214         (rep1 ---> abs2) r  = (rep1 ---> abs2) s)"
   215     unfolding expand_fun_eq
   216     apply(auto)
   217     using q1 q2 unfolding Quotient_def
   218     apply(metis)
   219     using q1 q2 unfolding Quotient_def
   220     apply(metis)
   221     using q1 q2 unfolding Quotient_def
   222     apply(metis)
   223     using q1 q2 unfolding Quotient_def
   224     apply(metis)
   225     done
   226   ultimately
   227   show "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
   228     unfolding Quotient_def by blast
   229 qed
   230 
   231 lemma abs_o_rep:
   232   assumes a: "Quotient R Abs Rep"
   233   shows "Abs o Rep = id"
   234   unfolding expand_fun_eq
   235   by (simp add: Quotient_abs_rep[OF a])
   236 
   237 lemma equals_rsp:
   238   assumes q: "Quotient R Abs Rep"
   239   and     a: "R xa xb" "R ya yb"
   240   shows "R xa ya = R xb yb"
   241   using a Quotient_symp[OF q] Quotient_transp[OF q]
   242   unfolding symp_def transp_def
   243   by blast
   244 
   245 lemma lambda_prs:
   246   assumes q1: "Quotient R1 Abs1 Rep1"
   247   and     q2: "Quotient R2 Abs2 Rep2"
   248   shows "(Rep1 ---> Abs2) (\<lambda>x. Rep2 (f (Abs1 x))) = (\<lambda>x. f x)"
   249   unfolding expand_fun_eq
   250   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
   251   by simp
   252 
   253 lemma lambda_prs1:
   254   assumes q1: "Quotient R1 Abs1 Rep1"
   255   and     q2: "Quotient R2 Abs2 Rep2"
   256   shows "(Rep1 ---> Abs2) (\<lambda>x. (Abs1 ---> Rep2) f x) = (\<lambda>x. f x)"
   257   unfolding expand_fun_eq
   258   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
   259   by simp
   260 
   261 lemma rep_abs_rsp:
   262   assumes q: "Quotient R Abs Rep"
   263   and     a: "R x1 x2"
   264   shows "R x1 (Rep (Abs x2))"
   265   using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
   266   by metis
   267 
   268 lemma rep_abs_rsp_left:
   269   assumes q: "Quotient R Abs Rep"
   270   and     a: "R x1 x2"
   271   shows "R (Rep (Abs x1)) x2"
   272   using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
   273   by metis
   274 
   275 text{*
   276   In the following theorem R1 can be instantiated with anything,
   277   but we know some of the types of the Rep and Abs functions;
   278   so by solving Quotient assumptions we can get a unique R1 that
   279   will be provable; which is why we need to use @{text apply_rsp} and
   280   not the primed version *}
   281 
   282 lemma apply_rsp:
   283   fixes f g::"'a \<Rightarrow> 'c"
   284   assumes q: "Quotient R1 Abs1 Rep1"
   285   and     a: "(R1 ===> R2) f g" "R1 x y"
   286   shows "R2 (f x) (g y)"
   287   using a by simp
   288 
   289 lemma apply_rsp':
   290   assumes a: "(R1 ===> R2) f g" "R1 x y"
   291   shows "R2 (f x) (g y)"
   292   using a by simp
   293 
   294 subsection {* lemmas for regularisation of ball and bex *}
   295 
   296 lemma ball_reg_eqv:
   297   fixes P :: "'a \<Rightarrow> bool"
   298   assumes a: "equivp R"
   299   shows "Ball (Respects R) P = (All P)"
   300   using a
   301   unfolding equivp_def
   302   by (auto simp add: in_respects)
   303 
   304 lemma bex_reg_eqv:
   305   fixes P :: "'a \<Rightarrow> bool"
   306   assumes a: "equivp R"
   307   shows "Bex (Respects R) P = (Ex P)"
   308   using a
   309   unfolding equivp_def
   310   by (auto simp add: in_respects)
   311 
   312 lemma ball_reg_right:
   313   assumes a: "\<And>x. R x \<Longrightarrow> P x \<longrightarrow> Q x"
   314   shows "All P \<longrightarrow> Ball R Q"
   315   using a by (metis COMBC_def Collect_def Collect_mem_eq)
   316 
   317 lemma bex_reg_left:
   318   assumes a: "\<And>x. R x \<Longrightarrow> Q x \<longrightarrow> P x"
   319   shows "Bex R Q \<longrightarrow> Ex P"
   320   using a by (metis COMBC_def Collect_def Collect_mem_eq)
   321 
   322 lemma ball_reg_left:
   323   assumes a: "equivp R"
   324   shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ball (Respects R) Q \<longrightarrow> All P"
   325   using a by (metis equivp_reflp in_respects)
   326 
   327 lemma bex_reg_right:
   328   assumes a: "equivp R"
   329   shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ex Q \<longrightarrow> Bex (Respects R) P"
   330   using a by (metis equivp_reflp in_respects)
   331 
   332 lemma ball_reg_eqv_range:
   333   fixes P::"'a \<Rightarrow> bool"
   334   and x::"'a"
   335   assumes a: "equivp R2"
   336   shows   "(Ball (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = All (\<lambda>f. P (f x)))"
   337   apply(rule iffI)
   338   apply(rule allI)
   339   apply(drule_tac x="\<lambda>y. f x" in bspec)
   340   apply(simp add: in_respects)
   341   apply(rule impI)
   342   using a equivp_reflp_symp_transp[of "R2"]
   343   apply(simp add: reflp_def)
   344   apply(simp)
   345   apply(simp)
   346   done
   347 
   348 lemma bex_reg_eqv_range:
   349   assumes a: "equivp R2"
   350   shows   "(Bex (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = Ex (\<lambda>f. P (f x)))"
   351   apply(auto)
   352   apply(rule_tac x="\<lambda>y. f x" in bexI)
   353   apply(simp)
   354   apply(simp add: Respects_def in_respects)
   355   apply(rule impI)
   356   using a equivp_reflp_symp_transp[of "R2"]
   357   apply(simp add: reflp_def)
   358   done
   359 
   360 (* Next four lemmas are unused *)
   361 lemma all_reg:
   362   assumes a: "!x :: 'a. (P x --> Q x)"
   363   and     b: "All P"
   364   shows "All Q"
   365   using a b by (metis)
   366 
   367 lemma ex_reg:
   368   assumes a: "!x :: 'a. (P x --> Q x)"
   369   and     b: "Ex P"
   370   shows "Ex Q"
   371   using a b by metis
   372 
   373 lemma ball_reg:
   374   assumes a: "!x :: 'a. (R x --> P x --> Q x)"
   375   and     b: "Ball R P"
   376   shows "Ball R Q"
   377   using a b by (metis COMBC_def Collect_def Collect_mem_eq)
   378 
   379 lemma bex_reg:
   380   assumes a: "!x :: 'a. (R x --> P x --> Q x)"
   381   and     b: "Bex R P"
   382   shows "Bex R Q"
   383   using a b by (metis COMBC_def Collect_def Collect_mem_eq)
   384 
   385 
   386 lemma ball_all_comm:
   387   assumes "\<And>y. (\<forall>x\<in>P. A x y) \<longrightarrow> (\<forall>x. B x y)"
   388   shows "(\<forall>x\<in>P. \<forall>y. A x y) \<longrightarrow> (\<forall>x. \<forall>y. B x y)"
   389   using assms by auto
   390 
   391 lemma bex_ex_comm:
   392   assumes "(\<exists>y. \<exists>x. A x y) \<longrightarrow> (\<exists>y. \<exists>x\<in>P. B x y)"
   393   shows "(\<exists>x. \<exists>y. A x y) \<longrightarrow> (\<exists>x\<in>P. \<exists>y. B x y)"
   394   using assms by auto
   395 
   396 subsection {* Bounded abstraction *}
   397 
   398 definition
   399   Babs :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
   400 where
   401   "x \<in> p \<Longrightarrow> Babs p m x = m x"
   402 
   403 lemma babs_rsp:
   404   assumes q: "Quotient R1 Abs1 Rep1"
   405   and     a: "(R1 ===> R2) f g"
   406   shows      "(R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)"
   407   apply (auto simp add: Babs_def in_respects)
   408   apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
   409   using a apply (simp add: Babs_def)
   410   apply (simp add: in_respects)
   411   using Quotient_rel[OF q]
   412   by metis
   413 
   414 lemma babs_prs:
   415   assumes q1: "Quotient R1 Abs1 Rep1"
   416   and     q2: "Quotient R2 Abs2 Rep2"
   417   shows "((Rep1 ---> Abs2) (Babs (Respects R1) ((Abs1 ---> Rep2) f))) = f"
   418   apply (rule ext)
   419   apply (simp)
   420   apply (subgoal_tac "Rep1 x \<in> Respects R1")
   421   apply (simp add: Babs_def Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2])
   422   apply (simp add: in_respects Quotient_rel_rep[OF q1])
   423   done
   424 
   425 lemma babs_simp:
   426   assumes q: "Quotient R1 Abs Rep"
   427   shows "((R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)) = ((R1 ===> R2) f g)"
   428   apply(rule iffI)
   429   apply(simp_all only: babs_rsp[OF q])
   430   apply(auto simp add: Babs_def)
   431   apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
   432   apply(metis Babs_def)
   433   apply (simp add: in_respects)
   434   using Quotient_rel[OF q]
   435   by metis
   436 
   437 (* If a user proves that a particular functional relation
   438    is an equivalence this may be useful in regularising *)
   439 lemma babs_reg_eqv:
   440   shows "equivp R \<Longrightarrow> Babs (Respects R) P = P"
   441   by (simp add: expand_fun_eq Babs_def in_respects equivp_reflp)
   442 
   443 
   444 (* 3 lemmas needed for proving repabs_inj *)
   445 lemma ball_rsp:
   446   assumes a: "(R ===> (op =)) f g"
   447   shows "Ball (Respects R) f = Ball (Respects R) g"
   448   using a by (simp add: Ball_def in_respects)
   449 
   450 lemma bex_rsp:
   451   assumes a: "(R ===> (op =)) f g"
   452   shows "(Bex (Respects R) f = Bex (Respects R) g)"
   453   using a by (simp add: Bex_def in_respects)
   454 
   455 lemma bex1_rsp:
   456   assumes a: "(R ===> (op =)) f g"
   457   shows "Ex1 (\<lambda>x. x \<in> Respects R \<and> f x) = Ex1 (\<lambda>x. x \<in> Respects R \<and> g x)"
   458   using a
   459   by (simp add: Ex1_def in_respects) auto
   460 
   461 (* 2 lemmas needed for cleaning of quantifiers *)
   462 lemma all_prs:
   463   assumes a: "Quotient R absf repf"
   464   shows "Ball (Respects R) ((absf ---> id) f) = All f"
   465   using a unfolding Quotient_def Ball_def in_respects fun_map_def id_apply
   466   by metis
   467 
   468 lemma ex_prs:
   469   assumes a: "Quotient R absf repf"
   470   shows "Bex (Respects R) ((absf ---> id) f) = Ex f"
   471   using a unfolding Quotient_def Bex_def in_respects fun_map_def id_apply
   472   by metis
   473 
   474 subsection {* @{text Bex1_rel} quantifier *}
   475 
   476 definition
   477   Bex1_rel :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
   478 where
   479   "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)))"
   480 
   481 lemma bex1_rel_aux:
   482   "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R x\<rbrakk> \<Longrightarrow> Bex1_rel R y"
   483   unfolding Bex1_rel_def
   484   apply (erule conjE)+
   485   apply (erule bexE)
   486   apply rule
   487   apply (rule_tac x="xa" in bexI)
   488   apply metis
   489   apply metis
   490   apply rule+
   491   apply (erule_tac x="xaa" in ballE)
   492   prefer 2
   493   apply (metis)
   494   apply (erule_tac x="ya" in ballE)
   495   prefer 2
   496   apply (metis)
   497   apply (metis in_respects)
   498   done
   499 
   500 lemma bex1_rel_aux2:
   501   "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R y\<rbrakk> \<Longrightarrow> Bex1_rel R x"
   502   unfolding Bex1_rel_def
   503   apply (erule conjE)+
   504   apply (erule bexE)
   505   apply rule
   506   apply (rule_tac x="xa" in bexI)
   507   apply metis
   508   apply metis
   509   apply rule+
   510   apply (erule_tac x="xaa" in ballE)
   511   prefer 2
   512   apply (metis)
   513   apply (erule_tac x="ya" in ballE)
   514   prefer 2
   515   apply (metis)
   516   apply (metis in_respects)
   517   done
   518 
   519 lemma bex1_rel_rsp:
   520   assumes a: "Quotient R absf repf"
   521   shows "((R ===> op =) ===> op =) (Bex1_rel R) (Bex1_rel R)"
   522   apply simp
   523   apply clarify
   524   apply rule
   525   apply (simp_all add: bex1_rel_aux bex1_rel_aux2)
   526   apply (erule bex1_rel_aux2)
   527   apply assumption
   528   done
   529 
   530 
   531 lemma ex1_prs:
   532   assumes a: "Quotient R absf repf"
   533   shows "((absf ---> id) ---> id) (Bex1_rel R) f = Ex1 f"
   534 apply simp
   535 apply (subst Bex1_rel_def)
   536 apply (subst Bex_def)
   537 apply (subst Ex1_def)
   538 apply simp
   539 apply rule
   540  apply (erule conjE)+
   541  apply (erule_tac exE)
   542  apply (erule conjE)
   543  apply (subgoal_tac "\<forall>y. R y y \<longrightarrow> f (absf y) \<longrightarrow> R x y")
   544   apply (rule_tac x="absf x" in exI)
   545   apply (simp)
   546   apply rule+
   547   using a unfolding Quotient_def
   548   apply metis
   549  apply rule+
   550  apply (erule_tac x="x" in ballE)
   551   apply (erule_tac x="y" in ballE)
   552    apply simp
   553   apply (simp add: in_respects)
   554  apply (simp add: in_respects)
   555 apply (erule_tac exE)
   556  apply rule
   557  apply (rule_tac x="repf x" in exI)
   558  apply (simp only: in_respects)
   559   apply rule
   560  apply (metis Quotient_rel_rep[OF a])
   561 using a unfolding Quotient_def apply (simp)
   562 apply rule+
   563 using a unfolding Quotient_def in_respects
   564 apply metis
   565 done
   566 
   567 lemma bex1_bexeq_reg: "(\<exists>!x\<in>Respects R. P x) \<longrightarrow> (Bex1_rel R (\<lambda>x. P x))"
   568   apply (simp add: Ex1_def Bex1_rel_def in_respects)
   569   apply clarify
   570   apply auto
   571   apply (rule bexI)
   572   apply assumption
   573   apply (simp add: in_respects)
   574   apply (simp add: in_respects)
   575   apply auto
   576   done
   577 
   578 subsection {* Various respects and preserve lemmas *}
   579 
   580 lemma quot_rel_rsp:
   581   assumes a: "Quotient R Abs Rep"
   582   shows "(R ===> R ===> op =) R R"
   583   apply(rule fun_rel_id)+
   584   apply(rule equals_rsp[OF a])
   585   apply(assumption)+
   586   done
   587 
   588 lemma o_prs:
   589   assumes q1: "Quotient R1 Abs1 Rep1"
   590   and     q2: "Quotient R2 Abs2 Rep2"
   591   and     q3: "Quotient R3 Abs3 Rep3"
   592   shows "((Abs2 ---> Rep3) ---> (Abs1 ---> Rep2) ---> (Rep1 ---> Abs3)) op \<circ> = op \<circ>"
   593   and   "(id ---> (Abs1 ---> id) ---> Rep1 ---> id) op \<circ> = op \<circ>"
   594   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2] Quotient_abs_rep[OF q3]
   595   unfolding o_def expand_fun_eq by simp_all
   596 
   597 lemma o_rsp:
   598   "((R2 ===> R3) ===> (R1 ===> R2) ===> (R1 ===> R3)) op \<circ> op \<circ>"
   599   "(op = ===> (R1 ===> op =) ===> R1 ===> op =) op \<circ> op \<circ>"
   600   unfolding fun_rel_def o_def expand_fun_eq 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 "(id ---> Rep ---> Rep ---> Abs) If = If"
   610   using Quotient_abs_rep[OF q]
   611   by (auto simp add: expand_fun_eq)
   612 
   613 lemma if_rsp:
   614   assumes q: "Quotient R Abs Rep"
   615   shows "(op = ===> R ===> R ===> R) If If"
   616   by auto
   617 
   618 lemma let_prs:
   619   assumes q1: "Quotient R1 Abs1 Rep1"
   620   and     q2: "Quotient R2 Abs2 Rep2"
   621   shows "Abs2 (Let (Rep1 x) ((Abs1 ---> Rep2) f)) = Let x f"
   622   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2] by auto
   623 
   624 lemma let_rsp:
   625   assumes q1: "Quotient R1 Abs1 Rep1"
   626   and     a1: "(R1 ===> R2) f g"
   627   and     a2: "R1 x y"
   628   shows "R2 ((Let x f)::'c) ((Let y g)::'c)"
   629   using apply_rsp[OF q1 a1] a2 by auto
   630 
   631 locale quot_type =
   632   fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   633   and   Abs :: "('a \<Rightarrow> bool) \<Rightarrow> 'b"
   634   and   Rep :: "'b \<Rightarrow> ('a \<Rightarrow> bool)"
   635   assumes equivp: "equivp R"
   636   and     rep_prop: "\<And>y. \<exists>x. Rep y = R x"
   637   and     rep_inverse: "\<And>x. Abs (Rep x) = x"
   638   and     abs_inverse: "\<And>x. (Rep (Abs (R x))) = (R x)"
   639   and     rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)"
   640 begin
   641 
   642 definition
   643   abs::"'a \<Rightarrow> 'b"
   644 where
   645   "abs x \<equiv> Abs (R x)"
   646 
   647 definition
   648   rep::"'b \<Rightarrow> 'a"
   649 where
   650   "rep a = Eps (Rep a)"
   651 
   652 lemma homeier_lem9:
   653   shows "R (Eps (R x)) = R x"
   654 proof -
   655   have a: "R x x" using equivp by (simp add: equivp_reflp_symp_transp reflp_def)
   656   then have "R x (Eps (R x))" by (rule someI)
   657   then show "R (Eps (R x)) = R x"
   658     using equivp unfolding equivp_def by simp
   659 qed
   660 
   661 theorem homeier_thm10:
   662   shows "abs (rep a) = a"
   663   unfolding abs_def rep_def
   664 proof -
   665   from rep_prop
   666   obtain x where eq: "Rep a = R x" by auto
   667   have "Abs (R (Eps (Rep a))) = Abs (R (Eps (R x)))" using eq by simp
   668   also have "\<dots> = Abs (R x)" using homeier_lem9 by simp
   669   also have "\<dots> = Abs (Rep a)" using eq by simp
   670   also have "\<dots> = a" using rep_inverse by simp
   671   finally
   672   show "Abs (R (Eps (Rep a))) = a" by simp
   673 qed
   674 
   675 lemma homeier_lem7:
   676   shows "(R x = R y) = (Abs (R x) = Abs (R y))" (is "?LHS = ?RHS")
   677 proof -
   678   have "?RHS = (Rep (Abs (R x)) = Rep (Abs (R y)))" by (simp add: rep_inject)
   679   also have "\<dots> = ?LHS" by (simp add: abs_inverse)
   680   finally show "?LHS = ?RHS" by simp
   681 qed
   682 
   683 theorem homeier_thm11:
   684   shows "R r r' = (abs r = abs r')"
   685   unfolding abs_def
   686   by (simp only: equivp[simplified equivp_def] homeier_lem7)
   687 
   688 lemma rep_refl:
   689   shows "R (rep a) (rep a)"
   690   unfolding rep_def
   691   by (simp add: equivp[simplified equivp_def])
   692 
   693 
   694 lemma rep_abs_rsp:
   695   shows "R f (rep (abs g)) = R f g"
   696   and   "R (rep (abs g)) f = R g f"
   697   by (simp_all add: homeier_thm10 homeier_thm11)
   698 
   699 lemma Quotient:
   700   shows "Quotient R abs rep"
   701   unfolding Quotient_def
   702   apply(simp add: homeier_thm10)
   703   apply(simp add: rep_refl)
   704   apply(subst homeier_thm11[symmetric])
   705   apply(simp add: equivp[simplified equivp_def])
   706   done
   707 
   708 end
   709 
   710 subsection {* ML setup *}
   711 
   712 text {* Auxiliary data for the quotient package *}
   713 
   714 use "~~/src/HOL/Tools/Quotient/quotient_info.ML"
   715 
   716 declare [[map "fun" = (fun_map, fun_rel)]]
   717 
   718 lemmas [quot_thm] = fun_quotient
   719 lemmas [quot_respect] = quot_rel_rsp if_rsp o_rsp
   720 lemmas [quot_preserve] = if_prs o_prs
   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 :: 'a) \<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